Skip to contentSkip to conversation

vfio/igd: Enable legacy mode on more devices

PATCH Applied Tomita Moeko 3 participants 14 messages Dec 6, 2024

lore
Share & tools
lore
#0 lore raw

v4 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.

Changelog:
v4:
* Move "vfio/igd: fix GTT stolen memory size calculation for gen 8+" to the first.

v3:
* Added new patch to fix GTT stolen memory size calculation for gen 8+ devicess.
* Fixed quirk index in "vfio/igd: add macro for declaring mirrored registers"
* Fixed condition in "vfio/igd: add x-igd-gms option back to set DSM region size for guest"
* Updated comments and commit message.

v2:
* Droped "vfio/igd: fix GTT stolen memory size calculation for gen 7".
* Fixed conditions when calculating GGMS size.
* Added Gemini Lake and Comet Lake device ids.
* Splited mirroring register declaration macro into a new patch.
* Minor fixes.

Tomita Moeko (10):
vfio/igd: fix GTT stolen memory size calculation for gen 8+ vfio/igd: remove unsupported device ids
vfio/igd: align generation with i915 kernel driver vfio/igd: canonicalize memory size calculations
vfio/igd: add Gemini Lake and Comet Lake device ids vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper Lake device ids vfio/igd: add macro for declaring mirrored registers 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 | 262 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 165 insertions(+), 97 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v4 1/10 vfio/igd: fix GTT stolen memory size calculation for gen 8+

On gen 8 and later devices, the GTT stolen memory size when GGMS equals 0 is 0 (no preallocated memory) rather than 1MB [1].

[1] 3.1.13, 5th Generation Intel Core Processor Family Datasheet Vol. 2

    https://www.intel.com/content/www/us/en/content-details/330835
Fixes c4c45e943e51 ("vfio/pci: Intel graphics legacy mode assignment")
---
 hw/vfio/igd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/igd.c +2 -2
index 4047f4f071..73ed1ec8e6 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 > 6 && ggms != 0) {
         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 > 6 && ggms_mb != 0) {
         ggms_mb = 1 << ggms_mb;
     }
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH v4 2/10 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 73ed1ec8e6..059ed56439 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
#3 lore raw

PATCH v4 3/10 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.

The condition of how GTT stolen memory size is calculated is changed accordingly as GGMS is in multiple of 2 starting from gen 8.

---
 hw/vfio/igd.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)
hw/vfio/igd.c +23 -22
index 059ed56439..09bd4e5383 100644
@@ -59,33 +59,34 @@
  */
 static int igd_gen(VFIOPCIDevice *vdev)
 {
-    if ((vdev->device_id & 0xfff) == 0xa84) {
-        return 8; /* Broxton */
+    /*
+     * Device IDs for Broxton/Apollo Lake are 0x0a84, 0x1a84, 0x1a85, 0x5a84
+     * and 0x5a85, match bit 11:1 here
+     * Prefix 0x0a is taken by Haswell, this rule should be matched first.
+     */
+    if ((vdev->device_id & 0xffe) == 0xa84) {
+        return 9;
     }
 
     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;
     }
 
@@ -258,7 +259,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 && ggms != 0) {
+    if (gen >= 8 && ggms != 0) {
         ggms = 1 << ggms;
     }
 
@@ -668,7 +669,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 && ggms_mb != 0) {
+    if (gen >= 8 && ggms_mb != 0) {
         ggms_mb = 1 << ggms_mb;
     }
 
Signature · 1 line
2.45.2
#4 lore raw

PATCH v4 4/10 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 09bd4e5383..e231865d72 100644
@@ -107,6 +107,53 @@ 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       /* SNB_GMCH in i915 */
+#define IGD_GMCH_GEN6_GMS_MASK      0x1f
+#define IGD_GMCH_GEN6_GGMS_SHIFT    8
+#define IGD_GMCH_GEN6_GGMS_MASK     0x3
+#define IGD_GMCH_GEN8_GMS_SHIFT     8       /* BDW_GMCH in i915 */
+#define IGD_GMCH_GEN8_GMS_MASK      0xff
+#define IGD_GMCH_GEN8_GGMS_SHIFT    6
+#define IGD_GMCH_GEN8_GGMS_MASK     0x3
+
+static uint64_t igd_gtt_memory_size(int gen, uint16_t gmch)
+{
+    uint64_t ggms;
+
+    if (gen < 8) {
+        ggms = (gmch >> IGD_GMCH_GEN6_GGMS_SHIFT) & IGD_GMCH_GEN6_GGMS_MASK;
+    } else {
+        ggms = (gmch >> IGD_GMCH_GEN8_GGMS_SHIFT) & IGD_GMCH_GEN8_GGMS_MASK;
+        if (ggms != 0) {
+            ggms = 1 << ggms;
+        }
+    }
+
+    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) {
+            return gms * 32 * MiB;
+    } else {
+        if (gms < 0xf0) {
+            return gms * 32 * MiB;
+        } else {
+            return (gms - 0xf0 + 1) * 4 * MiB;
+        }
+    }
+
+    return 0;
+}
 
 /*
  * The rather short list of registers that we copy from the host devices.
@@ -255,17 +302,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 >= 8 && ggms != 0) {
-        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);
 }
 
 /*
@@ -472,30 +512,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;
@@ -505,7 +521,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;
@@ -667,13 +684,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 >= 8 && ggms_mb != 0) {
-        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
@@ -684,7 +696,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));
 
@@ -735,5 +747,6 @@ 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,
+                                    (ggms_size + gms_size) / MiB);
 }
Signature · 1 line
2.45.2
#5 lore raw

PATCH v4 5/10 vfio/igd: add Gemini Lake and Comet Lake device ids

In reply to Tomita Moeko

Both Gemini Lake and Comet Lake are gen 9 devices. Many user reports on internet shows legacy mode of igd passthrough works as qemu treats them as gen 8 devices by default before e433f208973f ("vfio/igd:
return an invalid generation for unknown devices").

---
 hw/vfio/igd.c | 2 ++
 1 file changed, 2 insertions(+)
hw/vfio/igd.c +2 -0
index e231865d72..ed236f443a 100644
@@ -81,8 +81,10 @@ static int igd_gen(VFIOPCIDevice *vdev)
     case 0x2200:    /* Cherryview */
         return 8;
     case 0x1900:    /* Skylake */
