fdt.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. if (!mem)
  444. return NULL;
  445. memset(mem, 0, size);
  446. *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
  447. pr_debug(" unflattening %p...\n", mem);
  448. /* Second pass, do actual unflattening */
  449. unflatten_dt_nodes(blob, mem, dad, mynodes);
  450. if (be32_to_cpup(mem + size) != 0xdeadbeef)
  451. pr_warning("End of tree marker overwritten: %08x\n",
  452. be32_to_cpup(mem + size));
  453. if (detached && mynodes) {
  454. of_node_set_flag(*mynodes, OF_DETACHED);
  455. pr_debug("unflattened tree is detached\n");
  456. }
  457. pr_debug(" <- unflatten_device_tree()\n");
  458. return mem;
  459. }
  460. static void *kernel_tree_alloc(u64 size, u64 align)
  461. {
  462. return kzalloc(size, GFP_KERNEL);
  463. }
  464. static DEFINE_MUTEX(of_fdt_unflatten_mutex);
  465. /**
  466. * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
  467. * @blob: Flat device tree blob
  468. * @dad: Parent device node
  469. * @mynodes: The device tree created by the call
  470. *
  471. * unflattens the device-tree passed by the firmware, creating the
  472. * tree of struct device_node. It also fills the "name" and "type"
  473. * pointers of the nodes so the normal device-tree walking functions
  474. * can be used.
  475. *
  476. * Returns NULL on failure or the memory chunk containing the unflattened
  477. * device tree on success.
  478. */
  479. void *of_fdt_unflatten_tree(const unsigned long *blob,
  480. struct device_node *dad,
  481. struct device_node **mynodes)
  482. {
  483. void *mem;
  484. mutex_lock(&of_fdt_unflatten_mutex);
  485. mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
  486. true);
  487. mutex_unlock(&of_fdt_unflatten_mutex);
  488. return mem;
  489. }
  490. EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
  491. /* Everything below here references initial_boot_params directly. */
  492. int __initdata dt_root_addr_cells;
  493. int __initdata dt_root_size_cells;
  494. void *initial_boot_params;
  495. #ifdef CONFIG_OF_EARLY_FLATTREE
  496. static u32 of_fdt_crc32;
  497. /**
  498. * res_mem_reserve_reg() - reserve all memory described in 'reg' property
  499. */
  500. static int __init __reserved_mem_reserve_reg(unsigned long node,
  501. const char *uname)
  502. {
  503. int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
  504. phys_addr_t base, size;
  505. int len;
  506. const __be32 *prop;
  507. int nomap, first = 1;
  508. prop = of_get_flat_dt_prop(node, "reg", &len);
  509. if (!prop)
  510. return -ENOENT;
  511. if (len && len % t_len != 0) {
  512. pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
  513. uname);
  514. return -EINVAL;
  515. }
  516. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  517. while (len >= t_len) {
  518. base = dt_mem_next_cell(dt_root_addr_cells, &prop);
  519. size = dt_mem_next_cell(dt_root_size_cells, &prop);
  520. if (size &&
  521. early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
  522. pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
  523. uname, &base, (unsigned long)size / SZ_1M);
  524. else
  525. pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
  526. uname, &base, (unsigned long)size / SZ_1M);
  527. len -= t_len;
  528. if (first) {
  529. fdt_reserved_mem_save_node(node, uname, base, size);
  530. first = 0;
  531. }
  532. }
  533. return 0;
  534. }
  535. /**
  536. * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
  537. * in /reserved-memory matches the values supported by the current implementation,
  538. * also check if ranges property has been provided
  539. */
  540. static int __init __reserved_mem_check_root(unsigned long node)
  541. {
  542. const __be32 *prop;
  543. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  544. if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
  545. return -EINVAL;
  546. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  547. if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
  548. return -EINVAL;
  549. prop = of_get_flat_dt_prop(node, "ranges", NULL);
  550. if (!prop)
  551. return -EINVAL;
  552. return 0;
  553. }
  554. /**
  555. * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
  556. */
  557. static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
  558. int depth, void *data)
  559. {
  560. static int found;
  561. const char *status;
  562. int err;
  563. if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
  564. if (__reserved_mem_check_root(node) != 0) {
  565. pr_err("Reserved memory: unsupported node format, ignoring\n");
  566. /* break scan */
  567. return 1;
  568. }
  569. found = 1;
  570. /* scan next node */
  571. return 0;
  572. } else if (!found) {
  573. /* scan next node */
  574. return 0;
  575. } else if (found && depth < 2) {
  576. /* scanning of /reserved-memory has been finished */
  577. return 1;
  578. }
  579. status = of_get_flat_dt_prop(node, "status", NULL);
  580. if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
  581. return 0;
  582. err = __reserved_mem_reserve_reg(node, uname);
  583. if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
  584. fdt_reserved_mem_save_node(node, uname, 0, 0);
  585. /* scan next node */
  586. return 0;
  587. }
  588. /**
  589. * early_init_fdt_scan_reserved_mem() - create reserved memory regions
  590. *
  591. * This function grabs memory from early allocator for device exclusive use
  592. * defined in device tree structures. It should be called by arch specific code
  593. * once the early allocator (i.e. memblock) has been fully activated.
  594. */
  595. void __init early_init_fdt_scan_reserved_mem(void)
  596. {
  597. int n;
  598. u64 base, size;
  599. if (!initial_boot_params)
  600. return;
  601. /* Process header /memreserve/ fields */
  602. for (n = 0; ; n++) {
  603. fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
  604. if (!size)
  605. break;
  606. early_init_dt_reserve_memory_arch(base, size, 0);
  607. }
  608. of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
  609. fdt_init_reserved_mem();
  610. }
  611. /**
  612. * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
  613. */
  614. void __init early_init_fdt_reserve_self(void)
  615. {
  616. if (!initial_boot_params)
  617. return;
  618. /* Reserve the dtb region */
  619. early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
  620. fdt_totalsize(initial_boot_params),
  621. 0);
  622. }
  623. /**
  624. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  625. * @it: callback function
  626. * @data: context data pointer
  627. *
  628. * This function is used to scan the flattened device-tree, it is
  629. * used to extract the memory information at boot before we can
  630. * unflatten the tree
  631. */
  632. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  633. const char *uname, int depth,
  634. void *data),
  635. void *data)
  636. {
  637. const void *blob = initial_boot_params;
  638. const char *pathp;
  639. int offset, rc = 0, depth = -1;
  640. if (!blob)
  641. return 0;
  642. for (offset = fdt_next_node(blob, -1, &depth);
  643. offset >= 0 && depth >= 0 && !rc;
  644. offset = fdt_next_node(blob, offset, &depth)) {
  645. pathp = fdt_get_name(blob, offset, NULL);
  646. if (*pathp == '/')
  647. pathp = kbasename(pathp);
  648. rc = it(offset, pathp, depth, data);
  649. }
  650. return rc;
  651. }
  652. /**
  653. * of_get_flat_dt_subnode_by_name - get the subnode by given name
  654. *
  655. * @node: the parent node
  656. * @uname: the name of subnode
  657. * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
  658. */
  659. int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
  660. {
  661. return fdt_subnode_offset(initial_boot_params, node, uname);
  662. }
  663. /**
  664. * of_get_flat_dt_root - find the root node in the flat blob
  665. */
  666. unsigned long __init of_get_flat_dt_root(void)
  667. {
  668. return 0;
  669. }
  670. /**
  671. * of_get_flat_dt_size - Return the total size of the FDT
  672. */
  673. int __init of_get_flat_dt_size(void)
  674. {
  675. return fdt_totalsize(initial_boot_params);
  676. }
  677. /**
  678. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  679. *
  680. * This function can be used within scan_flattened_dt callback to get
  681. * access to properties
  682. */
  683. const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  684. int *size)
  685. {
  686. return fdt_getprop(initial_boot_params, node, name, size);
  687. }
  688. /**
  689. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  690. * @node: node to test
  691. * @compat: compatible string to compare with compatible list.
  692. */
  693. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  694. {
  695. return of_fdt_is_compatible(initial_boot_params, node, compat);
  696. }
  697. /**
  698. * of_flat_dt_match - Return true if node matches a list of compatible values
  699. */
  700. int __init of_flat_dt_match(unsigned long node, const char *const *compat)
  701. {
  702. return of_fdt_match(initial_boot_params, node, compat);
  703. }
  704. struct fdt_scan_status {
  705. const char *name;
  706. int namelen;
  707. int depth;
  708. int found;
  709. int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
  710. void *data;
  711. };
  712. const char * __init of_flat_dt_get_machine_name(void)
  713. {
  714. const char *name;
  715. unsigned long dt_root = of_get_flat_dt_root();
  716. name = of_get_flat_dt_prop(dt_root, "model", NULL);
  717. if (!name)
  718. name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  719. return name;
  720. }
  721. /**
  722. * of_flat_dt_match_machine - Iterate match tables to find matching machine.
  723. *
  724. * @default_match: A machine specific ptr to return in case of no match.
  725. * @get_next_compat: callback function to return next compatible match table.
  726. *
  727. * Iterate through machine match tables to find the best match for the machine
  728. * compatible string in the FDT.
  729. */
  730. const void * __init of_flat_dt_match_machine(const void *default_match,
  731. const void * (*get_next_compat)(const char * const**))
  732. {
  733. const void *data = NULL;
  734. const void *best_data = default_match;
  735. const char *const *compat;
  736. unsigned long dt_root;
  737. unsigned int best_score = ~1, score = 0;
  738. dt_root = of_get_flat_dt_root();
  739. while ((data = get_next_compat(&compat))) {
  740. score = of_flat_dt_match(dt_root, compat);
  741. if (score > 0 && score < best_score) {
  742. best_data = data;
  743. best_score = score;
  744. }
  745. }
  746. if (!best_data) {
  747. const char *prop;
  748. int size;
  749. pr_err("\n unrecognized device tree list:\n[ ");
  750. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  751. if (prop) {
  752. while (size > 0) {
  753. printk("'%s' ", prop);
  754. size -= strlen(prop) + 1;
  755. prop += strlen(prop) + 1;
  756. }
  757. }
  758. printk("]\n\n");
  759. return NULL;
  760. }
  761. pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
  762. return best_data;
  763. }
  764. #ifdef CONFIG_BLK_DEV_INITRD
  765. #ifndef __early_init_dt_declare_initrd
  766. static void __early_init_dt_declare_initrd(unsigned long start,
  767. unsigned long end)
  768. {
  769. initrd_start = (unsigned long)__va(start);
  770. initrd_end = (unsigned long)__va(end);
  771. initrd_below_start_ok = 1;
  772. }
  773. #endif
  774. /**
  775. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  776. * @node: reference to node containing initrd location ('chosen')
  777. */
  778. static void __init early_init_dt_check_for_initrd(unsigned long node)
  779. {
  780. u64 start, end;
  781. int len;
  782. const __be32 *prop;
  783. pr_debug("Looking for initrd properties... ");
  784. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  785. if (!prop)
  786. return;
  787. start = of_read_number(prop, len/4);
  788. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  789. if (!prop)
  790. return;
  791. end = of_read_number(prop, len/4);
  792. __early_init_dt_declare_initrd(start, end);
  793. pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
  794. (unsigned long long)start, (unsigned long long)end);
  795. }
  796. #else
  797. static inline void early_init_dt_check_for_initrd(unsigned long node)
  798. {
  799. }
  800. #endif /* CONFIG_BLK_DEV_INITRD */
  801. #ifdef CONFIG_SERIAL_EARLYCON
  802. int __init early_init_dt_scan_chosen_stdout(void)
  803. {
  804. int offset;
  805. const char *p, *q, *options = NULL;
  806. int l;
  807. const struct earlycon_id **p_match;
  808. const void *fdt = initial_boot_params;
  809. offset = fdt_path_offset(fdt, "/chosen");
  810. if (offset < 0)
  811. offset = fdt_path_offset(fdt, "/chosen@0");
  812. if (offset < 0)
  813. return -ENOENT;
  814. p = fdt_getprop(fdt, offset, "stdout-path", &l);
  815. if (!p)
  816. p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
  817. if (!p || !l)
  818. return -ENOENT;
  819. q = strchrnul(p, ':');
  820. if (*q != '\0')
  821. options = q + 1;
  822. l = q - p;
  823. /* Get the node specified by stdout-path */
  824. offset = fdt_path_offset_namelen(fdt, p, l);
  825. if (offset < 0) {
  826. pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
  827. return 0;
  828. }
  829. for (p_match = __earlycon_table; p_match < __earlycon_table_end;
  830. p_match++) {
  831. const struct earlycon_id *match = *p_match;
  832. if (!match->compatible[0])
  833. continue;
  834. if (fdt_node_check_compatible(fdt, offset, match->compatible))
  835. continue;
  836. of_setup_earlycon(match, offset, options);
  837. return 0;
  838. }
  839. return -ENODEV;
  840. }
  841. #endif
  842. /**
  843. * early_init_dt_scan_root - fetch the top level address and size cells
  844. */
  845. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  846. int depth, void *data)
  847. {
  848. const __be32 *prop;
  849. if (depth != 0)
  850. return 0;
  851. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  852. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  853. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  854. if (prop)
  855. dt_root_size_cells = be32_to_cpup(prop);
  856. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  857. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  858. if (prop)
  859. dt_root_addr_cells = be32_to_cpup(prop);
  860. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  861. /* break now */
  862. return 1;
  863. }
  864. u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
  865. {
  866. const __be32 *p = *cellp;
  867. *cellp = p + s;
  868. return of_read_number(p, s);
  869. }
  870. /**
  871. * early_init_dt_scan_memory - Look for an parse memory nodes
  872. */
  873. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  874. int depth, void *data)
  875. {
  876. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  877. const __be32 *reg, *endp;
  878. int l;
  879. /* We are scanning "memory" nodes only */
  880. if (type == NULL) {
  881. /*
  882. * The longtrail doesn't have a device_type on the
  883. * /memory node, so look for the node called /memory@0.
  884. */
  885. if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
  886. return 0;
  887. } else if (strcmp(type, "memory") != 0)
  888. return 0;
  889. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  890. if (reg == NULL)
  891. reg = of_get_flat_dt_prop(node, "reg", &l);
  892. if (reg == NULL)
  893. return 0;
  894. endp = reg + (l / sizeof(__be32));
  895. pr_debug("memory scan node %s, reg size %d,\n", uname, l);
  896. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  897. u64 base, size;
  898. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  899. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  900. if (size == 0)
  901. continue;
  902. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  903. (unsigned long long)size);
  904. early_init_dt_add_memory_arch(base, size);
  905. }
  906. return 0;
  907. }
  908. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  909. int depth, void *data)
  910. {
  911. int l;
  912. const char *p;
  913. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  914. if (depth != 1 || !data ||
  915. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  916. return 0;
  917. early_init_dt_check_for_initrd(node);
  918. /* Retrieve command line */
  919. p = of_get_flat_dt_prop(node, "bootargs", &l);
  920. if (p != NULL && l > 0)
  921. strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  922. /*
  923. * CONFIG_CMDLINE is meant to be a default in case nothing else
  924. * managed to set the command line, unless CONFIG_CMDLINE_FORCE
  925. * is set in which case we override whatever was found earlier.
  926. */
  927. #ifdef CONFIG_CMDLINE
  928. #if defined(CONFIG_CMDLINE_EXTEND)
  929. strlcat(data, " ", COMMAND_LINE_SIZE);
  930. strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  931. #elif defined(CONFIG_CMDLINE_FORCE)
  932. strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  933. #else
  934. /* No arguments from boot loader, use kernel's cmdl*/
  935. if (!((char *)data)[0])
  936. strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  937. #endif
  938. #endif /* CONFIG_CMDLINE */
  939. pr_debug("Command line is: %s\n", (char*)data);
  940. /* break now */
  941. return 1;
  942. }
  943. #ifdef CONFIG_HAVE_MEMBLOCK
  944. #ifndef MIN_MEMBLOCK_ADDR
  945. #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
  946. #endif
  947. #ifndef MAX_MEMBLOCK_ADDR
  948. #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
  949. #endif
  950. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  951. {
  952. const u64 phys_offset = MIN_MEMBLOCK_ADDR;
  953. if (!PAGE_ALIGNED(base)) {
  954. if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
  955. pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
  956. base, base + size);
  957. return;
  958. }
  959. size -= PAGE_SIZE - (base & ~PAGE_MASK);
  960. base = PAGE_ALIGN(base);
  961. }
  962. size &= PAGE_MASK;
  963. if (base > MAX_MEMBLOCK_ADDR) {
  964. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  965. base, base + size);
  966. return;
  967. }
  968. if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
  969. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  970. ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
  971. size = MAX_MEMBLOCK_ADDR - base + 1;
  972. }
  973. if (base + size < phys_offset) {
  974. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  975. base, base + size);
  976. return;
  977. }
  978. if (base < phys_offset) {
  979. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  980. base, phys_offset);
  981. size -= phys_offset - base;
  982. base = phys_offset;
  983. }
  984. memblock_add(base, size);
  985. }
  986. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  987. phys_addr_t size, bool nomap)
  988. {
  989. if (nomap)
  990. return memblock_remove(base, size);
  991. return memblock_reserve(base, size);
  992. }
  993. /*
  994. * called from unflatten_device_tree() to bootstrap devicetree itself
  995. * Architectures can override this definition if memblock isn't used
  996. */
  997. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  998. {
  999. return __va(memblock_alloc(size, align));
  1000. }
  1001. #else
  1002. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  1003. {
  1004. WARN_ON(1);
  1005. }
  1006. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  1007. phys_addr_t size, bool nomap)
  1008. {
  1009. pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
  1010. &base, &size, nomap ? " (nomap)" : "");
  1011. return -ENOSYS;
  1012. }
  1013. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  1014. {
  1015. WARN_ON(1);
  1016. return NULL;
  1017. }
  1018. #endif
  1019. bool __init early_init_dt_verify(void *params)
  1020. {
  1021. if (!params)
  1022. return false;
  1023. /* check device tree validity */
  1024. if (fdt_check_header(params))
  1025. return false;
  1026. /* Setup flat device-tree pointer */
  1027. initial_boot_params = params;
  1028. of_fdt_crc32 = crc32_be(~0, initial_boot_params,
  1029. fdt_totalsize(initial_boot_params));
  1030. return true;
  1031. }
  1032. void __init early_init_dt_scan_nodes(void)
  1033. {
  1034. /* Retrieve various information from the /chosen node */
  1035. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  1036. /* Initialize {size,address}-cells info */
  1037. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1038. /* Setup memory, calling early_init_dt_add_memory_arch */
  1039. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1040. }
  1041. bool __init early_init_dt_scan(void *params)
  1042. {
  1043. bool status;
  1044. status = early_init_dt_verify(params);
  1045. if (!status)
  1046. return false;
  1047. early_init_dt_scan_nodes();
  1048. return true;
  1049. }
  1050. /**
  1051. * unflatten_device_tree - create tree of device_nodes from flat blob
  1052. *
  1053. * unflattens the device-tree passed by the firmware, creating the
  1054. * tree of struct device_node. It also fills the "name" and "type"
  1055. * pointers of the nodes so the normal device-tree walking functions
  1056. * can be used.
  1057. */
  1058. void __init unflatten_device_tree(void)
  1059. {
  1060. __unflatten_device_tree(initial_boot_params, NULL, &of_root,
  1061. early_init_dt_alloc_memory_arch, false);
  1062. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  1063. of_alias_scan(early_init_dt_alloc_memory_arch);
  1064. }
  1065. /**
  1066. * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
  1067. *
  1068. * Copies and unflattens the device-tree passed by the firmware, creating the
  1069. * tree of struct device_node. It also fills the "name" and "type"
  1070. * pointers of the nodes so the normal device-tree walking functions
  1071. * can be used. This should only be used when the FDT memory has not been
  1072. * reserved such is the case when the FDT is built-in to the kernel init
  1073. * section. If the FDT memory is reserved already then unflatten_device_tree
  1074. * should be used instead.
  1075. */
  1076. void __init unflatten_and_copy_device_tree(void)
  1077. {
  1078. int size;
  1079. void *dt;
  1080. if (!initial_boot_params) {
  1081. pr_warn("No valid device tree found, continuing without\n");
  1082. return;
  1083. }
  1084. size = fdt_totalsize(initial_boot_params);
  1085. dt = early_init_dt_alloc_memory_arch(size,
  1086. roundup_pow_of_two(FDT_V17_SIZE));
  1087. if (dt) {
  1088. memcpy(dt, initial_boot_params, size);
  1089. initial_boot_params = dt;
  1090. }
  1091. unflatten_device_tree();
  1092. }
  1093. #ifdef CONFIG_SYSFS
  1094. static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
  1095. struct bin_attribute *bin_attr,
  1096. char *buf, loff_t off, size_t count)
  1097. {
  1098. memcpy(buf, initial_boot_params + off, count);
  1099. return count;
  1100. }
  1101. static int __init of_fdt_raw_init(void)
  1102. {
  1103. static struct bin_attribute of_fdt_raw_attr =
  1104. __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
  1105. if (!initial_boot_params)
  1106. return 0;
  1107. if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
  1108. fdt_totalsize(initial_boot_params))) {
  1109. pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
  1110. return 0;
  1111. }
  1112. of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
  1113. return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
  1114. }
  1115. late_initcall(of_fdt_raw_init);
  1116. #endif
  1117. #endif /* CONFIG_OF_EARLY_FLATTREE */