smp.c 14 KB

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