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