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/file.h>
8 #include <linux/sync_file.h>
9 #include <linux/uaccess.h>
10 
11 #include <drm/drm_drv.h>
12 #include <drm/drm_file.h>
13 #include <drm/drm_syncobj.h>
14 
15 #include "msm_drv.h"
16 #include "msm_gpu.h"
17 #include "msm_gem.h"
18 #include "msm_gpu_trace.h"
19 
20 /*
21  * Cmdstream submission:
22  */
23 
24 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
25 #define BO_VALID    0x8000   /* is current addr in cmdstream correct/valid? */
26 #define BO_LOCKED   0x4000
27 #define BO_PINNED   0x2000
28 
29 static struct msm_gem_submit *submit_create(struct drm_device *dev,
30 		struct msm_gpu *gpu,
31 		struct msm_gpu_submitqueue *queue, uint32_t nr_bos,
32 		uint32_t nr_cmds)
33 {
34 	struct msm_gem_submit *submit;
35 	uint64_t sz = struct_size(submit, bos, nr_bos) +
36 				  ((u64)nr_cmds * sizeof(submit->cmd[0]));
37 
38 	if (sz > SIZE_MAX)
39 		return NULL;
40 
41 	submit = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
42 	if (!submit)
43 		return NULL;
44 
45 	kref_init(&submit->ref);
46 	submit->dev = dev;
47 	submit->aspace = queue->ctx->aspace;
48 	submit->gpu = gpu;
49 	submit->fence = NULL;
50 	submit->cmd = (void *)&submit->bos[nr_bos];
51 	submit->queue = queue;
52 	submit->ring = gpu->rb[queue->prio];
53 
54 	/* initially, until copy_from_user() and bo lookup succeeds: */
55 	submit->nr_bos = 0;
56 	submit->nr_cmds = 0;
57 
58 	INIT_LIST_HEAD(&submit->node);
59 	INIT_LIST_HEAD(&submit->bo_list);
60 
61 	return submit;
62 }
63 
64 void __msm_gem_submit_destroy(struct kref *kref)
65 {
66 	struct msm_gem_submit *submit =
67 			container_of(kref, struct msm_gem_submit, ref);
68 	unsigned i;
69 
70 	dma_fence_put(submit->fence);
71 	put_pid(submit->pid);
72 	msm_submitqueue_put(submit->queue);
73 
74 	for (i = 0; i < submit->nr_cmds; i++)
75 		kfree(submit->cmd[i].relocs);
76 
77 	kfree(submit);
78 }
79 
80 static int submit_lookup_objects(struct msm_gem_submit *submit,
81 		struct drm_msm_gem_submit *args, struct drm_file *file)
82 {
83 	unsigned i;
84 	int ret = 0;
85 
86 	for (i = 0; i < args->nr_bos; i++) {
87 		struct drm_msm_gem_submit_bo submit_bo;
88 		void __user *userptr =
89 			u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
90 
91 		/* make sure we don't have garbage flags, in case we hit
92 		 * error path before flags is initialized:
93 		 */
94 		submit->bos[i].flags = 0;
95 
96 		if (copy_from_user(&submit_bo, userptr, sizeof(submit_bo))) {
97 			ret = -EFAULT;
98 			i = 0;
99 			goto out;
100 		}
101 
102 /* at least one of READ and/or WRITE flags should be set: */
103 #define MANDATORY_FLAGS (MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_WRITE)
104 
105 		if ((submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) ||
106 			!(submit_bo.flags & MANDATORY_FLAGS)) {
107 			DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
108 			ret = -EINVAL;
109 			i = 0;
110 			goto out;
111 		}
112 
113 		submit->bos[i].handle = submit_bo.handle;
114 		submit->bos[i].flags = submit_bo.flags;
115 		/* in validate_objects() we figure out if this is true: */
116 		submit->bos[i].iova  = submit_bo.presumed;
117 	}
118 
119 	spin_lock(&file->table_lock);
120 
121 	for (i = 0; i < args->nr_bos; i++) {
122 		struct drm_gem_object *obj;
123 		struct msm_gem_object *msm_obj;
124 
125 		/* normally use drm_gem_object_lookup(), but for bulk lookup
126 		 * all under single table_lock just hit object_idr directly:
127 		 */
128 		obj = idr_find(&file->object_idr, submit->bos[i].handle);
129 		if (!obj) {
130 			DRM_ERROR("invalid handle %u at index %u\n", submit->bos[i].handle, i);
131 			ret = -EINVAL;
132 			goto out_unlock;
133 		}
134 
135 		msm_obj = to_msm_bo(obj);
136 
137 		if (!list_empty(&msm_obj->submit_entry)) {
138 			DRM_ERROR("handle %u at index %u already on submit list\n",
139 					submit->bos[i].handle, i);
140 			ret = -EINVAL;
141 			goto out_unlock;
142 		}
143 
144 		drm_gem_object_get(obj);
145 
146 		submit->bos[i].obj = msm_obj;
147 
148 		list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
149 	}
150 
151 out_unlock:
152 	spin_unlock(&file->table_lock);
153 
154 out:
155 	submit->nr_bos = i;
156 
157 	return ret;
158 }
159 
160 static int submit_lookup_cmds(struct msm_gem_submit *submit,
161 		struct drm_msm_gem_submit *args, struct drm_file *file)
162 {
163 	unsigned i, sz;
164 	int ret = 0;
165 
166 	for (i = 0; i < args->nr_cmds; i++) {
167 		struct drm_msm_gem_submit_cmd submit_cmd;
168 		void __user *userptr =
169 			u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
170 
171 		ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
172 		if (ret) {
173 			ret = -EFAULT;
174 			goto out;
175 		}
176 
177 		/* validate input from userspace: */
178 		switch (submit_cmd.type) {
179 		case MSM_SUBMIT_CMD_BUF:
180 		case MSM_SUBMIT_CMD_IB_TARGET_BUF:
181 		case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
182 			break;
183 		default:
184 			DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
185 			return -EINVAL;
186 		}
187 
188 		if (submit_cmd.size % 4) {
189 			DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
190 					submit_cmd.size);
191 			ret = -EINVAL;
192 			goto out;
193 		}
194 
195 		submit->cmd[i].type = submit_cmd.type;
196 		submit->cmd[i].size = submit_cmd.size / 4;
197 		submit->cmd[i].offset = submit_cmd.submit_offset / 4;
198 		submit->cmd[i].idx  = submit_cmd.submit_idx;
199 		submit->cmd[i].nr_relocs = submit_cmd.nr_relocs;
200 
201 		sz = array_size(submit_cmd.nr_relocs,
202 				sizeof(struct drm_msm_gem_submit_reloc));
203 		/* check for overflow: */
204 		if (sz == SIZE_MAX) {
205 			ret = -ENOMEM;
206 			goto out;
207 		}
208 		submit->cmd[i].relocs = kmalloc(sz, GFP_KERNEL);
209 		ret = copy_from_user(submit->cmd[i].relocs, userptr, sz);
210 		if (ret) {
211 			ret = -EFAULT;
212 			goto out;
213 		}
214 	}
215 
216 out:
217 	return ret;
218 }
219 
220 static void submit_unlock_unpin_bo(struct msm_gem_submit *submit,
221 		int i, bool backoff)
222 {
223 	struct msm_gem_object *msm_obj = submit->bos[i].obj;
224 
225 	if (submit->bos[i].flags & BO_PINNED)
226 		msm_gem_unpin_iova_locked(&msm_obj->base, submit->aspace);
227 
228 	if (submit->bos[i].flags & BO_LOCKED)
229 		dma_resv_unlock(msm_obj->base.resv);
230 
231 	if (backoff && !(submit->bos[i].flags & BO_VALID))
232 		submit->bos[i].iova = 0;
233 
234 	submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
235 }
236 
237 /* This is where we make sure all the bo's are reserved and pin'd: */
238 static int submit_lock_objects(struct msm_gem_submit *submit)
239 {
240 	int contended, slow_locked = -1, i, ret = 0;
241 
242 retry:
243 	for (i = 0; i < submit->nr_bos; i++) {
244 		struct msm_gem_object *msm_obj = submit->bos[i].obj;
245 
246 		if (slow_locked == i)
247 			slow_locked = -1;
248 
249 		contended = i;
250 
251 		if (!(submit->bos[i].flags & BO_LOCKED)) {
252 			ret = dma_resv_lock_interruptible(msm_obj->base.resv,
253 							  &submit->ticket);
254 			if (ret)
255 				goto fail;
256 			submit->bos[i].flags |= BO_LOCKED;
257 		}
258 	}
259 
260 	ww_acquire_done(&submit->ticket);
261 
262 	return 0;
263 
264 fail:
265 	for (; i >= 0; i--)
266 		submit_unlock_unpin_bo(submit, i, true);
267 
268 	if (slow_locked > 0)
269 		submit_unlock_unpin_bo(submit, slow_locked, true);
270 
271 	if (ret == -EDEADLK) {
272 		struct msm_gem_object *msm_obj = submit->bos[contended].obj;
273 		/* we lost out in a seqno race, lock and retry.. */
274 		ret = dma_resv_lock_slow_interruptible(msm_obj->base.resv,
275 						       &submit->ticket);
276 		if (!ret) {
277 			submit->bos[contended].flags |= BO_LOCKED;
278 			slow_locked = contended;
279 			goto retry;
280 		}
281 	}
282 
283 	return ret;
284 }
285 
286 static int submit_fence_sync(struct msm_gem_submit *submit, bool no_implicit)
287 {
288 	int i, ret = 0;
289 
290 	for (i = 0; i < submit->nr_bos; i++) {
291 		struct msm_gem_object *msm_obj = submit->bos[i].obj;
292 		bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
293 
294 		if (!write) {
295 			/* NOTE: _reserve_shared() must happen before
296 			 * _add_shared_fence(), which makes this a slightly
297 			 * strange place to call it.  OTOH this is a
298 			 * convenient can-fail point to hook it in.
299 			 */
300 			ret = dma_resv_reserve_shared(msm_obj->base.resv,
301 								1);
302 			if (ret)
303 				return ret;
304 		}
305 
306 		if (no_implicit)
307 			continue;
308 
309 		ret = msm_gem_sync_object(&msm_obj->base, submit->ring->fctx,
310 			write);
311 		if (ret)
312 			break;
313 	}
314 
315 	return ret;
316 }
317 
318 static int submit_pin_objects(struct msm_gem_submit *submit)
319 {
320 	int i, ret = 0;
321 
322 	submit->valid = true;
323 
324 	for (i = 0; i < submit->nr_bos; i++) {
325 		struct msm_gem_object *msm_obj = submit->bos[i].obj;
326 		uint64_t iova;
327 
328 		/* if locking succeeded, pin bo: */
329 		ret = msm_gem_get_and_pin_iova_locked(&msm_obj->base,
330 				submit->aspace, &iova);
331 
332 		if (ret)
333 			break;
334 
335 		submit->bos[i].flags |= BO_PINNED;
336 
337 		if (iova == submit->bos[i].iova) {
338 			submit->bos[i].flags |= BO_VALID;
339 		} else {
340 			submit->bos[i].iova = iova;
341 			/* iova changed, so address in cmdstream is not valid: */
342 			submit->bos[i].flags &= ~BO_VALID;
343 			submit->valid = false;
344 		}
345 	}
346 
347 	return ret;
348 }
349 
350 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
351 		struct msm_gem_object **obj, uint64_t *iova, bool *valid)
352 {
353 	if (idx >= submit->nr_bos) {
354 		DRM_ERROR("invalid buffer index: %u (out of %u)\n",
355 				idx, submit->nr_bos);
356 		return -EINVAL;
357 	}
358 
359 	if (obj)
360 		*obj = submit->bos[idx].obj;
361 	if (iova)
362 		*iova = submit->bos[idx].iova;
363 	if (valid)
364 		*valid = !!(submit->bos[idx].flags & BO_VALID);
365 
366 	return 0;
367 }
368 
369 /* process the reloc's and patch up the cmdstream as needed: */
370 static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
371 		uint32_t offset, uint32_t nr_relocs, struct drm_msm_gem_submit_reloc *relocs)
372 {
373 	uint32_t i, last_offset = 0;
374 	uint32_t *ptr;
375 	int ret = 0;
376 
377 	if (!nr_relocs)
378 		return 0;
379 
380 	if (offset % 4) {
381 		DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
382 		return -EINVAL;
383 	}
384 
385 	/* For now, just map the entire thing.  Eventually we probably
386 	 * to do it page-by-page, w/ kmap() if not vmap()d..
387 	 */
388 	ptr = msm_gem_get_vaddr_locked(&obj->base);
389 
390 	if (IS_ERR(ptr)) {
391 		ret = PTR_ERR(ptr);
392 		DBG("failed to map: %d", ret);
393 		return ret;
394 	}
395 
396 	for (i = 0; i < nr_relocs; i++) {
397 		struct drm_msm_gem_submit_reloc submit_reloc = relocs[i];
398 		uint32_t off;
399 		uint64_t iova;
400 		bool valid;
401 
402 		if (submit_reloc.submit_offset % 4) {
403 			DRM_ERROR("non-aligned reloc offset: %u\n",
404 					submit_reloc.submit_offset);
405 			ret = -EINVAL;
406 			goto out;
407 		}
408 
409 		/* offset in dwords: */
410 		off = submit_reloc.submit_offset / 4;
411 
412 		if ((off >= (obj->base.size / 4)) ||
413 				(off < last_offset)) {
414 			DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
415 			ret = -EINVAL;
416 			goto out;
417 		}
418 
419 		ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
420 		if (ret)
421 			goto out;
422 
423 		if (valid)
424 			continue;
425 
426 		iova += submit_reloc.reloc_offset;
427 
428 		if (submit_reloc.shift < 0)
429 			iova >>= -submit_reloc.shift;
430 		else
431 			iova <<= submit_reloc.shift;
432 
433 		ptr[off] = iova | submit_reloc.or;
434 
435 		last_offset = off;
436 	}
437 
438 out:
439 	msm_gem_put_vaddr_locked(&obj->base);
440 
441 	return ret;
442 }
443 
444 static void submit_cleanup(struct msm_gem_submit *submit)
445 {
446 	unsigned i;
447 
448 	for (i = 0; i < submit->nr_bos; i++) {
449 		struct msm_gem_object *msm_obj = submit->bos[i].obj;
450 		submit_unlock_unpin_bo(submit, i, false);
451 		list_del_init(&msm_obj->submit_entry);
452 		drm_gem_object_put_locked(&msm_obj->base);
453 	}
454 }
455 
456 
457 struct msm_submit_post_dep {
458 	struct drm_syncobj *syncobj;
459 	uint64_t point;
460 	struct dma_fence_chain *chain;
461 };
462 
463 static struct drm_syncobj **msm_wait_deps(struct drm_device *dev,
464                                           struct drm_file *file,
465                                           uint64_t in_syncobjs_addr,
466                                           uint32_t nr_in_syncobjs,
467                                           size_t syncobj_stride,
468                                           struct msm_ringbuffer *ring)
469 {
470 	struct drm_syncobj **syncobjs = NULL;
471 	struct drm_msm_gem_submit_syncobj syncobj_desc = {0};
472 	int ret = 0;
473 	uint32_t i, j;
474 
475 	syncobjs = kcalloc(nr_in_syncobjs, sizeof(*syncobjs),
476 	                   GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
477 	if (!syncobjs)
478 		return ERR_PTR(-ENOMEM);
479 
480 	for (i = 0; i < nr_in_syncobjs; ++i) {
481 		uint64_t address = in_syncobjs_addr + i * syncobj_stride;
482 		struct dma_fence *fence;
483 
484 		if (copy_from_user(&syncobj_desc,
485 			           u64_to_user_ptr(address),
486 			           min(syncobj_stride, sizeof(syncobj_desc)))) {
487 			ret = -EFAULT;
488 			break;
489 		}
490 
491 		if (syncobj_desc.point &&
492 		    !drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE)) {
493 			ret = -EOPNOTSUPP;
494 			break;
495 		}
496 
497 		if (syncobj_desc.flags & ~MSM_SUBMIT_SYNCOBJ_FLAGS) {
498 			ret = -EINVAL;
499 			break;
500 		}
501 
502 		ret = drm_syncobj_find_fence(file, syncobj_desc.handle,
503 		                             syncobj_desc.point, 0, &fence);
504 		if (ret)
505 			break;
506 
507 		if (!dma_fence_match_context(fence, ring->fctx->context))
508 			ret = dma_fence_wait(fence, true);
509 
510 		dma_fence_put(fence);
511 		if (ret)
512 			break;
513 
514 		if (syncobj_desc.flags & MSM_SUBMIT_SYNCOBJ_RESET) {
515 			syncobjs[i] =
516 				drm_syncobj_find(file, syncobj_desc.handle);
517 			if (!syncobjs[i]) {
518 				ret = -EINVAL;
519 				break;
520 			}
521 		}
522 	}
523 
524 	if (ret) {
525 		for (j = 0; j <= i; ++j) {
526 			if (syncobjs[j])
527 				drm_syncobj_put(syncobjs[j]);
528 		}
529 		kfree(syncobjs);
530 		return ERR_PTR(ret);
531 	}
532 	return syncobjs;
533 }
534 
535 static void msm_reset_syncobjs(struct drm_syncobj **syncobjs,
536                                uint32_t nr_syncobjs)
537 {
538 	uint32_t i;
539 
540 	for (i = 0; syncobjs && i < nr_syncobjs; ++i) {
541 		if (syncobjs[i])
542 			drm_syncobj_replace_fence(syncobjs[i], NULL);
543 	}
544 }
545 
546 static struct msm_submit_post_dep *msm_parse_post_deps(struct drm_device *dev,
547                                                        struct drm_file *file,
548                                                        uint64_t syncobjs_addr,
549                                                        uint32_t nr_syncobjs,
550                                                        size_t syncobj_stride)
551 {
552 	struct msm_submit_post_dep *post_deps;
553 	struct drm_msm_gem_submit_syncobj syncobj_desc = {0};
554 	int ret = 0;
555 	uint32_t i, j;
556 
557 	post_deps = kmalloc_array(nr_syncobjs, sizeof(*post_deps),
558 	                          GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
559 	if (!post_deps)
560 		return ERR_PTR(-ENOMEM);
561 
562 	for (i = 0; i < nr_syncobjs; ++i) {
563 		uint64_t address = syncobjs_addr + i * syncobj_stride;
564 
565 		if (copy_from_user(&syncobj_desc,
566 			           u64_to_user_ptr(address),
567 			           min(syncobj_stride, sizeof(syncobj_desc)))) {
568 			ret = -EFAULT;
569 			break;
570 		}
571 
572 		post_deps[i].point = syncobj_desc.point;
573 		post_deps[i].chain = NULL;
574 
575 		if (syncobj_desc.flags) {
576 			ret = -EINVAL;
577 			break;
578 		}
579 
580 		if (syncobj_desc.point) {
581 			if (!drm_core_check_feature(dev,
582 			                            DRIVER_SYNCOBJ_TIMELINE)) {
583 				ret = -EOPNOTSUPP;
584 				break;
585 			}
586 
587 			post_deps[i].chain =
588 				kmalloc(sizeof(*post_deps[i].chain),
589 				        GFP_KERNEL);
590 			if (!post_deps[i].chain) {
591 				ret = -ENOMEM;
592 				break;
593 			}
594 		}
595 
596 		post_deps[i].syncobj =
597 			drm_syncobj_find(file, syncobj_desc.handle);
598 		if (!post_deps[i].syncobj) {
599 			ret = -EINVAL;
600 			break;
601 		}
602 	}
603 
604 	if (ret) {
605 		for (j = 0; j <= i; ++j) {
606 			kfree(post_deps[j].chain);
607 			if (post_deps[j].syncobj)
608 				drm_syncobj_put(post_deps[j].syncobj);
609 		}
610 
611 		kfree(post_deps);
612 		return ERR_PTR(ret);
613 	}
614 
615 	return post_deps;
616 }
617 
618 static void msm_process_post_deps(struct msm_submit_post_dep *post_deps,
619                                   uint32_t count, struct dma_fence *fence)
620 {
621 	uint32_t i;
622 
623 	for (i = 0; post_deps && i < count; ++i) {
624 		if (post_deps[i].chain) {
625 			drm_syncobj_add_point(post_deps[i].syncobj,
626 			                      post_deps[i].chain,
627 			                      fence, post_deps[i].point);
628 			post_deps[i].chain = NULL;
629 		} else {
630 			drm_syncobj_replace_fence(post_deps[i].syncobj,
631 			                          fence);
632 		}
633 	}
634 }
635 
636 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
637 		struct drm_file *file)
638 {
639 	static atomic_t ident = ATOMIC_INIT(0);
640 	struct msm_drm_private *priv = dev->dev_private;
641 	struct drm_msm_gem_submit *args = data;
642 	struct msm_file_private *ctx = file->driver_priv;
643 	struct msm_gem_submit *submit;
644 	struct msm_gpu *gpu = priv->gpu;
645 	struct sync_file *sync_file = NULL;
646 	struct msm_gpu_submitqueue *queue;
647 	struct msm_ringbuffer *ring;
648 	struct msm_submit_post_dep *post_deps = NULL;
649 	struct drm_syncobj **syncobjs_to_reset = NULL;
650 	int out_fence_fd = -1;
651 	struct pid *pid = get_pid(task_pid(current));
652 	bool has_ww_ticket = false;
653 	unsigned i;
654 	int ret, submitid;
655 	if (!gpu)
656 		return -ENXIO;
657 
658 	if (args->pad)
659 		return -EINVAL;
660 
661 	/* for now, we just have 3d pipe.. eventually this would need to
662 	 * be more clever to dispatch to appropriate gpu module:
663 	 */
664 	if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
665 		return -EINVAL;
666 
667 	if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
668 		return -EINVAL;
669 
670 	if (args->flags & MSM_SUBMIT_SUDO) {
671 		if (!IS_ENABLED(CONFIG_DRM_MSM_GPU_SUDO) ||
672 		    !capable(CAP_SYS_RAWIO))
673 			return -EINVAL;
674 	}
675 
676 	queue = msm_submitqueue_get(ctx, args->queueid);
677 	if (!queue)
678 		return -ENOENT;
679 
680 	/* Get a unique identifier for the submission for logging purposes */
681 	submitid = atomic_inc_return(&ident) - 1;
682 
683 	ring = gpu->rb[queue->prio];
684 	trace_msm_gpu_submit(pid_nr(pid), ring->id, submitid,
685 		args->nr_bos, args->nr_cmds);
686 
687 	if (args->flags & MSM_SUBMIT_FENCE_FD_IN) {
688 		struct dma_fence *in_fence;
689 
690 		in_fence = sync_file_get_fence(args->fence_fd);
691 
692 		if (!in_fence)
693 			return -EINVAL;
694 
695 		/*
696 		 * Wait if the fence is from a foreign context, or if the fence
697 		 * array contains any fence from a foreign context.
698 		 */
699 		ret = 0;
700 		if (!dma_fence_match_context(in_fence, ring->fctx->context))
701 			ret = dma_fence_wait(in_fence, true);
702 
703 		dma_fence_put(in_fence);
704 		if (ret)
705 			return ret;
706 	}
707 
708 	if (args->flags & MSM_SUBMIT_SYNCOBJ_IN) {
709 		syncobjs_to_reset = msm_wait_deps(dev, file,
710 		                                  args->in_syncobjs,
711 		                                  args->nr_in_syncobjs,
712 		                                  args->syncobj_stride, ring);
713 		if (IS_ERR(syncobjs_to_reset))
714 			return PTR_ERR(syncobjs_to_reset);
715 	}
716 
717 	if (args->flags & MSM_SUBMIT_SYNCOBJ_OUT) {
718 		post_deps = msm_parse_post_deps(dev, file,
719 		                                args->out_syncobjs,
720 		                                args->nr_out_syncobjs,
721 		                                args->syncobj_stride);
722 		if (IS_ERR(post_deps)) {
723 			ret = PTR_ERR(post_deps);
724 			goto out_post_unlock;
725 		}
726 	}
727 
728 	ret = mutex_lock_interruptible(&dev->struct_mutex);
729 	if (ret)
730 		goto out_post_unlock;
731 
732 	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
733 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
734 		if (out_fence_fd < 0) {
735 			ret = out_fence_fd;
736 			goto out_unlock;
737 		}
738 	}
739 
740 	submit = submit_create(dev, gpu, queue, args->nr_bos,
741 		args->nr_cmds);
742 	if (!submit) {
743 		ret = -ENOMEM;
744 		goto out_unlock;
745 	}
746 
747 	submit->pid = pid;
748 	submit->ident = submitid;
749 
750 	if (args->flags & MSM_SUBMIT_SUDO)
751 		submit->in_rb = true;
752 
753 	ret = submit_lookup_objects(submit, args, file);
754 	if (ret)
755 		goto out_pre_pm;
756 
757 	ret = submit_lookup_cmds(submit, args, file);
758 	if (ret)
759 		goto out_pre_pm;
760 
761 	/*
762 	 * Thanks to dev_pm_opp opp_table_lock interactions with mm->mmap_sem
763 	 * in the resume path, we need to to rpm get before we lock objs.
764 	 * Which unfortunately might involve powering up the GPU sooner than
765 	 * is necessary.  But at least in the explicit fencing case, we will
766 	 * have already done all the fence waiting.
767 	 */
768 	pm_runtime_get_sync(&gpu->pdev->dev);
769 
770 	/* copy_*_user while holding a ww ticket upsets lockdep */
771 	ww_acquire_init(&submit->ticket, &reservation_ww_class);
772 	has_ww_ticket = true;
773 	ret = submit_lock_objects(submit);
774 	if (ret)
775 		goto out;
776 
777 	ret = submit_fence_sync(submit, !!(args->flags & MSM_SUBMIT_NO_IMPLICIT));
778 	if (ret)
779 		goto out;
780 
781 	ret = submit_pin_objects(submit);
782 	if (ret)
783 		goto out;
784 
785 	for (i = 0; i < args->nr_cmds; i++) {
786 		struct msm_gem_object *msm_obj;
787 		uint64_t iova;
788 
789 		ret = submit_bo(submit, submit->cmd[i].idx,
790 				&msm_obj, &iova, NULL);
791 		if (ret)
792 			goto out;
793 
794 		if (!submit->cmd[i].size ||
795 			((submit->cmd[i].size + submit->cmd[i].offset) >
796 				msm_obj->base.size / 4)) {
797 			DRM_ERROR("invalid cmdstream size: %u\n", submit->cmd[i].size * 4);
798 			ret = -EINVAL;
799 			goto out;
800 		}
801 
802 		submit->cmd[i].iova = iova + (submit->cmd[i].offset * 4);
803 
804 		if (submit->valid)
805 			continue;
806 
807 		ret = submit_reloc(submit, msm_obj, submit->cmd[i].offset * 4,
808 				submit->cmd[i].nr_relocs, submit->cmd[i].relocs);
809 		if (ret)
810 			goto out;
811 	}
812 
813 	submit->nr_cmds = i;
814 
815 	submit->fence = msm_fence_alloc(ring->fctx);
816 	if (IS_ERR(submit->fence)) {
817 		ret = PTR_ERR(submit->fence);
818 		submit->fence = NULL;
819 		goto out;
820 	}
821 
822 	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
823 		sync_file = sync_file_create(submit->fence);
824 		if (!sync_file) {
825 			ret = -ENOMEM;
826 			goto out;
827 		}
828 	}
829 
830 	msm_gpu_submit(gpu, submit);
831 
832 	args->fence = submit->fence->seqno;
833 
834 	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
835 		fd_install(out_fence_fd, sync_file->file);
836 		args->fence_fd = out_fence_fd;
837 	}
838 
839 	msm_reset_syncobjs(syncobjs_to_reset, args->nr_in_syncobjs);
840 	msm_process_post_deps(post_deps, args->nr_out_syncobjs,
841 	                      submit->fence);
842 
843 
844 out:
845 	pm_runtime_put(&gpu->pdev->dev);
846 out_pre_pm:
847 	submit_cleanup(submit);
848 	if (has_ww_ticket)
849 		ww_acquire_fini(&submit->ticket);
850 	msm_gem_submit_put(submit);
851 out_unlock:
852 	if (ret && (out_fence_fd >= 0))
853 		put_unused_fd(out_fence_fd);
854 	mutex_unlock(&dev->struct_mutex);
855 
856 out_post_unlock:
857 	if (!IS_ERR_OR_NULL(post_deps)) {
858 		for (i = 0; i < args->nr_out_syncobjs; ++i) {
859 			kfree(post_deps[i].chain);
860 			drm_syncobj_put(post_deps[i].syncobj);
861 		}
862 		kfree(post_deps);
863 	}
864 
865 	if (!IS_ERR_OR_NULL(syncobjs_to_reset)) {
866 		for (i = 0; i < args->nr_in_syncobjs; ++i) {
867 			if (syncobjs_to_reset[i])
868 				drm_syncobj_put(syncobjs_to_reset[i]);
869 		}
870 		kfree(syncobjs_to_reset);
871 	}
872 
873 	return ret;
874 }
875