xref: /openbmc/linux/arch/x86/pci/mmconfig_64.c (revision c0fa40784cce9cc66b54499a3762cfe07e35353f)
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>
12376f70acSJiang Liu #include <linux/rcupdate.h>
13f68c0654SThomas Gleixner #include <asm/e820.h>
1482487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
15f68c0654SThomas Gleixner 
168c57786aSBjorn Helgaas #define PREFIX "PCI: "
178c57786aSBjorn Helgaas 
18f68c0654SThomas Gleixner static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
19f68c0654SThomas Gleixner {
20f6e1d8ccSBjorn Helgaas 	struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
21a0ca9909SIvan Kokshaysky 
22f6e1d8ccSBjorn Helgaas 	if (cfg && cfg->virt)
23f6e1d8ccSBjorn Helgaas 		return cfg->virt + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
24f68c0654SThomas Gleixner 	return NULL;
25f68c0654SThomas Gleixner }
26f68c0654SThomas Gleixner 
27f68c0654SThomas Gleixner static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
28f68c0654SThomas Gleixner 			  unsigned int devfn, int reg, int len, u32 *value)
29f68c0654SThomas Gleixner {
30f68c0654SThomas Gleixner 	char __iomem *addr;
31f68c0654SThomas Gleixner 
32f68c0654SThomas Gleixner 	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
33f68c0654SThomas Gleixner 	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
34a0ca9909SIvan Kokshaysky err:		*value = -1;
35f68c0654SThomas Gleixner 		return -EINVAL;
36f68c0654SThomas Gleixner 	}
37f68c0654SThomas Gleixner 
38376f70acSJiang Liu 	rcu_read_lock();
39f68c0654SThomas Gleixner 	addr = pci_dev_base(seg, bus, devfn);
40376f70acSJiang Liu 	if (!addr) {
41376f70acSJiang Liu 		rcu_read_unlock();
42a0ca9909SIvan Kokshaysky 		goto err;
43376f70acSJiang Liu 	}
44f68c0654SThomas Gleixner 
45f68c0654SThomas Gleixner 	switch (len) {
46f68c0654SThomas Gleixner 	case 1:
47f68c0654SThomas Gleixner 		*value = mmio_config_readb(addr + reg);
48f68c0654SThomas Gleixner 		break;
49f68c0654SThomas Gleixner 	case 2:
50f68c0654SThomas Gleixner 		*value = mmio_config_readw(addr + reg);
51f68c0654SThomas Gleixner 		break;
52f68c0654SThomas Gleixner 	case 4:
53f68c0654SThomas Gleixner 		*value = mmio_config_readl(addr + reg);
54f68c0654SThomas Gleixner 		break;
55f68c0654SThomas Gleixner 	}
56376f70acSJiang Liu 	rcu_read_unlock();
57f68c0654SThomas Gleixner 
58f68c0654SThomas Gleixner 	return 0;
59f68c0654SThomas Gleixner }
60f68c0654SThomas Gleixner 
61f68c0654SThomas Gleixner static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
62f68c0654SThomas Gleixner 			   unsigned int devfn, int reg, int len, u32 value)
63f68c0654SThomas Gleixner {
64f68c0654SThomas Gleixner 	char __iomem *addr;
65f68c0654SThomas Gleixner 
66f68c0654SThomas Gleixner 	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
67f68c0654SThomas Gleixner 	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
68f68c0654SThomas Gleixner 		return -EINVAL;
69f68c0654SThomas Gleixner 
70376f70acSJiang Liu 	rcu_read_lock();
71f68c0654SThomas Gleixner 	addr = pci_dev_base(seg, bus, devfn);
72376f70acSJiang Liu 	if (!addr) {
73376f70acSJiang Liu 		rcu_read_unlock();
74a0ca9909SIvan Kokshaysky 		return -EINVAL;
75376f70acSJiang Liu 	}
76f68c0654SThomas Gleixner 
77f68c0654SThomas Gleixner 	switch (len) {
78f68c0654SThomas Gleixner 	case 1:
79f68c0654SThomas Gleixner 		mmio_config_writeb(addr + reg, value);
80f68c0654SThomas Gleixner 		break;
81f68c0654SThomas Gleixner 	case 2:
82f68c0654SThomas Gleixner 		mmio_config_writew(addr + reg, value);
83f68c0654SThomas Gleixner 		break;
84f68c0654SThomas Gleixner 	case 4:
85f68c0654SThomas Gleixner 		mmio_config_writel(addr + reg, value);
86f68c0654SThomas Gleixner 		break;
87f68c0654SThomas Gleixner 	}
88376f70acSJiang Liu 	rcu_read_unlock();
89f68c0654SThomas Gleixner 
90f68c0654SThomas Gleixner 	return 0;
91f68c0654SThomas Gleixner }
92f68c0654SThomas Gleixner 
93*c0fa4078SJiang Liu const struct pci_raw_ops pci_mmcfg = {
94f68c0654SThomas Gleixner 	.read =		pci_mmcfg_read,
95f68c0654SThomas Gleixner 	.write =	pci_mmcfg_write,
96f68c0654SThomas Gleixner };
97f68c0654SThomas Gleixner 
989cf0105dSJiang Liu static void __iomem * __devinit mcfg_ioremap(struct pci_mmcfg_region *cfg)
99f68c0654SThomas Gleixner {
100f68c0654SThomas Gleixner 	void __iomem *addr;
101068258bcSYinghai Lu 	u64 start, size;
102df5eb1d6SBjorn Helgaas 	int num_buses;
103f68c0654SThomas Gleixner 
104d7e6b66fSBjorn Helgaas 	start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
105d7e6b66fSBjorn Helgaas 	num_buses = cfg->end_bus - cfg->start_bus + 1;
106df5eb1d6SBjorn Helgaas 	size = PCI_MMCFG_BUS_OFFSET(num_buses);
107068258bcSYinghai Lu 	addr = ioremap_nocache(start, size);
1088c57786aSBjorn Helgaas 	if (addr)
109d7e6b66fSBjorn Helgaas 		addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
110f68c0654SThomas Gleixner 	return addr;
111f68c0654SThomas Gleixner }
112f68c0654SThomas Gleixner 
113f68c0654SThomas Gleixner int __init pci_mmcfg_arch_init(void)
114f68c0654SThomas Gleixner {
1153f0f5503SBjorn Helgaas 	struct pci_mmcfg_region *cfg;
116f68c0654SThomas Gleixner 
1179cf0105dSJiang Liu 	list_for_each_entry(cfg, &pci_mmcfg_list, list)
1189cf0105dSJiang Liu 		if (pci_mmcfg_arch_map(cfg)) {
1190b64ad71SYinghai Lu 			pci_mmcfg_arch_free();
120f68c0654SThomas Gleixner 			return 0;
121f68c0654SThomas Gleixner 		}
1229cf0105dSJiang Liu 
123b6ce068aSMatthew Wilcox 	raw_pci_ext_ops = &pci_mmcfg;
1249cf0105dSJiang Liu 
125f68c0654SThomas Gleixner 	return 1;
126f68c0654SThomas Gleixner }
1270b64ad71SYinghai Lu 
1280b64ad71SYinghai Lu void __init pci_mmcfg_arch_free(void)
1290b64ad71SYinghai Lu {
1303f0f5503SBjorn Helgaas 	struct pci_mmcfg_region *cfg;
1310b64ad71SYinghai Lu 
1329cf0105dSJiang Liu 	list_for_each_entry(cfg, &pci_mmcfg_list, list)
1339cf0105dSJiang Liu 		pci_mmcfg_arch_unmap(cfg);
1349cf0105dSJiang Liu }
1359cf0105dSJiang Liu 
1369cf0105dSJiang Liu int __devinit pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg)
1379cf0105dSJiang Liu {
1389cf0105dSJiang Liu 	cfg->virt = mcfg_ioremap(cfg);
1399cf0105dSJiang Liu 	if (!cfg->virt) {
1409cf0105dSJiang Liu 		printk(KERN_ERR PREFIX "can't map MMCONFIG at %pR\n",
1419cf0105dSJiang Liu 		       &cfg->res);
1429cf0105dSJiang Liu 		return -ENOMEM;
1439cf0105dSJiang Liu 	}
1449cf0105dSJiang Liu 
1459cf0105dSJiang Liu 	return 0;
1469cf0105dSJiang Liu }
1479cf0105dSJiang Liu 
1489cf0105dSJiang Liu void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg)
1499cf0105dSJiang Liu {
1509cf0105dSJiang Liu 	if (cfg && cfg->virt) {
1513f0f5503SBjorn Helgaas 		iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));
1523f0f5503SBjorn Helgaas 		cfg->virt = NULL;
1530b64ad71SYinghai Lu 	}
1540b64ad71SYinghai Lu }
155