Skip to contentSkip to conversation

vfio/igd: Enable legacy mode on more devices

PATCH Superseded Tomita Moeko 3 participants 29 messages Dec 1, 2024 – Dec 2, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover vfio/igd: Enable legacy mode on more devices

This patchset extends the support of legacy mode igd passthrough to all Intel Gen 11 and 12 devices (including Ice Lake, Jasper Lake, Rocket Lake, Alder Lake and Raptor Lake), and emulates GGC register in MMIO BAR0 for better compatibiltiy (It is tested Windows and GOP driver will read this MMIO register).

It also replaces magic numbers with macros to improve readability, and aligns behavior (BDSM registor mirroring and GGMS calculation for gen7) with i915 driver to avoid possible issues.

The x-igd-gms option removed in 971ca22f041b ("vfio/igd: don't set stolen memory size to zero") is also added back so that data stolen memory size can be specified for guest. It is tested that GMS may related to framebuffer size, a small GMS value may cause display issues like blackscreen. It can be changed by DVMT Pre-allocated option in host BIOS, but not all BIOS comes with this option. Having it in QEMU helps resolves such issues.

This patchset was verified on Intel i9-12900K CPU(UHD 770, 8086:4680) with custom OVMF firmware [1] and IntelGopDriver extracted from host bios. IGD device works well in both Windows and Linux guests, and scored 726 in 3DMark Time Spy Graphics on Windows guest.

[1] https://github.com/tomitamoeko/edk2/commits/igd-pt-adl/

Btw, IO BAR4 seems never be used by guest, and it the IO BAR itself is not working on Gen11+ devices in my experiments. There is no hints about that in old commit message and mailing list. It would be greatly appreciated if someone shares the background.

Tomita Moeko (8):
vfio/igd: fix GTT stolen memory size calculation for gen 7 vfio/igd: canonicalize memory size calculations
vfio/igd: remove unsupported device ids
vfio/igd: align generation with i915 kernel driver vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper Lake device ids vfio/igd: emulate GGC register in mmio bar0
vfio/igd: emulate BDSM in mmio bar0 for gen 6-10 devices vfio/igd: add x-igd-gms option back to set DSM region size for guest

 hw/vfio/igd.c | 240 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 144 insertions(+), 96 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/8 vfio/igd: fix GTT stolen memory size calculation for gen 7

Both intel documentation [1][2] and i915 driver shows GGMS represents GTT stolen memory size in multiple of 1MB, not 2MB starting from gen 8.

[1] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-core-desktop-vol-2-datasheet.pdf
[2] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf

---
 hw/vfio/igd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/igd.c +2 -2
index 4047f4f071..e40e601026 100644
@@ -268,7 +268,7 @@ static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 
     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
     ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms = 1 << ggms;
     }
 
@@ -678,7 +678,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     /* Determine the size of stolen memory needed for GTT */
     ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms_mb = 1 << ggms_mb;
     }
 
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH 1/8] vfio/igd: fix GTT stolen memory size calculation for gen 7

Quoted 25 lines · On Mon, 2 Dec 2024 00:09:31 +0800 Tomita Moeko <[email protected]> wrote:

Both intel documentation [1][2] and i915 driver shows GGMS represents GTT stolen memory size in multiple of 1MB, not 2MB starting from gen 8.

[1] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-core-desktop-vol-2-datasheet.pdf
[2] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf

---
 hw/vfio/igd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/igd.c +1 -1
index 4047f4f071..e40e601026 100644
@@ -268,7 +268,7 @@ static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 
     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
     ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms = 1 << ggms;
     }
 

hw/vfio/igd.c void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) · line 684

Quoted 8 lines
hw/vfio/igd.c +1 -1
@@ -678,7 +678,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     /* Determine the size of stolen memory needed for GTT */
     ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms_mb = 1 << ggms_mb;
     }
 

I'd argue this should be rolled into patch 4. It's not really fixing anything because igd_gen() can't return anything between 6 and 8. This only allows for several device versions that we currently consider to be gen 6 to align with i915 kernel driver generation by calling them generation 7. We'd previously lumped them into generation 6 because there's no functional difference we care about here between 6 & 7.

In the next patch you replace this '1 << ggms_mb' with '*= 2', which would be equivalent to 'ggms_mb << 1' and matches your description that the increment is doubled. Is there a separate bug fix that needs to be pulled out here?

Also, please send a cover letter for any series longer than a single patch and please configure your tools to thread the series. Thanks,

Alex

#3 lore raw

Re: [PATCH 1/8] vfio/igd: fix GTT stolen memory size calculation for gen 7

Quoted 44 lines · On Sun, 1 Dec 2024 22:11:29 -0700 Alex Williamson <[email protected]>…
Quoted 25 lines · On Mon, 2 Dec 2024 00:09:31 +0800 Tomita Moeko <[email protected]> wrote:

Both intel documentation [1][2] and i915 driver shows GGMS represents GTT stolen memory size in multiple of 1MB, not 2MB starting from gen 8.

[1] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-core-desktop-vol-2-datasheet.pdf
[2] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf

---
 hw/vfio/igd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/igd.c +1 -1
index 4047f4f071..e40e601026 100644
@@ -268,7 +268,7 @@ static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 
     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
     ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms = 1 << ggms;
     }
 

hw/vfio/igd.c void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) · line 684

Quoted 8 lines
hw/vfio/igd.c +1 -1
@@ -678,7 +678,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     /* Determine the size of stolen memory needed for GTT */
     ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms_mb = 1 << ggms_mb;
     }
 

I'd argue this should be rolled into patch 4. It's not really fixing anything because igd_gen() can't return anything between 6 and 8. This only allows for several device versions that we currently consider to be gen 6 to align with i915 kernel driver generation by calling them generation 7. We'd previously lumped them into generation 6 because there's no functional difference we care about here between 6 & 7.

Quoted 7 lines

In the next patch you replace this '1 << ggms_mb' with '*= 2', which would be equivalent to 'ggms_mb << 1' and matches your description that the increment is doubled. Is there a separate bug fix that needs to be pulled out here?

Also, please send a cover letter for any series longer than a single patch and please configure your tools to thread the series. Thanks,

Disregard this latter comment, I just wasn't copied on the cover letter and didn't have it in my inbox to root the thread. Thanks,

Alex

#4 lore raw

Re: [PATCH 1/8] vfio/igd: fix GTT stolen memory size calculation for gen 7

Quoted 47 lines · Alex Williamson
Quoted 44 lines · On Sun, 1 Dec 2024 22:11:29 -0700 Alex Williamson <[email protected]>…
Quoted 25 lines · On Mon, 2 Dec 2024 00:09:31 +0800 Tomita Moeko <[email protected]> wrote:

Both intel documentation [1][2] and i915 driver shows GGMS represents GTT stolen memory size in multiple of 1MB, not 2MB starting from gen 8.

[1] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-core-desktop-vol-2-datasheet.pdf
[2] https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf

---
 hw/vfio/igd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/igd.c +1 -1
index 4047f4f071..e40e601026 100644
@@ -268,7 +268,7 @@ static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 
     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
     ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms = 1 << ggms;
     }
 

hw/vfio/igd.c void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) · line 684

Quoted 8 lines
hw/vfio/igd.c +1 -1
@@ -678,7 +678,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     /* Determine the size of stolen memory needed for GTT */
     ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms_mb = 1 << ggms_mb;
     }
 

I'd argue this should be rolled into patch 4. It's not really fixing anything because igd_gen() can't return anything between 6 and 8. This only allows for several device versions that we currently consider to be gen 6 to align with i915 kernel driver generation by calling them generation 7. We'd previously lumped them into generation 6 because there's no functional difference we care about here between 6 & 7.

Quoted 4 lines
Quoted 4 lines

In the next patch you replace this '1 << ggms_mb' with '*= 2', which would be equivalent to 'ggms_mb << 1' and matches your description that the increment is doubled. Is there a separate bug fix that needs to be pulled out here?

Sorry this was a mistake when I composing my patches. At this time, there was no gen 7, original condition won't cause any issue. It is more suitable to be included in patch 4. I will drop this one in v2.

Quoted 7 lines
Quoted 2 lines

Also, please send a cover letter for any series longer than a single patch and please configure your tools to thread the series. Thanks,

Disregard this latter comment, I just wasn't copied on the cover letter and didn't have it in my inbox to root the thread. Thanks,

Alex

CC of cover letter is not generated by get_mainainer.pl hook. Sorry for not checking it before sending, I will manually set correct CC in v2.

#5 lore raw

Re: [PATCH 1/8] vfio/igd: fix GTT stolen memory size calculation for gen 7

In reply to Tomita Moeko
Quoted 20 lines · Tomita Moeko

CAUTION: External Email!!
Both intel documentation [1][2] and i915 driver shows GGMS represents GTT stolen memory size in multiple of 1MB, not 2MB starting from gen 8.

[1]
https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-core-desktop-vol-2-datasheet.pdf
[2]
https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf

---
 hw/vfio/igd.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/igd.c +0 -0
index 4047f4f071..e40e601026 100644
@@ -268,7 +268,7 @@ static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
Quoted 5 lines


     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch)); ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {

This seems odd. The commit message talks about gen 8 but it changes the behaviour for gen 7 only. Additionally, ggms_mb is still shifted for gen 8 and later, so it's still increment by steps of 2 MB. Shouldn't this be something like gen < 8?

Quoted 5 lines

         ggms = 1 << ggms;
     }

hw/vfio/igd.c +0 -0
@@ -678,7 +678,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)
Quoted 8 lines


     /* Determine the size of stolen memory needed for GTT */ ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms_mb = 1 << ggms_mb;
     }

Btw. you could consider adding a link to the source code of i915 too.

Signature · 3 lines
Kind regards,
Corvin
disable-disclaimer-BADE
#6 lore raw

Re: [PATCH 1/8] vfio/igd: fix GTT stolen memory size calculation for gen 7

Quoted 16 lines · Corvin Köhne
Quoted 15 lines · Tomita Moeko

CAUTION: External Email!!
Both intel documentation [1][2] and i915 driver shows GGMS represents GTT stolen memory size in multiple of 1MB, not 2MB starting from gen 8.

[1]
https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-core-desktop-vol-2-datasheet.pdf
[2]
https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf

---
 hw/vfio/igd.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 15 lines
Quoted 10 lines
hw/vfio/igd.c +0 -0
index 4047f4f071..e40e601026 100644
@@ -268,7 +268,7 @@ static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)


     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch)); ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {

This seems odd. The commit message talks about gen 8 but it changes the behaviour for gen 7 only. Additionally, ggms_mb is still shifted for gen 8 and later, so it's still increment by steps of 2 MB. Shouldn't this be something like gen < 8?

Yes it should be. I think putting the change here is not correct, as alex said. I am going to remove this change in v2.

Quoted 17 lines
Quoted 5 lines

         ggms = 1 << ggms;
     }

