setup_64.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. *
  3. * Common boot and setup code.
  4. *
  5. * Copyright (C) 2001 PPC64 Team, IBM Corp
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/export.h>
  13. #include <linux/string.h>
  14. #include <linux/sched.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/reboot.h>
  18. #include <linux/delay.h>
  19. #include <linux/initrd.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/ioport.h>
  22. #include <linux/console.h>
  23. #include <linux/utsname.h>
  24. #include <linux/tty.h>
  25. #include <linux/root_dev.h>
  26. #include <linux/notifier.h>
  27. #include <linux/cpu.h>
  28. #include <linux/unistd.h>
  29. #include <linux/serial.h>
  30. #include <linux/serial_8250.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/pci.h>
  33. #include <linux/lockdep.h>
  34. #include <linux/memblock.h>
  35. #include <linux/memory.h>
  36. #include <linux/nmi.h>
  37. #include <asm/debugfs.h>
  38. #include <asm/io.h>
  39. #include <asm/kdump.h>
  40. #include <asm/prom.h>
  41. #include <asm/processor.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/smp.h>
  44. #include <asm/elf.h>
  45. #include <asm/machdep.h>
  46. #include <asm/paca.h>
  47. #include <asm/time.h>
  48. #include <asm/cputable.h>
  49. #include <asm/dt_cpu_ftrs.h>
  50. #include <asm/sections.h>
  51. #include <asm/btext.h>
  52. #include <asm/nvram.h>
  53. #include <asm/setup.h>
  54. #include <asm/rtas.h>
  55. #include <asm/iommu.h>
  56. #include <asm/serial.h>
  57. #include <asm/cache.h>
  58. #include <asm/page.h>
  59. #include <asm/mmu.h>
  60. #include <asm/firmware.h>
  61. #include <asm/xmon.h>
  62. #include <asm/udbg.h>
  63. #include <asm/kexec.h>
  64. #include <asm/code-patching.h>
  65. #include <asm/livepatch.h>
  66. #include <asm/opal.h>
  67. #include <asm/cputhreads.h>
  68. #include <asm/hw_irq.h>
  69. #include "setup.h"
  70. #ifdef DEBUG
  71. #define DBG(fmt...) udbg_printf(fmt)
  72. #else
  73. #define DBG(fmt...)
  74. #endif
  75. int spinning_secondaries;
  76. u64 ppc64_pft_size;
  77. struct ppc64_caches ppc64_caches = {
  78. .l1d = {
  79. .block_size = 0x40,
  80. .log_block_size = 6,
  81. },
  82. .l1i = {
  83. .block_size = 0x40,
  84. .log_block_size = 6
  85. },
  86. };
  87. EXPORT_SYMBOL_GPL(ppc64_caches);
  88. #if defined(CONFIG_PPC_BOOK3E) && defined(CONFIG_SMP)
  89. void __init setup_tlb_core_data(void)
  90. {
  91. int cpu;
  92. BUILD_BUG_ON(offsetof(struct tlb_core_data, lock) != 0);
  93. for_each_possible_cpu(cpu) {
  94. int first = cpu_first_thread_sibling(cpu);
  95. /*
  96. * If we boot via kdump on a non-primary thread,
  97. * make sure we point at the thread that actually
  98. * set up this TLB.
  99. */
  100. if (cpu_first_thread_sibling(boot_cpuid) == first)
  101. first = boot_cpuid;
  102. paca[cpu].tcd_ptr = &paca[first].tcd;
  103. /*
  104. * If we have threads, we need either tlbsrx.
  105. * or e6500 tablewalk mode, or else TLB handlers
  106. * will be racy and could produce duplicate entries.
  107. * Should we panic instead?
  108. */
  109. WARN_ONCE(smt_enabled_at_boot >= 2 &&
  110. !mmu_has_feature(MMU_FTR_USE_TLBRSRV) &&
  111. book3e_htw_mode != PPC_HTW_E6500,
  112. "%s: unsupported MMU configuration\n", __func__);
  113. }
  114. }
  115. #endif
  116. #ifdef CONFIG_SMP
  117. static char *smt_enabled_cmdline;
  118. /* Look for ibm,smt-enabled OF option */
  119. void __init check_smt_enabled(void)
  120. {
  121. struct device_node *dn;
  122. const char *smt_option;
  123. /* Default to enabling all threads */
  124. smt_enabled_at_boot = threads_per_core;
  125. /* Allow the command line to overrule the OF option */
  126. if (smt_enabled_cmdline) {
  127. if (!strcmp(smt_enabled_cmdline, "on"))
  128. smt_enabled_at_boot = threads_per_core;
  129. else if (!strcmp(smt_enabled_cmdline, "off"))
  130. smt_enabled_at_boot = 0;
  131. else {
  132. int smt;
  133. int rc;
  134. rc = kstrtoint(smt_enabled_cmdline, 10, &smt);
  135. if (!rc)
  136. smt_enabled_at_boot =
  137. min(threads_per_core, smt);
  138. }
  139. } else {
  140. dn = of_find_node_by_path("/options");
  141. if (dn) {
  142. smt_option = of_get_property(dn, "ibm,smt-enabled",
  143. NULL);
  144. if (smt_option) {
  145. if (!strcmp(smt_option, "on"))
  146. smt_enabled_at_boot = threads_per_core;
  147. else if (!strcmp(smt_option, "off"))
  148. smt_enabled_at_boot = 0;
  149. }
  150. of_node_put(dn);
  151. }
  152. }
  153. }
  154. /* Look for smt-enabled= cmdline option */
  155. static int __init early_smt_enabled(char *p)
  156. {
  157. smt_enabled_cmdline = p;
  158. return 0;
  159. }
  160. early_param("smt-enabled", early_smt_enabled);
  161. #endif /* CONFIG_SMP */
  162. /** Fix up paca fields required for the boot cpu */
  163. static void __init fixup_boot_paca(void)
  164. {
  165. /* The boot cpu is started */
  166. get_paca()->cpu_start = 1;
  167. /* Allow percpu accesses to work until we setup percpu data */
  168. get_paca()->data_offset = 0;
  169. /* Mark interrupts disabled in PACA */
  170. irq_soft_mask_set(IRQS_DISABLED);
  171. }
  172. static void __init configure_exceptions(void)
  173. {
  174. /*
  175. * Setup the trampolines from the lowmem exception vectors
  176. * to the kdump kernel when not using a relocatable kernel.
  177. */
  178. setup_kdump_trampoline();
  179. /* Under a PAPR hypervisor, we need hypercalls */
  180. if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
  181. /* Enable AIL if possible */
  182. pseries_enable_reloc_on_exc();
  183. /*
  184. * Tell the hypervisor that we want our exceptions to
  185. * be taken in little endian mode.
  186. *
  187. * We don't call this for big endian as our calling convention
  188. * makes us always enter in BE, and the call may fail under
  189. * some circumstances with kdump.
  190. */
  191. #ifdef __LITTLE_ENDIAN__
  192. pseries_little_endian_exceptions();
  193. #endif
  194. } else {
  195. /* Set endian mode using OPAL */
  196. if (firmware_has_feature(FW_FEATURE_OPAL))
  197. opal_configure_cores();
  198. /* AIL on native is done in cpu_ready_for_interrupts() */
  199. }
  200. }
  201. static void cpu_ready_for_interrupts(void)
  202. {
  203. /*
  204. * Enable AIL if supported, and we are in hypervisor mode. This
  205. * is called once for every processor.
  206. *
  207. * If we are not in hypervisor mode the job is done once for
  208. * the whole partition in configure_exceptions().
  209. */
  210. if (cpu_has_feature(CPU_FTR_HVMODE) &&
  211. cpu_has_feature(CPU_FTR_ARCH_207S)) {
  212. unsigned long lpcr = mfspr(SPRN_LPCR);
  213. mtspr(SPRN_LPCR, lpcr | LPCR_AIL_3);
  214. }
  215. /*
  216. * Fixup HFSCR:TM based on CPU features. The bit is set by our
  217. * early asm init because at that point we haven't updated our
  218. * CPU features from firmware and device-tree. Here we have,
  219. * so let's do it.
  220. */
  221. if (cpu_has_feature(CPU_FTR_HVMODE) && !cpu_has_feature(CPU_FTR_TM_COMP))
  222. mtspr(SPRN_HFSCR, mfspr(SPRN_HFSCR) & ~HFSCR_TM);
  223. /* Set IR and DR in PACA MSR */
  224. get_paca()->kernel_msr = MSR_KERNEL;
  225. }
  226. /*
  227. * Early initialization entry point. This is called by head.S
  228. * with MMU translation disabled. We rely on the "feature" of
  229. * the CPU that ignores the top 2 bits of the address in real
  230. * mode so we can access kernel globals normally provided we
  231. * only toy with things in the RMO region. From here, we do
  232. * some early parsing of the device-tree to setup out MEMBLOCK
  233. * data structures, and allocate & initialize the hash table
  234. * and segment tables so we can start running with translation
  235. * enabled.
  236. *
  237. * It is this function which will call the probe() callback of
  238. * the various platform types and copy the matching one to the
  239. * global ppc_md structure. Your platform can eventually do
  240. * some very early initializations from the probe() routine, but
  241. * this is not recommended, be very careful as, for example, the
  242. * device-tree is not accessible via normal means at this point.
  243. */
  244. void __init early_setup(unsigned long dt_ptr)
  245. {
  246. static __initdata struct paca_struct boot_paca;
  247. /* -------- printk is _NOT_ safe to use here ! ------- */
  248. /* Try new device tree based feature discovery ... */
  249. if (!dt_cpu_ftrs_init(__va(dt_ptr)))
  250. /* Otherwise use the old style CPU table */
  251. identify_cpu(0, mfspr(SPRN_PVR));
  252. /* Assume we're on cpu 0 for now. Don't write to the paca yet! */
  253. initialise_paca(&boot_paca, 0);
  254. setup_paca(&boot_paca);
  255. fixup_boot_paca();
  256. /* -------- printk is now safe to use ------- */
  257. /* Enable early debugging if any specified (see udbg.h) */
  258. udbg_early_init();
  259. DBG(" -> early_setup(), dt_ptr: 0x%lx\n", dt_ptr);
  260. /*
  261. * Do early initialization using the flattened device
  262. * tree, such as retrieving the physical memory map or
  263. * calculating/retrieving the hash table size.
  264. */
  265. early_init_devtree(__va(dt_ptr));
  266. /* Now we know the logical id of our boot cpu, setup the paca. */
  267. setup_paca(&paca[boot_cpuid]);
  268. fixup_boot_paca();
  269. /*
  270. * Configure exception handlers. This include setting up trampolines
  271. * if needed, setting exception endian mode, etc...
  272. */
  273. configure_exceptions();
  274. /* Apply all the dynamic patching */
  275. apply_feature_fixups();
  276. setup_feature_keys();
  277. /* Initialize the hash table or TLB handling */
  278. early_init_mmu();
  279. /*
  280. * After firmware and early platform setup code has set things up,
  281. * we note the SPR values for configurable control/performance
  282. * registers, and use those as initial defaults.
  283. */
  284. record_spr_defaults();
  285. /*
  286. * At this point, we can let interrupts switch to virtual mode
  287. * (the MMU has been setup), so adjust the MSR in the PACA to
  288. * have IR and DR set and enable AIL if it exists
  289. */
  290. cpu_ready_for_interrupts();
  291. DBG(" <- early_setup()\n");
  292. #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
  293. /*
  294. * This needs to be done *last* (after the above DBG() even)
  295. *
  296. * Right after we return from this function, we turn on the MMU
  297. * which means the real-mode access trick that btext does will
  298. * no longer work, it needs to switch to using a real MMU
  299. * mapping. This call will ensure that it does
  300. */
  301. btext_map();
  302. #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
  303. }
  304. #ifdef CONFIG_SMP
  305. void early_setup_secondary(void)
  306. {
  307. /* Mark interrupts disabled in PACA */
  308. irq_soft_mask_set(IRQS_DISABLED);
  309. /* Initialize the hash table or TLB handling */
  310. early_init_mmu_secondary();
  311. /*
  312. * At this point, we can let interrupts switch to virtual mode
  313. * (the MMU has been setup), so adjust the MSR in the PACA to
  314. * have IR and DR set.
  315. */
  316. cpu_ready_for_interrupts();
  317. }
  318. #endif /* CONFIG_SMP */
  319. #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
  320. static bool use_spinloop(void)
  321. {
  322. if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
  323. /*
  324. * See comments in head_64.S -- not all platforms insert
  325. * secondaries at __secondary_hold and wait at the spin
  326. * loop.
  327. */
  328. if (firmware_has_feature(FW_FEATURE_OPAL))
  329. return false;
  330. return true;
  331. }
  332. /*
  333. * When book3e boots from kexec, the ePAPR spin table does
  334. * not get used.
  335. */
  336. return of_property_read_bool(of_chosen, "linux,booted-from-kexec");
  337. }
  338. void smp_release_cpus(void)
  339. {
  340. unsigned long *ptr;
  341. int i;
  342. if (!use_spinloop())
  343. return;
  344. DBG(" -> smp_release_cpus()\n");
  345. /* All secondary cpus are spinning on a common spinloop, release them
  346. * all now so they can start to spin on their individual paca
  347. * spinloops. For non SMP kernels, the secondary cpus never get out
  348. * of the common spinloop.
  349. */
  350. ptr = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
  351. - PHYSICAL_START);
  352. *ptr = ppc_function_entry(generic_secondary_smp_init);
  353. /* And wait a bit for them to catch up */
  354. for (i = 0; i < 100000; i++) {
  355. mb();
  356. HMT_low();
  357. if (spinning_secondaries == 0)
  358. break;
  359. udelay(1);
  360. }
  361. DBG("spinning_secondaries = %d\n", spinning_secondaries);
  362. DBG(" <- smp_release_cpus()\n");
  363. }
  364. #endif /* CONFIG_SMP || CONFIG_KEXEC_CORE */
  365. /*
  366. * Initialize some remaining members of the ppc64_caches and systemcfg
  367. * structures
  368. * (at least until we get rid of them completely). This is mostly some
  369. * cache informations about the CPU that will be used by cache flush
  370. * routines and/or provided to userland
  371. */
  372. static void init_cache_info(struct ppc_cache_info *info, u32 size, u32 lsize,
  373. u32 bsize, u32 sets)
  374. {
  375. info->size = size;
  376. info->sets = sets;
  377. info->line_size = lsize;
  378. info->block_size = bsize;
  379. info->log_block_size = __ilog2(bsize);
  380. if (bsize)
  381. info->blocks_per_page = PAGE_SIZE / bsize;
  382. else
  383. info->blocks_per_page = 0;
  384. if (sets == 0)
  385. info->assoc = 0xffff;
  386. else
  387. info->assoc = size / (sets * lsize);
  388. }
  389. static bool __init parse_cache_info(struct device_node *np,
  390. bool icache,
  391. struct ppc_cache_info *info)
  392. {
  393. static const char *ipropnames[] __initdata = {
  394. "i-cache-size",
  395. "i-cache-sets",
  396. "i-cache-block-size",
  397. "i-cache-line-size",
  398. };
  399. static const char *dpropnames[] __initdata = {
  400. "d-cache-size",
  401. "d-cache-sets",
  402. "d-cache-block-size",
  403. "d-cache-line-size",
  404. };
  405. const char **propnames = icache ? ipropnames : dpropnames;
  406. const __be32 *sizep, *lsizep, *bsizep, *setsp;
  407. u32 size, lsize, bsize, sets;
  408. bool success = true;
  409. size = 0;
  410. sets = -1u;
  411. lsize = bsize = cur_cpu_spec->dcache_bsize;
  412. sizep = of_get_property(np, propnames[0], NULL);
  413. if (sizep != NULL)
  414. size = be32_to_cpu(*sizep);
  415. setsp = of_get_property(np, propnames[1], NULL);
  416. if (setsp != NULL)
  417. sets = be32_to_cpu(*setsp);
  418. bsizep = of_get_property(np, propnames[2], NULL);
  419. lsizep = of_get_property(np, propnames[3], NULL);
  420. if (bsizep == NULL)
  421. bsizep = lsizep;
  422. if (lsizep != NULL)
  423. lsize = be32_to_cpu(*lsizep);
  424. if (bsizep != NULL)
  425. bsize = be32_to_cpu(*bsizep);
  426. if (sizep == NULL || bsizep == NULL || lsizep == NULL)
  427. success = false;
  428. /*
  429. * OF is weird .. it represents fully associative caches
  430. * as "1 way" which doesn't make much sense and doesn't
  431. * leave room for direct mapped. We'll assume that 0
  432. * in OF means direct mapped for that reason.
  433. */
  434. if (sets == 1)
  435. sets = 0;
  436. else if (sets == 0)
  437. sets = 1;
  438. init_cache_info(info, size, lsize, bsize, sets);
  439. return success;
  440. }
  441. void __init initialize_cache_info(void)
  442. {
  443. struct device_node *cpu = NULL, *l2, *l3 = NULL;
  444. u32 pvr;
  445. DBG(" -> initialize_cache_info()\n");
  446. /*
  447. * All shipping POWER8 machines have a firmware bug that
  448. * puts incorrect information in the device-tree. This will
  449. * be (hopefully) fixed for future chips but for now hard
  450. * code the values if we are running on one of these
  451. */
  452. pvr = PVR_VER(mfspr(SPRN_PVR));
  453. if (pvr == PVR_POWER8 || pvr == PVR_POWER8E ||
  454. pvr == PVR_POWER8NVL) {
  455. /* size lsize blk sets */
  456. init_cache_info(&ppc64_caches.l1i, 0x8000, 128, 128, 32);
  457. init_cache_info(&ppc64_caches.l1d, 0x10000, 128, 128, 64);
  458. init_cache_info(&ppc64_caches.l2, 0x80000, 128, 0, 512);
  459. init_cache_info(&ppc64_caches.l3, 0x800000, 128, 0, 8192);
  460. } else
  461. cpu = of_find_node_by_type(NULL, "cpu");
  462. /*
  463. * We're assuming *all* of the CPUs have the same
  464. * d-cache and i-cache sizes... -Peter
  465. */
  466. if (cpu) {
  467. if (!parse_cache_info(cpu, false, &ppc64_caches.l1d))
  468. DBG("Argh, can't find dcache properties !\n");
  469. if (!parse_cache_info(cpu, true, &ppc64_caches.l1i))
  470. DBG("Argh, can't find icache properties !\n");
  471. /*
  472. * Try to find the L2 and L3 if any. Assume they are
  473. * unified and use the D-side properties.
  474. */
  475. l2 = of_find_next_cache_node(cpu);
  476. of_node_put(cpu);
  477. if (l2) {
  478. parse_cache_info(l2, false, &ppc64_caches.l2);
  479. l3 = of_find_next_cache_node(l2);
  480. of_node_put(l2);
  481. }
  482. if (l3) {
  483. parse_cache_info(l3, false, &ppc64_caches.l3);
  484. of_node_put(l3);
  485. }
  486. }
  487. /* For use by binfmt_elf */
  488. dcache_bsize = ppc64_caches.l1d.block_size;
  489. icache_bsize = ppc64_caches.l1i.block_size;
  490. cur_cpu_spec->dcache_bsize = dcache_bsize;
  491. cur_cpu_spec->icache_bsize = icache_bsize;
  492. DBG(" <- initialize_cache_info()\n");
  493. }
  494. /*
  495. * This returns the limit below which memory accesses to the linear
  496. * mapping are guarnateed not to cause an architectural exception (e.g.,
  497. * TLB or SLB miss fault).
  498. *
  499. * This is used to allocate PACAs and various interrupt stacks that
  500. * that are accessed early in interrupt handlers that must not cause
  501. * re-entrant interrupts.
  502. */
  503. __init u64 ppc64_bolted_size(void)
  504. {
  505. #ifdef CONFIG_PPC_BOOK3E
  506. /* Freescale BookE bolts the entire linear mapping */
  507. /* XXX: BookE ppc64_rma_limit setup seems to disagree? */
  508. if (early_mmu_has_feature(MMU_FTR_TYPE_FSL_E))
  509. return linear_map_top;
  510. /* Other BookE, we assume the first GB is bolted */
  511. return 1ul << 30;
  512. #else
  513. /* BookS radix, does not take faults on linear mapping */
  514. if (early_radix_enabled())
  515. return ULONG_MAX;
  516. /* BookS hash, the first segment is bolted */
  517. if (early_mmu_has_feature(MMU_FTR_1T_SEGMENT))
  518. return 1UL << SID_SHIFT_1T;
  519. return 1UL << SID_SHIFT;
  520. #endif
  521. }
  522. void __init irqstack_early_init(void)
  523. {
  524. u64 limit = ppc64_bolted_size();
  525. unsigned int i;
  526. /*
  527. * Interrupt stacks must be in the first segment since we
  528. * cannot afford to take SLB misses on them. They are not
  529. * accessed in realmode.
  530. */
  531. for_each_possible_cpu(i) {
  532. softirq_ctx[i] = (struct thread_info *)
  533. __va(memblock_alloc_base(THREAD_SIZE,
  534. THREAD_SIZE, limit));
  535. hardirq_ctx[i] = (struct thread_info *)
  536. __va(memblock_alloc_base(THREAD_SIZE,
  537. THREAD_SIZE, limit));
  538. }
  539. }
  540. #ifdef CONFIG_PPC_BOOK3E
  541. void __init exc_lvl_early_init(void)
  542. {
  543. unsigned int i;
  544. unsigned long sp;
  545. for_each_possible_cpu(i) {
  546. sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  547. critirq_ctx[i] = (struct thread_info *)__va(sp);
  548. paca[i].crit_kstack = __va(sp + THREAD_SIZE);
  549. sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  550. dbgirq_ctx[i] = (struct thread_info *)__va(sp);
  551. paca[i].dbg_kstack = __va(sp + THREAD_SIZE);
  552. sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  553. mcheckirq_ctx[i] = (struct thread_info *)__va(sp);
  554. paca[i].mc_kstack = __va(sp + THREAD_SIZE);
  555. }
  556. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
  557. patch_exception(0x040, exc_debug_debug_book3e);
  558. }
  559. #endif
  560. /*
  561. * Emergency stacks are used for a range of things, from asynchronous
  562. * NMIs (system reset, machine check) to synchronous, process context.
  563. * We set preempt_count to zero, even though that isn't necessarily correct. To
  564. * get the right value we'd need to copy it from the previous thread_info, but
  565. * doing that might fault causing more problems.
  566. * TODO: what to do with accounting?
  567. */
  568. static void emerg_stack_init_thread_info(struct thread_info *ti, int cpu)
  569. {
  570. ti->task = NULL;
  571. ti->cpu = cpu;
  572. ti->preempt_count = 0;
  573. ti->local_flags = 0;
  574. ti->flags = 0;
  575. klp_init_thread_info(ti);
  576. }
  577. /*
  578. * Stack space used when we detect a bad kernel stack pointer, and
  579. * early in SMP boots before relocation is enabled. Exclusive emergency
  580. * stack for machine checks.
  581. */
  582. void __init emergency_stack_init(void)
  583. {
  584. u64 limit;
  585. unsigned int i;
  586. /*
  587. * Emergency stacks must be under 256MB, we cannot afford to take
  588. * SLB misses on them. The ABI also requires them to be 128-byte
  589. * aligned.
  590. *
  591. * Since we use these as temporary stacks during secondary CPU
  592. * bringup, machine check, system reset, and HMI, we need to get
  593. * at them in real mode. This means they must also be within the RMO
  594. * region.
  595. *
  596. * The IRQ stacks allocated elsewhere in this file are zeroed and
  597. * initialized in kernel/irq.c. These are initialized here in order
  598. * to have emergency stacks available as early as possible.
  599. */
  600. limit = min(ppc64_bolted_size(), ppc64_rma_size);
  601. for_each_possible_cpu(i) {
  602. struct thread_info *ti;
  603. ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
  604. memset(ti, 0, THREAD_SIZE);
  605. emerg_stack_init_thread_info(ti, i);
  606. paca[i].emergency_sp = (void *)ti + THREAD_SIZE;
  607. #ifdef CONFIG_PPC_BOOK3S_64
  608. /* emergency stack for NMI exception handling. */
  609. ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
  610. memset(ti, 0, THREAD_SIZE);
  611. emerg_stack_init_thread_info(ti, i);
  612. paca[i].nmi_emergency_sp = (void *)ti + THREAD_SIZE;
  613. /* emergency stack for machine check exception handling. */
  614. ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
  615. memset(ti, 0, THREAD_SIZE);
  616. emerg_stack_init_thread_info(ti, i);
  617. paca[i].mc_emergency_sp = (void *)ti + THREAD_SIZE;
  618. #endif
  619. }
  620. }
  621. #ifdef CONFIG_SMP
  622. #define PCPU_DYN_SIZE ()
  623. static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
  624. {
  625. return __alloc_bootmem_node(NODE_DATA(early_cpu_to_node(cpu)), size, align,
  626. __pa(MAX_DMA_ADDRESS));
  627. }
  628. static void __init pcpu_fc_free(void *ptr, size_t size)
  629. {
  630. free_bootmem(__pa(ptr), size);
  631. }
  632. static int pcpu_cpu_distance(unsigned int from, unsigned int to)
  633. {
  634. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  635. return LOCAL_DISTANCE;
  636. else
  637. return REMOTE_DISTANCE;
  638. }
  639. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  640. EXPORT_SYMBOL(__per_cpu_offset);
  641. void __init setup_per_cpu_areas(void)
  642. {
  643. const size_t dyn_size = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  644. size_t atom_size;
  645. unsigned long delta;
  646. unsigned int cpu;
  647. int rc;
  648. /*
  649. * Linear mapping is one of 4K, 1M and 16M. For 4K, no need
  650. * to group units. For larger mappings, use 1M atom which
  651. * should be large enough to contain a number of units.
  652. */
  653. if (mmu_linear_psize == MMU_PAGE_4K)
  654. atom_size = PAGE_SIZE;
  655. else
  656. atom_size = 1 << 20;
  657. rc = pcpu_embed_first_chunk(0, dyn_size, atom_size, pcpu_cpu_distance,
  658. pcpu_fc_alloc, pcpu_fc_free);
  659. if (rc < 0)
  660. panic("cannot initialize percpu area (err=%d)", rc);
  661. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  662. for_each_possible_cpu(cpu) {
  663. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  664. paca[cpu].data_offset = __per_cpu_offset[cpu];
  665. }
  666. }
  667. #endif
  668. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  669. unsigned long memory_block_size_bytes(void)
  670. {
  671. if (ppc_md.memory_block_size)
  672. return ppc_md.memory_block_size();
  673. return MIN_MEMORY_BLOCK_SIZE;
  674. }
  675. #endif
  676. #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
  677. struct ppc_pci_io ppc_pci_io;
  678. EXPORT_SYMBOL(ppc_pci_io);
  679. #endif
  680. #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
  681. u64 hw_nmi_get_sample_period(int watchdog_thresh)
  682. {
  683. return ppc_proc_freq * watchdog_thresh;
  684. }
  685. #endif
  686. /*
  687. * The perf based hardlockup detector breaks PMU event based branches, so
  688. * disable it by default. Book3S has a soft-nmi hardlockup detector based
  689. * on the decrementer interrupt, so it does not suffer from this problem.
  690. *
  691. * It is likely to get false positives in VM guests, so disable it there
  692. * by default too.
  693. */
  694. static int __init disable_hardlockup_detector(void)
  695. {
  696. #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
  697. hardlockup_detector_disable();
  698. #else
  699. if (firmware_has_feature(FW_FEATURE_LPAR))
  700. hardlockup_detector_disable();
  701. #endif
  702. return 0;
  703. }
  704. early_initcall(disable_hardlockup_detector);
  705. #ifdef CONFIG_PPC_BOOK3S_64
  706. static enum l1d_flush_type enabled_flush_types;
  707. static void *l1d_flush_fallback_area;
  708. static bool no_rfi_flush;
  709. bool rfi_flush;
  710. static int __init handle_no_rfi_flush(char *p)
  711. {
  712. pr_info("rfi-flush: disabled on command line.");
  713. no_rfi_flush = true;
  714. return 0;
  715. }
  716. early_param("no_rfi_flush", handle_no_rfi_flush);
  717. /*
  718. * The RFI flush is not KPTI, but because users will see doco that says to use
  719. * nopti we hijack that option here to also disable the RFI flush.
  720. */
  721. static int __init handle_no_pti(char *p)
  722. {
  723. pr_info("rfi-flush: disabling due to 'nopti' on command line.\n");
  724. handle_no_rfi_flush(NULL);
  725. return 0;
  726. }
  727. early_param("nopti", handle_no_pti);
  728. static void do_nothing(void *unused)
  729. {
  730. /*
  731. * We don't need to do the flush explicitly, just enter+exit kernel is
  732. * sufficient, the RFI exit handlers will do the right thing.
  733. */
  734. }
  735. void rfi_flush_enable(bool enable)
  736. {
  737. if (rfi_flush == enable)
  738. return;
  739. if (enable) {
  740. do_rfi_flush_fixups(enabled_flush_types);
  741. on_each_cpu(do_nothing, NULL, 1);
  742. } else
  743. do_rfi_flush_fixups(L1D_FLUSH_NONE);
  744. rfi_flush = enable;
  745. }
  746. static void init_fallback_flush(void)
  747. {
  748. u64 l1d_size, limit;
  749. int cpu;
  750. l1d_size = ppc64_caches.l1d.size;
  751. limit = min(ppc64_bolted_size(), ppc64_rma_size);
  752. /*
  753. * Align to L1d size, and size it at 2x L1d size, to catch possible
  754. * hardware prefetch runoff. We don't have a recipe for load patterns to
  755. * reliably avoid the prefetcher.
  756. */
  757. l1d_flush_fallback_area = __va(memblock_alloc_base(l1d_size * 2, l1d_size, limit));
  758. memset(l1d_flush_fallback_area, 0, l1d_size * 2);
  759. for_each_possible_cpu(cpu) {
  760. paca[cpu].rfi_flush_fallback_area = l1d_flush_fallback_area;
  761. paca[cpu].l1d_flush_size = l1d_size;
  762. }
  763. }
  764. void __init setup_rfi_flush(enum l1d_flush_type types, bool enable)
  765. {
  766. if (types & L1D_FLUSH_FALLBACK) {
  767. pr_info("rfi-flush: Using fallback displacement flush\n");
  768. init_fallback_flush();
  769. }
  770. if (types & L1D_FLUSH_ORI)
  771. pr_info("rfi-flush: Using ori type flush\n");
  772. if (types & L1D_FLUSH_MTTRIG)
  773. pr_info("rfi-flush: Using mttrig type flush\n");
  774. enabled_flush_types = types;
  775. if (!no_rfi_flush)
  776. rfi_flush_enable(enable);
  777. }
  778. #ifdef CONFIG_DEBUG_FS
  779. static int rfi_flush_set(void *data, u64 val)
  780. {
  781. if (val == 1)
  782. rfi_flush_enable(true);
  783. else if (val == 0)
  784. rfi_flush_enable(false);
  785. else
  786. return -EINVAL;
  787. return 0;
  788. }
  789. static int rfi_flush_get(void *data, u64 *val)
  790. {
  791. *val = rfi_flush ? 1 : 0;
  792. return 0;
  793. }
  794. DEFINE_SIMPLE_ATTRIBUTE(fops_rfi_flush, rfi_flush_get, rfi_flush_set, "%llu\n");
  795. static __init int rfi_flush_debugfs_init(void)
  796. {
  797. debugfs_create_file("rfi_flush", 0600, powerpc_debugfs_root, NULL, &fops_rfi_flush);
  798. return 0;
  799. }
  800. device_initcall(rfi_flush_debugfs_init);
  801. #endif
  802. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
  803. {
  804. if (rfi_flush)
  805. return sprintf(buf, "Mitigation: RFI Flush\n");
  806. return sprintf(buf, "Vulnerable\n");
  807. }
  808. #endif /* CONFIG_PPC_BOOK3S_64 */