smp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Intel SMP support routines.
  4. *
  5. * (c) 1995 Alan Cox, Building #3 <[email protected]>
  6. * (c) 1998-99, 2000, 2009 Ingo Molnar <[email protected]>
  7. * (c) 2002,2003 Andi Kleen, SuSE Labs.
  8. *
  9. * i386 and x86_64 integration by Glauber Costa <[email protected]>
  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. ack_APIC_irq();
  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 cpu = smp_processor_id();
  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. if (atomic_cmpxchg(&stopping_cpu, -1, cpu) != -1)
  146. return;
  147. /* For kexec, ensure that offline CPUs are out of MWAIT and in HLT */
  148. if (kexec_in_progress)
  149. smp_kick_mwait_play_dead();
  150. /*
  151. * 1) Send an IPI on the reboot vector to all other CPUs.
  152. *
  153. * The other CPUs should react on it after leaving critical
  154. * sections and re-enabling interrupts. They might still hold
  155. * locks, but there is nothing which can be done about that.
  156. *
  157. * 2) Wait for all other CPUs to report that they reached the
  158. * HLT loop in stop_this_cpu()
  159. *
  160. * 3) If #2 timed out send an NMI to the CPUs which did not
  161. * yet report
  162. *
  163. * 4) Wait for all other CPUs to report that they reached the
  164. * HLT loop in stop_this_cpu()
  165. *
  166. * #3 can obviously race against a CPU reaching the HLT loop late.
  167. * That CPU will have reported already and the "have all CPUs
  168. * reached HLT" condition will be true despite the fact that the
  169. * other CPU is still handling the NMI. Again, there is no
  170. * protection against that as "disabled" APICs still respond to
  171. * NMIs.
  172. */
  173. cpumask_copy(&cpus_stop_mask, cpu_online_mask);
  174. cpumask_clear_cpu(cpu, &cpus_stop_mask);
  175. if (!cpumask_empty(&cpus_stop_mask)) {
  176. apic_send_IPI_allbutself(REBOOT_VECTOR);
  177. /*
  178. * Don't wait longer than a second for IPI completion. The
  179. * wait request is not checked here because that would
  180. * prevent an NMI shutdown attempt in case that not all
  181. * CPUs reach shutdown state.
  182. */
  183. timeout = USEC_PER_SEC;
  184. while (!cpumask_empty(&cpus_stop_mask) && timeout--)
  185. udelay(1);
  186. }
  187. /* if the REBOOT_VECTOR didn't work, try with the NMI */
  188. if (!cpumask_empty(&cpus_stop_mask)) {
  189. /*
  190. * If NMI IPI is enabled, try to register the stop handler
  191. * and send the IPI. In any case try to wait for the other
  192. * CPUs to stop.
  193. */
  194. if (!smp_no_nmi_ipi && !register_stop_handler()) {
  195. pr_emerg("Shutting down cpus with NMI\n");
  196. for_each_cpu(cpu, &cpus_stop_mask)
  197. apic->send_IPI(cpu, NMI_VECTOR);
  198. }
  199. /*
  200. * Don't wait longer than 10 ms if the caller didn't
  201. * request it. If wait is true, the machine hangs here if
  202. * one or more CPUs do not reach shutdown state.
  203. */
  204. timeout = USEC_PER_MSEC * 10;
  205. while (!cpumask_empty(&cpus_stop_mask) && (wait || timeout--))
  206. udelay(1);
  207. }
  208. local_irq_save(flags);
  209. disable_local_APIC();
  210. mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
  211. local_irq_restore(flags);
  212. /*
  213. * Ensure that the cpus_stop_mask cache lines are invalidated on
  214. * the other CPUs. See comment vs. SME in stop_this_cpu().
  215. */
  216. cpumask_clear(&cpus_stop_mask);
  217. }
  218. /*
  219. * Reschedule call back. KVM uses this interrupt to force a cpu out of
  220. * guest mode.
  221. */
  222. DEFINE_IDTENTRY_SYSVEC_SIMPLE(sysvec_reschedule_ipi)
  223. {
  224. ack_APIC_irq();
  225. trace_reschedule_entry(RESCHEDULE_VECTOR);
  226. inc_irq_stat(irq_resched_count);
  227. scheduler_ipi();
  228. trace_reschedule_exit(RESCHEDULE_VECTOR);
  229. }
  230. DEFINE_IDTENTRY_SYSVEC(sysvec_call_function)
  231. {
  232. ack_APIC_irq();
  233. trace_call_function_entry(CALL_FUNCTION_VECTOR);
  234. inc_irq_stat(irq_call_count);
  235. generic_smp_call_function_interrupt();
  236. trace_call_function_exit(CALL_FUNCTION_VECTOR);
  237. }
  238. DEFINE_IDTENTRY_SYSVEC(sysvec_call_function_single)
  239. {
  240. ack_APIC_irq();
  241. trace_call_function_single_entry(CALL_FUNCTION_SINGLE_VECTOR);
  242. inc_irq_stat(irq_call_count);
  243. generic_smp_call_function_single_interrupt();
  244. trace_call_function_single_exit(CALL_FUNCTION_SINGLE_VECTOR);
  245. }
  246. static int __init nonmi_ipi_setup(char *str)
  247. {
  248. smp_no_nmi_ipi = true;
  249. return 1;
  250. }
  251. __setup("nonmi_ipi", nonmi_ipi_setup);
  252. struct smp_ops smp_ops = {
  253. .smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
  254. .smp_prepare_cpus = native_smp_prepare_cpus,
  255. .smp_cpus_done = native_smp_cpus_done,
  256. .stop_other_cpus = native_stop_other_cpus,
  257. #if defined(CONFIG_KEXEC_CORE)
  258. .crash_stop_other_cpus = kdump_nmi_shootdown_cpus,
  259. #endif
  260. .smp_send_reschedule = native_smp_send_reschedule,
  261. .cpu_up = native_cpu_up,
  262. .cpu_die = native_cpu_die,
  263. .cpu_disable = native_cpu_disable,
  264. .play_dead = native_play_dead,
  265. .send_call_func_ipi = native_send_call_func_ipi,
  266. .send_call_func_single_ipi = native_send_call_func_single_ipi,
  267. };
  268. EXPORT_SYMBOL_GPL(smp_ops);