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);
77 int of_reconfig_notify(unsigned long action, void *p)
81 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
82 return notifier_to_errno(rc);
86 * of_reconfig_get_state_change() - Returns new state of device
87 * @action - action of the of notifier
88 * @arg - argument of the of notifier
90 * Returns the new state of a device based on the notifier used.
91 * Returns 0 on device going from enabled to disabled, 1 on device
92 * going from disabled to enabled and -1 on no change.
94 int of_reconfig_get_state_change(unsigned long action, void *arg)
96 struct device_node *dn;
97 struct property *prop, *old_prop;
98 struct of_prop_reconfig *pr;
99 int is_status, status_state, old_status_state, prev_state, new_state;
101 /* figure out if a device should be created or destroyed */
103 prop = old_prop = NULL;
105 case OF_RECONFIG_ATTACH_NODE:
106 case OF_RECONFIG_DETACH_NODE:
108 prop = of_find_property(dn, "status", NULL);
110 case OF_RECONFIG_ADD_PROPERTY:
111 case OF_RECONFIG_REMOVE_PROPERTY:
116 case OF_RECONFIG_UPDATE_PROPERTY:
120 old_prop = pr->old_prop;
123 return OF_RECONFIG_NO_CHANGE;
128 old_status_state = -1;
132 if (prop && !strcmp(prop->name, "status")) {
134 status_state = !strcmp(prop->value, "okay") ||
135 !strcmp(prop->value, "ok");
137 old_status_state = !strcmp(old_prop->value, "okay") ||
138 !strcmp(old_prop->value, "ok");
142 case OF_RECONFIG_ATTACH_NODE:
144 /* -1 & 0 status either missing or okay */
145 new_state = status_state != 0;
147 case OF_RECONFIG_DETACH_NODE:
148 /* -1 & 0 status either missing or okay */
149 prev_state = status_state != 0;
152 case OF_RECONFIG_ADD_PROPERTY:
154 /* no status property -> enabled (legacy) */
156 new_state = status_state;
159 case OF_RECONFIG_REMOVE_PROPERTY:
161 prev_state = status_state;
162 /* no status property -> enabled (legacy) */
166 case OF_RECONFIG_UPDATE_PROPERTY:
168 prev_state = old_status_state != 0;
169 new_state = status_state != 0;
174 if (prev_state == new_state)
175 return OF_RECONFIG_NO_CHANGE;
177 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
179 EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
181 int of_property_notify(int action, struct device_node *np,
182 struct property *prop, struct property *oldprop)
184 struct of_prop_reconfig pr;
186 /* only call notifiers if the node is attached */
187 if (!of_node_is_attached(np))
192 pr.old_prop = oldprop;
193 return of_reconfig_notify(action, &pr);
196 void __of_attach_node(struct device_node *np)
198 const __be32 *phandle;
201 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
202 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
204 phandle = __of_get_property(np, "phandle", &sz);
206 phandle = __of_get_property(np, "linux,phandle", &sz);
207 if (IS_ENABLED(PPC_PSERIES) && !phandle)
208 phandle = __of_get_property(np, "ibm,phandle", &sz);
209 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
212 np->sibling = np->parent->child;
213 np->allnext = np->parent->allnext;
214 np->parent->allnext = np;
215 np->parent->child = np;
216 of_node_clear_flag(np, OF_DETACHED);
220 * of_attach_node() - Plug a device node into the tree and global list.
222 int of_attach_node(struct device_node *np)
226 mutex_lock(&of_mutex);
227 raw_spin_lock_irqsave(&devtree_lock, flags);
228 __of_attach_node(np);
229 raw_spin_unlock_irqrestore(&devtree_lock, flags);
231 __of_attach_node_sysfs(np);
232 mutex_unlock(&of_mutex);
234 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
239 void __of_detach_node(struct device_node *np)
241 struct device_node *parent;
243 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
247 if (WARN_ON(!parent))
250 if (of_allnodes == np)
251 of_allnodes = np->allnext;
253 struct device_node *prev;
254 for (prev = of_allnodes;
256 prev = prev->allnext)
258 prev->allnext = np->allnext;
261 if (parent->child == np)
262 parent->child = np->sibling;
264 struct device_node *prevsib;
265 for (prevsib = np->parent->child;
266 prevsib->sibling != np;
267 prevsib = prevsib->sibling)
269 prevsib->sibling = np->sibling;
272 of_node_set_flag(np, OF_DETACHED);
276 * of_detach_node() - "Unplug" a node from the device tree.
278 * The caller must hold a reference to the node. The memory associated with
279 * the node is not freed until its refcount goes to zero.
281 int of_detach_node(struct device_node *np)
286 mutex_lock(&of_mutex);
287 raw_spin_lock_irqsave(&devtree_lock, flags);
288 __of_detach_node(np);
289 raw_spin_unlock_irqrestore(&devtree_lock, flags);
291 __of_detach_node_sysfs(np);
292 mutex_unlock(&of_mutex);
294 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
300 * of_node_release() - release a dynamically allocated node
301 * @kref: kref element of the node to be released
303 * In of_node_put() this function is passed to kref_put() as the destructor.
305 void of_node_release(struct kobject *kobj)
307 struct device_node *node = kobj_to_device_node(kobj);
308 struct property *prop = node->properties;
310 /* We should never be releasing nodes that haven't been detached. */
311 if (!of_node_check_flag(node, OF_DETACHED)) {
312 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
317 if (!of_node_check_flag(node, OF_DYNAMIC))
321 struct property *next = prop->next;
328 prop = node->deadprops;
329 node->deadprops = NULL;
332 kfree(node->full_name);
338 * __of_prop_dup - Copy a property dynamically.
339 * @prop: Property to copy
340 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
342 * Copy a property by dynamically allocating the memory of both the
343 * property stucture and the property name & contents. The property's
344 * flags have the OF_DYNAMIC bit set so that we can differentiate between
345 * dynamically allocated properties and not.
346 * Returns the newly allocated property or NULL on out of memory error.
348 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
350 struct property *new;
352 new = kzalloc(sizeof(*new), allocflags);
357 * NOTE: There is no check for zero length value.
358 * In case of a boolean property This will allocate a value
359 * of zero bytes. We do this to work around the use
360 * of of_get_property() calls on boolean values.
362 new->name = kstrdup(prop->name, allocflags);
363 new->value = kmemdup(prop->value, prop->length, allocflags);
364 new->length = prop->length;
365 if (!new->name || !new->value)
368 /* mark the property as dynamic */
369 of_property_set_flag(new, OF_DYNAMIC);
381 * __of_node_alloc() - Create an empty device node dynamically.
382 * @full_name: Full name of the new device node
383 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
385 * Create an empty device tree node, suitable for further modification.
386 * The node data are dynamically allocated and all the node flags
387 * have the OF_DYNAMIC & OF_DETACHED bits set.
388 * Returns the newly allocated node or NULL on out of memory error.
390 struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
392 struct device_node *node;
394 node = kzalloc(sizeof(*node), allocflags);
398 node->full_name = kstrdup(full_name, allocflags);
399 of_node_set_flag(node, OF_DYNAMIC);
400 of_node_set_flag(node, OF_DETACHED);
401 if (!node->full_name)
409 kfree(node->full_name);
414 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
422 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
424 switch (ce->action) {
425 case OF_RECONFIG_ADD_PROPERTY:
426 pr_debug("%p: %s %s/%s\n",
427 ce, "ADD_PROPERTY ", ce->np->full_name,
430 case OF_RECONFIG_REMOVE_PROPERTY:
431 pr_debug("%p: %s %s/%s\n",
432 ce, "REMOVE_PROPERTY", ce->np->full_name,
435 case OF_RECONFIG_UPDATE_PROPERTY:
436 pr_debug("%p: %s %s/%s\n",
437 ce, "UPDATE_PROPERTY", ce->np->full_name,
440 case OF_RECONFIG_ATTACH_NODE:
441 pr_debug("%p: %s %s\n",
442 ce, "ATTACH_NODE ", ce->np->full_name);
444 case OF_RECONFIG_DETACH_NODE:
445 pr_debug("%p: %s %s\n",
446 ce, "DETACH_NODE ", ce->np->full_name);
451 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
457 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
458 struct of_changeset_entry *rce)
460 memcpy(rce, ce, sizeof(*rce));
462 switch (ce->action) {
463 case OF_RECONFIG_ATTACH_NODE:
464 rce->action = OF_RECONFIG_DETACH_NODE;
466 case OF_RECONFIG_DETACH_NODE:
467 rce->action = OF_RECONFIG_ATTACH_NODE;
469 case OF_RECONFIG_ADD_PROPERTY:
470 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
472 case OF_RECONFIG_REMOVE_PROPERTY:
473 rce->action = OF_RECONFIG_ADD_PROPERTY;
475 case OF_RECONFIG_UPDATE_PROPERTY:
476 rce->old_prop = ce->prop;
477 rce->prop = ce->old_prop;
482 static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
484 struct of_changeset_entry ce_inverted;
488 __of_changeset_entry_invert(ce, &ce_inverted);
492 switch (ce->action) {
493 case OF_RECONFIG_ATTACH_NODE:
494 case OF_RECONFIG_DETACH_NODE:
495 ret = of_reconfig_notify(ce->action, ce->np);
497 case OF_RECONFIG_ADD_PROPERTY:
498 case OF_RECONFIG_REMOVE_PROPERTY:
499 case OF_RECONFIG_UPDATE_PROPERTY:
500 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
503 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
509 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
512 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
514 struct property *old_prop, **propp;
518 __of_changeset_entry_dump(ce);
520 raw_spin_lock_irqsave(&devtree_lock, flags);
521 switch (ce->action) {
522 case OF_RECONFIG_ATTACH_NODE:
523 __of_attach_node(ce->np);
525 case OF_RECONFIG_DETACH_NODE:
526 __of_detach_node(ce->np);
528 case OF_RECONFIG_ADD_PROPERTY:
529 /* If the property is in deadprops then it must be removed */
530 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
531 if (*propp == ce->prop) {
532 *propp = ce->prop->next;
533 ce->prop->next = NULL;
538 ret = __of_add_property(ce->np, ce->prop);
540 pr_err("%s: add_property failed @%s/%s\n",
541 __func__, ce->np->full_name,
546 case OF_RECONFIG_REMOVE_PROPERTY:
547 ret = __of_remove_property(ce->np, ce->prop);
549 pr_err("%s: remove_property failed @%s/%s\n",
550 __func__, ce->np->full_name,
556 case OF_RECONFIG_UPDATE_PROPERTY:
557 /* If the property is in deadprops then it must be removed */
558 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
559 if (*propp == ce->prop) {
560 *propp = ce->prop->next;
561 ce->prop->next = NULL;
566 ret = __of_update_property(ce->np, ce->prop, &old_prop);
568 pr_err("%s: update_property failed @%s/%s\n",
569 __func__, ce->np->full_name,
577 raw_spin_unlock_irqrestore(&devtree_lock, flags);
582 switch (ce->action) {
583 case OF_RECONFIG_ATTACH_NODE:
584 __of_attach_node_sysfs(ce->np);
586 case OF_RECONFIG_DETACH_NODE:
587 __of_detach_node_sysfs(ce->np);
589 case OF_RECONFIG_ADD_PROPERTY:
590 /* ignore duplicate names */
591 __of_add_property_sysfs(ce->np, ce->prop);
593 case OF_RECONFIG_REMOVE_PROPERTY:
594 __of_remove_property_sysfs(ce->np, ce->prop);
596 case OF_RECONFIG_UPDATE_PROPERTY:
597 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
604 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
606 struct of_changeset_entry ce_inverted;
608 __of_changeset_entry_invert(ce, &ce_inverted);
609 return __of_changeset_entry_apply(&ce_inverted);
613 * of_changeset_init - Initialize a changeset for use
615 * @ocs: changeset pointer
617 * Initialize a changeset structure
619 void of_changeset_init(struct of_changeset *ocs)
621 memset(ocs, 0, sizeof(*ocs));
622 INIT_LIST_HEAD(&ocs->entries);
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);
642 * of_changeset_apply - Applies a changeset
644 * @ocs: changeset pointer
646 * Applies a changeset to the live tree.
647 * Any side-effects of live tree state changes are applied here on
648 * sucess, like creation/destruction of devices and side-effects
649 * like creation of sysfs properties and directories.
650 * Returns 0 on success, a negative error value in case of an error.
651 * On error the partially applied effects are reverted.
653 int of_changeset_apply(struct of_changeset *ocs)
655 struct of_changeset_entry *ce;
658 /* perform the rest of the work */
659 pr_debug("of_changeset: applying...\n");
660 list_for_each_entry(ce, &ocs->entries, node) {
661 ret = __of_changeset_entry_apply(ce);
663 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
664 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
665 __of_changeset_entry_revert(ce);
669 pr_debug("of_changeset: applied, emitting notifiers.\n");
671 /* drop the global lock while emitting notifiers */
672 mutex_unlock(&of_mutex);
673 list_for_each_entry(ce, &ocs->entries, node)
674 __of_changeset_entry_notify(ce, 0);
675 mutex_lock(&of_mutex);
676 pr_debug("of_changeset: notifiers sent.\n");
682 * of_changeset_revert - Reverts an applied changeset
684 * @ocs: changeset pointer
686 * Reverts a changeset returning the state of the tree to what it
687 * was before the application.
688 * Any side-effects like creation/destruction of devices and
689 * removal of sysfs properties and directories are applied.
690 * Returns 0 on success, a negative error value in case of an error.
692 int of_changeset_revert(struct of_changeset *ocs)
694 struct of_changeset_entry *ce;
697 pr_debug("of_changeset: reverting...\n");
698 list_for_each_entry_reverse(ce, &ocs->entries, node) {
699 ret = __of_changeset_entry_revert(ce);
701 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
702 list_for_each_entry_continue(ce, &ocs->entries, node)
703 __of_changeset_entry_apply(ce);
707 pr_debug("of_changeset: reverted, emitting notifiers.\n");
709 /* drop the global lock while emitting notifiers */
710 mutex_unlock(&of_mutex);
711 list_for_each_entry_reverse(ce, &ocs->entries, node)
712 __of_changeset_entry_notify(ce, 1);
713 mutex_lock(&of_mutex);
714 pr_debug("of_changeset: notifiers sent.\n");
720 * of_changeset_action - Perform a changeset action
722 * @ocs: changeset pointer
723 * @action: action to perform
724 * @np: Pointer to device node
725 * @prop: Pointer to property
727 * On action being one of:
728 * + OF_RECONFIG_ATTACH_NODE
729 * + OF_RECONFIG_DETACH_NODE,
730 * + OF_RECONFIG_ADD_PROPERTY
731 * + OF_RECONFIG_REMOVE_PROPERTY,
732 * + OF_RECONFIG_UPDATE_PROPERTY
733 * Returns 0 on success, a negative error value in case of an error.
735 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
736 struct device_node *np, struct property *prop)
738 struct of_changeset_entry *ce;
740 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
742 pr_err("%s: Failed to allocate\n", __func__);
745 /* get a reference to the node */
747 ce->np = of_node_get(np);
750 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
751 ce->old_prop = of_find_property(np, prop->name, NULL);
753 /* add it to the list */
754 list_add_tail(&ce->node, &ocs->entries);