]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/of/overlay.c
of: overlay: loosen overly strict phandle clash check
[mirror_ubuntu-bionic-kernel.git] / drivers / of / overlay.c
CommitLineData
7518b589
PA
1/*
2 * Functions for working with device tree overlays
3 *
4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
5 * Copyright (C) 2012 Texas Instruments Inc.
6 *
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.
10 */
606ad42a
RH
11
12#define pr_fmt(fmt) "OF: overlay: " fmt
13
7518b589
PA
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>
7518b589
PA
21#include <linux/slab.h>
22#include <linux/err.h>
0d1886df 23#include <linux/idr.h>
7518b589
PA
24
25#include "of_private.h"
26
27/**
0290c4ca 28 * struct fragment - info about fragment nodes in overlay expanded device tree
7518b589 29 * @target: target of the overlay operation
0290c4ca 30 * @overlay: pointer to the __overlay__ node
7518b589 31 */
0290c4ca 32struct fragment {
7518b589
PA
33 struct device_node *target;
34 struct device_node *overlay;
d1651b03 35 bool is_symbols_node;
7518b589
PA
36};
37
38/**
0290c4ca
FR
39 * struct overlay_changeset
40 * @ovcs_list: list on which we are located
41 * @count: count of @fragments structures
42 * @fragments: info about fragment nodes in overlay expanded device tree
43 * @cset: changeset to apply fragments to live device tree
7518b589 44 */
0290c4ca 45struct overlay_changeset {
7518b589 46 int id;
0290c4ca 47 struct list_head ovcs_list;
7518b589 48 int count;
0290c4ca 49 struct fragment *fragments;
7518b589
PA
50 struct of_changeset cset;
51};
52
24789c5c
FR
53/* flags are sticky - once set, do not reset */
54static int devicetree_state_flags;
55#define DTSF_APPLY_FAIL 0x01
56#define DTSF_REVERT_FAIL 0x02
57
58/*
59 * If a changeset apply or revert encounters an error, an attempt will
60 * be made to undo partial changes, but may fail. If the undo fails
61 * we do not know the state of the devicetree.
62 */
63static int devicetree_corrupt(void)
64{
65 return devicetree_state_flags &
66 (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
67}
68
0290c4ca
FR
69static int build_changeset_next_level(struct overlay_changeset *ovcs,
70 struct device_node *target_node,
71 const struct device_node *overlay_node,
d1651b03 72 bool is_symbols_node);
7518b589 73
61b4de4e
FR
74static LIST_HEAD(ovcs_list);
75static DEFINE_IDR(ovcs_idr);
76
0290c4ca 77static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
39a842e2
AT
78
79int of_overlay_notifier_register(struct notifier_block *nb)
80{
0290c4ca 81 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
39a842e2
AT
82}
83EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
84
85int of_overlay_notifier_unregister(struct notifier_block *nb)
86{
0290c4ca 87 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
39a842e2
AT
88}
89EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
90
24789c5c
FR
91static char *of_overlay_action_name[] = {
92 "pre-apply",
93 "post-apply",
94 "pre-remove",
95 "post-remove",
96};
97
0290c4ca
FR
98static int overlay_notify(struct overlay_changeset *ovcs,
99 enum of_overlay_notify_action action)
39a842e2
AT
100{
101 struct of_overlay_notify_data nd;
102 int i, ret;
103
0290c4ca
FR
104 for (i = 0; i < ovcs->count; i++) {
105 struct fragment *fragment = &ovcs->fragments[i];
39a842e2 106
0290c4ca
FR
107 nd.target = fragment->target;
108 nd.overlay = fragment->overlay;
39a842e2 109
0290c4ca 110 ret = blocking_notifier_call_chain(&overlay_notify_chain,
39a842e2 111 action, &nd);
24789c5c
FR
112 if (ret == NOTIFY_STOP)
113 return 0;
114 if (ret) {
115 ret = notifier_to_errno(ret);
116 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
117 of_overlay_action_name[action], ret, nd.target);
118 return ret;
119 }
39a842e2
AT
120 }
121
122 return 0;
123}
124
42b2e94f
FR
125/*
126 * The properties in the "/__symbols__" node are "symbols".
127 *
128 * The value of properties in the "/__symbols__" node is the path of a
129 * node in the subtree of a fragment node's "__overlay__" node, for
130 * example "/fragment@0/__overlay__/symbol_path_tail". Symbol_path_tail
131 * can be a single node or it may be a multi-node path.
132 *
133 * The duplicated property value will be modified by replacing the
134 * "/fragment_name/__overlay/" portion of the value with the target
135 * path from the fragment node.
136 */
0290c4ca
FR
137static struct property *dup_and_fixup_symbol_prop(
138 struct overlay_changeset *ovcs, const struct property *prop)
d1651b03 139{
0290c4ca 140 struct fragment *fragment;
d1651b03
FR
141 struct property *new;
142 const char *overlay_name;
42b2e94f 143 char *symbol_path_tail;
d1651b03
FR
144 char *symbol_path;
145 const char *target_path;
146 int k;
42b2e94f 147 int symbol_path_tail_len;
d1651b03
FR
148 int overlay_name_len;
149 int target_path_len;
150
151 if (!prop->value)
152 return NULL;
153 symbol_path = prop->value;
154
155 new = kzalloc(sizeof(*new), GFP_KERNEL);
156 if (!new)
157 return NULL;
158
0290c4ca
FR
159 for (k = 0; k < ovcs->count; k++) {
160 fragment = &ovcs->fragments[k];
161 overlay_name = fragment->overlay->full_name;
d1651b03
FR
162 overlay_name_len = strlen(overlay_name);
163 if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
164 break;
165 }
166
0290c4ca 167 if (k >= ovcs->count)
d1651b03
FR
168 goto err_free;
169
0290c4ca 170 target_path = fragment->target->full_name;
d1651b03
FR
171 target_path_len = strlen(target_path);
172
42b2e94f
FR
173 symbol_path_tail = symbol_path + overlay_name_len;
174 symbol_path_tail_len = strlen(symbol_path_tail);
d1651b03
FR
175
176 new->name = kstrdup(prop->name, GFP_KERNEL);
42b2e94f 177 new->length = target_path_len + symbol_path_tail_len + 1;
d1651b03
FR
178 new->value = kzalloc(new->length, GFP_KERNEL);
179
180 if (!new->name || !new->value)
181 goto err_free;
182
183 strcpy(new->value, target_path);
42b2e94f 184 strcpy(new->value + target_path_len, symbol_path_tail);
d1651b03 185
d1651b03
FR
186 of_property_set_flag(new, OF_DYNAMIC);
187
188 return new;
189
190 err_free:
191 kfree(new->name);
192 kfree(new->value);
193 kfree(new);
194 return NULL;
d1651b03
FR
195}
196
0290c4ca
FR
197/**
198 * add_changeset_property() - add @overlay_prop to overlay changeset
199 * @ovcs: overlay changeset
200 * @target_node: where to place @overlay_prop in live tree
201 * @overlay_prop: property to add or update, from overlay tree
202 * is_symbols_node: 1 if @target_node is "/__symbols__"
203 *
204 * If @overlay_prop does not already exist in @target_node, add changeset entry
205 * to add @overlay_prop in @target_node, else add changeset entry to update
206 * value of @overlay_prop.
207 *
646afc4a 208 * Some special properties are not updated (no error returned).
0290c4ca 209 *
646afc4a 210 * Update of property in symbols node is not allowed.
0290c4ca
FR
211 *
212 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
213 * invalid @overlay.
646afc4a 214 */
0290c4ca
FR
215static int add_changeset_property(struct overlay_changeset *ovcs,
216 struct device_node *target_node,
217 struct property *overlay_prop,
d1651b03 218 bool is_symbols_node)
7518b589 219{
0290c4ca 220 struct property *new_prop = NULL, *prop;
ac0f3e30 221 int ret = 0;
7518b589 222
0290c4ca 223 prop = of_find_property(target_node, overlay_prop->name, NULL);
7518b589 224
0290c4ca
FR
225 if (!of_prop_cmp(overlay_prop->name, "name") ||
226 !of_prop_cmp(overlay_prop->name, "phandle") ||
227 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
7518b589
PA
228 return 0;
229
d1651b03 230 if (is_symbols_node) {
0290c4ca 231 if (prop)
d1651b03 232 return -EINVAL;
0290c4ca 233 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
d1651b03 234 } else {
0290c4ca 235 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
d1651b03
FR
236 }
237
0290c4ca 238 if (!new_prop)
7518b589
PA
239 return -ENOMEM;
240
0290c4ca
FR
241 if (!prop)
242 ret = of_changeset_add_property(&ovcs->cset, target_node,
243 new_prop);
646afc4a 244 else
0290c4ca
FR
245 ret = of_changeset_update_property(&ovcs->cset, target_node,
246 new_prop);
ac0f3e30
LW
247
248 if (ret) {
0290c4ca
FR
249 kfree(new_prop->name);
250 kfree(new_prop->value);
251 kfree(new_prop);
ac0f3e30
LW
252 }
253 return ret;
7518b589
PA
254}
255
0290c4ca
FR
256/**
257 * add_changeset_node() - add @node (and children) to overlay changeset
258 * @ovcs: overlay changeset
259 * @target_node: where to place @node in live tree
260 * @node: node from within overlay device tree fragment
261 *
262 * If @node does not already exist in @target_node, add changeset entry
263 * to add @node in @target_node.
264 *
265 * If @node already exists in @target_node, and the existing node has
266 * a phandle, the overlay node is not allowed to have a phandle.
267 *
268 * If @node has child nodes, add the children recursively via
269 * build_changeset_next_level().
270 *
271 * NOTE: Multiple mods of created nodes not supported.
24789c5c
FR
272 * If more than one fragment contains a node that does not already exist
273 * in the live tree, then for each fragment of_changeset_attach_node()
274 * will add a changeset entry to add the node. When the changeset is
275 * applied, __of_attach_node() will attach the node twice (once for
276 * each fragment). At this point the device tree will be corrupted.
277 *
278 * TODO: add integrity check to ensure that multiple fragments do not
279 * create the same node.
0290c4ca
FR
280 *
281 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
282 * invalid @overlay.
283 */
284static int add_changeset_node(struct overlay_changeset *ovcs,
285 struct device_node *target_node, struct device_node *node)
7518b589 286{
0290c4ca 287 const char *node_kbasename;
d3a89165 288 struct device_node *tchild;
7518b589
PA
289 int ret = 0;
290
0290c4ca
FR
291 node_kbasename = kbasename(node->full_name);
292 if (!node_kbasename)
7518b589
PA
293 return -ENOMEM;
294
0290c4ca
FR
295 for_each_child_of_node(target_node, tchild)
296 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
c1cd1e01
FR
297 break;
298
61b4de4e 299 if (!tchild) {
0290c4ca
FR
300 tchild = __of_node_dup(node, "%pOF/%s",
301 target_node, node_kbasename);
7518b589
PA
302 if (!tchild)
303 return -ENOMEM;
304
0290c4ca 305 tchild->parent = target_node;
7518b589 306
0290c4ca 307 ret = of_changeset_attach_node(&ovcs->cset, tchild);
7518b589
PA
308 if (ret)
309 return ret;
310
61b4de4e 311 return build_changeset_next_level(ovcs, tchild, node, 0);
7518b589
PA
312 }
313
6d0f5470
FR
314 if (node->phandle && tchild->phandle)
315 ret = -EINVAL;
316 else
317 ret = build_changeset_next_level(ovcs, tchild, node, 0);
61b4de4e
FR
318 of_node_put(tchild);
319
7518b589
PA
320 return ret;
321}
322
0290c4ca
FR
323/**
324 * build_changeset_next_level() - add level of overlay changeset
325 * @ovcs: overlay changeset
326 * @target_node: where to place @overlay_node in live tree
327 * @overlay_node: node from within an overlay device tree fragment
328 * @is_symbols_node: @overlay_node is node "/__symbols__"
7518b589 329 *
0290c4ca
FR
330 * Add the properties (if any) and nodes (if any) from @overlay_node to the
331 * @ovcs->cset changeset. If an added node has child nodes, they will
332 * be added recursively.
646afc4a
FR
333 *
334 * Do not allow symbols node to have any children.
0290c4ca
FR
335 *
336 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
337 * invalid @overlay_node.
7518b589 338 */
0290c4ca
FR
339static int build_changeset_next_level(struct overlay_changeset *ovcs,
340 struct device_node *target_node,
341 const struct device_node *overlay_node,
d1651b03 342 bool is_symbols_node)
7518b589
PA
343{
344 struct device_node *child;
345 struct property *prop;
346 int ret;
347
0290c4ca
FR
348 for_each_property_of_node(overlay_node, prop) {
349 ret = add_changeset_property(ovcs, target_node, prop,
350 is_symbols_node);
7518b589 351 if (ret) {
24789c5c
FR
352 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
353 target_node, prop->name, ret);
7518b589
PA
354 return ret;
355 }
356 }
357
d1651b03
FR
358 if (is_symbols_node)
359 return 0;
360
0290c4ca
FR
361 for_each_child_of_node(overlay_node, child) {
362 ret = add_changeset_node(ovcs, target_node, child);
bbed8794 363 if (ret) {
24789c5c
FR
364 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
365 target_node, child->name, ret);
001cf504 366 of_node_put(child);
7518b589
PA
367 return ret;
368 }
369 }
370
371 return 0;
372}
373
374/**
0290c4ca
FR
375 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
376 * @ovcs: Overlay changeset
7518b589 377 *
0290c4ca
FR
378 * Create changeset @ovcs->cset to contain the nodes and properties of the
379 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
380 * any portions of the changeset that were successfully created will remain
381 * in @ovcs->cset.
382 *
383 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
384 * invalid overlay in @ovcs->fragments[].
7518b589 385 */
0290c4ca 386static int build_changeset(struct overlay_changeset *ovcs)
7518b589 387{
0290c4ca 388 int i, ret;
7518b589 389
0290c4ca
FR
390 for (i = 0; i < ovcs->count; i++) {
391 struct fragment *fragment = &ovcs->fragments[i];
7518b589 392
0290c4ca
FR
393 ret = build_changeset_next_level(ovcs, fragment->target,
394 fragment->overlay,
395 fragment->is_symbols_node);
396 if (ret) {
24789c5c 397 pr_debug("apply failed '%pOF'\n", fragment->target);
0290c4ca 398 return ret;
7518b589
PA
399 }
400 }
401
402 return 0;
403}
404
405/*
406 * Find the target node using a number of different strategies
646afc4a 407 * in order of preference:
7518b589 408 *
646afc4a
FR
409 * 1) "target" property containing the phandle of the target
410 * 2) "target-path" property containing the path of the target
7518b589
PA
411 */
412static struct device_node *find_target_node(struct device_node *info_node)
413{
414 const char *path;
415 u32 val;
416 int ret;
417
7518b589 418 ret = of_property_read_u32(info_node, "target", &val);
bbed8794 419 if (!ret)
7518b589
PA
420 return of_find_node_by_phandle(val);
421
7518b589 422 ret = of_property_read_string(info_node, "target-path", &path);
bbed8794 423 if (!ret)
7518b589
PA
424 return of_find_node_by_path(path);
425
606ad42a 426 pr_err("Failed to find target for node %p (%s)\n",
7518b589
PA
427 info_node, info_node->name);
428
429 return NULL;
430}
431
7518b589 432/**
0290c4ca
FR
433 * init_overlay_changeset() - initialize overlay changeset from overlay tree
434 * @ovcs Overlay changeset to build
435 * @tree: Contains all the overlay fragments and overlay fixup nodes
7518b589 436 *
0290c4ca
FR
437 * Initialize @ovcs. Populate @ovcs->fragments with node information from
438 * the top level of @tree. The relevant top level nodes are the fragment
439 * nodes and the __symbols__ node. Any other top level node will be ignored.
7518b589 440 *
0290c4ca 441 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
61b4de4e 442 * detected in @tree, or -ENOSPC if idr_alloc() error.
7518b589 443 */
0290c4ca 444static int init_overlay_changeset(struct overlay_changeset *ovcs,
7518b589
PA
445 struct device_node *tree)
446{
61b4de4e 447 struct device_node *node, *overlay_node;
0290c4ca
FR
448 struct fragment *fragment;
449 struct fragment *fragments;
450 int cnt, ret;
7518b589 451
24789c5c
FR
452 /*
453 * Warn for some issues. Can not return -EINVAL for these until
454 * of_unittest_apply_overlay() is fixed to pass these checks.
455 */
456 if (!of_node_check_flag(tree, OF_DYNAMIC))
457 pr_debug("%s() tree is not dynamic\n", __func__);
458
459 if (!of_node_check_flag(tree, OF_DETACHED))
460 pr_debug("%s() tree is not detached\n", __func__);
461
462 if (!of_node_is_root(tree))
463 pr_debug("%s() tree is not root\n", __func__);
464
61b4de4e
FR
465 INIT_LIST_HEAD(&ovcs->ovcs_list);
466
467 of_changeset_init(&ovcs->cset);
468
469 ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
470 if (ovcs->id <= 0)
471 return ovcs->id;
472
7518b589 473 cnt = 0;
7518b589 474
61b4de4e
FR
475 /* fragment nodes */
476 for_each_child_of_node(tree, node) {
477 overlay_node = of_get_child_by_name(node, "__overlay__");
478 if (overlay_node) {
479 cnt++;
480 of_node_put(overlay_node);
481 }
482 }
483
484 node = of_get_child_by_name(tree, "__symbols__");
485 if (node) {
d1651b03 486 cnt++;
61b4de4e
FR
487 of_node_put(node);
488 }
d1651b03 489
0290c4ca 490 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
61b4de4e
FR
491 if (!fragments) {
492 ret = -ENOMEM;
493 goto err_free_idr;
494 }
7518b589
PA
495
496 cnt = 0;
497 for_each_child_of_node(tree, node) {
61b4de4e
FR
498 fragment = &fragments[cnt];
499 fragment->overlay = of_get_child_by_name(node, "__overlay__");
500 if (fragment->overlay) {
501 fragment->target = find_target_node(node);
502 if (!fragment->target) {
503 of_node_put(fragment->overlay);
504 ret = -EINVAL;
505 goto err_free_fragments;
506 } else {
507 cnt++;
508 }
509 }
7518b589
PA
510 }
511
d1651b03
FR
512 node = of_get_child_by_name(tree, "__symbols__");
513 if (node) {
0290c4ca
FR
514 fragment = &fragments[cnt];
515 fragment->overlay = node;
516 fragment->target = of_find_node_by_path("/__symbols__");
517 fragment->is_symbols_node = 1;
d1651b03 518
0290c4ca 519 if (!fragment->target) {
d1651b03 520 pr_err("no symbols in root of device tree.\n");
61b4de4e
FR
521 ret = -EINVAL;
522 goto err_free_fragments;
d1651b03
FR
523 }
524
525 cnt++;
526 }
527
bbed8794 528 if (!cnt) {
61b4de4e
FR
529 ret = -EINVAL;
530 goto err_free_fragments;
7518b589
PA
531 }
532
0290c4ca
FR
533 ovcs->count = cnt;
534 ovcs->fragments = fragments;
7518b589
PA
535
536 return 0;
61b4de4e 537
61b4de4e
FR
538err_free_fragments:
539 kfree(fragments);
540err_free_idr:
541 idr_remove(&ovcs_idr, ovcs->id);
542
24789c5c
FR
543 pr_err("%s() failed, ret = %d\n", __func__, ret);
544
61b4de4e 545 return ret;
7518b589
PA
546}
547
61b4de4e 548static void free_overlay_changeset(struct overlay_changeset *ovcs)
7518b589 549{
7518b589
PA
550 int i;
551
61b4de4e
FR
552 if (!ovcs->cset.entries.next)
553 return;
554 of_changeset_destroy(&ovcs->cset);
555
556 if (ovcs->id)
557 idr_remove(&ovcs_idr, ovcs->id);
558
559 for (i = 0; i < ovcs->count; i++) {
0290c4ca
FR
560 of_node_put(ovcs->fragments[i].target);
561 of_node_put(ovcs->fragments[i].overlay);
7518b589 562 }
0290c4ca 563 kfree(ovcs->fragments);
7518b589 564
61b4de4e
FR
565 kfree(ovcs);
566}
7518b589
PA
567
568/**
0290c4ca
FR
569 * of_overlay_apply() - Create and apply an overlay changeset
570 * @tree: Expanded overlay device tree
24789c5c
FR
571 * @ovcs_id: Pointer to overlay changeset id
572 *
573 * Creates and applies an overlay changeset.
7518b589 574 *
24789c5c
FR
575 * If an error occurs in a pre-apply notifier, then no changes are made
576 * to the device tree.
7518b589 577 *
24789c5c
FR
578
579 * A non-zero return value will not have created the changeset if error is from:
580 * - parameter checks
581 * - building the changeset
582 * - overlay changset pre-apply notifier
583 *
584 * If an error is returned by an overlay changeset pre-apply notifier
585 * then no further overlay changeset pre-apply notifier will be called.
586 *
587 * A non-zero return value will have created the changeset if error is from:
588 * - overlay changeset entry notifier
589 * - overlay changset post-apply notifier
590 *
591 * If an error is returned by an overlay changeset post-apply notifier
592 * then no further overlay changeset post-apply notifier will be called.
593 *
594 * If more than one notifier returns an error, then the last notifier
595 * error to occur is returned.
596 *
597 * If an error occurred while applying the overlay changeset, then an
598 * attempt is made to revert any changes that were made to the
599 * device tree. If there were any errors during the revert attempt
600 * then the state of the device tree can not be determined, and any
601 * following attempt to apply or remove an overlay changeset will be
602 * refused.
603 *
604 * Returns 0 on success, or a negative error number. Overlay changeset
605 * id is returned to *ovcs_id.
7518b589 606 */
24789c5c
FR
607
608int of_overlay_apply(struct device_node *tree, int *ovcs_id)
7518b589 609{
0290c4ca 610 struct overlay_changeset *ovcs;
24789c5c
FR
611 int ret = 0, ret_revert, ret_tmp;
612
613 *ovcs_id = 0;
614
615 if (devicetree_corrupt()) {
616 pr_err("devicetree state suspect, refuse to apply overlay\n");
617 ret = -EBUSY;
618 goto out;
619 }
7518b589 620
0290c4ca 621 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
24789c5c
FR
622 if (!ovcs) {
623 ret = -ENOMEM;
624 goto out;
625 }
7518b589
PA
626
627 mutex_lock(&of_mutex);
628
0290c4ca 629 ret = init_overlay_changeset(ovcs, tree);
24789c5c 630 if (ret)
61b4de4e 631 goto err_free_overlay_changeset;
7518b589 632
0290c4ca 633 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
24789c5c
FR
634 if (ret) {
635 pr_err("overlay changeset pre-apply notify error %d\n", ret);
61b4de4e 636 goto err_free_overlay_changeset;
39a842e2
AT
637 }
638
0290c4ca
FR
639 ret = build_changeset(ovcs);
640 if (ret)
61b4de4e 641 goto err_free_overlay_changeset;
606ad42a 642
24789c5c
FR
643 ret_revert = 0;
644 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
645 if (ret) {
646 if (ret_revert) {
647 pr_debug("overlay changeset revert error %d\n",
648 ret_revert);
649 devicetree_state_flags |= DTSF_APPLY_FAIL;
650 }
61b4de4e 651 goto err_free_overlay_changeset;
24789c5c
FR
652 } else {
653 ret = __of_changeset_apply_notify(&ovcs->cset);
654 if (ret)
655 pr_err("overlay changeset entry notify error %d\n",
656 ret);
657 /* fall through */
658 }
7518b589 659
0290c4ca 660 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
24789c5c
FR
661 *ovcs_id = ovcs->id;
662
663 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
664 if (ret_tmp) {
665 pr_err("overlay changeset post-apply notify error %d\n",
666 ret_tmp);
667 if (!ret)
668 ret = ret_tmp;
669 }
39a842e2 670
7518b589
PA
671 mutex_unlock(&of_mutex);
672
24789c5c 673 goto out;
61b4de4e
FR
674
675err_free_overlay_changeset:
676 free_overlay_changeset(ovcs);
7518b589 677
7518b589
PA
678 mutex_unlock(&of_mutex);
679
24789c5c
FR
680out:
681 pr_debug("%s() err=%d\n", __func__, ret);
682
0290c4ca 683 return ret;
7518b589 684}
0290c4ca 685EXPORT_SYMBOL_GPL(of_overlay_apply);
7518b589 686
646afc4a 687/*
0290c4ca
FR
688 * Find @np in @tree.
689 *
690 * Returns 1 if @np is @tree or is contained in @tree, else 0
646afc4a 691 */
0290c4ca 692static int find_node(struct device_node *tree, struct device_node *np)
7518b589
PA
693{
694 struct device_node *child;
695
0290c4ca 696 if (tree == np)
7518b589
PA
697 return 1;
698
699 for_each_child_of_node(tree, child) {
0290c4ca 700 if (find_node(child, np)) {
001cf504 701 of_node_put(child);
7518b589 702 return 1;
001cf504 703 }
7518b589
PA
704 }
705
706 return 0;
707}
708
646afc4a 709/*
87f242c1 710 * Is @remove_ce_node a child of, a parent of, or the same as any
0290c4ca
FR
711 * node in an overlay changeset more topmost than @remove_ovcs?
712 *
713 * Returns 1 if found, else 0
646afc4a 714 */
87f242c1
FR
715static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
716 struct device_node *remove_ce_node)
7518b589 717{
0290c4ca 718 struct overlay_changeset *ovcs;
7518b589
PA
719 struct of_changeset_entry *ce;
720
0290c4ca
FR
721 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
722 if (ovcs == remove_ovcs)
7518b589
PA
723 break;
724
0290c4ca 725 list_for_each_entry(ce, &ovcs->cset.entries, node) {
87f242c1
FR
726 if (find_node(ce->np, remove_ce_node)) {
727 pr_err("%s: #%d overlaps with #%d @%pOF\n",
0290c4ca 728 __func__, remove_ovcs->id, ovcs->id,
87f242c1
FR
729 remove_ce_node);
730 return 1;
731 }
732 if (find_node(remove_ce_node, ce->np)) {
733 pr_err("%s: #%d overlaps with #%d @%pOF\n",
734 __func__, remove_ovcs->id, ovcs->id,
735 remove_ce_node);
0290c4ca 736 return 1;
7518b589
PA
737 }
738 }
739 }
740
0290c4ca 741 return 0;
7518b589
PA
742}
743
744/*
745 * We can safely remove the overlay only if it's the top-most one.
746 * Newly applied overlays are inserted at the tail of the overlay list,
747 * so a top most overlay is the one that is closest to the tail.
748 *
749 * The topmost check is done by exploiting this property. For each
750 * affected device node in the log list we check if this overlay is
751 * the one closest to the tail. If another overlay has affected this
752 * device node and is closest to the tail, then removal is not permited.
753 */
0290c4ca 754static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
7518b589 755{
0290c4ca 756 struct of_changeset_entry *remove_ce;
7518b589 757
0290c4ca 758 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
87f242c1 759 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
0290c4ca 760 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
7518b589
PA
761 return 0;
762 }
763 }
764
765 return 1;
766}
767
768/**
0290c4ca 769 * of_overlay_remove() - Revert and free an overlay changeset
24789c5c 770 * @ovcs_id: Pointer to overlay changeset id
7518b589 771 *
24789c5c 772 * Removes an overlay if it is permissible. @ovcs_id was previously returned
0290c4ca 773 * by of_overlay_apply().
7518b589 774 *
24789c5c
FR
775 * If an error occurred while attempting to revert the overlay changeset,
776 * then an attempt is made to re-apply any changeset entry that was
777 * reverted. If an error occurs on re-apply then the state of the device
778 * tree can not be determined, and any following attempt to apply or remove
779 * an overlay changeset will be refused.
780 *
781 * A non-zero return value will not revert the changeset if error is from:
782 * - parameter checks
783 * - overlay changset pre-remove notifier
784 * - overlay changeset entry revert
785 *
786 * If an error is returned by an overlay changeset pre-remove notifier
787 * then no further overlay changeset pre-remove notifier will be called.
788 *
789 * If more than one notifier returns an error, then the last notifier
790 * error to occur is returned.
791 *
792 * A non-zero return value will revert the changeset if error is from:
793 * - overlay changeset entry notifier
794 * - overlay changset post-remove notifier
795 *
796 * If an error is returned by an overlay changeset post-remove notifier
797 * then no further overlay changeset post-remove notifier will be called.
798 *
799 * Returns 0 on success, or a negative error number. *ovcs_id is set to
800 * zero after reverting the changeset, even if a subsequent error occurs.
7518b589 801 */
24789c5c 802int of_overlay_remove(int *ovcs_id)
7518b589 803{
0290c4ca 804 struct overlay_changeset *ovcs;
24789c5c
FR
805 int ret, ret_apply, ret_tmp;
806
807 ret = 0;
808
809 if (devicetree_corrupt()) {
810 pr_err("suspect devicetree state, refuse to remove overlay\n");
811 ret = -EBUSY;
812 goto out;
813 }
7518b589
PA
814
815 mutex_lock(&of_mutex);
816
24789c5c 817 ovcs = idr_find(&ovcs_idr, *ovcs_id);
0290c4ca
FR
818 if (!ovcs) {
819 ret = -ENODEV;
24789c5c
FR
820 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
821 goto out_unlock;
7518b589
PA
822 }
823
0290c4ca
FR
824 if (!overlay_removal_is_ok(ovcs)) {
825 ret = -EBUSY;
24789c5c 826 goto out_unlock;
7518b589
PA
827 }
828
24789c5c
FR
829 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
830 if (ret) {
831 pr_err("overlay changeset pre-remove notify error %d\n", ret);
832 goto out_unlock;
833 }
61b4de4e 834
0290c4ca 835 list_del(&ovcs->ovcs_list);
61b4de4e 836
24789c5c
FR
837 ret_apply = 0;
838 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
839 if (ret) {
840 if (ret_apply)
841 devicetree_state_flags |= DTSF_REVERT_FAIL;
842 goto out_unlock;
843 } else {
844 ret = __of_changeset_revert_notify(&ovcs->cset);
845 if (ret) {
846 pr_err("overlay changeset entry notify error %d\n",
847 ret);
848 /* fall through - changeset was reverted */
849 }
850 }
61b4de4e 851
24789c5c
FR
852 *ovcs_id = 0;
853
854 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
855 if (ret_tmp) {
856 pr_err("overlay changeset post-remove notify error %d\n",
857 ret_tmp);
858 if (!ret)
859 ret = ret_tmp;
860 }
61b4de4e
FR
861
862 free_overlay_changeset(ovcs);
7518b589 863
24789c5c 864out_unlock:
7518b589
PA
865 mutex_unlock(&of_mutex);
866
24789c5c
FR
867out:
868 pr_debug("%s() err=%d\n", __func__, ret);
869
0290c4ca 870 return ret;
7518b589 871}
0290c4ca 872EXPORT_SYMBOL_GPL(of_overlay_remove);
7518b589
PA
873
874/**
0290c4ca 875 * of_overlay_remove_all() - Reverts and frees all overlay changesets
7518b589
PA
876 *
877 * Removes all overlays from the system in the correct order.
878 *
94a8bf97 879 * Returns 0 on success, or a negative error number
7518b589 880 */
0290c4ca 881int of_overlay_remove_all(void)
7518b589 882{
0290c4ca 883 struct overlay_changeset *ovcs, *ovcs_n;
61b4de4e 884 int ret;
7518b589
PA
885
886 /* the tail of list is guaranteed to be safe to remove */
0290c4ca 887 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
24789c5c 888 ret = of_overlay_remove(&ovcs->id);
61b4de4e
FR
889 if (ret)
890 return ret;
7518b589
PA
891 }
892
7518b589
PA
893 return 0;
894}
0290c4ca 895EXPORT_SYMBOL_GPL(of_overlay_remove_all);