smp.c 7.3 KB

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