The SPM VMID reservation made through AMDGPU_VM_OP_RESERVE_VMID is recorded on AMDGPU_GFXHUB(0) only, while gfxhub v1.2's setup_vm_pt_regs broadcasts the page table base to every XCC. On multi-XCC parts the two disagree, in both directions:
- the reservation only takes effect for jobs running on XCC 0, since the VMID grab path checks vm->reserved_vmid[ring->vm_hub] and the rings of XCC i use AMDGPU_GFXHUB(i); - the page table base is physically programmed on all XCCs anyway, and conflicting reservations are only detected on hub 0's id_mgr. Make the hub argument real end to end: amdgpu_gmc_bind_vmid() programs only the given hub, through the XCC-masked gfxhub callback where one exists, and amdgpu_vmid_free_spm() unbinds the hub it frees instead of hub 0. amdgpu_vm_spm_update() binds every GFXHUB on which @vm holds the reservation. The VM ioctl reserves the VMID on every GFXHUB instance through the new amdgpu_vmid_alloc_spm_gfxhubs() helper, so the reservation now takes effect, is bound and is flushed on every XCC the VM's jobs can run on, and a conflicting reservation shows up on whichever hub two VMs share. Signed-off-by: James Zhu <[email protected]> Assisted-by: Claude:claude-fable-5 --- drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 4 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 68 ++++++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 4 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 56 +++++++++++++------- 4 files changed, 114 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index 08d3ff61edcc..704a30ad388a 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c @@ -797,6 +797,7 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid, * * 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. + * Only @vmhub is programmed and flushed, other hubs keep their binding. */ void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub, unsigned int vmid, uint64_t pd_addr) @@ -805,6 +806,9 @@ void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub, if (is_mmhub) adev->mmhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr); + else if (adev->gfxhub.funcs->setup_vm_pt_regs_xcc) + adev->gfxhub.funcs->setup_vm_pt_regs_xcc(adev, vmid, pd_addr, + BIT(vmhub - AMDGPU_GFXHUB_START)); else adev->gfxhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c index 9c879ac68352..a4403e5cc344 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c @@ -651,12 +651,78 @@ void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, id_mgr->reserved_vmid = false; if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs) - amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0), + amdgpu_gmc_bind_vmid(adev, vmhub, adev->vm_manager.spm_vmid, 0); } mutex_unlock(&id_mgr->lock); } +/* + * amdgpu_vmid_alloc_spm_gfxhubs - reserve the SPM VMID on a set of GFXHUBs + * @adev: amdgpu device structure + * @vm: the VM to reserve the ID for + * @xcc_mask: XCC instances on whose GFXHUBs to reserve + * + * Reserve the SPM VMID on the GFXHUB of every XCC in @xcc_mask, so that + * the reservation covers every hub the VM's jobs can run on. + * + * Returns: 0 for success, the first per-hub error otherwise. On an error + * only the hubs this call reserved are released again, after waiting for + * the jobs which might have grabbed the reserved VMID in the meantime; + * hubs @vm already held before the call stay reserved. + */ +int amdgpu_vmid_alloc_spm_gfxhubs(struct amdgpu_device *adev, + struct amdgpu_vm *vm, uint32_t xcc_mask) +{ + uint32_t allocated = 0; + int i, r; + + for_each_inst(i, xcc_mask) { + bool held = amdgpu_vmid_uses_reserved(vm, AMDGPU_GFXHUB(i)); + + r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(i)); + if (r) + goto error_free; + if (!held) + allocated |= BIT(i); + } + + return 0; + +error_free: + /* + * A job may already have grabbed a freshly reserved VMID, give it + * the wait amdgpu_vmid_free_spm() demands before the rollback + * unbinds the VMID again. + */ + if (allocated) { + dma_resv_wait_timeout(vm->root.bo->tbo.base.resv, + DMA_RESV_USAGE_READ, false, + MAX_SCHEDULE_TIMEOUT); + amdgpu_vmid_free_spm_gfxhubs(adev, vm, allocated); + } + return r; +} + +/* + * amdgpu_vmid_free_spm_gfxhubs - undo amdgpu_vmid_alloc_spm_gfxhubs + * @adev: amdgpu device structure + * @vm: the VM with the reserved ID + * @xcc_mask: XCC instances on whose GFXHUBs to free the reservation + * + * Free the SPM VMID on every GFXHUB in @xcc_mask @vm holds it on, hubs + * without a reservation are skipped. The same wait requirement as for + * amdgpu_vmid_free_spm() applies. + */ +void amdgpu_vmid_free_spm_gfxhubs(struct amdgpu_device *adev, + struct amdgpu_vm *vm, uint32_t xcc_mask) +{ + int i; + + for_each_inst(i, xcc_mask) + amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(i)); +} + /** * amdgpu_vmid_reset - reset VMID to zero * diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h index c134000eecfd..d2b225d0aa38 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h @@ -129,6 +129,10 @@ int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, unsigned vmhub); void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, unsigned vmhub); +int amdgpu_vmid_alloc_spm_gfxhubs(struct amdgpu_device *adev, + struct amdgpu_vm *vm, uint32_t xcc_mask); +void amdgpu_vmid_free_spm_gfxhubs(struct amdgpu_device *adev, + struct amdgpu_vm *vm, uint32_t xcc_mask); int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring, struct amdgpu_job *job, struct dma_fence **fence); void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub, diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c index 6b4dc0276e27..a9c23759d058 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c @@ -589,30 +589,38 @@ uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm) * 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. + * address on every GFXHUB @vm holds the reservation on; hubs reserved by + * other VMs or not reserved at all are left alone, since @vm is not the + * one that could legitimately change their 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)]; + /* the GFXHUBs are indexed by logical XCC, adev->gfx.xcc_mask is physical */ + uint32_t gfxhub_mask = GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0); uint64_t pd_addr; + int i; 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); + if (!amdgpu_vm_ready(vm)) 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); + for_each_inst(i, gfxhub_mask) { + struct amdgpu_kq_vmid_mgr *id_mgr = + &adev->vm_manager.id_mgr[AMDGPU_GFXHUB(i)]; + + mutex_lock(&id_mgr->lock); + if (vm->reserved_vmid[AMDGPU_GFXHUB(i)] == + &id_mgr->ids[adev->vm_manager.spm_vmid]) + amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(i), + adev->vm_manager.spm_vmid, + pd_addr); + mutex_unlock(&id_mgr->lock); + } } /** @@ -2871,9 +2879,14 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) root = amdgpu_bo_ref(vm->root.bo); amdgpu_bo_reserve(root, true); /* Wait for the jobs which might still use the reserved VMID */ - if (vm->reserved_vmid[AMDGPU_GFXHUB(0)]) - dma_resv_wait_timeout(root->tbo.base.resv, DMA_RESV_USAGE_READ, - false, MAX_SCHEDULE_TIMEOUT); + for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) { + if (vm->reserved_vmid[i]) { + dma_resv_wait_timeout(root->tbo.base.resv, + DMA_RESV_USAGE_READ, + false, MAX_SCHEDULE_TIMEOUT); + break; + } + } dma_fence_wait(vm->last_unlocked, false); dma_fence_put(vm->last_unlocked); dma_fence_wait(vm->last_seq_flush, false); @@ -3015,10 +3028,12 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) switch (args->in.op) { case AMDGPU_VM_OP_RESERVE_VMID: { + /* logical XCCs, the GFXHUB index space; xcc_mask is physical */ + uint32_t gfxhub_mask = GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0); int r; /* We only have requirement to reserve vmid from gfxhub */ - r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0)); + r = amdgpu_vmid_alloc_spm_gfxhubs(adev, vm, gfxhub_mask); if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) { amdgpu_vm_spm_update(adev, vm); amdgpu_bo_unreserve(vm->root.bo); @@ -3026,15 +3041,22 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) return r; } case AMDGPU_VM_OP_UNRESERVE_VMID: { + /* logical XCCs, the GFXHUB index space; xcc_mask is physical */ + uint32_t gfxhub_mask = GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0); struct amdgpu_bo *root = vm->root.bo; + bool reserved = false; + int i; amdgpu_bo_reserve(root, true); + for_each_inst(i, gfxhub_mask) + reserved |= amdgpu_vmid_uses_reserved(vm, + AMDGPU_GFXHUB(i)); /* Wait for the jobs which might still use the reserved VMID */ - if (vm->reserved_vmid[AMDGPU_GFXHUB(0)]) + if (reserved) dma_resv_wait_timeout(root->tbo.base.resv, DMA_RESV_USAGE_READ, false, MAX_SCHEDULE_TIMEOUT); - amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0)); + amdgpu_vmid_free_spm_gfxhubs(adev, vm, gfxhub_mask); amdgpu_bo_unreserve(root); break; } -- 2.34.1