hw/vfio/igd.c +0 -0
@@ -678,7 +678,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)
Quoted 8 lines


     /* Determine the size of stolen memory needed for GTT */ ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 6) {
+    if (gen > 7) {
         ggms_mb = 1 << ggms_mb;
     }

Btw. you could consider adding a link to the source code of i915 too.

#7 lore raw

PATCH 2/8 vfio/igd: canonicalize memory size calculations

In reply to Tomita Moeko

Add helper functions igd_gtt_memory_size() and igd_stolen_size() for calculating GTT stolen memory and Data stolen memory size in bytes, and use macros to replace the hardware-related magic numbers for better readability.

---
 hw/vfio/igd.c | 101 ++++++++++++++++++++++++++++----------------------
 1 file changed, 57 insertions(+), 44 deletions(-)
hw/vfio/igd.c +57 -44
index e40e601026..b449316ec0 100644
@@ -116,6 +116,54 @@ typedef struct VFIOIGDQuirk {
 #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
 #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later */
 
+#define IGD_GMCH_GEN6_GMS_SHIFT     3
+#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
+#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;
+        ggms *= 2;
+    }
+
+    return ggms * MiB;
+}
+
+static uint64_t igd_stolen_memory_size(int gen, uint32_t gmch)
+{
+    uint64_t gms;
+
+    if (gen < 8) {
+        gms = (gmch >> IGD_GMCH_GEN6_GMS_SHIFT) & IGD_GMCH_GEN6_GMS_MASK;
+    } else {
+        gms = (gmch >> IGD_GMCH_GEN8_GMS_SHIFT) & IGD_GMCH_GEN8_GMS_MASK;
+    }
+
+    if (gen < 9) {
+        if (gms < 0x10) {
+            return gms * 32 * MiB;
+        }
+    } else {
+        if (gms < 0xf0) {
+            return gms * 32 * MiB;
+        } else {
+            return (gms - 0xf0 + 1) * 4 * MiB;
+        }
+    }
+
+    error_report("Unsupported IGD GMS value 0x%lx", gms);
+    return 0;
+}
 
 /*
  * The rather short list of registers that we copy from the host devices.
@@ -264,17 +312,10 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
 static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 {
     uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
-    int ggms, gen = igd_gen(vdev);
-
-    gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
-    ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms = 1 << ggms;
-    }
-
-    ggms *= MiB;
+    int gen = igd_gen(vdev);
+    uint64_t ggms_size = igd_gtt_memory_size(gen, gmch);
 
-    return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
+    return (ggms_size / (4 * KiB)) * (gen < 8 ? 4 : 8);
 }
 
 /*
@@ -481,30 +522,6 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 }
 
-static int igd_get_stolen_mb(int gen, uint32_t gmch)
-{
-    int gms;
-
-    if (gen < 8) {
-        gms = (gmch >> 3) & 0x1f;
-    } else {
-        gms = (gmch >> 8) & 0xff;
-    }
-
-    if (gen < 9) {
-        if (gms > 0x10) {
-            error_report("Unsupported IGD GMS value 0x%x", gms);
-            return 0;
-        }
-        return gms * 32;
-    } else {
-        if (gms < 0xf0)
-            return gms * 32;
-        else
-            return (gms - 0xf0) * 4 + 4;
-    }
-}
-
 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 {
     g_autofree struct vfio_region_info *rom = NULL;
@@ -514,7 +531,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     VFIOQuirk *quirk;
     VFIOIGDQuirk *igd;
     PCIDevice *lpc_bridge;
-    int i, ret, ggms_mb, gms_mb = 0, gen;
+    int i, ret, gen;
+    uint64_t ggms_size, gms_size;
     uint64_t *bdsm_size;
     uint32_t gmch;
     uint16_t cmd_orig, cmd;
@@ -676,13 +694,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 
-    /* Determine the size of stolen memory needed for GTT */
-    ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms_mb = 1 << ggms_mb;
-    }
-
-    gms_mb = igd_get_stolen_mb(gen, gmch);
+    ggms_size = igd_gtt_memory_size(gen, gmch);
+    gms_size = igd_stolen_memory_size(gen, gmch);
 
     /*
      * Request reserved memory for stolen memory via fw_cfg.  VM firmware
@@ -693,7 +706,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
      * config offset 0x5C.
      */
     bdsm_size = g_malloc(sizeof(*bdsm_size));
-    *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
+    *bdsm_size = cpu_to_le64(ggms_size + gms_size);
     fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
                     bdsm_size, sizeof(*bdsm_size));
 
@@ -744,5 +757,5 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
                      vdev->vbasedev.name);
     }
 
-    trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
+    trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, *bdsm_size / MiB);
 }
Signature · 1 line
2.45.2
#8 lore raw

Re: [PATCH 2/8] vfio/igd: canonicalize memory size calculations

Quoted 45 lines · On Mon, 2 Dec 2024 00:09:32 +0800 Tomita Moeko <[email protected]> wrote:

Add helper functions igd_gtt_memory_size() and igd_stolen_size() for calculating GTT stolen memory and Data stolen memory size in bytes, and use macros to replace the hardware-related magic numbers for better readability.

---
 hw/vfio/igd.c | 101 ++++++++++++++++++++++++++++----------------------
 1 file changed, 57 insertions(+), 44 deletions(-)
hw/vfio/igd.c +27 -0
index e40e601026..b449316ec0 100644
@@ -116,6 +116,54 @@ typedef struct VFIOIGDQuirk {
 #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
 #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later */
 
+#define IGD_GMCH_GEN6_GMS_SHIFT     3
+#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
+#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;
+        ggms *= 2;
+    }
+
+    return ggms * MiB;
+}
+
+static uint64_t igd_stolen_memory_size(int gen, uint32_t gmch)
+{
+    uint64_t gms;
+

hw/vfio/igd.c typedef struct VFIOIGDQuirk { · line 155

Quoted 10 lines
hw/vfio/igd.c +10 -0
+    if (gen < 8) {
+        gms = (gmch >> IGD_GMCH_GEN6_GMS_SHIFT) & IGD_GMCH_GEN6_GMS_MASK;
+    } else {
+        gms = (gmch >> IGD_GMCH_GEN8_GMS_SHIFT) & IGD_GMCH_GEN8_GMS_MASK;
+    }
+
+    if (gen < 9) {
+        if (gms < 0x10) {
+            return gms * 32 * MiB;
+        }

igd_get_stolen_mb() used:

        if (gms > 0x10) {
            error_report("Unsupported IGD GMS value 0x%x", gms);
            return 0;
        }
        return gms * 32;

Therefore this should use <= to be equivalent.

hw/vfio/igd.c typedef struct VFIOIGDQuirk { · line 160

Quoted 5 lines
hw/vfio/igd.c +5 -0
+    } else {
+        if (gms < 0xf0) {
+            return gms * 32 * MiB;
+        } else {
+            return (gms - 0xf0 + 1) * 4 * MiB;

This does not match the line from igd_get_stolen_mb():

            return (gms - 0xf0) * 4 + 4;

Explain.

Quoted 97 lines
hw/vfio/igd.c +14 -43
+        }
+    }
+
+    error_report("Unsupported IGD GMS value 0x%lx", gms);
+    return 0;
+}
 
 /*
  * The rather short list of registers that we copy from the host devices.
@@ -264,17 +312,10 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
 static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 {
     uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
-    int ggms, gen = igd_gen(vdev);
-
-    gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
-    ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms = 1 << ggms;
-    }
-
-    ggms *= MiB;
+    int gen = igd_gen(vdev);
+    uint64_t ggms_size = igd_gtt_memory_size(gen, gmch);
 
-    return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
+    return (ggms_size / (4 * KiB)) * (gen < 8 ? 4 : 8);
 }
 
 /*
@@ -481,30 +522,6 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 }
 
-static int igd_get_stolen_mb(int gen, uint32_t gmch)
-{
-    int gms;
-
-    if (gen < 8) {
-        gms = (gmch >> 3) & 0x1f;
-    } else {
-        gms = (gmch >> 8) & 0xff;
-    }
-
-    if (gen < 9) {
-        if (gms > 0x10) {
-            error_report("Unsupported IGD GMS value 0x%x", gms);
-            return 0;
-        }
-        return gms * 32;
-    } else {
-        if (gms < 0xf0)
-            return gms * 32;
-        else
-            return (gms - 0xf0) * 4 + 4;
-    }
-}
-
 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 {
     g_autofree struct vfio_region_info *rom = NULL;
@@ -514,7 +531,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     VFIOQuirk *quirk;
     VFIOIGDQuirk *igd;
     PCIDevice *lpc_bridge;
-    int i, ret, ggms_mb, gms_mb = 0, gen;
+    int i, ret, gen;
+    uint64_t ggms_size, gms_size;
     uint64_t *bdsm_size;
     uint32_t gmch;
     uint16_t cmd_orig, cmd;
@@ -676,13 +694,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 
-    /* Determine the size of stolen memory needed for GTT */
-    ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms_mb = 1 << ggms_mb;
-    }
-
-    gms_mb = igd_get_stolen_mb(gen, gmch);
+    ggms_size = igd_gtt_memory_size(gen, gmch);
+    gms_size = igd_stolen_memory_size(gen, gmch);
 
     /*
      * Request reserved memory for stolen memory via fw_cfg.  VM firmware
@@ -693,7 +706,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
      * config offset 0x5C.
      */
     bdsm_size = g_malloc(sizeof(*bdsm_size));
-    *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
+    *bdsm_size = cpu_to_le64(ggms_size + gms_size);
     fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
                     bdsm_size, sizeof(*bdsm_size));
 

hw/vfio/igd.c void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) · line 760

Quoted 5 lines
hw/vfio/igd.c +1 -1
@@ -744,5 +757,5 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
                      vdev->vbasedev.name);
     }
 
-    trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
+    trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, *bdsm_size / MiB);

Technically this should use le64_to_cpu(*bdsm_size), or retain the addition logic. Thanks,

Alex

#9 lore raw

Re: [PATCH 2/8] vfio/igd: canonicalize memory size calculations

Quoted 25 lines · Alex Williamson

CAUTION: External Email!!

Quoted 21 lines · On Mon,  2 Dec 2024 00:09:32 +0800 Tomita Moeko <[email protected]> wrote:

Add helper functions igd_gtt_memory_size() and igd_stolen_size() for calculating GTT stolen memory and Data stolen memory size in bytes, and use macros to replace the hardware-related magic numbers for better readability.

---
 hw/vfio/igd.c | 101 ++++++++++++++++++++++++++++-------------------
---

 1 file changed, 57 insertions(+), 44 deletions(-)
