Skip to contentSkip to conversation

media: add virtio-media driver

PATCH Superseded Alexandre Courbot 1 participant 1 message Feb 1, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 media: add virtio-media driver

Add the first version of the virtio-media driver.

This driver acts roughly as a V4L2 relay between user-space and the virtio virtual device on the host, so it is relatively simple, yet unconventional. It doesn't use VB2 or other frameworks typically used in a V4L2 driver, and most of its complexity resides in correctly and efficiently building the virtio descriptor chain to pass to the host, avoiding copies whenever possible. This is done by scatterlist_builder.[ch].

virtio_media_ioctls.c proxies each supported ioctl to the host, using code generated through macros for ioctls that can be forwarded directly, which is most of them.

virtio_media_driver.c provides the expected driver hooks, and support for mmapping and polling.

This version supports MMAP buffers, while USERPTR buffers can also be enabled through a driver option. DMABUF support is still pending.

---
This patch adds the virtio-media kernel driver. Virtio-media [1] encapsulates the V4L2 structures and protocol to enable the virtualization of host media devices into a guest. It's specification is in the final stages [2] of being merged and the virtualization of cameras and video accelerator devices has already been demonstrated using crosvm [3] and QEmu. v4l2-compliance also passes on all tested devices, which includes the "simple" virtual test device, proxied host UVC and vivid devices, and the FFmpeg virtual decoder devices (refer to
[3] in order to test these if desired).

Virtio-media is merged in AOSP [4] and ChromeOS. Upstreaming of the driver is overdue, but I hope we can start the review process and converge into something that can be merged.

Limitations:

- The driver is currently only available to little-endian, 64-bit kernels. This is because some of the V4L2 structures used for communication between guest and host have a layout dependent on the architecture, and the virtio-media protocol is standardized on the little-endian 64-bit versions. This can be fixed with a conversion layer similar to the one used to convert 32-bit ioctls to their 64-bit counterpart.
- DMABUF support is currently missing. It should be implemented using virtio objects, with possible support for memfds using the SHARED_PAGES memory type.
- No support for the media API and requests. While the use-case for these is less important on virtual devices where we want to present an abstraction as high as possible to limit VM exits, they do exist and it would be nice to add behind a virtio feature bit.
- Locking in the driver is still very basic. This is something I want to improve before merging, but I didn't want to delay upstream review any further.

[1] https://github.com/chromeos/virtio-media
[2] https://lore.kernel.org/virtio-comment/[email protected]/T/
[3] https://github.com/chromeos/virtio-media/blob/main/TRY_IT_OUT.md
[4] https://android.googlesource.com/platform/external/virtio-media/
---
Changes in v2:
- Fixed kernel test robot and media CI warnings (ignored a few false positives).
- Changed in-driver email address to personal one since my Google one
will soon become invalid.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 MAINTAINERS                                |    6 +
 drivers/media/Kconfig                      |   13 +
 drivers/media/Makefile                     |    2 +
 drivers/media/virtio/Makefile              |    9 +
 drivers/media/virtio/protocol.h            |  288 ++++++
 drivers/media/virtio/scatterlist_builder.c |  563 ++++++++++++
 drivers/media/virtio/scatterlist_builder.h |  111 +++
 drivers/media/virtio/session.h             |  109 +++
 drivers/media/virtio/virtio_media.h        |   93 ++
 drivers/media/virtio/virtio_media_driver.c |  959 ++++++++++++++++++++
 drivers/media/virtio/virtio_media_ioctls.c | 1303 ++++++++++++++++++++++++++++
 include/uapi/linux/virtio_ids.h            |    1 +
 12 files changed, 3457 insertions(+)
MAINTAINERS +6 -0
index 0fa7c5728f1e64d031f4a47b6fce1db484ce0fc2..42319dc40b2e902d3a2d5b7cca030006bc575979 100644
@@ -24920,6 +24920,12 @@ S:	Maintained
 F:	drivers/iommu/virtio-iommu.c
 F:	include/uapi/linux/virtio_iommu.h
 
+VIRTIO MEDIA DRIVER
+M:	Alexandre Courbot <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/media/virtio/
+
 VIRTIO MEM DRIVER
 M:	David Hildenbrand <[email protected]>
 L:	[email protected]
drivers/media/Kconfig +13 -0
index 6abc9302cd84d8563b7877d3d3da4b7e05a6b5d2..12bbb169c0b04565271092c7ac608b0fb11c0244 100644
@@ -230,6 +230,19 @@ source "drivers/media/platform/Kconfig"
 source "drivers/media/mmc/Kconfig"
 endif
 
+config MEDIA_VIRTIO
+	tristate "Virtio-media Driver"
+	depends on VIRTIO && VIDEO_DEV && 64BIT && (X86 || (ARM && CPU_LITTLE_ENDIAN))
+	select VIDEOBUF2_CORE
+	select VIDEOBUF2_MEMOPS
+	help
+	  Enables the virtio-media driver.
+
+	  This driver is used to virtualize media devices such as cameras or
+	  decoders from a host into a guest using the V4L2 protocol.
+
+	  If unsure, say N.
+
 if MEDIA_TEST_SUPPORT
 source "drivers/media/test-drivers/Kconfig"
 endif
drivers/media/Makefile +2 -0
index 20fac24e4f0f13134c12cd859141c8b0387030fa..7a1377661919701f27f4fa2b5ee2dcb1045deb3c 100644
@@ -25,6 +25,8 @@ obj-y += rc/
 
 obj-$(CONFIG_CEC_CORE) += cec/
 
+obj-$(CONFIG_MEDIA_VIRTIO) += virtio/
+
 #
 # Finally, merge the drivers that require the core
 #
drivers/media/virtio/Makefile new file +9 -0
new file mode 100644
index 0000000000000000000000000000000000000000..16f91304420d70e1212cc46f3b12f314a510c051
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for the virtio-media device driver.
+
+virtio-media-objs := scatterlist_builder.o virtio_media_ioctls.o \
+			virtio_media_driver.o
+
+obj-$(CONFIG_MEDIA_VIRTIO) += virtio-media.o
+
drivers/media/virtio/protocol.h new file +288 -0

288 lines not on this page · raw message

drivers/media/virtio/scatterlist_builder.c new file +563 -0

563 lines not on this page · raw message

drivers/media/virtio/scatterlist_builder.h new file +111 -0

111 lines not on this page · raw message

drivers/media/virtio/session.h new file +109 -0

109 lines not on this page · raw message

drivers/media/virtio/virtio_media.h new file +93 -0

93 lines not on this page · raw message

drivers/media/virtio/virtio_media_driver.c new file +959 -0

959 lines not on this page · raw message

drivers/media/virtio/virtio_media_ioctls.c new file +1303 -0

829 lines not on this page · raw message

include/uapi/linux/virtio_ids.h +1 -0
index 7aa2eb76620508fdc915533f74973d76308d3ef5..b4bb0ace0b26e37224c975f89bbf669c51921816 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

---
base-commit: ffd294d346d185b70e28b1a28abe367bbfe53c04
change-id: 20241229-virtio-media-25067bb27526

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>

Part of the diff is not shown here. View the raw message.