fdt.c 30 KB

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