hw/vfio/igd.c +0 -0
index e40e601026..b449316ec0 100644
@@ -116,6 +116,54 @@ typedef struct VFIOIGDQuirk {
  #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
  #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11

and later */

+#define IGD_GMCH_GEN6_GMS_SHIFT     3

Quoted 8 lines

hw/vfio/igd.c typedef struct VFIOIGDQuirk { · line 127

Quoted 8 lines
hw/vfio/igd.c +8 -0
+#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
+#define IGD_GMCH_GEN8_GMS_MASK      0xff
+#define IGD_GMCH_GEN8_GGMS_SHIFT    6
+#define IGD_GMCH_GEN8_GGMS_MASK     0x3
+

You should consider reusing the names used by Linux [1].

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/drm/intel/i915_drm.h?h=v6.12#n50

Quoted 35 lines
Quoted 24 lines
hw/vfio/igd.c +6 -0
+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;
+        ggms *= 2;
+    }

hw/vfio/igd.c +10 -0
+
+    return ggms * MiB;
+}
+
+static uint64_t igd_stolen_memory_size(int gen, uint32_t gmch)
+{
+    uint64_t gms;
+
+    if (gen < 8) {
+        gms = (gmch >> IGD_GMCH_GEN6_GMS_SHIFT) &

IGD_GMCH_GEN6_GMS_MASK;
+    } else {

Quoted 8 lines

+        gms = (gmch >> IGD_GMCH_GEN8_GMS_SHIFT) & IGD_GMCH_GEN8_GMS_MASK;
+    }
+
+    if (gen < 9) {
+        if (gms < 0x10) {
+            return gms * 32 * MiB;
+        }

igd_get_stolen_mb() used:

Quoted 7 lines

        if (gms > 0x10) {
            error_report("Unsupported IGD GMS value 0x%x", gms); return 0;
        }
        return gms * 32;

Therefore this should use <= to be equivalent.

I'd personally prefer moving the code in one commit and change the format in a second one.

If you want to touch this, I'd align it with Linux, which multiplies gms by 32 MB unconditionally for gen 6 and 8 [1].

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/early-quirks.c?h=v6.12#n404

Quoted 6 lines
Quoted 5 lines

+    } else {
+        if (gms < 0xf0) {
+            return gms * 32 * MiB;
+        } else {
+            return (gms - 0xf0 + 1) * 4 * MiB;

Quoted 6 lines

This does not match the line from igd_get_stolen_mb():

            return (gms - 0xf0) * 4 + 4;

Explain.

I've chosen the current format because it aligns with Linux [1]. I would keep it as is to make it easier to sync with Linux.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/early-quirks.c?h=v6.12#n460

Quoted 129 lines
Quoted 116 lines

+        }
+    }

hw/vfio/igd.c +4 -0
+
+    error_report("Unsupported IGD GMS value 0x%lx", gms);
+    return 0;
+}


 /*
  * The rather short list of registers that we copy from the host devices.

hw/vfio/igd.c +2 -9
@@ -264,17 +312,10 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice
 *vdev,
  static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
  {
      uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH,
 sizeof(gmch));
-    int ggms, gen = igd_gen(vdev);
-
-    gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH,
 sizeof(gmch));
-    ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms = 1 << ggms;
-    }
-
-    ggms *= MiB;
+    int gen = igd_gen(vdev);
+    uint64_t ggms_size = igd_gtt_memory_size(gen, gmch);


-    return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8); +    return (ggms_size / (4 * KiB)) * (gen < 8 ? 4 : 8); }

 /*

hw/vfio/igd.c +0 -0
@@ -481,30 +522,6 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice
 *vdev, int nr)
      QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
  }

hw/vfio/igd.c +0 -24
-static int igd_get_stolen_mb(int gen, uint32_t gmch)
-{
-    int gms;
-
-    if (gen < 8) {
-        gms = (gmch >> 3) & 0x1f;
-    } else {
-        gms = (gmch >> 8) & 0xff;
-    }
-
-    if (gen < 9) {
-        if (gms > 0x10) {
-            error_report("Unsupported IGD GMS value 0x%x", gms);
-            return 0;
-        }
-        return gms * 32;
-    } else {
-        if (gms < 0xf0)
-            return gms * 32;
-        else
-            return (gms - 0xf0) * 4 + 4;
-    }
-}
-

 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) {
     g_autofree struct vfio_region_info *rom = NULL;

hw/vfio/igd.c +2 -1
@@ -514,7 +531,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)
      VFIOQuirk *quirk;
      VFIOIGDQuirk *igd;
      PCIDevice *lpc_bridge;
-    int i, ret, ggms_mb, gms_mb = 0, gen;
+    int i, ret, gen;
+    uint64_t ggms_size, gms_size;
      uint64_t *bdsm_size;
      uint32_t gmch;

     uint16_t cmd_orig, cmd;

hw/vfio/igd.c +0 -0
@@ -676,13 +694,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)


     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
-    /* Determine the size of stolen memory needed for GTT */ -    ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms_mb = 1 << ggms_mb;
-    }
-
-    gms_mb = igd_get_stolen_mb(gen, gmch);
+    ggms_size = igd_gtt_memory_size(gen, gmch);
+    gms_size = igd_stolen_memory_size(gen, gmch);

     /*
      * Request reserved memory for stolen memory via fw_cfg.  VM firmware

hw/vfio/igd.c +1 -1
@@ -693,7 +706,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)
       * config offset 0x5C.
       */
      bdsm_size = g_malloc(sizeof(*bdsm_size));
-    *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
+    *bdsm_size = cpu_to_le64(ggms_size + gms_size);
      fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
                      bdsm_size, sizeof(*bdsm_size));

hw/vfio/igd.c +0 -0
@@ -744,5 +757,5 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)

                      vdev-

Quoted 1 line

https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAD3YuKN4_e

If2wAAADsWKVxcuZtvheRgRqIemuzgT-OABVjIqlMqKN- EU30bxV28qXZ_Y5_SMs0YfH8q_KtHOPLp1QG1mJuzWERjIVCEePSftEKzXou_Qr3jboY Ii_VUdvptqZTIPyfY-GJ9iOrnUrKoc_oifOB9wY1 );
     }

-    trace_vfio_pci_igd_bdsm_enabled(vdev-

Quoted 1 line

https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAD3YuKN4_e

Quoted 8 lines

If2wAAADsWKVxcuZtvheRgRqIemuzgT-OABVjIqlMqKN- EU30bxV28qXZ_Y5_SMs0YfH8q_KtHOPLp1QG1mJuzWERjIVCEePSftEKzXou_Qr3jboY Ii_VUdvptqZTIPyfY-GJ9iOrnUrKoc_oifOB9wY1 , ggms_mb + gms_mb); +    trace_vfio_pci_igd_bdsm_enabled(vdev-

Quoted 1 line

https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAD3YuKN4_e

If2wAAADsWKVxcuZtvheRgRqIemuzgT-OABVjIqlMqKN- EU30bxV28qXZ_Y5_SMs0YfH8q_KtHOPLp1QG1mJuzWERjIVCEePSftEKzXou_Qr3jboY Ii_VUdvptqZTIPyfY-GJ9iOrnUrKoc_oifOB9wY1 , *bdsm_size / MiB);

Technically this should use le64_to_cpu(*bdsm_size), or retain the addition logic.  Thanks,

Alex

Signature · 3 lines
Kind regards,
Corvin
disable-disclaimer-BADE
#10 lore raw

Re: [PATCH 2/8] vfio/igd: canonicalize memory size calculations

Quoted 37 lines · Corvin Köhne
Quoted 25 lines · Alex Williamson

CAUTION: External Email!!

Quoted 21 lines · On Mon,  2 Dec 2024 00:09:32 +0800 Tomita Moeko <[email protected]> wrote:

Add helper functions igd_gtt_memory_size() and igd_stolen_size() for calculating GTT stolen memory and Data stolen memory size in bytes, and use macros to replace the hardware-related magic numbers for better readability.

---
 hw/vfio/igd.c | 101 ++++++++++++++++++++++++++++-------------------
---

 1 file changed, 57 insertions(+), 44 deletions(-)
hw/vfio/igd.c +0 -0
index e40e601026..b449316ec0 100644
@@ -116,6 +116,54 @@ typedef struct VFIOIGDQuirk {
  #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
  #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11

and later */

+#define IGD_GMCH_GEN6_GMS_SHIFT     3

Quoted 8 lines

hw/vfio/igd.c typedef struct VFIOIGDQuirk { · line 127

Quoted 8 lines
hw/vfio/igd.c +8 -0
+#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
+#define IGD_GMCH_GEN8_GMS_MASK      0xff
+#define IGD_GMCH_GEN8_GGMS_SHIFT    6
+#define IGD_GMCH_GEN8_GGMS_MASK     0x3
+

You should consider reusing the names used by Linux [1].

Quoted 2 lines

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/drm/intel/i915_drm.h?h=v6.12#n50

Original SNB/BDW in i915 is not easy to understand I think, probably adding a comment here is better?

Quoted 62 lines
Quoted 35 lines
Quoted 24 lines
hw/vfio/igd.c +6 -0
+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;
+        ggms *= 2;
+    }

hw/vfio/igd.c +10 -0
+
+    return ggms * MiB;
+}
+
+static uint64_t igd_stolen_memory_size(int gen, uint32_t gmch)
+{
+    uint64_t gms;
+
+    if (gen < 8) {
+        gms = (gmch >> IGD_GMCH_GEN6_GMS_SHIFT) &

IGD_GMCH_GEN6_GMS_MASK;
+    } else {

Quoted 8 lines

+        gms = (gmch >> IGD_GMCH_GEN8_GMS_SHIFT) & IGD_GMCH_GEN8_GMS_MASK;
+    }
+
+    if (gen < 9) {
+        if (gms < 0x10) {
+            return gms * 32 * MiB;
+        }

igd_get_stolen_mb() used:

Quoted 7 lines

        if (gms > 0x10) {
            error_report("Unsupported IGD GMS value 0x%x", gms); return 0;
        }
        return gms * 32;

Therefore this should use <= to be equivalent.

I'd personally prefer moving the code in one commit and change the format in a second one.

If you want to touch this, I'd align it with Linux, which multiplies gms by 32 MB unconditionally for gen 6 and 8 [1].

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/early-quirks.c?h=v6.12#n404

Quoted 10 lines
Quoted 5 lines

+    } else {
+        if (gms < 0xf0) {
+            return gms * 32 * MiB;
+        } else {
+            return (gms - 0xf0 + 1) * 4 * MiB;

This does not match the line from igd_get_stolen_mb():

            return (gms - 0xf0) * 4 + 4;

Quoted 8 lines
Quoted 2 lines

Explain.

I've chosen the current format because it aligns with Linux [1]. I would keep it as is to make it easier to sync with Linux.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/early-quirks.c?h=v6.12#n460

Will use

	return ((gms - 0xf0) * 4 + 4) * MiB;

in v2

Quoted 130 lines
Quoted 129 lines
Quoted 116 lines

+        }
+    }

hw/vfio/igd.c +4 -0
+
+    error_report("Unsupported IGD GMS value 0x%lx", gms);
+    return 0;
+}


 /*
  * The rather short list of registers that we copy from the host devices.

hw/vfio/igd.c +2 -9
@@ -264,17 +312,10 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice
 *vdev,
  static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
  {
      uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH,
 sizeof(gmch));
-    int ggms, gen = igd_gen(vdev);
-
-    gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH,
 sizeof(gmch));
-    ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms = 1 << ggms;
-    }
-
-    ggms *= MiB;
+    int gen = igd_gen(vdev);
+    uint64_t ggms_size = igd_gtt_memory_size(gen, gmch);


-    return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8); +    return (ggms_size / (4 * KiB)) * (gen < 8 ? 4 : 8); }

 /*

hw/vfio/igd.c +0 -0
@@ -481,30 +522,6 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice
 *vdev, int nr)
      QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
  }

hw/vfio/igd.c +0 -24
-static int igd_get_stolen_mb(int gen, uint32_t gmch)
-{
-    int gms;
-
-    if (gen < 8) {
-        gms = (gmch >> 3) & 0x1f;
-    } else {
-        gms = (gmch >> 8) & 0xff;
-    }
-
-    if (gen < 9) {
-        if (gms > 0x10) {
-            error_report("Unsupported IGD GMS value 0x%x", gms);
-            return 0;
-        }
-        return gms * 32;
-    } else {
-        if (gms < 0xf0)
-            return gms * 32;
-        else
-            return (gms - 0xf0) * 4 + 4;
-    }
-}
-

 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) {
     g_autofree struct vfio_region_info *rom = NULL;

hw/vfio/igd.c +2 -1
@@ -514,7 +531,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)
      VFIOQuirk *quirk;
      VFIOIGDQuirk *igd;
      PCIDevice *lpc_bridge;
-    int i, ret, ggms_mb, gms_mb = 0, gen;
+    int i, ret, gen;
+    uint64_t ggms_size, gms_size;
      uint64_t *bdsm_size;
      uint32_t gmch;

     uint16_t cmd_orig, cmd;

hw/vfio/igd.c +0 -0
@@ -676,13 +694,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)


     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
-    /* Determine the size of stolen memory needed for GTT */ -    ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms_mb = 1 << ggms_mb;
-    }
-
-    gms_mb = igd_get_stolen_mb(gen, gmch);
+    ggms_size = igd_gtt_memory_size(gen, gmch);
+    gms_size = igd_stolen_memory_size(gen, gmch);

     /*
      * Request reserved memory for stolen memory via fw_cfg.  VM firmware

hw/vfio/igd.c +1 -1
@@ -693,7 +706,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)
       * config offset 0x5C.
       */
      bdsm_size = g_malloc(sizeof(*bdsm_size));
