Skip to contentSkip to conversation

virtio-gpu: Add userptr support and ROCm capability

RFC PATCH New Honglei Huang 1 participant 6 messages Dec 3

lore
Share & tools
lore
#2 lore raw

RFC PATCH v8 2/5 virtio-gpu: Add ROCm capability support

In reply to Honglei Huang

Add support for ROCm (Radeon Open Compute) capability in virtio-gpu. This enables GPU compute workloads using AMD's ROCm platform through the virtio-gpu interface.

Changes include:
- Add "rocm" property to virtio-gpu-gl device
- Define VIRTIO_GPU_FLAG_ROCM_ENABLED flag
- Enable VIRGL_RENDERER_USE_ROCM flag when ROCm is enabled
- Add virtio_gpu_rocm_enabled() helper macro

This allows guests to detect and utilize ROCm capabilities through the virtio-gpu device when the feature is enabled.

---
 hw/display/virtio-gpu-gl.c     |  2 ++
 hw/display/virtio-gpu-virgl.c  | 17 +++++++++++++++++
 include/hw/virtio/virtio-gpu.h |  3 +++
 3 files changed, 22 insertions(+)
hw/display/virtio-gpu-gl.c +2 -0
index 2b7a41c4664..d1f11a5bb92 100644
@@ -170,6 +170,8 @@ static const Property virtio_gpu_gl_properties[] = {
                     VIRTIO_GPU_FLAG_VENUS_ENABLED, false),
     DEFINE_PROP_BIT("drm_native_context", VirtIOGPU, parent_obj.conf.flags,
                     VIRTIO_GPU_FLAG_DRM_ENABLED, false),
+    DEFINE_PROP_BIT("rocm", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_ROCM_ENABLED, false),
 };
 
 static void virtio_gpu_gl_device_unrealize(DeviceState *qdev)
hw/display/virtio-gpu-virgl.c +17 -0
index f71f287d948..f3512337d05 100644
@@ -1514,6 +1514,14 @@ static int virtio_gpu_virgl_init(VirtIOGPU *g)
             return -EINVAL;
         }
     }
+    if (virtio_gpu_rocm_enabled(g->parent_obj.conf)) {
+#ifdef VIRGL_RENDERER_USE_ROCM
+        flags |= VIRGL_RENDERER_USE_ROCM;
+#else
+        error_report("rocm requires a ROCm-enabled virglrenderer");
+        return -EINVAL;
+#endif
+    }
 #endif
 
     ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
@@ -1615,5 +1623,14 @@ GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g)
         }
     }
 
+    if (virtio_gpu_rocm_enabled(g->parent_obj.conf)) {
+        virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_ROCM,
+                                   &capset_max_ver,
+                                   &capset_max_size);
+        if (capset_max_size) {
+            virtio_gpu_virgl_add_capset(capset_ids, VIRTIO_GPU_CAPSET_ROCM);
+        }
+    }
+
     return capset_ids;
 }
include/hw/virtio/virtio-gpu.h +3 -0
index 631dfc63a4d..3c5119ce0e4 100644
@@ -112,6 +112,7 @@ enum virtio_gpu_base_conf_flags {
     VIRTIO_GPU_FLAG_VENUS_ENABLED,
     VIRTIO_GPU_FLAG_RESOURCE_UUID_ENABLED,
     VIRTIO_GPU_FLAG_DRM_ENABLED,
+    VIRTIO_GPU_FLAG_ROCM_ENABLED,
 };
 
 #define virtio_gpu_virgl_enabled(_cfg) \
