| 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 | |
| 14 | #include "pci.h" |
| 15 | |
| 16 | /* Static virtual mapping of the MMCONFIG aperture */ |
| 17 | struct mmcfg_virt { |
| 18 | struct acpi_mcfg_allocation *cfg; |
| 19 | char __iomem *virt; |
| 20 | }; |
| 21 | static struct mmcfg_virt *pci_mmcfg_virt; |
| 22 | |
| 23 | static char __iomem *get_virt(unsigned int seg, unsigned bus) |
| 24 | { |
| 25 | struct acpi_mcfg_allocation *cfg; |
| 26 | int cfg_num; |
| 27 | |
| 28 | for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) { |
| 29 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
| 30 | if (cfg->pci_segment == seg && |
| 31 | (cfg->start_bus_number <= bus) && |
| 32 | (cfg->end_bus_number >= bus)) |
| 33 | return pci_mmcfg_virt[cfg_num].virt; |
| 34 | } |
| 35 | |
| 36 | /* Fall back to type 0 */ |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn) |
| 41 | { |
| 42 | char __iomem *addr; |
| 43 | |
| 44 | addr = get_virt(seg, bus); |
| 45 | if (!addr) |
| 46 | return NULL; |
| 47 | return addr + ((bus << 20) | (devfn << 12)); |
| 48 | } |
| 49 | |
| 50 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, |
| 51 | unsigned int devfn, int reg, int len, u32 *value) |
| 52 | { |
| 53 | char __iomem *addr; |
| 54 | |
| 55 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
| 56 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
| 57 | err: *value = -1; |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | addr = pci_dev_base(seg, bus, devfn); |
| 62 | if (!addr) |
| 63 | goto err; |
| 64 | |
| 65 | switch (len) { |
| 66 | case 1: |
| 67 | *value = mmio_config_readb(addr + reg); |
| 68 | break; |
| 69 | case 2: |
| 70 | *value = mmio_config_readw(addr + reg); |
| 71 | break; |
| 72 | case 4: |
| 73 | *value = mmio_config_readl(addr + reg); |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, |
| 81 | unsigned int devfn, int reg, int len, u32 value) |
| 82 | { |
| 83 | char __iomem *addr; |
| 84 | |
| 85 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
| 86 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | addr = pci_dev_base(seg, bus, devfn); |
| 90 | if (!addr) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | switch (len) { |
| 94 | case 1: |
| 95 | mmio_config_writeb(addr + reg, value); |
| 96 | break; |
| 97 | case 2: |
| 98 | mmio_config_writew(addr + reg, value); |
| 99 | break; |
| 100 | case 4: |
| 101 | mmio_config_writel(addr + reg, value); |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static struct pci_raw_ops pci_mmcfg = { |
| 109 | .read = pci_mmcfg_read, |
| 110 | .write = pci_mmcfg_write, |
| 111 | }; |
| 112 | |
| 113 | static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg) |
| 114 | { |
| 115 | void __iomem *addr; |
| 116 | u32 size; |
| 117 | |
| 118 | size = (cfg->end_bus_number + 1) << 20; |
| 119 | addr = ioremap_nocache(cfg->address, size); |
| 120 | if (addr) { |
| 121 | printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n", |
| 122 | cfg->address, cfg->address + size - 1); |
| 123 | } |
| 124 | return addr; |
| 125 | } |
| 126 | |
| 127 | int __init pci_mmcfg_arch_init(void) |
| 128 | { |
| 129 | int i; |
| 130 | pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * |
| 131 | pci_mmcfg_config_num, GFP_KERNEL); |
| 132 | if (pci_mmcfg_virt == NULL) { |
| 133 | printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
| 138 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; |
| 139 | pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); |
| 140 | if (!pci_mmcfg_virt[i].virt) { |
| 141 | printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " |
| 142 | "segment %d\n", |
| 143 | pci_mmcfg_config[i].pci_segment); |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | raw_pci_ext_ops = &pci_mmcfg; |
| 148 | return 1; |
| 149 | } |