1fb9aa6f1SThomas Gleixner /* 2fb9aa6f1SThomas Gleixner * mmconfig-shared.c - Low-level direct PCI config space access via 3fb9aa6f1SThomas Gleixner * MMCONFIG - common code between i386 and x86-64. 4fb9aa6f1SThomas Gleixner * 5fb9aa6f1SThomas Gleixner * This code does: 6fb9aa6f1SThomas Gleixner * - known chipset handling 7fb9aa6f1SThomas Gleixner * - ACPI decoding and validation 8fb9aa6f1SThomas Gleixner * 9fb9aa6f1SThomas Gleixner * Per-architecture code takes care of the mappings and accesses 10fb9aa6f1SThomas Gleixner * themselves. 11fb9aa6f1SThomas Gleixner */ 12fb9aa6f1SThomas Gleixner 13fb9aa6f1SThomas Gleixner #include <linux/pci.h> 14fb9aa6f1SThomas Gleixner #include <linux/init.h> 15fb9aa6f1SThomas Gleixner #include <linux/acpi.h> 16fb9aa6f1SThomas Gleixner #include <linux/bitmap.h> 17fb9aa6f1SThomas Gleixner #include <asm/e820.h> 18fb9aa6f1SThomas Gleixner 19fb9aa6f1SThomas Gleixner #include "pci.h" 20fb9aa6f1SThomas Gleixner 21fb9aa6f1SThomas Gleixner /* aperture is up to 256MB but BIOS may reserve less */ 22fb9aa6f1SThomas Gleixner #define MMCONFIG_APER_MIN (2 * 1024*1024) 23fb9aa6f1SThomas Gleixner #define MMCONFIG_APER_MAX (256 * 1024*1024) 24fb9aa6f1SThomas Gleixner 25fb9aa6f1SThomas Gleixner /* Indicate if the mmcfg resources have been placed into the resource table. */ 26fb9aa6f1SThomas Gleixner static int __initdata pci_mmcfg_resources_inserted; 27fb9aa6f1SThomas Gleixner 28fb9aa6f1SThomas Gleixner static const char __init *pci_mmcfg_e7520(void) 29fb9aa6f1SThomas Gleixner { 30fb9aa6f1SThomas Gleixner u32 win; 31*b6ce068aSMatthew Wilcox pci_direct_conf1.read(0, 0, PCI_DEVFN(0,0), 0xce, 2, &win); 32fb9aa6f1SThomas Gleixner 33fb9aa6f1SThomas Gleixner win = win & 0xf000; 34fb9aa6f1SThomas Gleixner if(win == 0x0000 || win == 0xf000) 35fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 36fb9aa6f1SThomas Gleixner else { 37fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 1; 38fb9aa6f1SThomas Gleixner pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL); 39fb9aa6f1SThomas Gleixner if (!pci_mmcfg_config) 40fb9aa6f1SThomas Gleixner return NULL; 41fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].address = win << 16; 42fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].pci_segment = 0; 43fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].start_bus_number = 0; 44fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].end_bus_number = 255; 45fb9aa6f1SThomas Gleixner } 46fb9aa6f1SThomas Gleixner 47fb9aa6f1SThomas Gleixner return "Intel Corporation E7520 Memory Controller Hub"; 48fb9aa6f1SThomas Gleixner } 49fb9aa6f1SThomas Gleixner 50fb9aa6f1SThomas Gleixner static const char __init *pci_mmcfg_intel_945(void) 51fb9aa6f1SThomas Gleixner { 52fb9aa6f1SThomas Gleixner u32 pciexbar, mask = 0, len = 0; 53fb9aa6f1SThomas Gleixner 54fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 1; 55fb9aa6f1SThomas Gleixner 56*b6ce068aSMatthew Wilcox pci_direct_conf1.read(0, 0, PCI_DEVFN(0,0), 0x48, 4, &pciexbar); 57fb9aa6f1SThomas Gleixner 58fb9aa6f1SThomas Gleixner /* Enable bit */ 59fb9aa6f1SThomas Gleixner if (!(pciexbar & 1)) 60fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 61fb9aa6f1SThomas Gleixner 62fb9aa6f1SThomas Gleixner /* Size bits */ 63fb9aa6f1SThomas Gleixner switch ((pciexbar >> 1) & 3) { 64fb9aa6f1SThomas Gleixner case 0: 65fb9aa6f1SThomas Gleixner mask = 0xf0000000U; 66fb9aa6f1SThomas Gleixner len = 0x10000000U; 67fb9aa6f1SThomas Gleixner break; 68fb9aa6f1SThomas Gleixner case 1: 69fb9aa6f1SThomas Gleixner mask = 0xf8000000U; 70fb9aa6f1SThomas Gleixner len = 0x08000000U; 71fb9aa6f1SThomas Gleixner break; 72fb9aa6f1SThomas Gleixner case 2: 73fb9aa6f1SThomas Gleixner mask = 0xfc000000U; 74fb9aa6f1SThomas Gleixner len = 0x04000000U; 75fb9aa6f1SThomas Gleixner break; 76fb9aa6f1SThomas Gleixner default: 77fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 78fb9aa6f1SThomas Gleixner } 79fb9aa6f1SThomas Gleixner 80fb9aa6f1SThomas Gleixner /* Errata #2, things break when not aligned on a 256Mb boundary */ 81fb9aa6f1SThomas Gleixner /* Can only happen in 64M/128M mode */ 82fb9aa6f1SThomas Gleixner 83fb9aa6f1SThomas Gleixner if ((pciexbar & mask) & 0x0fffffffU) 84fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 85fb9aa6f1SThomas Gleixner 86fb9aa6f1SThomas Gleixner /* Don't hit the APIC registers and their friends */ 87fb9aa6f1SThomas Gleixner if ((pciexbar & mask) >= 0xf0000000U) 88fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 89fb9aa6f1SThomas Gleixner 90fb9aa6f1SThomas Gleixner if (pci_mmcfg_config_num) { 91fb9aa6f1SThomas Gleixner pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL); 92fb9aa6f1SThomas Gleixner if (!pci_mmcfg_config) 93fb9aa6f1SThomas Gleixner return NULL; 94fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].address = pciexbar & mask; 95fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].pci_segment = 0; 96fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].start_bus_number = 0; 97fb9aa6f1SThomas Gleixner pci_mmcfg_config[0].end_bus_number = (len >> 20) - 1; 98fb9aa6f1SThomas Gleixner } 99fb9aa6f1SThomas Gleixner 100fb9aa6f1SThomas Gleixner return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub"; 101fb9aa6f1SThomas Gleixner } 102fb9aa6f1SThomas Gleixner 103fb9aa6f1SThomas Gleixner struct pci_mmcfg_hostbridge_probe { 104fb9aa6f1SThomas Gleixner u32 vendor; 105fb9aa6f1SThomas Gleixner u32 device; 106fb9aa6f1SThomas Gleixner const char *(*probe)(void); 107fb9aa6f1SThomas Gleixner }; 108fb9aa6f1SThomas Gleixner 109fb9aa6f1SThomas Gleixner static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = { 110fb9aa6f1SThomas Gleixner { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 }, 111fb9aa6f1SThomas Gleixner { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 }, 112fb9aa6f1SThomas Gleixner }; 113fb9aa6f1SThomas Gleixner 114fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_check_hostbridge(void) 115fb9aa6f1SThomas Gleixner { 116fb9aa6f1SThomas Gleixner u32 l; 117fb9aa6f1SThomas Gleixner u16 vendor, device; 118fb9aa6f1SThomas Gleixner int i; 119fb9aa6f1SThomas Gleixner const char *name; 120fb9aa6f1SThomas Gleixner 121*b6ce068aSMatthew Wilcox pci_direct_conf1.read(0, 0, PCI_DEVFN(0,0), 0, 4, &l); 122fb9aa6f1SThomas Gleixner vendor = l & 0xffff; 123fb9aa6f1SThomas Gleixner device = (l >> 16) & 0xffff; 124fb9aa6f1SThomas Gleixner 125fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 126fb9aa6f1SThomas Gleixner pci_mmcfg_config = NULL; 127fb9aa6f1SThomas Gleixner name = NULL; 128fb9aa6f1SThomas Gleixner 129fb9aa6f1SThomas Gleixner for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) { 130fb9aa6f1SThomas Gleixner if (pci_mmcfg_probes[i].vendor == vendor && 131fb9aa6f1SThomas Gleixner pci_mmcfg_probes[i].device == device) 132fb9aa6f1SThomas Gleixner name = pci_mmcfg_probes[i].probe(); 133fb9aa6f1SThomas Gleixner } 134fb9aa6f1SThomas Gleixner 135fb9aa6f1SThomas Gleixner if (name) { 136fb9aa6f1SThomas Gleixner printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n", 137fb9aa6f1SThomas Gleixner name, pci_mmcfg_config_num ? "with" : "without"); 138fb9aa6f1SThomas Gleixner } 139fb9aa6f1SThomas Gleixner 140fb9aa6f1SThomas Gleixner return name != NULL; 141fb9aa6f1SThomas Gleixner } 142fb9aa6f1SThomas Gleixner 143fb9aa6f1SThomas Gleixner static void __init pci_mmcfg_insert_resources(unsigned long resource_flags) 144fb9aa6f1SThomas Gleixner { 145fb9aa6f1SThomas Gleixner #define PCI_MMCFG_RESOURCE_NAME_LEN 19 146fb9aa6f1SThomas Gleixner int i; 147fb9aa6f1SThomas Gleixner struct resource *res; 148fb9aa6f1SThomas Gleixner char *names; 149fb9aa6f1SThomas Gleixner unsigned num_buses; 150fb9aa6f1SThomas Gleixner 151fb9aa6f1SThomas Gleixner res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res), 152fb9aa6f1SThomas Gleixner pci_mmcfg_config_num, GFP_KERNEL); 153fb9aa6f1SThomas Gleixner if (!res) { 154fb9aa6f1SThomas Gleixner printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n"); 155fb9aa6f1SThomas Gleixner return; 156fb9aa6f1SThomas Gleixner } 157fb9aa6f1SThomas Gleixner 158fb9aa6f1SThomas Gleixner names = (void *)&res[pci_mmcfg_config_num]; 159fb9aa6f1SThomas Gleixner for (i = 0; i < pci_mmcfg_config_num; i++, res++) { 160fb9aa6f1SThomas Gleixner struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i]; 161fb9aa6f1SThomas Gleixner num_buses = cfg->end_bus_number - cfg->start_bus_number + 1; 162fb9aa6f1SThomas Gleixner res->name = names; 163fb9aa6f1SThomas Gleixner snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u", 164fb9aa6f1SThomas Gleixner cfg->pci_segment); 165fb9aa6f1SThomas Gleixner res->start = cfg->address; 166fb9aa6f1SThomas Gleixner res->end = res->start + (num_buses << 20) - 1; 167fb9aa6f1SThomas Gleixner res->flags = IORESOURCE_MEM | resource_flags; 168fb9aa6f1SThomas Gleixner insert_resource(&iomem_resource, res); 169fb9aa6f1SThomas Gleixner names += PCI_MMCFG_RESOURCE_NAME_LEN; 170fb9aa6f1SThomas Gleixner } 171fb9aa6f1SThomas Gleixner 172fb9aa6f1SThomas Gleixner /* Mark that the resources have been inserted. */ 173fb9aa6f1SThomas Gleixner pci_mmcfg_resources_inserted = 1; 174fb9aa6f1SThomas Gleixner } 175fb9aa6f1SThomas Gleixner 176fb9aa6f1SThomas Gleixner static void __init pci_mmcfg_reject_broken(int type) 177fb9aa6f1SThomas Gleixner { 178fb9aa6f1SThomas Gleixner typeof(pci_mmcfg_config[0]) *cfg; 179fb9aa6f1SThomas Gleixner 180fb9aa6f1SThomas Gleixner if ((pci_mmcfg_config_num == 0) || 181fb9aa6f1SThomas Gleixner (pci_mmcfg_config == NULL) || 182fb9aa6f1SThomas Gleixner (pci_mmcfg_config[0].address == 0)) 183fb9aa6f1SThomas Gleixner return; 184fb9aa6f1SThomas Gleixner 185fb9aa6f1SThomas Gleixner cfg = &pci_mmcfg_config[0]; 186fb9aa6f1SThomas Gleixner 187fb9aa6f1SThomas Gleixner /* 188fb9aa6f1SThomas Gleixner * Handle more broken MCFG tables on Asus etc. 189fb9aa6f1SThomas Gleixner * They only contain a single entry for bus 0-0. 190fb9aa6f1SThomas Gleixner */ 191fb9aa6f1SThomas Gleixner if (pci_mmcfg_config_num == 1 && 192fb9aa6f1SThomas Gleixner cfg->pci_segment == 0 && 193fb9aa6f1SThomas Gleixner (cfg->start_bus_number | cfg->end_bus_number) == 0) { 194fb9aa6f1SThomas Gleixner printk(KERN_ERR "PCI: start and end of bus number is 0. " 195fb9aa6f1SThomas Gleixner "Rejected as broken MCFG.\n"); 196fb9aa6f1SThomas Gleixner goto reject; 197fb9aa6f1SThomas Gleixner } 198fb9aa6f1SThomas Gleixner 199fb9aa6f1SThomas Gleixner /* 200fb9aa6f1SThomas Gleixner * Only do this check when type 1 works. If it doesn't work 201fb9aa6f1SThomas Gleixner * assume we run on a Mac and always use MCFG 202fb9aa6f1SThomas Gleixner */ 203fb9aa6f1SThomas Gleixner if (type == 1 && !e820_all_mapped(cfg->address, 204fb9aa6f1SThomas Gleixner cfg->address + MMCONFIG_APER_MIN, 205fb9aa6f1SThomas Gleixner E820_RESERVED)) { 206fb9aa6f1SThomas Gleixner printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not" 207fb9aa6f1SThomas Gleixner " E820-reserved\n", cfg->address); 208fb9aa6f1SThomas Gleixner goto reject; 209fb9aa6f1SThomas Gleixner } 210fb9aa6f1SThomas Gleixner return; 211fb9aa6f1SThomas Gleixner 212fb9aa6f1SThomas Gleixner reject: 213fb9aa6f1SThomas Gleixner printk(KERN_ERR "PCI: Not using MMCONFIG.\n"); 214fb9aa6f1SThomas Gleixner kfree(pci_mmcfg_config); 215fb9aa6f1SThomas Gleixner pci_mmcfg_config = NULL; 216fb9aa6f1SThomas Gleixner pci_mmcfg_config_num = 0; 217fb9aa6f1SThomas Gleixner } 218fb9aa6f1SThomas Gleixner 219fb9aa6f1SThomas Gleixner void __init pci_mmcfg_init(int type) 220fb9aa6f1SThomas Gleixner { 221fb9aa6f1SThomas Gleixner int known_bridge = 0; 222fb9aa6f1SThomas Gleixner 223fb9aa6f1SThomas Gleixner if ((pci_probe & PCI_PROBE_MMCONF) == 0) 224fb9aa6f1SThomas Gleixner return; 225fb9aa6f1SThomas Gleixner 226fb9aa6f1SThomas Gleixner if (type == 1 && pci_mmcfg_check_hostbridge()) 227fb9aa6f1SThomas Gleixner known_bridge = 1; 228fb9aa6f1SThomas Gleixner 229fb9aa6f1SThomas Gleixner if (!known_bridge) { 230fb9aa6f1SThomas Gleixner acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg); 231fb9aa6f1SThomas Gleixner pci_mmcfg_reject_broken(type); 232fb9aa6f1SThomas Gleixner } 233fb9aa6f1SThomas Gleixner 234fb9aa6f1SThomas Gleixner if ((pci_mmcfg_config_num == 0) || 235fb9aa6f1SThomas Gleixner (pci_mmcfg_config == NULL) || 236fb9aa6f1SThomas Gleixner (pci_mmcfg_config[0].address == 0)) 237fb9aa6f1SThomas Gleixner return; 238fb9aa6f1SThomas Gleixner 239fb9aa6f1SThomas Gleixner if (pci_mmcfg_arch_init()) { 240fb9aa6f1SThomas Gleixner if (known_bridge) 241fb9aa6f1SThomas Gleixner pci_mmcfg_insert_resources(IORESOURCE_BUSY); 242fb9aa6f1SThomas Gleixner pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF; 243fb9aa6f1SThomas Gleixner } else { 244fb9aa6f1SThomas Gleixner /* 245fb9aa6f1SThomas Gleixner * Signal not to attempt to insert mmcfg resources because 246fb9aa6f1SThomas Gleixner * the architecture mmcfg setup could not initialize. 247fb9aa6f1SThomas Gleixner */ 248fb9aa6f1SThomas Gleixner pci_mmcfg_resources_inserted = 1; 249fb9aa6f1SThomas Gleixner } 250fb9aa6f1SThomas Gleixner } 251fb9aa6f1SThomas Gleixner 252fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_late_insert_resources(void) 253fb9aa6f1SThomas Gleixner { 254fb9aa6f1SThomas Gleixner /* 255fb9aa6f1SThomas Gleixner * If resources are already inserted or we are not using MMCONFIG, 256fb9aa6f1SThomas Gleixner * don't insert the resources. 257fb9aa6f1SThomas Gleixner */ 258fb9aa6f1SThomas Gleixner if ((pci_mmcfg_resources_inserted == 1) || 259fb9aa6f1SThomas Gleixner (pci_probe & PCI_PROBE_MMCONF) == 0 || 260fb9aa6f1SThomas Gleixner (pci_mmcfg_config_num == 0) || 261fb9aa6f1SThomas Gleixner (pci_mmcfg_config == NULL) || 262fb9aa6f1SThomas Gleixner (pci_mmcfg_config[0].address == 0)) 263fb9aa6f1SThomas Gleixner return 1; 264fb9aa6f1SThomas Gleixner 265fb9aa6f1SThomas Gleixner /* 266fb9aa6f1SThomas Gleixner * Attempt to insert the mmcfg resources but not with the busy flag 267fb9aa6f1SThomas Gleixner * marked so it won't cause request errors when __request_region is 268fb9aa6f1SThomas Gleixner * called. 269fb9aa6f1SThomas Gleixner */ 270fb9aa6f1SThomas Gleixner pci_mmcfg_insert_resources(0); 271fb9aa6f1SThomas Gleixner 272fb9aa6f1SThomas Gleixner return 0; 273fb9aa6f1SThomas Gleixner } 274fb9aa6f1SThomas Gleixner 275fb9aa6f1SThomas Gleixner /* 276fb9aa6f1SThomas Gleixner * Perform MMCONFIG resource insertion after PCI initialization to allow for 277fb9aa6f1SThomas Gleixner * misprogrammed MCFG tables that state larger sizes but actually conflict 278fb9aa6f1SThomas Gleixner * with other system resources. 279fb9aa6f1SThomas Gleixner */ 280fb9aa6f1SThomas Gleixner late_initcall(pci_mmcfg_late_insert_resources); 281