Merge git://git.infradead.org/users/eparis/audit
[deliverable/linux.git] / drivers / char / agp / frontend.c
CommitLineData
1da177e4
LT
1/*
2 * AGPGART driver frontend
3 * Copyright (C) 2004 Silicon Graphics, Inc.
4 * Copyright (C) 2002-2003 Dave Jones
5 * Copyright (C) 1999 Jeff Hartmann
6 * Copyright (C) 1999 Precision Insight, Inc.
7 * Copyright (C) 1999 Xi Graphics, Inc.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28
29#include <linux/types.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/mman.h>
33#include <linux/pci.h>
1da177e4
LT
34#include <linux/miscdevice.h>
35#include <linux/agp_backend.h>
36#include <linux/agpgart.h>
37#include <linux/slab.h>
38#include <linux/mm.h>
4e950f6f 39#include <linux/fs.h>
e8edc6e0 40#include <linux/sched.h>
1da177e4
LT
41#include <asm/uaccess.h>
42#include <asm/pgtable.h>
43#include "agp.h"
44
0316fe83 45struct agp_front_data agp_fe;
1da177e4 46
0316fe83 47struct agp_memory *agp_find_mem_by_key(int key)
1da177e4
LT
48{
49 struct agp_memory *curr;
50
51 if (agp_fe.current_controller == NULL)
52 return NULL;
53
54 curr = agp_fe.current_controller->pool;
55
56 while (curr != NULL) {
57 if (curr->key == key)
58 break;
59 curr = curr->next;
60 }
61
62 DBG("key=%d -> mem=%p", key, curr);
63 return curr;
64}
65
66static void agp_remove_from_pool(struct agp_memory *temp)
67{
68 struct agp_memory *prev;
69 struct agp_memory *next;
70
71 /* Check to see if this is even in the memory pool */
72
73 DBG("mem=%p", temp);
74 if (agp_find_mem_by_key(temp->key) != NULL) {
75 next = temp->next;
76 prev = temp->prev;
77
78 if (prev != NULL) {
79 prev->next = next;
80 if (next != NULL)
81 next->prev = prev;
82
83 } else {
84 /* This is the first item on the list */
85 if (next != NULL)
86 next->prev = NULL;
87
88 agp_fe.current_controller->pool = next;
89 }
90 }
91}
92
93/*
94 * Routines for managing each client's segment list -
95 * These routines handle adding and removing segments
96 * to each auth'ed client.
97 */
98
99static struct
100agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
101 unsigned long offset,
102 int size, pgprot_t page_prot)
103{
104 struct agp_segment_priv *seg;
105 int num_segments, i;
106 off_t pg_start;
107 size_t pg_count;
108
109 pg_start = offset / 4096;
110 pg_count = size / 4096;
111 seg = *(client->segments);
112 num_segments = client->num_segments;
113
114 for (i = 0; i < client->num_segments; i++) {
115 if ((seg[i].pg_start == pg_start) &&
116 (seg[i].pg_count == pg_count) &&
117 (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
118 return seg + i;
119 }
120 }
121
122 return NULL;
123}
124
125static void agp_remove_seg_from_client(struct agp_client *client)
126{
127 DBG("client=%p", client);
128
129 if (client->segments != NULL) {
130 if (*(client->segments) != NULL) {
131 DBG("Freeing %p from client %p", *(client->segments), client);
132 kfree(*(client->segments));
133 }
134 DBG("Freeing %p from client %p", client->segments, client);
135 kfree(client->segments);
136 client->segments = NULL;
137 }
138}
139
140static void agp_add_seg_to_client(struct agp_client *client,
141 struct agp_segment_priv ** seg, int num_segments)
142{
143 struct agp_segment_priv **prev_seg;
144
145 prev_seg = client->segments;
146
147 if (prev_seg != NULL)
148 agp_remove_seg_from_client(client);
149
150 DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
151 client->num_segments = num_segments;
152 client->segments = seg;
153}
154
1da177e4
LT
155static pgprot_t agp_convert_mmap_flags(int prot)
156{
1da177e4 157 unsigned long prot_bits;
1da177e4 158
804af2cf
HD
159 prot_bits = calc_vm_prot_bits(prot) | VM_SHARED;
160 return vm_get_page_prot(prot_bits);
1da177e4
LT
161}
162
0316fe83 163int agp_create_segment(struct agp_client *client, struct agp_region *region)
1da177e4
LT
164{
165 struct agp_segment_priv **ret_seg;
166 struct agp_segment_priv *seg;
167 struct agp_segment *user_seg;
168 size_t i;
169
0ea27d9f 170 seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
1da177e4
LT
171 if (seg == NULL) {
172 kfree(region->seg_list);
173 region->seg_list = NULL;
174 return -ENOMEM;
175 }
1da177e4
LT
176 user_seg = region->seg_list;
177
178 for (i = 0; i < region->seg_count; i++) {
179 seg[i].pg_start = user_seg[i].pg_start;
180 seg[i].pg_count = user_seg[i].pg_count;
181 seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
182 }
183 kfree(region->seg_list);
184 region->seg_list = NULL;
185
186 ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
187 if (ret_seg == NULL) {
188 kfree(seg);
189 return -ENOMEM;
190 }
191 *ret_seg = seg;
192 agp_add_seg_to_client(client, ret_seg, region->seg_count);
193 return 0;
194}
195
196/* End - Routines for managing each client's segment list */
197
198/* This function must only be called when current_controller != NULL */
199static void agp_insert_into_pool(struct agp_memory * temp)
200{
201 struct agp_memory *prev;
202
203 prev = agp_fe.current_controller->pool;
204
205 if (prev != NULL) {
206 prev->prev = temp;
207 temp->next = prev;
208 }
209 agp_fe.current_controller->pool = temp;
210}
211
212
213/* File private list routines */
214
0316fe83 215struct agp_file_private *agp_find_private(pid_t pid)
1da177e4
LT
216{
217 struct agp_file_private *curr;
218
219 curr = agp_fe.file_priv_list;
220
221 while (curr != NULL) {
222 if (curr->my_pid == pid)
223 return curr;
224 curr = curr->next;
225 }
226
227 return NULL;
228}
229
408b664a 230static void agp_insert_file_private(struct agp_file_private * priv)
1da177e4
LT
231{
232 struct agp_file_private *prev;
233
234 prev = agp_fe.file_priv_list;
235
236 if (prev != NULL)
237 prev->prev = priv;
238 priv->next = prev;
239 agp_fe.file_priv_list = priv;
240}
241
408b664a 242static void agp_remove_file_private(struct agp_file_private * priv)
1da177e4
LT
243{
244 struct agp_file_private *next;
245 struct agp_file_private *prev;
246
247 next = priv->next;
248 prev = priv->prev;
249
250 if (prev != NULL) {
251 prev->next = next;
252
253 if (next != NULL)
254 next->prev = prev;
255
256 } else {
257 if (next != NULL)
258 next->prev = NULL;
259
260 agp_fe.file_priv_list = next;
261 }
262}
263
264/* End - File flag list routines */
265
266/*
267 * Wrappers for agp_free_memory & agp_allocate_memory
268 * These make sure that internal lists are kept updated.
269 */
0316fe83 270void agp_free_memory_wrap(struct agp_memory *memory)
1da177e4
LT
271{
272 agp_remove_from_pool(memory);
273 agp_free_memory(memory);
274}
275
0316fe83 276struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
1da177e4
LT
277{
278 struct agp_memory *memory;
279
280 memory = agp_allocate_memory(agp_bridge, pg_count, type);
281 if (memory == NULL)
282 return NULL;
283
284 agp_insert_into_pool(memory);
285 return memory;
286}
287
288/* Routines for managing the list of controllers -
289 * These routines manage the current controller, and the list of
290 * controllers
291 */
292
293static struct agp_controller *agp_find_controller_by_pid(pid_t id)
294{
295 struct agp_controller *controller;
296
297 controller = agp_fe.controllers;
298
299 while (controller != NULL) {
300 if (controller->pid == id)
301 return controller;
302 controller = controller->next;
303 }
304
305 return NULL;
306}
307
308static struct agp_controller *agp_create_controller(pid_t id)
309{
310 struct agp_controller *controller;
311
0ea27d9f 312 controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
1da177e4
LT
313 if (controller == NULL)
314 return NULL;
315
1da177e4 316 controller->pid = id;
1da177e4
LT
317 return controller;
318}
319
320static int agp_insert_controller(struct agp_controller *controller)
321{
322 struct agp_controller *prev_controller;
323
324 prev_controller = agp_fe.controllers;
325 controller->next = prev_controller;
326
327 if (prev_controller != NULL)
328 prev_controller->prev = controller;
329
330 agp_fe.controllers = controller;
331
332 return 0;
333}
334
335static void agp_remove_all_clients(struct agp_controller *controller)
336{
337 struct agp_client *client;
338 struct agp_client *temp;
339
340 client = controller->clients;
341
342 while (client) {
343 struct agp_file_private *priv;
344
345 temp = client;
346 agp_remove_seg_from_client(temp);
347 priv = agp_find_private(temp->pid);
348
349 if (priv != NULL) {
350 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
351 clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
352 }
353 client = client->next;
354 kfree(temp);
355 }
356}
357
358static void agp_remove_all_memory(struct agp_controller *controller)
359{
360 struct agp_memory *memory;
361 struct agp_memory *temp;
362
363 memory = controller->pool;
364
365 while (memory) {
366 temp = memory;
367 memory = memory->next;
368 agp_free_memory_wrap(temp);
369 }
370}
371
372static int agp_remove_controller(struct agp_controller *controller)
373{
374 struct agp_controller *prev_controller;
375 struct agp_controller *next_controller;
376
377 prev_controller = controller->prev;
378 next_controller = controller->next;
379
380 if (prev_controller != NULL) {
381 prev_controller->next = next_controller;
382 if (next_controller != NULL)
383 next_controller->prev = prev_controller;
384
385 } else {
386 if (next_controller != NULL)
387 next_controller->prev = NULL;
388
389 agp_fe.controllers = next_controller;
390 }
391
392 agp_remove_all_memory(controller);
393 agp_remove_all_clients(controller);
394
395 if (agp_fe.current_controller == controller) {
396 agp_fe.current_controller = NULL;
9516b030 397 agp_fe.backend_acquired = false;
1da177e4
LT
398 agp_backend_release(agp_bridge);
399 }
400 kfree(controller);
401 return 0;
402}
403
404static void agp_controller_make_current(struct agp_controller *controller)
405{
406 struct agp_client *clients;
407
408 clients = controller->clients;
409
410 while (clients != NULL) {
411 struct agp_file_private *priv;
412
413 priv = agp_find_private(clients->pid);
414
415 if (priv != NULL) {
416 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
417 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
418 }
419 clients = clients->next;
420 }
421
422 agp_fe.current_controller = controller;
423}
424
425static void agp_controller_release_current(struct agp_controller *controller,
426 struct agp_file_private *controller_priv)
427{
428 struct agp_client *clients;
429
430 clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
431 clients = controller->clients;
432
433 while (clients != NULL) {
434 struct agp_file_private *priv;
435
436 priv = agp_find_private(clients->pid);
437
438 if (priv != NULL)
439 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
440
441 clients = clients->next;
442 }
443
444 agp_fe.current_controller = NULL;
9516b030 445 agp_fe.used_by_controller = false;
1da177e4
LT
446 agp_backend_release(agp_bridge);
447}
448
449/*
450 * Routines for managing client lists -
451 * These routines are for managing the list of auth'ed clients.
452 */
453
454static struct agp_client
455*agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
456{
457 struct agp_client *client;
458
459 if (controller == NULL)
460 return NULL;
461
462 client = controller->clients;
463
464 while (client != NULL) {
465 if (client->pid == id)
466 return client;
467 client = client->next;
468 }
469
470 return NULL;
471}
472
473static struct agp_controller *agp_find_controller_for_client(pid_t id)
474{
475 struct agp_controller *controller;
476
477 controller = agp_fe.controllers;
478
479 while (controller != NULL) {
480 if ((agp_find_client_in_controller(controller, id)) != NULL)
481 return controller;
482 controller = controller->next;
483 }
484
485 return NULL;
486}
487
0316fe83 488struct agp_client *agp_find_client_by_pid(pid_t id)
1da177e4
LT
489{
490 struct agp_client *temp;
491
492 if (agp_fe.current_controller == NULL)
493 return NULL;
494
495 temp = agp_find_client_in_controller(agp_fe.current_controller, id);
496 return temp;
497}
498
499static void agp_insert_client(struct agp_client *client)
500{
501 struct agp_client *prev_client;
502
503 prev_client = agp_fe.current_controller->clients;
504 client->next = prev_client;
505
506 if (prev_client != NULL)
507 prev_client->prev = client;
508
509 agp_fe.current_controller->clients = client;
510 agp_fe.current_controller->num_clients++;
511}
512
0316fe83 513struct agp_client *agp_create_client(pid_t id)
1da177e4
LT
514{
515 struct agp_client *new_client;
516
0ea27d9f 517 new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
1da177e4
LT
518 if (new_client == NULL)
519 return NULL;
520
1da177e4
LT
521 new_client->pid = id;
522 agp_insert_client(new_client);
523 return new_client;
524}
525
0316fe83 526int agp_remove_client(pid_t id)
1da177e4
LT
527{
528 struct agp_client *client;
529 struct agp_client *prev_client;
530 struct agp_client *next_client;
531 struct agp_controller *controller;
532
533 controller = agp_find_controller_for_client(id);
534 if (controller == NULL)
535 return -EINVAL;
536
537 client = agp_find_client_in_controller(controller, id);
538 if (client == NULL)
539 return -EINVAL;
540
541 prev_client = client->prev;
542 next_client = client->next;
543
544 if (prev_client != NULL) {
545 prev_client->next = next_client;
546 if (next_client != NULL)
547 next_client->prev = prev_client;
548
549 } else {
550 if (next_client != NULL)
551 next_client->prev = NULL;
552 controller->clients = next_client;
553 }
554
555 controller->num_clients--;
556 agp_remove_seg_from_client(client);
557 kfree(client);
558 return 0;
559}
560
561/* End - Routines for managing client lists */
562
563/* File Operations */
564
565static int agp_mmap(struct file *file, struct vm_area_struct *vma)
566{
567 unsigned int size, current_size;
568 unsigned long offset;
569 struct agp_client *client;
570 struct agp_file_private *priv = file->private_data;
571 struct agp_kern_info kerninfo;
572
16867823 573 mutex_lock(&(agp_fe.agp_mutex));
1da177e4 574
9516b030 575 if (agp_fe.backend_acquired != true)
1da177e4
LT
576 goto out_eperm;
577
578 if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
579 goto out_eperm;
580
581 agp_copy_info(agp_bridge, &kerninfo);
582 size = vma->vm_end - vma->vm_start;
583 current_size = kerninfo.aper_size;
584 current_size = current_size * 0x100000;
585 offset = vma->vm_pgoff << PAGE_SHIFT;
586 DBG("%lx:%lx", offset, offset+size);
587
588 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
589 if ((size + offset) > current_size)
590 goto out_inval;
591
592 client = agp_find_client_by_pid(current->pid);
593
594 if (client == NULL)
595 goto out_eperm;
596
597 if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
598 goto out_inval;
599
600 DBG("client vm_ops=%p", kerninfo.vm_ops);
601 if (kerninfo.vm_ops) {
602 vma->vm_ops = kerninfo.vm_ops;
603 } else if (io_remap_pfn_range(vma, vma->vm_start,
604 (kerninfo.aper_base + offset) >> PAGE_SHIFT,
f435046d
AL
605 size,
606 pgprot_writecombine(vma->vm_page_prot))) {
1da177e4
LT
607 goto out_again;
608 }
16867823 609 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
610 return 0;
611 }
612
613 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
614 if (size != current_size)
615 goto out_inval;
616
617 DBG("controller vm_ops=%p", kerninfo.vm_ops);
618 if (kerninfo.vm_ops) {
619 vma->vm_ops = kerninfo.vm_ops;
620 } else if (io_remap_pfn_range(vma, vma->vm_start,
f435046d
AL
621 kerninfo.aper_base >> PAGE_SHIFT,
622 size,
623 pgprot_writecombine(vma->vm_page_prot))) {
1da177e4
LT
624 goto out_again;
625 }
16867823 626 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
627 return 0;
628 }
629
630out_eperm:
16867823 631 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
632 return -EPERM;
633
634out_inval:
16867823 635 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
636 return -EINVAL;
637
638out_again:
16867823 639 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
640 return -EAGAIN;
641}
642
643static int agp_release(struct inode *inode, struct file *file)
644{
645 struct agp_file_private *priv = file->private_data;
646
16867823 647 mutex_lock(&(agp_fe.agp_mutex));
1da177e4
LT
648
649 DBG("priv=%p", priv);
650
651 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
652 struct agp_controller *controller;
653
654 controller = agp_find_controller_by_pid(priv->my_pid);
655
656 if (controller != NULL) {
657 if (controller == agp_fe.current_controller)
658 agp_controller_release_current(controller, priv);
659 agp_remove_controller(controller);
660 controller = NULL;
661 }
662 }
663
664 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
665 agp_remove_client(priv->my_pid);
666
667 agp_remove_file_private(priv);
668 kfree(priv);
669 file->private_data = NULL;
16867823 670 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
671 return 0;
672}
673
674static int agp_open(struct inode *inode, struct file *file)
675{
676 int minor = iminor(inode);
677 struct agp_file_private *priv;
678 struct agp_client *client;
1da177e4
LT
679
680 if (minor != AGPGART_MINOR)
55e858c8
JK
681 return -ENXIO;
682
683 mutex_lock(&(agp_fe.agp_mutex));
1da177e4 684
0ea27d9f 685 priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
55e858c8
JK
686 if (priv == NULL) {
687 mutex_unlock(&(agp_fe.agp_mutex));
688 return -ENOMEM;
689 }
1da177e4 690
1da177e4
LT
691 set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
692 priv->my_pid = current->pid;
693
55e858c8 694 if (capable(CAP_SYS_RAWIO))
1da177e4
LT
695 /* Root priv, can be controller */
696 set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
55e858c8 697
1da177e4
LT
698 client = agp_find_client_by_pid(current->pid);
699
700 if (client != NULL) {
701 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
702 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
703 }
704 file->private_data = (void *) priv;
705 agp_insert_file_private(priv);
706 DBG("private=%p, client=%p", priv, client);
1da177e4 707
16867823 708 mutex_unlock(&(agp_fe.agp_mutex));
55e858c8
JK
709
710 return 0;
1da177e4
LT
711}
712
713
714static ssize_t agp_read(struct file *file, char __user *buf,
715 size_t count, loff_t * ppos)
716{
717 return -EINVAL;
718}
719
720static ssize_t agp_write(struct file *file, const char __user *buf,
721 size_t count, loff_t * ppos)
722{
723 return -EINVAL;
724}
725
726static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
727{
728 struct agp_info userinfo;
729 struct agp_kern_info kerninfo;
730
731 agp_copy_info(agp_bridge, &kerninfo);
732
733 userinfo.version.major = kerninfo.version.major;
734 userinfo.version.minor = kerninfo.version.minor;
735 userinfo.bridge_id = kerninfo.device->vendor |
736 (kerninfo.device->device << 16);
737 userinfo.agp_mode = kerninfo.mode;
738 userinfo.aper_base = kerninfo.aper_base;
739 userinfo.aper_size = kerninfo.aper_size;
740 userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
741 userinfo.pg_used = kerninfo.current_memory;
742
743 if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
744 return -EFAULT;
745
746 return 0;
747}
748
0316fe83 749int agpioc_acquire_wrap(struct agp_file_private *priv)
1da177e4
LT
750{
751 struct agp_controller *controller;
752
753 DBG("");
754
755 if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
756 return -EPERM;
757
758 if (agp_fe.current_controller != NULL)
759 return -EBUSY;
760
6a92a4e0 761 if (!agp_bridge)
1da177e4
LT
762 return -ENODEV;
763
764 if (atomic_read(&agp_bridge->agp_in_use))
765 return -EBUSY;
766
767 atomic_inc(&agp_bridge->agp_in_use);
768
9516b030 769 agp_fe.backend_acquired = true;
1da177e4
LT
770
771 controller = agp_find_controller_by_pid(priv->my_pid);
772
773 if (controller != NULL) {
774 agp_controller_make_current(controller);
775 } else {
776 controller = agp_create_controller(priv->my_pid);
777
778 if (controller == NULL) {
9516b030 779 agp_fe.backend_acquired = false;
1da177e4
LT
780 agp_backend_release(agp_bridge);
781 return -ENOMEM;
782 }
783 agp_insert_controller(controller);
784 agp_controller_make_current(controller);
785 }
786
787 set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
788 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
789 return 0;
790}
791
0316fe83 792int agpioc_release_wrap(struct agp_file_private *priv)
1da177e4
LT
793{
794 DBG("");
795 agp_controller_release_current(agp_fe.current_controller, priv);
796 return 0;
797}
798
0316fe83 799int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
1da177e4
LT
800{
801 struct agp_setup mode;
802
803 DBG("");
804 if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
805 return -EFAULT;
806
807 agp_enable(agp_bridge, mode.agp_mode);
808 return 0;
809}
810
811static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
812{
813 struct agp_region reserve;
814 struct agp_client *client;
815 struct agp_file_private *client_priv;
816
817 DBG("");
818 if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
819 return -EFAULT;
820
821 if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
822 return -EFAULT;
823
824 client = agp_find_client_by_pid(reserve.pid);
825
826 if (reserve.seg_count == 0) {
827 /* remove a client */
828 client_priv = agp_find_private(reserve.pid);
829
830 if (client_priv != NULL) {
831 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
832 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
833 }
834 if (client == NULL) {
835 /* client is already removed */
836 return 0;
837 }
838 return agp_remove_client(reserve.pid);
839 } else {
840 struct agp_segment *segment;
841
842 if (reserve.seg_count >= 16384)
843 return -EINVAL;
844
845 segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
846 GFP_KERNEL);
847
848 if (segment == NULL)
849 return -ENOMEM;
850
851 if (copy_from_user(segment, (void __user *) reserve.seg_list,
852 sizeof(struct agp_segment) * reserve.seg_count)) {
853 kfree(segment);
854 return -EFAULT;
855 }
856 reserve.seg_list = segment;
857
858 if (client == NULL) {
859 /* Create the client and add the segment */
860 client = agp_create_client(reserve.pid);
861
862 if (client == NULL) {
863 kfree(segment);
864 return -ENOMEM;
865 }
866 client_priv = agp_find_private(reserve.pid);
867
868 if (client_priv != NULL) {
869 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
870 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
871 }
872 }
873 return agp_create_segment(client, &reserve);
874 }
875 /* Will never really happen */
876 return -EINVAL;
877}
878
0316fe83 879int agpioc_protect_wrap(struct agp_file_private *priv)
1da177e4
LT
880{
881 DBG("");
882 /* This function is not currently implemented */
883 return -EINVAL;
884}
885
886static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
887{
888 struct agp_memory *memory;
889 struct agp_allocate alloc;
890
891 DBG("");
892 if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
893 return -EFAULT;
894
a030ce44
TH
895 if (alloc.type >= AGP_USER_TYPES)
896 return -EINVAL;
897
1da177e4
LT
898 memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
899
900 if (memory == NULL)
901 return -ENOMEM;
902
903 alloc.key = memory->key;
904 alloc.physical = memory->physical;
905
906 if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
907 agp_free_memory_wrap(memory);
908 return -EFAULT;
909 }
910 return 0;
911}
912
0316fe83 913int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
1da177e4
LT
914{
915 struct agp_memory *memory;
916
917 DBG("");
918 memory = agp_find_mem_by_key(arg);
919
920 if (memory == NULL)
921 return -EINVAL;
922
923 agp_free_memory_wrap(memory);
924 return 0;
925}
926
927static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
928{
929 struct agp_bind bind_info;
930 struct agp_memory *memory;
931
932 DBG("");
933 if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
934 return -EFAULT;
935
936 memory = agp_find_mem_by_key(bind_info.key);
937
938 if (memory == NULL)
939 return -EINVAL;
940
941 return agp_bind_memory(memory, bind_info.pg_start);
942}
943
944static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
945{
946 struct agp_memory *memory;
947 struct agp_unbind unbind;
948
949 DBG("");
950 if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
951 return -EFAULT;
952
953 memory = agp_find_mem_by_key(unbind.key);
954
955 if (memory == NULL)
956 return -EINVAL;
957
958 return agp_unbind_memory(memory);
959}
960
09aa356b 961static long agp_ioctl(struct file *file,
1da177e4
LT
962 unsigned int cmd, unsigned long arg)
963{
964 struct agp_file_private *curr_priv = file->private_data;
965 int ret_val = -ENOTTY;
966
967 DBG("priv=%p, cmd=%x", curr_priv, cmd);
16867823 968 mutex_lock(&(agp_fe.agp_mutex));
1da177e4
LT
969
970 if ((agp_fe.current_controller == NULL) &&
971 (cmd != AGPIOC_ACQUIRE)) {
972 ret_val = -EINVAL;
973 goto ioctl_out;
974 }
9516b030 975 if ((agp_fe.backend_acquired != true) &&
1da177e4
LT
976 (cmd != AGPIOC_ACQUIRE)) {
977 ret_val = -EBUSY;
978 goto ioctl_out;
979 }
980 if (cmd != AGPIOC_ACQUIRE) {
981 if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
982 ret_val = -EPERM;
983 goto ioctl_out;
984 }
985 /* Use the original pid of the controller,
986 * in case it's threaded */
987
988 if (agp_fe.current_controller->pid != curr_priv->my_pid) {
989 ret_val = -EBUSY;
990 goto ioctl_out;
991 }
992 }
993
994 switch (cmd) {
995 case AGPIOC_INFO:
996 ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
997 break;
998
999 case AGPIOC_ACQUIRE:
1000 ret_val = agpioc_acquire_wrap(curr_priv);
1001 break;
1002
1003 case AGPIOC_RELEASE:
1004 ret_val = agpioc_release_wrap(curr_priv);
1005 break;
1006
1007 case AGPIOC_SETUP:
1008 ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
1009 break;
1010
1011 case AGPIOC_RESERVE:
1012 ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
1013 break;
1014
1015 case AGPIOC_PROTECT:
1016 ret_val = agpioc_protect_wrap(curr_priv);
1017 break;
1018
1019 case AGPIOC_ALLOCATE:
1020 ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
1021 break;
1022
1023 case AGPIOC_DEALLOCATE:
1024 ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
1025 break;
1026
1027 case AGPIOC_BIND:
1028 ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
1029 break;
1030
1031 case AGPIOC_UNBIND:
1032 ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
1033 break;
a13af4b4
DA
1034
1035 case AGPIOC_CHIPSET_FLUSH:
a13af4b4 1036 break;
1da177e4
LT
1037 }
1038
1039ioctl_out:
1040 DBG("ioctl returns %d\n", ret_val);
16867823 1041 mutex_unlock(&(agp_fe.agp_mutex));
1da177e4
LT
1042 return ret_val;
1043}
1044
62322d25 1045static const struct file_operations agp_fops =
1da177e4
LT
1046{
1047 .owner = THIS_MODULE,
1048 .llseek = no_llseek,
1049 .read = agp_read,
1050 .write = agp_write,
09aa356b 1051 .unlocked_ioctl = agp_ioctl,
0316fe83
ZM
1052#ifdef CONFIG_COMPAT
1053 .compat_ioctl = compat_agp_ioctl,
1054#endif
1da177e4
LT
1055 .mmap = agp_mmap,
1056 .open = agp_open,
1057 .release = agp_release,
1058};
1059
1060static struct miscdevice agp_miscdev =
1061{
1062 .minor = AGPGART_MINOR,
1063 .name = "agpgart",
1064 .fops = &agp_fops
1065};
1066
1067int agp_frontend_initialize(void)
1068{
1069 memset(&agp_fe, 0, sizeof(struct agp_front_data));
16867823 1070 mutex_init(&(agp_fe.agp_mutex));
1da177e4
LT
1071
1072 if (misc_register(&agp_miscdev)) {
1073 printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
1074 return -EIO;
1075 }
1076 return 0;
1077}
1078
1079void agp_frontend_cleanup(void)
1080{
1081 misc_deregister(&agp_miscdev);
1082}
This page took 1.039266 seconds and 5 git commands to generate.