@@ -136,6 +137,8 @@ enum virtio_gpu_base_conf_flags {
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VENUS_ENABLED))
 #define virtio_gpu_drm_enabled(_cfg) \
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DRM_ENABLED))
+#define virtio_gpu_rocm_enabled(_cfg) \
+    (_cfg.flags & (1 << VIRTIO_GPU_FLAG_ROCM_ENABLED))
 
 struct virtio_gpu_base_conf {
     uint32_t max_outputs;
Signature · 1 line
2.34.1
#3 lore raw

RFC PATCH v8 3/5 virtio-gpu: remove the artificial IOV mapping limit

In reply to Honglei Huang

Userptr-backed blobs can require a very large number of virtio_gpu_mem_entry elements when their pages are not contiguous. The guest virtio-gpu frontend forwards that entry count and does not impose the 16384-entry cap previously enforced by virtio_gpu_create_mapping_iov().

Replace that QEMU-only limit with a check that the entry array size does not overflow size_t, which can happen on 32-bit hosts. Make the entry loop counter unsigned like nr_entries so it cannot overflow either. Keep the existing allocation, copy-size, and DMA mapping checks.

---
 hw/display/virtio-gpu.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
hw/display/virtio-gpu.c +5 -7
index d248e50abcb..79968dcf12f 100644
@@ -988,16 +988,14 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
 {
     struct virtio_gpu_mem_entry *ents;
     size_t esize, s;
-    int e, v;
+    uint32_t e;
+    int v;
 
-    if (nr_entries > 16384) {
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: nr_entries is too big (%d > 16384)\n",
+    if (__builtin_mul_overflow(sizeof(*ents), nr_entries, &esize)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: entry count is too large (%u)\n",
                       __func__, nr_entries);
         return -1;
     }
-
-    esize = sizeof(*ents) * nr_entries;
     ents = g_try_malloc(esize);
     if (!ents && esize) {
         return -1;
@@ -1030,7 +1028,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
                                  MEMTXATTRS_UNSPECIFIED);
             if (!map) {
                 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map MMIO memory for"
-                              " element %d\n", __func__, e);
+                              " element %u\n", __func__, e);
                 goto err;
             }
 
Signature · 1 line
2.34.1
#4 lore raw

RFC PATCH v8 4/5 linux-headers: Update to v7.4-rc2-userptr-v11 for readonly blob (!UPSTREAM)

In reply to Honglei Huang

Add VIRTIO_GPU_F_BLOB_READONLY and VIRTIO_GPU_BLOB_FLAG_USE_READONLY to the imported virtio-gpu header.

The kernel series adding them is not merged yet:
https://lore.kernel.org/all/[email protected]/

---
 include/standard-headers/linux/virtio_gpu.h | 5 +++++
 1 file changed, 5 insertions(+)
include/standard-headers/linux/virtio_gpu.h +5 -0
index 976bd225875..95819d4a24c 100644
@@ -72,6 +72,10 @@
  * must be aligned to that value.
  */
 #define VIRTIO_GPU_F_BLOB_ALIGNMENT      5
