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