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