blob: 30d91f9070268ed2c6a9fa5bc3eb7b9b9ccce97a [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>
Frank Rowand39a751a2018-02-12 00:19:42 -080015#include <linux/of_fdt.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020016#include <linux/string.h>
17#include <linux/ctype.h>
18#include <linux/errno.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020019#include <linux/slab.h>
Frank Rowand39a751a2018-02-12 00:19:42 -080020#include <linux/libfdt.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020021#include <linux/err.h>
Mark Brown0d1886d2015-02-17 11:36:58 +090022#include <linux/idr.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020023
24#include "of_private.h"
25
26/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070027 * struct fragment - info about fragment nodes in overlay expanded device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020028 * @target: target of the overlay operation
Frank Rowand0290c4c2017-10-17 16:36:23 -070029 * @overlay: pointer to the __overlay__ node
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020030 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070031struct fragment {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020032 struct device_node *target;
33 struct device_node *overlay;
34};
35
36/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070037 * struct overlay_changeset
Frank Rowand39a751a2018-02-12 00:19:42 -080038 * @id: changeset identifier
Frank Rowand3912b792017-10-17 16:36:30 -070039 * @ovcs_list: list on which we are located
Frank Rowand39a751a2018-02-12 00:19:42 -080040 * @fdt: FDT that was unflattened to create @overlay_tree
Frank Rowande0a58f32017-10-17 16:36:31 -070041 * @overlay_tree: expanded device tree that contains the fragment nodes
Frank Rowand3912b792017-10-17 16:36:30 -070042 * @count: count of fragment structures
43 * @fragments: fragment nodes in the overlay expanded device tree
44 * @symbols_fragment: last element of @fragments[] is the __symbols__ node
45 * @cset: changeset to apply fragments to live device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020046 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070047struct overlay_changeset {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020048 int id;
Frank Rowand0290c4c2017-10-17 16:36:23 -070049 struct list_head ovcs_list;
Frank Rowand39a751a2018-02-12 00:19:42 -080050 const void *fdt;
Frank Rowande0a58f32017-10-17 16:36:31 -070051 struct device_node *overlay_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020052 int count;
Frank Rowand0290c4c2017-10-17 16:36:23 -070053 struct fragment *fragments;
Frank Rowand3912b792017-10-17 16:36:30 -070054 bool symbols_fragment;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020055 struct of_changeset cset;
56};
57
Frank Rowand24789c52017-10-17 16:36:26 -070058/* flags are sticky - once set, do not reset */
59static int devicetree_state_flags;
60#define DTSF_APPLY_FAIL 0x01
61#define DTSF_REVERT_FAIL 0x02
62
63/*
64 * If a changeset apply or revert encounters an error, an attempt will
65 * be made to undo partial changes, but may fail. If the undo fails
66 * we do not know the state of the devicetree.
67 */
68static int devicetree_corrupt(void)
69{
70 return devicetree_state_flags &
71 (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
72}
73
Frank Rowand0290c4c2017-10-17 16:36:23 -070074static int build_changeset_next_level(struct overlay_changeset *ovcs,
75 struct device_node *target_node,
Frank Rowand3912b792017-10-17 16:36:30 -070076 const struct device_node *overlay_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020077
Frank Rowandf948d6d2017-10-17 16:36:29 -070078/*
79 * of_resolve_phandles() finds the largest phandle in the live tree.
80 * of_overlay_apply() may add a larger phandle to the live tree.
81 * Do not allow race between two overlays being applied simultaneously:
82 * mutex_lock(&of_overlay_phandle_mutex)
83 * of_resolve_phandles()
84 * of_overlay_apply()
85 * mutex_unlock(&of_overlay_phandle_mutex)
86 */
87static DEFINE_MUTEX(of_overlay_phandle_mutex);
88
89void of_overlay_mutex_lock(void)
90{
91 mutex_lock(&of_overlay_phandle_mutex);
92}
93
94void of_overlay_mutex_unlock(void)
95{
96 mutex_unlock(&of_overlay_phandle_mutex);
97}
98
99
Frank Rowand61b4de42017-10-17 16:36:25 -0700100static LIST_HEAD(ovcs_list);
101static DEFINE_IDR(ovcs_idr);
102
Frank Rowand0290c4c2017-10-17 16:36:23 -0700103static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
Alan Tull39a842e2016-11-01 14:14:22 -0500104
105int of_overlay_notifier_register(struct notifier_block *nb)
106{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700107 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500108}
109EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
110
111int of_overlay_notifier_unregister(struct notifier_block *nb)
112{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700113 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500114}
115EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
116
Frank Rowand24789c52017-10-17 16:36:26 -0700117static char *of_overlay_action_name[] = {
118 "pre-apply",
119 "post-apply",
120 "pre-remove",
121 "post-remove",
122};
123
Frank Rowand0290c4c2017-10-17 16:36:23 -0700124static int overlay_notify(struct overlay_changeset *ovcs,
125 enum of_overlay_notify_action action)
Alan Tull39a842e2016-11-01 14:14:22 -0500126{
127 struct of_overlay_notify_data nd;
128 int i, ret;
129
Frank Rowand0290c4c2017-10-17 16:36:23 -0700130 for (i = 0; i < ovcs->count; i++) {
131 struct fragment *fragment = &ovcs->fragments[i];
Alan Tull39a842e2016-11-01 14:14:22 -0500132
Frank Rowand0290c4c2017-10-17 16:36:23 -0700133 nd.target = fragment->target;
134 nd.overlay = fragment->overlay;
Alan Tull39a842e2016-11-01 14:14:22 -0500135
Frank Rowand0290c4c2017-10-17 16:36:23 -0700136 ret = blocking_notifier_call_chain(&overlay_notify_chain,
Alan Tull39a842e2016-11-01 14:14:22 -0500137 action, &nd);
Frank Rowanda1d19bd2017-10-19 14:18:27 -0700138 if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
Frank Rowand24789c52017-10-17 16:36:26 -0700139 return 0;
140 if (ret) {
141 ret = notifier_to_errno(ret);
142 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
143 of_overlay_action_name[action], ret, nd.target);
144 return ret;
145 }
Alan Tull39a842e2016-11-01 14:14:22 -0500146 }
147
148 return 0;
149}
150
Frank Rowand42b2e942017-10-17 16:36:24 -0700151/*
Frank Rowande0a58f32017-10-17 16:36:31 -0700152 * The values of properties in the "/__symbols__" node are paths in
153 * the ovcs->overlay_tree. When duplicating the properties, the paths
154 * need to be adjusted to be the correct path for the live device tree.
Frank Rowand42b2e942017-10-17 16:36:24 -0700155 *
Frank Rowande0a58f32017-10-17 16:36:31 -0700156 * The paths refer to a node in the subtree of a fragment node's "__overlay__"
157 * node, for example "/fragment@0/__overlay__/symbol_path_tail",
158 * where symbol_path_tail can be a single node or it may be a multi-node path.
Frank Rowand42b2e942017-10-17 16:36:24 -0700159 *
160 * The duplicated property value will be modified by replacing the
161 * "/fragment_name/__overlay/" portion of the value with the target
162 * path from the fragment node.
163 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700164static struct property *dup_and_fixup_symbol_prop(
165 struct overlay_changeset *ovcs, const struct property *prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200166{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700167 struct fragment *fragment;
Frank Rowande0a58f32017-10-17 16:36:31 -0700168 struct property *new_prop;
169 struct device_node *fragment_node;
170 struct device_node *overlay_node;
171 const char *path;
172 const char *path_tail;
Frank Rowandd1651b02017-07-19 09:25:22 -0700173 const char *target_path;
174 int k;
Frank Rowandd1651b02017-07-19 09:25:22 -0700175 int overlay_name_len;
Frank Rowande0a58f32017-10-17 16:36:31 -0700176 int path_len;
177 int path_tail_len;
Frank Rowandd1651b02017-07-19 09:25:22 -0700178 int target_path_len;
179
180 if (!prop->value)
181 return NULL;
Frank Rowande0a58f32017-10-17 16:36:31 -0700182 if (strnlen(prop->value, prop->length) >= prop->length)
Frank Rowandd1651b02017-07-19 09:25:22 -0700183 return NULL;
Frank Rowande0a58f32017-10-17 16:36:31 -0700184 path = prop->value;
185 path_len = strlen(path);
186
187 if (path_len < 1)
188 return NULL;
189 fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
190 overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
191 of_node_put(fragment_node);
192 of_node_put(overlay_node);
Frank Rowandd1651b02017-07-19 09:25:22 -0700193
Frank Rowand0290c4c2017-10-17 16:36:23 -0700194 for (k = 0; k < ovcs->count; k++) {
195 fragment = &ovcs->fragments[k];
Frank Rowande0a58f32017-10-17 16:36:31 -0700196 if (fragment->overlay == overlay_node)
Frank Rowandd1651b02017-07-19 09:25:22 -0700197 break;
198 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700199 if (k >= ovcs->count)
Frank Rowande0a58f32017-10-17 16:36:31 -0700200 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700201
Frank Rowande0a58f32017-10-17 16:36:31 -0700202 overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
203
204 if (overlay_name_len > path_len)
205 return NULL;
206 path_tail = path + overlay_name_len;
207 path_tail_len = strlen(path_tail);
208
209 target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
210 if (!target_path)
211 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700212 target_path_len = strlen(target_path);
213
Frank Rowande0a58f32017-10-17 16:36:31 -0700214 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
215 if (!new_prop)
216 goto err_free_target_path;
Frank Rowandd1651b02017-07-19 09:25:22 -0700217
Frank Rowande0a58f32017-10-17 16:36:31 -0700218 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
219 new_prop->length = target_path_len + path_tail_len + 1;
220 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
221 if (!new_prop->name || !new_prop->value)
222 goto err_free_new_prop;
Frank Rowandd1651b02017-07-19 09:25:22 -0700223
Frank Rowande0a58f32017-10-17 16:36:31 -0700224 strcpy(new_prop->value, target_path);
225 strcpy(new_prop->value + target_path_len, path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700226
Frank Rowande0a58f32017-10-17 16:36:31 -0700227 of_property_set_flag(new_prop, OF_DYNAMIC);
Frank Rowandd1651b02017-07-19 09:25:22 -0700228
Frank Rowande0a58f32017-10-17 16:36:31 -0700229 return new_prop;
Frank Rowandd1651b02017-07-19 09:25:22 -0700230
Frank Rowande0a58f32017-10-17 16:36:31 -0700231err_free_new_prop:
232 kfree(new_prop->name);
233 kfree(new_prop->value);
234 kfree(new_prop);
235err_free_target_path:
236 kfree(target_path);
Frank Rowandd1651b02017-07-19 09:25:22 -0700237
Frank Rowandd1651b02017-07-19 09:25:22 -0700238 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700239}
240
Frank Rowand0290c4c2017-10-17 16:36:23 -0700241/**
242 * add_changeset_property() - add @overlay_prop to overlay changeset
243 * @ovcs: overlay changeset
244 * @target_node: where to place @overlay_prop in live tree
245 * @overlay_prop: property to add or update, from overlay tree
Frank Rowand3912b792017-10-17 16:36:30 -0700246 * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
Frank Rowand0290c4c2017-10-17 16:36:23 -0700247 *
248 * If @overlay_prop does not already exist in @target_node, add changeset entry
249 * to add @overlay_prop in @target_node, else add changeset entry to update
250 * value of @overlay_prop.
251 *
Frank Rowand646afc42017-10-17 16:36:21 -0700252 * Some special properties are not updated (no error returned).
Frank Rowand0290c4c2017-10-17 16:36:23 -0700253 *
Frank Rowand646afc42017-10-17 16:36:21 -0700254 * Update of property in symbols node is not allowed.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700255 *
256 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
257 * invalid @overlay.
Frank Rowand646afc42017-10-17 16:36:21 -0700258 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700259static int add_changeset_property(struct overlay_changeset *ovcs,
260 struct device_node *target_node,
261 struct property *overlay_prop,
Frank Rowand3912b792017-10-17 16:36:30 -0700262 bool is_symbols_prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700263{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700264 struct property *new_prop = NULL, *prop;
Lixin Wangac0f3e32017-10-16 17:54:32 +0800265 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200266
Frank Rowand0290c4c2017-10-17 16:36:23 -0700267 prop = of_find_property(target_node, overlay_prop->name, NULL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200268
Frank Rowand0290c4c2017-10-17 16:36:23 -0700269 if (!of_prop_cmp(overlay_prop->name, "name") ||
270 !of_prop_cmp(overlay_prop->name, "phandle") ||
271 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200272 return 0;
273
Frank Rowand3912b792017-10-17 16:36:30 -0700274 if (is_symbols_prop) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700275 if (prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700276 return -EINVAL;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700277 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
Frank Rowandd1651b02017-07-19 09:25:22 -0700278 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700279 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
Frank Rowandd1651b02017-07-19 09:25:22 -0700280 }
281
Frank Rowand0290c4c2017-10-17 16:36:23 -0700282 if (!new_prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200283 return -ENOMEM;
284
Frank Rowand0290c4c2017-10-17 16:36:23 -0700285 if (!prop)
286 ret = of_changeset_add_property(&ovcs->cset, target_node,
287 new_prop);
Frank Rowand646afc42017-10-17 16:36:21 -0700288 else
Frank Rowand0290c4c2017-10-17 16:36:23 -0700289 ret = of_changeset_update_property(&ovcs->cset, target_node,
290 new_prop);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200291
Lixin Wangac0f3e32017-10-16 17:54:32 +0800292 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700293 kfree(new_prop->name);
294 kfree(new_prop->value);
295 kfree(new_prop);
Lixin Wangac0f3e32017-10-16 17:54:32 +0800296 }
297 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200298}
299
Frank Rowand0290c4c2017-10-17 16:36:23 -0700300/**
301 * add_changeset_node() - add @node (and children) to overlay changeset
302 * @ovcs: overlay changeset
303 * @target_node: where to place @node in live tree
304 * @node: node from within overlay device tree fragment
305 *
306 * If @node does not already exist in @target_node, add changeset entry
307 * to add @node in @target_node.
308 *
309 * If @node already exists in @target_node, and the existing node has
310 * a phandle, the overlay node is not allowed to have a phandle.
311 *
312 * If @node has child nodes, add the children recursively via
313 * build_changeset_next_level().
314 *
Frank Rowandb89dae12018-02-26 14:01:23 -0800315 * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
316 * not contain the full path in node->full_name. Thus an overlay
317 * created from an FDT also will not contain the full path in
318 * node->full_name. However, a live devicetree created from Open
319 * Firmware may have the full path in node->full_name.
320 *
321 * add_changeset_node() follows the FDT convention and does not include
322 * the full path in node->full_name. Even though it expects the overlay
323 * to not contain the full path, it uses kbasename() to remove the
324 * full path should it exist. It also uses kbasename() in comparisons
325 * to nodes in the live devicetree so that it can apply an overlay to
326 * a live devicetree created from Open Firmware.
327 *
328 * NOTE_2: Multiple mods of created nodes not supported.
Frank Rowand24789c52017-10-17 16:36:26 -0700329 * If more than one fragment contains a node that does not already exist
330 * in the live tree, then for each fragment of_changeset_attach_node()
331 * will add a changeset entry to add the node. When the changeset is
332 * applied, __of_attach_node() will attach the node twice (once for
333 * each fragment). At this point the device tree will be corrupted.
334 *
335 * TODO: add integrity check to ensure that multiple fragments do not
336 * create the same node.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700337 *
338 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
339 * invalid @overlay.
340 */
341static int add_changeset_node(struct overlay_changeset *ovcs,
342 struct device_node *target_node, struct device_node *node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200343{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700344 const char *node_kbasename;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300345 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200346 int ret = 0;
347
Frank Rowand0290c4c2017-10-17 16:36:23 -0700348 node_kbasename = kbasename(node->full_name);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200349
Frank Rowand0290c4c2017-10-17 16:36:23 -0700350 for_each_child_of_node(target_node, tchild)
351 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700352 break;
353
Frank Rowand61b4de42017-10-17 16:36:25 -0700354 if (!tchild) {
Frank Rowandb89dae12018-02-26 14:01:23 -0800355 tchild = __of_node_dup(node, node_kbasename);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200356 if (!tchild)
357 return -ENOMEM;
358
Frank Rowand0290c4c2017-10-17 16:36:23 -0700359 tchild->parent = target_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200360
Frank Rowand0290c4c2017-10-17 16:36:23 -0700361 ret = of_changeset_attach_node(&ovcs->cset, tchild);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200362 if (ret)
363 return ret;
364
Frank Rowand3912b792017-10-17 16:36:30 -0700365 return build_changeset_next_level(ovcs, tchild, node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200366 }
367
Frank Rowand6d0f5472017-10-17 16:36:28 -0700368 if (node->phandle && tchild->phandle)
369 ret = -EINVAL;
370 else
Frank Rowand3912b792017-10-17 16:36:30 -0700371 ret = build_changeset_next_level(ovcs, tchild, node);
Frank Rowand61b4de42017-10-17 16:36:25 -0700372 of_node_put(tchild);
373
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200374 return ret;
375}
376
Frank Rowand0290c4c2017-10-17 16:36:23 -0700377/**
378 * build_changeset_next_level() - add level of overlay changeset
379 * @ovcs: overlay changeset
380 * @target_node: where to place @overlay_node in live tree
381 * @overlay_node: node from within an overlay device tree fragment
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200382 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700383 * Add the properties (if any) and nodes (if any) from @overlay_node to the
384 * @ovcs->cset changeset. If an added node has child nodes, they will
385 * be added recursively.
Frank Rowand646afc42017-10-17 16:36:21 -0700386 *
387 * Do not allow symbols node to have any children.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700388 *
389 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
390 * invalid @overlay_node.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200391 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700392static int build_changeset_next_level(struct overlay_changeset *ovcs,
393 struct device_node *target_node,
Frank Rowand3912b792017-10-17 16:36:30 -0700394 const struct device_node *overlay_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200395{
396 struct device_node *child;
397 struct property *prop;
398 int ret;
399
Frank Rowand0290c4c2017-10-17 16:36:23 -0700400 for_each_property_of_node(overlay_node, prop) {
Frank Rowand3912b792017-10-17 16:36:30 -0700401 ret = add_changeset_property(ovcs, target_node, prop, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200402 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700403 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
404 target_node, prop->name, ret);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200405 return ret;
406 }
407 }
408
Frank Rowand0290c4c2017-10-17 16:36:23 -0700409 for_each_child_of_node(overlay_node, child) {
410 ret = add_changeset_node(ovcs, target_node, child);
Frank Rowandbbed8792017-10-17 16:36:22 -0700411 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700412 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
413 target_node, child->name, ret);
Julia Lawall001cf502015-10-22 11:02:48 +0200414 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200415 return ret;
416 }
417 }
418
419 return 0;
420}
421
Frank Rowand3912b792017-10-17 16:36:30 -0700422/*
423 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
424 */
425static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
426 struct device_node *target_node,
427 const struct device_node *overlay_symbols_node)
428{
429 struct property *prop;
430 int ret;
431
432 for_each_property_of_node(overlay_symbols_node, prop) {
433 ret = add_changeset_property(ovcs, target_node, prop, 1);
434 if (ret) {
435 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
436 target_node, prop->name, ret);
437 return ret;
438 }
439 }
440
441 return 0;
442}
443
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200444/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700445 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
446 * @ovcs: Overlay changeset
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200447 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700448 * Create changeset @ovcs->cset to contain the nodes and properties of the
449 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
450 * any portions of the changeset that were successfully created will remain
451 * in @ovcs->cset.
452 *
453 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
454 * invalid overlay in @ovcs->fragments[].
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200455 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700456static int build_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200457{
Frank Rowand3912b792017-10-17 16:36:30 -0700458 struct fragment *fragment;
459 int fragments_count, i, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200460
Frank Rowand3912b792017-10-17 16:36:30 -0700461 /*
462 * if there is a symbols fragment in ovcs->fragments[i] it is
463 * the final element in the array
464 */
465 if (ovcs->symbols_fragment)
466 fragments_count = ovcs->count - 1;
467 else
468 fragments_count = ovcs->count;
469
470 for (i = 0; i < fragments_count; i++) {
471 fragment = &ovcs->fragments[i];
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200472
Frank Rowand0290c4c2017-10-17 16:36:23 -0700473 ret = build_changeset_next_level(ovcs, fragment->target,
Frank Rowand3912b792017-10-17 16:36:30 -0700474 fragment->overlay);
475 if (ret) {
476 pr_debug("apply failed '%pOF'\n", fragment->target);
477 return ret;
478 }
479 }
480
481 if (ovcs->symbols_fragment) {
482 fragment = &ovcs->fragments[ovcs->count - 1];
483 ret = build_changeset_symbols_node(ovcs, fragment->target,
484 fragment->overlay);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700485 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700486 pr_debug("apply failed '%pOF'\n", fragment->target);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700487 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200488 }
489 }
490
491 return 0;
492}
493
494/*
495 * Find the target node using a number of different strategies
Frank Rowand646afc42017-10-17 16:36:21 -0700496 * in order of preference:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200497 *
Frank Rowand646afc42017-10-17 16:36:21 -0700498 * 1) "target" property containing the phandle of the target
499 * 2) "target-path" property containing the path of the target
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200500 */
501static struct device_node *find_target_node(struct device_node *info_node)
502{
Frank Rowande547c002018-02-12 00:25:04 -0800503 struct device_node *node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200504 const char *path;
505 u32 val;
506 int ret;
507
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200508 ret = of_property_read_u32(info_node, "target", &val);
Frank Rowande547c002018-02-12 00:25:04 -0800509 if (!ret) {
510 node = of_find_node_by_phandle(val);
511 if (!node)
512 pr_err("find target, node: %pOF, phandle 0x%x not found\n",
513 info_node, val);
514 return node;
515 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200516
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200517 ret = of_property_read_string(info_node, "target-path", &path);
Frank Rowande547c002018-02-12 00:25:04 -0800518 if (!ret) {
519 node = of_find_node_by_path(path);
520 if (!node)
521 pr_err("find target, node: %pOF, path '%s' not found\n",
522 info_node, path);
523 return node;
524 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200525
Frank Rowande547c002018-02-12 00:25:04 -0800526 pr_err("find target, node: %pOF, no target property\n", info_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200527
528 return NULL;
529}
530
531/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700532 * init_overlay_changeset() - initialize overlay changeset from overlay tree
Frank Rowand39a751a2018-02-12 00:19:42 -0800533 * @ovcs: Overlay changeset to build
534 * @fdt: the FDT that was unflattened to create @tree
Frank Rowand0290c4c2017-10-17 16:36:23 -0700535 * @tree: Contains all the overlay fragments and overlay fixup nodes
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200536 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700537 * Initialize @ovcs. Populate @ovcs->fragments with node information from
538 * the top level of @tree. The relevant top level nodes are the fragment
539 * nodes and the __symbols__ node. Any other top level node will be ignored.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200540 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700541 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
Frank Rowand61b4de42017-10-17 16:36:25 -0700542 * detected in @tree, or -ENOSPC if idr_alloc() error.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200543 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700544static int init_overlay_changeset(struct overlay_changeset *ovcs,
Frank Rowand39a751a2018-02-12 00:19:42 -0800545 const void *fdt, struct device_node *tree)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200546{
Frank Rowand61b4de42017-10-17 16:36:25 -0700547 struct device_node *node, *overlay_node;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700548 struct fragment *fragment;
549 struct fragment *fragments;
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100550 int cnt, id, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200551
Frank Rowand24789c52017-10-17 16:36:26 -0700552 /*
553 * Warn for some issues. Can not return -EINVAL for these until
554 * of_unittest_apply_overlay() is fixed to pass these checks.
555 */
556 if (!of_node_check_flag(tree, OF_DYNAMIC))
557 pr_debug("%s() tree is not dynamic\n", __func__);
558
559 if (!of_node_check_flag(tree, OF_DETACHED))
560 pr_debug("%s() tree is not detached\n", __func__);
561
562 if (!of_node_is_root(tree))
563 pr_debug("%s() tree is not root\n", __func__);
564
Frank Rowande0a58f32017-10-17 16:36:31 -0700565 ovcs->overlay_tree = tree;
Frank Rowand39a751a2018-02-12 00:19:42 -0800566 ovcs->fdt = fdt;
Frank Rowande0a58f32017-10-17 16:36:31 -0700567
Frank Rowand61b4de42017-10-17 16:36:25 -0700568 INIT_LIST_HEAD(&ovcs->ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200569
Frank Rowand61b4de42017-10-17 16:36:25 -0700570 of_changeset_init(&ovcs->cset);
571
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100572 id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
573 if (id <= 0)
574 return id;
Frank Rowand61b4de42017-10-17 16:36:25 -0700575
576 cnt = 0;
577
578 /* fragment nodes */
579 for_each_child_of_node(tree, node) {
580 overlay_node = of_get_child_by_name(node, "__overlay__");
581 if (overlay_node) {
582 cnt++;
583 of_node_put(overlay_node);
584 }
585 }
586
587 node = of_get_child_by_name(tree, "__symbols__");
588 if (node) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700589 cnt++;
Frank Rowand61b4de42017-10-17 16:36:25 -0700590 of_node_put(node);
591 }
Frank Rowandd1651b02017-07-19 09:25:22 -0700592
Frank Rowand0290c4c2017-10-17 16:36:23 -0700593 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
Frank Rowand61b4de42017-10-17 16:36:25 -0700594 if (!fragments) {
595 ret = -ENOMEM;
596 goto err_free_idr;
597 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200598
599 cnt = 0;
600 for_each_child_of_node(tree, node) {
Geert Uytterhoeven35e691e2017-12-08 14:13:02 +0100601 overlay_node = of_get_child_by_name(node, "__overlay__");
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100602 if (!overlay_node)
603 continue;
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100604
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100605 fragment = &fragments[cnt];
606 fragment->overlay = overlay_node;
607 fragment->target = find_target_node(node);
608 if (!fragment->target) {
609 of_node_put(fragment->overlay);
610 ret = -EINVAL;
611 goto err_free_fragments;
Frank Rowand61b4de42017-10-17 16:36:25 -0700612 }
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100613
614 cnt++;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200615 }
616
Frank Rowand3912b792017-10-17 16:36:30 -0700617 /*
618 * if there is a symbols fragment in ovcs->fragments[i] it is
619 * the final element in the array
620 */
Frank Rowandd1651b02017-07-19 09:25:22 -0700621 node = of_get_child_by_name(tree, "__symbols__");
622 if (node) {
Frank Rowand3912b792017-10-17 16:36:30 -0700623 ovcs->symbols_fragment = 1;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700624 fragment = &fragments[cnt];
625 fragment->overlay = node;
626 fragment->target = of_find_node_by_path("/__symbols__");
Frank Rowandd1651b02017-07-19 09:25:22 -0700627
Frank Rowand0290c4c2017-10-17 16:36:23 -0700628 if (!fragment->target) {
Frank Rowand4ee7c0d2017-10-19 14:38:11 -0700629 pr_err("symbols in overlay, but not in live tree\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700630 ret = -EINVAL;
631 goto err_free_fragments;
Frank Rowandd1651b02017-07-19 09:25:22 -0700632 }
633
634 cnt++;
635 }
636
Frank Rowandbbed8792017-10-17 16:36:22 -0700637 if (!cnt) {
Frank Rowand39a751a2018-02-12 00:19:42 -0800638 pr_err("no fragments or symbols in overlay\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700639 ret = -EINVAL;
640 goto err_free_fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200641 }
642
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100643 ovcs->id = id;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700644 ovcs->count = cnt;
645 ovcs->fragments = fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200646
647 return 0;
Frank Rowand61b4de42017-10-17 16:36:25 -0700648
Frank Rowand61b4de42017-10-17 16:36:25 -0700649err_free_fragments:
650 kfree(fragments);
651err_free_idr:
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100652 idr_remove(&ovcs_idr, id);
Frank Rowand61b4de42017-10-17 16:36:25 -0700653
Frank Rowand24789c52017-10-17 16:36:26 -0700654 pr_err("%s() failed, ret = %d\n", __func__, ret);
655
Frank Rowand61b4de42017-10-17 16:36:25 -0700656 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200657}
658
Frank Rowand61b4de42017-10-17 16:36:25 -0700659static void free_overlay_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200660{
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200661 int i;
662
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100663 if (ovcs->cset.entries.next)
664 of_changeset_destroy(&ovcs->cset);
Frank Rowand61b4de42017-10-17 16:36:25 -0700665
666 if (ovcs->id)
667 idr_remove(&ovcs_idr, ovcs->id);
668
669 for (i = 0; i < ovcs->count; i++) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700670 of_node_put(ovcs->fragments[i].target);
671 of_node_put(ovcs->fragments[i].overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200672 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700673 kfree(ovcs->fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200674
Frank Rowand39a751a2018-02-12 00:19:42 -0800675 /*
676 * TODO
677 *
678 * would like to: kfree(ovcs->overlay_tree);
679 * but can not since drivers may have pointers into this data
680 *
681 * would like to: kfree(ovcs->fdt);
682 * but can not since drivers may have pointers into this data
683 */
684
Frank Rowand61b4de42017-10-17 16:36:25 -0700685 kfree(ovcs);
686}
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200687
Frank Rowand39a751a2018-02-12 00:19:42 -0800688/*
689 * internal documentation
690 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700691 * of_overlay_apply() - Create and apply an overlay changeset
Frank Rowand39a751a2018-02-12 00:19:42 -0800692 * @fdt: the FDT that was unflattened to create @tree
Frank Rowand0290c4c2017-10-17 16:36:23 -0700693 * @tree: Expanded overlay device tree
Frank Rowand24789c52017-10-17 16:36:26 -0700694 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200695 *
Frank Rowand24789c52017-10-17 16:36:26 -0700696 * Creates and applies an overlay changeset.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200697 *
Frank Rowand24789c52017-10-17 16:36:26 -0700698 * If an error occurs in a pre-apply notifier, then no changes are made
699 * to the device tree.
700 *
701
702 * A non-zero return value will not have created the changeset if error is from:
703 * - parameter checks
704 * - building the changeset
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100705 * - overlay changeset pre-apply notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700706 *
707 * If an error is returned by an overlay changeset pre-apply notifier
708 * then no further overlay changeset pre-apply notifier will be called.
709 *
710 * A non-zero return value will have created the changeset if error is from:
711 * - overlay changeset entry notifier
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100712 * - overlay changeset post-apply notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700713 *
714 * If an error is returned by an overlay changeset post-apply notifier
715 * then no further overlay changeset post-apply notifier will be called.
716 *
717 * If more than one notifier returns an error, then the last notifier
718 * error to occur is returned.
719 *
720 * If an error occurred while applying the overlay changeset, then an
721 * attempt is made to revert any changes that were made to the
722 * device tree. If there were any errors during the revert attempt
723 * then the state of the device tree can not be determined, and any
724 * following attempt to apply or remove an overlay changeset will be
725 * refused.
726 *
727 * Returns 0 on success, or a negative error number. Overlay changeset
728 * id is returned to *ovcs_id.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200729 */
Frank Rowand24789c52017-10-17 16:36:26 -0700730
Frank Rowand39a751a2018-02-12 00:19:42 -0800731static int of_overlay_apply(const void *fdt, struct device_node *tree,
732 int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200733{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700734 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700735 int ret = 0, ret_revert, ret_tmp;
736
Frank Rowand39a751a2018-02-12 00:19:42 -0800737 /*
738 * As of this point, fdt and tree belong to the overlay changeset.
739 * overlay changeset code is responsible for freeing them.
740 */
Frank Rowand24789c52017-10-17 16:36:26 -0700741
742 if (devicetree_corrupt()) {
743 pr_err("devicetree state suspect, refuse to apply overlay\n");
Frank Rowand39a751a2018-02-12 00:19:42 -0800744 kfree(fdt);
745 kfree(tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700746 ret = -EBUSY;
747 goto out;
748 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200749
Frank Rowand0290c4c2017-10-17 16:36:23 -0700750 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
Frank Rowand24789c52017-10-17 16:36:26 -0700751 if (!ovcs) {
Frank Rowand39a751a2018-02-12 00:19:42 -0800752 kfree(fdt);
753 kfree(tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700754 ret = -ENOMEM;
755 goto out;
756 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200757
Frank Rowandf948d6d2017-10-17 16:36:29 -0700758 of_overlay_mutex_lock();
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100759 mutex_lock(&of_mutex);
Frank Rowandf948d6d2017-10-17 16:36:29 -0700760
761 ret = of_resolve_phandles(tree);
762 if (ret)
Frank Rowand39a751a2018-02-12 00:19:42 -0800763 goto err_free_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200764
Frank Rowand39a751a2018-02-12 00:19:42 -0800765 ret = init_overlay_changeset(ovcs, fdt, tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700766 if (ret)
Frank Rowand39a751a2018-02-12 00:19:42 -0800767 goto err_free_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200768
Frank Rowand39a751a2018-02-12 00:19:42 -0800769 /*
770 * after overlay_notify(), ovcs->overlay_tree related pointers may have
771 * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
772 * and can not free fdt, aka ovcs->fdt
773 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700774 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
Frank Rowand24789c52017-10-17 16:36:26 -0700775 if (ret) {
776 pr_err("overlay changeset pre-apply notify error %d\n", ret);
Frank Rowand61b4de42017-10-17 16:36:25 -0700777 goto err_free_overlay_changeset;
Alan Tull39a842e2016-11-01 14:14:22 -0500778 }
779
Frank Rowand0290c4c2017-10-17 16:36:23 -0700780 ret = build_changeset(ovcs);
781 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700782 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200783
Frank Rowand24789c52017-10-17 16:36:26 -0700784 ret_revert = 0;
785 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
786 if (ret) {
787 if (ret_revert) {
788 pr_debug("overlay changeset revert error %d\n",
789 ret_revert);
790 devicetree_state_flags |= DTSF_APPLY_FAIL;
791 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700792 goto err_free_overlay_changeset;
Frank Rowand24789c52017-10-17 16:36:26 -0700793 }
Rob Herring606ad422016-06-15 08:32:18 -0500794
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100795 ret = __of_changeset_apply_notify(&ovcs->cset);
796 if (ret)
797 pr_err("overlay changeset entry notify error %d\n", ret);
798 /* notify failure is not fatal, continue */
799
Frank Rowand0290c4c2017-10-17 16:36:23 -0700800 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
Frank Rowand24789c52017-10-17 16:36:26 -0700801 *ovcs_id = ovcs->id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200802
Frank Rowand24789c52017-10-17 16:36:26 -0700803 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
804 if (ret_tmp) {
805 pr_err("overlay changeset post-apply notify error %d\n",
806 ret_tmp);
807 if (!ret)
808 ret = ret_tmp;
809 }
Alan Tull39a842e2016-11-01 14:14:22 -0500810
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100811 goto out_unlock;
Frank Rowandf948d6d2017-10-17 16:36:29 -0700812
Frank Rowand39a751a2018-02-12 00:19:42 -0800813err_free_tree:
814 kfree(fdt);
815 kfree(tree);
816
Frank Rowand61b4de42017-10-17 16:36:25 -0700817err_free_overlay_changeset:
818 free_overlay_changeset(ovcs);
819
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100820out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200821 mutex_unlock(&of_mutex);
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100822 of_overlay_mutex_unlock();
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200823
Frank Rowand24789c52017-10-17 16:36:26 -0700824out:
825 pr_debug("%s() err=%d\n", __func__, ret);
826
Frank Rowand0290c4c2017-10-17 16:36:23 -0700827 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200828}
Frank Rowand39a751a2018-02-12 00:19:42 -0800829
830int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
831 int *ovcs_id)
832{
833 const void *new_fdt;
834 int ret;
835 u32 size;
836 struct device_node *overlay_root;
837
838 *ovcs_id = 0;
839 ret = 0;
840
841 if (overlay_fdt_size < sizeof(struct fdt_header) ||
842 fdt_check_header(overlay_fdt)) {
843 pr_err("Invalid overlay_fdt header\n");
844 return -EINVAL;
845 }
846
847 size = fdt_totalsize(overlay_fdt);
848 if (overlay_fdt_size < size)
849 return -EINVAL;
850
851 /*
852 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
853 * will create pointers to the passed in FDT in the unflattened tree.
854 */
855 new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
856 if (!new_fdt)
857 return -ENOMEM;
858
859 of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
860 if (!overlay_root) {
861 pr_err("unable to unflatten overlay_fdt\n");
862 ret = -EINVAL;
863 goto out_free_new_fdt;
864 }
865
866 ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
867 if (ret < 0) {
868 /*
869 * new_fdt and overlay_root now belong to the overlay
870 * changeset.
871 * overlay changeset code is responsible for freeing them.
872 */
873 goto out;
874 }
875
876 return 0;
877
878
879out_free_new_fdt:
880 kfree(new_fdt);
881
882out:
883 return ret;
884}
885EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200886
Frank Rowand646afc42017-10-17 16:36:21 -0700887/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700888 * Find @np in @tree.
889 *
890 * Returns 1 if @np is @tree or is contained in @tree, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700891 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700892static int find_node(struct device_node *tree, struct device_node *np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200893{
894 struct device_node *child;
895
Frank Rowand0290c4c2017-10-17 16:36:23 -0700896 if (tree == np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200897 return 1;
898
899 for_each_child_of_node(tree, child) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700900 if (find_node(child, np)) {
Julia Lawall001cf502015-10-22 11:02:48 +0200901 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200902 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200903 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200904 }
905
906 return 0;
907}
908
Frank Rowand646afc42017-10-17 16:36:21 -0700909/*
Frank Rowand87f242c2017-10-17 16:36:27 -0700910 * Is @remove_ce_node a child of, a parent of, or the same as any
Frank Rowand0290c4c2017-10-17 16:36:23 -0700911 * node in an overlay changeset more topmost than @remove_ovcs?
912 *
913 * Returns 1 if found, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700914 */
Frank Rowand87f242c2017-10-17 16:36:27 -0700915static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
916 struct device_node *remove_ce_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200917{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700918 struct overlay_changeset *ovcs;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200919 struct of_changeset_entry *ce;
920
Frank Rowand0290c4c2017-10-17 16:36:23 -0700921 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
922 if (ovcs == remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200923 break;
924
Frank Rowand0290c4c2017-10-17 16:36:23 -0700925 list_for_each_entry(ce, &ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700926 if (find_node(ce->np, remove_ce_node)) {
927 pr_err("%s: #%d overlaps with #%d @%pOF\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700928 __func__, remove_ovcs->id, ovcs->id,
Frank Rowand87f242c2017-10-17 16:36:27 -0700929 remove_ce_node);
930 return 1;
931 }
932 if (find_node(remove_ce_node, ce->np)) {
933 pr_err("%s: #%d overlaps with #%d @%pOF\n",
934 __func__, remove_ovcs->id, ovcs->id,
935 remove_ce_node);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700936 return 1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200937 }
938 }
939 }
940
Frank Rowand0290c4c2017-10-17 16:36:23 -0700941 return 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200942}
943
944/*
945 * We can safely remove the overlay only if it's the top-most one.
946 * Newly applied overlays are inserted at the tail of the overlay list,
947 * so a top most overlay is the one that is closest to the tail.
948 *
949 * The topmost check is done by exploiting this property. For each
950 * affected device node in the log list we check if this overlay is
951 * the one closest to the tail. If another overlay has affected this
952 * device node and is closest to the tail, then removal is not permited.
953 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700954static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200955{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700956 struct of_changeset_entry *remove_ce;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200957
Frank Rowand0290c4c2017-10-17 16:36:23 -0700958 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700959 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700960 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200961 return 0;
962 }
963 }
964
965 return 1;
966}
967
968/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700969 * of_overlay_remove() - Revert and free an overlay changeset
Frank Rowand24789c52017-10-17 16:36:26 -0700970 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200971 *
Frank Rowand24789c52017-10-17 16:36:26 -0700972 * Removes an overlay if it is permissible. @ovcs_id was previously returned
Frank Rowand0290c4c2017-10-17 16:36:23 -0700973 * by of_overlay_apply().
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200974 *
Frank Rowand24789c52017-10-17 16:36:26 -0700975 * If an error occurred while attempting to revert the overlay changeset,
976 * then an attempt is made to re-apply any changeset entry that was
977 * reverted. If an error occurs on re-apply then the state of the device
978 * tree can not be determined, and any following attempt to apply or remove
979 * an overlay changeset will be refused.
980 *
981 * A non-zero return value will not revert the changeset if error is from:
982 * - parameter checks
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100983 * - overlay changeset pre-remove notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700984 * - overlay changeset entry revert
985 *
986 * If an error is returned by an overlay changeset pre-remove notifier
987 * then no further overlay changeset pre-remove notifier will be called.
988 *
989 * If more than one notifier returns an error, then the last notifier
990 * error to occur is returned.
991 *
992 * A non-zero return value will revert the changeset if error is from:
993 * - overlay changeset entry notifier
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100994 * - overlay changeset post-remove notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700995 *
996 * If an error is returned by an overlay changeset post-remove notifier
997 * then no further overlay changeset post-remove notifier will be called.
998 *
999 * Returns 0 on success, or a negative error number. *ovcs_id is set to
1000 * zero after reverting the changeset, even if a subsequent error occurs.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001001 */
Frank Rowand24789c52017-10-17 16:36:26 -07001002int of_overlay_remove(int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001003{
Frank Rowand0290c4c2017-10-17 16:36:23 -07001004 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -07001005 int ret, ret_apply, ret_tmp;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001006
Frank Rowand24789c52017-10-17 16:36:26 -07001007 ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001008
Frank Rowand24789c52017-10-17 16:36:26 -07001009 if (devicetree_corrupt()) {
1010 pr_err("suspect devicetree state, refuse to remove overlay\n");
Frank Rowand0290c4c2017-10-17 16:36:23 -07001011 ret = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001012 goto out;
1013 }
1014
Frank Rowand24789c52017-10-17 16:36:26 -07001015 mutex_lock(&of_mutex);
1016
1017 ovcs = idr_find(&ovcs_idr, *ovcs_id);
1018 if (!ovcs) {
1019 ret = -ENODEV;
1020 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
1021 goto out_unlock;
1022 }
1023
1024 if (!overlay_removal_is_ok(ovcs)) {
1025 ret = -EBUSY;
1026 goto out_unlock;
1027 }
1028
1029 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
1030 if (ret) {
1031 pr_err("overlay changeset pre-remove notify error %d\n", ret);
1032 goto out_unlock;
1033 }
Frank Rowand61b4de42017-10-17 16:36:25 -07001034
Frank Rowand0290c4c2017-10-17 16:36:23 -07001035 list_del(&ovcs->ovcs_list);
Frank Rowand61b4de42017-10-17 16:36:25 -07001036
Frank Rowand24789c52017-10-17 16:36:26 -07001037 ret_apply = 0;
1038 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
1039 if (ret) {
1040 if (ret_apply)
1041 devicetree_state_flags |= DTSF_REVERT_FAIL;
1042 goto out_unlock;
Frank Rowand24789c52017-10-17 16:36:26 -07001043 }
Frank Rowand61b4de42017-10-17 16:36:25 -07001044
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +01001045 ret = __of_changeset_revert_notify(&ovcs->cset);
1046 if (ret)
1047 pr_err("overlay changeset entry notify error %d\n", ret);
1048 /* notify failure is not fatal, continue */
1049
Frank Rowand24789c52017-10-17 16:36:26 -07001050 *ovcs_id = 0;
1051
1052 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
1053 if (ret_tmp) {
1054 pr_err("overlay changeset post-remove notify error %d\n",
1055 ret_tmp);
1056 if (!ret)
1057 ret = ret_tmp;
1058 }
Frank Rowand61b4de42017-10-17 16:36:25 -07001059
1060 free_overlay_changeset(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001061
Frank Rowand24789c52017-10-17 16:36:26 -07001062out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001063 mutex_unlock(&of_mutex);
1064
Frank Rowand24789c52017-10-17 16:36:26 -07001065out:
1066 pr_debug("%s() err=%d\n", __func__, ret);
1067
Frank Rowand0290c4c2017-10-17 16:36:23 -07001068 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001069}
Frank Rowand0290c4c2017-10-17 16:36:23 -07001070EXPORT_SYMBOL_GPL(of_overlay_remove);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001071
1072/**
Frank Rowand0290c4c2017-10-17 16:36:23 -07001073 * of_overlay_remove_all() - Reverts and frees all overlay changesets
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001074 *
1075 * Removes all overlays from the system in the correct order.
1076 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +02001077 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001078 */
Frank Rowand0290c4c2017-10-17 16:36:23 -07001079int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001080{
Frank Rowand0290c4c2017-10-17 16:36:23 -07001081 struct overlay_changeset *ovcs, *ovcs_n;
Frank Rowand61b4de42017-10-17 16:36:25 -07001082 int ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001083
1084 /* the tail of list is guaranteed to be safe to remove */
Frank Rowand0290c4c2017-10-17 16:36:23 -07001085 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
Frank Rowand24789c52017-10-17 16:36:26 -07001086 ret = of_overlay_remove(&ovcs->id);
Frank Rowand61b4de42017-10-17 16:36:25 -07001087 if (ret)
1088 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001089 }
1090
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001091 return 0;
1092}
Frank Rowand0290c4c2017-10-17 16:36:23 -07001093EXPORT_SYMBOL_GPL(of_overlay_remove_all);