smp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SMP Support
  4. *
  5. * Copyright (C) 1999 Walt Drummond <[email protected]>
  6. * Copyright (C) 1999, 2001, 2003 David Mosberger-Tang <[email protected]>
  7. *
  8. * Lots of stuff stolen from arch/alpha/kernel/smp.c
  9. *
  10. * 01/05/16 Rohit Seth <[email protected]> IA64-SMP functions. Reorganized
  11. * the existing code (on the lines of x86 port).
  12. * 00/09/11 David Mosberger <[email protected]> Do loops_per_jiffy
  13. * calibration on each CPU.
  14. * 00/08/23 Asit Mallick <[email protected]> fixed logical processor id
  15. * 00/03/31 Rohit Seth <[email protected]> Fixes for Bootstrap Processor
  16. * & cpu_online_map now gets done here (instead of setup.c)
  17. * 99/10/05 davidm Update to bring it in sync with new command-line processing
  18. * scheme.
  19. * 10/13/00 Goutham Rao <[email protected]> Updated smp_call_function and
  20. * smp_call_function_single to resend IPI on timeouts
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/smp.h>
  28. #include <linux/kernel_stat.h>
  29. #include <linux/mm.h>
  30. #include <linux/cache.h>
  31. #include <linux/delay.h>
  32. #include <linux/efi.h>
  33. #include <linux/bitops.h>
  34. #include <linux/kexec.h>
  35. #include <linux/atomic.h>
  36. #include <asm/current.h>
  37. #include <asm/delay.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/page.h>
  41. #include <asm/processor.h>
  42. #include <asm/ptrace.h>
  43. #include <asm/sal.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/unistd.h>
  46. #include <asm/mca.h>
  47. #include <asm/xtp.h>
  48. /*
  49. * Note: alignment of 4 entries/cacheline was empirically determined
  50. * to be a good tradeoff between hot cachelines & spreading the array
  51. * across too many cacheline.
  52. */
  53. static struct local_tlb_flush_counts {
  54. unsigned int count;
  55. } __attribute__((__aligned__(32))) local_tlb_flush_counts[NR_CPUS];
  56. static DEFINE_PER_CPU_SHARED_ALIGNED(unsigned short [NR_CPUS],
  57. shadow_flush_counts);
  58. #define IPI_CALL_FUNC 0
  59. #define IPI_CPU_STOP 1
  60. #define IPI_CALL_FUNC_SINGLE 2
  61. #define IPI_KDUMP_CPU_STOP 3
  62. /* This needs to be cacheline aligned because it is written to by *other* CPUs. */
  63. static DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, ipi_operation);
  64. extern void cpu_halt (void);
  65. static void
  66. stop_this_cpu(void)
  67. {
  68. /*
  69. * Remove this CPU:
  70. */
  71. set_cpu_online(smp_processor_id(), false);
  72. max_xtp();
  73. local_irq_disable();
  74. cpu_halt();
  75. }
  76. void
  77. cpu_die(void)
  78. {
  79. max_xtp();
  80. local_irq_disable();
  81. cpu_halt();
  82. /* Should never be here */
  83. BUG();
  84. for (;;);
  85. }
  86. irqreturn_t
  87. handle_IPI (int irq, void *dev_id)
  88. {
  89. int this_cpu = get_cpu();
  90. unsigned long *pending_ipis = &__ia64_per_cpu_var(ipi_operation);
  91. unsigned long ops;
  92. mb(); /* Order interrupt and bit testing. */
  93. while ((ops = xchg(pending_ipis, 0)) != 0) {
  94. mb(); /* Order bit clearing and data access. */
  95. do {
  96. unsigned long which;
  97. which = ffz(~ops);
  98. ops &= ~(1 << which);
  99. switch (which) {
  100. case IPI_CPU_STOP:
  101. stop_this_cpu();
  102. break;
  103. case IPI_CALL_FUNC:
  104. generic_smp_call_function_interrupt();
  105. break;
  106. case IPI_CALL_FUNC_SINGLE:
  107. generic_smp_call_function_single_interrupt();
  108. break;
  109. #ifdef CONFIG_KEXEC
  110. case IPI_KDUMP_CPU_STOP:
  111. unw_init_running(kdump_cpu_freeze, NULL);
  112. break;
  113. #endif
  114. default:
  115. printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
  116. this_cpu, which);
  117. break;
  118. }
  119. } while (ops);
  120. mb(); /* Order data access and bit testing. */
  121. }
  122. put_cpu();
  123. return IRQ_HANDLED;
  124. }
  125. /*
  126. * Called with preemption disabled.
  127. */
  128. static inline void
  129. send_IPI_single (int dest_cpu, int op)
  130. {
  131. set_bit(op, &per_cpu(ipi_operation, dest_cpu));
  132. ia64_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
  133. }
  134. /*
  135. * Called with preemption disabled.
  136. */
  137. static inline void
  138. send_IPI_allbutself (int op)
  139. {
  140. unsigned int i;
  141. for_each_online_cpu(i) {
  142. if (i != smp_processor_id())
  143. send_IPI_single(i, op);
  144. }
  145. }
  146. /*
  147. * Called with preemption disabled.
  148. */
  149. static inline void
  150. send_IPI_mask(const struct cpumask *mask, int op)
  151. {
  152. unsigned int cpu;
  153. for_each_cpu(cpu, mask) {
  154. send_IPI_single(cpu, op);
  155. }
  156. }
  157. /*
  158. * Called with preemption disabled.
  159. */
  160. static inline void
  161. send_IPI_all (int op)
  162. {
  163. int i;
  164. for_each_online_cpu(i) {
  165. send_IPI_single(i, op);
  166. }
  167. }
  168. /*
  169. * Called with preemption disabled.
  170. */
  171. static inline void
  172. send_IPI_self (int op)
  173. {
  174. send_IPI_single(smp_processor_id(), op);
  175. }
  176. #ifdef CONFIG_KEXEC
  177. void
  178. kdump_smp_send_stop(void)
  179. {
  180. send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
  181. }
  182. void
  183. kdump_smp_send_init(void)
  184. {
  185. unsigned int cpu, self_cpu;
  186. self_cpu = smp_processor_id();
  187. for_each_online_cpu(cpu) {
  188. if (cpu != self_cpu) {
  189. if(kdump_status[cpu] == 0)
  190. ia64_send_ipi(cpu, 0, IA64_IPI_DM_INIT, 0);
  191. }
  192. }
  193. }
  194. #endif
  195. /*
  196. * Called with preemption disabled.
  197. */
  198. void
  199. smp_send_reschedule (int cpu)
  200. {
  201. ia64_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
  202. }
  203. EXPORT_SYMBOL_GPL(smp_send_reschedule);
  204. /*
  205. * Called with preemption disabled.
  206. */
  207. static void
  208. smp_send_local_flush_tlb (int cpu)
  209. {
  210. ia64_send_ipi(cpu, IA64_IPI_LOCAL_TLB_FLUSH, IA64_IPI_DM_INT, 0);
  211. }
  212. void
  213. smp_local_flush_tlb(void)
  214. {
  215. /*
  216. * Use atomic ops. Otherwise, the load/increment/store sequence from
  217. * a "++" operation can have the line stolen between the load & store.
  218. * The overhead of the atomic op in negligible in this case & offers
  219. * significant benefit for the brief periods where lots of cpus
  220. * are simultaneously flushing TLBs.
  221. */
  222. ia64_fetchadd(1, &local_tlb_flush_counts[smp_processor_id()].count, acq);
  223. local_flush_tlb_all();
  224. }
  225. #define FLUSH_DELAY 5 /* Usec backoff to eliminate excessive cacheline bouncing */
  226. void
  227. smp_flush_tlb_cpumask(cpumask_t xcpumask)
  228. {
  229. unsigned short *counts = __ia64_per_cpu_var(shadow_flush_counts);
  230. cpumask_t cpumask = xcpumask;
  231. int mycpu, cpu, flush_mycpu = 0;
  232. preempt_disable();
  233. mycpu = smp_processor_id();
  234. for_each_cpu(cpu, &cpumask)
  235. counts[cpu] = local_tlb_flush_counts[cpu].count & 0xffff;
  236. mb();
  237. for_each_cpu(cpu, &cpumask) {
  238. if (cpu == mycpu)
  239. flush_mycpu = 1;
  240. else
  241. smp_send_local_flush_tlb(cpu);
  242. }
  243. if (flush_mycpu)
  244. smp_local_flush_tlb();
  245. for_each_cpu(cpu, &cpumask)
  246. while(counts[cpu] == (local_tlb_flush_counts[cpu].count & 0xffff))
  247. udelay(FLUSH_DELAY);
  248. preempt_enable();
  249. }
  250. void
  251. smp_flush_tlb_all (void)
  252. {
  253. on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1);
  254. }
  255. void
  256. smp_flush_tlb_mm (struct mm_struct *mm)
  257. {
  258. cpumask_var_t cpus;
  259. preempt_disable();
  260. /* this happens for the common case of a single-threaded fork(): */
  261. if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
  262. {
  263. local_finish_flush_tlb_mm(mm);
  264. preempt_enable();
  265. return;
  266. }
  267. if (!alloc_cpumask_var(&cpus, GFP_ATOMIC)) {
  268. smp_call_function((void (*)(void *))local_finish_flush_tlb_mm,
  269. mm, 1);
  270. } else {
  271. cpumask_copy(cpus, mm_cpumask(mm));
  272. smp_call_function_many(cpus,
  273. (void (*)(void *))local_finish_flush_tlb_mm, mm, 1);
  274. free_cpumask_var(cpus);
  275. }
  276. local_irq_disable();
  277. local_finish_flush_tlb_mm(mm);
  278. local_irq_enable();
  279. preempt_enable();
  280. }
  281. void arch_send_call_function_single_ipi(int cpu)
  282. {
  283. send_IPI_single(cpu, IPI_CALL_FUNC_SINGLE);
  284. }
  285. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  286. {
  287. send_IPI_mask(mask, IPI_CALL_FUNC);
  288. }
  289. /*
  290. * this function calls the 'stop' function on all other CPUs in the system.
  291. */
  292. void
  293. smp_send_stop (void)
  294. {
  295. send_IPI_allbutself(IPI_CPU_STOP);
  296. }