resolver.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. if (offset < 0 || offset + sizeof(__be32) > prop->length) {
  102. err = -EINVAL;
  103. goto err_fail;
  104. }
  105. *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
  106. }
  107. err_fail:
  108. kfree(value);
  109. return err;
  110. }
  111. /* compare nodes taking into account that 'name' strips out the @ part */
  112. static int node_name_cmp(const struct device_node *dn1,
  113. const struct device_node *dn2)
  114. {
  115. const char *n1 = kbasename(dn1->full_name);
  116. const char *n2 = kbasename(dn2->full_name);
  117. return of_node_cmp(n1, n2);
  118. }
  119. /*
  120. * Adjust the local phandle references by the given phandle delta.
  121. *
  122. * Subtree @local_fixups, which is overlay node __local_fixups__,
  123. * mirrors the fragment node structure at the root of the overlay.
  124. *
  125. * For each property in the fragments that contains a phandle reference,
  126. * @local_fixups has a property of the same name that contains a list
  127. * of offsets of the phandle reference(s) within the respective property
  128. * value(s). The values at these offsets will be fixed up.
  129. */
  130. static int adjust_local_phandle_references(struct device_node *local_fixups,
  131. struct device_node *overlay, int phandle_delta)
  132. {
  133. struct device_node *child, *overlay_child;
  134. struct property *prop_fix, *prop;
  135. int err, i, count;
  136. unsigned int off;
  137. if (!local_fixups)
  138. return 0;
  139. for_each_property_of_node(local_fixups, prop_fix) {
  140. /* skip properties added automatically */
  141. if (!of_prop_cmp(prop_fix->name, "name") ||
  142. !of_prop_cmp(prop_fix->name, "phandle") ||
  143. !of_prop_cmp(prop_fix->name, "linux,phandle"))
  144. continue;
  145. if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
  146. return -EINVAL;
  147. count = prop_fix->length / sizeof(__be32);
  148. for_each_property_of_node(overlay, prop) {
  149. if (!of_prop_cmp(prop->name, prop_fix->name))
  150. break;
  151. }
  152. if (!prop)
  153. return -EINVAL;
  154. for (i = 0; i < count; i++) {
  155. off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
  156. if ((off + 4) > prop->length)
  157. return -EINVAL;
  158. be32_add_cpu(prop->value + off, phandle_delta);
  159. }
  160. }
  161. /*
  162. * These nested loops recurse down two subtrees in parallel, where the
  163. * node names in the two subtrees match.
  164. *
  165. * The roots of the subtrees are the overlay's __local_fixups__ node
  166. * and the overlay's root node.
  167. */
  168. for_each_child_of_node(local_fixups, child) {
  169. for_each_child_of_node(overlay, overlay_child)
  170. if (!node_name_cmp(child, overlay_child)) {
  171. of_node_put(overlay_child);
  172. break;
  173. }
  174. if (!overlay_child) {
  175. of_node_put(child);
  176. return -EINVAL;
  177. }
  178. err = adjust_local_phandle_references(child, overlay_child,
  179. phandle_delta);
  180. if (err) {
  181. of_node_put(child);
  182. return err;
  183. }
  184. }
  185. return 0;
  186. }
  187. /**
  188. * of_resolve_phandles - Relocate and resolve overlay against live tree
  189. *
  190. * @overlay: Pointer to devicetree overlay to relocate and resolve
  191. *
  192. * Modify (relocate) values of local phandles in @overlay to a range that
  193. * does not conflict with the live expanded devicetree. Update references
  194. * to the local phandles in @overlay. Update (resolve) phandle references
  195. * in @overlay that refer to the live expanded devicetree.
  196. *
  197. * Phandle values in the live tree are in the range of
  198. * 1 .. live_tree_max_phandle(). The range of phandle values in the overlay
  199. * also begin with at 1. Adjust the phandle values in the overlay to begin
  200. * at live_tree_max_phandle() + 1. Update references to the phandles to
  201. * the adjusted phandle values.
  202. *
  203. * The name of each property in the "__fixups__" node in the overlay matches
  204. * the name of a symbol (a label) in the live tree. The values of each
  205. * property in the "__fixups__" node is a list of the property values in the
  206. * overlay that need to be updated to contain the phandle reference
  207. * corresponding to that symbol in the live tree. Update the references in
  208. * the overlay with the phandle values in the live tree.
  209. *
  210. * @overlay must be detached.
  211. *
  212. * Resolving and applying @overlay to the live expanded devicetree must be
  213. * protected by a mechanism to ensure that multiple overlays are processed
  214. * in a single threaded manner so that multiple overlays will not relocate
  215. * phandles to overlapping ranges. The mechanism to enforce this is not
  216. * yet implemented.
  217. *
  218. * Return: %0 on success or a negative error value on error.
  219. */
  220. int of_resolve_phandles(struct device_node *overlay)
  221. {
  222. struct device_node *child, *local_fixups, *refnode;
  223. struct device_node *tree_symbols, *overlay_fixups;
  224. struct property *prop;
  225. const char *refpath;
  226. phandle phandle, phandle_delta;
  227. int err;
  228. tree_symbols = NULL;
  229. if (!overlay) {
  230. pr_err("null overlay\n");
  231. err = -EINVAL;
  232. goto out;
  233. }
  234. if (!of_node_check_flag(overlay, OF_DETACHED)) {
  235. pr_err("overlay not detached\n");
  236. err = -EINVAL;
  237. goto out;
  238. }
  239. phandle_delta = live_tree_max_phandle() + 1;
  240. adjust_overlay_phandles(overlay, phandle_delta);
  241. for_each_child_of_node(overlay, local_fixups)
  242. if (of_node_name_eq(local_fixups, "__local_fixups__"))
  243. break;
  244. err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
  245. if (err)
  246. goto out;
  247. overlay_fixups = NULL;
  248. for_each_child_of_node(overlay, child) {
  249. if (of_node_name_eq(child, "__fixups__"))
  250. overlay_fixups = child;
  251. }
  252. if (!overlay_fixups) {
  253. err = 0;
  254. goto out;
  255. }
  256. tree_symbols = of_find_node_by_path("/__symbols__");
  257. if (!tree_symbols) {
  258. pr_err("no symbols in root of device tree.\n");
  259. err = -EINVAL;
  260. goto out;
  261. }
  262. for_each_property_of_node(overlay_fixups, prop) {
  263. /* skip properties added automatically */
  264. if (!of_prop_cmp(prop->name, "name"))
  265. continue;
  266. err = of_property_read_string(tree_symbols,
  267. prop->name, &refpath);
  268. if (err) {
  269. pr_err("node label '%s' not found in live devicetree symbols table\n",
  270. prop->name);
  271. goto out;
  272. }
  273. refnode = of_find_node_by_path(refpath);
  274. if (!refnode) {
  275. err = -ENOENT;
  276. goto out;
  277. }
  278. phandle = refnode->phandle;
  279. of_node_put(refnode);
  280. err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
  281. if (err)
  282. break;
  283. }
  284. out:
  285. if (err)
  286. pr_err("overlay phandle fixup failed: %d\n", err);
  287. of_node_put(tree_symbols);
  288. return err;
  289. }
  290. EXPORT_SYMBOL_GPL(of_resolve_phandles);