ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / pci_dn.c
1 /*
2  * pci_dn.c
3  *
4  * Copyright (C) 2001 Todd Inglett, IBM Corporation
5  *
6  * PCI manipulation via device_nodes.
7  *
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.
12  *    
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.
17  * 
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
21  */
22
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>
30
31 #include <asm/io.h>
32 #include <asm/pgtable.h>
33 #include <asm/irq.h>
34 #include <asm/prom.h>
35 #include <asm/machdep.h>
36 #include <asm/pci-bridge.h>
37 #include <asm/ppcdebug.h>
38 #include <asm/naca.h>
39 #include <asm/iommu.h>
40
41 #include "pci.h"
42
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.
45  */
46 static void * __init
47 update_dn_pci_info(struct device_node *dn, void *data)
48 {
49 #ifdef CONFIG_PPC_PSERIES
50         struct pci_controller *phb = (struct pci_controller *)data;
51         u32 *regs;
52         char *device_type = get_property(dn, "device_type", 0);
53         char *model;
54
55         dn->phb = phb;
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);
59                 dn->busno = regs[0];
60
61                 model = (char *)get_property(dn, "model", NULL);
62
63                 if (strstr(model, "U3"))
64                         dn->devfn = -1;
65                 else
66                         dn->devfn = 0;  /* assumption */
67         } else {
68                 regs = (u32 *)get_property(dn, "reg", 0);
69                 if (regs) {
70                         /* First register entry is addr (00BBSS00)  */
71                         dn->busno = (regs[0] >> 16) & 0xff;
72                         dn->devfn = (regs[0] >> 8) & 0xff;
73                 }
74         }
75 #endif
76         return NULL;
77 }
78
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.
84  *
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.
90  *
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
94  * properties).
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
97  * performance.
98  *
99  ******************************************************************/
100 void *traverse_pci_devices(struct device_node *start, traverse_func pre, traverse_func post, void *data)
101 {
102         struct device_node *dn, *nextdn;
103         void *ret;
104
105         if (pre && (ret = pre(start, data)) != NULL)
106                 return ret;
107         for (dn = start->child; dn; dn = nextdn) {
108                 nextdn = NULL;
109 #ifdef CONFIG_PPC_PSERIES
110                 if (get_property(dn, "class-code", 0)) {
111                         if (pre && (ret = pre(dn, data)) != NULL)
112                                 return ret;
113                         if (dn->child) {
114                                 /* Depth first...do children */
115                                 nextdn = dn->child;
116                         } else if (dn->sibling) {
117                                 /* ok, try next sibling instead. */
118                                 nextdn = dn->sibling;
119                         } else {
120                                 /* no more children or siblings...call "post" */
121                                 if (post)
122                                         post(dn, data);
123                         }
124                 }
125 #endif
126                 if (!nextdn) {
127                         /* Walk up to next valid sibling. */
128                         do {
129                                 dn = dn->parent;
130                                 if (dn == start)
131                                         return NULL;
132                         } while (dn->sibling == NULL);
133                         nextdn = dn->sibling;
134                 }
135         }
136         return NULL;
137 }
138
139 /* Same as traverse_pci_devices except this does it for all phbs.
140  */
141 void *traverse_all_pci_devices(traverse_func pre)
142 {
143         struct pci_controller* phb;
144         void *ret;
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)
147                         return ret;
148         return NULL;
149 }
150
151
152 /* Traversal func that looks for a <busno,devfcn> value.
153  * If found, the device_node is returned (thus terminating the traversal).
154  */
155 static void *
156 is_devfn_node(struct device_node *dn, void *data)
157 {
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;
161 }
162
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.
166  *
167  * It may also do fixups on the actual device since this happens
168  * on the first read/write.
169  *
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.
173  *
174  */
175 struct device_node *fetch_dev_dn(struct pci_dev *dev)
176 {
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;
182
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);
185         if (dn) {
186                 dev->sysdata = dn;
187                 /* ToDo: call some device init hook here */
188         }
189         return dn;
190 }
191 EXPORT_SYMBOL(fetch_dev_dn);
192
193
194 /******************************************************************
195  * Actually initialize the phbs.
196  * The buswalk on this phb has not happened yet.
197  ******************************************************************/
198 void __init
199 pci_devs_phb_init(void)
200 {
201         /* This must be done first so the device nodes have valid pci info! */
202         traverse_all_pci_devices(update_dn_pci_info);
203 }
204
205
206 static void __init
207 pci_fixup_bus_sysdata_list(struct list_head *bus_list)
208 {
209         struct list_head *ln;
210         struct pci_bus *bus;
211
212         for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
213                 bus = pci_bus_b(ln);
214                 if (bus->self)
215                         bus->sysdata = bus->self->sysdata;
216                 pci_fixup_bus_sysdata_list(&bus->children);
217         }
218 }
219
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
224  * correct.
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  ******************************************************************/
229 void __init
230 pci_fix_bus_sysdata(void)
231 {
232         pci_fixup_bus_sysdata_list(&pci_root_buses);
233 }