vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc64 / kernel / 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/config.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/spinlock.h>
26 #include <linux/cache.h>
27 #include <linux/err.h>
28 #include <linux/sysdev.h>
29 #include <linux/cpu.h>
30
31 #include <asm/ptrace.h>
32 #include <asm/atomic.h>
33 #include <asm/irq.h>
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/io.h>
37 #include <asm/prom.h>
38 #include <asm/smp.h>
39 #include <asm/paca.h>
40 #include <asm/time.h>
41 #include <asm/machdep.h>
42 #include <asm/xics.h>
43 #include <asm/cputable.h>
44 #include <asm/system.h>
45 #include <asm/rtas.h>
46 #include <asm/plpar_wrappers.h>
47
48 #include "mpic.h"
49
50 #ifdef DEBUG
51 #define DBG(fmt...) udbg_printf(fmt)
52 #else
53 #define DBG(fmt...)
54 #endif
55
56 extern void pSeries_secondary_smp_init(unsigned long);
57
58 /* Get state of physical CPU.
59  * Return codes:
60  *      0       - The processor is in the RTAS stopped state
61  *      1       - stop-self is in progress
62  *      2       - The processor is not in the RTAS stopped state
63  *      -1      - Hardware Error
64  *      -2      - Hardware Busy, Try again later.
65  */
66 static int query_cpu_stopped(unsigned int pcpu)
67 {
68         int cpu_status;
69         int status, qcss_tok;
70
71         qcss_tok = rtas_token("query-cpu-stopped-state");
72         if (qcss_tok == RTAS_UNKNOWN_SERVICE)
73                 return -1;
74         status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
75         if (status != 0) {
76                 printk(KERN_ERR
77                        "RTAS query-cpu-stopped-state failed: %i\n", status);
78                 return status;
79         }
80
81         return cpu_status;
82 }
83
84
85 #ifdef CONFIG_HOTPLUG_CPU
86
87 int pSeries_cpu_disable(void)
88 {
89         systemcfg->processorCount--;
90
91         /*fix boot_cpuid here*/
92         if (smp_processor_id() == boot_cpuid)
93                 boot_cpuid = any_online_cpu(cpu_online_map);
94
95         /* FIXME: abstract this to not be platform specific later on */
96         xics_migrate_irqs_away();
97         return 0;
98 }
99
100 void pSeries_cpu_die(unsigned int cpu)
101 {
102         int tries;
103         int cpu_status;
104         unsigned int pcpu = get_hard_smp_processor_id(cpu);
105
106         for (tries = 0; tries < 25; tries++) {
107                 cpu_status = query_cpu_stopped(pcpu);
108                 if (cpu_status == 0 || cpu_status == -1)
109                         break;
110                 msleep(200);
111         }
112         if (cpu_status != 0) {
113                 printk("Querying DEAD? cpu %i (%i) shows %i\n",
114                        cpu, pcpu, cpu_status);
115         }
116
117         /* Isolation and deallocation are definatly done by
118          * drslot_chrp_cpu.  If they were not they would be
119          * done here.  Change isolate state to Isolate and
120          * change allocation-state to Unusable.
121          */
122         paca[cpu].cpu_start = 0;
123 }
124
125 /* Search all cpu device nodes for an offline logical cpu.  If a
126  * device node has a "ibm,my-drc-index" property (meaning this is an
127  * LPAR), paranoid-check whether we own the cpu.  For each "thread"
128  * of a cpu, if it is offline and has the same hw index as before,
129  * grab that in preference.
130  */
131 static unsigned int find_physical_cpu_to_start(unsigned int old_hwindex)
132 {
133         struct device_node *np = NULL;
134         unsigned int best = -1U;
135
136         while ((np = of_find_node_by_type(np, "cpu"))) {
137                 int nr_threads, len;
138                 u32 *index = (u32 *)get_property(np, "ibm,my-drc-index", NULL);
139                 u32 *tid = (u32 *)
140                         get_property(np, "ibm,ppc-interrupt-server#s", &len);
141
142                 if (!tid)
143                         tid = (u32 *)get_property(np, "reg", &len);
144
145                 if (!tid)
146                         continue;
147
148                 /* If there is a drc-index, make sure that we own
149                  * the cpu.
150                  */
151                 if (index) {
152                         int state;
153                         int rc = rtas_get_sensor(9003, *index, &state);
154                         if (rc != 0 || state != 1)
155                                 continue;
156                 }
157
158                 nr_threads = len / sizeof(u32);
159
160                 while (nr_threads--) {
161                         if (0 == query_cpu_stopped(tid[nr_threads])) {
162                                 best = tid[nr_threads];
163                                 if (best == old_hwindex)
164                                         goto out;
165                         }
166                 }
167         }
168 out:
169         of_node_put(np);
170         return best;
171 }
172
173 /**
174  * smp_startup_cpu() - start the given cpu
175  *
176  * At boot time, there is nothing to do.  At run-time, call RTAS with
177  * the appropriate start location, if the cpu is in the RTAS stopped
178  * state.
179  *
180  * Returns:
181  *      0       - failure
182  *      1       - success
183  */
184 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
185 {
186         int status;
187         unsigned long start_here = __pa((u32)*((unsigned long *)
188                                                pSeries_secondary_smp_init));
189         unsigned int pcpu;
190
191         /* At boot time the cpus are already spinning in hold
192          * loops, so nothing to do. */
193         if (system_state < SYSTEM_RUNNING)
194                 return 1;
195
196         pcpu = find_physical_cpu_to_start(get_hard_smp_processor_id(lcpu));
197         if (pcpu == -1U) {
198                 printk(KERN_INFO "No more cpus available, failing\n");
199                 return 0;
200         }
201
202         /* Fixup atomic count: it exited inside IRQ handler. */
203         paca[lcpu].__current->thread_info->preempt_count        = 0;
204
205         /* At boot this is done in prom.c. */
206         paca[lcpu].hw_cpu_id = pcpu;
207
208         status = rtas_call(rtas_token("start-cpu"), 3, 1, NULL,
209                            pcpu, start_here, lcpu);
210         if (status != 0) {
211                 printk(KERN_ERR "start-cpu failed: %i\n", status);
212                 return 0;
213         }
214         return 1;
215 }
216 #else /* ... CONFIG_HOTPLUG_CPU */
217 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
218 {
219         return 1;
220 }
221 #endif /* CONFIG_HOTPLUG_CPU */
222
223 static inline void smp_xics_do_message(int cpu, int msg)
224 {
225         set_bit(msg, &xics_ipi_message[cpu].value);
226         mb();
227         xics_cause_IPI(cpu);
228 }
229
230 static void smp_xics_message_pass(int target, int msg)
231 {
232         unsigned int i;
233
234         if (target < NR_CPUS) {
235                 smp_xics_do_message(target, msg);
236         } else {
237                 for_each_online_cpu(i) {
238                         if (target == MSG_ALL_BUT_SELF
239                             && i == smp_processor_id())
240                                 continue;
241                         smp_xics_do_message(i, msg);
242                 }
243         }
244 }
245
246 static int __init smp_xics_probe(void)
247 {
248         xics_request_IPIs();
249
250         return cpus_weight(cpu_possible_map);
251 }
252
253 static void __devinit smp_xics_setup_cpu(int cpu)
254 {
255         if (cpu != boot_cpuid)
256                 xics_setup_cpu();
257
258         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
259                 vpa_init(cpu);
260
261         /*
262          * Put the calling processor into the GIQ.  This is really only
263          * necessary from a secondary thread as the OF start-cpu interface
264          * performs this function for us on primary threads.
265          */
266         rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
267                 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
268 }
269
270 static DEFINE_SPINLOCK(timebase_lock);
271 static unsigned long timebase = 0;
272
273 static void __devinit pSeries_give_timebase(void)
274 {
275         spin_lock(&timebase_lock);
276         rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
277         timebase = get_tb();
278         spin_unlock(&timebase_lock);
279
280         while (timebase)
281                 barrier();
282         rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
283 }
284
285 static void __devinit pSeries_take_timebase(void)
286 {
287         while (!timebase)
288                 barrier();
289         spin_lock(&timebase_lock);
290         set_tb(timebase >> 32, timebase & 0xffffffff);
291         timebase = 0;
292         spin_unlock(&timebase_lock);
293 }
294
295 static void __devinit smp_pSeries_kick_cpu(int nr)
296 {
297         BUG_ON(nr < 0 || nr >= NR_CPUS);
298
299         if (!smp_startup_cpu(nr))
300                 return;
301
302         /*
303          * The processor is currently spinning, waiting for the
304          * cpu_start field to become non-zero After we set cpu_start,
305          * the processor will continue on to secondary_start
306          */
307         paca[nr].cpu_start = 1;
308 }
309
310 static struct smp_ops_t pSeries_mpic_smp_ops = {
311         .message_pass   = smp_mpic_message_pass,
312         .probe          = smp_mpic_probe,
313         .kick_cpu       = smp_pSeries_kick_cpu,
314         .setup_cpu      = smp_mpic_setup_cpu,
315 };
316
317 static struct smp_ops_t pSeries_xics_smp_ops = {
318         .message_pass   = smp_xics_message_pass,
319         .probe          = smp_xics_probe,
320         .kick_cpu       = smp_pSeries_kick_cpu,
321         .setup_cpu      = smp_xics_setup_cpu,
322 };
323
324 /* This is called very early */
325 void __init smp_init_pSeries(void)
326 {
327         int ret, i;
328
329         DBG(" -> smp_init_pSeries()\n");
330
331         if (ppc64_interrupt_controller == IC_OPEN_PIC)
332                 smp_ops = &pSeries_mpic_smp_ops;
333         else
334                 smp_ops = &pSeries_xics_smp_ops;
335
336 #ifdef CONFIG_HOTPLUG_CPU
337         smp_ops->cpu_disable = pSeries_cpu_disable;
338         smp_ops->cpu_die = pSeries_cpu_die;
339 #endif
340
341         /* Start secondary threads on SMT systems; primary threads
342          * are already in the running state.
343          */
344         for_each_present_cpu(i) {
345                 if (query_cpu_stopped(get_hard_smp_processor_id(i)) == 0) {
346                         printk("%16.16x : starting thread\n", i);
347                         DBG("%16.16x : starting thread\n", i);
348                         rtas_call(rtas_token("start-cpu"), 3, 1, &ret,
349                                   get_hard_smp_processor_id(i),
350                                   __pa((u32)*((unsigned long *)
351                                               pSeries_secondary_smp_init)),
352                                   i);
353                 }
354         }
355
356         /* Non-lpar has additional take/give timebase */
357         if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
358                 smp_ops->give_timebase = pSeries_give_timebase;
359                 smp_ops->take_timebase = pSeries_take_timebase;
360         }
361
362         DBG(" <- smp_init_pSeries()\n");
363 }
364