xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
* [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen
@ 2023-03-12  9:22 Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 01/18] virtio: Add shared memory capability Huang Rui
                   ` (17 more replies)
  0 siblings, 18 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Huang Rui

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(-)

-- 
2.25.1



^ permalink raw reply	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 01/18] virtio: Add shared memory capability
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-13  9:01   ` Philippe Mathieu-Daudé
  2023-03-12  9:22 ` [RFC QEMU PATCH 02/18] virtio-gpu: hostmem Huang Rui
                   ` (16 subsequent siblings)
  17 siblings, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

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'.

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 hw/virtio/virtio-pci.c         | 18 ++++++++++++++++++
 include/hw/virtio/virtio-pci.h |  4 ++++
 2 files changed, 22 insertions(+)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index a1c9dfa7bb..ae4c29cb96 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -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)
 {
diff --git a/include/hw/virtio/virtio-pci.h b/include/hw/virtio/virtio-pci.h
index 938799e8f6..e67fe422a1 100644
--- a/include/hw/virtio/virtio-pci.h
+++ b/include/hw/virtio/virtio-pci.h
@@ -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
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 02/18] virtio-gpu: hostmem
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 01/18] virtio: Add shared memory capability Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 03/18] virtio-gpu: Handle resource blob commands Huang Rui
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Gerd Hoffmann <kraxel@redhat.com>

Use VIRTIO_GPU_SHM_ID_HOST_VISIBLE as id for virtio-gpu.

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
---
 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(-)

diff --git a/hw/display/virtio-gpu-pci.c b/hw/display/virtio-gpu-pci.c
index 93f214ff58..da6a99f038 100644
--- a/hw/display/virtio-gpu-pci.c
+++ b/hw/display/virtio-gpu-pci.c
@@ -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;
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 5e15c79b94..7b592f998d 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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(),
 };
 
diff --git a/hw/display/virtio-vga.c b/hw/display/virtio-vga.c
index 4dcb34c4a7..aa8d1ab993 100644
--- a/hw/display/virtio-vga.c
+++ b/hw/display/virtio-vga.c
@@ -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
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 2e28507efe..eafce75b04 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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;
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 03/18] virtio-gpu: Handle resource blob commands
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 01/18] virtio: Add shared memory capability Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 02/18] virtio-gpu: hostmem Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature Huang Rui
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Dmitry Osipenko

