fdt.c 32 KB

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