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);
118 /* The properties are already copied, now do the child nodes */
119 for_each_child_of_node(child, grandchild) {
120 ret = of_overlay_apply_single_device_node(ov, tchild, grandchild);
122 pr_err("%s: Failed to apply single node @%s/%s\n",
123 __func__, tchild->full_name,
134 * Apply a single overlay node recursively.
136 * Note that the in case of an error the target node is left
137 * in a inconsistent state. Error recovery should be performed
138 * by using the changeset.
140 static int of_overlay_apply_one(struct of_overlay *ov,
141 struct device_node *target, const struct device_node *overlay)
143 struct device_node *child;
144 struct property *prop;
147 for_each_property_of_node(overlay, prop) {
148 ret = of_overlay_apply_single_property(ov, target, prop);
150 pr_err("%s: Failed to apply prop @%s/%s\n",
151 __func__, target->full_name, prop->name);
156 for_each_child_of_node(overlay, child) {
157 ret = of_overlay_apply_single_device_node(ov, target, child);
159 pr_err("%s: Failed to apply single node @%s/%s\n",
160 __func__, target->full_name,
170 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
171 * @ov: Overlay to apply
173 * Applies the overlays given, while handling all error conditions
174 * appropriately. Either the operation succeeds, or if it fails the
175 * live tree is reverted to the state before the attempt.
176 * Returns 0, or an error if the overlay attempt failed.
178 static int of_overlay_apply(struct of_overlay *ov)
182 /* first we apply the overlays atomically */
183 for (i = 0; i < ov->count; i++) {
184 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
186 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
188 pr_err("%s: overlay failed '%s'\n",
189 __func__, ovinfo->target->full_name);
198 * Find the target node using a number of different strategies
199 * in order of preference
201 * "target" property containing the phandle of the target
202 * "target-path" property containing the path of the target
204 static struct device_node *find_target_node(struct device_node *info_node)
210 /* first try to go by using the target as a phandle */
211 ret = of_property_read_u32(info_node, "target", &val);
213 return of_find_node_by_phandle(val);
215 /* now try to locate by path */
216 ret = of_property_read_string(info_node, "target-path", &path);
218 return of_find_node_by_path(path);
220 pr_err("%s: Failed to find target for node %p (%s)\n", __func__,
221 info_node, info_node->name);
227 * of_fill_overlay_info() - Fill an overlay info structure
228 * @ov Overlay to fill
229 * @info_node: Device node containing the overlay
230 * @ovinfo: Pointer to the overlay info structure to fill
232 * Fills an overlay info structure with the overlay information
233 * from a device node. This device node must have a target property
234 * which contains a phandle of the overlay target node, and an
235 * __overlay__ child node which has the overlay contents.
236 * Both ovinfo->target & ovinfo->overlay have their references taken.
238 * Returns 0 on success, or a negative error value.
240 static int of_fill_overlay_info(struct of_overlay *ov,
241 struct device_node *info_node, struct of_overlay_info *ovinfo)
243 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
244 if (ovinfo->overlay == NULL)
247 ovinfo->target = find_target_node(info_node);
248 if (ovinfo->target == NULL)
254 of_node_put(ovinfo->target);
255 of_node_put(ovinfo->overlay);
257 memset(ovinfo, 0, sizeof(*ovinfo));
262 * of_build_overlay_info() - Build an overlay info array
263 * @ov Overlay to build
264 * @tree: Device node containing all the overlays
266 * Helper function that given a tree containing overlay information,
267 * allocates and builds an overlay info array containing it, ready
268 * for use using of_overlay_apply.
270 * Returns 0 on success with the @cntp @ovinfop pointers valid,
271 * while on error a negative error value is returned.
273 static int of_build_overlay_info(struct of_overlay *ov,
274 struct device_node *tree)
276 struct device_node *node;
277 struct of_overlay_info *ovinfo;
280 /* worst case; every child is a node */
282 for_each_child_of_node(tree, node)
285 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
290 for_each_child_of_node(tree, node) {
291 memset(&ovinfo[cnt], 0, sizeof(*ovinfo));
292 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
297 /* if nothing filled, return error */
304 ov->ovinfo_tab = ovinfo;
310 * of_free_overlay_info() - Free an overlay info array
311 * @ov Overlay to free the overlay info from
312 * @ovinfo_tab: Array of overlay_info's to free
314 * Releases the memory of a previously allocated ovinfo array
315 * by of_build_overlay_info.
316 * Returns 0, or an error if the arguments are bogus.
318 static int of_free_overlay_info(struct of_overlay *ov)
320 struct of_overlay_info *ovinfo;
323 /* do it in reverse */
324 for (i = ov->count - 1; i >= 0; i--) {
325 ovinfo = &ov->ovinfo_tab[i];
327 of_node_put(ovinfo->target);
328 of_node_put(ovinfo->overlay);
330 kfree(ov->ovinfo_tab);
335 static LIST_HEAD(ov_list);
336 static DEFINE_IDR(ov_idr);
339 * of_overlay_create() - Create and apply an overlay
340 * @tree: Device node containing all the overlays
342 * Creates and applies an overlay while also keeping track
343 * of the overlay in a list. This list can be used to prevent
344 * illegal overlay removals.
346 * Returns the id of the created overlay, or an negative error number
348 int of_overlay_create(struct device_node *tree)
350 struct of_overlay *ov;
353 /* allocate the overlay structure */
354 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
359 INIT_LIST_HEAD(&ov->node);
361 of_changeset_init(&ov->cset);
363 mutex_lock(&of_mutex);
365 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
367 pr_err("%s: idr_alloc() failed for tree@%s\n",
368 __func__, tree->full_name);
370 goto err_destroy_trans;
374 /* build the overlay info structures */
375 err = of_build_overlay_info(ov, tree);
377 pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
378 __func__, tree->full_name);
382 /* apply the overlay */
383 err = of_overlay_apply(ov);
385 pr_err("%s: of_overlay_apply() failed for tree@%s\n",
386 __func__, tree->full_name);
387 goto err_abort_trans;
390 /* apply the changeset */
391 err = of_changeset_apply(&ov->cset);
393 pr_err("%s: of_changeset_apply() failed for tree@%s\n",
394 __func__, tree->full_name);
395 goto err_revert_overlay;
398 /* add to the tail of the overlay list */
399 list_add_tail(&ov->node, &ov_list);
401 mutex_unlock(&of_mutex);
407 of_free_overlay_info(ov);
409 idr_remove(&ov_idr, ov->id);
411 of_changeset_destroy(&ov->cset);
413 mutex_unlock(&of_mutex);
417 EXPORT_SYMBOL_GPL(of_overlay_create);
419 /* check whether the given node, lies under the given tree */
420 static int overlay_subtree_check(struct device_node *tree,
421 struct device_node *dn)
423 struct device_node *child;
429 for_each_child_of_node(tree, child) {
430 if (overlay_subtree_check(child, dn))
437 /* check whether this overlay is the topmost */
438 static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
440 struct of_overlay *ovt;
441 struct of_changeset_entry *ce;
443 list_for_each_entry_reverse(ovt, &ov_list, node) {
444 /* if we hit ourselves, we're done */
448 /* check against each subtree affected by this overlay */
449 list_for_each_entry(ce, &ovt->cset.entries, node) {
450 if (overlay_subtree_check(ce->np, dn)) {
451 pr_err("%s: #%d clashes #%d @%s\n",
452 __func__, ov->id, ovt->id,
459 /* overlay is topmost */
464 * We can safely remove the overlay only if it's the top-most one.
465 * Newly applied overlays are inserted at the tail of the overlay list,
466 * so a top most overlay is the one that is closest to the tail.
468 * The topmost check is done by exploiting this property. For each
469 * affected device node in the log list we check if this overlay is
470 * the one closest to the tail. If another overlay has affected this
471 * device node and is closest to the tail, then removal is not permited.
473 static int overlay_removal_is_ok(struct of_overlay *ov)
475 struct of_changeset_entry *ce;
477 list_for_each_entry(ce, &ov->cset.entries, node) {
478 if (!overlay_is_topmost(ov, ce->np)) {
479 pr_err("%s: overlay #%d is not topmost\n",
489 * of_overlay_destroy() - Removes an overlay
490 * @id: Overlay id number returned by a previous call to of_overlay_create
492 * Removes an overlay if it is permissible.
494 * Returns 0 on success, or an negative error number
496 int of_overlay_destroy(int id)
498 struct of_overlay *ov;
501 mutex_lock(&of_mutex);
503 ov = idr_find(&ov_idr, id);
506 pr_err("%s: Could not find overlay #%d\n",
511 /* check whether the overlay is safe to remove */
512 if (!overlay_removal_is_ok(ov)) {
514 pr_err("%s: removal check failed for overlay #%d\n",
521 of_changeset_revert(&ov->cset);
522 of_free_overlay_info(ov);
523 idr_remove(&ov_idr, id);
524 of_changeset_destroy(&ov->cset);
530 mutex_unlock(&of_mutex);
534 EXPORT_SYMBOL_GPL(of_overlay_destroy);
537 * of_overlay_destroy_all() - Removes all overlays from the system
539 * Removes all overlays from the system in the correct order.
541 * Returns 0 on success, or an negative error number
543 int of_overlay_destroy_all(void)
545 struct of_overlay *ov, *ovn;
547 mutex_lock(&of_mutex);
549 /* the tail of list is guaranteed to be safe to remove */
550 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
552 of_changeset_revert(&ov->cset);
553 of_free_overlay_info(ov);
554 idr_remove(&ov_idr, ov->id);
558 mutex_unlock(&of_mutex);
562 EXPORT_SYMBOL_GPL(of_overlay_destroy_all);