vmwgfx_ioctl.c 8.6 KB

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