2 * SMP support for pSeries and BPA machines.
4 * Dave Engebretsen, Peter Bergner, and
5 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
7 * Plus various changes from other IBM teams...
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.
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>
31 #include <asm/ptrace.h>
32 #include <asm/atomic.h>
35 #include <asm/pgtable.h>
41 #include <asm/machdep.h>
43 #include <asm/cputable.h>
44 #include <asm/system.h>
46 #include <asm/plpar_wrappers.h>
47 #include <asm/pSeries_reconfig.h>
53 #define DBG(fmt...) udbg_printf(fmt)
59 * The primary thread of each non-boot processor is recorded here before
62 static cpumask_t of_spin_map;
64 extern void pSeries_secondary_smp_init(unsigned long);
66 #ifdef CONFIG_HOTPLUG_CPU
68 /* Get state of physical CPU.
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
74 * -2 - Hardware Busy, Try again later.
76 static int query_cpu_stopped(unsigned int pcpu)
81 qcss_tok = rtas_token("query-cpu-stopped-state");
82 if (qcss_tok == RTAS_UNKNOWN_SERVICE)
84 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
87 "RTAS query-cpu-stopped-state failed: %i\n", status);
94 int pSeries_cpu_disable(void)
96 systemcfg->processorCount--;
98 /*fix boot_cpuid here*/
99 if (smp_processor_id() == boot_cpuid)
100 boot_cpuid = any_online_cpu(cpu_online_map);
102 /* FIXME: abstract this to not be platform specific later on */
103 xics_migrate_irqs_away();
107 void pSeries_cpu_die(unsigned int cpu)
111 unsigned int pcpu = get_hard_smp_processor_id(cpu);
113 for (tries = 0; tries < 25; tries++) {
114 cpu_status = query_cpu_stopped(pcpu);
115 if (cpu_status == 0 || cpu_status == -1)
119 if (cpu_status != 0) {
120 printk("Querying DEAD? cpu %i (%i) shows %i\n",
121 cpu, pcpu, cpu_status);
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.
129 paca[cpu].cpu_start = 0;
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.
139 static int pSeries_add_processor(struct device_node *np)
142 cpumask_t candidate_map, tmp = CPU_MASK_NONE;
143 int err = -ENOSPC, len, nthreads, i;
146 intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
150 nthreads = len / sizeof(u32);
151 for (i = 0; i < nthreads; i++)
156 BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
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.
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));
170 while (!cpus_empty(tmp))
171 if (cpus_subset(tmp, candidate_map))
172 /* Found a range where we can insert the new cpu(s) */
175 cpus_shift_left(tmp, tmp, nthreads);
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,
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++);
191 unlock_cpu_hotplug();
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.
200 static void pSeries_remove_processor(struct device_node *np)
203 int len, nthreads, i;
206 intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
210 nthreads = len / sizeof(u32);
213 for (i = 0; i < nthreads; i++) {
214 for_each_present_cpu(cpu) {
215 if (get_hard_smp_processor_id(cpu) != intserv[i])
217 BUG_ON(cpu_online(cpu));
218 cpu_clear(cpu, cpu_present_map);
219 set_hard_smp_processor_id(cpu, -1);
223 printk(KERN_WARNING "Could not find cpu to remove "
224 "with physical id 0x%x\n", intserv[i]);
226 unlock_cpu_hotplug();
229 static int pSeries_smp_notifier(struct notifier_block *nb, unsigned long action, void *node)
234 case PSERIES_RECONFIG_ADD:
235 if (pSeries_add_processor(node))
238 case PSERIES_RECONFIG_REMOVE:
239 pSeries_remove_processor(node);
248 static struct notifier_block pSeries_smp_nb = {
249 .notifier_call = pSeries_smp_notifier,
252 #endif /* CONFIG_HOTPLUG_CPU */
255 * smp_startup_cpu() - start the given cpu
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.
265 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
268 unsigned long start_here = __pa((u32)*((unsigned long *)
269 pSeries_secondary_smp_init));
272 if (cpu_isset(lcpu, of_spin_map))
273 /* Already started by OF and sitting in spin loop */
276 pcpu = get_hard_smp_processor_id(lcpu);
278 /* Fixup atomic count: it exited inside IRQ handler. */
279 paca[lcpu].__current->thread_info->preempt_count = 0;
281 status = rtas_call(rtas_token("start-cpu"), 3, 1, NULL,
282 pcpu, start_here, lcpu);
284 printk(KERN_ERR "start-cpu failed: %i\n", status);
291 static inline void smp_xics_do_message(int cpu, int msg)
293 set_bit(msg, &xics_ipi_message[cpu].value);
298 static void smp_xics_message_pass(int target, int msg)
302 if (target < NR_CPUS) {
303 smp_xics_do_message(target, msg);
305 for_each_online_cpu(i) {
306 if (target == MSG_ALL_BUT_SELF
307 && i == smp_processor_id())
309 smp_xics_do_message(i, msg);
314 static int __init smp_xics_probe(void)
318 return cpus_weight(cpu_possible_map);
321 static void __devinit smp_xics_setup_cpu(int cpu)
323 if (cpu != boot_cpuid)
326 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
329 cpu_clear(cpu, of_spin_map);
332 #endif /* CONFIG_XICS */
333 #ifdef CONFIG_BPA_IIC
334 static void smp_iic_message_pass(int target, int msg)
338 if (target < NR_CPUS) {
339 iic_cause_IPI(target, msg);
341 for_each_online_cpu(i) {
342 if (target == MSG_ALL_BUT_SELF
343 && i == smp_processor_id())
345 iic_cause_IPI(i, msg);
350 static int __init smp_iic_probe(void)
354 return cpus_weight(cpu_possible_map);
357 static void __devinit smp_iic_setup_cpu(int cpu)
359 if (cpu != boot_cpuid)
362 #endif /* CONFIG_BPA_IIC */
364 static DEFINE_SPINLOCK(timebase_lock);
365 static unsigned long timebase = 0;
367 static void __devinit pSeries_give_timebase(void)
369 spin_lock(&timebase_lock);
370 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
372 spin_unlock(&timebase_lock);
376 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
379 static void __devinit pSeries_take_timebase(void)
383 spin_lock(&timebase_lock);
384 set_tb(timebase >> 32, timebase & 0xffffffff);
386 spin_unlock(&timebase_lock);
389 static void __devinit smp_pSeries_kick_cpu(int nr)
391 BUG_ON(nr < 0 || nr >= NR_CPUS);
393 if (!smp_startup_cpu(nr))
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
401 paca[nr].cpu_start = 1;
404 static int smp_pSeries_cpu_bootable(unsigned int nr)
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.
410 if (system_state < SYSTEM_RUNNING &&
411 cpu_has_feature(CPU_FTR_SMT) &&
412 !smt_enabled_at_boot && nr % 2 != 0)
418 static 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,
426 static 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,
434 #ifdef CONFIG_BPA_IIC
435 static 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,
444 /* This is called very early */
445 void __init smp_init_pSeries(void)
449 DBG(" -> smp_init_pSeries()\n");
451 switch (ppc64_interrupt_controller) {
454 smp_ops = &pSeries_mpic_smp_ops;
459 smp_ops = &pSeries_xics_smp_ops;
462 #ifdef CONFIG_BPA_IIC
464 smp_ops = &bpa_iic_smp_ops;
468 panic("Invalid interrupt controller");
471 #ifdef CONFIG_HOTPLUG_CPU
472 smp_ops->cpu_disable = pSeries_cpu_disable;
473 smp_ops->cpu_die = pSeries_cpu_die;
475 /* Processors can be added/removed only on LPAR */
476 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
477 pSeries_reconfig_notifier_register(&pSeries_smp_nb);
480 /* Mark threads which are still spinning in hold loops. */
481 if (cpu_has_feature(CPU_FTR_SMT)) {
482 for_each_present_cpu(i) {
485 * Even-numbered logical cpus correspond to
488 cpu_set(i, of_spin_map);
491 of_spin_map = cpu_present_map;
494 cpu_clear(boot_cpuid, of_spin_map);
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;
502 DBG(" <- smp_init_pSeries()\n");