frontend.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. #include <linux/types.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/mman.h>
  32. #include <linux/pci.h>
  33. #include <linux/init.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/agp_backend.h>
  36. #include <linux/agpgart.h>
  37. #include <linux/slab.h>
  38. #include <linux/mm.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/pgtable.h>
  41. #include "agp.h"
  42. static struct agp_front_data agp_fe;
  43. static struct agp_memory *agp_find_mem_by_key(int key)
  44. {
  45. struct agp_memory *curr;
  46. if (agp_fe.current_controller == NULL)
  47. return NULL;
  48. curr = agp_fe.current_controller->pool;
  49. while (curr != NULL) {
  50. if (curr->key == key)
  51. break;
  52. curr = curr->next;
  53. }
  54. DBG("key=%d -> mem=%p", key, curr);
  55. return curr;
  56. }
  57. static void agp_remove_from_pool(struct agp_memory *temp)
  58. {
  59. struct agp_memory *prev;
  60. struct agp_memory *next;
  61. /* Check to see if this is even in the memory pool */
  62. DBG("mem=%p", temp);
  63. if (agp_find_mem_by_key(temp->key) != NULL) {
  64. next = temp->next;
  65. prev = temp->prev;
  66. if (prev != NULL) {
  67. prev->next = next;
  68. if (next != NULL)
  69. next->prev = prev;
  70. } else {
  71. /* This is the first item on the list */
  72. if (next != NULL)
  73. next->prev = NULL;
  74. agp_fe.current_controller->pool = next;
  75. }
  76. }
  77. }
  78. /*
  79. * Routines for managing each client's segment list -
  80. * These routines handle adding and removing segments
  81. * to each auth'ed client.
  82. */
  83. static struct
  84. agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
  85. unsigned long offset,
  86. int size, pgprot_t page_prot)
  87. {
  88. struct agp_segment_priv *seg;
  89. int num_segments, i;
  90. off_t pg_start;
  91. size_t pg_count;
  92. pg_start = offset / 4096;
  93. pg_count = size / 4096;
  94. seg = *(client->segments);
  95. num_segments = client->num_segments;
  96. for (i = 0; i < client->num_segments; i++) {
  97. if ((seg[i].pg_start == pg_start) &&
  98. (seg[i].pg_count == pg_count) &&
  99. (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
  100. return seg + i;
  101. }
  102. }
  103. return NULL;
  104. }
  105. static void agp_remove_seg_from_client(struct agp_client *client)
  106. {
  107. DBG("client=%p", client);
  108. if (client->segments != NULL) {
  109. if (*(client->segments) != NULL) {
  110. DBG("Freeing %p from client %p", *(client->segments), client);
  111. kfree(*(client->segments));
  112. }
  113. DBG("Freeing %p from client %p", client->segments, client);
  114. kfree(client->segments);
  115. client->segments = NULL;
  116. }
  117. }
  118. static void agp_add_seg_to_client(struct agp_client *client,
  119. struct agp_segment_priv ** seg, int num_segments)
  120. {
  121. struct agp_segment_priv **prev_seg;
  122. prev_seg = client->segments;
  123. if (prev_seg != NULL)
  124. agp_remove_seg_from_client(client);
  125. DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
  126. client->num_segments = num_segments;
  127. client->segments = seg;
  128. }
  129. /* Originally taken from linux/mm/mmap.c from the array
  130. * protection_map.
  131. * The original really should be exported to modules, or
  132. * some routine which does the conversion for you
  133. */
  134. static const pgprot_t my_protect_map[16] =
  135. {
  136. __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
  137. __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
  138. };
  139. static pgprot_t agp_convert_mmap_flags(int prot)
  140. {
  141. #define _trans(x,bit1,bit2) \
  142. ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
  143. unsigned long prot_bits;
  144. pgprot_t temp;
  145. prot_bits = _trans(prot, PROT_READ, VM_READ) |
  146. _trans(prot, PROT_WRITE, VM_WRITE) |
  147. _trans(prot, PROT_EXEC, VM_EXEC);
  148. prot_bits |= VM_SHARED;
  149. temp = my_protect_map[prot_bits & 0x0000000f];
  150. return temp;
  151. }
  152. static int agp_create_segment(struct agp_client *client, struct agp_region *region)
  153. {
  154. struct agp_segment_priv **ret_seg;
  155. struct agp_segment_priv *seg;
  156. struct agp_segment *user_seg;
  157. size_t i;
  158. seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
  159. if (seg == NULL) {
  160. kfree(region->seg_list);
  161. region->seg_list = NULL;
  162. return -ENOMEM;
  163. }
  164. user_seg = region->seg_list;
  165. for (i = 0; i < region->seg_count; i++) {
  166. seg[i].pg_start = user_seg[i].pg_start;
  167. seg[i].pg_count = user_seg[i].pg_count;
  168. seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
  169. }
  170. kfree(region->seg_list);
  171. region->seg_list = NULL;
  172. ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
  173. if (ret_seg == NULL) {
  174. kfree(seg);
  175. return -ENOMEM;
  176. }
  177. *ret_seg = seg;
  178. agp_add_seg_to_client(client, ret_seg, region->seg_count);
  179. return 0;
  180. }
  181. /* End - Routines for managing each client's segment list */
  182. /* This function must only be called when current_controller != NULL */
  183. static void agp_insert_into_pool(struct agp_memory * temp)
  184. {
  185. struct agp_memory *prev;
  186. prev = agp_fe.current_controller->pool;
  187. if (prev != NULL) {
  188. prev->prev = temp;
  189. temp->next = prev;
  190. }
  191. agp_fe.current_controller->pool = temp;
  192. }
  193. /* File private list routines */
  194. static struct agp_file_private *agp_find_private(pid_t pid)
  195. {
  196. struct agp_file_private *curr;
  197. curr = agp_fe.file_priv_list;
  198. while (curr != NULL) {
  199. if (curr->my_pid == pid)
  200. return curr;
  201. curr = curr->next;
  202. }
  203. return NULL;
  204. }
  205. static void agp_insert_file_private(struct agp_file_private * priv)
  206. {
  207. struct agp_file_private *prev;
  208. prev = agp_fe.file_priv_list;
  209. if (prev != NULL)
  210. prev->prev = priv;
  211. priv->next = prev;
  212. agp_fe.file_priv_list = priv;
  213. }
  214. static void agp_remove_file_private(struct agp_file_private * priv)
  215. {
  216. struct agp_file_private *next;
  217. struct agp_file_private *prev;
  218. next = priv->next;
  219. prev = priv->prev;
  220. if (prev != NULL) {
  221. prev->next = next;
  222. if (next != NULL)
  223. next->prev = prev;
  224. } else {
  225. if (next != NULL)
  226. next->prev = NULL;
  227. agp_fe.file_priv_list = next;
  228. }
  229. }
  230. /* End - File flag list routines */
  231. /*
  232. * Wrappers for agp_free_memory & agp_allocate_memory
  233. * These make sure that internal lists are kept updated.
  234. */
  235. static void agp_free_memory_wrap(struct agp_memory *memory)
  236. {
  237. agp_remove_from_pool(memory);
  238. agp_free_memory(memory);
  239. }
  240. static struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
  241. {
  242. struct agp_memory *memory;
  243. memory = agp_allocate_memory(agp_bridge, pg_count, type);
  244. if (memory == NULL)
  245. return NULL;
  246. agp_insert_into_pool(memory);
  247. return memory;
  248. }
  249. /* Routines for managing the list of controllers -
  250. * These routines manage the current controller, and the list of
  251. * controllers
  252. */
  253. static struct agp_controller *agp_find_controller_by_pid(pid_t id)
  254. {
  255. struct agp_controller *controller;
  256. controller = agp_fe.controllers;
  257. while (controller != NULL) {
  258. if (controller->pid == id)
  259. return controller;
  260. controller = controller->next;
  261. }
  262. return NULL;
  263. }
  264. static struct agp_controller *agp_create_controller(pid_t id)
  265. {
  266. struct agp_controller *controller;
  267. controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
  268. if (controller == NULL)
  269. return NULL;
  270. controller->pid = id;
  271. return controller;
  272. }
  273. static int agp_insert_controller(struct agp_controller *controller)
  274. {
  275. struct agp_controller *prev_controller;
  276. prev_controller = agp_fe.controllers;
  277. controller->next = prev_controller;
  278. if (prev_controller != NULL)
  279. prev_controller->prev = controller;
  280. agp_fe.controllers = controller;
  281. return 0;
  282. }
  283. static void agp_remove_all_clients(struct agp_controller *controller)
  284. {
  285. struct agp_client *client;
  286. struct agp_client *temp;
  287. client = controller->clients;
  288. while (client) {
  289. struct agp_file_private *priv;
  290. temp = client;
  291. agp_remove_seg_from_client(temp);
  292. priv = agp_find_private(temp->pid);
  293. if (priv != NULL) {
  294. clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
  295. clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
  296. }
  297. client = client->next;
  298. kfree(temp);
  299. }
  300. }
  301. static void agp_remove_all_memory(struct agp_controller *controller)
  302. {
  303. struct agp_memory *memory;
  304. struct agp_memory *temp;
  305. memory = controller->pool;
  306. while (memory) {
  307. temp = memory;
  308. memory = memory->next;
  309. agp_free_memory_wrap(temp);
  310. }
  311. }
  312. static int agp_remove_controller(struct agp_controller *controller)
  313. {
  314. struct agp_controller *prev_controller;
  315. struct agp_controller *next_controller;
  316. prev_controller = controller->prev;
  317. next_controller = controller->next;
  318. if (prev_controller != NULL) {
  319. prev_controller->next = next_controller;
  320. if (next_controller != NULL)
  321. next_controller->prev = prev_controller;
  322. } else {
  323. if (next_controller != NULL)
  324. next_controller->prev = NULL;
  325. agp_fe.controllers = next_controller;
  326. }
  327. agp_remove_all_memory(controller);
  328. agp_remove_all_clients(controller);
  329. if (agp_fe.current_controller == controller) {
  330. agp_fe.current_controller = NULL;
  331. agp_fe.backend_acquired = FALSE;
  332. agp_backend_release(agp_bridge);
  333. }
  334. kfree(controller);
  335. return 0;
  336. }
  337. static void agp_controller_make_current(struct agp_controller *controller)
  338. {
  339. struct agp_client *clients;
  340. clients = controller->clients;
  341. while (clients != NULL) {
  342. struct agp_file_private *priv;
  343. priv = agp_find_private(clients->pid);
  344. if (priv != NULL) {
  345. set_bit(AGP_FF_IS_VALID, &priv->access_flags);
  346. set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
  347. }
  348. clients = clients->next;
  349. }
  350. agp_fe.current_controller = controller;
  351. }
  352. static void agp_controller_release_current(struct agp_controller *controller,
  353. struct agp_file_private *controller_priv)
  354. {
  355. struct agp_client *clients;
  356. clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
  357. clients = controller->clients;
  358. while (clients != NULL) {
  359. struct agp_file_private *priv;
  360. priv = agp_find_private(clients->pid);
  361. if (priv != NULL)
  362. clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
  363. clients = clients->next;
  364. }
  365. agp_fe.current_controller = NULL;
  366. agp_fe.used_by_controller = FALSE;
  367. agp_backend_release(agp_bridge);
  368. }
  369. /*
  370. * Routines for managing client lists -
  371. * These routines are for managing the list of auth'ed clients.
  372. */
  373. static struct agp_client
  374. *agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
  375. {
  376. struct agp_client *client;
  377. if (controller == NULL)
  378. return NULL;
  379. client = controller->clients;
  380. while (client != NULL) {
  381. if (client->pid == id)
  382. return client;
  383. client = client->next;
  384. }
  385. return NULL;
  386. }
  387. static struct agp_controller *agp_find_controller_for_client(pid_t id)
  388. {
  389. struct agp_controller *controller;
  390. controller = agp_fe.controllers;
  391. while (controller != NULL) {
  392. if ((agp_find_client_in_controller(controller, id)) != NULL)
  393. return controller;
  394. controller = controller->next;
  395. }
  396. return NULL;
  397. }
  398. static struct agp_client *agp_find_client_by_pid(pid_t id)
  399. {
  400. struct agp_client *temp;
  401. if (agp_fe.current_controller == NULL)
  402. return NULL;
  403. temp = agp_find_client_in_controller(agp_fe.current_controller, id);
  404. return temp;
  405. }
  406. static void agp_insert_client(struct agp_client *client)
  407. {
  408. struct agp_client *prev_client;
  409. prev_client = agp_fe.current_controller->clients;
  410. client->next = prev_client;
  411. if (prev_client != NULL)
  412. prev_client->prev = client;
  413. agp_fe.current_controller->clients = client;
  414. agp_fe.current_controller->num_clients++;
  415. }
  416. static struct agp_client *agp_create_client(pid_t id)
  417. {
  418. struct agp_client *new_client;
  419. new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
  420. if (new_client == NULL)
  421. return NULL;
  422. new_client->pid = id;
  423. agp_insert_client(new_client);
  424. return new_client;
  425. }
  426. static int agp_remove_client(pid_t id)
  427. {
  428. struct agp_client *client;
  429. struct agp_client *prev_client;
  430. struct agp_client *next_client;
  431. struct agp_controller *controller;
  432. controller = agp_find_controller_for_client(id);
  433. if (controller == NULL)
  434. return -EINVAL;
  435. client = agp_find_client_in_controller(controller, id);
  436. if (client == NULL)
  437. return -EINVAL;
  438. prev_client = client->prev;
  439. next_client = client->next;
  440. if (prev_client != NULL) {
  441. prev_client->next = next_client;
  442. if (next_client != NULL)
  443. next_client->prev = prev_client;
  444. } else {
  445. if (next_client != NULL)
  446. next_client->prev = NULL;
  447. controller->clients = next_client;
  448. }
  449. controller->num_clients--;
  450. agp_remove_seg_from_client(client);
  451. kfree(client);
  452. return 0;
  453. }
  454. /* End - Routines for managing client lists */
  455. /* File Operations */
  456. static int agp_mmap(struct file *file, struct vm_area_struct *vma)
  457. {
  458. unsigned int size, current_size;
  459. unsigned long offset;
  460. struct agp_client *client;
  461. struct agp_file_private *priv = file->private_data;
  462. struct agp_kern_info kerninfo;
  463. mutex_lock(&(agp_fe.agp_mutex));
  464. if (agp_fe.backend_acquired != TRUE)
  465. goto out_eperm;
  466. if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
  467. goto out_eperm;
  468. agp_copy_info(agp_bridge, &kerninfo);
  469. size = vma->vm_end - vma->vm_start;
  470. current_size = kerninfo.aper_size;
  471. current_size = current_size * 0x100000;
  472. offset = vma->vm_pgoff << PAGE_SHIFT;
  473. DBG("%lx:%lx", offset, offset+size);
  474. if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
  475. if ((size + offset) > current_size)
  476. goto out_inval;
  477. client = agp_find_client_by_pid(current->pid);
  478. if (client == NULL)
  479. goto out_eperm;
  480. if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
  481. goto out_inval;
  482. DBG("client vm_ops=%p", kerninfo.vm_ops);
  483. if (kerninfo.vm_ops) {
  484. vma->vm_ops = kerninfo.vm_ops;
  485. } else if (io_remap_pfn_range(vma, vma->vm_start,
  486. (kerninfo.aper_base + offset) >> PAGE_SHIFT,
  487. size, vma->vm_page_prot)) {
  488. goto out_again;
  489. }
  490. mutex_unlock(&(agp_fe.agp_mutex));
  491. return 0;
  492. }
  493. if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
  494. if (size != current_size)
  495. goto out_inval;
  496. DBG("controller vm_ops=%p", kerninfo.vm_ops);
  497. if (kerninfo.vm_ops) {
  498. vma->vm_ops = kerninfo.vm_ops;
  499. } else if (io_remap_pfn_range(vma, vma->vm_start,
  500. kerninfo.aper_base >> PAGE_SHIFT,
  501. size, vma->vm_page_prot)) {
  502. goto out_again;
  503. }
  504. mutex_unlock(&(agp_fe.agp_mutex));
  505. return 0;
  506. }
  507. out_eperm:
  508. mutex_unlock(&(agp_fe.agp_mutex));
  509. return -EPERM;
  510. out_inval:
  511. mutex_unlock(&(agp_fe.agp_mutex));
  512. return -EINVAL;
  513. out_again:
  514. mutex_unlock(&(agp_fe.agp_mutex));
  515. return -EAGAIN;
  516. }
  517. static int agp_release(struct inode *inode, struct file *file)
  518. {
  519. struct agp_file_private *priv = file->private_data;
  520. mutex_lock(&(agp_fe.agp_mutex));
  521. DBG("priv=%p", priv);
  522. if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
  523. struct agp_controller *controller;
  524. controller = agp_find_controller_by_pid(priv->my_pid);
  525. if (controller != NULL) {
  526. if (controller == agp_fe.current_controller)
  527. agp_controller_release_current(controller, priv);
  528. agp_remove_controller(controller);
  529. controller = NULL;
  530. }
  531. }
  532. if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
  533. agp_remove_client(priv->my_pid);
  534. agp_remove_file_private(priv);
  535. kfree(priv);
  536. file->private_data = NULL;
  537. mutex_unlock(&(agp_fe.agp_mutex));
  538. return 0;
  539. }
  540. static int agp_open(struct inode *inode, struct file *file)
  541. {
  542. int minor = iminor(inode);
  543. struct agp_file_private *priv;
  544. struct agp_client *client;
  545. int rc = -ENXIO;
  546. mutex_lock(&(agp_fe.agp_mutex));
  547. if (minor != AGPGART_MINOR)
  548. goto err_out;
  549. priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
  550. if (priv == NULL)
  551. goto err_out_nomem;
  552. set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
  553. priv->my_pid = current->pid;
  554. if ((current->uid == 0) || (current->suid == 0)) {
  555. /* Root priv, can be controller */
  556. set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
  557. }
  558. client = agp_find_client_by_pid(current->pid);
  559. if (client != NULL) {
  560. set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
  561. set_bit(AGP_FF_IS_VALID, &priv->access_flags);
  562. }
  563. file->private_data = (void *) priv;
  564. agp_insert_file_private(priv);
  565. DBG("private=%p, client=%p", priv, client);
  566. mutex_unlock(&(agp_fe.agp_mutex));
  567. return 0;
  568. err_out_nomem:
  569. rc = -ENOMEM;
  570. err_out:
  571. mutex_unlock(&(agp_fe.agp_mutex));
  572. return rc;
  573. }
  574. static ssize_t agp_read(struct file *file, char __user *buf,
  575. size_t count, loff_t * ppos)
  576. {
  577. return -EINVAL;
  578. }
  579. static ssize_t agp_write(struct file *file, const char __user *buf,
  580. size_t count, loff_t * ppos)
  581. {
  582. return -EINVAL;
  583. }
  584. static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
  585. {
  586. struct agp_info userinfo;
  587. struct agp_kern_info kerninfo;
  588. agp_copy_info(agp_bridge, &kerninfo);
  589. userinfo.version.major = kerninfo.version.major;
  590. userinfo.version.minor = kerninfo.version.minor;
  591. userinfo.bridge_id = kerninfo.device->vendor |
  592. (kerninfo.device->device << 16);
  593. userinfo.agp_mode = kerninfo.mode;
  594. userinfo.aper_base = kerninfo.aper_base;
  595. userinfo.aper_size = kerninfo.aper_size;
  596. userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
  597. userinfo.pg_used = kerninfo.current_memory;
  598. if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
  599. return -EFAULT;
  600. return 0;
  601. }
  602. static int agpioc_acquire_wrap(struct agp_file_private *priv)
  603. {
  604. struct agp_controller *controller;
  605. DBG("");
  606. if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
  607. return -EPERM;
  608. if (agp_fe.current_controller != NULL)
  609. return -EBUSY;
  610. if (!agp_bridge)
  611. return -ENODEV;
  612. if (atomic_read(&agp_bridge->agp_in_use))
  613. return -EBUSY;
  614. atomic_inc(&agp_bridge->agp_in_use);
  615. agp_fe.backend_acquired = TRUE;
  616. controller = agp_find_controller_by_pid(priv->my_pid);
  617. if (controller != NULL) {
  618. agp_controller_make_current(controller);
  619. } else {
  620. controller = agp_create_controller(priv->my_pid);
  621. if (controller == NULL) {
  622. agp_fe.backend_acquired = FALSE;
  623. agp_backend_release(agp_bridge);
  624. return -ENOMEM;
  625. }
  626. agp_insert_controller(controller);
  627. agp_controller_make_current(controller);
  628. }
  629. set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
  630. set_bit(AGP_FF_IS_VALID, &priv->access_flags);
  631. return 0;
  632. }
  633. static int agpioc_release_wrap(struct agp_file_private *priv)
  634. {
  635. DBG("");
  636. agp_controller_release_current(agp_fe.current_controller, priv);
  637. return 0;
  638. }
  639. static int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
  640. {
  641. struct agp_setup mode;
  642. DBG("");
  643. if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
  644. return -EFAULT;
  645. agp_enable(agp_bridge, mode.agp_mode);
  646. return 0;
  647. }
  648. static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
  649. {
  650. struct agp_region reserve;
  651. struct agp_client *client;
  652. struct agp_file_private *client_priv;
  653. DBG("");
  654. if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
  655. return -EFAULT;
  656. if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
  657. return -EFAULT;
  658. client = agp_find_client_by_pid(reserve.pid);
  659. if (reserve.seg_count == 0) {
  660. /* remove a client */
  661. client_priv = agp_find_private(reserve.pid);
  662. if (client_priv != NULL) {
  663. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  664. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  665. }
  666. if (client == NULL) {
  667. /* client is already removed */
  668. return 0;
  669. }
  670. return agp_remove_client(reserve.pid);
  671. } else {
  672. struct agp_segment *segment;
  673. if (reserve.seg_count >= 16384)
  674. return -EINVAL;
  675. segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
  676. GFP_KERNEL);
  677. if (segment == NULL)
  678. return -ENOMEM;
  679. if (copy_from_user(segment, (void __user *) reserve.seg_list,
  680. sizeof(struct agp_segment) * reserve.seg_count)) {
  681. kfree(segment);
  682. return -EFAULT;
  683. }
  684. reserve.seg_list = segment;
  685. if (client == NULL) {
  686. /* Create the client and add the segment */
  687. client = agp_create_client(reserve.pid);
  688. if (client == NULL) {
  689. kfree(segment);
  690. return -ENOMEM;
  691. }
  692. client_priv = agp_find_private(reserve.pid);
  693. if (client_priv != NULL) {
  694. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  695. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  696. }
  697. }
  698. return agp_create_segment(client, &reserve);
  699. }
  700. /* Will never really happen */
  701. return -EINVAL;
  702. }
  703. static int agpioc_protect_wrap(struct agp_file_private *priv)
  704. {
  705. DBG("");
  706. /* This function is not currently implemented */
  707. return -EINVAL;
  708. }
  709. static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
  710. {
  711. struct agp_memory *memory;
  712. struct agp_allocate alloc;
  713. DBG("");
  714. if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
  715. return -EFAULT;
  716. memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
  717. if (memory == NULL)
  718. return -ENOMEM;
  719. alloc.key = memory->key;
  720. alloc.physical = memory->physical;
  721. if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
  722. agp_free_memory_wrap(memory);
  723. return -EFAULT;
  724. }
  725. return 0;
  726. }
  727. static int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
  728. {
  729. struct agp_memory *memory;
  730. DBG("");
  731. memory = agp_find_mem_by_key(arg);
  732. if (memory == NULL)
  733. return -EINVAL;
  734. agp_free_memory_wrap(memory);
  735. return 0;
  736. }
  737. static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
  738. {
  739. struct agp_bind bind_info;
  740. struct agp_memory *memory;
  741. DBG("");
  742. if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
  743. return -EFAULT;
  744. memory = agp_find_mem_by_key(bind_info.key);
  745. if (memory == NULL)
  746. return -EINVAL;
  747. return agp_bind_memory(memory, bind_info.pg_start);
  748. }
  749. static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
  750. {
  751. struct agp_memory *memory;
  752. struct agp_unbind unbind;
  753. DBG("");
  754. if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
  755. return -EFAULT;
  756. memory = agp_find_mem_by_key(unbind.key);
  757. if (memory == NULL)
  758. return -EINVAL;
  759. return agp_unbind_memory(memory);
  760. }
  761. static int agp_ioctl(struct inode *inode, struct file *file,
  762. unsigned int cmd, unsigned long arg)
  763. {
  764. struct agp_file_private *curr_priv = file->private_data;
  765. int ret_val = -ENOTTY;
  766. DBG("priv=%p, cmd=%x", curr_priv, cmd);
  767. mutex_lock(&(agp_fe.agp_mutex));
  768. if ((agp_fe.current_controller == NULL) &&
  769. (cmd != AGPIOC_ACQUIRE)) {
  770. ret_val = -EINVAL;
  771. goto ioctl_out;
  772. }
  773. if ((agp_fe.backend_acquired != TRUE) &&
  774. (cmd != AGPIOC_ACQUIRE)) {
  775. ret_val = -EBUSY;
  776. goto ioctl_out;
  777. }
  778. if (cmd != AGPIOC_ACQUIRE) {
  779. if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
  780. ret_val = -EPERM;
  781. goto ioctl_out;
  782. }
  783. /* Use the original pid of the controller,
  784. * in case it's threaded */
  785. if (agp_fe.current_controller->pid != curr_priv->my_pid) {
  786. ret_val = -EBUSY;
  787. goto ioctl_out;
  788. }
  789. }
  790. switch (cmd) {
  791. case AGPIOC_INFO:
  792. ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
  793. break;
  794. case AGPIOC_ACQUIRE:
  795. ret_val = agpioc_acquire_wrap(curr_priv);
  796. break;
  797. case AGPIOC_RELEASE:
  798. ret_val = agpioc_release_wrap(curr_priv);
  799. break;
  800. case AGPIOC_SETUP:
  801. ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
  802. break;
  803. case AGPIOC_RESERVE:
  804. ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
  805. break;
  806. case AGPIOC_PROTECT:
  807. ret_val = agpioc_protect_wrap(curr_priv);
  808. break;
  809. case AGPIOC_ALLOCATE:
  810. ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
  811. break;
  812. case AGPIOC_DEALLOCATE:
  813. ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
  814. break;
  815. case AGPIOC_BIND:
  816. ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
  817. break;
  818. case AGPIOC_UNBIND:
  819. ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
  820. break;
  821. }
  822. ioctl_out:
  823. DBG("ioctl returns %d\n", ret_val);
  824. mutex_unlock(&(agp_fe.agp_mutex));
  825. return ret_val;
  826. }
  827. static struct file_operations agp_fops =
  828. {
  829. .owner = THIS_MODULE,
  830. .llseek = no_llseek,
  831. .read = agp_read,
  832. .write = agp_write,
  833. .ioctl = agp_ioctl,
  834. .mmap = agp_mmap,
  835. .open = agp_open,
  836. .release = agp_release,
  837. };
  838. static struct miscdevice agp_miscdev =
  839. {
  840. .minor = AGPGART_MINOR,
  841. .name = "agpgart",
  842. .fops = &agp_fops
  843. };
  844. int agp_frontend_initialize(void)
  845. {
  846. memset(&agp_fe, 0, sizeof(struct agp_front_data));
  847. mutex_init(&(agp_fe.agp_mutex));
  848. if (misc_register(&agp_miscdev)) {
  849. printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
  850. return -EIO;
  851. }
  852. return 0;
  853. }
  854. void agp_frontend_cleanup(void)
  855. {
  856. misc_deregister(&agp_miscdev);
  857. }