prom.c 23 KB

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