resolver.c 8.5 KB

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