Skip to contentSkip to conversation

Add VirtIO GPU and Passthrough GPU support on Xen

RFC PATCH Superseded Huang Rui 4 participants 30 messages Mar 12, 2023 – Apr 3, 2023 Arm/Amlogic Meson SoC support

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Huang Rui

author
#0 lore raw

cover Add VirtIO GPU and Passthrough GPU support on Xen

Hi all,

We are working to introduce VirtIO GPU and Passthrough GPU support on Xen.

https://lists.xenproject.org/archives/html/xen-devel/2023-03/msg00714.html

Currently, only virgl for VirtIO GPU and Passthrough GPU on PV dom0 can works on Xen. These series introduce more graphic support for Xen on QEMU part.

1) Add Venus with QEMU for VirtIO GPU on Xen

Antonio Caggiano made the venus with QEMU on KVM platform below, patch 1 to 11 are inherited from his original work to support the features of context init, hostmem, resource uuid, and blob resources for Venus. We have rebase the support to the latest version.
https://www.collabora.com/news-and-blog/blog/2021/11/26/venus-on-qemu-enabling-new-virtual-vulkan-driver/

But on Xen, it still needs to add "-mem-path" function in the QEMU. Because it requires the memory-backend-memfd support. Please check patch 12 to 16.

With the Venus supported on Xen, we can enable Zink + Venus for OpenGL rendering on guest domU as well.

2) Add Video hardware accelerate support for virgl

Please check patch 17, that adds get_drm_fd callback to let guest know hardware accel codec is used.

3) Enable Passthrough GPU on Xen PVH dom0 in QEMU

Please check patch 18, that translate host irq to gsi for PCIe Passthrough on PVH dom0.

Below are the screenshot of these functions, please take a look.

Venus: https://drive.google.com/file/d/1_lPq6DMwHu1JQv7LUUVRx31dBj0HJYcL/view?usp=share_link
Zink: https://drive.google.com/file/d/1FxLmKu6X7uJOxx1ZzwOm1yA6IL5WMGzd/view?usp=share_link
Passthrough GPU: https://drive.google.com/file/d/17onr5gvDK8KM_LniHTSQEI2hGJZlI09L/view?usp=share_link

Repositories
Kernel: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=upstream-fox-xen
Xen: https://gitlab.com/huangrui123/xen/-/commits/upstream-for-xen
QEMU: https://gitlab.com/huangrui123/qemu/-/commits/upstream-for-xen
Mesa: https://gitlab.freedesktop.org/rui/mesa/-/commits/upstream-for-xen
Virglrenderer: https://gitlab.freedesktop.org/rui/virglrenderer/-/commits/upstream-for-xen

Thanks,
Ray

Antonio Caggiano (8):
virtio-gpu: Handle resource blob commands
virtio-gpu: CONTEXT_INIT feature
virtio-gpu: Unrealize
virtio-gpu: Resource UUID
virtio-gpu: Support Venus capset
virtio-gpu: Initialize Venus
meson: Enable virglrenderer unstable APIs
virtio-gpu: Handle set scanout blob command

Chen Jiqian (1):
xen: translate irq of host pci device to gsi

Dr. David Alan Gilbert (1):
virtio: Add shared memory capability

Gerd Hoffmann (1):
virtio-gpu: hostmem

Honglei Huang (2):
virtio-gpu: fix hw-display-virtio-gpu.so undefined symbol
virtio_gpu_virgl_resource_unmap
virtio-gpu: Add video hardware accelerate support for virgl

Huang Rui (4):
softmmu: Fix the size to map cache with xen for host virtual address hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function
softmmu: Add ram block check to map the xen ram memory
softmmu: Enable qemu ram allocation with fd for Xen

Robert Beckett (1):
virtio-gpu: make blob scanout use dmabuf fd

 hw/display/meson.build                      |   2 +-
 hw/display/trace-events                     |   1 +
 hw/display/virtio-gpu-base.c                |   7 +-
 hw/display/virtio-gpu-pci.c                 |  14 +
 hw/display/virtio-gpu-virgl.c               | 326 +++++++++++++++++++-
 hw/display/virtio-gpu.c                     | 101 +++++-
 hw/display/virtio-vga.c                     |  33 +-
 hw/i386/xen/xen-hvm.c                       |  15 +
 hw/virtio/virtio-pci.c                      |  18 ++
 hw/xen/xen-host-pci-device.c                |   3 +-
 include/hw/virtio/virtio-gpu-bswap.h        |  18 ++
 include/hw/virtio/virtio-gpu.h              |  28 ++
 include/hw/virtio/virtio-pci.h              |   4 +
 include/hw/xen/xen.h                        |   1 +
 include/standard-headers/linux/virtio_gpu.h |   2 +
 meson.build                                 |  11 +
 softmmu/physmem.c                           |  12 +-
 17 files changed, 559 insertions(+), 37 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

RFC PATCH 1/18 virtio: Add shared memory capability

From: "Dr. David Alan Gilbert" <[email protected]>

Define a new capability type 'VIRTIO_PCI_CAP_SHARED_MEMORY_CFG' to allow defining shared memory regions with sizes and offsets of 2^32 and more. Multiple instances of the capability are allowed and distinguished by a device-specific 'id'.

---
 hw/virtio/virtio-pci.c         | 18 ++++++++++++++++++
 include/hw/virtio/virtio-pci.h |  4 ++++
 2 files changed, 22 insertions(+)
hw/virtio/virtio-pci.c +18 -0
index a1c9dfa7bb..ae4c29cb96 100644
@@ -1191,6 +1191,24 @@ static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
     return offset;
 }
 
+int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy,
+                           uint8_t bar, uint64_t offset, uint64_t length,
+                           uint8_t id)
+{
+    struct virtio_pci_cap64 cap = {
+        .cap.cap_len = sizeof cap,
+        .cap.cfg_type = VIRTIO_PCI_CAP_SHARED_MEMORY_CFG,
+    };
+
+    cap.cap.bar = bar;
+    cap.cap.length = cpu_to_le32(length);
+    cap.length_hi = cpu_to_le32(length >> 32);
+    cap.cap.offset = cpu_to_le32(offset);
+    cap.offset_hi = cpu_to_le32(offset >> 32);
+    cap.cap.id = id;
+    return virtio_pci_add_mem_cap(proxy, &cap.cap);
+}
+
 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
                                        unsigned size)
 {
include/hw/virtio/virtio-pci.h +4 -0
index 938799e8f6..e67fe422a1 100644
@@ -257,4 +257,8 @@ void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
  */
 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues);
 
+int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy,
+			   uint8_t bar, uint64_t offset, uint64_t length,
+			   uint8_t id);
+
 #endif
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC QEMU PATCH 01/18] virtio: Add shared memory capability

Quoted 27 lines · Huang Rui

From: "Dr. David Alan Gilbert" <[email protected]>

Define a new capability type 'VIRTIO_PCI_CAP_SHARED_MEMORY_CFG' to allow defining shared memory regions with sizes and offsets of 2^32 and more. Multiple instances of the capability are allowed and distinguished by a device-specific 'id'.

---
  hw/virtio/virtio-pci.c         | 18 ++++++++++++++++++
  include/hw/virtio/virtio-pci.h |  4 ++++
  2 files changed, 22 insertions(+)
hw/virtio/virtio-pci.c +5 -0
index a1c9dfa7bb..ae4c29cb96 100644
@@ -1191,6 +1191,24 @@ static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
      return offset;
  }
 
+int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy,
+                           uint8_t bar, uint64_t offset, uint64_t length,
+                           uint8_t id)
+{
+    struct virtio_pci_cap64 cap = {

hw/virtio/virtio-pci.c static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy, · line 1208

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+        .cap.cap_len = sizeof cap,
+        .cap.cfg_type = VIRTIO_PCI_CAP_SHARED_MEMORY_CFG,
+    };
+
+    cap.cap.bar = bar;
+    cap.cap.length = cpu_to_le32(length);
+    cap.length_hi = cpu_to_le32(length >> 32);
+    cap.cap.offset = cpu_to_le32(offset);
+    cap.offset_hi = cpu_to_le32(offset >> 32);
+    cap.cap.id = id;

Why initializing 'cap' using 2 different coding styles?

Quoted 2 lines
hw/virtio/virtio-pci.c +2 -0
+    return virtio_pci_add_mem_cap(proxy, &cap.cap);
+}
author
#3 lore raw

RFC PATCH 2/18 virtio-gpu: hostmem

In reply to Huang Rui

From: Gerd Hoffmann <[email protected]>

Use VIRTIO_GPU_SHM_ID_HOST_VISIBLE as id for virtio-gpu.

---
 hw/display/virtio-gpu-pci.c    | 14 ++++++++++++++
 hw/display/virtio-gpu.c        |  1 +
 hw/display/virtio-vga.c        | 33 ++++++++++++++++++++++++---------
 include/hw/virtio/virtio-gpu.h |  5 +++++
 4 files changed, 44 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-pci.c +14 -0
index 93f214ff58..da6a99f038 100644
@@ -33,6 +33,20 @@ static void virtio_gpu_pci_base_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     DeviceState *vdev = DEVICE(g);
     int i;
 
