frontend.c 24 KB

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