1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Software nodes for the firmware node framework. 4 * 5 * Copyright (C) 2018, Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 */ 8 9 #include <linux/container_of.h> 10 #include <linux/device.h> 11 #include <linux/err.h> 12 #include <linux/export.h> 13 #include <linux/idr.h> 14 #include <linux/init.h> 15 #include <linux/kobject.h> 16 #include <linux/kstrtox.h> 17 #include <linux/list.h> 18 #include <linux/property.h> 19 #include <linux/slab.h> 20 #include <linux/spinlock.h> 21 #include <linux/string.h> 22 #include <linux/sysfs.h> 23 #include <linux/types.h> 24 25 #include "base.h" 26 27 struct swnode { 28 struct kobject kobj; 29 struct fwnode_handle fwnode; 30 const struct software_node *node; 31 int id; 32 /* Device the managed node's lifetime is tied to. */ 33 struct device *owner; 34 35 /* hierarchy */ 36 struct ida child_ids; 37 struct list_head entry; 38 struct list_head children; 39 struct swnode *parent; 40 41 unsigned int allocated:1; 42 }; 43 44 static DEFINE_IDA(swnode_root_ids); 45 static struct kset *swnode_kset; 46 47 #define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj) 48 49 static const struct fwnode_operations software_node_ops; 50 51 bool is_software_node(const struct fwnode_handle *fwnode) 52 { 53 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops; 54 } 55 EXPORT_SYMBOL_GPL(is_software_node); 56 57 #define to_swnode(__fwnode) \ 58 ({ \ 59 typeof(__fwnode) __to_swnode_fwnode = __fwnode; \ 60 \ 61 is_software_node(__to_swnode_fwnode) ? \ 62 container_of(__to_swnode_fwnode, \ 63 struct swnode, fwnode) : NULL; \ 64 }) 65 66 static inline struct swnode *dev_to_swnode(struct device *dev) 67 { 68 struct fwnode_handle *fwnode = dev_fwnode(dev); 69 70 if (!fwnode) 71 return NULL; 72 73 if (!is_software_node(fwnode)) 74 fwnode = fwnode->secondary; 75 76 return to_swnode(fwnode); 77 } 78 79 static struct swnode * 80 software_node_to_swnode(const struct software_node *node) 81 { 82 struct swnode *swnode = NULL; 83 struct kobject *k; 84 85 if (!node) 86 return NULL; 87 88 spin_lock(&swnode_kset->list_lock); 89 90 list_for_each_entry(k, &swnode_kset->list, entry) { 91 swnode = kobj_to_swnode(k); 92 if (swnode->node == node) 93 break; 94 swnode = NULL; 95 } 96 97 spin_unlock(&swnode_kset->list_lock); 98 99 return swnode; 100 } 101 102 const struct software_node *to_software_node(const struct fwnode_handle *fwnode) 103 { 104 const struct swnode *swnode = to_swnode(fwnode); 105 106 return swnode ? swnode->node : NULL; 107 } 108 EXPORT_SYMBOL_GPL(to_software_node); 109 110 struct fwnode_handle *software_node_fwnode(const struct software_node *node) 111 { 112 struct swnode *swnode = software_node_to_swnode(node); 113 114 return swnode ? &swnode->fwnode : NULL; 115 } 116 EXPORT_SYMBOL_GPL(software_node_fwnode); 117 118 /* -------------------------------------------------------------------------- */ 119 /* property_entry processing */ 120 121 static const struct property_entry * 122 property_entry_get(const struct property_entry *prop, const char *name) 123 { 124 if (!prop) 125 return NULL; 126 127 for (; prop->name; prop++) 128 if (!strcmp(name, prop->name)) 129 return prop; 130 131 return NULL; 132 } 133 134 static const void *property_get_pointer(const struct property_entry *prop) 135 { 136 if (!prop->length) 137 return NULL; 138 139 return prop->is_inline ? &prop->value : prop->pointer; 140 } 141 142 static const void *property_entry_find(const struct property_entry *props, 143 const char *propname, size_t length) 144 { 145 const struct property_entry *prop; 146 const void *pointer; 147 148 prop = property_entry_get(props, propname); 149 if (!prop) 150 return ERR_PTR(-EINVAL); 151 pointer = property_get_pointer(prop); 152 if (!pointer) 153 return ERR_PTR(-ENODATA); 154 if (length > prop->length) 155 return ERR_PTR(-EOVERFLOW); 156 return pointer; 157 } 158 159 static int 160 property_entry_count_elems_of_size(const struct property_entry *props, 161 const char *propname, size_t length) 162 { 163 const struct property_entry *prop; 164 165 prop = property_entry_get(props, propname); 166 if (!prop) 167 return -EINVAL; 168 169 return prop->length / length; 170 } 171 172 static int property_entry_read_int_array(const struct property_entry *props, 173 const char *name, 174 unsigned int elem_size, void *val, 175 size_t nval) 176 { 177 const void *pointer; 178 size_t length; 179 180 if (!val) 181 return property_entry_count_elems_of_size(props, name, 182 elem_size); 183 184 if (!is_power_of_2(elem_size) || elem_size > sizeof(u64)) 185 return -ENXIO; 186 187 length = nval * elem_size; 188 189 pointer = property_entry_find(props, name, length); 190 if (IS_ERR(pointer)) 191 return PTR_ERR(pointer); 192 193 memcpy(val, pointer, length); 194 return 0; 195 } 196 197 static int property_entry_read_string_array(const struct property_entry *props, 198 const char *propname, 199 const char **strings, size_t nval) 200 { 201 const void *pointer; 202 size_t length; 203 int array_len; 204 205 /* Find out the array length. */ 206 array_len = property_entry_count_elems_of_size(props, propname, 207 sizeof(const char *)); 208 if (array_len < 0) 209 return array_len; 210 211 /* Return how many there are if strings is NULL. */ 212 if (!strings) 213 return array_len; 214 215 array_len = min_t(size_t, nval, array_len); 216 length = array_len * sizeof(*strings); 217 218 pointer = property_entry_find(props, propname, length); 219 if (IS_ERR(pointer)) 220 return PTR_ERR(pointer); 221 222 memcpy(strings, pointer, length); 223 224 return array_len; 225 } 226 227 static void property_entry_free_data(const struct property_entry *p) 228 { 229 const char * const *src_str; 230 size_t i, nval; 231 232 if (p->type == DEV_PROP_STRING) { 233 src_str = property_get_pointer(p); 234 nval = p->length / sizeof(*src_str); 235 for (i = 0; i < nval; i++) 236 kfree(src_str[i]); 237 } 238 239 if (!p->is_inline) 240 kfree(p->pointer); 241 242 kfree(p->name); 243 } 244 245 static bool property_copy_string_array(const char **dst_ptr, 246 const char * const *src_ptr, 247 size_t nval) 248 { 249 int i; 250 251 for (i = 0; i < nval; i++) { 252 dst_ptr[i] = kstrdup(src_ptr[i], GFP_KERNEL); 253 if (!dst_ptr[i] && src_ptr[i]) { 254 while (--i >= 0) 255 kfree(dst_ptr[i]); 256 return false; 257 } 258 } 259 260 return true; 261 } 262 263 static int property_entry_copy_data(struct property_entry *dst, 264 const struct property_entry *src) 265 { 266 const void *pointer = property_get_pointer(src); 267 void *dst_ptr; 268 size_t nval; 269 270 /* 271 * Properties with no data should not be marked as stored 272 * out of line. 273 */ 274 if (!src->is_inline && !src->length) 275 return -ENODATA; 276 277 /* 278 * Reference properties are never stored inline as 279 * they are too big. 280 */ 281 if (src->type == DEV_PROP_REF && src->is_inline) 282 return -EINVAL; 283 284 if (src->length <= sizeof(dst->value)) { 285 dst_ptr = &dst->value; 286 dst->is_inline = true; 287 } else { 288 dst_ptr = kmalloc(src->length, GFP_KERNEL); 289 if (!dst_ptr) 290 return -ENOMEM; 291 dst->pointer = dst_ptr; 292 } 293 294 if (src->type == DEV_PROP_STRING) { 295 nval = src->length / sizeof(const char *); 296 if (!property_copy_string_array(dst_ptr, pointer, nval)) { 297 if (!dst->is_inline) 298 kfree(dst->pointer); 299 return -ENOMEM; 300 } 301 } else { 302 memcpy(dst_ptr, pointer, src->length); 303 } 304 305 dst->length = src->length; 306 dst->type = src->type; 307 dst->name = kstrdup(src->name, GFP_KERNEL); 308 if (!dst->name) { 309 property_entry_free_data(dst); 310 return -ENOMEM; 311 } 312 313 return 0; 314 } 315 316 /** 317 * property_entries_dup - duplicate array of properties 318 * @properties: array of properties to copy 319 * 320 * This function creates a deep copy of the given NULL-terminated array 321 * of property entries. 322 */ 323 struct property_entry * 324 property_entries_dup(const struct property_entry *properties) 325 { 326 struct property_entry *p; 327 int i, n = 0; 328 int ret; 329 330 if (!properties) 331 return NULL; 332 333 while (properties[n].name) 334 n++; 335 336 p = kzalloc_objs(*p, n + 1); 337 if (!p) 338 return ERR_PTR(-ENOMEM); 339 340 for (i = 0; i < n; i++) { 341 ret = property_entry_copy_data(&p[i], &properties[i]); 342 if (ret) { 343 while (--i >= 0) 344 property_entry_free_data(&p[i]); 345 kfree(p); 346 return ERR_PTR(ret); 347 } 348 } 349 350 return p; 351 } 352 EXPORT_SYMBOL_GPL(property_entries_dup); 353 354 /** 355 * property_entries_free - free previously allocated array of properties 356 * @properties: array of properties to destroy 357 * 358 * This function frees given NULL-terminated array of property entries, 359 * along with their data. 360 */ 361 void property_entries_free(const struct property_entry *properties) 362 { 363 const struct property_entry *p; 364 365 if (!properties) 366 return; 367 368 for (p = properties; p->name; p++) 369 property_entry_free_data(p); 370 371 kfree(properties); 372 } 373 EXPORT_SYMBOL_GPL(property_entries_free); 374 375 /* -------------------------------------------------------------------------- */ 376 /* fwnode operations */ 377 378 static struct swnode *swnode_get(struct swnode *swnode) 379 { 380 kobject_get(&swnode->kobj); 381 382 return swnode; 383 } 384 385 static void swnode_put(struct swnode *swnode) 386 { 387 kobject_put(&swnode->kobj); 388 } 389 390 static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode) 391 { 392 struct swnode *swnode = swnode_get(to_swnode(fwnode)); 393 394 return &swnode->fwnode; 395 } 396 397 static void software_node_put(struct fwnode_handle *fwnode) 398 { 399 swnode_put(to_swnode(fwnode)); 400 } 401 402 static bool software_node_property_present(const struct fwnode_handle *fwnode, 403 const char *propname) 404 { 405 struct swnode *swnode = to_swnode(fwnode); 406 407 return !!property_entry_get(swnode->node->properties, propname); 408 } 409 410 static int software_node_read_int_array(const struct fwnode_handle *fwnode, 411 const char *propname, 412 unsigned int elem_size, void *val, 413 size_t nval) 414 { 415 struct swnode *swnode = to_swnode(fwnode); 416 417 return property_entry_read_int_array(swnode->node->properties, propname, 418 elem_size, val, nval); 419 } 420 421 static int software_node_read_string_array(const struct fwnode_handle *fwnode, 422 const char *propname, 423 const char **val, size_t nval) 424 { 425 struct swnode *swnode = to_swnode(fwnode); 426 427 return property_entry_read_string_array(swnode->node->properties, 428 propname, val, nval); 429 } 430 431 static const char * 432 software_node_get_name(const struct fwnode_handle *fwnode) 433 { 434 const struct swnode *swnode = to_swnode(fwnode); 435 436 return kobject_name(&swnode->kobj); 437 } 438 439 static const char * 440 software_node_get_name_prefix(const struct fwnode_handle *fwnode) 441 { 442 struct fwnode_handle *parent; 443 const char *prefix; 444 445 parent = fwnode_get_parent(fwnode); 446 if (!parent) 447 return ""; 448 449 /* Figure out the prefix from the parents. */ 450 while (is_software_node(parent)) 451 parent = fwnode_get_next_parent(parent); 452 453 prefix = fwnode_get_name_prefix(parent); 454 fwnode_handle_put(parent); 455 456 /* Guess something if prefix was NULL. */ 457 return prefix ?: "/"; 458 } 459 460 static struct fwnode_handle * 461 software_node_get_parent(const struct fwnode_handle *fwnode) 462 { 463 struct swnode *swnode = to_swnode(fwnode); 464 465 if (!swnode || !swnode->parent) 466 return NULL; 467 468 return fwnode_handle_get(&swnode->parent->fwnode); 469 } 470 471 static struct fwnode_handle * 472 software_node_get_next_child(const struct fwnode_handle *fwnode, 473 struct fwnode_handle *child) 474 { 475 struct swnode *p = to_swnode(fwnode); 476 struct swnode *c = to_swnode(child); 477 478 if (!p || list_empty(&p->children) || 479 (c && list_is_last(&c->entry, &p->children))) { 480 fwnode_handle_put(child); 481 return NULL; 482 } 483 484 if (c) 485 c = list_next_entry(c, entry); 486 else 487 c = list_first_entry(&p->children, struct swnode, entry); 488 489 fwnode_handle_put(child); 490 return fwnode_handle_get(&c->fwnode); 491 } 492 493 static struct fwnode_handle * 494 software_node_get_named_child_node(const struct fwnode_handle *fwnode, 495 const char *childname) 496 { 497 struct swnode *swnode = to_swnode(fwnode); 498 struct swnode *child; 499 500 if (!swnode || list_empty(&swnode->children)) 501 return NULL; 502 503 list_for_each_entry(child, &swnode->children, entry) { 504 if (!strcmp(childname, kobject_name(&child->kobj))) { 505 swnode_get(child); 506 return &child->fwnode; 507 } 508 } 509 return NULL; 510 } 511 512 static int 513 software_node_get_reference_args(const struct fwnode_handle *fwnode, 514 const char *propname, const char *nargs_prop, 515 unsigned int nargs, unsigned int index, 516 struct fwnode_reference_args *args) 517 { 518 struct swnode *swnode = to_swnode(fwnode); 519 const struct software_node_ref_args *ref_array; 520 const struct software_node_ref_args *ref; 521 const struct property_entry *prop; 522 struct fwnode_handle *refnode; 523 u32 nargs_prop_val; 524 int error; 525 int i; 526 527 prop = property_entry_get(swnode->node->properties, propname); 528 if (!prop) 529 return -ENOENT; 530 531 if (prop->type != DEV_PROP_REF) 532 return -EINVAL; 533 534 /* 535 * We expect that references are never stored inline, even 536 * single ones, as they are too big. 537 */ 538 if (prop->is_inline) 539 return -EINVAL; 540 541 if (index >= prop->length / sizeof(*ref)) 542 return -ENOENT; 543 544 ref_array = prop->pointer; 545 ref = &ref_array[index]; 546 547 /* 548 * A software node can reference other software nodes or firmware 549 * nodes (which are the abstraction layer sitting on top of them). 550 * This is done to ensure we can create references to static software 551 * nodes before they're registered with the firmware node framework. 552 * At the time the reference is being resolved, we expect the swnodes 553 * in question to already have been registered and to be backed by 554 * a firmware node. This is why we use the fwnode API below to read the 555 * relevant properties and bump the reference count. 556 */ 557 558 if (ref->swnode) 559 refnode = software_node_fwnode(ref->swnode); 560 else if (ref->fwnode) 561 refnode = ref->fwnode; 562 else 563 return -EINVAL; 564 565 if (!refnode) 566 return -ENOTCONN; 567 568 if (nargs_prop) { 569 error = fwnode_property_read_u32(refnode, nargs_prop, &nargs_prop_val); 570 if (error) 571 return error; 572 573 nargs = nargs_prop_val; 574 } 575 576 if (nargs > NR_FWNODE_REFERENCE_ARGS) 577 return -EINVAL; 578 579 if (!args) 580 return 0; 581 582 args->fwnode = fwnode_handle_get(refnode); 583 args->nargs = nargs; 584 585 for (i = 0; i < nargs; i++) 586 args->args[i] = ref->args[i]; 587 588 return 0; 589 } 590 591 static struct fwnode_handle * 592 swnode_graph_find_next_port(const struct fwnode_handle *parent, 593 struct fwnode_handle *port) 594 { 595 struct fwnode_handle *old = port; 596 597 while ((port = software_node_get_next_child(parent, old))) { 598 /* 599 * fwnode ports have naming style "port@", so we search for any 600 * children that follow that convention. 601 */ 602 if (!strncmp(to_swnode(port)->node->name, "port@", 603 strlen("port@"))) 604 return port; 605 old = port; 606 } 607 608 return NULL; 609 } 610 611 static struct fwnode_handle * 612 software_node_graph_get_next_endpoint(const struct fwnode_handle *fwnode, 613 struct fwnode_handle *endpoint) 614 { 615 struct swnode *swnode = to_swnode(fwnode); 616 struct fwnode_handle *parent; 617 struct fwnode_handle *port; 618 619 if (!swnode) 620 return NULL; 621 622 if (endpoint) { 623 port = software_node_get_parent(endpoint); 624 parent = software_node_get_parent(port); 625 } else { 626 parent = software_node_get_named_child_node(fwnode, "ports"); 627 if (!parent) 628 parent = software_node_get(&swnode->fwnode); 629 630 port = swnode_graph_find_next_port(parent, NULL); 631 } 632 633 for (; port; port = swnode_graph_find_next_port(parent, port)) { 634 endpoint = software_node_get_next_child(port, endpoint); 635 if (endpoint) { 636 fwnode_handle_put(port); 637 break; 638 } 639 } 640 641 fwnode_handle_put(parent); 642 643 return endpoint; 644 } 645 646 static struct fwnode_handle * 647 software_node_graph_get_remote_endpoint(const struct fwnode_handle *fwnode) 648 { 649 struct swnode *swnode = to_swnode(fwnode); 650 const struct software_node_ref_args *ref; 651 const struct property_entry *prop; 652 653 if (!swnode) 654 return NULL; 655 656 prop = property_entry_get(swnode->node->properties, "remote-endpoint"); 657 if (!prop || prop->type != DEV_PROP_REF || prop->is_inline) 658 return NULL; 659 660 ref = prop->pointer; 661 662 if (!ref->swnode) 663 return NULL; 664 665 return software_node_get(software_node_fwnode(ref->swnode)); 666 } 667 668 static struct fwnode_handle * 669 software_node_graph_get_port_parent(struct fwnode_handle *fwnode) 670 { 671 struct swnode *swnode = to_swnode(fwnode); 672 673 swnode = swnode->parent; 674 if (swnode && !strcmp(swnode->node->name, "ports")) 675 swnode = swnode->parent; 676 677 return swnode ? software_node_get(&swnode->fwnode) : NULL; 678 } 679 680 static int 681 software_node_graph_parse_endpoint(const struct fwnode_handle *fwnode, 682 struct fwnode_endpoint *endpoint) 683 { 684 struct swnode *swnode = to_swnode(fwnode); 685 const char *parent_name = swnode->parent->node->name; 686 int ret; 687 688 if (strlen("port@") >= strlen(parent_name) || 689 strncmp(parent_name, "port@", strlen("port@"))) 690 return -EINVAL; 691 692 /* Ports have naming style "port@n", we need to select the n */ 693 ret = kstrtou32(parent_name + strlen("port@"), 10, &endpoint->port); 694 if (ret) 695 return ret; 696 697 endpoint->id = swnode->id; 698 endpoint->local_fwnode = fwnode; 699 700 return 0; 701 } 702 703 static int software_node_add_links(struct fwnode_handle *fwnode) 704 { 705 const struct software_node_ref_args *ref, *ref_array; 706 struct swnode *swnode = to_swnode(fwnode); 707 const struct property_entry *prop; 708 struct fwnode_handle *refnode; 709 unsigned int count; 710 711 if (!swnode || !swnode->node->properties) 712 return 0; 713 714 /* 715 * Unlike Device Tree, where phandles appear in many non-supplier 716 * contexts and a curated allowlist is required, a software node only 717 * carries a DEV_PROP_REF property when the author explicitly describes 718 * a reference to another node. Every such reference is therefore an 719 * intentional supplier dependency, so we create fwnode links for all 720 * of them. 721 */ 722 for (prop = swnode->node->properties; prop->name; prop++) { 723 if (prop->type != DEV_PROP_REF || prop->is_inline) 724 continue; 725 726 /* 727 * TODO: Graph "remote-endpoint" references go both ways 728 * between endpoint child nodes and would create endpoint 729 * cycles. Let's leave it out for now until we have potential 730 * users. 731 */ 732 if (!strcmp(prop->name, "remote-endpoint")) 733 continue; 734 735 ref_array = prop->pointer; 736 count = prop->length / sizeof(*ref_array); 737 738 for (unsigned int i = 0; i < count; i++) { 739 ref = &ref_array[i]; 740 741 if (ref->swnode) 742 refnode = software_node_fwnode(ref->swnode); 743 else if (ref->fwnode) 744 refnode = ref->fwnode; 745 else 746 continue; 747 748 /* Supplier not registered yet, or self-reference. */ 749 if (!refnode || refnode == &swnode->fwnode) 750 continue; 751 752 fwnode_link_add(&swnode->fwnode, refnode, 0); 753 } 754 } 755 756 return 0; 757 } 758 759 static const struct fwnode_operations software_node_ops = { 760 .get = software_node_get, 761 .put = software_node_put, 762 .property_present = software_node_property_present, 763 .property_read_bool = software_node_property_present, 764 .property_read_int_array = software_node_read_int_array, 765 .property_read_string_array = software_node_read_string_array, 766 .get_name = software_node_get_name, 767 .get_name_prefix = software_node_get_name_prefix, 768 .get_parent = software_node_get_parent, 769 .get_next_child_node = software_node_get_next_child, 770 .get_named_child_node = software_node_get_named_child_node, 771 .get_reference_args = software_node_get_reference_args, 772 .graph_get_next_endpoint = software_node_graph_get_next_endpoint, 773 .graph_get_remote_endpoint = software_node_graph_get_remote_endpoint, 774 .graph_get_port_parent = software_node_graph_get_port_parent, 775 .graph_parse_endpoint = software_node_graph_parse_endpoint, 776 .add_links = software_node_add_links, 777 }; 778 779 /* -------------------------------------------------------------------------- */ 780 781 /** 782 * software_node_find_by_name - Find software node by name 783 * @parent: Parent of the software node 784 * @name: Name of the software node 785 * 786 * The function will find a node that is child of @parent and that is named 787 * @name. If no node is found, the function returns NULL. 788 * 789 * NOTE: you will need to drop the reference with fwnode_handle_put() after use. 790 */ 791 const struct software_node * 792 software_node_find_by_name(const struct software_node *parent, const char *name) 793 { 794 struct swnode *swnode = NULL; 795 struct kobject *k; 796 797 if (!name) 798 return NULL; 799 800 spin_lock(&swnode_kset->list_lock); 801 802 list_for_each_entry(k, &swnode_kset->list, entry) { 803 swnode = kobj_to_swnode(k); 804 if (parent == swnode->node->parent && swnode->node->name && 805 !strcmp(name, swnode->node->name)) { 806 swnode_get(swnode); 807 break; 808 } 809 swnode = NULL; 810 } 811 812 spin_unlock(&swnode_kset->list_lock); 813 814 return swnode ? swnode->node : NULL; 815 } 816 EXPORT_SYMBOL_GPL(software_node_find_by_name); 817 818 static struct software_node *software_node_alloc(const struct property_entry *properties) 819 { 820 struct property_entry *props; 821 struct software_node *node; 822 823 props = property_entries_dup(properties); 824 if (IS_ERR(props)) 825 return ERR_CAST(props); 826 827 node = kzalloc_obj(*node); 828 if (!node) { 829 property_entries_free(props); 830 return ERR_PTR(-ENOMEM); 831 } 832 833 node->properties = props; 834 835 return node; 836 } 837 838 static void software_node_free(const struct software_node *node) 839 { 840 property_entries_free(node->properties); 841 kfree(node); 842 } 843 844 static void software_node_release(struct kobject *kobj) 845 { 846 struct swnode *swnode = kobj_to_swnode(kobj); 847 848 fwnode_links_purge(&swnode->fwnode); 849 850 if (swnode->parent) { 851 ida_free(&swnode->parent->child_ids, swnode->id); 852 list_del(&swnode->entry); 853 } else { 854 ida_free(&swnode_root_ids, swnode->id); 855 } 856 857 if (swnode->allocated) 858 software_node_free(swnode->node); 859 860 ida_destroy(&swnode->child_ids); 861 kfree(swnode); 862 } 863 864 static const struct kobj_type software_node_type = { 865 .release = software_node_release, 866 .sysfs_ops = &kobj_sysfs_ops, 867 }; 868 869 static struct fwnode_handle * 870 swnode_register(const struct software_node *node, struct swnode *parent, 871 unsigned int allocated) 872 { 873 struct swnode *swnode; 874 int ret; 875 876 swnode = kzalloc_obj(*swnode); 877 if (!swnode) 878 return ERR_PTR(-ENOMEM); 879 880 ret = ida_alloc(parent ? &parent->child_ids : &swnode_root_ids, 881 GFP_KERNEL); 882 if (ret < 0) { 883 kfree(swnode); 884 return ERR_PTR(ret); 885 } 886 887 swnode->id = ret; 888 swnode->node = node; 889 swnode->parent = parent; 890 swnode->kobj.kset = swnode_kset; 891 fwnode_init(&swnode->fwnode, &software_node_ops); 892 893 ida_init(&swnode->child_ids); 894 INIT_LIST_HEAD(&swnode->entry); 895 INIT_LIST_HEAD(&swnode->children); 896 897 if (node->name) 898 ret = kobject_init_and_add(&swnode->kobj, &software_node_type, 899 parent ? &parent->kobj : NULL, 900 "%s", node->name); 901 else 902 ret = kobject_init_and_add(&swnode->kobj, &software_node_type, 903 parent ? &parent->kobj : NULL, 904 "node%d", swnode->id); 905 if (ret) { 906 swnode_put(swnode); 907 return ERR_PTR(ret); 908 } 909 910 /* 911 * Assign the flag only in the successful case, so 912 * the above swnode_put() won't mess up with properties. 913 */ 914 swnode->allocated = allocated; 915 916 if (parent) 917 list_add_tail(&swnode->entry, &parent->children); 918 919 kobject_uevent(&swnode->kobj, KOBJ_ADD); 920 return &swnode->fwnode; 921 } 922 923 /** 924 * software_node_register_node_group - Register a group of software nodes 925 * @node_group: NULL terminated array of software node pointers to be registered 926 * 927 * Register multiple software nodes at once. If any node in the array 928 * has its .parent pointer set (which can only be to another software_node), 929 * then its parent **must** have been registered before it is; either outside 930 * of this function or by ordering the array such that parent comes before 931 * child. 932 */ 933 int software_node_register_node_group(const struct software_node * const *node_group) 934 { 935 unsigned int i; 936 int ret; 937 938 if (!node_group) 939 return 0; 940 941 for (i = 0; node_group[i]; i++) { 942 ret = software_node_register(node_group[i]); 943 if (ret) { 944 software_node_unregister_node_group(node_group); 945 return ret; 946 } 947 } 948 949 return 0; 950 } 951 EXPORT_SYMBOL_GPL(software_node_register_node_group); 952 953 /** 954 * software_node_unregister_node_group - Unregister a group of software nodes 955 * @node_group: NULL terminated array of software node pointers to be unregistered 956 * 957 * Unregister multiple software nodes at once. If parent pointers are set up 958 * in any of the software nodes then the array **must** be ordered such that 959 * parents come before their children. 960 * 961 * NOTE: If you are uncertain whether the array is ordered such that 962 * parents will be unregistered before their children, it is wiser to 963 * remove the nodes individually, in the correct order (child before 964 * parent). 965 */ 966 void software_node_unregister_node_group(const struct software_node * const *node_group) 967 { 968 unsigned int i = 0; 969 970 if (!node_group) 971 return; 972 973 while (node_group[i]) 974 i++; 975 976 while (i--) 977 software_node_unregister(node_group[i]); 978 } 979 EXPORT_SYMBOL_GPL(software_node_unregister_node_group); 980 981 /** 982 * software_node_register - Register static software node 983 * @node: The software node to be registered 984 */ 985 int software_node_register(const struct software_node *node) 986 { 987 struct swnode *parent = software_node_to_swnode(node->parent); 988 989 if (software_node_to_swnode(node)) 990 return -EEXIST; 991 992 if (node->parent && !parent) 993 return -EINVAL; 994 995 return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0)); 996 } 997 EXPORT_SYMBOL_GPL(software_node_register); 998 999 /** 1000 * software_node_unregister - Unregister static software node 1001 * @node: The software node to be unregistered 1002 */ 1003 void software_node_unregister(const struct software_node *node) 1004 { 1005 struct swnode *swnode; 1006 1007 swnode = software_node_to_swnode(node); 1008 if (swnode) 1009 fwnode_remove_software_node(&swnode->fwnode); 1010 } 1011 EXPORT_SYMBOL_GPL(software_node_unregister); 1012 1013 struct fwnode_handle * 1014 fwnode_create_software_node(const struct property_entry *properties, 1015 const struct fwnode_handle *parent) 1016 { 1017 struct fwnode_handle *fwnode; 1018 struct software_node *node; 1019 struct swnode *p; 1020 1021 if (IS_ERR(parent)) 1022 return ERR_CAST(parent); 1023 1024 p = to_swnode(parent); 1025 if (parent && !p) 1026 return ERR_PTR(-EINVAL); 1027 1028 node = software_node_alloc(properties); 1029 if (IS_ERR(node)) 1030 return ERR_CAST(node); 1031 1032 node->parent = p ? p->node : NULL; 1033 1034 fwnode = swnode_register(node, p, 1); 1035 if (IS_ERR(fwnode)) 1036 software_node_free(node); 1037 1038 return fwnode; 1039 } 1040 EXPORT_SYMBOL_GPL(fwnode_create_software_node); 1041 1042 void fwnode_remove_software_node(struct fwnode_handle *fwnode) 1043 { 1044 struct swnode *swnode = to_swnode(fwnode); 1045 1046 if (!swnode) 1047 return; 1048 1049 swnode_put(swnode); 1050 } 1051 EXPORT_SYMBOL_GPL(fwnode_remove_software_node); 1052 1053 /** 1054 * device_add_software_node - Assign software node to a device 1055 * @dev: The device the software node is meant for. 1056 * @node: The software node. 1057 * 1058 * This function will make @node the secondary firmware node pointer of @dev. If 1059 * @dev has no primary node, then @node will become the primary node. The 1060 * function will register @node automatically if it wasn't already registered. 1061 */ 1062 int device_add_software_node(struct device *dev, const struct software_node *node) 1063 { 1064 struct swnode *swnode; 1065 int ret; 1066 1067 /* Only one software node per device. */ 1068 if (dev_to_swnode(dev)) 1069 return -EBUSY; 1070 1071 swnode = software_node_to_swnode(node); 1072 if (swnode) { 1073 swnode_get(swnode); 1074 } else { 1075 ret = software_node_register(node); 1076 if (ret) 1077 return ret; 1078 1079 swnode = software_node_to_swnode(node); 1080 } 1081 1082 set_secondary_fwnode(dev, &swnode->fwnode); 1083 1084 /* 1085 * If the device has been fully registered by the time this function is 1086 * called, software_node_notify() must be called separately so that the 1087 * symlinks get created and the reference count of the node is kept in 1088 * balance. 1089 */ 1090 if (device_is_registered(dev)) 1091 software_node_notify(dev); 1092 1093 return 0; 1094 } 1095 EXPORT_SYMBOL_GPL(device_add_software_node); 1096 1097 /** 1098 * device_remove_software_node - Remove device's software node 1099 * @dev: The device with the software node. 1100 * 1101 * This function will unregister the software node of @dev. 1102 */ 1103 void device_remove_software_node(struct device *dev) 1104 { 1105 struct swnode *swnode; 1106 1107 swnode = dev_to_swnode(dev); 1108 if (!swnode) 1109 return; 1110 1111 if (device_is_registered(dev)) 1112 software_node_notify_remove(dev); 1113 1114 set_secondary_fwnode(dev, NULL); 1115 swnode_put(swnode); 1116 } 1117 EXPORT_SYMBOL_GPL(device_remove_software_node); 1118 1119 /** 1120 * device_create_managed_software_node - Create a software node for a device 1121 * @dev: The device the software node is assigned to. 1122 * @properties: Device properties for the software node. 1123 * @parent: Parent of the software node. 1124 * 1125 * Creates a software node as a managed resource for @dev, which means the 1126 * lifetime of the newly created software node is tied to the lifetime of @dev. 1127 * Software nodes created with this function should not be reused or shared 1128 * because of that. The function takes a deep copy of @properties for the 1129 * software node. 1130 * 1131 * Since the new software node is assigned directly to @dev, and since it should 1132 * not be shared, it is not returned to the caller. The function returns 0 on 1133 * success, and errno in case of an error. 1134 */ 1135 int device_create_managed_software_node(struct device *dev, 1136 const struct property_entry *properties, 1137 const struct software_node *parent) 1138 { 1139 struct fwnode_handle *p = software_node_fwnode(parent); 1140 struct fwnode_handle *fwnode; 1141 1142 if (parent && !p) 1143 return -EINVAL; 1144 1145 fwnode = fwnode_create_software_node(properties, p); 1146 if (IS_ERR(fwnode)) 1147 return PTR_ERR(fwnode); 1148 1149 to_swnode(fwnode)->owner = dev; 1150 set_secondary_fwnode(dev, fwnode); 1151 1152 if (device_is_registered(dev)) 1153 software_node_notify(dev); 1154 1155 return 0; 1156 } 1157 EXPORT_SYMBOL_GPL(device_create_managed_software_node); 1158 1159 void software_node_notify(struct device *dev) 1160 { 1161 struct swnode *swnode; 1162 int ret; 1163 1164 swnode = dev_to_swnode(dev); 1165 if (!swnode) 1166 return; 1167 1168 /* 1169 * When the software node is the device's secondary firmware node, 1170 * the core only records the owning device on the primary fwnode 1171 * (see device_add()). fw_devlink resolves a supplier device through 1172 * fwnode->dev, so without this a consumer referencing the software 1173 * node could never find the supplier device and would defer forever. 1174 * Make fwnode.dev point to its owner in that case. 1175 */ 1176 if (!device_match_fwnode(dev, &swnode->fwnode) && !swnode->fwnode.dev) 1177 swnode->fwnode.dev = dev; 1178 1179 swnode_get(swnode); 1180 ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node"); 1181 if (ret) 1182 return; 1183 1184 ret = sysfs_create_link(&swnode->kobj, &dev->kobj, dev_name(dev)); 1185 if (ret) { 1186 sysfs_remove_link(&dev->kobj, "software_node"); 1187 return; 1188 } 1189 } 1190 1191 void software_node_notify_remove(struct device *dev) 1192 { 1193 struct swnode *swnode; 1194 1195 swnode = dev_to_swnode(dev); 1196 if (!swnode) 1197 return; 1198 1199 sysfs_remove_link(&swnode->kobj, dev_name(dev)); 1200 sysfs_remove_link(&dev->kobj, "software_node"); 1201 1202 /* 1203 * Drop the device pointer mirrored onto a secondary software node in 1204 * software_node_notify(). For a primary software node the core owns 1205 * fwnode->dev and clears it in device_del(). 1206 */ 1207 if (!device_match_fwnode(dev, &swnode->fwnode) && swnode->fwnode.dev == dev) 1208 swnode->fwnode.dev = NULL; 1209 1210 swnode_put(swnode); 1211 1212 if (swnode->owner == dev) { 1213 set_secondary_fwnode(dev, NULL); 1214 swnode_put(swnode); 1215 } 1216 } 1217 1218 void __init software_node_init(void) 1219 { 1220 swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj); 1221 if (!swnode_kset) 1222 pr_err("failed to register software nodes\n"); 1223 } 1224