[PATCH 23/26] drm/xe/pf: Handle VRAM migration data as part of PF control

Michał Winiarski
2025-10-11T19:42:05Z

Connect the helpers to allow save and restore of VRAM migration data in
stop_copy / resume device state.

Signed-off-by: Michał Winiarski <michal.winiarski@intel.com>
---
drivers/gpu/drm/xe/xe_gt_sriov_pf_control.c | 13 +
.../gpu/drm/xe/xe_gt_sriov_pf_control_types.h | 1 +
drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c | 228 ++++++++++++++++++
drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.h | 4 +
4 files changed, 246 insertions(+)

diff --git a/drivers/gpu/drm/xe/xe_gt_sriov_pf_control.c b/drivers/gpu/drm/xe/xe_gt_sriov_pf_control.c
index 7f8f816c10f20..646914a3f7121 100644
--- a/drivers/gpu/drm/xe/xe_gt_sriov_pf_control.c
+++ b/drivers/gpu/drm/xe/xe_gt_sriov_pf_control.c
@@ -190,6 +190,7 @@ static const char *control_bit_to_string(enum xe_gt_sriov_control_bits bit)
CASE2STR(SAVE_DATA_GUC);
CASE2STR(SAVE_DATA_GGTT);
CASE2STR(SAVE_DATA_MMIO);
+ CASE2STR(SAVE_DATA_VRAM);
CASE2STR(SAVE_FAILED);
CASE2STR(SAVED);
CASE2STR(RESTORE_WIP);
@@ -805,6 +806,7 @@ void xe_gt_sriov_pf_control_vf_data_eof(struct xe_gt *gt, unsigned int vfid)

static void pf_exit_vf_save_wip(struct xe_gt *gt, unsigned int vfid)
{
+ pf_escape_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_VRAM);
pf_escape_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_MMIO);
pf_escape_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_GGTT);
pf_escape_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_GUC);
@@ -861,6 +863,13 @@ static bool pf_handle_vf_save_wip(struct xe_gt *gt, unsigned int vfid)
return true;
}

