xref: /linux/arch/powerpc/kernel/pci_32.c (revision fbf46565c67c626849c7ce2a326972d3008d2a91)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Common pmac/prep/chrp pci routines. -- Cort
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/pci.h>
8 #include <linux/delay.h>
9 #include <linux/string.h>
10 #include <linux/init.h>
11 #include <linux/capability.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/memblock.h>
15 #include <linux/syscalls.h>
16 #include <linux/irq.h>
17 #include <linux/list.h>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/export.h>
21 
22 #include <asm/processor.h>
23 #include <asm/io.h>
24 #include <asm/sections.h>
25 #include <asm/pci-bridge.h>
26 #include <asm/ppc-pci.h>
27 #include <asm/byteorder.h>
28 #include <linux/uaccess.h>
29 #include <asm/machdep.h>
30 
31 #undef DEBUG
32 
33 unsigned long isa_io_base     = 0;
34 unsigned long pci_dram_offset = 0;
35 int pcibios_assign_bus_offset = 1;
36 EXPORT_SYMBOL(isa_io_base);
37 EXPORT_SYMBOL(pci_dram_offset);
38 
39 static void fixup_cpc710_pci64(struct pci_dev* dev);
40 
41 /* By default, we don't re-assign bus numbers. We do this only on
42  * some pmacs
43  */
44 static int pci_assign_all_buses;
45 
46 /* This will remain NULL for now, until isa-bridge.c is made common
47  * to both 32-bit and 64-bit.
48  */
49 struct pci_dev *isa_bridge_pcidev;
50 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
51 
52 static void
53 fixup_cpc710_pci64(struct pci_dev* dev)
54 {
55 	/* Hide the PCI64 BARs from the kernel as their content doesn't
56 	 * fit well in the resource management
57 	 */
58 	dev->resource[0].start = dev->resource[0].end = 0;
59 	dev->resource[0].flags = 0;
60 	dev->resource[1].start = dev->resource[1].end = 0;
61 	dev->resource[1].flags = 0;
62 }
63 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,	PCI_DEVICE_ID_IBM_CPC710_PCI64,	fixup_cpc710_pci64);
64 
65 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP)
66 
67 static u8* pci_to_OF_bus_map;
68 static int pci_bus_count;
69 
70 /*
71  * Functions below are used on OpenFirmware machines.
72  */
73 static void
74 make_one_node_map(struct device_node* node, u8 pci_bus)
75 {
76 	const int *bus_range;
77 	int len;
78 
79 	if (pci_bus >= pci_bus_count)
80 		return;
81 	bus_range = of_get_property(node, "bus-range", &len);
82 	if (bus_range == NULL || len < 2 * sizeof(int)) {
83 		printk(KERN_WARNING "Can't get bus-range for %pOF, "
84 		       "assuming it starts at 0\n", node);
85 		pci_to_OF_bus_map[pci_bus] = 0;
86 	} else
87 		pci_to_OF_bus_map[pci_bus] = bus_range[0];
88 
89 	for_each_child_of_node(node, node) {
90 		struct pci_dev* dev;
91 		const unsigned int *class_code, *reg;
92 
93 		class_code = of_get_property(node, "class-code", NULL);
94 		if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
95 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
96 			continue;
97 		reg = of_get_property(node, "reg", NULL);
98 		if (!reg)
99 			continue;
100 		dev = pci_get_domain_bus_and_slot(0, pci_bus,
101 						  ((reg[0] >> 8) & 0xff));
102 		if (!dev || !dev->subordinate) {
103 			pci_dev_put(dev);
104 			continue;
105 		}
106 		make_one_node_map(node, dev->subordinate->number);
107 		pci_dev_put(dev);
108 	}
109 }
110 
111 static void __init
112 pcibios_make_OF_bus_map(void)
113 {
114 	int i;
115 	struct pci_controller *hose, *tmp;
116 	struct property *map_prop;
117 	struct device_node *dn;
118 
119 	pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
120 	if (!pci_to_OF_bus_map) {
121 		printk(KERN_ERR "Can't allocate OF bus map !\n");
122 		return;
123 	}
124 
125 	/* We fill the bus map with invalid values, that helps
126 	 * debugging.
127 	 */
128 	for (i=0; i<pci_bus_count; i++)
129 		pci_to_OF_bus_map[i] = 0xff;
130 
131 	/* For each hose, we begin searching bridges */
132 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
133 		struct device_node* node = hose->dn;
134 
135 		if (!node)
136 			continue;
137 		make_one_node_map(node, hose->first_busno);
138 	}
139 	dn = of_find_node_by_path("/");
140 	map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
141 	if (map_prop) {
142 		BUG_ON(pci_bus_count > map_prop->length);
143 		memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
144 	}
145 	of_node_put(dn);
146 #ifdef DEBUG
147 	printk("PCI->OF bus map:\n");
148 	for (i=0; i<pci_bus_count; i++) {
149 		if (pci_to_OF_bus_map[i] == 0xff)
150 			continue;
151 		printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
152 	}
153 #endif
154 }
155 
156 
157 #ifdef CONFIG_PPC_PMAC
158 /*
159  * Returns the PCI device matching a given OF node
160  */
161 int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
162 {
163 	struct pci_dev *dev = NULL;
164 	const __be32 *reg;
165 	int size;
166 
167 	/* Check if it might have a chance to be a PCI device */
168 	if (!pci_find_hose_for_OF_device(node))
169 		return -ENODEV;
170 
171 	reg = of_get_property(node, "reg", &size);
172 	if (!reg || size < 5 * sizeof(u32))
173 		return -ENODEV;
174 
175 	*bus = (be32_to_cpup(&reg[0]) >> 16) & 0xff;
176 	*devfn = (be32_to_cpup(&reg[0]) >> 8) & 0xff;
177 
178 	/* Ok, here we need some tweak. If we have already renumbered
179 	 * all busses, we can't rely on the OF bus number any more.
180 	 * the pci_to_OF_bus_map is not enough as several PCI busses
181 	 * may match the same OF bus number.
182 	 */
183 	if (!pci_to_OF_bus_map)
184 		return 0;
185 
186 	for_each_pci_dev(dev)
187 		if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
188 				dev->devfn == *devfn) {
189 			*bus = dev->bus->number;
190 			pci_dev_put(dev);
191 			return 0;
192 		}
193 
194 	return -ENODEV;
195 }
196 EXPORT_SYMBOL(pci_device_from_OF_node);
197 #endif
198 
199 #ifdef CONFIG_PPC_CHRP
200 /* We create the "pci-OF-bus-map" property now so it appears in the
201  * /proc device tree
202  */
203 void __init
204 pci_create_OF_bus_map(void)
205 {
206 	struct property* of_prop;
207 	struct device_node *dn;
208 
209 	of_prop = memblock_alloc(sizeof(struct property) + 256,
210 				 SMP_CACHE_BYTES);
211 	if (!of_prop)
212 		panic("%s: Failed to allocate %zu bytes\n", __func__,
213 		      sizeof(struct property) + 256);
214 	dn = of_find_node_by_path("/");
215 	if (dn) {
216 		memset(of_prop, -1, sizeof(struct property) + 256);
217 		of_prop->name = "pci-OF-bus-map";
218 		of_prop->length = 256;
219 		of_prop->value = &of_prop[1];
220 		of_add_property(dn, of_prop);
221 		of_node_put(dn);
222 	}
223 }
224 #endif
225 
226 #endif /* defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP) */
227 
228 void pcibios_setup_phb_io_space(struct pci_controller *hose)
229 {
230 	unsigned long io_offset;
231 	struct resource *res = &hose->io_resource;
232 
233 	/* Fixup IO space offset */
234 	io_offset = pcibios_io_space_offset(hose);
235 	res->start += io_offset;
236 	res->end += io_offset;
237 }
238 
239 static int __init pcibios_init(void)
240 {
241 	struct pci_controller *hose, *tmp;
242 #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
243 	int next_busno = 0;
244 #endif
245 
246 	printk(KERN_INFO "PCI: Probing PCI hardware\n");
247 
248 #ifdef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
249 	/*
250 	 * Enable PCI domains in /proc when PCI bus numbers are not unique
251 	 * across all PCI domains to prevent conflicts. And keep PCI domain 0
252 	 * backward compatible in /proc for video cards.
253 	 */
254 	pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
255 #endif
256 
257 	if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
258 		pci_assign_all_buses = 1;
259 
260 	/* Scan all of the recorded PCI controllers.  */
261 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
262 #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
263 		if (pci_assign_all_buses)
264 			hose->first_busno = next_busno;
265 #endif
266 		hose->last_busno = 0xff;
267 		pcibios_scan_phb(hose);
268 		pci_bus_add_devices(hose->bus);
269 #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
270 		if (pci_assign_all_buses || next_busno <= hose->last_busno)
271 			next_busno = hose->last_busno + pcibios_assign_bus_offset;
272 #endif
273 	}
274 
275 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP)
276 	pci_bus_count = next_busno;
277 
278 	/* OpenFirmware based machines need a map of OF bus
279 	 * numbers vs. kernel bus numbers since we may have to
280 	 * remap them.
281 	 */
282 	if (pci_assign_all_buses)
283 		pcibios_make_OF_bus_map();
284 #endif
285 
286 	/* Call common code to handle resource allocation */
287 	pcibios_resource_survey();
288 
289 	/* Call machine dependent fixup */
290 	if (ppc_md.pcibios_fixup)
291 		ppc_md.pcibios_fixup();
292 
293 	/* Call machine dependent post-init code */
294 	if (ppc_md.pcibios_after_init)
295 		ppc_md.pcibios_after_init();
296 
297 	return 0;
298 }
299 
300 subsys_initcall(pcibios_init);
301 
302 static struct pci_controller*
303 pci_bus_to_hose(int bus)
304 {
305 	struct pci_controller *hose, *tmp;
306 
307 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
308 		if (bus >= hose->first_busno && bus <= hose->last_busno)
309 			return hose;
310 	return NULL;
311 }
312 
313 /* Provide information on locations of various I/O regions in physical
314  * memory.  Do this on a per-card basis so that we choose the right
315  * root bridge.
316  * Note that the returned IO or memory base is a physical address
317  */
318 
319 SYSCALL_DEFINE3(pciconfig_iobase, long, which,
320 		unsigned long, bus, unsigned long, devfn)
321 {
322 	struct pci_controller* hose;
323 	long result = -EOPNOTSUPP;
324 
325 	hose = pci_bus_to_hose(bus);
326 	if (!hose)
327 		return -ENODEV;
328 
329 	switch (which) {
330 	case IOBASE_BRIDGE_NUMBER:
331 		return (long)hose->first_busno;
332 	case IOBASE_MEMORY:
333 		return (long)hose->mem_offset[0];
334 	case IOBASE_IO:
335 		return (long)hose->io_base_phys;
336 	case IOBASE_ISA_IO:
337 		return (long)isa_io_base;
338 	case IOBASE_ISA_MEM:
339 		return (long)isa_mem_base;
340 	}
341 
342 	return result;
343 }
344