fdt.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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/crc32.h>
  12. #include <linux/kernel.h>
  13. #include <linux/initrd.h>
  14. #include <linux/memblock.h>
  15. #include <linux/of.h>
  16. #include <linux/of_fdt.h>
  17. #include <linux/of_reserved_mem.h>
  18. #include <linux/sizes.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/libfdt.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/serial_core.h>
  25. #include <linux/sysfs.h>
  26. #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
  27. #include <asm/page.h>
  28. /*
  29. * of_fdt_limit_memory - limit the number of regions in the /memory node
  30. * @limit: maximum entries
  31. *
  32. * Adjust the flattened device tree to have at most 'limit' number of
  33. * memory entries in the /memory node. This function may be called
  34. * any time after initial_boot_param is set.
  35. */
  36. void of_fdt_limit_memory(int limit)
  37. {
  38. int memory;
  39. int len;
  40. const void *val;
  41. int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  42. int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  43. const uint32_t *addr_prop;
  44. const uint32_t *size_prop;
  45. int root_offset;
  46. int cell_size;
  47. root_offset = fdt_path_offset(initial_boot_params, "/");
  48. if (root_offset < 0)
  49. return;
  50. addr_prop = fdt_getprop(initial_boot_params, root_offset,
  51. "#address-cells", NULL);
  52. if (addr_prop)
  53. nr_address_cells = fdt32_to_cpu(*addr_prop);
  54. size_prop = fdt_getprop(initial_boot_params, root_offset,
  55. "#size-cells", NULL);
  56. if (size_prop)
  57. nr_size_cells = fdt32_to_cpu(*size_prop);
  58. cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
  59. memory = fdt_path_offset(initial_boot_params, "/memory");
  60. if (memory > 0) {
  61. val = fdt_getprop(initial_boot_params, memory, "reg", &len);
  62. if (len > limit*cell_size) {
  63. len = limit*cell_size;
  64. pr_debug("Limiting number of entries to %d\n", limit);
  65. fdt_setprop(initial_boot_params, memory, "reg", val,
  66. len);
  67. }
  68. }
  69. }
  70. /**
  71. * of_fdt_is_compatible - Return true if given node from the given blob has
  72. * compat in its compatible list
  73. * @blob: A device tree blob
  74. * @node: node to test
  75. * @compat: compatible string to compare with compatible list.
  76. *
  77. * On match, returns a non-zero value with smaller values returned for more
  78. * specific compatible values.
  79. */
  80. int of_fdt_is_compatible(const void *blob,
  81. unsigned long node, const char *compat)
  82. {
  83. const char *cp;
  84. int cplen;
  85. unsigned long l, score = 0;
  86. cp = fdt_getprop(blob, node, "compatible", &cplen);
  87. if (cp == NULL)
  88. return 0;
  89. while (cplen > 0) {
  90. score++;
  91. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  92. return score;
  93. l = strlen(cp) + 1;
  94. cp += l;
  95. cplen -= l;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
  101. * @blob: A device tree blob
  102. * @node: node to test
  103. *
  104. * Returns true if the node has a "big-endian" property, or if the kernel
  105. * was compiled for BE *and* the node has a "native-endian" property.
  106. * Returns false otherwise.
  107. */
  108. bool of_fdt_is_big_endian(const void *blob, unsigned long node)
  109. {
  110. if (fdt_getprop(blob, node, "big-endian", NULL))
  111. return true;
  112. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  113. fdt_getprop(blob, node, "native-endian", NULL))
  114. return true;
  115. return false;
  116. }
  117. /**
  118. * of_fdt_match - Return true if node matches a list of compatible values
  119. */
  120. int of_fdt_match(const void *blob, unsigned long node,
  121. const char *const *compat)
  122. {
  123. unsigned int tmp, score = 0;
  124. if (!compat)
  125. return 0;
  126. while (*compat) {
  127. tmp = of_fdt_is_compatible(blob, node, *compat);
  128. if (tmp && (score == 0 || (tmp < score)))
  129. score = tmp;
  130. compat++;
  131. }
  132. return score;
  133. }
  134. static void *unflatten_dt_alloc(void **mem, unsigned long size,
  135. unsigned long align)
  136. {
  137. void *res;
  138. *mem = PTR_ALIGN(*mem, align);
  139. res = *mem;
  140. *mem += size;
  141. return res;
  142. }
  143. /**
  144. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  145. * @blob: The parent device tree blob
  146. * @mem: Memory chunk to use for allocating device nodes and properties
  147. * @poffset: pointer to node in flat tree
  148. * @dad: Parent struct device_node
  149. * @nodepp: The device_node tree created by the call
  150. * @fpsize: Size of the node path up at the current depth.
  151. * @dryrun: If true, do not allocate device nodes but still calculate needed
  152. * memory size
  153. */
  154. static void * unflatten_dt_node(const void *blob,
  155. void *mem,
  156. int *poffset,
  157. struct device_node *dad,
  158. struct device_node **nodepp,
  159. unsigned long fpsize,
  160. bool dryrun)
  161. {
  162. const __be32 *p;
  163. struct device_node *np;
  164. struct property *pp, **prev_pp = NULL;
  165. const char *pathp;
  166. unsigned int l, allocl;
  167. static int depth = 0;
  168. int old_depth;
  169. int offset;
  170. int has_name = 0;
  171. int new_format = 0;
  172. pathp = fdt_get_name(blob, *poffset, &l);
  173. if (!pathp)
  174. return mem;
  175. allocl = ++l;
  176. /* version 0x10 has a more compact unit name here instead of the full
  177. * path. we accumulate the full path size using "fpsize", we'll rebuild
  178. * it later. We detect this because the first character of the name is
  179. * not '/'.
  180. */
  181. if ((*pathp) != '/') {
  182. new_format = 1;
  183. if (fpsize == 0) {
  184. /* root node: special case. fpsize accounts for path
  185. * plus terminating zero. root node only has '/', so
  186. * fpsize should be 2, but we want to avoid the first
  187. * level nodes to have two '/' so we use fpsize 1 here
  188. */
  189. fpsize = 1;
  190. allocl = 2;
  191. l = 1;
  192. pathp = "";
  193. } else {
  194. /* account for '/' and path size minus terminal 0
  195. * already in 'l'
  196. */
  197. fpsize += l;
  198. allocl = fpsize;
  199. }
  200. }
  201. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  202. __alignof__(struct device_node));
  203. if (!dryrun) {
  204. char *fn;
  205. of_node_init(np);
  206. np->full_name = fn = ((char *)np) + sizeof(*np);
  207. if (new_format) {
  208. /* rebuild full path for new format */
  209. if (dad && dad->parent) {
  210. strcpy(fn, dad->full_name);
  211. #ifdef DEBUG
  212. if ((strlen(fn) + l + 1) != allocl) {
  213. pr_debug("%s: p: %d, l: %d, a: %d\n",
  214. pathp, (int)strlen(fn),
  215. l, allocl);
  216. }
  217. #endif
  218. fn += strlen(fn);
  219. }
  220. *(fn++) = '/';
  221. }
  222. memcpy(fn, pathp, l);
  223. prev_pp = &np->properties;
  224. if (dad != NULL) {
  225. np->parent = dad;
  226. np->sibling = dad->child;
  227. dad->child = np;
  228. }
  229. }
  230. /* process properties */
  231. for (offset = fdt_first_property_offset(blob, *poffset);
  232. (offset >= 0);
  233. (offset = fdt_next_property_offset(blob, offset))) {
  234. const char *pname;
  235. u32 sz;
  236. if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
  237. offset = -FDT_ERR_INTERNAL;
  238. break;
  239. }
  240. if (pname == NULL) {
  241. pr_info("Can't find property name in list !\n");
  242. break;
  243. }
  244. if (strcmp(pname, "name") == 0)
  245. has_name = 1;
  246. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  247. __alignof__(struct property));
  248. if (!dryrun) {
  249. /* We accept flattened tree phandles either in
  250. * ePAPR-style "phandle" properties, or the
  251. * legacy "linux,phandle" properties. If both
  252. * appear and have different values, things
  253. * will get weird. Don't do that. */
  254. if ((strcmp(pname, "phandle") == 0) ||
  255. (strcmp(pname, "linux,phandle") == 0)) {
  256. if (np->phandle == 0)
  257. np->phandle = be32_to_cpup(p);
  258. }
  259. /* And we process the "ibm,phandle" property
  260. * used in pSeries dynamic device tree
  261. * stuff */
  262. if (strcmp(pname, "ibm,phandle") == 0)
  263. np->phandle = be32_to_cpup(p);
  264. pp->name = (char *)pname;
  265. pp->length = sz;
  266. pp->value = (__be32 *)p;
  267. *prev_pp = pp;
  268. prev_pp = &pp->next;
  269. }
  270. }
  271. /* with version 0x10 we may not have the name property, recreate
  272. * it here from the unit name if absent
  273. */
  274. if (!has_name) {
  275. const char *p1 = pathp, *ps = pathp, *pa = NULL;
  276. int sz;
  277. while (*p1) {
  278. if ((*p1) == '@')
  279. pa = p1;
  280. if ((*p1) == '/')
  281. ps = p1 + 1;
  282. p1++;
  283. }
  284. if (pa < ps)
  285. pa = p1;
  286. sz = (pa - ps) + 1;
  287. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  288. __alignof__(struct property));
  289. if (!dryrun) {
  290. pp->name = "name";
  291. pp->length = sz;
  292. pp->value = pp + 1;
  293. *prev_pp = pp;
  294. prev_pp = &pp->next;
  295. memcpy(pp->value, ps, sz - 1);
  296. ((char *)pp->value)[sz - 1] = 0;
  297. pr_debug("fixed up name for %s -> %s\n", pathp,
  298. (char *)pp->value);
  299. }
  300. }
  301. if (!dryrun) {
  302. *prev_pp = NULL;
  303. np->name = of_get_property(np, "name", NULL);
  304. np->type = of_get_property(np, "device_type", NULL);
  305. if (!np->name)
  306. np->name = "<NULL>";
  307. if (!np->type)
  308. np->type = "<NULL>";
  309. }
  310. old_depth = depth;
  311. *poffset = fdt_next_node(blob, *poffset, &depth);
  312. if (depth < 0)
  313. depth = 0;
  314. while (*poffset > 0 && depth > old_depth)
  315. mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
  316. fpsize, dryrun);
  317. if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
  318. pr_err("unflatten: error %d processing FDT\n", *poffset);
  319. /*
  320. * Reverse the child list. Some drivers assumes node order matches .dts
  321. * node order
  322. */
  323. if (!dryrun && np->child) {
  324. struct device_node *child = np->child;
  325. np->child = NULL;
  326. while (child) {
  327. struct device_node *next = child->sibling;
  328. child->sibling = np->child;
  329. np->child = child;
  330. child = next;
  331. }
  332. }
  333. if (nodepp)
  334. *nodepp = np;
  335. return mem;
  336. }
  337. /**
  338. * __unflatten_device_tree - create tree of device_nodes from flat blob
  339. *
  340. * unflattens a device-tree, creating the
  341. * tree of struct device_node. It also fills the "name" and "type"
  342. * pointers of the nodes so the normal device-tree walking functions
  343. * can be used.
  344. * @blob: The blob to expand
  345. * @mynodes: The device_node tree created by the call
  346. * @dt_alloc: An allocator that provides a virtual address to memory
  347. * for the resulting tree
  348. */
  349. static void __unflatten_device_tree(const void *blob,
  350. struct device_node **mynodes,
  351. void * (*dt_alloc)(u64 size, u64 align))
  352. {
  353. unsigned long size;
  354. int start;
  355. void *mem;
  356. pr_debug(" -> unflatten_device_tree()\n");
  357. if (!blob) {
  358. pr_debug("No device tree pointer\n");
  359. return;
  360. }
  361. pr_debug("Unflattening device tree:\n");
  362. pr_debug("magic: %08x\n", fdt_magic(blob));
  363. pr_debug("size: %08x\n", fdt_totalsize(blob));
  364. pr_debug("version: %08x\n", fdt_version(blob));
  365. if (fdt_check_header(blob)) {
  366. pr_err("Invalid device tree blob header\n");
  367. return;
  368. }
  369. /* First pass, scan for size */
  370. start = 0;
  371. size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
  372. size = ALIGN(size, 4);
  373. pr_debug(" size is %lx, allocating...\n", size);
  374. /* Allocate memory for the expanded device tree */
  375. mem = dt_alloc(size + 4, __alignof__(struct device_node));
  376. memset(mem, 0, size);
  377. *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
  378. pr_debug(" unflattening %p...\n", mem);
  379. /* Second pass, do actual unflattening */
  380. start = 0;
  381. unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
  382. if (be32_to_cpup(mem + size) != 0xdeadbeef)
  383. pr_warning("End of tree marker overwritten: %08x\n",
  384. be32_to_cpup(mem + size));
  385. pr_debug(" <- unflatten_device_tree()\n");
  386. }
  387. static void *kernel_tree_alloc(u64 size, u64 align)
  388. {
  389. return kzalloc(size, GFP_KERNEL);
  390. }
  391. /**
  392. * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
  393. *
  394. * unflattens the device-tree passed by the firmware, creating the
  395. * tree of struct device_node. It also fills the "name" and "type"
  396. * pointers of the nodes so the normal device-tree walking functions
  397. * can be used.
  398. */
  399. void of_fdt_unflatten_tree(const unsigned long *blob,
  400. struct device_node **mynodes)
  401. {
  402. __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
  403. }
  404. EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
  405. /* Everything below here references initial_boot_params directly. */
  406. int __initdata dt_root_addr_cells;
  407. int __initdata dt_root_size_cells;
  408. void *initial_boot_params;
  409. #ifdef CONFIG_OF_EARLY_FLATTREE
  410. static u32 of_fdt_crc32;
  411. /**
  412. * res_mem_reserve_reg() - reserve all memory described in 'reg' property
  413. */
  414. static int __init __reserved_mem_reserve_reg(unsigned long node,
  415. const char *uname)
  416. {
  417. int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
  418. phys_addr_t base, size;
  419. int len;
  420. const __be32 *prop;
  421. int nomap, first = 1;
  422. prop = of_get_flat_dt_prop(node, "reg", &len);
  423. if (!prop)
  424. return -ENOENT;
  425. if (len && len % t_len != 0) {
  426. pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
  427. uname);
  428. return -EINVAL;
  429. }
  430. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  431. while (len >= t_len) {
  432. base = dt_mem_next_cell(dt_root_addr_cells, &prop);
  433. size = dt_mem_next_cell(dt_root_size_cells, &prop);
  434. if (size &&
  435. early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
  436. pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
  437. uname, &base, (unsigned long)size / SZ_1M);
  438. else
  439. pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
  440. uname, &base, (unsigned long)size / SZ_1M);
  441. len -= t_len;
  442. if (first) {
  443. fdt_reserved_mem_save_node(node, uname, base, size);
  444. first = 0;
  445. }
  446. }
  447. return 0;
  448. }
  449. /**
  450. * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
  451. * in /reserved-memory matches the values supported by the current implementation,
  452. * also check if ranges property has been provided
  453. */
  454. static int __init __reserved_mem_check_root(unsigned long node)
  455. {
  456. const __be32 *prop;
  457. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  458. if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
  459. return -EINVAL;
  460. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  461. if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
  462. return -EINVAL;
  463. prop = of_get_flat_dt_prop(node, "ranges", NULL);
  464. if (!prop)
  465. return -EINVAL;
  466. return 0;
  467. }
  468. /**
  469. * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
  470. */
  471. static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
  472. int depth, void *data)
  473. {
  474. static int found;
  475. const char *status;
  476. int err;
  477. if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
  478. if (__reserved_mem_check_root(node) != 0) {
  479. pr_err("Reserved memory: unsupported node format, ignoring\n");
  480. /* break scan */
  481. return 1;
  482. }
  483. found = 1;
  484. /* scan next node */
  485. return 0;
  486. } else if (!found) {
  487. /* scan next node */
  488. return 0;
  489. } else if (found && depth < 2) {
  490. /* scanning of /reserved-memory has been finished */
  491. return 1;
  492. }
  493. status = of_get_flat_dt_prop(node, "status", NULL);
  494. if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
  495. return 0;
  496. err = __reserved_mem_reserve_reg(node, uname);
  497. if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
  498. fdt_reserved_mem_save_node(node, uname, 0, 0);
  499. /* scan next node */
  500. return 0;
  501. }
  502. /**
  503. * early_init_fdt_scan_reserved_mem() - create reserved memory regions
  504. *
  505. * This function grabs memory from early allocator for device exclusive use
  506. * defined in device tree structures. It should be called by arch specific code
  507. * once the early allocator (i.e. memblock) has been fully activated.
  508. */
  509. void __init early_init_fdt_scan_reserved_mem(void)
  510. {
  511. int n;
  512. u64 base, size;
  513. if (!initial_boot_params)
  514. return;
  515. /* Process header /memreserve/ fields */
  516. for (n = 0; ; n++) {
  517. fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
  518. if (!size)
  519. break;
  520. early_init_dt_reserve_memory_arch(base, size, 0);
  521. }
  522. of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
  523. fdt_init_reserved_mem();
  524. }
  525. /**
  526. * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
  527. */
  528. void __init early_init_fdt_reserve_self(void)
  529. {
  530. if (!initial_boot_params)
  531. return;
  532. /* Reserve the dtb region */
  533. early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
  534. fdt_totalsize(initial_boot_params),
  535. 0);
  536. }
  537. /**
  538. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  539. * @it: callback function
  540. * @data: context data pointer
  541. *
  542. * This function is used to scan the flattened device-tree, it is
  543. * used to extract the memory information at boot before we can
  544. * unflatten the tree
  545. */
  546. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  547. const char *uname, int depth,
  548. void *data),
  549. void *data)
  550. {
  551. const void *blob = initial_boot_params;
  552. const char *pathp;
  553. int offset, rc = 0, depth = -1;
  554. for (offset = fdt_next_node(blob, -1, &depth);
  555. offset >= 0 && depth >= 0 && !rc;
  556. offset = fdt_next_node(blob, offset, &depth)) {
  557. pathp = fdt_get_name(blob, offset, NULL);
  558. if (*pathp == '/')
  559. pathp = kbasename(pathp);
  560. rc = it(offset, pathp, depth, data);
  561. }
  562. return rc;
  563. }
  564. /**
  565. * of_get_flat_dt_root - find the root node in the flat blob
  566. */
  567. unsigned long __init of_get_flat_dt_root(void)
  568. {
  569. return 0;
  570. }
  571. /**
  572. * of_get_flat_dt_size - Return the total size of the FDT
  573. */
  574. int __init of_get_flat_dt_size(void)
  575. {
  576. return fdt_totalsize(initial_boot_params);
  577. }
  578. /**
  579. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  580. *
  581. * This function can be used within scan_flattened_dt callback to get
  582. * access to properties
  583. */
  584. const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  585. int *size)
  586. {
  587. return fdt_getprop(initial_boot_params, node, name, size);
  588. }
  589. /**
  590. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  591. * @node: node to test
  592. * @compat: compatible string to compare with compatible list.
  593. */
  594. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  595. {
  596. return of_fdt_is_compatible(initial_boot_params, node, compat);
  597. }
  598. /**
  599. * of_flat_dt_match - Return true if node matches a list of compatible values
  600. */
  601. int __init of_flat_dt_match(unsigned long node, const char *const *compat)
  602. {
  603. return of_fdt_match(initial_boot_params, node, compat);
  604. }
  605. struct fdt_scan_status {
  606. const char *name;
  607. int namelen;
  608. int depth;
  609. int found;
  610. int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
  611. void *data;
  612. };
  613. const char * __init of_flat_dt_get_machine_name(void)
  614. {
  615. const char *name;
  616. unsigned long dt_root = of_get_flat_dt_root();
  617. name = of_get_flat_dt_prop(dt_root, "model", NULL);
  618. if (!name)
  619. name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  620. return name;
  621. }
  622. /**
  623. * of_flat_dt_match_machine - Iterate match tables to find matching machine.
  624. *
  625. * @default_match: A machine specific ptr to return in case of no match.
  626. * @get_next_compat: callback function to return next compatible match table.
  627. *
  628. * Iterate through machine match tables to find the best match for the machine
  629. * compatible string in the FDT.
  630. */
  631. const void * __init of_flat_dt_match_machine(const void *default_match,
  632. const void * (*get_next_compat)(const char * const**))
  633. {
  634. const void *data = NULL;
  635. const void *best_data = default_match;
  636. const char *const *compat;
  637. unsigned long dt_root;
  638. unsigned int best_score = ~1, score = 0;
  639. dt_root = of_get_flat_dt_root();
  640. while ((data = get_next_compat(&compat))) {
  641. score = of_flat_dt_match(dt_root, compat);
  642. if (score > 0 && score < best_score) {
  643. best_data = data;
  644. best_score = score;
  645. }
  646. }
  647. if (!best_data) {
  648. const char *prop;
  649. int size;
  650. pr_err("\n unrecognized device tree list:\n[ ");
  651. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  652. if (prop) {
  653. while (size > 0) {
  654. printk("'%s' ", prop);
  655. size -= strlen(prop) + 1;
  656. prop += strlen(prop) + 1;
  657. }
  658. }
  659. printk("]\n\n");
  660. return NULL;
  661. }
  662. pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
  663. return best_data;
  664. }
  665. #ifdef CONFIG_BLK_DEV_INITRD
  666. /**
  667. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  668. * @node: reference to node containing initrd location ('chosen')
  669. */
  670. static void __init early_init_dt_check_for_initrd(unsigned long node)
  671. {
  672. u64 start, end;
  673. int len;
  674. const __be32 *prop;
  675. pr_debug("Looking for initrd properties... ");
  676. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  677. if (!prop)
  678. return;
  679. start = of_read_number(prop, len/4);
  680. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  681. if (!prop)
  682. return;
  683. end = of_read_number(prop, len/4);
  684. initrd_start = (unsigned long)__va(start);
  685. initrd_end = (unsigned long)__va(end);
  686. initrd_below_start_ok = 1;
  687. pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
  688. (unsigned long long)start, (unsigned long long)end);
  689. }
  690. #else
  691. static inline void early_init_dt_check_for_initrd(unsigned long node)
  692. {
  693. }
  694. #endif /* CONFIG_BLK_DEV_INITRD */
  695. #ifdef CONFIG_SERIAL_EARLYCON
  696. extern struct of_device_id __earlycon_of_table[];
  697. static int __init early_init_dt_scan_chosen_serial(void)
  698. {
  699. int offset;
  700. const char *p;
  701. int l;
  702. const struct of_device_id *match = __earlycon_of_table;
  703. const void *fdt = initial_boot_params;
  704. offset = fdt_path_offset(fdt, "/chosen");
  705. if (offset < 0)
  706. offset = fdt_path_offset(fdt, "/chosen@0");
  707. if (offset < 0)
  708. return -ENOENT;
  709. p = fdt_getprop(fdt, offset, "stdout-path", &l);
  710. if (!p)
  711. p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
  712. if (!p || !l)
  713. return -ENOENT;
  714. /* Get the node specified by stdout-path */
  715. offset = fdt_path_offset(fdt, p);
  716. if (offset < 0)
  717. return -ENODEV;
  718. while (match->compatible[0]) {
  719. unsigned long addr;
  720. if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
  721. match++;
  722. continue;
  723. }
  724. addr = fdt_translate_address(fdt, offset);
  725. if (!addr)
  726. return -ENXIO;
  727. of_setup_earlycon(addr, match->data);
  728. return 0;
  729. }
  730. return -ENODEV;
  731. }
  732. static int __init setup_of_earlycon(char *buf)
  733. {
  734. if (buf)
  735. return 0;
  736. return early_init_dt_scan_chosen_serial();
  737. }
  738. early_param("earlycon", setup_of_earlycon);
  739. #endif
  740. /**
  741. * early_init_dt_scan_root - fetch the top level address and size cells
  742. */
  743. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  744. int depth, void *data)
  745. {
  746. const __be32 *prop;
  747. if (depth != 0)
  748. return 0;
  749. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  750. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  751. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  752. if (prop)
  753. dt_root_size_cells = be32_to_cpup(prop);
  754. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  755. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  756. if (prop)
  757. dt_root_addr_cells = be32_to_cpup(prop);
  758. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  759. /* break now */
  760. return 1;
  761. }
  762. u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
  763. {
  764. const __be32 *p = *cellp;
  765. *cellp = p + s;
  766. return of_read_number(p, s);
  767. }
  768. /**
  769. * early_init_dt_scan_memory - Look for an parse memory nodes
  770. */
  771. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  772. int depth, void *data)
  773. {
  774. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  775. const __be32 *reg, *endp;
  776. int l;
  777. /* We are scanning "memory" nodes only */
  778. if (type == NULL) {
  779. /*
  780. * The longtrail doesn't have a device_type on the
  781. * /memory node, so look for the node called /memory@0.
  782. */
  783. if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
  784. return 0;
  785. } else if (strcmp(type, "memory") != 0)
  786. return 0;
  787. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  788. if (reg == NULL)
  789. reg = of_get_flat_dt_prop(node, "reg", &l);
  790. if (reg == NULL)
  791. return 0;
  792. endp = reg + (l / sizeof(__be32));
  793. pr_debug("memory scan node %s, reg size %d,\n", uname, l);
  794. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  795. u64 base, size;
  796. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  797. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  798. if (size == 0)
  799. continue;
  800. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  801. (unsigned long long)size);
  802. early_init_dt_add_memory_arch(base, size);
  803. }
  804. return 0;
  805. }
  806. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  807. int depth, void *data)
  808. {
  809. int l;
  810. const char *p;
  811. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  812. if (depth != 1 || !data ||
  813. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  814. return 0;
  815. early_init_dt_check_for_initrd(node);
  816. /* Retrieve command line */
  817. p = of_get_flat_dt_prop(node, "bootargs", &l);
  818. if (p != NULL && l > 0)
  819. strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  820. /*
  821. * CONFIG_CMDLINE is meant to be a default in case nothing else
  822. * managed to set the command line, unless CONFIG_CMDLINE_FORCE
  823. * is set in which case we override whatever was found earlier.
  824. */
  825. #ifdef CONFIG_CMDLINE
  826. #ifndef CONFIG_CMDLINE_FORCE
  827. if (!((char *)data)[0])
  828. #endif
  829. strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  830. #endif /* CONFIG_CMDLINE */
  831. pr_debug("Command line is: %s\n", (char*)data);
  832. /* break now */
  833. return 1;
  834. }
  835. #ifdef CONFIG_HAVE_MEMBLOCK
  836. #ifndef MAX_MEMBLOCK_ADDR
  837. #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
  838. #endif
  839. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  840. {
  841. const u64 phys_offset = __pa(PAGE_OFFSET);
  842. if (!PAGE_ALIGNED(base)) {
  843. if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
  844. pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
  845. base, base + size);
  846. return;
  847. }
  848. size -= PAGE_SIZE - (base & ~PAGE_MASK);
  849. base = PAGE_ALIGN(base);
  850. }
  851. size &= PAGE_MASK;
  852. if (base > MAX_MEMBLOCK_ADDR) {
  853. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  854. base, base + size);
  855. return;
  856. }
  857. if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
  858. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  859. ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
  860. size = MAX_MEMBLOCK_ADDR - base + 1;
  861. }
  862. if (base + size < phys_offset) {
  863. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  864. base, base + size);
  865. return;
  866. }
  867. if (base < phys_offset) {
  868. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  869. base, phys_offset);
  870. size -= phys_offset - base;
  871. base = phys_offset;
  872. }
  873. memblock_add(base, size);
  874. }
  875. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  876. phys_addr_t size, bool nomap)
  877. {
  878. if (nomap)
  879. return memblock_remove(base, size);
  880. return memblock_reserve(base, size);
  881. }
  882. /*
  883. * called from unflatten_device_tree() to bootstrap devicetree itself
  884. * Architectures can override this definition if memblock isn't used
  885. */
  886. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  887. {
  888. return __va(memblock_alloc(size, align));
  889. }
  890. #else
  891. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  892. {
  893. WARN_ON(1);
  894. }
  895. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  896. phys_addr_t size, bool nomap)
  897. {
  898. pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
  899. &base, &size, nomap ? " (nomap)" : "");
  900. return -ENOSYS;
  901. }
  902. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  903. {
  904. WARN_ON(1);
  905. return NULL;
  906. }
  907. #endif
  908. bool __init early_init_dt_verify(void *params)
  909. {
  910. if (!params)
  911. return false;
  912. /* check device tree validity */
  913. if (fdt_check_header(params))
  914. return false;
  915. /* Setup flat device-tree pointer */
  916. initial_boot_params = params;
  917. of_fdt_crc32 = crc32_be(~0, initial_boot_params,
  918. fdt_totalsize(initial_boot_params));
  919. return true;
  920. }
  921. void __init early_init_dt_scan_nodes(void)
  922. {
  923. /* Retrieve various information from the /chosen node */
  924. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  925. /* Initialize {size,address}-cells info */
  926. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  927. /* Setup memory, calling early_init_dt_add_memory_arch */
  928. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  929. }
  930. bool __init early_init_dt_scan(void *params)
  931. {
  932. bool status;
  933. status = early_init_dt_verify(params);
  934. if (!status)
  935. return false;
  936. early_init_dt_scan_nodes();
  937. return true;
  938. }
  939. /**
  940. * unflatten_device_tree - create tree of device_nodes from flat blob
  941. *
  942. * unflattens the device-tree passed by the firmware, creating the
  943. * tree of struct device_node. It also fills the "name" and "type"
  944. * pointers of the nodes so the normal device-tree walking functions
  945. * can be used.
  946. */
  947. void __init unflatten_device_tree(void)
  948. {
  949. __unflatten_device_tree(initial_boot_params, &of_root,
  950. early_init_dt_alloc_memory_arch);
  951. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  952. of_alias_scan(early_init_dt_alloc_memory_arch);
  953. }
  954. /**
  955. * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
  956. *
  957. * Copies and unflattens the device-tree passed by the firmware, creating the
  958. * tree of struct device_node. It also fills the "name" and "type"
  959. * pointers of the nodes so the normal device-tree walking functions
  960. * can be used. This should only be used when the FDT memory has not been
  961. * reserved such is the case when the FDT is built-in to the kernel init
  962. * section. If the FDT memory is reserved already then unflatten_device_tree
  963. * should be used instead.
  964. */
  965. void __init unflatten_and_copy_device_tree(void)
  966. {
  967. int size;
  968. void *dt;
  969. if (!initial_boot_params) {
  970. pr_warn("No valid device tree found, continuing without\n");
  971. return;
  972. }
  973. size = fdt_totalsize(initial_boot_params);
  974. dt = early_init_dt_alloc_memory_arch(size,
  975. roundup_pow_of_two(FDT_V17_SIZE));
  976. if (dt) {
  977. memcpy(dt, initial_boot_params, size);
  978. initial_boot_params = dt;
  979. }
  980. unflatten_device_tree();
  981. }
  982. #ifdef CONFIG_SYSFS
  983. static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
  984. struct bin_attribute *bin_attr,
  985. char *buf, loff_t off, size_t count)
  986. {
  987. memcpy(buf, initial_boot_params + off, count);
  988. return count;
  989. }
  990. static int __init of_fdt_raw_init(void)
  991. {
  992. static struct bin_attribute of_fdt_raw_attr =
  993. __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
  994. if (!initial_boot_params)
  995. return 0;
  996. if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
  997. fdt_totalsize(initial_boot_params))) {
  998. pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
  999. return 0;
  1000. }
  1001. of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
  1002. return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
  1003. }
  1004. late_initcall(of_fdt_raw_init);
  1005. #endif
  1006. #endif /* CONFIG_OF_EARLY_FLATTREE */