mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-23 15:26:50 -04:00
drm/virtio: Add PM notifier to restore objects after hibernation
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. Suggested-by: Dmitry Osipenko <dmitry.osipenko@collabora.com> Cc: Vivek Kasireddy <vivek.kasireddy@intel.com> Signed-off-by: Dongwon Kim <dongwon.kim@intel.com> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com> Link: https://patch.msgid.link/20260526192814.179673-4-dongwon.kim@intel.com [dmitry.osipenko@collabora.com: rename vgdev->hibernation -> hibernated] [dmitry.osipenko@collabora.com: restore objs on suspend error] [dmitry.osipenko@collabora.com: no_cb int -> bool] [dmitry.osipenko@collabora.com: don't include pm_runtime.h] [dmitry.osipenko@collabora.com: proeprly handle failing register_pm_notifier]
This commit is contained in:
committed by
Dmitry Osipenko
parent
54a9700482
commit
78e434bdc0
@@ -167,6 +167,14 @@ static unsigned int features[] = {
|
||||
};
|
||||
|
||||
#ifdef CONFIG_PM_SLEEP
|
||||
static void virtgpu_hibernation_restore(struct virtio_gpu_device *vgdev)
|
||||
{
|
||||
if (vgdev->hibernated) {
|
||||
vgdev->hibernated = false;
|
||||
virtio_gpu_object_restore_all(vgdev);
|
||||
}
|
||||
}
|
||||
|
||||
static int virtgpu_freeze(struct virtio_device *vdev)
|
||||
{
|
||||
struct drm_device *dev = vdev->priv;
|
||||
@@ -179,6 +187,9 @@ static int virtgpu_freeze(struct virtio_device *vdev)
|
||||
return error;
|
||||
}
|
||||
|
||||
if (vgdev->hibernated)
|
||||
virtio_gpu_object_unref_all(vgdev);
|
||||
|
||||
flush_work(&vgdev->obj_free_work);
|
||||
flush_work(&vgdev->ctrlq.dequeue_work);
|
||||
flush_work(&vgdev->cursorq.dequeue_work);
|
||||
@@ -201,6 +212,8 @@ static int virtgpu_freeze(struct virtio_device *vdev)
|
||||
return 0;
|
||||
|
||||
err_resume:
|
||||
virtgpu_hibernation_restore(vgdev);
|
||||
|
||||
drm_mode_config_helper_resume(dev);
|
||||
|
||||
return error;
|
||||
@@ -220,6 +233,8 @@ static int virtgpu_restore(struct virtio_device *vdev)
|
||||
|
||||
virtio_device_ready(vdev);
|
||||
|
||||
virtgpu_hibernation_restore(vgdev);
|
||||
|
||||
error = drm_mode_config_helper_resume(dev);
|
||||
if (error) {
|
||||
DRM_ERROR("resume error: %d\n", error);
|
||||
|
||||
@@ -263,6 +263,7 @@ struct virtio_gpu_device {
|
||||
bool has_host_visible;
|
||||
bool has_context_init;
|
||||
bool has_blob_alignment;
|
||||
bool hibernated;
|
||||
struct virtio_shm_region host_visible_region;
|
||||
struct drm_mm host_visible_mm;
|
||||
|
||||
@@ -280,6 +281,8 @@ struct virtio_gpu_device {
|
||||
struct list_head cap_cache;
|
||||
uint32_t blob_alignment;
|
||||
|
||||
struct notifier_block pm_nb;
|
||||
|
||||
/* protects uuid state when exporting */
|
||||
spinlock_t resource_export_lock;
|
||||
/* protects map state and host_visible_mm */
|
||||
@@ -345,7 +348,8 @@ void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
|
||||
struct virtio_gpu_object_array *objs,
|
||||
struct virtio_gpu_fence *fence);
|
||||
void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
|
||||
struct virtio_gpu_object *bo);
|
||||
struct virtio_gpu_object *bo,
|
||||
bool no_cb);
|
||||
int virtio_gpu_panic_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
|
||||
uint64_t offset,
|
||||
uint32_t width, uint32_t height,
|
||||
@@ -497,6 +501,8 @@ void virtio_gpu_add_object_to_restore_list(struct virtio_gpu_device *vgdev,
|
||||
|
||||
int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev);
|
||||
|
||||
void virtio_gpu_object_unref_all(struct virtio_gpu_device *vgdev);
|
||||
|
||||
/* virtgpu_prime.c */
|
||||
int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev,
|
||||
struct virtio_gpu_object *bo);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <linux/suspend.h>
|
||||
#include <linux/virtio.h>
|
||||
#include <linux/virtio_config.h>
|
||||
#include <linux/virtio_ring.h>
|
||||
@@ -134,6 +135,31 @@ 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);
|
||||
|
||||
switch (mode) {
|
||||
case PM_HIBERNATION_PREPARE:
|
||||
if (vgdev->has_virgl_3d) {
|
||||
DRM_ERROR("S4 not allowed when VIRGL is enabled\n");
|
||||
return notifier_from_errno(-EPERM);
|
||||
}
|
||||
|
||||
vgdev->hibernated = true;
|
||||
break;
|
||||
|
||||
case PM_POST_HIBERNATION:
|
||||
vgdev->hibernated = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return NOTIFY_DONE;
|
||||
}
|
||||
|
||||
int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
|
||||
{
|
||||
struct virtio_gpu_device *vgdev;
|
||||
@@ -278,8 +304,16 @@ 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_modeset_fini;
|
||||
|
||||
return 0;
|
||||
|
||||
err_modeset_fini:
|
||||
virtio_gpu_modeset_fini(vgdev);
|
||||
err_scanouts:
|
||||
virtio_gpu_free_vbufs(vgdev);
|
||||
err_vbufs:
|
||||
@@ -303,6 +337,7 @@ void virtio_gpu_deinit(struct drm_device *dev)
|
||||
{
|
||||
struct virtio_gpu_device *vgdev = dev->dev_private;
|
||||
|
||||
unregister_pm_notifier(&vgdev->pm_nb);
|
||||
flush_work(&vgdev->obj_free_work);
|
||||
flush_work(&vgdev->ctrlq.dequeue_work);
|
||||
flush_work(&vgdev->cursorq.dequeue_work);
|
||||
|
||||
@@ -104,7 +104,7 @@ static void virtio_gpu_free_object(struct drm_gem_object *obj)
|
||||
|
||||
if (bo->created) {
|
||||
virtio_gpu_remove_from_restore_list(bo);
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo);
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo, false);
|
||||
virtio_gpu_notify(vgdev);
|
||||
/* completion handler calls virtio_gpu_cleanup_object() */
|
||||
return;
|
||||
@@ -344,3 +344,18 @@ int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void virtio_gpu_object_unref_all(struct virtio_gpu_device *vgdev)
|
||||
{
|
||||
struct virtio_gpu_object *bo, *tmp;
|
||||
|
||||
mutex_lock(&vgdev->obj_restore_lock);
|
||||
list_for_each_entry_safe(bo, tmp, &vgdev->obj_restore_list,
|
||||
restore_node)
|
||||
if (bo->created) {
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo, true);
|
||||
virtio_gpu_notify(vgdev);
|
||||
}
|
||||
|
||||
mutex_unlock(&vgdev->obj_restore_lock);
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
|
||||
|
||||
if (bo->created) {
|
||||
virtio_gpu_remove_from_restore_list(bo);
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo);
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo, false);
|
||||
virtio_gpu_notify(vgdev);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -640,14 +640,21 @@ static void virtio_gpu_cmd_unref_cb(struct virtio_gpu_device *vgdev,
|
||||
}
|
||||
|
||||
void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
|
||||
struct virtio_gpu_object *bo)
|
||||
struct virtio_gpu_object *bo,
|
||||
bool no_cb)
|
||||
{
|
||||
struct virtio_gpu_resource_unref *cmd_p;
|
||||
struct virtio_gpu_vbuffer *vbuf;
|
||||
int ret;
|
||||
|
||||
cmd_p = virtio_gpu_alloc_cmd_cb(vgdev, &vbuf, sizeof(*cmd_p),
|
||||
virtio_gpu_cmd_unref_cb);
|
||||
if (no_cb) {
|
||||
cmd_p = virtio_gpu_alloc_cmd_cb(vgdev, &vbuf, sizeof(*cmd_p),
|
||||
NULL);
|
||||
} else {
|
||||
cmd_p = virtio_gpu_alloc_cmd_cb(vgdev, &vbuf, sizeof(*cmd_p),
|
||||
virtio_gpu_cmd_unref_cb);
|
||||
}
|
||||
|
||||
memset(cmd_p, 0, sizeof(*cmd_p));
|
||||
|
||||
cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_UNREF);
|
||||
|
||||
@@ -21,7 +21,7 @@ static void virtio_gpu_vram_free(struct drm_gem_object *obj)
|
||||
virtio_gpu_cmd_unmap(vgdev, bo);
|
||||
|
||||
virtio_gpu_remove_from_restore_list(bo);
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo);
|
||||
virtio_gpu_cmd_unref_resource(vgdev, bo, false);
|
||||
virtio_gpu_notify(vgdev);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user