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