frontend.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. int rc = -ENXIO;
  531. mutex_lock(&(agp_fe.agp_mutex));
  532. if (minor != AGPGART_MINOR)
  533. goto err_out;
  534. priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
  535. if (priv == NULL)
  536. goto err_out_nomem;
  537. set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
  538. priv->my_pid = current->pid;
  539. if (capable(CAP_SYS_RAWIO)) {
  540. /* Root priv, can be controller */
  541. set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
  542. }
  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. err_out_nomem:
  554. rc = -ENOMEM;
  555. err_out:
  556. mutex_unlock(&(agp_fe.agp_mutex));
  557. return rc;
  558. }
  559. static ssize_t agp_read(struct file *file, char __user *buf,
  560. size_t count, loff_t * ppos)
  561. {
  562. return -EINVAL;
  563. }
  564. static ssize_t agp_write(struct file *file, const char __user *buf,
  565. size_t count, loff_t * ppos)
  566. {
  567. return -EINVAL;
  568. }
  569. static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
  570. {
  571. struct agp_info userinfo;
  572. struct agp_kern_info kerninfo;
  573. agp_copy_info(agp_bridge, &kerninfo);
  574. userinfo.version.major = kerninfo.version.major;
  575. userinfo.version.minor = kerninfo.version.minor;
  576. userinfo.bridge_id = kerninfo.device->vendor |
  577. (kerninfo.device->device << 16);
  578. userinfo.agp_mode = kerninfo.mode;
  579. userinfo.aper_base = kerninfo.aper_base;
  580. userinfo.aper_size = kerninfo.aper_size;
  581. userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
  582. userinfo.pg_used = kerninfo.current_memory;
  583. if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
  584. return -EFAULT;
  585. return 0;
  586. }
  587. int agpioc_acquire_wrap(struct agp_file_private *priv)
  588. {
  589. struct agp_controller *controller;
  590. DBG("");
  591. if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
  592. return -EPERM;
  593. if (agp_fe.current_controller != NULL)
  594. return -EBUSY;
  595. if (!agp_bridge)
  596. return -ENODEV;
  597. if (atomic_read(&agp_bridge->agp_in_use))
  598. return -EBUSY;
  599. atomic_inc(&agp_bridge->agp_in_use);
  600. agp_fe.backend_acquired = TRUE;
  601. controller = agp_find_controller_by_pid(priv->my_pid);
  602. if (controller != NULL) {
  603. agp_controller_make_current(controller);
  604. } else {
  605. controller = agp_create_controller(priv->my_pid);
  606. if (controller == NULL) {
  607. agp_fe.backend_acquired = FALSE;
  608. agp_backend_release(agp_bridge);
  609. return -ENOMEM;
  610. }
  611. agp_insert_controller(controller);
  612. agp_controller_make_current(controller);
  613. }
  614. set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
  615. set_bit(AGP_FF_IS_VALID, &priv->access_flags);
  616. return 0;
  617. }
  618. int agpioc_release_wrap(struct agp_file_private *priv)
  619. {
  620. DBG("");
  621. agp_controller_release_current(agp_fe.current_controller, priv);
  622. return 0;
  623. }
  624. int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
  625. {
  626. struct agp_setup mode;
  627. DBG("");
  628. if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
  629. return -EFAULT;
  630. agp_enable(agp_bridge, mode.agp_mode);
  631. return 0;
  632. }
  633. static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
  634. {
  635. struct agp_region reserve;
  636. struct agp_client *client;
  637. struct agp_file_private *client_priv;
  638. DBG("");
  639. if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
  640. return -EFAULT;
  641. if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
  642. return -EFAULT;
  643. client = agp_find_client_by_pid(reserve.pid);
  644. if (reserve.seg_count == 0) {
  645. /* remove a client */
  646. client_priv = agp_find_private(reserve.pid);
  647. if (client_priv != NULL) {
  648. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  649. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  650. }
  651. if (client == NULL) {
  652. /* client is already removed */
  653. return 0;
  654. }
  655. return agp_remove_client(reserve.pid);
  656. } else {
  657. struct agp_segment *segment;
  658. if (reserve.seg_count >= 16384)
  659. return -EINVAL;
  660. segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
  661. GFP_KERNEL);
  662. if (segment == NULL)
  663. return -ENOMEM;
  664. if (copy_from_user(segment, (void __user *) reserve.seg_list,
  665. sizeof(struct agp_segment) * reserve.seg_count)) {
  666. kfree(segment);
  667. return -EFAULT;
  668. }
  669. reserve.seg_list = segment;
  670. if (client == NULL) {
  671. /* Create the client and add the segment */
  672. client = agp_create_client(reserve.pid);
  673. if (client == NULL) {
  674. kfree(segment);
  675. return -ENOMEM;
  676. }
  677. client_priv = agp_find_private(reserve.pid);
  678. if (client_priv != NULL) {
  679. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  680. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  681. }
  682. }
  683. return agp_create_segment(client, &reserve);
  684. }
  685. /* Will never really happen */
  686. return -EINVAL;
  687. }
  688. int agpioc_protect_wrap(struct agp_file_private *priv)
  689. {
  690. DBG("");
  691. /* This function is not currently implemented */
  692. return -EINVAL;
  693. }
  694. static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
  695. {
  696. struct agp_memory *memory;
  697. struct agp_allocate alloc;
  698. DBG("");
  699. if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
  700. return -EFAULT;
  701. if (alloc.type >= AGP_USER_TYPES)
  702. return -EINVAL;
  703. memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
  704. if (memory == NULL)
  705. return -ENOMEM;
  706. alloc.key = memory->key;
  707. alloc.physical = memory->physical;
  708. if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
  709. agp_free_memory_wrap(memory);
  710. return -EFAULT;
  711. }
  712. return 0;
  713. }
  714. int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
  715. {
  716. struct agp_memory *memory;
  717. DBG("");
  718. memory = agp_find_mem_by_key(arg);
  719. if (memory == NULL)
  720. return -EINVAL;
  721. agp_free_memory_wrap(memory);
  722. return 0;
  723. }
  724. static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
  725. {
  726. struct agp_bind bind_info;
  727. struct agp_memory *memory;
  728. DBG("");
  729. if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
  730. return -EFAULT;
  731. memory = agp_find_mem_by_key(bind_info.key);
  732. if (memory == NULL)
  733. return -EINVAL;
  734. return agp_bind_memory(memory, bind_info.pg_start);
  735. }
  736. static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
  737. {
  738. struct agp_memory *memory;
  739. struct agp_unbind unbind;
  740. DBG("");
  741. if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
  742. return -EFAULT;
  743. memory = agp_find_mem_by_key(unbind.key);
  744. if (memory == NULL)
  745. return -EINVAL;
  746. return agp_unbind_memory(memory);
  747. }
  748. int agpioc_chipset_flush_wrap(struct agp_file_private *priv)
  749. {
  750. DBG("");
  751. agp_flush_chipset(agp_bridge);
  752. return 0;
  753. }
  754. static int agp_ioctl(struct inode *inode, struct file *file,
  755. unsigned int cmd, unsigned long arg)
  756. {
  757. struct agp_file_private *curr_priv = file->private_data;
  758. int ret_val = -ENOTTY;
  759. DBG("priv=%p, cmd=%x", curr_priv, cmd);
  760. mutex_lock(&(agp_fe.agp_mutex));
  761. if ((agp_fe.current_controller == NULL) &&
  762. (cmd != AGPIOC_ACQUIRE)) {
  763. ret_val = -EINVAL;
  764. goto ioctl_out;
  765. }
  766. if ((agp_fe.backend_acquired != TRUE) &&
  767. (cmd != AGPIOC_ACQUIRE)) {
  768. ret_val = -EBUSY;
  769. goto ioctl_out;
  770. }
  771. if (cmd != AGPIOC_ACQUIRE) {
  772. if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
  773. ret_val = -EPERM;
  774. goto ioctl_out;
  775. }
  776. /* Use the original pid of the controller,
  777. * in case it's threaded */
  778. if (agp_fe.current_controller->pid != curr_priv->my_pid) {
  779. ret_val = -EBUSY;
  780. goto ioctl_out;
  781. }
  782. }
  783. switch (cmd) {
  784. case AGPIOC_INFO:
  785. ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
  786. break;
  787. case AGPIOC_ACQUIRE:
  788. ret_val = agpioc_acquire_wrap(curr_priv);
  789. break;
  790. case AGPIOC_RELEASE:
  791. ret_val = agpioc_release_wrap(curr_priv);
  792. break;
  793. case AGPIOC_SETUP:
  794. ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
  795. break;
  796. case AGPIOC_RESERVE:
  797. ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
  798. break;
  799. case AGPIOC_PROTECT:
  800. ret_val = agpioc_protect_wrap(curr_priv);
  801. break;
  802. case AGPIOC_ALLOCATE:
  803. ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
  804. break;
  805. case AGPIOC_DEALLOCATE:
  806. ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
  807. break;
  808. case AGPIOC_BIND:
  809. ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
  810. break;
  811. case AGPIOC_UNBIND:
  812. ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
  813. break;
  814. case AGPIOC_CHIPSET_FLUSH:
  815. ret_val = agpioc_chipset_flush_wrap(curr_priv);
  816. break;
  817. }
  818. ioctl_out:
  819. DBG("ioctl returns %d\n", ret_val);
  820. mutex_unlock(&(agp_fe.agp_mutex));
  821. return ret_val;
  822. }
  823. static const struct file_operations agp_fops =
  824. {
  825. .owner = THIS_MODULE,
  826. .llseek = no_llseek,
  827. .read = agp_read,
  828. .write = agp_write,
  829. .ioctl = agp_ioctl,
  830. #ifdef CONFIG_COMPAT
  831. .compat_ioctl = compat_agp_ioctl,
  832. #endif
  833. .mmap = agp_mmap,
  834. .open = agp_open,
  835. .release = agp_release,
  836. };
  837. static struct miscdevice agp_miscdev =
  838. {
  839. .minor = AGPGART_MINOR,
  840. .name = "agpgart",
  841. .fops = &agp_fops
  842. };
  843. int agp_frontend_initialize(void)
  844. {
  845. memset(&agp_fe, 0, sizeof(struct agp_front_data));
  846. mutex_init(&(agp_fe.agp_mutex));
  847. if (misc_register(&agp_miscdev)) {
  848. printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
  849. return -EIO;
  850. }
  851. return 0;
  852. }
  853. void agp_frontend_cleanup(void)
  854. {
  855. misc_deregister(&agp_miscdev);
  856. }