smp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 "powernv.h"
  41. #ifdef DEBUG
  42. #include <asm/udbg.h>
  43. #define DBG(fmt...) udbg_printf(fmt)
  44. #else
  45. #define DBG(fmt...)
  46. #endif
  47. static void pnv_smp_setup_cpu(int cpu)
  48. {
  49. /*
  50. * P9 workaround for CI vector load (see traps.c),
  51. * enable the corresponding HMI interrupt
  52. */
  53. if (pvr_version_is(PVR_POWER9))
  54. mtspr(SPRN_HMEER, mfspr(SPRN_HMEER) | PPC_BIT(17));
  55. if (xive_enabled())
  56. xive_smp_setup_cpu();
  57. else if (cpu != boot_cpuid)
  58. xics_setup_cpu();
  59. }
  60. static int pnv_smp_kick_cpu(int nr)
  61. {
  62. unsigned int pcpu;
  63. unsigned long start_here =
  64. __pa(ppc_function_entry(generic_secondary_smp_init));
  65. long rc;
  66. uint8_t status;
  67. if (nr < 0 || nr >= nr_cpu_ids)
  68. return -EINVAL;
  69. pcpu = get_hard_smp_processor_id(nr);
  70. /*
  71. * If we already started or OPAL is not supported, we just
  72. * kick the CPU via the PACA
  73. */
  74. if (paca_ptrs[nr]->cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
  75. goto kick;
  76. /*
  77. * At this point, the CPU can either be spinning on the way in
  78. * from kexec or be inside OPAL waiting to be started for the
  79. * first time. OPAL v3 allows us to query OPAL to know if it
  80. * has the CPUs, so we do that
  81. */
  82. rc = opal_query_cpu_status(pcpu, &status);
  83. if (rc != OPAL_SUCCESS) {
  84. pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
  85. return -ENODEV;
  86. }
  87. /*
  88. * Already started, just kick it, probably coming from
  89. * kexec and spinning
  90. */
  91. if (status == OPAL_THREAD_STARTED)
  92. goto kick;
  93. /*
  94. * Available/inactive, let's kick it
  95. */
  96. if (status == OPAL_THREAD_INACTIVE) {
  97. pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
  98. rc = opal_start_cpu(pcpu, start_here);
  99. if (rc != OPAL_SUCCESS) {
  100. pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
  101. return -ENODEV;
  102. }
  103. } else {
  104. /*
  105. * An unavailable CPU (or any other unknown status)
  106. * shouldn't be started. It should also
  107. * not be in the possible map but currently it can
  108. * happen
  109. */
  110. pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
  111. " (status %d)...\n", nr, pcpu, status);
  112. return -ENODEV;
  113. }
  114. kick:
  115. return smp_generic_kick_cpu(nr);
  116. }
  117. #ifdef CONFIG_HOTPLUG_CPU
  118. static int pnv_smp_cpu_disable(void)
  119. {
  120. int cpu = smp_processor_id();
  121. /* This is identical to pSeries... might consolidate by
  122. * moving migrate_irqs_away to a ppc_md with default to
  123. * the generic fixup_irqs. --BenH.
  124. */
  125. set_cpu_online(cpu, false);
  126. vdso_data->processorCount--;
  127. if (cpu == boot_cpuid)
  128. boot_cpuid = cpumask_any(cpu_online_mask);
  129. if (xive_enabled())
  130. xive_smp_disable_cpu();
  131. else
  132. xics_migrate_irqs_away();
  133. return 0;
  134. }
  135. static void pnv_smp_cpu_kill_self(void)
  136. {
  137. unsigned int cpu;
  138. unsigned long srr1, wmask;
  139. /* Standard hot unplug procedure */
  140. /*
  141. * This hard disables local interurpts, ensuring we have no lazy
  142. * irqs pending.
  143. */
  144. WARN_ON(irqs_disabled());
  145. hard_irq_disable();
  146. WARN_ON(lazy_irq_pending());
  147. idle_task_exit();
  148. current->active_mm = NULL; /* for sanity */
  149. cpu = smp_processor_id();
  150. DBG("CPU%d offline\n", cpu);
  151. generic_set_cpu_dead(cpu);
  152. smp_wmb();
  153. wmask = SRR1_WAKEMASK;
  154. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  155. wmask = SRR1_WAKEMASK_P8;
  156. while (!generic_check_cpu_restart(cpu)) {
  157. /*
  158. * Clear IPI flag, since we don't handle IPIs while
  159. * offline, except for those when changing micro-threading
  160. * mode, which are handled explicitly below, and those
  161. * for coming online, which are handled via
  162. * generic_check_cpu_restart() calls.
  163. */
  164. kvmppc_set_host_ipi(cpu, 0);
  165. srr1 = pnv_cpu_offline(cpu);
  166. WARN_ON(lazy_irq_pending());
  167. /*
  168. * If the SRR1 value indicates that we woke up due to
  169. * an external interrupt, then clear the interrupt.
  170. * We clear the interrupt before checking for the
  171. * reason, so as to avoid a race where we wake up for
  172. * some other reason, find nothing and clear the interrupt
  173. * just as some other cpu is sending us an interrupt.
  174. * If we returned from power7_nap as a result of
  175. * having finished executing in a KVM guest, then srr1
  176. * contains 0.
  177. */
  178. if (((srr1 & wmask) == SRR1_WAKEEE) ||
  179. ((srr1 & wmask) == SRR1_WAKEHVI)) {
  180. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  181. if (xive_enabled())
  182. xive_flush_interrupt();
  183. else
  184. icp_opal_flush_interrupt();
  185. } else
  186. icp_native_flush_interrupt();
  187. } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
  188. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  189. asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
  190. } else if ((srr1 & wmask) == SRR1_WAKERESET) {
  191. irq_set_pending_from_srr1(srr1);
  192. /* Does not return */
  193. }
  194. smp_mb();
  195. /*
  196. * For kdump kernels, we process the ipi and jump to
  197. * crash_ipi_callback
  198. */
  199. if (kdump_in_progress()) {
  200. /*
  201. * If we got to this point, we've not used
  202. * NMI's, otherwise we would have gone
  203. * via the SRR1_WAKERESET path. We are
  204. * using regular IPI's for waking up offline
  205. * threads.
  206. */
  207. struct pt_regs regs;
  208. ppc_save_regs(&regs);
  209. crash_ipi_callback(&regs);
  210. /* Does not return */
  211. }
  212. if (cpu_core_split_required())
  213. continue;
  214. if (srr1 && !generic_check_cpu_restart(cpu))
  215. DBG("CPU%d Unexpected exit while offline srr1=%lx!\n",
  216. cpu, srr1);
  217. }
  218. DBG("CPU%d coming online...\n", cpu);
  219. }
  220. #endif /* CONFIG_HOTPLUG_CPU */
  221. static int pnv_cpu_bootable(unsigned int nr)
  222. {
  223. /*
  224. * Starting with POWER8, the subcore logic relies on all threads of a
  225. * core being booted so that they can participate in split mode
  226. * switches. So on those machines we ignore the smt_enabled_at_boot
  227. * setting (smt-enabled on the kernel command line).
  228. */
  229. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  230. return 1;
  231. return smp_generic_cpu_bootable(nr);
  232. }
  233. static int pnv_smp_prepare_cpu(int cpu)
  234. {
  235. if (xive_enabled())
  236. return xive_smp_prepare_cpu(cpu);
  237. return 0;
  238. }
  239. /* Cause IPI as setup by the interrupt controller (xics or xive) */
  240. static void (*ic_cause_ipi)(int cpu);
  241. static void pnv_cause_ipi(int cpu)
  242. {
  243. if (doorbell_try_core_ipi(cpu))
  244. return;
  245. ic_cause_ipi(cpu);
  246. }
  247. static void __init pnv_smp_probe(void)
  248. {
  249. if (xive_enabled())
  250. xive_smp_probe();
  251. else
  252. xics_smp_probe();
  253. if (cpu_has_feature(CPU_FTR_DBELL)) {
  254. ic_cause_ipi = smp_ops->cause_ipi;
  255. WARN_ON(!ic_cause_ipi);
  256. if (cpu_has_feature(CPU_FTR_ARCH_300))
  257. smp_ops->cause_ipi = doorbell_global_ipi;
  258. else
  259. smp_ops->cause_ipi = pnv_cause_ipi;
  260. }
  261. }
  262. static int pnv_system_reset_exception(struct pt_regs *regs)
  263. {
  264. if (smp_handle_nmi_ipi(regs))
  265. return 1;
  266. return 0;
  267. }
  268. static int pnv_cause_nmi_ipi(int cpu)
  269. {
  270. int64_t rc;
  271. if (cpu >= 0) {
  272. int h = get_hard_smp_processor_id(cpu);
  273. if (opal_check_token(OPAL_QUIESCE))
  274. opal_quiesce(QUIESCE_HOLD, h);
  275. rc = opal_signal_system_reset(h);
  276. if (opal_check_token(OPAL_QUIESCE))
  277. opal_quiesce(QUIESCE_RESUME, h);
  278. if (rc != OPAL_SUCCESS)
  279. return 0;
  280. return 1;
  281. } else if (cpu == NMI_IPI_ALL_OTHERS) {
  282. bool success = true;
  283. int c;
  284. if (opal_check_token(OPAL_QUIESCE))
  285. opal_quiesce(QUIESCE_HOLD, -1);
  286. /*
  287. * We do not use broadcasts (yet), because it's not clear
  288. * exactly what semantics Linux wants or the firmware should
  289. * provide.
  290. */
  291. for_each_online_cpu(c) {
  292. if (c == smp_processor_id())
  293. continue;
  294. rc = opal_signal_system_reset(
  295. get_hard_smp_processor_id(c));
  296. if (rc != OPAL_SUCCESS)
  297. success = false;
  298. }
  299. if (opal_check_token(OPAL_QUIESCE))
  300. opal_quiesce(QUIESCE_RESUME, -1);
  301. if (success)
  302. return 1;
  303. /*
  304. * Caller will fall back to doorbells, which may pick
  305. * up the remainders.
  306. */
  307. }
  308. return 0;
  309. }
  310. static struct smp_ops_t pnv_smp_ops = {
  311. .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
  312. .cause_ipi = NULL, /* Filled at runtime by pnv_smp_probe() */
  313. .cause_nmi_ipi = NULL,
  314. .probe = pnv_smp_probe,
  315. .prepare_cpu = pnv_smp_prepare_cpu,
  316. .kick_cpu = pnv_smp_kick_cpu,
  317. .setup_cpu = pnv_smp_setup_cpu,
  318. .cpu_bootable = pnv_cpu_bootable,
  319. #ifdef CONFIG_HOTPLUG_CPU
  320. .cpu_disable = pnv_smp_cpu_disable,
  321. .cpu_die = generic_cpu_die,
  322. #endif /* CONFIG_HOTPLUG_CPU */
  323. };
  324. /* This is called very early during platform setup_arch */
  325. void __init pnv_smp_init(void)
  326. {
  327. if (opal_check_token(OPAL_SIGNAL_SYSTEM_RESET)) {
  328. ppc_md.system_reset_exception = pnv_system_reset_exception;
  329. pnv_smp_ops.cause_nmi_ipi = pnv_cause_nmi_ipi;
  330. }
  331. smp_ops = &pnv_smp_ops;
  332. #ifdef CONFIG_HOTPLUG_CPU
  333. ppc_md.cpu_die = pnv_smp_cpu_kill_self;
  334. #ifdef CONFIG_KEXEC_CORE
  335. crash_wake_offline = 1;
  336. #endif
  337. #endif
  338. }