ipi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/cpumask.h>
  3. #include <linux/delay.h>
  4. #include <linux/smp.h>
  5. #include <asm/io_apic.h>
  6. #include "local.h"
  7. DEFINE_STATIC_KEY_FALSE(apic_use_ipi_shorthand);
  8. #ifdef CONFIG_SMP
  9. static int apic_ipi_shorthand_off __ro_after_init;
  10. static __init int apic_ipi_shorthand(char *str)
  11. {
  12. get_option(&str, &apic_ipi_shorthand_off);
  13. return 1;
  14. }
  15. __setup("no_ipi_broadcast=", apic_ipi_shorthand);
  16. static int __init print_ipi_mode(void)
  17. {
  18. pr_info("IPI shorthand broadcast: %s\n",
  19. apic_ipi_shorthand_off ? "disabled" : "enabled");
  20. return 0;
  21. }
  22. late_initcall(print_ipi_mode);
  23. void apic_smt_update(void)
  24. {
  25. /*
  26. * Do not switch to broadcast mode if:
  27. * - Disabled on the command line
  28. * - Only a single CPU is online
  29. * - Not all present CPUs have been at least booted once
  30. *
  31. * The latter is important as the local APIC might be in some
  32. * random state and a broadcast might cause havoc. That's
  33. * especially true for NMI broadcasting.
  34. */
  35. if (apic_ipi_shorthand_off || num_online_cpus() == 1 ||
  36. !cpumask_equal(cpu_present_mask, &cpus_booted_once_mask)) {
  37. static_branch_disable(&apic_use_ipi_shorthand);
  38. } else {
  39. static_branch_enable(&apic_use_ipi_shorthand);
  40. }
  41. }
  42. void apic_send_IPI_allbutself(unsigned int vector)
  43. {
  44. if (num_online_cpus() < 2)
  45. return;
  46. if (static_branch_likely(&apic_use_ipi_shorthand))
  47. __apic_send_IPI_allbutself(vector);
  48. else
  49. __apic_send_IPI_mask_allbutself(cpu_online_mask, vector);
  50. }
  51. /*
  52. * Send a 'reschedule' IPI to another CPU. It goes straight through and
  53. * wastes no time serializing anything. Worst case is that we lose a
  54. * reschedule ...
  55. */
  56. void native_smp_send_reschedule(int cpu)
  57. {
  58. if (unlikely(cpu_is_offline(cpu))) {
  59. WARN(1, "sched: Unexpected reschedule of offline CPU#%d!\n", cpu);
  60. return;
  61. }
  62. __apic_send_IPI(cpu, RESCHEDULE_VECTOR);
  63. }
  64. void native_send_call_func_single_ipi(int cpu)
  65. {
  66. __apic_send_IPI(cpu, CALL_FUNCTION_SINGLE_VECTOR);
  67. }
  68. void native_send_call_func_ipi(const struct cpumask *mask)
  69. {
  70. if (static_branch_likely(&apic_use_ipi_shorthand)) {
  71. unsigned int cpu = smp_processor_id();
  72. if (!cpumask_or_equal(mask, cpumask_of(cpu), cpu_online_mask))
  73. goto sendmask;
  74. if (cpumask_test_cpu(cpu, mask))
  75. __apic_send_IPI_all(CALL_FUNCTION_VECTOR);
  76. else if (num_online_cpus() > 1)
  77. __apic_send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  78. return;
  79. }
  80. sendmask:
  81. __apic_send_IPI_mask(mask, CALL_FUNCTION_VECTOR);
  82. }
  83. void apic_send_nmi_to_offline_cpu(unsigned int cpu)
  84. {
  85. if (WARN_ON_ONCE(!apic->nmi_to_offline_cpu))
  86. return;
  87. if (WARN_ON_ONCE(!cpumask_test_cpu(cpu, &cpus_booted_once_mask)))
  88. return;
  89. apic->send_IPI(cpu, NMI_VECTOR);
  90. }
  91. #endif /* CONFIG_SMP */
  92. static inline int __prepare_ICR2(unsigned int mask)
  93. {
  94. return SET_XAPIC_DEST_FIELD(mask);
  95. }
  96. u32 apic_mem_wait_icr_idle_timeout(void)
  97. {
  98. int cnt;
  99. for (cnt = 0; cnt < 1000; cnt++) {
  100. if (!(apic_read(APIC_ICR) & APIC_ICR_BUSY))
  101. return 0;
  102. inc_irq_stat(icr_read_retry_count);
  103. udelay(100);
  104. }
  105. return APIC_ICR_BUSY;
  106. }
  107. void apic_mem_wait_icr_idle(void)
  108. {
  109. while (native_apic_mem_read(APIC_ICR) & APIC_ICR_BUSY)
  110. cpu_relax();
  111. }
  112. /*
  113. * This is safe against interruption because it only writes the lower 32
  114. * bits of the APIC_ICR register. The destination field is ignored for
  115. * short hand IPIs.
  116. *
  117. * wait_icr_idle()
  118. * write(ICR2, dest)
  119. * NMI
  120. * wait_icr_idle()
  121. * write(ICR)
  122. * wait_icr_idle()
  123. * write(ICR)
  124. *
  125. * This function does not need to disable interrupts as there is no ICR2
  126. * interaction. The memory write is direct except when the machine is
  127. * affected by the 11AP Pentium erratum, which turns the plain write into
  128. * an XCHG operation.
  129. */
  130. static void __default_send_IPI_shortcut(unsigned int shortcut, int vector)
  131. {
  132. /*
  133. * Wait for the previous ICR command to complete. Use
  134. * safe_apic_wait_icr_idle() for the NMI vector as there have been
  135. * issues where otherwise the system hangs when the panic CPU tries
  136. * to stop the others before launching the kdump kernel.
  137. */
  138. if (unlikely(vector == NMI_VECTOR))
  139. apic_mem_wait_icr_idle_timeout();
  140. else
  141. apic_mem_wait_icr_idle();
  142. /* Destination field (ICR2) and the destination mode are ignored */
  143. native_apic_mem_write(APIC_ICR, __prepare_ICR(shortcut, vector, 0));
  144. }
  145. /*
  146. * This is used to send an IPI with no shorthand notation (the destination is
  147. * specified in bits 56 to 63 of the ICR).
  148. */
  149. void __default_send_IPI_dest_field(unsigned int dest_mask, int vector,
  150. unsigned int dest_mode)
  151. {
  152. /* See comment in __default_send_IPI_shortcut() */
  153. if (unlikely(vector == NMI_VECTOR))
  154. apic_mem_wait_icr_idle_timeout();
  155. else
  156. apic_mem_wait_icr_idle();
  157. /* Set the IPI destination field in the ICR */
  158. native_apic_mem_write(APIC_ICR2, __prepare_ICR2(dest_mask));
  159. /* Send it with the proper destination mode */
  160. native_apic_mem_write(APIC_ICR, __prepare_ICR(0, vector, dest_mode));
  161. }
  162. void default_send_IPI_single_phys(int cpu, int vector)
  163. {
  164. unsigned long flags;
  165. local_irq_save(flags);
  166. __default_send_IPI_dest_field(per_cpu(x86_cpu_to_apicid, cpu),
  167. vector, APIC_DEST_PHYSICAL);
  168. local_irq_restore(flags);
  169. }
  170. void default_send_IPI_mask_sequence_phys(const struct cpumask *mask, int vector)
  171. {
  172. unsigned long flags;
  173. unsigned long cpu;
  174. local_irq_save(flags);
  175. for_each_cpu(cpu, mask) {
  176. __default_send_IPI_dest_field(per_cpu(x86_cpu_to_apicid,
  177. cpu), vector, APIC_DEST_PHYSICAL);
  178. }
  179. local_irq_restore(flags);
  180. }
  181. void default_send_IPI_mask_allbutself_phys(const struct cpumask *mask,
  182. int vector)
  183. {
  184. unsigned int cpu, this_cpu = smp_processor_id();
  185. unsigned long flags;
  186. local_irq_save(flags);
  187. for_each_cpu(cpu, mask) {
  188. if (cpu == this_cpu)
  189. continue;
  190. __default_send_IPI_dest_field(per_cpu(x86_cpu_to_apicid,
  191. cpu), vector, APIC_DEST_PHYSICAL);
  192. }
  193. local_irq_restore(flags);
  194. }
  195. /*
  196. * Helper function for APICs which insist on cpumasks
  197. */
  198. void default_send_IPI_single(int cpu, int vector)
  199. {
  200. __apic_send_IPI_mask(cpumask_of(cpu), vector);
  201. }
  202. void default_send_IPI_allbutself(int vector)
  203. {
  204. __default_send_IPI_shortcut(APIC_DEST_ALLBUT, vector);
  205. }
  206. void default_send_IPI_all(int vector)
  207. {
  208. __default_send_IPI_shortcut(APIC_DEST_ALLINC, vector);
  209. }
  210. void default_send_IPI_self(int vector)
  211. {
  212. __default_send_IPI_shortcut(APIC_DEST_SELF, vector);
  213. }
  214. #ifdef CONFIG_X86_32
  215. void default_send_IPI_mask_sequence_logical(const struct cpumask *mask, int vector)
  216. {
  217. unsigned long flags;
  218. unsigned int cpu;
  219. local_irq_save(flags);
  220. for_each_cpu(cpu, mask)
  221. __default_send_IPI_dest_field(1U << cpu, vector, APIC_DEST_LOGICAL);
  222. local_irq_restore(flags);
  223. }
  224. void default_send_IPI_mask_allbutself_logical(const struct cpumask *mask,
  225. int vector)
  226. {
  227. unsigned int cpu, this_cpu = smp_processor_id();
  228. unsigned long flags;
  229. local_irq_save(flags);
  230. for_each_cpu(cpu, mask) {
  231. if (cpu == this_cpu)
  232. continue;
  233. __default_send_IPI_dest_field(1U << cpu, vector, APIC_DEST_LOGICAL);
  234. }
  235. local_irq_restore(flags);
  236. }
  237. void default_send_IPI_mask_logical(const struct cpumask *cpumask, int vector)
  238. {
  239. unsigned long mask = cpumask_bits(cpumask)[0];
  240. unsigned long flags;
  241. if (!mask)
  242. return;
  243. local_irq_save(flags);
  244. WARN_ON(mask & ~cpumask_bits(cpu_online_mask)[0]);
  245. __default_send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
  246. local_irq_restore(flags);
  247. }
  248. #ifdef CONFIG_SMP
  249. static int convert_apicid_to_cpu(u32 apic_id)
  250. {
  251. int i;
  252. for_each_possible_cpu(i) {
  253. if (per_cpu(x86_cpu_to_apicid, i) == apic_id)
  254. return i;
  255. }
  256. return -1;
  257. }
  258. int safe_smp_processor_id(void)
  259. {
  260. u32 apicid;
  261. int cpuid;
  262. if (!boot_cpu_has(X86_FEATURE_APIC))
  263. return 0;
  264. apicid = read_apic_id();
  265. if (apicid == BAD_APICID)
  266. return 0;
  267. cpuid = convert_apicid_to_cpu(apicid);
  268. return cpuid >= 0 ? cpuid : 0;
  269. }
  270. #endif
  271. #endif