Skip to contentSkip to conversation

Support blob memory and venus on qemu

PATCH Superseded Dmitry Osipenko 3 participants 36 messages Apr 18, 2024 – May 10, 2024

Subsystems (2)

v8 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v8 cover Support blob memory and venus on qemu

Hello,

This series enables Vulkan Venus context support on virtio-gpu.

All virglrender and almost all Linux kernel prerequisite changes needed by Venus are already in upstream. For kernel there is a pending KVM patchset that fixes mapping of compound pages needed for DRM drivers using TTM [1], othewrwise hostmem blob mapping will fail with a KVM error from Qemu.

[1] https://lore.kernel.org/kvm/[email protected]/

Example Qemu cmdline that enables Venus for virtio-gpu:

qemu-system-x86_64 -device virtio-vga-gl,hostmem=4G,blob=true,vulkan=true

Changes from V7 to V8

- Supported suspension of virtio-gpu commands processing and made unmapping of hostmem region asynchronous by blocking/suspending cmd processing until region is unmapped. Suggested by Akihiko Odaki.

- Fixed arm64 building of x86 targets using updated linux-headers. Corrected the update script. Thanks to Rob Clark for reporting the issue.

- Added new patch that makes registration of virgl capsets dynamic. Requested by Antonio Caggiano and Pierre-Eric Pelloux-Prayer.

- Venus capset now isn't advertised if Vulkan is disabled with vulkan=false

Changes from V6 to V7

- Used scripts/update-linux-headers.sh to update Qemu headers based on Linux v6.8-rc3 that adds Venus capset definition to virtio-gpu protocol, was requested by Peter Maydel

- Added r-bs that were given to v6 patches. Corrected missing s-o-bs

- Dropped context_init Qemu's virtio-gpu device configuration flag, was suggested by Marc-André Lureau

- Added missing error condition checks spotted by Marc-André Lureau and Akihiko Odaki, and few more

- Returned back res->mr referencing to memory_region_init_ram_ptr() like was suggested by Akihiko Odaki. Incorporated fix suggested by Pierre-Eric to specify the MR name

- Dropped the virgl_gpu_resource wrapper, cleaned up and simplified patch that adds blob-cmd support

- Fixed improper blob resource removal from resource list on resource_unref that was spotted by Akihiko Odaki

- Change order of the blob patches, was suggested by Akihiko Odaki. The cmd_set_scanout_blob support is enabled first

- Factored out patch that adds resource management support to virtio-gpu-gl, was requested by Marc-André Lureau

- Simplified and improved the UUID support patch, dropped the hash table as we don't need it for now. Moved QemuUUID to virtio_gpu_simple_resource. This all was suggested by Akihiko Odaki and Marc-André Lureau

- Dropped console_has_gl() check, suggested by Akihiko Odaki

- Reworked Meson cheking of libvirglrender features, made new features available based on virglrender pkgconfig version instead of checking symbols in header. This should fix build error using older virglrender version, reported by Alex Bennée

- Made enabling of Venus context configrable via new virtio-gpu device "vulkan=true" flag, suggested by Marc-André Lureau. The flag is disabled by default because it requires blob and hostmem options to be enabled and configured

Changes from V5 to V6

- Move macros configurations under virgl.found() and rename HAVE_VIRGL_CONTEXT_CREATE_WITH_FLAGS.

- Handle the case while context_init is disabled.

- Enable context_init by default.

- Move virtio_gpu_virgl_resource_unmap() into
virgl_cmd_resource_unmap_blob().

- Introduce new struct virgl_gpu_resource to store virgl specific members.

- Remove erro handling of g_new0, because glib will abort() on OOM.

- Set resource uuid as option.

- Implement optional subsection of vmstate_virtio_gpu_resource_uuid_state for virtio live migration.

- Use g_int_hash/g_int_equal instead of the default

- Add scanout_blob function for virtio-gpu-virgl

- Resolve the memory leak on virtio-gpu-virgl

- Remove the unstable API flags check because virglrenderer is already 1.0

- Squash the render server flag support into "Initialize Venus"

Changes from V4 (virtio gpu V4) to V5

- Inverted patch 5 and 6 because we should configure HAVE_VIRGL_CONTEXT_INIT firstly.

- Validate owner of memory region to avoid slowing down DMA.

- Use memory_region_init_ram_ptr() instead of
memory_region_init_ram_device_ptr().

- Adjust sequence to allocate gpu resource before virglrender resource creation

- Add virtio migration handling for uuid.

- Send kernel patch to define VIRTIO_GPU_CAPSET_VENUS.
https://lore.kernel.org/lkml/[email protected]/

- Add meson check to make sure unstable APIs defined from 0.9.0.

Changes from V1 to V2 (virtio gpu V4)

- Remove unused #include "hw/virtio/virtio-iommu.h"

- Add a local function, called virgl_resource_destroy(), that is used to release a vgpu resource on error paths and in resource_unref.

- Remove virtio_gpu_virgl_resource_unmap from virtio_gpu_cleanup_mapping(),
since this function won't be called on blob resources and also because blob resources are unmapped via virgl_cmd_resource_unmap_blob().

- In virgl_cmd_resource_create_blob(), do proper cleanup in error paths and move QTAILQ_INSERT_HEAD(&g->reslist, res, next) after the resource has been fully initialized.

- Memory region has a different life-cycle from virtio gpu resources i.e. cannot be released synchronously along with the vgpu resource. So, here the field "region" was changed to a pointer and is allocated dynamically when the blob is mapped.
Also, since the pointer can be used to indicate whether the blob is mapped, the explicite field "mapped" was removed.

- In virgl_cmd_resource_map_blob(), add check on the value of res->region, to prevent beeing called twice on the same resource.

- Add a patch to enable automatic deallocation of memory regions to resolve use-after-free memory corruption with a reference.

Antonio Caggiano (3):
virtio-gpu: Handle resource blob commands
virtio-gpu: Resource UUID
virtio-gpu: Support Venus context

Dmitry Osipenko (4):
linux-headers: Update to Linux v6.9-rc3
virtio-gpu: Use pkgconfig version to decide which virgl features are
available
virtio-gpu: Don't require udmabuf when blobs and virgl are enabled
virtio-gpu: Support suspension of commands processing

Huang Rui (2):
virtio-gpu: Support context-init feature with virglrenderer
virtio-gpu: Add virgl resource management

Pierre-Eric Pelloux-Prayer (1):
virtio-gpu: Register capsets dynamically

Robert Beckett (1):
virtio-gpu: Support blob scanout using dmabuf fd

 hw/display/trace-events                       |   1 +
 hw/display/virtio-gpu-base.c                  |   1 +
 hw/display/virtio-gpu-gl.c                    |   5 +
 hw/display/virtio-gpu-rutabaga.c              |   1 +
 hw/display/virtio-gpu-virgl.c                 | 519 ++++++++++++-
 hw/display/virtio-gpu.c                       |  42 +-
 hw/i386/x86.c                                 |   8 -
 include/hw/virtio/virtio-gpu.h                |  21 +
 include/standard-headers/asm-x86/bootparam.h  |  17 +-
 include/standard-headers/asm-x86/kvm_para.h   |   3 +-
 include/standard-headers/asm-x86/setup_data.h |  83 +++
 include/standard-headers/linux/ethtool.h      |  48 ++
 include/standard-headers/linux/fuse.h         |  39 +-
 .../linux/input-event-codes.h                 |   1 +
 include/standard-headers/linux/virtio_gpu.h   |   2 +
 include/standard-headers/linux/virtio_pci.h   |  10 +-
 include/standard-headers/linux/virtio_snd.h   | 154 ++++
 linux-headers/asm-arm64/kvm.h                 |  15 +-
 linux-headers/asm-arm64/sve_context.h         |  11 +
 linux-headers/asm-generic/bitsperlong.h       |   4 +
 linux-headers/asm-loongarch/kvm.h             |   2 -
 linux-headers/asm-mips/kvm.h                  |   2 -
 linux-headers/asm-powerpc/kvm.h               |  45 +-
 linux-headers/asm-riscv/kvm.h                 |   3 +-
 linux-headers/asm-s390/kvm.h                  | 315 +++++++-
 linux-headers/asm-x86/kvm.h                   | 308 +++++++-
 linux-headers/linux/bits.h                    |  15 +
 linux-headers/linux/kvm.h                     | 689 +-----------------
 linux-headers/linux/psp-sev.h                 |  59 ++
 linux-headers/linux/vhost.h                   |   7 +
 meson.build                                   |  10 +-
 scripts/update-linux-headers.sh               |   5 +-
 32 files changed, 1679 insertions(+), 766 deletions(-)
 create mode 100644 include/standard-headers/asm-x86/setup_data.h
 create mode 100644 linux-headers/linux/bits.h
Signature · 1 line
2.44.0
#1 lore raw

PATCH v8 1/11 linux-headers: Update to Linux v6.9-rc3

Update kernel headers to get new VirtIO-GPU capsets, in particular the Venus capset.

---
 hw/i386/x86.c                                 |   8 -
 include/standard-headers/asm-x86/bootparam.h  |  17 +-
 include/standard-headers/asm-x86/kvm_para.h   |   3 +-
 include/standard-headers/asm-x86/setup_data.h |  83 +++
 include/standard-headers/linux/ethtool.h      |  48 ++
 include/standard-headers/linux/fuse.h         |  39 +-
 .../linux/input-event-codes.h                 |   1 +
 include/standard-headers/linux/virtio_gpu.h   |   2 +
 include/standard-headers/linux/virtio_pci.h   |  10 +-
 include/standard-headers/linux/virtio_snd.h   | 154 ++++
 linux-headers/asm-arm64/kvm.h                 |  15 +-
 linux-headers/asm-arm64/sve_context.h         |  11 +
 linux-headers/asm-generic/bitsperlong.h       |   4 +
 linux-headers/asm-loongarch/kvm.h             |   2 -
 linux-headers/asm-mips/kvm.h                  |   2 -
 linux-headers/asm-powerpc/kvm.h               |  45 +-
 linux-headers/asm-riscv/kvm.h                 |   3 +-
 linux-headers/asm-s390/kvm.h                  | 315 +++++++-
 linux-headers/asm-x86/kvm.h                   | 308 +++++++-
 linux-headers/linux/bits.h                    |  15 +
 linux-headers/linux/kvm.h                     | 689 +-----------------
 linux-headers/linux/psp-sev.h                 |  59 ++
 linux-headers/linux/vhost.h                   |   7 +
 scripts/update-linux-headers.sh               |   5 +-
 24 files changed, 1106 insertions(+), 739 deletions(-)
 create mode 100644 include/standard-headers/asm-x86/setup_data.h
 create mode 100644 linux-headers/linux/bits.h
hw/i386/x86.c +0 -8
index ffbda48917fd..84a48019770b 100644
@@ -679,14 +679,6 @@ DeviceState *ioapic_init_secondary(GSIState *gsi_state)
     return dev;
 }
 
-struct setup_data {
-    uint64_t next;
-    uint32_t type;
-    uint32_t len;
-    uint8_t data[];
-} __attribute__((packed));
-
-
 /*
  * The entry point into the kernel for PVH boot is different from
  * the native entry point.  The PVH entry is defined by the x86/HVM
include/standard-headers/asm-x86/bootparam.h +2 -15
index 0b06d2bff1b9..b582a105c087 100644
@@ -2,21 +2,7 @@
 #ifndef _ASM_X86_BOOTPARAM_H
 #define _ASM_X86_BOOTPARAM_H
 
-/* setup_data/setup_indirect types */
-#define SETUP_NONE			0
-#define SETUP_E820_EXT			1
-#define SETUP_DTB			2
-#define SETUP_PCI			3
-#define SETUP_EFI			4
-#define SETUP_APPLE_PROPERTIES		5
-#define SETUP_JAILHOUSE			6
-#define SETUP_CC_BLOB			7
-#define SETUP_IMA			8
-#define SETUP_RNG_SEED			9
-#define SETUP_ENUM_MAX			SETUP_RNG_SEED
-
-#define SETUP_INDIRECT			(1<<31)
-#define SETUP_TYPE_MAX			(SETUP_ENUM_MAX | SETUP_INDIRECT)
+#include "standard-headers/asm-x86/setup_data.h"
 
 /* ram_size flags */
 #define RAMDISK_IMAGE_START_MASK	0x07FF
@@ -38,6 +24,7 @@
 #define XLF_EFI_KEXEC			(1<<4)
 #define XLF_5LEVEL			(1<<5)
 #define XLF_5LEVEL_ENABLED		(1<<6)
+#define XLF_MEM_ENCRYPTION		(1<<7)
 
 
 #endif /* _ASM_X86_BOOTPARAM_H */
