smp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * linux/arch/arm/kernel/smp.c
  3. *
  4. * Copyright (C) 2002 ARM Limited, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/config.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/cache.h>
  17. #include <linux/profile.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/cpu.h>
  21. #include <linux/smp.h>
  22. #include <linux/seq_file.h>
  23. #include <asm/atomic.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/cpu.h>
  26. #include <asm/processor.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/ptrace.h>
  29. /*
  30. * bitmask of present and online CPUs.
  31. * The present bitmask indicates that the CPU is physically present.
  32. * The online bitmask indicates that the CPU is up and running.
  33. */
  34. cpumask_t cpu_present_mask;
  35. cpumask_t cpu_online_map;
  36. /*
  37. * structures for inter-processor calls
  38. * - A collection of single bit ipi messages.
  39. */
  40. struct ipi_data {
  41. spinlock_t lock;
  42. unsigned long ipi_count;
  43. unsigned long bits;
  44. };
  45. static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
  46. .lock = SPIN_LOCK_UNLOCKED,
  47. };
  48. enum ipi_msg_type {
  49. IPI_TIMER,
  50. IPI_RESCHEDULE,
  51. IPI_CALL_FUNC,
  52. IPI_CPU_STOP,
  53. };
  54. struct smp_call_struct {
  55. void (*func)(void *info);
  56. void *info;
  57. int wait;
  58. cpumask_t pending;
  59. cpumask_t unfinished;
  60. };
  61. static struct smp_call_struct * volatile smp_call_function_data;
  62. static DEFINE_SPINLOCK(smp_call_function_lock);
  63. int __init __cpu_up(unsigned int cpu)
  64. {
  65. struct task_struct *idle;
  66. int ret;
  67. /*
  68. * Spawn a new process manually. Grab a pointer to
  69. * its task struct so we can mess with it
  70. */
  71. idle = fork_idle(cpu);
  72. if (IS_ERR(idle)) {
  73. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  74. return PTR_ERR(idle);
  75. }
  76. /*
  77. * Now bring the CPU into our world.
  78. */
  79. ret = boot_secondary(cpu, idle);
  80. if (ret) {
  81. printk(KERN_CRIT "cpu_up: processor %d failed to boot\n", cpu);
  82. /*
  83. * FIXME: We need to clean up the new idle thread. --rmk
  84. */
  85. }
  86. return ret;
  87. }
  88. /*
  89. * Called by both boot and secondaries to move global data into
  90. * per-processor storage.
  91. */
  92. void __init smp_store_cpu_info(unsigned int cpuid)
  93. {
  94. struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
  95. cpu_info->loops_per_jiffy = loops_per_jiffy;
  96. }
  97. void __init smp_cpus_done(unsigned int max_cpus)
  98. {
  99. int cpu;
  100. unsigned long bogosum = 0;
  101. for_each_online_cpu(cpu)
  102. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  103. printk(KERN_INFO "SMP: Total of %d processors activated "
  104. "(%lu.%02lu BogoMIPS).\n",
  105. num_online_cpus(),
  106. bogosum / (500000/HZ),
  107. (bogosum / (5000/HZ)) % 100);
  108. }
  109. void __init smp_prepare_boot_cpu(void)
  110. {
  111. unsigned int cpu = smp_processor_id();
  112. cpu_set(cpu, cpu_present_mask);
  113. cpu_set(cpu, cpu_online_map);
  114. }
  115. static void send_ipi_message(cpumask_t callmap, enum ipi_msg_type msg)
  116. {
  117. unsigned long flags;
  118. unsigned int cpu;
  119. local_irq_save(flags);
  120. for_each_cpu_mask(cpu, callmap) {
  121. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  122. spin_lock(&ipi->lock);
  123. ipi->bits |= 1 << msg;
  124. spin_unlock(&ipi->lock);
  125. }
  126. /*
  127. * Call the platform specific cross-CPU call function.
  128. */
  129. smp_cross_call(callmap);
  130. local_irq_restore(flags);
  131. }
  132. /*
  133. * You must not call this function with disabled interrupts, from a
  134. * hardware interrupt handler, nor from a bottom half handler.
  135. */
  136. int smp_call_function_on_cpu(void (*func)(void *info), void *info, int retry,
  137. int wait, cpumask_t callmap)
  138. {
  139. struct smp_call_struct data;
  140. unsigned long timeout;
  141. int ret = 0;
  142. data.func = func;
  143. data.info = info;
  144. data.wait = wait;
  145. cpu_clear(smp_processor_id(), callmap);
  146. if (cpus_empty(callmap))
  147. goto out;
  148. data.pending = callmap;
  149. if (wait)
  150. data.unfinished = callmap;
  151. /*
  152. * try to get the mutex on smp_call_function_data
  153. */
  154. spin_lock(&smp_call_function_lock);
  155. smp_call_function_data = &data;
  156. send_ipi_message(callmap, IPI_CALL_FUNC);
  157. timeout = jiffies + HZ;
  158. while (!cpus_empty(data.pending) && time_before(jiffies, timeout))
  159. barrier();
  160. /*
  161. * did we time out?
  162. */
  163. if (!cpus_empty(data.pending)) {
  164. /*
  165. * this may be causing our panic - report it
  166. */
  167. printk(KERN_CRIT
  168. "CPU%u: smp_call_function timeout for %p(%p)\n"
  169. " callmap %lx pending %lx, %swait\n",
  170. smp_processor_id(), func, info, callmap, data.pending,
  171. wait ? "" : "no ");
  172. /*
  173. * TRACE
  174. */
  175. timeout = jiffies + (5 * HZ);
  176. while (!cpus_empty(data.pending) && time_before(jiffies, timeout))
  177. barrier();
  178. if (cpus_empty(data.pending))
  179. printk(KERN_CRIT " RESOLVED\n");
  180. else
  181. printk(KERN_CRIT " STILL STUCK\n");
  182. }
  183. /*
  184. * whatever happened, we're done with the data, so release it
  185. */
  186. smp_call_function_data = NULL;
  187. spin_unlock(&smp_call_function_lock);
  188. if (!cpus_empty(data.pending)) {
  189. ret = -ETIMEDOUT;
  190. goto out;
  191. }
  192. if (wait)
  193. while (!cpus_empty(data.unfinished))
  194. barrier();
  195. out:
  196. return 0;
  197. }
  198. int smp_call_function(void (*func)(void *info), void *info, int retry,
  199. int wait)
  200. {
  201. return smp_call_function_on_cpu(func, info, retry, wait,
  202. cpu_online_map);
  203. }
  204. void show_ipi_list(struct seq_file *p)
  205. {
  206. unsigned int cpu;
  207. seq_puts(p, "IPI:");
  208. for_each_online_cpu(cpu)
  209. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  210. seq_putc(p, '\n');
  211. }
  212. static void ipi_timer(struct pt_regs *regs)
  213. {
  214. int user = user_mode(regs);
  215. irq_enter();
  216. profile_tick(CPU_PROFILING, regs);
  217. update_process_times(user);
  218. irq_exit();
  219. }
  220. /*
  221. * ipi_call_function - handle IPI from smp_call_function()
  222. *
  223. * Note that we copy data out of the cross-call structure and then
  224. * let the caller know that we're here and have done with their data
  225. */
  226. static void ipi_call_function(unsigned int cpu)
  227. {
  228. struct smp_call_struct *data = smp_call_function_data;
  229. void (*func)(void *info) = data->func;
  230. void *info = data->info;
  231. int wait = data->wait;
  232. cpu_clear(cpu, data->pending);
  233. func(info);
  234. if (wait)
  235. cpu_clear(cpu, data->unfinished);
  236. }
  237. static DEFINE_SPINLOCK(stop_lock);
  238. /*
  239. * ipi_cpu_stop - handle IPI from smp_send_stop()
  240. */
  241. static void ipi_cpu_stop(unsigned int cpu)
  242. {
  243. spin_lock(&stop_lock);
  244. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  245. dump_stack();
  246. spin_unlock(&stop_lock);
  247. cpu_clear(cpu, cpu_online_map);
  248. local_fiq_disable();
  249. local_irq_disable();
  250. while (1)
  251. cpu_relax();
  252. }
  253. /*
  254. * Main handler for inter-processor interrupts
  255. *
  256. * For ARM, the ipimask now only identifies a single
  257. * category of IPI (Bit 1 IPIs have been replaced by a
  258. * different mechanism):
  259. *
  260. * Bit 0 - Inter-processor function call
  261. */
  262. void do_IPI(struct pt_regs *regs)
  263. {
  264. unsigned int cpu = smp_processor_id();
  265. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  266. ipi->ipi_count++;
  267. for (;;) {
  268. unsigned long msgs;
  269. spin_lock(&ipi->lock);
  270. msgs = ipi->bits;
  271. ipi->bits = 0;
  272. spin_unlock(&ipi->lock);
  273. if (!msgs)
  274. break;
  275. do {
  276. unsigned nextmsg;
  277. nextmsg = msgs & -msgs;
  278. msgs &= ~nextmsg;
  279. nextmsg = ffz(~nextmsg);
  280. switch (nextmsg) {
  281. case IPI_TIMER:
  282. ipi_timer(regs);
  283. break;
  284. case IPI_RESCHEDULE:
  285. /*
  286. * nothing more to do - eveything is
  287. * done on the interrupt return path
  288. */
  289. break;
  290. case IPI_CALL_FUNC:
  291. ipi_call_function(cpu);
  292. break;
  293. case IPI_CPU_STOP:
  294. ipi_cpu_stop(cpu);
  295. break;
  296. default:
  297. printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
  298. cpu, nextmsg);
  299. break;
  300. }
  301. } while (msgs);
  302. }
  303. }
  304. void smp_send_reschedule(int cpu)
  305. {
  306. send_ipi_message(cpumask_of_cpu(cpu), IPI_RESCHEDULE);
  307. }
  308. void smp_send_timer(void)
  309. {
  310. cpumask_t mask = cpu_online_map;
  311. cpu_clear(smp_processor_id(), mask);
  312. send_ipi_message(mask, IPI_TIMER);
  313. }
  314. void smp_send_stop(void)
  315. {
  316. cpumask_t mask = cpu_online_map;
  317. cpu_clear(smp_processor_id(), mask);
  318. send_ipi_message(mask, IPI_CPU_STOP);
  319. }
  320. /*
  321. * not supported here
  322. */
  323. int __init setup_profiling_timer(unsigned int multiplier)
  324. {
  325. return -EINVAL;
  326. }