Skip to contentSkip to conversation

drm/amdgpu: bind the SPM VMID's page table to the reserving VM

PATCH In review Sunil Khatri 3 participants 7 messages Oct 13 – Oct 16

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

PATCH v4 1/2 drm/amdgpu: bind the SPM VMID's page table to the reserving VM

MES userqueues bypass amdgpu_vm_flush(), so a reservation granted to a userqueue never got its VMID's page table programmed. Add amdgpu_vm_spm_update() to bind the fixed SPM VMID's page-table-base to the reserving VM's root PD when that VM holds the reservation; it is a no-op for every other VM. It is called right after a successful AMDGPU_VM_OP_RESERVE_VMID (amdgpu_vm_ioctl(), under the root PD lock) and from amdgpu_userq_vm_validate_and_restore_queue() (already locked via drm_exec), the two paths that can actually change the binding.

amdgpu_vmid_free_reserved() waits for all work that used the reserved VMID to finish, then unbinds it to a dummy address under the same lock that clears the reservation, so in-flight submissions don't suddenly fault and stray SPM accesses afterward fault instead of racing a new reservation's binding.

Both binds go through the new amdgpu_gmc_bind_vmid() helper, which pairs setup_vm_pt_regs() with the matching TLB flush for a VMID bound outside the normal per-job VM flush path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c   | 24 ++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h   |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c   | 13 ++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c |  7 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 47 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  1 +
 6 files changed, 91 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +24 -0
index 5d4276cd0f7d3..4204c3c750a8d 100644
@@ -787,6 +787,30 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r);
 }
 
+/**
+ * amdgpu_gmc_bind_vmid - bind a VMID's page table base and flush its TLB
+ *
+ * @adev: amdgpu device pointer
+ * @vmhub: vmhub the VMID belongs to
+ * @vmid: VMID to bind
+ * @pd_addr: new page directory address, 0 to unbind to a faulting address
+ *
+ * For VMIDs that are bound directly to a VM's page table outside the
+ * normal per-job VM flush path, e.g. a dedicated out-of-pool VMID.
+ */
+void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub,
+			  unsigned int vmid, uint64_t pd_addr)
+{
+	bool is_mmhub = AMDGPU_IS_MMHUB0(vmhub) || AMDGPU_IS_MMHUB1(vmhub);
+
+	if (is_mmhub)
+		adev->mmhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr);
+	else
+		adev->gfxhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr);
+
+	amdgpu_gmc_flush_gpu_tlb(adev, vmid, vmhub, TLB_FLUSH_LEGACY);
+}
+
 static void amdgpu_gmc_flush_pasid_regs(struct amdgpu_device *adev, u16 pasid,
 					u32 flush_type, bool all_hub,
 					u32 inst)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +2 -0
index 76e6f1f96913c..d17055d4c50a9 100644
@@ -444,6 +444,8 @@ int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev);
 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev);
 void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 			      uint32_t vmhub, uint32_t flush_type);
+void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub,
+			  unsigned int vmid, uint64_t pd_addr);
 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, u16 pasid,
 				   u32 flush_type, bool all_hub, u32 inst);
 int amdgpu_gmc_flush_gpu_tlb_pasid_xccs(struct amdgpu_device *adev, u16 pasid,
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +12 -1
index 074c6942d83b0..e5d84a786c4f2 100644
@@ -633,16 +633,27 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  * @adev: amdgpu device structure
  * @vm: the VM with the reserved ID
  * @vmhub: the VMHUB which should be used
+ *
+ * Waits for all work that used the reserved VMID to finish before unbinding
+ * its page table, so submissions still in flight don't suddenly fault.
  */
 void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)
 {
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
+	struct amdgpu_vmid *id;
 
 	mutex_lock(&id_mgr->lock);
-	if (vm->reserved_vmid[vmhub]) {
+	id = vm->reserved_vmid[vmhub];
+	if (id) {
 		vm->reserved_vmid[vmhub] = NULL;
 		id_mgr->reserved_vmid = false;
+
+		amdgpu_sync_wait(&id->active, false);
+
+		if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)
+			amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0),
+					     adev->vm_manager.spm_vmid, 0);
 	}
 	mutex_unlock(&id_mgr->lock);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
