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