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
->allnext
= np
->parent
->allnext
;
121 np
->parent
->allnext
= np
;
122 np
->parent
->child
= np
;
123 of_node_clear_flag(np
, OF_DETACHED
);
127 * of_attach_node() - Plug a device node into the tree and global list.
129 int of_attach_node(struct device_node
*np
)
133 mutex_lock(&of_mutex
);
134 raw_spin_lock_irqsave(&devtree_lock
, flags
);
135 __of_attach_node(np
);
136 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
138 __of_attach_node_sysfs(np
);
139 mutex_unlock(&of_mutex
);
141 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
146 void __of_detach_node(struct device_node
*np
)
148 struct device_node
*parent
;
150 if (WARN_ON(of_node_check_flag(np
, OF_DETACHED
)))
154 if (WARN_ON(!parent
))
157 if (of_allnodes
== np
)
158 of_allnodes
= np
->allnext
;
160 struct device_node
*prev
;
161 for (prev
= of_allnodes
;
163 prev
= prev
->allnext
)
165 prev
->allnext
= np
->allnext
;
168 if (parent
->child
== np
)
169 parent
->child
= np
->sibling
;
171 struct device_node
*prevsib
;
172 for (prevsib
= np
->parent
->child
;
173 prevsib
->sibling
!= np
;
174 prevsib
= prevsib
->sibling
)
176 prevsib
->sibling
= np
->sibling
;
179 of_node_set_flag(np
, OF_DETACHED
);
183 * of_detach_node() - "Unplug" a node from the device tree.
185 * The caller must hold a reference to the node. The memory associated with
186 * the node is not freed until its refcount goes to zero.
188 int of_detach_node(struct device_node
*np
)
193 mutex_lock(&of_mutex
);
194 raw_spin_lock_irqsave(&devtree_lock
, flags
);
195 __of_detach_node(np
);
196 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
198 __of_detach_node_sysfs(np
);
199 mutex_unlock(&of_mutex
);
201 of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
207 * of_node_release() - release a dynamically allocated node
208 * @kref: kref element of the node to be released
210 * In of_node_put() this function is passed to kref_put() as the destructor.
212 void of_node_release(struct kobject
*kobj
)
214 struct device_node
*node
= kobj_to_device_node(kobj
);
215 struct property
*prop
= node
->properties
;
217 /* We should never be releasing nodes that haven't been detached. */
218 if (!of_node_check_flag(node
, OF_DETACHED
)) {
219 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
224 if (!of_node_check_flag(node
, OF_DYNAMIC
))
228 struct property
*next
= prop
->next
;
235 prop
= node
->deadprops
;
236 node
->deadprops
= NULL
;
239 kfree(node
->full_name
);
245 * __of_prop_dup - Copy a property dynamically.
246 * @prop: Property to copy
247 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
249 * Copy a property by dynamically allocating the memory of both the
250 * property stucture and the property name & contents. The property's
251 * flags have the OF_DYNAMIC bit set so that we can differentiate between
252 * dynamically allocated properties and not.
253 * Returns the newly allocated property or NULL on out of memory error.
255 struct property
*__of_prop_dup(const struct property
*prop
, gfp_t allocflags
)
257 struct property
*new;
259 new = kzalloc(sizeof(*new), allocflags
);
264 * NOTE: There is no check for zero length value.
265 * In case of a boolean property, this will allocate a value
266 * of zero bytes. We do this to work around the use
267 * of of_get_property() calls on boolean values.
269 new->name
= kstrdup(prop
->name
, allocflags
);
270 new->value
= kmemdup(prop
->value
, prop
->length
, allocflags
);
271 new->length
= prop
->length
;
272 if (!new->name
|| !new->value
)
275 /* mark the property as dynamic */
276 of_property_set_flag(new, OF_DYNAMIC
);
288 * __of_node_alloc() - Create an empty device node dynamically.
289 * @full_name: Full name of the new device node
290 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
292 * Create an empty device tree node, suitable for further modification.
293 * The node data are dynamically allocated and all the node flags
294 * have the OF_DYNAMIC & OF_DETACHED bits set.
295 * Returns the newly allocated node or NULL on out of memory error.
297 struct device_node
*__of_node_alloc(const char *full_name
, gfp_t allocflags
)
299 struct device_node
*node
;
301 node
= kzalloc(sizeof(*node
), allocflags
);
305 node
->full_name
= kstrdup(full_name
, allocflags
);
306 of_node_set_flag(node
, OF_DYNAMIC
);
307 of_node_set_flag(node
, OF_DETACHED
);
308 if (!node
->full_name
)
316 kfree(node
->full_name
);
321 static void __of_changeset_entry_destroy(struct of_changeset_entry
*ce
)
329 static void __of_changeset_entry_dump(struct of_changeset_entry
*ce
)
331 switch (ce
->action
) {
332 case OF_RECONFIG_ADD_PROPERTY
:
333 pr_debug("%p: %s %s/%s\n",
334 ce
, "ADD_PROPERTY ", ce
->np
->full_name
,
337 case OF_RECONFIG_REMOVE_PROPERTY
:
338 pr_debug("%p: %s %s/%s\n",
339 ce
, "REMOVE_PROPERTY", ce
->np
->full_name
,
342 case OF_RECONFIG_UPDATE_PROPERTY
:
343 pr_debug("%p: %s %s/%s\n",
344 ce
, "UPDATE_PROPERTY", ce
->np
->full_name
,
347 case OF_RECONFIG_ATTACH_NODE
:
348 pr_debug("%p: %s %s\n",
349 ce
, "ATTACH_NODE ", ce
->np
->full_name
);
351 case OF_RECONFIG_DETACH_NODE
:
352 pr_debug("%p: %s %s\n",
353 ce
, "DETACH_NODE ", ce
->np
->full_name
);
358 static inline void __of_changeset_entry_dump(struct of_changeset_entry
*ce
)
364 static void __of_changeset_entry_invert(struct of_changeset_entry
*ce
,
365 struct of_changeset_entry
*rce
)
367 memcpy(rce
, ce
, sizeof(*rce
));
369 switch (ce
->action
) {
370 case OF_RECONFIG_ATTACH_NODE
:
371 rce
->action
= OF_RECONFIG_DETACH_NODE
;
373 case OF_RECONFIG_DETACH_NODE
:
374 rce
->action
= OF_RECONFIG_ATTACH_NODE
;
376 case OF_RECONFIG_ADD_PROPERTY
:
377 rce
->action
= OF_RECONFIG_REMOVE_PROPERTY
;
379 case OF_RECONFIG_REMOVE_PROPERTY
:
380 rce
->action
= OF_RECONFIG_ADD_PROPERTY
;
382 case OF_RECONFIG_UPDATE_PROPERTY
:
383 rce
->old_prop
= ce
->prop
;
384 rce
->prop
= ce
->old_prop
;
389 static void __of_changeset_entry_notify(struct of_changeset_entry
*ce
, bool revert
)
391 struct of_changeset_entry ce_inverted
;
395 __of_changeset_entry_invert(ce
, &ce_inverted
);
399 switch (ce
->action
) {
400 case OF_RECONFIG_ATTACH_NODE
:
401 case OF_RECONFIG_DETACH_NODE
:
402 ret
= of_reconfig_notify(ce
->action
, ce
->np
);
404 case OF_RECONFIG_ADD_PROPERTY
:
405 case OF_RECONFIG_REMOVE_PROPERTY
:
406 case OF_RECONFIG_UPDATE_PROPERTY
:
407 ret
= of_property_notify(ce
->action
, ce
->np
, ce
->prop
, ce
->old_prop
);
410 pr_err("%s: invalid devicetree changeset action: %i\n", __func__
,
416 pr_err("%s: notifier error @%s\n", __func__
, ce
->np
->full_name
);
419 static int __of_changeset_entry_apply(struct of_changeset_entry
*ce
)
421 struct property
*old_prop
, **propp
;
425 __of_changeset_entry_dump(ce
);
427 raw_spin_lock_irqsave(&devtree_lock
, flags
);
428 switch (ce
->action
) {
429 case OF_RECONFIG_ATTACH_NODE
:
430 __of_attach_node(ce
->np
);
432 case OF_RECONFIG_DETACH_NODE
:
433 __of_detach_node(ce
->np
);
435 case OF_RECONFIG_ADD_PROPERTY
:
436 /* If the property is in deadprops then it must be removed */
437 for (propp
= &ce
->np
->deadprops
; *propp
; propp
= &(*propp
)->next
) {
438 if (*propp
== ce
->prop
) {
439 *propp
= ce
->prop
->next
;
440 ce
->prop
->next
= NULL
;
445 ret
= __of_add_property(ce
->np
, ce
->prop
);
447 pr_err("%s: add_property failed @%s/%s\n",
448 __func__
, ce
->np
->full_name
,
453 case OF_RECONFIG_REMOVE_PROPERTY
:
454 ret
= __of_remove_property(ce
->np
, ce
->prop
);
456 pr_err("%s: remove_property failed @%s/%s\n",
457 __func__
, ce
->np
->full_name
,
463 case OF_RECONFIG_UPDATE_PROPERTY
:
464 /* If the property is in deadprops then it must be removed */
465 for (propp
= &ce
->np
->deadprops
; *propp
; propp
= &(*propp
)->next
) {
466 if (*propp
== ce
->prop
) {
467 *propp
= ce
->prop
->next
;
468 ce
->prop
->next
= NULL
;
473 ret
= __of_update_property(ce
->np
, ce
->prop
, &old_prop
);
475 pr_err("%s: update_property failed @%s/%s\n",
476 __func__
, ce
->np
->full_name
,
484 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
489 switch (ce
->action
) {
490 case OF_RECONFIG_ATTACH_NODE
:
491 __of_attach_node_sysfs(ce
->np
);
493 case OF_RECONFIG_DETACH_NODE
:
494 __of_detach_node_sysfs(ce
->np
);
496 case OF_RECONFIG_ADD_PROPERTY
:
497 /* ignore duplicate names */
498 __of_add_property_sysfs(ce
->np
, ce
->prop
);
500 case OF_RECONFIG_REMOVE_PROPERTY
:
501 __of_remove_property_sysfs(ce
->np
, ce
->prop
);
503 case OF_RECONFIG_UPDATE_PROPERTY
:
504 __of_update_property_sysfs(ce
->np
, ce
->prop
, ce
->old_prop
);
511 static inline int __of_changeset_entry_revert(struct of_changeset_entry
*ce
)
513 struct of_changeset_entry ce_inverted
;
515 __of_changeset_entry_invert(ce
, &ce_inverted
);
516 return __of_changeset_entry_apply(&ce_inverted
);
520 * of_changeset_init - Initialize a changeset for use
522 * @ocs: changeset pointer
524 * Initialize a changeset structure
526 void of_changeset_init(struct of_changeset
*ocs
)
528 memset(ocs
, 0, sizeof(*ocs
));
529 INIT_LIST_HEAD(&ocs
->entries
);
533 * of_changeset_destroy - Destroy a changeset
535 * @ocs: changeset pointer
537 * Destroys a changeset. Note that if a changeset is applied,
538 * its changes to the tree cannot be reverted.
540 void of_changeset_destroy(struct of_changeset
*ocs
)
542 struct of_changeset_entry
*ce
, *cen
;
544 list_for_each_entry_safe_reverse(ce
, cen
, &ocs
->entries
, node
)
545 __of_changeset_entry_destroy(ce
);
549 * of_changeset_apply - Applies a changeset
551 * @ocs: changeset pointer
553 * Applies a changeset to the live tree.
554 * Any side-effects of live tree state changes are applied here on
555 * sucess, like creation/destruction of devices and side-effects
556 * like creation of sysfs properties and directories.
557 * Returns 0 on success, a negative error value in case of an error.
558 * On error the partially applied effects are reverted.
560 int of_changeset_apply(struct of_changeset
*ocs
)
562 struct of_changeset_entry
*ce
;
565 /* perform the rest of the work */
566 pr_debug("of_changeset: applying...\n");
567 list_for_each_entry(ce
, &ocs
->entries
, node
) {
568 ret
= __of_changeset_entry_apply(ce
);
570 pr_err("%s: Error applying changeset (%d)\n", __func__
, ret
);
571 list_for_each_entry_continue_reverse(ce
, &ocs
->entries
, node
)
572 __of_changeset_entry_revert(ce
);
576 pr_debug("of_changeset: applied, emitting notifiers.\n");
578 /* drop the global lock while emitting notifiers */
579 mutex_unlock(&of_mutex
);
580 list_for_each_entry(ce
, &ocs
->entries
, node
)
581 __of_changeset_entry_notify(ce
, 0);
582 mutex_lock(&of_mutex
);
583 pr_debug("of_changeset: notifiers sent.\n");
589 * of_changeset_revert - Reverts an applied changeset
591 * @ocs: changeset pointer
593 * Reverts a changeset returning the state of the tree to what it
594 * was before the application.
595 * Any side-effects like creation/destruction of devices and
596 * removal of sysfs properties and directories are applied.
597 * Returns 0 on success, a negative error value in case of an error.
599 int of_changeset_revert(struct of_changeset
*ocs
)
601 struct of_changeset_entry
*ce
;
604 pr_debug("of_changeset: reverting...\n");
605 list_for_each_entry_reverse(ce
, &ocs
->entries
, node
) {
606 ret
= __of_changeset_entry_revert(ce
);
608 pr_err("%s: Error reverting changeset (%d)\n", __func__
, ret
);
609 list_for_each_entry_continue(ce
, &ocs
->entries
, node
)
610 __of_changeset_entry_apply(ce
);
614 pr_debug("of_changeset: reverted, emitting notifiers.\n");
616 /* drop the global lock while emitting notifiers */
617 mutex_unlock(&of_mutex
);
618 list_for_each_entry_reverse(ce
, &ocs
->entries
, node
)
619 __of_changeset_entry_notify(ce
, 1);
620 mutex_lock(&of_mutex
);
621 pr_debug("of_changeset: notifiers sent.\n");
627 * of_changeset_action - Perform a changeset action
629 * @ocs: changeset pointer
630 * @action: action to perform
631 * @np: Pointer to device node
632 * @prop: Pointer to property
634 * On action being one of:
635 * + OF_RECONFIG_ATTACH_NODE
636 * + OF_RECONFIG_DETACH_NODE,
637 * + OF_RECONFIG_ADD_PROPERTY
638 * + OF_RECONFIG_REMOVE_PROPERTY,
639 * + OF_RECONFIG_UPDATE_PROPERTY
640 * Returns 0 on success, a negative error value in case of an error.
642 int of_changeset_action(struct of_changeset
*ocs
, unsigned long action
,
643 struct device_node
*np
, struct property
*prop
)
645 struct of_changeset_entry
*ce
;
647 ce
= kzalloc(sizeof(*ce
), GFP_KERNEL
);
649 pr_err("%s: Failed to allocate\n", __func__
);
652 /* get a reference to the node */
654 ce
->np
= of_node_get(np
);
657 if (action
== OF_RECONFIG_UPDATE_PROPERTY
&& prop
)
658 ce
->old_prop
= of_find_property(np
, prop
->name
, NULL
);
660 /* add it to the list */
661 list_add_tail(&ce
->node
, &ocs
->entries
);