[POWERPC] Convert from DBG() to pr_debug() in platforms/pseries/
[safe/jmp/linux-2.6] / arch / powerpc / platforms / pseries / smp.c
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
15 #undef DEBUG
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/smp.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/spinlock.h>
25 #include <linux/cache.h>
26 #include <linux/err.h>
27 #include <linux/sysdev.h>
28 #include <linux/cpu.h>
29
30 #include <asm/ptrace.h>
31 #include <asm/atomic.h>
32 #include <asm/irq.h>
33 #include <asm/page.h>
34 #include <asm/pgtable.h>
35 #include <asm/io.h>
36 #include <asm/prom.h>
37 #include <asm/smp.h>
38 #include <asm/paca.h>
39 #include <asm/time.h>
40 #include <asm/machdep.h>
41 #include "xics.h"
42 #include <asm/cputable.h>
43 #include <asm/firmware.h>
44 #include <asm/system.h>
45 #include <asm/rtas.h>
46 #include <asm/pSeries_reconfig.h>
47 #include <asm/mpic.h>
48 #include <asm/vdso_datapage.h>
49 #include <asm/cputhreads.h>
50
51 #include "plpar_wrappers.h"
52 #include "pseries.h"
53
54
55 /*
56  * The primary thread of each non-boot processor is recorded here before
57  * smp init.
58  */
59 static cpumask_t of_spin_map;
60
61 extern void generic_secondary_smp_init(unsigned long);
62
63 /**
64  * smp_startup_cpu() - start the given cpu
65  *
66  * At boot time, there is nothing to do for primary threads which were
67  * started from Open Firmware.  For anything else, call RTAS with the
68  * appropriate start location.
69  *
70  * Returns:
71  *      0       - failure
72  *      1       - success
73  */
74 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
75 {
76         int status;
77         unsigned long start_here = __pa((u32)*((unsigned long *)
78                                                generic_secondary_smp_init));
79         unsigned int pcpu;
80         int start_cpu;
81
82         if (cpu_isset(lcpu, of_spin_map))
83                 /* Already started by OF and sitting in spin loop */
84                 return 1;
85
86         pcpu = get_hard_smp_processor_id(lcpu);
87
88         /* Fixup atomic count: it exited inside IRQ handler. */
89         task_thread_info(paca[lcpu].__current)->preempt_count   = 0;
90
91         /* 
92          * If the RTAS start-cpu token does not exist then presume the
93          * cpu is already spinning.
94          */
95         start_cpu = rtas_token("start-cpu");
96         if (start_cpu == RTAS_UNKNOWN_SERVICE)
97                 return 1;
98
99         status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
100         if (status != 0) {
101                 printk(KERN_ERR "start-cpu failed: %i\n", status);
102                 return 0;
103         }
104
105         return 1;
106 }
107
108 #ifdef CONFIG_XICS
109 static inline void smp_xics_do_message(int cpu, int msg)
110 {
111         set_bit(msg, &xics_ipi_message[cpu].value);
112         mb();
113         xics_cause_IPI(cpu);
114 }
115
116 static void smp_xics_message_pass(int target, int msg)
117 {
118         unsigned int i;
119
120         if (target < NR_CPUS) {
121                 smp_xics_do_message(target, msg);
122         } else {
123                 for_each_online_cpu(i) {
124                         if (target == MSG_ALL_BUT_SELF
125                             && i == smp_processor_id())
126                                 continue;
127                         smp_xics_do_message(i, msg);
128                 }
129         }
130 }
131
132 static int __init smp_xics_probe(void)
133 {
134         xics_request_IPIs();
135
136         return cpus_weight(cpu_possible_map);
137 }
138
139 static void __devinit smp_xics_setup_cpu(int cpu)
140 {
141         if (cpu != boot_cpuid)
142                 xics_setup_cpu();
143
144         if (firmware_has_feature(FW_FEATURE_SPLPAR))
145                 vpa_init(cpu);
146
147         cpu_clear(cpu, of_spin_map);
148
149 }
150 #endif /* CONFIG_XICS */
151
152 static DEFINE_SPINLOCK(timebase_lock);
153 static unsigned long timebase = 0;
154
155 static void __devinit pSeries_give_timebase(void)
156 {
157         spin_lock(&timebase_lock);
158         rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
159         timebase = get_tb();
160         spin_unlock(&timebase_lock);
161
162         while (timebase)
163                 barrier();
164         rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
165 }
166
167 static void __devinit pSeries_take_timebase(void)
168 {
169         while (!timebase)
170                 barrier();
171         spin_lock(&timebase_lock);
172         set_tb(timebase >> 32, timebase & 0xffffffff);
173         timebase = 0;
174         spin_unlock(&timebase_lock);
175 }
176
177 static void __devinit smp_pSeries_kick_cpu(int nr)
178 {
179         BUG_ON(nr < 0 || nr >= NR_CPUS);
180
181         if (!smp_startup_cpu(nr))
182                 return;
183
184         /*
185          * The processor is currently spinning, waiting for the
186          * cpu_start field to become non-zero After we set cpu_start,
187          * the processor will continue on to secondary_start
188          */
189         paca[nr].cpu_start = 1;
190 }
191
192 static int smp_pSeries_cpu_bootable(unsigned int nr)
193 {
194         /* Special case - we inhibit secondary thread startup
195          * during boot if the user requests it.  Odd-numbered
196          * cpus are assumed to be secondary threads.
197          */
198         if (system_state < SYSTEM_RUNNING &&
199             cpu_has_feature(CPU_FTR_SMT) &&
200             !smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
201                 return 0;
202
203         return 1;
204 }
205 #ifdef CONFIG_MPIC
206 static struct smp_ops_t pSeries_mpic_smp_ops = {
207         .message_pass   = smp_mpic_message_pass,
208         .probe          = smp_mpic_probe,
209         .kick_cpu       = smp_pSeries_kick_cpu,
210         .setup_cpu      = smp_mpic_setup_cpu,
211 };
212 #endif
213 #ifdef CONFIG_XICS
214 static struct smp_ops_t pSeries_xics_smp_ops = {
215         .message_pass   = smp_xics_message_pass,
216         .probe          = smp_xics_probe,
217         .kick_cpu       = smp_pSeries_kick_cpu,
218         .setup_cpu      = smp_xics_setup_cpu,
219         .cpu_bootable   = smp_pSeries_cpu_bootable,
220 };
221 #endif
222
223 /* This is called very early */
224 static void __init smp_init_pseries(void)
225 {
226         int i;
227
228         pr_debug(" -> smp_init_pSeries()\n");
229
230         /* Mark threads which are still spinning in hold loops. */
231         if (cpu_has_feature(CPU_FTR_SMT)) {
232                 for_each_present_cpu(i) { 
233                         if (i % 2 == 0)
234                                 /*
235                                  * Even-numbered logical cpus correspond to
236                                  * primary threads.
237                                  */
238                                 cpu_set(i, of_spin_map);
239                 }
240         } else {
241                 of_spin_map = cpu_present_map;
242         }
243
244         cpu_clear(boot_cpuid, of_spin_map);
245
246         /* Non-lpar has additional take/give timebase */
247         if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
248                 smp_ops->give_timebase = pSeries_give_timebase;
249                 smp_ops->take_timebase = pSeries_take_timebase;
250         }
251
252         pr_debug(" <- smp_init_pSeries()\n");
253 }
254
255 #ifdef CONFIG_MPIC
256 void __init smp_init_pseries_mpic(void)
257 {
258         smp_ops = &pSeries_mpic_smp_ops;
259
260         smp_init_pseries();
261 }
262 #endif
263
264 void __init smp_init_pseries_xics(void)
265 {
266         smp_ops = &pSeries_xics_smp_ops;
267
268         smp_init_pseries();
269 }