index e29823ae8672e..d45357e092bb5 100644
@@ -1075,6 +1075,13 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 		if (unlikely(ret))
 			goto unlock_all;
 
+		/*
+		 * Userqueues never go through amdgpu_vm_flush(), so this is
+		 * the only place that can rebind the SPM VMID's page table
+		 * for a userqueue-held reservation.
+		 */
+		amdgpu_vm_spm_update(adev, vm);
+
 		/* This locks and validates the remaining evicted BOs */
 		ret = amdgpu_userq_bo_validate(adev, &exec, vm);
 		drm_exec_retry_on_contention(&exec);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +45 -2

60 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index b8aa45db1d844..2a2a2ccbaa2cc 100644
@@ -443,6 +443,7 @@ int amdgpu_vm_lock_individual(struct amdgpu_vm *vm, struct drm_exec *exec,
 			      unsigned int num_fences);
 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
 uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm);
+void amdgpu_vm_spm_update(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		       struct ww_acquire_ctx *ticket,
 		       int (*callback)(void *p, struct amdgpu_bo *bo),
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 2/2 drm/amdgpu: rename amdgpu_vmid_alloc/free_reserved to _spm

These functions only ever hand out the dedicated SPM VMID now, not a generic reserved VMID. Rename them to amdgpu_vmid_alloc_spm() and amdgpu_vmid_free_spm() to say so. Pure rename, no functional change.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -4
index e5d84a786c4f2..f15702993eeab 100644
@@ -589,7 +589,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
 }
 
 /*
- * amdgpu_vmid_alloc_reserved - reserve a specific VMID for this vm
+ * amdgpu_vmid_alloc_spm - reserve a specific VMID for this vm
  * @adev: amdgpu device structure
  * @vm: the VM to reserve an ID for
  * @vmhub: the VMHUB which should be used
@@ -598,7 +598,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
  *
  * Returns: 0 for success, -ENOENT if an ID is already reserved.
  */
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)
 {
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
@@ -629,7 +629,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 }
 
 /*
- * amdgpu_vmid_free_reserved - free up a reserved VMID again
+ * amdgpu_vmid_free_spm - free up a reserved VMID again
  * @adev: amdgpu device structure
  * @vm: the VM with the reserved ID
  * @vmhub: the VMHUB which should be used
@@ -637,7 +637,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  * Waits for all work that used the reserved VMID to finish before unbinding
  * its page table, so submissions still in flight don't suddenly fault.
  */
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)
 {
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +2 -2
index b85aa2ee19b19..c134000eecfdc 100644
@@ -125,9 +125,9 @@ void amdgpu_pasid_mgr_cleanup(void);
 bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
 			       struct amdgpu_vmid *id);
 bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub);
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub);
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub);
 int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
 		     struct amdgpu_job *job, struct dma_fence **fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -3
index 7fd6215510a94..8d4c13f2ef7fb 100644
@@ -2894,7 +2894,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	dma_fence_put(vm->last_update);
 
 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) {
-		amdgpu_vmid_free_reserved(adev, vm, i);
+		amdgpu_vmid_free_spm(adev, vm, i);
 	}
 
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
@@ -2997,7 +2997,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 		int r;
 
 		/* We only have requirement to reserve vmid from gfxhub */
-		r = amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0));
 		if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
 			amdgpu_vm_spm_update(adev, vm);
 			amdgpu_bo_unreserve(vm->root.bo);
@@ -3005,7 +3005,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 		return r;
 	}
 	case AMDGPU_VM_OP_UNRESERVE_VMID:
-		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0));
 		break;
 	default:
 		return -EINVAL;
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v4 2/2] drm/amdgpu: rename amdgpu_vmid_alloc/free_reserved to _spm

Quoted 26 lines · Sunil Khatri

