resolver.c 8.4 KB

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