include/standard-headers/asm-x86/kvm_para.h +1 -2
index f0235e58a1d3..9a011d20f017 100644
@@ -92,7 +92,7 @@ struct kvm_clock_pairing {
 #define KVM_ASYNC_PF_DELIVERY_AS_INT		(1 << 3)
 
 /* MSR_KVM_ASYNC_PF_INT */
-#define KVM_ASYNC_PF_VEC_MASK			GENMASK(7, 0)
+#define KVM_ASYNC_PF_VEC_MASK			__GENMASK(7, 0)
 
 /* MSR_KVM_MIGRATION_CONTROL */
 #define KVM_MIGRATION_READY		(1 << 0)
@@ -142,7 +142,6 @@ struct kvm_vcpu_pv_apf_data {
 	uint32_t token;
 
 	uint8_t pad[56];
-	uint32_t enabled;
 };
 
 #define KVM_PV_EOI_BIT 0
include/standard-headers/asm-x86/setup_data.h new file +83 -0

83 lines not on this page · raw message

include/standard-headers/linux/ethtool.h +48 -0

60 lines not on this page · raw message

include/standard-headers/linux/fuse.h +36 -3
index fc0dcd10aede..bac9dbc49f80 100644
@@ -211,6 +211,12 @@
  *  7.39
  *  - add FUSE_DIRECT_IO_ALLOW_MMAP
  *  - add FUSE_STATX and related structures
+ *
+ *  7.40
+ *  - add max_stack_depth to fuse_init_out, add FUSE_PASSTHROUGH init flag
+ *  - add backing_id to fuse_open_out, add FOPEN_PASSTHROUGH open flag
+ *  - add FUSE_NO_EXPORT_SUPPORT init flag
+ *  - add FUSE_NOTIFY_RESEND, add FUSE_HAS_RESEND init flag
  */
 
 #ifndef _LINUX_FUSE_H
@@ -242,7 +248,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 39
+#define FUSE_KERNEL_MINOR_VERSION 40
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
@@ -349,6 +355,7 @@ struct fuse_file_lock {
  * FOPEN_STREAM: the file is stream-like (no file position at all)
  * FOPEN_NOFLUSH: don't flush data cache on close (unless FUSE_WRITEBACK_CACHE)
  * FOPEN_PARALLEL_DIRECT_WRITES: Allow concurrent direct writes on the same inode
+ * FOPEN_PASSTHROUGH: passthrough read/write io for this open file
  */
 #define FOPEN_DIRECT_IO		(1 << 0)
 #define FOPEN_KEEP_CACHE	(1 << 1)
@@ -357,6 +364,7 @@ struct fuse_file_lock {
 #define FOPEN_STREAM		(1 << 4)
 #define FOPEN_NOFLUSH		(1 << 5)
 #define FOPEN_PARALLEL_DIRECT_WRITES	(1 << 6)
+#define FOPEN_PASSTHROUGH	(1 << 7)
 
 /**
  * INIT request/reply flags
@@ -406,6 +414,9 @@ struct fuse_file_lock {
  *			symlink and mknod (single group that matches parent)
  * FUSE_HAS_EXPIRE_ONLY: kernel supports expiry-only entry invalidation
  * FUSE_DIRECT_IO_ALLOW_MMAP: allow shared mmap in FOPEN_DIRECT_IO mode.
+ * FUSE_NO_EXPORT_SUPPORT: explicitly disable export support
+ * FUSE_HAS_RESEND: kernel supports resending pending requests, and the high bit
+ *		    of the request ID indicates resend requests
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -445,6 +456,9 @@ struct fuse_file_lock {
 #define FUSE_CREATE_SUPP_GROUP	(1ULL << 34)
 #define FUSE_HAS_EXPIRE_ONLY	(1ULL << 35)
 #define FUSE_DIRECT_IO_ALLOW_MMAP (1ULL << 36)
+#define FUSE_PASSTHROUGH	(1ULL << 37)
+#define FUSE_NO_EXPORT_SUPPORT	(1ULL << 38)
+#define FUSE_HAS_RESEND		(1ULL << 39)
 
 /* Obsolete alias for FUSE_DIRECT_IO_ALLOW_MMAP */
 #define FUSE_DIRECT_IO_RELAX	FUSE_DIRECT_IO_ALLOW_MMAP
@@ -631,6 +645,7 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_STORE = 4,
 	FUSE_NOTIFY_RETRIEVE = 5,
 	FUSE_NOTIFY_DELETE = 6,
+	FUSE_NOTIFY_RESEND = 7,
 	FUSE_NOTIFY_CODE_MAX,
 };
 
@@ -757,7 +772,7 @@ struct fuse_create_in {
 struct fuse_open_out {
 	uint64_t	fh;
 	uint32_t	open_flags;
-	uint32_t	padding;
+	int32_t		backing_id;
 };
 
 struct fuse_release_in {
@@ -873,7 +888,8 @@ struct fuse_init_out {
 	uint16_t	max_pages;
 	uint16_t	map_alignment;
 	uint32_t	flags2;
-	uint32_t	unused[7];
+	uint32_t	max_stack_depth;
+	uint32_t	unused[6];
 };
 
 #define CUSE_INIT_INFO_MAX 4096
@@ -956,6 +972,14 @@ struct fuse_fallocate_in {
 	uint32_t	padding;
 };
 
+/**
+ * FUSE request unique ID flag
+ *
+ * Indicates whether this is a resend request. The receiver should handle this
+ * request accordingly.
+ */
+#define FUSE_UNIQUE_RESEND (1ULL << 63)
+
 struct fuse_in_header {
 	uint32_t	len;
 	uint32_t	opcode;
@@ -1045,9 +1069,18 @@ struct fuse_notify_retrieve_in {
 	uint64_t	dummy4;
 };
 
+struct fuse_backing_map {
+	int32_t		fd;
+	uint32_t	flags;
+	uint64_t	padding;
+};
+
 /* Device ioctls: */
 #define FUSE_DEV_IOC_MAGIC		229
 #define FUSE_DEV_IOC_CLONE		_IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
+#define FUSE_DEV_IOC_BACKING_OPEN	_IOW(FUSE_DEV_IOC_MAGIC, 1, \
+					     struct fuse_backing_map)
+#define FUSE_DEV_IOC_BACKING_CLOSE	_IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
include/standard-headers/linux/input-event-codes.h +1 -0
index f6bab08540d8..2221b0c38348 100644
@@ -602,6 +602,7 @@
 
 #define KEY_ALS_TOGGLE		0x230	/* Ambient light sensor */
 #define KEY_ROTATE_LOCK_TOGGLE	0x231	/* Display rotation lock */
+#define KEY_REFRESH_RATE_TOGGLE	0x232	/* Display refresh rate toggle */
 
 #define KEY_BUTTONCONFIG		0x240	/* AL Button Configuration */
 #define KEY_TASKMANAGER		0x241	/* AL Task/Project Manager */
include/standard-headers/linux/virtio_gpu.h +2 -0
index 2da48d3d4c2c..2db643ed8fbf 100644
@@ -309,6 +309,8 @@ struct virtio_gpu_cmd_submit {
 
 #define VIRTIO_GPU_CAPSET_VIRGL 1
 #define VIRTIO_GPU_CAPSET_VIRGL2 2
+/* 3 is reserved for gfxstream */
+#define VIRTIO_GPU_CAPSET_VENUS 4
 
 /* VIRTIO_GPU_CMD_GET_CAPSET_INFO */
 struct virtio_gpu_get_capset_info {
include/standard-headers/linux/virtio_pci.h +5 -5
index 3e2bc2c97e6e..4010216103e5 100644
@@ -240,7 +240,7 @@ struct virtio_pci_cfg_cap {
 #define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ		0x5
 #define VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO		0x6
 
-struct QEMU_PACKED virtio_admin_cmd_hdr {
+struct virtio_admin_cmd_hdr {
 	uint16_t opcode;
 	/*
 	 * 1 - SR-IOV
@@ -252,20 +252,20 @@ struct QEMU_PACKED virtio_admin_cmd_hdr {
 	uint64_t group_member_id;
 };
 
-struct QEMU_PACKED virtio_admin_cmd_status {
+struct virtio_admin_cmd_status {
 	uint16_t status;
 	uint16_t status_qualifier;
 	/* Unused, reserved for future extensions. */
 	uint8_t reserved2[4];
 };
 
-struct QEMU_PACKED virtio_admin_cmd_legacy_wr_data {
+struct virtio_admin_cmd_legacy_wr_data {
 	uint8_t offset; /* Starting offset of the register(s) to write. */
 	uint8_t reserved[7];
 	uint8_t registers[];
 };
 
-struct QEMU_PACKED virtio_admin_cmd_legacy_rd_data {
+struct virtio_admin_cmd_legacy_rd_data {
 	uint8_t offset; /* Starting offset of the register(s) to read. */
 };
 
@@ -275,7 +275,7 @@ struct QEMU_PACKED virtio_admin_cmd_legacy_rd_data {
 
 #define VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO 4
 
-struct QEMU_PACKED virtio_admin_cmd_notify_info_data {
+struct virtio_admin_cmd_notify_info_data {
 	uint8_t flags; /* 0 = end of list, 1 = owner device, 2 = member device */
 	uint8_t bar; /* BAR of the member or the owner device */
 	uint8_t padding[6];
include/standard-headers/linux/virtio_snd.h +154 -0

182 lines not on this page · raw message

linux-headers/asm-arm64/kvm.h +9 -6
index c59ea55cd8eb..2af9931ae989 100644
@@ -37,9 +37,7 @@
 #include <asm/ptrace.h>
 #include <asm/sve_context.h>
 
-#define __KVM_HAVE_GUEST_DEBUG
 #define __KVM_HAVE_IRQ_LINE
-#define __KVM_HAVE_READONLY_MEM
 #define __KVM_HAVE_VCPU_EVENTS
 
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
@@ -76,11 +74,11 @@ struct kvm_regs {
 
 /* KVM_ARM_SET_DEVICE_ADDR ioctl id encoding */
 #define KVM_ARM_DEVICE_TYPE_SHIFT	0
-#define KVM_ARM_DEVICE_TYPE_MASK	GENMASK(KVM_ARM_DEVICE_TYPE_SHIFT + 15, \
-						KVM_ARM_DEVICE_TYPE_SHIFT)
+#define KVM_ARM_DEVICE_TYPE_MASK	__GENMASK(KVM_ARM_DEVICE_TYPE_SHIFT + 15, \
+						  KVM_ARM_DEVICE_TYPE_SHIFT)
 #define KVM_ARM_DEVICE_ID_SHIFT		16
-#define KVM_ARM_DEVICE_ID_MASK		GENMASK(KVM_ARM_DEVICE_ID_SHIFT + 15, \
-						KVM_ARM_DEVICE_ID_SHIFT)
+#define KVM_ARM_DEVICE_ID_MASK		__GENMASK(KVM_ARM_DEVICE_ID_SHIFT + 15, \
+						  KVM_ARM_DEVICE_ID_SHIFT)
 
 /* Supported device IDs */
 #define KVM_ARM_DEVICE_VGIC_V2		0
@@ -162,6 +160,11 @@ struct kvm_sync_regs {
 	__u64 device_irq_level;
 };
 
+/* Bits for run->s.regs.device_irq_level */
+#define KVM_ARM_DEV_EL1_VTIMER		(1 << 0)
+#define KVM_ARM_DEV_EL1_PTIMER		(1 << 1)
+#define KVM_ARM_DEV_PMU			(1 << 2)
+
 /*
  * PMU filter structure. Describe a range of events with a particular
  * action. To be used with KVM_ARM_VCPU_PMU_V3_FILTER.
linux-headers/asm-arm64/sve_context.h +11 -0
index 1d0e3e1d0950..d1b1ec8cb1f1 100644
@@ -13,6 +13,17 @@
 
 #define __SVE_VQ_BYTES		16	/* number of bytes per quadword */
 
+/*
+ * Yes, __SVE_VQ_MAX is 512 QUADWORDS.
+ *
+ * To help ensure forward portability, this is much larger than the
+ * current maximum value defined by the SVE architecture.  While arrays
+ * or static allocations can be sized based on this value, watch out!
+ * It will waste a surprisingly large amount of memory.
+ *
+ * Dynamic sizing based on the actual runtime vector length is likely to
+ * be preferable for most purposes.
+ */
 #define __SVE_VQ_MIN		1
 #define __SVE_VQ_MAX		512
 
linux-headers/asm-generic/bitsperlong.h +4 -0
index 75f320fa91e5..1fb4f0c9f278 100644
@@ -24,4 +24,8 @@
 #endif
 #endif
 
+#ifndef __BITS_PER_LONG_LONG
+#define __BITS_PER_LONG_LONG 64
+#endif
+
 #endif /* __ASM_GENERIC_BITS_PER_LONG */
linux-headers/asm-loongarch/kvm.h +0 -2
index 923d0bd38294..109785922cf9 100644
@@ -14,8 +14,6 @@
  * Some parts derived from the x86 version of this file.
  */
 
-#define __KVM_HAVE_READONLY_MEM
-
 #define KVM_COALESCED_MMIO_PAGE_OFFSET	1
 #define KVM_DIRTY_LOG_PAGE_OFFSET	64
 
linux-headers/asm-mips/kvm.h +0 -2
index edcf717c4327..9673dc9cb315 100644
@@ -20,8 +20,6 @@
  * Some parts derived from the x86 version of this file.
  */
 
-#define __KVM_HAVE_READONLY_MEM
-
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
 
 /*
linux-headers/asm-powerpc/kvm.h +44 -1

55 lines not on this page · raw message

linux-headers/asm-riscv/kvm.h +2 -1
index 7499e88a947c..b1c503c2959c 100644
@@ -16,7 +16,6 @@
 #include <asm/ptrace.h>
 
 #define __KVM_HAVE_IRQ_LINE
-#define __KVM_HAVE_READONLY_MEM
 
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
 
@@ -166,6 +165,8 @@ enum KVM_RISCV_ISA_EXT_ID {
 	KVM_RISCV_ISA_EXT_ZVFH,
 	KVM_RISCV_ISA_EXT_ZVFHMIN,
 	KVM_RISCV_ISA_EXT_ZFA,
+	KVM_RISCV_ISA_EXT_ZTSO,
+	KVM_RISCV_ISA_EXT_ZACAS,
 	KVM_RISCV_ISA_EXT_MAX,
 };
 
linux-headers/asm-s390/kvm.h +314 -1

321 lines not on this page · raw message

linux-headers/asm-x86/kvm.h +300 -8

346 lines not on this page · raw message

linux-headers/linux/bits.h new file +15 -0
new file mode 100644
index 000000000000..d9897771be8c
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/* bits.h: Macros for dealing with bitmasks.  */
+
+#ifndef _LINUX_BITS_H
+#define _LINUX_BITS_H
+
+#define __GENMASK(h, l) \
+        (((~_UL(0)) - (_UL(1) << (l)) + 1) & \
+         (~_UL(0) >> (__BITS_PER_LONG - 1 - (h))))
+
+#define __GENMASK_ULL(h, l) \
+        (((~_ULL(0)) - (_ULL(1) << (l)) + 1) & \
+         (~_ULL(0) >> (__BITS_PER_LONG_LONG - 1 - (h))))
+
+#endif /* _LINUX_BITS_H */
linux-headers/linux/kvm.h +5 -684

774 lines not on this page · raw message

linux-headers/linux/psp-sev.h +59 -0

77 lines not on this page · raw message

linux-headers/linux/vhost.h +7 -0
index 649560c685f1..bea697390613 100644
@@ -227,4 +227,11 @@
  */
 #define VHOST_VDPA_GET_VRING_DESC_GROUP	_IOWR(VHOST_VIRTIO, 0x7F,	\
 					      struct vhost_vring_state)
+
+/* Get the queue size of a specific virtqueue.
+ * userspace set the vring index in vhost_vring_state.index
+ * kernel set the queue size in vhost_vring_state.num
+ */
+#define VHOST_VDPA_GET_VRING_SIZE	_IOWR(VHOST_VIRTIO, 0x80,	\
+					      struct vhost_vring_state)
 #endif
scripts/update-linux-headers.sh +4 -1
index a0006eec6fd1..6430881eec50 100755
@@ -62,6 +62,7 @@ cp_portable() {
                                      -e 'linux/kernel' \
                                      -e 'linux/sysinfo' \
                                      -e 'asm-generic/kvm_para' \
+                                     -e 'asm-x86/setup_data.h' \
                                      > /dev/null
     then
         echo "Unexpected #include in input file $f".
@@ -149,9 +150,11 @@ for arch in $ARCHLIST; do
         cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
         cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
         cp_portable "$tmpdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch"
+        cp_portable "$tmpdir/include/asm/setup_data.h" "$output/include/standard-headers/asm-$arch"
         # Remove everything except the macros from bootparam.h avoiding the
         # unnecessary import of several video/ist/etc headers
         sed -e '/__ASSEMBLY__/,/__ASSEMBLY__/d' \
+            -e 's/<asm\/\([^>]*\)>/"standard-headers\/asm-x86\/\1"/' \
                "$tmpdir/include/asm/bootparam.h" > "$tmpdir/bootparam.h"
         cp_portable "$tmpdir/bootparam.h" \
                     "$output/include/standard-headers/asm-$arch"
@@ -165,7 +168,7 @@ rm -rf "$output/linux-headers/linux"
 mkdir -p "$output/linux-headers/linux"
 for header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \
               psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \
-              vduse.h iommufd.h; do
+              vduse.h iommufd.h bits.h; do
     cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
 done
 
Signature · 1 line
2.44.0
#2 lore raw

Re: [PATCH v8 01/11] linux-headers: Update to Linux v6.9-rc3

Quoted 40 lines · Dmitry Osipenko

Update kernel headers to get new VirtIO-GPU capsets, in particular the Venus capset.

---
 hw/i386/x86.c                                 |   8 -
 include/standard-headers/asm-x86/bootparam.h  |  17 +-
 include/standard-headers/asm-x86/kvm_para.h   |   3 +-
 include/standard-headers/asm-x86/setup_data.h |  83 +++
 include/standard-headers/linux/ethtool.h      |  48 ++
 include/standard-headers/linux/fuse.h         |  39 +-
 .../linux/input-event-codes.h                 |   1 +
 include/standard-headers/linux/virtio_gpu.h   |   2 +
 include/standard-headers/linux/virtio_pci.h   |  10 +-
 include/standard-headers/linux/virtio_snd.h   | 154 ++++
 linux-headers/asm-arm64/kvm.h                 |  15 +-
 linux-headers/asm-arm64/sve_context.h         |  11 +
 linux-headers/asm-generic/bitsperlong.h       |   4 +
 linux-headers/asm-loongarch/kvm.h             |   2 -
 linux-headers/asm-mips/kvm.h                  |   2 -
 linux-headers/asm-powerpc/kvm.h               |  45 +-
 linux-headers/asm-riscv/kvm.h                 |   3 +-
 linux-headers/asm-s390/kvm.h                  | 315 +++++++-
 linux-headers/asm-x86/kvm.h                   | 308 +++++++-
 linux-headers/linux/bits.h                    |  15 +
 linux-headers/linux/kvm.h                     | 689 +-----------------
 linux-headers/linux/psp-sev.h                 |  59 ++
 linux-headers/linux/vhost.h                   |   7 +
 scripts/update-linux-headers.sh               |   5 +-
 24 files changed, 1106 insertions(+), 739 deletions(-)
 create mode 100644 include/standard-headers/asm-x86/setup_data.h
 create mode 100644 linux-headers/linux/bits.h
hw/i386/x86.c +0 -0
index ffbda48917fd..84a48019770b 100644
@@ -679,14 +679,6 @@ DeviceState *ioapic_init_secondary(GSIState *gsi_state)
     return dev;

hw/i386/x86.c DeviceState *ioapic_init_secondary(GSIState *gsi_state) · line 689

Quoted 10 lines
hw/i386/x86.c +0 -8
 }
 
-struct setup_data {
-    uint64_t next;
-    uint32_t type;
-    uint32_t len;
-    uint8_t data[];
-} __attribute__((packed));
-
-

This isn't part of the header import. I'd rather see

- import the current header set
- update to the script
- clean-ups and additions

why are we migrating to using the kernels non-uapi assembler headers?

<snip>

scripts/update-linux-headers.sh cp_portable() { · line 65

Quoted 7 lines
scripts/update-linux-headers.sh +1 -0
@@ -62,6 +62,7 @@ cp_portable() {
                                      -e 'linux/kernel' \
                                      -e 'linux/sysinfo' \
                                      -e 'asm-generic/kvm_para' \
+                                     -e 'asm-x86/setup_data.h' \

some justification for this/

scripts/update-linux-headers.sh for arch in $ARCHLIST; do · line 153

Quoted 8 lines
scripts/update-linux-headers.sh +1 -0
                                      > /dev/null
     then
         echo "Unexpected #include in input file $f".
@@ -149,9 +150,11 @@ for arch in $ARCHLIST; do
         cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
         cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
         cp_portable "$tmpdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch"
+        cp_portable "$tmpdir/include/asm/setup_data.h" "$output/include/standard-headers/asm-$arch"

is there a portable setup_data.h? why is it asm-x86 above?

scripts/update-linux-headers.sh rm -rf "$output/linux-headers/linux" · line 171

Quoted 13 lines
scripts/update-linux-headers.sh +2 -1
         # Remove everything except the macros from bootparam.h avoiding the
         # unnecessary import of several video/ist/etc headers
         sed -e '/__ASSEMBLY__/,/__ASSEMBLY__/d' \
+            -e 's/<asm\/\([^>]*\)>/"standard-headers\/asm-x86\/\1"/' \
                "$tmpdir/include/asm/bootparam.h" > "$tmpdir/bootparam.h"
         cp_portable "$tmpdir/bootparam.h" \
                     "$output/include/standard-headers/asm-$arch"
@@ -165,7 +168,7 @@ rm -rf "$output/linux-headers/linux"
 mkdir -p "$output/linux-headers/linux"
 for header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \
               psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \
-              vduse.h iommufd.h; do
+              vduse.h iommufd.h bits.h; do

What do we need bits for here?

Quoted 2 lines
scripts/update-linux-headers.sh +0 -0
     cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
 done
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw

Re: [PATCH v8 01/11] linux-headers: Update to Linux v6.9-rc3

On 5/10/24 13:46, Alex Bennée wrote:
...

Quoted 4 lines
Quoted 2 lines

cp_portable "$tmpdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch"
+ cp_portable "$tmpdir/include/asm/setup_data.h" "$output/include/standard-headers/asm-$arch"

is there a portable setup_data.h? why is it asm-x86 above?

Yes, it shouldn't have been asm-x86

...

Quoted 6 lines

scripts/update-linux-headers.sh rm -rf "$output/linux-headers/linux" · line 171

Quoted 4 lines
 for header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \
               psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \
scripts/update-linux-headers.sh +1 -1
-              vduse.h iommufd.h; do
+              vduse.h iommufd.h bits.h; do

What do we need bits for here?

Some header started to include it

The kernel headers were already updated in Qemu and I dropped this patch from v9. No need to review it further, thanks!

Signature · 2 lines
Best regards,
Dmitry
#4 lore raw

PATCH v8 2/11 virtio-gpu: Use pkgconfig version to decide which virgl features are available

In reply to Dmitry Osipenko

New virglrerenderer features were stabilized with release of v1.0.0. Presence of symbols in virglrenderer.h doesn't guarantee ABI compatibility with pre-release development versions of libvirglerender. Use virglrenderer version to decide reliably which virgl features are available.

---
 meson.build | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
meson.build +2 -5
index 91a0aa64c640..cafc32521efb 100644
@@ -2286,11 +2286,8 @@ config_host_data.set('CONFIG_PNG', png.found())
 config_host_data.set('CONFIG_VNC', vnc.found())
 config_host_data.set('CONFIG_VNC_JPEG', jpeg.found())
 config_host_data.set('CONFIG_VNC_SASL', sasl.found())
-if virgl.found()
-  config_host_data.set('HAVE_VIRGL_D3D_INFO_EXT',
-                       cc.has_member('struct virgl_renderer_resource_info_ext', 'd3d_tex2d',
-                                     prefix: '#include <virglrenderer.h>',
-                                     dependencies: virgl))
+if virgl.version().version_compare('>=1.0.0')
+  config_host_data.set('HAVE_VIRGL_D3D_INFO_EXT', 1)
 endif
 config_host_data.set('CONFIG_VIRTFS', have_virtfs)
 config_host_data.set('CONFIG_VTE', vte.found())
Signature · 1 line
2.44.0
#5 lore raw

PATCH v8 3/11 virtio-gpu: Support context-init feature with virglrenderer

In reply to Dmitry Osipenko

From: Huang Rui <[email protected]>

Patch "virtio-gpu: CONTEXT_INIT feature" has added the context_init feature flags. Expose this feature and support creating virglrenderer context with flags using context_id if libvirglrenderer is new enough.

---
 hw/display/virtio-gpu-gl.c    |  4 ++++
 hw/display/virtio-gpu-virgl.c | 20 ++++++++++++++++++--
 meson.build                   |  1 +
 3 files changed, 23 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-gl.c +4 -0
index e06be60dfbfc..ba478124e2c2 100644
@@ -127,6 +127,10 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
     VIRTIO_GPU_BASE(g)->virtio_config.num_capsets =
         virtio_gpu_virgl_get_num_capsets(g);
 
+#ifdef HAVE_VIRGL_CONTEXT_CREATE_WITH_FLAGS
+    g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED;
+#endif
+
     virtio_gpu_device_realize(qdev, errp);
 }
 
hw/display/virtio-gpu-virgl.c +18 -2
index 9f34d0e6619c..ef598d8d23ee 100644
@@ -106,8 +106,24 @@ 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) {
+        if (!virtio_gpu_context_init_enabled(g->parent_obj.conf)) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: context_init disabled",
+                          __func__);
+            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+            return;
+        }
+
+#ifdef HAVE_VIRGL_CONTEXT_CREATE_WITH_FLAGS
+        virgl_renderer_context_create_with_flags(cc.hdr.ctx_id,
+                                                 cc.context_init,
+                                                 cc.nlen,
+                                                 cc.debug_name);
+        return;
+#endif
+    }
+
+    virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen, cc.debug_name);
 }
 
 static void virgl_cmd_context_destroy(VirtIOGPU *g,
meson.build +1 -0
index cafc32521efb..d71d33d69b45 100644
@@ -2288,6 +2288,7 @@ config_host_data.set('CONFIG_VNC_JPEG', jpeg.found())
 config_host_data.set('CONFIG_VNC_SASL', sasl.found())
 if virgl.version().version_compare('>=1.0.0')
   config_host_data.set('HAVE_VIRGL_D3D_INFO_EXT', 1)
+  config_host_data.set('HAVE_VIRGL_CONTEXT_CREATE_WITH_FLAGS', 1)
 endif
 config_host_data.set('CONFIG_VIRTFS', have_virtfs)
 config_host_data.set('CONFIG_VTE', vte.found())
Signature · 1 line
2.44.0
#6 lore raw

PATCH v8 4/11 virtio-gpu: Don't require udmabuf when blobs and virgl are enabled

In reply to Dmitry Osipenko

The udmabuf usage is mandatory when virgl is disabled and blobs feature enabled in the Qemu machine configuration. If virgl and blobs are enabled, then udmabuf requirement is optional. Since udmabuf isn't widely supported by a popular Linux distros today, let's relax the udmabuf requirement for blobs=on,virgl=on. Now, a full-featured virtio-gpu acceleration is available to Qemu users without a need to have udmabuf available in the system.

---
 hw/display/virtio-gpu.c | 1 +
 1 file changed, 1 insertion(+)
hw/display/virtio-gpu.c +1 -0
index ae831b6b3e3e..dac272ecadb1 100644
@@ -1472,6 +1472,7 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
 
     if (virtio_gpu_blob_enabled(g->parent_obj.conf)) {
         if (!virtio_gpu_rutabaga_enabled(g->parent_obj.conf) &&
+            !virtio_gpu_virgl_enabled(g->parent_obj.conf) &&
             !virtio_gpu_have_udmabuf()) {
             error_setg(errp, "need rutabaga or udmabuf for blob resources");
             return;
Signature · 1 line
2.44.0
#7 lore raw

PATCH v8 5/11 virtio-gpu: Add virgl resource management

In reply to Dmitry Osipenko

From: Huang Rui <[email protected]>

In a preparation to adding host blobs support to virtio-gpu, add virgl resource management that allows to retrieve resource based on its ID.

---
 hw/display/virtio-gpu-virgl.c | 57 +++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
hw/display/virtio-gpu-virgl.c +57 -0
index ef598d8d23ee..04f7a191c41a 100644
@@ -35,11 +35,34 @@ static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
 {
     struct virtio_gpu_resource_create_2d c2d;
     struct virgl_renderer_resource_create_args args;
+    struct virtio_gpu_simple_resource *res;
 
     VIRTIO_GPU_FILL_CMD(c2d);
     trace_virtio_gpu_cmd_res_create_2d(c2d.resource_id, c2d.format,
                                        c2d.width, c2d.height);
 
+    if (c2d.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, c2d.resource_id);
+    if (res) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already exists %d\n",
+                      __func__, c2d.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->width = c2d.width;
+    res->height = c2d.height;
+    res->format = c2d.format;
+    res->resource_id = c2d.resource_id;
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
+
     args.handle = c2d.resource_id;
     args.target = 2;
     args.format = c2d.format;
@@ -59,11 +82,34 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
 {
     struct virtio_gpu_resource_create_3d c3d;
     struct virgl_renderer_resource_create_args args;
+    struct virtio_gpu_simple_resource *res;
 
     VIRTIO_GPU_FILL_CMD(c3d);
     trace_virtio_gpu_cmd_res_create_3d(c3d.resource_id, c3d.format,
                                        c3d.width, c3d.height, c3d.depth);
 
+    if (c3d.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, c3d.resource_id);
+    if (res) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already exists %d\n",
+                      __func__, c3d.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->width = c3d.width;
+    res->height = c3d.height;
+    res->format = c3d.format;
+    res->resource_id = c3d.resource_id;
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
+
     args.handle = c3d.resource_id;
     args.target = c3d.target;
     args.format = c3d.format;
@@ -82,12 +128,19 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
                                      struct virtio_gpu_ctrl_command *cmd)
 {
     struct virtio_gpu_resource_unref unref;
+    struct virtio_gpu_simple_resource *res;
     struct iovec *res_iovs = NULL;
     int num_iovs = 0;
 
     VIRTIO_GPU_FILL_CMD(unref);
     trace_virtio_gpu_cmd_res_unref(unref.resource_id);
 
+    res = virtio_gpu_find_resource(g, unref.resource_id);
+    if (!res) {
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
     virgl_renderer_resource_detach_iov(unref.resource_id,
                                        &res_iovs,
                                        &num_iovs);
@@ -95,6 +148,10 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
         virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
     }
     virgl_renderer_resource_unref(unref.resource_id);
+
+    QTAILQ_REMOVE(&g->reslist, res, next);
+
+    g_free(res);
 }
 
 static void virgl_cmd_context_create(VirtIOGPU *g,
Signature · 1 line
2.44.0
#8 lore raw

PATCH v8 6/11 virtio-gpu: Support blob scanout using dmabuf fd

In reply to Dmitry Osipenko

From: Robert Beckett <[email protected]>

Support displaying blob resources by handling SET_SCANOUT_BLOB command.

---
 hw/display/virtio-gpu-virgl.c  | 109 +++++++++++++++++++++++++++++++++
 hw/display/virtio-gpu.c        |  12 ++--
 include/hw/virtio/virtio-gpu.h |   7 +++
 meson.build                    |   1 +
 4 files changed, 123 insertions(+), 6 deletions(-)
hw/display/virtio-gpu-virgl.c +109 -0
index 04f7a191c41a..c2057b0c2147 100644
@@ -17,6 +17,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-gpu-pixman.h"
 
 #include "ui/egl-helpers.h"
 
@@ -61,6 +63,7 @@ static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
     res->height = c2d.height;
     res->format = c2d.format;
     res->resource_id = c2d.resource_id;
+    res->dmabuf_fd = -1;
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 
     args.handle = c2d.resource_id;
@@ -108,6 +111,7 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
     res->height = c3d.height;
     res->format = c3d.format;
     res->resource_id = c3d.resource_id;
+    res->dmabuf_fd = -1;
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 
     args.handle = c3d.resource_id;
@@ -490,6 +494,106 @@ static void virgl_cmd_get_capset(VirtIOGPU *g,
     g_free(resp);
 }
 
+#ifdef HAVE_VIRGL_RESOURCE_BLOB
+static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
+                                       struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_framebuffer fb = { 0 };
+    struct virgl_renderer_resource_info info;
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_set_scanout_blob ss;
+    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;
+    }
+
+    res = virtio_gpu_find_resource(g, ss.resource_id);
+    if (!res) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource does not exist %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: resource does not have info %d\n",
+                      __func__, ss.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+    if (res->dmabuf_fd < 0) {
+        res->dmabuf_fd = info.fd;
+    }
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource not backed by dmabuf %d\n",
+                      __func__, ss.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    fb.format = virtio_gpu_get_pixman_format(ss.format);
+    if (!fb.format) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: pixel format not supported %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, &fb, &ss.r);
+}
+#endif /* HAVE_VIRGL_RESOURCE_BLOB */
+
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                       struct virtio_gpu_ctrl_command *cmd)
 {
@@ -556,6 +660,11 @@ 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_SET_SCANOUT_BLOB:
+        virgl_cmd_set_scanout_blob(g, cmd);
+        break;
+#endif /* HAVE_VIRGL_RESOURCE_BLOB */
     default:
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         break;
hw/display/virtio-gpu.c +6 -6
index dac272ecadb1..1e57a53d346c 100644
@@ -380,7 +380,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;
@@ -597,11 +597,11 @@ static void virtio_unref_resource(pixman_image_t *image, void *data)
     pixman_image_unref(data);
 }
 
-static void virtio_gpu_update_scanout(VirtIOGPU *g,
-                                      uint32_t scanout_id,
-                                      struct virtio_gpu_simple_resource *res,
-                                      struct virtio_gpu_framebuffer *fb,
-                                      struct virtio_gpu_rect *r)
+void virtio_gpu_update_scanout(VirtIOGPU *g,
+                               uint32_t scanout_id,
+                               struct virtio_gpu_simple_resource *res,
+                               struct virtio_gpu_framebuffer *fb,
+                               struct virtio_gpu_rect *r)
 {
     struct virtio_gpu_simple_resource *ores;
     struct virtio_gpu_scanout *scanout;
include/hw/virtio/virtio-gpu.h +7 -0
index ed44cdad6b34..44c676c3ca4a 100644
@@ -329,6 +329,13 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g,
                              struct virtio_gpu_framebuffer *fb,
                              struct virtio_gpu_rect *r);
 
+void virtio_gpu_update_scanout(VirtIOGPU *g,
+                               uint32_t scanout_id,
+                               struct virtio_gpu_simple_resource *res,
+                               struct virtio_gpu_framebuffer *fb,
+                               struct virtio_gpu_rect *r);
+void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id);
+
 /* virtio-gpu-3d.c */
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                   struct virtio_gpu_ctrl_command *cmd);
meson.build +1 -0
index d71d33d69b45..3ade035e8300 100644
@@ -2289,6 +2289,7 @@ config_host_data.set('CONFIG_VNC_SASL', sasl.found())
 if virgl.version().version_compare('>=1.0.0')
   config_host_data.set('HAVE_VIRGL_D3D_INFO_EXT', 1)
   config_host_data.set('HAVE_VIRGL_CONTEXT_CREATE_WITH_FLAGS', 1)
+  config_host_data.set('HAVE_VIRGL_RESOURCE_BLOB', 1)
 endif
 config_host_data.set('CONFIG_VIRTFS', have_virtfs)
 config_host_data.set('CONFIG_VTE', vte.found())
Signature · 1 line
2.44.0
#9 lore raw

PATCH v8 7/11 virtio-gpu: Support suspension of commands processing

In reply to Dmitry Osipenko

Add new "suspended" flag to virtio_gpu_ctrl_command telling cmd processor that it should stop processing commands and retry again next time until flag is unset.

---
 hw/display/virtio-gpu-gl.c       | 1 +
 hw/display/virtio-gpu-rutabaga.c | 1 +
 hw/display/virtio-gpu-virgl.c    | 3 +++
 hw/display/virtio-gpu.c          | 5 +++++
 include/hw/virtio/virtio-gpu.h   | 1 +
 5 files changed, 11 insertions(+)
hw/display/virtio-gpu-gl.c +1 -0
index ba478124e2c2..a8892bcc5346 100644
@@ -79,6 +79,7 @@ static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
         cmd->vq = vq;
         cmd->error = 0;
         cmd->finished = false;
+        cmd->suspended = false;
         QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
         cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
     }
hw/display/virtio-gpu-rutabaga.c +1 -0
index 17bf701a2163..b6e84d436fb2 100644
@@ -1061,6 +1061,7 @@ static void virtio_gpu_rutabaga_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
         cmd->vq = vq;
         cmd->error = 0;
         cmd->finished = false;
+        cmd->suspended = false;
         QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
         cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
     }
hw/display/virtio-gpu-virgl.c +3 -0
index c2057b0c2147..bb9ee1eba9a0 100644
@@ -670,6 +670,9 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
         break;
     }
 