+    if (virtio_gpu_hostmem_enabled(g->conf)) {
+        vpci_dev->msix_bar_idx = 1;
+        vpci_dev->modern_mem_bar_idx = 2;
+        memory_region_init(&g->hostmem, OBJECT(g), "virtio-gpu-hostmem",
+                           g->conf.hostmem);
+        pci_register_bar(&vpci_dev->pci_dev, 4,
+                         PCI_BASE_ADDRESS_SPACE_MEMORY |
+                         PCI_BASE_ADDRESS_MEM_PREFETCH |
+                         PCI_BASE_ADDRESS_MEM_TYPE_64,
+                         &g->hostmem);
+        virtio_pci_add_shm_cap(vpci_dev, 4, 0, g->conf.hostmem,
+                               VIRTIO_GPU_SHM_ID_HOST_VISIBLE);
+    }
+
     virtio_pci_force_virtio_1(vpci_dev);
     if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) {
         return;
hw/display/virtio-gpu.c +1 -0
index 5e15c79b94..7b592f998d 100644
@@ -1425,6 +1425,7 @@ static Property virtio_gpu_properties[] = {
                      256 * MiB),
     DEFINE_PROP_BIT("blob", VirtIOGPU, parent_obj.conf.flags,
                     VIRTIO_GPU_FLAG_BLOB_ENABLED, false),
+    DEFINE_PROP_SIZE("hostmem", VirtIOGPU, parent_obj.conf.hostmem, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/display/virtio-vga.c +24 -9
index 4dcb34c4a7..aa8d1ab993 100644
@@ -115,17 +115,32 @@ static void virtio_vga_base_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     pci_register_bar(&vpci_dev->pci_dev, 0,
                      PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
 
-    /*
-     * Configure virtio bar and regions
-     *
-     * We use bar #2 for the mmio regions, to be compatible with stdvga.
-     * virtio regions are moved to the end of bar #2, to make room for
-     * the stdvga mmio registers at the start of bar #2.
-     */
-    vpci_dev->modern_mem_bar_idx = 2;
-    vpci_dev->msix_bar_idx = 4;
     vpci_dev->modern_io_bar_idx = 5;
 
+    if (!virtio_gpu_hostmem_enabled(g->conf)) {
+        /*
+         * Configure virtio bar and regions
+         *
+         * We use bar #2 for the mmio regions, to be compatible with stdvga.
+         * virtio regions are moved to the end of bar #2, to make room for
+         * the stdvga mmio registers at the start of bar #2.
+         */
+        vpci_dev->modern_mem_bar_idx = 2;
+        vpci_dev->msix_bar_idx = 4;
+    } else {
+        vpci_dev->msix_bar_idx = 1;
+        vpci_dev->modern_mem_bar_idx = 2;
+        memory_region_init(&g->hostmem, OBJECT(g), "virtio-gpu-hostmem",
+                           g->conf.hostmem);
+        pci_register_bar(&vpci_dev->pci_dev, 4,
+                         PCI_BASE_ADDRESS_SPACE_MEMORY |
+                         PCI_BASE_ADDRESS_MEM_PREFETCH |
+                         PCI_BASE_ADDRESS_MEM_TYPE_64,
+                         &g->hostmem);
+        virtio_pci_add_shm_cap(vpci_dev, 4, 0, g->conf.hostmem,
+                               VIRTIO_GPU_SHM_ID_HOST_VISIBLE);
+    }
+
     if (!(vpci_dev->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ)) {
         /*
          * with page-per-vq=off there is no padding space we can use
include/hw/virtio/virtio-gpu.h +5 -0
index 2e28507efe..eafce75b04 100644
@@ -102,12 +102,15 @@ enum virtio_gpu_base_conf_flags {
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED))
 #define virtio_gpu_blob_enabled(_cfg) \
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED))
+#define virtio_gpu_hostmem_enabled(_cfg) \
+    (_cfg.hostmem > 0)
 
 struct virtio_gpu_base_conf {
     uint32_t max_outputs;
     uint32_t flags;
     uint32_t xres;
     uint32_t yres;
+    uint64_t hostmem;
 };
 
 struct virtio_gpu_ctrl_command {
@@ -131,6 +134,8 @@ struct VirtIOGPUBase {
     int renderer_blocked;
     int enable;
 
+    MemoryRegion hostmem;
+
     struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUTS];
 
     int enabled_output_bitmask;
Signature · 1 line
2.25.1
author
#4 lore raw

RFC PATCH 3/18 virtio-gpu: Handle resource blob commands

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Support BLOB resources creation, mapping and unmapping by calling the new stable virglrenderer 0.10 interface. Only enabled when available and via the blob config. E.g. -device virtio-vga-gl,blob=true

---
 hw/display/virtio-gpu-virgl.c        | 168 +++++++++++++++++++++++++++
 hw/display/virtio-gpu.c              |  12 +-
 include/hw/virtio/virtio-gpu-bswap.h |  18 +++
 include/hw/virtio/virtio-gpu.h       |   8 ++
 meson.build                          |   4 +
 5 files changed, 206 insertions(+), 4 deletions(-)
hw/display/virtio-gpu-virgl.c +168 -0
index 73cb92c8d5..9b5e3dc782 100644
@@ -16,6 +16,8 @@
 #include "trace.h"
 #include "hw/virtio/virtio.h"
 #include "hw/virtio/virtio-gpu.h"
+#include "hw/virtio/virtio-gpu-bswap.h"
+#include "hw/virtio/virtio-iommu.h"
 
 #include <virglrenderer.h>
 
@@ -398,6 +400,161 @@ static void virgl_cmd_get_capset(VirtIOGPU *g,
     g_free(resp);
 }
 
+#ifdef HAVE_VIRGL_RESOURCE_BLOB
+
+static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
+                                           struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_resource_create_blob cblob;
+    int ret;
+
+    VIRTIO_GPU_FILL_CMD(cblob);
+    virtio_gpu_create_blob_bswap(&cblob);
+    trace_virtio_gpu_cmd_res_create_blob(cblob.resource_id, cblob.size);
+
+    if (cblob.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;
+    }
+
+    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);
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
+
+    res->resource_id = cblob.resource_id;
+    res->blob_size = cblob.size;
+
+    if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
+        ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
+                                            cmd, &res->addrs, &res->iov,
+                                            &res->iov_cnt);
+        if (ret != 0) {
+            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+            return;
+        }
+    }
+
+    if (cblob.blob_mem == VIRTIO_GPU_BLOB_MEM_GUEST) {
+        virtio_gpu_init_udmabuf(res);
+    }
+    const struct virgl_renderer_resource_create_blob_args virgl_args = {
+        .res_handle = cblob.resource_id,
+        .ctx_id = cblob.hdr.ctx_id,
+        .blob_mem = cblob.blob_mem,
+        .blob_id = cblob.blob_id,
+        .blob_flags = cblob.blob_flags,
+        .size = cblob.size,
+        .iovecs = res->iov,
+        .num_iovs = res->iov_cnt,
+    };
+    ret = virgl_renderer_resource_create_blob(&virgl_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;
+    }
+}
+
+static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
+                                        struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_resource_map_blob mblob;
+    int ret;
+    uint64_t size;
+    struct virtio_gpu_resp_map_info resp;
+
+    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;
+    }
+
+    res = virtio_gpu_find_resource(g, mblob.resource_id);
+    if (!res) {
+        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;
+    }
+
+    ret = virgl_renderer_resource_map(res->resource_id, &res->mapped, &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;
+    }
+
+    memory_region_init_ram_device_ptr(&res->region, OBJECT(g), NULL, size, res->mapped);
+    memory_region_add_subregion(&g->parent_obj.hostmem, mblob.offset, &res->region);
+    memory_region_set_enabled(&res->region, true);
+
+    memset(&resp, 0, sizeof(resp));
+    resp.hdr.type = VIRTIO_GPU_RESP_OK_MAP_INFO;
+    virgl_renderer_resource_get_map_info(mblob.resource_id, &resp.map_info);
+    virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
+}
+
+int virtio_gpu_virgl_resource_unmap(VirtIOGPU *g,
+                                    struct virtio_gpu_simple_resource *res)
+{
+    if (!res->mapped) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already unmapped %d\n",
+                      __func__, res->resource_id);
+        return VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+    }
+
+    memory_region_set_enabled(&res->region, false);
+    memory_region_del_subregion(&g->parent_obj.hostmem, &res->region);
+    object_unparent(OBJECT(&res->region));
+
+    res->mapped = NULL;
+    return virgl_renderer_resource_unmap(res->resource_id);
+}
+
+static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
+                                        struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_resource_unmap_blob ublob;
+    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;
+    }
+
+    res = virtio_gpu_find_resource(g, ublob.resource_id);
+    if (!res) {
+        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;
+    }
+
+    virtio_gpu_virgl_resource_unmap(g, res);
+}
+
+#endif /* HAVE_VIRGL_RESOURCE_BLOB */
+
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                       struct virtio_gpu_ctrl_command *cmd)
 {
@@ -464,6 +621,17 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
     case VIRTIO_GPU_CMD_GET_EDID:
         virtio_gpu_get_edid(g, cmd);
         break;
+#ifdef HAVE_VIRGL_RESOURCE_BLOB
+    case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB:
+        virgl_cmd_resource_create_blob(g, cmd);
+        break;
+    case VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB:
+        virgl_cmd_resource_map_blob(g, cmd);
+        break;
+    case VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB:
+        virgl_cmd_resource_unmap_blob(g, cmd);
+        break;
+#endif /* HAVE_VIRGL_RESOURCE_BLOB */
     default:
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         break;
hw/display/virtio-gpu.c +8 -4
index 7b592f998d..95d1347a2e 100644
@@ -33,8 +33,6 @@
 
 #define VIRTIO_GPU_VM_VERSION 1
 
-static struct virtio_gpu_simple_resource*
-virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id);
 static struct virtio_gpu_simple_resource *
 virtio_gpu_find_check_resource(VirtIOGPU *g, uint32_t resource_id,
                                bool require_backing,
@@ -115,7 +113,7 @@ static void update_cursor(VirtIOGPU *g, struct virtio_gpu_update_cursor *cursor)
                   cursor->resource_id ? 1 : 0);
 }
 
-static struct virtio_gpu_simple_resource *
+struct virtio_gpu_simple_resource *
 virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id)
 {
     struct virtio_gpu_simple_resource *res;
@@ -875,6 +873,10 @@ void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g,
 static void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
                                        struct virtio_gpu_simple_resource *res)
 {
+    if (res->mapped) {
+        virtio_gpu_virgl_resource_unmap(g, res);
+    }
+
     virtio_gpu_cleanup_mapping_iov(g, res->iov, res->iov_cnt);
     res->iov = NULL;
     res->iov_cnt = 0;
@@ -1324,10 +1326,12 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
             return;
         }
 
+#ifndef HAVE_VIRGL_RESOURCE_BLOB
         if (virtio_gpu_virgl_enabled(g->parent_obj.conf)) {
-            error_setg(errp, "blobs and virgl are not compatible (yet)");
+            error_setg(errp, "Linked virglrenderer does not support blob resources");
             return;
         }
+#endif
     }
 
     if (!virtio_gpu_base_device_realize(qdev,
include/hw/virtio/virtio-gpu-bswap.h +18 -0
index 9124108485..dd1975e2d4 100644
@@ -63,10 +63,28 @@ virtio_gpu_create_blob_bswap(struct virtio_gpu_resource_create_blob *cblob)
 {
     virtio_gpu_ctrl_hdr_bswap(&cblob->hdr);
     le32_to_cpus(&cblob->resource_id);
+    le32_to_cpus(&cblob->blob_mem);
     le32_to_cpus(&cblob->blob_flags);
+    le32_to_cpus(&cblob->nr_entries);
+    le64_to_cpus(&cblob->blob_id);
     le64_to_cpus(&cblob->size);
 }
 
+static inline void
+virtio_gpu_map_blob_bswap(struct virtio_gpu_resource_map_blob *mblob)
+{
+    virtio_gpu_ctrl_hdr_bswap(&mblob->hdr);
+    le32_to_cpus(&mblob->resource_id);
+    le64_to_cpus(&mblob->offset);
+}
+
+static inline void
+virtio_gpu_unmap_blob_bswap(struct virtio_gpu_resource_unmap_blob *ublob)
+{
+    virtio_gpu_ctrl_hdr_bswap(&ublob->hdr);
+    le32_to_cpus(&ublob->resource_id);
+}
+
 static inline void
 virtio_gpu_scanout_blob_bswap(struct virtio_gpu_set_scanout_blob *ssb)
 {
include/hw/virtio/virtio-gpu.h +8 -0
index eafce75b04..caca834680 100644
@@ -55,6 +55,9 @@ struct virtio_gpu_simple_resource {
     int dmabuf_fd;
     uint8_t *remapped;
 
+    MemoryRegion region;
+    void *mapped;
+
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
 };
 
@@ -245,6 +248,9 @@ void virtio_gpu_base_fill_display_info(VirtIOGPUBase *g,
                         struct virtio_gpu_resp_display_info *dpy_info);
 
 /* virtio-gpu.c */
+struct virtio_gpu_simple_resource *
+virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id);
+
 void virtio_gpu_ctrl_response(VirtIOGPU *g,
                               struct virtio_gpu_ctrl_command *cmd,
                               struct virtio_gpu_ctrl_hdr *resp,
@@ -289,5 +295,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g);
 void virtio_gpu_virgl_reset(VirtIOGPU *g);
 int virtio_gpu_virgl_init(VirtIOGPU *g);
 int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g);
+int virtio_gpu_virgl_resource_unmap(VirtIOGPU *g,
+                                    struct virtio_gpu_simple_resource *res);
 
 #endif
meson.build +4 -0
index 5c6b5a1c75..be74b653b6 100644
@@ -774,6 +774,10 @@ if not get_option('virglrenderer').auto() or have_system or have_vhost_user_gpu
                      method: 'pkg-config',
                      required: get_option('virglrenderer'),
                      kwargs: static_kwargs)
