Home | History | Annotate | Line # | Download | only in qxl
      1 /*	$NetBSD: qxl_ioctl.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2013 Red Hat Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  * Authors: Dave Airlie
     25  *          Alon Levy
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: qxl_ioctl.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $");
     30 
     31 #include <linux/pci.h>
     32 #include <linux/uaccess.h>
     33 
     34 #include "qxl_drv.h"
     35 #include "qxl_object.h"
     36 
     37 /*
     38  * TODO: allocating a new gem(in qxl_bo) for each request.
     39  * This is wasteful since bo's are page aligned.
     40  */
     41 static int qxl_alloc_ioctl(struct drm_device *dev, void *data,
     42 			   struct drm_file *file_priv)
     43 {
     44 	struct qxl_device *qdev = dev->dev_private;
     45 	struct drm_qxl_alloc *qxl_alloc = data;
     46 	int ret;
     47 	struct qxl_bo *qobj;
     48 	uint32_t handle;
     49 	u32 domain = QXL_GEM_DOMAIN_VRAM;
     50 
     51 	if (qxl_alloc->size == 0) {
     52 		DRM_ERROR("invalid size %d\n", qxl_alloc->size);
     53 		return -EINVAL;
     54 	}
     55 	ret = qxl_gem_object_create_with_handle(qdev, file_priv,
     56 						domain,
     57 						qxl_alloc->size,
     58 						NULL,
     59 						&qobj, &handle);
     60 	if (ret) {
     61 		DRM_ERROR("%s: failed to create gem ret=%d\n",
     62 			  __func__, ret);
     63 		return -ENOMEM;
     64 	}
     65 	qxl_alloc->handle = handle;
     66 	return 0;
     67 }
     68 
     69 static int qxl_map_ioctl(struct drm_device *dev, void *data,
     70 			 struct drm_file *file_priv)
     71 {
     72 	struct qxl_device *qdev = dev->dev_private;
     73 	struct drm_qxl_map *qxl_map = data;
     74 
     75 	return qxl_mode_dumb_mmap(file_priv, &qdev->ddev, qxl_map->handle,
     76 				  &qxl_map->offset);
     77 }
     78 
     79 struct qxl_reloc_info {
     80 	int type;
     81 	struct qxl_bo *dst_bo;
     82 	uint32_t dst_offset;
     83 	struct qxl_bo *src_bo;
     84 	int src_offset;
     85 };
     86 
     87 /*
     88  * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
     89  * are on vram).
     90  * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
     91  */
     92 static void
     93 apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
     94 {
     95 	void *reloc_page;
     96 
     97 	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
     98 	*(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
     99 											      info->src_bo,
    100 											      info->src_offset);
    101 	qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
    102 }
    103 
    104 static void
    105 apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
    106 {
    107 	uint32_t id = 0;
    108 	void *reloc_page;
    109 
    110 	if (info->src_bo && !info->src_bo->is_primary)
    111 		id = info->src_bo->surface_id;
    112 
    113 	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
    114 	*(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id;
    115 	qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
    116 }
    117 
    118 /* return holding the reference to this object */
    119 static int qxlhw_handle_to_bo(struct drm_file *file_priv, uint64_t handle,
    120 			      struct qxl_release *release, struct qxl_bo **qbo_p)
    121 {
    122 	struct drm_gem_object *gobj;
    123 	struct qxl_bo *qobj;
    124 	int ret;
    125 
    126 	gobj = drm_gem_object_lookup(file_priv, handle);
    127 	if (!gobj)
    128 		return -EINVAL;
    129 
    130 	qobj = gem_to_qxl_bo(gobj);
    131 
    132 	ret = qxl_release_list_add(release, qobj);
    133 	drm_gem_object_put_unlocked(gobj);
    134 	if (ret)
    135 		return ret;
    136 
    137 	*qbo_p = qobj;
    138 	return 0;
    139 }
    140 
    141 /*
    142  * Usage of execbuffer:
    143  * Relocations need to take into account the full QXLDrawable size.
    144  * However, the command as passed from user space must *not* contain the initial
    145  * QXLReleaseInfo struct (first XXX bytes)
    146  */
    147 static int qxl_process_single_command(struct qxl_device *qdev,
    148 				      struct drm_qxl_command *cmd,
    149 				      struct drm_file *file_priv)
    150 {
    151 	struct qxl_reloc_info *reloc_info;
    152 	int release_type;
    153 	struct qxl_release *release;
    154 	struct qxl_bo *cmd_bo;
    155 	void *fb_cmd;
    156 	int i, ret, num_relocs;
    157 	int unwritten;
    158 
    159 	switch (cmd->type) {
    160 	case QXL_CMD_DRAW:
    161 		release_type = QXL_RELEASE_DRAWABLE;
    162 		break;
    163 	case QXL_CMD_SURFACE:
    164 	case QXL_CMD_CURSOR:
    165 	default:
    166 		DRM_DEBUG("Only draw commands in execbuffers\n");
    167 		return -EINVAL;
    168 		break;
    169 	}
    170 
    171 	if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
    172 		return -EINVAL;
    173 
    174 	if (!access_ok(u64_to_user_ptr(cmd->command),
    175 		       cmd->command_size))
    176 		return -EFAULT;
    177 
    178 	reloc_info = kmalloc_array(cmd->relocs_num,
    179 				   sizeof(struct qxl_reloc_info), GFP_KERNEL);
    180 	if (!reloc_info)
    181 		return -ENOMEM;
    182 
    183 	ret = qxl_alloc_release_reserved(qdev,
    184 					 sizeof(union qxl_release_info) +
    185 					 cmd->command_size,
    186 					 release_type,
    187 					 &release,
    188 					 &cmd_bo);
    189 	if (ret)
    190 		goto out_free_reloc;
    191 
    192 	/* TODO copy slow path code from i915 */
    193 	fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_MASK));
    194 	unwritten = __copy_from_user_inatomic_nocache
    195 		(fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_MASK),
    196 		 u64_to_user_ptr(cmd->command), cmd->command_size);
    197 
    198 	{
    199 		struct qxl_drawable *draw = fb_cmd;
    200 
    201 		draw->mm_time = qdev->rom->mm_clock;
    202 	}
    203 
    204 	qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
    205 	if (unwritten) {
    206 		DRM_ERROR("got unwritten %d\n", unwritten);
    207 		ret = -EFAULT;
    208 		goto out_free_release;
    209 	}
    210 
    211 	/* fill out reloc info structs */
    212 	num_relocs = 0;
    213 	for (i = 0; i < cmd->relocs_num; ++i) {
    214 		struct drm_qxl_reloc reloc;
    215 		struct drm_qxl_reloc __user *u = u64_to_user_ptr(cmd->relocs);
    216 
    217 		if (copy_from_user(&reloc, u + i, sizeof(reloc))) {
    218 			ret = -EFAULT;
    219 			goto out_free_bos;
    220 		}
    221 
    222 		/* add the bos to the list of bos to validate -
    223 		   need to validate first then process relocs? */
    224 		if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) {
    225 			DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type);
    226 
    227 			ret = -EINVAL;
    228 			goto out_free_bos;
    229 		}
    230 		reloc_info[i].type = reloc.reloc_type;
    231 
    232 		if (reloc.dst_handle) {
    233 			ret = qxlhw_handle_to_bo(file_priv, reloc.dst_handle, release,
    234 						 &reloc_info[i].dst_bo);
    235 			if (ret)
    236 				goto out_free_bos;
    237 			reloc_info[i].dst_offset = reloc.dst_offset;
    238 		} else {
    239 			reloc_info[i].dst_bo = cmd_bo;
    240 			reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset;
    241 		}
    242 		num_relocs++;
    243 
    244 		/* reserve and validate the reloc dst bo */
    245 		if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) {
    246 			ret = qxlhw_handle_to_bo(file_priv, reloc.src_handle, release,
    247 						 &reloc_info[i].src_bo);
    248 			if (ret)
    249 				goto out_free_bos;
    250 			reloc_info[i].src_offset = reloc.src_offset;
    251 		} else {
    252 			reloc_info[i].src_bo = NULL;
    253 			reloc_info[i].src_offset = 0;
    254 		}
    255 	}
    256 
    257 	/* validate all buffers */
    258 	ret = qxl_release_reserve_list(release, false);
    259 	if (ret)
    260 		goto out_free_bos;
    261 
    262 	for (i = 0; i < cmd->relocs_num; ++i) {
    263 		if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
    264 			apply_reloc(qdev, &reloc_info[i]);
    265 		else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
    266 			apply_surf_reloc(qdev, &reloc_info[i]);
    267 	}
    268 
    269 	ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
    270 	if (ret)
    271 		qxl_release_backoff_reserve_list(release);
    272 	else
    273 		qxl_release_fence_buffer_objects(release);
    274 
    275 out_free_bos:
    276 out_free_release:
    277 	if (ret)
    278 		qxl_release_free(qdev, release);
    279 out_free_reloc:
    280 	kfree(reloc_info);
    281 	return ret;
    282 }
    283 
    284 static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data,
    285 				struct drm_file *file_priv)
    286 {
    287 	struct qxl_device *qdev = dev->dev_private;
    288 	struct drm_qxl_execbuffer *execbuffer = data;
    289 	struct drm_qxl_command user_cmd;
    290 	int cmd_num;
    291 	int ret;
    292 
    293 	for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
    294 
    295 		struct drm_qxl_command __user *commands =
    296 			u64_to_user_ptr(execbuffer->commands);
    297 
    298 		if (copy_from_user(&user_cmd, commands + cmd_num,
    299 				       sizeof(user_cmd)))
    300 			return -EFAULT;
    301 
    302 		ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
    303 		if (ret)
    304 			return ret;
    305 	}
    306 	return 0;
    307 }
    308 
    309 static int qxl_update_area_ioctl(struct drm_device *dev, void *data,
    310 				 struct drm_file *file)
    311 {
    312 	struct qxl_device *qdev = dev->dev_private;
    313 	struct drm_qxl_update_area *update_area = data;
    314 	struct qxl_rect area = {.left = update_area->left,
    315 				.top = update_area->top,
    316 				.right = update_area->right,
    317 				.bottom = update_area->bottom};
    318 	int ret;
    319 	struct drm_gem_object *gobj = NULL;
    320 	struct qxl_bo *qobj = NULL;
    321 	struct ttm_operation_ctx ctx = { true, false };
    322 
    323 	if (update_area->left >= update_area->right ||
    324 	    update_area->top >= update_area->bottom)
    325 		return -EINVAL;
    326 
    327 	gobj = drm_gem_object_lookup(file, update_area->handle);
    328 	if (gobj == NULL)
    329 		return -ENOENT;
    330 
    331 	qobj = gem_to_qxl_bo(gobj);
    332 
    333 	ret = qxl_bo_reserve(qobj, false);
    334 	if (ret)
    335 		goto out;
    336 
    337 	if (!qobj->pin_count) {
    338 		qxl_ttm_placement_from_domain(qobj, qobj->type, false);
    339 		ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
    340 		if (unlikely(ret))
    341 			goto out;
    342 	}
    343 
    344 	ret = qxl_bo_check_id(qdev, qobj);
    345 	if (ret)
    346 		goto out2;
    347 	if (!qobj->surface_id)
    348 		DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
    349 	ret = qxl_io_update_area(qdev, qobj, &area);
    350 
    351 out2:
    352 	qxl_bo_unreserve(qobj);
    353 
    354 out:
    355 	drm_gem_object_put_unlocked(gobj);
    356 	return ret;
    357 }
    358 
    359 static int qxl_getparam_ioctl(struct drm_device *dev, void *data,
    360 		       struct drm_file *file_priv)
    361 {
    362 	struct qxl_device *qdev = dev->dev_private;
    363 	struct drm_qxl_getparam *param = data;
    364 
    365 	switch (param->param) {
    366 	case QXL_PARAM_NUM_SURFACES:
    367 		param->value = qdev->rom->n_surfaces;
    368 		break;
    369 	case QXL_PARAM_MAX_RELOCS:
    370 		param->value = QXL_MAX_RES;
    371 		break;
    372 	default:
    373 		return -EINVAL;
    374 	}
    375 	return 0;
    376 }
    377 
    378 static int qxl_clientcap_ioctl(struct drm_device *dev, void *data,
    379 				  struct drm_file *file_priv)
    380 {
    381 	struct qxl_device *qdev = dev->dev_private;
    382 	struct drm_qxl_clientcap *param = data;
    383 	int byte, idx;
    384 
    385 	byte = param->index / 8;
    386 	idx = param->index % 8;
    387 
    388 	if (dev->pdev->revision < 4)
    389 		return -ENOSYS;
    390 
    391 	if (byte >= 58)
    392 		return -ENOSYS;
    393 
    394 	if (qdev->rom->client_capabilities[byte] & (1 << idx))
    395 		return 0;
    396 	return -ENOSYS;
    397 }
    398 
    399 static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data,
    400 				struct drm_file *file)
    401 {
    402 	struct qxl_device *qdev = dev->dev_private;
    403 	struct drm_qxl_alloc_surf *param = data;
    404 	struct qxl_bo *qobj;
    405 	int handle;
    406 	int ret;
    407 	int size, actual_stride;
    408 	struct qxl_surface surf;
    409 
    410 	/* work out size allocate bo with handle */
    411 	actual_stride = param->stride < 0 ? -param->stride : param->stride;
    412 	size = actual_stride * param->height + actual_stride;
    413 
    414 	surf.format = param->format;
    415 	surf.width = param->width;
    416 	surf.height = param->height;
    417 	surf.stride = param->stride;
    418 	surf.data = 0;
    419 
    420 	ret = qxl_gem_object_create_with_handle(qdev, file,
    421 						QXL_GEM_DOMAIN_SURFACE,
    422 						size,
    423 						&surf,
    424 						&qobj, &handle);
    425 	if (ret) {
    426 		DRM_ERROR("%s: failed to create gem ret=%d\n",
    427 			  __func__, ret);
    428 		return -ENOMEM;
    429 	} else
    430 		param->handle = handle;
    431 	return ret;
    432 }
    433 
    434 const struct drm_ioctl_desc qxl_ioctls[] = {
    435 	DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
    436 
    437 	DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
    438 
    439 	DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl,
    440 							DRM_AUTH),
    441 	DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl,
    442 							DRM_AUTH),
    443 	DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl,
    444 							DRM_AUTH),
    445 	DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl,
    446 							DRM_AUTH),
    447 
    448 	DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl,
    449 			  DRM_AUTH),
    450 };
    451 
    452 int qxl_max_ioctls = ARRAY_SIZE(qxl_ioctls);
    453