| 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> |
| 10 | #include <linux/acpi.h> |
| 11 | #include <linux/bitmap.h> |
| 12 | #include <asm/e820.h> |
| 13 | #include <asm/pci_x86.h> |
| 14 | |
| 15 | /* Static virtual mapping of the MMCONFIG aperture */ |
| 16 | struct mmcfg_virt { |
| 17 | struct pci_mmcfg_region *cfg; |
| 18 | char __iomem *virt; |
| 19 | }; |
| 20 | static struct mmcfg_virt *pci_mmcfg_virt; |
| 21 | |
| 22 | static char __iomem *get_virt(unsigned int seg, unsigned bus) |
| 23 | { |
| 24 | struct pci_mmcfg_region *cfg; |
| 25 | int cfg_num; |
| 26 | |
| 27 | for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) { |
| 28 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
| 29 | if (cfg->segment == seg && |
| 30 | (cfg->start_bus <= bus) && |
| 31 | (cfg->end_bus >= bus)) |
| 32 | return pci_mmcfg_virt[cfg_num].virt; |
| 33 | } |
| 34 | |
| 35 | /* Fall back to type 0 */ |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn) |
| 40 | { |
| 41 | char __iomem *addr; |
| 42 | |
| 43 | addr = get_virt(seg, bus); |
| 44 | if (!addr) |
| 45 | return NULL; |
| 46 | return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12)); |
| 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 | { |
| 52 | char __iomem *addr; |
| 53 | |
| 54 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
| 55 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
| 56 | err: *value = -1; |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | addr = pci_dev_base(seg, bus, devfn); |
| 61 | if (!addr) |
| 62 | goto err; |
| 63 | |
| 64 | switch (len) { |
| 65 | case 1: |
| 66 | *value = mmio_config_readb(addr + reg); |
| 67 | break; |
| 68 | case 2: |
| 69 | *value = mmio_config_readw(addr + reg); |
| 70 | break; |
| 71 | case 4: |
| 72 | *value = mmio_config_readl(addr + reg); |
| 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 | { |
| 82 | char __iomem *addr; |
| 83 | |
| 84 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
| 85 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | addr = pci_dev_base(seg, bus, devfn); |
| 89 | if (!addr) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | switch (len) { |
| 93 | case 1: |
| 94 | mmio_config_writeb(addr + reg, value); |
| 95 | break; |
| 96 | case 2: |
| 97 | mmio_config_writew(addr + reg, value); |
| 98 | break; |
| 99 | case 4: |
| 100 | mmio_config_writel(addr + reg, value); |
| 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 | |
| 112 | static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg) |
| 113 | { |
| 114 | void __iomem *addr; |
| 115 | u64 start, size; |
| 116 | int num_buses; |
| 117 | |
| 118 | start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus); |
| 119 | num_buses = cfg->end_bus - cfg->start_bus + 1; |
| 120 | size = PCI_MMCFG_BUS_OFFSET(num_buses); |
| 121 | addr = ioremap_nocache(start, size); |
| 122 | if (addr) { |
| 123 | printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n", |
| 124 | start, start + size - 1); |
| 125 | addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus); |
| 126 | } |
| 127 | return addr; |
| 128 | } |
| 129 | |
| 130 | int __init pci_mmcfg_arch_init(void) |
| 131 | { |
| 132 | int i; |
| 133 | pci_mmcfg_virt = kzalloc(sizeof(*pci_mmcfg_virt) * |
| 134 | pci_mmcfg_config_num, GFP_KERNEL); |
| 135 | if (pci_mmcfg_virt == NULL) { |
| 136 | printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 141 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; |
| 142 | pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); |
| 143 | if (!pci_mmcfg_virt[i].virt) { |
| 144 | printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " |
| 145 | "segment %d\n", |
| 146 | pci_mmcfg_config[i].segment); |
| 147 | pci_mmcfg_arch_free(); |
| 148 | return 0; |
| 149 | } |
| 150 | } |
| 151 | raw_pci_ext_ops = &pci_mmcfg; |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | void __init pci_mmcfg_arch_free(void) |
| 156 | { |
| 157 | int i; |
| 158 | |
| 159 | if (pci_mmcfg_virt == NULL) |
| 160 | return; |
| 161 | |
| 162 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 163 | if (pci_mmcfg_virt[i].virt) { |
| 164 | iounmap(pci_mmcfg_virt[i].virt + PCI_MMCFG_BUS_OFFSET(pci_mmcfg_virt[i].cfg->start_bus)); |
| 165 | pci_mmcfg_virt[i].virt = NULL; |
| 166 | pci_mmcfg_virt[i].cfg = NULL; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | kfree(pci_mmcfg_virt); |
| 171 | pci_mmcfg_virt = NULL; |
| 172 | } |