Skip to contentSkip to conversation

drm/virtgpu: Add PM support for suspend/resume

PATCH In review Morris Pan 3 participants 6 messages Apr 1 – Apr 30

Subsystems (4)
lore
Share & tools
lore

Morris Pan

author
#0 lore raw

PATCH 1/1 drm/virtgpu: Add PM support for suspend/resume

Add freeze/restore callbacks to handle system suspend/resume. During freeze, delete virtqueues and reset device. During restore, reinitialize virtqueues and mark device ready.

---
 drivers/gpu/drm/virtio/virtgpu_drv.c | 40 +++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +39 -1
index a5ce96fb8a1d..fbca0a82958c 100644
@@ -145,6 +145,40 @@ static void virtio_gpu_config_changed(struct virtio_device *vdev)
 	schedule_work(&vgdev->config_changed_work);
 }
 
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{
+	vdev->config->del_vqs(vdev);
+	virtio_reset_device(vdev);
+
+	return 0;
+}
+
+static int virtgpu_restore(struct virtio_device *vdev)
+{
+	int ret;
+	struct virtqueue *vqs[2];
+	struct drm_device *dev = vdev->priv;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	struct virtqueue_info vqs_info[] = {
+		{ "control", virtio_gpu_ctrl_ack },
+		{ "cursor", virtio_gpu_cursor_ack },
+	};
+
+	ret = virtio_find_vqs(vdev, 2, vqs, vqs_info, NULL);
+	if (ret) {
+		DRM_ERROR("failed to find virtio queues\n");
+		return ret;
+	}
+
+	vgdev->ctrlq.vq = vqs[0];
+	vgdev->cursorq.vq = vqs[1];
+	virtio_device_ready(vdev);
+
+	return 0;
+}
+#endif
+
 static struct virtio_device_id id_table[] = {
 	{ VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
 	{ 0 },
@@ -172,7 +206,11 @@ static struct virtio_driver virtio_gpu_driver = {
 	.probe = virtio_gpu_probe,
 	.remove = virtio_gpu_remove,
 	.shutdown = virtio_gpu_shutdown,
-	.config_changed = virtio_gpu_config_changed
+	.config_changed = virtio_gpu_config_changed,
+#ifdef CONFIG_PM_SLEEP
+	.freeze = virtgpu_freeze,
+	.restore = virtgpu_restore
+#endif
 };
 
 static int __init virtio_gpu_driver_init(void)
Signature · 1 line
2.45.2
#1 lore raw

Hi

Am 01.04.26 um 08:10 schrieb Morris Pan:

Quoted 14 lines

Add freeze/restore callbacks to handle system suspend/resume. During freeze, delete virtqueues and reset device. During restore, reinitialize virtqueues and mark device ready.

---
  drivers/gpu/drm/virtio/virtgpu_drv.c | 40 +++++++++++++++++++++++++++-
  1 file changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +0 -0
index a5ce96fb8a1d..fbca0a82958c 100644

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct virtio_device *vdev) · line 150

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +3 -0
@@ -145,6 +145,40 @@ static void virtio_gpu_config_changed(struct virtio_device *vdev)
  	schedule_work(&vgdev->config_changed_work);
  }
 
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{

I think you first need to call drm_mode_config_helper_suspend() [1] to stop DRM and save the current state.

[1] https://elixir.bootlin.com/linux/v6.19.10/source/drivers/gpu/drm/drm_modeset_helper.c#L179

Quoted 16 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +16 -0
+	vdev->config->del_vqs(vdev);
+	virtio_reset_device(vdev);
+
+	return 0;
+}
+
+static int virtgpu_restore(struct virtio_device *vdev)
+{
+	int ret;
+	struct virtqueue *vqs[2];
+	struct drm_device *dev = vdev->priv;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	struct virtqueue_info vqs_info[] = {
+		{ "control", virtio_gpu_ctrl_ack },
+		{ "cursor", virtio_gpu_cursor_ack },
+	};

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct virtio_device *vdev) · line 176

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +10 -0
+
+	ret = virtio_find_vqs(vdev, 2, vqs, vqs_info, NULL);
+	if (ret) {
+		DRM_ERROR("failed to find virtio queues\n");
+		return ret;
+	}
+
+	vgdev->ctrlq.vq = vqs[0];
+	vgdev->cursorq.vq = vqs[1];
+	virtio_device_ready(vdev);

And here you have to call drm_mode_config_helper_resume() [2] to restore the DRM state from before the freeze.

