From: Albert Esteve <aesteve@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Thomas Huth" <thuth@redhat.com>,
fmartine@redhat.com, "Gerd Hoffmann" <kraxel@redhat.com>,
eballetb@redhat.com, "Albert Esteve" <aesteve@redhat.com>,
alex.bennee@linaro.org, "Laurent Vivier" <lvivier@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
sgarzare@redhat.com,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
peter.griffin@linaro.org, "Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PATCH 11/12] vhost-user-video-udmabuf: add udmabuf helpers
Date: Wed, 22 Mar 2023 15:21:31 +0100 [thread overview]
Message-ID: <20230322142132.22909-12-aesteve@redhat.com> (raw)
In-Reply-To: <20230322142132.22909-1-aesteve@redhat.com>
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
diff --git a/tools/vhost-user-video/meson.build b/tools/vhost-user-video/meson.build
index bf3b958dc6..87c70091e6 100644
--- a/tools/vhost-user-video/meson.build
+++ b/tools/vhost-user-video/meson.build
@@ -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'))
diff --git a/tools/vhost-user-video/virtio_video_helpers.h b/tools/vhost-user-video/virtio_video_helpers.h
index 8f35ccc4b5..171c74fc00 100644
--- a/tools/vhost-user-video/virtio_video_helpers.h
+++ b/tools/vhost-user-video/virtio_video_helpers.h
@@ -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;
diff --git a/tools/vhost-user-video/virtio_video_udmabuf.c b/tools/vhost-user-video/virtio_video_udmabuf.c
new file mode 100644
index 0000000000..4fda1b5a78
--- /dev/null
+++ b/tools/vhost-user-video/virtio_video_udmabuf.c
@@ -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);
+}
diff --git a/tools/vhost-user-video/vuvideo.h b/tools/vhost-user-video/vuvideo.h
index d853c69682..b198b71fad 100644
--- a/tools/vhost-user-video/vuvideo.h
+++ b/tools/vhost-user-video/vuvideo.h
@@ -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
--
2.39.2
next prev parent reply other threads:[~2023-03-22 14:22 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-22 14:21 [PATCH 00/12] [RFC PATCHv2] Add vhost-user Video decode Albert Esteve
2023-03-22 14:21 ` [PATCH 01/12] docs: Add a vhost-virtio-video rst file Albert Esteve
2023-03-22 14:21 ` [PATCH 02/12] MAINTAINERS: Add virtio-video section Albert Esteve
2023-03-22 14:45 ` Thomas Huth
2023-03-22 15:28 ` Albert Esteve
2023-03-22 14:21 ` [PATCH 03/12] vhost-user-video: boiler plate code for vhost-user-video device Albert Esteve
2023-03-22 14:21 ` [PATCH 04/12] vhost-user-video: add meson subdir build logic Albert Esteve
2023-03-22 14:21 ` [PATCH 05/12] standard-headers: Add virtio_video.h Albert Esteve
2023-03-22 14:21 ` [PATCH 06/12] hw/display: add vhost-user-video-pci Albert Esteve
2023-03-22 14:21 ` [PATCH 07/12] vhost-user.json: add video type Albert Esteve
2023-03-22 14:21 ` [PATCH 08/12] tools/vhost-user-video: Add initial vhost-user-video vmm Albert Esteve
2023-03-22 14:21 ` [PATCH 09/12] tests/qtest: add virtio-video test Albert Esteve
2023-03-22 14:21 ` [PATCH 10/12] vhost-user-video: add dev_type to CLI Albert Esteve
2023-03-22 14:21 ` Albert Esteve [this message]
2023-03-22 14:21 ` [PATCH 12/12] Add support for v4l2_memory_dmabuf Albert Esteve
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230322142132.22909-12-aesteve@redhat.com \
--to=aesteve@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=eballetb@redhat.com \
--cc=fmartine@redhat.com \
--cc=kraxel@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.griffin@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).