]>
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 | /** | |
28 | * struct of_overlay_info - Holds a single overlay info | |
29 | * @target: target of the overlay operation | |
30 | * @overlay: pointer to the overlay contents node | |
31 | * | |
32 | * Holds a single overlay state, including all the overlay logs & | |
33 | * records. | |
34 | */ | |
35 | struct of_overlay_info { | |
36 | struct device_node *target; | |
37 | struct device_node *overlay; | |
38 | }; | |
39 | ||
40 | /** | |
41 | * struct of_overlay - Holds a complete overlay transaction | |
42 | * @node: List on which we are located | |
43 | * @count: Count of ovinfo structures | |
44 | * @ovinfo_tab: Overlay info table (count sized) | |
45 | * @cset: Changeset to be used | |
46 | * | |
47 | * Holds a complete overlay transaction | |
48 | */ | |
49 | struct of_overlay { | |
50 | int id; | |
51 | struct list_head node; | |
52 | int count; | |
53 | struct of_overlay_info *ovinfo_tab; | |
54 | struct of_changeset cset; | |
55 | }; | |
56 | ||
57 | static int of_overlay_apply_one(struct of_overlay *ov, | |
58 | struct device_node *target, const struct device_node *overlay); | |
59 | ||
39a842e2 AT |
60 | static BLOCKING_NOTIFIER_HEAD(of_overlay_chain); |
61 | ||
62 | int of_overlay_notifier_register(struct notifier_block *nb) | |
63 | { | |
64 | return blocking_notifier_chain_register(&of_overlay_chain, nb); | |
65 | } | |
66 | EXPORT_SYMBOL_GPL(of_overlay_notifier_register); | |
67 | ||
68 | int of_overlay_notifier_unregister(struct notifier_block *nb) | |
69 | { | |
70 | return blocking_notifier_chain_unregister(&of_overlay_chain, nb); | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister); | |
73 | ||
74 | static int of_overlay_notify(struct of_overlay *ov, | |
75 | enum of_overlay_notify_action action) | |
76 | { | |
77 | struct of_overlay_notify_data nd; | |
78 | int i, ret; | |
79 | ||
80 | for (i = 0; i < ov->count; i++) { | |
81 | struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i]; | |
82 | ||
83 | nd.target = ovinfo->target; | |
84 | nd.overlay = ovinfo->overlay; | |
85 | ||
86 | ret = blocking_notifier_call_chain(&of_overlay_chain, | |
87 | action, &nd); | |
88 | if (ret) | |
89 | return notifier_to_errno(ret); | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
7518b589 PA |
95 | static int of_overlay_apply_single_property(struct of_overlay *ov, |
96 | struct device_node *target, struct property *prop) | |
97 | { | |
98 | struct property *propn, *tprop; | |
99 | ||
100 | /* NOTE: Multiple changes of single properties not supported */ | |
101 | tprop = of_find_property(target, prop->name, NULL); | |
102 | ||
103 | /* special properties are not meant to be updated (silent NOP) */ | |
104 | if (of_prop_cmp(prop->name, "name") == 0 || | |
105 | of_prop_cmp(prop->name, "phandle") == 0 || | |
106 | of_prop_cmp(prop->name, "linux,phandle") == 0) | |
107 | return 0; | |
108 | ||
109 | propn = __of_prop_dup(prop, GFP_KERNEL); | |
110 | if (propn == NULL) | |
111 | return -ENOMEM; | |
112 | ||
113 | /* not found? add */ | |
114 | if (tprop == NULL) | |
115 | return of_changeset_add_property(&ov->cset, target, propn); | |
116 | ||
117 | /* found? update */ | |
118 | return of_changeset_update_property(&ov->cset, target, propn); | |
119 | } | |
120 | ||
121 | static int of_overlay_apply_single_device_node(struct of_overlay *ov, | |
122 | struct device_node *target, struct device_node *child) | |
123 | { | |
124 | const char *cname; | |
d3a89165 | 125 | struct device_node *tchild; |
7518b589 PA |
126 | int ret = 0; |
127 | ||
128 | cname = kbasename(child->full_name); | |
129 | if (cname == NULL) | |
130 | return -ENOMEM; | |
131 | ||
132 | /* NOTE: Multiple mods of created nodes not supported */ | |
133 | tchild = of_get_child_by_name(target, cname); | |
134 | if (tchild != NULL) { | |
135 | /* apply overlay recursively */ | |
136 | ret = of_overlay_apply_one(ov, tchild, child); | |
137 | of_node_put(tchild); | |
138 | } else { | |
139 | /* create empty tree as a target */ | |
140 | tchild = __of_node_dup(child, "%s/%s", target->full_name, cname); | |
141 | if (!tchild) | |
142 | return -ENOMEM; | |
143 | ||
144 | /* point to parent */ | |
145 | tchild->parent = target; | |
146 | ||
147 | ret = of_changeset_attach_node(&ov->cset, tchild); | |
148 | if (ret) | |
149 | return ret; | |
150 | ||
151 | ret = of_overlay_apply_one(ov, tchild, child); | |
152 | if (ret) | |
153 | return ret; | |
7518b589 PA |
154 | } |
155 | ||
156 | return ret; | |
157 | } | |
158 | ||
159 | /* | |
160 | * Apply a single overlay node recursively. | |
161 | * | |
162 | * Note that the in case of an error the target node is left | |
163 | * in a inconsistent state. Error recovery should be performed | |
164 | * by using the changeset. | |
165 | */ | |
166 | static int of_overlay_apply_one(struct of_overlay *ov, | |
167 | struct device_node *target, const struct device_node *overlay) | |
168 | { | |
169 | struct device_node *child; | |
170 | struct property *prop; | |
171 | int ret; | |
172 | ||
173 | for_each_property_of_node(overlay, prop) { | |
174 | ret = of_overlay_apply_single_property(ov, target, prop); | |
175 | if (ret) { | |
606ad42a RH |
176 | pr_err("Failed to apply prop @%s/%s\n", |
177 | target->full_name, prop->name); | |
7518b589 PA |
178 | return ret; |
179 | } | |
180 | } | |
181 | ||
182 | for_each_child_of_node(overlay, child) { | |
183 | ret = of_overlay_apply_single_device_node(ov, target, child); | |
184 | if (ret != 0) { | |
606ad42a RH |
185 | pr_err("Failed to apply single node @%s/%s\n", |
186 | target->full_name, child->name); | |
001cf504 | 187 | of_node_put(child); |
7518b589 PA |
188 | return ret; |
189 | } | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | /** | |
196 | * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab | |
197 | * @ov: Overlay to apply | |
198 | * | |
199 | * Applies the overlays given, while handling all error conditions | |
200 | * appropriately. Either the operation succeeds, or if it fails the | |
201 | * live tree is reverted to the state before the attempt. | |
202 | * Returns 0, or an error if the overlay attempt failed. | |
203 | */ | |
204 | static int of_overlay_apply(struct of_overlay *ov) | |
205 | { | |
206 | int i, err; | |
207 | ||
208 | /* first we apply the overlays atomically */ | |
209 | for (i = 0; i < ov->count; i++) { | |
210 | struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i]; | |
211 | ||
212 | err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay); | |
213 | if (err != 0) { | |
606ad42a | 214 | pr_err("apply failed '%s'\n", ovinfo->target->full_name); |
7518b589 PA |
215 | return err; |
216 | } | |
217 | } | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | /* | |
223 | * Find the target node using a number of different strategies | |
224 | * in order of preference | |
225 | * | |
226 | * "target" property containing the phandle of the target | |
227 | * "target-path" property containing the path of the target | |
228 | */ | |
229 | static struct device_node *find_target_node(struct device_node *info_node) | |
230 | { | |
231 | const char *path; | |
232 | u32 val; | |
233 | int ret; | |
234 | ||
235 | /* first try to go by using the target as a phandle */ | |
236 | ret = of_property_read_u32(info_node, "target", &val); | |
237 | if (ret == 0) | |
238 | return of_find_node_by_phandle(val); | |
239 | ||
240 | /* now try to locate by path */ | |
241 | ret = of_property_read_string(info_node, "target-path", &path); | |
242 | if (ret == 0) | |
243 | return of_find_node_by_path(path); | |
244 | ||
606ad42a | 245 | pr_err("Failed to find target for node %p (%s)\n", |
7518b589 PA |
246 | info_node, info_node->name); |
247 | ||
248 | return NULL; | |
249 | } | |
250 | ||
251 | /** | |
252 | * of_fill_overlay_info() - Fill an overlay info structure | |
253 | * @ov Overlay to fill | |
254 | * @info_node: Device node containing the overlay | |
255 | * @ovinfo: Pointer to the overlay info structure to fill | |
256 | * | |
257 | * Fills an overlay info structure with the overlay information | |
258 | * from a device node. This device node must have a target property | |
259 | * which contains a phandle of the overlay target node, and an | |
260 | * __overlay__ child node which has the overlay contents. | |
261 | * Both ovinfo->target & ovinfo->overlay have their references taken. | |
262 | * | |
263 | * Returns 0 on success, or a negative error value. | |
264 | */ | |
265 | static int of_fill_overlay_info(struct of_overlay *ov, | |
266 | struct device_node *info_node, struct of_overlay_info *ovinfo) | |
267 | { | |
268 | ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__"); | |
269 | if (ovinfo->overlay == NULL) | |
270 | goto err_fail; | |
271 | ||
272 | ovinfo->target = find_target_node(info_node); | |
273 | if (ovinfo->target == NULL) | |
274 | goto err_fail; | |
275 | ||
276 | return 0; | |
277 | ||
278 | err_fail: | |
279 | of_node_put(ovinfo->target); | |
280 | of_node_put(ovinfo->overlay); | |
281 | ||
282 | memset(ovinfo, 0, sizeof(*ovinfo)); | |
283 | return -EINVAL; | |
284 | } | |
285 | ||
286 | /** | |
287 | * of_build_overlay_info() - Build an overlay info array | |
288 | * @ov Overlay to build | |
289 | * @tree: Device node containing all the overlays | |
290 | * | |
291 | * Helper function that given a tree containing overlay information, | |
292 | * allocates and builds an overlay info array containing it, ready | |
293 | * for use using of_overlay_apply. | |
294 | * | |
295 | * Returns 0 on success with the @cntp @ovinfop pointers valid, | |
296 | * while on error a negative error value is returned. | |
297 | */ | |
298 | static int of_build_overlay_info(struct of_overlay *ov, | |
299 | struct device_node *tree) | |
300 | { | |
301 | struct device_node *node; | |
302 | struct of_overlay_info *ovinfo; | |
303 | int cnt, err; | |
304 | ||
305 | /* worst case; every child is a node */ | |
306 | cnt = 0; | |
307 | for_each_child_of_node(tree, node) | |
308 | cnt++; | |
309 | ||
310 | ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL); | |
311 | if (ovinfo == NULL) | |
312 | return -ENOMEM; | |
313 | ||
314 | cnt = 0; | |
315 | for_each_child_of_node(tree, node) { | |
7518b589 PA |
316 | err = of_fill_overlay_info(ov, node, &ovinfo[cnt]); |
317 | if (err == 0) | |
318 | cnt++; | |
319 | } | |
320 | ||
321 | /* if nothing filled, return error */ | |
322 | if (cnt == 0) { | |
323 | kfree(ovinfo); | |
324 | return -ENODEV; | |
325 | } | |
326 | ||
327 | ov->count = cnt; | |
328 | ov->ovinfo_tab = ovinfo; | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | /** | |
334 | * of_free_overlay_info() - Free an overlay info array | |
335 | * @ov Overlay to free the overlay info from | |
336 | * @ovinfo_tab: Array of overlay_info's to free | |
337 | * | |
338 | * Releases the memory of a previously allocated ovinfo array | |
339 | * by of_build_overlay_info. | |
340 | * Returns 0, or an error if the arguments are bogus. | |
341 | */ | |
342 | static int of_free_overlay_info(struct of_overlay *ov) | |
343 | { | |
344 | struct of_overlay_info *ovinfo; | |
345 | int i; | |
346 | ||
347 | /* do it in reverse */ | |
348 | for (i = ov->count - 1; i >= 0; i--) { | |
349 | ovinfo = &ov->ovinfo_tab[i]; | |
350 | ||
351 | of_node_put(ovinfo->target); | |
352 | of_node_put(ovinfo->overlay); | |
353 | } | |
354 | kfree(ov->ovinfo_tab); | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
359 | static LIST_HEAD(ov_list); | |
360 | static DEFINE_IDR(ov_idr); | |
361 | ||
362 | /** | |
363 | * of_overlay_create() - Create and apply an overlay | |
364 | * @tree: Device node containing all the overlays | |
365 | * | |
366 | * Creates and applies an overlay while also keeping track | |
367 | * of the overlay in a list. This list can be used to prevent | |
368 | * illegal overlay removals. | |
369 | * | |
94a8bf97 | 370 | * Returns the id of the created overlay, or a negative error number |
7518b589 PA |
371 | */ |
372 | int of_overlay_create(struct device_node *tree) | |
373 | { | |
374 | struct of_overlay *ov; | |
375 | int err, id; | |
376 | ||
377 | /* allocate the overlay structure */ | |
378 | ov = kzalloc(sizeof(*ov), GFP_KERNEL); | |
379 | if (ov == NULL) | |
380 | return -ENOMEM; | |
381 | ov->id = -1; | |
382 | ||
383 | INIT_LIST_HEAD(&ov->node); | |
384 | ||
385 | of_changeset_init(&ov->cset); | |
386 | ||
387 | mutex_lock(&of_mutex); | |
388 | ||
389 | id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL); | |
390 | if (id < 0) { | |
7518b589 PA |
391 | err = id; |
392 | goto err_destroy_trans; | |
393 | } | |
394 | ov->id = id; | |
395 | ||
396 | /* build the overlay info structures */ | |
397 | err = of_build_overlay_info(ov, tree); | |
398 | if (err) { | |
606ad42a RH |
399 | pr_err("of_build_overlay_info() failed for tree@%s\n", |
400 | tree->full_name); | |
7518b589 PA |
401 | goto err_free_idr; |
402 | } | |
403 | ||
39a842e2 AT |
404 | err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY); |
405 | if (err < 0) { | |
406 | pr_err("%s: Pre-apply notifier failed (err=%d)\n", | |
407 | __func__, err); | |
408 | goto err_free_idr; | |
409 | } | |
410 | ||
7518b589 PA |
411 | /* apply the overlay */ |
412 | err = of_overlay_apply(ov); | |
606ad42a | 413 | if (err) |
7518b589 | 414 | goto err_abort_trans; |
7518b589 PA |
415 | |
416 | /* apply the changeset */ | |
18322377 | 417 | err = __of_changeset_apply(&ov->cset); |
606ad42a | 418 | if (err) |
7518b589 | 419 | goto err_revert_overlay; |
606ad42a | 420 | |
7518b589 PA |
421 | |
422 | /* add to the tail of the overlay list */ | |
423 | list_add_tail(&ov->node, &ov_list); | |
424 | ||
39a842e2 AT |
425 | of_overlay_notify(ov, OF_OVERLAY_POST_APPLY); |
426 | ||
7518b589 PA |
427 | mutex_unlock(&of_mutex); |
428 | ||
429 | return id; | |
430 | ||
431 | err_revert_overlay: | |
432 | err_abort_trans: | |
433 | of_free_overlay_info(ov); | |
434 | err_free_idr: | |
435 | idr_remove(&ov_idr, ov->id); | |
436 | err_destroy_trans: | |
437 | of_changeset_destroy(&ov->cset); | |
438 | kfree(ov); | |
439 | mutex_unlock(&of_mutex); | |
440 | ||
441 | return err; | |
442 | } | |
443 | EXPORT_SYMBOL_GPL(of_overlay_create); | |
444 | ||
445 | /* check whether the given node, lies under the given tree */ | |
446 | static int overlay_subtree_check(struct device_node *tree, | |
447 | struct device_node *dn) | |
448 | { | |
449 | struct device_node *child; | |
450 | ||
451 | /* match? */ | |
452 | if (tree == dn) | |
453 | return 1; | |
454 | ||
455 | for_each_child_of_node(tree, child) { | |
001cf504 JL |
456 | if (overlay_subtree_check(child, dn)) { |
457 | of_node_put(child); | |
7518b589 | 458 | return 1; |
001cf504 | 459 | } |
7518b589 PA |
460 | } |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | /* check whether this overlay is the topmost */ | |
466 | static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn) | |
467 | { | |
468 | struct of_overlay *ovt; | |
469 | struct of_changeset_entry *ce; | |
470 | ||
471 | list_for_each_entry_reverse(ovt, &ov_list, node) { | |
472 | /* if we hit ourselves, we're done */ | |
473 | if (ovt == ov) | |
474 | break; | |
475 | ||
476 | /* check against each subtree affected by this overlay */ | |
477 | list_for_each_entry(ce, &ovt->cset.entries, node) { | |
478 | if (overlay_subtree_check(ce->np, dn)) { | |
479 | pr_err("%s: #%d clashes #%d @%s\n", | |
480 | __func__, ov->id, ovt->id, | |
481 | dn->full_name); | |
482 | return 0; | |
483 | } | |
484 | } | |
485 | } | |
486 | ||
487 | /* overlay is topmost */ | |
488 | return 1; | |
489 | } | |
490 | ||
491 | /* | |
492 | * We can safely remove the overlay only if it's the top-most one. | |
493 | * Newly applied overlays are inserted at the tail of the overlay list, | |
494 | * so a top most overlay is the one that is closest to the tail. | |
495 | * | |
496 | * The topmost check is done by exploiting this property. For each | |
497 | * affected device node in the log list we check if this overlay is | |
498 | * the one closest to the tail. If another overlay has affected this | |
499 | * device node and is closest to the tail, then removal is not permited. | |
500 | */ | |
501 | static int overlay_removal_is_ok(struct of_overlay *ov) | |
502 | { | |
503 | struct of_changeset_entry *ce; | |
504 | ||
505 | list_for_each_entry(ce, &ov->cset.entries, node) { | |
506 | if (!overlay_is_topmost(ov, ce->np)) { | |
606ad42a | 507 | pr_err("overlay #%d is not topmost\n", ov->id); |
7518b589 PA |
508 | return 0; |
509 | } | |
510 | } | |
511 | ||
512 | return 1; | |
513 | } | |
514 | ||
515 | /** | |
516 | * of_overlay_destroy() - Removes an overlay | |
517 | * @id: Overlay id number returned by a previous call to of_overlay_create | |
518 | * | |
519 | * Removes an overlay if it is permissible. | |
520 | * | |
94a8bf97 | 521 | * Returns 0 on success, or a negative error number |
7518b589 PA |
522 | */ |
523 | int of_overlay_destroy(int id) | |
524 | { | |
525 | struct of_overlay *ov; | |
526 | int err; | |
527 | ||
528 | mutex_lock(&of_mutex); | |
529 | ||
530 | ov = idr_find(&ov_idr, id); | |
531 | if (ov == NULL) { | |
532 | err = -ENODEV; | |
606ad42a | 533 | pr_err("destroy: Could not find overlay #%d\n", id); |
7518b589 PA |
534 | goto out; |
535 | } | |
536 | ||
537 | /* check whether the overlay is safe to remove */ | |
538 | if (!overlay_removal_is_ok(ov)) { | |
539 | err = -EBUSY; | |
7518b589 PA |
540 | goto out; |
541 | } | |
542 | ||
39a842e2 | 543 | of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE); |
7518b589 | 544 | list_del(&ov->node); |
18322377 | 545 | __of_changeset_revert(&ov->cset); |
39a842e2 | 546 | of_overlay_notify(ov, OF_OVERLAY_POST_REMOVE); |
7518b589 PA |
547 | of_free_overlay_info(ov); |
548 | idr_remove(&ov_idr, id); | |
549 | of_changeset_destroy(&ov->cset); | |
550 | kfree(ov); | |
551 | ||
552 | err = 0; | |
553 | ||
554 | out: | |
555 | mutex_unlock(&of_mutex); | |
556 | ||
557 | return err; | |
558 | } | |
559 | EXPORT_SYMBOL_GPL(of_overlay_destroy); | |
560 | ||
561 | /** | |
562 | * of_overlay_destroy_all() - Removes all overlays from the system | |
563 | * | |
564 | * Removes all overlays from the system in the correct order. | |
565 | * | |
94a8bf97 | 566 | * Returns 0 on success, or a negative error number |
7518b589 PA |
567 | */ |
568 | int of_overlay_destroy_all(void) | |
569 | { | |
570 | struct of_overlay *ov, *ovn; | |
571 | ||
572 | mutex_lock(&of_mutex); | |
573 | ||
574 | /* the tail of list is guaranteed to be safe to remove */ | |
575 | list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) { | |
576 | list_del(&ov->node); | |
18322377 | 577 | __of_changeset_revert(&ov->cset); |
7518b589 PA |
578 | of_free_overlay_info(ov); |
579 | idr_remove(&ov_idr, ov->id); | |
580 | kfree(ov); | |
581 | } | |
582 | ||
583 | mutex_unlock(&of_mutex); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | EXPORT_SYMBOL_GPL(of_overlay_destroy_all); |