fdt.c 33 KB

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