Skip to contentSkip to conversation

drm/amdgpu: rework amdgpu open() path to function in sleep state

PATCH Superseded Shahyan Soltani 2 participants 4 messages Oct 7 – Oct 8

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover drm/amdgpu: rework amdgpu open() path to function in sleep state

This patch series reworks the amdgpu Open, VM, CS, and Validation functionality to allow for access of the GPU's CPU sided bookkeeping data without waking up the GPU from idle.

The overhaul will allow a few tools such as AMD-SMI to function without waking up the GPU, which can improve power efficiency within relevant domains, and can hopefully be expanded on later to adapt for useful utilities.

Shahyan Soltani (2):
drm/amdgpu: move amdgpu_vm_bo_update() call into correct location drm/amdgpu: defer GPU resume from open/close to first use

 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c     | 14 ++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c    | 16 +++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c  |  7 -------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     | 19 ++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h     |  7 ++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c  |  7 ++++++-
 7 files changed, 46 insertions(+), 26 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 drm/amdgpu: move amdgpu_vm_bo_update() call into correct location

Calling amdgpu_vm_bo_update() this early is unnecessary. moving it to amdgpu_cs_vm_handling is the correct location for this function and is required to allow the VM to initialize without waking up sleep.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c    | 14 ++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c |  7 -------
 2 files changed, 14 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +14 -0
index d059e0c92866..2d01a831d534 100644
@@ -1159,6 +1159,20 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 			return r;
 	}
 
+	if (fpriv->seq64_va) {
+		bo_va = fpriv->seq64_va;
+		r = amdgpu_vm_bo_update(adev, bo_va, false);
+		if (r) {
+			DRM_ERROR("failed to do vm_bo_update on userq sem\n");
+			return r;
+		}
+
+		r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update,
+				      GFP_KERNEL);
+		if (r)
+			return r;
+	}
+
 	/* FIXME: In theory this loop shouldn't be needed any more when
 	 * amdgpu_vm_handle_moved handles all moved BOs that are reserved
 	 * with p->ticket. But removing it caused test regressions, so I'm
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c +0 -7
index 21a225b0116a..fbf9ec7d467b 100644
@@ -103,13 +103,6 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error;
 	}
 
-	r = amdgpu_vm_bo_update(adev, *bo_va, false);
-	if (r) {
-		DRM_ERROR("failed to do vm_bo_update on userq sem\n");
-		amdgpu_vm_bo_del(adev, *bo_va);
-		goto error;
-	}
-
 error:
 	drm_exec_fini(&exec);
 	return r;
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] drm/amdgpu: move amdgpu_vm_bo_update() call into correct location

Quoted 5 lines · Shahyan Soltani

Calling amdgpu_vm_bo_update() this early is unnecessary. moving it to amdgpu_cs_vm_handling is the correct location for this function and is required to allow the VM to initialize without waking up sleep.

It could be that this is regressing user queues, but that would then be a separate bug in the user queue restore handling.

Adding Sunil on CC just for awareness.

Quoted 48 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c    | 14 ++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c |  7 -------
 2 files changed, 14 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +14 -0
index d059e0c92866..2d01a831d534 100644
@@ -1159,6 +1159,20 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 			return r;
 	}
 
+	if (fpriv->seq64_va) {
+		bo_va = fpriv->seq64_va;
+		r = amdgpu_vm_bo_update(adev, bo_va, false);
+		if (r) {
+			DRM_ERROR("failed to do vm_bo_update on userq sem\n");
+			return r;
+		}
+
+		r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update,
+				      GFP_KERNEL);
+		if (r)
+			return r;
+	}
+
 	/* FIXME: In theory this loop shouldn't be needed any more when
 	 * amdgpu_vm_handle_moved handles all moved BOs that are reserved
 	 * with p->ticket. But removing it caused test regressions, so I'm
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c +0 -7
index 21a225b0116a..fbf9ec7d467b 100644
@@ -103,13 +103,6 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error;
 	}
 
-	r = amdgpu_vm_bo_update(adev, *bo_va, false);
-	if (r) {
-		DRM_ERROR("failed to do vm_bo_update on userq sem\n");
-		amdgpu_vm_bo_del(adev, *bo_va);
-		goto error;
-	}
-
 error:
 	drm_exec_fini(&exec);
 	return r;
#3 lore raw

PATCH 2/2 drm/amdgpu: defer GPU resume from open/close to first use

In reply to Shahyan Soltani

Drop pm_runtime_get_sync() from open/close KMS to prevent unnecessary resumes. Instead, the PM reference will only be held if the GPU is currently awake.

Introduce delayed virtual memory hardware initialization through a powered_init and root_cleared flag. The powered_init flag handles initializing the VM domain in GTT/VRAM and moving a GTT domain based VM into a VRAM domain. The root_cleared flag handles the initial root PD clearing.

Handling is done through the modified amdgpu_vm_validate() and amdgpu_vm_pt_create() functions.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c    | 16 +++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     | 19 ++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h     |  7 ++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c  |  7 ++++++-
 5 files changed, 32 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7 -9
index f34150d20a2f..958d235abb11 100644
@@ -1488,9 +1488,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 
 	file_priv->driver_priv = NULL;
 
-	r = pm_runtime_get_sync(dev->dev);
-	if (r < 0)
-		goto pm_put;
+	int pm_ref = pm_runtime_get_if_active(dev->dev);
 
 	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
 	if (unlikely(!fpriv)) {
@@ -1504,7 +1502,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 
 	amdgpu_debugfs_vm_init(file_priv);
 
-	r = amdgpu_vm_init(adev, &fpriv->vm, fpriv->xcp_id);
+	r = amdgpu_vm_init(adev, &fpriv->vm, fpriv->xcp_id, !!pm_ref);
 	if (r)
 		goto error_pasid;
 
@@ -1569,9 +1567,8 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	kfree(fpriv);
 
 out_suspend:
-pm_put:
-	pm_runtime_put_autosuspend(dev->dev);
-
+	if (pm_ref > 0)
+		pm_runtime_put_autosuspend(dev->dev);
 	return r;
 }
 
@@ -1596,7 +1593,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	if (!fpriv)
 		return;
 
-	pm_runtime_get_sync(dev->dev);
+	int pm_ref = pm_runtime_get_if_active(dev->dev);
 
 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
 		amdgpu_uvd_free_handles(adev, file_priv);
@@ -1635,7 +1632,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	kfree(fpriv);
 	file_priv->driver_priv = NULL;
 
-	pm_runtime_put_autosuspend(dev->dev);
+	if (pm_ref > 0)
+		pm_runtime_put_autosuspend(dev->dev);
 }
 
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -1
index 17888cf54447..7b3851649d30 100644
@@ -4163,7 +4163,7 @@ int amdgpu_ualink_manager_start(struct amdgpu_device *adev)
 {
 	int i, r;
 
-	r = amdgpu_vm_init(adev, &adev->ualink.npa_vm, 0);
+	r = amdgpu_vm_init(adev, &adev->ualink.npa_vm, 0, true);
 	if (r)
 		goto out;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -7
index 7d3659ef11e5..39c13a4b45fa 100644
@@ -594,13 +594,14 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	int r;
 
 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
-	if (vm->generation != new_vm_generation) {
+	if (vm->generation != new_vm_generation || !vm->powered_init) {
 		vm->generation = new_vm_generation;
 		amdgpu_vm_bo_reset_state_machine(vm);
 		amdgpu_vm_fini_entities(vm);
 		r = amdgpu_vm_init_entities(adev, vm);
 		if (r)
 			return r;
+		vm->powered_init = true;
 	}
 
 	list_for_each_entry_safe(bo_base, tmp, &vm->kernel.evicted, vm_status) {
@@ -620,6 +621,13 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	scoped_guard(mutex, &vm->eviction_lock)
 		vm->evicting = false;
 
+	if (!vm->root_cleared) {
+		r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo), false);
+		if (r)
+			return r;
+		vm->root_cleared = true;
+	}
+
 	list_for_each_entry_safe(bo_base, tmp, &vm->always_valid.evicted,
 				 vm_status) {
 		r = validate(param, bo_base->bo);
@@ -2604,7 +2612,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
  * 0 for success, error for failure.
  */
 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
