resolver.c 8.4 KB

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