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 	case DRM_VMW_PARAM_GL43:
109 		param->value = has_gl43_context(dev_priv);
110 		break;
111 	default:
112 		return -EINVAL;
113 	}
114 
115 	return 0;
116 }
117 
118 
119 int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
120 			 struct drm_file *file_priv)
121 {
122 	struct drm_vmw_get_3d_cap_arg *arg =
123 		(struct drm_vmw_get_3d_cap_arg *) data;
124 	struct vmw_private *dev_priv = vmw_priv(dev);
125 	uint32_t size;
126 	void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
127 	void *bounce = NULL;
128 	int ret;
129 	struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
130 
131 	if (unlikely(arg->pad64 != 0 || arg->max_size == 0)) {
132 		VMW_DEBUG_USER("Illegal GET_3D_CAP argument.\n");
133 		return -EINVAL;
134 	}
135 
136 	size = vmw_devcaps_size(dev_priv, vmw_fp->gb_aware);
137 	if (unlikely(size == 0)) {
138 		DRM_ERROR("Failed to figure out the devcaps size (no 3D).\n");
139 		return -ENOMEM;
140 	}
141 
142 	if (arg->max_size < size)
143 		size = arg->max_size;
144 
145 	bounce = vzalloc(size);
146 	if (unlikely(bounce == NULL)) {
147 		DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
148 		return -ENOMEM;
149 	}
150 
151 	ret = vmw_devcaps_copy(dev_priv, vmw_fp->gb_aware, bounce, size);
152 	if (unlikely (ret != 0))
153 		goto out_err;
154 
155 	ret = copy_to_user(buffer, bounce, size);
156 	if (ret)
157 		ret = -EFAULT;
158 out_err:
159 	vfree(bounce);
160 
161 	if (unlikely(ret != 0))
162 		DRM_ERROR("Failed to report 3D caps info.\n");
163 
164 	return ret;
165 }
166 
167 int vmw_present_ioctl(struct drm_device *dev, void *data,
168 		      struct drm_file *file_priv)
169 {
170 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
171 	struct vmw_private *dev_priv = vmw_priv(dev);
172 	struct drm_vmw_present_arg *arg =
173 		(struct drm_vmw_present_arg *)data;
174 	struct vmw_surface *surface;
175 	struct drm_vmw_rect __user *clips_ptr;
176 	struct drm_vmw_rect *clips = NULL;
177 	struct drm_framebuffer *fb;
178 	struct vmw_framebuffer *vfb;
179 	struct vmw_resource *res;
180 	uint32_t num_clips;
181 	int ret;
182 
183 	num_clips = arg->num_clips;
184 	clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr;
185 
186 	if (unlikely(num_clips == 0))
187 		return 0;
188 
189 	if (clips_ptr == NULL) {
190 		VMW_DEBUG_USER("Variable clips_ptr must be specified.\n");
191 		ret = -EINVAL;
192 		goto out_clips;
193 	}
194 
195 	clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
196 	if (clips == NULL) {
197 		DRM_ERROR("Failed to allocate clip rect list.\n");
198 		ret = -ENOMEM;
199 		goto out_clips;
200 	}
201 
202 	ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
203 	if (ret) {
204 		DRM_ERROR("Failed to copy clip rects from userspace.\n");
205 		ret = -EFAULT;
206 		goto out_no_copy;
207 	}
208 
209 	drm_modeset_lock_all(dev);
210 
211 	fb = drm_framebuffer_lookup(dev, file_priv, arg->fb_id);
212 	if (!fb) {
213 		VMW_DEBUG_USER("Invalid framebuffer id.\n");
214 		ret = -ENOENT;
215 		goto out_no_fb;
216 	}
217 	vfb = vmw_framebuffer_to_vfb(fb);
218 
219 	ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
220 					      user_surface_converter,
221 					      &res);
222 	if (ret)
223 		goto out_no_surface;
224 
225 	surface = vmw_res_to_srf(res);
226 	ret = vmw_kms_present(dev_priv, file_priv,
227 			      vfb, surface, arg->sid,
228 			      arg->dest_x, arg->dest_y,
229 			      clips, num_clips);
230 
231 	/* vmw_user_surface_lookup takes one ref so does new_fb */
232 	vmw_surface_unreference(&surface);
233 
234 out_no_surface:
235 	drm_framebuffer_put(fb);
236 out_no_fb:
237 	drm_modeset_unlock_all(dev);
238 out_no_copy:
239 	kfree(clips);
240 out_clips:
241 	return ret;
242 }
243 
244 int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
245 			       struct drm_file *file_priv)
246 {
247 	struct vmw_private *dev_priv = vmw_priv(dev);
248 	struct drm_vmw_present_readback_arg *arg =
249 		(struct drm_vmw_present_readback_arg *)data;
250 	struct drm_vmw_fence_rep __user *user_fence_rep =
251 		(struct drm_vmw_fence_rep __user *)
252 		(unsigned long)arg->fence_rep;
253 	struct drm_vmw_rect __user *clips_ptr;
254 	struct drm_vmw_rect *clips = NULL;
255 	struct drm_framebuffer *fb;
256 	struct vmw_framebuffer *vfb;
257 	uint32_t num_clips;
258 	int ret;
259 
260 	num_clips = arg->num_clips;
261 	clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr;
262 
263 	if (unlikely(num_clips == 0))
264 		return 0;
265 
266 	if (clips_ptr == NULL) {
267 		VMW_DEBUG_USER("Argument clips_ptr must be specified.\n");
268 		ret = -EINVAL;
269 		goto out_clips;
270 	}
271 
272 	clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
273 	if (clips == NULL) {
274 		DRM_ERROR("Failed to allocate clip rect list.\n");
275 		ret = -ENOMEM;
276 		goto out_clips;
277 	}
278 
279 	ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
280 	if (ret) {
281 		DRM_ERROR("Failed to copy clip rects from userspace.\n");
282 		ret = -EFAULT;
283 		goto out_no_copy;
284 	}
285 
286 	drm_modeset_lock_all(dev);
287 
288 	fb = drm_framebuffer_lookup(dev, file_priv, arg->fb_id);
289 	if (!fb) {
290 		VMW_DEBUG_USER("Invalid framebuffer id.\n");
291 		ret = -ENOENT;
292 		goto out_no_fb;
293 	}
294 
295 	vfb = vmw_framebuffer_to_vfb(fb);
296 	if (!vfb->bo) {
297 		VMW_DEBUG_USER("Framebuffer not buffer backed.\n");
298 		ret = -EINVAL;
299 		goto out_no_ttm_lock;
300 	}
301 
302 	ret = vmw_kms_readback(dev_priv, file_priv,
303 			       vfb, user_fence_rep,
304 			       clips, num_clips);
305 
306 out_no_ttm_lock:
307 	drm_framebuffer_put(fb);
308 out_no_fb:
309 	drm_modeset_unlock_all(dev);
310 out_no_copy:
311 	kfree(clips);
312 out_clips:
313 	return ret;
314 }
315