1 #undef DEBUG 2 3 #include <linux/kernel.h> 4 #include <linux/string.h> 5 #include <linux/pci_regs.h> 6 #include <linux/module.h> 7 #include <linux/ioport.h> 8 #include <asm/prom.h> 9 #include <asm/pci-bridge.h> 10 11 #ifdef DEBUG 12 #define DBG(fmt...) do { printk(fmt); } while(0) 13 #else 14 #define DBG(fmt...) do { } while(0) 15 #endif 16 17 #ifdef CONFIG_PPC64 18 #define PRu64 "%lx" 19 #else 20 #define PRu64 "%llx" 21 #endif 22 23 /* Max address size we deal with */ 24 #define OF_MAX_ADDR_CELLS 4 25 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ 26 (ns) > 0) 27 28 static struct of_bus *of_match_bus(struct device_node *np); 29 static int __of_address_to_resource(struct device_node *dev, 30 const u32 *addrp, u64 size, unsigned int flags, 31 struct resource *r); 32 33 34 /* Debug utility */ 35 #ifdef DEBUG 36 static void of_dump_addr(const char *s, const u32 *addr, int na) 37 { 38 printk("%s", s); 39 while(na--) 40 printk(" %08x", *(addr++)); 41 printk("\n"); 42 } 43 #else 44 static void of_dump_addr(const char *s, const u32 *addr, int na) { } 45 #endif 46 47 48 /* Callbacks for bus specific translators */ 49 struct of_bus { 50 const char *name; 51 const char *addresses; 52 int (*match)(struct device_node *parent); 53 void (*count_cells)(struct device_node *child, 54 int *addrc, int *sizec); 55 u64 (*map)(u32 *addr, const u32 *range, 56 int na, int ns, int pna); 57 int (*translate)(u32 *addr, u64 offset, int na); 58 unsigned int (*get_flags)(const u32 *addr); 59 }; 60 61 62 /* 63 * Default translator (generic bus) 64 */ 65 66 static void of_bus_default_count_cells(struct device_node *dev, 67 int *addrc, int *sizec) 68 { 69 if (addrc) 70 *addrc = prom_n_addr_cells(dev); 71 if (sizec) 72 *sizec = prom_n_size_cells(dev); 73 } 74 75 static u64 of_bus_default_map(u32 *addr, const u32 *range, 76 int na, int ns, int pna) 77 { 78 u64 cp, s, da; 79 80 cp = of_read_number(range, na); 81 s = of_read_number(range + na + pna, ns); 82 da = of_read_number(addr, na); 83 84 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", 85 cp, s, da); 86 87 if (da < cp || da >= (cp + s)) 88 return OF_BAD_ADDR; 89 return da - cp; 90 } 91 92 static int of_bus_default_translate(u32 *addr, u64 offset, int na) 93 { 94 u64 a = of_read_number(addr, na); 95 memset(addr, 0, na * 4); 96 a += offset; 97 if (na > 1) 98 addr[na - 2] = a >> 32; 99 addr[na - 1] = a & 0xffffffffu; 100 101 return 0; 102 } 103 104 static unsigned int of_bus_default_get_flags(const u32 *addr) 105 { 106 return IORESOURCE_MEM; 107 } 108 109 110 #ifdef CONFIG_PCI 111 /* 112 * PCI bus specific translator 113 */ 114 115 static int of_bus_pci_match(struct device_node *np) 116 { 117 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */ 118 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci"); 119 } 120 121 static void of_bus_pci_count_cells(struct device_node *np, 122 int *addrc, int *sizec) 123 { 124 if (addrc) 125 *addrc = 3; 126 if (sizec) 127 *sizec = 2; 128 } 129 130 static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna) 131 { 132 u64 cp, s, da; 133 134 /* Check address type match */ 135 if ((addr[0] ^ range[0]) & 0x03000000) 136 return OF_BAD_ADDR; 137 138 /* Read address values, skipping high cell */ 139 cp = of_read_number(range + 1, na - 1); 140 s = of_read_number(range + na + pna, ns); 141 da = of_read_number(addr + 1, na - 1); 142 143 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); 144 145 if (da < cp || da >= (cp + s)) 146 return OF_BAD_ADDR; 147 return da - cp; 148 } 149 150 static int of_bus_pci_translate(u32 *addr, u64 offset, int na) 151 { 152 return of_bus_default_translate(addr + 1, offset, na - 1); 153 } 154 155 static unsigned int of_bus_pci_get_flags(const u32 *addr) 156 { 157 unsigned int flags = 0; 158 u32 w = addr[0]; 159 160 switch((w >> 24) & 0x03) { 161 case 0x01: 162 flags |= IORESOURCE_IO; 163 break; 164 case 0x02: /* 32 bits */ 165 case 0x03: /* 64 bits */ 166 flags |= IORESOURCE_MEM; 167 break; 168 } 169 if (w & 0x40000000) 170 flags |= IORESOURCE_PREFETCH; 171 return flags; 172 } 173 174 const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, 175 unsigned int *flags) 176 { 177 const u32 *prop; 178 unsigned int psize; 179 struct device_node *parent; 180 struct of_bus *bus; 181 int onesize, i, na, ns; 182 183 /* Get parent & match bus type */ 184 parent = of_get_parent(dev); 185 if (parent == NULL) 186 return NULL; 187 bus = of_match_bus(parent); 188 if (strcmp(bus->name, "pci")) { 189 of_node_put(parent); 190 return NULL; 191 } 192 bus->count_cells(dev, &na, &ns); 193 of_node_put(parent); 194 if (!OF_CHECK_COUNTS(na, ns)) 195 return NULL; 196 197 /* Get "reg" or "assigned-addresses" property */ 198 prop = get_property(dev, bus->addresses, &psize); 199 if (prop == NULL) 200 return NULL; 201 psize /= 4; 202 203 onesize = na + ns; 204 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) 205 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { 206 if (size) 207 *size = of_read_number(prop + na, ns); 208 if (flags) 209 *flags = bus->get_flags(prop); 210 return prop; 211 } 212 return NULL; 213 } 214 EXPORT_SYMBOL(of_get_pci_address); 215 216 int of_pci_address_to_resource(struct device_node *dev, int bar, 217 struct resource *r) 218 { 219 const u32 *addrp; 220 u64 size; 221 unsigned int flags; 222 223 addrp = of_get_pci_address(dev, bar, &size, &flags); 224 if (addrp == NULL) 225 return -EINVAL; 226 return __of_address_to_resource(dev, addrp, size, flags, r); 227 } 228 EXPORT_SYMBOL_GPL(of_pci_address_to_resource); 229 230 static u8 of_irq_pci_swizzle(u8 slot, u8 pin) 231 { 232 return (((pin - 1) + slot) % 4) + 1; 233 } 234 235 int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) 236 { 237 struct device_node *dn, *ppnode; 238 struct pci_dev *ppdev; 239 u32 lspec; 240 u32 laddr[3]; 241 u8 pin; 242 int rc; 243 244 /* Check if we have a device node, if yes, fallback to standard OF 245 * parsing 246 */ 247 dn = pci_device_to_OF_node(pdev); 248 if (dn) 249 return of_irq_map_one(dn, 0, out_irq); 250 251 /* Ok, we don't, time to have fun. Let's start by building up an 252 * interrupt spec. we assume #interrupt-cells is 1, which is standard 253 * for PCI. If you do different, then don't use that routine. 254 */ 255 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); 256 if (rc != 0) 257 return rc; 258 /* No pin, exit */ 259 if (pin == 0) 260 return -ENODEV; 261 262 /* Now we walk up the PCI tree */ 263 lspec = pin; 264 for (;;) { 265 /* Get the pci_dev of our parent */ 266 ppdev = pdev->bus->self; 267 268 /* Ouch, it's a host bridge... */ 269 if (ppdev == NULL) { 270 #ifdef CONFIG_PPC64 271 ppnode = pci_bus_to_OF_node(pdev->bus); 272 #else 273 struct pci_controller *host; 274 host = pci_bus_to_host(pdev->bus); 275 ppnode = host ? host->arch_data : NULL; 276 #endif 277 /* No node for host bridge ? give up */ 278 if (ppnode == NULL) 279 return -EINVAL; 280 } else 281 /* We found a P2P bridge, check if it has a node */ 282 ppnode = pci_device_to_OF_node(ppdev); 283 284 /* Ok, we have found a parent with a device-node, hand over to 285 * the OF parsing code. 286 * We build a unit address from the linux device to be used for 287 * resolution. Note that we use the linux bus number which may 288 * not match your firmware bus numbering. 289 * Fortunately, in most cases, interrupt-map-mask doesn't include 290 * the bus number as part of the matching. 291 * You should still be careful about that though if you intend 292 * to rely on this function (you ship a firmware that doesn't 293 * create device nodes for all PCI devices). 294 */ 295 if (ppnode) 296 break; 297 298 /* We can only get here if we hit a P2P bridge with no node, 299 * let's do standard swizzling and try again 300 */ 301 lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec); 302 pdev = ppdev; 303 } 304 305 laddr[0] = (pdev->bus->number << 16) 306 | (pdev->devfn << 8); 307 laddr[1] = laddr[2] = 0; 308 return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq); 309 } 310 EXPORT_SYMBOL_GPL(of_irq_map_pci); 311 #endif /* CONFIG_PCI */ 312 313 /* 314 * ISA bus specific translator 315 */ 316 317 static int of_bus_isa_match(struct device_node *np) 318 { 319 return !strcmp(np->name, "isa"); 320 } 321 322 static void of_bus_isa_count_cells(struct device_node *child, 323 int *addrc, int *sizec) 324 { 325 if (addrc) 326 *addrc = 2; 327 if (sizec) 328 *sizec = 1; 329 } 330 331 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna) 332 { 333 u64 cp, s, da; 334 335 /* Check address type match */ 336 if ((addr[0] ^ range[0]) & 0x00000001) 337 return OF_BAD_ADDR; 338 339 /* Read address values, skipping high cell */ 340 cp = of_read_number(range + 1, na - 1); 341 s = of_read_number(range + na + pna, ns); 342 da = of_read_number(addr + 1, na - 1); 343 344 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); 345 346 if (da < cp || da >= (cp + s)) 347 return OF_BAD_ADDR; 348 return da - cp; 349 } 350 351 static int of_bus_isa_translate(u32 *addr, u64 offset, int na) 352 { 353 return of_bus_default_translate(addr + 1, offset, na - 1); 354 } 355 356 static unsigned int of_bus_isa_get_flags(const u32 *addr) 357 { 358 unsigned int flags = 0; 359 u32 w = addr[0]; 360 361 if (w & 1) 362 flags |= IORESOURCE_IO; 363 else 364 flags |= IORESOURCE_MEM; 365 return flags; 366 } 367 368 369 /* 370 * Array of bus specific translators 371 */ 372 373 static struct of_bus of_busses[] = { 374 #ifdef CONFIG_PCI 375 /* PCI */ 376 { 377 .name = "pci", 378 .addresses = "assigned-addresses", 379 .match = of_bus_pci_match, 380 .count_cells = of_bus_pci_count_cells, 381 .map = of_bus_pci_map, 382 .translate = of_bus_pci_translate, 383 .get_flags = of_bus_pci_get_flags, 384 }, 385 #endif /* CONFIG_PCI */ 386 /* ISA */ 387 { 388 .name = "isa", 389 .addresses = "reg", 390 .match = of_bus_isa_match, 391 .count_cells = of_bus_isa_count_cells, 392 .map = of_bus_isa_map, 393 .translate = of_bus_isa_translate, 394 .get_flags = of_bus_isa_get_flags, 395 }, 396 /* Default */ 397 { 398 .name = "default", 399 .addresses = "reg", 400 .match = NULL, 401 .count_cells = of_bus_default_count_cells, 402 .map = of_bus_default_map, 403 .translate = of_bus_default_translate, 404 .get_flags = of_bus_default_get_flags, 405 }, 406 }; 407 408 static struct of_bus *of_match_bus(struct device_node *np) 409 { 410 int i; 411 412 for (i = 0; i < ARRAY_SIZE(of_busses); i ++) 413 if (!of_busses[i].match || of_busses[i].match(np)) 414 return &of_busses[i]; 415 BUG(); 416 return NULL; 417 } 418 419 static int of_translate_one(struct device_node *parent, struct of_bus *bus, 420 struct of_bus *pbus, u32 *addr, 421 int na, int ns, int pna) 422 { 423 const u32 *ranges; 424 unsigned int rlen; 425 int rone; 426 u64 offset = OF_BAD_ADDR; 427 428 /* Normally, an absence of a "ranges" property means we are 429 * crossing a non-translatable boundary, and thus the addresses 430 * below the current not cannot be converted to CPU physical ones. 431 * Unfortunately, while this is very clear in the spec, it's not 432 * what Apple understood, and they do have things like /uni-n or 433 * /ht nodes with no "ranges" property and a lot of perfectly 434 * useable mapped devices below them. Thus we treat the absence of 435 * "ranges" as equivalent to an empty "ranges" property which means 436 * a 1:1 translation at that level. It's up to the caller not to try 437 * to translate addresses that aren't supposed to be translated in 438 * the first place. --BenH. 439 */ 440 ranges = get_property(parent, "ranges", &rlen); 441 if (ranges == NULL || rlen == 0) { 442 offset = of_read_number(addr, na); 443 memset(addr, 0, pna * 4); 444 DBG("OF: no ranges, 1:1 translation\n"); 445 goto finish; 446 } 447 448 DBG("OF: walking ranges...\n"); 449 450 /* Now walk through the ranges */ 451 rlen /= 4; 452 rone = na + pna + ns; 453 for (; rlen >= rone; rlen -= rone, ranges += rone) { 454 offset = bus->map(addr, ranges, na, ns, pna); 455 if (offset != OF_BAD_ADDR) 456 break; 457 } 458 if (offset == OF_BAD_ADDR) { 459 DBG("OF: not found !\n"); 460 return 1; 461 } 462 memcpy(addr, ranges + na, 4 * pna); 463 464 finish: 465 of_dump_addr("OF: parent translation for:", addr, pna); 466 DBG("OF: with offset: "PRu64"\n", offset); 467 468 /* Translate it into parent bus space */ 469 return pbus->translate(addr, offset, pna); 470 } 471 472 473 /* 474 * Translate an address from the device-tree into a CPU physical address, 475 * this walks up the tree and applies the various bus mappings on the 476 * way. 477 * 478 * Note: We consider that crossing any level with #size-cells == 0 to mean 479 * that translation is impossible (that is we are not dealing with a value 480 * that can be mapped to a cpu physical address). This is not really specified 481 * that way, but this is traditionally the way IBM at least do things 482 */ 483 u64 of_translate_address(struct device_node *dev, const u32 *in_addr) 484 { 485 struct device_node *parent = NULL; 486 struct of_bus *bus, *pbus; 487 u32 addr[OF_MAX_ADDR_CELLS]; 488 int na, ns, pna, pns; 489 u64 result = OF_BAD_ADDR; 490 491 DBG("OF: ** translation for device %s **\n", dev->full_name); 492 493 /* Increase refcount at current level */ 494 of_node_get(dev); 495 496 /* Get parent & match bus type */ 497 parent = of_get_parent(dev); 498 if (parent == NULL) 499 goto bail; 500 bus = of_match_bus(parent); 501 502 /* Cound address cells & copy address locally */ 503 bus->count_cells(dev, &na, &ns); 504 if (!OF_CHECK_COUNTS(na, ns)) { 505 printk(KERN_ERR "prom_parse: Bad cell count for %s\n", 506 dev->full_name); 507 goto bail; 508 } 509 memcpy(addr, in_addr, na * 4); 510 511 DBG("OF: bus is %s (na=%d, ns=%d) on %s\n", 512 bus->name, na, ns, parent->full_name); 513 of_dump_addr("OF: translating address:", addr, na); 514 515 /* Translate */ 516 for (;;) { 517 /* Switch to parent bus */ 518 of_node_put(dev); 519 dev = parent; 520 parent = of_get_parent(dev); 521 522 /* If root, we have finished */ 523 if (parent == NULL) { 524 DBG("OF: reached root node\n"); 525 result = of_read_number(addr, na); 526 break; 527 } 528 529 /* Get new parent bus and counts */ 530 pbus = of_match_bus(parent); 531 pbus->count_cells(dev, &pna, &pns); 532 if (!OF_CHECK_COUNTS(pna, pns)) { 533 printk(KERN_ERR "prom_parse: Bad cell count for %s\n", 534 dev->full_name); 535 break; 536 } 537 538 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n", 539 pbus->name, pna, pns, parent->full_name); 540 541 /* Apply bus translation */ 542 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna)) 543 break; 544 545 /* Complete the move up one level */ 546 na = pna; 547 ns = pns; 548 bus = pbus; 549 550 of_dump_addr("OF: one level translation:", addr, na); 551 } 552 bail: 553 of_node_put(parent); 554 of_node_put(dev); 555 556 return result; 557 } 558 EXPORT_SYMBOL(of_translate_address); 559 560 const u32 *of_get_address(struct device_node *dev, int index, u64 *size, 561 unsigned int *flags) 562 { 563 const u32 *prop; 564 unsigned int psize; 565 struct device_node *parent; 566 struct of_bus *bus; 567 int onesize, i, na, ns; 568 569 /* Get parent & match bus type */ 570 parent = of_get_parent(dev); 571 if (parent == NULL) 572 return NULL; 573 bus = of_match_bus(parent); 574 bus->count_cells(dev, &na, &ns); 575 of_node_put(parent); 576 if (!OF_CHECK_COUNTS(na, ns)) 577 return NULL; 578 579 /* Get "reg" or "assigned-addresses" property */ 580 prop = get_property(dev, bus->addresses, &psize); 581 if (prop == NULL) 582 return NULL; 583 psize /= 4; 584 585 onesize = na + ns; 586 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) 587 if (i == index) { 588 if (size) 589 *size = of_read_number(prop + na, ns); 590 if (flags) 591 *flags = bus->get_flags(prop); 592 return prop; 593 } 594 return NULL; 595 } 596 EXPORT_SYMBOL(of_get_address); 597 598 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp, 599 u64 size, unsigned int flags, 600 struct resource *r) 601 { 602 u64 taddr; 603 604 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0) 605 return -EINVAL; 606 taddr = of_translate_address(dev, addrp); 607 if (taddr == OF_BAD_ADDR) 608 return -EINVAL; 609 memset(r, 0, sizeof(struct resource)); 610 if (flags & IORESOURCE_IO) { 611 unsigned long port; 612 port = pci_address_to_pio(taddr); 613 if (port == (unsigned long)-1) 614 return -EINVAL; 615 r->start = port; 616 r->end = port + size - 1; 617 } else { 618 r->start = taddr; 619 r->end = taddr + size - 1; 620 } 621 r->flags = flags; 622 r->name = dev->name; 623 return 0; 624 } 625 626 int of_address_to_resource(struct device_node *dev, int index, 627 struct resource *r) 628 { 629 const u32 *addrp; 630 u64 size; 631 unsigned int flags; 632 633 addrp = of_get_address(dev, index, &size, &flags); 634 if (addrp == NULL) 635 return -EINVAL; 636 return __of_address_to_resource(dev, addrp, size, flags, r); 637 } 638 EXPORT_SYMBOL_GPL(of_address_to_resource); 639 640 void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop, 641 unsigned long *busno, unsigned long *phys, unsigned long *size) 642 { 643 const u32 *dma_window; 644 u32 cells; 645 const unsigned char *prop; 646 647 dma_window = dma_window_prop; 648 649 /* busno is always one cell */ 650 *busno = *(dma_window++); 651 652 prop = get_property(dn, "ibm,#dma-address-cells", NULL); 653 if (!prop) 654 prop = get_property(dn, "#address-cells", NULL); 655 656 cells = prop ? *(u32 *)prop : prom_n_addr_cells(dn); 657 *phys = of_read_number(dma_window, cells); 658 659 dma_window += cells; 660 661 prop = get_property(dn, "ibm,#dma-size-cells", NULL); 662 cells = prop ? *(u32 *)prop : prom_n_size_cells(dn); 663 *size = of_read_number(dma_window, cells); 664 } 665 666 /* 667 * Interrupt remapper 668 */ 669 670 static unsigned int of_irq_workarounds; 671 static struct device_node *of_irq_dflt_pic; 672 673 static struct device_node *of_irq_find_parent(struct device_node *child) 674 { 675 struct device_node *p; 676 const phandle *parp; 677 678 if (!of_node_get(child)) 679 return NULL; 680 681 do { 682 parp = get_property(child, "interrupt-parent", NULL); 683 if (parp == NULL) 684 p = of_get_parent(child); 685 else { 686 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) 687 p = of_node_get(of_irq_dflt_pic); 688 else 689 p = of_find_node_by_phandle(*parp); 690 } 691 of_node_put(child); 692 child = p; 693 } while (p && get_property(p, "#interrupt-cells", NULL) == NULL); 694 695 return p; 696 } 697 698 /* This doesn't need to be called if you don't have any special workaround 699 * flags to pass 700 */ 701 void of_irq_map_init(unsigned int flags) 702 { 703 of_irq_workarounds = flags; 704 705 /* OldWorld, don't bother looking at other things */ 706 if (flags & OF_IMAP_OLDWORLD_MAC) 707 return; 708 709 /* If we don't have phandles, let's try to locate a default interrupt 710 * controller (happens when booting with BootX). We do a first match 711 * here, hopefully, that only ever happens on machines with one 712 * controller. 713 */ 714 if (flags & OF_IMAP_NO_PHANDLE) { 715 struct device_node *np; 716 717 for(np = NULL; (np = of_find_all_nodes(np)) != NULL;) { 718 if (get_property(np, "interrupt-controller", NULL) 719 == NULL) 720 continue; 721 /* Skip /chosen/interrupt-controller */ 722 if (strcmp(np->name, "chosen") == 0) 723 continue; 724 /* It seems like at least one person on this planet wants 725 * to use BootX on a machine with an AppleKiwi controller 726 * which happens to pretend to be an interrupt 727 * controller too. 728 */ 729 if (strcmp(np->name, "AppleKiwi") == 0) 730 continue; 731 /* I think we found one ! */ 732 of_irq_dflt_pic = np; 733 break; 734 } 735 } 736 737 } 738 739 int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize, 740 const u32 *addr, struct of_irq *out_irq) 741 { 742 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL; 743 const u32 *tmp, *imap, *imask; 744 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0; 745 int imaplen, match, i; 746 747 DBG("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n", 748 parent->full_name, intspec[0], intspec[1], ointsize); 749 750 ipar = of_node_get(parent); 751 752 /* First get the #interrupt-cells property of the current cursor 753 * that tells us how to interpret the passed-in intspec. If there 754 * is none, we are nice and just walk up the tree 755 */ 756 do { 757 tmp = get_property(ipar, "#interrupt-cells", NULL); 758 if (tmp != NULL) { 759 intsize = *tmp; 760 break; 761 } 762 tnode = ipar; 763 ipar = of_irq_find_parent(ipar); 764 of_node_put(tnode); 765 } while (ipar); 766 if (ipar == NULL) { 767 DBG(" -> no parent found !\n"); 768 goto fail; 769 } 770 771 DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize); 772 773 if (ointsize != intsize) 774 return -EINVAL; 775 776 /* Look for this #address-cells. We have to implement the old linux 777 * trick of looking for the parent here as some device-trees rely on it 778 */ 779 old = of_node_get(ipar); 780 do { 781 tmp = get_property(old, "#address-cells", NULL); 782 tnode = of_get_parent(old); 783 of_node_put(old); 784 old = tnode; 785 } while(old && tmp == NULL); 786 of_node_put(old); 787 old = NULL; 788 addrsize = (tmp == NULL) ? 2 : *tmp; 789 790 DBG(" -> addrsize=%d\n", addrsize); 791 792 /* Now start the actual "proper" walk of the interrupt tree */ 793 while (ipar != NULL) { 794 /* Now check if cursor is an interrupt-controller and if it is 795 * then we are done 796 */ 797 if (get_property(ipar, "interrupt-controller", NULL) != NULL) { 798 DBG(" -> got it !\n"); 799 memcpy(out_irq->specifier, intspec, 800 intsize * sizeof(u32)); 801 out_irq->size = intsize; 802 out_irq->controller = ipar; 803 of_node_put(old); 804 return 0; 805 } 806 807 /* Now look for an interrupt-map */ 808 imap = get_property(ipar, "interrupt-map", &imaplen); 809 /* No interrupt map, check for an interrupt parent */ 810 if (imap == NULL) { 811 DBG(" -> no map, getting parent\n"); 812 newpar = of_irq_find_parent(ipar); 813 goto skiplevel; 814 } 815 imaplen /= sizeof(u32); 816 817 /* Look for a mask */ 818 imask = get_property(ipar, "interrupt-map-mask", NULL); 819 820 /* If we were passed no "reg" property and we attempt to parse 821 * an interrupt-map, then #address-cells must be 0. 822 * Fail if it's not. 823 */ 824 if (addr == NULL && addrsize != 0) { 825 DBG(" -> no reg passed in when needed !\n"); 826 goto fail; 827 } 828 829 /* Parse interrupt-map */ 830 match = 0; 831 while (imaplen > (addrsize + intsize + 1) && !match) { 832 /* Compare specifiers */ 833 match = 1; 834 for (i = 0; i < addrsize && match; ++i) { 835 u32 mask = imask ? imask[i] : 0xffffffffu; 836 match = ((addr[i] ^ imap[i]) & mask) == 0; 837 } 838 for (; i < (addrsize + intsize) && match; ++i) { 839 u32 mask = imask ? imask[i] : 0xffffffffu; 840 match = 841 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0; 842 } 843 imap += addrsize + intsize; 844 imaplen -= addrsize + intsize; 845 846 DBG(" -> match=%d (imaplen=%d)\n", match, imaplen); 847 848 /* Get the interrupt parent */ 849 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) 850 newpar = of_node_get(of_irq_dflt_pic); 851 else 852 newpar = of_find_node_by_phandle((phandle)*imap); 853 imap++; 854 --imaplen; 855 856 /* Check if not found */ 857 if (newpar == NULL) { 858 DBG(" -> imap parent not found !\n"); 859 goto fail; 860 } 861 862 /* Get #interrupt-cells and #address-cells of new 863 * parent 864 */ 865 tmp = get_property(newpar, "#interrupt-cells", 866 NULL); 867 if (tmp == NULL) { 868 DBG(" -> parent lacks #interrupt-cells !\n"); 869 goto fail; 870 } 871 newintsize = *tmp; 872 tmp = get_property(newpar, "#address-cells", 873 NULL); 874 newaddrsize = (tmp == NULL) ? 0 : *tmp; 875 876 DBG(" -> newintsize=%d, newaddrsize=%d\n", 877 newintsize, newaddrsize); 878 879 /* Check for malformed properties */ 880 if (imaplen < (newaddrsize + newintsize)) 881 goto fail; 882 883 imap += newaddrsize + newintsize; 884 imaplen -= newaddrsize + newintsize; 885 886 DBG(" -> imaplen=%d\n", imaplen); 887 } 888 if (!match) 889 goto fail; 890 891 of_node_put(old); 892 old = of_node_get(newpar); 893 addrsize = newaddrsize; 894 intsize = newintsize; 895 intspec = imap - intsize; 896 addr = intspec - addrsize; 897 898 skiplevel: 899 /* Iterate again with new parent */ 900 DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>"); 901 of_node_put(ipar); 902 ipar = newpar; 903 newpar = NULL; 904 } 905 fail: 906 of_node_put(ipar); 907 of_node_put(old); 908 of_node_put(newpar); 909 910 return -EINVAL; 911 } 912 EXPORT_SYMBOL_GPL(of_irq_map_raw); 913 914 #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) 915 static int of_irq_map_oldworld(struct device_node *device, int index, 916 struct of_irq *out_irq) 917 { 918 const u32 *ints; 919 int intlen; 920 921 /* 922 * Old machines just have a list of interrupt numbers 923 * and no interrupt-controller nodes. We also have dodgy 924 * cases where the APPL,interrupts property is completely 925 * missing behind pci-pci bridges and we have to get it 926 * from the parent (the bridge itself, as apple just wired 927 * everything together on these) 928 */ 929 while (device) { 930 ints = get_property(device, "AAPL,interrupts", &intlen); 931 if (ints != NULL) 932 break; 933 device = device->parent; 934 if (device && strcmp(device->type, "pci") != 0) 935 break; 936 } 937 if (ints == NULL) 938 return -EINVAL; 939 intlen /= sizeof(u32); 940 941 if (index >= intlen) 942 return -EINVAL; 943 944 out_irq->controller = NULL; 945 out_irq->specifier[0] = ints[index]; 946 out_irq->size = 1; 947 948 return 0; 949 } 950 #else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */ 951 static int of_irq_map_oldworld(struct device_node *device, int index, 952 struct of_irq *out_irq) 953 { 954 return -EINVAL; 955 } 956 #endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */ 957 958 int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq) 959 { 960 struct device_node *p; 961 const u32 *intspec, *tmp, *addr; 962 u32 intsize, intlen; 963 int res; 964 965 DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index); 966 967 /* OldWorld mac stuff is "special", handle out of line */ 968 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC) 969 return of_irq_map_oldworld(device, index, out_irq); 970 971 /* Get the interrupts property */ 972 intspec = get_property(device, "interrupts", &intlen); 973 if (intspec == NULL) 974 return -EINVAL; 975 intlen /= sizeof(u32); 976 977 /* Get the reg property (if any) */ 978 addr = get_property(device, "reg", NULL); 979 980 /* Look for the interrupt parent. */ 981 p = of_irq_find_parent(device); 982 if (p == NULL) 983 return -EINVAL; 984 985 /* Get size of interrupt specifier */ 986 tmp = get_property(p, "#interrupt-cells", NULL); 987 if (tmp == NULL) { 988 of_node_put(p); 989 return -EINVAL; 990 } 991 intsize = *tmp; 992 993 DBG(" intsize=%d intlen=%d\n", intsize, intlen); 994 995 /* Check index */ 996 if ((index + 1) * intsize > intlen) 997 return -EINVAL; 998 999 /* Get new specifier and map it */ 1000 res = of_irq_map_raw(p, intspec + index * intsize, intsize, 1001 addr, out_irq); 1002 of_node_put(p); 1003 return res; 1004 } 1005 EXPORT_SYMBOL_GPL(of_irq_map_one); 1006