vmwgfx_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. struct svga_3d_compat_cap {
  31. SVGA3dCapsRecordHeader header;
  32. SVGA3dCapPair pairs[SVGA3D_DEVCAP_MAX];
  33. };
  34. int vmw_getparam_ioctl(struct drm_device *dev, void *data,
  35. struct drm_file *file_priv)
  36. {
  37. struct vmw_private *dev_priv = vmw_priv(dev);
  38. struct drm_vmw_getparam_arg *param =
  39. (struct drm_vmw_getparam_arg *)data;
  40. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  41. switch (param->param) {
  42. case DRM_VMW_PARAM_NUM_STREAMS:
  43. param->value = vmw_overlay_num_overlays(dev_priv);
  44. break;
  45. case DRM_VMW_PARAM_NUM_FREE_STREAMS:
  46. param->value = vmw_overlay_num_free_overlays(dev_priv);
  47. break;
  48. case DRM_VMW_PARAM_3D:
  49. param->value = vmw_fifo_have_3d(dev_priv) ? 1 : 0;
  50. break;
  51. case DRM_VMW_PARAM_HW_CAPS:
  52. param->value = dev_priv->capabilities;
  53. break;
  54. case DRM_VMW_PARAM_FIFO_CAPS:
  55. param->value = dev_priv->fifo.capabilities;
  56. break;
  57. case DRM_VMW_PARAM_MAX_FB_SIZE:
  58. param->value = dev_priv->prim_bb_mem;
  59. break;
  60. case DRM_VMW_PARAM_FIFO_HW_VERSION:
  61. {
  62. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  63. const struct vmw_fifo_state *fifo = &dev_priv->fifo;
  64. if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS)) {
  65. param->value = SVGA3D_HWVERSION_WS8_B1;
  66. break;
  67. }
  68. param->value =
  69. ioread32(fifo_mem +
  70. ((fifo->capabilities &
  71. SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
  72. SVGA_FIFO_3D_HWVERSION_REVISED :
  73. SVGA_FIFO_3D_HWVERSION));
  74. break;
  75. }
  76. case DRM_VMW_PARAM_MAX_SURF_MEMORY:
  77. if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS) &&
  78. !vmw_fp->gb_aware)
  79. param->value = dev_priv->max_mob_pages * PAGE_SIZE / 2;
  80. else
  81. param->value = dev_priv->memory_size;
  82. break;
  83. case DRM_VMW_PARAM_3D_CAPS_SIZE:
  84. if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS) &&
  85. vmw_fp->gb_aware)
  86. param->value = SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
  87. else if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS)
  88. param->value = sizeof(struct svga_3d_compat_cap) +
  89. sizeof(uint32_t);
  90. else
  91. param->value = (SVGA_FIFO_3D_CAPS_LAST -
  92. SVGA_FIFO_3D_CAPS + 1) *
  93. sizeof(uint32_t);
  94. break;
  95. case DRM_VMW_PARAM_MAX_MOB_MEMORY:
  96. vmw_fp->gb_aware = true;
  97. param->value = dev_priv->max_mob_pages * PAGE_SIZE;
  98. break;
  99. case DRM_VMW_PARAM_MAX_MOB_SIZE:
  100. param->value = dev_priv->max_mob_size;
  101. break;
  102. default:
  103. DRM_ERROR("Illegal vmwgfx get param request: %d\n",
  104. param->param);
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. static int vmw_fill_compat_cap(struct vmw_private *dev_priv, void *bounce,
  110. size_t size)
  111. {
  112. struct svga_3d_compat_cap *compat_cap =
  113. (struct svga_3d_compat_cap *) bounce;
  114. unsigned int i;
  115. size_t pair_offset = offsetof(struct svga_3d_compat_cap, pairs);
  116. unsigned int max_size;
  117. if (size < pair_offset)
  118. return -EINVAL;
  119. max_size = (size - pair_offset) / sizeof(SVGA3dCapPair);
  120. if (max_size > SVGA3D_DEVCAP_MAX)
  121. max_size = SVGA3D_DEVCAP_MAX;
  122. compat_cap->header.length =
  123. (pair_offset + max_size * sizeof(SVGA3dCapPair)) / sizeof(u32);
  124. compat_cap->header.type = SVGA3DCAPS_RECORD_DEVCAPS;
  125. spin_lock(&dev_priv->cap_lock);
  126. for (i = 0; i < max_size; ++i) {
  127. vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
  128. compat_cap->pairs[i][0] = i;
  129. compat_cap->pairs[i][1] = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
  130. }
  131. spin_unlock(&dev_priv->cap_lock);
  132. return 0;
  133. }
  134. int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
  135. struct drm_file *file_priv)
  136. {
  137. struct drm_vmw_get_3d_cap_arg *arg =
  138. (struct drm_vmw_get_3d_cap_arg *) data;
  139. struct vmw_private *dev_priv = vmw_priv(dev);
  140. uint32_t size;
  141. __le32 __iomem *fifo_mem;
  142. void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
  143. void *bounce;
  144. int ret;
  145. bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
  146. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  147. if (unlikely(arg->pad64 != 0)) {
  148. DRM_ERROR("Illegal GET_3D_CAP argument.\n");
  149. return -EINVAL;
  150. }
  151. if (gb_objects && vmw_fp->gb_aware)
  152. size = SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
  153. else if (gb_objects)
  154. size = sizeof(struct svga_3d_compat_cap) + sizeof(uint32_t);
  155. else
  156. size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) *
  157. sizeof(uint32_t);
  158. if (arg->max_size < size)
  159. size = arg->max_size;
  160. bounce = vzalloc(size);
  161. if (unlikely(bounce == NULL)) {
  162. DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
  163. return -ENOMEM;
  164. }
  165. if (gb_objects && vmw_fp->gb_aware) {
  166. int i, num;
  167. uint32_t *bounce32 = (uint32_t *) bounce;
  168. num = size / sizeof(uint32_t);
  169. if (num > SVGA3D_DEVCAP_MAX)
  170. num = SVGA3D_DEVCAP_MAX;
  171. spin_lock(&dev_priv->cap_lock);
  172. for (i = 0; i < num; ++i) {
  173. vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
  174. *bounce32++ = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
  175. }
  176. spin_unlock(&dev_priv->cap_lock);
  177. } else if (gb_objects) {
  178. ret = vmw_fill_compat_cap(dev_priv, bounce, size);
  179. if (unlikely(ret != 0))
  180. goto out_err;
  181. } else {
  182. fifo_mem = dev_priv->mmio_virt;
  183. memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
  184. }
  185. ret = copy_to_user(buffer, bounce, size);
  186. if (ret)
  187. ret = -EFAULT;
  188. out_err:
  189. vfree(bounce);
  190. if (unlikely(ret != 0))
  191. DRM_ERROR("Failed to report 3D caps info.\n");
  192. return ret;
  193. }
  194. int vmw_present_ioctl(struct drm_device *dev, void *data,
  195. struct drm_file *file_priv)
  196. {
  197. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  198. struct vmw_private *dev_priv = vmw_priv(dev);
  199. struct drm_vmw_present_arg *arg =
  200. (struct drm_vmw_present_arg *)data;
  201. struct vmw_surface *surface;
  202. struct drm_vmw_rect __user *clips_ptr;
  203. struct drm_vmw_rect *clips = NULL;
  204. struct drm_framebuffer *fb;
  205. struct vmw_framebuffer *vfb;
  206. struct vmw_resource *res;
  207. uint32_t num_clips;
  208. int ret;
  209. num_clips = arg->num_clips;
  210. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  211. if (unlikely(num_clips == 0))
  212. return 0;
  213. if (clips_ptr == NULL) {
  214. DRM_ERROR("Variable clips_ptr must be specified.\n");
  215. ret = -EINVAL;
  216. goto out_clips;
  217. }
  218. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  219. if (clips == NULL) {
  220. DRM_ERROR("Failed to allocate clip rect list.\n");
  221. ret = -ENOMEM;
  222. goto out_clips;
  223. }
  224. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  225. if (ret) {
  226. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  227. ret = -EFAULT;
  228. goto out_no_copy;
  229. }
  230. drm_modeset_lock_all(dev);
  231. fb = drm_framebuffer_lookup(dev, arg->fb_id);
  232. if (!fb) {
  233. DRM_ERROR("Invalid framebuffer id.\n");
  234. ret = -ENOENT;
  235. goto out_no_fb;
  236. }
  237. vfb = vmw_framebuffer_to_vfb(fb);
  238. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  239. if (unlikely(ret != 0))
  240. goto out_no_ttm_lock;
  241. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
  242. user_surface_converter,
  243. &res);
  244. if (ret)
  245. goto out_no_surface;
  246. surface = vmw_res_to_srf(res);
  247. ret = vmw_kms_present(dev_priv, file_priv,
  248. vfb, surface, arg->sid,
  249. arg->dest_x, arg->dest_y,
  250. clips, num_clips);
  251. /* vmw_user_surface_lookup takes one ref so does new_fb */
  252. vmw_surface_unreference(&surface);
  253. out_no_surface:
  254. ttm_read_unlock(&dev_priv->reservation_sem);
  255. out_no_ttm_lock:
  256. drm_framebuffer_unreference(fb);
  257. out_no_fb:
  258. drm_modeset_unlock_all(dev);
  259. out_no_copy:
  260. kfree(clips);
  261. out_clips:
  262. return ret;
  263. }
  264. int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
  265. struct drm_file *file_priv)
  266. {
  267. struct vmw_private *dev_priv = vmw_priv(dev);
  268. struct drm_vmw_present_readback_arg *arg =
  269. (struct drm_vmw_present_readback_arg *)data;
  270. struct drm_vmw_fence_rep __user *user_fence_rep =
  271. (struct drm_vmw_fence_rep __user *)
  272. (unsigned long)arg->fence_rep;
  273. struct drm_vmw_rect __user *clips_ptr;
  274. struct drm_vmw_rect *clips = NULL;
  275. struct drm_framebuffer *fb;
  276. struct vmw_framebuffer *vfb;
  277. uint32_t num_clips;
  278. int ret;
  279. num_clips = arg->num_clips;
  280. clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
  281. if (unlikely(num_clips == 0))
  282. return 0;
  283. if (clips_ptr == NULL) {
  284. DRM_ERROR("Argument clips_ptr must be specified.\n");
  285. ret = -EINVAL;
  286. goto out_clips;
  287. }
  288. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  289. if (clips == NULL) {
  290. DRM_ERROR("Failed to allocate clip rect list.\n");
  291. ret = -ENOMEM;
  292. goto out_clips;
  293. }
  294. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  295. if (ret) {
  296. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  297. ret = -EFAULT;
  298. goto out_no_copy;
  299. }
  300. drm_modeset_lock_all(dev);
  301. fb = drm_framebuffer_lookup(dev, arg->fb_id);
  302. if (!fb) {
  303. DRM_ERROR("Invalid framebuffer id.\n");
  304. ret = -ENOENT;
  305. goto out_no_fb;
  306. }
  307. vfb = vmw_framebuffer_to_vfb(fb);
  308. if (!vfb->dmabuf) {
  309. DRM_ERROR("Framebuffer not dmabuf backed.\n");
  310. ret = -EINVAL;
  311. goto out_no_ttm_lock;
  312. }
  313. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  314. if (unlikely(ret != 0))
  315. goto out_no_ttm_lock;
  316. ret = vmw_kms_readback(dev_priv, file_priv,
  317. vfb, user_fence_rep,
  318. clips, num_clips);
  319. ttm_read_unlock(&dev_priv->reservation_sem);
  320. out_no_ttm_lock:
  321. drm_framebuffer_unreference(fb);
  322. out_no_fb:
  323. drm_modeset_unlock_all(dev);
  324. out_no_copy:
  325. kfree(clips);
  326. out_clips:
  327. return ret;
  328. }
  329. /**
  330. * vmw_fops_poll - wrapper around the drm_poll function
  331. *
  332. * @filp: See the linux fops poll documentation.
  333. * @wait: See the linux fops poll documentation.
  334. *
  335. * Wrapper around the drm_poll function that makes sure the device is
  336. * processing the fifo if drm_poll decides to wait.
  337. */
  338. unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
  339. {
  340. struct drm_file *file_priv = filp->private_data;
  341. struct vmw_private *dev_priv =
  342. vmw_priv(file_priv->minor->dev);
  343. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  344. return drm_poll(filp, wait);
  345. }
  346. /**
  347. * vmw_fops_read - wrapper around the drm_read function
  348. *
  349. * @filp: See the linux fops read documentation.
  350. * @buffer: See the linux fops read documentation.
  351. * @count: See the linux fops read documentation.
  352. * offset: See the linux fops read documentation.
  353. *
  354. * Wrapper around the drm_read function that makes sure the device is
  355. * processing the fifo if drm_read decides to wait.
  356. */
  357. ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
  358. size_t count, loff_t *offset)
  359. {
  360. struct drm_file *file_priv = filp->private_data;
  361. struct vmw_private *dev_priv =
  362. vmw_priv(file_priv->minor->dev);
  363. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  364. return drm_read(filp, buffer, count, offset);
  365. }