smp.c 11 KB

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