-		   int32_t xcp_id)
+		   int32_t xcp_id, bool device_active)
 {
 	struct amdgpu_bo *root_bo;
 	struct amdgpu_bo_vm *root;
@@ -2628,6 +2636,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
+	vm->powered_init = device_active;
+	vm->root_cleared = false;
 	vm->is_compute_context = false;
 	vm->need_tlb_fence = amdgpu_userq_enabled(&adev->ddev);
 
@@ -2648,7 +2658,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
 	vm->last_tlb_flush = dma_fence_get_stub();
-	vm->generation = amdgpu_vm_generation(adev, NULL);
 
 	mutex_init(&vm->eviction_lock);
 	vm->evicting = false;
@@ -2671,10 +2680,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto error_free_root;
 
-	r = amdgpu_vm_pt_clear(adev, vm, root, false);
-	if (r)
-		goto error_free_root;
-
 	r = amdgpu_vm_create_task_info(vm);
 	if (r)
 		dev_dbg(adev->dev, "Failed to create task info for VM\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index 8e478d3bda1a..a40d57a1ae83 100644
@@ -372,6 +372,10 @@ struct amdgpu_vm {
 	bool			is_npa;
 	/* Flag to indicate if VM needs a TLB fence (KFD or KGD) */
 	bool			need_tlb_fence;
+	/* Flag to indicate if VM during initialization holds a runtime reference */
+	bool			powered_init;
+	/* Flag to indicate if VM has been cleared through SDMA at least once */
+	bool			root_cleared;
 
 	/* Memory partition number, -1 means any partition */
 	int8_t			mem_id;
@@ -432,7 +436,8 @@ void amdgpu_vm_manager_init(struct amdgpu_device *adev);
 void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
 
 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout);
-int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, int32_t xcp_id);
+int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, int32_t xcp_id,
+		   bool device_active);
 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 int amdgpu_vm_make_npa(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +6 -1
index c03327f1242d..54f0b62fde6f 100644
@@ -456,7 +456,7 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	bp.size = amdgpu_vm_pt_size(adev, level);
 	bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
 
-	if (!adev->gmc.is_app_apu)
+	if (!adev->gmc.is_app_apu && vm->powered_init)
 		bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
 	else
 		bp.domain = AMDGPU_GEM_DOMAIN_GTT;
@@ -486,6 +486,11 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		return r;
 
+	if (bp.domain == AMDGPU_GEM_DOMAIN_GTT && !adev->gmc.is_app_apu) {
+		(*vmbo)->bo.preferred_domains = AMDGPU_GEM_DOMAIN_VRAM;
+		(*vmbo)->bo.allowed_domains = AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM;
+	}
+
 	/* Assumes that reservation is shared with the VM root and that the
 	 * reservation is locked
 	 */
Signature · 1 line
2.55.0