1 // SPDX-License-Identifier: GPL-2.0
3 * Support for dynamic device trees.
5 * On some platforms, the device tree can be manipulated at runtime.
6 * The routines in this section support adding, removing and changing
10 #define pr_fmt(fmt) "OF: " fmt
13 #include <linux/spinlock.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/proc_fs.h>
18 #include "of_private.h"
20 static struct device_node *kobj_to_device_node(struct kobject *kobj)
22 return container_of(kobj, struct device_node, kobj);
26 * of_node_get() - Increment refcount of a node
27 * @node: Node to inc refcount, NULL is supported to simplify writing of
30 * Return: The node with refcount incremented.
32 struct device_node *of_node_get(struct device_node *node)
35 kobject_get(&node->kobj);
38 EXPORT_SYMBOL(of_node_get);
41 * of_node_put() - Decrement refcount of a node
42 * @node: Node to dec refcount, NULL is supported to simplify writing of
45 void of_node_put(struct device_node *node)
48 kobject_put(&node->kobj);
50 EXPORT_SYMBOL(of_node_put);
52 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
54 int of_reconfig_notifier_register(struct notifier_block *nb)
56 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
58 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
60 int of_reconfig_notifier_unregister(struct notifier_block *nb)
62 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
64 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
66 static const char *action_names[] = {
68 [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
69 [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
70 [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
71 [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
72 [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
75 #define _do_print(func, prefix, action, node, prop, ...) ({ \
76 func("changeset: " prefix "%-15s %pOF%s%s\n", \
77 ##__VA_ARGS__, action_names[action], node, \
78 prop ? ":" : "", prop ? prop->name : ""); \
80 #define of_changeset_action_err(...) _do_print(pr_err, __VA_ARGS__)
81 #define of_changeset_action_debug(...) _do_print(pr_debug, __VA_ARGS__)
83 int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
86 struct of_reconfig_data *pr = p;
88 of_changeset_action_debug("notify: ", action, pr->dn, pr->prop);
90 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
91 return notifier_to_errno(rc);
95 * of_reconfig_get_state_change() - Returns new state of device
96 * @action - action of the of notifier
97 * @arg - argument of the of notifier
99 * Returns the new state of a device based on the notifier used.
101 * Return: 0 on device going from enabled to disabled, 1 on device
102 * going from disabled to enabled and -1 on no change.
104 int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
106 struct property *prop, *old_prop = NULL;
107 int is_status, status_state, old_status_state, prev_state, new_state;
109 /* figure out if a device should be created or destroyed */
111 case OF_RECONFIG_ATTACH_NODE:
112 case OF_RECONFIG_DETACH_NODE:
113 prop = of_find_property(pr->dn, "status", NULL);
115 case OF_RECONFIG_ADD_PROPERTY:
116 case OF_RECONFIG_REMOVE_PROPERTY:
119 case OF_RECONFIG_UPDATE_PROPERTY:
121 old_prop = pr->old_prop;
124 return OF_RECONFIG_NO_CHANGE;
129 old_status_state = -1;
133 if (prop && !strcmp(prop->name, "status")) {
135 status_state = !strcmp(prop->value, "okay") ||
136 !strcmp(prop->value, "ok");
138 old_status_state = !strcmp(old_prop->value, "okay") ||
139 !strcmp(old_prop->value, "ok");
143 case OF_RECONFIG_ATTACH_NODE:
145 /* -1 & 0 status either missing or okay */
146 new_state = status_state != 0;
148 case OF_RECONFIG_DETACH_NODE:
149 /* -1 & 0 status either missing or okay */
150 prev_state = status_state != 0;
153 case OF_RECONFIG_ADD_PROPERTY:
155 /* no status property -> enabled (legacy) */
157 new_state = status_state;
160 case OF_RECONFIG_REMOVE_PROPERTY:
162 prev_state = status_state;
163 /* no status property -> enabled (legacy) */
167 case OF_RECONFIG_UPDATE_PROPERTY:
169 prev_state = old_status_state != 0;
170 new_state = status_state != 0;
175 if (prev_state == new_state)
176 return OF_RECONFIG_NO_CHANGE;
178 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
180 EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
182 int of_property_notify(int action, struct device_node *np,
183 struct property *prop, struct property *oldprop)
185 struct of_reconfig_data pr;
187 /* only call notifiers if the node is attached */
188 if (!of_node_is_attached(np))
193 pr.old_prop = oldprop;
194 return of_reconfig_notify(action, &pr);
197 static void __of_attach_node(struct device_node *np)
199 const __be32 *phandle;
203 raw_spin_lock_irqsave(&devtree_lock, flags);
205 if (!of_node_check_flag(np, OF_OVERLAY)) {
206 np->name = __of_get_property(np, "name", NULL);
210 phandle = __of_get_property(np, "phandle", &sz);
212 phandle = __of_get_property(np, "linux,phandle", &sz);
213 if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
214 phandle = __of_get_property(np, "ibm,phandle", &sz);
215 if (phandle && (sz >= 4))
216 np->phandle = be32_to_cpup(phandle);
222 np->sibling = np->parent->child;
223 np->parent->child = np;
224 of_node_clear_flag(np, OF_DETACHED);
225 np->fwnode.flags |= FWNODE_FLAG_NOT_DEVICE;
227 raw_spin_unlock_irqrestore(&devtree_lock, flags);
229 __of_attach_node_sysfs(np);
233 * of_attach_node() - Plug a device node into the tree and global list.
234 * @np: Pointer to the caller's Device Node
236 int of_attach_node(struct device_node *np)
238 struct of_reconfig_data rd;
240 memset(&rd, 0, sizeof(rd));
243 mutex_lock(&of_mutex);
244 __of_attach_node(np);
245 mutex_unlock(&of_mutex);
247 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
252 void __of_detach_node(struct device_node *np)
254 struct device_node *parent;
257 raw_spin_lock_irqsave(&devtree_lock, flags);
260 if (WARN_ON(of_node_check_flag(np, OF_DETACHED) || !parent)) {
261 raw_spin_unlock_irqrestore(&devtree_lock, flags);
265 if (parent->child == np)
266 parent->child = np->sibling;
268 struct device_node *prevsib;
269 for (prevsib = np->parent->child;
270 prevsib->sibling != np;
271 prevsib = prevsib->sibling)
273 prevsib->sibling = np->sibling;
276 of_node_set_flag(np, OF_DETACHED);
278 /* race with of_find_node_by_phandle() prevented by devtree_lock */
279 __of_phandle_cache_inv_entry(np->phandle);
281 raw_spin_unlock_irqrestore(&devtree_lock, flags);
283 __of_detach_node_sysfs(np);
287 * of_detach_node() - "Unplug" a node from the device tree.
288 * @np: Pointer to the caller's Device Node
290 int of_detach_node(struct device_node *np)
292 struct of_reconfig_data rd;
294 memset(&rd, 0, sizeof(rd));
297 mutex_lock(&of_mutex);
298 __of_detach_node(np);
299 mutex_unlock(&of_mutex);
301 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
305 EXPORT_SYMBOL_GPL(of_detach_node);
307 static void property_list_free(struct property *prop_list)
309 struct property *prop, *next;
311 for (prop = prop_list; prop != NULL; prop = next) {
320 * of_node_release() - release a dynamically allocated node
321 * @kobj: kernel object of the node to be released
323 * In of_node_put() this function is passed to kref_put() as the destructor.
325 void of_node_release(struct kobject *kobj)
327 struct device_node *node = kobj_to_device_node(kobj);
330 * can not use '"%pOF", node' in pr_err() calls from this function
331 * because an of_node_get(node) when refcount is already zero
332 * will result in an error and a stack dump
335 /* We should never be releasing nodes that haven't been detached. */
336 if (!of_node_check_flag(node, OF_DETACHED)) {
338 pr_err("ERROR: %s() detected bad of_node_put() on %pOF/%s\n",
339 __func__, node->parent, node->full_name);
342 * of unittests will test this path. Do not print the stack
343 * trace when the error is caused by unittest so that we do
344 * not display what a normal developer might reasonably
345 * consider a real bug.
347 if (!IS_ENABLED(CONFIG_OF_UNITTEST) ||
348 strcmp(node->parent->full_name, "testcase-data")) {
350 pr_err("ERROR: next of_node_put() on this node will result in a kobject warning 'refcount_t: underflow; use-after-free.'\n");
355 if (!of_node_check_flag(node, OF_DYNAMIC))
358 if (of_node_check_flag(node, OF_OVERLAY)) {
360 if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET)) {
361 /* premature refcount of zero, do not free memory */
362 pr_err("ERROR: memory leak before free overlay changeset, %pOF\n",
368 * If node->properties non-empty then properties were added
369 * to this node either by different overlay that has not
370 * yet been removed, or by a non-overlay mechanism.
372 if (node->properties)
373 pr_err("ERROR: %s(), unexpected properties in %pOF\n",
378 pr_err("ERROR: %s() unexpected children for %pOF/%s\n",
379 __func__, node->parent, node->full_name);
381 property_list_free(node->properties);
382 property_list_free(node->deadprops);
383 fwnode_links_purge(of_fwnode_handle(node));
385 kfree(node->full_name);
391 * __of_prop_dup - Copy a property dynamically.
392 * @prop: Property to copy
393 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
395 * Copy a property by dynamically allocating the memory of both the
396 * property structure and the property name & contents. The property's
397 * flags have the OF_DYNAMIC bit set so that we can differentiate between
398 * dynamically allocated properties and not.
400 * Return: The newly allocated property or NULL on out of memory error.
402 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
404 struct property *new;
406 new = kzalloc(sizeof(*new), allocflags);
411 * NOTE: There is no check for zero length value.
412 * In case of a boolean property, this will allocate a value
413 * of zero bytes. We do this to work around the use
414 * of of_get_property() calls on boolean values.
416 new->name = kstrdup(prop->name, allocflags);
417 new->value = kmemdup(prop->value, prop->length, allocflags);
418 new->length = prop->length;
419 if (!new->name || !new->value)
422 /* mark the property as dynamic */
423 of_property_set_flag(new, OF_DYNAMIC);
435 * __of_node_dup() - Duplicate or create an empty device node dynamically.
436 * @np: if not NULL, contains properties to be duplicated in new node
437 * @full_name: string value to be duplicated into new node's full_name field
439 * Create a device tree node, optionally duplicating the properties of
440 * another node. The node data are dynamically allocated and all the node
441 * flags have the OF_DYNAMIC & OF_DETACHED bits set.
443 * Return: The newly allocated node or NULL on out of memory error. Use
444 * of_node_put() on it when done to free the memory allocated for it.
446 struct device_node *__of_node_dup(const struct device_node *np,
447 const char *full_name)
449 struct device_node *node;
451 node = kzalloc(sizeof(*node), GFP_KERNEL);
454 node->full_name = kstrdup(full_name, GFP_KERNEL);
455 if (!node->full_name) {
460 of_node_set_flag(node, OF_DYNAMIC);
461 of_node_set_flag(node, OF_DETACHED);
464 /* Iterate over and duplicate all properties */
466 struct property *pp, *new_pp;
467 for_each_property_of_node(np, pp) {
468 new_pp = __of_prop_dup(pp, GFP_KERNEL);
471 if (__of_add_property(node, new_pp)) {
473 kfree(new_pp->value);
482 of_node_put(node); /* Frees the node and properties */
486 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
488 if (ce->action == OF_RECONFIG_ATTACH_NODE &&
489 of_node_check_flag(ce->np, OF_OVERLAY)) {
490 if (kref_read(&ce->np->kobj.kref) > 1) {
491 pr_err("ERROR: memory leak, expected refcount 1 instead of %d, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node %pOF\n",
492 kref_read(&ce->np->kobj.kref), ce->np);
494 of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET);
503 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
504 struct of_changeset_entry *rce)
506 memcpy(rce, ce, sizeof(*rce));
508 switch (ce->action) {
509 case OF_RECONFIG_ATTACH_NODE:
510 rce->action = OF_RECONFIG_DETACH_NODE;
512 case OF_RECONFIG_DETACH_NODE:
513 rce->action = OF_RECONFIG_ATTACH_NODE;
515 case OF_RECONFIG_ADD_PROPERTY:
516 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
518 case OF_RECONFIG_REMOVE_PROPERTY:
519 rce->action = OF_RECONFIG_ADD_PROPERTY;
521 case OF_RECONFIG_UPDATE_PROPERTY:
522 rce->old_prop = ce->prop;
523 rce->prop = ce->old_prop;
524 /* update was used but original property did not exist */
526 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
527 rce->prop = ce->prop;
533 static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
536 struct of_reconfig_data rd;
537 struct of_changeset_entry ce_inverted;
541 __of_changeset_entry_invert(ce, &ce_inverted);
545 switch (ce->action) {
546 case OF_RECONFIG_ATTACH_NODE:
547 case OF_RECONFIG_DETACH_NODE:
548 memset(&rd, 0, sizeof(rd));
550 ret = of_reconfig_notify(ce->action, &rd);
552 case OF_RECONFIG_ADD_PROPERTY:
553 case OF_RECONFIG_REMOVE_PROPERTY:
554 case OF_RECONFIG_UPDATE_PROPERTY:
555 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
558 pr_err("invalid devicetree changeset action: %i\n",
564 pr_err("changeset notifier error @%pOF\n", ce->np);
568 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
572 of_changeset_action_debug("apply: ", ce->action, ce->np, ce->prop);
574 switch (ce->action) {
575 case OF_RECONFIG_ATTACH_NODE:
576 __of_attach_node(ce->np);
578 case OF_RECONFIG_DETACH_NODE:
579 __of_detach_node(ce->np);
581 case OF_RECONFIG_ADD_PROPERTY:
582 ret = __of_add_property(ce->np, ce->prop);
584 case OF_RECONFIG_REMOVE_PROPERTY:
585 ret = __of_remove_property(ce->np, ce->prop);
588 case OF_RECONFIG_UPDATE_PROPERTY:
589 ret = __of_update_property(ce->np, ce->prop, &ce->old_prop);
596 of_changeset_action_err("apply failed: ", ce->action, ce->np, ce->prop);
603 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
605 struct of_changeset_entry ce_inverted;
607 __of_changeset_entry_invert(ce, &ce_inverted);
608 return __of_changeset_entry_apply(&ce_inverted);
612 * of_changeset_init - Initialize a changeset for use
614 * @ocs: changeset pointer
616 * Initialize a changeset structure
618 void of_changeset_init(struct of_changeset *ocs)
620 memset(ocs, 0, sizeof(*ocs));
621 INIT_LIST_HEAD(&ocs->entries);
623 EXPORT_SYMBOL_GPL(of_changeset_init);
626 * of_changeset_destroy - Destroy a changeset
628 * @ocs: changeset pointer
630 * Destroys a changeset. Note that if a changeset is applied,
631 * its changes to the tree cannot be reverted.
633 void of_changeset_destroy(struct of_changeset *ocs)
635 struct of_changeset_entry *ce, *cen;
637 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
638 __of_changeset_entry_destroy(ce);
640 EXPORT_SYMBOL_GPL(of_changeset_destroy);
643 * Apply the changeset entries in @ocs.
644 * If apply fails, an attempt is made to revert the entries that were
645 * successfully applied.
647 * If multiple revert errors occur then only the final revert error is reported.
649 * Returns 0 on success, a negative error value in case of an error.
650 * If a revert error occurs, it is returned in *ret_revert.
652 int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
654 struct of_changeset_entry *ce;
657 pr_debug("changeset: applying...\n");
658 list_for_each_entry(ce, &ocs->entries, node) {
659 ret = __of_changeset_entry_apply(ce);
661 pr_err("Error applying changeset (%d)\n", ret);
662 list_for_each_entry_continue_reverse(ce, &ocs->entries,
664 ret_tmp = __of_changeset_entry_revert(ce);
666 *ret_revert = ret_tmp;
676 * Returns 0 on success, a negative error value in case of an error.
678 * If multiple changeset entry notification errors occur then only the
679 * final notification error is reported.
681 int __of_changeset_apply_notify(struct of_changeset *ocs)
683 struct of_changeset_entry *ce;
684 int ret = 0, ret_tmp;
686 pr_debug("changeset: emitting notifiers.\n");
688 /* drop the global lock while emitting notifiers */
689 mutex_unlock(&of_mutex);
690 list_for_each_entry(ce, &ocs->entries, node) {
691 ret_tmp = __of_changeset_entry_notify(ce, 0);
695 mutex_lock(&of_mutex);
696 pr_debug("changeset: notifiers sent.\n");
702 * Returns 0 on success, a negative error value in case of an error.
704 * If a changeset entry apply fails, an attempt is made to revert any
705 * previous entries in the changeset. If any of the reverts fails,
706 * that failure is not reported. Thus the state of the device tree
707 * is unknown if an apply error occurs.
709 static int __of_changeset_apply(struct of_changeset *ocs)
711 int ret, ret_revert = 0;
713 ret = __of_changeset_apply_entries(ocs, &ret_revert);
715 ret = __of_changeset_apply_notify(ocs);
721 * of_changeset_apply - Applies a changeset
723 * @ocs: changeset pointer
725 * Applies a changeset to the live tree.
726 * Any side-effects of live tree state changes are applied here on
727 * success, like creation/destruction of devices and side-effects
728 * like creation of sysfs properties and directories.
730 * Return: 0 on success, a negative error value in case of an error.
731 * On error the partially applied effects are reverted.
733 int of_changeset_apply(struct of_changeset *ocs)
737 mutex_lock(&of_mutex);
738 ret = __of_changeset_apply(ocs);
739 mutex_unlock(&of_mutex);
743 EXPORT_SYMBOL_GPL(of_changeset_apply);
746 * Revert the changeset entries in @ocs.
747 * If revert fails, an attempt is made to re-apply the entries that were
748 * successfully removed.
750 * If multiple re-apply errors occur then only the final apply error is
753 * Returns 0 on success, a negative error value in case of an error.
754 * If an apply error occurs, it is returned in *ret_apply.
756 int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
758 struct of_changeset_entry *ce;
761 pr_debug("changeset: reverting...\n");
762 list_for_each_entry_reverse(ce, &ocs->entries, node) {
763 ret = __of_changeset_entry_revert(ce);
765 pr_err("Error reverting changeset (%d)\n", ret);
766 list_for_each_entry_continue(ce, &ocs->entries, node) {
767 ret_tmp = __of_changeset_entry_apply(ce);
769 *ret_apply = ret_tmp;
779 * If multiple changeset entry notification errors occur then only the
780 * final notification error is reported.
782 int __of_changeset_revert_notify(struct of_changeset *ocs)
784 struct of_changeset_entry *ce;
785 int ret = 0, ret_tmp;
787 pr_debug("changeset: emitting notifiers.\n");
789 /* drop the global lock while emitting notifiers */
790 mutex_unlock(&of_mutex);
791 list_for_each_entry_reverse(ce, &ocs->entries, node) {
792 ret_tmp = __of_changeset_entry_notify(ce, 1);
796 mutex_lock(&of_mutex);
797 pr_debug("changeset: notifiers sent.\n");
802 static int __of_changeset_revert(struct of_changeset *ocs)
807 ret = __of_changeset_revert_entries(ocs, &ret_reply);
810 ret = __of_changeset_revert_notify(ocs);
816 * of_changeset_revert - Reverts an applied changeset
818 * @ocs: changeset pointer
820 * Reverts a changeset returning the state of the tree to what it
821 * was before the application.
822 * Any side-effects like creation/destruction of devices and
823 * removal of sysfs properties and directories are applied.
825 * Return: 0 on success, a negative error value in case of an error.
827 int of_changeset_revert(struct of_changeset *ocs)
831 mutex_lock(&of_mutex);
832 ret = __of_changeset_revert(ocs);
833 mutex_unlock(&of_mutex);
837 EXPORT_SYMBOL_GPL(of_changeset_revert);
840 * of_changeset_action - Add an action to the tail of the changeset list
842 * @ocs: changeset pointer
843 * @action: action to perform
844 * @np: Pointer to device node
845 * @prop: Pointer to property
847 * On action being one of:
848 * + OF_RECONFIG_ATTACH_NODE
849 * + OF_RECONFIG_DETACH_NODE,
850 * + OF_RECONFIG_ADD_PROPERTY
851 * + OF_RECONFIG_REMOVE_PROPERTY,
852 * + OF_RECONFIG_UPDATE_PROPERTY
854 * Return: 0 on success, a negative error value in case of an error.
856 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
857 struct device_node *np, struct property *prop)
859 struct of_changeset_entry *ce;
861 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
865 if (WARN_ON(action >= ARRAY_SIZE(action_names)))
868 /* get a reference to the node */
870 ce->np = of_node_get(np);
873 /* add it to the list */
874 list_add_tail(&ce->node, &ocs->entries);
877 EXPORT_SYMBOL_GPL(of_changeset_action);