vmwgfx_ioctl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-2022 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 "vmwgfx_devcaps.h"
  29. #include "vmwgfx_kms.h"
  30. #include <drm/vmwgfx_drm.h>
  31. #include <linux/pci.h>
  32. int vmw_getparam_ioctl(struct drm_device *dev, void *data,
  33. struct drm_file *file_priv)
  34. {
  35. struct vmw_private *dev_priv = vmw_priv(dev);
  36. struct drm_vmw_getparam_arg *param =
  37. (struct drm_vmw_getparam_arg *)data;
  38. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  39. switch (param->param) {
  40. case DRM_VMW_PARAM_NUM_STREAMS:
  41. param->value = vmw_overlay_num_overlays(dev_priv);
  42. break;
  43. case DRM_VMW_PARAM_NUM_FREE_STREAMS:
  44. param->value = vmw_overlay_num_free_overlays(dev_priv);
  45. break;
  46. case DRM_VMW_PARAM_3D:
  47. param->value = vmw_supports_3d(dev_priv) ? 1 : 0;
  48. break;
  49. case DRM_VMW_PARAM_HW_CAPS:
  50. param->value = dev_priv->capabilities;
  51. break;
  52. case DRM_VMW_PARAM_HW_CAPS2:
  53. param->value = dev_priv->capabilities2;
  54. break;
  55. case DRM_VMW_PARAM_FIFO_CAPS:
  56. param->value = vmw_fifo_caps(dev_priv);
  57. break;
  58. case DRM_VMW_PARAM_MAX_FB_SIZE:
  59. param->value = dev_priv->max_primary_mem;
  60. break;
  61. case DRM_VMW_PARAM_FIFO_HW_VERSION:
  62. {
  63. if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
  64. param->value = SVGA3D_HWVERSION_WS8_B1;
  65. else
  66. param->value = vmw_fifo_mem_read(
  67. dev_priv,
  68. ((vmw_fifo_caps(dev_priv) &
  69. SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
  70. SVGA_FIFO_3D_HWVERSION_REVISED :
  71. SVGA_FIFO_3D_HWVERSION));
  72. break;
  73. }
  74. case DRM_VMW_PARAM_MAX_SURF_MEMORY:
  75. if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS) &&
  76. !vmw_fp->gb_aware)
  77. param->value = dev_priv->max_mob_pages * PAGE_SIZE / 2;
  78. else
  79. param->value = dev_priv->memory_size;
  80. break;
  81. case DRM_VMW_PARAM_3D_CAPS_SIZE:
  82. param->value = vmw_devcaps_size(dev_priv, vmw_fp->gb_aware);
  83. break;
  84. case DRM_VMW_PARAM_MAX_MOB_MEMORY:
  85. vmw_fp->gb_aware = true;
  86. param->value = dev_priv->max_mob_pages * PAGE_SIZE;
  87. break;
  88. case DRM_VMW_PARAM_MAX_MOB_SIZE:
  89. param->value = dev_priv->max_mob_size;
  90. break;
  91. case DRM_VMW_PARAM_SCREEN_TARGET:
  92. param->value =
  93. (dev_priv->active_display_unit == vmw_du_screen_target);
  94. break;
  95. case DRM_VMW_PARAM_DX:
  96. param->value = has_sm4_context(dev_priv);
  97. break;
  98. case DRM_VMW_PARAM_SM4_1:
  99. param->value = has_sm4_1_context(dev_priv);
  100. break;
  101. case DRM_VMW_PARAM_SM5:
  102. param->value = has_sm5_context(dev_priv);
  103. break;
  104. case DRM_VMW_PARAM_GL43:
  105. param->value = has_gl43_context(dev_priv);
  106. break;
  107. case DRM_VMW_PARAM_DEVICE_ID:
  108. param->value = to_pci_dev(dev_priv->drm.dev)->device;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. return 0;
  114. }
  115. int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
  116. struct drm_file *file_priv)
  117. {
  118. struct drm_vmw_get_3d_cap_arg *arg =
  119. (struct drm_vmw_get_3d_cap_arg *) data;
  120. struct vmw_private *dev_priv = vmw_priv(dev);
  121. uint32_t size;
  122. void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
  123. void *bounce = NULL;
  124. int ret;
  125. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  126. if (unlikely(arg->pad64 != 0 || arg->max_size == 0)) {
  127. VMW_DEBUG_USER("Illegal GET_3D_CAP argument.\n");
  128. return -EINVAL;
  129. }
  130. size = vmw_devcaps_size(dev_priv, vmw_fp->gb_aware);
  131. if (unlikely(size == 0)) {
  132. DRM_ERROR("Failed to figure out the devcaps size (no 3D).\n");
  133. return -ENOMEM;
  134. }
  135. if (arg->max_size < size)
  136. size = arg->max_size;
  137. bounce = vzalloc(size);
  138. if (unlikely(bounce == NULL)) {
  139. DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
  140. return -ENOMEM;
  141. }
  142. ret = vmw_devcaps_copy(dev_priv, vmw_fp->gb_aware, bounce, size);
  143. if (unlikely (ret != 0))
  144. goto out_err;
  145. ret = copy_to_user(buffer, bounce, size);
  146. if (ret)
  147. ret = -EFAULT;
  148. out_err:
  149. vfree(bounce);
  150. if (unlikely(ret != 0))
  151. DRM_ERROR("Failed to report 3D caps info.\n");
  152. return ret;
  153. }
  154. int vmw_present_ioctl(struct drm_device *dev, void *data,
  155. struct drm_file *file_priv)
  156. {
  157. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  158. struct vmw_private *dev_priv = vmw_priv(dev);
  159. struct drm_vmw_present_arg *arg =
  160. (struct drm_vmw_present_arg *)data;
  161. struct vmw_surface *surface;
  162. struct drm_vmw_rect __user *clips_ptr;
  163. struct drm_vmw_rect *clips = NULL;
  164. struct drm_framebuffer *fb;
  165. struct vmw_framebuffer *vfb;
  166. struct vmw_resource *res;
  167. uint32_t num_clips;
  168. int ret;
  169. num_clips = arg->num_clips;
  170. clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr;
  171. if (unlikely(num_clips == 0))
  172. return 0;
  173. if (clips_ptr == NULL) {
  174. VMW_DEBUG_USER("Variable clips_ptr must be specified.\n");
  175. ret = -EINVAL;
  176. goto out_clips;
  177. }
  178. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  179. if (clips == NULL) {
  180. DRM_ERROR("Failed to allocate clip rect list.\n");
  181. ret = -ENOMEM;
  182. goto out_clips;
  183. }
  184. ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
  185. if (ret) {
  186. DRM_ERROR("Failed to copy clip rects from userspace.\n");
  187. ret = -EFAULT;
  188. goto out_no_copy;
  189. }
  190. drm_modeset_lock_all(dev);
  191. fb = drm_framebuffer_lookup(dev, file_priv, arg->fb_id);
  192. if (!fb) {
  193. VMW_DEBUG_USER("Invalid framebuffer id.\n");
  194. ret = -ENOENT;
  195. goto out_no_fb;
  196. }
  197. vfb = vmw_framebuffer_to_vfb(fb);
  198. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
  199. user_surface_converter,
  200. &res);
  201. if (ret)
  202. goto out_no_surface;
  203. surface = vmw_res_to_srf(res);
  204. ret = vmw_kms_present(dev_priv, file_priv,
  205. vfb, surface, arg->sid,
  206. arg->dest_x, arg->dest_y,
  207. clips, num_clips);
  208. /* vmw_user_surface_lookup takes one ref so does new_fb */
  209. vmw_surface_unreference(&surface);
  210. out_no_surface:
  211. drm_framebuffer_put(fb);
  212. out_no_fb:
  213. drm_modeset_unlock_all(dev);
  214. out_no_copy:
  215. kfree(clips);
  216. out_clips:
  217. return ret;
  218. }
  219. int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
  220. struct drm_file *file_priv)
  221. {
  222. struct vmw_private *dev_priv = vmw_priv(dev);
  223. struct drm_vmw_present_readback_arg *arg =
  224. (struct drm_vmw_present_readback_arg *)data;
  225. struct drm_vmw_fence_rep __user *user_fence_rep =
  226. (struct drm_vmw_fence_rep __user *)
  227. (unsigned long)arg->fence_rep;
  228. struct drm_vmw_rect __user *clips_ptr;
  229. struct drm_vmw_rect *clips = NULL;
  230. struct drm_framebuffer *fb;
  231. struct vmw_framebuffer *vfb;
  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. VMW_DEBUG_USER("Argument 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. VMW_DEBUG_USER("Invalid framebuffer id.\n");
  259. ret = -ENOENT;
  260. goto out_no_fb;
  261. }
  262. vfb = vmw_framebuffer_to_vfb(fb);
  263. if (!vfb->bo) {
  264. VMW_DEBUG_USER("Framebuffer not buffer backed.\n");
  265. ret = -EINVAL;
  266. goto out_no_ttm_lock;
  267. }
  268. ret = vmw_kms_readback(dev_priv, file_priv,
  269. vfb, user_fence_rep,
  270. clips, num_clips);
  271. out_no_ttm_lock:
  272. drm_framebuffer_put(fb);
  273. out_no_fb:
  274. drm_modeset_unlock_all(dev);
  275. out_no_copy:
  276. kfree(clips);
  277. out_clips:
  278. return ret;
  279. }