drm/xe: evict user memory in PM notifier
authorMatthew Auld <matthew.auld@intel.com>
Wed, 16 Apr 2025 15:09:15 +0000 (16:09 +0100)
committerMatthew Auld <matthew.auld@intel.com>
Wed, 23 Apr 2025 08:32:16 +0000 (09:32 +0100)
In the case of VRAM we might need to allocate large amounts of
GFP_KERNEL memory on suspend, however doing that directly in the driver
.suspend()/.prepare() callback is not advisable (no swap for example).

To improve on this we can instead hook up to the PM notifier framework
which is invoked at an earlier stage. We effectively call the evict
routine twice, where the notifier will have hopefully have cleared out
most if not everything by the time we call it a second time when
entering the .suspend() callback. For s4 we also get the added benefit
of allocating the system pages before the hibernation image size is
calculated, which looks more sensible.

Note that the .suspend() hook is still responsible for dealing with all
the pinned memory. Improving that is left to another patch.

Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/1181
Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/4288
Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/4566
Suggested-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Signed-off-by: Matthew Auld <matthew.auld@intel.com>
Reviewed-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Link: https://lore.kernel.org/r/20250416150913.434369-6-matthew.auld@intel.com
drivers/gpu/drm/xe/xe_bo_evict.c
drivers/gpu/drm/xe/xe_bo_evict.h
drivers/gpu/drm/xe/xe_device_types.h
drivers/gpu/drm/xe/xe_pci.c
drivers/gpu/drm/xe/xe_pm.c
drivers/gpu/drm/xe/xe_pm.h

index 2bf74eb7f2817d0e280e969cc75a1e9721ae59f3..748360fd2439c930c4ae612aab4ed853ebab2b33 100644 (file)
@@ -47,25 +47,17 @@ static int xe_bo_apply_to_pinned(struct xe_device *xe,
 }
 
 /**
- * xe_bo_evict_all - evict all BOs from VRAM
- *
+ * xe_bo_evict_all_user - evict all non-pinned user BOs from VRAM
  * @xe: xe device
  *
- * Evict non-pinned user BOs first (via GPU), evict pinned external BOs next
- * (via GPU), wait for evictions, and finally evict pinned kernel BOs via CPU.
- * All eviction magic done via TTM calls.
+ * Evict non-pinned user BOs (via GPU).
  *
  * Evict == move VRAM BOs to temporary (typically system) memory.
- *
- * This function should be called before the device goes into a suspend state
- * where the VRAM loses power.
  */
-int xe_bo_evict_all(struct xe_device *xe)
+int xe_bo_evict_all_user(struct xe_device *xe)
 {
        struct ttm_device *bdev = &xe->ttm;
-       struct xe_tile *tile;
        u32 mem_type;
-       u8 id;
        int ret;
 
        /* User memory */
@@ -91,9 +83,34 @@ int xe_bo_evict_all(struct xe_device *xe)
                }
        }
 
