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> 13*82487711SJaswinder Singh Rajput #include <asm/pci_x86.h> 14f68c0654SThomas Gleixner 15f68c0654SThomas Gleixner /* Static virtual mapping of the MMCONFIG aperture */ 16f68c0654SThomas Gleixner struct mmcfg_virt { 17f68c0654SThomas Gleixner struct acpi_mcfg_allocation *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 { 24f68c0654SThomas Gleixner struct acpi_mcfg_allocation *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; 29f68c0654SThomas Gleixner if (cfg->pci_segment == seg && 30f68c0654SThomas Gleixner (cfg->start_bus_number <= bus) && 31f68c0654SThomas Gleixner (cfg->end_bus_number >= 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; 46f68c0654SThomas Gleixner return addr + ((bus << 20) | (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 112f68c0654SThomas Gleixner static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg) 113f68c0654SThomas Gleixner { 114f68c0654SThomas Gleixner void __iomem *addr; 115f68c0654SThomas Gleixner u32 size; 116f68c0654SThomas Gleixner 117f68c0654SThomas Gleixner size = (cfg->end_bus_number + 1) << 20; 118f68c0654SThomas Gleixner addr = ioremap_nocache(cfg->address, size); 119f68c0654SThomas Gleixner if (addr) { 120f68c0654SThomas Gleixner printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n", 121f68c0654SThomas Gleixner cfg->address, cfg->address + size - 1); 122f68c0654SThomas Gleixner } 123f68c0654SThomas Gleixner return addr; 124f68c0654SThomas Gleixner } 125f68c0654SThomas Gleixner 126f68c0654SThomas Gleixner int __init pci_mmcfg_arch_init(void) 127f68c0654SThomas Gleixner { 128f68c0654SThomas Gleixner int i; 1290b64ad71SYinghai Lu pci_mmcfg_virt = kzalloc(sizeof(*pci_mmcfg_virt) * 130f68c0654SThomas Gleixner pci_mmcfg_config_num, GFP_KERNEL); 131f68c0654SThomas Gleixner if (pci_mmcfg_virt == NULL) { 132f68c0654SThomas Gleixner printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); 133f68c0654SThomas Gleixner return 0; 134f68c0654SThomas Gleixner } 135f68c0654SThomas Gleixner 136f68c0654SThomas Gleixner for (i = 0; i < pci_mmcfg_config_num; ++i) { 137f68c0654SThomas Gleixner pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; 138f68c0654SThomas Gleixner pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); 139f68c0654SThomas Gleixner if (!pci_mmcfg_virt[i].virt) { 140f68c0654SThomas Gleixner printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " 141f68c0654SThomas Gleixner "segment %d\n", 142f68c0654SThomas Gleixner pci_mmcfg_config[i].pci_segment); 1430b64ad71SYinghai Lu pci_mmcfg_arch_free(); 144f68c0654SThomas Gleixner return 0; 145f68c0654SThomas Gleixner } 146f68c0654SThomas Gleixner } 147b6ce068aSMatthew Wilcox raw_pci_ext_ops = &pci_mmcfg; 148f68c0654SThomas Gleixner return 1; 149f68c0654SThomas Gleixner } 1500b64ad71SYinghai Lu 1510b64ad71SYinghai Lu void __init pci_mmcfg_arch_free(void) 1520b64ad71SYinghai Lu { 1530b64ad71SYinghai Lu int i; 1540b64ad71SYinghai Lu 1550b64ad71SYinghai Lu if (pci_mmcfg_virt == NULL) 1560b64ad71SYinghai Lu return; 1570b64ad71SYinghai Lu 1580b64ad71SYinghai Lu for (i = 0; i < pci_mmcfg_config_num; ++i) { 1590b64ad71SYinghai Lu if (pci_mmcfg_virt[i].virt) { 1600b64ad71SYinghai Lu iounmap(pci_mmcfg_virt[i].virt); 1610b64ad71SYinghai Lu pci_mmcfg_virt[i].virt = NULL; 1620b64ad71SYinghai Lu pci_mmcfg_virt[i].cfg = NULL; 1630b64ad71SYinghai Lu } 1640b64ad71SYinghai Lu } 1650b64ad71SYinghai Lu 1660b64ad71SYinghai Lu kfree(pci_mmcfg_virt); 1670b64ad71SYinghai Lu pci_mmcfg_virt = NULL; 1680b64ad71SYinghai Lu } 169