prom.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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_node_get - Increment refcount of a node
  747. * @node: Node to inc refcount, NULL is supported to
  748. * simplify writing of callers
  749. *
  750. * Returns node.
  751. */
  752. struct device_node *of_node_get(struct device_node *node)
  753. {
  754. if (node)
  755. kref_get(&node->kref);
  756. return node;
  757. }
  758. EXPORT_SYMBOL(of_node_get);
  759. static inline struct device_node *kref_to_device_node(struct kref *kref)
  760. {
  761. return container_of(kref, struct device_node, kref);
  762. }
  763. /**
  764. * of_node_release - release a dynamically allocated node
  765. * @kref: kref element of the node to be released
  766. *
  767. * In of_node_put() this function is passed to kref_put()
  768. * as the destructor.
  769. */
  770. static void of_node_release(struct kref *kref)
  771. {
  772. struct device_node *node = kref_to_device_node(kref);
  773. struct property *prop = node->properties;
  774. /* We should never be releasing nodes that haven't been detached. */
  775. if (!of_node_check_flag(node, OF_DETACHED)) {
  776. printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
  777. node->full_name);
  778. dump_stack();
  779. kref_init(&node->kref);
  780. return;
  781. }
  782. if (!of_node_check_flag(node, OF_DYNAMIC))
  783. return;
  784. while (prop) {
  785. struct property *next = prop->next;
  786. kfree(prop->name);
  787. kfree(prop->value);
  788. kfree(prop);
  789. prop = next;
  790. if (!prop) {
  791. prop = node->deadprops;
  792. node->deadprops = NULL;
  793. }
  794. }
  795. kfree(node->full_name);
  796. kfree(node->data);
  797. kfree(node);
  798. }
  799. /**
  800. * of_node_put - Decrement refcount of a node
  801. * @node: Node to dec refcount, NULL is supported to
  802. * simplify writing of callers
  803. *
  804. */
  805. void of_node_put(struct device_node *node)
  806. {
  807. if (node)
  808. kref_put(&node->kref, of_node_release);
  809. }
  810. EXPORT_SYMBOL(of_node_put);
  811. /*
  812. * Plug a device node into the tree and global list.
  813. */
  814. void of_attach_node(struct device_node *np)
  815. {
  816. unsigned long flags;
  817. write_lock_irqsave(&devtree_lock, flags);
  818. np->sibling = np->parent->child;
  819. np->allnext = allnodes;
  820. np->parent->child = np;
  821. allnodes = np;
  822. write_unlock_irqrestore(&devtree_lock, flags);
  823. }
  824. /*
  825. * "Unplug" a node from the device tree. The caller must hold
  826. * a reference to the node. The memory associated with the node
  827. * is not freed until its refcount goes to zero.
  828. */
  829. void of_detach_node(struct device_node *np)
  830. {
  831. struct device_node *parent;
  832. unsigned long flags;
  833. write_lock_irqsave(&devtree_lock, flags);
  834. parent = np->parent;
  835. if (!parent)
  836. goto out_unlock;
  837. if (allnodes == np)
  838. allnodes = np->allnext;
  839. else {
  840. struct device_node *prev;
  841. for (prev = allnodes;
  842. prev->allnext != np;
  843. prev = prev->allnext)
  844. ;
  845. prev->allnext = np->allnext;
  846. }
  847. if (parent->child == np)
  848. parent->child = np->sibling;
  849. else {
  850. struct device_node *prevsib;
  851. for (prevsib = np->parent->child;
  852. prevsib->sibling != np;
  853. prevsib = prevsib->sibling)
  854. ;
  855. prevsib->sibling = np->sibling;
  856. }
  857. of_node_set_flag(np, OF_DETACHED);
  858. out_unlock:
  859. write_unlock_irqrestore(&devtree_lock, flags);
  860. }
  861. /*
  862. * Add a property to a node
  863. */
  864. int prom_add_property(struct device_node *np, struct property *prop)
  865. {
  866. struct property **next;
  867. unsigned long flags;
  868. prop->next = NULL;
  869. write_lock_irqsave(&devtree_lock, flags);
  870. next = &np->properties;
  871. while (*next) {
  872. if (strcmp(prop->name, (*next)->name) == 0) {
  873. /* duplicate ! don't insert it */
  874. write_unlock_irqrestore(&devtree_lock, flags);
  875. return -1;
  876. }
  877. next = &(*next)->next;
  878. }
  879. *next = prop;
  880. write_unlock_irqrestore(&devtree_lock, flags);
  881. #ifdef CONFIG_PROC_DEVICETREE
  882. /* try to add to proc as well if it was initialized */
  883. if (np->pde)
  884. proc_device_tree_add_prop(np->pde, prop);
  885. #endif /* CONFIG_PROC_DEVICETREE */
  886. return 0;
  887. }
  888. /*
  889. * Remove a property from a node. Note that we don't actually
  890. * remove it, since we have given out who-knows-how-many pointers
  891. * to the data using get-property. Instead we just move the property
  892. * to the "dead properties" list, so it won't be found any more.
  893. */
  894. int prom_remove_property(struct device_node *np, struct property *prop)
  895. {
  896. struct property **next;
  897. unsigned long flags;
  898. int found = 0;
  899. write_lock_irqsave(&devtree_lock, flags);
  900. next = &np->properties;
  901. while (*next) {
  902. if (*next == prop) {
  903. /* found the node */
  904. *next = prop->next;
  905. prop->next = np->deadprops;
  906. np->deadprops = prop;
  907. found = 1;
  908. break;
  909. }
  910. next = &(*next)->next;
  911. }
  912. write_unlock_irqrestore(&devtree_lock, flags);
  913. if (!found)
  914. return -ENODEV;
  915. #ifdef CONFIG_PROC_DEVICETREE
  916. /* try to remove the proc node as well */
  917. if (np->pde)
  918. proc_device_tree_remove_prop(np->pde, prop);
  919. #endif /* CONFIG_PROC_DEVICETREE */
  920. return 0;
  921. }
  922. /*
  923. * Update a property in a node. Note that we don't actually
  924. * remove it, since we have given out who-knows-how-many pointers
  925. * to the data using get-property. Instead we just move the property
  926. * to the "dead properties" list, and add the new property to the
  927. * property list
  928. */
  929. int prom_update_property(struct device_node *np,
  930. struct property *newprop,
  931. struct property *oldprop)
  932. {
  933. struct property **next;
  934. unsigned long flags;
  935. int found = 0;
  936. write_lock_irqsave(&devtree_lock, flags);
  937. next = &np->properties;
  938. while (*next) {
  939. if (*next == oldprop) {
  940. /* found the node */
  941. newprop->next = oldprop->next;
  942. *next = newprop;
  943. oldprop->next = np->deadprops;
  944. np->deadprops = oldprop;
  945. found = 1;
  946. break;
  947. }
  948. next = &(*next)->next;
  949. }
  950. write_unlock_irqrestore(&devtree_lock, flags);
  951. if (!found)
  952. return -ENODEV;
  953. #ifdef CONFIG_PROC_DEVICETREE
  954. /* try to add to proc as well if it was initialized */
  955. if (np->pde)
  956. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  957. #endif /* CONFIG_PROC_DEVICETREE */
  958. return 0;
  959. }
  960. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  961. static struct debugfs_blob_wrapper flat_dt_blob;
  962. static int __init export_flat_device_tree(void)
  963. {
  964. struct dentry *d;
  965. flat_dt_blob.data = initial_boot_params;
  966. flat_dt_blob.size = initial_boot_params->totalsize;
  967. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  968. of_debugfs_root, &flat_dt_blob);
  969. if (!d)
  970. return 1;
  971. return 0;
  972. }
  973. device_initcall(export_flat_device_tree);
  974. #endif