smp.c 6.3 KB

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