xref: /openbmc/linux/arch/powerpc/kernel/pci_dn.c (revision c6296b962702d73413a66a544490c275bc0f4300)
17568cb4eSPaul Mackerras /*
27568cb4eSPaul Mackerras  * pci_dn.c
37568cb4eSPaul Mackerras  *
47568cb4eSPaul Mackerras  * Copyright (C) 2001 Todd Inglett, IBM Corporation
57568cb4eSPaul Mackerras  *
67568cb4eSPaul Mackerras  * PCI manipulation via device_nodes.
77568cb4eSPaul Mackerras  *
87568cb4eSPaul Mackerras  * This program is free software; you can redistribute it and/or modify
97568cb4eSPaul Mackerras  * it under the terms of the GNU General Public License as published by
107568cb4eSPaul Mackerras  * the Free Software Foundation; either version 2 of the License, or
117568cb4eSPaul Mackerras  * (at your option) any later version.
127568cb4eSPaul Mackerras  *
137568cb4eSPaul Mackerras  * This program is distributed in the hope that it will be useful,
147568cb4eSPaul Mackerras  * but WITHOUT ANY WARRANTY; without even the implied warranty of
157568cb4eSPaul Mackerras  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
167568cb4eSPaul Mackerras  * GNU General Public License for more details.
177568cb4eSPaul Mackerras  *
187568cb4eSPaul Mackerras  * You should have received a copy of the GNU General Public License
197568cb4eSPaul Mackerras  * along with this program; if not, write to the Free Software
207568cb4eSPaul Mackerras  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
217568cb4eSPaul Mackerras  */
227568cb4eSPaul Mackerras #include <linux/kernel.h>
237568cb4eSPaul Mackerras #include <linux/pci.h>
247568cb4eSPaul Mackerras #include <linux/string.h>
2566b15db6SPaul Gortmaker #include <linux/export.h>
267568cb4eSPaul Mackerras #include <linux/init.h>
275a0e3ad6STejun Heo #include <linux/gfp.h>
287568cb4eSPaul Mackerras 
297568cb4eSPaul Mackerras #include <asm/io.h>
307568cb4eSPaul Mackerras #include <asm/prom.h>
317568cb4eSPaul Mackerras #include <asm/pci-bridge.h>
327568cb4eSPaul Mackerras #include <asm/ppc-pci.h>
33095eed4fSStephen Rothwell #include <asm/firmware.h>
347568cb4eSPaul Mackerras 
35b72c1f65SBenjamin Herrenschmidt struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
36b72c1f65SBenjamin Herrenschmidt {
37b72c1f65SBenjamin Herrenschmidt 	struct device_node *dn = pci_device_to_OF_node(pdev);
38b72c1f65SBenjamin Herrenschmidt 	if (!dn)
39b72c1f65SBenjamin Herrenschmidt 		return NULL;
40b72c1f65SBenjamin Herrenschmidt 	return PCI_DN(dn);
41b72c1f65SBenjamin Herrenschmidt }
42b72c1f65SBenjamin Herrenschmidt 
437568cb4eSPaul Mackerras /*
447568cb4eSPaul Mackerras  * Traverse_func that inits the PCI fields of the device node.
457568cb4eSPaul Mackerras  * NOTE: this *must* be done before read/write config to the device.
467568cb4eSPaul Mackerras  */
47cad5cef6SGreg Kroah-Hartman void *update_dn_pci_info(struct device_node *dn, void *data)
487568cb4eSPaul Mackerras {
497568cb4eSPaul Mackerras 	struct pci_controller *phb = data;
50*c6296b96SAnton Blanchard 	const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
51*c6296b96SAnton Blanchard 	const __be32 *regs;
527568cb4eSPaul Mackerras 	struct pci_dn *pdn;
537568cb4eSPaul Mackerras 
54a56555e5SMilton Miller 	pdn = zalloc_maybe_bootmem(sizeof(*pdn), GFP_KERNEL);
557568cb4eSPaul Mackerras 	if (pdn == NULL)
567568cb4eSPaul Mackerras 		return NULL;
577568cb4eSPaul Mackerras 	dn->data = pdn;
587568cb4eSPaul Mackerras 	pdn->node = dn;
597568cb4eSPaul Mackerras 	pdn->phb = phb;
60184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
61184cd4a3SBenjamin Herrenschmidt 	pdn->pe_number = IODA_INVALID_PE;
62184cd4a3SBenjamin Herrenschmidt #endif
63e2eb6392SStephen Rothwell 	regs = of_get_property(dn, "reg", NULL);
647568cb4eSPaul Mackerras 	if (regs) {
65*c6296b96SAnton Blanchard 		u32 addr = of_read_number(regs, 1);
66*c6296b96SAnton Blanchard 
677568cb4eSPaul Mackerras 		/* First register entry is addr (00BBSS00)  */
68*c6296b96SAnton Blanchard 		pdn->busno = (addr >> 16) & 0xff;
69*c6296b96SAnton Blanchard 		pdn->devfn = (addr >> 8) & 0xff;
707568cb4eSPaul Mackerras 	}
717568cb4eSPaul Mackerras 
72*c6296b96SAnton Blanchard 	pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
737568cb4eSPaul Mackerras 	return NULL;
747568cb4eSPaul Mackerras }
757568cb4eSPaul Mackerras 
767568cb4eSPaul Mackerras /*
777568cb4eSPaul Mackerras  * Traverse a device tree stopping each PCI device in the tree.
787568cb4eSPaul Mackerras  * This is done depth first.  As each node is processed, a "pre"
797568cb4eSPaul Mackerras  * function is called and the children are processed recursively.
807568cb4eSPaul Mackerras  *
817568cb4eSPaul Mackerras  * The "pre" func returns a value.  If non-zero is returned from
827568cb4eSPaul Mackerras  * the "pre" func, the traversal stops and this value is returned.
837568cb4eSPaul Mackerras  * This return value is useful when using traverse as a method of
847568cb4eSPaul Mackerras  * finding a device.
857568cb4eSPaul Mackerras  *
867568cb4eSPaul Mackerras  * NOTE: we do not run the func for devices that do not appear to
877568cb4eSPaul Mackerras  * be PCI except for the start node which we assume (this is good
887568cb4eSPaul Mackerras  * because the start node is often a phb which may be missing PCI
897568cb4eSPaul Mackerras  * properties).
907568cb4eSPaul Mackerras  * We use the class-code as an indicator. If we run into
917568cb4eSPaul Mackerras  * one of these nodes we also assume its siblings are non-pci for
927568cb4eSPaul Mackerras  * performance.
937568cb4eSPaul Mackerras  */
947568cb4eSPaul Mackerras void *traverse_pci_devices(struct device_node *start, traverse_func pre,
957568cb4eSPaul Mackerras 		void *data)
967568cb4eSPaul Mackerras {
977568cb4eSPaul Mackerras 	struct device_node *dn, *nextdn;
987568cb4eSPaul Mackerras 	void *ret;
997568cb4eSPaul Mackerras 
1007568cb4eSPaul Mackerras 	/* We started with a phb, iterate all childs */
1017568cb4eSPaul Mackerras 	for (dn = start->child; dn; dn = nextdn) {
102*c6296b96SAnton Blanchard 		const __be32 *classp;
103*c6296b96SAnton Blanchard 		u32 class = 0;
1047568cb4eSPaul Mackerras 
1057568cb4eSPaul Mackerras 		nextdn = NULL;
106e2eb6392SStephen Rothwell 		classp = of_get_property(dn, "class-code", NULL);
107*c6296b96SAnton Blanchard 		if (classp)
108*c6296b96SAnton Blanchard 			class = of_read_number(classp, 1);
1097568cb4eSPaul Mackerras 
1107568cb4eSPaul Mackerras 		if (pre && ((ret = pre(dn, data)) != NULL))
1117568cb4eSPaul Mackerras 			return ret;
1127568cb4eSPaul Mackerras 
1137568cb4eSPaul Mackerras 		/* If we are a PCI bridge, go down */
1147568cb4eSPaul Mackerras 		if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
1157568cb4eSPaul Mackerras 				  (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
1167568cb4eSPaul Mackerras 			/* Depth first...do children */
1177568cb4eSPaul Mackerras 			nextdn = dn->child;
1187568cb4eSPaul Mackerras 		else if (dn->sibling)
1197568cb4eSPaul Mackerras 			/* ok, try next sibling instead. */
1207568cb4eSPaul Mackerras 			nextdn = dn->sibling;
1217568cb4eSPaul Mackerras 		if (!nextdn) {
1227568cb4eSPaul Mackerras 			/* Walk up to next valid sibling. */
1237568cb4eSPaul Mackerras 			do {
1247568cb4eSPaul Mackerras 				dn = dn->parent;
1257568cb4eSPaul Mackerras 				if (dn == start)
1267568cb4eSPaul Mackerras 					return NULL;
1277568cb4eSPaul Mackerras 			} while (dn->sibling == NULL);
1287568cb4eSPaul Mackerras 			nextdn = dn->sibling;
1297568cb4eSPaul Mackerras 		}
1307568cb4eSPaul Mackerras 	}
1317568cb4eSPaul Mackerras 	return NULL;
1327568cb4eSPaul Mackerras }
1337568cb4eSPaul Mackerras 
1347568cb4eSPaul Mackerras /**
1357568cb4eSPaul Mackerras  * pci_devs_phb_init_dynamic - setup pci devices under this PHB
1367568cb4eSPaul Mackerras  * phb: pci-to-host bridge (top-level bridge connecting to cpu)
1377568cb4eSPaul Mackerras  *
1387568cb4eSPaul Mackerras  * This routine is called both during boot, (before the memory
1397568cb4eSPaul Mackerras  * subsystem is set up, before kmalloc is valid) and during the
1407568cb4eSPaul Mackerras  * dynamic lpar operation of adding a PHB to a running system.
1417568cb4eSPaul Mackerras  */
142cad5cef6SGreg Kroah-Hartman void pci_devs_phb_init_dynamic(struct pci_controller *phb)
1437568cb4eSPaul Mackerras {
14444ef3390SStephen Rothwell 	struct device_node *dn = phb->dn;
1457568cb4eSPaul Mackerras 	struct pci_dn *pdn;
1467568cb4eSPaul Mackerras 
1477568cb4eSPaul Mackerras 	/* PHB nodes themselves must not match */
1487568cb4eSPaul Mackerras 	update_dn_pci_info(dn, phb);
1497568cb4eSPaul Mackerras 	pdn = dn->data;
1507568cb4eSPaul Mackerras 	if (pdn) {
1517568cb4eSPaul Mackerras 		pdn->devfn = pdn->busno = -1;
1527568cb4eSPaul Mackerras 		pdn->phb = phb;
1537568cb4eSPaul Mackerras 	}
1547568cb4eSPaul Mackerras 
1557568cb4eSPaul Mackerras 	/* Update dn->phb ptrs for new phb and children devices */
1567568cb4eSPaul Mackerras 	traverse_pci_devices(dn, update_dn_pci_info, phb);
1577568cb4eSPaul Mackerras }
1587568cb4eSPaul Mackerras 
1597568cb4eSPaul Mackerras /**
1607568cb4eSPaul Mackerras  * pci_devs_phb_init - Initialize phbs and pci devs under them.
1617568cb4eSPaul Mackerras  *
1627568cb4eSPaul Mackerras  * This routine walks over all phb's (pci-host bridges) on the
1637568cb4eSPaul Mackerras  * system, and sets up assorted pci-related structures
1647568cb4eSPaul Mackerras  * (including pci info in the device node structs) for each
1657568cb4eSPaul Mackerras  * pci device found underneath.  This routine runs once,
1667568cb4eSPaul Mackerras  * early in the boot sequence.
1677568cb4eSPaul Mackerras  */
1687568cb4eSPaul Mackerras void __init pci_devs_phb_init(void)
1697568cb4eSPaul Mackerras {
1707568cb4eSPaul Mackerras 	struct pci_controller *phb, *tmp;
1717568cb4eSPaul Mackerras 
1727568cb4eSPaul Mackerras 	/* This must be done first so the device nodes have valid pci info! */
1737568cb4eSPaul Mackerras 	list_for_each_entry_safe(phb, tmp, &hose_list, list_node)
1747568cb4eSPaul Mackerras 		pci_devs_phb_init_dynamic(phb);
1757568cb4eSPaul Mackerras }
176