fdt.c 15 KB

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