+ if (pf_exit_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_VRAM)) {
+ ret = xe_gt_sriov_pf_migration_vram_save(gt, vfid);
+ if (ret)
+ goto err;
+ return true;
+ }
+
xe_gt_sriov_pf_control_vf_data_eof(gt, vfid);
pf_exit_vf_save_wip(gt, vfid);
pf_enter_vf_saved(gt, vfid);
@@ -884,6 +893,8 @@ static bool pf_enter_vf_save_wip(struct xe_gt *gt, unsigned int vfid)
pf_enter_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_GGTT);
if (xe_gt_sriov_pf_migration_mmio_size(gt, vfid) > 0)
pf_enter_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_MMIO);
+ if (xe_gt_sriov_pf_migration_vram_size(gt, vfid) > 0)
+ pf_enter_vf_state(gt, vfid, XE_GT_SRIOV_STATE_SAVE_DATA_VRAM);
pf_queue_vf(gt, vfid);
return true;
}
@@ -998,6 +1009,8 @@ static int pf_handle_vf_restore_data(struct xe_gt *gt, unsigned int vfid,
return xe_gt_sriov_pf_migration_mmio_restore(gt, vfid, data);
case XE_SRIOV_MIG_DATA_GUC:
return xe_gt_sriov_pf_migration_guc_restore(gt, vfid, data);
+ case XE_SRIOV_MIG_DATA_VRAM:
+ return xe_gt_sriov_pf_migration_vram_restore(gt, vfid, data);
default:
xe_gt_sriov_notice(gt, "Skipping VF%u invalid data type: %d\n", vfid, data->type);
pf_enter_vf_restore_failed(gt, vfid);
diff --git a/drivers/gpu/drm/xe/xe_gt_sriov_pf_control_types.h b/drivers/gpu/drm/xe/xe_gt_sriov_pf_control_types.h
index f8647722bfb3c..d7efe4a3bab92 100644
--- a/drivers/gpu/drm/xe/xe_gt_sriov_pf_control_types.h
+++ b/drivers/gpu/drm/xe/xe_gt_sriov_pf_control_types.h
@@ -74,6 +74,7 @@ enum xe_gt_sriov_control_bits {
XE_GT_SRIOV_STATE_SAVE_DATA_GUC,
XE_GT_SRIOV_STATE_SAVE_DATA_GGTT,
XE_GT_SRIOV_STATE_SAVE_DATA_MMIO,
+ XE_GT_SRIOV_STATE_SAVE_DATA_VRAM,
XE_GT_SRIOV_STATE_SAVE_FAILED,
XE_GT_SRIOV_STATE_SAVED,

diff --git a/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c b/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c
index 43e6e1abb92f9..d0beec25bc86c 100644
--- a/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c
+++ b/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c
@@ -15,6 +15,7 @@
#include "xe_gt_sriov_printk.h"
#include "xe_guc_buf.h"
#include "xe_guc_ct.h"
+#include "xe_migrate.h"
#include "xe_sriov.h"
#include "xe_sriov_pf_migration.h"
#include "xe_sriov_pf_migration_data.h"
@@ -476,6 +477,226 @@ int xe_gt_sriov_pf_migration_mmio_restore(struct xe_gt *gt, unsigned int vfid,
return pf_restore_vf_mmio_mig_data(gt, vfid, data);
}

+/**
+ * xe_gt_sriov_pf_migration_vram_size() - Get the size of VF VRAM migration data.
+ * @gt: the &struct xe_gt
+ * @vfid: the VF identifier
+ *
+ * This function is for PF only.
+ *
+ * Return: size in bytes or a negative error code on failure.
+ */
+ssize_t xe_gt_sriov_pf_migration_vram_size(struct xe_gt *gt, unsigned int vfid)
+{
+ if (gt != xe_root_mmio_gt(gt_to_xe(gt)))
+ return 0;
+
+ return xe_gt_sriov_pf_config_get_lmem(gt, vfid);
+}
+
+static struct dma_fence *__pf_save_restore_vram(struct xe_gt *gt, unsigned int vfid,
+ struct xe_bo *vram, u64 vram_offset,
+ struct xe_bo *sysmem, u64 sysmem_offset,
+ struct xe_bo *ccs, u64 ccs_offset,
+ size_t size, bool save)
+{
+ struct dma_fence *ret = NULL;
+ struct drm_exec exec;
+ int err;
+
+ xe_gt_assert(gt, sysmem || ccs);
+
+ drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
+ drm_exec_until_all_locked(&exec) {
+ err = drm_exec_lock_obj(&exec, &vram->ttm.base);
+ drm_exec_retry_on_contention(&exec);
+ if (err) {
+ ret = ERR_PTR(err);
+ goto err;
+ }
+
+ if (sysmem) {
+ err = drm_exec_lock_obj(&exec, &sysmem->ttm.base);
+ drm_exec_retry_on_contention(&exec);
+ if (err) {
+ ret = ERR_PTR(err);
+ goto err;
+ }
+ }
+
+ if (ccs) {
+ err = drm_exec_lock_obj(&exec, &ccs->ttm.base);
+ drm_exec_retry_on_contention(&exec);
+ if (err) {
+ ret = ERR_PTR(err);
+ goto err;
+ }
+ }
+ }
+
+ ret = xe_migrate_raw_vram_copy(vram, vram_offset,
+ sysmem, sysmem_offset,
+ ccs, ccs_offset,
+ size, save);
+
+err:
+ drm_exec_fini(&exec);
+
+ return ret;
+}
+
+static int pf_save_vram_chunk(struct xe_gt *gt, unsigned int vfid,
+ struct xe_bo *src_vram, u64 src_vram_offset,
+ size_t size)
+{
+ struct xe_sriov_pf_migration_data *data;
+ struct dma_fence *fence;
+ int ret;
+
+ data = xe_sriov_pf_migration_data_alloc(gt_to_xe(gt));
+ if (!data)
+ return -ENOMEM;
+
+ ret = xe_sriov_pf_migration_data_init(data, gt->tile->id, gt->info.id,
+ XE_SRIOV_MIG_DATA_VRAM, src_vram_offset, size);
+ if (ret)
+ goto fail;
+
+ fence = __pf_save_restore_vram(gt, vfid,
+ src_vram, src_vram_offset,
+ data->bo, 0,
+ NULL, 0, size, true);
+
+ ret = dma_fence_wait_timeout(fence, false, 5 * HZ);
+ dma_fence_put(fence);
+ if (!ret) {
+ ret = -ETIME;
+ goto fail;
+ }
+
+ pf_dump_mig_data(gt, vfid, data);
+
+ ret = xe_gt_sriov_pf_migration_ring_produce(gt, vfid, data);
+ if (ret)
+ goto fail;
+
+ return 0;
+
+fail:
+ xe_sriov_pf_migration_data_free(data);
+ return ret;
+}
+
+#define VF_VRAM_STATE_CHUNK_MAX_SIZE SZ_512M
+static int pf_save_vf_vram_mig_data(struct xe_gt *gt, unsigned int vfid)
+{
+ struct xe_bo *vram;
+ loff_t offset = 0;
+ size_t size;
+ int ret;
+
+ vram = xe_gt_sriov_pf_config_get_lmem_obj(gt, vfid);
+ if (!vram)
+ return -ENXIO;
+
+ size = xe_bo_size(vram);
+
+ while (size > 0) {
+ size_t chunk_size = min(size, VF_VRAM_STATE_CHUNK_MAX_SIZE);
+
+ ret = pf_save_vram_chunk(gt, vfid, vram, offset, chunk_size);
+ if (ret)
+ goto fail;
+
+ offset += chunk_size;
+ size -= chunk_size;
+ }
+
+ xe_bo_put(vram);
+
+ return 0;
+
+fail:
+ xe_bo_put(vram);
+ xe_gt_sriov_err(gt, "Unable to save VF%u VRAM data (%d)\n", vfid, ret);
+ return ret;
+}
+
+static int pf_restore_vf_vram_mig_data(struct xe_gt *gt, unsigned int vfid,
+ struct xe_sriov_pf_migration_data *data)
+{
+ u64 end = data->hdr.offset + data->hdr.size;
+ struct dma_fence *fence;
+ struct xe_bo *vram;
+ size_t size;
+ int ret = 0;
+
+ vram = xe_gt_sriov_pf_config_get_lmem_obj(gt, vfid);
+ if (!vram)
+ return -ENXIO;
+
+ size = xe_bo_size(vram);
+
+ if (end > size || end < data->hdr.size) {
+ ret = -EINVAL;
+ goto err;
+ }
+
+ pf_dump_mig_data(gt, vfid, data);
+
+ fence = __pf_save_restore_vram(gt, vfid, vram, data->hdr.offset,
+ data->bo, 0,
+ NULL, 0, data->hdr.size, false);
+ ret = dma_fence_wait_timeout(fence, false, 5 * HZ);
+ dma_fence_put(fence);
+ if (!ret) {
+ ret = -ETIME;
+ goto err;
+ }
+
+ return 0;
+err:
+ xe_bo_put(vram);
+ return ret;
+}
+
+/**
+ * xe_gt_sriov_pf_migration_vram_save() - Save VF VRAM migration data.
+ * @gt: the &struct xe_gt
+ * @vfid: the VF identifier
+ *
+ * This function is for PF only.
+ *
+ * Return: 0 on success or a negative error code on failure.
+ */
+int xe_gt_sriov_pf_migration_vram_save(struct xe_gt *gt, unsigned int vfid)
+{
+ xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
+ xe_gt_assert(gt, vfid != PFID);
+ xe_gt_assert(gt, vfid <= xe_sriov_pf_get_totalvfs(gt_to_xe(gt)));
+
+ return pf_save_vf_vram_mig_data(gt, vfid);
+}
+
+/**
+ * xe_gt_sriov_pf_migration_vram_restore() - Restore VF VRAM migration data.
+ * @gt: the &struct xe_gt
+ * @vfid: the VF identifier
+ *
+ * This function is for PF only.
+ *
+ * Return: 0 on success or a negative error code on failure.
+ */
+int xe_gt_sriov_pf_migration_vram_restore(struct xe_gt *gt, unsigned int vfid,
+ struct xe_sriov_pf_migration_data *data)
+{
+ xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
+ xe_gt_assert(gt, vfid != PFID);
+ xe_gt_assert(gt, vfid <= xe_sriov_pf_get_totalvfs(gt_to_xe(gt)));
+
+ return pf_restore_vf_vram_mig_data(gt, vfid, data);
+}
+
/**
* xe_gt_sriov_pf_migration_size() - Total size of migration data from all components within a GT
* @gt: the &struct xe_gt
@@ -513,6 +734,13 @@ ssize_t xe_gt_sriov_pf_migration_size(struct xe_gt *gt, unsigned int vfid)
size += sizeof(struct xe_sriov_pf_migration_hdr);
total += size;

+ size = xe_gt_sriov_pf_migration_vram_size(gt, vfid);
+ if (size < 0)
+ return size;
+ else if (size > 0)
+ size += sizeof(struct xe_sriov_pf_migration_hdr);
+ total += size;
+
return total;
}

diff --git a/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.h b/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.h
index 66967da761254..c6e6821042619 100644
--- a/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.h
+++ b/drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.h
@@ -24,6 +24,10 @@ ssize_t xe_gt_sriov_pf_migration_mmio_size(struct xe_gt *gt, unsigned int vfid);
int xe_gt_sriov_pf_migration_mmio_save(struct xe_gt *gt, unsigned int vfid);
int xe_gt_sriov_pf_migration_mmio_restore(struct xe_gt *gt, unsigned int vfid,
struct xe_sriov_pf_migration_data *data);
+ssize_t xe_gt_sriov_pf_migration_vram_size(struct xe_gt *gt, unsigned int vfid);
+int xe_gt_sriov_pf_migration_vram_save(struct xe_gt *gt, unsigned int vfid);
+int xe_gt_sriov_pf_migration_vram_restore(struct xe_gt *gt, unsigned int vfid,
+ struct xe_sriov_pf_migration_data *data);

ssize_t xe_gt_sriov_pf_migration_size(struct xe_gt *gt, unsigned int vfid);

--
2.50.1