Linux Audio

Check our new training course

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