vmwgfx_ioctl.c 12.2 KB
Newer Older
1 2
/**************************************************************************
 *
S
Sinclair Yeh 已提交
3
 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "vmwgfx_drv.h"
29
#include <drm/vmwgfx_drm.h>
30
#include "vmwgfx_kms.h"
31
#include "device_include/svga3d_caps.h"
32

33 34 35 36 37
struct svga_3d_compat_cap {
	SVGA3dCapsRecordHeader header;
	SVGA3dCapPair pairs[SVGA3D_DEVCAP_MAX];
};

38 39 40 41 42 43
int vmw_getparam_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct vmw_private *dev_priv = vmw_priv(dev);
	struct drm_vmw_getparam_arg *param =
	    (struct drm_vmw_getparam_arg *)data;
44
	struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
45 46 47 48 49 50 51 52 53

	switch (param->param) {
	case DRM_VMW_PARAM_NUM_STREAMS:
		param->value = vmw_overlay_num_overlays(dev_priv);
		break;
	case DRM_VMW_PARAM_NUM_FREE_STREAMS:
		param->value = vmw_overlay_num_free_overlays(dev_priv);
		break;
	case DRM_VMW_PARAM_3D:
54
		param->value = vmw_fifo_have_3d(dev_priv) ? 1 : 0;
55
		break;
56 57 58 59 60 61
	case DRM_VMW_PARAM_HW_CAPS:
		param->value = dev_priv->capabilities;
		break;
	case DRM_VMW_PARAM_FIFO_CAPS:
		param->value = dev_priv->fifo.capabilities;
		break;
62
	case DRM_VMW_PARAM_MAX_FB_SIZE:
63
		param->value = dev_priv->prim_bb_mem;
64
		break;
65 66
	case DRM_VMW_PARAM_FIFO_HW_VERSION:
	{
67
		u32 *fifo_mem = dev_priv->mmio_virt;
68
		const struct vmw_fifo_state *fifo = &dev_priv->fifo;
69

70 71 72 73 74
		if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS)) {
			param->value = SVGA3D_HWVERSION_WS8_B1;
			break;
		}

75
		param->value =
76 77 78 79 80
			vmw_mmio_read(fifo_mem +
				      ((fifo->capabilities &
					SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
				       SVGA_FIFO_3D_HWVERSION_REVISED :
				       SVGA_FIFO_3D_HWVERSION));
81 82
		break;
	}
83
	case DRM_VMW_PARAM_MAX_SURF_MEMORY:
84 85 86 87 88
		if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS) &&
		    !vmw_fp->gb_aware)
			param->value = dev_priv->max_mob_pages * PAGE_SIZE / 2;
		else
			param->value = dev_priv->memory_size;
89 90
		break;
	case DRM_VMW_PARAM_3D_CAPS_SIZE:
91 92 93 94 95 96
		if ((dev_priv->capabilities & SVGA_CAP_GBOBJECTS) &&
		    vmw_fp->gb_aware)
			param->value = SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
		else if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS)
			param->value = sizeof(struct svga_3d_compat_cap) +
				sizeof(uint32_t);
97 98
		else
			param->value = (SVGA_FIFO_3D_CAPS_LAST -
99 100
					SVGA_FIFO_3D_CAPS + 1) *
				sizeof(uint32_t);
101
		break;
102
	case DRM_VMW_PARAM_MAX_MOB_MEMORY:
103
		vmw_fp->gb_aware = true;
104 105
		param->value = dev_priv->max_mob_pages * PAGE_SIZE;
		break;
106 107 108
	case DRM_VMW_PARAM_MAX_MOB_SIZE:
		param->value = dev_priv->max_mob_size;
		break;
109 110 111 112
	case DRM_VMW_PARAM_SCREEN_TARGET:
		param->value =
			(dev_priv->active_display_unit == vmw_du_screen_target);
		break;
113 114 115
	case DRM_VMW_PARAM_DX:
		param->value = dev_priv->has_dx;
		break;
116 117 118 119 120 121
	default:
		return -EINVAL;
	}

	return 0;
}
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
static u32 vmw_mask_multisample(unsigned int cap, u32 fmt_value)
{
	/* If the header is updated, update the format test as well! */
	BUILD_BUG_ON(SVGA3D_DEVCAP_DXFMT_BC5_UNORM + 1 != SVGA3D_DEVCAP_MAX);

	if (cap >= SVGA3D_DEVCAP_DXFMT_X8R8G8B8 &&
	    cap <= SVGA3D_DEVCAP_DXFMT_BC5_UNORM)
		fmt_value &= ~(SVGADX_DXFMT_MULTISAMPLE_2 |
			       SVGADX_DXFMT_MULTISAMPLE_4 |
			       SVGADX_DXFMT_MULTISAMPLE_8);
	else if (cap == SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES)
		return 0;

	return fmt_value;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static int vmw_fill_compat_cap(struct vmw_private *dev_priv, void *bounce,
			       size_t size)
{
	struct svga_3d_compat_cap *compat_cap =
		(struct svga_3d_compat_cap *) bounce;
	unsigned int i;
	size_t pair_offset = offsetof(struct svga_3d_compat_cap, pairs);
	unsigned int max_size;

	if (size < pair_offset)
		return -EINVAL;

	max_size = (size - pair_offset) / sizeof(SVGA3dCapPair);

	if (max_size > SVGA3D_DEVCAP_MAX)
		max_size = SVGA3D_DEVCAP_MAX;

	compat_cap->header.length =
		(pair_offset + max_size * sizeof(SVGA3dCapPair)) / sizeof(u32);
	compat_cap->header.type = SVGA3DCAPS_RECORD_DEVCAPS;

160
	spin_lock(&dev_priv->cap_lock);
161 162 163
	for (i = 0; i < max_size; ++i) {
		vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
		compat_cap->pairs[i][0] = i;
164 165
		compat_cap->pairs[i][1] = vmw_mask_multisample
			(i, vmw_read(dev_priv, SVGA_REG_DEV_CAP));
166
	}
167
	spin_unlock(&dev_priv->cap_lock);
168 169 170 171

	return 0;
}

