blob: 740d19bde601203eccb9e099d6411cf8380cfe3a [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0
Pantelis Antoniou7941b272014-07-04 19:59:20 +03002/*
3 * Functions for dealing with DT resolution
4 *
5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
6 * Copyright (C) 2012 Texas Instruments Inc.
Pantelis Antoniou7941b272014-07-04 19:59:20 +03007 */
8
Rob Herring606ad422016-06-15 08:32:18 -05009#define pr_fmt(fmt) "OF: resolver: " fmt
10
Pantelis Antoniou7941b272014-07-04 19:59:20 +030011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_device.h>
15#include <linux/string.h>
16#include <linux/ctype.h>
17#include <linux/errno.h>
Pantelis Antoniou7941b272014-07-04 19:59:20 +030018#include <linux/slab.h>
19
Rob Herring27497e12017-06-02 12:43:18 -050020#include "of_private.h"
21
Pantelis Antoniou7941b272014-07-04 19:59:20 +030022/* illegal phandle value (set when unresolved) */
23#define OF_PHANDLE_ILLEGAL 0xdeadbeef
24
Frank Rowandf94823f2016-10-28 23:26:24 -070025static phandle live_tree_max_phandle(void)
Pantelis Antoniou7941b272014-07-04 19:59:20 +030026{
27 struct device_node *node;
28 phandle phandle;
29 unsigned long flags;
30
Pantelis Antoniou7941b272014-07-04 19:59:20 +030031 raw_spin_lock_irqsave(&devtree_lock, flags);
32 phandle = 0;
33 for_each_of_allnodes(node) {
34 if (node->phandle != OF_PHANDLE_ILLEGAL &&
35 node->phandle > phandle)
36 phandle = node->phandle;
37 }
38 raw_spin_unlock_irqrestore(&devtree_lock, flags);
39
40 return phandle;
41}
42
Frank Rowand25e16872016-10-28 23:26:26 -070043static void adjust_overlay_phandles(struct device_node *overlay,
Pantelis Antoniou7941b272014-07-04 19:59:20 +030044 int phandle_delta)
45{
46 struct device_node *child;
47 struct property *prop;
48 phandle phandle;
49
Frank Rowand269f1a62016-10-28 23:26:29 -070050 /* adjust node's phandle in node */
Frank Rowand25e16872016-10-28 23:26:26 -070051 if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
52 overlay->phandle += phandle_delta;
Pantelis Antoniou7941b272014-07-04 19:59:20 +030053
Frank Rowand269f1a62016-10-28 23:26:29 -070054 /* copy adjusted phandle into *phandle properties */
Frank Rowand25e16872016-10-28 23:26:26 -070055 for_each_property_of_node(overlay, prop) {
Pantelis Antoniou7941b272014-07-04 19:59:20 +030056
Frank Rowand9f27ede2016-10-28 23:26:23 -070057 if (of_prop_cmp(prop->name, "phandle") &&
58 of_prop_cmp(prop->name, "linux,phandle"))
Pantelis Antoniou7941b272014-07-04 19:59:20 +030059 continue;
60
Pantelis Antoniou7941b272014-07-04 19:59:20 +030061 if (prop->length < 4)
62 continue;
63
Pantelis Antoniou7941b272014-07-04 19:59:20 +030064 phandle = be32_to_cpup(prop->value);
Frank Rowanda67976e2016-10-28 23:26:21 -070065 if (phandle == OF_PHANDLE_ILLEGAL)
Pantelis Antoniou7941b272014-07-04 19:59:20 +030066 continue;
67
Rob Herring17a70352017-05-04 12:56:12 -050068 *(__be32 *)prop->value = cpu_to_be32(overlay->phandle);
Pantelis Antoniou7941b272014-07-04 19:59:20 +030069 }
70
Frank Rowand25e16872016-10-28 23:26:26 -070071 for_each_child_of_node(overlay, child)
Frank Rowandf94823f2016-10-28 23:26:24 -070072 adjust_overlay_phandles(child, phandle_delta);
Pantelis Antoniou7941b272014-07-04 19:59:20 +030073}
74
Frank Rowand25e16872016-10-28 23:26:26 -070075static int update_usages_of_a_phandle_reference(struct device_node *overlay,
76 struct property *prop_fixup, phandle phandle)
Pantelis Antoniou7941b272014-07-04 19:59:20 +030077{
Pantelis Antoniou7941b272014-07-04 19:59:20 +030078 struct device_node *refnode;
Frank Rowand25e16872016-10-28 23:26:26 -070079 struct property *prop;
80 char *value, *cur, *end, *node_path, *prop_name, *s;
81 int offset, len;
Pantelis Antoniou7941b272014-07-04 19:59:20 +030082 int err = 0;
83
Stephen Boydeeb09502017-10-13 00:35:58 -070084 value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
Frank Rowand25e16872016-10-28 23:26:26 -070085 if (!value)
Pantelis Antoniou7941b272014-07-04 19:59:20 +030086 return -ENOMEM;
Pantelis Antoniou7941b272014-07-04 19:59:20 +030087
Frank Rowand269f1a62016-10-28 23:26:29 -070088 /* prop_fixup contains a list of tuples of path:property_name:offset */
Frank Rowand25e16872016-10-28 23:26:26 -070089 end = value + prop_fixup->length;
90 for (cur = value; cur < end; cur += len + 1) {
91 len = strlen(cur);
Pantelis Antoniou7941b272014-07-04 19:59:20 +030092
Frank Rowand25e16872016-10-28 23:26:26 -070093 node_path = cur;
94 s = strchr(cur, ':');
Pantelis Antoniou7941b272014-07-04 19:59:20 +030095 if (!s) {
Pantelis Antoniou7941b272014-07-04 19:59:20 +030096 err = -EINVAL;
97 goto err_fail;
98 }
99 *s++ = '\0';
100
Frank Rowand25e16872016-10-28 23:26:26 -0700101 prop_name = s;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300102 s = strchr(s, ':');
103 if (!s) {
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300104 err = -EINVAL;
105 goto err_fail;
106 }
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300107 *s++ = '\0';
Frank Rowand624ab2a2016-10-28 23:26:27 -0700108
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300109 err = kstrtoint(s, 10, &offset);
Frank Rowand9f27ede2016-10-28 23:26:23 -0700110 if (err)
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300111 goto err_fail;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300112
Rob Herring27497e12017-06-02 12:43:18 -0500113 refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path);
Frank Rowand96d1c8e2016-10-28 23:26:22 -0700114 if (!refnode)
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300115 continue;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300116
Frank Rowand25e16872016-10-28 23:26:26 -0700117 for_each_property_of_node(refnode, prop) {
118 if (!of_prop_cmp(prop->name, prop_name))
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300119 break;
120 }
Amitoj Kaur Chawla82f68752016-02-03 23:39:01 +0530121 of_node_put(refnode);
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300122
Frank Rowand25e16872016-10-28 23:26:26 -0700123 if (!prop) {
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300124 err = -ENOENT;
125 goto err_fail;
126 }
127
Frank Rowand25e16872016-10-28 23:26:26 -0700128 *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300129 }
130
131err_fail:
Frank Rowand25e16872016-10-28 23:26:26 -0700132 kfree(value);
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300133 return err;
134}
135
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200136/* compare nodes taking into account that 'name' strips out the @ part */
Frank Rowandfad556b2016-10-28 23:26:25 -0700137static int node_name_cmp(const struct device_node *dn1,
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200138 const struct device_node *dn2)
139{
Rob Herring95e6b1f2017-06-01 18:00:00 -0500140 const char *n1 = kbasename(dn1->full_name);
141 const char *n2 = kbasename(dn2->full_name);
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200142
143 return of_node_cmp(n1, n2);
144}
145
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300146/*
147 * Adjust the local phandle references by the given phandle delta.
Frank Rowand269f1a62016-10-28 23:26:29 -0700148 *
149 * Subtree @local_fixups, which is overlay node __local_fixups__,
150 * mirrors the fragment node structure at the root of the overlay.
151 *
152 * For each property in the fragments that contains a phandle reference,
153 * @local_fixups has a property of the same name that contains a list
154 * of offsets of the phandle reference(s) within the respective property
155 * value(s). The values at these offsets will be fixed up.
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300156 */
Frank Rowand25e16872016-10-28 23:26:26 -0700157static int adjust_local_phandle_references(struct device_node *local_fixups,
158 struct device_node *overlay, int phandle_delta)
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300159{
Frank Rowand25e16872016-10-28 23:26:26 -0700160 struct device_node *child, *overlay_child;
161 struct property *prop_fix, *prop;
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200162 int err, i, count;
163 unsigned int off;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300164
Frank Rowand25e16872016-10-28 23:26:26 -0700165 if (!local_fixups)
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300166 return 0;
167
Frank Rowand25e16872016-10-28 23:26:26 -0700168 for_each_property_of_node(local_fixups, prop_fix) {
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200169
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300170 /* skip properties added automatically */
Frank Rowand25e16872016-10-28 23:26:26 -0700171 if (!of_prop_cmp(prop_fix->name, "name") ||
172 !of_prop_cmp(prop_fix->name, "phandle") ||
173 !of_prop_cmp(prop_fix->name, "linux,phandle"))
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300174 continue;
175
Frank Rowand25e16872016-10-28 23:26:26 -0700176 if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200177 return -EINVAL;
Frank Rowand25e16872016-10-28 23:26:26 -0700178 count = prop_fix->length / sizeof(__be32);
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200179
Frank Rowand25e16872016-10-28 23:26:26 -0700180 for_each_property_of_node(overlay, prop) {
181 if (!of_prop_cmp(prop->name, prop_fix->name))
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200182 break;
183 }
184
Frank Rowand25e16872016-10-28 23:26:26 -0700185 if (!prop)
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200186 return -EINVAL;
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200187
188 for (i = 0; i < count; i++) {
Frank Rowand25e16872016-10-28 23:26:26 -0700189 off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
Frank Rowandea8229b2016-10-28 23:26:28 -0700190 if ((off + 4) > prop->length)
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200191 return -EINVAL;
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200192
Stephen Boydd35d6232017-10-13 00:35:57 -0700193 be32_add_cpu(prop->value + off, phandle_delta);
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200194 }
195 }
196
Frank Rowand269f1a62016-10-28 23:26:29 -0700197 /*
198 * These nested loops recurse down two subtrees in parallel, where the
199 * node names in the two subtrees match.
200 *
201 * The roots of the subtrees are the overlay's __local_fixups__ node
202 * and the overlay's root node.
203 */
Frank Rowand25e16872016-10-28 23:26:26 -0700204 for_each_child_of_node(local_fixups, child) {
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200205
Frank Rowand25e16872016-10-28 23:26:26 -0700206 for_each_child_of_node(overlay, overlay_child)
207 if (!node_name_cmp(child, overlay_child))
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200208 break;
209
Frank Rowand25e16872016-10-28 23:26:26 -0700210 if (!overlay_child)
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200211 return -EINVAL;
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200212
Frank Rowand25e16872016-10-28 23:26:26 -0700213 err = adjust_local_phandle_references(child, overlay_child,
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200214 phandle_delta);
Frank Rowand9f27ede2016-10-28 23:26:23 -0700215 if (err)
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300216 return err;
217 }
218
219 return 0;
220}
221
222/**
Frank Rowand269f1a62016-10-28 23:26:29 -0700223 * of_resolve_phandles - Relocate and resolve overlay against live tree
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300224 *
Frank Rowand269f1a62016-10-28 23:26:29 -0700225 * @overlay: Pointer to devicetree overlay to relocate and resolve
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300226 *
Frank Rowand269f1a62016-10-28 23:26:29 -0700227 * Modify (relocate) values of local phandles in @overlay to a range that
228 * does not conflict with the live expanded devicetree. Update references
229 * to the local phandles in @overlay. Update (resolve) phandle references
230 * in @overlay that refer to the live expanded devicetree.
231 *
232 * Phandle values in the live tree are in the range of
233 * 1 .. live_tree_max_phandle(). The range of phandle values in the overlay
234 * also begin with at 1. Adjust the phandle values in the overlay to begin
235 * at live_tree_max_phandle() + 1. Update references to the phandles to
236 * the adjusted phandle values.
237 *
238 * The name of each property in the "__fixups__" node in the overlay matches
239 * the name of a symbol (a label) in the live tree. The values of each
240 * property in the "__fixups__" node is a list of the property values in the
241 * overlay that need to be updated to contain the phandle reference
242 * corresponding to that symbol in the live tree. Update the references in
243 * the overlay with the phandle values in the live tree.
244 *
245 * @overlay must be detached.
246 *
247 * Resolving and applying @overlay to the live expanded devicetree must be
248 * protected by a mechanism to ensure that multiple overlays are processed
249 * in a single threaded manner so that multiple overlays will not relocate
250 * phandles to overlapping ranges. The mechanism to enforce this is not
251 * yet implemented.
252 *
253 * Return: %0 on success or a negative error value on error.
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300254 */
Frank Rowand25e16872016-10-28 23:26:26 -0700255int of_resolve_phandles(struct device_node *overlay)
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300256{
Frank Rowand25e16872016-10-28 23:26:26 -0700257 struct device_node *child, *local_fixups, *refnode;
Frank Rowand5581a952016-10-28 23:26:32 -0700258 struct device_node *tree_symbols, *overlay_fixups;
Frank Rowand25e16872016-10-28 23:26:26 -0700259 struct property *prop;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300260 const char *refpath;
261 phandle phandle, phandle_delta;
262 int err;
263
Frank Rowandd9181b22016-10-28 23:26:30 -0700264 tree_symbols = NULL;
265
Frank Rowand624ab2a2016-10-28 23:26:27 -0700266 if (!overlay) {
267 pr_err("null overlay\n");
Frank Rowandd9181b22016-10-28 23:26:30 -0700268 err = -EINVAL;
Moritz Fischer32bed312016-12-01 22:10:25 -0800269 goto out;
Frank Rowand624ab2a2016-10-28 23:26:27 -0700270 }
Frank Rowandf948d6d2017-10-17 16:36:29 -0700271
272#if 0
273 Temporarily disable check so that old style overlay unittests
274 do not fail when of_resolve_phandles() is moved into
275 of_overlay_apply().
276
Frank Rowand624ab2a2016-10-28 23:26:27 -0700277 if (!of_node_check_flag(overlay, OF_DETACHED)) {
278 pr_err("overlay not detached\n");
Frank Rowandd9181b22016-10-28 23:26:30 -0700279 err = -EINVAL;
Moritz Fischer32bed312016-12-01 22:10:25 -0800280 goto out;
Frank Rowand624ab2a2016-10-28 23:26:27 -0700281 }
Frank Rowandf948d6d2017-10-17 16:36:29 -0700282#endif
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300283
Frank Rowandf94823f2016-10-28 23:26:24 -0700284 phandle_delta = live_tree_max_phandle() + 1;
Frank Rowand25e16872016-10-28 23:26:26 -0700285 adjust_overlay_phandles(overlay, phandle_delta);
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200286
Frank Rowand25e16872016-10-28 23:26:26 -0700287 for_each_child_of_node(overlay, local_fixups)
288 if (!of_node_cmp(local_fixups->name, "__local_fixups__"))
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200289 break;
290
Frank Rowand624ab2a2016-10-28 23:26:27 -0700291 err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
292 if (err)
Moritz Fischer32bed312016-12-01 22:10:25 -0800293 goto out;
Pantelis Antoniouda56d042014-10-28 22:33:49 +0200294
Frank Rowand25e16872016-10-28 23:26:26 -0700295 overlay_fixups = NULL;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300296
Frank Rowand25e16872016-10-28 23:26:26 -0700297 for_each_child_of_node(overlay, child) {
Frank Rowand624ab2a2016-10-28 23:26:27 -0700298 if (!of_node_cmp(child->name, "__fixups__"))
Frank Rowand25e16872016-10-28 23:26:26 -0700299 overlay_fixups = child;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300300 }
301
Frank Rowand25e16872016-10-28 23:26:26 -0700302 if (!overlay_fixups) {
Frank Rowanda67976e2016-10-28 23:26:21 -0700303 err = 0;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300304 goto out;
305 }
306
Frank Rowand4458db42016-10-28 23:26:31 -0700307 tree_symbols = of_find_node_by_path("/__symbols__");
Frank Rowand25e16872016-10-28 23:26:26 -0700308 if (!tree_symbols) {
Frank Rowand624ab2a2016-10-28 23:26:27 -0700309 pr_err("no symbols in root of device tree.\n");
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300310 err = -EINVAL;
Moritz Fischer32bed312016-12-01 22:10:25 -0800311 goto out;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300312 }
313
Frank Rowand25e16872016-10-28 23:26:26 -0700314 for_each_property_of_node(overlay_fixups, prop) {
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300315
316 /* skip properties added automatically */
Frank Rowand25e16872016-10-28 23:26:26 -0700317 if (!of_prop_cmp(prop->name, "name"))
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300318 continue;
319
Frank Rowand25e16872016-10-28 23:26:26 -0700320 err = of_property_read_string(tree_symbols,
321 prop->name, &refpath);
Frank Rowand9f27ede2016-10-28 23:26:23 -0700322 if (err)
Moritz Fischer32bed312016-12-01 22:10:25 -0800323 goto out;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300324
325 refnode = of_find_node_by_path(refpath);
326 if (!refnode) {
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300327 err = -ENOENT;
Moritz Fischer32bed312016-12-01 22:10:25 -0800328 goto out;
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300329 }
330
331 phandle = refnode->phandle;
332 of_node_put(refnode);
333
Frank Rowand25e16872016-10-28 23:26:26 -0700334 err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300335 if (err)
336 break;
337 }
338
339out:
Moritz Fischer32bed312016-12-01 22:10:25 -0800340 if (err)
341 pr_err("overlay phandle fixup failed: %d\n", err);
Frank Rowand25e16872016-10-28 23:26:26 -0700342 of_node_put(tree_symbols);
Pantelis Antoniou7941b272014-07-04 19:59:20 +0300343
344 return err;
345}
346EXPORT_SYMBOL_GPL(of_resolve_phandles);