smp.c 5.3 KB

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