Skip to contentSkip to conversation

AMDGPU userqueue suspend/resume

PATCH New Shashank Sharma 1 participant 5 messages May 8, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover AMDGPU userqueue suspend/resume

This patch series adds support for suspending and resuming the gfx usermode queues. It also adds eviction fences which are primarily used by usermode queues.

This patch series is dependent on basic AMDGPU usermode queue series which is being reviewed here:
https://patchwork.freedesktop.org/series/113675/

Shashank Sharma (4):
drm/amdgpu: add gfx eviction fence helpers drm/amdgpu: add core userqueue suspend/resume functions drm/amdgpu: suspend gfx userqueues
drm/amdgpu: add userqueue resume

 drivers/gpu/drm/amd/amdgpu/Makefile           |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu.h           |  21 ++
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    | 112 ++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |  10 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c       |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c | 243 ++++++++++++++++++
 .../gpu/drm/amd/amdgpu/mes_v11_0_userqueue.c  |  31 +++
 .../gpu/drm/amd/include/amdgpu_userqueue.h    |  12 +
 9 files changed, 437 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
Signature · 1 line
2.43.2
#1 lore raw

PATCH 1/4 drm/amdgpu: add gfx eviction fence helpers

This patch adds basic eviction fence framework for the gfx buffers. The idea is to:
- One eviction fence is created per gfx process, at kms_open.
- This same fence is attached to all the gem buffers created by this process.

This framework will be further used for usermode queues.

V2: Addressed review comments from Christian
- keep fence_ctx and fence_seq directly in fpriv
- evcition_fence should be dynamically allocated
- do not save eviction fence instance in BO, there could be many such fences attached to one BO
- use dma_resv_replace_fence() in detach

Cc Christian Koenig <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/Makefile           |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu.h           |  20 ++++
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    | 106 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |  10 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c       |   8 +-
 5 files changed, 143 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index b0103f404957..9743bf06d6aa 100644
@@ -82,7 +82,7 @@ amdgpu-y += amdgpu_device.o amdgpu_doorbell_mgr.o amdgpu_kms.o \
 	amdgpu_fw_attestation.o amdgpu_securedisplay.o \
 	amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \
 	amdgpu_ring_mux.o amdgpu_xcp.o amdgpu_seq64.o amdgpu_aca.o \
-	amdgpu_userq_fence.o
+	amdgpu_userq_fence.o amdgpu_eviction_fence.o
 
 amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu.h +20 -0
index 2d5ef2e74c71..a37193fc9ddc 100644
@@ -465,6 +465,11 @@ struct amdgpu_flip_work {
 	bool				async;
 };
 
+struct amdgpu_eviction_fence {
+	struct dma_fence base;
+	spinlock_t	 lock;
+	char		 timeline_name[TASK_COMM_LEN];
+};
 
 /*
  * file private structure
@@ -479,6 +484,12 @@ struct amdgpu_fpriv {
 	struct idr		bo_list_handles;
 	struct amdgpu_ctx_mgr	ctx_mgr;
 	struct amdgpu_userq_mgr	userq_mgr;
+
+	/* Eviction fence infra */
+	u64			ev_fence_ctx;
+	atomic_t		ev_fence_seq;
+	struct amdgpu_eviction_fence *ev_fence;
+
 	/** GPU partition selection */
 	uint32_t		xcp_id;
 };
@@ -1480,6 +1491,15 @@ void amdgpu_disable_vblank_kms(struct drm_crtc *crtc);
 int amdgpu_info_ioctl(struct drm_device *dev, void *data,
 		      struct drm_file *filp);
 
+/* Eviction fence */
+struct amdgpu_eviction_fence *amdgpu_eviction_fence_create(struct amdgpu_fpriv *fpriv);
+void amdgpu_eviction_fence_destroy(struct amdgpu_fpriv *fpriv);
+int amdgpu_eviction_fence_attach(struct amdgpu_eviction_fence *ev_fence,
+				 struct amdgpu_bo *bo);
+void amdgpu_eviction_fence_detach(struct amdgpu_fpriv *fpriv,
+				  struct amdgpu_eviction_fence *ev_fence,
+				  struct amdgpu_bo *bo);
+
 /*
  * functions used by amdgpu_encoder.c
  */
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c new file +106 -0

