qxl_ioctl.c 11.6 KB
Newer Older
D
Dave Airlie 已提交
1 2 3 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 29 30 31 32
/*
 * Copyright 2013 Red Hat Inc.
 *
 * 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, sublicense,
 * 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
 *
 * Authors: Dave Airlie
 *          Alon Levy
 */

#include "qxl_drv.h"
#include "qxl_object.h"

/*
 * TODO: allocating a new gem(in qxl_bo) for each request.
 * This is wasteful since bo's are page aligned.
 */
33 34
static int qxl_alloc_ioctl(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
D
Dave Airlie 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_alloc *qxl_alloc = data;
	int ret;
	struct qxl_bo *qobj;
	uint32_t handle;
	u32 domain = QXL_GEM_DOMAIN_VRAM;

	if (qxl_alloc->size == 0) {
		DRM_ERROR("invalid size %d\n", qxl_alloc->size);
		return -EINVAL;
	}
	ret = qxl_gem_object_create_with_handle(qdev, file_priv,
						domain,
						qxl_alloc->size,
						NULL,
						&qobj, &handle);
	if (ret) {
		DRM_ERROR("%s: failed to create gem ret=%d\n",
			  __func__, ret);
		return -ENOMEM;
	}
	qxl_alloc->handle = handle;
	return 0;
}

61 62
static int qxl_map_ioctl(struct drm_device *dev, void *data,
			 struct drm_file *file_priv)
D
Dave Airlie 已提交
63 64 65 66
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_map *qxl_map = data;

67
	return qxl_mode_dumb_mmap(file_priv, &qdev->ddev, qxl_map->handle,
D
Dave Airlie 已提交
68 69 70
				  &qxl_map->offset);
}

71 72 73 74 75 76 77 78
struct qxl_reloc_info {
	int type;
	struct qxl_bo *dst_bo;
	uint32_t dst_offset;
	struct qxl_bo *src_bo;
	int src_offset;
};

D
Dave Airlie 已提交
79 80 81 82 83 84
/*
 * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
 * are on vram).
 * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
 */
static void
85
apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
D
Dave Airlie 已提交
86 87
{
	void *reloc_page;
88

89 90 91 92 93
	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
	*(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
											      info->src_bo,
											      info->src_offset);
	qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
D
Dave Airlie 已提交
94 95 96
}

static void
97
apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
D
Dave Airlie 已提交
98 99 100 101
{
	uint32_t id = 0;
	void *reloc_page;

102 103
	if (info->src_bo && !info->src_bo->is_primary)
		id = info->src_bo->surface_id;
D
Dave Airlie 已提交
104

105 106 107
	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
	*(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id;
	qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
D
Dave Airlie 已提交
108 109 110
}

/* return holding the reference to this object */
111
static int qxlhw_handle_to_bo(struct drm_file *file_priv, uint64_t handle,
112
			      struct qxl_release *release, struct qxl_bo **qbo_p)
D
Dave Airlie 已提交
113 114 115 116 117
{
	struct drm_gem_object *gobj;
	struct qxl_bo *qobj;
	int ret;

118
	gobj = drm_gem_object_lookup(file_priv, handle);
119
	if (!gobj)
120
		return -EINVAL;
121

D
Dave Airlie 已提交
122 123
	qobj = gem_to_qxl_bo(gobj);

124
	ret = qxl_release_list_add(release, qobj);
125
	drm_gem_object_put_unlocked(gobj);
126
	if (ret)
127
		return ret;
D
Dave Airlie 已提交
128

129 130
	*qbo_p = qobj;
	return 0;
D
Dave Airlie 已提交
131 132 133 134 135 136 137 138
}

/*
 * Usage of execbuffer:
 * Relocations need to take into account the full QXLDrawable size.
 * However, the command as passed from user space must *not* contain the initial
 * QXLReleaseInfo struct (first XXX bytes)
 */
139 140 141
static int qxl_process_single_command(struct qxl_device *qdev,
				      struct drm_qxl_command *cmd,
				      struct drm_file *file_priv)
D
Dave Airlie 已提交
142
{
143 144 145 146
	struct qxl_reloc_info *reloc_info;
	int release_type;
	struct qxl_release *release;
	struct qxl_bo *cmd_bo;
D
Dave Airlie 已提交
147
	void *fb_cmd;
148
	int i, ret, num_relocs;
D
Dave Airlie 已提交
149 150
	int unwritten;

151 152 153 154 155 156 157 158 159 160 161
	switch (cmd->type) {
	case QXL_CMD_DRAW:
		release_type = QXL_RELEASE_DRAWABLE;
		break;
	case QXL_CMD_SURFACE:
	case QXL_CMD_CURSOR:
	default:
		DRM_DEBUG("Only draw commands in execbuffers\n");
		return -EINVAL;
		break;
	}
D
Dave Airlie 已提交
162

163 164
	if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
		return -EINVAL;
D
Dave Airlie 已提交
165

166
	if (!access_ok(u64_to_user_ptr(cmd->command),
167 168
		       cmd->command_size))
		return -EFAULT;
D
Dave Airlie 已提交
169

170 171
	reloc_info = kmalloc_array(cmd->relocs_num,
				   sizeof(struct qxl_reloc_info), GFP_KERNEL);
172 173
	if (!reloc_info)
		return -ENOMEM;
174

175 176 177 178 179 180 181 182
	ret = qxl_alloc_release_reserved(qdev,
					 sizeof(union qxl_release_info) +
					 cmd->command_size,
					 release_type,
					 &release,
					 &cmd_bo);
	if (ret)
		goto out_free_reloc;
D
Dave Airlie 已提交
183

184
	/* TODO copy slow path code from i915 */
G
Gerd Hoffmann 已提交
185
	fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_MASK));
