compat_ioctl.c 7.4 KB

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