prom.c 24 KB

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