blob: 3397d764295849fa2c0c753c28e036a514171d83 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02002/*
3 * Functions for working with device tree overlays
4 *
5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
6 * Copyright (C) 2012 Texas Instruments Inc.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02007 */
Rob Herring606ad422016-06-15 08:32:18 -05008
9#define pr_fmt(fmt) "OF: overlay: " fmt
10
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_device.h>
15#include <linux/string.h>
16#include <linux/ctype.h>
17#include <linux/errno.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020018#include <linux/slab.h>
19#include <linux/err.h>
Mark Brown0d1886d2015-02-17 11:36:58 +090020#include <linux/idr.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020021
22#include "of_private.h"
23
24/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070025 * struct fragment - info about fragment nodes in overlay expanded device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020026 * @target: target of the overlay operation
Frank Rowand0290c4c2017-10-17 16:36:23 -070027 * @overlay: pointer to the __overlay__ node
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020028 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070029struct fragment {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020030 struct device_node *target;
31 struct device_node *overlay;
32};
33
34/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070035 * struct overlay_changeset
Frank Rowand3912b792017-10-17 16:36:30 -070036 * @ovcs_list: list on which we are located
Frank Rowande0a58f32017-10-17 16:36:31 -070037 * @overlay_tree: expanded device tree that contains the fragment nodes
Frank Rowand3912b792017-10-17 16:36:30 -070038 * @count: count of fragment structures
39 * @fragments: fragment nodes in the overlay expanded device tree
40 * @symbols_fragment: last element of @fragments[] is the __symbols__ node
41 * @cset: changeset to apply fragments to live device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020042 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070043struct overlay_changeset {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020044 int id;
Frank Rowand0290c4c2017-10-17 16:36:23 -070045 struct list_head ovcs_list;
Frank Rowande0a58f32017-10-17 16:36:31 -070046 struct device_node *overlay_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020047 int count;
Frank Rowand0290c4c2017-10-17 16:36:23 -070048 struct fragment *fragments;
Frank Rowand3912b792017-10-17 16:36:30 -070049 bool symbols_fragment;
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,
Frank Rowand3912b792017-10-17 16:36:30 -070071 const struct device_node *overlay_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020072
Frank Rowandf948d6d2017-10-17 16:36:29 -070073/*
74 * of_resolve_phandles() finds the largest phandle in the live tree.
75 * of_overlay_apply() may add a larger phandle to the live tree.
76 * Do not allow race between two overlays being applied simultaneously:
77 * mutex_lock(&of_overlay_phandle_mutex)
78 * of_resolve_phandles()
79 * of_overlay_apply()
80 * mutex_unlock(&of_overlay_phandle_mutex)
81 */
82static DEFINE_MUTEX(of_overlay_phandle_mutex);
83
84void of_overlay_mutex_lock(void)
85{
86 mutex_lock(&of_overlay_phandle_mutex);
87}
88
89void of_overlay_mutex_unlock(void)
90{
91 mutex_unlock(&of_overlay_phandle_mutex);
92}
93
94
Frank Rowand61b4de42017-10-17 16:36:25 -070095static LIST_HEAD(ovcs_list);
96static DEFINE_IDR(ovcs_idr);
97
Frank Rowand0290c4c2017-10-17 16:36:23 -070098static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
Alan Tull39a842e2016-11-01 14:14:22 -050099
100int of_overlay_notifier_register(struct notifier_block *nb)
101{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700102 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500103}
104EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
105
106int of_overlay_notifier_unregister(struct notifier_block *nb)
107{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700108 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500109}
110EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
111
Frank Rowand24789c52017-10-17 16:36:26 -0700112static char *of_overlay_action_name[] = {
113 "pre-apply",
114 "post-apply",
115 "pre-remove",
116 "post-remove",
117};
118
Frank Rowand0290c4c2017-10-17 16:36:23 -0700119static int overlay_notify(struct overlay_changeset *ovcs,
120 enum of_overlay_notify_action action)
Alan Tull39a842e2016-11-01 14:14:22 -0500121{
122 struct of_overlay_notify_data nd;
123 int i, ret;
124
Frank Rowand0290c4c2017-10-17 16:36:23 -0700125 for (i = 0; i < ovcs->count; i++) {
126 struct fragment *fragment = &ovcs->fragments[i];
Alan Tull39a842e2016-11-01 14:14:22 -0500127
Frank Rowand0290c4c2017-10-17 16:36:23 -0700128 nd.target = fragment->target;
129 nd.overlay = fragment->overlay;
Alan Tull39a842e2016-11-01 14:14:22 -0500130
Frank Rowand0290c4c2017-10-17 16:36:23 -0700131 ret = blocking_notifier_call_chain(&overlay_notify_chain,
Alan Tull39a842e2016-11-01 14:14:22 -0500132 action, &nd);
Frank Rowanda1d19bd2017-10-19 14:18:27 -0700133 if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
Frank Rowand24789c52017-10-17 16:36:26 -0700134 return 0;
135 if (ret) {
136 ret = notifier_to_errno(ret);
137 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
138 of_overlay_action_name[action], ret, nd.target);
139 return ret;
140 }
Alan Tull39a842e2016-11-01 14:14:22 -0500141 }
142
143 return 0;
144}
145
Frank Rowand42b2e942017-10-17 16:36:24 -0700146/*
Frank Rowande0a58f32017-10-17 16:36:31 -0700147 * The values of properties in the "/__symbols__" node are paths in
148 * the ovcs->overlay_tree. When duplicating the properties, the paths
149 * need to be adjusted to be the correct path for the live device tree.
Frank Rowand42b2e942017-10-17 16:36:24 -0700150 *
Frank Rowande0a58f32017-10-17 16:36:31 -0700151 * The paths refer to a node in the subtree of a fragment node's "__overlay__"
152 * node, for example "/fragment@0/__overlay__/symbol_path_tail",
153 * where symbol_path_tail can be a single node or it may be a multi-node path.
Frank Rowand42b2e942017-10-17 16:36:24 -0700154 *
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 Rowande0a58f32017-10-17 16:36:31 -0700163 struct property *new_prop;
164 struct device_node *fragment_node;
165 struct device_node *overlay_node;
166 const char *path;
167 const char *path_tail;
Frank Rowandd1651b02017-07-19 09:25:22 -0700168 const char *target_path;
169 int k;
Frank Rowandd1651b02017-07-19 09:25:22 -0700170 int overlay_name_len;
Frank Rowande0a58f32017-10-17 16:36:31 -0700171 int path_len;
172 int path_tail_len;
Frank Rowandd1651b02017-07-19 09:25:22 -0700173 int target_path_len;
174
175 if (!prop->value)
176 return NULL;
Frank Rowande0a58f32017-10-17 16:36:31 -0700177 if (strnlen(prop->value, prop->length) >= prop->length)
Frank Rowandd1651b02017-07-19 09:25:22 -0700178 return NULL;
Frank Rowande0a58f32017-10-17 16:36:31 -0700179 path = prop->value;
180 path_len = strlen(path);
181
182 if (path_len < 1)
183 return NULL;
184 fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
185 overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
186 of_node_put(fragment_node);
187 of_node_put(overlay_node);
Frank Rowandd1651b02017-07-19 09:25:22 -0700188
Frank Rowand0290c4c2017-10-17 16:36:23 -0700189 for (k = 0; k < ovcs->count; k++) {
190 fragment = &ovcs->fragments[k];
Frank Rowande0a58f32017-10-17 16:36:31 -0700191 if (fragment->overlay == overlay_node)
Frank Rowandd1651b02017-07-19 09:25:22 -0700192 break;
193 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700194 if (k >= ovcs->count)
Frank Rowande0a58f32017-10-17 16:36:31 -0700195 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700196
Frank Rowande0a58f32017-10-17 16:36:31 -0700197 overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
198
199 if (overlay_name_len > path_len)
200 return NULL;
201 path_tail = path + overlay_name_len;
202 path_tail_len = strlen(path_tail);
203
204 target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
205 if (!target_path)
206 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700207 target_path_len = strlen(target_path);
208
Frank Rowande0a58f32017-10-17 16:36:31 -0700209 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
210 if (!new_prop)
211 goto err_free_target_path;
Frank Rowandd1651b02017-07-19 09:25:22 -0700212
Frank Rowande0a58f32017-10-17 16:36:31 -0700213 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
214 new_prop->length = target_path_len + path_tail_len + 1;
215 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
216 if (!new_prop->name || !new_prop->value)
217 goto err_free_new_prop;
Frank Rowandd1651b02017-07-19 09:25:22 -0700218
Frank Rowande0a58f32017-10-17 16:36:31 -0700219 strcpy(new_prop->value, target_path);
220 strcpy(new_prop->value + target_path_len, path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700221
Frank Rowande0a58f32017-10-17 16:36:31 -0700222 of_property_set_flag(new_prop, OF_DYNAMIC);
Frank Rowandd1651b02017-07-19 09:25:22 -0700223
Frank Rowande0a58f32017-10-17 16:36:31 -0700224 return new_prop;
Frank Rowandd1651b02017-07-19 09:25:22 -0700225
Frank Rowande0a58f32017-10-17 16:36:31 -0700226err_free_new_prop:
227 kfree(new_prop->name);
228 kfree(new_prop->value);
229 kfree(new_prop);
230err_free_target_path:
231 kfree(target_path);
Frank Rowandd1651b02017-07-19 09:25:22 -0700232
Frank Rowandd1651b02017-07-19 09:25:22 -0700233 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700234}
235
Frank Rowand0290c4c2017-10-17 16:36:23 -0700236/**
237 * add_changeset_property() - add @overlay_prop to overlay changeset
238 * @ovcs: overlay changeset
239 * @target_node: where to place @overlay_prop in live tree
240 * @overlay_prop: property to add or update, from overlay tree
Frank Rowand3912b792017-10-17 16:36:30 -0700241 * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
Frank Rowand0290c4c2017-10-17 16:36:23 -0700242 *
243 * If @overlay_prop does not already exist in @target_node, add changeset entry
244 * to add @overlay_prop in @target_node, else add changeset entry to update
245 * value of @overlay_prop.
246 *
Frank Rowand646afc42017-10-17 16:36:21 -0700247 * Some special properties are not updated (no error returned).
Frank Rowand0290c4c2017-10-17 16:36:23 -0700248 *
Frank Rowand646afc42017-10-17 16:36:21 -0700249 * Update of property in symbols node is not allowed.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700250 *
251 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
252 * invalid @overlay.
Frank Rowand646afc42017-10-17 16:36:21 -0700253 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700254static int add_changeset_property(struct overlay_changeset *ovcs,
255 struct device_node *target_node,
256 struct property *overlay_prop,
Frank Rowand3912b792017-10-17 16:36:30 -0700257 bool is_symbols_prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700258{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700259 struct property *new_prop = NULL, *prop;
Lixin Wangac0f3e32017-10-16 17:54:32 +0800260 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200261
Frank Rowand0290c4c2017-10-17 16:36:23 -0700262 prop = of_find_property(target_node, overlay_prop->name, NULL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200263
Frank Rowand0290c4c2017-10-17 16:36:23 -0700264 if (!of_prop_cmp(overlay_prop->name, "name") ||
265 !of_prop_cmp(overlay_prop->name, "phandle") ||
266 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200267 return 0;
268
Frank Rowand3912b792017-10-17 16:36:30 -0700269 if (is_symbols_prop) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700270 if (prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700271 return -EINVAL;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700272 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
Frank Rowandd1651b02017-07-19 09:25:22 -0700273 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700274 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
Frank Rowandd1651b02017-07-19 09:25:22 -0700275 }
276
Frank Rowand0290c4c2017-10-17 16:36:23 -0700277 if (!new_prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200278 return -ENOMEM;
279
Frank Rowand0290c4c2017-10-17 16:36:23 -0700280 if (!prop)
281 ret = of_changeset_add_property(&ovcs->cset, target_node,
282 new_prop);
Frank Rowand646afc42017-10-17 16:36:21 -0700283 else
Frank Rowand0290c4c2017-10-17 16:36:23 -0700284 ret = of_changeset_update_property(&ovcs->cset, target_node,
285 new_prop);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200286
Lixin Wangac0f3e32017-10-16 17:54:32 +0800287 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700288 kfree(new_prop->name);
289 kfree(new_prop->value);
290 kfree(new_prop);
Lixin Wangac0f3e32017-10-16 17:54:32 +0800291 }
292 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200293}
294
Frank Rowand0290c4c2017-10-17 16:36:23 -0700295/**
296 * add_changeset_node() - add @node (and children) to overlay changeset
297 * @ovcs: overlay changeset
298 * @target_node: where to place @node in live tree
299 * @node: node from within overlay device tree fragment
300 *
301 * If @node does not already exist in @target_node, add changeset entry
302 * to add @node in @target_node.
303 *
304 * If @node already exists in @target_node, and the existing node has
305 * a phandle, the overlay node is not allowed to have a phandle.
306 *
307 * If @node has child nodes, add the children recursively via
308 * build_changeset_next_level().
309 *
310 * NOTE: Multiple mods of created nodes not supported.
Frank Rowand24789c52017-10-17 16:36:26 -0700311 * If more than one fragment contains a node that does not already exist
312 * in the live tree, then for each fragment of_changeset_attach_node()
313 * will add a changeset entry to add the node. When the changeset is
314 * applied, __of_attach_node() will attach the node twice (once for
315 * each fragment). At this point the device tree will be corrupted.
316 *
317 * TODO: add integrity check to ensure that multiple fragments do not
318 * create the same node.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700319 *
320 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
321 * invalid @overlay.
322 */
323static int add_changeset_node(struct overlay_changeset *ovcs,
324 struct device_node *target_node, struct device_node *node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200325{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700326 const char *node_kbasename;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300327 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200328 int ret = 0;
329
Frank Rowand0290c4c2017-10-17 16:36:23 -0700330 node_kbasename = kbasename(node->full_name);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200331
Frank Rowand0290c4c2017-10-17 16:36:23 -0700332 for_each_child_of_node(target_node, tchild)
333 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700334 break;
335
Frank Rowand61b4de42017-10-17 16:36:25 -0700336 if (!tchild) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700337 tchild = __of_node_dup(node, "%pOF/%s",
338 target_node, node_kbasename);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200339 if (!tchild)
340 return -ENOMEM;
341
Frank Rowand0290c4c2017-10-17 16:36:23 -0700342 tchild->parent = target_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200343
Frank Rowand0290c4c2017-10-17 16:36:23 -0700344 ret = of_changeset_attach_node(&ovcs->cset, tchild);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200345 if (ret)
346 return ret;
347
Frank Rowand3912b792017-10-17 16:36:30 -0700348 return build_changeset_next_level(ovcs, tchild, node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200349 }
350
Frank Rowand6d0f5472017-10-17 16:36:28 -0700351 if (node->phandle && tchild->phandle)
352 ret = -EINVAL;
353 else
Frank Rowand3912b792017-10-17 16:36:30 -0700354 ret = build_changeset_next_level(ovcs, tchild, node);
Frank Rowand61b4de42017-10-17 16:36:25 -0700355 of_node_put(tchild);
356
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200357 return ret;
358}
359
Frank Rowand0290c4c2017-10-17 16:36:23 -0700360/**
361 * build_changeset_next_level() - add level of overlay changeset
362 * @ovcs: overlay changeset
363 * @target_node: where to place @overlay_node in live tree
364 * @overlay_node: node from within an overlay device tree fragment
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200365 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700366 * Add the properties (if any) and nodes (if any) from @overlay_node to the
367 * @ovcs->cset changeset. If an added node has child nodes, they will
368 * be added recursively.
Frank Rowand646afc42017-10-17 16:36:21 -0700369 *
370 * Do not allow symbols node to have any children.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700371 *
372 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
373 * invalid @overlay_node.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200374 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700375static int build_changeset_next_level(struct overlay_changeset *ovcs,
376 struct device_node *target_node,
Frank Rowand3912b792017-10-17 16:36:30 -0700377 const struct device_node *overlay_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200378{
379 struct device_node *child;
380 struct property *prop;
381 int ret;
382
Frank Rowand0290c4c2017-10-17 16:36:23 -0700383 for_each_property_of_node(overlay_node, prop) {
Frank Rowand3912b792017-10-17 16:36:30 -0700384 ret = add_changeset_property(ovcs, target_node, prop, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200385 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700386 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
387 target_node, prop->name, ret);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200388 return ret;
389 }
390 }
391
Frank Rowand0290c4c2017-10-17 16:36:23 -0700392 for_each_child_of_node(overlay_node, child) {
393 ret = add_changeset_node(ovcs, target_node, child);
Frank Rowandbbed8792017-10-17 16:36:22 -0700394 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700395 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
396 target_node, child->name, ret);
Julia Lawall001cf502015-10-22 11:02:48 +0200397 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200398 return ret;
399 }
400 }
401
402 return 0;
403}
404
Frank Rowand3912b792017-10-17 16:36:30 -0700405/*
406 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
407 */
408static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
409 struct device_node *target_node,
410 const struct device_node *overlay_symbols_node)
411{
412 struct property *prop;
413 int ret;
414
415 for_each_property_of_node(overlay_symbols_node, prop) {
416 ret = add_changeset_property(ovcs, target_node, prop, 1);
417 if (ret) {
418 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
419 target_node, prop->name, ret);
420 return ret;
421 }
422 }
423
424 return 0;
425}
426
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200427/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700428 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
429 * @ovcs: Overlay changeset
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200430 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700431 * Create changeset @ovcs->cset to contain the nodes and properties of the
432 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
433 * any portions of the changeset that were successfully created will remain
434 * in @ovcs->cset.
435 *
436 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
437 * invalid overlay in @ovcs->fragments[].
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200438 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700439static int build_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200440{
Frank Rowand3912b792017-10-17 16:36:30 -0700441 struct fragment *fragment;
442 int fragments_count, i, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200443
Frank Rowand3912b792017-10-17 16:36:30 -0700444 /*
445 * if there is a symbols fragment in ovcs->fragments[i] it is
446 * the final element in the array
447 */
448 if (ovcs->symbols_fragment)
449 fragments_count = ovcs->count - 1;
450 else
451 fragments_count = ovcs->count;
452
453 for (i = 0; i < fragments_count; i++) {
454 fragment = &ovcs->fragments[i];
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200455
Frank Rowand0290c4c2017-10-17 16:36:23 -0700456 ret = build_changeset_next_level(ovcs, fragment->target,
Frank Rowand3912b792017-10-17 16:36:30 -0700457 fragment->overlay);
458 if (ret) {
459 pr_debug("apply failed '%pOF'\n", fragment->target);
460 return ret;
461 }
462 }
463
464 if (ovcs->symbols_fragment) {
465 fragment = &ovcs->fragments[ovcs->count - 1];
466 ret = build_changeset_symbols_node(ovcs, fragment->target,
467 fragment->overlay);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700468 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700469 pr_debug("apply failed '%pOF'\n", fragment->target);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700470 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200471 }
472 }
473
474 return 0;
475}
476
477/*
478 * Find the target node using a number of different strategies
Frank Rowand646afc42017-10-17 16:36:21 -0700479 * in order of preference:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200480 *
Frank Rowand646afc42017-10-17 16:36:21 -0700481 * 1) "target" property containing the phandle of the target
482 * 2) "target-path" property containing the path of the target
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200483 */
484static struct device_node *find_target_node(struct device_node *info_node)
485{
486 const char *path;
487 u32 val;
488 int ret;
489
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200490 ret = of_property_read_u32(info_node, "target", &val);
Frank Rowandbbed8792017-10-17 16:36:22 -0700491 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200492 return of_find_node_by_phandle(val);
493
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200494 ret = of_property_read_string(info_node, "target-path", &path);
Frank Rowandbbed8792017-10-17 16:36:22 -0700495 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200496 return of_find_node_by_path(path);
497
Rob Herring606ad422016-06-15 08:32:18 -0500498 pr_err("Failed to find target for node %p (%s)\n",
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200499 info_node, info_node->name);
500
501 return NULL;
502}
503
504/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700505 * init_overlay_changeset() - initialize overlay changeset from overlay tree
506 * @ovcs Overlay changeset to build
507 * @tree: Contains all the overlay fragments and overlay fixup nodes
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200508 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700509 * Initialize @ovcs. Populate @ovcs->fragments with node information from
510 * the top level of @tree. The relevant top level nodes are the fragment
511 * nodes and the __symbols__ node. Any other top level node will be ignored.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200512 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700513 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
Frank Rowand61b4de42017-10-17 16:36:25 -0700514 * detected in @tree, or -ENOSPC if idr_alloc() error.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200515 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700516static int init_overlay_changeset(struct overlay_changeset *ovcs,
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200517 struct device_node *tree)
518{
Frank Rowand61b4de42017-10-17 16:36:25 -0700519 struct device_node *node, *overlay_node;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700520 struct fragment *fragment;
521 struct fragment *fragments;
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100522 int cnt, id, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200523
Frank Rowand24789c52017-10-17 16:36:26 -0700524 /*
525 * Warn for some issues. Can not return -EINVAL for these until
526 * of_unittest_apply_overlay() is fixed to pass these checks.
527 */
528 if (!of_node_check_flag(tree, OF_DYNAMIC))
529 pr_debug("%s() tree is not dynamic\n", __func__);
530
531 if (!of_node_check_flag(tree, OF_DETACHED))
532 pr_debug("%s() tree is not detached\n", __func__);
533
534 if (!of_node_is_root(tree))
535 pr_debug("%s() tree is not root\n", __func__);
536
Frank Rowande0a58f32017-10-17 16:36:31 -0700537 ovcs->overlay_tree = tree;
538
Frank Rowand61b4de42017-10-17 16:36:25 -0700539 INIT_LIST_HEAD(&ovcs->ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200540
Frank Rowand61b4de42017-10-17 16:36:25 -0700541 of_changeset_init(&ovcs->cset);
542
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100543 id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
544 if (id <= 0)
545 return id;
Frank Rowand61b4de42017-10-17 16:36:25 -0700546
547 cnt = 0;
548
549 /* fragment nodes */
550 for_each_child_of_node(tree, node) {
551 overlay_node = of_get_child_by_name(node, "__overlay__");
552 if (overlay_node) {
553 cnt++;
554 of_node_put(overlay_node);
555 }
556 }
557
558 node = of_get_child_by_name(tree, "__symbols__");
559 if (node) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700560 cnt++;
Frank Rowand61b4de42017-10-17 16:36:25 -0700561 of_node_put(node);
562 }
Frank Rowandd1651b02017-07-19 09:25:22 -0700563
Frank Rowand0290c4c2017-10-17 16:36:23 -0700564 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
Frank Rowand61b4de42017-10-17 16:36:25 -0700565 if (!fragments) {
566 ret = -ENOMEM;
567 goto err_free_idr;
568 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200569
570 cnt = 0;
571 for_each_child_of_node(tree, node) {
Geert Uytterhoeven35e691e2017-12-08 14:13:02 +0100572 overlay_node = of_get_child_by_name(node, "__overlay__");
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100573 if (!overlay_node)
574 continue;
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100575
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100576 fragment = &fragments[cnt];
577 fragment->overlay = overlay_node;
578 fragment->target = find_target_node(node);
579 if (!fragment->target) {
580 of_node_put(fragment->overlay);
581 ret = -EINVAL;
582 goto err_free_fragments;
Frank Rowand61b4de42017-10-17 16:36:25 -0700583 }
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100584
585 cnt++;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200586 }
587
Frank Rowand3912b792017-10-17 16:36:30 -0700588 /*
589 * if there is a symbols fragment in ovcs->fragments[i] it is
590 * the final element in the array
591 */
Frank Rowandd1651b02017-07-19 09:25:22 -0700592 node = of_get_child_by_name(tree, "__symbols__");
593 if (node) {
Frank Rowand3912b792017-10-17 16:36:30 -0700594 ovcs->symbols_fragment = 1;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700595 fragment = &fragments[cnt];
596 fragment->overlay = node;
597 fragment->target = of_find_node_by_path("/__symbols__");
Frank Rowandd1651b02017-07-19 09:25:22 -0700598
Frank Rowand0290c4c2017-10-17 16:36:23 -0700599 if (!fragment->target) {
Frank Rowand4ee7c0d2017-10-19 14:38:11 -0700600 pr_err("symbols in overlay, but not in live tree\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700601 ret = -EINVAL;
602 goto err_free_fragments;
Frank Rowandd1651b02017-07-19 09:25:22 -0700603 }
604
605 cnt++;
606 }
607
Frank Rowandbbed8792017-10-17 16:36:22 -0700608 if (!cnt) {
Frank Rowand61b4de42017-10-17 16:36:25 -0700609 ret = -EINVAL;
610 goto err_free_fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200611 }
612
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100613 ovcs->id = id;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700614 ovcs->count = cnt;
615 ovcs->fragments = fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200616
617 return 0;
Frank Rowand61b4de42017-10-17 16:36:25 -0700618
Frank Rowand61b4de42017-10-17 16:36:25 -0700619err_free_fragments:
620 kfree(fragments);
621err_free_idr:
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100622 idr_remove(&ovcs_idr, id);
Frank Rowand61b4de42017-10-17 16:36:25 -0700623
Frank Rowand24789c52017-10-17 16:36:26 -0700624 pr_err("%s() failed, ret = %d\n", __func__, ret);
625
Frank Rowand61b4de42017-10-17 16:36:25 -0700626 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200627}
628
Frank Rowand61b4de42017-10-17 16:36:25 -0700629static void free_overlay_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200630{
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200631 int i;
632
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100633 if (ovcs->cset.entries.next)
634 of_changeset_destroy(&ovcs->cset);
Frank Rowand61b4de42017-10-17 16:36:25 -0700635
636 if (ovcs->id)
637 idr_remove(&ovcs_idr, ovcs->id);
638
639 for (i = 0; i < ovcs->count; i++) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700640 of_node_put(ovcs->fragments[i].target);
641 of_node_put(ovcs->fragments[i].overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200642 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700643 kfree(ovcs->fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200644
Frank Rowand61b4de42017-10-17 16:36:25 -0700645 kfree(ovcs);
646}
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200647
648/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700649 * of_overlay_apply() - Create and apply an overlay changeset
650 * @tree: Expanded overlay device tree
Frank Rowand24789c52017-10-17 16:36:26 -0700651 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200652 *
Frank Rowand24789c52017-10-17 16:36:26 -0700653 * Creates and applies an overlay changeset.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200654 *
Frank Rowand24789c52017-10-17 16:36:26 -0700655 * If an error occurs in a pre-apply notifier, then no changes are made
656 * to the device tree.
657 *
658
659 * A non-zero return value will not have created the changeset if error is from:
660 * - parameter checks
661 * - building the changeset
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100662 * - overlay changeset pre-apply notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700663 *
664 * If an error is returned by an overlay changeset pre-apply notifier
665 * then no further overlay changeset pre-apply notifier will be called.
666 *
667 * A non-zero return value will have created the changeset if error is from:
668 * - overlay changeset entry notifier
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100669 * - overlay changeset post-apply notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700670 *
671 * If an error is returned by an overlay changeset post-apply notifier
672 * then no further overlay changeset post-apply notifier will be called.
673 *
674 * If more than one notifier returns an error, then the last notifier
675 * error to occur is returned.
676 *
677 * If an error occurred while applying the overlay changeset, then an
678 * attempt is made to revert any changes that were made to the
679 * device tree. If there were any errors during the revert attempt
680 * then the state of the device tree can not be determined, and any
681 * following attempt to apply or remove an overlay changeset will be
682 * refused.
683 *
684 * Returns 0 on success, or a negative error number. Overlay changeset
685 * id is returned to *ovcs_id.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200686 */
Frank Rowand24789c52017-10-17 16:36:26 -0700687
688int of_overlay_apply(struct device_node *tree, int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200689{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700690 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700691 int ret = 0, ret_revert, ret_tmp;
692
693 *ovcs_id = 0;
694
695 if (devicetree_corrupt()) {
696 pr_err("devicetree state suspect, refuse to apply overlay\n");
697 ret = -EBUSY;
698 goto out;
699 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200700
Frank Rowand0290c4c2017-10-17 16:36:23 -0700701 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
Frank Rowand24789c52017-10-17 16:36:26 -0700702 if (!ovcs) {
703 ret = -ENOMEM;
704 goto out;
705 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200706
Frank Rowandf948d6d2017-10-17 16:36:29 -0700707 of_overlay_mutex_lock();
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100708 mutex_lock(&of_mutex);
Frank Rowandf948d6d2017-10-17 16:36:29 -0700709
710 ret = of_resolve_phandles(tree);
711 if (ret)
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100712 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200713
Frank Rowand0290c4c2017-10-17 16:36:23 -0700714 ret = init_overlay_changeset(ovcs, tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700715 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700716 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200717
Frank Rowand0290c4c2017-10-17 16:36:23 -0700718 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
Frank Rowand24789c52017-10-17 16:36:26 -0700719 if (ret) {
720 pr_err("overlay changeset pre-apply notify error %d\n", ret);
Frank Rowand61b4de42017-10-17 16:36:25 -0700721 goto err_free_overlay_changeset;
Alan Tull39a842e2016-11-01 14:14:22 -0500722 }
723
Frank Rowand0290c4c2017-10-17 16:36:23 -0700724 ret = build_changeset(ovcs);
725 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700726 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200727
Frank Rowand24789c52017-10-17 16:36:26 -0700728 ret_revert = 0;
729 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
730 if (ret) {
731 if (ret_revert) {
732 pr_debug("overlay changeset revert error %d\n",
733 ret_revert);
734 devicetree_state_flags |= DTSF_APPLY_FAIL;
735 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700736 goto err_free_overlay_changeset;
Frank Rowand24789c52017-10-17 16:36:26 -0700737 }
Rob Herring606ad422016-06-15 08:32:18 -0500738
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100739 ret = __of_changeset_apply_notify(&ovcs->cset);
740 if (ret)
741 pr_err("overlay changeset entry notify error %d\n", ret);
742 /* notify failure is not fatal, continue */
743
Frank Rowand0290c4c2017-10-17 16:36:23 -0700744 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
Frank Rowand24789c52017-10-17 16:36:26 -0700745 *ovcs_id = ovcs->id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200746
Frank Rowand24789c52017-10-17 16:36:26 -0700747 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
748 if (ret_tmp) {
749 pr_err("overlay changeset post-apply notify error %d\n",
750 ret_tmp);
751 if (!ret)
752 ret = ret_tmp;
753 }
Alan Tull39a842e2016-11-01 14:14:22 -0500754
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100755 goto out_unlock;
Frank Rowandf948d6d2017-10-17 16:36:29 -0700756
Frank Rowand61b4de42017-10-17 16:36:25 -0700757err_free_overlay_changeset:
758 free_overlay_changeset(ovcs);
759
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100760out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200761 mutex_unlock(&of_mutex);
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100762 of_overlay_mutex_unlock();
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200763
Frank Rowand24789c52017-10-17 16:36:26 -0700764out:
765 pr_debug("%s() err=%d\n", __func__, ret);
766
Frank Rowand0290c4c2017-10-17 16:36:23 -0700767 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200768}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700769EXPORT_SYMBOL_GPL(of_overlay_apply);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200770
Frank Rowand646afc42017-10-17 16:36:21 -0700771/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700772 * Find @np in @tree.
773 *
774 * Returns 1 if @np is @tree or is contained in @tree, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700775 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700776static int find_node(struct device_node *tree, struct device_node *np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200777{
778 struct device_node *child;
779
Frank Rowand0290c4c2017-10-17 16:36:23 -0700780 if (tree == np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200781 return 1;
782
783 for_each_child_of_node(tree, child) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700784 if (find_node(child, np)) {
Julia Lawall001cf502015-10-22 11:02:48 +0200785 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200786 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200787 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200788 }
789
790 return 0;
791}
792
Frank Rowand646afc42017-10-17 16:36:21 -0700793/*
Frank Rowand87f242c2017-10-17 16:36:27 -0700794 * Is @remove_ce_node a child of, a parent of, or the same as any
Frank Rowand0290c4c2017-10-17 16:36:23 -0700795 * node in an overlay changeset more topmost than @remove_ovcs?
796 *
797 * Returns 1 if found, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700798 */
Frank Rowand87f242c2017-10-17 16:36:27 -0700799static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
800 struct device_node *remove_ce_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200801{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700802 struct overlay_changeset *ovcs;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200803 struct of_changeset_entry *ce;
804
Frank Rowand0290c4c2017-10-17 16:36:23 -0700805 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
806 if (ovcs == remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200807 break;
808
Frank Rowand0290c4c2017-10-17 16:36:23 -0700809 list_for_each_entry(ce, &ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700810 if (find_node(ce->np, remove_ce_node)) {
811 pr_err("%s: #%d overlaps with #%d @%pOF\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700812 __func__, remove_ovcs->id, ovcs->id,
Frank Rowand87f242c2017-10-17 16:36:27 -0700813 remove_ce_node);
814 return 1;
815 }
816 if (find_node(remove_ce_node, ce->np)) {
817 pr_err("%s: #%d overlaps with #%d @%pOF\n",
818 __func__, remove_ovcs->id, ovcs->id,
819 remove_ce_node);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700820 return 1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200821 }
822 }
823 }
824
Frank Rowand0290c4c2017-10-17 16:36:23 -0700825 return 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200826}
827
828/*
829 * We can safely remove the overlay only if it's the top-most one.
830 * Newly applied overlays are inserted at the tail of the overlay list,
831 * so a top most overlay is the one that is closest to the tail.
832 *
833 * The topmost check is done by exploiting this property. For each
834 * affected device node in the log list we check if this overlay is
835 * the one closest to the tail. If another overlay has affected this
836 * device node and is closest to the tail, then removal is not permited.
837 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700838static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200839{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700840 struct of_changeset_entry *remove_ce;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200841
Frank Rowand0290c4c2017-10-17 16:36:23 -0700842 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700843 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700844 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200845 return 0;
846 }
847 }
848
849 return 1;
850}
851
852/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700853 * of_overlay_remove() - Revert and free an overlay changeset
Frank Rowand24789c52017-10-17 16:36:26 -0700854 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200855 *
Frank Rowand24789c52017-10-17 16:36:26 -0700856 * Removes an overlay if it is permissible. @ovcs_id was previously returned
Frank Rowand0290c4c2017-10-17 16:36:23 -0700857 * by of_overlay_apply().
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200858 *
Frank Rowand24789c52017-10-17 16:36:26 -0700859 * If an error occurred while attempting to revert the overlay changeset,
860 * then an attempt is made to re-apply any changeset entry that was
861 * reverted. If an error occurs on re-apply then the state of the device
862 * tree can not be determined, and any following attempt to apply or remove
863 * an overlay changeset will be refused.
864 *
865 * A non-zero return value will not revert the changeset if error is from:
866 * - parameter checks
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100867 * - overlay changeset pre-remove notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700868 * - overlay changeset entry revert
869 *
870 * If an error is returned by an overlay changeset pre-remove notifier
871 * then no further overlay changeset pre-remove notifier will be called.
872 *
873 * If more than one notifier returns an error, then the last notifier
874 * error to occur is returned.
875 *
876 * A non-zero return value will revert the changeset if error is from:
877 * - overlay changeset entry notifier
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100878 * - overlay changeset post-remove notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700879 *
880 * If an error is returned by an overlay changeset post-remove notifier
881 * then no further overlay changeset post-remove notifier will be called.
882 *
883 * Returns 0 on success, or a negative error number. *ovcs_id is set to
884 * zero after reverting the changeset, even if a subsequent error occurs.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200885 */
Frank Rowand24789c52017-10-17 16:36:26 -0700886int of_overlay_remove(int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200887{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700888 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700889 int ret, ret_apply, ret_tmp;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200890
Frank Rowand24789c52017-10-17 16:36:26 -0700891 ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200892
Frank Rowand24789c52017-10-17 16:36:26 -0700893 if (devicetree_corrupt()) {
894 pr_err("suspect devicetree state, refuse to remove overlay\n");
Frank Rowand0290c4c2017-10-17 16:36:23 -0700895 ret = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200896 goto out;
897 }
898
Frank Rowand24789c52017-10-17 16:36:26 -0700899 mutex_lock(&of_mutex);
900
901 ovcs = idr_find(&ovcs_idr, *ovcs_id);
902 if (!ovcs) {
903 ret = -ENODEV;
904 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
905 goto out_unlock;
906 }
907
908 if (!overlay_removal_is_ok(ovcs)) {
909 ret = -EBUSY;
910 goto out_unlock;
911 }
912
913 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
914 if (ret) {
915 pr_err("overlay changeset pre-remove notify error %d\n", ret);
916 goto out_unlock;
917 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700918
Frank Rowand0290c4c2017-10-17 16:36:23 -0700919 list_del(&ovcs->ovcs_list);
Frank Rowand61b4de42017-10-17 16:36:25 -0700920
Frank Rowand24789c52017-10-17 16:36:26 -0700921 ret_apply = 0;
922 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
923 if (ret) {
924 if (ret_apply)
925 devicetree_state_flags |= DTSF_REVERT_FAIL;
926 goto out_unlock;
Frank Rowand24789c52017-10-17 16:36:26 -0700927 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700928
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100929 ret = __of_changeset_revert_notify(&ovcs->cset);
930 if (ret)
931 pr_err("overlay changeset entry notify error %d\n", ret);
932 /* notify failure is not fatal, continue */
933
Frank Rowand24789c52017-10-17 16:36:26 -0700934 *ovcs_id = 0;
935
936 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
937 if (ret_tmp) {
938 pr_err("overlay changeset post-remove notify error %d\n",
939 ret_tmp);
940 if (!ret)
941 ret = ret_tmp;
942 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700943
944 free_overlay_changeset(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200945
Frank Rowand24789c52017-10-17 16:36:26 -0700946out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200947 mutex_unlock(&of_mutex);
948
Frank Rowand24789c52017-10-17 16:36:26 -0700949out:
950 pr_debug("%s() err=%d\n", __func__, ret);
951
Frank Rowand0290c4c2017-10-17 16:36:23 -0700952 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200953}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700954EXPORT_SYMBOL_GPL(of_overlay_remove);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200955
956/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700957 * of_overlay_remove_all() - Reverts and frees all overlay changesets
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200958 *
959 * Removes all overlays from the system in the correct order.
960 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200961 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200962 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700963int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200964{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700965 struct overlay_changeset *ovcs, *ovcs_n;
Frank Rowand61b4de42017-10-17 16:36:25 -0700966 int ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200967
968 /* the tail of list is guaranteed to be safe to remove */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700969 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
Frank Rowand24789c52017-10-17 16:36:26 -0700970 ret = of_overlay_remove(&ovcs->id);
Frank Rowand61b4de42017-10-17 16:36:25 -0700971 if (ret)
972 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200973 }
974
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200975 return 0;
976}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700977EXPORT_SYMBOL_GPL(of_overlay_remove_all);