smp.c 6.1 KB

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