xref: /openbmc/linux/arch/x86/pci/mmconfig_64.c (revision 8c57786ad3d921713c7ad8e44132aa537a1d0fec)
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 
15*8c57786aSBjorn Helgaas #define PREFIX "PCI: "
16*8c57786aSBjorn Helgaas 
17f68c0654SThomas Gleixner static char __iomem *get_virt(unsigned int seg, unsigned bus)
18f68c0654SThomas Gleixner {
19d215a9c8SBjorn Helgaas 	struct pci_mmcfg_region *cfg;
20f68c0654SThomas Gleixner 
21ff097dddSBjorn Helgaas 	list_for_each_entry(cfg, &pci_mmcfg_list, list)
22d7e6b66fSBjorn Helgaas 		if (cfg->segment == seg &&
23d7e6b66fSBjorn Helgaas 		    (cfg->start_bus <= bus) &&
24d7e6b66fSBjorn Helgaas 		    (cfg->end_bus >= bus))
253f0f5503SBjorn Helgaas 			return cfg->virt;
26f68c0654SThomas Gleixner 
27f68c0654SThomas Gleixner 	/* Fall back to type 0 */
28f68c0654SThomas Gleixner 	return NULL;
29f68c0654SThomas Gleixner }
30f68c0654SThomas Gleixner 
31f68c0654SThomas Gleixner static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
32f68c0654SThomas Gleixner {
33f68c0654SThomas Gleixner 	char __iomem *addr;
34a0ca9909SIvan Kokshaysky 
35f68c0654SThomas Gleixner 	addr = get_virt(seg, bus);
36f68c0654SThomas Gleixner 	if (!addr)
37f68c0654SThomas Gleixner 		return NULL;
38df5eb1d6SBjorn Helgaas 	return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
39f68c0654SThomas Gleixner }
40f68c0654SThomas Gleixner 
41f68c0654SThomas Gleixner static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
42f68c0654SThomas Gleixner 			  unsigned int devfn, int reg, int len, u32 *value)
43f68c0654SThomas Gleixner {
44f68c0654SThomas Gleixner 	char __iomem *addr;
45f68c0654SThomas Gleixner 
46f68c0654SThomas Gleixner 	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
47f68c0654SThomas Gleixner 	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
48a0ca9909SIvan Kokshaysky err:		*value = -1;
49f68c0654SThomas Gleixner 		return -EINVAL;
50f68c0654SThomas Gleixner 	}
51f68c0654SThomas Gleixner 
52f68c0654SThomas Gleixner 	addr = pci_dev_base(seg, bus, devfn);
53f68c0654SThomas Gleixner 	if (!addr)
54a0ca9909SIvan Kokshaysky 		goto err;
55f68c0654SThomas Gleixner 
56f68c0654SThomas Gleixner 	switch (len) {
57f68c0654SThomas Gleixner 	case 1:
58f68c0654SThomas Gleixner 		*value = mmio_config_readb(addr + reg);
59f68c0654SThomas Gleixner 		break;
60f68c0654SThomas Gleixner 	case 2:
61f68c0654SThomas Gleixner 		*value = mmio_config_readw(addr + reg);
62f68c0654SThomas Gleixner 		break;
63f68c0654SThomas Gleixner 	case 4:
64f68c0654SThomas Gleixner 		*value = mmio_config_readl(addr + reg);
65f68c0654SThomas Gleixner 		break;
66f68c0654SThomas Gleixner 	}
67f68c0654SThomas Gleixner 
68f68c0654SThomas Gleixner 	return 0;
69f68c0654SThomas Gleixner }
70f68c0654SThomas Gleixner 
71f68c0654SThomas Gleixner static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
72f68c0654SThomas Gleixner 			   unsigned int devfn, int reg, int len, u32 value)
73f68c0654SThomas Gleixner {
74f68c0654SThomas Gleixner 	char __iomem *addr;
75f68c0654SThomas Gleixner 
76f68c0654SThomas Gleixner 	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
77f68c0654SThomas Gleixner 	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
78f68c0654SThomas Gleixner 		return -EINVAL;
79f68c0654SThomas Gleixner 
80f68c0654SThomas Gleixner 	addr = pci_dev_base(seg, bus, devfn);
81f68c0654SThomas Gleixner 	if (!addr)
82a0ca9909SIvan Kokshaysky 		return -EINVAL;
83f68c0654SThomas Gleixner 
84f68c0654SThomas Gleixner 	switch (len) {
85f68c0654SThomas Gleixner 	case 1:
86f68c0654SThomas Gleixner 		mmio_config_writeb(addr + reg, value);
87f68c0654SThomas Gleixner 		break;
88f68c0654SThomas Gleixner 	case 2:
89f68c0654SThomas Gleixner 		mmio_config_writew(addr + reg, value);
90f68c0654SThomas Gleixner 		break;
91f68c0654SThomas Gleixner 	case 4:
92f68c0654SThomas Gleixner 		mmio_config_writel(addr + reg, value);
93f68c0654SThomas Gleixner 		break;
94f68c0654SThomas Gleixner 	}
95f68c0654SThomas Gleixner 
96f68c0654SThomas Gleixner 	return 0;
97f68c0654SThomas Gleixner }
98f68c0654SThomas Gleixner 
99f68c0654SThomas Gleixner static struct pci_raw_ops pci_mmcfg = {
100f68c0654SThomas Gleixner 	.read =		pci_mmcfg_read,
101f68c0654SThomas Gleixner 	.write =	pci_mmcfg_write,
102f68c0654SThomas Gleixner };
103f68c0654SThomas Gleixner 
104d215a9c8SBjorn Helgaas static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg)
105f68c0654SThomas Gleixner {
106f68c0654SThomas Gleixner 	void __iomem *addr;
107068258bcSYinghai Lu 	u64 start, size;
108df5eb1d6SBjorn Helgaas 	int num_buses;
109f68c0654SThomas Gleixner 
110d7e6b66fSBjorn Helgaas 	start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
111d7e6b66fSBjorn Helgaas 	num_buses = cfg->end_bus - cfg->start_bus + 1;
112df5eb1d6SBjorn Helgaas 	size = PCI_MMCFG_BUS_OFFSET(num_buses);
113068258bcSYinghai Lu 	addr = ioremap_nocache(start, size);
114*8c57786aSBjorn Helgaas 	if (addr)
115d7e6b66fSBjorn Helgaas 		addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
116f68c0654SThomas Gleixner 	return addr;
117f68c0654SThomas Gleixner }
118f68c0654SThomas Gleixner 
119f68c0654SThomas Gleixner int __init pci_mmcfg_arch_init(void)
120f68c0654SThomas Gleixner {
1213f0f5503SBjorn Helgaas 	struct pci_mmcfg_region *cfg;
122f68c0654SThomas Gleixner 
123ff097dddSBjorn Helgaas 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
1243f0f5503SBjorn Helgaas 		cfg->virt = mcfg_ioremap(cfg);
1253f0f5503SBjorn Helgaas 		if (!cfg->virt) {
126*8c57786aSBjorn Helgaas 			printk(KERN_ERR PREFIX "can't map MMCONFIG at %pR\n",
127*8c57786aSBjorn Helgaas 			       &cfg->res);
1280b64ad71SYinghai Lu 			pci_mmcfg_arch_free();
129f68c0654SThomas Gleixner 			return 0;
130f68c0654SThomas Gleixner 		}
131f68c0654SThomas Gleixner 	}
132b6ce068aSMatthew Wilcox 	raw_pci_ext_ops = &pci_mmcfg;
133f68c0654SThomas Gleixner 	return 1;
134f68c0654SThomas Gleixner }
1350b64ad71SYinghai Lu 
1360b64ad71SYinghai Lu void __init pci_mmcfg_arch_free(void)
1370b64ad71SYinghai Lu {
1383f0f5503SBjorn Helgaas 	struct pci_mmcfg_region *cfg;
1390b64ad71SYinghai Lu 
140ff097dddSBjorn Helgaas 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
1413f0f5503SBjorn Helgaas 		if (cfg->virt) {
1423f0f5503SBjorn Helgaas 			iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));
1433f0f5503SBjorn Helgaas 			cfg->virt = NULL;
1440b64ad71SYinghai Lu 		}
1450b64ad71SYinghai Lu 	}
1460b64ad71SYinghai Lu }
147