Register a 4 GiB shared memory PCI BAR (BAR 2) so the guest driver
can use it as a DMA cache for video buffers. The memory region is
allocated on device realize and exposed to the guest through a VirtIO
shared memory capability (shmem ID 0).
The host must be started with a memfd-backed memory object covering
at least the shared region size, e.g.:
-object memory-backend-memfd,id=mem,size=4G,share=on \
-numa node,memdev=mem
Signed-off-by: Albert Esteve <[email protected]>
---
hw/display/vhost-user-media-pci.c | 28 ++++++++++++++++++++++++++++
hw/display/vhost-user-media.c | 4 ++++
2 files changed, 32 insertions(+)
diff --git a/hw/display/vhost-user-media-pci.c
b/hw/display/vhost-user-media-pci.c
index ad9e3900a4..838a9da71d 100644
--- a/hw/display/vhost-user-media-pci.c
+++ b/hw/display/vhost-user-media-pci.c
@@ -12,12 +12,18 @@
#include "hw/virtio/vhost-user-media.h"
#include "hw/virtio/virtio-pci.h"
+/* BAR 2 is used for the shared memory cache region exposed to the guest */
+#define VIRTIO_MEDIA_PCI_CACHE_BAR 2
+
+#define VIRTIO_MEDIA_PCI_SHMCAP_ID_CACHE 0
+
#define TYPE_VHOST_USER_MEDIA_PCI "vhost-user-media-pci-base"
OBJECT_DECLARE_SIMPLE_TYPE(VHostUserMEDIAPCI, VHOST_USER_MEDIA_PCI)
struct VHostUserMEDIAPCI {
VirtIOPCIProxy parent_obj;
VHostUserMEDIA vdev;
+ MemoryRegion cachebar;
};
static const Property vumedia_pci_properties[] = {
@@ -31,12 +37,34 @@ static void vumedia_pci_realize(VirtIOPCIProxy *vpci_dev,
Error **errp)
{
VHostUserMEDIAPCI *dev = VHOST_USER_MEDIA_PCI(vpci_dev);
DeviceState *dev_state = DEVICE(&dev->vdev);
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev_state);
+ VirtioSharedMemory *shmem;
+ uint64_t cache_size;
if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
vpci_dev->nvectors = 1;
}
qdev_realize(dev_state, BUS(&vpci_dev->bus), errp);
+ if (*errp) {
+ return;
+ }
+
+ shmem = QSIMPLEQ_LAST(&vdev->shmem_list, VirtioSharedMemory, entry);
+ cache_size = memory_region_size(&shmem->mr);
+
+ memory_region_init(&dev->cachebar, OBJECT(vpci_dev),
+ "vhost-media-pci-cachebar", cache_size);
+ memory_region_add_subregion(&dev->cachebar, 0, &shmem->mr);
+ virtio_pci_add_shm_cap(vpci_dev, VIRTIO_MEDIA_PCI_CACHE_BAR, 0,
+ cache_size, VIRTIO_MEDIA_PCI_SHMCAP_ID_CACHE);
+
+ /* After 'realized' so the memory region exists */
+ pci_register_bar(&vpci_dev->pci_dev, VIRTIO_MEDIA_PCI_CACHE_BAR,
+ PCI_BASE_ADDRESS_SPACE_MEMORY |
+ PCI_BASE_ADDRESS_MEM_PREFETCH |
+ PCI_BASE_ADDRESS_MEM_TYPE_64,
+ &dev->cachebar);
}
static void vumedia_pci_class_init(ObjectClass *klass, const void *data)
diff --git a/hw/display/vhost-user-media.c b/hw/display/vhost-user-media.c
index 5c627437e7..bbb64ea4d3 100644
--- a/hw/display/vhost-user-media.c
+++ b/hw/display/vhost-user-media.c
@@ -20,6 +20,8 @@
#include "hw/virtio/virtio-bus.h"
#include "hw/virtio/vhost-user-media.h"
+#define CACHE_SIZE (1ull << 32)
+
static const int feature_bits[] = {
VIRTIO_F_RING_RESET,
VHOST_INVALID_FEATURE_BIT
@@ -288,6 +290,8 @@ static void vhost_user_media_device_realize(DeviceState
*dev, Error **errp)
virtio_init(vdev, VIRTIO_ID_MEDIA, sizeof(struct virtio_media_config));
+ virtio_new_shmem_region(vdev, 0, CACHE_SIZE);
+
/* one command queue and one event queue */
media->vhost_dev.nvqs = 2;
media->vhost_dev.vqs = g_new0(struct vhost_virtqueue,
--
2.54.0