1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Procedures for creating, accessing and interpreting the device tree. 4 * 5 * Paul Mackerras August 1996. 6 * Copyright (C) 1996-2005 Paul Mackerras. 7 * 8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. 9 * {engebret|bergner}@us.ibm.com 10 * 11 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net 12 * 13 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and 14 * Grant Likely. 15 */ 16 17 #define pr_fmt(fmt) "OF: " fmt 18 19 #include <linux/bitmap.h> 20 #include <linux/console.h> 21 #include <linux/ctype.h> 22 #include <linux/cpu.h> 23 #include <linux/module.h> 24 #include <linux/of.h> 25 #include <linux/of_device.h> 26 #include <linux/of_graph.h> 27 #include <linux/spinlock.h> 28 #include <linux/slab.h> 29 #include <linux/string.h> 30 #include <linux/proc_fs.h> 31 32 #include "of_private.h" 33 34 LIST_HEAD(aliases_lookup); 35 36 struct device_node *of_root; 37 EXPORT_SYMBOL(of_root); 38 struct device_node *of_chosen; 39 struct device_node *of_aliases; 40 struct device_node *of_stdout; 41 static const char *of_stdout_options; 42 43 struct kset *of_kset; 44 45 /* 46 * Used to protect the of_aliases, to hold off addition of nodes to sysfs. 47 * This mutex must be held whenever modifications are being made to the 48 * device tree. The of_{attach,detach}_node() and 49 * of_{add,remove,update}_property() helpers make sure this happens. 50 */ 51 DEFINE_MUTEX(of_mutex); 52 53 /* use when traversing tree through the child, sibling, 54 * or parent members of struct device_node. 55 */ 56 DEFINE_RAW_SPINLOCK(devtree_lock); 57 58 bool of_node_name_eq(const struct device_node *np, const char *name) 59 { 60 const char *node_name; 61 size_t len; 62 63 if (!np) 64 return false; 65 66 node_name = kbasename(np->full_name); 67 len = strchrnul(node_name, '@') - node_name; 68 69 return (strlen(name) == len) && (strncmp(node_name, name, len) == 0); 70 } 71 EXPORT_SYMBOL(of_node_name_eq); 72 73 bool of_node_name_prefix(const struct device_node *np, const char *prefix) 74 { 75 if (!np) 76 return false; 77 78 return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0; 79 } 80 EXPORT_SYMBOL(of_node_name_prefix); 81 82 int of_n_addr_cells(struct device_node *np) 83 { 84 u32 cells; 85 86 do { 87 if (np->parent) 88 np = np->parent; 89 if (!of_property_read_u32(np, "#address-cells", &cells)) 90 return cells; 91 } while (np->parent); 92 /* No #address-cells property for the root node */ 93 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; 94 } 95 EXPORT_SYMBOL(of_n_addr_cells); 96 97 int of_n_size_cells(struct device_node *np) 98 { 99 u32 cells; 100 101 do { 102 if (np->parent) 103 np = np->parent; 104 if (!of_property_read_u32(np, "#size-cells", &cells)) 105 return cells; 106 } while (np->parent); 107 /* No #size-cells property for the root node */ 108 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; 109 } 110 EXPORT_SYMBOL(of_n_size_cells); 111 112 #ifdef CONFIG_NUMA 113 int __weak of_node_to_nid(struct device_node *np) 114 { 115 return NUMA_NO_NODE; 116 } 117 #endif 118 119 static struct device_node **phandle_cache; 120 static u32 phandle_cache_mask; 121 122 /* 123 * Assumptions behind phandle_cache implementation: 124 * - phandle property values are in a contiguous range of 1..n 125 * 126 * If the assumptions do not hold, then 127 * - the phandle lookup overhead reduction provided by the cache 128 * will likely be less 129 */ 130 void of_populate_phandle_cache(void) 131 { 132 unsigned long flags; 133 u32 cache_entries; 134 struct device_node *np; 135 u32 phandles = 0; 136 137 raw_spin_lock_irqsave(&devtree_lock, flags); 138 139 kfree(phandle_cache); 140 phandle_cache = NULL; 141 142 for_each_of_allnodes(np) 143 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) 144 phandles++; 145 146 if (!phandles) 147 goto out; 148 149 cache_entries = roundup_pow_of_two(phandles); 150 phandle_cache_mask = cache_entries - 1; 151 152 phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache), 153 GFP_ATOMIC); 154 if (!phandle_cache) 155 goto out; 156 157 for_each_of_allnodes(np) 158 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) 159 phandle_cache[np->phandle & phandle_cache_mask] = np; 160 161 out: 162 raw_spin_unlock_irqrestore(&devtree_lock, flags); 163 } 164 165 int of_free_phandle_cache(void) 166 { 167 unsigned long flags; 168 169 raw_spin_lock_irqsave(&devtree_lock, flags); 170 171 kfree(phandle_cache); 172 phandle_cache = NULL; 173 174 raw_spin_unlock_irqrestore(&devtree_lock, flags); 175 176 return 0; 177 } 178 #if !defined(CONFIG_MODULES) 179 late_initcall_sync(of_free_phandle_cache); 180 #endif 181 182 void __init of_core_init(void) 183 { 184 struct device_node *np; 185 186 of_populate_phandle_cache(); 187 188 /* Create the kset, and register existing nodes */ 189 mutex_lock(&of_mutex); 190 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj); 191 if (!of_kset) { 192 mutex_unlock(&of_mutex); 193 pr_err("failed to register existing nodes\n"); 194 return; 195 } 196 for_each_of_allnodes(np) 197 __of_attach_node_sysfs(np); 198 mutex_unlock(&of_mutex); 199 200 /* Symlink in /proc as required by userspace ABI */ 201 if (of_root) 202 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base"); 203 } 204 205 static struct property *__of_find_property(const struct device_node *np, 206 const char *name, int *lenp) 207 { 208 struct property *pp; 209 210 if (!np) 211 return NULL; 212 213 for (pp = np->properties; pp; pp = pp->next) { 214 if (of_prop_cmp(pp->name, name) == 0) { 215 if (lenp) 216 *lenp = pp->length; 217 break; 218 } 219 } 220 221 return pp; 222 } 223 224 struct property *of_find_property(const struct device_node *np, 225 const char *name, 226 int *lenp) 227 { 228 struct property *pp; 229 unsigned long flags; 230 231 raw_spin_lock_irqsave(&devtree_lock, flags); 232 pp = __of_find_property(np, name, lenp); 233 raw_spin_unlock_irqrestore(&devtree_lock, flags); 234 235 return pp; 236 } 237 EXPORT_SYMBOL(of_find_property); 238 239 struct device_node *__of_find_all_nodes(struct device_node *prev) 240 { 241 struct device_node *np; 242 if (!prev) { 243 np = of_root; 244 } else if (prev->child) { 245 np = prev->child; 246 } else { 247 /* Walk back up looking for a sibling, or the end of the structure */ 248 np = prev; 249 while (np->parent && !np->sibling) 250 np = np->parent; 251 np = np->sibling; /* Might be null at the end of the tree */ 252 } 253 return np; 254 } 255 256 /** 257 * of_find_all_nodes - Get next node in global list 258 * @prev: Previous node or NULL to start iteration 259 * of_node_put() will be called on it 260 * 261 * Returns a node pointer with refcount incremented, use 262 * of_node_put() on it when done. 263 */ 264 struct device_node *of_find_all_nodes(struct device_node *prev) 265 { 266 struct device_node *np; 267 unsigned long flags; 268 269 raw_spin_lock_irqsave(&devtree_lock, flags); 270 np = __of_find_all_nodes(prev); 271 of_node_get(np); 272 of_node_put(prev); 273 raw_spin_unlock_irqrestore(&devtree_lock, flags); 274 return np; 275 } 276 EXPORT_SYMBOL(of_find_all_nodes); 277 278 /* 279 * Find a property with a given name for a given node 280 * and return the value. 281 */ 282 const void *__of_get_property(const struct device_node *np, 283 const char *name, int *lenp) 284 { 285 struct property *pp = __of_find_property(np, name, lenp); 286 287 return pp ? pp->value : NULL; 288 } 289 290 /* 291 * Find a property with a given name for a given node 292 * and return the value. 293 */ 294 const void *of_get_property(const struct device_node *np, const char *name, 295 int *lenp) 296 { 297 struct property *pp = of_find_property(np, name, lenp); 298 299 return pp ? pp->value : NULL; 300 } 301 EXPORT_SYMBOL(of_get_property); 302 303 /* 304 * arch_match_cpu_phys_id - Match the given logical CPU and physical id 305 * 306 * @cpu: logical cpu index of a core/thread 307 * @phys_id: physical identifier of a core/thread 308 * 309 * CPU logical to physical index mapping is architecture specific. 310 * However this __weak function provides a default match of physical 311 * id to logical cpu index. phys_id provided here is usually values read 312 * from the device tree which must match the hardware internal registers. 313 * 314 * Returns true if the physical identifier and the logical cpu index 315 * correspond to the same core/thread, false otherwise. 316 */ 317 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id) 318 { 319 return (u32)phys_id == cpu; 320 } 321 322 /** 323 * Checks if the given "prop_name" property holds the physical id of the 324 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not 325 * NULL, local thread number within the core is returned in it. 326 */ 327 static bool __of_find_n_match_cpu_property(struct device_node *cpun, 328 const char *prop_name, int cpu, unsigned int *thread) 329 { 330 const __be32 *cell; 331 int ac, prop_len, tid; 332 u64 hwid; 333 334 ac = of_n_addr_cells(cpun); 335 cell = of_get_property(cpun, prop_name, &prop_len); 336 if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0)) 337 return true; 338 if (!cell || !ac) 339 return false; 340 prop_len /= sizeof(*cell) * ac; 341 for (tid = 0; tid < prop_len; tid++) { 342 hwid = of_read_number(cell, ac); 343 if (arch_match_cpu_phys_id(cpu, hwid)) { 344 if (thread) 345 *thread = tid; 346 return true; 347 } 348 cell += ac; 349 } 350 return false; 351 } 352 353 /* 354 * arch_find_n_match_cpu_physical_id - See if the given device node is 355 * for the cpu corresponding to logical cpu 'cpu'. Return true if so, 356 * else false. If 'thread' is non-NULL, the local thread number within the 357 * core is returned in it. 358 */ 359 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun, 360 int cpu, unsigned int *thread) 361 { 362 /* Check for non-standard "ibm,ppc-interrupt-server#s" property 363 * for thread ids on PowerPC. If it doesn't exist fallback to 364 * standard "reg" property. 365 */ 366 if (IS_ENABLED(CONFIG_PPC) && 367 __of_find_n_match_cpu_property(cpun, 368 "ibm,ppc-interrupt-server#s", 369 cpu, thread)) 370 return true; 371 372 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread); 373 } 374 375 /** 376 * of_get_cpu_node - Get device node associated with the given logical CPU 377 * 378 * @cpu: CPU number(logical index) for which device node is required 379 * @thread: if not NULL, local thread number within the physical core is 380 * returned 381 * 382 * The main purpose of this function is to retrieve the device node for the 383 * given logical CPU index. It should be used to initialize the of_node in 384 * cpu device. Once of_node in cpu device is populated, all the further 385 * references can use that instead. 386 * 387 * CPU logical to physical index mapping is architecture specific and is built 388 * before booting secondary cores. This function uses arch_match_cpu_phys_id 389 * which can be overridden by architecture specific implementation. 390 * 391 * Returns a node pointer for the logical cpu with refcount incremented, use 392 * of_node_put() on it when done. Returns NULL if not found. 393 */ 394 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) 395 { 396 struct device_node *cpun; 397 398 for_each_of_cpu_node(cpun) { 399 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread)) 400 return cpun; 401 } 402 return NULL; 403 } 404 EXPORT_SYMBOL(of_get_cpu_node); 405 406 /** 407 * of_cpu_node_to_id: Get the logical CPU number for a given device_node 408 * 409 * @cpu_node: Pointer to the device_node for CPU. 410 * 411 * Returns the logical CPU number of the given CPU device_node. 412 * Returns -ENODEV if the CPU is not found. 413 */ 414 int of_cpu_node_to_id(struct device_node *cpu_node) 415 { 416 int cpu; 417 bool found = false; 418 struct device_node *np; 419 420 for_each_possible_cpu(cpu) { 421 np = of_cpu_device_node_get(cpu); 422 found = (cpu_node == np); 423 of_node_put(np); 424 if (found) 425 return cpu; 426 } 427 428 return -ENODEV; 429 } 430 EXPORT_SYMBOL(of_cpu_node_to_id); 431 432 /** 433 * __of_device_is_compatible() - Check if the node matches given constraints 434 * @device: pointer to node 435 * @compat: required compatible string, NULL or "" for any match 436 * @type: required device_type value, NULL or "" for any match 437 * @name: required node name, NULL or "" for any match 438 * 439 * Checks if the given @compat, @type and @name strings match the 440 * properties of the given @device. A constraints can be skipped by 441 * passing NULL or an empty string as the constraint. 442 * 443 * Returns 0 for no match, and a positive integer on match. The return 444 * value is a relative score with larger values indicating better 445 * matches. The score is weighted for the most specific compatible value 446 * to get the highest score. Matching type is next, followed by matching 447 * name. Practically speaking, this results in the following priority 448 * order for matches: 449 * 450 * 1. specific compatible && type && name 451 * 2. specific compatible && type 452 * 3. specific compatible && name 453 * 4. specific compatible 454 * 5. general compatible && type && name 455 * 6. general compatible && type 456 * 7. general compatible && name 457 * 8. general compatible 458 * 9. type && name 459 * 10. type 460 * 11. name 461 */ 462 static int __of_device_is_compatible(const struct device_node *device, 463 const char *compat, const char *type, const char *name) 464 { 465 struct property *prop; 466 const char *cp; 467 int index = 0, score = 0; 468 469 /* Compatible match has highest priority */ 470 if (compat && compat[0]) { 471 prop = __of_find_property(device, "compatible", NULL); 472 for (cp = of_prop_next_string(prop, NULL); cp; 473 cp = of_prop_next_string(prop, cp), index++) { 474 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { 475 score = INT_MAX/2 - (index << 2); 476 break; 477 } 478 } 479 if (!score) 480 return 0; 481 } 482 483 /* Matching type is better than matching name */ 484 if (type && type[0]) { 485 if (!device->type || of_node_cmp(type, device->type)) 486 return 0; 487 score += 2; 488 } 489 490 /* Matching name is a bit better than not */ 491 if (name && name[0]) { 492 if (!device->name || of_node_cmp(name, device->name)) 493 return 0; 494 score++; 495 } 496 497 return score; 498 } 499 500 /** Checks if the given "compat" string matches one of the strings in 501 * the device's "compatible" property 502 */ 503 int of_device_is_compatible(const struct device_node *device, 504 const char *compat) 505 { 506 unsigned long flags; 507 int res; 508 509 raw_spin_lock_irqsave(&devtree_lock, flags); 510 res = __of_device_is_compatible(device, compat, NULL, NULL); 511 raw_spin_unlock_irqrestore(&devtree_lock, flags); 512 return res; 513 } 514 EXPORT_SYMBOL(of_device_is_compatible); 515 516 /** Checks if the device is compatible with any of the entries in 517 * a NULL terminated array of strings. Returns the best match 518 * score or 0. 519 */ 520 int of_device_compatible_match(struct device_node *device, 521 const char *const *compat) 522 { 523 unsigned int tmp, score = 0; 524 525 if (!compat) 526 return 0; 527 528 while (*compat) { 529 tmp = of_device_is_compatible(device, *compat); 530 if (tmp > score) 531 score = tmp; 532 compat++; 533 } 534 535 return score; 536 } 537 538 /** 539 * of_machine_is_compatible - Test root of device tree for a given compatible value 540 * @compat: compatible string to look for in root node's compatible property. 541 * 542 * Returns a positive integer if the root node has the given value in its 543 * compatible property. 544 */ 545 int of_machine_is_compatible(const char *compat) 546 { 547 struct device_node *root; 548 int rc = 0; 549 550 root = of_find_node_by_path("/"); 551 if (root) { 552 rc = of_device_is_compatible(root, compat); 553 of_node_put(root); 554 } 555 return rc; 556 } 557 EXPORT_SYMBOL(of_machine_is_compatible); 558 559 /** 560 * __of_device_is_available - check if a device is available for use 561 * 562 * @device: Node to check for availability, with locks already held 563 * 564 * Returns true if the status property is absent or set to "okay" or "ok", 565 * false otherwise 566 */ 567 static bool __of_device_is_available(const struct device_node *device) 568 { 569 const char *status; 570 int statlen; 571 572 if (!device) 573 return false; 574 575 status = __of_get_property(device, "status", &statlen); 576 if (status == NULL) 577 return true; 578 579 if (statlen > 0) { 580 if (!strcmp(status, "okay") || !strcmp(status, "ok")) 581 return true; 582 } 583 584 return false; 585 } 586 587 /** 588 * of_device_is_available - check if a device is available for use 589 * 590 * @device: Node to check for availability 591 * 592 * Returns true if the status property is absent or set to "okay" or "ok", 593 * false otherwise 594 */ 595 bool of_device_is_available(const struct device_node *device) 596 { 597 unsigned long flags; 598 bool res; 599 600 raw_spin_lock_irqsave(&devtree_lock, flags); 601 res = __of_device_is_available(device); 602 raw_spin_unlock_irqrestore(&devtree_lock, flags); 603 return res; 604 605 } 606 EXPORT_SYMBOL(of_device_is_available); 607 608 /** 609 * of_device_is_big_endian - check if a device has BE registers 610 * 611 * @device: Node to check for endianness 612 * 613 * Returns true if the device has a "big-endian" property, or if the kernel 614 * was compiled for BE *and* the device has a "native-endian" property. 615 * Returns false otherwise. 616 * 617 * Callers would nominally use ioread32be/iowrite32be if 618 * of_device_is_big_endian() == true, or readl/writel otherwise. 619 */ 620 bool of_device_is_big_endian(const struct device_node *device) 621 { 622 if (of_property_read_bool(device, "big-endian")) 623 return true; 624 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && 625 of_property_read_bool(device, "native-endian")) 626 return true; 627 return false; 628 } 629 EXPORT_SYMBOL(of_device_is_big_endian); 630 631 /** 632 * of_get_parent - Get a node's parent if any 633 * @node: Node to get parent 634 * 635 * Returns a node pointer with refcount incremented, use 636 * of_node_put() on it when done. 637 */ 638 struct device_node *of_get_parent(const struct device_node *node) 639 { 640 struct device_node *np; 641 unsigned long flags; 642 643 if (!node) 644 return NULL; 645 646 raw_spin_lock_irqsave(&devtree_lock, flags); 647 np = of_node_get(node->parent); 648 raw_spin_unlock_irqrestore(&devtree_lock, flags); 649 return np; 650 } 651 EXPORT_SYMBOL(of_get_parent); 652 653 /** 654 * of_get_next_parent - Iterate to a node's parent 655 * @node: Node to get parent of 656 * 657 * This is like of_get_parent() except that it drops the 658 * refcount on the passed node, making it suitable for iterating 659 * through a node's parents. 660 * 661 * Returns a node pointer with refcount incremented, use 662 * of_node_put() on it when done. 663 */ 664 struct device_node *of_get_next_parent(struct device_node *node) 665 { 666 struct device_node *parent; 667 unsigned long flags; 668 669 if (!node) 670 return NULL; 671 672 raw_spin_lock_irqsave(&devtree_lock, flags); 673 parent = of_node_get(node->parent); 674 of_node_put(node); 675 raw_spin_unlock_irqrestore(&devtree_lock, flags); 676 return parent; 677 } 678 EXPORT_SYMBOL(of_get_next_parent); 679 680 static struct device_node *__of_get_next_child(const struct device_node *node, 681 struct device_node *prev) 682 { 683 struct device_node *next; 684 685 if (!node) 686 return NULL; 687 688 next = prev ? prev->sibling : node->child; 689 for (; next; next = next->sibling) 690 if (of_node_get(next)) 691 break; 692 of_node_put(prev); 693 return next; 694 } 695 #define __for_each_child_of_node(parent, child) \ 696 for (child = __of_get_next_child(parent, NULL); child != NULL; \ 697 child = __of_get_next_child(parent, child)) 698 699 /** 700 * of_get_next_child - Iterate a node childs 701 * @node: parent node 702 * @prev: previous child of the parent node, or NULL to get first 703 * 704 * Returns a node pointer with refcount incremented, use of_node_put() on 705 * it when done. Returns NULL when prev is the last child. Decrements the 706 * refcount of prev. 707 */ 708 struct device_node *of_get_next_child(const struct device_node *node, 709 struct device_node *prev) 710 { 711 struct device_node *next; 712 unsigned long flags; 713 714 raw_spin_lock_irqsave(&devtree_lock, flags); 715 next = __of_get_next_child(node, prev); 716 raw_spin_unlock_irqrestore(&devtree_lock, flags); 717 return next; 718 } 719 EXPORT_SYMBOL(of_get_next_child); 720 721 /** 722 * of_get_next_available_child - Find the next available child node 723 * @node: parent node 724 * @prev: previous child of the parent node, or NULL to get first 725 * 726 * This function is like of_get_next_child(), except that it 727 * automatically skips any disabled nodes (i.e. status = "disabled"). 728 */ 729 struct device_node *of_get_next_available_child(const struct device_node *node, 730 struct device_node *prev) 731 { 732 struct device_node *next; 733 unsigned long flags; 734 735 if (!node) 736 return NULL; 737 738 raw_spin_lock_irqsave(&devtree_lock, flags); 739 next = prev ? prev->sibling : node->child; 740 for (; next; next = next->sibling) { 741 if (!__of_device_is_available(next)) 742 continue; 743 if (of_node_get(next)) 744 break; 745 } 746 of_node_put(prev); 747 raw_spin_unlock_irqrestore(&devtree_lock, flags); 748 return next; 749 } 750 EXPORT_SYMBOL(of_get_next_available_child); 751 752 /** 753 * of_get_next_cpu_node - Iterate on cpu nodes 754 * @prev: previous child of the /cpus node, or NULL to get first 755 * 756 * Returns a cpu node pointer with refcount incremented, use of_node_put() 757 * on it when done. Returns NULL when prev is the last child. Decrements 758 * the refcount of prev. 759 */ 760 struct device_node *of_get_next_cpu_node(struct device_node *prev) 761 { 762 struct device_node *next = NULL; 763 unsigned long flags; 764 struct device_node *node; 765 766 if (!prev) 767 node = of_find_node_by_path("/cpus"); 768 769 raw_spin_lock_irqsave(&devtree_lock, flags); 770 if (prev) 771 next = prev->sibling; 772 else if (node) { 773 next = node->child; 774 of_node_put(node); 775 } 776 for (; next; next = next->sibling) { 777 if (!(of_node_name_eq(next, "cpu") || 778 (next->type && !of_node_cmp(next->type, "cpu")))) 779 continue; 780 if (!__of_device_is_available(next)) 781 continue; 782 if (of_node_get(next)) 783 break; 784 } 785 of_node_put(prev); 786 raw_spin_unlock_irqrestore(&devtree_lock, flags); 787 return next; 788 } 789 EXPORT_SYMBOL(of_get_next_cpu_node); 790 791 /** 792 * of_get_compatible_child - Find compatible child node 793 * @parent: parent node 794 * @compatible: compatible string 795 * 796 * Lookup child node whose compatible property contains the given compatible 797 * string. 798 * 799 * Returns a node pointer with refcount incremented, use of_node_put() on it 800 * when done; or NULL if not found. 801 */ 802 struct device_node *of_get_compatible_child(const struct device_node *parent, 803 const char *compatible) 804 { 805 struct device_node *child; 806 807 for_each_child_of_node(parent, child) { 808 if (of_device_is_compatible(child, compatible)) 809 break; 810 } 811 812 return child; 813 } 814 EXPORT_SYMBOL(of_get_compatible_child); 815 816 /** 817 * of_get_child_by_name - Find the child node by name for a given parent 818 * @node: parent node 819 * @name: child name to look for. 820 * 821 * This function looks for child node for given matching name 822 * 823 * Returns a node pointer if found, with refcount incremented, use 824 * of_node_put() on it when done. 825 * Returns NULL if node is not found. 826 */ 827 struct device_node *of_get_child_by_name(const struct device_node *node, 828 const char *name) 829 { 830 struct device_node *child; 831 832 for_each_child_of_node(node, child) 833 if (child->name && (of_node_cmp(child->name, name) == 0)) 834 break; 835 return child; 836 } 837 EXPORT_SYMBOL(of_get_child_by_name); 838 839 struct device_node *__of_find_node_by_path(struct device_node *parent, 840 const char *path) 841 { 842 struct device_node *child; 843 int len; 844 845 len = strcspn(path, "/:"); 846 if (!len) 847 return NULL; 848 849 __for_each_child_of_node(parent, child) { 850 const char *name = kbasename(child->full_name); 851 if (strncmp(path, name, len) == 0 && (strlen(name) == len)) 852 return child; 853 } 854 return NULL; 855 } 856 857 struct device_node *__of_find_node_by_full_path(struct device_node *node, 858 const char *path) 859 { 860 const char *separator = strchr(path, ':'); 861 862 while (node && *path == '/') { 863 struct device_node *tmp = node; 864 865 path++; /* Increment past '/' delimiter */ 866 node = __of_find_node_by_path(node, path); 867 of_node_put(tmp); 868 path = strchrnul(path, '/'); 869 if (separator && separator < path) 870 break; 871 } 872 return node; 873 } 874 875 /** 876 * of_find_node_opts_by_path - Find a node matching a full OF path 877 * @path: Either the full path to match, or if the path does not 878 * start with '/', the name of a property of the /aliases 879 * node (an alias). In the case of an alias, the node 880 * matching the alias' value will be returned. 881 * @opts: Address of a pointer into which to store the start of 882 * an options string appended to the end of the path with 883 * a ':' separator. 884 * 885 * Valid paths: 886 * /foo/bar Full path 887 * foo Valid alias 888 * foo/bar Valid alias + relative path 889 * 890 * Returns a node pointer with refcount incremented, use 891 * of_node_put() on it when done. 892 */ 893 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts) 894 { 895 struct device_node *np = NULL; 896 struct property *pp; 897 unsigned long flags; 898 const char *separator = strchr(path, ':'); 899 900 if (opts) 901 *opts = separator ? separator + 1 : NULL; 902 903 if (strcmp(path, "/") == 0) 904 return of_node_get(of_root); 905 906 /* The path could begin with an alias */ 907 if (*path != '/') { 908 int len; 909 const char *p = separator; 910 911 if (!p) 912 p = strchrnul(path, '/'); 913 len = p - path; 914 915 /* of_aliases must not be NULL */ 916 if (!of_aliases) 917 return NULL; 918 919 for_each_property_of_node(of_aliases, pp) { 920 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) { 921 np = of_find_node_by_path(pp->value); 922 break; 923 } 924 } 925 if (!np) 926 return NULL; 927 path = p; 928 } 929 930 /* Step down the tree matching path components */ 931 raw_spin_lock_irqsave(&devtree_lock, flags); 932 if (!np) 933 np = of_node_get(of_root); 934 np = __of_find_node_by_full_path(np, path); 935 raw_spin_unlock_irqrestore(&devtree_lock, flags); 936 return np; 937 } 938 EXPORT_SYMBOL(of_find_node_opts_by_path); 939 940 /** 941 * of_find_node_by_name - Find a node by its "name" property 942 * @from: The node to start searching from or NULL; the node 943 * you pass will not be searched, only the next one 944 * will. Typically, you pass what the previous call 945 * returned. of_node_put() will be called on @from. 946 * @name: The name string to match against 947 * 948 * Returns a node pointer with refcount incremented, use 949 * of_node_put() on it when done. 950 */ 951 struct device_node *of_find_node_by_name(struct device_node *from, 952 const char *name) 953 { 954 struct device_node *np; 955 unsigned long flags; 956 957 raw_spin_lock_irqsave(&devtree_lock, flags); 958 for_each_of_allnodes_from(from, np) 959 if (np->name && (of_node_cmp(np->name, name) == 0) 960 && of_node_get(np)) 961 break; 962 of_node_put(from); 963 raw_spin_unlock_irqrestore(&devtree_lock, flags); 964 return np; 965 } 966 EXPORT_SYMBOL(of_find_node_by_name); 967 968 /** 969 * of_find_node_by_type - Find a node by its "device_type" property 970 * @from: The node to start searching from, or NULL to start searching 971 * the entire device tree. The node you pass will not be 972 * searched, only the next one will; typically, you pass 973 * what the previous call returned. of_node_put() will be 974 * called on from for you. 975 * @type: The type string to match against 976 * 977 * Returns a node pointer with refcount incremented, use 978 * of_node_put() on it when done. 979 */ 980 struct device_node *of_find_node_by_type(struct device_node *from, 981 const char *type) 982 { 983 struct device_node *np; 984 unsigned long flags; 985 986 raw_spin_lock_irqsave(&devtree_lock, flags); 987 for_each_of_allnodes_from(from, np) 988 if (np->type && (of_node_cmp(np->type, type) == 0) 989 && of_node_get(np)) 990 break; 991 of_node_put(from); 992 raw_spin_unlock_irqrestore(&devtree_lock, flags); 993 return np; 994 } 995 EXPORT_SYMBOL(of_find_node_by_type); 996 997 /** 998 * of_find_compatible_node - Find a node based on type and one of the 999 * tokens in its "compatible" property 1000 * @from: The node to start searching from or NULL, the node 1001 * you pass will not be searched, only the next one 1002 * will; typically, you pass what the previous call 1003 * returned. of_node_put() will be called on it 1004 * @type: The type string to match "device_type" or NULL to ignore 1005 * @compatible: The string to match to one of the tokens in the device 1006 * "compatible" list. 1007 * 1008 * Returns a node pointer with refcount incremented, use 1009 * of_node_put() on it when done. 1010 */ 1011 struct device_node *of_find_compatible_node(struct device_node *from, 1012 const char *type, const char *compatible) 1013 { 1014 struct device_node *np; 1015 unsigned long flags; 1016 1017 raw_spin_lock_irqsave(&devtree_lock, flags); 1018 for_each_of_allnodes_from(from, np) 1019 if (__of_device_is_compatible(np, compatible, type, NULL) && 1020 of_node_get(np)) 1021 break; 1022 of_node_put(from); 1023 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1024 return np; 1025 } 1026 EXPORT_SYMBOL(of_find_compatible_node); 1027 1028 /** 1029 * of_find_node_with_property - Find a node which has a property with 1030 * the given name. 1031 * @from: The node to start searching from or NULL, the node 1032 * you pass will not be searched, only the next one 1033 * will; typically, you pass what the previous call 1034 * returned. of_node_put() will be called on it 1035 * @prop_name: The name of the property to look for. 1036 * 1037 * Returns a node pointer with refcount incremented, use 1038 * of_node_put() on it when done. 1039 */ 1040 struct device_node *of_find_node_with_property(struct device_node *from, 1041 const char *prop_name) 1042 { 1043 struct device_node *np; 1044 struct property *pp; 1045 unsigned long flags; 1046 1047 raw_spin_lock_irqsave(&devtree_lock, flags); 1048 for_each_of_allnodes_from(from, np) { 1049 for (pp = np->properties; pp; pp = pp->next) { 1050 if (of_prop_cmp(pp->name, prop_name) == 0) { 1051 of_node_get(np); 1052 goto out; 1053 } 1054 } 1055 } 1056 out: 1057 of_node_put(from); 1058 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1059 return np; 1060 } 1061 EXPORT_SYMBOL(of_find_node_with_property); 1062 1063 static 1064 const struct of_device_id *__of_match_node(const struct of_device_id *matches, 1065 const struct device_node *node) 1066 { 1067 const struct of_device_id *best_match = NULL; 1068 int score, best_score = 0; 1069 1070 if (!matches) 1071 return NULL; 1072 1073 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { 1074 score = __of_device_is_compatible(node, matches->compatible, 1075 matches->type, matches->name); 1076 if (score > best_score) { 1077 best_match = matches; 1078 best_score = score; 1079 } 1080 } 1081 1082 return best_match; 1083 } 1084 1085 /** 1086 * of_match_node - Tell if a device_node has a matching of_match structure 1087 * @matches: array of of device match structures to search in 1088 * @node: the of device structure to match against 1089 * 1090 * Low level utility function used by device matching. 1091 */ 1092 const struct of_device_id *of_match_node(const struct of_device_id *matches, 1093 const struct device_node *node) 1094 { 1095 const struct of_device_id *match; 1096 unsigned long flags; 1097 1098 raw_spin_lock_irqsave(&devtree_lock, flags); 1099 match = __of_match_node(matches, node); 1100 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1101 return match; 1102 } 1103 EXPORT_SYMBOL(of_match_node); 1104 1105 /** 1106 * of_find_matching_node_and_match - Find a node based on an of_device_id 1107 * match table. 1108 * @from: The node to start searching from or NULL, the node 1109 * you pass will not be searched, only the next one 1110 * will; typically, you pass what the previous call 1111 * returned. of_node_put() will be called on it 1112 * @matches: array of of device match structures to search in 1113 * @match Updated to point at the matches entry which matched 1114 * 1115 * Returns a node pointer with refcount incremented, use 1116 * of_node_put() on it when done. 1117 */ 1118 struct device_node *of_find_matching_node_and_match(struct device_node *from, 1119 const struct of_device_id *matches, 1120 const struct of_device_id **match) 1121 { 1122 struct device_node *np; 1123 const struct of_device_id *m; 1124 unsigned long flags; 1125 1126 if (match) 1127 *match = NULL; 1128 1129 raw_spin_lock_irqsave(&devtree_lock, flags); 1130 for_each_of_allnodes_from(from, np) { 1131 m = __of_match_node(matches, np); 1132 if (m && of_node_get(np)) { 1133 if (match) 1134 *match = m; 1135 break; 1136 } 1137 } 1138 of_node_put(from); 1139 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1140 return np; 1141 } 1142 EXPORT_SYMBOL(of_find_matching_node_and_match); 1143 1144 /** 1145 * of_modalias_node - Lookup appropriate modalias for a device node 1146 * @node: pointer to a device tree node 1147 * @modalias: Pointer to buffer that modalias value will be copied into 1148 * @len: Length of modalias value 1149 * 1150 * Based on the value of the compatible property, this routine will attempt 1151 * to choose an appropriate modalias value for a particular device tree node. 1152 * It does this by stripping the manufacturer prefix (as delimited by a ',') 1153 * from the first entry in the compatible list property. 1154 * 1155 * This routine returns 0 on success, <0 on failure. 1156 */ 1157 int of_modalias_node(struct device_node *node, char *modalias, int len) 1158 { 1159 const char *compatible, *p; 1160 int cplen; 1161 1162 compatible = of_get_property(node, "compatible", &cplen); 1163 if (!compatible || strlen(compatible) > cplen) 1164 return -ENODEV; 1165 p = strchr(compatible, ','); 1166 strlcpy(modalias, p ? p + 1 : compatible, len); 1167 return 0; 1168 } 1169 EXPORT_SYMBOL_GPL(of_modalias_node); 1170 1171 /** 1172 * of_find_node_by_phandle - Find a node given a phandle 1173 * @handle: phandle of the node to find 1174 * 1175 * Returns a node pointer with refcount incremented, use 1176 * of_node_put() on it when done. 1177 */ 1178 struct device_node *of_find_node_by_phandle(phandle handle) 1179 { 1180 struct device_node *np = NULL; 1181 unsigned long flags; 1182 phandle masked_handle; 1183 1184 if (!handle) 1185 return NULL; 1186 1187 raw_spin_lock_irqsave(&devtree_lock, flags); 1188 1189 masked_handle = handle & phandle_cache_mask; 1190 1191 if (phandle_cache) { 1192 if (phandle_cache[masked_handle] && 1193 handle == phandle_cache[masked_handle]->phandle) 1194 np = phandle_cache[masked_handle]; 1195 } 1196 1197 if (!np) { 1198 for_each_of_allnodes(np) 1199 if (np->phandle == handle) { 1200 if (phandle_cache) 1201 phandle_cache[masked_handle] = np; 1202 break; 1203 } 1204 } 1205 1206 of_node_get(np); 1207 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1208 return np; 1209 } 1210 EXPORT_SYMBOL(of_find_node_by_phandle); 1211 1212 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args) 1213 { 1214 int i; 1215 printk("%s %pOF", msg, args->np); 1216 for (i = 0; i < args->args_count; i++) { 1217 const char delim = i ? ',' : ':'; 1218 1219 pr_cont("%c%08x", delim, args->args[i]); 1220 } 1221 pr_cont("\n"); 1222 } 1223 1224 int of_phandle_iterator_init(struct of_phandle_iterator *it, 1225 const struct device_node *np, 1226 const char *list_name, 1227 const char *cells_name, 1228 int cell_count) 1229 { 1230 const __be32 *list; 1231 int size; 1232 1233 memset(it, 0, sizeof(*it)); 1234 1235 list = of_get_property(np, list_name, &size); 1236 if (!list) 1237 return -ENOENT; 1238 1239 it->cells_name = cells_name; 1240 it->cell_count = cell_count; 1241 it->parent = np; 1242 it->list_end = list + size / sizeof(*list); 1243 it->phandle_end = list; 1244 it->cur = list; 1245 1246 return 0; 1247 } 1248 EXPORT_SYMBOL_GPL(of_phandle_iterator_init); 1249 1250 int of_phandle_iterator_next(struct of_phandle_iterator *it) 1251 { 1252 uint32_t count = 0; 1253 1254 if (it->node) { 1255 of_node_put(it->node); 1256 it->node = NULL; 1257 } 1258 1259 if (!it->cur || it->phandle_end >= it->list_end) 1260 return -ENOENT; 1261 1262 it->cur = it->phandle_end; 1263 1264 /* If phandle is 0, then it is an empty entry with no arguments. */ 1265 it->phandle = be32_to_cpup(it->cur++); 1266 1267 if (it->phandle) { 1268 1269 /* 1270 * Find the provider node and parse the #*-cells property to 1271 * determine the argument length. 1272 */ 1273 it->node = of_find_node_by_phandle(it->phandle); 1274 1275 if (it->cells_name) { 1276 if (!it->node) { 1277 pr_err("%pOF: could not find phandle\n", 1278 it->parent); 1279 goto err; 1280 } 1281 1282 if (of_property_read_u32(it->node, it->cells_name, 1283 &count)) { 1284 pr_err("%pOF: could not get %s for %pOF\n", 1285 it->parent, 1286 it->cells_name, 1287 it->node); 1288 goto err; 1289 } 1290 } else { 1291 count = it->cell_count; 1292 } 1293 1294 /* 1295 * Make sure that the arguments actually fit in the remaining 1296 * property data length 1297 */ 1298 if (it->cur + count > it->list_end) { 1299 pr_err("%pOF: arguments longer than property\n", 1300 it->parent); 1301 goto err; 1302 } 1303 } 1304 1305 it->phandle_end = it->cur + count; 1306 it->cur_count = count; 1307 1308 return 0; 1309 1310 err: 1311 if (it->node) { 1312 of_node_put(it->node); 1313 it->node = NULL; 1314 } 1315 1316 return -EINVAL; 1317 } 1318 EXPORT_SYMBOL_GPL(of_phandle_iterator_next); 1319 1320 int of_phandle_iterator_args(struct of_phandle_iterator *it, 1321 uint32_t *args, 1322 int size) 1323 { 1324 int i, count; 1325 1326 count = it->cur_count; 1327 1328 if (WARN_ON(size < count)) 1329 count = size; 1330 1331 for (i = 0; i < count; i++) 1332 args[i] = be32_to_cpup(it->cur++); 1333 1334 return count; 1335 } 1336 1337 static int __of_parse_phandle_with_args(const struct device_node *np, 1338 const char *list_name, 1339 const char *cells_name, 1340 int cell_count, int index, 1341 struct of_phandle_args *out_args) 1342 { 1343 struct of_phandle_iterator it; 1344 int rc, cur_index = 0; 1345 1346 /* Loop over the phandles until all the requested entry is found */ 1347 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) { 1348 /* 1349 * All of the error cases bail out of the loop, so at 1350 * this point, the parsing is successful. If the requested 1351 * index matches, then fill the out_args structure and return, 1352 * or return -ENOENT for an empty entry. 1353 */ 1354 rc = -ENOENT; 1355 if (cur_index == index) { 1356 if (!it.phandle) 1357 goto err; 1358 1359 if (out_args) { 1360 int c; 1361 1362 c = of_phandle_iterator_args(&it, 1363 out_args->args, 1364 MAX_PHANDLE_ARGS); 1365 out_args->np = it.node; 1366 out_args->args_count = c; 1367 } else { 1368 of_node_put(it.node); 1369 } 1370 1371 /* Found it! return success */ 1372 return 0; 1373 } 1374 1375 cur_index++; 1376 } 1377 1378 /* 1379 * Unlock node before returning result; will be one of: 1380 * -ENOENT : index is for empty phandle 1381 * -EINVAL : parsing error on data 1382 */ 1383 1384 err: 1385 of_node_put(it.node); 1386 return rc; 1387 } 1388 1389 /** 1390 * of_parse_phandle - Resolve a phandle property to a device_node pointer 1391 * @np: Pointer to device node holding phandle property 1392 * @phandle_name: Name of property holding a phandle value 1393 * @index: For properties holding a table of phandles, this is the index into 1394 * the table 1395 * 1396 * Returns the device_node pointer with refcount incremented. Use 1397 * of_node_put() on it when done. 1398 */ 1399 struct device_node *of_parse_phandle(const struct device_node *np, 1400 const char *phandle_name, int index) 1401 { 1402 struct of_phandle_args args; 1403 1404 if (index < 0) 1405 return NULL; 1406 1407 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, 1408 index, &args)) 1409 return NULL; 1410 1411 return args.np; 1412 } 1413 EXPORT_SYMBOL(of_parse_phandle); 1414 1415 /** 1416 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list 1417 * @np: pointer to a device tree node containing a list 1418 * @list_name: property name that contains a list 1419 * @cells_name: property name that specifies phandles' arguments count 1420 * @index: index of a phandle to parse out 1421 * @out_args: optional pointer to output arguments structure (will be filled) 1422 * 1423 * This function is useful to parse lists of phandles and their arguments. 1424 * Returns 0 on success and fills out_args, on error returns appropriate 1425 * errno value. 1426 * 1427 * Caller is responsible to call of_node_put() on the returned out_args->np 1428 * pointer. 1429 * 1430 * Example: 1431 * 1432 * phandle1: node1 { 1433 * #list-cells = <2>; 1434 * } 1435 * 1436 * phandle2: node2 { 1437 * #list-cells = <1>; 1438 * } 1439 * 1440 * node3 { 1441 * list = <&phandle1 1 2 &phandle2 3>; 1442 * } 1443 * 1444 * To get a device_node of the `node2' node you may call this: 1445 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); 1446 */ 1447 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, 1448 const char *cells_name, int index, 1449 struct of_phandle_args *out_args) 1450 { 1451 if (index < 0) 1452 return -EINVAL; 1453 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, 1454 index, out_args); 1455 } 1456 EXPORT_SYMBOL(of_parse_phandle_with_args); 1457 1458 /** 1459 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it 1460 * @np: pointer to a device tree node containing a list 1461 * @list_name: property name that contains a list 1462 * @stem_name: stem of property names that specify phandles' arguments count 1463 * @index: index of a phandle to parse out 1464 * @out_args: optional pointer to output arguments structure (will be filled) 1465 * 1466 * This function is useful to parse lists of phandles and their arguments. 1467 * Returns 0 on success and fills out_args, on error returns appropriate errno 1468 * value. The difference between this function and of_parse_phandle_with_args() 1469 * is that this API remaps a phandle if the node the phandle points to has 1470 * a <@stem_name>-map property. 1471 * 1472 * Caller is responsible to call of_node_put() on the returned out_args->np 1473 * pointer. 1474 * 1475 * Example: 1476 * 1477 * phandle1: node1 { 1478 * #list-cells = <2>; 1479 * } 1480 * 1481 * phandle2: node2 { 1482 * #list-cells = <1>; 1483 * } 1484 * 1485 * phandle3: node3 { 1486 * #list-cells = <1>; 1487 * list-map = <0 &phandle2 3>, 1488 * <1 &phandle2 2>, 1489 * <2 &phandle1 5 1>; 1490 * list-map-mask = <0x3>; 1491 * }; 1492 * 1493 * node4 { 1494 * list = <&phandle1 1 2 &phandle3 0>; 1495 * } 1496 * 1497 * To get a device_node of the `node2' node you may call this: 1498 * of_parse_phandle_with_args(node4, "list", "list", 1, &args); 1499 */ 1500 int of_parse_phandle_with_args_map(const struct device_node *np, 1501 const char *list_name, 1502 const char *stem_name, 1503 int index, struct of_phandle_args *out_args) 1504 { 1505 char *cells_name, *map_name = NULL, *mask_name = NULL; 1506 char *pass_name = NULL; 1507 struct device_node *cur, *new = NULL; 1508 const __be32 *map, *mask, *pass; 1509 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 }; 1510 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 }; 1511 __be32 initial_match_array[MAX_PHANDLE_ARGS]; 1512 const __be32 *match_array = initial_match_array; 1513 int i, ret, map_len, match; 1514 u32 list_size, new_size; 1515 1516 if (index < 0) 1517 return -EINVAL; 1518 1519 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name); 1520 if (!cells_name) 1521 return -ENOMEM; 1522 1523 ret = -ENOMEM; 1524 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name); 1525 if (!map_name) 1526 goto free; 1527 1528 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); 1529 if (!mask_name) 1530 goto free; 1531 1532 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name); 1533 if (!pass_name) 1534 goto free; 1535 1536 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index, 1537 out_args); 1538 if (ret) 1539 goto free; 1540 1541 /* Get the #<list>-cells property */ 1542 cur = out_args->np; 1543 ret = of_property_read_u32(cur, cells_name, &list_size); 1544 if (ret < 0) 1545 goto put; 1546 1547 /* Precalculate the match array - this simplifies match loop */ 1548 for (i = 0; i < list_size; i++) 1549 initial_match_array[i] = cpu_to_be32(out_args->args[i]); 1550 1551 ret = -EINVAL; 1552 while (cur) { 1553 /* Get the <list>-map property */ 1554 map = of_get_property(cur, map_name, &map_len); 1555 if (!map) { 1556 ret = 0; 1557 goto free; 1558 } 1559 map_len /= sizeof(u32); 1560 1561 /* Get the <list>-map-mask property (optional) */ 1562 mask = of_get_property(cur, mask_name, NULL); 1563 if (!mask) 1564 mask = dummy_mask; 1565 /* Iterate through <list>-map property */ 1566 match = 0; 1567 while (map_len > (list_size + 1) && !match) { 1568 /* Compare specifiers */ 1569 match = 1; 1570 for (i = 0; i < list_size; i++, map_len--) 1571 match &= !((match_array[i] ^ *map++) & mask[i]); 1572 1573 of_node_put(new); 1574 new = of_find_node_by_phandle(be32_to_cpup(map)); 1575 map++; 1576 map_len--; 1577 1578 /* Check if not found */ 1579 if (!new) 1580 goto put; 1581 1582 if (!of_device_is_available(new)) 1583 match = 0; 1584 1585 ret = of_property_read_u32(new, cells_name, &new_size); 1586 if (ret) 1587 goto put; 1588 1589 /* Check for malformed properties */ 1590 if (WARN_ON(new_size > MAX_PHANDLE_ARGS)) 1591 goto put; 1592 if (map_len < new_size) 1593 goto put; 1594 1595 /* Move forward by new node's #<list>-cells amount */ 1596 map += new_size; 1597 map_len -= new_size; 1598 } 1599 if (!match) 1600 goto put; 1601 1602 /* Get the <list>-map-pass-thru property (optional) */ 1603 pass = of_get_property(cur, pass_name, NULL); 1604 if (!pass) 1605 pass = dummy_pass; 1606 1607 /* 1608 * Successfully parsed a <list>-map translation; copy new 1609 * specifier into the out_args structure, keeping the 1610 * bits specified in <list>-map-pass-thru. 1611 */ 1612 match_array = map - new_size; 1613 for (i = 0; i < new_size; i++) { 1614 __be32 val = *(map - new_size + i); 1615 1616 if (i < list_size) { 1617 val &= ~pass[i]; 1618 val |= cpu_to_be32(out_args->args[i]) & pass[i]; 1619 } 1620 1621 out_args->args[i] = be32_to_cpu(val); 1622 } 1623 out_args->args_count = list_size = new_size; 1624 /* Iterate again with new provider */ 1625 out_args->np = new; 1626 of_node_put(cur); 1627 cur = new; 1628 } 1629 put: 1630 of_node_put(cur); 1631 of_node_put(new); 1632 free: 1633 kfree(mask_name); 1634 kfree(map_name); 1635 kfree(cells_name); 1636 kfree(pass_name); 1637 1638 return ret; 1639 } 1640 EXPORT_SYMBOL(of_parse_phandle_with_args_map); 1641 1642 /** 1643 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list 1644 * @np: pointer to a device tree node containing a list 1645 * @list_name: property name that contains a list 1646 * @cell_count: number of argument cells following the phandle 1647 * @index: index of a phandle to parse out 1648 * @out_args: optional pointer to output arguments structure (will be filled) 1649 * 1650 * This function is useful to parse lists of phandles and their arguments. 1651 * Returns 0 on success and fills out_args, on error returns appropriate 1652 * errno value. 1653 * 1654 * Caller is responsible to call of_node_put() on the returned out_args->np 1655 * pointer. 1656 * 1657 * Example: 1658 * 1659 * phandle1: node1 { 1660 * } 1661 * 1662 * phandle2: node2 { 1663 * } 1664 * 1665 * node3 { 1666 * list = <&phandle1 0 2 &phandle2 2 3>; 1667 * } 1668 * 1669 * To get a device_node of the `node2' node you may call this: 1670 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); 1671 */ 1672 int of_parse_phandle_with_fixed_args(const struct device_node *np, 1673 const char *list_name, int cell_count, 1674 int index, struct of_phandle_args *out_args) 1675 { 1676 if (index < 0) 1677 return -EINVAL; 1678 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, 1679 index, out_args); 1680 } 1681 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args); 1682 1683 /** 1684 * of_count_phandle_with_args() - Find the number of phandles references in a property 1685 * @np: pointer to a device tree node containing a list 1686 * @list_name: property name that contains a list 1687 * @cells_name: property name that specifies phandles' arguments count 1688 * 1689 * Returns the number of phandle + argument tuples within a property. It 1690 * is a typical pattern to encode a list of phandle and variable 1691 * arguments into a single property. The number of arguments is encoded 1692 * by a property in the phandle-target node. For example, a gpios 1693 * property would contain a list of GPIO specifies consisting of a 1694 * phandle and 1 or more arguments. The number of arguments are 1695 * determined by the #gpio-cells property in the node pointed to by the 1696 * phandle. 1697 */ 1698 int of_count_phandle_with_args(const struct device_node *np, const char *list_name, 1699 const char *cells_name) 1700 { 1701 struct of_phandle_iterator it; 1702 int rc, cur_index = 0; 1703 1704 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0); 1705 if (rc) 1706 return rc; 1707 1708 while ((rc = of_phandle_iterator_next(&it)) == 0) 1709 cur_index += 1; 1710 1711 if (rc != -ENOENT) 1712 return rc; 1713 1714 return cur_index; 1715 } 1716 EXPORT_SYMBOL(of_count_phandle_with_args); 1717 1718 /** 1719 * __of_add_property - Add a property to a node without lock operations 1720 */ 1721 int __of_add_property(struct device_node *np, struct property *prop) 1722 { 1723 struct property **next; 1724 1725 prop->next = NULL; 1726 next = &np->properties; 1727 while (*next) { 1728 if (strcmp(prop->name, (*next)->name) == 0) 1729 /* duplicate ! don't insert it */ 1730 return -EEXIST; 1731 1732 next = &(*next)->next; 1733 } 1734 *next = prop; 1735 1736 return 0; 1737 } 1738 1739 /** 1740 * of_add_property - Add a property to a node 1741 */ 1742 int of_add_property(struct device_node *np, struct property *prop) 1743 { 1744 unsigned long flags; 1745 int rc; 1746 1747 mutex_lock(&of_mutex); 1748 1749 raw_spin_lock_irqsave(&devtree_lock, flags); 1750 rc = __of_add_property(np, prop); 1751 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1752 1753 if (!rc) 1754 __of_add_property_sysfs(np, prop); 1755 1756 mutex_unlock(&of_mutex); 1757 1758 if (!rc) 1759 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL); 1760 1761 return rc; 1762 } 1763 1764 int __of_remove_property(struct device_node *np, struct property *prop) 1765 { 1766 struct property **next; 1767 1768 for (next = &np->properties; *next; next = &(*next)->next) { 1769 if (*next == prop) 1770 break; 1771 } 1772 if (*next == NULL) 1773 return -ENODEV; 1774 1775 /* found the node */ 1776 *next = prop->next; 1777 prop->next = np->deadprops; 1778 np->deadprops = prop; 1779 1780 return 0; 1781 } 1782 1783 /** 1784 * of_remove_property - Remove a property from a node. 1785 * 1786 * Note that we don't actually remove it, since we have given out 1787 * who-knows-how-many pointers to the data using get-property. 1788 * Instead we just move the property to the "dead properties" 1789 * list, so it won't be found any more. 1790 */ 1791 int of_remove_property(struct device_node *np, struct property *prop) 1792 { 1793 unsigned long flags; 1794 int rc; 1795 1796 if (!prop) 1797 return -ENODEV; 1798 1799 mutex_lock(&of_mutex); 1800 1801 raw_spin_lock_irqsave(&devtree_lock, flags); 1802 rc = __of_remove_property(np, prop); 1803 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1804 1805 if (!rc) 1806 __of_remove_property_sysfs(np, prop); 1807 1808 mutex_unlock(&of_mutex); 1809 1810 if (!rc) 1811 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL); 1812 1813 return rc; 1814 } 1815 1816 int __of_update_property(struct device_node *np, struct property *newprop, 1817 struct property **oldpropp) 1818 { 1819 struct property **next, *oldprop; 1820 1821 for (next = &np->properties; *next; next = &(*next)->next) { 1822 if (of_prop_cmp((*next)->name, newprop->name) == 0) 1823 break; 1824 } 1825 *oldpropp = oldprop = *next; 1826 1827 if (oldprop) { 1828 /* replace the node */ 1829 newprop->next = oldprop->next; 1830 *next = newprop; 1831 oldprop->next = np->deadprops; 1832 np->deadprops = oldprop; 1833 } else { 1834 /* new node */ 1835 newprop->next = NULL; 1836 *next = newprop; 1837 } 1838 1839 return 0; 1840 } 1841 1842 /* 1843 * of_update_property - Update a property in a node, if the property does 1844 * not exist, add it. 1845 * 1846 * Note that we don't actually remove it, since we have given out 1847 * who-knows-how-many pointers to the data using get-property. 1848 * Instead we just move the property to the "dead properties" list, 1849 * and add the new property to the property list 1850 */ 1851 int of_update_property(struct device_node *np, struct property *newprop) 1852 { 1853 struct property *oldprop; 1854 unsigned long flags; 1855 int rc; 1856 1857 if (!newprop->name) 1858 return -EINVAL; 1859 1860 mutex_lock(&of_mutex); 1861 1862 raw_spin_lock_irqsave(&devtree_lock, flags); 1863 rc = __of_update_property(np, newprop, &oldprop); 1864 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1865 1866 if (!rc) 1867 __of_update_property_sysfs(np, newprop, oldprop); 1868 1869 mutex_unlock(&of_mutex); 1870 1871 if (!rc) 1872 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop); 1873 1874 return rc; 1875 } 1876 1877 static void of_alias_add(struct alias_prop *ap, struct device_node *np, 1878 int id, const char *stem, int stem_len) 1879 { 1880 ap->np = np; 1881 ap->id = id; 1882 strncpy(ap->stem, stem, stem_len); 1883 ap->stem[stem_len] = 0; 1884 list_add_tail(&ap->link, &aliases_lookup); 1885 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n", 1886 ap->alias, ap->stem, ap->id, np); 1887 } 1888 1889 /** 1890 * of_alias_scan - Scan all properties of the 'aliases' node 1891 * 1892 * The function scans all the properties of the 'aliases' node and populates 1893 * the global lookup table with the properties. It returns the 1894 * number of alias properties found, or an error code in case of failure. 1895 * 1896 * @dt_alloc: An allocator that provides a virtual address to memory 1897 * for storing the resulting tree 1898 */ 1899 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) 1900 { 1901 struct property *pp; 1902 1903 of_aliases = of_find_node_by_path("/aliases"); 1904 of_chosen = of_find_node_by_path("/chosen"); 1905 if (of_chosen == NULL) 1906 of_chosen = of_find_node_by_path("/chosen@0"); 1907 1908 if (of_chosen) { 1909 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */ 1910 const char *name = NULL; 1911 1912 if (of_property_read_string(of_chosen, "stdout-path", &name)) 1913 of_property_read_string(of_chosen, "linux,stdout-path", 1914 &name); 1915 if (IS_ENABLED(CONFIG_PPC) && !name) 1916 of_property_read_string(of_aliases, "stdout", &name); 1917 if (name) 1918 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options); 1919 } 1920 1921 if (!of_aliases) 1922 return; 1923 1924 for_each_property_of_node(of_aliases, pp) { 1925 const char *start = pp->name; 1926 const char *end = start + strlen(start); 1927 struct device_node *np; 1928 struct alias_prop *ap; 1929 int id, len; 1930 1931 /* Skip those we do not want to proceed */ 1932 if (!strcmp(pp->name, "name") || 1933 !strcmp(pp->name, "phandle") || 1934 !strcmp(pp->name, "linux,phandle")) 1935 continue; 1936 1937 np = of_find_node_by_path(pp->value); 1938 if (!np) 1939 continue; 1940 1941 /* walk the alias backwards to extract the id and work out 1942 * the 'stem' string */ 1943 while (isdigit(*(end-1)) && end > start) 1944 end--; 1945 len = end - start; 1946 1947 if (kstrtoint(end, 10, &id) < 0) 1948 continue; 1949 1950 /* Allocate an alias_prop with enough space for the stem */ 1951 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap)); 1952 if (!ap) 1953 continue; 1954 memset(ap, 0, sizeof(*ap) + len + 1); 1955 ap->alias = start; 1956 of_alias_add(ap, np, id, start, len); 1957 } 1958 } 1959 1960 /** 1961 * of_alias_get_id - Get alias id for the given device_node 1962 * @np: Pointer to the given device_node 1963 * @stem: Alias stem of the given device_node 1964 * 1965 * The function travels the lookup table to get the alias id for the given 1966 * device_node and alias stem. It returns the alias id if found. 1967 */ 1968 int of_alias_get_id(struct device_node *np, const char *stem) 1969 { 1970 struct alias_prop *app; 1971 int id = -ENODEV; 1972 1973 mutex_lock(&of_mutex); 1974 list_for_each_entry(app, &aliases_lookup, link) { 1975 if (strcmp(app->stem, stem) != 0) 1976 continue; 1977 1978 if (np == app->np) { 1979 id = app->id; 1980 break; 1981 } 1982 } 1983 mutex_unlock(&of_mutex); 1984 1985 return id; 1986 } 1987 EXPORT_SYMBOL_GPL(of_alias_get_id); 1988 1989 /** 1990 * of_alias_get_alias_list - Get alias list for the given device driver 1991 * @matches: Array of OF device match structures to search in 1992 * @stem: Alias stem of the given device_node 1993 * @bitmap: Bitmap field pointer 1994 * @nbits: Maximum number of alias IDs which can be recorded in bitmap 1995 * 1996 * The function travels the lookup table to record alias ids for the given 1997 * device match structures and alias stem. 1998 * 1999 * Return: 0 or -ENOSYS when !CONFIG_OF or 2000 * -EOVERFLOW if alias ID is greater then allocated nbits 2001 */ 2002 int of_alias_get_alias_list(const struct of_device_id *matches, 2003 const char *stem, unsigned long *bitmap, 2004 unsigned int nbits) 2005 { 2006 struct alias_prop *app; 2007 int ret = 0; 2008 2009 /* Zero bitmap field to make sure that all the time it is clean */ 2010 bitmap_zero(bitmap, nbits); 2011 2012 mutex_lock(&of_mutex); 2013 pr_debug("%s: Looking for stem: %s\n", __func__, stem); 2014 list_for_each_entry(app, &aliases_lookup, link) { 2015 pr_debug("%s: stem: %s, id: %d\n", 2016 __func__, app->stem, app->id); 2017 2018 if (strcmp(app->stem, stem) != 0) { 2019 pr_debug("%s: stem comparison didn't pass %s\n", 2020 __func__, app->stem); 2021 continue; 2022 } 2023 2024 if (of_match_node(matches, app->np)) { 2025 pr_debug("%s: Allocated ID %d\n", __func__, app->id); 2026 2027 if (app->id >= nbits) { 2028 pr_warn("%s: ID %d >= than bitmap field %d\n", 2029 __func__, app->id, nbits); 2030 ret = -EOVERFLOW; 2031 } else { 2032 set_bit(app->id, bitmap); 2033 } 2034 } 2035 } 2036 mutex_unlock(&of_mutex); 2037 2038 return ret; 2039 } 2040 EXPORT_SYMBOL_GPL(of_alias_get_alias_list); 2041 2042 /** 2043 * of_alias_get_highest_id - Get highest alias id for the given stem 2044 * @stem: Alias stem to be examined 2045 * 2046 * The function travels the lookup table to get the highest alias id for the 2047 * given alias stem. It returns the alias id if found. 2048 */ 2049 int of_alias_get_highest_id(const char *stem) 2050 { 2051 struct alias_prop *app; 2052 int id = -ENODEV; 2053 2054 mutex_lock(&of_mutex); 2055 list_for_each_entry(app, &aliases_lookup, link) { 2056 if (strcmp(app->stem, stem) != 0) 2057 continue; 2058 2059 if (app->id > id) 2060 id = app->id; 2061 } 2062 mutex_unlock(&of_mutex); 2063 2064 return id; 2065 } 2066 EXPORT_SYMBOL_GPL(of_alias_get_highest_id); 2067 2068 /** 2069 * of_console_check() - Test and setup console for DT setup 2070 * @dn - Pointer to device node 2071 * @name - Name to use for preferred console without index. ex. "ttyS" 2072 * @index - Index to use for preferred console. 2073 * 2074 * Check if the given device node matches the stdout-path property in the 2075 * /chosen node. If it does then register it as the preferred console and return 2076 * TRUE. Otherwise return FALSE. 2077 */ 2078 bool of_console_check(struct device_node *dn, char *name, int index) 2079 { 2080 if (!dn || dn != of_stdout || console_set_on_cmdline) 2081 return false; 2082 2083 /* 2084 * XXX: cast `options' to char pointer to suppress complication 2085 * warnings: printk, UART and console drivers expect char pointer. 2086 */ 2087 return !add_preferred_console(name, index, (char *)of_stdout_options); 2088 } 2089 EXPORT_SYMBOL_GPL(of_console_check); 2090 2091 /** 2092 * of_find_next_cache_node - Find a node's subsidiary cache 2093 * @np: node of type "cpu" or "cache" 2094 * 2095 * Returns a node pointer with refcount incremented, use 2096 * of_node_put() on it when done. Caller should hold a reference 2097 * to np. 2098 */ 2099 struct device_node *of_find_next_cache_node(const struct device_node *np) 2100 { 2101 struct device_node *child, *cache_node; 2102 2103 cache_node = of_parse_phandle(np, "l2-cache", 0); 2104 if (!cache_node) 2105 cache_node = of_parse_phandle(np, "next-level-cache", 0); 2106 2107 if (cache_node) 2108 return cache_node; 2109 2110 /* OF on pmac has nodes instead of properties named "l2-cache" 2111 * beneath CPU nodes. 2112 */ 2113 if (IS_ENABLED(CONFIG_PPC_PMAC) && !strcmp(np->type, "cpu")) 2114 for_each_child_of_node(np, child) 2115 if (!strcmp(child->type, "cache")) 2116 return child; 2117 2118 return NULL; 2119 } 2120 2121 /** 2122 * of_find_last_cache_level - Find the level at which the last cache is 2123 * present for the given logical cpu 2124 * 2125 * @cpu: cpu number(logical index) for which the last cache level is needed 2126 * 2127 * Returns the the level at which the last cache is present. It is exactly 2128 * same as the total number of cache levels for the given logical cpu. 2129 */ 2130 int of_find_last_cache_level(unsigned int cpu) 2131 { 2132 u32 cache_level = 0; 2133 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu); 2134 2135 while (np) { 2136 prev = np; 2137 of_node_put(np); 2138 np = of_find_next_cache_node(np); 2139 } 2140 2141 of_property_read_u32(prev, "cache-level", &cache_level); 2142 2143 return cache_level; 2144 } 2145 2146 /** 2147 * of_map_rid - Translate a requester ID through a downstream mapping. 2148 * @np: root complex device node. 2149 * @rid: device requester ID to map. 2150 * @map_name: property name of the map to use. 2151 * @map_mask_name: optional property name of the mask to use. 2152 * @target: optional pointer to a target device node. 2153 * @id_out: optional pointer to receive the translated ID. 2154 * 2155 * Given a device requester ID, look up the appropriate implementation-defined 2156 * platform ID and/or the target device which receives transactions on that 2157 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or 2158 * @id_out may be NULL if only the other is required. If @target points to 2159 * a non-NULL device node pointer, only entries targeting that node will be 2160 * matched; if it points to a NULL value, it will receive the device node of 2161 * the first matching target phandle, with a reference held. 2162 * 2163 * Return: 0 on success or a standard error code on failure. 2164 */ 2165 int of_map_rid(struct device_node *np, u32 rid, 2166 const char *map_name, const char *map_mask_name, 2167 struct device_node **target, u32 *id_out) 2168 { 2169 u32 map_mask, masked_rid; 2170 int map_len; 2171 const __be32 *map = NULL; 2172 2173 if (!np || !map_name || (!target && !id_out)) 2174 return -EINVAL; 2175 2176 map = of_get_property(np, map_name, &map_len); 2177 if (!map) { 2178 if (target) 2179 return -ENODEV; 2180 /* Otherwise, no map implies no translation */ 2181 *id_out = rid; 2182 return 0; 2183 } 2184 2185 if (!map_len || map_len % (4 * sizeof(*map))) { 2186 pr_err("%pOF: Error: Bad %s length: %d\n", np, 2187 map_name, map_len); 2188 return -EINVAL; 2189 } 2190 2191 /* The default is to select all bits. */ 2192 map_mask = 0xffffffff; 2193 2194 /* 2195 * Can be overridden by "{iommu,msi}-map-mask" property. 2196 * If of_property_read_u32() fails, the default is used. 2197 */ 2198 if (map_mask_name) 2199 of_property_read_u32(np, map_mask_name, &map_mask); 2200 2201 masked_rid = map_mask & rid; 2202 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { 2203 struct device_node *phandle_node; 2204 u32 rid_base = be32_to_cpup(map + 0); 2205 u32 phandle = be32_to_cpup(map + 1); 2206 u32 out_base = be32_to_cpup(map + 2); 2207 u32 rid_len = be32_to_cpup(map + 3); 2208 2209 if (rid_base & ~map_mask) { 2210 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", 2211 np, map_name, map_name, 2212 map_mask, rid_base); 2213 return -EFAULT; 2214 } 2215 2216 if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) 2217 continue; 2218 2219 phandle_node = of_find_node_by_phandle(phandle); 2220 if (!phandle_node) 2221 return -ENODEV; 2222 2223 if (target) { 2224 if (*target) 2225 of_node_put(phandle_node); 2226 else 2227 *target = phandle_node; 2228 2229 if (*target != phandle_node) 2230 continue; 2231 } 2232 2233 if (id_out) 2234 *id_out = masked_rid - rid_base + out_base; 2235 2236 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", 2237 np, map_name, map_mask, rid_base, out_base, 2238 rid_len, rid, masked_rid - rid_base + out_base); 2239 return 0; 2240 } 2241 2242 pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n", 2243 np, map_name, rid, target && *target ? *target : NULL); 2244 return -EFAULT; 2245 } 2246 EXPORT_SYMBOL_GPL(of_map_rid); 2247