+  config_host_data.set('HAVE_VIRGL_RESOURCE_BLOB',
+		       cc.has_function('virgl_renderer_resource_create_blob',
+				       prefix: '#include <virglrenderer.h>',
+				       dependencies: virgl))
 endif
 blkio = not_found
 if not get_option('blkio').auto() or have_block
Signature · 1 line
2.25.1
author
#5 lore raw

RFC PATCH 4/18 virtio-gpu: CONTEXT_INIT feature

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Create virgl renderer context with flags using context_id when valid. The feature can be enabled via the context_init config option. A warning message will be emitted and the feature will not be used when linking with virglrenderer versions without context_init support.

---
 hw/display/virtio-gpu-base.c   |  3 +++
 hw/display/virtio-gpu-virgl.c  | 16 ++++++++++++++--
 hw/display/virtio-gpu.c        |  2 ++
 include/hw/virtio/virtio-gpu.h |  3 +++
 meson.build                    |  4 ++++
 5 files changed, 26 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-base.c +3 -0
index a29f191aa8..6c5f1f327f 100644
@@ -215,6 +215,9 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t features,
     if (virtio_gpu_blob_enabled(g->conf)) {
         features |= (1 << VIRTIO_GPU_F_RESOURCE_BLOB);
     }
+    if (virtio_gpu_context_init_enabled(g->conf)) {
+        features |= (1 << VIRTIO_GPU_F_CONTEXT_INIT);
+    }
 
     return features;
 }
