1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fb9aa6f1SThomas Gleixner /*
3fb9aa6f1SThomas Gleixner * mmconfig-shared.c - Low-level direct PCI config space access via
4fb9aa6f1SThomas Gleixner * MMCONFIG - common code between i386 and x86-64.
5fb9aa6f1SThomas Gleixner *
6fb9aa6f1SThomas Gleixner * This code does:
7fb9aa6f1SThomas Gleixner * - known chipset handling
8fb9aa6f1SThomas Gleixner * - ACPI decoding and validation
9fb9aa6f1SThomas Gleixner *
10fb9aa6f1SThomas Gleixner * Per-architecture code takes care of the mappings and accesses
11fb9aa6f1SThomas Gleixner * themselves.
12fb9aa6f1SThomas Gleixner */
13fb9aa6f1SThomas Gleixner
144590d98fSAndy Shevchenko #include <linux/acpi.h>
15fd3a8cffSBjorn Helgaas #include <linux/efi.h>
16fb9aa6f1SThomas Gleixner #include <linux/pci.h>
17fb9aa6f1SThomas Gleixner #include <linux/init.h>
18fb9aa6f1SThomas Gleixner #include <linux/bitmap.h>
199a08f7d3SBjorn Helgaas #include <linux/dmi.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21376f70acSJiang Liu #include <linux/mutex.h>
22376f70acSJiang Liu #include <linux/rculist.h>
2366441bd3SIngo Molnar #include <asm/e820/api.h>
2482487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
255f0db7a2SFeng Tang #include <asm/acpi.h>
26fb9aa6f1SThomas Gleixner
27f4a2d584SLen Brown #define PREFIX "PCI: "
28a192a958SLen Brown
29fb9aa6f1SThomas Gleixner /* Indicate if the mmcfg resources have been placed into the resource table. */
3095c5e92fSJiang Liu static bool pci_mmcfg_running_state;
319c95111bSJiang Liu static bool pci_mmcfg_arch_init_failed;
32376f70acSJiang Liu static DEFINE_MUTEX(pci_mmcfg_lock);
33842a56cfSJoel Fernandes (Google) #define pci_mmcfg_lock_held() lock_is_held(&(pci_mmcfg_lock).dep_map)
34fb9aa6f1SThomas Gleixner
35ff097dddSBjorn Helgaas LIST_HEAD(pci_mmcfg_list);
36ff097dddSBjorn Helgaas
pci_mmconfig_remove(struct pci_mmcfg_region * cfg)3764474b52SMathias Krause static void __init pci_mmconfig_remove(struct pci_mmcfg_region *cfg)
387da7d360SBjorn Helgaas {
3956ddf4d3SBjorn Helgaas if (cfg->res.parent)
4056ddf4d3SBjorn Helgaas release_resource(&cfg->res);
41ff097dddSBjorn Helgaas list_del(&cfg->list);
42ff097dddSBjorn Helgaas kfree(cfg);
4356ddf4d3SBjorn Helgaas }
44ba2afbabSBjorn Helgaas
free_all_mmcfg(void)4564474b52SMathias Krause static void __init free_all_mmcfg(void)
46ba2afbabSBjorn Helgaas {
47ba2afbabSBjorn Helgaas struct pci_mmcfg_region *cfg, *tmp;
48ba2afbabSBjorn Helgaas
49ba2afbabSBjorn Helgaas pci_mmcfg_arch_free();
50ba2afbabSBjorn Helgaas list_for_each_entry_safe(cfg, tmp, &pci_mmcfg_list, list)
51ba2afbabSBjorn Helgaas pci_mmconfig_remove(cfg);
52ff097dddSBjorn Helgaas }
53ff097dddSBjorn Helgaas
list_add_sorted(struct pci_mmcfg_region * new)54a18e3690SGreg Kroah-Hartman static void list_add_sorted(struct pci_mmcfg_region *new)
55ff097dddSBjorn Helgaas {
56ff097dddSBjorn Helgaas struct pci_mmcfg_region *cfg;
57ff097dddSBjorn Helgaas
58ff097dddSBjorn Helgaas /* keep list sorted by segment and starting bus number */
59842a56cfSJoel Fernandes (Google) list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held()) {
60ff097dddSBjorn Helgaas if (cfg->segment > new->segment ||
61ff097dddSBjorn Helgaas (cfg->segment == new->segment &&
62ff097dddSBjorn Helgaas cfg->start_bus >= new->start_bus)) {
63376f70acSJiang Liu list_add_tail_rcu(&new->list, &cfg->list);
64ff097dddSBjorn Helgaas return;
65ff097dddSBjorn Helgaas }
66ff097dddSBjorn Helgaas }
67376f70acSJiang Liu list_add_tail_rcu(&new->list, &pci_mmcfg_list);
687da7d360SBjorn Helgaas }
697da7d360SBjorn Helgaas
pci_mmconfig_alloc(int segment,int start,int end,u64 addr)70a18e3690SGreg Kroah-Hartman static struct pci_mmcfg_region *pci_mmconfig_alloc(int segment, int start,
71d215a9c8SBjorn Helgaas int end, u64 addr)
72068258bcSYinghai Lu {
73d215a9c8SBjorn Helgaas struct pci_mmcfg_region *new;
7456ddf4d3SBjorn Helgaas struct resource *res;
75068258bcSYinghai Lu
76f7ca6984SBjorn Helgaas if (addr == 0)
77f7ca6984SBjorn Helgaas return NULL;
78f7ca6984SBjorn Helgaas
79ff097dddSBjorn Helgaas new = kzalloc(sizeof(*new), GFP_KERNEL);
80068258bcSYinghai Lu if (!new)
817da7d360SBjorn Helgaas return NULL;
82068258bcSYinghai Lu
8395cf1cf0SBjorn Helgaas new->address = addr;
8495cf1cf0SBjorn Helgaas new->segment = segment;
8595cf1cf0SBjorn Helgaas new->start_bus = start;
8695cf1cf0SBjorn Helgaas new->end_bus = end;
877da7d360SBjorn Helgaas
8856ddf4d3SBjorn Helgaas res = &new->res;
8956ddf4d3SBjorn Helgaas res->start = addr + PCI_MMCFG_BUS_OFFSET(start);
901ca98fa6SBjorn Helgaas res->end = addr + PCI_MMCFG_BUS_OFFSET(end + 1) - 1;
9156ddf4d3SBjorn Helgaas res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
9256ddf4d3SBjorn Helgaas snprintf(new->name, PCI_MMCFG_RESOURCE_NAME_LEN,
9356ddf4d3SBjorn Helgaas "PCI MMCONFIG %04x [bus %02x-%02x]", segment, start, end);
9456ddf4d3SBjorn Helgaas res->name = new->name;
9556ddf4d3SBjorn Helgaas
96ff097dddSBjorn Helgaas return new;
97068258bcSYinghai Lu }
98068258bcSYinghai Lu
pci_mmconfig_add(int segment,int start,int end,u64 addr)996fa4a94eSOtavio Pontes struct pci_mmcfg_region *__init pci_mmconfig_add(int segment, int start,
100846e4023SJiang Liu int end, u64 addr)
101846e4023SJiang Liu {
102846e4023SJiang Liu struct pci_mmcfg_region *new;
103846e4023SJiang Liu
104846e4023SJiang Liu new = pci_mmconfig_alloc(segment, start, end, addr);
105376f70acSJiang Liu if (new) {
106376f70acSJiang Liu mutex_lock(&pci_mmcfg_lock);
107846e4023SJiang Liu list_add_sorted(new);
108376f70acSJiang Liu mutex_unlock(&pci_mmcfg_lock);
1099c95111bSJiang Liu
11024c97f04SJiang Liu pr_info(PREFIX
1119c95111bSJiang Liu "MMCONFIG for domain %04x [bus %02x-%02x] at %pR "
1129c95111bSJiang Liu "(base %#lx)\n",
1139c95111bSJiang Liu segment, start, end, &new->res, (unsigned long)addr);
114376f70acSJiang Liu }
115846e4023SJiang Liu
116846e4023SJiang Liu return new;
117846e4023SJiang Liu }
118846e4023SJiang Liu
pci_mmconfig_lookup(int segment,int bus)119f6e1d8ccSBjorn Helgaas struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus)
120f6e1d8ccSBjorn Helgaas {
121f6e1d8ccSBjorn Helgaas struct pci_mmcfg_region *cfg;
122f6e1d8ccSBjorn Helgaas
123842a56cfSJoel Fernandes (Google) list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held())
124f6e1d8ccSBjorn Helgaas if (cfg->segment == segment &&
125f6e1d8ccSBjorn Helgaas cfg->start_bus <= bus && bus <= cfg->end_bus)
126f6e1d8ccSBjorn Helgaas return cfg;
127f6e1d8ccSBjorn Helgaas
128f6e1d8ccSBjorn Helgaas return NULL;
129f6e1d8ccSBjorn Helgaas }
130f6e1d8ccSBjorn Helgaas
pci_mmcfg_e7520(void)13164474b52SMathias Krause static const char *__init pci_mmcfg_e7520(void)
132fb9aa6f1SThomas Gleixner {
133fb9aa6f1SThomas Gleixner u32 win;
134bb63b421SYinghai Lu raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
135fb9aa6f1SThomas Gleixner
136fb9aa6f1SThomas Gleixner win = win & 0xf000;
137fb9aa6f1SThomas Gleixner if (win == 0x0000 || win == 0xf000)
138fb9aa6f1SThomas Gleixner return NULL;
139068258bcSYinghai Lu
1407da7d360SBjorn Helgaas if (pci_mmconfig_add(0, 0, 255, win << 16) == NULL)
141068258bcSYinghai Lu return NULL;
142068258bcSYinghai Lu
143fb9aa6f1SThomas Gleixner return "Intel Corporation E7520 Memory Controller Hub";
144fb9aa6f1SThomas Gleixner }
145fb9aa6f1SThomas Gleixner
pci_mmcfg_intel_945(void)14664474b52SMathias Krause static const char *__init pci_mmcfg_intel_945(void)
147fb9aa6f1SThomas Gleixner {
148fb9aa6f1SThomas Gleixner u32 pciexbar, mask = 0, len = 0;
149fb9aa6f1SThomas Gleixner
150bb63b421SYinghai Lu raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
151fb9aa6f1SThomas Gleixner
152fb9aa6f1SThomas Gleixner /* Enable bit */
153fb9aa6f1SThomas Gleixner if (!(pciexbar & 1))
154068258bcSYinghai Lu return NULL;
155fb9aa6f1SThomas Gleixner
156fb9aa6f1SThomas Gleixner /* Size bits */
157fb9aa6f1SThomas Gleixner switch ((pciexbar >> 1) & 3) {
158fb9aa6f1SThomas Gleixner case 0:
159fb9aa6f1SThomas Gleixner mask = 0xf0000000U;
160fb9aa6f1SThomas Gleixner len = 0x10000000U;
161fb9aa6f1SThomas Gleixner break;
162fb9aa6f1SThomas Gleixner case 1:
163fb9aa6f1SThomas Gleixner mask = 0xf8000000U;
164fb9aa6f1SThomas Gleixner len = 0x08000000U;
165fb9aa6f1SThomas Gleixner break;
166fb9aa6f1SThomas Gleixner case 2:
167fb9aa6f1SThomas Gleixner mask = 0xfc000000U;
168fb9aa6f1SThomas Gleixner len = 0x04000000U;
169fb9aa6f1SThomas Gleixner break;
170fb9aa6f1SThomas Gleixner default:
171068258bcSYinghai Lu return NULL;
172fb9aa6f1SThomas Gleixner }
173fb9aa6f1SThomas Gleixner
174fb9aa6f1SThomas Gleixner /* Errata #2, things break when not aligned on a 256Mb boundary */
175fb9aa6f1SThomas Gleixner /* Can only happen in 64M/128M mode */
176fb9aa6f1SThomas Gleixner
177fb9aa6f1SThomas Gleixner if ((pciexbar & mask) & 0x0fffffffU)
178068258bcSYinghai Lu return NULL;
179fb9aa6f1SThomas Gleixner
180fb9aa6f1SThomas Gleixner /* Don't hit the APIC registers and their friends */
181fb9aa6f1SThomas Gleixner if ((pciexbar & mask) >= 0xf0000000U)
182fb9aa6f1SThomas Gleixner return NULL;
183068258bcSYinghai Lu
1847da7d360SBjorn Helgaas if (pci_mmconfig_add(0, 0, (len >> 20) - 1, pciexbar & mask) == NULL)
185068258bcSYinghai Lu return NULL;
186068258bcSYinghai Lu
187fb9aa6f1SThomas Gleixner return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
188fb9aa6f1SThomas Gleixner }
189fb9aa6f1SThomas Gleixner
pci_mmcfg_amd_fam10h(void)19064474b52SMathias Krause static const char *__init pci_mmcfg_amd_fam10h(void)
1917fd0da40SYinghai Lu {
1927fd0da40SYinghai Lu u32 low, high, address;
1937fd0da40SYinghai Lu u64 base, msr;
1947fd0da40SYinghai Lu int i;
1957da7d360SBjorn Helgaas unsigned segnbits = 0, busnbits, end_bus;
1967fd0da40SYinghai Lu
1975f0b2976SYinghai Lu if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
1985f0b2976SYinghai Lu return NULL;
1995f0b2976SYinghai Lu
2007fd0da40SYinghai Lu address = MSR_FAM10H_MMIO_CONF_BASE;
2017fd0da40SYinghai Lu if (rdmsr_safe(address, &low, &high))
2027fd0da40SYinghai Lu return NULL;
2037fd0da40SYinghai Lu
2047fd0da40SYinghai Lu msr = high;
2057fd0da40SYinghai Lu msr <<= 32;
2067fd0da40SYinghai Lu msr |= low;
2077fd0da40SYinghai Lu
2087fd0da40SYinghai Lu /* mmconfig is not enable */
2097fd0da40SYinghai Lu if (!(msr & FAM10H_MMIO_CONF_ENABLE))
2107fd0da40SYinghai Lu return NULL;
2117fd0da40SYinghai Lu
2127fd0da40SYinghai Lu base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
2137fd0da40SYinghai Lu
2147fd0da40SYinghai Lu busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
2157fd0da40SYinghai Lu FAM10H_MMIO_CONF_BUSRANGE_MASK;
2167fd0da40SYinghai Lu
2177fd0da40SYinghai Lu /*
2187fd0da40SYinghai Lu * only handle bus 0 ?
2197fd0da40SYinghai Lu * need to skip it
2207fd0da40SYinghai Lu */
2217fd0da40SYinghai Lu if (!busnbits)
2227fd0da40SYinghai Lu return NULL;
2237fd0da40SYinghai Lu
2247fd0da40SYinghai Lu if (busnbits > 8) {
2257fd0da40SYinghai Lu segnbits = busnbits - 8;
2267fd0da40SYinghai Lu busnbits = 8;
2277fd0da40SYinghai Lu }
2287fd0da40SYinghai Lu
2297da7d360SBjorn Helgaas end_bus = (1 << busnbits) - 1;
230068258bcSYinghai Lu for (i = 0; i < (1 << segnbits); i++)
2317da7d360SBjorn Helgaas if (pci_mmconfig_add(i, 0, end_bus,
2327da7d360SBjorn Helgaas base + (1<<28) * i) == NULL) {
2337da7d360SBjorn Helgaas free_all_mmcfg();
2347da7d360SBjorn Helgaas return NULL;
2357da7d360SBjorn Helgaas }
2367fd0da40SYinghai Lu
2377fd0da40SYinghai Lu return "AMD Family 10h NB";
2387fd0da40SYinghai Lu }
2397fd0da40SYinghai Lu
2405546d6f5SEd Swierk static bool __initdata mcp55_checked;
pci_mmcfg_nvidia_mcp55(void)24164474b52SMathias Krause static const char *__init pci_mmcfg_nvidia_mcp55(void)
2425546d6f5SEd Swierk {
2435546d6f5SEd Swierk int bus;
2445546d6f5SEd Swierk int mcp55_mmconf_found = 0;
2455546d6f5SEd Swierk
246776f7ad6SMathias Krause static const u32 extcfg_regnum __initconst = 0x90;
247776f7ad6SMathias Krause static const u32 extcfg_regsize __initconst = 4;
248776f7ad6SMathias Krause static const u32 extcfg_enable_mask __initconst = 1 << 31;
249776f7ad6SMathias Krause static const u32 extcfg_start_mask __initconst = 0xff << 16;
250776f7ad6SMathias Krause static const int extcfg_start_shift __initconst = 16;
251776f7ad6SMathias Krause static const u32 extcfg_size_mask __initconst = 0x3 << 28;
252776f7ad6SMathias Krause static const int extcfg_size_shift __initconst = 28;
253776f7ad6SMathias Krause static const int extcfg_sizebus[] __initconst = {
254776f7ad6SMathias Krause 0x100, 0x80, 0x40, 0x20
255776f7ad6SMathias Krause };
256776f7ad6SMathias Krause static const u32 extcfg_base_mask[] __initconst = {
257776f7ad6SMathias Krause 0x7ff8, 0x7ffc, 0x7ffe, 0x7fff
258776f7ad6SMathias Krause };
259776f7ad6SMathias Krause static const int extcfg_base_lshift __initconst = 25;
2605546d6f5SEd Swierk
2615546d6f5SEd Swierk /*
2625546d6f5SEd Swierk * do check if amd fam10h already took over
2635546d6f5SEd Swierk */
264ff097dddSBjorn Helgaas if (!acpi_disabled || !list_empty(&pci_mmcfg_list) || mcp55_checked)
2655546d6f5SEd Swierk return NULL;
2665546d6f5SEd Swierk
2675546d6f5SEd Swierk mcp55_checked = true;
2685546d6f5SEd Swierk for (bus = 0; bus < 256; bus++) {
2695546d6f5SEd Swierk u64 base;
2705546d6f5SEd Swierk u32 l, extcfg;
2715546d6f5SEd Swierk u16 vendor, device;
2725546d6f5SEd Swierk int start, size_index, end;
2735546d6f5SEd Swierk
2745546d6f5SEd Swierk raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
2755546d6f5SEd Swierk vendor = l & 0xffff;
2765546d6f5SEd Swierk device = (l >> 16) & 0xffff;
2775546d6f5SEd Swierk
2785546d6f5SEd Swierk if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
2795546d6f5SEd Swierk continue;
2805546d6f5SEd Swierk
2815546d6f5SEd Swierk raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
2825546d6f5SEd Swierk extcfg_regsize, &extcfg);
2835546d6f5SEd Swierk
2845546d6f5SEd Swierk if (!(extcfg & extcfg_enable_mask))
2855546d6f5SEd Swierk continue;
2865546d6f5SEd Swierk
2875546d6f5SEd Swierk size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
2885546d6f5SEd Swierk base = extcfg & extcfg_base_mask[size_index];
2895546d6f5SEd Swierk /* base could > 4G */
2905546d6f5SEd Swierk base <<= extcfg_base_lshift;
2915546d6f5SEd Swierk start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
2925546d6f5SEd Swierk end = start + extcfg_sizebus[size_index] - 1;
2937da7d360SBjorn Helgaas if (pci_mmconfig_add(0, start, end, base) == NULL)
2947da7d360SBjorn Helgaas continue;
2955546d6f5SEd Swierk mcp55_mmconf_found++;
2965546d6f5SEd Swierk }
2975546d6f5SEd Swierk
2985546d6f5SEd Swierk if (!mcp55_mmconf_found)
2995546d6f5SEd Swierk return NULL;
3005546d6f5SEd Swierk
3015546d6f5SEd Swierk return "nVidia MCP55";
3025546d6f5SEd Swierk }
3035546d6f5SEd Swierk
304fb9aa6f1SThomas Gleixner struct pci_mmcfg_hostbridge_probe {
3057fd0da40SYinghai Lu u32 bus;
3067fd0da40SYinghai Lu u32 devfn;
307fb9aa6f1SThomas Gleixner u32 vendor;
308fb9aa6f1SThomas Gleixner u32 device;
309fb9aa6f1SThomas Gleixner const char *(*probe)(void);
310fb9aa6f1SThomas Gleixner };
311fb9aa6f1SThomas Gleixner
3126af13bacSMathias Krause static const struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initconst = {
3137fd0da40SYinghai Lu { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
3147fd0da40SYinghai Lu PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
3157fd0da40SYinghai Lu { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
3167fd0da40SYinghai Lu PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
3177fd0da40SYinghai Lu { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
3187fd0da40SYinghai Lu 0x1200, pci_mmcfg_amd_fam10h },
3197fd0da40SYinghai Lu { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
3207fd0da40SYinghai Lu 0x1200, pci_mmcfg_amd_fam10h },
3215546d6f5SEd Swierk { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
3225546d6f5SEd Swierk 0x0369, pci_mmcfg_nvidia_mcp55 },
323fb9aa6f1SThomas Gleixner };
324fb9aa6f1SThomas Gleixner
pci_mmcfg_check_end_bus_number(void)325068258bcSYinghai Lu static void __init pci_mmcfg_check_end_bus_number(void)
326068258bcSYinghai Lu {
327987c367bSBjorn Helgaas struct pci_mmcfg_region *cfg, *cfgx;
328068258bcSYinghai Lu
329bb8d4133SThomas Gleixner /* Fixup overlaps */
330ff097dddSBjorn Helgaas list_for_each_entry(cfg, &pci_mmcfg_list, list) {
331d7e6b66fSBjorn Helgaas if (cfg->end_bus < cfg->start_bus)
332d7e6b66fSBjorn Helgaas cfg->end_bus = 255;
333068258bcSYinghai Lu
334bb8d4133SThomas Gleixner /* Don't access the list head ! */
335bb8d4133SThomas Gleixner if (cfg->list.next == &pci_mmcfg_list)
336bb8d4133SThomas Gleixner break;
337bb8d4133SThomas Gleixner
338ff097dddSBjorn Helgaas cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
339bb8d4133SThomas Gleixner if (cfg->end_bus >= cfgx->start_bus)
340d7e6b66fSBjorn Helgaas cfg->end_bus = cfgx->start_bus - 1;
341068258bcSYinghai Lu }
342068258bcSYinghai Lu }
343068258bcSYinghai Lu
pci_mmcfg_check_hostbridge(void)344fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_check_hostbridge(void)
345fb9aa6f1SThomas Gleixner {
346fb9aa6f1SThomas Gleixner u32 l;
3477fd0da40SYinghai Lu u32 bus, devfn;
348fb9aa6f1SThomas Gleixner u16 vendor, device;
349fb9aa6f1SThomas Gleixner int i;
350fb9aa6f1SThomas Gleixner const char *name;
351fb9aa6f1SThomas Gleixner
352bb63b421SYinghai Lu if (!raw_pci_ops)
353bb63b421SYinghai Lu return 0;
354bb63b421SYinghai Lu
3557da7d360SBjorn Helgaas free_all_mmcfg();
356fb9aa6f1SThomas Gleixner
357068258bcSYinghai Lu for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
3587fd0da40SYinghai Lu bus = pci_mmcfg_probes[i].bus;
3597fd0da40SYinghai Lu devfn = pci_mmcfg_probes[i].devfn;
360bb63b421SYinghai Lu raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
3617fd0da40SYinghai Lu vendor = l & 0xffff;
3627fd0da40SYinghai Lu device = (l >> 16) & 0xffff;
3637fd0da40SYinghai Lu
364068258bcSYinghai Lu name = NULL;
365fb9aa6f1SThomas Gleixner if (pci_mmcfg_probes[i].vendor == vendor &&
366fb9aa6f1SThomas Gleixner pci_mmcfg_probes[i].device == device)
367fb9aa6f1SThomas Gleixner name = pci_mmcfg_probes[i].probe();
368068258bcSYinghai Lu
369068258bcSYinghai Lu if (name)
37024c97f04SJiang Liu pr_info(PREFIX "%s with MMCONFIG support\n", name);
371fb9aa6f1SThomas Gleixner }
372fb9aa6f1SThomas Gleixner
373068258bcSYinghai Lu /* some end_bus_number is crazy, fix it */
374068258bcSYinghai Lu pci_mmcfg_check_end_bus_number();
375fb9aa6f1SThomas Gleixner
376ff097dddSBjorn Helgaas return !list_empty(&pci_mmcfg_list);
377fb9aa6f1SThomas Gleixner }
378fb9aa6f1SThomas Gleixner
check_mcfg_resource(struct acpi_resource * res,void * data)379a18e3690SGreg Kroah-Hartman static acpi_status check_mcfg_resource(struct acpi_resource *res, void *data)
3807752d5cfSRobert Hancock {
3817752d5cfSRobert Hancock struct resource *mcfg_res = data;
3827752d5cfSRobert Hancock struct acpi_resource_address64 address;
3837752d5cfSRobert Hancock acpi_status status;
3847752d5cfSRobert Hancock
3857752d5cfSRobert Hancock if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
3867752d5cfSRobert Hancock struct acpi_resource_fixed_memory32 *fixmem32 =
3877752d5cfSRobert Hancock &res->data.fixed_memory32;
3887752d5cfSRobert Hancock if (!fixmem32)
3897752d5cfSRobert Hancock return AE_OK;
3907752d5cfSRobert Hancock if ((mcfg_res->start >= fixmem32->address) &&
39175e613cdSYinghai Lu (mcfg_res->end < (fixmem32->address +
3927752d5cfSRobert Hancock fixmem32->address_length))) {
3937752d5cfSRobert Hancock mcfg_res->flags = 1;
3947752d5cfSRobert Hancock return AE_CTRL_TERMINATE;
3957752d5cfSRobert Hancock }
3967752d5cfSRobert Hancock }
3977752d5cfSRobert Hancock if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
3987752d5cfSRobert Hancock (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
3997752d5cfSRobert Hancock return AE_OK;
4007752d5cfSRobert Hancock
4017752d5cfSRobert Hancock status = acpi_resource_to_address64(res, &address);
4027752d5cfSRobert Hancock if (ACPI_FAILURE(status) ||
403a45de93eSLv Zheng (address.address.address_length <= 0) ||
4047752d5cfSRobert Hancock (address.resource_type != ACPI_MEMORY_RANGE))
4057752d5cfSRobert Hancock return AE_OK;
4067752d5cfSRobert Hancock
407a45de93eSLv Zheng if ((mcfg_res->start >= address.address.minimum) &&
408a45de93eSLv Zheng (mcfg_res->end < (address.address.minimum + address.address.address_length))) {
4097752d5cfSRobert Hancock mcfg_res->flags = 1;
4107752d5cfSRobert Hancock return AE_CTRL_TERMINATE;
4117752d5cfSRobert Hancock }
4127752d5cfSRobert Hancock return AE_OK;
4137752d5cfSRobert Hancock }
4147752d5cfSRobert Hancock
find_mboard_resource(acpi_handle handle,u32 lvl,void * context,void ** rv)415a18e3690SGreg Kroah-Hartman static acpi_status find_mboard_resource(acpi_handle handle, u32 lvl,
4167752d5cfSRobert Hancock void *context, void **rv)
4177752d5cfSRobert Hancock {
4187752d5cfSRobert Hancock struct resource *mcfg_res = context;
4197752d5cfSRobert Hancock
4207752d5cfSRobert Hancock acpi_walk_resources(handle, METHOD_NAME__CRS,
4217752d5cfSRobert Hancock check_mcfg_resource, context);
4227752d5cfSRobert Hancock
4237752d5cfSRobert Hancock if (mcfg_res->flags)
4247752d5cfSRobert Hancock return AE_CTRL_TERMINATE;
4257752d5cfSRobert Hancock
4267752d5cfSRobert Hancock return AE_OK;
4277752d5cfSRobert Hancock }
4287752d5cfSRobert Hancock
is_acpi_reserved(u64 start,u64 end,enum e820_type not_used)42983321c33SSami Tolvanen static bool is_acpi_reserved(u64 start, u64 end, enum e820_type not_used)
4307752d5cfSRobert Hancock {
4317752d5cfSRobert Hancock struct resource mcfg_res;
4327752d5cfSRobert Hancock
4337752d5cfSRobert Hancock mcfg_res.start = start;
43475e613cdSYinghai Lu mcfg_res.end = end - 1;
4357752d5cfSRobert Hancock mcfg_res.flags = 0;
4367752d5cfSRobert Hancock
4377752d5cfSRobert Hancock acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
4387752d5cfSRobert Hancock
4397752d5cfSRobert Hancock if (!mcfg_res.flags)
4407752d5cfSRobert Hancock acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
4417752d5cfSRobert Hancock NULL);
4427752d5cfSRobert Hancock
4437752d5cfSRobert Hancock return mcfg_res.flags;
4447752d5cfSRobert Hancock }
4457752d5cfSRobert Hancock
is_efi_mmio(u64 start,u64 end,enum e820_type not_used)446fd3a8cffSBjorn Helgaas static bool is_efi_mmio(u64 start, u64 end, enum e820_type not_used)
447fd3a8cffSBjorn Helgaas {
448fd3a8cffSBjorn Helgaas #ifdef CONFIG_EFI
449fd3a8cffSBjorn Helgaas efi_memory_desc_t *md;
450fd3a8cffSBjorn Helgaas u64 size, mmio_start, mmio_end;
451fd3a8cffSBjorn Helgaas
452fd3a8cffSBjorn Helgaas for_each_efi_memory_desc(md) {
453fd3a8cffSBjorn Helgaas if (md->type == EFI_MEMORY_MAPPED_IO) {
454fd3a8cffSBjorn Helgaas size = md->num_pages << EFI_PAGE_SHIFT;
455fd3a8cffSBjorn Helgaas mmio_start = md->phys_addr;
456fd3a8cffSBjorn Helgaas mmio_end = mmio_start + size;
457fd3a8cffSBjorn Helgaas
458fd3a8cffSBjorn Helgaas /*
459fd3a8cffSBjorn Helgaas * N.B. Caller supplies (start, start + size),
460fd3a8cffSBjorn Helgaas * so to match, mmio_end is the first address
461fd3a8cffSBjorn Helgaas * *past* the EFI_MEMORY_MAPPED_IO area.
462fd3a8cffSBjorn Helgaas */
463fd3a8cffSBjorn Helgaas if (mmio_start <= start && end <= mmio_end)
464fd3a8cffSBjorn Helgaas return true;
465fd3a8cffSBjorn Helgaas }
466fd3a8cffSBjorn Helgaas }
467fd3a8cffSBjorn Helgaas #endif
468fd3a8cffSBjorn Helgaas
469fd3a8cffSBjorn Helgaas return false;
470fd3a8cffSBjorn Helgaas }
471fd3a8cffSBjorn Helgaas
47283321c33SSami Tolvanen typedef bool (*check_reserved_t)(u64 start, u64 end, enum e820_type type);
473a83fe32fSYinghai Lu
is_mmconf_reserved(check_reserved_t is_reserved,struct pci_mmcfg_region * cfg,struct device * dev,const char * method)47481b3e090SIngo Molnar static bool __ref is_mmconf_reserved(check_reserved_t is_reserved,
47595c5e92fSJiang Liu struct pci_mmcfg_region *cfg,
476a48fe637SBjorn Helgaas struct device *dev, const char *method)
477a83fe32fSYinghai Lu {
4782f2a8b9cSBjorn Helgaas u64 addr = cfg->res.start;
4792f2a8b9cSBjorn Helgaas u64 size = resource_size(&cfg->res);
480a83fe32fSYinghai Lu u64 old_size = size;
48195c5e92fSJiang Liu int num_buses;
482a83fe32fSYinghai Lu
48309821ff1SIngo Molnar while (!is_reserved(addr, addr + size, E820_TYPE_RESERVED)) {
484a83fe32fSYinghai Lu size >>= 1;
485a83fe32fSYinghai Lu if (size < (16UL<<20))
486a83fe32fSYinghai Lu break;
487a83fe32fSYinghai Lu }
488a83fe32fSYinghai Lu
48995c5e92fSJiang Liu if (size < (16UL<<20) && size != old_size)
4900a470c84SYang Li return false;
49195c5e92fSJiang Liu
49295c5e92fSJiang Liu if (dev)
493a48fe637SBjorn Helgaas dev_info(dev, "MMCONFIG at %pR reserved as %s\n",
49495c5e92fSJiang Liu &cfg->res, method);
49595c5e92fSJiang Liu else
496a48fe637SBjorn Helgaas pr_info(PREFIX "MMCONFIG at %pR reserved as %s\n",
49795c5e92fSJiang Liu &cfg->res, method);
498a83fe32fSYinghai Lu
499a83fe32fSYinghai Lu if (old_size != size) {
500d7e6b66fSBjorn Helgaas /* update end_bus */
501d7e6b66fSBjorn Helgaas cfg->end_bus = cfg->start_bus + ((size>>20) - 1);
50256ddf4d3SBjorn Helgaas num_buses = cfg->end_bus - cfg->start_bus + 1;
50356ddf4d3SBjorn Helgaas cfg->res.end = cfg->res.start +
50456ddf4d3SBjorn Helgaas PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
50556ddf4d3SBjorn Helgaas snprintf(cfg->name, PCI_MMCFG_RESOURCE_NAME_LEN,
50656ddf4d3SBjorn Helgaas "PCI MMCONFIG %04x [bus %02x-%02x]",
50756ddf4d3SBjorn Helgaas cfg->segment, cfg->start_bus, cfg->end_bus);
50895c5e92fSJiang Liu
50995c5e92fSJiang Liu if (dev)
51095c5e92fSJiang Liu dev_info(dev,
51195c5e92fSJiang Liu "MMCONFIG "
51295c5e92fSJiang Liu "at %pR (base %#lx) (size reduced!)\n",
51395c5e92fSJiang Liu &cfg->res, (unsigned long) cfg->address);
51495c5e92fSJiang Liu else
51524c97f04SJiang Liu pr_info(PREFIX
5168c57786aSBjorn Helgaas "MMCONFIG for %04x [bus%02x-%02x] "
5178c57786aSBjorn Helgaas "at %pR (base %#lx) (size reduced!)\n",
5188c57786aSBjorn Helgaas cfg->segment, cfg->start_bus, cfg->end_bus,
5198c57786aSBjorn Helgaas &cfg->res, (unsigned long) cfg->address);
520a83fe32fSYinghai Lu }
52195c5e92fSJiang Liu
5220a470c84SYang Li return true;
523a83fe32fSYinghai Lu }
524a83fe32fSYinghai Lu
52581b3e090SIngo Molnar static bool __ref
pci_mmcfg_check_reserved(struct device * dev,struct pci_mmcfg_region * cfg,int early)52681b3e090SIngo Molnar pci_mmcfg_check_reserved(struct device *dev, struct pci_mmcfg_region *cfg, int early)
527fb9aa6f1SThomas Gleixner {
528eaf64126SBjorn Helgaas struct resource *conflict;
529eaf64126SBjorn Helgaas
530*d6873accSBjorn Helgaas if (early) {
531*d6873accSBjorn Helgaas
532*d6873accSBjorn Helgaas /*
533*d6873accSBjorn Helgaas * Don't try to do this check unless configuration type 1
534*d6873accSBjorn Helgaas * is available. How about type 2?
535*d6873accSBjorn Helgaas */
536*d6873accSBjorn Helgaas
537*d6873accSBjorn Helgaas /*
538*d6873accSBjorn Helgaas * 946f2ee5c731 ("Check that MCFG points to an e820
539*d6873accSBjorn Helgaas * reserved area") added this E820 check in 2006 to work
540*d6873accSBjorn Helgaas * around BIOS defects.
541*d6873accSBjorn Helgaas *
542*d6873accSBjorn Helgaas * Per PCI Firmware r3.3, sec 4.1.2, ECAM space must be
543*d6873accSBjorn Helgaas * reserved by a PNP0C02 resource, but it need not be
544*d6873accSBjorn Helgaas * mentioned in E820. Before the ACPI interpreter is
545*d6873accSBjorn Helgaas * available, we can't check for PNP0C02 resources, so
546*d6873accSBjorn Helgaas * there's no reliable way to verify the region in this
547*d6873accSBjorn Helgaas * early check. Keep it only for the old machines that
548*d6873accSBjorn Helgaas * motivated 946f2ee5c731.
549*d6873accSBjorn Helgaas */
550*d6873accSBjorn Helgaas if (dmi_get_bios_year() < 2016 && raw_pci_ops)
551*d6873accSBjorn Helgaas return is_mmconf_reserved(e820__mapped_all, cfg, dev,
552*d6873accSBjorn Helgaas "E820 entry");
553*d6873accSBjorn Helgaas
554*d6873accSBjorn Helgaas return true;
555*d6873accSBjorn Helgaas }
556*d6873accSBjorn Helgaas
557*d6873accSBjorn Helgaas if (!acpi_disabled) {
558a48fe637SBjorn Helgaas if (is_mmconf_reserved(is_acpi_reserved, cfg, dev,
559a48fe637SBjorn Helgaas "ACPI motherboard resource"))
5600a470c84SYang Li return true;
56195c5e92fSJiang Liu
56295c5e92fSJiang Liu if (dev)
56395c5e92fSJiang Liu dev_info(dev, FW_INFO
56495c5e92fSJiang Liu "MMCONFIG at %pR not reserved in "
56595c5e92fSJiang Liu "ACPI motherboard resources\n",
56695c5e92fSJiang Liu &cfg->res);
567a02ce953SFeng Tang else
56824c97f04SJiang Liu pr_info(FW_INFO PREFIX
5698c57786aSBjorn Helgaas "MMCONFIG at %pR not reserved in "
570a02ce953SFeng Tang "ACPI motherboard resources\n",
571a02ce953SFeng Tang &cfg->res);
572fd3a8cffSBjorn Helgaas
573fd3a8cffSBjorn Helgaas if (is_mmconf_reserved(is_efi_mmio, cfg, dev,
574eaf64126SBjorn Helgaas "EfiMemoryMappedIO")) {
575eaf64126SBjorn Helgaas conflict = insert_resource_conflict(&iomem_resource,
576eaf64126SBjorn Helgaas &cfg->res);
577eaf64126SBjorn Helgaas if (conflict)
578eaf64126SBjorn Helgaas pr_warn("MMCONFIG %pR conflicts with %s %pR\n",
579eaf64126SBjorn Helgaas &cfg->res, conflict->name, conflict);
580eaf64126SBjorn Helgaas else
581eaf64126SBjorn Helgaas pr_info("MMCONFIG %pR reserved to work around lack of ACPI motherboard _CRS\n",
582eaf64126SBjorn Helgaas &cfg->res);
583fd3a8cffSBjorn Helgaas return true;
584a02ce953SFeng Tang }
585eaf64126SBjorn Helgaas }
586a83fe32fSYinghai Lu
58795c5e92fSJiang Liu /*
5883bce64f0SIngo Molnar * e820__mapped_all() is marked as __init.
58995c5e92fSJiang Liu * All entries from ACPI MCFG table have been checked at boot time.
59095c5e92fSJiang Liu * For MCFG information constructed from hotpluggable host bridge's
59195c5e92fSJiang Liu * _CBA method, just assume it's reserved.
59295c5e92fSJiang Liu */
593*d6873accSBjorn Helgaas return pci_mmcfg_running_state;
5947752d5cfSRobert Hancock }
5957752d5cfSRobert Hancock
pci_mmcfg_reject_broken(int early)5962a76c450SJiang Liu static void __init pci_mmcfg_reject_broken(int early)
5972a76c450SJiang Liu {
5982a76c450SJiang Liu struct pci_mmcfg_region *cfg;
599fb9aa6f1SThomas Gleixner
6002a76c450SJiang Liu list_for_each_entry(cfg, &pci_mmcfg_list, list) {
60195c5e92fSJiang Liu if (pci_mmcfg_check_reserved(NULL, cfg, early) == 0) {
60224c97f04SJiang Liu pr_info(PREFIX "not using MMCONFIG\n");
6037da7d360SBjorn Helgaas free_all_mmcfg();
6042a76c450SJiang Liu return;
6052a76c450SJiang Liu }
6062a76c450SJiang Liu }
607fb9aa6f1SThomas Gleixner }
608fb9aa6f1SThomas Gleixner
acpi_mcfg_check_entry(struct acpi_table_mcfg * mcfg,struct acpi_mcfg_allocation * cfg)6099a08f7d3SBjorn Helgaas static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
6109a08f7d3SBjorn Helgaas struct acpi_mcfg_allocation *cfg)
611c4bf2f37SLen Brown {
6129a08f7d3SBjorn Helgaas if (cfg->address < 0xFFFFFFFF)
613c4bf2f37SLen Brown return 0;
6149a08f7d3SBjorn Helgaas
615526018bcSMike Travis if (!strncmp(mcfg->header.oem_id, "SGI", 3))
6169a08f7d3SBjorn Helgaas return 0;
6179a08f7d3SBjorn Helgaas
61869c42d49SAndy Shevchenko if ((mcfg->header.revision >= 1) && (dmi_get_bios_year() >= 2010))
6199a08f7d3SBjorn Helgaas return 0;
6209a08f7d3SBjorn Helgaas
62124c97f04SJiang Liu pr_err(PREFIX "MCFG region for %04x [bus %02x-%02x] at %#llx "
6229a08f7d3SBjorn Helgaas "is above 4GB, ignored\n", cfg->pci_segment,
6239a08f7d3SBjorn Helgaas cfg->start_bus_number, cfg->end_bus_number, cfg->address);
6249a08f7d3SBjorn Helgaas return -EINVAL;
625c4bf2f37SLen Brown }
626c4bf2f37SLen Brown
pci_parse_mcfg(struct acpi_table_header * header)627c4bf2f37SLen Brown static int __init pci_parse_mcfg(struct acpi_table_header *header)
628c4bf2f37SLen Brown {
629c4bf2f37SLen Brown struct acpi_table_mcfg *mcfg;
630d3578ef7SBjorn Helgaas struct acpi_mcfg_allocation *cfg_table, *cfg;
631c4bf2f37SLen Brown unsigned long i;
6327da7d360SBjorn Helgaas int entries;
633c4bf2f37SLen Brown
634c4bf2f37SLen Brown if (!header)
635c4bf2f37SLen Brown return -EINVAL;
636c4bf2f37SLen Brown
637c4bf2f37SLen Brown mcfg = (struct acpi_table_mcfg *)header;
638c4bf2f37SLen Brown
639c4bf2f37SLen Brown /* how many config structures do we have */
6407da7d360SBjorn Helgaas free_all_mmcfg();
641e823d6ffSBjorn Helgaas entries = 0;
642c4bf2f37SLen Brown i = header->length - sizeof(struct acpi_table_mcfg);
643c4bf2f37SLen Brown while (i >= sizeof(struct acpi_mcfg_allocation)) {
644e823d6ffSBjorn Helgaas entries++;
645c4bf2f37SLen Brown i -= sizeof(struct acpi_mcfg_allocation);
6464b8073e4SPeter Senna Tschudin }
647e823d6ffSBjorn Helgaas if (entries == 0) {
64824c97f04SJiang Liu pr_err(PREFIX "MMCONFIG has no entries\n");
649c4bf2f37SLen Brown return -ENODEV;
650c4bf2f37SLen Brown }
651c4bf2f37SLen Brown
652d3578ef7SBjorn Helgaas cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
653e823d6ffSBjorn Helgaas for (i = 0; i < entries; i++) {
654d3578ef7SBjorn Helgaas cfg = &cfg_table[i];
655d3578ef7SBjorn Helgaas if (acpi_mcfg_check_entry(mcfg, cfg)) {
6567da7d360SBjorn Helgaas free_all_mmcfg();
657c4bf2f37SLen Brown return -ENODEV;
658c4bf2f37SLen Brown }
6597da7d360SBjorn Helgaas
6607da7d360SBjorn Helgaas if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
6617da7d360SBjorn Helgaas cfg->end_bus_number, cfg->address) == NULL) {
66224c97f04SJiang Liu pr_warn(PREFIX "no memory for MCFG entries\n");
6637da7d360SBjorn Helgaas free_all_mmcfg();
6647da7d360SBjorn Helgaas return -ENOMEM;
6657da7d360SBjorn Helgaas }
666c4bf2f37SLen Brown }
667c4bf2f37SLen Brown
668c4bf2f37SLen Brown return 0;
669c4bf2f37SLen Brown }
670c4bf2f37SLen Brown
671d91525ebSChen, Gong #ifdef CONFIG_ACPI_APEI
672d91525ebSChen, Gong extern int (*arch_apei_filter_addr)(int (*func)(__u64 start, __u64 size,
673d91525ebSChen, Gong void *data), void *data);
674d91525ebSChen, Gong
pci_mmcfg_for_each_region(int (* func)(__u64 start,__u64 size,void * data),void * data)675d91525ebSChen, Gong static int pci_mmcfg_for_each_region(int (*func)(__u64 start, __u64 size,
676d91525ebSChen, Gong void *data), void *data)
677d91525ebSChen, Gong {
678d91525ebSChen, Gong struct pci_mmcfg_region *cfg;
679d91525ebSChen, Gong int rc;
680d91525ebSChen, Gong
681d91525ebSChen, Gong if (list_empty(&pci_mmcfg_list))
682d91525ebSChen, Gong return 0;
683d91525ebSChen, Gong
684d91525ebSChen, Gong list_for_each_entry(cfg, &pci_mmcfg_list, list) {
685d91525ebSChen, Gong rc = func(cfg->res.start, resource_size(&cfg->res), data);
686d91525ebSChen, Gong if (rc)
687d91525ebSChen, Gong return rc;
688d91525ebSChen, Gong }
689d91525ebSChen, Gong
690d91525ebSChen, Gong return 0;
691d91525ebSChen, Gong }
692d91525ebSChen, Gong #define set_apei_filter() (arch_apei_filter_addr = pci_mmcfg_for_each_region)
693d91525ebSChen, Gong #else
694d91525ebSChen, Gong #define set_apei_filter()
695d91525ebSChen, Gong #endif
696d91525ebSChen, Gong
__pci_mmcfg_init(int early)697968cbfadSThomas Gleixner static void __init __pci_mmcfg_init(int early)
698fb9aa6f1SThomas Gleixner {
699bb63b421SYinghai Lu pci_mmcfg_reject_broken(early);
700ff097dddSBjorn Helgaas if (list_empty(&pci_mmcfg_list))
701fb9aa6f1SThomas Gleixner return;
702fb9aa6f1SThomas Gleixner
703a3170c1fSJan Beulich if (pcibios_last_bus < 0) {
704a3170c1fSJan Beulich const struct pci_mmcfg_region *cfg;
705a3170c1fSJan Beulich
706a3170c1fSJan Beulich list_for_each_entry(cfg, &pci_mmcfg_list, list) {
707a3170c1fSJan Beulich if (cfg->segment)
708a3170c1fSJan Beulich break;
709a3170c1fSJan Beulich pcibios_last_bus = cfg->end_bus;
710a3170c1fSJan Beulich }
711a3170c1fSJan Beulich }
712a3170c1fSJan Beulich
713ebd60cd6SYinghai Lu if (pci_mmcfg_arch_init())
714fb9aa6f1SThomas Gleixner pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
715ebd60cd6SYinghai Lu else {
71666e8850aSJiang Liu free_all_mmcfg();
7179c95111bSJiang Liu pci_mmcfg_arch_init_failed = true;
718fb9aa6f1SThomas Gleixner }
719fb9aa6f1SThomas Gleixner }
720fb9aa6f1SThomas Gleixner
721574a5941SJiang Liu static int __initdata known_bridge;
722574a5941SJiang Liu
pci_mmcfg_early_init(void)723bb63b421SYinghai Lu void __init pci_mmcfg_early_init(void)
72405c58b8aSYinghai Lu {
725574a5941SJiang Liu if (pci_probe & PCI_PROBE_MMCONF) {
726574a5941SJiang Liu if (pci_mmcfg_check_hostbridge())
727574a5941SJiang Liu known_bridge = 1;
728574a5941SJiang Liu else
7294590d98fSAndy Shevchenko acpi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
730bb63b421SYinghai Lu __pci_mmcfg_init(1);
731d91525ebSChen, Gong
732d91525ebSChen, Gong set_apei_filter();
73305c58b8aSYinghai Lu }
734574a5941SJiang Liu }
73505c58b8aSYinghai Lu
pci_mmcfg_late_init(void)73605c58b8aSYinghai Lu void __init pci_mmcfg_late_init(void)
73705c58b8aSYinghai Lu {
738574a5941SJiang Liu /* MMCONFIG disabled */
739574a5941SJiang Liu if ((pci_probe & PCI_PROBE_MMCONF) == 0)
740574a5941SJiang Liu return;
741574a5941SJiang Liu
742574a5941SJiang Liu if (known_bridge)
743574a5941SJiang Liu return;
744574a5941SJiang Liu
745574a5941SJiang Liu /* MMCONFIG hasn't been enabled yet, try again */
746574a5941SJiang Liu if (pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF) {
7474590d98fSAndy Shevchenko acpi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
748bb63b421SYinghai Lu __pci_mmcfg_init(0);
74905c58b8aSYinghai Lu }
750574a5941SJiang Liu }
75105c58b8aSYinghai Lu
pci_mmcfg_late_insert_resources(void)752fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_late_insert_resources(void)
753fb9aa6f1SThomas Gleixner {
75466e8850aSJiang Liu struct pci_mmcfg_region *cfg;
75566e8850aSJiang Liu
75695c5e92fSJiang Liu pci_mmcfg_running_state = true;
75795c5e92fSJiang Liu
75866e8850aSJiang Liu /* If we are not using MMCONFIG, don't insert the resources. */
75966e8850aSJiang Liu if ((pci_probe & PCI_PROBE_MMCONF) == 0)
760fb9aa6f1SThomas Gleixner return 1;
761fb9aa6f1SThomas Gleixner
762fb9aa6f1SThomas Gleixner /*
763fb9aa6f1SThomas Gleixner * Attempt to insert the mmcfg resources but not with the busy flag
764fb9aa6f1SThomas Gleixner * marked so it won't cause request errors when __request_region is
765fb9aa6f1SThomas Gleixner * called.
766fb9aa6f1SThomas Gleixner */
76766e8850aSJiang Liu list_for_each_entry(cfg, &pci_mmcfg_list, list)
76866e8850aSJiang Liu if (!cfg->res.parent)
76966e8850aSJiang Liu insert_resource(&iomem_resource, &cfg->res);
770fb9aa6f1SThomas Gleixner
771fb9aa6f1SThomas Gleixner return 0;
772fb9aa6f1SThomas Gleixner }
773fb9aa6f1SThomas Gleixner
774fb9aa6f1SThomas Gleixner /*
775fb9aa6f1SThomas Gleixner * Perform MMCONFIG resource insertion after PCI initialization to allow for
776fb9aa6f1SThomas Gleixner * misprogrammed MCFG tables that state larger sizes but actually conflict
777fb9aa6f1SThomas Gleixner * with other system resources.
778fb9aa6f1SThomas Gleixner */
779fb9aa6f1SThomas Gleixner late_initcall(pci_mmcfg_late_insert_resources);
7809c95111bSJiang Liu
7819c95111bSJiang Liu /* Add MMCFG information for host bridges */
pci_mmconfig_insert(struct device * dev,u16 seg,u8 start,u8 end,phys_addr_t addr)782a18e3690SGreg Kroah-Hartman int pci_mmconfig_insert(struct device *dev, u16 seg, u8 start, u8 end,
7839c95111bSJiang Liu phys_addr_t addr)
7849c95111bSJiang Liu {
7859c95111bSJiang Liu int rc;
7869c95111bSJiang Liu struct resource *tmp = NULL;
7879c95111bSJiang Liu struct pci_mmcfg_region *cfg;
7889c95111bSJiang Liu
7899c95111bSJiang Liu if (!(pci_probe & PCI_PROBE_MMCONF) || pci_mmcfg_arch_init_failed)
7909c95111bSJiang Liu return -ENODEV;
7919c95111bSJiang Liu
79267d470e0SBjorn Helgaas if (start > end)
7939c95111bSJiang Liu return -EINVAL;
7949c95111bSJiang Liu
7959c95111bSJiang Liu mutex_lock(&pci_mmcfg_lock);
7969c95111bSJiang Liu cfg = pci_mmconfig_lookup(seg, start);
7979c95111bSJiang Liu if (cfg) {
7989c95111bSJiang Liu if (cfg->end_bus < end)
7999c95111bSJiang Liu dev_info(dev, FW_INFO
8009c95111bSJiang Liu "MMCONFIG for "
8019c95111bSJiang Liu "domain %04x [bus %02x-%02x] "
8029c95111bSJiang Liu "only partially covers this bridge\n",
8039c95111bSJiang Liu cfg->segment, cfg->start_bus, cfg->end_bus);
8049c95111bSJiang Liu mutex_unlock(&pci_mmcfg_lock);
8059c95111bSJiang Liu return -EEXIST;
8069c95111bSJiang Liu }
8079c95111bSJiang Liu
80867d470e0SBjorn Helgaas if (!addr) {
80967d470e0SBjorn Helgaas mutex_unlock(&pci_mmcfg_lock);
81067d470e0SBjorn Helgaas return -EINVAL;
81167d470e0SBjorn Helgaas }
81267d470e0SBjorn Helgaas
8139c95111bSJiang Liu rc = -EBUSY;
8149c95111bSJiang Liu cfg = pci_mmconfig_alloc(seg, start, end, addr);
8159c95111bSJiang Liu if (cfg == NULL) {
8169c95111bSJiang Liu dev_warn(dev, "fail to add MMCONFIG (out of memory)\n");
8179c95111bSJiang Liu rc = -ENOMEM;
8189c95111bSJiang Liu } else if (!pci_mmcfg_check_reserved(dev, cfg, 0)) {
8199c95111bSJiang Liu dev_warn(dev, FW_BUG "MMCONFIG %pR isn't reserved\n",
8209c95111bSJiang Liu &cfg->res);
8219c95111bSJiang Liu } else {
8229c95111bSJiang Liu /* Insert resource if it's not in boot stage */
8239c95111bSJiang Liu if (pci_mmcfg_running_state)
8249c95111bSJiang Liu tmp = insert_resource_conflict(&iomem_resource,
8259c95111bSJiang Liu &cfg->res);
8269c95111bSJiang Liu
8279c95111bSJiang Liu if (tmp) {
8289c95111bSJiang Liu dev_warn(dev,
8299c95111bSJiang Liu "MMCONFIG %pR conflicts with "
8309c95111bSJiang Liu "%s %pR\n",
8319c95111bSJiang Liu &cfg->res, tmp->name, tmp);
8329c95111bSJiang Liu } else if (pci_mmcfg_arch_map(cfg)) {
8339c95111bSJiang Liu dev_warn(dev, "fail to map MMCONFIG %pR.\n",
8349c95111bSJiang Liu &cfg->res);
8359c95111bSJiang Liu } else {
8369c95111bSJiang Liu list_add_sorted(cfg);
8379c95111bSJiang Liu dev_info(dev, "MMCONFIG at %pR (base %#lx)\n",
8389c95111bSJiang Liu &cfg->res, (unsigned long)addr);
8399c95111bSJiang Liu cfg = NULL;
8409c95111bSJiang Liu rc = 0;
8419c95111bSJiang Liu }
8429c95111bSJiang Liu }
8439c95111bSJiang Liu
8449c95111bSJiang Liu if (cfg) {
8459c95111bSJiang Liu if (cfg->res.parent)
8469c95111bSJiang Liu release_resource(&cfg->res);
8479c95111bSJiang Liu kfree(cfg);
8489c95111bSJiang Liu }
8499c95111bSJiang Liu
8509c95111bSJiang Liu mutex_unlock(&pci_mmcfg_lock);
8519c95111bSJiang Liu
8529c95111bSJiang Liu return rc;
8539c95111bSJiang Liu }
8549c95111bSJiang Liu
8559c95111bSJiang Liu /* Delete MMCFG information for host bridges */
pci_mmconfig_delete(u16 seg,u8 start,u8 end)8569c95111bSJiang Liu int pci_mmconfig_delete(u16 seg, u8 start, u8 end)
8579c95111bSJiang Liu {
8589c95111bSJiang Liu struct pci_mmcfg_region *cfg;
8599c95111bSJiang Liu
8609c95111bSJiang Liu mutex_lock(&pci_mmcfg_lock);
8619c95111bSJiang Liu list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
8629c95111bSJiang Liu if (cfg->segment == seg && cfg->start_bus == start &&
8639c95111bSJiang Liu cfg->end_bus == end) {
8649c95111bSJiang Liu list_del_rcu(&cfg->list);
8659c95111bSJiang Liu synchronize_rcu();
8669c95111bSJiang Liu pci_mmcfg_arch_unmap(cfg);
8679c95111bSJiang Liu if (cfg->res.parent)
8689c95111bSJiang Liu release_resource(&cfg->res);
8699c95111bSJiang Liu mutex_unlock(&pci_mmcfg_lock);
8709c95111bSJiang Liu kfree(cfg);
8719c95111bSJiang Liu return 0;
8729c95111bSJiang Liu }
8739c95111bSJiang Liu mutex_unlock(&pci_mmcfg_lock);
8749c95111bSJiang Liu
8759c95111bSJiang Liu return -ENOENT;
8769c95111bSJiang Liu }
877