vmwgfx_ioctl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <drm/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. const struct vmw_fifo_state *fifo = &dev_priv->fifo;
  59. param->value =
  60. ioread32(fifo_mem +
  61. ((fifo->capabilities &
  62. SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
  63. SVGA_FIFO_3D_HWVERSION_REVISED :
  64. SVGA_FIFO_3D_HWVERSION));
  65. break;
  66. }
  67. default:
  68. DRM_ERROR("Illegal vmwgfx get param request: %d\n",
  69. param->param);
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
  75. struct drm_file *file_priv)
  76. {
  77. struct drm_vmw_get_3d_cap_arg *arg =
  78. (struct drm_vmw_get_3d_cap_arg *) data;
  79. struct vmw_private *dev_priv = vmw_priv(dev);
  80. uint32_t size;
  81. __le32 __iomem *fifo_mem;
  82. void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
  83. void *bounce;
  84. int ret;
  85. if (unlikely(arg->pad64 != 0)) {
  86. DRM_ERROR("Illegal GET_3D_CAP argument.\n");
  87. return -EINVAL;
  88. }
  89. size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) << 2;
  90. if (arg->max_size < size)
  91. size = arg->max_size;
  92. bounce = vmalloc(size);
  93. if (unlikely(bounce == NULL)) {
  94. DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
  95. return -ENOMEM;
  96. }
  97. fifo_mem = dev_priv->mmio_virt;
  98. memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
  99. ret = copy_to_user(buffer, bounce, size);
  100. if (ret)
  101. ret = -EFAULT;
  102. vfree(bounce);
  103. if (unlikely(ret != 0))
  104. DRM_ERROR("Failed to report 3D caps info.\n");
  105. return ret;
  106. }
  107. int vmw_present_ioctl(struct drm_device *dev, void *data,
  108. struct drm_file *file_priv)
  109. {
  110. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  111. struct vmw_private *dev_priv = vmw_priv(dev);
  112. struct drm_vmw_present_arg *arg =
  113. (struct drm_vmw_present_arg *)data;
  114. struct vmw_surface *surface;
  115. struct vmw_master *vmaster = vmw_master(file_priv->master);
  116. struct drm_vmw_rect __user *clips_ptr;
  117. struct drm_vmw_rect *clips = NULL;
  118. struct drm_framebuffer *fb;
  119. struct vmw_framebuffer *vfb;
  120. struct vmw_resource *res;
  121. uint32_t num_clips;
  122. int ret;
  123. num_clips = arg->num_clips;
  124. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  125. if (unlikely(num_clips == 0))
  126. return 0;
  127. if (clips_ptr == NULL) {
  128. DRM_ERROR("Variable clips_ptr must be specified.\n");
  129. ret = -EINVAL;
  130. goto out_clips;
  131. }
  132. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  133. if (clips == NULL) {
  134. DRM_ERROR("Failed to allocate clip rect list.\n");
  135. ret = -ENOMEM;
  136. goto out_clips;
  137. }
  138. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  139. if (ret) {
  140. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  141. ret = -EFAULT;
  142. goto out_no_copy;
  143. }
  144. drm_modeset_lock_all(dev);
  145. fb = drm_framebuffer_lookup(dev, arg->fb_id);
  146. if (!fb) {
  147. DRM_ERROR("Invalid framebuffer id.\n");
  148. ret = -EINVAL;
  149. goto out_no_fb;
  150. }
  151. vfb = vmw_framebuffer_to_vfb(fb);
  152. ret = ttm_read_lock(&vmaster->lock, true);
  153. if (unlikely(ret != 0))
  154. goto out_no_ttm_lock;
  155. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
  156. user_surface_converter,
  157. &res);
  158. if (ret)
  159. goto out_no_surface;
  160. surface = vmw_res_to_srf(res);
  161. ret = vmw_kms_present(dev_priv, file_priv,
  162. vfb, surface, arg->sid,
  163. arg->dest_x, arg->dest_y,
  164. clips, num_clips);
  165. /* vmw_user_surface_lookup takes one ref so does new_fb */
  166. vmw_surface_unreference(&surface);
  167. out_no_surface:
  168. ttm_read_unlock(&vmaster->lock);
  169. out_no_ttm_lock:
  170. drm_framebuffer_unreference(fb);
  171. out_no_fb:
  172. drm_modeset_unlock_all(dev);
  173. out_no_copy:
  174. kfree(clips);
  175. out_clips:
  176. return ret;
  177. }
  178. int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
  179. struct drm_file *file_priv)
  180. {
  181. struct vmw_private *dev_priv = vmw_priv(dev);
  182. struct drm_vmw_present_readback_arg *arg =
  183. (struct drm_vmw_present_readback_arg *)data;
  184. struct drm_vmw_fence_rep __user *user_fence_rep =
  185. (struct drm_vmw_fence_rep __user *)
  186. (unsigned long)arg->fence_rep;
  187. struct vmw_master *vmaster = vmw_master(file_priv->master);
  188. struct drm_vmw_rect __user *clips_ptr;
  189. struct drm_vmw_rect *clips = NULL;
  190. struct drm_framebuffer *fb;
  191. struct vmw_framebuffer *vfb;
  192. uint32_t num_clips;
  193. int ret;
  194. num_clips = arg->num_clips;
  195. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  196. if (unlikely(num_clips == 0))
  197. return 0;
  198. if (clips_ptr == NULL) {
  199. DRM_ERROR("Argument clips_ptr must be specified.\n");
  200. ret = -EINVAL;
  201. goto out_clips;
  202. }
  203. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  204. if (clips == NULL) {
  205. DRM_ERROR("Failed to allocate clip rect list.\n");
  206. ret = -ENOMEM;
  207. goto out_clips;
  208. }
  209. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  210. if (ret) {
  211. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  212. ret = -EFAULT;
  213. goto out_no_copy;
  214. }
  215. drm_modeset_lock_all(dev);
  216. fb = drm_framebuffer_lookup(dev, arg->fb_id);
  217. if (!fb) {
  218. DRM_ERROR("Invalid framebuffer id.\n");
  219. ret = -EINVAL;
  220. goto out_no_fb;
  221. }
  222. vfb = vmw_framebuffer_to_vfb(fb);
  223. if (!vfb->dmabuf) {
  224. DRM_ERROR("Framebuffer not dmabuf backed.\n");
  225. ret = -EINVAL;
  226. goto out_no_ttm_lock;
  227. }
  228. ret = ttm_read_lock(&vmaster->lock, true);
  229. if (unlikely(ret != 0))
  230. goto out_no_ttm_lock;
  231. ret = vmw_kms_readback(dev_priv, file_priv,
  232. vfb, user_fence_rep,
  233. clips, num_clips);
  234. ttm_read_unlock(&vmaster->lock);
  235. out_no_ttm_lock:
  236. drm_framebuffer_unreference(fb);
  237. out_no_fb:
  238. drm_modeset_unlock_all(dev);
  239. out_no_copy:
  240. kfree(clips);
  241. out_clips:
  242. return ret;
  243. }
  244. /**
  245. * vmw_fops_poll - wrapper around the drm_poll function
  246. *
  247. * @filp: See the linux fops poll documentation.
  248. * @wait: See the linux fops poll documentation.
  249. *
  250. * Wrapper around the drm_poll function that makes sure the device is
  251. * processing the fifo if drm_poll decides to wait.
  252. */
  253. unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
  254. {
  255. struct drm_file *file_priv = filp->private_data;
  256. struct vmw_private *dev_priv =
  257. vmw_priv(file_priv->minor->dev);
  258. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  259. return drm_poll(filp, wait);
  260. }
  261. /**
  262. * vmw_fops_read - wrapper around the drm_read function
  263. *
  264. * @filp: See the linux fops read documentation.
  265. * @buffer: See the linux fops read documentation.
  266. * @count: See the linux fops read documentation.
  267. * offset: See the linux fops read documentation.
  268. *
  269. * Wrapper around the drm_read function that makes sure the device is
  270. * processing the fifo if drm_read decides to wait.
  271. */
  272. ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
  273. size_t count, loff_t *offset)
  274. {
  275. struct drm_file *file_priv = filp->private_data;
  276. struct vmw_private *dev_priv =
  277. vmw_priv(file_priv->minor->dev);
  278. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  279. return drm_read(filp, buffer, count, offset);
  280. }