+    if (cmd->suspended) {
+        return;
+    }
     if (cmd->finished) {
         return;
     }
hw/display/virtio-gpu.c +5 -0
index 1e57a53d346c..a1bd4d6914c4 100644
@@ -1054,6 +1054,10 @@ void virtio_gpu_process_cmdq(VirtIOGPU *g)
         /* process command */
         vgc->process_cmd(g, cmd);
 
+        if (cmd->suspended) {
+            break;
+        }
+
         QTAILQ_REMOVE(&g->cmdq, cmd, next);
         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
             g->stats.requests++;
@@ -1113,6 +1117,7 @@ static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
         cmd->vq = vq;
         cmd->error = 0;
         cmd->finished = false;
+        cmd->suspended = false;
         QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
         cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
     }
include/hw/virtio/virtio-gpu.h +1 -0
index 44c676c3ca4a..dc24360656ce 100644
@@ -132,6 +132,7 @@ struct virtio_gpu_ctrl_command {
     struct virtio_gpu_ctrl_hdr cmd_hdr;
     uint32_t error;
     bool finished;
+    bool suspended;
     QTAILQ_ENTRY(virtio_gpu_ctrl_command) next;
 };
 
Signature · 1 line
2.44.0
#10 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 5 lines · Dmitry Osipenko

