blob: ea431e551c6b2704a19d068376d9edb84c3da577 [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 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 1999, 2012
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04005 * 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>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020041#include <asm/vtimer.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 */
Heiko Carstens50ab9a92012-09-04 17:36:16 +020069 int polarization; /* physical polarization */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040070 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 Carstens50ab9a92012-09-04 17:36:16 +020077/*
78 * The smp_cpu_state_mutex must be held when changing the state or polarization
79 * member of a pcpu data structure within the pcpu_devices arreay.
80 */
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020081DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +010082
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040083/*
84 * Signal processor helper functions.
85 */
86static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstens5c0b9122009-09-11 10:29:05 +020087{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040088 register unsigned int reg1 asm ("1") = parm;
89 int cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020090
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040091 asm volatile(
92 " sigp %1,%2,0(%3)\n"
93 " ipm %0\n"
94 " srl %0,28\n"
95 : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
96 if (status && cc == 1)
97 *status = reg1;
98 return cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020099}
100
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400101static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100102{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400103 int cc;
104
105 while (1) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200106 cc = __pcpu_sigp(addr, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200107 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400108 return cc;
109 cpu_relax();
110 }
111}
112
113static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
114{
115 int cc, retry;
116
117 for (retry = 0; ; retry++) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200118 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200119 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400120 break;
121 if (retry >= 3)
122 udelay(10);
123 }
124 return cc;
125}
126
127static inline int pcpu_stopped(struct pcpu *pcpu)
128{
Heiko Carstens41459d32012-09-14 11:09:52 +0200129 u32 uninitialized_var(status);
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200130
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200131 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200132 0, &status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400133 return 0;
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200134 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400135}
136
137static inline int pcpu_running(struct pcpu *pcpu)
138{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200139 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200140 0, NULL) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400141 return 1;
Heiko Carstens524b24a2012-06-04 12:11:41 +0200142 /* Status stored condition code is equivalent to cpu not running. */
143 return 0;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100144}
145
Michael Holzheu1943f532011-10-30 15:16:38 +0100146/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400147 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100148 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400149static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100150{
151 int cpu;
152
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400153 for_each_cpu(cpu, mask)
154 if (pcpu_devices[cpu].address == address)
155 return pcpu_devices + cpu;
156 return NULL;
157}
158
159static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
160{
161 int order;
162
163 set_bit(ec_bit, &pcpu->ec_mask);
164 order = pcpu_running(pcpu) ?
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200165 SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400166 pcpu_sigp_retry(pcpu, order, 0);
167}
168
169static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
170{
171 struct _lowcore *lc;
172
173 if (pcpu != &pcpu_devices[0]) {
174 pcpu->lowcore = (struct _lowcore *)
175 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
176 pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
177 pcpu->panic_stack = __get_free_page(GFP_KERNEL);
178 if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
179 goto out;
Michael Holzheu1943f532011-10-30 15:16:38 +0100180 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400181 lc = pcpu->lowcore;
182 memcpy(lc, &S390_lowcore, 512);
183 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
184 lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
185 lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
186 lc->cpu_nr = cpu;
187#ifndef CONFIG_64BIT
188 if (MACHINE_HAS_IEEE) {
189 lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
190 if (!lc->extended_save_area_addr)
191 goto out;
192 }
193#else
194 if (vdso_alloc_per_cpu(lc))
195 goto out;
196#endif
197 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200198 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400199 return 0;
200out:
201 if (pcpu != &pcpu_devices[0]) {
202 free_page(pcpu->panic_stack);
203 free_pages(pcpu->async_stack, ASYNC_ORDER);
204 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
205 }
206 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100207}
208
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200209#ifdef CONFIG_HOTPLUG_CPU
210
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400211static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100212{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200213 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400214 lowcore_ptr[pcpu - pcpu_devices] = NULL;
215#ifndef CONFIG_64BIT
216 if (MACHINE_HAS_IEEE) {
217 struct _lowcore *lc = pcpu->lowcore;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100218
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400219 free_page((unsigned long) lc->extended_save_area_addr);
220 lc->extended_save_area_addr = 0;
221 }
222#else
223 vdso_free_per_cpu(pcpu->lowcore);
224#endif
225 if (pcpu != &pcpu_devices[0]) {
226 free_page(pcpu->panic_stack);
227 free_pages(pcpu->async_stack, ASYNC_ORDER);
228 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
229 }
Heiko Carstens2c2df112010-02-26 22:37:34 +0100230}
231
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200232#endif /* CONFIG_HOTPLUG_CPU */
233
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400234static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100235{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400236 struct _lowcore *lc = pcpu->lowcore;
237
238 atomic_inc(&init_mm.context.attach_count);
239 lc->cpu_nr = cpu;
240 lc->percpu_offset = __per_cpu_offset[cpu];
241 lc->kernel_asce = S390_lowcore.kernel_asce;
242 lc->machine_flags = S390_lowcore.machine_flags;
243 lc->ftrace_func = S390_lowcore.ftrace_func;
244 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
245 __ctl_store(lc->cregs_save_area, 0, 15);
246 save_access_regs((unsigned int *) lc->access_regs_save_area);
247 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
248 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100249}
250
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400251static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
252{
253 struct _lowcore *lc = pcpu->lowcore;
254 struct thread_info *ti = task_thread_info(tsk);
255
256 lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
257 lc->thread_info = (unsigned long) task_thread_info(tsk);
258 lc->current_task = (unsigned long) tsk;
259 lc->user_timer = ti->user_timer;
260 lc->system_timer = ti->system_timer;
261 lc->steal_timer = 0;
262}
263
264static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
265{
266 struct _lowcore *lc = pcpu->lowcore;
267
268 lc->restart_stack = lc->kernel_stack;
269 lc->restart_fn = (unsigned long) func;
270 lc->restart_data = (unsigned long) data;
271 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200272 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400273}
274
275/*
276 * Call function via PSW restart on pcpu and stop the current cpu.
277 */
278static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
279 void *data, unsigned long stack)
280{
Michael Holzheu061da3d2012-05-24 14:38:26 +0200281 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
Heiko Carstensfbe76562012-06-05 09:59:52 +0200282 unsigned long source_cpu = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400283
284 __load_psw_mask(psw_kernel_bits);
Heiko Carstensfbe76562012-06-05 09:59:52 +0200285 if (pcpu->address == source_cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400286 func(data); /* should not return */
287 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200288 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400289 /* Restart func on the target cpu and stop the current cpu. */
Heiko Carstensfbe76562012-06-05 09:59:52 +0200290 mem_assign_absolute(lc->restart_stack, stack);
291 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
292 mem_assign_absolute(lc->restart_data, (unsigned long) data);
293 mem_assign_absolute(lc->restart_source, source_cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400294 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200295 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400296 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200297 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400298 " brc 2,1b # busy, try again\n"
Heiko Carstensfbe76562012-06-05 09:59:52 +0200299 : : "d" (pcpu->address), "d" (source_cpu),
Heiko Carstenseb546192012-06-04 15:05:43 +0200300 "K" (SIGP_RESTART), "K" (SIGP_STOP)
301 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400302 for (;;) ;
303}
304
305/*
306 * Call function on an online CPU.
307 */
308void smp_call_online_cpu(void (*func)(void *), void *data)
309{
310 struct pcpu *pcpu;
311
312 /* Use the current cpu if it is online. */
313 pcpu = pcpu_find_address(cpu_online_mask, stap());
314 if (!pcpu)
315 /* Use the first online cpu. */
316 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
317 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
318}
319
320/*
321 * Call function on the ipl CPU.
322 */
323void smp_call_ipl_cpu(void (*func)(void *), void *data)
324{
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400325 pcpu_delegate(&pcpu_devices[0], func, data,
326 pcpu_devices->panic_stack + PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400327}
328
329int smp_find_processor_id(u16 address)
330{
331 int cpu;
332
333 for_each_present_cpu(cpu)
334 if (pcpu_devices[cpu].address == address)
335 return cpu;
336 return -1;
337}
338
339int smp_vcpu_scheduled(int cpu)
340{
341 return pcpu_running(pcpu_devices + cpu);
342}
343
344void smp_yield(void)
345{
346 if (MACHINE_HAS_DIAG44)
347 asm volatile("diag 0,0,0x44");
348}
349
350void smp_yield_cpu(int cpu)
351{
352 if (MACHINE_HAS_DIAG9C)
353 asm volatile("diag %0,0,0x9c"
354 : : "d" (pcpu_devices[cpu].address));
355 else if (MACHINE_HAS_DIAG44)
356 asm volatile("diag 0,0,0x44");
357}
358
359/*
360 * Send cpus emergency shutdown signal. This gives the cpus the
361 * opportunity to complete outstanding interrupts.
362 */
363void smp_emergency_stop(cpumask_t *cpumask)
364{
365 u64 end;
366 int cpu;
367
368 end = get_clock() + (1000000UL << 12);
369 for_each_cpu(cpu, cpumask) {
370 struct pcpu *pcpu = pcpu_devices + cpu;
371 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200372 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
373 0, NULL) == SIGP_CC_BUSY &&
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400374 get_clock() < end)
375 cpu_relax();
376 }
377 while (get_clock() < end) {
378 for_each_cpu(cpu, cpumask)
379 if (pcpu_stopped(pcpu_devices + cpu))
380 cpumask_clear_cpu(cpu, cpumask);
381 if (cpumask_empty(cpumask))
382 break;
383 cpu_relax();
384 }
385}
386
387/*
388 * Stop all cpus but the current one.
389 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100390void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100392 cpumask_t cpumask;
393 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Heiko Carstens677d7622007-11-20 11:13:37 +0100395 /* Disable all interrupts/machine checks */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100396 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100397 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100398
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400399 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100400 cpumask_copy(&cpumask, cpu_online_mask);
401 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400403 if (oops_in_progress)
404 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100405
406 /* stop all processors */
407 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400408 struct pcpu *pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200409 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400410 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100411 cpu_relax();
412 }
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400416 * Stop the current cpu.
417 */
418void smp_stop_cpu(void)
419{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200420 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400421 for (;;) ;
422}
423
424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * This is the main routine where commands issued by other
426 * cpus are handled.
427 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400428static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200429 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200431 unsigned long bits;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400432 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400434 cpu = smp_processor_id();
Heiko Carstensfde15c32012-03-11 11:59:31 -0400435 if (ext_code.code == 0x1202)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400436 kstat_cpu(cpu).irqs[EXTINT_EXC]++;
Heiko Carstens2a3a2d62011-10-30 15:17:19 +0100437 else
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400438 kstat_cpu(cpu).irqs[EXTINT_EMS]++;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200439 /*
440 * handle bit signal external calls
Heiko Carstens39ce0102007-04-27 16:02:00 +0200441 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400442 bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100444 if (test_bit(ec_stop_cpu, &bits))
445 smp_stop_cpu();
446
Peter Zijlstra184748c2011-04-05 17:23:39 +0200447 if (test_bit(ec_schedule, &bits))
448 scheduler_ipi();
449
Heiko Carstens39ce0102007-04-27 16:02:00 +0200450 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100451 generic_smp_call_function_interrupt();
452
453 if (test_bit(ec_call_function_single, &bits))
454 generic_smp_call_function_single_interrupt();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Rusty Russell630cd042009-09-24 09:34:45 -0600458void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100459{
460 int cpu;
461
Rusty Russell630cd042009-09-24 09:34:45 -0600462 for_each_cpu(cpu, mask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400463 pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100464}
465
466void arch_send_call_function_single_ipi(int cpu)
467{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400468 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100469}
470
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800471#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
473 * this function sends a 'purge tlb' signal to another CPU.
474 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200475static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200477 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480void smp_ptlb_all(void)
481{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200482 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800485#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487/*
488 * this function sends a 'reschedule' IPI to another CPU.
489 * it goes straight through and wastes no time serializing
490 * anything. Worst case is that we lose a reschedule ...
491 */
492void smp_send_reschedule(int cpu)
493{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400494 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/*
498 * parameter area for the set/clear control bit callbacks
499 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200500struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400501 unsigned long orval;
502 unsigned long andval;
503 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200504};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506/*
507 * callback for setting/clearing control bits
508 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200509static void smp_ctl_bit_callback(void *info)
510{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200511 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200513
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200514 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400515 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200516 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/*
520 * Set a bit in a control register of all cpus
521 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200522void smp_ctl_set_bit(int cr, int bit)
523{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400524 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200526 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200528EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/*
531 * Clear a bit in a control register of all cpus
532 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200533void smp_ctl_clear_bit(int cr, int bit)
534{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400535 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200537 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200539EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Michael Holzheu60a0c682011-10-30 15:16:40 +0100541#if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
Michael Holzheu411ed322007-04-27 16:01:49 +0200542
Heiko Carstensf64ca212010-02-26 22:37:32 +0100543struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200544EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
545
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400546static void __init smp_get_save_area(int cpu, u16 address)
Heiko Carstens08d07962008-01-26 14:10:56 +0100547{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400548 void *lc = pcpu_devices[0].lowcore;
549 struct save_area *save_area;
Heiko Carstens08d07962008-01-26 14:10:56 +0100550
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400551 if (is_kdump_kernel())
552 return;
553 if (!OLDMEM_BASE && (address == boot_cpu_address ||
554 ipl_info.type != IPL_TYPE_FCP_DUMP))
555 return;
556 if (cpu >= NR_CPUS) {
557 pr_warning("CPU %i exceeds the maximum %i and is excluded "
558 "from the dump\n", cpu, NR_CPUS - 1);
559 return;
Heiko Carstens08d07962008-01-26 14:10:56 +0100560 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400561 save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
562 if (!save_area)
563 panic("could not allocate memory for save area\n");
564 zfcpdump_save_areas[cpu] = save_area;
565#ifdef CONFIG_CRASH_DUMP
566 if (address == boot_cpu_address) {
567 /* Copy the registers of the boot cpu. */
568 copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
569 SAVE_AREA_BASE - PAGE_SIZE, 0);
570 return;
571 }
572#endif
573 /* Get the registers of a non-boot cpu. */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200574 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400575 memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
576}
577
578int smp_store_status(int cpu)
579{
580 struct pcpu *pcpu;
581
582 pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200583 if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
584 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400585 return -EIO;
Heiko Carstens08d07962008-01-26 14:10:56 +0100586 return 0;
587}
588
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400589#else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
Heiko Carstens08d07962008-01-26 14:10:56 +0100590
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400591static inline void smp_get_save_area(int cpu, u16 address) { }
Heiko Carstens08d07962008-01-26 14:10:56 +0100592
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400593#endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
594
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200595void smp_cpu_set_polarization(int cpu, int val)
596{
597 pcpu_devices[cpu].polarization = val;
598}
599
600int smp_cpu_get_polarization(int cpu)
601{
602 return pcpu_devices[cpu].polarization;
603}
604
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400605static struct sclp_cpu_info *smp_get_cpu_info(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100606{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400607 static int use_sigp_detection;
Heiko Carstens08d07962008-01-26 14:10:56 +0100608 struct sclp_cpu_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400609 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100610
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400611 info = kzalloc(sizeof(*info), GFP_KERNEL);
612 if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
613 use_sigp_detection = 1;
614 for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200615 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
616 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400617 continue;
618 info->cpu[info->configured].address = address;
619 info->configured++;
620 }
621 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100622 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400623 return info;
Heiko Carstens08d07962008-01-26 14:10:56 +0100624}
625
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400626static int __devinit smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100627
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400628static int __devinit __smp_rescan_cpus(struct sclp_cpu_info *info,
629 int sysfs_add)
630{
631 struct pcpu *pcpu;
632 cpumask_t avail;
633 int cpu, nr, i;
634
635 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200636 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400637 cpu = cpumask_first(&avail);
638 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
639 if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
640 continue;
641 if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
642 continue;
643 pcpu = pcpu_devices + cpu;
644 pcpu->address = info->cpu[i].address;
645 pcpu->state = (cpu >= info->configured) ?
646 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200647 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400648 set_cpu_present(cpu, true);
649 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
650 set_cpu_present(cpu, false);
651 else
652 nr++;
653 cpu = cpumask_next(cpu, &avail);
654 }
655 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Heiko Carstens48483b32008-01-26 14:11:05 +0100658static void __init smp_detect_cpus(void)
659{
660 unsigned int cpu, c_cpus, s_cpus;
661 struct sclp_cpu_info *info;
Heiko Carstens48483b32008-01-26 14:11:05 +0100662
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400663 info = smp_get_cpu_info();
Heiko Carstens48483b32008-01-26 14:11:05 +0100664 if (!info)
665 panic("smp_detect_cpus failed to allocate memory\n");
Heiko Carstens48483b32008-01-26 14:11:05 +0100666 if (info->has_cpu_type) {
667 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400668 if (info->cpu[cpu].address != boot_cpu_address)
669 continue;
670 /* The boot cpu dictates the cpu type. */
671 boot_cpu_type = info->cpu[cpu].type;
672 break;
Heiko Carstens48483b32008-01-26 14:11:05 +0100673 }
674 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400675 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100676 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400677 if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100678 continue;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400679 if (cpu < info->configured) {
680 smp_get_save_area(c_cpus, info->cpu[cpu].address);
681 c_cpus++;
682 } else
Heiko Carstens48483b32008-01-26 14:11:05 +0100683 s_cpus++;
Heiko Carstens48483b32008-01-26 14:11:05 +0100684 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100685 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100686 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400687 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100688 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400689 kfree(info);
Heiko Carstens48483b32008-01-26 14:11:05 +0100690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200693 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400695static void __cpuinit smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400697 S390_lowcore.last_update_clock = get_clock();
698 S390_lowcore.restart_stack = (unsigned long) restart_stack;
699 S390_lowcore.restart_fn = (unsigned long) do_restart;
700 S390_lowcore.restart_data = 0;
701 S390_lowcore.restart_source = -1UL;
702 restore_access_regs(S390_lowcore.access_regs_save_area);
703 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
704 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200705 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800706 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200707 init_cpu_timer();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200708 init_cpu_vtimer();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100709 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200710 notify_cpu_starting(smp_processor_id());
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200711 set_cpu_online(smp_processor_id(), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 local_irq_enable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200713 /* cpu_idle will call schedule for us */
714 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/* Upping and downing of CPUs */
Thomas Gleixner8239c252012-04-20 13:05:42 +0000718int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400720 struct pcpu *pcpu;
721 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400723 pcpu = pcpu_devices + cpu;
724 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100725 return -EIO;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200726 if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
727 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400728 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000729
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400730 rc = pcpu_alloc_lowcore(pcpu, cpu);
731 if (rc)
732 return rc;
733 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000734 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400735 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 while (!cpu_online(cpu))
737 cpu_relax();
738 return 0;
739}
740
Heiko Carstens37a33022006-02-17 13:52:47 -0800741static int __init setup_possible_cpus(char *s)
742{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400743 int max, cpu;
Heiko Carstens48483b32008-01-26 14:11:05 +0100744
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400745 if (kstrtoint(s, 0, &max) < 0)
746 return 0;
Heiko Carstens88e01282009-04-14 15:36:25 +0200747 init_cpu_possible(cpumask_of(0));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400748 for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100749 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800750 return 0;
751}
752early_param("possible_cpus", setup_possible_cpus);
753
Heiko Carstens48483b32008-01-26 14:11:05 +0100754#ifdef CONFIG_HOTPLUG_CPU
755
Heiko Carstens39ce0102007-04-27 16:02:00 +0200756int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400758 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400760 set_cpu_online(smp_processor_id(), false);
761 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100762 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400763 /* Disable interrupt sources via control register. */
764 __ctl_store(cregs, 0, 15);
765 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
766 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
767 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
768 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
770}
771
Heiko Carstens39ce0102007-04-27 16:02:00 +0200772void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400774 struct pcpu *pcpu;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400777 pcpu = pcpu_devices + cpu;
778 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400780 pcpu_free_lowcore(pcpu);
Martin Schwidefsky050eef32010-08-24 09:26:21 +0200781 atomic_dec(&init_mm.context.attach_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Heiko Carstensb456d942011-05-23 10:24:33 +0200784void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 idle_task_exit();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200787 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400788 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Heiko Carstens255acee2006-02-17 13:52:46 -0800791#endif /* CONFIG_HOTPLUG_CPU */
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793void __init smp_prepare_cpus(unsigned int max_cpus)
794{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200795 /* request the 0x1201 emergency signal external interrupt */
796 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
797 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100798 /* request the 0x1202 external call external interrupt */
799 if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
800 panic("Couldn't request external interrupt 0x1202");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400801 smp_detect_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200804void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400806 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400808 boot_cpu_address = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400809 pcpu->state = CPU_STATE_CONFIGURED;
810 pcpu->address = boot_cpu_address;
811 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
812 pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
813 pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
814 S390_lowcore.percpu_offset = __per_cpu_offset[0];
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200815 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200816 set_cpu_present(0, true);
817 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200820void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Heiko Carstens02beacc2010-01-13 20:44:34 +0100824void __init smp_setup_processor_id(void)
825{
826 S390_lowcore.cpu_nr = 0;
Heiko Carstens02beacc2010-01-13 20:44:34 +0100827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/*
830 * the frequency of the profiling timer can be changed
831 * by writing a multiplier value into /proc/profile.
832 *
833 * usually you want to run this on all CPUs ;)
834 */
835int setup_profiling_timer(unsigned int multiplier)
836{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Heiko Carstens08d07962008-01-26 14:10:56 +0100840#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800841static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400842 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100843{
844 ssize_t count;
845
846 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400847 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100848 mutex_unlock(&smp_cpu_state_mutex);
849 return count;
850}
851
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800852static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400853 struct device_attribute *attr,
854 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100855{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400856 struct pcpu *pcpu;
857 int cpu, val, rc;
Heiko Carstens08d07962008-01-26 14:10:56 +0100858 char delim;
859
860 if (sscanf(buf, "%d %c", &val, &delim) != 1)
861 return -EINVAL;
862 if (val != 0 && val != 1)
863 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100864 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200865 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100866 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100867 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400868 cpu = dev->id;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100869 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100870 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400871 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100872 rc = 0;
873 switch (val) {
874 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400875 if (pcpu->state != CPU_STATE_CONFIGURED)
876 break;
877 rc = sclp_cpu_deconfigure(pcpu->address);
878 if (rc)
879 break;
880 pcpu->state = CPU_STATE_STANDBY;
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200881 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400882 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100883 break;
884 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400885 if (pcpu->state != CPU_STATE_STANDBY)
886 break;
887 rc = sclp_cpu_configure(pcpu->address);
888 if (rc)
889 break;
890 pcpu->state = CPU_STATE_CONFIGURED;
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200891 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400892 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100893 break;
894 default:
895 break;
896 }
897out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100898 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200899 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100900 return rc ? rc : count;
901}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800902static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +0100903#endif /* CONFIG_HOTPLUG_CPU */
904
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800905static ssize_t show_cpu_address(struct device *dev,
906 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100907{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400908 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +0100909}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800910static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100911
Heiko Carstens08d07962008-01-26 14:10:56 +0100912static struct attribute *cpu_common_attrs[] = {
913#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800914 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100915#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800916 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100917 NULL,
918};
919
920static struct attribute_group cpu_common_attr_group = {
921 .attrs = cpu_common_attrs,
922};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800924static ssize_t show_idle_count(struct device *dev,
925 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200926{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400927 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200928 unsigned long long idle_count;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200929 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200930
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400931 do {
932 sequence = ACCESS_ONCE(idle->sequence);
933 idle_count = ACCESS_ONCE(idle->idle_count);
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200934 if (ACCESS_ONCE(idle->clock_idle_enter))
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400935 idle_count++;
936 } while ((sequence & 1) || (idle->sequence != sequence));
Heiko Carstensfae8b222007-10-22 12:52:39 +0200937 return sprintf(buf, "%llu\n", idle_count);
938}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800939static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200940
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800941static ssize_t show_idle_time(struct device *dev,
942 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200943{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400944 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
945 unsigned long long now, idle_time, idle_enter, idle_exit;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200946 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200947
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400948 do {
949 now = get_clock();
950 sequence = ACCESS_ONCE(idle->sequence);
951 idle_time = ACCESS_ONCE(idle->idle_time);
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200952 idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
953 idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400954 } while ((sequence & 1) || (idle->sequence != sequence));
955 idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100956 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200957}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800958static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200959
Heiko Carstens08d07962008-01-26 14:10:56 +0100960static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800961 &dev_attr_idle_count.attr,
962 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200963 NULL,
964};
965
Heiko Carstens08d07962008-01-26 14:10:56 +0100966static struct attribute_group cpu_online_attr_group = {
967 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200968};
969
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200970static int __cpuinit smp_cpu_notify(struct notifier_block *self,
971 unsigned long action, void *hcpu)
972{
973 unsigned int cpu = (unsigned int)(long)hcpu;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400974 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800975 struct device *s = &c->dev;
Akinobu Mitad882ba62010-05-26 14:43:34 -0700976 int err = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200977
Heiko Carstens1c725922012-08-27 15:43:49 +0200978 switch (action & ~CPU_TASKS_FROZEN) {
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200979 case CPU_ONLINE:
Akinobu Mitad882ba62010-05-26 14:43:34 -0700980 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200981 break;
982 case CPU_DEAD:
Heiko Carstens08d07962008-01-26 14:10:56 +0100983 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200984 break;
985 }
Akinobu Mitad882ba62010-05-26 14:43:34 -0700986 return notifier_from_errno(err);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200987}
988
Heiko Carstens2bc89b52008-02-05 16:50:40 +0100989static int __devinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100990{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400991 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800992 struct device *s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +0100993 int rc;
994
995 c->hotpluggable = 1;
996 rc = register_cpu(c, cpu);
997 if (rc)
998 goto out;
999 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1000 if (rc)
1001 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +01001002 if (cpu_online(cpu)) {
1003 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1004 if (rc)
1005 goto out_online;
1006 }
1007 rc = topology_cpu_init(c);
1008 if (rc)
1009 goto out_topology;
1010 return 0;
1011
1012out_topology:
1013 if (cpu_online(cpu))
1014 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1015out_online:
Heiko Carstens08d07962008-01-26 14:10:56 +01001016 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1017out_cpu:
1018#ifdef CONFIG_HOTPLUG_CPU
1019 unregister_cpu(c);
1020#endif
1021out:
1022 return rc;
1023}
1024
1025#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001026
Heiko Carstens67060d92008-05-30 10:03:27 +02001027int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001028{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001029 struct sclp_cpu_info *info;
1030 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001031
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001032 info = smp_get_cpu_info();
1033 if (!info)
1034 return -ENOMEM;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001035 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001036 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001037 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001038 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001039 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001040 kfree(info);
1041 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001042 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001043 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001044}
1045
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001046static ssize_t __ref rescan_store(struct device *dev,
1047 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001048 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001049 size_t count)
1050{
1051 int rc;
1052
1053 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001054 return rc ? rc : count;
1055}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001056static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001057#endif /* CONFIG_HOTPLUG_CPU */
1058
Heiko Carstens83a24e32011-12-27 11:27:09 +01001059static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001060{
Heiko Carstens83a24e32011-12-27 11:27:09 +01001061 int cpu, rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001062
Heiko Carstens7755d6b2012-08-27 15:14:47 +02001063 hotcpu_notifier(smp_cpu_notify, 0);
Heiko Carstens08d07962008-01-26 14:10:56 +01001064#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001065 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001066 if (rc)
1067 return rc;
1068#endif
1069 for_each_present_cpu(cpu) {
1070 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001071 if (rc)
1072 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074 return 0;
1075}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001076subsys_initcall(s390_smp_init);