blob: 69610637af8802ceb82577edb433785f9ca7c660 [file] [log] [blame]
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001/*
2 * Functions for working with device tree overlays
3 *
4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
5 * Copyright (C) 2012 Texas Instruments Inc.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 */
Rob Herring606ad422016-06-15 08:32:18 -050011
12#define pr_fmt(fmt) "OF: overlay: " fmt
13
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/string.h>
19#include <linux/ctype.h>
20#include <linux/errno.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020021#include <linux/slab.h>
22#include <linux/err.h>
Mark Brown0d1886d2015-02-17 11:36:58 +090023#include <linux/idr.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020024
25#include "of_private.h"
26
27/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070028 * struct fragment - info about fragment nodes in overlay expanded device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020029 * @target: target of the overlay operation
Frank Rowand0290c4c2017-10-17 16:36:23 -070030 * @overlay: pointer to the __overlay__ node
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020031 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070032struct fragment {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020033 struct device_node *target;
34 struct device_node *overlay;
Frank Rowandd1651b02017-07-19 09:25:22 -070035 bool is_symbols_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020036};
37
38/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070039 * struct overlay_changeset
40 * @ovcs_list: list on which we are located
41 * @count: count of @fragments structures
42 * @fragments: info about fragment nodes in overlay expanded device tree
43 * @cset: changeset to apply fragments to live device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020044 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070045struct overlay_changeset {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020046 int id;
Frank Rowand0290c4c2017-10-17 16:36:23 -070047 struct list_head ovcs_list;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020048 int count;
Frank Rowand0290c4c2017-10-17 16:36:23 -070049 struct fragment *fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020050 struct of_changeset cset;
51};
52
Frank Rowand0290c4c2017-10-17 16:36:23 -070053static int build_changeset_next_level(struct overlay_changeset *ovcs,
54 struct device_node *target_node,
55 const struct device_node *overlay_node,
Frank Rowandd1651b02017-07-19 09:25:22 -070056 bool is_symbols_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020057
Frank Rowand0290c4c2017-10-17 16:36:23 -070058static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
Alan Tull39a842e2016-11-01 14:14:22 -050059
60int of_overlay_notifier_register(struct notifier_block *nb)
61{
Frank Rowand0290c4c2017-10-17 16:36:23 -070062 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -050063}
64EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
65
66int of_overlay_notifier_unregister(struct notifier_block *nb)
67{
Frank Rowand0290c4c2017-10-17 16:36:23 -070068 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -050069}
70EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
71
Frank Rowand0290c4c2017-10-17 16:36:23 -070072static int overlay_notify(struct overlay_changeset *ovcs,
73 enum of_overlay_notify_action action)
Alan Tull39a842e2016-11-01 14:14:22 -050074{
75 struct of_overlay_notify_data nd;
76 int i, ret;
77
Frank Rowand0290c4c2017-10-17 16:36:23 -070078 for (i = 0; i < ovcs->count; i++) {
79 struct fragment *fragment = &ovcs->fragments[i];
Alan Tull39a842e2016-11-01 14:14:22 -050080
Frank Rowand0290c4c2017-10-17 16:36:23 -070081 nd.target = fragment->target;
82 nd.overlay = fragment->overlay;
Alan Tull39a842e2016-11-01 14:14:22 -050083
Frank Rowand0290c4c2017-10-17 16:36:23 -070084 ret = blocking_notifier_call_chain(&overlay_notify_chain,
Alan Tull39a842e2016-11-01 14:14:22 -050085 action, &nd);
86 if (ret)
87 return notifier_to_errno(ret);
88 }
89
90 return 0;
91}
92
Frank Rowand0290c4c2017-10-17 16:36:23 -070093static struct property *dup_and_fixup_symbol_prop(
94 struct overlay_changeset *ovcs, const struct property *prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020095{
Frank Rowand0290c4c2017-10-17 16:36:23 -070096 struct fragment *fragment;
Frank Rowandd1651b02017-07-19 09:25:22 -070097 struct property *new;
98 const char *overlay_name;
99 char *label_path;
100 char *symbol_path;
101 const char *target_path;
102 int k;
103 int label_path_len;
104 int overlay_name_len;
105 int target_path_len;
106
107 if (!prop->value)
108 return NULL;
109 symbol_path = prop->value;
110
111 new = kzalloc(sizeof(*new), GFP_KERNEL);
112 if (!new)
113 return NULL;
114
Frank Rowand0290c4c2017-10-17 16:36:23 -0700115 for (k = 0; k < ovcs->count; k++) {
116 fragment = &ovcs->fragments[k];
117 overlay_name = fragment->overlay->full_name;
Frank Rowandd1651b02017-07-19 09:25:22 -0700118 overlay_name_len = strlen(overlay_name);
119 if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
120 break;
121 }
122
Frank Rowand0290c4c2017-10-17 16:36:23 -0700123 if (k >= ovcs->count)
Frank Rowandd1651b02017-07-19 09:25:22 -0700124 goto err_free;
125
Frank Rowand0290c4c2017-10-17 16:36:23 -0700126 target_path = fragment->target->full_name;
Frank Rowandd1651b02017-07-19 09:25:22 -0700127 target_path_len = strlen(target_path);
128
129 label_path = symbol_path + overlay_name_len;
130 label_path_len = strlen(label_path);
131
132 new->name = kstrdup(prop->name, GFP_KERNEL);
133 new->length = target_path_len + label_path_len + 1;
134 new->value = kzalloc(new->length, GFP_KERNEL);
135
136 if (!new->name || !new->value)
137 goto err_free;
138
139 strcpy(new->value, target_path);
140 strcpy(new->value + target_path_len, label_path);
141
Frank Rowandd1651b02017-07-19 09:25:22 -0700142 of_property_set_flag(new, OF_DYNAMIC);
143
144 return new;
145
146 err_free:
147 kfree(new->name);
148 kfree(new->value);
149 kfree(new);
150 return NULL;
151
152
153}
154
Frank Rowand0290c4c2017-10-17 16:36:23 -0700155/**
156 * add_changeset_property() - add @overlay_prop to overlay changeset
157 * @ovcs: overlay changeset
158 * @target_node: where to place @overlay_prop in live tree
159 * @overlay_prop: property to add or update, from overlay tree
160 * is_symbols_node: 1 if @target_node is "/__symbols__"
161 *
162 * If @overlay_prop does not already exist in @target_node, add changeset entry
163 * to add @overlay_prop in @target_node, else add changeset entry to update
164 * value of @overlay_prop.
165 *
Frank Rowand646afc42017-10-17 16:36:21 -0700166 * Some special properties are not updated (no error returned).
Frank Rowand0290c4c2017-10-17 16:36:23 -0700167 *
Frank Rowand646afc42017-10-17 16:36:21 -0700168 * Update of property in symbols node is not allowed.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700169 *
170 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
171 * invalid @overlay.
Frank Rowand646afc42017-10-17 16:36:21 -0700172 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700173static int add_changeset_property(struct overlay_changeset *ovcs,
174 struct device_node *target_node,
175 struct property *overlay_prop,
Frank Rowandd1651b02017-07-19 09:25:22 -0700176 bool is_symbols_node)
177{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700178 struct property *new_prop = NULL, *prop;
Lixin Wangac0f3e32017-10-16 17:54:32 +0800179 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200180
Frank Rowand0290c4c2017-10-17 16:36:23 -0700181 prop = of_find_property(target_node, overlay_prop->name, NULL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200182
Frank Rowand0290c4c2017-10-17 16:36:23 -0700183 if (!of_prop_cmp(overlay_prop->name, "name") ||
184 !of_prop_cmp(overlay_prop->name, "phandle") ||
185 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200186 return 0;
187
Frank Rowandd1651b02017-07-19 09:25:22 -0700188 if (is_symbols_node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700189 if (prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700190 return -EINVAL;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700191 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
Frank Rowandd1651b02017-07-19 09:25:22 -0700192 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700193 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
Frank Rowandd1651b02017-07-19 09:25:22 -0700194 }
195
Frank Rowand0290c4c2017-10-17 16:36:23 -0700196 if (!new_prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200197 return -ENOMEM;
198
Frank Rowand0290c4c2017-10-17 16:36:23 -0700199 if (!prop)
200 ret = of_changeset_add_property(&ovcs->cset, target_node,
201 new_prop);
Frank Rowand646afc42017-10-17 16:36:21 -0700202 else
Frank Rowand0290c4c2017-10-17 16:36:23 -0700203 ret = of_changeset_update_property(&ovcs->cset, target_node,
204 new_prop);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200205
Lixin Wangac0f3e32017-10-16 17:54:32 +0800206 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700207 kfree(new_prop->name);
208 kfree(new_prop->value);
209 kfree(new_prop);
Lixin Wangac0f3e32017-10-16 17:54:32 +0800210 }
211 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200212}
213
Frank Rowand0290c4c2017-10-17 16:36:23 -0700214/**
215 * add_changeset_node() - add @node (and children) to overlay changeset
216 * @ovcs: overlay changeset
217 * @target_node: where to place @node in live tree
218 * @node: node from within overlay device tree fragment
219 *
220 * If @node does not already exist in @target_node, add changeset entry
221 * to add @node in @target_node.
222 *
223 * If @node already exists in @target_node, and the existing node has
224 * a phandle, the overlay node is not allowed to have a phandle.
225 *
226 * If @node has child nodes, add the children recursively via
227 * build_changeset_next_level().
228 *
229 * NOTE: Multiple mods of created nodes not supported.
230 *
231 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
232 * invalid @overlay.
233 */
234static int add_changeset_node(struct overlay_changeset *ovcs,
235 struct device_node *target_node, struct device_node *node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200236{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700237 const char *node_kbasename;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300238 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200239 int ret = 0;
240
Frank Rowand0290c4c2017-10-17 16:36:23 -0700241 node_kbasename = kbasename(node->full_name);
242 if (!node_kbasename)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200243 return -ENOMEM;
244
Frank Rowand0290c4c2017-10-17 16:36:23 -0700245 for_each_child_of_node(target_node, tchild)
246 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700247 break;
248
Frank Rowandbbed8792017-10-17 16:36:22 -0700249 if (tchild) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700250 if (node->phandle)
Frank Rowand7a12f452017-06-21 12:21:56 -0700251 return -EINVAL;
252
Frank Rowand0290c4c2017-10-17 16:36:23 -0700253 ret = build_changeset_next_level(ovcs, tchild, node, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200254 of_node_put(tchild);
255 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700256 tchild = __of_node_dup(node, "%pOF/%s",
257 target_node, node_kbasename);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200258 if (!tchild)
259 return -ENOMEM;
260
Frank Rowand0290c4c2017-10-17 16:36:23 -0700261 tchild->parent = target_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200262
Frank Rowand0290c4c2017-10-17 16:36:23 -0700263 ret = of_changeset_attach_node(&ovcs->cset, tchild);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200264 if (ret)
265 return ret;
266
Frank Rowand0290c4c2017-10-17 16:36:23 -0700267 ret = build_changeset_next_level(ovcs, tchild, node, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200268 if (ret)
269 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200270 }
271
272 return ret;
273}
274
Frank Rowand0290c4c2017-10-17 16:36:23 -0700275/**
276 * build_changeset_next_level() - add level of overlay changeset
277 * @ovcs: overlay changeset
278 * @target_node: where to place @overlay_node in live tree
279 * @overlay_node: node from within an overlay device tree fragment
280 * @is_symbols_node: @overlay_node is node "/__symbols__"
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200281 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700282 * Add the properties (if any) and nodes (if any) from @overlay_node to the
283 * @ovcs->cset changeset. If an added node has child nodes, they will
284 * be added recursively.
Frank Rowand646afc42017-10-17 16:36:21 -0700285 *
286 * Do not allow symbols node to have any children.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700287 *
288 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
289 * invalid @overlay_node.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200290 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700291static int build_changeset_next_level(struct overlay_changeset *ovcs,
292 struct device_node *target_node,
293 const struct device_node *overlay_node,
Frank Rowandd1651b02017-07-19 09:25:22 -0700294 bool is_symbols_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200295{
296 struct device_node *child;
297 struct property *prop;
298 int ret;
299
Frank Rowand0290c4c2017-10-17 16:36:23 -0700300 for_each_property_of_node(overlay_node, prop) {
301 ret = add_changeset_property(ovcs, target_node, prop,
302 is_symbols_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200303 if (ret) {
Rob Herring0d638a02017-06-01 15:50:55 -0500304 pr_err("Failed to apply prop @%pOF/%s\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700305 target_node, prop->name);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200306 return ret;
307 }
308 }
309
Frank Rowandd1651b02017-07-19 09:25:22 -0700310 if (is_symbols_node)
311 return 0;
312
Frank Rowand0290c4c2017-10-17 16:36:23 -0700313 for_each_child_of_node(overlay_node, child) {
314 ret = add_changeset_node(ovcs, target_node, child);
Frank Rowandbbed8792017-10-17 16:36:22 -0700315 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700316 pr_err("Failed to apply node @%pOF/%s\n",
317 target_node, child->name);
Julia Lawall001cf502015-10-22 11:02:48 +0200318 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200319 return ret;
320 }
321 }
322
323 return 0;
324}
325
326/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700327 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
328 * @ovcs: Overlay changeset
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200329 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700330 * Create changeset @ovcs->cset to contain the nodes and properties of the
331 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
332 * any portions of the changeset that were successfully created will remain
333 * in @ovcs->cset.
334 *
335 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
336 * invalid overlay in @ovcs->fragments[].
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200337 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700338static int build_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200339{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700340 int i, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200341
Frank Rowand0290c4c2017-10-17 16:36:23 -0700342 for (i = 0; i < ovcs->count; i++) {
343 struct fragment *fragment = &ovcs->fragments[i];
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200344
Frank Rowand0290c4c2017-10-17 16:36:23 -0700345 ret = build_changeset_next_level(ovcs, fragment->target,
346 fragment->overlay,
347 fragment->is_symbols_node);
348 if (ret) {
349 pr_err("apply failed '%pOF'\n", fragment->target);
350 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200351 }
352 }
353
354 return 0;
355}
356
357/*
358 * Find the target node using a number of different strategies
Frank Rowand646afc42017-10-17 16:36:21 -0700359 * in order of preference:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200360 *
Frank Rowand646afc42017-10-17 16:36:21 -0700361 * 1) "target" property containing the phandle of the target
362 * 2) "target-path" property containing the path of the target
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200363 */
364static struct device_node *find_target_node(struct device_node *info_node)
365{
366 const char *path;
367 u32 val;
368 int ret;
369
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200370 ret = of_property_read_u32(info_node, "target", &val);
Frank Rowandbbed8792017-10-17 16:36:22 -0700371 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200372 return of_find_node_by_phandle(val);
373
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200374 ret = of_property_read_string(info_node, "target-path", &path);
Frank Rowandbbed8792017-10-17 16:36:22 -0700375 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200376 return of_find_node_by_path(path);
377
Rob Herring606ad422016-06-15 08:32:18 -0500378 pr_err("Failed to find target for node %p (%s)\n",
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200379 info_node, info_node->name);
380
381 return NULL;
382}
383
384/**
385 * of_fill_overlay_info() - Fill an overlay info structure
386 * @ov Overlay to fill
387 * @info_node: Device node containing the overlay
388 * @ovinfo: Pointer to the overlay info structure to fill
389 *
390 * Fills an overlay info structure with the overlay information
391 * from a device node. This device node must have a target property
392 * which contains a phandle of the overlay target node, and an
393 * __overlay__ child node which has the overlay contents.
394 * Both ovinfo->target & ovinfo->overlay have their references taken.
395 *
396 * Returns 0 on success, or a negative error value.
397 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700398static int of_fill_overlay_info(struct overlay_changeset *ovcset,
399 struct device_node *info_node, struct fragment *fragment)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200400{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700401 fragment->overlay = of_get_child_by_name(info_node, "__overlay__");
402 if (!fragment->overlay)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200403 goto err_fail;
404
Frank Rowand0290c4c2017-10-17 16:36:23 -0700405 fragment->target = find_target_node(info_node);
406 if (!fragment->target)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200407 goto err_fail;
408
409 return 0;
410
411err_fail:
Frank Rowand0290c4c2017-10-17 16:36:23 -0700412 of_node_put(fragment->target);
413 of_node_put(fragment->overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200414
Frank Rowand0290c4c2017-10-17 16:36:23 -0700415 memset(fragment, 0, sizeof(*fragment));
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200416 return -EINVAL;
417}
418
419/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700420 * init_overlay_changeset() - initialize overlay changeset from overlay tree
421 * @ovcs Overlay changeset to build
422 * @tree: Contains all the overlay fragments and overlay fixup nodes
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200423 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700424 * Initialize @ovcs. Populate @ovcs->fragments with node information from
425 * the top level of @tree. The relevant top level nodes are the fragment
426 * nodes and the __symbols__ node. Any other top level node will be ignored.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200427 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700428 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
429 * detected in @tree, or -ENODEV if no valid nodes found.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200430 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700431static int init_overlay_changeset(struct overlay_changeset *ovcs,
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200432 struct device_node *tree)
433{
434 struct device_node *node;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700435 struct fragment *fragment;
436 struct fragment *fragments;
437 int cnt, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200438
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200439 cnt = 0;
440 for_each_child_of_node(tree, node)
441 cnt++;
442
Frank Rowandd1651b02017-07-19 09:25:22 -0700443 if (of_get_child_by_name(tree, "__symbols__"))
444 cnt++;
445
Frank Rowand0290c4c2017-10-17 16:36:23 -0700446 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
447 if (!fragments)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200448 return -ENOMEM;
449
450 cnt = 0;
451 for_each_child_of_node(tree, node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700452 ret = of_fill_overlay_info(ovcs, node, &fragments[cnt]);
453 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200454 cnt++;
455 }
456
Frank Rowandd1651b02017-07-19 09:25:22 -0700457 node = of_get_child_by_name(tree, "__symbols__");
458 if (node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700459 fragment = &fragments[cnt];
460 fragment->overlay = node;
461 fragment->target = of_find_node_by_path("/__symbols__");
462 fragment->is_symbols_node = 1;
Frank Rowandd1651b02017-07-19 09:25:22 -0700463
Frank Rowand0290c4c2017-10-17 16:36:23 -0700464 if (!fragment->target) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700465 pr_err("no symbols in root of device tree.\n");
466 return -EINVAL;
467 }
468
469 cnt++;
470 }
471
Frank Rowandbbed8792017-10-17 16:36:22 -0700472 if (!cnt) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700473 kfree(fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200474 return -ENODEV;
475 }
476
Frank Rowand0290c4c2017-10-17 16:36:23 -0700477 ovcs->count = cnt;
478 ovcs->fragments = fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200479
480 return 0;
481}
482
483/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700484 * free_overlay_fragments() - Free a fragments array
485 * @ovcs Overlay to free the overlay info from
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200486 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700487 * Frees the memory of an ovcs->fragments[] array.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200488 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700489static void free_overlay_fragments(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200490{
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200491 int i;
492
493 /* do it in reverse */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700494 for (i = ovcs->count - 1; i >= 0; i--) {
495 of_node_put(ovcs->fragments[i].target);
496 of_node_put(ovcs->fragments[i].overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200497 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200498
Frank Rowand0290c4c2017-10-17 16:36:23 -0700499 kfree(ovcs->fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200500}
501
Frank Rowand0290c4c2017-10-17 16:36:23 -0700502static LIST_HEAD(ovcs_list);
503static DEFINE_IDR(ovcs_idr);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200504
505/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700506 * of_overlay_apply() - Create and apply an overlay changeset
507 * @tree: Expanded overlay device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200508 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700509 * Creates and applies an overlay changeset. If successful, the overlay
510 * changeset is added to the overlay changeset list.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200511 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700512 * Returns the id of the created overlay changeset, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200513 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700514int of_overlay_apply(struct device_node *tree)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200515{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700516 struct overlay_changeset *ovcs;
517 int id, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200518
Frank Rowand0290c4c2017-10-17 16:36:23 -0700519 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
520 if (!ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200521 return -ENOMEM;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700522 ovcs->id = -1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200523
Frank Rowand0290c4c2017-10-17 16:36:23 -0700524 INIT_LIST_HEAD(&ovcs->ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200525
Frank Rowand0290c4c2017-10-17 16:36:23 -0700526 of_changeset_init(&ovcs->cset);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200527
528 mutex_lock(&of_mutex);
529
Frank Rowand0290c4c2017-10-17 16:36:23 -0700530 id = idr_alloc(&ovcs_idr, ovcs, 0, 0, GFP_KERNEL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200531 if (id < 0) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700532 ret = id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200533 goto err_destroy_trans;
534 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700535 ovcs->id = id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200536
Frank Rowand0290c4c2017-10-17 16:36:23 -0700537 ret = init_overlay_changeset(ovcs, tree);
538 if (ret) {
539 pr_err("init_overlay_changeset() failed for tree@%pOF\n",
Rob Herring0d638a02017-06-01 15:50:55 -0500540 tree);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200541 goto err_free_idr;
542 }
543
Frank Rowand0290c4c2017-10-17 16:36:23 -0700544 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
545 if (ret < 0) {
546 pr_err("%s: Pre-apply notifier failed (ret=%d)\n",
547 __func__, ret);
548 goto err_free_overlay_fragments;
Alan Tull39a842e2016-11-01 14:14:22 -0500549 }
550
Frank Rowand0290c4c2017-10-17 16:36:23 -0700551 ret = build_changeset(ovcs);
552 if (ret)
553 goto err_free_overlay_fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200554
Frank Rowand0290c4c2017-10-17 16:36:23 -0700555 ret = __of_changeset_apply(&ovcs->cset);
556 if (ret)
557 goto err_free_overlay_fragments;
Rob Herring606ad422016-06-15 08:32:18 -0500558
Frank Rowand0290c4c2017-10-17 16:36:23 -0700559 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200560
Frank Rowand0290c4c2017-10-17 16:36:23 -0700561 overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
Alan Tull39a842e2016-11-01 14:14:22 -0500562
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200563 mutex_unlock(&of_mutex);
564
565 return id;
566
Frank Rowand0290c4c2017-10-17 16:36:23 -0700567err_free_overlay_fragments:
568 free_overlay_fragments(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200569err_free_idr:
Frank Rowand0290c4c2017-10-17 16:36:23 -0700570 idr_remove(&ovcs_idr, ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200571err_destroy_trans:
Frank Rowand0290c4c2017-10-17 16:36:23 -0700572 of_changeset_destroy(&ovcs->cset);
573 kfree(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200574 mutex_unlock(&of_mutex);
575
Frank Rowand0290c4c2017-10-17 16:36:23 -0700576 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200577}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700578EXPORT_SYMBOL_GPL(of_overlay_apply);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200579
Frank Rowand646afc42017-10-17 16:36:21 -0700580/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700581 * Find @np in @tree.
582 *
583 * Returns 1 if @np is @tree or is contained in @tree, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700584 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700585static int find_node(struct device_node *tree, struct device_node *np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200586{
587 struct device_node *child;
588
Frank Rowand0290c4c2017-10-17 16:36:23 -0700589 if (tree == np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200590 return 1;
591
592 for_each_child_of_node(tree, child) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700593 if (find_node(child, np)) {
Julia Lawall001cf502015-10-22 11:02:48 +0200594 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200595 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200596 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200597 }
598
599 return 0;
600}
601
Frank Rowand646afc42017-10-17 16:36:21 -0700602/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700603 * Is @remove_ce_np a child of or the same as any
604 * node in an overlay changeset more topmost than @remove_ovcs?
605 *
606 * Returns 1 if found, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700607 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700608static int node_in_later_cs(struct overlay_changeset *remove_ovcs,
609 struct device_node *remove_ce_np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200610{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700611 struct overlay_changeset *ovcs;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200612 struct of_changeset_entry *ce;
613
Frank Rowand0290c4c2017-10-17 16:36:23 -0700614 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
615 if (ovcs == remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200616 break;
617
Frank Rowand0290c4c2017-10-17 16:36:23 -0700618 list_for_each_entry(ce, &ovcs->cset.entries, node) {
619 if (find_node(ce->np, remove_ce_np)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500620 pr_err("%s: #%d clashes #%d @%pOF\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700621 __func__, remove_ovcs->id, ovcs->id,
622 remove_ce_np);
623 return 1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200624 }
625 }
626 }
627
Frank Rowand0290c4c2017-10-17 16:36:23 -0700628 return 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200629}
630
631/*
632 * We can safely remove the overlay only if it's the top-most one.
633 * Newly applied overlays are inserted at the tail of the overlay list,
634 * so a top most overlay is the one that is closest to the tail.
635 *
636 * The topmost check is done by exploiting this property. For each
637 * affected device node in the log list we check if this overlay is
638 * the one closest to the tail. If another overlay has affected this
639 * device node and is closest to the tail, then removal is not permited.
640 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700641static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200642{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700643 struct of_changeset_entry *remove_ce;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200644
Frank Rowand0290c4c2017-10-17 16:36:23 -0700645 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
646 if (node_in_later_cs(remove_ovcs, remove_ce->np)) {
647 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200648 return 0;
649 }
650 }
651
652 return 1;
653}
654
655/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700656 * of_overlay_remove() - Revert and free an overlay changeset
657 * @ovcs_id: Overlay changeset id number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200658 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700659 * Removes an overlay if it is permissible. ovcs_id was previously returned
660 * by of_overlay_apply().
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200661 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200662 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200663 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700664int of_overlay_remove(int ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200665{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700666 struct overlay_changeset *ovcs;
667 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200668
669 mutex_lock(&of_mutex);
670
Frank Rowand0290c4c2017-10-17 16:36:23 -0700671 ovcs = idr_find(&ovcs_idr, ovcs_id);
672 if (!ovcs) {
673 ret = -ENODEV;
674 pr_err("remove: Could not find overlay #%d\n", ovcs_id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200675 goto out;
676 }
677
Frank Rowand0290c4c2017-10-17 16:36:23 -0700678 if (!overlay_removal_is_ok(ovcs)) {
679 ret = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200680 goto out;
681 }
682
Frank Rowand0290c4c2017-10-17 16:36:23 -0700683 overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
684 list_del(&ovcs->ovcs_list);
685 __of_changeset_revert(&ovcs->cset);
686 overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
687 free_overlay_fragments(ovcs);
688 idr_remove(&ovcs_idr, ovcs_id);
689 of_changeset_destroy(&ovcs->cset);
690 kfree(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200691
692out:
693 mutex_unlock(&of_mutex);
694
Frank Rowand0290c4c2017-10-17 16:36:23 -0700695 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200696}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700697EXPORT_SYMBOL_GPL(of_overlay_remove);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200698
699/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700700 * of_overlay_remove_all() - Reverts and frees all overlay changesets
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200701 *
702 * Removes all overlays from the system in the correct order.
703 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200704 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200705 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700706int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200707{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700708 struct overlay_changeset *ovcs, *ovcs_n;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200709
710 mutex_lock(&of_mutex);
711
712 /* the tail of list is guaranteed to be safe to remove */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700713 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
714 list_del(&ovcs->ovcs_list);
715 __of_changeset_revert(&ovcs->cset);
716 free_overlay_fragments(ovcs);
717 idr_remove(&ovcs_idr, ovcs->id);
718 kfree(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200719 }
720
721 mutex_unlock(&of_mutex);
722
723 return 0;
724}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700725EXPORT_SYMBOL_GPL(of_overlay_remove_all);