106 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -1
index 85f1c05890bb..3355b750a652 100644
@@ -199,8 +199,14 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 	 * but not for export, this is a different lock class that cannot lead to
 	 * circular lock dependencies.
 	 */
-	if (!vm->is_compute_context || !vm->process_info)
+	if (!vm->is_compute_context || !vm->process_info) {
+		/* attach gfx eviction fence */
+		if (amdgpu_eviction_fence_attach(fpriv->ev_fence, abo))
+			DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
+
 		return 0;
+	}
+
 	if (!obj->import_attach ||
 	    !dma_buf_is_dynamic(obj->import_attach->dmabuf))
 		return 0;
@@ -236,6 +242,8 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 	struct drm_exec exec;
 	long r;
 
+	amdgpu_eviction_fence_detach(fpriv, fpriv->ev_fence, bo);
+
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
 		r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7 -1
index d78b06af834e..9f4ffd195fea 100644
@@ -1294,7 +1294,6 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 	return 0;
 }
 
-
 /*
  * Outdated mess for old drm with Xorg being in charge (void function now).
  */
@@ -1387,6 +1386,12 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	mutex_init(&fpriv->bo_list_lock);
 	idr_init_base(&fpriv->bo_list_handles, 1);
 
+	fpriv->ev_fence = amdgpu_eviction_fence_create(fpriv);
+	if (!fpriv->ev_fence) {
+		DRM_ERROR("Failed to craete eviction fence\n");
+		goto error_vm;
+	}
+
 	amdgpu_ctx_mgr_init(&fpriv->ctx_mgr, adev);
 
 	r = amdgpu_userq_mgr_init(&fpriv->userq_mgr, adev);
@@ -1460,6 +1465,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 		amdgpu_bo_unreserve(pd);
 	}
 
+	amdgpu_eviction_fence_destroy(fpriv);
 	amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
 	amdgpu_vm_fini(adev, &fpriv->vm);
 	amdgpu_userq_mgr_fini(&fpriv->userq_mgr);
Signature · 1 line
2.43.2
#2 lore raw

PATCH 2/4 drm/amdgpu: add core userqueue suspend/resume functions

In reply to Shashank Sharma

This patch adds userqueue suspend/resume functions at core MES V11 IP level.

Cc Alex Deucher <[email protected]>
Cc Christian Koenig <[email protected]>
---
 .../gpu/drm/amd/amdgpu/mes_v11_0_userqueue.c  | 31 +++++++++++++++++++
 .../gpu/drm/amd/include/amdgpu_userqueue.h    |  5 +++
 2 files changed, 36 insertions(+)
drivers/gpu/drm/amd/amdgpu/mes_v11_0_userqueue.c +31 -0
index 412970376b49..4e05da3c8f53 100644
@@ -321,7 +321,38 @@ mes_v11_0_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr,
 	amdgpu_userqueue_destroy_object(uq_mgr, &queue->mqd);
 }
 
+static int mes_v11_0_userq_suspend(struct amdgpu_userq_mgr *uq_mgr,
+				   struct amdgpu_usermode_queue *queue)
+{
+	if (queue->queue_active) {
+		mes_v11_0_userq_unmap(uq_mgr, queue);
+		queue->queue_active = 0;
+	}
+
+	return 0;
+}
+
+static int mes_v11_0_userq_resume(struct amdgpu_userq_mgr *uq_mgr,
+				  struct amdgpu_usermode_queue *queue)
+{
+	int ret;
+
+	if (queue->queue_active)
+		return 0;
+
+	ret = mes_v11_0_userq_map(uq_mgr, queue, queue->userq_prop);
+	if (ret) {
+		DRM_ERROR("Failed to resume queue\n");
+		return ret;
+	}
+
+	queue->queue_active = 1;
+	return 0;
+}
+
 const struct amdgpu_userq_funcs userq_mes_v11_0_funcs = {
 	.mqd_create = mes_v11_0_userq_mqd_create,
 	.mqd_destroy = mes_v11_0_userq_mqd_destroy,
+	.suspend = mes_v11_0_userq_suspend,
+	.resume = mes_v11_0_userq_resume,
 };