hw/display/virtio-gpu-virgl.c +14 -2
index 9b5e3dc782..41712b79ee 100644
@@ -99,8 +99,20 @@ static void virgl_cmd_context_create(VirtIOGPU *g,
     trace_virtio_gpu_cmd_ctx_create(cc.hdr.ctx_id,
                                     cc.debug_name);
 
-    virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen,
-                                  cc.debug_name);
+    if (cc.context_init) {
+#ifdef HAVE_VIRGL_CONTEXT_INIT
+        virgl_renderer_context_create_with_flags(cc.hdr.ctx_id,
+                                                 cc.context_init,
+                                                 cc.nlen,
+                                                 cc.debug_name);
+        return;
+#else
+        qemu_log_mask(LOG_UNIMP,
+                      "Linked virglrenderer does not support context-init\n");
+#endif
+    }
+    
+    virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen, cc.debug_name);
 }
 
 static void virgl_cmd_context_destroy(VirtIOGPU *g,
hw/display/virtio-gpu.c +2 -0
index 95d1347a2e..11f3e56013 100644
@@ -1430,6 +1430,8 @@ static Property virtio_gpu_properties[] = {
     DEFINE_PROP_BIT("blob", VirtIOGPU, parent_obj.conf.flags,
                     VIRTIO_GPU_FLAG_BLOB_ENABLED, false),
     DEFINE_PROP_SIZE("hostmem", VirtIOGPU, parent_obj.conf.hostmem, 0),
+    DEFINE_PROP_BIT("context_init", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
include/hw/virtio/virtio-gpu.h +3 -0
index caca834680..d1ae97153f 100644
@@ -93,6 +93,7 @@ enum virtio_gpu_base_conf_flags {
     VIRTIO_GPU_FLAG_EDID_ENABLED,
     VIRTIO_GPU_FLAG_DMABUF_ENABLED,
     VIRTIO_GPU_FLAG_BLOB_ENABLED,
+    VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED,
 };
 
 #define virtio_gpu_virgl_enabled(_cfg) \
@@ -107,6 +108,8 @@ enum virtio_gpu_base_conf_flags {
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED))
 #define virtio_gpu_hostmem_enabled(_cfg) \
     (_cfg.hostmem > 0)
+#define virtio_gpu_context_init_enabled(_cfg) \
+    (_cfg.flags & (1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED))
 
 struct virtio_gpu_base_conf {
     uint32_t max_outputs;
meson.build +4 -0
index be74b653b6..826b3473c4 100644
@@ -778,6 +778,10 @@ if not get_option('virglrenderer').auto() or have_system or have_vhost_user_gpu
 		       cc.has_function('virgl_renderer_resource_create_blob',
 				       prefix: '#include <virglrenderer.h>',
 				       dependencies: virgl))
+  config_host_data.set('HAVE_VIRGL_CONTEXT_INIT',
+                       cc.has_function('virgl_renderer_context_create_with_flags',
+                                       prefix: '#include <virglrenderer.h>',
+                                       dependencies: virgl))
 endif
 blkio = not_found
 if not get_option('blkio').auto() or have_block
Signature · 1 line
2.25.1
#6 lore raw

Re: [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature

Quoted 28 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Create virgl renderer context with flags using context_id when valid. The feature can be enabled via the context_init config option. A warning message will be emitted and the feature will not be used when linking with virglrenderer versions without context_init support.

---
  hw/display/virtio-gpu-base.c   |  3 +++
  hw/display/virtio-gpu-virgl.c  | 16 ++++++++++++++--
  hw/display/virtio-gpu.c        |  2 ++
  include/hw/virtio/virtio-gpu.h |  3 +++
  meson.build                    |  4 ++++
  5 files changed, 26 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -2
index 9b5e3dc782..41712b79ee 100644
@@ -99,8 +99,20 @@ static void virgl_cmd_context_create(VirtIOGPU *g,
      trace_virtio_gpu_cmd_ctx_create(cc.hdr.ctx_id,
                                      cc.debug_name);
 
-    virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen,
-                                  cc.debug_name);

hw/display/virtio-gpu-virgl.c static void virgl_cmd_context_create(VirtIOGPU *g, · line 111

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+    if (cc.context_init) {
+#ifdef HAVE_VIRGL_CONTEXT_INIT
+        virgl_renderer_context_create_with_flags(cc.hdr.ctx_id,
+                                                 cc.context_init,
+                                                 cc.nlen,
+                                                 cc.debug_name);
+        return;
+#else
+        qemu_log_mask(LOG_UNIMP,
+                      "Linked virglrenderer does not support context-init\n");

This is dubious, almost nobody will notice this log.

Can this happen? If so, this void function doesn't create any context...

Quoted 5 lines
hw/display/virtio-gpu-virgl.c +4 -0
+#endif
+    }
+
+    virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen, cc.debug_name);
  }
author
#7 lore raw

RFC PATCH 5/18 virtio-gpu: Unrealize

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Implement an unrealize function for virtio gpu device.

---
 hw/display/virtio-gpu-base.c   |  2 +-
 hw/display/virtio-gpu.c        | 11 +++++++++++
 include/hw/virtio/virtio-gpu.h |  1 +
 3 files changed, 13 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-base.c +1 -1
index 6c5f1f327f..5cb71e71ad 100644
@@ -230,7 +230,7 @@ virtio_gpu_base_set_features(VirtIODevice *vdev, uint64_t features)
     trace_virtio_gpu_features(((features & virgl) == virgl));
 }
 
-static void
+void
 virtio_gpu_base_device_unrealize(DeviceState *qdev)
 {
     VirtIOGPUBase *g = VIRTIO_GPU_BASE(qdev);
hw/display/virtio-gpu.c +11 -0
index 11f3e56013..62239dee0f 100644
@@ -1350,6 +1350,16 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
     QTAILQ_INIT(&g->fenceq);
 }
 
+static void virtio_gpu_device_unrealize(DeviceState *qdev)
+{
+    VirtIOGPU *g = VIRTIO_GPU(qdev);
+
+    qemu_bh_delete(g->cursor_bh);
+    qemu_bh_delete(g->ctrl_bh);
+
+    virtio_gpu_base_device_unrealize(qdev);
+}
+
 void virtio_gpu_reset(VirtIODevice *vdev)
 {
     VirtIOGPU *g = VIRTIO_GPU(vdev);
@@ -1448,6 +1458,7 @@ static void virtio_gpu_class_init(ObjectClass *klass, void *data)
     vgbc->gl_flushed = virtio_gpu_handle_gl_flushed;
 
     vdc->realize = virtio_gpu_device_realize;
+    vdc->unrealize = virtio_gpu_device_unrealize;
     vdc->reset = virtio_gpu_reset;
     vdc->get_config = virtio_gpu_get_config;
     vdc->set_config = virtio_gpu_set_config;
include/hw/virtio/virtio-gpu.h +1 -0
index d1ae97153f..ef02190f97 100644
@@ -246,6 +246,7 @@ bool virtio_gpu_base_device_realize(DeviceState *qdev,
                                     VirtIOHandleOutput ctrl_cb,
                                     VirtIOHandleOutput cursor_cb,
                                     Error **errp);
+void virtio_gpu_base_device_unrealize(DeviceState *qdev);
 void virtio_gpu_base_reset(VirtIOGPUBase *g);
 void virtio_gpu_base_fill_display_info(VirtIOGPUBase *g,
                         struct virtio_gpu_resp_display_info *dpy_info);
Signature · 1 line
2.25.1
author
#8 lore raw

RFC PATCH 6/18 virtio-gpu: Resource UUID

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Enable resource UUID feature and implement command resource assign UUID. This is done by introducing a hash table to map resource IDs to their UUIDs.

---
 hw/display/trace-events        |  1 +
 hw/display/virtio-gpu-base.c   |  2 ++
 hw/display/virtio-gpu-virgl.c  | 11 +++++++++
 hw/display/virtio-gpu.c        | 41 ++++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-gpu.h |  4 ++++
 5 files changed, 59 insertions(+)
hw/display/trace-events +1 -0
index 0c0ffcbe42..6632344322 100644
@@ -41,6 +41,7 @@ virtio_gpu_cmd_res_create_blob(uint32_t res, uint64_t size) "res 0x%x, size %" P
 virtio_gpu_cmd_res_unref(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_back_attach(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_back_detach(uint32_t res) "res 0x%x"
+virtio_gpu_cmd_res_assign_uuid(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_xfer_toh_2d(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_xfer_toh_3d(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_xfer_fromh_3d(uint32_t res) "res 0x%x"
hw/display/virtio-gpu-base.c +2 -0
index 5cb71e71ad..54792aa501 100644
@@ -219,6 +219,8 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t features,
         features |= (1 << VIRTIO_GPU_F_CONTEXT_INIT);
     }
 
+    features |= (1 << VIRTIO_GPU_F_RESOURCE_UUID);
+
     return features;
 }
 
hw/display/virtio-gpu-virgl.c +11 -0
index 41712b79ee..a3c388f907 100644
@@ -45,6 +45,10 @@ static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
     args.nr_samples = 0;
     args.flags = VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP;
     virgl_renderer_resource_create(&args, NULL, 0);
+
+    struct virtio_gpu_simple_resource *res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->resource_id = c2d.resource_id;
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
 static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
@@ -69,6 +73,10 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
     args.nr_samples = c3d.nr_samples;
     args.flags = c3d.flags;
     virgl_renderer_resource_create(&args, NULL, 0);
+
+    struct virtio_gpu_simple_resource *res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->resource_id = c3d.resource_id;
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
 static void virgl_cmd_resource_unref(VirtIOGPU *g,
@@ -621,6 +629,9 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
         /* TODO add security */
         virgl_cmd_ctx_detach_resource(g, cmd);
         break;
+    case VIRTIO_GPU_CMD_RESOURCE_ASSIGN_UUID:
+        virtio_gpu_resource_assign_uuid(g, cmd);
+        break;
     case VIRTIO_GPU_CMD_GET_CAPSET_INFO:
         virgl_cmd_get_capset_info(g, cmd);
         break;
hw/display/virtio-gpu.c +41 -0

71 lines not on this page · raw message

include/hw/virtio/virtio-gpu.h +4 -0
index ef02190f97..0a44aea4ee 100644
@@ -200,6 +200,8 @@ struct VirtIOGPU {
         QTAILQ_HEAD(, VGPUDMABuf) bufs;
         VGPUDMABuf *primary[VIRTIO_GPU_MAX_SCANOUTS];
     } dmabuf;
+
+    GHashTable *resource_uuids;
 };
 
 struct VirtIOGPUClass {
@@ -273,6 +275,8 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
                                   uint32_t *niov);
 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g,
                                     struct iovec *iov, uint32_t count);
+void virtio_gpu_resource_assign_uuid(VirtIOGPU *g,
+                                     struct virtio_gpu_ctrl_command *cmd);
 void virtio_gpu_process_cmdq(VirtIOGPU *g);
 void virtio_gpu_device_realize(DeviceState *qdev, Error **errp);
 void virtio_gpu_reset(VirtIODevice *vdev);
Signature · 1 line
2.25.1
author
#9 lore raw

RFC PATCH 7/18 virtio-gpu: Support Venus capset

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Add support for the Venus capset, which enables Vulkan support through the Venus Vulkan driver for virtio-gpu.

---
 hw/display/virtio-gpu-virgl.c               | 21 +++++++++++++++++----
 include/standard-headers/linux/virtio_gpu.h |  2 ++
 2 files changed, 19 insertions(+), 4 deletions(-)
hw/display/virtio-gpu-virgl.c +17 -4
index a3c388f907..fe03dc916f 100644
@@ -388,6 +388,11 @@ static void virgl_cmd_get_capset_info(VirtIOGPU *g,
         virgl_renderer_get_cap_set(resp.capset_id,
                                    &resp.capset_max_version,
                                    &resp.capset_max_size);
+    } else if (info.capset_index == 2) {
+        resp.capset_id = VIRTIO_GPU_CAPSET_VENUS;
+        virgl_renderer_get_cap_set(resp.capset_id,
+                                   &resp.capset_max_version,
+                                   &resp.capset_max_size);
     } else {
         resp.capset_max_version = 0;
         resp.capset_max_size = 0;
@@ -817,10 +822,18 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 
 int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
 {
-    uint32_t capset2_max_ver, capset2_max_size;
+    uint32_t capset2_max_ver, capset2_max_size, num_capsets;
+    num_capsets = 1;
+
     virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VIRGL2,
-                              &capset2_max_ver,
-                              &capset2_max_size);
+                               &capset2_max_ver,
+                               &capset2_max_size);
+    num_capsets += capset2_max_ver ? 1 : 0;
+
+    virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VENUS,
+                               &capset2_max_ver,
+                               &capset2_max_size);
+    num_capsets += capset2_max_size ? 1 : 0;
 
-    return capset2_max_ver ? 2 : 1;
+    return num_capsets;
 }
include/standard-headers/linux/virtio_gpu.h +2 -0
index 2da48d3d4c..2db643ed8f 100644
@@ -309,6 +309,8 @@ struct virtio_gpu_cmd_submit {
 
 #define VIRTIO_GPU_CAPSET_VIRGL 1
 #define VIRTIO_GPU_CAPSET_VIRGL2 2
+/* 3 is reserved for gfxstream */
+#define VIRTIO_GPU_CAPSET_VENUS 4
 
 /* VIRTIO_GPU_CMD_GET_CAPSET_INFO */
 struct virtio_gpu_get_capset_info {
Signature · 1 line
2.25.1
author
#10 lore raw

RFC PATCH 8/18 virtio-gpu: Initialize Venus

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +4 -0
index fe03dc916f..f5ce206b93 100644
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif
     if (ret != 0) {
         error_report("virgl could not be initialized: %d", ret);
         return ret;
Signature · 1 line
2.25.1
#11 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
Signature · 2 lines
Best regards,
Dmitry
#12 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

Quoted 27 lines · Dmitry Osipenko
Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

Quoted 8 lines

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);

BTW, Alex reviewed the Venus v3 patches a month ago [1] and the review comments need to be addressed. AFAICS, you're actually using the very old Venus patches here that stopped working about a year ago, so again you're using a very outdated virglrenderer version.

Please take it all into account if you'll beat me to posting the next version of Venus patches ;)

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 2 lines
Best regards,
Dmitry
author
#13 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

Quoted 42 lines · Dmitry Osipenko
Quoted 27 lines · Dmitry Osipenko
Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

Quoted 8 lines

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);

BTW, Alex reviewed the Venus v3 patches a month ago [1] and the review comments need to be addressed. AFAICS, you're actually using the very old Venus patches here that stopped working about a year ago, so again you're using a very outdated virglrenderer version.

Quoted 6 lines

Please take it all into account if you'll beat me to posting the next version of Venus patches ;)

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks Dmitry point it out, I will use the latest v3 patches, and try to address the comments in next version. :-)

Thanks,
Ray

author
#14 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

In reply to Dmitry Osipenko
Quoted 27 lines · Dmitry Osipenko
Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

Quoted 7 lines

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);

In fact, we have rebased to the latest virglrenderer:

We check both VIRGL_RENDERER_RENDER_SERVER or VIRGL_RENDERER_VENUS in virglrenderer, alternative of them works.

https://gitlab.freedesktop.org/rui/virglrenderer/-/commit/c1322a8a84379b1ef7939f56c6761b0114716f45

Thanks,
Ray

#15 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

Quoted 34 lines · Huang Rui
Quoted 33 lines · Dmitry Osipenko
Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

Quoted 8 lines
Quoted 1 line
    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);

