xref: /openbmc/linux/drivers/char/agp/frontend.c (revision 4dc7ccf7)
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>
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 <linux/fs.h>
41 #include <linux/sched.h>
42 #include <linux/smp_lock.h>
43 #include <asm/uaccess.h>
44 #include <asm/pgtable.h>
45 #include "agp.h"
46 
47 struct agp_front_data agp_fe;
48 
49 struct agp_memory *agp_find_mem_by_key(int key)
50 {
51 	struct agp_memory *curr;
52 
53 	if (agp_fe.current_controller == NULL)
54 		return NULL;
55 
56 	curr = agp_fe.current_controller->pool;
57 
58 	while (curr != NULL) {
59 		if (curr->key == key)
60 			break;
61 		curr = curr->next;
62 	}
63 
64 	DBG("key=%d -> mem=%p", key, curr);
65 	return curr;
66 }
67 
68 static void agp_remove_from_pool(struct agp_memory *temp)
69 {
70 	struct agp_memory *prev;
71 	struct agp_memory *next;
72 
73 	/* Check to see if this is even in the memory pool */
74 
75 	DBG("mem=%p", temp);
76 	if (agp_find_mem_by_key(temp->key) != NULL) {
77 		next = temp->next;
78 		prev = temp->prev;
79 
80 		if (prev != NULL) {
81 			prev->next = next;
82 			if (next != NULL)
83 				next->prev = prev;
84 
85 		} else {
86 			/* This is the first item on the list */
87 			if (next != NULL)
88 				next->prev = NULL;
89 
90 			agp_fe.current_controller->pool = next;
91 		}
92 	}
93 }
94 
95 /*
96  * Routines for managing each client's segment list -
97  * These routines handle adding and removing segments
98  * to each auth'ed client.
99  */
100 
101 static struct
102 agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
103 						unsigned long offset,
104 					    int size, pgprot_t page_prot)
105 {
106 	struct agp_segment_priv *seg;
107 	int num_segments, i;
108 	off_t pg_start;
109 	size_t pg_count;
110 
111 	pg_start = offset / 4096;
112 	pg_count = size / 4096;
113 	seg = *(client->segments);
114 	num_segments = client->num_segments;
115 
116 	for (i = 0; i < client->num_segments; i++) {
117 		if ((seg[i].pg_start == pg_start) &&
118 		    (seg[i].pg_count == pg_count) &&
119 		    (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
120 			return seg + i;
121 		}
122 	}
123 
124 	return NULL;
125 }
126 
127 static void agp_remove_seg_from_client(struct agp_client *client)
128 {
129 	DBG("client=%p", client);
130 
131 	if (client->segments != NULL) {
132 		if (*(client->segments) != NULL) {
133 			DBG("Freeing %p from client %p", *(client->segments), client);
134 			kfree(*(client->segments));
135 		}
136 		DBG("Freeing %p from client %p", client->segments, client);
137 		kfree(client->segments);
138 		client->segments = NULL;
139 	}
140 }
141 
142 static void agp_add_seg_to_client(struct agp_client *client,
143 			       struct agp_segment_priv ** seg, int num_segments)
144 {
145 	struct agp_segment_priv **prev_seg;
146 
147 	prev_seg = client->segments;
148 
149 	if (prev_seg != NULL)
150 		agp_remove_seg_from_client(client);
151 
152 	DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
153 	client->num_segments = num_segments;
154 	client->segments = seg;
155 }
156 
157 static pgprot_t agp_convert_mmap_flags(int prot)
158 {
159 	unsigned long prot_bits;
160 
161 	prot_bits = calc_vm_prot_bits(prot) | VM_SHARED;
162 	return vm_get_page_prot(prot_bits);
163 }
164 
165 int agp_create_segment(struct agp_client *client, struct agp_region *region)
166 {
167 	struct agp_segment_priv **ret_seg;
168 	struct agp_segment_priv *seg;
169 	struct agp_segment *user_seg;
170 	size_t i;
171 
172 	seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
173 	if (seg == NULL) {
174 		kfree(region->seg_list);
175 		region->seg_list = NULL;
176 		return -ENOMEM;
177 	}
178 	user_seg = region->seg_list;
179 
180 	for (i = 0; i < region->seg_count; i++) {
181 		seg[i].pg_start = user_seg[i].pg_start;
182 		seg[i].pg_count = user_seg[i].pg_count;
183 		seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
184 	}
185 	kfree(region->seg_list);
186 	region->seg_list = NULL;
187 
188 	ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
189 	if (ret_seg == NULL) {
190 		kfree(seg);
191 		return -ENOMEM;
192 	}
193 	*ret_seg = seg;
194 	agp_add_seg_to_client(client, ret_seg, region->seg_count);
195 	return 0;
196 }
197 
198 /* End - Routines for managing each client's segment list */
199 
200 /* This function must only be called when current_controller != NULL */
201 static void agp_insert_into_pool(struct agp_memory * temp)
202 {
203 	struct agp_memory *prev;
204 
205 	prev = agp_fe.current_controller->pool;
206 
207 	if (prev != NULL) {
208 		prev->prev = temp;
209 		temp->next = prev;
210 	}
211 	agp_fe.current_controller->pool = temp;
212 }
213 
214 
215 /* File private list routines */
216 
217 struct agp_file_private *agp_find_private(pid_t pid)
218 {
219 	struct agp_file_private *curr;
220 
221 	curr = agp_fe.file_priv_list;
222 
223 	while (curr != NULL) {
224 		if (curr->my_pid == pid)
225 			return curr;
226 		curr = curr->next;
227 	}
228 
229 	return NULL;
230 }
231 
232 static void agp_insert_file_private(struct agp_file_private * priv)
233 {
234 	struct agp_file_private *prev;
235 
236 	prev = agp_fe.file_priv_list;
237 
238 	if (prev != NULL)
239 		prev->prev = priv;
240 	priv->next = prev;
241 	agp_fe.file_priv_list = priv;
242 }
243 
244 static void agp_remove_file_private(struct agp_file_private * priv)
245 {
246 	struct agp_file_private *next;
247 	struct agp_file_private *prev;
248 
249 	next = priv->next;
250 	prev = priv->prev;
251 
252 	if (prev != NULL) {
253 		prev->next = next;
254 
255 		if (next != NULL)
256 			next->prev = prev;
257 
258 	} else {
259 		if (next != NULL)
260 			next->prev = NULL;
261 
262 		agp_fe.file_priv_list = next;
263 	}
264 }
265 
266 /* End - File flag list routines */
267 
268 /*
269  * Wrappers for agp_free_memory & agp_allocate_memory
270  * These make sure that internal lists are kept updated.
271  */
272 void agp_free_memory_wrap(struct agp_memory *memory)
273 {
274 	agp_remove_from_pool(memory);
275 	agp_free_memory(memory);
276 }
277 
278 struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
279 {
280 	struct agp_memory *memory;
281 
282 	memory = agp_allocate_memory(agp_bridge, pg_count, type);
283 	if (memory == NULL)
284 		return NULL;
285 
286 	agp_insert_into_pool(memory);
287 	return memory;
288 }
289 
290 /* Routines for managing the list of controllers -
291  * These routines manage the current controller, and the list of
292  * controllers
293  */
294 
295 static struct agp_controller *agp_find_controller_by_pid(pid_t id)
296 {
297 	struct agp_controller *controller;
298 
299 	controller = agp_fe.controllers;
300 
301 	while (controller != NULL) {
302 		if (controller->pid == id)
303 			return controller;
304 		controller = controller->next;
305 	}
306 
307 	return NULL;
308 }
309 
310 static struct agp_controller *agp_create_controller(pid_t id)
311 {
312 	struct agp_controller *controller;
313 
314 	controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
315 	if (controller == NULL)
316 		return NULL;
317 
318 	controller->pid = id;
319 	return controller;
320 }
321 
322 static int agp_insert_controller(struct agp_controller *controller)
323 {
324 	struct agp_controller *prev_controller;
325 
326 	prev_controller = agp_fe.controllers;
327 	controller->next = prev_controller;
328 
329 	if (prev_controller != NULL)
330 		prev_controller->prev = controller;
331 
332 	agp_fe.controllers = controller;
333 
334 	return 0;
335 }
336 
337 static void agp_remove_all_clients(struct agp_controller *controller)
338 {
339 	struct agp_client *client;
340 	struct agp_client *temp;
341 
342 	client = controller->clients;
343 
344 	while (client) {
345 		struct agp_file_private *priv;
346 
347 		temp = client;
348 		agp_remove_seg_from_client(temp);
349 		priv = agp_find_private(temp->pid);
350 
351 		if (priv != NULL) {
352 			clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
353 			clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
354 		}
355 		client = client->next;
356 		kfree(temp);
357 	}
358 }
359 
360 static void agp_remove_all_memory(struct agp_controller *controller)
361 {
362 	struct agp_memory *memory;
363 	struct agp_memory *temp;
364 
365 	memory = controller->pool;
366 
367 	while (memory) {
368 		temp = memory;
369 		memory = memory->next;
370 		agp_free_memory_wrap(temp);
371 	}
372 }
373 
374 static int agp_remove_controller(struct agp_controller *controller)
375 {
376 	struct agp_controller *prev_controller;
377 	struct agp_controller *next_controller;
378 
379 	prev_controller = controller->prev;
380 	next_controller = controller->next;
381 
382 	if (prev_controller != NULL) {
383 		prev_controller->next = next_controller;
384 		if (next_controller != NULL)
385 			next_controller->prev = prev_controller;
386 
387 	} else {
388 		if (next_controller != NULL)
389 			next_controller->prev = NULL;
390 
391 		agp_fe.controllers = next_controller;
392 	}
393 
394 	agp_remove_all_memory(controller);
395 	agp_remove_all_clients(controller);
396 
397 	if (agp_fe.current_controller == controller) {
398 		agp_fe.current_controller = NULL;
399 		agp_fe.backend_acquired = false;
400 		agp_backend_release(agp_bridge);
401 	}
402 	kfree(controller);
403 	return 0;
404 }
405 
406 static void agp_controller_make_current(struct agp_controller *controller)
407 {
408 	struct agp_client *clients;
409 
410 	clients = controller->clients;
411 
412 	while (clients != NULL) {
413 		struct agp_file_private *priv;
414 
415 		priv = agp_find_private(clients->pid);
416 
417 		if (priv != NULL) {
418 			set_bit(AGP_FF_IS_VALID, &priv->access_flags);
419 			set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
420 		}
421 		clients = clients->next;
422 	}
423 
424 	agp_fe.current_controller = controller;
425 }
426 
427 static void agp_controller_release_current(struct agp_controller *controller,
428 				      struct agp_file_private *controller_priv)
429 {
430 	struct agp_client *clients;
431 
432 	clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
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 			clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
442 
443 		clients = clients->next;
444 	}
445 
446 	agp_fe.current_controller = NULL;
447 	agp_fe.used_by_controller = false;
448 	agp_backend_release(agp_bridge);
449 }
450 
451 /*
452  * Routines for managing client lists -
453  * These routines are for managing the list of auth'ed clients.
454  */
455 
456 static struct agp_client
457 *agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
458 {
459 	struct agp_client *client;
460 
461 	if (controller == NULL)
462 		return NULL;
463 
464 	client = controller->clients;
465 
466 	while (client != NULL) {
467 		if (client->pid == id)
468 			return client;
469 		client = client->next;
470 	}
471 
472 	return NULL;
473 }
474 
475 static struct agp_controller *agp_find_controller_for_client(pid_t id)
476 {
477 	struct agp_controller *controller;
478 
479 	controller = agp_fe.controllers;
480 
481 	while (controller != NULL) {
482 		if ((agp_find_client_in_controller(controller, id)) != NULL)
483 			return controller;
484 		controller = controller->next;
485 	}
486 
487 	return NULL;
488 }
489 
490 struct agp_client *agp_find_client_by_pid(pid_t id)
491 {
492 	struct agp_client *temp;
493 
494 	if (agp_fe.current_controller == NULL)
495 		return NULL;
496 
497 	temp = agp_find_client_in_controller(agp_fe.current_controller, id);
498 	return temp;
499 }
500 
501 static void agp_insert_client(struct agp_client *client)
502 {
503 	struct agp_client *prev_client;
504 
505 	prev_client = agp_fe.current_controller->clients;
506 	client->next = prev_client;
507 
508 	if (prev_client != NULL)
509 		prev_client->prev = client;
510 
511 	agp_fe.current_controller->clients = client;
512 	agp_fe.current_controller->num_clients++;
513 }
514 
515 struct agp_client *agp_create_client(pid_t id)
516 {
517 	struct agp_client *new_client;
518 
519 	new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
520 	if (new_client == NULL)
521 		return NULL;
522 
523 	new_client->pid = id;
524 	agp_insert_client(new_client);
525 	return new_client;
526 }
527 
528 int agp_remove_client(pid_t id)
529 {
530 	struct agp_client *client;
531 	struct agp_client *prev_client;
532 	struct agp_client *next_client;
533 	struct agp_controller *controller;
534 
535 	controller = agp_find_controller_for_client(id);
536 	if (controller == NULL)
537 		return -EINVAL;
538 
539 	client = agp_find_client_in_controller(controller, id);
540 	if (client == NULL)
541 		return -EINVAL;
542 
543 	prev_client = client->prev;
544 	next_client = client->next;
545 
546 	if (prev_client != NULL) {
547 		prev_client->next = next_client;
548 		if (next_client != NULL)
549 			next_client->prev = prev_client;
550 
551 	} else {
552 		if (next_client != NULL)
553 			next_client->prev = NULL;
554 		controller->clients = next_client;
555 	}
556 
557 	controller->num_clients--;
558 	agp_remove_seg_from_client(client);
559 	kfree(client);
560 	return 0;
561 }
562 
563 /* End - Routines for managing client lists */
564 
565 /* File Operations */
566 
567 static int agp_mmap(struct file *file, struct vm_area_struct *vma)
568 {
569 	unsigned int size, current_size;
570 	unsigned long offset;
571 	struct agp_client *client;
572 	struct agp_file_private *priv = file->private_data;
573 	struct agp_kern_info kerninfo;
574 
575 	mutex_lock(&(agp_fe.agp_mutex));
576 
577 	if (agp_fe.backend_acquired != true)
578 		goto out_eperm;
579 
580 	if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
581 		goto out_eperm;
582 
583 	agp_copy_info(agp_bridge, &kerninfo);
584 	size = vma->vm_end - vma->vm_start;
585 	current_size = kerninfo.aper_size;
586 	current_size = current_size * 0x100000;
587 	offset = vma->vm_pgoff << PAGE_SHIFT;
588 	DBG("%lx:%lx", offset, offset+size);
589 
590 	if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
591 		if ((size + offset) > current_size)
592 			goto out_inval;
593 
594 		client = agp_find_client_by_pid(current->pid);
595 
596 		if (client == NULL)
597 			goto out_eperm;
598 
599 		if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
600 			goto out_inval;
601 
602 		DBG("client vm_ops=%p", kerninfo.vm_ops);
603 		if (kerninfo.vm_ops) {
604 			vma->vm_ops = kerninfo.vm_ops;
605 		} else if (io_remap_pfn_range(vma, vma->vm_start,
606 				(kerninfo.aper_base + offset) >> PAGE_SHIFT,
607 					    size, vma->vm_page_prot)) {
608 			goto out_again;
609 		}
610 		mutex_unlock(&(agp_fe.agp_mutex));
611 		return 0;
612 	}
613 
614 	if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
615 		if (size != current_size)
616 			goto out_inval;
617 
618 		DBG("controller vm_ops=%p", kerninfo.vm_ops);
619 		if (kerninfo.vm_ops) {
620 			vma->vm_ops = kerninfo.vm_ops;
621 		} else if (io_remap_pfn_range(vma, vma->vm_start,
622 					    kerninfo.aper_base >> PAGE_SHIFT,
623 					    size, vma->vm_page_prot)) {
624 			goto out_again;
625 		}
626 		mutex_unlock(&(agp_fe.agp_mutex));
627 		return 0;
628 	}
629 
630 out_eperm:
631 	mutex_unlock(&(agp_fe.agp_mutex));
632 	return -EPERM;
633 
634 out_inval:
635 	mutex_unlock(&(agp_fe.agp_mutex));
636 	return -EINVAL;
637 
638 out_again:
639 	mutex_unlock(&(agp_fe.agp_mutex));
640 	return -EAGAIN;
641 }
642 
643 static int agp_release(struct inode *inode, struct file *file)
644 {
645 	struct agp_file_private *priv = file->private_data;
646 
647 	mutex_lock(&(agp_fe.agp_mutex));
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;
670 	mutex_unlock(&(agp_fe.agp_mutex));
671 	return 0;
672 }
673 
674 static 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;
679 
680 	if (minor != AGPGART_MINOR)
681 		return -ENXIO;
682 
683 	mutex_lock(&(agp_fe.agp_mutex));
684 
685 	priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
686 	if (priv == NULL) {
687 		mutex_unlock(&(agp_fe.agp_mutex));
688 		return -ENOMEM;
689 	}
690 
691 	set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
692 	priv->my_pid = current->pid;
693 
694 	if (capable(CAP_SYS_RAWIO))
695 		/* Root priv, can be controller */
696 		set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
697 
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);
707 
708 	mutex_unlock(&(agp_fe.agp_mutex));
709 
710 	return 0;
711 }
712 
713 
714 static ssize_t agp_read(struct file *file, char __user *buf,
715 			size_t count, loff_t * ppos)
716 {
717 	return -EINVAL;
718 }
719 
720 static ssize_t agp_write(struct file *file, const char __user *buf,
721 			 size_t count, loff_t * ppos)
722 {
723 	return -EINVAL;
724 }
725 
726 static 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 
749 int agpioc_acquire_wrap(struct agp_file_private *priv)
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 
761 	if (!agp_bridge)
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 
769 	agp_fe.backend_acquired = true;
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) {
779 			agp_fe.backend_acquired = false;
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 
792 int agpioc_release_wrap(struct agp_file_private *priv)
793 {
794 	DBG("");
795 	agp_controller_release_current(agp_fe.current_controller, priv);
796 	return 0;
797 }
798 
799 int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
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 
811 static 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 
879 int agpioc_protect_wrap(struct agp_file_private *priv)
880 {
881 	DBG("");
882 	/* This function is not currently implemented */
883 	return -EINVAL;
884 }
885 
886 static 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 
895 	if (alloc.type >= AGP_USER_TYPES)
896 		return -EINVAL;
897 
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 
913 int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
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 
927 static 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 
944 static 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 
961 int agpioc_chipset_flush_wrap(struct agp_file_private *priv)
962 {
963 	DBG("");
964 	agp_flush_chipset(agp_bridge);
965 	return 0;
966 }
967 
968 static long agp_ioctl(struct file *file,
969 		     unsigned int cmd, unsigned long arg)
970 {
971 	struct agp_file_private *curr_priv = file->private_data;
972 	int ret_val = -ENOTTY;
973 
974 	DBG("priv=%p, cmd=%x", curr_priv, cmd);
975 	mutex_lock(&(agp_fe.agp_mutex));
976 
977 	if ((agp_fe.current_controller == NULL) &&
978 	    (cmd != AGPIOC_ACQUIRE)) {
979 		ret_val = -EINVAL;
980 		goto ioctl_out;
981 	}
982 	if ((agp_fe.backend_acquired != true) &&
983 	    (cmd != AGPIOC_ACQUIRE)) {
984 		ret_val = -EBUSY;
985 		goto ioctl_out;
986 	}
987 	if (cmd != AGPIOC_ACQUIRE) {
988 		if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
989 			ret_val = -EPERM;
990 			goto ioctl_out;
991 		}
992 		/* Use the original pid of the controller,
993 		 * in case it's threaded */
994 
995 		if (agp_fe.current_controller->pid != curr_priv->my_pid) {
996 			ret_val = -EBUSY;
997 			goto ioctl_out;
998 		}
999 	}
1000 
1001 	switch (cmd) {
1002 	case AGPIOC_INFO:
1003 		ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
1004 		break;
1005 
1006 	case AGPIOC_ACQUIRE:
1007 		ret_val = agpioc_acquire_wrap(curr_priv);
1008 		break;
1009 
1010 	case AGPIOC_RELEASE:
1011 		ret_val = agpioc_release_wrap(curr_priv);
1012 		break;
1013 
1014 	case AGPIOC_SETUP:
1015 		ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
1016 		break;
1017 
1018 	case AGPIOC_RESERVE:
1019 		ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
1020 		break;
1021 
1022 	case AGPIOC_PROTECT:
1023 		ret_val = agpioc_protect_wrap(curr_priv);
1024 		break;
1025 
1026 	case AGPIOC_ALLOCATE:
1027 		ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
1028 		break;
1029 
1030 	case AGPIOC_DEALLOCATE:
1031 		ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
1032 		break;
1033 
1034 	case AGPIOC_BIND:
1035 		ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
1036 		break;
1037 
1038 	case AGPIOC_UNBIND:
1039 		ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
1040 		break;
1041 
1042 	case AGPIOC_CHIPSET_FLUSH:
1043 		ret_val = agpioc_chipset_flush_wrap(curr_priv);
1044 		break;
1045 	}
1046 
1047 ioctl_out:
1048 	DBG("ioctl returns %d\n", ret_val);
1049 	mutex_unlock(&(agp_fe.agp_mutex));
1050 	return ret_val;
1051 }
1052 
1053 static const struct file_operations agp_fops =
1054 {
1055 	.owner		= THIS_MODULE,
1056 	.llseek		= no_llseek,
1057 	.read		= agp_read,
1058 	.write		= agp_write,
1059 	.unlocked_ioctl	= agp_ioctl,
1060 #ifdef CONFIG_COMPAT
1061 	.compat_ioctl	= compat_agp_ioctl,
1062 #endif
1063 	.mmap		= agp_mmap,
1064 	.open		= agp_open,
1065 	.release	= agp_release,
1066 };
1067 
1068 static struct miscdevice agp_miscdev =
1069 {
1070 	.minor	= AGPGART_MINOR,
1071 	.name	= "agpgart",
1072 	.fops	= &agp_fops
1073 };
1074 
1075 int agp_frontend_initialize(void)
1076 {
1077 	memset(&agp_fe, 0, sizeof(struct agp_front_data));
1078 	mutex_init(&(agp_fe.agp_mutex));
1079 
1080 	if (misc_register(&agp_miscdev)) {
1081 		printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
1082 		return -EIO;
1083 	}
1084 	return 0;
1085 }
1086 
1087 void agp_frontend_cleanup(void)
1088 {
1089 	misc_deregister(&agp_miscdev);
1090 }
1091