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_alloc() - Create an empty device node dynamically.
408 * @full_name: Full name of the new device node
409 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
411 * Create an empty device tree node, suitable for further modification.
412 * The node data are dynamically allocated and all the node flags
413 * have the OF_DYNAMIC & OF_DETACHED bits set.
414 * Returns the newly allocated node or NULL on out of memory error.
416 struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
418 struct device_node *node;
420 node = kzalloc(sizeof(*node), allocflags);
424 node->full_name = kstrdup(full_name, allocflags);
425 of_node_set_flag(node, OF_DYNAMIC);
426 of_node_set_flag(node, OF_DETACHED);
427 if (!node->full_name)
435 kfree(node->full_name);
440 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
448 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
450 switch (ce->action) {
451 case OF_RECONFIG_ADD_PROPERTY:
452 case OF_RECONFIG_REMOVE_PROPERTY:
453 case OF_RECONFIG_UPDATE_PROPERTY:
454 pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action],
455 ce->np->full_name, ce->prop->name);
457 case OF_RECONFIG_ATTACH_NODE:
458 case OF_RECONFIG_DETACH_NODE:
459 pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action],
465 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
471 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
472 struct of_changeset_entry *rce)
474 memcpy(rce, ce, sizeof(*rce));
476 switch (ce->action) {
477 case OF_RECONFIG_ATTACH_NODE:
478 rce->action = OF_RECONFIG_DETACH_NODE;
480 case OF_RECONFIG_DETACH_NODE:
481 rce->action = OF_RECONFIG_ATTACH_NODE;
483 case OF_RECONFIG_ADD_PROPERTY:
484 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
486 case OF_RECONFIG_REMOVE_PROPERTY:
487 rce->action = OF_RECONFIG_ADD_PROPERTY;
489 case OF_RECONFIG_UPDATE_PROPERTY:
490 rce->old_prop = ce->prop;
491 rce->prop = ce->old_prop;
496 static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
498 struct of_reconfig_data rd;
499 struct of_changeset_entry ce_inverted;
503 __of_changeset_entry_invert(ce, &ce_inverted);
507 switch (ce->action) {
508 case OF_RECONFIG_ATTACH_NODE:
509 case OF_RECONFIG_DETACH_NODE:
510 memset(&rd, 0, sizeof(rd));
512 ret = of_reconfig_notify(ce->action, &rd);
514 case OF_RECONFIG_ADD_PROPERTY:
515 case OF_RECONFIG_REMOVE_PROPERTY:
516 case OF_RECONFIG_UPDATE_PROPERTY:
517 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
520 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
526 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
529 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
531 struct property *old_prop, **propp;
535 __of_changeset_entry_dump(ce);
537 raw_spin_lock_irqsave(&devtree_lock, flags);
538 switch (ce->action) {
539 case OF_RECONFIG_ATTACH_NODE:
540 __of_attach_node(ce->np);
542 case OF_RECONFIG_DETACH_NODE:
543 __of_detach_node(ce->np);
545 case OF_RECONFIG_ADD_PROPERTY:
546 /* If the property is in deadprops then it must be removed */
547 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
548 if (*propp == ce->prop) {
549 *propp = ce->prop->next;
550 ce->prop->next = NULL;
555 ret = __of_add_property(ce->np, ce->prop);
557 pr_err("%s: add_property failed @%s/%s\n",
558 __func__, ce->np->full_name,
563 case OF_RECONFIG_REMOVE_PROPERTY:
564 ret = __of_remove_property(ce->np, ce->prop);
566 pr_err("%s: remove_property failed @%s/%s\n",
567 __func__, ce->np->full_name,
573 case OF_RECONFIG_UPDATE_PROPERTY:
574 /* If the property is in deadprops then it must be removed */
575 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
576 if (*propp == ce->prop) {
577 *propp = ce->prop->next;
578 ce->prop->next = NULL;
583 ret = __of_update_property(ce->np, ce->prop, &old_prop);
585 pr_err("%s: update_property failed @%s/%s\n",
586 __func__, ce->np->full_name,
594 raw_spin_unlock_irqrestore(&devtree_lock, flags);
599 switch (ce->action) {
600 case OF_RECONFIG_ATTACH_NODE:
601 __of_attach_node_sysfs(ce->np);
603 case OF_RECONFIG_DETACH_NODE:
604 __of_detach_node_sysfs(ce->np);
606 case OF_RECONFIG_ADD_PROPERTY:
607 /* ignore duplicate names */
608 __of_add_property_sysfs(ce->np, ce->prop);
610 case OF_RECONFIG_REMOVE_PROPERTY:
611 __of_remove_property_sysfs(ce->np, ce->prop);
613 case OF_RECONFIG_UPDATE_PROPERTY:
614 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
621 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
623 struct of_changeset_entry ce_inverted;
625 __of_changeset_entry_invert(ce, &ce_inverted);
626 return __of_changeset_entry_apply(&ce_inverted);
630 * of_changeset_init - Initialize a changeset for use
632 * @ocs: changeset pointer
634 * Initialize a changeset structure
636 void of_changeset_init(struct of_changeset *ocs)
638 memset(ocs, 0, sizeof(*ocs));
639 INIT_LIST_HEAD(&ocs->entries);
643 * of_changeset_destroy - Destroy a changeset
645 * @ocs: changeset pointer
647 * Destroys a changeset. Note that if a changeset is applied,
648 * its changes to the tree cannot be reverted.
650 void of_changeset_destroy(struct of_changeset *ocs)
652 struct of_changeset_entry *ce, *cen;
654 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
655 __of_changeset_entry_destroy(ce);
659 * of_changeset_apply - Applies a changeset
661 * @ocs: changeset pointer
663 * Applies a changeset to the live tree.
664 * Any side-effects of live tree state changes are applied here on
665 * sucess, like creation/destruction of devices and side-effects
666 * like creation of sysfs properties and directories.
667 * Returns 0 on success, a negative error value in case of an error.
668 * On error the partially applied effects are reverted.
670 int of_changeset_apply(struct of_changeset *ocs)
672 struct of_changeset_entry *ce;
675 /* perform the rest of the work */
676 pr_debug("of_changeset: applying...\n");
677 list_for_each_entry(ce, &ocs->entries, node) {
678 ret = __of_changeset_entry_apply(ce);
680 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
681 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
682 __of_changeset_entry_revert(ce);
686 pr_debug("of_changeset: applied, 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 __of_changeset_entry_notify(ce, 0);
692 mutex_lock(&of_mutex);
693 pr_debug("of_changeset: notifiers sent.\n");
699 * of_changeset_revert - Reverts an applied changeset
701 * @ocs: changeset pointer
703 * Reverts a changeset returning the state of the tree to what it
704 * was before the application.
705 * Any side-effects like creation/destruction of devices and
706 * removal of sysfs properties and directories are applied.
707 * Returns 0 on success, a negative error value in case of an error.
709 int of_changeset_revert(struct of_changeset *ocs)
711 struct of_changeset_entry *ce;
714 pr_debug("of_changeset: reverting...\n");
715 list_for_each_entry_reverse(ce, &ocs->entries, node) {
716 ret = __of_changeset_entry_revert(ce);
718 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
719 list_for_each_entry_continue(ce, &ocs->entries, node)
720 __of_changeset_entry_apply(ce);
724 pr_debug("of_changeset: reverted, emitting notifiers.\n");
726 /* drop the global lock while emitting notifiers */
727 mutex_unlock(&of_mutex);
728 list_for_each_entry_reverse(ce, &ocs->entries, node)
729 __of_changeset_entry_notify(ce, 1);
730 mutex_lock(&of_mutex);
731 pr_debug("of_changeset: notifiers sent.\n");
737 * of_changeset_action - Perform a changeset action
739 * @ocs: changeset pointer
740 * @action: action to perform
741 * @np: Pointer to device node
742 * @prop: Pointer to property
744 * On action being one of:
745 * + OF_RECONFIG_ATTACH_NODE
746 * + OF_RECONFIG_DETACH_NODE,
747 * + OF_RECONFIG_ADD_PROPERTY
748 * + OF_RECONFIG_REMOVE_PROPERTY,
749 * + OF_RECONFIG_UPDATE_PROPERTY
750 * Returns 0 on success, a negative error value in case of an error.
752 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
753 struct device_node *np, struct property *prop)
755 struct of_changeset_entry *ce;
757 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
759 pr_err("%s: Failed to allocate\n", __func__);
762 /* get a reference to the node */
764 ce->np = of_node_get(np);
767 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
768 ce->old_prop = of_find_property(np, prop->name, NULL);
770 /* add it to the list */
771 list_add_tail(&ce->node, &ocs->entries);