resolver.c 8.8 KB

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