From: Dongwon Kim <[email protected]>

Register a PM notifier in virtio-gpu to handle suspend/hibernate
events. On PM_POST_HIBERNATION, restore all GPU objects so that the
driver can properly recover after resume.

v2: Remove unused header - drm_atomic_helper.h
    (Dmitry Osipenko)

v3: Objects for virgl usecase can't be recovered after resume so
    blocking S4 when virgl is enabled
    (Dmitry Osipenko)

v4: Restoring objects in the PM notifier is too late, as virtio-gpu
    message communication begins in virtgpu_restore once virtqueues
    are re-established. To address this, a 'hibernation' flag is set
    during the PM_HIBERNATION_PREPARE phase in the notifier. This flag
    is then used in virtgpu_restore to detect if the system is resuming
    from S4, allowing objects to be recovered immediately after virtqueues
    are reconfigured.

Suggested-by: Dmitry Osipenko <[email protected]>
Cc: Vivek Kasireddy <[email protected]>
Signed-off-by: Dongwon Kim <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.c |  9 +++++++++
 drivers/gpu/drm/virtio/virtgpu_drv.h |  3 +++
 drivers/gpu/drm/virtio/virtgpu_kms.c | 27 +++++++++++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.c 
b/drivers/gpu/drm/virtio/virtgpu_drv.c
index 676893e90a9f..5ff79e3775e8 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.c
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.c
@@ -207,6 +207,15 @@ static int virtgpu_restore(struct virtio_device *vdev)
 
        virtio_device_ready(vdev);
 
+       if (vgdev->hibernation) {
+               vgdev->hibernation = false;
+               error = virtio_gpu_object_restore_all(vgdev);
+               if (error) {
+                       DRM_ERROR("Failed to recover virtio-gpu objects\n");
+                       return error;
+               }
+       }
+
        error = drm_mode_config_helper_resume(dev);
        if (error) {
                DRM_ERROR("resume error %d\n", error);
diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h 
b/drivers/gpu/drm/virtio/virtgpu_drv.h
index db1d4aa6eae8..4eae6e4b4a73 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.h
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
@@ -261,6 +261,7 @@ struct virtio_gpu_device {
        bool has_resource_blob;
        bool has_host_visible;
        bool has_context_init;
+       bool hibernation;
        struct virtio_shm_region host_visible_region;
        struct drm_mm host_visible_mm;
 
@@ -277,6 +278,8 @@ struct virtio_gpu_device {
        uint64_t capset_id_mask;
        struct list_head cap_cache;
 
+       struct notifier_block pm_nb;
+
        /* protects uuid state when exporting */
        spinlock_t resource_export_lock;
        /* protects map state and host_visible_mm */
diff --git a/drivers/gpu/drm/virtio/virtgpu_kms.c 
b/drivers/gpu/drm/virtio/virtgpu_kms.c
index 8ad79de70d85..6cb1ae8de16a 100644
--- a/drivers/gpu/drm/virtio/virtgpu_kms.c
+++ b/drivers/gpu/drm/virtio/virtgpu_kms.c
@@ -26,6 +26,8 @@
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
+#include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
@@ -133,6 +135,24 @@ int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev)
        return 0;
 }
 
+static int virtio_gpu_pm_notifier(struct notifier_block *nb, unsigned long 
mode,
+                                 void *data)
+{
+       struct virtio_gpu_device *vgdev = container_of(nb,
+                                               struct virtio_gpu_device,
+                                               pm_nb);
+
+       if (mode == PM_HIBERNATION_PREPARE) {
+               if (vgdev->has_virgl_3d) {
+                       DRM_ERROR("S4 not allowed when VIRGL is enabled\n");
+                       return notifier_from_errno(-EPERM);
+               }
+               vgdev->hibernation = true;
+       }
+
+       return NOTIFY_DONE;
+}
+
 int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 {
        struct virtio_gpu_device *vgdev;
@@ -269,6 +289,12 @@ int virtio_gpu_init(struct virtio_device *vdev, struct 
drm_device *dev)
                wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
                                   5 * HZ);
        }
+
+       vgdev->pm_nb.notifier_call = virtio_gpu_pm_notifier;
+       ret = register_pm_notifier(&vgdev->pm_nb);
+       if (ret)
+               goto err_scanouts;
+
        return 0;
 
 err_scanouts:
@@ -301,6 +327,7 @@ void virtio_gpu_deinit(struct drm_device *dev)
        virtio_reset_device(vgdev->vdev);
        vgdev->vdev->config->del_vqs(vgdev->vdev);
        mutex_destroy(&vgdev->obj_restore_lock);
+       unregister_pm_notifier(&vgdev->pm_nb);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
-- 
2.34.1

Reply via email to