-    *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
+    *bdsm_size = cpu_to_le64(ggms_size + gms_size);
      fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
                      bdsm_size, sizeof(*bdsm_size));

hw/vfio/igd.c +0 -0
@@ -744,5 +757,5 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice
 *vdev, int nr)

                      vdev-

Quoted 1 line

https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAD3YuKN4_e

If2wAAADsWKVxcuZtvheRgRqIemuzgT-OABVjIqlMqKN- EU30bxV28qXZ_Y5_SMs0YfH8q_KtHOPLp1QG1mJuzWERjIVCEePSftEKzXou_Qr3jboY Ii_VUdvptqZTIPyfY-GJ9iOrnUrKoc_oifOB9wY1 );
     }

-    trace_vfio_pci_igd_bdsm_enabled(vdev-

Quoted 1 line

https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAD3YuKN4_e

Quoted 8 lines

If2wAAADsWKVxcuZtvheRgRqIemuzgT-OABVjIqlMqKN- EU30bxV28qXZ_Y5_SMs0YfH8q_KtHOPLp1QG1mJuzWERjIVCEePSftEKzXou_Qr3jboY Ii_VUdvptqZTIPyfY-GJ9iOrnUrKoc_oifOB9wY1 , ggms_mb + gms_mb); +    trace_vfio_pci_igd_bdsm_enabled(vdev-

Quoted 1 line

https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAD3YuKN4_e

If2wAAADsWKVxcuZtvheRgRqIemuzgT-OABVjIqlMqKN- EU30bxV28qXZ_Y5_SMs0YfH8q_KtHOPLp1QG1mJuzWERjIVCEePSftEKzXou_Qr3jboY Ii_VUdvptqZTIPyfY-GJ9iOrnUrKoc_oifOB9wY1 , *bdsm_size / MiB);

Technically this should use le64_to_cpu(*bdsm_size), or retain the addition logic.  Thanks,

Alex

#11 lore raw

Re: [PATCH 2/8] vfio/igd: canonicalize memory size calculations

In reply to Alex Williamson
Quoted 61 lines · Alex Williamson
Quoted 45 lines · On Mon, 2 Dec 2024 00:09:32 +0800 Tomita Moeko <[email protected]> wrote:

Add helper functions igd_gtt_memory_size() and igd_stolen_size() for calculating GTT stolen memory and Data stolen memory size in bytes, and use macros to replace the hardware-related magic numbers for better readability.

---
 hw/vfio/igd.c | 101 ++++++++++++++++++++++++++++----------------------
 1 file changed, 57 insertions(+), 44 deletions(-)
hw/vfio/igd.c +27 -0
index e40e601026..b449316ec0 100644
@@ -116,6 +116,54 @@ typedef struct VFIOIGDQuirk {
 #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
 #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later */
 
+#define IGD_GMCH_GEN6_GMS_SHIFT     3
+#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
+#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;
+        ggms *= 2;
+    }
+
+    return ggms * MiB;
+}
+
+static uint64_t igd_stolen_memory_size(int gen, uint32_t gmch)
+{
+    uint64_t gms;
+

hw/vfio/igd.c typedef struct VFIOIGDQuirk { · line 155

Quoted 10 lines
hw/vfio/igd.c +10 -0
+    if (gen < 8) {
+        gms = (gmch >> IGD_GMCH_GEN6_GMS_SHIFT) & IGD_GMCH_GEN6_GMS_MASK;
+    } else {
+        gms = (gmch >> IGD_GMCH_GEN8_GMS_SHIFT) & IGD_GMCH_GEN8_GMS_MASK;
+    }
+
+    if (gen < 9) {
+        if (gms < 0x10) {
+            return gms * 32 * MiB;
+        }

igd_get_stolen_mb() used:

Quoted 7 lines
        if (gms > 0x10) {
            error_report("Unsupported IGD GMS value 0x%x", gms);
            return 0;
        }
        return gms * 32;

Therefore this should use <= to be equivalent.

Got it, will fix in v2

Quoted 11 lines

hw/vfio/igd.c typedef struct VFIOIGDQuirk { · line 160

Quoted 5 lines
hw/vfio/igd.c +5 -0
+    } else {
+        if (gms < 0xf0) {
+            return gms * 32 * MiB;
+        } else {
+            return (gms - 0xf0 + 1) * 4 * MiB;

This does not match the line from igd_get_stolen_mb():

            return (gms - 0xf0) * 4 + 4;

Explain.

It was changed to return size in bytes, instead of size in MB, to make the behavior of 2 size calculation functions consistent. Original one should be changed to.

	return ((gms - 0xf0) * 4 + 4) * MiB;
Then I changed it to
	return (gms - 0xf0 + 1) * 4 * MiB;

to make it easier to read. gms is uint64, the masked value is 0xff at most, +1 won't cause overflow here.

Quoted 100 lines
Quoted 100 lines
hw/vfio/igd.c +14 -43
+        }
+    }
+
+    error_report("Unsupported IGD GMS value 0x%lx", gms);
+    return 0;
+}
 
 /*
  * The rather short list of registers that we copy from the host devices.
@@ -264,17 +312,10 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
 static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
 {
     uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
-    int ggms, gen = igd_gen(vdev);
-
-    gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
-    ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms = 1 << ggms;
-    }
-
-    ggms *= MiB;
+    int gen = igd_gen(vdev);
+    uint64_t ggms_size = igd_gtt_memory_size(gen, gmch);
 
-    return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
+    return (ggms_size / (4 * KiB)) * (gen < 8 ? 4 : 8);
 }
 
 /*
@@ -481,30 +522,6 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 }
 
-static int igd_get_stolen_mb(int gen, uint32_t gmch)
-{
-    int gms;
-
-    if (gen < 8) {
-        gms = (gmch >> 3) & 0x1f;
-    } else {
-        gms = (gmch >> 8) & 0xff;
-    }
-
-    if (gen < 9) {
-        if (gms > 0x10) {
-            error_report("Unsupported IGD GMS value 0x%x", gms);
-            return 0;
-        }
-        return gms * 32;
-    } else {
-        if (gms < 0xf0)
-            return gms * 32;
-        else
-            return (gms - 0xf0) * 4 + 4;
-    }
-}
-
 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 {
     g_autofree struct vfio_region_info *rom = NULL;
@@ -514,7 +531,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     VFIOQuirk *quirk;
     VFIOIGDQuirk *igd;
     PCIDevice *lpc_bridge;
-    int i, ret, ggms_mb, gms_mb = 0, gen;
+    int i, ret, gen;
+    uint64_t ggms_size, gms_size;
     uint64_t *bdsm_size;
     uint32_t gmch;
     uint16_t cmd_orig, cmd;
@@ -676,13 +694,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 
-    /* Determine the size of stolen memory needed for GTT */
-    ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
-    if (gen > 7) {
-        ggms_mb = 1 << ggms_mb;
-    }
-
-    gms_mb = igd_get_stolen_mb(gen, gmch);
+    ggms_size = igd_gtt_memory_size(gen, gmch);
+    gms_size = igd_stolen_memory_size(gen, gmch);
 
     /*
      * Request reserved memory for stolen memory via fw_cfg.  VM firmware
@@ -693,7 +706,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
      * config offset 0x5C.
      */
     bdsm_size = g_malloc(sizeof(*bdsm_size));
-    *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
+    *bdsm_size = cpu_to_le64(ggms_size + gms_size);
     fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
                     bdsm_size, sizeof(*bdsm_size));
 
@@ -744,5 +757,5 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
                      vdev->vbasedev.name);
     }
 
Quoted 5 lines

hw/vfio/igd.c void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) · line 760

Quoted 2 lines
hw/vfio/igd.c +1 -1
-    trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
+    trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, *bdsm_size / MiB);

Technically this should use le64_to_cpu(*bdsm_size), or retain the addition logic. Thanks,

Got it.

Quoted 2 lines

Alex

#12 lore raw

PATCH 3/8 vfio/igd: remove unsupported device ids

In reply to Tomita Moeko

Since e433f208973f ("vfio/igd: return an invalid generation for unknown devices"), the default return of igd_gen() was changed to unsupported. There is no need to filter out those unsupported devices.

---
 hw/vfio/igd.c | 10 ----------
 1 file changed, 10 deletions(-)
