smp.c 13 KB

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