xref: /openbmc/qemu/hw/vfio/igd.c (revision 8719224166832ff8230d7dd8599f42bd60e2eb96)
129d62771SThomas Huth /*
229d62771SThomas Huth  * IGD device quirks
329d62771SThomas Huth  *
429d62771SThomas Huth  * Copyright Red Hat, Inc. 2016
529d62771SThomas Huth  *
629d62771SThomas Huth  * Authors:
729d62771SThomas Huth  *  Alex Williamson <alex.williamson@redhat.com>
829d62771SThomas Huth  *
929d62771SThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2.  See
1029d62771SThomas Huth  * the COPYING file in the top-level directory.
1129d62771SThomas Huth  */
1229d62771SThomas Huth 
1329d62771SThomas Huth #include "qemu/osdep.h"
1429d62771SThomas Huth #include "qemu/units.h"
15cc37d98bSRichard Henderson #include "qemu/error-report.h"
1629d62771SThomas Huth #include "qapi/error.h"
1729d62771SThomas Huth #include "hw/hw.h"
1829d62771SThomas Huth #include "hw/nvram/fw_cfg.h"
1929d62771SThomas Huth #include "pci.h"
2029d62771SThomas Huth #include "trace.h"
2129d62771SThomas Huth 
2229d62771SThomas Huth /*
2329d62771SThomas Huth  * Intel IGD support
2429d62771SThomas Huth  *
2529d62771SThomas Huth  * Obviously IGD is not a discrete device, this is evidenced not only by it
2629d62771SThomas Huth  * being integrated into the CPU, but by the various chipset and BIOS
2729d62771SThomas Huth  * dependencies that it brings along with it.  Intel is trying to move away
2829d62771SThomas Huth  * from this and Broadwell and newer devices can run in what Intel calls
2929d62771SThomas Huth  * "Universal Pass-Through" mode, or UPT.  Theoretically in UPT mode, nothing
3029d62771SThomas Huth  * more is required beyond assigning the IGD device to a VM.  There are
3129d62771SThomas Huth  * however support limitations to this mode.  It only supports IGD as a
3229d62771SThomas Huth  * secondary graphics device in the VM and it doesn't officially support any
3329d62771SThomas Huth  * physical outputs.
3429d62771SThomas Huth  *
3529d62771SThomas Huth  * The code here attempts to enable what we'll call legacy mode assignment,
3629d62771SThomas Huth  * IGD retains most of the capabilities we expect for it to have on bare
3729d62771SThomas Huth  * metal.  To enable this mode, the IGD device must be assigned to the VM
3829d62771SThomas Huth  * at PCI address 00:02.0, it must have a ROM, it very likely needs VGA
3929d62771SThomas Huth  * support, we must have VM BIOS support for reserving and populating some
4029d62771SThomas Huth  * of the required tables, and we need to tweak the chipset with revisions
4129d62771SThomas Huth  * and IDs and an LPC/ISA bridge device.  The intention is to make all of
4229d62771SThomas Huth  * this happen automatically by installing the device at the correct VM PCI
4329d62771SThomas Huth  * bus address.  If any of the conditions are not met, we cross our fingers
4429d62771SThomas Huth  * and hope the user knows better.
4529d62771SThomas Huth  *
4629d62771SThomas Huth  * NB - It is possible to enable physical outputs in UPT mode by supplying
4729d62771SThomas Huth  * an OpRegion table.  We don't do this by default because the guest driver
4829d62771SThomas Huth  * behaves differently if an OpRegion is provided and no monitor is attached
4929d62771SThomas Huth  * vs no OpRegion and a monitor being attached or not.  Effectively, if a
5029d62771SThomas Huth  * headless setup is desired, the OpRegion gets in the way of that.
5129d62771SThomas Huth  */
5229d62771SThomas Huth 
5329d62771SThomas Huth /*
5429d62771SThomas Huth  * This presumes the device is already known to be an Intel VGA device, so we
5529d62771SThomas Huth  * take liberties in which device ID bits match which generation.  This should
5629d62771SThomas Huth  * not be taken as an indication that all the devices are supported, or even
5729d62771SThomas Huth  * supportable, some of them don't even support VT-d.
5829d62771SThomas Huth  * See linux:include/drm/i915_pciids.h for IDs.
5929d62771SThomas Huth  */
6029d62771SThomas Huth static int igd_gen(VFIOPCIDevice *vdev)
6129d62771SThomas Huth {
6229d62771SThomas Huth     if ((vdev->device_id & 0xfff) == 0xa84) {
6329d62771SThomas Huth         return 8; /* Broxton */
6429d62771SThomas Huth     }
6529d62771SThomas Huth 
6629d62771SThomas Huth     switch (vdev->device_id & 0xff00) {
6729d62771SThomas Huth     /* Old, untested, unavailable, unknown */
6829d62771SThomas Huth     case 0x0000:
6929d62771SThomas Huth     case 0x2500:
7029d62771SThomas Huth     case 0x2700:
7129d62771SThomas Huth     case 0x2900:
7229d62771SThomas Huth     case 0x2a00:
7329d62771SThomas Huth     case 0x2e00:
7429d62771SThomas Huth     case 0x3500:
7529d62771SThomas Huth     case 0xa000:
7629d62771SThomas Huth         return -1;
7729d62771SThomas Huth     /* SandyBridge, IvyBridge, ValleyView, Haswell */
7829d62771SThomas Huth     case 0x0100:
7929d62771SThomas Huth     case 0x0400:
8029d62771SThomas Huth     case 0x0a00:
8129d62771SThomas Huth     case 0x0c00:
8229d62771SThomas Huth     case 0x0d00:
8329d62771SThomas Huth     case 0x0f00:
8429d62771SThomas Huth         return 6;
8529d62771SThomas Huth     /* BroadWell, CherryView, SkyLake, KabyLake */
8629d62771SThomas Huth     case 0x1600:
8729d62771SThomas Huth     case 0x1900:
8829d62771SThomas Huth     case 0x2200:
8929d62771SThomas Huth     case 0x5900:
9029d62771SThomas Huth         return 8;
919c86b9fbSCorvin Köhne     /* ElkhartLake */
929c86b9fbSCorvin Köhne     case 0x4500:
939c86b9fbSCorvin Köhne         return 11;
949c86b9fbSCorvin Köhne     /* TigerLake */
959c86b9fbSCorvin Köhne     case 0x9A00:
969c86b9fbSCorvin Köhne         return 12;
9729d62771SThomas Huth     }
9829d62771SThomas Huth 
99e433f208SCorvin Köhne     /*
100e433f208SCorvin Köhne      * Unfortunately, Intel changes it's specification quite often. This makes
101e433f208SCorvin Köhne      * it impossible to use a suitable default value for unknown devices.
102e433f208SCorvin Köhne      */
103e433f208SCorvin Köhne     return -1;
10429d62771SThomas Huth }
10529d62771SThomas Huth 
10629d62771SThomas Huth typedef struct VFIOIGDQuirk {
10729d62771SThomas Huth     struct VFIOPCIDevice *vdev;
10829d62771SThomas Huth     uint32_t index;
1097bafcd17SCorvin Köhne     uint64_t bdsm;
11029d62771SThomas Huth } VFIOIGDQuirk;
11129d62771SThomas Huth 
11229d62771SThomas Huth #define IGD_GMCH 0x50 /* Graphics Control Register */
11329d62771SThomas Huth #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
1147bafcd17SCorvin Köhne #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later */
11529d62771SThomas Huth 
11629d62771SThomas Huth 
11729d62771SThomas Huth /*
11829d62771SThomas Huth  * The rather short list of registers that we copy from the host devices.
11929d62771SThomas Huth  * The LPC/ISA bridge values are definitely needed to support the vBIOS, the
12029d62771SThomas Huth  * host bridge values may or may not be needed depending on the guest OS.
12129d62771SThomas Huth  * Since we're only munging revision and subsystem values on the host bridge,
12229d62771SThomas Huth  * we don't require our own device.  The LPC/ISA bridge needs to be our very
12329d62771SThomas Huth  * own though.
12429d62771SThomas Huth  */
12529d62771SThomas Huth typedef struct {
12629d62771SThomas Huth     uint8_t offset;
12729d62771SThomas Huth     uint8_t len;
12829d62771SThomas Huth } IGDHostInfo;
12929d62771SThomas Huth 
13029d62771SThomas Huth static const IGDHostInfo igd_host_bridge_infos[] = {
13129d62771SThomas Huth     {PCI_REVISION_ID,         2},
13229d62771SThomas Huth     {PCI_SUBSYSTEM_VENDOR_ID, 2},
13329d62771SThomas Huth     {PCI_SUBSYSTEM_ID,        2},
13429d62771SThomas Huth };
13529d62771SThomas Huth 
13629d62771SThomas Huth static const IGDHostInfo igd_lpc_bridge_infos[] = {
13729d62771SThomas Huth     {PCI_VENDOR_ID,           2},
13829d62771SThomas Huth     {PCI_DEVICE_ID,           2},
13929d62771SThomas Huth     {PCI_REVISION_ID,         2},
14029d62771SThomas Huth     {PCI_SUBSYSTEM_VENDOR_ID, 2},
14129d62771SThomas Huth     {PCI_SUBSYSTEM_ID,        2},
14229d62771SThomas Huth };
14329d62771SThomas Huth 
14429d62771SThomas Huth static int vfio_pci_igd_copy(VFIOPCIDevice *vdev, PCIDevice *pdev,
14529d62771SThomas Huth                              struct vfio_region_info *info,
14629d62771SThomas Huth                              const IGDHostInfo *list, int len)
14729d62771SThomas Huth {
14829d62771SThomas Huth     int i, ret;
14929d62771SThomas Huth 
15029d62771SThomas Huth     for (i = 0; i < len; i++) {
15129d62771SThomas Huth         ret = pread(vdev->vbasedev.fd, pdev->config + list[i].offset,
15229d62771SThomas Huth                     list[i].len, info->offset + list[i].offset);
15329d62771SThomas Huth         if (ret != list[i].len) {
15429d62771SThomas Huth             error_report("IGD copy failed: %m");
15529d62771SThomas Huth             return -errno;
15629d62771SThomas Huth         }
15729d62771SThomas Huth     }
15829d62771SThomas Huth 
15929d62771SThomas Huth     return 0;
16029d62771SThomas Huth }
16129d62771SThomas Huth 
16229d62771SThomas Huth /*
16329d62771SThomas Huth  * Stuff a few values into the host bridge.
16429d62771SThomas Huth  */
16529d62771SThomas Huth static int vfio_pci_igd_host_init(VFIOPCIDevice *vdev,
16629d62771SThomas Huth                                   struct vfio_region_info *info)
16729d62771SThomas Huth {
16829d62771SThomas Huth     PCIBus *bus;
16929d62771SThomas Huth     PCIDevice *host_bridge;
17029d62771SThomas Huth     int ret;
17129d62771SThomas Huth 
17229d62771SThomas Huth     bus = pci_device_root_bus(&vdev->pdev);
17329d62771SThomas Huth     host_bridge = pci_find_device(bus, 0, PCI_DEVFN(0, 0));
17429d62771SThomas Huth 
17529d62771SThomas Huth     if (!host_bridge) {
17629d62771SThomas Huth         error_report("Can't find host bridge");
17729d62771SThomas Huth         return -ENODEV;
17829d62771SThomas Huth     }
17929d62771SThomas Huth 
18029d62771SThomas Huth     ret = vfio_pci_igd_copy(vdev, host_bridge, info, igd_host_bridge_infos,
18129d62771SThomas Huth                             ARRAY_SIZE(igd_host_bridge_infos));
18229d62771SThomas Huth     if (!ret) {
18329d62771SThomas Huth         trace_vfio_pci_igd_host_bridge_enabled(vdev->vbasedev.name);
18429d62771SThomas Huth     }
18529d62771SThomas Huth 
18629d62771SThomas Huth     return ret;
18729d62771SThomas Huth }
18829d62771SThomas Huth 
18929d62771SThomas Huth /*
19029d62771SThomas Huth  * IGD LPC/ISA bridge support code.  The vBIOS needs this, but we can't write
19129d62771SThomas Huth  * arbitrary values into just any bridge, so we must create our own.  We try
19229d62771SThomas Huth  * to handle if the user has created it for us, which they might want to do
19329d62771SThomas Huth  * to enable multifunction so we don't occupy the whole PCI slot.
19429d62771SThomas Huth  */
19529d62771SThomas Huth static void vfio_pci_igd_lpc_bridge_realize(PCIDevice *pdev, Error **errp)
19629d62771SThomas Huth {
19729d62771SThomas Huth     if (pdev->devfn != PCI_DEVFN(0x1f, 0)) {
19829d62771SThomas Huth         error_setg(errp, "VFIO dummy ISA/LPC bridge must have address 1f.0");
19929d62771SThomas Huth     }
20029d62771SThomas Huth }
20129d62771SThomas Huth 
20229d62771SThomas Huth static void vfio_pci_igd_lpc_bridge_class_init(ObjectClass *klass, void *data)
20329d62771SThomas Huth {
20429d62771SThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
20529d62771SThomas Huth     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
20629d62771SThomas Huth 
20729d62771SThomas Huth     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
20829d62771SThomas Huth     dc->desc = "VFIO dummy ISA/LPC bridge for IGD assignment";
20929d62771SThomas Huth     dc->hotpluggable = false;
21029d62771SThomas Huth     k->realize = vfio_pci_igd_lpc_bridge_realize;
21129d62771SThomas Huth     k->class_id = PCI_CLASS_BRIDGE_ISA;
21229d62771SThomas Huth }
21329d62771SThomas Huth 
2145e78c98bSBernhard Beschow static const TypeInfo vfio_pci_igd_lpc_bridge_info = {
21529d62771SThomas Huth     .name = "vfio-pci-igd-lpc-bridge",
21629d62771SThomas Huth     .parent = TYPE_PCI_DEVICE,
21729d62771SThomas Huth     .class_init = vfio_pci_igd_lpc_bridge_class_init,
21829d62771SThomas Huth     .interfaces = (InterfaceInfo[]) {
21929d62771SThomas Huth         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
22029d62771SThomas Huth         { },
22129d62771SThomas Huth     },
22229d62771SThomas Huth };
22329d62771SThomas Huth 
22429d62771SThomas Huth static void vfio_pci_igd_register_types(void)
22529d62771SThomas Huth {
22629d62771SThomas Huth     type_register_static(&vfio_pci_igd_lpc_bridge_info);
22729d62771SThomas Huth }
22829d62771SThomas Huth 
22929d62771SThomas Huth type_init(vfio_pci_igd_register_types)
23029d62771SThomas Huth 
23129d62771SThomas Huth static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
23229d62771SThomas Huth                                  struct vfio_region_info *info)
23329d62771SThomas Huth {
23429d62771SThomas Huth     PCIDevice *lpc_bridge;
23529d62771SThomas Huth     int ret;
23629d62771SThomas Huth 
23729d62771SThomas Huth     lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
23829d62771SThomas Huth                                  0, PCI_DEVFN(0x1f, 0));
23929d62771SThomas Huth     if (!lpc_bridge) {
24029d62771SThomas Huth         lpc_bridge = pci_create_simple(pci_device_root_bus(&vdev->pdev),
24129d62771SThomas Huth                                  PCI_DEVFN(0x1f, 0), "vfio-pci-igd-lpc-bridge");
24229d62771SThomas Huth     }
24329d62771SThomas Huth 
24429d62771SThomas Huth     ret = vfio_pci_igd_copy(vdev, lpc_bridge, info, igd_lpc_bridge_infos,
24529d62771SThomas Huth                             ARRAY_SIZE(igd_lpc_bridge_infos));
24629d62771SThomas Huth     if (!ret) {
24729d62771SThomas Huth         trace_vfio_pci_igd_lpc_bridge_enabled(vdev->vbasedev.name);
24829d62771SThomas Huth     }
24929d62771SThomas Huth 
25029d62771SThomas Huth     return ret;
25129d62771SThomas Huth }
25229d62771SThomas Huth 
25329d62771SThomas Huth /*
25429d62771SThomas Huth  * IGD Gen8 and newer support up to 8MB for the GTT and use a 64bit PTE
25529d62771SThomas Huth  * entry, older IGDs use 2MB and 32bit.  Each PTE maps a 4k page.  Therefore
25629d62771SThomas Huth  * we either have 2M/4k * 4 = 2k or 8M/4k * 8 = 16k as the maximum iobar index
25729d62771SThomas Huth  * for programming the GTT.
25829d62771SThomas Huth  *
25929d62771SThomas Huth  * See linux:include/drm/i915_drm.h for shift and mask values.
26029d62771SThomas Huth  */
26129d62771SThomas Huth static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
26229d62771SThomas Huth {
26329d62771SThomas Huth     uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
26429d62771SThomas Huth     int ggms, gen = igd_gen(vdev);
26529d62771SThomas Huth 
26629d62771SThomas Huth     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
26729d62771SThomas Huth     ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
26829d62771SThomas Huth     if (gen > 6) {
26929d62771SThomas Huth         ggms = 1 << ggms;
27029d62771SThomas Huth     }
27129d62771SThomas Huth 
27229d62771SThomas Huth     ggms *= MiB;
27329d62771SThomas Huth 
27429d62771SThomas Huth     return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
27529d62771SThomas Huth }
27629d62771SThomas Huth 
27729d62771SThomas Huth /*
27829d62771SThomas Huth  * The IGD ROM will make use of stolen memory (GGMS) for support of VESA modes.
27929d62771SThomas Huth  * Somehow the host stolen memory range is used for this, but how the ROM gets
28029d62771SThomas Huth  * it is a mystery, perhaps it's hardcoded into the ROM.  Thankfully though, it
28129d62771SThomas Huth  * reprograms the GTT through the IOBAR where we can trap it and transpose the
28229d62771SThomas Huth  * programming to the VM allocated buffer.  That buffer gets reserved by the VM
28329d62771SThomas Huth  * firmware via the fw_cfg entry added below.  Here we're just monitoring the
28429d62771SThomas Huth  * IOBAR address and data registers to detect a write sequence targeting the
28529d62771SThomas Huth  * GTTADR.  This code is developed by observed behavior and doesn't have a
28629d62771SThomas Huth  * direct spec reference, unfortunately.
28729d62771SThomas Huth  */
28829d62771SThomas Huth static uint64_t vfio_igd_quirk_data_read(void *opaque,
28929d62771SThomas Huth                                          hwaddr addr, unsigned size)
29029d62771SThomas Huth {
29129d62771SThomas Huth     VFIOIGDQuirk *igd = opaque;
29229d62771SThomas Huth     VFIOPCIDevice *vdev = igd->vdev;
29329d62771SThomas Huth 
29429d62771SThomas Huth     igd->index = ~0;
29529d62771SThomas Huth 
29629d62771SThomas Huth     return vfio_region_read(&vdev->bars[4].region, addr + 4, size);
29729d62771SThomas Huth }
29829d62771SThomas Huth 
29929d62771SThomas Huth static void vfio_igd_quirk_data_write(void *opaque, hwaddr addr,
30029d62771SThomas Huth                                       uint64_t data, unsigned size)
30129d62771SThomas Huth {
30229d62771SThomas Huth     VFIOIGDQuirk *igd = opaque;
30329d62771SThomas Huth     VFIOPCIDevice *vdev = igd->vdev;
30429d62771SThomas Huth     uint64_t val = data;
30529d62771SThomas Huth     int gen = igd_gen(vdev);
30629d62771SThomas Huth 
30729d62771SThomas Huth     /*
30829d62771SThomas Huth      * Programming the GGMS starts at index 0x1 and uses every 4th index (ie.
30929d62771SThomas Huth      * 0x1, 0x5, 0x9, 0xd,...).  For pre-Gen8 each 4-byte write is a whole PTE
31029d62771SThomas Huth      * entry, with 0th bit enable set.  For Gen8 and up, PTEs are 64bit, so
31129d62771SThomas Huth      * entries 0x5 & 0xd are the high dword, in our case zero.  Each PTE points
31229d62771SThomas Huth      * to a 4k page, which we translate to a page from the VM allocated region,
31329d62771SThomas Huth      * pointed to by the BDSM register.  If this is not set, we fail.
31429d62771SThomas Huth      *
31529d62771SThomas Huth      * We trap writes to the full configured GTT size, but we typically only
31629d62771SThomas Huth      * see the vBIOS writing up to (nearly) the 1MB barrier.  In fact it often
31729d62771SThomas Huth      * seems to miss the last entry for an even 1MB GTT.  Doing a gratuitous
31829d62771SThomas Huth      * write of that last entry does work, but is hopefully unnecessary since
31929d62771SThomas Huth      * we clear the previous GTT on initialization.
32029d62771SThomas Huth      */
32129d62771SThomas Huth     if ((igd->index % 4 == 1) && igd->index < vfio_igd_gtt_max(vdev)) {
32229d62771SThomas Huth         if (gen < 8 || (igd->index % 8 == 1)) {
3237bafcd17SCorvin Köhne             uint64_t base;
32429d62771SThomas Huth 
3257bafcd17SCorvin Köhne             if (gen < 11) {
32629d62771SThomas Huth                 base = pci_get_long(vdev->pdev.config + IGD_BDSM);
3277bafcd17SCorvin Köhne             } else {
3287bafcd17SCorvin Köhne                 base = pci_get_quad(vdev->pdev.config + IGD_BDSM_GEN11);
3297bafcd17SCorvin Köhne             }
33029d62771SThomas Huth             if (!base) {
33129d62771SThomas Huth                 hw_error("vfio-igd: Guest attempted to program IGD GTT before "
33229d62771SThomas Huth                          "BIOS reserved stolen memory.  Unsupported BIOS?");
33329d62771SThomas Huth             }
33429d62771SThomas Huth 
33529d62771SThomas Huth             val = data - igd->bdsm + base;
33629d62771SThomas Huth         } else {
33729d62771SThomas Huth             val = 0; /* upper 32bits of pte, we only enable below 4G PTEs */
33829d62771SThomas Huth         }
33929d62771SThomas Huth 
34029d62771SThomas Huth         trace_vfio_pci_igd_bar4_write(vdev->vbasedev.name,
34129d62771SThomas Huth                                       igd->index, data, val);
34229d62771SThomas Huth     }
34329d62771SThomas Huth 
34429d62771SThomas Huth     vfio_region_write(&vdev->bars[4].region, addr + 4, val, size);
34529d62771SThomas Huth 
34629d62771SThomas Huth     igd->index = ~0;
34729d62771SThomas Huth }
34829d62771SThomas Huth 
34929d62771SThomas Huth static const MemoryRegionOps vfio_igd_data_quirk = {
35029d62771SThomas Huth     .read = vfio_igd_quirk_data_read,
35129d62771SThomas Huth     .write = vfio_igd_quirk_data_write,
35229d62771SThomas Huth     .endianness = DEVICE_LITTLE_ENDIAN,
35329d62771SThomas Huth };
35429d62771SThomas Huth 
35529d62771SThomas Huth static uint64_t vfio_igd_quirk_index_read(void *opaque,
35629d62771SThomas Huth                                           hwaddr addr, unsigned size)
35729d62771SThomas Huth {
35829d62771SThomas Huth     VFIOIGDQuirk *igd = opaque;
35929d62771SThomas Huth     VFIOPCIDevice *vdev = igd->vdev;
36029d62771SThomas Huth 
36129d62771SThomas Huth     igd->index = ~0;
36229d62771SThomas Huth 
36329d62771SThomas Huth     return vfio_region_read(&vdev->bars[4].region, addr, size);
36429d62771SThomas Huth }
36529d62771SThomas Huth 
36629d62771SThomas Huth static void vfio_igd_quirk_index_write(void *opaque, hwaddr addr,
36729d62771SThomas Huth                                        uint64_t data, unsigned size)
36829d62771SThomas Huth {
36929d62771SThomas Huth     VFIOIGDQuirk *igd = opaque;
37029d62771SThomas Huth     VFIOPCIDevice *vdev = igd->vdev;
37129d62771SThomas Huth 
37229d62771SThomas Huth     igd->index = data;
37329d62771SThomas Huth 
37429d62771SThomas Huth     vfio_region_write(&vdev->bars[4].region, addr, data, size);
37529d62771SThomas Huth }
37629d62771SThomas Huth 
37729d62771SThomas Huth static const MemoryRegionOps vfio_igd_index_quirk = {
37829d62771SThomas Huth     .read = vfio_igd_quirk_index_read,
37929d62771SThomas Huth     .write = vfio_igd_quirk_index_write,
38029d62771SThomas Huth     .endianness = DEVICE_LITTLE_ENDIAN,
38129d62771SThomas Huth };
38229d62771SThomas Huth 
38311b5ce95SCorvin Köhne #define IGD_BDSM_MMIO_OFFSET 0x1080C0
38411b5ce95SCorvin Köhne 
38511b5ce95SCorvin Köhne static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
38611b5ce95SCorvin Köhne                                           hwaddr addr, unsigned size)
38711b5ce95SCorvin Köhne {
38811b5ce95SCorvin Köhne     VFIOPCIDevice *vdev = opaque;
38911b5ce95SCorvin Köhne     uint64_t offset;
39011b5ce95SCorvin Köhne 
39111b5ce95SCorvin Köhne     offset = IGD_BDSM_GEN11 + addr;
39211b5ce95SCorvin Köhne 
39311b5ce95SCorvin Köhne     switch (size) {
39411b5ce95SCorvin Köhne     case 1:
39511b5ce95SCorvin Köhne         return pci_get_byte(vdev->pdev.config + offset);
39611b5ce95SCorvin Köhne     case 2:
39711b5ce95SCorvin Köhne         return pci_get_word(vdev->pdev.config + offset);
39811b5ce95SCorvin Köhne     case 4:
39911b5ce95SCorvin Köhne         return pci_get_long(vdev->pdev.config + offset);
40011b5ce95SCorvin Köhne     case 8:
40111b5ce95SCorvin Köhne         return pci_get_quad(vdev->pdev.config + offset);
40211b5ce95SCorvin Köhne     default:
40311b5ce95SCorvin Köhne         hw_error("igd: unsupported read size, %u bytes", size);
40411b5ce95SCorvin Köhne         break;
40511b5ce95SCorvin Köhne     }
40611b5ce95SCorvin Köhne 
40711b5ce95SCorvin Köhne     return 0;
40811b5ce95SCorvin Köhne }
40911b5ce95SCorvin Köhne 
41011b5ce95SCorvin Köhne static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
41111b5ce95SCorvin Köhne                                        uint64_t data, unsigned size)
41211b5ce95SCorvin Köhne {
41311b5ce95SCorvin Köhne     VFIOPCIDevice *vdev = opaque;
41411b5ce95SCorvin Köhne     uint64_t offset;
41511b5ce95SCorvin Köhne 
41611b5ce95SCorvin Köhne     offset = IGD_BDSM_GEN11 + addr;
41711b5ce95SCorvin Köhne 
41811b5ce95SCorvin Köhne     switch (size) {
41911b5ce95SCorvin Köhne     case 1:
42011b5ce95SCorvin Köhne         pci_set_byte(vdev->pdev.config + offset, data);
42111b5ce95SCorvin Köhne         break;
42211b5ce95SCorvin Köhne     case 2:
42311b5ce95SCorvin Köhne         pci_set_word(vdev->pdev.config + offset, data);
42411b5ce95SCorvin Köhne         break;
42511b5ce95SCorvin Köhne     case 4:
42611b5ce95SCorvin Köhne         pci_set_long(vdev->pdev.config + offset, data);
42711b5ce95SCorvin Köhne         break;
42811b5ce95SCorvin Köhne     case 8:
42911b5ce95SCorvin Köhne         pci_set_quad(vdev->pdev.config + offset, data);
43011b5ce95SCorvin Köhne         break;
43111b5ce95SCorvin Köhne     default:
43211b5ce95SCorvin Köhne         hw_error("igd: unsupported read size, %u bytes", size);
43311b5ce95SCorvin Köhne         break;
43411b5ce95SCorvin Köhne     }
43511b5ce95SCorvin Köhne }
43611b5ce95SCorvin Köhne 
43711b5ce95SCorvin Köhne static const MemoryRegionOps vfio_igd_bdsm_quirk = {
43811b5ce95SCorvin Köhne     .read = vfio_igd_quirk_bdsm_read,
43911b5ce95SCorvin Köhne     .write = vfio_igd_quirk_bdsm_write,
44011b5ce95SCorvin Köhne     .endianness = DEVICE_LITTLE_ENDIAN,
44111b5ce95SCorvin Köhne };
44211b5ce95SCorvin Köhne 
44311b5ce95SCorvin Köhne void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
44411b5ce95SCorvin Köhne {
44511b5ce95SCorvin Köhne     VFIOQuirk *quirk;
44611b5ce95SCorvin Köhne     int gen;
44711b5ce95SCorvin Köhne 
44811b5ce95SCorvin Köhne     /*
44911b5ce95SCorvin Köhne      * This must be an Intel VGA device at address 00:02.0 for us to even
45011b5ce95SCorvin Köhne      * consider enabling legacy mode. Some driver have dependencies on the PCI
45111b5ce95SCorvin Köhne      * bus address.
45211b5ce95SCorvin Köhne      */
45311b5ce95SCorvin Köhne     if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
45411b5ce95SCorvin Köhne         !vfio_is_vga(vdev) || nr != 0 ||
45511b5ce95SCorvin Köhne         &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
45611b5ce95SCorvin Köhne                                        0, PCI_DEVFN(0x2, 0))) {
45711b5ce95SCorvin Köhne         return;
45811b5ce95SCorvin Köhne     }
45911b5ce95SCorvin Köhne 
46011b5ce95SCorvin Köhne     /*
46111b5ce95SCorvin Köhne      * Only on IGD devices of gen 11 and above, the BDSM register is mirrored
46211b5ce95SCorvin Köhne      * into MMIO space and read from MMIO space by the Windows driver.
46311b5ce95SCorvin Köhne      */
46411b5ce95SCorvin Köhne     gen = igd_gen(vdev);
46511b5ce95SCorvin Köhne     if (gen < 11) {
46611b5ce95SCorvin Köhne         return;
46711b5ce95SCorvin Köhne     }
46811b5ce95SCorvin Köhne 
46911b5ce95SCorvin Köhne     quirk = vfio_quirk_alloc(1);
47011b5ce95SCorvin Köhne     quirk->data = vdev;
47111b5ce95SCorvin Köhne 
47211b5ce95SCorvin Köhne     memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_bdsm_quirk,
47311b5ce95SCorvin Köhne                           vdev, "vfio-igd-bdsm-quirk", 8);
47411b5ce95SCorvin Köhne     memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
47511b5ce95SCorvin Köhne                                         IGD_BDSM_MMIO_OFFSET, &quirk->mem[0],
47611b5ce95SCorvin Köhne                                         1);
47711b5ce95SCorvin Köhne 
47811b5ce95SCorvin Köhne     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
47911b5ce95SCorvin Köhne }
48011b5ce95SCorvin Köhne 
481971ca22fSCorvin Köhne static int igd_get_stolen_mb(int gen, uint32_t gmch)
482971ca22fSCorvin Köhne {
483971ca22fSCorvin Köhne     int gms;
484971ca22fSCorvin Köhne 
485971ca22fSCorvin Köhne     if (gen < 8) {
486971ca22fSCorvin Köhne         gms = (gmch >> 3) & 0x1f;
487971ca22fSCorvin Köhne     } else {
488971ca22fSCorvin Köhne         gms = (gmch >> 8) & 0xff;
489971ca22fSCorvin Köhne     }
490971ca22fSCorvin Köhne 
491*87192241SCorvin Köhne     if (gen < 9) {
492971ca22fSCorvin Köhne         if (gms > 0x10) {
493971ca22fSCorvin Köhne             error_report("Unsupported IGD GMS value 0x%x", gms);
494971ca22fSCorvin Köhne             return 0;
495971ca22fSCorvin Köhne         }
496971ca22fSCorvin Köhne         return gms * 32;
497*87192241SCorvin Köhne     } else {
498*87192241SCorvin Köhne         if (gms < 0xf0)
499*87192241SCorvin Köhne             return gms * 32;
500*87192241SCorvin Köhne         else
501*87192241SCorvin Köhne             return gms * 4 + 4;
502*87192241SCorvin Köhne     }
503971ca22fSCorvin Köhne }
504971ca22fSCorvin Köhne 
50529d62771SThomas Huth void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
50629d62771SThomas Huth {
5070d3e89beSZhenzhong Duan     g_autofree struct vfio_region_info *rom = NULL;
508b4e1670cSZhenzhong Duan     g_autofree struct vfio_region_info *opregion = NULL;
509b4e1670cSZhenzhong Duan     g_autofree struct vfio_region_info *host = NULL;
510b4e1670cSZhenzhong Duan     g_autofree struct vfio_region_info *lpc = NULL;
51129d62771SThomas Huth     VFIOQuirk *quirk;
51229d62771SThomas Huth     VFIOIGDQuirk *igd;
51329d62771SThomas Huth     PCIDevice *lpc_bridge;
51429d62771SThomas Huth     int i, ret, ggms_mb, gms_mb = 0, gen;
51529d62771SThomas Huth     uint64_t *bdsm_size;
51629d62771SThomas Huth     uint32_t gmch;
51729d62771SThomas Huth     uint16_t cmd_orig, cmd;
51829d62771SThomas Huth     Error *err = NULL;
51929d62771SThomas Huth 
52029d62771SThomas Huth     /*
52129d62771SThomas Huth      * This must be an Intel VGA device at address 00:02.0 for us to even
52229d62771SThomas Huth      * consider enabling legacy mode.  The vBIOS has dependencies on the
52329d62771SThomas Huth      * PCI bus address.
52429d62771SThomas Huth      */
52529d62771SThomas Huth     if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
52629d62771SThomas Huth         !vfio_is_vga(vdev) || nr != 4 ||
52729d62771SThomas Huth         &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
52829d62771SThomas Huth                                        0, PCI_DEVFN(0x2, 0))) {
52929d62771SThomas Huth         return;
53029d62771SThomas Huth     }
53129d62771SThomas Huth 
53229d62771SThomas Huth     /*
53329d62771SThomas Huth      * We need to create an LPC/ISA bridge at PCI bus address 00:1f.0 that we
53429d62771SThomas Huth      * can stuff host values into, so if there's already one there and it's not
53529d62771SThomas Huth      * one we can hack on, legacy mode is no-go.  Sorry Q35.
53629d62771SThomas Huth      */
53729d62771SThomas Huth     lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
53829d62771SThomas Huth                                  0, PCI_DEVFN(0x1f, 0));
53929d62771SThomas Huth     if (lpc_bridge && !object_dynamic_cast(OBJECT(lpc_bridge),
54029d62771SThomas Huth                                            "vfio-pci-igd-lpc-bridge")) {
54129d62771SThomas Huth         error_report("IGD device %s cannot support legacy mode due to existing "
54229d62771SThomas Huth                      "devices at address 1f.0", vdev->vbasedev.name);
54329d62771SThomas Huth         return;
54429d62771SThomas Huth     }
54529d62771SThomas Huth 
54629d62771SThomas Huth     /*
54729d62771SThomas Huth      * IGD is not a standard, they like to change their specs often.  We
54829d62771SThomas Huth      * only attempt to support back to SandBridge and we hope that newer
54929d62771SThomas Huth      * devices maintain compatibility with generation 8.
55029d62771SThomas Huth      */
55129d62771SThomas Huth     gen = igd_gen(vdev);
552abd9dda9SCorvin Köhne     if (gen == -1) {
55329d62771SThomas Huth         error_report("IGD device %s is unsupported in legacy mode, "
55429d62771SThomas Huth                      "try SandyBridge or newer", vdev->vbasedev.name);
55529d62771SThomas Huth         return;
55629d62771SThomas Huth     }
55729d62771SThomas Huth 
55829d62771SThomas Huth     /*
55929d62771SThomas Huth      * Most of what we're doing here is to enable the ROM to run, so if
56029d62771SThomas Huth      * there's no ROM, there's no point in setting up this quirk.
56129d62771SThomas Huth      * NB. We only seem to get BIOS ROMs, so a UEFI VM would need CSM support.
56229d62771SThomas Huth      */
56329d62771SThomas Huth     ret = vfio_get_region_info(&vdev->vbasedev,
56429d62771SThomas Huth                                VFIO_PCI_ROM_REGION_INDEX, &rom);
56529d62771SThomas Huth     if ((ret || !rom->size) && !vdev->pdev.romfile) {
56629d62771SThomas Huth         error_report("IGD device %s has no ROM, legacy mode disabled",
56729d62771SThomas Huth                      vdev->vbasedev.name);
568b4e1670cSZhenzhong Duan         return;
56929d62771SThomas Huth     }
57029d62771SThomas Huth 
57129d62771SThomas Huth     /*
57229d62771SThomas Huth      * Ignore the hotplug corner case, mark the ROM failed, we can't
57329d62771SThomas Huth      * create the devices we need for legacy mode in the hotplug scenario.
57429d62771SThomas Huth      */
57529d62771SThomas Huth     if (vdev->pdev.qdev.hotplugged) {
57629d62771SThomas Huth         error_report("IGD device %s hotplugged, ROM disabled, "
57729d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
57829d62771SThomas Huth         vdev->rom_read_failed = true;
579b4e1670cSZhenzhong Duan         return;
58029d62771SThomas Huth     }
58129d62771SThomas Huth 
58229d62771SThomas Huth     /*
58329d62771SThomas Huth      * Check whether we have all the vfio device specific regions to
58429d62771SThomas Huth      * support legacy mode (added in Linux v4.6).  If not, bail.
58529d62771SThomas Huth      */
58629d62771SThomas Huth     ret = vfio_get_dev_region_info(&vdev->vbasedev,
58729d62771SThomas Huth                         VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
58829d62771SThomas Huth                         VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion);
58929d62771SThomas Huth     if (ret) {
59029d62771SThomas Huth         error_report("IGD device %s does not support OpRegion access,"
59129d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
592b4e1670cSZhenzhong Duan         return;
59329d62771SThomas Huth     }
59429d62771SThomas Huth 
59529d62771SThomas Huth     ret = vfio_get_dev_region_info(&vdev->vbasedev,
59629d62771SThomas Huth                         VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
59729d62771SThomas Huth                         VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, &host);
59829d62771SThomas Huth     if (ret) {
59929d62771SThomas Huth         error_report("IGD device %s does not support host bridge access,"
60029d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
601b4e1670cSZhenzhong Duan         return;
60229d62771SThomas Huth     }
60329d62771SThomas Huth 
60429d62771SThomas Huth     ret = vfio_get_dev_region_info(&vdev->vbasedev,
60529d62771SThomas Huth                         VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
60629d62771SThomas Huth                         VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG, &lpc);
60729d62771SThomas Huth     if (ret) {
60829d62771SThomas Huth         error_report("IGD device %s does not support LPC bridge access,"
60929d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
610b4e1670cSZhenzhong Duan         return;
61129d62771SThomas Huth     }
61229d62771SThomas Huth 
61329d62771SThomas Huth     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, 4);
61429d62771SThomas Huth 
61529d62771SThomas Huth     /*
61629d62771SThomas Huth      * If IGD VGA Disable is clear (expected) and VGA is not already enabled,
61729d62771SThomas Huth      * try to enable it.  Probably shouldn't be using legacy mode without VGA,
61829d62771SThomas Huth      * but also no point in us enabling VGA if disabled in hardware.
61929d62771SThomas Huth      */
62064410a74SZhenzhong Duan     if (!(gmch & 0x2) && !vdev->vga && !vfio_populate_vga(vdev, &err)) {
62129d62771SThomas Huth         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
62229d62771SThomas Huth         error_report("IGD device %s failed to enable VGA access, "
62329d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
624b4e1670cSZhenzhong Duan         return;
62529d62771SThomas Huth     }
62629d62771SThomas Huth 
62729d62771SThomas Huth     /* Create our LPC/ISA bridge */
62829d62771SThomas Huth     ret = vfio_pci_igd_lpc_init(vdev, lpc);
62929d62771SThomas Huth     if (ret) {
63029d62771SThomas Huth         error_report("IGD device %s failed to create LPC bridge, "
63129d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
632b4e1670cSZhenzhong Duan         return;
63329d62771SThomas Huth     }
63429d62771SThomas Huth 
63529d62771SThomas Huth     /* Stuff some host values into the VM PCI host bridge */
63629d62771SThomas Huth     ret = vfio_pci_igd_host_init(vdev, host);
63729d62771SThomas Huth     if (ret) {
63829d62771SThomas Huth         error_report("IGD device %s failed to modify host bridge, "
63929d62771SThomas Huth                      "legacy mode disabled", vdev->vbasedev.name);
640b4e1670cSZhenzhong Duan         return;
64129d62771SThomas Huth     }
64229d62771SThomas Huth 
64329d62771SThomas Huth     /* Setup OpRegion access */
644d3c6a18bSZhenzhong Duan     if (!vfio_pci_igd_opregion_init(vdev, opregion, &err)) {
64529d62771SThomas Huth         error_append_hint(&err, "IGD legacy mode disabled\n");
64629d62771SThomas Huth         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
647b4e1670cSZhenzhong Duan         return;
64829d62771SThomas Huth     }
64929d62771SThomas Huth 
65029d62771SThomas Huth     /* Setup our quirk to munge GTT addresses to the VM allocated buffer */
65129d62771SThomas Huth     quirk = vfio_quirk_alloc(2);
65229d62771SThomas Huth     igd = quirk->data = g_malloc0(sizeof(*igd));
65329d62771SThomas Huth     igd->vdev = vdev;
65429d62771SThomas Huth     igd->index = ~0;
6557bafcd17SCorvin Köhne     if (gen < 11) {
65629d62771SThomas Huth         igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM, 4);
6577bafcd17SCorvin Köhne     } else {
6587bafcd17SCorvin Köhne         igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM_GEN11, 4);
6597bafcd17SCorvin Köhne         igd->bdsm |=
6607bafcd17SCorvin Köhne             (uint64_t)vfio_pci_read_config(&vdev->pdev, IGD_BDSM_GEN11 + 4, 4) << 32;
6617bafcd17SCorvin Köhne     }
66229d62771SThomas Huth     igd->bdsm &= ~((1 * MiB) - 1); /* 1MB aligned */
66329d62771SThomas Huth 
66429d62771SThomas Huth     memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_index_quirk,
66529d62771SThomas Huth                           igd, "vfio-igd-index-quirk", 4);
66629d62771SThomas Huth     memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
66729d62771SThomas Huth                                         0, &quirk->mem[0], 1);
66829d62771SThomas Huth 
66929d62771SThomas Huth     memory_region_init_io(&quirk->mem[1], OBJECT(vdev), &vfio_igd_data_quirk,
67029d62771SThomas Huth                           igd, "vfio-igd-data-quirk", 4);
67129d62771SThomas Huth     memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
67229d62771SThomas Huth                                         4, &quirk->mem[1], 1);
67329d62771SThomas Huth 
67429d62771SThomas Huth     QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
67529d62771SThomas Huth 
67629d62771SThomas Huth     /* Determine the size of stolen memory needed for GTT */
67729d62771SThomas Huth     ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
67829d62771SThomas Huth     if (gen > 6) {
67929d62771SThomas Huth         ggms_mb = 1 << ggms_mb;
68029d62771SThomas Huth     }
68129d62771SThomas Huth 
682971ca22fSCorvin Köhne     gms_mb = igd_get_stolen_mb(gen, gmch);
68329d62771SThomas Huth 
68429d62771SThomas Huth     /*
68529d62771SThomas Huth      * Request reserved memory for stolen memory via fw_cfg.  VM firmware
68629d62771SThomas Huth      * must allocate a 1MB aligned reserved memory region below 4GB with
68729d62771SThomas Huth      * the requested size (in bytes) for use by the Intel PCI class VGA
68829d62771SThomas Huth      * device at VM address 00:02.0.  The base address of this reserved
689631ba5a1SCai Huoqing      * memory region must be written to the device BDSM register at PCI
69029d62771SThomas Huth      * config offset 0x5C.
69129d62771SThomas Huth      */
69229d62771SThomas Huth     bdsm_size = g_malloc(sizeof(*bdsm_size));
69329d62771SThomas Huth     *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
69429d62771SThomas Huth     fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
69529d62771SThomas Huth                     bdsm_size, sizeof(*bdsm_size));
69629d62771SThomas Huth 
69729d62771SThomas Huth     /* GMCH is read-only, emulated */
69829d62771SThomas Huth     pci_set_long(vdev->pdev.config + IGD_GMCH, gmch);
69929d62771SThomas Huth     pci_set_long(vdev->pdev.wmask + IGD_GMCH, 0);
70029d62771SThomas Huth     pci_set_long(vdev->emulated_config_bits + IGD_GMCH, ~0);
70129d62771SThomas Huth 
70229d62771SThomas Huth     /* BDSM is read-write, emulated.  The BIOS needs to be able to write it */
7037bafcd17SCorvin Köhne     if (gen < 11) {
70429d62771SThomas Huth         pci_set_long(vdev->pdev.config + IGD_BDSM, 0);
70529d62771SThomas Huth         pci_set_long(vdev->pdev.wmask + IGD_BDSM, ~0);
70629d62771SThomas Huth         pci_set_long(vdev->emulated_config_bits + IGD_BDSM, ~0);
7077bafcd17SCorvin Köhne     } else {
7087bafcd17SCorvin Köhne         pci_set_quad(vdev->pdev.config + IGD_BDSM_GEN11, 0);
7097bafcd17SCorvin Köhne         pci_set_quad(vdev->pdev.wmask + IGD_BDSM_GEN11, ~0);
7107bafcd17SCorvin Köhne         pci_set_quad(vdev->emulated_config_bits + IGD_BDSM_GEN11, ~0);
7117bafcd17SCorvin Köhne     }
71229d62771SThomas Huth 
71329d62771SThomas Huth     /*
71429d62771SThomas Huth      * This IOBAR gives us access to GTTADR, which allows us to write to
71529d62771SThomas Huth      * the GTT itself.  So let's go ahead and write zero to all the GTT
71629d62771SThomas Huth      * entries to avoid spurious DMA faults.  Be sure I/O access is enabled
71729d62771SThomas Huth      * before talking to the device.
71829d62771SThomas Huth      */
71929d62771SThomas Huth     if (pread(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
72029d62771SThomas Huth               vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
72129d62771SThomas Huth         error_report("IGD device %s - failed to read PCI command register",
72229d62771SThomas Huth                      vdev->vbasedev.name);
72329d62771SThomas Huth     }
72429d62771SThomas Huth 
72529d62771SThomas Huth     cmd = cmd_orig | PCI_COMMAND_IO;
72629d62771SThomas Huth 
72729d62771SThomas Huth     if (pwrite(vdev->vbasedev.fd, &cmd, sizeof(cmd),
72829d62771SThomas Huth                vdev->config_offset + PCI_COMMAND) != sizeof(cmd)) {
72929d62771SThomas Huth         error_report("IGD device %s - failed to write PCI command register",
73029d62771SThomas Huth                      vdev->vbasedev.name);
73129d62771SThomas Huth     }
73229d62771SThomas Huth 
73329d62771SThomas Huth     for (i = 1; i < vfio_igd_gtt_max(vdev); i += 4) {
73429d62771SThomas Huth         vfio_region_write(&vdev->bars[4].region, 0, i, 4);
73529d62771SThomas Huth         vfio_region_write(&vdev->bars[4].region, 4, 0, 4);
73629d62771SThomas Huth     }
73729d62771SThomas Huth 
73829d62771SThomas Huth     if (pwrite(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
73929d62771SThomas Huth                vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
74029d62771SThomas Huth         error_report("IGD device %s - failed to restore PCI command register",
74129d62771SThomas Huth                      vdev->vbasedev.name);
74229d62771SThomas Huth     }
74329d62771SThomas Huth 
74429d62771SThomas Huth     trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
74529d62771SThomas Huth }
746