162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT 262306a36Sopenharmony_ci/************************************************************************** 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Copyright 2009-2022 VMware, Inc., Palo Alto, CA., USA 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 762306a36Sopenharmony_ci * copy of this software and associated documentation files (the 862306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 962306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 1062306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 1162306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 1262306a36Sopenharmony_ci * the following conditions: 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the 1562306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 1662306a36Sopenharmony_ci * of the Software. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1962306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2062306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 2162306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 2262306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2362306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 2462306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci **************************************************************************/ 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci#include "vmwgfx_drv.h" 2962306a36Sopenharmony_ci#include "vmwgfx_devcaps.h" 3062306a36Sopenharmony_ci#include "vmwgfx_kms.h" 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci#include <drm/vmwgfx_drm.h> 3362306a36Sopenharmony_ci#include <linux/pci.h> 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ciint vmw_getparam_ioctl(struct drm_device *dev, void *data, 3662306a36Sopenharmony_ci struct drm_file *file_priv) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci struct vmw_private *dev_priv = vmw_priv(dev); 3962306a36Sopenharmony_ci struct drm_vmw_getparam_arg *param = 4062306a36Sopenharmony_ci (struct drm_vmw_getparam_arg *)data; 4162306a36Sopenharmony_ci struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv); 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci switch (param->param) { 4462306a36Sopenharmony_ci case DRM_VMW_PARAM_NUM_STREAMS: 4562306a36Sopenharmony_ci param->value = vmw_overlay_num_overlays(dev_priv); 4662306a36Sopenharmony_ci break; 4762306a36Sopenharmony_ci case DRM_VMW_PARAM_NUM_FREE_STREAMS: 4862306a36Sopenharmony_ci param->value = vmw_overlay_num_free_overlays(dev_priv); 4962306a36Sopenharmony_ci break; 5062306a36Sopenharmony_ci case DRM_VMW_PARAM_3D: 5162306a36Sopenharmony_ci param->value = vmw_supports_3d(dev_priv) ? 1 : 0; 5262306a36Sopenharmony_ci break; 5362306a36Sopenharmony_ci case DRM_VMW_PARAM_HW_CAPS: 5462306a36Sopenharmony_ci param->value = dev_priv->capabilities; 5562306a36Sopenharmony_ci break; 5662306a36Sopenharmony_ci case DRM_VMW_PARAM_HW_CAPS2: 5762306a36Sopenharmony_ci param->value = dev_priv->capabilities2; 5862306a36Sopenharmony_ci break; 5962306a36Sopenharmony_ci case DRM_VMW_PARAM_FIFO_CAPS: 6062306a36Sopenharmony_ci param->value = vmw_fifo_caps(dev_priv); 6162306a36Sopenharmony_ci break; 6262306a36Sopenharmony_ci case DRM_VMW_PARAM_MAX_FB_SIZE: 6362306a36Sopenharmony_ci param->value = dev_priv->max_primary_mem; 6462306a36Sopenharmony_ci break; 6562306a36Sopenharmony_ci case DRM_VMW_PARAM_FIFO_HW_VERSION: 6662306a36Sopenharmony_ci { 6762306a36Sopenharmony_ci if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS)) 6862306a36Sopenharmony_ci param->value = SVGA3D_HWVERSION_WS8_B1; 6962306a36Sopenharmony_ci else 7062306a36Sopenharmony_ci param->value = vmw_fifo_mem_read( 7162306a36Sopenharmony_ci dev_priv, 7262306a36Sopenharmony_ci ((vmw_fifo_caps(dev_priv) & 7362306a36Sopenharmony_ci SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ? 7462306a36Sopenharmony_ci SVGA_FIFO_3D_HWVERSION_REVISED : 7562306a36Sopenharmony_ci SVGA_FIFO_3D_HWVERSION)); 7662306a36Sopenharmony_ci break; 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci case DRM_VMW_PARAM_MAX_SURF_MEMORY: 7962306a36Sopenharmony_ci if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS) && 8062306a36Sopenharmony_ci !vmw_fp->gb_aware) 8162306a36Sopenharmony_ci param->value = dev_priv->max_mob_pages * PAGE_SIZE / 2; 8262306a36Sopenharmony_ci else 8362306a36Sopenharmony_ci param->value = dev_priv->memory_size; 8462306a36Sopenharmony_ci break; 8562306a36Sopenharmony_ci case DRM_VMW_PARAM_3D_CAPS_SIZE: 8662306a36Sopenharmony_ci param->value = vmw_devcaps_size(dev_priv, vmw_fp->gb_aware); 8762306a36Sopenharmony_ci break; 8862306a36Sopenharmony_ci case DRM_VMW_PARAM_MAX_MOB_MEMORY: 8962306a36Sopenharmony_ci vmw_fp->gb_aware = true; 9062306a36Sopenharmony_ci param->value = dev_priv->max_mob_pages * PAGE_SIZE; 9162306a36Sopenharmony_ci break; 9262306a36Sopenharmony_ci case DRM_VMW_PARAM_MAX_MOB_SIZE: 9362306a36Sopenharmony_ci param->value = dev_priv->max_mob_size; 9462306a36Sopenharmony_ci break; 9562306a36Sopenharmony_ci case DRM_VMW_PARAM_SCREEN_TARGET: 9662306a36Sopenharmony_ci param->value = 9762306a36Sopenharmony_ci (dev_priv->active_display_unit == vmw_du_screen_target); 9862306a36Sopenharmony_ci break; 9962306a36Sopenharmony_ci case DRM_VMW_PARAM_DX: 10062306a36Sopenharmony_ci param->value = has_sm4_context(dev_priv); 10162306a36Sopenharmony_ci break; 10262306a36Sopenharmony_ci case DRM_VMW_PARAM_SM4_1: 10362306a36Sopenharmony_ci param->value = has_sm4_1_context(dev_priv); 10462306a36Sopenharmony_ci break; 10562306a36Sopenharmony_ci case DRM_VMW_PARAM_SM5: 10662306a36Sopenharmony_ci param->value = has_sm5_context(dev_priv); 10762306a36Sopenharmony_ci break; 10862306a36Sopenharmony_ci case DRM_VMW_PARAM_GL43: 10962306a36Sopenharmony_ci param->value = has_gl43_context(dev_priv); 11062306a36Sopenharmony_ci break; 11162306a36Sopenharmony_ci case DRM_VMW_PARAM_DEVICE_ID: 11262306a36Sopenharmony_ci param->value = to_pci_dev(dev_priv->drm.dev)->device; 11362306a36Sopenharmony_ci break; 11462306a36Sopenharmony_ci default: 11562306a36Sopenharmony_ci return -EINVAL; 11662306a36Sopenharmony_ci } 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci return 0; 11962306a36Sopenharmony_ci} 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ciint vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data, 12362306a36Sopenharmony_ci struct drm_file *file_priv) 12462306a36Sopenharmony_ci{ 12562306a36Sopenharmony_ci struct drm_vmw_get_3d_cap_arg *arg = 12662306a36Sopenharmony_ci (struct drm_vmw_get_3d_cap_arg *) data; 12762306a36Sopenharmony_ci struct vmw_private *dev_priv = vmw_priv(dev); 12862306a36Sopenharmony_ci uint32_t size; 12962306a36Sopenharmony_ci void __user *buffer = (void __user *)((unsigned long)(arg->buffer)); 13062306a36Sopenharmony_ci void *bounce = NULL; 13162306a36Sopenharmony_ci int ret; 13262306a36Sopenharmony_ci struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci if (unlikely(arg->pad64 != 0 || arg->max_size == 0)) { 13562306a36Sopenharmony_ci VMW_DEBUG_USER("Illegal GET_3D_CAP argument.\n"); 13662306a36Sopenharmony_ci return -EINVAL; 13762306a36Sopenharmony_ci } 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci size = vmw_devcaps_size(dev_priv, vmw_fp->gb_aware); 14062306a36Sopenharmony_ci if (unlikely(size == 0)) { 14162306a36Sopenharmony_ci DRM_ERROR("Failed to figure out the devcaps size (no 3D).\n"); 14262306a36Sopenharmony_ci return -ENOMEM; 14362306a36Sopenharmony_ci } 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci if (arg->max_size < size) 14662306a36Sopenharmony_ci size = arg->max_size; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci bounce = vzalloc(size); 14962306a36Sopenharmony_ci if (unlikely(bounce == NULL)) { 15062306a36Sopenharmony_ci DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n"); 15162306a36Sopenharmony_ci return -ENOMEM; 15262306a36Sopenharmony_ci } 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci ret = vmw_devcaps_copy(dev_priv, vmw_fp->gb_aware, bounce, size); 15562306a36Sopenharmony_ci if (unlikely (ret != 0)) 15662306a36Sopenharmony_ci goto out_err; 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci ret = copy_to_user(buffer, bounce, size); 15962306a36Sopenharmony_ci if (ret) 16062306a36Sopenharmony_ci ret = -EFAULT; 16162306a36Sopenharmony_ciout_err: 16262306a36Sopenharmony_ci vfree(bounce); 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci if (unlikely(ret != 0)) 16562306a36Sopenharmony_ci DRM_ERROR("Failed to report 3D caps info.\n"); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci return ret; 16862306a36Sopenharmony_ci} 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ciint vmw_present_ioctl(struct drm_device *dev, void *data, 17162306a36Sopenharmony_ci struct drm_file *file_priv) 17262306a36Sopenharmony_ci{ 17362306a36Sopenharmony_ci struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 17462306a36Sopenharmony_ci struct vmw_private *dev_priv = vmw_priv(dev); 17562306a36Sopenharmony_ci struct drm_vmw_present_arg *arg = 17662306a36Sopenharmony_ci (struct drm_vmw_present_arg *)data; 17762306a36Sopenharmony_ci struct vmw_surface *surface; 17862306a36Sopenharmony_ci struct drm_vmw_rect __user *clips_ptr; 17962306a36Sopenharmony_ci struct drm_vmw_rect *clips = NULL; 18062306a36Sopenharmony_ci struct drm_framebuffer *fb; 18162306a36Sopenharmony_ci struct vmw_framebuffer *vfb; 18262306a36Sopenharmony_ci struct vmw_resource *res; 18362306a36Sopenharmony_ci uint32_t num_clips; 18462306a36Sopenharmony_ci int ret; 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci num_clips = arg->num_clips; 18762306a36Sopenharmony_ci clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr; 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci if (unlikely(num_clips == 0)) 19062306a36Sopenharmony_ci return 0; 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci if (clips_ptr == NULL) { 19362306a36Sopenharmony_ci VMW_DEBUG_USER("Variable clips_ptr must be specified.\n"); 19462306a36Sopenharmony_ci ret = -EINVAL; 19562306a36Sopenharmony_ci goto out_clips; 19662306a36Sopenharmony_ci } 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL); 19962306a36Sopenharmony_ci if (clips == NULL) { 20062306a36Sopenharmony_ci DRM_ERROR("Failed to allocate clip rect list.\n"); 20162306a36Sopenharmony_ci ret = -ENOMEM; 20262306a36Sopenharmony_ci goto out_clips; 20362306a36Sopenharmony_ci } 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips)); 20662306a36Sopenharmony_ci if (ret) { 20762306a36Sopenharmony_ci DRM_ERROR("Failed to copy clip rects from userspace.\n"); 20862306a36Sopenharmony_ci ret = -EFAULT; 20962306a36Sopenharmony_ci goto out_no_copy; 21062306a36Sopenharmony_ci } 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci drm_modeset_lock_all(dev); 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci fb = drm_framebuffer_lookup(dev, file_priv, arg->fb_id); 21562306a36Sopenharmony_ci if (!fb) { 21662306a36Sopenharmony_ci VMW_DEBUG_USER("Invalid framebuffer id.\n"); 21762306a36Sopenharmony_ci ret = -ENOENT; 21862306a36Sopenharmony_ci goto out_no_fb; 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci vfb = vmw_framebuffer_to_vfb(fb); 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid, 22362306a36Sopenharmony_ci user_surface_converter, 22462306a36Sopenharmony_ci &res); 22562306a36Sopenharmony_ci if (ret) 22662306a36Sopenharmony_ci goto out_no_surface; 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci surface = vmw_res_to_srf(res); 22962306a36Sopenharmony_ci ret = vmw_kms_present(dev_priv, file_priv, 23062306a36Sopenharmony_ci vfb, surface, arg->sid, 23162306a36Sopenharmony_ci arg->dest_x, arg->dest_y, 23262306a36Sopenharmony_ci clips, num_clips); 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci /* vmw_user_surface_lookup takes one ref so does new_fb */ 23562306a36Sopenharmony_ci vmw_surface_unreference(&surface); 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ciout_no_surface: 23862306a36Sopenharmony_ci drm_framebuffer_put(fb); 23962306a36Sopenharmony_ciout_no_fb: 24062306a36Sopenharmony_ci drm_modeset_unlock_all(dev); 24162306a36Sopenharmony_ciout_no_copy: 24262306a36Sopenharmony_ci kfree(clips); 24362306a36Sopenharmony_ciout_clips: 24462306a36Sopenharmony_ci return ret; 24562306a36Sopenharmony_ci} 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ciint vmw_present_readback_ioctl(struct drm_device *dev, void *data, 24862306a36Sopenharmony_ci struct drm_file *file_priv) 24962306a36Sopenharmony_ci{ 25062306a36Sopenharmony_ci struct vmw_private *dev_priv = vmw_priv(dev); 25162306a36Sopenharmony_ci struct drm_vmw_present_readback_arg *arg = 25262306a36Sopenharmony_ci (struct drm_vmw_present_readback_arg *)data; 25362306a36Sopenharmony_ci struct drm_vmw_fence_rep __user *user_fence_rep = 25462306a36Sopenharmony_ci (struct drm_vmw_fence_rep __user *) 25562306a36Sopenharmony_ci (unsigned long)arg->fence_rep; 25662306a36Sopenharmony_ci struct drm_vmw_rect __user *clips_ptr; 25762306a36Sopenharmony_ci struct drm_vmw_rect *clips = NULL; 25862306a36Sopenharmony_ci struct drm_framebuffer *fb; 25962306a36Sopenharmony_ci struct vmw_framebuffer *vfb; 26062306a36Sopenharmony_ci uint32_t num_clips; 26162306a36Sopenharmony_ci int ret; 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci num_clips = arg->num_clips; 26462306a36Sopenharmony_ci clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr; 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci if (unlikely(num_clips == 0)) 26762306a36Sopenharmony_ci return 0; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci if (clips_ptr == NULL) { 27062306a36Sopenharmony_ci VMW_DEBUG_USER("Argument clips_ptr must be specified.\n"); 27162306a36Sopenharmony_ci ret = -EINVAL; 27262306a36Sopenharmony_ci goto out_clips; 27362306a36Sopenharmony_ci } 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL); 27662306a36Sopenharmony_ci if (clips == NULL) { 27762306a36Sopenharmony_ci DRM_ERROR("Failed to allocate clip rect list.\n"); 27862306a36Sopenharmony_ci ret = -ENOMEM; 27962306a36Sopenharmony_ci goto out_clips; 28062306a36Sopenharmony_ci } 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips)); 28362306a36Sopenharmony_ci if (ret) { 28462306a36Sopenharmony_ci DRM_ERROR("Failed to copy clip rects from userspace.\n"); 28562306a36Sopenharmony_ci ret = -EFAULT; 28662306a36Sopenharmony_ci goto out_no_copy; 28762306a36Sopenharmony_ci } 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci drm_modeset_lock_all(dev); 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci fb = drm_framebuffer_lookup(dev, file_priv, arg->fb_id); 29262306a36Sopenharmony_ci if (!fb) { 29362306a36Sopenharmony_ci VMW_DEBUG_USER("Invalid framebuffer id.\n"); 29462306a36Sopenharmony_ci ret = -ENOENT; 29562306a36Sopenharmony_ci goto out_no_fb; 29662306a36Sopenharmony_ci } 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci vfb = vmw_framebuffer_to_vfb(fb); 29962306a36Sopenharmony_ci if (!vfb->bo) { 30062306a36Sopenharmony_ci VMW_DEBUG_USER("Framebuffer not buffer backed.\n"); 30162306a36Sopenharmony_ci ret = -EINVAL; 30262306a36Sopenharmony_ci goto out_no_ttm_lock; 30362306a36Sopenharmony_ci } 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci ret = vmw_kms_readback(dev_priv, file_priv, 30662306a36Sopenharmony_ci vfb, user_fence_rep, 30762306a36Sopenharmony_ci clips, num_clips); 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ciout_no_ttm_lock: 31062306a36Sopenharmony_ci drm_framebuffer_put(fb); 31162306a36Sopenharmony_ciout_no_fb: 31262306a36Sopenharmony_ci drm_modeset_unlock_all(dev); 31362306a36Sopenharmony_ciout_no_copy: 31462306a36Sopenharmony_ci kfree(clips); 31562306a36Sopenharmony_ciout_clips: 31662306a36Sopenharmony_ci return ret; 31762306a36Sopenharmony_ci} 318