1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
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 <drm/vmwgfx_drm.h>
30 #include "vmwgfx_kms.h"
31 
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 
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_fifo_have_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_FIFO_CAPS:
53 		param->value = dev_priv->fifo.capabilities;
54 		break;
55 	case DRM_VMW_PARAM_MAX_FB_SIZE:
56 		param->value = dev_priv->prim_bb_mem;
57 		break;
58 	case DRM_VMW_PARAM_FIFO_HW_VERSION:
59 	{
60 		__le32 __iomem *fifo_mem = dev_priv->mmio_virt;
61 		const struct vmw_fifo_state *fifo = &dev_priv->fifo;
62 
63 		param->value =
64 			ioread32(fifo_mem +
65 				 ((fifo->capabilities &
66 				   SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
67 				  SVGA_FIFO_3D_HWVERSION_REVISED :
68 				  SVGA_FIFO_3D_HWVERSION));
69 		break;
70 	}
71 	case DRM_VMW_PARAM_MAX_SURF_MEMORY:
72 		param->value = dev_priv->memory_size;
73 		break;
74 	case DRM_VMW_PARAM_3D_CAPS_SIZE:
75 		if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS)
76 			param->value = SVGA3D_DEVCAP_MAX;
77 		else
78 			param->value = (SVGA_FIFO_3D_CAPS_LAST -
79 					SVGA_FIFO_3D_CAPS + 1);
80 		param->value *= sizeof(uint32_t);
81 		break;
82 	case DRM_VMW_PARAM_MAX_MOB_MEMORY:
83 		param->value = dev_priv->max_mob_pages * PAGE_SIZE;
84 		break;
85 	default:
86 		DRM_ERROR("Illegal vmwgfx get param request: %d\n",
87 			  param->param);
88 		return -EINVAL;
89 	}
90 
91 	return 0;
92 }
93 
94 
95 int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
96 			 struct drm_file *file_priv)
97 {
98 	struct drm_vmw_get_3d_cap_arg *arg =
99 		(struct drm_vmw_get_3d_cap_arg *) data;
100 	struct vmw_private *dev_priv = vmw_priv(dev);
101 	uint32_t size;
102 	__le32 __iomem *fifo_mem;
103 	void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
104 	void *bounce;
105 	int ret;
106 	bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
107 
108 	if (unlikely(arg->pad64 != 0)) {
109 		DRM_ERROR("Illegal GET_3D_CAP argument.\n");
110 		return -EINVAL;
111 	}
112 
113 	if (gb_objects)
114 		size = SVGA3D_DEVCAP_MAX;
115 	else
116 		size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1);
117 
118 	size *= sizeof(uint32_t);
119 
120 	if (arg->max_size < size)
121 		size = arg->max_size;
122 
123 	bounce = vmalloc(size);
124 	if (unlikely(bounce == NULL)) {
125 		DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
126 		return -ENOMEM;
127 	}
128 
129 	if (gb_objects) {
130 		int i;
131 		uint32_t *bounce32 = (uint32_t *) bounce;
132 
133 		mutex_lock(&dev_priv->hw_mutex);
134 		for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
135 			vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
136 			*bounce32++ = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
137 		}
138 		mutex_unlock(&dev_priv->hw_mutex);
139 
140 	} else {
141 
142 		fifo_mem = dev_priv->mmio_virt;
143 		memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
144 	}
145 
146 	ret = copy_to_user(buffer, bounce, size);
147 	if (ret)
148 		ret = -EFAULT;
149 	vfree(bounce);
150 
151 	if (unlikely(ret != 0))
152 		DRM_ERROR("Failed to report 3D caps info.\n");
153 
154 	return ret;
155 }
156 
157 int vmw_present_ioctl(struct drm_device *dev, void *data,
158 		      struct drm_file *file_priv)
159 {
160 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
161 	struct vmw_private *dev_priv = vmw_priv(dev);
162 	struct drm_vmw_present_arg *arg =
163 		(struct drm_vmw_present_arg *)data;
164 	struct vmw_surface *surface;
165 	struct vmw_master *vmaster = vmw_master(file_priv->master);
166 	struct drm_vmw_rect __user *clips_ptr;
167 	struct drm_vmw_rect *clips = NULL;
168 	struct drm_framebuffer *fb;
169 	struct vmw_framebuffer *vfb;
170 	struct vmw_resource *res;
171 	uint32_t num_clips;
172 	int ret;
173 
174 	num_clips = arg->num_clips;
175 	clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
176 
177 	if (unlikely(num_clips == 0))
178 		return 0;
179 
180 	if (clips_ptr == NULL) {
181 		DRM_ERROR("Variable clips_ptr must be specified.\n");
182 		ret = -EINVAL;
183 		goto out_clips;
184 	}
185 
186 	clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
187 	if (clips == NULL) {
188 		DRM_ERROR("Failed to allocate clip rect list.\n");
189 		ret = -ENOMEM;
190 		goto out_clips;
191 	}
192 
193 	ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
194 	if (ret) {
195 		DRM_ERROR("Failed to copy clip rects from userspace.\n");
196 		ret = -EFAULT;
197 		goto out_no_copy;
198 	}
199 
200 	drm_modeset_lock_all(dev);
201 
202 	fb = drm_framebuffer_lookup(dev, arg->fb_id);
203 	if (!fb) {
204 		DRM_ERROR("Invalid framebuffer id.\n");
205 		ret = -ENOENT;
206 		goto out_no_fb;
207 	}
208 	vfb = vmw_framebuffer_to_vfb(fb);
209 
210 	ret = ttm_read_lock(&vmaster->lock, true);
211 	if (unlikely(ret != 0))
212 		goto out_no_ttm_lock;
213 
214 	ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
215 					      user_surface_converter,
216 					      &res);
217 	if (ret)
218 		goto out_no_surface;
219 
220 	surface = vmw_res_to_srf(res);
221 	ret = vmw_kms_present(dev_priv, file_priv,
222 			      vfb, surface, arg->sid,
223 			      arg->dest_x, arg->dest_y,
224 			      clips, num_clips);
225 
226 	/* vmw_user_surface_lookup takes one ref so does new_fb */
227 	vmw_surface_unreference(&surface);
228 
229 out_no_surface:
230 	ttm_read_unlock(&vmaster->lock);
231 out_no_ttm_lock:
232 	drm_framebuffer_unreference(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 vmw_master *vmaster = vmw_master(file_priv->master);
251 	struct drm_vmw_rect __user *clips_ptr;
252 	struct drm_vmw_rect *clips = NULL;
253 	struct drm_framebuffer *fb;
254 	struct vmw_framebuffer *vfb;
255 	uint32_t num_clips;
256 	int ret;
257 
258 	num_clips = arg->num_clips;
259 	clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
260 
261 	if (unlikely(num_clips == 0))
262 		return 0;
263 
264 	if (clips_ptr == NULL) {
265 		DRM_ERROR("Argument clips_ptr must be specified.\n");
266 		ret = -EINVAL;
267 		goto out_clips;
268 	}
269 
270 	clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
271 	if (clips == NULL) {
272 		DRM_ERROR("Failed to allocate clip rect list.\n");
273 		ret = -ENOMEM;
274 		goto out_clips;
275 	}
276 
277 	ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
278 	if (ret) {
279 		DRM_ERROR("Failed to copy clip rects from userspace.\n");
280 		ret = -EFAULT;
281 		goto out_no_copy;
282 	}
283 
284 	drm_modeset_lock_all(dev);
285 
286 	fb = drm_framebuffer_lookup(dev, arg->fb_id);
287 	if (!fb) {
288 		DRM_ERROR("Invalid framebuffer id.\n");
289 		ret = -ENOENT;
290 		goto out_no_fb;
291 	}
292 
293 	vfb = vmw_framebuffer_to_vfb(fb);
294 	if (!vfb->dmabuf) {
295 		DRM_ERROR("Framebuffer not dmabuf backed.\n");
296 		ret = -EINVAL;
297 		goto out_no_ttm_lock;
298 	}
299 
300 	ret = ttm_read_lock(&vmaster->lock, true);
301 	if (unlikely(ret != 0))
302 		goto out_no_ttm_lock;
303 
304 	ret = vmw_kms_readback(dev_priv, file_priv,
305 			       vfb, user_fence_rep,
306 			       clips, num_clips);
307 
308 	ttm_read_unlock(&vmaster->lock);
309 out_no_ttm_lock:
310 	drm_framebuffer_unreference(fb);
311 out_no_fb:
312 	drm_modeset_unlock_all(dev);
313 out_no_copy:
314 	kfree(clips);
315 out_clips:
316 	return ret;
317 }
318 
319 
320 /**
321  * vmw_fops_poll - wrapper around the drm_poll function
322  *
323  * @filp: See the linux fops poll documentation.
324  * @wait: See the linux fops poll documentation.
325  *
326  * Wrapper around the drm_poll function that makes sure the device is
327  * processing the fifo if drm_poll decides to wait.
328  */
329 unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
330 {
331 	struct drm_file *file_priv = filp->private_data;
332 	struct vmw_private *dev_priv =
333 		vmw_priv(file_priv->minor->dev);
334 
335 	vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
336 	return drm_poll(filp, wait);
337 }
338 
339 
340 /**
341  * vmw_fops_read - wrapper around the drm_read function
342  *
343  * @filp: See the linux fops read documentation.
344  * @buffer: See the linux fops read documentation.
345  * @count: See the linux fops read documentation.
346  * offset: See the linux fops read documentation.
347  *
348  * Wrapper around the drm_read function that makes sure the device is
349  * processing the fifo if drm_read decides to wait.
350  */
351 ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
352 		      size_t count, loff_t *offset)
353 {
354 	struct drm_file *file_priv = filp->private_data;
355 	struct vmw_private *dev_priv =
356 		vmw_priv(file_priv->minor->dev);
357 
358 	vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
359 	return drm_read(filp, buffer, count, offset);
360 }
361