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