[QEMU PATCH v5 1/2] virtio-pci: Add freeze_mode case for virtio pci

From: Jiqian Chen
Date: Tue Sep 19 2023 - 07:03:10 EST


When guest vm do S3, Qemu will reset and clear some things of virtio
devices, but guest can't aware that, so that may cause some problems.
For excample, Qemu calls virtio_reset->virtio_gpu_gl_reset, that will
destroy render resources of virtio-gpu. As a result, after guest resume,
the display can't come back and we only saw a black screen. Due to guest
can't re-create all the resources, so we need to let Qemu not to destroy
them when S3.

For above purpose, this patch add a new parameter named freeze_mode to
struct VirtIODevice, and when guest suspends, guest can write freeze_mode
to be FREEZE_S3, so that virtio devices can change their reset behavior
on Qemu side according to that mode.

Signed-off-by: Jiqian Chen <Jiqian.Chen@xxxxxxx>
---
hw/virtio/virtio-pci.c | 5 +++++
hw/virtio/virtio.c | 1 +
include/hw/virtio/virtio.h | 2 ++
3 files changed, 8 insertions(+)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index edbc0daa18..7a3cca79b4 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -1651,6 +1651,11 @@ static void virtio_pci_common_write(void *opaque, hwaddr addr,
proxy->vqs[vdev->queue_sel].enabled = 0;
}
break;
+ case VIRTIO_PCI_COMMON_F_MODE:
+ virtio_pci_freeze_mode_t freeze_mode = (virtio_pci_freeze_mode_t)val;
+ if ((1 << freeze_mode) & VIRTIO_PCI_FREEZE_MODE_MASK)
+ vdev->freeze_mode = freeze_mode;
+ break;
default:
break;
}
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 969c25f4cf..4278cedef4 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -3201,6 +3201,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size)
vdev->vhost_started = false;
vdev->device_id = device_id;
vdev->status = 0;
+ vdev->freeze_mode = VIRTIO_PCI_FREEZE_MODE_UNFREEZE;
qatomic_set(&vdev->isr, 0);
vdev->queue_sel = 0;
vdev->config_vector = VIRTIO_NO_VECTOR;
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index c8f72850bc..f8bfd9da28 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -21,6 +21,7 @@
#include "qemu/event_notifier.h"
#include "standard-headers/linux/virtio_config.h"
#include "standard-headers/linux/virtio_ring.h"
+#include "standard-headers/linux/virtio_pci.h"
#include "qom/object.h"

/*
@@ -106,6 +107,7 @@ struct VirtIODevice
DeviceState parent_obj;
const char *name;
uint8_t status;
+ virtio_pci_freeze_mode_t freeze_mode;
uint8_t isr;
uint16_t queue_sel;
/**
--
2.34.1