drivers/gpu/drm/amd/include/amdgpu_userqueue.h +5 -0
index 5416de0bdf25..afaf93faa824 100644
@@ -37,6 +37,7 @@ struct amdgpu_userq_obj {
 
 struct amdgpu_usermode_queue {
 	int			queue_type;
+	uint8_t			queue_active;
 	uint64_t		doorbell_handle;
 	uint64_t		doorbell_index;
 	uint64_t		flags;
@@ -57,6 +58,10 @@ struct amdgpu_userq_funcs {
 			  struct amdgpu_usermode_queue *queue);
 	void (*mqd_destroy)(struct amdgpu_userq_mgr *uq_mgr,
 			    struct amdgpu_usermode_queue *uq);
+	int (*suspend)(struct amdgpu_userq_mgr *uq_mgr,
+		       struct amdgpu_usermode_queue *queue);
+	int (*resume)(struct amdgpu_userq_mgr *uq_mgr,
+		      struct amdgpu_usermode_queue *queue);
 };
 
 /* Usermode queues for gfx */
Signature · 1 line
2.43.2
#3 lore raw

PATCH 3/4 drm/amdgpu: suspend gfx userqueues

In reply to Shashank Sharma

This patch adds suspend support for gfx userqueues. It typically does the following:
- adds an enable_signaling function for the eviction fence, so that it can trigger the userqueue suspend,
- adds a delayed function for suspending the userqueues, to suspend all the queues under this userq manager and signals the eviction fence,
- adds reference of userq manager in the eviction fence container so that it can be used in the suspend function.

Cc Alex Deucher <[email protected]>
Cc Christian Koenig <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h           |  1 +
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  6 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c | 77 +++++++++++++++++++
 .../gpu/drm/amd/include/amdgpu_userqueue.h    |  6 ++
 5 files changed, 91 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
index a37193fc9ddc..1856fe11dd05 100644
@@ -469,6 +469,7 @@ struct amdgpu_eviction_fence {
 	struct dma_fence base;
 	spinlock_t	 lock;
 	char		 timeline_name[TASK_COMM_LEN];
+	struct amdgpu_userq_mgr *uq_mgr;
 };
 
 /*
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +6 -0
index 1a03f040ccc8..3f806e44f614 100644
@@ -39,10 +39,16 @@ amdgpu_ev_fence_get_timeline_name(struct dma_fence *f)
 	return ef->timeline_name;
 }
 
+static bool amdgpu_ev_fence_enable_signaling(struct dma_fence *f)
+{
+	return !amdgpu_userqueue_enable_signaling(f);
+}
+
 static const struct dma_fence_ops amdgpu_eviction_fence_ops = {
 	.use_64bit_seqno = true,
 	.get_driver_name = amdgpu_ev_fence_get_driver_name,
 	.get_timeline_name = amdgpu_ev_fence_get_timeline_name,
+	.enable_signaling = amdgpu_ev_fence_enable_signaling,
 };
 
 struct amdgpu_eviction_fence *
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
index fa03d9e4874c..8c13de7f2a19 100644
@@ -111,6 +111,7 @@ struct amdgpu_bo {
 #endif
 	struct kgd_mem                  *kfd_bo;
 
+
 	/*
 	 * For GPUs with spatial partitioning, xcp partition number, -1 means
 	 * any partition. For other ASICs without spatial partition, always 0
drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c +77 -0

106 lines not on this page · raw message

drivers/gpu/drm/amd/include/amdgpu_userqueue.h +6 -0
index afaf93faa824..647e63bf03ab 100644
@@ -26,6 +26,8 @@
 #define AMDGPU_USERQUEUE_H_
 
 #define AMDGPU_MAX_USERQ_COUNT 512
+#define AMDGPU_USERQ_SUSPEND_TIME_MS 1
+#define AMDGPU_USERQ_RESUME_TIME_MS 100
 
 struct amdgpu_mqd_prop;
 
@@ -69,6 +71,8 @@ struct amdgpu_userq_mgr {
 	struct idr			userq_idr;
 	struct mutex			userq_mutex;
 	struct amdgpu_device		*adev;
+
+	struct delayed_work		suspend_work;
 };
 
 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
@@ -86,4 +90,6 @@ void amdgpu_userqueue_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
 
 int amdgpu_userqueue_update_bo_mapping(struct drm_file *filp, struct amdgpu_bo *bo,
 				       uint32_t syncobj_handle, uint16_t point);
+
+int amdgpu_userqueue_enable_signaling(struct dma_fence *f);
 #endif
Signature · 1 line
2.43.2
#4 lore raw

PATCH 4/4 drm/amdgpu: add userqueue resume

In reply to Shashank Sharma

This patch adds support for userqueue resume. What it typically does is this:
- adds a new delayed work for resuming all the queues.
- schedules this delayed work from the suspend work.
- validates the BOs and replaces the eviction fence before resuming all the queues running under this instance of userq manager.

Cc Alex Deucher <[email protected]>
Cc Christian Koenig <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c | 166 ++++++++++++++++++
 .../gpu/drm/amd/include/amdgpu_userqueue.h    |   1 +
 2 files changed, 167 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c +166 -0
index fdbd542e7f53..02ddd713d068 100644
@@ -398,6 +398,167 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 	return r;
 }
 
+static int
+amdgpu_userqueue_resume_all(struct amdgpu_userq_mgr *uq_mgr)
+{
+	struct amdgpu_device *adev = uq_mgr->adev;
+	const struct amdgpu_userq_funcs *userq_funcs;
+	struct amdgpu_usermode_queue *queue;
+	int queue_id, ret;
+
+	userq_funcs = adev->userq_funcs[AMDGPU_HW_IP_GFX];
+
+	/* Resume all the queues for this process */
+	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
+		ret = userq_funcs->resume(uq_mgr, queue);
+		if (ret)
+			DRM_ERROR("Failed to resume queue %d\n", queue_id);
+	}
+
+	return ret;
+}
+
+static int
+amdgpu_userqueue_replace_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
+				  struct drm_exec *exec)
+{
+	int ret;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_eviction_fence *old_ef, *new_ef;
+	struct amdgpu_bo_va *bo_va, *tmp;
+
+	old_ef = fpriv->ev_fence;
+	new_ef = amdgpu_eviction_fence_create(fpriv);
+	if (!new_ef) {
+		DRM_ERROR("Failed to create new eviction fence\n");
+		return ret;
+	}
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
+		struct amdgpu_bo *bo = bo_va->base.bo;
+
+		/* Skip pinned BOs */
+		if (bo->tbo.pin_count)
+			continue;
+
+		ret = drm_exec_lock_obj(exec, &bo->tbo.base);
+		if (unlikely(ret)) {
+			DRM_ERROR("Failed to lock BO for eviction fence replacement\n");
+			goto free_err;
+		}
+
+		/* replace the old eviction fence with new one */
+		amdgpu_eviction_fence_detach(fpriv, old_ef, bo);
+		ret = amdgpu_eviction_fence_attach(new_ef, bo);
+		if (ret) {
+			DRM_ERROR("Failed to attch new eviction fence\n");
+			goto free_err;
+		}
+	}
+
+	/* Update the new eviction fence */
+	fpriv->ev_fence = new_ef;
+	kfree(old_ef);
+	return 0;
+
+free_err:
+	kfree(new_ef);
+	return ret;
+}
+
+/* Expects drm_exec_until_all_locked called on this exec */
+static int
+amdgpu_userqueue_validate_bos(struct amdgpu_userq_mgr *uq_mgr,
+			      struct drm_exec *exec)
+{
+	int ret;
+	struct amdgpu_bo *bo;
+	struct amdgpu_bo_va *bo_va, *tmp;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
+		bo = bo_va->base.bo;
+		ret = drm_exec_lock_obj(exec, &bo->tbo.base);
+		if (unlikely(ret)) {
+			DRM_ERROR("Failed to exec lock  for validation\n");
+			goto unlock_all;
+		}
+	}
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
+		bo = bo_va->base.bo;
+		ret = drm_exec_lock_obj(exec, &bo->tbo.base);
+		if (unlikely(ret)) {
+			DRM_ERROR("Failed to lock BO for validation\n");
+			goto unlock_all;
+		}
+
+		ret = amdgpu_bo_reserve(bo, false);
+		if (unlikely(ret)) {
+			DRM_ERROR("Failed to reserve BO for validation\n");
+			goto unlock_all;
+		}
+
+		ret = amdgpu_userqueue_validate_bo(bo);
+		amdgpu_bo_unreserve(bo);
+		if (ret) {
+			DRM_ERROR("Failed to validate BO\n");
+			goto unlock_all;
+		}
+	}
+
+	ret = amdgpu_vm_handle_moved(uq_mgr->adev, vm, NULL);
+	if (ret)
+		DRM_ERROR("Failed to handle moved BOs\n");
+
+unlock_all:
+	return ret;
+}
+
+static void amdgpu_userqueue_resume_worker(struct work_struct *work)
+{
+	int ret;
+	struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct drm_exec exec;
+
+	mutex_lock(&uq_mgr->userq_mutex);
+
+	drm_exec_init(&exec, 0, 0);
+	drm_exec_until_all_locked(&exec) {
+		ret = amdgpu_vm_lock_pd(vm, &exec, 2);
+		if (unlikely(ret)) {
+			DRM_ERROR("Failed to lock PD\n");
+			goto unlock_all;
+		}
+	}
+
+	ret = amdgpu_userqueue_validate_bos(uq_mgr, &exec);
+	if (ret) {
+		DRM_ERROR("Failed to validate BOs to restore\n");
+		goto unlock_all;
+	}
+
+	ret = amdgpu_userqueue_replace_ev_fence(uq_mgr, &exec);
+	if (ret) {
+		DRM_ERROR("Failed to signal eviction\n");
+		goto unlock_all;
+	}
+
+	ret = amdgpu_userqueue_resume_all(uq_mgr);
+	if (ret) {
+		DRM_ERROR("Failed to resume all queues\n");
+		goto unlock_all;
+	}
+
+unlock_all:
+	drm_exec_fini(&exec);
+	mutex_unlock(&uq_mgr->userq_mutex);
+}
+
 static int
 amdgpu_userqueue_suspend_all(struct amdgpu_userq_mgr *uq_mgr)
 {
@@ -442,6 +603,10 @@ amdgpu_userqueue_suspend_worker(struct work_struct *work)
 		return;
 	}
 
+	/* Schedule a work to restore userqueue after 100 ms */
+	schedule_delayed_work(&uq_mgr->resume_work,
+			      msecs_to_jiffies(AMDGPU_USERQ_RESUME_TIME_MS));
+
 unlock:
 	mutex_unlock(&uq_mgr->userq_mutex);
 }
@@ -475,6 +640,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct amdgpu_devi
 	/* This reference is required for suspend work */
 	fpriv->ev_fence->uq_mgr = userq_mgr;
 	INIT_DELAYED_WORK(&userq_mgr->suspend_work, amdgpu_userqueue_suspend_worker);
+	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userqueue_resume_worker);
 	return 0;
 }
 
drivers/gpu/drm/amd/include/amdgpu_userqueue.h +1 -0
index 647e63bf03ab..2e3fe784188e 100644
@@ -73,6 +73,7 @@ struct amdgpu_userq_mgr {
 	struct amdgpu_device		*adev;
 
 	struct delayed_work		suspend_work;
+	struct delayed_work		resume_work;
 };
 
 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
Signature · 1 line
2.43.2