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 |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 22 | #define PCI_MMCFG_MAX_CHECK_BUS 16 |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | /* Static virtual mapping of the MMCONFIG aperture */ |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 25 | struct mmcfg_virt { |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 26 | struct acpi_mcfg_allocation *cfg; |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 27 | char __iomem *virt; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 28 | }; |
| 29 | static struct mmcfg_virt *pci_mmcfg_virt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
OGAWA Hirofumi | faed197 | 2007-02-13 13:26:20 +0100 | [diff] [blame^] | 31 | static inline int mcfg_broken(void) |
| 32 | { |
| 33 | struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[0]; |
| 34 | |
| 35 | /* Handle more broken MCFG tables on Asus etc. |
| 36 | They only contain a single entry for bus 0-0. Assume |
| 37 | this applies to all busses. */ |
| 38 | if (pci_mmcfg_config_num == 1 && |
| 39 | cfg->pci_segment_group_number == 0 && |
| 40 | (cfg->start_bus_number | cfg->end_bus_number) == 0) |
| 41 | return 1; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static void __iomem *mcfg_ioremap(struct acpi_mcfg_allocation *cfg) |
| 46 | { |
| 47 | void __iomem *addr; |
| 48 | u32 size; |
| 49 | |
| 50 | if (mcfg_broken()) |
| 51 | size = 256 << 20; |
| 52 | else |
| 53 | size = (cfg->end_bus_number + 1) << 20; |
| 54 | |
| 55 | addr = ioremap_nocache(cfg->base_address, size); |
| 56 | if (addr) { |
| 57 | printk(KERN_INFO "PCI: Using MMCONFIG at %x - %x\n", |
| 58 | cfg->base_address, |
| 59 | cfg->base_address + size - 1); |
| 60 | } |
| 61 | return addr; |
| 62 | } |
| 63 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 64 | static char __iomem *get_virt(unsigned int seg, unsigned bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 66 | int cfg_num = -1; |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 67 | struct acpi_mcfg_allocation *cfg; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 68 | |
| 69 | while (1) { |
| 70 | ++cfg_num; |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 71 | if (cfg_num >= pci_mmcfg_config_num) |
| 72 | break; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 73 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 74 | if (cfg->pci_segment != seg) |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 75 | continue; |
| 76 | if ((cfg->start_bus_number <= bus) && |
| 77 | (cfg->end_bus_number >= bus)) |
| 78 | return pci_mmcfg_virt[cfg_num].virt; |
| 79 | } |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 80 | |
OGAWA Hirofumi | faed197 | 2007-02-13 13:26:20 +0100 | [diff] [blame^] | 81 | if (mcfg_broken()) |
Andi Kleen | 1de6bf3 | 2006-02-03 21:51:29 +0100 | [diff] [blame] | 82 | return pci_mmcfg_virt[0].virt; |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 83 | |
| 84 | /* Fall back to type 0 */ |
Al Viro | cc59853 | 2006-02-03 20:28:01 -0500 | [diff] [blame] | 85 | return NULL; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 88 | 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] | 89 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 90 | char __iomem *addr; |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 91 | if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS && |
| 92 | test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots)) |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 93 | return NULL; |
| 94 | addr = get_virt(seg, bus); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 95 | if (!addr) |
| 96 | return NULL; |
| 97 | return addr + ((bus << 20) | (devfn << 12)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 101 | unsigned int devfn, int reg, int len, u32 *value) |
| 102 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 103 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 105 | /* 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] | 106 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 107 | *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return -EINVAL; |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 111 | addr = pci_dev_base(seg, bus, devfn); |
| 112 | if (!addr) |
| 113 | return pci_conf1_read(seg,bus,devfn,reg,len,value); |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | switch (len) { |
| 116 | case 1: |
| 117 | *value = readb(addr + reg); |
| 118 | break; |
| 119 | case 2: |
| 120 | *value = readw(addr + reg); |
| 121 | break; |
| 122 | case 4: |
| 123 | *value = readl(addr + reg); |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 131 | unsigned int devfn, int reg, int len, u32 value) |
| 132 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 133 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 135 | /* 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] | 136 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 137 | return -EINVAL; |
| 138 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 139 | addr = pci_dev_base(seg, bus, devfn); |
| 140 | if (!addr) |
| 141 | return pci_conf1_write(seg,bus,devfn,reg,len,value); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | switch (len) { |
| 144 | case 1: |
| 145 | writeb(value, addr + reg); |
| 146 | break; |
| 147 | case 2: |
| 148 | writew(value, addr + reg); |
| 149 | break; |
| 150 | case 4: |
| 151 | writel(value, addr + reg); |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static struct pci_raw_ops pci_mmcfg = { |
| 159 | .read = pci_mmcfg_read, |
| 160 | .write = pci_mmcfg_write, |
| 161 | }; |
| 162 | |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 163 | int __init pci_mmcfg_arch_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 165 | int i; |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 166 | pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * |
| 167 | pci_mmcfg_config_num, GFP_KERNEL); |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 168 | if (pci_mmcfg_virt == NULL) { |
Dave Jones | 3095fc0 | 2006-10-20 14:45:33 -0700 | [diff] [blame] | 169 | printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 170 | return 0; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 171 | } |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 172 | |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 173 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 174 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; |
OGAWA Hirofumi | faed197 | 2007-02-13 13:26:20 +0100 | [diff] [blame^] | 175 | pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 176 | if (!pci_mmcfg_virt[i].virt) { |
Dave Jones | 3095fc0 | 2006-10-20 14:45:33 -0700 | [diff] [blame] | 177 | printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " |
| 178 | "segment %d\n", |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 179 | pci_mmcfg_config[i].pci_segment); |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 180 | return 0; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 181 | } |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | raw_pci_ops = &pci_mmcfg; |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 184 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |