1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
7 #include <linux/file.h>
8 #include <linux/sync_file.h>
9 #include <linux/uaccess.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_file.h>
13 #include <drm/drm_syncobj.h>
18 #include "msm_gpu_trace.h"
21 * Cmdstream submission:
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 */
30 static struct msm_gem_submit *submit_create(struct drm_device *dev,
32 struct msm_gpu_submitqueue *queue, uint32_t nr_bos,
35 struct msm_gem_submit *submit;
39 sz = struct_size(submit, bos, nr_bos) +
40 ((u64)nr_cmds * sizeof(submit->cmd[0]));
43 return ERR_PTR(-ENOMEM);
45 submit = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
47 return ERR_PTR(-ENOMEM);
49 ret = drm_sched_job_init(&submit->base, queue->entity, queue);
55 xa_init_flags(&submit->deps, XA_FLAGS_ALLOC);
57 kref_init(&submit->ref);
59 submit->aspace = queue->ctx->aspace;
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;
66 INIT_LIST_HEAD(&submit->node);
71 void __msm_gem_submit_destroy(struct kref *kref)
73 struct msm_gem_submit *submit =
74 container_of(kref, struct msm_gem_submit, ref);
76 struct dma_fence *fence;
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);
85 xa_for_each (&submit->deps, index, fence) {
89 xa_destroy(&submit->deps);
91 dma_fence_put(submit->user_fence);
92 dma_fence_put(submit->hw_fence);
95 msm_submitqueue_put(submit->queue);
97 for (i = 0; i < submit->nr_cmds; i++)
98 kfree(submit->cmd[i].relocs);
103 static int submit_lookup_objects(struct msm_gem_submit *submit,
104 struct drm_msm_gem_submit *args, struct drm_file *file)
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)));
114 /* make sure we don't have garbage flags, in case we hit
115 * error path before flags is initialized:
117 submit->bos[i].flags = 0;
119 if (copy_from_user(&submit_bo, userptr, sizeof(submit_bo))) {
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)
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);
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;
142 spin_lock(&file->table_lock);
144 for (i = 0; i < args->nr_bos; i++) {
145 struct drm_gem_object *obj;
147 /* normally use drm_gem_object_lookup(), but for bulk lookup
148 * all under single table_lock just hit object_idr directly:
150 obj = idr_find(&file->object_idr, submit->bos[i].handle);
152 DRM_ERROR("invalid handle %u at index %u\n", submit->bos[i].handle, i);
157 drm_gem_object_get(obj);
159 submit->bos[i].obj = to_msm_bo(obj);
163 spin_unlock(&file->table_lock);
171 static int submit_lookup_cmds(struct msm_gem_submit *submit,
172 struct drm_msm_gem_submit *args, struct drm_file *file)
178 for (i = 0; i < args->nr_cmds; i++) {
179 struct drm_msm_gem_submit_cmd submit_cmd;
180 void __user *userptr =
181 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
183 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
189 /* validate input from userspace: */
190 switch (submit_cmd.type) {
191 case MSM_SUBMIT_CMD_BUF:
192 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
193 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
196 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
200 if (submit_cmd.size % 4) {
201 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
207 submit->cmd[i].type = submit_cmd.type;
208 submit->cmd[i].size = submit_cmd.size / 4;
209 submit->cmd[i].offset = submit_cmd.submit_offset / 4;
210 submit->cmd[i].idx = submit_cmd.submit_idx;
211 submit->cmd[i].nr_relocs = submit_cmd.nr_relocs;
213 userptr = u64_to_user_ptr(submit_cmd.relocs);
215 sz = array_size(submit_cmd.nr_relocs,
216 sizeof(struct drm_msm_gem_submit_reloc));
217 /* check for overflow: */
218 if (sz == SIZE_MAX) {
222 submit->cmd[i].relocs = kmalloc(sz, GFP_KERNEL);
223 ret = copy_from_user(submit->cmd[i].relocs, userptr, sz);
234 /* Unwind bo state, according to cleanup_flags. In the success case, only
235 * the lock is dropped at the end of the submit (and active/pin ref is dropped
236 * later when the submit is retired).
238 static void submit_cleanup_bo(struct msm_gem_submit *submit, int i,
239 unsigned cleanup_flags)
241 struct drm_gem_object *obj = &submit->bos[i].obj->base;
242 unsigned flags = submit->bos[i].flags & cleanup_flags;
244 if (flags & BO_PINNED)
245 msm_gem_unpin_iova_locked(obj, submit->aspace);
247 if (flags & BO_ACTIVE)
248 msm_gem_active_put(obj);
250 if (flags & BO_LOCKED)
251 dma_resv_unlock(obj->resv);
253 submit->bos[i].flags &= ~cleanup_flags;
256 static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
258 submit_cleanup_bo(submit, i, BO_PINNED | BO_ACTIVE | BO_LOCKED);
260 if (!(submit->bos[i].flags & BO_VALID))
261 submit->bos[i].iova = 0;
264 /* This is where we make sure all the bo's are reserved and pin'd: */
265 static int submit_lock_objects(struct msm_gem_submit *submit)
267 int contended, slow_locked = -1, i, ret = 0;
270 for (i = 0; i < submit->nr_bos; i++) {
271 struct msm_gem_object *msm_obj = submit->bos[i].obj;
273 if (slow_locked == i)
278 if (!(submit->bos[i].flags & BO_LOCKED)) {
279 ret = dma_resv_lock_interruptible(msm_obj->base.resv,
283 submit->bos[i].flags |= BO_LOCKED;
287 ww_acquire_done(&submit->ticket);
292 if (ret == -EALREADY) {
293 DRM_ERROR("handle %u at index %u already on submit list\n",
294 submit->bos[i].handle, i);
299 submit_unlock_unpin_bo(submit, i);
302 submit_unlock_unpin_bo(submit, slow_locked);
304 if (ret == -EDEADLK) {
305 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
306 /* we lost out in a seqno race, lock and retry.. */
307 ret = dma_resv_lock_slow_interruptible(msm_obj->base.resv,
310 submit->bos[contended].flags |= BO_LOCKED;
311 slow_locked = contended;
315 /* Not expecting -EALREADY here, if the bo was already
316 * locked, we should have gotten -EALREADY already from
317 * the dma_resv_lock_interruptable() call.
319 WARN_ON_ONCE(ret == -EALREADY);
325 static int submit_fence_sync(struct msm_gem_submit *submit, bool no_implicit)
329 for (i = 0; i < submit->nr_bos; i++) {
330 struct drm_gem_object *obj = &submit->bos[i].obj->base;
331 bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
334 /* NOTE: _reserve_shared() must happen before
335 * _add_shared_fence(), which makes this a slightly
336 * strange place to call it. OTOH this is a
337 * convenient can-fail point to hook it in.
339 ret = dma_resv_reserve_shared(obj->resv, 1);
347 ret = drm_gem_fence_array_add_implicit(&submit->deps, obj,
356 static int submit_pin_objects(struct msm_gem_submit *submit)
360 submit->valid = true;
363 * Increment active_count first, so if under memory pressure, we
364 * don't inadvertently evict a bo needed by the submit in order
365 * to pin an earlier bo in the same submit.
367 for (i = 0; i < submit->nr_bos; i++) {
368 struct drm_gem_object *obj = &submit->bos[i].obj->base;
370 msm_gem_active_get(obj, submit->gpu);
371 submit->bos[i].flags |= BO_ACTIVE;
374 for (i = 0; i < submit->nr_bos; i++) {
375 struct drm_gem_object *obj = &submit->bos[i].obj->base;
378 /* if locking succeeded, pin bo: */
379 ret = msm_gem_get_and_pin_iova_locked(obj,
380 submit->aspace, &iova);
385 submit->bos[i].flags |= BO_PINNED;
387 if (iova == submit->bos[i].iova) {
388 submit->bos[i].flags |= BO_VALID;
390 submit->bos[i].iova = iova;
391 /* iova changed, so address in cmdstream is not valid: */
392 submit->bos[i].flags &= ~BO_VALID;
393 submit->valid = false;
400 static void submit_attach_object_fences(struct msm_gem_submit *submit)
404 for (i = 0; i < submit->nr_bos; i++) {
405 struct drm_gem_object *obj = &submit->bos[i].obj->base;
407 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
408 dma_resv_add_excl_fence(obj->resv, submit->user_fence);
409 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
410 dma_resv_add_shared_fence(obj->resv, submit->user_fence);
414 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
415 struct msm_gem_object **obj, uint64_t *iova, bool *valid)
417 if (idx >= submit->nr_bos) {
418 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
419 idx, submit->nr_bos);
424 *obj = submit->bos[idx].obj;
426 *iova = submit->bos[idx].iova;
428 *valid = !!(submit->bos[idx].flags & BO_VALID);
433 /* process the reloc's and patch up the cmdstream as needed: */
434 static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
435 uint32_t offset, uint32_t nr_relocs, struct drm_msm_gem_submit_reloc *relocs)
437 uint32_t i, last_offset = 0;
445 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
449 /* For now, just map the entire thing. Eventually we probably
450 * to do it page-by-page, w/ kmap() if not vmap()d..
452 ptr = msm_gem_get_vaddr_locked(&obj->base);
456 DBG("failed to map: %d", ret);
460 for (i = 0; i < nr_relocs; i++) {
461 struct drm_msm_gem_submit_reloc submit_reloc = relocs[i];
466 if (submit_reloc.submit_offset % 4) {
467 DRM_ERROR("non-aligned reloc offset: %u\n",
468 submit_reloc.submit_offset);
473 /* offset in dwords: */
474 off = submit_reloc.submit_offset / 4;
476 if ((off >= (obj->base.size / 4)) ||
477 (off < last_offset)) {
478 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
483 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
490 iova += submit_reloc.reloc_offset;
492 if (submit_reloc.shift < 0)
493 iova >>= -submit_reloc.shift;
495 iova <<= submit_reloc.shift;
497 ptr[off] = iova | submit_reloc.or;
503 msm_gem_put_vaddr_locked(&obj->base);
508 /* Cleanup submit at end of ioctl. In the error case, this also drops
509 * references, unpins, and drops active refcnt. In the non-error case,
510 * this is done when the submit is retired.
512 static void submit_cleanup(struct msm_gem_submit *submit, bool error)
514 unsigned cleanup_flags = BO_LOCKED;
518 cleanup_flags |= BO_PINNED | BO_ACTIVE;
520 for (i = 0; i < submit->nr_bos; i++) {
521 struct msm_gem_object *msm_obj = submit->bos[i].obj;
522 submit_cleanup_bo(submit, i, cleanup_flags);
524 drm_gem_object_put(&msm_obj->base);
528 void msm_submit_retire(struct msm_gem_submit *submit)
532 for (i = 0; i < submit->nr_bos; i++) {
533 struct drm_gem_object *obj = &submit->bos[i].obj->base;
536 submit_cleanup_bo(submit, i, BO_PINNED | BO_ACTIVE);
538 drm_gem_object_put(obj);
542 struct msm_submit_post_dep {
543 struct drm_syncobj *syncobj;
545 struct dma_fence_chain *chain;
548 static struct drm_syncobj **msm_parse_deps(struct msm_gem_submit *submit,
549 struct drm_file *file,
550 uint64_t in_syncobjs_addr,
551 uint32_t nr_in_syncobjs,
552 size_t syncobj_stride,
553 struct msm_ringbuffer *ring)
555 struct drm_syncobj **syncobjs = NULL;
556 struct drm_msm_gem_submit_syncobj syncobj_desc = {0};
560 syncobjs = kcalloc(nr_in_syncobjs, sizeof(*syncobjs),
561 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
563 return ERR_PTR(-ENOMEM);
565 for (i = 0; i < nr_in_syncobjs; ++i) {
566 uint64_t address = in_syncobjs_addr + i * syncobj_stride;
567 struct dma_fence *fence;
569 if (copy_from_user(&syncobj_desc,
570 u64_to_user_ptr(address),
571 min(syncobj_stride, sizeof(syncobj_desc)))) {
576 if (syncobj_desc.point &&
577 !drm_core_check_feature(submit->dev, DRIVER_SYNCOBJ_TIMELINE)) {
582 if (syncobj_desc.flags & ~MSM_SUBMIT_SYNCOBJ_FLAGS) {
587 ret = drm_syncobj_find_fence(file, syncobj_desc.handle,
588 syncobj_desc.point, 0, &fence);
592 ret = drm_gem_fence_array_add(&submit->deps, fence);
596 if (syncobj_desc.flags & MSM_SUBMIT_SYNCOBJ_RESET) {
598 drm_syncobj_find(file, syncobj_desc.handle);
607 for (j = 0; j <= i; ++j) {
609 drm_syncobj_put(syncobjs[j]);
617 static void msm_reset_syncobjs(struct drm_syncobj **syncobjs,
618 uint32_t nr_syncobjs)
622 for (i = 0; syncobjs && i < nr_syncobjs; ++i) {
624 drm_syncobj_replace_fence(syncobjs[i], NULL);
628 static struct msm_submit_post_dep *msm_parse_post_deps(struct drm_device *dev,
629 struct drm_file *file,
630 uint64_t syncobjs_addr,
631 uint32_t nr_syncobjs,
632 size_t syncobj_stride)
634 struct msm_submit_post_dep *post_deps;
635 struct drm_msm_gem_submit_syncobj syncobj_desc = {0};
639 post_deps = kmalloc_array(nr_syncobjs, sizeof(*post_deps),
640 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
642 return ERR_PTR(-ENOMEM);
644 for (i = 0; i < nr_syncobjs; ++i) {
645 uint64_t address = syncobjs_addr + i * syncobj_stride;
647 if (copy_from_user(&syncobj_desc,
648 u64_to_user_ptr(address),
649 min(syncobj_stride, sizeof(syncobj_desc)))) {
654 post_deps[i].point = syncobj_desc.point;
655 post_deps[i].chain = NULL;
657 if (syncobj_desc.flags) {
662 if (syncobj_desc.point) {
663 if (!drm_core_check_feature(dev,
664 DRIVER_SYNCOBJ_TIMELINE)) {
669 post_deps[i].chain = dma_fence_chain_alloc();
670 if (!post_deps[i].chain) {
676 post_deps[i].syncobj =
677 drm_syncobj_find(file, syncobj_desc.handle);
678 if (!post_deps[i].syncobj) {
685 for (j = 0; j <= i; ++j) {
686 dma_fence_chain_free(post_deps[j].chain);
687 if (post_deps[j].syncobj)
688 drm_syncobj_put(post_deps[j].syncobj);
698 static void msm_process_post_deps(struct msm_submit_post_dep *post_deps,
699 uint32_t count, struct dma_fence *fence)
703 for (i = 0; post_deps && i < count; ++i) {
704 if (post_deps[i].chain) {
705 drm_syncobj_add_point(post_deps[i].syncobj,
707 fence, post_deps[i].point);
708 post_deps[i].chain = NULL;
710 drm_syncobj_replace_fence(post_deps[i].syncobj,
716 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
717 struct drm_file *file)
719 static atomic_t ident = ATOMIC_INIT(0);
720 struct msm_drm_private *priv = dev->dev_private;
721 struct drm_msm_gem_submit *args = data;
722 struct msm_file_private *ctx = file->driver_priv;
723 struct msm_gem_submit *submit = NULL;
724 struct msm_gpu *gpu = priv->gpu;
725 struct msm_gpu_submitqueue *queue;
726 struct msm_ringbuffer *ring;
727 struct msm_submit_post_dep *post_deps = NULL;
728 struct drm_syncobj **syncobjs_to_reset = NULL;
729 int out_fence_fd = -1;
730 struct pid *pid = get_pid(task_pid(current));
731 bool has_ww_ticket = false;
741 /* for now, we just have 3d pipe.. eventually this would need to
742 * be more clever to dispatch to appropriate gpu module:
744 if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
747 if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
750 if (args->flags & MSM_SUBMIT_SUDO) {
751 if (!IS_ENABLED(CONFIG_DRM_MSM_GPU_SUDO) ||
752 !capable(CAP_SYS_RAWIO))
756 queue = msm_submitqueue_get(ctx, args->queueid);
760 /* Get a unique identifier for the submission for logging purposes */
761 submitid = atomic_inc_return(&ident) - 1;
763 ring = gpu->rb[queue->ring_nr];
764 trace_msm_gpu_submit(pid_nr(pid), ring->id, submitid,
765 args->nr_bos, args->nr_cmds);
767 ret = mutex_lock_interruptible(&queue->lock);
769 goto out_post_unlock;
771 if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
772 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
773 if (out_fence_fd < 0) {
779 submit = submit_create(dev, gpu, queue, args->nr_bos,
781 if (IS_ERR(submit)) {
782 ret = PTR_ERR(submit);
787 submit->ident = submitid;
789 if (args->flags & MSM_SUBMIT_SUDO)
790 submit->in_rb = true;
792 if (args->flags & MSM_SUBMIT_FENCE_FD_IN) {
793 struct dma_fence *in_fence;
795 in_fence = sync_file_get_fence(args->fence_fd);
802 ret = drm_gem_fence_array_add(&submit->deps, in_fence);
807 if (args->flags & MSM_SUBMIT_SYNCOBJ_IN) {
808 syncobjs_to_reset = msm_parse_deps(submit, file,
810 args->nr_in_syncobjs,
811 args->syncobj_stride, ring);
812 if (IS_ERR(syncobjs_to_reset)) {
813 ret = PTR_ERR(syncobjs_to_reset);
818 if (args->flags & MSM_SUBMIT_SYNCOBJ_OUT) {
819 post_deps = msm_parse_post_deps(dev, file,
821 args->nr_out_syncobjs,
822 args->syncobj_stride);
823 if (IS_ERR(post_deps)) {
824 ret = PTR_ERR(post_deps);
829 ret = submit_lookup_objects(submit, args, file);
833 ret = submit_lookup_cmds(submit, args, file);
837 /* copy_*_user while holding a ww ticket upsets lockdep */
838 ww_acquire_init(&submit->ticket, &reservation_ww_class);
839 has_ww_ticket = true;
840 ret = submit_lock_objects(submit);
844 ret = submit_fence_sync(submit, !!(args->flags & MSM_SUBMIT_NO_IMPLICIT));
848 ret = submit_pin_objects(submit);
852 for (i = 0; i < args->nr_cmds; i++) {
853 struct msm_gem_object *msm_obj;
856 ret = submit_bo(submit, submit->cmd[i].idx,
857 &msm_obj, &iova, NULL);
861 if (!submit->cmd[i].size ||
862 ((submit->cmd[i].size + submit->cmd[i].offset) >
863 msm_obj->base.size / 4)) {
864 DRM_ERROR("invalid cmdstream size: %u\n", submit->cmd[i].size * 4);
869 submit->cmd[i].iova = iova + (submit->cmd[i].offset * 4);
874 ret = submit_reloc(submit, msm_obj, submit->cmd[i].offset * 4,
875 submit->cmd[i].nr_relocs, submit->cmd[i].relocs);
882 submit->user_fence = dma_fence_get(&submit->base.s_fence->finished);
885 * Allocate an id which can be used by WAIT_FENCE ioctl to map back
886 * to the underlying fence.
888 submit->fence_id = idr_alloc_cyclic(&queue->fence_idr,
889 submit->user_fence, 0, INT_MAX, GFP_KERNEL);
890 if (submit->fence_id < 0) {
891 ret = submit->fence_id = 0;
892 submit->fence_id = 0;
896 if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
897 struct sync_file *sync_file = sync_file_create(submit->user_fence);
902 fd_install(out_fence_fd, sync_file->file);
903 args->fence_fd = out_fence_fd;
906 submit_attach_object_fences(submit);
908 /* The scheduler owns a ref now: */
909 msm_gem_submit_get(submit);
911 drm_sched_entity_push_job(&submit->base, queue->entity);
913 args->fence = submit->fence_id;
915 msm_reset_syncobjs(syncobjs_to_reset, args->nr_in_syncobjs);
916 msm_process_post_deps(post_deps, args->nr_out_syncobjs,
921 submit_cleanup(submit, !!ret);
923 ww_acquire_fini(&submit->ticket);
925 if (ret && (out_fence_fd >= 0))
926 put_unused_fd(out_fence_fd);
927 mutex_unlock(&queue->lock);
929 msm_gem_submit_put(submit);
931 if (!IS_ERR_OR_NULL(post_deps)) {
932 for (i = 0; i < args->nr_out_syncobjs; ++i) {
933 kfree(post_deps[i].chain);
934 drm_syncobj_put(post_deps[i].syncobj);
939 if (!IS_ERR_OR_NULL(syncobjs_to_reset)) {
940 for (i = 0; i < args->nr_in_syncobjs; ++i) {
941 if (syncobjs_to_reset[i])
942 drm_syncobj_put(syncobjs_to_reset[i]);
944 kfree(syncobjs_to_reset);