fdt.c 32 KB

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