frontend.c 24 KB

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