fdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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/initrd.h>
  13. #include <linux/of.h>
  14. #include <linux/of_fdt.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #ifdef CONFIG_PPC
  18. #include <asm/machdep.h>
  19. #endif /* CONFIG_PPC */
  20. #include <asm/page.h>
  21. int __initdata dt_root_addr_cells;
  22. int __initdata dt_root_size_cells;
  23. struct boot_param_header *initial_boot_params;
  24. char *find_flat_dt_string(u32 offset)
  25. {
  26. return ((char *)initial_boot_params) +
  27. be32_to_cpu(initial_boot_params->off_dt_strings) + offset;
  28. }
  29. /**
  30. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  31. * @it: callback function
  32. * @data: context data pointer
  33. *
  34. * This function is used to scan the flattened device-tree, it is
  35. * used to extract the memory information at boot before we can
  36. * unflatten the tree
  37. */
  38. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  39. const char *uname, int depth,
  40. void *data),
  41. void *data)
  42. {
  43. unsigned long p = ((unsigned long)initial_boot_params) +
  44. be32_to_cpu(initial_boot_params->off_dt_struct);
  45. int rc = 0;
  46. int depth = -1;
  47. do {
  48. u32 tag = be32_to_cpup((__be32 *)p);
  49. char *pathp;
  50. p += 4;
  51. if (tag == OF_DT_END_NODE) {
  52. depth--;
  53. continue;
  54. }
  55. if (tag == OF_DT_NOP)
  56. continue;
  57. if (tag == OF_DT_END)
  58. break;
  59. if (tag == OF_DT_PROP) {
  60. u32 sz = be32_to_cpup((__be32 *)p);
  61. p += 8;
  62. if (be32_to_cpu(initial_boot_params->version) < 0x10)
  63. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  64. p += sz;
  65. p = _ALIGN(p, 4);
  66. continue;
  67. }
  68. if (tag != OF_DT_BEGIN_NODE) {
  69. pr_err("Invalid tag %x in flat device tree!\n", tag);
  70. return -EINVAL;
  71. }
  72. depth++;
  73. pathp = (char *)p;
  74. p = _ALIGN(p + strlen(pathp) + 1, 4);
  75. if ((*pathp) == '/') {
  76. char *lp, *np;
  77. for (lp = NULL, np = pathp; *np; np++)
  78. if ((*np) == '/')
  79. lp = np+1;
  80. if (lp != NULL)
  81. pathp = lp;
  82. }
  83. rc = it(p, pathp, depth, data);
  84. if (rc != 0)
  85. break;
  86. } while (1);
  87. return rc;
  88. }
  89. /**
  90. * of_get_flat_dt_root - find the root node in the flat blob
  91. */
  92. unsigned long __init of_get_flat_dt_root(void)
  93. {
  94. unsigned long p = ((unsigned long)initial_boot_params) +
  95. be32_to_cpu(initial_boot_params->off_dt_struct);
  96. while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
  97. p += 4;
  98. BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
  99. p += 4;
  100. return _ALIGN(p + strlen((char *)p) + 1, 4);
  101. }
  102. /**
  103. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  104. *
  105. * This function can be used within scan_flattened_dt callback to get
  106. * access to properties
  107. */
  108. void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  109. unsigned long *size)
  110. {
  111. unsigned long p = node;
  112. do {
  113. u32 tag = be32_to_cpup((__be32 *)p);
  114. u32 sz, noff;
  115. const char *nstr;
  116. p += 4;
  117. if (tag == OF_DT_NOP)
  118. continue;
  119. if (tag != OF_DT_PROP)
  120. return NULL;
  121. sz = be32_to_cpup((__be32 *)p);
  122. noff = be32_to_cpup((__be32 *)(p + 4));
  123. p += 8;
  124. if (be32_to_cpu(initial_boot_params->version) < 0x10)
  125. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  126. nstr = find_flat_dt_string(noff);
  127. if (nstr == NULL) {
  128. pr_warning("Can't find property index name !\n");
  129. return NULL;
  130. }
  131. if (strcmp(name, nstr) == 0) {
  132. if (size)
  133. *size = sz;
  134. return (void *)p;
  135. }
  136. p += sz;
  137. p = _ALIGN(p, 4);
  138. } while (1);
  139. }
  140. /**
  141. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  142. * @node: node to test
  143. * @compat: compatible string to compare with compatible list.
  144. */
  145. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  146. {
  147. const char *cp;
  148. unsigned long cplen, l;
  149. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  150. if (cp == NULL)
  151. return 0;
  152. while (cplen > 0) {
  153. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  154. return 1;
  155. l = strlen(cp) + 1;
  156. cp += l;
  157. cplen -= l;
  158. }
  159. return 0;
  160. }
  161. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  162. unsigned long align)
  163. {
  164. void *res;
  165. *mem = _ALIGN(*mem, align);
  166. res = (void *)*mem;
  167. *mem += size;
  168. return res;
  169. }
  170. /**
  171. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  172. * @p: pointer to node in flat tree
  173. * @dad: Parent struct device_node
  174. * @allnextpp: pointer to ->allnext from last allocated device_node
  175. * @fpsize: Size of the node path up at the current depth.
  176. */
  177. unsigned long __init unflatten_dt_node(unsigned long mem,
  178. unsigned long *p,
  179. struct device_node *dad,
  180. struct device_node ***allnextpp,
  181. unsigned long fpsize)
  182. {
  183. struct device_node *np;
  184. struct property *pp, **prev_pp = NULL;
  185. char *pathp;
  186. u32 tag;
  187. unsigned int l, allocl;
  188. int has_name = 0;
  189. int new_format = 0;
  190. tag = be32_to_cpup((__be32 *)(*p));
  191. if (tag != OF_DT_BEGIN_NODE) {
  192. pr_err("Weird tag at start of node: %x\n", tag);
  193. return mem;
  194. }
  195. *p += 4;
  196. pathp = (char *)*p;
  197. l = allocl = strlen(pathp) + 1;
  198. *p = _ALIGN(*p + l, 4);
  199. /* version 0x10 has a more compact unit name here instead of the full
  200. * path. we accumulate the full path size using "fpsize", we'll rebuild
  201. * it later. We detect this because the first character of the name is
  202. * not '/'.
  203. */
  204. if ((*pathp) != '/') {
  205. new_format = 1;
  206. if (fpsize == 0) {
  207. /* root node: special case. fpsize accounts for path
  208. * plus terminating zero. root node only has '/', so
  209. * fpsize should be 2, but we want to avoid the first
  210. * level nodes to have two '/' so we use fpsize 1 here
  211. */
  212. fpsize = 1;
  213. allocl = 2;
  214. } else {
  215. /* account for '/' and path size minus terminal 0
  216. * already in 'l'
  217. */
  218. fpsize += l;
  219. allocl = fpsize;
  220. }
  221. }
  222. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  223. __alignof__(struct device_node));
  224. if (allnextpp) {
  225. memset(np, 0, sizeof(*np));
  226. np->full_name = ((char *)np) + sizeof(struct device_node);
  227. if (new_format) {
  228. char *fn = np->full_name;
  229. /* rebuild full path for new format */
  230. if (dad && dad->parent) {
  231. strcpy(fn, dad->full_name);
  232. #ifdef DEBUG
  233. if ((strlen(fn) + l + 1) != allocl) {
  234. pr_debug("%s: p: %d, l: %d, a: %d\n",
  235. pathp, (int)strlen(fn),
  236. l, allocl);
  237. }
  238. #endif
  239. fn += strlen(fn);
  240. }
  241. *(fn++) = '/';
  242. memcpy(fn, pathp, l);
  243. } else
  244. memcpy(np->full_name, pathp, l);
  245. prev_pp = &np->properties;
  246. **allnextpp = np;
  247. *allnextpp = &np->allnext;
  248. if (dad != NULL) {
  249. np->parent = dad;
  250. /* we temporarily use the next field as `last_child'*/
  251. if (dad->next == NULL)
  252. dad->child = np;
  253. else
  254. dad->next->sibling = np;
  255. dad->next = np;
  256. }
  257. kref_init(&np->kref);
  258. }
  259. while (1) {
  260. u32 sz, noff;
  261. char *pname;
  262. tag = be32_to_cpup((__be32 *)(*p));
  263. if (tag == OF_DT_NOP) {
  264. *p += 4;
  265. continue;
  266. }
  267. if (tag != OF_DT_PROP)
  268. break;
  269. *p += 4;
  270. sz = be32_to_cpup((__be32 *)(*p));
  271. noff = be32_to_cpup((__be32 *)((*p) + 4));
  272. *p += 8;
  273. if (be32_to_cpu(initial_boot_params->version) < 0x10)
  274. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  275. pname = find_flat_dt_string(noff);
  276. if (pname == NULL) {
  277. pr_info("Can't find property name in list !\n");
  278. break;
  279. }
  280. if (strcmp(pname, "name") == 0)
  281. has_name = 1;
  282. l = strlen(pname) + 1;
  283. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  284. __alignof__(struct property));
  285. if (allnextpp) {
  286. /* We accept flattened tree phandles either in
  287. * ePAPR-style "phandle" properties, or the
  288. * legacy "linux,phandle" properties. If both
  289. * appear and have different values, things
  290. * will get weird. Don't do that. */
  291. if ((strcmp(pname, "phandle") == 0) ||
  292. (strcmp(pname, "linux,phandle") == 0)) {
  293. if (np->phandle == 0)
  294. np->phandle = *((u32 *)*p);
  295. }
  296. /* And we process the "ibm,phandle" property
  297. * used in pSeries dynamic device tree
  298. * stuff */
  299. if (strcmp(pname, "ibm,phandle") == 0)
  300. np->phandle = *((u32 *)*p);
  301. pp->name = pname;
  302. pp->length = sz;
  303. pp->value = (void *)*p;
  304. *prev_pp = pp;
  305. prev_pp = &pp->next;
  306. }
  307. *p = _ALIGN((*p) + sz, 4);
  308. }
  309. /* with version 0x10 we may not have the name property, recreate
  310. * it here from the unit name if absent
  311. */
  312. if (!has_name) {
  313. char *p1 = pathp, *ps = pathp, *pa = NULL;
  314. int sz;
  315. while (*p1) {
  316. if ((*p1) == '@')
  317. pa = p1;
  318. if ((*p1) == '/')
  319. ps = p1 + 1;
  320. p1++;
  321. }
  322. if (pa < ps)
  323. pa = p1;
  324. sz = (pa - ps) + 1;
  325. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  326. __alignof__(struct property));
  327. if (allnextpp) {
  328. pp->name = "name";
  329. pp->length = sz;
  330. pp->value = pp + 1;
  331. *prev_pp = pp;
  332. prev_pp = &pp->next;
  333. memcpy(pp->value, ps, sz - 1);
  334. ((char *)pp->value)[sz - 1] = 0;
  335. pr_debug("fixed up name for %s -> %s\n", pathp,
  336. (char *)pp->value);
  337. }
  338. }
  339. if (allnextpp) {
  340. *prev_pp = NULL;
  341. np->name = of_get_property(np, "name", NULL);
  342. np->type = of_get_property(np, "device_type", NULL);
  343. if (!np->name)
  344. np->name = "<NULL>";
  345. if (!np->type)
  346. np->type = "<NULL>";
  347. }
  348. while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
  349. if (tag == OF_DT_NOP)
  350. *p += 4;
  351. else
  352. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  353. tag = be32_to_cpup((__be32 *)(*p));
  354. }
  355. if (tag != OF_DT_END_NODE) {
  356. pr_err("Weird tag at end of node: %x\n", tag);
  357. return mem;
  358. }
  359. *p += 4;
  360. return mem;
  361. }
  362. #ifdef CONFIG_BLK_DEV_INITRD
  363. /**
  364. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  365. * @node: reference to node containing initrd location ('chosen')
  366. */
  367. void __init early_init_dt_check_for_initrd(unsigned long node)
  368. {
  369. unsigned long start, end, len;
  370. __be32 *prop;
  371. pr_debug("Looking for initrd properties... ");
  372. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  373. if (!prop)
  374. return;
  375. start = of_read_ulong(prop, len/4);
  376. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  377. if (!prop)
  378. return;
  379. end = of_read_ulong(prop, len/4);
  380. early_init_dt_setup_initrd_arch(start, end);
  381. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", start, end);
  382. }
  383. #else
  384. inline void early_init_dt_check_for_initrd(unsigned long node)
  385. {
  386. }
  387. #endif /* CONFIG_BLK_DEV_INITRD */
  388. /**
  389. * early_init_dt_scan_root - fetch the top level address and size cells
  390. */
  391. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  392. int depth, void *data)
  393. {
  394. __be32 *prop;
  395. if (depth != 0)
  396. return 0;
  397. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  398. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  399. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  400. if (prop)
  401. dt_root_size_cells = be32_to_cpup(prop);
  402. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  403. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  404. if (prop)
  405. dt_root_addr_cells = be32_to_cpup(prop);
  406. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  407. /* break now */
  408. return 1;
  409. }
  410. u64 __init dt_mem_next_cell(int s, __be32 **cellp)
  411. {
  412. __be32 *p = *cellp;
  413. *cellp = p + s;
  414. return of_read_number(p, s);
  415. }
  416. /**
  417. * early_init_dt_scan_memory - Look for an parse memory nodes
  418. */
  419. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  420. int depth, void *data)
  421. {
  422. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  423. __be32 *reg, *endp;
  424. unsigned long l;
  425. /* We are scanning "memory" nodes only */
  426. if (type == NULL) {
  427. /*
  428. * The longtrail doesn't have a device_type on the
  429. * /memory node, so look for the node called /memory@0.
  430. */
  431. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  432. return 0;
  433. } else if (strcmp(type, "memory") != 0)
  434. return 0;
  435. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  436. if (reg == NULL)
  437. reg = of_get_flat_dt_prop(node, "reg", &l);
  438. if (reg == NULL)
  439. return 0;
  440. endp = reg + (l / sizeof(__be32));
  441. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  442. uname, l, reg[0], reg[1], reg[2], reg[3]);
  443. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  444. u64 base, size;
  445. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  446. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  447. if (size == 0)
  448. continue;
  449. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  450. (unsigned long long)size);
  451. early_init_dt_add_memory_arch(base, size);
  452. }
  453. return 0;
  454. }
  455. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  456. int depth, void *data)
  457. {
  458. unsigned long l;
  459. char *p;
  460. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  461. if (depth != 1 ||
  462. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  463. return 0;
  464. early_init_dt_check_for_initrd(node);
  465. /* Retreive command line */
  466. p = of_get_flat_dt_prop(node, "bootargs", &l);
  467. if (p != NULL && l > 0)
  468. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  469. #ifdef CONFIG_CMDLINE
  470. #ifndef CONFIG_CMDLINE_FORCE
  471. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  472. #endif
  473. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  474. #endif /* CONFIG_CMDLINE */
  475. early_init_dt_scan_chosen_arch(node);
  476. pr_debug("Command line is: %s\n", cmd_line);
  477. /* break now */
  478. return 1;
  479. }
  480. /**
  481. * unflatten_device_tree - create tree of device_nodes from flat blob
  482. *
  483. * unflattens the device-tree passed by the firmware, creating the
  484. * tree of struct device_node. It also fills the "name" and "type"
  485. * pointers of the nodes so the normal device-tree walking functions
  486. * can be used.
  487. */
  488. void __init unflatten_device_tree(void)
  489. {
  490. unsigned long start, mem, size;
  491. struct device_node **allnextp = &allnodes;
  492. pr_debug(" -> unflatten_device_tree()\n");
  493. if (!initial_boot_params) {
  494. pr_debug("No device tree pointer\n");
  495. return;
  496. }
  497. pr_debug("Unflattening device tree:\n");
  498. pr_debug("magic: %08x\n", be32_to_cpu(initial_boot_params->magic));
  499. pr_debug("size: %08x\n", be32_to_cpu(initial_boot_params->totalsize));
  500. pr_debug("version: %08x\n", be32_to_cpu(initial_boot_params->version));
  501. if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
  502. pr_err("Invalid device tree blob header\n");
  503. return;
  504. }
  505. /* First pass, scan for size */
  506. start = ((unsigned long)initial_boot_params) +
  507. be32_to_cpu(initial_boot_params->off_dt_struct);
  508. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  509. size = (size | 3) + 1;
  510. pr_debug(" size is %lx, allocating...\n", size);
  511. /* Allocate memory for the expanded device tree */
  512. mem = early_init_dt_alloc_memory_arch(size + 4,
  513. __alignof__(struct device_node));
  514. mem = (unsigned long) __va(mem);
  515. ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
  516. pr_debug(" unflattening %lx...\n", mem);
  517. /* Second pass, do actual unflattening */
  518. start = ((unsigned long)initial_boot_params) +
  519. be32_to_cpu(initial_boot_params->off_dt_struct);
  520. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  521. if (be32_to_cpup((__be32 *)start) != OF_DT_END)
  522. pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
  523. if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
  524. pr_warning("End of tree marker overwritten: %08x\n",
  525. be32_to_cpu(((__be32 *)mem)[size / 4]));
  526. *allnextp = NULL;
  527. /* Get pointer to OF "/chosen" node for use everywhere */
  528. of_chosen = of_find_node_by_path("/chosen");
  529. if (of_chosen == NULL)
  530. of_chosen = of_find_node_by_path("/chosen@0");
  531. pr_debug(" <- unflatten_device_tree()\n");
  532. }