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/slab.h>
22 #include <linux/err.h>
23 #include <linux/idr.h>
25 #include "of_private.h"
28 * struct of_overlay_info - Holds a single overlay info
29 * @target: target of the overlay operation
30 * @overlay: pointer to the overlay contents node
32 * Holds a single overlay state, including all the overlay logs &
35 struct of_overlay_info {
36 struct device_node *target;
37 struct device_node *overlay;
41 * struct of_overlay - Holds a complete overlay transaction
42 * @node: List on which we are located
43 * @count: Count of ovinfo structures
44 * @ovinfo_tab: Overlay info table (count sized)
45 * @cset: Changeset to be used
47 * Holds a complete overlay transaction
51 struct list_head node;
53 struct of_overlay_info *ovinfo_tab;
54 struct of_changeset cset;
57 static int of_overlay_apply_one(struct of_overlay *ov,
58 struct device_node *target, const struct device_node *overlay);
60 static BLOCKING_NOTIFIER_HEAD(of_overlay_chain);
62 int of_overlay_notifier_register(struct notifier_block *nb)
64 return blocking_notifier_chain_register(&of_overlay_chain, nb);
66 EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
68 int of_overlay_notifier_unregister(struct notifier_block *nb)
70 return blocking_notifier_chain_unregister(&of_overlay_chain, nb);
72 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
74 static int of_overlay_notify(struct of_overlay *ov,
75 enum of_overlay_notify_action action)
77 struct of_overlay_notify_data nd;
80 for (i = 0; i < ov->count; i++) {
81 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
83 nd.target = ovinfo->target;
84 nd.overlay = ovinfo->overlay;
86 ret = blocking_notifier_call_chain(&of_overlay_chain,
89 return notifier_to_errno(ret);
95 static int of_overlay_apply_single_property(struct of_overlay *ov,
96 struct device_node *target, struct property *prop)
98 struct property *propn, *tprop;
100 /* NOTE: Multiple changes of single properties not supported */
101 tprop = of_find_property(target, prop->name, NULL);
103 /* special properties are not meant to be updated (silent NOP) */
104 if (of_prop_cmp(prop->name, "name") == 0 ||
105 of_prop_cmp(prop->name, "phandle") == 0 ||
106 of_prop_cmp(prop->name, "linux,phandle") == 0)
109 propn = __of_prop_dup(prop, GFP_KERNEL);
115 return of_changeset_add_property(&ov->cset, target, propn);
118 return of_changeset_update_property(&ov->cset, target, propn);
121 static int of_overlay_apply_single_device_node(struct of_overlay *ov,
122 struct device_node *target, struct device_node *child)
125 struct device_node *tchild;
128 cname = kbasename(child->full_name);
132 /* NOTE: Multiple mods of created nodes not supported */
133 for_each_child_of_node(target, tchild)
134 if (!of_node_cmp(cname, kbasename(tchild->full_name)))
137 if (tchild != NULL) {
138 /* new overlay phandle value conflicts with existing value */
142 /* apply overlay recursively */
143 ret = of_overlay_apply_one(ov, tchild, child);
146 /* create empty tree as a target */
147 tchild = __of_node_dup(child, "%pOF/%s", target, cname);
151 /* point to parent */
152 tchild->parent = target;
154 ret = of_changeset_attach_node(&ov->cset, tchild);
158 ret = of_overlay_apply_one(ov, tchild, child);
167 * Apply a single overlay node recursively.
169 * Note that the in case of an error the target node is left
170 * in a inconsistent state. Error recovery should be performed
171 * by using the changeset.
173 static int of_overlay_apply_one(struct of_overlay *ov,
174 struct device_node *target, const struct device_node *overlay)
176 struct device_node *child;
177 struct property *prop;
180 for_each_property_of_node(overlay, prop) {
181 ret = of_overlay_apply_single_property(ov, target, prop);
183 pr_err("Failed to apply prop @%pOF/%s\n",
189 for_each_child_of_node(overlay, child) {
190 ret = of_overlay_apply_single_device_node(ov, target, child);
192 pr_err("Failed to apply single node @%pOF/%s\n",
193 target, child->name);
203 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
204 * @ov: Overlay to apply
206 * Applies the overlays given, while handling all error conditions
207 * appropriately. Either the operation succeeds, or if it fails the
208 * live tree is reverted to the state before the attempt.
209 * Returns 0, or an error if the overlay attempt failed.
211 static int of_overlay_apply(struct of_overlay *ov)
215 /* first we apply the overlays atomically */
216 for (i = 0; i < ov->count; i++) {
217 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
219 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
221 pr_err("apply failed '%pOF'\n", ovinfo->target);
230 * Find the target node using a number of different strategies
231 * in order of preference
233 * "target" property containing the phandle of the target
234 * "target-path" property containing the path of the target
236 static struct device_node *find_target_node(struct device_node *info_node)
242 /* first try to go by using the target as a phandle */
243 ret = of_property_read_u32(info_node, "target", &val);
245 return of_find_node_by_phandle(val);
247 /* now try to locate by path */
248 ret = of_property_read_string(info_node, "target-path", &path);
250 return of_find_node_by_path(path);
252 pr_err("Failed to find target for node %p (%s)\n",
253 info_node, info_node->name);
259 * of_fill_overlay_info() - Fill an overlay info structure
260 * @ov Overlay to fill
261 * @info_node: Device node containing the overlay
262 * @ovinfo: Pointer to the overlay info structure to fill
264 * Fills an overlay info structure with the overlay information
265 * from a device node. This device node must have a target property
266 * which contains a phandle of the overlay target node, and an
267 * __overlay__ child node which has the overlay contents.
268 * Both ovinfo->target & ovinfo->overlay have their references taken.
270 * Returns 0 on success, or a negative error value.
272 static int of_fill_overlay_info(struct of_overlay *ov,
273 struct device_node *info_node, struct of_overlay_info *ovinfo)
275 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
276 if (ovinfo->overlay == NULL)
279 ovinfo->target = find_target_node(info_node);
280 if (ovinfo->target == NULL)
286 of_node_put(ovinfo->target);
287 of_node_put(ovinfo->overlay);
289 memset(ovinfo, 0, sizeof(*ovinfo));
294 * of_build_overlay_info() - Build an overlay info array
295 * @ov Overlay to build
296 * @tree: Device node containing all the overlays
298 * Helper function that given a tree containing overlay information,
299 * allocates and builds an overlay info array containing it, ready
300 * for use using of_overlay_apply.
302 * Returns 0 on success with the @cntp @ovinfop pointers valid,
303 * while on error a negative error value is returned.
305 static int of_build_overlay_info(struct of_overlay *ov,
306 struct device_node *tree)
308 struct device_node *node;
309 struct of_overlay_info *ovinfo;
312 /* worst case; every child is a node */
314 for_each_child_of_node(tree, node)
317 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
322 for_each_child_of_node(tree, node) {
323 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
328 /* if nothing filled, return error */
335 ov->ovinfo_tab = ovinfo;
341 * of_free_overlay_info() - Free an overlay info array
342 * @ov Overlay to free the overlay info from
343 * @ovinfo_tab: Array of overlay_info's to free
345 * Releases the memory of a previously allocated ovinfo array
346 * by of_build_overlay_info.
347 * Returns 0, or an error if the arguments are bogus.
349 static int of_free_overlay_info(struct of_overlay *ov)
351 struct of_overlay_info *ovinfo;
354 /* do it in reverse */
355 for (i = ov->count - 1; i >= 0; i--) {
356 ovinfo = &ov->ovinfo_tab[i];
358 of_node_put(ovinfo->target);
359 of_node_put(ovinfo->overlay);
361 kfree(ov->ovinfo_tab);
366 static int of_overlay_add_symbols(
367 struct device_node *tree,
368 struct of_overlay *ov)
370 struct of_overlay_info *ovinfo;
371 struct device_node *root_sym = NULL;
372 struct device_node *child = NULL;
373 struct property *prop;
374 const char *path, *s;
378 /* both may fail (if no fixups are required) */
379 root_sym = of_find_node_by_path("/__symbols__");
380 child = of_get_child_by_name(tree, "__symbols__");
383 /* do nothing if either is NULL */
384 if (!root_sym || !child)
387 for_each_property_of_node(child, prop) {
389 /* skip properties added automatically */
390 if (of_prop_cmp(prop->name, "name") == 0)
393 err = of_property_read_string(child,
396 pr_err("Could not find symbol '%s'\n", prop->name);
400 /* now find fragment index */
403 /* compare paths to find fragment index */
404 for (i = 0, ovinfo = NULL, len = -1; i < ov->count; i++) {
405 ovinfo = &ov->ovinfo_tab[i];
407 pr_debug("#%d: overlay->name=%s target->name=%s\n",
408 i, ovinfo->overlay->full_name,
409 ovinfo->target->full_name);
411 len = strlen(ovinfo->overlay->full_name);
412 if (strncasecmp(path, ovinfo->overlay->full_name,
413 len) == 0 && path[len] == '/')
420 pr_debug("found target at #%d\n", i);
421 new_path = kasprintf(GFP_KERNEL, "%s%s",
422 ovinfo->target->full_name,
425 pr_err("Failed to allocate propname for \"%s\"\n",
431 err = of_changeset_add_property_string(&ov->cset, root_sym,
432 prop->name, new_path);
438 pr_err("Failed to add property for \"%s\"\n",
446 of_node_put(root_sym);
451 static LIST_HEAD(ov_list);
452 static DEFINE_IDR(ov_idr);
455 * of_overlay_create() - Create and apply an overlay
456 * @tree: Device node containing all the overlays
458 * Creates and applies an overlay while also keeping track
459 * of the overlay in a list. This list can be used to prevent
460 * illegal overlay removals.
462 * Returns the id of the created overlay, or a negative error number
464 int of_overlay_create(struct device_node *tree)
466 struct of_overlay *ov;
469 /* allocate the overlay structure */
470 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
475 INIT_LIST_HEAD(&ov->node);
477 of_changeset_init(&ov->cset);
479 mutex_lock(&of_mutex);
481 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
484 goto err_destroy_trans;
488 /* build the overlay info structures */
489 err = of_build_overlay_info(ov, tree);
491 pr_err("of_build_overlay_info() failed for tree@%pOF\n",
496 err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY);
498 pr_err("%s: Pre-apply notifier failed (err=%d)\n",
503 /* apply the overlay */
504 err = of_overlay_apply(ov);
506 goto err_abort_trans;
508 err = of_overlay_add_symbols(tree, ov);
510 pr_err("%s: of_overlay_add_symbols() failed for tree@%s\n",
511 __func__, tree->full_name);
512 goto err_abort_trans;
515 /* apply the changeset */
516 err = __of_changeset_apply(&ov->cset);
518 goto err_revert_overlay;
521 /* add to the tail of the overlay list */
522 list_add_tail(&ov->node, &ov_list);
524 of_overlay_notify(ov, OF_OVERLAY_POST_APPLY);
526 mutex_unlock(&of_mutex);
532 of_free_overlay_info(ov);
534 idr_remove(&ov_idr, ov->id);
536 of_changeset_destroy(&ov->cset);
538 mutex_unlock(&of_mutex);
542 EXPORT_SYMBOL_GPL(of_overlay_create);
544 /* check whether the given node, lies under the given tree */
545 static int overlay_subtree_check(struct device_node *tree,
546 struct device_node *dn)
548 struct device_node *child;
554 for_each_child_of_node(tree, child) {
555 if (overlay_subtree_check(child, dn)) {
564 /* check whether this overlay is the topmost */
565 static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
567 struct of_overlay *ovt;
568 struct of_changeset_entry *ce;
570 list_for_each_entry_reverse(ovt, &ov_list, node) {
571 /* if we hit ourselves, we're done */
575 /* check against each subtree affected by this overlay */
576 list_for_each_entry(ce, &ovt->cset.entries, node) {
577 if (overlay_subtree_check(ce->np, dn)) {
578 pr_err("%s: #%d clashes #%d @%pOF\n",
579 __func__, ov->id, ovt->id, dn);
585 /* overlay is topmost */
590 * We can safely remove the overlay only if it's the top-most one.
591 * Newly applied overlays are inserted at the tail of the overlay list,
592 * so a top most overlay is the one that is closest to the tail.
594 * The topmost check is done by exploiting this property. For each
595 * affected device node in the log list we check if this overlay is
596 * the one closest to the tail. If another overlay has affected this
597 * device node and is closest to the tail, then removal is not permited.
599 static int overlay_removal_is_ok(struct of_overlay *ov)
601 struct of_changeset_entry *ce;
603 list_for_each_entry(ce, &ov->cset.entries, node) {
604 if (!overlay_is_topmost(ov, ce->np)) {
605 pr_err("overlay #%d is not topmost\n", ov->id);
614 * of_overlay_destroy() - Removes an overlay
615 * @id: Overlay id number returned by a previous call to of_overlay_create
617 * Removes an overlay if it is permissible.
619 * Returns 0 on success, or a negative error number
621 int of_overlay_destroy(int id)
623 struct of_overlay *ov;
626 mutex_lock(&of_mutex);
628 ov = idr_find(&ov_idr, id);
631 pr_err("destroy: Could not find overlay #%d\n", id);
635 /* check whether the overlay is safe to remove */
636 if (!overlay_removal_is_ok(ov)) {
641 of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE);
643 __of_changeset_revert(&ov->cset);
644 of_overlay_notify(ov, OF_OVERLAY_POST_REMOVE);
645 of_free_overlay_info(ov);
646 idr_remove(&ov_idr, id);
647 of_changeset_destroy(&ov->cset);
653 mutex_unlock(&of_mutex);
657 EXPORT_SYMBOL_GPL(of_overlay_destroy);
660 * of_overlay_destroy_all() - Removes all overlays from the system
662 * Removes all overlays from the system in the correct order.
664 * Returns 0 on success, or a negative error number
666 int of_overlay_destroy_all(void)
668 struct of_overlay *ov, *ovn;
670 mutex_lock(&of_mutex);
672 /* the tail of list is guaranteed to be safe to remove */
673 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
675 __of_changeset_revert(&ov->cset);
676 of_free_overlay_info(ov);
677 idr_remove(&ov_idr, ov->id);
681 mutex_unlock(&of_mutex);
685 EXPORT_SYMBOL_GPL(of_overlay_destroy_all);