xref: /openbmc/linux/arch/x86/pci/mmconfig_64.c (revision 643d1f7f)
1 /*
2  * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
3  *
4  * This is an 64bit optimized version that always keeps the full mmconfig
5  * space mapped. This allows lockless config space operation.
6  */
7 
8 #include <linux/pci.h>
9 #include <linux/init.h>
10 #include <linux/acpi.h>
11 #include <linux/bitmap.h>
12 #include <asm/e820.h>
13 
14 #include "pci.h"
15 
16 /* Static virtual mapping of the MMCONFIG aperture */
17 struct mmcfg_virt {
18 	struct acpi_mcfg_allocation *cfg;
19 	char __iomem *virt;
20 };
21 static struct mmcfg_virt *pci_mmcfg_virt;
22 
23 static char __iomem *get_virt(unsigned int seg, unsigned bus)
24 {
25 	struct acpi_mcfg_allocation *cfg;
26 	int cfg_num;
27 
28 	for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) {
29 		cfg = pci_mmcfg_virt[cfg_num].cfg;
30 		if (cfg->pci_segment == seg &&
31 		    (cfg->start_bus_number <= bus) &&
32 		    (cfg->end_bus_number >= bus))
33 			return pci_mmcfg_virt[cfg_num].virt;
34 	}
35 
36 	/* Fall back to type 0 */
37 	return NULL;
38 }
39 
40 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
41 {
42 	char __iomem *addr;
43 	if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
44 		test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
45 		return NULL;
46 	addr = get_virt(seg, bus);
47 	if (!addr)
48 		return NULL;
49  	return addr + ((bus << 20) | (devfn << 12));
50 }
51 
52 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
53 			  unsigned int devfn, int reg, int len, u32 *value)
54 {
55 	char __iomem *addr;
56 
57 	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
58 	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
59 		*value = -1;
60 		return -EINVAL;
61 	}
62 
63 	addr = pci_dev_base(seg, bus, devfn);
64 	if (!addr)
65 		return pci_conf1_read(seg,bus,devfn,reg,len,value);
66 
67 	switch (len) {
68 	case 1:
69 		*value = mmio_config_readb(addr + reg);
70 		break;
71 	case 2:
72 		*value = mmio_config_readw(addr + reg);
73 		break;
74 	case 4:
75 		*value = mmio_config_readl(addr + reg);
76 		break;
77 	}
78 
79 	return 0;
80 }
81 
82 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
83 			   unsigned int devfn, int reg, int len, u32 value)
84 {
85 	char __iomem *addr;
86 
87 	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
88 	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
89 		return -EINVAL;
90 
91 	addr = pci_dev_base(seg, bus, devfn);
92 	if (!addr)
93 		return pci_conf1_write(seg,bus,devfn,reg,len,value);
94 
95 	switch (len) {
96 	case 1:
97 		mmio_config_writeb(addr + reg, value);
98 		break;
99 	case 2:
100 		mmio_config_writew(addr + reg, value);
101 		break;
102 	case 4:
103 		mmio_config_writel(addr + reg, value);
104 		break;
105 	}
106 
107 	return 0;
108 }
109 
110 static struct pci_raw_ops pci_mmcfg = {
111 	.read =		pci_mmcfg_read,
112 	.write =	pci_mmcfg_write,
113 };
114 
115 static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
116 {
117 	void __iomem *addr;
118 	u32 size;
119 
120 	size = (cfg->end_bus_number + 1) << 20;
121 	addr = ioremap_nocache(cfg->address, size);
122 	if (addr) {
123 		printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
124 		       cfg->address, cfg->address + size - 1);
125 	}
126 	return addr;
127 }
128 
129 int __init pci_mmcfg_arch_reachable(unsigned int seg, unsigned int bus,
130 				    unsigned int devfn)
131 {
132 	return pci_dev_base(seg, bus, devfn) != NULL;
133 }
134 
135 int __init pci_mmcfg_arch_init(void)
136 {
137 	int i;
138 	pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
139 				 pci_mmcfg_config_num, GFP_KERNEL);
140 	if (pci_mmcfg_virt == NULL) {
141 		printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
142 		return 0;
143 	}
144 
145 	for (i = 0; i < pci_mmcfg_config_num; ++i) {
146 		pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
147 		pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
148 		if (!pci_mmcfg_virt[i].virt) {
149 			printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
150 					"segment %d\n",
151 				pci_mmcfg_config[i].pci_segment);
152 			return 0;
153 		}
154 	}
155 	raw_pci_ops = &pci_mmcfg;
156 	return 1;
157 }
158