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