media: add virtio-media driver
Subsystems (2)
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
@@ -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
@@ -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
@@ -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
@@ -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
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.