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_node_get(next)) 781 break; 782 } 783 of_node_put(prev); 784 raw_spin_unlock_irqrestore(&devtree_lock, flags); 785 return next; 786 } 787 EXPORT_SYMBOL(of_get_next_cpu_node); 788 789 /** 790 * of_get_compatible_child - Find compatible child node 791 * @parent: parent node 792 * @compatible: compatible string 793 * 794 * Lookup child node whose compatible property contains the given compatible 795 * string. 796 * 797 * Returns a node pointer with refcount incremented, use of_node_put() on it 798 * when done; or NULL if not found. 799 */ 800 struct device_node *of_get_compatible_child(const struct device_node *parent, 801 const char *compatible) 802 { 803 struct device_node *child; 804 805 for_each_child_of_node(parent, child) { 806 if (of_device_is_compatible(child, compatible)) 807 break; 808 } 809 810 return child; 811 } 812 EXPORT_SYMBOL(of_get_compatible_child); 813 814 /** 815 * of_get_child_by_name - Find the child node by name for a given parent 816 * @node: parent node 817 * @name: child name to look for. 818 * 819 * This function looks for child node for given matching name 820 * 821 * Returns a node pointer if found, with refcount incremented, use 822 * of_node_put() on it when done. 823 * Returns NULL if node is not found. 824 */ 825 struct device_node *of_get_child_by_name(const struct device_node *node, 826 const char *name) 827 { 828 struct device_node *child; 829 830 for_each_child_of_node(node, child) 831 if (child->name && (of_node_cmp(child->name, name) == 0)) 832 break; 833 return child; 834 } 835 EXPORT_SYMBOL(of_get_child_by_name); 836 837 struct device_node *__of_find_node_by_path(struct device_node *parent, 838 const char *path) 839 { 840 struct device_node *child; 841 int len; 842 843 len = strcspn(path, "/:"); 844 if (!len) 845 return NULL; 846 847 __for_each_child_of_node(parent, child) { 848 const char *name = kbasename(child->full_name); 849 if (strncmp(path, name, len) == 0 && (strlen(name) == len)) 850 return child; 851 } 852 return NULL; 853 } 854 855 struct device_node *__of_find_node_by_full_path(struct device_node *node, 856 const char *path) 857 { 858 const char *separator = strchr(path, ':'); 859 860 while (node && *path == '/') { 861 struct device_node *tmp = node; 862 863 path++; /* Increment past '/' delimiter */ 864 node = __of_find_node_by_path(node, path); 865 of_node_put(tmp); 866 path = strchrnul(path, '/'); 867 if (separator && separator < path) 868 break; 869 } 870 return node; 871 } 872 873 /** 874 * of_find_node_opts_by_path - Find a node matching a full OF path 875 * @path: Either the full path to match, or if the path does not 876 * start with '/', the name of a property of the /aliases 877 * node (an alias). In the case of an alias, the node 878 * matching the alias' value will be returned. 879 * @opts: Address of a pointer into which to store the start of 880 * an options string appended to the end of the path with 881 * a ':' separator. 882 * 883 * Valid paths: 884 * /foo/bar Full path 885 * foo Valid alias 886 * foo/bar Valid alias + relative path 887 * 888 * Returns a node pointer with refcount incremented, use 889 * of_node_put() on it when done. 890 */ 891 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts) 892 { 893 struct device_node *np = NULL; 894 struct property *pp; 895 unsigned long flags; 896 const char *separator = strchr(path, ':'); 897 898 if (opts) 899 *opts = separator ? separator + 1 : NULL; 900 901 if (strcmp(path, "/") == 0) 902 return of_node_get(of_root); 903 904 /* The path could begin with an alias */ 905 if (*path != '/') { 906 int len; 907 const char *p = separator; 908 909 if (!p) 910 p = strchrnul(path, '/'); 911 len = p - path; 912 913 /* of_aliases must not be NULL */ 914 if (!of_aliases) 915 return NULL; 916 917 for_each_property_of_node(of_aliases, pp) { 918 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) { 919 np = of_find_node_by_path(pp->value); 920 break; 921 } 922 } 923 if (!np) 924 return NULL; 925 path = p; 926 } 927 928 /* Step down the tree matching path components */ 929 raw_spin_lock_irqsave(&devtree_lock, flags); 930 if (!np) 931 np = of_node_get(of_root); 932 np = __of_find_node_by_full_path(np, path); 933 raw_spin_unlock_irqrestore(&devtree_lock, flags); 934 return np; 935 } 936 EXPORT_SYMBOL(of_find_node_opts_by_path); 937 938 /** 939 * of_find_node_by_name - Find a node by its "name" property 940 * @from: The node to start searching from or NULL; the node 941 * you pass will not be searched, only the next one 942 * will. Typically, you pass what the previous call 943 * returned. of_node_put() will be called on @from. 944 * @name: The name string to match against 945 * 946 * Returns a node pointer with refcount incremented, use 947 * of_node_put() on it when done. 948 */ 949 struct device_node *of_find_node_by_name(struct device_node *from, 950 const char *name) 951 { 952 struct device_node *np; 953 unsigned long flags; 954 955 raw_spin_lock_irqsave(&devtree_lock, flags); 956 for_each_of_allnodes_from(from, np) 957 if (np->name && (of_node_cmp(np->name, name) == 0) 958 && of_node_get(np)) 959 break; 960 of_node_put(from); 961 raw_spin_unlock_irqrestore(&devtree_lock, flags); 962 return np; 963 } 964 EXPORT_SYMBOL(of_find_node_by_name); 965 966 /** 967 * of_find_node_by_type - Find a node by its "device_type" property 968 * @from: The node to start searching from, or NULL to start searching 969 * the entire device tree. The node you pass will not be 970 * searched, only the next one will; typically, you pass 971 * what the previous call returned. of_node_put() will be 972 * called on from for you. 973 * @type: The type string to match against 974 * 975 * Returns a node pointer with refcount incremented, use 976 * of_node_put() on it when done. 977 */ 978 struct device_node *of_find_node_by_type(struct device_node *from, 979 const char *type) 980 { 981 struct device_node *np; 982 unsigned long flags; 983 984 raw_spin_lock_irqsave(&devtree_lock, flags); 985 for_each_of_allnodes_from(from, np) 986 if (np->type && (of_node_cmp(np->type, type) == 0) 987 && of_node_get(np)) 988 break; 989 of_node_put(from); 990 raw_spin_unlock_irqrestore(&devtree_lock, flags); 991 return np; 992 } 993 EXPORT_SYMBOL(of_find_node_by_type); 994 995 /** 996 * of_find_compatible_node - Find a node based on type and one of the 997 * tokens in its "compatible" property 998 * @from: The node to start searching from or NULL, the node 999 * you pass will not be searched, only the next one 1000 * will; typically, you pass what the previous call 1001 * returned. of_node_put() will be called on it 1002 * @type: The type string to match "device_type" or NULL to ignore 1003 * @compatible: The string to match to one of the tokens in the device 1004 * "compatible" list. 1005 * 1006 * Returns a node pointer with refcount incremented, use 1007 * of_node_put() on it when done. 1008 */ 1009 struct device_node *of_find_compatible_node(struct device_node *from, 1010 const char *type, const char *compatible) 1011 { 1012 struct device_node *np; 1013 unsigned long flags; 1014 1015 raw_spin_lock_irqsave(&devtree_lock, flags); 1016 for_each_of_allnodes_from(from, np) 1017 if (__of_device_is_compatible(np, compatible, type, NULL) && 1018 of_node_get(np)) 1019 break; 1020 of_node_put(from); 1021 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1022 return np; 1023 } 1024 EXPORT_SYMBOL(of_find_compatible_node); 1025 1026 /** 1027 * of_find_node_with_property - Find a node which has a property with 1028 * the given name. 1029 * @from: The node to start searching from or NULL, the node 1030 * you pass will not be searched, only the next one 1031 * will; typically, you pass what the previous call 1032 * returned. of_node_put() will be called on it 1033 * @prop_name: The name of the property to look for. 1034 * 1035 * Returns a node pointer with refcount incremented, use 1036 * of_node_put() on it when done. 1037 */ 1038 struct device_node *of_find_node_with_property(struct device_node *from, 1039 const char *prop_name) 1040 { 1041 struct device_node *np; 1042 struct property *pp; 1043 unsigned long flags; 1044 1045 raw_spin_lock_irqsave(&devtree_lock, flags); 1046 for_each_of_allnodes_from(from, np) { 1047 for (pp = np->properties; pp; pp = pp->next) { 1048 if (of_prop_cmp(pp->name, prop_name) == 0) { 1049 of_node_get(np); 1050 goto out; 1051 } 1052 } 1053 } 1054 out: 1055 of_node_put(from); 1056 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1057 return np; 1058 } 1059 EXPORT_SYMBOL(of_find_node_with_property); 1060 1061 static 1062 const struct of_device_id *__of_match_node(const struct of_device_id *matches, 1063 const struct device_node *node) 1064 { 1065 const struct of_device_id *best_match = NULL; 1066 int score, best_score = 0; 1067 1068 if (!matches) 1069 return NULL; 1070 1071 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { 1072 score = __of_device_is_compatible(node, matches->compatible, 1073 matches->type, matches->name); 1074 if (score > best_score) { 1075 best_match = matches; 1076 best_score = score; 1077 } 1078 } 1079 1080 return best_match; 1081 } 1082 1083 /** 1084 * of_match_node - Tell if a device_node has a matching of_match structure 1085 * @matches: array of of device match structures to search in 1086 * @node: the of device structure to match against 1087 * 1088 * Low level utility function used by device matching. 1089 */ 1090 const struct of_device_id *of_match_node(const struct of_device_id *matches, 1091 const struct device_node *node) 1092 { 1093 const struct of_device_id *match; 1094 unsigned long flags; 1095 1096 raw_spin_lock_irqsave(&devtree_lock, flags); 1097 match = __of_match_node(matches, node); 1098 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1099 return match; 1100 } 1101 EXPORT_SYMBOL(of_match_node); 1102 1103 /** 1104 * of_find_matching_node_and_match - Find a node based on an of_device_id 1105 * match table. 1106 * @from: The node to start searching from or NULL, the node 1107 * you pass will not be searched, only the next one 1108 * will; typically, you pass what the previous call 1109 * returned. of_node_put() will be called on it 1110 * @matches: array of of device match structures to search in 1111 * @match Updated to point at the matches entry which matched 1112 * 1113 * Returns a node pointer with refcount incremented, use 1114 * of_node_put() on it when done. 1115 */ 1116 struct device_node *of_find_matching_node_and_match(struct device_node *from, 1117 const struct of_device_id *matches, 1118 const struct of_device_id **match) 1119 { 1120 struct device_node *np; 1121 const struct of_device_id *m; 1122 unsigned long flags; 1123 1124 if (match) 1125 *match = NULL; 1126 1127 raw_spin_lock_irqsave(&devtree_lock, flags); 1128 for_each_of_allnodes_from(from, np) { 1129 m = __of_match_node(matches, np); 1130 if (m && of_node_get(np)) { 1131 if (match) 1132 *match = m; 1133 break; 1134 } 1135 } 1136 of_node_put(from); 1137 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1138 return np; 1139 } 1140 EXPORT_SYMBOL(of_find_matching_node_and_match); 1141 1142 /** 1143 * of_modalias_node - Lookup appropriate modalias for a device node 1144 * @node: pointer to a device tree node 1145 * @modalias: Pointer to buffer that modalias value will be copied into 1146 * @len: Length of modalias value 1147 * 1148 * Based on the value of the compatible property, this routine will attempt 1149 * to choose an appropriate modalias value for a particular device tree node. 1150 * It does this by stripping the manufacturer prefix (as delimited by a ',') 1151 * from the first entry in the compatible list property. 1152 * 1153 * This routine returns 0 on success, <0 on failure. 1154 */ 1155 int of_modalias_node(struct device_node *node, char *modalias, int len) 1156 { 1157 const char *compatible, *p; 1158 int cplen; 1159 1160 compatible = of_get_property(node, "compatible", &cplen); 1161 if (!compatible || strlen(compatible) > cplen) 1162 return -ENODEV; 1163 p = strchr(compatible, ','); 1164 strlcpy(modalias, p ? p + 1 : compatible, len); 1165 return 0; 1166 } 1167 EXPORT_SYMBOL_GPL(of_modalias_node); 1168 1169 /** 1170 * of_find_node_by_phandle - Find a node given a phandle 1171 * @handle: phandle of the node to find 1172 * 1173 * Returns a node pointer with refcount incremented, use 1174 * of_node_put() on it when done. 1175 */ 1176 struct device_node *of_find_node_by_phandle(phandle handle) 1177 { 1178 struct device_node *np = NULL; 1179 unsigned long flags; 1180 phandle masked_handle; 1181 1182 if (!handle) 1183 return NULL; 1184 1185 raw_spin_lock_irqsave(&devtree_lock, flags); 1186 1187 masked_handle = handle & phandle_cache_mask; 1188 1189 if (phandle_cache) { 1190 if (phandle_cache[masked_handle] && 1191 handle == phandle_cache[masked_handle]->phandle) 1192 np = phandle_cache[masked_handle]; 1193 } 1194 1195 if (!np) { 1196 for_each_of_allnodes(np) 1197 if (np->phandle == handle) { 1198 if (phandle_cache) 1199 phandle_cache[masked_handle] = np; 1200 break; 1201 } 1202 } 1203 1204 of_node_get(np); 1205 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1206 return np; 1207 } 1208 EXPORT_SYMBOL(of_find_node_by_phandle); 1209 1210 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args) 1211 { 1212 int i; 1213 printk("%s %pOF", msg, args->np); 1214 for (i = 0; i < args->args_count; i++) { 1215 const char delim = i ? ',' : ':'; 1216 1217 pr_cont("%c%08x", delim, args->args[i]); 1218 } 1219 pr_cont("\n"); 1220 } 1221 1222 int of_phandle_iterator_init(struct of_phandle_iterator *it, 1223 const struct device_node *np, 1224 const char *list_name, 1225 const char *cells_name, 1226 int cell_count) 1227 { 1228 const __be32 *list; 1229 int size; 1230 1231 memset(it, 0, sizeof(*it)); 1232 1233 list = of_get_property(np, list_name, &size); 1234 if (!list) 1235 return -ENOENT; 1236 1237 it->cells_name = cells_name; 1238 it->cell_count = cell_count; 1239 it->parent = np; 1240 it->list_end = list + size / sizeof(*list); 1241 it->phandle_end = list; 1242 it->cur = list; 1243 1244 return 0; 1245 } 1246 EXPORT_SYMBOL_GPL(of_phandle_iterator_init); 1247 1248 int of_phandle_iterator_next(struct of_phandle_iterator *it) 1249 { 1250 uint32_t count = 0; 1251 1252 if (it->node) { 1253 of_node_put(it->node); 1254 it->node = NULL; 1255 } 1256 1257 if (!it->cur || it->phandle_end >= it->list_end) 1258 return -ENOENT; 1259 1260 it->cur = it->phandle_end; 1261 1262 /* If phandle is 0, then it is an empty entry with no arguments. */ 1263 it->phandle = be32_to_cpup(it->cur++); 1264 1265 if (it->phandle) { 1266 1267 /* 1268 * Find the provider node and parse the #*-cells property to 1269 * determine the argument length. 1270 */ 1271 it->node = of_find_node_by_phandle(it->phandle); 1272 1273 if (it->cells_name) { 1274 if (!it->node) { 1275 pr_err("%pOF: could not find phandle\n", 1276 it->parent); 1277 goto err; 1278 } 1279 1280 if (of_property_read_u32(it->node, it->cells_name, 1281 &count)) { 1282 pr_err("%pOF: could not get %s for %pOF\n", 1283 it->parent, 1284 it->cells_name, 1285 it->node); 1286 goto err; 1287 } 1288 } else { 1289 count = it->cell_count; 1290 } 1291 1292 /* 1293 * Make sure that the arguments actually fit in the remaining 1294 * property data length 1295 */ 1296 if (it->cur + count > it->list_end) { 1297 pr_err("%pOF: arguments longer than property\n", 1298 it->parent); 1299 goto err; 1300 } 1301 } 1302 1303 it->phandle_end = it->cur + count; 1304 it->cur_count = count; 1305 1306 return 0; 1307 1308 err: 1309 if (it->node) { 1310 of_node_put(it->node); 1311 it->node = NULL; 1312 } 1313 1314 return -EINVAL; 1315 } 1316 EXPORT_SYMBOL_GPL(of_phandle_iterator_next); 1317 1318 int of_phandle_iterator_args(struct of_phandle_iterator *it, 1319 uint32_t *args, 1320 int size) 1321 { 1322 int i, count; 1323 1324 count = it->cur_count; 1325 1326 if (WARN_ON(size < count)) 1327 count = size; 1328 1329 for (i = 0; i < count; i++) 1330 args[i] = be32_to_cpup(it->cur++); 1331 1332 return count; 1333 } 1334 1335 static int __of_parse_phandle_with_args(const struct device_node *np, 1336 const char *list_name, 1337 const char *cells_name, 1338 int cell_count, int index, 1339 struct of_phandle_args *out_args) 1340 { 1341 struct of_phandle_iterator it; 1342 int rc, cur_index = 0; 1343 1344 /* Loop over the phandles until all the requested entry is found */ 1345 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) { 1346 /* 1347 * All of the error cases bail out of the loop, so at 1348 * this point, the parsing is successful. If the requested 1349 * index matches, then fill the out_args structure and return, 1350 * or return -ENOENT for an empty entry. 1351 */ 1352 rc = -ENOENT; 1353 if (cur_index == index) { 1354 if (!it.phandle) 1355 goto err; 1356 1357 if (out_args) { 1358 int c; 1359 1360 c = of_phandle_iterator_args(&it, 1361 out_args->args, 1362 MAX_PHANDLE_ARGS); 1363 out_args->np = it.node; 1364 out_args->args_count = c; 1365 } else { 1366 of_node_put(it.node); 1367 } 1368 1369 /* Found it! return success */ 1370 return 0; 1371 } 1372 1373 cur_index++; 1374 } 1375 1376 /* 1377 * Unlock node before returning result; will be one of: 1378 * -ENOENT : index is for empty phandle 1379 * -EINVAL : parsing error on data 1380 */ 1381 1382 err: 1383 of_node_put(it.node); 1384 return rc; 1385 } 1386 1387 /** 1388 * of_parse_phandle - Resolve a phandle property to a device_node pointer 1389 * @np: Pointer to device node holding phandle property 1390 * @phandle_name: Name of property holding a phandle value 1391 * @index: For properties holding a table of phandles, this is the index into 1392 * the table 1393 * 1394 * Returns the device_node pointer with refcount incremented. Use 1395 * of_node_put() on it when done. 1396 */ 1397 struct device_node *of_parse_phandle(const struct device_node *np, 1398 const char *phandle_name, int index) 1399 { 1400 struct of_phandle_args args; 1401 1402 if (index < 0) 1403 return NULL; 1404 1405 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, 1406 index, &args)) 1407 return NULL; 1408 1409 return args.np; 1410 } 1411 EXPORT_SYMBOL(of_parse_phandle); 1412 1413 /** 1414 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list 1415 * @np: pointer to a device tree node containing a list 1416 * @list_name: property name that contains a list 1417 * @cells_name: property name that specifies phandles' arguments count 1418 * @index: index of a phandle to parse out 1419 * @out_args: optional pointer to output arguments structure (will be filled) 1420 * 1421 * This function is useful to parse lists of phandles and their arguments. 1422 * Returns 0 on success and fills out_args, on error returns appropriate 1423 * errno value. 1424 * 1425 * Caller is responsible to call of_node_put() on the returned out_args->np 1426 * pointer. 1427 * 1428 * Example: 1429 * 1430 * phandle1: node1 { 1431 * #list-cells = <2>; 1432 * } 1433 * 1434 * phandle2: node2 { 1435 * #list-cells = <1>; 1436 * } 1437 * 1438 * node3 { 1439 * list = <&phandle1 1 2 &phandle2 3>; 1440 * } 1441 * 1442 * To get a device_node of the `node2' node you may call this: 1443 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); 1444 */ 1445 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, 1446 const char *cells_name, int index, 1447 struct of_phandle_args *out_args) 1448 { 1449 if (index < 0) 1450 return -EINVAL; 1451 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, 1452 index, out_args); 1453 } 1454 EXPORT_SYMBOL(of_parse_phandle_with_args); 1455 1456 /** 1457 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it 1458 * @np: pointer to a device tree node containing a list 1459 * @list_name: property name that contains a list 1460 * @stem_name: stem of property names that specify phandles' arguments count 1461 * @index: index of a phandle to parse out 1462 * @out_args: optional pointer to output arguments structure (will be filled) 1463 * 1464 * This function is useful to parse lists of phandles and their arguments. 1465 * Returns 0 on success and fills out_args, on error returns appropriate errno 1466 * value. The difference between this function and of_parse_phandle_with_args() 1467 * is that this API remaps a phandle if the node the phandle points to has 1468 * a <@stem_name>-map property. 1469 * 1470 * Caller is responsible to call of_node_put() on the returned out_args->np 1471 * pointer. 1472 * 1473 * Example: 1474 * 1475 * phandle1: node1 { 1476 * #list-cells = <2>; 1477 * } 1478 * 1479 * phandle2: node2 { 1480 * #list-cells = <1>; 1481 * } 1482 * 1483 * phandle3: node3 { 1484 * #list-cells = <1>; 1485 * list-map = <0 &phandle2 3>, 1486 * <1 &phandle2 2>, 1487 * <2 &phandle1 5 1>; 1488 * list-map-mask = <0x3>; 1489 * }; 1490 * 1491 * node4 { 1492 * list = <&phandle1 1 2 &phandle3 0>; 1493 * } 1494 * 1495 * To get a device_node of the `node2' node you may call this: 1496 * of_parse_phandle_with_args(node4, "list", "list", 1, &args); 1497 */ 1498 int of_parse_phandle_with_args_map(const struct device_node *np, 1499 const char *list_name, 1500 const char *stem_name, 1501 int index, struct of_phandle_args *out_args) 1502 { 1503 char *cells_name, *map_name = NULL, *mask_name = NULL; 1504 char *pass_name = NULL; 1505 struct device_node *cur, *new = NULL; 1506 const __be32 *map, *mask, *pass; 1507 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 }; 1508 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 }; 1509 __be32 initial_match_array[MAX_PHANDLE_ARGS]; 1510 const __be32 *match_array = initial_match_array; 1511 int i, ret, map_len, match; 1512 u32 list_size, new_size; 1513 1514 if (index < 0) 1515 return -EINVAL; 1516 1517 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name); 1518 if (!cells_name) 1519 return -ENOMEM; 1520 1521 ret = -ENOMEM; 1522 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name); 1523 if (!map_name) 1524 goto free; 1525 1526 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); 1527 if (!mask_name) 1528 goto free; 1529 1530 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name); 1531 if (!pass_name) 1532 goto free; 1533 1534 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index, 1535 out_args); 1536 if (ret) 1537 goto free; 1538 1539 /* Get the #<list>-cells property */ 1540 cur = out_args->np; 1541 ret = of_property_read_u32(cur, cells_name, &list_size); 1542 if (ret < 0) 1543 goto put; 1544 1545 /* Precalculate the match array - this simplifies match loop */ 1546 for (i = 0; i < list_size; i++) 1547 initial_match_array[i] = cpu_to_be32(out_args->args[i]); 1548 1549 ret = -EINVAL; 1550 while (cur) { 1551 /* Get the <list>-map property */ 1552 map = of_get_property(cur, map_name, &map_len); 1553 if (!map) { 1554 ret = 0; 1555 goto free; 1556 } 1557 map_len /= sizeof(u32); 1558 1559 /* Get the <list>-map-mask property (optional) */ 1560 mask = of_get_property(cur, mask_name, NULL); 1561 if (!mask) 1562 mask = dummy_mask; 1563 /* Iterate through <list>-map property */ 1564 match = 0; 1565 while (map_len > (list_size + 1) && !match) { 1566 /* Compare specifiers */ 1567 match = 1; 1568 for (i = 0; i < list_size; i++, map_len--) 1569 match &= !((match_array[i] ^ *map++) & mask[i]); 1570 1571 of_node_put(new); 1572 new = of_find_node_by_phandle(be32_to_cpup(map)); 1573 map++; 1574 map_len--; 1575 1576 /* Check if not found */ 1577 if (!new) 1578 goto put; 1579 1580 if (!of_device_is_available(new)) 1581 match = 0; 1582 1583 ret = of_property_read_u32(new, cells_name, &new_size); 1584 if (ret) 1585 goto put; 1586 1587 /* Check for malformed properties */ 1588 if (WARN_ON(new_size > MAX_PHANDLE_ARGS)) 1589 goto put; 1590 if (map_len < new_size) 1591 goto put; 1592 1593 /* Move forward by new node's #<list>-cells amount */ 1594 map += new_size; 1595 map_len -= new_size; 1596 } 1597 if (!match) 1598 goto put; 1599 1600 /* Get the <list>-map-pass-thru property (optional) */ 1601 pass = of_get_property(cur, pass_name, NULL); 1602 if (!pass) 1603 pass = dummy_pass; 1604 1605 /* 1606 * Successfully parsed a <list>-map translation; copy new 1607 * specifier into the out_args structure, keeping the 1608 * bits specified in <list>-map-pass-thru. 1609 */ 1610 match_array = map - new_size; 1611 for (i = 0; i < new_size; i++) { 1612 __be32 val = *(map - new_size + i); 1613 1614 if (i < list_size) { 1615 val &= ~pass[i]; 1616 val |= cpu_to_be32(out_args->args[i]) & pass[i]; 1617 } 1618 1619 out_args->args[i] = be32_to_cpu(val); 1620 } 1621 out_args->args_count = list_size = new_size; 1622 /* Iterate again with new provider */ 1623 out_args->np = new; 1624 of_node_put(cur); 1625 cur = new; 1626 } 1627 put: 1628 of_node_put(cur); 1629 of_node_put(new); 1630 free: 1631 kfree(mask_name); 1632 kfree(map_name); 1633 kfree(cells_name); 1634 kfree(pass_name); 1635 1636 return ret; 1637 } 1638 EXPORT_SYMBOL(of_parse_phandle_with_args_map); 1639 1640 /** 1641 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list 1642 * @np: pointer to a device tree node containing a list 1643 * @list_name: property name that contains a list 1644 * @cell_count: number of argument cells following the phandle 1645 * @index: index of a phandle to parse out 1646 * @out_args: optional pointer to output arguments structure (will be filled) 1647 * 1648 * This function is useful to parse lists of phandles and their arguments. 1649 * Returns 0 on success and fills out_args, on error returns appropriate 1650 * errno value. 1651 * 1652 * Caller is responsible to call of_node_put() on the returned out_args->np 1653 * pointer. 1654 * 1655 * Example: 1656 * 1657 * phandle1: node1 { 1658 * } 1659 * 1660 * phandle2: node2 { 1661 * } 1662 * 1663 * node3 { 1664 * list = <&phandle1 0 2 &phandle2 2 3>; 1665 * } 1666 * 1667 * To get a device_node of the `node2' node you may call this: 1668 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); 1669 */ 1670 int of_parse_phandle_with_fixed_args(const struct device_node *np, 1671 const char *list_name, int cell_count, 1672 int index, struct of_phandle_args *out_args) 1673 { 1674 if (index < 0) 1675 return -EINVAL; 1676 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, 1677 index, out_args); 1678 } 1679 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args); 1680 1681 /** 1682 * of_count_phandle_with_args() - Find the number of phandles references in a property 1683 * @np: pointer to a device tree node containing a list 1684 * @list_name: property name that contains a list 1685 * @cells_name: property name that specifies phandles' arguments count 1686 * 1687 * Returns the number of phandle + argument tuples within a property. It 1688 * is a typical pattern to encode a list of phandle and variable 1689 * arguments into a single property. The number of arguments is encoded 1690 * by a property in the phandle-target node. For example, a gpios 1691 * property would contain a list of GPIO specifies consisting of a 1692 * phandle and 1 or more arguments. The number of arguments are 1693 * determined by the #gpio-cells property in the node pointed to by the 1694 * phandle. 1695 */ 1696 int of_count_phandle_with_args(const struct device_node *np, const char *list_name, 1697 const char *cells_name) 1698 { 1699 struct of_phandle_iterator it; 1700 int rc, cur_index = 0; 1701 1702 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0); 1703 if (rc) 1704 return rc; 1705 1706 while ((rc = of_phandle_iterator_next(&it)) == 0) 1707 cur_index += 1; 1708 1709 if (rc != -ENOENT) 1710 return rc; 1711 1712 return cur_index; 1713 } 1714 EXPORT_SYMBOL(of_count_phandle_with_args); 1715 1716 /** 1717 * __of_add_property - Add a property to a node without lock operations 1718 */ 1719 int __of_add_property(struct device_node *np, struct property *prop) 1720 { 1721 struct property **next; 1722 1723 prop->next = NULL; 1724 next = &np->properties; 1725 while (*next) { 1726 if (strcmp(prop->name, (*next)->name) == 0) 1727 /* duplicate ! don't insert it */ 1728 return -EEXIST; 1729 1730 next = &(*next)->next; 1731 } 1732 *next = prop; 1733 1734 return 0; 1735 } 1736 1737 /** 1738 * of_add_property - Add a property to a node 1739 */ 1740 int of_add_property(struct device_node *np, struct property *prop) 1741 { 1742 unsigned long flags; 1743 int rc; 1744 1745 mutex_lock(&of_mutex); 1746 1747 raw_spin_lock_irqsave(&devtree_lock, flags); 1748 rc = __of_add_property(np, prop); 1749 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1750 1751 if (!rc) 1752 __of_add_property_sysfs(np, prop); 1753 1754 mutex_unlock(&of_mutex); 1755 1756 if (!rc) 1757 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL); 1758 1759 return rc; 1760 } 1761 1762 int __of_remove_property(struct device_node *np, struct property *prop) 1763 { 1764 struct property **next; 1765 1766 for (next = &np->properties; *next; next = &(*next)->next) { 1767 if (*next == prop) 1768 break; 1769 } 1770 if (*next == NULL) 1771 return -ENODEV; 1772 1773 /* found the node */ 1774 *next = prop->next; 1775 prop->next = np->deadprops; 1776 np->deadprops = prop; 1777 1778 return 0; 1779 } 1780 1781 /** 1782 * of_remove_property - Remove a property from a node. 1783 * 1784 * Note that we don't actually remove it, since we have given out 1785 * who-knows-how-many pointers to the data using get-property. 1786 * Instead we just move the property to the "dead properties" 1787 * list, so it won't be found any more. 1788 */ 1789 int of_remove_property(struct device_node *np, struct property *prop) 1790 { 1791 unsigned long flags; 1792 int rc; 1793 1794 if (!prop) 1795 return -ENODEV; 1796 1797 mutex_lock(&of_mutex); 1798 1799 raw_spin_lock_irqsave(&devtree_lock, flags); 1800 rc = __of_remove_property(np, prop); 1801 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1802 1803 if (!rc) 1804 __of_remove_property_sysfs(np, prop); 1805 1806 mutex_unlock(&of_mutex); 1807 1808 if (!rc) 1809 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL); 1810 1811 return rc; 1812 } 1813 1814 int __of_update_property(struct device_node *np, struct property *newprop, 1815 struct property **oldpropp) 1816 { 1817 struct property **next, *oldprop; 1818 1819 for (next = &np->properties; *next; next = &(*next)->next) { 1820 if (of_prop_cmp((*next)->name, newprop->name) == 0) 1821 break; 1822 } 1823 *oldpropp = oldprop = *next; 1824 1825 if (oldprop) { 1826 /* replace the node */ 1827 newprop->next = oldprop->next; 1828 *next = newprop; 1829 oldprop->next = np->deadprops; 1830 np->deadprops = oldprop; 1831 } else { 1832 /* new node */ 1833 newprop->next = NULL; 1834 *next = newprop; 1835 } 1836 1837 return 0; 1838 } 1839 1840 /* 1841 * of_update_property - Update a property in a node, if the property does 1842 * not exist, add it. 1843 * 1844 * Note that we don't actually remove it, since we have given out 1845 * who-knows-how-many pointers to the data using get-property. 1846 * Instead we just move the property to the "dead properties" list, 1847 * and add the new property to the property list 1848 */ 1849 int of_update_property(struct device_node *np, struct property *newprop) 1850 { 1851 struct property *oldprop; 1852 unsigned long flags; 1853 int rc; 1854 1855 if (!newprop->name) 1856 return -EINVAL; 1857 1858 mutex_lock(&of_mutex); 1859 1860 raw_spin_lock_irqsave(&devtree_lock, flags); 1861 rc = __of_update_property(np, newprop, &oldprop); 1862 raw_spin_unlock_irqrestore(&devtree_lock, flags); 1863 1864 if (!rc) 1865 __of_update_property_sysfs(np, newprop, oldprop); 1866 1867 mutex_unlock(&of_mutex); 1868 1869 if (!rc) 1870 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop); 1871 1872 return rc; 1873 } 1874 1875 static void of_alias_add(struct alias_prop *ap, struct device_node *np, 1876 int id, const char *stem, int stem_len) 1877 { 1878 ap->np = np; 1879 ap->id = id; 1880 strncpy(ap->stem, stem, stem_len); 1881 ap->stem[stem_len] = 0; 1882 list_add_tail(&ap->link, &aliases_lookup); 1883 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n", 1884 ap->alias, ap->stem, ap->id, np); 1885 } 1886 1887 /** 1888 * of_alias_scan - Scan all properties of the 'aliases' node 1889 * 1890 * The function scans all the properties of the 'aliases' node and populates 1891 * the global lookup table with the properties. It returns the 1892 * number of alias properties found, or an error code in case of failure. 1893 * 1894 * @dt_alloc: An allocator that provides a virtual address to memory 1895 * for storing the resulting tree 1896 */ 1897 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) 1898 { 1899 struct property *pp; 1900 1901 of_aliases = of_find_node_by_path("/aliases"); 1902 of_chosen = of_find_node_by_path("/chosen"); 1903 if (of_chosen == NULL) 1904 of_chosen = of_find_node_by_path("/chosen@0"); 1905 1906 if (of_chosen) { 1907 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */ 1908 const char *name = NULL; 1909 1910 if (of_property_read_string(of_chosen, "stdout-path", &name)) 1911 of_property_read_string(of_chosen, "linux,stdout-path", 1912 &name); 1913 if (IS_ENABLED(CONFIG_PPC) && !name) 1914 of_property_read_string(of_aliases, "stdout", &name); 1915 if (name) 1916 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options); 1917 } 1918 1919 if (!of_aliases) 1920 return; 1921 1922 for_each_property_of_node(of_aliases, pp) { 1923 const char *start = pp->name; 1924 const char *end = start + strlen(start); 1925 struct device_node *np; 1926 struct alias_prop *ap; 1927 int id, len; 1928 1929 /* Skip those we do not want to proceed */ 1930 if (!strcmp(pp->name, "name") || 1931 !strcmp(pp->name, "phandle") || 1932 !strcmp(pp->name, "linux,phandle")) 1933 continue; 1934 1935 np = of_find_node_by_path(pp->value); 1936 if (!np) 1937 continue; 1938 1939 /* walk the alias backwards to extract the id and work out 1940 * the 'stem' string */ 1941 while (isdigit(*(end-1)) && end > start) 1942 end--; 1943 len = end - start; 1944 1945 if (kstrtoint(end, 10, &id) < 0) 1946 continue; 1947 1948 /* Allocate an alias_prop with enough space for the stem */ 1949 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap)); 1950 if (!ap) 1951 continue; 1952 memset(ap, 0, sizeof(*ap) + len + 1); 1953 ap->alias = start; 1954 of_alias_add(ap, np, id, start, len); 1955 } 1956 } 1957 1958 /** 1959 * of_alias_get_id - Get alias id for the given device_node 1960 * @np: Pointer to the given device_node 1961 * @stem: Alias stem of the given device_node 1962 * 1963 * The function travels the lookup table to get the alias id for the given 1964 * device_node and alias stem. It returns the alias id if found. 1965 */ 1966 int of_alias_get_id(struct device_node *np, const char *stem) 1967 { 1968 struct alias_prop *app; 1969 int id = -ENODEV; 1970 1971 mutex_lock(&of_mutex); 1972 list_for_each_entry(app, &aliases_lookup, link) { 1973 if (strcmp(app->stem, stem) != 0) 1974 continue; 1975 1976 if (np == app->np) { 1977 id = app->id; 1978 break; 1979 } 1980 } 1981 mutex_unlock(&of_mutex); 1982 1983 return id; 1984 } 1985 EXPORT_SYMBOL_GPL(of_alias_get_id); 1986 1987 /** 1988 * of_alias_get_alias_list - Get alias list for the given device driver 1989 * @matches: Array of OF device match structures to search in 1990 * @stem: Alias stem of the given device_node 1991 * @bitmap: Bitmap field pointer 1992 * @nbits: Maximum number of alias IDs which can be recorded in bitmap 1993 * 1994 * The function travels the lookup table to record alias ids for the given 1995 * device match structures and alias stem. 1996 * 1997 * Return: 0 or -ENOSYS when !CONFIG_OF or 1998 * -EOVERFLOW if alias ID is greater then allocated nbits 1999 */ 2000 int of_alias_get_alias_list(const struct of_device_id *matches, 2001 const char *stem, unsigned long *bitmap, 2002 unsigned int nbits) 2003 { 2004 struct alias_prop *app; 2005 int ret = 0; 2006 2007 /* Zero bitmap field to make sure that all the time it is clean */ 2008 bitmap_zero(bitmap, nbits); 2009 2010 mutex_lock(&of_mutex); 2011 pr_debug("%s: Looking for stem: %s\n", __func__, stem); 2012 list_for_each_entry(app, &aliases_lookup, link) { 2013 pr_debug("%s: stem: %s, id: %d\n", 2014 __func__, app->stem, app->id); 2015 2016 if (strcmp(app->stem, stem) != 0) { 2017 pr_debug("%s: stem comparison didn't pass %s\n", 2018 __func__, app->stem); 2019 continue; 2020 } 2021 2022 if (of_match_node(matches, app->np)) { 2023 pr_debug("%s: Allocated ID %d\n", __func__, app->id); 2024 2025 if (app->id >= nbits) { 2026 pr_warn("%s: ID %d >= than bitmap field %d\n", 2027 __func__, app->id, nbits); 2028 ret = -EOVERFLOW; 2029 } else { 2030 set_bit(app->id, bitmap); 2031 } 2032 } 2033 } 2034 mutex_unlock(&of_mutex); 2035 2036 return ret; 2037 } 2038 EXPORT_SYMBOL_GPL(of_alias_get_alias_list); 2039 2040 /** 2041 * of_alias_get_highest_id - Get highest alias id for the given stem 2042 * @stem: Alias stem to be examined 2043 * 2044 * The function travels the lookup table to get the highest alias id for the 2045 * given alias stem. It returns the alias id if found. 2046 */ 2047 int of_alias_get_highest_id(const char *stem) 2048 { 2049 struct alias_prop *app; 2050 int id = -ENODEV; 2051 2052 mutex_lock(&of_mutex); 2053 list_for_each_entry(app, &aliases_lookup, link) { 2054 if (strcmp(app->stem, stem) != 0) 2055 continue; 2056 2057 if (app->id > id) 2058 id = app->id; 2059 } 2060 mutex_unlock(&of_mutex); 2061 2062 return id; 2063 } 2064 EXPORT_SYMBOL_GPL(of_alias_get_highest_id); 2065 2066 /** 2067 * of_console_check() - Test and setup console for DT setup 2068 * @dn - Pointer to device node 2069 * @name - Name to use for preferred console without index. ex. "ttyS" 2070 * @index - Index to use for preferred console. 2071 * 2072 * Check if the given device node matches the stdout-path property in the 2073 * /chosen node. If it does then register it as the preferred console and return 2074 * TRUE. Otherwise return FALSE. 2075 */ 2076 bool of_console_check(struct device_node *dn, char *name, int index) 2077 { 2078 if (!dn || dn != of_stdout || console_set_on_cmdline) 2079 return false; 2080 2081 /* 2082 * XXX: cast `options' to char pointer to suppress complication 2083 * warnings: printk, UART and console drivers expect char pointer. 2084 */ 2085 return !add_preferred_console(name, index, (char *)of_stdout_options); 2086 } 2087 EXPORT_SYMBOL_GPL(of_console_check); 2088 2089 /** 2090 * of_find_next_cache_node - Find a node's subsidiary cache 2091 * @np: node of type "cpu" or "cache" 2092 * 2093 * Returns a node pointer with refcount incremented, use 2094 * of_node_put() on it when done. Caller should hold a reference 2095 * to np. 2096 */ 2097 struct device_node *of_find_next_cache_node(const struct device_node *np) 2098 { 2099 struct device_node *child, *cache_node; 2100 2101 cache_node = of_parse_phandle(np, "l2-cache", 0); 2102 if (!cache_node) 2103 cache_node = of_parse_phandle(np, "next-level-cache", 0); 2104 2105 if (cache_node) 2106 return cache_node; 2107 2108 /* OF on pmac has nodes instead of properties named "l2-cache" 2109 * beneath CPU nodes. 2110 */ 2111 if (IS_ENABLED(CONFIG_PPC_PMAC) && !strcmp(np->type, "cpu")) 2112 for_each_child_of_node(np, child) 2113 if (!strcmp(child->type, "cache")) 2114 return child; 2115 2116 return NULL; 2117 } 2118 2119 /** 2120 * of_find_last_cache_level - Find the level at which the last cache is 2121 * present for the given logical cpu 2122 * 2123 * @cpu: cpu number(logical index) for which the last cache level is needed 2124 * 2125 * Returns the the level at which the last cache is present. It is exactly 2126 * same as the total number of cache levels for the given logical cpu. 2127 */ 2128 int of_find_last_cache_level(unsigned int cpu) 2129 { 2130 u32 cache_level = 0; 2131 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu); 2132 2133 while (np) { 2134 prev = np; 2135 of_node_put(np); 2136 np = of_find_next_cache_node(np); 2137 } 2138 2139 of_property_read_u32(prev, "cache-level", &cache_level); 2140 2141 return cache_level; 2142 } 2143 2144 /** 2145 * of_map_rid - Translate a requester ID through a downstream mapping. 2146 * @np: root complex device node. 2147 * @rid: device requester ID to map. 2148 * @map_name: property name of the map to use. 2149 * @map_mask_name: optional property name of the mask to use. 2150 * @target: optional pointer to a target device node. 2151 * @id_out: optional pointer to receive the translated ID. 2152 * 2153 * Given a device requester ID, look up the appropriate implementation-defined 2154 * platform ID and/or the target device which receives transactions on that 2155 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or 2156 * @id_out may be NULL if only the other is required. If @target points to 2157 * a non-NULL device node pointer, only entries targeting that node will be 2158 * matched; if it points to a NULL value, it will receive the device node of 2159 * the first matching target phandle, with a reference held. 2160 * 2161 * Return: 0 on success or a standard error code on failure. 2162 */ 2163 int of_map_rid(struct device_node *np, u32 rid, 2164 const char *map_name, const char *map_mask_name, 2165 struct device_node **target, u32 *id_out) 2166 { 2167 u32 map_mask, masked_rid; 2168 int map_len; 2169 const __be32 *map = NULL; 2170 2171 if (!np || !map_name || (!target && !id_out)) 2172 return -EINVAL; 2173 2174 map = of_get_property(np, map_name, &map_len); 2175 if (!map) { 2176 if (target) 2177 return -ENODEV; 2178 /* Otherwise, no map implies no translation */ 2179 *id_out = rid; 2180 return 0; 2181 } 2182 2183 if (!map_len || map_len % (4 * sizeof(*map))) { 2184 pr_err("%pOF: Error: Bad %s length: %d\n", np, 2185 map_name, map_len); 2186 return -EINVAL; 2187 } 2188 2189 /* The default is to select all bits. */ 2190 map_mask = 0xffffffff; 2191 2192 /* 2193 * Can be overridden by "{iommu,msi}-map-mask" property. 2194 * If of_property_read_u32() fails, the default is used. 2195 */ 2196 if (map_mask_name) 2197 of_property_read_u32(np, map_mask_name, &map_mask); 2198 2199 masked_rid = map_mask & rid; 2200 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { 2201 struct device_node *phandle_node; 2202 u32 rid_base = be32_to_cpup(map + 0); 2203 u32 phandle = be32_to_cpup(map + 1); 2204 u32 out_base = be32_to_cpup(map + 2); 2205 u32 rid_len = be32_to_cpup(map + 3); 2206 2207 if (rid_base & ~map_mask) { 2208 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", 2209 np, map_name, map_name, 2210 map_mask, rid_base); 2211 return -EFAULT; 2212 } 2213 2214 if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) 2215 continue; 2216 2217 phandle_node = of_find_node_by_phandle(phandle); 2218 if (!phandle_node) 2219 return -ENODEV; 2220 2221 if (target) { 2222 if (*target) 2223 of_node_put(phandle_node); 2224 else 2225 *target = phandle_node; 2226 2227 if (*target != phandle_node) 2228 continue; 2229 } 2230 2231 if (id_out) 2232 *id_out = masked_rid - rid_base + out_base; 2233 2234 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", 2235 np, map_name, map_mask, rid_base, out_base, 2236 rid_len, rid, masked_rid - rid_base + out_base); 2237 return 0; 2238 } 2239 2240 pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n", 2241 np, map_name, rid, target && *target ? *target : NULL); 2242 return -EFAULT; 2243 } 2244 EXPORT_SYMBOL_GPL(of_map_rid); 2245