prom.c 24 KB

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