xref: /openbmc/qemu/hw/vfio/igd.c (revision e50a24ea)
1 /*
2  * IGD device quirks
3  *
4  * Copyright Red Hat, Inc. 2016
5  *
6  * Authors:
7  *  Alex Williamson <alex.williamson@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/units.h"
15 #include "qemu/error-report.h"
16 #include "qapi/error.h"
17 #include "hw/hw.h"
18 #include "hw/nvram/fw_cfg.h"
19 #include "pci.h"
20 #include "trace.h"
21 
22 /*
23  * Intel IGD support
24  *
25  * Obviously IGD is not a discrete device, this is evidenced not only by it
26  * being integrated into the CPU, but by the various chipset and BIOS
27  * dependencies that it brings along with it.  Intel is trying to move away
28  * from this and Broadwell and newer devices can run in what Intel calls
29  * "Universal Pass-Through" mode, or UPT.  Theoretically in UPT mode, nothing
30  * more is required beyond assigning the IGD device to a VM.  There are
31  * however support limitations to this mode.  It only supports IGD as a
32  * secondary graphics device in the VM and it doesn't officially support any
33  * physical outputs.
34  *
35  * The code here attempts to enable what we'll call legacy mode assignment,
36  * IGD retains most of the capabilities we expect for it to have on bare
37  * metal.  To enable this mode, the IGD device must be assigned to the VM
38  * at PCI address 00:02.0, it must have a ROM, it very likely needs VGA
39  * support, we must have VM BIOS support for reserving and populating some
40  * of the required tables, and we need to tweak the chipset with revisions
41  * and IDs and an LPC/ISA bridge device.  The intention is to make all of
42  * this happen automatically by installing the device at the correct VM PCI
43  * bus address.  If any of the conditions are not met, we cross our fingers
44  * and hope the user knows better.
45  *
46  * NB - It is possible to enable physical outputs in UPT mode by supplying
47  * an OpRegion table.  We don't do this by default because the guest driver
48  * behaves differently if an OpRegion is provided and no monitor is attached
49  * vs no OpRegion and a monitor being attached or not.  Effectively, if a
50  * headless setup is desired, the OpRegion gets in the way of that.
51  */
52 
53 /*
54  * This presumes the device is already known to be an Intel VGA device, so we
55  * take liberties in which device ID bits match which generation.  This should
56  * not be taken as an indication that all the devices are supported, or even
57  * supportable, some of them don't even support VT-d.
58  * See linux:include/drm/i915_pciids.h for IDs.
59  */
60 static int igd_gen(VFIOPCIDevice *vdev)
61 {
62     if ((vdev->device_id & 0xfff) == 0xa84) {
63         return 8; /* Broxton */
64     }
65 
66     switch (vdev->device_id & 0xff00) {
67     /* Old, untested, unavailable, unknown */
68     case 0x0000:
69     case 0x2500:
70     case 0x2700:
71     case 0x2900:
72     case 0x2a00:
73     case 0x2e00:
74     case 0x3500:
75     case 0xa000:
76         return -1;
77     /* SandyBridge, IvyBridge, ValleyView, Haswell */
78     case 0x0100:
79     case 0x0400:
80     case 0x0a00:
81     case 0x0c00:
82     case 0x0d00:
83     case 0x0f00:
84         return 6;
85     /* BroadWell, CherryView, SkyLake, KabyLake */
86     case 0x1600:
87     case 0x1900:
88     case 0x2200:
89     case 0x5900:
90         return 8;
91     /* CoffeeLake */
92     case 0x3e00:
93         return 9;
94     /* ElkhartLake */
95     case 0x4500:
96         return 11;
97     /* TigerLake */
98     case 0x9A00:
99         return 12;
100     }
101 
102     /*
103      * Unfortunately, Intel changes it's specification quite often. This makes
104      * it impossible to use a suitable default value for unknown devices.
105      */
106     return -1;
107 }
108 
109 typedef struct VFIOIGDQuirk {
110     struct VFIOPCIDevice *vdev;
111     uint32_t index;
112     uint64_t bdsm;
113 } VFIOIGDQuirk;
114 
115 #define IGD_GMCH 0x50 /* Graphics Control Register */
116 #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
117 #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later */
118 
119 
120 /*
121  * The rather short list of registers that we copy from the host devices.
122  * The LPC/ISA bridge values are definitely needed to support the vBIOS, the
123  * host bridge values may or may not be needed depending on the guest OS.
124  * Since we're only munging revision and subsystem values on the host bridge,
125  * we don't require our own device.  The LPC/ISA bridge needs to be our very
126  * own though.
127  */
128 typedef struct {
129     uint8_t offset;
130     uint8_t len;
131 } IGDHostInfo;
132 
133 static const IGDHostInfo igd_host_bridge_infos[] = {
134     {PCI_REVISION_ID,         2},
135     {PCI_SUBSYSTEM_VENDOR_ID, 2},
136     {PCI_SUBSYSTEM_ID,        2},
137 };
138 
139 static const IGDHostInfo igd_lpc_bridge_infos[] = {
140     {PCI_VENDOR_ID,           2},
141     {PCI_DEVICE_ID,           2},
142     {PCI_REVISION_ID,         2},
143     {PCI_SUBSYSTEM_VENDOR_ID, 2},
144     {PCI_SUBSYSTEM_ID,        2},
145 };
146 
147 static int vfio_pci_igd_copy(VFIOPCIDevice *vdev, PCIDevice *pdev,
148                              struct vfio_region_info *info,
149                              const IGDHostInfo *list, int len)
150 {
151     int i, ret;
152 
153     for (i = 0; i < len; i++) {
154         ret = pread(vdev->vbasedev.fd, pdev->config + list[i].offset,
155                     list[i].len, info->offset + list[i].offset);
156         if (ret != list[i].len) {
157             error_report("IGD copy failed: %m");
158             return -errno;
159         }
160     }
161 
162     return 0;
163 }
164 
165 /*
166  * Stuff a few values into the host bridge.
167  */
168 static int vfio_pci_igd_host_init(VFIOPCIDevice *vdev,
169                                   struct vfio_region_info *info)
170 {
171     PCIBus *bus;
172     PCIDevice *host_bridge;
173     int ret;
174 
175     bus = pci_device_root_bus(&vdev->pdev);
176     host_bridge = pci_find_device(bus, 0, PCI_DEVFN(0, 0));
177 
178     if (!host_bridge) {
179         error_report("Can't find host bridge");
180         return -ENODEV;
181     }
182 
183     ret = vfio_pci_igd_copy(vdev, host_bridge, info, igd_host_bridge_infos,
184                             ARRAY_SIZE(igd_host_bridge_infos));
185     if (!ret) {
186         trace_vfio_pci_igd_host_bridge_enabled(vdev->vbasedev.name);
187     }
188 
189     return ret;
190 }
191 
192 /*
193  * IGD LPC/ISA bridge support code.  The vBIOS needs this, but we can't write
194  * arbitrary values into just any bridge, so we must create our own.  We try
195  * to handle if the user has created it for us, which they might want to do
196  * to enable multifunction so we don't occupy the whole PCI slot.
197  */
198 static void vfio_pci_igd_lpc_bridge_realize(PCIDevice *pdev, Error **errp)
199 {
200     if (pdev->devfn != PCI_DEVFN(0x1f, 0)) {
201         error_setg(errp, "VFIO dummy ISA/LPC bridge must have address 1f.0");
202     }
203 }
204 
205 static void vfio_pci_igd_lpc_bridge_class_init(ObjectClass *klass, void *data)
206 {
207     DeviceClass *dc = DEVICE_CLASS(klass);
208     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
209 
210     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
211     dc->desc = "VFIO dummy ISA/LPC bridge for IGD assignment";
212     dc->hotpluggable = false;
213     k->realize = vfio_pci_igd_lpc_bridge_realize;
214     k->class_id = PCI_CLASS_BRIDGE_ISA;
215 }
216 
217 static const TypeInfo vfio_pci_igd_lpc_bridge_info = {
218     .name = "vfio-pci-igd-lpc-bridge",
219     .parent = TYPE_PCI_DEVICE,
220     .class_init = vfio_pci_igd_lpc_bridge_class_init,
221     .interfaces = (InterfaceInfo[]) {
222         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
223         { },
224     },
225 };
226 
227 static void vfio_pci_igd_register_types(void)
228 {
229     type_register_static(&vfio_pci_igd_lpc_bridge_info);
230 }
231 
232 type_init(vfio_pci_igd_register_types)
233 
234 static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
235                                  struct vfio_region_info *info)
236 {
237     PCIDevice *lpc_bridge;
238     int ret;
239 
240     lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
241                                  0, PCI_DEVFN(0x1f, 0));
242     if (!lpc_bridge) {
243         lpc_bridge = pci_create_simple(pci_device_root_bus(&vdev->pdev),
244                                  PCI_DEVFN(0x1f, 0), "vfio-pci-igd-lpc-bridge");
245     }
246 
247     ret = vfio_pci_igd_copy(vdev, lpc_bridge, info, igd_lpc_bridge_infos,
248                             ARRAY_SIZE(igd_lpc_bridge_infos));
249     if (!ret) {
250         trace_vfio_pci_igd_lpc_bridge_enabled(vdev->vbasedev.name);
251     }
252 
253     return ret;
254 }
255 
256 /*
257  * IGD Gen8 and newer support up to 8MB for the GTT and use a 64bit PTE
258  * entry, older IGDs use 2MB and 32bit.  Each PTE maps a 4k page.  Therefore
259  * we either have 2M/4k * 4 = 2k or 8M/4k * 8 = 16k as the maximum iobar index
260  * for programming the GTT.
261  *
262  * See linux:include/drm/i915_drm.h for shift and mask values.
263  */
264 static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
265 {
266     uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
267     int ggms, gen = igd_gen(vdev);
268 
269     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
270     ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
271     if (gen > 6) {
272         ggms = 1 << ggms;
273     }
274 
275     ggms *= MiB;
276 
277     return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
278 }
279 
280 /*
281  * The IGD ROM will make use of stolen memory (GGMS) for support of VESA modes.
282  * Somehow the host stolen memory range is used for this, but how the ROM gets
283  * it is a mystery, perhaps it's hardcoded into the ROM.  Thankfully though, it
284  * reprograms the GTT through the IOBAR where we can trap it and transpose the
285  * programming to the VM allocated buffer.  That buffer gets reserved by the VM
286  * firmware via the fw_cfg entry added below.  Here we're just monitoring the
287  * IOBAR address and data registers to detect a write sequence targeting the
288  * GTTADR.  This code is developed by observed behavior and doesn't have a
289  * direct spec reference, unfortunately.
290  */
291 static uint64_t vfio_igd_quirk_data_read(void *opaque,
292                                          hwaddr addr, unsigned size)
293 {
294     VFIOIGDQuirk *igd = opaque;
295     VFIOPCIDevice *vdev = igd->vdev;
296 
297     igd->index = ~0;
298 
299     return vfio_region_read(&vdev->bars[4].region, addr + 4, size);
300 }
301 
302 static void vfio_igd_quirk_data_write(void *opaque, hwaddr addr,
303                                       uint64_t data, unsigned size)
304 {
305     VFIOIGDQuirk *igd = opaque;
306     VFIOPCIDevice *vdev = igd->vdev;
307     uint64_t val = data;
308     int gen = igd_gen(vdev);
309 
310     /*
311      * Programming the GGMS starts at index 0x1 and uses every 4th index (ie.
312      * 0x1, 0x5, 0x9, 0xd,...).  For pre-Gen8 each 4-byte write is a whole PTE
313      * entry, with 0th bit enable set.  For Gen8 and up, PTEs are 64bit, so
314      * entries 0x5 & 0xd are the high dword, in our case zero.  Each PTE points
315      * to a 4k page, which we translate to a page from the VM allocated region,
316      * pointed to by the BDSM register.  If this is not set, we fail.
317      *
318      * We trap writes to the full configured GTT size, but we typically only
319      * see the vBIOS writing up to (nearly) the 1MB barrier.  In fact it often
320      * seems to miss the last entry for an even 1MB GTT.  Doing a gratuitous
321      * write of that last entry does work, but is hopefully unnecessary since
322      * we clear the previous GTT on initialization.
323      */
324     if ((igd->index % 4 == 1) && igd->index < vfio_igd_gtt_max(vdev)) {
325         if (gen < 8 || (igd->index % 8 == 1)) {
326             uint64_t base;
327 
328             if (gen < 11) {
329                 base = pci_get_long(vdev->pdev.config + IGD_BDSM);
330             } else {
331                 base = pci_get_quad(vdev->pdev.config + IGD_BDSM_GEN11);
332             }
333             if (!base) {
334                 hw_error("vfio-igd: Guest attempted to program IGD GTT before "
335                          "BIOS reserved stolen memory.  Unsupported BIOS?");
336             }
337 
338             val = data - igd->bdsm + base;
339         } else {
340             val = 0; /* upper 32bits of pte, we only enable below 4G PTEs */
341         }
342 
343         trace_vfio_pci_igd_bar4_write(vdev->vbasedev.name,
344                                       igd->index, data, val);
345     }
346 
347     vfio_region_write(&vdev->bars[4].region, addr + 4, val, size);
348 
349     igd->index = ~0;
350 }
351 
352 static const MemoryRegionOps vfio_igd_data_quirk = {
353     .read = vfio_igd_quirk_data_read,
354     .write = vfio_igd_quirk_data_write,
355     .endianness = DEVICE_LITTLE_ENDIAN,
356 };
357 
358 static uint64_t vfio_igd_quirk_index_read(void *opaque,
359                                           hwaddr addr, unsigned size)
360 {
361     VFIOIGDQuirk *igd = opaque;
362     VFIOPCIDevice *vdev = igd->vdev;
363 
364     igd->index = ~0;
365 
366     return vfio_region_read(&vdev->bars[4].region, addr, size);
367 }
368 
369 static void vfio_igd_quirk_index_write(void *opaque, hwaddr addr,
370                                        uint64_t data, unsigned size)
371 {
372     VFIOIGDQuirk *igd = opaque;
373     VFIOPCIDevice *vdev = igd->vdev;
374 
375     igd->index = data;
376 
377     vfio_region_write(&vdev->bars[4].region, addr, data, size);
378 }
379 
380 static const MemoryRegionOps vfio_igd_index_quirk = {
381     .read = vfio_igd_quirk_index_read,
382     .write = vfio_igd_quirk_index_write,
383     .endianness = DEVICE_LITTLE_ENDIAN,
384 };
385 
386 #define IGD_BDSM_MMIO_OFFSET 0x1080C0
387 
388 static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
389                                           hwaddr addr, unsigned size)
390 {
391     VFIOPCIDevice *vdev = opaque;
392     uint64_t offset;
393 
394     offset = IGD_BDSM_GEN11 + addr;
395 
396     switch (size) {
397     case 1:
398         return pci_get_byte(vdev->pdev.config + offset);
399     case 2:
400         return pci_get_word(vdev->pdev.config + offset);
401     case 4:
402         return pci_get_long(vdev->pdev.config + offset);
403     case 8:
404         return pci_get_quad(vdev->pdev.config + offset);
405     default:
406         hw_error("igd: unsupported read size, %u bytes", size);
407         break;
408     }
409 
410     return 0;
411 }
412 
413 static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
414                                        uint64_t data, unsigned size)
415 {
416     VFIOPCIDevice *vdev = opaque;
417     uint64_t offset;
418 
419     offset = IGD_BDSM_GEN11 + addr;
420 
421     switch (size) {
422     case 1:
423         pci_set_byte(vdev->pdev.config + offset, data);
424         break;
425     case 2:
426         pci_set_word(vdev->pdev.config + offset, data);
427         break;
428     case 4:
429         pci_set_long(vdev->pdev.config + offset, data);
430         break;
431     case 8:
432         pci_set_quad(vdev->pdev.config + offset, data);
433         break;
434     default:
435         hw_error("igd: unsupported read size, %u bytes", size);
436         break;
437     }
438 }
439 
440 static const MemoryRegionOps vfio_igd_bdsm_quirk = {
441     .read = vfio_igd_quirk_bdsm_read,
442     .write = vfio_igd_quirk_bdsm_write,
443     .endianness = DEVICE_LITTLE_ENDIAN,
444 };
445 
446 void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
447 {
448     VFIOQuirk *quirk;
449     int gen;
450 
451     /*
452      * This must be an Intel VGA device at address 00:02.0 for us to even
453      * consider enabling legacy mode. Some driver have dependencies on the PCI
454      * bus address.
455      */
456     if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
457         !vfio_is_vga(vdev) || nr != 0 ||
458         &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
459                                        0, PCI_DEVFN(0x2, 0))) {
460         return;
461     }
462 
463     /*
464      * Only on IGD devices of gen 11 and above, the BDSM register is mirrored
465      * into MMIO space and read from MMIO space by the Windows driver.
466      */
467     gen = igd_gen(vdev);
468     if (gen < 11) {
469         return;
470     }
471 
472     quirk = vfio_quirk_alloc(1);
473     quirk->data = vdev;
474 
475     memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_bdsm_quirk,
476                           vdev, "vfio-igd-bdsm-quirk", 8);
477     memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
478                                         IGD_BDSM_MMIO_OFFSET, &quirk->mem[0],
479                                         1);
480 
481     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
482 }
483 
484 static int igd_get_stolen_mb(int gen, uint32_t gmch)
485 {
486     int gms;
487 
488     if (gen < 8) {
489         gms = (gmch >> 3) & 0x1f;
490     } else {
491         gms = (gmch >> 8) & 0xff;
492     }
493 
494     if (gen < 9) {
495         if (gms > 0x10) {
496             error_report("Unsupported IGD GMS value 0x%x", gms);
497             return 0;
498         }
499         return gms * 32;
500     } else {
501         if (gms < 0xf0)
502             return gms * 32;
503         else
504             return (gms - 0xf0) * 4 + 4;
505     }
506 }
507 
508 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
509 {
510     g_autofree struct vfio_region_info *rom = NULL;
511     g_autofree struct vfio_region_info *opregion = NULL;
512     g_autofree struct vfio_region_info *host = NULL;
513     g_autofree struct vfio_region_info *lpc = NULL;
514     VFIOQuirk *quirk;
515     VFIOIGDQuirk *igd;
516     PCIDevice *lpc_bridge;
517     int i, ret, ggms_mb, gms_mb = 0, gen;
518     uint64_t *bdsm_size;
519     uint32_t gmch;
520     uint16_t cmd_orig, cmd;
521     Error *err = NULL;
522 
523     /*
524      * This must be an Intel VGA device at address 00:02.0 for us to even
525      * consider enabling legacy mode.  The vBIOS has dependencies on the
526      * PCI bus address.
527      */
528     if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
529         !vfio_is_vga(vdev) || nr != 4 ||
530         &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
531                                        0, PCI_DEVFN(0x2, 0))) {
532         return;
533     }
534 
535     /*
536      * We need to create an LPC/ISA bridge at PCI bus address 00:1f.0 that we
537      * can stuff host values into, so if there's already one there and it's not
538      * one we can hack on, legacy mode is no-go.  Sorry Q35.
539      */
540     lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
541                                  0, PCI_DEVFN(0x1f, 0));
542     if (lpc_bridge && !object_dynamic_cast(OBJECT(lpc_bridge),
543                                            "vfio-pci-igd-lpc-bridge")) {
544         error_report("IGD device %s cannot support legacy mode due to existing "
545                      "devices at address 1f.0", vdev->vbasedev.name);
546         return;
547     }
548 
549     /*
550      * IGD is not a standard, they like to change their specs often.  We
551      * only attempt to support back to SandBridge and we hope that newer
552      * devices maintain compatibility with generation 8.
553      */
554     gen = igd_gen(vdev);
555     if (gen == -1) {
556         error_report("IGD device %s is unsupported in legacy mode, "
557                      "try SandyBridge or newer", vdev->vbasedev.name);
558         return;
559     }
560 
561     /*
562      * Most of what we're doing here is to enable the ROM to run, so if
563      * there's no ROM, there's no point in setting up this quirk.
564      * NB. We only seem to get BIOS ROMs, so a UEFI VM would need CSM support.
565      */
566     ret = vfio_get_region_info(&vdev->vbasedev,
567                                VFIO_PCI_ROM_REGION_INDEX, &rom);
568     if ((ret || !rom->size) && !vdev->pdev.romfile) {
569         error_report("IGD device %s has no ROM, legacy mode disabled",
570                      vdev->vbasedev.name);
571         return;
572     }
573 
574     /*
575      * Ignore the hotplug corner case, mark the ROM failed, we can't
576      * create the devices we need for legacy mode in the hotplug scenario.
577      */
578     if (vdev->pdev.qdev.hotplugged) {
579         error_report("IGD device %s hotplugged, ROM disabled, "
580                      "legacy mode disabled", vdev->vbasedev.name);
581         vdev->rom_read_failed = true;
582         return;
583     }
584 
585     /*
586      * Check whether we have all the vfio device specific regions to
587      * support legacy mode (added in Linux v4.6).  If not, bail.
588      */
589     ret = vfio_get_dev_region_info(&vdev->vbasedev,
590                         VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
591                         VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion);
592     if (ret) {
593         error_report("IGD device %s does not support OpRegion access,"
594                      "legacy mode disabled", vdev->vbasedev.name);
595         return;
596     }
597 
598     ret = vfio_get_dev_region_info(&vdev->vbasedev,
599                         VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
600                         VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, &host);
601     if (ret) {
602         error_report("IGD device %s does not support host bridge access,"
603                      "legacy mode disabled", vdev->vbasedev.name);
604         return;
605     }
606 
607     ret = vfio_get_dev_region_info(&vdev->vbasedev,
608                         VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
609                         VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG, &lpc);
610     if (ret) {
611         error_report("IGD device %s does not support LPC bridge access,"
612                      "legacy mode disabled", vdev->vbasedev.name);
613         return;
614     }
615 
616     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, 4);
617 
618     /*
619      * If IGD VGA Disable is clear (expected) and VGA is not already enabled,
620      * try to enable it.  Probably shouldn't be using legacy mode without VGA,
621      * but also no point in us enabling VGA if disabled in hardware.
622      */
623     if (!(gmch & 0x2) && !vdev->vga && !vfio_populate_vga(vdev, &err)) {
624         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
625         error_report("IGD device %s failed to enable VGA access, "
626                      "legacy mode disabled", vdev->vbasedev.name);
627         return;
628     }
629 
630     /* Create our LPC/ISA bridge */
631     ret = vfio_pci_igd_lpc_init(vdev, lpc);
632     if (ret) {
633         error_report("IGD device %s failed to create LPC bridge, "
634                      "legacy mode disabled", vdev->vbasedev.name);
635         return;
636     }
637 
638     /* Stuff some host values into the VM PCI host bridge */
639     ret = vfio_pci_igd_host_init(vdev, host);
640     if (ret) {
641         error_report("IGD device %s failed to modify host bridge, "
642                      "legacy mode disabled", vdev->vbasedev.name);
643         return;
644     }
645 
646     /* Setup OpRegion access */
647     if (!vfio_pci_igd_opregion_init(vdev, opregion, &err)) {
648         error_append_hint(&err, "IGD legacy mode disabled\n");
649         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
650         return;
651     }
652 
653     /* Setup our quirk to munge GTT addresses to the VM allocated buffer */
654     quirk = vfio_quirk_alloc(2);
655     igd = quirk->data = g_malloc0(sizeof(*igd));
656     igd->vdev = vdev;
657     igd->index = ~0;
658     if (gen < 11) {
659         igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM, 4);
660     } else {
661         igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM_GEN11, 4);
662         igd->bdsm |=
663             (uint64_t)vfio_pci_read_config(&vdev->pdev, IGD_BDSM_GEN11 + 4, 4) << 32;
664     }
665     igd->bdsm &= ~((1 * MiB) - 1); /* 1MB aligned */
666 
667     memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_index_quirk,
668                           igd, "vfio-igd-index-quirk", 4);
669     memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
670                                         0, &quirk->mem[0], 1);
671 
672     memory_region_init_io(&quirk->mem[1], OBJECT(vdev), &vfio_igd_data_quirk,
673                           igd, "vfio-igd-data-quirk", 4);
674     memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
675                                         4, &quirk->mem[1], 1);
676 
677     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
678 
679     /* Determine the size of stolen memory needed for GTT */
680     ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
681     if (gen > 6) {
682         ggms_mb = 1 << ggms_mb;
683     }
684 
685     gms_mb = igd_get_stolen_mb(gen, gmch);
686 
687     /*
688      * Request reserved memory for stolen memory via fw_cfg.  VM firmware
689      * must allocate a 1MB aligned reserved memory region below 4GB with
690      * the requested size (in bytes) for use by the Intel PCI class VGA
691      * device at VM address 00:02.0.  The base address of this reserved
692      * memory region must be written to the device BDSM register at PCI
693      * config offset 0x5C.
694      */
695     bdsm_size = g_malloc(sizeof(*bdsm_size));
696     *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
697     fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
698                     bdsm_size, sizeof(*bdsm_size));
699 
700     /* GMCH is read-only, emulated */
701     pci_set_long(vdev->pdev.config + IGD_GMCH, gmch);
702     pci_set_long(vdev->pdev.wmask + IGD_GMCH, 0);
703     pci_set_long(vdev->emulated_config_bits + IGD_GMCH, ~0);
704 
705     /* BDSM is read-write, emulated.  The BIOS needs to be able to write it */
706     if (gen < 11) {
707         pci_set_long(vdev->pdev.config + IGD_BDSM, 0);
708         pci_set_long(vdev->pdev.wmask + IGD_BDSM, ~0);
709         pci_set_long(vdev->emulated_config_bits + IGD_BDSM, ~0);
710     } else {
711         pci_set_quad(vdev->pdev.config + IGD_BDSM_GEN11, 0);
712         pci_set_quad(vdev->pdev.wmask + IGD_BDSM_GEN11, ~0);
713         pci_set_quad(vdev->emulated_config_bits + IGD_BDSM_GEN11, ~0);
714     }
715 
716     /*
717      * This IOBAR gives us access to GTTADR, which allows us to write to
718      * the GTT itself.  So let's go ahead and write zero to all the GTT
719      * entries to avoid spurious DMA faults.  Be sure I/O access is enabled
720      * before talking to the device.
721      */
722     if (pread(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
723               vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
724         error_report("IGD device %s - failed to read PCI command register",
725                      vdev->vbasedev.name);
726     }
727 
728     cmd = cmd_orig | PCI_COMMAND_IO;
729 
730     if (pwrite(vdev->vbasedev.fd, &cmd, sizeof(cmd),
731                vdev->config_offset + PCI_COMMAND) != sizeof(cmd)) {
732         error_report("IGD device %s - failed to write PCI command register",
733                      vdev->vbasedev.name);
734     }
735 
736     for (i = 1; i < vfio_igd_gtt_max(vdev); i += 4) {
737         vfio_region_write(&vdev->bars[4].region, 0, i, 4);
738         vfio_region_write(&vdev->bars[4].region, 4, 0, 4);
739     }
740 
741     if (pwrite(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
742                vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
743         error_report("IGD device %s - failed to restore PCI command register",
744                      vdev->vbasedev.name);
745     }
746 
747     trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
748 }
749