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 BUG_ON(!of_node_is_initialized(np));
52 /* only remove properties if on sysfs */
53 if (of_node_is_attached(np)) {
54 for_each_property_of_node(np, pp)
55 sysfs_remove_bin_file(&np->kobj, &pp->attr);
56 kobject_del(&np->kobj);
59 /* finally remove the kobj_init ref */
63 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
65 int of_reconfig_notifier_register(struct notifier_block *nb)
67 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
69 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
71 int of_reconfig_notifier_unregister(struct notifier_block *nb)
73 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
75 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
78 const char *action_names[] = {
79 [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
80 [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
81 [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
82 [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
83 [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
87 int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
91 struct of_reconfig_data *pr = p;
94 case OF_RECONFIG_ATTACH_NODE:
95 case OF_RECONFIG_DETACH_NODE:
96 pr_debug("of/notify %-15s %s\n", action_names[action],
99 case OF_RECONFIG_ADD_PROPERTY:
100 case OF_RECONFIG_REMOVE_PROPERTY:
101 case OF_RECONFIG_UPDATE_PROPERTY:
102 pr_debug("of/notify %-15s %s:%s\n", action_names[action],
103 pr->dn->full_name, pr->prop->name);
108 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
109 return notifier_to_errno(rc);
113 * of_reconfig_get_state_change() - Returns new state of device
114 * @action - action of the of notifier
115 * @arg - argument of the of notifier
117 * Returns the new state of a device based on the notifier used.
118 * Returns 0 on device going from enabled to disabled, 1 on device
119 * going from disabled to enabled and -1 on no change.
121 int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
123 struct property *prop, *old_prop = NULL;
124 int is_status, status_state, old_status_state, prev_state, new_state;
126 /* figure out if a device should be created or destroyed */
128 case OF_RECONFIG_ATTACH_NODE:
129 case OF_RECONFIG_DETACH_NODE:
130 prop = of_find_property(pr->dn, "status", NULL);
132 case OF_RECONFIG_ADD_PROPERTY:
133 case OF_RECONFIG_REMOVE_PROPERTY:
136 case OF_RECONFIG_UPDATE_PROPERTY:
138 old_prop = pr->old_prop;
141 return OF_RECONFIG_NO_CHANGE;
146 old_status_state = -1;
150 if (prop && !strcmp(prop->name, "status")) {
152 status_state = !strcmp(prop->value, "okay") ||
153 !strcmp(prop->value, "ok");
155 old_status_state = !strcmp(old_prop->value, "okay") ||
156 !strcmp(old_prop->value, "ok");
160 case OF_RECONFIG_ATTACH_NODE:
162 /* -1 & 0 status either missing or okay */
163 new_state = status_state != 0;
165 case OF_RECONFIG_DETACH_NODE:
166 /* -1 & 0 status either missing or okay */
167 prev_state = status_state != 0;
170 case OF_RECONFIG_ADD_PROPERTY:
172 /* no status property -> enabled (legacy) */
174 new_state = status_state;
177 case OF_RECONFIG_REMOVE_PROPERTY:
179 prev_state = status_state;
180 /* no status property -> enabled (legacy) */
184 case OF_RECONFIG_UPDATE_PROPERTY:
186 prev_state = old_status_state != 0;
187 new_state = status_state != 0;
192 if (prev_state == new_state)
193 return OF_RECONFIG_NO_CHANGE;
195 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
197 EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
199 int of_property_notify(int action, struct device_node *np,
200 struct property *prop, struct property *oldprop)
202 struct of_reconfig_data pr;
204 /* only call notifiers if the node is attached */
205 if (!of_node_is_attached(np))
210 pr.old_prop = oldprop;
211 return of_reconfig_notify(action, &pr);
214 void __of_attach_node(struct device_node *np)
216 const __be32 *phandle;
219 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
220 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
222 phandle = __of_get_property(np, "phandle", &sz);
224 phandle = __of_get_property(np, "linux,phandle", &sz);
225 if (IS_ENABLED(PPC_PSERIES) && !phandle)
226 phandle = __of_get_property(np, "ibm,phandle", &sz);
227 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
230 np->sibling = np->parent->child;
231 np->allnext = np->parent->allnext;
232 np->parent->allnext = np;
233 np->parent->child = np;
234 of_node_clear_flag(np, OF_DETACHED);
238 * of_attach_node() - Plug a device node into the tree and global list.
240 int of_attach_node(struct device_node *np)
242 struct of_reconfig_data rd;
245 memset(&rd, 0, sizeof(rd));
248 mutex_lock(&of_mutex);
249 raw_spin_lock_irqsave(&devtree_lock, flags);
250 __of_attach_node(np);
251 raw_spin_unlock_irqrestore(&devtree_lock, flags);
253 __of_attach_node_sysfs(np);
254 mutex_unlock(&of_mutex);
256 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
261 void __of_detach_node(struct device_node *np)
263 struct device_node *parent;
265 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
269 if (WARN_ON(!parent))
272 if (of_allnodes == np)
273 of_allnodes = np->allnext;
275 struct device_node *prev;
276 for (prev = of_allnodes;
278 prev = prev->allnext)
280 prev->allnext = np->allnext;
283 if (parent->child == np)
284 parent->child = np->sibling;
286 struct device_node *prevsib;
287 for (prevsib = np->parent->child;
288 prevsib->sibling != np;
289 prevsib = prevsib->sibling)
291 prevsib->sibling = np->sibling;
294 of_node_set_flag(np, OF_DETACHED);
298 * of_detach_node() - "Unplug" a node from the device tree.
300 * The caller must hold a reference to the node. The memory associated with
301 * the node is not freed until its refcount goes to zero.
303 int of_detach_node(struct device_node *np)
305 struct of_reconfig_data rd;
309 memset(&rd, 0, sizeof(rd));
312 mutex_lock(&of_mutex);
313 raw_spin_lock_irqsave(&devtree_lock, flags);
314 __of_detach_node(np);
315 raw_spin_unlock_irqrestore(&devtree_lock, flags);
317 __of_detach_node_sysfs(np);
318 mutex_unlock(&of_mutex);
320 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
326 * of_node_release() - release a dynamically allocated node
327 * @kref: kref element of the node to be released
329 * In of_node_put() this function is passed to kref_put() as the destructor.
331 void of_node_release(struct kobject *kobj)
333 struct device_node *node = kobj_to_device_node(kobj);
334 struct property *prop = node->properties;
336 /* We should never be releasing nodes that haven't been detached. */
337 if (!of_node_check_flag(node, OF_DETACHED)) {
338 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
343 if (!of_node_check_flag(node, OF_DYNAMIC))
347 struct property *next = prop->next;
354 prop = node->deadprops;
355 node->deadprops = NULL;
358 kfree(node->full_name);
364 * __of_prop_dup - Copy a property dynamically.
365 * @prop: Property to copy
366 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
368 * Copy a property by dynamically allocating the memory of both the
369 * property stucture and the property name & contents. The property's
370 * flags have the OF_DYNAMIC bit set so that we can differentiate between
371 * dynamically allocated properties and not.
372 * Returns the newly allocated property or NULL on out of memory error.
374 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
376 struct property *new;
378 new = kzalloc(sizeof(*new), allocflags);
383 * NOTE: There is no check for zero length value.
384 * In case of a boolean property This will allocate a value
385 * of zero bytes. We do this to work around the use
386 * of of_get_property() calls on boolean values.
388 new->name = kstrdup(prop->name, allocflags);
389 new->value = kmemdup(prop->value, prop->length, allocflags);
390 new->length = prop->length;
391 if (!new->name || !new->value)
394 /* mark the property as dynamic */
395 of_property_set_flag(new, OF_DYNAMIC);
407 * __of_node_dup() - Duplicate or create an empty device node dynamically.
408 * @fmt: Format string (plus vargs) for new full name of the device node
410 * Create an device tree node, either by duplicating an empty node or by allocating
411 * an empty one suitable for further modification. The node data are
412 * dynamically allocated and all the node flags have the OF_DYNAMIC &
413 * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of
416 struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...)
419 struct device_node *node;
421 node = kzalloc(sizeof(*node), GFP_KERNEL);
424 va_start(vargs, fmt);
425 node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
427 if (!node->full_name) {
432 of_node_set_flag(node, OF_DYNAMIC);
433 of_node_set_flag(node, OF_DETACHED);
436 /* Iterate over and duplicate all properties */
438 struct property *pp, *new_pp;
439 for_each_property_of_node(np, pp) {
440 new_pp = __of_prop_dup(pp, GFP_KERNEL);
443 if (__of_add_property(node, new_pp)) {
445 kfree(new_pp->value);
454 of_node_put(node); /* Frees the node and properties */
458 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
466 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
468 switch (ce->action) {
469 case OF_RECONFIG_ADD_PROPERTY:
470 case OF_RECONFIG_REMOVE_PROPERTY:
471 case OF_RECONFIG_UPDATE_PROPERTY:
472 pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action],
473 ce->np->full_name, ce->prop->name);
475 case OF_RECONFIG_ATTACH_NODE:
476 case OF_RECONFIG_DETACH_NODE:
477 pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action],
483 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
489 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
490 struct of_changeset_entry *rce)
492 memcpy(rce, ce, sizeof(*rce));
494 switch (ce->action) {
495 case OF_RECONFIG_ATTACH_NODE:
496 rce->action = OF_RECONFIG_DETACH_NODE;
498 case OF_RECONFIG_DETACH_NODE:
499 rce->action = OF_RECONFIG_ATTACH_NODE;
501 case OF_RECONFIG_ADD_PROPERTY:
502 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
504 case OF_RECONFIG_REMOVE_PROPERTY:
505 rce->action = OF_RECONFIG_ADD_PROPERTY;
507 case OF_RECONFIG_UPDATE_PROPERTY:
508 rce->old_prop = ce->prop;
509 rce->prop = ce->old_prop;
514 static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
516 struct of_reconfig_data rd;
517 struct of_changeset_entry ce_inverted;
521 __of_changeset_entry_invert(ce, &ce_inverted);
525 switch (ce->action) {
526 case OF_RECONFIG_ATTACH_NODE:
527 case OF_RECONFIG_DETACH_NODE:
528 memset(&rd, 0, sizeof(rd));
530 ret = of_reconfig_notify(ce->action, &rd);
532 case OF_RECONFIG_ADD_PROPERTY:
533 case OF_RECONFIG_REMOVE_PROPERTY:
534 case OF_RECONFIG_UPDATE_PROPERTY:
535 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
538 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
544 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
547 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
549 struct property *old_prop, **propp;
553 __of_changeset_entry_dump(ce);
555 raw_spin_lock_irqsave(&devtree_lock, flags);
556 switch (ce->action) {
557 case OF_RECONFIG_ATTACH_NODE:
558 __of_attach_node(ce->np);
560 case OF_RECONFIG_DETACH_NODE:
561 __of_detach_node(ce->np);
563 case OF_RECONFIG_ADD_PROPERTY:
564 /* If the property is in deadprops then it must be removed */
565 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
566 if (*propp == ce->prop) {
567 *propp = ce->prop->next;
568 ce->prop->next = NULL;
573 ret = __of_add_property(ce->np, ce->prop);
575 pr_err("%s: add_property failed @%s/%s\n",
576 __func__, ce->np->full_name,
581 case OF_RECONFIG_REMOVE_PROPERTY:
582 ret = __of_remove_property(ce->np, ce->prop);
584 pr_err("%s: remove_property failed @%s/%s\n",
585 __func__, ce->np->full_name,
591 case OF_RECONFIG_UPDATE_PROPERTY:
592 /* If the property is in deadprops then it must be removed */
593 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
594 if (*propp == ce->prop) {
595 *propp = ce->prop->next;
596 ce->prop->next = NULL;
601 ret = __of_update_property(ce->np, ce->prop, &old_prop);
603 pr_err("%s: update_property failed @%s/%s\n",
604 __func__, ce->np->full_name,
612 raw_spin_unlock_irqrestore(&devtree_lock, flags);
617 switch (ce->action) {
618 case OF_RECONFIG_ATTACH_NODE:
619 __of_attach_node_sysfs(ce->np);
621 case OF_RECONFIG_DETACH_NODE:
622 __of_detach_node_sysfs(ce->np);
624 case OF_RECONFIG_ADD_PROPERTY:
625 /* ignore duplicate names */
626 __of_add_property_sysfs(ce->np, ce->prop);
628 case OF_RECONFIG_REMOVE_PROPERTY:
629 __of_remove_property_sysfs(ce->np, ce->prop);
631 case OF_RECONFIG_UPDATE_PROPERTY:
632 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
639 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
641 struct of_changeset_entry ce_inverted;
643 __of_changeset_entry_invert(ce, &ce_inverted);
644 return __of_changeset_entry_apply(&ce_inverted);
648 * of_changeset_init - Initialize a changeset for use
650 * @ocs: changeset pointer
652 * Initialize a changeset structure
654 void of_changeset_init(struct of_changeset *ocs)
656 memset(ocs, 0, sizeof(*ocs));
657 INIT_LIST_HEAD(&ocs->entries);
661 * of_changeset_destroy - Destroy a changeset
663 * @ocs: changeset pointer
665 * Destroys a changeset. Note that if a changeset is applied,
666 * its changes to the tree cannot be reverted.
668 void of_changeset_destroy(struct of_changeset *ocs)
670 struct of_changeset_entry *ce, *cen;
672 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
673 __of_changeset_entry_destroy(ce);
677 * of_changeset_apply - Applies a changeset
679 * @ocs: changeset pointer
681 * Applies a changeset to the live tree.
682 * Any side-effects of live tree state changes are applied here on
683 * sucess, like creation/destruction of devices and side-effects
684 * like creation of sysfs properties and directories.
685 * Returns 0 on success, a negative error value in case of an error.
686 * On error the partially applied effects are reverted.
688 int of_changeset_apply(struct of_changeset *ocs)
690 struct of_changeset_entry *ce;
693 /* perform the rest of the work */
694 pr_debug("of_changeset: applying...\n");
695 list_for_each_entry(ce, &ocs->entries, node) {
696 ret = __of_changeset_entry_apply(ce);
698 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
699 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
700 __of_changeset_entry_revert(ce);
704 pr_debug("of_changeset: applied, emitting notifiers.\n");
706 /* drop the global lock while emitting notifiers */
707 mutex_unlock(&of_mutex);
708 list_for_each_entry(ce, &ocs->entries, node)
709 __of_changeset_entry_notify(ce, 0);
710 mutex_lock(&of_mutex);
711 pr_debug("of_changeset: notifiers sent.\n");
717 * of_changeset_revert - Reverts an applied changeset
719 * @ocs: changeset pointer
721 * Reverts a changeset returning the state of the tree to what it
722 * was before the application.
723 * Any side-effects like creation/destruction of devices and
724 * removal of sysfs properties and directories are applied.
725 * Returns 0 on success, a negative error value in case of an error.
727 int of_changeset_revert(struct of_changeset *ocs)
729 struct of_changeset_entry *ce;
732 pr_debug("of_changeset: reverting...\n");
733 list_for_each_entry_reverse(ce, &ocs->entries, node) {
734 ret = __of_changeset_entry_revert(ce);
736 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
737 list_for_each_entry_continue(ce, &ocs->entries, node)
738 __of_changeset_entry_apply(ce);
742 pr_debug("of_changeset: reverted, emitting notifiers.\n");
744 /* drop the global lock while emitting notifiers */
745 mutex_unlock(&of_mutex);
746 list_for_each_entry_reverse(ce, &ocs->entries, node)
747 __of_changeset_entry_notify(ce, 1);
748 mutex_lock(&of_mutex);
749 pr_debug("of_changeset: notifiers sent.\n");
755 * of_changeset_action - Perform a changeset action
757 * @ocs: changeset pointer
758 * @action: action to perform
759 * @np: Pointer to device node
760 * @prop: Pointer to property
762 * On action being one of:
763 * + OF_RECONFIG_ATTACH_NODE
764 * + OF_RECONFIG_DETACH_NODE,
765 * + OF_RECONFIG_ADD_PROPERTY
766 * + OF_RECONFIG_REMOVE_PROPERTY,
767 * + OF_RECONFIG_UPDATE_PROPERTY
768 * Returns 0 on success, a negative error value in case of an error.
770 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
771 struct device_node *np, struct property *prop)
773 struct of_changeset_entry *ce;
775 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
777 pr_err("%s: Failed to allocate\n", __func__);
780 /* get a reference to the node */
782 ce->np = of_node_get(np);
785 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
786 ce->old_prop = of_find_property(np, prop->name, NULL);
788 /* add it to the list */
789 list_add_tail(&ce->node, &ocs->entries);