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 and flushes only the given hub, and amdgpu_vmid_alloc_spm() and amdgpu_vmid_free_spm() take the set of GFXHUBs directly, reserving the VMID on every hub the VM's jobs can run on. The hub loops use a logical XCC mask (GENMASK(NUM_XCC() - 1, 0)): the GFXHUBs are indexed by logical XCC, while adev->gfx.xcc_mask holds physical instances and can be sparse when an XCC is harvested. On a partial reservation failure only the hubs the call newly reserved are released again, after waiting for jobs which might have grabbed the VMID in the meantime. Both functions require the root PD reservation as a documented, asserted lock contract; AMDGPU_VM_OP_RESERVE_VMID takes it before reserving, which also serializes the per-VM reservation state against concurrent RESERVE/UNRESERVE calls and makes a failed root PD reservation an ioctl error instead of silently skipping the bind. amdgpu_vm_fini() gives the reservation up while the root PD is still held and checks every hub, not just hub 0, before waiting for jobs. amdgpu_vm_spm_update() binds every GFXHUB on which @vm holds the reservation, so 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 | 5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 102 +++++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 87 +++++++++++++------- 4 files changed, 150 insertions(+), 48 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index c0efb90e1d2e..97068471e7a1 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c @@ -797,6 +797,9 @@ 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 flushed, and for a GFXHUB only @vmhub is programmed; + * the MMHUB callback still programs every AID's MMHUB on IPs with + * more than one. */ void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub, unsigned int vmid, uint64_t pd_addr) @@ -807,7 +810,7 @@ void amdgpu_gmc_bind_vmid(struct amdgpu_device *adev, unsigned int vmhub, adev->mmhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr); else adev->gfxhub.funcs->setup_vm_pt_regs(adev, vmid, pd_addr, - amdgpu_gfx_logical_xcc_mask(adev)); + BIT(vmhub - AMDGPU_GFXHUB_START)); amdgpu_gmc_flush_gpu_tlb(adev, vmid, vmhub, TLB_FLUSH_LEGACY); } diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c index 9c879ac68352..cf4cc798912d 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c @@ -589,17 +589,15 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub) } /* - * amdgpu_vmid_alloc_spm - reserve a specific VMID for this vm + * amdgpu_vmid_alloc_spm_hub - reserve the SPM VMID on one VMHUB * @adev: amdgpu device structure * @vm: the VM to reserve an ID for * @vmhub: the VMHUB which should be used * - * Mostly used to have a reserved VMID for debugging and SPM. - * * Returns: 0 for success, -ENOENT if an ID is already reserved. */ -int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, - unsigned vmhub) +static int amdgpu_vmid_alloc_spm_hub(struct amdgpu_device *adev, + struct amdgpu_vm *vm, unsigned int vmhub) { struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; struct amdgpu_vmid *id; @@ -628,18 +626,8 @@ int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, return r; } -/* - * 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 - * - * The caller must wait for all work that used the reserved VMID to finish - * before, so submissions still in flight don't suddenly fault when its page - * table gets unbound. - */ -void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, - unsigned vmhub) +static void amdgpu_vmid_free_spm_hub(struct amdgpu_device *adev, + struct amdgpu_vm *vm, unsigned int vmhub) { struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; struct amdgpu_vmid *id; @@ -651,12 +639,90 @@ 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_free_spm - free up a reserved VMID again + * @adev: amdgpu device structure + * @vm: the VM with the reserved ID + * @gfxhub_mask: logical XCCs on whose GFXHUBs to free the reservation + * + * Free the SPM VMID on every GFXHUB in @gfxhub_mask @vm holds it on, hubs + * without a reservation are skipped. The caller must hold @vm's root PD + * reservation, and must wait for all work that used the reserved VMID to + * finish before, so submissions still in flight don't suddenly fault when + * its page table gets unbound. + */ +void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, + uint32_t gfxhub_mask) +{ + int i; + + dma_resv_assert_held(vm->root.bo->tbo.base.resv); + + for_each_inst(i, gfxhub_mask) + amdgpu_vmid_free_spm_hub(adev, vm, AMDGPU_GFXHUB(i)); +} + +/* + * amdgpu_vmid_alloc_spm - reserve the SPM VMID for this vm + * @adev: amdgpu device structure + * @vm: the VM to reserve the ID for + * @gfxhub_mask: logical XCCs on whose GFXHUBs to reserve + * + * Mostly used to have a reserved VMID for debugging and SPM, reserved on + * the GFXHUB of every XCC in @gfxhub_mask so that the reservation covers + * every hub the VM's jobs can run on. + * + * The caller must hold @vm's root PD reservation: it serializes the + * per-VM reservation state and keeps new jobs of @vm from grabbing a + * freshly reserved VMID while the error path unbinds it again. + * + * Returns: 0 for success, the first per-hub error otherwise (-ENOENT if + * the ID is already reserved). 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(struct amdgpu_device *adev, struct amdgpu_vm *vm, + uint32_t gfxhub_mask) +{ + uint32_t allocated = 0; + int i, r; + + dma_resv_assert_held(vm->root.bo->tbo.base.resv); + + for_each_inst(i, gfxhub_mask) { + bool held = amdgpu_vmid_uses_reserved(vm, AMDGPU_GFXHUB(i)); + + r = amdgpu_vmid_alloc_spm_hub(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(adev, vm, allocated); + } + return r; +} + /** * 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..f2a721c836af 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h @@ -126,9 +126,9 @@ 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_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, - unsigned vmhub); + uint32_t gfxhub_mask); void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, - unsigned vmhub); + uint32_t gfxhub_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..e81fbe32e3c8 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c @@ -589,30 +589,45 @@ 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)]; - uint64_t pd_addr; + uint32_t gfxhub_mask = amdgpu_gfx_logical_xcc_mask(adev); + uint64_t pd_addr = 0; + 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)]; + + /* + * reserved_vmid[] only changes under the root PD reservation + * the caller holds, so the common case - a VM not holding + * the SPM VMID - gets away without taking any lock. + */ + if (vm->reserved_vmid[AMDGPU_GFXHUB(i)] != + &id_mgr->ids[adev->vm_manager.spm_vmid]) + continue; + + if (!pd_addr) + pd_addr = amdgpu_gmc_pd_addr(vm->root.bo); + + mutex_lock(&id_mgr->lock); + amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(i), + adev->vm_manager.spm_vmid, pd_addr); + mutex_unlock(&id_mgr->lock); + } } /** @@ -2871,9 +2886,15 @@ 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; + } + } + amdgpu_vmid_free_spm(adev, vm, amdgpu_gfx_logical_xcc_mask(adev)); dma_fence_wait(vm->last_unlocked, false); dma_fence_put(vm->last_unlocked); dma_fence_wait(vm->last_seq_flush, false); @@ -2914,10 +2935,6 @@ 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_spm(adev, vm, i); - } - ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move); if (!amdgpu_vm_stats_is_zero(vm)) { @@ -3015,26 +3032,42 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) switch (args->in.op) { case AMDGPU_VM_OP_RESERVE_VMID: { + uint32_t gfxhub_mask = amdgpu_gfx_logical_xcc_mask(adev); int r; + /* + * The root PD reservation serializes the per-VM reservation + * state and keeps new jobs of this VM from grabbing a freshly + * reserved VMID while an error path would unbind it again. + */ + r = amdgpu_bo_reserve(vm->root.bo, true); + if (r) + return r; /* We only have requirement to reserve vmid from gfxhub */ - r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0)); - if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) { + r = amdgpu_vmid_alloc_spm(adev, vm, gfxhub_mask); + if (!r) amdgpu_vm_spm_update(adev, vm); - amdgpu_bo_unreserve(vm->root.bo); - } + amdgpu_bo_unreserve(vm->root.bo); return r; } case AMDGPU_VM_OP_UNRESERVE_VMID: { + uint32_t gfxhub_mask = amdgpu_gfx_logical_xcc_mask(adev); struct amdgpu_bo *root = vm->root.bo; + bool reserved = false; + int i, r; - amdgpu_bo_reserve(root, true); + r = amdgpu_bo_reserve(root, true); + if (r) + return r; + 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(adev, vm, gfxhub_mask); amdgpu_bo_unreserve(root); break; } -- 2.34.1
