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);
80 int of_reconfig_notify(unsigned long action, void *p)
84 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
85 return notifier_to_errno(rc);
88 int of_property_notify(int action, struct device_node *np,
89 struct property *prop, struct property *oldprop)
91 struct of_prop_reconfig pr;
93 /* only call notifiers if the node is attached */
94 if (!of_node_is_attached(np))
99 pr.old_prop = oldprop;
100 return of_reconfig_notify(action, &pr);
103 void __of_attach_node(struct device_node *np)
105 const __be32 *phandle;
108 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
109 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
111 phandle = __of_get_property(np, "phandle", &sz);
113 phandle = __of_get_property(np, "linux,phandle", &sz);
114 if (IS_ENABLED(PPC_PSERIES) && !phandle)
115 phandle = __of_get_property(np, "ibm,phandle", &sz);
116 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
119 np->sibling = np->parent->child;
120 np->parent->child = np;
121 of_node_clear_flag(np, OF_DETACHED);
125 * of_attach_node() - Plug a device node into the tree and global list.
127 int of_attach_node(struct device_node *np)
131 mutex_lock(&of_mutex);
132 raw_spin_lock_irqsave(&devtree_lock, flags);
133 __of_attach_node(np);
134 raw_spin_unlock_irqrestore(&devtree_lock, flags);
136 __of_attach_node_sysfs(np);
137 mutex_unlock(&of_mutex);
139 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
144 void __of_detach_node(struct device_node *np)
146 struct device_node *parent;
148 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
152 if (WARN_ON(!parent))
155 if (parent->child == np)
156 parent->child = np->sibling;
158 struct device_node *prevsib;
159 for (prevsib = np->parent->child;
160 prevsib->sibling != np;
161 prevsib = prevsib->sibling)
163 prevsib->sibling = np->sibling;
166 of_node_set_flag(np, OF_DETACHED);
170 * of_detach_node() - "Unplug" a node from the device tree.
172 * The caller must hold a reference to the node. The memory associated with
173 * the node is not freed until its refcount goes to zero.
175 int of_detach_node(struct device_node *np)
180 mutex_lock(&of_mutex);
181 raw_spin_lock_irqsave(&devtree_lock, flags);
182 __of_detach_node(np);
183 raw_spin_unlock_irqrestore(&devtree_lock, flags);
185 __of_detach_node_sysfs(np);
186 mutex_unlock(&of_mutex);
188 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
194 * of_node_release() - release a dynamically allocated node
195 * @kref: kref element of the node to be released
197 * In of_node_put() this function is passed to kref_put() as the destructor.
199 void of_node_release(struct kobject *kobj)
201 struct device_node *node = kobj_to_device_node(kobj);
202 struct property *prop = node->properties;
204 /* We should never be releasing nodes that haven't been detached. */
205 if (!of_node_check_flag(node, OF_DETACHED)) {
206 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
211 if (!of_node_check_flag(node, OF_DYNAMIC))
215 struct property *next = prop->next;
222 prop = node->deadprops;
223 node->deadprops = NULL;
226 kfree(node->full_name);
232 * __of_prop_dup - Copy a property dynamically.
233 * @prop: Property to copy
234 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
236 * Copy a property by dynamically allocating the memory of both the
237 * property structure and the property name & contents. The property's
238 * flags have the OF_DYNAMIC bit set so that we can differentiate between
239 * dynamically allocated properties and not.
240 * Returns the newly allocated property or NULL on out of memory error.
242 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
244 struct property *new;
246 new = kzalloc(sizeof(*new), allocflags);
251 * NOTE: There is no check for zero length value.
252 * In case of a boolean property, this will allocate a value
253 * of zero bytes. We do this to work around the use
254 * of of_get_property() calls on boolean values.
256 new->name = kstrdup(prop->name, allocflags);
257 new->value = kmemdup(prop->value, prop->length, allocflags);
258 new->length = prop->length;
259 if (!new->name || !new->value)
262 /* mark the property as dynamic */
263 of_property_set_flag(new, OF_DYNAMIC);
275 * __of_node_alloc() - Create an empty device node dynamically.
276 * @full_name: Full name of the new device node
278 * Create an empty device tree node, suitable for further modification.
279 * The node data are dynamically allocated and all the node flags
280 * have the OF_DYNAMIC & OF_DETACHED bits set.
281 * Returns the newly allocated node or NULL on out of memory error.
283 struct device_node *__of_node_alloc(const char *fmt, ...)
286 struct device_node *node;
288 node = kzalloc(sizeof(*node), GFP_KERNEL);
291 va_start(vargs, fmt);
292 node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
294 if (!node->full_name)
297 of_node_set_flag(node, OF_DYNAMIC);
298 of_node_set_flag(node, OF_DETACHED);
308 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
316 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
318 switch (ce->action) {
319 case OF_RECONFIG_ADD_PROPERTY:
320 pr_debug("%p: %s %s/%s\n",
321 ce, "ADD_PROPERTY ", ce->np->full_name,
324 case OF_RECONFIG_REMOVE_PROPERTY:
325 pr_debug("%p: %s %s/%s\n",
326 ce, "REMOVE_PROPERTY", ce->np->full_name,
329 case OF_RECONFIG_UPDATE_PROPERTY:
330 pr_debug("%p: %s %s/%s\n",
331 ce, "UPDATE_PROPERTY", ce->np->full_name,
334 case OF_RECONFIG_ATTACH_NODE:
335 pr_debug("%p: %s %s\n",
336 ce, "ATTACH_NODE ", ce->np->full_name);
338 case OF_RECONFIG_DETACH_NODE:
339 pr_debug("%p: %s %s\n",
340 ce, "DETACH_NODE ", ce->np->full_name);
345 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
351 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
352 struct of_changeset_entry *rce)
354 memcpy(rce, ce, sizeof(*rce));
356 switch (ce->action) {
357 case OF_RECONFIG_ATTACH_NODE:
358 rce->action = OF_RECONFIG_DETACH_NODE;
360 case OF_RECONFIG_DETACH_NODE:
361 rce->action = OF_RECONFIG_ATTACH_NODE;
363 case OF_RECONFIG_ADD_PROPERTY:
364 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
366 case OF_RECONFIG_REMOVE_PROPERTY:
367 rce->action = OF_RECONFIG_ADD_PROPERTY;
369 case OF_RECONFIG_UPDATE_PROPERTY:
370 rce->old_prop = ce->prop;
371 rce->prop = ce->old_prop;
376 static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
378 struct of_changeset_entry ce_inverted;
382 __of_changeset_entry_invert(ce, &ce_inverted);
386 switch (ce->action) {
387 case OF_RECONFIG_ATTACH_NODE:
388 case OF_RECONFIG_DETACH_NODE:
389 ret = of_reconfig_notify(ce->action, ce->np);
391 case OF_RECONFIG_ADD_PROPERTY:
392 case OF_RECONFIG_REMOVE_PROPERTY:
393 case OF_RECONFIG_UPDATE_PROPERTY:
394 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
397 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
403 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
406 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
408 struct property *old_prop, **propp;
412 __of_changeset_entry_dump(ce);
414 raw_spin_lock_irqsave(&devtree_lock, flags);
415 switch (ce->action) {
416 case OF_RECONFIG_ATTACH_NODE:
417 __of_attach_node(ce->np);
419 case OF_RECONFIG_DETACH_NODE:
420 __of_detach_node(ce->np);
422 case OF_RECONFIG_ADD_PROPERTY:
423 /* If the property is in deadprops then it must be removed */
424 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
425 if (*propp == ce->prop) {
426 *propp = ce->prop->next;
427 ce->prop->next = NULL;
432 ret = __of_add_property(ce->np, ce->prop);
434 pr_err("%s: add_property failed @%s/%s\n",
435 __func__, ce->np->full_name,
440 case OF_RECONFIG_REMOVE_PROPERTY:
441 ret = __of_remove_property(ce->np, ce->prop);
443 pr_err("%s: remove_property failed @%s/%s\n",
444 __func__, ce->np->full_name,
450 case OF_RECONFIG_UPDATE_PROPERTY:
451 /* If the property is in deadprops then it must be removed */
452 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
453 if (*propp == ce->prop) {
454 *propp = ce->prop->next;
455 ce->prop->next = NULL;
460 ret = __of_update_property(ce->np, ce->prop, &old_prop);
462 pr_err("%s: update_property failed @%s/%s\n",
463 __func__, ce->np->full_name,
471 raw_spin_unlock_irqrestore(&devtree_lock, flags);
476 switch (ce->action) {
477 case OF_RECONFIG_ATTACH_NODE:
478 __of_attach_node_sysfs(ce->np);
480 case OF_RECONFIG_DETACH_NODE:
481 __of_detach_node_sysfs(ce->np);
483 case OF_RECONFIG_ADD_PROPERTY:
484 /* ignore duplicate names */
485 __of_add_property_sysfs(ce->np, ce->prop);
487 case OF_RECONFIG_REMOVE_PROPERTY:
488 __of_remove_property_sysfs(ce->np, ce->prop);
490 case OF_RECONFIG_UPDATE_PROPERTY:
491 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
498 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
500 struct of_changeset_entry ce_inverted;
502 __of_changeset_entry_invert(ce, &ce_inverted);
503 return __of_changeset_entry_apply(&ce_inverted);
507 * of_changeset_init - Initialize a changeset for use
509 * @ocs: changeset pointer
511 * Initialize a changeset structure
513 void of_changeset_init(struct of_changeset *ocs)
515 memset(ocs, 0, sizeof(*ocs));
516 INIT_LIST_HEAD(&ocs->entries);
520 * of_changeset_destroy - Destroy a changeset
522 * @ocs: changeset pointer
524 * Destroys a changeset. Note that if a changeset is applied,
525 * its changes to the tree cannot be reverted.
527 void of_changeset_destroy(struct of_changeset *ocs)
529 struct of_changeset_entry *ce, *cen;
531 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
532 __of_changeset_entry_destroy(ce);
536 * of_changeset_apply - Applies a changeset
538 * @ocs: changeset pointer
540 * Applies a changeset to the live tree.
541 * Any side-effects of live tree state changes are applied here on
542 * sucess, like creation/destruction of devices and side-effects
543 * like creation of sysfs properties and directories.
544 * Returns 0 on success, a negative error value in case of an error.
545 * On error the partially applied effects are reverted.
547 int of_changeset_apply(struct of_changeset *ocs)
549 struct of_changeset_entry *ce;
552 /* perform the rest of the work */
553 pr_debug("of_changeset: applying...\n");
554 list_for_each_entry(ce, &ocs->entries, node) {
555 ret = __of_changeset_entry_apply(ce);
557 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
558 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
559 __of_changeset_entry_revert(ce);
563 pr_debug("of_changeset: applied, emitting notifiers.\n");
565 /* drop the global lock while emitting notifiers */
566 mutex_unlock(&of_mutex);
567 list_for_each_entry(ce, &ocs->entries, node)
568 __of_changeset_entry_notify(ce, 0);
569 mutex_lock(&of_mutex);
570 pr_debug("of_changeset: notifiers sent.\n");
576 * of_changeset_revert - Reverts an applied changeset
578 * @ocs: changeset pointer
580 * Reverts a changeset returning the state of the tree to what it
581 * was before the application.
582 * Any side-effects like creation/destruction of devices and
583 * removal of sysfs properties and directories are applied.
584 * Returns 0 on success, a negative error value in case of an error.
586 int of_changeset_revert(struct of_changeset *ocs)
588 struct of_changeset_entry *ce;
591 pr_debug("of_changeset: reverting...\n");
592 list_for_each_entry_reverse(ce, &ocs->entries, node) {
593 ret = __of_changeset_entry_revert(ce);
595 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
596 list_for_each_entry_continue(ce, &ocs->entries, node)
597 __of_changeset_entry_apply(ce);
601 pr_debug("of_changeset: reverted, emitting notifiers.\n");
603 /* drop the global lock while emitting notifiers */
604 mutex_unlock(&of_mutex);
605 list_for_each_entry_reverse(ce, &ocs->entries, node)
606 __of_changeset_entry_notify(ce, 1);
607 mutex_lock(&of_mutex);
608 pr_debug("of_changeset: notifiers sent.\n");
614 * of_changeset_action - Perform a changeset action
616 * @ocs: changeset pointer
617 * @action: action to perform
618 * @np: Pointer to device node
619 * @prop: Pointer to property
621 * On action being one of:
622 * + OF_RECONFIG_ATTACH_NODE
623 * + OF_RECONFIG_DETACH_NODE,
624 * + OF_RECONFIG_ADD_PROPERTY
625 * + OF_RECONFIG_REMOVE_PROPERTY,
626 * + OF_RECONFIG_UPDATE_PROPERTY
627 * Returns 0 on success, a negative error value in case of an error.
629 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
630 struct device_node *np, struct property *prop)
632 struct of_changeset_entry *ce;
634 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
636 pr_err("%s: Failed to allocate\n", __func__);
639 /* get a reference to the node */
641 ce->np = of_node_get(np);
644 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
645 ce->old_prop = of_find_property(np, prop->name, NULL);
647 /* add it to the list */
648 list_add_tail(&ce->node, &ocs->entries);