Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Matthew Wilcox <matthew@wil.cx> |
| 3 | * Copyright (C) 2004 Intel Corp. |
| 4 | * |
| 5 | * This code is released under the GNU General Public License version 2. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * mmconfig.c - Low-level direct PCI config space access via MMCONFIG |
| 10 | */ |
| 11 | |
| 12 | #include <linux/pci.h> |
| 13 | #include <linux/init.h> |
Greg Kroah-Hartman | 5454939 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 14 | #include <linux/acpi.h> |
Arjan van de Ven | 946f2ee | 2006-04-07 19:49:30 +0200 | [diff] [blame] | 15 | #include <asm/e820.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "pci.h" |
| 17 | |
Chuck Ebbert | ead2bfe | 2006-06-15 04:41:52 -0400 | [diff] [blame] | 18 | /* aperture is up to 256MB but BIOS may reserve less */ |
| 19 | #define MMCONFIG_APER_MIN (2 * 1024*1024) |
| 20 | #define MMCONFIG_APER_MAX (256 * 1024*1024) |
Arjan van de Ven | 946f2ee | 2006-04-07 19:49:30 +0200 | [diff] [blame] | 21 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 22 | /* Assume systems with more busses have correct MCFG */ |
| 23 | #define MAX_CHECK_BUS 16 |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG)) |
| 26 | |
| 27 | /* The base address of the last MMCONFIG device accessed */ |
| 28 | static u32 mmcfg_last_accessed_device; |
| 29 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 30 | static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32); |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Functions for accessing PCI configuration space with MMCONFIG accesses |
| 34 | */ |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 35 | static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Greg Kroah-Hartman | d57e26c | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 37 | int cfg_num = -1; |
| 38 | struct acpi_table_mcfg_config *cfg; |
| 39 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 40 | if (seg == 0 && bus < MAX_CHECK_BUS && |
| 41 | test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots)) |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 42 | return 0; |
| 43 | |
Greg Kroah-Hartman | d57e26c | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 44 | while (1) { |
| 45 | ++cfg_num; |
| 46 | if (cfg_num >= pci_mmcfg_config_num) { |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 47 | break; |
Greg Kroah-Hartman | d57e26c | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 48 | } |
| 49 | cfg = &pci_mmcfg_config[cfg_num]; |
| 50 | if (cfg->pci_segment_group_number != seg) |
| 51 | continue; |
| 52 | if ((cfg->start_bus_number <= bus) && |
| 53 | (cfg->end_bus_number >= bus)) |
| 54 | return cfg->base_address; |
| 55 | } |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 56 | |
| 57 | /* Handle more broken MCFG tables on Asus etc. |
| 58 | They only contain a single entry for bus 0-0. Assume |
| 59 | this applies to all busses. */ |
| 60 | cfg = &pci_mmcfg_config[0]; |
| 61 | if (pci_mmcfg_config_num == 1 && |
| 62 | cfg->pci_segment_group_number == 0 && |
| 63 | (cfg->start_bus_number | cfg->end_bus_number) == 0) |
| 64 | return cfg->base_address; |
| 65 | |
| 66 | /* Fall back to type 0 */ |
| 67 | return 0; |
Greg Kroah-Hartman | d57e26c | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Andrew Morton | be5b7a8 | 2006-09-30 23:27:10 -0700 | [diff] [blame^] | 70 | /* |
| 71 | * This is always called under pci_config_lock |
| 72 | */ |
| 73 | static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn) |
Greg Kroah-Hartman | d57e26c | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 74 | { |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 75 | u32 dev_base = base | (bus << 20) | (devfn << 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (dev_base != mmcfg_last_accessed_device) { |
| 77 | mmcfg_last_accessed_device = dev_base; |
| 78 | set_fixmap_nocache(FIX_PCIE_MCFG, dev_base); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 83 | unsigned int devfn, int reg, int len, u32 *value) |
| 84 | { |
| 85 | unsigned long flags; |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 86 | u32 base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Andi Kleen | ecc16ba | 2006-04-11 12:54:48 +0200 | [diff] [blame] | 88 | if ((bus > 255) || (devfn > 255) || (reg > 4095)) { |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 89 | *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return -EINVAL; |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 91 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 93 | base = get_base_addr(seg, bus, devfn); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 94 | if (!base) |
| 95 | return pci_conf1_read(seg,bus,devfn,reg,len,value); |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | spin_lock_irqsave(&pci_config_lock, flags); |
| 98 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 99 | pci_exp_set_dev_base(base, bus, devfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | switch (len) { |
| 102 | case 1: |
| 103 | *value = readb(mmcfg_virt_addr + reg); |
| 104 | break; |
| 105 | case 2: |
| 106 | *value = readw(mmcfg_virt_addr + reg); |
| 107 | break; |
| 108 | case 4: |
| 109 | *value = readl(mmcfg_virt_addr + reg); |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | spin_unlock_irqrestore(&pci_config_lock, flags); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 119 | unsigned int devfn, int reg, int len, u32 value) |
| 120 | { |
| 121 | unsigned long flags; |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 122 | u32 base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | if ((bus > 255) || (devfn > 255) || (reg > 4095)) |
| 125 | return -EINVAL; |
| 126 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 127 | base = get_base_addr(seg, bus, devfn); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 128 | if (!base) |
| 129 | return pci_conf1_write(seg,bus,devfn,reg,len,value); |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | spin_lock_irqsave(&pci_config_lock, flags); |
| 132 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 133 | pci_exp_set_dev_base(base, bus, devfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | switch (len) { |
| 136 | case 1: |
| 137 | writeb(value, mmcfg_virt_addr + reg); |
| 138 | break; |
| 139 | case 2: |
| 140 | writew(value, mmcfg_virt_addr + reg); |
| 141 | break; |
| 142 | case 4: |
| 143 | writel(value, mmcfg_virt_addr + reg); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | spin_unlock_irqrestore(&pci_config_lock, flags); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static struct pci_raw_ops pci_mmcfg = { |
| 153 | .read = pci_mmcfg_read, |
| 154 | .write = pci_mmcfg_write, |
| 155 | }; |
| 156 | |
Andi Kleen | de09bdd | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 157 | |
| 158 | static __init void pci_mmcfg_insert_resources(void) |
| 159 | { |
| 160 | #define PCI_MMCFG_RESOURCE_NAME_LEN 19 |
| 161 | int i; |
| 162 | struct resource *res; |
| 163 | char *names; |
| 164 | unsigned num_buses; |
| 165 | |
| 166 | res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res), |
| 167 | pci_mmcfg_config_num, GFP_KERNEL); |
| 168 | |
| 169 | if (!res) { |
| 170 | printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | names = (void *)&res[pci_mmcfg_config_num]; |
| 175 | for (i = 0; i < pci_mmcfg_config_num; i++, res++) { |
| 176 | num_buses = pci_mmcfg_config[i].end_bus_number - |
| 177 | pci_mmcfg_config[i].start_bus_number + 1; |
| 178 | res->name = names; |
| 179 | snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u", |
| 180 | pci_mmcfg_config[i].pci_segment_group_number); |
| 181 | res->start = pci_mmcfg_config[i].base_address; |
| 182 | res->end = res->start + (num_buses << 20) - 1; |
| 183 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 184 | insert_resource(&iomem_resource, res); |
| 185 | names += PCI_MMCFG_RESOURCE_NAME_LEN; |
| 186 | } |
| 187 | } |
| 188 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 189 | /* K8 systems have some devices (typically in the builtin northbridge) |
| 190 | that are only accessible using type1 |
| 191 | Normally this can be expressed in the MCFG by not listing them |
| 192 | and assigning suitable _SEGs, but this isn't implemented in some BIOS. |
| 193 | Instead try to discover all devices on bus 0 that are unreachable using MM |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 194 | and fallback for them. */ |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 195 | static __init void unreachable_devices(void) |
| 196 | { |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 197 | int i, k; |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 198 | unsigned long flags; |
| 199 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 200 | for (k = 0; k < MAX_CHECK_BUS; k++) { |
| 201 | for (i = 0; i < 32; i++) { |
| 202 | u32 val1; |
| 203 | u32 addr; |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 204 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 205 | pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1); |
| 206 | if (val1 == 0xffffffff) |
| 207 | continue; |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 208 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 209 | /* Locking probably not needed, but safer */ |
| 210 | spin_lock_irqsave(&pci_config_lock, flags); |
| 211 | addr = get_base_addr(0, k, PCI_DEVFN(i, 0)); |
| 212 | if (addr != 0) |
| 213 | pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0)); |
| 214 | if (addr == 0 || |
| 215 | readl((u32 __iomem *)mmcfg_virt_addr) != val1) { |
Daniel Ritz | fd4dc27 | 2006-08-22 07:29:09 -0700 | [diff] [blame] | 216 | set_bit(i + 32*k, fallback_slots); |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 217 | printk(KERN_NOTICE |
| 218 | "PCI: No mmconfig possible on %x:%x\n", k, i); |
| 219 | } |
| 220 | spin_unlock_irqrestore(&pci_config_lock, flags); |
| 221 | } |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Andi Kleen | de09bdd | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 225 | |
| 226 | |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 227 | void __init pci_mmcfg_init(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Linus Torvalds | 79e453d | 2006-09-19 08:15:22 -0700 | [diff] [blame] | 229 | if ((pci_probe & PCI_PROBE_MMCONF) == 0) |
Andi Kleen | 92c05fc | 2006-03-23 14:35:12 -0800 | [diff] [blame] | 230 | return; |
Greg Kroah-Hartman | 5454939 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 231 | |
| 232 | acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg); |
| 233 | if ((pci_mmcfg_config_num == 0) || |
| 234 | (pci_mmcfg_config == NULL) || |
| 235 | (pci_mmcfg_config[0].base_address == 0)) |
Andi Kleen | 92c05fc | 2006-03-23 14:35:12 -0800 | [diff] [blame] | 236 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Andi Kleen | 9abd792 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 238 | /* Only do this check when type 1 works. If it doesn't work |
| 239 | assume we run on a Mac and always use MCFG */ |
| 240 | if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].base_address, |
Linus Torvalds | 79e453d | 2006-09-19 08:15:22 -0700 | [diff] [blame] | 241 | pci_mmcfg_config[0].base_address + MMCONFIG_APER_MIN, |
| 242 | E820_RESERVED)) { |
| 243 | printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %x is not E820-reserved\n", |
| 244 | pci_mmcfg_config[0].base_address); |
| 245 | printk(KERN_ERR "PCI: Not using MMCONFIG.\n"); |
| 246 | return; |
| 247 | } |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | printk(KERN_INFO "PCI: Using MMCONFIG\n"); |
| 250 | raw_pci_ops = &pci_mmcfg; |
| 251 | pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF; |
| 252 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 253 | unreachable_devices(); |
Andi Kleen | de09bdd | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 254 | pci_mmcfg_insert_resources(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |