Re: [PATCH v2 8/9] drm/xe/migrate: Introduce xe_migrate_reinit()

Matthew Brost
2026-02-27T18:34:30Z

On Fri, Feb 27, 2026 at 10:30:48PM +0530, Raag Jadav wrote:
> In preparation of usecases which require re-initializing migrate context
> after PCIe FLR, introduce xe_migrate_reinit() helper. Migrate exec queue
> and pt_bo already exist in migrate structure but since their contents live
> on VRAM, they are lost on PCIe FLR and need re-initialization.
>
> Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> ---
> v2: Re-initialize migrate context (Matthew Brost)
> ---
> drivers/gpu/drm/xe/xe_gt.c | 10 +++++
> drivers/gpu/drm/xe/xe_migrate.c | 65 +++++++++++++++++++++++++--------
> drivers/gpu/drm/xe/xe_migrate.h | 1 +
> 3 files changed, 61 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/gpu/drm/xe/xe_gt.c b/drivers/gpu/drm/xe/xe_gt.c
> index ff4a4e769fb1..ab34312830e3 100644
> --- a/drivers/gpu/drm/xe/xe_gt.c
> +++ b/drivers/gpu/drm/xe/xe_gt.c
> @@ -966,6 +966,16 @@ void xe_gt_flr_prepare(struct xe_gt *gt)
> */
> int xe_gt_flr_done(struct xe_gt *gt)
> {
> + int err;
> +
> + if (xe_gt_is_main_type(gt)) {
> + struct xe_tile *tile = gt_to_tile(gt);
> +
> + err = xe_migrate_reinit(tile->migrate);

I think there is work ahead for future platforms where multiple tiles
point to the same migrate object, so we’ll need to keep an eye on that.
Perhaps calling xe_migrate_reinit twice is harmless too.

Anyways the patch looks correct.

Reviewed-by: Matthew Brost <matthew.brost@intel.com>

> + if (err)
> + return err;
> + }
> +
> return xe_uc_flr_done(&gt->uc);
> }
>
> diff --git a/drivers/gpu/drm/xe/xe_migrate.c b/drivers/gpu/drm/xe/xe_migrate.c
> index 333af7b57ae9..f4a2ad3ce601 100644
> --- a/drivers/gpu/drm/xe/xe_migrate.c
> +++ b/drivers/gpu/drm/xe/xe_migrate.c
> @@ -184,19 +184,11 @@ static void xe_migrate_program_identity(struct xe_device *xe, struct xe_vm *vm,
> xe_assert(xe, pos == vram_limit);
> }
>
> -static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
> - struct xe_vm *vm, struct drm_exec *exec)
> +static int xe_migrate_pt_bo_alloc(struct xe_tile *tile, struct xe_migrate *m,
> + struct xe_vm *vm, struct drm_exec *exec)
> {
> - struct xe_device *xe = tile_to_xe(tile);
> - u16 pat_index = xe->pat.idx[XE_CACHE_WB];
> - u8 id = tile->id;
> - u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level;
> -#define VRAM_IDENTITY_MAP_COUNT 2
> - u32 num_setup = num_level + VRAM_IDENTITY_MAP_COUNT;
> -#undef VRAM_IDENTITY_MAP_COUNT
> - u32 map_ofs, level, i;
> struct xe_bo *bo, *batch = tile->mem.kernel_bb_pool->bo;
> - u64 entry, pt29_ofs;
> + u32 num_entries = NUM_PT_SLOTS;
>
> /* Can't bump NUM_PT_SLOTS too high */
> BUILD_BUG_ON(NUM_PT_SLOTS > SZ_2M/XE_PAGE_SIZE);
> @@ -216,6 +208,24 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
> if (IS_ERR(bo))
> return PTR_ERR(bo);
>
> + m->pt_bo = bo;
> + return 0;
> +}
> +
> +static void xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
> + struct xe_vm *vm, u32 *ofs)
> +{
> + struct xe_device *xe = tile_to_xe(tile);
> + u16 pat_index = xe->pat.idx[XE_CACHE_WB];
> + u8 id = tile->id;
> + u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level;
> +#define VRAM_IDENTITY_MAP_COUNT 2
> + u32 num_setup = num_level + VRAM_IDENTITY_MAP_COUNT;
> +#undef VRAM_IDENTITY_MAP_COUNT
> + u32 map_ofs, level, i;
> + struct xe_bo *bo = m->pt_bo, *batch = tile->mem.kernel_bb_pool->bo;
> + u64 entry, pt29_ofs;
> +
> /* PT30 & PT31 reserved for 2M identity map */
> pt29_ofs = xe_bo_size(bo) - 3 * XE_PAGE_SIZE;
> entry = vm->pt_ops->pde_encode_bo(bo, pt29_ofs);
> @@ -338,6 +348,12 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
> }
> }
>
> + if (ofs)
> + *ofs = map_ofs;
> +}
> +
> +static void xe_migrate_suballoc_manager_init(struct xe_migrate *m, u32 map_ofs)
> +{
> /*
> * Example layout created above, with root level = 3:
> * [PT0...PT7]: kernel PT's for copy/clear; 64 or 4KiB PTE's
> @@ -363,9 +379,6 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
> drm_suballoc_manager_init(&m->vm_update_sa,
> (size_t)(map_ofs / XE_PAGE_SIZE - NUM_KERNEL_PDE) *
> NUM_VMUSA_UNIT_PER_PAGE, 0);
> -
> - m->pt_bo = bo;
> - return 0;
> }
>
> /*
> @@ -416,12 +429,22 @@ static int xe_migrate_lock_prepare_vm(struct xe_tile *tile, struct xe_migrate *m
> struct xe_device *xe = tile_to_xe(tile);
> struct xe_validation_ctx ctx;
> struct drm_exec exec;
> + u32 map_ofs;
> int err = 0;
>
> xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) {
> err = xe_vm_drm_exec_lock(vm, &exec);
> + if (err)
> + return err;
> +
> drm_exec_retry_on_contention(&exec);
> - err = xe_migrate_prepare_vm(tile, m, vm, &exec);
> +
> + err = xe_migrate_pt_bo_alloc(tile, m, vm, &exec);
> + if (err)
> + return err;
> +
> + xe_migrate_prepare_vm(tile, m, vm, &map_ofs);
> + xe_migrate_suballoc_manager_init(m, map_ofs);
> drm_exec_retry_on_contention(&exec);
> xe_validation_retry_on_oom(&ctx, &err);
> }
> @@ -429,6 +452,18 @@ static int xe_migrate_lock_prepare_vm(struct xe_tile *tile, struct xe_migrate *m
> return err;
> }
>
> +/**
> + * xe_migrate_reinit() - Re-initialize a migrate context
> + * @m: The migration context
> + *
> + * Returns: 0 on success, negative error code otherwise.
> + */
> +int xe_migrate_reinit(struct xe_migrate *m)
> +{
> + xe_migrate_prepare_vm(m->tile, m, m->q->vm, NULL);
> + return xe_exec_queue_reinit(m->q);
> +}
> +
> /**
> * xe_migrate_init() - Initialize a migrate context
> * @m: The migration context
> diff --git a/drivers/gpu/drm/xe/xe_migrate.h b/drivers/gpu/drm/xe/xe_migrate.h
> index 1522afb37dcf..fffbcab8b2e3 100644
> --- a/drivers/gpu/drm/xe/xe_migrate.h
> +++ b/drivers/gpu/drm/xe/xe_migrate.h
> @@ -112,6 +112,7 @@ struct xe_migrate_pt_update {
>
> struct xe_migrate *xe_migrate_alloc(struct xe_tile *tile);
> int xe_migrate_init(struct xe_migrate *m);
> +int xe_migrate_reinit(struct xe_migrate *m);
>
> struct dma_fence *xe_migrate_to_vram(struct xe_migrate *m,
> unsigned long npages,
> --
> 2.43.0
>