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 /* 344 * Array of bus specific translators 345 */ 346 347 static const struct of_bus of_busses[] = { 348 #ifdef CONFIG_PCI 349 /* PCI */ 350 { 351 .name = "pci", 352 .addresses = "assigned-addresses", 353 .match = of_bus_pci_match, 354 .count_cells = of_bus_pci_count_cells, 355 .map = of_bus_pci_map, 356 .translate = of_bus_default_flags_translate, 357 .flag_cells = 1, 358 .get_flags = of_bus_pci_get_flags, 359 }, 360 #endif /* CONFIG_PCI */ 361 /* ISA */ 362 { 363 .name = "isa", 364 .addresses = "reg", 365 .match = of_bus_isa_match, 366 .count_cells = of_bus_isa_count_cells, 367 .map = of_bus_isa_map, 368 .translate = of_bus_default_flags_translate, 369 .flag_cells = 1, 370 .get_flags = of_bus_isa_get_flags, 371 }, 372 /* Default with flags cell */ 373 { 374 .name = "default-flags", 375 .addresses = "reg", 376 .match = of_bus_default_flags_match, 377 .count_cells = of_bus_default_count_cells, 378 .map = of_bus_default_flags_map, 379 .translate = of_bus_default_flags_translate, 380 .flag_cells = 1, 381 .get_flags = of_bus_default_flags_get_flags, 382 }, 383 /* Default */ 384 { 385 .name = "default", 386 .addresses = "reg", 387 .match = NULL, 388 .count_cells = of_bus_default_count_cells, 389 .map = of_bus_default_map, 390 .translate = of_bus_default_translate, 391 .get_flags = of_bus_default_get_flags, 392 }, 393 }; 394 395 static const struct of_bus *of_match_bus(struct device_node *np) 396 { 397 int i; 398 399 for (i = 0; i < ARRAY_SIZE(of_busses); i++) 400 if (!of_busses[i].match || of_busses[i].match(np)) 401 return &of_busses[i]; 402 BUG(); 403 return NULL; 404 } 405 406 static int of_empty_ranges_quirk(const struct device_node *np) 407 { 408 if (IS_ENABLED(CONFIG_PPC)) { 409 /* To save cycles, we cache the result for global "Mac" setting */ 410 static int quirk_state = -1; 411 412 /* PA-SEMI sdc DT bug */ 413 if (of_device_is_compatible(np, "1682m-sdc")) 414 return true; 415 416 /* Make quirk cached */ 417 if (quirk_state < 0) 418 quirk_state = 419 of_machine_is_compatible("Power Macintosh") || 420 of_machine_is_compatible("MacRISC"); 421 return quirk_state; 422 } 423 return false; 424 } 425 426 static int of_translate_one(const struct device_node *parent, const struct of_bus *bus, 427 const struct of_bus *pbus, __be32 *addr, 428 int na, int ns, int pna, const char *rprop) 429 { 430 const __be32 *ranges; 431 unsigned int rlen; 432 int rone; 433 u64 offset = OF_BAD_ADDR; 434 435 /* 436 * Normally, an absence of a "ranges" property means we are 437 * crossing a non-translatable boundary, and thus the addresses 438 * below the current cannot be converted to CPU physical ones. 439 * Unfortunately, while this is very clear in the spec, it's not 440 * what Apple understood, and they do have things like /uni-n or 441 * /ht nodes with no "ranges" property and a lot of perfectly 442 * useable mapped devices below them. Thus we treat the absence of 443 * "ranges" as equivalent to an empty "ranges" property which means 444 * a 1:1 translation at that level. It's up to the caller not to try 445 * to translate addresses that aren't supposed to be translated in 446 * the first place. --BenH. 447 * 448 * As far as we know, this damage only exists on Apple machines, so 449 * This code is only enabled on powerpc. --gcl 450 * 451 * This quirk also applies for 'dma-ranges' which frequently exist in 452 * child nodes without 'dma-ranges' in the parent nodes. --RobH 453 */ 454 ranges = of_get_property(parent, rprop, &rlen); 455 if (ranges == NULL && !of_empty_ranges_quirk(parent) && 456 strcmp(rprop, "dma-ranges")) { 457 pr_debug("no ranges; cannot translate\n"); 458 return 1; 459 } 460 if (ranges == NULL || rlen == 0) { 461 offset = of_read_number(addr, na); 462 /* set address to zero, pass flags through */ 463 memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4); 464 pr_debug("empty ranges; 1:1 translation\n"); 465 goto finish; 466 } 467 468 pr_debug("walking ranges...\n"); 469 470 /* Now walk through the ranges */ 471 rlen /= 4; 472 rone = na + pna + ns; 473 for (; rlen >= rone; rlen -= rone, ranges += rone) { 474 offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells); 475 if (offset != OF_BAD_ADDR) 476 break; 477 } 478 if (offset == OF_BAD_ADDR) { 479 pr_debug("not found !\n"); 480 return 1; 481 } 482 memcpy(addr, ranges + na, 4 * pna); 483 484 finish: 485 of_dump_addr("parent translation for:", addr, pna); 486 pr_debug("with offset: %llx\n", offset); 487 488 /* Translate it into parent bus space */ 489 return pbus->translate(addr, offset, pna); 490 } 491 492 /* 493 * Translate an address from the device-tree into a CPU physical address, 494 * this walks up the tree and applies the various bus mappings on the 495 * way. 496 * 497 * Note: We consider that crossing any level with #size-cells == 0 to mean 498 * that translation is impossible (that is we are not dealing with a value 499 * that can be mapped to a cpu physical address). This is not really specified 500 * that way, but this is traditionally the way IBM at least do things 501 * 502 * Whenever the translation fails, the *host pointer will be set to the 503 * device that had registered logical PIO mapping, and the return code is 504 * relative to that node. 505 */ 506 static u64 __of_translate_address(struct device_node *node, 507 struct device_node *(*get_parent)(const struct device_node *), 508 const __be32 *in_addr, const char *rprop, 509 struct device_node **host) 510 { 511 struct device_node *dev __free(device_node) = of_node_get(node); 512 struct device_node *parent __free(device_node) = get_parent(dev); 513 const struct of_bus *bus, *pbus; 514 __be32 addr[OF_MAX_ADDR_CELLS]; 515 int na, ns, pna, pns; 516 517 pr_debug("** translation for device %pOF **\n", dev); 518 519 *host = NULL; 520 521 if (parent == NULL) 522 return OF_BAD_ADDR; 523 bus = of_match_bus(parent); 524 525 /* Count address cells & copy address locally */ 526 bus->count_cells(dev, &na, &ns); 527 if (!OF_CHECK_COUNTS(na, ns)) { 528 pr_debug("Bad cell count for %pOF\n", dev); 529 return OF_BAD_ADDR; 530 } 531 memcpy(addr, in_addr, na * 4); 532 533 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n", 534 bus->name, na, ns, parent); 535 of_dump_addr("translating address:", addr, na); 536 537 /* Translate */ 538 for (;;) { 539 struct logic_pio_hwaddr *iorange; 540 541 /* Switch to parent bus */ 542 of_node_put(dev); 543 dev = parent; 544 parent = get_parent(dev); 545 546 /* If root, we have finished */ 547 if (parent == NULL) { 548 pr_debug("reached root node\n"); 549 return of_read_number(addr, na); 550 } 551 552 /* 553 * For indirectIO device which has no ranges property, get 554 * the address from reg directly. 555 */ 556 iorange = find_io_range_by_fwnode(&dev->fwnode); 557 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) { 558 u64 result = of_read_number(addr + 1, na - 1); 559 pr_debug("indirectIO matched(%pOF) 0x%llx\n", 560 dev, result); 561 *host = no_free_ptr(dev); 562 return result; 563 } 564 565 /* Get new parent bus and counts */ 566 pbus = of_match_bus(parent); 567 pbus->count_cells(dev, &pna, &pns); 568 if (!OF_CHECK_COUNTS(pna, pns)) { 569 pr_err("Bad cell count for %pOF\n", dev); 570 return OF_BAD_ADDR; 571 } 572 573 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n", 574 pbus->name, pna, pns, parent); 575 576 /* Apply bus translation */ 577 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) 578 return OF_BAD_ADDR; 579 580 /* Complete the move up one level */ 581 na = pna; 582 ns = pns; 583 bus = pbus; 584 585 of_dump_addr("one level translation:", addr, na); 586 } 587 588 unreachable(); 589 } 590 591 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr) 592 { 593 struct device_node *host; 594 u64 ret; 595 596 ret = __of_translate_address(dev, of_get_parent, 597 in_addr, "ranges", &host); 598 if (host) { 599 of_node_put(host); 600 return OF_BAD_ADDR; 601 } 602 603 return ret; 604 } 605 EXPORT_SYMBOL(of_translate_address); 606 607 #ifdef CONFIG_HAS_DMA 608 struct device_node *__of_get_dma_parent(const struct device_node *np) 609 { 610 struct of_phandle_args args; 611 int ret, index; 612 613 index = of_property_match_string(np, "interconnect-names", "dma-mem"); 614 if (index < 0) 615 return of_get_parent(np); 616 617 ret = of_parse_phandle_with_args(np, "interconnects", 618 "#interconnect-cells", 619 index, &args); 620 if (ret < 0) 621 return of_get_parent(np); 622 623 return args.np; 624 } 625 #endif 626 627 static struct device_node *of_get_next_dma_parent(struct device_node *np) 628 { 629 struct device_node *parent; 630 631 parent = __of_get_dma_parent(np); 632 of_node_put(np); 633 634 return parent; 635 } 636 637 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) 638 { 639 struct device_node *host; 640 u64 ret; 641 642 ret = __of_translate_address(dev, __of_get_dma_parent, 643 in_addr, "dma-ranges", &host); 644 645 if (host) { 646 of_node_put(host); 647 return OF_BAD_ADDR; 648 } 649 650 return ret; 651 } 652 EXPORT_SYMBOL(of_translate_dma_address); 653 654 /** 655 * of_translate_dma_region - Translate device tree address and size tuple 656 * @dev: device tree node for which to translate 657 * @prop: pointer into array of cells 658 * @start: return value for the start of the DMA range 659 * @length: return value for the length of the DMA range 660 * 661 * Returns a pointer to the cell immediately following the translated DMA region. 662 */ 663 const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop, 664 phys_addr_t *start, size_t *length) 665 { 666 struct device_node *parent __free(device_node) = __of_get_dma_parent(dev); 667 u64 address, size; 668 int na, ns; 669 670 if (!parent) 671 return NULL; 672 673 na = of_bus_n_addr_cells(parent); 674 ns = of_bus_n_size_cells(parent); 675 676 address = of_translate_dma_address(dev, prop); 677 if (address == OF_BAD_ADDR) 678 return NULL; 679 680 size = of_read_number(prop + na, ns); 681 682 if (start) 683 *start = address; 684 685 if (length) 686 *length = size; 687 688 return prop + na + ns; 689 } 690 EXPORT_SYMBOL(of_translate_dma_region); 691 692 const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no, 693 u64 *size, unsigned int *flags) 694 { 695 const __be32 *prop; 696 unsigned int psize; 697 struct device_node *parent __free(device_node) = of_get_parent(dev); 698 const struct of_bus *bus; 699 int onesize, i, na, ns; 700 701 if (parent == NULL) 702 return NULL; 703 704 /* match the parent's bus type */ 705 bus = of_match_bus(parent); 706 if (strcmp(bus->name, "pci") && (bar_no >= 0)) 707 return NULL; 708 709 /* Get "reg" or "assigned-addresses" property */ 710 prop = of_get_property(dev, bus->addresses, &psize); 711 if (prop == NULL) 712 return NULL; 713 psize /= 4; 714 715 bus->count_cells(dev, &na, &ns); 716 if (!OF_CHECK_ADDR_COUNT(na)) 717 return NULL; 718 719 onesize = na + ns; 720 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) { 721 u32 val = be32_to_cpu(prop[0]); 722 /* PCI bus matches on BAR number instead of index */ 723 if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) || 724 ((index >= 0) && (i == index))) { 725 if (size) 726 *size = of_read_number(prop + na, ns); 727 if (flags) 728 *flags = bus->get_flags(prop); 729 return prop; 730 } 731 } 732 return NULL; 733 } 734 EXPORT_SYMBOL(__of_get_address); 735 736 /** 737 * of_property_read_reg - Retrieve the specified "reg" entry index without translating 738 * @np: device tree node for which to retrieve "reg" from 739 * @idx: "reg" entry index to read 740 * @addr: return value for the untranslated address 741 * @size: return value for the entry size 742 * 743 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and 744 * size values filled in. 745 */ 746 int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size) 747 { 748 const __be32 *prop = of_get_address(np, idx, size, NULL); 749 750 if (!prop) 751 return -EINVAL; 752 753 *addr = of_read_number(prop, of_n_addr_cells(np)); 754 755 return 0; 756 } 757 EXPORT_SYMBOL(of_property_read_reg); 758 759 static int parser_init(struct of_pci_range_parser *parser, 760 struct device_node *node, const char *name) 761 { 762 int rlen; 763 764 parser->node = node; 765 parser->pna = of_n_addr_cells(node); 766 parser->na = of_bus_n_addr_cells(node); 767 parser->ns = of_bus_n_size_cells(node); 768 parser->dma = !strcmp(name, "dma-ranges"); 769 parser->bus = of_match_bus(node); 770 771 parser->range = of_get_property(node, name, &rlen); 772 if (parser->range == NULL) 773 return -ENOENT; 774 775 parser->end = parser->range + rlen / sizeof(__be32); 776 777 return 0; 778 } 779 780 int of_pci_range_parser_init(struct of_pci_range_parser *parser, 781 struct device_node *node) 782 { 783 return parser_init(parser, node, "ranges"); 784 } 785 EXPORT_SYMBOL_GPL(of_pci_range_parser_init); 786 787 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser, 788 struct device_node *node) 789 { 790 return parser_init(parser, node, "dma-ranges"); 791 } 792 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init); 793 #define of_dma_range_parser_init of_pci_dma_range_parser_init 794 795 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, 796 struct of_pci_range *range) 797 { 798 int na = parser->na; 799 int ns = parser->ns; 800 int np = parser->pna + na + ns; 801 int busflag_na = parser->bus->flag_cells; 802 803 if (!range) 804 return NULL; 805 806 if (!parser->range || parser->range + np > parser->end) 807 return NULL; 808 809 range->flags = parser->bus->get_flags(parser->range); 810 811 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); 812 813 if (parser->dma) 814 range->cpu_addr = of_translate_dma_address(parser->node, 815 parser->range + na); 816 else 817 range->cpu_addr = of_translate_address(parser->node, 818 parser->range + na); 819 range->size = of_read_number(parser->range + parser->pna + na, ns); 820 821 parser->range += np; 822 823 /* Now consume following elements while they are contiguous */ 824 while (parser->range + np <= parser->end) { 825 u32 flags = 0; 826 u64 bus_addr, cpu_addr, size; 827 828 flags = parser->bus->get_flags(parser->range); 829 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); 830 if (parser->dma) 831 cpu_addr = of_translate_dma_address(parser->node, 832 parser->range + na); 833 else 834 cpu_addr = of_translate_address(parser->node, 835 parser->range + na); 836 size = of_read_number(parser->range + parser->pna + na, ns); 837 838 if (flags != range->flags) 839 break; 840 if (bus_addr != range->bus_addr + range->size || 841 cpu_addr != range->cpu_addr + range->size) 842 break; 843 844 range->size += size; 845 parser->range += np; 846 } 847 848 return range; 849 } 850 EXPORT_SYMBOL_GPL(of_pci_range_parser_one); 851 852 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr, 853 u64 size) 854 { 855 u64 taddr; 856 unsigned long port; 857 struct device_node *host; 858 859 taddr = __of_translate_address(dev, of_get_parent, 860 in_addr, "ranges", &host); 861 if (host) { 862 /* host-specific port access */ 863 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size); 864 of_node_put(host); 865 } else { 866 /* memory-mapped I/O range */ 867 port = pci_address_to_pio(taddr); 868 } 869 870 if (port == (unsigned long)-1) 871 return OF_BAD_ADDR; 872 873 return port; 874 } 875 876 #ifdef CONFIG_HAS_DMA 877 /** 878 * of_dma_get_range - Get DMA range info and put it into a map array 879 * @np: device node to get DMA range info 880 * @map: dma range structure to return 881 * 882 * Look in bottom up direction for the first "dma-ranges" property 883 * and parse it. Put the information into a DMA offset map array. 884 * 885 * dma-ranges format: 886 * DMA addr (dma_addr) : naddr cells 887 * CPU addr (phys_addr_t) : pna cells 888 * size : nsize cells 889 * 890 * It returns -ENODEV if "dma-ranges" property was not found for this 891 * device in the DT. 892 */ 893 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map) 894 { 895 struct device_node *node __free(device_node) = of_node_get(np); 896 const __be32 *ranges = NULL; 897 bool found_dma_ranges = false; 898 struct of_range_parser parser; 899 struct of_range range; 900 struct bus_dma_region *r; 901 int len, num_ranges = 0; 902 903 while (node) { 904 ranges = of_get_property(node, "dma-ranges", &len); 905 906 /* Ignore empty ranges, they imply no translation required */ 907 if (ranges && len > 0) 908 break; 909 910 /* Once we find 'dma-ranges', then a missing one is an error */ 911 if (found_dma_ranges && !ranges) 912 return -ENODEV; 913 914 found_dma_ranges = true; 915 916 node = of_get_next_dma_parent(node); 917 } 918 919 if (!node || !ranges) { 920 pr_debug("no dma-ranges found for node(%pOF)\n", np); 921 return -ENODEV; 922 } 923 of_dma_range_parser_init(&parser, node); 924 for_each_of_range(&parser, &range) { 925 if (range.cpu_addr == OF_BAD_ADDR) { 926 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n", 927 range.bus_addr, node); 928 continue; 929 } 930 num_ranges++; 931 } 932 933 if (!num_ranges) 934 return -EINVAL; 935 936 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL); 937 if (!r) 938 return -ENOMEM; 939 940 /* 941 * Record all info in the generic DMA ranges array for struct device, 942 * returning an error if we don't find any parsable ranges. 943 */ 944 *map = r; 945 of_dma_range_parser_init(&parser, node); 946 for_each_of_range(&parser, &range) { 947 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", 948 range.bus_addr, range.cpu_addr, range.size); 949 if (range.cpu_addr == OF_BAD_ADDR) 950 continue; 951 r->cpu_start = range.cpu_addr; 952 r->dma_start = range.bus_addr; 953 r->size = range.size; 954 r++; 955 } 956 return 0; 957 } 958 #endif /* CONFIG_HAS_DMA */ 959 960 /** 961 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA 962 * @np: The node to start searching from or NULL to start from the root 963 * 964 * Gets the highest CPU physical address that is addressable by all DMA masters 965 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no 966 * DMA constrained device is found, it returns PHYS_ADDR_MAX. 967 */ 968 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np) 969 { 970 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX; 971 struct of_range_parser parser; 972 phys_addr_t subtree_max_addr; 973 struct device_node *child; 974 struct of_range range; 975 const __be32 *ranges; 976 u64 cpu_end = 0; 977 int len; 978 979 if (!np) 980 np = of_root; 981 982 ranges = of_get_property(np, "dma-ranges", &len); 983 if (ranges && len) { 984 of_dma_range_parser_init(&parser, np); 985 for_each_of_range(&parser, &range) 986 if (range.cpu_addr + range.size > cpu_end) 987 cpu_end = range.cpu_addr + range.size - 1; 988 989 if (max_cpu_addr > cpu_end) 990 max_cpu_addr = cpu_end; 991 } 992 993 for_each_available_child_of_node(np, child) { 994 subtree_max_addr = of_dma_get_max_cpu_address(child); 995 if (max_cpu_addr > subtree_max_addr) 996 max_cpu_addr = subtree_max_addr; 997 } 998 999 return max_cpu_addr; 1000 } 1001 1002 /** 1003 * of_dma_is_coherent - Check if device is coherent 1004 * @np: device node 1005 * 1006 * It returns true if "dma-coherent" property was found 1007 * for this device in the DT, or if DMA is coherent by 1008 * default for OF devices on the current platform and no 1009 * "dma-noncoherent" property was found for this device. 1010 */ 1011 bool of_dma_is_coherent(struct device_node *np) 1012 { 1013 struct device_node *node __free(device_node) = of_node_get(np); 1014 1015 while (node) { 1016 if (of_property_read_bool(node, "dma-coherent")) 1017 return true; 1018 1019 if (of_property_read_bool(node, "dma-noncoherent")) 1020 return false; 1021 1022 node = of_get_next_dma_parent(node); 1023 } 1024 return dma_default_coherent; 1025 } 1026 EXPORT_SYMBOL_GPL(of_dma_is_coherent); 1027 1028 /** 1029 * of_mmio_is_nonposted - Check if device uses non-posted MMIO 1030 * @np: device node 1031 * 1032 * Returns true if the "nonposted-mmio" property was found for 1033 * the device's bus. 1034 * 1035 * This is currently only enabled on builds that support Apple ARM devices, as 1036 * an optimization. 1037 */ 1038 static bool of_mmio_is_nonposted(const struct device_node *np) 1039 { 1040 if (!IS_ENABLED(CONFIG_ARCH_APPLE)) 1041 return false; 1042 1043 struct device_node *parent __free(device_node) = of_get_parent(np); 1044 if (!parent) 1045 return false; 1046 1047 return of_property_read_bool(parent, "nonposted-mmio"); 1048 } 1049 1050 static int __of_address_to_resource(struct device_node *dev, int index, int bar_no, 1051 struct resource *r) 1052 { 1053 u64 taddr; 1054 const __be32 *addrp; 1055 u64 size; 1056 unsigned int flags; 1057 const char *name = NULL; 1058 1059 addrp = __of_get_address(dev, index, bar_no, &size, &flags); 1060 if (addrp == NULL) 1061 return -EINVAL; 1062 1063 /* Get optional "reg-names" property to add a name to a resource */ 1064 if (index >= 0) 1065 of_property_read_string_index(dev, "reg-names", index, &name); 1066 1067 if (flags & IORESOURCE_MEM) 1068 taddr = of_translate_address(dev, addrp); 1069 else if (flags & IORESOURCE_IO) 1070 taddr = of_translate_ioport(dev, addrp, size); 1071 else 1072 return -EINVAL; 1073 1074 if (taddr == OF_BAD_ADDR) 1075 return -EINVAL; 1076 memset(r, 0, sizeof(struct resource)); 1077 1078 if (of_mmio_is_nonposted(dev)) 1079 flags |= IORESOURCE_MEM_NONPOSTED; 1080 1081 r->flags = flags; 1082 r->name = name ? name : dev->full_name; 1083 1084 return __of_address_resource_bounds(r, taddr, size); 1085 } 1086 1087 /** 1088 * of_address_to_resource - Translate device tree address and return as resource 1089 * @dev: Caller's Device Node 1090 * @index: Index into the array 1091 * @r: Pointer to resource array 1092 * 1093 * Returns -EINVAL if the range cannot be converted to resource. 1094 * 1095 * Note that if your address is a PIO address, the conversion will fail if 1096 * the physical address can't be internally converted to an IO token with 1097 * pci_address_to_pio(), that is because it's either called too early or it 1098 * can't be matched to any host bridge IO space 1099 */ 1100 int of_address_to_resource(struct device_node *dev, int index, 1101 struct resource *r) 1102 { 1103 return __of_address_to_resource(dev, index, -1, r); 1104 } 1105 EXPORT_SYMBOL_GPL(of_address_to_resource); 1106 1107 int of_pci_address_to_resource(struct device_node *dev, int bar, 1108 struct resource *r) 1109 { 1110 1111 if (!IS_ENABLED(CONFIG_PCI)) 1112 return -ENOSYS; 1113 1114 return __of_address_to_resource(dev, -1, bar, r); 1115 } 1116 EXPORT_SYMBOL_GPL(of_pci_address_to_resource); 1117 1118 /** 1119 * of_iomap - Maps the memory mapped IO for a given device_node 1120 * @np: the device whose io range will be mapped 1121 * @index: index of the io range 1122 * 1123 * Returns a pointer to the mapped memory 1124 */ 1125 void __iomem *of_iomap(struct device_node *np, int index) 1126 { 1127 struct resource res; 1128 1129 if (of_address_to_resource(np, index, &res)) 1130 return NULL; 1131 1132 if (res.flags & IORESOURCE_MEM_NONPOSTED) 1133 return ioremap_np(res.start, resource_size(&res)); 1134 else 1135 return ioremap(res.start, resource_size(&res)); 1136 } 1137 EXPORT_SYMBOL(of_iomap); 1138 1139 /* 1140 * of_io_request_and_map - Requests a resource and maps the memory mapped IO 1141 * for a given device_node 1142 * @device: the device whose io range will be mapped 1143 * @index: index of the io range 1144 * @name: name "override" for the memory region request or NULL 1145 * 1146 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded 1147 * error code on failure. Usage example: 1148 * 1149 * base = of_io_request_and_map(node, 0, "foo"); 1150 * if (IS_ERR(base)) 1151 * return PTR_ERR(base); 1152 */ 1153 void __iomem *of_io_request_and_map(struct device_node *np, int index, 1154 const char *name) 1155 { 1156 struct resource res; 1157 void __iomem *mem; 1158 1159 if (of_address_to_resource(np, index, &res)) 1160 return IOMEM_ERR_PTR(-EINVAL); 1161 1162 if (!name) 1163 name = res.name; 1164 if (!request_mem_region(res.start, resource_size(&res), name)) 1165 return IOMEM_ERR_PTR(-EBUSY); 1166 1167 if (res.flags & IORESOURCE_MEM_NONPOSTED) 1168 mem = ioremap_np(res.start, resource_size(&res)); 1169 else 1170 mem = ioremap(res.start, resource_size(&res)); 1171 1172 if (!mem) { 1173 release_mem_region(res.start, resource_size(&res)); 1174 return IOMEM_ERR_PTR(-ENOMEM); 1175 } 1176 1177 return mem; 1178 } 1179 EXPORT_SYMBOL(of_io_request_and_map); 1180