fdt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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 (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. #define MAX_PHYS_ADDR ((phys_addr_t)~0)
  798. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  799. {
  800. const u64 phys_offset = __pa(PAGE_OFFSET);
  801. if (!PAGE_ALIGNED(base)) {
  802. size -= PAGE_SIZE - (base & ~PAGE_MASK);
  803. base = PAGE_ALIGN(base);
  804. }
  805. size &= PAGE_MASK;
  806. if (base > MAX_PHYS_ADDR) {
  807. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  808. base, base + size);
  809. return;
  810. }
  811. if (base + size - 1 > MAX_PHYS_ADDR) {
  812. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  813. ((u64)MAX_PHYS_ADDR) + 1, base + size);
  814. size = MAX_PHYS_ADDR - base + 1;
  815. }
  816. if (base + size < phys_offset) {
  817. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  818. base, base + size);
  819. return;
  820. }
  821. if (base < phys_offset) {
  822. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  823. base, phys_offset);
  824. size -= phys_offset - base;
  825. base = phys_offset;
  826. }
  827. memblock_add(base, size);
  828. }
  829. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  830. phys_addr_t size, bool nomap)
  831. {
  832. if (memblock_is_region_reserved(base, size))
  833. return -EBUSY;
  834. if (nomap)
  835. return memblock_remove(base, size);
  836. return memblock_reserve(base, size);
  837. }
  838. /*
  839. * called from unflatten_device_tree() to bootstrap devicetree itself
  840. * Architectures can override this definition if memblock isn't used
  841. */
  842. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  843. {
  844. return __va(memblock_alloc(size, align));
  845. }
  846. #else
  847. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  848. phys_addr_t size, bool nomap)
  849. {
  850. pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
  851. &base, &size, nomap ? " (nomap)" : "");
  852. return -ENOSYS;
  853. }
  854. #endif
  855. bool __init early_init_dt_verify(void *params)
  856. {
  857. if (!params)
  858. return false;
  859. /* Setup flat device-tree pointer */
  860. initial_boot_params = params;
  861. /* check device tree validity */
  862. if (fdt_check_header(params)) {
  863. initial_boot_params = NULL;
  864. return false;
  865. }
  866. return true;
  867. }
  868. void __init early_init_dt_scan_nodes(void)
  869. {
  870. /* Retrieve various information from the /chosen node */
  871. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  872. /* Initialize {size,address}-cells info */
  873. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  874. /* Setup memory, calling early_init_dt_add_memory_arch */
  875. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  876. }
  877. bool __init early_init_dt_scan(void *params)
  878. {
  879. bool status;
  880. status = early_init_dt_verify(params);
  881. if (!status)
  882. return false;
  883. early_init_dt_scan_nodes();
  884. return true;
  885. }
  886. /**
  887. * unflatten_device_tree - create tree of device_nodes from flat blob
  888. *
  889. * unflattens the device-tree passed by the firmware, creating the
  890. * tree of struct device_node. It also fills the "name" and "type"
  891. * pointers of the nodes so the normal device-tree walking functions
  892. * can be used.
  893. */
  894. void __init unflatten_device_tree(void)
  895. {
  896. __unflatten_device_tree(initial_boot_params, &of_allnodes,
  897. early_init_dt_alloc_memory_arch);
  898. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  899. of_alias_scan(early_init_dt_alloc_memory_arch);
  900. }
  901. /**
  902. * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
  903. *
  904. * Copies and unflattens the device-tree passed by the firmware, creating the
  905. * tree of struct device_node. It also fills the "name" and "type"
  906. * pointers of the nodes so the normal device-tree walking functions
  907. * can be used. This should only be used when the FDT memory has not been
  908. * reserved such is the case when the FDT is built-in to the kernel init
  909. * section. If the FDT memory is reserved already then unflatten_device_tree
  910. * should be used instead.
  911. */
  912. void __init unflatten_and_copy_device_tree(void)
  913. {
  914. int size;
  915. void *dt;
  916. if (!initial_boot_params) {
  917. pr_warn("No valid device tree found, continuing without\n");
  918. return;
  919. }
  920. size = fdt_totalsize(initial_boot_params);
  921. dt = early_init_dt_alloc_memory_arch(size,
  922. roundup_pow_of_two(FDT_V17_SIZE));
  923. if (dt) {
  924. memcpy(dt, initial_boot_params, size);
  925. initial_boot_params = dt;
  926. }
  927. unflatten_device_tree();
  928. }
  929. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  930. static struct debugfs_blob_wrapper flat_dt_blob;
  931. static int __init of_flat_dt_debugfs_export_fdt(void)
  932. {
  933. struct dentry *d = debugfs_create_dir("device-tree", NULL);
  934. if (!d)
  935. return -ENOENT;
  936. flat_dt_blob.data = initial_boot_params;
  937. flat_dt_blob.size = fdt_totalsize(initial_boot_params);
  938. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  939. d, &flat_dt_blob);
  940. if (!d)
  941. return -ENOENT;
  942. return 0;
  943. }
  944. module_init(of_flat_dt_debugfs_export_fdt);
  945. #endif
  946. #endif /* CONFIG_OF_EARLY_FLATTREE */