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> |
Jaswinder Singh Rajput | 8248771 | 2008-12-27 18:32:28 +0530 | [diff] [blame] | 13 | #include <asm/pci_x86.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | /* Static virtual mapping of the MMCONFIG aperture */ |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 16 | struct mmcfg_virt { |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 17 | struct acpi_mcfg_allocation *cfg; |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 18 | char __iomem *virt; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 19 | }; |
| 20 | static struct mmcfg_virt *pci_mmcfg_virt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 22 | static char __iomem *get_virt(unsigned int seg, unsigned bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 24 | struct acpi_mcfg_allocation *cfg; |
OGAWA Hirofumi | 429d512 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 25 | int cfg_num; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 26 | |
OGAWA Hirofumi | 429d512 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 27 | for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 28 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
OGAWA Hirofumi | 429d512 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 29 | if (cfg->pci_segment == seg && |
| 30 | (cfg->start_bus_number <= bus) && |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 31 | (cfg->end_bus_number >= bus)) |
| 32 | return pci_mmcfg_virt[cfg_num].virt; |
| 33 | } |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 34 | |
Andi Kleen | 3103039 | 2006-01-27 02:03:50 +0100 | [diff] [blame] | 35 | /* Fall back to type 0 */ |
Al Viro | cc59853 | 2006-02-03 20:28:01 -0500 | [diff] [blame] | 36 | return NULL; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 39 | 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] | 40 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 41 | char __iomem *addr; |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 42 | |
Andi Kleen | d6ece54 | 2005-12-12 22:17:11 -0800 | [diff] [blame] | 43 | addr = get_virt(seg, bus); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 44 | if (!addr) |
| 45 | return NULL; |
| 46 | return addr + ((bus << 20) | (devfn << 12)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 50 | unsigned int devfn, int reg, int len, u32 *value) |
| 51 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 52 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 54 | /* 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] | 55 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 56 | err: *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return -EINVAL; |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 60 | addr = pci_dev_base(seg, bus, devfn); |
| 61 | if (!addr) |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 62 | goto err; |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | switch (len) { |
| 65 | case 1: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 66 | *value = mmio_config_readb(addr + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | break; |
| 68 | case 2: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 69 | *value = mmio_config_readw(addr + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | break; |
| 71 | case 4: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 72 | *value = mmio_config_readl(addr + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | break; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 80 | unsigned int devfn, int reg, int len, u32 value) |
| 81 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 82 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 84 | /* 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] | 85 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 86 | return -EINVAL; |
| 87 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 88 | addr = pci_dev_base(seg, bus, devfn); |
| 89 | if (!addr) |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 90 | return -EINVAL; |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | switch (len) { |
| 93 | case 1: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 94 | mmio_config_writeb(addr + reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | break; |
| 96 | case 2: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 97 | mmio_config_writew(addr + reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | break; |
| 99 | case 4: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 100 | mmio_config_writel(addr + reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static struct pci_raw_ops pci_mmcfg = { |
| 108 | .read = pci_mmcfg_read, |
| 109 | .write = pci_mmcfg_write, |
| 110 | }; |
| 111 | |
OGAWA Hirofumi | 44de020 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 112 | static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg) |
| 113 | { |
| 114 | void __iomem *addr; |
| 115 | u32 size; |
| 116 | |
| 117 | size = (cfg->end_bus_number + 1) << 20; |
| 118 | addr = ioremap_nocache(cfg->address, size); |
| 119 | if (addr) { |
| 120 | printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n", |
| 121 | cfg->address, cfg->address + size - 1); |
| 122 | } |
| 123 | return addr; |
| 124 | } |
| 125 | |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 126 | int __init pci_mmcfg_arch_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 128 | int i; |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 129 | pci_mmcfg_virt = kzalloc(sizeof(*pci_mmcfg_virt) * |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 130 | pci_mmcfg_config_num, GFP_KERNEL); |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 131 | if (pci_mmcfg_virt == NULL) { |
Dave Jones | 3095fc0 | 2006-10-20 14:45:33 -0700 | [diff] [blame] | 132 | printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 133 | return 0; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 134 | } |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 135 | |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 136 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 137 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; |
OGAWA Hirofumi | faed197 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 138 | pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 139 | if (!pci_mmcfg_virt[i].virt) { |
Dave Jones | 3095fc0 | 2006-10-20 14:45:33 -0700 | [diff] [blame] | 140 | printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " |
| 141 | "segment %d\n", |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 142 | pci_mmcfg_config[i].pci_segment); |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 143 | pci_mmcfg_arch_free(); |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 144 | return 0; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 145 | } |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 146 | } |
Matthew Wilcox | b6ce068 | 2008-02-10 09:45:28 -0500 | [diff] [blame] | 147 | raw_pci_ext_ops = &pci_mmcfg; |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 148 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 150 | |
| 151 | void __init pci_mmcfg_arch_free(void) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | if (pci_mmcfg_virt == NULL) |
| 156 | return; |
| 157 | |
| 158 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 159 | if (pci_mmcfg_virt[i].virt) { |
| 160 | iounmap(pci_mmcfg_virt[i].virt); |
| 161 | pci_mmcfg_virt[i].virt = NULL; |
| 162 | pci_mmcfg_virt[i].cfg = NULL; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | kfree(pci_mmcfg_virt); |
| 167 | pci_mmcfg_virt = NULL; |
| 168 | } |