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