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
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);
67 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",
76 int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
80 struct of_reconfig_data *pr = p;
83 case OF_RECONFIG_ATTACH_NODE:
84 case OF_RECONFIG_DETACH_NODE:
85 pr_debug("notify %-15s %pOF\n", action_names[action],
88 case OF_RECONFIG_ADD_PROPERTY:
89 case OF_RECONFIG_REMOVE_PROPERTY:
90 case OF_RECONFIG_UPDATE_PROPERTY:
91 pr_debug("notify %-15s %pOF:%s\n", action_names[action],
92 pr->dn, pr->prop->name);
97 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
98 return notifier_to_errno(rc);
102 * of_reconfig_get_state_change() - Returns new state of device
103 * @action - action of the of notifier
104 * @arg - argument of the of notifier
106 * Returns the new state of a device based on the notifier used.
107 * Returns 0 on device going from enabled to disabled, 1 on device
108 * going from disabled to enabled and -1 on no change.
110 int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
112 struct property *prop, *old_prop = NULL;
113 int is_status, status_state, old_status_state, prev_state, new_state;
115 /* figure out if a device should be created or destroyed */
117 case OF_RECONFIG_ATTACH_NODE:
118 case OF_RECONFIG_DETACH_NODE:
119 prop = of_find_property(pr->dn, "status", NULL);
121 case OF_RECONFIG_ADD_PROPERTY:
122 case OF_RECONFIG_REMOVE_PROPERTY:
125 case OF_RECONFIG_UPDATE_PROPERTY:
127 old_prop = pr->old_prop;
130 return OF_RECONFIG_NO_CHANGE;
135 old_status_state = -1;
139 if (prop && !strcmp(prop->name, "status")) {
141 status_state = !strcmp(prop->value, "okay") ||
142 !strcmp(prop->value, "ok");
144 old_status_state = !strcmp(old_prop->value, "okay") ||
145 !strcmp(old_prop->value, "ok");
149 case OF_RECONFIG_ATTACH_NODE:
151 /* -1 & 0 status either missing or okay */
152 new_state = status_state != 0;
154 case OF_RECONFIG_DETACH_NODE:
155 /* -1 & 0 status either missing or okay */
156 prev_state = status_state != 0;
159 case OF_RECONFIG_ADD_PROPERTY:
161 /* no status property -> enabled (legacy) */
163 new_state = status_state;
166 case OF_RECONFIG_REMOVE_PROPERTY:
168 prev_state = status_state;
169 /* no status property -> enabled (legacy) */
173 case OF_RECONFIG_UPDATE_PROPERTY:
175 prev_state = old_status_state != 0;
176 new_state = status_state != 0;
181 if (prev_state == new_state)
182 return OF_RECONFIG_NO_CHANGE;
184 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
186 EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
188 int of_property_notify(int action, struct device_node *np,
189 struct property *prop, struct property *oldprop)
191 struct of_reconfig_data pr;
193 /* only call notifiers if the node is attached */
194 if (!of_node_is_attached(np))
199 pr.old_prop = oldprop;
200 return of_reconfig_notify(action, &pr);
203 static void __of_attach_node(struct device_node *np)
205 const __be32 *phandle;
208 if (!of_node_check_flag(np, OF_OVERLAY)) {
209 np->name = __of_get_property(np, "name", NULL);
210 np->type = __of_get_property(np, "device_type", NULL);
216 phandle = __of_get_property(np, "phandle", &sz);
218 phandle = __of_get_property(np, "linux,phandle", &sz);
219 if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
220 phandle = __of_get_property(np, "ibm,phandle", &sz);
221 if (phandle && (sz >= 4))
222 np->phandle = be32_to_cpup(phandle);
228 np->sibling = np->parent->child;
229 np->parent->child = np;
230 of_node_clear_flag(np, OF_DETACHED);
234 * of_attach_node() - Plug a device node into the tree and global list.
236 int of_attach_node(struct device_node *np)
238 struct of_reconfig_data rd;
241 memset(&rd, 0, sizeof(rd));
244 mutex_lock(&of_mutex);
245 raw_spin_lock_irqsave(&devtree_lock, flags);
246 __of_attach_node(np);
247 raw_spin_unlock_irqrestore(&devtree_lock, flags);
249 __of_attach_node_sysfs(np);
250 mutex_unlock(&of_mutex);
252 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
257 void __of_detach_node(struct device_node *np)
259 struct device_node *parent;
261 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
265 if (WARN_ON(!parent))
268 if (parent->child == np)
269 parent->child = np->sibling;
271 struct device_node *prevsib;
272 for (prevsib = np->parent->child;
273 prevsib->sibling != np;
274 prevsib = prevsib->sibling)
276 prevsib->sibling = np->sibling;
279 of_node_set_flag(np, OF_DETACHED);
281 /* race with of_find_node_by_phandle() prevented by devtree_lock */
282 __of_free_phandle_cache_entry(np->phandle);
286 * of_detach_node() - "Unplug" a node from the device tree.
288 int of_detach_node(struct device_node *np)
290 struct of_reconfig_data rd;
294 memset(&rd, 0, sizeof(rd));
297 mutex_lock(&of_mutex);
298 raw_spin_lock_irqsave(&devtree_lock, flags);
299 __of_detach_node(np);
300 raw_spin_unlock_irqrestore(&devtree_lock, flags);
302 __of_detach_node_sysfs(np);
303 mutex_unlock(&of_mutex);
305 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
309 EXPORT_SYMBOL_GPL(of_detach_node);
311 static void property_list_free(struct property *prop_list)
313 struct property *prop, *next;
315 for (prop = prop_list; prop != NULL; prop = next) {
324 * of_node_release() - release a dynamically allocated node
325 * @kref: kref element of the node to be released
327 * In of_node_put() this function is passed to kref_put() as the destructor.
329 void of_node_release(struct kobject *kobj)
331 struct device_node *node = kobj_to_device_node(kobj);
333 /* We should never be releasing nodes that haven't been detached. */
334 if (!of_node_check_flag(node, OF_DETACHED)) {
335 pr_err("ERROR: Bad of_node_put() on %pOF\n", node);
339 if (!of_node_check_flag(node, OF_DYNAMIC))
342 if (of_node_check_flag(node, OF_OVERLAY)) {
344 if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET)) {
345 /* premature refcount of zero, do not free memory */
346 pr_err("ERROR: memory leak before free overlay changeset, %pOF\n",
352 * If node->properties non-empty then properties were added
353 * to this node either by different overlay that has not
354 * yet been removed, or by a non-overlay mechanism.
356 if (node->properties)
357 pr_err("ERROR: %s(), unexpected properties in %pOF\n",
361 property_list_free(node->properties);
362 property_list_free(node->deadprops);
364 kfree(node->full_name);
370 * __of_prop_dup - Copy a property dynamically.
371 * @prop: Property to copy
372 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
374 * Copy a property by dynamically allocating the memory of both the
375 * property structure and the property name & contents. The property's
376 * flags have the OF_DYNAMIC bit set so that we can differentiate between
377 * dynamically allocated properties and not.
378 * Returns the newly allocated property or NULL on out of memory error.
380 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
382 struct property *new;
384 new = kzalloc(sizeof(*new), allocflags);
389 * NOTE: There is no check for zero length value.
390 * In case of a boolean property, this will allocate a value
391 * of zero bytes. We do this to work around the use
392 * of of_get_property() calls on boolean values.
394 new->name = kstrdup(prop->name, allocflags);
395 new->value = kmemdup(prop->value, prop->length, allocflags);
396 new->length = prop->length;
397 if (!new->name || !new->value)
400 /* mark the property as dynamic */
401 of_property_set_flag(new, OF_DYNAMIC);
413 * __of_node_dup() - Duplicate or create an empty device node dynamically.
414 * @np: if not NULL, contains properties to be duplicated in new node
415 * @full_name: string value to be duplicated into new node's full_name field
417 * Create a device tree node, optionally duplicating the properties of
418 * another node. The node data are dynamically allocated and all the node
419 * flags have the OF_DYNAMIC & OF_DETACHED bits set.
421 * Returns the newly allocated node or NULL on out of memory error.
423 struct device_node *__of_node_dup(const struct device_node *np,
424 const char *full_name)
426 struct device_node *node;
428 node = kzalloc(sizeof(*node), GFP_KERNEL);
431 node->full_name = kstrdup(full_name, GFP_KERNEL);
432 if (!node->full_name) {
437 of_node_set_flag(node, OF_DYNAMIC);
438 of_node_set_flag(node, OF_DETACHED);
441 /* Iterate over and duplicate all properties */
443 struct property *pp, *new_pp;
444 for_each_property_of_node(np, pp) {
445 new_pp = __of_prop_dup(pp, GFP_KERNEL);
448 if (__of_add_property(node, new_pp)) {
450 kfree(new_pp->value);
459 of_node_put(node); /* Frees the node and properties */
463 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
465 if (ce->action == OF_RECONFIG_ATTACH_NODE &&
466 of_node_check_flag(ce->np, OF_OVERLAY)) {
467 if (kref_read(&ce->np->kobj.kref) > 1) {
468 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",
469 kref_read(&ce->np->kobj.kref), ce->np);
471 of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET);
481 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
483 switch (ce->action) {
484 case OF_RECONFIG_ADD_PROPERTY:
485 case OF_RECONFIG_REMOVE_PROPERTY:
486 case OF_RECONFIG_UPDATE_PROPERTY:
487 pr_debug("cset<%p> %-15s %pOF/%s\n", ce, action_names[ce->action],
488 ce->np, ce->prop->name);
490 case OF_RECONFIG_ATTACH_NODE:
491 case OF_RECONFIG_DETACH_NODE:
492 pr_debug("cset<%p> %-15s %pOF\n", ce, action_names[ce->action],
498 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
504 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
505 struct of_changeset_entry *rce)
507 memcpy(rce, ce, sizeof(*rce));
509 switch (ce->action) {
510 case OF_RECONFIG_ATTACH_NODE:
511 rce->action = OF_RECONFIG_DETACH_NODE;
513 case OF_RECONFIG_DETACH_NODE:
514 rce->action = OF_RECONFIG_ATTACH_NODE;
516 case OF_RECONFIG_ADD_PROPERTY:
517 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
519 case OF_RECONFIG_REMOVE_PROPERTY:
520 rce->action = OF_RECONFIG_ADD_PROPERTY;
522 case OF_RECONFIG_UPDATE_PROPERTY:
523 rce->old_prop = ce->prop;
524 rce->prop = ce->old_prop;
525 /* update was used but original property did not exist */
527 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
528 rce->prop = ce->prop;
534 static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
537 struct of_reconfig_data rd;
538 struct of_changeset_entry ce_inverted;
542 __of_changeset_entry_invert(ce, &ce_inverted);
546 switch (ce->action) {
547 case OF_RECONFIG_ATTACH_NODE:
548 case OF_RECONFIG_DETACH_NODE:
549 memset(&rd, 0, sizeof(rd));
551 ret = of_reconfig_notify(ce->action, &rd);
553 case OF_RECONFIG_ADD_PROPERTY:
554 case OF_RECONFIG_REMOVE_PROPERTY:
555 case OF_RECONFIG_UPDATE_PROPERTY:
556 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
559 pr_err("invalid devicetree changeset action: %i\n",
565 pr_err("changeset notifier error @%pOF\n", ce->np);
569 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
571 struct property *old_prop, **propp;
575 __of_changeset_entry_dump(ce);
577 raw_spin_lock_irqsave(&devtree_lock, flags);
578 switch (ce->action) {
579 case OF_RECONFIG_ATTACH_NODE:
580 __of_attach_node(ce->np);
582 case OF_RECONFIG_DETACH_NODE:
583 __of_detach_node(ce->np);
585 case OF_RECONFIG_ADD_PROPERTY:
586 /* If the property is in deadprops then it must be removed */
587 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
588 if (*propp == ce->prop) {
589 *propp = ce->prop->next;
590 ce->prop->next = NULL;
595 ret = __of_add_property(ce->np, ce->prop);
597 pr_err("changeset: add_property failed @%pOF/%s\n",
603 case OF_RECONFIG_REMOVE_PROPERTY:
604 ret = __of_remove_property(ce->np, ce->prop);
606 pr_err("changeset: remove_property failed @%pOF/%s\n",
613 case OF_RECONFIG_UPDATE_PROPERTY:
614 /* If the property is in deadprops then it must be removed */
615 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
616 if (*propp == ce->prop) {
617 *propp = ce->prop->next;
618 ce->prop->next = NULL;
623 ret = __of_update_property(ce->np, ce->prop, &old_prop);
625 pr_err("changeset: update_property failed @%pOF/%s\n",
634 raw_spin_unlock_irqrestore(&devtree_lock, flags);
639 switch (ce->action) {
640 case OF_RECONFIG_ATTACH_NODE:
641 __of_attach_node_sysfs(ce->np);
643 case OF_RECONFIG_DETACH_NODE:
644 __of_detach_node_sysfs(ce->np);
646 case OF_RECONFIG_ADD_PROPERTY:
647 /* ignore duplicate names */
648 __of_add_property_sysfs(ce->np, ce->prop);
650 case OF_RECONFIG_REMOVE_PROPERTY:
651 __of_remove_property_sysfs(ce->np, ce->prop);
653 case OF_RECONFIG_UPDATE_PROPERTY:
654 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
661 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
663 struct of_changeset_entry ce_inverted;
665 __of_changeset_entry_invert(ce, &ce_inverted);
666 return __of_changeset_entry_apply(&ce_inverted);
670 * of_changeset_init - Initialize a changeset for use
672 * @ocs: changeset pointer
674 * Initialize a changeset structure
676 void of_changeset_init(struct of_changeset *ocs)
678 memset(ocs, 0, sizeof(*ocs));
679 INIT_LIST_HEAD(&ocs->entries);
681 EXPORT_SYMBOL_GPL(of_changeset_init);
684 * of_changeset_destroy - Destroy a changeset
686 * @ocs: changeset pointer
688 * Destroys a changeset. Note that if a changeset is applied,
689 * its changes to the tree cannot be reverted.
691 void of_changeset_destroy(struct of_changeset *ocs)
693 struct of_changeset_entry *ce, *cen;
695 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
696 __of_changeset_entry_destroy(ce);
698 EXPORT_SYMBOL_GPL(of_changeset_destroy);
701 * Apply the changeset entries in @ocs.
702 * If apply fails, an attempt is made to revert the entries that were
703 * successfully applied.
705 * If multiple revert errors occur then only the final revert error is reported.
707 * Returns 0 on success, a negative error value in case of an error.
708 * If a revert error occurs, it is returned in *ret_revert.
710 int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
712 struct of_changeset_entry *ce;
715 pr_debug("changeset: applying...\n");
716 list_for_each_entry(ce, &ocs->entries, node) {
717 ret = __of_changeset_entry_apply(ce);
719 pr_err("Error applying changeset (%d)\n", ret);
720 list_for_each_entry_continue_reverse(ce, &ocs->entries,
722 ret_tmp = __of_changeset_entry_revert(ce);
724 *ret_revert = ret_tmp;
734 * Returns 0 on success, a negative error value in case of an error.
736 * If multiple changeset entry notification errors occur then only the
737 * final notification error is reported.
739 int __of_changeset_apply_notify(struct of_changeset *ocs)
741 struct of_changeset_entry *ce;
742 int ret = 0, ret_tmp;
744 pr_debug("changeset: emitting notifiers.\n");
746 /* drop the global lock while emitting notifiers */
747 mutex_unlock(&of_mutex);
748 list_for_each_entry(ce, &ocs->entries, node) {
749 ret_tmp = __of_changeset_entry_notify(ce, 0);
753 mutex_lock(&of_mutex);
754 pr_debug("changeset: notifiers sent.\n");
760 * Returns 0 on success, a negative error value in case of an error.
762 * If a changeset entry apply fails, an attempt is made to revert any
763 * previous entries in the changeset. If any of the reverts fails,
764 * that failure is not reported. Thus the state of the device tree
765 * is unknown if an apply error occurs.
767 static int __of_changeset_apply(struct of_changeset *ocs)
769 int ret, ret_revert = 0;
771 ret = __of_changeset_apply_entries(ocs, &ret_revert);
773 ret = __of_changeset_apply_notify(ocs);
779 * of_changeset_apply - Applies a changeset
781 * @ocs: changeset pointer
783 * Applies a changeset to the live tree.
784 * Any side-effects of live tree state changes are applied here on
785 * success, like creation/destruction of devices and side-effects
786 * like creation of sysfs properties and directories.
787 * Returns 0 on success, a negative error value in case of an error.
788 * On error the partially applied effects are reverted.
790 int of_changeset_apply(struct of_changeset *ocs)
794 mutex_lock(&of_mutex);
795 ret = __of_changeset_apply(ocs);
796 mutex_unlock(&of_mutex);
800 EXPORT_SYMBOL_GPL(of_changeset_apply);
803 * Revert the changeset entries in @ocs.
804 * If revert fails, an attempt is made to re-apply the entries that were
805 * successfully removed.
807 * If multiple re-apply errors occur then only the final apply error is
810 * Returns 0 on success, a negative error value in case of an error.
811 * If an apply error occurs, it is returned in *ret_apply.
813 int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
815 struct of_changeset_entry *ce;
818 pr_debug("changeset: reverting...\n");
819 list_for_each_entry_reverse(ce, &ocs->entries, node) {
820 ret = __of_changeset_entry_revert(ce);
822 pr_err("Error reverting changeset (%d)\n", ret);
823 list_for_each_entry_continue(ce, &ocs->entries, node) {
824 ret_tmp = __of_changeset_entry_apply(ce);
826 *ret_apply = ret_tmp;
836 * If multiple changeset entry notification errors occur then only the
837 * final notification error is reported.
839 int __of_changeset_revert_notify(struct of_changeset *ocs)
841 struct of_changeset_entry *ce;
842 int ret = 0, ret_tmp;
844 pr_debug("changeset: emitting notifiers.\n");
846 /* drop the global lock while emitting notifiers */
847 mutex_unlock(&of_mutex);
848 list_for_each_entry_reverse(ce, &ocs->entries, node) {
849 ret_tmp = __of_changeset_entry_notify(ce, 1);
853 mutex_lock(&of_mutex);
854 pr_debug("changeset: notifiers sent.\n");
859 static int __of_changeset_revert(struct of_changeset *ocs)
864 ret = __of_changeset_revert_entries(ocs, &ret_reply);
867 ret = __of_changeset_revert_notify(ocs);
873 * of_changeset_revert - Reverts an applied changeset
875 * @ocs: changeset pointer
877 * Reverts a changeset returning the state of the tree to what it
878 * was before the application.
879 * Any side-effects like creation/destruction of devices and
880 * removal of sysfs properties and directories are applied.
881 * Returns 0 on success, a negative error value in case of an error.
883 int of_changeset_revert(struct of_changeset *ocs)
887 mutex_lock(&of_mutex);
888 ret = __of_changeset_revert(ocs);
889 mutex_unlock(&of_mutex);
893 EXPORT_SYMBOL_GPL(of_changeset_revert);
896 * of_changeset_action - Add an action to the tail of the changeset list
898 * @ocs: changeset pointer
899 * @action: action to perform
900 * @np: Pointer to device node
901 * @prop: Pointer to property
903 * On action being one of:
904 * + OF_RECONFIG_ATTACH_NODE
905 * + OF_RECONFIG_DETACH_NODE,
906 * + OF_RECONFIG_ADD_PROPERTY
907 * + OF_RECONFIG_REMOVE_PROPERTY,
908 * + OF_RECONFIG_UPDATE_PROPERTY
909 * Returns 0 on success, a negative error value in case of an error.
911 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
912 struct device_node *np, struct property *prop)
914 struct of_changeset_entry *ce;
916 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
920 /* get a reference to the node */
922 ce->np = of_node_get(np);
925 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
926 ce->old_prop = of_find_property(np, prop->name, NULL);
928 /* add it to the list */
929 list_add_tail(&ce->node, &ocs->entries);
932 EXPORT_SYMBOL_GPL(of_changeset_action);