more changes on original files
[linux-2.4.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/init.h>
37 #include <asm/pci-bridge.h>
38 #include <asm/ppcdebug.h>
39 #include <asm/naca.h>
40 #include <asm/pci_dma.h>
41
42 #include "pci.h"
43
44 /* Traverse_func that inits the PCI fields of the device node.
45  * NOTE: this *must* be done before read/write config to the device.
46  */
47 static void * __init
48 update_dn_pci_info(struct device_node *dn, void *data)
49 {
50         struct pci_controller *phb = (struct pci_controller *)data;
51         u32 *regs;
52         char *device_type = get_property(dn, "device_type", 0);
53         char *status = get_property(dn, "status", 0);
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                 dn->devfn = 0;  /* assumption */
61         } else {
62                 regs = (u32 *)get_property(dn, "reg", 0);
63                 if (regs) {
64                         /* First register entry is addr (00BBSS00)  */
65                         dn->busno = (regs[0] >> 16) & 0xff;
66                         dn->devfn = (regs[0] >> 8) & 0xff;
67                 }
68         }
69         if (status && strcmp(status, "ok") != 0) {
70                 char *name = get_property(dn, "name", 0);
71                 printk(KERN_ERR "PCI: %04x:%02x.%x %s (%s) has bad status from firmware! (%s)", dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn), name ? name : "<no name>", device_type ? device_type : "<unknown type>", status);
72                 dn->status = 1;
73         }
74         return NULL;
75 }
76
77 /*
78  * Hit all the BARs of all the devices with values from OF.
79  * This is unnecessary on most systems, but also harmless.
80  */
81 static void * __init
82 write_OF_bars(struct device_node *dn, void *data)
83 {
84 #ifdef CONFIG_PPC_PSERIES
85         int i;
86         u32 oldbar, newbar, newbartest;
87         u8  config_offset;
88 #endif
89         char *name = get_property(dn, "name", 0);
90         char *device_type = get_property(dn, "device_type", 0);
91         char devname[128];
92         sprintf(devname, "%04x:%02x.%x %s (%s)", dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn), name ? name : "<no name>", device_type ? device_type : "<unknown type>");
93
94         if (device_type && strcmp(device_type, "pci") == 0 &&
95             get_property(dn, "class-code", 0) == 0)
96                 return NULL;    /* This is probably a phb.  Skip it. */
97
98         if (dn->n_addrs == 0)
99                 return NULL;    /* This is normal for some adapters or bridges */
100
101         if (dn->addrs == NULL) {
102                 /* This shouldn't happen. */
103                 printk(KERN_WARNING "write_OF_bars %s: device has %d BARs, but no addrs recorded\n", devname, dn->n_addrs);
104                 return NULL;
105         }
106
107 #ifndef CONFIG_PPC_ISERIES 
108         for (i = 0; i < dn->n_addrs; i++) {
109                 newbar = dn->addrs[i].address;
110                 config_offset = dn->addrs[i].space & 0xff;
111                 if (ppc_md.pcibios_read_config_dword(dn, config_offset, &oldbar) != PCIBIOS_SUCCESSFUL) {
112                         printk(KERN_WARNING "write_OF_bars %s: read BAR%d failed\n", devname, i);
113                         continue;
114                 }
115                 /* Need to update this BAR. */
116                 if (ppc_md.pcibios_write_config_dword(dn, config_offset, newbar) != PCIBIOS_SUCCESSFUL) {
117                         printk(KERN_WARNING "write_OF_bars %s: write BAR%d with 0x%08x failed (old was 0x%08x)\n", devname, i, newbar, oldbar);
118                         continue;
119                 }
120                 /* sanity check */
121                 if (ppc_md.pcibios_read_config_dword(dn, config_offset, &newbartest) != PCIBIOS_SUCCESSFUL) {
122                         printk(KERN_WARNING "write_OF_bars %s: sanity test read BAR%d failed?\n", devname, i);
123                         continue;
124                 }
125                 if ((newbar & PCI_BASE_ADDRESS_MEM_MASK) != (newbartest & PCI_BASE_ADDRESS_MEM_MASK)) {
126                         printk(KERN_WARNING "write_OF_bars %s: oops...BAR%d read back as 0x%08x%s!\n", devname, i, newbartest, (oldbar & PCI_BASE_ADDRESS_MEM_MASK) == (newbartest & PCI_BASE_ADDRESS_MEM_MASK) ? " (original value)" : "");
127                         continue;
128                 }
129         }
130 #endif
131         return NULL; 
132 }
133
134 #if 0
135 /* Traverse_func that starts the BIST (self test) */
136 static void * __init
137 startBIST(struct device_node *dn, void *data)
138 {
139         struct pci_controller *phb = (struct pci_controller *)data;
140         u8 bist;
141
142         char *name = get_property(dn, "name", 0);
143         udbg_printf("startBIST: %s phb=%p, device=%p\n", name ? name : "<unknown>", phb, dn);
144
145         if (ppc_md.pcibios_read_config_byte(dn, PCI_BIST, &bist) == PCIBIOS_SUCCESSFUL) {
146                 if (bist & PCI_BIST_CAPABLE) {
147                         udbg_printf("  -> is BIST capable!\n", phb, dn);
148                         /* Start bist here */
149                 }
150         }
151         return NULL;
152 }
153 #endif
154
155
156 /******************************************************************
157  * Traverse a device tree stopping each PCI device in the tree.
158  * This is done depth first.  As each node is processed, a "pre"
159  * function is called, the children are processed recursively, and
160  * then a "post" function is called.
161  *
162  * The "pre" and "post" funcs return a value.  If non-zero
163  * is returned from the "pre" func, the traversal stops and this
164  * value is returned.  The return value from "post" is not used.
165  * This return value is useful when using traverse as
166  * a method of finding a device.
167  *
168  * NOTE: we do not run the funcs for devices that do not appear to
169  * be PCI except for the start node which we assume (this is good
170  * because the start node is often a phb which may be missing PCI
171  * properties).
172  * We use the class-code as an indicator. If we run into
173  * one of these nodes we also assume its siblings are non-pci for
174  * performance.
175  *
176  ******************************************************************/
177 void *traverse_pci_devices(struct device_node *start, traverse_func pre, traverse_func post, void *data)
178 {
179         struct device_node *dn, *nextdn;
180         void *ret;
181
182         if (pre && (ret = pre(start, data)) != NULL)
183                 return ret;
184         for (dn = start->child; dn; dn = nextdn) {
185                 nextdn = NULL;
186                 if (get_property(dn, "class-code", 0)) {
187                         if (pre && (ret = pre(dn, data)) != NULL)
188                                 return ret;
189                         if (dn->child) {
190                                 /* Depth first...do children */
191                                 nextdn = dn->child;
192                         } else if (dn->sibling) {
193                                 /* ok, try next sibling instead. */
194                                 nextdn = dn->sibling;
195                         } else {
196                                 /* no more children or siblings...call "post" */
197                                 if (post)
198                                         post(dn, data);
199                         }
200                 }
201                 if (!nextdn) {
202                         /* Walk up to next valid sibling. */
203                         do {
204                                 dn = dn->parent;
205                                 if (dn == start)
206                                         return NULL;
207                         } while (dn->sibling == NULL);
208                         nextdn = dn->sibling;
209                 }
210         }
211         return NULL;
212 }
213
214 /* Same as traverse_pci_devices except this does it for all phbs.
215  */
216 void *traverse_all_pci_devices(traverse_func pre)
217 {
218         struct pci_controller* phb;
219         void *ret;
220         for (phb=hose_head;phb;phb=phb->next)
221                 if ((ret = traverse_pci_devices((struct device_node *)phb->arch_data, pre, NULL, phb)) != NULL)
222                         return ret;
223         return NULL;
224 }
225
226
227 /* Traversal func that looks for a <busno,devfcn> value.
228  * If found, the device_node is returned (thus terminating the traversal).
229  */
230 static void *
231 is_devfn_node(struct device_node *dn, void *data)
232 {
233         int busno = ((unsigned long)data >> 8) & 0xff;
234         int devfn = ((unsigned long)data) & 0xff;
235         return (devfn == dn->devfn && busno == dn->busno) ? dn : NULL;
236 }
237
238 /* Same as is_devfn_node except ignore the "fn" part of the "devfn".
239  */
240 static void *
241 is_devfn_sub_node(struct device_node *dn, void *data)
242 {
243         int busno = ((unsigned long)data >> 8) & 0xff;
244         int devfn = ((unsigned long)data) & 0xf8;
245         return (devfn == (dn->devfn & 0xf8) && busno == dn->busno) ? dn : NULL;
246 }
247
248 /* Given an existing EADs (pci bridge) device node create a fake one
249  * that will simulate function zero.  Make it a sibling of other_eads.
250  */
251 static struct device_node *
252 create_eads_node(struct device_node *other_eads)
253 {
254         struct device_node *eads = (struct device_node *)kmalloc(sizeof(struct device_node), GFP_KERNEL);
255
256         if (!eads) return NULL; /* huh? */
257         *eads = *other_eads;
258         eads->devfn &= ~7;      /* make it function zero */
259         eads->tce_table = NULL;
260         /*
261          * NOTE: share properties.  We could copy but for now this should
262          * suffice.  The full_name is also incorrect...but seems harmless.
263          */
264         eads->child = NULL;
265         eads->next = NULL;
266         other_eads->allnext = eads;
267         other_eads->sibling = eads;
268         return eads;
269 }
270
271 /* This is the "slow" path for looking up a device_node from a
272  * pci_dev.  It will hunt for the device under it's parent's
273  * phb and then update sysdata for a future fastpath.
274  *
275  * It may also do fixups on the actual device since this happens
276  * on the first read/write.
277  *
278  * Note that it also must deal with devices that don't exist.
279  * In this case it may probe for real hardware ("just in case")
280  * and add a device_node to the device tree if necessary.
281  *
282  */
283 struct device_node *fetch_dev_dn(struct pci_dev *dev)
284 {
285         struct device_node *orig_dn = (struct device_node *)dev->sysdata;
286         struct pci_controller *phb = orig_dn->phb; /* assume same phb as orig_dn */
287         struct device_node *phb_dn;
288         struct device_node *dn;
289         unsigned long searchval = (dev->bus->number << 8) | dev->devfn;
290
291         phb_dn = (struct device_node *)(phb->arch_data);
292         dn = (struct device_node *)traverse_pci_devices(phb_dn, is_devfn_node, NULL, (void *)searchval);
293         if (dn) {
294                 dev->sysdata = dn;
295                 /* ToDo: call some device init hook here */
296         } else {
297                 /* Now it is very possible that we can't find the device
298                  * because it is not the zero'th device of a mutifunction
299                  * device and we don't have permission to read the zero'th
300                  * device.  If this is the case, Linux would ordinarily skip
301                  * all the other functions.
302                  */
303                 if ((searchval & 0x7) == 0) {
304                         struct device_node *thisdevdn;
305                         /* Ok, we are looking for fn == 0.  Let's check for other functions. */
306                         thisdevdn = (struct device_node *)traverse_pci_devices(phb_dn, is_devfn_sub_node, NULL, (void *)searchval);
307                         if (thisdevdn) {
308                                 /* Ah ha!  There does exist a sub function.
309                                  * Now this isn't an exact match for
310                                  * searchval, but in order to get Linux to
311                                  * believe the sub functions exist we will
312                                  * need to manufacture a fake device_node for
313                                  * this zero'th function.  To keept this
314                                  * simple for now we only handle pci bridges
315                                  * and we just hand back the found node which
316                                  * isn't correct, but Linux won't care.
317                                  */
318                                 char *device_type = (char *)get_property(thisdevdn, "device_type", 0);
319                                 if (device_type && strcmp(device_type, "pci") == 0) {
320                                         return create_eads_node(thisdevdn);
321                                 }
322                         }
323                 }
324                 /* ToDo: device not found...probe for it anyway with a fake dn?
325                 struct device_node fake_dn;
326                 memset(&fake_dn, 0, sizeof(fake_dn));
327                 fake_dn.phb = phb;
328                 fake_dn.busno = dev->bus->number;
329                 fake_dn.devfn = dev->devfn;
330                 ... now do ppc_md.pcibios_read_config_dword(&fake_dn.....)
331                  ... if ok, alloc a real device_node and dn = real_dn;
332                  */
333         }
334         return dn;
335 }
336
337
338 /******************************************************************
339  * Actually initialize the phbs.
340  * The buswalk on this phb has not happened yet.
341  ******************************************************************/
342 void __init
343 pci_devs_phb_init(void)
344 {
345         /* This must be done first so the device nodes have valid pci info! */
346         traverse_all_pci_devices(update_dn_pci_info);
347
348         /* Hack for regatta which does not init the bars correctly */
349         traverse_all_pci_devices(write_OF_bars);
350 #if 0
351         traverse_all_pci_devices(startBIST);
352         mdelay(5000);
353         traverse_all_pci_devices(checkBIST);
354 #endif
355 }
356
357
358 static void __init
359 pci_fixup_bus_sysdata_list(struct list_head *bus_list)
360 {
361         struct list_head *ln;
362         struct pci_bus *bus;
363         struct pci_controller *phb;
364         int newnum;
365
366         for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
367                 bus = pci_bus_b(ln);
368                 if (bus->self) {
369                         bus->sysdata = bus->self->sysdata;
370                         /* Also fixup the bus number on large bus systems to
371                          * include the PHB# in the next byte
372                          */
373                         phb = PCI_GET_DN(bus)->phb;
374                         if (phb && phb->buid) {
375                                 newnum = (phb->global_number << 8) | bus->number;
376                                 bus->number = newnum;
377                                 sprintf(bus->name, "PCI Bus #%x", bus->number);
378                         }
379                 }
380                 pci_fixup_bus_sysdata_list(&bus->children);
381         }
382 }
383
384 /******************************************************************
385  * Fixup the bus->sysdata ptrs to point to the bus' device_node.
386  * This is done late in pcibios_init().  We do this mostly for
387  * sanity, but pci_dma.c uses these at DMA time so they must be
388  * correct.
389  * To do this we recurse down the bus hierarchy.  Note that PHB's
390  * have bus->self == NULL, but fortunately bus->sysdata is already
391  * correct in this case.
392  ******************************************************************/
393 void __init
394 pci_fix_bus_sysdata(void)
395 {
396         pci_fixup_bus_sysdata_list(&pci_root_buses);
397 }