blob: 1b192395a90c840d48362fdf81a139a75243e08f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040040#include <linux/fs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040041#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/pgtable.h>
44#include "agp.h"
45
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -080046struct agp_front_data agp_fe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -080048struct agp_memory *agp_find_mem_by_key(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct agp_memory *curr;
51
52 if (agp_fe.current_controller == NULL)
53 return NULL;
54
55 curr = agp_fe.current_controller->pool;
56
57 while (curr != NULL) {
58 if (curr->key == key)
59 break;
60 curr = curr->next;
61 }
62
63 DBG("key=%d -> mem=%p", key, curr);
64 return curr;
65}
66
67static void agp_remove_from_pool(struct agp_memory *temp)
68{
69 struct agp_memory *prev;
70 struct agp_memory *next;
71
72 /* Check to see if this is even in the memory pool */
73
74 DBG("mem=%p", temp);
75 if (agp_find_mem_by_key(temp->key) != NULL) {
76 next = temp->next;
77 prev = temp->prev;
78
79 if (prev != NULL) {
80 prev->next = next;
81 if (next != NULL)
82 next->prev = prev;
83
84 } else {
85 /* This is the first item on the list */
86 if (next != NULL)
87 next->prev = NULL;
88
89 agp_fe.current_controller->pool = next;
90 }
91 }
92}
93
94/*
95 * Routines for managing each client's segment list -
96 * These routines handle adding and removing segments
97 * to each auth'ed client.
98 */
99
100static struct
101agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
102 unsigned long offset,
103 int size, pgprot_t page_prot)
104{
105 struct agp_segment_priv *seg;
106 int num_segments, i;
107 off_t pg_start;
108 size_t pg_count;
109
110 pg_start = offset / 4096;
111 pg_count = size / 4096;
112 seg = *(client->segments);
113 num_segments = client->num_segments;
114
115 for (i = 0; i < client->num_segments; i++) {
116 if ((seg[i].pg_start == pg_start) &&
117 (seg[i].pg_count == pg_count) &&
118 (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
119 return seg + i;
120 }
121 }
122
123 return NULL;
124}
125
126static void agp_remove_seg_from_client(struct agp_client *client)
127{
128 DBG("client=%p", client);
129
130 if (client->segments != NULL) {
131 if (*(client->segments) != NULL) {
132 DBG("Freeing %p from client %p", *(client->segments), client);
133 kfree(*(client->segments));
134 }
135 DBG("Freeing %p from client %p", client->segments, client);
136 kfree(client->segments);
137 client->segments = NULL;
138 }
139}
140
141static void agp_add_seg_to_client(struct agp_client *client,
142 struct agp_segment_priv ** seg, int num_segments)
143{
144 struct agp_segment_priv **prev_seg;
145
146 prev_seg = client->segments;
147
148 if (prev_seg != NULL)
149 agp_remove_seg_from_client(client);
150
151 DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
152 client->num_segments = num_segments;
153 client->segments = seg;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static pgprot_t agp_convert_mmap_flags(int prot)
157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long prot_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Hugh Dickins804af2c2006-07-26 21:39:49 +0100160 prot_bits = calc_vm_prot_bits(prot) | VM_SHARED;
161 return vm_get_page_prot(prot_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800164int agp_create_segment(struct agp_client *client, struct agp_region *region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct agp_segment_priv **ret_seg;
167 struct agp_segment_priv *seg;
168 struct agp_segment *user_seg;
169 size_t i;
170
Dave Jones0ea27d92005-10-20 15:12:16 -0700171 seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (seg == NULL) {
173 kfree(region->seg_list);
174 region->seg_list = NULL;
175 return -ENOMEM;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 user_seg = region->seg_list;
178
179 for (i = 0; i < region->seg_count; i++) {
180 seg[i].pg_start = user_seg[i].pg_start;
181 seg[i].pg_count = user_seg[i].pg_count;
182 seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
183 }
184 kfree(region->seg_list);
185 region->seg_list = NULL;
186
187 ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
188 if (ret_seg == NULL) {
189 kfree(seg);
190 return -ENOMEM;
191 }
192 *ret_seg = seg;
193 agp_add_seg_to_client(client, ret_seg, region->seg_count);
194 return 0;
195}
196
197/* End - Routines for managing each client's segment list */
198
199/* This function must only be called when current_controller != NULL */
200static void agp_insert_into_pool(struct agp_memory * temp)
201{
202 struct agp_memory *prev;
203
204 prev = agp_fe.current_controller->pool;
205
206 if (prev != NULL) {
207 prev->prev = temp;
208 temp->next = prev;
209 }
210 agp_fe.current_controller->pool = temp;
211}
212
213
214/* File private list routines */
215
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800216struct agp_file_private *agp_find_private(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct agp_file_private *curr;
219
220 curr = agp_fe.file_priv_list;
221
222 while (curr != NULL) {
223 if (curr->my_pid == pid)
224 return curr;
225 curr = curr->next;
226 }
227
228 return NULL;
229}
230
Adrian Bunk408b6642005-05-01 08:59:29 -0700231static void agp_insert_file_private(struct agp_file_private * priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct agp_file_private *prev;
234
235 prev = agp_fe.file_priv_list;
236
237 if (prev != NULL)
238 prev->prev = priv;
239 priv->next = prev;
240 agp_fe.file_priv_list = priv;
241}
242
Adrian Bunk408b6642005-05-01 08:59:29 -0700243static void agp_remove_file_private(struct agp_file_private * priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct agp_file_private *next;
246 struct agp_file_private *prev;
247
248 next = priv->next;
249 prev = priv->prev;
250
251 if (prev != NULL) {
252 prev->next = next;
253
254 if (next != NULL)
255 next->prev = prev;
256
257 } else {
258 if (next != NULL)
259 next->prev = NULL;
260
261 agp_fe.file_priv_list = next;
262 }
263}
264
265/* End - File flag list routines */
266
267/*
268 * Wrappers for agp_free_memory & agp_allocate_memory
269 * These make sure that internal lists are kept updated.
270 */
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800271void agp_free_memory_wrap(struct agp_memory *memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 agp_remove_from_pool(memory);
274 agp_free_memory(memory);
275}
276
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800277struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct agp_memory *memory;
280
281 memory = agp_allocate_memory(agp_bridge, pg_count, type);
282 if (memory == NULL)
283 return NULL;
284
285 agp_insert_into_pool(memory);
286 return memory;
287}
288
289/* Routines for managing the list of controllers -
290 * These routines manage the current controller, and the list of
291 * controllers
292 */
293
294static struct agp_controller *agp_find_controller_by_pid(pid_t id)
295{
296 struct agp_controller *controller;
297
298 controller = agp_fe.controllers;
299
300 while (controller != NULL) {
301 if (controller->pid == id)
302 return controller;
303 controller = controller->next;
304 }
305
306 return NULL;
307}
308
309static struct agp_controller *agp_create_controller(pid_t id)
310{
311 struct agp_controller *controller;
312
Dave Jones0ea27d92005-10-20 15:12:16 -0700313 controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (controller == NULL)
315 return NULL;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 controller->pid = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return controller;
319}
320
321static int agp_insert_controller(struct agp_controller *controller)
322{
323 struct agp_controller *prev_controller;
324
325 prev_controller = agp_fe.controllers;
326 controller->next = prev_controller;
327
328 if (prev_controller != NULL)
329 prev_controller->prev = controller;
330
331 agp_fe.controllers = controller;
332
333 return 0;
334}
335
336static void agp_remove_all_clients(struct agp_controller *controller)
337{
338 struct agp_client *client;
339 struct agp_client *temp;
340
341 client = controller->clients;
342
343 while (client) {
344 struct agp_file_private *priv;
345
346 temp = client;
347 agp_remove_seg_from_client(temp);
348 priv = agp_find_private(temp->pid);
349
350 if (priv != NULL) {
351 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
352 clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
353 }
354 client = client->next;
355 kfree(temp);
356 }
357}
358
359static void agp_remove_all_memory(struct agp_controller *controller)
360{
361 struct agp_memory *memory;
362 struct agp_memory *temp;
363
364 memory = controller->pool;
365
366 while (memory) {
367 temp = memory;
368 memory = memory->next;
369 agp_free_memory_wrap(temp);
370 }
371}
372
373static int agp_remove_controller(struct agp_controller *controller)
374{
375 struct agp_controller *prev_controller;
376 struct agp_controller *next_controller;
377
378 prev_controller = controller->prev;
379 next_controller = controller->next;
380
381 if (prev_controller != NULL) {
382 prev_controller->next = next_controller;
383 if (next_controller != NULL)
384 next_controller->prev = prev_controller;
385
386 } else {
387 if (next_controller != NULL)
388 next_controller->prev = NULL;
389
390 agp_fe.controllers = next_controller;
391 }
392
393 agp_remove_all_memory(controller);
394 agp_remove_all_clients(controller);
395
396 if (agp_fe.current_controller == controller) {
397 agp_fe.current_controller = NULL;
Dave Airlie9516b032008-06-19 10:42:17 +1000398 agp_fe.backend_acquired = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 agp_backend_release(agp_bridge);
400 }
401 kfree(controller);
402 return 0;
403}
404
405static void agp_controller_make_current(struct agp_controller *controller)
406{
407 struct agp_client *clients;
408
409 clients = controller->clients;
410
411 while (clients != NULL) {
412 struct agp_file_private *priv;
413
414 priv = agp_find_private(clients->pid);
415
416 if (priv != NULL) {
417 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
418 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
419 }
420 clients = clients->next;
421 }
422
423 agp_fe.current_controller = controller;
424}
425
426static void agp_controller_release_current(struct agp_controller *controller,
427 struct agp_file_private *controller_priv)
428{
429 struct agp_client *clients;
430
431 clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
432 clients = controller->clients;
433
434 while (clients != NULL) {
435 struct agp_file_private *priv;
436
437 priv = agp_find_private(clients->pid);
438
439 if (priv != NULL)
440 clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
441
442 clients = clients->next;
443 }
444
445 agp_fe.current_controller = NULL;
Dave Airlie9516b032008-06-19 10:42:17 +1000446 agp_fe.used_by_controller = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 agp_backend_release(agp_bridge);
448}
449
450/*
451 * Routines for managing client lists -
452 * These routines are for managing the list of auth'ed clients.
453 */
454
455static struct agp_client
456*agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
457{
458 struct agp_client *client;
459
460 if (controller == NULL)
461 return NULL;
462
463 client = controller->clients;
464
465 while (client != NULL) {
466 if (client->pid == id)
467 return client;
468 client = client->next;
469 }
470
471 return NULL;
472}
473
474static struct agp_controller *agp_find_controller_for_client(pid_t id)
475{
476 struct agp_controller *controller;
477
478 controller = agp_fe.controllers;
479
480 while (controller != NULL) {
481 if ((agp_find_client_in_controller(controller, id)) != NULL)
482 return controller;
483 controller = controller->next;
484 }
485
486 return NULL;
487}
488
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800489struct agp_client *agp_find_client_by_pid(pid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct agp_client *temp;
492
493 if (agp_fe.current_controller == NULL)
494 return NULL;
495
496 temp = agp_find_client_in_controller(agp_fe.current_controller, id);
497 return temp;
498}
499
500static void agp_insert_client(struct agp_client *client)
501{
502 struct agp_client *prev_client;
503
504 prev_client = agp_fe.current_controller->clients;
505 client->next = prev_client;
506
507 if (prev_client != NULL)
508 prev_client->prev = client;
509
510 agp_fe.current_controller->clients = client;
511 agp_fe.current_controller->num_clients++;
512}
513
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800514struct agp_client *agp_create_client(pid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct agp_client *new_client;
517
Dave Jones0ea27d92005-10-20 15:12:16 -0700518 new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (new_client == NULL)
520 return NULL;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 new_client->pid = id;
523 agp_insert_client(new_client);
524 return new_client;
525}
526
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800527int agp_remove_client(pid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct agp_client *client;
530 struct agp_client *prev_client;
531 struct agp_client *next_client;
532 struct agp_controller *controller;
533
534 controller = agp_find_controller_for_client(id);
535 if (controller == NULL)
536 return -EINVAL;
537
538 client = agp_find_client_in_controller(controller, id);
539 if (client == NULL)
540 return -EINVAL;
541
542 prev_client = client->prev;
543 next_client = client->next;
544
545 if (prev_client != NULL) {
546 prev_client->next = next_client;
547 if (next_client != NULL)
548 next_client->prev = prev_client;
549
550 } else {
551 if (next_client != NULL)
552 next_client->prev = NULL;
553 controller->clients = next_client;
554 }
555
556 controller->num_clients--;
557 agp_remove_seg_from_client(client);
558 kfree(client);
559 return 0;
560}
561
562/* End - Routines for managing client lists */
563
564/* File Operations */
565
566static int agp_mmap(struct file *file, struct vm_area_struct *vma)
567{
568 unsigned int size, current_size;
569 unsigned long offset;
570 struct agp_client *client;
571 struct agp_file_private *priv = file->private_data;
572 struct agp_kern_info kerninfo;
573
akpm@osdl.org16867822006-01-13 15:51:02 -0800574 mutex_lock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Airlie9516b032008-06-19 10:42:17 +1000576 if (agp_fe.backend_acquired != true)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto out_eperm;
578
579 if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
580 goto out_eperm;
581
582 agp_copy_info(agp_bridge, &kerninfo);
583 size = vma->vm_end - vma->vm_start;
584 current_size = kerninfo.aper_size;
585 current_size = current_size * 0x100000;
586 offset = vma->vm_pgoff << PAGE_SHIFT;
587 DBG("%lx:%lx", offset, offset+size);
588
589 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
590 if ((size + offset) > current_size)
591 goto out_inval;
592
593 client = agp_find_client_by_pid(current->pid);
594
595 if (client == NULL)
596 goto out_eperm;
597
598 if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
599 goto out_inval;
600
601 DBG("client vm_ops=%p", kerninfo.vm_ops);
602 if (kerninfo.vm_ops) {
603 vma->vm_ops = kerninfo.vm_ops;
604 } else if (io_remap_pfn_range(vma, vma->vm_start,
605 (kerninfo.aper_base + offset) >> PAGE_SHIFT,
Andy Lutomirskif4350462013-05-13 23:58:43 +0000606 size,
607 pgprot_writecombine(vma->vm_page_prot))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto out_again;
609 }
akpm@osdl.org16867822006-01-13 15:51:02 -0800610 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 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,
Andy Lutomirskif4350462013-05-13 23:58:43 +0000622 kerninfo.aper_base >> PAGE_SHIFT,
623 size,
624 pgprot_writecombine(vma->vm_page_prot))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto out_again;
626 }
akpm@osdl.org16867822006-01-13 15:51:02 -0800627 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629 }
630
631out_eperm:
akpm@osdl.org16867822006-01-13 15:51:02 -0800632 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return -EPERM;
634
635out_inval:
akpm@osdl.org16867822006-01-13 15:51:02 -0800636 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return -EINVAL;
638
639out_again:
akpm@osdl.org16867822006-01-13 15:51:02 -0800640 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -EAGAIN;
642}
643
644static int agp_release(struct inode *inode, struct file *file)
645{
646 struct agp_file_private *priv = file->private_data;
647
akpm@osdl.org16867822006-01-13 15:51:02 -0800648 mutex_lock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 DBG("priv=%p", priv);
651
652 if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
653 struct agp_controller *controller;
654
655 controller = agp_find_controller_by_pid(priv->my_pid);
656
657 if (controller != NULL) {
658 if (controller == agp_fe.current_controller)
659 agp_controller_release_current(controller, priv);
660 agp_remove_controller(controller);
661 controller = NULL;
662 }
663 }
664
665 if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
666 agp_remove_client(priv->my_pid);
667
668 agp_remove_file_private(priv);
669 kfree(priv);
670 file->private_data = NULL;
akpm@osdl.org16867822006-01-13 15:51:02 -0800671 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
673}
674
675static int agp_open(struct inode *inode, struct file *file)
676{
677 int minor = iminor(inode);
678 struct agp_file_private *priv;
679 struct agp_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (minor != AGPGART_MINOR)
John Kacur55e858c2009-10-11 22:24:25 +0200682 return -ENXIO;
683
684 mutex_lock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dave Jones0ea27d92005-10-20 15:12:16 -0700686 priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
John Kacur55e858c2009-10-11 22:24:25 +0200687 if (priv == NULL) {
688 mutex_unlock(&(agp_fe.agp_mutex));
689 return -ENOMEM;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
693 priv->my_pid = current->pid;
694
John Kacur55e858c2009-10-11 22:24:25 +0200695 if (capable(CAP_SYS_RAWIO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Root priv, can be controller */
697 set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
John Kacur55e858c2009-10-11 22:24:25 +0200698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 client = agp_find_client_by_pid(current->pid);
700
701 if (client != NULL) {
702 set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
703 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
704 }
705 file->private_data = (void *) priv;
706 agp_insert_file_private(priv);
707 DBG("private=%p, client=%p", priv, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
akpm@osdl.org16867822006-01-13 15:51:02 -0800709 mutex_unlock(&(agp_fe.agp_mutex));
John Kacur55e858c2009-10-11 22:24:25 +0200710
711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714
715static ssize_t agp_read(struct file *file, char __user *buf,
716 size_t count, loff_t * ppos)
717{
718 return -EINVAL;
719}
720
721static ssize_t agp_write(struct file *file, const char __user *buf,
722 size_t count, loff_t * ppos)
723{
724 return -EINVAL;
725}
726
727static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
728{
729 struct agp_info userinfo;
730 struct agp_kern_info kerninfo;
731
732 agp_copy_info(agp_bridge, &kerninfo);
733
734 userinfo.version.major = kerninfo.version.major;
735 userinfo.version.minor = kerninfo.version.minor;
736 userinfo.bridge_id = kerninfo.device->vendor |
737 (kerninfo.device->device << 16);
738 userinfo.agp_mode = kerninfo.mode;
739 userinfo.aper_base = kerninfo.aper_base;
740 userinfo.aper_size = kerninfo.aper_size;
741 userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
742 userinfo.pg_used = kerninfo.current_memory;
743
744 if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
745 return -EFAULT;
746
747 return 0;
748}
749
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800750int agpioc_acquire_wrap(struct agp_file_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct agp_controller *controller;
753
754 DBG("");
755
756 if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
757 return -EPERM;
758
759 if (agp_fe.current_controller != NULL)
760 return -EBUSY;
761
Dave Jones6a92a4e2006-02-28 00:54:25 -0500762 if (!agp_bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -ENODEV;
764
765 if (atomic_read(&agp_bridge->agp_in_use))
766 return -EBUSY;
767
768 atomic_inc(&agp_bridge->agp_in_use);
769
Dave Airlie9516b032008-06-19 10:42:17 +1000770 agp_fe.backend_acquired = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 controller = agp_find_controller_by_pid(priv->my_pid);
773
774 if (controller != NULL) {
775 agp_controller_make_current(controller);
776 } else {
777 controller = agp_create_controller(priv->my_pid);
778
779 if (controller == NULL) {
Dave Airlie9516b032008-06-19 10:42:17 +1000780 agp_fe.backend_acquired = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 agp_backend_release(agp_bridge);
782 return -ENOMEM;
783 }
784 agp_insert_controller(controller);
785 agp_controller_make_current(controller);
786 }
787
788 set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
789 set_bit(AGP_FF_IS_VALID, &priv->access_flags);
790 return 0;
791}
792
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800793int agpioc_release_wrap(struct agp_file_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 DBG("");
796 agp_controller_release_current(agp_fe.current_controller, priv);
797 return 0;
798}
799
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800800int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct agp_setup mode;
803
804 DBG("");
805 if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
806 return -EFAULT;
807
808 agp_enable(agp_bridge, mode.agp_mode);
809 return 0;
810}
811
812static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
813{
814 struct agp_region reserve;
815 struct agp_client *client;
816 struct agp_file_private *client_priv;
817
818 DBG("");
819 if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
820 return -EFAULT;
821
822 if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
823 return -EFAULT;
824
825 client = agp_find_client_by_pid(reserve.pid);
826
827 if (reserve.seg_count == 0) {
828 /* remove a client */
829 client_priv = agp_find_private(reserve.pid);
830
831 if (client_priv != NULL) {
832 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
833 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
834 }
835 if (client == NULL) {
836 /* client is already removed */
837 return 0;
838 }
839 return agp_remove_client(reserve.pid);
840 } else {
841 struct agp_segment *segment;
842
843 if (reserve.seg_count >= 16384)
844 return -EINVAL;
845
846 segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
847 GFP_KERNEL);
848
849 if (segment == NULL)
850 return -ENOMEM;
851
852 if (copy_from_user(segment, (void __user *) reserve.seg_list,
853 sizeof(struct agp_segment) * reserve.seg_count)) {
854 kfree(segment);
855 return -EFAULT;
856 }
857 reserve.seg_list = segment;
858
859 if (client == NULL) {
860 /* Create the client and add the segment */
861 client = agp_create_client(reserve.pid);
862
863 if (client == NULL) {
864 kfree(segment);
865 return -ENOMEM;
866 }
867 client_priv = agp_find_private(reserve.pid);
868
869 if (client_priv != NULL) {
870 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
871 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
872 }
873 }
874 return agp_create_segment(client, &reserve);
875 }
876 /* Will never really happen */
877 return -EINVAL;
878}
879
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800880int agpioc_protect_wrap(struct agp_file_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 DBG("");
883 /* This function is not currently implemented */
884 return -EINVAL;
885}
886
887static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
888{
889 struct agp_memory *memory;
890 struct agp_allocate alloc;
891
892 DBG("");
893 if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
894 return -EFAULT;
895
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100896 if (alloc.type >= AGP_USER_TYPES)
897 return -EINVAL;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
900
901 if (memory == NULL)
902 return -ENOMEM;
903
904 alloc.key = memory->key;
905 alloc.physical = memory->physical;
906
907 if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
908 agp_free_memory_wrap(memory);
909 return -EFAULT;
910 }
911 return 0;
912}
913
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -0800914int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct agp_memory *memory;
917
918 DBG("");
919 memory = agp_find_mem_by_key(arg);
920
921 if (memory == NULL)
922 return -EINVAL;
923
924 agp_free_memory_wrap(memory);
925 return 0;
926}
927
928static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
929{
930 struct agp_bind bind_info;
931 struct agp_memory *memory;
932
933 DBG("");
934 if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
935 return -EFAULT;
936
937 memory = agp_find_mem_by_key(bind_info.key);
938
939 if (memory == NULL)
940 return -EINVAL;
941
942 return agp_bind_memory(memory, bind_info.pg_start);
943}
944
945static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
946{
947 struct agp_memory *memory;
948 struct agp_unbind unbind;
949
950 DBG("");
951 if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
952 return -EFAULT;
953
954 memory = agp_find_mem_by_key(unbind.key);
955
956 if (memory == NULL)
957 return -EINVAL;
958
959 return agp_unbind_memory(memory);
960}
961
Mathieu Segaud09aa3562008-04-18 13:29:38 -0700962static long agp_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned int cmd, unsigned long arg)
964{
965 struct agp_file_private *curr_priv = file->private_data;
966 int ret_val = -ENOTTY;
967
968 DBG("priv=%p, cmd=%x", curr_priv, cmd);
akpm@osdl.org16867822006-01-13 15:51:02 -0800969 mutex_lock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if ((agp_fe.current_controller == NULL) &&
972 (cmd != AGPIOC_ACQUIRE)) {
973 ret_val = -EINVAL;
974 goto ioctl_out;
975 }
Dave Airlie9516b032008-06-19 10:42:17 +1000976 if ((agp_fe.backend_acquired != true) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 (cmd != AGPIOC_ACQUIRE)) {
978 ret_val = -EBUSY;
979 goto ioctl_out;
980 }
981 if (cmd != AGPIOC_ACQUIRE) {
982 if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
983 ret_val = -EPERM;
984 goto ioctl_out;
985 }
986 /* Use the original pid of the controller,
987 * in case it's threaded */
988
989 if (agp_fe.current_controller->pid != curr_priv->my_pid) {
990 ret_val = -EBUSY;
991 goto ioctl_out;
992 }
993 }
994
995 switch (cmd) {
996 case AGPIOC_INFO:
997 ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
998 break;
999
1000 case AGPIOC_ACQUIRE:
1001 ret_val = agpioc_acquire_wrap(curr_priv);
1002 break;
1003
1004 case AGPIOC_RELEASE:
1005 ret_val = agpioc_release_wrap(curr_priv);
1006 break;
1007
1008 case AGPIOC_SETUP:
1009 ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
1010 break;
1011
1012 case AGPIOC_RESERVE:
1013 ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
1014 break;
1015
1016 case AGPIOC_PROTECT:
1017 ret_val = agpioc_protect_wrap(curr_priv);
1018 break;
1019
1020 case AGPIOC_ALLOCATE:
1021 ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
1022 break;
1023
1024 case AGPIOC_DEALLOCATE:
1025 ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
1026 break;
1027
1028 case AGPIOC_BIND:
1029 ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
1030 break;
1031
1032 case AGPIOC_UNBIND:
1033 ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
1034 break;
Dave Airliea13af4b2007-10-29 15:14:03 +10001035
1036 case AGPIOC_CHIPSET_FLUSH:
Dave Airliea13af4b2007-10-29 15:14:03 +10001037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040ioctl_out:
1041 DBG("ioctl returns %d\n", ret_val);
akpm@osdl.org16867822006-01-13 15:51:02 -08001042 mutex_unlock(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return ret_val;
1044}
1045
Arjan van de Ven62322d22006-07-03 00:24:21 -07001046static const struct file_operations agp_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 .owner = THIS_MODULE,
1049 .llseek = no_llseek,
1050 .read = agp_read,
1051 .write = agp_write,
Mathieu Segaud09aa3562008-04-18 13:29:38 -07001052 .unlocked_ioctl = agp_ioctl,
Zwane Mwaikambo0316fe82007-01-29 21:20:31 -08001053#ifdef CONFIG_COMPAT
1054 .compat_ioctl = compat_agp_ioctl,
1055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .mmap = agp_mmap,
1057 .open = agp_open,
1058 .release = agp_release,
1059};
1060
1061static struct miscdevice agp_miscdev =
1062{
1063 .minor = AGPGART_MINOR,
1064 .name = "agpgart",
1065 .fops = &agp_fops
1066};
1067
1068int agp_frontend_initialize(void)
1069{
1070 memset(&agp_fe, 0, sizeof(struct agp_front_data));
akpm@osdl.org16867822006-01-13 15:51:02 -08001071 mutex_init(&(agp_fe.agp_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (misc_register(&agp_miscdev)) {
1074 printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
1075 return -EIO;
1076 }
1077 return 0;
1078}
1079
1080void agp_frontend_cleanup(void)
1081{
1082 misc_deregister(&agp_miscdev);
1083}