+    case 0x3100:    /* Gemini Lake */
     case 0x5900:    /* Kaby Lake */
     case 0x3e00:    /* Coffee Lake */
+    case 0x9B00:    /* Comet Lake */
         return 9;
     case 0x4500:    /* Elkhart Lake */
         return 11;
Signature · 1 line
2.45.2
#6 lore raw

PATCH v4 6/10 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 ed236f443a..49b6547776 100644
@@ -86,9 +86,14 @@ static int igd_gen(VFIOPCIDevice *vdev)
     case 0x3e00:    /* Coffee Lake */
     case 0x9B00:    /* Comet 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
#7 lore raw

PATCH v4 7/10 vfio/igd: add macro for declaring mirrored registers

In reply to Tomita Moeko

igd devices have multipe registers mirroring mmio address and pci config space, more than a single BDSM register. To support this, the read/write functions are made common and a macro is defined to simplify the declaration of MemoryRegionOps.

---
 hw/vfio/igd.c | 60 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 24 deletions(-)
hw/vfio/igd.c +36 -24
index 49b6547776..18d159aa8e 100644
@@ -421,16 +421,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);
@@ -441,21 +434,17 @@ static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
     case 8:
         return pci_get_quad(vdev->pdev.config + offset);
     default:
-        hw_error("igd: unsupported read size, %u bytes", size);
+        hw_error("igd: unsupported pci config read at %lx, size %u",
+                 offset, size);
         break;
     }
 
     return 0;
 }
 
-static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
-                                       uint64_t data, unsigned size)
+static void vfio_igd_pci_config_write(VFIOPCIDevice *vdev, uint64_t offset,
+                                      uint64_t data, unsigned size)
 {
-    VFIOPCIDevice *vdev = opaque;
-    uint64_t offset;
-
-    offset = IGD_BDSM_GEN11 + addr;
-
     switch (size) {
     case 1:
         pci_set_byte(vdev->pdev.config + offset, data);
@@ -470,17 +459,39 @@ 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 pci config write at %lx, size %u",
+                 offset, 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_BDSM_GEN11, bdsm)
+
+#define IGD_BDSM_MMIO_OFFSET    0x1080C0
+
 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
 {
     VFIOQuirk *quirk;
@@ -510,8 +521,9 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
     quirk = vfio_quirk_alloc(1);
     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_bdsm, vdev,
+                          "vfio-igd-bdsm-quirk", 8);
     memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
                                         IGD_BDSM_MMIO_OFFSET, &quirk->mem[0],
                                         1);
Signature · 1 line
2.45.2
#8 lore raw

PATCH v4 8/10 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.

[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 | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
hw/vfio/igd.c +11 -2
index 18d159aa8e..12e0553e83 100644
@@ -488,8 +488,10 @@ static const MemoryRegionOps vfio_igd_quirk_mirror_##name = {           \
     .endianness = DEVICE_LITTLE_ENDIAN,                                 \
 };
 
+VFIO_IGD_QUIRK_MIRROR_REG(IGD_GMCH, ggc)
 VFIO_IGD_QUIRK_MIRROR_REG(IGD_BDSM_GEN11, bdsm)
 
+#define IGD_GGC_MMIO_OFFSET     0x108040
 #define IGD_BDSM_MMIO_OFFSET    0x1080C0
 
 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
@@ -518,14 +520,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_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
#9 lore raw

PATCH v4 9/10 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 12e0553e83..73c06bbf64 100644
@@ -489,7 +489,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)
 
 #define IGD_GGC_MMIO_OFFSET     0x108040
 #define IGD_BDSM_MMIO_OFFSET    0x1080C0
@@ -516,7 +517,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;
     }
 
@@ -530,12 +531,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
#10 lore raw

PATCH v4 10/10 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). Since 971ca22f041b ("vfio/igd: don't set stolen memory size to zero"), the x-igd-gms option was functionally removed, QEMU uses host's original value, which is determined by DVMT Pre-Allocated option in Intel FSP of host bios.

However, 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. So this commit brings this option back, keeping its old behavior. When it is not specified, QEMU uses host's value.

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 | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
hw/vfio/igd.c +26 -0
index 73c06bbf64..b0fef90240 100644
@@ -14,6 +14,7 @@
 #include "qemu/units.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
+#include "qapi/qmp/qerror.h"
 #include "hw/hw.h"
 #include "hw/nvram/fw_cfg.h"
 #include "pci.h"
@@ -722,6 +723,31 @@ 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) {
+            if (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 {
+                error_report(QERR_INVALID_PARAMETER_VALUE,
+                             "x-igd-gms", "0~0x10");
+            }
+        } 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(QERR_INVALID_PARAMETER_VALUE,
+                             "x-igd-gms", "0~0x40");
+            }
+        }
+    }
+
     ggms_size = igd_gtt_memory_size(gen, gmch);
     gms_size = igd_stolen_memory_size(gen, gmch);
 
Signature · 1 line
2.45.2
#11 lore raw
In reply to Tomita Moeko
Quoted 30 lines · Tomita Moeko

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.

Quoted 5 lines

Changelog:
v4:
* Move "vfio/igd: fix GTT stolen memory size calculation for gen 8+" to the first.

A v4 was not necessary for just this change. Anyhow, It seems the series is ready and an overall 'Acked-by' from Alex or Corvin would be appreciated before applying to vfio-next.

Thanks,

C.

Quoted 34 lines

v3:
* Added new patch to fix GTT stolen memory size calculation for gen 8+ devicess.
* Fixed quirk index in "vfio/igd: add macro for declaring mirrored registers"
* Fixed condition in "vfio/igd: add x-igd-gms option back to set DSM region size for guest"
* Updated comments and commit message.

v2:
* Droped "vfio/igd: fix GTT stolen memory size calculation for gen 7".
* Fixed conditions when calculating GGMS size.
* Added Gemini Lake and Comet Lake device ids.
* Splited mirroring register declaration macro into a new patch.
* Minor fixes.

Tomita Moeko (10):
vfio/igd: fix GTT stolen memory size calculation for gen 8+ vfio/igd: remove unsupported device ids
vfio/igd: align generation with i915 kernel driver vfio/igd: canonicalize memory size calculations
vfio/igd: add Gemini Lake and Comet Lake device ids vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper Lake device ids vfio/igd: add macro for declaring mirrored registers 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 | 262 +++++++++++++++++++++++++++++++-------------------
  1 file changed, 165 insertions(+), 97 deletions(-)
Reviewed-by on the v4 series
#12 lore raw
In reply to Tomita Moeko
Quoted 30 lines · On Fri, 6 Dec 2024 20:27:38 +0800 Tomita Moeko <[email protected]> wrote:

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.

Quoted 5 lines

Changelog:
v4:
* Move "vfio/igd: fix GTT stolen memory size calculation for gen 8+" to the first.

Series:

#13 lore raw
In reply to Tomita Moeko
Quoted 66 lines · Tomita Moeko

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.

Changelog:
v4:
* Move "vfio/igd: fix GTT stolen memory size calculation for gen 8+" to the first.

v3:
* Added new patch to fix GTT stolen memory size calculation for gen 8+ devicess.
* Fixed quirk index in "vfio/igd: add macro for declaring mirrored registers"
* Fixed condition in "vfio/igd: add x-igd-gms option back to set DSM region size for guest"
* Updated comments and commit message.

v2:
* Droped "vfio/igd: fix GTT stolen memory size calculation for gen 7".
* Fixed conditions when calculating GGMS size.
* Added Gemini Lake and Comet Lake device ids.
* Splited mirroring register declaration macro into a new patch.
* Minor fixes.

Tomita Moeko (10):
vfio/igd: fix GTT stolen memory size calculation for gen 8+ vfio/igd: remove unsupported device ids
vfio/igd: align generation with i915 kernel driver vfio/igd: canonicalize memory size calculations
vfio/igd: add Gemini Lake and Comet Lake device ids vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper Lake device ids vfio/igd: add macro for declaring mirrored registers 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

Quoted 3 lines
  hw/vfio/igd.c | 262 +++++++++++++++++++++++++++++++-------------------
  1 file changed, 165 insertions(+), 97 deletions(-)

Applied to vfio-next.

Thanks,

C.