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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "pci.h" |
| 13 | |
| 14 | #define MMCONFIG_APER_SIZE (256*1024*1024) |
| 15 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 16 | static DECLARE_BITMAP(fallback_slots, 32); |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* Static virtual mapping of the MMCONFIG aperture */ |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 19 | struct mmcfg_virt { |
| 20 | struct acpi_table_mcfg_config *cfg; |
| 21 | char *virt; |
| 22 | }; |
| 23 | static struct mmcfg_virt *pci_mmcfg_virt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 25 | static char *get_virt(unsigned int seg, unsigned bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 27 | int cfg_num = -1; |
| 28 | struct acpi_table_mcfg_config *cfg; |
| 29 | |
| 30 | while (1) { |
| 31 | ++cfg_num; |
| 32 | if (cfg_num >= pci_mmcfg_config_num) { |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 33 | /* Not found - fall back to type 1. This happens |
| 34 | e.g. on the internal devices of a K8 northbridge. */ |
| 35 | return NULL; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 36 | } |
| 37 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
| 38 | if (cfg->pci_segment_group_number != seg) |
| 39 | continue; |
| 40 | if ((cfg->start_bus_number <= bus) && |
| 41 | (cfg->end_bus_number >= bus)) |
| 42 | return pci_mmcfg_virt[cfg_num].virt; |
| 43 | } |
| 44 | } |
| 45 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 46 | static char *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] | 47 | { |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 48 | char *addr; |
| 49 | if (seg == 0 && bus == 0 && test_bit(PCI_SLOT(devfn), &fallback_slots)) |
| 50 | return NULL; |
| 51 | addr = get_virt(seg, bus); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 52 | if (!addr) |
| 53 | return NULL; |
| 54 | return addr + ((bus << 20) | (devfn << 12)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 58 | unsigned int devfn, int reg, int len, u32 *value) |
| 59 | { |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 60 | char *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 62 | /* 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] | 63 | if (unlikely(!value || (bus > 255) || (devfn > 255) || (reg > 4095))) |
| 64 | return -EINVAL; |
| 65 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 66 | addr = pci_dev_base(seg, bus, devfn); |
| 67 | if (!addr) |
| 68 | return pci_conf1_read(seg,bus,devfn,reg,len,value); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | switch (len) { |
| 71 | case 1: |
| 72 | *value = readb(addr + reg); |
| 73 | break; |
| 74 | case 2: |
| 75 | *value = readw(addr + reg); |
| 76 | break; |
| 77 | case 4: |
| 78 | *value = readl(addr + reg); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 86 | unsigned int devfn, int reg, int len, u32 value) |
| 87 | { |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 88 | char *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 90 | /* 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] | 91 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 92 | return -EINVAL; |
| 93 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 94 | addr = pci_dev_base(seg, bus, devfn); |
| 95 | if (!addr) |
| 96 | return pci_conf1_write(seg,bus,devfn,reg,len,value); |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | switch (len) { |
| 99 | case 1: |
| 100 | writeb(value, addr + reg); |
| 101 | break; |
| 102 | case 2: |
| 103 | writew(value, addr + reg); |
| 104 | break; |
| 105 | case 4: |
| 106 | writel(value, addr + reg); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static struct pci_raw_ops pci_mmcfg = { |
| 114 | .read = pci_mmcfg_read, |
| 115 | .write = pci_mmcfg_write, |
| 116 | }; |
| 117 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 118 | /* K8 systems have some devices (typically in the builtin northbridge) |
| 119 | that are only accessible using type1 |
| 120 | Normally this can be expressed in the MCFG by not listing them |
| 121 | and assigning suitable _SEGs, but this isn't implemented in some BIOS. |
| 122 | Instead try to discover all devices on bus 0 that are unreachable using MM |
| 123 | and fallback for them. |
| 124 | We only do this for bus 0/seg 0 */ |
| 125 | static __init void unreachable_devices(void) |
| 126 | { |
| 127 | int i; |
| 128 | for (i = 0; i < 32; i++) { |
| 129 | u32 val1; |
| 130 | char *addr; |
| 131 | |
| 132 | pci_conf1_read(0, 0, PCI_DEVFN(i,0), 0, 4, &val1); |
| 133 | if (val1 == 0xffffffff) |
| 134 | continue; |
| 135 | addr = pci_dev_base(0, 0, PCI_DEVFN(i, 0)); |
| 136 | if (addr == NULL|| readl(addr) != val1) { |
| 137 | set_bit(i, &fallback_slots); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static int __init pci_mmcfg_init(void) |
| 143 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 144 | int i; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if ((pci_probe & PCI_PROBE_MMCONF) == 0) |
| 147 | return 0; |
Greg Kroah-Hartman | 5454939 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 148 | |
| 149 | acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg); |
| 150 | if ((pci_mmcfg_config_num == 0) || |
| 151 | (pci_mmcfg_config == NULL) || |
| 152 | (pci_mmcfg_config[0].base_address == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* RED-PEN i386 doesn't do _nocache right now */ |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 156 | pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL); |
| 157 | if (pci_mmcfg_virt == NULL) { |
| 158 | printk("PCI: Can not allocate memory for mmconfig structures\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return 0; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 160 | } |
| 161 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 162 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; |
| 163 | pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].base_address, MMCONFIG_APER_SIZE); |
| 164 | if (!pci_mmcfg_virt[i].virt) { |
| 165 | printk("PCI: Cannot map mmconfig aperture for segment %d\n", |
| 166 | pci_mmcfg_config[i].pci_segment_group_number); |
| 167 | return 0; |
| 168 | } |
| 169 | printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_config[i].base_address); |
| 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 172 | unreachable_devices(); |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | raw_pci_ops = &pci_mmcfg; |
| 175 | pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | arch_initcall(pci_mmcfg_init); |