blob: 0ddffebd7838a4a4b101bb21b5ace334013429d1 [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/**
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 */
35struct 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 */
49struct 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
57static int of_overlay_apply_one(struct of_overlay *ov,
58 struct device_node *target, const struct device_node *overlay);
59
Alan Tull39a842e2016-11-01 14:14:22 -050060static BLOCKING_NOTIFIER_HEAD(of_overlay_chain);
61
62int of_overlay_notifier_register(struct notifier_block *nb)
63{
64 return blocking_notifier_chain_register(&of_overlay_chain, nb);
65}
66EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
67
68int of_overlay_notifier_unregister(struct notifier_block *nb)
69{
70 return blocking_notifier_chain_unregister(&of_overlay_chain, nb);
71}
72EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
73
74static 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
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020095static 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
121static 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;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300125 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200126 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) {
Frank Rowand7a12f452017-06-21 12:21:56 -0700135 /* new overlay phandle value conflicts with existing value */
136 if (child->phandle)
137 return -EINVAL;
138
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200139 /* apply overlay recursively */
140 ret = of_overlay_apply_one(ov, tchild, child);
141 of_node_put(tchild);
142 } else {
143 /* create empty tree as a target */
Rob Herring0d638a02017-06-01 15:50:55 -0500144 tchild = __of_node_dup(child, "%pOF/%s", target, cname);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200145 if (!tchild)
146 return -ENOMEM;
147
148 /* point to parent */
149 tchild->parent = target;
150
151 ret = of_changeset_attach_node(&ov->cset, tchild);
152 if (ret)
153 return ret;
154
155 ret = of_overlay_apply_one(ov, tchild, child);
156 if (ret)
157 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200158 }
159
160 return ret;
161}
162
163/*
164 * Apply a single overlay node recursively.
165 *
166 * Note that the in case of an error the target node is left
167 * in a inconsistent state. Error recovery should be performed
168 * by using the changeset.
169 */
170static int of_overlay_apply_one(struct of_overlay *ov,
171 struct device_node *target, const struct device_node *overlay)
172{
173 struct device_node *child;
174 struct property *prop;
175 int ret;
176
177 for_each_property_of_node(overlay, prop) {
178 ret = of_overlay_apply_single_property(ov, target, prop);
179 if (ret) {
Rob Herring0d638a02017-06-01 15:50:55 -0500180 pr_err("Failed to apply prop @%pOF/%s\n",
181 target, prop->name);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200182 return ret;
183 }
184 }
185
186 for_each_child_of_node(overlay, child) {
187 ret = of_overlay_apply_single_device_node(ov, target, child);
188 if (ret != 0) {
Rob Herring0d638a02017-06-01 15:50:55 -0500189 pr_err("Failed to apply single node @%pOF/%s\n",
190 target, child->name);
Julia Lawall001cf502015-10-22 11:02:48 +0200191 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200192 return ret;
193 }
194 }
195
196 return 0;
197}
198
199/**
200 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
201 * @ov: Overlay to apply
202 *
203 * Applies the overlays given, while handling all error conditions
204 * appropriately. Either the operation succeeds, or if it fails the
205 * live tree is reverted to the state before the attempt.
206 * Returns 0, or an error if the overlay attempt failed.
207 */
208static int of_overlay_apply(struct of_overlay *ov)
209{
210 int i, err;
211
212 /* first we apply the overlays atomically */
213 for (i = 0; i < ov->count; i++) {
214 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
215
216 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
217 if (err != 0) {
Rob Herring0d638a02017-06-01 15:50:55 -0500218 pr_err("apply failed '%pOF'\n", ovinfo->target);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200219 return err;
220 }
221 }
222
223 return 0;
224}
225
226/*
227 * Find the target node using a number of different strategies
228 * in order of preference
229 *
230 * "target" property containing the phandle of the target
231 * "target-path" property containing the path of the target
232 */
233static struct device_node *find_target_node(struct device_node *info_node)
234{
235 const char *path;
236 u32 val;
237 int ret;
238
239 /* first try to go by using the target as a phandle */
240 ret = of_property_read_u32(info_node, "target", &val);
241 if (ret == 0)
242 return of_find_node_by_phandle(val);
243
244 /* now try to locate by path */
245 ret = of_property_read_string(info_node, "target-path", &path);
246 if (ret == 0)
247 return of_find_node_by_path(path);
248
Rob Herring606ad422016-06-15 08:32:18 -0500249 pr_err("Failed to find target for node %p (%s)\n",
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200250 info_node, info_node->name);
251
252 return NULL;
253}
254
255/**
256 * of_fill_overlay_info() - Fill an overlay info structure
257 * @ov Overlay to fill
258 * @info_node: Device node containing the overlay
259 * @ovinfo: Pointer to the overlay info structure to fill
260 *
261 * Fills an overlay info structure with the overlay information
262 * from a device node. This device node must have a target property
263 * which contains a phandle of the overlay target node, and an
264 * __overlay__ child node which has the overlay contents.
265 * Both ovinfo->target & ovinfo->overlay have their references taken.
266 *
267 * Returns 0 on success, or a negative error value.
268 */
269static int of_fill_overlay_info(struct of_overlay *ov,
270 struct device_node *info_node, struct of_overlay_info *ovinfo)
271{
272 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
273 if (ovinfo->overlay == NULL)
274 goto err_fail;
275
276 ovinfo->target = find_target_node(info_node);
277 if (ovinfo->target == NULL)
278 goto err_fail;
279
280 return 0;
281
282err_fail:
283 of_node_put(ovinfo->target);
284 of_node_put(ovinfo->overlay);
285
286 memset(ovinfo, 0, sizeof(*ovinfo));
287 return -EINVAL;
288}
289
290/**
291 * of_build_overlay_info() - Build an overlay info array
292 * @ov Overlay to build
293 * @tree: Device node containing all the overlays
294 *
295 * Helper function that given a tree containing overlay information,
296 * allocates and builds an overlay info array containing it, ready
297 * for use using of_overlay_apply.
298 *
299 * Returns 0 on success with the @cntp @ovinfop pointers valid,
300 * while on error a negative error value is returned.
301 */
302static int of_build_overlay_info(struct of_overlay *ov,
303 struct device_node *tree)
304{
305 struct device_node *node;
306 struct of_overlay_info *ovinfo;
307 int cnt, err;
308
309 /* worst case; every child is a node */
310 cnt = 0;
311 for_each_child_of_node(tree, node)
312 cnt++;
313
314 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
315 if (ovinfo == NULL)
316 return -ENOMEM;
317
318 cnt = 0;
319 for_each_child_of_node(tree, node) {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200320 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
321 if (err == 0)
322 cnt++;
323 }
324
325 /* if nothing filled, return error */
326 if (cnt == 0) {
327 kfree(ovinfo);
328 return -ENODEV;
329 }
330
331 ov->count = cnt;
332 ov->ovinfo_tab = ovinfo;
333
334 return 0;
335}
336
337/**
338 * of_free_overlay_info() - Free an overlay info array
339 * @ov Overlay to free the overlay info from
340 * @ovinfo_tab: Array of overlay_info's to free
341 *
342 * Releases the memory of a previously allocated ovinfo array
343 * by of_build_overlay_info.
344 * Returns 0, or an error if the arguments are bogus.
345 */
346static int of_free_overlay_info(struct of_overlay *ov)
347{
348 struct of_overlay_info *ovinfo;
349 int i;
350
351 /* do it in reverse */
352 for (i = ov->count - 1; i >= 0; i--) {
353 ovinfo = &ov->ovinfo_tab[i];
354
355 of_node_put(ovinfo->target);
356 of_node_put(ovinfo->overlay);
357 }
358 kfree(ov->ovinfo_tab);
359
360 return 0;
361}
362
363static LIST_HEAD(ov_list);
364static DEFINE_IDR(ov_idr);
365
366/**
367 * of_overlay_create() - Create and apply an overlay
368 * @tree: Device node containing all the overlays
369 *
370 * Creates and applies an overlay while also keeping track
371 * of the overlay in a list. This list can be used to prevent
372 * illegal overlay removals.
373 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200374 * Returns the id of the created overlay, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200375 */
376int of_overlay_create(struct device_node *tree)
377{
378 struct of_overlay *ov;
379 int err, id;
380
381 /* allocate the overlay structure */
382 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
383 if (ov == NULL)
384 return -ENOMEM;
385 ov->id = -1;
386
387 INIT_LIST_HEAD(&ov->node);
388
389 of_changeset_init(&ov->cset);
390
391 mutex_lock(&of_mutex);
392
393 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
394 if (id < 0) {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200395 err = id;
396 goto err_destroy_trans;
397 }
398 ov->id = id;
399
400 /* build the overlay info structures */
401 err = of_build_overlay_info(ov, tree);
402 if (err) {
Rob Herring0d638a02017-06-01 15:50:55 -0500403 pr_err("of_build_overlay_info() failed for tree@%pOF\n",
404 tree);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200405 goto err_free_idr;
406 }
407
Alan Tull39a842e2016-11-01 14:14:22 -0500408 err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY);
409 if (err < 0) {
410 pr_err("%s: Pre-apply notifier failed (err=%d)\n",
411 __func__, err);
412 goto err_free_idr;
413 }
414
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200415 /* apply the overlay */
416 err = of_overlay_apply(ov);
Rob Herring606ad422016-06-15 08:32:18 -0500417 if (err)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200418 goto err_abort_trans;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200419
420 /* apply the changeset */
Gavin Shan18322372015-11-05 00:12:49 +1100421 err = __of_changeset_apply(&ov->cset);
Rob Herring606ad422016-06-15 08:32:18 -0500422 if (err)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200423 goto err_revert_overlay;
Rob Herring606ad422016-06-15 08:32:18 -0500424
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200425
426 /* add to the tail of the overlay list */
427 list_add_tail(&ov->node, &ov_list);
428
Alan Tull39a842e2016-11-01 14:14:22 -0500429 of_overlay_notify(ov, OF_OVERLAY_POST_APPLY);
430
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200431 mutex_unlock(&of_mutex);
432
433 return id;
434
435err_revert_overlay:
436err_abort_trans:
437 of_free_overlay_info(ov);
438err_free_idr:
439 idr_remove(&ov_idr, ov->id);
440err_destroy_trans:
441 of_changeset_destroy(&ov->cset);
442 kfree(ov);
443 mutex_unlock(&of_mutex);
444
445 return err;
446}
447EXPORT_SYMBOL_GPL(of_overlay_create);
448
449/* check whether the given node, lies under the given tree */
450static int overlay_subtree_check(struct device_node *tree,
451 struct device_node *dn)
452{
453 struct device_node *child;
454
455 /* match? */
456 if (tree == dn)
457 return 1;
458
459 for_each_child_of_node(tree, child) {
Julia Lawall001cf502015-10-22 11:02:48 +0200460 if (overlay_subtree_check(child, dn)) {
461 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200462 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200463 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200464 }
465
466 return 0;
467}
468
469/* check whether this overlay is the topmost */
470static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
471{
472 struct of_overlay *ovt;
473 struct of_changeset_entry *ce;
474
475 list_for_each_entry_reverse(ovt, &ov_list, node) {
476 /* if we hit ourselves, we're done */
477 if (ovt == ov)
478 break;
479
480 /* check against each subtree affected by this overlay */
481 list_for_each_entry(ce, &ovt->cset.entries, node) {
482 if (overlay_subtree_check(ce->np, dn)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500483 pr_err("%s: #%d clashes #%d @%pOF\n",
484 __func__, ov->id, ovt->id, dn);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200485 return 0;
486 }
487 }
488 }
489
490 /* overlay is topmost */
491 return 1;
492}
493
494/*
495 * We can safely remove the overlay only if it's the top-most one.
496 * Newly applied overlays are inserted at the tail of the overlay list,
497 * so a top most overlay is the one that is closest to the tail.
498 *
499 * The topmost check is done by exploiting this property. For each
500 * affected device node in the log list we check if this overlay is
501 * the one closest to the tail. If another overlay has affected this
502 * device node and is closest to the tail, then removal is not permited.
503 */
504static int overlay_removal_is_ok(struct of_overlay *ov)
505{
506 struct of_changeset_entry *ce;
507
508 list_for_each_entry(ce, &ov->cset.entries, node) {
509 if (!overlay_is_topmost(ov, ce->np)) {
Rob Herring606ad422016-06-15 08:32:18 -0500510 pr_err("overlay #%d is not topmost\n", ov->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200511 return 0;
512 }
513 }
514
515 return 1;
516}
517
518/**
519 * of_overlay_destroy() - Removes an overlay
520 * @id: Overlay id number returned by a previous call to of_overlay_create
521 *
522 * Removes an overlay if it is permissible.
523 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200524 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200525 */
526int of_overlay_destroy(int id)
527{
528 struct of_overlay *ov;
529 int err;
530
531 mutex_lock(&of_mutex);
532
533 ov = idr_find(&ov_idr, id);
534 if (ov == NULL) {
535 err = -ENODEV;
Rob Herring606ad422016-06-15 08:32:18 -0500536 pr_err("destroy: Could not find overlay #%d\n", id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200537 goto out;
538 }
539
540 /* check whether the overlay is safe to remove */
541 if (!overlay_removal_is_ok(ov)) {
542 err = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200543 goto out;
544 }
545
Alan Tull39a842e2016-11-01 14:14:22 -0500546 of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200547 list_del(&ov->node);
Gavin Shan18322372015-11-05 00:12:49 +1100548 __of_changeset_revert(&ov->cset);
Alan Tull39a842e2016-11-01 14:14:22 -0500549 of_overlay_notify(ov, OF_OVERLAY_POST_REMOVE);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200550 of_free_overlay_info(ov);
551 idr_remove(&ov_idr, id);
552 of_changeset_destroy(&ov->cset);
553 kfree(ov);
554
555 err = 0;
556
557out:
558 mutex_unlock(&of_mutex);
559
560 return err;
561}
562EXPORT_SYMBOL_GPL(of_overlay_destroy);
563
564/**
565 * of_overlay_destroy_all() - Removes all overlays from the system
566 *
567 * Removes all overlays from the system in the correct order.
568 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200569 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200570 */
571int of_overlay_destroy_all(void)
572{
573 struct of_overlay *ov, *ovn;
574
575 mutex_lock(&of_mutex);
576
577 /* the tail of list is guaranteed to be safe to remove */
578 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
579 list_del(&ov->node);
Gavin Shan18322372015-11-05 00:12:49 +1100580 __of_changeset_revert(&ov->cset);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200581 of_free_overlay_info(ov);
582 idr_remove(&ov_idr, ov->id);
583 kfree(ov);
584 }
585
586 mutex_unlock(&of_mutex);
587
588 return 0;
589}
590EXPORT_SYMBOL_GPL(of_overlay_destroy_all);