smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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/config.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/smp.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/cache.h>
  28. #include <linux/err.h>
  29. #include <linux/sysdev.h>
  30. #include <linux/cpu.h>
  31. #include <linux/notifier.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. #ifdef CONFIG_PPC64
  45. #include <asm/paca.h>
  46. #endif
  47. int smp_hw_index[NR_CPUS];
  48. struct thread_info *secondary_ti;
  49. #ifdef DEBUG
  50. #define DBG(fmt...) udbg_printf(fmt)
  51. #else
  52. #define DBG(fmt...)
  53. #endif
  54. cpumask_t cpu_possible_map = CPU_MASK_NONE;
  55. cpumask_t cpu_online_map = CPU_MASK_NONE;
  56. cpumask_t cpu_sibling_map[NR_CPUS] = { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
  57. EXPORT_SYMBOL(cpu_online_map);
  58. EXPORT_SYMBOL(cpu_possible_map);
  59. /* SMP operations for this machine */
  60. struct smp_ops_t *smp_ops;
  61. static volatile unsigned int cpu_callin_map[NR_CPUS];
  62. void smp_call_function_interrupt(void);
  63. int smt_enabled_at_boot = 1;
  64. #ifdef CONFIG_MPIC
  65. int __init smp_mpic_probe(void)
  66. {
  67. int nr_cpus;
  68. DBG("smp_mpic_probe()...\n");
  69. nr_cpus = cpus_weight(cpu_possible_map);
  70. DBG("nr_cpus: %d\n", nr_cpus);
  71. if (nr_cpus > 1)
  72. mpic_request_ipis();
  73. return nr_cpus;
  74. }
  75. void __devinit smp_mpic_setup_cpu(int cpu)
  76. {
  77. mpic_setup_this_cpu();
  78. }
  79. #endif /* CONFIG_MPIC */
  80. #ifdef CONFIG_PPC64
  81. void __devinit smp_generic_kick_cpu(int nr)
  82. {
  83. BUG_ON(nr < 0 || nr >= NR_CPUS);
  84. /*
  85. * The processor is currently spinning, waiting for the
  86. * cpu_start field to become non-zero After we set cpu_start,
  87. * the processor will continue on to secondary_start
  88. */
  89. paca[nr].cpu_start = 1;
  90. smp_mb();
  91. }
  92. #endif
  93. void smp_message_recv(int msg, struct pt_regs *regs)
  94. {
  95. switch(msg) {
  96. case PPC_MSG_CALL_FUNCTION:
  97. smp_call_function_interrupt();
  98. break;
  99. case PPC_MSG_RESCHEDULE:
  100. /* XXX Do we have to do this? */
  101. set_need_resched();
  102. break;
  103. #ifdef CONFIG_DEBUGGER
  104. case PPC_MSG_DEBUGGER_BREAK:
  105. debugger_ipi(regs);
  106. break;
  107. #endif
  108. default:
  109. printk("SMP %d: smp_message_recv(): unknown msg %d\n",
  110. smp_processor_id(), msg);
  111. break;
  112. }
  113. }
  114. void smp_send_reschedule(int cpu)
  115. {
  116. smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
  117. }
  118. #ifdef CONFIG_DEBUGGER
  119. void smp_send_debugger_break(int cpu)
  120. {
  121. smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
  122. }
  123. #endif
  124. static void stop_this_cpu(void *dummy)
  125. {
  126. local_irq_disable();
  127. while (1)
  128. ;
  129. }
  130. void smp_send_stop(void)
  131. {
  132. smp_call_function(stop_this_cpu, NULL, 1, 0);
  133. }
  134. /*
  135. * Structure and data for smp_call_function(). This is designed to minimise
  136. * static memory requirements. It also looks cleaner.
  137. * Stolen from the i386 version.
  138. */
  139. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
  140. static struct call_data_struct {
  141. void (*func) (void *info);
  142. void *info;
  143. atomic_t started;
  144. atomic_t finished;
  145. int wait;
  146. } *call_data;
  147. /* delay of at least 8 seconds */
  148. #define SMP_CALL_TIMEOUT 8
  149. /*
  150. * This function sends a 'generic call function' IPI to all other CPUs
  151. * in the system.
  152. *
  153. * [SUMMARY] Run a function on all other CPUs.
  154. * <func> The function to run. This must be fast and non-blocking.
  155. * <info> An arbitrary pointer to pass to the function.
  156. * <nonatomic> currently unused.
  157. * <wait> If true, wait (atomically) until function has completed on other CPUs.
  158. * [RETURNS] 0 on success, else a negative status code. Does not return until
  159. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  160. *
  161. * You must not call this function with disabled interrupts or from a
  162. * hardware interrupt handler or from a bottom half handler.
  163. */
  164. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  165. int wait)
  166. {
  167. struct call_data_struct data;
  168. int ret = -1, cpus;
  169. u64 timeout;
  170. /* Can deadlock when called with interrupts disabled */
  171. WARN_ON(irqs_disabled());
  172. data.func = func;
  173. data.info = info;
  174. atomic_set(&data.started, 0);
  175. data.wait = wait;
  176. if (wait)
  177. atomic_set(&data.finished, 0);
  178. spin_lock(&call_lock);
  179. /* Must grab online cpu count with preempt disabled, otherwise
  180. * it can change. */
  181. cpus = num_online_cpus() - 1;
  182. if (!cpus) {
  183. ret = 0;
  184. goto out;
  185. }
  186. call_data = &data;
  187. smp_wmb();
  188. /* Send a message to all other CPUs and wait for them to respond */
  189. smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION);
  190. timeout = get_tb() + (u64) SMP_CALL_TIMEOUT * tb_ticks_per_sec;
  191. /* Wait for response */
  192. while (atomic_read(&data.started) != cpus) {
  193. HMT_low();
  194. if (get_tb() >= timeout) {
  195. printk("smp_call_function on cpu %d: other cpus not "
  196. "responding (%d)\n", smp_processor_id(),
  197. atomic_read(&data.started));
  198. debugger(NULL);
  199. goto out;
  200. }
  201. }
  202. if (wait) {
  203. while (atomic_read(&data.finished) != cpus) {
  204. HMT_low();
  205. if (get_tb() >= timeout) {
  206. printk("smp_call_function on cpu %d: other "
  207. "cpus not finishing (%d/%d)\n",
  208. smp_processor_id(),
  209. atomic_read(&data.finished),
  210. atomic_read(&data.started));
  211. debugger(NULL);
  212. goto out;
  213. }
  214. }
  215. }
  216. ret = 0;
  217. out:
  218. call_data = NULL;
  219. HMT_medium();
  220. spin_unlock(&call_lock);
  221. return ret;
  222. }
  223. EXPORT_SYMBOL(smp_call_function);
  224. void smp_call_function_interrupt(void)
  225. {
  226. void (*func) (void *info);
  227. void *info;
  228. int wait;
  229. /* call_data will be NULL if the sender timed out while
  230. * waiting on us to receive the call.
  231. */
  232. if (!call_data)
  233. return;
  234. func = call_data->func;
  235. info = call_data->info;
  236. wait = call_data->wait;
  237. if (!wait)
  238. smp_mb__before_atomic_inc();
  239. /*
  240. * Notify initiating CPU that I've grabbed the data and am
  241. * about to execute the function
  242. */
  243. atomic_inc(&call_data->started);
  244. /*
  245. * At this point the info structure may be out of scope unless wait==1
  246. */
  247. (*func)(info);
  248. if (wait) {
  249. smp_mb__before_atomic_inc();
  250. atomic_inc(&call_data->finished);
  251. }
  252. }
  253. extern struct gettimeofday_struct do_gtod;
  254. struct thread_info *current_set[NR_CPUS];
  255. DECLARE_PER_CPU(unsigned int, pvr);
  256. static void __devinit smp_store_cpu_info(int id)
  257. {
  258. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  259. }
  260. static void __init smp_create_idle(unsigned int cpu)
  261. {
  262. struct task_struct *p;
  263. /* create a process for the processor */
  264. p = fork_idle(cpu);
  265. if (IS_ERR(p))
  266. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  267. #ifdef CONFIG_PPC64
  268. paca[cpu].__current = p;
  269. #endif
  270. current_set[cpu] = p->thread_info;
  271. p->thread_info->cpu = cpu;
  272. }
  273. void __init smp_prepare_cpus(unsigned int max_cpus)
  274. {
  275. unsigned int cpu;
  276. DBG("smp_prepare_cpus\n");
  277. /*
  278. * setup_cpu may need to be called on the boot cpu. We havent
  279. * spun any cpus up but lets be paranoid.
  280. */
  281. BUG_ON(boot_cpuid != smp_processor_id());
  282. /* Fixup boot cpu */
  283. smp_store_cpu_info(boot_cpuid);
  284. cpu_callin_map[boot_cpuid] = 1;
  285. max_cpus = smp_ops->probe();
  286. smp_space_timers(max_cpus);
  287. for_each_cpu(cpu)
  288. if (cpu != boot_cpuid)
  289. smp_create_idle(cpu);
  290. }
  291. void __devinit smp_prepare_boot_cpu(void)
  292. {
  293. BUG_ON(smp_processor_id() != boot_cpuid);
  294. cpu_set(boot_cpuid, cpu_online_map);
  295. #ifdef CONFIG_PPC64
  296. paca[boot_cpuid].__current = current;
  297. #endif
  298. current_set[boot_cpuid] = current->thread_info;
  299. }
  300. #ifdef CONFIG_HOTPLUG_CPU
  301. /* State of each CPU during hotplug phases */
  302. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  303. int generic_cpu_disable(void)
  304. {
  305. unsigned int cpu = smp_processor_id();
  306. if (cpu == boot_cpuid)
  307. return -EBUSY;
  308. systemcfg->processorCount--;
  309. cpu_clear(cpu, cpu_online_map);
  310. fixup_irqs(cpu_online_map);
  311. return 0;
  312. }
  313. int generic_cpu_enable(unsigned int cpu)
  314. {
  315. /* Do the normal bootup if we haven't
  316. * already bootstrapped. */
  317. if (system_state != SYSTEM_RUNNING)
  318. return -ENOSYS;
  319. /* get the target out of it's holding state */
  320. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  321. smp_wmb();
  322. while (!cpu_online(cpu))
  323. cpu_relax();
  324. fixup_irqs(cpu_online_map);
  325. /* counter the irq disable in fixup_irqs */
  326. local_irq_enable();
  327. return 0;
  328. }
  329. void generic_cpu_die(unsigned int cpu)
  330. {
  331. int i;
  332. for (i = 0; i < 100; i++) {
  333. smp_rmb();
  334. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  335. return;
  336. msleep(100);
  337. }
  338. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  339. }
  340. void generic_mach_cpu_die(void)
  341. {
  342. unsigned int cpu;
  343. local_irq_disable();
  344. cpu = smp_processor_id();
  345. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  346. __get_cpu_var(cpu_state) = CPU_DEAD;
  347. smp_wmb();
  348. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  349. cpu_relax();
  350. flush_tlb_pending();
  351. cpu_set(cpu, cpu_online_map);
  352. local_irq_enable();
  353. }
  354. #endif
  355. static int __devinit cpu_enable(unsigned int cpu)
  356. {
  357. if (smp_ops->cpu_enable)
  358. return smp_ops->cpu_enable(cpu);
  359. return -ENOSYS;
  360. }
  361. int __devinit __cpu_up(unsigned int cpu)
  362. {
  363. int c;
  364. secondary_ti = current_set[cpu];
  365. if (!cpu_enable(cpu))
  366. return 0;
  367. if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu))
  368. return -EINVAL;
  369. #ifdef CONFIG_PPC64
  370. paca[cpu].default_decr = tb_ticks_per_jiffy;
  371. #endif
  372. /* Make sure callin-map entry is 0 (can be leftover a CPU
  373. * hotplug
  374. */
  375. cpu_callin_map[cpu] = 0;
  376. /* The information for processor bringup must
  377. * be written out to main store before we release
  378. * the processor.
  379. */
  380. smp_mb();
  381. /* wake up cpus */
  382. DBG("smp: kicking cpu %d\n", cpu);
  383. smp_ops->kick_cpu(cpu);
  384. /*
  385. * wait to see if the cpu made a callin (is actually up).
  386. * use this value that I found through experimentation.
  387. * -- Cort
  388. */
  389. if (system_state < SYSTEM_RUNNING)
  390. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  391. udelay(100);
  392. #ifdef CONFIG_HOTPLUG_CPU
  393. else
  394. /*
  395. * CPUs can take much longer to come up in the
  396. * hotplug case. Wait five seconds.
  397. */
  398. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  399. msleep(200);
  400. }
  401. #endif
  402. if (!cpu_callin_map[cpu]) {
  403. printk("Processor %u is stuck.\n", cpu);
  404. return -ENOENT;
  405. }
  406. printk("Processor %u found.\n", cpu);
  407. if (smp_ops->give_timebase)
  408. smp_ops->give_timebase();
  409. /* Wait until cpu puts itself in the online map */
  410. while (!cpu_online(cpu))
  411. cpu_relax();
  412. return 0;
  413. }
  414. /* Activate a secondary processor. */
  415. int __devinit start_secondary(void *unused)
  416. {
  417. unsigned int cpu = smp_processor_id();
  418. atomic_inc(&init_mm.mm_count);
  419. current->active_mm = &init_mm;
  420. smp_store_cpu_info(cpu);
  421. set_dec(tb_ticks_per_jiffy);
  422. cpu_callin_map[cpu] = 1;
  423. smp_ops->setup_cpu(cpu);
  424. if (smp_ops->take_timebase)
  425. smp_ops->take_timebase();
  426. spin_lock(&call_lock);
  427. cpu_set(cpu, cpu_online_map);
  428. spin_unlock(&call_lock);
  429. local_irq_enable();
  430. cpu_idle();
  431. return 0;
  432. }
  433. int setup_profiling_timer(unsigned int multiplier)
  434. {
  435. return 0;
  436. }
  437. void __init smp_cpus_done(unsigned int max_cpus)
  438. {
  439. cpumask_t old_mask;
  440. /* We want the setup_cpu() here to be called from CPU 0, but our
  441. * init thread may have been "borrowed" by another CPU in the meantime
  442. * se we pin us down to CPU 0 for a short while
  443. */
  444. old_mask = current->cpus_allowed;
  445. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  446. smp_ops->setup_cpu(boot_cpuid);
  447. set_cpus_allowed(current, old_mask);
  448. }
  449. #ifdef CONFIG_HOTPLUG_CPU
  450. int __cpu_disable(void)
  451. {
  452. if (smp_ops->cpu_disable)
  453. return smp_ops->cpu_disable();
  454. return -ENOSYS;
  455. }
  456. void __cpu_die(unsigned int cpu)
  457. {
  458. if (smp_ops->cpu_die)
  459. smp_ops->cpu_die(cpu);
  460. }
  461. #endif