From: Antonio Caggiano <antonio.caggiano@collabora.com>

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

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
 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(-)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index 73cb92c8d5..9b5e3dc782 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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;
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 7b592f998d..95d1347a2e 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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,
diff --git a/include/hw/virtio/virtio-gpu-bswap.h b/include/hw/virtio/virtio-gpu-bswap.h
index 9124108485..dd1975e2d4 100644
--- a/include/hw/virtio/virtio-gpu-bswap.h
+++ b/include/hw/virtio/virtio-gpu-bswap.h
@@ -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)
 {
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index eafce75b04..caca834680 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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
diff --git a/meson.build b/meson.build
index 5c6b5a1c75..be74b653b6 100644
--- a/meson.build
+++ b/meson.build
@@ -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
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (2 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 03/18] virtio-gpu: Handle resource blob commands Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-13  9:06   ` Philippe Mathieu-Daudé
  2023-03-12  9:22 ` [RFC QEMU PATCH 05/18] virtio-gpu: Unrealize Huang Rui
                   ` (13 subsequent siblings)
  17 siblings, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Marc-André Lureau

From: Antonio Caggiano <antonio.caggiano@collabora.com>

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.

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 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(-)

diff --git a/hw/display/virtio-gpu-base.c b/hw/display/virtio-gpu-base.c
index a29f191aa8..6c5f1f327f 100644
--- a/hw/display/virtio-gpu-base.c
+++ b/hw/display/virtio-gpu-base.c
@@ -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;
 }
diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index 9b5e3dc782..41712b79ee 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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,
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 95d1347a2e..11f3e56013 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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(),
 };
 
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index caca834680..d1ae97153f 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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;
diff --git a/meson.build b/meson.build
index be74b653b6..826b3473c4 100644
--- a/meson.build
+++ b/meson.build
@@ -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
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 05/18] virtio-gpu: Unrealize
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (3 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 06/18] virtio-gpu: Resource UUID Huang Rui
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Antonio Caggiano <antonio.caggiano@collabora.com>

Implement an unrealize function for virtio gpu device.

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 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(-)

diff --git a/hw/display/virtio-gpu-base.c b/hw/display/virtio-gpu-base.c
index 6c5f1f327f..5cb71e71ad 100644
--- a/hw/display/virtio-gpu-base.c
+++ b/hw/display/virtio-gpu-base.c
@@ -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);
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 11f3e56013..62239dee0f 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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;
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index d1ae97153f..ef02190f97 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 06/18] virtio-gpu: Resource UUID
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (4 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 05/18] virtio-gpu: Unrealize Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 07/18] virtio-gpu: Support Venus capset Huang Rui
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Antonio Caggiano <antonio.caggiano@collabora.com>

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.

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 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(+)

diff --git a/hw/display/trace-events b/hw/display/trace-events
index 0c0ffcbe42..6632344322 100644
--- a/hw/display/trace-events
+++ b/hw/display/trace-events
@@ -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"
diff --git a/hw/display/virtio-gpu-base.c b/hw/display/virtio-gpu-base.c
index 5cb71e71ad..54792aa501 100644
--- a/hw/display/virtio-gpu-base.c
+++ b/hw/display/virtio-gpu-base.c
@@ -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;
 }
 
diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index 41712b79ee..a3c388f907 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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;
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 62239dee0f..c7d1e52cb5 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -940,6 +940,37 @@ virtio_gpu_resource_detach_backing(VirtIOGPU *g,
     virtio_gpu_cleanup_mapping(g, res);
 }
 
+void virtio_gpu_resource_assign_uuid(VirtIOGPU *g,
+                                     struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_resource_assign_uuid assign;
+    struct virtio_gpu_resp_resource_uuid resp;
+    QemuUUID *uuid = NULL;
+
+    VIRTIO_GPU_FILL_CMD(assign);
+    virtio_gpu_bswap_32(&assign, sizeof(assign));
+    trace_virtio_gpu_cmd_res_assign_uuid(assign.resource_id);
+
+    res = virtio_gpu_find_check_resource(g, assign.resource_id, false, __func__, &cmd->error);
+    if (!res) {
+        return;
+    }
+
+    memset(&resp, 0, sizeof(resp));
+    resp.hdr.type = VIRTIO_GPU_RESP_OK_RESOURCE_UUID;
+
+    uuid = g_hash_table_lookup(g->resource_uuids, GUINT_TO_POINTER(assign.resource_id));
+    if (!uuid) {
+        uuid = g_new(QemuUUID, 1);
+        qemu_uuid_generate(uuid);
+        g_hash_table_insert(g->resource_uuids, GUINT_TO_POINTER(assign.resource_id), uuid);
+    }
+
+    memcpy(resp.uuid, uuid, sizeof(QemuUUID));
+    virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
+}
+
 void virtio_gpu_simple_process_cmd(VirtIOGPU *g,
                                    struct virtio_gpu_ctrl_command *cmd)
 {
@@ -988,6 +1019,9 @@ void virtio_gpu_simple_process_cmd(VirtIOGPU *g,
     case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
         virtio_gpu_resource_detach_backing(g, cmd);
         break;
+    case VIRTIO_GPU_CMD_RESOURCE_ASSIGN_UUID:
+        virtio_gpu_resource_assign_uuid(g, cmd);
+        break;
     default:
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         break;
@@ -1348,12 +1382,15 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
     QTAILQ_INIT(&g->reslist);
     QTAILQ_INIT(&g->cmdq);
     QTAILQ_INIT(&g->fenceq);
+
+    g->resource_uuids = g_hash_table_new_full(NULL, NULL, NULL, g_free);
 }
 
 static void virtio_gpu_device_unrealize(DeviceState *qdev)
 {
     VirtIOGPU *g = VIRTIO_GPU(qdev);
 
+    g_hash_table_destroy(g->resource_uuids);
     qemu_bh_delete(g->cursor_bh);
     qemu_bh_delete(g->ctrl_bh);
 
@@ -1383,6 +1420,10 @@ void virtio_gpu_reset(VirtIODevice *vdev)
         g_free(cmd);
     }
 
+    if (g->resource_uuids) {
+        g_hash_table_remove_all(g->resource_uuids);
+    }
+
     virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
 }
 
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index ef02190f97..0a44aea4ee 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 07/18] virtio-gpu: Support Venus capset
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (5 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 06/18] virtio-gpu: Resource UUID Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus Huang Rui
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Antonio Caggiano <antonio.caggiano@collabora.com>

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

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 hw/display/virtio-gpu-virgl.c               | 21 +++++++++++++++++----
 include/standard-headers/linux/virtio_gpu.h |  2 ++
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index a3c388f907..fe03dc916f 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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;
 }
diff --git a/include/standard-headers/linux/virtio_gpu.h b/include/standard-headers/linux/virtio_gpu.h
index 2da48d3d4c..2db643ed8f 100644
--- a/include/standard-headers/linux/virtio_gpu.h
+++ b/include/standard-headers/linux/virtio_gpu.h
@@ -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 {
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (6 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 07/18] virtio-gpu: Support Venus capset Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12 17:51   ` Dmitry Osipenko
  2023-03-12  9:22 ` [RFC QEMU PATCH 09/18] meson: Enable virglrenderer unstable APIs Huang Rui
                   ` (9 subsequent siblings)
  17 siblings, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Antonio Caggiano <antonio.caggiano@collabora.com>

Request Venus when initializing VirGL.

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 hw/display/virtio-gpu-virgl.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index fe03dc916f..f5ce206b93 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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;
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 09/18] meson: Enable virglrenderer unstable APIs
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (7 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 10/18] virtio-gpu: Handle set scanout blob command Huang Rui
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Antonio Caggiano <antonio.caggiano@collabora.com>

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

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 meson.build | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/meson.build b/meson.build
index 826b3473c4..8cd453844f 100644
--- a/meson.build
+++ b/meson.build
@@ -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
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 10/18] virtio-gpu: Handle set scanout blob command
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (8 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 09/18] meson: Enable virglrenderer unstable APIs Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 11/18] virtio-gpu: make blob scanout use dmabuf fd Huang Rui
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Antonio Caggiano <antonio.caggiano@collabora.com>

