vmwgfx_ioctl.c 8.9 KB

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