fdt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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/memblock.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_fdt.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
  21. #ifdef CONFIG_PPC
  22. #include <asm/machdep.h>
  23. #endif /* CONFIG_PPC */
  24. #include <asm/page.h>
  25. char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
  26. {
  27. return ((char *)blob) +
  28. be32_to_cpu(blob->off_dt_strings) + offset;
  29. }
  30. /**
  31. * of_fdt_get_property - Given a node in the given flat blob, return
  32. * the property ptr
  33. */
  34. void *of_fdt_get_property(struct boot_param_header *blob,
  35. unsigned long node, const char *name,
  36. unsigned long *size)
  37. {
  38. unsigned long p = node;
  39. do {
  40. u32 tag = be32_to_cpup((__be32 *)p);
  41. u32 sz, noff;
  42. const char *nstr;
  43. p += 4;
  44. if (tag == OF_DT_NOP)
  45. continue;
  46. if (tag != OF_DT_PROP)
  47. return NULL;
  48. sz = be32_to_cpup((__be32 *)p);
  49. noff = be32_to_cpup((__be32 *)(p + 4));
  50. p += 8;
  51. if (be32_to_cpu(blob->version) < 0x10)
  52. p = ALIGN(p, sz >= 8 ? 8 : 4);
  53. nstr = of_fdt_get_string(blob, noff);
  54. if (nstr == NULL) {
  55. pr_warning("Can't find property index name !\n");
  56. return NULL;
  57. }
  58. if (strcmp(name, nstr) == 0) {
  59. if (size)
  60. *size = sz;
  61. return (void *)p;
  62. }
  63. p += sz;
  64. p = ALIGN(p, 4);
  65. } while (1);
  66. }
  67. /**
  68. * of_fdt_is_compatible - Return true if given node from the given blob has
  69. * compat in its compatible list
  70. * @blob: A device tree blob
  71. * @node: node to test
  72. * @compat: compatible string to compare with compatible list.
  73. *
  74. * On match, returns a non-zero value with smaller values returned for more
  75. * specific compatible values.
  76. */
  77. int of_fdt_is_compatible(struct boot_param_header *blob,
  78. unsigned long node, const char *compat)
  79. {
  80. const char *cp;
  81. unsigned long cplen, l, score = 0;
  82. cp = of_fdt_get_property(blob, node, "compatible", &cplen);
  83. if (cp == NULL)
  84. return 0;
  85. while (cplen > 0) {
  86. score++;
  87. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  88. return score;
  89. l = strlen(cp) + 1;
  90. cp += l;
  91. cplen -= l;
  92. }
  93. return 0;
  94. }
  95. /**
  96. * of_fdt_match - Return true if node matches a list of compatible values
  97. */
  98. int of_fdt_match(struct boot_param_header *blob, unsigned long node,
  99. const char *const *compat)
  100. {
  101. unsigned int tmp, score = 0;
  102. if (!compat)
  103. return 0;
  104. while (*compat) {
  105. tmp = of_fdt_is_compatible(blob, node, *compat);
  106. if (tmp && (score == 0 || (tmp < score)))
  107. score = tmp;
  108. compat++;
  109. }
  110. return score;
  111. }
  112. static void *unflatten_dt_alloc(void **mem, unsigned long size,
  113. unsigned long align)
  114. {
  115. void *res;
  116. *mem = PTR_ALIGN(*mem, align);
  117. res = *mem;
  118. *mem += size;
  119. return res;
  120. }
  121. /**
  122. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  123. * @blob: The parent device tree blob
  124. * @mem: Memory chunk to use for allocating device nodes and properties
  125. * @p: pointer to node in flat tree
  126. * @dad: Parent struct device_node
  127. * @allnextpp: pointer to ->allnext from last allocated device_node
  128. * @fpsize: Size of the node path up at the current depth.
  129. */
  130. static void * unflatten_dt_node(struct boot_param_header *blob,
  131. void *mem,
  132. void **p,
  133. struct device_node *dad,
  134. struct device_node ***allnextpp,
  135. unsigned long fpsize)
  136. {
  137. struct device_node *np;
  138. struct property *pp, **prev_pp = NULL;
  139. char *pathp;
  140. u32 tag;
  141. unsigned int l, allocl;
  142. int has_name = 0;
  143. int new_format = 0;
  144. tag = be32_to_cpup(*p);
  145. if (tag != OF_DT_BEGIN_NODE) {
  146. pr_err("Weird tag at start of node: %x\n", tag);
  147. return mem;
  148. }
  149. *p += 4;
  150. pathp = *p;
  151. l = allocl = strlen(pathp) + 1;
  152. *p = PTR_ALIGN(*p + l, 4);
  153. /* version 0x10 has a more compact unit name here instead of the full
  154. * path. we accumulate the full path size using "fpsize", we'll rebuild
  155. * it later. We detect this because the first character of the name is
  156. * not '/'.
  157. */
  158. if ((*pathp) != '/') {
  159. new_format = 1;
  160. if (fpsize == 0) {
  161. /* root node: special case. fpsize accounts for path
  162. * plus terminating zero. root node only has '/', so
  163. * fpsize should be 2, but we want to avoid the first
  164. * level nodes to have two '/' so we use fpsize 1 here
  165. */
  166. fpsize = 1;
  167. allocl = 2;
  168. l = 1;
  169. *pathp = '\0';
  170. } else {
  171. /* account for '/' and path size minus terminal 0
  172. * already in 'l'
  173. */
  174. fpsize += l;
  175. allocl = fpsize;
  176. }
  177. }
  178. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  179. __alignof__(struct device_node));
  180. if (allnextpp) {
  181. char *fn;
  182. np->full_name = fn = ((char *)np) + sizeof(*np);
  183. if (new_format) {
  184. /* rebuild full path for new format */
  185. if (dad && dad->parent) {
  186. strcpy(fn, dad->full_name);
  187. #ifdef DEBUG
  188. if ((strlen(fn) + l + 1) != allocl) {
  189. pr_debug("%s: p: %d, l: %d, a: %d\n",
  190. pathp, (int)strlen(fn),
  191. l, allocl);
  192. }
  193. #endif
  194. fn += strlen(fn);
  195. }
  196. *(fn++) = '/';
  197. }
  198. memcpy(fn, pathp, l);
  199. prev_pp = &np->properties;
  200. **allnextpp = np;
  201. *allnextpp = &np->allnext;
  202. if (dad != NULL) {
  203. np->parent = dad;
  204. /* we temporarily use the next field as `last_child'*/
  205. if (dad->next == NULL)
  206. dad->child = np;
  207. else
  208. dad->next->sibling = np;
  209. dad->next = np;
  210. }
  211. kref_init(&np->kref);
  212. }
  213. /* process properties */
  214. while (1) {
  215. u32 sz, noff;
  216. char *pname;
  217. tag = be32_to_cpup(*p);
  218. if (tag == OF_DT_NOP) {
  219. *p += 4;
  220. continue;
  221. }
  222. if (tag != OF_DT_PROP)
  223. break;
  224. *p += 4;
  225. sz = be32_to_cpup(*p);
  226. noff = be32_to_cpup(*p + 4);
  227. *p += 8;
  228. if (be32_to_cpu(blob->version) < 0x10)
  229. *p = PTR_ALIGN(*p, sz >= 8 ? 8 : 4);
  230. pname = of_fdt_get_string(blob, noff);
  231. if (pname == NULL) {
  232. pr_info("Can't find property name in list !\n");
  233. break;
  234. }
  235. if (strcmp(pname, "name") == 0)
  236. has_name = 1;
  237. l = strlen(pname) + 1;
  238. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  239. __alignof__(struct property));
  240. if (allnextpp) {
  241. /* We accept flattened tree phandles either in
  242. * ePAPR-style "phandle" properties, or the
  243. * legacy "linux,phandle" properties. If both
  244. * appear and have different values, things
  245. * will get weird. Don't do that. */
  246. if ((strcmp(pname, "phandle") == 0) ||
  247. (strcmp(pname, "linux,phandle") == 0)) {
  248. if (np->phandle == 0)
  249. np->phandle = be32_to_cpup((__be32*)*p);
  250. }
  251. /* And we process the "ibm,phandle" property
  252. * used in pSeries dynamic device tree
  253. * stuff */
  254. if (strcmp(pname, "ibm,phandle") == 0)
  255. np->phandle = be32_to_cpup((__be32 *)*p);
  256. pp->name = pname;
  257. pp->length = sz;
  258. pp->value = *p;
  259. *prev_pp = pp;
  260. prev_pp = &pp->next;
  261. }
  262. *p = PTR_ALIGN((*p) + sz, 4);
  263. }
  264. /* with version 0x10 we may not have the name property, recreate
  265. * it here from the unit name if absent
  266. */
  267. if (!has_name) {
  268. char *p1 = pathp, *ps = pathp, *pa = NULL;
  269. int sz;
  270. while (*p1) {
  271. if ((*p1) == '@')
  272. pa = p1;
  273. if ((*p1) == '/')
  274. ps = p1 + 1;
  275. p1++;
  276. }
  277. if (pa < ps)
  278. pa = p1;
  279. sz = (pa - ps) + 1;
  280. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  281. __alignof__(struct property));
  282. if (allnextpp) {
  283. pp->name = "name";
  284. pp->length = sz;
  285. pp->value = pp + 1;
  286. *prev_pp = pp;
  287. prev_pp = &pp->next;
  288. memcpy(pp->value, ps, sz - 1);
  289. ((char *)pp->value)[sz - 1] = 0;
  290. pr_debug("fixed up name for %s -> %s\n", pathp,
  291. (char *)pp->value);
  292. }
  293. }
  294. if (allnextpp) {
  295. *prev_pp = NULL;
  296. np->name = of_get_property(np, "name", NULL);
  297. np->type = of_get_property(np, "device_type", NULL);
  298. if (!np->name)
  299. np->name = "<NULL>";
  300. if (!np->type)
  301. np->type = "<NULL>";
  302. }
  303. while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
  304. if (tag == OF_DT_NOP)
  305. *p += 4;
  306. else
  307. mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
  308. fpsize);
  309. tag = be32_to_cpup(*p);
  310. }
  311. if (tag != OF_DT_END_NODE) {
  312. pr_err("Weird tag at end of node: %x\n", tag);
  313. return mem;
  314. }
  315. *p += 4;
  316. return mem;
  317. }
  318. /**
  319. * __unflatten_device_tree - create tree of device_nodes from flat blob
  320. *
  321. * unflattens a device-tree, creating the
  322. * tree of struct device_node. It also fills the "name" and "type"
  323. * pointers of the nodes so the normal device-tree walking functions
  324. * can be used.
  325. * @blob: The blob to expand
  326. * @mynodes: The device_node tree created by the call
  327. * @dt_alloc: An allocator that provides a virtual address to memory
  328. * for the resulting tree
  329. */
  330. static void __unflatten_device_tree(struct boot_param_header *blob,
  331. struct device_node **mynodes,
  332. void * (*dt_alloc)(u64 size, u64 align))
  333. {
  334. unsigned long size;
  335. void *start, *mem;
  336. struct device_node **allnextp = mynodes;
  337. pr_debug(" -> unflatten_device_tree()\n");
  338. if (!blob) {
  339. pr_debug("No device tree pointer\n");
  340. return;
  341. }
  342. pr_debug("Unflattening device tree:\n");
  343. pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
  344. pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
  345. pr_debug("version: %08x\n", be32_to_cpu(blob->version));
  346. if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
  347. pr_err("Invalid device tree blob header\n");
  348. return;
  349. }
  350. /* First pass, scan for size */
  351. start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
  352. size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
  353. size = ALIGN(size, 4);
  354. pr_debug(" size is %lx, allocating...\n", size);
  355. /* Allocate memory for the expanded device tree */
  356. mem = dt_alloc(size + 4, __alignof__(struct device_node));
  357. memset(mem, 0, size);
  358. *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
  359. pr_debug(" unflattening %p...\n", mem);
  360. /* Second pass, do actual unflattening */
  361. start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
  362. unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
  363. if (be32_to_cpup(start) != OF_DT_END)
  364. pr_warning("Weird tag at end of tree: %08x\n", be32_to_cpup(start));
  365. if (be32_to_cpup(mem + size) != 0xdeadbeef)
  366. pr_warning("End of tree marker overwritten: %08x\n",
  367. be32_to_cpup(mem + size));
  368. *allnextp = NULL;
  369. pr_debug(" <- unflatten_device_tree()\n");
  370. }
  371. static void *kernel_tree_alloc(u64 size, u64 align)
  372. {
  373. return kzalloc(size, GFP_KERNEL);
  374. }
  375. /**
  376. * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
  377. *
  378. * unflattens the device-tree passed by the firmware, creating the
  379. * tree of struct device_node. It also fills the "name" and "type"
  380. * pointers of the nodes so the normal device-tree walking functions
  381. * can be used.
  382. */
  383. void of_fdt_unflatten_tree(unsigned long *blob,
  384. struct device_node **mynodes)
  385. {
  386. struct boot_param_header *device_tree =
  387. (struct boot_param_header *)blob;
  388. __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
  389. }
  390. EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
  391. /* Everything below here references initial_boot_params directly. */
  392. int __initdata dt_root_addr_cells;
  393. int __initdata dt_root_size_cells;
  394. struct boot_param_header *initial_boot_params;
  395. #ifdef CONFIG_OF_EARLY_FLATTREE
  396. /**
  397. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  398. * @it: callback function
  399. * @data: context data pointer
  400. *
  401. * This function is used to scan the flattened device-tree, it is
  402. * used to extract the memory information at boot before we can
  403. * unflatten the tree
  404. */
  405. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  406. const char *uname, int depth,
  407. void *data),
  408. void *data)
  409. {
  410. unsigned long p = ((unsigned long)initial_boot_params) +
  411. be32_to_cpu(initial_boot_params->off_dt_struct);
  412. int rc = 0;
  413. int depth = -1;
  414. do {
  415. u32 tag = be32_to_cpup((__be32 *)p);
  416. const char *pathp;
  417. p += 4;
  418. if (tag == OF_DT_END_NODE) {
  419. depth--;
  420. continue;
  421. }
  422. if (tag == OF_DT_NOP)
  423. continue;
  424. if (tag == OF_DT_END)
  425. break;
  426. if (tag == OF_DT_PROP) {
  427. u32 sz = be32_to_cpup((__be32 *)p);
  428. p += 8;
  429. if (be32_to_cpu(initial_boot_params->version) < 0x10)
  430. p = ALIGN(p, sz >= 8 ? 8 : 4);
  431. p += sz;
  432. p = ALIGN(p, 4);
  433. continue;
  434. }
  435. if (tag != OF_DT_BEGIN_NODE) {
  436. pr_err("Invalid tag %x in flat device tree!\n", tag);
  437. return -EINVAL;
  438. }
  439. depth++;
  440. pathp = (char *)p;
  441. p = ALIGN(p + strlen(pathp) + 1, 4);
  442. if (*pathp == '/')
  443. pathp = kbasename(pathp);
  444. rc = it(p, pathp, depth, data);
  445. if (rc != 0)
  446. break;
  447. } while (1);
  448. return rc;
  449. }
  450. /**
  451. * of_get_flat_dt_root - find the root node in the flat blob
  452. */
  453. unsigned long __init of_get_flat_dt_root(void)
  454. {
  455. unsigned long p = ((unsigned long)initial_boot_params) +
  456. be32_to_cpu(initial_boot_params->off_dt_struct);
  457. while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
  458. p += 4;
  459. BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
  460. p += 4;
  461. return ALIGN(p + strlen((char *)p) + 1, 4);
  462. }
  463. /**
  464. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  465. *
  466. * This function can be used within scan_flattened_dt callback to get
  467. * access to properties
  468. */
  469. void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  470. unsigned long *size)
  471. {
  472. return of_fdt_get_property(initial_boot_params, node, name, size);
  473. }
  474. /**
  475. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  476. * @node: node to test
  477. * @compat: compatible string to compare with compatible list.
  478. */
  479. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  480. {
  481. return of_fdt_is_compatible(initial_boot_params, node, compat);
  482. }
  483. /**
  484. * of_flat_dt_match - Return true if node matches a list of compatible values
  485. */
  486. int __init of_flat_dt_match(unsigned long node, const char *const *compat)
  487. {
  488. return of_fdt_match(initial_boot_params, node, compat);
  489. }
  490. struct fdt_scan_status {
  491. const char *name;
  492. int namelen;
  493. int depth;
  494. int found;
  495. int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
  496. void *data;
  497. };
  498. /**
  499. * fdt_scan_node_by_path - iterator for of_scan_flat_dt_by_path function
  500. */
  501. static int __init fdt_scan_node_by_path(unsigned long node, const char *uname,
  502. int depth, void *data)
  503. {
  504. struct fdt_scan_status *st = data;
  505. /*
  506. * if scan at the requested fdt node has been completed,
  507. * return -ENXIO to abort further scanning
  508. */
  509. if (depth <= st->depth)
  510. return -ENXIO;
  511. /* requested fdt node has been found, so call iterator function */
  512. if (st->found)
  513. return st->iterator(node, uname, depth, st->data);
  514. /* check if scanning automata is entering next level of fdt nodes */
  515. if (depth == st->depth + 1 &&
  516. strncmp(st->name, uname, st->namelen) == 0 &&
  517. uname[st->namelen] == 0) {
  518. st->depth += 1;
  519. if (st->name[st->namelen] == 0) {
  520. st->found = 1;
  521. } else {
  522. const char *next = st->name + st->namelen + 1;
  523. st->name = next;
  524. st->namelen = strcspn(next, "/");
  525. }
  526. return 0;
  527. }
  528. /* scan next fdt node */
  529. return 0;
  530. }
  531. /**
  532. * of_scan_flat_dt_by_path - scan flattened tree blob and call callback on each
  533. * child of the given path.
  534. * @path: path to start searching for children
  535. * @it: callback function
  536. * @data: context data pointer
  537. *
  538. * This function is used to scan the flattened device-tree starting from the
  539. * node given by path. It is used to extract information (like reserved
  540. * memory), which is required on ealy boot before we can unflatten the tree.
  541. */
  542. int __init of_scan_flat_dt_by_path(const char *path,
  543. int (*it)(unsigned long node, const char *name, int depth, void *data),
  544. void *data)
  545. {
  546. struct fdt_scan_status st = {path, 0, -1, 0, it, data};
  547. int ret = 0;
  548. if (initial_boot_params)
  549. ret = of_scan_flat_dt(fdt_scan_node_by_path, &st);
  550. if (!st.found)
  551. return -ENOENT;
  552. else if (ret == -ENXIO) /* scan has been completed */
  553. return 0;
  554. else
  555. return ret;
  556. }
  557. const char * __init of_flat_dt_get_machine_name(void)
  558. {
  559. const char *name;
  560. unsigned long dt_root = of_get_flat_dt_root();
  561. name = of_get_flat_dt_prop(dt_root, "model", NULL);
  562. if (!name)
  563. name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  564. return name;
  565. }
  566. /**
  567. * of_flat_dt_match_machine - Iterate match tables to find matching machine.
  568. *
  569. * @default_match: A machine specific ptr to return in case of no match.
  570. * @get_next_compat: callback function to return next compatible match table.
  571. *
  572. * Iterate through machine match tables to find the best match for the machine
  573. * compatible string in the FDT.
  574. */
  575. const void * __init of_flat_dt_match_machine(const void *default_match,
  576. const void * (*get_next_compat)(const char * const**))
  577. {
  578. const void *data = NULL;
  579. const void *best_data = default_match;
  580. const char *const *compat;
  581. unsigned long dt_root;
  582. unsigned int best_score = ~1, score = 0;
  583. dt_root = of_get_flat_dt_root();
  584. while ((data = get_next_compat(&compat))) {
  585. score = of_flat_dt_match(dt_root, compat);
  586. if (score > 0 && score < best_score) {
  587. best_data = data;
  588. best_score = score;
  589. }
  590. }
  591. if (!best_data) {
  592. const char *prop;
  593. long size;
  594. pr_err("\n unrecognized device tree list:\n[ ");
  595. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  596. if (prop) {
  597. while (size > 0) {
  598. printk("'%s' ", prop);
  599. size -= strlen(prop) + 1;
  600. prop += strlen(prop) + 1;
  601. }
  602. }
  603. printk("]\n\n");
  604. return NULL;
  605. }
  606. pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
  607. return best_data;
  608. }
  609. #ifdef CONFIG_BLK_DEV_INITRD
  610. /**
  611. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  612. * @node: reference to node containing initrd location ('chosen')
  613. */
  614. static void __init early_init_dt_check_for_initrd(unsigned long node)
  615. {
  616. u64 start, end;
  617. unsigned long len;
  618. __be32 *prop;
  619. pr_debug("Looking for initrd properties... ");
  620. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  621. if (!prop)
  622. return;
  623. start = of_read_number(prop, len/4);
  624. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  625. if (!prop)
  626. return;
  627. end = of_read_number(prop, len/4);
  628. initrd_start = (unsigned long)__va(start);
  629. initrd_end = (unsigned long)__va(end);
  630. initrd_below_start_ok = 1;
  631. pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
  632. (unsigned long long)start, (unsigned long long)end);
  633. }
  634. #else
  635. static inline void early_init_dt_check_for_initrd(unsigned long node)
  636. {
  637. }
  638. #endif /* CONFIG_BLK_DEV_INITRD */
  639. /**
  640. * early_init_dt_scan_root - fetch the top level address and size cells
  641. */
  642. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  643. int depth, void *data)
  644. {
  645. __be32 *prop;
  646. if (depth != 0)
  647. return 0;
  648. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  649. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  650. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  651. if (prop)
  652. dt_root_size_cells = be32_to_cpup(prop);
  653. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  654. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  655. if (prop)
  656. dt_root_addr_cells = be32_to_cpup(prop);
  657. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  658. /* break now */
  659. return 1;
  660. }
  661. u64 __init dt_mem_next_cell(int s, __be32 **cellp)
  662. {
  663. __be32 *p = *cellp;
  664. *cellp = p + s;
  665. return of_read_number(p, s);
  666. }
  667. /**
  668. * early_init_dt_scan_memory - Look for an parse memory nodes
  669. */
  670. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  671. int depth, void *data)
  672. {
  673. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  674. __be32 *reg, *endp;
  675. unsigned long l;
  676. /* We are scanning "memory" nodes only */
  677. if (type == NULL) {
  678. /*
  679. * The longtrail doesn't have a device_type on the
  680. * /memory node, so look for the node called /memory@0.
  681. */
  682. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  683. return 0;
  684. } else if (strcmp(type, "memory") != 0)
  685. return 0;
  686. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  687. if (reg == NULL)
  688. reg = of_get_flat_dt_prop(node, "reg", &l);
  689. if (reg == NULL)
  690. return 0;
  691. endp = reg + (l / sizeof(__be32));
  692. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  693. uname, l, reg[0], reg[1], reg[2], reg[3]);
  694. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  695. u64 base, size;
  696. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  697. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  698. if (size == 0)
  699. continue;
  700. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  701. (unsigned long long)size);
  702. early_init_dt_add_memory_arch(base, size);
  703. }
  704. return 0;
  705. }
  706. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  707. int depth, void *data)
  708. {
  709. unsigned long l;
  710. char *p;
  711. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  712. if (depth != 1 || !data ||
  713. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  714. return 0;
  715. early_init_dt_check_for_initrd(node);
  716. /* Retrieve command line */
  717. p = of_get_flat_dt_prop(node, "bootargs", &l);
  718. if (p != NULL && l > 0)
  719. strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  720. /*
  721. * CONFIG_CMDLINE is meant to be a default in case nothing else
  722. * managed to set the command line, unless CONFIG_CMDLINE_FORCE
  723. * is set in which case we override whatever was found earlier.
  724. */
  725. #ifdef CONFIG_CMDLINE
  726. #ifndef CONFIG_CMDLINE_FORCE
  727. if (!((char *)data)[0])
  728. #endif
  729. strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  730. #endif /* CONFIG_CMDLINE */
  731. pr_debug("Command line is: %s\n", (char*)data);
  732. /* break now */
  733. return 1;
  734. }
  735. #ifdef CONFIG_HAVE_MEMBLOCK
  736. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  737. {
  738. const u64 phys_offset = __pa(PAGE_OFFSET);
  739. base &= PAGE_MASK;
  740. size &= PAGE_MASK;
  741. if (base + size < phys_offset) {
  742. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  743. base, base + size);
  744. return;
  745. }
  746. if (base < phys_offset) {
  747. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  748. base, phys_offset);
  749. size -= phys_offset - base;
  750. base = phys_offset;
  751. }
  752. memblock_add(base, size);
  753. }
  754. /*
  755. * called from unflatten_device_tree() to bootstrap devicetree itself
  756. * Architectures can override this definition if memblock isn't used
  757. */
  758. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  759. {
  760. return __va(memblock_alloc(size, align));
  761. }
  762. #endif
  763. bool __init early_init_dt_scan(void *params)
  764. {
  765. if (!params)
  766. return false;
  767. /* Setup flat device-tree pointer */
  768. initial_boot_params = params;
  769. /* check device tree validity */
  770. if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
  771. initial_boot_params = NULL;
  772. return false;
  773. }
  774. /* Retrieve various information from the /chosen node */
  775. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  776. /* Initialize {size,address}-cells info */
  777. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  778. /* Setup memory, calling early_init_dt_add_memory_arch */
  779. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  780. return true;
  781. }
  782. /**
  783. * unflatten_device_tree - create tree of device_nodes from flat blob
  784. *
  785. * unflattens the device-tree passed by the firmware, creating the
  786. * tree of struct device_node. It also fills the "name" and "type"
  787. * pointers of the nodes so the normal device-tree walking functions
  788. * can be used.
  789. */
  790. void __init unflatten_device_tree(void)
  791. {
  792. __unflatten_device_tree(initial_boot_params, &of_allnodes,
  793. early_init_dt_alloc_memory_arch);
  794. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  795. of_alias_scan(early_init_dt_alloc_memory_arch);
  796. }
  797. /**
  798. * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
  799. *
  800. * Copies and unflattens the device-tree passed by the firmware, creating the
  801. * tree of struct device_node. It also fills the "name" and "type"
  802. * pointers of the nodes so the normal device-tree walking functions
  803. * can be used. This should only be used when the FDT memory has not been
  804. * reserved such is the case when the FDT is built-in to the kernel init
  805. * section. If the FDT memory is reserved already then unflatten_device_tree
  806. * should be used instead.
  807. */
  808. void __init unflatten_and_copy_device_tree(void)
  809. {
  810. int size = __be32_to_cpu(initial_boot_params->totalsize);
  811. void *dt = early_init_dt_alloc_memory_arch(size,
  812. __alignof__(struct boot_param_header));
  813. if (dt) {
  814. memcpy(dt, initial_boot_params, size);
  815. initial_boot_params = dt;
  816. }
  817. unflatten_device_tree();
  818. }
  819. #endif /* CONFIG_OF_EARLY_FLATTREE */