resolver.c 8.9 KB

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