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