1/* 2 * Support for dynamic device trees. 3 * 4 * On some platforms, the device tree can be manipulated at runtime. 5 * The routines in this section support adding, removing and changing 6 * device tree nodes. 7 */ 8 9#include <linux/of.h> 10#include <linux/spinlock.h> 11#include <linux/slab.h> 12#include <linux/string.h> 13#include <linux/proc_fs.h> 14 15#include "of_private.h" 16 17/** 18 * of_node_get() - Increment refcount of a node 19 * @node: Node to inc refcount, NULL is supported to simplify writing of 20 * callers 21 * 22 * Returns node. 23 */ 24struct device_node *of_node_get(struct device_node *node) 25{ 26 if (node) 27 kobject_get(&node->kobj); 28 return node; 29} 30EXPORT_SYMBOL(of_node_get); 31 32/** 33 * of_node_put() - Decrement refcount of a node 34 * @node: Node to dec refcount, NULL is supported to simplify writing of 35 * callers 36 */ 37void of_node_put(struct device_node *node) 38{ 39 if (node) 40 kobject_put(&node->kobj); 41} 42EXPORT_SYMBOL(of_node_put); 43 44void __of_detach_node_sysfs(struct device_node *np) 45{ 46 struct property *pp; 47 48 if (!IS_ENABLED(CONFIG_SYSFS)) 49 return; 50 51 BUG_ON(!of_node_is_initialized(np)); 52 if (!of_kset) 53 return; 54 55 /* only remove properties if on sysfs */ 56 if (of_node_is_attached(np)) { 57 for_each_property_of_node(np, pp) 58 sysfs_remove_bin_file(&np->kobj, &pp->attr); 59 kobject_del(&np->kobj); 60 } 61 62 /* finally remove the kobj_init ref */ 63 of_node_put(np); 64} 65 66static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); 67 68int of_reconfig_notifier_register(struct notifier_block *nb) 69{ 70 return blocking_notifier_chain_register(&of_reconfig_chain, nb); 71} 72EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); 73 74int of_reconfig_notifier_unregister(struct notifier_block *nb) 75{ 76 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); 77} 78EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); 79 80int of_reconfig_notify(unsigned long action, void *p) 81{ 82 int rc; 83 84 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); 85 return notifier_to_errno(rc); 86} 87 88int of_property_notify(int action, struct device_node *np, 89 struct property *prop, struct property *oldprop) 90{ 91 struct of_prop_reconfig pr; 92 93 /* only call notifiers if the node is attached */ 94 if (!of_node_is_attached(np)) 95 return 0; 96 97 pr.dn = np; 98 pr.prop = prop; 99 pr.old_prop = oldprop; 100 return of_reconfig_notify(action, &pr); 101} 102 103void __of_attach_node(struct device_node *np) 104{ 105 const __be32 *phandle; 106 int sz; 107 108 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>"; 109 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>"; 110 111 phandle = __of_get_property(np, "phandle", &sz); 112 if (!phandle) 113 phandle = __of_get_property(np, "linux,phandle", &sz); 114 if (IS_ENABLED(PPC_PSERIES) && !phandle) 115 phandle = __of_get_property(np, "ibm,phandle", &sz); 116 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0; 117 118 np->child = NULL; 119 np->sibling = np->parent->child; 120 np->allnext = np->parent->allnext; 121 np->parent->allnext = np; 122 np->parent->child = np; 123 of_node_clear_flag(np, OF_DETACHED); 124} 125 126/** 127 * of_attach_node() - Plug a device node into the tree and global list. 128 */ 129int of_attach_node(struct device_node *np) 130{ 131 unsigned long flags; 132 133 mutex_lock(&of_mutex); 134 raw_spin_lock_irqsave(&devtree_lock, flags); 135 __of_attach_node(np); 136 raw_spin_unlock_irqrestore(&devtree_lock, flags); 137 138 __of_attach_node_sysfs(np); 139 mutex_unlock(&of_mutex); 140 141 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); 142 143 return 0; 144} 145 146void __of_detach_node(struct device_node *np) 147{ 148 struct device_node *parent; 149 150 if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) 151 return; 152 153 parent = np->parent; 154 if (WARN_ON(!parent)) 155 return; 156 157 if (of_allnodes == np) 158 of_allnodes = np->allnext; 159 else { 160 struct device_node *prev; 161 for (prev = of_allnodes; 162 prev->allnext != np; 163 prev = prev->allnext) 164 ; 165 prev->allnext = np->allnext; 166 } 167 168 if (parent->child == np) 169 parent->child = np->sibling; 170 else { 171 struct device_node *prevsib; 172 for (prevsib = np->parent->child; 173 prevsib->sibling != np; 174 prevsib = prevsib->sibling) 175 ; 176 prevsib->sibling = np->sibling; 177 } 178 179 of_node_set_flag(np, OF_DETACHED); 180} 181 182/** 183 * of_detach_node() - "Unplug" a node from the device tree. 184 * 185 * The caller must hold a reference to the node. The memory associated with 186 * the node is not freed until its refcount goes to zero. 187 */ 188int of_detach_node(struct device_node *np) 189{ 190 unsigned long flags; 191 int rc = 0; 192 193 mutex_lock(&of_mutex); 194 raw_spin_lock_irqsave(&devtree_lock, flags); 195 __of_detach_node(np); 196 raw_spin_unlock_irqrestore(&devtree_lock, flags); 197 198 __of_detach_node_sysfs(np); 199 mutex_unlock(&of_mutex); 200 201 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); 202 203 return rc; 204} 205 206/** 207 * of_node_release() - release a dynamically allocated node 208 * @kref: kref element of the node to be released 209 * 210 * In of_node_put() this function is passed to kref_put() as the destructor. 211 */ 212void of_node_release(struct kobject *kobj) 213{ 214 struct device_node *node = kobj_to_device_node(kobj); 215 struct property *prop = node->properties; 216 217 /* We should never be releasing nodes that haven't been detached. */ 218 if (!of_node_check_flag(node, OF_DETACHED)) { 219 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); 220 dump_stack(); 221 return; 222 } 223 224 if (!of_node_check_flag(node, OF_DYNAMIC)) 225 return; 226 227 while (prop) { 228 struct property *next = prop->next; 229 kfree(prop->name); 230 kfree(prop->value); 231 kfree(prop); 232 prop = next; 233 234 if (!prop) { 235 prop = node->deadprops; 236 node->deadprops = NULL; 237 } 238 } 239 kfree(node->full_name); 240 kfree(node->data); 241 kfree(node); 242} 243 244/** 245 * __of_prop_dup - Copy a property dynamically. 246 * @prop: Property to copy 247 * @allocflags: Allocation flags (typically pass GFP_KERNEL) 248 * 249 * Copy a property by dynamically allocating the memory of both the 250 * property structure and the property name & contents. The property's 251 * flags have the OF_DYNAMIC bit set so that we can differentiate between 252 * dynamically allocated properties and not. 253 * Returns the newly allocated property or NULL on out of memory error. 254 */ 255struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) 256{ 257 struct property *new; 258 259 new = kzalloc(sizeof(*new), allocflags); 260 if (!new) 261 return NULL; 262 263 /* 264 * NOTE: There is no check for zero length value. 265 * In case of a boolean property, this will allocate a value 266 * of zero bytes. We do this to work around the use 267 * of of_get_property() calls on boolean values. 268 */ 269 new->name = kstrdup(prop->name, allocflags); 270 new->value = kmemdup(prop->value, prop->length, allocflags); 271 new->length = prop->length; 272 if (!new->name || !new->value) 273 goto err_free; 274 275 /* mark the property as dynamic */ 276 of_property_set_flag(new, OF_DYNAMIC); 277 278 return new; 279 280 err_free: 281 kfree(new->name); 282 kfree(new->value); 283 kfree(new); 284 return NULL; 285} 286 287/** 288 * __of_node_alloc() - Create an empty device node dynamically. 289 * @full_name: Full name of the new device node 290 * @allocflags: Allocation flags (typically pass GFP_KERNEL) 291 * 292 * Create an empty device tree node, suitable for further modification. 293 * The node data are dynamically allocated and all the node flags 294 * have the OF_DYNAMIC & OF_DETACHED bits set. 295 * Returns the newly allocated node or NULL on out of memory error. 296 */ 297struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags) 298{ 299 struct device_node *node; 300 301 node = kzalloc(sizeof(*node), allocflags); 302 if (!node) 303 return NULL; 304 305 node->full_name = kstrdup(full_name, allocflags); 306 of_node_set_flag(node, OF_DYNAMIC); 307 of_node_set_flag(node, OF_DETACHED); 308 if (!node->full_name) 309 goto err_free; 310 311 of_node_init(node); 312 313 return node; 314 315 err_free: 316 kfree(node->full_name); 317 kfree(node); 318 return NULL; 319} 320 321static void __of_changeset_entry_destroy(struct of_changeset_entry *ce) 322{ 323 of_node_put(ce->np); 324 list_del(&ce->node); 325 kfree(ce); 326} 327 328#ifdef DEBUG 329static void __of_changeset_entry_dump(struct of_changeset_entry *ce) 330{ 331 switch (ce->action) { 332 case OF_RECONFIG_ADD_PROPERTY: 333 pr_debug("%p: %s %s/%s\n", 334 ce, "ADD_PROPERTY ", ce->np->full_name, 335 ce->prop->name); 336 break; 337 case OF_RECONFIG_REMOVE_PROPERTY: 338 pr_debug("%p: %s %s/%s\n", 339 ce, "REMOVE_PROPERTY", ce->np->full_name, 340 ce->prop->name); 341 break; 342 case OF_RECONFIG_UPDATE_PROPERTY: 343 pr_debug("%p: %s %s/%s\n", 344 ce, "UPDATE_PROPERTY", ce->np->full_name, 345 ce->prop->name); 346 break; 347 case OF_RECONFIG_ATTACH_NODE: 348 pr_debug("%p: %s %s\n", 349 ce, "ATTACH_NODE ", ce->np->full_name); 350 break; 351 case OF_RECONFIG_DETACH_NODE: 352 pr_debug("%p: %s %s\n", 353 ce, "DETACH_NODE ", ce->np->full_name); 354 break; 355 } 356} 357#else 358static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce) 359{ 360 /* empty */ 361} 362#endif 363 364static void __of_changeset_entry_invert(struct of_changeset_entry *ce, 365 struct of_changeset_entry *rce) 366{ 367 memcpy(rce, ce, sizeof(*rce)); 368 369 switch (ce->action) { 370 case OF_RECONFIG_ATTACH_NODE: 371 rce->action = OF_RECONFIG_DETACH_NODE; 372 break; 373 case OF_RECONFIG_DETACH_NODE: 374 rce->action = OF_RECONFIG_ATTACH_NODE; 375 break; 376 case OF_RECONFIG_ADD_PROPERTY: 377 rce->action = OF_RECONFIG_REMOVE_PROPERTY; 378 break; 379 case OF_RECONFIG_REMOVE_PROPERTY: 380 rce->action = OF_RECONFIG_ADD_PROPERTY; 381 break; 382 case OF_RECONFIG_UPDATE_PROPERTY: 383 rce->old_prop = ce->prop; 384 rce->prop = ce->old_prop; 385 break; 386 } 387} 388 389static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert) 390{ 391 struct of_changeset_entry ce_inverted; 392 int ret; 393 394 if (revert) { 395 __of_changeset_entry_invert(ce, &ce_inverted); 396 ce = &ce_inverted; 397 } 398 399 switch (ce->action) { 400 case OF_RECONFIG_ATTACH_NODE: 401 case OF_RECONFIG_DETACH_NODE: 402 ret = of_reconfig_notify(ce->action, ce->np); 403 break; 404 case OF_RECONFIG_ADD_PROPERTY: 405 case OF_RECONFIG_REMOVE_PROPERTY: 406 case OF_RECONFIG_UPDATE_PROPERTY: 407 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop); 408 break; 409 default: 410 pr_err("%s: invalid devicetree changeset action: %i\n", __func__, 411 (int)ce->action); 412 return; 413 } 414 415 if (ret) 416 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name); 417} 418 419static int __of_changeset_entry_apply(struct of_changeset_entry *ce) 420{ 421 struct property *old_prop, **propp; 422 unsigned long flags; 423 int ret = 0; 424 425 __of_changeset_entry_dump(ce); 426 427 raw_spin_lock_irqsave(&devtree_lock, flags); 428 switch (ce->action) { 429 case OF_RECONFIG_ATTACH_NODE: 430 __of_attach_node(ce->np); 431 break; 432 case OF_RECONFIG_DETACH_NODE: 433 __of_detach_node(ce->np); 434 break; 435 case OF_RECONFIG_ADD_PROPERTY: 436 /* If the property is in deadprops then it must be removed */ 437 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { 438 if (*propp == ce->prop) { 439 *propp = ce->prop->next; 440 ce->prop->next = NULL; 441 break; 442 } 443 } 444 445 ret = __of_add_property(ce->np, ce->prop); 446 if (ret) { 447 pr_err("%s: add_property failed @%s/%s\n", 448 __func__, ce->np->full_name, 449 ce->prop->name); 450 break; 451 } 452 break; 453 case OF_RECONFIG_REMOVE_PROPERTY: 454 ret = __of_remove_property(ce->np, ce->prop); 455 if (ret) { 456 pr_err("%s: remove_property failed @%s/%s\n", 457 __func__, ce->np->full_name, 458 ce->prop->name); 459 break; 460 } 461 break; 462 463 case OF_RECONFIG_UPDATE_PROPERTY: 464 /* If the property is in deadprops then it must be removed */ 465 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { 466 if (*propp == ce->prop) { 467 *propp = ce->prop->next; 468 ce->prop->next = NULL; 469 break; 470 } 471 } 472 473 ret = __of_update_property(ce->np, ce->prop, &old_prop); 474 if (ret) { 475 pr_err("%s: update_property failed @%s/%s\n", 476 __func__, ce->np->full_name, 477 ce->prop->name); 478 break; 479 } 480 break; 481 default: 482 ret = -EINVAL; 483 } 484 raw_spin_unlock_irqrestore(&devtree_lock, flags); 485 486 if (ret) 487 return ret; 488 489 switch (ce->action) { 490 case OF_RECONFIG_ATTACH_NODE: 491 __of_attach_node_sysfs(ce->np); 492 break; 493 case OF_RECONFIG_DETACH_NODE: 494 __of_detach_node_sysfs(ce->np); 495 break; 496 case OF_RECONFIG_ADD_PROPERTY: 497 /* ignore duplicate names */ 498 __of_add_property_sysfs(ce->np, ce->prop); 499 break; 500 case OF_RECONFIG_REMOVE_PROPERTY: 501 __of_remove_property_sysfs(ce->np, ce->prop); 502 break; 503 case OF_RECONFIG_UPDATE_PROPERTY: 504 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop); 505 break; 506 } 507 508 return 0; 509} 510 511static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce) 512{ 513 struct of_changeset_entry ce_inverted; 514 515 __of_changeset_entry_invert(ce, &ce_inverted); 516 return __of_changeset_entry_apply(&ce_inverted); 517} 518 519/** 520 * of_changeset_init - Initialize a changeset for use 521 * 522 * @ocs: changeset pointer 523 * 524 * Initialize a changeset structure 525 */ 526void of_changeset_init(struct of_changeset *ocs) 527{ 528 memset(ocs, 0, sizeof(*ocs)); 529 INIT_LIST_HEAD(&ocs->entries); 530} 531 532/** 533 * of_changeset_destroy - Destroy a changeset 534 * 535 * @ocs: changeset pointer 536 * 537 * Destroys a changeset. Note that if a changeset is applied, 538 * its changes to the tree cannot be reverted. 539 */ 540void of_changeset_destroy(struct of_changeset *ocs) 541{ 542 struct of_changeset_entry *ce, *cen; 543 544 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node) 545 __of_changeset_entry_destroy(ce); 546} 547 548/** 549 * of_changeset_apply - Applies a changeset 550 * 551 * @ocs: changeset pointer 552 * 553 * Applies a changeset to the live tree. 554 * Any side-effects of live tree state changes are applied here on 555 * sucess, like creation/destruction of devices and side-effects 556 * like creation of sysfs properties and directories. 557 * Returns 0 on success, a negative error value in case of an error. 558 * On error the partially applied effects are reverted. 559 */ 560int of_changeset_apply(struct of_changeset *ocs) 561{ 562 struct of_changeset_entry *ce; 563 int ret; 564 565 /* perform the rest of the work */ 566 pr_debug("of_changeset: applying...\n"); 567 list_for_each_entry(ce, &ocs->entries, node) { 568 ret = __of_changeset_entry_apply(ce); 569 if (ret) { 570 pr_err("%s: Error applying changeset (%d)\n", __func__, ret); 571 list_for_each_entry_continue_reverse(ce, &ocs->entries, node) 572 __of_changeset_entry_revert(ce); 573 return ret; 574 } 575 } 576 pr_debug("of_changeset: applied, emitting notifiers.\n"); 577 578 /* drop the global lock while emitting notifiers */ 579 mutex_unlock(&of_mutex); 580 list_for_each_entry(ce, &ocs->entries, node) 581 __of_changeset_entry_notify(ce, 0); 582 mutex_lock(&of_mutex); 583 pr_debug("of_changeset: notifiers sent.\n"); 584 585 return 0; 586} 587 588/** 589 * of_changeset_revert - Reverts an applied changeset 590 * 591 * @ocs: changeset pointer 592 * 593 * Reverts a changeset returning the state of the tree to what it 594 * was before the application. 595 * Any side-effects like creation/destruction of devices and 596 * removal of sysfs properties and directories are applied. 597 * Returns 0 on success, a negative error value in case of an error. 598 */ 599int of_changeset_revert(struct of_changeset *ocs) 600{ 601 struct of_changeset_entry *ce; 602 int ret; 603 604 pr_debug("of_changeset: reverting...\n"); 605 list_for_each_entry_reverse(ce, &ocs->entries, node) { 606 ret = __of_changeset_entry_revert(ce); 607 if (ret) { 608 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret); 609 list_for_each_entry_continue(ce, &ocs->entries, node) 610 __of_changeset_entry_apply(ce); 611 return ret; 612 } 613 } 614 pr_debug("of_changeset: reverted, emitting notifiers.\n"); 615 616 /* drop the global lock while emitting notifiers */ 617 mutex_unlock(&of_mutex); 618 list_for_each_entry_reverse(ce, &ocs->entries, node) 619 __of_changeset_entry_notify(ce, 1); 620 mutex_lock(&of_mutex); 621 pr_debug("of_changeset: notifiers sent.\n"); 622 623 return 0; 624} 625 626/** 627 * of_changeset_action - Perform a changeset action 628 * 629 * @ocs: changeset pointer 630 * @action: action to perform 631 * @np: Pointer to device node 632 * @prop: Pointer to property 633 * 634 * On action being one of: 635 * + OF_RECONFIG_ATTACH_NODE 636 * + OF_RECONFIG_DETACH_NODE, 637 * + OF_RECONFIG_ADD_PROPERTY 638 * + OF_RECONFIG_REMOVE_PROPERTY, 639 * + OF_RECONFIG_UPDATE_PROPERTY 640 * Returns 0 on success, a negative error value in case of an error. 641 */ 642int of_changeset_action(struct of_changeset *ocs, unsigned long action, 643 struct device_node *np, struct property *prop) 644{ 645 struct of_changeset_entry *ce; 646 647 ce = kzalloc(sizeof(*ce), GFP_KERNEL); 648 if (!ce) { 649 pr_err("%s: Failed to allocate\n", __func__); 650 return -ENOMEM; 651 } 652 /* get a reference to the node */ 653 ce->action = action; 654 ce->np = of_node_get(np); 655 ce->prop = prop; 656 657 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop) 658 ce->old_prop = of_find_property(np, prop->name, NULL); 659 660 /* add it to the list */ 661 list_add_tail(&ce->node, &ocs->entries); 662 return 0; 663} 664