fdt.c 29 KB

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