Import 2.3.18pre1
[davej-history.git] / arch / sparc64 / kernel / pci_common.c
bloba3600df9cbf4908cdf0b71a8ad77ee846cdcefe7
1 /* $Id: pci_common.c,v 1.3 1999/09/04 22:26:32 ecd Exp $
2 * pci_common.c: PCI controller common support.
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
5 */
7 #include <linux/string.h>
8 #include <linux/malloc.h>
9 #include <linux/init.h>
11 #include <asm/pbm.h>
13 /* Find the OBP PROM device tree node for a PCI device.
14 * Return zero if not found.
16 static int __init find_device_prom_node(struct pci_pbm_info *pbm,
17 struct pci_dev *pdev,
18 int bus_prom_node,
19 struct linux_prom_pci_registers *pregs,
20 int *nregs)
22 int node;
25 * Return the PBM's PROM node in case we are it's PCI device,
26 * as the PBM's reg property is different to standard PCI reg
27 * properties. We would delete this device entry otherwise,
28 * which confuses XFree86's device probing...
30 if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
31 (pdev->vendor == PCI_VENDOR_ID_SUN) &&
32 (pdev->device == PCI_DEVICE_ID_SUN_PBM)) {
33 *nregs = 0;
34 return bus_prom_node;
37 node = prom_getchild(bus_prom_node);
38 while (node != 0) {
39 int err = prom_getproperty(node, "reg",
40 (char *)pregs,
41 sizeof(*pregs) * PROMREG_MAX);
42 if (err == 0 || err == -1)
43 goto do_next_sibling;
44 if (((pregs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
45 *nregs = err / sizeof(*pregs);
46 return node;
49 do_next_sibling:
50 node = prom_getsibling(node);
52 return 0;
55 /* Remove a PCI device from the device trees, then
56 * free it up. Note that this must run before
57 * the device's resources are registered because we
58 * do not handle unregistering them here.
60 static void pci_device_delete(struct pci_dev *pdev)
62 struct pci_dev **dpp;
64 /* First, unlink from list of all devices. */
65 dpp = &pci_devices;
66 while (*dpp != NULL) {
67 if (*dpp == pdev) {
68 *dpp = pdev->next;
69 pdev->next = NULL;
70 break;
72 dpp = &(*dpp)->next;
75 /* Next, unlink from bus sibling chain. */
76 dpp = &pdev->bus->devices;
77 while (*dpp != NULL) {
78 if (*dpp == pdev) {
79 *dpp = pdev->sibling;
80 pdev->sibling = NULL;
81 break;
83 dpp = &(*dpp)->sibling;
86 /* Ok, all references are gone, free it up. */
87 kfree(pdev);
90 /* Fill in the PCI device cookie sysdata for the given
91 * PCI device. This cookie is the means by which one
92 * can get to OBP and PCI controller specific information
93 * for a PCI device.
95 static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
96 struct pci_dev *pdev,
97 int bus_prom_node)
99 struct linux_prom_pci_registers pregs[PROMREG_MAX];
100 struct pcidev_cookie *pcp;
101 int device_prom_node, nregs, err;
103 device_prom_node = find_device_prom_node(pbm, pdev, bus_prom_node,
104 pregs, &nregs);
105 if (device_prom_node == 0) {
106 /* If it is not in the OBP device tree then
107 * there must be a damn good reason for it.
109 * So what we do is delete the device from the
110 * PCI device tree completely. This scenerio
111 * is seen, for example, on CP1500 for the
112 * second EBUS/HappyMeal pair if the external
113 * connector for it is not present.
115 pci_device_delete(pdev);
116 return;
119 pcp = kmalloc(sizeof(*pcp), GFP_ATOMIC);
120 if (pcp == NULL) {
121 prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
122 prom_halt();
124 pcp->pbm = pbm;
125 pcp->prom_node = device_prom_node;
126 memcpy(pcp->prom_regs, pregs, sizeof(pcp->prom_regs));
127 pcp->num_prom_regs = nregs;
128 err = prom_getproperty(device_prom_node, "name",
129 pcp->prom_name, sizeof(pcp->prom_name));
130 if (err > 0)
131 pcp->prom_name[err] = 0;
132 else
133 pcp->prom_name[0] = 0;
134 if (strcmp(pcp->prom_name, "ebus") == 0) {
135 struct linux_prom_ebus_ranges erng[PROM_PCIRNG_MAX];
136 int iter;
138 /* EBUS is special... */
139 err = prom_getproperty(device_prom_node, "ranges",
140 (char *)&erng[0], sizeof(erng));
141 if (err == 0 || err == -1) {
142 prom_printf("EBUS: Fatal error, no range property\n");
143 prom_halt();
145 err = (err / sizeof(erng[0]));
146 for(iter = 0; iter < err; iter++) {
147 struct linux_prom_ebus_ranges *ep = &erng[iter];
148 struct linux_prom_pci_registers *ap;
150 ap = &pcp->prom_assignments[iter];
152 ap->phys_hi = ep->parent_phys_hi;
153 ap->phys_mid = ep->parent_phys_mid;
154 ap->phys_lo = ep->parent_phys_lo;
155 ap->size_hi = 0;
156 ap->size_lo = ep->size;
158 pcp->num_prom_assignments = err;
159 } else {
160 err = prom_getproperty(device_prom_node,
161 "assigned-addresses",
162 (char *)pcp->prom_assignments,
163 sizeof(pcp->prom_assignments));
164 if (err == 0 || err == -1)
165 pcp->num_prom_assignments = 0;
166 else
167 pcp->num_prom_assignments =
168 (err / sizeof(pcp->prom_assignments[0]));
171 pdev->sysdata = pcp;
174 void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
175 struct pci_pbm_info *pbm,
176 int prom_node)
178 struct pci_dev *pdev;
180 /* This loop is coded like this because the cookie
181 * fillin routine can delete devices from the tree.
183 pdev = pbus->devices;
184 while (pdev != NULL) {
185 struct pci_dev *next = pdev->sibling;
187 pdev_cookie_fillin(pbm, pdev, prom_node);
189 pdev = next;
192 for (pbus = pbus->children; pbus; pbus = pbus->next) {
193 struct pcidev_cookie *pcp = pbus->self->sysdata;
194 pci_fill_in_pbm_cookies(pbus, pbm, pcp->prom_node);
198 static void __init bad_assignment(struct linux_prom_pci_registers *ap,
199 struct resource *res,
200 int do_prom_halt)
202 prom_printf("PCI: Bogus PROM assignment.\n");
203 if (ap)
204 prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
205 ap->phys_hi, ap->phys_mid, ap->phys_lo,
206 ap->size_hi, ap->size_lo);
207 if (res)
208 prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
209 res->start, res->end, res->flags);
210 prom_printf("Please email this information to davem@redhat.com\n");
211 if (do_prom_halt)
212 prom_halt();
215 static struct resource *
216 __init get_root_resource(struct linux_prom_pci_registers *ap,
217 struct pci_pbm_info *pbm)
219 int space = (ap->phys_hi >> 24) & 3;
221 switch (space) {
222 case 0:
223 /* Configuration space, silently ignore it. */
224 return NULL;
226 case 1:
227 /* 16-bit IO space */
228 return &pbm->io_space;
230 case 2:
231 /* 32-bit MEM space */
232 return &pbm->mem_space;
234 case 3:
235 default:
236 /* 64-bit MEM space, unsupported. */
237 printk("PCI: 64-bit MEM assignment??? "
238 "Tell davem@redhat.com about it!\n");
239 return NULL;
243 static struct resource *
244 __init get_device_resource(struct linux_prom_pci_registers *ap,
245 struct pci_dev *pdev)
247 int breg = (ap->phys_hi & 0xff);
248 int space = (ap->phys_hi >> 24) & 3;
250 switch (breg) {
251 case PCI_ROM_ADDRESS:
252 /* It had better be MEM space. */
253 if (space != 2)
254 bad_assignment(ap, NULL, 0);
256 return &pdev->resource[PCI_ROM_RESOURCE];
258 case PCI_BASE_ADDRESS_0:
259 case PCI_BASE_ADDRESS_1:
260 case PCI_BASE_ADDRESS_2:
261 case PCI_BASE_ADDRESS_3:
262 case PCI_BASE_ADDRESS_4:
263 case PCI_BASE_ADDRESS_5:
264 return &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
266 default:
267 bad_assignment(ap, NULL, 0);
268 return NULL;
272 static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
273 struct pci_dev *pdev)
275 struct pcidev_cookie *pcp = pdev->sysdata;
276 int i;
278 for (i = 0; i < pcp->num_prom_assignments; i++) {
279 struct linux_prom_pci_registers *ap;
280 struct resource *root, *res;
282 /* The format of this property is specified in
283 * the PCI Bus Binding to IEEE1275-1994.
285 ap = &pcp->prom_assignments[i];
286 root = get_root_resource(ap, pbm);
287 res = get_device_resource(ap, pdev);
288 if (root == NULL || res == NULL)
289 continue;
291 /* Ok we know which resource this PROM assignment is
292 * for, sanity check it.
294 if ((res->start & 0xffffffffUL) != ap->phys_lo)
295 bad_assignment(ap, res, 1);
297 /* Adjust the resource into the physical address space
298 * of this PBM.
300 pbm->parent->resource_adjust(pdev, res, root);
302 if (request_resource(root, res) < 0) {
303 /* OK, there is some conflict. But this is fine
304 * since we'll reassign it in the fixup pass.
305 * Nevertheless notify the user that OBP made
306 * an error.
308 printk(KERN_ERR "PCI: Address space collision on region %ld "
309 "of device %s\n",
310 (res - &pdev->resource[0]), pdev->name);
315 void __init pci_record_assignments(struct pci_pbm_info *pbm,
316 struct pci_bus *pbus)
318 struct pci_dev *pdev;
320 for (pdev = pbus->devices; pdev; pdev = pdev->sibling)
321 pdev_record_assignments(pbm, pdev);
323 for (pbus = pbus->children; pbus; pbus = pbus->next)
324 pci_record_assignments(pbm, pbus);
327 static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
328 struct pci_dev *pdev)
330 u32 reg;
331 u16 cmd;
332 int i, io_seen, mem_seen;
334 io_seen = mem_seen = 0;
335 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
336 struct resource *root, *res;
337 unsigned long size, min, max, align;
339 res = &pdev->resource[i];
341 if (res->flags & IORESOURCE_IO)
342 io_seen++;
343 else if (res->flags & IORESOURCE_MEM)
344 mem_seen++;
346 /* If it is already assigned or the resource does
347 * not exist, there is nothing to do.
349 if (res->parent != NULL || res->flags == 0UL)
350 continue;
352 /* Determine the root we allocate from. */
353 if (res->flags & IORESOURCE_IO) {
354 root = &pbm->io_space;
355 min = root->start + 0x400UL;
356 max = root->end;
357 } else {
358 root = &pbm->mem_space;
359 min = root->start;
360 max = min + 0x80000000UL;
363 size = res->end - res->start;
364 align = size + 1;
365 if (allocate_resource(root, res, size + 1, min, max, align) < 0) {
366 /* uh oh */
367 prom_printf("PCI: Failed to allocate resource %d for %s\n",
368 i, pdev->name);
369 prom_halt();
372 /* Update PCI config space. */
373 pbm->parent->base_address_update(pdev, i);
376 /* Special case, disable the ROM. Several devices
377 * act funny (ie. do not respond to memory space writes)
378 * when it is left enabled. A good example are Qlogic,ISP
379 * adapters.
381 pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &reg);
382 reg &= ~PCI_ROM_ADDRESS_ENABLE;
383 pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
385 /* If we saw I/O or MEM resources, enable appropriate
386 * bits in PCI command register.
388 if (io_seen || mem_seen) {
389 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
390 if (io_seen)
391 cmd |= PCI_COMMAND_IO;
392 if (mem_seen)
393 cmd |= PCI_COMMAND_MEMORY;
394 pci_write_config_word(pdev, PCI_COMMAND, cmd);
397 /* If this is a PCI bridge or an IDE controller,
398 * enable bus mastering. In the former case also
399 * set the cache line size correctly.
401 if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
402 (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
403 ((pdev->class & 0x80) != 0))) {
404 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
405 cmd |= PCI_COMMAND_MASTER;
406 pci_write_config_word(pdev, PCI_COMMAND, cmd);
408 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
409 pci_write_config_byte(pdev,
410 PCI_CACHE_LINE_SIZE,
411 (64 / sizeof(u32)));
415 void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
416 struct pci_bus *pbus)
418 struct pci_dev *pdev;
420 for (pdev = pbus->devices; pdev; pdev = pdev->sibling)
421 pdev_assign_unassigned(pbm, pdev);
423 for (pbus = pbus->children; pbus; pbus = pbus->next)
424 pci_assign_unassigned(pbm, pbus);
427 static int __init pci_intmap_match(struct pci_dev *pdev, unsigned int *interrupt)
429 struct pcidev_cookie *dev_pcp = pdev->sysdata;
430 struct pci_pbm_info *pbm = dev_pcp->pbm;
431 struct linux_prom_pci_registers *pregs = dev_pcp->prom_regs;
432 unsigned int hi, mid, lo, irq;
433 int i;
435 if (pbm->num_pbm_intmap == 0)
436 return 0;
438 /* If we are underneath a PCI bridge, use PROM register
439 * property of parent bridge.
441 if (pdev->bus->number != pbm->pci_first_busno) {
442 struct pcidev_cookie *bus_pcp;
443 int offset;
445 bus_pcp = pdev->bus->self->sysdata;
446 pregs = bus_pcp->prom_regs;
447 offset = prom_getint(bus_pcp->prom_node,
448 "fcode-rom-offset");
450 /* Did PROM know better and assign an interrupt other
451 * than #INTA to the device? - We test here for presence of
452 * FCODE on the card, in this case we assume PROM has set
453 * correct 'interrupts' property, unless it is quadhme.
455 if (offset == -1 ||
456 !strcmp(bus_pcp->prom_name, "SUNW,qfe") ||
457 !strcmp(bus_pcp->prom_name, "qfe")) {
459 * No, use low slot number bits of child as IRQ line.
461 *interrupt = ((*interrupt - 1 + PCI_SLOT(pdev->devfn)) & 3) + 1;
465 hi = pregs->phys_hi & pbm->pbm_intmask.phys_hi;
466 mid = pregs->phys_mid & pbm->pbm_intmask.phys_mid;
467 lo = pregs->phys_lo & pbm->pbm_intmask.phys_lo;
468 irq = *interrupt & pbm->pbm_intmask.interrupt;
470 for (i = 0; i < pbm->num_pbm_intmap; i++) {
471 if (pbm->pbm_intmap[i].phys_hi == hi &&
472 pbm->pbm_intmap[i].phys_mid == mid &&
473 pbm->pbm_intmap[i].phys_lo == lo &&
474 pbm->pbm_intmap[i].interrupt == irq) {
475 *interrupt = pbm->pbm_intmap[i].cinterrupt;
476 return 1;
480 prom_printf("pbm_intmap_match: bus %02x, devfn %02x: ",
481 pdev->bus->number, pdev->devfn);
482 prom_printf("IRQ [%08x.%08x.%08x.%08x] not found in interrupt-map\n",
483 pregs->phys_hi, pregs->phys_mid, pregs->phys_lo, *interrupt);
484 prom_printf("Please email this information to davem@redhat.com\n");
485 prom_halt();
488 static void __init pdev_fixup_irq(struct pci_dev *pdev)
490 struct pcidev_cookie *pcp = pdev->sysdata;
491 struct pci_pbm_info *pbm = pcp->pbm;
492 struct pci_controller_info *p = pbm->parent;
493 unsigned int portid = p->portid;
494 unsigned int prom_irq;
495 int prom_node = pcp->prom_node;
496 int err;
498 err = prom_getproperty(prom_node, "interrupts",
499 (char *)&prom_irq, sizeof(prom_irq));
500 if (err == 0 || err == -1) {
501 pdev->irq = 0;
502 return;
505 /* Fully specified already? */
506 if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
507 pdev->irq = p->irq_build(p, pdev, prom_irq);
508 goto have_irq;
511 /* An onboard device? (bit 5 set) */
512 if ((prom_irq & PCI_IRQ_INO) & 0x20) {
513 pdev->irq = p->irq_build(p, pdev, (portid << 6 | prom_irq));
514 goto have_irq;
517 /* Can we find a matching entry in the interrupt-map? */
518 if (pci_intmap_match(pdev, &prom_irq)) {
519 pdev->irq = p->irq_build(p, pdev, (portid << 6) | prom_irq);
520 goto have_irq;
523 /* Ok, we have to do it the hard way. */
525 unsigned int bus, slot, line;
527 bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
529 /* If we have a legal interrupt property, use it as
530 * the IRQ line.
532 if (prom_irq > 0 && prom_irq < 5) {
533 line = ((prom_irq - 1) & 3);
534 } else {
535 u8 pci_irq_line;
537 /* Else just directly consult PCI config space. */
538 pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
539 line = ((pci_irq_line - 1) & 3);
542 /* Now figure out the slot. */
543 if (pdev->bus->number == pbm->pci_first_busno) {
544 if (pbm == &pbm->parent->pbm_A)
545 slot = (pdev->devfn >> 3) - 1;
546 else
547 slot = (pdev->devfn >> 3) - 2;
548 } else {
549 if (pbm == &pbm->parent->pbm_A)
550 slot = (pdev->bus->self->devfn >> 3) - 1;
551 else
552 slot = (pdev->bus->self->devfn >> 3) - 2;
554 slot = slot << 2;
556 pdev->irq = p->irq_build(p, pdev,
557 ((portid << 6) & PCI_IRQ_IGN) |
558 (bus | slot | line));
561 have_irq:
562 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
563 pdev->irq & PCI_IRQ_INO);
566 void __init pci_fixup_irq(struct pci_pbm_info *pbm,
567 struct pci_bus *pbus)
569 struct pci_dev *pdev;
571 for (pdev = pbus->devices; pdev; pdev = pdev->sibling)
572 pdev_fixup_irq(pdev);
574 for (pbus = pbus->children; pbus; pbus = pbus->next)
575 pci_fixup_irq(pbm, pbus);
578 /* Generic helper routines for PCI error reporting. */
579 void pci_scan_for_target_abort(struct pci_controller_info *p,
580 struct pci_pbm_info *pbm,
581 struct pci_bus *pbus)
583 struct pci_dev *pdev;
585 for (pdev = pbus->devices; pdev; pdev = pdev->sibling) {
586 u16 status, error_bits;
588 pci_read_config_word(pdev, PCI_STATUS, &status);
589 error_bits =
590 (status & (PCI_STATUS_SIG_TARGET_ABORT |
591 PCI_STATUS_REC_TARGET_ABORT));
592 if (error_bits) {
593 pci_write_config_word(pdev, PCI_STATUS, error_bits);
594 printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
595 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
596 pdev->name, status);
600 for (pbus = pbus->children; pbus; pbus = pbus->next)
601 pci_scan_for_target_abort(p, pbm, pbus);
604 void pci_scan_for_master_abort(struct pci_controller_info *p,
605 struct pci_pbm_info *pbm,
606 struct pci_bus *pbus)
608 struct pci_dev *pdev;
610 for (pdev = pbus->devices; pdev; pdev = pdev->sibling) {
611 u16 status, error_bits;
613 pci_read_config_word(pdev, PCI_STATUS, &status);
614 error_bits =
615 (status & (PCI_STATUS_REC_MASTER_ABORT));
616 if (error_bits) {
617 pci_write_config_word(pdev, PCI_STATUS, error_bits);
618 printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
619 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
620 pdev->name, status);
624 for (pbus = pbus->children; pbus; pbus = pbus->next)
625 pci_scan_for_master_abort(p, pbm, pbus);
628 void pci_scan_for_parity_error(struct pci_controller_info *p,
629 struct pci_pbm_info *pbm,
630 struct pci_bus *pbus)
632 struct pci_dev *pdev;
634 for (pdev = pbus->devices; pdev; pdev = pdev->sibling) {
635 u16 status, error_bits;
637 pci_read_config_word(pdev, PCI_STATUS, &status);
638 error_bits =
639 (status & (PCI_STATUS_PARITY |
640 PCI_STATUS_DETECTED_PARITY));
641 if (error_bits) {
642 pci_write_config_word(pdev, PCI_STATUS, error_bits);
643 printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
644 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
645 pdev->name, status);
649 for (pbus = pbus->children; pbus; pbus = pbus->next)
650 pci_scan_for_parity_error(p, pbm, pbus);