fdt.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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_match - Return true if node matches a list of compatible values
  101. */
  102. int of_fdt_match(const void *blob, unsigned long node,
  103. const char *const *compat)
  104. {
  105. unsigned int tmp, score = 0;
  106. if (!compat)
  107. return 0;
  108. while (*compat) {
  109. tmp = of_fdt_is_compatible(blob, node, *compat);
  110. if (tmp && (score == 0 || (tmp < score)))
  111. score = tmp;
  112. compat++;
  113. }
  114. return score;
  115. }
  116. static void *unflatten_dt_alloc(void **mem, unsigned long size,
  117. unsigned long align)
  118. {
  119. void *res;
  120. *mem = PTR_ALIGN(*mem, align);
  121. res = *mem;
  122. *mem += size;
  123. return res;
  124. }
  125. /**
  126. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  127. * @blob: The parent device tree blob
  128. * @mem: Memory chunk to use for allocating device nodes and properties
  129. * @p: pointer to node in flat tree
  130. * @dad: Parent struct 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 **nodepp,
  138. unsigned long fpsize,
  139. bool dryrun)
  140. {
  141. const __be32 *p;
  142. struct device_node *np;
  143. struct property *pp, **prev_pp = NULL;
  144. const char *pathp;
  145. unsigned int l, allocl;
  146. static int depth = 0;
  147. int old_depth;
  148. int offset;
  149. int has_name = 0;
  150. int new_format = 0;
  151. pathp = fdt_get_name(blob, *poffset, &l);
  152. if (!pathp)
  153. return mem;
  154. allocl = l++;
  155. /* version 0x10 has a more compact unit name here instead of the full
  156. * path. we accumulate the full path size using "fpsize", we'll rebuild
  157. * it later. We detect this because the first character of the name is
  158. * not '/'.
  159. */
  160. if ((*pathp) != '/') {
  161. new_format = 1;
  162. if (fpsize == 0) {
  163. /* root node: special case. fpsize accounts for path
  164. * plus terminating zero. root node only has '/', so
  165. * fpsize should be 2, but we want to avoid the first
  166. * level nodes to have two '/' so we use fpsize 1 here
  167. */
  168. fpsize = 1;
  169. allocl = 2;
  170. l = 1;
  171. pathp = "";
  172. } else {
  173. /* account for '/' and path size minus terminal 0
  174. * already in 'l'
  175. */
  176. fpsize += l;
  177. allocl = fpsize;
  178. }
  179. }
  180. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  181. __alignof__(struct device_node));
  182. if (!dryrun) {
  183. char *fn;
  184. of_node_init(np);
  185. np->full_name = fn = ((char *)np) + sizeof(*np);
  186. if (new_format) {
  187. /* rebuild full path for new format */
  188. if (dad && dad->parent) {
  189. strcpy(fn, dad->full_name);
  190. #ifdef DEBUG
  191. if ((strlen(fn) + l + 1) != allocl) {
  192. pr_debug("%s: p: %d, l: %d, a: %d\n",
  193. pathp, (int)strlen(fn),
  194. l, allocl);
  195. }
  196. #endif
  197. fn += strlen(fn);
  198. }
  199. *(fn++) = '/';
  200. }
  201. memcpy(fn, pathp, l);
  202. prev_pp = &np->properties;
  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 (!dryrun) {
  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 (!dryrun) {
  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 (!dryrun) {
  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, NULL,
  299. fpsize, dryrun);
  300. if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
  301. pr_err("unflatten: error %d processing FDT\n", *poffset);
  302. if (nodepp)
  303. *nodepp = np;
  304. return mem;
  305. }
  306. /**
  307. * __unflatten_device_tree - create tree of device_nodes from flat blob
  308. *
  309. * unflattens a device-tree, creating the
  310. * tree of struct device_node. It also fills the "name" and "type"
  311. * pointers of the nodes so the normal device-tree walking functions
  312. * can be used.
  313. * @blob: The blob to expand
  314. * @mynodes: The device_node tree created by the call
  315. * @dt_alloc: An allocator that provides a virtual address to memory
  316. * for the resulting tree
  317. */
  318. static void __unflatten_device_tree(void *blob,
  319. struct device_node **mynodes,
  320. void * (*dt_alloc)(u64 size, u64 align))
  321. {
  322. unsigned long size;
  323. int start;
  324. void *mem;
  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, true);
  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, mynodes, 0, false);
  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. 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. static u32 of_fdt_crc32;
  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. else
  408. pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
  409. uname, &base, (unsigned long)size / SZ_1M);
  410. len -= t_len;
  411. if (first) {
  412. fdt_reserved_mem_save_node(node, uname, base, size);
  413. first = 0;
  414. }
  415. }
  416. return 0;
  417. }
  418. /**
  419. * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
  420. * in /reserved-memory matches the values supported by the current implementation,
  421. * also check if ranges property has been provided
  422. */
  423. static int __init __reserved_mem_check_root(unsigned long node)
  424. {
  425. const __be32 *prop;
  426. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  427. if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
  428. return -EINVAL;
  429. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  430. if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
  431. return -EINVAL;
  432. prop = of_get_flat_dt_prop(node, "ranges", NULL);
  433. if (!prop)
  434. return -EINVAL;
  435. return 0;
  436. }
  437. /**
  438. * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
  439. */
  440. static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
  441. int depth, void *data)
  442. {
  443. static int found;
  444. const char *status;
  445. int err;
  446. if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
  447. if (__reserved_mem_check_root(node) != 0) {
  448. pr_err("Reserved memory: unsupported node format, ignoring\n");
  449. /* break scan */
  450. return 1;
  451. }
  452. found = 1;
  453. /* scan next node */
  454. return 0;
  455. } else if (!found) {
  456. /* scan next node */
  457. return 0;
  458. } else if (found && depth < 2) {
  459. /* scanning of /reserved-memory has been finished */
  460. return 1;
  461. }
  462. status = of_get_flat_dt_prop(node, "status", NULL);
  463. if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
  464. return 0;
  465. err = __reserved_mem_reserve_reg(node, uname);
  466. if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
  467. fdt_reserved_mem_save_node(node, uname, 0, 0);
  468. /* scan next node */
  469. return 0;
  470. }
  471. /**
  472. * early_init_fdt_scan_reserved_mem() - create reserved memory regions
  473. *
  474. * This function grabs memory from early allocator for device exclusive use
  475. * defined in device tree structures. It should be called by arch specific code
  476. * once the early allocator (i.e. memblock) has been fully activated.
  477. */
  478. void __init early_init_fdt_scan_reserved_mem(void)
  479. {
  480. int n;
  481. u64 base, size;
  482. if (!initial_boot_params)
  483. return;
  484. /* Reserve the dtb region */
  485. early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
  486. fdt_totalsize(initial_boot_params),
  487. 0);
  488. /* Process header /memreserve/ fields */
  489. for (n = 0; ; n++) {
  490. fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
  491. if (!size)
  492. break;
  493. early_init_dt_reserve_memory_arch(base, size, 0);
  494. }
  495. of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
  496. fdt_init_reserved_mem();
  497. }
  498. /**
  499. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  500. * @it: callback function
  501. * @data: context data pointer
  502. *
  503. * This function is used to scan the flattened device-tree, it is
  504. * used to extract the memory information at boot before we can
  505. * unflatten the tree
  506. */
  507. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  508. const char *uname, int depth,
  509. void *data),
  510. void *data)
  511. {
  512. const void *blob = initial_boot_params;
  513. const char *pathp;
  514. int offset, rc = 0, depth = -1;
  515. for (offset = fdt_next_node(blob, -1, &depth);
  516. offset >= 0 && depth >= 0 && !rc;
  517. offset = fdt_next_node(blob, offset, &depth)) {
  518. pathp = fdt_get_name(blob, offset, NULL);
  519. if (*pathp == '/')
  520. pathp = kbasename(pathp);
  521. rc = it(offset, pathp, depth, data);
  522. }
  523. return rc;
  524. }
  525. /**
  526. * of_get_flat_dt_root - find the root node in the flat blob
  527. */
  528. unsigned long __init of_get_flat_dt_root(void)
  529. {
  530. return 0;
  531. }
  532. /**
  533. * of_get_flat_dt_size - Return the total size of the FDT
  534. */
  535. int __init of_get_flat_dt_size(void)
  536. {
  537. return fdt_totalsize(initial_boot_params);
  538. }
  539. /**
  540. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  541. *
  542. * This function can be used within scan_flattened_dt callback to get
  543. * access to properties
  544. */
  545. const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  546. int *size)
  547. {
  548. return fdt_getprop(initial_boot_params, node, name, size);
  549. }
  550. /**
  551. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  552. * @node: node to test
  553. * @compat: compatible string to compare with compatible list.
  554. */
  555. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  556. {
  557. return of_fdt_is_compatible(initial_boot_params, node, compat);
  558. }
  559. /**
  560. * of_flat_dt_match - Return true if node matches a list of compatible values
  561. */
  562. int __init of_flat_dt_match(unsigned long node, const char *const *compat)
  563. {
  564. return of_fdt_match(initial_boot_params, node, compat);
  565. }
  566. struct fdt_scan_status {
  567. const char *name;
  568. int namelen;
  569. int depth;
  570. int found;
  571. int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
  572. void *data;
  573. };
  574. const char * __init of_flat_dt_get_machine_name(void)
  575. {
  576. const char *name;
  577. unsigned long dt_root = of_get_flat_dt_root();
  578. name = of_get_flat_dt_prop(dt_root, "model", NULL);
  579. if (!name)
  580. name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  581. return name;
  582. }
  583. /**
  584. * of_flat_dt_match_machine - Iterate match tables to find matching machine.
  585. *
  586. * @default_match: A machine specific ptr to return in case of no match.
  587. * @get_next_compat: callback function to return next compatible match table.
  588. *
  589. * Iterate through machine match tables to find the best match for the machine
  590. * compatible string in the FDT.
  591. */
  592. const void * __init of_flat_dt_match_machine(const void *default_match,
  593. const void * (*get_next_compat)(const char * const**))
  594. {
  595. const void *data = NULL;
  596. const void *best_data = default_match;
  597. const char *const *compat;
  598. unsigned long dt_root;
  599. unsigned int best_score = ~1, score = 0;
  600. dt_root = of_get_flat_dt_root();
  601. while ((data = get_next_compat(&compat))) {
  602. score = of_flat_dt_match(dt_root, compat);
  603. if (score > 0 && score < best_score) {
  604. best_data = data;
  605. best_score = score;
  606. }
  607. }
  608. if (!best_data) {
  609. const char *prop;
  610. int size;
  611. pr_err("\n unrecognized device tree list:\n[ ");
  612. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  613. if (prop) {
  614. while (size > 0) {
  615. printk("'%s' ", prop);
  616. size -= strlen(prop) + 1;
  617. prop += strlen(prop) + 1;
  618. }
  619. }
  620. printk("]\n\n");
  621. return NULL;
  622. }
  623. pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
  624. return best_data;
  625. }
  626. #ifdef CONFIG_BLK_DEV_INITRD
  627. /**
  628. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  629. * @node: reference to node containing initrd location ('chosen')
  630. */
  631. static void __init early_init_dt_check_for_initrd(unsigned long node)
  632. {
  633. u64 start, end;
  634. int len;
  635. const __be32 *prop;
  636. pr_debug("Looking for initrd properties... ");
  637. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  638. if (!prop)
  639. return;
  640. start = of_read_number(prop, len/4);
  641. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  642. if (!prop)
  643. return;
  644. end = of_read_number(prop, len/4);
  645. initrd_start = (unsigned long)__va(start);
  646. initrd_end = (unsigned long)__va(end);
  647. initrd_below_start_ok = 1;
  648. pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
  649. (unsigned long long)start, (unsigned long long)end);
  650. }
  651. #else
  652. static inline void early_init_dt_check_for_initrd(unsigned long node)
  653. {
  654. }
  655. #endif /* CONFIG_BLK_DEV_INITRD */
  656. #ifdef CONFIG_SERIAL_EARLYCON
  657. extern struct of_device_id __earlycon_of_table[];
  658. int __init early_init_dt_scan_chosen_serial(void)
  659. {
  660. int offset;
  661. const char *p;
  662. int l;
  663. const struct of_device_id *match = __earlycon_of_table;
  664. const void *fdt = initial_boot_params;
  665. offset = fdt_path_offset(fdt, "/chosen");
  666. if (offset < 0)
  667. offset = fdt_path_offset(fdt, "/chosen@0");
  668. if (offset < 0)
  669. return -ENOENT;
  670. p = fdt_getprop(fdt, offset, "stdout-path", &l);
  671. if (!p)
  672. p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
  673. if (!p || !l)
  674. return -ENOENT;
  675. /* Get the node specified by stdout-path */
  676. offset = fdt_path_offset(fdt, p);
  677. if (offset < 0)
  678. return -ENODEV;
  679. while (match->compatible[0]) {
  680. unsigned long addr;
  681. if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
  682. match++;
  683. continue;
  684. }
  685. addr = fdt_translate_address(fdt, offset);
  686. if (!addr)
  687. return -ENXIO;
  688. of_setup_earlycon(addr, match->data);
  689. return 0;
  690. }
  691. return -ENODEV;
  692. }
  693. static int __init setup_of_earlycon(char *buf)
  694. {
  695. if (buf)
  696. return 0;
  697. return early_init_dt_scan_chosen_serial();
  698. }
  699. early_param("earlycon", setup_of_earlycon);
  700. #endif
  701. /**
  702. * early_init_dt_scan_root - fetch the top level address and size cells
  703. */
  704. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  705. int depth, void *data)
  706. {
  707. const __be32 *prop;
  708. if (depth != 0)
  709. return 0;
  710. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  711. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  712. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  713. if (prop)
  714. dt_root_size_cells = be32_to_cpup(prop);
  715. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  716. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  717. if (prop)
  718. dt_root_addr_cells = be32_to_cpup(prop);
  719. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  720. /* break now */
  721. return 1;
  722. }
  723. u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
  724. {
  725. const __be32 *p = *cellp;
  726. *cellp = p + s;
  727. return of_read_number(p, s);
  728. }
  729. /**
  730. * early_init_dt_scan_memory - Look for an parse memory nodes
  731. */
  732. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  733. int depth, void *data)
  734. {
  735. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  736. const __be32 *reg, *endp;
  737. int l;
  738. /* We are scanning "memory" nodes only */
  739. if (type == NULL) {
  740. /*
  741. * The longtrail doesn't have a device_type on the
  742. * /memory node, so look for the node called /memory@0.
  743. */
  744. if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
  745. return 0;
  746. } else if (strcmp(type, "memory") != 0)
  747. return 0;
  748. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  749. if (reg == NULL)
  750. reg = of_get_flat_dt_prop(node, "reg", &l);
  751. if (reg == NULL)
  752. return 0;
  753. endp = reg + (l / sizeof(__be32));
  754. pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
  755. uname, l, reg[0], reg[1], reg[2], reg[3]);
  756. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  757. u64 base, size;
  758. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  759. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  760. if (size == 0)
  761. continue;
  762. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  763. (unsigned long long)size);
  764. early_init_dt_add_memory_arch(base, size);
  765. }
  766. return 0;
  767. }
  768. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  769. int depth, void *data)
  770. {
  771. int l;
  772. const char *p;
  773. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  774. if (depth != 1 || !data ||
  775. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  776. return 0;
  777. early_init_dt_check_for_initrd(node);
  778. /* Retrieve command line */
  779. p = of_get_flat_dt_prop(node, "bootargs", &l);
  780. if (p != NULL && l > 0)
  781. strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  782. /*
  783. * CONFIG_CMDLINE is meant to be a default in case nothing else
  784. * managed to set the command line, unless CONFIG_CMDLINE_FORCE
  785. * is set in which case we override whatever was found earlier.
  786. */
  787. #ifdef CONFIG_CMDLINE
  788. #ifndef CONFIG_CMDLINE_FORCE
  789. if (!((char *)data)[0])
  790. #endif
  791. strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  792. #endif /* CONFIG_CMDLINE */
  793. pr_debug("Command line is: %s\n", (char*)data);
  794. /* break now */
  795. return 1;
  796. }
  797. #ifdef CONFIG_HAVE_MEMBLOCK
  798. #define MAX_PHYS_ADDR ((phys_addr_t)~0)
  799. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  800. {
  801. const u64 phys_offset = __pa(PAGE_OFFSET);
  802. if (!PAGE_ALIGNED(base)) {
  803. if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
  804. pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
  805. base, base + size);
  806. return;
  807. }
  808. size -= PAGE_SIZE - (base & ~PAGE_MASK);
  809. base = PAGE_ALIGN(base);
  810. }
  811. size &= PAGE_MASK;
  812. if (base > MAX_PHYS_ADDR) {
  813. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  814. base, base + size);
  815. return;
  816. }
  817. if (base + size - 1 > MAX_PHYS_ADDR) {
  818. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  819. ((u64)MAX_PHYS_ADDR) + 1, base + size);
  820. size = MAX_PHYS_ADDR - base + 1;
  821. }
  822. if (base + size < phys_offset) {
  823. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  824. base, base + size);
  825. return;
  826. }
  827. if (base < phys_offset) {
  828. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  829. base, phys_offset);
  830. size -= phys_offset - base;
  831. base = phys_offset;
  832. }
  833. memblock_add(base, size);
  834. }
  835. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  836. phys_addr_t size, bool nomap)
  837. {
  838. if (memblock_is_region_reserved(base, size))
  839. return -EBUSY;
  840. if (nomap)
  841. return memblock_remove(base, size);
  842. return memblock_reserve(base, size);
  843. }
  844. /*
  845. * called from unflatten_device_tree() to bootstrap devicetree itself
  846. * Architectures can override this definition if memblock isn't used
  847. */
  848. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  849. {
  850. return __va(memblock_alloc(size, align));
  851. }
  852. #else
  853. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  854. phys_addr_t size, bool nomap)
  855. {
  856. pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
  857. &base, &size, nomap ? " (nomap)" : "");
  858. return -ENOSYS;
  859. }
  860. #endif
  861. bool __init early_init_dt_verify(void *params)
  862. {
  863. if (!params)
  864. return false;
  865. /* check device tree validity */
  866. if (fdt_check_header(params))
  867. return false;
  868. /* Setup flat device-tree pointer */
  869. initial_boot_params = params;
  870. of_fdt_crc32 = crc32_be(~0, initial_boot_params,
  871. fdt_totalsize(initial_boot_params));
  872. return true;
  873. }
  874. void __init early_init_dt_scan_nodes(void)
  875. {
  876. /* Retrieve various information from the /chosen node */
  877. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  878. /* Initialize {size,address}-cells info */
  879. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  880. /* Setup memory, calling early_init_dt_add_memory_arch */
  881. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  882. }
  883. bool __init early_init_dt_scan(void *params)
  884. {
  885. bool status;
  886. status = early_init_dt_verify(params);
  887. if (!status)
  888. return false;
  889. early_init_dt_scan_nodes();
  890. return true;
  891. }
  892. /**
  893. * unflatten_device_tree - create tree of device_nodes from flat blob
  894. *
  895. * unflattens the device-tree passed by the firmware, creating the
  896. * tree of struct device_node. It also fills the "name" and "type"
  897. * pointers of the nodes so the normal device-tree walking functions
  898. * can be used.
  899. */
  900. void __init unflatten_device_tree(void)
  901. {
  902. __unflatten_device_tree(initial_boot_params, &of_root,
  903. early_init_dt_alloc_memory_arch);
  904. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  905. of_alias_scan(early_init_dt_alloc_memory_arch);
  906. }
  907. /**
  908. * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
  909. *
  910. * Copies and unflattens the device-tree passed by the firmware, creating the
  911. * tree of struct device_node. It also fills the "name" and "type"
  912. * pointers of the nodes so the normal device-tree walking functions
  913. * can be used. This should only be used when the FDT memory has not been
  914. * reserved such is the case when the FDT is built-in to the kernel init
  915. * section. If the FDT memory is reserved already then unflatten_device_tree
  916. * should be used instead.
  917. */
  918. void __init unflatten_and_copy_device_tree(void)
  919. {
  920. int size;
  921. void *dt;
  922. if (!initial_boot_params) {
  923. pr_warn("No valid device tree found, continuing without\n");
  924. return;
  925. }
  926. size = fdt_totalsize(initial_boot_params);
  927. dt = early_init_dt_alloc_memory_arch(size,
  928. roundup_pow_of_two(FDT_V17_SIZE));
  929. if (dt) {
  930. memcpy(dt, initial_boot_params, size);
  931. initial_boot_params = dt;
  932. }
  933. unflatten_device_tree();
  934. }
  935. #ifdef CONFIG_SYSFS
  936. static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
  937. struct bin_attribute *bin_attr,
  938. char *buf, loff_t off, size_t count)
  939. {
  940. memcpy(buf, initial_boot_params + off, count);
  941. return count;
  942. }
  943. static int __init of_fdt_raw_init(void)
  944. {
  945. static struct bin_attribute of_fdt_raw_attr =
  946. __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
  947. if (!initial_boot_params)
  948. return 0;
  949. if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
  950. fdt_totalsize(initial_boot_params))) {
  951. pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
  952. return 0;
  953. }
  954. of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
  955. return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
  956. }
  957. late_initcall(of_fdt_raw_init);
  958. #endif
  959. #endif /* CONFIG_OF_EARLY_FLATTREE */