Use mapped data pointer as data for the scanout.

Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
---
 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(-)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index f5ce206b93..1fe144f64d 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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;
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index c7d1e52cb5..1e334a1e78 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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 };
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 0a44aea4ee..ce49cdfafb 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 11/18] virtio-gpu: make blob scanout use dmabuf fd
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (9 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 10/18] virtio-gpu: Handle set scanout blob command Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address Huang Rui
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

From: Robert Beckett <bob.beckett@collabora.com>

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

Signed-off-by: Robert Beckett <bob.beckett@collabora.com>
---
 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(-)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index 1fe144f64d..69083d15a9 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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);
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 1e334a1e78..95e421a525 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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)
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index ce49cdfafb..ba509833db 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -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);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (10 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 11/18] virtio-gpu: make blob scanout use dmabuf fd Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-18  0:31   ` Stefano Stabellini
  2023-03-12  9:22 ` [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function Huang Rui
                   ` (5 subsequent siblings)
  17 siblings, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	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}

Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 softmmu/physmem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index 1b606a3002..1b0bb35da9 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -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);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (11 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-18  0:38   ` Stefano Stabellini
  2023-03-12  9:22 ` [RFC QEMU PATCH 14/18] softmmu: Add ram block check to map the xen ram memory Huang Rui
                   ` (4 subsequent siblings)
  17 siblings, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Huang Rui

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

Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 hw/i386/xen/xen-hvm.c | 15 +++++++++++++++
 include/hw/xen/xen.h  |  1 +
 2 files changed, 16 insertions(+)

diff --git a/hw/i386/xen/xen-hvm.c b/hw/i386/xen/xen-hvm.c
index e4293d6d66..a4f12aefce 100644
--- a/hw/i386/xen/xen-hvm.c
+++ b/hw/i386/xen/xen-hvm.c
@@ -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;
diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h
index afdf9c436a..99a383eb17 100644
--- a/include/hw/xen/xen.h
+++ b/include/hw/xen/xen.h
@@ -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 */
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 14/18] softmmu: Add ram block check to map the xen ram memory
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (12 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 15/18] softmmu: Enable qemu ram allocation with fd for Xen Huang Rui
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	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.

Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 softmmu/physmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index 1b0bb35da9..e54561bace 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -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);
         }
 
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 15/18] softmmu: Enable qemu ram allocation with fd for Xen
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (13 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 14/18] softmmu: Add ram block check to map the xen ram memory Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 16/18] virtio-gpu: fix hw-display-virtio-gpu.so undefined symbol virtio_gpu_virgl_resource_unmap Huang Rui
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Huang Rui

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

Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 softmmu/physmem.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index e54561bace..2838dee2f2 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -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");
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 16/18] virtio-gpu: fix hw-display-virtio-gpu.so undefined symbol virtio_gpu_virgl_resource_unmap
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (14 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 15/18] softmmu: Enable qemu ram allocation with fd for Xen Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 17/18] virtio-gpu: Add video hardware accelerate support for virgl Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 18/18] xen: translate irq of host pci device to gsi Huang Rui
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Huang Rui

