fdt.c 29 KB

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