xref: /linux/arch/mips/pci/pci-legacy.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f8091a88SPaul Burton /*
3f8091a88SPaul Burton  *
4f8091a88SPaul Burton  * Copyright (C) 2003, 04, 11 Ralf Baechle (ralf@linux-mips.org)
5f8091a88SPaul Burton  * Copyright (C) 2011 Wind River Systems,
6f8091a88SPaul Burton  *   written by Ralf Baechle (ralf@linux-mips.org)
7f8091a88SPaul Burton  */
8f8091a88SPaul Burton #include <linux/bug.h>
9f8091a88SPaul Burton #include <linux/kernel.h>
10f8091a88SPaul Burton #include <linux/mm.h>
1157c8a661SMike Rapoport #include <linux/memblock.h>
12f8091a88SPaul Burton #include <linux/export.h>
13f8091a88SPaul Burton #include <linux/init.h>
14f8091a88SPaul Burton #include <linux/types.h>
15f8091a88SPaul Burton #include <linux/pci.h>
16f8091a88SPaul Burton #include <linux/of_address.h>
17f8091a88SPaul Burton 
18f8091a88SPaul Burton #include <asm/cpu-info.h>
19f8091a88SPaul Burton 
20f8091a88SPaul Burton /*
21f8091a88SPaul Burton  * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource
22f8091a88SPaul Burton  * assignments.
23f8091a88SPaul Burton  */
24f8091a88SPaul Burton 
25f8091a88SPaul Burton /*
26f8091a88SPaul Burton  * The PCI controller list.
27f8091a88SPaul Burton  */
28f8091a88SPaul Burton static LIST_HEAD(controllers);
29f8091a88SPaul Burton 
30f8091a88SPaul Burton static int pci_initialized;
31f8091a88SPaul Burton 
32f8091a88SPaul Burton /*
33f8091a88SPaul Burton  * We need to avoid collisions with `mirrored' VGA ports
34f8091a88SPaul Burton  * and other strange ISA hardware, so we always want the
35f8091a88SPaul Burton  * addresses to be allocated in the 0x000-0x0ff region
36f8091a88SPaul Burton  * modulo 0x400.
37f8091a88SPaul Burton  *
38f8091a88SPaul Burton  * Why? Because some silly external IO cards only decode
39f8091a88SPaul Burton  * the low 10 bits of the IO address. The 0x00-0xff region
40f8091a88SPaul Burton  * is reserved for motherboard devices that decode all 16
41f8091a88SPaul Burton  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
42f8091a88SPaul Burton  * but we want to try to avoid allocating at 0x2900-0x2bff
43f8091a88SPaul Burton  * which might have be mirrored at 0x0100-0x03ff..
44f8091a88SPaul Burton  */
45f8091a88SPaul Burton resource_size_t
pcibios_align_resource(void * data,const struct resource * res,resource_size_t size,resource_size_t align)46f8091a88SPaul Burton pcibios_align_resource(void *data, const struct resource *res,
47f8091a88SPaul Burton 		       resource_size_t size, resource_size_t align)
48f8091a88SPaul Burton {
49f8091a88SPaul Burton 	struct pci_dev *dev = data;
50f8091a88SPaul Burton 	struct pci_controller *hose = dev->sysdata;
51f8091a88SPaul Burton 	resource_size_t start = res->start;
52f8091a88SPaul Burton 
53f8091a88SPaul Burton 	if (res->flags & IORESOURCE_IO) {
54f8091a88SPaul Burton 		/* Make sure we start at our min on all hoses */
55f8091a88SPaul Burton 		if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
56f8091a88SPaul Burton 			start = PCIBIOS_MIN_IO + hose->io_resource->start;
57f8091a88SPaul Burton 
58f8091a88SPaul Burton 		/*
59f8091a88SPaul Burton 		 * Put everything into 0x00-0xff region modulo 0x400
60f8091a88SPaul Burton 		 */
61f8091a88SPaul Burton 		if (start & 0x300)
62f8091a88SPaul Burton 			start = (start + 0x3ff) & ~0x3ff;
63f8091a88SPaul Burton 	} else if (res->flags & IORESOURCE_MEM) {
64f8091a88SPaul Burton 		/* Make sure we start at our min on all hoses */
65f8091a88SPaul Burton 		if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
66f8091a88SPaul Burton 			start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
67f8091a88SPaul Burton 	}
68f8091a88SPaul Burton 
69f8091a88SPaul Burton 	return start;
70f8091a88SPaul Burton }
71f8091a88SPaul Burton 
pcibios_scanbus(struct pci_controller * hose)72f8091a88SPaul Burton static void pcibios_scanbus(struct pci_controller *hose)
73f8091a88SPaul Burton {
74f8091a88SPaul Burton 	static int next_busno;
75f8091a88SPaul Burton 	static int need_domain_info;
76f8091a88SPaul Burton 	LIST_HEAD(resources);
77f8091a88SPaul Burton 	struct pci_bus *bus;
7804c81c72SLorenzo Pieralisi 	struct pci_host_bridge *bridge;
7904c81c72SLorenzo Pieralisi 	int ret;
8004c81c72SLorenzo Pieralisi 
8104c81c72SLorenzo Pieralisi 	bridge = pci_alloc_host_bridge(0);
8204c81c72SLorenzo Pieralisi 	if (!bridge)
8304c81c72SLorenzo Pieralisi 		return;
84f8091a88SPaul Burton 
85f8091a88SPaul Burton 	if (hose->get_busno && pci_has_flag(PCI_PROBE_ONLY))
86f8091a88SPaul Burton 		next_busno = (*hose->get_busno)();
87f8091a88SPaul Burton 
88f8091a88SPaul Burton 	pci_add_resource_offset(&resources,
89f8091a88SPaul Burton 				hose->mem_resource, hose->mem_offset);
90f8091a88SPaul Burton 	pci_add_resource_offset(&resources,
91f8091a88SPaul Burton 				hose->io_resource, hose->io_offset);
9204c81c72SLorenzo Pieralisi 	list_splice_init(&resources, &bridge->windows);
9304c81c72SLorenzo Pieralisi 	bridge->dev.parent = NULL;
9404c81c72SLorenzo Pieralisi 	bridge->sysdata = hose;
9504c81c72SLorenzo Pieralisi 	bridge->busnr = next_busno;
9604c81c72SLorenzo Pieralisi 	bridge->ops = hose->pci_ops;
9704c81c72SLorenzo Pieralisi 	bridge->swizzle_irq = pci_common_swizzle;
9804c81c72SLorenzo Pieralisi 	bridge->map_irq = pcibios_map_irq;
9904c81c72SLorenzo Pieralisi 	ret = pci_scan_root_bus_bridge(bridge);
10004c81c72SLorenzo Pieralisi 	if (ret) {
10104c81c72SLorenzo Pieralisi 		pci_free_host_bridge(bridge);
102f8091a88SPaul Burton 		return;
103f8091a88SPaul Burton 	}
104f8091a88SPaul Burton 
10504c81c72SLorenzo Pieralisi 	hose->bus = bus = bridge->bus;
106902d886dSLorenzo Pieralisi 
107902d886dSLorenzo Pieralisi 	need_domain_info = need_domain_info || pci_domain_nr(bus);
108902d886dSLorenzo Pieralisi 	set_pci_need_domain_info(hose, need_domain_info);
109902d886dSLorenzo Pieralisi 
110f8091a88SPaul Burton 	next_busno = bus->busn_res.end + 1;
111f8091a88SPaul Burton 	/* Don't allow 8-bit bus number overflow inside the hose -
112f8091a88SPaul Burton 	   reserve some space for bridges. */
113f8091a88SPaul Burton 	if (next_busno > 224) {
114f8091a88SPaul Burton 		next_busno = 0;
115f8091a88SPaul Burton 		need_domain_info = 1;
116f8091a88SPaul Burton 	}
117f8091a88SPaul Burton 
118f8091a88SPaul Burton 	/*
119f8091a88SPaul Burton 	 * We insert PCI resources into the iomem_resource and
120f8091a88SPaul Burton 	 * ioport_resource trees in either pci_bus_claim_resources()
121f8091a88SPaul Burton 	 * or pci_bus_assign_resources().
122f8091a88SPaul Burton 	 */
123f8091a88SPaul Burton 	if (pci_has_flag(PCI_PROBE_ONLY)) {
124f8091a88SPaul Burton 		pci_bus_claim_resources(bus);
125f8091a88SPaul Burton 	} else {
1262794f688SHuacai Chen 		struct pci_bus *child;
1272794f688SHuacai Chen 
128f8091a88SPaul Burton 		pci_bus_size_bridges(bus);
129f8091a88SPaul Burton 		pci_bus_assign_resources(bus);
1302794f688SHuacai Chen 		list_for_each_entry(child, &bus->children, node)
1312794f688SHuacai Chen 			pcie_bus_configure_settings(child);
132f8091a88SPaul Burton 	}
133f8091a88SPaul Burton 	pci_bus_add_devices(bus);
134f8091a88SPaul Burton }
135f8091a88SPaul Burton 
136f8091a88SPaul Burton #ifdef CONFIG_OF
pci_load_of_ranges(struct pci_controller * hose,struct device_node * node)137f8091a88SPaul Burton void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node)
138f8091a88SPaul Burton {
139f8091a88SPaul Burton 	struct of_pci_range range;
140f8091a88SPaul Burton 	struct of_pci_range_parser parser;
141f8091a88SPaul Burton 
142f8091a88SPaul Burton 	hose->of_node = node;
143f8091a88SPaul Burton 
144f8091a88SPaul Burton 	if (of_pci_range_parser_init(&parser, node))
145f8091a88SPaul Burton 		return;
146f8091a88SPaul Burton 
147f8091a88SPaul Burton 	for_each_of_pci_range(&parser, &range) {
148f8091a88SPaul Burton 		struct resource *res = NULL;
149f8091a88SPaul Burton 
150f8091a88SPaul Burton 		switch (range.flags & IORESOURCE_TYPE_BITS) {
151f8091a88SPaul Burton 		case IORESOURCE_IO:
152f8091a88SPaul Burton 			hose->io_map_base =
153f8091a88SPaul Burton 				(unsigned long)ioremap(range.cpu_addr,
154f8091a88SPaul Burton 						       range.size);
155f8091a88SPaul Burton 			res = hose->io_resource;
156f8091a88SPaul Burton 			break;
157f8091a88SPaul Burton 		case IORESOURCE_MEM:
158f8091a88SPaul Burton 			res = hose->mem_resource;
159f8091a88SPaul Burton 			break;
160f8091a88SPaul Burton 		}
1613ecb9dc1SIlya Lipnitskiy 		if (res != NULL) {
1623ecb9dc1SIlya Lipnitskiy 			res->name = node->full_name;
1633ecb9dc1SIlya Lipnitskiy 			res->flags = range.flags;
1643ecb9dc1SIlya Lipnitskiy 			res->start = range.cpu_addr;
1653ecb9dc1SIlya Lipnitskiy 			res->end = range.cpu_addr + range.size - 1;
1663ecb9dc1SIlya Lipnitskiy 			res->parent = res->child = res->sibling = NULL;
1673ecb9dc1SIlya Lipnitskiy 		}
168f8091a88SPaul Burton 	}
169f8091a88SPaul Burton }
170f8091a88SPaul Burton 
pcibios_get_phb_of_node(struct pci_bus * bus)171f8091a88SPaul Burton struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
172f8091a88SPaul Burton {
173f8091a88SPaul Burton 	struct pci_controller *hose = bus->sysdata;
174f8091a88SPaul Burton 
175f8091a88SPaul Burton 	return of_node_get(hose->of_node);
176f8091a88SPaul Burton }
177f8091a88SPaul Burton #endif
178f8091a88SPaul Burton 
179f8091a88SPaul Burton static DEFINE_MUTEX(pci_scan_mutex);
180f8091a88SPaul Burton 
register_pci_controller(struct pci_controller * hose)181f8091a88SPaul Burton void register_pci_controller(struct pci_controller *hose)
182f8091a88SPaul Burton {
183f8091a88SPaul Burton 	struct resource *parent;
184f8091a88SPaul Burton 
185f8091a88SPaul Burton 	parent = hose->mem_resource->parent;
186f8091a88SPaul Burton 	if (!parent)
187f8091a88SPaul Burton 		parent = &iomem_resource;
188f8091a88SPaul Burton 
189f8091a88SPaul Burton 	if (request_resource(parent, hose->mem_resource) < 0)
190f8091a88SPaul Burton 		goto out;
191f8091a88SPaul Burton 
192f8091a88SPaul Burton 	parent = hose->io_resource->parent;
193f8091a88SPaul Burton 	if (!parent)
194f8091a88SPaul Burton 		parent = &ioport_resource;
195f8091a88SPaul Burton 
196f8091a88SPaul Burton 	if (request_resource(parent, hose->io_resource) < 0) {
197f8091a88SPaul Burton 		release_resource(hose->mem_resource);
198f8091a88SPaul Burton 		goto out;
199f8091a88SPaul Burton 	}
200f8091a88SPaul Burton 
201f8091a88SPaul Burton 	INIT_LIST_HEAD(&hose->list);
202edb0b6a0SMathias Kresin 	list_add_tail(&hose->list, &controllers);
203f8091a88SPaul Burton 
204f8091a88SPaul Burton 	/*
205f8091a88SPaul Burton 	 * Do not panic here but later - this might happen before console init.
206f8091a88SPaul Burton 	 */
207f8091a88SPaul Burton 	if (!hose->io_map_base) {
208f8091a88SPaul Burton 		printk(KERN_WARNING
209f8091a88SPaul Burton 		       "registering PCI controller with io_map_base unset\n");
210f8091a88SPaul Burton 	}
211f8091a88SPaul Burton 
212f8091a88SPaul Burton 	/*
213f8091a88SPaul Burton 	 * Scan the bus if it is register after the PCI subsystem
214f8091a88SPaul Burton 	 * initialization.
215f8091a88SPaul Burton 	 */
216f8091a88SPaul Burton 	if (pci_initialized) {
217f8091a88SPaul Burton 		mutex_lock(&pci_scan_mutex);
218f8091a88SPaul Burton 		pcibios_scanbus(hose);
219f8091a88SPaul Burton 		mutex_unlock(&pci_scan_mutex);
220f8091a88SPaul Burton 	}
221f8091a88SPaul Burton 
222f8091a88SPaul Burton 	return;
223f8091a88SPaul Burton 
224f8091a88SPaul Burton out:
225f8091a88SPaul Burton 	printk(KERN_WARNING
226f8091a88SPaul Burton 	       "Skipping PCI bus scan due to resource conflict\n");
227f8091a88SPaul Burton }
228f8091a88SPaul Burton 
pcibios_init(void)229f8091a88SPaul Burton static int __init pcibios_init(void)
230f8091a88SPaul Burton {
231f8091a88SPaul Burton 	struct pci_controller *hose;
232f8091a88SPaul Burton 
233f8091a88SPaul Burton 	/* Scan all of the recorded PCI controllers.  */
234f8091a88SPaul Burton 	list_for_each_entry(hose, &controllers, list)
235f8091a88SPaul Burton 		pcibios_scanbus(hose);
236f8091a88SPaul Burton 
237f8091a88SPaul Burton 	pci_initialized = 1;
238f8091a88SPaul Burton 
239f8091a88SPaul Burton 	return 0;
240f8091a88SPaul Burton }
241f8091a88SPaul Burton 
242f8091a88SPaul Burton subsys_initcall(pcibios_init);
243f8091a88SPaul Burton 
pcibios_enable_resources(struct pci_dev * dev,int mask)244987b4207SIlya Lipnitskiy static int pcibios_enable_resources(struct pci_dev *dev, int mask)
245987b4207SIlya Lipnitskiy {
246987b4207SIlya Lipnitskiy 	u16 cmd, old_cmd;
247987b4207SIlya Lipnitskiy 	int idx;
248987b4207SIlya Lipnitskiy 	struct resource *r;
249987b4207SIlya Lipnitskiy 
250987b4207SIlya Lipnitskiy 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
251987b4207SIlya Lipnitskiy 	old_cmd = cmd;
252*09cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, r, idx) {
253987b4207SIlya Lipnitskiy 		/* Only set up the requested stuff */
254987b4207SIlya Lipnitskiy 		if (!(mask & (1<<idx)))
255987b4207SIlya Lipnitskiy 			continue;
256987b4207SIlya Lipnitskiy 
257987b4207SIlya Lipnitskiy 		if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
258987b4207SIlya Lipnitskiy 			continue;
259987b4207SIlya Lipnitskiy 		if ((idx == PCI_ROM_RESOURCE) &&
260987b4207SIlya Lipnitskiy 				(!(r->flags & IORESOURCE_ROM_ENABLE)))
261987b4207SIlya Lipnitskiy 			continue;
262987b4207SIlya Lipnitskiy 		if (!r->start && r->end) {
263987b4207SIlya Lipnitskiy 			pci_err(dev,
264987b4207SIlya Lipnitskiy 				"can't enable device: resource collisions\n");
265987b4207SIlya Lipnitskiy 			return -EINVAL;
266987b4207SIlya Lipnitskiy 		}
267987b4207SIlya Lipnitskiy 		if (r->flags & IORESOURCE_IO)
268987b4207SIlya Lipnitskiy 			cmd |= PCI_COMMAND_IO;
269987b4207SIlya Lipnitskiy 		if (r->flags & IORESOURCE_MEM)
270987b4207SIlya Lipnitskiy 			cmd |= PCI_COMMAND_MEMORY;
271987b4207SIlya Lipnitskiy 	}
272987b4207SIlya Lipnitskiy 	if (cmd != old_cmd) {
273987b4207SIlya Lipnitskiy 		pci_info(dev, "enabling device (%04x -> %04x)\n", old_cmd, cmd);
274987b4207SIlya Lipnitskiy 		pci_write_config_word(dev, PCI_COMMAND, cmd);
275987b4207SIlya Lipnitskiy 	}
276987b4207SIlya Lipnitskiy 	return 0;
277987b4207SIlya Lipnitskiy }
278987b4207SIlya Lipnitskiy 
pcibios_enable_device(struct pci_dev * dev,int mask)279f8091a88SPaul Burton int pcibios_enable_device(struct pci_dev *dev, int mask)
280f8091a88SPaul Burton {
281987b4207SIlya Lipnitskiy 	int err = pcibios_enable_resources(dev, mask);
282f8091a88SPaul Burton 
28399bca615SIlya Lipnitskiy 	if (err < 0)
284f8091a88SPaul Burton 		return err;
285f8091a88SPaul Burton 
286f8091a88SPaul Burton 	return pcibios_plat_dev_init(dev);
287f8091a88SPaul Burton }
288f8091a88SPaul Burton 
pcibios_fixup_bus(struct pci_bus * bus)289f8091a88SPaul Burton void pcibios_fixup_bus(struct pci_bus *bus)
290f8091a88SPaul Burton {
291f8091a88SPaul Burton 	struct pci_dev *dev = bus->self;
292f8091a88SPaul Burton 
293f8091a88SPaul Burton 	if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
294f8091a88SPaul Burton 	    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
295f8091a88SPaul Burton 		pci_read_bridge_bases(bus);
296f8091a88SPaul Burton 	}
297f8091a88SPaul Burton }
298f8091a88SPaul Burton 
299f8091a88SPaul Burton char * (*pcibios_plat_setup)(char *str) __initdata;
300f8091a88SPaul Burton 
pcibios_setup(char * str)301f8091a88SPaul Burton char *__init pcibios_setup(char *str)
302f8091a88SPaul Burton {
303f8091a88SPaul Burton 	if (pcibios_plat_setup)
304f8091a88SPaul Burton 		return pcibios_plat_setup(str);
305f8091a88SPaul Burton 	return str;
306f8091a88SPaul Burton }
307