From: Honglei Huang <honglei1.huang@amd.com>

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.

Suggested-by: Stefano Stabellini <sstabellini@kernel.org>
Signed-off-by: Honglei Huang <honglei1.huang@amd.com>
Reviewed-by: Huang Rui <ray.huang@amd.com>
Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 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(-)

diff --git a/hw/display/meson.build b/hw/display/meson.build
index 7a725ed80e..21999dfbe0 100644
--- a/hw/display/meson.build
+++ b/hw/display/meson.build
@@ -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'))
diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index 69083d15a9..d5214e0f43 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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)
 {
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 95e421a525..95eaed61b3 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -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)
 {
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 17/18] virtio-gpu: Add video hardware accelerate support for virgl
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (15 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 16/18] virtio-gpu: fix hw-display-virtio-gpu.so undefined symbol virtio_gpu_virgl_resource_unmap Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  2023-03-12  9:22 ` [RFC QEMU PATCH 18/18] xen: translate irq of host pci device to gsi Huang Rui
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Huang Rui

From: Honglei Huang <honglei1.huang@amd.com>

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

Signed-off-by: Honglei Huang <honglei1.huang@amd.com>
Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 hw/display/virtio-gpu-virgl.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index d5214e0f43..aef735a427 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -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);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [RFC QEMU PATCH 18/18] xen: translate irq of host pci device to gsi
  2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
                   ` (16 preceding siblings ...)
  2023-03-12  9:22 ` [RFC QEMU PATCH 17/18] virtio-gpu: Add video hardware accelerate support for virgl Huang Rui
@ 2023-03-12  9:22 ` Huang Rui
  17 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-12  9:22 UTC (permalink / raw)
  To: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Huang Rui

From: Chen Jiqian <Jiqian.Chen@amd.com>

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

Signed-off-by: Chen Jiqian <Jiqian.Chen@amd.com>
Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 hw/xen/xen-host-pci-device.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/xen/xen-host-pci-device.c b/hw/xen/xen-host-pci-device.c
index 8c6e9a1716..6ca841a967 100644
--- a/hw/xen/xen-host-pci-device.c
+++ b/hw/xen/xen-host-pci-device.c
@@ -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) {
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-12  9:22 ` [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus Huang Rui
@ 2023-03-12 17:51   ` Dmitry Osipenko
  2023-03-13  2:22     ` Dmitry Osipenko
  2023-03-13 15:55     ` Huang Rui
  0 siblings, 2 replies; 30+ messages in thread
From: Dmitry Osipenko @ 2023-03-12 17:51 UTC (permalink / raw)
  To: Huang Rui, Gerd Hoffmann, Michael S . Tsirkin,
	Stefano Stabellini, Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

On 3/12/23 12:22, Huang Rui wrote:
> From: Antonio Caggiano <antonio.caggiano@collabora.com>
> 
> Request Venus when initializing VirGL.
> 
> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
> ---
>  hw/display/virtio-gpu-virgl.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> index fe03dc916f..f5ce206b93 100644
> --- a/hw/display/virtio-gpu-virgl.c
> +++ b/hw/display/virtio-gpu-virgl.c
> @@ -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);

