smp.c 12 KB

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