smp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. struct thread_info *current_set[NR_CPUS];
  152. static void __devinit smp_store_cpu_info(int id)
  153. {
  154. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  155. }
  156. static void __init smp_create_idle(unsigned int cpu)
  157. {
  158. struct task_struct *p;
  159. /* create a process for the processor */
  160. p = fork_idle(cpu);
  161. if (IS_ERR(p))
  162. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  163. #ifdef CONFIG_PPC64
  164. paca[cpu].__current = p;
  165. paca[cpu].kstack = (unsigned long) task_thread_info(p)
  166. + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  167. #endif
  168. current_set[cpu] = task_thread_info(p);
  169. task_thread_info(p)->cpu = cpu;
  170. }
  171. void __init smp_prepare_cpus(unsigned int max_cpus)
  172. {
  173. unsigned int cpu;
  174. DBG("smp_prepare_cpus\n");
  175. /*
  176. * setup_cpu may need to be called on the boot cpu. We havent
  177. * spun any cpus up but lets be paranoid.
  178. */
  179. BUG_ON(boot_cpuid != smp_processor_id());
  180. /* Fixup boot cpu */
  181. smp_store_cpu_info(boot_cpuid);
  182. cpu_callin_map[boot_cpuid] = 1;
  183. if (smp_ops)
  184. max_cpus = smp_ops->probe();
  185. else
  186. max_cpus = 1;
  187. smp_space_timers(max_cpus);
  188. for_each_possible_cpu(cpu)
  189. if (cpu != boot_cpuid)
  190. smp_create_idle(cpu);
  191. }
  192. void __devinit smp_prepare_boot_cpu(void)
  193. {
  194. BUG_ON(smp_processor_id() != boot_cpuid);
  195. cpu_set(boot_cpuid, cpu_online_map);
  196. #ifdef CONFIG_PPC64
  197. paca[boot_cpuid].__current = current;
  198. #endif
  199. current_set[boot_cpuid] = task_thread_info(current);
  200. }
  201. #ifdef CONFIG_HOTPLUG_CPU
  202. /* State of each CPU during hotplug phases */
  203. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  204. int generic_cpu_disable(void)
  205. {
  206. unsigned int cpu = smp_processor_id();
  207. if (cpu == boot_cpuid)
  208. return -EBUSY;
  209. cpu_clear(cpu, cpu_online_map);
  210. #ifdef CONFIG_PPC64
  211. vdso_data->processorCount--;
  212. fixup_irqs(cpu_online_map);
  213. #endif
  214. return 0;
  215. }
  216. int generic_cpu_enable(unsigned int cpu)
  217. {
  218. /* Do the normal bootup if we haven't
  219. * already bootstrapped. */
  220. if (system_state != SYSTEM_RUNNING)
  221. return -ENOSYS;
  222. /* get the target out of it's holding state */
  223. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  224. smp_wmb();
  225. while (!cpu_online(cpu))
  226. cpu_relax();
  227. #ifdef CONFIG_PPC64
  228. fixup_irqs(cpu_online_map);
  229. /* counter the irq disable in fixup_irqs */
  230. local_irq_enable();
  231. #endif
  232. return 0;
  233. }
  234. void generic_cpu_die(unsigned int cpu)
  235. {
  236. int i;
  237. for (i = 0; i < 100; i++) {
  238. smp_rmb();
  239. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  240. return;
  241. msleep(100);
  242. }
  243. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  244. }
  245. void generic_mach_cpu_die(void)
  246. {
  247. unsigned int cpu;
  248. local_irq_disable();
  249. cpu = smp_processor_id();
  250. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  251. __get_cpu_var(cpu_state) = CPU_DEAD;
  252. smp_wmb();
  253. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  254. cpu_relax();
  255. cpu_set(cpu, cpu_online_map);
  256. local_irq_enable();
  257. }
  258. #endif
  259. static int __devinit cpu_enable(unsigned int cpu)
  260. {
  261. if (smp_ops && smp_ops->cpu_enable)
  262. return smp_ops->cpu_enable(cpu);
  263. return -ENOSYS;
  264. }
  265. int __cpuinit __cpu_up(unsigned int cpu)
  266. {
  267. int c;
  268. secondary_ti = current_set[cpu];
  269. if (!cpu_enable(cpu))
  270. return 0;
  271. if (smp_ops == NULL ||
  272. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  273. return -EINVAL;
  274. /* Make sure callin-map entry is 0 (can be leftover a CPU
  275. * hotplug
  276. */
  277. cpu_callin_map[cpu] = 0;
  278. /* The information for processor bringup must
  279. * be written out to main store before we release
  280. * the processor.
  281. */
  282. smp_mb();
  283. /* wake up cpus */
  284. DBG("smp: kicking cpu %d\n", cpu);
  285. smp_ops->kick_cpu(cpu);
  286. /*
  287. * wait to see if the cpu made a callin (is actually up).
  288. * use this value that I found through experimentation.
  289. * -- Cort
  290. */
  291. if (system_state < SYSTEM_RUNNING)
  292. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  293. udelay(100);
  294. #ifdef CONFIG_HOTPLUG_CPU
  295. else
  296. /*
  297. * CPUs can take much longer to come up in the
  298. * hotplug case. Wait five seconds.
  299. */
  300. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  301. msleep(200);
  302. }
  303. #endif
  304. if (!cpu_callin_map[cpu]) {
  305. printk("Processor %u is stuck.\n", cpu);
  306. return -ENOENT;
  307. }
  308. printk("Processor %u found.\n", cpu);
  309. if (smp_ops->give_timebase)
  310. smp_ops->give_timebase();
  311. /* Wait until cpu puts itself in the online map */
  312. while (!cpu_online(cpu))
  313. cpu_relax();
  314. return 0;
  315. }
  316. /* Activate a secondary processor. */
  317. int __devinit start_secondary(void *unused)
  318. {
  319. unsigned int cpu = smp_processor_id();
  320. atomic_inc(&init_mm.mm_count);
  321. current->active_mm = &init_mm;
  322. smp_store_cpu_info(cpu);
  323. set_dec(tb_ticks_per_jiffy);
  324. preempt_disable();
  325. cpu_callin_map[cpu] = 1;
  326. smp_ops->setup_cpu(cpu);
  327. if (smp_ops->take_timebase)
  328. smp_ops->take_timebase();
  329. if (system_state > SYSTEM_BOOTING)
  330. snapshot_timebase();
  331. secondary_cpu_time_init();
  332. ipi_call_lock();
  333. cpu_set(cpu, cpu_online_map);
  334. ipi_call_unlock();
  335. local_irq_enable();
  336. cpu_idle();
  337. return 0;
  338. }
  339. int setup_profiling_timer(unsigned int multiplier)
  340. {
  341. return 0;
  342. }
  343. void __init smp_cpus_done(unsigned int max_cpus)
  344. {
  345. cpumask_t old_mask;
  346. /* We want the setup_cpu() here to be called from CPU 0, but our
  347. * init thread may have been "borrowed" by another CPU in the meantime
  348. * se we pin us down to CPU 0 for a short while
  349. */
  350. old_mask = current->cpus_allowed;
  351. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  352. if (smp_ops)
  353. smp_ops->setup_cpu(boot_cpuid);
  354. set_cpus_allowed(current, old_mask);
  355. snapshot_timebases();
  356. dump_numa_cpu_topology();
  357. }
  358. #ifdef CONFIG_HOTPLUG_CPU
  359. int __cpu_disable(void)
  360. {
  361. if (smp_ops->cpu_disable)
  362. return smp_ops->cpu_disable();
  363. return -ENOSYS;
  364. }
  365. void __cpu_die(unsigned int cpu)
  366. {
  367. if (smp_ops->cpu_die)
  368. smp_ops->cpu_die(cpu);
  369. }
  370. #endif