These functions only ever hand out the dedicated SPM VMID now, not a generic reserved VMID. Rename them to amdgpu_vmid_alloc_spm() and amdgpu_vmid_free_spm() to say so. Pure rename, no functional change.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +1 -1
index e5d84a786c4f2..f15702993eeab 100644
@@ -589,7 +589,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
 }
 
 /*
- * amdgpu_vmid_alloc_reserved - reserve a specific VMID for this vm
+ * amdgpu_vmid_alloc_spm - reserve a specific VMID for this vm
  * @adev: amdgpu device structure
  * @vm: the VM to reserve an ID for
  * @vmhub: the VMHUB which should be used

drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub) · line 602

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +1 -1
@@ -598,7 +598,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
  *
  * Returns: 0 for success, -ENOENT if an ID is already reserved.
  */
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)

I think we also need to make sure that the hub is a graphics VMHUB.

There is really no use case for an MMHUB here.

Apart from that looks good to me,
Christian.

Quoted 67 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -2
 {
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
@@ -629,7 +629,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 }
 
 /*
- * amdgpu_vmid_free_reserved - free up a reserved VMID again
+ * amdgpu_vmid_free_spm - free up a reserved VMID again
  * @adev: amdgpu device structure
  * @vm: the VM with the reserved ID
  * @vmhub: the VMHUB which should be used
@@ -637,7 +637,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  * Waits for all work that used the reserved VMID to finish before unbinding
  * its page table, so submissions still in flight don't suddenly fault.
  */
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)
 {
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +2 -2
index b85aa2ee19b19..c134000eecfdc 100644
@@ -125,9 +125,9 @@ void amdgpu_pasid_mgr_cleanup(void);
 bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
 			       struct amdgpu_vmid *id);
 bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub);
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub);
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub);
 int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
 		     struct amdgpu_job *job, struct dma_fence **fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -3
index 7fd6215510a94..8d4c13f2ef7fb 100644
@@ -2894,7 +2894,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	dma_fence_put(vm->last_update);
 
 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) {
-		amdgpu_vmid_free_reserved(adev, vm, i);
+		amdgpu_vmid_free_spm(adev, vm, i);
 	}
 
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
@@ -2997,7 +2997,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 		int r;
 
 		/* We only have requirement to reserve vmid from gfxhub */
-		r = amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0));
 		if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
 			amdgpu_vm_spm_update(adev, vm);
 			amdgpu_bo_unreserve(vm->root.bo);
@@ -3005,7 +3005,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 		return r;
 	}
 	case AMDGPU_VM_OP_UNRESERVE_VMID:
-		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0));
 		break;
 	default:
 		return -EINVAL;
#3 lore raw

Re: [PATCH v4 2/2] drm/amdgpu: rename amdgpu_vmid_alloc/free_reserved to _spm

Quoted 21 lines · Christian König
Quoted 19 lines · Sunil Khatri

These functions only ever hand out the dedicated SPM VMID now, not a generic reserved VMID. Rename them to amdgpu_vmid_alloc_spm() and amdgpu_vmid_free_spm() to say so. Pure rename, no functional change.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 ++++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 4 ++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 6 +++---
  3 files changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +0 -0
index e5d84a786c4f2..f15702993eeab 100644
@@ -589,7 +589,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
  }
 
Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub) · line 602

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -2
  /*
- * amdgpu_vmid_alloc_reserved - reserve a specific VMID for this vm
+ * amdgpu_vmid_alloc_spm - reserve a specific VMID for this vm
   * @adev: amdgpu device structure
   * @vm: the VM to reserve an ID for
   * @vmhub: the VMHUB which should be used
@@ -598,7 +598,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
   *
   * Returns: 0 for success, -ENOENT if an ID is already reserved.
   */
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			       unsigned vmhub)

I think we also need to make sure that the hub is a graphics VMHUB.

amdgpu_vmid_free_spm/alloc passed explicitly AMDGPU_GFXHUB(0) only... i left the function deliberately to be generic but caller is within the kernel and we hold control to call with GFXHUB only4

Regards
Sunil Khatri

Quoted 73 lines

There is really no use case for an MMHUB here.

Apart from that looks good to me,
Christian.

