blob: e01408429ad6ab60101c8ed298bd7a0716b56cb9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04002 * SMP related functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04004 * Copyright IBM Corp. 1999,2012
5 * Author(s): Denis Joseph Barrow,
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Heiko Carstens39ce0102007-04-27 16:02:00 +02009 * based on other smp stuff by
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
11 * (c) 1998 Ingo Molnar
12 *
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040013 * The code outside of smp.c uses logical cpu numbers, only smp.c does
14 * the translation of logical to physical cpu ids. All new code that
15 * operates on physical cpu numbers needs to go into smp.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Martin Schwidefsky395d31d2008-12-25 13:39:50 +010018#define KMSG_COMPONENT "cpu"
19#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
Heiko Carstensf2308862011-01-05 12:48:08 +010021#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040025#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/spinlock.h>
27#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
Christian Borntraeger3324e602009-03-26 15:23:56 +010030#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010033#include <linux/crash_dump.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010034#include <asm/asm-offsets.h>
Heiko Carstens1e3cab22012-03-30 09:40:55 +020035#include <asm/switch_to.h>
36#include <asm/facility.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010037#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010038#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010041#include <asm/timer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020042#include <asm/lowcore.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010043#include <asm/sclp.h>
Martin Schwidefskyc742b312008-12-31 15:11:42 +010044#include <asm/vdso.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040045#include <asm/debug.h>
Michael Holzheu4857d4b2012-03-11 11:59:34 -040046#include <asm/os_info.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020047#include <asm/sigp.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020048#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040050enum {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040051 ec_schedule = 0,
52 ec_call_function,
53 ec_call_function_single,
54 ec_stop_cpu,
55};
Heiko Carstens08d07962008-01-26 14:10:56 +010056
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040057enum {
Heiko Carstens08d07962008-01-26 14:10:56 +010058 CPU_STATE_STANDBY,
59 CPU_STATE_CONFIGURED,
60};
61
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040062struct pcpu {
63 struct cpu cpu;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040064 struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
65 unsigned long async_stack; /* async stack for the cpu */
66 unsigned long panic_stack; /* panic stack for the cpu */
67 unsigned long ec_mask; /* bit mask for ec_xxx functions */
68 int state; /* physical cpu state */
69 u32 status; /* last status received via sigp */
70 u16 address; /* physical cpu address */
71};
72
73static u8 boot_cpu_type;
74static u16 boot_cpu_address;
75static struct pcpu pcpu_devices[NR_CPUS];
76
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020077DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +010078
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040079/*
80 * Signal processor helper functions.
81 */
82static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstens5c0b9122009-09-11 10:29:05 +020083{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040084 register unsigned int reg1 asm ("1") = parm;
85 int cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020086
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040087 asm volatile(
88 " sigp %1,%2,0(%3)\n"
89 " ipm %0\n"
90 " srl %0,28\n"
91 : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
92 if (status && cc == 1)
93 *status = reg1;
94 return cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020095}
96
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040097static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010098{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040099 int cc;
100
101 while (1) {
102 cc = __pcpu_sigp(addr, order, parm, status);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200103 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400104 return cc;
105 cpu_relax();
106 }
107}
108
109static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
110{
111 int cc, retry;
112
113 for (retry = 0; ; retry++) {
114 cc = __pcpu_sigp(pcpu->address, order, parm, &pcpu->status);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200115 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400116 break;
117 if (retry >= 3)
118 udelay(10);
119 }
120 return cc;
121}
122
123static inline int pcpu_stopped(struct pcpu *pcpu)
124{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200125 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
126 0, &pcpu->status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400127 return 0;
Heiko Carstensa095a8a2012-06-04 14:07:47 +0200128 return !!(pcpu->status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400129}
130
131static inline int pcpu_running(struct pcpu *pcpu)
132{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200133 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
134 0, &pcpu->status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400135 return 1;
Heiko Carstens524b24a2012-06-04 12:11:41 +0200136 /* Status stored condition code is equivalent to cpu not running. */
137 return 0;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100138}
139
Michael Holzheu1943f532011-10-30 15:16:38 +0100140/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400141 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100142 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400143static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100144{
145 int cpu;
146
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400147 for_each_cpu(cpu, mask)
148 if (pcpu_devices[cpu].address == address)
149 return pcpu_devices + cpu;
150 return NULL;
151}
152
153static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
154{
155 int order;
156
157 set_bit(ec_bit, &pcpu->ec_mask);
158 order = pcpu_running(pcpu) ?
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200159 SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400160 pcpu_sigp_retry(pcpu, order, 0);
161}
162
163static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
164{
165 struct _lowcore *lc;
166
167 if (pcpu != &pcpu_devices[0]) {
168 pcpu->lowcore = (struct _lowcore *)
169 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
170 pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
171 pcpu->panic_stack = __get_free_page(GFP_KERNEL);
172 if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
173 goto out;
Michael Holzheu1943f532011-10-30 15:16:38 +0100174 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400175 lc = pcpu->lowcore;
176 memcpy(lc, &S390_lowcore, 512);
177 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
178 lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
179 lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
180 lc->cpu_nr = cpu;
181#ifndef CONFIG_64BIT
182 if (MACHINE_HAS_IEEE) {
183 lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
184 if (!lc->extended_save_area_addr)
185 goto out;
186 }
187#else
188 if (vdso_alloc_per_cpu(lc))
189 goto out;
190#endif
191 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200192 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400193 return 0;
194out:
195 if (pcpu != &pcpu_devices[0]) {
196 free_page(pcpu->panic_stack);
197 free_pages(pcpu->async_stack, ASYNC_ORDER);
198 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
199 }
200 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100201}
202
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200203#ifdef CONFIG_HOTPLUG_CPU
204
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400205static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100206{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200207 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400208 lowcore_ptr[pcpu - pcpu_devices] = NULL;
209#ifndef CONFIG_64BIT
210 if (MACHINE_HAS_IEEE) {
211 struct _lowcore *lc = pcpu->lowcore;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100212
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400213 free_page((unsigned long) lc->extended_save_area_addr);
214 lc->extended_save_area_addr = 0;
215 }
216#else
217 vdso_free_per_cpu(pcpu->lowcore);
218#endif
219 if (pcpu != &pcpu_devices[0]) {
220 free_page(pcpu->panic_stack);
221 free_pages(pcpu->async_stack, ASYNC_ORDER);
222 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
223 }
Heiko Carstens2c2df112010-02-26 22:37:34 +0100224}
225
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200226#endif /* CONFIG_HOTPLUG_CPU */
227
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400228static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100229{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400230 struct _lowcore *lc = pcpu->lowcore;
231
232 atomic_inc(&init_mm.context.attach_count);
233 lc->cpu_nr = cpu;
234 lc->percpu_offset = __per_cpu_offset[cpu];
235 lc->kernel_asce = S390_lowcore.kernel_asce;
236 lc->machine_flags = S390_lowcore.machine_flags;
237 lc->ftrace_func = S390_lowcore.ftrace_func;
238 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
239 __ctl_store(lc->cregs_save_area, 0, 15);
240 save_access_regs((unsigned int *) lc->access_regs_save_area);
241 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
242 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100243}
244
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400245static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
246{
247 struct _lowcore *lc = pcpu->lowcore;
248 struct thread_info *ti = task_thread_info(tsk);
249
250 lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
251 lc->thread_info = (unsigned long) task_thread_info(tsk);
252 lc->current_task = (unsigned long) tsk;
253 lc->user_timer = ti->user_timer;
254 lc->system_timer = ti->system_timer;
255 lc->steal_timer = 0;
256}
257
258static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
259{
260 struct _lowcore *lc = pcpu->lowcore;
261
262 lc->restart_stack = lc->kernel_stack;
263 lc->restart_fn = (unsigned long) func;
264 lc->restart_data = (unsigned long) data;
265 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200266 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400267}
268
269/*
270 * Call function via PSW restart on pcpu and stop the current cpu.
271 */
272static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
273 void *data, unsigned long stack)
274{
Michael Holzheu061da3d2012-05-24 14:38:26 +0200275 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
276 struct {
277 unsigned long stack;
278 void *func;
279 void *data;
280 unsigned long source;
281 } restart = { stack, func, data, stap() };
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400282
283 __load_psw_mask(psw_kernel_bits);
Michael Holzheu061da3d2012-05-24 14:38:26 +0200284 if (pcpu->address == restart.source)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400285 func(data); /* should not return */
286 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200287 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400288 /* Restart func on the target cpu and stop the current cpu. */
Michael Holzheu061da3d2012-05-24 14:38:26 +0200289 memcpy_absolute(&lc->restart_stack, &restart, sizeof(restart));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400290 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200291 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400292 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200293 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400294 " brc 2,1b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200295 : : "d" (pcpu->address), "d" (restart.source),
296 "K" (SIGP_RESTART), "K" (SIGP_STOP)
297 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400298 for (;;) ;
299}
300
301/*
302 * Call function on an online CPU.
303 */
304void smp_call_online_cpu(void (*func)(void *), void *data)
305{
306 struct pcpu *pcpu;
307
308 /* Use the current cpu if it is online. */
309 pcpu = pcpu_find_address(cpu_online_mask, stap());
310 if (!pcpu)
311 /* Use the first online cpu. */
312 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
313 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
314}
315
316/*
317 * Call function on the ipl CPU.
318 */
319void smp_call_ipl_cpu(void (*func)(void *), void *data)
320{
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400321 pcpu_delegate(&pcpu_devices[0], func, data,
322 pcpu_devices->panic_stack + PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400323}
324
325int smp_find_processor_id(u16 address)
326{
327 int cpu;
328
329 for_each_present_cpu(cpu)
330 if (pcpu_devices[cpu].address == address)
331 return cpu;
332 return -1;
333}
334
335int smp_vcpu_scheduled(int cpu)
336{
337 return pcpu_running(pcpu_devices + cpu);
338}
339
340void smp_yield(void)
341{
342 if (MACHINE_HAS_DIAG44)
343 asm volatile("diag 0,0,0x44");
344}
345
346void smp_yield_cpu(int cpu)
347{
348 if (MACHINE_HAS_DIAG9C)
349 asm volatile("diag %0,0,0x9c"
350 : : "d" (pcpu_devices[cpu].address));
351 else if (MACHINE_HAS_DIAG44)
352 asm volatile("diag 0,0,0x44");
353}
354
355/*
356 * Send cpus emergency shutdown signal. This gives the cpus the
357 * opportunity to complete outstanding interrupts.
358 */
359void smp_emergency_stop(cpumask_t *cpumask)
360{
361 u64 end;
362 int cpu;
363
364 end = get_clock() + (1000000UL << 12);
365 for_each_cpu(cpu, cpumask) {
366 struct pcpu *pcpu = pcpu_devices + cpu;
367 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200368 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
369 0, NULL) == SIGP_CC_BUSY &&
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400370 get_clock() < end)
371 cpu_relax();
372 }
373 while (get_clock() < end) {
374 for_each_cpu(cpu, cpumask)
375 if (pcpu_stopped(pcpu_devices + cpu))
376 cpumask_clear_cpu(cpu, cpumask);
377 if (cpumask_empty(cpumask))
378 break;
379 cpu_relax();
380 }
381}
382
383/*
384 * Stop all cpus but the current one.
385 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100386void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100388 cpumask_t cpumask;
389 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Heiko Carstens677d7622007-11-20 11:13:37 +0100391 /* Disable all interrupts/machine checks */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100392 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100393 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100394
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400395 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100396 cpumask_copy(&cpumask, cpu_online_mask);
397 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400399 if (oops_in_progress)
400 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100401
402 /* stop all processors */
403 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400404 struct pcpu *pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200405 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400406 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100407 cpu_relax();
408 }
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400412 * Stop the current cpu.
413 */
414void smp_stop_cpu(void)
415{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200416 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400417 for (;;) ;
418}
419
420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * This is the main routine where commands issued by other
422 * cpus are handled.
423 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400424static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200425 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200427 unsigned long bits;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400428 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400430 cpu = smp_processor_id();
Heiko Carstensfde15c32012-03-11 11:59:31 -0400431 if (ext_code.code == 0x1202)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400432 kstat_cpu(cpu).irqs[EXTINT_EXC]++;
Heiko Carstens2a3a2d62011-10-30 15:17:19 +0100433 else
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400434 kstat_cpu(cpu).irqs[EXTINT_EMS]++;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200435 /*
436 * handle bit signal external calls
Heiko Carstens39ce0102007-04-27 16:02:00 +0200437 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400438 bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100440 if (test_bit(ec_stop_cpu, &bits))
441 smp_stop_cpu();
442
Peter Zijlstra184748c2011-04-05 17:23:39 +0200443 if (test_bit(ec_schedule, &bits))
444 scheduler_ipi();
445
Heiko Carstens39ce0102007-04-27 16:02:00 +0200446 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100447 generic_smp_call_function_interrupt();
448
449 if (test_bit(ec_call_function_single, &bits))
450 generic_smp_call_function_single_interrupt();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Rusty Russell630cd042009-09-24 09:34:45 -0600454void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100455{
456 int cpu;
457
Rusty Russell630cd042009-09-24 09:34:45 -0600458 for_each_cpu(cpu, mask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400459 pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100460}
461
462void arch_send_call_function_single_ipi(int cpu)
463{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400464 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100465}
466
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800467#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * this function sends a 'purge tlb' signal to another CPU.
470 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200471static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200473 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476void smp_ptlb_all(void)
477{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200478 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800481#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483/*
484 * this function sends a 'reschedule' IPI to another CPU.
485 * it goes straight through and wastes no time serializing
486 * anything. Worst case is that we lose a reschedule ...
487 */
488void smp_send_reschedule(int cpu)
489{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400490 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
494 * parameter area for the set/clear control bit callbacks
495 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200496struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400497 unsigned long orval;
498 unsigned long andval;
499 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200500};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502/*
503 * callback for setting/clearing control bits
504 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200505static void smp_ctl_bit_callback(void *info)
506{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200507 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200509
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200510 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400511 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200512 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/*
516 * Set a bit in a control register of all cpus
517 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200518void smp_ctl_set_bit(int cr, int bit)
519{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400520 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200522 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200524EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526/*
527 * Clear a bit in a control register of all cpus
528 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200529void smp_ctl_clear_bit(int cr, int bit)
530{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400531 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200533 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200535EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Michael Holzheu60a0c682011-10-30 15:16:40 +0100537#if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
Michael Holzheu411ed322007-04-27 16:01:49 +0200538
Heiko Carstensf64ca212010-02-26 22:37:32 +0100539struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200540EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
541
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400542static void __init smp_get_save_area(int cpu, u16 address)
Heiko Carstens08d07962008-01-26 14:10:56 +0100543{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400544 void *lc = pcpu_devices[0].lowcore;
545 struct save_area *save_area;
Heiko Carstens08d07962008-01-26 14:10:56 +0100546
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400547 if (is_kdump_kernel())
548 return;
549 if (!OLDMEM_BASE && (address == boot_cpu_address ||
550 ipl_info.type != IPL_TYPE_FCP_DUMP))
551 return;
552 if (cpu >= NR_CPUS) {
553 pr_warning("CPU %i exceeds the maximum %i and is excluded "
554 "from the dump\n", cpu, NR_CPUS - 1);
555 return;
Heiko Carstens08d07962008-01-26 14:10:56 +0100556 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400557 save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
558 if (!save_area)
559 panic("could not allocate memory for save area\n");
560 zfcpdump_save_areas[cpu] = save_area;
561#ifdef CONFIG_CRASH_DUMP
562 if (address == boot_cpu_address) {
563 /* Copy the registers of the boot cpu. */
564 copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
565 SAVE_AREA_BASE - PAGE_SIZE, 0);
566 return;
567 }
568#endif
569 /* Get the registers of a non-boot cpu. */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200570 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400571 memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
572}
573
574int smp_store_status(int cpu)
575{
576 struct pcpu *pcpu;
577
578 pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200579 if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
580 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400581 return -EIO;
Heiko Carstens08d07962008-01-26 14:10:56 +0100582 return 0;
583}
584
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400585#else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
Heiko Carstens08d07962008-01-26 14:10:56 +0100586
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400587static inline void smp_get_save_area(int cpu, u16 address) { }
Heiko Carstens08d07962008-01-26 14:10:56 +0100588
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400589#endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
590
591static struct sclp_cpu_info *smp_get_cpu_info(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100592{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400593 static int use_sigp_detection;
Heiko Carstens08d07962008-01-26 14:10:56 +0100594 struct sclp_cpu_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400595 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100596
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400597 info = kzalloc(sizeof(*info), GFP_KERNEL);
598 if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
599 use_sigp_detection = 1;
600 for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200601 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
602 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400603 continue;
604 info->cpu[info->configured].address = address;
605 info->configured++;
606 }
607 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100608 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400609 return info;
Heiko Carstens08d07962008-01-26 14:10:56 +0100610}
611
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400612static int __devinit smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100613
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400614static int __devinit __smp_rescan_cpus(struct sclp_cpu_info *info,
615 int sysfs_add)
616{
617 struct pcpu *pcpu;
618 cpumask_t avail;
619 int cpu, nr, i;
620
621 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200622 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400623 cpu = cpumask_first(&avail);
624 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
625 if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
626 continue;
627 if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
628 continue;
629 pcpu = pcpu_devices + cpu;
630 pcpu->address = info->cpu[i].address;
631 pcpu->state = (cpu >= info->configured) ?
632 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
633 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
634 set_cpu_present(cpu, true);
635 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
636 set_cpu_present(cpu, false);
637 else
638 nr++;
639 cpu = cpumask_next(cpu, &avail);
640 }
641 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Heiko Carstens48483b32008-01-26 14:11:05 +0100644static void __init smp_detect_cpus(void)
645{
646 unsigned int cpu, c_cpus, s_cpus;
647 struct sclp_cpu_info *info;
Heiko Carstens48483b32008-01-26 14:11:05 +0100648
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400649 info = smp_get_cpu_info();
Heiko Carstens48483b32008-01-26 14:11:05 +0100650 if (!info)
651 panic("smp_detect_cpus failed to allocate memory\n");
Heiko Carstens48483b32008-01-26 14:11:05 +0100652 if (info->has_cpu_type) {
653 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400654 if (info->cpu[cpu].address != boot_cpu_address)
655 continue;
656 /* The boot cpu dictates the cpu type. */
657 boot_cpu_type = info->cpu[cpu].type;
658 break;
Heiko Carstens48483b32008-01-26 14:11:05 +0100659 }
660 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400661 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100662 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400663 if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100664 continue;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400665 if (cpu < info->configured) {
666 smp_get_save_area(c_cpus, info->cpu[cpu].address);
667 c_cpus++;
668 } else
Heiko Carstens48483b32008-01-26 14:11:05 +0100669 s_cpus++;
Heiko Carstens48483b32008-01-26 14:11:05 +0100670 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100671 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100672 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400673 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100674 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400675 kfree(info);
Heiko Carstens48483b32008-01-26 14:11:05 +0100676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200679 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400681static void __cpuinit smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400683 S390_lowcore.last_update_clock = get_clock();
684 S390_lowcore.restart_stack = (unsigned long) restart_stack;
685 S390_lowcore.restart_fn = (unsigned long) do_restart;
686 S390_lowcore.restart_data = 0;
687 S390_lowcore.restart_source = -1UL;
688 restore_access_regs(S390_lowcore.access_regs_save_area);
689 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
690 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200691 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800692 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200693 init_cpu_timer();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200694 init_cpu_vtimer();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100695 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200696 notify_cpu_starting(smp_processor_id());
Heiko Carstensca9fc752008-12-25 13:38:39 +0100697 ipi_call_lock();
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200698 set_cpu_online(smp_processor_id(), true);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100699 ipi_call_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 local_irq_enable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200701 /* cpu_idle will call schedule for us */
702 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/* Upping and downing of CPUs */
Thomas Gleixner8239c252012-04-20 13:05:42 +0000706int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400708 struct pcpu *pcpu;
709 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400711 pcpu = pcpu_devices + cpu;
712 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100713 return -EIO;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200714 if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
715 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400716 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000717
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400718 rc = pcpu_alloc_lowcore(pcpu, cpu);
719 if (rc)
720 return rc;
721 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000722 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400723 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 while (!cpu_online(cpu))
725 cpu_relax();
726 return 0;
727}
728
Heiko Carstens37a33022006-02-17 13:52:47 -0800729static int __init setup_possible_cpus(char *s)
730{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400731 int max, cpu;
Heiko Carstens48483b32008-01-26 14:11:05 +0100732
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400733 if (kstrtoint(s, 0, &max) < 0)
734 return 0;
Heiko Carstens88e01282009-04-14 15:36:25 +0200735 init_cpu_possible(cpumask_of(0));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400736 for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100737 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800738 return 0;
739}
740early_param("possible_cpus", setup_possible_cpus);
741
Heiko Carstens48483b32008-01-26 14:11:05 +0100742#ifdef CONFIG_HOTPLUG_CPU
743
Heiko Carstens39ce0102007-04-27 16:02:00 +0200744int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400746 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400748 set_cpu_online(smp_processor_id(), false);
749 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100750 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400751 /* Disable interrupt sources via control register. */
752 __ctl_store(cregs, 0, 15);
753 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
754 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
755 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
756 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758}
759
Heiko Carstens39ce0102007-04-27 16:02:00 +0200760void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400762 struct pcpu *pcpu;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400765 pcpu = pcpu_devices + cpu;
766 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400768 pcpu_free_lowcore(pcpu);
Martin Schwidefsky050eef32010-08-24 09:26:21 +0200769 atomic_dec(&init_mm.context.attach_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Heiko Carstensb456d942011-05-23 10:24:33 +0200772void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 idle_task_exit();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200775 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400776 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Heiko Carstens255acee2006-02-17 13:52:46 -0800779#endif /* CONFIG_HOTPLUG_CPU */
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781void __init smp_prepare_cpus(unsigned int max_cpus)
782{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200783 /* request the 0x1201 emergency signal external interrupt */
784 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
785 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100786 /* request the 0x1202 external call external interrupt */
787 if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
788 panic("Couldn't request external interrupt 0x1202");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400789 smp_detect_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200792void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400794 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400796 boot_cpu_address = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400797 pcpu->state = CPU_STATE_CONFIGURED;
798 pcpu->address = boot_cpu_address;
799 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
800 pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
801 pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
802 S390_lowcore.percpu_offset = __per_cpu_offset[0];
803 cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200804 set_cpu_present(0, true);
805 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200808void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Heiko Carstens02beacc2010-01-13 20:44:34 +0100812void __init smp_setup_processor_id(void)
813{
814 S390_lowcore.cpu_nr = 0;
Heiko Carstens02beacc2010-01-13 20:44:34 +0100815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
818 * the frequency of the profiling timer can be changed
819 * by writing a multiplier value into /proc/profile.
820 *
821 * usually you want to run this on all CPUs ;)
822 */
823int setup_profiling_timer(unsigned int multiplier)
824{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Heiko Carstens08d07962008-01-26 14:10:56 +0100828#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800829static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400830 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100831{
832 ssize_t count;
833
834 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400835 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100836 mutex_unlock(&smp_cpu_state_mutex);
837 return count;
838}
839
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800840static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400841 struct device_attribute *attr,
842 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100843{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400844 struct pcpu *pcpu;
845 int cpu, val, rc;
Heiko Carstens08d07962008-01-26 14:10:56 +0100846 char delim;
847
848 if (sscanf(buf, "%d %c", &val, &delim) != 1)
849 return -EINVAL;
850 if (val != 0 && val != 1)
851 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100852 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200853 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100854 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100855 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400856 cpu = dev->id;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100857 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100858 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400859 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100860 rc = 0;
861 switch (val) {
862 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400863 if (pcpu->state != CPU_STATE_CONFIGURED)
864 break;
865 rc = sclp_cpu_deconfigure(pcpu->address);
866 if (rc)
867 break;
868 pcpu->state = CPU_STATE_STANDBY;
869 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
870 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100871 break;
872 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400873 if (pcpu->state != CPU_STATE_STANDBY)
874 break;
875 rc = sclp_cpu_configure(pcpu->address);
876 if (rc)
877 break;
878 pcpu->state = CPU_STATE_CONFIGURED;
879 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
880 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100881 break;
882 default:
883 break;
884 }
885out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100886 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200887 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100888 return rc ? rc : count;
889}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800890static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +0100891#endif /* CONFIG_HOTPLUG_CPU */
892
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800893static ssize_t show_cpu_address(struct device *dev,
894 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100895{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400896 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +0100897}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800898static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100899
Heiko Carstens08d07962008-01-26 14:10:56 +0100900static struct attribute *cpu_common_attrs[] = {
901#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800902 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100903#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800904 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100905 NULL,
906};
907
908static struct attribute_group cpu_common_attr_group = {
909 .attrs = cpu_common_attrs,
910};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800912static ssize_t show_idle_count(struct device *dev,
913 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200914{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400915 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200916 unsigned long long idle_count;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200917 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200918
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400919 do {
920 sequence = ACCESS_ONCE(idle->sequence);
921 idle_count = ACCESS_ONCE(idle->idle_count);
922 if (ACCESS_ONCE(idle->idle_enter))
923 idle_count++;
924 } while ((sequence & 1) || (idle->sequence != sequence));
Heiko Carstensfae8b222007-10-22 12:52:39 +0200925 return sprintf(buf, "%llu\n", idle_count);
926}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800927static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200928
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800929static ssize_t show_idle_time(struct device *dev,
930 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200931{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400932 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
933 unsigned long long now, idle_time, idle_enter, idle_exit;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200934 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200935
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400936 do {
937 now = get_clock();
938 sequence = ACCESS_ONCE(idle->sequence);
939 idle_time = ACCESS_ONCE(idle->idle_time);
940 idle_enter = ACCESS_ONCE(idle->idle_enter);
941 idle_exit = ACCESS_ONCE(idle->idle_exit);
942 } while ((sequence & 1) || (idle->sequence != sequence));
943 idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100944 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200945}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800946static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200947
Heiko Carstens08d07962008-01-26 14:10:56 +0100948static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800949 &dev_attr_idle_count.attr,
950 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200951 NULL,
952};
953
Heiko Carstens08d07962008-01-26 14:10:56 +0100954static struct attribute_group cpu_online_attr_group = {
955 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200956};
957
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200958static int __cpuinit smp_cpu_notify(struct notifier_block *self,
959 unsigned long action, void *hcpu)
960{
961 unsigned int cpu = (unsigned int)(long)hcpu;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400962 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800963 struct device *s = &c->dev;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200964 struct s390_idle_data *idle;
Akinobu Mitad882ba62010-05-26 14:43:34 -0700965 int err = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200966
967 switch (action) {
968 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700969 case CPU_ONLINE_FROZEN:
Heiko Carstensfae8b222007-10-22 12:52:39 +0200970 idle = &per_cpu(s390_idle, cpu);
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200971 memset(idle, 0, sizeof(struct s390_idle_data));
Akinobu Mitad882ba62010-05-26 14:43:34 -0700972 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200973 break;
974 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700975 case CPU_DEAD_FROZEN:
Heiko Carstens08d07962008-01-26 14:10:56 +0100976 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200977 break;
978 }
Akinobu Mitad882ba62010-05-26 14:43:34 -0700979 return notifier_from_errno(err);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200980}
981
982static struct notifier_block __cpuinitdata smp_cpu_nb = {
Heiko Carstens39ce0102007-04-27 16:02:00 +0200983 .notifier_call = smp_cpu_notify,
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200984};
985
Heiko Carstens2bc89b52008-02-05 16:50:40 +0100986static int __devinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100987{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400988 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800989 struct device *s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +0100990 int rc;
991
992 c->hotpluggable = 1;
993 rc = register_cpu(c, cpu);
994 if (rc)
995 goto out;
996 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
997 if (rc)
998 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +0100999 if (cpu_online(cpu)) {
1000 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1001 if (rc)
1002 goto out_online;
1003 }
1004 rc = topology_cpu_init(c);
1005 if (rc)
1006 goto out_topology;
1007 return 0;
1008
1009out_topology:
1010 if (cpu_online(cpu))
1011 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1012out_online:
Heiko Carstens08d07962008-01-26 14:10:56 +01001013 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1014out_cpu:
1015#ifdef CONFIG_HOTPLUG_CPU
1016 unregister_cpu(c);
1017#endif
1018out:
1019 return rc;
1020}
1021
1022#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001023
Heiko Carstens67060d92008-05-30 10:03:27 +02001024int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001025{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001026 struct sclp_cpu_info *info;
1027 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001028
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001029 info = smp_get_cpu_info();
1030 if (!info)
1031 return -ENOMEM;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001032 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001033 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001034 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001035 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001036 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001037 kfree(info);
1038 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001039 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001040 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001041}
1042
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001043static ssize_t __ref rescan_store(struct device *dev,
1044 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001045 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001046 size_t count)
1047{
1048 int rc;
1049
1050 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001051 return rc ? rc : count;
1052}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001053static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001054#endif /* CONFIG_HOTPLUG_CPU */
1055
Heiko Carstens83a24e32011-12-27 11:27:09 +01001056static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001057{
Heiko Carstens83a24e32011-12-27 11:27:09 +01001058 int cpu, rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001059
1060 register_cpu_notifier(&smp_cpu_nb);
Heiko Carstens08d07962008-01-26 14:10:56 +01001061#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001062 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001063 if (rc)
1064 return rc;
1065#endif
1066 for_each_present_cpu(cpu) {
1067 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001068 if (rc)
1069 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 return 0;
1072}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001073subsys_initcall(s390_smp_init);