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