vfio/pci: Introduce x-pci-class-code option
PATCH v3 1/1 vfio/pci: Introduce x-pci-class-code option
Introduce x-pci-class-code option to allow users to override PCI class code of a device, similar to the existing x-pci-vendor-id option. Only the lower 24 bits of this option are used, though a uint32 is used here for determining whether the value is valid and set by user.
Additionally, to ensure VGA ranges are only exposed on VGA devices, pci_register_vga() is now called in vfio_pci_config_setup(), after the class code override is completed.
This is mainly intended for IGD devices that expose themselves either as VGA controller (primary display) or Display controller (non-primary display). The UEFI GOP driver depends on the device reporting a VGA controller class code (0x030000).
---
v3:
* Save the vdev class code when it is unspecified.
* Call pci_register_vga() in vfio_pci_config_setup() to ensure VGA regions are only get exposed when vdev is VGA.
* Check vdev class code in vfio_is_vga().
v2:
* Add vdev class code check in vfio_populate_vga().
* Fix type in trace-events.
hw/vfio/pci.c | 29 +++++++++++++++++++++++++---- hw/vfio/pci.h | 6 ++---- hw/vfio/trace-events | 1 + 3 files changed, 28 insertions(+), 8 deletions(-)
hw/vfio/pci.c +25 -4
@@ -2893,10 +2893,6 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)"vfio-vga-io@0x3c0" , QEMU_PCI_VGA_IO_HI_SIZE); -pci_register_vga (&vdev->pdev, &vdev->vga->region[QEMU_PCI_VGA_MEM ].mem, - &vdev->vga->region[QEMU_PCI_VGA_IO_LO ].mem, - &vdev->vga->region[QEMU_PCI_VGA_IO_HI ].mem); -return true; }
@@ -3228,6 +3224,23 @@ bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp) vdev->sub_device_id); } +/* +* Class code is a 24-bit value at config space 0x09. Allow overriding it +* with any 24-bit value. +*/ +if (vdev->class_code !=PCI_ANY_ID ) { +if (vdev->class_code >0xffffff ) { +error_setg (errp,"invalid PCI class code provided" ); +return false; + } +/* Higher 24 bits of PCI_CLASS_REVISION are class code */ +vfio_add_emulated_long (vdev,PCI_CLASS_REVISION , + vdev->class_code <<8 , ~0xff ); +trace_vfio_pci_emulated_class_code (vbasedev->name, vdev->class_code); + }else { + vdev->class_code =pci_get_long (pdev->config +PCI_CLASS_REVISION ) >>8 ; + } +/* QEMU can change multi-function devices to single function, or reverse */ vdev->emulated_config_bits[PCI_HEADER_TYPE ] =PCI_HEADER_TYPE_MULTI_FUNCTION ;
@@ -3257,6 +3270,12 @@ bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)vfio_bars_register (vdev); +if (vdev->vga &&vfio_is_vga (vdev)) { +pci_register_vga (&vdev->pdev, &vdev->vga->region[QEMU_PCI_VGA_MEM ].mem, + &vdev->vga->region[QEMU_PCI_VGA_IO_LO ].mem, + &vdev->vga->region[QEMU_PCI_VGA_IO_HI ].mem); + } +return true; }
@@ -3643,6 +3662,8 @@ static const Property vfio_pci_dev_properties[] = { sub_vendor_id, PCI_ANY_ID),DEFINE_PROP_UINT32 ("x-pci-sub-device-id" , VFIOPCIDevice, sub_device_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-class-code" , VFIOPCIDevice, + class_code,PCI_ANY_ID ),DEFINE_PROP_UINT32 ("x-igd-gms" , VFIOPCIDevice, igd_gms,0 ), DEFINE_PROP_UNSIGNED_NODEFAULT("x-nv-gpudirect-clique", VFIOPCIDevice, nv_gpudirect_clique,
hw/vfio/pci.h +2 -4
@@ -157,6 +157,7 @@ struct VFIOPCIDevice {uint32_t device_id;uint32_t sub_vendor_id;uint32_t sub_device_id; +uint32_t class_code;uint32_t features;#define VFIO_FEATURE_ENABLE_VGA_BIT 0#define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)
@@ -205,10 +206,7 @@ static inline bool vfio_pci_is(VFIOPCIDevice *vdev, uint32_t vendor, uint32_t destatic inline bool vfio_is_vga (VFIOPCIDevice *vdev) { -PCIDevice *pdev = &vdev->pdev; -uint16_t class =pci_get_word (pdev->config +PCI_CLASS_DEVICE ); - -return class ==PCI_CLASS_DISPLAY_VGA ; +return (vdev->class_code >>8 ) ==PCI_CLASS_DISPLAY_VGA ; }/* MSI/MSI-X/INTx */
hw/vfio/trace-events +1 -0
@@ -48,6 +48,7 @@ vfio_pci_emulated_vendor_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_device_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_sub_vendor_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_sub_device_id(const char *name, uint16_t val) "%s 0x%04x" +vfio_pci_emulated_class_code(const char *name, uint32_t val) "%s 0x%06x" # pci-quirks.c vfio_quirk_rom_in_denylist(const char *name, uint16_t vid, uint16_t did) "%s %04x:%04x"
Signature · 1 line
2.47.2
Quoted 26 lines · On Tue, 8 Jul 2025 22:52:11 +0800 Tomita Moeko <[email protected]> wrote:
Introduce x-pci-class-code option to allow users to override PCI class code of a device, similar to the existing x-pci-vendor-id option. Only the lower 24 bits of this option are used, though a uint32 is used here for determining whether the value is valid and set by user.
Additionally, to ensure VGA ranges are only exposed on VGA devices,
pci_register_vga()is now called invfio_pci_config_setup(), after the class code override is completed.This is mainly intended for IGD devices that expose themselves either as VGA controller (primary display) or Display controller (non-primary display). The UEFI GOP driver depends on the device reporting a VGA controller class code (
0x030000).Signed-off-by Tomita Moeko <[email protected]>---
v3:
* Save the vdev class code when it is unspecified.
* Callpci_register_vga()invfio_pci_config_setup()to ensure VGA regions are only get exposed when vdev is VGA.
* Check vdev class code invfio_is_vga().v2:
* Add vdev class code check invfio_populate_vga().
* Fix type in trace-events.
Quoted 6 lines
hw/vfio/pci.c | 29 +++++++++++++++++++++++++---- hw/vfio/pci.h | 6 ++---- hw/vfio/trace-events | 1 + 3 files changed, 28 insertions(+), 8 deletions(-)
Cédric Le Goater
AppliedQuoted 9 lines · Tomita Moeko
Introduce x-pci-class-code option to allow users to override PCI class code of a device, similar to the existing x-pci-vendor-id option. Only the lower 24 bits of this option are used, though a uint32 is used here for determining whether the value is valid and set by user.
Additionally, to ensure VGA ranges are only exposed on VGA devices,
pci_register_vga()is now called invfio_pci_config_setup(), after the class code override is completed.
Quoted 6 lines
This is mainly intended for IGD devices that expose themselves either as VGA controller (primary display) or Display controller (non-primary display). The UEFI GOP driver depends on the device reporting a VGA controller class code (
0x030000).Signed-off-by Tomita Moeko <[email protected]>
Applied to vfio-next.
Thanks,
C.
Quoted 115 lines
---
v3:
* Save the vdev class code when it is unspecified.
* Callpci_register_vga()invfio_pci_config_setup()to ensure VGA regions are only get exposed when vdev is VGA.
* Check vdev class code invfio_is_vga().v2:
* Add vdev class code check invfio_populate_vga().
* Fix type in trace-events.hw/vfio/pci.c | 29 +++++++++++++++++++++++++---- hw/vfio/pci.h | 6 ++---- hw/vfio/trace-events | 1 + 3 files changed, 28 insertions(+), 8 deletions(-)hw/vfio/pci.c +25
-4@@ -2893,10 +2893,6 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)"vfio-vga-io@0x3c0" , QEMU_PCI_VGA_IO_HI_SIZE); -pci_register_vga (&vdev->pdev, &vdev->vga->region[QEMU_PCI_VGA_MEM ].mem, - &vdev->vga->region[QEMU_PCI_VGA_IO_LO ].mem, - &vdev->vga->region[QEMU_PCI_VGA_IO_HI ].mem); -return true; }@@ -3228,6 +3224,23 @@ bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp) vdev->sub_device_id); } +/* +* Class code is a 24-bit value at config space 0x09. Allow overriding it +* with any 24-bit value. +*/ +if (vdev->class_code !=PCI_ANY_ID ) { +if (vdev->class_code >0xffffff ) { +error_setg (errp,"invalid PCI class code provided" ); +return false; + } +/* Higher 24 bits of PCI_CLASS_REVISION are class code */ +vfio_add_emulated_long (vdev,PCI_CLASS_REVISION , + vdev->class_code <<8 , ~0xff ); +trace_vfio_pci_emulated_class_code (vbasedev->name, vdev->class_code); + }else { + vdev->class_code =pci_get_long (pdev->config +PCI_CLASS_REVISION ) >>8 ; + } +/* QEMU can change multi-function devices to single function, or reverse */ vdev->emulated_config_bits[PCI_HEADER_TYPE ] =PCI_HEADER_TYPE_MULTI_FUNCTION ;@@ -3257,6 +3270,12 @@ bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)vfio_bars_register (vdev); +if (vdev->vga &&vfio_is_vga (vdev)) { +pci_register_vga (&vdev->pdev, &vdev->vga->region[QEMU_PCI_VGA_MEM ].mem, + &vdev->vga->region[QEMU_PCI_VGA_IO_LO ].mem, + &vdev->vga->region[QEMU_PCI_VGA_IO_HI ].mem); + } +return true; }@@ -3643,6 +3662,8 @@ static const Property vfio_pci_dev_properties[] = { sub_vendor_id, PCI_ANY_ID),DEFINE_PROP_UINT32 ("x-pci-sub-device-id" , VFIOPCIDevice, sub_device_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-class-code" , VFIOPCIDevice, + class_code,PCI_ANY_ID ),DEFINE_PROP_UINT32 ("x-igd-gms" , VFIOPCIDevice, igd_gms,0 ), DEFINE_PROP_UNSIGNED_NODEFAULT("x-nv-gpudirect-clique", VFIOPCIDevice, nv_gpudirect_clique,hw/vfio/pci.h +2
-4@@ -157,6 +157,7 @@ struct VFIOPCIDevice {uint32_t device_id;uint32_t sub_vendor_id;uint32_t sub_device_id; +uint32_t class_code;uint32_t features;#define VFIO_FEATURE_ENABLE_VGA_BIT 0#define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)@@ -205,10 +206,7 @@ static inline bool vfio_pci_is(VFIOPCIDevice *vdev, uint32_t vendor, uint32_t destatic inline bool vfio_is_vga (VFIOPCIDevice *vdev) { -PCIDevice *pdev = &vdev->pdev; -uint16_t class =pci_get_word (pdev->config +PCI_CLASS_DEVICE ); - -return class ==PCI_CLASS_DISPLAY_VGA ; +return (vdev->class_code >>8 ) ==PCI_CLASS_DISPLAY_VGA ; }/* MSI/MSI-X/INTx */ hw/vfio/trace-events +1
-0@@ -48,6 +48,7 @@ vfio_pci_emulated_vendor_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_device_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_sub_vendor_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_sub_device_id(const char *name, uint16_t val) "%s 0x%04x" +vfio_pci_emulated_class_code(const char *name, uint32_t val) "%s 0x%06x" # pci-quirks.c vfio_quirk_rom_in_denylist(const char *name, uint16_t vid, uint16_t did) "%s %04x:%04x"