smp.c 6.2 KB

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