Add new "suspended" flag to virtio_gpu_ctrl_command telling cmd processor that it should stop processing commands and retry again next time until flag is unset.

This flag shouldn't be added to virtio_gpu_ctrl_command. suspended is just !finished in virtio-gpu.c. Only virtio_gpu_virgl_process_cmd() needs the distinction of suspended and !finished so it is not appropriate to add this flag the common structure.

Regards,
Akihiko Odaki

#11 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 11 lines · Akihiko Odaki
Quoted 5 lines · Dmitry Osipenko

Add new "suspended" flag to virtio_gpu_ctrl_command telling cmd processor that it should stop processing commands and retry again next time until flag is unset.

This flag shouldn't be added to virtio_gpu_ctrl_command. suspended is just !finished in virtio-gpu.c. Only virtio_gpu_virgl_process_cmd() needs the distinction of suspended and !finished so it is not appropriate to add this flag the common structure.

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO.

The flag reflects the exact command status. The !finished + !suspended means that command is fenced, i.e. these flags don't have exactly same meaning.

I'd keep the flag if there are no better suggestions.

Signature · 2 lines
Best regards,
Dmitry
#12 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 13 lines · Dmitry Osipenko
Quoted 11 lines · Akihiko Odaki
Quoted 5 lines · Dmitry Osipenko

Add new "suspended" flag to virtio_gpu_ctrl_command telling cmd processor that it should stop processing commands and retry again next time until flag is unset.

This flag shouldn't be added to virtio_gpu_ctrl_command. suspended is just !finished in virtio-gpu.c. Only virtio_gpu_virgl_process_cmd() needs the distinction of suspended and !finished so it is not appropriate to add this flag the common structure.

