Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
69a80d3f | 2 | * SMP support for pSeries machines. |
1da177e4 LT |
3 | * |
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 | ||
1da177e4 LT |
17 | #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> | |
007e8f51 | 41 | #include "xics.h" |
1da177e4 | 42 | #include <asm/cputable.h> |
1ababe11 | 43 | #include <asm/firmware.h> |
1da177e4 LT |
44 | #include <asm/system.h> |
45 | #include <asm/rtas.h> | |
1da177e4 | 46 | #include <asm/pSeries_reconfig.h> |
bbeb3f4c | 47 | #include <asm/mpic.h> |
271c3f35 | 48 | #include <asm/vdso_datapage.h> |
1da177e4 | 49 | |
a1218720 ME |
50 | #include "plpar_wrappers.h" |
51 | ||
1da177e4 | 52 | #ifdef DEBUG |
f9e4ec57 | 53 | #include <asm/udbg.h> |
1da177e4 LT |
54 | #define DBG(fmt...) udbg_printf(fmt) |
55 | #else | |
56 | #define DBG(fmt...) | |
57 | #endif | |
58 | ||
59 | /* | |
60 | * The primary thread of each non-boot processor is recorded here before | |
61 | * smp init. | |
62 | */ | |
63 | static cpumask_t of_spin_map; | |
64 | ||
65 | extern void pSeries_secondary_smp_init(unsigned long); | |
66 | ||
67 | #ifdef CONFIG_HOTPLUG_CPU | |
68 | ||
69 | /* Get state of physical CPU. | |
70 | * Return codes: | |
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 | |
74 | * -1 - Hardware Error | |
75 | * -2 - Hardware Busy, Try again later. | |
76 | */ | |
77 | static int query_cpu_stopped(unsigned int pcpu) | |
78 | { | |
79 | int cpu_status; | |
80 | int status, qcss_tok; | |
81 | ||
82 | qcss_tok = rtas_token("query-cpu-stopped-state"); | |
83 | if (qcss_tok == RTAS_UNKNOWN_SERVICE) | |
84 | return -1; | |
85 | status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu); | |
86 | if (status != 0) { | |
87 | printk(KERN_ERR | |
88 | "RTAS query-cpu-stopped-state failed: %i\n", status); | |
89 | return status; | |
90 | } | |
91 | ||
92 | return cpu_status; | |
93 | } | |
94 | ||
8446196a | 95 | static int pSeries_cpu_disable(void) |
1da177e4 | 96 | { |
f3705136 ZM |
97 | int cpu = smp_processor_id(); |
98 | ||
99 | cpu_clear(cpu, cpu_online_map); | |
271c3f35 | 100 | vdso_data->processorCount--; |
1da177e4 LT |
101 | |
102 | /*fix boot_cpuid here*/ | |
f3705136 | 103 | if (cpu == boot_cpuid) |
1da177e4 LT |
104 | boot_cpuid = any_online_cpu(cpu_online_map); |
105 | ||
106 | /* FIXME: abstract this to not be platform specific later on */ | |
107 | xics_migrate_irqs_away(); | |
108 | return 0; | |
109 | } | |
110 | ||
8446196a | 111 | static void pSeries_cpu_die(unsigned int cpu) |
1da177e4 LT |
112 | { |
113 | int tries; | |
114 | int cpu_status; | |
115 | unsigned int pcpu = get_hard_smp_processor_id(cpu); | |
116 | ||
117 | for (tries = 0; tries < 25; tries++) { | |
118 | cpu_status = query_cpu_stopped(pcpu); | |
119 | if (cpu_status == 0 || cpu_status == -1) | |
120 | break; | |
121 | msleep(200); | |
122 | } | |
123 | if (cpu_status != 0) { | |
124 | printk("Querying DEAD? cpu %i (%i) shows %i\n", | |
125 | cpu, pcpu, cpu_status); | |
126 | } | |
127 | ||
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. | |
132 | */ | |
133 | paca[cpu].cpu_start = 0; | |
134 | } | |
135 | ||
136 | /* | |
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. | |
142 | */ | |
143 | static int pSeries_add_processor(struct device_node *np) | |
144 | { | |
145 | unsigned int cpu; | |
146 | cpumask_t candidate_map, tmp = CPU_MASK_NONE; | |
147 | int err = -ENOSPC, len, nthreads, i; | |
148 | u32 *intserv; | |
149 | ||
150 | intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len); | |
151 | if (!intserv) | |
152 | return 0; | |
153 | ||
154 | nthreads = len / sizeof(u32); | |
155 | for (i = 0; i < nthreads; i++) | |
156 | cpu_set(i, tmp); | |
157 | ||
158 | lock_cpu_hotplug(); | |
159 | ||
160 | BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map)); | |
161 | ||
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. | |
167 | */ | |
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)); | |
171 | goto out_unlock; | |
172 | } | |
173 | ||
174 | while (!cpus_empty(tmp)) | |
175 | if (cpus_subset(tmp, candidate_map)) | |
176 | /* Found a range where we can insert the new cpu(s) */ | |
177 | break; | |
178 | else | |
179 | cpus_shift_left(tmp, tmp, nthreads); | |
180 | ||
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, | |
184 | nthreads); | |
185 | goto out_unlock; | |
186 | } | |
187 | ||
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++); | |
192 | } | |
193 | err = 0; | |
194 | out_unlock: | |
195 | unlock_cpu_hotplug(); | |
196 | return err; | |
197 | } | |
198 | ||
199 | /* | |
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. | |
203 | */ | |
204 | static void pSeries_remove_processor(struct device_node *np) | |
205 | { | |
206 | unsigned int cpu; | |
207 | int len, nthreads, i; | |
208 | u32 *intserv; | |
209 | ||
210 | intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len); | |
211 | if (!intserv) | |
212 | return; | |
213 | ||
214 | nthreads = len / sizeof(u32); | |
215 | ||
216 | lock_cpu_hotplug(); | |
217 | for (i = 0; i < nthreads; i++) { | |
218 | for_each_present_cpu(cpu) { | |
219 | if (get_hard_smp_processor_id(cpu) != intserv[i]) | |
220 | continue; | |
221 | BUG_ON(cpu_online(cpu)); | |
222 | cpu_clear(cpu, cpu_present_map); | |
223 | set_hard_smp_processor_id(cpu, -1); | |
224 | break; | |
225 | } | |
226 | if (cpu == NR_CPUS) | |
227 | printk(KERN_WARNING "Could not find cpu to remove " | |
228 | "with physical id 0x%x\n", intserv[i]); | |
229 | } | |
230 | unlock_cpu_hotplug(); | |
231 | } | |
232 | ||
233 | static int pSeries_smp_notifier(struct notifier_block *nb, unsigned long action, void *node) | |
234 | { | |
235 | int err = NOTIFY_OK; | |
236 | ||
237 | switch (action) { | |
238 | case PSERIES_RECONFIG_ADD: | |
239 | if (pSeries_add_processor(node)) | |
240 | err = NOTIFY_BAD; | |
241 | break; | |
242 | case PSERIES_RECONFIG_REMOVE: | |
243 | pSeries_remove_processor(node); | |
244 | break; | |
245 | default: | |
246 | err = NOTIFY_DONE; | |
247 | break; | |
248 | } | |
249 | return err; | |
250 | } | |
251 | ||
252 | static struct notifier_block pSeries_smp_nb = { | |
253 | .notifier_call = pSeries_smp_notifier, | |
254 | }; | |
255 | ||
256 | #endif /* CONFIG_HOTPLUG_CPU */ | |
257 | ||
258 | /** | |
259 | * smp_startup_cpu() - start the given cpu | |
260 | * | |
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. | |
264 | * | |
265 | * Returns: | |
266 | * 0 - failure | |
267 | * 1 - success | |
268 | */ | |
269 | static inline int __devinit smp_startup_cpu(unsigned int lcpu) | |
270 | { | |
271 | int status; | |
272 | unsigned long start_here = __pa((u32)*((unsigned long *) | |
273 | pSeries_secondary_smp_init)); | |
274 | unsigned int pcpu; | |
1ed2fd2d | 275 | int start_cpu; |
1da177e4 LT |
276 | |
277 | if (cpu_isset(lcpu, of_spin_map)) | |
278 | /* Already started by OF and sitting in spin loop */ | |
279 | return 1; | |
280 | ||
281 | pcpu = get_hard_smp_processor_id(lcpu); | |
282 | ||
283 | /* Fixup atomic count: it exited inside IRQ handler. */ | |
b5e2fc1c | 284 | task_thread_info(paca[lcpu].__current)->preempt_count = 0; |
1da177e4 | 285 | |
1ed2fd2d AB |
286 | /* |
287 | * If the RTAS start-cpu token does not exist then presume the | |
288 | * cpu is already spinning. | |
289 | */ | |
290 | start_cpu = rtas_token("start-cpu"); | |
291 | if (start_cpu == RTAS_UNKNOWN_SERVICE) | |
292 | return 1; | |
293 | ||
496b7a51 | 294 | status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu); |
1da177e4 LT |
295 | if (status != 0) { |
296 | printk(KERN_ERR "start-cpu failed: %i\n", status); | |
297 | return 0; | |
298 | } | |
1ed2fd2d | 299 | |
1da177e4 LT |
300 | return 1; |
301 | } | |
302 | ||
cebf589c | 303 | #ifdef CONFIG_XICS |
1da177e4 LT |
304 | static inline void smp_xics_do_message(int cpu, int msg) |
305 | { | |
306 | set_bit(msg, &xics_ipi_message[cpu].value); | |
307 | mb(); | |
308 | xics_cause_IPI(cpu); | |
309 | } | |
310 | ||
311 | static void smp_xics_message_pass(int target, int msg) | |
312 | { | |
313 | unsigned int i; | |
314 | ||
315 | if (target < NR_CPUS) { | |
316 | smp_xics_do_message(target, msg); | |
317 | } else { | |
318 | for_each_online_cpu(i) { | |
319 | if (target == MSG_ALL_BUT_SELF | |
320 | && i == smp_processor_id()) | |
321 | continue; | |
322 | smp_xics_do_message(i, msg); | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
327 | static int __init smp_xics_probe(void) | |
328 | { | |
329 | xics_request_IPIs(); | |
330 | ||
331 | return cpus_weight(cpu_possible_map); | |
332 | } | |
333 | ||
334 | static void __devinit smp_xics_setup_cpu(int cpu) | |
335 | { | |
336 | if (cpu != boot_cpuid) | |
337 | xics_setup_cpu(); | |
338 | ||
1ababe11 | 339 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) |
1da177e4 LT |
340 | vpa_init(cpu); |
341 | ||
342 | cpu_clear(cpu, of_spin_map); | |
343 | ||
1da177e4 | 344 | } |
cebf589c | 345 | #endif /* CONFIG_XICS */ |
1da177e4 LT |
346 | |
347 | static DEFINE_SPINLOCK(timebase_lock); | |
348 | static unsigned long timebase = 0; | |
349 | ||
350 | static void __devinit pSeries_give_timebase(void) | |
351 | { | |
352 | spin_lock(&timebase_lock); | |
353 | rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL); | |
354 | timebase = get_tb(); | |
355 | spin_unlock(&timebase_lock); | |
356 | ||
357 | while (timebase) | |
358 | barrier(); | |
359 | rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL); | |
360 | } | |
361 | ||
362 | static void __devinit pSeries_take_timebase(void) | |
363 | { | |
364 | while (!timebase) | |
365 | barrier(); | |
366 | spin_lock(&timebase_lock); | |
367 | set_tb(timebase >> 32, timebase & 0xffffffff); | |
368 | timebase = 0; | |
369 | spin_unlock(&timebase_lock); | |
370 | } | |
371 | ||
372 | static void __devinit smp_pSeries_kick_cpu(int nr) | |
373 | { | |
374 | BUG_ON(nr < 0 || nr >= NR_CPUS); | |
375 | ||
376 | if (!smp_startup_cpu(nr)) | |
377 | return; | |
378 | ||
379 | /* | |
380 | * The processor is currently spinning, waiting for the | |
381 | * cpu_start field to become non-zero After we set cpu_start, | |
382 | * the processor will continue on to secondary_start | |
383 | */ | |
384 | paca[nr].cpu_start = 1; | |
385 | } | |
386 | ||
387 | static int smp_pSeries_cpu_bootable(unsigned int nr) | |
388 | { | |
389 | /* Special case - we inhibit secondary thread startup | |
390 | * during boot if the user requests it. Odd-numbered | |
391 | * cpus are assumed to be secondary threads. | |
392 | */ | |
393 | if (system_state < SYSTEM_RUNNING && | |
0231c290 | 394 | cpu_has_feature(CPU_FTR_SMT) && |
1da177e4 LT |
395 | !smt_enabled_at_boot && nr % 2 != 0) |
396 | return 0; | |
397 | ||
398 | return 1; | |
399 | } | |
cebf589c | 400 | #ifdef CONFIG_MPIC |
1da177e4 LT |
401 | static struct smp_ops_t pSeries_mpic_smp_ops = { |
402 | .message_pass = smp_mpic_message_pass, | |
403 | .probe = smp_mpic_probe, | |
404 | .kick_cpu = smp_pSeries_kick_cpu, | |
405 | .setup_cpu = smp_mpic_setup_cpu, | |
406 | }; | |
cebf589c AB |
407 | #endif |
408 | #ifdef CONFIG_XICS | |
1da177e4 LT |
409 | static struct smp_ops_t pSeries_xics_smp_ops = { |
410 | .message_pass = smp_xics_message_pass, | |
411 | .probe = smp_xics_probe, | |
412 | .kick_cpu = smp_pSeries_kick_cpu, | |
413 | .setup_cpu = smp_xics_setup_cpu, | |
414 | .cpu_bootable = smp_pSeries_cpu_bootable, | |
415 | }; | |
cebf589c | 416 | #endif |
1da177e4 LT |
417 | |
418 | /* This is called very early */ | |
0ebfff14 | 419 | static void __init smp_init_pseries(void) |
1da177e4 LT |
420 | { |
421 | int i; | |
422 | ||
423 | DBG(" -> smp_init_pSeries()\n"); | |
424 | ||
1da177e4 LT |
425 | #ifdef CONFIG_HOTPLUG_CPU |
426 | smp_ops->cpu_disable = pSeries_cpu_disable; | |
427 | smp_ops->cpu_die = pSeries_cpu_die; | |
428 | ||
429 | /* Processors can be added/removed only on LPAR */ | |
57cfb814 | 430 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
1da177e4 LT |
431 | pSeries_reconfig_notifier_register(&pSeries_smp_nb); |
432 | #endif | |
433 | ||
434 | /* Mark threads which are still spinning in hold loops. */ | |
0231c290 AB |
435 | if (cpu_has_feature(CPU_FTR_SMT)) { |
436 | for_each_present_cpu(i) { | |
1da177e4 LT |
437 | if (i % 2 == 0) |
438 | /* | |
439 | * Even-numbered logical cpus correspond to | |
440 | * primary threads. | |
441 | */ | |
442 | cpu_set(i, of_spin_map); | |
443 | } | |
0231c290 | 444 | } else { |
1da177e4 | 445 | of_spin_map = cpu_present_map; |
0231c290 | 446 | } |
1da177e4 LT |
447 | |
448 | cpu_clear(boot_cpuid, of_spin_map); | |
449 | ||
450 | /* Non-lpar has additional take/give timebase */ | |
451 | if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) { | |
452 | smp_ops->give_timebase = pSeries_give_timebase; | |
453 | smp_ops->take_timebase = pSeries_take_timebase; | |
454 | } | |
455 | ||
456 | DBG(" <- smp_init_pSeries()\n"); | |
457 | } | |
458 | ||
0ebfff14 BH |
459 | #ifdef CONFIG_MPIC |
460 | void __init smp_init_pseries_mpic(void) | |
461 | { | |
462 | smp_ops = &pSeries_mpic_smp_ops; | |
463 | ||
464 | smp_init_pseries(); | |
465 | } | |
466 | #endif | |
467 | ||
468 | void __init smp_init_pseries_xics(void) | |
469 | { | |
470 | smp_ops = &pSeries_xics_smp_ops; | |
471 | ||
472 | smp_init_pseries(); | |
473 | } |