prom.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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)
  460. __va((u32)of_read_ulong(prop, l/4));
  461. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  462. if (prop) {
  463. initrd_end = (unsigned long)
  464. __va((u32)of_read_ulong(prop, 1/4));
  465. initrd_below_start_ok = 1;
  466. } else {
  467. initrd_start = 0;
  468. }
  469. }
  470. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
  471. initrd_start, initrd_end);
  472. }
  473. #else
  474. static inline void early_init_dt_check_for_initrd(unsigned long node)
  475. {
  476. }
  477. #endif /* CONFIG_BLK_DEV_INITRD */
  478. static int __init early_init_dt_scan_chosen(unsigned long node,
  479. const char *uname, int depth, void *data)
  480. {
  481. unsigned long l;
  482. char *p;
  483. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  484. if (depth != 1 ||
  485. (strcmp(uname, "chosen") != 0 &&
  486. strcmp(uname, "chosen@0") != 0))
  487. return 0;
  488. #ifdef CONFIG_KEXEC
  489. lprop = (u64 *)of_get_flat_dt_prop(node,
  490. "linux,crashkernel-base", NULL);
  491. if (lprop)
  492. crashk_res.start = *lprop;
  493. lprop = (u64 *)of_get_flat_dt_prop(node,
  494. "linux,crashkernel-size", NULL);
  495. if (lprop)
  496. crashk_res.end = crashk_res.start + *lprop - 1;
  497. #endif
  498. early_init_dt_check_for_initrd(node);
  499. /* Retreive command line */
  500. p = of_get_flat_dt_prop(node, "bootargs", &l);
  501. if (p != NULL && l > 0)
  502. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  503. #ifdef CONFIG_CMDLINE
  504. #ifndef CONFIG_CMDLINE_FORCE
  505. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  506. #endif
  507. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  508. #endif /* CONFIG_CMDLINE */
  509. pr_debug("Command line is: %s\n", cmd_line);
  510. /* break now */
  511. return 1;
  512. }
  513. static int __init early_init_dt_scan_root(unsigned long node,
  514. const char *uname, int depth, void *data)
  515. {
  516. u32 *prop;
  517. if (depth != 0)
  518. return 0;
  519. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  520. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  521. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  522. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  523. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  524. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  525. /* break now */
  526. return 1;
  527. }
  528. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  529. {
  530. cell_t *p = *cellp;
  531. *cellp = p + s;
  532. return of_read_number(p, s);
  533. }
  534. static int __init early_init_dt_scan_memory(unsigned long node,
  535. const char *uname, int depth, void *data)
  536. {
  537. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  538. cell_t *reg, *endp;
  539. unsigned long l;
  540. /* Look for the ibm,dynamic-reconfiguration-memory node */
  541. /* if (depth == 1 &&
  542. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  543. return early_init_dt_scan_drconf_memory(node);
  544. */
  545. /* We are scanning "memory" nodes only */
  546. if (type == NULL) {
  547. /*
  548. * The longtrail doesn't have a device_type on the
  549. * /memory node, so look for the node called /memory@0.
  550. */
  551. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  552. return 0;
  553. } else if (strcmp(type, "memory") != 0)
  554. return 0;
  555. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  556. if (reg == NULL)
  557. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  558. if (reg == NULL)
  559. return 0;
  560. endp = reg + (l / sizeof(cell_t));
  561. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  562. uname, l, reg[0], reg[1], reg[2], reg[3]);
  563. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  564. u64 base, size;
  565. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  566. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  567. if (size == 0)
  568. continue;
  569. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  570. (unsigned long long)size);
  571. lmb_add(base, size);
  572. }
  573. return 0;
  574. }
  575. #ifdef CONFIG_PHYP_DUMP
  576. /**
  577. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  578. *
  579. * Function to find the largest size we need to reserve
  580. * during early boot process.
  581. *
  582. * It either looks for boot param and returns that OR
  583. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  584. *
  585. */
  586. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  587. {
  588. unsigned long tmp;
  589. if (phyp_dump_info->reserve_bootvar)
  590. return phyp_dump_info->reserve_bootvar;
  591. /* divide by 20 to get 5% of value */
  592. tmp = lmb_end_of_DRAM();
  593. do_div(tmp, 20);
  594. /* round it down in multiples of 256 */
  595. tmp = tmp & ~0x0FFFFFFFUL;
  596. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  597. }
  598. /**
  599. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  600. *
  601. * This routine may reserve memory regions in the kernel only
  602. * if the system is supported and a dump was taken in last
  603. * boot instance or if the hardware is supported and the
  604. * scratch area needs to be setup. In other instances it returns
  605. * without reserving anything. The memory in case of dump being
  606. * active is freed when the dump is collected (by userland tools).
  607. */
  608. static void __init phyp_dump_reserve_mem(void)
  609. {
  610. unsigned long base, size;
  611. unsigned long variable_reserve_size;
  612. if (!phyp_dump_info->phyp_dump_configured) {
  613. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  614. return;
  615. }
  616. if (!phyp_dump_info->phyp_dump_at_boot) {
  617. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  618. return;
  619. }
  620. variable_reserve_size = phyp_dump_calculate_reserve_size();
  621. if (phyp_dump_info->phyp_dump_is_active) {
  622. /* Reserve *everything* above RMR.Area freed by userland tools*/
  623. base = variable_reserve_size;
  624. size = lmb_end_of_DRAM() - base;
  625. /* XXX crashed_ram_end is wrong, since it may be beyond
  626. * the memory_limit, it will need to be adjusted. */
  627. lmb_reserve(base, size);
  628. phyp_dump_info->init_reserve_start = base;
  629. phyp_dump_info->init_reserve_size = size;
  630. } else {
  631. size = phyp_dump_info->cpu_state_size +
  632. phyp_dump_info->hpte_region_size +
  633. variable_reserve_size;
  634. base = lmb_end_of_DRAM() - size;
  635. lmb_reserve(base, size);
  636. phyp_dump_info->init_reserve_start = base;
  637. phyp_dump_info->init_reserve_size = size;
  638. }
  639. }
  640. #else
  641. static inline void __init phyp_dump_reserve_mem(void) {}
  642. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  643. #ifdef CONFIG_EARLY_PRINTK
  644. /* MS this is Microblaze specifig function */
  645. static int __init early_init_dt_scan_serial(unsigned long node,
  646. const char *uname, int depth, void *data)
  647. {
  648. unsigned long l;
  649. char *p;
  650. int *addr;
  651. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  652. /* find all serial nodes */
  653. if (strncmp(uname, "serial", 6) != 0)
  654. return 0;
  655. early_init_dt_check_for_initrd(node);
  656. /* find compatible node with uartlite */
  657. p = of_get_flat_dt_prop(node, "compatible", &l);
  658. if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
  659. (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
  660. return 0;
  661. addr = of_get_flat_dt_prop(node, "reg", &l);
  662. return *addr; /* return address */
  663. }
  664. /* this function is looking for early uartlite console - Microblaze specific */
  665. int __init early_uartlite_console(void)
  666. {
  667. return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
  668. }
  669. #endif
  670. void __init early_init_devtree(void *params)
  671. {
  672. pr_debug(" -> early_init_devtree(%p)\n", params);
  673. /* Setup flat device-tree pointer */
  674. initial_boot_params = params;
  675. #ifdef CONFIG_PHYP_DUMP
  676. /* scan tree to see if dump occured during last boot */
  677. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  678. #endif
  679. /* Retrieve various informations from the /chosen node of the
  680. * device-tree, including the platform type, initrd location and
  681. * size, TCE reserve, and more ...
  682. */
  683. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  684. /* Scan memory nodes and rebuild LMBs */
  685. lmb_init();
  686. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  687. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  688. /* Save command line for /proc/cmdline and then parse parameters */
  689. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  690. parse_early_param();
  691. lmb_analyze();
  692. pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
  693. pr_debug("Scanning CPUs ...\n");
  694. /* Retreive CPU related informations from the flat tree
  695. * (altivec support, boot CPU ID, ...)
  696. */
  697. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  698. pr_debug(" <- early_init_devtree()\n");
  699. }
  700. /**
  701. * Indicates whether the root node has a given value in its
  702. * compatible property.
  703. */
  704. int machine_is_compatible(const char *compat)
  705. {
  706. struct device_node *root;
  707. int rc = 0;
  708. root = of_find_node_by_path("/");
  709. if (root) {
  710. rc = of_device_is_compatible(root, compat);
  711. of_node_put(root);
  712. }
  713. return rc;
  714. }
  715. EXPORT_SYMBOL(machine_is_compatible);
  716. /*******
  717. *
  718. * New implementation of the OF "find" APIs, return a refcounted
  719. * object, call of_node_put() when done. The device tree and list
  720. * are protected by a rw_lock.
  721. *
  722. * Note that property management will need some locking as well,
  723. * this isn't dealt with yet.
  724. *
  725. *******/
  726. /**
  727. * of_find_node_by_phandle - Find a node given a phandle
  728. * @handle: phandle of the node to find
  729. *
  730. * Returns a node pointer with refcount incremented, use
  731. * of_node_put() on it when done.
  732. */
  733. struct device_node *of_find_node_by_phandle(phandle handle)
  734. {
  735. struct device_node *np;
  736. read_lock(&devtree_lock);
  737. for (np = allnodes; np != NULL; np = np->allnext)
  738. if (np->linux_phandle == handle)
  739. break;
  740. of_node_get(np);
  741. read_unlock(&devtree_lock);
  742. return np;
  743. }
  744. EXPORT_SYMBOL(of_find_node_by_phandle);
  745. /**
  746. * of_find_all_nodes - Get next node in global list
  747. * @prev: Previous node or NULL to start iteration
  748. * of_node_put() will be called on it
  749. *
  750. * Returns a node pointer with refcount incremented, use
  751. * of_node_put() on it when done.
  752. */
  753. struct device_node *of_find_all_nodes(struct device_node *prev)
  754. {
  755. struct device_node *np;
  756. read_lock(&devtree_lock);
  757. np = prev ? prev->allnext : allnodes;
  758. for (; np != NULL; np = np->allnext)
  759. if (of_node_get(np))
  760. break;
  761. of_node_put(prev);
  762. read_unlock(&devtree_lock);
  763. return np;
  764. }
  765. EXPORT_SYMBOL(of_find_all_nodes);
  766. /**
  767. * of_node_get - Increment refcount of a node
  768. * @node: Node to inc refcount, NULL is supported to
  769. * simplify writing of callers
  770. *
  771. * Returns node.
  772. */
  773. struct device_node *of_node_get(struct device_node *node)
  774. {
  775. if (node)
  776. kref_get(&node->kref);
  777. return node;
  778. }
  779. EXPORT_SYMBOL(of_node_get);
  780. static inline struct device_node *kref_to_device_node(struct kref *kref)
  781. {
  782. return container_of(kref, struct device_node, kref);
  783. }
  784. /**
  785. * of_node_release - release a dynamically allocated node
  786. * @kref: kref element of the node to be released
  787. *
  788. * In of_node_put() this function is passed to kref_put()
  789. * as the destructor.
  790. */
  791. static void of_node_release(struct kref *kref)
  792. {
  793. struct device_node *node = kref_to_device_node(kref);
  794. struct property *prop = node->properties;
  795. /* We should never be releasing nodes that haven't been detached. */
  796. if (!of_node_check_flag(node, OF_DETACHED)) {
  797. printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
  798. node->full_name);
  799. dump_stack();
  800. kref_init(&node->kref);
  801. return;
  802. }
  803. if (!of_node_check_flag(node, OF_DYNAMIC))
  804. return;
  805. while (prop) {
  806. struct property *next = prop->next;
  807. kfree(prop->name);
  808. kfree(prop->value);
  809. kfree(prop);
  810. prop = next;
  811. if (!prop) {
  812. prop = node->deadprops;
  813. node->deadprops = NULL;
  814. }
  815. }
  816. kfree(node->full_name);
  817. kfree(node->data);
  818. kfree(node);
  819. }
  820. /**
  821. * of_node_put - Decrement refcount of a node
  822. * @node: Node to dec refcount, NULL is supported to
  823. * simplify writing of callers
  824. *
  825. */
  826. void of_node_put(struct device_node *node)
  827. {
  828. if (node)
  829. kref_put(&node->kref, of_node_release);
  830. }
  831. EXPORT_SYMBOL(of_node_put);
  832. /*
  833. * Plug a device node into the tree and global list.
  834. */
  835. void of_attach_node(struct device_node *np)
  836. {
  837. unsigned long flags;
  838. write_lock_irqsave(&devtree_lock, flags);
  839. np->sibling = np->parent->child;
  840. np->allnext = allnodes;
  841. np->parent->child = np;
  842. allnodes = np;
  843. write_unlock_irqrestore(&devtree_lock, flags);
  844. }
  845. /*
  846. * "Unplug" a node from the device tree. The caller must hold
  847. * a reference to the node. The memory associated with the node
  848. * is not freed until its refcount goes to zero.
  849. */
  850. void of_detach_node(struct device_node *np)
  851. {
  852. struct device_node *parent;
  853. unsigned long flags;
  854. write_lock_irqsave(&devtree_lock, flags);
  855. parent = np->parent;
  856. if (!parent)
  857. goto out_unlock;
  858. if (allnodes == np)
  859. allnodes = np->allnext;
  860. else {
  861. struct device_node *prev;
  862. for (prev = allnodes;
  863. prev->allnext != np;
  864. prev = prev->allnext)
  865. ;
  866. prev->allnext = np->allnext;
  867. }
  868. if (parent->child == np)
  869. parent->child = np->sibling;
  870. else {
  871. struct device_node *prevsib;
  872. for (prevsib = np->parent->child;
  873. prevsib->sibling != np;
  874. prevsib = prevsib->sibling)
  875. ;
  876. prevsib->sibling = np->sibling;
  877. }
  878. of_node_set_flag(np, OF_DETACHED);
  879. out_unlock:
  880. write_unlock_irqrestore(&devtree_lock, flags);
  881. }
  882. /*
  883. * Add a property to a node
  884. */
  885. int prom_add_property(struct device_node *np, struct property *prop)
  886. {
  887. struct property **next;
  888. unsigned long flags;
  889. prop->next = NULL;
  890. write_lock_irqsave(&devtree_lock, flags);
  891. next = &np->properties;
  892. while (*next) {
  893. if (strcmp(prop->name, (*next)->name) == 0) {
  894. /* duplicate ! don't insert it */
  895. write_unlock_irqrestore(&devtree_lock, flags);
  896. return -1;
  897. }
  898. next = &(*next)->next;
  899. }
  900. *next = prop;
  901. write_unlock_irqrestore(&devtree_lock, flags);
  902. #ifdef CONFIG_PROC_DEVICETREE
  903. /* try to add to proc as well if it was initialized */
  904. if (np->pde)
  905. proc_device_tree_add_prop(np->pde, prop);
  906. #endif /* CONFIG_PROC_DEVICETREE */
  907. return 0;
  908. }
  909. /*
  910. * Remove a property from a node. Note that we don't actually
  911. * remove it, since we have given out who-knows-how-many pointers
  912. * to the data using get-property. Instead we just move the property
  913. * to the "dead properties" list, so it won't be found any more.
  914. */
  915. int prom_remove_property(struct device_node *np, struct property *prop)
  916. {
  917. struct property **next;
  918. unsigned long flags;
  919. int found = 0;
  920. write_lock_irqsave(&devtree_lock, flags);
  921. next = &np->properties;
  922. while (*next) {
  923. if (*next == prop) {
  924. /* found the node */
  925. *next = prop->next;
  926. prop->next = np->deadprops;
  927. np->deadprops = prop;
  928. found = 1;
  929. break;
  930. }
  931. next = &(*next)->next;
  932. }
  933. write_unlock_irqrestore(&devtree_lock, flags);
  934. if (!found)
  935. return -ENODEV;
  936. #ifdef CONFIG_PROC_DEVICETREE
  937. /* try to remove the proc node as well */
  938. if (np->pde)
  939. proc_device_tree_remove_prop(np->pde, prop);
  940. #endif /* CONFIG_PROC_DEVICETREE */
  941. return 0;
  942. }
  943. /*
  944. * Update a property in a node. Note that we don't actually
  945. * remove it, since we have given out who-knows-how-many pointers
  946. * to the data using get-property. Instead we just move the property
  947. * to the "dead properties" list, and add the new property to the
  948. * property list
  949. */
  950. int prom_update_property(struct device_node *np,
  951. struct property *newprop,
  952. struct property *oldprop)
  953. {
  954. struct property **next;
  955. unsigned long flags;
  956. int found = 0;
  957. write_lock_irqsave(&devtree_lock, flags);
  958. next = &np->properties;
  959. while (*next) {
  960. if (*next == oldprop) {
  961. /* found the node */
  962. newprop->next = oldprop->next;
  963. *next = newprop;
  964. oldprop->next = np->deadprops;
  965. np->deadprops = oldprop;
  966. found = 1;
  967. break;
  968. }
  969. next = &(*next)->next;
  970. }
  971. write_unlock_irqrestore(&devtree_lock, flags);
  972. if (!found)
  973. return -ENODEV;
  974. #ifdef CONFIG_PROC_DEVICETREE
  975. /* try to add to proc as well if it was initialized */
  976. if (np->pde)
  977. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  978. #endif /* CONFIG_PROC_DEVICETREE */
  979. return 0;
  980. }
  981. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  982. static struct debugfs_blob_wrapper flat_dt_blob;
  983. static int __init export_flat_device_tree(void)
  984. {
  985. struct dentry *d;
  986. flat_dt_blob.data = initial_boot_params;
  987. flat_dt_blob.size = initial_boot_params->totalsize;
  988. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  989. of_debugfs_root, &flat_dt_blob);
  990. if (!d)
  991. return 1;
  992. return 0;
  993. }
  994. device_initcall(export_flat_device_tree);
  995. #endif