fdt.c 33 KB

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