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 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
22 /* illegal phandle value (set when unresolved) */
23 #define OF_PHANDLE_ILLEGAL 0xdeadbeef
26 * Find a node with the give full name by recursively following any of
27 * the child node links.
29 static struct device_node
*__of_find_node_by_full_name(struct device_node
*node
,
30 const char *full_name
)
32 struct device_node
*child
, *found
;
38 if (of_node_cmp(node
->full_name
, full_name
) == 0)
41 for_each_child_of_node(node
, child
) {
42 found
= __of_find_node_by_full_name(child
, full_name
);
51 * Find live tree's maximum phandle value.
53 static phandle
of_get_tree_max_phandle(void)
55 struct device_node
*node
;
59 /* now search recursively */
60 raw_spin_lock_irqsave(&devtree_lock
, flags
);
62 for_each_of_allnodes(node
) {
63 if (node
->phandle
!= OF_PHANDLE_ILLEGAL
&&
64 node
->phandle
> phandle
)
65 phandle
= node
->phandle
;
67 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
73 * Adjust a subtree's phandle values by a given delta.
74 * Makes sure not to just adjust the device node's phandle value,
75 * but modify the phandle properties values as well.
77 static void __of_adjust_tree_phandles(struct device_node
*node
,
80 struct device_node
*child
;
81 struct property
*prop
;
84 /* first adjust the node's phandle direct value */
85 if (node
->phandle
!= 0 && node
->phandle
!= OF_PHANDLE_ILLEGAL
)
86 node
->phandle
+= phandle_delta
;
88 /* now adjust phandle & linux,phandle values */
89 for_each_property_of_node(node
, prop
) {
91 /* only look for these two */
92 if (of_prop_cmp(prop
->name
, "phandle") != 0 &&
93 of_prop_cmp(prop
->name
, "linux,phandle") != 0)
96 /* must be big enough */
100 /* read phandle value */
101 phandle
= be32_to_cpup(prop
->value
);
102 if (phandle
== OF_PHANDLE_ILLEGAL
) /* unresolved */
106 *(uint32_t *)prop
->value
= cpu_to_be32(node
->phandle
);
109 /* now do the children recursively */
110 for_each_child_of_node(node
, child
)
111 __of_adjust_tree_phandles(child
, phandle_delta
);
114 static int __of_adjust_phandle_ref(struct device_node
*node
,
115 struct property
*rprop
, int value
)
118 struct device_node
*refnode
;
119 struct property
*sprop
;
120 char *propval
, *propcur
, *propend
, *nodestr
, *propstr
, *s
;
121 int offset
, propcurlen
;
125 propval
= kmalloc(rprop
->length
, GFP_KERNEL
);
127 pr_err("%s: Could not copy value of '%s'\n",
128 __func__
, rprop
->name
);
131 memcpy(propval
, rprop
->value
, rprop
->length
);
133 propend
= propval
+ rprop
->length
;
134 for (propcur
= propval
; propcur
< propend
; propcur
+= propcurlen
+ 1) {
135 propcurlen
= strlen(propcur
);
138 s
= strchr(propcur
, ':');
140 pr_err("%s: Illegal symbol entry '%s' (1)\n",
150 pr_err("%s: Illegal symbol entry '%s' (2)\n",
151 __func__
, (char *)rprop
->value
);
157 err
= kstrtoint(s
, 10, &offset
);
159 pr_err("%s: Could get offset '%s'\n",
160 __func__
, (char *)rprop
->value
);
164 /* look into the resolve node for the full path */
165 refnode
= __of_find_node_by_full_name(node
, nodestr
);
167 pr_warn("%s: Could not find refnode '%s'\n",
168 __func__
, (char *)rprop
->value
);
172 /* now find the property */
173 for_each_property_of_node(refnode
, sprop
) {
174 if (of_prop_cmp(sprop
->name
, propstr
) == 0)
179 pr_err("%s: Could not find property '%s'\n",
180 __func__
, (char *)rprop
->value
);
186 *(__be32
*)(sprop
->value
+ offset
) = cpu_to_be32(phandle
);
194 /* compare nodes taking into account that 'name' strips out the @ part */
195 static int __of_node_name_cmp(const struct device_node
*dn1
,
196 const struct device_node
*dn2
)
198 const char *n1
= strrchr(dn1
->full_name
, '/') ? : "/";
199 const char *n2
= strrchr(dn2
->full_name
, '/') ? : "/";
201 return of_node_cmp(n1
, n2
);
205 * Adjust the local phandle references by the given phandle delta.
206 * Assumes the existances of a __local_fixups__ node at the root.
207 * Assumes that __of_verify_tree_phandle_references has been called.
208 * Does not take any devtree locks so make sure you call this on a tree
209 * which is at the detached state.
211 static int __of_adjust_tree_phandle_references(struct device_node
*node
,
212 struct device_node
*target
, int phandle_delta
)
214 struct device_node
*child
, *childtarget
;
215 struct property
*rprop
, *sprop
;
223 for_each_property_of_node(node
, rprop
) {
225 /* skip properties added automatically */
226 if (of_prop_cmp(rprop
->name
, "name") == 0 ||
227 of_prop_cmp(rprop
->name
, "phandle") == 0 ||
228 of_prop_cmp(rprop
->name
, "linux,phandle") == 0)
231 if ((rprop
->length
% 4) != 0 || rprop
->length
== 0) {
232 pr_err("%s: Illegal property (size) '%s' @%s\n",
233 __func__
, rprop
->name
, node
->full_name
);
236 count
= rprop
->length
/ sizeof(__be32
);
238 /* now find the target property */
239 for_each_property_of_node(target
, sprop
) {
240 if (of_prop_cmp(sprop
->name
, rprop
->name
) == 0)
245 pr_err("%s: Could not find target property '%s' @%s\n",
246 __func__
, rprop
->name
, node
->full_name
);
250 for (i
= 0; i
< count
; i
++) {
251 off
= be32_to_cpu(((__be32
*)rprop
->value
)[i
]);
252 /* make sure the offset doesn't overstep (even wrap) */
253 if (off
>= sprop
->length
||
254 (off
+ 4) > sprop
->length
) {
255 pr_err("%s: Illegal property '%s' @%s\n",
256 __func__
, rprop
->name
,
263 phandle
= be32_to_cpu(*(__be32
*)(sprop
->value
+ off
));
264 phandle
+= phandle_delta
;
265 *(__be32
*)(sprop
->value
+ off
) = cpu_to_be32(phandle
);
270 for_each_child_of_node(node
, child
) {
272 for_each_child_of_node(target
, childtarget
)
273 if (__of_node_name_cmp(child
, childtarget
) == 0)
277 pr_err("%s: Could not find target child '%s' @%s\n",
278 __func__
, child
->name
, node
->full_name
);
282 err
= __of_adjust_tree_phandle_references(child
, childtarget
,
292 * of_resolve - Resolve the given node against the live tree.
294 * @resolve: Node to resolve
296 * Perform dynamic Device Tree resolution against the live tree
297 * to the given node to resolve. This depends on the live tree
298 * having a __symbols__ node, and the resolve node the __fixups__ &
299 * __local_fixups__ nodes (if needed).
300 * The result of the operation is a resolve node that it's contents
301 * are fit to be inserted or operate upon the live tree.
302 * Returns 0 on success or a negative error value on error.
304 int of_resolve_phandles(struct device_node
*resolve
)
306 struct device_node
*child
, *childroot
, *refnode
;
307 struct device_node
*root_sym
, *resolve_sym
, *resolve_fix
;
308 struct property
*rprop
;
310 phandle phandle
, phandle_delta
;
313 /* the resolve node must exist, and be detached */
314 if (!resolve
|| !of_node_check_flag(resolve
, OF_DETACHED
))
317 /* first we need to adjust the phandles */
318 phandle_delta
= of_get_tree_max_phandle() + 1;
319 __of_adjust_tree_phandles(resolve
, phandle_delta
);
321 /* locate the local fixups */
323 for_each_child_of_node(resolve
, childroot
)
324 if (of_node_cmp(childroot
->name
, "__local_fixups__") == 0)
327 if (childroot
!= NULL
) {
328 /* resolve root is guaranteed to be the '/' */
329 err
= __of_adjust_tree_phandle_references(childroot
,
334 BUG_ON(__of_adjust_tree_phandle_references(childroot
,
335 resolve
, phandle_delta
));
342 /* this may fail (if no fixups are required) */
343 root_sym
= of_find_node_by_path("/__symbols__");
345 /* locate the symbols & fixups nodes on resolve */
346 for_each_child_of_node(resolve
, child
) {
349 of_node_cmp(child
->name
, "__symbols__") == 0)
353 of_node_cmp(child
->name
, "__fixups__") == 0)
356 /* both found, don't bother anymore */
357 if (resolve_sym
&& resolve_fix
)
361 /* we do allow for the case where no fixups are needed */
363 err
= 0; /* no error */
367 /* we need to fixup, but no root symbols... */
373 for_each_property_of_node(resolve_fix
, rprop
) {
375 /* skip properties added automatically */
376 if (of_prop_cmp(rprop
->name
, "name") == 0)
379 err
= of_property_read_string(root_sym
,
380 rprop
->name
, &refpath
);
382 pr_err("%s: Could not find symbol '%s'\n",
383 __func__
, rprop
->name
);
387 refnode
= of_find_node_by_path(refpath
);
389 pr_err("%s: Could not find node by path '%s'\n",
395 phandle
= refnode
->phandle
;
396 of_node_put(refnode
);
398 pr_debug("%s: %s phandle is 0x%08x\n",
399 __func__
, rprop
->name
, phandle
);
401 err
= __of_adjust_phandle_ref(resolve
, rprop
, phandle
);
407 /* NULL is handled by of_node_put as NOP */
408 of_node_put(root_sym
);
412 EXPORT_SYMBOL_GPL(of_resolve_phandles
);