fdt.c 28 KB

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