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