blob: 3cf78a6cd27c32d55659cec05350ed28f8835a86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mackerras69a80d32005-10-10 22:03:41 +10002 * SMP support for pSeries 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>
David Gibson007e8f52005-10-28 15:35:50 +100042#include "xics.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/cputable.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100044#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/system.h>
46#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/pSeries_reconfig.h>
Stephen Rothwellbbeb3f42005-09-27 13:51:59 +100048#include <asm/mpic.h>
Paul Mackerras271c3f32005-11-11 23:04:40 +110049#include <asm/vdso_datapage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Michael Ellermana1218722005-11-03 15:33:31 +110051#include "plpar_wrappers.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef DEBUG
Michael Ellermanf9e4ec52005-11-15 15:16:38 +110054#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define DBG(fmt...) udbg_printf(fmt)
56#else
57#define DBG(fmt...)
58#endif
59
60/*
61 * The primary thread of each non-boot processor is recorded here before
62 * smp init.
63 */
64static cpumask_t of_spin_map;
65
66extern void pSeries_secondary_smp_init(unsigned long);
67
68#ifdef CONFIG_HOTPLUG_CPU
69
70/* Get state of physical CPU.
71 * Return codes:
72 * 0 - The processor is in the RTAS stopped state
73 * 1 - stop-self is in progress
74 * 2 - The processor is not in the RTAS stopped state
75 * -1 - Hardware Error
76 * -2 - Hardware Busy, Try again later.
77 */
78static int query_cpu_stopped(unsigned int pcpu)
79{
80 int cpu_status;
81 int status, qcss_tok;
82
83 qcss_tok = rtas_token("query-cpu-stopped-state");
84 if (qcss_tok == RTAS_UNKNOWN_SERVICE)
85 return -1;
86 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
87 if (status != 0) {
88 printk(KERN_ERR
89 "RTAS query-cpu-stopped-state failed: %i\n", status);
90 return status;
91 }
92
93 return cpu_status;
94}
95
Arnd Bergmann84461962006-01-11 00:00:02 +000096static int pSeries_cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Zwane Mwaikambof3705132005-06-25 14:54:50 -070098 int cpu = smp_processor_id();
99
100 cpu_clear(cpu, cpu_online_map);
Paul Mackerras271c3f32005-11-11 23:04:40 +1100101 vdso_data->processorCount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /*fix boot_cpuid here*/
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700104 if (cpu == boot_cpuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 boot_cpuid = any_online_cpu(cpu_online_map);
106
107 /* FIXME: abstract this to not be platform specific later on */
108 xics_migrate_irqs_away();
109 return 0;
110}
111
Arnd Bergmann84461962006-01-11 00:00:02 +0000112static void pSeries_cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 int tries;
115 int cpu_status;
116 unsigned int pcpu = get_hard_smp_processor_id(cpu);
117
118 for (tries = 0; tries < 25; tries++) {
119 cpu_status = query_cpu_stopped(pcpu);
120 if (cpu_status == 0 || cpu_status == -1)
121 break;
122 msleep(200);
123 }
124 if (cpu_status != 0) {
125 printk("Querying DEAD? cpu %i (%i) shows %i\n",
126 cpu, pcpu, cpu_status);
127 }
128
129 /* Isolation and deallocation are definatly done by
130 * drslot_chrp_cpu. If they were not they would be
131 * done here. Change isolate state to Isolate and
132 * change allocation-state to Unusable.
133 */
134 paca[cpu].cpu_start = 0;
135}
136
137/*
138 * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle
139 * here is that a cpu device node may represent up to two logical cpus
140 * in the SMT case. We must honor the assumption in other code that
141 * the logical ids for sibling SMT threads x and y are adjacent, such
142 * that x^1 == y and y^1 == x.
143 */
144static int pSeries_add_processor(struct device_node *np)
145{
146 unsigned int cpu;
147 cpumask_t candidate_map, tmp = CPU_MASK_NONE;
148 int err = -ENOSPC, len, nthreads, i;
149 u32 *intserv;
150
151 intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
152 if (!intserv)
153 return 0;
154
155 nthreads = len / sizeof(u32);
156 for (i = 0; i < nthreads; i++)
157 cpu_set(i, tmp);
158
159 lock_cpu_hotplug();
160
161 BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
162
163 /* Get a bitmap of unoccupied slots. */
164 cpus_xor(candidate_map, cpu_possible_map, cpu_present_map);
165 if (cpus_empty(candidate_map)) {
166 /* If we get here, it most likely means that NR_CPUS is
167 * less than the partition's max processors setting.
168 */
169 printk(KERN_ERR "Cannot add cpu %s; this system configuration"
170 " supports %d logical cpus.\n", np->full_name,
171 cpus_weight(cpu_possible_map));
172 goto out_unlock;
173 }
174
175 while (!cpus_empty(tmp))
176 if (cpus_subset(tmp, candidate_map))
177 /* Found a range where we can insert the new cpu(s) */
178 break;
179 else
180 cpus_shift_left(tmp, tmp, nthreads);
181
182 if (cpus_empty(tmp)) {
183 printk(KERN_ERR "Unable to find space in cpu_present_map for"
184 " processor %s with %d thread(s)\n", np->name,
185 nthreads);
186 goto out_unlock;
187 }
188
189 for_each_cpu_mask(cpu, tmp) {
190 BUG_ON(cpu_isset(cpu, cpu_present_map));
191 cpu_set(cpu, cpu_present_map);
192 set_hard_smp_processor_id(cpu, *intserv++);
193 }
194 err = 0;
195out_unlock:
196 unlock_cpu_hotplug();
197 return err;
198}
199
200/*
201 * Update the present map for a cpu node which is going away, and set
202 * the hard id in the paca(s) to -1 to be consistent with boot time
203 * convention for non-present cpus.
204 */
205static void pSeries_remove_processor(struct device_node *np)
206{
207 unsigned int cpu;
208 int len, nthreads, i;
209 u32 *intserv;
210
211 intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
212 if (!intserv)
213 return;
214
215 nthreads = len / sizeof(u32);
216
217 lock_cpu_hotplug();
218 for (i = 0; i < nthreads; i++) {
219 for_each_present_cpu(cpu) {
220 if (get_hard_smp_processor_id(cpu) != intserv[i])
221 continue;
222 BUG_ON(cpu_online(cpu));
223 cpu_clear(cpu, cpu_present_map);
224 set_hard_smp_processor_id(cpu, -1);
225 break;
226 }
227 if (cpu == NR_CPUS)
228 printk(KERN_WARNING "Could not find cpu to remove "
229 "with physical id 0x%x\n", intserv[i]);
230 }
231 unlock_cpu_hotplug();
232}
233
234static int pSeries_smp_notifier(struct notifier_block *nb, unsigned long action, void *node)
235{
236 int err = NOTIFY_OK;
237
238 switch (action) {
239 case PSERIES_RECONFIG_ADD:
240 if (pSeries_add_processor(node))
241 err = NOTIFY_BAD;
242 break;
243 case PSERIES_RECONFIG_REMOVE:
244 pSeries_remove_processor(node);
245 break;
246 default:
247 err = NOTIFY_DONE;
248 break;
249 }
250 return err;
251}
252
253static struct notifier_block pSeries_smp_nb = {
254 .notifier_call = pSeries_smp_notifier,
255};
256
257#endif /* CONFIG_HOTPLUG_CPU */
258
259/**
260 * smp_startup_cpu() - start the given cpu
261 *
262 * At boot time, there is nothing to do for primary threads which were
263 * started from Open Firmware. For anything else, call RTAS with the
264 * appropriate start location.
265 *
266 * Returns:
267 * 0 - failure
268 * 1 - success
269 */
270static inline int __devinit smp_startup_cpu(unsigned int lcpu)
271{
272 int status;
273 unsigned long start_here = __pa((u32)*((unsigned long *)
274 pSeries_secondary_smp_init));
275 unsigned int pcpu;
Anton Blanchard1ed2fd22005-09-12 13:17:27 +1000276 int start_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (cpu_isset(lcpu, of_spin_map))
279 /* Already started by OF and sitting in spin loop */
280 return 1;
281
282 pcpu = get_hard_smp_processor_id(lcpu);
283
284 /* Fixup atomic count: it exited inside IRQ handler. */
Al Virob5e2fc12006-01-12 01:06:01 -0800285 task_thread_info(paca[lcpu].__current)->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Anton Blanchard1ed2fd22005-09-12 13:17:27 +1000287 /*
288 * If the RTAS start-cpu token does not exist then presume the
289 * cpu is already spinning.
290 */
291 start_cpu = rtas_token("start-cpu");
292 if (start_cpu == RTAS_UNKNOWN_SERVICE)
293 return 1;
294
Michael Ellerman496b7a52006-02-16 14:13:53 +1100295 status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (status != 0) {
297 printk(KERN_ERR "start-cpu failed: %i\n", status);
298 return 0;
299 }
Anton Blanchard1ed2fd22005-09-12 13:17:27 +1000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 1;
302}
303
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000304#ifdef CONFIG_XICS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static inline void smp_xics_do_message(int cpu, int msg)
306{
307 set_bit(msg, &xics_ipi_message[cpu].value);
308 mb();
309 xics_cause_IPI(cpu);
310}
311
312static void smp_xics_message_pass(int target, int msg)
313{
314 unsigned int i;
315
316 if (target < NR_CPUS) {
317 smp_xics_do_message(target, msg);
318 } else {
319 for_each_online_cpu(i) {
320 if (target == MSG_ALL_BUT_SELF
321 && i == smp_processor_id())
322 continue;
323 smp_xics_do_message(i, msg);
324 }
325 }
326}
327
328static int __init smp_xics_probe(void)
329{
330 xics_request_IPIs();
331
332 return cpus_weight(cpu_possible_map);
333}
334
335static void __devinit smp_xics_setup_cpu(int cpu)
336{
337 if (cpu != boot_cpuid)
338 xics_setup_cpu();
339
Stephen Rothwell1ababe12005-08-03 14:35:25 +1000340 if (firmware_has_feature(FW_FEATURE_SPLPAR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 vpa_init(cpu);
342
343 cpu_clear(cpu, of_spin_map);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000346#endif /* CONFIG_XICS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348static DEFINE_SPINLOCK(timebase_lock);
349static unsigned long timebase = 0;
350
351static void __devinit pSeries_give_timebase(void)
352{
353 spin_lock(&timebase_lock);
354 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
355 timebase = get_tb();
356 spin_unlock(&timebase_lock);
357
358 while (timebase)
359 barrier();
360 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
361}
362
363static void __devinit pSeries_take_timebase(void)
364{
365 while (!timebase)
366 barrier();
367 spin_lock(&timebase_lock);
368 set_tb(timebase >> 32, timebase & 0xffffffff);
369 timebase = 0;
370 spin_unlock(&timebase_lock);
371}
372
373static void __devinit smp_pSeries_kick_cpu(int nr)
374{
375 BUG_ON(nr < 0 || nr >= NR_CPUS);
376
377 if (!smp_startup_cpu(nr))
378 return;
379
380 /*
381 * The processor is currently spinning, waiting for the
382 * cpu_start field to become non-zero After we set cpu_start,
383 * the processor will continue on to secondary_start
384 */
385 paca[nr].cpu_start = 1;
386}
387
388static int smp_pSeries_cpu_bootable(unsigned int nr)
389{
390 /* Special case - we inhibit secondary thread startup
391 * during boot if the user requests it. Odd-numbered
392 * cpus are assumed to be secondary threads.
393 */
394 if (system_state < SYSTEM_RUNNING &&
Anton Blanchard0231c292005-06-20 21:43:15 +1000395 cpu_has_feature(CPU_FTR_SMT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 !smt_enabled_at_boot && nr % 2 != 0)
397 return 0;
398
399 return 1;
400}
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000401#ifdef CONFIG_MPIC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static struct smp_ops_t pSeries_mpic_smp_ops = {
403 .message_pass = smp_mpic_message_pass,
404 .probe = smp_mpic_probe,
405 .kick_cpu = smp_pSeries_kick_cpu,
406 .setup_cpu = smp_mpic_setup_cpu,
407};
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000408#endif
409#ifdef CONFIG_XICS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static struct smp_ops_t pSeries_xics_smp_ops = {
411 .message_pass = smp_xics_message_pass,
412 .probe = smp_xics_probe,
413 .kick_cpu = smp_pSeries_kick_cpu,
414 .setup_cpu = smp_xics_setup_cpu,
415 .cpu_bootable = smp_pSeries_cpu_bootable,
416};
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000417#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/* This is called very early */
420void __init smp_init_pSeries(void)
421{
422 int i;
423
424 DBG(" -> smp_init_pSeries()\n");
425
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000426 switch (ppc64_interrupt_controller) {
427#ifdef CONFIG_MPIC
428 case IC_OPEN_PIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 smp_ops = &pSeries_mpic_smp_ops;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000430 break;
431#endif
432#ifdef CONFIG_XICS
433 case IC_PPC_XIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 smp_ops = &pSeries_xics_smp_ops;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000435 break;
436#endif
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000437 default:
438 panic("Invalid interrupt controller");
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441#ifdef CONFIG_HOTPLUG_CPU
442 smp_ops->cpu_disable = pSeries_cpu_disable;
443 smp_ops->cpu_die = pSeries_cpu_die;
444
445 /* Processors can be added/removed only on LPAR */
Michael Ellerman57cfb812006-03-21 20:45:59 +1100446 if (firmware_has_feature(FW_FEATURE_LPAR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 pSeries_reconfig_notifier_register(&pSeries_smp_nb);
448#endif
449
450 /* Mark threads which are still spinning in hold loops. */
Anton Blanchard0231c292005-06-20 21:43:15 +1000451 if (cpu_has_feature(CPU_FTR_SMT)) {
452 for_each_present_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (i % 2 == 0)
454 /*
455 * Even-numbered logical cpus correspond to
456 * primary threads.
457 */
458 cpu_set(i, of_spin_map);
459 }
Anton Blanchard0231c292005-06-20 21:43:15 +1000460 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 of_spin_map = cpu_present_map;
Anton Blanchard0231c292005-06-20 21:43:15 +1000462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 cpu_clear(boot_cpuid, of_spin_map);
465
466 /* Non-lpar has additional take/give timebase */
467 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
468 smp_ops->give_timebase = pSeries_give_timebase;
469 smp_ops->take_timebase = pSeries_take_timebase;
470 }
471
472 DBG(" <- smp_init_pSeries()\n");
473}
474