smp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SMP support for BPA 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. #undef DEBUG
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/smp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/cache.h>
  25. #include <linux/err.h>
  26. #include <linux/sysdev.h>
  27. #include <linux/cpu.h>
  28. #include <asm/ptrace.h>
  29. #include <asm/atomic.h>
  30. #include <asm/irq.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/io.h>
  34. #include <asm/prom.h>
  35. #include <asm/smp.h>
  36. #include <asm/paca.h>
  37. #include <asm/time.h>
  38. #include <asm/machdep.h>
  39. #include <asm/cputable.h>
  40. #include <asm/firmware.h>
  41. #include <asm/system.h>
  42. #include <asm/rtas.h>
  43. #include "interrupt.h"
  44. #ifdef DEBUG
  45. #define DBG(fmt...) udbg_printf(fmt)
  46. #else
  47. #define DBG(fmt...)
  48. #endif
  49. /*
  50. * The primary thread of each non-boot processor is recorded here before
  51. * smp init.
  52. */
  53. static cpumask_t of_spin_map;
  54. extern void pSeries_secondary_smp_init(unsigned long);
  55. /**
  56. * smp_startup_cpu() - start the given cpu
  57. *
  58. * At boot time, there is nothing to do for primary threads which were
  59. * started from Open Firmware. For anything else, call RTAS with the
  60. * appropriate start location.
  61. *
  62. * Returns:
  63. * 0 - failure
  64. * 1 - success
  65. */
  66. static inline int __devinit smp_startup_cpu(unsigned int lcpu)
  67. {
  68. int status;
  69. unsigned long start_here = __pa((u32)*((unsigned long *)
  70. pSeries_secondary_smp_init));
  71. unsigned int pcpu;
  72. int start_cpu;
  73. if (cpu_isset(lcpu, of_spin_map))
  74. /* Already started by OF and sitting in spin loop */
  75. return 1;
  76. pcpu = get_hard_smp_processor_id(lcpu);
  77. /* Fixup atomic count: it exited inside IRQ handler. */
  78. task_thread_info(paca[lcpu].__current)->preempt_count = 0;
  79. /*
  80. * If the RTAS start-cpu token does not exist then presume the
  81. * cpu is already spinning.
  82. */
  83. start_cpu = rtas_token("start-cpu");
  84. if (start_cpu == RTAS_UNKNOWN_SERVICE)
  85. return 1;
  86. status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, lcpu);
  87. if (status != 0) {
  88. printk(KERN_ERR "start-cpu failed: %i\n", status);
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. static void smp_iic_message_pass(int target, int msg)
  94. {
  95. unsigned int i;
  96. if (target < NR_CPUS) {
  97. iic_cause_IPI(target, msg);
  98. } else {
  99. for_each_online_cpu(i) {
  100. if (target == MSG_ALL_BUT_SELF
  101. && i == smp_processor_id())
  102. continue;
  103. iic_cause_IPI(i, msg);
  104. }
  105. }
  106. }
  107. static int __init smp_iic_probe(void)
  108. {
  109. iic_request_IPIs();
  110. return cpus_weight(cpu_possible_map);
  111. }
  112. static void __devinit smp_iic_setup_cpu(int cpu)
  113. {
  114. if (cpu != boot_cpuid)
  115. iic_setup_cpu();
  116. }
  117. static DEFINE_SPINLOCK(timebase_lock);
  118. static unsigned long timebase = 0;
  119. static void __devinit cell_give_timebase(void)
  120. {
  121. spin_lock(&timebase_lock);
  122. rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
  123. timebase = get_tb();
  124. spin_unlock(&timebase_lock);
  125. while (timebase)
  126. barrier();
  127. rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
  128. }
  129. static void __devinit cell_take_timebase(void)
  130. {
  131. while (!timebase)
  132. barrier();
  133. spin_lock(&timebase_lock);
  134. set_tb(timebase >> 32, timebase & 0xffffffff);
  135. timebase = 0;
  136. spin_unlock(&timebase_lock);
  137. }
  138. static void __devinit smp_cell_kick_cpu(int nr)
  139. {
  140. BUG_ON(nr < 0 || nr >= NR_CPUS);
  141. if (!smp_startup_cpu(nr))
  142. return;
  143. /*
  144. * The processor is currently spinning, waiting for the
  145. * cpu_start field to become non-zero After we set cpu_start,
  146. * the processor will continue on to secondary_start
  147. */
  148. paca[nr].cpu_start = 1;
  149. }
  150. static int smp_cell_cpu_bootable(unsigned int nr)
  151. {
  152. /* Special case - we inhibit secondary thread startup
  153. * during boot if the user requests it. Odd-numbered
  154. * cpus are assumed to be secondary threads.
  155. */
  156. if (system_state < SYSTEM_RUNNING &&
  157. cpu_has_feature(CPU_FTR_SMT) &&
  158. !smt_enabled_at_boot && nr % 2 != 0)
  159. return 0;
  160. return 1;
  161. }
  162. static struct smp_ops_t bpa_iic_smp_ops = {
  163. .message_pass = smp_iic_message_pass,
  164. .probe = smp_iic_probe,
  165. .kick_cpu = smp_cell_kick_cpu,
  166. .setup_cpu = smp_iic_setup_cpu,
  167. .cpu_bootable = smp_cell_cpu_bootable,
  168. };
  169. /* This is called very early */
  170. void __init smp_init_cell(void)
  171. {
  172. int i;
  173. DBG(" -> smp_init_cell()\n");
  174. smp_ops = &bpa_iic_smp_ops;
  175. /* Mark threads which are still spinning in hold loops. */
  176. if (cpu_has_feature(CPU_FTR_SMT)) {
  177. for_each_present_cpu(i) {
  178. if (i % 2 == 0)
  179. /*
  180. * Even-numbered logical cpus correspond to
  181. * primary threads.
  182. */
  183. cpu_set(i, of_spin_map);
  184. }
  185. } else {
  186. of_spin_map = cpu_present_map;
  187. }
  188. cpu_clear(boot_cpuid, of_spin_map);
  189. /* Non-lpar has additional take/give timebase */
  190. if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
  191. smp_ops->give_timebase = cell_give_timebase;
  192. smp_ops->take_timebase = cell_take_timebase;
  193. }
  194. DBG(" <- smp_init_cell()\n");
  195. }