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