Quoted 67 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -2
  {
  	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
@@ -629,7 +629,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  }
 
  /*
- * amdgpu_vmid_free_reserved - free up a reserved VMID again
+ * amdgpu_vmid_free_spm - free up a reserved VMID again
   * @adev: amdgpu device structure
   * @vm: the VM with the reserved ID
   * @vmhub: the VMHUB which should be used
@@ -637,7 +637,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   * Waits for all work that used the reserved VMID to finish before unbinding
   * its page table, so submissions still in flight don't suddenly fault.
   */
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			       unsigned vmhub)
  {
  	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +2 -2
index b85aa2ee19b19..c134000eecfdc 100644
@@ -125,9 +125,9 @@ void amdgpu_pasid_mgr_cleanup(void);
  bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
  			       struct amdgpu_vmid *id);
  bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub);
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			       unsigned vmhub);
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			       unsigned vmhub);
  int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  		     struct amdgpu_job *job, struct dma_fence **fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -3
index 7fd6215510a94..8d4c13f2ef7fb 100644
@@ -2894,7 +2894,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  	dma_fence_put(vm->last_update);
 
  	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) {
-		amdgpu_vmid_free_reserved(adev, vm, i);
+		amdgpu_vmid_free_spm(adev, vm, i);
  	}
 
  	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
@@ -2997,7 +2997,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  		int r;
 
  		/* We only have requirement to reserve vmid from gfxhub */
-		r = amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0));
  		if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
  			amdgpu_vm_spm_update(adev, vm);
  			amdgpu_bo_unreserve(vm->root.bo);
@@ -3005,7 +3005,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  		return r;
  	}
  	case AMDGPU_VM_OP_UNRESERVE_VMID:
-		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0));
  		break;
  	default:
  		return -EINVAL;
maintainer
#4 lore raw

Re: [PATCH v4 2/2] drm/amdgpu: rename amdgpu_vmid_alloc/free_reserved to _spm

Quoted 36 lines · Sunil
Quoted 26 lines · Christian König
Quoted 24 lines · Sunil Khatri

These functions only ever hand out the dedicated SPM VMID now, not a generic reserved VMID. Rename them to amdgpu_vmid_alloc_spm() and amdgpu_vmid_free_spm() to say so. Pure rename, no functional change.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 ++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 4 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 6 +++---

  3 files changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +1 -1
index e5d84a786c4f2..f15702993eeab 100644
@@ -589,7 +589,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
   }
     /*
- * amdgpu_vmid_alloc_reserved - reserve a specific VMID for this vm
+ * amdgpu_vmid_alloc_spm - reserve a specific VMID for this vm
    * @adev: amdgpu device structure
    * @vm: the VM to reserve an ID for
    * @vmhub: the VMHUB which should be used
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub) · line 602

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +1 -1
@@ -598,7 +598,7 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
    *
    * Returns: 0 for success, -ENOENT if an ID is already reserved.
    */
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                      unsigned vmhub)

I think we also need to make sure that the hub is a graphics VMHUB.

Quoted 1 line

amdgpu_vmid_free_spm/alloc passed explicitly AMDGPU_GFXHUB(0) only... i left the function deliberately to be generic but caller is within the kernel and we hold control to call with GFXHUB only4

Yeah passing AMDGPU_GFXHUB(*0*) is not 100% correct because we need the vmhub depending on the partition the VM uses.

But I think SPM is not really used on that HW or otherwise somebody would have complained already.

Regards,
Christian.

Quoted 73 lines

Regards
Sunil Khatri

Quoted 69 lines

There is really no use case for an MMHUB here.

Apart from that looks good to me,
Christian.

Quoted 63 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -2
   {
       struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
@@ -629,7 +629,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   }
     /*
- * amdgpu_vmid_free_reserved - free up a reserved VMID again
+ * amdgpu_vmid_free_spm - free up a reserved VMID again
    * @adev: amdgpu device structure
    * @vm: the VM with the reserved ID
    * @vmhub: the VMHUB which should be used
@@ -637,7 +637,7 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    * Waits for all work that used the reserved VMID to finish before unbinding
    * its page table, so submissions still in flight don't suddenly fault.
    */
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                      unsigned vmhub)
   {
       struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +2 -2
index b85aa2ee19b19..c134000eecfdc 100644
@@ -125,9 +125,9 @@ void amdgpu_pasid_mgr_cleanup(void);
   bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
                      struct amdgpu_vmid *id);
   bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub);
