resolver.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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,
  96. struct property *rprop, int value)
  97. {
  98. phandle phandle;
  99. struct device_node *refnode;
  100. struct property *sprop;
  101. char *propval, *propcur, *propend, *nodestr, *propstr, *s;
  102. int offset, propcurlen;
  103. int err = 0;
  104. /* make a copy */
  105. propval = kmalloc(rprop->length, GFP_KERNEL);
  106. if (!propval) {
  107. pr_err("%s: Could not copy value of '%s'\n",
  108. __func__, rprop->name);
  109. return -ENOMEM;
  110. }
  111. memcpy(propval, rprop->value, rprop->length);
  112. propend = propval + rprop->length;
  113. for (propcur = propval; propcur < propend; propcur += propcurlen + 1) {
  114. propcurlen = strlen(propcur);
  115. nodestr = propcur;
  116. s = strchr(propcur, ':');
  117. if (!s) {
  118. pr_err("%s: Illegal symbol entry '%s' (1)\n",
  119. __func__, propcur);
  120. err = -EINVAL;
  121. goto err_fail;
  122. }
  123. *s++ = '\0';
  124. propstr = s;
  125. s = strchr(s, ':');
  126. if (!s) {
  127. pr_err("%s: Illegal symbol entry '%s' (2)\n",
  128. __func__, (char *)rprop->value);
  129. err = -EINVAL;
  130. goto err_fail;
  131. }
  132. *s++ = '\0';
  133. err = kstrtoint(s, 10, &offset);
  134. if (err != 0) {
  135. pr_err("%s: Could get offset '%s'\n",
  136. __func__, (char *)rprop->value);
  137. goto err_fail;
  138. }
  139. /* look into the resolve node for the full path */
  140. refnode = __of_find_node_by_full_name(node, nodestr);
  141. if (!refnode) {
  142. pr_warn("%s: Could not find refnode '%s'\n",
  143. __func__, (char *)rprop->value);
  144. continue;
  145. }
  146. /* now find the property */
  147. for_each_property_of_node(refnode, sprop) {
  148. if (of_prop_cmp(sprop->name, propstr) == 0)
  149. break;
  150. }
  151. if (!sprop) {
  152. pr_err("%s: Could not find property '%s'\n",
  153. __func__, (char *)rprop->value);
  154. err = -ENOENT;
  155. goto err_fail;
  156. }
  157. phandle = value;
  158. *(__be32 *)(sprop->value + offset) = cpu_to_be32(phandle);
  159. }
  160. err_fail:
  161. kfree(propval);
  162. return err;
  163. }
  164. /* compare nodes taking into account that 'name' strips out the @ part */
  165. static int __of_node_name_cmp(const struct device_node *dn1,
  166. const struct device_node *dn2)
  167. {
  168. const char *n1 = strrchr(dn1->full_name, '/') ? : "/";
  169. const char *n2 = strrchr(dn2->full_name, '/') ? : "/";
  170. return of_node_cmp(n1, n2);
  171. }
  172. /*
  173. * Adjust the local phandle references by the given phandle delta.
  174. * Assumes the existances of a __local_fixups__ node at the root.
  175. * Assumes that __of_verify_tree_phandle_references has been called.
  176. * Does not take any devtree locks so make sure you call this on a tree
  177. * which is at the detached state.
  178. */
  179. static int __of_adjust_tree_phandle_references(struct device_node *node,
  180. struct device_node *target, int phandle_delta)
  181. {
  182. struct device_node *child, *childtarget;
  183. struct property *rprop, *sprop;
  184. int err, i, count;
  185. unsigned int off;
  186. phandle phandle;
  187. if (node == NULL)
  188. return 0;
  189. for_each_property_of_node(node, rprop) {
  190. /* skip properties added automatically */
  191. if (of_prop_cmp(rprop->name, "name") == 0 ||
  192. of_prop_cmp(rprop->name, "phandle") == 0 ||
  193. of_prop_cmp(rprop->name, "linux,phandle") == 0)
  194. continue;
  195. if ((rprop->length % 4) != 0 || rprop->length == 0) {
  196. pr_err("%s: Illegal property (size) '%s' @%s\n",
  197. __func__, rprop->name, node->full_name);
  198. return -EINVAL;
  199. }
  200. count = rprop->length / sizeof(__be32);
  201. /* now find the target property */
  202. for_each_property_of_node(target, sprop) {
  203. if (of_prop_cmp(sprop->name, rprop->name) == 0)
  204. break;
  205. }
  206. if (sprop == NULL) {
  207. pr_err("%s: Could not find target property '%s' @%s\n",
  208. __func__, rprop->name, node->full_name);
  209. return -EINVAL;
  210. }
  211. for (i = 0; i < count; i++) {
  212. off = be32_to_cpu(((__be32 *)rprop->value)[i]);
  213. /* make sure the offset doesn't overstep (even wrap) */
  214. if (off >= sprop->length ||
  215. (off + 4) > sprop->length) {
  216. pr_err("%s: Illegal property '%s' @%s\n",
  217. __func__, rprop->name,
  218. node->full_name);
  219. return -EINVAL;
  220. }
  221. if (phandle_delta) {
  222. /* adjust */
  223. phandle = be32_to_cpu(*(__be32 *)(sprop->value + off));
  224. phandle += phandle_delta;
  225. *(__be32 *)(sprop->value + off) = cpu_to_be32(phandle);
  226. }
  227. }
  228. }
  229. for_each_child_of_node(node, child) {
  230. for_each_child_of_node(target, childtarget)
  231. if (__of_node_name_cmp(child, childtarget) == 0)
  232. break;
  233. if (!childtarget) {
  234. pr_err("%s: Could not find target child '%s' @%s\n",
  235. __func__, child->name, node->full_name);
  236. return -EINVAL;
  237. }
  238. err = __of_adjust_tree_phandle_references(child, childtarget,
  239. phandle_delta);
  240. if (err != 0)
  241. return err;
  242. }
  243. return 0;
  244. }
  245. /**
  246. * of_resolve - Resolve the given node against the live tree.
  247. *
  248. * @resolve: Node to resolve
  249. *
  250. * Perform dynamic Device Tree resolution against the live tree
  251. * to the given node to resolve. This depends on the live tree
  252. * having a __symbols__ node, and the resolve node the __fixups__ &
  253. * __local_fixups__ nodes (if needed).
  254. * The result of the operation is a resolve node that it's contents
  255. * are fit to be inserted or operate upon the live tree.
  256. * Returns 0 on success or a negative error value on error.
  257. */
  258. int of_resolve_phandles(struct device_node *resolve)
  259. {
  260. struct device_node *child, *childroot, *refnode;
  261. struct device_node *root_sym, *resolve_sym, *resolve_fix;
  262. struct property *rprop;
  263. const char *refpath;
  264. phandle phandle, phandle_delta;
  265. int err;
  266. /* the resolve node must exist, and be detached */
  267. if (!resolve || !of_node_check_flag(resolve, OF_DETACHED))
  268. return -EINVAL;
  269. /* first we need to adjust the phandles */
  270. phandle_delta = of_get_tree_max_phandle() + 1;
  271. __of_adjust_tree_phandles(resolve, phandle_delta);
  272. /* locate the local fixups */
  273. childroot = NULL;
  274. for_each_child_of_node(resolve, childroot)
  275. if (of_node_cmp(childroot->name, "__local_fixups__") == 0)
  276. break;
  277. if (childroot != NULL) {
  278. /* resolve root is guaranteed to be the '/' */
  279. err = __of_adjust_tree_phandle_references(childroot,
  280. resolve, 0);
  281. if (err != 0)
  282. return err;
  283. BUG_ON(__of_adjust_tree_phandle_references(childroot,
  284. resolve, phandle_delta));
  285. }
  286. root_sym = NULL;
  287. resolve_sym = NULL;
  288. resolve_fix = NULL;
  289. /* this may fail (if no fixups are required) */
  290. root_sym = of_find_node_by_path("/__symbols__");
  291. /* locate the symbols & fixups nodes on resolve */
  292. for_each_child_of_node(resolve, child) {
  293. if (!resolve_sym &&
  294. of_node_cmp(child->name, "__symbols__") == 0)
  295. resolve_sym = child;
  296. if (!resolve_fix &&
  297. of_node_cmp(child->name, "__fixups__") == 0)
  298. resolve_fix = child;
  299. /* both found, don't bother anymore */
  300. if (resolve_sym && resolve_fix)
  301. break;
  302. }
  303. /* we do allow for the case where no fixups are needed */
  304. if (!resolve_fix) {
  305. err = 0; /* no error */
  306. goto out;
  307. }
  308. /* we need to fixup, but no root symbols... */
  309. if (!root_sym) {
  310. err = -EINVAL;
  311. goto out;
  312. }
  313. for_each_property_of_node(resolve_fix, rprop) {
  314. /* skip properties added automatically */
  315. if (of_prop_cmp(rprop->name, "name") == 0)
  316. continue;
  317. err = of_property_read_string(root_sym,
  318. rprop->name, &refpath);
  319. if (err != 0) {
  320. pr_err("%s: Could not find symbol '%s'\n",
  321. __func__, rprop->name);
  322. goto out;
  323. }
  324. refnode = of_find_node_by_path(refpath);
  325. if (!refnode) {
  326. pr_err("%s: Could not find node by path '%s'\n",
  327. __func__, refpath);
  328. err = -ENOENT;
  329. goto out;
  330. }
  331. phandle = refnode->phandle;
  332. of_node_put(refnode);
  333. pr_debug("%s: %s phandle is 0x%08x\n",
  334. __func__, rprop->name, phandle);
  335. err = __of_adjust_phandle_ref(resolve, rprop, phandle);
  336. if (err)
  337. break;
  338. }
  339. out:
  340. /* NULL is handled by of_node_put as NOP */
  341. of_node_put(root_sym);
  342. return err;
  343. }
  344. EXPORT_SYMBOL_GPL(of_resolve_phandles);