Quoted 7 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended means that command is fenced, i.e. these flags don't have exactly same meaning.

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Quoted 3 lines

I'd keep the flag if there are no better suggestions.

#13 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 11 lines · Akihiko Odaki
Quoted 8 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended means that command is fenced, i.e. these flags don't have exactly same meaning.

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Not sure what you're meaning. The !finished says that cmd is fenced, this fenced command is added to the polling list and the fence is checked periodically by the fence_poll timer, meanwhile next virgl commands are executed in the same time.

This is completely different from the suspension where whole cmd processing is blocked until command is resumed.

Signature · 2 lines
Best regards,
Dmitry
#14 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 13 lines · Dmitry Osipenko
Quoted 11 lines · Akihiko Odaki
Quoted 8 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended means that command is fenced, i.e. these flags don't have exactly same meaning.

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Quoted 8 lines

Not sure what you're meaning. The !finished says that cmd is fenced, this fenced command is added to the polling list and the fence is checked periodically by the fence_poll timer, meanwhile next virgl commands are executed in the same time.

This is completely different from the suspension where whole cmd processing is blocked until command is resumed.

!finished means you have not sent a response with virtio_gpu_ctrl_response(). Currently such a situation only happens when a fence is requested and virtio_gpu_process_cmdq() exploits the fact, but we are adding a new case without a fence.

So we need to add code to check if we are fencing or not in virtio_gpu_process_cmdq(). This can be achieved by evaluating the following expression as done in virtio_gpu_virgl_process_cmd():
cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE

#15 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 28 lines · Akihiko Odaki
Quoted 13 lines · Dmitry Osipenko
Quoted 11 lines · Akihiko Odaki
Quoted 8 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended means that command is fenced, i.e. these flags don't have exactly same meaning.

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Quoted 8 lines

Not sure what you're meaning. The !finished says that cmd is fenced, this fenced command is added to the polling list and the fence is checked periodically by the fence_poll timer, meanwhile next virgl commands are executed in the same time.

This is completely different from the suspension where whole cmd processing is blocked until command is resumed.

!finished means you have not sent a response with virtio_gpu_ctrl_response(). Currently such a situation only happens when a fence is requested and virtio_gpu_process_cmdq() exploits the fact, but we are adding a new case without a fence.

Quoted 4 lines

So we need to add code to check if we are fencing or not in virtio_gpu_process_cmdq(). This can be achieved by evaluating the following expression as done in virtio_gpu_virgl_process_cmd():
cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE

This works, but then I'll add back the res->async_unmap_in_progress because we need to know whether unmapping has been started.

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

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 29 lines · Dmitry Osipenko
Quoted 28 lines · Akihiko Odaki
Quoted 13 lines · Dmitry Osipenko
Quoted 11 lines · Akihiko Odaki
Quoted 8 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended means that command is fenced, i.e. these flags don't have exactly same meaning.

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Quoted 8 lines

Not sure what you're meaning. The !finished says that cmd is fenced, this fenced command is added to the polling list and the fence is checked periodically by the fence_poll timer, meanwhile next virgl commands are executed in the same time.

This is completely different from the suspension where whole cmd processing is blocked until command is resumed.

!finished means you have not sent a response with virtio_gpu_ctrl_response(). Currently such a situation only happens when a fence is requested and virtio_gpu_process_cmdq() exploits the fact, but we are adding a new case without a fence.

Quoted 8 lines
Quoted 4 lines

So we need to add code to check if we are fencing or not in virtio_gpu_process_cmdq(). This can be achieved by evaluating the following expression as done in virtio_gpu_virgl_process_cmd():
cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE

This works, but then I'll add back the res->async_unmap_in_progress because we need to know whether unmapping has been started.

Isn't the command processing paused when an unmapping operation is in progress?

#17 lore raw

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 38 lines · Akihiko Odaki
Quoted 37 lines · Dmitry Osipenko
Quoted 35 lines · Akihiko Odaki
Quoted 16 lines · Dmitry Osipenko
Quoted 12 lines · Akihiko Odaki
Quoted 11 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by
every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended
means that command is fenced, i.e. these flags don't have exactly same
meaning.

Quoted 2 lines

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Quoted 8 lines

Not sure what you're meaning. The !finished says that cmd is fenced, this fenced command is added to the polling list and the fence is checked periodically by the fence_poll timer, meanwhile next virgl commands are executed in the same time.

This is completely different from the suspension where whole cmd processing is blocked until command is resumed.

!finished means you have not sent a response with virtio_gpu_ctrl_response(). Currently such a situation only happens when a fence is requested and virtio_gpu_process_cmdq() exploits the fact, but we are adding a new case without a fence.

So we need to add code to check if we are fencing or not in virtio_gpu_process_cmdq(). This can be achieved by evaluating the following expression as done in virtio_gpu_virgl_process_cmd():
cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE

Quoted 6 lines
Quoted 3 lines

This works, but then I'll add back the res->async_unmap_in_progress because we need to know whether unmapping has been started.

Isn't the command processing paused when an unmapping operation is in progress?

The virtio_gpu_process_cmdq() continues to be invoked periodically while unmapping is paused. Should be console doing that, see virtio_gpu_handle_gl_flushed().

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

Re: [PATCH v8 07/11] virtio-gpu: Support suspension of commands processing

Quoted 43 lines · Dmitry Osipenko
Quoted 42 lines · Akihiko Odaki
Quoted 40 lines · Dmitry Osipenko
Quoted 31 lines · Akihiko Odaki
Quoted 16 lines · Dmitry Osipenko
Quoted 12 lines · Akihiko Odaki
Quoted 11 lines

The VIRTIO_GPU_FILL_CMD() macro returns void and this macro is used by
every function processing commands. Changing process_cmd() to return bool will require to change all those functions. Not worthwhile to change it, IMO. >
The flag reflects the exact command status. The !finished + !suspended
means that command is fenced, i.e. these flags don't have exactly same
meaning.

Quoted 2 lines

It is not necessary to change the signature of process_cmd(). You can just refer to !finished. No need to have the suspended flag.

Quoted 8 lines

Not sure what you're meaning. The !finished says that cmd is fenced, this fenced command is added to the polling list and the fence is checked periodically by the fence_poll timer, meanwhile next virgl commands are executed in the same time.

This is completely different from the suspension where whole cmd processing is blocked until command is resumed.

!finished means you have not sent a response with virtio_gpu_ctrl_response(). Currently such a situation only happens when a fence is requested and virtio_gpu_process_cmdq() exploits the fact, but we are adding a new case without a fence.

Quoted 4 lines

So we need to add code to check if we are fencing or not in virtio_gpu_process_cmdq(). This can be achieved by evaluating the following expression as done in virtio_gpu_virgl_process_cmd():
cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE

This works, but then I'll add back the res->async_unmap_in_progress because we need to know whether unmapping has been started.

Quoted 6 lines
Quoted 2 lines

Isn't the command processing paused when an unmapping operation is in progress?

The virtio_gpu_process_cmdq() continues to be invoked periodically while unmapping is paused. Should be console doing that, see virtio_gpu_handle_gl_flushed().

Though, we're now blocking the render, and thus, virtio_gpu_process_cmdq() won't do anything while cmd is paused. I'll check that nothing else is missed and then won't add async_unmap_in_progress in v11, thanks!

Signature · 2 lines
Best regards,
Dmitry
#19 lore raw

PATCH v8 8/11 virtio-gpu: Handle resource blob commands

In reply to Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

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

---
 hw/display/virtio-gpu-virgl.c  | 248 +++++++++++++++++++++++++++++++++
 hw/display/virtio-gpu.c        |   4 +-
 include/hw/virtio/virtio-gpu.h |   4 +
 3 files changed, 255 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +248 -0
index bb9ee1eba9a0..de132b22f554 100644
@@ -32,6 +32,102 @@ virgl_get_egl_display(G_GNUC_UNUSED void *cookie)
 }
 #endif
 
+#ifdef HAVE_VIRGL_RESOURCE_BLOB
+struct virtio_gpu_virgl_hostmem_region {
+    MemoryRegion mr;
+    VirtIOGPUBase *b;
+    struct virtio_gpu_simple_resource *res;
+};
+
+static void virtio_gpu_virgl_hostmem_region_free(void *obj)
+{
+    MemoryRegion *mr = MEMORY_REGION(obj);
+    struct virtio_gpu_virgl_hostmem_region *vmr;
+
+    vmr = container_of(mr, struct virtio_gpu_virgl_hostmem_region, mr);
+    vmr->res->async_unmap_in_progress = false;
+    vmr->res->async_unmap_completed = true;
+    vmr->b->renderer_blocked--;
+
+    g_free(vmr);
+}
+
+static int
+virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
+                                   struct virtio_gpu_simple_resource *res,
+                                   uint64_t offset)
+{
+    struct virtio_gpu_virgl_hostmem_region *vmr;
+    VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+    uint64_t size;
+    void *data;
+    int ret;
+
+    if (!virtio_gpu_hostmem_enabled(b->conf)) {
+        return -EOPNOTSUPP;
+    }
+
+    ret = virgl_renderer_resource_map(res->resource_id, &data, &size);
+    if (ret) {
+        return -ret;
+    }
+
+    vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
+    MemoryRegion *mr = &vmr->mr;
+    vmr->res = res;
+    vmr->b = b;
+
+    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_add_subregion(&b->hostmem, offset, mr);
+    memory_region_set_enabled(mr, true);
+
+    /*
+     * Potentially, MR could outlive the resource if MR's reference is held
+     * outside of virtio-gpu. In order to prevent unmapping resource while
+     * MR is alive, and thus, making the data pointer invalid, we will block
+     * virtio-gpu command processing until MR is fully unreferenced and
+     * released.
+     */
+    OBJECT(mr)->free = virtio_gpu_virgl_hostmem_region_free;
+
+    res->mr = mr;
+
+    return 0;
+}
+
+static bool
+virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
+                                     struct virtio_gpu_simple_resource *res)
+{
+    VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+
+    if (!res->async_unmap_in_progress && !res->async_unmap_completed) {
+        /* memory region owns self res->mr object and frees it by itself */
+        MemoryRegion *mr = res->mr;
+        res->mr = NULL;
+
+        res->async_unmap_in_progress = true;
+
+        /* render will be unblocked when MR is freed */
+        b->renderer_blocked++;
+
+        memory_region_set_enabled(mr, false);
+        memory_region_del_subregion(&b->hostmem, mr);
+        object_unparent(OBJECT(mr));
+    }
+
+    if (!res->async_unmap_completed) {
+        return false;
+    }
+
+    virgl_renderer_resource_unmap(res->resource_id);
+    res->async_unmap_completed = false;
+
+    return true;
+
+}
+#endif /* HAVE_VIRGL_RESOURCE_BLOB */
+
 static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
                                          struct virtio_gpu_ctrl_command *cmd)
 {
@@ -145,6 +241,14 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
         return;
     }
 
+    if (res->mr || cmd->suspended) {
+        bool unmapped = virtio_gpu_virgl_unmap_resource_blob(g, res);
+        cmd->suspended = !unmapped;
+        if (cmd->suspended) {
+            return;
+        }
+    }
+
     virgl_renderer_resource_detach_iov(unref.resource_id,
                                        &res_iovs,
                                        &num_iovs);
@@ -495,6 +599,141 @@ static void virgl_cmd_get_capset(VirtIOGPU *g,
 }
 
 #ifdef HAVE_VIRGL_RESOURCE_BLOB
+static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
+                                           struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virgl_renderer_resource_create_blob_args virgl_args = { 0 };
+    struct virtio_gpu_resource_create_blob cblob;
+    struct virtio_gpu_simple_resource *res;
+    int ret;
+
+    if (!virtio_gpu_blob_enabled(g->parent_obj.conf)) {
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
+    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);
+    res->resource_id = cblob.resource_id;
+    res->blob_size = cblob.size;
+    res->dmabuf_fd = -1;
+
+    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) {
+            g_free(res);
+            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+            return;
+        }
+    }
+
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
+
+    virgl_args.res_handle = cblob.resource_id;
+    virgl_args.ctx_id = cblob.hdr.ctx_id;
+    virgl_args.blob_mem = cblob.blob_mem;
+    virgl_args.blob_id = cblob.blob_id;
+    virgl_args.blob_flags = cblob.blob_flags;
+    virgl_args.size = cblob.size;
+    virgl_args.iovecs = res->iov;
+    virgl_args.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_resource_map_blob mblob;
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_resp_map_info resp;
+    int ret;
+
+    VIRTIO_GPU_FILL_CMD(mblob);
+    virtio_gpu_map_blob_bswap(&mblob);
+
+    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;
+    }
+
+    if (res->mr) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already mapped %d\n",
+                      __func__, mblob.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    ret = virtio_gpu_virgl_map_resource_blob(g, res, mblob.offset);
+    if (ret) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource map error: %s\n",
+                      __func__, strerror(ret));
+        cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
+        return;
+    }
+
+    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));
+}
+
+static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
+                                          struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_resource_unmap_blob ublob;
+    struct virtio_gpu_simple_resource *res;
+
+    VIRTIO_GPU_FILL_CMD(ublob);
+    virtio_gpu_unmap_blob_bswap(&ublob);
+
+    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;
+    }
+
+    if (!res->mr && !cmd->suspended) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already unmapped %d\n",
+                      __func__, ublob.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    bool unmapped = virtio_gpu_virgl_unmap_resource_blob(g, res);
+    cmd->suspended = !unmapped;
+}
+
 static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
                                        struct virtio_gpu_ctrl_command *cmd)
 {
@@ -661,6 +900,15 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
         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;
     case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
         virgl_cmd_set_scanout_blob(g, cmd);
         break;
hw/display/virtio-gpu.c +3 -1
index a1bd4d6914c4..45c1f2006712 100644
@@ -1483,10 +1483,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, "old virglrenderer, blob resources unsupported");
             return;
         }