hw/vfio/igd.c +0 -10
index b449316ec0..8f300498e4 100644
@@ -64,16 +64,6 @@ static int igd_gen(VFIOPCIDevice *vdev)
     }
 
     switch (vdev->device_id & 0xff00) {
-    /* Old, untested, unavailable, unknown */
-    case 0x0000:
-    case 0x2500:
-    case 0x2700:
-    case 0x2900:
-    case 0x2a00:
-    case 0x2e00:
-    case 0x3500:
-    case 0xa000:
-        return -1;
     /* SandyBridge, IvyBridge, ValleyView, Haswell */
     case 0x0100:
     case 0x0400:
Signature · 1 line
2.45.2
Reviewed-by on v1 3/8
#13 lore raw

Re: [PATCH 3/8] vfio/igd: remove unsupported device ids

Quoted 20 lines · On Mon, 2 Dec 2024 00:09:33 +0800 Tomita Moeko <[email protected]> wrote:

Since e433f208973f ("vfio/igd: return an invalid generation for unknown devices"), the default return of igd_gen() was changed to unsupported. There is no need to filter out those unsupported devices.

---
 hw/vfio/igd.c | 10 ----------
 1 file changed, 10 deletions(-)
hw/vfio/igd.c +0 -3
index b449316ec0..8f300498e4 100644
@@ -64,16 +64,6 @@ static int igd_gen(VFIOPCIDevice *vdev)
     }
 
     switch (vdev->device_id & 0xff00) {
-    /* Old, untested, unavailable, unknown */
-    case 0x0000:
-    case 0x2500:

hw/vfio/igd.c static int igd_gen(VFIOPCIDevice *vdev) · line 69

Quoted 10 lines
hw/vfio/igd.c +0 -7
-    case 0x2700:
-    case 0x2900:
-    case 0x2a00:
-    case 0x2e00:
-    case 0x3500:
-    case 0xa000:
-        return -1;
     /* SandyBridge, IvyBridge, ValleyView, Haswell */
     case 0x0100:
     case 0x0400:
Reviewed-by
#14 lore raw

Re: [PATCH 3/8] vfio/igd: remove unsupported device ids

Quoted 30 lines · Alex Williamson

CAUTION: External Email!!

Quoted 26 lines · On Mon,  2 Dec 2024 00:09:33 +0800 Tomita Moeko <[email protected]> wrote:

Since e433f208973f ("vfio/igd: return an invalid generation for unknown
devices"), the default return of igd_gen() was changed to unsupported.
There is no need to filter out those unsupported devices.

---
 hw/vfio/igd.c | 10 ----------

 1 file changed, 10 deletions(-)
hw/vfio/igd.c +0 -0
index b449316ec0..8f300498e4 100644
@@ -64,16 +64,6 @@ static int igd_gen(VFIOPCIDevice *vdev)
      }


     switch (vdev->device_id & 0xff00) {
-    /* Old, untested, unavailable, unknown */
-    case 0x0000:
-    case 0x2500:
-    case 0x2700:
-    case 0x2900:
-    case 0x2a00:
-    case 0x2e00:

Quoted 10 lines
Quoted 6 lines

-    case 0x3500:
-    case 0xa000:
-        return -1;
     /* SandyBridge, IvyBridge, ValleyView, Haswell */ case 0x0100:
     case 0x0400:

disable-disclaimer-BADE

#15 lore raw

PATCH 4/8 vfio/igd: align generation with i915 kernel driver

In reply to Tomita Moeko

Define the igd device generations according to i915 kernel driver to avoid confusion, and adjust comment placement to clearly reflect the relationship between ids and devices.

---
 hw/vfio/igd.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
hw/vfio/igd.c +16 -20
index 8f300498e4..71342863d6 100644
@@ -59,33 +59,29 @@
  */
 static int igd_gen(VFIOPCIDevice *vdev)
 {
-    if ((vdev->device_id & 0xfff) == 0xa84) {
-        return 8; /* Broxton */
+    if ((vdev->device_id & 0xffe) == 0xa84) {
+        return 9;   /* Broxton/Apollo Lake, Gemini Lake */
     }
 
     switch (vdev->device_id & 0xff00) {
-    /* SandyBridge, IvyBridge, ValleyView, Haswell */
-    case 0x0100:
-    case 0x0400:
-    case 0x0a00:
-    case 0x0c00:
-    case 0x0d00:
-    case 0x0f00:
+    case 0x0100:    /* SandyBridge, IvyBridge */
         return 6;
-    /* BroadWell, CherryView, SkyLake, KabyLake */
-    case 0x1600:
-    case 0x1900:
-    case 0x2200:
-    case 0x5900:
+    case 0x0400:    /* Haswell */
+    case 0x0a00:    /* Haswell */
+    case 0x0c00:    /* Haswell */
+    case 0x0d00:    /* Haswell */
+    case 0x0f00:    /* Valleyview/Bay Trail */
+        return 7;
+    case 0x1600:    /* Broadwell */
+    case 0x2200:    /* Cherryview */
         return 8;
-    /* CoffeeLake */
-    case 0x3e00:
+    case 0x1900:    /* Skylake */
+    case 0x5900:    /* Kaby Lake */
+    case 0x3e00:    /* Coffee Lake */
         return 9;
-    /* ElkhartLake */
-    case 0x4500:
+    case 0x4500:    /* Elkhart Lake */
         return 11;
-    /* TigerLake */
-    case 0x9A00:
+    case 0x9A00:    /* Tiger Lake */
         return 12;
     }
 
Signature · 1 line
2.45.2
#16 lore raw

Re: [PATCH 4/8] vfio/igd: align generation with i915 kernel driver

Quoted 14 lines · On Mon, 2 Dec 2024 00:09:34 +0800 Tomita Moeko <[email protected]> wrote:

Define the igd device generations according to i915 kernel driver to avoid confusion, and adjust comment placement to clearly reflect the relationship between ids and devices.

---
 hw/vfio/igd.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
hw/vfio/igd.c +0 -0
index 8f300498e4..71342863d6 100644

hw/vfio/igd.c line 64

Quoted 8 lines
hw/vfio/igd.c +2 -2
@@ -59,33 +59,29 @@
  */
 static int igd_gen(VFIOPCIDevice *vdev)
 {
-    if ((vdev->device_id & 0xfff) == 0xa84) {
-        return 8; /* Broxton */
+    if ((vdev->device_id & 0xffe) == 0xa84) {
+        return 9;   /* Broxton/Apollo Lake, Gemini Lake */
     }

J4105 is a Gemini Lake, the IGD device ID is 0x3185 according to ark[1]??

I do find Apollo Lake examples (N3450) with ID 0x5a85, which I assume justifies masking out the zero bit of the ID.

Since Broxton was apparently canceled, I think this unique device ID test was concocted from i915. Maybe Apollo Lake should just be added under gen9 below with 0x5a00? Then Gemini Lake should be added as 0x3100. Both should be noted as new IDs. Have either been tested?

Quoted 10 lines
hw/vfio/igd.c +1 -7
     switch (vdev->device_id & 0xff00) {
-    /* SandyBridge, IvyBridge, ValleyView, Haswell */
-    case 0x0100:
-    case 0x0400:
-    case 0x0a00:
-    case 0x0c00:
-    case 0x0d00:
-    case 0x0f00:
+    case 0x0100:    /* SandyBridge, IvyBridge */

hw/vfio/igd.c line 72

Quoted 10 lines
hw/vfio/igd.c +4 -5
         return 6;
-    /* BroadWell, CherryView, SkyLake, KabyLake */
-    case 0x1600:
-    case 0x1900:
-    case 0x2200:
-    case 0x5900:
+    case 0x0400:    /* Haswell */
+    case 0x0a00:    /* Haswell */
+    case 0x0c00:    /* Haswell */
+    case 0x0d00:    /* Haswell */

Crystal Well?

hw/vfio/igd.c line 76

Quoted 4 lines
hw/vfio/igd.c +4 -0
+    case 0x0f00:    /* Valleyview/Bay Trail */
+        return 7;
+    case 0x1600:    /* Broadwell */
+    case 0x2200:    /* Cherryview */

These two jump from gen6/7 to gen8, that's certainly not just a cosmetic change.

hw/vfio/igd.c line 79

Quoted 5 lines
hw/vfio/igd.c +2 -2
         return 8;
-    /* CoffeeLake */
-    case 0x3e00:
+    case 0x1900:    /* Skylake */
+    case 0x5900:    /* Kaby Lake */

These two jump from gen6/7 to gen9, so again, functional differences for these. Have these changes been tested?

hw/vfio/igd.c line 87

Quoted 12 lines
hw/vfio/igd.c +3 -4
+    case 0x3e00:    /* Coffee Lake */
         return 9;
-    /* ElkhartLake */
-    case 0x4500:
+    case 0x4500:    /* Elkhart Lake */
         return 11;
-    /* TigerLake */
-    case 0x9A00:
+    case 0x9A00:    /* Tiger Lake */
         return 12;
     }
 

Actually, patch 1 doesn't even need to be addressed here because patch 2 already made the change to check (gen < 8) rather than (gen > 6), so we can just entirely drop patch 1 (ideally with an updated commit log in 2 to note the logic change for this upcoming split of gen6/7). Thanks,

Alex

[1]https://www.intel.com/content/www/us/en/products/sku/128989/intel-celeron-j4105-processor-4m-cache-up-to-2-50-ghz/specifications.html

#17 lore raw

Re: [PATCH 4/8] vfio/igd: align generation with i915 kernel driver

Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Mon, 2 Dec 2024 00:09:34 +0800 Tomita Moeko <[email protected]> wrote:

Define the igd device generations according to i915 kernel driver to avoid confusion, and adjust comment placement to clearly reflect the relationship between ids and devices.

---
 hw/vfio/igd.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
Quoted 16 lines

hw/vfio/igd.c line 64

Quoted 13 lines
hw/vfio/igd.c +2 -2
index 8f300498e4..71342863d6 100644
@@ -59,33 +59,29 @@
  */
 static int igd_gen(VFIOPCIDevice *vdev)
 {
-    if ((vdev->device_id & 0xfff) == 0xa84) {
-        return 8; /* Broxton */
+    if ((vdev->device_id & 0xffe) == 0xa84) {
+        return 9;   /* Broxton/Apollo Lake, Gemini Lake */
     }

J4105 is a Gemini Lake, the IGD device ID is 0x3185 according to ark[1]??

I made a mistake, Gemini Lake have device id 0x3184 and 0x3185, it won't match here.

Quoted 7 lines

I do find Apollo Lake examples (N3450) with ID 0x5a85, which I assume justifies masking out the zero bit of the ID.

Since Broxton was apparently canceled, I think this unique device ID test was concocted from i915. Maybe Apollo Lake should just be added under gen9 below with 0x5a00? Then Gemini Lake should be added as 0x3100. Both should be noted as new IDs. Have either been tested?

Broxton is the name for the GPU part in Apollo Lake
https://www.intel.com/content/www/us/en/content-details/685496/2016-intel-atom-processors-celeron-processors-and-pentium-processors-based-on-the-apollo-lake-platform-broxton-graphics-programmer-s-reference-manual-preface.html

Apollo lake have 5 ids, 0x0a84, 0x1a84, 0x1a85, 0x5a84, 0x5a85, the problem is 0x0a84, the prefix 0x0a00 is the same as Haswell, and their generation are different, the GGC register need to be emulated qemu are also different. Masking 0xffe here resolves it.

Though I don't have a Gemini Lake device in hand, some proxmox users reported passthrough works. Giving that in old qemu releases, they were treated as gen 8 by default, and there is no difference in qemu for gen 8 and gen 9 device, I think it's okay to declare it as gen 9 here?

https://forum.proxmox.com/threads/asrock-j4105-gpu-passthrough-vgabios-romfile.67507/

Quoted 21 lines
Quoted 10 lines
hw/vfio/igd.c +1 -7
     switch (vdev->device_id & 0xff00) {
-    /* SandyBridge, IvyBridge, ValleyView, Haswell */
-    case 0x0100:
-    case 0x0400:
-    case 0x0a00:
-    case 0x0c00:
-    case 0x0d00:
-    case 0x0f00:
+    case 0x0100:    /* SandyBridge, IvyBridge */

hw/vfio/igd.c line 72

Quoted 10 lines
hw/vfio/igd.c +4 -5
         return 6;
-    /* BroadWell, CherryView, SkyLake, KabyLake */
-    case 0x1600:
-    case 0x1900:
-    case 0x2200:
-    case 0x5900:
+    case 0x0400:    /* Haswell */
+    case 0x0a00:    /* Haswell */
+    case 0x0c00:    /* Haswell */
+    case 0x0d00:    /* Haswell */
Quoted 1 line

Crystal Well?

All 4 are Haswell device id prefixes.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/drm/intel/i915_pciids.h?h=v6.12#n187

Quoted 7 lines

hw/vfio/igd.c line 76

Quoted 4 lines
hw/vfio/igd.c +4 -0
+    case 0x0f00:    /* Valleyview/Bay Trail */
+        return 7;
+    case 0x1600:    /* Broadwell */
+    case 0x2200:    /* Cherryview */

These two jump from gen6/7 to gen8, that's certainly not just a cosmetic change.

Broadwell and Cherryview are gen 8 previously, no change here.
https://gitlab.com/qemu-project/qemu/-/blob/v9.2.0-rc2/hw/vfio/igd.c#L86

Quoted 22 lines

hw/vfio/igd.c line 79

Quoted 5 lines
hw/vfio/igd.c +2 -2
         return 8;
-    /* CoffeeLake */
-    case 0x3e00:
+    case 0x1900:    /* Skylake */
+    case 0x5900:    /* Kaby Lake */

These two jump from gen6/7 to gen9, so again, functional differences for these. Have these changes been tested?

hw/vfio/igd.c line 87

Quoted 12 lines
hw/vfio/igd.c +3 -4
+    case 0x3e00:    /* Coffee Lake */
         return 9;
-    /* ElkhartLake */
-    case 0x4500:
+    case 0x4500:    /* Elkhart Lake */
         return 11;
-    /* TigerLake */
-    case 0x9A00:
+    case 0x9A00:    /* Tiger Lake */
         return 12;
     }
 
Quoted 7 lines

Actually, patch 1 doesn't even need to be addressed here because patch 2 already made the change to check (gen < 8) rather than (gen > 6), so we can just entirely drop patch 1 (ideally with an updated commit log in 2 to note the logic change for this upcoming split of gen6/7). Thanks,

Alex

Agreed, previous (gen > 6) actually means (gen >= 8), patch 1 is unnecessary. I am going to change the condition in this commit, and mention it in commit message.

Quoted 2 lines

[1]https://www.intel.com/content/www/us/en/products/sku/128989/intel-celeron-j4105-processor-4m-cache-up-to-2-50-ghz/specifications.html

#18 lore raw

PATCH 5/8 vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper Lake device ids

In reply to Tomita Moeko

All gen 11 and 12 igd devices have 64 bit BDSM register at 0xC0 in its config space, add them to the list to support igd passthrough on Alder/ Raptor/Rocket/Ice/Jasper Lake platforms.

Tested legacy mode of igd passthrough works properly on both linux and windows guests with AlderLake-S GT1 (8086:4680).

---
 hw/vfio/igd.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/vfio/igd.c +5 -0
index 71342863d6..a86faf2fa9 100644
@@ -79,9 +79,14 @@ static int igd_gen(VFIOPCIDevice *vdev)
     case 0x5900:    /* Kaby Lake */
     case 0x3e00:    /* Coffee Lake */
         return 9;
+    case 0x8A00:    /* Ice Lake */
     case 0x4500:    /* Elkhart Lake */
+    case 0x4E00:    /* Jasper Lake */
         return 11;
     case 0x9A00:    /* Tiger Lake */
+    case 0x4C00:    /* Rocket Lake */
+    case 0x4600:    /* Alder Lake */
+    case 0xA700:    /* Raptor Lake */
         return 12;
     }
 
Signature · 1 line
2.45.2
Reviewed-by on v1 5/8
#19 lore raw

Re: [PATCH 5/8] vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper Lake device ids

Quoted 25 lines · Tomita Moeko

CAUTION: External Email!!
All gen 11 and 12 igd devices have 64 bit BDSM register at 0xC0 in its config space, add them to the list to support igd passthrough on Alder/
Raptor/Rocket/Ice/Jasper Lake platforms.

Tested legacy mode of igd passthrough works properly on both linux and windows guests with AlderLake-S GT1 (8086:4680).

---
 hw/vfio/igd.c | 5 +++++

 1 file changed, 5 insertions(+)
hw/vfio/igd.c +0 -0
index 71342863d6..a86faf2fa9 100644
@@ -79,9 +79,14 @@ static int igd_gen(VFIOPCIDevice *vdev)

     case 0x5900:    /* Kaby Lake */
     case 0x3e00:    /* Coffee Lake */
         return 9;
+    case 0x8A00:    /* Ice Lake */
     case 0x4500:    /* Elkhart Lake */
+    case 0x4E00:    /* Jasper Lake */

Quoted 8 lines

         return 11;
     case 0x9A00:    /* Tiger Lake */
+    case 0x4C00:    /* Rocket Lake */
+    case 0x4600:    /* Alder Lake */
+    case 0xA700:    /* Raptor Lake */
         return 12;
     }

disable-disclaimer-BADE

#20 lore raw

PATCH 6/8 vfio/igd: emulate GGC register in mmio bar0

In reply to Tomita Moeko

The GGC register at 0x50 of pci config space is a mirror of the same register at 0x108040 of mmio bar0 [1]. i915 driver also reads that register from mmio bar0 instead of config space. As GGC is programmed and emulated by qemu, the mmio address should also be emulated, in the same way of BDSM register.

A macro is defined to simplify the declaration of MemoryRegionOps for a register mirrored to pcie config space.

[1] 4.1.28, 12th Generation Intel Core Processors Datasheet Volume 2

    https://www.intel.com/content/www/us/en/content-details/655259
---
 hw/vfio/igd.c | 67 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 25 deletions(-)
hw/vfio/igd.c +42 -25
index a86faf2fa9..07700dce30 100644
@@ -415,16 +415,9 @@ static const MemoryRegionOps vfio_igd_index_quirk = {
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
-#define IGD_BDSM_MMIO_OFFSET 0x1080C0
-
-static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
-                                          hwaddr addr, unsigned size)
+static uint64_t vfio_igd_pci_config_read(VFIOPCIDevice *vdev, uint64_t offset,
+                                         unsigned size)
 {
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;
-
-    offset = IGD_BDSM_GEN11 + addr;
-
     switch (size) {
     case 1:
         return pci_get_byte(vdev->pdev.config + offset);
@@ -442,14 +435,12 @@ static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
     return 0;
 }
 
-static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
-                                       uint64_t data, unsigned size)
-{
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;
-
-    offset = IGD_BDSM_GEN11 + addr;
+#define IGD_GGC_MMIO_OFFSET     0x108040
+#define IGD_BDSM_MMIO_OFFSET    0x1080C0
 
+static void vfio_igd_pci_config_write(VFIOPCIDevice *vdev, uint64_t offset,
+                                      uint64_t data, unsigned size)
+{
     switch (size) {
     case 1:
         pci_set_byte(vdev->pdev.config + offset, data);
@@ -464,17 +455,35 @@ static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
         pci_set_quad(vdev->pdev.config + offset, data);
         break;
     default:
-        hw_error("igd: unsupported read size, %u bytes", size);
+        hw_error("igd: unsupported write size, %u bytes", size);
         break;
     }
 }
 
-static const MemoryRegionOps vfio_igd_bdsm_quirk = {
-    .read = vfio_igd_quirk_bdsm_read,
-    .write = vfio_igd_quirk_bdsm_write,
-    .endianness = DEVICE_LITTLE_ENDIAN,
+#define VFIO_IGD_QUIRK_MIRROR_REG(reg, name)                            \
+static uint64_t vfio_igd_quirk_read_##name(void *opaque,                \
+                                           hwaddr addr, unsigned size)  \
+{                                                                       \
+    VFIOPCIDevice *vdev = opaque;                                       \
+    return vfio_igd_pci_config_read(vdev, reg + addr, size);            \
+}                                                                       \
+                                                                        \
+static void vfio_igd_quirk_write_##name(void *opaque, hwaddr addr,      \
+                                        uint64_t data, unsigned size)   \
+{                                                                       \
+    VFIOPCIDevice *vdev = opaque;                                       \
+    vfio_igd_pci_config_write(vdev, reg + addr, data, size);            \
+}                                                                       \
+                                                                        \
+static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
+    .read = vfio_igd_quirk_read_##name,                                 \
+    .write = vfio_igd_quirk_write_##name,                               \
+    .endianness = DEVICE_LITTLE_ENDIAN,                                 \
 };
 
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
+
 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
 {
     VFIOQuirk *quirk;
@@ -501,13 +510,21 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
         return;
     }
 
-    quirk = vfio_quirk_alloc(1);
+    quirk = vfio_quirk_alloc(2);
     quirk->data = vdev;
 
-    memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_bdsm_quirk,
-                          vdev, "vfio-igd-bdsm-quirk", 8);
+    memory_region_init_io(&quirk->mem[0], OBJECT(vdev),
+                          &vfio_igd_quirk_mirror_ggc, vdev,
+                          "vfio-igd-ggc-quirk", 2);
+    memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
+                                        IGD_GGC_MMIO_OFFSET, &quirk->mem[0],
+                                        1);
+
+    memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                          &vfio_igd_quirk_mirror_bdsm, vdev,
+                          "vfio-igd-bdsm-quirk", 8);
     memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
-                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[0],
+                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[1],
                                         1);
 
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
Signature · 1 line
2.45.2
#21 lore raw

Re: [PATCH 6/8] vfio/igd: emulate GGC register in mmio bar0

Quoted 54 lines · Tomita Moeko

CAUTION: External Email!!
The GGC register at 0x50 of pci config space is a mirror of the same register at 0x108040 of mmio bar0 [1]. i915 driver also reads that register from mmio bar0 instead of config space. As GGC is programmed and emulated by qemu, the mmio address should also be emulated, in the same way of BDSM register.

A macro is defined to simplify the declaration of MemoryRegionOps for a register mirrored to pcie config space.

[1] 4.1.28, 12th Generation Intel Core Processors Datasheet Volume 2
    https://www.intel.com/content/www/us/en/content-details/655259

---
 hw/vfio/igd.c | 67 ++++++++++++++++++++++++++++++++-------------------

 1 file changed, 42 insertions(+), 25 deletions(-)
hw/vfio/igd.c +0 -0
index a86faf2fa9..07700dce30 100644
@@ -415,16 +415,9 @@ static const MemoryRegionOps vfio_igd_index_quirk = {
      .endianness = DEVICE_LITTLE_ENDIAN,
  };

hw/vfio/igd.c +2 -4
-#define IGD_BDSM_MMIO_OFFSET 0x1080C0
-
-static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
-                                          hwaddr addr, unsigned size)
+static uint64_t vfio_igd_pci_config_read(VFIOPCIDevice *vdev, uint64_t offset,
+                                         unsigned size)

 {
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;

hw/vfio/igd.c +0 -3
-
-    offset = IGD_BDSM_GEN11 + addr;
-

     switch (size) {
     case 1:
         return pci_get_byte(vdev->pdev.config + offset);

hw/vfio/igd.c +0 -0
@@ -442,14 +435,12 @@ static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
      return 0;
  }

hw/vfio/igd.c +2 -7
-static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
-                                       uint64_t data, unsigned size)
-{
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;
-
-    offset = IGD_BDSM_GEN11 + addr;
+#define IGD_GGC_MMIO_OFFSET     0x108040
+#define IGD_BDSM_MMIO_OFFSET    0x1080C0
Quoted 1 line

While already moving this down, I would move it even more down in front of vfio_probe_igd_bar0_quirk to the place where it's actually used.

Quoted 9 lines
hw/vfio/igd.c +3 -0
+static void vfio_igd_pci_config_write(VFIOPCIDevice *vdev, uint64_t offset,
+                                      uint64_t data, unsigned size)
+{

     switch (size) {
     case 1:
         pci_set_byte(vdev->pdev.config + offset, data);

hw/vfio/igd.c +0 -0
@@ -464,17 +455,35 @@ static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
          pci_set_quad(vdev->pdev.config + offset, data);
          break;
Quoted 3 lines

     default:
-        hw_error("igd: unsupported read size, %u bytes", size); +        hw_error("igd: unsupported write size, %u bytes", size);

It would make sense to log the faulting address too.

Quoted 53 lines

         break;
     }
 }

hw/vfio/igd.c +19 -4
-static const MemoryRegionOps vfio_igd_bdsm_quirk = {
-    .read = vfio_igd_quirk_bdsm_read,
-    .write = vfio_igd_quirk_bdsm_write,
-    .endianness = DEVICE_LITTLE_ENDIAN,
+#define VFIO_IGD_QUIRK_MIRROR_REG(reg, name)                            \
+static uint64_t vfio_igd_quirk_read_##name(void *opaque,                \
+                                           hwaddr addr, unsigned size)  \
+{                                                                       \
+    VFIOPCIDevice *vdev = opaque;                                       \
+    return vfio_igd_pci_config_read(vdev, reg + addr, size);            \
+}                                                                       \
+                                                                        \
+static void vfio_igd_quirk_write_##name(void *opaque, hwaddr addr,      \
+                                        uint64_t data, unsigned size)   \
+{                                                                       \
+    VFIOPCIDevice *vdev = opaque;                                       \
+    vfio_igd_pci_config_write(vdev, reg + addr, data, size);            \
+}                                                                       \
+                                                                        \
+static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
+    .read = vfio_igd_quirk_read_##name,                                 \
+    .write = vfio_igd_quirk_write_##name,                               \
+    .endianness = DEVICE_LITTLE_ENDIAN,                                 \

 };

