resolver.c 9.3 KB

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