2 * Functions for working with device tree overlays
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>
22 #include <linux/idr.h>
24 #include "of_private.h"
27 * struct of_overlay_info - Holds a single overlay info
28 * @target: target of the overlay operation
29 * @overlay: pointer to the overlay contents node
31 * Holds a single overlay state, including all the overlay logs &
34 struct of_overlay_info {
35 struct device_node *target;
36 struct device_node *overlay;
40 * struct of_overlay - Holds a complete overlay transaction
41 * @node: List on which we are located
42 * @count: Count of ovinfo structures
43 * @ovinfo_tab: Overlay info table (count sized)
44 * @cset: Changeset to be used
46 * Holds a complete overlay transaction
50 struct list_head node;
52 struct of_overlay_info *ovinfo_tab;
53 struct of_changeset cset;
56 static int of_overlay_apply_one(struct of_overlay *ov,
57 struct device_node *target, const struct device_node *overlay);
59 static int of_overlay_apply_single_property(struct of_overlay *ov,
60 struct device_node *target, struct property *prop)
62 struct property *propn, *tprop;
64 /* NOTE: Multiple changes of single properties not supported */
65 tprop = of_find_property(target, prop->name, NULL);
67 /* special properties are not meant to be updated (silent NOP) */
68 if (of_prop_cmp(prop->name, "name") == 0 ||
69 of_prop_cmp(prop->name, "phandle") == 0 ||
70 of_prop_cmp(prop->name, "linux,phandle") == 0)
73 propn = __of_prop_dup(prop, GFP_KERNEL);
79 return of_changeset_add_property(&ov->cset, target, propn);
82 return of_changeset_update_property(&ov->cset, target, propn);
85 static int of_overlay_apply_single_device_node(struct of_overlay *ov,
86 struct device_node *target, struct device_node *child)
89 struct device_node *tchild;
92 cname = kbasename(child->full_name);
96 /* NOTE: Multiple mods of created nodes not supported */
97 tchild = of_get_child_by_name(target, cname);
99 /* apply overlay recursively */
100 ret = of_overlay_apply_one(ov, tchild, child);
103 /* create empty tree as a target */
104 tchild = __of_node_dup(child, "%s/%s", target->full_name, cname);
108 /* point to parent */
109 tchild->parent = target;
111 ret = of_changeset_attach_node(&ov->cset, tchild);
115 ret = of_overlay_apply_one(ov, tchild, child);
124 * Apply a single overlay node recursively.
126 * Note that the in case of an error the target node is left
127 * in a inconsistent state. Error recovery should be performed
128 * by using the changeset.
130 static int of_overlay_apply_one(struct of_overlay *ov,
131 struct device_node *target, const struct device_node *overlay)
133 struct device_node *child;
134 struct property *prop;
137 for_each_property_of_node(overlay, prop) {
138 ret = of_overlay_apply_single_property(ov, target, prop);
140 pr_err("%s: Failed to apply prop @%s/%s\n",
141 __func__, target->full_name, prop->name);
146 for_each_child_of_node(overlay, child) {
147 ret = of_overlay_apply_single_device_node(ov, target, child);
149 pr_err("%s: Failed to apply single node @%s/%s\n",
150 __func__, target->full_name,
161 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
162 * @ov: Overlay to apply
164 * Applies the overlays given, while handling all error conditions
165 * appropriately. Either the operation succeeds, or if it fails the
166 * live tree is reverted to the state before the attempt.
167 * Returns 0, or an error if the overlay attempt failed.
169 static int of_overlay_apply(struct of_overlay *ov)
173 /* first we apply the overlays atomically */
174 for (i = 0; i < ov->count; i++) {
175 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
177 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
179 pr_err("%s: overlay failed '%s'\n",
180 __func__, ovinfo->target->full_name);
189 * Find the target node using a number of different strategies
190 * in order of preference
192 * "target" property containing the phandle of the target
193 * "target-path" property containing the path of the target
195 static struct device_node *find_target_node(struct device_node *info_node)
201 /* first try to go by using the target as a phandle */
202 ret = of_property_read_u32(info_node, "target", &val);
204 return of_find_node_by_phandle(val);
206 /* now try to locate by path */
207 ret = of_property_read_string(info_node, "target-path", &path);
209 return of_find_node_by_path(path);
211 pr_err("%s: Failed to find target for node %p (%s)\n", __func__,
212 info_node, info_node->name);
218 * of_fill_overlay_info() - Fill an overlay info structure
219 * @ov Overlay to fill
220 * @info_node: Device node containing the overlay
221 * @ovinfo: Pointer to the overlay info structure to fill
223 * Fills an overlay info structure with the overlay information
224 * from a device node. This device node must have a target property
225 * which contains a phandle of the overlay target node, and an
226 * __overlay__ child node which has the overlay contents.
227 * Both ovinfo->target & ovinfo->overlay have their references taken.
229 * Returns 0 on success, or a negative error value.
231 static int of_fill_overlay_info(struct of_overlay *ov,
232 struct device_node *info_node, struct of_overlay_info *ovinfo)
234 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
235 if (ovinfo->overlay == NULL)
238 ovinfo->target = find_target_node(info_node);
239 if (ovinfo->target == NULL)
245 of_node_put(ovinfo->target);
246 of_node_put(ovinfo->overlay);
248 memset(ovinfo, 0, sizeof(*ovinfo));
253 * of_build_overlay_info() - Build an overlay info array
254 * @ov Overlay to build
255 * @tree: Device node containing all the overlays
257 * Helper function that given a tree containing overlay information,
258 * allocates and builds an overlay info array containing it, ready
259 * for use using of_overlay_apply.
261 * Returns 0 on success with the @cntp @ovinfop pointers valid,
262 * while on error a negative error value is returned.
264 static int of_build_overlay_info(struct of_overlay *ov,
265 struct device_node *tree)
267 struct device_node *node;
268 struct of_overlay_info *ovinfo;
271 /* worst case; every child is a node */
273 for_each_child_of_node(tree, node)
276 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
281 for_each_child_of_node(tree, node) {
282 memset(&ovinfo[cnt], 0, sizeof(*ovinfo));
283 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
288 /* if nothing filled, return error */
295 ov->ovinfo_tab = ovinfo;
301 * of_free_overlay_info() - Free an overlay info array
302 * @ov Overlay to free the overlay info from
303 * @ovinfo_tab: Array of overlay_info's to free
305 * Releases the memory of a previously allocated ovinfo array
306 * by of_build_overlay_info.
307 * Returns 0, or an error if the arguments are bogus.
309 static int of_free_overlay_info(struct of_overlay *ov)
311 struct of_overlay_info *ovinfo;
314 /* do it in reverse */
315 for (i = ov->count - 1; i >= 0; i--) {
316 ovinfo = &ov->ovinfo_tab[i];
318 of_node_put(ovinfo->target);
319 of_node_put(ovinfo->overlay);
321 kfree(ov->ovinfo_tab);
326 static LIST_HEAD(ov_list);
327 static DEFINE_IDR(ov_idr);
330 * of_overlay_create() - Create and apply an overlay
331 * @tree: Device node containing all the overlays
333 * Creates and applies an overlay while also keeping track
334 * of the overlay in a list. This list can be used to prevent
335 * illegal overlay removals.
337 * Returns the id of the created overlay, or a negative error number
339 int of_overlay_create(struct device_node *tree)
341 struct of_overlay *ov;
344 /* allocate the overlay structure */
345 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
350 INIT_LIST_HEAD(&ov->node);
352 of_changeset_init(&ov->cset);
354 mutex_lock(&of_mutex);
356 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
358 pr_err("%s: idr_alloc() failed for tree@%s\n",
359 __func__, tree->full_name);
361 goto err_destroy_trans;
365 /* build the overlay info structures */
366 err = of_build_overlay_info(ov, tree);
368 pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
369 __func__, tree->full_name);
373 /* apply the overlay */
374 err = of_overlay_apply(ov);
376 pr_err("%s: of_overlay_apply() failed for tree@%s\n",
377 __func__, tree->full_name);
378 goto err_abort_trans;
381 /* apply the changeset */
382 err = __of_changeset_apply(&ov->cset);
384 pr_err("%s: __of_changeset_apply() failed for tree@%s\n",
385 __func__, tree->full_name);
386 goto err_revert_overlay;
389 /* add to the tail of the overlay list */
390 list_add_tail(&ov->node, &ov_list);
392 mutex_unlock(&of_mutex);
398 of_free_overlay_info(ov);
400 idr_remove(&ov_idr, ov->id);
402 of_changeset_destroy(&ov->cset);
404 mutex_unlock(&of_mutex);
408 EXPORT_SYMBOL_GPL(of_overlay_create);
410 /* check whether the given node, lies under the given tree */
411 static int overlay_subtree_check(struct device_node *tree,
412 struct device_node *dn)
414 struct device_node *child;
420 for_each_child_of_node(tree, child) {
421 if (overlay_subtree_check(child, dn)) {
430 /* check whether this overlay is the topmost */
431 static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
433 struct of_overlay *ovt;
434 struct of_changeset_entry *ce;
436 list_for_each_entry_reverse(ovt, &ov_list, node) {
437 /* if we hit ourselves, we're done */
441 /* check against each subtree affected by this overlay */
442 list_for_each_entry(ce, &ovt->cset.entries, node) {
443 if (overlay_subtree_check(ce->np, dn)) {
444 pr_err("%s: #%d clashes #%d @%s\n",
445 __func__, ov->id, ovt->id,
452 /* overlay is topmost */
457 * We can safely remove the overlay only if it's the top-most one.
458 * Newly applied overlays are inserted at the tail of the overlay list,
459 * so a top most overlay is the one that is closest to the tail.
461 * The topmost check is done by exploiting this property. For each
462 * affected device node in the log list we check if this overlay is
463 * the one closest to the tail. If another overlay has affected this
464 * device node and is closest to the tail, then removal is not permited.
466 static int overlay_removal_is_ok(struct of_overlay *ov)
468 struct of_changeset_entry *ce;
470 list_for_each_entry(ce, &ov->cset.entries, node) {
471 if (!overlay_is_topmost(ov, ce->np)) {
472 pr_err("%s: overlay #%d is not topmost\n",
482 * of_overlay_destroy() - Removes an overlay
483 * @id: Overlay id number returned by a previous call to of_overlay_create
485 * Removes an overlay if it is permissible.
487 * Returns 0 on success, or a negative error number
489 int of_overlay_destroy(int id)
491 struct of_overlay *ov;
494 mutex_lock(&of_mutex);
496 ov = idr_find(&ov_idr, id);
499 pr_err("%s: Could not find overlay #%d\n",
504 /* check whether the overlay is safe to remove */
505 if (!overlay_removal_is_ok(ov)) {
507 pr_err("%s: removal check failed for overlay #%d\n",
514 __of_changeset_revert(&ov->cset);
515 of_free_overlay_info(ov);
516 idr_remove(&ov_idr, id);
517 of_changeset_destroy(&ov->cset);
523 mutex_unlock(&of_mutex);
527 EXPORT_SYMBOL_GPL(of_overlay_destroy);
530 * of_overlay_destroy_all() - Removes all overlays from the system
532 * Removes all overlays from the system in the correct order.
534 * Returns 0 on success, or a negative error number
536 int of_overlay_destroy_all(void)
538 struct of_overlay *ov, *ovn;
540 mutex_lock(&of_mutex);
542 /* the tail of list is guaranteed to be safe to remove */
543 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
545 __of_changeset_revert(&ov->cset);
546 of_free_overlay_info(ov);
547 idr_remove(&ov_idr, ov->id);
551 mutex_unlock(&of_mutex);
555 EXPORT_SYMBOL_GPL(of_overlay_destroy_all);