blob: e9bc2a528e5d7806d7c661e5a9224870a2abb689 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/smp.h>
21#include <linux/interrupt.h>
22#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/spinlock.h>
25#include <linux/cache.h>
26#include <linux/err.h>
27#include <linux/sysdev.h>
28#include <linux/cpu.h>
29
30#include <asm/ptrace.h>
31#include <asm/atomic.h>
32#include <asm/irq.h>
33#include <asm/page.h>
34#include <asm/pgtable.h>
35#include <asm/io.h>
36#include <asm/prom.h>
37#include <asm/smp.h>
38#include <asm/paca.h>
39#include <asm/time.h>
40#include <asm/machdep.h>
David Gibson007e8f52005-10-28 15:35:50 +100041#include "xics.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/cputable.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100043#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/system.h>
45#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/pSeries_reconfig.h>
Stephen Rothwellbbeb3f42005-09-27 13:51:59 +100047#include <asm/mpic.h>
Paul Mackerras271c3f32005-11-11 23:04:40 +110048#include <asm/vdso_datapage.h>
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100049#include <asm/cputhreads.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Michael Ellermana1218722005-11-03 15:33:31 +110051#include "plpar_wrappers.h"
Michael Ellerman8feaeca2007-02-08 18:33:55 +110052#include "pseries.h"
Michael Ellermana1218722005-11-03 15:33:31 +110053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * The primary thread of each non-boot processor is recorded here before
57 * smp init.
58 */
59static cpumask_t of_spin_map;
60
Olof Johanssonf39b7a52006-08-11 00:07:08 -050061extern void generic_secondary_smp_init(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/**
64 * smp_startup_cpu() - start the given cpu
65 *
66 * At boot time, there is nothing to do for primary threads which were
67 * started from Open Firmware. For anything else, call RTAS with the
68 * appropriate start location.
69 *
70 * Returns:
71 * 0 - failure
72 * 1 - success
73 */
74static inline int __devinit smp_startup_cpu(unsigned int lcpu)
75{
76 int status;
77 unsigned long start_here = __pa((u32)*((unsigned long *)
Olof Johanssonf39b7a52006-08-11 00:07:08 -050078 generic_secondary_smp_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned int pcpu;
Anton Blanchard1ed2fd22005-09-12 13:17:27 +100080 int start_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (cpu_isset(lcpu, of_spin_map))
83 /* Already started by OF and sitting in spin loop */
84 return 1;
85
86 pcpu = get_hard_smp_processor_id(lcpu);
87
88 /* Fixup atomic count: it exited inside IRQ handler. */
Al Virob5e2fc12006-01-12 01:06:01 -080089 task_thread_info(paca[lcpu].__current)->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Anton Blanchard1ed2fd22005-09-12 13:17:27 +100091 /*
92 * If the RTAS start-cpu token does not exist then presume the
93 * cpu is already spinning.
94 */
95 start_cpu = rtas_token("start-cpu");
96 if (start_cpu == RTAS_UNKNOWN_SERVICE)
97 return 1;
98
Michael Ellerman496b7a52006-02-16 14:13:53 +110099 status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (status != 0) {
101 printk(KERN_ERR "start-cpu failed: %i\n", status);
102 return 0;
103 }
Anton Blanchard1ed2fd22005-09-12 13:17:27 +1000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 1;
106}
107
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000108#ifdef CONFIG_XICS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static inline void smp_xics_do_message(int cpu, int msg)
110{
111 set_bit(msg, &xics_ipi_message[cpu].value);
112 mb();
113 xics_cause_IPI(cpu);
114}
115
116static void smp_xics_message_pass(int target, int msg)
117{
118 unsigned int i;
119
120 if (target < NR_CPUS) {
121 smp_xics_do_message(target, msg);
122 } else {
123 for_each_online_cpu(i) {
124 if (target == MSG_ALL_BUT_SELF
125 && i == smp_processor_id())
126 continue;
127 smp_xics_do_message(i, msg);
128 }
129 }
130}
131
132static int __init smp_xics_probe(void)
133{
134 xics_request_IPIs();
135
136 return cpus_weight(cpu_possible_map);
137}
138
139static void __devinit smp_xics_setup_cpu(int cpu)
140{
141 if (cpu != boot_cpuid)
142 xics_setup_cpu();
143
Stephen Rothwell1ababe12005-08-03 14:35:25 +1000144 if (firmware_has_feature(FW_FEATURE_SPLPAR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 vpa_init(cpu);
146
147 cpu_clear(cpu, of_spin_map);
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000150#endif /* CONFIG_XICS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static DEFINE_SPINLOCK(timebase_lock);
153static unsigned long timebase = 0;
154
155static void __devinit pSeries_give_timebase(void)
156{
157 spin_lock(&timebase_lock);
158 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
159 timebase = get_tb();
160 spin_unlock(&timebase_lock);
161
162 while (timebase)
163 barrier();
164 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
165}
166
167static void __devinit pSeries_take_timebase(void)
168{
169 while (!timebase)
170 barrier();
171 spin_lock(&timebase_lock);
172 set_tb(timebase >> 32, timebase & 0xffffffff);
173 timebase = 0;
174 spin_unlock(&timebase_lock);
175}
176
177static void __devinit smp_pSeries_kick_cpu(int nr)
178{
179 BUG_ON(nr < 0 || nr >= NR_CPUS);
180
181 if (!smp_startup_cpu(nr))
182 return;
183
184 /*
185 * The processor is currently spinning, waiting for the
186 * cpu_start field to become non-zero After we set cpu_start,
187 * the processor will continue on to secondary_start
188 */
189 paca[nr].cpu_start = 1;
190}
191
192static int smp_pSeries_cpu_bootable(unsigned int nr)
193{
194 /* Special case - we inhibit secondary thread startup
195 * during boot if the user requests it. Odd-numbered
196 * cpus are assumed to be secondary threads.
197 */
198 if (system_state < SYSTEM_RUNNING &&
Anton Blanchard0231c292005-06-20 21:43:15 +1000199 cpu_has_feature(CPU_FTR_SMT) &&
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +1000200 !smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202
203 return 1;
204}
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000205#ifdef CONFIG_MPIC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static struct smp_ops_t pSeries_mpic_smp_ops = {
207 .message_pass = smp_mpic_message_pass,
208 .probe = smp_mpic_probe,
209 .kick_cpu = smp_pSeries_kick_cpu,
210 .setup_cpu = smp_mpic_setup_cpu,
211};
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000212#endif
213#ifdef CONFIG_XICS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static struct smp_ops_t pSeries_xics_smp_ops = {
215 .message_pass = smp_xics_message_pass,
216 .probe = smp_xics_probe,
217 .kick_cpu = smp_pSeries_kick_cpu,
218 .setup_cpu = smp_xics_setup_cpu,
219 .cpu_bootable = smp_pSeries_cpu_bootable,
220};
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223/* This is called very early */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000224static void __init smp_init_pseries(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int i;
227
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000228 pr_debug(" -> smp_init_pSeries()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Mark threads which are still spinning in hold loops. */
Anton Blanchard0231c292005-06-20 21:43:15 +1000231 if (cpu_has_feature(CPU_FTR_SMT)) {
232 for_each_present_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (i % 2 == 0)
234 /*
235 * Even-numbered logical cpus correspond to
236 * primary threads.
237 */
238 cpu_set(i, of_spin_map);
239 }
Anton Blanchard0231c292005-06-20 21:43:15 +1000240 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 of_spin_map = cpu_present_map;
Anton Blanchard0231c292005-06-20 21:43:15 +1000242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 cpu_clear(boot_cpuid, of_spin_map);
245
246 /* Non-lpar has additional take/give timebase */
247 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
248 smp_ops->give_timebase = pSeries_give_timebase;
249 smp_ops->take_timebase = pSeries_take_timebase;
250 }
251
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000252 pr_debug(" <- smp_init_pSeries()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000255#ifdef CONFIG_MPIC
256void __init smp_init_pseries_mpic(void)
257{
258 smp_ops = &pSeries_mpic_smp_ops;
259
260 smp_init_pseries();
261}
262#endif
263
264void __init smp_init_pseries_xics(void)
265{
266 smp_ops = &pSeries_xics_smp_ops;
267
268 smp_init_pseries();
269}