smp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Intel SMP support routines.
  4. *
  5. * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
  6. * (c) 1998-99, 2000, 2009 Ingo Molnar <mingo@redhat.com>
  7. * (c) 2002,2003 Andi Kleen, SuSE Labs.
  8. *
  9. * i386 and x86_64 integration by Glauber Costa <gcosta@redhat.com>
  10. */
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/delay.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/mc146818rtc.h>
  18. #include <linux/cache.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/cpu.h>
  21. #include <linux/gfp.h>
  22. #include <linux/kexec.h>
  23. #include <asm/mtrr.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/proto.h>
  27. #include <asm/apic.h>
  28. #include <asm/cpu.h>
  29. #include <asm/idtentry.h>
  30. #include <asm/nmi.h>
  31. #include <asm/mce.h>
  32. #include <asm/trace/irq_vectors.h>
  33. #include <asm/kexec.h>
  34. #include <asm/reboot.h>
  35. /*
  36. * Some notes on x86 processor bugs affecting SMP operation:
  37. *
  38. * Pentium, Pentium Pro, II, III (and all CPUs) have bugs.
  39. * The Linux implications for SMP are handled as follows:
  40. *
  41. * Pentium III / [Xeon]
  42. * None of the E1AP-E3AP errata are visible to the user.
  43. *
  44. * E1AP. see PII A1AP
  45. * E2AP. see PII A2AP
  46. * E3AP. see PII A3AP
  47. *
  48. * Pentium II / [Xeon]
  49. * None of the A1AP-A3AP errata are visible to the user.
  50. *
  51. * A1AP. see PPro 1AP
  52. * A2AP. see PPro 2AP
  53. * A3AP. see PPro 7AP
  54. *
  55. * Pentium Pro
  56. * None of 1AP-9AP errata are visible to the normal user,
  57. * except occasional delivery of 'spurious interrupt' as trap #15.
  58. * This is very rare and a non-problem.
  59. *
  60. * 1AP. Linux maps APIC as non-cacheable
  61. * 2AP. worked around in hardware
  62. * 3AP. fixed in C0 and above steppings microcode update.
  63. * Linux does not use excessive STARTUP_IPIs.
  64. * 4AP. worked around in hardware
  65. * 5AP. symmetric IO mode (normal Linux operation) not affected.
  66. * 'noapic' mode has vector 0xf filled out properly.
  67. * 6AP. 'noapic' mode might be affected - fixed in later steppings
  68. * 7AP. We do not assume writes to the LVT deasserting IRQs
  69. * 8AP. We do not enable low power mode (deep sleep) during MP bootup
  70. * 9AP. We do not use mixed mode
  71. *
  72. * Pentium
  73. * There is a marginal case where REP MOVS on 100MHz SMP
  74. * machines with B stepping processors can fail. XXX should provide
  75. * an L1cache=Writethrough or L1cache=off option.
  76. *
  77. * B stepping CPUs may hang. There are hardware work arounds
  78. * for this. We warn about it in case your board doesn't have the work
  79. * arounds. Basically that's so I can tell anyone with a B stepping
  80. * CPU and SMP problems "tough".
  81. *
  82. * Specific items [From Pentium Processor Specification Update]
  83. *
  84. * 1AP. Linux doesn't use remote read
  85. * 2AP. Linux doesn't trust APIC errors
  86. * 3AP. We work around this
  87. * 4AP. Linux never generated 3 interrupts of the same priority
  88. * to cause a lost local interrupt.
  89. * 5AP. Remote read is never used
  90. * 6AP. not affected - worked around in hardware
  91. * 7AP. not affected - worked around in hardware
  92. * 8AP. worked around in hardware - we get explicit CS errors if not
  93. * 9AP. only 'noapic' mode affected. Might generate spurious
  94. * interrupts, we log only the first one and count the
  95. * rest silently.
  96. * 10AP. not affected - worked around in hardware
  97. * 11AP. Linux reads the APIC between writes to avoid this, as per
  98. * the documentation. Make sure you preserve this as it affects
  99. * the C stepping chips too.
  100. * 12AP. not affected - worked around in hardware
  101. * 13AP. not affected - worked around in hardware
  102. * 14AP. we always deassert INIT during bootup
  103. * 15AP. not affected - worked around in hardware
  104. * 16AP. not affected - worked around in hardware
  105. * 17AP. not affected - worked around in hardware
  106. * 18AP. not affected - worked around in hardware
  107. * 19AP. not affected - worked around in BIOS
  108. *
  109. * If this sounds worrying believe me these bugs are either ___RARE___,
  110. * or are signal timing bugs worked around in hardware and there's
  111. * about nothing of note with C stepping upwards.
  112. */
  113. static atomic_t stopping_cpu = ATOMIC_INIT(-1);
  114. static bool smp_no_nmi_ipi = false;
  115. static int smp_stop_nmi_callback(unsigned int val, struct pt_regs *regs)
  116. {
  117. /* We are registered on stopping cpu too, avoid spurious NMI */
  118. if (raw_smp_processor_id() == atomic_read(&stopping_cpu))
  119. return NMI_HANDLED;
  120. cpu_emergency_disable_virtualization();
  121. stop_this_cpu(NULL);
  122. return NMI_HANDLED;
  123. }
  124. /*
  125. * this function calls the 'stop' function on all other CPUs in the system.
  126. */
  127. DEFINE_IDTENTRY_SYSVEC(sysvec_reboot)
  128. {
  129. apic_eoi();
  130. cpu_emergency_disable_virtualization();
  131. stop_this_cpu(NULL);
  132. }
  133. static int register_stop_handler(void)
  134. {
  135. return register_nmi_handler(NMI_LOCAL, smp_stop_nmi_callback,
  136. NMI_FLAG_FIRST, "smp_stop");
  137. }
  138. static void native_stop_other_cpus(int wait)
  139. {
  140. unsigned int old_cpu, this_cpu;
  141. unsigned long flags, timeout;
  142. if (reboot_force)
  143. return;
  144. /* Only proceed if this is the first CPU to reach this code */
  145. old_cpu = -1;
  146. this_cpu = smp_processor_id();
  147. if (!atomic_try_cmpxchg(&stopping_cpu, &old_cpu, this_cpu))
  148. return;
  149. /* For kexec, ensure that offline CPUs are out of MWAIT and in HLT */
  150. if (kexec_in_progress)
  151. smp_kick_mwait_play_dead();
  152. /*
  153. * 1) Send an IPI on the reboot vector to all other CPUs.
  154. *
  155. * The other CPUs should react on it after leaving critical
  156. * sections and re-enabling interrupts. They might still hold
  157. * locks, but there is nothing which can be done about that.
  158. *
  159. * 2) Wait for all other CPUs to report that they reached the
  160. * HLT loop in stop_this_cpu()
  161. *
  162. * 3) If #2 timed out send an NMI to the CPUs which did not
  163. * yet report
  164. *
  165. * 4) Wait for all other CPUs to report that they reached the
  166. * HLT loop in stop_this_cpu()
  167. *
  168. * #3 can obviously race against a CPU reaching the HLT loop late.
  169. * That CPU will have reported already and the "have all CPUs
  170. * reached HLT" condition will be true despite the fact that the
  171. * other CPU is still handling the NMI. Again, there is no
  172. * protection against that as "disabled" APICs still respond to
  173. * NMIs.
  174. */
  175. cpumask_copy(&cpus_stop_mask, cpu_online_mask);
  176. cpumask_clear_cpu(this_cpu, &cpus_stop_mask);
  177. if (!cpumask_empty(&cpus_stop_mask)) {
  178. apic_send_IPI_allbutself(REBOOT_VECTOR);
  179. /*
  180. * Don't wait longer than a second for IPI completion. The
  181. * wait request is not checked here because that would
  182. * prevent an NMI shutdown attempt in case that not all
  183. * CPUs reach shutdown state.
  184. */
  185. timeout = USEC_PER_SEC;
  186. while (!cpumask_empty(&cpus_stop_mask) && timeout--)
  187. udelay(1);
  188. }
  189. /* if the REBOOT_VECTOR didn't work, try with the NMI */
  190. if (!cpumask_empty(&cpus_stop_mask)) {
  191. /*
  192. * If NMI IPI is enabled, try to register the stop handler
  193. * and send the IPI. In any case try to wait for the other
  194. * CPUs to stop.
  195. */
  196. if (!smp_no_nmi_ipi && !register_stop_handler()) {
  197. unsigned int cpu;
  198. pr_emerg("Shutting down cpus with NMI\n");
  199. for_each_cpu(cpu, &cpus_stop_mask)
  200. __apic_send_IPI(cpu, NMI_VECTOR);
  201. }
  202. /*
  203. * Don't wait longer than 10 ms if the caller didn't
  204. * request it. If wait is true, the machine hangs here if
  205. * one or more CPUs do not reach shutdown state.
  206. */
  207. timeout = USEC_PER_MSEC * 10;
  208. while (!cpumask_empty(&cpus_stop_mask) && (wait || timeout--))
  209. udelay(1);
  210. }
  211. local_irq_save(flags);
  212. disable_local_APIC();
  213. mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
  214. local_irq_restore(flags);
  215. /*
  216. * Ensure that the cpus_stop_mask cache lines are invalidated on
  217. * the other CPUs. See comment vs. SME in stop_this_cpu().
  218. */
  219. cpumask_clear(&cpus_stop_mask);
  220. }
  221. /*
  222. * Reschedule call back. KVM uses this interrupt to force a cpu out of
  223. * guest mode.
  224. */
  225. DEFINE_IDTENTRY_SYSVEC_SIMPLE(sysvec_reschedule_ipi)
  226. {
  227. apic_eoi();
  228. trace_reschedule_entry(RESCHEDULE_VECTOR);
  229. inc_irq_stat(irq_resched_count);
  230. scheduler_ipi();
  231. trace_reschedule_exit(RESCHEDULE_VECTOR);
  232. }
  233. DEFINE_IDTENTRY_SYSVEC(sysvec_call_function)
  234. {
  235. apic_eoi();
  236. trace_call_function_entry(CALL_FUNCTION_VECTOR);
  237. inc_irq_stat(irq_call_count);
  238. generic_smp_call_function_interrupt();
  239. trace_call_function_exit(CALL_FUNCTION_VECTOR);
  240. }
  241. DEFINE_IDTENTRY_SYSVEC(sysvec_call_function_single)
  242. {
  243. apic_eoi();
  244. trace_call_function_single_entry(CALL_FUNCTION_SINGLE_VECTOR);
  245. inc_irq_stat(irq_call_count);
  246. generic_smp_call_function_single_interrupt();
  247. trace_call_function_single_exit(CALL_FUNCTION_SINGLE_VECTOR);
  248. }
  249. static int __init nonmi_ipi_setup(char *str)
  250. {
  251. smp_no_nmi_ipi = true;
  252. return 1;
  253. }
  254. __setup("nonmi_ipi", nonmi_ipi_setup);
  255. struct smp_ops smp_ops = {
  256. .smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
  257. .smp_prepare_cpus = native_smp_prepare_cpus,
  258. .smp_cpus_done = native_smp_cpus_done,
  259. .stop_other_cpus = native_stop_other_cpus,
  260. #if defined(CONFIG_CRASH_DUMP)
  261. .crash_stop_other_cpus = kdump_nmi_shootdown_cpus,
  262. #endif
  263. .smp_send_reschedule = native_smp_send_reschedule,
  264. .kick_ap_alive = native_kick_ap,
  265. .cpu_disable = native_cpu_disable,
  266. .play_dead = native_play_dead,
  267. .send_call_func_ipi = native_send_call_func_ipi,
  268. .send_call_func_single_ipi = native_send_call_func_single_ipi,
  269. };
  270. EXPORT_SYMBOL_GPL(smp_ops);