setup_64.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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_ptrs[cpu]->tcd_ptr = &paca_ptrs[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. unsigned long spr_default_dscr = 0;
  227. void __init record_spr_defaults(void)
  228. {
  229. if (early_cpu_has_feature(CPU_FTR_DSCR))
  230. spr_default_dscr = mfspr(SPRN_DSCR);
  231. }
  232. /*
  233. * Early initialization entry point. This is called by head.S
  234. * with MMU translation disabled. We rely on the "feature" of
  235. * the CPU that ignores the top 2 bits of the address in real
  236. * mode so we can access kernel globals normally provided we
  237. * only toy with things in the RMO region. From here, we do
  238. * some early parsing of the device-tree to setup out MEMBLOCK
  239. * data structures, and allocate & initialize the hash table
  240. * and segment tables so we can start running with translation
  241. * enabled.
  242. *
  243. * It is this function which will call the probe() callback of
  244. * the various platform types and copy the matching one to the
  245. * global ppc_md structure. Your platform can eventually do
  246. * some very early initializations from the probe() routine, but
  247. * this is not recommended, be very careful as, for example, the
  248. * device-tree is not accessible via normal means at this point.
  249. */
  250. void __init early_setup(unsigned long dt_ptr)
  251. {
  252. static __initdata struct paca_struct boot_paca;
  253. /* -------- printk is _NOT_ safe to use here ! ------- */
  254. /* Try new device tree based feature discovery ... */
  255. if (!dt_cpu_ftrs_init(__va(dt_ptr)))
  256. /* Otherwise use the old style CPU table */
  257. identify_cpu(0, mfspr(SPRN_PVR));
  258. /* Assume we're on cpu 0 for now. Don't write to the paca yet! */
  259. initialise_paca(&boot_paca, 0);
  260. setup_paca(&boot_paca);
  261. fixup_boot_paca();
  262. /* -------- printk is now safe to use ------- */
  263. /* Enable early debugging if any specified (see udbg.h) */
  264. udbg_early_init();
  265. DBG(" -> early_setup(), dt_ptr: 0x%lx\n", dt_ptr);
  266. /*
  267. * Do early initialization using the flattened device
  268. * tree, such as retrieving the physical memory map or
  269. * calculating/retrieving the hash table size.
  270. */
  271. early_init_devtree(__va(dt_ptr));
  272. /* Now we know the logical id of our boot cpu, setup the paca. */
  273. if (boot_cpuid != 0) {
  274. /* Poison paca_ptrs[0] again if it's not the boot cpu */
  275. memset(&paca_ptrs[0], 0x88, sizeof(paca_ptrs[0]));
  276. }
  277. setup_paca(paca_ptrs[boot_cpuid]);
  278. fixup_boot_paca();
  279. /*
  280. * Configure exception handlers. This include setting up trampolines
  281. * if needed, setting exception endian mode, etc...
  282. */
  283. configure_exceptions();
  284. /* Apply all the dynamic patching */
  285. apply_feature_fixups();
  286. setup_feature_keys();
  287. /* Initialize the hash table or TLB handling */
  288. early_init_mmu();
  289. /*
  290. * After firmware and early platform setup code has set things up,
  291. * we note the SPR values for configurable control/performance
  292. * registers, and use those as initial defaults.
  293. */
  294. record_spr_defaults();
  295. /*
  296. * At this point, we can let interrupts switch to virtual mode
  297. * (the MMU has been setup), so adjust the MSR in the PACA to
  298. * have IR and DR set and enable AIL if it exists
  299. */
  300. cpu_ready_for_interrupts();
  301. /*
  302. * We enable ftrace here, but since we only support DYNAMIC_FTRACE, it
  303. * will only actually get enabled on the boot cpu much later once
  304. * ftrace itself has been initialized.
  305. */
  306. this_cpu_enable_ftrace();
  307. DBG(" <- early_setup()\n");
  308. #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
  309. /*
  310. * This needs to be done *last* (after the above DBG() even)
  311. *
  312. * Right after we return from this function, we turn on the MMU
  313. * which means the real-mode access trick that btext does will
  314. * no longer work, it needs to switch to using a real MMU
  315. * mapping. This call will ensure that it does
  316. */
  317. btext_map();
  318. #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
  319. }
  320. #ifdef CONFIG_SMP
  321. void early_setup_secondary(void)
  322. {
  323. /* Mark interrupts disabled in PACA */
  324. irq_soft_mask_set(IRQS_DISABLED);
  325. /* Initialize the hash table or TLB handling */
  326. early_init_mmu_secondary();
  327. /*
  328. * At this point, we can let interrupts switch to virtual mode
  329. * (the MMU has been setup), so adjust the MSR in the PACA to
  330. * have IR and DR set.
  331. */
  332. cpu_ready_for_interrupts();
  333. }
  334. #endif /* CONFIG_SMP */
  335. void panic_smp_self_stop(void)
  336. {
  337. hard_irq_disable();
  338. spin_begin();
  339. while (1)
  340. spin_cpu_relax();
  341. }
  342. #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
  343. static bool use_spinloop(void)
  344. {
  345. if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
  346. /*
  347. * See comments in head_64.S -- not all platforms insert
  348. * secondaries at __secondary_hold and wait at the spin
  349. * loop.
  350. */
  351. if (firmware_has_feature(FW_FEATURE_OPAL))
  352. return false;
  353. return true;
  354. }
  355. /*
  356. * When book3e boots from kexec, the ePAPR spin table does
  357. * not get used.
  358. */
  359. return of_property_read_bool(of_chosen, "linux,booted-from-kexec");
  360. }
  361. void smp_release_cpus(void)
  362. {
  363. unsigned long *ptr;
  364. int i;
  365. if (!use_spinloop())
  366. return;
  367. DBG(" -> smp_release_cpus()\n");
  368. /* All secondary cpus are spinning on a common spinloop, release them
  369. * all now so they can start to spin on their individual paca
  370. * spinloops. For non SMP kernels, the secondary cpus never get out
  371. * of the common spinloop.
  372. */
  373. ptr = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
  374. - PHYSICAL_START);
  375. *ptr = ppc_function_entry(generic_secondary_smp_init);
  376. /* And wait a bit for them to catch up */
  377. for (i = 0; i < 100000; i++) {
  378. mb();
  379. HMT_low();
  380. if (spinning_secondaries == 0)
  381. break;
  382. udelay(1);
  383. }
  384. DBG("spinning_secondaries = %d\n", spinning_secondaries);
  385. DBG(" <- smp_release_cpus()\n");
  386. }
  387. #endif /* CONFIG_SMP || CONFIG_KEXEC_CORE */
  388. /*
  389. * Initialize some remaining members of the ppc64_caches and systemcfg
  390. * structures
  391. * (at least until we get rid of them completely). This is mostly some
  392. * cache informations about the CPU that will be used by cache flush
  393. * routines and/or provided to userland
  394. */
  395. static void init_cache_info(struct ppc_cache_info *info, u32 size, u32 lsize,
  396. u32 bsize, u32 sets)
  397. {
  398. info->size = size;
  399. info->sets = sets;
  400. info->line_size = lsize;
  401. info->block_size = bsize;
  402. info->log_block_size = __ilog2(bsize);
  403. if (bsize)
  404. info->blocks_per_page = PAGE_SIZE / bsize;
  405. else
  406. info->blocks_per_page = 0;
  407. if (sets == 0)
  408. info->assoc = 0xffff;
  409. else
  410. info->assoc = size / (sets * lsize);
  411. }
  412. static bool __init parse_cache_info(struct device_node *np,
  413. bool icache,
  414. struct ppc_cache_info *info)
  415. {
  416. static const char *ipropnames[] __initdata = {
  417. "i-cache-size",
  418. "i-cache-sets",
  419. "i-cache-block-size",
  420. "i-cache-line-size",
  421. };
  422. static const char *dpropnames[] __initdata = {
  423. "d-cache-size",
  424. "d-cache-sets",
  425. "d-cache-block-size",
  426. "d-cache-line-size",
  427. };
  428. const char **propnames = icache ? ipropnames : dpropnames;
  429. const __be32 *sizep, *lsizep, *bsizep, *setsp;
  430. u32 size, lsize, bsize, sets;
  431. bool success = true;
  432. size = 0;
  433. sets = -1u;
  434. lsize = bsize = cur_cpu_spec->dcache_bsize;
  435. sizep = of_get_property(np, propnames[0], NULL);
  436. if (sizep != NULL)
  437. size = be32_to_cpu(*sizep);
  438. setsp = of_get_property(np, propnames[1], NULL);
  439. if (setsp != NULL)
  440. sets = be32_to_cpu(*setsp);
  441. bsizep = of_get_property(np, propnames[2], NULL);
  442. lsizep = of_get_property(np, propnames[3], NULL);
  443. if (bsizep == NULL)
  444. bsizep = lsizep;
  445. if (lsizep != NULL)
  446. lsize = be32_to_cpu(*lsizep);
  447. if (bsizep != NULL)
  448. bsize = be32_to_cpu(*bsizep);
  449. if (sizep == NULL || bsizep == NULL || lsizep == NULL)
  450. success = false;
  451. /*
  452. * OF is weird .. it represents fully associative caches
  453. * as "1 way" which doesn't make much sense and doesn't
  454. * leave room for direct mapped. We'll assume that 0
  455. * in OF means direct mapped for that reason.
  456. */
  457. if (sets == 1)
  458. sets = 0;
  459. else if (sets == 0)
  460. sets = 1;
  461. init_cache_info(info, size, lsize, bsize, sets);
  462. return success;
  463. }
  464. void __init initialize_cache_info(void)
  465. {
  466. struct device_node *cpu = NULL, *l2, *l3 = NULL;
  467. u32 pvr;
  468. DBG(" -> initialize_cache_info()\n");
  469. /*
  470. * All shipping POWER8 machines have a firmware bug that
  471. * puts incorrect information in the device-tree. This will
  472. * be (hopefully) fixed for future chips but for now hard
  473. * code the values if we are running on one of these
  474. */
  475. pvr = PVR_VER(mfspr(SPRN_PVR));
  476. if (pvr == PVR_POWER8 || pvr == PVR_POWER8E ||
  477. pvr == PVR_POWER8NVL) {
  478. /* size lsize blk sets */
  479. init_cache_info(&ppc64_caches.l1i, 0x8000, 128, 128, 32);
  480. init_cache_info(&ppc64_caches.l1d, 0x10000, 128, 128, 64);
  481. init_cache_info(&ppc64_caches.l2, 0x80000, 128, 0, 512);
  482. init_cache_info(&ppc64_caches.l3, 0x800000, 128, 0, 8192);
  483. } else
  484. cpu = of_find_node_by_type(NULL, "cpu");
  485. /*
  486. * We're assuming *all* of the CPUs have the same
  487. * d-cache and i-cache sizes... -Peter
  488. */
  489. if (cpu) {
  490. if (!parse_cache_info(cpu, false, &ppc64_caches.l1d))
  491. DBG("Argh, can't find dcache properties !\n");
  492. if (!parse_cache_info(cpu, true, &ppc64_caches.l1i))
  493. DBG("Argh, can't find icache properties !\n");
  494. /*
  495. * Try to find the L2 and L3 if any. Assume they are
  496. * unified and use the D-side properties.
  497. */
  498. l2 = of_find_next_cache_node(cpu);
  499. of_node_put(cpu);
  500. if (l2) {
  501. parse_cache_info(l2, false, &ppc64_caches.l2);
  502. l3 = of_find_next_cache_node(l2);
  503. of_node_put(l2);
  504. }
  505. if (l3) {
  506. parse_cache_info(l3, false, &ppc64_caches.l3);
  507. of_node_put(l3);
  508. }
  509. }
  510. /* For use by binfmt_elf */
  511. dcache_bsize = ppc64_caches.l1d.block_size;
  512. icache_bsize = ppc64_caches.l1i.block_size;
  513. cur_cpu_spec->dcache_bsize = dcache_bsize;
  514. cur_cpu_spec->icache_bsize = icache_bsize;
  515. DBG(" <- initialize_cache_info()\n");
  516. }
  517. /*
  518. * This returns the limit below which memory accesses to the linear
  519. * mapping are guarnateed not to cause an architectural exception (e.g.,
  520. * TLB or SLB miss fault).
  521. *
  522. * This is used to allocate PACAs and various interrupt stacks that
  523. * that are accessed early in interrupt handlers that must not cause
  524. * re-entrant interrupts.
  525. */
  526. __init u64 ppc64_bolted_size(void)
  527. {
  528. #ifdef CONFIG_PPC_BOOK3E
  529. /* Freescale BookE bolts the entire linear mapping */
  530. /* XXX: BookE ppc64_rma_limit setup seems to disagree? */
  531. if (early_mmu_has_feature(MMU_FTR_TYPE_FSL_E))
  532. return linear_map_top;
  533. /* Other BookE, we assume the first GB is bolted */
  534. return 1ul << 30;
  535. #else
  536. /* BookS radix, does not take faults on linear mapping */
  537. if (early_radix_enabled())
  538. return ULONG_MAX;
  539. /* BookS hash, the first segment is bolted */
  540. if (early_mmu_has_feature(MMU_FTR_1T_SEGMENT))
  541. return 1UL << SID_SHIFT_1T;
  542. return 1UL << SID_SHIFT;
  543. #endif
  544. }
  545. static void *__init alloc_stack(unsigned long limit, int cpu)
  546. {
  547. unsigned long pa;
  548. pa = memblock_alloc_base_nid(THREAD_SIZE, THREAD_SIZE, limit,
  549. early_cpu_to_node(cpu), MEMBLOCK_NONE);
  550. if (!pa) {
  551. pa = memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit);
  552. if (!pa)
  553. panic("cannot allocate stacks");
  554. }
  555. return __va(pa);
  556. }
  557. void __init irqstack_early_init(void)
  558. {
  559. u64 limit = ppc64_bolted_size();
  560. unsigned int i;
  561. /*
  562. * Interrupt stacks must be in the first segment since we
  563. * cannot afford to take SLB misses on them. They are not
  564. * accessed in realmode.
  565. */
  566. for_each_possible_cpu(i) {
  567. softirq_ctx[i] = alloc_stack(limit, i);
  568. hardirq_ctx[i] = alloc_stack(limit, i);
  569. }
  570. }
  571. #ifdef CONFIG_PPC_BOOK3E
  572. void __init exc_lvl_early_init(void)
  573. {
  574. unsigned int i;
  575. for_each_possible_cpu(i) {
  576. void *sp;
  577. sp = alloc_stack(ULONG_MAX, i);
  578. critirq_ctx[i] = sp;
  579. paca_ptrs[i]->crit_kstack = sp + THREAD_SIZE;
  580. sp = alloc_stack(ULONG_MAX, i);
  581. dbgirq_ctx[i] = sp;
  582. paca_ptrs[i]->dbg_kstack = sp + THREAD_SIZE;
  583. sp = alloc_stack(ULONG_MAX, i);
  584. mcheckirq_ctx[i] = sp;
  585. paca_ptrs[i]->mc_kstack = sp + THREAD_SIZE;
  586. }
  587. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
  588. patch_exception(0x040, exc_debug_debug_book3e);
  589. }
  590. #endif
  591. /*
  592. * Emergency stacks are used for a range of things, from asynchronous
  593. * NMIs (system reset, machine check) to synchronous, process context.
  594. * We set preempt_count to zero, even though that isn't necessarily correct. To
  595. * get the right value we'd need to copy it from the previous thread_info, but
  596. * doing that might fault causing more problems.
  597. * TODO: what to do with accounting?
  598. */
  599. static void emerg_stack_init_thread_info(struct thread_info *ti, int cpu)
  600. {
  601. ti->task = NULL;
  602. ti->cpu = cpu;
  603. ti->preempt_count = 0;
  604. ti->local_flags = 0;
  605. ti->flags = 0;
  606. klp_init_thread_info(ti);
  607. }
  608. /*
  609. * Stack space used when we detect a bad kernel stack pointer, and
  610. * early in SMP boots before relocation is enabled. Exclusive emergency
  611. * stack for machine checks.
  612. */
  613. void __init emergency_stack_init(void)
  614. {
  615. u64 limit;
  616. unsigned int i;
  617. /*
  618. * Emergency stacks must be under 256MB, we cannot afford to take
  619. * SLB misses on them. The ABI also requires them to be 128-byte
  620. * aligned.
  621. *
  622. * Since we use these as temporary stacks during secondary CPU
  623. * bringup, machine check, system reset, and HMI, we need to get
  624. * at them in real mode. This means they must also be within the RMO
  625. * region.
  626. *
  627. * The IRQ stacks allocated elsewhere in this file are zeroed and
  628. * initialized in kernel/irq.c. These are initialized here in order
  629. * to have emergency stacks available as early as possible.
  630. */
  631. limit = min(ppc64_bolted_size(), ppc64_rma_size);
  632. for_each_possible_cpu(i) {
  633. struct thread_info *ti;
  634. ti = alloc_stack(limit, i);
  635. memset(ti, 0, THREAD_SIZE);
  636. emerg_stack_init_thread_info(ti, i);
  637. paca_ptrs[i]->emergency_sp = (void *)ti + THREAD_SIZE;
  638. #ifdef CONFIG_PPC_BOOK3S_64
  639. /* emergency stack for NMI exception handling. */
  640. ti = alloc_stack(limit, i);
  641. memset(ti, 0, THREAD_SIZE);
  642. emerg_stack_init_thread_info(ti, i);
  643. paca_ptrs[i]->nmi_emergency_sp = (void *)ti + THREAD_SIZE;
  644. /* emergency stack for machine check exception handling. */
  645. ti = alloc_stack(limit, i);
  646. memset(ti, 0, THREAD_SIZE);
  647. emerg_stack_init_thread_info(ti, i);
  648. paca_ptrs[i]->mc_emergency_sp = (void *)ti + THREAD_SIZE;
  649. #endif
  650. }
  651. }
  652. #ifdef CONFIG_SMP
  653. #define PCPU_DYN_SIZE ()
  654. static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
  655. {
  656. return __alloc_bootmem_node(NODE_DATA(early_cpu_to_node(cpu)), size, align,
  657. __pa(MAX_DMA_ADDRESS));
  658. }
  659. static void __init pcpu_fc_free(void *ptr, size_t size)
  660. {
  661. free_bootmem(__pa(ptr), size);
  662. }
  663. static int pcpu_cpu_distance(unsigned int from, unsigned int to)
  664. {
  665. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  666. return LOCAL_DISTANCE;
  667. else
  668. return REMOTE_DISTANCE;
  669. }
  670. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  671. EXPORT_SYMBOL(__per_cpu_offset);
  672. void __init setup_per_cpu_areas(void)
  673. {
  674. const size_t dyn_size = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  675. size_t atom_size;
  676. unsigned long delta;
  677. unsigned int cpu;
  678. int rc;
  679. /*
  680. * Linear mapping is one of 4K, 1M and 16M. For 4K, no need
  681. * to group units. For larger mappings, use 1M atom which
  682. * should be large enough to contain a number of units.
  683. */
  684. if (mmu_linear_psize == MMU_PAGE_4K)
  685. atom_size = PAGE_SIZE;
  686. else
  687. atom_size = 1 << 20;
  688. rc = pcpu_embed_first_chunk(0, dyn_size, atom_size, pcpu_cpu_distance,
  689. pcpu_fc_alloc, pcpu_fc_free);
  690. if (rc < 0)
  691. panic("cannot initialize percpu area (err=%d)", rc);
  692. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  693. for_each_possible_cpu(cpu) {
  694. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  695. paca_ptrs[cpu]->data_offset = __per_cpu_offset[cpu];
  696. }
  697. }
  698. #endif
  699. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  700. unsigned long memory_block_size_bytes(void)
  701. {
  702. if (ppc_md.memory_block_size)
  703. return ppc_md.memory_block_size();
  704. return MIN_MEMORY_BLOCK_SIZE;
  705. }
  706. #endif
  707. #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
  708. struct ppc_pci_io ppc_pci_io;
  709. EXPORT_SYMBOL(ppc_pci_io);
  710. #endif
  711. #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
  712. u64 hw_nmi_get_sample_period(int watchdog_thresh)
  713. {
  714. return ppc_proc_freq * watchdog_thresh;
  715. }
  716. #endif
  717. /*
  718. * The perf based hardlockup detector breaks PMU event based branches, so
  719. * disable it by default. Book3S has a soft-nmi hardlockup detector based
  720. * on the decrementer interrupt, so it does not suffer from this problem.
  721. *
  722. * It is likely to get false positives in VM guests, so disable it there
  723. * by default too.
  724. */
  725. static int __init disable_hardlockup_detector(void)
  726. {
  727. #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
  728. hardlockup_detector_disable();
  729. #else
  730. if (firmware_has_feature(FW_FEATURE_LPAR))
  731. hardlockup_detector_disable();
  732. #endif
  733. return 0;
  734. }
  735. early_initcall(disable_hardlockup_detector);
  736. #ifdef CONFIG_PPC_BOOK3S_64
  737. static enum l1d_flush_type enabled_flush_types;
  738. static void *l1d_flush_fallback_area;
  739. static bool no_rfi_flush;
  740. bool rfi_flush;
  741. static int __init handle_no_rfi_flush(char *p)
  742. {
  743. pr_info("rfi-flush: disabled on command line.");
  744. no_rfi_flush = true;
  745. return 0;
  746. }
  747. early_param("no_rfi_flush", handle_no_rfi_flush);
  748. /*
  749. * The RFI flush is not KPTI, but because users will see doco that says to use
  750. * nopti we hijack that option here to also disable the RFI flush.
  751. */
  752. static int __init handle_no_pti(char *p)
  753. {
  754. pr_info("rfi-flush: disabling due to 'nopti' on command line.\n");
  755. handle_no_rfi_flush(NULL);
  756. return 0;
  757. }
  758. early_param("nopti", handle_no_pti);
  759. static void do_nothing(void *unused)
  760. {
  761. /*
  762. * We don't need to do the flush explicitly, just enter+exit kernel is
  763. * sufficient, the RFI exit handlers will do the right thing.
  764. */
  765. }
  766. void rfi_flush_enable(bool enable)
  767. {
  768. if (enable) {
  769. do_rfi_flush_fixups(enabled_flush_types);
  770. on_each_cpu(do_nothing, NULL, 1);
  771. } else
  772. do_rfi_flush_fixups(L1D_FLUSH_NONE);
  773. rfi_flush = enable;
  774. }
  775. static void __ref init_fallback_flush(void)
  776. {
  777. u64 l1d_size, limit;
  778. int cpu;
  779. /* Only allocate the fallback flush area once (at boot time). */
  780. if (l1d_flush_fallback_area)
  781. return;
  782. l1d_size = ppc64_caches.l1d.size;
  783. /*
  784. * If there is no d-cache-size property in the device tree, l1d_size
  785. * could be zero. That leads to the loop in the asm wrapping around to
  786. * 2^64-1, and then walking off the end of the fallback area and
  787. * eventually causing a page fault which is fatal. Just default to
  788. * something vaguely sane.
  789. */
  790. if (!l1d_size)
  791. l1d_size = (64 * 1024);
  792. limit = min(ppc64_bolted_size(), ppc64_rma_size);
  793. /*
  794. * Align to L1d size, and size it at 2x L1d size, to catch possible
  795. * hardware prefetch runoff. We don't have a recipe for load patterns to
  796. * reliably avoid the prefetcher.
  797. */
  798. l1d_flush_fallback_area = __va(memblock_alloc_base(l1d_size * 2, l1d_size, limit));
  799. memset(l1d_flush_fallback_area, 0, l1d_size * 2);
  800. for_each_possible_cpu(cpu) {
  801. struct paca_struct *paca = paca_ptrs[cpu];
  802. paca->rfi_flush_fallback_area = l1d_flush_fallback_area;
  803. paca->l1d_flush_size = l1d_size;
  804. }
  805. }
  806. void setup_rfi_flush(enum l1d_flush_type types, bool enable)
  807. {
  808. if (types & L1D_FLUSH_FALLBACK) {
  809. pr_info("rfi-flush: fallback displacement flush available\n");
  810. init_fallback_flush();
  811. }
  812. if (types & L1D_FLUSH_ORI)
  813. pr_info("rfi-flush: ori type flush available\n");
  814. if (types & L1D_FLUSH_MTTRIG)
  815. pr_info("rfi-flush: mttrig type flush available\n");
  816. enabled_flush_types = types;
  817. if (!no_rfi_flush)
  818. rfi_flush_enable(enable);
  819. }
  820. #ifdef CONFIG_DEBUG_FS
  821. static int rfi_flush_set(void *data, u64 val)
  822. {
  823. bool enable;
  824. if (val == 1)
  825. enable = true;
  826. else if (val == 0)
  827. enable = false;
  828. else
  829. return -EINVAL;
  830. /* Only do anything if we're changing state */
  831. if (enable != rfi_flush)
  832. rfi_flush_enable(enable);
  833. return 0;
  834. }
  835. static int rfi_flush_get(void *data, u64 *val)
  836. {
  837. *val = rfi_flush ? 1 : 0;
  838. return 0;
  839. }
  840. DEFINE_SIMPLE_ATTRIBUTE(fops_rfi_flush, rfi_flush_get, rfi_flush_set, "%llu\n");
  841. static __init int rfi_flush_debugfs_init(void)
  842. {
  843. debugfs_create_file("rfi_flush", 0600, powerpc_debugfs_root, NULL, &fops_rfi_flush);
  844. return 0;
  845. }
  846. device_initcall(rfi_flush_debugfs_init);
  847. #endif
  848. #endif /* CONFIG_PPC_BOOK3S_64 */