+#endif
     }
 
     if (!virtio_gpu_base_device_realize(qdev,
include/hw/virtio/virtio-gpu.h +4 -0
index dc24360656ce..b9d5e106f3c5 100644
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
     int dmabuf_fd;
     uint8_t *remapped;
 
+    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
 };
 
Signature · 1 line
2.44.0
#20 lore raw

Re: [PATCH v8 08/11] virtio-gpu: Handle resource blob commands

Quoted 30 lines · Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

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

---
  hw/display/virtio-gpu-virgl.c  | 248 +++++++++++++++++++++++++++++++++
  hw/display/virtio-gpu.c        |   4 +-
  include/hw/virtio/virtio-gpu.h |   4 +
  3 files changed, 255 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +6 -0
index bb9ee1eba9a0..de132b22f554 100644
@@ -32,6 +32,102 @@ virgl_get_egl_display(G_GNUC_UNUSED void *cookie)
  }
  #endif
 
+#ifdef HAVE_VIRGL_RESOURCE_BLOB
+struct virtio_gpu_virgl_hostmem_region {
+    MemoryRegion mr;
+    VirtIOGPUBase *b;
+    struct virtio_gpu_simple_resource *res;
+};

hw/display/virtio-gpu-virgl.c virgl_get_egl_display(G_GNUC_UNUSED void *cookie) · line 50

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+
+static void virtio_gpu_virgl_hostmem_region_free(void *obj)
+{
+    MemoryRegion *mr = MEMORY_REGION(obj);
+    struct virtio_gpu_virgl_hostmem_region *vmr;
+
+    vmr = container_of(mr, struct virtio_gpu_virgl_hostmem_region, mr);
+    vmr->res->async_unmap_in_progress = false;
+    vmr->res->async_unmap_completed = true;
+    vmr->b->renderer_blocked--;

Resume the command queue processing here.

Quoted 7 lines
hw/display/virtio-gpu-virgl.c +7 -0
+
+    g_free(vmr);
+}
+
+static int
+virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
+                                   struct virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-virgl.c virgl_get_egl_display(G_GNUC_UNUSED void *cookie) · line 67

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+                                   uint64_t offset)
+{
+    struct virtio_gpu_virgl_hostmem_region *vmr;
+    VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+    uint64_t size;
+    void *data;
+    int ret;
+
+    if (!virtio_gpu_hostmem_enabled(b->conf)) {
+        return -EOPNOTSUPP;

Log a message here instead of picking an error number.

hw/display/virtio-gpu-virgl.c virgl_get_egl_display(G_GNUC_UNUSED void *cookie) · line 76

Quoted 9 lines
hw/display/virtio-gpu-virgl.c +9 -0
+    }
+
+    ret = virgl_renderer_resource_map(res->resource_id, &data, &size);
+    if (ret) {
+        return -ret;
+    }
+
+    vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
+    MemoryRegion *mr = &vmr->mr;

Mixed declarations are not allowed; see: docs/devel/style.rst

Quoted 33 lines
hw/display/virtio-gpu-virgl.c +33 -0
+    vmr->res = res;
+    vmr->b = b;
+
+    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_add_subregion(&b->hostmem, offset, mr);
+    memory_region_set_enabled(mr, true);
+
+    /*
+     * Potentially, MR could outlive the resource if MR's reference is held
+     * outside of virtio-gpu. In order to prevent unmapping resource while
+     * MR is alive, and thus, making the data pointer invalid, we will block
+     * virtio-gpu command processing until MR is fully unreferenced and
+     * released.
+     */
+    OBJECT(mr)->free = virtio_gpu_virgl_hostmem_region_free;
+
+    res->mr = mr;
+
+    return 0;
+}
+
+static bool
+virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
+                                     struct virtio_gpu_simple_resource *res)
+{
+    VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+
+    if (!res->async_unmap_in_progress && !res->async_unmap_completed) {
+        /* memory region owns self res->mr object and frees it by itself */
+        MemoryRegion *mr = res->mr;
+        res->mr = NULL;
+
+        res->async_unmap_in_progress = true;

hw/display/virtio-gpu-virgl.c virgl_get_egl_display(G_GNUC_UNUSED void *cookie) · line 119

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+
+        /* render will be unblocked when MR is freed */
+        b->renderer_blocked++;
+
+        memory_region_set_enabled(mr, false);
+        memory_region_del_subregion(&b->hostmem, mr);
+        object_unparent(OBJECT(mr));
+    }
+
+    if (!res->async_unmap_completed) {

This check is unnecessary as the command processing is blocked until the unmap operation completes.

Quoted 210 lines
hw/display/virtio-gpu-virgl.c +163 -0
+        return false;
+    }
+
+    virgl_renderer_resource_unmap(res->resource_id);
+    res->async_unmap_completed = false;
+
+    return true;
+
+}
+#endif /* HAVE_VIRGL_RESOURCE_BLOB */
+
  static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
                                           struct virtio_gpu_ctrl_command *cmd)
  {
@@ -145,6 +241,14 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
          return;
      }
 
+    if (res->mr || cmd->suspended) {
+        bool unmapped = virtio_gpu_virgl_unmap_resource_blob(g, res);
+        cmd->suspended = !unmapped;
+        if (cmd->suspended) {
+            return;
+        }
+    }
+
      virgl_renderer_resource_detach_iov(unref.resource_id,
                                         &res_iovs,
                                         &num_iovs);
@@ -495,6 +599,141 @@ static void virgl_cmd_get_capset(VirtIOGPU *g,
  }
 
  #ifdef HAVE_VIRGL_RESOURCE_BLOB
+static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
+                                           struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virgl_renderer_resource_create_blob_args virgl_args = { 0 };
+    struct virtio_gpu_resource_create_blob cblob;
+    struct virtio_gpu_simple_resource *res;
+    int ret;
+
+    if (!virtio_gpu_blob_enabled(g->parent_obj.conf)) {
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+    }
+
+    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);
+    res->resource_id = cblob.resource_id;
+    res->blob_size = cblob.size;
+    res->dmabuf_fd = -1;
+
+    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) {
+            g_free(res);
+            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+            return;
+        }
+    }
+
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
+
+    virgl_args.res_handle = cblob.resource_id;
+    virgl_args.ctx_id = cblob.hdr.ctx_id;
+    virgl_args.blob_mem = cblob.blob_mem;
+    virgl_args.blob_id = cblob.blob_id;
+    virgl_args.blob_flags = cblob.blob_flags;
+    virgl_args.size = cblob.size;
+    virgl_args.iovecs = res->iov;
+    virgl_args.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_resource_map_blob mblob;
+    struct virtio_gpu_simple_resource *res;
+    struct virtio_gpu_resp_map_info resp;
+    int ret;
+
+    VIRTIO_GPU_FILL_CMD(mblob);
+    virtio_gpu_map_blob_bswap(&mblob);
+
+    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;
+    }
+
+    if (res->mr) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already mapped %d\n",
+                      __func__, mblob.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    ret = virtio_gpu_virgl_map_resource_blob(g, res, mblob.offset);
+    if (ret) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource map error: %s\n",
+                      __func__, strerror(ret));
+        cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
+        return;
+    }
+
+    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));
+}
+
+static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
+                                          struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_resource_unmap_blob ublob;
+    struct virtio_gpu_simple_resource *res;
+
+    VIRTIO_GPU_FILL_CMD(ublob);
+    virtio_gpu_unmap_blob_bswap(&ublob);
+
+    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;
+    }
+
+    if (!res->mr && !cmd->suspended) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already unmapped %d\n",
+                      __func__, ublob.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    bool unmapped = virtio_gpu_virgl_unmap_resource_blob(g, res);
+    cmd->suspended = !unmapped;
+}
+
  static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
                                         struct virtio_gpu_ctrl_command *cmd)
  {
@@ -661,6 +900,15 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
          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;
      case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
          virgl_cmd_set_scanout_blob(g, cmd);
          break;
hw/display/virtio-gpu.c +3 -1
index a1bd4d6914c4..45c1f2006712 100644
@@ -1483,10 +1483,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, "old virglrenderer, blob resources unsupported");
              return;
          }
