vfio/igd: remove incorrect IO BAR4 quirk
cover vfio/igd: remove incorrect IO BAR4 quirk
Based on experiments and reverse engineering about the mysterious IO BAR4, it appears that the current quirk implementation is incorrect. As discussed in a previous mail thread [1], current implementation believes VBIOS is writing HPA of Data Stolen Memory (DSM) in GTT entries, so it intercepts and writes (addr - Host BDSM + Guest BDSM) instead, probably believes the addresses are hardcoded in VBIOS.
With disassembling a VBIOS of Intel Sandy/Ivy Bridge and Haswell [2] (thankfully there is only a few 32-bit out instructions), VBIOS read BDSM from pci config space, which is programmed by SeaBIOS [3] in guest. That is to say, VBIOS actually writes GPA when programming GTT initial entries, the current quirk is incorrect.
The change was tested on a Kaby Lake HD620 (Gen 9) by comparing GTT entries read from MMIO BAR0 and IO BAR4.
This patchset also creates a new function vfio_config_quirk_setup() for the config space quirk previously in vfio_probe_igd_bar4_quirk(). The OpRegion quirk in vfio_realize() will also be moved to it in a later change, so that all the IGD-related quirks can be put in igd.c.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://winraid.level1techs.com/t/offer-intel-sandy-ivy-bridge-and-haswell-vbios-modules/30272
[3] https://gitlab.com/qemu-project/seabios/-/blob/1.12-stable/src/fw/pciinit.c#L319-332
Related VBIOS disassembly:
# read BDSM
6498: 66 53 push %ebx
649a: 66 2e 83 3e 94 64 00 cmpl $0x0,%cs:0x6494 # BDSM already read?
64a1: 74 07 je 0x64aa
64a3: 66 2e a1 94 64 mov %cs:0x6494,%eax
64a8: eb 0f jmp 0x64b9
64aa: b8 5e 10 mov $0x105e,%ax # 5e == 5c + 2, that's why shl 16 later?
# ((2 << 11) + 0x5e)
64ad: e8 55 05 call 0x6a05 # read config space via port cf8/cfc
64b0: 66 c1 e0 10 shl $0x10,%eax # eax << 16
64b4: 66 2e a3 94 64 mov %eax,%cs:0x6494 # save BDSM
64b9: 66 5b pop %ebx
64bb: c3 ret
# program single GTT entry, esi: addr, eax: data
6d16: 66 50 push %eax
6d18: 52 push %dx
6d19: 2e 8b 16 4a e4 mov %cs:-0x1bb6,%dx # saved MMIO_Index port
6d1e: 66 96 xchg %eax,%esi
6d20: 66 ef out %eax,(%dx) # write MMIO_Index
6d22: 2e 8b 16 4c e4 mov %cs:-0x1bb4,%dx # saved MMIO_Data port
6d27: 66 96 xchg %eax,%esi
6d29: 66 83 c8 01 or $0x1,%eax # set valid bit
6d2d: 66 ef out %eax,(%dx) # write MMIO_Data
6d2f: 5a pop %dx
6d30: 66 58 pop %eax
6d32: c3 ret
# program GTT entries, esi: entry addr, eax: page addr
e929: 51 push %cx
e92a: e8 6b 7b call 0x6498 # read BDSM
e92d: 25 00 e0 and $0xe000,%ax
e930: 66 83 c8 01 or $0x1,%eax # valid bit
e934: 66 be 01 00 00 00 mov $0x1,%esi # gtt access bit in addr
e93a: e8 d9 83 call 0x6d16 # single entry routine
e93d: 66 83 c6 04 add $0x4,%esi # next entry
e941: 66 05 00 10 00 00 add $0x1000,%eax # next page
e947: e2 f1 loop 0xe93a
e949: 59 pop %cx
e94a: c3 retObserved layout of the undocumented MMIO_Index register:
31 2 1 0
+-------------------------------------------------------------------+ | Offset | Rsvd | Sel | +-------------------------------------------------------------------+
- Offset: Byte offset in specified region, 4-byte aligned.
- Sel: Region selector
0: MMIO register region (first half of MMIO BAR0) 1: GTT region (second half of MMIO BAR0). Pre Gen11 only.
Tomita Moeko (4):
vfio/igd: remove GTT write quirk in IO BAR 4
vfio/pci: add placeholder for device-specific config space quirks vfio/igd: refactor vfio_probe_igd_bar4_quirk() into pci config quirk vfio/igd: do not include GTT stolen size in etc/igd-bdsm-size
hw/vfio/igd.c | 247 ++++--------------------------------------- hw/vfio/pci-quirks.c | 11 +- hw/vfio/pci.c | 4 + hw/vfio/pci.h | 3 +- 4 files changed, 35 insertions(+), 230 deletions(-)
Signature · 1 line
2.45.2
PATCH 1/4 vfio/igd: remove GTT write quirk in IO BAR 4
The IO BAR4 of IGD devices contains a pair of 32-bit address/data registers, MMIO_Index (0x0) and MMIO_Data (0x4), which provide access to the MMIO BAR0 (GTTMMADR) from IO space. These registers are probably only used by the VBIOS, and are not documented by intel. The observed layout of MMIO_Index register is:
31 2 1 0
+-------------------------------------------------------------------+ | Offset | Rsvd | Sel | +-------------------------------------------------------------------+
- Offset: Byte offset in specified region, 4-byte aligned.
- Sel: Region selector
0: MMIO register region (first half of MMIO BAR0) 1: GTT region (second half of MMIO BAR0). Pre Gen11 only.
Currently, QEMU implements a quirk that adjusts the guest Data Stolen Memory (DSM) region address to be (addr - host BDSM + guest BDSM) when programming GTT entries via IO BAR4, assuming guest still programs GTT with host DSM address, which is not the case. Guest's BDSM register is emulated and initialized to 0 at startup by QEMU, then SeaBIOS programs its value[1]. As result, the address programmed to GTT entries by VBIOS running in guest are valid GPA, and this unnecessary adjustment brings inconsistency.
[1] https://gitlab.com/qemu-project/seabios/-/blob/1.12-stable/src/fw/pciinit.c#L319-332
--- hw/vfio/igd.c | 191 +------------------------------------------------- 1 file changed, 1 insertion(+), 190 deletions(-)
hw/vfio/igd.c +1 -190
@@ -106,12 +106,6 @@ static int igd_gen(VFIOPCIDevice *vdev)return -1 ; } -typedef struct VFIOIGDQuirk { -struct VFIOPCIDevice *vdev; -uint32_t index; -uint64_t bdsm; -}VFIOIGDQuirk ; -#define IGD_GMCH 0x50/* Graphics Control Register */ #define IGD_BDSM 0x5c/* Base Data of Stolen Memory */ #define IGD_BDSM_GEN11 0xc0/* Base Data of Stolen Memory of gen 11 and later */
@@ -300,129 +294,6 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,return ret; } -/* -* IGD Gen8 and newer support up to 8MB for the GTT and use a 64bit PTE -* entry, older IGDs use 2MB and 32bit. Each PTE maps a 4k page. Therefore -* we either have 2M/4k * 4 = 2k or 8M/4k * 8 = 16k as the maximum iobar index -* for programming the GTT. -* -* See linux:include/drm/i915_drm.h for shift and mask values. -*/ -static int vfio_igd_gtt_max (VFIOPCIDevice *vdev) -{ -uint32_t gmch =vfio_pci_read_config (&vdev->pdev,IGD_GMCH ,sizeof (gmch)); -int gen =igd_gen (vdev); -uint64_t ggms_size =igd_gtt_memory_size (gen, gmch); - -return (ggms_size / (4 * KiB)) * (gen <8 ?4 :8 ); -} - -/* -* The IGD ROM will make use of stolen memory (GGMS) for support of VESA modes. -* Somehow the host stolen memory range is used for this, but how the ROM gets -* it is a mystery, perhaps it's hardcoded into the ROM. Thankfully though, it -* reprograms the GTT through the IOBAR where we can trap it and transpose the -* programming to the VM allocated buffer. That buffer gets reserved by the VM -* firmware via the fw_cfg entry added below. Here we're just monitoring the -* IOBAR address and data registers to detect a write sequence targeting the -* GTTADR. This code is developed by observed behavior and doesn't have a -* direct spec reference, unfortunately. -*/ -static uint64_t vfio_igd_quirk_data_read (void *opaque, -hwaddr addr,unsigned size) -{ -VFIOIGDQuirk *igd = opaque; -VFIOPCIDevice *vdev = igd->vdev; - - igd->index = ~0 ; - -return vfio_region_read (&vdev->bars[4 ].region, addr +4 , size); -} - -static void vfio_igd_quirk_data_write (void *opaque,hwaddr addr, -uint64_t data,unsigned size) -{ -VFIOIGDQuirk *igd = opaque; -VFIOPCIDevice *vdev = igd->vdev; -uint64_t val = data; -int gen =igd_gen (vdev); - -/* -* Programming the GGMS starts at index 0x1 and uses every 4th index (ie. -* 0x1, 0x5, 0x9, 0xd,...). For pre-Gen8 each 4-byte write is a whole PTE -* entry, with 0th bit enable set. For Gen8 and up, PTEs are 64bit, so -* entries 0x5 & 0xd are the high dword, in our case zero. Each PTE points -* to a 4k page, which we translate to a page from the VM allocated region, -* pointed to by the BDSM register. If this is not set, we fail. -* -* We trap writes to the full configured GTT size, but we typically only -* see the vBIOS writing up to (nearly) the 1MB barrier. In fact it often -* seems to miss the last entry for an even 1MB GTT. Doing a gratuitous -* write of that last entry does work, but is hopefully unnecessary since -* we clear the previous GTT on initialization. -*/ -if ((igd->index %4 ==1 ) && igd->index <vfio_igd_gtt_max (vdev)) { -if (gen <8 || (igd->index %8 ==1 )) { -uint64_t base; - -if (gen <11 ) { - base =pci_get_long (vdev->pdev.config +IGD_BDSM ); - }else { - base =pci_get_quad (vdev->pdev.config +IGD_BDSM_GEN11 ); - } -if (!base) { -hw_error ("vfio-igd: Guest attempted to program IGD GTT before " -"BIOS reserved stolen memory. Unsupported BIOS?" ); - } - - val = data - igd->bdsm + base; - }else { - val =0 ;/* upper 32bits of pte, we only enable below 4G PTEs */ - } - -trace_vfio_pci_igd_bar4_write (vdev->vbasedev.name, - igd->index, data, val); - } - -vfio_region_write (&vdev->bars[4 ].region, addr +4 , val, size); - - igd->index = ~0 ; -} - -static const MemoryRegionOps vfio_igd_data_quirk = { - .read = vfio_igd_quirk_data_read, - .write = vfio_igd_quirk_data_write, - .endianness =DEVICE_LITTLE_ENDIAN , -}; - -static uint64_t vfio_igd_quirk_index_read (void *opaque, -hwaddr addr,unsigned size) -{ -VFIOIGDQuirk *igd = opaque; -VFIOPCIDevice *vdev = igd->vdev; - - igd->index = ~0 ; - -return vfio_region_read (&vdev->bars[4 ].region, addr, size); -} - -static void vfio_igd_quirk_index_write (void *opaque,hwaddr addr, -uint64_t data,unsigned size) -{ -VFIOIGDQuirk *igd = opaque; -VFIOPCIDevice *vdev = igd->vdev; - - igd->index = data; - -vfio_region_write (&vdev->bars[4 ].region, addr, data, size); -} - -static const MemoryRegionOps vfio_igd_index_quirk = { - .read = vfio_igd_quirk_index_read, - .write = vfio_igd_quirk_index_write, - .endianness =DEVICE_LITTLE_ENDIAN , -}; -#define IGD_GGC_MMIO_OFFSET 0x108040#define IGD_BDSM_MMIO_OFFSET 0x1080C0
@@ -494,14 +365,11 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) g_autofree struct vfio_region_info *opregion = NULL; g_autofree struct vfio_region_info *host = NULL; g_autofree struct vfio_region_info *lpc = NULL; -VFIOQuirk *quirk; -VFIOIGDQuirk *igd;PCIDevice *lpc_bridge; -int i, ret, gen; +int ret, gen;uint64_t ggms_size, gms_size;uint64_t *bdsm_size;uint32_t gmch; -uint16_t cmd_orig, cmd;Error *err =NULL ;/*
@@ -634,32 +502,6 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)return ; } -/* Setup our quirk to munge GTT addresses to the VM allocated buffer */ - quirk =vfio_quirk_alloc (2 ); - igd = quirk->data =g_malloc0 (sizeof (*igd)); - igd->vdev = vdev; - igd->index = ~0 ; -if (gen <11 ) { - igd->bdsm =vfio_pci_read_config (&vdev->pdev,IGD_BDSM ,4 ); - }else { - igd->bdsm =vfio_pci_read_config (&vdev->pdev,IGD_BDSM_GEN11 ,4 ); - igd->bdsm |= - (uint64_t )vfio_pci_read_config (&vdev->pdev,IGD_BDSM_GEN11 +4 ,4 ) <<32 ; - } - igd->bdsm &= ~((1 * MiB) -1 );/* 1MB aligned */ - -memory_region_init_io (&quirk->mem[0 ],OBJECT (vdev), &vfio_igd_index_quirk, - igd,"vfio-igd-index-quirk" ,4 ); -memory_region_add_subregion_overlap (vdev->bars[nr].region.mem, -0 , &quirk->mem[0 ],1 ); - -memory_region_init_io (&quirk->mem[1 ],OBJECT (vdev), &vfio_igd_data_quirk, - igd,"vfio-igd-data-quirk" ,4 ); -memory_region_add_subregion_overlap (vdev->bars[nr].region.mem, -4 , &quirk->mem[1 ],1 ); - -QLIST_INSERT_HEAD (&vdev->bars[nr].quirks, quirk, next); -/* * Allow user to override dsm size using x-igd-gms option, in multiples of * 32MiB. This option should only be used when the desired size cannot be
@@ -717,37 +559,6 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)pci_set_quad (vdev->emulated_config_bits +IGD_BDSM_GEN11 , ~0 ); } -/* -* This IOBAR gives us access to GTTADR, which allows us to write to -* the GTT itself. So let's go ahead and write zero to all the GTT -* entries to avoid spurious DMA faults. Be sure I/O access is enabled -* before talking to the device. -*/ -if (pread (vdev->vbasedev.fd, &cmd_orig,sizeof (cmd_orig), - vdev->config_offset +PCI_COMMAND ) !=sizeof (cmd_orig)) { -error_report ("IGD device %s - failed to read PCI command register" , - vdev->vbasedev.name); - } - - cmd = cmd_orig |PCI_COMMAND_IO ; - -if (pwrite (vdev->vbasedev.fd, &cmd,sizeof (cmd), - vdev->config_offset +PCI_COMMAND ) !=sizeof (cmd)) { -error_report ("IGD device %s - failed to write PCI command register" , - vdev->vbasedev.name); - } - -for (i =1 ; i <vfio_igd_gtt_max (vdev); i +=4 ) { -vfio_region_write (&vdev->bars[4 ].region,0 , i,4 ); -vfio_region_write (&vdev->bars[4 ].region,4 ,0 ,4 ); - } - -if (pwrite (vdev->vbasedev.fd, &cmd_orig,sizeof (cmd_orig), - vdev->config_offset +PCI_COMMAND ) !=sizeof (cmd_orig)) { -error_report ("IGD device %s - failed to restore PCI command register" , - vdev->vbasedev.name); - } -trace_vfio_pci_igd_bdsm_enabled (vdev->vbasedev.name, (ggms_size + gms_size) / MiB); }
Signature · 1 line
2.45.2
PATCH 2/4 vfio/pci: add placeholder for device-specific config space quirks
In reply to Tomita MoekoSome devices, such as IGD, require device-specific quirks to be applied to their pci config spaces. Currently, these quirks are either part of BAR quirk, or being a part of vfio_realize(). Add a placeholder for pci config quirks for moving the quirks to one place later.
--- hw/vfio/pci-quirks.c | 5 +++++ hw/vfio/pci.c | 4 ++++ hw/vfio/pci.h | 1 + 3 files changed, 10 insertions(+)
hw/vfio/pci-quirks.c +5 -0
@@ -1167,6 +1167,11 @@ bool vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev,/* * Common quirk probe entry points. */ +bool vfio_config_quirk_setup (VFIOPCIDevice *vdev,Error **errp) +{ +return true; +} +void vfio_vga_quirk_setup (VFIOPCIDevice *vdev) {vfio_vga_probe_ati_3c3_quirk (vdev);
hw/vfio/pci.c +4 -0
@@ -3124,6 +3124,10 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) goto out_unset_idev; } +if (!vfio_config_quirk_setup (vdev, errp)) { + goto out_unset_idev; + } +if (vdev->vga) {vfio_vga_quirk_setup (vdev); }
hw/vfio/pci.h +1 -0
@@ -205,6 +205,7 @@ uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);void vfio_vga_write (void *opaque,hwaddr addr,uint64_t data,unsigned size);bool vfio_opt_rom_in_denylist (VFIOPCIDevice *vdev); +bool vfio_config_quirk_setup (VFIOPCIDevice *vdev,Error **errp);void vfio_vga_quirk_setup (VFIOPCIDevice *vdev);void vfio_vga_quirk_exit (VFIOPCIDevice *vdev);void vfio_vga_quirk_finalize (VFIOPCIDevice *vdev);
Signature · 1 line
2.45.2
PATCH 3/4 vfio/igd: refactor vfio_probe_igd_bar4_quirk() into pci config quirk
In reply to Tomita MoekoThe actual IO BAR4 write quirk in vfio_probe_igd_bar4_quirk() was removed in previous change, leaving the function not matching its name, so move it into the newly introduced vfio_config_quirk_setup(). There is no functional change in this commit. If any failure occurs, the function simply returns and proceeds.
--- hw/vfio/igd.c | 30 ++++++++++++++++-------------- hw/vfio/pci-quirks.c | 6 +++++- hw/vfio/pci.h | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-)
hw/vfio/igd.c +16 -14
@@ -359,7 +359,8 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)QLIST_INSERT_HEAD (&vdev->bars[nr].quirks, bdsm_quirk, next); } -void vfio_probe_igd_bar4_quirk (VFIOPCIDevice *vdev,int nr) +bool vfio_probe_igd_config_quirk (VFIOPCIDevice *vdev, + Error **errp G_GNUC_UNUSED) { g_autofree struct vfio_region_info *rom = NULL; g_autofree struct vfio_region_info *opregion = NULL;
@@ -378,10 +379,9 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)* PCI bus address. */ if (!vfio_pci_is (vdev,PCI_VENDOR_ID_INTEL ,PCI_ANY_ID ) || - nr !=4 || &vdev->pdev !=pci_find_device (pci_device_root_bus (&vdev->pdev),0 ,PCI_DEVFN (0x2 ,0 ))) { -return ; +return true; }/*
@@ -395,7 +395,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) "vfio-pci-igd-lpc-bridge")) {error_report ("IGD device %s cannot support legacy mode due to existing " "devices at address 1f.0" , vdev->vbasedev.name); -return ; +return true; }/*
@@ -407,7 +407,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (gen ==-1 ) {error_report ("IGD device %s is unsupported in legacy mode, " "try SandyBridge or newer" , vdev->vbasedev.name); -return ; +return true; }/*
@@ -420,7 +420,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if ((ret || !rom->size) && !vdev->pdev.romfile) {error_report ("IGD device %s has no ROM, legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/*
@@ -431,7 +431,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)error_report ("IGD device %s hotplugged, ROM disabled, " "legacy mode disabled" , vdev->vbasedev.name); vdev->rom_read_failed = true; -return ; +return true; }/*
@@ -444,7 +444,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s does not support OpRegion access," "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; } ret = vfio_get_dev_region_info(&vdev->vbasedev,
@@ -453,7 +453,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s does not support host bridge access," "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; } ret = vfio_get_dev_region_info(&vdev->vbasedev,
@@ -462,7 +462,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s does not support LPC bridge access," "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; } gmch =vfio_pci_read_config (&vdev->pdev,IGD_GMCH ,4 );
@@ -476,7 +476,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)error_reportf_err (err,VFIO_MSG_PREFIX , vdev->vbasedev.name);error_report ("IGD device %s failed to enable VGA access, " "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* Create our LPC/ISA bridge */
@@ -484,7 +484,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s failed to create LPC bridge, " "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* Stuff some host values into the VM PCI host bridge */
@@ -492,14 +492,14 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s failed to modify host bridge, " "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* Setup OpRegion access */ if (!vfio_pci_igd_opregion_init (vdev, opregion, &err)) {error_append_hint (&err,"IGD legacy mode disabled\n" );error_reportf_err (err,VFIO_MSG_PREFIX , vdev->vbasedev.name); -return ; +return true; }/*
@@ -561,4 +561,6 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)trace_vfio_pci_igd_bdsm_enabled (vdev->vbasedev.name, (ggms_size + gms_size) / MiB); + +return true; }
hw/vfio/pci-quirks.c +5 -1
@@ -1169,6 +1169,11 @@ bool vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev,*/ bool vfio_config_quirk_setup (VFIOPCIDevice *vdev,Error **errp) { +#ifdef CONFIG_VFIO_IGD +if (!vfio_probe_igd_config_quirk (vdev, errp)) { +return false; + } +#endif return true; }
@@ -1220,7 +1225,6 @@ void vfio_bar_quirk_setup(VFIOPCIDevice *vdev, int nr)vfio_probe_rtl8168_bar2_quirk (vdev, nr);#ifdef CONFIG_VFIO_IGD vfio_probe_igd_bar0_quirk (vdev, nr); -vfio_probe_igd_bar4_quirk (vdev, nr);#endif }
hw/vfio/pci.h +1 -1
@@ -217,7 +217,7 @@ bool vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp);void vfio_quirk_reset (VFIOPCIDevice *vdev);VFIOQuirk *vfio_quirk_alloc (int nr_mem);void vfio_probe_igd_bar0_quirk (VFIOPCIDevice *vdev,int nr); -void vfio_probe_igd_bar4_quirk (VFIOPCIDevice *vdev,int nr); +bool vfio_probe_igd_config_quirk (VFIOPCIDevice *vdev,Error **errp);extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
Signature · 1 line
2.45.2
Re: [PATCH 3/4] vfio/igd: refactor vfio_probe_igd_bar4_quirk() into pci config quirk
Quoted 5 lines · On Thu, 23 Jan 2025 01:17:30 +0800 Tomita Moeko <[email protected]> wrote:
The actual IO BAR4 write quirk in
vfio_probe_igd_bar4_quirk()was removed in previous change, leaving the function not matching its name, so move it into the newly introducedvfio_config_quirk_setup(). There is no functional change in this commit. If any failure occurs, the function simply returns and proceeds.
I don't understand why vfio_config_quirk_setup() returns bool rather than void given that it can never fail based on this series. Otherwise, while I'm surprised that the GTT re-writing is unnecessary (seems I wouldn't have invented such a need), removing it is really the only way to fully validate that, and we can always revisit if we start getting regression reports. Thanks,
Alex
Quoted 175 lines
Signed-off-by Tomita Moeko <[email protected]>--- hw/vfio/igd.c | 30 ++++++++++++++++-------------- hw/vfio/pci-quirks.c | 6 +++++- hw/vfio/pci.h | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-)hw/vfio/igd.c +16
-14@@ -359,7 +359,8 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)QLIST_INSERT_HEAD (&vdev->bars[nr].quirks, bdsm_quirk, next); } -void vfio_probe_igd_bar4_quirk (VFIOPCIDevice *vdev,int nr) +bool vfio_probe_igd_config_quirk (VFIOPCIDevice *vdev, + Error **errp G_GNUC_UNUSED) { g_autofree struct vfio_region_info *rom = NULL; g_autofree struct vfio_region_info *opregion = NULL;@@ -378,10 +379,9 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)* PCI bus address. */ if (!vfio_pci_is (vdev,PCI_VENDOR_ID_INTEL ,PCI_ANY_ID ) || - nr !=4 || &vdev->pdev !=pci_find_device (pci_device_root_bus (&vdev->pdev),0 ,PCI_DEVFN (0x2 ,0 ))) { -return ; +return true; }/* @@ -395,7 +395,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) "vfio-pci-igd-lpc-bridge")) {error_report ("IGD device %s cannot support legacy mode due to existing " "devices at address 1f.0" , vdev->vbasedev.name); -return ; +return true; }/* @@ -407,7 +407,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (gen ==-1 ) {error_report ("IGD device %s is unsupported in legacy mode, " "try SandyBridge or newer" , vdev->vbasedev.name); -return ; +return true; }/* @@ -420,7 +420,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if ((ret || !rom->size) && !vdev->pdev.romfile) {error_report ("IGD device %s has no ROM, legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* @@ -431,7 +431,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)error_report ("IGD device %s hotplugged, ROM disabled, " "legacy mode disabled" , vdev->vbasedev.name); vdev->rom_read_failed = true; -return ; +return true; }/* @@ -444,7 +444,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s does not support OpRegion access," "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; } ret = vfio_get_dev_region_info(&vdev->vbasedev,@@ -453,7 +453,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s does not support host bridge access," "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; } ret = vfio_get_dev_region_info(&vdev->vbasedev,@@ -462,7 +462,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s does not support LPC bridge access," "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; } gmch =vfio_pci_read_config (&vdev->pdev,IGD_GMCH ,4 );@@ -476,7 +476,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)error_reportf_err (err,VFIO_MSG_PREFIX , vdev->vbasedev.name);error_report ("IGD device %s failed to enable VGA access, " "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* Create our LPC/ISA bridge */ @@ -484,7 +484,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s failed to create LPC bridge, " "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* Stuff some host values into the VM PCI host bridge */ @@ -492,14 +492,14 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)if (ret) {error_report ("IGD device %s failed to modify host bridge, " "legacy mode disabled" , vdev->vbasedev.name); -return ; +return true; }/* Setup OpRegion access */ if (!vfio_pci_igd_opregion_init (vdev, opregion, &err)) {error_append_hint (&err,"IGD legacy mode disabled\n" );error_reportf_err (err,VFIO_MSG_PREFIX , vdev->vbasedev.name); -return ; +return true; }/* @@ -561,4 +561,6 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)trace_vfio_pci_igd_bdsm_enabled (vdev->vbasedev.name, (ggms_size + gms_size) / MiB); + +return true; }hw/vfio/pci-quirks.c +5
-1@@ -1169,6 +1169,11 @@ bool vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev,*/ bool vfio_config_quirk_setup (VFIOPCIDevice *vdev,Error **errp) { +#ifdef CONFIG_VFIO_IGD +if (!vfio_probe_igd_config_quirk (vdev, errp)) { +return false; + } +#endif return true; }@@ -1220,7 +1225,6 @@ void vfio_bar_quirk_setup(VFIOPCIDevice *vdev, int nr)vfio_probe_rtl8168_bar2_quirk (vdev, nr);#ifdef CONFIG_VFIO_IGD vfio_probe_igd_bar0_quirk (vdev, nr); -vfio_probe_igd_bar4_quirk (vdev, nr);#endif }hw/vfio/pci.h +1
-1@@ -217,7 +217,7 @@ bool vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp);void vfio_quirk_reset (VFIOPCIDevice *vdev);VFIOQuirk *vfio_quirk_alloc (int nr_mem);void vfio_probe_igd_bar0_quirk (VFIOPCIDevice *vdev,int nr); -void vfio_probe_igd_bar4_quirk (VFIOPCIDevice *vdev,int nr); +bool vfio_probe_igd_config_quirk (VFIOPCIDevice *vdev,Error **errp);extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
PATCH 4/4 vfio/igd: do not include GTT stolen size in etc/igd-bdsm-size
In reply to Tomita MoekoThough GTT Stolen Memory (GSM) is right below Data Stolen Memory (DSM) in host address space, direct access to GSM is prohibited, and it is not mapped to guest address space. Both host and guest accesses GSM indirectly through the second half of MMIO BAR0 (GTTMMADR).
Guest firmware only need to reserve a memory region for DSM and program the BDSM register with the base address of that region, that's actually what both SeaBIOS[1] and OVMF does now.
[1] https://gitlab.com/qemu-project/seabios/-/blob/1.12-stable/src/fw/pciinit.c#L319-332
--- hw/vfio/igd.c | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-)
hw/vfio/igd.c +3 -25
@@ -112,28 +112,8 @@ static int igd_gen(VFIOPCIDevice *vdev)#define IGD_GMCH_GEN6_GMS_SHIFT 3/* SNB_GMCH in i915 */ #define IGD_GMCH_GEN6_GMS_MASK 0x1f -#define IGD_GMCH_GEN6_GGMS_SHIFT 8 -#define IGD_GMCH_GEN6_GGMS_MASK 0x3#define IGD_GMCH_GEN8_GMS_SHIFT 8/* BDW_GMCH in i915 */ #define IGD_GMCH_GEN8_GMS_MASK 0xff -#define IGD_GMCH_GEN8_GGMS_SHIFT 6 -#define IGD_GMCH_GEN8_GGMS_MASK 0x3 - -static uint64_t igd_gtt_memory_size (int gen,uint16_t gmch) -{ -uint64_t ggms; - -if (gen <8 ) { - ggms = (gmch >>IGD_GMCH_GEN6_GGMS_SHIFT ) &IGD_GMCH_GEN6_GGMS_MASK ; - }else { - ggms = (gmch >>IGD_GMCH_GEN8_GGMS_SHIFT ) &IGD_GMCH_GEN8_GGMS_MASK ; -if (ggms !=0 ) { - ggms =1ULL << ggms; - } - } - -return ggms * MiB; -}static uint64_t igd_stolen_memory_size (int gen,uint32_t gmch) {
@@ -368,7 +348,7 @@ bool vfio_probe_igd_config_quirk(VFIOPCIDevice *vdev, g_autofree struct vfio_region_info *lpc = NULL;PCIDevice *lpc_bridge;int ret, gen; -uint64_t ggms_size, gms_size; +uint64_t gms_size;uint64_t *bdsm_size;uint32_t gmch;Error *err =NULL ;
@@ -527,7 +507,6 @@ bool vfio_probe_igd_config_quirk(VFIOPCIDevice *vdev, } } - ggms_size =igd_gtt_memory_size (gen, gmch); gms_size =igd_stolen_memory_size (gen, gmch);/*
@@ -539,7 +518,7 @@ bool vfio_probe_igd_config_quirk(VFIOPCIDevice *vdev,* config offset 0x5C. */ bdsm_size =g_malloc (sizeof (*bdsm_size)); - *bdsm_size =cpu_to_le64 (ggms_size + gms_size); + *bdsm_size =cpu_to_le64 (gms_size);fw_cfg_add_file (fw_cfg_find (),"etc/igd-bdsm-size" , bdsm_size,sizeof (*bdsm_size));
@@ -559,8 +538,7 @@ bool vfio_probe_igd_config_quirk(VFIOPCIDevice *vdev,pci_set_quad (vdev->emulated_config_bits +IGD_BDSM_GEN11 , ~0 ); } -trace_vfio_pci_igd_bdsm_enabled (vdev->vbasedev.name, - (ggms_size + gms_size) / MiB); +trace_vfio_pci_igd_bdsm_enabled (vdev->vbasedev.name, (gms_size / MiB));return true; }
Signature · 1 line
2.45.2