Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mmconfig.c - Low-level direct PCI config space access via MMCONFIG |
| 3 | * |
| 4 | * This is an 64bit optimized version that always keeps the full mmconfig |
| 5 | * space mapped. This allows lockless config space operation. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/pci.h> |
| 9 | #include <linux/init.h> |
Greg Kroah-Hartman | 5454939 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 10 | #include <linux/acpi.h> |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 11 | #include <linux/bitmap.h> |
Arjan van de Ven | 946f2ee | 2006-04-07 19:49:30 +0200 | [diff] [blame] | 12 | #include <asm/e820.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "pci.h" |
| 15 | |
Chuck Ebbert | ead2bfe | 2006-06-15 04:41:52 -0400 | [diff] [blame] | 16 | /* aperture is up to 256MB but BIOS may reserve less */ |
| 17 | #define MMCONFIG_APER_MIN (2 * 1024*1024) |
| 18 | #define MMCONFIG_APER_MAX (256 * 1024*1024) |
| 19 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 20 | /* Verify the first 16 busses. We assume that systems with more busses |
| 21 | get MCFG right. */ |
| 22 | #define MAX_CHECK_BUS 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 24 | static DECLARE_BITMAP(fallback_slots, 32*MAX_CHECK_BUS); |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* Static virtual mapping of the MMCONFIG aperture */ |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 27 | struct mmcfg_virt { |
| 28 | struct acpi_table_mcfg_config *cfg; |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 29 | char __iomem *virt; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 30 | }; |
| 31 | static struct mmcfg_virt *pci_mmcfg_virt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 33 | static char __iomem *get_virt(unsigned int seg, unsigned bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 35 | int cfg_num = -1; |
| 36 | struct acpi_table_mcfg_config *cfg; |
| 37 | |
| 38 | while (1) { |
| 39 | ++cfg_num; |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 40 | if (cfg_num >= pci_mmcfg_config_num) |
| 41 | break; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 42 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
| 43 | if (cfg->pci_segment_group_number != seg) |
| 44 | continue; |
| 45 | if ((cfg->start_bus_number <= bus) && |
| 46 | (cfg->end_bus_number >= bus)) |
| 47 | return pci_mmcfg_virt[cfg_num].virt; |
| 48 | } |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 49 | |
| 50 | /* Handle more broken MCFG tables on Asus etc. |
| 51 | They only contain a single entry for bus 0-0. Assume |
| 52 | this applies to all busses. */ |
| 53 | cfg = &pci_mmcfg_config[0]; |
| 54 | if (pci_mmcfg_config_num == 1 && |
| 55 | cfg->pci_segment_group_number == 0 && |
| 56 | (cfg->start_bus_number | cfg->end_bus_number) == 0) |
Andi Kleen | 1de6bf3 | 2006-02-03 21:51:29 +0100 | [diff] [blame] | 57 | return pci_mmcfg_virt[0].virt; |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 58 | |
| 59 | /* Fall back to type 0 */ |
Al Viro | cc59853 | 2006-02-03 20:28:01 -0500 | [diff] [blame] | 60 | return NULL; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 63 | static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn) |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 64 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 65 | char __iomem *addr; |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 66 | if (seg == 0 && bus < MAX_CHECK_BUS && |
| 67 | test_bit(32*bus + PCI_SLOT(devfn), fallback_slots)) |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 68 | return NULL; |
| 69 | addr = get_virt(seg, bus); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 70 | if (!addr) |
| 71 | return NULL; |
| 72 | return addr + ((bus << 20) | (devfn << 12)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 76 | unsigned int devfn, int reg, int len, u32 *value) |
| 77 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 78 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 80 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
Andi Kleen | ecc16ba | 2006-04-11 12:54:48 +0200 | [diff] [blame] | 81 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 82 | *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return -EINVAL; |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 86 | addr = pci_dev_base(seg, bus, devfn); |
| 87 | if (!addr) |
| 88 | return pci_conf1_read(seg,bus,devfn,reg,len,value); |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | switch (len) { |
| 91 | case 1: |
| 92 | *value = readb(addr + reg); |
| 93 | break; |
| 94 | case 2: |
| 95 | *value = readw(addr + reg); |
| 96 | break; |
| 97 | case 4: |
| 98 | *value = readl(addr + reg); |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 106 | unsigned int devfn, int reg, int len, u32 value) |
| 107 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 108 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 110 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 112 | return -EINVAL; |
| 113 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 114 | addr = pci_dev_base(seg, bus, devfn); |
| 115 | if (!addr) |
| 116 | return pci_conf1_write(seg,bus,devfn,reg,len,value); |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | switch (len) { |
| 119 | case 1: |
| 120 | writeb(value, addr + reg); |
| 121 | break; |
| 122 | case 2: |
| 123 | writew(value, addr + reg); |
| 124 | break; |
| 125 | case 4: |
| 126 | writel(value, addr + reg); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static struct pci_raw_ops pci_mmcfg = { |
| 134 | .read = pci_mmcfg_read, |
| 135 | .write = pci_mmcfg_write, |
| 136 | }; |
| 137 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 138 | /* K8 systems have some devices (typically in the builtin northbridge) |
| 139 | that are only accessible using type1 |
| 140 | Normally this can be expressed in the MCFG by not listing them |
| 141 | and assigning suitable _SEGs, but this isn't implemented in some BIOS. |
| 142 | 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] | 143 | and fallback for them. */ |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 144 | static __init void unreachable_devices(void) |
| 145 | { |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 146 | int i, k; |
| 147 | /* Use the max bus number from ACPI here? */ |
Andi Kleen | 44b940c | 2006-04-11 12:54:51 +0200 | [diff] [blame] | 148 | for (k = 0; k < MAX_CHECK_BUS; k++) { |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 149 | for (i = 0; i < 32; i++) { |
| 150 | u32 val1; |
| 151 | char __iomem *addr; |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 152 | |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 153 | pci_conf1_read(0, k, PCI_DEVFN(i,0), 0, 4, &val1); |
| 154 | if (val1 == 0xffffffff) |
| 155 | continue; |
| 156 | addr = pci_dev_base(0, k, PCI_DEVFN(i, 0)); |
| 157 | if (addr == NULL|| readl(addr) != val1) { |
| 158 | set_bit(i + 32*k, fallback_slots); |
Brice Goglin | 40bee2e | 2006-09-26 10:52:35 +0200 | [diff] [blame] | 159 | printk(KERN_NOTICE "PCI: No mmconfig possible" |
| 160 | " on device %02x:%02x\n", k, i); |
Andi Kleen | 8c30b1a74 | 2006-04-07 19:50:12 +0200 | [diff] [blame] | 161 | } |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Aaron Durbin | 4c6e052 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 166 | static __init void pci_mmcfg_insert_resources(void) |
| 167 | { |
| 168 | #define PCI_MMCFG_RESOURCE_NAME_LEN 19 |
| 169 | int i; |
| 170 | struct resource *res; |
| 171 | char *names; |
| 172 | unsigned num_buses; |
| 173 | |
| 174 | res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res), |
| 175 | pci_mmcfg_config_num, GFP_KERNEL); |
| 176 | |
| 177 | if (!res) { |
| 178 | printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | names = (void *)&res[pci_mmcfg_config_num]; |
| 183 | for (i = 0; i < pci_mmcfg_config_num; i++, res++) { |
| 184 | num_buses = pci_mmcfg_config[i].end_bus_number - |
| 185 | pci_mmcfg_config[i].start_bus_number + 1; |
| 186 | res->name = names; |
| 187 | snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u", |
| 188 | pci_mmcfg_config[i].pci_segment_group_number); |
| 189 | res->start = pci_mmcfg_config[i].base_address; |
| 190 | res->end = res->start + (num_buses << 20) - 1; |
| 191 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 192 | insert_resource(&iomem_resource, res); |
| 193 | names += PCI_MMCFG_RESOURCE_NAME_LEN; |
| 194 | } |
| 195 | } |
| 196 | |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 197 | void __init pci_mmcfg_init(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 199 | int i; |
| 200 | |
Linus Torvalds | 79e453d | 2006-09-19 08:15:22 -0700 | [diff] [blame] | 201 | if ((pci_probe & PCI_PROBE_MMCONF) == 0) |
Akinobu Mita | 3d1712c | 2006-03-24 03:15:11 -0800 | [diff] [blame] | 202 | return; |
Greg Kroah-Hartman | 5454939 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 203 | |
| 204 | acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg); |
| 205 | if ((pci_mmcfg_config_num == 0) || |
| 206 | (pci_mmcfg_config == NULL) || |
| 207 | (pci_mmcfg_config[0].base_address == 0)) |
Akinobu Mita | 3d1712c | 2006-03-24 03:15:11 -0800 | [diff] [blame] | 208 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Andi Kleen | 9abd792 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 210 | /* Only do this check when type 1 works. If it doesn't work |
| 211 | assume we run on a Mac and always use MCFG */ |
| 212 | if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].base_address, |
Linus Torvalds | 79e453d | 2006-09-19 08:15:22 -0700 | [diff] [blame] | 213 | pci_mmcfg_config[0].base_address + MMCONFIG_APER_MIN, |
| 214 | E820_RESERVED)) { |
| 215 | printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %x is not E820-reserved\n", |
| 216 | pci_mmcfg_config[0].base_address); |
| 217 | printk(KERN_ERR "PCI: Not using MMCONFIG.\n"); |
| 218 | return; |
| 219 | } |
| 220 | |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 221 | pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL); |
| 222 | if (pci_mmcfg_virt == NULL) { |
Dave Jones | 3095fc0 | 2006-10-20 14:45:33 -0700 | [diff] [blame^] | 223 | printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); |
Akinobu Mita | 3d1712c | 2006-03-24 03:15:11 -0800 | [diff] [blame] | 224 | return; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 225 | } |
| 226 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 227 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; |
Chuck Ebbert | ead2bfe | 2006-06-15 04:41:52 -0400 | [diff] [blame] | 228 | pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].base_address, |
| 229 | MMCONFIG_APER_MAX); |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 230 | if (!pci_mmcfg_virt[i].virt) { |
Dave Jones | 3095fc0 | 2006-10-20 14:45:33 -0700 | [diff] [blame^] | 231 | printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " |
| 232 | "segment %d\n", |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 233 | pci_mmcfg_config[i].pci_segment_group_number); |
Akinobu Mita | 3d1712c | 2006-03-24 03:15:11 -0800 | [diff] [blame] | 234 | return; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 235 | } |
| 236 | printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_config[i].base_address); |
| 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 239 | unreachable_devices(); |
Aaron Durbin | 4c6e052 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 240 | pci_mmcfg_insert_resources(); |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | raw_pci_ops = &pci_mmcfg; |
| 243 | pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |