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