prom.c 25 KB

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