Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / agp / frontend.c
blobf633623ac802f18b621030782eae4b8b3700ea0a
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.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
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>
34 #include <linux/init.h>
35 #include <linux/miscdevice.h>
36 #include <linux/agp_backend.h>
37 #include <linux/agpgart.h>
38 #include <linux/slab.h>
39 #include <linux/mm.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
42 #include "agp.h"
44 static struct agp_front_data agp_fe;
46 static struct agp_memory *agp_find_mem_by_key(int key)
48 struct agp_memory *curr;
50 if (agp_fe.current_controller == NULL)
51 return NULL;
53 curr = agp_fe.current_controller->pool;
55 while (curr != NULL) {
56 if (curr->key == key)
57 break;
58 curr = curr->next;
61 DBG("key=%d -> mem=%p", key, curr);
62 return curr;
65 static void agp_remove_from_pool(struct agp_memory *temp)
67 struct agp_memory *prev;
68 struct agp_memory *next;
70 /* Check to see if this is even in the memory pool */
72 DBG("mem=%p", temp);
73 if (agp_find_mem_by_key(temp->key) != NULL) {
74 next = temp->next;
75 prev = temp->prev;
77 if (prev != NULL) {
78 prev->next = next;
79 if (next != NULL)
80 next->prev = prev;
82 } else {
83 /* This is the first item on the list */
84 if (next != NULL)
85 next->prev = NULL;
87 agp_fe.current_controller->pool = next;
93 * Routines for managing each client's segment list -
94 * These routines handle adding and removing segments
95 * to each auth'ed client.
98 static struct
99 agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
100 unsigned long offset,
101 int size, pgprot_t page_prot)
103 struct agp_segment_priv *seg;
104 int num_segments, i;
105 off_t pg_start;
106 size_t pg_count;
108 pg_start = offset / 4096;
109 pg_count = size / 4096;
110 seg = *(client->segments);
111 num_segments = client->num_segments;
113 for (i = 0; i < client->num_segments; i++) {
114 if ((seg[i].pg_start == pg_start) &&
115 (seg[i].pg_count == pg_count) &&
116 (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
117 return seg + i;
121 return NULL;
124 static void agp_remove_seg_from_client(struct agp_client *client)
126 DBG("client=%p", client);
128 if (client->segments != NULL) {
129 if (*(client->segments) != NULL) {
130 DBG("Freeing %p from client %p", *(client->segments), client);
131 kfree(*(client->segments));
133 DBG("Freeing %p from client %p", client->segments, client);
134 kfree(client->segments);
135 client->segments = NULL;
139 static void agp_add_seg_to_client(struct agp_client *client,
140 struct agp_segment_priv ** seg, int num_segments)
142 struct agp_segment_priv **prev_seg;
144 prev_seg = client->segments;
146 if (prev_seg != NULL)
147 agp_remove_seg_from_client(client);
149 DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
150 client->num_segments = num_segments;
151 client->segments = seg;
154 /* Originally taken from linux/mm/mmap.c from the array
155 * protection_map.
156 * The original really should be exported to modules, or
157 * some routine which does the conversion for you
160 static const pgprot_t my_protect_map[16] =
162 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
163 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
166 static pgprot_t agp_convert_mmap_flags(int prot)
168 #define _trans(x,bit1,bit2) \
169 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
171 unsigned long prot_bits;
172 pgprot_t temp;
174 prot_bits = _trans(prot, PROT_READ, VM_READ) |
175 _trans(prot, PROT_WRITE, VM_WRITE) |
176 _trans(prot, PROT_EXEC, VM_EXEC);
178 prot_bits |= VM_SHARED;
180 temp = my_protect_map[prot_bits & 0x0000000f];
182 return temp;
185 static int agp_create_segment(struct agp_client *client, struct agp_region *region)
187 struct agp_segment_priv **ret_seg;
188 struct agp_segment_priv *seg;
189 struct agp_segment *user_seg;
190 size_t i;
192 seg = kmalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
193 if (seg == NULL) {
194 kfree(region->seg_list);
195 region->seg_list = NULL;
196 return -ENOMEM;
198 memset(seg, 0, (sizeof(struct agp_segment_priv) * region->seg_count));
199 user_seg = region->seg_list;
201 for (i = 0; i < region->seg_count; i++) {
202 seg[i].pg_start = user_seg[i].pg_start;
203 seg[i].pg_count = user_seg[i].pg_count;
204 seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
206 kfree(region->seg_list);
207 region->seg_list = NULL;
209 ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
210 if (ret_seg == NULL) {
211 kfree(seg);
212 return -ENOMEM;
214 *ret_seg = seg;
215 agp_add_seg_to_client(client, ret_seg, region->seg_count);
216 return 0;
219 /* End - Routines for managing each client's segment list */
221 /* This function must only be called when current_controller != NULL */
222 static void agp_insert_into_pool(struct agp_memory * temp)
224 struct agp_memory *prev;
226 prev = agp_fe.current_controller->pool;
228 if (prev != NULL) {
229 prev->prev = temp;
230 temp->next = prev;
232 agp_fe.current_controller->pool = temp;
236 /* File private list routines */
238 struct agp_file_private *agp_find_private(pid_t pid)
240 struct agp_file_private *curr;
242 curr = agp_fe.file_priv_list;
244 while (curr != NULL) {
245 if (curr->my_pid == pid)
246 return curr;
247 curr = curr->next;
250 return NULL;
253 void agp_insert_file_private(struct agp_file_private * priv)
255 struct agp_file_private *prev;
257 prev = agp_fe.file_priv_list;
259 if (prev != NULL)
260 prev->prev = priv;
261 priv->next = prev;
262 agp_fe.file_priv_list = priv;
265 void agp_remove_file_private(struct agp_file_private * priv)
267 struct agp_file_private *next;
268 struct agp_file_private *prev;
270 next = priv->next;
271 prev = priv->prev;
273 if (prev != NULL) {
274 prev->next = next;
276 if (next != NULL)
277 next->prev = prev;
279 } else {
280 if (next != NULL)
281 next->prev = NULL;
283 agp_fe.file_priv_list = next;
287 /* End - File flag list routines */
290 * Wrappers for agp_free_memory & agp_allocate_memory
291 * These make sure that internal lists are kept updated.
293 static void agp_free_memory_wrap(struct agp_memory *memory)
295 agp_remove_from_pool(memory);
296 agp_free_memory(memory);
299 static struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
301 struct agp_memory *memory;
303 memory = agp_allocate_memory(agp_bridge, pg_count, type);
304 if (memory == NULL)
305 return NULL;
307 agp_insert_into_pool(memory);
308 return memory;
311 /* Routines for managing the list of controllers -
312 * These routines manage the current controller, and the list of
313 * controllers
316 static struct agp_controller *agp_find_controller_by_pid(pid_t id)
318 struct agp_controller *controller;
320 controller = agp_fe.controllers;
322 while (controller != NULL) {
323 if (controller->pid == id)
324 return controller;
325 controller = controller->next;
328 return NULL;
331 static struct agp_controller *agp_create_controller(pid_t id)
333 struct agp_controller *controller;
335 controller = kmalloc(sizeof(struct agp_controller), GFP_KERNEL);
337 if (controller == NULL)
338 return NULL;
340 memset(controller, 0, sizeof(struct agp_controller));
341 controller->pid = id;
343 return controller;
346 static int agp_insert_controller(struct agp_controller *controller)
348 struct agp_controller *prev_controller;
350 prev_controller = agp_fe.controllers;
351 controller->next = prev_controller;
353 if (prev_controller != NULL)
354 prev_controller->prev = controller;
356 agp_fe.controllers = controller;
358 return 0;
361 static void agp_remove_all_clients(struct agp_controller *controller)
363 struct agp_client *client;
364 struct agp_client *temp;
366 client = controller->clients;
368 while (client) {
369 struct agp_file_private *priv;
371 temp = client;
372 agp_remove_seg_from_client(temp);
373 priv = agp_find_private(temp->pid);
375 if (priv != NULL) {
376 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
377 clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
379 client = client->next;
380 kfree(temp);
384 static void agp_remove_all_memory(struct agp_controller *controller)
386 struct agp_memory *memory;
387 struct agp_memory *temp;
389 memory = controller->pool;
391 while (memory) {
392 temp = memory;
393 memory = memory->next;
394 agp_free_memory_wrap(temp);
398 static int agp_remove_controller(struct agp_controller *controller)
400 struct agp_controller *prev_controller;
401 struct agp_controller *next_controller;
403 prev_controller = controller->prev;
404 next_controller = controller->next;
406 if (prev_controller != NULL) {
407 prev_controller->next = next_controller;
408 if (next_controller != NULL)
409 next_controller->prev = prev_controller;
411 } else {
412 if (next_controller != NULL)
413 next_controller->prev = NULL;
415 agp_fe.controllers = next_controller;
418 agp_remove_all_memory(controller);
419 agp_remove_all_clients(controller);
421 if (agp_fe.current_controller == controller) {
422 agp_fe.current_controller = NULL;
423 agp_fe.backend_acquired = FALSE;
424 agp_backend_release(agp_bridge);
426 kfree(controller);
427 return 0;
430 static void agp_controller_make_current(struct agp_controller *controller)
432 struct agp_client *clients;
434 clients = controller->clients;
436 while (clients != NULL) {
437 struct agp_file_private *priv;
439 priv = agp_find_private(clients->pid);
441 if (priv != NULL) {
442 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
443 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
445 clients = clients->next;
448 agp_fe.current_controller = controller;
451 static void agp_controller_release_current(struct agp_controller *controller,
452 struct agp_file_private *controller_priv)
454 struct agp_client *clients;
456 clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
457 clients = controller->clients;
459 while (clients != NULL) {
460 struct agp_file_private *priv;
462 priv = agp_find_private(clients->pid);
464 if (priv != NULL)
465 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
467 clients = clients->next;
470 agp_fe.current_controller = NULL;
471 agp_fe.used_by_controller = FALSE;
472 agp_backend_release(agp_bridge);
476 * Routines for managing client lists -
477 * These routines are for managing the list of auth'ed clients.
480 static struct agp_client
481 *agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
483 struct agp_client *client;
485 if (controller == NULL)
486 return NULL;
488 client = controller->clients;
490 while (client != NULL) {
491 if (client->pid == id)
492 return client;
493 client = client->next;
496 return NULL;
499 static struct agp_controller *agp_find_controller_for_client(pid_t id)
501 struct agp_controller *controller;
503 controller = agp_fe.controllers;
505 while (controller != NULL) {
506 if ((agp_find_client_in_controller(controller, id)) != NULL)
507 return controller;
508 controller = controller->next;
511 return NULL;
514 static struct agp_client *agp_find_client_by_pid(pid_t id)
516 struct agp_client *temp;
518 if (agp_fe.current_controller == NULL)
519 return NULL;
521 temp = agp_find_client_in_controller(agp_fe.current_controller, id);
522 return temp;
525 static void agp_insert_client(struct agp_client *client)
527 struct agp_client *prev_client;
529 prev_client = agp_fe.current_controller->clients;
530 client->next = prev_client;
532 if (prev_client != NULL)
533 prev_client->prev = client;
535 agp_fe.current_controller->clients = client;
536 agp_fe.current_controller->num_clients++;
539 static struct agp_client *agp_create_client(pid_t id)
541 struct agp_client *new_client;
543 new_client = kmalloc(sizeof(struct agp_client), GFP_KERNEL);
545 if (new_client == NULL)
546 return NULL;
548 memset(new_client, 0, sizeof(struct agp_client));
549 new_client->pid = id;
550 agp_insert_client(new_client);
551 return new_client;
554 static int agp_remove_client(pid_t id)
556 struct agp_client *client;
557 struct agp_client *prev_client;
558 struct agp_client *next_client;
559 struct agp_controller *controller;
561 controller = agp_find_controller_for_client(id);
562 if (controller == NULL)
563 return -EINVAL;
565 client = agp_find_client_in_controller(controller, id);
566 if (client == NULL)
567 return -EINVAL;
569 prev_client = client->prev;
570 next_client = client->next;
572 if (prev_client != NULL) {
573 prev_client->next = next_client;
574 if (next_client != NULL)
575 next_client->prev = prev_client;
577 } else {
578 if (next_client != NULL)
579 next_client->prev = NULL;
580 controller->clients = next_client;
583 controller->num_clients--;
584 agp_remove_seg_from_client(client);
585 kfree(client);
586 return 0;
589 /* End - Routines for managing client lists */
591 /* File Operations */
593 static int agp_mmap(struct file *file, struct vm_area_struct *vma)
595 unsigned int size, current_size;
596 unsigned long offset;
597 struct agp_client *client;
598 struct agp_file_private *priv = file->private_data;
599 struct agp_kern_info kerninfo;
601 down(&(agp_fe.agp_mutex));
603 if (agp_fe.backend_acquired != TRUE)
604 goto out_eperm;
606 if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
607 goto out_eperm;
609 agp_copy_info(agp_bridge, &kerninfo);
610 size = vma->vm_end - vma->vm_start;
611 current_size = kerninfo.aper_size;
612 current_size = current_size * 0x100000;
613 offset = vma->vm_pgoff << PAGE_SHIFT;
614 DBG("%lx:%lx", offset, offset+size);
616 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
617 if ((size + offset) > current_size)
618 goto out_inval;
620 client = agp_find_client_by_pid(current->pid);
622 if (client == NULL)
623 goto out_eperm;
625 if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
626 goto out_inval;
628 DBG("client vm_ops=%p", kerninfo.vm_ops);
629 if (kerninfo.vm_ops) {
630 vma->vm_ops = kerninfo.vm_ops;
631 } else if (io_remap_pfn_range(vma, vma->vm_start,
632 (kerninfo.aper_base + offset) >> PAGE_SHIFT,
633 size, vma->vm_page_prot)) {
634 goto out_again;
636 up(&(agp_fe.agp_mutex));
637 return 0;
640 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
641 if (size != current_size)
642 goto out_inval;
644 DBG("controller vm_ops=%p", kerninfo.vm_ops);
645 if (kerninfo.vm_ops) {
646 vma->vm_ops = kerninfo.vm_ops;
647 } else if (io_remap_pfn_range(vma, vma->vm_start,
648 kerninfo.aper_base >> PAGE_SHIFT,
649 size, vma->vm_page_prot)) {
650 goto out_again;
652 up(&(agp_fe.agp_mutex));
653 return 0;
656 out_eperm:
657 up(&(agp_fe.agp_mutex));
658 return -EPERM;
660 out_inval:
661 up(&(agp_fe.agp_mutex));
662 return -EINVAL;
664 out_again:
665 up(&(agp_fe.agp_mutex));
666 return -EAGAIN;
669 static int agp_release(struct inode *inode, struct file *file)
671 struct agp_file_private *priv = file->private_data;
673 down(&(agp_fe.agp_mutex));
675 DBG("priv=%p", priv);
677 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
678 struct agp_controller *controller;
680 controller = agp_find_controller_by_pid(priv->my_pid);
682 if (controller != NULL) {
683 if (controller == agp_fe.current_controller)
684 agp_controller_release_current(controller, priv);
685 agp_remove_controller(controller);
686 controller = NULL;
690 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
691 agp_remove_client(priv->my_pid);
693 agp_remove_file_private(priv);
694 kfree(priv);
695 file->private_data = NULL;
696 up(&(agp_fe.agp_mutex));
697 return 0;
700 static int agp_open(struct inode *inode, struct file *file)
702 int minor = iminor(inode);
703 struct agp_file_private *priv;
704 struct agp_client *client;
705 int rc = -ENXIO;
707 down(&(agp_fe.agp_mutex));
709 if (minor != AGPGART_MINOR)
710 goto err_out;
712 priv = kmalloc(sizeof(struct agp_file_private), GFP_KERNEL);
713 if (priv == NULL)
714 goto err_out_nomem;
716 memset(priv, 0, sizeof(struct agp_file_private));
717 set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
718 priv->my_pid = current->pid;
720 if ((current->uid == 0) || (current->suid == 0)) {
721 /* Root priv, can be controller */
722 set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
724 client = agp_find_client_by_pid(current->pid);
726 if (client != NULL) {
727 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
728 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
730 file->private_data = (void *) priv;
731 agp_insert_file_private(priv);
732 DBG("private=%p, client=%p", priv, client);
733 up(&(agp_fe.agp_mutex));
734 return 0;
736 err_out_nomem:
737 rc = -ENOMEM;
738 err_out:
739 up(&(agp_fe.agp_mutex));
740 return rc;
744 static ssize_t agp_read(struct file *file, char __user *buf,
745 size_t count, loff_t * ppos)
747 return -EINVAL;
750 static ssize_t agp_write(struct file *file, const char __user *buf,
751 size_t count, loff_t * ppos)
753 return -EINVAL;
756 static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
758 struct agp_info userinfo;
759 struct agp_kern_info kerninfo;
761 agp_copy_info(agp_bridge, &kerninfo);
763 userinfo.version.major = kerninfo.version.major;
764 userinfo.version.minor = kerninfo.version.minor;
765 userinfo.bridge_id = kerninfo.device->vendor |
766 (kerninfo.device->device << 16);
767 userinfo.agp_mode = kerninfo.mode;
768 userinfo.aper_base = kerninfo.aper_base;
769 userinfo.aper_size = kerninfo.aper_size;
770 userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
771 userinfo.pg_used = kerninfo.current_memory;
773 if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
774 return -EFAULT;
776 return 0;
779 static int agpioc_acquire_wrap(struct agp_file_private *priv)
781 struct agp_controller *controller;
783 DBG("");
785 if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
786 return -EPERM;
788 if (agp_fe.current_controller != NULL)
789 return -EBUSY;
791 if(!agp_bridge)
792 return -ENODEV;
794 if (atomic_read(&agp_bridge->agp_in_use))
795 return -EBUSY;
797 atomic_inc(&agp_bridge->agp_in_use);
799 agp_fe.backend_acquired = TRUE;
801 controller = agp_find_controller_by_pid(priv->my_pid);
803 if (controller != NULL) {
804 agp_controller_make_current(controller);
805 } else {
806 controller = agp_create_controller(priv->my_pid);
808 if (controller == NULL) {
809 agp_fe.backend_acquired = FALSE;
810 agp_backend_release(agp_bridge);
811 return -ENOMEM;
813 agp_insert_controller(controller);
814 agp_controller_make_current(controller);
817 set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
818 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
819 return 0;
822 static int agpioc_release_wrap(struct agp_file_private *priv)
824 DBG("");
825 agp_controller_release_current(agp_fe.current_controller, priv);
826 return 0;
829 static int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
831 struct agp_setup mode;
833 DBG("");
834 if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
835 return -EFAULT;
837 agp_enable(agp_bridge, mode.agp_mode);
838 return 0;
841 static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
843 struct agp_region reserve;
844 struct agp_client *client;
845 struct agp_file_private *client_priv;
847 DBG("");
848 if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
849 return -EFAULT;
851 if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
852 return -EFAULT;
854 client = agp_find_client_by_pid(reserve.pid);
856 if (reserve.seg_count == 0) {
857 /* remove a client */
858 client_priv = agp_find_private(reserve.pid);
860 if (client_priv != NULL) {
861 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
862 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
864 if (client == NULL) {
865 /* client is already removed */
866 return 0;
868 return agp_remove_client(reserve.pid);
869 } else {
870 struct agp_segment *segment;
872 if (reserve.seg_count >= 16384)
873 return -EINVAL;
875 segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
876 GFP_KERNEL);
878 if (segment == NULL)
879 return -ENOMEM;
881 if (copy_from_user(segment, (void __user *) reserve.seg_list,
882 sizeof(struct agp_segment) * reserve.seg_count)) {
883 kfree(segment);
884 return -EFAULT;
886 reserve.seg_list = segment;
888 if (client == NULL) {
889 /* Create the client and add the segment */
890 client = agp_create_client(reserve.pid);
892 if (client == NULL) {
893 kfree(segment);
894 return -ENOMEM;
896 client_priv = agp_find_private(reserve.pid);
898 if (client_priv != NULL) {
899 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
900 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
903 return agp_create_segment(client, &reserve);
905 /* Will never really happen */
906 return -EINVAL;
909 static int agpioc_protect_wrap(struct agp_file_private *priv)
911 DBG("");
912 /* This function is not currently implemented */
913 return -EINVAL;
916 static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
918 struct agp_memory *memory;
919 struct agp_allocate alloc;
921 DBG("");
922 if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
923 return -EFAULT;
925 memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
927 if (memory == NULL)
928 return -ENOMEM;
930 alloc.key = memory->key;
931 alloc.physical = memory->physical;
933 if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
934 agp_free_memory_wrap(memory);
935 return -EFAULT;
937 return 0;
940 static int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
942 struct agp_memory *memory;
944 DBG("");
945 memory = agp_find_mem_by_key(arg);
947 if (memory == NULL)
948 return -EINVAL;
950 agp_free_memory_wrap(memory);
951 return 0;
954 static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
956 struct agp_bind bind_info;
957 struct agp_memory *memory;
959 DBG("");
960 if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
961 return -EFAULT;
963 memory = agp_find_mem_by_key(bind_info.key);
965 if (memory == NULL)
966 return -EINVAL;
968 return agp_bind_memory(memory, bind_info.pg_start);
971 static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
973 struct agp_memory *memory;
974 struct agp_unbind unbind;
976 DBG("");
977 if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
978 return -EFAULT;
980 memory = agp_find_mem_by_key(unbind.key);
982 if (memory == NULL)
983 return -EINVAL;
985 return agp_unbind_memory(memory);
988 static int agp_ioctl(struct inode *inode, struct file *file,
989 unsigned int cmd, unsigned long arg)
991 struct agp_file_private *curr_priv = file->private_data;
992 int ret_val = -ENOTTY;
994 DBG("priv=%p, cmd=%x", curr_priv, cmd);
995 down(&(agp_fe.agp_mutex));
997 if ((agp_fe.current_controller == NULL) &&
998 (cmd != AGPIOC_ACQUIRE)) {
999 ret_val = -EINVAL;
1000 goto ioctl_out;
1002 if ((agp_fe.backend_acquired != TRUE) &&
1003 (cmd != AGPIOC_ACQUIRE)) {
1004 ret_val = -EBUSY;
1005 goto ioctl_out;
1007 if (cmd != AGPIOC_ACQUIRE) {
1008 if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
1009 ret_val = -EPERM;
1010 goto ioctl_out;
1012 /* Use the original pid of the controller,
1013 * in case it's threaded */
1015 if (agp_fe.current_controller->pid != curr_priv->my_pid) {
1016 ret_val = -EBUSY;
1017 goto ioctl_out;
1021 switch (cmd) {
1022 case AGPIOC_INFO:
1023 ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
1024 break;
1026 case AGPIOC_ACQUIRE:
1027 ret_val = agpioc_acquire_wrap(curr_priv);
1028 break;
1030 case AGPIOC_RELEASE:
1031 ret_val = agpioc_release_wrap(curr_priv);
1032 break;
1034 case AGPIOC_SETUP:
1035 ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
1036 break;
1038 case AGPIOC_RESERVE:
1039 ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
1040 break;
1042 case AGPIOC_PROTECT:
1043 ret_val = agpioc_protect_wrap(curr_priv);
1044 break;
1046 case AGPIOC_ALLOCATE:
1047 ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
1048 break;
1050 case AGPIOC_DEALLOCATE:
1051 ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
1052 break;
1054 case AGPIOC_BIND:
1055 ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
1056 break;
1058 case AGPIOC_UNBIND:
1059 ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
1060 break;
1063 ioctl_out:
1064 DBG("ioctl returns %d\n", ret_val);
1065 up(&(agp_fe.agp_mutex));
1066 return ret_val;
1069 static struct file_operations agp_fops =
1071 .owner = THIS_MODULE,
1072 .llseek = no_llseek,
1073 .read = agp_read,
1074 .write = agp_write,
1075 .ioctl = agp_ioctl,
1076 .mmap = agp_mmap,
1077 .open = agp_open,
1078 .release = agp_release,
1081 static struct miscdevice agp_miscdev =
1083 .minor = AGPGART_MINOR,
1084 .name = "agpgart",
1085 .fops = &agp_fops
1088 int agp_frontend_initialize(void)
1090 memset(&agp_fe, 0, sizeof(struct agp_front_data));
1091 sema_init(&(agp_fe.agp_mutex), 1);
1093 if (misc_register(&agp_miscdev)) {
1094 printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
1095 return -EIO;
1097 return 0;
1100 void agp_frontend_cleanup(void)
1102 misc_deregister(&agp_miscdev);