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;
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,
60 bool is_symbols_node);
62 static BLOCKING_NOTIFIER_HEAD(of_overlay_chain);
64 int of_overlay_notifier_register(struct notifier_block *nb)
66 return blocking_notifier_chain_register(&of_overlay_chain, nb);
68 EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
70 int of_overlay_notifier_unregister(struct notifier_block *nb)
72 return blocking_notifier_chain_unregister(&of_overlay_chain, nb);
74 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
76 static int of_overlay_notify(struct of_overlay *ov,
77 enum of_overlay_notify_action action)
79 struct of_overlay_notify_data nd;
82 for (i = 0; i < ov->count; i++) {
83 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
85 nd.target = ovinfo->target;
86 nd.overlay = ovinfo->overlay;
88 ret = blocking_notifier_call_chain(&of_overlay_chain,
91 return notifier_to_errno(ret);
97 static struct property *dup_and_fixup_symbol_prop(struct of_overlay *ov,
98 const struct property *prop)
100 struct of_overlay_info *ovinfo;
101 struct property *new;
102 const char *overlay_name;
105 const char *target_path;
108 int overlay_name_len;
113 symbol_path = prop->value;
115 new = kzalloc(sizeof(*new), GFP_KERNEL);
119 for (k = 0; k < ov->count; k++) {
120 ovinfo = &ov->ovinfo_tab[k];
121 overlay_name = ovinfo->overlay->full_name;
122 overlay_name_len = strlen(overlay_name);
123 if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
130 target_path = ovinfo->target->full_name;
131 target_path_len = strlen(target_path);
133 label_path = symbol_path + overlay_name_len;
134 label_path_len = strlen(label_path);
136 new->name = kstrdup(prop->name, GFP_KERNEL);
137 new->length = target_path_len + label_path_len + 1;
138 new->value = kzalloc(new->length, GFP_KERNEL);
140 if (!new->name || !new->value)
143 strcpy(new->value, target_path);
144 strcpy(new->value + target_path_len, label_path);
146 /* mark the property as dynamic */
147 of_property_set_flag(new, OF_DYNAMIC);
160 static int of_overlay_apply_single_property(struct of_overlay *ov,
161 struct device_node *target, struct property *prop,
162 bool is_symbols_node)
164 struct property *propn = NULL, *tprop;
166 /* NOTE: Multiple changes of single properties not supported */
167 tprop = of_find_property(target, prop->name, NULL);
169 /* special properties are not meant to be updated (silent NOP) */
170 if (of_prop_cmp(prop->name, "name") == 0 ||
171 of_prop_cmp(prop->name, "phandle") == 0 ||
172 of_prop_cmp(prop->name, "linux,phandle") == 0)
175 if (is_symbols_node) {
176 /* changing a property in __symbols__ node not allowed */
179 propn = dup_and_fixup_symbol_prop(ov, prop);
181 propn = __of_prop_dup(prop, GFP_KERNEL);
189 return of_changeset_add_property(&ov->cset, target, propn);
192 return of_changeset_update_property(&ov->cset, target, propn);
195 static int of_overlay_apply_single_device_node(struct of_overlay *ov,
196 struct device_node *target, struct device_node *child)
199 struct device_node *tchild;
202 cname = kbasename(child->full_name);
206 /* NOTE: Multiple mods of created nodes not supported */
207 for_each_child_of_node(target, tchild)
208 if (!of_node_cmp(cname, kbasename(tchild->full_name)))
211 if (tchild != NULL) {
212 /* new overlay phandle value conflicts with existing value */
216 /* apply overlay recursively */
217 ret = of_overlay_apply_one(ov, tchild, child, 0);
220 /* create empty tree as a target */
221 tchild = __of_node_dup(child, "%pOF/%s", target, cname);
225 /* point to parent */
226 tchild->parent = target;
228 ret = of_changeset_attach_node(&ov->cset, tchild);
232 ret = of_overlay_apply_one(ov, tchild, child, 0);
241 * Apply a single overlay node recursively.
243 * Note that the in case of an error the target node is left
244 * in a inconsistent state. Error recovery should be performed
245 * by using the changeset.
247 static int of_overlay_apply_one(struct of_overlay *ov,
248 struct device_node *target, const struct device_node *overlay,
249 bool is_symbols_node)
251 struct device_node *child;
252 struct property *prop;
255 for_each_property_of_node(overlay, prop) {
256 ret = of_overlay_apply_single_property(ov, target, prop,
259 pr_err("Failed to apply prop @%pOF/%s\n",
265 /* do not allow symbols node to have any children */
269 for_each_child_of_node(overlay, child) {
270 ret = of_overlay_apply_single_device_node(ov, target, child);
272 pr_err("Failed to apply single node @%pOF/%s\n",
273 target, child->name);
283 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
284 * @ov: Overlay to apply
286 * Applies the overlays given, while handling all error conditions
287 * appropriately. Either the operation succeeds, or if it fails the
288 * live tree is reverted to the state before the attempt.
289 * Returns 0, or an error if the overlay attempt failed.
291 static int of_overlay_apply(struct of_overlay *ov)
295 /* first we apply the overlays atomically */
296 for (i = 0; i < ov->count; i++) {
297 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
299 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay,
300 ovinfo->is_symbols_node);
302 pr_err("apply failed '%pOF'\n", ovinfo->target);
311 * Find the target node using a number of different strategies
312 * in order of preference
314 * "target" property containing the phandle of the target
315 * "target-path" property containing the path of the target
317 static struct device_node *find_target_node(struct device_node *info_node)
323 /* first try to go by using the target as a phandle */
324 ret = of_property_read_u32(info_node, "target", &val);
326 return of_find_node_by_phandle(val);
328 /* now try to locate by path */
329 ret = of_property_read_string(info_node, "target-path", &path);
331 return of_find_node_by_path(path);
333 pr_err("Failed to find target for node %p (%s)\n",
334 info_node, info_node->name);
340 * of_fill_overlay_info() - Fill an overlay info structure
341 * @ov Overlay to fill
342 * @info_node: Device node containing the overlay
343 * @ovinfo: Pointer to the overlay info structure to fill
345 * Fills an overlay info structure with the overlay information
346 * from a device node. This device node must have a target property
347 * which contains a phandle of the overlay target node, and an
348 * __overlay__ child node which has the overlay contents.
349 * Both ovinfo->target & ovinfo->overlay have their references taken.
351 * Returns 0 on success, or a negative error value.
353 static int of_fill_overlay_info(struct of_overlay *ov,
354 struct device_node *info_node, struct of_overlay_info *ovinfo)
356 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
357 if (ovinfo->overlay == NULL)
360 ovinfo->target = find_target_node(info_node);
361 if (ovinfo->target == NULL)
367 of_node_put(ovinfo->target);
368 of_node_put(ovinfo->overlay);
370 memset(ovinfo, 0, sizeof(*ovinfo));
375 * of_build_overlay_info() - Build an overlay info array
376 * @ov Overlay to build
377 * @tree: Device node containing all the overlays
379 * Helper function that given a tree containing overlay information,
380 * allocates and builds an overlay info array containing it, ready
381 * for use using of_overlay_apply.
383 * Returns 0 on success with the @cntp @ovinfop pointers valid,
384 * while on error a negative error value is returned.
386 static int of_build_overlay_info(struct of_overlay *ov,
387 struct device_node *tree)
389 struct device_node *node;
390 struct of_overlay_info *ovinfo;
393 /* worst case; every child is a node */
395 for_each_child_of_node(tree, node)
398 if (of_get_child_by_name(tree, "__symbols__"))
401 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
406 for_each_child_of_node(tree, node) {
407 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
412 node = of_get_child_by_name(tree, "__symbols__");
414 ovinfo[cnt].overlay = node;
415 ovinfo[cnt].target = of_find_node_by_path("/__symbols__");
416 ovinfo[cnt].is_symbols_node = 1;
418 if (!ovinfo[cnt].target) {
419 pr_err("no symbols in root of device tree.\n");
426 /* if nothing filled, return error */
433 ov->ovinfo_tab = ovinfo;
439 * of_free_overlay_info() - Free an overlay info array
440 * @ov Overlay to free the overlay info from
441 * @ovinfo_tab: Array of overlay_info's to free
443 * Releases the memory of a previously allocated ovinfo array
444 * by of_build_overlay_info.
445 * Returns 0, or an error if the arguments are bogus.
447 static int of_free_overlay_info(struct of_overlay *ov)
449 struct of_overlay_info *ovinfo;
452 /* do it in reverse */
453 for (i = ov->count - 1; i >= 0; i--) {
454 ovinfo = &ov->ovinfo_tab[i];
456 of_node_put(ovinfo->target);
457 of_node_put(ovinfo->overlay);
459 kfree(ov->ovinfo_tab);
464 static LIST_HEAD(ov_list);
465 static DEFINE_IDR(ov_idr);
468 * of_overlay_create() - Create and apply an overlay
469 * @tree: Device node containing all the overlays
471 * Creates and applies an overlay while also keeping track
472 * of the overlay in a list. This list can be used to prevent
473 * illegal overlay removals.
475 * Returns the id of the created overlay, or a negative error number
477 int of_overlay_create(struct device_node *tree)
479 struct of_overlay *ov;
482 /* allocate the overlay structure */
483 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
488 INIT_LIST_HEAD(&ov->node);
490 of_changeset_init(&ov->cset);
492 mutex_lock(&of_mutex);
494 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
497 goto err_destroy_trans;
501 /* build the overlay info structures */
502 err = of_build_overlay_info(ov, tree);
504 pr_err("of_build_overlay_info() failed for tree@%pOF\n",
509 err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY);
511 pr_err("%s: Pre-apply notifier failed (err=%d)\n",
516 /* apply the overlay */
517 err = of_overlay_apply(ov);
519 goto err_abort_trans;
521 /* apply the changeset */
522 err = __of_changeset_apply(&ov->cset);
524 goto err_revert_overlay;
527 /* add to the tail of the overlay list */
528 list_add_tail(&ov->node, &ov_list);
530 of_overlay_notify(ov, OF_OVERLAY_POST_APPLY);
532 mutex_unlock(&of_mutex);
538 of_free_overlay_info(ov);
540 idr_remove(&ov_idr, ov->id);
542 of_changeset_destroy(&ov->cset);
544 mutex_unlock(&of_mutex);
548 EXPORT_SYMBOL_GPL(of_overlay_create);
550 /* check whether the given node, lies under the given tree */
551 static int overlay_subtree_check(struct device_node *tree,
552 struct device_node *dn)
554 struct device_node *child;
560 for_each_child_of_node(tree, child) {
561 if (overlay_subtree_check(child, dn)) {
570 /* check whether this overlay is the topmost */
571 static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
573 struct of_overlay *ovt;
574 struct of_changeset_entry *ce;
576 list_for_each_entry_reverse(ovt, &ov_list, node) {
577 /* if we hit ourselves, we're done */
581 /* check against each subtree affected by this overlay */
582 list_for_each_entry(ce, &ovt->cset.entries, node) {
583 if (overlay_subtree_check(ce->np, dn)) {
584 pr_err("%s: #%d clashes #%d @%pOF\n",
585 __func__, ov->id, ovt->id, dn);
591 /* overlay is topmost */
596 * We can safely remove the overlay only if it's the top-most one.
597 * Newly applied overlays are inserted at the tail of the overlay list,
598 * so a top most overlay is the one that is closest to the tail.
600 * The topmost check is done by exploiting this property. For each
601 * affected device node in the log list we check if this overlay is
602 * the one closest to the tail. If another overlay has affected this
603 * device node and is closest to the tail, then removal is not permited.
605 static int overlay_removal_is_ok(struct of_overlay *ov)
607 struct of_changeset_entry *ce;
609 list_for_each_entry(ce, &ov->cset.entries, node) {
610 if (!overlay_is_topmost(ov, ce->np)) {
611 pr_err("overlay #%d is not topmost\n", ov->id);
620 * of_overlay_destroy() - Removes an overlay
621 * @id: Overlay id number returned by a previous call to of_overlay_create
623 * Removes an overlay if it is permissible.
625 * Returns 0 on success, or a negative error number
627 int of_overlay_destroy(int id)
629 struct of_overlay *ov;
632 mutex_lock(&of_mutex);
634 ov = idr_find(&ov_idr, id);
637 pr_err("destroy: Could not find overlay #%d\n", id);
641 /* check whether the overlay is safe to remove */
642 if (!overlay_removal_is_ok(ov)) {
647 of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE);
649 __of_changeset_revert(&ov->cset);
650 of_overlay_notify(ov, OF_OVERLAY_POST_REMOVE);
651 of_free_overlay_info(ov);
652 idr_remove(&ov_idr, id);
653 of_changeset_destroy(&ov->cset);
659 mutex_unlock(&of_mutex);
663 EXPORT_SYMBOL_GPL(of_overlay_destroy);
666 * of_overlay_destroy_all() - Removes all overlays from the system
668 * Removes all overlays from the system in the correct order.
670 * Returns 0 on success, or a negative error number
672 int of_overlay_destroy_all(void)
674 struct of_overlay *ov, *ovn;
676 mutex_lock(&of_mutex);
678 /* the tail of list is guaranteed to be safe to remove */
679 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
681 __of_changeset_revert(&ov->cset);
682 of_free_overlay_info(ov);
683 idr_remove(&ov_idr, ov->id);
687 mutex_unlock(&of_mutex);
691 EXPORT_SYMBOL_GPL(of_overlay_destroy_all);