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