In fact, we have rebased to the latest virglrenderer:

We check both VIRGL_RENDERER_RENDER_SERVER or VIRGL_RENDERER_VENUS in virglrenderer, alternative of them works.

https://gitlab.freedesktop.org/rui/virglrenderer/-/commit/c1322a8a84379b1ef7939f56c6761b0114716f45

All the extra changes you made to virglrenderer that Qemu depends on need to go upstream. Please open all the relevant merge requests. Thanks!

Signature · 2 lines
Best regards,
Dmitry
author
#16 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

Quoted 39 lines · Dmitry Osipenko
Quoted 38 lines · Huang Rui
Quoted 27 lines · Dmitry Osipenko
Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

Quoted 7 lines

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);

In fact, we have rebased to the latest virglrenderer:

Quoted 8 lines
Quoted 4 lines

We check both VIRGL_RENDERER_RENDER_SERVER or VIRGL_RENDERER_VENUS in virglrenderer, alternative of them works.

https://gitlab.freedesktop.org/rui/virglrenderer/-/commit/c1322a8a84379b1ef7939f56c6761b0114716f45

All the extra changes you made to virglrenderer that Qemu depends on need to go upstream. Please open all the relevant merge requests. Thanks!

Dmitry, sorry to late response, I have created relevant merge requests below:

Virglrenderer:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1068

Mesa:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22108

I'd appreciate any comments. :-)

Thanks,
Ray

#17 lore raw

Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus

Quoted 52 lines · Huang Rui
Quoted 39 lines · Dmitry Osipenko
Quoted 38 lines · Huang Rui
Quoted 27 lines · Dmitry Osipenko
Quoted 14 lines · Huang Rui

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL.

---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index fe03dc916f..f5ce206b93 100644

hw/display/virtio-gpu-virgl.c int virtio_gpu_virgl_init(VirtIOGPU *g) · line 810

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +4 -0
@@ -803,7 +803,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifdef VIRGL_RENDERER_VENUS
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+#else
     ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+#endif

Note that Venus now requires VIRGL_RENDERER_RENDER_SERVER flag to be set. Please test the patches with the latest virglrenderer and etc.

Quoted 7 lines

The #ifdef also doesn't allow adding new flags, it should look like:

#ifdef VIRGL_RENDERER_VENUS
    flags |= VIRGL_RENDERER_RENDER_SERVER;

#endif

    ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);

In fact, we have rebased to the latest virglrenderer:

Quoted 8 lines
Quoted 4 lines

We check both VIRGL_RENDERER_RENDER_SERVER or VIRGL_RENDERER_VENUS in virglrenderer, alternative of them works.

https://gitlab.freedesktop.org/rui/virglrenderer/-/commit/c1322a8a84379b1ef7939f56c6761b0114716f45

All the extra changes you made to virglrenderer that Qemu depends on need to go upstream. Please open all the relevant merge requests. Thanks!

Dmitry, sorry to late response, I have created relevant merge requests below:

Quoted 7 lines

Virglrenderer:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1068

Mesa:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22108

I'd appreciate any comments. :-)

Thanks, Ray. I'll try to get to the patches soon.

Signature · 2 lines
Best regards,
Dmitry
author
#18 lore raw

RFC PATCH 9/18 meson: Enable virglrenderer unstable APIs

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Also, use alternatives to meson compiler has_function, which does not work properly on my development environment.

---
 meson.build | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
meson.build +6 -3
index 826b3473c4..8cd453844f 100644
@@ -774,13 +774,16 @@ if not get_option('virglrenderer').auto() or have_system or have_vhost_user_gpu
                      method: 'pkg-config',
                      required: get_option('virglrenderer'),
                      kwargs: static_kwargs)
