The HMM range invalidation callback (.hmm_invalidate) has identical logic across NPU generations: waiting on the GEM object's reservation fences using dma_resv_wait_timeout() with DMA_RESV_USAGE_BOOKKEEP.
Consolidate this callback by moving it from aie2_ctx.c into aie.c as aie_hmm_invalidate(), and declare it in aie.h. Wire .hmm_invalidate to aie_hmm_invalidate in aie2_ops, aie4_vf_ops, and aie4_classic_ops. Co-developed-by: Hayden Laccabue <[email protected]> Signed-off-by: Hayden Laccabue <[email protected]> Signed-off-by: David Zhang <[email protected]> --- drivers/accel/amdxdna/aie.c | 18 ++++++++++++++++++ drivers/accel/amdxdna/aie.h | 2 ++ drivers/accel/amdxdna/aie2_ctx.c | 15 --------------- drivers/accel/amdxdna/aie2_pci.c | 2 +- drivers/accel/amdxdna/aie2_pci.h | 1 - drivers/accel/amdxdna/aie4_pci.c | 2 ++ 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/drivers/accel/amdxdna/aie.c b/drivers/accel/amdxdna/aie.c index 01a439c0ccf4..010d94715ff4 100644 --- a/drivers/accel/amdxdna/aie.c +++ b/drivers/accel/amdxdna/aie.c @@ -3,9 +3,12 @@ * Copyright (C) 2026, Advanced Micro Devices, Inc. */ +#include <linux/dma-resv.h> #include <linux/errno.h> +#include <linux/sched.h> #include "aie.h" +#include "amdxdna_gem.h" #include "amdxdna_mailbox_helper.h" #include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" @@ -202,3 +205,18 @@ void amdxdna_free_msg_buffer(struct amdxdna_dev *xdna, size_t size, dma_free_noncoherent(xdna->ddev.dev, size, cpu_addr, dma_addr, DMA_FROM_DEVICE); } + +void aie_hmm_invalidate(struct amdxdna_gem_obj *abo, + unsigned long cur_seq) +{ + struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); + struct drm_gem_object *gobj = to_gobj(abo); + long ret; + + ret = dma_resv_wait_timeout(gobj->resv, DMA_RESV_USAGE_BOOKKEEP, + true, MAX_SCHEDULE_TIMEOUT); + if (!ret) + XDNA_ERR(xdna, "Failed to wait for bo, ret %ld", ret); + else if (ret == -ERESTARTSYS) + XDNA_DBG(xdna, "Wait for bo interrupted by signal"); +} diff --git a/drivers/accel/amdxdna/aie.h b/drivers/accel/amdxdna/aie.h index 6268b708d17b..5cb9ebf45379 100644 --- a/drivers/accel/amdxdna/aie.h +++ b/drivers/accel/amdxdna/aie.h @@ -14,6 +14,7 @@ struct psp_device; struct smu_device; +struct amdxdna_gem_obj; struct aie_device { struct amdxdna_dev *xdna; @@ -141,6 +142,7 @@ void *amdxdna_alloc_msg_buffer(struct amdxdna_dev *xdna, u32 *size, dma_addr_t *dma_addr); void amdxdna_free_msg_buffer(struct amdxdna_dev *xdna, size_t size, void *cpu_addr, dma_addr_t dma_addr); +void aie_hmm_invalidate(struct amdxdna_gem_obj *abo, unsigned long cur_seq); int amdxdna_get_aie_version(struct amdxdna_client *client, struct amdxdna_drm_get_info *args, struct amdxdna_drm_query_aie_version *version); diff --git a/drivers/accel/amdxdna/aie2_ctx.c b/drivers/accel/amdxdna/aie2_ctx.c index d927c8c9d557..8c1b29964f8f 100644 --- a/drivers/accel/amdxdna/aie2_ctx.c +++ b/drivers/accel/amdxdna/aie2_ctx.c @@ -1277,21 +1277,6 @@ int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, return ret; } -void aie2_hmm_invalidate(struct amdxdna_gem_obj *abo, - unsigned long cur_seq) -{ - struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); - struct drm_gem_object *gobj = to_gobj(abo); - long ret; - - ret = dma_resv_wait_timeout(gobj->resv, DMA_RESV_USAGE_BOOKKEEP, - true, MAX_SCHEDULE_TIMEOUT); - if (!ret) - XDNA_ERR(xdna, "Failed to wait for bo, ret %ld", ret); - else if (ret == -ERESTARTSYS) - XDNA_DBG(xdna, "Wait for bo interrupted by signal"); -} - int aie2_hwctx_heap_expand(struct amdxdna_hwctx *hwctx, struct amdxdna_gem_obj *heap) { diff --git a/drivers/accel/amdxdna/aie2_pci.c b/drivers/accel/amdxdna/aie2_pci.c index b70af1923643..c4e916e832cb 100644 --- a/drivers/accel/amdxdna/aie2_pci.c +++ b/drivers/accel/amdxdna/aie2_pci.c @@ -1216,7 +1216,7 @@ const struct amdxdna_dev_ops aie2_ops = { .hwctx_config = aie2_hwctx_config, .hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo, .cmd_submit = aie2_cmd_submit, - .hmm_invalidate = aie2_hmm_invalidate, + .hmm_invalidate = aie_hmm_invalidate, .get_array = aie2_get_array, .get_dev_revision = aie2_get_dev_rev, .hwctx_heap_expand = aie2_hwctx_heap_expand, diff --git a/drivers/accel/amdxdna/aie2_pci.h b/drivers/accel/amdxdna/aie2_pci.h index 0c8dd6510292..ba17df97223a 100644 --- a/drivers/accel/amdxdna/aie2_pci.h +++ b/drivers/accel/amdxdna/aie2_pci.h @@ -272,7 +272,6 @@ int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl); void aie2_hwctx_suspend(struct amdxdna_client *client); int aie2_hwctx_resume(struct amdxdna_client *client); int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq); -void aie2_hmm_invalidate(struct amdxdna_gem_obj *abo, unsigned long cur_seq); int aie2_hwctx_heap_expand(struct amdxdna_hwctx *hwctx, struct amdxdna_gem_obj *heap); #endif /* _AIE2_PCI_H_ */ diff --git a/drivers/accel/amdxdna/aie4_pci.c b/drivers/accel/amdxdna/aie4_pci.c index c7b12bde50c0..3b930197d5ee 100644 --- a/drivers/accel/amdxdna/aie4_pci.c +++ b/drivers/accel/amdxdna/aie4_pci.c @@ -1082,6 +1082,7 @@ const struct amdxdna_dev_ops aie4_vf_ops = { .hwctx_fini = aie4_hwctx_fini, .cmd_submit = aie4_cmd_submit, .cmd_wait = aie4_cmd_wait, + .hmm_invalidate = aie_hmm_invalidate, .get_aie_info = aie4_get_info, .set_aie_state = aie4_set_state, }; @@ -1095,6 +1096,7 @@ const struct amdxdna_dev_ops aie4_classic_ops = { .hwctx_fini = aie4_hwctx_fini, .cmd_submit = aie4_cmd_submit, .cmd_wait = aie4_cmd_wait, + .hmm_invalidate = aie_hmm_invalidate, .get_aie_info = aie4_get_info, .set_aie_state = aie4_set_state, }; -- 2.34.1
