hw/display: add vhost-user-media device
v2 cover hw/display: add vhost-user-media device
This series introduces the QEMU frontend for the virtio-media device, a VirtIO device that exposes V4L2 video device functionality to the guest over the virtio-media protocol, as specified in the VirtIO specification v1.4, section 5.22 [1].
Overview
--------
Patch 1 adds VIRTIO_ID_MEDIA definition from Linux v7.3-rc1.
Patch 2 introduces the vhost-user-media device frontend as defined by the specification.
Patch 3 adds a shared memory PCI BAR (BAR 2) sized by GET_SHMEM_CONFIG and used by the guest driver as a MMAP mapping window for video buffer exchange with the host backend. The region is exposed to the guest through a VirtIO shared memory capability.
Testing
-------
The series was tested with:
Guest driver: the virtio-media out-of-tree Linux kernel driver [2]. An upstream series for this driver has also been posted [3].
Host backend: the rust-vmm vhost-device-media backend [4]:
cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxy
Full QEMU invocation:
qemu-system-x86_64 \
-chardev socket,path=/path/to/media.sock,id=media \
-device vhost-user-media-pci,chardev=media,id=media \
-object memory-backend-memfd,id=mem,size=4G,share=on \
-numa node,memdev=mem[1] https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.html
[2] https://github.com/chromeos/virtio-media/tree/main/driver
[3] https://lore.kernel.org/all/[email protected]/
[4] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-media
--
Changes in v2:
- virtio_ids.h header updated from kernel sources
- Updated copyright year to current
- Added MAINTAINERS entry for vhost-user-media
- Added GET_SHMEM_CONFIG request for media shm region
- Few other changes to align media with other backends
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]/
---
Albert Esteve (3):
linux-headers: add VIRTIO_ID_MEDIA
hw/display: add vhost-user-media device hw/display/vhost-user-media: add shared memory region BAR
MAINTAINERS | 6 + hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-media-pci.c | 115 +++++++++ hw/display/vhost-user-media.c | 388 ++++++++++++++++++++++++++++ hw/virtio/virtio.c | 3 +- include/hw/virtio/vhost-user-media.h | 46 ++++ include/standard-headers/linux/virtio_ids.h | 1 + 8 files changed, 566 insertions(+), 1 deletion(-)
---
base-commit: d2e570cc0f97b936902a5b1b86b73c0f5998b475
change-id: 20260901-virtio-media-81b38a604aaa
Best regards,
Signature · 1 line
Albert Esteve <[email protected]>
PATCH v2 1/3 linux-headers: add VIRTIO_ID_MEDIA
The ID was added in Linux commit 2187be212179 ("virtio: Add ID for virtio media") and is included in v7.3-rc1.
--- include/standard-headers/linux/virtio_ids.h | 1 + 1 file changed, 1 insertion(+)
include/standard-headers/linux/virtio_ids.h +1 -0
@@ -69,6 +69,7 @@#define VIRTIO_ID_BT 40/* virtio bluetooth */ #define VIRTIO_ID_GPIO 41/* virtio gpio */ #define VIRTIO_ID_SPI 45/* virtio spi */ +#define VIRTIO_ID_MEDIA 48/* virtio media */ /* * Virtio Transitional IDs
Signature · 1 line
2.55.0
PATCH v2 2/3 hw/display: add vhost-user-media device
In reply to Albert EsteveAdd the QEMU side of the vhost-user-media device, which connects to a virtio-media vhost-user backend daemon implementing the V4L2 API over the virtio-media protocol.
The virtio-media device is specified in the VirtIO specification v1.4, section 5.22:
https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.html
Tested with the rust-vmm vhost-device-media backend [1]:
cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxy
Example invocation:
qemu-system-x86_64 \
-chardev socket,path=/path/to/media.sock,id=media \
-device vhost-user-media-pci,chardev=media,id=media[1] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-media
--- MAINTAINERS | 6 + hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-media-pci.c | 80 ++++++++ hw/display/vhost-user-media.c | 348 +++++++++++++++++++++++++++++++++++ hw/virtio/virtio.c | 3 +- include/hw/virtio/vhost-user-media.h | 46 +++++ 7 files changed, 490 insertions(+), 1 deletion(-)
MAINTAINERS +6 -0
@@ -2697,6 +2697,12 @@ S: Supported F: include/hw/virtio/vhost-user-rtc.h F: hw/virtio/vhost-user-rtc* +vhost-user-media +M: Albert Esteve <[email protected]> +S: Supported +F: hw/display/vhost-user-media* +F: include/hw/virtio/vhost-user-media.h + virtio-crypto M: Gonglei <[email protected]> S: Supported
hw/display/Kconfig +5 -0
@@ -120,6 +120,11 @@ config VHOST_USER_VGA default y depends on VIRTIO_VGA && VHOST_USER_GPU +config VHOST_USER_MEDIA + bool + default y + depends on VIRTIO && VHOST_USER + config DPCD bool select AUX
hw/display/meson.build +3 -0
@@ -37,6 +37,9 @@ system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) system_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-dmabuf.c')) system_ss.add(when: 'CONFIG_DM163', if_true: files('dm163.c')) +system_ss.add(when: 'CONFIG_VHOST_USER_MEDIA', if_true: files('vhost-user-media.c')) +system_ss.add(when: ['CONFIG_VHOST_USER_MEDIA', 'CONFIG_VIRTIO_PCI'], + if_true: files('vhost-user-media-pci.c')) stub_ss.add([files('acpi-vga-stub.c'), pixman]) if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') or
hw/display/vhost-user-media-pci.c new file +80 -0
80 lines not on this page · raw message
hw/display/vhost-user-media.c new file +348 -0
348 lines not on this page · raw message
hw/virtio/virtio.c +2 -1
@@ -198,7 +198,8 @@ const char *virtio_device_names[] = { [VIRTIO_ID_AUDIO_POLICY] = "virtio-audio-pol", [VIRTIO_ID_BT] = "virtio-bluetooth", [VIRTIO_ID_GPIO] = "virtio-gpio", - [VIRTIO_ID_SPI] = "virtio-spi" + [VIRTIO_ID_SPI] = "virtio-spi", + [VIRTIO_ID_MEDIA] = "virtio-media", };static const char *virtio_id_to_name (uint16_t device_id)
include/hw/virtio/vhost-user-media.h new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 3/3 hw/display/vhost-user-media: add shared memory region BAR
In reply to Albert EsteveExpose virtio-media shared memory region 0 (spec 5.22.6.1.6) as a prefetchable 64-bit PCI BAR (BAR 2) with a VirtIO shm capability. The size is obtained through GET_SHMEM_CONFIG and requires at least one shared region. The guest maps host-provisioned MMAP buffers into this window; QEMU creates a container region on realize, not a full-sized RAM allocation.
modern-pio-notify is rejected because it claims BAR 2.
vhost-user still needs shareable guest RAM, e.g.:
-object memory-backend-memfd,id=mem,size=4G,share=on \
-numa node,memdev=mem--- hw/display/vhost-user-media-pci.c | 35 ++++++++++++++++++++++++++++++++ hw/display/vhost-user-media.c | 42 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-)
hw/display/vhost-user-media-pci.c +35 -0
@@ -13,12 +13,18 @@#include "hw/virtio/vhost-user-media.h" #include "hw/virtio/virtio-pci.h" +/* BAR 2 is used for the shared memory region exposed to the guest */ +#define VIRTIO_MEDIA_PCI_SHM_BAR 2 + +#define VIRTIO_MEDIA_PCI_SHMCAP_ID_SHM 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 shmbar; };static const Property vumedia_pci_properties[] = {
@@ -32,14 +38,43 @@ 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 shm_size;if (vpci_dev->nvectors ==DEV_NVECTORS_UNSPECIFIED ) { vpci_dev->nvectors =1 ; } +if (vpci_dev->flags &VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY ) { +error_setg (errp,"modern-pio-notify is not supported due to PCI BAR " +"layout limitations" ); +return ; + } +if (!qdev_realize (dev_state,BUS (&vpci_dev->bus), errp)) {return ; } + + shmem =virtio_find_shmem_region (vdev,VIRTIO_MEDIA_PCI_SHMCAP_ID_SHM ); +if (!shmem) { +error_setg (errp,"vhost-user-media: missing shared memory region" ); +return ; + } + shm_size =memory_region_size (&shmem->mr); + +memory_region_init (&dev->shmbar,OBJECT (vpci_dev), +"vhost-media-pci-shmbar" , shm_size); +memory_region_add_subregion (&dev->shmbar,0 , &shmem->mr); +virtio_pci_add_shm_cap (vpci_dev,VIRTIO_MEDIA_PCI_SHM_BAR ,0 , + shm_size,VIRTIO_MEDIA_PCI_SHMCAP_ID_SHM ); + +/* After 'realized' so the memory region exists */ +pci_register_bar (&vpci_dev->pci_dev,VIRTIO_MEDIA_PCI_SHM_BAR , +PCI_BASE_ADDRESS_SPACE_MEMORY | +PCI_BASE_ADDRESS_MEM_PREFETCH | +PCI_BASE_ADDRESS_MEM_TYPE_64 , + &dev->shmbar); }static void vumedia_pci_class_init (ObjectClass *klass,const void *data)
hw/display/vhost-user-media.c +41 -1
64 lines not on this page · raw message
Signature · 1 line
2.55.0