resolver.c 9.3 KB

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