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