smp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * SMP support for ppc.
  3. *
  4. * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
  5. * deal of code from the sparc and intel versions.
  6. *
  7. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  8. *
  9. * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
  10. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #undef DEBUG
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/smp.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/cache.h>
  27. #include <linux/err.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/cpu.h>
  30. #include <linux/notifier.h>
  31. #include <linux/topology.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/atomic.h>
  34. #include <asm/irq.h>
  35. #include <asm/page.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/prom.h>
  38. #include <asm/smp.h>
  39. #include <asm/time.h>
  40. #include <asm/machdep.h>
  41. #include <asm/cputable.h>
  42. #include <asm/system.h>
  43. #include <asm/mpic.h>
  44. #include <asm/vdso_datapage.h>
  45. #ifdef CONFIG_PPC64
  46. #include <asm/paca.h>
  47. #endif
  48. #ifdef DEBUG
  49. #include <asm/udbg.h>
  50. #define DBG(fmt...) udbg_printf(fmt)
  51. #else
  52. #define DBG(fmt...)
  53. #endif
  54. int smp_hw_index[NR_CPUS];
  55. struct thread_info *secondary_ti;
  56. cpumask_t cpu_possible_map = CPU_MASK_NONE;
  57. cpumask_t cpu_online_map = CPU_MASK_NONE;
  58. DEFINE_PER_CPU(cpumask_t, cpu_sibling_map) = CPU_MASK_NONE;
  59. EXPORT_SYMBOL(cpu_online_map);
  60. EXPORT_SYMBOL(cpu_possible_map);
  61. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  62. /* SMP operations for this machine */
  63. struct smp_ops_t *smp_ops;
  64. static volatile unsigned int cpu_callin_map[NR_CPUS];
  65. int smt_enabled_at_boot = 1;
  66. static void (*crash_ipi_function_ptr)(struct pt_regs *) = NULL;
  67. #ifdef CONFIG_PPC64
  68. void __devinit smp_generic_kick_cpu(int nr)
  69. {
  70. BUG_ON(nr < 0 || nr >= NR_CPUS);
  71. /*
  72. * The processor is currently spinning, waiting for the
  73. * cpu_start field to become non-zero After we set cpu_start,
  74. * the processor will continue on to secondary_start
  75. */
  76. paca[nr].cpu_start = 1;
  77. smp_mb();
  78. }
  79. #endif
  80. void smp_message_recv(int msg)
  81. {
  82. switch(msg) {
  83. case PPC_MSG_CALL_FUNCTION:
  84. generic_smp_call_function_interrupt();
  85. break;
  86. case PPC_MSG_RESCHEDULE:
  87. /* XXX Do we have to do this? */
  88. set_need_resched();
  89. break;
  90. case PPC_MSG_CALL_FUNC_SINGLE:
  91. generic_smp_call_function_single_interrupt();
  92. break;
  93. case PPC_MSG_DEBUGGER_BREAK:
  94. if (crash_ipi_function_ptr) {
  95. crash_ipi_function_ptr(get_irq_regs());
  96. break;
  97. }
  98. #ifdef CONFIG_DEBUGGER
  99. debugger_ipi(get_irq_regs());
  100. break;
  101. #endif /* CONFIG_DEBUGGER */
  102. /* FALLTHROUGH */
  103. default:
  104. printk("SMP %d: smp_message_recv(): unknown msg %d\n",
  105. smp_processor_id(), msg);
  106. break;
  107. }
  108. }
  109. void smp_send_reschedule(int cpu)
  110. {
  111. if (likely(smp_ops))
  112. smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
  113. }
  114. void arch_send_call_function_single_ipi(int cpu)
  115. {
  116. smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNC_SINGLE);
  117. }
  118. void arch_send_call_function_ipi(cpumask_t mask)
  119. {
  120. unsigned int cpu;
  121. for_each_cpu_mask(cpu, mask)
  122. smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  123. }
  124. #ifdef CONFIG_DEBUGGER
  125. void smp_send_debugger_break(int cpu)
  126. {
  127. if (likely(smp_ops))
  128. smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
  129. }
  130. #endif
  131. #ifdef CONFIG_KEXEC
  132. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  133. {
  134. crash_ipi_function_ptr = crash_ipi_callback;
  135. if (crash_ipi_callback && smp_ops) {
  136. mb();
  137. smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_DEBUGGER_BREAK);
  138. }
  139. }
  140. #endif
  141. static void stop_this_cpu(void *dummy)
  142. {
  143. local_irq_disable();
  144. while (1)
  145. ;
  146. }
  147. void smp_send_stop(void)
  148. {
  149. smp_call_function(stop_this_cpu, NULL, 0);
  150. }
  151. extern struct gettimeofday_struct do_gtod;
  152. struct thread_info *current_set[NR_CPUS];
  153. DECLARE_PER_CPU(unsigned int, pvr);
  154. static void __devinit smp_store_cpu_info(int id)
  155. {
  156. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  157. }
  158. static void __init smp_create_idle(unsigned int cpu)
  159. {
  160. struct task_struct *p;
  161. /* create a process for the processor */
  162. p = fork_idle(cpu);
  163. if (IS_ERR(p))
  164. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  165. #ifdef CONFIG_PPC64
  166. paca[cpu].__current = p;
  167. paca[cpu].kstack = (unsigned long) task_thread_info(p)
  168. + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  169. #endif
  170. current_set[cpu] = task_thread_info(p);
  171. task_thread_info(p)->cpu = cpu;
  172. }
  173. void __init smp_prepare_cpus(unsigned int max_cpus)
  174. {
  175. unsigned int cpu;
  176. DBG("smp_prepare_cpus\n");
  177. /*
  178. * setup_cpu may need to be called on the boot cpu. We havent
  179. * spun any cpus up but lets be paranoid.
  180. */
  181. BUG_ON(boot_cpuid != smp_processor_id());
  182. /* Fixup boot cpu */
  183. smp_store_cpu_info(boot_cpuid);
  184. cpu_callin_map[boot_cpuid] = 1;
  185. if (smp_ops)
  186. max_cpus = smp_ops->probe();
  187. else
  188. max_cpus = 1;
  189. smp_space_timers(max_cpus);
  190. for_each_possible_cpu(cpu)
  191. if (cpu != boot_cpuid)
  192. smp_create_idle(cpu);
  193. }
  194. void __devinit smp_prepare_boot_cpu(void)
  195. {
  196. BUG_ON(smp_processor_id() != boot_cpuid);
  197. cpu_set(boot_cpuid, cpu_online_map);
  198. #ifdef CONFIG_PPC64
  199. paca[boot_cpuid].__current = current;
  200. #endif
  201. current_set[boot_cpuid] = task_thread_info(current);
  202. }
  203. #ifdef CONFIG_HOTPLUG_CPU
  204. /* State of each CPU during hotplug phases */
  205. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  206. int generic_cpu_disable(void)
  207. {
  208. unsigned int cpu = smp_processor_id();
  209. if (cpu == boot_cpuid)
  210. return -EBUSY;
  211. cpu_clear(cpu, cpu_online_map);
  212. #ifdef CONFIG_PPC64
  213. vdso_data->processorCount--;
  214. fixup_irqs(cpu_online_map);
  215. #endif
  216. return 0;
  217. }
  218. int generic_cpu_enable(unsigned int cpu)
  219. {
  220. /* Do the normal bootup if we haven't
  221. * already bootstrapped. */
  222. if (system_state != SYSTEM_RUNNING)
  223. return -ENOSYS;
  224. /* get the target out of it's holding state */
  225. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  226. smp_wmb();
  227. while (!cpu_online(cpu))
  228. cpu_relax();
  229. #ifdef CONFIG_PPC64
  230. fixup_irqs(cpu_online_map);
  231. /* counter the irq disable in fixup_irqs */
  232. local_irq_enable();
  233. #endif
  234. return 0;
  235. }
  236. void generic_cpu_die(unsigned int cpu)
  237. {
  238. int i;
  239. for (i = 0; i < 100; i++) {
  240. smp_rmb();
  241. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  242. return;
  243. msleep(100);
  244. }
  245. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  246. }
  247. void generic_mach_cpu_die(void)
  248. {
  249. unsigned int cpu;
  250. local_irq_disable();
  251. cpu = smp_processor_id();
  252. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  253. __get_cpu_var(cpu_state) = CPU_DEAD;
  254. smp_wmb();
  255. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  256. cpu_relax();
  257. cpu_set(cpu, cpu_online_map);
  258. local_irq_enable();
  259. }
  260. #endif
  261. static int __devinit cpu_enable(unsigned int cpu)
  262. {
  263. if (smp_ops && smp_ops->cpu_enable)
  264. return smp_ops->cpu_enable(cpu);
  265. return -ENOSYS;
  266. }
  267. int __cpuinit __cpu_up(unsigned int cpu)
  268. {
  269. int c;
  270. secondary_ti = current_set[cpu];
  271. if (!cpu_enable(cpu))
  272. return 0;
  273. if (smp_ops == NULL ||
  274. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  275. return -EINVAL;
  276. /* Make sure callin-map entry is 0 (can be leftover a CPU
  277. * hotplug
  278. */
  279. cpu_callin_map[cpu] = 0;
  280. /* The information for processor bringup must
  281. * be written out to main store before we release
  282. * the processor.
  283. */
  284. smp_mb();
  285. /* wake up cpus */
  286. DBG("smp: kicking cpu %d\n", cpu);
  287. smp_ops->kick_cpu(cpu);
  288. /*
  289. * wait to see if the cpu made a callin (is actually up).
  290. * use this value that I found through experimentation.
  291. * -- Cort
  292. */
  293. if (system_state < SYSTEM_RUNNING)
  294. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  295. udelay(100);
  296. #ifdef CONFIG_HOTPLUG_CPU
  297. else
  298. /*
  299. * CPUs can take much longer to come up in the
  300. * hotplug case. Wait five seconds.
  301. */
  302. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  303. msleep(200);
  304. }
  305. #endif
  306. if (!cpu_callin_map[cpu]) {
  307. printk("Processor %u is stuck.\n", cpu);
  308. return -ENOENT;
  309. }
  310. printk("Processor %u found.\n", cpu);
  311. if (smp_ops->give_timebase)
  312. smp_ops->give_timebase();
  313. /* Wait until cpu puts itself in the online map */
  314. while (!cpu_online(cpu))
  315. cpu_relax();
  316. return 0;
  317. }
  318. /* Activate a secondary processor. */
  319. int __devinit start_secondary(void *unused)
  320. {
  321. unsigned int cpu = smp_processor_id();
  322. atomic_inc(&init_mm.mm_count);
  323. current->active_mm = &init_mm;
  324. smp_store_cpu_info(cpu);
  325. set_dec(tb_ticks_per_jiffy);
  326. preempt_disable();
  327. cpu_callin_map[cpu] = 1;
  328. smp_ops->setup_cpu(cpu);
  329. if (smp_ops->take_timebase)
  330. smp_ops->take_timebase();
  331. if (system_state > SYSTEM_BOOTING)
  332. snapshot_timebase();
  333. secondary_cpu_time_init();
  334. ipi_call_lock();
  335. cpu_set(cpu, cpu_online_map);
  336. ipi_call_unlock();
  337. local_irq_enable();
  338. cpu_idle();
  339. return 0;
  340. }
  341. int setup_profiling_timer(unsigned int multiplier)
  342. {
  343. return 0;
  344. }
  345. void __init smp_cpus_done(unsigned int max_cpus)
  346. {
  347. cpumask_t old_mask;
  348. /* We want the setup_cpu() here to be called from CPU 0, but our
  349. * init thread may have been "borrowed" by another CPU in the meantime
  350. * se we pin us down to CPU 0 for a short while
  351. */
  352. old_mask = current->cpus_allowed;
  353. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  354. if (smp_ops)
  355. smp_ops->setup_cpu(boot_cpuid);
  356. set_cpus_allowed(current, old_mask);
  357. snapshot_timebases();
  358. dump_numa_cpu_topology();
  359. }
  360. #ifdef CONFIG_HOTPLUG_CPU
  361. int __cpu_disable(void)
  362. {
  363. if (smp_ops->cpu_disable)
  364. return smp_ops->cpu_disable();
  365. return -ENOSYS;
  366. }
  367. void __cpu_die(unsigned int cpu)
  368. {
  369. if (smp_ops->cpu_die)
  370. smp_ops->cpu_die(cpu);
  371. }
  372. #endif