resolver.c 10 KB

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