* [RFC PATCH 1/2] hw/display/virtio-accel: add DRM-based accelerator device
2026-12-02 7:44 [RFC PATCH 0/2] Introduce virtio-accel device for NPU virtualization Wendy Liang
@ 2026-12-02 7:44 ` Wendy Liang
2026-12-02 7:44 ` [RFC PATCH 2/2] hw/display/virtio-accel: add host-memory blob mapping Wendy Liang
1 sibling, 0 replies; 3+ messages in thread
From: Wendy Liang @ 2026-12-02 7:44 UTC (permalink / raw)
To: qemu-devel
Cc: Wendy Liang, Akihiko Odaki, Alex Bennée,
Daniel P. Berrangé, Dmitry Osipenko, Lizhi Hou,
Marc-André Lureau, Max Zhen, Michael S. Tsirkin,
Paolo Bonzini, Pierrick Bouvier
Add a virtio-gpu DRM native-context device that virtualizes an AMD XDNA
NPU (AIE) over the existing virtio-gpu control queue. The device uses a
library that implements the vaccel C APIs declared in vaccel.h:
vaccel_create, vaccel_submit_ccmd, vaccel_submit_fence, vaccel_resource_map
and so on. libvxdna (github.com/amd/xdna-driver, src/vxdna) is the only library
that provides the implementation today and it supports AMD XDNA only. This
library is discovered at build time via pkg-config. The device is gated behind
the drm_accel build option and activated by the device property
accel-node=accelN. The code opens /dev/accel/<accel-node>.
This device:
* queries capset for capset negotiation during device initialization. The guest
sees VIRTIO_GPU_CAPSET_DRM capset ID with the AMD XDNA context type inside.
* manages contexts
* manages resource blobs
* QEMU turns the guest page list into host virtual iovecs and passes them to
libvxdna, and the NPU accesses guest memory through amdxdna userptr buffer
objects.
* executes CCMDs. It supports asynchronous fence completions. libvxdna calls back
into the device, and then the device completes the command in a bottom half.
Use build option `--enable-drm-accel` to compile the virtio-accel device.
Launch QEMU with the following options to enable the accel device in the guest:
-object memory-backend-memfd,id=mem0,size=16G,share=on \
-machine q35,memory-backend=mem0,accel=kvm \
-device virtio-accel-pci,accel-node=accel0
Signed-off-by: Wendy Liang <wendy.liang@amd.com>
---
MAINTAINERS | 7 +
hw/display/meson.build | 15 +
hw/display/trace-events | 7 +
hw/display/virtio-accel-pci.c | 52 ++++
hw/display/virtio-accel.c | 549 +++++++++++++++++++++++++++++++++
hw/display/virtio-gpu-base.c | 3 +-
hw/display/virtio-gpu.c | 1 +
include/hw/virtio/virtio-gpu.h | 30 ++
meson.build | 7 +
meson_options.txt | 2 +
scripts/meson-buildoptions.sh | 3 +
11 files changed, 675 insertions(+), 1 deletion(-)
create mode 100644 hw/display/virtio-accel-pci.c
create mode 100644 hw/display/virtio-accel.c
diff --git a/MAINTAINERS b/MAINTAINERS
index ad9dfc43898..ccd52e42547 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3006,6 +3006,13 @@ F: docs/system/devices/virtio/virtio-gpu.rst
F: tests/functional/aarch64/test_virt_gpu.py
F: tests/functional/x86_64/test_virtio_gpu.py
+virtio-accel
+M: Wendy Liang <wendy.liang@amd.com>
+M: Lizhi Hou <lizhi.hou@amd.com>
+S: Maintained
+F: hw/display/virtio-accel.c
+F: hw/display/virtio-accel-pci.c
+
vhost-user-blk
M: Raphael Norwitz <rnorwitz@nvidia.com>
S: Maintained
diff --git a/hw/display/meson.build b/hw/display/meson.build
index d8b4e121fea..699cdbbb1df 100644
--- a/hw/display/meson.build
+++ b/hw/display/meson.build
@@ -93,6 +93,13 @@ if config_all_devices.has_key('CONFIG_VIRTIO_GPU')
if_true: [files('virtio-gpu-rutabaga.c'), pixman])
hw_display_modules += {'virtio-gpu-rutabaga': virtio_gpu_rutabaga_ss}
endif
+
+ if drm_accel.found()
+ virtio_accel_ss = ss.source_set()
+ virtio_accel_ss.add(when: ['CONFIG_VIRTIO_GPU'],
+ if_true: [files('virtio-accel.c'), drm_accel])
+ hw_display_modules += {'virtio-accel': virtio_accel_ss}
+ endif
endif
if config_all_devices.has_key('CONFIG_VIRTIO_PCI')
@@ -109,12 +116,20 @@ if config_all_devices.has_key('CONFIG_VIRTIO_PCI')
if_true: [files('virtio-gpu-pci-gl.c'), pixman])
hw_display_modules += {'virtio-gpu-pci-gl': virtio_gpu_pci_gl_ss}
endif
+
if rutabaga.found()
virtio_gpu_pci_rutabaga_ss = ss.source_set()
virtio_gpu_pci_rutabaga_ss.add(when: ['CONFIG_VIRTIO_GPU', 'CONFIG_VIRTIO_PCI', rutabaga],
if_true: [files('virtio-gpu-pci-rutabaga.c'), pixman])
hw_display_modules += {'virtio-gpu-pci-rutabaga': virtio_gpu_pci_rutabaga_ss}
endif
+
+ if drm_accel.found()
+ virtio_accel_pci_ss = ss.source_set()
+ virtio_accel_pci_ss.add(when: ['CONFIG_VIRTIO_GPU', 'CONFIG_VIRTIO_PCI'],
+ if_true: [files('virtio-accel-pci.c'), drm_accel])
+ hw_display_modules += {'virtio-accel-pci': virtio_accel_pci_ss}
+ endif
endif
if config_all_devices.has_key('CONFIG_VIRTIO_VGA')
diff --git a/hw/display/trace-events b/hw/display/trace-events
index 223a085922b..7123b7c2c3a 100644
--- a/hw/display/trace-events
+++ b/hw/display/trace-events
@@ -59,6 +59,13 @@ virtio_gpu_inc_inflight_fences(uint32_t inflight) "in-flight+ %u"
virtio_gpu_dec_inflight_fences(uint32_t inflight) "in-flight- %u"
virtio_gpu_cmd_suspended(uint32_t cmd) "cmd 0x%x"
+# virtio-accel.c
+virtio_accel_cmd(uint32_t type, uint32_t ctx_id, uint32_t flags) "type=0x%x ctx=0x%x flags=0x%x"
+virtio_accel_cmd_ctx_create(uint32_t ctx_id, uint32_t nlen, const char *name, const char *node) "ctx=0x%x nlen=%u name=%s node=%s"
+virtio_accel_cmd_ctx_destroy(uint32_t ctx_id) "ctx=0x%x"
+virtio_accel_cmd_resource_create_blob(uint32_t resource_id, uint32_t ctx_id, uint64_t blob_id, uint32_t blob_flags, uint32_t blob_mem, uint64_t size) "resource_id=0x%x ctx_id=0x%x blob_id=0x%" PRIx64 " blob_flags=0x%x blob_mem=0x%x size=0x%" PRIx64
+virtio_accel_create_blob(uint32_t resource_id, uint32_t blob_mem, uint32_t nr_entries, uint64_t size, uint32_t ctx_id) "resource_id=0x%x blob_mem=0x%x nr_entries=%u size=0x%" PRIx64 " ctx=0x%x"
+
# qxl.c
qxl_io_write_vga(int qid, const char *mode, uint32_t addr, uint32_t val) "%d %s addr=%u val=%u"
qxl_create_guest_primary(int qid, uint32_t width, uint32_t height, uint64_t mem, uint32_t format, uint32_t position) "%d %ux%u mem=0x%" PRIx64 " %u,%u"
diff --git a/hw/display/virtio-accel-pci.c b/hw/display/virtio-accel-pci.c
new file mode 100644
index 00000000000..4adc280677f
--- /dev/null
+++ b/hw/display/virtio-accel-pci.c
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Virtio Accelerator Device
+ *
+ * Copyright (C) 2026, Advanced Micro Devices, Inc.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/module.h"
+#include "hw/pci/pci.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/virtio-gpu-pci.h"
+#include "qom/object.h"
+
+#define TYPE_VIRTIO_ACCEL_PCI "virtio-accel-pci"
+OBJECT_DECLARE_SIMPLE_TYPE(VirtIOAccelPCI, VIRTIO_ACCEL_PCI)
+
+struct VirtIOAccelPCI {
+ VirtIOGPUPCIBase parent_obj;
+ VirtIOAccel vdev;
+};
+
+static void virtio_accel_initfn(Object *obj)
+{
+ VirtIOAccelPCI *dev = VIRTIO_ACCEL_PCI(obj);
+
+ virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
+ TYPE_VIRTIO_ACCEL);
+ VIRTIO_GPU_PCI_BASE(obj)->vgpu = VIRTIO_GPU_BASE(&dev->vdev);
+}
+
+static const TypeInfo virtio_accel_pci_info[] = {
+ {
+ .name = TYPE_VIRTIO_ACCEL_PCI,
+ .parent = TYPE_VIRTIO_GPU_PCI_BASE,
+ .instance_size = sizeof(VirtIOAccelPCI),
+ .instance_init = virtio_accel_initfn,
+ .interfaces = (InterfaceInfo[]) {
+ { INTERFACE_CONVENTIONAL_PCI_DEVICE },
+ { },
+ }
+ },
+};
+
+DEFINE_TYPES(virtio_accel_pci_info)
+
+module_obj(TYPE_VIRTIO_ACCEL_PCI);
+module_kconfig(VIRTIO_PCI);
+module_dep("hw-display-virtio-gpu-pci");
diff --git a/hw/display/virtio-accel.c b/hw/display/virtio-accel.c
new file mode 100644
index 00000000000..3de2ada5ca9
--- /dev/null
+++ b/hw/display/virtio-accel.c
@@ -0,0 +1,549 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Virtio Accelerator Device
+ *
+ * Copyright (C) 2026, Advanced Micro Devices, Inc.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/error-report.h"
+#include "qemu/iov.h"
+#include "qemu/log.h"
+#include "qemu/thread.h"
+#include "qemu/main-loop.h"
+#include "trace.h"
+#include "migration/blocker.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-gpu.h"
+#include "hw/virtio/virtio-gpu-bswap.h"
+
+#include <vaccel.h>
+
+static int accel_get_drm_fd(void *opaque)
+{
+ VirtIOGPU *g = opaque;
+ VirtIOAccel *accel = VIRTIO_ACCEL(g);
+ char name[64];
+
+ snprintf(name, sizeof(name), "/dev/accel/%s", accel->accel_node);
+ return qemu_open_old(name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
+}
+
+/*
+ * Drain fence completions queued by accel_write_context_fence(). Runs as a
+ * bottom half on the main loop, so the BQL is held and it is safe to touch
+ * g->fenceq and the virtqueue here.
+ */
+static void accel_context_fence_bh(void *opaque)
+{
+ VirtIOGPU *g = opaque;
+ VirtIOAccel *accel = VIRTIO_ACCEL(g);
+ QSLIST_HEAD(, accel_context_fence) fenceq;
+ struct accel_context_fence *f;
+ struct virtio_gpu_ctrl_command *cmd, *tmp;
+
+ QSLIST_MOVE_ATOMIC(&fenceq, &accel->async_fenceq);
+
+ while (!QSLIST_EMPTY(&fenceq)) {
+ f = QSLIST_FIRST(&fenceq);
+ QSLIST_REMOVE_HEAD(&fenceq, next);
+
+ QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
+ if (cmd->cmd_hdr.fence_id > f->fence_id) {
+ continue;
+ }
+ if (cmd->cmd_hdr.ctx_id != f->ctx_id) {
+ continue;
+ }
+ if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_INFO_RING_IDX &&
+ cmd->cmd_hdr.ring_idx != f->ring_idx) {
+ continue;
+ }
+
+ virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
+ QTAILQ_REMOVE(&g->fenceq, cmd, next);
+ g_free(cmd);
+ g->inflight--;
+ }
+
+ g_free(f);
+ }
+}
+
+/*
+ * Invoked on the vaccel fence-polling thread (not a QEMU thread). Never take
+ * the BQL here: ctx teardown joins this thread while holding the BQL, so doing
+ * so deadlocks. Enqueue lock-free and kick the BH, which does the real work on
+ * the main loop under the BQL.
+ */
+static void accel_write_context_fence(void *opaque, uint32_t ctx_id,
+ uint32_t ring_idx, uint64_t fence_id)
+{
+ VirtIOGPU *g = opaque;
+ VirtIOAccel *accel = VIRTIO_ACCEL(g);
+ struct accel_context_fence *f = g_new(struct accel_context_fence, 1);
+
+ f->ctx_id = ctx_id;
+ f->ring_idx = ring_idx;
+ f->fence_id = fence_id;
+
+ QSLIST_INSERT_HEAD_ATOMIC(&accel->async_fenceq, f, next);
+ qemu_bh_schedule(accel->fence_bh);
+}
+
+static struct vaccel_callbacks vaccel_cbs = {
+ .get_device_fd = accel_get_drm_fd,
+ .write_context_fence = accel_write_context_fence,
+};
+
+static void virtio_accel_realize(DeviceState *qdev, Error **errp)
+{
+ ERRP_GUARD();
+ VirtIOGPUBase *bdev = VIRTIO_GPU_BASE(qdev);
+ VirtIOGPU *g = VIRTIO_GPU(qdev);
+ VirtIOAccel *accel = VIRTIO_ACCEL(g);
+ int ret;
+
+ bdev->virtio_config.num_capsets = 1;
+ bdev->conf.max_outputs = 0;
+ bdev->conf.flags |= (1 << VIRTIO_GPU_FLAG_ACCEL_ENABLED);
+ bdev->conf.flags |= (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED);
+ bdev->conf.flags |= (1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED);
+
+ /* virtio-accel does not support migration yet. */
+ error_setg(&bdev->migration_blocker,
+ "virtio-accel does not support migration");
+ if (migrate_add_blocker(&bdev->migration_blocker, errp) < 0) {
+ return;
+ }
+
+ /*
+ * Set up the fence-completion BH before any context (hence polling thread)
+ * can exist and call accel_write_context_fence().
+ */
+ QSLIST_INIT(&accel->async_fenceq);
+ accel->fence_bh = aio_bh_new_guarded(qemu_get_aio_context(),
+ accel_context_fence_bh, g,
+ &DEVICE(g)->mem_reentrancy_guard);
+
+ ret = vaccel_create(g, VIRACCEL_CAPSET_ID_AMDXDNA, &vaccel_cbs);
+ if (ret) {
+ error_setg(errp, "Init specified accel device failed");
+ qemu_bh_delete(accel->fence_bh);
+ accel->fence_bh = NULL;
+ migrate_del_blocker(&bdev->migration_blocker);
+ return;
+ }
+
+ virtio_gpu_device_realize(qdev, errp);
+ if (*errp) {
+ vaccel_destroy(g);
+ qemu_bh_delete(accel->fence_bh);
+ accel->fence_bh = NULL;
+ migrate_del_blocker(&bdev->migration_blocker);
+ }
+}
+
+static void virtio_accel_unrealize(DeviceState *qdev)
+{
+ VirtIOGPU *g = VIRTIO_GPU(qdev);
+ VirtIOAccel *accel = VIRTIO_ACCEL(g);
+ struct accel_context_fence *f;
+
+ vaccel_destroy(g);
+
+ if (accel->fence_bh) {
+ qemu_bh_delete(accel->fence_bh);
+ accel->fence_bh = NULL;
+ }
+ while ((f = QSLIST_FIRST(&accel->async_fenceq))) {
+ QSLIST_REMOVE_HEAD(&accel->async_fenceq, next);
+ g_free(f);
+ }
+
+ migrate_del_blocker(&VIRTIO_GPU_BASE(qdev)->migration_blocker);
+}
+
+static void
+accel_cmd_get_capset_info(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_get_capset_info info;
+ struct virtio_gpu_resp_capset_info resp;
+
+ VIRTIO_GPU_FILL_CMD(info);
+
+ memset(&resp, 0, sizeof(resp));
+
+ /*
+ * Since NPU accelerator is GPU DRM device, we use VIRTIO_GPU_CAPSET_DRM
+ * capset ID for NPU eventhough NPU doesn't have rendering capability.
+ */
+ resp.capset_id = VIRTIO_GPU_CAPSET_DRM;
+ int ret = vaccel_get_capset_info(g, &resp.capset_max_version,
+ &resp.capset_max_size);
+ if (ret) {
+ error_report("virtio-accel: Failed to get capset info: %s",
+ strerror(-ret));
+ }
+ resp.hdr.type = VIRTIO_GPU_RESP_OK_CAPSET_INFO;
+ virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
+}
+
+static void
+accel_cmd_get_capset(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_get_capset gc;
+ struct virtio_gpu_resp_capset *resp;
+ uint32_t max_ver, max_size;
+
+ VIRTIO_GPU_FILL_CMD(gc);
+
+ vaccel_get_capset_info(g, &max_ver, &max_size);
+ if (!max_size) {
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+ return;
+ }
+
+ resp = g_malloc0(sizeof(*resp) + max_size);
+ resp->hdr.type = VIRTIO_GPU_RESP_OK_CAPSET;
+ vaccel_fill_capset(g, max_size, (void *)resp->capset_data);
+ virtio_gpu_ctrl_response(g, cmd, &resp->hdr, sizeof(*resp) + max_size);
+ g_free(resp);
+}
+
+static void
+accel_cmd_ctx_create(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_ctx_create cc;
+ VirtIOAccel *accel = VIRTIO_ACCEL(g);
+
+ VIRTIO_GPU_FILL_CMD(cc);
+
+ trace_virtio_accel_cmd_ctx_create(cc.hdr.ctx_id, cc.nlen, cc.debug_name,
+ accel->accel_node);
+
+ vaccel_create_ctx_with_flags(g, cc.hdr.ctx_id, 0, cc.nlen, cc.debug_name);
+}
+
+static void
+accel_cmd_ctx_destroy(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_ctx_destroy cd;
+
+ VIRTIO_GPU_FILL_CMD(cd);
+
+ trace_virtio_accel_cmd_ctx_destroy(cd.hdr.ctx_id);
+
+ vaccel_destroy_ctx(g, cd.hdr.ctx_id);
+}
+
+static void
+accel_cmd_resource_create_blob(VirtIOGPU *g,
+ struct virtio_gpu_ctrl_command *cmd)
+{
+ struct vaccel_create_resource_blob_args vaccel_args = { 0 };
+ struct virtio_gpu_resource_create_blob cblob;
+ struct virtio_gpu_simple_resource *res;
+ int ret;
+
+ if (!virtio_gpu_blob_enabled(g->parent_obj.conf)) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: blob not enabled\n", __func__);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+ return;
+ }
+
+ VIRTIO_GPU_FILL_CMD(cblob);
+ virtio_gpu_create_blob_bswap(&cblob);
+
+ trace_virtio_accel_create_blob(cblob.resource_id, cblob.blob_mem,
+ cblob.nr_entries, cblob.size,
+ cblob.hdr.ctx_id);
+
+ res = virtio_gpu_find_resource(g, cblob.resource_id);
+ if (res) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already exists %d\n",
+ __func__, cblob.resource_id);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ res = g_new0(struct virtio_gpu_simple_resource, 1);
+ res->resource_id = cblob.resource_id;
+ res->blob_size = cblob.size;
+
+ if (cblob.blob_mem == VIRTIO_GPU_BLOB_MEM_GUEST) {
+ ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
+ cmd, &res->addrs,
+ &res->iov, &res->iov_cnt);
+ if (ret) {
+ qemu_log_mask(LOG_GUEST_ERROR,
+ "%s: create_mapping_iov failed: res=%d, "
+ "nr_entries=%d, size=%ld, ret=%d\n",
+ __func__, cblob.resource_id, cblob.nr_entries,
+ (long)cblob.size, ret);
+ cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+ g_free(res);
+ return;
+ }
+ }
+
+ vaccel_args.res_handle = cblob.resource_id;
+ vaccel_args.ctx_id = cblob.hdr.ctx_id;
+ vaccel_args.blob_mem = cblob.blob_mem;
+ vaccel_args.blob_id = cblob.blob_id;
+ vaccel_args.blob_flags = cblob.blob_flags;
+ vaccel_args.size = cblob.size;
+ vaccel_args.iovecs = res->iov;
+ vaccel_args.num_iovs = res->iov_cnt;
+ trace_virtio_accel_cmd_resource_create_blob(cblob.resource_id,
+ cblob.hdr.ctx_id, cblob.blob_id,
+ cblob.blob_flags,
+ cblob.blob_mem, cblob.size);
+
+ ret = vaccel_create_resource_blob(g, &vaccel_args);
+ if (ret) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: virgl blob create error: %s\n",
+ __func__, strerror(-ret));
+ cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+ goto cleanup_mapping;
+ }
+
+ QTAILQ_INSERT_HEAD(&g->reslist, res, next);
+ res = NULL;
+
+ return;
+
+cleanup_mapping:
+ virtio_gpu_cleanup_mapping(g, res);
+ g_free(res);
+ return;
+}
+
+static void accel_cmd_resource_unref(VirtIOGPU *g,
+ struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_resource_unref unref;
+ struct virtio_gpu_simple_resource *res;
+ struct iovec *res_iovs = NULL;
+ uint32_t num_iovs = 0;
+
+ VIRTIO_GPU_FILL_CMD(unref);
+
+
+ res = virtio_gpu_find_resource(g, unref.resource_id);
+ if (!res) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource does not exist %d\n",
+ __func__, unref.resource_id);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ vaccel_detach_resource_blob(g, unref.resource_id,
+ &res_iovs,
+ &num_iovs);
+ if (res_iovs != NULL && num_iovs != 0) {
+ virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
+ }
+
+ vaccel_destroy_resource_blob(g, unref.resource_id);
+ QTAILQ_REMOVE(&g->reslist, res, next);
+ g_free(res);
+}
+
+static void accel_cmd_submit(VirtIOGPU *g,
+ struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_cmd_submit cs;
+ uint8_t stack_buf[2048];
+ void *heap_buf = NULL;
+ void *buf;
+ size_t s;
+ size_t available;
+ int ret;
+
+ VIRTIO_GPU_FILL_CMD(cs);
+
+ available = iov_size(cmd->elem.out_sg, cmd->elem.out_num);
+ if (cs.size > VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ||
+ available < sizeof(cs) || cs.size > available - sizeof(cs)) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid submit size %u\n",
+ __func__, cs.size);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+ return;
+ }
+
+ if (cs.size <= sizeof(stack_buf)) {
+ buf = stack_buf;
+ } else {
+ buf = heap_buf = g_try_malloc(cs.size);
+ if (!buf) {
+ cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
+ return;
+ }
+ }
+
+ s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
+ sizeof(cs), buf, cs.size);
+ if (s != cs.size) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: size mismatch (%zd/%u)\n",
+ __func__, s, cs.size);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+ goto out;
+ }
+
+ ret = vaccel_submit_ccmd(g, cs.hdr.ctx_id, buf, cs.size);
+ if (ret) {
+ cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+ }
+
+out:
+ g_free(heap_buf);
+}
+
+static void
+virtio_accel_process_cmd(VirtIOGPU *g,
+ struct virtio_gpu_ctrl_command *cmd)
+{
+ VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
+
+ trace_virtio_accel_cmd(cmd->cmd_hdr.type, cmd->cmd_hdr.ctx_id,
+ cmd->cmd_hdr.flags);
+
+ switch (cmd->cmd_hdr.type) {
+ case VIRTIO_GPU_CMD_GET_CAPSET_INFO:
+ accel_cmd_get_capset_info(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_GET_CAPSET:
+ accel_cmd_get_capset(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_CTX_CREATE:
+ accel_cmd_ctx_create(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_CTX_DESTROY:
+ accel_cmd_ctx_destroy(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB:
+ accel_cmd_resource_create_blob(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE:
+ break;
+ case VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE:
+ break;
+ case VIRTIO_GPU_CMD_RESOURCE_UNREF:
+ accel_cmd_resource_unref(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_SUBMIT_3D:
+ accel_cmd_submit(g, cmd);
+ break;
+ default:
+ cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+ break;
+ }
+
+ if (cmd->finished) {
+ return;
+ }
+
+ if (cmd->error) {
+ virtio_gpu_ctrl_response_nodata(g, cmd, cmd->error);
+ return;
+ }
+
+ if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE) {
+ int ret;
+
+ ret = vaccel_submit_fence(g, cmd->cmd_hdr.ctx_id, 0,
+ cmd->cmd_hdr.ring_idx, cmd->cmd_hdr.fence_id);
+ if (ret) {
+ /*
+ * On success the command is left unfinished so the base cmdq loop
+ * queues it on g->fenceq and the fence-polling thread completes it
+ * later. If submission fails no completion is ever produced, so
+ * respond now (which sets cmd->finished) to avoid stranding the
+ * command on fenceq and leaking g->inflight.
+ */
+ qemu_log_mask(LOG_GUEST_ERROR,
+ "%s: fence submit failed (ctx %u, ring %u, fence %"
+ PRIu64 "): %s\n", __func__, cmd->cmd_hdr.ctx_id,
+ cmd->cmd_hdr.ring_idx, cmd->cmd_hdr.fence_id,
+ strerror(-ret));
+ virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_ERR_UNSPEC);
+ }
+ return;
+ }
+ virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
+}
+
+/*
+ * Max commands processed per ctrl_bh run before yielding. Draining the whole
+ * queue in one BH can starve other devices' BHs (notably a co-located
+ * virtio-gpu's rendering), so after this many commands we reschedule ctrl_bh
+ * and return, letting the main loop service other work before we resume
+ * draining.
+ */
+#define VIRTIO_ACCEL_CTRL_BUDGET 16
+
+static void virtio_accel_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
+{
+ VirtIOGPU *g = VIRTIO_GPU(vdev);
+ struct virtio_gpu_ctrl_command *cmd;
+ unsigned int processed = 0;
+
+ if (!virtio_queue_ready(vq)) {
+ return;
+ }
+
+ cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
+ while (cmd) {
+ cmd->vq = vq;
+ cmd->error = 0;
+ cmd->finished = false;
+ QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
+ virtio_gpu_process_cmdq(g);
+
+ if (++processed >= VIRTIO_ACCEL_CTRL_BUDGET) {
+ /*
+ * Yield: reschedule ourselves so pending BHs (e.g. virtio-gpu's
+ * ctrl_bh) get to run, then resume draining the queue on the next
+ * ctrl_bh invocation.
+ */
+ qemu_bh_schedule(g->ctrl_bh);
+ break;
+ }
+ cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
+ }
+}
+
+static const Property virtio_accel_properties[] = {
+ DEFINE_PROP_STRING("accel-node", VirtIOAccel,
+ accel_node),
+};
+
+static void virtio_accel_class_init(ObjectClass *klass, const void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+ VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass);
+
+ vgc->handle_ctrl = virtio_accel_handle_ctrl;
+ vgc->process_cmd = virtio_accel_process_cmd;
+ vdc->realize = virtio_accel_realize;
+ vdc->unrealize = virtio_accel_unrealize;
+ device_class_set_props(dc, virtio_accel_properties);
+}
+
+static const TypeInfo virtio_accel_info[] = {
+ {
+ .name = TYPE_VIRTIO_ACCEL,
+ .parent = TYPE_VIRTIO_GPU,
+ .instance_size = sizeof(VirtIOAccel),
+ .class_init = virtio_accel_class_init,
+ },
+};
+
+DEFINE_TYPES(virtio_accel_info)
+
+module_obj(TYPE_VIRTIO_ACCEL);
+module_kconfig(VIRTIO_GPU);
+module_dep("hw-display-virtio-gpu");
diff --git a/hw/display/virtio-gpu-base.c b/hw/display/virtio-gpu-base.c
index 270fbaae102..0c3a87aa312 100644
--- a/hw/display/virtio-gpu-base.c
+++ b/hw/display/virtio-gpu-base.c
@@ -261,7 +261,8 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t features,
VirtIOGPUBase *g = VIRTIO_GPU_BASE(vdev);
if (virtio_gpu_virgl_enabled(g->conf) ||
- virtio_gpu_rutabaga_enabled(g->conf)) {
+ virtio_gpu_rutabaga_enabled(g->conf) ||
+ virtio_accel_enabled(g->conf)) {
features |= (1 << VIRTIO_GPU_F_VIRGL);
}
if (virtio_gpu_edid_enabled(g->conf)) {
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index d248e50abcb..fe177af97fb 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -1740,6 +1740,7 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
if (virtio_gpu_blob_enabled(g->parent_obj.conf)) {
if (!virtio_gpu_rutabaga_enabled(g->parent_obj.conf) &&
!virtio_gpu_virgl_enabled(g->parent_obj.conf) &&
+ !virtio_accel_enabled(g->parent_obj.conf) &&
!virtio_gpu_have_udmabuf()) {
error_setg(errp, "need rutabaga or udmabuf for blob resources");
return;
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 631dfc63a4d..f39103ec5a2 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -43,6 +43,9 @@ OBJECT_DECLARE_SIMPLE_TYPE(VhostUserGPU, VHOST_USER_GPU)
#define TYPE_VIRTIO_GPU_RUTABAGA "virtio-gpu-rutabaga-device"
OBJECT_DECLARE_SIMPLE_TYPE(VirtIOGPURutabaga, VIRTIO_GPU_RUTABAGA)
+#define TYPE_VIRTIO_ACCEL "virtio-accel-device"
+OBJECT_DECLARE_SIMPLE_TYPE(VirtIOAccel, VIRTIO_ACCEL)
+
struct virtio_gpu_simple_resource {
uint32_t resource_id;
uint32_t width;
@@ -112,6 +115,7 @@ enum virtio_gpu_base_conf_flags {
VIRTIO_GPU_FLAG_VENUS_ENABLED,
VIRTIO_GPU_FLAG_RESOURCE_UUID_ENABLED,
VIRTIO_GPU_FLAG_DRM_ENABLED,
+ VIRTIO_GPU_FLAG_ACCEL_ENABLED,
};
#define virtio_gpu_virgl_enabled(_cfg) \
@@ -136,6 +140,8 @@ enum virtio_gpu_base_conf_flags {
(_cfg.flags & (1 << VIRTIO_GPU_FLAG_VENUS_ENABLED))
#define virtio_gpu_drm_enabled(_cfg) \
(_cfg.flags & (1 << VIRTIO_GPU_FLAG_DRM_ENABLED))
+#define virtio_accel_enabled(_cfg) \
+ (_cfg.flags & (1 << VIRTIO_GPU_FLAG_ACCEL_ENABLED))
struct virtio_gpu_base_conf {
uint32_t max_outputs;
@@ -307,6 +313,30 @@ struct VirtIOGPURutabaga {
*/
#define VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE (4 * MiB)
+struct accel_context_fence {
+ uint32_t ctx_id;
+ uint32_t ring_idx;
+ uint64_t fence_id;
+ QSLIST_ENTRY(accel_context_fence) next;
+};
+
+struct VirtIOAccel {
+ VirtIOGPU parent_obj;
+ char *accel_node;
+
+ /*
+ * Fence completions are delivered on the vaccel fence-polling thread,
+ * which is not a QEMU thread and must not touch g->fenceq / the virtqueue
+ * (or take the BQL) directly: ctx teardown joins that thread while holding
+ * the BQL, so grabbing the BQL from the callback would deadlock. The
+ * callback instead pushes onto this lock-free list and schedules fence_bh,
+ * which drains it on the main loop under the BQL. Mirrors virgl's
+ * async_fenceq/async_fence_bh.
+ */
+ QEMUBH *fence_bh;
+ QSLIST_HEAD(, accel_context_fence) async_fenceq;
+};
+
#define VIRTIO_GPU_FILL_CMD(out) do { \
size_t virtiogpufillcmd_s_ = \
iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
diff --git a/meson.build b/meson.build
index f8dd3926dd8..bd97e2bbcf4 100644
--- a/meson.build
+++ b/meson.build
@@ -1437,6 +1437,12 @@ if not get_option('rutabaga_gfx').auto() or have_system or have_vhost_user_gpu
method: 'pkg-config',
required: get_option('rutabaga_gfx'))
endif
+drm_accel = not_found
+if not get_option('drm_accel').auto() or have_system
+ drm_accel = dependency('vxdna',
+ method: 'pkg-config',
+ required: get_option('drm_accel'))
+endif
blkio = not_found
if not get_option('blkio').auto() or have_block
blkio = dependency('blkio',
@@ -4959,6 +4965,7 @@ summary_info += {'SDL image support': sdl_image}
summary_info += {'GTK support': gtk}
summary_info += {'pixman': pixman}
summary_info += {'VTE support': vte}
+summary_info += {'DRM based accelerator support': drm_accel}
summary_info += {'PNG support': png}
summary_info += {'VNC support': vnc}
if vnc.found()
diff --git a/meson_options.txt b/meson_options.txt
index ada233473ea..96266d765c8 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -256,6 +256,8 @@ option('virglrenderer', type : 'feature', value : 'auto',
description: 'virgl rendering support')
option('rutabaga_gfx', type : 'feature', value : 'auto',
description: 'rutabaga_gfx support')
+option('drm_accel', type : 'feature', value : 'auto',
+ description: 'DRM accelerator support')
option('png', type : 'feature', value : 'auto',
description: 'PNG support with libpng')
option('vnc', type : 'feature', value : 'auto',
diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh
index 0f8b56bd7f8..3c27c568e7f 100644
--- a/scripts/meson-buildoptions.sh
+++ b/scripts/meson-buildoptions.sh
@@ -121,6 +121,7 @@ meson_options_help() {
printf "%s\n" ' dbus-display -display dbus support'
printf "%s\n" ' dmg dmg image format support'
printf "%s\n" ' docs Documentations build support'
+ printf "%s\n" ' drm-accel DRM accelerator support'
printf "%s\n" ' dsound DirectSound sound support'
printf "%s\n" ' fuse FUSE block device export'
printf "%s\n" ' fuse-lseek SEEK_HOLE/SEEK_DATA support for FUSE exports'
@@ -323,6 +324,8 @@ _meson_option_parse() {
--docdir=*) quote_sh "-Ddocdir=$2" ;;
--enable-docs) printf "%s" -Ddocs=enabled ;;
--disable-docs) printf "%s" -Ddocs=disabled ;;
+ --enable-drm-accel) printf "%s" -Ddrm_accel=enabled ;;
+ --disable-drm-accel) printf "%s" -Ddrm_accel=disabled ;;
--enable-dsound) printf "%s" -Ddsound=enabled ;;
--disable-dsound) printf "%s" -Ddsound=disabled ;;
--enable-fdt) printf "%s" -Dfdt=enabled ;;
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [RFC PATCH 2/2] hw/display/virtio-accel: add host-memory blob mapping
2026-12-02 7:44 [RFC PATCH 0/2] Introduce virtio-accel device for NPU virtualization Wendy Liang
2026-12-02 7:44 ` [RFC PATCH 1/2] hw/display/virtio-accel: add DRM-based accelerator device Wendy Liang
@ 2026-12-02 7:44 ` Wendy Liang
1 sibling, 0 replies; 3+ messages in thread
From: Wendy Liang @ 2026-12-02 7:44 UTC (permalink / raw)
To: qemu-devel
Cc: Wendy Liang, Akihiko Odaki, Alex Bennée,
Daniel P. Berrangé, Dmitry Osipenko, Lizhi Hou,
Marc-André Lureau, Max Zhen, Michael S. Tsirkin,
Paolo Bonzini, Pierrick Bouvier
Implement host memory resource blob map and unmap. The virtio-accel device
retrieves a GEM buffer with libvxdna, maps it into QEMU's address space, and
then wraps it in a memory region and puts it in virtio-accel's hostmem BAR
so that the guest can access the memory directly via the virtio-gpu blob
mechanism. The unmap blob operation removes the memory region and unmaps the
buffer.
To use host-memory, add "hostmem=" to the device options:
-device virtio-accel-pci,accel-node=accel0,hostmem=8G
Signed-off-by: Wendy Liang <wendy.liang@amd.com>
---
hw/display/trace-events | 2 +
hw/display/virtio-accel.c | 185 +++++++++++++++++++++++++++++++++++++-
2 files changed, 186 insertions(+), 1 deletion(-)
diff --git a/hw/display/trace-events b/hw/display/trace-events
index 7123b7c2c3a..3293795539b 100644
--- a/hw/display/trace-events
+++ b/hw/display/trace-events
@@ -65,6 +65,8 @@ virtio_accel_cmd_ctx_create(uint32_t ctx_id, uint32_t nlen, const char *name, co
virtio_accel_cmd_ctx_destroy(uint32_t ctx_id) "ctx=0x%x"
virtio_accel_cmd_resource_create_blob(uint32_t resource_id, uint32_t ctx_id, uint64_t blob_id, uint32_t blob_flags, uint32_t blob_mem, uint64_t size) "resource_id=0x%x ctx_id=0x%x blob_id=0x%" PRIx64 " blob_flags=0x%x blob_mem=0x%x size=0x%" PRIx64
virtio_accel_create_blob(uint32_t resource_id, uint32_t blob_mem, uint32_t nr_entries, uint64_t size, uint32_t ctx_id) "resource_id=0x%x blob_mem=0x%x nr_entries=%u size=0x%" PRIx64 " ctx=0x%x"
+virtio_accel_cmd_resource_map_blob(uint32_t resource_id, uint64_t offset, uint64_t size) "resource_id=0x%x offset=0x%" PRIx64 " size=0x%" PRIx64
+virtio_accel_cmd_resource_unmap_blob(uint32_t resource_id) "resource_id=0x%x"
# qxl.c
qxl_io_write_vga(int qid, const char *mode, uint32_t addr, uint32_t val) "%d %s addr=%u val=%u"
diff --git a/hw/display/virtio-accel.c b/hw/display/virtio-accel.c
index 3de2ada5ca9..65a4d415b94 100644
--- a/hw/display/virtio-accel.c
+++ b/hw/display/virtio-accel.c
@@ -20,6 +20,30 @@
#include <vaccel.h>
+struct accel_resource {
+ struct virtio_gpu_simple_resource base;
+ MemoryRegion *region; /* For host memory mapping */
+};
+
+static inline struct accel_resource *
+to_accel_res(struct virtio_gpu_simple_resource *res)
+{
+ return container_of(res, struct accel_resource, base);
+}
+
+static struct accel_resource *
+accel_find_resource(VirtIOGPU *g, uint32_t resource_id)
+{
+ struct virtio_gpu_simple_resource *res;
+
+ res = virtio_gpu_find_resource(g, resource_id);
+ if (!res) {
+ return NULL;
+ }
+
+ return to_accel_res(res);
+}
+
static int accel_get_drm_fd(void *opaque)
{
VirtIOGPU *g = opaque;
@@ -268,7 +292,7 @@ accel_cmd_resource_create_blob(VirtIOGPU *g,
return;
}
- res = g_new0(struct virtio_gpu_simple_resource, 1);
+ res = &((struct accel_resource *)g_new0(struct accel_resource, 1))->base;
res->resource_id = cblob.resource_id;
res->blob_size = cblob.size;
@@ -320,11 +344,153 @@ cleanup_mapping:
return;
}
+/*
+ * Tear down a blob's host mapping. Disabling and removing the MemoryRegion are
+ * batched into one memory transaction, so a single transaction commit drops the
+ * KVM memslot (one EPT/NPT flush instead of two). The commit still completes
+ * before vaccel_resource_unmap() munmaps the backing, so a concurrent vCPU can
+ * no longer reach it once the backing is freed. Returns the
+ * vaccel_resource_unmap() result.
+ */
+static int accel_resource_unmap_region(VirtIOGPU *g,
+ struct accel_resource *vres)
+{
+ VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+ MemoryRegion *mr = vres->region;
+
+ memory_region_transaction_begin();
+ memory_region_set_enabled(mr, false);
+ memory_region_del_subregion(&b->hostmem, mr);
+ memory_region_transaction_commit();
+ object_unparent(OBJECT(mr));
+ vres->region = NULL;
+
+ return vaccel_resource_unmap(g, vres->base.resource_id);
+}
+
+static void
+accel_cmd_resource_map_blob(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
+{
+ struct virtio_gpu_resource_map_blob mblob;
+ struct accel_resource *vres;
+ MemoryRegion *mr;
+ int ret;
+ void *data;
+ uint64_t size;
+ struct virtio_gpu_resp_map_info resp;
+ VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+
+ VIRTIO_GPU_FILL_CMD(mblob);
+ virtio_gpu_map_blob_bswap(&mblob);
+
+ if (mblob.resource_id == 0) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
+ __func__);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ if (!virtio_gpu_hostmem_enabled(b->conf)) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: hostmem disabled\n", __func__);
+ cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+ return;
+ }
+
+ vres = accel_find_resource(g, mblob.resource_id);
+ if (!vres) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource does not exist %d\n",
+ __func__, mblob.resource_id);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+ if (vres->region) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already mapped %d\n",
+ __func__, mblob.resource_id);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ ret = vaccel_resource_map(g, vres->base.resource_id, &data, &size);
+ if (ret) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource map error: %s\n",
+ __func__, strerror(-ret));
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ if (size > b->conf.hostmem || mblob.offset > b->conf.hostmem - size) {
+ qemu_log_mask(LOG_GUEST_ERROR,
+ "%s: map offset 0x%" PRIx64 " size 0x%" PRIx64
+ " exceeds hostmem window 0x%" PRIx64 "\n",
+ __func__, mblob.offset, size, b->conf.hostmem);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+ vaccel_resource_unmap(g, vres->base.resource_id);
+ return;
+ }
+
+ vres->region = MEMORY_REGION(object_new(TYPE_MEMORY_REGION));
+ mr = vres->region;
+
+ memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+ memory_region_add_subregion(&b->hostmem, mblob.offset, mr);
+
+ memset(&resp, 0, sizeof(resp));
+ resp.hdr.type = VIRTIO_GPU_RESP_OK_MAP_INFO;
+ vaccel_resource_get_map_info(g, mblob.resource_id, &resp.map_info);
+ trace_virtio_accel_cmd_resource_map_blob(mblob.resource_id, mblob.offset,
+ size);
+ virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
+}
+
+static void
+accel_cmd_resource_unmap_blob(VirtIOGPU *g,
+ struct virtio_gpu_ctrl_command *cmd)
+{
+ struct accel_resource *vres;
+ struct virtio_gpu_resource_unmap_blob ublob;
+ int ret;
+
+ VIRTIO_GPU_FILL_CMD(ublob);
+ virtio_gpu_unmap_blob_bswap(&ublob);
+
+ if (ublob.resource_id == 0) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
+ __func__);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ vres = accel_find_resource(g, ublob.resource_id);
+ if (!vres) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource does not exist %d\n",
+ __func__, ublob.resource_id);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ if (!vres->region) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already unmapped %d\n",
+ __func__, ublob.resource_id);
+ cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+ return;
+ }
+
+ trace_virtio_accel_cmd_resource_unmap_blob(vres->base.resource_id);
+ ret = accel_resource_unmap_region(g, vres);
+ if (ret) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: resource unmap error: %s\n",
+ __func__, strerror(-ret));
+ cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+ return;
+ }
+}
+
static void accel_cmd_resource_unref(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
struct virtio_gpu_resource_unref unref;
struct virtio_gpu_simple_resource *res;
+ struct accel_resource *vres;
struct iovec *res_iovs = NULL;
uint32_t num_iovs = 0;
@@ -339,6 +505,17 @@ static void accel_cmd_resource_unref(VirtIOGPU *g,
return;
}
+ /*
+ * Do not trust the guest to unmap before unref: a still-mapped blob leaves
+ * a MemoryRegion in the hostmem window backed by memory that the destroy
+ * below frees, which the guest could then reach through its BAR. Tear the
+ * mapping down first.
+ */
+ vres = to_accel_res(res);
+ if (vres->region) {
+ accel_resource_unmap_region(g, vres);
+ }
+
vaccel_detach_resource_blob(g, unref.resource_id,
&res_iovs,
&num_iovs);
@@ -426,6 +603,12 @@ virtio_accel_process_cmd(VirtIOGPU *g,
case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB:
accel_cmd_resource_create_blob(g, cmd);
break;
+ case VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB:
+ accel_cmd_resource_map_blob(g, cmd);
+ break;
+ case VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB:
+ accel_cmd_resource_unmap_blob(g, cmd);
+ break;
case VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE:
break;
case VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE:
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread