prom.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Procedures for creating, accessing and interpreting the device tree.
  4. *
  5. * Paul Mackerras August 1996.
  6. * Copyright (C) 1996-2005 Paul Mackerras.
  7. *
  8. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  9. * {engebret|bergner}@us.ibm.com
  10. */
  11. #undef DEBUG
  12. #include <stdarg.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/init.h>
  16. #include <linux/threads.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. #include <linux/pci.h>
  20. #include <linux/delay.h>
  21. #include <linux/initrd.h>
  22. #include <linux/bitops.h>
  23. #include <linux/export.h>
  24. #include <linux/kexec.h>
  25. #include <linux/irq.h>
  26. #include <linux/memblock.h>
  27. #include <linux/of.h>
  28. #include <linux/of_fdt.h>
  29. #include <linux/libfdt.h>
  30. #include <linux/cpu.h>
  31. #include <linux/pgtable.h>
  32. #include <asm/prom.h>
  33. #include <asm/rtas.h>
  34. #include <asm/page.h>
  35. #include <asm/processor.h>
  36. #include <asm/irq.h>
  37. #include <asm/io.h>
  38. #include <asm/kdump.h>
  39. #include <asm/smp.h>
  40. #include <asm/mmu.h>
  41. #include <asm/paca.h>
  42. #include <asm/powernv.h>
  43. #include <asm/iommu.h>
  44. #include <asm/btext.h>
  45. #include <asm/sections.h>
  46. #include <asm/machdep.h>
  47. #include <asm/pci-bridge.h>
  48. #include <asm/kexec.h>
  49. #include <asm/opal.h>
  50. #include <asm/fadump.h>
  51. #include <asm/epapr_hcalls.h>
  52. #include <asm/firmware.h>
  53. #include <asm/dt_cpu_ftrs.h>
  54. #include <asm/drmem.h>
  55. #include <asm/ultravisor.h>
  56. #include <mm/mmu_decl.h>
  57. #ifdef DEBUG
  58. #define DBG(fmt...) printk(KERN_ERR fmt)
  59. #else
  60. #define DBG(fmt...)
  61. #endif
  62. #ifdef CONFIG_PPC64
  63. int __initdata iommu_is_off;
  64. int __initdata iommu_force_on;
  65. unsigned long tce_alloc_start, tce_alloc_end;
  66. u64 ppc64_rma_size;
  67. #endif
  68. static phys_addr_t first_memblock_size;
  69. static int __initdata boot_cpu_count;
  70. static int __init early_parse_mem(char *p)
  71. {
  72. if (!p)
  73. return 1;
  74. memory_limit = PAGE_ALIGN(memparse(p, &p));
  75. DBG("memory limit = 0x%llx\n", memory_limit);
  76. return 0;
  77. }
  78. early_param("mem", early_parse_mem);
  79. /*
  80. * overlaps_initrd - check for overlap with page aligned extension of
  81. * initrd.
  82. */
  83. static inline int overlaps_initrd(unsigned long start, unsigned long size)
  84. {
  85. #ifdef CONFIG_BLK_DEV_INITRD
  86. if (!initrd_start)
  87. return 0;
  88. return (start + size) > ALIGN_DOWN(initrd_start, PAGE_SIZE) &&
  89. start <= ALIGN(initrd_end, PAGE_SIZE);
  90. #else
  91. return 0;
  92. #endif
  93. }
  94. /**
  95. * move_device_tree - move tree to an unused area, if needed.
  96. *
  97. * The device tree may be allocated beyond our memory limit, or inside the
  98. * crash kernel region for kdump, or within the page aligned range of initrd.
  99. * If so, move it out of the way.
  100. */
  101. static void __init move_device_tree(void)
  102. {
  103. unsigned long start, size;
  104. void *p;
  105. DBG("-> move_device_tree\n");
  106. start = __pa(initial_boot_params);
  107. size = fdt_totalsize(initial_boot_params);
  108. if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
  109. !memblock_is_memory(start + size - 1) ||
  110. overlaps_crashkernel(start, size) || overlaps_initrd(start, size)) {
  111. p = memblock_alloc_raw(size, PAGE_SIZE);
  112. if (!p)
  113. panic("Failed to allocate %lu bytes to move device tree\n",
  114. size);
  115. memcpy(p, initial_boot_params, size);
  116. initial_boot_params = p;
  117. DBG("Moved device tree to 0x%px\n", p);
  118. }
  119. DBG("<- move_device_tree\n");
  120. }
  121. /*
  122. * ibm,pa-features is a per-cpu property that contains a string of
  123. * attribute descriptors, each of which has a 2 byte header plus up
  124. * to 254 bytes worth of processor attribute bits. First header
  125. * byte specifies the number of bytes following the header.
  126. * Second header byte is an "attribute-specifier" type, of which
  127. * zero is the only currently-defined value.
  128. * Implementation: Pass in the byte and bit offset for the feature
  129. * that we are interested in. The function will return -1 if the
  130. * pa-features property is missing, or a 1/0 to indicate if the feature
  131. * is supported/not supported. Note that the bit numbers are
  132. * big-endian to match the definition in PAPR.
  133. */
  134. static struct ibm_pa_feature {
  135. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  136. unsigned long mmu_features; /* MMU_FTR_xxx bit */
  137. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  138. unsigned int cpu_user_ftrs2; /* PPC_FEATURE2_xxx bit */
  139. unsigned char pabyte; /* byte number in ibm,pa-features */
  140. unsigned char pabit; /* bit number (big-endian) */
  141. unsigned char invert; /* if 1, pa bit set => clear feature */
  142. } ibm_pa_features[] __initdata = {
  143. { .pabyte = 0, .pabit = 0, .cpu_user_ftrs = PPC_FEATURE_HAS_MMU },
  144. { .pabyte = 0, .pabit = 1, .cpu_user_ftrs = PPC_FEATURE_HAS_FPU },
  145. { .pabyte = 0, .pabit = 3, .cpu_features = CPU_FTR_CTRL },
  146. { .pabyte = 0, .pabit = 6, .cpu_features = CPU_FTR_NOEXECUTE },
  147. { .pabyte = 1, .pabit = 2, .mmu_features = MMU_FTR_CI_LARGE_PAGE },
  148. #ifdef CONFIG_PPC_RADIX_MMU
  149. { .pabyte = 40, .pabit = 0, .mmu_features = MMU_FTR_TYPE_RADIX | MMU_FTR_GTSE },
  150. #endif
  151. { .pabyte = 1, .pabit = 1, .invert = 1, .cpu_features = CPU_FTR_NODSISRALIGN },
  152. { .pabyte = 5, .pabit = 0, .cpu_features = CPU_FTR_REAL_LE,
  153. .cpu_user_ftrs = PPC_FEATURE_TRUE_LE },
  154. /*
  155. * If the kernel doesn't support TM (ie CONFIG_PPC_TRANSACTIONAL_MEM=n),
  156. * we don't want to turn on TM here, so we use the *_COMP versions
  157. * which are 0 if the kernel doesn't support TM.
  158. */
  159. { .pabyte = 22, .pabit = 0, .cpu_features = CPU_FTR_TM_COMP,
  160. .cpu_user_ftrs2 = PPC_FEATURE2_HTM_COMP | PPC_FEATURE2_HTM_NOSC_COMP },
  161. { .pabyte = 64, .pabit = 0, .cpu_features = CPU_FTR_DAWR1 },
  162. };
  163. static void __init scan_features(unsigned long node, const unsigned char *ftrs,
  164. unsigned long tablelen,
  165. struct ibm_pa_feature *fp,
  166. unsigned long ft_size)
  167. {
  168. unsigned long i, len, bit;
  169. /* find descriptor with type == 0 */
  170. for (;;) {
  171. if (tablelen < 3)
  172. return;
  173. len = 2 + ftrs[0];
  174. if (tablelen < len)
  175. return; /* descriptor 0 not found */
  176. if (ftrs[1] == 0)
  177. break;
  178. tablelen -= len;
  179. ftrs += len;
  180. }
  181. /* loop over bits we know about */
  182. for (i = 0; i < ft_size; ++i, ++fp) {
  183. if (fp->pabyte >= ftrs[0])
  184. continue;
  185. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  186. if (bit ^ fp->invert) {
  187. cur_cpu_spec->cpu_features |= fp->cpu_features;
  188. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  189. cur_cpu_spec->cpu_user_features2 |= fp->cpu_user_ftrs2;
  190. cur_cpu_spec->mmu_features |= fp->mmu_features;
  191. } else {
  192. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  193. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  194. cur_cpu_spec->cpu_user_features2 &= ~fp->cpu_user_ftrs2;
  195. cur_cpu_spec->mmu_features &= ~fp->mmu_features;
  196. }
  197. }
  198. }
  199. static void __init check_cpu_pa_features(unsigned long node)
  200. {
  201. const unsigned char *pa_ftrs;
  202. int tablelen;
  203. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  204. if (pa_ftrs == NULL)
  205. return;
  206. scan_features(node, pa_ftrs, tablelen,
  207. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  208. }
  209. #ifdef CONFIG_PPC_BOOK3S_64
  210. static void __init init_mmu_slb_size(unsigned long node)
  211. {
  212. const __be32 *slb_size_ptr;
  213. slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL) ? :
  214. of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  215. if (slb_size_ptr)
  216. mmu_slb_size = be32_to_cpup(slb_size_ptr);
  217. }
  218. #else
  219. #define init_mmu_slb_size(node) do { } while(0)
  220. #endif
  221. static struct feature_property {
  222. const char *name;
  223. u32 min_value;
  224. unsigned long cpu_feature;
  225. unsigned long cpu_user_ftr;
  226. } feature_properties[] __initdata = {
  227. #ifdef CONFIG_ALTIVEC
  228. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  229. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  230. #endif /* CONFIG_ALTIVEC */
  231. #ifdef CONFIG_VSX
  232. /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
  233. {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
  234. #endif /* CONFIG_VSX */
  235. #ifdef CONFIG_PPC64
  236. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  237. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  238. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  239. #endif /* CONFIG_PPC64 */
  240. };
  241. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  242. static __init void identical_pvr_fixup(unsigned long node)
  243. {
  244. unsigned int pvr;
  245. const char *model = of_get_flat_dt_prop(node, "model", NULL);
  246. /*
  247. * Since 440GR(x)/440EP(x) processors have the same pvr,
  248. * we check the node path and set bit 28 in the cur_cpu_spec
  249. * pvr for EP(x) processor version. This bit is always 0 in
  250. * the "real" pvr. Then we call identify_cpu again with
  251. * the new logical pvr to enable FPU support.
  252. */
  253. if (model && strstr(model, "440EP")) {
  254. pvr = cur_cpu_spec->pvr_value | 0x8;
  255. identify_cpu(0, pvr);
  256. DBG("Using logical pvr %x for %s\n", pvr, model);
  257. }
  258. }
  259. #else
  260. #define identical_pvr_fixup(node) do { } while(0)
  261. #endif
  262. static void __init check_cpu_feature_properties(unsigned long node)
  263. {
  264. int i;
  265. struct feature_property *fp = feature_properties;
  266. const __be32 *prop;
  267. for (i = 0; i < (int)ARRAY_SIZE(feature_properties); ++i, ++fp) {
  268. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  269. if (prop && be32_to_cpup(prop) >= fp->min_value) {
  270. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  271. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  272. }
  273. }
  274. }
  275. static int __init early_init_dt_scan_cpus(unsigned long node,
  276. const char *uname, int depth,
  277. void *data)
  278. {
  279. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  280. const __be32 *prop;
  281. const __be32 *intserv;
  282. int i, nthreads;
  283. int len;
  284. int found = -1;
  285. int found_thread = 0;
  286. /* We are scanning "cpu" nodes only */
  287. if (type == NULL || strcmp(type, "cpu") != 0)
  288. return 0;
  289. /* Get physical cpuid */
  290. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  291. if (!intserv)
  292. intserv = of_get_flat_dt_prop(node, "reg", &len);
  293. nthreads = len / sizeof(int);
  294. /*
  295. * Now see if any of these threads match our boot cpu.
  296. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  297. */
  298. for (i = 0; i < nthreads; i++) {
  299. if (be32_to_cpu(intserv[i]) ==
  300. fdt_boot_cpuid_phys(initial_boot_params)) {
  301. found = boot_cpu_count;
  302. found_thread = i;
  303. }
  304. #ifdef CONFIG_SMP
  305. /* logical cpu id is always 0 on UP kernels */
  306. boot_cpu_count++;
  307. #endif
  308. }
  309. /* Not the boot CPU */
  310. if (found < 0)
  311. return 0;
  312. DBG("boot cpu: logical %d physical %d\n", found,
  313. be32_to_cpu(intserv[found_thread]));
  314. boot_cpuid = found;
  315. /*
  316. * PAPR defines "logical" PVR values for cpus that
  317. * meet various levels of the architecture:
  318. * 0x0f000001 Architecture version 2.04
  319. * 0x0f000002 Architecture version 2.05
  320. * If the cpu-version property in the cpu node contains
  321. * such a value, we call identify_cpu again with the
  322. * logical PVR value in order to use the cpu feature
  323. * bits appropriate for the architecture level.
  324. *
  325. * A POWER6 partition in "POWER6 architected" mode
  326. * uses the 0x0f000002 PVR value; in POWER5+ mode
  327. * it uses 0x0f000001.
  328. *
  329. * If we're using device tree CPU feature discovery then we don't
  330. * support the cpu-version property, and it's the responsibility of the
  331. * firmware/hypervisor to provide the correct feature set for the
  332. * architecture level via the ibm,powerpc-cpu-features binding.
  333. */
  334. if (!dt_cpu_ftrs_in_use()) {
  335. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  336. if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000)
  337. identify_cpu(0, be32_to_cpup(prop));
  338. check_cpu_feature_properties(node);
  339. check_cpu_pa_features(node);
  340. }
  341. identical_pvr_fixup(node);
  342. init_mmu_slb_size(node);
  343. #ifdef CONFIG_PPC64
  344. if (nthreads == 1)
  345. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  346. else if (!dt_cpu_ftrs_in_use())
  347. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  348. allocate_paca(boot_cpuid);
  349. #endif
  350. set_hard_smp_processor_id(found, be32_to_cpu(intserv[found_thread]));
  351. return 0;
  352. }
  353. static int __init early_init_dt_scan_chosen_ppc(unsigned long node,
  354. const char *uname,
  355. int depth, void *data)
  356. {
  357. const unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
  358. /* Use common scan routine to determine if this is the chosen node */
  359. if (early_init_dt_scan_chosen(node, uname, depth, data) == 0)
  360. return 0;
  361. #ifdef CONFIG_PPC64
  362. /* check if iommu is forced on or off */
  363. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  364. iommu_is_off = 1;
  365. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  366. iommu_force_on = 1;
  367. #endif
  368. /* mem=x on the command line is the preferred mechanism */
  369. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  370. if (lprop)
  371. memory_limit = *lprop;
  372. #ifdef CONFIG_PPC64
  373. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  374. if (lprop)
  375. tce_alloc_start = *lprop;
  376. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  377. if (lprop)
  378. tce_alloc_end = *lprop;
  379. #endif
  380. #ifdef CONFIG_KEXEC_CORE
  381. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  382. if (lprop)
  383. crashk_res.start = *lprop;
  384. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  385. if (lprop)
  386. crashk_res.end = crashk_res.start + *lprop - 1;
  387. #endif
  388. /* break now */
  389. return 1;
  390. }
  391. /*
  392. * Compare the range against max mem limit and update
  393. * size if it cross the limit.
  394. */
  395. #ifdef CONFIG_SPARSEMEM
  396. static bool validate_mem_limit(u64 base, u64 *size)
  397. {
  398. u64 max_mem = 1UL << (MAX_PHYSMEM_BITS);
  399. if (base >= max_mem)
  400. return false;
  401. if ((base + *size) > max_mem)
  402. *size = max_mem - base;
  403. return true;
  404. }
  405. #else
  406. static bool validate_mem_limit(u64 base, u64 *size)
  407. {
  408. return true;
  409. }
  410. #endif
  411. #ifdef CONFIG_PPC_PSERIES
  412. /*
  413. * Interpret the ibm dynamic reconfiguration memory LMBs.
  414. * This contains a list of memory blocks along with NUMA affinity
  415. * information.
  416. */
  417. static int __init early_init_drmem_lmb(struct drmem_lmb *lmb,
  418. const __be32 **usm,
  419. void *data)
  420. {
  421. u64 base, size;
  422. int is_kexec_kdump = 0, rngs;
  423. base = lmb->base_addr;
  424. size = drmem_lmb_size();
  425. rngs = 1;
  426. /*
  427. * Skip this block if the reserved bit is set in flags
  428. * or if the block is not assigned to this partition.
  429. */
  430. if ((lmb->flags & DRCONF_MEM_RESERVED) ||
  431. !(lmb->flags & DRCONF_MEM_ASSIGNED))
  432. return 0;
  433. if (*usm)
  434. is_kexec_kdump = 1;
  435. if (is_kexec_kdump) {
  436. /*
  437. * For each memblock in ibm,dynamic-memory, a
  438. * corresponding entry in linux,drconf-usable-memory
  439. * property contains a counter 'p' followed by 'p'
  440. * (base, size) duple. Now read the counter from
  441. * linux,drconf-usable-memory property
  442. */
  443. rngs = dt_mem_next_cell(dt_root_size_cells, usm);
  444. if (!rngs) /* there are no (base, size) duple */
  445. return 0;
  446. }
  447. do {
  448. if (is_kexec_kdump) {
  449. base = dt_mem_next_cell(dt_root_addr_cells, usm);
  450. size = dt_mem_next_cell(dt_root_size_cells, usm);
  451. }
  452. if (iommu_is_off) {
  453. if (base >= 0x80000000ul)
  454. continue;
  455. if ((base + size) > 0x80000000ul)
  456. size = 0x80000000ul - base;
  457. }
  458. if (!validate_mem_limit(base, &size))
  459. continue;
  460. DBG("Adding: %llx -> %llx\n", base, size);
  461. memblock_add(base, size);
  462. if (lmb->flags & DRCONF_MEM_HOTREMOVABLE)
  463. memblock_mark_hotplug(base, size);
  464. } while (--rngs);
  465. return 0;
  466. }
  467. #endif /* CONFIG_PPC_PSERIES */
  468. static int __init early_init_dt_scan_memory_ppc(unsigned long node,
  469. const char *uname,
  470. int depth, void *data)
  471. {
  472. #ifdef CONFIG_PPC_PSERIES
  473. if (depth == 1 &&
  474. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0) {
  475. walk_drmem_lmbs_early(node, NULL, early_init_drmem_lmb);
  476. return 0;
  477. }
  478. #endif
  479. return early_init_dt_scan_memory(node, uname, depth, data);
  480. }
  481. /*
  482. * For a relocatable kernel, we need to get the memstart_addr first,
  483. * then use it to calculate the virtual kernel start address. This has
  484. * to happen at a very early stage (before machine_init). In this case,
  485. * we just want to get the memstart_address and would not like to mess the
  486. * memblock at this stage. So introduce a variable to skip the memblock_add()
  487. * for this reason.
  488. */
  489. #ifdef CONFIG_RELOCATABLE
  490. static int add_mem_to_memblock = 1;
  491. #else
  492. #define add_mem_to_memblock 1
  493. #endif
  494. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  495. {
  496. #ifdef CONFIG_PPC64
  497. if (iommu_is_off) {
  498. if (base >= 0x80000000ul)
  499. return;
  500. if ((base + size) > 0x80000000ul)
  501. size = 0x80000000ul - base;
  502. }
  503. #endif
  504. /* Keep track of the beginning of memory -and- the size of
  505. * the very first block in the device-tree as it represents
  506. * the RMA on ppc64 server
  507. */
  508. if (base < memstart_addr) {
  509. memstart_addr = base;
  510. first_memblock_size = size;
  511. }
  512. /* Add the chunk to the MEMBLOCK list */
  513. if (add_mem_to_memblock) {
  514. if (validate_mem_limit(base, &size))
  515. memblock_add(base, size);
  516. }
  517. }
  518. static void __init early_reserve_mem_dt(void)
  519. {
  520. unsigned long i, dt_root;
  521. int len;
  522. const __be32 *prop;
  523. early_init_fdt_reserve_self();
  524. early_init_fdt_scan_reserved_mem();
  525. dt_root = of_get_flat_dt_root();
  526. prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
  527. if (!prop)
  528. return;
  529. DBG("Found new-style reserved-ranges\n");
  530. /* Each reserved range is an (address,size) pair, 2 cells each,
  531. * totalling 4 cells per range. */
  532. for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
  533. u64 base, size;
  534. base = of_read_number(prop + (i * 4) + 0, 2);
  535. size = of_read_number(prop + (i * 4) + 2, 2);
  536. if (size) {
  537. DBG("reserving: %llx -> %llx\n", base, size);
  538. memblock_reserve(base, size);
  539. }
  540. }
  541. }
  542. static void __init early_reserve_mem(void)
  543. {
  544. __be64 *reserve_map;
  545. reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
  546. fdt_off_mem_rsvmap(initial_boot_params));
  547. /* Look for the new "reserved-regions" property in the DT */
  548. early_reserve_mem_dt();
  549. #ifdef CONFIG_BLK_DEV_INITRD
  550. /* Then reserve the initrd, if any */
  551. if (initrd_start && (initrd_end > initrd_start)) {
  552. memblock_reserve(ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
  553. ALIGN(initrd_end, PAGE_SIZE) -
  554. ALIGN_DOWN(initrd_start, PAGE_SIZE));
  555. }
  556. #endif /* CONFIG_BLK_DEV_INITRD */
  557. #ifdef CONFIG_PPC32
  558. /*
  559. * Handle the case where we might be booting from an old kexec
  560. * image that setup the mem_rsvmap as pairs of 32-bit values
  561. */
  562. if (be64_to_cpup(reserve_map) > 0xffffffffull) {
  563. u32 base_32, size_32;
  564. __be32 *reserve_map_32 = (__be32 *)reserve_map;
  565. DBG("Found old 32-bit reserve map\n");
  566. while (1) {
  567. base_32 = be32_to_cpup(reserve_map_32++);
  568. size_32 = be32_to_cpup(reserve_map_32++);
  569. if (size_32 == 0)
  570. break;
  571. DBG("reserving: %x -> %x\n", base_32, size_32);
  572. memblock_reserve(base_32, size_32);
  573. }
  574. return;
  575. }
  576. #endif
  577. }
  578. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  579. static bool tm_disabled __initdata;
  580. static int __init parse_ppc_tm(char *str)
  581. {
  582. bool res;
  583. if (kstrtobool(str, &res))
  584. return -EINVAL;
  585. tm_disabled = !res;
  586. return 0;
  587. }
  588. early_param("ppc_tm", parse_ppc_tm);
  589. static void __init tm_init(void)
  590. {
  591. if (tm_disabled) {
  592. pr_info("Disabling hardware transactional memory (HTM)\n");
  593. cur_cpu_spec->cpu_user_features2 &=
  594. ~(PPC_FEATURE2_HTM_NOSC | PPC_FEATURE2_HTM);
  595. cur_cpu_spec->cpu_features &= ~CPU_FTR_TM;
  596. return;
  597. }
  598. pnv_tm_init();
  599. }
  600. #else
  601. static void tm_init(void) { }
  602. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  603. #ifdef CONFIG_PPC64
  604. static void __init save_fscr_to_task(void)
  605. {
  606. /*
  607. * Ensure the init_task (pid 0, aka swapper) uses the value of FSCR we
  608. * have configured via the device tree features or via __init_FSCR().
  609. * That value will then be propagated to pid 1 (init) and all future
  610. * processes.
  611. */
  612. if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
  613. init_task.thread.fscr = mfspr(SPRN_FSCR);
  614. }
  615. #else
  616. static inline void save_fscr_to_task(void) {};
  617. #endif
  618. void __init early_init_devtree(void *params)
  619. {
  620. phys_addr_t limit;
  621. DBG(" -> early_init_devtree(%px)\n", params);
  622. /* Too early to BUG_ON(), do it by hand */
  623. if (!early_init_dt_verify(params))
  624. panic("BUG: Failed verifying flat device tree, bad version?");
  625. #ifdef CONFIG_PPC_RTAS
  626. /* Some machines might need RTAS info for debugging, grab it now. */
  627. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  628. #endif
  629. #ifdef CONFIG_PPC_POWERNV
  630. /* Some machines might need OPAL info for debugging, grab it now. */
  631. of_scan_flat_dt(early_init_dt_scan_opal, NULL);
  632. /* Scan tree for ultravisor feature */
  633. of_scan_flat_dt(early_init_dt_scan_ultravisor, NULL);
  634. #endif
  635. #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
  636. /* scan tree to see if dump is active during last boot */
  637. of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
  638. #endif
  639. /* Retrieve various informations from the /chosen node of the
  640. * device-tree, including the platform type, initrd location and
  641. * size, TCE reserve, and more ...
  642. */
  643. of_scan_flat_dt(early_init_dt_scan_chosen_ppc, boot_command_line);
  644. /* Scan memory nodes and rebuild MEMBLOCKs */
  645. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  646. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  647. parse_early_param();
  648. /* make sure we've parsed cmdline for mem= before this */
  649. if (memory_limit)
  650. first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
  651. setup_initial_memory_limit(memstart_addr, first_memblock_size);
  652. /* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
  653. memblock_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  654. /* If relocatable, reserve first 32k for interrupt vectors etc. */
  655. if (PHYSICAL_START > MEMORY_START)
  656. memblock_reserve(MEMORY_START, 0x8000);
  657. reserve_kdump_trampoline();
  658. #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
  659. /*
  660. * If we fail to reserve memory for firmware-assisted dump then
  661. * fallback to kexec based kdump.
  662. */
  663. if (fadump_reserve_mem() == 0)
  664. #endif
  665. reserve_crashkernel();
  666. early_reserve_mem();
  667. /* Ensure that total memory size is page-aligned. */
  668. limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
  669. memblock_enforce_memory_limit(limit);
  670. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_PPC_4K_PAGES)
  671. if (!early_radix_enabled())
  672. memblock_cap_memory_range(0, 1UL << (H_MAX_PHYSMEM_BITS));
  673. #endif
  674. memblock_allow_resize();
  675. memblock_dump_all();
  676. DBG("Phys. mem: %llx\n", (unsigned long long)memblock_phys_mem_size());
  677. /* We may need to relocate the flat tree, do it now.
  678. * FIXME .. and the initrd too? */
  679. move_device_tree();
  680. allocate_paca_ptrs();
  681. DBG("Scanning CPUs ...\n");
  682. dt_cpu_ftrs_scan();
  683. /* Retrieve CPU related informations from the flat tree
  684. * (altivec support, boot CPU ID, ...)
  685. */
  686. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  687. if (boot_cpuid < 0) {
  688. printk("Failed to identify boot CPU !\n");
  689. BUG();
  690. }
  691. save_fscr_to_task();
  692. #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
  693. /* We'll later wait for secondaries to check in; there are
  694. * NCPUS-1 non-boot CPUs :-)
  695. */
  696. spinning_secondaries = boot_cpu_count - 1;
  697. #endif
  698. mmu_early_init_devtree();
  699. #ifdef CONFIG_PPC_POWERNV
  700. /* Scan and build the list of machine check recoverable ranges */
  701. of_scan_flat_dt(early_init_dt_scan_recoverable_ranges, NULL);
  702. #endif
  703. epapr_paravirt_early_init();
  704. /* Now try to figure out if we are running on LPAR and so on */
  705. pseries_probe_fw_features();
  706. /*
  707. * Initialize pkey features and default AMR/IAMR values
  708. */
  709. pkey_early_init_devtree();
  710. #ifdef CONFIG_PPC_PS3
  711. /* Identify PS3 firmware */
  712. if (of_flat_dt_is_compatible(of_get_flat_dt_root(), "sony,ps3"))
  713. powerpc_firmware_features |= FW_FEATURE_PS3_POSSIBLE;
  714. #endif
  715. tm_init();
  716. DBG(" <- early_init_devtree()\n");
  717. }
  718. #ifdef CONFIG_RELOCATABLE
  719. /*
  720. * This function run before early_init_devtree, so we have to init
  721. * initial_boot_params.
  722. */
  723. void __init early_get_first_memblock_info(void *params, phys_addr_t *size)
  724. {
  725. /* Setup flat device-tree pointer */
  726. initial_boot_params = params;
  727. /*
  728. * Scan the memory nodes and set add_mem_to_memblock to 0 to avoid
  729. * mess the memblock.
  730. */
  731. add_mem_to_memblock = 0;
  732. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  733. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  734. add_mem_to_memblock = 1;
  735. if (size)
  736. *size = first_memblock_size;
  737. }
  738. #endif
  739. /*******
  740. *
  741. * New implementation of the OF "find" APIs, return a refcounted
  742. * object, call of_node_put() when done. The device tree and list
  743. * are protected by a rw_lock.
  744. *
  745. * Note that property management will need some locking as well,
  746. * this isn't dealt with yet.
  747. *
  748. *******/
  749. /**
  750. * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
  751. * @np: device node of the device
  752. *
  753. * This looks for a property "ibm,chip-id" in the node or any
  754. * of its parents and returns its content, or -1 if it cannot
  755. * be found.
  756. */
  757. int of_get_ibm_chip_id(struct device_node *np)
  758. {
  759. of_node_get(np);
  760. while (np) {
  761. u32 chip_id;
  762. /*
  763. * Skiboot may produce memory nodes that contain more than one
  764. * cell in chip-id, we only read the first one here.
  765. */
  766. if (!of_property_read_u32(np, "ibm,chip-id", &chip_id)) {
  767. of_node_put(np);
  768. return chip_id;
  769. }
  770. np = of_get_next_parent(np);
  771. }
  772. return -1;
  773. }
  774. EXPORT_SYMBOL(of_get_ibm_chip_id);
  775. /**
  776. * cpu_to_chip_id - Return the cpus chip-id
  777. * @cpu: The logical cpu number.
  778. *
  779. * Return the value of the ibm,chip-id property corresponding to the given
  780. * logical cpu number. If the chip-id can not be found, returns -1.
  781. */
  782. int cpu_to_chip_id(int cpu)
  783. {
  784. struct device_node *np;
  785. np = of_get_cpu_node(cpu, NULL);
  786. if (!np)
  787. return -1;
  788. of_node_put(np);
  789. return of_get_ibm_chip_id(np);
  790. }
  791. EXPORT_SYMBOL(cpu_to_chip_id);
  792. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  793. {
  794. #ifdef CONFIG_SMP
  795. /*
  796. * Early firmware scanning must use this rather than
  797. * get_hard_smp_processor_id because we don't have pacas allocated
  798. * until memory topology is discovered.
  799. */
  800. if (cpu_to_phys_id != NULL)
  801. return (int)phys_id == cpu_to_phys_id[cpu];
  802. #endif
  803. return (int)phys_id == get_hard_smp_processor_id(cpu);
  804. }