smp.c 12 KB

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