resolver.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/string.h>
  16. #include <linux/ctype.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.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 *__of_find_node_by_full_name(struct device_node *node,
  27. const char *full_name)
  28. {
  29. struct device_node *child, *found;
  30. if (node == NULL)
  31. return NULL;
  32. /* check */
  33. if (of_node_cmp(node->full_name, full_name) == 0)
  34. return node;
  35. for_each_child_of_node(node, child) {
  36. found = __of_find_node_by_full_name(child, full_name);
  37. if (found != NULL)
  38. return found;
  39. }
  40. return NULL;
  41. }
  42. /*
  43. * Find live tree's maximum phandle value.
  44. */
  45. static phandle of_get_tree_max_phandle(void)
  46. {
  47. struct device_node *node;
  48. phandle phandle;
  49. unsigned long flags;
  50. /* now search recursively */
  51. raw_spin_lock_irqsave(&devtree_lock, flags);
  52. phandle = 0;
  53. for_each_of_allnodes(node) {
  54. if (node->phandle != OF_PHANDLE_ILLEGAL &&
  55. node->phandle > phandle)
  56. phandle = node->phandle;
  57. }
  58. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  59. return phandle;
  60. }
  61. /*
  62. * Adjust a subtree's phandle values by a given delta.
  63. * Makes sure not to just adjust the device node's phandle value,
  64. * but modify the phandle properties values as well.
  65. */
  66. static void __of_adjust_tree_phandles(struct device_node *node,
  67. int phandle_delta)
  68. {
  69. struct device_node *child;
  70. struct property *prop;
  71. phandle phandle;
  72. /* first adjust the node's phandle direct value */
  73. if (node->phandle != 0 && node->phandle != OF_PHANDLE_ILLEGAL)
  74. node->phandle += phandle_delta;
  75. /* now adjust phandle & linux,phandle values */
  76. for_each_property_of_node(node, prop) {
  77. /* only look for these two */
  78. if (of_prop_cmp(prop->name, "phandle") != 0 &&
  79. of_prop_cmp(prop->name, "linux,phandle") != 0)
  80. continue;
  81. /* must be big enough */
  82. if (prop->length < 4)
  83. continue;
  84. /* read phandle value */
  85. phandle = be32_to_cpup(prop->value);
  86. if (phandle == OF_PHANDLE_ILLEGAL) /* unresolved */
  87. continue;
  88. /* adjust */
  89. *(uint32_t *)prop->value = cpu_to_be32(node->phandle);
  90. }
  91. /* now do the children recursively */
  92. for_each_child_of_node(node, child)
  93. __of_adjust_tree_phandles(child, phandle_delta);
  94. }
  95. static int __of_adjust_phandle_ref(struct device_node *node, struct property *rprop, int value, bool is_delta)
  96. {
  97. phandle phandle;
  98. struct device_node *refnode;
  99. struct property *sprop;
  100. char *propval, *propcur, *propend, *nodestr, *propstr, *s;
  101. int offset, propcurlen;
  102. int err = 0;
  103. /* make a copy */
  104. propval = kmalloc(rprop->length, GFP_KERNEL);
  105. if (!propval) {
  106. pr_err("%s: Could not copy value of '%s'\n",
  107. __func__, rprop->name);
  108. return -ENOMEM;
  109. }
  110. memcpy(propval, rprop->value, rprop->length);
  111. propend = propval + rprop->length;
  112. for (propcur = propval; propcur < propend; propcur += propcurlen + 1) {
  113. propcurlen = strlen(propcur);
  114. nodestr = propcur;
  115. s = strchr(propcur, ':');
  116. if (!s) {
  117. pr_err("%s: Illegal symbol entry '%s' (1)\n",
  118. __func__, propcur);
  119. err = -EINVAL;
  120. goto err_fail;
  121. }
  122. *s++ = '\0';
  123. propstr = s;
  124. s = strchr(s, ':');
  125. if (!s) {
  126. pr_err("%s: Illegal symbol entry '%s' (2)\n",
  127. __func__, (char *)rprop->value);
  128. err = -EINVAL;
  129. goto err_fail;
  130. }
  131. *s++ = '\0';
  132. err = kstrtoint(s, 10, &offset);
  133. if (err != 0) {
  134. pr_err("%s: Could get offset '%s'\n",
  135. __func__, (char *)rprop->value);
  136. goto err_fail;
  137. }
  138. /* look into the resolve node for the full path */
  139. refnode = __of_find_node_by_full_name(node, nodestr);
  140. if (!refnode) {
  141. pr_warn("%s: Could not find refnode '%s'\n",
  142. __func__, (char *)rprop->value);
  143. continue;
  144. }
  145. /* now find the property */
  146. for_each_property_of_node(refnode, sprop) {
  147. if (of_prop_cmp(sprop->name, propstr) == 0)
  148. break;
  149. }
  150. if (!sprop) {
  151. pr_err("%s: Could not find property '%s'\n",
  152. __func__, (char *)rprop->value);
  153. err = -ENOENT;
  154. goto err_fail;
  155. }
  156. phandle = is_delta ? be32_to_cpup(sprop->value + offset) + value : value;
  157. *(__be32 *)(sprop->value + offset) = cpu_to_be32(phandle);
  158. }
  159. err_fail:
  160. kfree(propval);
  161. return err;
  162. }
  163. /*
  164. * Adjust the local phandle references by the given phandle delta.
  165. * Assumes the existances of a __local_fixups__ node at the root
  166. * of the tree. Does not take any devtree locks so make sure you
  167. * call this on a tree which is at the detached state.
  168. */
  169. static int __of_adjust_tree_phandle_references(struct device_node *node,
  170. int phandle_delta)
  171. {
  172. struct device_node *child;
  173. struct property *rprop;
  174. int err;
  175. /* locate the symbols & fixups nodes on resolve */
  176. for_each_child_of_node(node, child)
  177. if (of_node_cmp(child->name, "__local_fixups__") == 0)
  178. break;
  179. /* no local fixups */
  180. if (!child)
  181. return 0;
  182. /* find the local fixups property */
  183. for_each_property_of_node(child, rprop) {
  184. /* skip properties added automatically */
  185. if (of_prop_cmp(rprop->name, "name") == 0)
  186. continue;
  187. err = __of_adjust_phandle_ref(node, rprop, phandle_delta, true);
  188. if (err)
  189. return err;
  190. }
  191. return 0;
  192. }
  193. /**
  194. * of_resolve - Resolve the given node against the live tree.
  195. *
  196. * @resolve: Node to resolve
  197. *
  198. * Perform dynamic Device Tree resolution against the live tree
  199. * to the given node to resolve. This depends on the live tree
  200. * having a __symbols__ node, and the resolve node the __fixups__ &
  201. * __local_fixups__ nodes (if needed).
  202. * The result of the operation is a resolve node that it's contents
  203. * are fit to be inserted or operate upon the live tree.
  204. * Returns 0 on success or a negative error value on error.
  205. */
  206. int of_resolve_phandles(struct device_node *resolve)
  207. {
  208. struct device_node *child, *refnode;
  209. struct device_node *root_sym, *resolve_sym, *resolve_fix;
  210. struct property *rprop;
  211. const char *refpath;
  212. phandle phandle, phandle_delta;
  213. int err;
  214. /* the resolve node must exist, and be detached */
  215. if (!resolve || !of_node_check_flag(resolve, OF_DETACHED))
  216. return -EINVAL;
  217. /* first we need to adjust the phandles */
  218. phandle_delta = of_get_tree_max_phandle() + 1;
  219. __of_adjust_tree_phandles(resolve, phandle_delta);
  220. err = __of_adjust_tree_phandle_references(resolve, phandle_delta);
  221. if (err != 0)
  222. return err;
  223. root_sym = NULL;
  224. resolve_sym = NULL;
  225. resolve_fix = NULL;
  226. /* this may fail (if no fixups are required) */
  227. root_sym = of_find_node_by_path("/__symbols__");
  228. /* locate the symbols & fixups nodes on resolve */
  229. for_each_child_of_node(resolve, child) {
  230. if (!resolve_sym &&
  231. of_node_cmp(child->name, "__symbols__") == 0)
  232. resolve_sym = child;
  233. if (!resolve_fix &&
  234. of_node_cmp(child->name, "__fixups__") == 0)
  235. resolve_fix = child;
  236. /* both found, don't bother anymore */
  237. if (resolve_sym && resolve_fix)
  238. break;
  239. }
  240. /* we do allow for the case where no fixups are needed */
  241. if (!resolve_fix) {
  242. err = 0; /* no error */
  243. goto out;
  244. }
  245. /* we need to fixup, but no root symbols... */
  246. if (!root_sym) {
  247. err = -EINVAL;
  248. goto out;
  249. }
  250. for_each_property_of_node(resolve_fix, rprop) {
  251. /* skip properties added automatically */
  252. if (of_prop_cmp(rprop->name, "name") == 0)
  253. continue;
  254. err = of_property_read_string(root_sym,
  255. rprop->name, &refpath);
  256. if (err != 0) {
  257. pr_err("%s: Could not find symbol '%s'\n",
  258. __func__, rprop->name);
  259. goto out;
  260. }
  261. refnode = of_find_node_by_path(refpath);
  262. if (!refnode) {
  263. pr_err("%s: Could not find node by path '%s'\n",
  264. __func__, refpath);
  265. err = -ENOENT;
  266. goto out;
  267. }
  268. phandle = refnode->phandle;
  269. of_node_put(refnode);
  270. pr_debug("%s: %s phandle is 0x%08x\n",
  271. __func__, rprop->name, phandle);
  272. err = __of_adjust_phandle_ref(resolve, rprop, phandle, false);
  273. if (err)
  274. break;
  275. }
  276. out:
  277. /* NULL is handled by of_node_put as NOP */
  278. of_node_put(root_sym);
  279. return err;
  280. }
  281. EXPORT_SYMBOL_GPL(of_resolve_phandles);