2 * Functions for working with device tree overlays
4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
5 * Copyright (C) 2012 Texas Instruments Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
12 #define pr_fmt(fmt) "OF: overlay: " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/errno.h>
21 #include <linux/string.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/idr.h>
26 #include "of_private.h"
29 * struct of_overlay_info - Holds a single overlay info
30 * @target: target of the overlay operation
31 * @overlay: pointer to the overlay contents node
33 * Holds a single overlay state, including all the overlay logs &
36 struct of_overlay_info
{
37 struct device_node
*target
;
38 struct device_node
*overlay
;
42 * struct of_overlay - Holds a complete overlay transaction
43 * @node: List on which we are located
44 * @count: Count of ovinfo structures
45 * @ovinfo_tab: Overlay info table (count sized)
46 * @cset: Changeset to be used
48 * Holds a complete overlay transaction
52 struct list_head node
;
54 struct of_overlay_info
*ovinfo_tab
;
55 struct of_changeset cset
;
58 static int of_overlay_apply_one(struct of_overlay
*ov
,
59 struct device_node
*target
, const struct device_node
*overlay
);
61 static BLOCKING_NOTIFIER_HEAD(of_overlay_chain
);
63 int of_overlay_notifier_register(struct notifier_block
*nb
)
65 return blocking_notifier_chain_register(&of_overlay_chain
, nb
);
67 EXPORT_SYMBOL_GPL(of_overlay_notifier_register
);
69 int of_overlay_notifier_unregister(struct notifier_block
*nb
)
71 return blocking_notifier_chain_unregister(&of_overlay_chain
, nb
);
73 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister
);
75 static int of_overlay_notify(struct of_overlay
*ov
,
76 enum of_overlay_notify_action action
)
78 struct of_overlay_notify_data nd
;
81 for (i
= 0; i
< ov
->count
; i
++) {
82 struct of_overlay_info
*ovinfo
= &ov
->ovinfo_tab
[i
];
84 nd
.target
= ovinfo
->target
;
85 nd
.overlay
= ovinfo
->overlay
;
87 ret
= blocking_notifier_call_chain(&of_overlay_chain
,
90 return notifier_to_errno(ret
);
96 static int of_overlay_apply_single_property(struct of_overlay
*ov
,
97 struct device_node
*target
, struct property
*prop
)
99 struct property
*propn
, *tprop
;
101 /* NOTE: Multiple changes of single properties not supported */
102 tprop
= of_find_property(target
, prop
->name
, NULL
);
104 /* special properties are not meant to be updated (silent NOP) */
105 if (of_prop_cmp(prop
->name
, "name") == 0 ||
106 of_prop_cmp(prop
->name
, "phandle") == 0 ||
107 of_prop_cmp(prop
->name
, "linux,phandle") == 0)
110 propn
= __of_prop_dup(prop
, GFP_KERNEL
);
116 return of_changeset_add_property(&ov
->cset
, target
, propn
);
119 return of_changeset_update_property(&ov
->cset
, target
, propn
);
122 static int of_overlay_apply_single_device_node(struct of_overlay
*ov
,
123 struct device_node
*target
, struct device_node
*child
)
126 struct device_node
*tchild
;
129 cname
= kbasename(child
->full_name
);
133 /* NOTE: Multiple mods of created nodes not supported */
134 tchild
= of_get_child_by_name(target
, cname
);
135 if (tchild
!= NULL
) {
136 /* apply overlay recursively */
137 ret
= of_overlay_apply_one(ov
, tchild
, child
);
140 /* create empty tree as a target */
141 tchild
= __of_node_dup(child
, "%s/%s", target
->full_name
, cname
);
145 /* point to parent */
146 tchild
->parent
= target
;
148 ret
= of_changeset_attach_node(&ov
->cset
, tchild
);
152 ret
= of_overlay_apply_one(ov
, tchild
, child
);
161 * Apply a single overlay node recursively.
163 * Note that the in case of an error the target node is left
164 * in a inconsistent state. Error recovery should be performed
165 * by using the changeset.
167 static int of_overlay_apply_one(struct of_overlay
*ov
,
168 struct device_node
*target
, const struct device_node
*overlay
)
170 struct device_node
*child
;
171 struct property
*prop
;
174 for_each_property_of_node(overlay
, prop
) {
175 ret
= of_overlay_apply_single_property(ov
, target
, prop
);
177 pr_err("Failed to apply prop @%s/%s\n",
178 target
->full_name
, prop
->name
);
183 for_each_child_of_node(overlay
, child
) {
184 ret
= of_overlay_apply_single_device_node(ov
, target
, child
);
186 pr_err("Failed to apply single node @%s/%s\n",
187 target
->full_name
, child
->name
);
197 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
198 * @ov: Overlay to apply
200 * Applies the overlays given, while handling all error conditions
201 * appropriately. Either the operation succeeds, or if it fails the
202 * live tree is reverted to the state before the attempt.
203 * Returns 0, or an error if the overlay attempt failed.
205 static int of_overlay_apply(struct of_overlay
*ov
)
209 /* first we apply the overlays atomically */
210 for (i
= 0; i
< ov
->count
; i
++) {
211 struct of_overlay_info
*ovinfo
= &ov
->ovinfo_tab
[i
];
213 err
= of_overlay_apply_one(ov
, ovinfo
->target
, ovinfo
->overlay
);
215 pr_err("apply failed '%s'\n", ovinfo
->target
->full_name
);
224 * Find the target node using a number of different strategies
225 * in order of preference
227 * "target" property containing the phandle of the target
228 * "target-path" property containing the path of the target
230 static struct device_node
*find_target_node(struct device_node
*info_node
)
236 /* first try to go by using the target as a phandle */
237 ret
= of_property_read_u32(info_node
, "target", &val
);
239 return of_find_node_by_phandle(val
);
241 /* now try to locate by path */
242 ret
= of_property_read_string(info_node
, "target-path", &path
);
244 return of_find_node_by_path(path
);
246 pr_err("Failed to find target for node %p (%s)\n",
247 info_node
, info_node
->name
);
253 * of_fill_overlay_info() - Fill an overlay info structure
254 * @ov Overlay to fill
255 * @info_node: Device node containing the overlay
256 * @ovinfo: Pointer to the overlay info structure to fill
258 * Fills an overlay info structure with the overlay information
259 * from a device node. This device node must have a target property
260 * which contains a phandle of the overlay target node, and an
261 * __overlay__ child node which has the overlay contents.
262 * Both ovinfo->target & ovinfo->overlay have their references taken.
264 * Returns 0 on success, or a negative error value.
266 static int of_fill_overlay_info(struct of_overlay
*ov
,
267 struct device_node
*info_node
, struct of_overlay_info
*ovinfo
)
269 ovinfo
->overlay
= of_get_child_by_name(info_node
, "__overlay__");
270 if (ovinfo
->overlay
== NULL
)
273 ovinfo
->target
= find_target_node(info_node
);
274 if (ovinfo
->target
== NULL
)
280 of_node_put(ovinfo
->target
);
281 of_node_put(ovinfo
->overlay
);
283 memset(ovinfo
, 0, sizeof(*ovinfo
));
288 * of_build_overlay_info() - Build an overlay info array
289 * @ov Overlay to build
290 * @tree: Device node containing all the overlays
292 * Helper function that given a tree containing overlay information,
293 * allocates and builds an overlay info array containing it, ready
294 * for use using of_overlay_apply.
296 * Returns 0 on success with the @cntp @ovinfop pointers valid,
297 * while on error a negative error value is returned.
299 static int of_build_overlay_info(struct of_overlay
*ov
,
300 struct device_node
*tree
)
302 struct device_node
*node
;
303 struct of_overlay_info
*ovinfo
;
306 /* worst case; every child is a node */
308 for_each_child_of_node(tree
, node
)
311 ovinfo
= kcalloc(cnt
, sizeof(*ovinfo
), GFP_KERNEL
);
316 for_each_child_of_node(tree
, node
) {
317 memset(&ovinfo
[cnt
], 0, sizeof(*ovinfo
));
318 err
= of_fill_overlay_info(ov
, node
, &ovinfo
[cnt
]);
323 /* if nothing filled, return error */
330 ov
->ovinfo_tab
= ovinfo
;
336 * of_free_overlay_info() - Free an overlay info array
337 * @ov Overlay to free the overlay info from
338 * @ovinfo_tab: Array of overlay_info's to free
340 * Releases the memory of a previously allocated ovinfo array
341 * by of_build_overlay_info.
342 * Returns 0, or an error if the arguments are bogus.
344 static int of_free_overlay_info(struct of_overlay
*ov
)
346 struct of_overlay_info
*ovinfo
;
349 /* do it in reverse */
350 for (i
= ov
->count
- 1; i
>= 0; i
--) {
351 ovinfo
= &ov
->ovinfo_tab
[i
];
353 of_node_put(ovinfo
->target
);
354 of_node_put(ovinfo
->overlay
);
356 kfree(ov
->ovinfo_tab
);
361 static LIST_HEAD(ov_list
);
362 static DEFINE_IDR(ov_idr
);
365 * of_overlay_create() - Create and apply an overlay
366 * @tree: Device node containing all the overlays
368 * Creates and applies an overlay while also keeping track
369 * of the overlay in a list. This list can be used to prevent
370 * illegal overlay removals.
372 * Returns the id of the created overlay, or a negative error number
374 int of_overlay_create(struct device_node
*tree
)
376 struct of_overlay
*ov
;
379 /* allocate the overlay structure */
380 ov
= kzalloc(sizeof(*ov
), GFP_KERNEL
);
385 INIT_LIST_HEAD(&ov
->node
);
387 of_changeset_init(&ov
->cset
);
389 mutex_lock(&of_mutex
);
391 id
= idr_alloc(&ov_idr
, ov
, 0, 0, GFP_KERNEL
);
394 goto err_destroy_trans
;
398 /* build the overlay info structures */
399 err
= of_build_overlay_info(ov
, tree
);
401 pr_err("of_build_overlay_info() failed for tree@%s\n",
406 err
= of_overlay_notify(ov
, OF_OVERLAY_PRE_APPLY
);
408 pr_err("%s: Pre-apply notifier failed (err=%d)\n",
413 /* apply the overlay */
414 err
= of_overlay_apply(ov
);
416 goto err_abort_trans
;
418 /* apply the changeset */
419 err
= __of_changeset_apply(&ov
->cset
);
421 goto err_revert_overlay
;
424 /* add to the tail of the overlay list */
425 list_add_tail(&ov
->node
, &ov_list
);
427 of_overlay_notify(ov
, OF_OVERLAY_POST_APPLY
);
429 mutex_unlock(&of_mutex
);
435 of_free_overlay_info(ov
);
437 idr_remove(&ov_idr
, ov
->id
);
439 of_changeset_destroy(&ov
->cset
);
441 mutex_unlock(&of_mutex
);
445 EXPORT_SYMBOL_GPL(of_overlay_create
);
447 /* check whether the given node, lies under the given tree */
448 static int overlay_subtree_check(struct device_node
*tree
,
449 struct device_node
*dn
)
451 struct device_node
*child
;
457 for_each_child_of_node(tree
, child
) {
458 if (overlay_subtree_check(child
, dn
)) {
467 /* check whether this overlay is the topmost */
468 static int overlay_is_topmost(struct of_overlay
*ov
, struct device_node
*dn
)
470 struct of_overlay
*ovt
;
471 struct of_changeset_entry
*ce
;
473 list_for_each_entry_reverse(ovt
, &ov_list
, node
) {
474 /* if we hit ourselves, we're done */
478 /* check against each subtree affected by this overlay */
479 list_for_each_entry(ce
, &ovt
->cset
.entries
, node
) {
480 if (overlay_subtree_check(ce
->np
, dn
)) {
481 pr_err("%s: #%d clashes #%d @%s\n",
482 __func__
, ov
->id
, ovt
->id
,
489 /* overlay is topmost */
494 * We can safely remove the overlay only if it's the top-most one.
495 * Newly applied overlays are inserted at the tail of the overlay list,
496 * so a top most overlay is the one that is closest to the tail.
498 * The topmost check is done by exploiting this property. For each
499 * affected device node in the log list we check if this overlay is
500 * the one closest to the tail. If another overlay has affected this
501 * device node and is closest to the tail, then removal is not permited.
503 static int overlay_removal_is_ok(struct of_overlay
*ov
)
505 struct of_changeset_entry
*ce
;
507 list_for_each_entry(ce
, &ov
->cset
.entries
, node
) {
508 if (!overlay_is_topmost(ov
, ce
->np
)) {
509 pr_err("overlay #%d is not topmost\n", ov
->id
);
518 * of_overlay_destroy() - Removes an overlay
519 * @id: Overlay id number returned by a previous call to of_overlay_create
521 * Removes an overlay if it is permissible.
523 * Returns 0 on success, or a negative error number
525 int of_overlay_destroy(int id
)
527 struct of_overlay
*ov
;
530 mutex_lock(&of_mutex
);
532 ov
= idr_find(&ov_idr
, id
);
535 pr_err("destroy: Could not find overlay #%d\n", id
);
539 /* check whether the overlay is safe to remove */
540 if (!overlay_removal_is_ok(ov
)) {
545 of_overlay_notify(ov
, OF_OVERLAY_PRE_REMOVE
);
547 __of_changeset_revert(&ov
->cset
);
548 of_overlay_notify(ov
, OF_OVERLAY_POST_REMOVE
);
549 of_free_overlay_info(ov
);
550 idr_remove(&ov_idr
, id
);
551 of_changeset_destroy(&ov
->cset
);
557 mutex_unlock(&of_mutex
);
561 EXPORT_SYMBOL_GPL(of_overlay_destroy
);
564 * of_overlay_destroy_all() - Removes all overlays from the system
566 * Removes all overlays from the system in the correct order.
568 * Returns 0 on success, or a negative error number
570 int of_overlay_destroy_all(void)
572 struct of_overlay
*ov
, *ovn
;
574 mutex_lock(&of_mutex
);
576 /* the tail of list is guaranteed to be safe to remove */
577 list_for_each_entry_safe_reverse(ov
, ovn
, &ov_list
, node
) {
579 __of_changeset_revert(&ov
->cset
);
580 of_free_overlay_info(ov
);
581 idr_remove(&ov_idr
, ov
->id
);
585 mutex_unlock(&of_mutex
);
589 EXPORT_SYMBOL_GPL(of_overlay_destroy_all
);