-- 
Best regards,
Dmitry



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-12 17:51   ` Dmitry Osipenko
@ 2023-03-13  2:22     ` Dmitry Osipenko
  2023-03-13 15:57       ` Huang Rui
  2023-03-13 15:55     ` Huang Rui
  1 sibling, 1 reply; 30+ messages in thread
From: Dmitry Osipenko @ 2023-03-13  2:22 UTC (permalink / raw)
  To: Huang Rui, Gerd Hoffmann, Michael S . Tsirkin,
	Stefano Stabellini, Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

On 3/12/23 20:51, Dmitry Osipenko wrote:
> On 3/12/23 12:22, Huang Rui wrote:
>> From: Antonio Caggiano <antonio.caggiano@collabora.com>
>>
>> Request Venus when initializing VirGL.
>>
>> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
>> ---
>>  hw/display/virtio-gpu-virgl.c | 4 ++++
>>  1 file changed, 4 insertions(+)
>>
>> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
>> index fe03dc916f..f5ce206b93 100644
>> --- a/hw/display/virtio-gpu-virgl.c
>> +++ b/hw/display/virtio-gpu-virgl.c
>> @@ -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);
> 

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/20220926142422.22325-1-antonio.caggiano@collabora.com/

-- 
Best regards,
Dmitry



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 01/18] virtio: Add shared memory capability
  2023-03-12  9:22 ` [RFC QEMU PATCH 01/18] virtio: Add shared memory capability Huang Rui
@ 2023-03-13  9:01   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-03-13  9:01 UTC (permalink / raw)
  To: Huang Rui, Gerd Hoffmann, Michael S . Tsirkin,
	Stefano Stabellini, Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian

On 12/3/23 10:22, Huang Rui wrote:
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
> 
> 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'.
> 
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
> ---
>   hw/virtio/virtio-pci.c         | 18 ++++++++++++++++++
>   include/hw/virtio/virtio-pci.h |  4 ++++
>   2 files changed, 22 insertions(+)
> 
> diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
> index a1c9dfa7bb..ae4c29cb96 100644
> --- a/hw/virtio/virtio-pci.c
> +++ b/hw/virtio/virtio-pci.c
> @@ -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;

Why initializing 'cap' using 2 different coding styles?

> +    return virtio_pci_add_mem_cap(proxy, &cap.cap);
> +}



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature
  2023-03-12  9:22 ` [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature Huang Rui
@ 2023-03-13  9:06   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 30+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-03-13  9:06 UTC (permalink / raw)
  To: Huang Rui, Gerd Hoffmann, Michael S . Tsirkin,
	Stefano Stabellini, Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel
  Cc: Alex Deucher, Christian König, Stewart Hildebrand,
	Xenia Ragiadakou, Honglei Huang, Julia Zhang, Chen Jiqian,
	Marc-André Lureau

On 12/3/23 10:22, Huang Rui wrote:
> From: Antonio Caggiano <antonio.caggiano@collabora.com>
> 
> 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.
> 
> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>   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(-)


> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> index 9b5e3dc782..41712b79ee 100644
> --- a/hw/display/virtio-gpu-virgl.c
> +++ b/hw/display/virtio-gpu-virgl.c
> @@ -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");

This is dubious, almost nobody will notice this log.

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

> +#endif
> +    }
> +
> +    virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen, cc.debug_name);
>   }



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-12 17:51   ` Dmitry Osipenko
  2023-03-13  2:22     ` Dmitry Osipenko