+  virgl = declare_dependency(compile_args: '-DVIRGL_RENDERER_UNSTABLE_APIS',
+                             dependencies: virgl)
+
   config_host_data.set('HAVE_VIRGL_RESOURCE_BLOB',
-		       cc.has_function('virgl_renderer_resource_create_blob',
+                       cc.has_type('struct virgl_renderer_resource_create_blob_args',
 				       prefix: '#include <virglrenderer.h>',
 				       dependencies: virgl))
   config_host_data.set('HAVE_VIRGL_CONTEXT_INIT',
-                       cc.has_function('virgl_renderer_context_create_with_flags',
-                                       prefix: '#include <virglrenderer.h>',
+                       cc.has_header_symbol('virglrenderer.h',
+                                       'VIRGL_RENDERER_CONTEXT_FLAG_CAPSET_ID_MASK',
                                        dependencies: virgl))
 endif
 blkio = not_found
Signature · 1 line
2.25.1
author
#19 lore raw

RFC PATCH 10/18 virtio-gpu: Handle set scanout blob command

In reply to Huang Rui

From: Antonio Caggiano <[email protected]>

Use mapped data pointer as data for the scanout.

---
 hw/display/virtio-gpu-virgl.c  |  3 +++
 hw/display/virtio-gpu.c        | 11 +++++++----
 include/hw/virtio/virtio-gpu.h |  2 ++
 3 files changed, 12 insertions(+), 4 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -0
index f5ce206b93..1fe144f64d 100644
@@ -653,6 +653,9 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
     case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB:
         virgl_cmd_resource_create_blob(g, cmd);
         break;
+    case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
+        virtio_gpu_set_scanout_blob(g, cmd);
+        break;
     case VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB:
         virgl_cmd_resource_map_blob(g, cmd);
         break;
hw/display/virtio-gpu.c +7 -4
index c7d1e52cb5..1e334a1e78 100644
@@ -144,7 +144,7 @@ virtio_gpu_find_check_resource(VirtIOGPU *g, uint32_t resource_id,
     }
 
     if (require_backing) {
-        if (!res->iov || (!res->image && !res->blob)) {
+        if (!res->iov || (!res->image && !res->blob) || !res->mapped) {
             qemu_log_mask(LOG_GUEST_ERROR, "%s: no backing storage %d\n",
                           caller, resource_id);
             if (error) {
@@ -637,7 +637,10 @@ static void virtio_gpu_do_set_scanout(VirtIOGPU *g,
         }
 
         data = res->blob;
-    } else {
+    } else if (res->mapped) {
+        data = (uint8_t *)res->mapped;
+    }
+    else {
         data = (uint8_t *)pixman_image_get_data(res->image);
     }
 
@@ -714,8 +717,8 @@ static void virtio_gpu_set_scanout(VirtIOGPU *g,
                               &fb, res, &ss.r, &cmd->error);
 }
 
-static void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
-                                        struct virtio_gpu_ctrl_command *cmd)
+void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
+                                 struct virtio_gpu_ctrl_command *cmd)
 {
     struct virtio_gpu_simple_resource *res;
     struct virtio_gpu_framebuffer fb = { 0 };
include/hw/virtio/virtio-gpu.h +2 -0
index 0a44aea4ee..ce49cdfafb 100644
@@ -284,6 +284,8 @@ void virtio_gpu_simple_process_cmd(VirtIOGPU *g, struct virtio_gpu_ctrl_command
 void virtio_gpu_update_cursor_data(VirtIOGPU *g,
                                    struct virtio_gpu_scanout *s,
                                    uint32_t resource_id);
+void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
+                                 struct virtio_gpu_ctrl_command *cmd);
 
 /* virtio-gpu-udmabuf.c */
 bool virtio_gpu_have_udmabuf(void);
Signature · 1 line
2.25.1
author
#20 lore raw

RFC PATCH 11/18 virtio-gpu: make blob scanout use dmabuf fd

In reply to Huang Rui

From: Robert Beckett <[email protected]>

This relies on a virglrenderer change to include the dmabuf fd when returning resource info.

---
 hw/display/virtio-gpu-virgl.c  | 103 ++++++++++++++++++++++++++++++++-
 hw/display/virtio-gpu.c        |   4 +-
 include/hw/virtio/virtio-gpu.h |   5 ++
 3 files changed, 109 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +102 -1
index 1fe144f64d..69083d15a9 100644
@@ -18,6 +18,7 @@
 #include "hw/virtio/virtio-gpu.h"
 #include "hw/virtio/virtio-gpu-bswap.h"
 #include "hw/virtio/virtio-iommu.h"
+#include "hw/virtio/virtio-gpu-pixman.h"
 
 #include <virglrenderer.h>
 
@@ -208,6 +209,106 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
     g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
 }
 
+static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
+                                 struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_framebuffer fb = { 0 };
+    struct virtio_gpu_set_scanout_blob ss;
+    struct virgl_renderer_resource_info info;
+    uint64_t fbend;
+
+    VIRTIO_GPU_FILL_CMD(ss);
+    virtio_gpu_scanout_blob_bswap(&ss);
+    trace_virtio_gpu_cmd_set_scanout_blob(ss.scanout_id, ss.resource_id,
+                                          ss.r.width, ss.r.height, ss.r.x,
+                                          ss.r.y);
+
+    if (ss.scanout_id >= g->parent_obj.conf.max_outputs) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout id specified %d",
+                      __func__, ss.scanout_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
+        return;
+    }
+
+    if (ss.resource_id == 0) {
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
+        return;
+    }
+
+    if (ss.width < 16 ||
+	ss.height < 16 ||
+	ss.r.x + ss.r.width > ss.width ||
+	ss.r.y + ss.r.height > ss.height) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout %d bounds for"
+                      " resource %d, rect (%d,%d)+%d,%d, fb %d %d\n",
+                      __func__, ss.scanout_id, ss.resource_id,
+                      ss.r.x, ss.r.y, ss.r.width, ss.r.height,
+                      ss.width, ss.height);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
+    if (!console_has_gl(g->parent_obj.scanout[ss.scanout_id].con)) {
+	    qemu_log_mask(LOG_GUEST_ERROR, "%s: unable to scanout blot without GL!\n", __func__);
+	    return;
+    }
+
+    res = virtio_gpu_find_resource(g, ss.resource_id);
+    if (!res) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: illegal resource specified %d\n",
+                          __func__, ss.resource_id);
+            cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+            return;
+    }
+    if (virgl_renderer_resource_get_info(ss.resource_id, &info)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: illegal virgl resource specified %d\n",
+                          __func__, ss.resource_id);
+            cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+            return;
+    }
+    if (!res->dmabuf_fd && info.fd)
+	    res->dmabuf_fd = info.fd;
+
+    fb.format = virtio_gpu_get_pixman_format(ss.format);
+    if (!fb.format) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: host couldn't handle guest format %d\n",
+                      __func__, ss.format);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
+    fb.bytes_pp = DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb.format), 8);
+    fb.width = ss.width;
+    fb.height = ss.height;
+    fb.stride = ss.strides[0];
+    fb.offset = ss.offsets[0] + ss.r.x * fb.bytes_pp + ss.r.y * fb.stride;
+
+    fbend = fb.offset;
+    fbend += fb.stride * (ss.r.height - 1);
+    fbend += fb.bytes_pp * ss.r.width;
+    if (fbend > res->blob_size) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: fb end out of range\n",
+                      __func__);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
+    g->parent_obj.enable = 1;
+    if (virtio_gpu_update_dmabuf(g, ss.scanout_id, res, &fb, &ss.r)) {
+	    qemu_log_mask(LOG_GUEST_ERROR,
+			  "%s: failed to update dmabuf\n", __func__);
+	    cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+	    return;
+    }
+    virtio_gpu_update_scanout(g, ss.scanout_id, res, &ss.r);
+}
+
+
 static void virgl_cmd_submit_3d(VirtIOGPU *g,
                                 struct virtio_gpu_ctrl_command *cmd)
 {
@@ -654,7 +755,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
         virgl_cmd_resource_create_blob(g, cmd);
         break;
     case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
-        virtio_gpu_set_scanout_blob(g, cmd);
+        virgl_cmd_set_scanout_blob(g, cmd);
         break;
     case VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB:
         virgl_cmd_resource_map_blob(g, cmd);
hw/display/virtio-gpu.c +2 -2
index 1e334a1e78..95e421a525 100644
@@ -371,7 +371,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
-static void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
+void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
     struct virtio_gpu_simple_resource *res;
@@ -575,7 +575,7 @@ static void virtio_unref_resource(pixman_image_t *image, void *data)
     pixman_image_unref(data);
 }
 
-static void virtio_gpu_update_scanout(VirtIOGPU *g,
+void virtio_gpu_update_scanout(VirtIOGPU *g,
                                       uint32_t scanout_id,
                                       struct virtio_gpu_simple_resource *res,
                                       struct virtio_gpu_rect *r)
include/hw/virtio/virtio-gpu.h +5 -0
index ce49cdfafb..ba509833db 100644
@@ -286,6 +286,11 @@ void virtio_gpu_update_cursor_data(VirtIOGPU *g,
                                    uint32_t resource_id);
 void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
                                  struct virtio_gpu_ctrl_command *cmd);
+void virtio_gpu_update_scanout(VirtIOGPU *g,
+                                      uint32_t scanout_id,
+                                      struct virtio_gpu_simple_resource *res,
+                                      struct virtio_gpu_rect *r);
+void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id);
 
 /* virtio-gpu-udmabuf.c */
 bool virtio_gpu_have_udmabuf(void);
Signature · 1 line
2.25.1
author
#21 lore raw

RFC PATCH 12/18 softmmu: Fix the size to map cache with xen for host virtual address

In reply to Huang Rui

The xen_map_cache function wants to pass offset and size of this memory block as the input parameters to map the host virtual address. However, block->offset is too large as 0x100000000 (4G), if we assign the size as block->max_length (0x110000000), the mapped host address will be out of block->max_length and easy to overflow. We have to assign the size as (block->max_length - block->offset), then that is able to ensure the address will be located in legal range inside of max_length.

