fdt.c 33 KB

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