@ 2023-03-13 15:55     ` Huang Rui
  2023-03-15 23:14       ` Dmitry Osipenko
  1 sibling, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-13 15:55 UTC (permalink / raw)
  To: Dmitry Osipenko
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel, Deucher, Alexander, Koenig, Christian, Hildebrand,
	Stewart, Xenia Ragiadakou, Huang, Honglei1, Zhang, Julia, Chen,
	Jiqian

On Mon, Mar 13, 2023 at 01:51:03AM +0800, Dmitry Osipenko wrote:
> On 3/12/23 12:22, Huang Rui wrote:
> > From: Antonio Caggiano <antonio.caggiano@collabora.com>
> > 
> > Request Venus when initializing VirGL.
> > 
> > Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
> > ---
> >  hw/display/virtio-gpu-virgl.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> > index fe03dc916f..f5ce206b93 100644
> > --- a/hw/display/virtio-gpu-virgl.c
> > +++ b/hw/display/virtio-gpu-virgl.c
> > @@ -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);

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


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-13  2:22     ` Dmitry Osipenko
@ 2023-03-13 15:57       ` Huang Rui
  0 siblings, 0 replies; 30+ messages in thread
From: Huang Rui @ 2023-03-13 15:57 UTC (permalink / raw)
  To: Dmitry Osipenko
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel, Deucher, Alexander, Koenig, Christian, Hildebrand,
	Stewart, Xenia Ragiadakou, Huang, Honglei1, Zhang, Julia, Chen,
	Jiqian

On Mon, Mar 13, 2023 at 10:22:24AM +0800, Dmitry Osipenko wrote:
> On 3/12/23 20:51, Dmitry Osipenko wrote:
> > On 3/12/23 12:22, Huang Rui wrote:
> >> From: Antonio Caggiano <antonio.caggiano@collabora.com>
> >>
> >> Request Venus when initializing VirGL.
> >>
> >> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
> >> ---
> >>  hw/display/virtio-gpu-virgl.c | 4 ++++
> >>  1 file changed, 4 insertions(+)
> >>
> >> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> >> index fe03dc916f..f5ce206b93 100644
> >> --- a/hw/display/virtio-gpu-virgl.c
> >> +++ b/hw/display/virtio-gpu-virgl.c
> >> @@ -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);
> > 
> 
> 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/20220926142422.22325-1-antonio.caggiano@collabora.com/
> 

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