hw/vfio/igd.c +3 -0
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
+

 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr) {
     VFIOQuirk *quirk;

hw/vfio/igd.c +0 -0
@@ -501,13 +510,21 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
          return;
      }


-    quirk = vfio_quirk_alloc(1);
+    quirk = vfio_quirk_alloc(2);
     quirk->data = vdev;

-    memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_bdsm_quirk, -                          vdev, "vfio-igd-bdsm-quirk", 8);
+    memory_region_init_io(&quirk->mem[0], OBJECT(vdev),
+                          &vfio_igd_quirk_mirror_ggc, vdev,
+                          "vfio-igd-ggc-quirk", 2);
+    memory_region_add_subregion_overlap(vdev->bars[0].region.mem, +                                        IGD_GGC_MMIO_OFFSET, &quirk->mem[0], +                                        1);
+
+    memory_region_init_io(&quirk->mem[1], OBJECT(vdev),

Quoted 8 lines

+                          &vfio_igd_quirk_mirror_bdsm, vdev,
+                          "vfio-igd-bdsm-quirk", 8); memory_region_add_subregion_overlap(vdev->bars[0].region.mem, -                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[0], +                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[1], 1);

     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);

I slightly prefer splitting this patch into two. The first one adding a new macro for mirroring register and the second one adding the GGC mirror. However, that's just my personal preference.

