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 */
igd_gen(VFIOPCIDevice * vdev)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;
91aff0c39cSCorvin Köhne /* CoffeeLake */
92aff0c39cSCorvin Köhne case 0x3e00:
93aff0c39cSCorvin Köhne return 9;
949c86b9fbSCorvin Köhne /* ElkhartLake */
959c86b9fbSCorvin Köhne case 0x4500:
969c86b9fbSCorvin Köhne return 11;
979c86b9fbSCorvin Köhne /* TigerLake */
989c86b9fbSCorvin Köhne case 0x9A00:
999c86b9fbSCorvin Köhne return 12;
10029d62771SThomas Huth }
10129d62771SThomas Huth
102e433f208SCorvin Köhne /*
103e433f208SCorvin Köhne * Unfortunately, Intel changes it's specification quite often. This makes
104e433f208SCorvin Köhne * it impossible to use a suitable default value for unknown devices.
105e433f208SCorvin Köhne */
106e433f208SCorvin Köhne return -1;
10729d62771SThomas Huth }
10829d62771SThomas Huth
10929d62771SThomas Huth typedef struct VFIOIGDQuirk {
11029d62771SThomas Huth struct VFIOPCIDevice *vdev;
11129d62771SThomas Huth uint32_t index;
1127bafcd17SCorvin Köhne uint64_t bdsm;
11329d62771SThomas Huth } VFIOIGDQuirk;
11429d62771SThomas Huth
11529d62771SThomas Huth #define IGD_GMCH 0x50 /* Graphics Control Register */
11629d62771SThomas Huth #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
1177bafcd17SCorvin Köhne #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later */
11829d62771SThomas Huth
11929d62771SThomas Huth
12029d62771SThomas Huth /*
12129d62771SThomas Huth * The rather short list of registers that we copy from the host devices.
12229d62771SThomas Huth * The LPC/ISA bridge values are definitely needed to support the vBIOS, the
12329d62771SThomas Huth * host bridge values may or may not be needed depending on the guest OS.
12429d62771SThomas Huth * Since we're only munging revision and subsystem values on the host bridge,
12529d62771SThomas Huth * we don't require our own device. The LPC/ISA bridge needs to be our very
12629d62771SThomas Huth * own though.
12729d62771SThomas Huth */
12829d62771SThomas Huth typedef struct {
12929d62771SThomas Huth uint8_t offset;
13029d62771SThomas Huth uint8_t len;
13129d62771SThomas Huth } IGDHostInfo;
13229d62771SThomas Huth
13329d62771SThomas Huth static const IGDHostInfo igd_host_bridge_infos[] = {
13429d62771SThomas Huth {PCI_REVISION_ID, 2},
13529d62771SThomas Huth {PCI_SUBSYSTEM_VENDOR_ID, 2},
13629d62771SThomas Huth {PCI_SUBSYSTEM_ID, 2},
13729d62771SThomas Huth };
13829d62771SThomas Huth
13929d62771SThomas Huth static const IGDHostInfo igd_lpc_bridge_infos[] = {
14029d62771SThomas Huth {PCI_VENDOR_ID, 2},
14129d62771SThomas Huth {PCI_DEVICE_ID, 2},
14229d62771SThomas Huth {PCI_REVISION_ID, 2},
14329d62771SThomas Huth {PCI_SUBSYSTEM_VENDOR_ID, 2},
14429d62771SThomas Huth {PCI_SUBSYSTEM_ID, 2},
14529d62771SThomas Huth };
14629d62771SThomas Huth
vfio_pci_igd_copy(VFIOPCIDevice * vdev,PCIDevice * pdev,struct vfio_region_info * info,const IGDHostInfo * list,int len)14729d62771SThomas Huth static int vfio_pci_igd_copy(VFIOPCIDevice *vdev, PCIDevice *pdev,
14829d62771SThomas Huth struct vfio_region_info *info,
14929d62771SThomas Huth const IGDHostInfo *list, int len)
15029d62771SThomas Huth {
15129d62771SThomas Huth int i, ret;
15229d62771SThomas Huth
15329d62771SThomas Huth for (i = 0; i < len; i++) {
15429d62771SThomas Huth ret = pread(vdev->vbasedev.fd, pdev->config + list[i].offset,
15529d62771SThomas Huth list[i].len, info->offset + list[i].offset);
15629d62771SThomas Huth if (ret != list[i].len) {
15729d62771SThomas Huth error_report("IGD copy failed: %m");
15829d62771SThomas Huth return -errno;
15929d62771SThomas Huth }
16029d62771SThomas Huth }
16129d62771SThomas Huth
16229d62771SThomas Huth return 0;
16329d62771SThomas Huth }
16429d62771SThomas Huth
16529d62771SThomas Huth /*
16629d62771SThomas Huth * Stuff a few values into the host bridge.
16729d62771SThomas Huth */
vfio_pci_igd_host_init(VFIOPCIDevice * vdev,struct vfio_region_info * info)16829d62771SThomas Huth static int vfio_pci_igd_host_init(VFIOPCIDevice *vdev,
16929d62771SThomas Huth struct vfio_region_info *info)
17029d62771SThomas Huth {
17129d62771SThomas Huth PCIBus *bus;
17229d62771SThomas Huth PCIDevice *host_bridge;
17329d62771SThomas Huth int ret;
17429d62771SThomas Huth
17529d62771SThomas Huth bus = pci_device_root_bus(&vdev->pdev);
17629d62771SThomas Huth host_bridge = pci_find_device(bus, 0, PCI_DEVFN(0, 0));
17729d62771SThomas Huth
17829d62771SThomas Huth if (!host_bridge) {
17929d62771SThomas Huth error_report("Can't find host bridge");
18029d62771SThomas Huth return -ENODEV;
18129d62771SThomas Huth }
18229d62771SThomas Huth
18329d62771SThomas Huth ret = vfio_pci_igd_copy(vdev, host_bridge, info, igd_host_bridge_infos,
18429d62771SThomas Huth ARRAY_SIZE(igd_host_bridge_infos));
18529d62771SThomas Huth if (!ret) {
18629d62771SThomas Huth trace_vfio_pci_igd_host_bridge_enabled(vdev->vbasedev.name);
18729d62771SThomas Huth }
18829d62771SThomas Huth
18929d62771SThomas Huth return ret;
19029d62771SThomas Huth }
19129d62771SThomas Huth
19229d62771SThomas Huth /*
19329d62771SThomas Huth * IGD LPC/ISA bridge support code. The vBIOS needs this, but we can't write
19429d62771SThomas Huth * arbitrary values into just any bridge, so we must create our own. We try
19529d62771SThomas Huth * to handle if the user has created it for us, which they might want to do
19629d62771SThomas Huth * to enable multifunction so we don't occupy the whole PCI slot.
19729d62771SThomas Huth */
vfio_pci_igd_lpc_bridge_realize(PCIDevice * pdev,Error ** errp)19829d62771SThomas Huth static void vfio_pci_igd_lpc_bridge_realize(PCIDevice *pdev, Error **errp)
19929d62771SThomas Huth {
20029d62771SThomas Huth if (pdev->devfn != PCI_DEVFN(0x1f, 0)) {
20129d62771SThomas Huth error_setg(errp, "VFIO dummy ISA/LPC bridge must have address 1f.0");
20229d62771SThomas Huth }
20329d62771SThomas Huth }
20429d62771SThomas Huth
vfio_pci_igd_lpc_bridge_class_init(ObjectClass * klass,void * data)20529d62771SThomas Huth static void vfio_pci_igd_lpc_bridge_class_init(ObjectClass *klass, void *data)
20629d62771SThomas Huth {
20729d62771SThomas Huth DeviceClass *dc = DEVICE_CLASS(klass);
20829d62771SThomas Huth PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
20929d62771SThomas Huth
21029d62771SThomas Huth set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
21129d62771SThomas Huth dc->desc = "VFIO dummy ISA/LPC bridge for IGD assignment";
21229d62771SThomas Huth dc->hotpluggable = false;
21329d62771SThomas Huth k->realize = vfio_pci_igd_lpc_bridge_realize;
21429d62771SThomas Huth k->class_id = PCI_CLASS_BRIDGE_ISA;
21529d62771SThomas Huth }
21629d62771SThomas Huth
2175e78c98bSBernhard Beschow static const TypeInfo vfio_pci_igd_lpc_bridge_info = {
21829d62771SThomas Huth .name = "vfio-pci-igd-lpc-bridge",
21929d62771SThomas Huth .parent = TYPE_PCI_DEVICE,
22029d62771SThomas Huth .class_init = vfio_pci_igd_lpc_bridge_class_init,
22129d62771SThomas Huth .interfaces = (InterfaceInfo[]) {
22229d62771SThomas Huth { INTERFACE_CONVENTIONAL_PCI_DEVICE },
22329d62771SThomas Huth { },
22429d62771SThomas Huth },
22529d62771SThomas Huth };
22629d62771SThomas Huth
vfio_pci_igd_register_types(void)22729d62771SThomas Huth static void vfio_pci_igd_register_types(void)
22829d62771SThomas Huth {
22929d62771SThomas Huth type_register_static(&vfio_pci_igd_lpc_bridge_info);
23029d62771SThomas Huth }
23129d62771SThomas Huth
type_init(vfio_pci_igd_register_types)23229d62771SThomas Huth type_init(vfio_pci_igd_register_types)
23329d62771SThomas Huth
23429d62771SThomas Huth static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
23529d62771SThomas Huth struct vfio_region_info *info)
23629d62771SThomas Huth {
23729d62771SThomas Huth PCIDevice *lpc_bridge;
23829d62771SThomas Huth int ret;
23929d62771SThomas Huth
24029d62771SThomas Huth lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
24129d62771SThomas Huth 0, PCI_DEVFN(0x1f, 0));
24229d62771SThomas Huth if (!lpc_bridge) {
24329d62771SThomas Huth lpc_bridge = pci_create_simple(pci_device_root_bus(&vdev->pdev),
24429d62771SThomas Huth PCI_DEVFN(0x1f, 0), "vfio-pci-igd-lpc-bridge");
24529d62771SThomas Huth }
24629d62771SThomas Huth
24729d62771SThomas Huth ret = vfio_pci_igd_copy(vdev, lpc_bridge, info, igd_lpc_bridge_infos,
24829d62771SThomas Huth ARRAY_SIZE(igd_lpc_bridge_infos));
24929d62771SThomas Huth if (!ret) {
25029d62771SThomas Huth trace_vfio_pci_igd_lpc_bridge_enabled(vdev->vbasedev.name);
25129d62771SThomas Huth }
25229d62771SThomas Huth
25329d62771SThomas Huth return ret;
25429d62771SThomas Huth }
25529d62771SThomas Huth
25629d62771SThomas Huth /*
25729d62771SThomas Huth * IGD Gen8 and newer support up to 8MB for the GTT and use a 64bit PTE
25829d62771SThomas Huth * entry, older IGDs use 2MB and 32bit. Each PTE maps a 4k page. Therefore
25929d62771SThomas Huth * we either have 2M/4k * 4 = 2k or 8M/4k * 8 = 16k as the maximum iobar index
26029d62771SThomas Huth * for programming the GTT.
26129d62771SThomas Huth *
26229d62771SThomas Huth * See linux:include/drm/i915_drm.h for shift and mask values.
26329d62771SThomas Huth */
vfio_igd_gtt_max(VFIOPCIDevice * vdev)26429d62771SThomas Huth static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
26529d62771SThomas Huth {
26629d62771SThomas Huth uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
26729d62771SThomas Huth int ggms, gen = igd_gen(vdev);
26829d62771SThomas Huth
26929d62771SThomas Huth gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
27029d62771SThomas Huth ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
27129d62771SThomas Huth if (gen > 6) {
27229d62771SThomas Huth ggms = 1 << ggms;
27329d62771SThomas Huth }
27429d62771SThomas Huth
27529d62771SThomas Huth ggms *= MiB;
27629d62771SThomas Huth
27729d62771SThomas Huth return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
27829d62771SThomas Huth }
27929d62771SThomas Huth
28029d62771SThomas Huth /*
28129d62771SThomas Huth * The IGD ROM will make use of stolen memory (GGMS) for support of VESA modes.
28229d62771SThomas Huth * Somehow the host stolen memory range is used for this, but how the ROM gets
28329d62771SThomas Huth * it is a mystery, perhaps it's hardcoded into the ROM. Thankfully though, it
28429d62771SThomas Huth * reprograms the GTT through the IOBAR where we can trap it and transpose the
28529d62771SThomas Huth * programming to the VM allocated buffer. That buffer gets reserved by the VM
28629d62771SThomas Huth * firmware via the fw_cfg entry added below. Here we're just monitoring the
28729d62771SThomas Huth * IOBAR address and data registers to detect a write sequence targeting the
28829d62771SThomas Huth * GTTADR. This code is developed by observed behavior and doesn't have a
28929d62771SThomas Huth * direct spec reference, unfortunately.
29029d62771SThomas Huth */
vfio_igd_quirk_data_read(void * opaque,hwaddr addr,unsigned size)29129d62771SThomas Huth static uint64_t vfio_igd_quirk_data_read(void *opaque,
29229d62771SThomas Huth hwaddr addr, unsigned size)
29329d62771SThomas Huth {
29429d62771SThomas Huth VFIOIGDQuirk *igd = opaque;
29529d62771SThomas Huth VFIOPCIDevice *vdev = igd->vdev;
29629d62771SThomas Huth
29729d62771SThomas Huth igd->index = ~0;
29829d62771SThomas Huth
29929d62771SThomas Huth return vfio_region_read(&vdev->bars[4].region, addr + 4, size);
30029d62771SThomas Huth }
30129d62771SThomas Huth
vfio_igd_quirk_data_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)30229d62771SThomas Huth static void vfio_igd_quirk_data_write(void *opaque, hwaddr addr,
30329d62771SThomas Huth uint64_t data, unsigned size)
30429d62771SThomas Huth {
30529d62771SThomas Huth VFIOIGDQuirk *igd = opaque;
30629d62771SThomas Huth VFIOPCIDevice *vdev = igd->vdev;
30729d62771SThomas Huth uint64_t val = data;
30829d62771SThomas Huth int gen = igd_gen(vdev);
30929d62771SThomas Huth
31029d62771SThomas Huth /*
31129d62771SThomas Huth * Programming the GGMS starts at index 0x1 and uses every 4th index (ie.
31229d62771SThomas Huth * 0x1, 0x5, 0x9, 0xd,...). For pre-Gen8 each 4-byte write is a whole PTE
31329d62771SThomas Huth * entry, with 0th bit enable set. For Gen8 and up, PTEs are 64bit, so
31429d62771SThomas Huth * entries 0x5 & 0xd are the high dword, in our case zero. Each PTE points
31529d62771SThomas Huth * to a 4k page, which we translate to a page from the VM allocated region,
31629d62771SThomas Huth * pointed to by the BDSM register. If this is not set, we fail.
31729d62771SThomas Huth *
31829d62771SThomas Huth * We trap writes to the full configured GTT size, but we typically only
31929d62771SThomas Huth * see the vBIOS writing up to (nearly) the 1MB barrier. In fact it often
32029d62771SThomas Huth * seems to miss the last entry for an even 1MB GTT. Doing a gratuitous
32129d62771SThomas Huth * write of that last entry does work, but is hopefully unnecessary since
32229d62771SThomas Huth * we clear the previous GTT on initialization.
32329d62771SThomas Huth */
32429d62771SThomas Huth if ((igd->index % 4 == 1) && igd->index < vfio_igd_gtt_max(vdev)) {
32529d62771SThomas Huth if (gen < 8 || (igd->index % 8 == 1)) {
3267bafcd17SCorvin Köhne uint64_t base;
32729d62771SThomas Huth
3287bafcd17SCorvin Köhne if (gen < 11) {
32929d62771SThomas Huth base = pci_get_long(vdev->pdev.config + IGD_BDSM);
3307bafcd17SCorvin Köhne } else {
3317bafcd17SCorvin Köhne base = pci_get_quad(vdev->pdev.config + IGD_BDSM_GEN11);
3327bafcd17SCorvin Köhne }
33329d62771SThomas Huth if (!base) {
33429d62771SThomas Huth hw_error("vfio-igd: Guest attempted to program IGD GTT before "
33529d62771SThomas Huth "BIOS reserved stolen memory. Unsupported BIOS?");
33629d62771SThomas Huth }
33729d62771SThomas Huth
33829d62771SThomas Huth val = data - igd->bdsm + base;
33929d62771SThomas Huth } else {
34029d62771SThomas Huth val = 0; /* upper 32bits of pte, we only enable below 4G PTEs */
34129d62771SThomas Huth }
34229d62771SThomas Huth
34329d62771SThomas Huth trace_vfio_pci_igd_bar4_write(vdev->vbasedev.name,
34429d62771SThomas Huth igd->index, data, val);
34529d62771SThomas Huth }
34629d62771SThomas Huth
34729d62771SThomas Huth vfio_region_write(&vdev->bars[4].region, addr + 4, val, size);
34829d62771SThomas Huth
34929d62771SThomas Huth igd->index = ~0;
35029d62771SThomas Huth }
35129d62771SThomas Huth
35229d62771SThomas Huth static const MemoryRegionOps vfio_igd_data_quirk = {
35329d62771SThomas Huth .read = vfio_igd_quirk_data_read,
35429d62771SThomas Huth .write = vfio_igd_quirk_data_write,
35529d62771SThomas Huth .endianness = DEVICE_LITTLE_ENDIAN,
35629d62771SThomas Huth };
35729d62771SThomas Huth
vfio_igd_quirk_index_read(void * opaque,hwaddr addr,unsigned size)35829d62771SThomas Huth static uint64_t vfio_igd_quirk_index_read(void *opaque,
35929d62771SThomas Huth hwaddr addr, unsigned size)
36029d62771SThomas Huth {
36129d62771SThomas Huth VFIOIGDQuirk *igd = opaque;
36229d62771SThomas Huth VFIOPCIDevice *vdev = igd->vdev;
36329d62771SThomas Huth
36429d62771SThomas Huth igd->index = ~0;
36529d62771SThomas Huth
36629d62771SThomas Huth return vfio_region_read(&vdev->bars[4].region, addr, size);
36729d62771SThomas Huth }
36829d62771SThomas Huth
vfio_igd_quirk_index_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)36929d62771SThomas Huth static void vfio_igd_quirk_index_write(void *opaque, hwaddr addr,
37029d62771SThomas Huth uint64_t data, unsigned size)
37129d62771SThomas Huth {
37229d62771SThomas Huth VFIOIGDQuirk *igd = opaque;
37329d62771SThomas Huth VFIOPCIDevice *vdev = igd->vdev;
37429d62771SThomas Huth
37529d62771SThomas Huth igd->index = data;
37629d62771SThomas Huth
37729d62771SThomas Huth vfio_region_write(&vdev->bars[4].region, addr, data, size);
37829d62771SThomas Huth }
37929d62771SThomas Huth
38029d62771SThomas Huth static const MemoryRegionOps vfio_igd_index_quirk = {
38129d62771SThomas Huth .read = vfio_igd_quirk_index_read,
38229d62771SThomas Huth .write = vfio_igd_quirk_index_write,
38329d62771SThomas Huth .endianness = DEVICE_LITTLE_ENDIAN,
38429d62771SThomas Huth };
38529d62771SThomas Huth
38611b5ce95SCorvin Köhne #define IGD_BDSM_MMIO_OFFSET 0x1080C0
38711b5ce95SCorvin Köhne
vfio_igd_quirk_bdsm_read(void * opaque,hwaddr addr,unsigned size)38811b5ce95SCorvin Köhne static uint64_t vfio_igd_quirk_bdsm_read(void *opaque,
38911b5ce95SCorvin Köhne hwaddr addr, unsigned size)
39011b5ce95SCorvin Köhne {
39111b5ce95SCorvin Köhne VFIOPCIDevice *vdev = opaque;
39211b5ce95SCorvin Köhne uint64_t offset;
39311b5ce95SCorvin Köhne
39411b5ce95SCorvin Köhne offset = IGD_BDSM_GEN11 + addr;
39511b5ce95SCorvin Köhne
39611b5ce95SCorvin Köhne switch (size) {
39711b5ce95SCorvin Köhne case 1:
39811b5ce95SCorvin Köhne return pci_get_byte(vdev->pdev.config + offset);
39911b5ce95SCorvin Köhne case 2:
40011b5ce95SCorvin Köhne return pci_get_word(vdev->pdev.config + offset);
40111b5ce95SCorvin Köhne case 4:
40211b5ce95SCorvin Köhne return pci_get_long(vdev->pdev.config + offset);
40311b5ce95SCorvin Köhne case 8:
40411b5ce95SCorvin Köhne return pci_get_quad(vdev->pdev.config + offset);
40511b5ce95SCorvin Köhne default:
40611b5ce95SCorvin Köhne hw_error("igd: unsupported read size, %u bytes", size);
40711b5ce95SCorvin Köhne break;
40811b5ce95SCorvin Köhne }
40911b5ce95SCorvin Köhne
41011b5ce95SCorvin Köhne return 0;
41111b5ce95SCorvin Köhne }
41211b5ce95SCorvin Köhne
vfio_igd_quirk_bdsm_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)41311b5ce95SCorvin Köhne static void vfio_igd_quirk_bdsm_write(void *opaque, hwaddr addr,
41411b5ce95SCorvin Köhne uint64_t data, unsigned size)
41511b5ce95SCorvin Köhne {
41611b5ce95SCorvin Köhne VFIOPCIDevice *vdev = opaque;
41711b5ce95SCorvin Köhne uint64_t offset;
41811b5ce95SCorvin Köhne
41911b5ce95SCorvin Köhne offset = IGD_BDSM_GEN11 + addr;
42011b5ce95SCorvin Köhne
42111b5ce95SCorvin Köhne switch (size) {
42211b5ce95SCorvin Köhne case 1:
42311b5ce95SCorvin Köhne pci_set_byte(vdev->pdev.config + offset, data);
42411b5ce95SCorvin Köhne break;
42511b5ce95SCorvin Köhne case 2:
42611b5ce95SCorvin Köhne pci_set_word(vdev->pdev.config + offset, data);
42711b5ce95SCorvin Köhne break;
42811b5ce95SCorvin Köhne case 4:
42911b5ce95SCorvin Köhne pci_set_long(vdev->pdev.config + offset, data);
43011b5ce95SCorvin Köhne break;
43111b5ce95SCorvin Köhne case 8:
43211b5ce95SCorvin Köhne pci_set_quad(vdev->pdev.config + offset, data);
43311b5ce95SCorvin Köhne break;
43411b5ce95SCorvin Köhne default:
43511b5ce95SCorvin Köhne hw_error("igd: unsupported read size, %u bytes", size);
43611b5ce95SCorvin Köhne break;
43711b5ce95SCorvin Köhne }
43811b5ce95SCorvin Köhne }
43911b5ce95SCorvin Köhne
44011b5ce95SCorvin Köhne static const MemoryRegionOps vfio_igd_bdsm_quirk = {
44111b5ce95SCorvin Köhne .read = vfio_igd_quirk_bdsm_read,
44211b5ce95SCorvin Köhne .write = vfio_igd_quirk_bdsm_write,
44311b5ce95SCorvin Köhne .endianness = DEVICE_LITTLE_ENDIAN,
44411b5ce95SCorvin Köhne };
44511b5ce95SCorvin Köhne
vfio_probe_igd_bar0_quirk(VFIOPCIDevice * vdev,int nr)44611b5ce95SCorvin Köhne void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)
44711b5ce95SCorvin Köhne {
44811b5ce95SCorvin Köhne VFIOQuirk *quirk;
44911b5ce95SCorvin Köhne int gen;
45011b5ce95SCorvin Köhne
45111b5ce95SCorvin Köhne /*
45211b5ce95SCorvin Köhne * This must be an Intel VGA device at address 00:02.0 for us to even
45311b5ce95SCorvin Köhne * consider enabling legacy mode. Some driver have dependencies on the PCI
45411b5ce95SCorvin Köhne * bus address.
45511b5ce95SCorvin Köhne */
45611b5ce95SCorvin Köhne if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
45711b5ce95SCorvin Köhne !vfio_is_vga(vdev) || nr != 0 ||
45811b5ce95SCorvin Köhne &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
45911b5ce95SCorvin Köhne 0, PCI_DEVFN(0x2, 0))) {
46011b5ce95SCorvin Köhne return;
46111b5ce95SCorvin Köhne }
46211b5ce95SCorvin Köhne
46311b5ce95SCorvin Köhne /*
46411b5ce95SCorvin Köhne * Only on IGD devices of gen 11 and above, the BDSM register is mirrored
46511b5ce95SCorvin Köhne * into MMIO space and read from MMIO space by the Windows driver.
46611b5ce95SCorvin Köhne */
46711b5ce95SCorvin Köhne gen = igd_gen(vdev);
46811b5ce95SCorvin Köhne if (gen < 11) {
46911b5ce95SCorvin Köhne return;
47011b5ce95SCorvin Köhne }
47111b5ce95SCorvin Köhne
47211b5ce95SCorvin Köhne quirk = vfio_quirk_alloc(1);
47311b5ce95SCorvin Köhne quirk->data = vdev;
47411b5ce95SCorvin Köhne
47511b5ce95SCorvin Köhne memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_bdsm_quirk,
47611b5ce95SCorvin Köhne vdev, "vfio-igd-bdsm-quirk", 8);
47711b5ce95SCorvin Köhne memory_region_add_subregion_overlap(vdev->bars[0].region.mem,
47811b5ce95SCorvin Köhne IGD_BDSM_MMIO_OFFSET, &quirk->mem[0],
47911b5ce95SCorvin Köhne 1);
48011b5ce95SCorvin Köhne
48111b5ce95SCorvin Köhne QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
48211b5ce95SCorvin Köhne }
48311b5ce95SCorvin Köhne
igd_get_stolen_mb(int gen,uint32_t gmch)484971ca22fSCorvin Köhne static int igd_get_stolen_mb(int gen, uint32_t gmch)
485971ca22fSCorvin Köhne {
486971ca22fSCorvin Köhne int gms;
487971ca22fSCorvin Köhne
488971ca22fSCorvin Köhne if (gen < 8) {
489971ca22fSCorvin Köhne gms = (gmch >> 3) & 0x1f;
490971ca22fSCorvin Köhne } else {
491971ca22fSCorvin Köhne gms = (gmch >> 8) & 0xff;
492971ca22fSCorvin Köhne }
493971ca22fSCorvin Köhne
49487192241SCorvin Köhne if (gen < 9) {
495971ca22fSCorvin Köhne if (gms > 0x10) {
496971ca22fSCorvin Köhne error_report("Unsupported IGD GMS value 0x%x", gms);
497971ca22fSCorvin Köhne return 0;
498971ca22fSCorvin Köhne }
499971ca22fSCorvin Köhne return gms * 32;
50087192241SCorvin Köhne } else {
50187192241SCorvin Köhne if (gms < 0xf0)
50287192241SCorvin Köhne return gms * 32;
50387192241SCorvin Köhne else
504*66650fd0SCorvin Köhne return (gms - 0xf0) * 4 + 4;
50587192241SCorvin Köhne }
506971ca22fSCorvin Köhne }
507971ca22fSCorvin Köhne
vfio_probe_igd_bar4_quirk(VFIOPCIDevice * vdev,int nr)50829d62771SThomas Huth void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
50929d62771SThomas Huth {
5100d3e89beSZhenzhong Duan g_autofree struct vfio_region_info *rom = NULL;
511b4e1670cSZhenzhong Duan g_autofree struct vfio_region_info *opregion = NULL;
512b4e1670cSZhenzhong Duan g_autofree struct vfio_region_info *host = NULL;
513b4e1670cSZhenzhong Duan g_autofree struct vfio_region_info *lpc = NULL;
51429d62771SThomas Huth VFIOQuirk *quirk;
51529d62771SThomas Huth VFIOIGDQuirk *igd;
51629d62771SThomas Huth PCIDevice *lpc_bridge;
51729d62771SThomas Huth int i, ret, ggms_mb, gms_mb = 0, gen;
51829d62771SThomas Huth uint64_t *bdsm_size;
51929d62771SThomas Huth uint32_t gmch;
52029d62771SThomas Huth uint16_t cmd_orig, cmd;
52129d62771SThomas Huth Error *err = NULL;
52229d62771SThomas Huth
52329d62771SThomas Huth /*
52429d62771SThomas Huth * This must be an Intel VGA device at address 00:02.0 for us to even
52529d62771SThomas Huth * consider enabling legacy mode. The vBIOS has dependencies on the
52629d62771SThomas Huth * PCI bus address.
52729d62771SThomas Huth */
52829d62771SThomas Huth if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
52929d62771SThomas Huth !vfio_is_vga(vdev) || nr != 4 ||
53029d62771SThomas Huth &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
53129d62771SThomas Huth 0, PCI_DEVFN(0x2, 0))) {
53229d62771SThomas Huth return;
53329d62771SThomas Huth }
53429d62771SThomas Huth
53529d62771SThomas Huth /*
53629d62771SThomas Huth * We need to create an LPC/ISA bridge at PCI bus address 00:1f.0 that we
53729d62771SThomas Huth * can stuff host values into, so if there's already one there and it's not
53829d62771SThomas Huth * one we can hack on, legacy mode is no-go. Sorry Q35.
53929d62771SThomas Huth */
54029d62771SThomas Huth lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
54129d62771SThomas Huth 0, PCI_DEVFN(0x1f, 0));
54229d62771SThomas Huth if (lpc_bridge && !object_dynamic_cast(OBJECT(lpc_bridge),
54329d62771SThomas Huth "vfio-pci-igd-lpc-bridge")) {
54429d62771SThomas Huth error_report("IGD device %s cannot support legacy mode due to existing "
54529d62771SThomas Huth "devices at address 1f.0", vdev->vbasedev.name);
54629d62771SThomas Huth return;
54729d62771SThomas Huth }
54829d62771SThomas Huth
54929d62771SThomas Huth /*
55029d62771SThomas Huth * IGD is not a standard, they like to change their specs often. We
55129d62771SThomas Huth * only attempt to support back to SandBridge and we hope that newer
55229d62771SThomas Huth * devices maintain compatibility with generation 8.
55329d62771SThomas Huth */
55429d62771SThomas Huth gen = igd_gen(vdev);
555abd9dda9SCorvin Köhne if (gen == -1) {
55629d62771SThomas Huth error_report("IGD device %s is unsupported in legacy mode, "
55729d62771SThomas Huth "try SandyBridge or newer", vdev->vbasedev.name);
55829d62771SThomas Huth return;
55929d62771SThomas Huth }
56029d62771SThomas Huth
56129d62771SThomas Huth /*
56229d62771SThomas Huth * Most of what we're doing here is to enable the ROM to run, so if
56329d62771SThomas Huth * there's no ROM, there's no point in setting up this quirk.
56429d62771SThomas Huth * NB. We only seem to get BIOS ROMs, so a UEFI VM would need CSM support.
56529d62771SThomas Huth */
56629d62771SThomas Huth ret = vfio_get_region_info(&vdev->vbasedev,
56729d62771SThomas Huth VFIO_PCI_ROM_REGION_INDEX, &rom);
56829d62771SThomas Huth if ((ret || !rom->size) && !vdev->pdev.romfile) {
56929d62771SThomas Huth error_report("IGD device %s has no ROM, legacy mode disabled",
57029d62771SThomas Huth vdev->vbasedev.name);
571b4e1670cSZhenzhong Duan return;
57229d62771SThomas Huth }
57329d62771SThomas Huth
57429d62771SThomas Huth /*
57529d62771SThomas Huth * Ignore the hotplug corner case, mark the ROM failed, we can't
57629d62771SThomas Huth * create the devices we need for legacy mode in the hotplug scenario.
57729d62771SThomas Huth */
57829d62771SThomas Huth if (vdev->pdev.qdev.hotplugged) {
57929d62771SThomas Huth error_report("IGD device %s hotplugged, ROM disabled, "
58029d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
58129d62771SThomas Huth vdev->rom_read_failed = true;
582b4e1670cSZhenzhong Duan return;
58329d62771SThomas Huth }
58429d62771SThomas Huth
58529d62771SThomas Huth /*
58629d62771SThomas Huth * Check whether we have all the vfio device specific regions to
58729d62771SThomas Huth * support legacy mode (added in Linux v4.6). If not, bail.
58829d62771SThomas Huth */
58929d62771SThomas Huth ret = vfio_get_dev_region_info(&vdev->vbasedev,
59029d62771SThomas Huth VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
59129d62771SThomas Huth VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion);
59229d62771SThomas Huth if (ret) {
59329d62771SThomas Huth error_report("IGD device %s does not support OpRegion access,"
59429d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
595b4e1670cSZhenzhong Duan return;
59629d62771SThomas Huth }
59729d62771SThomas Huth
59829d62771SThomas Huth ret = vfio_get_dev_region_info(&vdev->vbasedev,
59929d62771SThomas Huth VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
60029d62771SThomas Huth VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, &host);
60129d62771SThomas Huth if (ret) {
60229d62771SThomas Huth error_report("IGD device %s does not support host bridge access,"
60329d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
604b4e1670cSZhenzhong Duan return;
60529d62771SThomas Huth }
60629d62771SThomas Huth
60729d62771SThomas Huth ret = vfio_get_dev_region_info(&vdev->vbasedev,
60829d62771SThomas Huth VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
60929d62771SThomas Huth VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG, &lpc);
61029d62771SThomas Huth if (ret) {
61129d62771SThomas Huth error_report("IGD device %s does not support LPC bridge access,"
61229d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
613b4e1670cSZhenzhong Duan return;
61429d62771SThomas Huth }
61529d62771SThomas Huth
61629d62771SThomas Huth gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, 4);
61729d62771SThomas Huth
61829d62771SThomas Huth /*
61929d62771SThomas Huth * If IGD VGA Disable is clear (expected) and VGA is not already enabled,
62029d62771SThomas Huth * try to enable it. Probably shouldn't be using legacy mode without VGA,
62129d62771SThomas Huth * but also no point in us enabling VGA if disabled in hardware.
62229d62771SThomas Huth */
62364410a74SZhenzhong Duan if (!(gmch & 0x2) && !vdev->vga && !vfio_populate_vga(vdev, &err)) {
62429d62771SThomas Huth error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
62529d62771SThomas Huth error_report("IGD device %s failed to enable VGA access, "
62629d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
627b4e1670cSZhenzhong Duan return;
62829d62771SThomas Huth }
62929d62771SThomas Huth
63029d62771SThomas Huth /* Create our LPC/ISA bridge */
63129d62771SThomas Huth ret = vfio_pci_igd_lpc_init(vdev, lpc);
63229d62771SThomas Huth if (ret) {
63329d62771SThomas Huth error_report("IGD device %s failed to create LPC bridge, "
63429d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
635b4e1670cSZhenzhong Duan return;
63629d62771SThomas Huth }
63729d62771SThomas Huth
63829d62771SThomas Huth /* Stuff some host values into the VM PCI host bridge */
63929d62771SThomas Huth ret = vfio_pci_igd_host_init(vdev, host);
64029d62771SThomas Huth if (ret) {
64129d62771SThomas Huth error_report("IGD device %s failed to modify host bridge, "
64229d62771SThomas Huth "legacy mode disabled", vdev->vbasedev.name);
643b4e1670cSZhenzhong Duan return;
64429d62771SThomas Huth }
64529d62771SThomas Huth
64629d62771SThomas Huth /* Setup OpRegion access */
647d3c6a18bSZhenzhong Duan if (!vfio_pci_igd_opregion_init(vdev, opregion, &err)) {
64829d62771SThomas Huth error_append_hint(&err, "IGD legacy mode disabled\n");
64929d62771SThomas Huth error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
650b4e1670cSZhenzhong Duan return;
65129d62771SThomas Huth }
65229d62771SThomas Huth
65329d62771SThomas Huth /* Setup our quirk to munge GTT addresses to the VM allocated buffer */
65429d62771SThomas Huth quirk = vfio_quirk_alloc(2);
65529d62771SThomas Huth igd = quirk->data = g_malloc0(sizeof(*igd));
65629d62771SThomas Huth igd->vdev = vdev;
65729d62771SThomas Huth igd->index = ~0;
6587bafcd17SCorvin Köhne if (gen < 11) {
65929d62771SThomas Huth igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM, 4);
6607bafcd17SCorvin Köhne } else {
6617bafcd17SCorvin Köhne igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM_GEN11, 4);
6627bafcd17SCorvin Köhne igd->bdsm |=
6637bafcd17SCorvin Köhne (uint64_t)vfio_pci_read_config(&vdev->pdev, IGD_BDSM_GEN11 + 4, 4) << 32;
6647bafcd17SCorvin Köhne }
66529d62771SThomas Huth igd->bdsm &= ~((1 * MiB) - 1); /* 1MB aligned */
66629d62771SThomas Huth
66729d62771SThomas Huth memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_index_quirk,
66829d62771SThomas Huth igd, "vfio-igd-index-quirk", 4);
66929d62771SThomas Huth memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
67029d62771SThomas Huth 0, &quirk->mem[0], 1);
67129d62771SThomas Huth
67229d62771SThomas Huth memory_region_init_io(&quirk->mem[1], OBJECT(vdev), &vfio_igd_data_quirk,
67329d62771SThomas Huth igd, "vfio-igd-data-quirk", 4);
67429d62771SThomas Huth memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
67529d62771SThomas Huth 4, &quirk->mem[1], 1);
67629d62771SThomas Huth
67729d62771SThomas Huth QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
67829d62771SThomas Huth
67929d62771SThomas Huth /* Determine the size of stolen memory needed for GTT */
68029d62771SThomas Huth ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
68129d62771SThomas Huth if (gen > 6) {
68229d62771SThomas Huth ggms_mb = 1 << ggms_mb;
68329d62771SThomas Huth }
68429d62771SThomas Huth
685971ca22fSCorvin Köhne gms_mb = igd_get_stolen_mb(gen, gmch);
68629d62771SThomas Huth
68729d62771SThomas Huth /*
68829d62771SThomas Huth * Request reserved memory for stolen memory via fw_cfg. VM firmware
68929d62771SThomas Huth * must allocate a 1MB aligned reserved memory region below 4GB with
69029d62771SThomas Huth * the requested size (in bytes) for use by the Intel PCI class VGA
69129d62771SThomas Huth * device at VM address 00:02.0. The base address of this reserved
692631ba5a1SCai Huoqing * memory region must be written to the device BDSM register at PCI
69329d62771SThomas Huth * config offset 0x5C.
69429d62771SThomas Huth */
69529d62771SThomas Huth bdsm_size = g_malloc(sizeof(*bdsm_size));
69629d62771SThomas Huth *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
69729d62771SThomas Huth fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
69829d62771SThomas Huth bdsm_size, sizeof(*bdsm_size));
69929d62771SThomas Huth
70029d62771SThomas Huth /* GMCH is read-only, emulated */
70129d62771SThomas Huth pci_set_long(vdev->pdev.config + IGD_GMCH, gmch);
70229d62771SThomas Huth pci_set_long(vdev->pdev.wmask + IGD_GMCH, 0);
70329d62771SThomas Huth pci_set_long(vdev->emulated_config_bits + IGD_GMCH, ~0);
70429d62771SThomas Huth
70529d62771SThomas Huth /* BDSM is read-write, emulated. The BIOS needs to be able to write it */
7067bafcd17SCorvin Köhne if (gen < 11) {
70729d62771SThomas Huth pci_set_long(vdev->pdev.config + IGD_BDSM, 0);
70829d62771SThomas Huth pci_set_long(vdev->pdev.wmask + IGD_BDSM, ~0);
70929d62771SThomas Huth pci_set_long(vdev->emulated_config_bits + IGD_BDSM, ~0);
7107bafcd17SCorvin Köhne } else {
7117bafcd17SCorvin Köhne pci_set_quad(vdev->pdev.config + IGD_BDSM_GEN11, 0);
7127bafcd17SCorvin Köhne pci_set_quad(vdev->pdev.wmask + IGD_BDSM_GEN11, ~0);
7137bafcd17SCorvin Köhne pci_set_quad(vdev->emulated_config_bits + IGD_BDSM_GEN11, ~0);
7147bafcd17SCorvin Köhne }
71529d62771SThomas Huth
71629d62771SThomas Huth /*
71729d62771SThomas Huth * This IOBAR gives us access to GTTADR, which allows us to write to
71829d62771SThomas Huth * the GTT itself. So let's go ahead and write zero to all the GTT
71929d62771SThomas Huth * entries to avoid spurious DMA faults. Be sure I/O access is enabled
72029d62771SThomas Huth * before talking to the device.
72129d62771SThomas Huth */
72229d62771SThomas Huth if (pread(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
72329d62771SThomas Huth vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
72429d62771SThomas Huth error_report("IGD device %s - failed to read PCI command register",
72529d62771SThomas Huth vdev->vbasedev.name);
72629d62771SThomas Huth }
72729d62771SThomas Huth
72829d62771SThomas Huth cmd = cmd_orig | PCI_COMMAND_IO;
72929d62771SThomas Huth
73029d62771SThomas Huth if (pwrite(vdev->vbasedev.fd, &cmd, sizeof(cmd),
73129d62771SThomas Huth vdev->config_offset + PCI_COMMAND) != sizeof(cmd)) {
73229d62771SThomas Huth error_report("IGD device %s - failed to write PCI command register",
73329d62771SThomas Huth vdev->vbasedev.name);
73429d62771SThomas Huth }
73529d62771SThomas Huth
73629d62771SThomas Huth for (i = 1; i < vfio_igd_gtt_max(vdev); i += 4) {
73729d62771SThomas Huth vfio_region_write(&vdev->bars[4].region, 0, i, 4);
73829d62771SThomas Huth vfio_region_write(&vdev->bars[4].region, 4, 0, 4);
73929d62771SThomas Huth }
74029d62771SThomas Huth
74129d62771SThomas Huth if (pwrite(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
74229d62771SThomas Huth vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
74329d62771SThomas Huth error_report("IGD device %s - failed to restore PCI command register",
74429d62771SThomas Huth vdev->vbasedev.name);
74529d62771SThomas Huth }
74629d62771SThomas Huth
74729d62771SThomas Huth trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
74829d62771SThomas Huth }
749