vmwgfx_ioctl.c 8.9 KB

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