vmwgfx_ioctl.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_mode_object *obj;
  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. ret = mutex_lock_interruptible(&dev->mode_config.mutex);
  145. if (unlikely(ret != 0)) {
  146. ret = -ERESTARTSYS;
  147. goto out_no_mode_mutex;
  148. }
  149. obj = drm_mode_object_find(dev, arg->fb_id, DRM_MODE_OBJECT_FB);
  150. if (!obj) {
  151. DRM_ERROR("Invalid framebuffer id.\n");
  152. ret = -EINVAL;
  153. goto out_no_fb;
  154. }
  155. vfb = vmw_framebuffer_to_vfb(obj_to_fb(obj));
  156. ret = ttm_read_lock(&vmaster->lock, true);
  157. if (unlikely(ret != 0))
  158. goto out_no_ttm_lock;
  159. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
  160. user_surface_converter,
  161. &res);
  162. if (ret)
  163. goto out_no_surface;
  164. surface = vmw_res_to_srf(res);
  165. ret = vmw_kms_present(dev_priv, file_priv,
  166. vfb, surface, arg->sid,
  167. arg->dest_x, arg->dest_y,
  168. clips, num_clips);
  169. /* vmw_user_surface_lookup takes one ref so does new_fb */
  170. vmw_surface_unreference(&surface);
  171. out_no_surface:
  172. ttm_read_unlock(&vmaster->lock);
  173. out_no_ttm_lock:
  174. out_no_fb:
  175. mutex_unlock(&dev->mode_config.mutex);
  176. out_no_mode_mutex:
  177. out_no_copy:
  178. kfree(clips);
  179. out_clips:
  180. return ret;
  181. }
  182. int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
  183. struct drm_file *file_priv)
  184. {
  185. struct vmw_private *dev_priv = vmw_priv(dev);
  186. struct drm_vmw_present_readback_arg *arg =
  187. (struct drm_vmw_present_readback_arg *)data;
  188. struct drm_vmw_fence_rep __user *user_fence_rep =
  189. (struct drm_vmw_fence_rep __user *)
  190. (unsigned long)arg->fence_rep;
  191. struct vmw_master *vmaster = vmw_master(file_priv->master);
  192. struct drm_vmw_rect __user *clips_ptr;
  193. struct drm_vmw_rect *clips = NULL;
  194. struct drm_mode_object *obj;
  195. struct vmw_framebuffer *vfb;
  196. uint32_t num_clips;
  197. int ret;
  198. num_clips = arg->num_clips;
  199. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  200. if (unlikely(num_clips == 0))
  201. return 0;
  202. if (clips_ptr == NULL) {
  203. DRM_ERROR("Argument clips_ptr must be specified.\n");
  204. ret = -EINVAL;
  205. goto out_clips;
  206. }
  207. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  208. if (clips == NULL) {
  209. DRM_ERROR("Failed to allocate clip rect list.\n");
  210. ret = -ENOMEM;
  211. goto out_clips;
  212. }
  213. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  214. if (ret) {
  215. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  216. ret = -EFAULT;
  217. goto out_no_copy;
  218. }
  219. ret = mutex_lock_interruptible(&dev->mode_config.mutex);
  220. if (unlikely(ret != 0)) {
  221. ret = -ERESTARTSYS;
  222. goto out_no_mode_mutex;
  223. }
  224. obj = drm_mode_object_find(dev, arg->fb_id, DRM_MODE_OBJECT_FB);
  225. if (!obj) {
  226. DRM_ERROR("Invalid framebuffer id.\n");
  227. ret = -EINVAL;
  228. goto out_no_fb;
  229. }
  230. vfb = vmw_framebuffer_to_vfb(obj_to_fb(obj));
  231. if (!vfb->dmabuf) {
  232. DRM_ERROR("Framebuffer not dmabuf backed.\n");
  233. ret = -EINVAL;
  234. goto out_no_fb;
  235. }
  236. ret = ttm_read_lock(&vmaster->lock, true);
  237. if (unlikely(ret != 0))
  238. goto out_no_ttm_lock;
  239. ret = vmw_kms_readback(dev_priv, file_priv,
  240. vfb, user_fence_rep,
  241. clips, num_clips);
  242. ttm_read_unlock(&vmaster->lock);
  243. out_no_ttm_lock:
  244. out_no_fb:
  245. mutex_unlock(&dev->mode_config.mutex);
  246. out_no_mode_mutex:
  247. out_no_copy:
  248. kfree(clips);
  249. out_clips:
  250. return ret;
  251. }
  252. /**
  253. * vmw_fops_poll - wrapper around the drm_poll function
  254. *
  255. * @filp: See the linux fops poll documentation.
  256. * @wait: See the linux fops poll documentation.
  257. *
  258. * Wrapper around the drm_poll function that makes sure the device is
  259. * processing the fifo if drm_poll decides to wait.
  260. */
  261. unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
  262. {
  263. struct drm_file *file_priv = filp->private_data;
  264. struct vmw_private *dev_priv =
  265. vmw_priv(file_priv->minor->dev);
  266. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  267. return drm_poll(filp, wait);
  268. }
  269. /**
  270. * vmw_fops_read - wrapper around the drm_read function
  271. *
  272. * @filp: See the linux fops read documentation.
  273. * @buffer: See the linux fops read documentation.
  274. * @count: See the linux fops read documentation.
  275. * offset: See the linux fops read documentation.
  276. *
  277. * Wrapper around the drm_read function that makes sure the device is
  278. * processing the fifo if drm_read decides to wait.
  279. */
  280. ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
  281. size_t count, loff_t *offset)
  282. {
  283. struct drm_file *file_priv = filp->private_data;
  284. struct vmw_private *dev_priv =
  285. vmw_priv(file_priv->minor->dev);
  286. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  287. return drm_read(filp, buffer, count, offset);
  288. }