172 173 174 175 176 177 178 179

int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
			 struct drm_file *file_priv)
{
	struct drm_vmw_get_3d_cap_arg *arg =
		(struct drm_vmw_get_3d_cap_arg *) data;
	struct vmw_private *dev_priv = vmw_priv(dev);
	uint32_t size;
180
	u32 *fifo_mem;
181 182 183
	void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
	void *bounce;
	int ret;
184
	bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
185
	struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
186

187
	if (unlikely(arg->pad64 != 0 || arg->max_size == 0)) {
188 189 190 191
		DRM_ERROR("Illegal GET_3D_CAP argument.\n");
		return -EINVAL;
	}

192 193 194 195
	if (gb_objects && vmw_fp->gb_aware)
		size = SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
	else if (gb_objects)
		size = sizeof(struct svga_3d_compat_cap) + sizeof(uint32_t);
196
	else
197 198
		size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) *
			sizeof(uint32_t);
199 200 201 202

	if (arg->max_size < size)
		size = arg->max_size;

203
	bounce = vzalloc(size);
204 205 206 207 208
	if (unlikely(bounce == NULL)) {
		DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
		return -ENOMEM;
	}

209 210
	if (gb_objects && vmw_fp->gb_aware) {
		int i, num;
211 212
		uint32_t *bounce32 = (uint32_t *) bounce;

213
		num = size / sizeof(uint32_t);
214 215
		if (num > SVGA3D_DEVCAP_MAX)
			num = SVGA3D_DEVCAP_MAX;
216

217
		spin_lock(&dev_priv->cap_lock);
218
		for (i = 0; i < num; ++i) {
219
			vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
220 221
			*bounce32++ = vmw_mask_multisample
				(i, vmw_read(dev_priv, SVGA_REG_DEV_CAP));
222
		}
223
		spin_unlock(&dev_priv->cap_lock);
224 225 226 227
	} else if (gb_objects) {
		ret = vmw_fill_compat_cap(dev_priv, bounce, size);
		if (unlikely(ret != 0))
			goto out_err;
228 229
	} else {
		fifo_mem = dev_priv->mmio_virt;
230
		memcpy(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
231
	}
232 233

	ret = copy_to_user(buffer, bounce, size);
234 235
	if (ret)
		ret = -EFAULT;
236
out_err:
237 238 239 240 241 242 243
	vfree(bounce);

	if (unlikely(ret != 0))
		DRM_ERROR("Failed to report 3D caps info.\n");

	return ret;
}
244 245 246 247 248 249 250 251 252 253 254

int vmw_present_ioctl(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
{
	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
	struct vmw_private *dev_priv = vmw_priv(dev);
	struct drm_vmw_present_arg *arg =
		(struct drm_vmw_present_arg *)data;
	struct vmw_surface *surface;
	struct drm_vmw_rect __user *clips_ptr;
	struct drm_vmw_rect *clips = NULL;
255
	struct drm_framebuffer *fb;
256
	struct vmw_framebuffer *vfb;
257
	struct vmw_resource *res;
258 259 260 261
	uint32_t num_clips;
	int ret;

	num_clips = arg->num_clips;
262
	clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr;
263 264 265 266 267 268 269 270 271 272

	if (unlikely(num_clips == 0))
		return 0;

	if (clips_ptr == NULL) {
		DRM_ERROR("Variable clips_ptr must be specified.\n");
		ret = -EINVAL;
		goto out_clips;
	}

273
	clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
274 275 276 277 278 279 280 281 282
	if (clips == NULL) {
		DRM_ERROR("Failed to allocate clip rect list.\n");
		ret = -ENOMEM;
		goto out_clips;
	}

	ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
	if (ret) {
		DRM_ERROR("Failed to copy clip rects from userspace.\n");
283
		ret = -EFAULT;
284 285 286
		goto out_no_copy;
	}

287
	drm_modeset_lock_all(dev);
288

289 290
	fb = drm_framebuffer_lookup(dev, arg->fb_id);
	if (!fb) {
291
		DRM_ERROR("Invalid framebuffer id.\n");
292
		ret = -ENOENT;
293 294
		goto out_no_fb;
	}
295
	vfb = vmw_framebuffer_to_vfb(fb);
296

297
	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
298 299 300
	if (unlikely(ret != 0))
		goto out_no_ttm_lock;

301 302 303
	ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
					      user_surface_converter,
					      &res);
304 305 306
	if (ret)
		goto out_no_surface;

307
	surface = vmw_res_to_srf(res);
308 309 310 311 312 313 314 315 316
	ret = vmw_kms_present(dev_priv, file_priv,
			      vfb, surface, arg->sid,
			      arg->dest_x, arg->dest_y,
			      clips, num_clips);

	/* vmw_user_surface_lookup takes one ref so does new_fb */
	vmw_surface_unreference(&surface);

out_no_surface:
317
	ttm_read_unlock(&dev_priv->reservation_sem);
318
out_no_ttm_lock:
319
	drm_framebuffer_unreference(fb);
320
out_no_fb:
321
	drm_modeset_unlock_all(dev);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
out_no_copy:
	kfree(clips);
out_clips:
	return ret;
}