-int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                      unsigned vmhub);
-void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                      unsigned vmhub);
   int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
                struct amdgpu_job *job, struct dma_fence **fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -2
index 7fd6215510a94..8d4c13f2ef7fb 100644
@@ -2894,7 +2894,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
       dma_fence_put(vm->last_update);
         for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) {
-        amdgpu_vmid_free_reserved(adev, vm, i);
+        amdgpu_vmid_free_spm(adev, vm, i);
       }
         ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
@@ -2997,7 +2997,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
           int r;
             /* We only have requirement to reserve vmid from gfxhub */
-        r = amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+        r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0));
           if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
               amdgpu_vm_spm_update(adev, vm);
               amdgpu_bo_unreserve(vm->root.bo);
@@ -3005,7 +3005,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
           return r;
       }

      case AMDGPU_VM_OP_UNRESERVE_VMID:
-        amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0)); +        amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0)); break;
      default:
          return -EINVAL;

maintainer
#5 lore raw
In reply to Sunil Khatri
Quoted 93 lines · Sunil Khatri

MES userqueues bypass amdgpu_vm_flush(), so a reservation granted to a userqueue never got its VMID's page table programmed. Add amdgpu_vm_spm_update() to bind the fixed SPM VMID's page-table-base to the reserving VM's root PD when that VM holds the reservation; it is a no-op for every other VM. It is called right after a successful AMDGPU_VM_OP_RESERVE_VMID (amdgpu_vm_ioctl(), under the root PD lock) and from amdgpu_userq_vm_validate_and_restore_queue() (already locked via drm_exec), the two paths that can actually change the binding.

amdgpu_vmid_free_reserved() waits for all work that used the reserved VMID to finish, then unbinds it to a dummy address under the same lock that clears the reservation, so in-flight submissions don't suddenly fault and stray SPM accesses afterward fault instead of racing a new reservation's binding.

Both binds go through the new amdgpu_gmc_bind_vmid() helper, which pairs setup_vm_pt_regs() with the matching TLB flush for a VMID bound outside the normal per-job VM flush path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c   | 24 ++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h   |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c   | 13 ++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c |  7 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 47 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  1 +
 6 files changed, 91 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +24 -0
index 5d4276cd0f7d3..4204c3c750a8d 100644
@@ -787,6 +787,30 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r);
 }
 
+/**
+ * amdgpu_gmc_bind_vmid - bind a VMID's page table base and flush its TLB
+ *
+ * @adev: amdgpu device pointer
+ * @vmhub: vmhub the VMID belongs to
+ * @vmid: VMID to bind
+ * @pd_addr: new page directory address, 0 to unbind to a faulting address
+ *
+ * For VMIDs that are bound directly to a VM's page table outside the
+ * normal per-job VM flush path, e.g. a dedicated out-of-pool VMID.
+ */
+void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub,
+			  unsigned int vmid, uint64_t pd_addr)
+{
+	bool is_mmhub = AMDGPU_IS_MMHUB0(vmhub) || AMDGPU_IS_MMHUB1(vmhub);
+
+	if (is_mmhub)
+		adev->mmhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr);
+	else
+		adev->gfxhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr);
+
+	amdgpu_gmc_flush_gpu_tlb(adev, vmid, vmhub, TLB_FLUSH_LEGACY);
+}
+
 static void amdgpu_gmc_flush_pasid_regs(struct amdgpu_device *adev, u16 pasid,
 					u32 flush_type, bool all_hub,
 					u32 inst)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +2 -0
index 76e6f1f96913c..d17055d4c50a9 100644
@@ -444,6 +444,8 @@ int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev);
 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev);
 void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 			      uint32_t vmhub, uint32_t flush_type);
+void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub,
+			  unsigned int vmid, uint64_t pd_addr);
 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, u16 pasid,
 				   u32 flush_type, bool all_hub, u32 inst);
 int amdgpu_gmc_flush_gpu_tlb_pasid_xccs(struct amdgpu_device *adev, u16 pasid,
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +3 -0
index 074c6942d83b0..e5d84a786c4f2 100644
@@ -633,16 +633,27 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  * @adev: amdgpu device structure
  * @vm: the VM with the reserved ID
  * @vmhub: the VMHUB which should be used
+ *
+ * Waits for all work that used the reserved VMID to finish before unbinding
+ * its page table, so submissions still in flight don't suddenly fault.
  */
 void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)
 {
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];

drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 652

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +5 -1
+	struct amdgpu_vmid *id;
 
 	mutex_lock(&id_mgr->lock);
-	if (vm->reserved_vmid[vmhub]) {
+	id = vm->reserved_vmid[vmhub];
+	if (id) {
 		vm->reserved_vmid[vmhub] = NULL;
 		id_mgr->reserved_vmid = false;
+
+		amdgpu_sync_wait(&id->active, false);

amdgpu_sync_wait() intentionally excludes userqueues (both KFD and gfx). I think that is fine here, but we should probably add a comment.

With that done Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 103 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -0
+
+		if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)
+			amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0),
+					     adev->vm_manager.spm_vmid, 0);
 	}
 	mutex_unlock(&id_mgr->lock);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
index e29823ae8672e..d45357e092bb5 100644
@@ -1075,6 +1075,13 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 		if (unlikely(ret))
 			goto unlock_all;
 
+		/*
+		 * Userqueues never go through amdgpu_vm_flush(), so this is
+		 * the only place that can rebind the SPM VMID's page table
+		 * for a userqueue-held reservation.
+		 */
+		amdgpu_vm_spm_update(adev, vm);
+
 		/* This locks and validates the remaining evicted BOs */
 		ret = amdgpu_userq_bo_validate(adev, &exec, vm);
 		drm_exec_retry_on_contention(&exec);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +45 -2
index de992405ed718..7fd6215510a94 100644
@@ -568,6 +568,41 @@ uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	return result;
 }
 
+/**
+ * amdgpu_vm_spm_update - bind the SPM VMID's page table base to @vm
+ *
+ * @adev: amdgpu device pointer
+ * @vm: vm to check against the SPM reservation
+ *
+ * The SPM VMID is fixed (adev->vm_manager.spm_vmid), so its page-table-base
+ * register must track whichever VM currently holds the reservation instead
+ * of being set per-job like a normal VMID. Bind it to @vm's real root PD
+ * address if @vm holds the reservation; otherwise do nothing, since @vm is
+ * not the one that could legitimately change the current binding.
+ *
+ * @vm's root PD must be locked by the caller.
+ */
+void amdgpu_vm_spm_update(struct amdgpu_device *adev, struct amdgpu_vm *vm)
+{
+	struct amdgpu_kq_vmid_mgr *id_mgr =
+		&adev->vm_manager.id_mgr[AMDGPU_GFXHUB(0)];
+	uint64_t pd_addr;
+
+	if (!adev->vm_manager.spm_vmid || !adev->gfxhub.funcs)
+		return;
+
+	mutex_lock(&id_mgr->lock);
+	if (vm->reserved_vmid[AMDGPU_GFXHUB(0)] !=
+	    &id_mgr->ids[adev->vm_manager.spm_vmid] || !amdgpu_vm_ready(vm)) {
+		mutex_unlock(&id_mgr->lock);
+		return;
+	}
+
+	pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
+	amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0), adev->vm_manager.spm_vmid, pd_addr);
+	mutex_unlock(&id_mgr->lock);
+}
+
 /**
  * amdgpu_vm_validate - validate evicted BOs tracked in the VM
  *
@@ -2958,9 +2993,17 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 		return -EINVAL;
 
 	switch (args->in.op) {
-	case AMDGPU_VM_OP_RESERVE_VMID:
+	case AMDGPU_VM_OP_RESERVE_VMID: {
+		int r;
+
 		/* We only have requirement to reserve vmid from gfxhub */
-		return amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		r = amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
+			amdgpu_vm_spm_update(adev, vm);
+			amdgpu_bo_unreserve(vm->root.bo);
+		}
+		return r;
+	}
 	case AMDGPU_VM_OP_UNRESERVE_VMID:
 		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
 		break;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index b8aa45db1d844..2a2a2ccbaa2cc 100644