Signature · 3 lines
Kind regards,
Corvin
disable-disclaimer-BADE
#22 lore raw

Re: [PATCH 6/8] vfio/igd: emulate GGC register in mmio bar0

Quoted 51 lines · Corvin Köhne
Quoted 50 lines · Tomita Moeko

CAUTION: External Email!!
The GGC register at 0x50 of pci config space is a mirror of the same register at 0x108040 of mmio bar0 [1]. i915 driver also reads that register from mmio bar0 instead of config space. As GGC is programmed and emulated by qemu, the mmio address should also be emulated, in the same way of BDSM register.

A macro is defined to simplify the declaration of MemoryRegionOps for a register mirrored to pcie config space.

[1] 4.1.28, 12th Generation Intel Core Processors Datasheet Volume 2
    https://www.intel.com/content/www/us/en/content-details/655259

---
 hw/vfio/igd.c | 67 ++++++++++++++++++++++++++++++++-------------------

 1 file changed, 42 insertions(+), 25 deletions(-)
hw/vfio/igd.c +0 -0
index a86faf2fa9..07700dce30 100644
@@ -415,16 +415,9 @@ static const MemoryRegionOps vfio_igd_index_quirk = {
      .endianness = DEVICE_LITTLE_ENDIAN,
  };

hw/vfio/igd.c +2 -4
-#define IGD_BDSM_MMIO_OFFSET 0x1080C0
-
-static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
-                                          hwaddr addr, unsigned size)
+static uint64_t vfio_igd_pci_config_read(VFIOPCIDevice *vdev, uint64_t offset,
+                                         unsigned size)

 {
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;

hw/vfio/igd.c +0 -3
-
-    offset = IGD_BDSM_GEN11 + addr;
-

     switch (size) {
     case 1:
         return pci_get_byte(vdev->pdev.config + offset);

hw/vfio/igd.c +0 -0
@@ -442,14 +435,12 @@ static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
      return 0;
  }

hw/vfio/igd.c +0 -5
-static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
-                                       uint64_t data, unsigned size)
-{
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;
Quoted 8 lines
Quoted 5 lines
hw/vfio/igd.c +2 -2
-
-    offset = IGD_BDSM_GEN11 + addr;
+#define IGD_GGC_MMIO_OFFSET     0x108040
+#define IGD_BDSM_MMIO_OFFSET    0x1080C0

While already moving this down, I would move it even more down in front of vfio_probe_igd_bar0_quirk to the place where it's actually used.

Will move it right above vfio_probe_igd_bar0_quirk()

Quoted 6 lines
Quoted 6 lines
hw/vfio/igd.c +3 -0
+static void vfio_igd_pci_config_write(VFIOPCIDevice *vdev, uint64_t offset,
+                                      uint64_t data, unsigned size)
+{

     switch (size) {
     case 1:
         pci_set_byte(vdev->pdev.config + offset, data);

Quoted 8 lines
Quoted 6 lines
hw/vfio/igd.c +0 -0
@@ -464,17 +455,35 @@ static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
          pci_set_quad(vdev->pdev.config + offset, data);
          break;

     default:
-        hw_error("igd: unsupported read size, %u bytes", size); +        hw_error("igd: unsupported write size, %u bytes", size);

It would make sense to log the faulting address too.

Good catch

Quoted 56 lines
Quoted 56 lines

         break;
     }
 }

hw/vfio/igd.c +19 -4
-static const MemoryRegionOps vfio_igd_bdsm_quirk = {
-    .read = vfio_igd_quirk_bdsm_read,
-    .write = vfio_igd_quirk_bdsm_write,
-    .endianness = DEVICE_LITTLE_ENDIAN,
+#define VFIO_IGD_QUIRK_MIRROR_REG(reg, name)                            \
+static uint64_t vfio_igd_quirk_read_##name(void *opaque,                \
+                                           hwaddr addr, unsigned size)  \
+{                                                                       \
+    VFIOPCIDevice *vdev = opaque;                                       \
+    return vfio_igd_pci_config_read(vdev, reg + addr, size);            \
+}                                                                       \
+                                                                        \
+static void vfio_igd_quirk_write_##name(void *opaque, hwaddr addr,      \
+                                        uint64_t data, unsigned size)   \
+{                                                                       \
+    VFIOPCIDevice *vdev = opaque;                                       \
+    vfio_igd_pci_config_write(vdev, reg + addr, data, size);            \
+}                                                                       \
+                                                                        \
+static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
+    .read = vfio_igd_quirk_read_##name,                                 \
+    .write = vfio_igd_quirk_write_##name,                               \
+    .endianness = DEVICE_LITTLE_ENDIAN,                                 \

 };

hw/vfio/igd.c +3 -0
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
+

 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr) {
     VFIOQuirk *quirk;

hw/vfio/igd.c +0 -0
@@ -501,13 +510,21 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
          return;
      }


-    quirk = vfio_quirk_alloc(1);
+    quirk = vfio_quirk_alloc(2);
     quirk->data = vdev;

-    memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_bdsm_quirk, -                          vdev, "vfio-igd-bdsm-quirk", 8);
+    memory_region_init_io(&quirk->mem[0], OBJECT(vdev),
+                          &vfio_igd_quirk_mirror_ggc, vdev,
+                          "vfio-igd-ggc-quirk", 2);
+    memory_region_add_subregion_overlap(vdev->bars[0].region.mem, +                                        IGD_GGC_MMIO_OFFSET, &quirk->mem[0], +                                        1);
+
+    memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                          &vfio_igd_quirk_mirror_bdsm, vdev,
+                          "vfio-igd-bdsm-quirk", 8); memory_region_add_subregion_overlap(vdev->bars[0].region.mem,

Quoted 9 lines
Quoted 5 lines

-                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[0], +                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[1], 1);

     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);

I slightly prefer splitting this patch into two. The first one adding a new macro for mirroring register and the second one adding the GGC mirror. However, that's just my personal preference.

Will split them in v2.

#23 lore raw

PATCH 7/8 vfio/igd: emulate BDSM in mmio bar0 for gen 6-10 devices

In reply to Tomita Moeko

A recent commit in i915 driver [1] claims the BDSM register at 0x1080c0 of mmio bar0 has been there since gen 6. Mirror this register to the 32 bit BDSM register at 0x5c in pci config space for gen6-10 devices.

[1] https://patchwork.freedesktop.org/patch/msgid/[email protected]

---
 hw/vfio/igd.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
hw/vfio/igd.c +18 -8
index 07700dce30..3fd822241d 100644
@@ -482,7 +482,8 @@ static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
 };
 
 VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)
-VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM, bdsm)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm64)
 
 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
 {
@@ -506,7 +507,7 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
      * into MMIO space and read from MMIO space by the Windows driver.
      */
     gen = igd_gen(vdev);
-    if (gen < 11) {
+    if (gen < 6) {
         return;
     }
 
@@ -520,12 +521,21 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
                                         IGD_GGC_MMIO_OFFSET, &quirk->mem[0],
                                         1);
 
-    memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
-                          &vfio_igd_quirk_mirror_bdsm, vdev,
-                          "vfio-igd-bdsm-quirk", 8);
-    memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
-                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[1],
-                                        1);
+    if (gen < 11) {
+        memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                              &vfio_igd_quirk_mirror_bdsm, vdev,
+                              "vfio-igd-bdsm-quirk", 4);
+        memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
+                                            IGD_BDSM_MMIO_OFFSET,
+                                            &quirk->mem[1], 1);
+    } else {
+        memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                              &vfio_igd_quirk_mirror_bdsm64, vdev,
+                              "vfio-igd-bdsm-quirk", 8);
+        memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
+                                            IGD_BDSM_MMIO_OFFSET,
+                                            &quirk->mem[1], 1);
+    }
 
     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 }
Signature · 1 line
2.45.2
#24 lore raw

Re: [PATCH 7/8] vfio/igd: emulate BDSM in mmio bar0 for gen 6-10 devices

Quoted 29 lines · Tomita Moeko

CAUTION: External Email!!
A recent commit in i915 driver [1] claims the BDSM register at 0x1080c0 of mmio bar0 has been there since gen 6. Mirror this register to the 32 bit BDSM register at 0x5c in pci config space for gen6-10 devices.

[1]
https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAAi96BdMcJV7rkAAABy4L3Y-n1H4M5Erk3toZdMOmhFmc4HUpjw_YG-G4-GcwmVdS8wC_pUyKH6p7x9_O-6Hprvyv9mbbWh_j2w5E6zLArS3crC1154aEYTRFcrIBLPMIoEAv9iTERXE2XB0E260O3pkmmo1UJOMUhb_fI5OUT3-68jnkq4QwTNgbnwmbVRnL2AyAM1pjuZqoqP6GP9y5e6OgocW-BJp0QtHO3eP9NbqTufifAAMfKlU7ekAZLWfPzejtqwaw2

---
 hw/vfio/igd.c | 26 ++++++++++++++++++--------

 1 file changed, 18 insertions(+), 8 deletions(-)
hw/vfio/igd.c +0 -0
index 07700dce30..3fd822241d 100644
@@ -482,7 +482,8 @@ static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
  };


 VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)

hw/vfio/igd.c +2 -1
-VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM, bdsm)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm64)


 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr) {

hw/vfio/igd.c void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr) · line 510

Quoted 5 lines
hw/vfio/igd.c +1 -1
@@ -506,7 +507,7 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
       * into MMIO space and read from MMIO space by the Windows driver.
       */
      gen = igd_gen(vdev);
-    if (gen < 11) {
+    if (gen < 6) {

This also causes the GGC register to be mirrored for gen 6 and later. Is this intended? If so, you should mention it in the commit message as well.

Quoted 31 lines
hw/vfio/igd.c +0 -0
          return;
      }

hw/vfio/igd.c +0 -0
@@ -520,12 +521,21 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
                                          IGD_GGC_MMIO_OFFSET, &quirk->mem[0],
                                          1);


