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