int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
			       struct drm_file *file_priv)
{
	struct vmw_private *dev_priv = vmw_priv(dev);
	struct drm_vmw_present_readback_arg *arg =
		(struct drm_vmw_present_readback_arg *)data;
	struct drm_vmw_fence_rep __user *user_fence_rep =
		(struct drm_vmw_fence_rep __user *)
		(unsigned long)arg->fence_rep;
	struct drm_vmw_rect __user *clips_ptr;
	struct drm_vmw_rect *clips = NULL;
339
	struct drm_framebuffer *fb;
340 341 342 343 344
	struct vmw_framebuffer *vfb;
	uint32_t num_clips;
	int ret;

	num_clips = arg->num_clips;
345
	clips_ptr = (struct drm_vmw_rect __user *)(unsigned long)arg->clips_ptr;
346 347 348 349 350 351 352 353 354 355

	if (unlikely(num_clips == 0))
		return 0;

	if (clips_ptr == NULL) {
		DRM_ERROR("Argument clips_ptr must be specified.\n");
		ret = -EINVAL;
		goto out_clips;
	}

356
	clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
357 358 359 360 361 362 363 364 365
	if (clips == NULL) {
		DRM_ERROR("Failed to allocate clip rect list.\n");
		ret = -ENOMEM;
		goto out_clips;
	}

	ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
	if (ret) {
		DRM_ERROR("Failed to copy clip rects from userspace.\n");
366
		ret = -EFAULT;
367 368 369
		goto out_no_copy;
	}

370
	drm_modeset_lock_all(dev);
371

372 373
	fb = drm_framebuffer_lookup(dev, arg->fb_id);
	if (!fb) {
374
		DRM_ERROR("Invalid framebuffer id.\n");
375
		ret = -ENOENT;
376 377 378
		goto out_no_fb;
	}

379
	vfb = vmw_framebuffer_to_vfb(fb);
380 381 382
	if (!vfb->dmabuf) {
		DRM_ERROR("Framebuffer not dmabuf backed.\n");
		ret = -EINVAL;
383
		goto out_no_ttm_lock;
384 385
	}

386
	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
387 388 389 390 391 392 393
	if (unlikely(ret != 0))
		goto out_no_ttm_lock;

	ret = vmw_kms_readback(dev_priv, file_priv,
			       vfb, user_fence_rep,
			       clips, num_clips);

394
	ttm_read_unlock(&dev_priv->reservation_sem);
395
out_no_ttm_lock:
396
	drm_framebuffer_unreference(fb);
397
out_no_fb:
398
	drm_modeset_unlock_all(dev);
399 400 401 402 403
out_no_copy:
	kfree(clips);
out_clips:
	return ret;
}
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446


/**
 * vmw_fops_poll - wrapper around the drm_poll function
 *
 * @filp: See the linux fops poll documentation.
 * @wait: See the linux fops poll documentation.
 *
 * Wrapper around the drm_poll function that makes sure the device is
 * processing the fifo if drm_poll decides to wait.
 */
unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
{
	struct drm_file *file_priv = filp->private_data;
	struct vmw_private *dev_priv =
		vmw_priv(file_priv->minor->dev);

	vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
	return drm_poll(filp, wait);
}


/**
 * vmw_fops_read - wrapper around the drm_read function
 *
 * @filp: See the linux fops read documentation.
 * @buffer: See the linux fops read documentation.
 * @count: See the linux fops read documentation.
 * offset: See the linux fops read documentation.
 *
 * Wrapper around the drm_read function that makes sure the device is
 * processing the fifo if drm_read decides to wait.
 */
ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
		      size_t count, loff_t *offset)
{
	struct drm_file *file_priv = filp->private_data;
	struct vmw_private *dev_priv =
		vmw_priv(file_priv->minor->dev);

	vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
	return drm_read(filp, buffer, count, offset);
}