1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Functions for dealing with DT resolution
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
6*4882a593Smuzhiyun * Copyright (C) 2012 Texas Instruments Inc.
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #define pr_fmt(fmt) "OF: resolver: " fmt
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/of_device.h>
15*4882a593Smuzhiyun #include <linux/string.h>
16*4882a593Smuzhiyun #include <linux/ctype.h>
17*4882a593Smuzhiyun #include <linux/errno.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "of_private.h"
21*4882a593Smuzhiyun
live_tree_max_phandle(void)22*4882a593Smuzhiyun static phandle live_tree_max_phandle(void)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun struct device_node *node;
25*4882a593Smuzhiyun phandle phandle;
26*4882a593Smuzhiyun unsigned long flags;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun raw_spin_lock_irqsave(&devtree_lock, flags);
29*4882a593Smuzhiyun phandle = 0;
30*4882a593Smuzhiyun for_each_of_allnodes(node) {
31*4882a593Smuzhiyun if (node->phandle != OF_PHANDLE_ILLEGAL &&
32*4882a593Smuzhiyun node->phandle > phandle)
33*4882a593Smuzhiyun phandle = node->phandle;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&devtree_lock, flags);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun return phandle;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
adjust_overlay_phandles(struct device_node * overlay,int phandle_delta)40*4882a593Smuzhiyun static void adjust_overlay_phandles(struct device_node *overlay,
41*4882a593Smuzhiyun int phandle_delta)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun struct device_node *child;
44*4882a593Smuzhiyun struct property *prop;
45*4882a593Smuzhiyun phandle phandle;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* adjust node's phandle in node */
48*4882a593Smuzhiyun if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
49*4882a593Smuzhiyun overlay->phandle += phandle_delta;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* copy adjusted phandle into *phandle properties */
52*4882a593Smuzhiyun for_each_property_of_node(overlay, prop) {
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (of_prop_cmp(prop->name, "phandle") &&
55*4882a593Smuzhiyun of_prop_cmp(prop->name, "linux,phandle"))
56*4882a593Smuzhiyun continue;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun if (prop->length < 4)
59*4882a593Smuzhiyun continue;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun phandle = be32_to_cpup(prop->value);
62*4882a593Smuzhiyun if (phandle == OF_PHANDLE_ILLEGAL)
63*4882a593Smuzhiyun continue;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun *(__be32 *)prop->value = cpu_to_be32(overlay->phandle);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun for_each_child_of_node(overlay, child)
69*4882a593Smuzhiyun adjust_overlay_phandles(child, phandle_delta);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
update_usages_of_a_phandle_reference(struct device_node * overlay,struct property * prop_fixup,phandle phandle)72*4882a593Smuzhiyun static int update_usages_of_a_phandle_reference(struct device_node *overlay,
73*4882a593Smuzhiyun struct property *prop_fixup, phandle phandle)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun struct device_node *refnode;
76*4882a593Smuzhiyun struct property *prop;
77*4882a593Smuzhiyun char *value, *cur, *end, *node_path, *prop_name, *s;
78*4882a593Smuzhiyun int offset, len;
79*4882a593Smuzhiyun int err = 0;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
82*4882a593Smuzhiyun if (!value)
83*4882a593Smuzhiyun return -ENOMEM;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* prop_fixup contains a list of tuples of path:property_name:offset */
86*4882a593Smuzhiyun end = value + prop_fixup->length;
87*4882a593Smuzhiyun for (cur = value; cur < end; cur += len + 1) {
88*4882a593Smuzhiyun len = strlen(cur);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun node_path = cur;
91*4882a593Smuzhiyun s = strchr(cur, ':');
92*4882a593Smuzhiyun if (!s) {
93*4882a593Smuzhiyun err = -EINVAL;
94*4882a593Smuzhiyun goto err_fail;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun *s++ = '\0';
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun prop_name = s;
99*4882a593Smuzhiyun s = strchr(s, ':');
100*4882a593Smuzhiyun if (!s) {
101*4882a593Smuzhiyun err = -EINVAL;
102*4882a593Smuzhiyun goto err_fail;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun *s++ = '\0';
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun err = kstrtoint(s, 10, &offset);
107*4882a593Smuzhiyun if (err)
108*4882a593Smuzhiyun goto err_fail;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path);
111*4882a593Smuzhiyun if (!refnode)
112*4882a593Smuzhiyun continue;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun for_each_property_of_node(refnode, prop) {
115*4882a593Smuzhiyun if (!of_prop_cmp(prop->name, prop_name))
116*4882a593Smuzhiyun break;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun of_node_put(refnode);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (!prop) {
121*4882a593Smuzhiyun err = -ENOENT;
122*4882a593Smuzhiyun goto err_fail;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (offset < 0 || offset + sizeof(__be32) > prop->length) {
126*4882a593Smuzhiyun err = -EINVAL;
127*4882a593Smuzhiyun goto err_fail;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun err_fail:
134*4882a593Smuzhiyun kfree(value);
135*4882a593Smuzhiyun return err;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* compare nodes taking into account that 'name' strips out the @ part */
node_name_cmp(const struct device_node * dn1,const struct device_node * dn2)139*4882a593Smuzhiyun static int node_name_cmp(const struct device_node *dn1,
140*4882a593Smuzhiyun const struct device_node *dn2)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun const char *n1 = kbasename(dn1->full_name);
143*4882a593Smuzhiyun const char *n2 = kbasename(dn2->full_name);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun return of_node_cmp(n1, n2);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun * Adjust the local phandle references by the given phandle delta.
150*4882a593Smuzhiyun *
151*4882a593Smuzhiyun * Subtree @local_fixups, which is overlay node __local_fixups__,
152*4882a593Smuzhiyun * mirrors the fragment node structure at the root of the overlay.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * For each property in the fragments that contains a phandle reference,
155*4882a593Smuzhiyun * @local_fixups has a property of the same name that contains a list
156*4882a593Smuzhiyun * of offsets of the phandle reference(s) within the respective property
157*4882a593Smuzhiyun * value(s). The values at these offsets will be fixed up.
158*4882a593Smuzhiyun */
adjust_local_phandle_references(struct device_node * local_fixups,struct device_node * overlay,int phandle_delta)159*4882a593Smuzhiyun static int adjust_local_phandle_references(struct device_node *local_fixups,
160*4882a593Smuzhiyun struct device_node *overlay, int phandle_delta)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct device_node *child, *overlay_child;
163*4882a593Smuzhiyun struct property *prop_fix, *prop;
164*4882a593Smuzhiyun int err, i, count;
165*4882a593Smuzhiyun unsigned int off;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun if (!local_fixups)
168*4882a593Smuzhiyun return 0;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun for_each_property_of_node(local_fixups, prop_fix) {
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* skip properties added automatically */
173*4882a593Smuzhiyun if (!of_prop_cmp(prop_fix->name, "name") ||
174*4882a593Smuzhiyun !of_prop_cmp(prop_fix->name, "phandle") ||
175*4882a593Smuzhiyun !of_prop_cmp(prop_fix->name, "linux,phandle"))
176*4882a593Smuzhiyun continue;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
179*4882a593Smuzhiyun return -EINVAL;
180*4882a593Smuzhiyun count = prop_fix->length / sizeof(__be32);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun for_each_property_of_node(overlay, prop) {
183*4882a593Smuzhiyun if (!of_prop_cmp(prop->name, prop_fix->name))
184*4882a593Smuzhiyun break;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (!prop)
188*4882a593Smuzhiyun return -EINVAL;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun for (i = 0; i < count; i++) {
191*4882a593Smuzhiyun off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
192*4882a593Smuzhiyun if ((off + 4) > prop->length)
193*4882a593Smuzhiyun return -EINVAL;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun be32_add_cpu(prop->value + off, phandle_delta);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /*
200*4882a593Smuzhiyun * These nested loops recurse down two subtrees in parallel, where the
201*4882a593Smuzhiyun * node names in the two subtrees match.
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * The roots of the subtrees are the overlay's __local_fixups__ node
204*4882a593Smuzhiyun * and the overlay's root node.
205*4882a593Smuzhiyun */
206*4882a593Smuzhiyun for_each_child_of_node(local_fixups, child) {
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun for_each_child_of_node(overlay, overlay_child)
209*4882a593Smuzhiyun if (!node_name_cmp(child, overlay_child)) {
210*4882a593Smuzhiyun of_node_put(overlay_child);
211*4882a593Smuzhiyun break;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun if (!overlay_child) {
215*4882a593Smuzhiyun of_node_put(child);
216*4882a593Smuzhiyun return -EINVAL;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun err = adjust_local_phandle_references(child, overlay_child,
220*4882a593Smuzhiyun phandle_delta);
221*4882a593Smuzhiyun if (err) {
222*4882a593Smuzhiyun of_node_put(child);
223*4882a593Smuzhiyun return err;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun return 0;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun * of_resolve_phandles - Relocate and resolve overlay against live tree
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * @overlay: Pointer to devicetree overlay to relocate and resolve
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * Modify (relocate) values of local phandles in @overlay to a range that
236*4882a593Smuzhiyun * does not conflict with the live expanded devicetree. Update references
237*4882a593Smuzhiyun * to the local phandles in @overlay. Update (resolve) phandle references
238*4882a593Smuzhiyun * in @overlay that refer to the live expanded devicetree.
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun * Phandle values in the live tree are in the range of
241*4882a593Smuzhiyun * 1 .. live_tree_max_phandle(). The range of phandle values in the overlay
242*4882a593Smuzhiyun * also begin with at 1. Adjust the phandle values in the overlay to begin
243*4882a593Smuzhiyun * at live_tree_max_phandle() + 1. Update references to the phandles to
244*4882a593Smuzhiyun * the adjusted phandle values.
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * The name of each property in the "__fixups__" node in the overlay matches
247*4882a593Smuzhiyun * the name of a symbol (a label) in the live tree. The values of each
248*4882a593Smuzhiyun * property in the "__fixups__" node is a list of the property values in the
249*4882a593Smuzhiyun * overlay that need to be updated to contain the phandle reference
250*4882a593Smuzhiyun * corresponding to that symbol in the live tree. Update the references in
251*4882a593Smuzhiyun * the overlay with the phandle values in the live tree.
252*4882a593Smuzhiyun *
253*4882a593Smuzhiyun * @overlay must be detached.
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * Resolving and applying @overlay to the live expanded devicetree must be
256*4882a593Smuzhiyun * protected by a mechanism to ensure that multiple overlays are processed
257*4882a593Smuzhiyun * in a single threaded manner so that multiple overlays will not relocate
258*4882a593Smuzhiyun * phandles to overlapping ranges. The mechanism to enforce this is not
259*4882a593Smuzhiyun * yet implemented.
260*4882a593Smuzhiyun *
261*4882a593Smuzhiyun * Return: %0 on success or a negative error value on error.
262*4882a593Smuzhiyun */
of_resolve_phandles(struct device_node * overlay)263*4882a593Smuzhiyun int of_resolve_phandles(struct device_node *overlay)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun struct device_node *child, *local_fixups, *refnode;
266*4882a593Smuzhiyun struct device_node *tree_symbols, *overlay_fixups;
267*4882a593Smuzhiyun struct property *prop;
268*4882a593Smuzhiyun const char *refpath;
269*4882a593Smuzhiyun phandle phandle, phandle_delta;
270*4882a593Smuzhiyun int err;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun tree_symbols = NULL;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun if (!overlay) {
275*4882a593Smuzhiyun pr_err("null overlay\n");
276*4882a593Smuzhiyun err = -EINVAL;
277*4882a593Smuzhiyun goto out;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (!of_node_check_flag(overlay, OF_DETACHED)) {
281*4882a593Smuzhiyun pr_err("overlay not detached\n");
282*4882a593Smuzhiyun err = -EINVAL;
283*4882a593Smuzhiyun goto out;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun phandle_delta = live_tree_max_phandle() + 1;
287*4882a593Smuzhiyun adjust_overlay_phandles(overlay, phandle_delta);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun for_each_child_of_node(overlay, local_fixups)
290*4882a593Smuzhiyun if (of_node_name_eq(local_fixups, "__local_fixups__"))
291*4882a593Smuzhiyun break;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
294*4882a593Smuzhiyun if (err)
295*4882a593Smuzhiyun goto out;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun overlay_fixups = NULL;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun for_each_child_of_node(overlay, child) {
300*4882a593Smuzhiyun if (of_node_name_eq(child, "__fixups__"))
301*4882a593Smuzhiyun overlay_fixups = child;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun if (!overlay_fixups) {
305*4882a593Smuzhiyun err = 0;
306*4882a593Smuzhiyun goto out;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun tree_symbols = of_find_node_by_path("/__symbols__");
310*4882a593Smuzhiyun if (!tree_symbols) {
311*4882a593Smuzhiyun pr_err("no symbols in root of device tree.\n");
312*4882a593Smuzhiyun err = -EINVAL;
313*4882a593Smuzhiyun goto out;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun for_each_property_of_node(overlay_fixups, prop) {
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun /* skip properties added automatically */
319*4882a593Smuzhiyun if (!of_prop_cmp(prop->name, "name"))
320*4882a593Smuzhiyun continue;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun err = of_property_read_string(tree_symbols,
323*4882a593Smuzhiyun prop->name, &refpath);
324*4882a593Smuzhiyun if (err) {
325*4882a593Smuzhiyun pr_err("node label '%s' not found in live devicetree symbols table\n",
326*4882a593Smuzhiyun prop->name);
327*4882a593Smuzhiyun goto out;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun refnode = of_find_node_by_path(refpath);
331*4882a593Smuzhiyun if (!refnode) {
332*4882a593Smuzhiyun err = -ENOENT;
333*4882a593Smuzhiyun goto out;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun phandle = refnode->phandle;
337*4882a593Smuzhiyun of_node_put(refnode);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
340*4882a593Smuzhiyun if (err)
341*4882a593Smuzhiyun break;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun out:
345*4882a593Smuzhiyun if (err)
346*4882a593Smuzhiyun pr_err("overlay phandle fixup failed: %d\n", err);
347*4882a593Smuzhiyun of_node_put(tree_symbols);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun return err;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_resolve_phandles);
352