Thanks,
Ray


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-13 15:55     ` Huang Rui
@ 2023-03-15 23:14       ` Dmitry Osipenko
  2023-03-24 13:22         ` Huang Rui
  0 siblings, 1 reply; 30+ messages in thread
From: Dmitry Osipenko @ 2023-03-15 23:14 UTC (permalink / raw)
  To: Huang Rui
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel, Deucher, Alexander, Koenig, Christian, Hildebrand,
	Stewart, Xenia Ragiadakou, Huang, Honglei1, Zhang, Julia, Chen,
	Jiqian

On 3/13/23 18:55, Huang Rui wrote:
> On Mon, Mar 13, 2023 at 01:51:03AM +0800, Dmitry Osipenko wrote:
>> On 3/12/23 12:22, Huang Rui wrote:
>>> From: Antonio Caggiano <antonio.caggiano@collabora.com>
>>>
>>> Request Venus when initializing VirGL.
>>>
>>> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
>>> ---
>>>  hw/display/virtio-gpu-virgl.c | 4 ++++
>>>  1 file changed, 4 insertions(+)
>>>
>>> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
>>> index fe03dc916f..f5ce206b93 100644
>>> --- a/hw/display/virtio-gpu-virgl.c
>>> +++ b/hw/display/virtio-gpu-virgl.c
>>> @@ -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);
> 
> 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!

-- 
Best regards,
Dmitry



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address
  2023-03-12  9:22 ` [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address Huang Rui
@ 2023-03-18  0:31   ` Stefano Stabellini
  0 siblings, 0 replies; 30+ messages in thread
From: Stefano Stabellini @ 2023-03-18  0:31 UTC (permalink / raw)
  To: Huang Rui
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel, Alex Deucher,
	Christian König, Stewart Hildebrand, Xenia Ragiadakou,
	Honglei Huang, Julia Zhang, Chen Jiqian, Xenia.Ragiadakou

On Sun, 12 Mar 2023, Huang Rui wrote:
> 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) ?


> 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}
> 
> Signed-off-by: Huang Rui <ray.huang@amd.com>
> ---
>  softmmu/physmem.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/softmmu/physmem.c b/softmmu/physmem.c
> index 1b606a3002..1b0bb35da9 100644
> --- a/softmmu/physmem.c
> +++ b/softmmu/physmem.c
> @@ -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?


>      }
>      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?


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function
  2023-03-12  9:22 ` [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function Huang Rui
@ 2023-03-18  0:38   ` Stefano Stabellini
  0 siblings, 0 replies; 30+ messages in thread
From: Stefano Stabellini @ 2023-03-18  0:38 UTC (permalink / raw)
  To: Huang Rui
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Antonio Caggiano, Dr . David Alan Gilbert,
	Robert Beckett, qemu-devel, xen-devel, Alex Deucher,
	Christian König, Stewart Hildebrand, Xenia Ragiadakou,
	Honglei Huang, Julia Zhang, Chen Jiqian

On Sun, 12 Mar 2023, Huang Rui wrote:
> Introduce xen_ram_block_check function to check whether current ramblock
> is xen ram memory.
> 
> Signed-off-by: Huang Rui <ray.huang@amd.com>
> ---
>  hw/i386/xen/xen-hvm.c | 15 +++++++++++++++
>  include/hw/xen/xen.h  |  1 +
>  2 files changed, 16 insertions(+)
> 
> diff --git a/hw/i386/xen/xen-hvm.c b/hw/i386/xen/xen-hvm.c
> index e4293d6d66..a4f12aefce 100644
> --- a/hw/i386/xen/xen-hvm.c
> +++ b/hw/i386/xen/xen-hvm.c
> @@ -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;

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:


diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index fb412a56e1..3e2640dabd 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -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);
         }
 


> +	return ret;
> +}
> +
>  void xen_shutdown_fatal_error(const char *fmt, ...)
>  {
>      va_list ap;
> diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h
> index afdf9c436a..99a383eb17 100644
> --- a/include/hw/xen/xen.h
> +++ b/include/hw/xen/xen.h
> @@ -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 */
> -- 
> 2.25.1
> 


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-15 23:14       ` Dmitry Osipenko
@ 2023-03-24 13:22         ` Huang Rui
  2023-04-03 21:03           ` Dmitry Osipenko
  0 siblings, 1 reply; 30+ messages in thread
From: Huang Rui @ 2023-03-24 13:22 UTC (permalink / raw)
  To: Dmitry Osipenko
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel, Deucher, Alexander, Koenig, Christian, Hildebrand,
	Stewart, Xenia Ragiadakou, Huang, Honglei1, Zhang, Julia, Chen,
	Jiqian

On Thu, Mar 16, 2023 at 07:14:47AM +0800, Dmitry Osipenko wrote:
> On 3/13/23 18:55, Huang Rui wrote:
> > On Mon, Mar 13, 2023 at 01:51:03AM +0800, Dmitry Osipenko wrote:
> >> On 3/12/23 12:22, Huang Rui wrote:
> >>> From: Antonio Caggiano <antonio.caggiano@collabora.com>
> >>>
> >>> Request Venus when initializing VirGL.
> >>>
> >>> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
> >>> ---
> >>>  hw/display/virtio-gpu-virgl.c | 4 ++++
> >>>  1 file changed, 4 insertions(+)
> >>>
> >>> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> >>> index fe03dc916f..f5ce206b93 100644
> >>> --- a/hw/display/virtio-gpu-virgl.c
> >>> +++ b/hw/display/virtio-gpu-virgl.c
> >>> @@ -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);
> > 
> > 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!
> 

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


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus
  2023-03-24 13:22         ` Huang Rui
@ 2023-04-03 21:03           ` Dmitry Osipenko
  0 siblings, 0 replies; 30+ messages in thread
From: Dmitry Osipenko @ 2023-04-03 21:03 UTC (permalink / raw)
  To: Huang Rui
  Cc: Gerd Hoffmann, Michael S . Tsirkin, Stefano Stabellini,
	Anthony PERARD, Roger Pau Monné,
	Jan Beulich, Dr . David Alan Gilbert, Robert Beckett, qemu-devel,
	xen-devel, Deucher, Alexander, Koenig, Christian, Hildebrand,
	Stewart, Xenia Ragiadakou, Huang, Honglei1, Zhang, Julia, Chen,
	Jiqian

On 3/24/23 16:22, Huang Rui wrote:
> On Thu, Mar 16, 2023 at 07:14:47AM +0800, Dmitry Osipenko wrote:
>> On 3/13/23 18:55, Huang Rui wrote:
>>> On Mon, Mar 13, 2023 at 01:51:03AM +0800, Dmitry Osipenko wrote:
>>>> On 3/12/23 12:22, Huang Rui wrote:
>>>>> From: Antonio Caggiano <antonio.caggiano@collabora.com>
>>>>>
>>>>> Request Venus when initializing VirGL.
>>>>>
>>>>> Signed-off-by: Antonio Caggiano <antonio.caggiano@collabora.com>
>>>>> ---
>>>>>  hw/display/virtio-gpu-virgl.c | 4 ++++
>>>>>  1 file changed, 4 insertions(+)
>>>>>
>>>>> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
>>>>> index fe03dc916f..f5ce206b93 100644
>>>>> --- a/hw/display/virtio-gpu-virgl.c
>>>>> +++ b/hw/display/virtio-gpu-virgl.c
>>>>> @@ -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);
>>>
>>> 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!
>>
> 
> 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. I'll try to get to the patches soon.


