@@ -1,5 +1,5 @@
executable('vhost-user-video', files(
- 'vhost-user-video.c', 'v4l2_backend.c', 'virtio_video_helpers.c'),
+ 'vhost-user-video.c', 'v4l2_backend.c', 'virtio_video_helpers.c', 'virtio_video_udmabuf.c'),
dependencies: [qemuutil, glib, gio, vhost_user],
install: true,
install_dir: get_option('libexecdir'))
@@ -21,17 +21,37 @@
#include <linux/videodev2.h>
#include "libvhost-user-glib.h"
#include "libvhost-user.h"
+#include "qemu/uuid.h"
+#include "qemu/queue.h"
/*
* Structure to track internal state of VIDEO Device
*/
+struct resource;
+struct VuVideoDMABuf;
+
+struct vuvbm_device {
+ bool opened;
+ int fd;
+
+ bool (*alloc_bm)(struct VuVideoDMABuf *buf);
+ void (*free_bm)(struct VuVideoDMABuf *buf);
+ int (*get_fd)(struct VuVideoDMABuf *buf);
+ bool (*map_bm)(struct VuVideoDMABuf *buf);
+ void (*unmap_bm)(struct VuVideoDMABuf *buf);
+ void (*device_destroy)(struct vuvbm_device *dev);
+
+ GHashTable *resource_uuids;
+};
+
typedef struct VuVideo {
VugDev dev;
struct virtio_video_config virtio_config;
GMainLoop *loop;
struct v4l2_device *v4l2_dev;
GList *streams;
+ struct vuvbm_device *bm_dev;
} VuVideo;
struct v4l2_device {
@@ -56,10 +76,18 @@ struct vu_video_ctrl_command {
};
+typedef struct VuVideoDMABuf {
+ struct vuvbm_device *dev;
+ int memfd;
+ int dmafd;
+
+ void *start;
+ size_t length;
+} VuVideoDMABuf;
+
/*
* Structure to track internal state of a Stream
*/
-
struct stream {
struct virtio_video_stream_create vio_stream;
uint32_t stream_id;
@@ -89,11 +117,13 @@ struct stream {
struct resource {
uint32_t stream_id;
+ QemuUUID uuid;
struct virtio_video_resource_create vio_resource;
struct virtio_video_resource_queue vio_res_q;
struct iovec *iov;
uint32_t iov_count;
uint32_t v4l2_index;
+ struct VuVideoDMABuf *buf;
enum v4l2_buf_type type;
struct vu_video_ctrl_command *vio_q_cmd;
bool queued;
new file mode 100644
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Virtio Video Device
+ *
+ * Copyright Red Hat, Inc. 2023
+ *
+ * Authors:
+ * Albert Esteve <aesteve@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include "linux/udmabuf.h"
+
+#include "vuvideo.h"
+
+static size_t
+udmabuf_get_size(struct VuVideoDMABuf *buf)
+{
+ return ROUND_UP(buf->length, qemu_real_host_page_size());
+}
+
+static bool
+udmabuf_alloc_bm(struct VuVideoDMABuf *buf)
+{
+ int ret;
+
+ buf->memfd = memfd_create("udmabuf-video-bm", MFD_ALLOW_SEALING);
+ if (buf->memfd < 0) {
+ g_printerr("%s: memfd_create failed.", __func__);
+ return false;
+ }
+
+ ret = ftruncate(buf->memfd, udmabuf_get_size(buf));
+ if (ret < 0) {
+ g_printerr("%s: ftruncate failed.", __func__);
+ close(buf->memfd);
+ return false;
+ }
+
+ ret = fcntl(buf->memfd, F_ADD_SEALS, F_SEAL_SHRINK);
+ if (ret < 0) {
+ g_printerr("%s: fcntl failed.", __func__);
+ close(buf->memfd);
+ return false;
+ }
+
+ return true;
+}
+
+static void
+udmabuf_free_bm(struct VuVideoDMABuf *buf)
+{
+ close(buf->memfd);
+}
+
+static bool
+udmabuf_map_bm(struct VuVideoDMABuf *buf)
+{
+ g_debug("Map the buffer memory.");
+ buf->start = mmap(NULL, udmabuf_get_size(buf),
+ PROT_READ | PROT_WRITE, MAP_SHARED, buf->memfd, 0);
+ if (buf->start == MAP_FAILED) {
+ return false;
+ }
+
+ return true;
+}
+
+static void
+udmabuf_unmap_bm(struct VuVideoDMABuf *buf)
+{
+ g_debug("Unmap the buffer memory.");
+ munmap(buf->start, udmabuf_get_size(buf));
+}
+
+static int
+udmabuf_get_fd(struct VuVideoDMABuf *buf)
+{
+ if (buf->dmafd > 0) {
+ return buf->dmafd;
+ }
+
+ struct udmabuf_create create = {
+ .memfd = buf->memfd,
+ .offset = 0,
+ .size = udmabuf_get_size(buf),
+ };
+
+ buf->dmafd = ioctl(buf->dev->fd, UDMABUF_CREATE, &create);
+ if (buf->dmafd < 0) {
+ g_printerr("%s: UDMABUF_CREATE failed.", __func__);
+ }
+
+ return buf->dmafd;
+}
+
+static void
+udmabuf_device_destroy(struct vuvbm_device *dev)
+{
+ close(dev->fd);
+}
+
+static bool
+vuvbm_buffer_map(struct VuVideoDMABuf *buf)
+{
+ struct vuvbm_device *dev = buf->dev;
+
+ return dev->map_bm(buf);
+}
+
+bool vuvbm_buffer_create(struct vuvbm_device *dev,
+ struct VuVideoDMABuf *buffer, uint32_t len)
+{
+ g_debug("Creating buffer length(%d)", len);
+ buffer->dev = dev;
+ buffer->length = len;
+ if (!dev->alloc_bm(buffer)) {
+ g_warning("alloc_bm failed");
+ return false;
+ }
+
+ if (!vuvbm_buffer_map(buffer)) {
+ g_warning("map_bm failed");
+ goto err;
+ }
+
+ return true;
+
+err:
+ buffer->dev->free_bm(buffer);
+ return false;
+}
+
+void vuvbm_init_device(struct vuvbm_device *dev)
+{
+ if (!dev->opened && g_file_test("/dev/udmabuf", G_FILE_TEST_EXISTS)) {
+ dev->fd = open("/dev/udmabuf", O_RDWR);
+ if (dev->fd >= 0) {
+ g_debug("Using experimental udmabuf backend");
+ dev->alloc_bm = udmabuf_alloc_bm;
+ dev->free_bm = udmabuf_free_bm;
+ dev->get_fd = udmabuf_get_fd;
+ dev->map_bm = udmabuf_map_bm;
+ dev->unmap_bm = udmabuf_unmap_bm;
+ dev->device_destroy = udmabuf_device_destroy;
+ dev->resource_uuids = g_hash_table_new_full(
+ NULL, NULL, NULL, g_free);
+ dev->opened = true;
+ }
+ }
+ g_debug("Using udmabuf backend");
+}
+
+struct VuVideoDMABuf *vuvbm_lookup(struct vuvbm_device *dev, QemuUUID uuid)
+{
+ g_debug("Lookup for buffer uuid(%s)", qemu_uuid_unparse_strdup(&uuid));
+ return g_hash_table_lookup(dev->resource_uuids, &uuid);
+}
+
+void vuvbm_buffer_destroy(struct VuVideoDMABuf *buffer)
+{
+ struct vuvbm_device *dev = buffer->dev;
+
+ dev->unmap_bm(buffer);
+ dev->free_bm(buffer);
+}
+
+void vuvbm_device_destroy(struct vuvbm_device *dev)
+{
+ if (!dev->opened) {
+ return;
+ }
+
+ dev->device_destroy(dev);
+}
@@ -18,7 +18,6 @@
#include "virtio_video_helpers.h"
#include "v4l2_backend.h"
-#include "vuvideo.h"
GList *get_resource_list(struct stream *s, uint32_t queue_type);
void send_qclear_res_reply(gpointer data, gpointer user_data);
@@ -40,4 +39,13 @@ void remove_all_resources(struct stream *s, uint32_t queue_type);
void handle_queue_clear_cmd(struct VuVideo *v,
struct vu_video_ctrl_command *vio_cmd);
+/* virtio_video_udmabuf.c */
+bool vuvbm_buffer_create(struct vuvbm_device *dev,
+ struct VuVideoDMABuf *buffer,
+ uint32_t len);
+void vuvbm_init_device(struct vuvbm_device *dev);
+struct VuVideoDMABuf *vuvbm_lookup(struct vuvbm_device *dev, QemuUUID uuid);
+void vuvbm_buffer_destroy(struct VuVideoDMABuf *buffer);
+void vuvbm_device_destroy(struct vuvbm_device *dev);
+
#endif
Add helper for handling user-created DMA buffers through the udmabuf device. Buffers are stored in a hash table and associated to an UUID, so that they can be looked up later from the backend. Signed-off-by: Albert Esteve <aesteve@redhat.com> --- tools/vhost-user-video/meson.build | 2 +- tools/vhost-user-video/virtio_video_helpers.h | 32 +++- tools/vhost-user-video/virtio_video_udmabuf.c | 180 ++++++++++++++++++ tools/vhost-user-video/vuvideo.h | 10 +- 4 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 tools/vhost-user-video/virtio_video_udmabuf.c