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