smp.c 6.2 KB

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