@@ -443,6 +443,7 @@ int amdgpu_vm_lock_individual(struct amdgpu_vm *vm, struct drm_exec *exec,
 			      unsigned int num_fences);
 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
 uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm);
+void amdgpu_vm_spm_update(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		       struct ww_acquire_ctx *ticket,
 		       int (*callback)(void *p, struct amdgpu_bo *bo),
#6 lore raw
Quoted 96 lines · Christian König
Quoted 95 lines · Sunil Khatri

MES userqueues bypass amdgpu_vm_flush(), so a reservation granted to a userqueue never got its VMID's page table programmed. Add amdgpu_vm_spm_update() to bind the fixed SPM VMID's page-table-base to the reserving VM's root PD when that VM holds the reservation; it is a no-op for every other VM. It is called right after a successful AMDGPU_VM_OP_RESERVE_VMID (amdgpu_vm_ioctl(), under the root PD lock) and from amdgpu_userq_vm_validate_and_restore_queue() (already locked via drm_exec), the two paths that can actually change the binding.

amdgpu_vmid_free_reserved() waits for all work that used the reserved VMID to finish, then unbinds it to a dummy address under the same lock that clears the reservation, so in-flight submissions don't suddenly fault and stray SPM accesses afterward fault instead of racing a new reservation's binding.

Both binds go through the new amdgpu_gmc_bind_vmid() helper, which pairs setup_vm_pt_regs() with the matching TLB flush for a VMID bound outside the normal per-job VM flush path.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c   | 24 ++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h   |  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c   | 13 ++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c |  7 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 47 ++++++++++++++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  1 +
  6 files changed, 91 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +24 -0
index 5d4276cd0f7d3..4204c3c750a8d 100644
@@ -787,6 +787,30 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
  	dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r);
  }
 
+/**
+ * amdgpu_gmc_bind_vmid - bind a VMID's page table base and flush its TLB
+ *
+ * @adev: amdgpu device pointer
+ * @vmhub: vmhub the VMID belongs to
+ * @vmid: VMID to bind
+ * @pd_addr: new page directory address, 0 to unbind to a faulting address
+ *
+ * For VMIDs that are bound directly to a VM's page table outside the
+ * normal per-job VM flush path, e.g. a dedicated out-of-pool VMID.
+ */
+void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub,
+			  unsigned int vmid, uint64_t pd_addr)
+{
+	bool is_mmhub = AMDGPU_IS_MMHUB0(vmhub) || AMDGPU_IS_MMHUB1(vmhub);
+
+	if (is_mmhub)
+		adev->mmhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr);
+	else
+		adev->gfxhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr);
+
+	amdgpu_gmc_flush_gpu_tlb(adev, vmid, vmhub, TLB_FLUSH_LEGACY);
+}
+
  static void amdgpu_gmc_flush_pasid_regs(struct amdgpu_device *adev, u16 pasid,
  					u32 flush_type, bool all_hub,
  					u32 inst)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +2 -0
index 76e6f1f96913c..d17055d4c50a9 100644
@@ -444,6 +444,8 @@ int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev);
  int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev);
  void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
  			      uint32_t vmhub, uint32_t flush_type);
+void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub,
+			  unsigned int vmid, uint64_t pd_addr);
  int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, u16 pasid,
  				   u32 flush_type, bool all_hub, u32 inst);
  int amdgpu_gmc_flush_gpu_tlb_pasid_xccs(struct amdgpu_device *adev, u16 pasid,
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -0
index 074c6942d83b0..e5d84a786c4f2 100644
@@ -633,16 +633,27 @@ int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   * @adev: amdgpu device structure
   * @vm: the VM with the reserved ID
   * @vmhub: the VMHUB which should be used
+ *
+ * Waits for all work that used the reserved VMID to finish before unbinding
+ * its page table, so submissions still in flight don't suddenly fault.
   */
  void amdgpu_vmid_free_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			       unsigned vmhub)
  {
  	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
+	struct amdgpu_vmid *id;
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 652

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -1
  	mutex_lock(&id_mgr->lock);
-	if (vm->reserved_vmid[vmhub]) {
+	id = vm->reserved_vmid[vmhub];
+	if (id) {
  		vm->reserved_vmid[vmhub] = NULL;
  		id_mgr->reserved_vmid = false;
+
+		amdgpu_sync_wait(&id->active, false);

amdgpu_sync_wait() intentionally excludes userqueues (both KFD and gfx). I think that is fine here, but we should probably add a comment.

Yes, in case it goes with kernel queues. I will add a comment that this only waits in case of kernel queues only.

Regards
Sunil Khatri

Quoted 109 lines

With that done Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 103 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -0
+
+		if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)
+			amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0),
+					     adev->vm_manager.spm_vmid, 0);
  	}
  	mutex_unlock(&id_mgr->lock);
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
index e29823ae8672e..d45357e092bb5 100644
@@ -1075,6 +1075,13 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
  		if (unlikely(ret))
  			goto unlock_all;
 
