fdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Functions for working with the Flattened Device Tree data format
  3. *
  4. * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
  5. * benh@kernel.crashing.org
  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/lmb.h>
  13. #include <linux/initrd.h>
  14. #include <linux/of.h>
  15. #include <linux/of_fdt.h>
  16. int __initdata dt_root_addr_cells;
  17. int __initdata dt_root_size_cells;
  18. struct boot_param_header *initial_boot_params;
  19. char *find_flat_dt_string(u32 offset)
  20. {
  21. return ((char *)initial_boot_params) +
  22. initial_boot_params->off_dt_strings + offset;
  23. }
  24. /**
  25. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  26. * @it: callback function
  27. * @data: context data pointer
  28. *
  29. * This function is used to scan the flattened device-tree, it is
  30. * used to extract the memory information at boot before we can
  31. * unflatten the tree
  32. */
  33. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  34. const char *uname, int depth,
  35. void *data),
  36. void *data)
  37. {
  38. unsigned long p = ((unsigned long)initial_boot_params) +
  39. initial_boot_params->off_dt_struct;
  40. int rc = 0;
  41. int depth = -1;
  42. do {
  43. u32 tag = *((u32 *)p);
  44. char *pathp;
  45. p += 4;
  46. if (tag == OF_DT_END_NODE) {
  47. depth--;
  48. continue;
  49. }
  50. if (tag == OF_DT_NOP)
  51. continue;
  52. if (tag == OF_DT_END)
  53. break;
  54. if (tag == OF_DT_PROP) {
  55. u32 sz = *((u32 *)p);
  56. p += 8;
  57. if (initial_boot_params->version < 0x10)
  58. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  59. p += sz;
  60. p = _ALIGN(p, 4);
  61. continue;
  62. }
  63. if (tag != OF_DT_BEGIN_NODE) {
  64. pr_err("Invalid tag %x in flat device tree!\n", tag);
  65. return -EINVAL;
  66. }
  67. depth++;
  68. pathp = (char *)p;
  69. p = _ALIGN(p + strlen(pathp) + 1, 4);
  70. if ((*pathp) == '/') {
  71. char *lp, *np;
  72. for (lp = NULL, np = pathp; *np; np++)
  73. if ((*np) == '/')
  74. lp = np+1;
  75. if (lp != NULL)
  76. pathp = lp;
  77. }
  78. rc = it(p, pathp, depth, data);
  79. if (rc != 0)
  80. break;
  81. } while (1);
  82. return rc;
  83. }
  84. /**
  85. * of_get_flat_dt_root - find the root node in the flat blob
  86. */
  87. unsigned long __init of_get_flat_dt_root(void)
  88. {
  89. unsigned long p = ((unsigned long)initial_boot_params) +
  90. initial_boot_params->off_dt_struct;
  91. while (*((u32 *)p) == OF_DT_NOP)
  92. p += 4;
  93. BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
  94. p += 4;
  95. return _ALIGN(p + strlen((char *)p) + 1, 4);
  96. }
  97. /**
  98. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  99. *
  100. * This function can be used within scan_flattened_dt callback to get
  101. * access to properties
  102. */
  103. void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  104. unsigned long *size)
  105. {
  106. unsigned long p = node;
  107. do {
  108. u32 tag = *((u32 *)p);
  109. u32 sz, noff;
  110. const char *nstr;
  111. p += 4;
  112. if (tag == OF_DT_NOP)
  113. continue;
  114. if (tag != OF_DT_PROP)
  115. return NULL;
  116. sz = *((u32 *)p);
  117. noff = *((u32 *)(p + 4));
  118. p += 8;
  119. if (initial_boot_params->version < 0x10)
  120. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  121. nstr = find_flat_dt_string(noff);
  122. if (nstr == NULL) {
  123. pr_warning("Can't find property index name !\n");
  124. return NULL;
  125. }
  126. if (strcmp(name, nstr) == 0) {
  127. if (size)
  128. *size = sz;
  129. return (void *)p;
  130. }
  131. p += sz;
  132. p = _ALIGN(p, 4);
  133. } while (1);
  134. }
  135. /**
  136. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  137. * @node: node to test
  138. * @compat: compatible string to compare with compatible list.
  139. */
  140. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  141. {
  142. const char *cp;
  143. unsigned long cplen, l;
  144. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  145. if (cp == NULL)
  146. return 0;
  147. while (cplen > 0) {
  148. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  149. return 1;
  150. l = strlen(cp) + 1;
  151. cp += l;
  152. cplen -= l;
  153. }
  154. return 0;
  155. }
  156. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  157. unsigned long align)
  158. {
  159. void *res;
  160. *mem = _ALIGN(*mem, align);
  161. res = (void *)*mem;
  162. *mem += size;
  163. return res;
  164. }
  165. /**
  166. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  167. * @p: pointer to node in flat tree
  168. * @dad: Parent struct device_node
  169. * @allnextpp: pointer to ->allnext from last allocated device_node
  170. * @fpsize: Size of the node path up at the current depth.
  171. */
  172. unsigned long __init unflatten_dt_node(unsigned long mem,
  173. unsigned long *p,
  174. struct device_node *dad,
  175. struct device_node ***allnextpp,
  176. unsigned long fpsize)
  177. {
  178. struct device_node *np;
  179. struct property *pp, **prev_pp = NULL;
  180. char *pathp;
  181. u32 tag;
  182. unsigned int l, allocl;
  183. int has_name = 0;
  184. int new_format = 0;
  185. tag = *((u32 *)(*p));
  186. if (tag != OF_DT_BEGIN_NODE) {
  187. pr_err("Weird tag at start of node: %x\n", tag);
  188. return mem;
  189. }
  190. *p += 4;
  191. pathp = (char *)*p;
  192. l = allocl = strlen(pathp) + 1;
  193. *p = _ALIGN(*p + l, 4);
  194. /* version 0x10 has a more compact unit name here instead of the full
  195. * path. we accumulate the full path size using "fpsize", we'll rebuild
  196. * it later. We detect this because the first character of the name is
  197. * not '/'.
  198. */
  199. if ((*pathp) != '/') {
  200. new_format = 1;
  201. if (fpsize == 0) {
  202. /* root node: special case. fpsize accounts for path
  203. * plus terminating zero. root node only has '/', so
  204. * fpsize should be 2, but we want to avoid the first
  205. * level nodes to have two '/' so we use fpsize 1 here
  206. */
  207. fpsize = 1;
  208. allocl = 2;
  209. } else {
  210. /* account for '/' and path size minus terminal 0
  211. * already in 'l'
  212. */
  213. fpsize += l;
  214. allocl = fpsize;
  215. }
  216. }
  217. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  218. __alignof__(struct device_node));
  219. if (allnextpp) {
  220. memset(np, 0, sizeof(*np));
  221. np->full_name = ((char *)np) + sizeof(struct device_node);
  222. if (new_format) {
  223. char *fn = np->full_name;
  224. /* rebuild full path for new format */
  225. if (dad && dad->parent) {
  226. strcpy(fn, dad->full_name);
  227. #ifdef DEBUG
  228. if ((strlen(fn) + l + 1) != allocl) {
  229. pr_debug("%s: p: %d, l: %d, a: %d\n",
  230. pathp, (int)strlen(fn),
  231. l, allocl);
  232. }
  233. #endif
  234. fn += strlen(fn);
  235. }
  236. *(fn++) = '/';
  237. memcpy(fn, pathp, l);
  238. } else
  239. memcpy(np->full_name, pathp, l);
  240. prev_pp = &np->properties;
  241. **allnextpp = np;
  242. *allnextpp = &np->allnext;
  243. if (dad != NULL) {
  244. np->parent = dad;
  245. /* we temporarily use the next field as `last_child'*/
  246. if (dad->next == NULL)
  247. dad->child = np;
  248. else
  249. dad->next->sibling = np;
  250. dad->next = np;
  251. }
  252. kref_init(&np->kref);
  253. }
  254. while (1) {
  255. u32 sz, noff;
  256. char *pname;
  257. tag = *((u32 *)(*p));
  258. if (tag == OF_DT_NOP) {
  259. *p += 4;
  260. continue;
  261. }
  262. if (tag != OF_DT_PROP)
  263. break;
  264. *p += 4;
  265. sz = *((u32 *)(*p));
  266. noff = *((u32 *)((*p) + 4));
  267. *p += 8;
  268. if (initial_boot_params->version < 0x10)
  269. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  270. pname = find_flat_dt_string(noff);
  271. if (pname == NULL) {
  272. pr_info("Can't find property name in list !\n");
  273. break;
  274. }
  275. if (strcmp(pname, "name") == 0)
  276. has_name = 1;
  277. l = strlen(pname) + 1;
  278. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  279. __alignof__(struct property));
  280. if (allnextpp) {
  281. if (strcmp(pname, "linux,phandle") == 0) {
  282. np->node = *((u32 *)*p);
  283. if (np->linux_phandle == 0)
  284. np->linux_phandle = np->node;
  285. }
  286. if (strcmp(pname, "ibm,phandle") == 0)
  287. np->linux_phandle = *((u32 *)*p);
  288. pp->name = pname;
  289. pp->length = sz;
  290. pp->value = (void *)*p;
  291. *prev_pp = pp;
  292. prev_pp = &pp->next;
  293. }
  294. *p = _ALIGN((*p) + sz, 4);
  295. }
  296. /* with version 0x10 we may not have the name property, recreate
  297. * it here from the unit name if absent
  298. */
  299. if (!has_name) {
  300. char *p1 = pathp, *ps = pathp, *pa = NULL;
  301. int sz;
  302. while (*p1) {
  303. if ((*p1) == '@')
  304. pa = p1;
  305. if ((*p1) == '/')
  306. ps = p1 + 1;
  307. p1++;
  308. }
  309. if (pa < ps)
  310. pa = p1;
  311. sz = (pa - ps) + 1;
  312. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  313. __alignof__(struct property));
  314. if (allnextpp) {
  315. pp->name = "name";
  316. pp->length = sz;
  317. pp->value = pp + 1;
  318. *prev_pp = pp;
  319. prev_pp = &pp->next;
  320. memcpy(pp->value, ps, sz - 1);
  321. ((char *)pp->value)[sz - 1] = 0;
  322. pr_debug("fixed up name for %s -> %s\n", pathp,
  323. (char *)pp->value);
  324. }
  325. }
  326. if (allnextpp) {
  327. *prev_pp = NULL;
  328. np->name = of_get_property(np, "name", NULL);
  329. np->type = of_get_property(np, "device_type", NULL);
  330. if (!np->name)
  331. np->name = "<NULL>";
  332. if (!np->type)
  333. np->type = "<NULL>";
  334. }
  335. while (tag == OF_DT_BEGIN_NODE) {
  336. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  337. tag = *((u32 *)(*p));
  338. }
  339. if (tag != OF_DT_END_NODE) {
  340. pr_err("Weird tag at end of node: %x\n", tag);
  341. return mem;
  342. }
  343. *p += 4;
  344. return mem;
  345. }
  346. #ifdef CONFIG_BLK_DEV_INITRD
  347. /**
  348. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  349. * @node: reference to node containing initrd location ('chosen')
  350. */
  351. void __init early_init_dt_check_for_initrd(unsigned long node)
  352. {
  353. unsigned long len;
  354. u32 *prop;
  355. pr_debug("Looking for initrd properties... ");
  356. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  357. if (prop) {
  358. initrd_start = (unsigned long)
  359. __va(of_read_ulong(prop, len/4));
  360. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  361. if (prop) {
  362. initrd_end = (unsigned long)
  363. __va(of_read_ulong(prop, len/4));
  364. initrd_below_start_ok = 1;
  365. } else {
  366. initrd_start = 0;
  367. }
  368. }
  369. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
  370. initrd_start, initrd_end);
  371. }
  372. #else
  373. inline void early_init_dt_check_for_initrd(unsigned long node)
  374. {
  375. }
  376. #endif /* CONFIG_BLK_DEV_INITRD */
  377. /**
  378. * early_init_dt_scan_root - fetch the top level address and size cells
  379. */
  380. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  381. int depth, void *data)
  382. {
  383. u32 *prop;
  384. if (depth != 0)
  385. return 0;
  386. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  387. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  388. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  389. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  390. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  391. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  392. /* break now */
  393. return 1;
  394. }
  395. /**
  396. * unflatten_device_tree - create tree of device_nodes from flat blob
  397. *
  398. * unflattens the device-tree passed by the firmware, creating the
  399. * tree of struct device_node. It also fills the "name" and "type"
  400. * pointers of the nodes so the normal device-tree walking functions
  401. * can be used.
  402. */
  403. void __init unflatten_device_tree(void)
  404. {
  405. unsigned long start, mem, size;
  406. struct device_node **allnextp = &allnodes;
  407. pr_debug(" -> unflatten_device_tree()\n");
  408. /* First pass, scan for size */
  409. start = ((unsigned long)initial_boot_params) +
  410. initial_boot_params->off_dt_struct;
  411. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  412. size = (size | 3) + 1;
  413. pr_debug(" size is %lx, allocating...\n", size);
  414. /* Allocate memory for the expanded device tree */
  415. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  416. mem = (unsigned long) __va(mem);
  417. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  418. pr_debug(" unflattening %lx...\n", mem);
  419. /* Second pass, do actual unflattening */
  420. start = ((unsigned long)initial_boot_params) +
  421. initial_boot_params->off_dt_struct;
  422. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  423. if (*((u32 *)start) != OF_DT_END)
  424. pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
  425. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  426. pr_warning("End of tree marker overwritten: %08x\n",
  427. ((u32 *)mem)[size / 4]);
  428. *allnextp = NULL;
  429. /* Get pointer to OF "/chosen" node for use everywhere */
  430. of_chosen = of_find_node_by_path("/chosen");
  431. if (of_chosen == NULL)
  432. of_chosen = of_find_node_by_path("/chosen@0");
  433. pr_debug(" <- unflatten_device_tree()\n");
  434. }