smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SMP support for PowerNV machines.
  4. *
  5. * Copyright 2011 IBM Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <linux/sched/hotplug.h>
  11. #include <linux/smp.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/cpu.h>
  17. #include <asm/irq.h>
  18. #include <asm/smp.h>
  19. #include <asm/paca.h>
  20. #include <asm/machdep.h>
  21. #include <asm/cputable.h>
  22. #include <asm/firmware.h>
  23. #include <asm/vdso_datapage.h>
  24. #include <asm/cputhreads.h>
  25. #include <asm/xics.h>
  26. #include <asm/xive.h>
  27. #include <asm/opal.h>
  28. #include <asm/runlatch.h>
  29. #include <asm/code-patching.h>
  30. #include <asm/dbell.h>
  31. #include <asm/kvm_ppc.h>
  32. #include <asm/ppc-opcode.h>
  33. #include <asm/cpuidle.h>
  34. #include <asm/kexec.h>
  35. #include <asm/reg.h>
  36. #include <asm/powernv.h>
  37. #include "powernv.h"
  38. #ifdef DEBUG
  39. #include <asm/udbg.h>
  40. #define DBG(fmt...) udbg_printf(fmt)
  41. #else
  42. #define DBG(fmt...) do { } while (0)
  43. #endif
  44. static void pnv_smp_setup_cpu(int cpu)
  45. {
  46. /*
  47. * P9 workaround for CI vector load (see traps.c),
  48. * enable the corresponding HMI interrupt
  49. */
  50. if (pvr_version_is(PVR_POWER9))
  51. mtspr(SPRN_HMEER, mfspr(SPRN_HMEER) | PPC_BIT(17));
  52. if (xive_enabled())
  53. xive_smp_setup_cpu();
  54. else if (cpu != boot_cpuid)
  55. xics_setup_cpu();
  56. }
  57. static int pnv_smp_kick_cpu(int nr)
  58. {
  59. unsigned int pcpu;
  60. unsigned long start_here =
  61. __pa(ppc_function_entry(generic_secondary_smp_init));
  62. long rc;
  63. uint8_t status;
  64. if (nr < 0 || nr >= nr_cpu_ids)
  65. return -EINVAL;
  66. pcpu = get_hard_smp_processor_id(nr);
  67. /*
  68. * If we already started or OPAL is not supported, we just
  69. * kick the CPU via the PACA
  70. */
  71. if (paca_ptrs[nr]->cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
  72. goto kick;
  73. /*
  74. * At this point, the CPU can either be spinning on the way in
  75. * from kexec or be inside OPAL waiting to be started for the
  76. * first time. OPAL v3 allows us to query OPAL to know if it
  77. * has the CPUs, so we do that
  78. */
  79. rc = opal_query_cpu_status(pcpu, &status);
  80. if (rc != OPAL_SUCCESS) {
  81. pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
  82. return -ENODEV;
  83. }
  84. /*
  85. * Already started, just kick it, probably coming from
  86. * kexec and spinning
  87. */
  88. if (status == OPAL_THREAD_STARTED)
  89. goto kick;
  90. /*
  91. * Available/inactive, let's kick it
  92. */
  93. if (status == OPAL_THREAD_INACTIVE) {
  94. pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
  95. rc = opal_start_cpu(pcpu, start_here);
  96. if (rc != OPAL_SUCCESS) {
  97. pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
  98. return -ENODEV;
  99. }
  100. } else {
  101. /*
  102. * An unavailable CPU (or any other unknown status)
  103. * shouldn't be started. It should also
  104. * not be in the possible map but currently it can
  105. * happen
  106. */
  107. pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
  108. " (status %d)...\n", nr, pcpu, status);
  109. return -ENODEV;
  110. }
  111. kick:
  112. return smp_generic_kick_cpu(nr);
  113. }
  114. #ifdef CONFIG_HOTPLUG_CPU
  115. static int pnv_smp_cpu_disable(void)
  116. {
  117. int cpu = smp_processor_id();
  118. /* This is identical to pSeries... might consolidate by
  119. * moving migrate_irqs_away to a ppc_md with default to
  120. * the generic fixup_irqs. --BenH.
  121. */
  122. set_cpu_online(cpu, false);
  123. vdso_data->processorCount--;
  124. if (cpu == boot_cpuid)
  125. boot_cpuid = cpumask_any(cpu_online_mask);
  126. if (xive_enabled())
  127. xive_smp_disable_cpu();
  128. else
  129. xics_migrate_irqs_away();
  130. cleanup_cpu_mmu_context();
  131. return 0;
  132. }
  133. static void pnv_flush_interrupts(void)
  134. {
  135. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  136. if (xive_enabled())
  137. xive_flush_interrupt();
  138. else
  139. icp_opal_flush_interrupt();
  140. } else {
  141. icp_native_flush_interrupt();
  142. }
  143. }
  144. static void pnv_cpu_offline_self(void)
  145. {
  146. unsigned long srr1, unexpected_mask, wmask;
  147. unsigned int cpu;
  148. u64 lpcr_val;
  149. /* Standard hot unplug procedure */
  150. idle_task_exit();
  151. cpu = smp_processor_id();
  152. DBG("CPU%d offline\n", cpu);
  153. generic_set_cpu_dead(cpu);
  154. smp_wmb();
  155. wmask = SRR1_WAKEMASK;
  156. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  157. wmask = SRR1_WAKEMASK_P8;
  158. /*
  159. * This turns the irq soft-disabled state we're called with, into a
  160. * hard-disabled state with pending irq_happened interrupts cleared.
  161. *
  162. * PACA_IRQ_DEC - Decrementer should be ignored.
  163. * PACA_IRQ_HMI - Can be ignored, processing is done in real mode.
  164. * PACA_IRQ_DBELL, EE, PMI - Unexpected.
  165. */
  166. hard_irq_disable();
  167. if (generic_check_cpu_restart(cpu))
  168. goto out;
  169. unexpected_mask = ~(PACA_IRQ_DEC | PACA_IRQ_HMI | PACA_IRQ_HARD_DIS);
  170. if (local_paca->irq_happened & unexpected_mask) {
  171. if (local_paca->irq_happened & PACA_IRQ_EE)
  172. pnv_flush_interrupts();
  173. DBG("CPU%d Unexpected exit while offline irq_happened=%lx!\n",
  174. cpu, local_paca->irq_happened);
  175. }
  176. local_paca->irq_happened = PACA_IRQ_HARD_DIS;
  177. /*
  178. * We don't want to take decrementer interrupts while we are
  179. * offline, so clear LPCR:PECE1. We keep PECE2 (and
  180. * LPCR_PECE_HVEE on P9) enabled so as to let IPIs in.
  181. *
  182. * If the CPU gets woken up by a special wakeup, ensure that
  183. * the SLW engine sets LPCR with decrementer bit cleared, else
  184. * the CPU will come back to the kernel due to a spurious
  185. * wakeup.
  186. */
  187. lpcr_val = mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1;
  188. pnv_program_cpu_hotplug_lpcr(cpu, lpcr_val);
  189. while (!generic_check_cpu_restart(cpu)) {
  190. /*
  191. * Clear IPI flag, since we don't handle IPIs while
  192. * offline, except for those when changing micro-threading
  193. * mode, which are handled explicitly below, and those
  194. * for coming online, which are handled via
  195. * generic_check_cpu_restart() calls.
  196. */
  197. kvmppc_clear_host_ipi(cpu);
  198. srr1 = pnv_cpu_offline(cpu);
  199. WARN_ON_ONCE(!irqs_disabled());
  200. WARN_ON(lazy_irq_pending());
  201. /*
  202. * If the SRR1 value indicates that we woke up due to
  203. * an external interrupt, then clear the interrupt.
  204. * We clear the interrupt before checking for the
  205. * reason, so as to avoid a race where we wake up for
  206. * some other reason, find nothing and clear the interrupt
  207. * just as some other cpu is sending us an interrupt.
  208. * If we returned from power7_nap as a result of
  209. * having finished executing in a KVM guest, then srr1
  210. * contains 0.
  211. */
  212. if (((srr1 & wmask) == SRR1_WAKEEE) ||
  213. ((srr1 & wmask) == SRR1_WAKEHVI)) {
  214. pnv_flush_interrupts();
  215. } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
  216. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  217. asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
  218. } else if ((srr1 & wmask) == SRR1_WAKERESET) {
  219. irq_set_pending_from_srr1(srr1);
  220. /* Does not return */
  221. }
  222. smp_mb();
  223. /*
  224. * For kdump kernels, we process the ipi and jump to
  225. * crash_ipi_callback
  226. */
  227. if (kdump_in_progress()) {
  228. /*
  229. * If we got to this point, we've not used
  230. * NMI's, otherwise we would have gone
  231. * via the SRR1_WAKERESET path. We are
  232. * using regular IPI's for waking up offline
  233. * threads.
  234. */
  235. struct pt_regs regs;
  236. ppc_save_regs(&regs);
  237. crash_ipi_callback(&regs);
  238. /* Does not return */
  239. }
  240. if (cpu_core_split_required())
  241. continue;
  242. if (srr1 && !generic_check_cpu_restart(cpu))
  243. DBG("CPU%d Unexpected exit while offline srr1=%lx!\n",
  244. cpu, srr1);
  245. }
  246. /*
  247. * Re-enable decrementer interrupts in LPCR.
  248. *
  249. * Further, we want stop states to be woken up by decrementer
  250. * for non-hotplug cases. So program the LPCR via stop api as
  251. * well.
  252. */
  253. lpcr_val = mfspr(SPRN_LPCR) | (u64)LPCR_PECE1;
  254. pnv_program_cpu_hotplug_lpcr(cpu, lpcr_val);
  255. out:
  256. DBG("CPU%d coming online...\n", cpu);
  257. }
  258. #endif /* CONFIG_HOTPLUG_CPU */
  259. static int pnv_cpu_bootable(unsigned int nr)
  260. {
  261. /*
  262. * Starting with POWER8, the subcore logic relies on all threads of a
  263. * core being booted so that they can participate in split mode
  264. * switches. So on those machines we ignore the smt_enabled_at_boot
  265. * setting (smt-enabled on the kernel command line).
  266. */
  267. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  268. return 1;
  269. return smp_generic_cpu_bootable(nr);
  270. }
  271. static int pnv_smp_prepare_cpu(int cpu)
  272. {
  273. if (xive_enabled())
  274. return xive_smp_prepare_cpu(cpu);
  275. return 0;
  276. }
  277. /* Cause IPI as setup by the interrupt controller (xics or xive) */
  278. static void (*ic_cause_ipi)(int cpu);
  279. static void pnv_cause_ipi(int cpu)
  280. {
  281. if (doorbell_try_core_ipi(cpu))
  282. return;
  283. ic_cause_ipi(cpu);
  284. }
  285. static void __init pnv_smp_probe(void)
  286. {
  287. if (xive_enabled())
  288. xive_smp_probe();
  289. else
  290. xics_smp_probe();
  291. if (cpu_has_feature(CPU_FTR_DBELL)) {
  292. ic_cause_ipi = smp_ops->cause_ipi;
  293. WARN_ON(!ic_cause_ipi);
  294. if (cpu_has_feature(CPU_FTR_ARCH_300))
  295. smp_ops->cause_ipi = doorbell_global_ipi;
  296. else
  297. smp_ops->cause_ipi = pnv_cause_ipi;
  298. }
  299. }
  300. noinstr static int pnv_system_reset_exception(struct pt_regs *regs)
  301. {
  302. if (smp_handle_nmi_ipi(regs))
  303. return 1;
  304. return 0;
  305. }
  306. static int pnv_cause_nmi_ipi(int cpu)
  307. {
  308. int64_t rc;
  309. if (cpu >= 0) {
  310. int h = get_hard_smp_processor_id(cpu);
  311. if (opal_check_token(OPAL_QUIESCE))
  312. opal_quiesce(QUIESCE_HOLD, h);
  313. rc = opal_signal_system_reset(h);
  314. if (opal_check_token(OPAL_QUIESCE))
  315. opal_quiesce(QUIESCE_RESUME, h);
  316. if (rc != OPAL_SUCCESS)
  317. return 0;
  318. return 1;
  319. } else if (cpu == NMI_IPI_ALL_OTHERS) {
  320. bool success = true;
  321. int c;
  322. if (opal_check_token(OPAL_QUIESCE))
  323. opal_quiesce(QUIESCE_HOLD, -1);
  324. /*
  325. * We do not use broadcasts (yet), because it's not clear
  326. * exactly what semantics Linux wants or the firmware should
  327. * provide.
  328. */
  329. for_each_online_cpu(c) {
  330. if (c == smp_processor_id())
  331. continue;
  332. rc = opal_signal_system_reset(
  333. get_hard_smp_processor_id(c));
  334. if (rc != OPAL_SUCCESS)
  335. success = false;
  336. }
  337. if (opal_check_token(OPAL_QUIESCE))
  338. opal_quiesce(QUIESCE_RESUME, -1);
  339. if (success)
  340. return 1;
  341. /*
  342. * Caller will fall back to doorbells, which may pick
  343. * up the remainders.
  344. */
  345. }
  346. return 0;
  347. }
  348. static struct smp_ops_t pnv_smp_ops = {
  349. .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
  350. .cause_ipi = NULL, /* Filled at runtime by pnv_smp_probe() */
  351. .cause_nmi_ipi = NULL,
  352. .probe = pnv_smp_probe,
  353. .prepare_cpu = pnv_smp_prepare_cpu,
  354. .kick_cpu = pnv_smp_kick_cpu,
  355. .setup_cpu = pnv_smp_setup_cpu,
  356. .cpu_bootable = pnv_cpu_bootable,
  357. #ifdef CONFIG_HOTPLUG_CPU
  358. .cpu_disable = pnv_smp_cpu_disable,
  359. .cpu_die = generic_cpu_die,
  360. .cpu_offline_self = pnv_cpu_offline_self,
  361. #endif /* CONFIG_HOTPLUG_CPU */
  362. };
  363. /* This is called very early during platform setup_arch */
  364. void __init pnv_smp_init(void)
  365. {
  366. if (opal_check_token(OPAL_SIGNAL_SYSTEM_RESET)) {
  367. ppc_md.system_reset_exception = pnv_system_reset_exception;
  368. pnv_smp_ops.cause_nmi_ipi = pnv_cause_nmi_ipi;
  369. }
  370. smp_ops = &pnv_smp_ops;
  371. #ifdef CONFIG_HOTPLUG_CPU
  372. #ifdef CONFIG_KEXEC_CORE
  373. crash_wake_offline = 1;
  374. #endif
  375. #endif
  376. }