{rcu = {next = 0x0, func = 0x0}, mr = 0x55555681b620, host = 0x0, colo_cache = 0x0, offset = 0x100000000, used_length = 0x110000000, max_length = 0x110000000, resized = 0x0, flags = 0x10, idstr = {0x78,

0x65, 0x6e, 0x2e, 0x72, 0x61, 0x6d, 0x0 <repeats 249 times>}, next = {
    le_next = 0x5555568c61b0, le_prev = 0x55555681c640},

ramblock_notifiers = {lh_first = 0x0}, fd = 0xffffffff, page_size = 0x1000, bmap = 0x0, receivedmap = 0x0, clear_bmap = 0x0, clear_bmap_shift = 0x0, postcopy_length = 0x0}

---
 softmmu/physmem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
softmmu/physmem.c +3 -2
index 1b606a3002..1b0bb35da9 100644
@@ -2304,7 +2304,7 @@ void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr)
             return xen_map_cache(addr, 0, 0, false);
         }
 
-        block->host = xen_map_cache(block->offset, block->max_length, 1, false);
+	block->host = xen_map_cache(block->offset, block->max_length, 1, false);
     }
     return ramblock_ptr(block, addr);
 }
@@ -2337,7 +2337,8 @@ static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr,
             return xen_map_cache(addr, *size, lock, lock);
         }
 
-        block->host = xen_map_cache(block->offset, block->max_length, 1, lock);
+	block->host = xen_map_cache(block->offset,
+				    block->max_length - block->offset, 1, lock);
     }
 
     return ramblock_ptr(block, addr);
Signature · 1 line
2.25.1
#22 lore raw

Re: [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address

Quoted 5 lines · Huang Rui

The xen_map_cache function wants to pass offset and size of this memory block as the input parameters to map the host virtual address. However, block->offset is too large as 0x100000000 (4G), if we assign the size as block->max_length (0x110000000), the mapped host address will be out of block->max_length and easy to overflow.

Hi Ray,

Is this patch still required after all the other fixes?

If it is required, where is the overflow that it is trying to prevent? Is it a failure in the hypercall mapping the memory to QEMU (hw/i386/xen/xen-mapcache.c:xen_remap_bucket) ?

Quoted 23 lines

We have to assign the size as
(block->max_length - block->offset), then that is able to ensure the address will be located in legal range inside of max_length.

{rcu = {next = 0x0, func = 0x0}, mr = 0x55555681b620, host = 0x0, colo_cache = 0x0, offset = 0x100000000, used_length = 0x110000000, max_length = 0x110000000, resized = 0x0, flags = 0x10, idstr = {0x78,

0x65, 0x6e, 0x2e, 0x72, 0x61, 0x6d, 0x0 <repeats 249 times>}, next = {
    le_next = 0x5555568c61b0, le_prev = 0x55555681c640},

ramblock_notifiers = {lh_first = 0x0}, fd = 0xffffffff, page_size = 0x1000, bmap = 0x0, receivedmap = 0x0, clear_bmap = 0x0, clear_bmap_shift = 0x0, postcopy_length = 0x0}

---
 softmmu/physmem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
softmmu/physmem.c +0 -0
index 1b606a3002..1b0bb35da9 100644

softmmu/physmem.c void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr) · line 2307

Quoted 5 lines
softmmu/physmem.c +1 -1
@@ -2304,7 +2304,7 @@ void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr)
             return xen_map_cache(addr, 0, 0, false);
         }
 
-        block->host = xen_map_cache(block->offset, block->max_length, 1, false);
+	block->host = xen_map_cache(block->offset, block->max_length, 1, false);

Coding style: indentation is 4 spaces. In any case, this looks like a spurious change?

softmmu/physmem.c static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr, · line 2343

Quoted 12 lines
softmmu/physmem.c +2 -1
     }
     return ramblock_ptr(block, addr);
 }
@@ -2337,7 +2337,8 @@ static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr,
             return xen_map_cache(addr, *size, lock, lock);
         }
 
-        block->host = xen_map_cache(block->offset, block->max_length, 1, lock);
+	block->host = xen_map_cache(block->offset,
+				    block->max_length - block->offset, 1, lock);
     }
     return ramblock_ptr(block, addr);

block->offset is the address of the beginning of the block, and block->max_length is the size. Here the behavior is theoretically
correct: if block->host is not set (not mapped in QEMU yet), then call xen_map_cache to map the entire block from beginning to end, setting block->host with a pointer to the beginning of the mapped area in QEMU. From that point onward, ramblock_ptr() will then behave correctly.

Of course if xen_map_cache fails to map the entire region at once because it is too large or other error, then we have a big problem.

But I think in that case this patch would still cause issues. In this example offset (start of the ramblock) is 0x100000000, and max_length (size of the ramblock) is 0x110000000. So with this change we are mapping 0x110000000-0x100000000 = 0x10000000 which is only the first 256MB of the region which is more than 4GB.

What happens the next time qemu_ram_ptr_length is called for an address above the first 256MB? It will break because block->host != NULL so the function will behave as if the entire ramblock is mapped in QEMU while it is not (only the first 256MB are). ramblock_ptr will return block->host + something-more-than-256MB which is actually invalid.

I think we would need more something along this line where we fall back to temporary mappings of a smaller region if we can't map it all at once. MAX_SIZE would be the max size where a single mapping still succeeds, maybe 4GB?

        if (block->offset == 0 || block->max_length > MAX_SIZE) {
            return xen_map_cache(addr, *size, lock, lock);
        }

Otherwise, maybe the error could be due to max_length being incorrect to begin with?

author
#23 lore raw

RFC PATCH 13/18 hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function

In reply to Huang Rui

Introduce xen_ram_block_check function to check whether current ramblock is xen ram memory.

---
 hw/i386/xen/xen-hvm.c | 15 +++++++++++++++
 include/hw/xen/xen.h  |  1 +
 2 files changed, 16 insertions(+)
hw/i386/xen/xen-hvm.c +15 -0
index e4293d6d66..a4f12aefce 100644
@@ -32,6 +32,7 @@
 #include "sysemu/xen.h"
 #include "sysemu/xen-mapcache.h"
 #include "trace.h"
+#include "include/exec/ramblock.h"
 
 #include <xen/hvm/ioreq.h>
 #include <xen/hvm/e820.h>
@@ -1564,6 +1565,20 @@ void xen_register_framebuffer(MemoryRegion *mr)
     framebuffer = mr;
 }
 
