blob: fbe1980accb6f4427e377c59e1c101136089e8d4 [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 */
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700133 for_each_child_of_node(target, tchild)
134 if (!of_node_cmp(cname, kbasename(tchild->full_name)))
135 break;
136
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200137 if (tchild != NULL) {
Frank Rowand7a12f452017-06-21 12:21:56 -0700138 /* new overlay phandle value conflicts with existing value */
139 if (child->phandle)
140 return -EINVAL;
141
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200142 /* apply overlay recursively */
143 ret = of_overlay_apply_one(ov, tchild, child);
144 of_node_put(tchild);
145 } else {
146 /* create empty tree as a target */
Rob Herring0d638a02017-06-01 15:50:55 -0500147 tchild = __of_node_dup(child, "%pOF/%s", target, cname);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200148 if (!tchild)
149 return -ENOMEM;
150
151 /* point to parent */
152 tchild->parent = target;
153
154 ret = of_changeset_attach_node(&ov->cset, tchild);
155 if (ret)
156 return ret;
157
158 ret = of_overlay_apply_one(ov, tchild, child);
159 if (ret)
160 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200161 }
162
163 return ret;
164}
165
166/*
167 * Apply a single overlay node recursively.
168 *
169 * Note that the in case of an error the target node is left
170 * in a inconsistent state. Error recovery should be performed
171 * by using the changeset.
172 */
173static int of_overlay_apply_one(struct of_overlay *ov,
174 struct device_node *target, const struct device_node *overlay)
175{
176 struct device_node *child;
177 struct property *prop;
178 int ret;
179
180 for_each_property_of_node(overlay, prop) {
181 ret = of_overlay_apply_single_property(ov, target, prop);
182 if (ret) {
Rob Herring0d638a02017-06-01 15:50:55 -0500183 pr_err("Failed to apply prop @%pOF/%s\n",
184 target, prop->name);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200185 return ret;
186 }
187 }
188
189 for_each_child_of_node(overlay, child) {
190 ret = of_overlay_apply_single_device_node(ov, target, child);
191 if (ret != 0) {
Rob Herring0d638a02017-06-01 15:50:55 -0500192 pr_err("Failed to apply single node @%pOF/%s\n",
193 target, child->name);
Julia Lawall001cf502015-10-22 11:02:48 +0200194 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200195 return ret;
196 }
197 }
198
199 return 0;
200}
201
202/**
203 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
204 * @ov: Overlay to apply
205 *
206 * Applies the overlays given, while handling all error conditions
207 * appropriately. Either the operation succeeds, or if it fails the
208 * live tree is reverted to the state before the attempt.
209 * Returns 0, or an error if the overlay attempt failed.
210 */
211static int of_overlay_apply(struct of_overlay *ov)
212{
213 int i, err;
214
215 /* first we apply the overlays atomically */
216 for (i = 0; i < ov->count; i++) {
217 struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
218
219 err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
220 if (err != 0) {
Rob Herring0d638a02017-06-01 15:50:55 -0500221 pr_err("apply failed '%pOF'\n", ovinfo->target);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200222 return err;
223 }
224 }
225
226 return 0;
227}
228
229/*
230 * Find the target node using a number of different strategies
231 * in order of preference
232 *
233 * "target" property containing the phandle of the target
234 * "target-path" property containing the path of the target
235 */
236static struct device_node *find_target_node(struct device_node *info_node)
237{
238 const char *path;
239 u32 val;
240 int ret;
241
242 /* first try to go by using the target as a phandle */
243 ret = of_property_read_u32(info_node, "target", &val);
244 if (ret == 0)
245 return of_find_node_by_phandle(val);
246
247 /* now try to locate by path */
248 ret = of_property_read_string(info_node, "target-path", &path);
249 if (ret == 0)
250 return of_find_node_by_path(path);
251
Rob Herring606ad422016-06-15 08:32:18 -0500252 pr_err("Failed to find target for node %p (%s)\n",
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200253 info_node, info_node->name);
254
255 return NULL;
256}
257
258/**
259 * of_fill_overlay_info() - Fill an overlay info structure
260 * @ov Overlay to fill
261 * @info_node: Device node containing the overlay
262 * @ovinfo: Pointer to the overlay info structure to fill
263 *
264 * Fills an overlay info structure with the overlay information
265 * from a device node. This device node must have a target property
266 * which contains a phandle of the overlay target node, and an
267 * __overlay__ child node which has the overlay contents.
268 * Both ovinfo->target & ovinfo->overlay have their references taken.
269 *
270 * Returns 0 on success, or a negative error value.
271 */
272static int of_fill_overlay_info(struct of_overlay *ov,
273 struct device_node *info_node, struct of_overlay_info *ovinfo)
274{
275 ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
276 if (ovinfo->overlay == NULL)
277 goto err_fail;
278
279 ovinfo->target = find_target_node(info_node);
280 if (ovinfo->target == NULL)
281 goto err_fail;
282
283 return 0;
284
285err_fail:
286 of_node_put(ovinfo->target);
287 of_node_put(ovinfo->overlay);
288
289 memset(ovinfo, 0, sizeof(*ovinfo));
290 return -EINVAL;
291}
292
293/**
294 * of_build_overlay_info() - Build an overlay info array
295 * @ov Overlay to build
296 * @tree: Device node containing all the overlays
297 *
298 * Helper function that given a tree containing overlay information,
299 * allocates and builds an overlay info array containing it, ready
300 * for use using of_overlay_apply.
301 *
302 * Returns 0 on success with the @cntp @ovinfop pointers valid,
303 * while on error a negative error value is returned.
304 */
305static int of_build_overlay_info(struct of_overlay *ov,
306 struct device_node *tree)
307{
308 struct device_node *node;
309 struct of_overlay_info *ovinfo;
310 int cnt, err;
311
312 /* worst case; every child is a node */
313 cnt = 0;
314 for_each_child_of_node(tree, node)
315 cnt++;
316
317 ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
318 if (ovinfo == NULL)
319 return -ENOMEM;
320
321 cnt = 0;
322 for_each_child_of_node(tree, node) {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200323 err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
324 if (err == 0)
325 cnt++;
326 }
327
328 /* if nothing filled, return error */
329 if (cnt == 0) {
330 kfree(ovinfo);
331 return -ENODEV;
332 }
333
334 ov->count = cnt;
335 ov->ovinfo_tab = ovinfo;
336
337 return 0;
338}
339
340/**
341 * of_free_overlay_info() - Free an overlay info array
342 * @ov Overlay to free the overlay info from
343 * @ovinfo_tab: Array of overlay_info's to free
344 *
345 * Releases the memory of a previously allocated ovinfo array
346 * by of_build_overlay_info.
347 * Returns 0, or an error if the arguments are bogus.
348 */
349static int of_free_overlay_info(struct of_overlay *ov)
350{
351 struct of_overlay_info *ovinfo;
352 int i;
353
354 /* do it in reverse */
355 for (i = ov->count - 1; i >= 0; i--) {
356 ovinfo = &ov->ovinfo_tab[i];
357
358 of_node_put(ovinfo->target);
359 of_node_put(ovinfo->overlay);
360 }
361 kfree(ov->ovinfo_tab);
362
363 return 0;
364}
365
366static LIST_HEAD(ov_list);
367static DEFINE_IDR(ov_idr);
368
369/**
370 * of_overlay_create() - Create and apply an overlay
371 * @tree: Device node containing all the overlays
372 *
373 * Creates and applies an overlay while also keeping track
374 * of the overlay in a list. This list can be used to prevent
375 * illegal overlay removals.
376 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200377 * Returns the id of the created overlay, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200378 */
379int of_overlay_create(struct device_node *tree)
380{
381 struct of_overlay *ov;
382 int err, id;
383
384 /* allocate the overlay structure */
385 ov = kzalloc(sizeof(*ov), GFP_KERNEL);
386 if (ov == NULL)
387 return -ENOMEM;
388 ov->id = -1;
389
390 INIT_LIST_HEAD(&ov->node);
391
392 of_changeset_init(&ov->cset);
393
394 mutex_lock(&of_mutex);
395
396 id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
397 if (id < 0) {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200398 err = id;
399 goto err_destroy_trans;
400 }
401 ov->id = id;
402
403 /* build the overlay info structures */
404 err = of_build_overlay_info(ov, tree);
405 if (err) {
Rob Herring0d638a02017-06-01 15:50:55 -0500406 pr_err("of_build_overlay_info() failed for tree@%pOF\n",
407 tree);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200408 goto err_free_idr;
409 }
410
Alan Tull39a842e2016-11-01 14:14:22 -0500411 err = of_overlay_notify(ov, OF_OVERLAY_PRE_APPLY);
412 if (err < 0) {
413 pr_err("%s: Pre-apply notifier failed (err=%d)\n",
414 __func__, err);
415 goto err_free_idr;
416 }
417
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200418 /* apply the overlay */
419 err = of_overlay_apply(ov);
Rob Herring606ad422016-06-15 08:32:18 -0500420 if (err)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200421 goto err_abort_trans;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200422
423 /* apply the changeset */
Gavin Shan18322372015-11-05 00:12:49 +1100424 err = __of_changeset_apply(&ov->cset);
Rob Herring606ad422016-06-15 08:32:18 -0500425 if (err)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200426 goto err_revert_overlay;
Rob Herring606ad422016-06-15 08:32:18 -0500427
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200428
429 /* add to the tail of the overlay list */
430 list_add_tail(&ov->node, &ov_list);
431
Alan Tull39a842e2016-11-01 14:14:22 -0500432 of_overlay_notify(ov, OF_OVERLAY_POST_APPLY);
433
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200434 mutex_unlock(&of_mutex);
435
436 return id;
437
438err_revert_overlay:
439err_abort_trans:
440 of_free_overlay_info(ov);
441err_free_idr:
442 idr_remove(&ov_idr, ov->id);
443err_destroy_trans:
444 of_changeset_destroy(&ov->cset);
445 kfree(ov);
446 mutex_unlock(&of_mutex);
447
448 return err;
449}
450EXPORT_SYMBOL_GPL(of_overlay_create);
451
452/* check whether the given node, lies under the given tree */
453static int overlay_subtree_check(struct device_node *tree,
454 struct device_node *dn)
455{
456 struct device_node *child;
457
458 /* match? */
459 if (tree == dn)
460 return 1;
461
462 for_each_child_of_node(tree, child) {
Julia Lawall001cf502015-10-22 11:02:48 +0200463 if (overlay_subtree_check(child, dn)) {
464 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200465 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200466 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200467 }
468
469 return 0;
470}
471
472/* check whether this overlay is the topmost */
473static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
474{
475 struct of_overlay *ovt;
476 struct of_changeset_entry *ce;
477
478 list_for_each_entry_reverse(ovt, &ov_list, node) {
479 /* if we hit ourselves, we're done */
480 if (ovt == ov)
481 break;
482
483 /* check against each subtree affected by this overlay */
484 list_for_each_entry(ce, &ovt->cset.entries, node) {
485 if (overlay_subtree_check(ce->np, dn)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500486 pr_err("%s: #%d clashes #%d @%pOF\n",
487 __func__, ov->id, ovt->id, dn);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200488 return 0;
489 }
490 }
491 }
492
493 /* overlay is topmost */
494 return 1;
495}
496
497/*
498 * We can safely remove the overlay only if it's the top-most one.
499 * Newly applied overlays are inserted at the tail of the overlay list,
500 * so a top most overlay is the one that is closest to the tail.
501 *
502 * The topmost check is done by exploiting this property. For each
503 * affected device node in the log list we check if this overlay is
504 * the one closest to the tail. If another overlay has affected this
505 * device node and is closest to the tail, then removal is not permited.
506 */
507static int overlay_removal_is_ok(struct of_overlay *ov)
508{
509 struct of_changeset_entry *ce;
510
511 list_for_each_entry(ce, &ov->cset.entries, node) {
512 if (!overlay_is_topmost(ov, ce->np)) {
Rob Herring606ad422016-06-15 08:32:18 -0500513 pr_err("overlay #%d is not topmost\n", ov->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200514 return 0;
515 }
516 }
517
518 return 1;
519}
520
521/**
522 * of_overlay_destroy() - Removes an overlay
523 * @id: Overlay id number returned by a previous call to of_overlay_create
524 *
525 * Removes an overlay if it is permissible.
526 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200527 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200528 */
529int of_overlay_destroy(int id)
530{
531 struct of_overlay *ov;
532 int err;
533
534 mutex_lock(&of_mutex);
535
536 ov = idr_find(&ov_idr, id);
537 if (ov == NULL) {
538 err = -ENODEV;
Rob Herring606ad422016-06-15 08:32:18 -0500539 pr_err("destroy: Could not find overlay #%d\n", id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200540 goto out;
541 }
542
543 /* check whether the overlay is safe to remove */
544 if (!overlay_removal_is_ok(ov)) {
545 err = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200546 goto out;
547 }
548
Alan Tull39a842e2016-11-01 14:14:22 -0500549 of_overlay_notify(ov, OF_OVERLAY_PRE_REMOVE);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200550 list_del(&ov->node);
Gavin Shan18322372015-11-05 00:12:49 +1100551 __of_changeset_revert(&ov->cset);
Alan Tull39a842e2016-11-01 14:14:22 -0500552 of_overlay_notify(ov, OF_OVERLAY_POST_REMOVE);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200553 of_free_overlay_info(ov);
554 idr_remove(&ov_idr, id);
555 of_changeset_destroy(&ov->cset);
556 kfree(ov);
557
558 err = 0;
559
560out:
561 mutex_unlock(&of_mutex);
562
563 return err;
564}
565EXPORT_SYMBOL_GPL(of_overlay_destroy);
566
567/**
568 * of_overlay_destroy_all() - Removes all overlays from the system
569 *
570 * Removes all overlays from the system in the correct order.
571 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200572 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200573 */
574int of_overlay_destroy_all(void)
575{
576 struct of_overlay *ov, *ovn;
577
578 mutex_lock(&of_mutex);
579
580 /* the tail of list is guaranteed to be safe to remove */
581 list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
582 list_del(&ov->node);
Gavin Shan18322372015-11-05 00:12:49 +1100583 __of_changeset_revert(&ov->cset);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200584 of_free_overlay_info(ov);
585 idr_remove(&ov_idr, ov->id);
586 kfree(ov);
587 }
588
589 mutex_unlock(&of_mutex);
590
591 return 0;
592}
593EXPORT_SYMBOL_GPL(of_overlay_destroy_all);