+/*
+ * VIRTIO_GPU_BLOB_FLAG_USE_READONLY
+ */
+#define VIRTIO_GPU_F_BLOB_READONLY       6
 
 enum virtio_gpu_ctrl_type {
 	VIRTIO_GPU_UNDEFINED = 0,
@@ -415,6 +419,7 @@ struct virtio_gpu_resource_create_blob {
 #define VIRTIO_GPU_BLOB_FLAG_USE_MAPPABLE     0x0001
 #define VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE    0x0002
 #define VIRTIO_GPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004
+#define VIRTIO_GPU_BLOB_FLAG_USE_READONLY     0x0008
 	/* zero is invalid blob mem */
 	uint32_t blob_mem;
 	uint32_t blob_flags;
Signature · 1 line
2.34.1
#5 lore raw

RFC PATCH v8 5/5 virtio-gpu: advertise readonly blobs

In reply to Honglei Huang

Advertise VIRTIO_GPU_F_BLOB_READONLY whenever blob resources are enabled. The flag is generic: VIRTIO_GPU_BLOB_FLAG_USE_READONLY is passed to the renderer with the other blob flags, and each context type decides how to honour it.

Reject a CREATE_BLOB request that sets the flag without negotiating the feature in the 2D, virgl and rutabaga backends.

---
 hw/display/virtio-gpu-base.c     | 1 +
 hw/display/virtio-gpu-rutabaga.c | 7 +++++++
 hw/display/virtio-gpu-virgl.c    | 7 +++++++
 hw/display/virtio-gpu.c          | 7 +++++++
 4 files changed, 22 insertions(+)
hw/display/virtio-gpu-base.c +1 -0
index 270fbaae102..f699648b9d5 100644
@@ -269,6 +269,7 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t features,
     }
     if (virtio_gpu_blob_enabled(g->conf)) {
         features |= (1 << VIRTIO_GPU_F_RESOURCE_BLOB);
+        features |= (1 << VIRTIO_GPU_F_BLOB_READONLY);
     }
     if (virtio_gpu_context_init_enabled(g->conf)) {
         features |= (1 << VIRTIO_GPU_F_CONTEXT_INIT);
hw/display/virtio-gpu-rutabaga.c +7 -0
index 1312a67d701..3cff87e25b8 100644
@@ -633,6 +633,13 @@ rutabaga_cmd_resource_create_blob(VirtIOGPU *g,
     VIRTIO_GPU_FILL_CMD(cblob);
     trace_virtio_gpu_cmd_res_create_blob(cblob.resource_id, cblob.size);
 
+    if ((cblob.blob_flags & VIRTIO_GPU_BLOB_FLAG_USE_READONLY) &&
+        !virtio_vdev_has_feature(VIRTIO_DEVICE(g),
+                                VIRTIO_GPU_F_BLOB_READONLY)) {
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
     CHECK(cblob.resource_id != 0, cmd);
 
     res = virtio_gpu_simple_resource_new_blob(cblob.resource_id, cblob.size);
hw/display/virtio-gpu-virgl.c +7 -0
index f3512337d05..be38fc59cf2 100644
@@ -866,6 +866,13 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
     virtio_gpu_create_blob_bswap(&cblob);
     trace_virtio_gpu_cmd_res_create_blob(cblob.resource_id, cblob.size);
 
+    if ((cblob.blob_flags & VIRTIO_GPU_BLOB_FLAG_USE_READONLY) &&
+        !virtio_vdev_has_feature(VIRTIO_DEVICE(g),
+                                VIRTIO_GPU_F_BLOB_READONLY)) {
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
     if (cblob.resource_id == 0) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
                       __func__);
hw/display/virtio-gpu.c +7 -0
index 79968dcf12f..4e05f1305e5 100644
@@ -368,6 +368,13 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     virtio_gpu_create_blob_bswap(&cblob);
     trace_virtio_gpu_cmd_res_create_blob(cblob.resource_id, cblob.size);
 
+    if ((cblob.blob_flags & VIRTIO_GPU_BLOB_FLAG_USE_READONLY) &&
+        !virtio_vdev_has_feature(VIRTIO_DEVICE(g),
+                                VIRTIO_GPU_F_BLOB_READONLY)) {
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
     if (cblob.resource_id == 0) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
                       __func__);
Signature · 1 line
2.34.1
#0 lore raw

v8 cover virtio-gpu: Add userptr support and ROCm capability

This patch series introduces enhancements to virtio-gpu to improve memory management and GPU virtualization capabilities for ROCm (Radeon Open Compute) workloads.

Usage example:
-device virtio-gpu-gl,rocm=on

The series includes:

1. Add ROCm capset ID 10 to the imported Linux headers.
2. Enable ROCm native contexts through the "rocm=on" device property.
3. Replace the qemu side 16384 entry limit with an overflow check.
4. Add VIRTIO_GPU_F_BLOB_READONLY and VIRTIO_GPU_BLOB_FLAG_USE_READONLY to the imported Linux headers.
5. Advertise readonly blob support whenever blobs are enabled.

See overall detail in virglrenderer pr [1].
kernel header change patch: [2].
Virtio spec change request: [3].

Changes in v8:
- Rebase onto current master.
- Patches 1, 4: generate with update-linux-headers.sh from [2] (Alex).
- Patch 2: add Dmitry's Reviewed-by.
- Patch 3: check entry array size overflow, not a fixed limit (Dmitry).
- Patch 5: advertise readonly blobs generically (Dmitry).

Changes in v7:
- Replace the obsolete USE_USERPTR=0x8 / USERPTR_RDONLY=0x10 header definitions with negotiated BLOB_READONLY feature bit 6 and USE_READONLY=0x8.
- Use ROCm capset ID 10 rather than 8.
- Add readonly advertisement for the paired ROCm blob backend.

Changes in v6:
- Changed to RFC status
- Removed VIRTIO_GPU_F_RESOURCE_USERPTR feature flag from patch 1
- Added VIRTIO_GPU_BLOB_FLAG_USERPTR_RDONLY flag to patch 1

Changes in v5:
- Split userptr header updates into dedicated patch (patch 1/4)
- Extracted nr_entries limit removal into standalone commit (patch 2/4)
- Split ROCm header updates into dedicated patch (patch 3/4)
- Removed VIRTIO_GPU_F_RESOURCE_USERPTR feature detection
- Keep ROCm capability support with "rocm=on" device property (patch 4/4)
- Change corresponding commit messages, change corresponding cover letter content
- Remove RFC label

Changes in v4:
- Change this series to RFC
- Renamed HSAKMT-related functionality to ROCm for better clarity and alignment with the ROCm ecosystem terminology
- Changed device property from "hsakmt=on" to "rocm=on"
- Changed flag from VIRTIO_GPU_FLAG_HSAKMT_ENABLED to VIRTIO_GPU_FLAG_ROCM_ENABLED
- Changed renderer flag from VIRGL_RENDER_USE_HSAKMT to VIRGL_RENDERER_USE_ROCM
- Updated capset handling to use VIRTIO_GPU_CAPSET_ROCM (8) instead of VIRTIO_GPU_CAPSET_HSAKMT (8)
- Removed the error handling fix from patch 1 (virtio-gpu-virgl.c) into another thread

Changes in v3:
- Renamed HSAKMT-related functionality to ROCm for better clarity and alignment with the ROCm ecosystem terminology
- Changed device property from "hsakmt=on" to "rocm=on"
- Changed flag from VIRTIO_GPU_FLAG_HSAKMT_ENABLED to VIRTIO_GPU_FLAG_ROCM_ENABLED
- Changed renderer flag from VIRGL_RENDER_USE_HSAKMT to VIRGL_RENDERER_USE_ROCM
- Updated capset handling to use VIRTIO_GPU_CAPSET_ROCM (8) instead of VIRTIO_GPU_CAPSET_HSAKMT (8)
- Removed the error handling fix from patch 1 (virtio-gpu-virgl.c) into another thread

Changes in v2:
- Fixed error handling bug in virtio-gpu-virgl.c where the return value check was inverted (changed from "if (!ret)" to "if (ret != 0)")
- Added VIRGL_RENDER_USE_HSAKMT flag initialization in virtio_gpu_virgl_init() when HSAKMT support is enabled
- Simplified blob resource creation logic by removing complex conditional branching for userptr vs regular blob handling
- Updated commit messages to reflect bug fixes and improvements
- Added references to related patches in Linux kernel, virglrenderer, and ROCm Runtime components for complete feature implementation tracking

[1]: https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1708
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/virtio-comment/[email protected]/

Honglei Huang (5):
linux-headers: Update to v7.4-rc2-userptr-v11 for ROCm capset

    (!UPSTREAM)
  virtio-gpu: Add ROCm capability support
  virtio-gpu: remove the artificial IOV mapping limit
  linux-headers: Update to v7.4-rc2-userptr-v11 for readonly blob
    (!UPSTREAM)

virtio-gpu: advertise readonly blobs

 hw/display/virtio-gpu-base.c                |  1 +
 hw/display/virtio-gpu-gl.c                  |  2 ++
 hw/display/virtio-gpu-rutabaga.c            |  7 ++++++
 hw/display/virtio-gpu-virgl.c               | 24 +++++++++++++++++++++
 hw/display/virtio-gpu.c                     | 19 ++++++++++------
 include/hw/virtio/virtio-gpu.h              |  3 +++
 include/standard-headers/linux/virtio_gpu.h |  6 ++++++
 7 files changed, 55 insertions(+), 7 deletions(-)

base-commit: 9cda597a1a3903d090e8e55e23f5b8a859da88ab

Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v8 1/5 linux-headers: Update to v7.4-rc2-userptr-v11 for ROCm capset (!UPSTREAM)

Add VIRTIO_GPU_CAPSET_ROCM to the imported virtio_gpu.h so the device can advertise the ROCm capability set to the guest.

The kernel series adding it is not merged yet:
https://lore.kernel.org/all/[email protected]/

---
 include/standard-headers/linux/virtio_gpu.h | 1 +
 1 file changed, 1 insertion(+)
include/standard-headers/linux/virtio_gpu.h +1 -0
index c67f50f56cd..976bd225875 100644
@@ -321,6 +321,7 @@ struct virtio_gpu_cmd_submit {
 #define VIRTIO_GPU_CAPSET_VENUS 4
 #define VIRTIO_GPU_CAPSET_CROSS_DOMAIN 5
 #define VIRTIO_GPU_CAPSET_DRM 6
+#define VIRTIO_GPU_CAPSET_ROCM 10
 
 /* VIRTIO_GPU_CMD_GET_CAPSET_INFO */
 struct virtio_gpu_get_capset_info {
Signature · 1 line
2.34.1