-    memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
-                          &vfio_igd_quirk_mirror_bdsm, vdev,
-                          "vfio-igd-bdsm-quirk", 8);
-    memory_region_add_subregion_overlap(vdev->bars[0].region.mem, -                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[1], -                                        1);
+    if (gen < 11) {
+        memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                              &vfio_igd_quirk_mirror_bdsm, vdev, +                              "vfio-igd-bdsm-quirk", 4);
+        memory_region_add_subregion_overlap(vdev->bars[0].region.mem, +                                            IGD_BDSM_MMIO_OFFSET, +                                            &quirk->mem[1], 1); +    } else {
+        memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                              &vfio_igd_quirk_mirror_bdsm64, vdev, +                              "vfio-igd-bdsm-quirk", 8);
+        memory_region_add_subregion_overlap(vdev->bars[0].region.mem, +                                            IGD_BDSM_MMIO_OFFSET, +                                            &quirk->mem[1], 1); +    }

     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 }

Signature · 3 lines
Kind regards,
Corvin
disable-disclaimer-BADE
#25 lore raw

Re: [PATCH 7/8] vfio/igd: emulate BDSM in mmio bar0 for gen 6-10 devices

Quoted 30 lines · Corvin Köhne
Quoted 29 lines · Tomita Moeko

CAUTION: External Email!!
A recent commit in i915 driver [1] claims the BDSM register at 0x1080c0 of mmio bar0 has been there since gen 6. Mirror this register to the 32 bit BDSM register at 0x5c in pci config space for gen6-10 devices.

[1]
https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAAi96BdMcJV7rkAAABy4L3Y-n1H4M5Erk3toZdMOmhFmc4HUpjw_YG-G4-GcwmVdS8wC_pUyKH6p7x9_O-6Hprvyv9mbbWh_j2w5E6zLArS3crC1154aEYTRFcrIBLPMIoEAv9iTERXE2XB0E260O3pkmmo1UJOMUhb_fI5OUT3-68jnkq4QwTNgbnwmbVRnL2AyAM1pjuZqoqP6GP9y5e6OgocW-BJp0QtHO3eP9NbqTufifAAMfKlU7ekAZLWfPzejtqwaw2

---
 hw/vfio/igd.c | 26 ++++++++++++++++++--------

 1 file changed, 18 insertions(+), 8 deletions(-)
hw/vfio/igd.c +0 -0
index 07700dce30..3fd822241d 100644
@@ -482,7 +482,8 @@ static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
  };


 VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)

hw/vfio/igd.c +2 -1
-VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM, bdsm)
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm64)


 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr) {

hw/vfio/igd.c +0 -0
@@ -506,7 +507,7 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
Quoted 8 lines

hw/vfio/igd.c void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr) · line 510

Quoted 5 lines
hw/vfio/igd.c +1 -1
       * into MMIO space and read from MMIO space by the Windows driver.
       */
      gen = igd_gen(vdev);
-    if (gen < 11) {
+    if (gen < 6) {

This also causes the GGC register to be mirrored for gen 6 and later. Is this intended? If so, you should mention it in the commit message as well.

Yes, I linked the i915 patch in commit message.

Quoted 32 lines
Quoted 31 lines
hw/vfio/igd.c +0 -0
          return;
      }

hw/vfio/igd.c +0 -0
@@ -520,12 +521,21 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
                                          IGD_GGC_MMIO_OFFSET, &quirk->mem[0],
                                          1);


-    memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
-                          &vfio_igd_quirk_mirror_bdsm, vdev,
-                          "vfio-igd-bdsm-quirk", 8);
-    memory_region_add_subregion_overlap(vdev->bars[0].region.mem, -                                        IGD_BDSM_MMIO_OFFSET, &quirk->mem[1], -                                        1);
+    if (gen < 11) {
+        memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                              &vfio_igd_quirk_mirror_bdsm, vdev, +                              "vfio-igd-bdsm-quirk", 4);
+        memory_region_add_subregion_overlap(vdev->bars[0].region.mem, +                                            IGD_BDSM_MMIO_OFFSET, +                                            &quirk->mem[1], 1); +    } else {
+        memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
+                              &vfio_igd_quirk_mirror_bdsm64, vdev, +                              "vfio-igd-bdsm-quirk", 8);
+        memory_region_add_subregion_overlap(vdev->bars[0].region.mem, +                                            IGD_BDSM_MMIO_OFFSET, +                                            &quirk->mem[1], 1); +    }

     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
 }

#26 lore raw

PATCH 8/8 vfio/igd: add x-igd-gms option back to set DSM region size for guest

In reply to Tomita Moeko

DSM region is likely to store framebuffer in Windows, a small DSM region may cause display issues (e.g. half of the screen is black). By default, QEMU uses host's original value, which is determined by DVMT Pre-Allocated option in Intel FSP of host bios. Some vendors do not expose this config item to users. In such cases, x-igd-gms option can be used to manually set the data stolen memory size for guest.

When DVMT Pre-Allocated option is available in host BIOS, user should set DSM region size there instead of using x-igd-gms option.

---
 hw/vfio/igd.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
hw/vfio/igd.c +17 -0
index 3fd822241d..614223123b 100644
@@ -712,6 +712,23 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     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
+     * set from DVMT Pre-Allocated option in host BIOS.
+     */
+    if (vdev->igd_gms) {
+        if (gen < 8 && vdev->igd_gms <= 0x10) {
+            gmch &= ~(IGD_GMCH_GEN6_GMS_MASK << IGD_GMCH_GEN6_GMS_SHIFT);
+            gmch |= vdev->igd_gms << IGD_GMCH_GEN6_GMS_SHIFT;
+        } else if (vdev->igd_gms <= 0x40) {
+            gmch &= ~(IGD_GMCH_GEN8_GMS_MASK << IGD_GMCH_GEN8_GMS_SHIFT);
+            gmch |= vdev->igd_gms << IGD_GMCH_GEN8_GMS_SHIFT;
+        } else {
+            error_report("Unsupported IGD GMS value 0x%x", vdev->igd_gms);
+        }
+    }
+
     ggms_size = igd_gtt_memory_size(gen, gmch);
     gms_size = igd_stolen_memory_size(gen, gmch);
 
Signature · 1 line
2.45.2
#27 lore raw

Re: [PATCH 8/8] vfio/igd: add x-igd-gms option back to set DSM region size for guest

Quoted 37 lines · Tomita Moeko

CAUTION: External Email!!
DSM region is likely to store framebuffer in Windows, a small DSM region may cause display issues (e.g. half of the screen is black). By default, QEMU uses host's original value, which is determined by DVMT Pre-Allocated option in Intel FSP of host bios. Some vendors do not expose this config item to users. In such cases, x-igd-gms option can be used to manually set the data stolen memory size for guest.

When DVMT Pre-Allocated option is available in host BIOS, user should set DSM region size there instead of using x-igd-gms option.

---
 hw/vfio/igd.c | 17 +++++++++++++++++

 1 file changed, 17 insertions(+)
hw/vfio/igd.c +0 -0
index 3fd822241d..614223123b 100644
@@ -712,6 +712,23 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)


     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 +     * set from DVMT Pre-Allocated option in host BIOS.
+     */
+    if (vdev->igd_gms) {
+        if (gen < 8 && vdev->igd_gms <= 0x10) {
+            gmch &= ~(IGD_GMCH_GEN6_GMS_MASK << IGD_GMCH_GEN6_GMS_SHIFT); +            gmch |= vdev->igd_gms << IGD_GMCH_GEN6_GMS_SHIFT;
+        } else if (vdev->igd_gms <= 0x40) {
+            gmch &= ~(IGD_GMCH_GEN8_GMS_MASK << IGD_GMCH_GEN8_GMS_SHIFT); +            gmch |= vdev->igd_gms << IGD_GMCH_GEN8_GMS_SHIFT;

Quoted 8 lines

+        } else {
+            error_report("Unsupported IGD GMS value 0x%x", vdev->igd_gms); +        }
+    }
+
     ggms_size = igd_gtt_memory_size(gen, gmch);
     gms_size = igd_stolen_memory_size(gen, gmch);

As mentioned in my commit removing the x-igd-gms option, I've seen issues on Windows guest when setting a wrong gms value. I can try to recheck this.

Signature · 3 lines
Kind regards,
Corvin
disable-disclaimer-BADE
#28 lore raw

Re: [PATCH 8/8] vfio/igd: add x-igd-gms option back to set DSM region size for guest

Quoted 41 lines · Corvin Köhne
Quoted 40 lines · Tomita Moeko

CAUTION: External Email!!
DSM region is likely to store framebuffer in Windows, a small DSM region may cause display issues (e.g. half of the screen is black). By default, QEMU uses host's original value, which is determined by DVMT Pre-Allocated option in Intel FSP of host bios. Some vendors do not expose this config item to users. In such cases, x-igd-gms option can be used to manually set the data stolen memory size for guest.

When DVMT Pre-Allocated option is available in host BIOS, user should set DSM region size there instead of using x-igd-gms option.

---
 hw/vfio/igd.c | 17 +++++++++++++++++

 1 file changed, 17 insertions(+)
hw/vfio/igd.c +0 -0
index 3fd822241d..614223123b 100644
@@ -712,6 +712,23 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)


     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 +     * set from DVMT Pre-Allocated option in host BIOS.
+     */
+    if (vdev->igd_gms) {
+        if (gen < 8 && vdev->igd_gms <= 0x10) {
+            gmch &= ~(IGD_GMCH_GEN6_GMS_MASK << IGD_GMCH_GEN6_GMS_SHIFT); +            gmch |= vdev->igd_gms << IGD_GMCH_GEN6_GMS_SHIFT;
+        } else if (vdev->igd_gms <= 0x40) {
+            gmch &= ~(IGD_GMCH_GEN8_GMS_MASK << IGD_GMCH_GEN8_GMS_SHIFT); +            gmch |= vdev->igd_gms << IGD_GMCH_GEN8_GMS_SHIFT;
+        } else {
+            error_report("Unsupported IGD GMS value 0x%x", vdev->igd_gms); +        }

Quoted 9 lines
Quoted 5 lines

+    }
+
     ggms_size = igd_gtt_memory_size(gen, gmch); gms_size = igd_stolen_memory_size(gen, gmch);

As mentioned in my commit removing the x-igd-gms option, I've seen issues on Windows guest when setting a wrong gms value. I can try to recheck this.

What's the windows GPU driver version you're using? I am using latest 32.0.101.6299. My DVMT Pre-allocated is 64M in BIOS. When using default value, with a 4K display, strange behaviors were discovered during my tests, like the lower half (actuall about 1/3) of the screen is black, 3D mark only runs at 2560*1440 and the refresh rate was limited to 30Hz. Restarting guest won't fix this, but when I forcefully set the GMS size in emulated GGC to 128M, these issue disappears. That's why I suspect DSM may contain framebuffer.

Per my result, it seems windows guest is using the DSM as something like dedicated VRAM, and GPU itself can handle guest physical address internally.

The OVMF firmware I use is
https://github.com/tomitamoeko/edk2/commits/igd-pt-adl/
with GOP driver extracted from bios image from motherboard vendor.

I'm also wondering why the BAR4 quirks were implemented before, GTT are not located in DSM, but in GGMS size below BDSM. This region is inaccessible directly, reading it via /dev/mem immediately hangs the system. The only way to access GTT is via MMIO BAR0.