Linux 4.14.51
[linux/fpc-iii.git] / drivers / of / overlay.c
blob8ecfee31ab6d3874602f9346cf077f985c2d484f
1 /*
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>
16 #include <linux/of.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"
27 /**
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 &
33 * records.
35 struct of_overlay_info {
36 struct device_node *target;
37 struct device_node *overlay;
38 bool is_symbols_node;
41 /**
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
50 struct of_overlay {
51 int id;
52 struct list_head node;
53 int count;
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;
80 int i, ret;
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,
89 action, &nd);
90 if (ret)
91 return notifier_to_errno(ret);
94 return 0;
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;
103 char *label_path;
104 char *symbol_path;
105 const char *target_path;
106 int k;
107 int label_path_len;
108 int overlay_name_len;
109 int target_path_len;
111 if (!prop->value)
112 return NULL;
113 symbol_path = prop->value;
115 new = kzalloc(sizeof(*new), GFP_KERNEL);
116 if (!new)
117 return NULL;
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))
124 break;
127 if (k >= ov->count)
128 goto err_free;
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)
141 goto err_free;
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);
149 return new;
151 err_free:
152 kfree(new->name);
153 kfree(new->value);
154 kfree(new);
155 return NULL;
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)
173 return 0;
175 if (is_symbols_node) {
176 /* changing a property in __symbols__ node not allowed */
177 if (tprop)
178 return -EINVAL;
179 propn = dup_and_fixup_symbol_prop(ov, prop);
180 } else {
181 propn = __of_prop_dup(prop, GFP_KERNEL);
184 if (propn == NULL)
185 return -ENOMEM;
187 /* not found? add */
188 if (tprop == NULL)
189 return of_changeset_add_property(&ov->cset, target, propn);
191 /* found? update */
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)
198 const char *cname;
199 struct device_node *tchild;
200 int ret = 0;
202 cname = kbasename(child->full_name);
203 if (cname == NULL)
204 return -ENOMEM;
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)))
209 break;
211 if (tchild != NULL) {
212 /* new overlay phandle value conflicts with existing value */
213 if (child->phandle)
214 return -EINVAL;
216 /* apply overlay recursively */
217 ret = of_overlay_apply_one(ov, tchild, child, 0);
218 of_node_put(tchild);
219 } else {
220 /* create empty tree as a target */
221 tchild = __of_node_dup(child, "%pOF/%s", target, cname);
222 if (!tchild)
223 return -ENOMEM;
225 /* point to parent */
226 tchild->parent = target;
228 ret = of_changeset_attach_node(&ov->cset, tchild);
229 if (ret)
230 return ret;
232 ret = of_overlay_apply_one(ov, tchild, child, 0);
233 if (ret)
234 return ret;
237 return ret;
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;
253 int ret;
255 for_each_property_of_node(overlay, prop) {
256 ret = of_overlay_apply_single_property(ov, target, prop,
257 is_symbols_node);
258 if (ret) {
259 pr_err("Failed to apply prop @%pOF/%s\n",
260 target, prop->name);
261 return ret;
265 /* do not allow symbols node to have any children */
266 if (is_symbols_node)
267 return 0;
269 for_each_child_of_node(overlay, child) {
270 ret = of_overlay_apply_single_device_node(ov, target, child);
271 if (ret != 0) {
272 pr_err("Failed to apply single node @%pOF/%s\n",
273 target, child->name);
274 of_node_put(child);
275 return ret;
279 return 0;
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)
293 int i, err;
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);
301 if (err != 0) {
302 pr_err("apply failed '%pOF'\n", ovinfo->target);
303 return err;
307 return 0;
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)
319 const char *path;
320 u32 val;
321 int ret;
323 /* first try to go by using the target as a phandle */
324 ret = of_property_read_u32(info_node, "target", &val);
325 if (ret == 0)
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);
330 if (ret == 0)
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);
336 return NULL;
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)
358 goto err_fail;
360 ovinfo->target = find_target_node(info_node);
361 if (ovinfo->target == NULL)
362 goto err_fail;
364 return 0;
366 err_fail:
367 of_node_put(ovinfo->target);
368 of_node_put(ovinfo->overlay);
370 memset(ovinfo, 0, sizeof(*ovinfo));
371 return -EINVAL;
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;
391 int cnt, err;
393 /* worst case; every child is a node */
394 cnt = 0;
395 for_each_child_of_node(tree, node)
396 cnt++;
398 if (of_get_child_by_name(tree, "__symbols__"))
399 cnt++;
401 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
402 if (ovinfo == NULL)
403 return -ENOMEM;
405 cnt = 0;
406 for_each_child_of_node(tree, node) {
407 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
408 if (err == 0)
409 cnt++;
412 node = of_get_child_by_name(tree, "__symbols__");
413 if (node) {
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");
420 return -EINVAL;
423 cnt++;
426 /* if nothing filled, return error */
427 if (cnt == 0) {
428 kfree(ovinfo);
429 return -ENODEV;
432 ov->count = cnt;
433 ov->ovinfo_tab = ovinfo;
435 return 0;
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;
450 int i;
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);
461 return 0;
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;
480 int err, id;
482 /* allocate the overlay structure */
483 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
484 if (ov == NULL)
485 return -ENOMEM;
486 ov->id = -1;
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);
495 if (id < 0) {
496 err = id;
497 goto err_destroy_trans;
499 ov->id = id;
501 /* build the overlay info structures */
502 err = of_build_overlay_info(ov, tree);
503 if (err) {
504 pr_err("of_build_overlay_info() failed for tree@%pOF\n",
505 tree);
506 goto err_free_idr;
509 err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY);
510 if (err < 0) {
511 pr_err("%s: Pre-apply notifier failed (err=%d)\n",
512 __func__, err);
513 goto err_free_idr;
516 /* apply the overlay */
517 err = of_overlay_apply(ov);
518 if (err)
519 goto err_abort_trans;
521 /* apply the changeset */
522 err = __of_changeset_apply(&ov->cset);
523 if (err)
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);
534 return id;
536 err_revert_overlay:
537 err_abort_trans:
538 of_free_overlay_info(ov);
539 err_free_idr:
540 idr_remove(&ov_idr, ov->id);
541 err_destroy_trans:
542 of_changeset_destroy(&ov->cset);
543 kfree(ov);
544 mutex_unlock(&of_mutex);
546 return err;
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;
556 /* match? */
557 if (tree == dn)
558 return 1;
560 for_each_child_of_node(tree, child) {
561 if (overlay_subtree_check(child, dn)) {
562 of_node_put(child);
563 return 1;
567 return 0;
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 */
578 if (ovt == ov)
579 break;
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);
586 return 0;
591 /* overlay is topmost */
592 return 1;
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);
612 return 0;
616 return 1;
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;
630 int err;
632 mutex_lock(&of_mutex);
634 ov = idr_find(&ov_idr, id);
635 if (ov == NULL) {
636 err = -ENODEV;
637 pr_err("destroy: Could not find overlay #%d\n", id);
638 goto out;
641 /* check whether the overlay is safe to remove */
642 if (!overlay_removal_is_ok(ov)) {
643 err = -EBUSY;
644 goto out;
647 of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE);
648 list_del(&ov->node);
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);
654 kfree(ov);
656 err = 0;
658 out:
659 mutex_unlock(&of_mutex);
661 return err;
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) {
680 list_del(&ov->node);
681 __of_changeset_revert(&ov->cset);
682 of_free_overlay_info(ov);
683 idr_remove(&ov_idr, ov->id);
684 kfree(ov);
687 mutex_unlock(&of_mutex);
689 return 0;
691 EXPORT_SYMBOL_GPL(of_overlay_destroy_all);