xref: /linux/arch/powerpc/kernel/pci_64.c (revision 420b5eeaee5b877829c4f0a514a5ad21448596af)
17568cb4eSPaul Mackerras /*
27568cb4eSPaul Mackerras  * Port for PPC64 David Engebretsen, IBM Corp.
37568cb4eSPaul Mackerras  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
47568cb4eSPaul Mackerras  *
57568cb4eSPaul Mackerras  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
67568cb4eSPaul Mackerras  *   Rework, based on alpha PCI code.
77568cb4eSPaul Mackerras  *
87568cb4eSPaul Mackerras  *      This program is free software; you can redistribute it and/or
97568cb4eSPaul Mackerras  *      modify it under the terms of the GNU General Public License
107568cb4eSPaul Mackerras  *      as published by the Free Software Foundation; either version
117568cb4eSPaul Mackerras  *      2 of the License, or (at your option) any later version.
127568cb4eSPaul Mackerras  */
137568cb4eSPaul Mackerras 
147568cb4eSPaul Mackerras #undef DEBUG
157568cb4eSPaul Mackerras 
167568cb4eSPaul Mackerras #include <linux/kernel.h>
177568cb4eSPaul Mackerras #include <linux/pci.h>
187568cb4eSPaul Mackerras #include <linux/string.h>
197568cb4eSPaul Mackerras #include <linux/init.h>
207568cb4eSPaul Mackerras #include <linux/bootmem.h>
217568cb4eSPaul Mackerras #include <linux/mm.h>
227568cb4eSPaul Mackerras #include <linux/list.h>
237568cb4eSPaul Mackerras #include <linux/syscalls.h>
246e99e458SBenjamin Herrenschmidt #include <linux/irq.h>
253d5134eeSBenjamin Herrenschmidt #include <linux/vmalloc.h>
267568cb4eSPaul Mackerras 
277568cb4eSPaul Mackerras #include <asm/processor.h>
287568cb4eSPaul Mackerras #include <asm/io.h>
297568cb4eSPaul Mackerras #include <asm/prom.h>
307568cb4eSPaul Mackerras #include <asm/pci-bridge.h>
317568cb4eSPaul Mackerras #include <asm/byteorder.h>
327568cb4eSPaul Mackerras #include <asm/machdep.h>
337568cb4eSPaul Mackerras #include <asm/ppc-pci.h>
347568cb4eSPaul Mackerras 
357568cb4eSPaul Mackerras #ifdef DEBUG
36f9e4ec57SMichael Ellerman #include <asm/udbg.h>
371beb6a7dSBenjamin Herrenschmidt #define DBG(fmt...) printk(fmt)
387568cb4eSPaul Mackerras #else
397568cb4eSPaul Mackerras #define DBG(fmt...)
407568cb4eSPaul Mackerras #endif
417568cb4eSPaul Mackerras 
427568cb4eSPaul Mackerras unsigned long pci_probe_only = 1;
437568cb4eSPaul Mackerras 
447568cb4eSPaul Mackerras /* pci_io_base -- the base address from which io bars are offsets.
457568cb4eSPaul Mackerras  * This is the lowest I/O base address (so bar values are always positive),
467568cb4eSPaul Mackerras  * and it *must* be the start of ISA space if an ISA bus exists because
473d5134eeSBenjamin Herrenschmidt  * ISA drivers use hard coded offsets.  If no ISA bus exists nothing
483d5134eeSBenjamin Herrenschmidt  * is mapped on the first 64K of IO space
497568cb4eSPaul Mackerras  */
503d5134eeSBenjamin Herrenschmidt unsigned long pci_io_base = ISA_IO_BASE;
517568cb4eSPaul Mackerras EXPORT_SYMBOL(pci_io_base);
527568cb4eSPaul Mackerras 
537568cb4eSPaul Mackerras LIST_HEAD(hose_list);
547568cb4eSPaul Mackerras 
5557190708SStephen Rothwell static struct dma_mapping_ops *pci_dma_ops;
567568cb4eSPaul Mackerras 
5798747770SStephen Rothwell void set_pci_dma_ops(struct dma_mapping_ops *dma_ops)
5898747770SStephen Rothwell {
5998747770SStephen Rothwell 	pci_dma_ops = dma_ops;
6098747770SStephen Rothwell }
6198747770SStephen Rothwell 
6257190708SStephen Rothwell struct dma_mapping_ops *get_pci_dma_ops(void)
6357190708SStephen Rothwell {
6457190708SStephen Rothwell 	return pci_dma_ops;
6557190708SStephen Rothwell }
6657190708SStephen Rothwell EXPORT_SYMBOL(get_pci_dma_ops);
6757190708SStephen Rothwell 
6884631f37SMichael Ellerman 
6984631f37SMichael Ellerman int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
7084631f37SMichael Ellerman {
7184631f37SMichael Ellerman 	return dma_set_mask(&dev->dev, mask);
7284631f37SMichael Ellerman }
7384631f37SMichael Ellerman 
7484631f37SMichael Ellerman int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
7584631f37SMichael Ellerman {
7684631f37SMichael Ellerman 	int rc;
7784631f37SMichael Ellerman 
7884631f37SMichael Ellerman 	rc = dma_set_mask(&dev->dev, mask);
7984631f37SMichael Ellerman 	dev->dev.coherent_dma_mask = dev->dma_mask;
8084631f37SMichael Ellerman 
8184631f37SMichael Ellerman 	return rc;
8284631f37SMichael Ellerman }
8384631f37SMichael Ellerman 
847568cb4eSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev)
857568cb4eSPaul Mackerras {
867568cb4eSPaul Mackerras 	if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
877568cb4eSPaul Mackerras 		dev->vendor = PCI_VENDOR_ID_AMD;
887568cb4eSPaul Mackerras 		pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
897568cb4eSPaul Mackerras 	}
907568cb4eSPaul Mackerras }
917568cb4eSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
927568cb4eSPaul Mackerras 
937568cb4eSPaul Mackerras 
947568cb4eSPaul Mackerras static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
957568cb4eSPaul Mackerras {
96a7f67bdfSJeremy Kerr 	const u32 *prop;
977568cb4eSPaul Mackerras 	int len;
987568cb4eSPaul Mackerras 
99e2eb6392SStephen Rothwell 	prop = of_get_property(np, name, &len);
1007568cb4eSPaul Mackerras 	if (prop && len >= 4)
1017568cb4eSPaul Mackerras 		return *prop;
1027568cb4eSPaul Mackerras 	return def;
1037568cb4eSPaul Mackerras }
1047568cb4eSPaul Mackerras 
1057568cb4eSPaul Mackerras static unsigned int pci_parse_of_flags(u32 addr0)
1067568cb4eSPaul Mackerras {
1077568cb4eSPaul Mackerras 	unsigned int flags = 0;
1087568cb4eSPaul Mackerras 
1097568cb4eSPaul Mackerras 	if (addr0 & 0x02000000) {
1107568cb4eSPaul Mackerras 		flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
1117568cb4eSPaul Mackerras 		flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
1127568cb4eSPaul Mackerras 		flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
1137568cb4eSPaul Mackerras 		if (addr0 & 0x40000000)
1147568cb4eSPaul Mackerras 			flags |= IORESOURCE_PREFETCH
1157568cb4eSPaul Mackerras 				 | PCI_BASE_ADDRESS_MEM_PREFETCH;
1167568cb4eSPaul Mackerras 	} else if (addr0 & 0x01000000)
1177568cb4eSPaul Mackerras 		flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
1187568cb4eSPaul Mackerras 	return flags;
1197568cb4eSPaul Mackerras }
1207568cb4eSPaul Mackerras 
1217568cb4eSPaul Mackerras 
1227568cb4eSPaul Mackerras static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
1237568cb4eSPaul Mackerras {
1247568cb4eSPaul Mackerras 	u64 base, size;
1257568cb4eSPaul Mackerras 	unsigned int flags;
1267568cb4eSPaul Mackerras 	struct resource *res;
127a7f67bdfSJeremy Kerr 	const u32 *addrs;
128a7f67bdfSJeremy Kerr 	u32 i;
1297568cb4eSPaul Mackerras 	int proplen;
1307568cb4eSPaul Mackerras 
131e2eb6392SStephen Rothwell 	addrs = of_get_property(node, "assigned-addresses", &proplen);
1327568cb4eSPaul Mackerras 	if (!addrs)
1337568cb4eSPaul Mackerras 		return;
1341beb6a7dSBenjamin Herrenschmidt 	DBG("    parse addresses (%d bytes) @ %p\n", proplen, addrs);
1357568cb4eSPaul Mackerras 	for (; proplen >= 20; proplen -= 20, addrs += 5) {
1367568cb4eSPaul Mackerras 		flags = pci_parse_of_flags(addrs[0]);
1377568cb4eSPaul Mackerras 		if (!flags)
1387568cb4eSPaul Mackerras 			continue;
139327e22dfSJon Loeliger 		base = of_read_number(&addrs[1], 2);
140327e22dfSJon Loeliger 		size = of_read_number(&addrs[3], 2);
1417568cb4eSPaul Mackerras 		if (!size)
1427568cb4eSPaul Mackerras 			continue;
1437568cb4eSPaul Mackerras 		i = addrs[0] & 0xff;
1441beb6a7dSBenjamin Herrenschmidt 		DBG("  base: %llx, size: %llx, i: %x\n",
1451beb6a7dSBenjamin Herrenschmidt 		    (unsigned long long)base, (unsigned long long)size, i);
1461beb6a7dSBenjamin Herrenschmidt 
1477568cb4eSPaul Mackerras 		if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
1487568cb4eSPaul Mackerras 			res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
1497568cb4eSPaul Mackerras 		} else if (i == dev->rom_base_reg) {
1507568cb4eSPaul Mackerras 			res = &dev->resource[PCI_ROM_RESOURCE];
1517568cb4eSPaul Mackerras 			flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
1527568cb4eSPaul Mackerras 		} else {
1537568cb4eSPaul Mackerras 			printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
1547568cb4eSPaul Mackerras 			continue;
1557568cb4eSPaul Mackerras 		}
1567568cb4eSPaul Mackerras 		res->start = base;
1577568cb4eSPaul Mackerras 		res->end = base + size - 1;
1587568cb4eSPaul Mackerras 		res->flags = flags;
1597568cb4eSPaul Mackerras 		res->name = pci_name(dev);
1607568cb4eSPaul Mackerras 	}
1617568cb4eSPaul Mackerras }
1627568cb4eSPaul Mackerras 
1637568cb4eSPaul Mackerras struct pci_dev *of_create_pci_dev(struct device_node *node,
1647568cb4eSPaul Mackerras 				 struct pci_bus *bus, int devfn)
1657568cb4eSPaul Mackerras {
1667568cb4eSPaul Mackerras 	struct pci_dev *dev;
1677568cb4eSPaul Mackerras 	const char *type;
1687568cb4eSPaul Mackerras 
169bab41e9bSMichael Ellerman 	dev = alloc_pci_dev();
1707568cb4eSPaul Mackerras 	if (!dev)
1717568cb4eSPaul Mackerras 		return NULL;
172e2eb6392SStephen Rothwell 	type = of_get_property(node, "device_type", NULL);
1737568cb4eSPaul Mackerras 	if (type == NULL)
1747568cb4eSPaul Mackerras 		type = "";
1757568cb4eSPaul Mackerras 
1761beb6a7dSBenjamin Herrenschmidt 	DBG("    create device, devfn: %x, type: %s\n", devfn, type);
1771beb6a7dSBenjamin Herrenschmidt 
1787568cb4eSPaul Mackerras 	dev->bus = bus;
1797568cb4eSPaul Mackerras 	dev->sysdata = node;
1807568cb4eSPaul Mackerras 	dev->dev.parent = bus->bridge;
1817568cb4eSPaul Mackerras 	dev->dev.bus = &pci_bus_type;
1827568cb4eSPaul Mackerras 	dev->devfn = devfn;
1837568cb4eSPaul Mackerras 	dev->multifunction = 0;		/* maybe a lie? */
1847568cb4eSPaul Mackerras 
1857568cb4eSPaul Mackerras 	dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
1867568cb4eSPaul Mackerras 	dev->device = get_int_prop(node, "device-id", 0xffff);
1877568cb4eSPaul Mackerras 	dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
1887568cb4eSPaul Mackerras 	dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
1897568cb4eSPaul Mackerras 
1909d17a5c6SBenjamin Herrenschmidt 	dev->cfg_size = pci_cfg_space_size(dev);
1917568cb4eSPaul Mackerras 
192*420b5eeaSStephen Rothwell 	dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
1937568cb4eSPaul Mackerras 		dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
1947568cb4eSPaul Mackerras 	dev->class = get_int_prop(node, "class-code", 0);
195b8a3a521SAuke Kok 	dev->revision = get_int_prop(node, "revision-id", 0);
1967568cb4eSPaul Mackerras 
1971beb6a7dSBenjamin Herrenschmidt 	DBG("    class: 0x%x\n", dev->class);
198b8a3a521SAuke Kok 	DBG("    revision: 0x%x\n", dev->revision);
1991beb6a7dSBenjamin Herrenschmidt 
2007568cb4eSPaul Mackerras 	dev->current_state = 4;		/* unknown power state */
201bb63ab13SLinas Vepstas 	dev->error_state = pci_channel_io_normal;
2028f2ea1fdSBenjamin Herrenschmidt 	dev->dma_mask = 0xffffffff;
2037568cb4eSPaul Mackerras 
204bb53bb3dSJake Moilanen 	if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
2057568cb4eSPaul Mackerras 		/* a PCI-PCI bridge */
2067568cb4eSPaul Mackerras 		dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
2077568cb4eSPaul Mackerras 		dev->rom_base_reg = PCI_ROM_ADDRESS1;
2087568cb4eSPaul Mackerras 	} else if (!strcmp(type, "cardbus")) {
2097568cb4eSPaul Mackerras 		dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
2107568cb4eSPaul Mackerras 	} else {
2117568cb4eSPaul Mackerras 		dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
2127568cb4eSPaul Mackerras 		dev->rom_base_reg = PCI_ROM_ADDRESS;
2130ebfff14SBenjamin Herrenschmidt 		/* Maybe do a default OF mapping here */
2147568cb4eSPaul Mackerras 		dev->irq = NO_IRQ;
2157568cb4eSPaul Mackerras 	}
2167568cb4eSPaul Mackerras 
2177568cb4eSPaul Mackerras 	pci_parse_of_addrs(node, dev);
2187568cb4eSPaul Mackerras 
2191beb6a7dSBenjamin Herrenschmidt 	DBG("    adding to system ...\n");
2201beb6a7dSBenjamin Herrenschmidt 
2217568cb4eSPaul Mackerras 	pci_device_add(dev, bus);
2227568cb4eSPaul Mackerras 
2237568cb4eSPaul Mackerras 	return dev;
2247568cb4eSPaul Mackerras }
2257568cb4eSPaul Mackerras EXPORT_SYMBOL(of_create_pci_dev);
2267568cb4eSPaul Mackerras 
2277568cb4eSPaul Mackerras void __devinit of_scan_bus(struct device_node *node,
2287568cb4eSPaul Mackerras 			   struct pci_bus *bus)
2297568cb4eSPaul Mackerras {
23085e99b9fSStephen Rothwell 	struct device_node *child;
231a7f67bdfSJeremy Kerr 	const u32 *reg;
2327568cb4eSPaul Mackerras 	int reglen, devfn;
2337568cb4eSPaul Mackerras 	struct pci_dev *dev;
2347568cb4eSPaul Mackerras 
2351beb6a7dSBenjamin Herrenschmidt 	DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
2361beb6a7dSBenjamin Herrenschmidt 
237bf5e2ba2SBenjamin Herrenschmidt 	/* Scan direct children */
23885e99b9fSStephen Rothwell 	for_each_child_of_node(node, child) {
2391beb6a7dSBenjamin Herrenschmidt 		DBG("  * %s\n", child->full_name);
240e2eb6392SStephen Rothwell 		reg = of_get_property(child, "reg", &reglen);
2417568cb4eSPaul Mackerras 		if (reg == NULL || reglen < 20)
2427568cb4eSPaul Mackerras 			continue;
2437568cb4eSPaul Mackerras 		devfn = (reg[0] >> 8) & 0xff;
2441beb6a7dSBenjamin Herrenschmidt 
2457568cb4eSPaul Mackerras 		/* create a new pci_dev for this device */
2467568cb4eSPaul Mackerras 		dev = of_create_pci_dev(child, bus, devfn);
2477568cb4eSPaul Mackerras 		if (!dev)
2487568cb4eSPaul Mackerras 			continue;
2491beb6a7dSBenjamin Herrenschmidt 		DBG("    dev header type: %x\n", dev->hdr_type);
2507568cb4eSPaul Mackerras 	}
2517568cb4eSPaul Mackerras 
252bf5e2ba2SBenjamin Herrenschmidt 	/* Ally all fixups */
253bf5e2ba2SBenjamin Herrenschmidt 	pcibios_fixup_of_probed_bus(bus);
254bf5e2ba2SBenjamin Herrenschmidt 
255bf5e2ba2SBenjamin Herrenschmidt 	/* Now scan child busses */
256bf5e2ba2SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
257bf5e2ba2SBenjamin Herrenschmidt 		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
258bf5e2ba2SBenjamin Herrenschmidt 		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
259bf5e2ba2SBenjamin Herrenschmidt 			struct device_node *child = pci_device_to_OF_node(dev);
260bf5e2ba2SBenjamin Herrenschmidt 			if (dev)
261bf5e2ba2SBenjamin Herrenschmidt 				of_scan_pci_bridge(child, dev);
262bf5e2ba2SBenjamin Herrenschmidt 		}
263bf5e2ba2SBenjamin Herrenschmidt 	}
2647568cb4eSPaul Mackerras }
2657568cb4eSPaul Mackerras EXPORT_SYMBOL(of_scan_bus);
2667568cb4eSPaul Mackerras 
2677568cb4eSPaul Mackerras void __devinit of_scan_pci_bridge(struct device_node *node,
2687568cb4eSPaul Mackerras 				  struct pci_dev *dev)
2697568cb4eSPaul Mackerras {
2707568cb4eSPaul Mackerras 	struct pci_bus *bus;
271a7f67bdfSJeremy Kerr 	const u32 *busrange, *ranges;
2727568cb4eSPaul Mackerras 	int len, i, mode;
2737568cb4eSPaul Mackerras 	struct resource *res;
2747568cb4eSPaul Mackerras 	unsigned int flags;
2757568cb4eSPaul Mackerras 	u64 size;
2767568cb4eSPaul Mackerras 
2771beb6a7dSBenjamin Herrenschmidt 	DBG("of_scan_pci_bridge(%s)\n", node->full_name);
2781beb6a7dSBenjamin Herrenschmidt 
2797568cb4eSPaul Mackerras 	/* parse bus-range property */
280e2eb6392SStephen Rothwell 	busrange = of_get_property(node, "bus-range", &len);
2817568cb4eSPaul Mackerras 	if (busrange == NULL || len != 8) {
2821beb6a7dSBenjamin Herrenschmidt 		printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
2837568cb4eSPaul Mackerras 		       node->full_name);
2847568cb4eSPaul Mackerras 		return;
2857568cb4eSPaul Mackerras 	}
286e2eb6392SStephen Rothwell 	ranges = of_get_property(node, "ranges", &len);
2877568cb4eSPaul Mackerras 	if (ranges == NULL) {
2881beb6a7dSBenjamin Herrenschmidt 		printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
2897568cb4eSPaul Mackerras 		       node->full_name);
2907568cb4eSPaul Mackerras 		return;
2917568cb4eSPaul Mackerras 	}
2927568cb4eSPaul Mackerras 
2937568cb4eSPaul Mackerras 	bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
2947568cb4eSPaul Mackerras 	if (!bus) {
2957568cb4eSPaul Mackerras 		printk(KERN_ERR "Failed to create pci bus for %s\n",
2967568cb4eSPaul Mackerras 		       node->full_name);
2977568cb4eSPaul Mackerras 		return;
2987568cb4eSPaul Mackerras 	}
2997568cb4eSPaul Mackerras 
3007568cb4eSPaul Mackerras 	bus->primary = dev->bus->number;
3017568cb4eSPaul Mackerras 	bus->subordinate = busrange[1];
3027568cb4eSPaul Mackerras 	bus->bridge_ctl = 0;
3037568cb4eSPaul Mackerras 	bus->sysdata = node;
3047568cb4eSPaul Mackerras 
3057568cb4eSPaul Mackerras 	/* parse ranges property */
3067568cb4eSPaul Mackerras 	/* PCI #address-cells == 3 and #size-cells == 2 always */
3077568cb4eSPaul Mackerras 	res = &dev->resource[PCI_BRIDGE_RESOURCES];
3087568cb4eSPaul Mackerras 	for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
3097568cb4eSPaul Mackerras 		res->flags = 0;
3107568cb4eSPaul Mackerras 		bus->resource[i] = res;
3117568cb4eSPaul Mackerras 		++res;
3127568cb4eSPaul Mackerras 	}
3137568cb4eSPaul Mackerras 	i = 1;
3147568cb4eSPaul Mackerras 	for (; len >= 32; len -= 32, ranges += 8) {
3157568cb4eSPaul Mackerras 		flags = pci_parse_of_flags(ranges[0]);
316327e22dfSJon Loeliger 		size = of_read_number(&ranges[6], 2);
3177568cb4eSPaul Mackerras 		if (flags == 0 || size == 0)
3187568cb4eSPaul Mackerras 			continue;
3197568cb4eSPaul Mackerras 		if (flags & IORESOURCE_IO) {
3207568cb4eSPaul Mackerras 			res = bus->resource[0];
3217568cb4eSPaul Mackerras 			if (res->flags) {
3227568cb4eSPaul Mackerras 				printk(KERN_ERR "PCI: ignoring extra I/O range"
3237568cb4eSPaul Mackerras 				       " for bridge %s\n", node->full_name);
3247568cb4eSPaul Mackerras 				continue;
3257568cb4eSPaul Mackerras 			}
3267568cb4eSPaul Mackerras 		} else {
3277568cb4eSPaul Mackerras 			if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
3287568cb4eSPaul Mackerras 				printk(KERN_ERR "PCI: too many memory ranges"
3297568cb4eSPaul Mackerras 				       " for bridge %s\n", node->full_name);
3307568cb4eSPaul Mackerras 				continue;
3317568cb4eSPaul Mackerras 			}
3327568cb4eSPaul Mackerras 			res = bus->resource[i];
3337568cb4eSPaul Mackerras 			++i;
3347568cb4eSPaul Mackerras 		}
335327e22dfSJon Loeliger 		res->start = of_read_number(&ranges[1], 2);
3367568cb4eSPaul Mackerras 		res->end = res->start + size - 1;
3377568cb4eSPaul Mackerras 		res->flags = flags;
3387568cb4eSPaul Mackerras 	}
3397568cb4eSPaul Mackerras 	sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
3407568cb4eSPaul Mackerras 		bus->number);
3411beb6a7dSBenjamin Herrenschmidt 	DBG("    bus name: %s\n", bus->name);
3427568cb4eSPaul Mackerras 
3437568cb4eSPaul Mackerras 	mode = PCI_PROBE_NORMAL;
3447568cb4eSPaul Mackerras 	if (ppc_md.pci_probe_mode)
3457568cb4eSPaul Mackerras 		mode = ppc_md.pci_probe_mode(bus);
3461beb6a7dSBenjamin Herrenschmidt 	DBG("    probe mode: %d\n", mode);
3471beb6a7dSBenjamin Herrenschmidt 
3487568cb4eSPaul Mackerras 	if (mode == PCI_PROBE_DEVTREE)
3497568cb4eSPaul Mackerras 		of_scan_bus(node, bus);
3507568cb4eSPaul Mackerras 	else if (mode == PCI_PROBE_NORMAL)
3517568cb4eSPaul Mackerras 		pci_scan_child_bus(bus);
3527568cb4eSPaul Mackerras }
3537568cb4eSPaul Mackerras EXPORT_SYMBOL(of_scan_pci_bridge);
3547568cb4eSPaul Mackerras 
3557568cb4eSPaul Mackerras void __devinit scan_phb(struct pci_controller *hose)
3567568cb4eSPaul Mackerras {
3577568cb4eSPaul Mackerras 	struct pci_bus *bus;
35844ef3390SStephen Rothwell 	struct device_node *node = hose->dn;
3597568cb4eSPaul Mackerras 	int i, mode;
3607568cb4eSPaul Mackerras 
36150c9bc2fSBenjamin Herrenschmidt 	DBG("PCI: Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
3621beb6a7dSBenjamin Herrenschmidt 
3633fd94c6bSBenjamin Herrenschmidt 	/* Create an empty bus for the toplevel */
364803d4573SBenjamin Herrenschmidt 	bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
3657568cb4eSPaul Mackerras 	if (bus == NULL) {
3667568cb4eSPaul Mackerras 		printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
3677568cb4eSPaul Mackerras 		       hose->global_number);
3687568cb4eSPaul Mackerras 		return;
3697568cb4eSPaul Mackerras 	}
3707568cb4eSPaul Mackerras 	bus->secondary = hose->first_busno;
3717568cb4eSPaul Mackerras 	hose->bus = bus;
3727568cb4eSPaul Mackerras 
3733fd94c6bSBenjamin Herrenschmidt 	/* Get some IO space for the new PHB */
3743d5134eeSBenjamin Herrenschmidt 	pcibios_map_io_space(bus);
3753d5134eeSBenjamin Herrenschmidt 
3763fd94c6bSBenjamin Herrenschmidt 	/* Wire up PHB bus resources */
37750c9bc2fSBenjamin Herrenschmidt 	DBG("PCI: PHB IO resource    = %016lx-%016lx [%lx]\n",
37850c9bc2fSBenjamin Herrenschmidt 	    hose->io_resource.start, hose->io_resource.end,
37950c9bc2fSBenjamin Herrenschmidt 	    hose->io_resource.flags);
3809d5f4928SBenjamin Herrenschmidt 	bus->resource[0] = &hose->io_resource;
38150c9bc2fSBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
38250c9bc2fSBenjamin Herrenschmidt 		DBG("PCI: PHB MEM resource %d = %016lx-%016lx [%lx]\n", i,
38350c9bc2fSBenjamin Herrenschmidt 		    hose->mem_resources[i].start,
38450c9bc2fSBenjamin Herrenschmidt 		    hose->mem_resources[i].end,
38550c9bc2fSBenjamin Herrenschmidt 		    hose->mem_resources[i].flags);
3863fd94c6bSBenjamin Herrenschmidt 		bus->resource[i+1] = &hose->mem_resources[i];
38750c9bc2fSBenjamin Herrenschmidt 	}
38850c9bc2fSBenjamin Herrenschmidt 	DBG("PCI: PHB MEM offset     = %016lx\n", hose->pci_mem_offset);
38950c9bc2fSBenjamin Herrenschmidt 	DBG("PCI: PHB IO  offset     = %08lx\n",
39050c9bc2fSBenjamin Herrenschmidt 	    (unsigned long)hose->io_base_virt - _IO_BASE);
3917568cb4eSPaul Mackerras 
3923fd94c6bSBenjamin Herrenschmidt 	/* Get probe mode and perform scan */
3937568cb4eSPaul Mackerras 	mode = PCI_PROBE_NORMAL;
3941beb6a7dSBenjamin Herrenschmidt 	if (node && ppc_md.pci_probe_mode)
3957568cb4eSPaul Mackerras 		mode = ppc_md.pci_probe_mode(bus);
3961beb6a7dSBenjamin Herrenschmidt 	DBG("    probe mode: %d\n", mode);
3977568cb4eSPaul Mackerras 	if (mode == PCI_PROBE_DEVTREE) {
3987568cb4eSPaul Mackerras 		bus->subordinate = hose->last_busno;
3997568cb4eSPaul Mackerras 		of_scan_bus(node, bus);
4007568cb4eSPaul Mackerras 	}
40199a565baSs.hauer@pengutronix.de 
4027568cb4eSPaul Mackerras 	if (mode == PCI_PROBE_NORMAL)
4037568cb4eSPaul Mackerras 		hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
4047568cb4eSPaul Mackerras }
4057568cb4eSPaul Mackerras 
4067568cb4eSPaul Mackerras static int __init pcibios_init(void)
4077568cb4eSPaul Mackerras {
4087568cb4eSPaul Mackerras 	struct pci_controller *hose, *tmp;
4097568cb4eSPaul Mackerras 
4103fd94c6bSBenjamin Herrenschmidt 	printk(KERN_INFO "PCI: Probing PCI hardware\n");
4113fd94c6bSBenjamin Herrenschmidt 
4127568cb4eSPaul Mackerras 	/* For now, override phys_mem_access_prot. If we need it,
4137568cb4eSPaul Mackerras 	 * later, we may move that initialization to each ppc_md
4147568cb4eSPaul Mackerras 	 */
4157568cb4eSPaul Mackerras 	ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
4167568cb4eSPaul Mackerras 
4173fd94c6bSBenjamin Herrenschmidt 	if (pci_probe_only)
4183fd94c6bSBenjamin Herrenschmidt 		ppc_pci_flags |= PPC_PCI_PROBE_ONLY;
4197568cb4eSPaul Mackerras 
4207568cb4eSPaul Mackerras 	/* Scan all of the recorded PCI controllers.  */
42192eb4602SJohn Rose 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
4227568cb4eSPaul Mackerras 		scan_phb(hose);
42392eb4602SJohn Rose 		pci_bus_add_devices(hose->bus);
42492eb4602SJohn Rose 	}
4257568cb4eSPaul Mackerras 
4263fd94c6bSBenjamin Herrenschmidt 	/* Call common code to handle resource allocation */
4273fd94c6bSBenjamin Herrenschmidt 	pcibios_resource_survey();
4287568cb4eSPaul Mackerras 
429e884e9c5SOlof Johansson 	printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
4307568cb4eSPaul Mackerras 
4317568cb4eSPaul Mackerras 	return 0;
4327568cb4eSPaul Mackerras }
4337568cb4eSPaul Mackerras 
4347568cb4eSPaul Mackerras subsys_initcall(pcibios_init);
4357568cb4eSPaul Mackerras 
4363d5134eeSBenjamin Herrenschmidt #ifdef CONFIG_HOTPLUG
4373d5134eeSBenjamin Herrenschmidt 
4383d5134eeSBenjamin Herrenschmidt int pcibios_unmap_io_space(struct pci_bus *bus)
4397568cb4eSPaul Mackerras {
4403d5134eeSBenjamin Herrenschmidt 	struct pci_controller *hose;
4417568cb4eSPaul Mackerras 
4423d5134eeSBenjamin Herrenschmidt 	WARN_ON(bus == NULL);
443de821204SBenjamin Herrenschmidt 
4443d5134eeSBenjamin Herrenschmidt 	/* If this is not a PHB, we only flush the hash table over
4453d5134eeSBenjamin Herrenschmidt 	 * the area mapped by this bridge. We don't play with the PTE
4463d5134eeSBenjamin Herrenschmidt 	 * mappings since we might have to deal with sub-page alignemnts
4473d5134eeSBenjamin Herrenschmidt 	 * so flushing the hash table is the only sane way to make sure
4483d5134eeSBenjamin Herrenschmidt 	 * that no hash entries are covering that removed bridge area
4493d5134eeSBenjamin Herrenschmidt 	 * while still allowing other busses overlapping those pages
450de821204SBenjamin Herrenschmidt 	 */
4513d5134eeSBenjamin Herrenschmidt 	if (bus->self) {
4523d5134eeSBenjamin Herrenschmidt 		struct resource *res = bus->resource[0];
4533d5134eeSBenjamin Herrenschmidt 
4543d5134eeSBenjamin Herrenschmidt 		DBG("IO unmapping for PCI-PCI bridge %s\n",
4553d5134eeSBenjamin Herrenschmidt 		    pci_name(bus->self));
4563d5134eeSBenjamin Herrenschmidt 
4573d5134eeSBenjamin Herrenschmidt 		__flush_hash_table_range(&init_mm, res->start + _IO_BASE,
4583d5134eeSBenjamin Herrenschmidt 					 res->end - res->start + 1);
4593d5134eeSBenjamin Herrenschmidt 		return 0;
4607568cb4eSPaul Mackerras 	}
4617568cb4eSPaul Mackerras 
4623d5134eeSBenjamin Herrenschmidt 	/* Get the host bridge */
4633d5134eeSBenjamin Herrenschmidt 	hose = pci_bus_to_host(bus);
4643d5134eeSBenjamin Herrenschmidt 
4653d5134eeSBenjamin Herrenschmidt 	/* Check if we have IOs allocated */
4663d5134eeSBenjamin Herrenschmidt 	if (hose->io_base_alloc == 0)
4673d5134eeSBenjamin Herrenschmidt 		return 0;
4683d5134eeSBenjamin Herrenschmidt 
46944ef3390SStephen Rothwell 	DBG("IO unmapping for PHB %s\n", hose->dn->full_name);
4703d5134eeSBenjamin Herrenschmidt 	DBG("  alloc=0x%p\n", hose->io_base_alloc);
4713d5134eeSBenjamin Herrenschmidt 
4723d5134eeSBenjamin Herrenschmidt 	/* This is a PHB, we fully unmap the IO area */
4733d5134eeSBenjamin Herrenschmidt 	vunmap(hose->io_base_alloc);
4743d5134eeSBenjamin Herrenschmidt 
4753d5134eeSBenjamin Herrenschmidt 	return 0;
4763d5134eeSBenjamin Herrenschmidt }
4773d5134eeSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
4783d5134eeSBenjamin Herrenschmidt 
4793d5134eeSBenjamin Herrenschmidt #endif /* CONFIG_HOTPLUG */
4803d5134eeSBenjamin Herrenschmidt 
4813d5134eeSBenjamin Herrenschmidt int __devinit pcibios_map_io_space(struct pci_bus *bus)
4827568cb4eSPaul Mackerras {
4833d5134eeSBenjamin Herrenschmidt 	struct vm_struct *area;
4843d5134eeSBenjamin Herrenschmidt 	unsigned long phys_page;
4853d5134eeSBenjamin Herrenschmidt 	unsigned long size_page;
4867568cb4eSPaul Mackerras 	unsigned long io_virt_offset;
4873d5134eeSBenjamin Herrenschmidt 	struct pci_controller *hose;
4887568cb4eSPaul Mackerras 
4893d5134eeSBenjamin Herrenschmidt 	WARN_ON(bus == NULL);
490de821204SBenjamin Herrenschmidt 
4913d5134eeSBenjamin Herrenschmidt 	/* If this not a PHB, nothing to do, page tables still exist and
4923d5134eeSBenjamin Herrenschmidt 	 * thus HPTEs will be faulted in when needed
4933d5134eeSBenjamin Herrenschmidt 	 */
4943d5134eeSBenjamin Herrenschmidt 	if (bus->self) {
4953d5134eeSBenjamin Herrenschmidt 		DBG("IO mapping for PCI-PCI bridge %s\n",
4963d5134eeSBenjamin Herrenschmidt 		    pci_name(bus->self));
4973d5134eeSBenjamin Herrenschmidt 		DBG("  virt=0x%016lx...0x%016lx\n",
4983d5134eeSBenjamin Herrenschmidt 		    bus->resource[0]->start + _IO_BASE,
4993d5134eeSBenjamin Herrenschmidt 		    bus->resource[0]->end + _IO_BASE);
5007568cb4eSPaul Mackerras 		return 0;
5017568cb4eSPaul Mackerras 	}
5027568cb4eSPaul Mackerras 
5033d5134eeSBenjamin Herrenschmidt 	/* Get the host bridge */
5043d5134eeSBenjamin Herrenschmidt 	hose = pci_bus_to_host(bus);
5053d5134eeSBenjamin Herrenschmidt 	phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
5063d5134eeSBenjamin Herrenschmidt 	size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
5077568cb4eSPaul Mackerras 
5083d5134eeSBenjamin Herrenschmidt 	/* Make sure IO area address is clear */
5093d5134eeSBenjamin Herrenschmidt 	hose->io_base_alloc = NULL;
5107568cb4eSPaul Mackerras 
5113d5134eeSBenjamin Herrenschmidt 	/* If there's no IO to map on that bus, get away too */
5123d5134eeSBenjamin Herrenschmidt 	if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
5133d5134eeSBenjamin Herrenschmidt 		return 0;
5143d5134eeSBenjamin Herrenschmidt 
5153d5134eeSBenjamin Herrenschmidt 	/* Let's allocate some IO space for that guy. We don't pass
5163d5134eeSBenjamin Herrenschmidt 	 * VM_IOREMAP because we don't care about alignment tricks that
5173d5134eeSBenjamin Herrenschmidt 	 * the core does in that case. Maybe we should due to stupid card
5183d5134eeSBenjamin Herrenschmidt 	 * with incomplete address decoding but I'd rather not deal with
5193d5134eeSBenjamin Herrenschmidt 	 * those outside of the reserved 64K legacy region.
5203d5134eeSBenjamin Herrenschmidt 	 */
5213d5134eeSBenjamin Herrenschmidt 	area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
5223d5134eeSBenjamin Herrenschmidt 	if (area == NULL)
5233d5134eeSBenjamin Herrenschmidt 		return -ENOMEM;
5243d5134eeSBenjamin Herrenschmidt 	hose->io_base_alloc = area->addr;
5253d5134eeSBenjamin Herrenschmidt 	hose->io_base_virt = (void __iomem *)(area->addr +
5263d5134eeSBenjamin Herrenschmidt 					      hose->io_base_phys - phys_page);
5273d5134eeSBenjamin Herrenschmidt 
52844ef3390SStephen Rothwell 	DBG("IO mapping for PHB %s\n", hose->dn->full_name);
5293d5134eeSBenjamin Herrenschmidt 	DBG("  phys=0x%016lx, virt=0x%p (alloc=0x%p)\n",
5303d5134eeSBenjamin Herrenschmidt 	    hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
5313d5134eeSBenjamin Herrenschmidt 	DBG("  size=0x%016lx (alloc=0x%016lx)\n",
5323d5134eeSBenjamin Herrenschmidt 	    hose->pci_io_size, size_page);
5333d5134eeSBenjamin Herrenschmidt 
5343d5134eeSBenjamin Herrenschmidt 	/* Establish the mapping */
5353d5134eeSBenjamin Herrenschmidt 	if (__ioremap_at(phys_page, area->addr, size_page,
5363d5134eeSBenjamin Herrenschmidt 			 _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
5373d5134eeSBenjamin Herrenschmidt 		return -ENOMEM;
5383d5134eeSBenjamin Herrenschmidt 
5393d5134eeSBenjamin Herrenschmidt 	/* Fixup hose IO resource */
5403d5134eeSBenjamin Herrenschmidt 	io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
5413d5134eeSBenjamin Herrenschmidt 	hose->io_resource.start += io_virt_offset;
5423d5134eeSBenjamin Herrenschmidt 	hose->io_resource.end += io_virt_offset;
5433d5134eeSBenjamin Herrenschmidt 
5443d5134eeSBenjamin Herrenschmidt 	DBG("  hose->io_resource=0x%016lx...0x%016lx\n",
5453d5134eeSBenjamin Herrenschmidt 	    hose->io_resource.start, hose->io_resource.end);
5467568cb4eSPaul Mackerras 
5477568cb4eSPaul Mackerras 	return 0;
5487568cb4eSPaul Mackerras }
5493d5134eeSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_map_io_space);
5507568cb4eSPaul Mackerras 
55112d04eefSBenjamin Herrenschmidt void __devinit pcibios_setup_new_device(struct pci_dev *dev)
55212d04eefSBenjamin Herrenschmidt {
55312d04eefSBenjamin Herrenschmidt 	struct dev_archdata *sd = &dev->dev.archdata;
55412d04eefSBenjamin Herrenschmidt 
55512d04eefSBenjamin Herrenschmidt 	sd->of_node = pci_device_to_OF_node(dev);
55612d04eefSBenjamin Herrenschmidt 
557bf5e2ba2SBenjamin Herrenschmidt 	DBG("PCI: device %s OF node: %s\n", pci_name(dev),
55812d04eefSBenjamin Herrenschmidt 	    sd->of_node ? sd->of_node->full_name : "<none>");
55912d04eefSBenjamin Herrenschmidt 
56012d04eefSBenjamin Herrenschmidt 	sd->dma_ops = pci_dma_ops;
56112d04eefSBenjamin Herrenschmidt #ifdef CONFIG_NUMA
56212d04eefSBenjamin Herrenschmidt 	sd->numa_node = pcibus_to_node(dev->bus);
56312d04eefSBenjamin Herrenschmidt #else
56412d04eefSBenjamin Herrenschmidt 	sd->numa_node = -1;
56512d04eefSBenjamin Herrenschmidt #endif
56612d04eefSBenjamin Herrenschmidt 	if (ppc_md.pci_dma_dev_setup)
56712d04eefSBenjamin Herrenschmidt 		ppc_md.pci_dma_dev_setup(dev);
56812d04eefSBenjamin Herrenschmidt }
56912d04eefSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_setup_new_device);
570463ce0e1SBenjamin Herrenschmidt 
571bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_do_bus_setup(struct pci_bus *bus)
5727568cb4eSPaul Mackerras {
5737568cb4eSPaul Mackerras 	struct pci_dev *dev;
5747568cb4eSPaul Mackerras 
57512d04eefSBenjamin Herrenschmidt 	if (ppc_md.pci_dma_bus_setup)
57612d04eefSBenjamin Herrenschmidt 		ppc_md.pci_dma_bus_setup(bus);
5777568cb4eSPaul Mackerras 
5787568cb4eSPaul Mackerras 	list_for_each_entry(dev, &bus->devices, bus_list)
57912d04eefSBenjamin Herrenschmidt 		pcibios_setup_new_device(dev);
580f90bb153SBenjamin Herrenschmidt }
5817568cb4eSPaul Mackerras 
582f2c4583aSBenjamin Herrenschmidt unsigned long pci_address_to_pio(phys_addr_t address)
583d4e4b352SStephen Rothwell {
584d4e4b352SStephen Rothwell 	struct pci_controller *hose, *tmp;
585d4e4b352SStephen Rothwell 
586d4e4b352SStephen Rothwell 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
587d4e4b352SStephen Rothwell 		if (address >= hose->io_base_phys &&
588f2c4583aSBenjamin Herrenschmidt 		    address < (hose->io_base_phys + hose->pci_io_size)) {
589f2c4583aSBenjamin Herrenschmidt 			unsigned long base =
5903d5134eeSBenjamin Herrenschmidt 				(unsigned long)hose->io_base_virt - _IO_BASE;
591f2c4583aSBenjamin Herrenschmidt 			return base + (address - hose->io_base_phys);
592f2c4583aSBenjamin Herrenschmidt 		}
593d4e4b352SStephen Rothwell 	}
594d4e4b352SStephen Rothwell 	return (unsigned int)-1;
595d4e4b352SStephen Rothwell }
596d4e4b352SStephen Rothwell EXPORT_SYMBOL_GPL(pci_address_to_pio);
597d4e4b352SStephen Rothwell 
5987568cb4eSPaul Mackerras 
5997568cb4eSPaul Mackerras #define IOBASE_BRIDGE_NUMBER	0
6007568cb4eSPaul Mackerras #define IOBASE_MEMORY		1
6017568cb4eSPaul Mackerras #define IOBASE_IO		2
6027568cb4eSPaul Mackerras #define IOBASE_ISA_IO		3
6037568cb4eSPaul Mackerras #define IOBASE_ISA_MEM		4
6047568cb4eSPaul Mackerras 
6057568cb4eSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long in_bus,
6067568cb4eSPaul Mackerras 			  unsigned long in_devfn)
6077568cb4eSPaul Mackerras {
6087568cb4eSPaul Mackerras 	struct pci_controller* hose;
6097568cb4eSPaul Mackerras 	struct list_head *ln;
6107568cb4eSPaul Mackerras 	struct pci_bus *bus = NULL;
6117568cb4eSPaul Mackerras 	struct device_node *hose_node;
6127568cb4eSPaul Mackerras 
6137568cb4eSPaul Mackerras 	/* Argh ! Please forgive me for that hack, but that's the
6147568cb4eSPaul Mackerras 	 * simplest way to get existing XFree to not lockup on some
6157568cb4eSPaul Mackerras 	 * G5 machines... So when something asks for bus 0 io base
6167568cb4eSPaul Mackerras 	 * (bus 0 is HT root), we return the AGP one instead.
6177568cb4eSPaul Mackerras 	 */
6187568cb4eSPaul Mackerras 	if (machine_is_compatible("MacRISC4"))
6197568cb4eSPaul Mackerras 		if (in_bus == 0)
6207568cb4eSPaul Mackerras 			in_bus = 0xf0;
6217568cb4eSPaul Mackerras 
6227568cb4eSPaul Mackerras 	/* That syscall isn't quite compatible with PCI domains, but it's
6237568cb4eSPaul Mackerras 	 * used on pre-domains setup. We return the first match
6247568cb4eSPaul Mackerras 	 */
6257568cb4eSPaul Mackerras 
6267568cb4eSPaul Mackerras 	for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
6277568cb4eSPaul Mackerras 		bus = pci_bus_b(ln);
628545da94fSBenjamin Herrenschmidt 		if (in_bus >= bus->number && in_bus <= bus->subordinate)
6297568cb4eSPaul Mackerras 			break;
6307568cb4eSPaul Mackerras 		bus = NULL;
6317568cb4eSPaul Mackerras 	}
6327568cb4eSPaul Mackerras 	if (bus == NULL || bus->sysdata == NULL)
6337568cb4eSPaul Mackerras 		return -ENODEV;
6347568cb4eSPaul Mackerras 
6357568cb4eSPaul Mackerras 	hose_node = (struct device_node *)bus->sysdata;
6367568cb4eSPaul Mackerras 	hose = PCI_DN(hose_node)->phb;
6377568cb4eSPaul Mackerras 
6387568cb4eSPaul Mackerras 	switch (which) {
6397568cb4eSPaul Mackerras 	case IOBASE_BRIDGE_NUMBER:
6407568cb4eSPaul Mackerras 		return (long)hose->first_busno;
6417568cb4eSPaul Mackerras 	case IOBASE_MEMORY:
6427568cb4eSPaul Mackerras 		return (long)hose->pci_mem_offset;
6437568cb4eSPaul Mackerras 	case IOBASE_IO:
6447568cb4eSPaul Mackerras 		return (long)hose->io_base_phys;
6457568cb4eSPaul Mackerras 	case IOBASE_ISA_IO:
6467568cb4eSPaul Mackerras 		return (long)isa_io_base;
6477568cb4eSPaul Mackerras 	case IOBASE_ISA_MEM:
6487568cb4eSPaul Mackerras 		return -EINVAL;
6497568cb4eSPaul Mackerras 	}
6507568cb4eSPaul Mackerras 
6517568cb4eSPaul Mackerras 	return -EOPNOTSUPP;
6527568cb4eSPaul Mackerras }
653357518faSAnton Blanchard 
654357518faSAnton Blanchard #ifdef CONFIG_NUMA
655357518faSAnton Blanchard int pcibus_to_node(struct pci_bus *bus)
656357518faSAnton Blanchard {
657357518faSAnton Blanchard 	struct pci_controller *phb = pci_bus_to_host(bus);
658357518faSAnton Blanchard 	return phb->node;
659357518faSAnton Blanchard }
660357518faSAnton Blanchard EXPORT_SYMBOL(pcibus_to_node);
661357518faSAnton Blanchard #endif
662