smp.c 13 KB

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