2 * mmconfig-shared.c - Low-level direct PCI config space access via
3 * MMCONFIG - common code between i386 and x86-64.
6 * - known chipset handling
7 * - ACPI decoding and validation
9 * Per-architecture code takes care of the mappings and accesses
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/acpi.h>
16 #include <linux/bitmap.h>
21 /* aperture is up to 256MB but BIOS may reserve less */
22 #define MMCONFIG_APER_MIN (2 * 1024*1024)
23 #define MMCONFIG_APER_MAX (256 * 1024*1024)
25 /* Indicate if the mmcfg resources have been placed into the resource table. */
26 static int __initdata pci_mmcfg_resources_inserted
;
28 static const char __init
*pci_mmcfg_e7520(void)
31 pci_direct_conf1
.read(0, 0, PCI_DEVFN(0,0), 0xce, 2, &win
);
34 if(win
== 0x0000 || win
== 0xf000)
35 pci_mmcfg_config_num
= 0;
37 pci_mmcfg_config_num
= 1;
38 pci_mmcfg_config
= kzalloc(sizeof(pci_mmcfg_config
[0]), GFP_KERNEL
);
39 if (!pci_mmcfg_config
)
41 pci_mmcfg_config
[0].address
= win
<< 16;
42 pci_mmcfg_config
[0].pci_segment
= 0;
43 pci_mmcfg_config
[0].start_bus_number
= 0;
44 pci_mmcfg_config
[0].end_bus_number
= 255;
47 return "Intel Corporation E7520 Memory Controller Hub";
50 static const char __init
*pci_mmcfg_intel_945(void)
52 u32 pciexbar
, mask
= 0, len
= 0;
54 pci_mmcfg_config_num
= 1;
56 pci_direct_conf1
.read(0, 0, PCI_DEVFN(0,0), 0x48, 4, &pciexbar
);
60 pci_mmcfg_config_num
= 0;
63 switch ((pciexbar
>> 1) & 3) {
77 pci_mmcfg_config_num
= 0;
80 /* Errata #2, things break when not aligned on a 256Mb boundary */
81 /* Can only happen in 64M/128M mode */
83 if ((pciexbar
& mask
) & 0x0fffffffU
)
84 pci_mmcfg_config_num
= 0;
86 /* Don't hit the APIC registers and their friends */
87 if ((pciexbar
& mask
) >= 0xf0000000U
)
88 pci_mmcfg_config_num
= 0;
90 if (pci_mmcfg_config_num
) {
91 pci_mmcfg_config
= kzalloc(sizeof(pci_mmcfg_config
[0]), GFP_KERNEL
);
92 if (!pci_mmcfg_config
)
94 pci_mmcfg_config
[0].address
= pciexbar
& mask
;
95 pci_mmcfg_config
[0].pci_segment
= 0;
96 pci_mmcfg_config
[0].start_bus_number
= 0;
97 pci_mmcfg_config
[0].end_bus_number
= (len
>> 20) - 1;
100 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
103 struct pci_mmcfg_hostbridge_probe
{
106 const char *(*probe
)(void);
109 static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes
[] __initdata
= {
110 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_E7520_MCH
, pci_mmcfg_e7520
},
111 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82945G_HB
, pci_mmcfg_intel_945
},
114 static int __init
pci_mmcfg_check_hostbridge(void)
121 pci_direct_conf1
.read(0, 0, PCI_DEVFN(0,0), 0, 4, &l
);
123 device
= (l
>> 16) & 0xffff;
125 pci_mmcfg_config_num
= 0;
126 pci_mmcfg_config
= NULL
;
129 for (i
= 0; !name
&& i
< ARRAY_SIZE(pci_mmcfg_probes
); i
++) {
130 if (pci_mmcfg_probes
[i
].vendor
== vendor
&&
131 pci_mmcfg_probes
[i
].device
== device
)
132 name
= pci_mmcfg_probes
[i
].probe();
136 printk(KERN_INFO
"PCI: Found %s %s MMCONFIG support.\n",
137 name
, pci_mmcfg_config_num
? "with" : "without");
143 static void __init
pci_mmcfg_insert_resources(unsigned long resource_flags
)
145 #define PCI_MMCFG_RESOURCE_NAME_LEN 19
147 struct resource
*res
;
151 res
= kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN
+ sizeof(*res
),
152 pci_mmcfg_config_num
, GFP_KERNEL
);
154 printk(KERN_ERR
"PCI: Unable to allocate MMCONFIG resources\n");
158 names
= (void *)&res
[pci_mmcfg_config_num
];
159 for (i
= 0; i
< pci_mmcfg_config_num
; i
++, res
++) {
160 struct acpi_mcfg_allocation
*cfg
= &pci_mmcfg_config
[i
];
161 num_buses
= cfg
->end_bus_number
- cfg
->start_bus_number
+ 1;
163 snprintf(names
, PCI_MMCFG_RESOURCE_NAME_LEN
, "PCI MMCONFIG %u",
165 res
->start
= cfg
->address
;
166 res
->end
= res
->start
+ (num_buses
<< 20) - 1;
167 res
->flags
= IORESOURCE_MEM
| resource_flags
;
168 insert_resource(&iomem_resource
, res
);
169 names
+= PCI_MMCFG_RESOURCE_NAME_LEN
;
172 /* Mark that the resources have been inserted. */
173 pci_mmcfg_resources_inserted
= 1;
176 static void __init
pci_mmcfg_reject_broken(int type
)
178 typeof(pci_mmcfg_config
[0]) *cfg
;
180 if ((pci_mmcfg_config_num
== 0) ||
181 (pci_mmcfg_config
== NULL
) ||
182 (pci_mmcfg_config
[0].address
== 0))
185 cfg
= &pci_mmcfg_config
[0];
188 * Handle more broken MCFG tables on Asus etc.
189 * They only contain a single entry for bus 0-0.
191 if (pci_mmcfg_config_num
== 1 &&
192 cfg
->pci_segment
== 0 &&
193 (cfg
->start_bus_number
| cfg
->end_bus_number
) == 0) {
194 printk(KERN_ERR
"PCI: start and end of bus number is 0. "
195 "Rejected as broken MCFG.\n");
200 * Only do this check when type 1 works. If it doesn't work
201 * assume we run on a Mac and always use MCFG
203 if (type
== 1 && !e820_all_mapped(cfg
->address
,
204 cfg
->address
+ MMCONFIG_APER_MIN
,
206 printk(KERN_ERR
"PCI: BIOS Bug: MCFG area at %Lx is not"
207 " E820-reserved\n", cfg
->address
);
213 printk(KERN_ERR
"PCI: Not using MMCONFIG.\n");
214 kfree(pci_mmcfg_config
);
215 pci_mmcfg_config
= NULL
;
216 pci_mmcfg_config_num
= 0;
219 void __init
pci_mmcfg_init(int type
)
221 int known_bridge
= 0;
223 if ((pci_probe
& PCI_PROBE_MMCONF
) == 0)
226 if (type
== 1 && pci_mmcfg_check_hostbridge())
230 acpi_table_parse(ACPI_SIG_MCFG
, acpi_parse_mcfg
);
231 pci_mmcfg_reject_broken(type
);
234 if ((pci_mmcfg_config_num
== 0) ||
235 (pci_mmcfg_config
== NULL
) ||
236 (pci_mmcfg_config
[0].address
== 0))
239 if (pci_mmcfg_arch_init()) {
241 pci_mmcfg_insert_resources(IORESOURCE_BUSY
);
242 pci_probe
= (pci_probe
& ~PCI_PROBE_MASK
) | PCI_PROBE_MMCONF
;
245 * Signal not to attempt to insert mmcfg resources because
246 * the architecture mmcfg setup could not initialize.
248 pci_mmcfg_resources_inserted
= 1;
252 static int __init
pci_mmcfg_late_insert_resources(void)
255 * If resources are already inserted or we are not using MMCONFIG,
256 * don't insert the resources.
258 if ((pci_mmcfg_resources_inserted
== 1) ||
259 (pci_probe
& PCI_PROBE_MMCONF
) == 0 ||
260 (pci_mmcfg_config_num
== 0) ||
261 (pci_mmcfg_config
== NULL
) ||
262 (pci_mmcfg_config
[0].address
== 0))
266 * Attempt to insert the mmcfg resources but not with the busy flag
267 * marked so it won't cause request errors when __request_region is
270 pci_mmcfg_insert_resources(0);
276 * Perform MMCONFIG resource insertion after PCI initialization to allow for
277 * misprogrammed MCFG tables that state larger sizes but actually conflict
278 * with other system resources.
280 late_initcall(pci_mmcfg_late_insert_resources
);