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> |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 12 | #include <linux/rcupdate.h> |
Arjan van de Ven | 946f2ee | 2006-04-07 19:49:30 +0200 | [diff] [blame] | 13 | #include <asm/e820.h> |
Jaswinder Singh Rajput | 8248771 | 2008-12-27 18:32:28 +0530 | [diff] [blame] | 14 | #include <asm/pci_x86.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Bjorn Helgaas | 8c57786 | 2009-11-13 17:34:59 -0700 | [diff] [blame] | 16 | #define PREFIX "PCI: " |
| 17 | |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 18 | 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] | 19 | { |
Bjorn Helgaas | f6e1d8c | 2009-11-13 17:35:04 -0700 | [diff] [blame] | 20 | struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus); |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 21 | |
Bjorn Helgaas | f6e1d8c | 2009-11-13 17:35:04 -0700 | [diff] [blame] | 22 | if (cfg && cfg->virt) |
| 23 | return cfg->virt + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12)); |
| 24 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 28 | unsigned int devfn, int reg, int len, u32 *value) |
| 29 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 30 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 32 | /* 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] | 33 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 34 | err: *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | return -EINVAL; |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 36 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 38 | rcu_read_lock(); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 39 | addr = pci_dev_base(seg, bus, devfn); |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 40 | if (!addr) { |
| 41 | rcu_read_unlock(); |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 42 | goto err; |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 43 | } |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | switch (len) { |
| 46 | case 1: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 47 | *value = mmio_config_readb(addr + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | break; |
| 49 | case 2: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 50 | *value = mmio_config_readw(addr + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | break; |
| 52 | case 4: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 53 | *value = mmio_config_readl(addr + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | break; |
| 55 | } |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 56 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 62 | unsigned int devfn, int reg, int len, u32 value) |
| 63 | { |
Al Viro | 8b8a4e3 | 2005-12-15 09:17:44 +0000 | [diff] [blame] | 64 | char __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 66 | /* 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] | 67 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 68 | return -EINVAL; |
| 69 | |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 70 | rcu_read_lock(); |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 71 | addr = pci_dev_base(seg, bus, devfn); |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 72 | if (!addr) { |
| 73 | rcu_read_unlock(); |
Ivan Kokshaysky | a0ca990 | 2008-01-14 17:31:09 -0500 | [diff] [blame] | 74 | return -EINVAL; |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 75 | } |
Andi Kleen | 928cf8c | 2005-12-12 22:17:10 -0800 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | switch (len) { |
| 78 | case 1: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 79 | mmio_config_writeb(addr + reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | break; |
| 81 | case 2: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 82 | mmio_config_writew(addr + reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | break; |
| 84 | case 4: |
dean gaudet | 3320ad9 | 2007-08-10 22:30:59 +0200 | [diff] [blame] | 85 | mmio_config_writel(addr + reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | break; |
| 87 | } |
Jiang Liu | 376f70a | 2012-06-22 14:55:12 +0800 | [diff] [blame] | 88 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Jiang Liu | c0fa407 | 2012-06-22 14:55:17 +0800 | [diff] [blame] | 93 | const struct pci_raw_ops pci_mmcfg = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .read = pci_mmcfg_read, |
| 95 | .write = pci_mmcfg_write, |
| 96 | }; |
| 97 | |
Jiang Liu | 9cf0105 | 2012-06-22 14:55:13 +0800 | [diff] [blame] | 98 | static void __iomem * __devinit mcfg_ioremap(struct pci_mmcfg_region *cfg) |
OGAWA Hirofumi | 44de020 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 99 | { |
| 100 | void __iomem *addr; |
Yinghai Lu | 068258b | 2009-03-19 20:55:35 -0700 | [diff] [blame] | 101 | u64 start, size; |
Bjorn Helgaas | df5eb1d | 2009-11-13 17:34:08 -0700 | [diff] [blame] | 102 | int num_buses; |
OGAWA Hirofumi | 44de020 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 103 | |
Bjorn Helgaas | d7e6b66 | 2009-11-13 17:34:18 -0700 | [diff] [blame] | 104 | start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus); |
| 105 | num_buses = cfg->end_bus - cfg->start_bus + 1; |
Bjorn Helgaas | df5eb1d | 2009-11-13 17:34:08 -0700 | [diff] [blame] | 106 | size = PCI_MMCFG_BUS_OFFSET(num_buses); |
Yinghai Lu | 068258b | 2009-03-19 20:55:35 -0700 | [diff] [blame] | 107 | addr = ioremap_nocache(start, size); |
Bjorn Helgaas | 8c57786 | 2009-11-13 17:34:59 -0700 | [diff] [blame] | 108 | if (addr) |
Bjorn Helgaas | d7e6b66 | 2009-11-13 17:34:18 -0700 | [diff] [blame] | 109 | addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus); |
OGAWA Hirofumi | 44de020 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 110 | return addr; |
| 111 | } |
| 112 | |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 113 | int __init pci_mmcfg_arch_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Bjorn Helgaas | 3f0f550 | 2009-11-13 17:34:39 -0700 | [diff] [blame] | 115 | struct pci_mmcfg_region *cfg; |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 116 | |
Jiang Liu | 9cf0105 | 2012-06-22 14:55:13 +0800 | [diff] [blame] | 117 | list_for_each_entry(cfg, &pci_mmcfg_list, list) |
| 118 | if (pci_mmcfg_arch_map(cfg)) { |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 119 | pci_mmcfg_arch_free(); |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 120 | return 0; |
Greg Kroah-Hartman | 1cde8a1 | 2005-06-23 17:35:56 -0700 | [diff] [blame] | 121 | } |
Jiang Liu | 9cf0105 | 2012-06-22 14:55:13 +0800 | [diff] [blame] | 122 | |
Matthew Wilcox | b6ce068 | 2008-02-10 09:45:28 -0500 | [diff] [blame] | 123 | raw_pci_ext_ops = &pci_mmcfg; |
Jiang Liu | 9cf0105 | 2012-06-22 14:55:13 +0800 | [diff] [blame] | 124 | |
Olivier Galibert | b786739 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 125 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 127 | |
| 128 | void __init pci_mmcfg_arch_free(void) |
| 129 | { |
Bjorn Helgaas | 3f0f550 | 2009-11-13 17:34:39 -0700 | [diff] [blame] | 130 | struct pci_mmcfg_region *cfg; |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 131 | |
Jiang Liu | 9cf0105 | 2012-06-22 14:55:13 +0800 | [diff] [blame] | 132 | list_for_each_entry(cfg, &pci_mmcfg_list, list) |
| 133 | pci_mmcfg_arch_unmap(cfg); |
| 134 | } |
| 135 | |
| 136 | int __devinit pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg) |
| 137 | { |
| 138 | cfg->virt = mcfg_ioremap(cfg); |
| 139 | if (!cfg->virt) { |
Jiang Liu | 24c97f0 | 2012-06-22 14:55:22 +0800 | [diff] [blame] | 140 | pr_err(PREFIX "can't map MMCONFIG at %pR\n", &cfg->res); |
Jiang Liu | 9cf0105 | 2012-06-22 14:55:13 +0800 | [diff] [blame] | 141 | return -ENOMEM; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg) |
| 148 | { |
| 149 | if (cfg && cfg->virt) { |
| 150 | iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus)); |
| 151 | cfg->virt = NULL; |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 152 | } |
Yinghai Lu | 0b64ad7 | 2008-02-15 01:28:41 -0800 | [diff] [blame] | 153 | } |