smp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Structure and data for smp_call_function(). This is designed to minimise static memory
  52. * requirements. It also looks cleaner.
  53. */
  54. static __cacheline_aligned DEFINE_SPINLOCK(call_lock);
  55. struct call_data_struct {
  56. void (*func) (void *info);
  57. void *info;
  58. long wait;
  59. atomic_t started;
  60. atomic_t finished;
  61. };
  62. static volatile struct call_data_struct *call_data;
  63. #define IPI_CALL_FUNC 0
  64. #define IPI_CPU_STOP 1
  65. #define IPI_KDUMP_CPU_STOP 3
  66. /* This needs to be cacheline aligned because it is written to by *other* CPUs. */
  67. static DEFINE_PER_CPU(u64, ipi_operation) ____cacheline_aligned;
  68. extern void cpu_halt (void);
  69. void
  70. lock_ipi_calllock(void)
  71. {
  72. spin_lock_irq(&call_lock);
  73. }
  74. void
  75. unlock_ipi_calllock(void)
  76. {
  77. spin_unlock_irq(&call_lock);
  78. }
  79. static void
  80. stop_this_cpu (void)
  81. {
  82. /*
  83. * Remove this CPU:
  84. */
  85. cpu_clear(smp_processor_id(), cpu_online_map);
  86. max_xtp();
  87. local_irq_disable();
  88. cpu_halt();
  89. }
  90. void
  91. cpu_die(void)
  92. {
  93. max_xtp();
  94. local_irq_disable();
  95. cpu_halt();
  96. /* Should never be here */
  97. BUG();
  98. for (;;);
  99. }
  100. irqreturn_t
  101. handle_IPI (int irq, void *dev_id)
  102. {
  103. int this_cpu = get_cpu();
  104. unsigned long *pending_ipis = &__ia64_per_cpu_var(ipi_operation);
  105. unsigned long ops;
  106. mb(); /* Order interrupt and bit testing. */
  107. while ((ops = xchg(pending_ipis, 0)) != 0) {
  108. mb(); /* Order bit clearing and data access. */
  109. do {
  110. unsigned long which;
  111. which = ffz(~ops);
  112. ops &= ~(1 << which);
  113. switch (which) {
  114. case IPI_CALL_FUNC:
  115. {
  116. struct call_data_struct *data;
  117. void (*func)(void *info);
  118. void *info;
  119. int wait;
  120. /* release the 'pointer lock' */
  121. data = (struct call_data_struct *) call_data;
  122. func = data->func;
  123. info = data->info;
  124. wait = data->wait;
  125. mb();
  126. atomic_inc(&data->started);
  127. /*
  128. * At this point the structure may be gone unless
  129. * wait is true.
  130. */
  131. (*func)(info);
  132. /* Notify the sending CPU that the task is done. */
  133. mb();
  134. if (wait)
  135. atomic_inc(&data->finished);
  136. }
  137. break;
  138. case IPI_CPU_STOP:
  139. stop_this_cpu();
  140. break;
  141. #ifdef CONFIG_KEXEC
  142. case IPI_KDUMP_CPU_STOP:
  143. unw_init_running(kdump_cpu_freeze, NULL);
  144. break;
  145. #endif
  146. default:
  147. printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n", this_cpu, which);
  148. break;
  149. }
  150. } while (ops);
  151. mb(); /* Order data access and bit testing. */
  152. }
  153. put_cpu();
  154. return IRQ_HANDLED;
  155. }
  156. /*
  157. * Called with preeemption disabled.
  158. */
  159. static inline void
  160. send_IPI_single (int dest_cpu, int op)
  161. {
  162. set_bit(op, &per_cpu(ipi_operation, dest_cpu));
  163. platform_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
  164. }
  165. /*
  166. * Called with preeemption disabled.
  167. */
  168. static inline void
  169. send_IPI_allbutself (int op)
  170. {
  171. unsigned int i;
  172. for_each_online_cpu(i) {
  173. if (i != smp_processor_id())
  174. send_IPI_single(i, op);
  175. }
  176. }
  177. /*
  178. * Called with preeemption disabled.
  179. */
  180. static inline void
  181. send_IPI_all (int op)
  182. {
  183. int i;
  184. for_each_online_cpu(i) {
  185. send_IPI_single(i, op);
  186. }
  187. }
  188. /*
  189. * Called with preeemption disabled.
  190. */
  191. static inline void
  192. send_IPI_self (int op)
  193. {
  194. send_IPI_single(smp_processor_id(), op);
  195. }
  196. #ifdef CONFIG_KEXEC
  197. void
  198. kdump_smp_send_stop(void)
  199. {
  200. send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
  201. }
  202. void
  203. kdump_smp_send_init(void)
  204. {
  205. unsigned int cpu, self_cpu;
  206. self_cpu = smp_processor_id();
  207. for_each_online_cpu(cpu) {
  208. if (cpu != self_cpu) {
  209. if(kdump_status[cpu] == 0)
  210. platform_send_ipi(cpu, 0, IA64_IPI_DM_INIT, 0);
  211. }
  212. }
  213. }
  214. #endif
  215. /*
  216. * Called with preeemption disabled.
  217. */
  218. void
  219. smp_send_reschedule (int cpu)
  220. {
  221. platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
  222. }
  223. void
  224. smp_flush_tlb_all (void)
  225. {
  226. on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1, 1);
  227. }
  228. void
  229. smp_flush_tlb_mm (struct mm_struct *mm)
  230. {
  231. preempt_disable();
  232. /* this happens for the common case of a single-threaded fork(): */
  233. if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
  234. {
  235. local_finish_flush_tlb_mm(mm);
  236. preempt_enable();
  237. return;
  238. }
  239. preempt_enable();
  240. /*
  241. * We could optimize this further by using mm->cpu_vm_mask to track which CPUs
  242. * have been running in the address space. It's not clear that this is worth the
  243. * trouble though: to avoid races, we have to raise the IPI on the target CPU
  244. * anyhow, and once a CPU is interrupted, the cost of local_flush_tlb_all() is
  245. * rather trivial.
  246. */
  247. on_each_cpu((void (*)(void *))local_finish_flush_tlb_mm, mm, 1, 1);
  248. }
  249. /*
  250. * Run a function on another CPU
  251. * <func> The function to run. This must be fast and non-blocking.
  252. * <info> An arbitrary pointer to pass to the function.
  253. * <nonatomic> Currently unused.
  254. * <wait> If true, wait until function has completed on other CPUs.
  255. * [RETURNS] 0 on success, else a negative status code.
  256. *
  257. * Does not return until the remote CPU is nearly ready to execute <func>
  258. * or is or has executed.
  259. */
  260. int
  261. smp_call_function_single (int cpuid, void (*func) (void *info), void *info, int nonatomic,
  262. int wait)
  263. {
  264. struct call_data_struct data;
  265. int cpus = 1;
  266. int me = get_cpu(); /* prevent preemption and reschedule on another processor */
  267. if (cpuid == me) {
  268. printk(KERN_INFO "%s: trying to call self\n", __FUNCTION__);
  269. put_cpu();
  270. return -EBUSY;
  271. }
  272. data.func = func;
  273. data.info = info;
  274. atomic_set(&data.started, 0);
  275. data.wait = wait;
  276. if (wait)
  277. atomic_set(&data.finished, 0);
  278. spin_lock_bh(&call_lock);
  279. call_data = &data;
  280. mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
  281. send_IPI_single(cpuid, IPI_CALL_FUNC);
  282. /* Wait for response */
  283. while (atomic_read(&data.started) != cpus)
  284. cpu_relax();
  285. if (wait)
  286. while (atomic_read(&data.finished) != cpus)
  287. cpu_relax();
  288. call_data = NULL;
  289. spin_unlock_bh(&call_lock);
  290. put_cpu();
  291. return 0;
  292. }
  293. EXPORT_SYMBOL(smp_call_function_single);
  294. /*
  295. * this function sends a 'generic call function' IPI to all other CPUs
  296. * in the system.
  297. */
  298. /*
  299. * [SUMMARY] Run a function on all other CPUs.
  300. * <func> The function to run. This must be fast and non-blocking.
  301. * <info> An arbitrary pointer to pass to the function.
  302. * <nonatomic> currently unused.
  303. * <wait> If true, wait (atomically) until function has completed on other CPUs.
  304. * [RETURNS] 0 on success, else a negative status code.
  305. *
  306. * Does not return until remote CPUs are nearly ready to execute <func> or are or have
  307. * executed.
  308. *
  309. * You must not call this function with disabled interrupts or from a
  310. * hardware interrupt handler or from a bottom half handler.
  311. */
  312. int
  313. smp_call_function (void (*func) (void *info), void *info, int nonatomic, int wait)
  314. {
  315. struct call_data_struct data;
  316. int cpus;
  317. spin_lock(&call_lock);
  318. cpus = num_online_cpus() - 1;
  319. if (!cpus) {
  320. spin_unlock(&call_lock);
  321. return 0;
  322. }
  323. /* Can deadlock when called with interrupts disabled */
  324. WARN_ON(irqs_disabled());
  325. data.func = func;
  326. data.info = info;
  327. atomic_set(&data.started, 0);
  328. data.wait = wait;
  329. if (wait)
  330. atomic_set(&data.finished, 0);
  331. call_data = &data;
  332. mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
  333. send_IPI_allbutself(IPI_CALL_FUNC);
  334. /* Wait for response */
  335. while (atomic_read(&data.started) != cpus)
  336. cpu_relax();
  337. if (wait)
  338. while (atomic_read(&data.finished) != cpus)
  339. cpu_relax();
  340. call_data = NULL;
  341. spin_unlock(&call_lock);
  342. return 0;
  343. }
  344. EXPORT_SYMBOL(smp_call_function);
  345. /*
  346. * this function calls the 'stop' function on all other CPUs in the system.
  347. */
  348. void
  349. smp_send_stop (void)
  350. {
  351. send_IPI_allbutself(IPI_CPU_STOP);
  352. }
  353. int __init
  354. setup_profiling_timer (unsigned int multiplier)
  355. {
  356. return -EINVAL;
  357. }