Merge 'acpi-2.6.12' branch into to-akpm
[sfrench/cifs-2.6.git] / arch / ppc64 / kernel / pSeries_smp.c
1 /*
2  * SMP support for pSeries and 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
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 #include <asm/pSeries_reconfig.h>
48
49 #include "mpic.h"
50 #include "bpa_iic.h"
51
52 #ifdef DEBUG
53 #define DBG(fmt...) udbg_printf(fmt)
54 #else
55 #define DBG(fmt...)
56 #endif
57
58 /*
59  * The primary thread of each non-boot processor is recorded here before
60  * smp init.
61  */
62 static cpumask_t of_spin_map;
63
64 extern void pSeries_secondary_smp_init(unsigned long);
65
66 #ifdef CONFIG_HOTPLUG_CPU
67
68 /* Get state of physical CPU.
69  * Return codes:
70  *      0       - The processor is in the RTAS stopped state
71  *      1       - stop-self is in progress
72  *      2       - The processor is not in the RTAS stopped state
73  *      -1      - Hardware Error
74  *      -2      - Hardware Busy, Try again later.
75  */
76 static int query_cpu_stopped(unsigned int pcpu)
77 {
78         int cpu_status;
79         int status, qcss_tok;
80
81         qcss_tok = rtas_token("query-cpu-stopped-state");
82         if (qcss_tok == RTAS_UNKNOWN_SERVICE)
83                 return -1;
84         status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
85         if (status != 0) {
86                 printk(KERN_ERR
87                        "RTAS query-cpu-stopped-state failed: %i\n", status);
88                 return status;
89         }
90
91         return cpu_status;
92 }
93
94 int pSeries_cpu_disable(void)
95 {
96         int cpu = smp_processor_id();
97
98         cpu_clear(cpu, cpu_online_map);
99         systemcfg->processorCount--;
100
101         /*fix boot_cpuid here*/
102         if (cpu == boot_cpuid)
103                 boot_cpuid = any_online_cpu(cpu_online_map);
104
105         /* FIXME: abstract this to not be platform specific later on */
106         xics_migrate_irqs_away();
107         return 0;
108 }
109
110 void pSeries_cpu_die(unsigned int cpu)
111 {
112         int tries;
113         int cpu_status;
114         unsigned int pcpu = get_hard_smp_processor_id(cpu);
115
116         for (tries = 0; tries < 25; tries++) {
117                 cpu_status = query_cpu_stopped(pcpu);
118                 if (cpu_status == 0 || cpu_status == -1)
119                         break;
120                 msleep(200);
121         }
122         if (cpu_status != 0) {
123                 printk("Querying DEAD? cpu %i (%i) shows %i\n",
124                        cpu, pcpu, cpu_status);
125         }
126
127         /* Isolation and deallocation are definatly done by
128          * drslot_chrp_cpu.  If they were not they would be
129          * done here.  Change isolate state to Isolate and
130          * change allocation-state to Unusable.
131          */
132         paca[cpu].cpu_start = 0;
133 }
134
135 /*
136  * Update cpu_present_map and paca(s) for a new cpu node.  The wrinkle
137  * here is that a cpu device node may represent up to two logical cpus
138  * in the SMT case.  We must honor the assumption in other code that
139  * the logical ids for sibling SMT threads x and y are adjacent, such
140  * that x^1 == y and y^1 == x.
141  */
142 static int pSeries_add_processor(struct device_node *np)
143 {
144         unsigned int cpu;
145         cpumask_t candidate_map, tmp = CPU_MASK_NONE;
146         int err = -ENOSPC, len, nthreads, i;
147         u32 *intserv;
148
149         intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
150         if (!intserv)
151                 return 0;
152
153         nthreads = len / sizeof(u32);
154         for (i = 0; i < nthreads; i++)
155                 cpu_set(i, tmp);
156
157         lock_cpu_hotplug();
158
159         BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
160
161         /* Get a bitmap of unoccupied slots. */
162         cpus_xor(candidate_map, cpu_possible_map, cpu_present_map);
163         if (cpus_empty(candidate_map)) {
164                 /* If we get here, it most likely means that NR_CPUS is
165                  * less than the partition's max processors setting.
166                  */
167                 printk(KERN_ERR "Cannot add cpu %s; this system configuration"
168                        " supports %d logical cpus.\n", np->full_name,
169                        cpus_weight(cpu_possible_map));
170                 goto out_unlock;
171         }
172
173         while (!cpus_empty(tmp))
174                 if (cpus_subset(tmp, candidate_map))
175                         /* Found a range where we can insert the new cpu(s) */
176                         break;
177                 else
178                         cpus_shift_left(tmp, tmp, nthreads);
179
180         if (cpus_empty(tmp)) {
181                 printk(KERN_ERR "Unable to find space in cpu_present_map for"
182                        " processor %s with %d thread(s)\n", np->name,
183                        nthreads);
184                 goto out_unlock;
185         }
186
187         for_each_cpu_mask(cpu, tmp) {
188                 BUG_ON(cpu_isset(cpu, cpu_present_map));
189                 cpu_set(cpu, cpu_present_map);
190                 set_hard_smp_processor_id(cpu, *intserv++);
191         }
192         err = 0;
193 out_unlock:
194         unlock_cpu_hotplug();
195         return err;
196 }
197
198 /*
199  * Update the present map for a cpu node which is going away, and set
200  * the hard id in the paca(s) to -1 to be consistent with boot time
201  * convention for non-present cpus.
202  */
203 static void pSeries_remove_processor(struct device_node *np)
204 {
205         unsigned int cpu;
206         int len, nthreads, i;
207         u32 *intserv;
208
209         intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
210         if (!intserv)
211                 return;
212
213         nthreads = len / sizeof(u32);
214
215         lock_cpu_hotplug();
216         for (i = 0; i < nthreads; i++) {
217                 for_each_present_cpu(cpu) {
218                         if (get_hard_smp_processor_id(cpu) != intserv[i])
219                                 continue;
220                         BUG_ON(cpu_online(cpu));
221                         cpu_clear(cpu, cpu_present_map);
222                         set_hard_smp_processor_id(cpu, -1);
223                         break;
224                 }
225                 if (cpu == NR_CPUS)
226                         printk(KERN_WARNING "Could not find cpu to remove "
227                                "with physical id 0x%x\n", intserv[i]);
228         }
229         unlock_cpu_hotplug();
230 }
231
232 static int pSeries_smp_notifier(struct notifier_block *nb, unsigned long action, void *node)
233 {
234         int err = NOTIFY_OK;
235
236         switch (action) {
237         case PSERIES_RECONFIG_ADD:
238                 if (pSeries_add_processor(node))
239                         err = NOTIFY_BAD;
240                 break;
241         case PSERIES_RECONFIG_REMOVE:
242                 pSeries_remove_processor(node);
243                 break;
244         default:
245                 err = NOTIFY_DONE;
246                 break;
247         }
248         return err;
249 }
250
251 static struct notifier_block pSeries_smp_nb = {
252         .notifier_call = pSeries_smp_notifier,
253 };
254
255 #endif /* CONFIG_HOTPLUG_CPU */
256
257 /**
258  * smp_startup_cpu() - start the given cpu
259  *
260  * At boot time, there is nothing to do for primary threads which were
261  * started from Open Firmware.  For anything else, call RTAS with the
262  * appropriate start location.
263  *
264  * Returns:
265  *      0       - failure
266  *      1       - success
267  */
268 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
269 {
270         int status;
271         unsigned long start_here = __pa((u32)*((unsigned long *)
272                                                pSeries_secondary_smp_init));
273         unsigned int pcpu;
274
275         if (cpu_isset(lcpu, of_spin_map))
276                 /* Already started by OF and sitting in spin loop */
277                 return 1;
278
279         pcpu = get_hard_smp_processor_id(lcpu);
280
281         /* Fixup atomic count: it exited inside IRQ handler. */
282         paca[lcpu].__current->thread_info->preempt_count        = 0;
283
284         status = rtas_call(rtas_token("start-cpu"), 3, 1, NULL,
285                            pcpu, start_here, lcpu);
286         if (status != 0) {
287                 printk(KERN_ERR "start-cpu failed: %i\n", status);
288                 return 0;
289         }
290         return 1;
291 }
292
293 #ifdef CONFIG_XICS
294 static inline void smp_xics_do_message(int cpu, int msg)
295 {
296         set_bit(msg, &xics_ipi_message[cpu].value);
297         mb();
298         xics_cause_IPI(cpu);
299 }
300
301 static void smp_xics_message_pass(int target, int msg)
302 {
303         unsigned int i;
304
305         if (target < NR_CPUS) {
306                 smp_xics_do_message(target, msg);
307         } else {
308                 for_each_online_cpu(i) {
309                         if (target == MSG_ALL_BUT_SELF
310                             && i == smp_processor_id())
311                                 continue;
312                         smp_xics_do_message(i, msg);
313                 }
314         }
315 }
316
317 static int __init smp_xics_probe(void)
318 {
319         xics_request_IPIs();
320
321         return cpus_weight(cpu_possible_map);
322 }
323
324 static void __devinit smp_xics_setup_cpu(int cpu)
325 {
326         if (cpu != boot_cpuid)
327                 xics_setup_cpu();
328
329         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
330                 vpa_init(cpu);
331
332         cpu_clear(cpu, of_spin_map);
333
334 }
335 #endif /* CONFIG_XICS */
336 #ifdef CONFIG_BPA_IIC
337 static void smp_iic_message_pass(int target, int msg)
338 {
339         unsigned int i;
340
341         if (target < NR_CPUS) {
342                 iic_cause_IPI(target, msg);
343         } else {
344                 for_each_online_cpu(i) {
345                         if (target == MSG_ALL_BUT_SELF
346                             && i == smp_processor_id())
347                                 continue;
348                         iic_cause_IPI(i, msg);
349                 }
350         }
351 }
352
353 static int __init smp_iic_probe(void)
354 {
355         iic_request_IPIs();
356
357         return cpus_weight(cpu_possible_map);
358 }
359
360 static void __devinit smp_iic_setup_cpu(int cpu)
361 {
362         if (cpu != boot_cpuid)
363                 iic_setup_cpu();
364 }
365 #endif /* CONFIG_BPA_IIC */
366
367 static DEFINE_SPINLOCK(timebase_lock);
368 static unsigned long timebase = 0;
369
370 static void __devinit pSeries_give_timebase(void)
371 {
372         spin_lock(&timebase_lock);
373         rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
374         timebase = get_tb();
375         spin_unlock(&timebase_lock);
376
377         while (timebase)
378                 barrier();
379         rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
380 }
381
382 static void __devinit pSeries_take_timebase(void)
383 {
384         while (!timebase)
385                 barrier();
386         spin_lock(&timebase_lock);
387         set_tb(timebase >> 32, timebase & 0xffffffff);
388         timebase = 0;
389         spin_unlock(&timebase_lock);
390 }
391
392 static void __devinit smp_pSeries_kick_cpu(int nr)
393 {
394         BUG_ON(nr < 0 || nr >= NR_CPUS);
395
396         if (!smp_startup_cpu(nr))
397                 return;
398
399         /*
400          * The processor is currently spinning, waiting for the
401          * cpu_start field to become non-zero After we set cpu_start,
402          * the processor will continue on to secondary_start
403          */
404         paca[nr].cpu_start = 1;
405 }
406
407 static int smp_pSeries_cpu_bootable(unsigned int nr)
408 {
409         /* Special case - we inhibit secondary thread startup
410          * during boot if the user requests it.  Odd-numbered
411          * cpus are assumed to be secondary threads.
412          */
413         if (system_state < SYSTEM_RUNNING &&
414             cpu_has_feature(CPU_FTR_SMT) &&
415             !smt_enabled_at_boot && nr % 2 != 0)
416                 return 0;
417
418         return 1;
419 }
420 #ifdef CONFIG_MPIC
421 static struct smp_ops_t pSeries_mpic_smp_ops = {
422         .message_pass   = smp_mpic_message_pass,
423         .probe          = smp_mpic_probe,
424         .kick_cpu       = smp_pSeries_kick_cpu,
425         .setup_cpu      = smp_mpic_setup_cpu,
426 };
427 #endif
428 #ifdef CONFIG_XICS
429 static struct smp_ops_t pSeries_xics_smp_ops = {
430         .message_pass   = smp_xics_message_pass,
431         .probe          = smp_xics_probe,
432         .kick_cpu       = smp_pSeries_kick_cpu,
433         .setup_cpu      = smp_xics_setup_cpu,
434         .cpu_bootable   = smp_pSeries_cpu_bootable,
435 };
436 #endif
437 #ifdef CONFIG_BPA_IIC
438 static struct smp_ops_t bpa_iic_smp_ops = {
439         .message_pass   = smp_iic_message_pass,
440         .probe          = smp_iic_probe,
441         .kick_cpu       = smp_pSeries_kick_cpu,
442         .setup_cpu      = smp_iic_setup_cpu,
443         .cpu_bootable   = smp_pSeries_cpu_bootable,
444 };
445 #endif
446
447 /* This is called very early */
448 void __init smp_init_pSeries(void)
449 {
450         int i;
451
452         DBG(" -> smp_init_pSeries()\n");
453
454         switch (ppc64_interrupt_controller) {
455 #ifdef CONFIG_MPIC
456         case IC_OPEN_PIC:
457                 smp_ops = &pSeries_mpic_smp_ops;
458                 break;
459 #endif
460 #ifdef CONFIG_XICS
461         case IC_PPC_XIC:
462                 smp_ops = &pSeries_xics_smp_ops;
463                 break;
464 #endif
465 #ifdef CONFIG_BPA_IIC
466         case IC_BPA_IIC:
467                 smp_ops = &bpa_iic_smp_ops;
468                 break;
469 #endif
470         default:
471                 panic("Invalid interrupt controller");
472         }
473
474 #ifdef CONFIG_HOTPLUG_CPU
475         smp_ops->cpu_disable = pSeries_cpu_disable;
476         smp_ops->cpu_die = pSeries_cpu_die;
477
478         /* Processors can be added/removed only on LPAR */
479         if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
480                 pSeries_reconfig_notifier_register(&pSeries_smp_nb);
481 #endif
482
483         /* Mark threads which are still spinning in hold loops. */
484         if (cpu_has_feature(CPU_FTR_SMT)) {
485                 for_each_present_cpu(i) { 
486                         if (i % 2 == 0)
487                                 /*
488                                  * Even-numbered logical cpus correspond to
489                                  * primary threads.
490                                  */
491                                 cpu_set(i, of_spin_map);
492                 }
493         } else {
494                 of_spin_map = cpu_present_map;
495         }
496
497         cpu_clear(boot_cpuid, of_spin_map);
498
499         /* Non-lpar has additional take/give timebase */
500         if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
501                 smp_ops->give_timebase = pSeries_give_timebase;
502                 smp_ops->take_timebase = pSeries_take_timebase;
503         }
504
505         DBG(" <- smp_init_pSeries()\n");
506 }
507