smp.c 8.6 KB

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