smp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/mpic.h>
  39. #include <asm/vdso_datapage.h>
  40. #include <asm/cputhreads.h>
  41. #include <asm/xics.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. /*
  53. * If we multiplex IPI mechanisms, store the appropriate XICS IPI mechanism here
  54. */
  55. static void (*xics_cause_ipi)(int cpu, unsigned long data);
  56. /* Query where a cpu is now. Return codes #defined in plpar_wrappers.h */
  57. int smp_query_cpu_stopped(unsigned int pcpu)
  58. {
  59. int cpu_status, status;
  60. int qcss_tok = rtas_token("query-cpu-stopped-state");
  61. if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
  62. printk_once(KERN_INFO
  63. "Firmware doesn't support query-cpu-stopped-state\n");
  64. return QCSS_HARDWARE_ERROR;
  65. }
  66. status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
  67. if (status != 0) {
  68. printk(KERN_ERR
  69. "RTAS query-cpu-stopped-state failed: %i\n", status);
  70. return status;
  71. }
  72. return cpu_status;
  73. }
  74. /**
  75. * smp_startup_cpu() - start the given cpu
  76. *
  77. * At boot time, there is nothing to do for primary threads which were
  78. * started from Open Firmware. For anything else, call RTAS with the
  79. * appropriate start location.
  80. *
  81. * Returns:
  82. * 0 - failure
  83. * 1 - success
  84. */
  85. static inline int smp_startup_cpu(unsigned int lcpu)
  86. {
  87. int status;
  88. unsigned long start_here =
  89. __pa(ppc_function_entry(generic_secondary_smp_init));
  90. unsigned int pcpu;
  91. int start_cpu;
  92. if (cpumask_test_cpu(lcpu, of_spin_mask))
  93. /* Already started by OF and sitting in spin loop */
  94. return 1;
  95. pcpu = get_hard_smp_processor_id(lcpu);
  96. /* Check to see if the CPU out of FW already for kexec */
  97. if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
  98. cpumask_set_cpu(lcpu, of_spin_mask);
  99. return 1;
  100. }
  101. /* Fixup atomic count: it exited inside IRQ handler. */
  102. task_thread_info(paca[lcpu].__current)->preempt_count = 0;
  103. #ifdef CONFIG_HOTPLUG_CPU
  104. if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
  105. goto out;
  106. #endif
  107. /*
  108. * If the RTAS start-cpu token does not exist then presume the
  109. * cpu is already spinning.
  110. */
  111. start_cpu = rtas_token("start-cpu");
  112. if (start_cpu == RTAS_UNKNOWN_SERVICE)
  113. return 1;
  114. status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
  115. if (status != 0) {
  116. printk(KERN_ERR "start-cpu failed: %i\n", status);
  117. return 0;
  118. }
  119. #ifdef CONFIG_HOTPLUG_CPU
  120. out:
  121. #endif
  122. return 1;
  123. }
  124. static void smp_xics_setup_cpu(int cpu)
  125. {
  126. if (cpu != boot_cpuid)
  127. xics_setup_cpu();
  128. if (cpu_has_feature(CPU_FTR_DBELL))
  129. doorbell_setup_this_cpu();
  130. if (firmware_has_feature(FW_FEATURE_SPLPAR))
  131. vpa_init(cpu);
  132. cpumask_clear_cpu(cpu, of_spin_mask);
  133. #ifdef CONFIG_HOTPLUG_CPU
  134. set_cpu_current_state(cpu, CPU_STATE_ONLINE);
  135. set_default_offline_state(cpu);
  136. #endif
  137. }
  138. static int smp_pSeries_kick_cpu(int nr)
  139. {
  140. BUG_ON(nr < 0 || nr >= NR_CPUS);
  141. if (!smp_startup_cpu(nr))
  142. return -ENOENT;
  143. /*
  144. * The processor is currently spinning, waiting for the
  145. * cpu_start field to become non-zero After we set cpu_start,
  146. * the processor will continue on to secondary_start
  147. */
  148. paca[nr].cpu_start = 1;
  149. #ifdef CONFIG_HOTPLUG_CPU
  150. set_preferred_offline_state(nr, CPU_STATE_ONLINE);
  151. if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
  152. long rc;
  153. unsigned long hcpuid;
  154. hcpuid = get_hard_smp_processor_id(nr);
  155. rc = plpar_hcall_norets(H_PROD, hcpuid);
  156. if (rc != H_SUCCESS)
  157. printk(KERN_ERR "Error: Prod to wake up processor %d "
  158. "Ret= %ld\n", nr, rc);
  159. }
  160. #endif
  161. return 0;
  162. }
  163. /* Only used on systems that support multiple IPI mechanisms */
  164. static void pSeries_cause_ipi_mux(int cpu, unsigned long data)
  165. {
  166. if (cpumask_test_cpu(cpu, cpu_sibling_mask(smp_processor_id())))
  167. doorbell_cause_ipi(cpu, data);
  168. else
  169. xics_cause_ipi(cpu, data);
  170. }
  171. static __init int pSeries_smp_probe(void)
  172. {
  173. int ret = xics_smp_probe();
  174. if (cpu_has_feature(CPU_FTR_DBELL)) {
  175. xics_cause_ipi = smp_ops->cause_ipi;
  176. smp_ops->cause_ipi = pSeries_cause_ipi_mux;
  177. }
  178. return ret;
  179. }
  180. static struct smp_ops_t pSeries_mpic_smp_ops = {
  181. .message_pass = smp_mpic_message_pass,
  182. .probe = smp_mpic_probe,
  183. .kick_cpu = smp_pSeries_kick_cpu,
  184. .setup_cpu = smp_mpic_setup_cpu,
  185. };
  186. static struct smp_ops_t pSeries_xics_smp_ops = {
  187. .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
  188. .cause_ipi = NULL, /* Filled at runtime by pSeries_smp_probe() */
  189. .probe = pSeries_smp_probe,
  190. .kick_cpu = smp_pSeries_kick_cpu,
  191. .setup_cpu = smp_xics_setup_cpu,
  192. .cpu_bootable = smp_generic_cpu_bootable,
  193. };
  194. /* This is called very early */
  195. static void __init smp_init_pseries(void)
  196. {
  197. int i;
  198. pr_debug(" -> smp_init_pSeries()\n");
  199. alloc_bootmem_cpumask_var(&of_spin_mask);
  200. /*
  201. * Mark threads which are still spinning in hold loops
  202. *
  203. * We know prom_init will not have started them if RTAS supports
  204. * query-cpu-stopped-state.
  205. */
  206. if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
  207. if (cpu_has_feature(CPU_FTR_SMT)) {
  208. for_each_present_cpu(i) {
  209. if (cpu_thread_in_core(i) == 0)
  210. cpumask_set_cpu(i, of_spin_mask);
  211. }
  212. } else
  213. cpumask_copy(of_spin_mask, cpu_present_mask);
  214. cpumask_clear_cpu(boot_cpuid, of_spin_mask);
  215. }
  216. /* Non-lpar has additional take/give timebase */
  217. if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
  218. smp_ops->give_timebase = rtas_give_timebase;
  219. smp_ops->take_timebase = rtas_take_timebase;
  220. }
  221. pr_debug(" <- smp_init_pSeries()\n");
  222. }
  223. void __init smp_init_pseries_mpic(void)
  224. {
  225. smp_ops = &pSeries_mpic_smp_ops;
  226. smp_init_pseries();
  227. }
  228. void __init smp_init_pseries_xics(void)
  229. {
  230. smp_ops = &pSeries_xics_smp_ops;
  231. smp_init_pseries();
  232. }