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>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.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
)
53 curr
= agp_fe
.current_controller
->pool
;
55 while (curr
!= NULL
) {
61 DBG("key=%d -> mem=%p", key
, 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 */
73 if (agp_find_mem_by_key(temp
->key
) != NULL
) {
83 /* This is the first item on the list */
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.
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
;
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
))) {
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
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
;
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];
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
;
192 seg
= kmalloc((sizeof(struct agp_segment_priv
) * region
->seg_count
), GFP_KERNEL
);
194 kfree(region
->seg_list
);
195 region
->seg_list
= NULL
;
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
) {
215 agp_add_seg_to_client(client
, ret_seg
, region
->seg_count
);
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
;
232 agp_fe
.current_controller
->pool
= temp
;
236 /* File private list routines */
238 static 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
)
253 static void agp_insert_file_private(struct agp_file_private
* priv
)
255 struct agp_file_private
*prev
;
257 prev
= agp_fe
.file_priv_list
;
262 agp_fe
.file_priv_list
= priv
;
265 static void agp_remove_file_private(struct agp_file_private
* priv
)
267 struct agp_file_private
*next
;
268 struct agp_file_private
*prev
;
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
);
307 agp_insert_into_pool(memory
);
311 /* Routines for managing the list of controllers -
312 * These routines manage the current controller, and the list of
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
)
325 controller
= controller
->next
;
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
)
340 memset(controller
, 0, sizeof(struct agp_controller
));
341 controller
->pid
= id
;
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
;
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
;
369 struct agp_file_private
*priv
;
372 agp_remove_seg_from_client(temp
);
373 priv
= agp_find_private(temp
->pid
);
376 clear_bit(AGP_FF_IS_VALID
, &priv
->access_flags
);
377 clear_bit(AGP_FF_IS_CLIENT
, &priv
->access_flags
);
379 client
= client
->next
;
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
;
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
;
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
);
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
);
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
);
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
)
488 client
= controller
->clients
;
490 while (client
!= NULL
) {
491 if (client
->pid
== id
)
493 client
= client
->next
;
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
)
508 controller
= controller
->next
;
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
)
521 temp
= agp_find_client_in_controller(agp_fe
.current_controller
, id
);
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
)
548 memset(new_client
, 0, sizeof(struct agp_client
));
549 new_client
->pid
= id
;
550 agp_insert_client(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
)
565 client
= agp_find_client_in_controller(controller
, id
);
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
;
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
);
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
)
606 if (!(test_bit(AGP_FF_IS_VALID
, &priv
->access_flags
)))
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
)
620 client
= agp_find_client_by_pid(current
->pid
);
625 if (!agp_find_seg_in_client(client
, offset
, size
, vma
->vm_page_prot
))
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
)) {
636 up(&(agp_fe
.agp_mutex
));
640 if (test_bit(AGP_FF_IS_CONTROLLER
, &priv
->access_flags
)) {
641 if (size
!= current_size
)
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
)) {
652 up(&(agp_fe
.agp_mutex
));
657 up(&(agp_fe
.agp_mutex
));
661 up(&(agp_fe
.agp_mutex
));
665 up(&(agp_fe
.agp_mutex
));
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
);
690 if (test_bit(AGP_FF_IS_CLIENT
, &priv
->access_flags
))
691 agp_remove_client(priv
->my_pid
);
693 agp_remove_file_private(priv
);
695 file
->private_data
= NULL
;
696 up(&(agp_fe
.agp_mutex
));
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
;
707 down(&(agp_fe
.agp_mutex
));
709 if (minor
!= AGPGART_MINOR
)
712 priv
= kmalloc(sizeof(struct agp_file_private
), GFP_KERNEL
);
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
));
739 up(&(agp_fe
.agp_mutex
));
744 static ssize_t
agp_read(struct file
*file
, char __user
*buf
,
745 size_t count
, loff_t
* ppos
)
750 static ssize_t
agp_write(struct file
*file
, const char __user
*buf
,
751 size_t count
, loff_t
* ppos
)
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
)))
779 static int agpioc_acquire_wrap(struct agp_file_private
*priv
)
781 struct agp_controller
*controller
;
785 if (!(test_bit(AGP_FF_ALLOW_CONTROLLER
, &priv
->access_flags
)))
788 if (agp_fe
.current_controller
!= NULL
)
794 if (atomic_read(&agp_bridge
->agp_in_use
))
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
);
806 controller
= agp_create_controller(priv
->my_pid
);
808 if (controller
== NULL
) {
809 agp_fe
.backend_acquired
= FALSE
;
810 agp_backend_release(agp_bridge
);
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
);
822 static int agpioc_release_wrap(struct agp_file_private
*priv
)
825 agp_controller_release_current(agp_fe
.current_controller
, priv
);
829 static int agpioc_setup_wrap(struct agp_file_private
*priv
, void __user
*arg
)
831 struct agp_setup mode
;
834 if (copy_from_user(&mode
, arg
, sizeof(struct agp_setup
)))
837 agp_enable(agp_bridge
, mode
.agp_mode
);
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
;
848 if (copy_from_user(&reserve
, arg
, sizeof(struct agp_region
)))
851 if ((unsigned) reserve
.seg_count
>= ~0U/sizeof(struct agp_segment
))
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 */
868 return agp_remove_client(reserve
.pid
);
870 struct agp_segment
*segment
;
872 if (reserve
.seg_count
>= 16384)
875 segment
= kmalloc((sizeof(struct agp_segment
) * reserve
.seg_count
),
881 if (copy_from_user(segment
, (void __user
*) reserve
.seg_list
,
882 sizeof(struct agp_segment
) * reserve
.seg_count
)) {
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
) {
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 */
909 static int agpioc_protect_wrap(struct agp_file_private
*priv
)
912 /* This function is not currently implemented */
916 static int agpioc_allocate_wrap(struct agp_file_private
*priv
, void __user
*arg
)
918 struct agp_memory
*memory
;
919 struct agp_allocate alloc
;
922 if (copy_from_user(&alloc
, arg
, sizeof(struct agp_allocate
)))
925 memory
= agp_allocate_memory_wrap(alloc
.pg_count
, alloc
.type
);
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
);
940 static int agpioc_deallocate_wrap(struct agp_file_private
*priv
, int arg
)
942 struct agp_memory
*memory
;
945 memory
= agp_find_mem_by_key(arg
);
950 agp_free_memory_wrap(memory
);
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
;
960 if (copy_from_user(&bind_info
, arg
, sizeof(struct agp_bind
)))
963 memory
= agp_find_mem_by_key(bind_info
.key
);
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
;
977 if (copy_from_user(&unbind
, arg
, sizeof(struct agp_unbind
)))
980 memory
= agp_find_mem_by_key(unbind
.key
);
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
)) {
1002 if ((agp_fe
.backend_acquired
!= TRUE
) &&
1003 (cmd
!= AGPIOC_ACQUIRE
)) {
1007 if (cmd
!= AGPIOC_ACQUIRE
) {
1008 if (!(test_bit(AGP_FF_IS_CONTROLLER
, &curr_priv
->access_flags
))) {
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
) {
1023 ret_val
= agpioc_info_wrap(curr_priv
, (void __user
*) arg
);
1026 case AGPIOC_ACQUIRE
:
1027 ret_val
= agpioc_acquire_wrap(curr_priv
);
1030 case AGPIOC_RELEASE
:
1031 ret_val
= agpioc_release_wrap(curr_priv
);
1035 ret_val
= agpioc_setup_wrap(curr_priv
, (void __user
*) arg
);
1038 case AGPIOC_RESERVE
:
1039 ret_val
= agpioc_reserve_wrap(curr_priv
, (void __user
*) arg
);
1042 case AGPIOC_PROTECT
:
1043 ret_val
= agpioc_protect_wrap(curr_priv
);
1046 case AGPIOC_ALLOCATE
:
1047 ret_val
= agpioc_allocate_wrap(curr_priv
, (void __user
*) arg
);
1050 case AGPIOC_DEALLOCATE
:
1051 ret_val
= agpioc_deallocate_wrap(curr_priv
, (int) arg
);
1055 ret_val
= agpioc_bind_wrap(curr_priv
, (void __user
*) arg
);
1059 ret_val
= agpioc_unbind_wrap(curr_priv
, (void __user
*) arg
);
1064 DBG("ioctl returns %d\n", ret_val
);
1065 up(&(agp_fe
.agp_mutex
));
1069 static struct file_operations agp_fops
=
1071 .owner
= THIS_MODULE
,
1072 .llseek
= no_llseek
,
1078 .release
= agp_release
,
1081 static struct miscdevice agp_miscdev
=
1083 .minor
= AGPGART_MINOR
,
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
);
1100 void agp_frontend_cleanup(void)
1102 misc_deregister(&agp_miscdev
);