-- 
Best regards,
Dmitry



^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2023-04-03 21:03 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-12  9:22 [RFC QEMU PATCH 00/18] Add VirtIO GPU and Passthrough GPU support on Xen Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 01/18] virtio: Add shared memory capability Huang Rui
2023-03-13  9:01   ` Philippe Mathieu-Daudé
2023-03-12  9:22 ` [RFC QEMU PATCH 02/18] virtio-gpu: hostmem Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 03/18] virtio-gpu: Handle resource blob commands Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 04/18] virtio-gpu: CONTEXT_INIT feature Huang Rui
2023-03-13  9:06   ` Philippe Mathieu-Daudé
2023-03-12  9:22 ` [RFC QEMU PATCH 05/18] virtio-gpu: Unrealize Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 06/18] virtio-gpu: Resource UUID Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 07/18] virtio-gpu: Support Venus capset Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 08/18] virtio-gpu: Initialize Venus Huang Rui
2023-03-12 17:51   ` Dmitry Osipenko
2023-03-13  2:22     ` Dmitry Osipenko
2023-03-13 15:57       ` Huang Rui
2023-03-13 15:55     ` Huang Rui
2023-03-15 23:14       ` Dmitry Osipenko
2023-03-24 13:22         ` Huang Rui
2023-04-03 21:03           ` Dmitry Osipenko
2023-03-12  9:22 ` [RFC QEMU PATCH 09/18] meson: Enable virglrenderer unstable APIs Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 10/18] virtio-gpu: Handle set scanout blob command Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 11/18] virtio-gpu: make blob scanout use dmabuf fd Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 12/18] softmmu: Fix the size to map cache with xen for host virtual address Huang Rui
2023-03-18  0:31   ` Stefano Stabellini
2023-03-12  9:22 ` [RFC QEMU PATCH 13/18] hw/i386/xen/xen-hvm: Introduce xen_ram_block_check function Huang Rui
2023-03-18  0:38   ` Stefano Stabellini
2023-03-12  9:22 ` [RFC QEMU PATCH 14/18] softmmu: Add ram block check to map the xen ram memory Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 15/18] softmmu: Enable qemu ram allocation with fd for Xen Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 16/18] virtio-gpu: fix hw-display-virtio-gpu.so undefined symbol virtio_gpu_virgl_resource_unmap Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 17/18] virtio-gpu: Add video hardware accelerate support for virgl Huang Rui
2023-03-12  9:22 ` [RFC QEMU PATCH 18/18] xen: translate irq of host pci device to gsi Huang Rui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).