prom.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <stdarg.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/threads.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/stringify.h>
  24. #include <linux/delay.h>
  25. #include <linux/initrd.h>
  26. #include <linux/bitops.h>
  27. #include <linux/module.h>
  28. #include <linux/kexec.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/irq.h>
  31. #include <linux/lmb.h>
  32. #include <asm/prom.h>
  33. #include <asm/page.h>
  34. #include <asm/processor.h>
  35. #include <asm/irq.h>
  36. #include <linux/io.h>
  37. #include <asm/system.h>
  38. #include <asm/mmu.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/sections.h>
  41. #include <asm/pci-bridge.h>
  42. static int __initdata dt_root_addr_cells;
  43. static int __initdata dt_root_size_cells;
  44. typedef u32 cell_t;
  45. static struct boot_param_header *initial_boot_params;
  46. /* export that to outside world */
  47. struct device_node *of_chosen;
  48. static inline char *find_flat_dt_string(u32 offset)
  49. {
  50. return ((char *)initial_boot_params) +
  51. initial_boot_params->off_dt_strings + offset;
  52. }
  53. /**
  54. * This function is used to scan the flattened device-tree, it is
  55. * used to extract the memory informations at boot before we can
  56. * unflatten the tree
  57. */
  58. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  59. const char *uname, int depth,
  60. void *data),
  61. void *data)
  62. {
  63. unsigned long p = ((unsigned long)initial_boot_params) +
  64. initial_boot_params->off_dt_struct;
  65. int rc = 0;
  66. int depth = -1;
  67. do {
  68. u32 tag = *((u32 *)p);
  69. char *pathp;
  70. p += 4;
  71. if (tag == OF_DT_END_NODE) {
  72. depth--;
  73. continue;
  74. }
  75. if (tag == OF_DT_NOP)
  76. continue;
  77. if (tag == OF_DT_END)
  78. break;
  79. if (tag == OF_DT_PROP) {
  80. u32 sz = *((u32 *)p);
  81. p += 8;
  82. if (initial_boot_params->version < 0x10)
  83. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  84. p += sz;
  85. p = _ALIGN(p, 4);
  86. continue;
  87. }
  88. if (tag != OF_DT_BEGIN_NODE) {
  89. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  90. " device tree !\n", tag);
  91. return -EINVAL;
  92. }
  93. depth++;
  94. pathp = (char *)p;
  95. p = _ALIGN(p + strlen(pathp) + 1, 4);
  96. if ((*pathp) == '/') {
  97. char *lp, *np;
  98. for (lp = NULL, np = pathp; *np; np++)
  99. if ((*np) == '/')
  100. lp = np+1;
  101. if (lp != NULL)
  102. pathp = lp;
  103. }
  104. rc = it(p, pathp, depth, data);
  105. if (rc != 0)
  106. break;
  107. } while (1);
  108. return rc;
  109. }
  110. unsigned long __init of_get_flat_dt_root(void)
  111. {
  112. unsigned long p = ((unsigned long)initial_boot_params) +
  113. initial_boot_params->off_dt_struct;
  114. while (*((u32 *)p) == OF_DT_NOP)
  115. p += 4;
  116. BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
  117. p += 4;
  118. return _ALIGN(p + strlen((char *)p) + 1, 4);
  119. }
  120. /**
  121. * This function can be used within scan_flattened_dt callback to get
  122. * access to properties
  123. */
  124. void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  125. unsigned long *size)
  126. {
  127. unsigned long p = node;
  128. do {
  129. u32 tag = *((u32 *)p);
  130. u32 sz, noff;
  131. const char *nstr;
  132. p += 4;
  133. if (tag == OF_DT_NOP)
  134. continue;
  135. if (tag != OF_DT_PROP)
  136. return NULL;
  137. sz = *((u32 *)p);
  138. noff = *((u32 *)(p + 4));
  139. p += 8;
  140. if (initial_boot_params->version < 0x10)
  141. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  142. nstr = find_flat_dt_string(noff);
  143. if (nstr == NULL) {
  144. printk(KERN_WARNING "Can't find property index"
  145. " name !\n");
  146. return NULL;
  147. }
  148. if (strcmp(name, nstr) == 0) {
  149. if (size)
  150. *size = sz;
  151. return (void *)p;
  152. }
  153. p += sz;
  154. p = _ALIGN(p, 4);
  155. } while (1);
  156. }
  157. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  158. {
  159. const char *cp;
  160. unsigned long cplen, l;
  161. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  162. if (cp == NULL)
  163. return 0;
  164. while (cplen > 0) {
  165. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  166. return 1;
  167. l = strlen(cp) + 1;
  168. cp += l;
  169. cplen -= l;
  170. }
  171. return 0;
  172. }
  173. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  174. unsigned long align)
  175. {
  176. void *res;
  177. *mem = _ALIGN(*mem, align);
  178. res = (void *)*mem;
  179. *mem += size;
  180. return res;
  181. }
  182. static unsigned long __init unflatten_dt_node(unsigned long mem,
  183. unsigned long *p,
  184. struct device_node *dad,
  185. struct device_node ***allnextpp,
  186. unsigned long fpsize)
  187. {
  188. struct device_node *np;
  189. struct property *pp, **prev_pp = NULL;
  190. char *pathp;
  191. u32 tag;
  192. unsigned int l, allocl;
  193. int has_name = 0;
  194. int new_format = 0;
  195. tag = *((u32 *)(*p));
  196. if (tag != OF_DT_BEGIN_NODE) {
  197. printk("Weird tag at start of node: %x\n", tag);
  198. return mem;
  199. }
  200. *p += 4;
  201. pathp = (char *)*p;
  202. l = allocl = strlen(pathp) + 1;
  203. *p = _ALIGN(*p + l, 4);
  204. /* version 0x10 has a more compact unit name here instead of the full
  205. * path. we accumulate the full path size using "fpsize", we'll rebuild
  206. * it later. We detect this because the first character of the name is
  207. * not '/'.
  208. */
  209. if ((*pathp) != '/') {
  210. new_format = 1;
  211. if (fpsize == 0) {
  212. /* root node: special case. fpsize accounts for path
  213. * plus terminating zero. root node only has '/', so
  214. * fpsize should be 2, but we want to avoid the first
  215. * level nodes to have two '/' so we use fpsize 1 here
  216. */
  217. fpsize = 1;
  218. allocl = 2;
  219. } else {
  220. /* account for '/' and path size minus terminal 0
  221. * already in 'l'
  222. */
  223. fpsize += l;
  224. allocl = fpsize;
  225. }
  226. }
  227. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  228. __alignof__(struct device_node));
  229. if (allnextpp) {
  230. memset(np, 0, sizeof(*np));
  231. np->full_name = ((char *)np) + sizeof(struct device_node);
  232. if (new_format) {
  233. char *p2 = np->full_name;
  234. /* rebuild full path for new format */
  235. if (dad && dad->parent) {
  236. strcpy(p2, dad->full_name);
  237. #ifdef DEBUG
  238. if ((strlen(p2) + l + 1) != allocl) {
  239. pr_debug("%s: p: %d, l: %d, a: %d\n",
  240. pathp, (int)strlen(p2),
  241. l, allocl);
  242. }
  243. #endif
  244. p2 += strlen(p2);
  245. }
  246. *(p2++) = '/';
  247. memcpy(p2, pathp, l);
  248. } else
  249. memcpy(np->full_name, pathp, l);
  250. prev_pp = &np->properties;
  251. **allnextpp = np;
  252. *allnextpp = &np->allnext;
  253. if (dad != NULL) {
  254. np->parent = dad;
  255. /* we temporarily use the next field as `last_child'*/
  256. if (dad->next == NULL)
  257. dad->child = np;
  258. else
  259. dad->next->sibling = np;
  260. dad->next = np;
  261. }
  262. kref_init(&np->kref);
  263. }
  264. while (1) {
  265. u32 sz, noff;
  266. char *pname;
  267. tag = *((u32 *)(*p));
  268. if (tag == OF_DT_NOP) {
  269. *p += 4;
  270. continue;
  271. }
  272. if (tag != OF_DT_PROP)
  273. break;
  274. *p += 4;
  275. sz = *((u32 *)(*p));
  276. noff = *((u32 *)((*p) + 4));
  277. *p += 8;
  278. if (initial_boot_params->version < 0x10)
  279. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  280. pname = find_flat_dt_string(noff);
  281. if (pname == NULL) {
  282. printk(KERN_INFO
  283. "Can't find property name in list !\n");
  284. break;
  285. }
  286. if (strcmp(pname, "name") == 0)
  287. has_name = 1;
  288. l = strlen(pname) + 1;
  289. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  290. __alignof__(struct property));
  291. if (allnextpp) {
  292. if (strcmp(pname, "linux,phandle") == 0) {
  293. np->node = *((u32 *)*p);
  294. if (np->linux_phandle == 0)
  295. np->linux_phandle = np->node;
  296. }
  297. if (strcmp(pname, "ibm,phandle") == 0)
  298. np->linux_phandle = *((u32 *)*p);
  299. pp->name = pname;
  300. pp->length = sz;
  301. pp->value = (void *)*p;
  302. *prev_pp = pp;
  303. prev_pp = &pp->next;
  304. }
  305. *p = _ALIGN((*p) + sz, 4);
  306. }
  307. /* with version 0x10 we may not have the name property, recreate
  308. * it here from the unit name if absent
  309. */
  310. if (!has_name) {
  311. char *p1 = pathp, *ps = pathp, *pa = NULL;
  312. int sz;
  313. while (*p1) {
  314. if ((*p1) == '@')
  315. pa = p1;
  316. if ((*p1) == '/')
  317. ps = p1 + 1;
  318. p1++;
  319. }
  320. if (pa < ps)
  321. pa = p1;
  322. sz = (pa - ps) + 1;
  323. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  324. __alignof__(struct property));
  325. if (allnextpp) {
  326. pp->name = "name";
  327. pp->length = sz;
  328. pp->value = pp + 1;
  329. *prev_pp = pp;
  330. prev_pp = &pp->next;
  331. memcpy(pp->value, ps, sz - 1);
  332. ((char *)pp->value)[sz - 1] = 0;
  333. pr_debug("fixed up name for %s -> %s\n", pathp,
  334. (char *)pp->value);
  335. }
  336. }
  337. if (allnextpp) {
  338. *prev_pp = NULL;
  339. np->name = of_get_property(np, "name", NULL);
  340. np->type = of_get_property(np, "device_type", NULL);
  341. if (!np->name)
  342. np->name = "<NULL>";
  343. if (!np->type)
  344. np->type = "<NULL>";
  345. }
  346. while (tag == OF_DT_BEGIN_NODE) {
  347. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  348. tag = *((u32 *)(*p));
  349. }
  350. if (tag != OF_DT_END_NODE) {
  351. printk(KERN_INFO "Weird tag at end of node: %x\n", tag);
  352. return mem;
  353. }
  354. *p += 4;
  355. return mem;
  356. }
  357. /**
  358. * unflattens the device-tree passed by the firmware, creating the
  359. * tree of struct device_node. It also fills the "name" and "type"
  360. * pointers of the nodes so the normal device-tree walking functions
  361. * can be used (this used to be done by finish_device_tree)
  362. */
  363. void __init unflatten_device_tree(void)
  364. {
  365. unsigned long start, mem, size;
  366. struct device_node **allnextp = &allnodes;
  367. pr_debug(" -> unflatten_device_tree()\n");
  368. /* First pass, scan for size */
  369. start = ((unsigned long)initial_boot_params) +
  370. initial_boot_params->off_dt_struct;
  371. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  372. size = (size | 3) + 1;
  373. pr_debug(" size is %lx, allocating...\n", size);
  374. /* Allocate memory for the expanded device tree */
  375. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  376. mem = (unsigned long) __va(mem);
  377. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  378. pr_debug(" unflattening %lx...\n", mem);
  379. /* Second pass, do actual unflattening */
  380. start = ((unsigned long)initial_boot_params) +
  381. initial_boot_params->off_dt_struct;
  382. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  383. if (*((u32 *)start) != OF_DT_END)
  384. printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
  385. *((u32 *)start));
  386. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  387. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  388. ((u32 *)mem)[size / 4]);
  389. *allnextp = NULL;
  390. /* Get pointer to OF "/chosen" node for use everywhere */
  391. of_chosen = of_find_node_by_path("/chosen");
  392. if (of_chosen == NULL)
  393. of_chosen = of_find_node_by_path("/chosen@0");
  394. pr_debug(" <- unflatten_device_tree()\n");
  395. }
  396. #define early_init_dt_scan_drconf_memory(node) 0
  397. static int __init early_init_dt_scan_cpus(unsigned long node,
  398. const char *uname, int depth,
  399. void *data)
  400. {
  401. static int logical_cpuid;
  402. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  403. const u32 *intserv;
  404. int i, nthreads;
  405. int found = 0;
  406. /* We are scanning "cpu" nodes only */
  407. if (type == NULL || strcmp(type, "cpu") != 0)
  408. return 0;
  409. /* Get physical cpuid */
  410. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  411. nthreads = 1;
  412. /*
  413. * Now see if any of these threads match our boot cpu.
  414. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  415. */
  416. for (i = 0; i < nthreads; i++) {
  417. /*
  418. * version 2 of the kexec param format adds the phys cpuid of
  419. * booted proc.
  420. */
  421. if (initial_boot_params && initial_boot_params->version >= 2) {
  422. if (intserv[i] ==
  423. initial_boot_params->boot_cpuid_phys) {
  424. found = 1;
  425. break;
  426. }
  427. } else {
  428. /*
  429. * Check if it's the boot-cpu, set it's hw index now,
  430. * unfortunately this format did not support booting
  431. * off secondary threads.
  432. */
  433. if (of_get_flat_dt_prop(node,
  434. "linux,boot-cpu", NULL) != NULL) {
  435. found = 1;
  436. break;
  437. }
  438. }
  439. #ifdef CONFIG_SMP
  440. /* logical cpu id is always 0 on UP kernels */
  441. logical_cpuid++;
  442. #endif
  443. }
  444. if (found) {
  445. pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
  446. intserv[i]);
  447. boot_cpuid = logical_cpuid;
  448. }
  449. return 0;
  450. }
  451. #ifdef CONFIG_BLK_DEV_INITRD
  452. static void __init early_init_dt_check_for_initrd(unsigned long node)
  453. {
  454. unsigned long l;
  455. u32 *prop;
  456. pr_debug("Looking for initrd properties... ");
  457. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  458. if (prop) {
  459. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  460. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  461. if (prop) {
  462. initrd_end = (unsigned long)
  463. __va(of_read_ulong(prop, l/4));
  464. initrd_below_start_ok = 1;
  465. } else {
  466. initrd_start = 0;
  467. }
  468. }
  469. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
  470. initrd_start, initrd_end);
  471. }
  472. #else
  473. static inline void early_init_dt_check_for_initrd(unsigned long node)
  474. {
  475. }
  476. #endif /* CONFIG_BLK_DEV_INITRD */
  477. static int __init early_init_dt_scan_chosen(unsigned long node,
  478. const char *uname, int depth, void *data)
  479. {
  480. unsigned long l;
  481. char *p;
  482. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  483. if (depth != 1 ||
  484. (strcmp(uname, "chosen") != 0 &&
  485. strcmp(uname, "chosen@0") != 0))
  486. return 0;
  487. #ifdef CONFIG_KEXEC
  488. lprop = (u64 *)of_get_flat_dt_prop(node,
  489. "linux,crashkernel-base", NULL);
  490. if (lprop)
  491. crashk_res.start = *lprop;
  492. lprop = (u64 *)of_get_flat_dt_prop(node,
  493. "linux,crashkernel-size", NULL);
  494. if (lprop)
  495. crashk_res.end = crashk_res.start + *lprop - 1;
  496. #endif
  497. early_init_dt_check_for_initrd(node);
  498. /* Retreive command line */
  499. p = of_get_flat_dt_prop(node, "bootargs", &l);
  500. if (p != NULL && l > 0)
  501. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  502. #ifdef CONFIG_CMDLINE
  503. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  504. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  505. #endif /* CONFIG_CMDLINE */
  506. pr_debug("Command line is: %s\n", cmd_line);
  507. /* break now */
  508. return 1;
  509. }
  510. static int __init early_init_dt_scan_root(unsigned long node,
  511. const char *uname, int depth, void *data)
  512. {
  513. u32 *prop;
  514. if (depth != 0)
  515. return 0;
  516. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  517. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  518. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  519. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  520. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  521. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  522. /* break now */
  523. return 1;
  524. }
  525. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  526. {
  527. cell_t *p = *cellp;
  528. *cellp = p + s;
  529. return of_read_number(p, s);
  530. }
  531. static int __init early_init_dt_scan_memory(unsigned long node,
  532. const char *uname, int depth, void *data)
  533. {
  534. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  535. cell_t *reg, *endp;
  536. unsigned long l;
  537. /* Look for the ibm,dynamic-reconfiguration-memory node */
  538. /* if (depth == 1 &&
  539. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  540. return early_init_dt_scan_drconf_memory(node);
  541. */
  542. /* We are scanning "memory" nodes only */
  543. if (type == NULL) {
  544. /*
  545. * The longtrail doesn't have a device_type on the
  546. * /memory node, so look for the node called /memory@0.
  547. */
  548. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  549. return 0;
  550. } else if (strcmp(type, "memory") != 0)
  551. return 0;
  552. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  553. if (reg == NULL)
  554. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  555. if (reg == NULL)
  556. return 0;
  557. endp = reg + (l / sizeof(cell_t));
  558. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  559. uname, l, reg[0], reg[1], reg[2], reg[3]);
  560. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  561. u64 base, size;
  562. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  563. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  564. if (size == 0)
  565. continue;
  566. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  567. (unsigned long long)size);
  568. lmb_add(base, size);
  569. }
  570. return 0;
  571. }
  572. #ifdef CONFIG_PHYP_DUMP
  573. /**
  574. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  575. *
  576. * Function to find the largest size we need to reserve
  577. * during early boot process.
  578. *
  579. * It either looks for boot param and returns that OR
  580. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  581. *
  582. */
  583. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  584. {
  585. unsigned long tmp;
  586. if (phyp_dump_info->reserve_bootvar)
  587. return phyp_dump_info->reserve_bootvar;
  588. /* divide by 20 to get 5% of value */
  589. tmp = lmb_end_of_DRAM();
  590. do_div(tmp, 20);
  591. /* round it down in multiples of 256 */
  592. tmp = tmp & ~0x0FFFFFFFUL;
  593. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  594. }
  595. /**
  596. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  597. *
  598. * This routine may reserve memory regions in the kernel only
  599. * if the system is supported and a dump was taken in last
  600. * boot instance or if the hardware is supported and the
  601. * scratch area needs to be setup. In other instances it returns
  602. * without reserving anything. The memory in case of dump being
  603. * active is freed when the dump is collected (by userland tools).
  604. */
  605. static void __init phyp_dump_reserve_mem(void)
  606. {
  607. unsigned long base, size;
  608. unsigned long variable_reserve_size;
  609. if (!phyp_dump_info->phyp_dump_configured) {
  610. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  611. return;
  612. }
  613. if (!phyp_dump_info->phyp_dump_at_boot) {
  614. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  615. return;
  616. }
  617. variable_reserve_size = phyp_dump_calculate_reserve_size();
  618. if (phyp_dump_info->phyp_dump_is_active) {
  619. /* Reserve *everything* above RMR.Area freed by userland tools*/
  620. base = variable_reserve_size;
  621. size = lmb_end_of_DRAM() - base;
  622. /* XXX crashed_ram_end is wrong, since it may be beyond
  623. * the memory_limit, it will need to be adjusted. */
  624. lmb_reserve(base, size);
  625. phyp_dump_info->init_reserve_start = base;
  626. phyp_dump_info->init_reserve_size = size;
  627. } else {
  628. size = phyp_dump_info->cpu_state_size +
  629. phyp_dump_info->hpte_region_size +
  630. variable_reserve_size;
  631. base = lmb_end_of_DRAM() - size;
  632. lmb_reserve(base, size);
  633. phyp_dump_info->init_reserve_start = base;
  634. phyp_dump_info->init_reserve_size = size;
  635. }
  636. }
  637. #else
  638. static inline void __init phyp_dump_reserve_mem(void) {}
  639. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  640. #ifdef CONFIG_EARLY_PRINTK
  641. /* MS this is Microblaze specifig function */
  642. static int __init early_init_dt_scan_serial(unsigned long node,
  643. const char *uname, int depth, void *data)
  644. {
  645. unsigned long l;
  646. char *p;
  647. int *addr;
  648. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  649. /* find all serial nodes */
  650. if (strncmp(uname, "serial", 6) != 0)
  651. return 0;
  652. early_init_dt_check_for_initrd(node);
  653. /* find compatible node with uartlite */
  654. p = of_get_flat_dt_prop(node, "compatible", &l);
  655. if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
  656. (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
  657. return 0;
  658. addr = of_get_flat_dt_prop(node, "reg", &l);
  659. return *addr; /* return address */
  660. }
  661. /* this function is looking for early uartlite console - Microblaze specific */
  662. int __init early_uartlite_console(void)
  663. {
  664. return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
  665. }
  666. #endif
  667. void __init early_init_devtree(void *params)
  668. {
  669. pr_debug(" -> early_init_devtree(%p)\n", params);
  670. /* Setup flat device-tree pointer */
  671. initial_boot_params = params;
  672. #ifdef CONFIG_PHYP_DUMP
  673. /* scan tree to see if dump occured during last boot */
  674. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  675. #endif
  676. /* Retrieve various informations from the /chosen node of the
  677. * device-tree, including the platform type, initrd location and
  678. * size, TCE reserve, and more ...
  679. */
  680. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  681. /* Scan memory nodes and rebuild LMBs */
  682. lmb_init();
  683. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  684. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  685. /* Save command line for /proc/cmdline and then parse parameters */
  686. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  687. parse_early_param();
  688. lmb_analyze();
  689. pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
  690. pr_debug("Scanning CPUs ...\n");
  691. /* Retreive CPU related informations from the flat tree
  692. * (altivec support, boot CPU ID, ...)
  693. */
  694. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  695. pr_debug(" <- early_init_devtree()\n");
  696. }
  697. /**
  698. * Indicates whether the root node has a given value in its
  699. * compatible property.
  700. */
  701. int machine_is_compatible(const char *compat)
  702. {
  703. struct device_node *root;
  704. int rc = 0;
  705. root = of_find_node_by_path("/");
  706. if (root) {
  707. rc = of_device_is_compatible(root, compat);
  708. of_node_put(root);
  709. }
  710. return rc;
  711. }
  712. EXPORT_SYMBOL(machine_is_compatible);
  713. /*******
  714. *
  715. * New implementation of the OF "find" APIs, return a refcounted
  716. * object, call of_node_put() when done. The device tree and list
  717. * are protected by a rw_lock.
  718. *
  719. * Note that property management will need some locking as well,
  720. * this isn't dealt with yet.
  721. *
  722. *******/
  723. /**
  724. * of_find_node_by_phandle - Find a node given a phandle
  725. * @handle: phandle of the node to find
  726. *
  727. * Returns a node pointer with refcount incremented, use
  728. * of_node_put() on it when done.
  729. */
  730. struct device_node *of_find_node_by_phandle(phandle handle)
  731. {
  732. struct device_node *np;
  733. read_lock(&devtree_lock);
  734. for (np = allnodes; np != NULL; np = np->allnext)
  735. if (np->linux_phandle == handle)
  736. break;
  737. of_node_get(np);
  738. read_unlock(&devtree_lock);
  739. return np;
  740. }
  741. EXPORT_SYMBOL(of_find_node_by_phandle);
  742. /**
  743. * of_find_all_nodes - Get next node in global list
  744. * @prev: Previous node or NULL to start iteration
  745. * of_node_put() will be called on it
  746. *
  747. * Returns a node pointer with refcount incremented, use
  748. * of_node_put() on it when done.
  749. */
  750. struct device_node *of_find_all_nodes(struct device_node *prev)
  751. {
  752. struct device_node *np;
  753. read_lock(&devtree_lock);
  754. np = prev ? prev->allnext : allnodes;
  755. for (; np != NULL; np = np->allnext)
  756. if (of_node_get(np))
  757. break;
  758. of_node_put(prev);
  759. read_unlock(&devtree_lock);
  760. return np;
  761. }
  762. EXPORT_SYMBOL(of_find_all_nodes);
  763. /**
  764. * of_node_get - Increment refcount of a node
  765. * @node: Node to inc refcount, NULL is supported to
  766. * simplify writing of callers
  767. *
  768. * Returns node.
  769. */
  770. struct device_node *of_node_get(struct device_node *node)
  771. {
  772. if (node)
  773. kref_get(&node->kref);
  774. return node;
  775. }
  776. EXPORT_SYMBOL(of_node_get);
  777. static inline struct device_node *kref_to_device_node(struct kref *kref)
  778. {
  779. return container_of(kref, struct device_node, kref);
  780. }
  781. /**
  782. * of_node_release - release a dynamically allocated node
  783. * @kref: kref element of the node to be released
  784. *
  785. * In of_node_put() this function is passed to kref_put()
  786. * as the destructor.
  787. */
  788. static void of_node_release(struct kref *kref)
  789. {
  790. struct device_node *node = kref_to_device_node(kref);
  791. struct property *prop = node->properties;
  792. /* We should never be releasing nodes that haven't been detached. */
  793. if (!of_node_check_flag(node, OF_DETACHED)) {
  794. printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
  795. node->full_name);
  796. dump_stack();
  797. kref_init(&node->kref);
  798. return;
  799. }
  800. if (!of_node_check_flag(node, OF_DYNAMIC))
  801. return;
  802. while (prop) {
  803. struct property *next = prop->next;
  804. kfree(prop->name);
  805. kfree(prop->value);
  806. kfree(prop);
  807. prop = next;
  808. if (!prop) {
  809. prop = node->deadprops;
  810. node->deadprops = NULL;
  811. }
  812. }
  813. kfree(node->full_name);
  814. kfree(node->data);
  815. kfree(node);
  816. }
  817. /**
  818. * of_node_put - Decrement refcount of a node
  819. * @node: Node to dec refcount, NULL is supported to
  820. * simplify writing of callers
  821. *
  822. */
  823. void of_node_put(struct device_node *node)
  824. {
  825. if (node)
  826. kref_put(&node->kref, of_node_release);
  827. }
  828. EXPORT_SYMBOL(of_node_put);
  829. /*
  830. * Plug a device node into the tree and global list.
  831. */
  832. void of_attach_node(struct device_node *np)
  833. {
  834. unsigned long flags;
  835. write_lock_irqsave(&devtree_lock, flags);
  836. np->sibling = np->parent->child;
  837. np->allnext = allnodes;
  838. np->parent->child = np;
  839. allnodes = np;
  840. write_unlock_irqrestore(&devtree_lock, flags);
  841. }
  842. /*
  843. * "Unplug" a node from the device tree. The caller must hold
  844. * a reference to the node. The memory associated with the node
  845. * is not freed until its refcount goes to zero.
  846. */
  847. void of_detach_node(struct device_node *np)
  848. {
  849. struct device_node *parent;
  850. unsigned long flags;
  851. write_lock_irqsave(&devtree_lock, flags);
  852. parent = np->parent;
  853. if (!parent)
  854. goto out_unlock;
  855. if (allnodes == np)
  856. allnodes = np->allnext;
  857. else {
  858. struct device_node *prev;
  859. for (prev = allnodes;
  860. prev->allnext != np;
  861. prev = prev->allnext)
  862. ;
  863. prev->allnext = np->allnext;
  864. }
  865. if (parent->child == np)
  866. parent->child = np->sibling;
  867. else {
  868. struct device_node *prevsib;
  869. for (prevsib = np->parent->child;
  870. prevsib->sibling != np;
  871. prevsib = prevsib->sibling)
  872. ;
  873. prevsib->sibling = np->sibling;
  874. }
  875. of_node_set_flag(np, OF_DETACHED);
  876. out_unlock:
  877. write_unlock_irqrestore(&devtree_lock, flags);
  878. }
  879. /*
  880. * Add a property to a node
  881. */
  882. int prom_add_property(struct device_node *np, struct property *prop)
  883. {
  884. struct property **next;
  885. unsigned long flags;
  886. prop->next = NULL;
  887. write_lock_irqsave(&devtree_lock, flags);
  888. next = &np->properties;
  889. while (*next) {
  890. if (strcmp(prop->name, (*next)->name) == 0) {
  891. /* duplicate ! don't insert it */
  892. write_unlock_irqrestore(&devtree_lock, flags);
  893. return -1;
  894. }
  895. next = &(*next)->next;
  896. }
  897. *next = prop;
  898. write_unlock_irqrestore(&devtree_lock, flags);
  899. #ifdef CONFIG_PROC_DEVICETREE
  900. /* try to add to proc as well if it was initialized */
  901. if (np->pde)
  902. proc_device_tree_add_prop(np->pde, prop);
  903. #endif /* CONFIG_PROC_DEVICETREE */
  904. return 0;
  905. }
  906. /*
  907. * Remove a property from a node. Note that we don't actually
  908. * remove it, since we have given out who-knows-how-many pointers
  909. * to the data using get-property. Instead we just move the property
  910. * to the "dead properties" list, so it won't be found any more.
  911. */
  912. int prom_remove_property(struct device_node *np, struct property *prop)
  913. {
  914. struct property **next;
  915. unsigned long flags;
  916. int found = 0;
  917. write_lock_irqsave(&devtree_lock, flags);
  918. next = &np->properties;
  919. while (*next) {
  920. if (*next == prop) {
  921. /* found the node */
  922. *next = prop->next;
  923. prop->next = np->deadprops;
  924. np->deadprops = prop;
  925. found = 1;
  926. break;
  927. }
  928. next = &(*next)->next;
  929. }
  930. write_unlock_irqrestore(&devtree_lock, flags);
  931. if (!found)
  932. return -ENODEV;
  933. #ifdef CONFIG_PROC_DEVICETREE
  934. /* try to remove the proc node as well */
  935. if (np->pde)
  936. proc_device_tree_remove_prop(np->pde, prop);
  937. #endif /* CONFIG_PROC_DEVICETREE */
  938. return 0;
  939. }
  940. /*
  941. * Update a property in a node. Note that we don't actually
  942. * remove it, since we have given out who-knows-how-many pointers
  943. * to the data using get-property. Instead we just move the property
  944. * to the "dead properties" list, and add the new property to the
  945. * property list
  946. */
  947. int prom_update_property(struct device_node *np,
  948. struct property *newprop,
  949. struct property *oldprop)
  950. {
  951. struct property **next;
  952. unsigned long flags;
  953. int found = 0;
  954. write_lock_irqsave(&devtree_lock, flags);
  955. next = &np->properties;
  956. while (*next) {
  957. if (*next == oldprop) {
  958. /* found the node */
  959. newprop->next = oldprop->next;
  960. *next = newprop;
  961. oldprop->next = np->deadprops;
  962. np->deadprops = oldprop;
  963. found = 1;
  964. break;
  965. }
  966. next = &(*next)->next;
  967. }
  968. write_unlock_irqrestore(&devtree_lock, flags);
  969. if (!found)
  970. return -ENODEV;
  971. #ifdef CONFIG_PROC_DEVICETREE
  972. /* try to add to proc as well if it was initialized */
  973. if (np->pde)
  974. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  975. #endif /* CONFIG_PROC_DEVICETREE */
  976. return 0;
  977. }
  978. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  979. static struct debugfs_blob_wrapper flat_dt_blob;
  980. static int __init export_flat_device_tree(void)
  981. {
  982. struct dentry *d;
  983. flat_dt_blob.data = initial_boot_params;
  984. flat_dt_blob.size = initial_boot_params->totalsize;
  985. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  986. of_debugfs_root, &flat_dt_blob);
  987. if (!d)
  988. return 1;
  989. return 0;
  990. }
  991. device_initcall(export_flat_device_tree);
  992. #endif