fdt.c 29 KB

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