blob: d164f86e55418ee63b54d98528e9efef4937e27a [file] [log] [blame]
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001/*
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 */
Rob Herring606ad422016-06-15 08:32:18 -050011
12#define pr_fmt(fmt) "OF: overlay: " fmt
13
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020014#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>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020021#include <linux/slab.h>
22#include <linux/err.h>
Mark Brown0d1886d2015-02-17 11:36:58 +090023#include <linux/idr.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020024
25#include "of_private.h"
26
27/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070028 * struct fragment - info about fragment nodes in overlay expanded device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020029 * @target: target of the overlay operation
Frank Rowand0290c4c2017-10-17 16:36:23 -070030 * @overlay: pointer to the __overlay__ node
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020031 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070032struct fragment {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020033 struct device_node *target;
34 struct device_node *overlay;
Frank Rowandd1651b02017-07-19 09:25:22 -070035 bool is_symbols_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020036};
37
38/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070039 * 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
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020044 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070045struct overlay_changeset {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020046 int id;
Frank Rowand0290c4c2017-10-17 16:36:23 -070047 struct list_head ovcs_list;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020048 int count;
Frank Rowand0290c4c2017-10-17 16:36:23 -070049 struct fragment *fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020050 struct of_changeset cset;
51};
52
Frank Rowand24789c52017-10-17 16:36:26 -070053/* 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
Frank Rowand0290c4c2017-10-17 16:36:23 -070069static int build_changeset_next_level(struct overlay_changeset *ovcs,
70 struct device_node *target_node,
71 const struct device_node *overlay_node,
Frank Rowandd1651b02017-07-19 09:25:22 -070072 bool is_symbols_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020073
Frank Rowandf948d6d2017-10-17 16:36:29 -070074/*
75 * of_resolve_phandles() finds the largest phandle in the live tree.
76 * of_overlay_apply() may add a larger phandle to the live tree.
77 * Do not allow race between two overlays being applied simultaneously:
78 * mutex_lock(&of_overlay_phandle_mutex)
79 * of_resolve_phandles()
80 * of_overlay_apply()
81 * mutex_unlock(&of_overlay_phandle_mutex)
82 */
83static DEFINE_MUTEX(of_overlay_phandle_mutex);
84
85void of_overlay_mutex_lock(void)
86{
87 mutex_lock(&of_overlay_phandle_mutex);
88}
89
90void of_overlay_mutex_unlock(void)
91{
92 mutex_unlock(&of_overlay_phandle_mutex);
93}
94
95
Frank Rowand61b4de42017-10-17 16:36:25 -070096static LIST_HEAD(ovcs_list);
97static DEFINE_IDR(ovcs_idr);
98
Frank Rowand0290c4c2017-10-17 16:36:23 -070099static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
Alan Tull39a842e2016-11-01 14:14:22 -0500100
101int of_overlay_notifier_register(struct notifier_block *nb)
102{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700103 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500104}
105EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
106
107int of_overlay_notifier_unregister(struct notifier_block *nb)
108{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700109 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500110}
111EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
112
Frank Rowand24789c52017-10-17 16:36:26 -0700113static char *of_overlay_action_name[] = {
114 "pre-apply",
115 "post-apply",
116 "pre-remove",
117 "post-remove",
118};
119
Frank Rowand0290c4c2017-10-17 16:36:23 -0700120static int overlay_notify(struct overlay_changeset *ovcs,
121 enum of_overlay_notify_action action)
Alan Tull39a842e2016-11-01 14:14:22 -0500122{
123 struct of_overlay_notify_data nd;
124 int i, ret;
125
Frank Rowand0290c4c2017-10-17 16:36:23 -0700126 for (i = 0; i < ovcs->count; i++) {
127 struct fragment *fragment = &ovcs->fragments[i];
Alan Tull39a842e2016-11-01 14:14:22 -0500128
Frank Rowand0290c4c2017-10-17 16:36:23 -0700129 nd.target = fragment->target;
130 nd.overlay = fragment->overlay;
Alan Tull39a842e2016-11-01 14:14:22 -0500131
Frank Rowand0290c4c2017-10-17 16:36:23 -0700132 ret = blocking_notifier_call_chain(&overlay_notify_chain,
Alan Tull39a842e2016-11-01 14:14:22 -0500133 action, &nd);
Frank Rowand24789c52017-10-17 16:36:26 -0700134 if (ret == NOTIFY_STOP)
135 return 0;
136 if (ret) {
137 ret = notifier_to_errno(ret);
138 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
139 of_overlay_action_name[action], ret, nd.target);
140 return ret;
141 }
Alan Tull39a842e2016-11-01 14:14:22 -0500142 }
143
144 return 0;
145}
146
Frank Rowand42b2e942017-10-17 16:36:24 -0700147/*
148 * The properties in the "/__symbols__" node are "symbols".
149 *
150 * The value of properties in the "/__symbols__" node is the path of a
151 * node in the subtree of a fragment node's "__overlay__" node, for
152 * example "/fragment@0/__overlay__/symbol_path_tail". Symbol_path_tail
153 * can be a single node or it may be a multi-node path.
154 *
155 * The duplicated property value will be modified by replacing the
156 * "/fragment_name/__overlay/" portion of the value with the target
157 * path from the fragment node.
158 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700159static struct property *dup_and_fixup_symbol_prop(
160 struct overlay_changeset *ovcs, const struct property *prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200161{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700162 struct fragment *fragment;
Frank Rowandd1651b02017-07-19 09:25:22 -0700163 struct property *new;
164 const char *overlay_name;
Frank Rowand42b2e942017-10-17 16:36:24 -0700165 char *symbol_path_tail;
Frank Rowandd1651b02017-07-19 09:25:22 -0700166 char *symbol_path;
167 const char *target_path;
168 int k;
Frank Rowand42b2e942017-10-17 16:36:24 -0700169 int symbol_path_tail_len;
Frank Rowandd1651b02017-07-19 09:25:22 -0700170 int overlay_name_len;
171 int target_path_len;
172
173 if (!prop->value)
174 return NULL;
175 symbol_path = prop->value;
176
177 new = kzalloc(sizeof(*new), GFP_KERNEL);
178 if (!new)
179 return NULL;
180
Frank Rowand0290c4c2017-10-17 16:36:23 -0700181 for (k = 0; k < ovcs->count; k++) {
182 fragment = &ovcs->fragments[k];
183 overlay_name = fragment->overlay->full_name;
Frank Rowandd1651b02017-07-19 09:25:22 -0700184 overlay_name_len = strlen(overlay_name);
185 if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
186 break;
187 }
188
Frank Rowand0290c4c2017-10-17 16:36:23 -0700189 if (k >= ovcs->count)
Frank Rowandd1651b02017-07-19 09:25:22 -0700190 goto err_free;
191
Frank Rowand0290c4c2017-10-17 16:36:23 -0700192 target_path = fragment->target->full_name;
Frank Rowandd1651b02017-07-19 09:25:22 -0700193 target_path_len = strlen(target_path);
194
Frank Rowand42b2e942017-10-17 16:36:24 -0700195 symbol_path_tail = symbol_path + overlay_name_len;
196 symbol_path_tail_len = strlen(symbol_path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700197
198 new->name = kstrdup(prop->name, GFP_KERNEL);
Frank Rowand42b2e942017-10-17 16:36:24 -0700199 new->length = target_path_len + symbol_path_tail_len + 1;
Frank Rowandd1651b02017-07-19 09:25:22 -0700200 new->value = kzalloc(new->length, GFP_KERNEL);
201
202 if (!new->name || !new->value)
203 goto err_free;
204
205 strcpy(new->value, target_path);
Frank Rowand42b2e942017-10-17 16:36:24 -0700206 strcpy(new->value + target_path_len, symbol_path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700207
Frank Rowandd1651b02017-07-19 09:25:22 -0700208 of_property_set_flag(new, OF_DYNAMIC);
209
210 return new;
211
212 err_free:
213 kfree(new->name);
214 kfree(new->value);
215 kfree(new);
216 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700217}
218
Frank Rowand0290c4c2017-10-17 16:36:23 -0700219/**
220 * add_changeset_property() - add @overlay_prop to overlay changeset
221 * @ovcs: overlay changeset
222 * @target_node: where to place @overlay_prop in live tree
223 * @overlay_prop: property to add or update, from overlay tree
224 * is_symbols_node: 1 if @target_node is "/__symbols__"
225 *
226 * If @overlay_prop does not already exist in @target_node, add changeset entry
227 * to add @overlay_prop in @target_node, else add changeset entry to update
228 * value of @overlay_prop.
229 *
Frank Rowand646afc42017-10-17 16:36:21 -0700230 * Some special properties are not updated (no error returned).
Frank Rowand0290c4c2017-10-17 16:36:23 -0700231 *
Frank Rowand646afc42017-10-17 16:36:21 -0700232 * Update of property in symbols node is not allowed.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700233 *
234 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
235 * invalid @overlay.
Frank Rowand646afc42017-10-17 16:36:21 -0700236 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700237static int add_changeset_property(struct overlay_changeset *ovcs,
238 struct device_node *target_node,
239 struct property *overlay_prop,
Frank Rowandd1651b02017-07-19 09:25:22 -0700240 bool is_symbols_node)
241{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700242 struct property *new_prop = NULL, *prop;
Lixin Wangac0f3e32017-10-16 17:54:32 +0800243 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200244
Frank Rowand0290c4c2017-10-17 16:36:23 -0700245 prop = of_find_property(target_node, overlay_prop->name, NULL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200246
Frank Rowand0290c4c2017-10-17 16:36:23 -0700247 if (!of_prop_cmp(overlay_prop->name, "name") ||
248 !of_prop_cmp(overlay_prop->name, "phandle") ||
249 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200250 return 0;
251
Frank Rowandd1651b02017-07-19 09:25:22 -0700252 if (is_symbols_node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700253 if (prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700254 return -EINVAL;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700255 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
Frank Rowandd1651b02017-07-19 09:25:22 -0700256 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700257 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
Frank Rowandd1651b02017-07-19 09:25:22 -0700258 }
259
Frank Rowand0290c4c2017-10-17 16:36:23 -0700260 if (!new_prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200261 return -ENOMEM;
262
Frank Rowand0290c4c2017-10-17 16:36:23 -0700263 if (!prop)
264 ret = of_changeset_add_property(&ovcs->cset, target_node,
265 new_prop);
Frank Rowand646afc42017-10-17 16:36:21 -0700266 else
Frank Rowand0290c4c2017-10-17 16:36:23 -0700267 ret = of_changeset_update_property(&ovcs->cset, target_node,
268 new_prop);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200269
Lixin Wangac0f3e32017-10-16 17:54:32 +0800270 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700271 kfree(new_prop->name);
272 kfree(new_prop->value);
273 kfree(new_prop);
Lixin Wangac0f3e32017-10-16 17:54:32 +0800274 }
275 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200276}
277
Frank Rowand0290c4c2017-10-17 16:36:23 -0700278/**
279 * add_changeset_node() - add @node (and children) to overlay changeset
280 * @ovcs: overlay changeset
281 * @target_node: where to place @node in live tree
282 * @node: node from within overlay device tree fragment
283 *
284 * If @node does not already exist in @target_node, add changeset entry
285 * to add @node in @target_node.
286 *
287 * If @node already exists in @target_node, and the existing node has
288 * a phandle, the overlay node is not allowed to have a phandle.
289 *
290 * If @node has child nodes, add the children recursively via
291 * build_changeset_next_level().
292 *
293 * NOTE: Multiple mods of created nodes not supported.
Frank Rowand24789c52017-10-17 16:36:26 -0700294 * If more than one fragment contains a node that does not already exist
295 * in the live tree, then for each fragment of_changeset_attach_node()
296 * will add a changeset entry to add the node. When the changeset is
297 * applied, __of_attach_node() will attach the node twice (once for
298 * each fragment). At this point the device tree will be corrupted.
299 *
300 * TODO: add integrity check to ensure that multiple fragments do not
301 * create the same node.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700302 *
303 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
304 * invalid @overlay.
305 */
306static int add_changeset_node(struct overlay_changeset *ovcs,
307 struct device_node *target_node, struct device_node *node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200308{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700309 const char *node_kbasename;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300310 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200311 int ret = 0;
312
Frank Rowand0290c4c2017-10-17 16:36:23 -0700313 node_kbasename = kbasename(node->full_name);
314 if (!node_kbasename)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200315 return -ENOMEM;
316
Frank Rowand0290c4c2017-10-17 16:36:23 -0700317 for_each_child_of_node(target_node, tchild)
318 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700319 break;
320
Frank Rowand61b4de42017-10-17 16:36:25 -0700321 if (!tchild) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700322 tchild = __of_node_dup(node, "%pOF/%s",
323 target_node, node_kbasename);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200324 if (!tchild)
325 return -ENOMEM;
326
Frank Rowand0290c4c2017-10-17 16:36:23 -0700327 tchild->parent = target_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200328
Frank Rowand0290c4c2017-10-17 16:36:23 -0700329 ret = of_changeset_attach_node(&ovcs->cset, tchild);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200330 if (ret)
331 return ret;
332
Frank Rowand61b4de42017-10-17 16:36:25 -0700333 return build_changeset_next_level(ovcs, tchild, node, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200334 }
335
Frank Rowand6d0f5472017-10-17 16:36:28 -0700336 if (node->phandle && tchild->phandle)
337 ret = -EINVAL;
338 else
339 ret = build_changeset_next_level(ovcs, tchild, node, 0);
Frank Rowand61b4de42017-10-17 16:36:25 -0700340 of_node_put(tchild);
341
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200342 return ret;
343}
344
Frank Rowand0290c4c2017-10-17 16:36:23 -0700345/**
346 * build_changeset_next_level() - add level of overlay changeset
347 * @ovcs: overlay changeset
348 * @target_node: where to place @overlay_node in live tree
349 * @overlay_node: node from within an overlay device tree fragment
350 * @is_symbols_node: @overlay_node is node "/__symbols__"
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200351 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700352 * Add the properties (if any) and nodes (if any) from @overlay_node to the
353 * @ovcs->cset changeset. If an added node has child nodes, they will
354 * be added recursively.
Frank Rowand646afc42017-10-17 16:36:21 -0700355 *
356 * Do not allow symbols node to have any children.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700357 *
358 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
359 * invalid @overlay_node.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200360 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700361static int build_changeset_next_level(struct overlay_changeset *ovcs,
362 struct device_node *target_node,
363 const struct device_node *overlay_node,
Frank Rowandd1651b02017-07-19 09:25:22 -0700364 bool is_symbols_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200365{
366 struct device_node *child;
367 struct property *prop;
368 int ret;
369
Frank Rowand0290c4c2017-10-17 16:36:23 -0700370 for_each_property_of_node(overlay_node, prop) {
371 ret = add_changeset_property(ovcs, target_node, prop,
372 is_symbols_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200373 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700374 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
375 target_node, prop->name, ret);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200376 return ret;
377 }
378 }
379
Frank Rowandd1651b02017-07-19 09:25:22 -0700380 if (is_symbols_node)
381 return 0;
382
Frank Rowand0290c4c2017-10-17 16:36:23 -0700383 for_each_child_of_node(overlay_node, child) {
384 ret = add_changeset_node(ovcs, target_node, child);
Frank Rowandbbed8792017-10-17 16:36:22 -0700385 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700386 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
387 target_node, child->name, ret);
Julia Lawall001cf502015-10-22 11:02:48 +0200388 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200389 return ret;
390 }
391 }
392
393 return 0;
394}
395
396/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700397 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
398 * @ovcs: Overlay changeset
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200399 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700400 * Create changeset @ovcs->cset to contain the nodes and properties of the
401 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
402 * any portions of the changeset that were successfully created will remain
403 * in @ovcs->cset.
404 *
405 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
406 * invalid overlay in @ovcs->fragments[].
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200407 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700408static int build_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200409{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700410 int i, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200411
Frank Rowand0290c4c2017-10-17 16:36:23 -0700412 for (i = 0; i < ovcs->count; i++) {
413 struct fragment *fragment = &ovcs->fragments[i];
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200414
Frank Rowand0290c4c2017-10-17 16:36:23 -0700415 ret = build_changeset_next_level(ovcs, fragment->target,
416 fragment->overlay,
417 fragment->is_symbols_node);
418 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700419 pr_debug("apply failed '%pOF'\n", fragment->target);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700420 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200421 }
422 }
423
424 return 0;
425}
426
427/*
428 * Find the target node using a number of different strategies
Frank Rowand646afc42017-10-17 16:36:21 -0700429 * in order of preference:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200430 *
Frank Rowand646afc42017-10-17 16:36:21 -0700431 * 1) "target" property containing the phandle of the target
432 * 2) "target-path" property containing the path of the target
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200433 */
434static struct device_node *find_target_node(struct device_node *info_node)
435{
436 const char *path;
437 u32 val;
438 int ret;
439
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200440 ret = of_property_read_u32(info_node, "target", &val);
Frank Rowandbbed8792017-10-17 16:36:22 -0700441 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200442 return of_find_node_by_phandle(val);
443
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200444 ret = of_property_read_string(info_node, "target-path", &path);
Frank Rowandbbed8792017-10-17 16:36:22 -0700445 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200446 return of_find_node_by_path(path);
447
Rob Herring606ad422016-06-15 08:32:18 -0500448 pr_err("Failed to find target for node %p (%s)\n",
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200449 info_node, info_node->name);
450
451 return NULL;
452}
453
454/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700455 * init_overlay_changeset() - initialize overlay changeset from overlay tree
456 * @ovcs Overlay changeset to build
457 * @tree: Contains all the overlay fragments and overlay fixup nodes
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200458 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700459 * Initialize @ovcs. Populate @ovcs->fragments with node information from
460 * the top level of @tree. The relevant top level nodes are the fragment
461 * nodes and the __symbols__ node. Any other top level node will be ignored.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200462 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700463 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
Frank Rowand61b4de42017-10-17 16:36:25 -0700464 * detected in @tree, or -ENOSPC if idr_alloc() error.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200465 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700466static int init_overlay_changeset(struct overlay_changeset *ovcs,
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200467 struct device_node *tree)
468{
Frank Rowand61b4de42017-10-17 16:36:25 -0700469 struct device_node *node, *overlay_node;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700470 struct fragment *fragment;
471 struct fragment *fragments;
472 int cnt, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200473
Frank Rowand24789c52017-10-17 16:36:26 -0700474 /*
475 * Warn for some issues. Can not return -EINVAL for these until
476 * of_unittest_apply_overlay() is fixed to pass these checks.
477 */
478 if (!of_node_check_flag(tree, OF_DYNAMIC))
479 pr_debug("%s() tree is not dynamic\n", __func__);
480
481 if (!of_node_check_flag(tree, OF_DETACHED))
482 pr_debug("%s() tree is not detached\n", __func__);
483
484 if (!of_node_is_root(tree))
485 pr_debug("%s() tree is not root\n", __func__);
486
Frank Rowand61b4de42017-10-17 16:36:25 -0700487 INIT_LIST_HEAD(&ovcs->ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200488
Frank Rowand61b4de42017-10-17 16:36:25 -0700489 of_changeset_init(&ovcs->cset);
490
491 ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
492 if (ovcs->id <= 0)
493 return ovcs->id;
494
495 cnt = 0;
496
497 /* fragment nodes */
498 for_each_child_of_node(tree, node) {
499 overlay_node = of_get_child_by_name(node, "__overlay__");
500 if (overlay_node) {
501 cnt++;
502 of_node_put(overlay_node);
503 }
504 }
505
506 node = of_get_child_by_name(tree, "__symbols__");
507 if (node) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700508 cnt++;
Frank Rowand61b4de42017-10-17 16:36:25 -0700509 of_node_put(node);
510 }
Frank Rowandd1651b02017-07-19 09:25:22 -0700511
Frank Rowand0290c4c2017-10-17 16:36:23 -0700512 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
Frank Rowand61b4de42017-10-17 16:36:25 -0700513 if (!fragments) {
514 ret = -ENOMEM;
515 goto err_free_idr;
516 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200517
518 cnt = 0;
519 for_each_child_of_node(tree, node) {
Frank Rowand61b4de42017-10-17 16:36:25 -0700520 fragment = &fragments[cnt];
521 fragment->overlay = of_get_child_by_name(node, "__overlay__");
522 if (fragment->overlay) {
523 fragment->target = find_target_node(node);
524 if (!fragment->target) {
525 of_node_put(fragment->overlay);
526 ret = -EINVAL;
527 goto err_free_fragments;
528 } else {
529 cnt++;
530 }
531 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200532 }
533
Frank Rowandd1651b02017-07-19 09:25:22 -0700534 node = of_get_child_by_name(tree, "__symbols__");
535 if (node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700536 fragment = &fragments[cnt];
537 fragment->overlay = node;
538 fragment->target = of_find_node_by_path("/__symbols__");
539 fragment->is_symbols_node = 1;
Frank Rowandd1651b02017-07-19 09:25:22 -0700540
Frank Rowand0290c4c2017-10-17 16:36:23 -0700541 if (!fragment->target) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700542 pr_err("no symbols in root of device tree.\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700543 ret = -EINVAL;
544 goto err_free_fragments;
Frank Rowandd1651b02017-07-19 09:25:22 -0700545 }
546
547 cnt++;
548 }
549
Frank Rowandbbed8792017-10-17 16:36:22 -0700550 if (!cnt) {
Frank Rowand61b4de42017-10-17 16:36:25 -0700551 ret = -EINVAL;
552 goto err_free_fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200553 }
554
Frank Rowand0290c4c2017-10-17 16:36:23 -0700555 ovcs->count = cnt;
556 ovcs->fragments = fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200557
558 return 0;
Frank Rowand61b4de42017-10-17 16:36:25 -0700559
Frank Rowand61b4de42017-10-17 16:36:25 -0700560err_free_fragments:
561 kfree(fragments);
562err_free_idr:
563 idr_remove(&ovcs_idr, ovcs->id);
564
Frank Rowand24789c52017-10-17 16:36:26 -0700565 pr_err("%s() failed, ret = %d\n", __func__, ret);
566
Frank Rowand61b4de42017-10-17 16:36:25 -0700567 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200568}
569
Frank Rowand61b4de42017-10-17 16:36:25 -0700570static void free_overlay_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200571{
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200572 int i;
573
Frank Rowand61b4de42017-10-17 16:36:25 -0700574 if (!ovcs->cset.entries.next)
575 return;
576 of_changeset_destroy(&ovcs->cset);
577
578 if (ovcs->id)
579 idr_remove(&ovcs_idr, ovcs->id);
580
581 for (i = 0; i < ovcs->count; i++) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700582 of_node_put(ovcs->fragments[i].target);
583 of_node_put(ovcs->fragments[i].overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200584 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700585 kfree(ovcs->fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200586
Frank Rowand61b4de42017-10-17 16:36:25 -0700587 kfree(ovcs);
588}
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200589
590/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700591 * of_overlay_apply() - Create and apply an overlay changeset
592 * @tree: Expanded overlay device tree
Frank Rowand24789c52017-10-17 16:36:26 -0700593 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200594 *
Frank Rowand24789c52017-10-17 16:36:26 -0700595 * Creates and applies an overlay changeset.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200596 *
Frank Rowand24789c52017-10-17 16:36:26 -0700597 * If an error occurs in a pre-apply notifier, then no changes are made
598 * to the device tree.
599 *
600
601 * A non-zero return value will not have created the changeset if error is from:
602 * - parameter checks
603 * - building the changeset
604 * - overlay changset pre-apply notifier
605 *
606 * If an error is returned by an overlay changeset pre-apply notifier
607 * then no further overlay changeset pre-apply notifier will be called.
608 *
609 * A non-zero return value will have created the changeset if error is from:
610 * - overlay changeset entry notifier
611 * - overlay changset post-apply notifier
612 *
613 * If an error is returned by an overlay changeset post-apply notifier
614 * then no further overlay changeset post-apply notifier will be called.
615 *
616 * If more than one notifier returns an error, then the last notifier
617 * error to occur is returned.
618 *
619 * If an error occurred while applying the overlay changeset, then an
620 * attempt is made to revert any changes that were made to the
621 * device tree. If there were any errors during the revert attempt
622 * then the state of the device tree can not be determined, and any
623 * following attempt to apply or remove an overlay changeset will be
624 * refused.
625 *
626 * Returns 0 on success, or a negative error number. Overlay changeset
627 * id is returned to *ovcs_id.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200628 */
Frank Rowand24789c52017-10-17 16:36:26 -0700629
630int of_overlay_apply(struct device_node *tree, int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200631{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700632 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700633 int ret = 0, ret_revert, ret_tmp;
634
635 *ovcs_id = 0;
636
637 if (devicetree_corrupt()) {
638 pr_err("devicetree state suspect, refuse to apply overlay\n");
639 ret = -EBUSY;
640 goto out;
641 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200642
Frank Rowand0290c4c2017-10-17 16:36:23 -0700643 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
Frank Rowand24789c52017-10-17 16:36:26 -0700644 if (!ovcs) {
645 ret = -ENOMEM;
646 goto out;
647 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200648
Frank Rowandf948d6d2017-10-17 16:36:29 -0700649 of_overlay_mutex_lock();
650
651 ret = of_resolve_phandles(tree);
652 if (ret)
653 goto err_overlay_unlock;
654
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200655 mutex_lock(&of_mutex);
656
Frank Rowand0290c4c2017-10-17 16:36:23 -0700657 ret = init_overlay_changeset(ovcs, tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700658 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700659 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200660
Frank Rowand0290c4c2017-10-17 16:36:23 -0700661 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
Frank Rowand24789c52017-10-17 16:36:26 -0700662 if (ret) {
663 pr_err("overlay changeset pre-apply notify error %d\n", ret);
Frank Rowand61b4de42017-10-17 16:36:25 -0700664 goto err_free_overlay_changeset;
Alan Tull39a842e2016-11-01 14:14:22 -0500665 }
666
Frank Rowand0290c4c2017-10-17 16:36:23 -0700667 ret = build_changeset(ovcs);
668 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700669 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200670
Frank Rowand24789c52017-10-17 16:36:26 -0700671 ret_revert = 0;
672 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
673 if (ret) {
674 if (ret_revert) {
675 pr_debug("overlay changeset revert error %d\n",
676 ret_revert);
677 devicetree_state_flags |= DTSF_APPLY_FAIL;
678 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700679 goto err_free_overlay_changeset;
Frank Rowand24789c52017-10-17 16:36:26 -0700680 } else {
681 ret = __of_changeset_apply_notify(&ovcs->cset);
682 if (ret)
683 pr_err("overlay changeset entry notify error %d\n",
684 ret);
685 /* fall through */
686 }
Rob Herring606ad422016-06-15 08:32:18 -0500687
Frank Rowand0290c4c2017-10-17 16:36:23 -0700688 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
Frank Rowand24789c52017-10-17 16:36:26 -0700689 *ovcs_id = ovcs->id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200690
Frank Rowand24789c52017-10-17 16:36:26 -0700691 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
692 if (ret_tmp) {
693 pr_err("overlay changeset post-apply notify error %d\n",
694 ret_tmp);
695 if (!ret)
696 ret = ret_tmp;
697 }
Alan Tull39a842e2016-11-01 14:14:22 -0500698
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200699 mutex_unlock(&of_mutex);
Frank Rowandf948d6d2017-10-17 16:36:29 -0700700 of_overlay_mutex_unlock();
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200701
Frank Rowand24789c52017-10-17 16:36:26 -0700702 goto out;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200703
Frank Rowandf948d6d2017-10-17 16:36:29 -0700704err_overlay_unlock:
705 of_overlay_mutex_unlock();
706
Frank Rowand61b4de42017-10-17 16:36:25 -0700707err_free_overlay_changeset:
708 free_overlay_changeset(ovcs);
709
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200710 mutex_unlock(&of_mutex);
711
Frank Rowand24789c52017-10-17 16:36:26 -0700712out:
713 pr_debug("%s() err=%d\n", __func__, ret);
714
Frank Rowand0290c4c2017-10-17 16:36:23 -0700715 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200716}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700717EXPORT_SYMBOL_GPL(of_overlay_apply);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200718
Frank Rowand646afc42017-10-17 16:36:21 -0700719/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700720 * Find @np in @tree.
721 *
722 * Returns 1 if @np is @tree or is contained in @tree, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700723 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700724static int find_node(struct device_node *tree, struct device_node *np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200725{
726 struct device_node *child;
727
Frank Rowand0290c4c2017-10-17 16:36:23 -0700728 if (tree == np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200729 return 1;
730
731 for_each_child_of_node(tree, child) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700732 if (find_node(child, np)) {
Julia Lawall001cf502015-10-22 11:02:48 +0200733 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200734 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200735 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200736 }
737
738 return 0;
739}
740
Frank Rowand646afc42017-10-17 16:36:21 -0700741/*
Frank Rowand87f242c2017-10-17 16:36:27 -0700742 * Is @remove_ce_node a child of, a parent of, or the same as any
Frank Rowand0290c4c2017-10-17 16:36:23 -0700743 * node in an overlay changeset more topmost than @remove_ovcs?
744 *
745 * Returns 1 if found, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700746 */
Frank Rowand87f242c2017-10-17 16:36:27 -0700747static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
748 struct device_node *remove_ce_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200749{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700750 struct overlay_changeset *ovcs;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200751 struct of_changeset_entry *ce;
752
Frank Rowand0290c4c2017-10-17 16:36:23 -0700753 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
754 if (ovcs == remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200755 break;
756
Frank Rowand0290c4c2017-10-17 16:36:23 -0700757 list_for_each_entry(ce, &ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700758 if (find_node(ce->np, remove_ce_node)) {
759 pr_err("%s: #%d overlaps with #%d @%pOF\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700760 __func__, remove_ovcs->id, ovcs->id,
Frank Rowand87f242c2017-10-17 16:36:27 -0700761 remove_ce_node);
762 return 1;
763 }
764 if (find_node(remove_ce_node, ce->np)) {
765 pr_err("%s: #%d overlaps with #%d @%pOF\n",
766 __func__, remove_ovcs->id, ovcs->id,
767 remove_ce_node);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700768 return 1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200769 }
770 }
771 }
772
Frank Rowand0290c4c2017-10-17 16:36:23 -0700773 return 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200774}
775
776/*
777 * We can safely remove the overlay only if it's the top-most one.
778 * Newly applied overlays are inserted at the tail of the overlay list,
779 * so a top most overlay is the one that is closest to the tail.
780 *
781 * The topmost check is done by exploiting this property. For each
782 * affected device node in the log list we check if this overlay is
783 * the one closest to the tail. If another overlay has affected this
784 * device node and is closest to the tail, then removal is not permited.
785 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700786static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200787{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700788 struct of_changeset_entry *remove_ce;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200789
Frank Rowand0290c4c2017-10-17 16:36:23 -0700790 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700791 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700792 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200793 return 0;
794 }
795 }
796
797 return 1;
798}
799
800/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700801 * of_overlay_remove() - Revert and free an overlay changeset
Frank Rowand24789c52017-10-17 16:36:26 -0700802 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200803 *
Frank Rowand24789c52017-10-17 16:36:26 -0700804 * Removes an overlay if it is permissible. @ovcs_id was previously returned
Frank Rowand0290c4c2017-10-17 16:36:23 -0700805 * by of_overlay_apply().
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200806 *
Frank Rowand24789c52017-10-17 16:36:26 -0700807 * If an error occurred while attempting to revert the overlay changeset,
808 * then an attempt is made to re-apply any changeset entry that was
809 * reverted. If an error occurs on re-apply then the state of the device
810 * tree can not be determined, and any following attempt to apply or remove
811 * an overlay changeset will be refused.
812 *
813 * A non-zero return value will not revert the changeset if error is from:
814 * - parameter checks
815 * - overlay changset pre-remove notifier
816 * - overlay changeset entry revert
817 *
818 * If an error is returned by an overlay changeset pre-remove notifier
819 * then no further overlay changeset pre-remove notifier will be called.
820 *
821 * If more than one notifier returns an error, then the last notifier
822 * error to occur is returned.
823 *
824 * A non-zero return value will revert the changeset if error is from:
825 * - overlay changeset entry notifier
826 * - overlay changset post-remove notifier
827 *
828 * If an error is returned by an overlay changeset post-remove notifier
829 * then no further overlay changeset post-remove notifier will be called.
830 *
831 * Returns 0 on success, or a negative error number. *ovcs_id is set to
832 * zero after reverting the changeset, even if a subsequent error occurs.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200833 */
Frank Rowand24789c52017-10-17 16:36:26 -0700834int of_overlay_remove(int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200835{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700836 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700837 int ret, ret_apply, ret_tmp;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200838
Frank Rowand24789c52017-10-17 16:36:26 -0700839 ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200840
Frank Rowand24789c52017-10-17 16:36:26 -0700841 if (devicetree_corrupt()) {
842 pr_err("suspect devicetree state, refuse to remove overlay\n");
Frank Rowand0290c4c2017-10-17 16:36:23 -0700843 ret = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200844 goto out;
845 }
846
Frank Rowand24789c52017-10-17 16:36:26 -0700847 mutex_lock(&of_mutex);
848
849 ovcs = idr_find(&ovcs_idr, *ovcs_id);
850 if (!ovcs) {
851 ret = -ENODEV;
852 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
853 goto out_unlock;
854 }
855
856 if (!overlay_removal_is_ok(ovcs)) {
857 ret = -EBUSY;
858 goto out_unlock;
859 }
860
861 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
862 if (ret) {
863 pr_err("overlay changeset pre-remove notify error %d\n", ret);
864 goto out_unlock;
865 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700866
Frank Rowand0290c4c2017-10-17 16:36:23 -0700867 list_del(&ovcs->ovcs_list);
Frank Rowand61b4de42017-10-17 16:36:25 -0700868
Frank Rowand24789c52017-10-17 16:36:26 -0700869 ret_apply = 0;
870 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
871 if (ret) {
872 if (ret_apply)
873 devicetree_state_flags |= DTSF_REVERT_FAIL;
874 goto out_unlock;
875 } else {
876 ret = __of_changeset_revert_notify(&ovcs->cset);
877 if (ret) {
878 pr_err("overlay changeset entry notify error %d\n",
879 ret);
880 /* fall through - changeset was reverted */
881 }
882 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700883
Frank Rowand24789c52017-10-17 16:36:26 -0700884 *ovcs_id = 0;
885
886 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
887 if (ret_tmp) {
888 pr_err("overlay changeset post-remove notify error %d\n",
889 ret_tmp);
890 if (!ret)
891 ret = ret_tmp;
892 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700893
894 free_overlay_changeset(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200895
Frank Rowand24789c52017-10-17 16:36:26 -0700896out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200897 mutex_unlock(&of_mutex);
898
Frank Rowand24789c52017-10-17 16:36:26 -0700899out:
900 pr_debug("%s() err=%d\n", __func__, ret);
901
Frank Rowand0290c4c2017-10-17 16:36:23 -0700902 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200903}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700904EXPORT_SYMBOL_GPL(of_overlay_remove);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200905
906/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700907 * of_overlay_remove_all() - Reverts and frees all overlay changesets
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200908 *
909 * Removes all overlays from the system in the correct order.
910 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200911 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200912 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700913int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200914{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700915 struct overlay_changeset *ovcs, *ovcs_n;
Frank Rowand61b4de42017-10-17 16:36:25 -0700916 int ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200917
918 /* the tail of list is guaranteed to be safe to remove */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700919 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
Frank Rowand24789c52017-10-17 16:36:26 -0700920 ret = of_overlay_remove(&ovcs->id);
Frank Rowand61b4de42017-10-17 16:36:25 -0700921 if (ret)
922 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200923 }
924
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200925 return 0;
926}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700927EXPORT_SYMBOL_GPL(of_overlay_remove_all);