compat_ioctl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * AGPGART driver frontend compatibility ioctls
  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/kernel.h>
  29. #include <linux/pci.h>
  30. #include <linux/fs.h>
  31. #include <linux/agpgart.h>
  32. #include <linux/slab.h>
  33. #include <asm/uaccess.h>
  34. #include "agp.h"
  35. #include "compat_ioctl.h"
  36. static int compat_agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
  37. {
  38. struct agp_info32 userinfo;
  39. struct agp_kern_info kerninfo;
  40. agp_copy_info(agp_bridge, &kerninfo);
  41. userinfo.version.major = kerninfo.version.major;
  42. userinfo.version.minor = kerninfo.version.minor;
  43. userinfo.bridge_id = kerninfo.device->vendor |
  44. (kerninfo.device->device << 16);
  45. userinfo.agp_mode = kerninfo.mode;
  46. userinfo.aper_base = (compat_long_t)kerninfo.aper_base;
  47. userinfo.aper_size = kerninfo.aper_size;
  48. userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
  49. userinfo.pg_used = kerninfo.current_memory;
  50. if (copy_to_user(arg, &userinfo, sizeof(userinfo)))
  51. return -EFAULT;
  52. return 0;
  53. }
  54. static int compat_agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
  55. {
  56. struct agp_region32 ureserve;
  57. struct agp_region kreserve;
  58. struct agp_client *client;
  59. struct agp_file_private *client_priv;
  60. DBG("");
  61. if (copy_from_user(&ureserve, arg, sizeof(ureserve)))
  62. return -EFAULT;
  63. if ((unsigned) ureserve.seg_count >= ~0U/sizeof(struct agp_segment32))
  64. return -EFAULT;
  65. kreserve.pid = ureserve.pid;
  66. kreserve.seg_count = ureserve.seg_count;
  67. client = agp_find_client_by_pid(kreserve.pid);
  68. if (kreserve.seg_count == 0) {
  69. /* remove a client */
  70. client_priv = agp_find_private(kreserve.pid);
  71. if (client_priv != NULL) {
  72. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  73. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  74. }
  75. if (client == NULL) {
  76. /* client is already removed */
  77. return 0;
  78. }
  79. return agp_remove_client(kreserve.pid);
  80. } else {
  81. struct agp_segment32 *usegment;
  82. struct agp_segment *ksegment;
  83. int seg;
  84. if (ureserve.seg_count >= 16384)
  85. return -EINVAL;
  86. usegment = kmalloc(sizeof(*usegment) * ureserve.seg_count, GFP_KERNEL);
  87. if (!usegment)
  88. return -ENOMEM;
  89. ksegment = kmalloc(sizeof(*ksegment) * kreserve.seg_count, GFP_KERNEL);
  90. if (!ksegment) {
  91. kfree(usegment);
  92. return -ENOMEM;
  93. }
  94. if (copy_from_user(usegment, (void __user *) ureserve.seg_list,
  95. sizeof(*usegment) * ureserve.seg_count)) {
  96. kfree(usegment);
  97. kfree(ksegment);
  98. return -EFAULT;
  99. }
  100. for (seg = 0; seg < ureserve.seg_count; seg++) {
  101. ksegment[seg].pg_start = usegment[seg].pg_start;
  102. ksegment[seg].pg_count = usegment[seg].pg_count;
  103. ksegment[seg].prot = usegment[seg].prot;
  104. }
  105. kfree(usegment);
  106. kreserve.seg_list = ksegment;
  107. if (client == NULL) {
  108. /* Create the client and add the segment */
  109. client = agp_create_client(kreserve.pid);
  110. if (client == NULL) {
  111. kfree(ksegment);
  112. return -ENOMEM;
  113. }
  114. client_priv = agp_find_private(kreserve.pid);
  115. if (client_priv != NULL) {
  116. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  117. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  118. }
  119. }
  120. return agp_create_segment(client, &kreserve);
  121. }
  122. /* Will never really happen */
  123. return -EINVAL;
  124. }
  125. static int compat_agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
  126. {
  127. struct agp_memory *memory;
  128. struct agp_allocate32 alloc;
  129. DBG("");
  130. if (copy_from_user(&alloc, arg, sizeof(alloc)))
  131. return -EFAULT;
  132. memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
  133. if (memory == NULL)
  134. return -ENOMEM;
  135. alloc.key = memory->key;
  136. alloc.physical = memory->physical;
  137. if (copy_to_user(arg, &alloc, sizeof(alloc))) {
  138. agp_free_memory_wrap(memory);
  139. return -EFAULT;
  140. }
  141. return 0;
  142. }
  143. static int compat_agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
  144. {
  145. struct agp_bind32 bind_info;
  146. struct agp_memory *memory;
  147. DBG("");
  148. if (copy_from_user(&bind_info, arg, sizeof(bind_info)))
  149. return -EFAULT;
  150. memory = agp_find_mem_by_key(bind_info.key);
  151. if (memory == NULL)
  152. return -EINVAL;
  153. return agp_bind_memory(memory, bind_info.pg_start);
  154. }
  155. static int compat_agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
  156. {
  157. struct agp_memory *memory;
  158. struct agp_unbind32 unbind;
  159. DBG("");
  160. if (copy_from_user(&unbind, arg, sizeof(unbind)))
  161. return -EFAULT;
  162. memory = agp_find_mem_by_key(unbind.key);
  163. if (memory == NULL)
  164. return -EINVAL;
  165. return agp_unbind_memory(memory);
  166. }
  167. long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  168. {
  169. struct agp_file_private *curr_priv = file->private_data;
  170. int ret_val = -ENOTTY;
  171. mutex_lock(&(agp_fe.agp_mutex));
  172. if ((agp_fe.current_controller == NULL) &&
  173. (cmd != AGPIOC_ACQUIRE32)) {
  174. ret_val = -EINVAL;
  175. goto ioctl_out;
  176. }
  177. if ((agp_fe.backend_acquired != true) &&
  178. (cmd != AGPIOC_ACQUIRE32)) {
  179. ret_val = -EBUSY;
  180. goto ioctl_out;
  181. }
  182. if (cmd != AGPIOC_ACQUIRE32) {
  183. if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
  184. ret_val = -EPERM;
  185. goto ioctl_out;
  186. }
  187. /* Use the original pid of the controller,
  188. * in case it's threaded */
  189. if (agp_fe.current_controller->pid != curr_priv->my_pid) {
  190. ret_val = -EBUSY;
  191. goto ioctl_out;
  192. }
  193. }
  194. switch (cmd) {
  195. case AGPIOC_INFO32:
  196. ret_val = compat_agpioc_info_wrap(curr_priv, (void __user *) arg);
  197. break;
  198. case AGPIOC_ACQUIRE32:
  199. ret_val = agpioc_acquire_wrap(curr_priv);
  200. break;
  201. case AGPIOC_RELEASE32:
  202. ret_val = agpioc_release_wrap(curr_priv);
  203. break;
  204. case AGPIOC_SETUP32:
  205. ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
  206. break;
  207. case AGPIOC_RESERVE32:
  208. ret_val = compat_agpioc_reserve_wrap(curr_priv, (void __user *) arg);
  209. break;
  210. case AGPIOC_PROTECT32:
  211. ret_val = agpioc_protect_wrap(curr_priv);
  212. break;
  213. case AGPIOC_ALLOCATE32:
  214. ret_val = compat_agpioc_allocate_wrap(curr_priv, (void __user *) arg);
  215. break;
  216. case AGPIOC_DEALLOCATE32:
  217. ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
  218. break;
  219. case AGPIOC_BIND32:
  220. ret_val = compat_agpioc_bind_wrap(curr_priv, (void __user *) arg);
  221. break;
  222. case AGPIOC_UNBIND32:
  223. ret_val = compat_agpioc_unbind_wrap(curr_priv, (void __user *) arg);
  224. break;
  225. case AGPIOC_CHIPSET_FLUSH32:
  226. break;
  227. }
  228. ioctl_out:
  229. DBG("ioctl returns %d\n", ret_val);
  230. mutex_unlock(&(agp_fe.agp_mutex));
  231. return ret_val;
  232. }