[2] https://elixir.bootlin.com/linux/v6.19.10/source/drivers/gpu/drm/drm_modeset_helper.c#L227

Best regards
Thomas

Quoted 21 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +10 -1
+
+	return 0;
+}
+#endif
+
  static struct virtio_device_id id_table[] = {
  	{ VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
  	{ 0 },
@@ -172,7 +206,11 @@ static struct virtio_driver virtio_gpu_driver = {
  	.probe = virtio_gpu_probe,
  	.remove = virtio_gpu_remove,
  	.shutdown = virtio_gpu_shutdown,
-	.config_changed = virtio_gpu_config_changed
+	.config_changed = virtio_gpu_config_changed,
+#ifdef CONFIG_PM_SLEEP
+	.freeze = virtgpu_freeze,
+	.restore = virtgpu_restore
+#endif
  };
 
  static int __init virtio_gpu_driver_init(void)
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
#2 lore raw
Quoted 97 lines · Thomas Zimmermann

External email : Please do not click links or open attachments until you have verified the sender or the content.

Hi

Am 01.04.26 um 08:10 schrieb Morris Pan:

Quoted 16 lines

Add freeze/restore callbacks to handle system suspend/resume. During freeze, delete virtqueues and reset device. During restore, reinitialize virtqueues and mark device ready.

---
  drivers/gpu/drm/virtio/virtgpu_drv.c | 40
+++++++++++++++++++++++++++-

  1 file changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +0 -0
index a5ce96fb8a1d..fbca0a82958c 100644

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct · line 151

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +3 -0
@@ -145,6 +145,40 @@ static void virtio_gpu_config_changed(struct
 virtio_device *vdev)
       schedule_work(&vgdev->config_changed_work);
   }
 
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{

I think you first need to call drm_mode_config_helper_suspend() [1] to
stop DRM and save the current state.

[1]
https://urldefense.com/v3/__https://elixir.bootlin.com/linux/v6.19.10/source/drivers/gpu/drm/drm_modeset_helper.c*L179__;Iw!!CTRNKA9wMg0ARbw!j0dxkuooYYkTXqgRzLyo558VesWoTL84VgkwhRczVxZ-PZlBeLg0vnY4B36rtYVRDVpGmTqVjGKJoFh7KnVelak$

Quoted 16 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +16 -0
+     vdev->config->del_vqs(vdev);
+     virtio_reset_device(vdev);
+
+     return 0;
+}
+
+static int virtgpu_restore(struct virtio_device *vdev)
+{
+     int ret;
+     struct virtqueue *vqs[2];
+     struct drm_device *dev = vdev->priv;
+     struct virtio_gpu_device *vgdev = dev->dev_private;
+     struct virtqueue_info vqs_info[] = {
+             { "control", virtio_gpu_ctrl_ack },
+             { "cursor", virtio_gpu_cursor_ack },
+     };

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct virtio_device *vdev) · line 176

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +10 -0
+
+     ret = virtio_find_vqs(vdev, 2, vqs, vqs_info, NULL);
+     if (ret) {
+             DRM_ERROR("failed to find virtio queues\n");
+             return ret;
+     }
+
+     vgdev->ctrlq.vq = vqs[0];
+     vgdev->cursorq.vq = vqs[1];
+     virtio_device_ready(vdev);

And here you have to call drm_mode_config_helper_resume() [2] to restore
the DRM state from before the freeze.

[2]
https://urldefense.com/v3/__https://elixir.bootlin.com/linux/v6.19.10/source/drivers/gpu/drm/drm_modeset_helper.c*L227__;Iw!!CTRNKA9wMg0ARbw!j0dxkuooYYkTXqgRzLyo558VesWoTL84VgkwhRczVxZ-PZlBeLg0vnY4B36rtYVRDVpGmTqVjGKJoFh7cv1EDgQ$

Best regards
Thomas

Quoted 21 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +5 -0
+
+     return 0;
+}
+#endif
+
   static struct virtio_device_id id_table[] = {
       { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },

      { 0 },

drivers/gpu/drm/virtio/virtgpu_drv.c +5 -1
@@ -172,7 +206,11 @@ static struct virtio_driver virtio_gpu_driver
 = {
       .probe = virtio_gpu_probe,
       .remove = virtio_gpu_remove,
       .shutdown = virtio_gpu_shutdown,
-     .config_changed = virtio_gpu_config_changed
+     .config_changed = virtio_gpu_config_changed,
+#ifdef CONFIG_PM_SLEEP
+     .freeze = virtgpu_freeze,
+     .restore = virtgpu_restore
+#endif
   };
 
Quoted 13 lines
Quoted 1 line

  static int __init virtio_gpu_driver_init(void)

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany,
https://urldefense.com/v3/__http://www.suse.com__;!!CTRNKA9wMg0ARbw!j0dxkuooYYkTXqgRzLyo558VesWoTL84VgkwhRczVxZ-PZlBeLg0vnY4B36rtYVRDVpGmTqVjGKJoFh74dj3x1M$
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)

