resolver.c 10 KB

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