G
Gerd Hoffmann 已提交
186
	unwritten = __copy_from_user_inatomic_nocache
G
Gerd Hoffmann 已提交
187
		(fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_MASK),
G
Gerd Hoffmann 已提交
188
		 u64_to_user_ptr(cmd->command), cmd->command_size);
189

190 191
	{
		struct qxl_drawable *draw = fb_cmd;
192

193 194
		draw->mm_time = qdev->rom->mm_clock;
	}
195

196 197 198 199 200 201 202 203 204 205 206
	qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
	if (unwritten) {
		DRM_ERROR("got unwritten %d\n", unwritten);
		ret = -EFAULT;
		goto out_free_release;
	}

	/* fill out reloc info structs */
	num_relocs = 0;
	for (i = 0; i < cmd->relocs_num; ++i) {
		struct drm_qxl_reloc reloc;
G
Gerd Hoffmann 已提交
207
		struct drm_qxl_reloc __user *u = u64_to_user_ptr(cmd->relocs);
208

G
Gerd Hoffmann 已提交
209
		if (copy_from_user(&reloc, u + i, sizeof(reloc))) {
210 211
			ret = -EFAULT;
			goto out_free_bos;
D
Dave Airlie 已提交
212 213
		}

214 215 216
		/* add the bos to the list of bos to validate -
		   need to validate first then process relocs? */
		if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) {
217
			DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type);
D
Dave Airlie 已提交
218

219 220 221 222 223 224
			ret = -EINVAL;
			goto out_free_bos;
		}
		reloc_info[i].type = reloc.reloc_type;

		if (reloc.dst_handle) {
225
			ret = qxlhw_handle_to_bo(file_priv, reloc.dst_handle, release,
226 227
						 &reloc_info[i].dst_bo);
			if (ret)
228 229 230 231 232 233 234 235 236
				goto out_free_bos;
			reloc_info[i].dst_offset = reloc.dst_offset;
		} else {
			reloc_info[i].dst_bo = cmd_bo;
			reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset;
		}
		num_relocs++;

		/* reserve and validate the reloc dst bo */
237
		if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) {
238
			ret = qxlhw_handle_to_bo(file_priv, reloc.src_handle, release,
239 240
						 &reloc_info[i].src_bo);
			if (ret)
241 242 243 244 245 246 247
				goto out_free_bos;
			reloc_info[i].src_offset = reloc.src_offset;
		} else {
			reloc_info[i].src_bo = NULL;
			reloc_info[i].src_offset = 0;
		}
	}
D
Dave Airlie 已提交
248

249 250 251 252
	/* validate all buffers */
	ret = qxl_release_reserve_list(release, false);
	if (ret)
		goto out_free_bos;
D
Dave Airlie 已提交
253

254 255 256 257 258 259
	for (i = 0; i < cmd->relocs_num; ++i) {
		if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
			apply_reloc(qdev, &reloc_info[i]);
		else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
			apply_surf_reloc(qdev, &reloc_info[i]);
	}
D
Dave Airlie 已提交
260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
	if (ret)
		qxl_release_backoff_reserve_list(release);
	else
		qxl_release_fence_buffer_objects(release);

out_free_bos:
out_free_release:
	if (ret)
		qxl_release_free(qdev, release);
out_free_reloc:
	kfree(reloc_info);
	return ret;
}

static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data,
				struct drm_file *file_priv)
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_execbuffer *execbuffer = data;
	struct drm_qxl_command user_cmd;
	int cmd_num;
	int ret;

	for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
D
Dave Airlie 已提交
286

G
Gerd Hoffmann 已提交
287 288
		struct drm_qxl_command __user *commands =
			u64_to_user_ptr(execbuffer->commands);
