vmwgfx_ioctl.c 12 KB

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