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