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
);
85 int of_property_notify(int action
, struct device_node
*np
,
86 struct property
*prop
, struct property
*oldprop
)
88 struct of_prop_reconfig pr
;
90 /* only call notifiers if the node is attached */
91 if (!of_node_is_attached(np
))
96 pr
.old_prop
= oldprop
;
97 return of_reconfig_notify(action
, &pr
);
100 void __of_attach_node(struct device_node
*np
)
102 const __be32
*phandle
;
105 np
->name
= __of_get_property(np
, "name", NULL
) ? : "<NULL>";
106 np
->type
= __of_get_property(np
, "device_type", NULL
) ? : "<NULL>";
108 phandle
= __of_get_property(np
, "phandle", &sz
);
110 phandle
= __of_get_property(np
, "linux,phandle", &sz
);
111 if (IS_ENABLED(PPC_PSERIES
) && !phandle
)
112 phandle
= __of_get_property(np
, "ibm,phandle", &sz
);
113 np
->phandle
= (phandle
&& (sz
>= 4)) ? be32_to_cpup(phandle
) : 0;
116 np
->sibling
= np
->parent
->child
;
117 np
->allnext
= np
->parent
->allnext
;
118 np
->parent
->allnext
= np
;
119 np
->parent
->child
= np
;
120 of_node_clear_flag(np
, OF_DETACHED
);
124 * of_attach_node() - Plug a device node into the tree and global list.
126 int of_attach_node(struct device_node
*np
)
130 mutex_lock(&of_mutex
);
131 raw_spin_lock_irqsave(&devtree_lock
, flags
);
132 __of_attach_node(np
);
133 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
135 __of_attach_node_sysfs(np
);
136 mutex_unlock(&of_mutex
);
138 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
143 void __of_detach_node(struct device_node
*np
)
145 struct device_node
*parent
;
147 if (WARN_ON(of_node_check_flag(np
, OF_DETACHED
)))
151 if (WARN_ON(!parent
))
154 if (of_allnodes
== np
)
155 of_allnodes
= np
->allnext
;
157 struct device_node
*prev
;
158 for (prev
= of_allnodes
;
160 prev
= prev
->allnext
)
162 prev
->allnext
= np
->allnext
;
165 if (parent
->child
== np
)
166 parent
->child
= np
->sibling
;
168 struct device_node
*prevsib
;
169 for (prevsib
= np
->parent
->child
;
170 prevsib
->sibling
!= np
;
171 prevsib
= prevsib
->sibling
)
173 prevsib
->sibling
= np
->sibling
;
176 of_node_set_flag(np
, OF_DETACHED
);
180 * of_detach_node() - "Unplug" a node from the device tree.
182 * The caller must hold a reference to the node. The memory associated with
183 * the node is not freed until its refcount goes to zero.
185 int of_detach_node(struct device_node
*np
)
190 mutex_lock(&of_mutex
);
191 raw_spin_lock_irqsave(&devtree_lock
, flags
);
192 __of_detach_node(np
);
193 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
195 __of_detach_node_sysfs(np
);
196 mutex_unlock(&of_mutex
);
198 of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
204 * of_node_release() - release a dynamically allocated node
205 * @kref: kref element of the node to be released
207 * In of_node_put() this function is passed to kref_put() as the destructor.
209 void of_node_release(struct kobject
*kobj
)
211 struct device_node
*node
= kobj_to_device_node(kobj
);
212 struct property
*prop
= node
->properties
;
214 /* We should never be releasing nodes that haven't been detached. */
215 if (!of_node_check_flag(node
, OF_DETACHED
)) {
216 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
221 if (!of_node_check_flag(node
, OF_DYNAMIC
))
225 struct property
*next
= prop
->next
;
232 prop
= node
->deadprops
;
233 node
->deadprops
= NULL
;
236 kfree(node
->full_name
);
242 * __of_prop_dup - Copy a property dynamically.
243 * @prop: Property to copy
244 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
246 * Copy a property by dynamically allocating the memory of both the
247 * property stucture and the property name & contents. The property's
248 * flags have the OF_DYNAMIC bit set so that we can differentiate between
249 * dynamically allocated properties and not.
250 * Returns the newly allocated property or NULL on out of memory error.
252 struct property
*__of_prop_dup(const struct property
*prop
, gfp_t allocflags
)
254 struct property
*new;
256 new = kzalloc(sizeof(*new), allocflags
);
261 * NOTE: There is no check for zero length value.
262 * In case of a boolean property, this will allocate a value
263 * of zero bytes. We do this to work around the use
264 * of of_get_property() calls on boolean values.
266 new->name
= kstrdup(prop
->name
, allocflags
);
267 new->value
= kmemdup(prop
->value
, prop
->length
, allocflags
);
268 new->length
= prop
->length
;
269 if (!new->name
|| !new->value
)
272 /* mark the property as dynamic */
273 of_property_set_flag(new, OF_DYNAMIC
);
285 * __of_node_alloc() - Create an empty device node dynamically.
286 * @full_name: Full name of the new device node
287 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
289 * Create an empty device tree node, suitable for further modification.
290 * The node data are dynamically allocated and all the node flags
291 * have the OF_DYNAMIC & OF_DETACHED bits set.
292 * Returns the newly allocated node or NULL on out of memory error.
294 struct device_node
*__of_node_alloc(const char *full_name
, gfp_t allocflags
)
296 struct device_node
*node
;
298 node
= kzalloc(sizeof(*node
), allocflags
);
302 node
->full_name
= kstrdup(full_name
, allocflags
);
303 of_node_set_flag(node
, OF_DYNAMIC
);
304 of_node_set_flag(node
, OF_DETACHED
);
305 if (!node
->full_name
)
313 kfree(node
->full_name
);
318 static void __of_changeset_entry_destroy(struct of_changeset_entry
*ce
)
326 static void __of_changeset_entry_dump(struct of_changeset_entry
*ce
)
328 switch (ce
->action
) {
329 case OF_RECONFIG_ADD_PROPERTY
:
330 pr_debug("%p: %s %s/%s\n",
331 ce
, "ADD_PROPERTY ", ce
->np
->full_name
,
334 case OF_RECONFIG_REMOVE_PROPERTY
:
335 pr_debug("%p: %s %s/%s\n",
336 ce
, "REMOVE_PROPERTY", ce
->np
->full_name
,
339 case OF_RECONFIG_UPDATE_PROPERTY
:
340 pr_debug("%p: %s %s/%s\n",
341 ce
, "UPDATE_PROPERTY", ce
->np
->full_name
,
344 case OF_RECONFIG_ATTACH_NODE
:
345 pr_debug("%p: %s %s\n",
346 ce
, "ATTACH_NODE ", ce
->np
->full_name
);
348 case OF_RECONFIG_DETACH_NODE
:
349 pr_debug("%p: %s %s\n",
350 ce
, "DETACH_NODE ", ce
->np
->full_name
);
355 static inline void __of_changeset_entry_dump(struct of_changeset_entry
*ce
)
361 static void __of_changeset_entry_invert(struct of_changeset_entry
*ce
,
362 struct of_changeset_entry
*rce
)
364 memcpy(rce
, ce
, sizeof(*rce
));
366 switch (ce
->action
) {
367 case OF_RECONFIG_ATTACH_NODE
:
368 rce
->action
= OF_RECONFIG_DETACH_NODE
;
370 case OF_RECONFIG_DETACH_NODE
:
371 rce
->action
= OF_RECONFIG_ATTACH_NODE
;
373 case OF_RECONFIG_ADD_PROPERTY
:
374 rce
->action
= OF_RECONFIG_REMOVE_PROPERTY
;
376 case OF_RECONFIG_REMOVE_PROPERTY
:
377 rce
->action
= OF_RECONFIG_ADD_PROPERTY
;
379 case OF_RECONFIG_UPDATE_PROPERTY
:
380 rce
->old_prop
= ce
->prop
;
381 rce
->prop
= ce
->old_prop
;
386 static void __of_changeset_entry_notify(struct of_changeset_entry
*ce
, bool revert
)
388 struct of_changeset_entry ce_inverted
;
392 __of_changeset_entry_invert(ce
, &ce_inverted
);
396 switch (ce
->action
) {
397 case OF_RECONFIG_ATTACH_NODE
:
398 case OF_RECONFIG_DETACH_NODE
:
399 ret
= of_reconfig_notify(ce
->action
, ce
->np
);
401 case OF_RECONFIG_ADD_PROPERTY
:
402 case OF_RECONFIG_REMOVE_PROPERTY
:
403 case OF_RECONFIG_UPDATE_PROPERTY
:
404 ret
= of_property_notify(ce
->action
, ce
->np
, ce
->prop
, ce
->old_prop
);
407 pr_err("%s: invalid devicetree changeset action: %i\n", __func__
,
413 pr_err("%s: notifier error @%s\n", __func__
, ce
->np
->full_name
);
416 static int __of_changeset_entry_apply(struct of_changeset_entry
*ce
)
418 struct property
*old_prop
, **propp
;
422 __of_changeset_entry_dump(ce
);
424 raw_spin_lock_irqsave(&devtree_lock
, flags
);
425 switch (ce
->action
) {
426 case OF_RECONFIG_ATTACH_NODE
:
427 __of_attach_node(ce
->np
);
429 case OF_RECONFIG_DETACH_NODE
:
430 __of_detach_node(ce
->np
);
432 case OF_RECONFIG_ADD_PROPERTY
:
433 /* If the property is in deadprops then it must be removed */
434 for (propp
= &ce
->np
->deadprops
; *propp
; propp
= &(*propp
)->next
) {
435 if (*propp
== ce
->prop
) {
436 *propp
= ce
->prop
->next
;
437 ce
->prop
->next
= NULL
;
442 ret
= __of_add_property(ce
->np
, ce
->prop
);
444 pr_err("%s: add_property failed @%s/%s\n",
445 __func__
, ce
->np
->full_name
,
450 case OF_RECONFIG_REMOVE_PROPERTY
:
451 ret
= __of_remove_property(ce
->np
, ce
->prop
);
453 pr_err("%s: remove_property failed @%s/%s\n",
454 __func__
, ce
->np
->full_name
,
460 case OF_RECONFIG_UPDATE_PROPERTY
:
461 /* If the property is in deadprops then it must be removed */
462 for (propp
= &ce
->np
->deadprops
; *propp
; propp
= &(*propp
)->next
) {
463 if (*propp
== ce
->prop
) {
464 *propp
= ce
->prop
->next
;
465 ce
->prop
->next
= NULL
;
470 ret
= __of_update_property(ce
->np
, ce
->prop
, &old_prop
);
472 pr_err("%s: update_property failed @%s/%s\n",
473 __func__
, ce
->np
->full_name
,
481 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
486 switch (ce
->action
) {
487 case OF_RECONFIG_ATTACH_NODE
:
488 __of_attach_node_sysfs(ce
->np
);
490 case OF_RECONFIG_DETACH_NODE
:
491 __of_detach_node_sysfs(ce
->np
);
493 case OF_RECONFIG_ADD_PROPERTY
:
494 /* ignore duplicate names */
495 __of_add_property_sysfs(ce
->np
, ce
->prop
);
497 case OF_RECONFIG_REMOVE_PROPERTY
:
498 __of_remove_property_sysfs(ce
->np
, ce
->prop
);
500 case OF_RECONFIG_UPDATE_PROPERTY
:
501 __of_update_property_sysfs(ce
->np
, ce
->prop
, ce
->old_prop
);
508 static inline int __of_changeset_entry_revert(struct of_changeset_entry
*ce
)
510 struct of_changeset_entry ce_inverted
;
512 __of_changeset_entry_invert(ce
, &ce_inverted
);
513 return __of_changeset_entry_apply(&ce_inverted
);
517 * of_changeset_init - Initialize a changeset for use
519 * @ocs: changeset pointer
521 * Initialize a changeset structure
523 void of_changeset_init(struct of_changeset
*ocs
)
525 memset(ocs
, 0, sizeof(*ocs
));
526 INIT_LIST_HEAD(&ocs
->entries
);
530 * of_changeset_destroy - Destroy a changeset
532 * @ocs: changeset pointer
534 * Destroys a changeset. Note that if a changeset is applied,
535 * its changes to the tree cannot be reverted.
537 void of_changeset_destroy(struct of_changeset
*ocs
)
539 struct of_changeset_entry
*ce
, *cen
;
541 list_for_each_entry_safe_reverse(ce
, cen
, &ocs
->entries
, node
)
542 __of_changeset_entry_destroy(ce
);
546 * of_changeset_apply - Applies a changeset
548 * @ocs: changeset pointer
550 * Applies a changeset to the live tree.
551 * Any side-effects of live tree state changes are applied here on
552 * sucess, like creation/destruction of devices and side-effects
553 * like creation of sysfs properties and directories.
554 * Returns 0 on success, a negative error value in case of an error.
555 * On error the partially applied effects are reverted.
557 int of_changeset_apply(struct of_changeset
*ocs
)
559 struct of_changeset_entry
*ce
;
562 /* perform the rest of the work */
563 pr_debug("of_changeset: applying...\n");
564 list_for_each_entry(ce
, &ocs
->entries
, node
) {
565 ret
= __of_changeset_entry_apply(ce
);
567 pr_err("%s: Error applying changeset (%d)\n", __func__
, ret
);
568 list_for_each_entry_continue_reverse(ce
, &ocs
->entries
, node
)
569 __of_changeset_entry_revert(ce
);
573 pr_debug("of_changeset: applied, emitting notifiers.\n");
575 /* drop the global lock while emitting notifiers */
576 mutex_unlock(&of_mutex
);
577 list_for_each_entry(ce
, &ocs
->entries
, node
)
578 __of_changeset_entry_notify(ce
, 0);
579 mutex_lock(&of_mutex
);
580 pr_debug("of_changeset: notifiers sent.\n");
586 * of_changeset_revert - Reverts an applied changeset
588 * @ocs: changeset pointer
590 * Reverts a changeset returning the state of the tree to what it
591 * was before the application.
592 * Any side-effects like creation/destruction of devices and
593 * removal of sysfs properties and directories are applied.
594 * Returns 0 on success, a negative error value in case of an error.
596 int of_changeset_revert(struct of_changeset
*ocs
)
598 struct of_changeset_entry
*ce
;
601 pr_debug("of_changeset: reverting...\n");
602 list_for_each_entry_reverse(ce
, &ocs
->entries
, node
) {
603 ret
= __of_changeset_entry_revert(ce
);
605 pr_err("%s: Error reverting changeset (%d)\n", __func__
, ret
);
606 list_for_each_entry_continue(ce
, &ocs
->entries
, node
)
607 __of_changeset_entry_apply(ce
);
611 pr_debug("of_changeset: reverted, emitting notifiers.\n");
613 /* drop the global lock while emitting notifiers */
614 mutex_unlock(&of_mutex
);
615 list_for_each_entry_reverse(ce
, &ocs
->entries
, node
)
616 __of_changeset_entry_notify(ce
, 1);
617 mutex_lock(&of_mutex
);
618 pr_debug("of_changeset: notifiers sent.\n");
624 * of_changeset_action - Perform a changeset action
626 * @ocs: changeset pointer
627 * @action: action to perform
628 * @np: Pointer to device node
629 * @prop: Pointer to property
631 * On action being one of:
632 * + OF_RECONFIG_ATTACH_NODE
633 * + OF_RECONFIG_DETACH_NODE,
634 * + OF_RECONFIG_ADD_PROPERTY
635 * + OF_RECONFIG_REMOVE_PROPERTY,
636 * + OF_RECONFIG_UPDATE_PROPERTY
637 * Returns 0 on success, a negative error value in case of an error.
639 int of_changeset_action(struct of_changeset
*ocs
, unsigned long action
,
640 struct device_node
*np
, struct property
*prop
)
642 struct of_changeset_entry
*ce
;
644 ce
= kzalloc(sizeof(*ce
), GFP_KERNEL
);
646 pr_err("%s: Failed to allocate\n", __func__
);
649 /* get a reference to the node */
651 ce
->np
= of_node_get(np
);
654 if (action
== OF_RECONFIG_UPDATE_PROPERTY
&& prop
)
655 ce
->old_prop
= of_find_property(np
, prop
->name
, NULL
);
657 /* add it to the list */
658 list_add_tail(&ce
->node
, &ocs
->entries
);