compat_ioctl.c 7.3 KB

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