+#endif
      }
 
      if (!virtio_gpu_base_device_realize(qdev,
include/hw/virtio/virtio-gpu.h +0 -0
index dc24360656ce..b9d5e106f3c5 100644

include/hw/virtio/virtio-gpu.h struct virtio_gpu_simple_resource { · line 67

Quoted 7 lines
include/hw/virtio/virtio-gpu.h +4 -0
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
      int dmabuf_fd;
      uint8_t *remapped;
 
+    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+

Don't add fields to virtio_gpu_simple_resource but instead create a struct that embeds virtio_gpu_simple_resource in virtio-gpu-virgl.c.

Quoted 3 lines
include/hw/virtio/virtio-gpu.h +0 -0
      QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
  };
 
#21 lore raw

Re: [PATCH v8 08/11] virtio-gpu: Handle resource blob commands

Quoted 10 lines · Akihiko Odaki

include/hw/virtio/virtio-gpu.h struct virtio_gpu_simple_resource { · line 66

Quoted 7 lines
include/hw/virtio/virtio-gpu.h +3 -0
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
       int dmabuf_fd;
       uint8_t *remapped;
   +    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+

Don't add fields to virtio_gpu_simple_resource but instead create a struct that embeds virtio_gpu_simple_resource in virtio-gpu-virgl.c.

Please give a justification. I'd rather rename virtio_gpu_simple_resource s/_simple//. Simple resource already supports blob and the added fields are directly related to the blob. Don't see why another struct is needed.

Signature · 2 lines
Best regards,
Dmitry
#22 lore raw

Re: [PATCH v8 08/11] virtio-gpu: Handle resource blob commands

Quoted 12 lines · Dmitry Osipenko
Quoted 10 lines · Akihiko Odaki

include/hw/virtio/virtio-gpu.h struct virtio_gpu_simple_resource { · line 66

Quoted 7 lines
include/hw/virtio/virtio-gpu.h +3 -0
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
       int dmabuf_fd;
       uint8_t *remapped;
   +    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+

Don't add fields to virtio_gpu_simple_resource but instead create a struct that embeds virtio_gpu_simple_resource in virtio-gpu-virgl.c.

Quoted 5 lines

Please give a justification. I'd rather rename virtio_gpu_simple_resource s/_simple//. Simple resource already supports blob and the added fields are directly related to the blob. Don't see why another struct is needed.

Because mapping is only implemented in virtio-gpu-gl while blob itself is implemented also in virtio-gpu.

#23 lore raw

Re: [PATCH v8 08/11] virtio-gpu: Handle resource blob commands

Quoted 13 lines · Akihiko Odaki
Quoted 12 lines · Dmitry Osipenko
Quoted 10 lines · Akihiko Odaki

include/hw/virtio/virtio-gpu.h struct virtio_gpu_simple_resource { · line 66

Quoted 7 lines
include/hw/virtio/virtio-gpu.h +3 -0
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
        int dmabuf_fd;
        uint8_t *remapped;
    +    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+

Don't add fields to virtio_gpu_simple_resource but instead create a struct that embeds virtio_gpu_simple_resource in virtio-gpu-virgl.c.

Quoted 8 lines
Quoted 5 lines

Please give a justification. I'd rather rename virtio_gpu_simple_resource s/_simple//. Simple resource already supports blob and the added fields are directly related to the blob. Don't see why another struct is needed.

Because mapping is only implemented in virtio-gpu-gl while blob itself is implemented also in virtio-gpu.

Rutubaga maps blobs and it should do unmapping blobs asynchronously as well, AFAICT.

Signature · 2 lines
Best regards,
Dmitry
#24 lore raw

Re: [PATCH v8 08/11] virtio-gpu: Handle resource blob commands

Quoted 20 lines · Dmitry Osipenko
Quoted 19 lines · Akihiko Odaki
Quoted 17 lines · Dmitry Osipenko
Quoted 10 lines · Akihiko Odaki

include/hw/virtio/virtio-gpu.h struct virtio_gpu_simple_resource { · line 66

Quoted 7 lines
include/hw/virtio/virtio-gpu.h +3 -0
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
        int dmabuf_fd;
        uint8_t *remapped;
    +    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+

Don't add fields to virtio_gpu_simple_resource but instead create a struct that embeds virtio_gpu_simple_resource in virtio-gpu-virgl.c.

Please give a justification. I'd rather rename virtio_gpu_simple_resource s/_simple//. Simple resource already supports blob and the added fields are directly related to the blob. Don't see why another struct is needed.

Quoted 6 lines
Quoted 2 lines

Because mapping is only implemented in virtio-gpu-gl while blob itself is implemented also in virtio-gpu.

Rutubaga maps blobs and it should do unmapping blobs asynchronously as well, AFAICT.

Right. It makes sense to put mr in struct virtio_gpu_simple_resource in preparation for such a situation.

Based on this discussion, I think it is fine to put mr either in struct virtio_gpu_simple_resource or a distinct struct. However if you put mr in struct virtio_gpu_simple_resource, the logic that manages MemoryRegion should also be moved to virtio-gpu.c for consistency.

#25 lore raw

Re: [PATCH v8 08/11] virtio-gpu: Handle resource blob commands

Quoted 29 lines · Akihiko Odaki
Quoted 21 lines · Dmitry Osipenko
Quoted 20 lines · Akihiko Odaki
Quoted 18 lines · Dmitry Osipenko
Quoted 10 lines · Akihiko Odaki

include/hw/virtio/virtio-gpu.h struct virtio_gpu_simple_resource { · line 66

Quoted 7 lines
include/hw/virtio/virtio-gpu.h +3 -0
@@ -61,6 +61,10 @@ struct virtio_gpu_simple_resource {
         int dmabuf_fd;
         uint8_t *remapped;
     +    MemoryRegion *mr;
+    bool async_unmap_completed;
+    bool async_unmap_in_progress;
+

Don't add fields to virtio_gpu_simple_resource but instead create a struct that embeds virtio_gpu_simple_resource in virtio-gpu-virgl.c.

Please give a justification. I'd rather rename virtio_gpu_simple_resource s/_simple//. Simple resource already supports
blob and the added fields are directly related to the blob. Don't see why another struct is needed.

Quoted 6 lines
Quoted 2 lines

Because mapping is only implemented in virtio-gpu-gl while blob itself is implemented also in virtio-gpu.

Rutubaga maps blobs and it should do unmapping blobs asynchronously as well, AFAICT.

Quoted 7 lines

Right. It makes sense to put mr in struct virtio_gpu_simple_resource in preparation for such a situation.

Based on this discussion, I think it is fine to put mr either in struct virtio_gpu_simple_resource or a distinct struct. However if you put mr in struct virtio_gpu_simple_resource, the logic that manages MemoryRegion should also be moved to virtio-gpu.c for consistency.

Rutabaga uses static MRs. It will either need a different workaround or will have to move to dynamic MRs. I'll keep using distinct struct for now.

AFAICT, its a lesser problem for rutabaga because static MR isn't subjected to the dynamic MR UAF problem that virgl has. On the other hand, rutabaga is re-initing already inited static MR object on each new mapping, that looks like a bug and it will need to move to dynamic MRs.

Signature · 2 lines
Best regards,
Dmitry
#26 lore raw

PATCH v8 9/11 virtio-gpu: Resource UUID

In reply to Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

Enable resource UUID feature and implement command resource assign UUID. UUID feature availability is mandatory for Vulkan Venus context.

UUID is intended for sharing dmabufs between virtio devices on host. Qemu doesn't have second virtio device for sharing, thus a simple stub UUID implementation is enough. More complete implementation using global UUID resource table might become interesting for a multi-gpu cases.

---
 hw/display/trace-events        |  1 +
 hw/display/virtio-gpu-base.c   |  1 +
 hw/display/virtio-gpu-virgl.c  | 31 +++++++++++++++++++++++++++++++
 hw/display/virtio-gpu.c        |  5 +++++
 include/hw/virtio/virtio-gpu.h |  3 +++
 5 files changed, 41 insertions(+)
hw/display/trace-events +1 -0
index 2336a0ca1570..54d6894c59f4 100644
@@ -41,6 +41,7 @@ virtio_gpu_cmd_res_create_blob(uint32_t res, uint64_t size) "res 0x%x, size %" P
 virtio_gpu_cmd_res_unref(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_back_attach(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_back_detach(uint32_t res) "res 0x%x"
+virtio_gpu_cmd_res_assign_uuid(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_xfer_toh_2d(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_xfer_toh_3d(uint32_t res) "res 0x%x"
 virtio_gpu_cmd_res_xfer_fromh_3d(uint32_t res) "res 0x%x"
hw/display/virtio-gpu-base.c +1 -0
index 4fc7ef8896c1..610926348bd9 100644
@@ -225,6 +225,7 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t features,
     if (virtio_gpu_virgl_enabled(g->conf) ||
         virtio_gpu_rutabaga_enabled(g->conf)) {
         features |= (1 << VIRTIO_GPU_F_VIRGL);
+        features |= (1 << VIRTIO_GPU_F_RESOURCE_UUID);
     }
     if (virtio_gpu_edid_enabled(g->conf)) {
         features |= (1 << VIRTIO_GPU_F_EDID);
hw/display/virtio-gpu-virgl.c +31 -0
index de132b22f554..eee3816b987f 100644
@@ -160,6 +160,7 @@ static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
     res->format = c2d.format;
     res->resource_id = c2d.resource_id;
     res->dmabuf_fd = -1;
+    qemu_uuid_generate(&res->uuid);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 
     args.handle = c2d.resource_id;
@@ -208,6 +209,7 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
     res->format = c3d.format;
     res->resource_id = c3d.resource_id;
     res->dmabuf_fd = -1;
+    qemu_uuid_generate(&res->uuid);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 
     args.handle = c3d.resource_id;
@@ -635,6 +637,7 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
     res->resource_id = cblob.resource_id;
     res->blob_size = cblob.size;
     res->dmabuf_fd = -1;
+    qemu_uuid_generate(&res->uuid);
 
     if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
         ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
@@ -833,6 +836,31 @@ static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
 }
 #endif /* HAVE_VIRGL_RESOURCE_BLOB */
 
+static void virgl_cmd_assign_uuid(VirtIOGPU *g,
+                                  struct virtio_gpu_ctrl_command *cmd)
+{
+    struct virtio_gpu_resource_assign_uuid assign;
+    struct virtio_gpu_resp_resource_uuid resp;
+    struct virtio_gpu_simple_resource *res;
+
+    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_resource(g, assign.resource_id);
+    if (!res) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: resource does not exist %d\n",
+                      __func__, assign.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
+    }
+
+    memset(&resp, 0, sizeof(resp));
+    resp.hdr.type = VIRTIO_GPU_RESP_OK_RESOURCE_UUID;
+    memcpy(resp.uuid, res->uuid.data, sizeof(resp.uuid));
+    virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
+}
+
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                       struct virtio_gpu_ctrl_command *cmd)
 {
@@ -887,6 +915,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:
+        virgl_cmd_assign_uuid(g, cmd);
+        break;
     case VIRTIO_GPU_CMD_GET_CAPSET_INFO:
         virgl_cmd_get_capset_info(g, cmd);
         break;
hw/display/virtio-gpu.c +5 -0
index 45c1f2006712..fbf5c0e6b8b7 100644
@@ -1236,6 +1236,7 @@ static int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
         }
         qemu_put_buffer(f, (void *)pixman_image_get_data(res->image),
                         pixman_image_get_stride(res->image) * res->height);
+        qemu_put_buffer(f, res->uuid.data, sizeof(res->uuid.data));
     }
     qemu_put_be32(f, 0); /* end of list */
 
@@ -1333,6 +1334,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
         }
         qemu_get_buffer(f, (void *)pixman_image_get_data(res->image),
                         pixman_image_get_stride(res->image) * res->height);
+        qemu_get_buffer(f, res->uuid.data, sizeof(res->uuid.data));
 
         if (!virtio_gpu_load_restore_mapping(g, res)) {
             pixman_image_unref(res->image);
@@ -1371,6 +1373,7 @@ static int virtio_gpu_blob_save(QEMUFile *f, void *opaque, size_t size,
             qemu_put_be64(f, res->addrs[i]);
             qemu_put_be32(f, res->iov[i].iov_len);
         }
+        qemu_put_buffer(f, res->uuid.data, sizeof(res->uuid.data));
     }
     qemu_put_be32(f, 0); /* end of list */
 
@@ -1405,6 +1408,8 @@ static int virtio_gpu_blob_load(QEMUFile *f, void *opaque, size_t size,
             res->iov[i].iov_len = qemu_get_be32(f);
         }
 
+        qemu_get_buffer(f, res->uuid.data, sizeof(res->uuid.data));
+
         if (!virtio_gpu_load_restore_mapping(g, res)) {
             g_free(res);
             return -EINVAL;
include/hw/virtio/virtio-gpu.h +3 -0
index b9d5e106f3c5..d2a0d542fbb3 100644
@@ -19,6 +19,7 @@
 #include "ui/console.h"
 #include "hw/virtio/virtio.h"
 #include "qemu/log.h"
+#include "qemu/uuid.h"
 #include "sysemu/vhost-user-backend.h"
 
 #include "standard-headers/linux/virtio_gpu.h"
@@ -65,6 +66,8 @@ struct virtio_gpu_simple_resource {
     bool async_unmap_completed;
     bool async_unmap_in_progress;
 
+    QemuUUID uuid;
+
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
 };
 
Signature · 1 line
2.44.0
#27 lore raw

Re: [PATCH v8 09/11] virtio-gpu: Resource UUID

Quoted 9 lines · Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

Enable resource UUID feature and implement command resource assign UUID. UUID feature availability is mandatory for Vulkan Venus context.

UUID is intended for sharing dmabufs between virtio devices on host. Qemu doesn't have second virtio device for sharing, thus a simple stub UUID implementation is enough. More complete implementation using global UUID resource table might become interesting for a multi-gpu cases.

Isn't it possible to add two virtio-gpu devices even now?

A new subsection should also be added for migration compatibility; see: docs/devel/migration/main.rst

#28 lore raw

Re: [PATCH v8 09/11] virtio-gpu: Resource UUID

Quoted 6 lines · Akihiko Odaki
Quoted 5 lines · Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

Enable resource UUID feature and implement command resource assign UUID. UUID feature availability is mandatory for Vulkan Venus context.

Quoted 6 lines
Quoted 4 lines

UUID is intended for sharing dmabufs between virtio devices on host. Qemu doesn't have second virtio device for sharing, thus a simple stub UUID implementation is enough. More complete implementation using global UUID resource table might become interesting for a multi-gpu cases.

Isn't it possible to add two virtio-gpu devices even now?

We can add two virtio-gpu devices, but these devices can't interact with each other efficiently. They won't be able to share host blob resources without proper UUID implementation.

Quoted 2 lines

A new subsection should also be added for migration compatibility; see:
docs/devel/migration/main.rst

Will update the docs, thanks.

Signature · 2 lines
Best regards,
Dmitry
#29 lore raw

Re: [PATCH v8 09/11] virtio-gpu: Resource UUID

In reply to Dmitry Osipenko

hw/display/virtio-gpu.c static int virtio_gpu_blob_load(QEMUFile *f, void *opaque, size_t size, · line 1411

Quoted 5 lines · Dmitry Osipenko
hw/display/virtio-gpu.c +1 -0
@@ -1405,6 +1408,8 @@ static int virtio_gpu_blob_load(QEMUFile *f, void *opaque, size_t size,
             res->iov[i].iov_len = qemu_get_be32(f);
         }
 
+        qemu_get_buffer(f, res->uuid.data, sizeof(res->uuid.data));

Save/loading uuid without changing vm version was a bad idea. Will drop it in v9, we don't need to save/load uuid for virgl anyways.

Signature · 2 lines
Best regards,
Dmitry
#30 lore raw

PATCH v8 10/11 virtio-gpu: Register capsets dynamically

In reply to Dmitry Osipenko

From: Pierre-Eric Pelloux-Prayer <[email protected]>

virtio_gpu_virgl_get_num_capsets will return "num_capsets", but we can't assume that capset_index 1 is always VIRGL2 once we'll support more capsets, like Venus and DRM capsets. Register capsets dynamically to avoid that problem.

---
 hw/display/virtio-gpu-virgl.c  | 37 ++++++++++++++++++++++------------
 include/hw/virtio/virtio-gpu.h |  3 +++
 2 files changed, 27 insertions(+), 13 deletions(-)
hw/display/virtio-gpu-virgl.c +24 -13
index eee3816b987f..c0e1ca3ff339 100644
@@ -558,19 +558,12 @@ static void virgl_cmd_get_capset_info(VirtIOGPU *g,
     VIRTIO_GPU_FILL_CMD(info);
 
     memset(&resp, 0, sizeof(resp));
-    if (info.capset_index == 0) {
-        resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL;
-        virgl_renderer_get_cap_set(resp.capset_id,
-                                   &resp.capset_max_version,
-                                   &resp.capset_max_size);
-    } else if (info.capset_index == 1) {
-        resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL2;
+
+    if (info.capset_index < g->num_capsets) {
+        resp.capset_id = g->capset_ids[info.capset_index];
         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;
     }
     resp.hdr.type = VIRTIO_GPU_RESP_OK_CAPSET_INFO;
     virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
@@ -1120,12 +1113,30 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     return 0;
 }
 
+static void virtio_gpu_virgl_add_capset(VirtIOGPU *g, uint32_t capset_id)
+{
+    g->capset_ids = g_realloc_n(g->capset_ids, g->num_capsets + 1,
+                                sizeof(*g->capset_ids));
+    g->capset_ids[g->num_capsets++] = capset_id;
+}
+
 int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
 {
     uint32_t capset2_max_ver, capset2_max_size;
+
+    if (g->num_capsets) {
+        return g->num_capsets;
+    }
+
+    /* VIRGL is always supported. */
+    virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VIRGL);
+
     virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VIRGL2,
-                              &capset2_max_ver,
-                              &capset2_max_size);
+                               &capset2_max_ver,
+                               &capset2_max_size);
+    if (capset2_max_ver) {
+        virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VIRGL2);
+    }
 
-    return capset2_max_ver ? 2 : 1;
+    return g->num_capsets;
 }
include/hw/virtio/virtio-gpu.h +3 -0
index d2a0d542fbb3..3d7d001a85c5 100644
@@ -218,6 +218,9 @@ struct VirtIOGPU {
         QTAILQ_HEAD(, VGPUDMABuf) bufs;
         VGPUDMABuf *primary[VIRTIO_GPU_MAX_SCANOUTS];
     } dmabuf;
+
+    uint32_t *capset_ids;
+    uint32_t num_capsets;
 };
 
 struct VirtIOGPUClass {
Signature · 1 line
2.44.0
#31 lore raw

Re: [PATCH v8 10/11] virtio-gpu: Register capsets dynamically

Quoted 79 lines · Dmitry Osipenko

From: Pierre-Eric Pelloux-Prayer <[email protected]>

virtio_gpu_virgl_get_num_capsets will return "num_capsets", but we can't assume that capset_index 1 is always VIRGL2 once we'll support more capsets, like Venus and DRM capsets. Register capsets dynamically to avoid that problem.

---
  hw/display/virtio-gpu-virgl.c  | 37 ++++++++++++++++++++++------------
  include/hw/virtio/virtio-gpu.h |  3 +++
  2 files changed, 27 insertions(+), 13 deletions(-)
hw/display/virtio-gpu-virgl.c +24 -13
index eee3816b987f..c0e1ca3ff339 100644
@@ -558,19 +558,12 @@ static void virgl_cmd_get_capset_info(VirtIOGPU *g,
      VIRTIO_GPU_FILL_CMD(info);
 
      memset(&resp, 0, sizeof(resp));
-    if (info.capset_index == 0) {
-        resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL;
-        virgl_renderer_get_cap_set(resp.capset_id,
-                                   &resp.capset_max_version,
-                                   &resp.capset_max_size);
-    } else if (info.capset_index == 1) {
-        resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL2;
+
+    if (info.capset_index < g->num_capsets) {
+        resp.capset_id = g->capset_ids[info.capset_index];
          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;
      }
      resp.hdr.type = VIRTIO_GPU_RESP_OK_CAPSET_INFO;
      virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
@@ -1120,12 +1113,30 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      return 0;
  }
 
+static void virtio_gpu_virgl_add_capset(VirtIOGPU *g, uint32_t capset_id)
+{
+    g->capset_ids = g_realloc_n(g->capset_ids, g->num_capsets + 1,
+                                sizeof(*g->capset_ids));
+    g->capset_ids[g->num_capsets++] = capset_id;
+}
+
  int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
  {
      uint32_t capset2_max_ver, capset2_max_size;
+
+    if (g->num_capsets) {
+        return g->num_capsets;
+    }
+
+    /* VIRGL is always supported. */
+    virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VIRGL);
+
      virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VIRGL2,
-                              &capset2_max_ver,
-                              &capset2_max_size);
+                               &capset2_max_ver,
+                               &capset2_max_size);
+    if (capset2_max_ver) {
+        virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VIRGL2);
+    }
 
-    return capset2_max_ver ? 2 : 1;
+    return g->num_capsets;
  }
include/hw/virtio/virtio-gpu.h +0 -0
index d2a0d542fbb3..3d7d001a85c5 100644

include/hw/virtio/virtio-gpu.h struct VirtIOGPU { · line 223

Quoted 6 lines
include/hw/virtio/virtio-gpu.h +3 -0
@@ -218,6 +218,9 @@ struct VirtIOGPU {
          QTAILQ_HEAD(, VGPUDMABuf) bufs;
          VGPUDMABuf *primary[VIRTIO_GPU_MAX_SCANOUTS];
      } dmabuf;
+
+    uint32_t *capset_ids;
+    uint32_t num_capsets;

Use GArray.

#32 lore raw

PATCH v8 11/11 virtio-gpu: Support Venus context

In reply to Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL and if vulkan=true flag is set for virtio-gpu device.

---
 hw/display/virtio-gpu-virgl.c  | 14 ++++++++++++++
 hw/display/virtio-gpu.c        | 15 +++++++++++++++
 include/hw/virtio/virtio-gpu.h |  3 +++
 meson.build                    |  1 +
 4 files changed, 33 insertions(+)
hw/display/virtio-gpu-virgl.c +14 -0
index c0e1ca3ff339..2eac09370b84 100644
@@ -1095,6 +1095,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
         flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
     }
 #endif
+#ifdef VIRGL_RENDERER_VENUS
+    if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
+        flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
+    }
+#endif
 
     ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
     if (ret != 0) {
@@ -1138,5 +1143,14 @@ int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
         virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VIRGL2);
     }
 
+    if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
+        virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VENUS,
+                                   &capset2_max_ver,
+                                   &capset2_max_size);
+        if (capset2_max_size) {
+            virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VENUS);
+        }
+    }
+
     return g->num_capsets;
 }
