xref: /linux/drivers/gpu/drm/msm/msm_gem_submit.c (revision 4db102dcb0396a4ccf89b1eac0f4eb3fd167a080)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27198e6b0SRob Clark /*
37198e6b0SRob Clark  * Copyright (C) 2013 Red Hat
47198e6b0SRob Clark  * Author: Rob Clark <robdclark@gmail.com>
57198e6b0SRob Clark  */
67198e6b0SRob Clark 
7feea39a8SSam Ravnborg #include <linux/file.h>
8f0a42bb5SRob Clark #include <linux/sync_file.h>
9feea39a8SSam Ravnborg #include <linux/uaccess.h>
10feea39a8SSam Ravnborg 
11ab723b7aSBas Nieuwenhuizen #include <drm/drm_drv.h>
12feea39a8SSam Ravnborg #include <drm/drm_file.h>
13ab723b7aSBas Nieuwenhuizen #include <drm/drm_syncobj.h>
14f0a42bb5SRob Clark 
157198e6b0SRob Clark #include "msm_drv.h"
167198e6b0SRob Clark #include "msm_gpu.h"
177198e6b0SRob Clark #include "msm_gem.h"
184241db42SJordan Crouse #include "msm_gpu_trace.h"
197198e6b0SRob Clark 
20b2acb89aSRob Clark /* For userspace errors, use DRM_UT_DRIVER.. so that userspace can enable
21b2acb89aSRob Clark  * error msgs for debugging, but we don't spam dmesg by default
22b2acb89aSRob Clark  */
23b2acb89aSRob Clark #define SUBMIT_ERROR(submit, fmt, ...) \
24b2acb89aSRob Clark 	DRM_DEV_DEBUG_DRIVER((submit)->dev->dev, fmt, ##__VA_ARGS__)
25b2acb89aSRob Clark 
267198e6b0SRob Clark /*
277198e6b0SRob Clark  * Cmdstream submission:
287198e6b0SRob Clark  */
297198e6b0SRob Clark 
submit_create(struct drm_device * dev,struct msm_gpu * gpu,struct msm_gpu_submitqueue * queue,uint32_t nr_bos,uint32_t nr_cmds)307198e6b0SRob Clark static struct msm_gem_submit *submit_create(struct drm_device *dev,
31cf655d61SJordan Crouse 		struct msm_gpu *gpu,
32295b22aeSJordan Crouse 		struct msm_gpu_submitqueue *queue, uint32_t nr_bos,
33295b22aeSJordan Crouse 		uint32_t nr_cmds)
347198e6b0SRob Clark {
35fc1dd04cSRob Clark 	static atomic_t ident = ATOMIC_INIT(0);
367198e6b0SRob Clark 	struct msm_gem_submit *submit;
3779341eb7SRob Clark 	uint64_t sz;
381d8a5ca4SRob Clark 	int ret;
3979341eb7SRob Clark 
4079341eb7SRob Clark 	sz = struct_size(submit, bos, nr_bos) +
4165e93108SDan Carpenter 			((u64)nr_cmds * sizeof(submit->cmd[0]));
424a630fadSKasin Li 
434a630fadSKasin Li 	if (sz > SIZE_MAX)
4479341eb7SRob Clark 		return ERR_PTR(-ENOMEM);
457198e6b0SRob Clark 
466c15808dSRob Clark 	submit = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
476860b56cSRob Clark 	if (!submit)
4879341eb7SRob Clark 		return ERR_PTR(-ENOMEM);
496860b56cSRob Clark 
50f94e6a51SRob Clark 	submit->hw_fence = msm_fence_alloc();
51f94e6a51SRob Clark 	if (IS_ERR(submit->hw_fence)) {
52f94e6a51SRob Clark 		ret = PTR_ERR(submit->hw_fence);
53f94e6a51SRob Clark 		kfree(submit);
54f94e6a51SRob Clark 		return ERR_PTR(ret);
55f94e6a51SRob Clark 	}
56f94e6a51SRob Clark 
57a78422e9SDanilo Krummrich 	ret = drm_sched_job_init(&submit->base, queue->entity, 1, queue);
581d8a5ca4SRob Clark 	if (ret) {
59f94e6a51SRob Clark 		kfree(submit->hw_fence);
601d8a5ca4SRob Clark 		kfree(submit);
611d8a5ca4SRob Clark 		return ERR_PTR(ret);
621d8a5ca4SRob Clark 	}
631d8a5ca4SRob Clark 
64964d2f97SRob Clark 	kref_init(&submit->ref);
657198e6b0SRob Clark 	submit->dev = dev;
66cf655d61SJordan Crouse 	submit->aspace = queue->ctx->aspace;
677198e6b0SRob Clark 	submit->gpu = gpu;
686b597ce2SRob Clark 	submit->cmd = (void *)&submit->bos[nr_bos];
69f7de1545SJordan Crouse 	submit->queue = queue;
70fc1dd04cSRob Clark 	submit->pid = get_pid(task_pid(current));
71fc40e5e1SRob Clark 	submit->ring = gpu->rb[queue->ring_nr];
72e25e92e0SRob Clark 	submit->fault_dumped = false;
737198e6b0SRob Clark 
74fc1dd04cSRob Clark 	/* Get a unique identifier for the submission for logging purposes */
75fc1dd04cSRob Clark 	submit->ident = atomic_inc_return(&ident) - 1;
76fc1dd04cSRob Clark 
77ba344afdSRob Clark 	INIT_LIST_HEAD(&submit->node);
787198e6b0SRob Clark 
797198e6b0SRob Clark 	return submit;
807198e6b0SRob Clark }
817198e6b0SRob Clark 
__msm_gem_submit_destroy(struct kref * kref)82964d2f97SRob Clark void __msm_gem_submit_destroy(struct kref *kref)
8340e6815bSRob Clark {
84964d2f97SRob Clark 	struct msm_gem_submit *submit =
85964d2f97SRob Clark 			container_of(kref, struct msm_gem_submit, ref);
8620224d71SRob Clark 	unsigned i;
8720224d71SRob Clark 
88a61acbbeSRob Clark 	if (submit->fence_id) {
89e4f020c6SRob Clark 		spin_lock(&submit->queue->idr_lock);
90a61acbbeSRob Clark 		idr_remove(&submit->queue->fence_idr, submit->fence_id);
91e4f020c6SRob Clark 		spin_unlock(&submit->queue->idr_lock);
92a61acbbeSRob Clark 	}
93a61acbbeSRob Clark 
941d8a5ca4SRob Clark 	dma_fence_put(submit->user_fence);
951cd0787fSRob Clark 
961cd0787fSRob Clark 	/*
971cd0787fSRob Clark 	 * If the submit is freed before msm_job_run(), then hw_fence is
981cd0787fSRob Clark 	 * just some pre-allocated memory, not a reference counted fence.
991cd0787fSRob Clark 	 * Once the job runs and the hw_fence is initialized, it will
1001cd0787fSRob Clark 	 * have a refcount of at least one, since the submit holds a ref
1011cd0787fSRob Clark 	 * to the hw_fence.
1021cd0787fSRob Clark 	 */
1031cd0787fSRob Clark 	if (kref_read(&submit->hw_fence->refcount) == 0) {
1041cd0787fSRob Clark 		kfree(submit->hw_fence);
1051cd0787fSRob Clark 	} else {
1061d8a5ca4SRob Clark 		dma_fence_put(submit->hw_fence);
1071cd0787fSRob Clark 	}
108a61acbbeSRob Clark 
1094816b626SRob Clark 	put_pid(submit->pid);
110f7de1545SJordan Crouse 	msm_submitqueue_put(submit->queue);
111f7de1545SJordan Crouse 
11220224d71SRob Clark 	for (i = 0; i < submit->nr_cmds; i++)
11320224d71SRob Clark 		kfree(submit->cmd[i].relocs);
11420224d71SRob Clark 
11540e6815bSRob Clark 	kfree(submit);
11640e6815bSRob Clark }
11740e6815bSRob Clark 
submit_lookup_objects(struct msm_gem_submit * submit,struct drm_msm_gem_submit * args,struct drm_file * file)1187198e6b0SRob Clark static int submit_lookup_objects(struct msm_gem_submit *submit,
1197198e6b0SRob Clark 		struct drm_msm_gem_submit *args, struct drm_file *file)
1207198e6b0SRob Clark {
1217198e6b0SRob Clark 	unsigned i;
1227198e6b0SRob Clark 	int ret = 0;
1237198e6b0SRob Clark 
1247198e6b0SRob Clark 	for (i = 0; i < args->nr_bos; i++) {
1257198e6b0SRob Clark 		struct drm_msm_gem_submit_bo submit_bo;
1267198e6b0SRob Clark 		void __user *userptr =
1273ed605bcSGustavo Padovan 			u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
1287198e6b0SRob Clark 
129a9e26cabSRob Clark 		/* make sure we don't have garbage flags, in case we hit
130a9e26cabSRob Clark 		 * error path before flags is initialized:
131a9e26cabSRob Clark 		 */
132a9e26cabSRob Clark 		submit->bos[i].flags = 0;
133a9e26cabSRob Clark 
13421c42da1SDan Carpenter 		if (copy_from_user(&submit_bo, userptr, sizeof(submit_bo))) {
13521c42da1SDan Carpenter 			ret = -EFAULT;
136b673499aSKristian H. Kristensen 			i = 0;
13789f82cbbSRob Clark 			goto out;
13821c42da1SDan Carpenter 		}
1397198e6b0SRob Clark 
1407a93d5c3SRob Clark /* at least one of READ and/or WRITE flags should be set: */
1417a93d5c3SRob Clark #define MANDATORY_FLAGS (MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_WRITE)
1427a93d5c3SRob Clark 
143a6cb3b86SJordan Crouse 		if ((submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) ||
1447a93d5c3SRob Clark 			!(submit_bo.flags & MANDATORY_FLAGS)) {
145b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "invalid flags: %x\n", submit_bo.flags);
1467198e6b0SRob Clark 			ret = -EINVAL;
147b673499aSKristian H. Kristensen 			i = 0;
148b673499aSKristian H. Kristensen 			goto out;
1497198e6b0SRob Clark 		}
1507198e6b0SRob Clark 
151b673499aSKristian H. Kristensen 		submit->bos[i].handle = submit_bo.handle;
1527198e6b0SRob Clark 		submit->bos[i].flags = submit_bo.flags;
153b673499aSKristian H. Kristensen 	}
154b673499aSKristian H. Kristensen 
155b673499aSKristian H. Kristensen 	spin_lock(&file->table_lock);
156b673499aSKristian H. Kristensen 
157b673499aSKristian H. Kristensen 	for (i = 0; i < args->nr_bos; i++) {
158b673499aSKristian H. Kristensen 		struct drm_gem_object *obj;
1597198e6b0SRob Clark 
1607198e6b0SRob Clark 		/* normally use drm_gem_object_lookup(), but for bulk lookup
1617198e6b0SRob Clark 		 * all under single table_lock just hit object_idr directly:
1627198e6b0SRob Clark 		 */
163b673499aSKristian H. Kristensen 		obj = idr_find(&file->object_idr, submit->bos[i].handle);
1647198e6b0SRob Clark 		if (!obj) {
165b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "invalid handle %u at index %u\n", submit->bos[i].handle, i);
1667198e6b0SRob Clark 			ret = -EINVAL;
1677198e6b0SRob Clark 			goto out_unlock;
1687198e6b0SRob Clark 		}
1697198e6b0SRob Clark 
17064686886SThomas Zimmermann 		drm_gem_object_get(obj);
1717198e6b0SRob Clark 
1726ba5daa5SRob Clark 		submit->bos[i].obj = obj;
1737198e6b0SRob Clark 	}
1747198e6b0SRob Clark 
1757198e6b0SRob Clark out_unlock:
1767198e6b0SRob Clark 	spin_unlock(&file->table_lock);
1777198e6b0SRob Clark 
17889f82cbbSRob Clark out:
17989f82cbbSRob Clark 	submit->nr_bos = i;
18089f82cbbSRob Clark 
1817198e6b0SRob Clark 	return ret;
1827198e6b0SRob Clark }
1837198e6b0SRob Clark 
submit_lookup_cmds(struct msm_gem_submit * submit,struct drm_msm_gem_submit * args,struct drm_file * file)18420224d71SRob Clark static int submit_lookup_cmds(struct msm_gem_submit *submit,
18520224d71SRob Clark 		struct drm_msm_gem_submit *args, struct drm_file *file)
18620224d71SRob Clark {
18795c58291SArnd Bergmann 	unsigned i;
18895c58291SArnd Bergmann 	size_t sz;
18920224d71SRob Clark 	int ret = 0;
19020224d71SRob Clark 
19120224d71SRob Clark 	for (i = 0; i < args->nr_cmds; i++) {
19220224d71SRob Clark 		struct drm_msm_gem_submit_cmd submit_cmd;
19320224d71SRob Clark 		void __user *userptr =
19420224d71SRob Clark 			u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
19520224d71SRob Clark 
19620224d71SRob Clark 		ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
19720224d71SRob Clark 		if (ret) {
19820224d71SRob Clark 			ret = -EFAULT;
19920224d71SRob Clark 			goto out;
20020224d71SRob Clark 		}
20120224d71SRob Clark 
20220224d71SRob Clark 		/* validate input from userspace: */
20320224d71SRob Clark 		switch (submit_cmd.type) {
20420224d71SRob Clark 		case MSM_SUBMIT_CMD_BUF:
20520224d71SRob Clark 		case MSM_SUBMIT_CMD_IB_TARGET_BUF:
20620224d71SRob Clark 		case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
20720224d71SRob Clark 			break;
20820224d71SRob Clark 		default:
209b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "invalid type: %08x\n", submit_cmd.type);
21020224d71SRob Clark 			return -EINVAL;
21120224d71SRob Clark 		}
21220224d71SRob Clark 
21320224d71SRob Clark 		if (submit_cmd.size % 4) {
214b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "non-aligned cmdstream buffer size: %u\n",
21520224d71SRob Clark 				     submit_cmd.size);
21620224d71SRob Clark 			ret = -EINVAL;
21720224d71SRob Clark 			goto out;
21820224d71SRob Clark 		}
21920224d71SRob Clark 
22020224d71SRob Clark 		submit->cmd[i].type = submit_cmd.type;
22120224d71SRob Clark 		submit->cmd[i].size = submit_cmd.size / 4;
22220224d71SRob Clark 		submit->cmd[i].offset = submit_cmd.submit_offset / 4;
22320224d71SRob Clark 		submit->cmd[i].idx  = submit_cmd.submit_idx;
22420224d71SRob Clark 		submit->cmd[i].nr_relocs = submit_cmd.nr_relocs;
22520224d71SRob Clark 
226c8d99bb9SRob Clark 		userptr = u64_to_user_ptr(submit_cmd.relocs);
227c8d99bb9SRob Clark 
22820224d71SRob Clark 		sz = array_size(submit_cmd.nr_relocs,
22920224d71SRob Clark 				sizeof(struct drm_msm_gem_submit_reloc));
23020224d71SRob Clark 		/* check for overflow: */
23120224d71SRob Clark 		if (sz == SIZE_MAX) {
23220224d71SRob Clark 			ret = -ENOMEM;
23320224d71SRob Clark 			goto out;
23420224d71SRob Clark 		}
2356c15808dSRob Clark 		submit->cmd[i].relocs = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN);
236d839f081SJiasheng Jiang 		if (!submit->cmd[i].relocs) {
237d839f081SJiasheng Jiang 			ret = -ENOMEM;
238d839f081SJiasheng Jiang 			goto out;
239d839f081SJiasheng Jiang 		}
24020224d71SRob Clark 		ret = copy_from_user(submit->cmd[i].relocs, userptr, sz);
24120224d71SRob Clark 		if (ret) {
24220224d71SRob Clark 			ret = -EFAULT;
24320224d71SRob Clark 			goto out;
24420224d71SRob Clark 		}
24520224d71SRob Clark 	}
24620224d71SRob Clark 
24720224d71SRob Clark out:
24820224d71SRob Clark 	return ret;
24920224d71SRob Clark }
25020224d71SRob Clark 
2517198e6b0SRob Clark /* This is where we make sure all the bo's are reserved and pin'd: */
submit_lock_objects(struct msm_gem_submit * submit)252340faef2SRob Clark static int submit_lock_objects(struct msm_gem_submit *submit)
2537198e6b0SRob Clark {
254*a6397e63SRob Clark 	int ret;
2557198e6b0SRob Clark 
256*a6397e63SRob Clark 	drm_exec_init(&submit->exec, DRM_EXEC_INTERRUPTIBLE_WAIT, submit->nr_bos);
257*a6397e63SRob Clark 
258*a6397e63SRob Clark 	drm_exec_until_all_locked (&submit->exec) {
259*a6397e63SRob Clark 		for (unsigned i = 0; i < submit->nr_bos; i++) {
2606ba5daa5SRob Clark 			struct drm_gem_object *obj = submit->bos[i].obj;
261*a6397e63SRob Clark 			ret = drm_exec_prepare_obj(&submit->exec, obj, 1);
262*a6397e63SRob Clark 			drm_exec_retry_on_contention(&submit->exec);
2637198e6b0SRob Clark 			if (ret)
264*a6397e63SRob Clark 				goto error;
2657198e6b0SRob Clark 		}
2667198e6b0SRob Clark 	}
2677198e6b0SRob Clark 
2687198e6b0SRob Clark 	return 0;
2697198e6b0SRob Clark 
270*a6397e63SRob Clark error:
2717198e6b0SRob Clark 	return ret;
2727198e6b0SRob Clark }
2737198e6b0SRob Clark 
submit_fence_sync(struct msm_gem_submit * submit)274*a6397e63SRob Clark static int submit_fence_sync(struct msm_gem_submit *submit)
275b6295f9aSRob Clark {
276b6295f9aSRob Clark 	int i, ret = 0;
277b6295f9aSRob Clark 
278b6295f9aSRob Clark 	for (i = 0; i < submit->nr_bos; i++) {
2796ba5daa5SRob Clark 		struct drm_gem_object *obj = submit->bos[i].obj;
280b6295f9aSRob Clark 		bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
281b6295f9aSRob Clark 
282b1bf64f8SRob Clark 		/* Otherwise userspace can ask for implicit sync to be
283b1bf64f8SRob Clark 		 * disabled on specific buffers.  This is useful for internal
284b1bf64f8SRob Clark 		 * usermode driver managed buffers, suballocation, etc.
285b1bf64f8SRob Clark 		 */
286b1bf64f8SRob Clark 		if (submit->bos[i].flags & MSM_SUBMIT_BO_NO_IMPLICIT)
287b1bf64f8SRob Clark 			continue;
288b1bf64f8SRob Clark 
28980bcfbd3SDaniel Vetter 		ret = drm_sched_job_add_implicit_dependencies(&submit->base,
29080bcfbd3SDaniel Vetter 							      obj,
291f97decacSJordan Crouse 							      write);
292b6295f9aSRob Clark 		if (ret)
293b6295f9aSRob Clark 			break;
294b6295f9aSRob Clark 	}
295b6295f9aSRob Clark 
296b6295f9aSRob Clark 	return ret;
297b6295f9aSRob Clark }
298b6295f9aSRob Clark 
submit_pin_objects(struct msm_gem_submit * submit)299340faef2SRob Clark static int submit_pin_objects(struct msm_gem_submit *submit)
300340faef2SRob Clark {
301fc896cf3SRob Clark 	struct msm_drm_private *priv = submit->dev->dev_private;
302340faef2SRob Clark 	int i, ret = 0;
303340faef2SRob Clark 
304340faef2SRob Clark 	for (i = 0; i < submit->nr_bos; i++) {
3056ba5daa5SRob Clark 		struct drm_gem_object *obj = submit->bos[i].obj;
30627674c66SRob Clark 		struct msm_gem_vma *vma;
307340faef2SRob Clark 
308340faef2SRob Clark 		/* if locking succeeded, pin bo: */
30927674c66SRob Clark 		vma = msm_gem_get_vma_locked(obj, submit->aspace);
31027674c66SRob Clark 		if (IS_ERR(vma)) {
31127674c66SRob Clark 			ret = PTR_ERR(vma);
31227674c66SRob Clark 			break;
31327674c66SRob Clark 		}
314340faef2SRob Clark 
31527674c66SRob Clark 		ret = msm_gem_pin_vma_locked(obj, vma);
316340faef2SRob Clark 		if (ret)
317340faef2SRob Clark 			break;
318340faef2SRob Clark 
31927674c66SRob Clark 		submit->bos[i].iova = vma->iova;
320340faef2SRob Clark 	}
321340faef2SRob Clark 
322fc896cf3SRob Clark 	/*
323fc896cf3SRob Clark 	 * A second loop while holding the LRU lock (a) avoids acquiring/dropping
324fc896cf3SRob Clark 	 * the LRU lock for each individual bo, while (b) avoiding holding the
325fc896cf3SRob Clark 	 * LRU lock while calling msm_gem_pin_vma_locked() (which could trigger
326fc896cf3SRob Clark 	 * get_pages() which could trigger reclaim.. and if we held the LRU lock
327fc896cf3SRob Clark 	 * could trigger deadlock with the shrinker).
328fc896cf3SRob Clark 	 */
329fc896cf3SRob Clark 	mutex_lock(&priv->lru.lock);
330fc896cf3SRob Clark 	for (i = 0; i < submit->nr_bos; i++) {
331fc896cf3SRob Clark 		msm_gem_pin_obj_locked(submit->bos[i].obj);
332fc896cf3SRob Clark 	}
333fc896cf3SRob Clark 	mutex_unlock(&priv->lru.lock);
334fc896cf3SRob Clark 
3352d7d2c4eSRob Clark 	submit->bos_pinned = true;
3362d7d2c4eSRob Clark 
337340faef2SRob Clark 	return ret;
338340faef2SRob Clark }
339340faef2SRob Clark 
submit_unpin_objects(struct msm_gem_submit * submit)3402d7d2c4eSRob Clark static void submit_unpin_objects(struct msm_gem_submit *submit)
3412d7d2c4eSRob Clark {
3422d7d2c4eSRob Clark 	if (!submit->bos_pinned)
3432d7d2c4eSRob Clark 		return;
3442d7d2c4eSRob Clark 
3452d7d2c4eSRob Clark 	for (int i = 0; i < submit->nr_bos; i++) {
3462d7d2c4eSRob Clark 		struct drm_gem_object *obj = submit->bos[i].obj;
3472d7d2c4eSRob Clark 
3482d7d2c4eSRob Clark 		msm_gem_unpin_locked(obj);
3492d7d2c4eSRob Clark 	}
3502d7d2c4eSRob Clark 
3512d7d2c4eSRob Clark 	submit->bos_pinned = false;
3522d7d2c4eSRob Clark }
3532d7d2c4eSRob Clark 
submit_attach_object_fences(struct msm_gem_submit * submit)354be40596bSRob Clark static void submit_attach_object_fences(struct msm_gem_submit *submit)
355be40596bSRob Clark {
356be40596bSRob Clark 	int i;
357be40596bSRob Clark 
358be40596bSRob Clark 	for (i = 0; i < submit->nr_bos; i++) {
3596ba5daa5SRob Clark 		struct drm_gem_object *obj = submit->bos[i].obj;
360be40596bSRob Clark 
361be40596bSRob Clark 		if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
36273511edfSChristian König 			dma_resv_add_fence(obj->resv, submit->user_fence,
36373511edfSChristian König 					   DMA_RESV_USAGE_WRITE);
364be40596bSRob Clark 		else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
36573511edfSChristian König 			dma_resv_add_fence(obj->resv, submit->user_fence,
36673511edfSChristian König 					   DMA_RESV_USAGE_READ);
367be40596bSRob Clark 	}
368be40596bSRob Clark }
369be40596bSRob Clark 
submit_bo(struct msm_gem_submit * submit,uint32_t idx,struct drm_gem_object ** obj,uint64_t * iova)3707198e6b0SRob Clark static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
371a3dec9cdSRob Clark 		struct drm_gem_object **obj, uint64_t *iova)
3727198e6b0SRob Clark {
3737198e6b0SRob Clark 	if (idx >= submit->nr_bos) {
374b2acb89aSRob Clark 		SUBMIT_ERROR(submit, "invalid buffer index: %u (out of %u)\n",
37519872533SRob Clark 			     idx, submit->nr_bos);
37619872533SRob Clark 		return -EINVAL;
3777198e6b0SRob Clark 	}
3787198e6b0SRob Clark 
3797198e6b0SRob Clark 	if (obj)
3807198e6b0SRob Clark 		*obj = submit->bos[idx].obj;
3817198e6b0SRob Clark 	if (iova)
3827198e6b0SRob Clark 		*iova = submit->bos[idx].iova;
3837198e6b0SRob Clark 
3847198e6b0SRob Clark 	return 0;
3857198e6b0SRob Clark }
3867198e6b0SRob Clark 
3877198e6b0SRob Clark /* process the reloc's and patch up the cmdstream as needed: */
submit_reloc(struct msm_gem_submit * submit,struct drm_gem_object * obj,uint32_t offset,uint32_t nr_relocs,struct drm_msm_gem_submit_reloc * relocs)3886ba5daa5SRob Clark static int submit_reloc(struct msm_gem_submit *submit, struct drm_gem_object *obj,
38920224d71SRob Clark 		uint32_t offset, uint32_t nr_relocs, struct drm_msm_gem_submit_reloc *relocs)
3907198e6b0SRob Clark {
3917198e6b0SRob Clark 	uint32_t i, last_offset = 0;
3927198e6b0SRob Clark 	uint32_t *ptr;
3936490abc4SJordan Crouse 	int ret = 0;
3947198e6b0SRob Clark 
3957198e6b0SRob Clark 	if (offset % 4) {
396b2acb89aSRob Clark 		SUBMIT_ERROR(submit, "non-aligned cmdstream buffer: %u\n", offset);
3977198e6b0SRob Clark 		return -EINVAL;
3987198e6b0SRob Clark 	}
3997198e6b0SRob Clark 
4007198e6b0SRob Clark 	/* For now, just map the entire thing.  Eventually we probably
4017198e6b0SRob Clark 	 * to do it page-by-page, w/ kmap() if not vmap()d..
4027198e6b0SRob Clark 	 */
4036ba5daa5SRob Clark 	ptr = msm_gem_get_vaddr_locked(obj);
4047198e6b0SRob Clark 
4057198e6b0SRob Clark 	if (IS_ERR(ptr)) {
4067198e6b0SRob Clark 		ret = PTR_ERR(ptr);
4077198e6b0SRob Clark 		DBG("failed to map: %d", ret);
4087198e6b0SRob Clark 		return ret;
4097198e6b0SRob Clark 	}
4107198e6b0SRob Clark 
4117198e6b0SRob Clark 	for (i = 0; i < nr_relocs; i++) {
41220224d71SRob Clark 		struct drm_msm_gem_submit_reloc submit_reloc = relocs[i];
41378babc16SRob Clark 		uint32_t off;
41478babc16SRob Clark 		uint64_t iova;
4157198e6b0SRob Clark 
4167198e6b0SRob Clark 		if (submit_reloc.submit_offset % 4) {
417b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "non-aligned reloc offset: %u\n",
4187198e6b0SRob Clark 				     submit_reloc.submit_offset);
4196490abc4SJordan Crouse 			ret = -EINVAL;
4206490abc4SJordan Crouse 			goto out;
4217198e6b0SRob Clark 		}
4227198e6b0SRob Clark 
4237198e6b0SRob Clark 		/* offset in dwords: */
4247198e6b0SRob Clark 		off = submit_reloc.submit_offset / 4;
4257198e6b0SRob Clark 
4266ba5daa5SRob Clark 		if ((off >= (obj->size / 4)) ||
4277198e6b0SRob Clark 				(off < last_offset)) {
428b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "invalid offset %u at reloc %u\n", off, i);
4296490abc4SJordan Crouse 			ret = -EINVAL;
4306490abc4SJordan Crouse 			goto out;
4317198e6b0SRob Clark 		}
4327198e6b0SRob Clark 
433a3dec9cdSRob Clark 		ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova);
4347198e6b0SRob Clark 		if (ret)
4356490abc4SJordan Crouse 			goto out;
4367198e6b0SRob Clark 
4377198e6b0SRob Clark 		iova += submit_reloc.reloc_offset;
4387198e6b0SRob Clark 
4397198e6b0SRob Clark 		if (submit_reloc.shift < 0)
4407198e6b0SRob Clark 			iova >>= -submit_reloc.shift;
4417198e6b0SRob Clark 		else
4427198e6b0SRob Clark 			iova <<= submit_reloc.shift;
4437198e6b0SRob Clark 
4447198e6b0SRob Clark 		ptr[off] = iova | submit_reloc.or;
4457198e6b0SRob Clark 
4467198e6b0SRob Clark 		last_offset = off;
4477198e6b0SRob Clark 	}
4487198e6b0SRob Clark 
4496490abc4SJordan Crouse out:
4506ba5daa5SRob Clark 	msm_gem_put_vaddr_locked(obj);
45118f23049SRob Clark 
4526490abc4SJordan Crouse 	return ret;
4537198e6b0SRob Clark }
4547198e6b0SRob Clark 
455be40596bSRob Clark /* Cleanup submit at end of ioctl.  In the error case, this also drops
456be40596bSRob Clark  * references, unpins, and drops active refcnt.  In the non-error case,
457be40596bSRob Clark  * this is done when the submit is retired.
458be40596bSRob Clark  */
submit_cleanup(struct msm_gem_submit * submit,bool error)459be40596bSRob Clark static void submit_cleanup(struct msm_gem_submit *submit, bool error)
4607198e6b0SRob Clark {
461*a6397e63SRob Clark 	if (error) {
4622d7d2c4eSRob Clark 		submit_unpin_objects(submit);
463*a6397e63SRob Clark 		/* job wasn't enqueued to scheduler, so early retirement: */
464*a6397e63SRob Clark 		msm_submit_retire(submit);
4657198e6b0SRob Clark 	}
466*a6397e63SRob Clark 
467*a6397e63SRob Clark 	if (submit->exec.objects)
468*a6397e63SRob Clark 		drm_exec_fini(&submit->exec);
4697198e6b0SRob Clark }
4707198e6b0SRob Clark 
msm_submit_retire(struct msm_gem_submit * submit)471be40596bSRob Clark void msm_submit_retire(struct msm_gem_submit *submit)
472be40596bSRob Clark {
473be40596bSRob Clark 	int i;
474be40596bSRob Clark 
475be40596bSRob Clark 	for (i = 0; i < submit->nr_bos; i++) {
4766ba5daa5SRob Clark 		struct drm_gem_object *obj = submit->bos[i].obj;
477be40596bSRob Clark 
478be40596bSRob Clark 		drm_gem_object_put(obj);
479be40596bSRob Clark 	}
480be40596bSRob Clark }
481ab723b7aSBas Nieuwenhuizen 
482ab723b7aSBas Nieuwenhuizen struct msm_submit_post_dep {
483ab723b7aSBas Nieuwenhuizen 	struct drm_syncobj *syncobj;
484ab723b7aSBas Nieuwenhuizen 	uint64_t point;
485ab723b7aSBas Nieuwenhuizen 	struct dma_fence_chain *chain;
486ab723b7aSBas Nieuwenhuizen };
487ab723b7aSBas Nieuwenhuizen 
msm_parse_deps(struct msm_gem_submit * submit,struct drm_file * file,uint64_t in_syncobjs_addr,uint32_t nr_in_syncobjs,size_t syncobj_stride)4881d8a5ca4SRob Clark static struct drm_syncobj **msm_parse_deps(struct msm_gem_submit *submit,
489ab723b7aSBas Nieuwenhuizen                                            struct drm_file *file,
490ab723b7aSBas Nieuwenhuizen                                            uint64_t in_syncobjs_addr,
491ab723b7aSBas Nieuwenhuizen                                            uint32_t nr_in_syncobjs,
49263545526SDmitry Baryshkov                                            size_t syncobj_stride)
493ab723b7aSBas Nieuwenhuizen {
494ab723b7aSBas Nieuwenhuizen 	struct drm_syncobj **syncobjs = NULL;
495ab723b7aSBas Nieuwenhuizen 	struct drm_msm_gem_submit_syncobj syncobj_desc = {0};
496ab723b7aSBas Nieuwenhuizen 	int ret = 0;
497ab723b7aSBas Nieuwenhuizen 	uint32_t i, j;
498ab723b7aSBas Nieuwenhuizen 
499ab723b7aSBas Nieuwenhuizen 	syncobjs = kcalloc(nr_in_syncobjs, sizeof(*syncobjs),
500ab723b7aSBas Nieuwenhuizen 	                   GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
501ab723b7aSBas Nieuwenhuizen 	if (!syncobjs)
502ab723b7aSBas Nieuwenhuizen 		return ERR_PTR(-ENOMEM);
503ab723b7aSBas Nieuwenhuizen 
504ab723b7aSBas Nieuwenhuizen 	for (i = 0; i < nr_in_syncobjs; ++i) {
505ab723b7aSBas Nieuwenhuizen 		uint64_t address = in_syncobjs_addr + i * syncobj_stride;
506ab723b7aSBas Nieuwenhuizen 
507ab723b7aSBas Nieuwenhuizen 		if (copy_from_user(&syncobj_desc,
508ab723b7aSBas Nieuwenhuizen 			           u64_to_user_ptr(address),
509ab723b7aSBas Nieuwenhuizen 			           min(syncobj_stride, sizeof(syncobj_desc)))) {
510ab723b7aSBas Nieuwenhuizen 			ret = -EFAULT;
511ab723b7aSBas Nieuwenhuizen 			break;
512ab723b7aSBas Nieuwenhuizen 		}
513ab723b7aSBas Nieuwenhuizen 
514ab723b7aSBas Nieuwenhuizen 		if (syncobj_desc.point &&
5151d8a5ca4SRob Clark 		    !drm_core_check_feature(submit->dev, DRIVER_SYNCOBJ_TIMELINE)) {
516ab723b7aSBas Nieuwenhuizen 			ret = -EOPNOTSUPP;
517ab723b7aSBas Nieuwenhuizen 			break;
518ab723b7aSBas Nieuwenhuizen 		}
519ab723b7aSBas Nieuwenhuizen 
520ab723b7aSBas Nieuwenhuizen 		if (syncobj_desc.flags & ~MSM_SUBMIT_SYNCOBJ_FLAGS) {
521ab723b7aSBas Nieuwenhuizen 			ret = -EINVAL;
522ab723b7aSBas Nieuwenhuizen 			break;
523ab723b7aSBas Nieuwenhuizen 		}
524ab723b7aSBas Nieuwenhuizen 
525b92e01b4SMaíra Canal 		ret = drm_sched_job_add_syncobj_dependency(&submit->base, file,
526b92e01b4SMaíra Canal 							   syncobj_desc.handle, syncobj_desc.point);
527ab723b7aSBas Nieuwenhuizen 		if (ret)
528ab723b7aSBas Nieuwenhuizen 			break;
529ab723b7aSBas Nieuwenhuizen 
530ab723b7aSBas Nieuwenhuizen 		if (syncobj_desc.flags & MSM_SUBMIT_SYNCOBJ_RESET) {
531ab723b7aSBas Nieuwenhuizen 			syncobjs[i] =
532ab723b7aSBas Nieuwenhuizen 				drm_syncobj_find(file, syncobj_desc.handle);
533ab723b7aSBas Nieuwenhuizen 			if (!syncobjs[i]) {
534ab723b7aSBas Nieuwenhuizen 				ret = -EINVAL;
535ab723b7aSBas Nieuwenhuizen 				break;
536ab723b7aSBas Nieuwenhuizen 			}
537ab723b7aSBas Nieuwenhuizen 		}
538ab723b7aSBas Nieuwenhuizen 	}
539ab723b7aSBas Nieuwenhuizen 
540ab723b7aSBas Nieuwenhuizen 	if (ret) {
541ab723b7aSBas Nieuwenhuizen 		for (j = 0; j <= i; ++j) {
542ab723b7aSBas Nieuwenhuizen 			if (syncobjs[j])
543ab723b7aSBas Nieuwenhuizen 				drm_syncobj_put(syncobjs[j]);
544ab723b7aSBas Nieuwenhuizen 		}
545ab723b7aSBas Nieuwenhuizen 		kfree(syncobjs);
546ab723b7aSBas Nieuwenhuizen 		return ERR_PTR(ret);
547ab723b7aSBas Nieuwenhuizen 	}
548ab723b7aSBas Nieuwenhuizen 	return syncobjs;
549ab723b7aSBas Nieuwenhuizen }
550ab723b7aSBas Nieuwenhuizen 
msm_reset_syncobjs(struct drm_syncobj ** syncobjs,uint32_t nr_syncobjs)551ab723b7aSBas Nieuwenhuizen static void msm_reset_syncobjs(struct drm_syncobj **syncobjs,
552ab723b7aSBas Nieuwenhuizen                                uint32_t nr_syncobjs)
553ab723b7aSBas Nieuwenhuizen {
554ab723b7aSBas Nieuwenhuizen 	uint32_t i;
555ab723b7aSBas Nieuwenhuizen 
556ab723b7aSBas Nieuwenhuizen 	for (i = 0; syncobjs && i < nr_syncobjs; ++i) {
557ab723b7aSBas Nieuwenhuizen 		if (syncobjs[i])
558ab723b7aSBas Nieuwenhuizen 			drm_syncobj_replace_fence(syncobjs[i], NULL);
559ab723b7aSBas Nieuwenhuizen 	}
560ab723b7aSBas Nieuwenhuizen }
561ab723b7aSBas Nieuwenhuizen 
msm_parse_post_deps(struct drm_device * dev,struct drm_file * file,uint64_t syncobjs_addr,uint32_t nr_syncobjs,size_t syncobj_stride)562ab723b7aSBas Nieuwenhuizen static struct msm_submit_post_dep *msm_parse_post_deps(struct drm_device *dev,
563ab723b7aSBas Nieuwenhuizen                                                        struct drm_file *file,
564ab723b7aSBas Nieuwenhuizen                                                        uint64_t syncobjs_addr,
565ab723b7aSBas Nieuwenhuizen                                                        uint32_t nr_syncobjs,
566ab723b7aSBas Nieuwenhuizen                                                        size_t syncobj_stride)
567ab723b7aSBas Nieuwenhuizen {
568ab723b7aSBas Nieuwenhuizen 	struct msm_submit_post_dep *post_deps;
569ab723b7aSBas Nieuwenhuizen 	struct drm_msm_gem_submit_syncobj syncobj_desc = {0};
570ab723b7aSBas Nieuwenhuizen 	int ret = 0;
571ab723b7aSBas Nieuwenhuizen 	uint32_t i, j;
572ab723b7aSBas Nieuwenhuizen 
5738a86f213SRob Clark 	post_deps = kcalloc(nr_syncobjs, sizeof(*post_deps),
574ab723b7aSBas Nieuwenhuizen 			    GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
575ab723b7aSBas Nieuwenhuizen 	if (!post_deps)
576ab723b7aSBas Nieuwenhuizen 		return ERR_PTR(-ENOMEM);
577ab723b7aSBas Nieuwenhuizen 
578ab723b7aSBas Nieuwenhuizen 	for (i = 0; i < nr_syncobjs; ++i) {
579ab723b7aSBas Nieuwenhuizen 		uint64_t address = syncobjs_addr + i * syncobj_stride;
580ab723b7aSBas Nieuwenhuizen 
581ab723b7aSBas Nieuwenhuizen 		if (copy_from_user(&syncobj_desc,
582ab723b7aSBas Nieuwenhuizen 			           u64_to_user_ptr(address),
583ab723b7aSBas Nieuwenhuizen 			           min(syncobj_stride, sizeof(syncobj_desc)))) {
584ab723b7aSBas Nieuwenhuizen 			ret = -EFAULT;
585ab723b7aSBas Nieuwenhuizen 			break;
586ab723b7aSBas Nieuwenhuizen 		}
587ab723b7aSBas Nieuwenhuizen 
588ab723b7aSBas Nieuwenhuizen 		post_deps[i].point = syncobj_desc.point;
589ab723b7aSBas Nieuwenhuizen 
590ab723b7aSBas Nieuwenhuizen 		if (syncobj_desc.flags) {
591ab723b7aSBas Nieuwenhuizen 			ret = -EINVAL;
592ab723b7aSBas Nieuwenhuizen 			break;
593ab723b7aSBas Nieuwenhuizen 		}
594ab723b7aSBas Nieuwenhuizen 
595ab723b7aSBas Nieuwenhuizen 		if (syncobj_desc.point) {
596ab723b7aSBas Nieuwenhuizen 			if (!drm_core_check_feature(dev,
597ab723b7aSBas Nieuwenhuizen 			                            DRIVER_SYNCOBJ_TIMELINE)) {
598ab723b7aSBas Nieuwenhuizen 				ret = -EOPNOTSUPP;
599ab723b7aSBas Nieuwenhuizen 				break;
600ab723b7aSBas Nieuwenhuizen 			}
601ab723b7aSBas Nieuwenhuizen 
602440d0f12SChristian König 			post_deps[i].chain = dma_fence_chain_alloc();
603ab723b7aSBas Nieuwenhuizen 			if (!post_deps[i].chain) {
604ab723b7aSBas Nieuwenhuizen 				ret = -ENOMEM;
605ab723b7aSBas Nieuwenhuizen 				break;
606ab723b7aSBas Nieuwenhuizen 			}
607ab723b7aSBas Nieuwenhuizen 		}
608ab723b7aSBas Nieuwenhuizen 
609ab723b7aSBas Nieuwenhuizen 		post_deps[i].syncobj =
610ab723b7aSBas Nieuwenhuizen 			drm_syncobj_find(file, syncobj_desc.handle);
611ab723b7aSBas Nieuwenhuizen 		if (!post_deps[i].syncobj) {
612ab723b7aSBas Nieuwenhuizen 			ret = -EINVAL;
613ab723b7aSBas Nieuwenhuizen 			break;
614ab723b7aSBas Nieuwenhuizen 		}
615ab723b7aSBas Nieuwenhuizen 	}
616ab723b7aSBas Nieuwenhuizen 
617ab723b7aSBas Nieuwenhuizen 	if (ret) {
618ab723b7aSBas Nieuwenhuizen 		for (j = 0; j <= i; ++j) {
619440d0f12SChristian König 			dma_fence_chain_free(post_deps[j].chain);
620ab723b7aSBas Nieuwenhuizen 			if (post_deps[j].syncobj)
621ab723b7aSBas Nieuwenhuizen 				drm_syncobj_put(post_deps[j].syncobj);
622ab723b7aSBas Nieuwenhuizen 		}
623ab723b7aSBas Nieuwenhuizen 
624ab723b7aSBas Nieuwenhuizen 		kfree(post_deps);
625ab723b7aSBas Nieuwenhuizen 		return ERR_PTR(ret);
626ab723b7aSBas Nieuwenhuizen 	}
627ab723b7aSBas Nieuwenhuizen 
628ab723b7aSBas Nieuwenhuizen 	return post_deps;
629ab723b7aSBas Nieuwenhuizen }
630ab723b7aSBas Nieuwenhuizen 
msm_process_post_deps(struct msm_submit_post_dep * post_deps,uint32_t count,struct dma_fence * fence)631ab723b7aSBas Nieuwenhuizen static void msm_process_post_deps(struct msm_submit_post_dep *post_deps,
632ab723b7aSBas Nieuwenhuizen                                   uint32_t count, struct dma_fence *fence)
633ab723b7aSBas Nieuwenhuizen {
634ab723b7aSBas Nieuwenhuizen 	uint32_t i;
635ab723b7aSBas Nieuwenhuizen 
636ab723b7aSBas Nieuwenhuizen 	for (i = 0; post_deps && i < count; ++i) {
637ab723b7aSBas Nieuwenhuizen 		if (post_deps[i].chain) {
638ab723b7aSBas Nieuwenhuizen 			drm_syncobj_add_point(post_deps[i].syncobj,
639ab723b7aSBas Nieuwenhuizen 			                      post_deps[i].chain,
640ab723b7aSBas Nieuwenhuizen 			                      fence, post_deps[i].point);
641ab723b7aSBas Nieuwenhuizen 			post_deps[i].chain = NULL;
642ab723b7aSBas Nieuwenhuizen 		} else {
643ab723b7aSBas Nieuwenhuizen 			drm_syncobj_replace_fence(post_deps[i].syncobj,
644ab723b7aSBas Nieuwenhuizen 			                          fence);
645ab723b7aSBas Nieuwenhuizen 		}
646ab723b7aSBas Nieuwenhuizen 	}
647ab723b7aSBas Nieuwenhuizen }
648ab723b7aSBas Nieuwenhuizen 
msm_ioctl_gem_submit(struct drm_device * dev,void * data,struct drm_file * file)6497198e6b0SRob Clark int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
6507198e6b0SRob Clark 		struct drm_file *file)
6517198e6b0SRob Clark {
6527198e6b0SRob Clark 	struct msm_drm_private *priv = dev->dev_private;
6537198e6b0SRob Clark 	struct drm_msm_gem_submit *args = data;
6547198e6b0SRob Clark 	struct msm_file_private *ctx = file->driver_priv;
65568dc6c2dSRob Clark 	struct msm_gem_submit *submit = NULL;
656c01a958eSRob Clark 	struct msm_gpu *gpu = priv->gpu;
657f7de1545SJordan Crouse 	struct msm_gpu_submitqueue *queue;
658f97decacSJordan Crouse 	struct msm_ringbuffer *ring;
659ab723b7aSBas Nieuwenhuizen 	struct msm_submit_post_dep *post_deps = NULL;
660ab723b7aSBas Nieuwenhuizen 	struct drm_syncobj **syncobjs_to_reset = NULL;
6614cd09459SRob Clark 	int out_fence_fd = -1;
6627198e6b0SRob Clark 	unsigned i;
663fc1dd04cSRob Clark 	int ret;
664375f9a63SRob Clark 
665c01a958eSRob Clark 	if (!gpu)
666c01a958eSRob Clark 		return -ENXIO;
667c01a958eSRob Clark 
668ab723b7aSBas Nieuwenhuizen 	if (args->pad)
669ab723b7aSBas Nieuwenhuizen 		return -EINVAL;
670ab723b7aSBas Nieuwenhuizen 
6714a257ca6SRob Clark 	if (unlikely(!ctx->aspace) && !capable(CAP_SYS_RAWIO)) {
6724a257ca6SRob Clark 		DRM_ERROR_RATELIMITED("IOMMU support or CAP_SYS_RAWIO required!\n");
6734a257ca6SRob Clark 		return -EPERM;
6744a257ca6SRob Clark 	}
6754a257ca6SRob Clark 
6767198e6b0SRob Clark 	/* for now, we just have 3d pipe.. eventually this would need to
6777198e6b0SRob Clark 	 * be more clever to dispatch to appropriate gpu module:
6787198e6b0SRob Clark 	 */
679d9c181e2SRob Clark 	if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
680d9c181e2SRob Clark 		return -EINVAL;
681d9c181e2SRob Clark 
682d9c181e2SRob Clark 	if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
6837198e6b0SRob Clark 		return -EINVAL;
6847198e6b0SRob Clark 
6856a8bd08dSRob Clark 	if (args->flags & MSM_SUBMIT_SUDO) {
6866a8bd08dSRob Clark 		if (!IS_ENABLED(CONFIG_DRM_MSM_GPU_SUDO) ||
6876a8bd08dSRob Clark 		    !capable(CAP_SYS_RAWIO))
6886a8bd08dSRob Clark 			return -EINVAL;
6896a8bd08dSRob Clark 	}
6906a8bd08dSRob Clark 
691f7de1545SJordan Crouse 	queue = msm_submitqueue_get(ctx, args->queueid);
692f7de1545SJordan Crouse 	if (!queue)
693f7de1545SJordan Crouse 		return -ENOENT;
694f7de1545SJordan Crouse 
695fc40e5e1SRob Clark 	ring = gpu->rb[queue->ring_nr];
696f97decacSJordan Crouse 
6974cd09459SRob Clark 	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
6984cd09459SRob Clark 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
6994cd09459SRob Clark 		if (out_fence_fd < 0) {
7004cd09459SRob Clark 			ret = out_fence_fd;
70168dc6c2dSRob Clark 			goto out_post_unlock;
7024cd09459SRob Clark 		}
7034cd09459SRob Clark 	}
7044cd09459SRob Clark 
705f0de40a1SRob Clark 	submit = submit_create(dev, gpu, queue, args->nr_bos, args->nr_cmds);
70668dc6c2dSRob Clark 	if (IS_ERR(submit)) {
70768dc6c2dSRob Clark 		ret = PTR_ERR(submit);
70868dc6c2dSRob Clark 		goto out_post_unlock;
70968dc6c2dSRob Clark 	}
710f0de40a1SRob Clark 
711fc1dd04cSRob Clark 	trace_msm_gpu_submit(pid_nr(submit->pid), ring->id, submit->ident,
712fc1dd04cSRob Clark 		args->nr_bos, args->nr_cmds);
713fc1dd04cSRob Clark 
714f0de40a1SRob Clark 	ret = mutex_lock_interruptible(&queue->lock);
715f0de40a1SRob Clark 	if (ret)
716f0de40a1SRob Clark 		goto out_post_unlock;
7177198e6b0SRob Clark 
7186a8bd08dSRob Clark 	if (args->flags & MSM_SUBMIT_SUDO)
7196a8bd08dSRob Clark 		submit->in_rb = true;
7206a8bd08dSRob Clark 
7211d8a5ca4SRob Clark 	if (args->flags & MSM_SUBMIT_FENCE_FD_IN) {
7221d8a5ca4SRob Clark 		struct dma_fence *in_fence;
7231d8a5ca4SRob Clark 
7241d8a5ca4SRob Clark 		in_fence = sync_file_get_fence(args->fence_fd);
7251d8a5ca4SRob Clark 
7261d8a5ca4SRob Clark 		if (!in_fence) {
7271d8a5ca4SRob Clark 			ret = -EINVAL;
7281d8a5ca4SRob Clark 			goto out_unlock;
7291d8a5ca4SRob Clark 		}
7301d8a5ca4SRob Clark 
73180bcfbd3SDaniel Vetter 		ret = drm_sched_job_add_dependency(&submit->base, in_fence);
7321d8a5ca4SRob Clark 		if (ret)
7331d8a5ca4SRob Clark 			goto out_unlock;
7341d8a5ca4SRob Clark 	}
7351d8a5ca4SRob Clark 
7361d8a5ca4SRob Clark 	if (args->flags & MSM_SUBMIT_SYNCOBJ_IN) {
7371d8a5ca4SRob Clark 		syncobjs_to_reset = msm_parse_deps(submit, file,
7381d8a5ca4SRob Clark 		                                   args->in_syncobjs,
7391d8a5ca4SRob Clark 		                                   args->nr_in_syncobjs,
74063545526SDmitry Baryshkov 		                                   args->syncobj_stride);
7411d8a5ca4SRob Clark 		if (IS_ERR(syncobjs_to_reset)) {
7421d8a5ca4SRob Clark 			ret = PTR_ERR(syncobjs_to_reset);
7431d8a5ca4SRob Clark 			goto out_unlock;
7441d8a5ca4SRob Clark 		}
7451d8a5ca4SRob Clark 	}
7461d8a5ca4SRob Clark 
7471d8a5ca4SRob Clark 	if (args->flags & MSM_SUBMIT_SYNCOBJ_OUT) {
7481d8a5ca4SRob Clark 		post_deps = msm_parse_post_deps(dev, file,
7491d8a5ca4SRob Clark 		                                args->out_syncobjs,
7501d8a5ca4SRob Clark 		                                args->nr_out_syncobjs,
7511d8a5ca4SRob Clark 		                                args->syncobj_stride);
7521d8a5ca4SRob Clark 		if (IS_ERR(post_deps)) {
7531d8a5ca4SRob Clark 			ret = PTR_ERR(post_deps);
7541d8a5ca4SRob Clark 			goto out_unlock;
7551d8a5ca4SRob Clark 		}
7561d8a5ca4SRob Clark 	}
7571d8a5ca4SRob Clark 
7587198e6b0SRob Clark 	ret = submit_lookup_objects(submit, args, file);
7597198e6b0SRob Clark 	if (ret)
7601d8a5ca4SRob Clark 		goto out;
7617198e6b0SRob Clark 
76220224d71SRob Clark 	ret = submit_lookup_cmds(submit, args, file);
76320224d71SRob Clark 	if (ret)
7641d8a5ca4SRob Clark 		goto out;
76520224d71SRob Clark 
76692ec0767SDaniel Vetter 	/* copy_*_user while holding a ww ticket upsets lockdep */
767340faef2SRob Clark 	ret = submit_lock_objects(submit);
768340faef2SRob Clark 	if (ret)
769340faef2SRob Clark 		goto out;
770340faef2SRob Clark 
771*a6397e63SRob Clark 	if (!(args->flags & MSM_SUBMIT_NO_IMPLICIT)) {
772*a6397e63SRob Clark 		ret = submit_fence_sync(submit);
773b6295f9aSRob Clark 		if (ret)
774b6295f9aSRob Clark 			goto out;
775*a6397e63SRob Clark 	}
776b6295f9aSRob Clark 
777340faef2SRob Clark 	ret = submit_pin_objects(submit);
7787198e6b0SRob Clark 	if (ret)
7797198e6b0SRob Clark 		goto out;
7807198e6b0SRob Clark 
7817198e6b0SRob Clark 	for (i = 0; i < args->nr_cmds; i++) {
7826ba5daa5SRob Clark 		struct drm_gem_object *obj;
78378babc16SRob Clark 		uint64_t iova;
7847198e6b0SRob Clark 
785a3dec9cdSRob Clark 		ret = submit_bo(submit, submit->cmd[i].idx, &obj, &iova);
7867198e6b0SRob Clark 		if (ret)
7877198e6b0SRob Clark 			goto out;
7887198e6b0SRob Clark 
78920224d71SRob Clark 		if (!submit->cmd[i].size ||
79020224d71SRob Clark 			((submit->cmd[i].size + submit->cmd[i].offset) >
7916ba5daa5SRob Clark 				obj->size / 4)) {
792b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "invalid cmdstream size: %u\n", submit->cmd[i].size * 4);
7937198e6b0SRob Clark 			ret = -EINVAL;
7947198e6b0SRob Clark 			goto out;
7957198e6b0SRob Clark 		}
7967198e6b0SRob Clark 
79720224d71SRob Clark 		submit->cmd[i].iova = iova + (submit->cmd[i].offset * 4);
7987198e6b0SRob Clark 
799a3dec9cdSRob Clark 		if (likely(!submit->cmd[i].nr_relocs))
8007198e6b0SRob Clark 			continue;
8017198e6b0SRob Clark 
80242854f8dSRob Clark 		if (!gpu->allow_relocs) {
803b2acb89aSRob Clark 			SUBMIT_ERROR(submit, "relocs not allowed\n");
80442854f8dSRob Clark 			ret = -EINVAL;
80542854f8dSRob Clark 			goto out;
80642854f8dSRob Clark 		}
80742854f8dSRob Clark 
8086ba5daa5SRob Clark 		ret = submit_reloc(submit, obj, submit->cmd[i].offset * 4,
80920224d71SRob Clark 				submit->cmd[i].nr_relocs, submit->cmd[i].relocs);
8107198e6b0SRob Clark 		if (ret)
8117198e6b0SRob Clark 			goto out;
8127198e6b0SRob Clark 	}
8137198e6b0SRob Clark 
8147198e6b0SRob Clark 	submit->nr_cmds = i;
8157198e6b0SRob Clark 
81644c20087SRob Clark 	idr_preload(GFP_KERNEL);
81744c20087SRob Clark 
818e4f020c6SRob Clark 	spin_lock(&queue->idr_lock);
81905ba44b3SRob Clark 
82017154addSRob Clark 	/*
82117154addSRob Clark 	 * If using userspace provided seqno fence, validate that the id
82217154addSRob Clark 	 * is available before arming sched job.  Since access to fence_idr
82317154addSRob Clark 	 * is serialized on the queue lock, the slot should be still avail
82417154addSRob Clark 	 * after the job is armed
82517154addSRob Clark 	 */
82617154addSRob Clark 	if ((args->flags & MSM_SUBMIT_FENCE_SN_IN) &&
8271b5d0ddcSRob Clark 			(!args->fence || idr_find(&queue->fence_idr, args->fence))) {
828e4f020c6SRob Clark 		spin_unlock(&queue->idr_lock);
82944c20087SRob Clark 		idr_preload_end();
83017154addSRob Clark 		ret = -EINVAL;
83117154addSRob Clark 		goto out;
83217154addSRob Clark 	}
83317154addSRob Clark 
834357285a2SDaniel Vetter 	drm_sched_job_arm(&submit->base);
835357285a2SDaniel Vetter 
8361d8a5ca4SRob Clark 	submit->user_fence = dma_fence_get(&submit->base.s_fence->finished);
837f44d32c7SRob Clark 
83817154addSRob Clark 	if (args->flags & MSM_SUBMIT_FENCE_SN_IN) {
839a61acbbeSRob Clark 		/*
84017154addSRob Clark 		 * Userspace has assigned the seqno fence that it wants
84117154addSRob Clark 		 * us to use.  It is an error to pick a fence sequence
84217154addSRob Clark 		 * number that is not available.
84317154addSRob Clark 		 */
84417154addSRob Clark 		submit->fence_id = args->fence;
84517154addSRob Clark 		ret = idr_alloc_u32(&queue->fence_idr, submit->user_fence,
84617154addSRob Clark 				    &submit->fence_id, submit->fence_id,
84744c20087SRob Clark 				    GFP_NOWAIT);
84817154addSRob Clark 		/*
84917154addSRob Clark 		 * We've already validated that the fence_id slot is valid,
85017154addSRob Clark 		 * so if idr_alloc_u32 failed, it is a kernel bug
85117154addSRob Clark 		 */
85217154addSRob Clark 		WARN_ON(ret);
85317154addSRob Clark 	} else {
85417154addSRob Clark 		/*
85517154addSRob Clark 		 * Allocate an id which can be used by WAIT_FENCE ioctl to map
85617154addSRob Clark 		 * back to the underlying fence.
857a61acbbeSRob Clark 		 */
858a61acbbeSRob Clark 		submit->fence_id = idr_alloc_cyclic(&queue->fence_idr,
85917154addSRob Clark 						    submit->user_fence, 1,
86044c20087SRob Clark 						    INT_MAX, GFP_NOWAIT);
86117154addSRob Clark 	}
86205ba44b3SRob Clark 
863e4f020c6SRob Clark 	spin_unlock(&queue->idr_lock);
86444c20087SRob Clark 	idr_preload_end();
86505ba44b3SRob Clark 
866a61acbbeSRob Clark 	if (submit->fence_id < 0) {
86708de2141SRob Clark 		ret = submit->fence_id;
868a61acbbeSRob Clark 		submit->fence_id = 0;
869f44d32c7SRob Clark 	}
870f44d32c7SRob Clark 
871357285a2SDaniel Vetter 	if (ret == 0 && args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
8721d8a5ca4SRob Clark 		struct sync_file *sync_file = sync_file_create(submit->user_fence);
8734cd09459SRob Clark 		if (!sync_file) {
8744cd09459SRob Clark 			ret = -ENOMEM;
875357285a2SDaniel Vetter 		} else {
8764cd09459SRob Clark 			fd_install(out_fence_fd, sync_file->file);
8774cd09459SRob Clark 			args->fence_fd = out_fence_fd;
8784cd09459SRob Clark 		}
879357285a2SDaniel Vetter 	}
8804cd09459SRob Clark 
881ceab575cSRob Clark 	if (ret)
882ceab575cSRob Clark 		goto out;
883ceab575cSRob Clark 
884be40596bSRob Clark 	submit_attach_object_fences(submit);
885be40596bSRob Clark 
8861d8a5ca4SRob Clark 	/* The scheduler owns a ref now: */
8871d8a5ca4SRob Clark 	msm_gem_submit_get(submit);
8881d8a5ca4SRob Clark 
889171f580eSRob Clark 	msm_rd_dump_submit(priv->rd, submit, NULL);
890171f580eSRob Clark 
8910e10e9a1SDaniel Vetter 	drm_sched_entity_push_job(&submit->base);
892f44d32c7SRob Clark 
893a61acbbeSRob Clark 	args->fence = submit->fence_id;
894067ecab9SRob Clark 	queue->last_fence = submit->fence_id;
895f44d32c7SRob Clark 
896ab723b7aSBas Nieuwenhuizen 	msm_reset_syncobjs(syncobjs_to_reset, args->nr_in_syncobjs);
897ab723b7aSBas Nieuwenhuizen 	msm_process_post_deps(post_deps, args->nr_out_syncobjs,
8981d8a5ca4SRob Clark 	                      submit->user_fence);
899ab723b7aSBas Nieuwenhuizen 
900ab723b7aSBas Nieuwenhuizen 
9017198e6b0SRob Clark out:
902be40596bSRob Clark 	submit_cleanup(submit, !!ret);
903b5b4c264SRob Clark out_unlock:
904e3e24ee5SRob Clark 	mutex_unlock(&queue->lock);
905f0de40a1SRob Clark out_post_unlock:
90668dc6c2dSRob Clark 	if (ret && (out_fence_fd >= 0))
90768dc6c2dSRob Clark 		put_unused_fd(out_fence_fd);
90868dc6c2dSRob Clark 
90968dc6c2dSRob Clark 	if (!IS_ERR_OR_NULL(submit)) {
910e3e24ee5SRob Clark 		msm_gem_submit_put(submit);
91168dc6c2dSRob Clark 	} else {
91268dc6c2dSRob Clark 		/*
91368dc6c2dSRob Clark 		 * If the submit hasn't yet taken ownership of the queue
91468dc6c2dSRob Clark 		 * then we need to drop the reference ourself:
91568dc6c2dSRob Clark 		 */
91668dc6c2dSRob Clark 		msm_submitqueue_put(queue);
91768dc6c2dSRob Clark 	}
918ab723b7aSBas Nieuwenhuizen 	if (!IS_ERR_OR_NULL(post_deps)) {
919ab723b7aSBas Nieuwenhuizen 		for (i = 0; i < args->nr_out_syncobjs; ++i) {
920ab723b7aSBas Nieuwenhuizen 			kfree(post_deps[i].chain);
921ab723b7aSBas Nieuwenhuizen 			drm_syncobj_put(post_deps[i].syncobj);
922ab723b7aSBas Nieuwenhuizen 		}
923ab723b7aSBas Nieuwenhuizen 		kfree(post_deps);
924ab723b7aSBas Nieuwenhuizen 	}
925ab723b7aSBas Nieuwenhuizen 
926ab723b7aSBas Nieuwenhuizen 	if (!IS_ERR_OR_NULL(syncobjs_to_reset)) {
927ab723b7aSBas Nieuwenhuizen 		for (i = 0; i < args->nr_in_syncobjs; ++i) {
928ab723b7aSBas Nieuwenhuizen 			if (syncobjs_to_reset[i])
929ab723b7aSBas Nieuwenhuizen 				drm_syncobj_put(syncobjs_to_reset[i]);
930ab723b7aSBas Nieuwenhuizen 		}
931ab723b7aSBas Nieuwenhuizen 		kfree(syncobjs_to_reset);
932ab723b7aSBas Nieuwenhuizen 	}
933ab723b7aSBas Nieuwenhuizen 
9347198e6b0SRob Clark 	return ret;
9357198e6b0SRob Clark }
936