vmwgfx_ioctl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "vmwgfx_drm.h"
  29. #include "vmwgfx_kms.h"
  30. int vmw_getparam_ioctl(struct drm_device *dev, void *data,
  31. struct drm_file *file_priv)
  32. {
  33. struct vmw_private *dev_priv = vmw_priv(dev);
  34. struct drm_vmw_getparam_arg *param =
  35. (struct drm_vmw_getparam_arg *)data;
  36. switch (param->param) {
  37. case DRM_VMW_PARAM_NUM_STREAMS:
  38. param->value = vmw_overlay_num_overlays(dev_priv);
  39. break;
  40. case DRM_VMW_PARAM_NUM_FREE_STREAMS:
  41. param->value = vmw_overlay_num_free_overlays(dev_priv);
  42. break;
  43. case DRM_VMW_PARAM_3D:
  44. param->value = vmw_fifo_have_3d(dev_priv) ? 1 : 0;
  45. break;
  46. case DRM_VMW_PARAM_HW_CAPS:
  47. param->value = dev_priv->capabilities;
  48. break;
  49. case DRM_VMW_PARAM_FIFO_CAPS:
  50. param->value = dev_priv->fifo.capabilities;
  51. break;
  52. case DRM_VMW_PARAM_MAX_FB_SIZE:
  53. param->value = dev_priv->vram_size;
  54. break;
  55. case DRM_VMW_PARAM_FIFO_HW_VERSION:
  56. {
  57. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  58. param->value = ioread32(fifo_mem + SVGA_FIFO_3D_HWVERSION);
  59. break;
  60. }
  61. default:
  62. DRM_ERROR("Illegal vmwgfx get param request: %d\n",
  63. param->param);
  64. return -EINVAL;
  65. }
  66. return 0;
  67. }
  68. int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
  69. struct drm_file *file_priv)
  70. {
  71. struct drm_vmw_get_3d_cap_arg *arg =
  72. (struct drm_vmw_get_3d_cap_arg *) data;
  73. struct vmw_private *dev_priv = vmw_priv(dev);
  74. uint32_t size;
  75. __le32 __iomem *fifo_mem;
  76. void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
  77. void *bounce;
  78. int ret;
  79. if (unlikely(arg->pad64 != 0)) {
  80. DRM_ERROR("Illegal GET_3D_CAP argument.\n");
  81. return -EINVAL;
  82. }
  83. size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) << 2;
  84. if (arg->max_size < size)
  85. size = arg->max_size;
  86. bounce = vmalloc(size);
  87. if (unlikely(bounce == NULL)) {
  88. DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
  89. return -ENOMEM;
  90. }
  91. fifo_mem = dev_priv->mmio_virt;
  92. memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
  93. ret = copy_to_user(buffer, bounce, size);
  94. vfree(bounce);
  95. if (unlikely(ret != 0))
  96. DRM_ERROR("Failed to report 3D caps info.\n");
  97. return ret;
  98. }
  99. int vmw_present_ioctl(struct drm_device *dev, void *data,
  100. struct drm_file *file_priv)
  101. {
  102. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  103. struct vmw_private *dev_priv = vmw_priv(dev);
  104. struct drm_vmw_present_arg *arg =
  105. (struct drm_vmw_present_arg *)data;
  106. struct vmw_surface *surface;
  107. struct vmw_master *vmaster = vmw_master(file_priv->master);
  108. struct drm_vmw_rect __user *clips_ptr;
  109. struct drm_vmw_rect *clips = NULL;
  110. struct drm_mode_object *obj;
  111. struct vmw_framebuffer *vfb;
  112. uint32_t num_clips;
  113. int ret;
  114. num_clips = arg->num_clips;
  115. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  116. if (unlikely(num_clips == 0))
  117. return 0;
  118. if (clips_ptr == NULL) {
  119. DRM_ERROR("Variable clips_ptr must be specified.\n");
  120. ret = -EINVAL;
  121. goto out_clips;
  122. }
  123. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  124. if (clips == NULL) {
  125. DRM_ERROR("Failed to allocate clip rect list.\n");
  126. ret = -ENOMEM;
  127. goto out_clips;
  128. }
  129. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  130. if (ret) {
  131. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  132. ret = -EFAULT;
  133. goto out_no_copy;
  134. }
  135. ret = mutex_lock_interruptible(&dev->mode_config.mutex);
  136. if (unlikely(ret != 0)) {
  137. ret = -ERESTARTSYS;
  138. goto out_no_mode_mutex;
  139. }
  140. obj = drm_mode_object_find(dev, arg->fb_id, DRM_MODE_OBJECT_FB);
  141. if (!obj) {
  142. DRM_ERROR("Invalid framebuffer id.\n");
  143. ret = -EINVAL;
  144. goto out_no_fb;
  145. }
  146. vfb = vmw_framebuffer_to_vfb(obj_to_fb(obj));
  147. if (!vfb->dmabuf) {
  148. DRM_ERROR("Framebuffer not dmabuf backed.\n");
  149. ret = -EINVAL;
  150. goto out_no_fb;
  151. }
  152. ret = ttm_read_lock(&vmaster->lock, true);
  153. if (unlikely(ret != 0))
  154. goto out_no_ttm_lock;
  155. ret = vmw_user_surface_lookup_handle(dev_priv, tfile, arg->sid,
  156. &surface);
  157. if (ret)
  158. goto out_no_surface;
  159. ret = vmw_kms_present(dev_priv, file_priv,
  160. vfb, surface, arg->sid,
  161. arg->dest_x, arg->dest_y,
  162. clips, num_clips);
  163. /* vmw_user_surface_lookup takes one ref so does new_fb */
  164. vmw_surface_unreference(&surface);
  165. out_no_surface:
  166. ttm_read_unlock(&vmaster->lock);
  167. out_no_ttm_lock:
  168. out_no_fb:
  169. mutex_unlock(&dev->mode_config.mutex);
  170. out_no_mode_mutex:
  171. out_no_copy:
  172. kfree(clips);
  173. out_clips:
  174. return ret;
  175. }
  176. int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
  177. struct drm_file *file_priv)
  178. {
  179. struct vmw_private *dev_priv = vmw_priv(dev);
  180. struct drm_vmw_present_readback_arg *arg =
  181. (struct drm_vmw_present_readback_arg *)data;
  182. struct drm_vmw_fence_rep __user *user_fence_rep =
  183. (struct drm_vmw_fence_rep __user *)
  184. (unsigned long)arg->fence_rep;
  185. struct vmw_master *vmaster = vmw_master(file_priv->master);
  186. struct drm_vmw_rect __user *clips_ptr;
  187. struct drm_vmw_rect *clips = NULL;
  188. struct drm_mode_object *obj;
  189. struct vmw_framebuffer *vfb;
  190. uint32_t num_clips;
  191. int ret;
  192. num_clips = arg->num_clips;
  193. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  194. if (unlikely(num_clips == 0))
  195. return 0;
  196. if (clips_ptr == NULL) {
  197. DRM_ERROR("Argument clips_ptr must be specified.\n");
  198. ret = -EINVAL;
  199. goto out_clips;
  200. }
  201. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  202. if (clips == NULL) {
  203. DRM_ERROR("Failed to allocate clip rect list.\n");
  204. ret = -ENOMEM;
  205. goto out_clips;
  206. }
  207. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  208. if (ret) {
  209. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  210. ret = -EFAULT;
  211. goto out_no_copy;
  212. }
  213. ret = mutex_lock_interruptible(&dev->mode_config.mutex);
  214. if (unlikely(ret != 0)) {
  215. ret = -ERESTARTSYS;
  216. goto out_no_mode_mutex;
  217. }
  218. obj = drm_mode_object_find(dev, arg->fb_id, DRM_MODE_OBJECT_FB);
  219. if (!obj) {
  220. DRM_ERROR("Invalid framebuffer id.\n");
  221. ret = -EINVAL;
  222. goto out_no_fb;
  223. }
  224. vfb = vmw_framebuffer_to_vfb(obj_to_fb(obj));
  225. if (!vfb->dmabuf) {
  226. DRM_ERROR("Framebuffer not dmabuf backed.\n");
  227. ret = -EINVAL;
  228. goto out_no_fb;
  229. }
  230. ret = ttm_read_lock(&vmaster->lock, true);
  231. if (unlikely(ret != 0))
  232. goto out_no_ttm_lock;
  233. ret = vmw_kms_readback(dev_priv, file_priv,
  234. vfb, user_fence_rep,
  235. clips, num_clips);
  236. ttm_read_unlock(&vmaster->lock);
  237. out_no_ttm_lock:
  238. out_no_fb:
  239. mutex_unlock(&dev->mode_config.mutex);
  240. out_no_mode_mutex:
  241. out_no_copy:
  242. kfree(clips);
  243. out_clips:
  244. return ret;
  245. }
  246. /**
  247. * vmw_fops_poll - wrapper around the drm_poll function
  248. *
  249. * @filp: See the linux fops poll documentation.
  250. * @wait: See the linux fops poll documentation.
  251. *
  252. * Wrapper around the drm_poll function that makes sure the device is
  253. * processing the fifo if drm_poll decides to wait.
  254. */
  255. unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
  256. {
  257. struct drm_file *file_priv = filp->private_data;
  258. struct vmw_private *dev_priv =
  259. vmw_priv(file_priv->minor->dev);
  260. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  261. return drm_poll(filp, wait);
  262. }
  263. /**
  264. * vmw_fops_read - wrapper around the drm_read function
  265. *
  266. * @filp: See the linux fops read documentation.
  267. * @buffer: See the linux fops read documentation.
  268. * @count: See the linux fops read documentation.
  269. * offset: See the linux fops read documentation.
  270. *
  271. * Wrapper around the drm_read function that makes sure the device is
  272. * processing the fifo if drm_read decides to wait.
  273. */
  274. ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
  275. size_t count, loff_t *offset)
  276. {
  277. struct drm_file *file_priv = filp->private_data;
  278. struct vmw_private *dev_priv =
  279. vmw_priv(file_priv->minor->dev);
  280. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  281. return drm_read(filp, buffer, count, offset);
  282. }