1f68c0654SThomas Gleixner /* 2f68c0654SThomas Gleixner * mmconfig.c - Low-level direct PCI config space access via MMCONFIG 3f68c0654SThomas Gleixner * 4f68c0654SThomas Gleixner * This is an 64bit optimized version that always keeps the full mmconfig 5f68c0654SThomas Gleixner * space mapped. This allows lockless config space operation. 6f68c0654SThomas Gleixner */ 7f68c0654SThomas Gleixner 8f68c0654SThomas Gleixner #include <linux/pci.h> 9f68c0654SThomas Gleixner #include <linux/init.h> 10f68c0654SThomas Gleixner #include <linux/acpi.h> 11f68c0654SThomas Gleixner #include <linux/bitmap.h> 12f68c0654SThomas Gleixner #include <asm/e820.h> 1382487711SJaswinder Singh Rajput #include <asm/pci_x86.h> 14f68c0654SThomas Gleixner 15f68c0654SThomas Gleixner /* Static virtual mapping of the MMCONFIG aperture */ 16f68c0654SThomas Gleixner struct mmcfg_virt { 17d215a9c8SBjorn Helgaas struct pci_mmcfg_region *cfg; 18f68c0654SThomas Gleixner char __iomem *virt; 19f68c0654SThomas Gleixner }; 20f68c0654SThomas Gleixner static struct mmcfg_virt *pci_mmcfg_virt; 21f68c0654SThomas Gleixner 22f68c0654SThomas Gleixner static char __iomem *get_virt(unsigned int seg, unsigned bus) 23f68c0654SThomas Gleixner { 24d215a9c8SBjorn Helgaas struct pci_mmcfg_region *cfg; 25f68c0654SThomas Gleixner int cfg_num; 26f68c0654SThomas Gleixner 27f68c0654SThomas Gleixner for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) { 28f68c0654SThomas Gleixner cfg = pci_mmcfg_virt[cfg_num].cfg; 29*d7e6b66fSBjorn Helgaas if (cfg->segment == seg && 30*d7e6b66fSBjorn Helgaas (cfg->start_bus <= bus) && 31*d7e6b66fSBjorn Helgaas (cfg->end_bus >= bus)) 32f68c0654SThomas Gleixner return pci_mmcfg_virt[cfg_num].virt; 33f68c0654SThomas Gleixner } 34f68c0654SThomas Gleixner 35f68c0654SThomas Gleixner /* Fall back to type 0 */ 36f68c0654SThomas Gleixner return NULL; 37f68c0654SThomas Gleixner } 38f68c0654SThomas Gleixner 39f68c0654SThomas Gleixner static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn) 40f68c0654SThomas Gleixner { 41f68c0654SThomas Gleixner char __iomem *addr; 42a0ca9909SIvan Kokshaysky 43f68c0654SThomas Gleixner addr = get_virt(seg, bus); 44f68c0654SThomas Gleixner if (!addr) 45f68c0654SThomas Gleixner return NULL; 46df5eb1d6SBjorn Helgaas return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12)); 47f68c0654SThomas Gleixner } 48f68c0654SThomas Gleixner 49f68c0654SThomas Gleixner static int pci_mmcfg_read(unsigned int seg, unsigned int bus, 50f68c0654SThomas Gleixner unsigned int devfn, int reg, int len, u32 *value) 51f68c0654SThomas Gleixner { 52f68c0654SThomas Gleixner char __iomem *addr; 53f68c0654SThomas Gleixner 54f68c0654SThomas Gleixner /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ 55f68c0654SThomas Gleixner if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { 56a0ca9909SIvan Kokshaysky err: *value = -1; 57f68c0654SThomas Gleixner return -EINVAL; 58f68c0654SThomas Gleixner } 59f68c0654SThomas Gleixner 60f68c0654SThomas Gleixner addr = pci_dev_base(seg, bus, devfn); 61f68c0654SThomas Gleixner if (!addr) 62a0ca9909SIvan Kokshaysky goto err; 63f68c0654SThomas Gleixner 64f68c0654SThomas Gleixner switch (len) { 65f68c0654SThomas Gleixner case 1: 66f68c0654SThomas Gleixner *value = mmio_config_readb(addr + reg); 67f68c0654SThomas Gleixner break; 68f68c0654SThomas Gleixner case 2: 69f68c0654SThomas Gleixner *value = mmio_config_readw(addr + reg); 70f68c0654SThomas Gleixner break; 71f68c0654SThomas Gleixner case 4: 72f68c0654SThomas Gleixner *value = mmio_config_readl(addr + reg); 73f68c0654SThomas Gleixner break; 74f68c0654SThomas Gleixner } 75f68c0654SThomas Gleixner 76f68c0654SThomas Gleixner return 0; 77f68c0654SThomas Gleixner } 78f68c0654SThomas Gleixner 79f68c0654SThomas Gleixner static int pci_mmcfg_write(unsigned int seg, unsigned int bus, 80f68c0654SThomas Gleixner unsigned int devfn, int reg, int len, u32 value) 81f68c0654SThomas Gleixner { 82f68c0654SThomas Gleixner char __iomem *addr; 83f68c0654SThomas Gleixner 84f68c0654SThomas Gleixner /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ 85f68c0654SThomas Gleixner if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) 86f68c0654SThomas Gleixner return -EINVAL; 87f68c0654SThomas Gleixner 88f68c0654SThomas Gleixner addr = pci_dev_base(seg, bus, devfn); 89f68c0654SThomas Gleixner if (!addr) 90a0ca9909SIvan Kokshaysky return -EINVAL; 91f68c0654SThomas Gleixner 92f68c0654SThomas Gleixner switch (len) { 93f68c0654SThomas Gleixner case 1: 94f68c0654SThomas Gleixner mmio_config_writeb(addr + reg, value); 95f68c0654SThomas Gleixner break; 96f68c0654SThomas Gleixner case 2: 97f68c0654SThomas Gleixner mmio_config_writew(addr + reg, value); 98f68c0654SThomas Gleixner break; 99f68c0654SThomas Gleixner case 4: 100f68c0654SThomas Gleixner mmio_config_writel(addr + reg, value); 101f68c0654SThomas Gleixner break; 102f68c0654SThomas Gleixner } 103f68c0654SThomas Gleixner 104f68c0654SThomas Gleixner return 0; 105f68c0654SThomas Gleixner } 106f68c0654SThomas Gleixner 107f68c0654SThomas Gleixner static struct pci_raw_ops pci_mmcfg = { 108f68c0654SThomas Gleixner .read = pci_mmcfg_read, 109f68c0654SThomas Gleixner .write = pci_mmcfg_write, 110f68c0654SThomas Gleixner }; 111f68c0654SThomas Gleixner 112d215a9c8SBjorn Helgaas static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg) 113f68c0654SThomas Gleixner { 114f68c0654SThomas Gleixner void __iomem *addr; 115068258bcSYinghai Lu u64 start, size; 116df5eb1d6SBjorn Helgaas int num_buses; 117f68c0654SThomas Gleixner 118*d7e6b66fSBjorn Helgaas start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus); 119*d7e6b66fSBjorn Helgaas num_buses = cfg->end_bus - cfg->start_bus + 1; 120df5eb1d6SBjorn Helgaas size = PCI_MMCFG_BUS_OFFSET(num_buses); 121068258bcSYinghai Lu addr = ioremap_nocache(start, size); 122f68c0654SThomas Gleixner if (addr) { 123f68c0654SThomas Gleixner printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n", 124068258bcSYinghai Lu start, start + size - 1); 125*d7e6b66fSBjorn Helgaas addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus); 126f68c0654SThomas Gleixner } 127f68c0654SThomas Gleixner return addr; 128f68c0654SThomas Gleixner } 129f68c0654SThomas Gleixner 130f68c0654SThomas Gleixner int __init pci_mmcfg_arch_init(void) 131f68c0654SThomas Gleixner { 132f68c0654SThomas Gleixner int i; 1330b64ad71SYinghai Lu pci_mmcfg_virt = kzalloc(sizeof(*pci_mmcfg_virt) * 134f68c0654SThomas Gleixner pci_mmcfg_config_num, GFP_KERNEL); 135f68c0654SThomas Gleixner if (pci_mmcfg_virt == NULL) { 136f68c0654SThomas Gleixner printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); 137f68c0654SThomas Gleixner return 0; 138f68c0654SThomas Gleixner } 139f68c0654SThomas Gleixner 140f68c0654SThomas Gleixner for (i = 0; i < pci_mmcfg_config_num; ++i) { 141f68c0654SThomas Gleixner pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; 142f68c0654SThomas Gleixner pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); 143f68c0654SThomas Gleixner if (!pci_mmcfg_virt[i].virt) { 144f68c0654SThomas Gleixner printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " 145f68c0654SThomas Gleixner "segment %d\n", 146*d7e6b66fSBjorn Helgaas pci_mmcfg_config[i].segment); 1470b64ad71SYinghai Lu pci_mmcfg_arch_free(); 148f68c0654SThomas Gleixner return 0; 149f68c0654SThomas Gleixner } 150f68c0654SThomas Gleixner } 151b6ce068aSMatthew Wilcox raw_pci_ext_ops = &pci_mmcfg; 152f68c0654SThomas Gleixner return 1; 153f68c0654SThomas Gleixner } 1540b64ad71SYinghai Lu 1550b64ad71SYinghai Lu void __init pci_mmcfg_arch_free(void) 1560b64ad71SYinghai Lu { 1570b64ad71SYinghai Lu int i; 1580b64ad71SYinghai Lu 1590b64ad71SYinghai Lu if (pci_mmcfg_virt == NULL) 1600b64ad71SYinghai Lu return; 1610b64ad71SYinghai Lu 1620b64ad71SYinghai Lu for (i = 0; i < pci_mmcfg_config_num; ++i) { 1630b64ad71SYinghai Lu if (pci_mmcfg_virt[i].virt) { 164*d7e6b66fSBjorn Helgaas iounmap(pci_mmcfg_virt[i].virt + PCI_MMCFG_BUS_OFFSET(pci_mmcfg_virt[i].cfg->start_bus)); 1650b64ad71SYinghai Lu pci_mmcfg_virt[i].virt = NULL; 1660b64ad71SYinghai Lu pci_mmcfg_virt[i].cfg = NULL; 1670b64ad71SYinghai Lu } 1680b64ad71SYinghai Lu } 1690b64ad71SYinghai Lu 1700b64ad71SYinghai Lu kfree(pci_mmcfg_virt); 1710b64ad71SYinghai Lu pci_mmcfg_virt = NULL; 1720b64ad71SYinghai Lu } 173