-       ret = xe_bo_apply_to_pinned(xe, &xe->pinned.late.external,
-                                   &xe->pinned.late.external,
-                                   xe_bo_evict_pinned);
+       return 0;
+}
+
+/**
+ * xe_bo_evict_all - evict all BOs from VRAM
+ * @xe: xe device
+ *
+ * Evict non-pinned user BOs first (via GPU), evict pinned external BOs next
+ * (via GPU), wait for evictions, and finally evict pinned kernel BOs via CPU.
+ * All eviction magic done via TTM calls.
+ *
+ * Evict == move VRAM BOs to temporary (typically system) memory.
+ *
+ * This function should be called before the device goes into a suspend state
+ * where the VRAM loses power.
+ */
+int xe_bo_evict_all(struct xe_device *xe)
+{
+       struct xe_tile *tile;
+       u8 id;
+       int ret;
+
+       ret = xe_bo_evict_all_user(xe);
+       if (ret)
+               return ret;
+
+       ret = xe_bo_apply_to_pinned(xe, &xe->pinned.late.kernel_bo_present,
+                                   &xe->pinned.late.evicted, xe_bo_evict_pinned);
 
        if (!ret)
                ret = xe_bo_apply_to_pinned(xe, &xe->pinned.late.kernel_bo_present,
index d63eb3fc5cc914ca75d19cd719fa028d2d6e8ea2..e7f048634b3239028d22459a9c9a2976a556a0a2 100644 (file)
@@ -9,6 +9,7 @@
 struct xe_device;
 
 int xe_bo_evict_all(struct xe_device *xe);
+int xe_bo_evict_all_user(struct xe_device *xe);
 int xe_bo_restore_early(struct xe_device *xe);
 int xe_bo_restore_late(struct xe_device *xe);
 
index a42cb26e7d6def294ecb8f78e793a09e32f8e562..3745389ead0d536eb885ee888408f8664de3030c 100644 (file)
@@ -522,6 +522,9 @@ struct xe_device {
                struct mutex lock;
        } d3cold;
 
+       /** @pm_notifier: Our PM notifier to perform actions in response to various PM events. */
+       struct notifier_block pm_notifier;
+
        /** @pmt: Support the PMT driver callback interface */
        struct {
                /** @pmt.lock: protect access for telemetry data */
index 07fe994f2a807da6bdc234cfc063772e5c037090..882398e09b7e3fcdf247e220cb3fec3279e385f6 100644 (file)
@@ -742,7 +742,7 @@ static void xe_pci_remove(struct pci_dev *pdev)
                return;
 
        xe_device_remove(xe);
-       xe_pm_runtime_fini(xe);
+       xe_pm_fini(xe);
 }
 
 /*
index 4e112fbacada45ec588656aa59a0b8cc35827bcd..d8a411d3ee9664c0af410a49ba3c82923a91c9d8 100644 (file)
@@ -286,6 +286,29 @@ static u32 vram_threshold_value(struct xe_device *xe)
        return DEFAULT_VRAM_THRESHOLD;
 }
 
+static int xe_pm_notifier_callback(struct notifier_block *nb,
+                                  unsigned long action, void *data)
+{
+       struct xe_device *xe = container_of(nb, struct xe_device, pm_notifier);
+       int err = 0;
+
+       switch (action) {
+       case PM_HIBERNATION_PREPARE:
+       case PM_SUSPEND_PREPARE:
+               xe_pm_runtime_get(xe);
+               err = xe_bo_evict_all_user(xe);
+               xe_pm_runtime_put(xe);
+               if (err)
+                       drm_dbg(&xe->drm, "Notifier evict user failed (%d)\n", err);
+               break;
+       }
+
+       if (err)
+               return NOTIFY_BAD;
+
+       return NOTIFY_DONE;
+}
+
 /**
  * xe_pm_init - Initialize Xe Power Management
  * @xe: xe device instance
@@ -299,6 +322,11 @@ int xe_pm_init(struct xe_device *xe)
        u32 vram_threshold;
        int err;
 
+       xe->pm_notifier.notifier_call = xe_pm_notifier_callback;
+       err = register_pm_notifier(&xe->pm_notifier);
+       if (err)
+               return err;
+
        /* For now suspend/resume is only allowed with GuC */
        if (!xe_device_uc_enabled(xe))
                return 0;
@@ -308,24 +336,23 @@ int xe_pm_init(struct xe_device *xe)
        if (xe->d3cold.capable) {
                err = xe_device_sysfs_init(xe);
                if (err)
-                       return err;
+                       goto err_unregister;
 
                vram_threshold = vram_threshold_value(xe);
                err = xe_pm_set_vram_threshold(xe, vram_threshold);
                if (err)
-                       return err;
+                       goto err_unregister;
        }
 
        xe_pm_runtime_init(xe);
-
        return 0;
+
+err_unregister:
+       unregister_pm_notifier(&xe->pm_notifier);
+       return err;
 }
 
-/**
- * xe_pm_runtime_fini - Finalize Runtime PM
- * @xe: xe device instance
- */
-void xe_pm_runtime_fini(struct xe_device *xe)
+static void xe_pm_runtime_fini(struct xe_device *xe)
 {
        struct device *dev = xe->drm.dev;
 
@@ -333,6 +360,18 @@ void xe_pm_runtime_fini(struct xe_device *xe)
        pm_runtime_forbid(dev);
 }
 
+/**
+ * xe_pm_fini - Finalize PM
+ * @xe: xe device instance
+ */
+void xe_pm_fini(struct xe_device *xe)
+{
+       if (xe_device_uc_enabled(xe))
+               xe_pm_runtime_fini(xe);
+
+       unregister_pm_notifier(&xe->pm_notifier);
+}
+
 static void xe_pm_write_callback_task(struct xe_device *xe,
                                      struct task_struct *task)
 {
index 998d1ed645560a67524c77890f50bd23e192f66e..59678b310e55f1a6b30f16ec09f5f23e1d30d0f5 100644 (file)
@@ -17,7 +17,7 @@ int xe_pm_resume(struct xe_device *xe);
 
 int xe_pm_init_early(struct xe_device *xe);
 int xe_pm_init(struct xe_device *xe);
-void xe_pm_runtime_fini(struct xe_device *xe);
+void xe_pm_fini(struct xe_device *xe);
 bool xe_pm_runtime_suspended(struct xe_device *xe);
 int xe_pm_runtime_suspend(struct xe_device *xe);
 int xe_pm_runtime_resume(struct xe_device *xe);