smp.c 12 KB

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