smp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/time.h>
  36. #include <asm/machdep.h>
  37. #include <asm/cputable.h>
  38. #include <asm/firmware.h>
  39. #include <asm/system.h>
  40. #include <asm/rtas.h>
  41. #include <asm/pSeries_reconfig.h>
  42. #include <asm/mpic.h>
  43. #include <asm/vdso_datapage.h>
  44. #include <asm/cputhreads.h>
  45. #include "plpar_wrappers.h"
  46. #include "pseries.h"
  47. #include "xics.h"
  48. /*
  49. * The Primary thread of each non-boot processor was started from the OF client
  50. * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
  51. */
  52. static cpumask_t of_spin_map;
  53. extern void generic_secondary_smp_init(unsigned long);
  54. /**
  55. * smp_startup_cpu() - start the given cpu
  56. *
  57. * At boot time, there is nothing to do for primary threads which were
  58. * started from Open Firmware. For anything else, call RTAS with the
  59. * appropriate start location.
  60. *
  61. * Returns:
  62. * 0 - failure
  63. * 1 - success
  64. */
  65. static inline int __devinit smp_startup_cpu(unsigned int lcpu)
  66. {
  67. int status;
  68. unsigned long start_here = __pa((u32)*((unsigned long *)
  69. generic_secondary_smp_init));
  70. unsigned int pcpu;
  71. int start_cpu;
  72. if (cpu_isset(lcpu, of_spin_map))
  73. /* Already started by OF and sitting in spin loop */
  74. return 1;
  75. pcpu = get_hard_smp_processor_id(lcpu);
  76. /* Fixup atomic count: it exited inside IRQ handler. */
  77. task_thread_info(paca[lcpu].__current)->preempt_count = 0;
  78. /*
  79. * If the RTAS start-cpu token does not exist then presume the
  80. * cpu is already spinning.
  81. */
  82. start_cpu = rtas_token("start-cpu");
  83. if (start_cpu == RTAS_UNKNOWN_SERVICE)
  84. return 1;
  85. status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
  86. if (status != 0) {
  87. printk(KERN_ERR "start-cpu failed: %i\n", status);
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. #ifdef CONFIG_XICS
  93. static void __devinit smp_xics_setup_cpu(int cpu)
  94. {
  95. if (cpu != boot_cpuid)
  96. xics_setup_cpu();
  97. if (firmware_has_feature(FW_FEATURE_SPLPAR))
  98. vpa_init(cpu);
  99. cpu_clear(cpu, of_spin_map);
  100. }
  101. #endif /* CONFIG_XICS */
  102. static DEFINE_SPINLOCK(timebase_lock);
  103. static unsigned long timebase = 0;
  104. static void __devinit pSeries_give_timebase(void)
  105. {
  106. spin_lock(&timebase_lock);
  107. rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
  108. timebase = get_tb();
  109. spin_unlock(&timebase_lock);
  110. while (timebase)
  111. barrier();
  112. rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
  113. }
  114. static void __devinit pSeries_take_timebase(void)
  115. {
  116. while (!timebase)
  117. barrier();
  118. spin_lock(&timebase_lock);
  119. set_tb(timebase >> 32, timebase & 0xffffffff);
  120. timebase = 0;
  121. spin_unlock(&timebase_lock);
  122. }
  123. static void __devinit smp_pSeries_kick_cpu(int nr)
  124. {
  125. BUG_ON(nr < 0 || nr >= NR_CPUS);
  126. if (!smp_startup_cpu(nr))
  127. return;
  128. /*
  129. * The processor is currently spinning, waiting for the
  130. * cpu_start field to become non-zero After we set cpu_start,
  131. * the processor will continue on to secondary_start
  132. */
  133. paca[nr].cpu_start = 1;
  134. }
  135. static int smp_pSeries_cpu_bootable(unsigned int nr)
  136. {
  137. /* Special case - we inhibit secondary thread startup
  138. * during boot if the user requests it.
  139. */
  140. if (system_state < SYSTEM_RUNNING &&
  141. cpu_has_feature(CPU_FTR_SMT) &&
  142. !smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
  143. return 0;
  144. return 1;
  145. }
  146. #ifdef CONFIG_MPIC
  147. static struct smp_ops_t pSeries_mpic_smp_ops = {
  148. .message_pass = smp_mpic_message_pass,
  149. .probe = smp_mpic_probe,
  150. .kick_cpu = smp_pSeries_kick_cpu,
  151. .setup_cpu = smp_mpic_setup_cpu,
  152. };
  153. #endif
  154. #ifdef CONFIG_XICS
  155. static struct smp_ops_t pSeries_xics_smp_ops = {
  156. .message_pass = smp_xics_message_pass,
  157. .probe = smp_xics_probe,
  158. .kick_cpu = smp_pSeries_kick_cpu,
  159. .setup_cpu = smp_xics_setup_cpu,
  160. .cpu_bootable = smp_pSeries_cpu_bootable,
  161. };
  162. #endif
  163. /* This is called very early */
  164. static void __init smp_init_pseries(void)
  165. {
  166. int i;
  167. pr_debug(" -> smp_init_pSeries()\n");
  168. /* Mark threads which are still spinning in hold loops. */
  169. if (cpu_has_feature(CPU_FTR_SMT)) {
  170. for_each_present_cpu(i) {
  171. if (cpu_thread_in_core(i) == 0)
  172. cpu_set(i, of_spin_map);
  173. }
  174. } else {
  175. of_spin_map = cpu_present_map;
  176. }
  177. cpu_clear(boot_cpuid, of_spin_map);
  178. /* Non-lpar has additional take/give timebase */
  179. if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
  180. smp_ops->give_timebase = pSeries_give_timebase;
  181. smp_ops->take_timebase = pSeries_take_timebase;
  182. }
  183. pr_debug(" <- smp_init_pSeries()\n");
  184. }
  185. #ifdef CONFIG_MPIC
  186. void __init smp_init_pseries_mpic(void)
  187. {
  188. smp_ops = &pSeries_mpic_smp_ops;
  189. smp_init_pseries();
  190. }
  191. #endif
  192. void __init smp_init_pseries_xics(void)
  193. {
  194. smp_ops = &pSeries_xics_smp_ops;
  195. smp_init_pseries();
  196. }