smp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * SMP support for pSeries machines.
  3. *
  4. * Dave Engebretsen, Peter Bergner, and
  5. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  6. *
  7. * Plus various changes from other IBM teams...
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/smp.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/cache.h>
  22. #include <linux/err.h>
  23. #include <linux/device.h>
  24. #include <linux/cpu.h>
  25. #include <asm/ptrace.h>
  26. #include <linux/atomic.h>
  27. #include <asm/irq.h>
  28. #include <asm/page.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #include <asm/smp.h>
  33. #include <asm/paca.h>
  34. #include <asm/machdep.h>
  35. #include <asm/cputable.h>
  36. #include <asm/firmware.h>
  37. #include <asm/rtas.h>
  38. #include <asm/vdso_datapage.h>
  39. #include <asm/cputhreads.h>
  40. #include <asm/xics.h>
  41. #include <asm/xive.h>
  42. #include <asm/dbell.h>
  43. #include <asm/plpar_wrappers.h>
  44. #include <asm/code-patching.h>
  45. #include "pseries.h"
  46. #include "offline_states.h"
  47. /*
  48. * The Primary thread of each non-boot processor was started from the OF client
  49. * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
  50. */
  51. static cpumask_var_t of_spin_mask;
  52. /* Query where a cpu is now. Return codes #defined in plpar_wrappers.h */
  53. int smp_query_cpu_stopped(unsigned int pcpu)
  54. {
  55. int cpu_status, status;
  56. int qcss_tok = rtas_token("query-cpu-stopped-state");
  57. if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
  58. printk_once(KERN_INFO
  59. "Firmware doesn't support query-cpu-stopped-state\n");
  60. return QCSS_HARDWARE_ERROR;
  61. }
  62. status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
  63. if (status != 0) {
  64. printk(KERN_ERR
  65. "RTAS query-cpu-stopped-state failed: %i\n", status);
  66. return status;
  67. }
  68. return cpu_status;
  69. }
  70. /**
  71. * smp_startup_cpu() - start the given cpu
  72. *
  73. * At boot time, there is nothing to do for primary threads which were
  74. * started from Open Firmware. For anything else, call RTAS with the
  75. * appropriate start location.
  76. *
  77. * Returns:
  78. * 0 - failure
  79. * 1 - success
  80. */
  81. static inline int smp_startup_cpu(unsigned int lcpu)
  82. {
  83. int status;
  84. unsigned long start_here =
  85. __pa(ppc_function_entry(generic_secondary_smp_init));
  86. unsigned int pcpu;
  87. int start_cpu;
  88. if (cpumask_test_cpu(lcpu, of_spin_mask))
  89. /* Already started by OF and sitting in spin loop */
  90. return 1;
  91. pcpu = get_hard_smp_processor_id(lcpu);
  92. /* Check to see if the CPU out of FW already for kexec */
  93. if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
  94. cpumask_set_cpu(lcpu, of_spin_mask);
  95. return 1;
  96. }
  97. /* Fixup atomic count: it exited inside IRQ handler. */
  98. task_thread_info(paca_ptrs[lcpu]->__current)->preempt_count = 0;
  99. #ifdef CONFIG_HOTPLUG_CPU
  100. if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
  101. goto out;
  102. #endif
  103. /*
  104. * If the RTAS start-cpu token does not exist then presume the
  105. * cpu is already spinning.
  106. */
  107. start_cpu = rtas_token("start-cpu");
  108. if (start_cpu == RTAS_UNKNOWN_SERVICE)
  109. return 1;
  110. status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
  111. if (status != 0) {
  112. printk(KERN_ERR "start-cpu failed: %i\n", status);
  113. return 0;
  114. }
  115. #ifdef CONFIG_HOTPLUG_CPU
  116. out:
  117. #endif
  118. return 1;
  119. }
  120. static void smp_setup_cpu(int cpu)
  121. {
  122. if (xive_enabled())
  123. xive_smp_setup_cpu();
  124. else if (cpu != boot_cpuid)
  125. xics_setup_cpu();
  126. if (firmware_has_feature(FW_FEATURE_SPLPAR))
  127. vpa_init(cpu);
  128. cpumask_clear_cpu(cpu, of_spin_mask);
  129. #ifdef CONFIG_HOTPLUG_CPU
  130. set_cpu_current_state(cpu, CPU_STATE_ONLINE);
  131. set_default_offline_state(cpu);
  132. #endif
  133. }
  134. static int smp_pSeries_kick_cpu(int nr)
  135. {
  136. if (nr < 0 || nr >= nr_cpu_ids)
  137. return -EINVAL;
  138. if (!smp_startup_cpu(nr))
  139. return -ENOENT;
  140. /*
  141. * The processor is currently spinning, waiting for the
  142. * cpu_start field to become non-zero After we set cpu_start,
  143. * the processor will continue on to secondary_start
  144. */
  145. paca_ptrs[nr]->cpu_start = 1;
  146. #ifdef CONFIG_HOTPLUG_CPU
  147. set_preferred_offline_state(nr, CPU_STATE_ONLINE);
  148. if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
  149. long rc;
  150. unsigned long hcpuid;
  151. hcpuid = get_hard_smp_processor_id(nr);
  152. rc = plpar_hcall_norets(H_PROD, hcpuid);
  153. if (rc != H_SUCCESS)
  154. printk(KERN_ERR "Error: Prod to wake up processor %d "
  155. "Ret= %ld\n", nr, rc);
  156. }
  157. #endif
  158. return 0;
  159. }
  160. static int pseries_smp_prepare_cpu(int cpu)
  161. {
  162. if (xive_enabled())
  163. return xive_smp_prepare_cpu(cpu);
  164. return 0;
  165. }
  166. static void smp_pseries_cause_ipi(int cpu)
  167. {
  168. /* POWER9 should not use this handler */
  169. if (doorbell_try_core_ipi(cpu))
  170. return;
  171. icp_ops->cause_ipi(cpu);
  172. }
  173. static int pseries_cause_nmi_ipi(int cpu)
  174. {
  175. int hwcpu;
  176. if (cpu == NMI_IPI_ALL_OTHERS) {
  177. hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
  178. } else {
  179. if (cpu < 0) {
  180. WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
  181. return 0;
  182. }
  183. hwcpu = get_hard_smp_processor_id(cpu);
  184. }
  185. if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
  186. return 1;
  187. return 0;
  188. }
  189. static __init void pSeries_smp_probe_xics(void)
  190. {
  191. xics_smp_probe();
  192. if (cpu_has_feature(CPU_FTR_DBELL))
  193. smp_ops->cause_ipi = smp_pseries_cause_ipi;
  194. else
  195. smp_ops->cause_ipi = icp_ops->cause_ipi;
  196. }
  197. static __init void pSeries_smp_probe(void)
  198. {
  199. if (xive_enabled())
  200. /*
  201. * Don't use P9 doorbells when XIVE is enabled. IPIs
  202. * using MMIOs should be faster
  203. */
  204. xive_smp_probe();
  205. else
  206. pSeries_smp_probe_xics();
  207. }
  208. static struct smp_ops_t pseries_smp_ops = {
  209. .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
  210. .cause_ipi = NULL, /* Filled at runtime by pSeries_smp_probe() */
  211. .cause_nmi_ipi = pseries_cause_nmi_ipi,
  212. .probe = pSeries_smp_probe,
  213. .prepare_cpu = pseries_smp_prepare_cpu,
  214. .kick_cpu = smp_pSeries_kick_cpu,
  215. .setup_cpu = smp_setup_cpu,
  216. .cpu_bootable = smp_generic_cpu_bootable,
  217. };
  218. /* This is called very early */
  219. void __init smp_init_pseries(void)
  220. {
  221. int i;
  222. pr_debug(" -> smp_init_pSeries()\n");
  223. smp_ops = &pseries_smp_ops;
  224. alloc_bootmem_cpumask_var(&of_spin_mask);
  225. /*
  226. * Mark threads which are still spinning in hold loops
  227. *
  228. * We know prom_init will not have started them if RTAS supports
  229. * query-cpu-stopped-state.
  230. */
  231. if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
  232. if (cpu_has_feature(CPU_FTR_SMT)) {
  233. for_each_present_cpu(i) {
  234. if (cpu_thread_in_core(i) == 0)
  235. cpumask_set_cpu(i, of_spin_mask);
  236. }
  237. } else
  238. cpumask_copy(of_spin_mask, cpu_present_mask);
  239. cpumask_clear_cpu(boot_cpuid, of_spin_mask);
  240. }
  241. /* Non-lpar has additional take/give timebase */
  242. if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
  243. smp_ops->give_timebase = rtas_give_timebase;
  244. smp_ops->take_timebase = rtas_take_timebase;
  245. }
  246. pr_debug(" <- smp_init_pSeries()\n");
  247. }