2 * Functions for dealing with DT resolution
4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
5 * Copyright (C) 2012 Texas Instruments Inc.
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.
12 #define pr_fmt(fmt) "OF: resolver: " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.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>
24 /* illegal phandle value (set when unresolved) */
25 #define OF_PHANDLE_ILLEGAL 0xdeadbeef
28 * Find a node with the give full name by recursively following any of
29 * the child node links.
31 static struct device_node
*__of_find_node_by_full_name(struct device_node
*node
,
32 const char *full_name
)
34 struct device_node
*child
, *found
;
40 if (of_node_cmp(node
->full_name
, full_name
) == 0)
41 return of_node_get(node
);
43 for_each_child_of_node(node
, child
) {
44 found
= __of_find_node_by_full_name(child
, full_name
);
55 * Find live tree's maximum phandle value.
57 static phandle
of_get_tree_max_phandle(void)
59 struct device_node
*node
;
63 /* now search recursively */
64 raw_spin_lock_irqsave(&devtree_lock
, flags
);
66 for_each_of_allnodes(node
) {
67 if (node
->phandle
!= OF_PHANDLE_ILLEGAL
&&
68 node
->phandle
> phandle
)
69 phandle
= node
->phandle
;
71 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
77 * Adjust a subtree's phandle values by a given delta.
78 * Makes sure not to just adjust the device node's phandle value,
79 * but modify the phandle properties values as well.
81 static void __of_adjust_tree_phandles(struct device_node
*node
,
84 struct device_node
*child
;
85 struct property
*prop
;
88 /* first adjust the node's phandle direct value */
89 if (node
->phandle
!= 0 && node
->phandle
!= OF_PHANDLE_ILLEGAL
)
90 node
->phandle
+= phandle_delta
;
92 /* now adjust phandle & linux,phandle values */
93 for_each_property_of_node(node
, prop
) {
95 /* only look for these two */
96 if (of_prop_cmp(prop
->name
, "phandle") != 0 &&
97 of_prop_cmp(prop
->name
, "linux,phandle") != 0)
100 /* must be big enough */
101 if (prop
->length
< 4)
104 /* read phandle value */
105 phandle
= be32_to_cpup(prop
->value
);
106 if (phandle
== OF_PHANDLE_ILLEGAL
) /* unresolved */
110 *(uint32_t *)prop
->value
= cpu_to_be32(node
->phandle
);
113 /* now do the children recursively */
114 for_each_child_of_node(node
, child
)
115 __of_adjust_tree_phandles(child
, phandle_delta
);
118 static int __of_adjust_phandle_ref(struct device_node
*node
,
119 struct property
*rprop
, int value
)
122 struct device_node
*refnode
;
123 struct property
*sprop
;
124 char *propval
, *propcur
, *propend
, *nodestr
, *propstr
, *s
;
125 int offset
, propcurlen
;
129 propval
= kmalloc(rprop
->length
, GFP_KERNEL
);
131 pr_err("%s: Could not copy value of '%s'\n",
132 __func__
, rprop
->name
);
135 memcpy(propval
, rprop
->value
, rprop
->length
);
137 propend
= propval
+ rprop
->length
;
138 for (propcur
= propval
; propcur
< propend
; propcur
+= propcurlen
+ 1) {
139 propcurlen
= strlen(propcur
);
142 s
= strchr(propcur
, ':');
144 pr_err("%s: Illegal symbol entry '%s' (1)\n",
154 pr_err("%s: Illegal symbol entry '%s' (2)\n",
155 __func__
, (char *)rprop
->value
);
161 err
= kstrtoint(s
, 10, &offset
);
163 pr_err("%s: Could get offset '%s'\n",
164 __func__
, (char *)rprop
->value
);
168 /* look into the resolve node for the full path */
169 refnode
= __of_find_node_by_full_name(node
, nodestr
);
171 pr_warn("%s: Could not find refnode '%s'\n",
172 __func__
, (char *)rprop
->value
);
176 /* now find the property */
177 for_each_property_of_node(refnode
, sprop
) {
178 if (of_prop_cmp(sprop
->name
, propstr
) == 0)
181 of_node_put(refnode
);
184 pr_err("%s: Could not find property '%s'\n",
185 __func__
, (char *)rprop
->value
);
191 *(__be32
*)(sprop
->value
+ offset
) = cpu_to_be32(phandle
);
199 /* compare nodes taking into account that 'name' strips out the @ part */
200 static int __of_node_name_cmp(const struct device_node
*dn1
,
201 const struct device_node
*dn2
)
203 const char *n1
= strrchr(dn1
->full_name
, '/') ? : "/";
204 const char *n2
= strrchr(dn2
->full_name
, '/') ? : "/";
206 return of_node_cmp(n1
, n2
);
210 * Adjust the local phandle references by the given phandle delta.
211 * Assumes the existances of a __local_fixups__ node at the root.
212 * Assumes that __of_verify_tree_phandle_references has been called.
213 * Does not take any devtree locks so make sure you call this on a tree
214 * which is at the detached state.
216 static int __of_adjust_tree_phandle_references(struct device_node
*node
,
217 struct device_node
*target
, int phandle_delta
)
219 struct device_node
*child
, *childtarget
;
220 struct property
*rprop
, *sprop
;
228 for_each_property_of_node(node
, rprop
) {
230 /* skip properties added automatically */
231 if (of_prop_cmp(rprop
->name
, "name") == 0 ||
232 of_prop_cmp(rprop
->name
, "phandle") == 0 ||
233 of_prop_cmp(rprop
->name
, "linux,phandle") == 0)
236 if ((rprop
->length
% 4) != 0 || rprop
->length
== 0) {
237 pr_err("%s: Illegal property (size) '%s' @%s\n",
238 __func__
, rprop
->name
, node
->full_name
);
241 count
= rprop
->length
/ sizeof(__be32
);
243 /* now find the target property */
244 for_each_property_of_node(target
, sprop
) {
245 if (of_prop_cmp(sprop
->name
, rprop
->name
) == 0)
250 pr_err("%s: Could not find target property '%s' @%s\n",
251 __func__
, rprop
->name
, node
->full_name
);
255 for (i
= 0; i
< count
; i
++) {
256 off
= be32_to_cpu(((__be32
*)rprop
->value
)[i
]);
257 /* make sure the offset doesn't overstep (even wrap) */
258 if (off
>= sprop
->length
||
259 (off
+ 4) > sprop
->length
) {
260 pr_err("%s: Illegal property '%s' @%s\n",
261 __func__
, rprop
->name
,
268 phandle
= be32_to_cpu(*(__be32
*)(sprop
->value
+ off
));
269 phandle
+= phandle_delta
;
270 *(__be32
*)(sprop
->value
+ off
) = cpu_to_be32(phandle
);
275 for_each_child_of_node(node
, child
) {
277 for_each_child_of_node(target
, childtarget
)
278 if (__of_node_name_cmp(child
, childtarget
) == 0)
282 pr_err("%s: Could not find target child '%s' @%s\n",
283 __func__
, child
->name
, node
->full_name
);
287 err
= __of_adjust_tree_phandle_references(child
, childtarget
,
297 * of_resolve - Resolve the given node against the live tree.
299 * @resolve: Node to resolve
301 * Perform dynamic Device Tree resolution against the live tree
302 * to the given node to resolve. This depends on the live tree
303 * having a __symbols__ node, and the resolve node the __fixups__ &
304 * __local_fixups__ nodes (if needed).
305 * The result of the operation is a resolve node that it's contents
306 * are fit to be inserted or operate upon the live tree.
307 * Returns 0 on success or a negative error value on error.
309 int of_resolve_phandles(struct device_node
*resolve
)
311 struct device_node
*child
, *childroot
, *refnode
;
312 struct device_node
*root_sym
, *resolve_sym
, *resolve_fix
;
313 struct property
*rprop
;
315 phandle phandle
, phandle_delta
;
319 pr_err("%s: null node\n", __func__
);
320 if (resolve
&& !of_node_check_flag(resolve
, OF_DETACHED
))
321 pr_err("%s: node %s not detached\n", __func__
,
323 /* the resolve node must exist, and be detached */
324 if (!resolve
|| !of_node_check_flag(resolve
, OF_DETACHED
))
327 /* first we need to adjust the phandles */
328 phandle_delta
= of_get_tree_max_phandle() + 1;
329 __of_adjust_tree_phandles(resolve
, phandle_delta
);
331 /* locate the local fixups */
333 for_each_child_of_node(resolve
, childroot
)
334 if (of_node_cmp(childroot
->name
, "__local_fixups__") == 0)
337 if (childroot
!= NULL
) {
338 /* resolve root is guaranteed to be the '/' */
339 err
= __of_adjust_tree_phandle_references(childroot
,
344 BUG_ON(__of_adjust_tree_phandle_references(childroot
,
345 resolve
, phandle_delta
));
352 /* this may fail (if no fixups are required) */
353 root_sym
= of_find_node_by_path("/__symbols__");
355 /* locate the symbols & fixups nodes on resolve */
356 for_each_child_of_node(resolve
, child
) {
359 of_node_cmp(child
->name
, "__symbols__") == 0)
363 of_node_cmp(child
->name
, "__fixups__") == 0)
366 /* both found, don't bother anymore */
367 if (resolve_sym
&& resolve_fix
)
371 /* we do allow for the case where no fixups are needed */
373 err
= 0; /* no error */
377 /* we need to fixup, but no root symbols... */
379 pr_err("%s: no symbols in root of device tree.\n", __func__
);
384 for_each_property_of_node(resolve_fix
, rprop
) {
386 /* skip properties added automatically */
387 if (of_prop_cmp(rprop
->name
, "name") == 0)
390 err
= of_property_read_string(root_sym
,
391 rprop
->name
, &refpath
);
393 pr_err("%s: Could not find symbol '%s'\n",
394 __func__
, rprop
->name
);
398 refnode
= of_find_node_by_path(refpath
);
400 pr_err("%s: Could not find node by path '%s'\n",
406 phandle
= refnode
->phandle
;
407 of_node_put(refnode
);
409 pr_debug("%s: %s phandle is 0x%08x\n",
410 __func__
, rprop
->name
, phandle
);
412 err
= __of_adjust_phandle_ref(resolve
, rprop
, phandle
);
418 /* NULL is handled by of_node_put as NOP */
419 of_node_put(root_sym
);
423 EXPORT_SYMBOL_GPL(of_resolve_phandles
);