]>
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 | ||
154 | /* Originally taken from linux/mm/mmap.c from the array | |
155 | * protection_map. | |
156 | * The original really should be exported to modules, or | |
157 | * some routine which does the conversion for you | |
158 | */ | |
159 | ||
160 | static const pgprot_t my_protect_map[16] = | |
161 | { | |
162 | __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111, | |
163 | __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111 | |
164 | }; | |
165 | ||
166 | static pgprot_t agp_convert_mmap_flags(int prot) | |
167 | { | |
168 | #define _trans(x,bit1,bit2) \ | |
169 | ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0) | |
170 | ||
171 | unsigned long prot_bits; | |
172 | pgprot_t temp; | |
173 | ||
174 | prot_bits = _trans(prot, PROT_READ, VM_READ) | | |
175 | _trans(prot, PROT_WRITE, VM_WRITE) | | |
176 | _trans(prot, PROT_EXEC, VM_EXEC); | |
177 | ||
178 | prot_bits |= VM_SHARED; | |
179 | ||
180 | temp = my_protect_map[prot_bits & 0x0000000f]; | |
181 | ||
182 | return temp; | |
183 | } | |
184 | ||
185 | static int agp_create_segment(struct agp_client *client, struct agp_region *region) | |
186 | { | |
187 | struct agp_segment_priv **ret_seg; | |
188 | struct agp_segment_priv *seg; | |
189 | struct agp_segment *user_seg; | |
190 | size_t i; | |
191 | ||
0ea27d9f | 192 | seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL); |
1da177e4 LT |
193 | if (seg == NULL) { |
194 | kfree(region->seg_list); | |
195 | region->seg_list = NULL; | |
196 | return -ENOMEM; | |
197 | } | |
1da177e4 LT |
198 | user_seg = region->seg_list; |
199 | ||
200 | for (i = 0; i < region->seg_count; i++) { | |
201 | seg[i].pg_start = user_seg[i].pg_start; | |
202 | seg[i].pg_count = user_seg[i].pg_count; | |
203 | seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot); | |
204 | } | |
205 | kfree(region->seg_list); | |
206 | region->seg_list = NULL; | |
207 | ||
208 | ret_seg = kmalloc(sizeof(void *), GFP_KERNEL); | |
209 | if (ret_seg == NULL) { | |
210 | kfree(seg); | |
211 | return -ENOMEM; | |
212 | } | |
213 | *ret_seg = seg; | |
214 | agp_add_seg_to_client(client, ret_seg, region->seg_count); | |
215 | return 0; | |
216 | } | |
217 | ||
218 | /* End - Routines for managing each client's segment list */ | |
219 | ||
220 | /* This function must only be called when current_controller != NULL */ | |
221 | static void agp_insert_into_pool(struct agp_memory * temp) | |
222 | { | |
223 | struct agp_memory *prev; | |
224 | ||
225 | prev = agp_fe.current_controller->pool; | |
226 | ||
227 | if (prev != NULL) { | |
228 | prev->prev = temp; | |
229 | temp->next = prev; | |
230 | } | |
231 | agp_fe.current_controller->pool = temp; | |
232 | } | |
233 | ||
234 | ||
235 | /* File private list routines */ | |
236 | ||
408b664a | 237 | static struct agp_file_private *agp_find_private(pid_t pid) |
1da177e4 LT |
238 | { |
239 | struct agp_file_private *curr; | |
240 | ||
241 | curr = agp_fe.file_priv_list; | |
242 | ||
243 | while (curr != NULL) { | |
244 | if (curr->my_pid == pid) | |
245 | return curr; | |
246 | curr = curr->next; | |
247 | } | |
248 | ||
249 | return NULL; | |
250 | } | |
251 | ||
408b664a | 252 | static void agp_insert_file_private(struct agp_file_private * priv) |
1da177e4 LT |
253 | { |
254 | struct agp_file_private *prev; | |
255 | ||
256 | prev = agp_fe.file_priv_list; | |
257 | ||
258 | if (prev != NULL) | |
259 | prev->prev = priv; | |
260 | priv->next = prev; | |
261 | agp_fe.file_priv_list = priv; | |
262 | } | |
263 | ||
408b664a | 264 | static void agp_remove_file_private(struct agp_file_private * priv) |
1da177e4 LT |
265 | { |
266 | struct agp_file_private *next; | |
267 | struct agp_file_private *prev; | |
268 | ||
269 | next = priv->next; | |
270 | prev = priv->prev; | |
271 | ||
272 | if (prev != NULL) { | |
273 | prev->next = next; | |
274 | ||
275 | if (next != NULL) | |
276 | next->prev = prev; | |
277 | ||
278 | } else { | |
279 | if (next != NULL) | |
280 | next->prev = NULL; | |
281 | ||
282 | agp_fe.file_priv_list = next; | |
283 | } | |
284 | } | |
285 | ||
286 | /* End - File flag list routines */ | |
287 | ||
288 | /* | |
289 | * Wrappers for agp_free_memory & agp_allocate_memory | |
290 | * These make sure that internal lists are kept updated. | |
291 | */ | |
292 | static void agp_free_memory_wrap(struct agp_memory *memory) | |
293 | { | |
294 | agp_remove_from_pool(memory); | |
295 | agp_free_memory(memory); | |
296 | } | |
297 | ||
298 | static struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type) | |
299 | { | |
300 | struct agp_memory *memory; | |
301 | ||
302 | memory = agp_allocate_memory(agp_bridge, pg_count, type); | |
303 | if (memory == NULL) | |
304 | return NULL; | |
305 | ||
306 | agp_insert_into_pool(memory); | |
307 | return memory; | |
308 | } | |
309 | ||
310 | /* Routines for managing the list of controllers - | |
311 | * These routines manage the current controller, and the list of | |
312 | * controllers | |
313 | */ | |
314 | ||
315 | static struct agp_controller *agp_find_controller_by_pid(pid_t id) | |
316 | { | |
317 | struct agp_controller *controller; | |
318 | ||
319 | controller = agp_fe.controllers; | |
320 | ||
321 | while (controller != NULL) { | |
322 | if (controller->pid == id) | |
323 | return controller; | |
324 | controller = controller->next; | |
325 | } | |
326 | ||
327 | return NULL; | |
328 | } | |
329 | ||
330 | static struct agp_controller *agp_create_controller(pid_t id) | |
331 | { | |
332 | struct agp_controller *controller; | |
333 | ||
0ea27d9f | 334 | controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL); |
1da177e4 LT |
335 | if (controller == NULL) |
336 | return NULL; | |
337 | ||
1da177e4 | 338 | controller->pid = id; |
1da177e4 LT |
339 | return controller; |
340 | } | |
341 | ||
342 | static int agp_insert_controller(struct agp_controller *controller) | |
343 | { | |
344 | struct agp_controller *prev_controller; | |
345 | ||
346 | prev_controller = agp_fe.controllers; | |
347 | controller->next = prev_controller; | |
348 | ||
349 | if (prev_controller != NULL) | |
350 | prev_controller->prev = controller; | |
351 | ||
352 | agp_fe.controllers = controller; | |
353 | ||
354 | return 0; | |
355 | } | |
356 | ||
357 | static void agp_remove_all_clients(struct agp_controller *controller) | |
358 | { | |
359 | struct agp_client *client; | |
360 | struct agp_client *temp; | |
361 | ||
362 | client = controller->clients; | |
363 | ||
364 | while (client) { | |
365 | struct agp_file_private *priv; | |
366 | ||
367 | temp = client; | |
368 | agp_remove_seg_from_client(temp); | |
369 | priv = agp_find_private(temp->pid); | |
370 | ||
371 | if (priv != NULL) { | |
372 | clear_bit(AGP_FF_IS_VALID, &priv->access_flags); | |
373 | clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags); | |
374 | } | |
375 | client = client->next; | |
376 | kfree(temp); | |
377 | } | |
378 | } | |
379 | ||
380 | static void agp_remove_all_memory(struct agp_controller *controller) | |
381 | { | |
382 | struct agp_memory *memory; | |
383 | struct agp_memory *temp; | |
384 | ||
385 | memory = controller->pool; | |
386 | ||
387 | while (memory) { | |
388 | temp = memory; | |
389 | memory = memory->next; | |
390 | agp_free_memory_wrap(temp); | |
391 | } | |
392 | } | |
393 | ||
394 | static int agp_remove_controller(struct agp_controller *controller) | |
395 | { | |
396 | struct agp_controller *prev_controller; | |
397 | struct agp_controller *next_controller; | |
398 | ||
399 | prev_controller = controller->prev; | |
400 | next_controller = controller->next; | |
401 | ||
402 | if (prev_controller != NULL) { | |
403 | prev_controller->next = next_controller; | |
404 | if (next_controller != NULL) | |
405 | next_controller->prev = prev_controller; | |
406 | ||
407 | } else { | |
408 | if (next_controller != NULL) | |
409 | next_controller->prev = NULL; | |
410 | ||
411 | agp_fe.controllers = next_controller; | |
412 | } | |
413 | ||
414 | agp_remove_all_memory(controller); | |
415 | agp_remove_all_clients(controller); | |
416 | ||
417 | if (agp_fe.current_controller == controller) { | |
418 | agp_fe.current_controller = NULL; | |
419 | agp_fe.backend_acquired = FALSE; | |
420 | agp_backend_release(agp_bridge); | |
421 | } | |
422 | kfree(controller); | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static void agp_controller_make_current(struct agp_controller *controller) | |
427 | { | |
428 | struct agp_client *clients; | |
429 | ||
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 | set_bit(AGP_FF_IS_VALID, &priv->access_flags); | |
439 | set_bit(AGP_FF_IS_CLIENT, &priv->access_flags); | |
440 | } | |
441 | clients = clients->next; | |
442 | } | |
443 | ||
444 | agp_fe.current_controller = controller; | |
445 | } | |
446 | ||
447 | static void agp_controller_release_current(struct agp_controller *controller, | |
448 | struct agp_file_private *controller_priv) | |
449 | { | |
450 | struct agp_client *clients; | |
451 | ||
452 | clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags); | |
453 | clients = controller->clients; | |
454 | ||
455 | while (clients != NULL) { | |
456 | struct agp_file_private *priv; | |
457 | ||
458 | priv = agp_find_private(clients->pid); | |
459 | ||
460 | if (priv != NULL) | |
461 | clear_bit(AGP_FF_IS_VALID, &priv->access_flags); | |
462 | ||
463 | clients = clients->next; | |
464 | } | |
465 | ||
466 | agp_fe.current_controller = NULL; | |
467 | agp_fe.used_by_controller = FALSE; | |
468 | agp_backend_release(agp_bridge); | |
469 | } | |
470 | ||
471 | /* | |
472 | * Routines for managing client lists - | |
473 | * These routines are for managing the list of auth'ed clients. | |
474 | */ | |
475 | ||
476 | static struct agp_client | |
477 | *agp_find_client_in_controller(struct agp_controller *controller, pid_t id) | |
478 | { | |
479 | struct agp_client *client; | |
480 | ||
481 | if (controller == NULL) | |
482 | return NULL; | |
483 | ||
484 | client = controller->clients; | |
485 | ||
486 | while (client != NULL) { | |
487 | if (client->pid == id) | |
488 | return client; | |
489 | client = client->next; | |
490 | } | |
491 | ||
492 | return NULL; | |
493 | } | |
494 | ||
495 | static struct agp_controller *agp_find_controller_for_client(pid_t id) | |
496 | { | |
497 | struct agp_controller *controller; | |
498 | ||
499 | controller = agp_fe.controllers; | |
500 | ||
501 | while (controller != NULL) { | |
502 | if ((agp_find_client_in_controller(controller, id)) != NULL) | |
503 | return controller; | |
504 | controller = controller->next; | |
505 | } | |
506 | ||
507 | return NULL; | |
508 | } | |
509 | ||
510 | static struct agp_client *agp_find_client_by_pid(pid_t id) | |
511 | { | |
512 | struct agp_client *temp; | |
513 | ||
514 | if (agp_fe.current_controller == NULL) | |
515 | return NULL; | |
516 | ||
517 | temp = agp_find_client_in_controller(agp_fe.current_controller, id); | |
518 | return temp; | |
519 | } | |
520 | ||
521 | static void agp_insert_client(struct agp_client *client) | |
522 | { | |
523 | struct agp_client *prev_client; | |
524 | ||
525 | prev_client = agp_fe.current_controller->clients; | |
526 | client->next = prev_client; | |
527 | ||
528 | if (prev_client != NULL) | |
529 | prev_client->prev = client; | |
530 | ||
531 | agp_fe.current_controller->clients = client; | |
532 | agp_fe.current_controller->num_clients++; | |
533 | } | |
534 | ||
535 | static struct agp_client *agp_create_client(pid_t id) | |
536 | { | |
537 | struct agp_client *new_client; | |
538 | ||
0ea27d9f | 539 | new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL); |
1da177e4 LT |
540 | if (new_client == NULL) |
541 | return NULL; | |
542 | ||
1da177e4 LT |
543 | new_client->pid = id; |
544 | agp_insert_client(new_client); | |
545 | return new_client; | |
546 | } | |
547 | ||
548 | static int agp_remove_client(pid_t id) | |
549 | { | |
550 | struct agp_client *client; | |
551 | struct agp_client *prev_client; | |
552 | struct agp_client *next_client; | |
553 | struct agp_controller *controller; | |
554 | ||
555 | controller = agp_find_controller_for_client(id); | |
556 | if (controller == NULL) | |
557 | return -EINVAL; | |
558 | ||
559 | client = agp_find_client_in_controller(controller, id); | |
560 | if (client == NULL) | |
561 | return -EINVAL; | |
562 | ||
563 | prev_client = client->prev; | |
564 | next_client = client->next; | |
565 | ||
566 | if (prev_client != NULL) { | |
567 | prev_client->next = next_client; | |
568 | if (next_client != NULL) | |
569 | next_client->prev = prev_client; | |
570 | ||
571 | } else { | |
572 | if (next_client != NULL) | |
573 | next_client->prev = NULL; | |
574 | controller->clients = next_client; | |
575 | } | |
576 | ||
577 | controller->num_clients--; | |
578 | agp_remove_seg_from_client(client); | |
579 | kfree(client); | |
580 | return 0; | |
581 | } | |
582 | ||
583 | /* End - Routines for managing client lists */ | |
584 | ||
585 | /* File Operations */ | |
586 | ||
587 | static int agp_mmap(struct file *file, struct vm_area_struct *vma) | |
588 | { | |
589 | unsigned int size, current_size; | |
590 | unsigned long offset; | |
591 | struct agp_client *client; | |
592 | struct agp_file_private *priv = file->private_data; | |
593 | struct agp_kern_info kerninfo; | |
594 | ||
16867823 | 595 | mutex_lock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
596 | |
597 | if (agp_fe.backend_acquired != TRUE) | |
598 | goto out_eperm; | |
599 | ||
600 | if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags))) | |
601 | goto out_eperm; | |
602 | ||
603 | agp_copy_info(agp_bridge, &kerninfo); | |
604 | size = vma->vm_end - vma->vm_start; | |
605 | current_size = kerninfo.aper_size; | |
606 | current_size = current_size * 0x100000; | |
607 | offset = vma->vm_pgoff << PAGE_SHIFT; | |
608 | DBG("%lx:%lx", offset, offset+size); | |
609 | ||
610 | if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) { | |
611 | if ((size + offset) > current_size) | |
612 | goto out_inval; | |
613 | ||
614 | client = agp_find_client_by_pid(current->pid); | |
615 | ||
616 | if (client == NULL) | |
617 | goto out_eperm; | |
618 | ||
619 | if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot)) | |
620 | goto out_inval; | |
621 | ||
622 | DBG("client vm_ops=%p", kerninfo.vm_ops); | |
623 | if (kerninfo.vm_ops) { | |
624 | vma->vm_ops = kerninfo.vm_ops; | |
625 | } else if (io_remap_pfn_range(vma, vma->vm_start, | |
626 | (kerninfo.aper_base + offset) >> PAGE_SHIFT, | |
627 | size, vma->vm_page_prot)) { | |
628 | goto out_again; | |
629 | } | |
16867823 | 630 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
631 | return 0; |
632 | } | |
633 | ||
634 | if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) { | |
635 | if (size != current_size) | |
636 | goto out_inval; | |
637 | ||
638 | DBG("controller vm_ops=%p", kerninfo.vm_ops); | |
639 | if (kerninfo.vm_ops) { | |
640 | vma->vm_ops = kerninfo.vm_ops; | |
641 | } else if (io_remap_pfn_range(vma, vma->vm_start, | |
642 | kerninfo.aper_base >> PAGE_SHIFT, | |
643 | size, vma->vm_page_prot)) { | |
644 | goto out_again; | |
645 | } | |
16867823 | 646 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
647 | return 0; |
648 | } | |
649 | ||
650 | out_eperm: | |
16867823 | 651 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
652 | return -EPERM; |
653 | ||
654 | out_inval: | |
16867823 | 655 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
656 | return -EINVAL; |
657 | ||
658 | out_again: | |
16867823 | 659 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
660 | return -EAGAIN; |
661 | } | |
662 | ||
663 | static int agp_release(struct inode *inode, struct file *file) | |
664 | { | |
665 | struct agp_file_private *priv = file->private_data; | |
666 | ||
16867823 | 667 | mutex_lock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
668 | |
669 | DBG("priv=%p", priv); | |
670 | ||
671 | if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) { | |
672 | struct agp_controller *controller; | |
673 | ||
674 | controller = agp_find_controller_by_pid(priv->my_pid); | |
675 | ||
676 | if (controller != NULL) { | |
677 | if (controller == agp_fe.current_controller) | |
678 | agp_controller_release_current(controller, priv); | |
679 | agp_remove_controller(controller); | |
680 | controller = NULL; | |
681 | } | |
682 | } | |
683 | ||
684 | if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) | |
685 | agp_remove_client(priv->my_pid); | |
686 | ||
687 | agp_remove_file_private(priv); | |
688 | kfree(priv); | |
689 | file->private_data = NULL; | |
16867823 | 690 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
691 | return 0; |
692 | } | |
693 | ||
694 | static int agp_open(struct inode *inode, struct file *file) | |
695 | { | |
696 | int minor = iminor(inode); | |
697 | struct agp_file_private *priv; | |
698 | struct agp_client *client; | |
699 | int rc = -ENXIO; | |
700 | ||
16867823 | 701 | mutex_lock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
702 | |
703 | if (minor != AGPGART_MINOR) | |
704 | goto err_out; | |
705 | ||
0ea27d9f | 706 | priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL); |
1da177e4 LT |
707 | if (priv == NULL) |
708 | goto err_out_nomem; | |
709 | ||
1da177e4 LT |
710 | set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags); |
711 | priv->my_pid = current->pid; | |
712 | ||
713 | if ((current->uid == 0) || (current->suid == 0)) { | |
714 | /* Root priv, can be controller */ | |
715 | set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags); | |
716 | } | |
717 | client = agp_find_client_by_pid(current->pid); | |
718 | ||
719 | if (client != NULL) { | |
720 | set_bit(AGP_FF_IS_CLIENT, &priv->access_flags); | |
721 | set_bit(AGP_FF_IS_VALID, &priv->access_flags); | |
722 | } | |
723 | file->private_data = (void *) priv; | |
724 | agp_insert_file_private(priv); | |
725 | DBG("private=%p, client=%p", priv, client); | |
16867823 | 726 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
727 | return 0; |
728 | ||
729 | err_out_nomem: | |
730 | rc = -ENOMEM; | |
731 | err_out: | |
16867823 | 732 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
733 | return rc; |
734 | } | |
735 | ||
736 | ||
737 | static ssize_t agp_read(struct file *file, char __user *buf, | |
738 | size_t count, loff_t * ppos) | |
739 | { | |
740 | return -EINVAL; | |
741 | } | |
742 | ||
743 | static ssize_t agp_write(struct file *file, const char __user *buf, | |
744 | size_t count, loff_t * ppos) | |
745 | { | |
746 | return -EINVAL; | |
747 | } | |
748 | ||
749 | static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg) | |
750 | { | |
751 | struct agp_info userinfo; | |
752 | struct agp_kern_info kerninfo; | |
753 | ||
754 | agp_copy_info(agp_bridge, &kerninfo); | |
755 | ||
756 | userinfo.version.major = kerninfo.version.major; | |
757 | userinfo.version.minor = kerninfo.version.minor; | |
758 | userinfo.bridge_id = kerninfo.device->vendor | | |
759 | (kerninfo.device->device << 16); | |
760 | userinfo.agp_mode = kerninfo.mode; | |
761 | userinfo.aper_base = kerninfo.aper_base; | |
762 | userinfo.aper_size = kerninfo.aper_size; | |
763 | userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory; | |
764 | userinfo.pg_used = kerninfo.current_memory; | |
765 | ||
766 | if (copy_to_user(arg, &userinfo, sizeof(struct agp_info))) | |
767 | return -EFAULT; | |
768 | ||
769 | return 0; | |
770 | } | |
771 | ||
772 | static int agpioc_acquire_wrap(struct agp_file_private *priv) | |
773 | { | |
774 | struct agp_controller *controller; | |
775 | ||
776 | DBG(""); | |
777 | ||
778 | if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags))) | |
779 | return -EPERM; | |
780 | ||
781 | if (agp_fe.current_controller != NULL) | |
782 | return -EBUSY; | |
783 | ||
6a92a4e0 | 784 | if (!agp_bridge) |
1da177e4 LT |
785 | return -ENODEV; |
786 | ||
787 | if (atomic_read(&agp_bridge->agp_in_use)) | |
788 | return -EBUSY; | |
789 | ||
790 | atomic_inc(&agp_bridge->agp_in_use); | |
791 | ||
792 | agp_fe.backend_acquired = TRUE; | |
793 | ||
794 | controller = agp_find_controller_by_pid(priv->my_pid); | |
795 | ||
796 | if (controller != NULL) { | |
797 | agp_controller_make_current(controller); | |
798 | } else { | |
799 | controller = agp_create_controller(priv->my_pid); | |
800 | ||
801 | if (controller == NULL) { | |
802 | agp_fe.backend_acquired = FALSE; | |
803 | agp_backend_release(agp_bridge); | |
804 | return -ENOMEM; | |
805 | } | |
806 | agp_insert_controller(controller); | |
807 | agp_controller_make_current(controller); | |
808 | } | |
809 | ||
810 | set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags); | |
811 | set_bit(AGP_FF_IS_VALID, &priv->access_flags); | |
812 | return 0; | |
813 | } | |
814 | ||
815 | static int agpioc_release_wrap(struct agp_file_private *priv) | |
816 | { | |
817 | DBG(""); | |
818 | agp_controller_release_current(agp_fe.current_controller, priv); | |
819 | return 0; | |
820 | } | |
821 | ||
822 | static int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg) | |
823 | { | |
824 | struct agp_setup mode; | |
825 | ||
826 | DBG(""); | |
827 | if (copy_from_user(&mode, arg, sizeof(struct agp_setup))) | |
828 | return -EFAULT; | |
829 | ||
830 | agp_enable(agp_bridge, mode.agp_mode); | |
831 | return 0; | |
832 | } | |
833 | ||
834 | static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg) | |
835 | { | |
836 | struct agp_region reserve; | |
837 | struct agp_client *client; | |
838 | struct agp_file_private *client_priv; | |
839 | ||
840 | DBG(""); | |
841 | if (copy_from_user(&reserve, arg, sizeof(struct agp_region))) | |
842 | return -EFAULT; | |
843 | ||
844 | if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment)) | |
845 | return -EFAULT; | |
846 | ||
847 | client = agp_find_client_by_pid(reserve.pid); | |
848 | ||
849 | if (reserve.seg_count == 0) { | |
850 | /* remove a client */ | |
851 | client_priv = agp_find_private(reserve.pid); | |
852 | ||
853 | if (client_priv != NULL) { | |
854 | set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags); | |
855 | set_bit(AGP_FF_IS_VALID, &client_priv->access_flags); | |
856 | } | |
857 | if (client == NULL) { | |
858 | /* client is already removed */ | |
859 | return 0; | |
860 | } | |
861 | return agp_remove_client(reserve.pid); | |
862 | } else { | |
863 | struct agp_segment *segment; | |
864 | ||
865 | if (reserve.seg_count >= 16384) | |
866 | return -EINVAL; | |
867 | ||
868 | segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count), | |
869 | GFP_KERNEL); | |
870 | ||
871 | if (segment == NULL) | |
872 | return -ENOMEM; | |
873 | ||
874 | if (copy_from_user(segment, (void __user *) reserve.seg_list, | |
875 | sizeof(struct agp_segment) * reserve.seg_count)) { | |
876 | kfree(segment); | |
877 | return -EFAULT; | |
878 | } | |
879 | reserve.seg_list = segment; | |
880 | ||
881 | if (client == NULL) { | |
882 | /* Create the client and add the segment */ | |
883 | client = agp_create_client(reserve.pid); | |
884 | ||
885 | if (client == NULL) { | |
886 | kfree(segment); | |
887 | return -ENOMEM; | |
888 | } | |
889 | client_priv = agp_find_private(reserve.pid); | |
890 | ||
891 | if (client_priv != NULL) { | |
892 | set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags); | |
893 | set_bit(AGP_FF_IS_VALID, &client_priv->access_flags); | |
894 | } | |
895 | } | |
896 | return agp_create_segment(client, &reserve); | |
897 | } | |
898 | /* Will never really happen */ | |
899 | return -EINVAL; | |
900 | } | |
901 | ||
902 | static int agpioc_protect_wrap(struct agp_file_private *priv) | |
903 | { | |
904 | DBG(""); | |
905 | /* This function is not currently implemented */ | |
906 | return -EINVAL; | |
907 | } | |
908 | ||
909 | static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg) | |
910 | { | |
911 | struct agp_memory *memory; | |
912 | struct agp_allocate alloc; | |
913 | ||
914 | DBG(""); | |
915 | if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate))) | |
916 | return -EFAULT; | |
917 | ||
918 | memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type); | |
919 | ||
920 | if (memory == NULL) | |
921 | return -ENOMEM; | |
922 | ||
923 | alloc.key = memory->key; | |
924 | alloc.physical = memory->physical; | |
925 | ||
926 | if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) { | |
927 | agp_free_memory_wrap(memory); | |
928 | return -EFAULT; | |
929 | } | |
930 | return 0; | |
931 | } | |
932 | ||
933 | static int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg) | |
934 | { | |
935 | struct agp_memory *memory; | |
936 | ||
937 | DBG(""); | |
938 | memory = agp_find_mem_by_key(arg); | |
939 | ||
940 | if (memory == NULL) | |
941 | return -EINVAL; | |
942 | ||
943 | agp_free_memory_wrap(memory); | |
944 | return 0; | |
945 | } | |
946 | ||
947 | static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg) | |
948 | { | |
949 | struct agp_bind bind_info; | |
950 | struct agp_memory *memory; | |
951 | ||
952 | DBG(""); | |
953 | if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind))) | |
954 | return -EFAULT; | |
955 | ||
956 | memory = agp_find_mem_by_key(bind_info.key); | |
957 | ||
958 | if (memory == NULL) | |
959 | return -EINVAL; | |
960 | ||
961 | return agp_bind_memory(memory, bind_info.pg_start); | |
962 | } | |
963 | ||
964 | static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg) | |
965 | { | |
966 | struct agp_memory *memory; | |
967 | struct agp_unbind unbind; | |
968 | ||
969 | DBG(""); | |
970 | if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind))) | |
971 | return -EFAULT; | |
972 | ||
973 | memory = agp_find_mem_by_key(unbind.key); | |
974 | ||
975 | if (memory == NULL) | |
976 | return -EINVAL; | |
977 | ||
978 | return agp_unbind_memory(memory); | |
979 | } | |
980 | ||
981 | static int agp_ioctl(struct inode *inode, struct file *file, | |
982 | unsigned int cmd, unsigned long arg) | |
983 | { | |
984 | struct agp_file_private *curr_priv = file->private_data; | |
985 | int ret_val = -ENOTTY; | |
986 | ||
987 | DBG("priv=%p, cmd=%x", curr_priv, cmd); | |
16867823 | 988 | mutex_lock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
989 | |
990 | if ((agp_fe.current_controller == NULL) && | |
991 | (cmd != AGPIOC_ACQUIRE)) { | |
992 | ret_val = -EINVAL; | |
993 | goto ioctl_out; | |
994 | } | |
995 | if ((agp_fe.backend_acquired != TRUE) && | |
996 | (cmd != AGPIOC_ACQUIRE)) { | |
997 | ret_val = -EBUSY; | |
998 | goto ioctl_out; | |
999 | } | |
1000 | if (cmd != AGPIOC_ACQUIRE) { | |
1001 | if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) { | |
1002 | ret_val = -EPERM; | |
1003 | goto ioctl_out; | |
1004 | } | |
1005 | /* Use the original pid of the controller, | |
1006 | * in case it's threaded */ | |
1007 | ||
1008 | if (agp_fe.current_controller->pid != curr_priv->my_pid) { | |
1009 | ret_val = -EBUSY; | |
1010 | goto ioctl_out; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | switch (cmd) { | |
1015 | case AGPIOC_INFO: | |
1016 | ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg); | |
1017 | break; | |
1018 | ||
1019 | case AGPIOC_ACQUIRE: | |
1020 | ret_val = agpioc_acquire_wrap(curr_priv); | |
1021 | break; | |
1022 | ||
1023 | case AGPIOC_RELEASE: | |
1024 | ret_val = agpioc_release_wrap(curr_priv); | |
1025 | break; | |
1026 | ||
1027 | case AGPIOC_SETUP: | |
1028 | ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg); | |
1029 | break; | |
1030 | ||
1031 | case AGPIOC_RESERVE: | |
1032 | ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg); | |
1033 | break; | |
1034 | ||
1035 | case AGPIOC_PROTECT: | |
1036 | ret_val = agpioc_protect_wrap(curr_priv); | |
1037 | break; | |
1038 | ||
1039 | case AGPIOC_ALLOCATE: | |
1040 | ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg); | |
1041 | break; | |
1042 | ||
1043 | case AGPIOC_DEALLOCATE: | |
1044 | ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg); | |
1045 | break; | |
1046 | ||
1047 | case AGPIOC_BIND: | |
1048 | ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg); | |
1049 | break; | |
1050 | ||
1051 | case AGPIOC_UNBIND: | |
1052 | ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg); | |
1053 | break; | |
1054 | } | |
1055 | ||
1056 | ioctl_out: | |
1057 | DBG("ioctl returns %d\n", ret_val); | |
16867823 | 1058 | mutex_unlock(&(agp_fe.agp_mutex)); |
1da177e4 LT |
1059 | return ret_val; |
1060 | } | |
1061 | ||
1062 | static struct file_operations agp_fops = | |
1063 | { | |
1064 | .owner = THIS_MODULE, | |
1065 | .llseek = no_llseek, | |
1066 | .read = agp_read, | |
1067 | .write = agp_write, | |
1068 | .ioctl = agp_ioctl, | |
1069 | .mmap = agp_mmap, | |
1070 | .open = agp_open, | |
1071 | .release = agp_release, | |
1072 | }; | |
1073 | ||
1074 | static struct miscdevice agp_miscdev = | |
1075 | { | |
1076 | .minor = AGPGART_MINOR, | |
1077 | .name = "agpgart", | |
1078 | .fops = &agp_fops | |
1079 | }; | |
1080 | ||
1081 | int agp_frontend_initialize(void) | |
1082 | { | |
1083 | memset(&agp_fe, 0, sizeof(struct agp_front_data)); | |
16867823 | 1084 | mutex_init(&(agp_fe.agp_mutex)); |
1da177e4 LT |
1085 | |
1086 | if (misc_register(&agp_miscdev)) { | |
1087 | printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR); | |
1088 | return -EIO; | |
1089 | } | |
1090 | return 0; | |
1091 | } | |
1092 | ||
1093 | void agp_frontend_cleanup(void) | |
1094 | { | |
1095 | misc_deregister(&agp_miscdev); | |
1096 | } |