xref: /linux/drivers/gpu/drm/msm/msm_gem_submit.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/dma-fence-unwrap.h>
8 #include <linux/file.h>
9 #include <linux/sync_file.h>
10 #include <linux/uaccess.h>
11 
12 #include <drm/drm_drv.h>
13 #include <drm/drm_file.h>
14 #include <drm/drm_syncobj.h>
15 
16 #include "msm_drv.h"
17 #include "msm_gpu.h"
18 #include "msm_gem.h"
19 #include "msm_gpu_trace.h"
20 #include "msm_syncobj.h"
21 
22 /* For userspace errors, use DRM_UT_DRIVER.. so that userspace can enable
23  * error msgs for debugging, but we don't spam dmesg by default
24  */
25 #define SUBMIT_ERROR(err, submit, fmt, ...) \
26 	UERR(err, (submit)->dev, fmt, ##__VA_ARGS__)
27 
28 /*
29  * Cmdstream submission:
30  */
31 
32 static struct msm_gem_submit *submit_create(struct drm_device *dev,
33 		struct msm_gpu *gpu,
34 		struct msm_gpu_submitqueue *queue, uint32_t nr_bos,
35 		uint32_t nr_cmds, u64 drm_client_id)
36 {
37 	static atomic_t ident = ATOMIC_INIT(0);
38 	struct msm_gem_submit *submit;
39 	uint64_t sz;
40 	int ret;
41 
42 	sz = struct_size(submit, bos, nr_bos) +
43 			((u64)nr_cmds * sizeof(submit->cmd[0]));
44 
45 	if (sz > SIZE_MAX)
46 		return ERR_PTR(-ENOMEM);
47 
48 	submit = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
49 	if (!submit)
50 		return ERR_PTR(-ENOMEM);
51 
52 	submit->hw_fence = msm_fence_alloc();
53 	if (IS_ERR(submit->hw_fence)) {
54 		ret = PTR_ERR(submit->hw_fence);
55 		kfree(submit);
56 		return ERR_PTR(ret);
57 	}
58 
59 	ret = drm_sched_job_init(&submit->base, queue->entity, 1, queue,
60 				 drm_client_id);
61 	if (ret) {
62 		kfree(submit->hw_fence);
63 		kfree(submit);
64 		return ERR_PTR(ret);
65 	}
66 
67 	kref_init(&submit->ref);
68 	submit->dev = dev;
69 	submit->vm = msm_context_vm(dev, queue->ctx);
70 	submit->gpu = gpu;
71 	submit->cmd = (void *)&submit->bos[nr_bos];
72 	submit->queue = queue;
73 	submit->pid = get_pid(task_pid(current));
74 	submit->ring = gpu->rb[queue->ring_nr];
75 	submit->fault_dumped = false;
76 
77 	/* Get a unique identifier for the submission for logging purposes */
78 	submit->ident = atomic_inc_return(&ident) - 1;
79 
80 	INIT_LIST_HEAD(&submit->node);
81 
82 	return submit;
83 }
84 
85 void __msm_gem_submit_destroy(struct kref *kref)
86 {
87 	struct msm_gem_submit *submit =
88 			container_of(kref, struct msm_gem_submit, ref);
89 	unsigned i;
90 
91 	/*
92 	 * In error paths, we could unref the submit without calling
93 	 * drm_sched_entity_push_job(), so msm_job_free() will never
94 	 * get called.  Since drm_sched_job_cleanup() will NULL out
95 	 * s_fence, we can use that to detect this case.
96 	 */
97 	if (submit->base.s_fence)
98 		drm_sched_job_cleanup(&submit->base);
99 
100 	if (submit->fence_id) {
101 		spin_lock(&submit->queue->idr_lock);
102 		idr_remove(&submit->queue->fence_idr, submit->fence_id);
103 		spin_unlock(&submit->queue->idr_lock);
104 	}
105 
106 	dma_fence_put(submit->user_fence);
107 
108 	/*
109 	 * If the submit is freed before msm_job_run(), then hw_fence is
110 	 * just some pre-allocated memory, not a reference counted fence.
111 	 * Once the job runs and the hw_fence is initialized, it will
112 	 * have a refcount of at least one, since the submit holds a ref
113 	 * to the hw_fence.
114 	 */
115 	if (kref_read(&submit->hw_fence->refcount) == 0) {
116 		kfree(submit->hw_fence);
117 	} else {
118 		dma_fence_put(submit->hw_fence);
119 	}
120 
121 	put_pid(submit->pid);
122 	msm_submitqueue_put(submit->queue);
123 
124 	for (i = 0; i < submit->nr_cmds; i++)
125 		kfree(submit->cmd[i].relocs);
126 
127 	kfree(submit);
128 }
129 
130 static int submit_lookup_objects(struct msm_gem_submit *submit,
131 		struct drm_msm_gem_submit *args, struct drm_file *file)
132 {
133 	unsigned i;
134 	int ret = 0;
135 
136 	for (i = 0; i < args->nr_bos; i++) {
137 		struct drm_msm_gem_submit_bo submit_bo;
138 		void __user *userptr =
139 			u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
140 
141 		/* make sure we don't have garbage flags, in case we hit
142 		 * error path before flags is initialized:
143 		 */
144 		submit->bos[i].flags = 0;
145 
146 		if (copy_from_user(&submit_bo, userptr, sizeof(submit_bo))) {
147 			ret = -EFAULT;
148 			i = 0;
149 			goto out;
150 		}
151 
152 /* at least one of READ and/or WRITE flags should be set: */
153 #define MANDATORY_FLAGS (MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_WRITE)
154 
155 		if ((submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) ||
156 			!(submit_bo.flags & MANDATORY_FLAGS)) {
157 			ret = SUBMIT_ERROR(EINVAL, submit, "invalid flags: %x\n", submit_bo.flags);
158 			i = 0;
159 			goto out;
160 		}
161 
162 		submit->bos[i].handle = submit_bo.handle;
163 		submit->bos[i].flags = submit_bo.flags;
164 	}
165 
166 	spin_lock(&file->table_lock);
167 
168 	for (i = 0; i < args->nr_bos; i++) {
169 		struct drm_gem_object *obj;
170 
171 		/* normally use drm_gem_object_lookup(), but for bulk lookup
172 		 * all under single table_lock just hit object_idr directly:
173 		 */
174 		obj = idr_find(&file->object_idr, submit->bos[i].handle);
175 		if (!obj) {
176 			ret = SUBMIT_ERROR(EINVAL, submit, "invalid handle %u at index %u\n", submit->bos[i].handle, i);
177 			goto out_unlock;
178 		}
179 
180 		drm_gem_object_get(obj);
181 
182 		submit->bos[i].obj = obj;
183 	}
184 
185 out_unlock:
186 	spin_unlock(&file->table_lock);
187 
188 out:
189 	submit->nr_bos = i;
190 
191 	return ret;
192 }
193 
194 static int submit_lookup_cmds(struct msm_gem_submit *submit,
195 		struct drm_msm_gem_submit *args, struct drm_file *file)
196 {
197 	struct msm_context *ctx = file->driver_priv;
198 	unsigned i;
199 	size_t sz;
200 	int ret = 0;
201 
202 	for (i = 0; i < args->nr_cmds; i++) {
203 		struct drm_msm_gem_submit_cmd submit_cmd;
204 		void __user *userptr =
205 			u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
206 
207 		ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
208 		if (ret) {
209 			ret = -EFAULT;
210 			goto out;
211 		}
212 
213 		/* validate input from userspace: */
214 		switch (submit_cmd.type) {
215 		case MSM_SUBMIT_CMD_BUF:
216 		case MSM_SUBMIT_CMD_IB_TARGET_BUF:
217 		case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
218 			break;
219 		default:
220 			return SUBMIT_ERROR(EINVAL, submit, "invalid type: %08x\n", submit_cmd.type);
221 		}
222 
223 		if (submit_cmd.size % 4) {
224 			ret = SUBMIT_ERROR(EINVAL, submit, "non-aligned cmdstream buffer size: %u\n",
225 					   submit_cmd.size);
226 			goto out;
227 		}
228 
229 		if (msm_context_is_vmbind(ctx)) {
230 			if (submit_cmd.nr_relocs) {
231 				ret = SUBMIT_ERROR(EINVAL, submit, "nr_relocs must be zero");
232 				goto out;
233 			}
234 
235 			if (submit_cmd.submit_idx || submit_cmd.submit_offset) {
236 				ret = SUBMIT_ERROR(EINVAL, submit, "submit_idx/offset must be zero");
237 				goto out;
238 			}
239 
240 			submit->cmd[i].iova = submit_cmd.iova;
241 		}
242 
243 		submit->cmd[i].type = submit_cmd.type;
244 		submit->cmd[i].size = submit_cmd.size / 4;
245 		submit->cmd[i].offset = submit_cmd.submit_offset / 4;
246 		submit->cmd[i].idx  = submit_cmd.submit_idx;
247 		submit->cmd[i].nr_relocs = submit_cmd.nr_relocs;
248 
249 		userptr = u64_to_user_ptr(submit_cmd.relocs);
250 
251 		sz = array_size(submit_cmd.nr_relocs,
252 				sizeof(struct drm_msm_gem_submit_reloc));
253 		/* check for overflow: */
254 		if (sz == SIZE_MAX) {
255 			ret = -ENOMEM;
256 			goto out;
257 		}
258 		submit->cmd[i].relocs = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN);
259 		if (!submit->cmd[i].relocs) {
260 			ret = -ENOMEM;
261 			goto out;
262 		}
263 		ret = copy_from_user(submit->cmd[i].relocs, userptr, sz);
264 		if (ret) {
265 			ret = -EFAULT;
266 			goto out;
267 		}
268 	}
269 
270 out:
271 	return ret;
272 }
273 
274 /* This is where we make sure all the bo's are reserved and pin'd: */
275 static int submit_lock_objects(struct msm_gem_submit *submit)
276 {
277 	unsigned flags = DRM_EXEC_INTERRUPTIBLE_WAIT;
278 	struct drm_exec *exec = &submit->exec;
279 	int ret;
280 
281 	if (msm_context_is_vmbind(submit->queue->ctx)) {
282 		flags |= DRM_EXEC_IGNORE_DUPLICATES;
283 
284 		drm_exec_init(&submit->exec, flags, submit->nr_bos);
285 
286 		drm_exec_until_all_locked (&submit->exec) {
287 			ret = drm_gpuvm_prepare_vm(submit->vm, exec, 1);
288 			drm_exec_retry_on_contention(exec);
289 			if (ret)
290 				return ret;
291 
292 			ret = drm_gpuvm_prepare_objects(submit->vm, exec, 1);
293 			drm_exec_retry_on_contention(exec);
294 			if (ret)
295 				return ret;
296 		}
297 
298 		return 0;
299 	}
300 
301 	drm_exec_init(&submit->exec, flags, submit->nr_bos);
302 
303 	drm_exec_until_all_locked (&submit->exec) {
304 		ret = drm_exec_lock_obj(&submit->exec,
305 					drm_gpuvm_resv_obj(submit->vm));
306 		drm_exec_retry_on_contention(&submit->exec);
307 		if (ret)
308 			return ret;
309 		for (unsigned i = 0; i < submit->nr_bos; i++) {
310 			struct drm_gem_object *obj = submit->bos[i].obj;
311 			ret = drm_exec_prepare_obj(&submit->exec, obj, 1);
312 			drm_exec_retry_on_contention(&submit->exec);
313 			if (ret)
314 				return ret;
315 		}
316 	}
317 
318 	return 0;
319 }
320 
321 static int submit_fence_sync(struct msm_gem_submit *submit)
322 {
323 	int i, ret = 0;
324 
325 	for (i = 0; i < submit->nr_bos; i++) {
326 		struct drm_gem_object *obj = submit->bos[i].obj;
327 		bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
328 
329 		/* Otherwise userspace can ask for implicit sync to be
330 		 * disabled on specific buffers.  This is useful for internal
331 		 * usermode driver managed buffers, suballocation, etc.
332 		 */
333 		if (submit->bos[i].flags & MSM_SUBMIT_BO_NO_IMPLICIT)
334 			continue;
335 
336 		ret = drm_sched_job_add_implicit_dependencies(&submit->base,
337 							      obj,
338 							      write);
339 		if (ret)
340 			break;
341 	}
342 
343 	return ret;
344 }
345 
346 static int submit_pin_objects(struct msm_gem_submit *submit)
347 {
348 	struct msm_drm_private *priv = submit->dev->dev_private;
349 	int i, ret = 0;
350 
351 	for (i = 0; i < submit->nr_bos; i++) {
352 		struct drm_gem_object *obj = submit->bos[i].obj;
353 		struct drm_gpuva *vma;
354 
355 		/* if locking succeeded, pin bo: */
356 		vma = msm_gem_get_vma_locked(obj, submit->vm);
357 		if (IS_ERR(vma)) {
358 			ret = PTR_ERR(vma);
359 			break;
360 		}
361 
362 		ret = msm_gem_pin_vma_locked(obj, vma);
363 		if (ret)
364 			break;
365 
366 		submit->bos[i].vm_bo = drm_gpuvm_bo_get(vma->vm_bo);
367 		submit->bos[i].iova = vma->va.addr;
368 	}
369 
370 	/*
371 	 * A second loop while holding the LRU lock (a) avoids acquiring/dropping
372 	 * the LRU lock for each individual bo, while (b) avoiding holding the
373 	 * LRU lock while calling msm_gem_pin_vma_locked() (which could trigger
374 	 * get_pages() which could trigger reclaim.. and if we held the LRU lock
375 	 * could trigger deadlock with the shrinker).
376 	 */
377 	mutex_lock(&priv->lru.lock);
378 	for (i = 0; i < submit->nr_bos; i++) {
379 		msm_gem_pin_obj_locked(submit->bos[i].obj);
380 	}
381 	mutex_unlock(&priv->lru.lock);
382 
383 	submit->bos_pinned = true;
384 
385 	return ret;
386 }
387 
388 static void submit_unpin_objects(struct msm_gem_submit *submit)
389 {
390 	if (!submit->bos_pinned)
391 		return;
392 
393 	for (int i = 0; i < submit->nr_bos; i++) {
394 		struct drm_gem_object *obj = submit->bos[i].obj;
395 
396 		msm_gem_unpin_locked(obj);
397 	}
398 
399 	submit->bos_pinned = false;
400 }
401 
402 static void submit_attach_object_fences(struct msm_gem_submit *submit)
403 {
404 	struct msm_gem_vm *vm = to_msm_vm(submit->vm);
405 	struct dma_fence *last_fence;
406 
407 	if (msm_context_is_vmbind(submit->queue->ctx)) {
408 		drm_gpuvm_resv_add_fence(submit->vm, &submit->exec,
409 					 submit->user_fence,
410 					 DMA_RESV_USAGE_BOOKKEEP,
411 					 DMA_RESV_USAGE_BOOKKEEP);
412 		return;
413 	}
414 
415 	for (unsigned i = 0; i < submit->nr_bos; i++) {
416 		struct drm_gem_object *obj = submit->bos[i].obj;
417 
418 		if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
419 			dma_resv_add_fence(obj->resv, submit->user_fence,
420 					   DMA_RESV_USAGE_WRITE);
421 		else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
422 			dma_resv_add_fence(obj->resv, submit->user_fence,
423 					   DMA_RESV_USAGE_READ);
424 	}
425 
426 	last_fence = vm->last_fence;
427 	vm->last_fence = dma_fence_unwrap_merge(submit->user_fence, last_fence);
428 	dma_fence_put(last_fence);
429 }
430 
431 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
432 		struct drm_gem_object **obj, uint64_t *iova)
433 {
434 	if (idx >= submit->nr_bos) {
435 		return SUBMIT_ERROR(EINVAL, submit, "invalid buffer index: %u (out of %u)\n",
436 				    idx, submit->nr_bos);
437 	}
438 
439 	if (obj)
440 		*obj = submit->bos[idx].obj;
441 	if (iova)
442 		*iova = submit->bos[idx].iova;
443 
444 	return 0;
445 }
446 
447 /* process the reloc's and patch up the cmdstream as needed: */
448 static int submit_reloc(struct msm_gem_submit *submit, struct drm_gem_object *obj,
449 		uint32_t offset, uint32_t nr_relocs, struct drm_msm_gem_submit_reloc *relocs)
450 {
451 	uint32_t i, last_offset = 0;
452 	uint32_t *ptr;
453 	int ret = 0;
454 
455 	if (offset % 4)
456 		return SUBMIT_ERROR(EINVAL, submit, "non-aligned cmdstream buffer: %u\n", offset);
457 
458 	/* For now, just map the entire thing.  Eventually we probably
459 	 * to do it page-by-page, w/ kmap() if not vmap()d..
460 	 */
461 	ptr = msm_gem_get_vaddr_locked(obj);
462 
463 	if (IS_ERR(ptr)) {
464 		ret = PTR_ERR(ptr);
465 		DBG("failed to map: %d", ret);
466 		return ret;
467 	}
468 
469 	for (i = 0; i < nr_relocs; i++) {
470 		struct drm_msm_gem_submit_reloc submit_reloc = relocs[i];
471 		uint32_t off;
472 		uint64_t iova;
473 
474 		if (submit_reloc.submit_offset % 4) {
475 			ret = SUBMIT_ERROR(EINVAL, submit, "non-aligned reloc offset: %u\n",
476 					   submit_reloc.submit_offset);
477 			goto out;
478 		}
479 
480 		/* offset in dwords: */
481 		off = submit_reloc.submit_offset / 4;
482 
483 		if ((off >= (obj->size / 4)) ||
484 				(off < last_offset)) {
485 			ret = SUBMIT_ERROR(EINVAL, submit, "invalid offset %u at reloc %u\n", off, i);
486 			goto out;
487 		}
488 
489 		ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova);
490 		if (ret)
491 			goto out;
492 
493 		iova += submit_reloc.reloc_offset;
494 
495 		if (submit_reloc.shift < 0)
496 			iova >>= -submit_reloc.shift;
497 		else
498 			iova <<= submit_reloc.shift;
499 
500 		ptr[off] = iova | submit_reloc.or;
501 
502 		last_offset = off;
503 	}
504 
505 out:
506 	msm_gem_put_vaddr_locked(obj);
507 
508 	return ret;
509 }
510 
511 /* Cleanup submit at end of ioctl.  In the error case, this also drops
512  * references, unpins, and drops active refcnt.  In the non-error case,
513  * this is done when the submit is retired.
514  */
515 static void submit_cleanup(struct msm_gem_submit *submit, bool error)
516 {
517 	if (submit->exec.objects)
518 		drm_exec_fini(&submit->exec);
519 
520 	if (error) {
521 		submit_unpin_objects(submit);
522 		/* job wasn't enqueued to scheduler, so early retirement: */
523 		msm_submit_retire(submit);
524 	}
525 }
526 
527 void msm_submit_retire(struct msm_gem_submit *submit)
528 {
529 	int i;
530 
531 	for (i = 0; i < submit->nr_bos; i++) {
532 		struct drm_gem_object *obj = submit->bos[i].obj;
533 		struct drm_gpuvm_bo *vm_bo = submit->bos[i].vm_bo;
534 
535 		msm_gem_lock(obj);
536 		drm_gpuvm_bo_put(vm_bo);
537 		msm_gem_unlock(obj);
538 		drm_gem_object_put(obj);
539 	}
540 }
541 
542 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
543 		struct drm_file *file)
544 {
545 	struct msm_drm_private *priv = dev->dev_private;
546 	struct drm_msm_gem_submit *args = data;
547 	struct msm_context *ctx = file->driver_priv;
548 	struct msm_gem_submit *submit = NULL;
549 	struct msm_gpu *gpu = priv->gpu;
550 	struct msm_gpu_submitqueue *queue;
551 	struct msm_ringbuffer *ring;
552 	struct msm_syncobj_post_dep *post_deps = NULL;
553 	struct drm_syncobj **syncobjs_to_reset = NULL;
554 	struct sync_file *sync_file = NULL;
555 	unsigned cmds_to_parse;
556 	int out_fence_fd = -1;
557 	unsigned i;
558 	int ret;
559 
560 	if (!gpu)
561 		return -ENXIO;
562 
563 	if (args->pad)
564 		return -EINVAL;
565 
566 	if (to_msm_vm(ctx->vm)->unusable)
567 		return UERR(EPIPE, dev, "context is unusable");
568 
569 	/* for now, we just have 3d pipe.. eventually this would need to
570 	 * be more clever to dispatch to appropriate gpu module:
571 	 */
572 	if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
573 		return UERR(EINVAL, dev, "invalid pipe");
574 
575 	if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
576 		return UERR(EINVAL, dev, "invalid flags");
577 
578 	if (args->flags & MSM_SUBMIT_SUDO) {
579 		if (!IS_ENABLED(CONFIG_DRM_MSM_GPU_SUDO) ||
580 		    !capable(CAP_SYS_RAWIO))
581 			return -EINVAL;
582 	}
583 
584 	queue = msm_submitqueue_get(ctx, args->queueid);
585 	if (!queue)
586 		return -ENOENT;
587 
588 	if (queue->flags & MSM_SUBMITQUEUE_VM_BIND) {
589 		ret = UERR(EINVAL, dev, "Invalid queue type");
590 		goto out_post_unlock;
591 	}
592 
593 	ring = gpu->rb[queue->ring_nr];
594 
595 	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
596 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
597 		if (out_fence_fd < 0) {
598 			ret = out_fence_fd;
599 			goto out_post_unlock;
600 		}
601 	}
602 
603 	submit = submit_create(dev, gpu, queue, args->nr_bos, args->nr_cmds,
604 			       file->client_id);
605 	if (IS_ERR(submit)) {
606 		ret = PTR_ERR(submit);
607 		goto out_post_unlock;
608 	}
609 
610 	trace_msm_gpu_submit(pid_nr(submit->pid), ring->id, submit->ident,
611 		args->nr_bos, args->nr_cmds);
612 
613 	ret = mutex_lock_interruptible(&queue->lock);
614 	if (ret)
615 		goto out_post_unlock;
616 
617 	if (args->flags & MSM_SUBMIT_SUDO)
618 		submit->in_rb = true;
619 
620 	if (args->flags & MSM_SUBMIT_FENCE_FD_IN) {
621 		struct dma_fence *in_fence;
622 
623 		in_fence = sync_file_get_fence(args->fence_fd);
624 
625 		if (!in_fence) {
626 			ret = UERR(EINVAL, dev, "invalid in-fence");
627 			goto out_unlock;
628 		}
629 
630 		ret = drm_sched_job_add_dependency(&submit->base, in_fence);
631 		if (ret)
632 			goto out_unlock;
633 	}
634 
635 	if (args->flags & MSM_SUBMIT_SYNCOBJ_IN) {
636 		syncobjs_to_reset = msm_syncobj_parse_deps(dev, &submit->base,
637 							   file, args->in_syncobjs,
638 							   args->nr_in_syncobjs,
639 							   args->syncobj_stride);
640 		if (IS_ERR(syncobjs_to_reset)) {
641 			ret = PTR_ERR(syncobjs_to_reset);
642 			goto out_unlock;
643 		}
644 	}
645 
646 	if (args->flags & MSM_SUBMIT_SYNCOBJ_OUT) {
647 		post_deps = msm_syncobj_parse_post_deps(dev, file,
648 							args->out_syncobjs,
649 							args->nr_out_syncobjs,
650 							args->syncobj_stride);
651 		if (IS_ERR(post_deps)) {
652 			ret = PTR_ERR(post_deps);
653 			goto out_unlock;
654 		}
655 	}
656 
657 	ret = submit_lookup_objects(submit, args, file);
658 	if (ret)
659 		goto out;
660 
661 	ret = submit_lookup_cmds(submit, args, file);
662 	if (ret)
663 		goto out;
664 
665 	/* copy_*_user while holding a ww ticket upsets lockdep */
666 	ret = submit_lock_objects(submit);
667 	if (ret)
668 		goto out;
669 
670 	if (!(args->flags & MSM_SUBMIT_NO_IMPLICIT)) {
671 		ret = submit_fence_sync(submit);
672 		if (ret)
673 			goto out;
674 	}
675 
676 	ret = submit_pin_objects(submit);
677 	if (ret)
678 		goto out;
679 
680 	cmds_to_parse = msm_context_is_vmbind(ctx) ? 0 : args->nr_cmds;
681 
682 	for (i = 0; i < cmds_to_parse; i++) {
683 		struct drm_gem_object *obj;
684 		uint64_t iova;
685 
686 		ret = submit_bo(submit, submit->cmd[i].idx, &obj, &iova);
687 		if (ret)
688 			goto out;
689 
690 		if (!submit->cmd[i].size ||
691 		    (size_add(submit->cmd[i].size, submit->cmd[i].offset) > obj->size / 4)) {
692 			ret = UERR(EINVAL, dev, "invalid cmdstream size: %u\n",
693 				   submit->cmd[i].size * 4);
694 			goto out;
695 		}
696 
697 		submit->cmd[i].iova = iova + (submit->cmd[i].offset * 4);
698 
699 		if (likely(!submit->cmd[i].nr_relocs))
700 			continue;
701 
702 		if (!gpu->allow_relocs) {
703 			ret = UERR(EINVAL, dev, "relocs not allowed\n");
704 			goto out;
705 		}
706 
707 		ret = submit_reloc(submit, obj, submit->cmd[i].offset * 4,
708 				submit->cmd[i].nr_relocs, submit->cmd[i].relocs);
709 		if (ret)
710 			goto out;
711 	}
712 
713 	submit->nr_cmds = args->nr_cmds;
714 
715 	idr_preload(GFP_KERNEL);
716 
717 	spin_lock(&queue->idr_lock);
718 
719 	/*
720 	 * If using userspace provided seqno fence, validate that the id
721 	 * is available before arming sched job.  Since access to fence_idr
722 	 * is serialized on the queue lock, the slot should be still avail
723 	 * after the job is armed
724 	 */
725 	if ((args->flags & MSM_SUBMIT_FENCE_SN_IN) &&
726 			(!args->fence || idr_find(&queue->fence_idr, args->fence))) {
727 		spin_unlock(&queue->idr_lock);
728 		idr_preload_end();
729 		ret = UERR(EINVAL, dev, "invalid in-fence-sn");
730 		goto out;
731 	}
732 
733 	drm_sched_job_arm(&submit->base);
734 
735 	submit->user_fence = dma_fence_get(&submit->base.s_fence->finished);
736 
737 	if (args->flags & MSM_SUBMIT_FENCE_SN_IN) {
738 		/*
739 		 * Userspace has assigned the seqno fence that it wants
740 		 * us to use.  It is an error to pick a fence sequence
741 		 * number that is not available.
742 		 */
743 		submit->fence_id = args->fence;
744 		ret = idr_alloc_u32(&queue->fence_idr, submit->user_fence,
745 				    &submit->fence_id, submit->fence_id,
746 				    GFP_NOWAIT);
747 		/*
748 		 * We've already validated that the fence_id slot is valid,
749 		 * so if idr_alloc_u32 failed, it is a kernel bug
750 		 */
751 		WARN_ON(ret);
752 	} else {
753 		/*
754 		 * Allocate an id which can be used by WAIT_FENCE ioctl to map
755 		 * back to the underlying fence.
756 		 */
757 		submit->fence_id = idr_alloc_cyclic(&queue->fence_idr,
758 						    submit->user_fence, 1,
759 						    INT_MAX, GFP_NOWAIT);
760 	}
761 
762 	spin_unlock(&queue->idr_lock);
763 	idr_preload_end();
764 
765 	if (submit->fence_id < 0) {
766 		ret = submit->fence_id;
767 		submit->fence_id = 0;
768 	}
769 
770 	if (ret == 0 && args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
771 		sync_file = sync_file_create(submit->user_fence);
772 		if (!sync_file) {
773 			ret = -ENOMEM;
774 		} else {
775 			fd_install(out_fence_fd, sync_file->file);
776 			args->fence_fd = out_fence_fd;
777 		}
778 	}
779 
780 	if (ret)
781 		goto out;
782 
783 	submit_attach_object_fences(submit);
784 
785 	if (msm_context_is_vmbind(ctx)) {
786 		/*
787 		 * If we are not using VM_BIND, submit_pin_vmas() will validate
788 		 * just the BOs attached to the submit.  In that case we don't
789 		 * need to validate the _entire_ vm, because userspace tracked
790 		 * what BOs are associated with the submit.
791 		 */
792 		ret = drm_gpuvm_validate(submit->vm, &submit->exec);
793 		if (ret)
794 			goto out;
795 	}
796 
797 	/* The scheduler owns a ref now: */
798 	msm_gem_submit_get(submit);
799 
800 	msm_rd_dump_submit(priv->rd, submit, NULL);
801 
802 	drm_sched_entity_push_job(&submit->base);
803 
804 	args->fence = submit->fence_id;
805 	queue->last_fence = submit->fence_id;
806 
807 	msm_syncobj_reset(syncobjs_to_reset, args->nr_in_syncobjs);
808 	msm_syncobj_process_post_deps(post_deps, args->nr_out_syncobjs, submit->user_fence);
809 
810 out:
811 	submit_cleanup(submit, !!ret);
812 out_unlock:
813 	mutex_unlock(&queue->lock);
814 out_post_unlock:
815 	if (ret && (out_fence_fd >= 0)) {
816 		put_unused_fd(out_fence_fd);
817 		if (sync_file)
818 			fput(sync_file->file);
819 	}
820 
821 	if (!IS_ERR_OR_NULL(submit)) {
822 		msm_gem_submit_put(submit);
823 	} else {
824 		/*
825 		 * If the submit hasn't yet taken ownership of the queue
826 		 * then we need to drop the reference ourself:
827 		 */
828 		msm_submitqueue_put(queue);
829 	}
830 	if (!IS_ERR_OR_NULL(post_deps)) {
831 		for (i = 0; i < args->nr_out_syncobjs; ++i) {
832 			kfree(post_deps[i].chain);
833 			drm_syncobj_put(post_deps[i].syncobj);
834 		}
835 		kfree(post_deps);
836 	}
837 
838 	if (!IS_ERR_OR_NULL(syncobjs_to_reset)) {
839 		for (i = 0; i < args->nr_in_syncobjs; ++i) {
840 			if (syncobjs_to_reset[i])
841 				drm_syncobj_put(syncobjs_to_reset[i]);
842 		}
843 		kfree(syncobjs_to_reset);
844 	}
845 
846 	return ret;
847 }
848