+bool xen_ram_block_check(RAMBlock *rb)
+{
+	bool ret;
+
+	if (!rb)
+		return false;
+
+	ret = (rb == ram_memory.ram_block);
+	if (ret)
+		rb->offset = 0;
+
+	return ret;
+}
+
 void xen_shutdown_fatal_error(const char *fmt, ...)
 {
     va_list ap;
include/hw/xen/xen.h +1 -0
index afdf9c436a..99a383eb17 100644
@@ -31,5 +31,6 @@ qemu_irq *xen_interrupt_controller_init(void);
 void xenstore_store_pv_console_info(int i, Chardev *chr);
 
 void xen_register_framebuffer(struct MemoryRegion *mr);
+bool xen_ram_block_check(RAMBlock *rb);
 
 #endif /* QEMU_HW_XEN_H */
Signature · 1 line
2.25.1
#24 lore raw

Re: [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function

Quoted 25 lines · Huang Rui

Introduce xen_ram_block_check function to check whether current ramblock is xen ram memory.

---
 hw/i386/xen/xen-hvm.c | 15 +++++++++++++++
 include/hw/xen/xen.h  |  1 +
 2 files changed, 16 insertions(+)
hw/i386/xen/xen-hvm.c +1 -0
index e4293d6d66..a4f12aefce 100644
@@ -32,6 +32,7 @@
 #include "sysemu/xen.h"
 #include "sysemu/xen-mapcache.h"
 #include "trace.h"
+#include "include/exec/ramblock.h"
 
 #include <xen/hvm/ioreq.h>
 #include <xen/hvm/e820.h>
@@ -1564,6 +1565,20 @@ void xen_register_framebuffer(MemoryRegion *mr)
     framebuffer = mr;
 }
 

hw/i386/xen/xen-hvm.c void xen_register_framebuffer(MemoryRegion *mr) · line 1577

Quoted 10 lines
hw/i386/xen/xen-hvm.c +10 -0
+bool xen_ram_block_check(RAMBlock *rb)
+{
+	bool ret;
+
+	if (!rb)
+		return false;
+
+	ret = (rb == ram_memory.ram_block);
+	if (ret)
+		rb->offset = 0;

I take that this is needed because there is a ramblock that is ram_memory but with offset != 0? So it would fail the block->offset == 0 check in qemu_ram_ptr_length (which is meant to capture all accesses to ram_memory, but failing at it)?

If so, would it be possible to just do this instead:

softmmu/physmem.c +1 -1
index fb412a56e1..3e2640dabd 100644
@@ -2149,7 +2149,7 @@ static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr,
          * because we don't want to map the entire memory in QEMU.
          * In that case just map the requested area.
          */
-        if (block->offset == 0) {
+        if (block->offset == 0 || block == ram_memory.ram_block) {
             return xen_map_cache(addr, *size, lock, lock);
         }
 
Quoted 20 lines
hw/i386/xen/xen-hvm.c +3 -0
+	return ret;
+}
+
 void xen_shutdown_fatal_error(const char *fmt, ...)
 {
     va_list ap;
include/hw/xen/xen.h +1 -0
index afdf9c436a..99a383eb17 100644
@@ -31,5 +31,6 @@ qemu_irq *xen_interrupt_controller_init(void);
 void xenstore_store_pv_console_info(int i, Chardev *chr);
 
 void xen_register_framebuffer(struct MemoryRegion *mr);
+bool xen_ram_block_check(RAMBlock *rb);
 
 #endif /* QEMU_HW_XEN_H */
Signature · 1 line
2.25.1
author
#25 lore raw

RFC PATCH 14/18 softmmu: Add ram block check to map the xen ram memory

In reply to Huang Rui

The xen ram memory should be mapped with addr instead of ramblock offset. So we need to add a check to make sure current ramblock is xen ram memory.

---
 softmmu/physmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
softmmu/physmem.c +1 -1
index 1b0bb35da9..e54561bace 100644
@@ -2333,7 +2333,7 @@ static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr,
          * because we don't want to map the entire memory in QEMU.
          * In that case just map the requested area.
          */
-        if (block->offset == 0) {
+        if (block->offset == 0 || (!lock && xen_ram_block_check(block))) {
             return xen_map_cache(addr, *size, lock, lock);
         }
 
Signature · 1 line
2.25.1
author
#26 lore raw

RFC PATCH 15/18 softmmu: Enable qemu ram allocation with fd for Xen

In reply to Huang Rui

Venus is requesting the function on Xen as well, enable this path on Xen hypervisor.

---
 softmmu/physmem.c | 5 -----
 1 file changed, 5 deletions(-)
softmmu/physmem.c +0 -5
index e54561bace..2838dee2f2 100644
@@ -2060,11 +2060,6 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
                           RAM_PROTECTED)) == 0);
 
-    if (xen_enabled()) {
-        error_setg(errp, "-mem-path not supported with Xen");
-        return NULL;
-    }
-
     if (kvm_enabled() && !kvm_has_sync_mmu()) {
         error_setg(errp,
                    "host lacks kvm mmu notifiers, -mem-path unsupported");
Signature · 1 line
2.25.1
author
#27 lore raw

RFC PATCH 16/18 virtio-gpu: fix hw-display-virtio-gpu.so undefined symbol virtio_gpu_virgl_resource_unmap

In reply to Huang Rui

From: Honglei Huang <[email protected]>

Move virtio_gpu_virgl_resource_unmap to virtio-gpu.c cause virtio-gpu.so call this function but this function in virtio-gpu-gl.so before.

Add virgl dependency into virtio-gpu-gl.so.

---
 hw/display/meson.build        |  2 +-
 hw/display/virtio-gpu-virgl.c | 17 -----------------
 hw/display/virtio-gpu.c       | 19 +++++++++++++++++++
 3 files changed, 20 insertions(+), 18 deletions(-)
hw/display/meson.build +1 -1
index 7a725ed80e..21999dfbe0 100644
@@ -67,7 +67,7 @@ softmmu_ss.add(when: [pixman, 'CONFIG_ATI_VGA'], if_true: files('ati.c', 'ati_2d
 if config_all_devices.has_key('CONFIG_VIRTIO_GPU')
   virtio_gpu_ss = ss.source_set()
   virtio_gpu_ss.add(when: 'CONFIG_VIRTIO_GPU',
-                    if_true: [files('virtio-gpu-base.c', 'virtio-gpu.c'), pixman])
+                    if_true: [files('virtio-gpu-base.c', 'virtio-gpu.c'), pixman, virgl])
   virtio_gpu_ss.add(when: 'CONFIG_LINUX', if_true: files('virtio-gpu-udmabuf.c'),
                                           if_false: files('virtio-gpu-udmabuf-stubs.c'))
   virtio_gpu_ss.add(when: 'CONFIG_VHOST_USER_GPU', if_true: files('vhost-user-gpu.c'))
hw/display/virtio-gpu-virgl.c +0 -17
index 69083d15a9..d5214e0f43 100644
@@ -636,23 +636,6 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
     virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
 }
 
-int virtio_gpu_virgl_resource_unmap(VirtIOGPU *g,
-                                    struct virtio_gpu_simple_resource *res)
-{
-    if (!res->mapped) {
-        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already unmapped %d\n",
-                      __func__, res->resource_id);
-        return VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
-    }
-
-    memory_region_set_enabled(&res->region, false);
-    memory_region_del_subregion(&g->parent_obj.hostmem, &res->region);
-    object_unparent(OBJECT(&res->region));
-
-    res->mapped = NULL;
-    return virgl_renderer_resource_unmap(res->resource_id);
-}
-
 static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
                                         struct virtio_gpu_ctrl_command *cmd)
 {
hw/display/virtio-gpu.c +19 -0
index 95e421a525..95eaed61b3 100644
@@ -31,6 +31,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 
+#include <virglrenderer.h>
+
 #define VIRTIO_GPU_VM_VERSION 1
 
 static struct virtio_gpu_simple_resource *
@@ -873,6 +875,23 @@ void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g,
     g_free(iov);
 }
 
+int virtio_gpu_virgl_resource_unmap(VirtIOGPU *g,
+                                    struct virtio_gpu_simple_resource *res)
+{
+    if (!res->mapped) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already unmapped %d\n",
+                      __func__, res->resource_id);
+        return VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+    }
+
+    memory_region_set_enabled(&res->region, false);
+    memory_region_del_subregion(&g->parent_obj.hostmem, &res->region);
+    object_unparent(OBJECT(&res->region));
+
+    res->mapped = NULL;
+    return virgl_renderer_resource_unmap(res->resource_id);
+}
+
 static void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
                                        struct virtio_gpu_simple_resource *res)
 {
Signature · 1 line
2.25.1
author
#28 lore raw

RFC PATCH 17/18 virtio-gpu: Add video hardware accelerate support for virgl

In reply to Huang Rui

From: Honglei Huang <[email protected]>

Add get_drm_fd callback let the guest OS can get render node to using hw accel codec.

---
 hw/display/virtio-gpu-virgl.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-virgl.c +19 -2
index d5214e0f43..aef735a427 100644
@@ -13,6 +13,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/iov.h"
+#include "qemu/drm.h"
 #include "trace.h"
 #include "hw/virtio/virtio.h"
 #include "hw/virtio/virtio-gpu.h"
@@ -827,12 +828,22 @@ static int virgl_make_context_current(void *opaque, int scanout_idx,
                                    qctx);
 }
 
+static int virgl_get_drm_fd(void *opaque)
+{
+    int fd = -1;
+
+    fd = qemu_drm_rendernode_open(NULL);
+
+    return fd;
+}
+
 static struct virgl_renderer_callbacks virtio_gpu_3d_cbs = {
     .version             = 1,
     .write_fence         = virgl_write_fence,
     .create_gl_context   = virgl_create_context,
     .destroy_gl_context  = virgl_destroy_context,
     .make_current        = virgl_make_context_current,
+    .get_drm_fd          = virgl_get_drm_fd,
 };
 
 static void virtio_gpu_print_stats(void *opaque)
@@ -886,14 +897,20 @@ void virtio_gpu_virgl_reset(VirtIOGPU *g)
     virgl_renderer_reset();
 }
 
+
+
 int virtio_gpu_virgl_init(VirtIOGPU *g)
 {
     int ret;
 
+#ifndef VIRGL_RENDERER_USE_VIDEO
+#define VIRGL_RENDERER_USE_VIDEO (1 << 11)
+#endif
+
 #ifdef VIRGL_RENDERER_VENUS
-    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS, &virtio_gpu_3d_cbs);
+    ret = virgl_renderer_init(g, VIRGL_RENDERER_VENUS | VIRGL_RENDERER_USE_VIDEO, &virtio_gpu_3d_cbs);
 #else
-    ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
+    ret = virgl_renderer_init(g, 0 | VIRGL_RENDERER_USE_VIDEO, &virtio_gpu_3d_cbs);
 #endif
     if (ret != 0) {
         error_report("virgl could not be initialized: %d", ret);
Signature · 1 line
2.25.1
author
#29 lore raw

RFC PATCH 18/18 xen: translate irq of host pci device to gsi

In reply to Huang Rui

From: Chen Jiqian <[email protected]>

Use the new interface in the kernel to map pirq for qemu.

---
 hw/xen/xen-host-pci-device.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/xen/xen-host-pci-device.c +2 -1
index 8c6e9a1716..6ca841a967 100644
@@ -9,6 +9,7 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "qemu/cutils.h"
+#include "hw/xen/xen_common.h"
 #include "xen-host-pci-device.h"
 
 #define XEN_HOST_PCI_MAX_EXT_CAP \
@@ -368,7 +369,7 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
     if (*errp) {
         goto error;
     }
-    d->irq = v;
+    d->irq = xc_physdev_gsi_from_irq(xen_xc, v);
 
     xen_host_pci_get_hex_value(d, "class", &v, errp);
     if (*errp) {
Signature · 1 line
2.25.1