powerpc, ftrace: use create_branch lib function
[linux-2.6] / arch / powerpc / kernel / pci_32.c
1 /*
2  * Common pmac/prep/chrp pci routines. -- Cort
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/pci.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/bootmem.h>
14 #include <linux/irq.h>
15 #include <linux/list.h>
16 #include <linux/of.h>
17
18 #include <asm/processor.h>
19 #include <asm/io.h>
20 #include <asm/prom.h>
21 #include <asm/sections.h>
22 #include <asm/pci-bridge.h>
23 #include <asm/ppc-pci.h>
24 #include <asm/byteorder.h>
25 #include <asm/uaccess.h>
26 #include <asm/machdep.h>
27
28 #undef DEBUG
29
30 unsigned long isa_io_base     = 0;
31 unsigned long pci_dram_offset = 0;
32 int pcibios_assign_bus_offset = 1;
33
34 void pcibios_make_OF_bus_map(void);
35
36 static void fixup_broken_pcnet32(struct pci_dev* dev);
37 static void fixup_cpc710_pci64(struct pci_dev* dev);
38 #ifdef CONFIG_PPC_OF
39 static u8* pci_to_OF_bus_map;
40 #endif
41
42 /* By default, we don't re-assign bus numbers. We do this only on
43  * some pmacs
44  */
45 static int pci_assign_all_buses;
46
47 static int pci_bus_count;
48
49 /* This will remain NULL for now, until isa-bridge.c is made common
50  * to both 32-bit and 64-bit.
51  */
52 struct pci_dev *isa_bridge_pcidev;
53 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
54
55 static void
56 fixup_hide_host_resource_fsl(struct pci_dev *dev)
57 {
58         int i, class = dev->class >> 8;
59
60         if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
61              class == PCI_CLASS_BRIDGE_OTHER) &&
62                 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
63                 (dev->bus->parent == NULL)) {
64                 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
65                         dev->resource[i].start = 0;
66                         dev->resource[i].end = 0;
67                         dev->resource[i].flags = 0;
68                 }
69         }
70 }
71 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); 
72 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); 
73
74 static void
75 fixup_broken_pcnet32(struct pci_dev* dev)
76 {
77         if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
78                 dev->vendor = PCI_VENDOR_ID_AMD;
79                 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
80         }
81 }
82 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID,                     fixup_broken_pcnet32);
83
84 static void
85 fixup_cpc710_pci64(struct pci_dev* dev)
86 {
87         /* Hide the PCI64 BARs from the kernel as their content doesn't
88          * fit well in the resource management
89          */
90         dev->resource[0].start = dev->resource[0].end = 0;
91         dev->resource[0].flags = 0;
92         dev->resource[1].start = dev->resource[1].end = 0;
93         dev->resource[1].flags = 0;
94 }
95 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,     PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
96
97 #ifdef CONFIG_PPC_OF
98 /*
99  * Functions below are used on OpenFirmware machines.
100  */
101 static void
102 make_one_node_map(struct device_node* node, u8 pci_bus)
103 {
104         const int *bus_range;
105         int len;
106
107         if (pci_bus >= pci_bus_count)
108                 return;
109         bus_range = of_get_property(node, "bus-range", &len);
110         if (bus_range == NULL || len < 2 * sizeof(int)) {
111                 printk(KERN_WARNING "Can't get bus-range for %s, "
112                        "assuming it starts at 0\n", node->full_name);
113                 pci_to_OF_bus_map[pci_bus] = 0;
114         } else
115                 pci_to_OF_bus_map[pci_bus] = bus_range[0];
116
117         for_each_child_of_node(node, node) {
118                 struct pci_dev* dev;
119                 const unsigned int *class_code, *reg;
120         
121                 class_code = of_get_property(node, "class-code", NULL);
122                 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
123                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
124                         continue;
125                 reg = of_get_property(node, "reg", NULL);
126                 if (!reg)
127                         continue;
128                 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
129                 if (!dev || !dev->subordinate) {
130                         pci_dev_put(dev);
131                         continue;
132                 }
133                 make_one_node_map(node, dev->subordinate->number);
134                 pci_dev_put(dev);
135         }
136 }
137         
138 void
139 pcibios_make_OF_bus_map(void)
140 {
141         int i;
142         struct pci_controller *hose, *tmp;
143         struct property *map_prop;
144         struct device_node *dn;
145
146         pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
147         if (!pci_to_OF_bus_map) {
148                 printk(KERN_ERR "Can't allocate OF bus map !\n");
149                 return;
150         }
151
152         /* We fill the bus map with invalid values, that helps
153          * debugging.
154          */
155         for (i=0; i<pci_bus_count; i++)
156                 pci_to_OF_bus_map[i] = 0xff;
157
158         /* For each hose, we begin searching bridges */
159         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
160                 struct device_node* node = hose->dn;
161
162                 if (!node)
163                         continue;
164                 make_one_node_map(node, hose->first_busno);
165         }
166         dn = of_find_node_by_path("/");
167         map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
168         if (map_prop) {
169                 BUG_ON(pci_bus_count > map_prop->length);
170                 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
171         }
172         of_node_put(dn);
173 #ifdef DEBUG
174         printk("PCI->OF bus map:\n");
175         for (i=0; i<pci_bus_count; i++) {
176                 if (pci_to_OF_bus_map[i] == 0xff)
177                         continue;
178                 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
179         }
180 #endif
181 }
182
183 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
184
185 static struct device_node*
186 scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
187 {
188         struct device_node *node;
189         struct device_node* sub_node;
190
191         for_each_child_of_node(parent, node) {
192                 const unsigned int *class_code;
193         
194                 if (filter(node, data)) {
195                         of_node_put(node);
196                         return node;
197                 }
198
199                 /* For PCI<->PCI bridges or CardBus bridges, we go down
200                  * Note: some OFs create a parent node "multifunc-device" as
201                  * a fake root for all functions of a multi-function device,
202                  * we go down them as well.
203                  */
204                 class_code = of_get_property(node, "class-code", NULL);
205                 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
206                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
207                         strcmp(node->name, "multifunc-device"))
208                         continue;
209                 sub_node = scan_OF_pci_childs(node, filter, data);
210                 if (sub_node) {
211                         of_node_put(node);
212                         return sub_node;
213                 }
214         }
215         return NULL;
216 }
217
218 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
219                                                unsigned int devfn)
220 {
221         struct device_node *np;
222         const u32 *reg;
223         unsigned int psize;
224
225         for_each_child_of_node(parent, np) {
226                 reg = of_get_property(np, "reg", &psize);
227                 if (reg == NULL || psize < 4)
228                         continue;
229                 if (((reg[0] >> 8) & 0xff) == devfn)
230                         return np;
231         }
232         return NULL;
233 }
234
235
236 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
237 {
238         struct device_node *parent, *np;
239
240         /* Are we a root bus ? */
241         if (bus->self == NULL || bus->parent == NULL) {
242                 struct pci_controller *hose = pci_bus_to_host(bus);
243                 if (hose == NULL)
244                         return NULL;
245                 return of_node_get(hose->dn);
246         }
247
248         /* not a root bus, we need to get our parent */
249         parent = scan_OF_for_pci_bus(bus->parent);
250         if (parent == NULL)
251                 return NULL;
252
253         /* now iterate for children for a match */
254         np = scan_OF_for_pci_dev(parent, bus->self->devfn);
255         of_node_put(parent);
256
257         return np;
258 }
259
260 /*
261  * Scans the OF tree for a device node matching a PCI device
262  */
263 struct device_node *
264 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
265 {
266         struct device_node *parent, *np;
267
268         pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
269         parent = scan_OF_for_pci_bus(bus);
270         if (parent == NULL)
271                 return NULL;
272         pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
273         np = scan_OF_for_pci_dev(parent, devfn);
274         of_node_put(parent);
275         pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
276
277         /* XXX most callers don't release the returned node
278          * mostly because ppc64 doesn't increase the refcount,
279          * we need to fix that.
280          */
281         return np;
282 }
283 EXPORT_SYMBOL(pci_busdev_to_OF_node);
284
285 struct device_node*
286 pci_device_to_OF_node(struct pci_dev *dev)
287 {
288         return pci_busdev_to_OF_node(dev->bus, dev->devfn);
289 }
290 EXPORT_SYMBOL(pci_device_to_OF_node);
291
292 static int
293 find_OF_pci_device_filter(struct device_node* node, void* data)
294 {
295         return ((void *)node == data);
296 }
297
298 /*
299  * Returns the PCI device matching a given OF node
300  */
301 int
302 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
303 {
304         const unsigned int *reg;
305         struct pci_controller* hose;
306         struct pci_dev* dev = NULL;
307         
308         /* Make sure it's really a PCI device */
309         hose = pci_find_hose_for_OF_device(node);
310         if (!hose || !hose->dn)
311                 return -ENODEV;
312         if (!scan_OF_pci_childs(hose->dn,
313                         find_OF_pci_device_filter, (void *)node))
314                 return -ENODEV;
315         reg = of_get_property(node, "reg", NULL);
316         if (!reg)
317                 return -ENODEV;
318         *bus = (reg[0] >> 16) & 0xff;
319         *devfn = ((reg[0] >> 8) & 0xff);
320
321         /* Ok, here we need some tweak. If we have already renumbered
322          * all busses, we can't rely on the OF bus number any more.
323          * the pci_to_OF_bus_map is not enough as several PCI busses
324          * may match the same OF bus number.
325          */
326         if (!pci_to_OF_bus_map)
327                 return 0;
328
329         for_each_pci_dev(dev)
330                 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
331                                 dev->devfn == *devfn) {
332                         *bus = dev->bus->number;
333                         pci_dev_put(dev);
334                         return 0;
335                 }
336
337         return -ENODEV;
338 }
339 EXPORT_SYMBOL(pci_device_from_OF_node);
340
341 /* We create the "pci-OF-bus-map" property now so it appears in the
342  * /proc device tree
343  */
344 void __init
345 pci_create_OF_bus_map(void)
346 {
347         struct property* of_prop;
348         struct device_node *dn;
349
350         of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
351         if (!of_prop)
352                 return;
353         dn = of_find_node_by_path("/");
354         if (dn) {
355                 memset(of_prop, -1, sizeof(struct property) + 256);
356                 of_prop->name = "pci-OF-bus-map";
357                 of_prop->length = 256;
358                 of_prop->value = &of_prop[1];
359                 prom_add_property(dn, of_prop);
360                 of_node_put(dn);
361         }
362 }
363
364 #else /* CONFIG_PPC_OF */
365 void pcibios_make_OF_bus_map(void)
366 {
367 }
368 #endif /* CONFIG_PPC_OF */
369
370 static void __devinit pcibios_scan_phb(struct pci_controller *hose)
371 {
372         struct pci_bus *bus;
373         struct device_node *node = hose->dn;
374         unsigned long io_offset;
375         struct resource *res = &hose->io_resource;
376
377         pr_debug("PCI: Scanning PHB %s\n",
378                  node ? node->full_name : "<NO NAME>");
379
380         /* Create an empty bus for the toplevel */
381         bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
382         if (bus == NULL) {
383                 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
384                        hose->global_number);
385                 return;
386         }
387         bus->secondary = hose->first_busno;
388         hose->bus = bus;
389
390         /* Fixup IO space offset */
391         io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
392         res->start = (res->start + io_offset) & 0xffffffffu;
393         res->end = (res->end + io_offset) & 0xffffffffu;
394
395         /* Wire up PHB bus resources */
396         pcibios_setup_phb_resources(hose);
397
398         /* Scan children */
399         hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
400 }
401
402 static int __init pcibios_init(void)
403 {
404         struct pci_controller *hose, *tmp;
405         int next_busno = 0;
406
407         printk(KERN_INFO "PCI: Probing PCI hardware\n");
408
409         if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
410                 pci_assign_all_buses = 1;
411
412         /* Scan all of the recorded PCI controllers.  */
413         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
414                 if (pci_assign_all_buses)
415                         hose->first_busno = next_busno;
416                 hose->last_busno = 0xff;
417                 pcibios_scan_phb(hose);
418                 pci_bus_add_devices(hose->bus);
419                 if (pci_assign_all_buses || next_busno <= hose->last_busno)
420                         next_busno = hose->last_busno + pcibios_assign_bus_offset;
421         }
422         pci_bus_count = next_busno;
423
424         /* OpenFirmware based machines need a map of OF bus
425          * numbers vs. kernel bus numbers since we may have to
426          * remap them.
427          */
428         if (pci_assign_all_buses)
429                 pcibios_make_OF_bus_map();
430
431         /* Call common code to handle resource allocation */
432         pcibios_resource_survey();
433
434         /* Call machine dependent post-init code */
435         if (ppc_md.pcibios_after_init)
436                 ppc_md.pcibios_after_init();
437
438         return 0;
439 }
440
441 subsys_initcall(pcibios_init);
442
443 /* the next one is stolen from the alpha port... */
444 void __init
445 pcibios_update_irq(struct pci_dev *dev, int irq)
446 {
447         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
448         /* XXX FIXME - update OF device tree node interrupt property */
449 }
450
451 static struct pci_controller*
452 pci_bus_to_hose(int bus)
453 {
454         struct pci_controller *hose, *tmp;
455
456         list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
457                 if (bus >= hose->first_busno && bus <= hose->last_busno)
458                         return hose;
459         return NULL;
460 }
461
462 /* Provide information on locations of various I/O regions in physical
463  * memory.  Do this on a per-card basis so that we choose the right
464  * root bridge.
465  * Note that the returned IO or memory base is a physical address
466  */
467
468 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
469 {
470         struct pci_controller* hose;
471         long result = -EOPNOTSUPP;
472
473         hose = pci_bus_to_hose(bus);
474         if (!hose)
475                 return -ENODEV;
476
477         switch (which) {
478         case IOBASE_BRIDGE_NUMBER:
479                 return (long)hose->first_busno;
480         case IOBASE_MEMORY:
481                 return (long)hose->pci_mem_offset;
482         case IOBASE_IO:
483                 return (long)hose->io_base_phys;
484         case IOBASE_ISA_IO:
485                 return (long)isa_io_base;
486         case IOBASE_ISA_MEM:
487                 return (long)isa_mem_base;
488         }
489
490         return result;
491 }
492
493 /*
494  * Null PCI config access functions, for the case when we can't
495  * find a hose.
496  */
497 #define NULL_PCI_OP(rw, size, type)                                     \
498 static int                                                              \
499 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)    \
500 {                                                                       \
501         return PCIBIOS_DEVICE_NOT_FOUND;                                \
502 }
503
504 static int
505 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
506                  int len, u32 *val)
507 {
508         return PCIBIOS_DEVICE_NOT_FOUND;
509 }
510
511 static int
512 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
513                   int len, u32 val)
514 {
515         return PCIBIOS_DEVICE_NOT_FOUND;
516 }
517
518 static struct pci_ops null_pci_ops =
519 {
520         .read = null_read_config,
521         .write = null_write_config,
522 };
523
524 /*
525  * These functions are used early on before PCI scanning is done
526  * and all of the pci_dev and pci_bus structures have been created.
527  */
528 static struct pci_bus *
529 fake_pci_bus(struct pci_controller *hose, int busnr)
530 {
531         static struct pci_bus bus;
532
533         if (hose == 0) {
534                 hose = pci_bus_to_hose(busnr);
535                 if (hose == 0)
536                         printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
537         }
538         bus.number = busnr;
539         bus.sysdata = hose;
540         bus.ops = hose? hose->ops: &null_pci_ops;
541         return &bus;
542 }
543
544 #define EARLY_PCI_OP(rw, size, type)                                    \
545 int early_##rw##_config_##size(struct pci_controller *hose, int bus,    \
546                                int devfn, int offset, type value)       \
547 {                                                                       \
548         return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),    \
549                                             devfn, offset, value);      \
550 }
551
552 EARLY_PCI_OP(read, byte, u8 *)
553 EARLY_PCI_OP(read, word, u16 *)
554 EARLY_PCI_OP(read, dword, u32 *)
555 EARLY_PCI_OP(write, byte, u8)
556 EARLY_PCI_OP(write, word, u16)
557 EARLY_PCI_OP(write, dword, u32)
558
559 extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
560 int early_find_capability(struct pci_controller *hose, int bus, int devfn,
561                           int cap)
562 {
563         return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
564 }