hw/display/virtio-gpu.c +15 -0
index fbf5c0e6b8b7..a811a86dd600 100644
@@ -1496,6 +1496,19 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
 #endif
     }
 
+    if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
+#ifdef HAVE_VIRGL_VENUS
+        if (!virtio_gpu_blob_enabled(g->parent_obj.conf) ||
+            !virtio_gpu_hostmem_enabled(g->parent_obj.conf)) {
+            error_setg(errp, "venus requires enabled blob and hostmem options");
+            return;
+        }
+#else
+        error_setg(errp, "old virglrenderer, venus unsupported");
+        return;
+#endif
+    }
+
     if (!virtio_gpu_base_device_realize(qdev,
                                         virtio_gpu_handle_ctrl_cb,
                                         virtio_gpu_handle_cursor_cb,
@@ -1672,6 +1685,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("vulkan", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_VENUS_ENABLED, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
include/hw/virtio/virtio-gpu.h +3 -0
index 3d7d001a85c5..87d812972988 100644
@@ -106,6 +106,7 @@ enum virtio_gpu_base_conf_flags {
     VIRTIO_GPU_FLAG_BLOB_ENABLED,
     VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED,
     VIRTIO_GPU_FLAG_RUTABAGA_ENABLED,
+    VIRTIO_GPU_FLAG_VENUS_ENABLED,
 };
 
 #define virtio_gpu_virgl_enabled(_cfg) \
@@ -124,6 +125,8 @@ enum virtio_gpu_base_conf_flags {
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_RUTABAGA_ENABLED))
 #define virtio_gpu_hostmem_enabled(_cfg) \
     (_cfg.hostmem > 0)
+#define virtio_gpu_venus_enabled(_cfg) \
+    (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VENUS_ENABLED))
 
 struct virtio_gpu_base_conf {
     uint32_t max_outputs;
meson.build +1 -0
index 3ade035e8300..30c4eaa43de0 100644
@@ -2290,6 +2290,7 @@ if virgl.version().version_compare('>=1.0.0')
   config_host_data.set('HAVE_VIRGL_D3D_INFO_EXT', 1)
   config_host_data.set('HAVE_VIRGL_CONTEXT_CREATE_WITH_FLAGS', 1)
   config_host_data.set('HAVE_VIRGL_RESOURCE_BLOB', 1)
+  config_host_data.set('HAVE_VIRGL_VENUS', 1)
 endif
 config_host_data.set('CONFIG_VIRTFS', have_virtfs)
 config_host_data.set('CONFIG_VTE', vte.found())
Signature · 1 line
2.44.0
#33 lore raw

Re: [PATCH v8 11/11] virtio-gpu: Support Venus context

Quoted 32 lines · Dmitry Osipenko

From: Antonio Caggiano <[email protected]>

Request Venus when initializing VirGL and if vulkan=true flag is set for virtio-gpu device.

---
  hw/display/virtio-gpu-virgl.c  | 14 ++++++++++++++
  hw/display/virtio-gpu.c        | 15 +++++++++++++++
  include/hw/virtio/virtio-gpu.h |  3 +++
  meson.build                    |  1 +
  4 files changed, 33 insertions(+)
hw/display/virtio-gpu-virgl.c +5 -0
index c0e1ca3ff339..2eac09370b84 100644
@@ -1095,6 +1095,11 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
          flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
      }
  #endif
+#ifdef VIRGL_RENDERER_VENUS
+    if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
+        flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
+    }
+#endif
 
      ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
      if (ret != 0) {

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

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +5 -0
@@ -1138,5 +1143,14 @@ int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
          virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VIRGL2);
      }
 
+    if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
+        virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VENUS,
+                                   &capset2_max_ver,
+                                   &capset2_max_size);
+        if (capset2_max_size) {

Now capset2_max_ver and capset2_max_size are misnomers as they are used not only for VIRTIO_GPU_CAPSET_VIRGL2 but also VIRTIO_GPU_CAPSET_VENUS. Just removing the "capset2_" prefix would be fine.

Quoted 30 lines
hw/display/virtio-gpu-virgl.c +4 -0
+            virtio_gpu_virgl_add_capset(g, VIRTIO_GPU_CAPSET_VENUS);
+        }
+    }
+
      return g->num_capsets;
  }
hw/display/virtio-gpu.c +13 -0
index fbf5c0e6b8b7..a811a86dd600 100644
@@ -1496,6 +1496,19 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
  #endif
      }
 
+    if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
+#ifdef HAVE_VIRGL_VENUS
+        if (!virtio_gpu_blob_enabled(g->parent_obj.conf) ||
+            !virtio_gpu_hostmem_enabled(g->parent_obj.conf)) {
+            error_setg(errp, "venus requires enabled blob and hostmem options");
+            return;
+        }
+#else
+        error_setg(errp, "old virglrenderer, venus unsupported");
+        return;
+#endif
+    }
+ >       if (!virtio_gpu_base_device_realize(qdev,
                                          virtio_gpu_handle_ctrl_cb,
                                          virtio_gpu_handle_cursor_cb,

hw/display/virtio-gpu.c static Property virtio_gpu_properties[] = { · line 1689

Quoted 5 lines
hw/display/virtio-gpu.c +2 -0
@@ -1672,6 +1685,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("vulkan", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_VENUS_ENABLED, false),

This property shouldn't be added here because it is specific to virtio-gpu-gl.

#34 lore raw
In reply to Dmitry Osipenko
Quoted 4 lines · Dmitry Osipenko

Hello,

This series enables Vulkan Venus context support on virtio-gpu.

Quoted 8 lines

All virglrender and almost all Linux kernel prerequisite changes needed by Venus are already in upstream. For kernel there is a pending KVM patchset that fixes mapping of compound pages needed for DRM drivers using TTM [1], othewrwise hostmem blob mapping will fail with a KVM error from Qemu.

[1]
https://lore.kernel.org/kvm/[email protected]/

Following the link for the TTM/KVM patches on the kernel side points at changes for AMD cards getting NAK'ed so I'm a little confused as to what parts are needed.

Is this only relevant for ensuring the virtual mappings to the underlying hardware aren't moved around when KVM is exporting those pages to the guest?

Our interest is in Xen which obviously mediates everything through stage 2 mappings to from the real PA to the IPA the domains see. However AIUI all the blob allocation is managed by the GEM/TTM layer of whichever kernel is responsible for driving the GPU. Does this layer work with kernel vaddr or the underlying IPA of the resources? We shouldn't expect the IPA to change between allocations should we?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#35 lore raw
Quoted 12 lines · Alex Bennée
Quoted 10 lines · Dmitry Osipenko

Hello,

This series enables Vulkan Venus context support on virtio-gpu.

All virglrender and almost all Linux kernel prerequisite changes needed by Venus are already in upstream. For kernel there is a pending KVM patchset that fixes mapping of compound pages needed for DRM drivers using TTM [1], othewrwise hostmem blob mapping will fail with a KVM error from Qemu.

Quoted 6 lines
Quoted 2 lines

[1]
https://lore.kernel.org/kvm/[email protected]/

Following the link for the TTM/KVM patches on the kernel side points at changes for AMD cards getting NAK'ed so I'm a little confused as to what parts are needed.

I wouldn't say that patches are NAK'ed, they more having a problem with getting a review. Without KMV patches host blobs don't work depending on a host GPU driver and kernel configuration.

It's actually not only TTM drivers that are requiring the KVM changes, but a non-TTM GPU drivers that use huge pages may also need them too. You may need a patched KVM for i915 driver that doesn't use TTM, depending on whether transparent huge pages are enabled in the kernel config.

Quoted 3 lines

Is this only relevant for ensuring the virtual mappings to the underlying hardware aren't moved around when KVM is exporting those pages to the guest?

Yes, host GPU driver needs to handle guest access page fault to keep pages in place.

Quoted 6 lines

Our interest is in Xen which obviously mediates everything through stage 2 mappings to from the real PA to the IPA the domains see. However AIUI all the blob allocation is managed by the GEM/TTM layer of whichever kernel is responsible for driving the GPU. Does this layer work with kernel vaddr or the underlying IPA of the resources? We shouldn't expect the IPA to change between allocations should we?

TTM works with memory pages and it moves pages around. It may swap out pages and then relies on a working page faulting notification to swap-in pages back.

Whether PA stays fixed, I don't know for sure. Robert Beckett or somebody from AMD should know better how it works for Xen and may comment on it.

Signature · 2 lines
Best regards,
Dmitry