Hi Thomas,

Thank you very much for your detailed review and the constructive feedback on my patch.

I am currently working on the revisions based on your comments.

Thanks again for your time and guidance.

Best regards,
Morris

maintainer
#3 lore raw
In reply to Morris Pan

Hi,

Quoted 6 lines · Morris Pan
drivers/gpu/drm/virtio/virtgpu_drv.c +0 -0
index a5ce96fb8a1d..fbca0a82958c 100644
@@ -145,6 +145,40 @@ static void virtio_gpu_config_changed(struct virtio_device *vdev)
 	schedule_work(&vgdev->config_changed_work);

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct virtio_device *vdev) · line 155

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +8 -0
 }
 
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{
+	vdev->config->del_vqs(vdev);
+	virtio_reset_device(vdev);
+
+	return 0;
+}

VirtIO-GPU shouldn't be reset freely. See [1] and follow previous hibernation-support discussions on the ML.

[1[
https://lore.kernel.org/dri-devel/[email protected]/

Signature · 2 lines
Best regards,
Dmitry
#4 lore raw
Quoted 25 lines · Dmitry Osipenko

Hi,

Quoted 10 lines · Morris Pan
drivers/gpu/drm/virtio/virtgpu_drv.c +0 -0
index a5ce96fb8a1d..fbca0a82958c 100644
@@ -145,6 +145,40 @@ static void virtio_gpu_config_changed(struct
 virtio_device *vdev)
  	schedule_work(&vgdev->config_changed_work);
  }

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct virtio_device *vdev) · line 155

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +8 -0
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{
+	vdev->config->del_vqs(vdev);
+	virtio_reset_device(vdev);
+
+	return 0;
+}

VirtIO-GPU shouldn't be reset freely. See [1] and follow previous hibernation-support discussions on the ML.

Quoted 4 lines

[1[
https://lore.kernel.org/dri-devel/[email protected]/

Hi Dmitry,

Thank you for the guidance and for pointing me to that thread.

I have looked into the series you mentioned, specifically "PATCH v7 1/3: drm/virtio: Freeze and restore hooks to support suspend and resume" (link).
I have verified that this change resolves the issue I was experiencing in my envirronment.

I will keep an eye on that series and follow its progress. Thank you for your time and help.

Best regards,

Morris Pan

maintainer
#5 lore raw
Quoted 35 lines · Morris Pan (潘泰為)
Quoted 25 lines · Dmitry Osipenko

Hi,

Quoted 10 lines · Morris Pan
drivers/gpu/drm/virtio/virtgpu_drv.c +0 -0
index a5ce96fb8a1d..fbca0a82958c 100644
@@ -145,6 +145,40 @@ static void virtio_gpu_config_changed(struct
 virtio_device *vdev)
  	schedule_work(&vgdev->config_changed_work);
  }

drivers/gpu/drm/virtio/virtgpu_drv.c static void virtio_gpu_config_changed(struct virtio_device *vdev) · line 155

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_drv.c +8 -0
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{
+	vdev->config->del_vqs(vdev);
+	virtio_reset_device(vdev);
+
+	return 0;
+}

VirtIO-GPU shouldn't be reset freely. See [1] and follow previous hibernation-support discussions on the ML.

Quoted 4 lines

[1[
https://lore.kernel.org/dri-devel/[email protected]/

Hi Dmitry,

Thank you for the guidance and for pointing me to that thread.

Quoted 8 lines

I have looked into the series you mentioned, specifically "PATCH v7 1/3: drm/virtio: Freeze and restore hooks to support suspend and resume" (link).
I have verified that this change resolves the issue I was experiencing in my envirronment.

I will keep an eye on that series and follow its progress. Thank you for your time and help.

You're welcome. There is now v8 sent by Kim [2], feel free to test it and reply with your review and/or tested-by on the patches.

[2]
https://lore.kernel.org/dri-devel/[email protected]/

Signature · 2 lines
Best regards,
Dmitry