prom.c 25 KB

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