2 * Support for dynamic device trees.
4 * On some platforms, the device tree can be manipulated at runtime.
5 * The routines in this section support adding, removing and changing
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
13 #include <linux/proc_fs.h>
15 #include "of_private.h"
18 * of_node_get() - Increment refcount of a node
19 * @node: Node to inc refcount, NULL is supported to simplify writing of
24 struct device_node *of_node_get(struct device_node *node)
27 kobject_get(&node->kobj);
30 EXPORT_SYMBOL(of_node_get);
33 * of_node_put() - Decrement refcount of a node
34 * @node: Node to dec refcount, NULL is supported to simplify writing of
37 void of_node_put(struct device_node *node)
40 kobject_put(&node->kobj);
42 EXPORT_SYMBOL(of_node_put);
44 void __of_detach_node_sysfs(struct device_node *np)
48 if (!IS_ENABLED(CONFIG_SYSFS))
51 BUG_ON(!of_node_is_initialized(np));
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);
62 /* finally remove the kobj_init ref */
66 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
68 int of_reconfig_notifier_register(struct notifier_block *nb)
70 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
72 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
74 int of_reconfig_notifier_unregister(struct notifier_block *nb)
76 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
78 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
81 const char *action_names[] = {
82 [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
83 [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
84 [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
85 [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
86 [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
90 int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
94 struct of_reconfig_data *pr = p;
97 case OF_RECONFIG_ATTACH_NODE:
98 case OF_RECONFIG_DETACH_NODE:
99 pr_debug("of/notify %-15s %s\n", action_names[action],
102 case OF_RECONFIG_ADD_PROPERTY:
103 case OF_RECONFIG_REMOVE_PROPERTY:
104 case OF_RECONFIG_UPDATE_PROPERTY:
105 pr_debug("of/notify %-15s %s:%s\n", action_names[action],
106 pr->dn->full_name, pr->prop->name);
111 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
112 return notifier_to_errno(rc);
116 * of_reconfig_get_state_change() - Returns new state of device
117 * @action - action of the of notifier
118 * @arg - argument of the of notifier
120 * Returns the new state of a device based on the notifier used.
121 * Returns 0 on device going from enabled to disabled, 1 on device
122 * going from disabled to enabled and -1 on no change.
124 int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
126 struct property *prop, *old_prop = NULL;
127 int is_status, status_state, old_status_state, prev_state, new_state;
129 /* figure out if a device should be created or destroyed */
131 case OF_RECONFIG_ATTACH_NODE:
132 case OF_RECONFIG_DETACH_NODE:
133 prop = of_find_property(pr->dn, "status", NULL);
135 case OF_RECONFIG_ADD_PROPERTY:
136 case OF_RECONFIG_REMOVE_PROPERTY:
139 case OF_RECONFIG_UPDATE_PROPERTY:
141 old_prop = pr->old_prop;
144 return OF_RECONFIG_NO_CHANGE;
149 old_status_state = -1;
153 if (prop && !strcmp(prop->name, "status")) {
155 status_state = !strcmp(prop->value, "okay") ||
156 !strcmp(prop->value, "ok");
158 old_status_state = !strcmp(old_prop->value, "okay") ||
159 !strcmp(old_prop->value, "ok");
163 case OF_RECONFIG_ATTACH_NODE:
165 /* -1 & 0 status either missing or okay */
166 new_state = status_state != 0;
168 case OF_RECONFIG_DETACH_NODE:
169 /* -1 & 0 status either missing or okay */
170 prev_state = status_state != 0;
173 case OF_RECONFIG_ADD_PROPERTY:
175 /* no status property -> enabled (legacy) */
177 new_state = status_state;
180 case OF_RECONFIG_REMOVE_PROPERTY:
182 prev_state = status_state;
183 /* no status property -> enabled (legacy) */
187 case OF_RECONFIG_UPDATE_PROPERTY:
189 prev_state = old_status_state != 0;
190 new_state = status_state != 0;
195 if (prev_state == new_state)
196 return OF_RECONFIG_NO_CHANGE;
198 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
200 EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
202 int of_property_notify(int action, struct device_node *np,
203 struct property *prop, struct property *oldprop)
205 struct of_reconfig_data pr;
207 /* only call notifiers if the node is attached */
208 if (!of_node_is_attached(np))
213 pr.old_prop = oldprop;
214 return of_reconfig_notify(action, &pr);
217 void __of_attach_node(struct device_node *np)
219 const __be32 *phandle;
222 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
223 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
225 phandle = __of_get_property(np, "phandle", &sz);
227 phandle = __of_get_property(np, "linux,phandle", &sz);
228 if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
229 phandle = __of_get_property(np, "ibm,phandle", &sz);
230 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
233 np->sibling = np->parent->child;
234 np->parent->child = np;
235 of_node_clear_flag(np, OF_DETACHED);
239 * of_attach_node() - Plug a device node into the tree and global list.
241 int of_attach_node(struct device_node *np)
243 struct of_reconfig_data rd;
246 memset(&rd, 0, sizeof(rd));
249 mutex_lock(&of_mutex);
250 raw_spin_lock_irqsave(&devtree_lock, flags);
251 __of_attach_node(np);
252 raw_spin_unlock_irqrestore(&devtree_lock, flags);
254 __of_attach_node_sysfs(np);
255 mutex_unlock(&of_mutex);
257 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
262 void __of_detach_node(struct device_node *np)
264 struct device_node *parent;
266 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
270 if (WARN_ON(!parent))
273 if (parent->child == np)
274 parent->child = np->sibling;
276 struct device_node *prevsib;
277 for (prevsib = np->parent->child;
278 prevsib->sibling != np;
279 prevsib = prevsib->sibling)
281 prevsib->sibling = np->sibling;
284 of_node_set_flag(np, OF_DETACHED);
288 * of_detach_node() - "Unplug" a node from the device tree.
290 * The caller must hold a reference to the node. The memory associated with
291 * the node is not freed until its refcount goes to zero.
293 int of_detach_node(struct device_node *np)
295 struct of_reconfig_data rd;
299 memset(&rd, 0, sizeof(rd));
302 mutex_lock(&of_mutex);
303 raw_spin_lock_irqsave(&devtree_lock, flags);
304 __of_detach_node(np);
305 raw_spin_unlock_irqrestore(&devtree_lock, flags);
307 __of_detach_node_sysfs(np);
308 mutex_unlock(&of_mutex);
310 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
316 * of_node_release() - release a dynamically allocated node
317 * @kref: kref element of the node to be released
319 * In of_node_put() this function is passed to kref_put() as the destructor.
321 void of_node_release(struct kobject *kobj)
323 struct device_node *node = kobj_to_device_node(kobj);
324 struct property *prop = node->properties;
326 /* We should never be releasing nodes that haven't been detached. */
327 if (!of_node_check_flag(node, OF_DETACHED)) {
328 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
333 if (!of_node_check_flag(node, OF_DYNAMIC))
337 struct property *next = prop->next;
344 prop = node->deadprops;
345 node->deadprops = NULL;
348 kfree(node->full_name);
354 * __of_prop_dup - Copy a property dynamically.
355 * @prop: Property to copy
356 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
358 * Copy a property by dynamically allocating the memory of both the
359 * property structure and the property name & contents. The property's
360 * flags have the OF_DYNAMIC bit set so that we can differentiate between
361 * dynamically allocated properties and not.
362 * Returns the newly allocated property or NULL on out of memory error.
364 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
366 struct property *new;
368 new = kzalloc(sizeof(*new), allocflags);
373 * NOTE: There is no check for zero length value.
374 * In case of a boolean property, this will allocate a value
375 * of zero bytes. We do this to work around the use
376 * of of_get_property() calls on boolean values.
378 new->name = kstrdup(prop->name, allocflags);
379 new->value = kmemdup(prop->value, prop->length, allocflags);
380 new->length = prop->length;
381 if (!new->name || !new->value)
384 /* mark the property as dynamic */
385 of_property_set_flag(new, OF_DYNAMIC);
397 * __of_node_dup() - Duplicate or create an empty device node dynamically.
398 * @fmt: Format string (plus vargs) for new full name of the device node
400 * Create an device tree node, either by duplicating an empty node or by allocating
401 * an empty one suitable for further modification. The node data are
402 * dynamically allocated and all the node flags have the OF_DYNAMIC &
403 * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of
406 struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...)
409 struct device_node *node;
411 node = kzalloc(sizeof(*node), GFP_KERNEL);
414 va_start(vargs, fmt);
415 node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
417 if (!node->full_name) {
422 of_node_set_flag(node, OF_DYNAMIC);
423 of_node_set_flag(node, OF_DETACHED);
426 /* Iterate over and duplicate all properties */
428 struct property *pp, *new_pp;
429 for_each_property_of_node(np, pp) {
430 new_pp = __of_prop_dup(pp, GFP_KERNEL);
433 if (__of_add_property(node, new_pp)) {
435 kfree(new_pp->value);
444 of_node_put(node); /* Frees the node and properties */
448 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
456 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
458 switch (ce->action) {
459 case OF_RECONFIG_ADD_PROPERTY:
460 case OF_RECONFIG_REMOVE_PROPERTY:
461 case OF_RECONFIG_UPDATE_PROPERTY:
462 pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action],
463 ce->np->full_name, ce->prop->name);
465 case OF_RECONFIG_ATTACH_NODE:
466 case OF_RECONFIG_DETACH_NODE:
467 pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action],
473 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
479 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
480 struct of_changeset_entry *rce)
482 memcpy(rce, ce, sizeof(*rce));
484 switch (ce->action) {
485 case OF_RECONFIG_ATTACH_NODE:
486 rce->action = OF_RECONFIG_DETACH_NODE;
488 case OF_RECONFIG_DETACH_NODE:
489 rce->action = OF_RECONFIG_ATTACH_NODE;
491 case OF_RECONFIG_ADD_PROPERTY:
492 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
494 case OF_RECONFIG_REMOVE_PROPERTY:
495 rce->action = OF_RECONFIG_ADD_PROPERTY;
497 case OF_RECONFIG_UPDATE_PROPERTY:
498 rce->old_prop = ce->prop;
499 rce->prop = ce->old_prop;
504 static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
506 struct of_reconfig_data rd;
507 struct of_changeset_entry ce_inverted;
511 __of_changeset_entry_invert(ce, &ce_inverted);
515 switch (ce->action) {
516 case OF_RECONFIG_ATTACH_NODE:
517 case OF_RECONFIG_DETACH_NODE:
518 memset(&rd, 0, sizeof(rd));
520 ret = of_reconfig_notify(ce->action, &rd);
522 case OF_RECONFIG_ADD_PROPERTY:
523 case OF_RECONFIG_REMOVE_PROPERTY:
524 case OF_RECONFIG_UPDATE_PROPERTY:
525 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
528 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
534 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
537 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
539 struct property *old_prop, **propp;
543 __of_changeset_entry_dump(ce);
545 raw_spin_lock_irqsave(&devtree_lock, flags);
546 switch (ce->action) {
547 case OF_RECONFIG_ATTACH_NODE:
548 __of_attach_node(ce->np);
550 case OF_RECONFIG_DETACH_NODE:
551 __of_detach_node(ce->np);
553 case OF_RECONFIG_ADD_PROPERTY:
554 /* If the property is in deadprops then it must be removed */
555 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
556 if (*propp == ce->prop) {
557 *propp = ce->prop->next;
558 ce->prop->next = NULL;
563 ret = __of_add_property(ce->np, ce->prop);
565 pr_err("%s: add_property failed @%s/%s\n",
566 __func__, ce->np->full_name,
571 case OF_RECONFIG_REMOVE_PROPERTY:
572 ret = __of_remove_property(ce->np, ce->prop);
574 pr_err("%s: remove_property failed @%s/%s\n",
575 __func__, ce->np->full_name,
581 case OF_RECONFIG_UPDATE_PROPERTY:
582 /* If the property is in deadprops then it must be removed */
583 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
584 if (*propp == ce->prop) {
585 *propp = ce->prop->next;
586 ce->prop->next = NULL;
591 ret = __of_update_property(ce->np, ce->prop, &old_prop);
593 pr_err("%s: update_property failed @%s/%s\n",
594 __func__, ce->np->full_name,
602 raw_spin_unlock_irqrestore(&devtree_lock, flags);
607 switch (ce->action) {
608 case OF_RECONFIG_ATTACH_NODE:
609 __of_attach_node_sysfs(ce->np);
611 case OF_RECONFIG_DETACH_NODE:
612 __of_detach_node_sysfs(ce->np);
614 case OF_RECONFIG_ADD_PROPERTY:
615 /* ignore duplicate names */
616 __of_add_property_sysfs(ce->np, ce->prop);
618 case OF_RECONFIG_REMOVE_PROPERTY:
619 __of_remove_property_sysfs(ce->np, ce->prop);
621 case OF_RECONFIG_UPDATE_PROPERTY:
622 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
629 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
631 struct of_changeset_entry ce_inverted;
633 __of_changeset_entry_invert(ce, &ce_inverted);
634 return __of_changeset_entry_apply(&ce_inverted);
638 * of_changeset_init - Initialize a changeset for use
640 * @ocs: changeset pointer
642 * Initialize a changeset structure
644 void of_changeset_init(struct of_changeset *ocs)
646 memset(ocs, 0, sizeof(*ocs));
647 INIT_LIST_HEAD(&ocs->entries);
649 EXPORT_SYMBOL_GPL(of_changeset_init);
652 * of_changeset_destroy - Destroy a changeset
654 * @ocs: changeset pointer
656 * Destroys a changeset. Note that if a changeset is applied,
657 * its changes to the tree cannot be reverted.
659 void of_changeset_destroy(struct of_changeset *ocs)
661 struct of_changeset_entry *ce, *cen;
663 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
664 __of_changeset_entry_destroy(ce);
666 EXPORT_SYMBOL_GPL(of_changeset_destroy);
668 int __of_changeset_apply(struct of_changeset *ocs)
670 struct of_changeset_entry *ce;
673 /* perform the rest of the work */
674 pr_debug("of_changeset: applying...\n");
675 list_for_each_entry(ce, &ocs->entries, node) {
676 ret = __of_changeset_entry_apply(ce);
678 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
679 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
680 __of_changeset_entry_revert(ce);
684 pr_debug("of_changeset: applied, emitting notifiers.\n");
686 /* drop the global lock while emitting notifiers */
687 mutex_unlock(&of_mutex);
688 list_for_each_entry(ce, &ocs->entries, node)
689 __of_changeset_entry_notify(ce, 0);
690 mutex_lock(&of_mutex);
691 pr_debug("of_changeset: notifiers sent.\n");
697 * of_changeset_apply - Applies a changeset
699 * @ocs: changeset pointer
701 * Applies a changeset to the live tree.
702 * Any side-effects of live tree state changes are applied here on
703 * success, like creation/destruction of devices and side-effects
704 * like creation of sysfs properties and directories.
705 * Returns 0 on success, a negative error value in case of an error.
706 * On error the partially applied effects are reverted.
708 int of_changeset_apply(struct of_changeset *ocs)
712 mutex_lock(&of_mutex);
713 ret = __of_changeset_apply(ocs);
714 mutex_unlock(&of_mutex);
718 EXPORT_SYMBOL_GPL(of_changeset_apply);
720 int __of_changeset_revert(struct of_changeset *ocs)
722 struct of_changeset_entry *ce;
725 pr_debug("of_changeset: reverting...\n");
726 list_for_each_entry_reverse(ce, &ocs->entries, node) {
727 ret = __of_changeset_entry_revert(ce);
729 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
730 list_for_each_entry_continue(ce, &ocs->entries, node)
731 __of_changeset_entry_apply(ce);
735 pr_debug("of_changeset: reverted, emitting notifiers.\n");
737 /* drop the global lock while emitting notifiers */
738 mutex_unlock(&of_mutex);
739 list_for_each_entry_reverse(ce, &ocs->entries, node)
740 __of_changeset_entry_notify(ce, 1);
741 mutex_lock(&of_mutex);
742 pr_debug("of_changeset: notifiers sent.\n");
748 * of_changeset_revert - Reverts an applied changeset
750 * @ocs: changeset pointer
752 * Reverts a changeset returning the state of the tree to what it
753 * was before the application.
754 * Any side-effects like creation/destruction of devices and
755 * removal of sysfs properties and directories are applied.
756 * Returns 0 on success, a negative error value in case of an error.
758 int of_changeset_revert(struct of_changeset *ocs)
762 mutex_lock(&of_mutex);
763 ret = __of_changeset_revert(ocs);
764 mutex_unlock(&of_mutex);
768 EXPORT_SYMBOL_GPL(of_changeset_revert);
771 * of_changeset_action - Perform a changeset action
773 * @ocs: changeset pointer
774 * @action: action to perform
775 * @np: Pointer to device node
776 * @prop: Pointer to property
778 * On action being one of:
779 * + OF_RECONFIG_ATTACH_NODE
780 * + OF_RECONFIG_DETACH_NODE,
781 * + OF_RECONFIG_ADD_PROPERTY
782 * + OF_RECONFIG_REMOVE_PROPERTY,
783 * + OF_RECONFIG_UPDATE_PROPERTY
784 * Returns 0 on success, a negative error value in case of an error.
786 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
787 struct device_node *np, struct property *prop)
789 struct of_changeset_entry *ce;
791 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
793 pr_err("%s: Failed to allocate\n", __func__);
796 /* get a reference to the node */
798 ce->np = of_node_get(np);
801 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
802 ce->old_prop = of_find_property(np, prop->name, NULL);
804 /* add it to the list */
805 list_add_tail(&ce->node, &ocs->entries);
808 EXPORT_SYMBOL_GPL(of_changeset_action);