Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[pandora-kernel.git] / arch / sparc64 / kernel / pci_common.c
1 /* $Id: pci_common.c,v 1.29 2002/02/01 00:56:03 davem Exp $
2  * pci_common.c: PCI controller common support.
3  *
4  * Copyright (C) 1999 David S. Miller (davem@redhat.com)
5  */
6
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10
11 #include <asm/pbm.h>
12 #include <asm/prom.h>
13
14 #include "pci_impl.h"
15
16 /* Pass "pci=irq_verbose" on the kernel command line to enable this.  */
17 int pci_irq_verbose;
18
19 /* Fix self device of BUS and hook it into BUS->self.
20  * The pci_scan_bus does not do this for the host bridge.
21  */
22 void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
23 {
24         struct pci_dev *pdev;
25
26         list_for_each_entry(pdev, &pbus->devices, bus_list) {
27                 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
28                         pbus->self = pdev;
29                         return;
30                 }
31         }
32
33         prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
34         prom_halt();
35 }
36
37 /* Find the OBP PROM device tree node for a PCI device.  */
38 static struct device_node * __init
39 find_device_prom_node(struct pci_pbm_info *pbm, struct pci_dev *pdev,
40                       struct device_node *bus_node,
41                       struct linux_prom_pci_registers **pregs,
42                       int *nregs)
43 {
44         struct device_node *dp;
45
46         *nregs = 0;
47
48         /*
49          * Return the PBM's PROM node in case we are it's PCI device,
50          * as the PBM's reg property is different to standard PCI reg
51          * properties. We would delete this device entry otherwise,
52          * which confuses XFree86's device probing...
53          */
54         if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
55             (pdev->vendor == PCI_VENDOR_ID_SUN) &&
56             (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
57              pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
58              pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
59              pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
60              pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
61                 return bus_node;
62
63         dp = bus_node->child;
64         while (dp) {
65                 struct linux_prom_pci_registers *regs;
66                 struct property *prop;
67                 int len;
68
69                 prop = of_find_property(dp, "reg", &len);
70                 if (!prop)
71                         goto do_next_sibling;
72
73                 regs = prop->value;
74                 if (((regs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
75                         *pregs = regs;
76                         *nregs = len / sizeof(struct linux_prom_pci_registers);
77                         return dp;
78                 }
79
80         do_next_sibling:
81                 dp = dp->sibling;
82         }
83
84         return NULL;
85 }
86
87 /* Older versions of OBP on PCI systems encode 64-bit MEM
88  * space assignments incorrectly, this fixes them up.  We also
89  * take the opportunity here to hide other kinds of bogus
90  * assignments.
91  */
92 static void __init fixup_obp_assignments(struct pci_dev *pdev,
93                                          struct pcidev_cookie *pcp)
94 {
95         int i;
96
97         if (pdev->vendor == PCI_VENDOR_ID_AL &&
98             (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
99              pdev->device == PCI_DEVICE_ID_AL_M1533)) {
100                 int i;
101
102                 /* Zap all of the normal resources, they are
103                  * meaningless and generate bogus resource collision
104                  * messages.  This is OpenBoot's ill-fated attempt to
105                  * represent the implicit resources that these devices
106                  * have.
107                  */
108                 pcp->num_prom_assignments = 0;
109                 for (i = 0; i < 6; i++) {
110                         pdev->resource[i].start =
111                                 pdev->resource[i].end =
112                                 pdev->resource[i].flags = 0;
113                 }
114                 pdev->resource[PCI_ROM_RESOURCE].start =
115                         pdev->resource[PCI_ROM_RESOURCE].end =
116                         pdev->resource[PCI_ROM_RESOURCE].flags = 0;
117                 return;
118         }
119
120         for (i = 0; i < pcp->num_prom_assignments; i++) {
121                 struct linux_prom_pci_registers *ap;
122                 int space;
123
124                 ap = &pcp->prom_assignments[i];
125                 space = ap->phys_hi >> 24;
126                 if ((space & 0x3) == 2 &&
127                     (space & 0x4) != 0) {
128                         ap->phys_hi &= ~(0x7 << 24);
129                         ap->phys_hi |= 0x3 << 24;
130                 }
131         }
132 }
133
134 /* Fill in the PCI device cookie sysdata for the given
135  * PCI device.  This cookie is the means by which one
136  * can get to OBP and PCI controller specific information
137  * for a PCI device.
138  */
139 static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
140                                       struct pci_dev *pdev,
141                                       struct device_node *bus_node)
142 {
143         struct linux_prom_pci_registers *pregs = NULL;
144         struct pcidev_cookie *pcp;
145         struct device_node *dp;
146         struct property *prop;
147         int nregs, len;
148
149         dp = find_device_prom_node(pbm, pdev, bus_node,
150                                    &pregs, &nregs);
151         if (!dp) {
152                 /* If it is not in the OBP device tree then
153                  * there must be a damn good reason for it.
154                  *
155                  * So what we do is delete the device from the
156                  * PCI device tree completely.  This scenario
157                  * is seen, for example, on CP1500 for the
158                  * second EBUS/HappyMeal pair if the external
159                  * connector for it is not present.
160                  */
161                 pci_remove_bus_device(pdev);
162                 return;
163         }
164
165         pcp = kzalloc(sizeof(*pcp), GFP_ATOMIC);
166         if (pcp == NULL) {
167                 prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
168                 prom_halt();
169         }
170         pcp->pbm = pbm;
171         pcp->prom_node = dp;
172         memcpy(pcp->prom_regs, pregs,
173                nregs * sizeof(struct linux_prom_pci_registers));
174         pcp->num_prom_regs = nregs;
175
176         /* We can't have the pcidev_cookie assignments be just
177          * direct pointers into the property value, since they
178          * are potentially modified by the probing process.
179          */
180         prop = of_find_property(dp, "assigned-addresses", &len);
181         if (!prop) {
182                 pcp->num_prom_assignments = 0;
183         } else {
184                 memcpy(pcp->prom_assignments, prop->value, len);
185                 pcp->num_prom_assignments =
186                         (len / sizeof(pcp->prom_assignments[0]));
187         }
188
189         if (strcmp(dp->name, "ebus") == 0) {
190                 struct linux_prom_ebus_ranges *erng;
191                 int iter;
192
193                 /* EBUS is special... */
194                 prop = of_find_property(dp, "ranges", &len);
195                 if (!prop) {
196                         prom_printf("EBUS: Fatal error, no range property\n");
197                         prom_halt();
198                 }
199                 erng = prop->value;
200                 len = (len / sizeof(erng[0]));
201                 for (iter = 0; iter < len; iter++) {
202                         struct linux_prom_ebus_ranges *ep = &erng[iter];
203                         struct linux_prom_pci_registers *ap;
204
205                         ap = &pcp->prom_assignments[iter];
206
207                         ap->phys_hi = ep->parent_phys_hi;
208                         ap->phys_mid = ep->parent_phys_mid;
209                         ap->phys_lo = ep->parent_phys_lo;
210                         ap->size_hi = 0;
211                         ap->size_lo = ep->size;
212                 }
213                 pcp->num_prom_assignments = len;
214         }
215
216         fixup_obp_assignments(pdev, pcp);
217
218         pdev->sysdata = pcp;
219 }
220
221 void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
222                                     struct pci_pbm_info *pbm,
223                                     struct device_node *dp)
224 {
225         struct pci_dev *pdev, *pdev_next;
226         struct pci_bus *this_pbus, *pbus_next;
227
228         /* This must be _safe because the cookie fillin
229            routine can delete devices from the tree.  */
230         list_for_each_entry_safe(pdev, pdev_next, &pbus->devices, bus_list)
231                 pdev_cookie_fillin(pbm, pdev, dp);
232
233         list_for_each_entry_safe(this_pbus, pbus_next, &pbus->children, node) {
234                 struct pcidev_cookie *pcp = this_pbus->self->sysdata;
235
236                 pci_fill_in_pbm_cookies(this_pbus, pbm, pcp->prom_node);
237         }
238 }
239
240 static void __init bad_assignment(struct pci_dev *pdev,
241                                   struct linux_prom_pci_registers *ap,
242                                   struct resource *res,
243                                   int do_prom_halt)
244 {
245         prom_printf("PCI: Bogus PROM assignment. BUS[%02x] DEVFN[%x]\n",
246                     pdev->bus->number, pdev->devfn);
247         if (ap)
248                 prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
249                             ap->phys_hi, ap->phys_mid, ap->phys_lo,
250                             ap->size_hi, ap->size_lo);
251         if (res)
252                 prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
253                             res->start, res->end, res->flags);
254         if (do_prom_halt)
255                 prom_halt();
256 }
257
258 static struct resource *
259 __init get_root_resource(struct linux_prom_pci_registers *ap,
260                          struct pci_pbm_info *pbm)
261 {
262         int space = (ap->phys_hi >> 24) & 3;
263
264         switch (space) {
265         case 0:
266                 /* Configuration space, silently ignore it. */
267                 return NULL;
268
269         case 1:
270                 /* 16-bit IO space */
271                 return &pbm->io_space;
272
273         case 2:
274                 /* 32-bit MEM space */
275                 return &pbm->mem_space;
276
277         case 3:
278                 /* 64-bit MEM space, these are allocated out of
279                  * the 32-bit mem_space range for the PBM, ie.
280                  * we just zero out the upper 32-bits.
281                  */
282                 return &pbm->mem_space;
283
284         default:
285                 printk("PCI: What is resource space %x?\n", space);
286                 return NULL;
287         };
288 }
289
290 static struct resource *
291 __init get_device_resource(struct linux_prom_pci_registers *ap,
292                            struct pci_dev *pdev)
293 {
294         struct resource *res;
295         int breg = (ap->phys_hi & 0xff);
296
297         switch (breg) {
298         case  PCI_ROM_ADDRESS:
299                 /* Unfortunately I have seen several cases where
300                  * buggy FCODE uses a space value of '1' (I/O space)
301                  * in the register property for the ROM address
302                  * so disable this sanity check for now.
303                  */
304 #if 0
305         {
306                 int space = (ap->phys_hi >> 24) & 3;
307
308                 /* It had better be MEM space. */
309                 if (space != 2)
310                         bad_assignment(pdev, ap, NULL, 0);
311         }
312 #endif
313                 res = &pdev->resource[PCI_ROM_RESOURCE];
314                 break;
315
316         case PCI_BASE_ADDRESS_0:
317         case PCI_BASE_ADDRESS_1:
318         case PCI_BASE_ADDRESS_2:
319         case PCI_BASE_ADDRESS_3:
320         case PCI_BASE_ADDRESS_4:
321         case PCI_BASE_ADDRESS_5:
322                 res = &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
323                 break;
324
325         default:
326                 bad_assignment(pdev, ap, NULL, 0);
327                 res = NULL;
328                 break;
329         };
330
331         return res;
332 }
333
334 static int __init pdev_resource_collisions_expected(struct pci_dev *pdev)
335 {
336         if (pdev->vendor != PCI_VENDOR_ID_SUN)
337                 return 0;
338
339         if (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS ||
340             pdev->device == PCI_DEVICE_ID_SUN_RIO_1394 ||
341             pdev->device == PCI_DEVICE_ID_SUN_RIO_USB)
342                 return 1;
343
344         return 0;
345 }
346
347 static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
348                                            struct pci_dev *pdev)
349 {
350         struct pcidev_cookie *pcp = pdev->sysdata;
351         int i;
352
353         for (i = 0; i < pcp->num_prom_assignments; i++) {
354                 struct linux_prom_pci_registers *ap;
355                 struct resource *root, *res;
356
357                 /* The format of this property is specified in
358                  * the PCI Bus Binding to IEEE1275-1994.
359                  */
360                 ap = &pcp->prom_assignments[i];
361                 root = get_root_resource(ap, pbm);
362                 res = get_device_resource(ap, pdev);
363                 if (root == NULL || res == NULL ||
364                     res->flags == 0)
365                         continue;
366
367                 /* Ok we know which resource this PROM assignment is
368                  * for, sanity check it.
369                  */
370                 if ((res->start & 0xffffffffUL) != ap->phys_lo)
371                         bad_assignment(pdev, ap, res, 1);
372
373                 /* If it is a 64-bit MEM space assignment, verify that
374                  * the resource is too and that the upper 32-bits match.
375                  */
376                 if (((ap->phys_hi >> 24) & 3) == 3) {
377                         if (((res->flags & IORESOURCE_MEM) == 0) ||
378                             ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
379                              != PCI_BASE_ADDRESS_MEM_TYPE_64))
380                                 bad_assignment(pdev, ap, res, 1);
381                         if ((res->start >> 32) != ap->phys_mid)
382                                 bad_assignment(pdev, ap, res, 1);
383
384                         /* PBM cannot generate cpu initiated PIOs
385                          * to the full 64-bit space.  Therefore the
386                          * upper 32-bits better be zero.  If it is
387                          * not, just skip it and we will assign it
388                          * properly ourselves.
389                          */
390                         if ((res->start >> 32) != 0UL) {
391                                 printk(KERN_ERR "PCI: OBP assigns out of range MEM address "
392                                        "%016lx for region %ld on device %s\n",
393                                        res->start, (res - &pdev->resource[0]), pci_name(pdev));
394                                 continue;
395                         }
396                 }
397
398                 /* Adjust the resource into the physical address space
399                  * of this PBM.
400                  */
401                 pbm->parent->resource_adjust(pdev, res, root);
402
403                 if (request_resource(root, res) < 0) {
404                         /* OK, there is some conflict.  But this is fine
405                          * since we'll reassign it in the fixup pass.
406                          *
407                          * We notify the user that OBP made an error if it
408                          * is a case we don't expect.
409                          */
410                         if (!pdev_resource_collisions_expected(pdev)) {
411                                 printk(KERN_ERR "PCI: Address space collision on region %ld "
412                                        "[%016lx:%016lx] of device %s\n",
413                                        (res - &pdev->resource[0]),
414                                        res->start, res->end,
415                                        pci_name(pdev));
416                         }
417                 }
418         }
419 }
420
421 void __init pci_record_assignments(struct pci_pbm_info *pbm,
422                                    struct pci_bus *pbus)
423 {
424         struct pci_dev *dev;
425         struct pci_bus *bus;
426
427         list_for_each_entry(dev, &pbus->devices, bus_list)
428                 pdev_record_assignments(pbm, dev);
429
430         list_for_each_entry(bus, &pbus->children, node)
431                 pci_record_assignments(pbm, bus);
432 }
433
434 /* Return non-zero if PDEV has implicit I/O resources even
435  * though it may not have an I/O base address register
436  * active.
437  */
438 static int __init has_implicit_io(struct pci_dev *pdev)
439 {
440         int class = pdev->class >> 8;
441
442         if (class == PCI_CLASS_NOT_DEFINED ||
443             class == PCI_CLASS_NOT_DEFINED_VGA ||
444             class == PCI_CLASS_STORAGE_IDE ||
445             (pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
446                 return 1;
447
448         return 0;
449 }
450
451 static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
452                                           struct pci_dev *pdev)
453 {
454         u32 reg;
455         u16 cmd;
456         int i, io_seen, mem_seen;
457
458         io_seen = mem_seen = 0;
459         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
460                 struct resource *root, *res;
461                 unsigned long size, min, max, align;
462
463                 res = &pdev->resource[i];
464
465                 if (res->flags & IORESOURCE_IO)
466                         io_seen++;
467                 else if (res->flags & IORESOURCE_MEM)
468                         mem_seen++;
469
470                 /* If it is already assigned or the resource does
471                  * not exist, there is nothing to do.
472                  */
473                 if (res->parent != NULL || res->flags == 0UL)
474                         continue;
475
476                 /* Determine the root we allocate from. */
477                 if (res->flags & IORESOURCE_IO) {
478                         root = &pbm->io_space;
479                         min = root->start + 0x400UL;
480                         max = root->end;
481                 } else {
482                         root = &pbm->mem_space;
483                         min = root->start;
484                         max = min + 0x80000000UL;
485                 }
486
487                 size = res->end - res->start;
488                 align = size + 1;
489                 if (allocate_resource(root, res, size + 1, min, max, align, NULL, NULL) < 0) {
490                         /* uh oh */
491                         prom_printf("PCI: Failed to allocate resource %d for %s\n",
492                                     i, pci_name(pdev));
493                         prom_halt();
494                 }
495
496                 /* Update PCI config space. */
497                 pbm->parent->base_address_update(pdev, i);
498         }
499
500         /* Special case, disable the ROM.  Several devices
501          * act funny (ie. do not respond to memory space writes)
502          * when it is left enabled.  A good example are Qlogic,ISP
503          * adapters.
504          */
505         pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &reg);
506         reg &= ~PCI_ROM_ADDRESS_ENABLE;
507         pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
508
509         /* If we saw I/O or MEM resources, enable appropriate
510          * bits in PCI command register.
511          */
512         if (io_seen || mem_seen) {
513                 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
514                 if (io_seen || has_implicit_io(pdev))
515                         cmd |= PCI_COMMAND_IO;
516                 if (mem_seen)
517                         cmd |= PCI_COMMAND_MEMORY;
518                 pci_write_config_word(pdev, PCI_COMMAND, cmd);
519         }
520
521         /* If this is a PCI bridge or an IDE controller,
522          * enable bus mastering.  In the former case also
523          * set the cache line size correctly.
524          */
525         if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
526             (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
527              ((pdev->class & 0x80) != 0))) {
528                 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
529                 cmd |= PCI_COMMAND_MASTER;
530                 pci_write_config_word(pdev, PCI_COMMAND, cmd);
531
532                 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
533                         pci_write_config_byte(pdev,
534                                               PCI_CACHE_LINE_SIZE,
535                                               (64 / sizeof(u32)));
536         }
537 }
538
539 void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
540                                   struct pci_bus *pbus)
541 {
542         struct pci_dev *dev;
543         struct pci_bus *bus;
544
545         list_for_each_entry(dev, &pbus->devices, bus_list)
546                 pdev_assign_unassigned(pbm, dev);
547
548         list_for_each_entry(bus, &pbus->children, node)
549                 pci_assign_unassigned(pbm, bus);
550 }
551
552 static inline unsigned int pci_slot_swivel(struct pci_pbm_info *pbm,
553                                            struct pci_dev *toplevel_pdev,
554                                            struct pci_dev *pdev,
555                                            unsigned int interrupt)
556 {
557         unsigned int ret;
558
559         if (unlikely(interrupt < 1 || interrupt > 4)) {
560                 printk("%s: Device %s interrupt value of %u is strange.\n",
561                        pbm->name, pci_name(pdev), interrupt);
562                 return interrupt;
563         }
564
565         ret = ((interrupt - 1 + (PCI_SLOT(pdev->devfn) & 3)) & 3) + 1;
566
567         if (pci_irq_verbose)
568                 printk("%s: %s IRQ Swivel %s [%x:%x] -> [%x]\n",
569                        pbm->name, pci_name(toplevel_pdev), pci_name(pdev),
570                        interrupt, PCI_SLOT(pdev->devfn), ret);
571
572         return ret;
573 }
574
575 static inline unsigned int pci_apply_intmap(struct pci_pbm_info *pbm,
576                                             struct pci_dev *toplevel_pdev,
577                                             struct pci_dev *pbus,
578                                             struct pci_dev *pdev,
579                                             unsigned int interrupt,
580                                             struct device_node **cnode)
581 {
582         struct linux_prom_pci_intmap *imap;
583         struct linux_prom_pci_intmask *imask;
584         struct pcidev_cookie *pbus_pcp = pbus->sysdata;
585         struct pcidev_cookie *pdev_pcp = pdev->sysdata;
586         struct linux_prom_pci_registers *pregs = pdev_pcp->prom_regs;
587         struct property *prop;
588         int plen, num_imap, i;
589         unsigned int hi, mid, lo, irq, orig_interrupt;
590
591         *cnode = pbus_pcp->prom_node;
592
593         prop = of_find_property(pbus_pcp->prom_node, "interrupt-map", &plen);
594         if (!prop ||
595             (plen % sizeof(struct linux_prom_pci_intmap)) != 0) {
596                 printk("%s: Device %s interrupt-map has bad len %d\n",
597                        pbm->name, pci_name(pbus), plen);
598                 goto no_intmap;
599         }
600         imap = prop->value;
601         num_imap = plen / sizeof(struct linux_prom_pci_intmap);
602
603         prop = of_find_property(pbus_pcp->prom_node, "interrupt-map-mask", &plen);
604         if (!prop ||
605             (plen % sizeof(struct linux_prom_pci_intmask)) != 0) {
606                 printk("%s: Device %s interrupt-map-mask has bad len %d\n",
607                        pbm->name, pci_name(pbus), plen);
608                 goto no_intmap;
609         }
610         imask = prop->value;
611
612         orig_interrupt = interrupt;
613
614         hi   = pregs->phys_hi & imask->phys_hi;
615         mid  = pregs->phys_mid & imask->phys_mid;
616         lo   = pregs->phys_lo & imask->phys_lo;
617         irq  = interrupt & imask->interrupt;
618
619         for (i = 0; i < num_imap; i++) {
620                 if (imap[i].phys_hi  == hi   &&
621                     imap[i].phys_mid == mid  &&
622                     imap[i].phys_lo  == lo   &&
623                     imap[i].interrupt == irq) {
624                         *cnode = of_find_node_by_phandle(imap[i].cnode);
625                         interrupt = imap[i].cinterrupt;
626                 }
627         }
628
629         if (pci_irq_verbose)
630                 printk("%s: %s MAP BUS %s DEV %s [%x] -> [%x]\n",
631                        pbm->name, pci_name(toplevel_pdev),
632                        pci_name(pbus), pci_name(pdev),
633                        orig_interrupt, interrupt);
634
635 no_intmap:
636         return interrupt;
637 }
638
639 /* For each PCI bus on the way to the root:
640  * 1) If it has an interrupt-map property, apply it.
641  * 2) Else, swivel the interrupt number based upon the PCI device number.
642  *
643  * Return the "IRQ controller" node.  If this is the PBM's device node,
644  * all interrupt translations are complete, else we should use that node's
645  * "reg" property to apply the PBM's "interrupt-{map,mask}" to the interrupt.
646  */
647 static struct device_node * __init
648 pci_intmap_match_to_root(struct pci_pbm_info *pbm,
649                          struct pci_dev *pdev,
650                          unsigned int *interrupt)
651 {
652         struct pci_dev *toplevel_pdev = pdev;
653         struct pcidev_cookie *toplevel_pcp = toplevel_pdev->sysdata;
654         struct device_node *cnode = toplevel_pcp->prom_node;
655
656         while (pdev->bus->number != pbm->pci_first_busno) {
657                 struct pci_dev *pbus = pdev->bus->self;
658                 struct pcidev_cookie *pcp = pbus->sysdata;
659                 struct property *prop;
660
661                 prop = of_find_property(pcp->prom_node, "interrupt-map", NULL);
662                 if (!prop) {
663                         *interrupt = pci_slot_swivel(pbm, toplevel_pdev,
664                                                      pdev, *interrupt);
665                         cnode = pcp->prom_node;
666                 } else {
667                         *interrupt = pci_apply_intmap(pbm, toplevel_pdev,
668                                                       pbus, pdev,
669                                                       *interrupt, &cnode);
670
671                         while (pcp->prom_node != cnode &&
672                                pbus->bus->number != pbm->pci_first_busno) {
673                                 pbus = pbus->bus->self;
674                                 pcp = pbus->sysdata;
675                         }
676                 }
677                 pdev = pbus;
678
679                 if (cnode == pbm->prom_node)
680                         break;
681         }
682
683         return cnode;
684 }
685
686 static int __init pci_intmap_match(struct pci_dev *pdev, unsigned int *interrupt)
687 {
688         struct pcidev_cookie *dev_pcp = pdev->sysdata;
689         struct pci_pbm_info *pbm = dev_pcp->pbm;
690         struct linux_prom_pci_registers *reg;
691         struct device_node *cnode;
692         struct property *prop;
693         unsigned int hi, mid, lo, irq;
694         int i, plen;
695
696         cnode = pci_intmap_match_to_root(pbm, pdev, interrupt);
697         if (cnode == pbm->prom_node)
698                 goto success;
699
700         prop = of_find_property(cnode, "reg", &plen);
701         if (!prop ||
702             (plen % sizeof(struct linux_prom_pci_registers)) != 0) {
703                 printk("%s: OBP node %s reg property has bad len %d\n",
704                        pbm->name, cnode->full_name, plen);
705                 goto fail;
706         }
707         reg = prop->value;
708
709         hi   = reg[0].phys_hi & pbm->pbm_intmask->phys_hi;
710         mid  = reg[0].phys_mid & pbm->pbm_intmask->phys_mid;
711         lo   = reg[0].phys_lo & pbm->pbm_intmask->phys_lo;
712         irq  = *interrupt & pbm->pbm_intmask->interrupt;
713
714         for (i = 0; i < pbm->num_pbm_intmap; i++) {
715                 struct linux_prom_pci_intmap *intmap;
716
717                 intmap = &pbm->pbm_intmap[i];
718
719                 if (intmap->phys_hi  == hi  &&
720                     intmap->phys_mid == mid &&
721                     intmap->phys_lo  == lo  &&
722                     intmap->interrupt == irq) {
723                         *interrupt = intmap->cinterrupt;
724                         goto success;
725                 }
726         }
727
728 fail:
729         return 0;
730
731 success:
732         if (pci_irq_verbose)
733                 printk("%s: Routing bus[%2x] slot[%2x] to INO[%02x]\n",
734                        pbm->name,
735                        pdev->bus->number, PCI_SLOT(pdev->devfn),
736                        *interrupt);
737         return 1;
738 }
739
740 static void __init pdev_fixup_irq(struct pci_dev *pdev)
741 {
742         struct pcidev_cookie *pcp = pdev->sysdata;
743         struct pci_pbm_info *pbm = pcp->pbm;
744         struct pci_controller_info *p = pbm->parent;
745         unsigned int portid = pbm->portid;
746         unsigned int prom_irq;
747         struct device_node *dp = pcp->prom_node;
748         struct property *prop;
749
750         /* If this is an empty EBUS device, sometimes OBP fails to
751          * give it a valid fully specified interrupts property.
752          * The EBUS hooked up to SunHME on PCI I/O boards of
753          * Ex000 systems is one such case.
754          *
755          * The interrupt is not important so just ignore it.
756          */
757         if (pdev->vendor == PCI_VENDOR_ID_SUN &&
758             pdev->device == PCI_DEVICE_ID_SUN_EBUS &&
759             !dp->child) {
760                 pdev->irq = 0;
761                 return;
762         }
763
764         prop = of_find_property(dp, "interrupts", NULL);
765         if (!prop) {
766                 pdev->irq = 0;
767                 return;
768         }
769         prom_irq = *(unsigned int *) prop->value;
770
771         if (tlb_type != hypervisor) {
772                 /* Fully specified already? */
773                 if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
774                         pdev->irq = p->irq_build(pbm, pdev, prom_irq);
775                         goto have_irq;
776                 }
777
778                 /* An onboard device? (bit 5 set) */
779                 if ((prom_irq & PCI_IRQ_INO) & 0x20) {
780                         pdev->irq = p->irq_build(pbm, pdev, (portid << 6 | prom_irq));
781                         goto have_irq;
782                 }
783         }
784
785         /* Can we find a matching entry in the interrupt-map? */
786         if (pci_intmap_match(pdev, &prom_irq)) {
787                 pdev->irq = p->irq_build(pbm, pdev, (portid << 6) | prom_irq);
788                 goto have_irq;
789         }
790
791         /* Ok, we have to do it the hard way. */
792         {
793                 unsigned int bus, slot, line;
794
795                 bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
796
797                 /* If we have a legal interrupt property, use it as
798                  * the IRQ line.
799                  */
800                 if (prom_irq > 0 && prom_irq < 5) {
801                         line = ((prom_irq - 1) & 3);
802                 } else {
803                         u8 pci_irq_line;
804
805                         /* Else just directly consult PCI config space. */
806                         pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
807                         line = ((pci_irq_line - 1) & 3);
808                 }
809
810                 /* Now figure out the slot.
811                  *
812                  * Basically, device number zero on the top-level bus is
813                  * always the PCI host controller.  Slot 0 is then device 1.
814                  * PBM A supports two external slots (0 and 1), and PBM B
815                  * supports 4 external slots (0, 1, 2, and 3).  On-board PCI
816                  * devices are wired to device numbers outside of these
817                  * ranges. -DaveM
818                  */
819                 if (pdev->bus->number == pbm->pci_first_busno) {
820                         slot = PCI_SLOT(pdev->devfn) - pbm->pci_first_slot;
821                 } else {
822                         struct pci_dev *bus_dev;
823
824                         /* Underneath a bridge, use slot number of parent
825                          * bridge which is closest to the PBM.
826                          */
827                         bus_dev = pdev->bus->self;
828                         while (bus_dev->bus &&
829                                bus_dev->bus->number != pbm->pci_first_busno)
830                                 bus_dev = bus_dev->bus->self;
831
832                         slot = PCI_SLOT(bus_dev->devfn) - pbm->pci_first_slot;
833                 }
834                 slot = slot << 2;
835
836                 pdev->irq = p->irq_build(pbm, pdev,
837                                          ((portid << 6) & PCI_IRQ_IGN) |
838                                          (bus | slot | line));
839         }
840
841 have_irq:
842         pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
843                               pdev->irq & PCI_IRQ_INO);
844 }
845
846 void __init pci_fixup_irq(struct pci_pbm_info *pbm,
847                           struct pci_bus *pbus)
848 {
849         struct pci_dev *dev;
850         struct pci_bus *bus;
851
852         list_for_each_entry(dev, &pbus->devices, bus_list)
853                 pdev_fixup_irq(dev);
854
855         list_for_each_entry(bus, &pbus->children, node)
856                 pci_fixup_irq(pbm, bus);
857 }
858
859 static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
860 {
861         u16 cmd;
862         u8 hdr_type, min_gnt, ltimer;
863
864         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
865         cmd |= PCI_COMMAND_MASTER;
866         pci_write_config_word(pdev, PCI_COMMAND, cmd);
867
868         /* Read it back, if the mastering bit did not
869          * get set, the device does not support bus
870          * mastering so we have nothing to do here.
871          */
872         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
873         if ((cmd & PCI_COMMAND_MASTER) == 0)
874                 return;
875
876         /* Set correct cache line size, 64-byte on all
877          * Sparc64 PCI systems.  Note that the value is
878          * measured in 32-bit words.
879          */
880         pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
881                               64 / sizeof(u32));
882
883         pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
884         hdr_type &= ~0x80;
885         if (hdr_type != PCI_HEADER_TYPE_NORMAL)
886                 return;
887
888         /* If the latency timer is already programmed with a non-zero
889          * value, assume whoever set it (OBP or whoever) knows what
890          * they are doing.
891          */
892         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
893         if (ltimer != 0)
894                 return;
895
896         /* XXX Since I'm tipping off the min grant value to
897          * XXX choose a suitable latency timer value, I also
898          * XXX considered making use of the max latency value
899          * XXX as well.  Unfortunately I've seen too many bogusly
900          * XXX low settings for it to the point where it lacks
901          * XXX any usefulness.  In one case, an ethernet card
902          * XXX claimed a min grant of 10 and a max latency of 5.
903          * XXX Now, if I had two such cards on the same bus I
904          * XXX could not set the desired burst period (calculated
905          * XXX from min grant) without violating the max latency
906          * XXX bound.  Duh...
907          * XXX
908          * XXX I blame dumb PC bios implementors for stuff like
909          * XXX this, most of them don't even try to do something
910          * XXX sensible with latency timer values and just set some
911          * XXX default value (usually 32) into every device.
912          */
913
914         pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
915
916         if (min_gnt == 0) {
917                 /* If no min_gnt setting then use a default
918                  * value.
919                  */
920                 if (is_66mhz)
921                         ltimer = 16;
922                 else
923                         ltimer = 32;
924         } else {
925                 int shift_factor;
926
927                 if (is_66mhz)
928                         shift_factor = 2;
929                 else
930                         shift_factor = 3;
931
932                 /* Use a default value when the min_gnt value
933                  * is erroneously high.
934                  */
935                 if (((unsigned int) min_gnt << shift_factor) > 512 ||
936                     ((min_gnt << shift_factor) & 0xff) == 0) {
937                         ltimer = 8 << shift_factor;
938                 } else {
939                         ltimer = min_gnt << shift_factor;
940                 }
941         }
942
943         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
944 }
945
946 void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
947                                      struct pci_bus *pbus)
948 {
949         struct pci_dev *pdev;
950         int all_are_66mhz;
951         u16 status;
952
953         if (pbm->is_66mhz_capable == 0) {
954                 all_are_66mhz = 0;
955                 goto out;
956         }
957
958         all_are_66mhz = 1;
959         list_for_each_entry(pdev, &pbus->devices, bus_list) {
960                 pci_read_config_word(pdev, PCI_STATUS, &status);
961                 if (!(status & PCI_STATUS_66MHZ)) {
962                         all_are_66mhz = 0;
963                         break;
964                 }
965         }
966 out:
967         pbm->all_devs_66mhz = all_are_66mhz;
968
969         printk("PCI%d(PBM%c): Bus running at %dMHz\n",
970                pbm->parent->index,
971                (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
972                (all_are_66mhz ? 66 : 33));
973 }
974
975 void pci_setup_busmastering(struct pci_pbm_info *pbm,
976                             struct pci_bus *pbus)
977 {
978         struct pci_dev *dev;
979         struct pci_bus *bus;
980         int is_66mhz;
981
982         is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
983
984         list_for_each_entry(dev, &pbus->devices, bus_list)
985                 pdev_setup_busmastering(dev, is_66mhz);
986
987         list_for_each_entry(bus, &pbus->children, node)
988                 pci_setup_busmastering(pbm, bus);
989 }
990
991 void pci_register_legacy_regions(struct resource *io_res,
992                                  struct resource *mem_res)
993 {
994         struct resource *p;
995
996         /* VGA Video RAM. */
997         p = kzalloc(sizeof(*p), GFP_KERNEL);
998         if (!p)
999                 return;
1000
1001         p->name = "Video RAM area";
1002         p->start = mem_res->start + 0xa0000UL;
1003         p->end = p->start + 0x1ffffUL;
1004         p->flags = IORESOURCE_BUSY;
1005         request_resource(mem_res, p);
1006
1007         p = kzalloc(sizeof(*p), GFP_KERNEL);
1008         if (!p)
1009                 return;
1010
1011         p->name = "System ROM";
1012         p->start = mem_res->start + 0xf0000UL;
1013         p->end = p->start + 0xffffUL;
1014         p->flags = IORESOURCE_BUSY;
1015         request_resource(mem_res, p);
1016
1017         p = kzalloc(sizeof(*p), GFP_KERNEL);
1018         if (!p)
1019                 return;
1020
1021         p->name = "Video ROM";
1022         p->start = mem_res->start + 0xc0000UL;
1023         p->end = p->start + 0x7fffUL;
1024         p->flags = IORESOURCE_BUSY;
1025         request_resource(mem_res, p);
1026 }
1027
1028 /* Generic helper routines for PCI error reporting. */
1029 void pci_scan_for_target_abort(struct pci_controller_info *p,
1030                                struct pci_pbm_info *pbm,
1031                                struct pci_bus *pbus)
1032 {
1033         struct pci_dev *pdev;
1034         struct pci_bus *bus;
1035
1036         list_for_each_entry(pdev, &pbus->devices, bus_list) {
1037                 u16 status, error_bits;
1038
1039                 pci_read_config_word(pdev, PCI_STATUS, &status);
1040                 error_bits =
1041                         (status & (PCI_STATUS_SIG_TARGET_ABORT |
1042                                    PCI_STATUS_REC_TARGET_ABORT));
1043                 if (error_bits) {
1044                         pci_write_config_word(pdev, PCI_STATUS, error_bits);
1045                         printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
1046                                p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1047                                pci_name(pdev), status);
1048                 }
1049         }
1050
1051         list_for_each_entry(bus, &pbus->children, node)
1052                 pci_scan_for_target_abort(p, pbm, bus);
1053 }
1054
1055 void pci_scan_for_master_abort(struct pci_controller_info *p,
1056                                struct pci_pbm_info *pbm,
1057                                struct pci_bus *pbus)
1058 {
1059         struct pci_dev *pdev;
1060         struct pci_bus *bus;
1061
1062         list_for_each_entry(pdev, &pbus->devices, bus_list) {
1063                 u16 status, error_bits;
1064
1065                 pci_read_config_word(pdev, PCI_STATUS, &status);
1066                 error_bits =
1067                         (status & (PCI_STATUS_REC_MASTER_ABORT));
1068                 if (error_bits) {
1069                         pci_write_config_word(pdev, PCI_STATUS, error_bits);
1070                         printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
1071                                p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1072                                pci_name(pdev), status);
1073                 }
1074         }
1075
1076         list_for_each_entry(bus, &pbus->children, node)
1077                 pci_scan_for_master_abort(p, pbm, bus);
1078 }
1079
1080 void pci_scan_for_parity_error(struct pci_controller_info *p,
1081                                struct pci_pbm_info *pbm,
1082                                struct pci_bus *pbus)
1083 {
1084         struct pci_dev *pdev;
1085         struct pci_bus *bus;
1086
1087         list_for_each_entry(pdev, &pbus->devices, bus_list) {
1088                 u16 status, error_bits;
1089
1090                 pci_read_config_word(pdev, PCI_STATUS, &status);
1091                 error_bits =
1092                         (status & (PCI_STATUS_PARITY |
1093                                    PCI_STATUS_DETECTED_PARITY));
1094                 if (error_bits) {
1095                         pci_write_config_word(pdev, PCI_STATUS, error_bits);
1096                         printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
1097                                p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1098                                pci_name(pdev), status);
1099                 }
1100         }
1101
1102         list_for_each_entry(bus, &pbus->children, node)
1103                 pci_scan_for_parity_error(p, pbm, bus);
1104 }