fdt.c 16 KB

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