+		/*
+		 * Userqueues never go through amdgpu_vm_flush(), so this is
+		 * the only place that can rebind the SPM VMID's page table
+		 * for a userqueue-held reservation.
+		 */
+		amdgpu_vm_spm_update(adev, vm);
+
  		/* This locks and validates the remaining evicted BOs */
  		ret = amdgpu_userq_bo_validate(adev, &exec, vm);
  		drm_exec_retry_on_contention(&exec);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +45 -2
index de992405ed718..7fd6215510a94 100644
@@ -568,6 +568,41 @@ uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  	return result;
  }
 
+/**
+ * amdgpu_vm_spm_update - bind the SPM VMID's page table base to @vm
+ *
+ * @adev: amdgpu device pointer
+ * @vm: vm to check against the SPM reservation
+ *
+ * The SPM VMID is fixed (adev->vm_manager.spm_vmid), so its page-table-base
+ * register must track whichever VM currently holds the reservation instead
+ * of being set per-job like a normal VMID. Bind it to @vm's real root PD
+ * address if @vm holds the reservation; otherwise do nothing, since @vm is
+ * not the one that could legitimately change the current binding.
+ *
+ * @vm's root PD must be locked by the caller.
+ */
+void amdgpu_vm_spm_update(struct amdgpu_device *adev, struct amdgpu_vm *vm)
+{
+	struct amdgpu_kq_vmid_mgr *id_mgr =
+		&adev->vm_manager.id_mgr[AMDGPU_GFXHUB(0)];
+	uint64_t pd_addr;
+
+	if (!adev->vm_manager.spm_vmid || !adev->gfxhub.funcs)
+		return;
+
+	mutex_lock(&id_mgr->lock);
+	if (vm->reserved_vmid[AMDGPU_GFXHUB(0)] !=
+	    &id_mgr->ids[adev->vm_manager.spm_vmid] || !amdgpu_vm_ready(vm)) {
+		mutex_unlock(&id_mgr->lock);
+		return;
+	}
+
+	pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
+	amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0), adev->vm_manager.spm_vmid, pd_addr);
+	mutex_unlock(&id_mgr->lock);
+}
+
  /**
   * amdgpu_vm_validate - validate evicted BOs tracked in the VM
   *
@@ -2958,9 +2993,17 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  		return -EINVAL;
 
  	switch (args->in.op) {
-	case AMDGPU_VM_OP_RESERVE_VMID:
+	case AMDGPU_VM_OP_RESERVE_VMID: {
+		int r;
+
  		/* We only have requirement to reserve vmid from gfxhub */
-		return amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		r = amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0));
+		if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
+			amdgpu_vm_spm_update(adev, vm);
+			amdgpu_bo_unreserve(vm->root.bo);
+		}
+		return r;
+	}
  	case AMDGPU_VM_OP_UNRESERVE_VMID:
  		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
  		break;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index b8aa45db1d844..2a2a2ccbaa2cc 100644
@@ -443,6 +443,7 @@ int amdgpu_vm_lock_individual(struct amdgpu_vm *vm, struct drm_exec *exec,
  			      unsigned int num_fences);
  bool amdgpu_vm_ready(struct amdgpu_vm *vm);
  uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm);
+void amdgpu_vm_spm_update(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		       struct ww_acquire_ctx *ticket,
  		       int (*callback)(void *p, struct amdgpu_bo *bo),