prom.c 28 KB

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