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 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
23 #include "of_private.h"
26 * struct of_overlay_info - Holds a single overlay info
27 * @target: target of the overlay operation
28 * @overlay: pointer to the overlay contents node
30 * Holds a single overlay state, including all the overlay logs &
33 struct of_overlay_info
{
34 struct device_node
*target
;
35 struct device_node
*overlay
;
39 * struct of_overlay - Holds a complete overlay transaction
40 * @node: List on which we are located
41 * @count: Count of ovinfo structures
42 * @ovinfo_tab: Overlay info table (count sized)
43 * @cset: Changeset to be used
45 * Holds a complete overlay transaction
49 struct list_head node
;
51 struct of_overlay_info
*ovinfo_tab
;
52 struct of_changeset cset
;
55 static int of_overlay_apply_one(struct of_overlay
*ov
,
56 struct device_node
*target
, const struct device_node
*overlay
);
58 static int of_overlay_apply_single_property(struct of_overlay
*ov
,
59 struct device_node
*target
, struct property
*prop
)
61 struct property
*propn
, *tprop
;
63 /* NOTE: Multiple changes of single properties not supported */
64 tprop
= of_find_property(target
, prop
->name
, NULL
);
66 /* special properties are not meant to be updated (silent NOP) */
67 if (of_prop_cmp(prop
->name
, "name") == 0 ||
68 of_prop_cmp(prop
->name
, "phandle") == 0 ||
69 of_prop_cmp(prop
->name
, "linux,phandle") == 0)
72 propn
= __of_prop_dup(prop
, GFP_KERNEL
);
78 return of_changeset_add_property(&ov
->cset
, target
, propn
);
81 return of_changeset_update_property(&ov
->cset
, target
, propn
);
84 static int of_overlay_apply_single_device_node(struct of_overlay
*ov
,
85 struct device_node
*target
, struct device_node
*child
)
88 struct device_node
*tchild
, *grandchild
;
91 cname
= kbasename(child
->full_name
);
95 /* NOTE: Multiple mods of created nodes not supported */
96 tchild
= of_get_child_by_name(target
, cname
);
98 /* apply overlay recursively */
99 ret
= of_overlay_apply_one(ov
, tchild
, child
);
102 /* create empty tree as a target */
103 tchild
= __of_node_dup(child
, "%s/%s", target
->full_name
, cname
);
107 /* point to parent */
108 tchild
->parent
= target
;
110 ret
= of_changeset_attach_node(&ov
->cset
, tchild
);
114 ret
= of_overlay_apply_one(ov
, tchild
, child
);
123 * Apply a single overlay node recursively.
125 * Note that the in case of an error the target node is left
126 * in a inconsistent state. Error recovery should be performed
127 * by using the changeset.
129 static int of_overlay_apply_one(struct of_overlay
*ov
,
130 struct device_node
*target
, const struct device_node
*overlay
)
132 struct device_node
*child
;
133 struct property
*prop
;
136 for_each_property_of_node(overlay
, prop
) {
137 ret
= of_overlay_apply_single_property(ov
, target
, prop
);
139 pr_err("%s: Failed to apply prop @%s/%s\n",
140 __func__
, target
->full_name
, prop
->name
);
145 for_each_child_of_node(overlay
, child
) {
146 ret
= of_overlay_apply_single_device_node(ov
, target
, child
);
148 pr_err("%s: Failed to apply single node @%s/%s\n",
149 __func__
, target
->full_name
,
159 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
160 * @ov: Overlay to apply
162 * Applies the overlays given, while handling all error conditions
163 * appropriately. Either the operation succeeds, or if it fails the
164 * live tree is reverted to the state before the attempt.
165 * Returns 0, or an error if the overlay attempt failed.
167 static int of_overlay_apply(struct of_overlay
*ov
)
171 /* first we apply the overlays atomically */
172 for (i
= 0; i
< ov
->count
; i
++) {
173 struct of_overlay_info
*ovinfo
= &ov
->ovinfo_tab
[i
];
175 err
= of_overlay_apply_one(ov
, ovinfo
->target
, ovinfo
->overlay
);
177 pr_err("%s: overlay failed '%s'\n",
178 __func__
, ovinfo
->target
->full_name
);
187 * Find the target node using a number of different strategies
188 * in order of preference
190 * "target" property containing the phandle of the target
191 * "target-path" property containing the path of the target
193 static struct device_node
*find_target_node(struct device_node
*info_node
)
199 /* first try to go by using the target as a phandle */
200 ret
= of_property_read_u32(info_node
, "target", &val
);
202 return of_find_node_by_phandle(val
);
204 /* now try to locate by path */
205 ret
= of_property_read_string(info_node
, "target-path", &path
);
207 return of_find_node_by_path(path
);
209 pr_err("%s: Failed to find target for node %p (%s)\n", __func__
,
210 info_node
, info_node
->name
);
216 * of_fill_overlay_info() - Fill an overlay info structure
217 * @ov Overlay to fill
218 * @info_node: Device node containing the overlay
219 * @ovinfo: Pointer to the overlay info structure to fill
221 * Fills an overlay info structure with the overlay information
222 * from a device node. This device node must have a target property
223 * which contains a phandle of the overlay target node, and an
224 * __overlay__ child node which has the overlay contents.
225 * Both ovinfo->target & ovinfo->overlay have their references taken.
227 * Returns 0 on success, or a negative error value.
229 static int of_fill_overlay_info(struct of_overlay
*ov
,
230 struct device_node
*info_node
, struct of_overlay_info
*ovinfo
)
232 ovinfo
->overlay
= of_get_child_by_name(info_node
, "__overlay__");
233 if (ovinfo
->overlay
== NULL
)
236 ovinfo
->target
= find_target_node(info_node
);
237 if (ovinfo
->target
== NULL
)
243 of_node_put(ovinfo
->target
);
244 of_node_put(ovinfo
->overlay
);
246 memset(ovinfo
, 0, sizeof(*ovinfo
));
251 * of_build_overlay_info() - Build an overlay info array
252 * @ov Overlay to build
253 * @tree: Device node containing all the overlays
255 * Helper function that given a tree containing overlay information,
256 * allocates and builds an overlay info array containing it, ready
257 * for use using of_overlay_apply.
259 * Returns 0 on success with the @cntp @ovinfop pointers valid,
260 * while on error a negative error value is returned.
262 static int of_build_overlay_info(struct of_overlay
*ov
,
263 struct device_node
*tree
)
265 struct device_node
*node
;
266 struct of_overlay_info
*ovinfo
;
269 /* worst case; every child is a node */
271 for_each_child_of_node(tree
, node
)
274 ovinfo
= kcalloc(cnt
, sizeof(*ovinfo
), GFP_KERNEL
);
279 for_each_child_of_node(tree
, node
) {
280 memset(&ovinfo
[cnt
], 0, sizeof(*ovinfo
));
281 err
= of_fill_overlay_info(ov
, node
, &ovinfo
[cnt
]);
286 /* if nothing filled, return error */
293 ov
->ovinfo_tab
= ovinfo
;
299 * of_free_overlay_info() - Free an overlay info array
300 * @ov Overlay to free the overlay info from
301 * @ovinfo_tab: Array of overlay_info's to free
303 * Releases the memory of a previously allocated ovinfo array
304 * by of_build_overlay_info.
305 * Returns 0, or an error if the arguments are bogus.
307 static int of_free_overlay_info(struct of_overlay
*ov
)
309 struct of_overlay_info
*ovinfo
;
312 /* do it in reverse */
313 for (i
= ov
->count
- 1; i
>= 0; i
--) {
314 ovinfo
= &ov
->ovinfo_tab
[i
];
316 of_node_put(ovinfo
->target
);
317 of_node_put(ovinfo
->overlay
);
319 kfree(ov
->ovinfo_tab
);
324 static LIST_HEAD(ov_list
);
325 static DEFINE_IDR(ov_idr
);
328 * of_overlay_create() - Create and apply an overlay
329 * @tree: Device node containing all the overlays
331 * Creates and applies an overlay while also keeping track
332 * of the overlay in a list. This list can be used to prevent
333 * illegal overlay removals.
335 * Returns the id of the created overlay, or an negative error number
337 int of_overlay_create(struct device_node
*tree
)
339 struct of_overlay
*ov
;
342 /* allocate the overlay structure */
343 ov
= kzalloc(sizeof(*ov
), GFP_KERNEL
);
348 INIT_LIST_HEAD(&ov
->node
);
350 of_changeset_init(&ov
->cset
);
352 mutex_lock(&of_mutex
);
354 id
= idr_alloc(&ov_idr
, ov
, 0, 0, GFP_KERNEL
);
356 pr_err("%s: idr_alloc() failed for tree@%s\n",
357 __func__
, tree
->full_name
);
359 goto err_destroy_trans
;
363 /* build the overlay info structures */
364 err
= of_build_overlay_info(ov
, tree
);
366 pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
367 __func__
, tree
->full_name
);
371 /* apply the overlay */
372 err
= of_overlay_apply(ov
);
374 pr_err("%s: of_overlay_apply() failed for tree@%s\n",
375 __func__
, tree
->full_name
);
376 goto err_abort_trans
;
379 /* apply the changeset */
380 err
= of_changeset_apply(&ov
->cset
);
382 pr_err("%s: of_changeset_apply() failed for tree@%s\n",
383 __func__
, tree
->full_name
);
384 goto err_revert_overlay
;
387 /* add to the tail of the overlay list */
388 list_add_tail(&ov
->node
, &ov_list
);
390 mutex_unlock(&of_mutex
);
396 of_free_overlay_info(ov
);
398 idr_remove(&ov_idr
, ov
->id
);
400 of_changeset_destroy(&ov
->cset
);
402 mutex_unlock(&of_mutex
);
406 EXPORT_SYMBOL_GPL(of_overlay_create
);
408 /* check whether the given node, lies under the given tree */
409 static int overlay_subtree_check(struct device_node
*tree
,
410 struct device_node
*dn
)
412 struct device_node
*child
;
418 for_each_child_of_node(tree
, child
) {
419 if (overlay_subtree_check(child
, dn
))
426 /* check whether this overlay is the topmost */
427 static int overlay_is_topmost(struct of_overlay
*ov
, struct device_node
*dn
)
429 struct of_overlay
*ovt
;
430 struct of_changeset_entry
*ce
;
432 list_for_each_entry_reverse(ovt
, &ov_list
, node
) {
433 /* if we hit ourselves, we're done */
437 /* check against each subtree affected by this overlay */
438 list_for_each_entry(ce
, &ovt
->cset
.entries
, node
) {
439 if (overlay_subtree_check(ce
->np
, dn
)) {
440 pr_err("%s: #%d clashes #%d @%s\n",
441 __func__
, ov
->id
, ovt
->id
,
448 /* overlay is topmost */
453 * We can safely remove the overlay only if it's the top-most one.
454 * Newly applied overlays are inserted at the tail of the overlay list,
455 * so a top most overlay is the one that is closest to the tail.
457 * The topmost check is done by exploiting this property. For each
458 * affected device node in the log list we check if this overlay is
459 * the one closest to the tail. If another overlay has affected this
460 * device node and is closest to the tail, then removal is not permited.
462 static int overlay_removal_is_ok(struct of_overlay
*ov
)
464 struct of_changeset_entry
*ce
;
466 list_for_each_entry(ce
, &ov
->cset
.entries
, node
) {
467 if (!overlay_is_topmost(ov
, ce
->np
)) {
468 pr_err("%s: overlay #%d is not topmost\n",
478 * of_overlay_destroy() - Removes an overlay
479 * @id: Overlay id number returned by a previous call to of_overlay_create
481 * Removes an overlay if it is permissible.
483 * Returns 0 on success, or an negative error number
485 int of_overlay_destroy(int id
)
487 struct of_overlay
*ov
;
490 mutex_lock(&of_mutex
);
492 ov
= idr_find(&ov_idr
, id
);
495 pr_err("%s: Could not find overlay #%d\n",
500 /* check whether the overlay is safe to remove */
501 if (!overlay_removal_is_ok(ov
)) {
503 pr_err("%s: removal check failed for overlay #%d\n",
510 of_changeset_revert(&ov
->cset
);
511 of_free_overlay_info(ov
);
512 idr_remove(&ov_idr
, id
);
513 of_changeset_destroy(&ov
->cset
);
519 mutex_unlock(&of_mutex
);
523 EXPORT_SYMBOL_GPL(of_overlay_destroy
);
526 * of_overlay_destroy_all() - Removes all overlays from the system
528 * Removes all overlays from the system in the correct order.
530 * Returns 0 on success, or an negative error number
532 int of_overlay_destroy_all(void)
534 struct of_overlay
*ov
, *ovn
;
536 mutex_lock(&of_mutex
);
538 /* the tail of list is guaranteed to be safe to remove */
539 list_for_each_entry_safe_reverse(ov
, ovn
, &ov_list
, node
) {
541 of_changeset_revert(&ov
->cset
);
542 of_free_overlay_info(ov
);
543 idr_remove(&ov_idr
, ov
->id
);
547 mutex_unlock(&of_mutex
);
551 EXPORT_SYMBOL_GPL(of_overlay_destroy_all
);
This page took 0.046025 seconds and 5 git commands to generate.