vmwgfx_ioctl.c 12 KB

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