setup_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. #define DEBUG
  13. #include <linux/export.h>
  14. #include <linux/string.h>
  15. #include <linux/sched.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/reboot.h>
  19. #include <linux/delay.h>
  20. #include <linux/initrd.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/ioport.h>
  23. #include <linux/console.h>
  24. #include <linux/utsname.h>
  25. #include <linux/tty.h>
  26. #include <linux/root_dev.h>
  27. #include <linux/notifier.h>
  28. #include <linux/cpu.h>
  29. #include <linux/unistd.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_8250.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/pci.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/memblock.h>
  36. #include <linux/memory.h>
  37. #include <linux/nmi.h>
  38. #include <linux/debugfs.h>
  39. #include <asm/io.h>
  40. #include <asm/kdump.h>
  41. #include <asm/prom.h>
  42. #include <asm/processor.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/smp.h>
  45. #include <asm/elf.h>
  46. #include <asm/machdep.h>
  47. #include <asm/paca.h>
  48. #include <asm/time.h>
  49. #include <asm/cputable.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. #ifdef DEBUG
  69. #define DBG(fmt...) udbg_printf(fmt)
  70. #else
  71. #define DBG(fmt...)
  72. #endif
  73. int spinning_secondaries;
  74. u64 ppc64_pft_size;
  75. /* Pick defaults since we might want to patch instructions
  76. * before we've read this from the device tree.
  77. */
  78. struct ppc64_caches ppc64_caches = {
  79. .dline_size = 0x40,
  80. .log_dline_size = 6,
  81. .iline_size = 0x40,
  82. .log_iline_size = 6
  83. };
  84. EXPORT_SYMBOL_GPL(ppc64_caches);
  85. /*
  86. * These are used in binfmt_elf.c to put aux entries on the stack
  87. * for each elf executable being started.
  88. */
  89. int dcache_bsize;
  90. int icache_bsize;
  91. int ucache_bsize;
  92. #if defined(CONFIG_PPC_BOOK3E) && defined(CONFIG_SMP)
  93. void __init setup_tlb_core_data(void)
  94. {
  95. int cpu;
  96. BUILD_BUG_ON(offsetof(struct tlb_core_data, lock) != 0);
  97. for_each_possible_cpu(cpu) {
  98. int first = cpu_first_thread_sibling(cpu);
  99. /*
  100. * If we boot via kdump on a non-primary thread,
  101. * make sure we point at the thread that actually
  102. * set up this TLB.
  103. */
  104. if (cpu_first_thread_sibling(boot_cpuid) == first)
  105. first = boot_cpuid;
  106. paca[cpu].tcd_ptr = &paca[first].tcd;
  107. /*
  108. * If we have threads, we need either tlbsrx.
  109. * or e6500 tablewalk mode, or else TLB handlers
  110. * will be racy and could produce duplicate entries.
  111. */
  112. if (smt_enabled_at_boot >= 2 &&
  113. !mmu_has_feature(MMU_FTR_USE_TLBRSRV) &&
  114. book3e_htw_mode != PPC_HTW_E6500) {
  115. /* Should we panic instead? */
  116. WARN_ONCE("%s: unsupported MMU configuration -- expect problems\n",
  117. __func__);
  118. }
  119. }
  120. }
  121. #endif
  122. #ifdef CONFIG_SMP
  123. static char *smt_enabled_cmdline;
  124. /* Look for ibm,smt-enabled OF option */
  125. void __init check_smt_enabled(void)
  126. {
  127. struct device_node *dn;
  128. const char *smt_option;
  129. /* Default to enabling all threads */
  130. smt_enabled_at_boot = threads_per_core;
  131. /* Allow the command line to overrule the OF option */
  132. if (smt_enabled_cmdline) {
  133. if (!strcmp(smt_enabled_cmdline, "on"))
  134. smt_enabled_at_boot = threads_per_core;
  135. else if (!strcmp(smt_enabled_cmdline, "off"))
  136. smt_enabled_at_boot = 0;
  137. else {
  138. int smt;
  139. int rc;
  140. rc = kstrtoint(smt_enabled_cmdline, 10, &smt);
  141. if (!rc)
  142. smt_enabled_at_boot =
  143. min(threads_per_core, smt);
  144. }
  145. } else {
  146. dn = of_find_node_by_path("/options");
  147. if (dn) {
  148. smt_option = of_get_property(dn, "ibm,smt-enabled",
  149. NULL);
  150. if (smt_option) {
  151. if (!strcmp(smt_option, "on"))
  152. smt_enabled_at_boot = threads_per_core;
  153. else if (!strcmp(smt_option, "off"))
  154. smt_enabled_at_boot = 0;
  155. }
  156. of_node_put(dn);
  157. }
  158. }
  159. }
  160. /* Look for smt-enabled= cmdline option */
  161. static int __init early_smt_enabled(char *p)
  162. {
  163. smt_enabled_cmdline = p;
  164. return 0;
  165. }
  166. early_param("smt-enabled", early_smt_enabled);
  167. #endif /* CONFIG_SMP */
  168. /** Fix up paca fields required for the boot cpu */
  169. static void __init fixup_boot_paca(void)
  170. {
  171. /* The boot cpu is started */
  172. get_paca()->cpu_start = 1;
  173. /* Allow percpu accesses to work until we setup percpu data */
  174. get_paca()->data_offset = 0;
  175. }
  176. static void __init configure_exceptions(void)
  177. {
  178. /*
  179. * Setup the trampolines from the lowmem exception vectors
  180. * to the kdump kernel when not using a relocatable kernel.
  181. */
  182. setup_kdump_trampoline();
  183. /* Under a PAPR hypervisor, we need hypercalls */
  184. if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
  185. /* Enable AIL if possible */
  186. pseries_enable_reloc_on_exc();
  187. /*
  188. * Tell the hypervisor that we want our exceptions to
  189. * be taken in little endian mode.
  190. *
  191. * We don't call this for big endian as our calling convention
  192. * makes us always enter in BE, and the call may fail under
  193. * some circumstances with kdump.
  194. */
  195. #ifdef __LITTLE_ENDIAN__
  196. pseries_little_endian_exceptions();
  197. #endif
  198. } else {
  199. /* Set endian mode using OPAL */
  200. if (firmware_has_feature(FW_FEATURE_OPAL))
  201. opal_configure_cores();
  202. /* AIL on native is done in cpu_ready_for_interrupts() */
  203. }
  204. }
  205. static void cpu_ready_for_interrupts(void)
  206. {
  207. /*
  208. * Enable AIL if supported, and we are in hypervisor mode. This
  209. * is called once for every processor.
  210. *
  211. * If we are not in hypervisor mode the job is done once for
  212. * the whole partition in configure_exceptions().
  213. */
  214. if (early_cpu_has_feature(CPU_FTR_HVMODE) &&
  215. early_cpu_has_feature(CPU_FTR_ARCH_207S)) {
  216. unsigned long lpcr = mfspr(SPRN_LPCR);
  217. mtspr(SPRN_LPCR, lpcr | LPCR_AIL_3);
  218. }
  219. /*
  220. * Fixup HFSCR:TM based on CPU features. The bit is set by our
  221. * early asm init because at that point we haven't updated our
  222. * CPU features from firmware and device-tree. Here we have,
  223. * so let's do it.
  224. */
  225. if (cpu_has_feature(CPU_FTR_HVMODE) && !cpu_has_feature(CPU_FTR_TM_COMP))
  226. mtspr(SPRN_HFSCR, mfspr(SPRN_HFSCR) & ~HFSCR_TM);
  227. /* Set IR and DR in PACA MSR */
  228. get_paca()->kernel_msr = MSR_KERNEL;
  229. }
  230. /*
  231. * Early initialization entry point. This is called by head.S
  232. * with MMU translation disabled. We rely on the "feature" of
  233. * the CPU that ignores the top 2 bits of the address in real
  234. * mode so we can access kernel globals normally provided we
  235. * only toy with things in the RMO region. From here, we do
  236. * some early parsing of the device-tree to setup out MEMBLOCK
  237. * data structures, and allocate & initialize the hash table
  238. * and segment tables so we can start running with translation
  239. * enabled.
  240. *
  241. * It is this function which will call the probe() callback of
  242. * the various platform types and copy the matching one to the
  243. * global ppc_md structure. Your platform can eventually do
  244. * some very early initializations from the probe() routine, but
  245. * this is not recommended, be very careful as, for example, the
  246. * device-tree is not accessible via normal means at this point.
  247. */
  248. void __init early_setup(unsigned long dt_ptr)
  249. {
  250. static __initdata struct paca_struct boot_paca;
  251. /* -------- printk is _NOT_ safe to use here ! ------- */
  252. /* Identify CPU type */
  253. identify_cpu(0, mfspr(SPRN_PVR));
  254. /* Assume we're on cpu 0 for now. Don't write to the paca yet! */
  255. initialise_paca(&boot_paca, 0);
  256. setup_paca(&boot_paca);
  257. fixup_boot_paca();
  258. /* -------- printk is now safe to use ------- */
  259. /* Enable early debugging if any specified (see udbg.h) */
  260. udbg_early_init();
  261. DBG(" -> early_setup(), dt_ptr: 0x%lx\n", dt_ptr);
  262. /*
  263. * Do early initialization using the flattened device
  264. * tree, such as retrieving the physical memory map or
  265. * calculating/retrieving the hash table size.
  266. */
  267. early_init_devtree(__va(dt_ptr));
  268. /* Now we know the logical id of our boot cpu, setup the paca. */
  269. setup_paca(&paca[boot_cpuid]);
  270. fixup_boot_paca();
  271. /*
  272. * Configure exception handlers. This include setting up trampolines
  273. * if needed, setting exception endian mode, etc...
  274. */
  275. configure_exceptions();
  276. /* Apply all the dynamic patching */
  277. apply_feature_fixups();
  278. setup_feature_keys();
  279. /* Initialize the hash table or TLB handling */
  280. early_init_mmu();
  281. /*
  282. * At this point, we can let interrupts switch to virtual mode
  283. * (the MMU has been setup), so adjust the MSR in the PACA to
  284. * have IR and DR set and enable AIL if it exists
  285. */
  286. cpu_ready_for_interrupts();
  287. DBG(" <- early_setup()\n");
  288. #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
  289. /*
  290. * This needs to be done *last* (after the above DBG() even)
  291. *
  292. * Right after we return from this function, we turn on the MMU
  293. * which means the real-mode access trick that btext does will
  294. * no longer work, it needs to switch to using a real MMU
  295. * mapping. This call will ensure that it does
  296. */
  297. btext_map();
  298. #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
  299. }
  300. #ifdef CONFIG_SMP
  301. void early_setup_secondary(void)
  302. {
  303. /* Mark interrupts disabled in PACA */
  304. get_paca()->soft_enabled = 0;
  305. /* Initialize the hash table or TLB handling */
  306. early_init_mmu_secondary();
  307. /*
  308. * At this point, we can let interrupts switch to virtual mode
  309. * (the MMU has been setup), so adjust the MSR in the PACA to
  310. * have IR and DR set.
  311. */
  312. cpu_ready_for_interrupts();
  313. }
  314. #endif /* CONFIG_SMP */
  315. #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
  316. static bool use_spinloop(void)
  317. {
  318. if (!IS_ENABLED(CONFIG_PPC_BOOK3E))
  319. return true;
  320. /*
  321. * When book3e boots from kexec, the ePAPR spin table does
  322. * not get used.
  323. */
  324. return of_property_read_bool(of_chosen, "linux,booted-from-kexec");
  325. }
  326. void smp_release_cpus(void)
  327. {
  328. unsigned long *ptr;
  329. int i;
  330. if (!use_spinloop())
  331. return;
  332. DBG(" -> smp_release_cpus()\n");
  333. /* All secondary cpus are spinning on a common spinloop, release them
  334. * all now so they can start to spin on their individual paca
  335. * spinloops. For non SMP kernels, the secondary cpus never get out
  336. * of the common spinloop.
  337. */
  338. ptr = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
  339. - PHYSICAL_START);
  340. *ptr = ppc_function_entry(generic_secondary_smp_init);
  341. /* And wait a bit for them to catch up */
  342. for (i = 0; i < 100000; i++) {
  343. mb();
  344. HMT_low();
  345. if (spinning_secondaries == 0)
  346. break;
  347. udelay(1);
  348. }
  349. DBG("spinning_secondaries = %d\n", spinning_secondaries);
  350. DBG(" <- smp_release_cpus()\n");
  351. }
  352. #endif /* CONFIG_SMP || CONFIG_KEXEC */
  353. /*
  354. * Initialize some remaining members of the ppc64_caches and systemcfg
  355. * structures
  356. * (at least until we get rid of them completely). This is mostly some
  357. * cache informations about the CPU that will be used by cache flush
  358. * routines and/or provided to userland
  359. */
  360. void __init initialize_cache_info(void)
  361. {
  362. struct device_node *np;
  363. unsigned long num_cpus = 0;
  364. DBG(" -> initialize_cache_info()\n");
  365. for_each_node_by_type(np, "cpu") {
  366. num_cpus += 1;
  367. /*
  368. * We're assuming *all* of the CPUs have the same
  369. * d-cache and i-cache sizes... -Peter
  370. */
  371. if (num_cpus == 1) {
  372. const __be32 *sizep, *lsizep;
  373. u32 size, lsize;
  374. size = 0;
  375. lsize = cur_cpu_spec->dcache_bsize;
  376. sizep = of_get_property(np, "d-cache-size", NULL);
  377. if (sizep != NULL)
  378. size = be32_to_cpu(*sizep);
  379. lsizep = of_get_property(np, "d-cache-block-size",
  380. NULL);
  381. /* fallback if block size missing */
  382. if (lsizep == NULL)
  383. lsizep = of_get_property(np,
  384. "d-cache-line-size",
  385. NULL);
  386. if (lsizep != NULL)
  387. lsize = be32_to_cpu(*lsizep);
  388. if (sizep == NULL || lsizep == NULL)
  389. DBG("Argh, can't find dcache properties ! "
  390. "sizep: %p, lsizep: %p\n", sizep, lsizep);
  391. ppc64_caches.dsize = size;
  392. ppc64_caches.dline_size = lsize;
  393. ppc64_caches.log_dline_size = __ilog2(lsize);
  394. ppc64_caches.dlines_per_page = PAGE_SIZE / lsize;
  395. size = 0;
  396. lsize = cur_cpu_spec->icache_bsize;
  397. sizep = of_get_property(np, "i-cache-size", NULL);
  398. if (sizep != NULL)
  399. size = be32_to_cpu(*sizep);
  400. lsizep = of_get_property(np, "i-cache-block-size",
  401. NULL);
  402. if (lsizep == NULL)
  403. lsizep = of_get_property(np,
  404. "i-cache-line-size",
  405. NULL);
  406. if (lsizep != NULL)
  407. lsize = be32_to_cpu(*lsizep);
  408. if (sizep == NULL || lsizep == NULL)
  409. DBG("Argh, can't find icache properties ! "
  410. "sizep: %p, lsizep: %p\n", sizep, lsizep);
  411. ppc64_caches.isize = size;
  412. ppc64_caches.iline_size = lsize;
  413. ppc64_caches.log_iline_size = __ilog2(lsize);
  414. ppc64_caches.ilines_per_page = PAGE_SIZE / lsize;
  415. }
  416. }
  417. /* For use by binfmt_elf */
  418. dcache_bsize = ppc64_caches.dline_size;
  419. icache_bsize = ppc64_caches.iline_size;
  420. DBG(" <- initialize_cache_info()\n");
  421. }
  422. /* This returns the limit below which memory accesses to the linear
  423. * mapping are guarnateed not to cause a TLB or SLB miss. This is
  424. * used to allocate interrupt or emergency stacks for which our
  425. * exception entry path doesn't deal with being interrupted.
  426. */
  427. static __init u64 safe_stack_limit(void)
  428. {
  429. #ifdef CONFIG_PPC_BOOK3E
  430. /* Freescale BookE bolts the entire linear mapping */
  431. if (mmu_has_feature(MMU_FTR_TYPE_FSL_E))
  432. return linear_map_top;
  433. /* Other BookE, we assume the first GB is bolted */
  434. return 1ul << 30;
  435. #else
  436. /* BookS, the first segment is bolted */
  437. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  438. return 1UL << SID_SHIFT_1T;
  439. return 1UL << SID_SHIFT;
  440. #endif
  441. }
  442. void __init irqstack_early_init(void)
  443. {
  444. u64 limit = safe_stack_limit();
  445. unsigned int i;
  446. /*
  447. * Interrupt stacks must be in the first segment since we
  448. * cannot afford to take SLB misses on them.
  449. */
  450. for_each_possible_cpu(i) {
  451. softirq_ctx[i] = (struct thread_info *)
  452. __va(memblock_alloc_base(THREAD_SIZE,
  453. THREAD_SIZE, limit));
  454. hardirq_ctx[i] = (struct thread_info *)
  455. __va(memblock_alloc_base(THREAD_SIZE,
  456. THREAD_SIZE, limit));
  457. }
  458. }
  459. #ifdef CONFIG_PPC_BOOK3E
  460. void __init exc_lvl_early_init(void)
  461. {
  462. unsigned int i;
  463. unsigned long sp;
  464. for_each_possible_cpu(i) {
  465. sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  466. critirq_ctx[i] = (struct thread_info *)__va(sp);
  467. paca[i].crit_kstack = __va(sp + THREAD_SIZE);
  468. sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  469. dbgirq_ctx[i] = (struct thread_info *)__va(sp);
  470. paca[i].dbg_kstack = __va(sp + THREAD_SIZE);
  471. sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  472. mcheckirq_ctx[i] = (struct thread_info *)__va(sp);
  473. paca[i].mc_kstack = __va(sp + THREAD_SIZE);
  474. }
  475. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
  476. patch_exception(0x040, exc_debug_debug_book3e);
  477. }
  478. #endif
  479. /*
  480. * Stack space used when we detect a bad kernel stack pointer, and
  481. * early in SMP boots before relocation is enabled. Exclusive emergency
  482. * stack for machine checks.
  483. */
  484. void __init emergency_stack_init(void)
  485. {
  486. u64 limit;
  487. unsigned int i;
  488. /*
  489. * Emergency stacks must be under 256MB, we cannot afford to take
  490. * SLB misses on them. The ABI also requires them to be 128-byte
  491. * aligned.
  492. *
  493. * Since we use these as temporary stacks during secondary CPU
  494. * bringup, we need to get at them in real mode. This means they
  495. * must also be within the RMO region.
  496. */
  497. limit = min(safe_stack_limit(), ppc64_rma_size);
  498. for_each_possible_cpu(i) {
  499. struct thread_info *ti;
  500. ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
  501. klp_init_thread_info(ti);
  502. paca[i].emergency_sp = (void *)ti + THREAD_SIZE;
  503. #ifdef CONFIG_PPC_BOOK3S_64
  504. /* emergency stack for machine check exception handling. */
  505. ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
  506. klp_init_thread_info(ti);
  507. paca[i].mc_emergency_sp = (void *)ti + THREAD_SIZE;
  508. #endif
  509. }
  510. }
  511. #ifdef CONFIG_SMP
  512. #define PCPU_DYN_SIZE ()
  513. static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
  514. {
  515. return __alloc_bootmem_node(NODE_DATA(cpu_to_node(cpu)), size, align,
  516. __pa(MAX_DMA_ADDRESS));
  517. }
  518. static void __init pcpu_fc_free(void *ptr, size_t size)
  519. {
  520. free_bootmem(__pa(ptr), size);
  521. }
  522. static int pcpu_cpu_distance(unsigned int from, unsigned int to)
  523. {
  524. if (cpu_to_node(from) == cpu_to_node(to))
  525. return LOCAL_DISTANCE;
  526. else
  527. return REMOTE_DISTANCE;
  528. }
  529. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  530. EXPORT_SYMBOL(__per_cpu_offset);
  531. void __init setup_per_cpu_areas(void)
  532. {
  533. const size_t dyn_size = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  534. size_t atom_size;
  535. unsigned long delta;
  536. unsigned int cpu;
  537. int rc;
  538. /*
  539. * Linear mapping is one of 4K, 1M and 16M. For 4K, no need
  540. * to group units. For larger mappings, use 1M atom which
  541. * should be large enough to contain a number of units.
  542. */
  543. if (mmu_linear_psize == MMU_PAGE_4K)
  544. atom_size = PAGE_SIZE;
  545. else
  546. atom_size = 1 << 20;
  547. rc = pcpu_embed_first_chunk(0, dyn_size, atom_size, pcpu_cpu_distance,
  548. pcpu_fc_alloc, pcpu_fc_free);
  549. if (rc < 0)
  550. panic("cannot initialize percpu area (err=%d)", rc);
  551. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  552. for_each_possible_cpu(cpu) {
  553. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  554. paca[cpu].data_offset = __per_cpu_offset[cpu];
  555. }
  556. }
  557. #endif
  558. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  559. unsigned long memory_block_size_bytes(void)
  560. {
  561. if (ppc_md.memory_block_size)
  562. return ppc_md.memory_block_size();
  563. return MIN_MEMORY_BLOCK_SIZE;
  564. }
  565. #endif
  566. #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
  567. struct ppc_pci_io ppc_pci_io;
  568. EXPORT_SYMBOL(ppc_pci_io);
  569. #endif
  570. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  571. u64 hw_nmi_get_sample_period(int watchdog_thresh)
  572. {
  573. return ppc_proc_freq * watchdog_thresh;
  574. }
  575. /*
  576. * The hardlockup detector breaks PMU event based branches and is likely
  577. * to get false positives in KVM guests, so disable it by default.
  578. */
  579. static int __init disable_hardlockup_detector(void)
  580. {
  581. hardlockup_detector_disable();
  582. return 0;
  583. }
  584. early_initcall(disable_hardlockup_detector);
  585. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  586. #ifdef CONFIG_PPC_BOOK3S_64
  587. static enum l1d_flush_type enabled_flush_types;
  588. static void *l1d_flush_fallback_area;
  589. static bool no_rfi_flush;
  590. bool rfi_flush;
  591. static int __init handle_no_rfi_flush(char *p)
  592. {
  593. pr_info("rfi-flush: disabled on command line.");
  594. no_rfi_flush = true;
  595. return 0;
  596. }
  597. early_param("no_rfi_flush", handle_no_rfi_flush);
  598. /*
  599. * The RFI flush is not KPTI, but because users will see doco that says to use
  600. * nopti we hijack that option here to also disable the RFI flush.
  601. */
  602. static int __init handle_no_pti(char *p)
  603. {
  604. pr_info("rfi-flush: disabling due to 'nopti' on command line.\n");
  605. handle_no_rfi_flush(NULL);
  606. return 0;
  607. }
  608. early_param("nopti", handle_no_pti);
  609. static void do_nothing(void *unused)
  610. {
  611. /*
  612. * We don't need to do the flush explicitly, just enter+exit kernel is
  613. * sufficient, the RFI exit handlers will do the right thing.
  614. */
  615. }
  616. void rfi_flush_enable(bool enable)
  617. {
  618. if (enable) {
  619. do_rfi_flush_fixups(enabled_flush_types);
  620. on_each_cpu(do_nothing, NULL, 1);
  621. } else
  622. do_rfi_flush_fixups(L1D_FLUSH_NONE);
  623. rfi_flush = enable;
  624. }
  625. static void __ref init_fallback_flush(void)
  626. {
  627. u64 l1d_size, limit;
  628. int cpu;
  629. /* Only allocate the fallback flush area once (at boot time). */
  630. if (l1d_flush_fallback_area)
  631. return;
  632. l1d_size = ppc64_caches.dsize;
  633. limit = min(safe_stack_limit(), ppc64_rma_size);
  634. /*
  635. * Align to L1d size, and size it at 2x L1d size, to catch possible
  636. * hardware prefetch runoff. We don't have a recipe for load patterns to
  637. * reliably avoid the prefetcher.
  638. */
  639. l1d_flush_fallback_area = __va(memblock_alloc_base(l1d_size * 2, l1d_size, limit));
  640. memset(l1d_flush_fallback_area, 0, l1d_size * 2);
  641. for_each_possible_cpu(cpu) {
  642. paca[cpu].rfi_flush_fallback_area = l1d_flush_fallback_area;
  643. paca[cpu].l1d_flush_size = l1d_size;
  644. }
  645. }
  646. void setup_rfi_flush(enum l1d_flush_type types, bool enable)
  647. {
  648. if (types & L1D_FLUSH_FALLBACK) {
  649. pr_info("rfi-flush: fallback displacement flush available\n");
  650. init_fallback_flush();
  651. }
  652. if (types & L1D_FLUSH_ORI)
  653. pr_info("rfi-flush: ori type flush available\n");
  654. if (types & L1D_FLUSH_MTTRIG)
  655. pr_info("rfi-flush: mttrig type flush available\n");
  656. enabled_flush_types = types;
  657. if (!no_rfi_flush)
  658. rfi_flush_enable(enable);
  659. }
  660. #ifdef CONFIG_DEBUG_FS
  661. static int rfi_flush_set(void *data, u64 val)
  662. {
  663. bool enable;
  664. if (val == 1)
  665. enable = true;
  666. else if (val == 0)
  667. enable = false;
  668. else
  669. return -EINVAL;
  670. /* Only do anything if we're changing state */
  671. if (enable != rfi_flush)
  672. rfi_flush_enable(enable);
  673. return 0;
  674. }
  675. static int rfi_flush_get(void *data, u64 *val)
  676. {
  677. *val = rfi_flush ? 1 : 0;
  678. return 0;
  679. }
  680. DEFINE_SIMPLE_ATTRIBUTE(fops_rfi_flush, rfi_flush_get, rfi_flush_set, "%llu\n");
  681. static __init int rfi_flush_debugfs_init(void)
  682. {
  683. debugfs_create_file("rfi_flush", 0600, powerpc_debugfs_root, NULL, &fops_rfi_flush);
  684. return 0;
  685. }
  686. device_initcall(rfi_flush_debugfs_init);
  687. #endif
  688. #endif /* CONFIG_PPC_BOOK3S_64 */