1 // SPDX-License-Identifier: GPL-2.0 2 #define pr_fmt(fmt) "OF: " fmt 3 4 #include <linux/device.h> 5 #include <linux/fwnode.h> 6 #include <linux/io.h> 7 #include <linux/ioport.h> 8 #include <linux/logic_pio.h> 9 #include <linux/module.h> 10 #include <linux/of_address.h> 11 #include <linux/pci.h> 12 #include <linux/pci_regs.h> 13 #include <linux/sizes.h> 14 #include <linux/slab.h> 15 #include <linux/string.h> 16 #include <linux/dma-direct.h> /* for bus_dma_region */ 17 18 #include "of_private.h" 19 20 /* Max address size we deal with */ 21 #define OF_MAX_ADDR_CELLS 4 22 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) 23 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0) 24 25 static struct of_bus *of_match_bus(struct device_node *np); 26 static int __of_address_to_resource(struct device_node *dev, 27 const __be32 *addrp, u64 size, unsigned int flags, 28 const char *name, struct resource *r); 29 static bool of_mmio_is_nonposted(struct device_node *np); 30 31 /* Debug utility */ 32 #ifdef DEBUG 33 static void of_dump_addr(const char *s, const __be32 *addr, int na) 34 { 35 pr_debug("%s", s); 36 while (na--) 37 pr_cont(" %08x", be32_to_cpu(*(addr++))); 38 pr_cont("\n"); 39 } 40 #else 41 static void of_dump_addr(const char *s, const __be32 *addr, int na) { } 42 #endif 43 44 /* Callbacks for bus specific translators */ 45 struct of_bus { 46 const char *name; 47 const char *addresses; 48 int (*match)(struct device_node *parent); 49 void (*count_cells)(struct device_node *child, 50 int *addrc, int *sizec); 51 u64 (*map)(__be32 *addr, const __be32 *range, 52 int na, int ns, int pna); 53 int (*translate)(__be32 *addr, u64 offset, int na); 54 bool has_flags; 55 unsigned int (*get_flags)(const __be32 *addr); 56 }; 57 58 /* 59 * Default translator (generic bus) 60 */ 61 62 static void of_bus_default_count_cells(struct device_node *dev, 63 int *addrc, int *sizec) 64 { 65 if (addrc) 66 *addrc = of_n_addr_cells(dev); 67 if (sizec) 68 *sizec = of_n_size_cells(dev); 69 } 70 71 static u64 of_bus_default_map(__be32 *addr, const __be32 *range, 72 int na, int ns, int pna) 73 { 74 u64 cp, s, da; 75 76 cp = of_read_number(range, na); 77 s = of_read_number(range + na + pna, ns); 78 da = of_read_number(addr, na); 79 80 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", 81 (unsigned long long)cp, (unsigned long long)s, 82 (unsigned long long)da); 83 84 if (da < cp || da >= (cp + s)) 85 return OF_BAD_ADDR; 86 return da - cp; 87 } 88 89 static int of_bus_default_translate(__be32 *addr, u64 offset, int na) 90 { 91 u64 a = of_read_number(addr, na); 92 memset(addr, 0, na * 4); 93 a += offset; 94 if (na > 1) 95 addr[na - 2] = cpu_to_be32(a >> 32); 96 addr[na - 1] = cpu_to_be32(a & 0xffffffffu); 97 98 return 0; 99 } 100 101 static unsigned int of_bus_default_get_flags(const __be32 *addr) 102 { 103 return IORESOURCE_MEM; 104 } 105 106 #ifdef CONFIG_PCI 107 static unsigned int of_bus_pci_get_flags(const __be32 *addr) 108 { 109 unsigned int flags = 0; 110 u32 w = be32_to_cpup(addr); 111 112 if (!IS_ENABLED(CONFIG_PCI)) 113 return 0; 114 115 switch((w >> 24) & 0x03) { 116 case 0x01: 117 flags |= IORESOURCE_IO; 118 break; 119 case 0x02: /* 32 bits */ 120 case 0x03: /* 64 bits */ 121 flags |= IORESOURCE_MEM; 122 break; 123 } 124 if (w & 0x40000000) 125 flags |= IORESOURCE_PREFETCH; 126 return flags; 127 } 128 129 /* 130 * PCI bus specific translator 131 */ 132 133 static bool of_node_is_pcie(struct device_node *np) 134 { 135 bool is_pcie = of_node_name_eq(np, "pcie"); 136 137 if (is_pcie) 138 pr_warn_once("%pOF: Missing device_type\n", np); 139 140 return is_pcie; 141 } 142 143 static int of_bus_pci_match(struct device_node *np) 144 { 145 /* 146 * "pciex" is PCI Express 147 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs 148 * "ht" is hypertransport 149 * 150 * If none of the device_type match, and that the node name is 151 * "pcie", accept the device as PCI (with a warning). 152 */ 153 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") || 154 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") || 155 of_node_is_pcie(np); 156 } 157 158 static void of_bus_pci_count_cells(struct device_node *np, 159 int *addrc, int *sizec) 160 { 161 if (addrc) 162 *addrc = 3; 163 if (sizec) 164 *sizec = 2; 165 } 166 167 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, 168 int pna) 169 { 170 u64 cp, s, da; 171 unsigned int af, rf; 172 173 af = of_bus_pci_get_flags(addr); 174 rf = of_bus_pci_get_flags(range); 175 176 /* Check address type match */ 177 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) 178 return OF_BAD_ADDR; 179 180 /* Read address values, skipping high cell */ 181 cp = of_read_number(range + 1, na - 1); 182 s = of_read_number(range + na + pna, ns); 183 da = of_read_number(addr + 1, na - 1); 184 185 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n", 186 (unsigned long long)cp, (unsigned long long)s, 187 (unsigned long long)da); 188 189 if (da < cp || da >= (cp + s)) 190 return OF_BAD_ADDR; 191 return da - cp; 192 } 193 194 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) 195 { 196 return of_bus_default_translate(addr + 1, offset, na - 1); 197 } 198 199 const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, 200 unsigned int *flags) 201 { 202 const __be32 *prop; 203 unsigned int psize; 204 struct device_node *parent; 205 struct of_bus *bus; 206 int onesize, i, na, ns; 207 208 /* Get parent & match bus type */ 209 parent = of_get_parent(dev); 210 if (parent == NULL) 211 return NULL; 212 bus = of_match_bus(parent); 213 if (strcmp(bus->name, "pci")) { 214 of_node_put(parent); 215 return NULL; 216 } 217 bus->count_cells(dev, &na, &ns); 218 of_node_put(parent); 219 if (!OF_CHECK_ADDR_COUNT(na)) 220 return NULL; 221 222 /* Get "reg" or "assigned-addresses" property */ 223 prop = of_get_property(dev, bus->addresses, &psize); 224 if (prop == NULL) 225 return NULL; 226 psize /= 4; 227 228 onesize = na + ns; 229 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) { 230 u32 val = be32_to_cpu(prop[0]); 231 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { 232 if (size) 233 *size = of_read_number(prop + na, ns); 234 if (flags) 235 *flags = bus->get_flags(prop); 236 return prop; 237 } 238 } 239 return NULL; 240 } 241 EXPORT_SYMBOL(of_get_pci_address); 242 243 int of_pci_address_to_resource(struct device_node *dev, int bar, 244 struct resource *r) 245 { 246 const __be32 *addrp; 247 u64 size; 248 unsigned int flags; 249 250 addrp = of_get_pci_address(dev, bar, &size, &flags); 251 if (addrp == NULL) 252 return -EINVAL; 253 return __of_address_to_resource(dev, addrp, size, flags, NULL, r); 254 } 255 EXPORT_SYMBOL_GPL(of_pci_address_to_resource); 256 257 /* 258 * of_pci_range_to_resource - Create a resource from an of_pci_range 259 * @range: the PCI range that describes the resource 260 * @np: device node where the range belongs to 261 * @res: pointer to a valid resource that will be updated to 262 * reflect the values contained in the range. 263 * 264 * Returns EINVAL if the range cannot be converted to resource. 265 * 266 * Note that if the range is an IO range, the resource will be converted 267 * using pci_address_to_pio() which can fail if it is called too early or 268 * if the range cannot be matched to any host bridge IO space (our case here). 269 * To guard against that we try to register the IO range first. 270 * If that fails we know that pci_address_to_pio() will do too. 271 */ 272 int of_pci_range_to_resource(struct of_pci_range *range, 273 struct device_node *np, struct resource *res) 274 { 275 int err; 276 res->flags = range->flags; 277 res->parent = res->child = res->sibling = NULL; 278 res->name = np->full_name; 279 280 if (res->flags & IORESOURCE_IO) { 281 unsigned long port; 282 err = pci_register_io_range(&np->fwnode, range->cpu_addr, 283 range->size); 284 if (err) 285 goto invalid_range; 286 port = pci_address_to_pio(range->cpu_addr); 287 if (port == (unsigned long)-1) { 288 err = -EINVAL; 289 goto invalid_range; 290 } 291 res->start = port; 292 } else { 293 if ((sizeof(resource_size_t) < 8) && 294 upper_32_bits(range->cpu_addr)) { 295 err = -EINVAL; 296 goto invalid_range; 297 } 298 299 res->start = range->cpu_addr; 300 } 301 res->end = res->start + range->size - 1; 302 return 0; 303 304 invalid_range: 305 res->start = (resource_size_t)OF_BAD_ADDR; 306 res->end = (resource_size_t)OF_BAD_ADDR; 307 return err; 308 } 309 EXPORT_SYMBOL(of_pci_range_to_resource); 310 #endif /* CONFIG_PCI */ 311 312 /* 313 * ISA bus specific translator 314 */ 315 316 static int of_bus_isa_match(struct device_node *np) 317 { 318 return of_node_name_eq(np, "isa"); 319 } 320 321 static void of_bus_isa_count_cells(struct device_node *child, 322 int *addrc, int *sizec) 323 { 324 if (addrc) 325 *addrc = 2; 326 if (sizec) 327 *sizec = 1; 328 } 329 330 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns, 331 int pna) 332 { 333 u64 cp, s, da; 334 335 /* Check address type match */ 336 if ((addr[0] ^ range[0]) & cpu_to_be32(1)) 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 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n", 345 (unsigned long long)cp, (unsigned long long)s, 346 (unsigned long long)da); 347 348 if (da < cp || da >= (cp + s)) 349 return OF_BAD_ADDR; 350 return da - cp; 351 } 352 353 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na) 354 { 355 return of_bus_default_translate(addr + 1, offset, na - 1); 356 } 357 358 static unsigned int of_bus_isa_get_flags(const __be32 *addr) 359 { 360 unsigned int flags = 0; 361 u32 w = be32_to_cpup(addr); 362 363 if (w & 1) 364 flags |= IORESOURCE_IO; 365 else 366 flags |= IORESOURCE_MEM; 367 return flags; 368 } 369 370 /* 371 * Array of bus specific translators 372 */ 373 374 static struct of_bus of_busses[] = { 375 #ifdef CONFIG_PCI 376 /* PCI */ 377 { 378 .name = "pci", 379 .addresses = "assigned-addresses", 380 .match = of_bus_pci_match, 381 .count_cells = of_bus_pci_count_cells, 382 .map = of_bus_pci_map, 383 .translate = of_bus_pci_translate, 384 .has_flags = true, 385 .get_flags = of_bus_pci_get_flags, 386 }, 387 #endif /* CONFIG_PCI */ 388 /* ISA */ 389 { 390 .name = "isa", 391 .addresses = "reg", 392 .match = of_bus_isa_match, 393 .count_cells = of_bus_isa_count_cells, 394 .map = of_bus_isa_map, 395 .translate = of_bus_isa_translate, 396 .has_flags = true, 397 .get_flags = of_bus_isa_get_flags, 398 }, 399 /* Default */ 400 { 401 .name = "default", 402 .addresses = "reg", 403 .match = NULL, 404 .count_cells = of_bus_default_count_cells, 405 .map = of_bus_default_map, 406 .translate = of_bus_default_translate, 407 .get_flags = of_bus_default_get_flags, 408 }, 409 }; 410 411 static struct of_bus *of_match_bus(struct device_node *np) 412 { 413 int i; 414 415 for (i = 0; i < ARRAY_SIZE(of_busses); i++) 416 if (!of_busses[i].match || of_busses[i].match(np)) 417 return &of_busses[i]; 418 BUG(); 419 return NULL; 420 } 421 422 static int of_empty_ranges_quirk(struct device_node *np) 423 { 424 if (IS_ENABLED(CONFIG_PPC)) { 425 /* To save cycles, we cache the result for global "Mac" setting */ 426 static int quirk_state = -1; 427 428 /* PA-SEMI sdc DT bug */ 429 if (of_device_is_compatible(np, "1682m-sdc")) 430 return true; 431 432 /* Make quirk cached */ 433 if (quirk_state < 0) 434 quirk_state = 435 of_machine_is_compatible("Power Macintosh") || 436 of_machine_is_compatible("MacRISC"); 437 return quirk_state; 438 } 439 return false; 440 } 441 442 static int of_translate_one(struct device_node *parent, struct of_bus *bus, 443 struct of_bus *pbus, __be32 *addr, 444 int na, int ns, int pna, const char *rprop) 445 { 446 const __be32 *ranges; 447 unsigned int rlen; 448 int rone; 449 u64 offset = OF_BAD_ADDR; 450 451 /* 452 * Normally, an absence of a "ranges" property means we are 453 * crossing a non-translatable boundary, and thus the addresses 454 * below the current cannot be converted to CPU physical ones. 455 * Unfortunately, while this is very clear in the spec, it's not 456 * what Apple understood, and they do have things like /uni-n or 457 * /ht nodes with no "ranges" property and a lot of perfectly 458 * useable mapped devices below them. Thus we treat the absence of 459 * "ranges" as equivalent to an empty "ranges" property which means 460 * a 1:1 translation at that level. It's up to the caller not to try 461 * to translate addresses that aren't supposed to be translated in 462 * the first place. --BenH. 463 * 464 * As far as we know, this damage only exists on Apple machines, so 465 * This code is only enabled on powerpc. --gcl 466 * 467 * This quirk also applies for 'dma-ranges' which frequently exist in 468 * child nodes without 'dma-ranges' in the parent nodes. --RobH 469 */ 470 ranges = of_get_property(parent, rprop, &rlen); 471 if (ranges == NULL && !of_empty_ranges_quirk(parent) && 472 strcmp(rprop, "dma-ranges")) { 473 pr_debug("no ranges; cannot translate\n"); 474 return 1; 475 } 476 if (ranges == NULL || rlen == 0) { 477 offset = of_read_number(addr, na); 478 memset(addr, 0, pna * 4); 479 pr_debug("empty ranges; 1:1 translation\n"); 480 goto finish; 481 } 482 483 pr_debug("walking ranges...\n"); 484 485 /* Now walk through the ranges */ 486 rlen /= 4; 487 rone = na + pna + ns; 488 for (; rlen >= rone; rlen -= rone, ranges += rone) { 489 offset = bus->map(addr, ranges, na, ns, pna); 490 if (offset != OF_BAD_ADDR) 491 break; 492 } 493 if (offset == OF_BAD_ADDR) { 494 pr_debug("not found !\n"); 495 return 1; 496 } 497 memcpy(addr, ranges + na, 4 * pna); 498 499 finish: 500 of_dump_addr("parent translation for:", addr, pna); 501 pr_debug("with offset: %llx\n", (unsigned long long)offset); 502 503 /* Translate it into parent bus space */ 504 return pbus->translate(addr, offset, pna); 505 } 506 507 /* 508 * Translate an address from the device-tree into a CPU physical address, 509 * this walks up the tree and applies the various bus mappings on the 510 * way. 511 * 512 * Note: We consider that crossing any level with #size-cells == 0 to mean 513 * that translation is impossible (that is we are not dealing with a value 514 * that can be mapped to a cpu physical address). This is not really specified 515 * that way, but this is traditionally the way IBM at least do things 516 * 517 * Whenever the translation fails, the *host pointer will be set to the 518 * device that had registered logical PIO mapping, and the return code is 519 * relative to that node. 520 */ 521 static u64 __of_translate_address(struct device_node *dev, 522 struct device_node *(*get_parent)(const struct device_node *), 523 const __be32 *in_addr, const char *rprop, 524 struct device_node **host) 525 { 526 struct device_node *parent = NULL; 527 struct of_bus *bus, *pbus; 528 __be32 addr[OF_MAX_ADDR_CELLS]; 529 int na, ns, pna, pns; 530 u64 result = OF_BAD_ADDR; 531 532 pr_debug("** translation for device %pOF **\n", dev); 533 534 /* Increase refcount at current level */ 535 of_node_get(dev); 536 537 *host = NULL; 538 /* Get parent & match bus type */ 539 parent = get_parent(dev); 540 if (parent == NULL) 541 goto bail; 542 bus = of_match_bus(parent); 543 544 /* Count address cells & copy address locally */ 545 bus->count_cells(dev, &na, &ns); 546 if (!OF_CHECK_COUNTS(na, ns)) { 547 pr_debug("Bad cell count for %pOF\n", dev); 548 goto bail; 549 } 550 memcpy(addr, in_addr, na * 4); 551 552 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n", 553 bus->name, na, ns, parent); 554 of_dump_addr("translating address:", addr, na); 555 556 /* Translate */ 557 for (;;) { 558 struct logic_pio_hwaddr *iorange; 559 560 /* Switch to parent bus */ 561 of_node_put(dev); 562 dev = parent; 563 parent = get_parent(dev); 564 565 /* If root, we have finished */ 566 if (parent == NULL) { 567 pr_debug("reached root node\n"); 568 result = of_read_number(addr, na); 569 break; 570 } 571 572 /* 573 * For indirectIO device which has no ranges property, get 574 * the address from reg directly. 575 */ 576 iorange = find_io_range_by_fwnode(&dev->fwnode); 577 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) { 578 result = of_read_number(addr + 1, na - 1); 579 pr_debug("indirectIO matched(%pOF) 0x%llx\n", 580 dev, result); 581 *host = of_node_get(dev); 582 break; 583 } 584 585 /* Get new parent bus and counts */ 586 pbus = of_match_bus(parent); 587 pbus->count_cells(dev, &pna, &pns); 588 if (!OF_CHECK_COUNTS(pna, pns)) { 589 pr_err("Bad cell count for %pOF\n", dev); 590 break; 591 } 592 593 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n", 594 pbus->name, pna, pns, parent); 595 596 /* Apply bus translation */ 597 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) 598 break; 599 600 /* Complete the move up one level */ 601 na = pna; 602 ns = pns; 603 bus = pbus; 604 605 of_dump_addr("one level translation:", addr, na); 606 } 607 bail: 608 of_node_put(parent); 609 of_node_put(dev); 610 611 return result; 612 } 613 614 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr) 615 { 616 struct device_node *host; 617 u64 ret; 618 619 ret = __of_translate_address(dev, of_get_parent, 620 in_addr, "ranges", &host); 621 if (host) { 622 of_node_put(host); 623 return OF_BAD_ADDR; 624 } 625 626 return ret; 627 } 628 EXPORT_SYMBOL(of_translate_address); 629 630 static struct device_node *__of_get_dma_parent(const struct device_node *np) 631 { 632 struct of_phandle_args args; 633 int ret, index; 634 635 index = of_property_match_string(np, "interconnect-names", "dma-mem"); 636 if (index < 0) 637 return of_get_parent(np); 638 639 ret = of_parse_phandle_with_args(np, "interconnects", 640 "#interconnect-cells", 641 index, &args); 642 if (ret < 0) 643 return of_get_parent(np); 644 645 return of_node_get(args.np); 646 } 647 648 static struct device_node *of_get_next_dma_parent(struct device_node *np) 649 { 650 struct device_node *parent; 651 652 parent = __of_get_dma_parent(np); 653 of_node_put(np); 654 655 return parent; 656 } 657 658 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) 659 { 660 struct device_node *host; 661 u64 ret; 662 663 ret = __of_translate_address(dev, __of_get_dma_parent, 664 in_addr, "dma-ranges", &host); 665 666 if (host) { 667 of_node_put(host); 668 return OF_BAD_ADDR; 669 } 670 671 return ret; 672 } 673 EXPORT_SYMBOL(of_translate_dma_address); 674 675 const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, 676 unsigned int *flags) 677 { 678 const __be32 *prop; 679 unsigned int psize; 680 struct device_node *parent; 681 struct of_bus *bus; 682 int onesize, i, na, ns; 683 684 /* Get parent & match bus type */ 685 parent = of_get_parent(dev); 686 if (parent == NULL) 687 return NULL; 688 bus = of_match_bus(parent); 689 bus->count_cells(dev, &na, &ns); 690 of_node_put(parent); 691 if (!OF_CHECK_ADDR_COUNT(na)) 692 return NULL; 693 694 /* Get "reg" or "assigned-addresses" property */ 695 prop = of_get_property(dev, bus->addresses, &psize); 696 if (prop == NULL) 697 return NULL; 698 psize /= 4; 699 700 onesize = na + ns; 701 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) 702 if (i == index) { 703 if (size) 704 *size = of_read_number(prop + na, ns); 705 if (flags) 706 *flags = bus->get_flags(prop); 707 return prop; 708 } 709 return NULL; 710 } 711 EXPORT_SYMBOL(of_get_address); 712 713 static int parser_init(struct of_pci_range_parser *parser, 714 struct device_node *node, const char *name) 715 { 716 int rlen; 717 718 parser->node = node; 719 parser->pna = of_n_addr_cells(node); 720 parser->na = of_bus_n_addr_cells(node); 721 parser->ns = of_bus_n_size_cells(node); 722 parser->dma = !strcmp(name, "dma-ranges"); 723 parser->bus = of_match_bus(node); 724 725 parser->range = of_get_property(node, name, &rlen); 726 if (parser->range == NULL) 727 return -ENOENT; 728 729 parser->end = parser->range + rlen / sizeof(__be32); 730 731 return 0; 732 } 733 734 int of_pci_range_parser_init(struct of_pci_range_parser *parser, 735 struct device_node *node) 736 { 737 return parser_init(parser, node, "ranges"); 738 } 739 EXPORT_SYMBOL_GPL(of_pci_range_parser_init); 740 741 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser, 742 struct device_node *node) 743 { 744 return parser_init(parser, node, "dma-ranges"); 745 } 746 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init); 747 #define of_dma_range_parser_init of_pci_dma_range_parser_init 748 749 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, 750 struct of_pci_range *range) 751 { 752 int na = parser->na; 753 int ns = parser->ns; 754 int np = parser->pna + na + ns; 755 int busflag_na = 0; 756 757 if (!range) 758 return NULL; 759 760 if (!parser->range || parser->range + np > parser->end) 761 return NULL; 762 763 range->flags = parser->bus->get_flags(parser->range); 764 765 /* A extra cell for resource flags */ 766 if (parser->bus->has_flags) 767 busflag_na = 1; 768 769 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); 770 771 if (parser->dma) 772 range->cpu_addr = of_translate_dma_address(parser->node, 773 parser->range + na); 774 else 775 range->cpu_addr = of_translate_address(parser->node, 776 parser->range + na); 777 range->size = of_read_number(parser->range + parser->pna + na, ns); 778 779 parser->range += np; 780 781 /* Now consume following elements while they are contiguous */ 782 while (parser->range + np <= parser->end) { 783 u32 flags = 0; 784 u64 bus_addr, cpu_addr, size; 785 786 flags = parser->bus->get_flags(parser->range); 787 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); 788 if (parser->dma) 789 cpu_addr = of_translate_dma_address(parser->node, 790 parser->range + na); 791 else 792 cpu_addr = of_translate_address(parser->node, 793 parser->range + na); 794 size = of_read_number(parser->range + parser->pna + na, ns); 795 796 if (flags != range->flags) 797 break; 798 if (bus_addr != range->bus_addr + range->size || 799 cpu_addr != range->cpu_addr + range->size) 800 break; 801 802 range->size += size; 803 parser->range += np; 804 } 805 806 return range; 807 } 808 EXPORT_SYMBOL_GPL(of_pci_range_parser_one); 809 810 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr, 811 u64 size) 812 { 813 u64 taddr; 814 unsigned long port; 815 struct device_node *host; 816 817 taddr = __of_translate_address(dev, of_get_parent, 818 in_addr, "ranges", &host); 819 if (host) { 820 /* host-specific port access */ 821 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size); 822 of_node_put(host); 823 } else { 824 /* memory-mapped I/O range */ 825 port = pci_address_to_pio(taddr); 826 } 827 828 if (port == (unsigned long)-1) 829 return OF_BAD_ADDR; 830 831 return port; 832 } 833 834 static int __of_address_to_resource(struct device_node *dev, 835 const __be32 *addrp, u64 size, unsigned int flags, 836 const char *name, struct resource *r) 837 { 838 u64 taddr; 839 840 if (flags & IORESOURCE_MEM) 841 taddr = of_translate_address(dev, addrp); 842 else if (flags & IORESOURCE_IO) 843 taddr = of_translate_ioport(dev, addrp, size); 844 else 845 return -EINVAL; 846 847 if (taddr == OF_BAD_ADDR) 848 return -EINVAL; 849 memset(r, 0, sizeof(struct resource)); 850 851 if (of_mmio_is_nonposted(dev)) 852 flags |= IORESOURCE_MEM_NONPOSTED; 853 854 r->start = taddr; 855 r->end = taddr + size - 1; 856 r->flags = flags; 857 r->name = name ? name : dev->full_name; 858 859 return 0; 860 } 861 862 /** 863 * of_address_to_resource - Translate device tree address and return as resource 864 * 865 * Note that if your address is a PIO address, the conversion will fail if 866 * the physical address can't be internally converted to an IO token with 867 * pci_address_to_pio(), that is because it's either called too early or it 868 * can't be matched to any host bridge IO space 869 */ 870 int of_address_to_resource(struct device_node *dev, int index, 871 struct resource *r) 872 { 873 const __be32 *addrp; 874 u64 size; 875 unsigned int flags; 876 const char *name = NULL; 877 878 addrp = of_get_address(dev, index, &size, &flags); 879 if (addrp == NULL) 880 return -EINVAL; 881 882 /* Get optional "reg-names" property to add a name to a resource */ 883 of_property_read_string_index(dev, "reg-names", index, &name); 884 885 return __of_address_to_resource(dev, addrp, size, flags, name, r); 886 } 887 EXPORT_SYMBOL_GPL(of_address_to_resource); 888 889 /** 890 * of_iomap - Maps the memory mapped IO for a given device_node 891 * @np: the device whose io range will be mapped 892 * @index: index of the io range 893 * 894 * Returns a pointer to the mapped memory 895 */ 896 void __iomem *of_iomap(struct device_node *np, int index) 897 { 898 struct resource res; 899 900 if (of_address_to_resource(np, index, &res)) 901 return NULL; 902 903 if (res.flags & IORESOURCE_MEM_NONPOSTED) 904 return ioremap_np(res.start, resource_size(&res)); 905 else 906 return ioremap(res.start, resource_size(&res)); 907 } 908 EXPORT_SYMBOL(of_iomap); 909 910 /* 911 * of_io_request_and_map - Requests a resource and maps the memory mapped IO 912 * for a given device_node 913 * @device: the device whose io range will be mapped 914 * @index: index of the io range 915 * @name: name "override" for the memory region request or NULL 916 * 917 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded 918 * error code on failure. Usage example: 919 * 920 * base = of_io_request_and_map(node, 0, "foo"); 921 * if (IS_ERR(base)) 922 * return PTR_ERR(base); 923 */ 924 void __iomem *of_io_request_and_map(struct device_node *np, int index, 925 const char *name) 926 { 927 struct resource res; 928 void __iomem *mem; 929 930 if (of_address_to_resource(np, index, &res)) 931 return IOMEM_ERR_PTR(-EINVAL); 932 933 if (!name) 934 name = res.name; 935 if (!request_mem_region(res.start, resource_size(&res), name)) 936 return IOMEM_ERR_PTR(-EBUSY); 937 938 if (res.flags & IORESOURCE_MEM_NONPOSTED) 939 mem = ioremap_np(res.start, resource_size(&res)); 940 else 941 mem = ioremap(res.start, resource_size(&res)); 942 943 if (!mem) { 944 release_mem_region(res.start, resource_size(&res)); 945 return IOMEM_ERR_PTR(-ENOMEM); 946 } 947 948 return mem; 949 } 950 EXPORT_SYMBOL(of_io_request_and_map); 951 952 #ifdef CONFIG_HAS_DMA 953 /** 954 * of_dma_get_range - Get DMA range info and put it into a map array 955 * @np: device node to get DMA range info 956 * @map: dma range structure to return 957 * 958 * Look in bottom up direction for the first "dma-ranges" property 959 * and parse it. Put the information into a DMA offset map array. 960 * 961 * dma-ranges format: 962 * DMA addr (dma_addr) : naddr cells 963 * CPU addr (phys_addr_t) : pna cells 964 * size : nsize cells 965 * 966 * It returns -ENODEV if "dma-ranges" property was not found for this 967 * device in the DT. 968 */ 969 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map) 970 { 971 struct device_node *node = of_node_get(np); 972 const __be32 *ranges = NULL; 973 bool found_dma_ranges = false; 974 struct of_range_parser parser; 975 struct of_range range; 976 struct bus_dma_region *r; 977 int len, num_ranges = 0; 978 int ret = 0; 979 980 while (node) { 981 ranges = of_get_property(node, "dma-ranges", &len); 982 983 /* Ignore empty ranges, they imply no translation required */ 984 if (ranges && len > 0) 985 break; 986 987 /* Once we find 'dma-ranges', then a missing one is an error */ 988 if (found_dma_ranges && !ranges) { 989 ret = -ENODEV; 990 goto out; 991 } 992 found_dma_ranges = true; 993 994 node = of_get_next_dma_parent(node); 995 } 996 997 if (!node || !ranges) { 998 pr_debug("no dma-ranges found for node(%pOF)\n", np); 999 ret = -ENODEV; 1000 goto out; 1001 } 1002 1003 of_dma_range_parser_init(&parser, node); 1004 for_each_of_range(&parser, &range) 1005 num_ranges++; 1006 1007 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL); 1008 if (!r) { 1009 ret = -ENOMEM; 1010 goto out; 1011 } 1012 1013 /* 1014 * Record all info in the generic DMA ranges array for struct device. 1015 */ 1016 *map = r; 1017 of_dma_range_parser_init(&parser, node); 1018 for_each_of_range(&parser, &range) { 1019 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", 1020 range.bus_addr, range.cpu_addr, range.size); 1021 if (range.cpu_addr == OF_BAD_ADDR) { 1022 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n", 1023 range.bus_addr, node); 1024 continue; 1025 } 1026 r->cpu_start = range.cpu_addr; 1027 r->dma_start = range.bus_addr; 1028 r->size = range.size; 1029 r->offset = range.cpu_addr - range.bus_addr; 1030 r++; 1031 } 1032 out: 1033 of_node_put(node); 1034 return ret; 1035 } 1036 #endif /* CONFIG_HAS_DMA */ 1037 1038 /** 1039 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA 1040 * @np: The node to start searching from or NULL to start from the root 1041 * 1042 * Gets the highest CPU physical address that is addressable by all DMA masters 1043 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no 1044 * DMA constrained device is found, it returns PHYS_ADDR_MAX. 1045 */ 1046 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np) 1047 { 1048 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX; 1049 struct of_range_parser parser; 1050 phys_addr_t subtree_max_addr; 1051 struct device_node *child; 1052 struct of_range range; 1053 const __be32 *ranges; 1054 u64 cpu_end = 0; 1055 int len; 1056 1057 if (!np) 1058 np = of_root; 1059 1060 ranges = of_get_property(np, "dma-ranges", &len); 1061 if (ranges && len) { 1062 of_dma_range_parser_init(&parser, np); 1063 for_each_of_range(&parser, &range) 1064 if (range.cpu_addr + range.size > cpu_end) 1065 cpu_end = range.cpu_addr + range.size - 1; 1066 1067 if (max_cpu_addr > cpu_end) 1068 max_cpu_addr = cpu_end; 1069 } 1070 1071 for_each_available_child_of_node(np, child) { 1072 subtree_max_addr = of_dma_get_max_cpu_address(child); 1073 if (max_cpu_addr > subtree_max_addr) 1074 max_cpu_addr = subtree_max_addr; 1075 } 1076 1077 return max_cpu_addr; 1078 } 1079 1080 /** 1081 * of_dma_is_coherent - Check if device is coherent 1082 * @np: device node 1083 * 1084 * It returns true if "dma-coherent" property was found 1085 * for this device in the DT, or if DMA is coherent by 1086 * default for OF devices on the current platform. 1087 */ 1088 bool of_dma_is_coherent(struct device_node *np) 1089 { 1090 struct device_node *node; 1091 1092 if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT)) 1093 return true; 1094 1095 node = of_node_get(np); 1096 1097 while (node) { 1098 if (of_property_read_bool(node, "dma-coherent")) { 1099 of_node_put(node); 1100 return true; 1101 } 1102 node = of_get_next_dma_parent(node); 1103 } 1104 of_node_put(node); 1105 return false; 1106 } 1107 EXPORT_SYMBOL_GPL(of_dma_is_coherent); 1108 1109 /** 1110 * of_mmio_is_nonposted - Check if device uses non-posted MMIO 1111 * @np: device node 1112 * 1113 * Returns true if the "nonposted-mmio" property was found for 1114 * the device's bus. 1115 * 1116 * This is currently only enabled on builds that support Apple ARM devices, as 1117 * an optimization. 1118 */ 1119 static bool of_mmio_is_nonposted(struct device_node *np) 1120 { 1121 struct device_node *parent; 1122 bool nonposted; 1123 1124 if (!IS_ENABLED(CONFIG_ARCH_APPLE)) 1125 return false; 1126 1127 parent = of_get_parent(np); 1128 if (!parent) 1129 return false; 1130 1131 nonposted = of_property_read_bool(parent, "nonposted-mmio"); 1132 1133 of_node_put(parent); 1134 return nonposted; 1135 } 1136