frontend.c 25 KB

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