289

G
Gerd Hoffmann 已提交
290
		if (copy_from_user(&user_cmd, commands + cmd_num,
291 292 293 294 295
				       sizeof(user_cmd)))
			return -EFAULT;

		ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
		if (ret)
D
Dave Airlie 已提交
296 297 298 299 300
			return ret;
	}
	return 0;
}

301 302
static int qxl_update_area_ioctl(struct drm_device *dev, void *data,
				 struct drm_file *file)
D
Dave Airlie 已提交
303 304 305 306 307 308 309 310 311 312
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_update_area *update_area = data;
	struct qxl_rect area = {.left = update_area->left,
				.top = update_area->top,
				.right = update_area->right,
				.bottom = update_area->bottom};
	int ret;
	struct drm_gem_object *gobj = NULL;
	struct qxl_bo *qobj = NULL;
313
	struct ttm_operation_ctx ctx = { true, false };
D
Dave Airlie 已提交
314 315 316 317 318

	if (update_area->left >= update_area->right ||
	    update_area->top >= update_area->bottom)
		return -EINVAL;

319
	gobj = drm_gem_object_lookup(file, update_area->handle);
D
Dave Airlie 已提交
320 321 322 323 324 325 326 327 328 329
	if (gobj == NULL)
		return -ENOENT;

	qobj = gem_to_qxl_bo(gobj);

	ret = qxl_bo_reserve(qobj, false);
	if (ret)
		goto out;

	if (!qobj->pin_count) {
330
		qxl_ttm_placement_from_domain(qobj, qobj->type, false);
331
		ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
D
Dave Airlie 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		if (unlikely(ret))
			goto out;
	}

	ret = qxl_bo_check_id(qdev, qobj);
	if (ret)
		goto out2;
	if (!qobj->surface_id)
		DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
	ret = qxl_io_update_area(qdev, qobj, &area);

out2:
	qxl_bo_unreserve(qobj);

out:
347
	drm_gem_object_put_unlocked(gobj);
D
Dave Airlie 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	return ret;
}

static int qxl_getparam_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_getparam *param = data;

	switch (param->param) {
	case QXL_PARAM_NUM_SURFACES:
		param->value = qdev->rom->n_surfaces;
		break;
	case QXL_PARAM_MAX_RELOCS:
		param->value = QXL_MAX_RES;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

static int qxl_clientcap_ioctl(struct drm_device *dev, void *data,
				  struct drm_file *file_priv)
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_clientcap *param = data;
	int byte, idx;

	byte = param->index / 8;
	idx = param->index % 8;

380
	if (dev->pdev->revision < 4)
D
Dave Airlie 已提交
381 382
		return -ENOSYS;

D
Dave Airlie 已提交
383
	if (byte >= 58)
D
Dave Airlie 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		return -ENOSYS;

	if (qdev->rom->client_capabilities[byte] & (1 << idx))
		return 0;
	return -ENOSYS;
}

static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data,
				struct drm_file *file)
{
	struct qxl_device *qdev = dev->dev_private;
	struct drm_qxl_alloc_surf *param = data;
	struct qxl_bo *qobj;
	int handle;
	int ret;
	int size, actual_stride;
	struct qxl_surface surf;

	/* work out size allocate bo with handle */
	actual_stride = param->stride < 0 ? -param->stride : param->stride;
	size = actual_stride * param->height + actual_stride;

	surf.format = param->format;
	surf.width = param->width;
	surf.height = param->height;
	surf.stride = param->stride;
	surf.data = 0;

	ret = qxl_gem_object_create_with_handle(qdev, file,
						QXL_GEM_DOMAIN_SURFACE,
						size,
						&surf,
						&qobj, &handle);
	if (ret) {
		DRM_ERROR("%s: failed to create gem ret=%d\n",
			  __func__, ret);
		return -ENOMEM;
	} else
		param->handle = handle;
	return ret;
}

R
Rob Clark 已提交
426
const struct drm_ioctl_desc qxl_ioctls[] = {
427
	DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
D
Dave Airlie 已提交
428

429
	DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
D
Dave Airlie 已提交
430 431

	DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl,
432
							DRM_AUTH),
D
Dave Airlie 已提交
433
	DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl,
434
							DRM_AUTH),
D
Dave Airlie 已提交
435
	DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl,
436
							DRM_AUTH),
D
Dave Airlie 已提交
437
	DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl,
438
							DRM_AUTH),
D
Dave Airlie 已提交
439 440

	DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl,
441
			  DRM_AUTH),
D
Dave Airlie 已提交
442 443
};

444
int qxl_max_ioctls = ARRAY_SIZE(qxl_ioctls);