vmwgfx_ioctl.c 12 KB

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