4 * Copyright (C) 2001 Todd Inglett, IBM Corporation
6 * PCI manipulation via device_nodes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/pci.h>
26 #include <linux/delay.h>
27 #include <linux/string.h>
28 #include <linux/init.h>
29 #include <linux/bootmem.h>
32 #include <asm/pgtable.h>
35 #include <asm/machdep.h>
36 #include <asm/pci-bridge.h>
37 #include <asm/ppcdebug.h>
39 #include <asm/iommu.h>
43 /* Traverse_func that inits the PCI fields of the device node.
44 * NOTE: this *must* be done before read/write config to the device.
47 update_dn_pci_info(struct device_node *dn, void *data)
49 #ifdef CONFIG_PPC_PSERIES
50 struct pci_controller *phb = (struct pci_controller *)data;
52 char *device_type = get_property(dn, "device_type", 0);
56 if (device_type && strcmp(device_type, "pci") == 0 && get_property(dn, "class-code", 0) == 0) {
57 /* special case for PHB's. Sigh. */
58 regs = (u32 *)get_property(dn, "bus-range", 0);
61 model = (char *)get_property(dn, "model", NULL);
63 if (strstr(model, "U3"))
66 dn->devfn = 0; /* assumption */
68 regs = (u32 *)get_property(dn, "reg", 0);
70 /* First register entry is addr (00BBSS00) */
71 dn->busno = (regs[0] >> 16) & 0xff;
72 dn->devfn = (regs[0] >> 8) & 0xff;
79 /******************************************************************
80 * Traverse a device tree stopping each PCI device in the tree.
81 * This is done depth first. As each node is processed, a "pre"
82 * function is called, the children are processed recursively, and
83 * then a "post" function is called.
85 * The "pre" and "post" funcs return a value. If non-zero
86 * is returned from the "pre" func, the traversal stops and this
87 * value is returned. The return value from "post" is not used.
88 * This return value is useful when using traverse as
89 * a method of finding a device.
91 * NOTE: we do not run the funcs for devices that do not appear to
92 * be PCI except for the start node which we assume (this is good
93 * because the start node is often a phb which may be missing PCI
95 * We use the class-code as an indicator. If we run into
96 * one of these nodes we also assume its siblings are non-pci for
99 ******************************************************************/
100 void *traverse_pci_devices(struct device_node *start, traverse_func pre, traverse_func post, void *data)
102 struct device_node *dn, *nextdn;
105 if (pre && (ret = pre(start, data)) != NULL)
107 for (dn = start->child; dn; dn = nextdn) {
109 #ifdef CONFIG_PPC_PSERIES
110 if (get_property(dn, "class-code", 0)) {
111 if (pre && (ret = pre(dn, data)) != NULL)
114 /* Depth first...do children */
116 } else if (dn->sibling) {
117 /* ok, try next sibling instead. */
118 nextdn = dn->sibling;
120 /* no more children or siblings...call "post" */
127 /* Walk up to next valid sibling. */
132 } while (dn->sibling == NULL);
133 nextdn = dn->sibling;
139 /* Same as traverse_pci_devices except this does it for all phbs.
141 void *traverse_all_pci_devices(traverse_func pre)
143 struct pci_controller* phb;
145 for (phb=hose_head;phb;phb=phb->next)
146 if ((ret = traverse_pci_devices((struct device_node *)phb->arch_data, pre, NULL, phb)) != NULL)
152 /* Traversal func that looks for a <busno,devfcn> value.
153 * If found, the device_node is returned (thus terminating the traversal).
156 is_devfn_node(struct device_node *dn, void *data)
158 int busno = ((unsigned long)data >> 8) & 0xff;
159 int devfn = ((unsigned long)data) & 0xff;
160 return (devfn == dn->devfn && busno == dn->busno) ? dn : NULL;
163 /* This is the "slow" path for looking up a device_node from a
164 * pci_dev. It will hunt for the device under its parent's
165 * phb and then update sysdata for a future fastpath.
167 * It may also do fixups on the actual device since this happens
168 * on the first read/write.
170 * Note that it also must deal with devices that don't exist.
171 * In this case it may probe for real hardware ("just in case")
172 * and add a device_node to the device tree if necessary.
175 struct device_node *fetch_dev_dn(struct pci_dev *dev)
177 struct device_node *orig_dn = (struct device_node *)dev->sysdata;
178 struct pci_controller *phb = orig_dn->phb; /* assume same phb as orig_dn */
179 struct device_node *phb_dn;
180 struct device_node *dn;
181 unsigned long searchval = (dev->bus->number << 8) | dev->devfn;
183 phb_dn = (struct device_node *)(phb->arch_data);
184 dn = (struct device_node *)traverse_pci_devices(phb_dn, is_devfn_node, NULL, (void *)searchval);
187 /* ToDo: call some device init hook here */
191 EXPORT_SYMBOL(fetch_dev_dn);
194 /******************************************************************
195 * Actually initialize the phbs.
196 * The buswalk on this phb has not happened yet.
197 ******************************************************************/
199 pci_devs_phb_init(void)
201 /* This must be done first so the device nodes have valid pci info! */
202 traverse_all_pci_devices(update_dn_pci_info);
207 pci_fixup_bus_sysdata_list(struct list_head *bus_list)
209 struct list_head *ln;
212 for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
215 bus->sysdata = bus->self->sysdata;
216 pci_fixup_bus_sysdata_list(&bus->children);
220 /******************************************************************
221 * Fixup the bus->sysdata ptrs to point to the bus' device_node.
222 * This is done late in pcibios_init(). We do this mostly for
223 * sanity, but pci_dma.c uses these at DMA time so they must be
225 * To do this we recurse down the bus hierarchy. Note that PHB's
226 * have bus->self == NULL, but fortunately bus->sysdata is already
227 * correct in this case.
228 ******************************************************************/
230 pci_fix_bus_sysdata(void)
232 pci_fixup_bus_sysdata_list(&pci_root_buses);