prom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. #undef DEBUG
  16. #include <stdarg.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/threads.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/stringify.h>
  25. #include <linux/delay.h>
  26. #include <linux/initrd.h>
  27. #include <linux/bitops.h>
  28. #include <linux/module.h>
  29. #include <linux/kexec.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/irq.h>
  32. #include <linux/lmb.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. #include <asm/page.h>
  36. #include <asm/processor.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/kdump.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/btext.h>
  47. #include <asm/sections.h>
  48. #include <asm/machdep.h>
  49. #include <asm/pSeries_reconfig.h>
  50. #include <asm/pci-bridge.h>
  51. #include <asm/phyp_dump.h>
  52. #include <asm/kexec.h>
  53. #include <mm/mmu_decl.h>
  54. #ifdef DEBUG
  55. #define DBG(fmt...) printk(KERN_ERR fmt)
  56. #else
  57. #define DBG(fmt...)
  58. #endif
  59. #ifdef CONFIG_PPC64
  60. int __initdata iommu_is_off;
  61. int __initdata iommu_force_on;
  62. unsigned long tce_alloc_start, tce_alloc_end;
  63. #endif
  64. static int __init early_parse_mem(char *p)
  65. {
  66. if (!p)
  67. return 1;
  68. memory_limit = PAGE_ALIGN(memparse(p, &p));
  69. DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
  70. return 0;
  71. }
  72. early_param("mem", early_parse_mem);
  73. /**
  74. * move_device_tree - move tree to an unused area, if needed.
  75. *
  76. * The device tree may be allocated beyond our memory limit, or inside the
  77. * crash kernel region for kdump. If so, move it out of the way.
  78. */
  79. static void __init move_device_tree(void)
  80. {
  81. unsigned long start, size;
  82. void *p;
  83. DBG("-> move_device_tree\n");
  84. start = __pa(initial_boot_params);
  85. size = be32_to_cpu(initial_boot_params->totalsize);
  86. if ((memory_limit && (start + size) > memory_limit) ||
  87. overlaps_crashkernel(start, size)) {
  88. p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
  89. memcpy(p, initial_boot_params, size);
  90. initial_boot_params = (struct boot_param_header *)p;
  91. DBG("Moved device tree to 0x%p\n", p);
  92. }
  93. DBG("<- move_device_tree\n");
  94. }
  95. /*
  96. * ibm,pa-features is a per-cpu property that contains a string of
  97. * attribute descriptors, each of which has a 2 byte header plus up
  98. * to 254 bytes worth of processor attribute bits. First header
  99. * byte specifies the number of bytes following the header.
  100. * Second header byte is an "attribute-specifier" type, of which
  101. * zero is the only currently-defined value.
  102. * Implementation: Pass in the byte and bit offset for the feature
  103. * that we are interested in. The function will return -1 if the
  104. * pa-features property is missing, or a 1/0 to indicate if the feature
  105. * is supported/not supported. Note that the bit numbers are
  106. * big-endian to match the definition in PAPR.
  107. */
  108. static struct ibm_pa_feature {
  109. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  110. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  111. unsigned char pabyte; /* byte number in ibm,pa-features */
  112. unsigned char pabit; /* bit number (big-endian) */
  113. unsigned char invert; /* if 1, pa bit set => clear feature */
  114. } ibm_pa_features[] __initdata = {
  115. {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  116. {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  117. {CPU_FTR_SLB, 0, 0, 2, 0},
  118. {CPU_FTR_CTRL, 0, 0, 3, 0},
  119. {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
  120. {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
  121. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  122. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  123. };
  124. static void __init scan_features(unsigned long node, unsigned char *ftrs,
  125. unsigned long tablelen,
  126. struct ibm_pa_feature *fp,
  127. unsigned long ft_size)
  128. {
  129. unsigned long i, len, bit;
  130. /* find descriptor with type == 0 */
  131. for (;;) {
  132. if (tablelen < 3)
  133. return;
  134. len = 2 + ftrs[0];
  135. if (tablelen < len)
  136. return; /* descriptor 0 not found */
  137. if (ftrs[1] == 0)
  138. break;
  139. tablelen -= len;
  140. ftrs += len;
  141. }
  142. /* loop over bits we know about */
  143. for (i = 0; i < ft_size; ++i, ++fp) {
  144. if (fp->pabyte >= ftrs[0])
  145. continue;
  146. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  147. if (bit ^ fp->invert) {
  148. cur_cpu_spec->cpu_features |= fp->cpu_features;
  149. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  150. } else {
  151. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  152. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  153. }
  154. }
  155. }
  156. static void __init check_cpu_pa_features(unsigned long node)
  157. {
  158. unsigned char *pa_ftrs;
  159. unsigned long tablelen;
  160. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  161. if (pa_ftrs == NULL)
  162. return;
  163. scan_features(node, pa_ftrs, tablelen,
  164. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  165. }
  166. #ifdef CONFIG_PPC_STD_MMU_64
  167. static void __init check_cpu_slb_size(unsigned long node)
  168. {
  169. u32 *slb_size_ptr;
  170. slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
  171. if (slb_size_ptr != NULL) {
  172. mmu_slb_size = *slb_size_ptr;
  173. return;
  174. }
  175. slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  176. if (slb_size_ptr != NULL) {
  177. mmu_slb_size = *slb_size_ptr;
  178. }
  179. }
  180. #else
  181. #define check_cpu_slb_size(node) do { } while(0)
  182. #endif
  183. static struct feature_property {
  184. const char *name;
  185. u32 min_value;
  186. unsigned long cpu_feature;
  187. unsigned long cpu_user_ftr;
  188. } feature_properties[] __initdata = {
  189. #ifdef CONFIG_ALTIVEC
  190. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  191. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  192. #endif /* CONFIG_ALTIVEC */
  193. #ifdef CONFIG_VSX
  194. /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
  195. {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
  196. #endif /* CONFIG_VSX */
  197. #ifdef CONFIG_PPC64
  198. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  199. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  200. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  201. #endif /* CONFIG_PPC64 */
  202. };
  203. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  204. static inline void identical_pvr_fixup(unsigned long node)
  205. {
  206. unsigned int pvr;
  207. char *model = of_get_flat_dt_prop(node, "model", NULL);
  208. /*
  209. * Since 440GR(x)/440EP(x) processors have the same pvr,
  210. * we check the node path and set bit 28 in the cur_cpu_spec
  211. * pvr for EP(x) processor version. This bit is always 0 in
  212. * the "real" pvr. Then we call identify_cpu again with
  213. * the new logical pvr to enable FPU support.
  214. */
  215. if (model && strstr(model, "440EP")) {
  216. pvr = cur_cpu_spec->pvr_value | 0x8;
  217. identify_cpu(0, pvr);
  218. DBG("Using logical pvr %x for %s\n", pvr, model);
  219. }
  220. }
  221. #else
  222. #define identical_pvr_fixup(node) do { } while(0)
  223. #endif
  224. static void __init check_cpu_feature_properties(unsigned long node)
  225. {
  226. unsigned long i;
  227. struct feature_property *fp = feature_properties;
  228. const u32 *prop;
  229. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  230. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  231. if (prop && *prop >= fp->min_value) {
  232. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  233. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  234. }
  235. }
  236. }
  237. static int __init early_init_dt_scan_cpus(unsigned long node,
  238. const char *uname, int depth,
  239. void *data)
  240. {
  241. static int logical_cpuid = 0;
  242. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  243. const u32 *prop;
  244. const u32 *intserv;
  245. int i, nthreads;
  246. unsigned long len;
  247. int found = 0;
  248. /* We are scanning "cpu" nodes only */
  249. if (type == NULL || strcmp(type, "cpu") != 0)
  250. return 0;
  251. /* Get physical cpuid */
  252. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  253. if (intserv) {
  254. nthreads = len / sizeof(int);
  255. } else {
  256. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  257. nthreads = 1;
  258. }
  259. /*
  260. * Now see if any of these threads match our boot cpu.
  261. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  262. */
  263. for (i = 0; i < nthreads; i++) {
  264. /*
  265. * version 2 of the kexec param format adds the phys cpuid of
  266. * booted proc.
  267. */
  268. if (initial_boot_params && initial_boot_params->version >= 2) {
  269. if (intserv[i] ==
  270. initial_boot_params->boot_cpuid_phys) {
  271. found = 1;
  272. break;
  273. }
  274. } else {
  275. /*
  276. * Check if it's the boot-cpu, set it's hw index now,
  277. * unfortunately this format did not support booting
  278. * off secondary threads.
  279. */
  280. if (of_get_flat_dt_prop(node,
  281. "linux,boot-cpu", NULL) != NULL) {
  282. found = 1;
  283. break;
  284. }
  285. }
  286. #ifdef CONFIG_SMP
  287. /* logical cpu id is always 0 on UP kernels */
  288. logical_cpuid++;
  289. #endif
  290. }
  291. if (found) {
  292. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  293. intserv[i]);
  294. boot_cpuid = logical_cpuid;
  295. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  296. /*
  297. * PAPR defines "logical" PVR values for cpus that
  298. * meet various levels of the architecture:
  299. * 0x0f000001 Architecture version 2.04
  300. * 0x0f000002 Architecture version 2.05
  301. * If the cpu-version property in the cpu node contains
  302. * such a value, we call identify_cpu again with the
  303. * logical PVR value in order to use the cpu feature
  304. * bits appropriate for the architecture level.
  305. *
  306. * A POWER6 partition in "POWER6 architected" mode
  307. * uses the 0x0f000002 PVR value; in POWER5+ mode
  308. * it uses 0x0f000001.
  309. */
  310. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  311. if (prop && (*prop & 0xff000000) == 0x0f000000)
  312. identify_cpu(0, *prop);
  313. identical_pvr_fixup(node);
  314. }
  315. check_cpu_feature_properties(node);
  316. check_cpu_pa_features(node);
  317. check_cpu_slb_size(node);
  318. #ifdef CONFIG_PPC_PSERIES
  319. if (nthreads > 1)
  320. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  321. else
  322. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  323. #endif
  324. return 0;
  325. }
  326. void __init early_init_dt_scan_chosen_arch(unsigned long node)
  327. {
  328. unsigned long *lprop;
  329. #ifdef CONFIG_PPC64
  330. /* check if iommu is forced on or off */
  331. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  332. iommu_is_off = 1;
  333. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  334. iommu_force_on = 1;
  335. #endif
  336. /* mem=x on the command line is the preferred mechanism */
  337. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  338. if (lprop)
  339. memory_limit = *lprop;
  340. #ifdef CONFIG_PPC64
  341. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  342. if (lprop)
  343. tce_alloc_start = *lprop;
  344. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  345. if (lprop)
  346. tce_alloc_end = *lprop;
  347. #endif
  348. #ifdef CONFIG_KEXEC
  349. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  350. if (lprop)
  351. crashk_res.start = *lprop;
  352. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  353. if (lprop)
  354. crashk_res.end = crashk_res.start + *lprop - 1;
  355. #endif
  356. }
  357. #ifdef CONFIG_PPC_PSERIES
  358. /*
  359. * Interpret the ibm,dynamic-memory property in the
  360. * /ibm,dynamic-reconfiguration-memory node.
  361. * This contains a list of memory blocks along with NUMA affinity
  362. * information.
  363. */
  364. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  365. {
  366. __be32 *dm, *ls, *usm;
  367. unsigned long l, n, flags;
  368. u64 base, size, lmb_size;
  369. unsigned int is_kexec_kdump = 0, rngs;
  370. ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  371. if (ls == NULL || l < dt_root_size_cells * sizeof(__be32))
  372. return 0;
  373. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  374. dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  375. if (dm == NULL || l < sizeof(__be32))
  376. return 0;
  377. n = *dm++; /* number of entries */
  378. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(__be32))
  379. return 0;
  380. /* check if this is a kexec/kdump kernel. */
  381. usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
  382. &l);
  383. if (usm != NULL)
  384. is_kexec_kdump = 1;
  385. for (; n != 0; --n) {
  386. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  387. flags = dm[3];
  388. /* skip DRC index, pad, assoc. list index, flags */
  389. dm += 4;
  390. /* skip this block if the reserved bit is set in flags (0x80)
  391. or if the block is not assigned to this partition (0x8) */
  392. if ((flags & 0x80) || !(flags & 0x8))
  393. continue;
  394. size = lmb_size;
  395. rngs = 1;
  396. if (is_kexec_kdump) {
  397. /*
  398. * For each lmb in ibm,dynamic-memory, a corresponding
  399. * entry in linux,drconf-usable-memory property contains
  400. * a counter 'p' followed by 'p' (base, size) duple.
  401. * Now read the counter from
  402. * linux,drconf-usable-memory property
  403. */
  404. rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
  405. if (!rngs) /* there are no (base, size) duple */
  406. continue;
  407. }
  408. do {
  409. if (is_kexec_kdump) {
  410. base = dt_mem_next_cell(dt_root_addr_cells,
  411. &usm);
  412. size = dt_mem_next_cell(dt_root_size_cells,
  413. &usm);
  414. }
  415. if (iommu_is_off) {
  416. if (base >= 0x80000000ul)
  417. continue;
  418. if ((base + size) > 0x80000000ul)
  419. size = 0x80000000ul - base;
  420. }
  421. lmb_add(base, size);
  422. } while (--rngs);
  423. }
  424. lmb_dump_all();
  425. return 0;
  426. }
  427. #else
  428. #define early_init_dt_scan_drconf_memory(node) 0
  429. #endif /* CONFIG_PPC_PSERIES */
  430. static int __init early_init_dt_scan_memory_ppc(unsigned long node,
  431. const char *uname,
  432. int depth, void *data)
  433. {
  434. if (depth == 1 &&
  435. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  436. return early_init_dt_scan_drconf_memory(node);
  437. return early_init_dt_scan_memory(node, uname, depth, data);
  438. }
  439. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  440. {
  441. #if defined(CONFIG_PPC64)
  442. if (iommu_is_off) {
  443. if (base >= 0x80000000ul)
  444. return;
  445. if ((base + size) > 0x80000000ul)
  446. size = 0x80000000ul - base;
  447. }
  448. #endif
  449. lmb_add(base, size);
  450. memstart_addr = min((u64)memstart_addr, base);
  451. }
  452. u64 __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  453. {
  454. return lmb_alloc(size, align);
  455. }
  456. #ifdef CONFIG_BLK_DEV_INITRD
  457. void __init early_init_dt_setup_initrd_arch(unsigned long start,
  458. unsigned long end)
  459. {
  460. initrd_start = (unsigned long)__va(start);
  461. initrd_end = (unsigned long)__va(end);
  462. initrd_below_start_ok = 1;
  463. }
  464. #endif
  465. static void __init early_reserve_mem(void)
  466. {
  467. u64 base, size;
  468. u64 *reserve_map;
  469. unsigned long self_base;
  470. unsigned long self_size;
  471. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  472. initial_boot_params->off_mem_rsvmap);
  473. /* before we do anything, lets reserve the dt blob */
  474. self_base = __pa((unsigned long)initial_boot_params);
  475. self_size = initial_boot_params->totalsize;
  476. lmb_reserve(self_base, self_size);
  477. #ifdef CONFIG_BLK_DEV_INITRD
  478. /* then reserve the initrd, if any */
  479. if (initrd_start && (initrd_end > initrd_start))
  480. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  481. #endif /* CONFIG_BLK_DEV_INITRD */
  482. #ifdef CONFIG_PPC32
  483. /*
  484. * Handle the case where we might be booting from an old kexec
  485. * image that setup the mem_rsvmap as pairs of 32-bit values
  486. */
  487. if (*reserve_map > 0xffffffffull) {
  488. u32 base_32, size_32;
  489. u32 *reserve_map_32 = (u32 *)reserve_map;
  490. while (1) {
  491. base_32 = *(reserve_map_32++);
  492. size_32 = *(reserve_map_32++);
  493. if (size_32 == 0)
  494. break;
  495. /* skip if the reservation is for the blob */
  496. if (base_32 == self_base && size_32 == self_size)
  497. continue;
  498. DBG("reserving: %x -> %x\n", base_32, size_32);
  499. lmb_reserve(base_32, size_32);
  500. }
  501. return;
  502. }
  503. #endif
  504. while (1) {
  505. base = *(reserve_map++);
  506. size = *(reserve_map++);
  507. if (size == 0)
  508. break;
  509. DBG("reserving: %llx -> %llx\n", base, size);
  510. lmb_reserve(base, size);
  511. }
  512. }
  513. #ifdef CONFIG_PHYP_DUMP
  514. /**
  515. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  516. *
  517. * Function to find the largest size we need to reserve
  518. * during early boot process.
  519. *
  520. * It either looks for boot param and returns that OR
  521. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  522. *
  523. */
  524. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  525. {
  526. unsigned long tmp;
  527. if (phyp_dump_info->reserve_bootvar)
  528. return phyp_dump_info->reserve_bootvar;
  529. /* divide by 20 to get 5% of value */
  530. tmp = lmb_end_of_DRAM();
  531. do_div(tmp, 20);
  532. /* round it down in multiples of 256 */
  533. tmp = tmp & ~0x0FFFFFFFUL;
  534. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  535. }
  536. /**
  537. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  538. *
  539. * This routine may reserve memory regions in the kernel only
  540. * if the system is supported and a dump was taken in last
  541. * boot instance or if the hardware is supported and the
  542. * scratch area needs to be setup. In other instances it returns
  543. * without reserving anything. The memory in case of dump being
  544. * active is freed when the dump is collected (by userland tools).
  545. */
  546. static void __init phyp_dump_reserve_mem(void)
  547. {
  548. unsigned long base, size;
  549. unsigned long variable_reserve_size;
  550. if (!phyp_dump_info->phyp_dump_configured) {
  551. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  552. return;
  553. }
  554. if (!phyp_dump_info->phyp_dump_at_boot) {
  555. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  556. return;
  557. }
  558. variable_reserve_size = phyp_dump_calculate_reserve_size();
  559. if (phyp_dump_info->phyp_dump_is_active) {
  560. /* Reserve *everything* above RMR.Area freed by userland tools*/
  561. base = variable_reserve_size;
  562. size = lmb_end_of_DRAM() - base;
  563. /* XXX crashed_ram_end is wrong, since it may be beyond
  564. * the memory_limit, it will need to be adjusted. */
  565. lmb_reserve(base, size);
  566. phyp_dump_info->init_reserve_start = base;
  567. phyp_dump_info->init_reserve_size = size;
  568. } else {
  569. size = phyp_dump_info->cpu_state_size +
  570. phyp_dump_info->hpte_region_size +
  571. variable_reserve_size;
  572. base = lmb_end_of_DRAM() - size;
  573. lmb_reserve(base, size);
  574. phyp_dump_info->init_reserve_start = base;
  575. phyp_dump_info->init_reserve_size = size;
  576. }
  577. }
  578. #else
  579. static inline void __init phyp_dump_reserve_mem(void) {}
  580. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  581. void __init early_init_devtree(void *params)
  582. {
  583. phys_addr_t limit;
  584. DBG(" -> early_init_devtree(%p)\n", params);
  585. /* Setup flat device-tree pointer */
  586. initial_boot_params = params;
  587. #ifdef CONFIG_PPC_RTAS
  588. /* Some machines might need RTAS info for debugging, grab it now. */
  589. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  590. #endif
  591. #ifdef CONFIG_PHYP_DUMP
  592. /* scan tree to see if dump occured during last boot */
  593. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  594. #endif
  595. /* Retrieve various informations from the /chosen node of the
  596. * device-tree, including the platform type, initrd location and
  597. * size, TCE reserve, and more ...
  598. */
  599. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  600. /* Scan memory nodes and rebuild LMBs */
  601. lmb_init();
  602. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  603. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  604. /* Save command line for /proc/cmdline and then parse parameters */
  605. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  606. parse_early_param();
  607. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  608. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  609. /* If relocatable, reserve first 32k for interrupt vectors etc. */
  610. if (PHYSICAL_START > MEMORY_START)
  611. lmb_reserve(MEMORY_START, 0x8000);
  612. reserve_kdump_trampoline();
  613. reserve_crashkernel();
  614. early_reserve_mem();
  615. phyp_dump_reserve_mem();
  616. limit = memory_limit;
  617. if (! limit) {
  618. phys_addr_t memsize;
  619. /* Ensure that total memory size is page-aligned, because
  620. * otherwise mark_bootmem() gets upset. */
  621. lmb_analyze();
  622. memsize = lmb_phys_mem_size();
  623. if ((memsize & PAGE_MASK) != memsize)
  624. limit = memsize & PAGE_MASK;
  625. }
  626. lmb_enforce_memory_limit(limit);
  627. lmb_analyze();
  628. lmb_dump_all();
  629. DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
  630. /* We may need to relocate the flat tree, do it now.
  631. * FIXME .. and the initrd too? */
  632. move_device_tree();
  633. DBG("Scanning CPUs ...\n");
  634. /* Retreive CPU related informations from the flat tree
  635. * (altivec support, boot CPU ID, ...)
  636. */
  637. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  638. DBG(" <- early_init_devtree()\n");
  639. }
  640. /*******
  641. *
  642. * New implementation of the OF "find" APIs, return a refcounted
  643. * object, call of_node_put() when done. The device tree and list
  644. * are protected by a rw_lock.
  645. *
  646. * Note that property management will need some locking as well,
  647. * this isn't dealt with yet.
  648. *
  649. *******/
  650. /**
  651. * of_find_next_cache_node - Find a node's subsidiary cache
  652. * @np: node of type "cpu" or "cache"
  653. *
  654. * Returns a node pointer with refcount incremented, use
  655. * of_node_put() on it when done. Caller should hold a reference
  656. * to np.
  657. */
  658. struct device_node *of_find_next_cache_node(struct device_node *np)
  659. {
  660. struct device_node *child;
  661. const phandle *handle;
  662. handle = of_get_property(np, "l2-cache", NULL);
  663. if (!handle)
  664. handle = of_get_property(np, "next-level-cache", NULL);
  665. if (handle)
  666. return of_find_node_by_phandle(*handle);
  667. /* OF on pmac has nodes instead of properties named "l2-cache"
  668. * beneath CPU nodes.
  669. */
  670. if (!strcmp(np->type, "cpu"))
  671. for_each_child_of_node(np, child)
  672. if (!strcmp(child->type, "cache"))
  673. return child;
  674. return NULL;
  675. }
  676. #ifdef CONFIG_PPC_PSERIES
  677. /*
  678. * Fix up the uninitialized fields in a new device node:
  679. * name, type and pci-specific fields
  680. */
  681. static int of_finish_dynamic_node(struct device_node *node)
  682. {
  683. struct device_node *parent = of_get_parent(node);
  684. int err = 0;
  685. const phandle *ibm_phandle;
  686. node->name = of_get_property(node, "name", NULL);
  687. node->type = of_get_property(node, "device_type", NULL);
  688. if (!node->name)
  689. node->name = "<NULL>";
  690. if (!node->type)
  691. node->type = "<NULL>";
  692. if (!parent) {
  693. err = -ENODEV;
  694. goto out;
  695. }
  696. /* We don't support that function on PowerMac, at least
  697. * not yet
  698. */
  699. if (machine_is(powermac))
  700. return -ENODEV;
  701. /* fix up new node's phandle field */
  702. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  703. node->phandle = *ibm_phandle;
  704. out:
  705. of_node_put(parent);
  706. return err;
  707. }
  708. static int prom_reconfig_notifier(struct notifier_block *nb,
  709. unsigned long action, void *node)
  710. {
  711. int err;
  712. switch (action) {
  713. case PSERIES_RECONFIG_ADD:
  714. err = of_finish_dynamic_node(node);
  715. if (err < 0) {
  716. printk(KERN_ERR "finish_node returned %d\n", err);
  717. err = NOTIFY_BAD;
  718. }
  719. break;
  720. default:
  721. err = NOTIFY_DONE;
  722. break;
  723. }
  724. return err;
  725. }
  726. static struct notifier_block prom_reconfig_nb = {
  727. .notifier_call = prom_reconfig_notifier,
  728. .priority = 10, /* This one needs to run first */
  729. };
  730. static int __init prom_reconfig_setup(void)
  731. {
  732. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  733. }
  734. __initcall(prom_reconfig_setup);
  735. #endif
  736. /* Find the device node for a given logical cpu number, also returns the cpu
  737. * local thread number (index in ibm,interrupt-server#s) if relevant and
  738. * asked for (non NULL)
  739. */
  740. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  741. {
  742. int hardid;
  743. struct device_node *np;
  744. hardid = get_hard_smp_processor_id(cpu);
  745. for_each_node_by_type(np, "cpu") {
  746. const u32 *intserv;
  747. unsigned int plen, t;
  748. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  749. * fallback to "reg" property and assume no threads
  750. */
  751. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  752. &plen);
  753. if (intserv == NULL) {
  754. const u32 *reg = of_get_property(np, "reg", NULL);
  755. if (reg == NULL)
  756. continue;
  757. if (*reg == hardid) {
  758. if (thread)
  759. *thread = 0;
  760. return np;
  761. }
  762. } else {
  763. plen /= sizeof(u32);
  764. for (t = 0; t < plen; t++) {
  765. if (hardid == intserv[t]) {
  766. if (thread)
  767. *thread = t;
  768. return np;
  769. }
  770. }
  771. }
  772. }
  773. return NULL;
  774. }
  775. EXPORT_SYMBOL(of_get_cpu_node);
  776. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  777. static struct debugfs_blob_wrapper flat_dt_blob;
  778. static int __init export_flat_device_tree(void)
  779. {
  780. struct dentry *d;
  781. flat_dt_blob.data = initial_boot_params;
  782. flat_dt_blob.size = initial_boot_params->totalsize;
  783. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  784. powerpc_debugfs_root, &flat_dt_blob);
  785. if (!d)
  786. return 1;
  787. return 0;
  788. }
  789. __initcall(export_flat_device_tree);
  790. #endif