2 * SMP support for pSeries 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>
51 #define DBG(fmt...) udbg_printf(fmt)
56 extern void pSeries_secondary_smp_init(unsigned long);
58 /* Get state of physical CPU.
60 * 0 - The processor is in the RTAS stopped state
61 * 1 - stop-self is in progress
62 * 2 - The processor is not in the RTAS stopped state
64 * -2 - Hardware Busy, Try again later.
66 static int query_cpu_stopped(unsigned int pcpu)
71 qcss_tok = rtas_token("query-cpu-stopped-state");
72 if (qcss_tok == RTAS_UNKNOWN_SERVICE)
74 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
77 "RTAS query-cpu-stopped-state failed: %i\n", status);
85 #ifdef CONFIG_HOTPLUG_CPU
87 int pSeries_cpu_disable(void)
89 systemcfg->processorCount--;
91 /*fix boot_cpuid here*/
92 if (smp_processor_id() == boot_cpuid)
93 boot_cpuid = any_online_cpu(cpu_online_map);
95 /* FIXME: abstract this to not be platform specific later on */
96 xics_migrate_irqs_away();
100 void pSeries_cpu_die(unsigned int cpu)
104 unsigned int pcpu = get_hard_smp_processor_id(cpu);
106 for (tries = 0; tries < 25; tries++) {
107 cpu_status = query_cpu_stopped(pcpu);
108 if (cpu_status == 0 || cpu_status == -1)
112 if (cpu_status != 0) {
113 printk("Querying DEAD? cpu %i (%i) shows %i\n",
114 cpu, pcpu, cpu_status);
117 /* Isolation and deallocation are definatly done by
118 * drslot_chrp_cpu. If they were not they would be
119 * done here. Change isolate state to Isolate and
120 * change allocation-state to Unusable.
122 paca[cpu].cpu_start = 0;
125 /* Search all cpu device nodes for an offline logical cpu. If a
126 * device node has a "ibm,my-drc-index" property (meaning this is an
127 * LPAR), paranoid-check whether we own the cpu. For each "thread"
128 * of a cpu, if it is offline and has the same hw index as before,
129 * grab that in preference.
131 static unsigned int find_physical_cpu_to_start(unsigned int old_hwindex)
133 struct device_node *np = NULL;
134 unsigned int best = -1U;
136 while ((np = of_find_node_by_type(np, "cpu"))) {
138 u32 *index = (u32 *)get_property(np, "ibm,my-drc-index", NULL);
140 get_property(np, "ibm,ppc-interrupt-server#s", &len);
143 tid = (u32 *)get_property(np, "reg", &len);
148 /* If there is a drc-index, make sure that we own
153 int rc = rtas_get_sensor(9003, *index, &state);
154 if (rc != 0 || state != 1)
158 nr_threads = len / sizeof(u32);
160 while (nr_threads--) {
161 if (0 == query_cpu_stopped(tid[nr_threads])) {
162 best = tid[nr_threads];
163 if (best == old_hwindex)
174 * smp_startup_cpu() - start the given cpu
176 * At boot time, there is nothing to do. At run-time, call RTAS with
177 * the appropriate start location, if the cpu is in the RTAS stopped
184 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
187 unsigned long start_here = __pa((u32)*((unsigned long *)
188 pSeries_secondary_smp_init));
191 /* At boot time the cpus are already spinning in hold
192 * loops, so nothing to do. */
193 if (system_state < SYSTEM_RUNNING)
196 pcpu = find_physical_cpu_to_start(get_hard_smp_processor_id(lcpu));
198 printk(KERN_INFO "No more cpus available, failing\n");
202 /* Fixup atomic count: it exited inside IRQ handler. */
203 paca[lcpu].__current->thread_info->preempt_count = 0;
205 /* At boot this is done in prom.c. */
206 paca[lcpu].hw_cpu_id = pcpu;
208 status = rtas_call(rtas_token("start-cpu"), 3, 1, NULL,
209 pcpu, start_here, lcpu);
211 printk(KERN_ERR "start-cpu failed: %i\n", status);
216 #else /* ... CONFIG_HOTPLUG_CPU */
217 static inline int __devinit smp_startup_cpu(unsigned int lcpu)
221 #endif /* CONFIG_HOTPLUG_CPU */
223 static inline void smp_xics_do_message(int cpu, int msg)
225 set_bit(msg, &xics_ipi_message[cpu].value);
230 static void smp_xics_message_pass(int target, int msg)
234 if (target < NR_CPUS) {
235 smp_xics_do_message(target, msg);
237 for_each_online_cpu(i) {
238 if (target == MSG_ALL_BUT_SELF
239 && i == smp_processor_id())
241 smp_xics_do_message(i, msg);
246 static int __init smp_xics_probe(void)
250 return cpus_weight(cpu_possible_map);
253 static void __devinit smp_xics_setup_cpu(int cpu)
255 if (cpu != boot_cpuid)
258 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
262 * Put the calling processor into the GIQ. This is really only
263 * necessary from a secondary thread as the OF start-cpu interface
264 * performs this function for us on primary threads.
266 rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
267 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
270 static DEFINE_SPINLOCK(timebase_lock);
271 static unsigned long timebase = 0;
273 static void __devinit pSeries_give_timebase(void)
275 spin_lock(&timebase_lock);
276 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
278 spin_unlock(&timebase_lock);
282 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
285 static void __devinit pSeries_take_timebase(void)
289 spin_lock(&timebase_lock);
290 set_tb(timebase >> 32, timebase & 0xffffffff);
292 spin_unlock(&timebase_lock);
295 static void __devinit smp_pSeries_kick_cpu(int nr)
297 BUG_ON(nr < 0 || nr >= NR_CPUS);
299 if (!smp_startup_cpu(nr))
303 * The processor is currently spinning, waiting for the
304 * cpu_start field to become non-zero After we set cpu_start,
305 * the processor will continue on to secondary_start
307 paca[nr].cpu_start = 1;
310 static struct smp_ops_t pSeries_mpic_smp_ops = {
311 .message_pass = smp_mpic_message_pass,
312 .probe = smp_mpic_probe,
313 .kick_cpu = smp_pSeries_kick_cpu,
314 .setup_cpu = smp_mpic_setup_cpu,
317 static struct smp_ops_t pSeries_xics_smp_ops = {
318 .message_pass = smp_xics_message_pass,
319 .probe = smp_xics_probe,
320 .kick_cpu = smp_pSeries_kick_cpu,
321 .setup_cpu = smp_xics_setup_cpu,
324 /* This is called very early */
325 void __init smp_init_pSeries(void)
329 DBG(" -> smp_init_pSeries()\n");
331 if (ppc64_interrupt_controller == IC_OPEN_PIC)
332 smp_ops = &pSeries_mpic_smp_ops;
334 smp_ops = &pSeries_xics_smp_ops;
336 #ifdef CONFIG_HOTPLUG_CPU
337 smp_ops->cpu_disable = pSeries_cpu_disable;
338 smp_ops->cpu_die = pSeries_cpu_die;
341 /* Start secondary threads on SMT systems; primary threads
342 * are already in the running state.
344 for_each_present_cpu(i) {
345 if (query_cpu_stopped(get_hard_smp_processor_id(i)) == 0) {
346 printk("%16.16x : starting thread\n", i);
347 DBG("%16.16x : starting thread\n", i);
348 rtas_call(rtas_token("start-cpu"), 3, 1, &ret,
349 get_hard_smp_processor_id(i),
350 __pa((u32)*((unsigned long *)
351 pSeries_secondary_smp_init)),
356 /* Non-lpar has additional take/give timebase */
357 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
358 smp_ops->give_timebase = pSeries_give_timebase;
359 smp_ops->take_timebase = pSeries_take_timebase;
362 DBG(" <- smp_init_pSeries()\n");