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/firmware.h>
45 #include <asm/system.h>
47 #include <asm/plpar_wrappers.h>
48 #include <asm/pSeries_reconfig.h>
54 #define DBG(fmt...) udbg_printf(fmt)
60 * The primary thread of each non-boot processor is recorded here before
63 static cpumask_t of_spin_map
;
65 extern void pSeries_secondary_smp_init(unsigned long);
67 #ifdef CONFIG_HOTPLUG_CPU
69 /* Get state of physical CPU.
71 * 0 - The processor is in the RTAS stopped state
72 * 1 - stop-self is in progress
73 * 2 - The processor is not in the RTAS stopped state
75 * -2 - Hardware Busy, Try again later.
77 static int query_cpu_stopped(unsigned int pcpu
)
82 qcss_tok
= rtas_token("query-cpu-stopped-state");
83 if (qcss_tok
== RTAS_UNKNOWN_SERVICE
)
85 status
= rtas_call(qcss_tok
, 1, 2, &cpu_status
, pcpu
);
88 "RTAS query-cpu-stopped-state failed: %i\n", status
);
95 int pSeries_cpu_disable(void)
97 int cpu
= smp_processor_id();
99 cpu_clear(cpu
, cpu_online_map
);
100 systemcfg
->processorCount
--;
102 /*fix boot_cpuid here*/
103 if (cpu
== boot_cpuid
)
104 boot_cpuid
= any_online_cpu(cpu_online_map
);
106 /* FIXME: abstract this to not be platform specific later on */
107 xics_migrate_irqs_away();
111 void pSeries_cpu_die(unsigned int cpu
)
115 unsigned int pcpu
= get_hard_smp_processor_id(cpu
);
117 for (tries
= 0; tries
< 25; tries
++) {
118 cpu_status
= query_cpu_stopped(pcpu
);
119 if (cpu_status
== 0 || cpu_status
== -1)
123 if (cpu_status
!= 0) {
124 printk("Querying DEAD? cpu %i (%i) shows %i\n",
125 cpu
, pcpu
, cpu_status
);
128 /* Isolation and deallocation are definatly done by
129 * drslot_chrp_cpu. If they were not they would be
130 * done here. Change isolate state to Isolate and
131 * change allocation-state to Unusable.
133 paca
[cpu
].cpu_start
= 0;
137 * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle
138 * here is that a cpu device node may represent up to two logical cpus
139 * in the SMT case. We must honor the assumption in other code that
140 * the logical ids for sibling SMT threads x and y are adjacent, such
141 * that x^1 == y and y^1 == x.
143 static int pSeries_add_processor(struct device_node
*np
)
146 cpumask_t candidate_map
, tmp
= CPU_MASK_NONE
;
147 int err
= -ENOSPC
, len
, nthreads
, i
;
150 intserv
= (u32
*)get_property(np
, "ibm,ppc-interrupt-server#s", &len
);
154 nthreads
= len
/ sizeof(u32
);
155 for (i
= 0; i
< nthreads
; i
++)
160 BUG_ON(!cpus_subset(cpu_present_map
, cpu_possible_map
));
162 /* Get a bitmap of unoccupied slots. */
163 cpus_xor(candidate_map
, cpu_possible_map
, cpu_present_map
);
164 if (cpus_empty(candidate_map
)) {
165 /* If we get here, it most likely means that NR_CPUS is
166 * less than the partition's max processors setting.
168 printk(KERN_ERR
"Cannot add cpu %s; this system configuration"
169 " supports %d logical cpus.\n", np
->full_name
,
170 cpus_weight(cpu_possible_map
));
174 while (!cpus_empty(tmp
))
175 if (cpus_subset(tmp
, candidate_map
))
176 /* Found a range where we can insert the new cpu(s) */
179 cpus_shift_left(tmp
, tmp
, nthreads
);
181 if (cpus_empty(tmp
)) {
182 printk(KERN_ERR
"Unable to find space in cpu_present_map for"
183 " processor %s with %d thread(s)\n", np
->name
,
188 for_each_cpu_mask(cpu
, tmp
) {
189 BUG_ON(cpu_isset(cpu
, cpu_present_map
));
190 cpu_set(cpu
, cpu_present_map
);
191 set_hard_smp_processor_id(cpu
, *intserv
++);
195 unlock_cpu_hotplug();
200 * Update the present map for a cpu node which is going away, and set
201 * the hard id in the paca(s) to -1 to be consistent with boot time
202 * convention for non-present cpus.
204 static void pSeries_remove_processor(struct device_node
*np
)
207 int len
, nthreads
, i
;
210 intserv
= (u32
*)get_property(np
, "ibm,ppc-interrupt-server#s", &len
);
214 nthreads
= len
/ sizeof(u32
);
217 for (i
= 0; i
< nthreads
; i
++) {
218 for_each_present_cpu(cpu
) {
219 if (get_hard_smp_processor_id(cpu
) != intserv
[i
])
221 BUG_ON(cpu_online(cpu
));
222 cpu_clear(cpu
, cpu_present_map
);
223 set_hard_smp_processor_id(cpu
, -1);
227 printk(KERN_WARNING
"Could not find cpu to remove "
228 "with physical id 0x%x\n", intserv
[i
]);
230 unlock_cpu_hotplug();
233 static int pSeries_smp_notifier(struct notifier_block
*nb
, unsigned long action
, void *node
)
238 case PSERIES_RECONFIG_ADD
:
239 if (pSeries_add_processor(node
))
242 case PSERIES_RECONFIG_REMOVE
:
243 pSeries_remove_processor(node
);
252 static struct notifier_block pSeries_smp_nb
= {
253 .notifier_call
= pSeries_smp_notifier
,
256 #endif /* CONFIG_HOTPLUG_CPU */
259 * smp_startup_cpu() - start the given cpu
261 * At boot time, there is nothing to do for primary threads which were
262 * started from Open Firmware. For anything else, call RTAS with the
263 * appropriate start location.
269 static inline int __devinit
smp_startup_cpu(unsigned int lcpu
)
272 unsigned long start_here
= __pa((u32
)*((unsigned long *)
273 pSeries_secondary_smp_init
));
276 if (cpu_isset(lcpu
, of_spin_map
))
277 /* Already started by OF and sitting in spin loop */
280 pcpu
= get_hard_smp_processor_id(lcpu
);
282 /* Fixup atomic count: it exited inside IRQ handler. */
283 paca
[lcpu
].__current
->thread_info
->preempt_count
= 0;
285 status
= rtas_call(rtas_token("start-cpu"), 3, 1, NULL
,
286 pcpu
, start_here
, lcpu
);
288 printk(KERN_ERR
"start-cpu failed: %i\n", status
);
295 static inline void smp_xics_do_message(int cpu
, int msg
)
297 set_bit(msg
, &xics_ipi_message
[cpu
].value
);
302 static void smp_xics_message_pass(int target
, int msg
)
306 if (target
< NR_CPUS
) {
307 smp_xics_do_message(target
, msg
);
309 for_each_online_cpu(i
) {
310 if (target
== MSG_ALL_BUT_SELF
311 && i
== smp_processor_id())
313 smp_xics_do_message(i
, msg
);
318 static int __init
smp_xics_probe(void)
322 return cpus_weight(cpu_possible_map
);
325 static void __devinit
smp_xics_setup_cpu(int cpu
)
327 if (cpu
!= boot_cpuid
)
330 if (firmware_has_feature(FW_FEATURE_SPLPAR
))
333 cpu_clear(cpu
, of_spin_map
);
336 #endif /* CONFIG_XICS */
337 #ifdef CONFIG_BPA_IIC
338 static void smp_iic_message_pass(int target
, int msg
)
342 if (target
< NR_CPUS
) {
343 iic_cause_IPI(target
, msg
);
345 for_each_online_cpu(i
) {
346 if (target
== MSG_ALL_BUT_SELF
347 && i
== smp_processor_id())
349 iic_cause_IPI(i
, msg
);
354 static int __init
smp_iic_probe(void)
358 return cpus_weight(cpu_possible_map
);
361 static void __devinit
smp_iic_setup_cpu(int cpu
)
363 if (cpu
!= boot_cpuid
)
366 #endif /* CONFIG_BPA_IIC */
368 static DEFINE_SPINLOCK(timebase_lock
);
369 static unsigned long timebase
= 0;
371 static void __devinit
pSeries_give_timebase(void)
373 spin_lock(&timebase_lock
);
374 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL
);
376 spin_unlock(&timebase_lock
);
380 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL
);
383 static void __devinit
pSeries_take_timebase(void)
387 spin_lock(&timebase_lock
);
388 set_tb(timebase
>> 32, timebase
& 0xffffffff);
390 spin_unlock(&timebase_lock
);
393 static void __devinit
smp_pSeries_kick_cpu(int nr
)
395 BUG_ON(nr
< 0 || nr
>= NR_CPUS
);
397 if (!smp_startup_cpu(nr
))
401 * The processor is currently spinning, waiting for the
402 * cpu_start field to become non-zero After we set cpu_start,
403 * the processor will continue on to secondary_start
405 paca
[nr
].cpu_start
= 1;
408 static int smp_pSeries_cpu_bootable(unsigned int nr
)
410 /* Special case - we inhibit secondary thread startup
411 * during boot if the user requests it. Odd-numbered
412 * cpus are assumed to be secondary threads.
414 if (system_state
< SYSTEM_RUNNING
&&
415 cpu_has_feature(CPU_FTR_SMT
) &&
416 !smt_enabled_at_boot
&& nr
% 2 != 0)
422 static struct smp_ops_t pSeries_mpic_smp_ops
= {
423 .message_pass
= smp_mpic_message_pass
,
424 .probe
= smp_mpic_probe
,
425 .kick_cpu
= smp_pSeries_kick_cpu
,
426 .setup_cpu
= smp_mpic_setup_cpu
,
430 static struct smp_ops_t pSeries_xics_smp_ops
= {
431 .message_pass
= smp_xics_message_pass
,
432 .probe
= smp_xics_probe
,
433 .kick_cpu
= smp_pSeries_kick_cpu
,
434 .setup_cpu
= smp_xics_setup_cpu
,
435 .cpu_bootable
= smp_pSeries_cpu_bootable
,
438 #ifdef CONFIG_BPA_IIC
439 static struct smp_ops_t bpa_iic_smp_ops
= {
440 .message_pass
= smp_iic_message_pass
,
441 .probe
= smp_iic_probe
,
442 .kick_cpu
= smp_pSeries_kick_cpu
,
443 .setup_cpu
= smp_iic_setup_cpu
,
444 .cpu_bootable
= smp_pSeries_cpu_bootable
,
448 /* This is called very early */
449 void __init
smp_init_pSeries(void)
453 DBG(" -> smp_init_pSeries()\n");
455 switch (ppc64_interrupt_controller
) {
458 smp_ops
= &pSeries_mpic_smp_ops
;
463 smp_ops
= &pSeries_xics_smp_ops
;
466 #ifdef CONFIG_BPA_IIC
468 smp_ops
= &bpa_iic_smp_ops
;
472 panic("Invalid interrupt controller");
475 #ifdef CONFIG_HOTPLUG_CPU
476 smp_ops
->cpu_disable
= pSeries_cpu_disable
;
477 smp_ops
->cpu_die
= pSeries_cpu_die
;
479 /* Processors can be added/removed only on LPAR */
480 if (systemcfg
->platform
== PLATFORM_PSERIES_LPAR
)
481 pSeries_reconfig_notifier_register(&pSeries_smp_nb
);
484 /* Mark threads which are still spinning in hold loops. */
485 if (cpu_has_feature(CPU_FTR_SMT
)) {
486 for_each_present_cpu(i
) {
489 * Even-numbered logical cpus correspond to
492 cpu_set(i
, of_spin_map
);
495 of_spin_map
= cpu_present_map
;
498 cpu_clear(boot_cpuid
, of_spin_map
);
500 /* Non-lpar has additional take/give timebase */
501 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE
) {
502 smp_ops
->give_timebase
= pSeries_give_timebase
;
503 smp_ops
->take_timebase
= pSeries_take_timebase
;
506 DBG(" <- smp_init_pSeries()\n");