blob: 05674b6690019012f3acdbb86ea5325616df9f76 [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 Carstens41459d36cf2012-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);
Martin Schwidefskydc7ee002013-04-24 10:20:43 +0200184 lc->async_stack = pcpu->async_stack + ASYNC_SIZE
185 - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
186 lc->panic_stack = pcpu->panic_stack + PAGE_SIZE
187 - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400188 lc->cpu_nr = cpu;
189#ifndef CONFIG_64BIT
190 if (MACHINE_HAS_IEEE) {
191 lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
192 if (!lc->extended_save_area_addr)
193 goto out;
194 }
195#else
196 if (vdso_alloc_per_cpu(lc))
197 goto out;
198#endif
199 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200200 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400201 return 0;
202out:
203 if (pcpu != &pcpu_devices[0]) {
204 free_page(pcpu->panic_stack);
205 free_pages(pcpu->async_stack, ASYNC_ORDER);
206 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
207 }
208 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100209}
210
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200211#ifdef CONFIG_HOTPLUG_CPU
212
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400213static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100214{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200215 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400216 lowcore_ptr[pcpu - pcpu_devices] = NULL;
217#ifndef CONFIG_64BIT
218 if (MACHINE_HAS_IEEE) {
219 struct _lowcore *lc = pcpu->lowcore;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100220
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400221 free_page((unsigned long) lc->extended_save_area_addr);
222 lc->extended_save_area_addr = 0;
223 }
224#else
225 vdso_free_per_cpu(pcpu->lowcore);
226#endif
227 if (pcpu != &pcpu_devices[0]) {
228 free_page(pcpu->panic_stack);
229 free_pages(pcpu->async_stack, ASYNC_ORDER);
230 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
231 }
Heiko Carstens2c2df112010-02-26 22:37:34 +0100232}
233
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200234#endif /* CONFIG_HOTPLUG_CPU */
235
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400236static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100237{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400238 struct _lowcore *lc = pcpu->lowcore;
239
240 atomic_inc(&init_mm.context.attach_count);
241 lc->cpu_nr = cpu;
242 lc->percpu_offset = __per_cpu_offset[cpu];
243 lc->kernel_asce = S390_lowcore.kernel_asce;
244 lc->machine_flags = S390_lowcore.machine_flags;
245 lc->ftrace_func = S390_lowcore.ftrace_func;
246 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
247 __ctl_store(lc->cregs_save_area, 0, 15);
248 save_access_regs((unsigned int *) lc->access_regs_save_area);
249 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
250 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100251}
252
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400253static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
254{
255 struct _lowcore *lc = pcpu->lowcore;
256 struct thread_info *ti = task_thread_info(tsk);
257
Martin Schwidefskydc7ee002013-04-24 10:20:43 +0200258 lc->kernel_stack = (unsigned long) task_stack_page(tsk)
259 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400260 lc->thread_info = (unsigned long) task_thread_info(tsk);
261 lc->current_task = (unsigned long) tsk;
262 lc->user_timer = ti->user_timer;
263 lc->system_timer = ti->system_timer;
264 lc->steal_timer = 0;
265}
266
267static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
268{
269 struct _lowcore *lc = pcpu->lowcore;
270
271 lc->restart_stack = lc->kernel_stack;
272 lc->restart_fn = (unsigned long) func;
273 lc->restart_data = (unsigned long) data;
274 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200275 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400276}
277
278/*
279 * Call function via PSW restart on pcpu and stop the current cpu.
280 */
281static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
282 void *data, unsigned long stack)
283{
Michael Holzheu061da3d2012-05-24 14:38:26 +0200284 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
Heiko Carstensfbe76562012-06-05 09:59:52 +0200285 unsigned long source_cpu = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400286
287 __load_psw_mask(psw_kernel_bits);
Heiko Carstensfbe76562012-06-05 09:59:52 +0200288 if (pcpu->address == source_cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400289 func(data); /* should not return */
290 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200291 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400292 /* Restart func on the target cpu and stop the current cpu. */
Heiko Carstensfbe76562012-06-05 09:59:52 +0200293 mem_assign_absolute(lc->restart_stack, stack);
294 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
295 mem_assign_absolute(lc->restart_data, (unsigned long) data);
296 mem_assign_absolute(lc->restart_source, source_cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400297 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200298 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400299 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200300 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400301 " brc 2,1b # busy, try again\n"
Heiko Carstensfbe76562012-06-05 09:59:52 +0200302 : : "d" (pcpu->address), "d" (source_cpu),
Heiko Carstenseb546192012-06-04 15:05:43 +0200303 "K" (SIGP_RESTART), "K" (SIGP_STOP)
304 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400305 for (;;) ;
306}
307
308/*
309 * Call function on an online CPU.
310 */
311void smp_call_online_cpu(void (*func)(void *), void *data)
312{
313 struct pcpu *pcpu;
314
315 /* Use the current cpu if it is online. */
316 pcpu = pcpu_find_address(cpu_online_mask, stap());
317 if (!pcpu)
318 /* Use the first online cpu. */
319 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
320 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
321}
322
323/*
324 * Call function on the ipl CPU.
325 */
326void smp_call_ipl_cpu(void (*func)(void *), void *data)
327{
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400328 pcpu_delegate(&pcpu_devices[0], func, data,
329 pcpu_devices->panic_stack + PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400330}
331
332int smp_find_processor_id(u16 address)
333{
334 int cpu;
335
336 for_each_present_cpu(cpu)
337 if (pcpu_devices[cpu].address == address)
338 return cpu;
339 return -1;
340}
341
342int smp_vcpu_scheduled(int cpu)
343{
344 return pcpu_running(pcpu_devices + cpu);
345}
346
347void smp_yield(void)
348{
349 if (MACHINE_HAS_DIAG44)
350 asm volatile("diag 0,0,0x44");
351}
352
353void smp_yield_cpu(int cpu)
354{
355 if (MACHINE_HAS_DIAG9C)
356 asm volatile("diag %0,0,0x9c"
357 : : "d" (pcpu_devices[cpu].address));
358 else if (MACHINE_HAS_DIAG44)
359 asm volatile("diag 0,0,0x44");
360}
361
362/*
363 * Send cpus emergency shutdown signal. This gives the cpus the
364 * opportunity to complete outstanding interrupts.
365 */
366void smp_emergency_stop(cpumask_t *cpumask)
367{
368 u64 end;
369 int cpu;
370
Heiko Carstens1aae0562013-01-30 09:49:40 +0100371 end = get_tod_clock() + (1000000UL << 12);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400372 for_each_cpu(cpu, cpumask) {
373 struct pcpu *pcpu = pcpu_devices + cpu;
374 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200375 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
376 0, NULL) == SIGP_CC_BUSY &&
Heiko Carstens1aae0562013-01-30 09:49:40 +0100377 get_tod_clock() < end)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400378 cpu_relax();
379 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100380 while (get_tod_clock() < end) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400381 for_each_cpu(cpu, cpumask)
382 if (pcpu_stopped(pcpu_devices + cpu))
383 cpumask_clear_cpu(cpu, cpumask);
384 if (cpumask_empty(cpumask))
385 break;
386 cpu_relax();
387 }
388}
389
390/*
391 * Stop all cpus but the current one.
392 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100393void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100395 cpumask_t cpumask;
396 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Heiko Carstens677d7622007-11-20 11:13:37 +0100398 /* Disable all interrupts/machine checks */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100399 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100400 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100401
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400402 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100403 cpumask_copy(&cpumask, cpu_online_mask);
404 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400406 if (oops_in_progress)
407 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100408
409 /* stop all processors */
410 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400411 struct pcpu *pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200412 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400413 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100414 cpu_relax();
415 }
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400419 * Stop the current cpu.
420 */
421void smp_stop_cpu(void)
422{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200423 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400424 for (;;) ;
425}
426
427/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * This is the main routine where commands issued by other
429 * cpus are handled.
430 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400431static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200432 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200434 unsigned long bits;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400435 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400437 cpu = smp_processor_id();
Heiko Carstensfde15c32012-03-11 11:59:31 -0400438 if (ext_code.code == 0x1202)
Heiko Carstens420f42e2013-01-02 15:18:18 +0100439 inc_irq_stat(IRQEXT_EXC);
Heiko Carstens2a3a2d62011-10-30 15:17:19 +0100440 else
Heiko Carstens420f42e2013-01-02 15:18:18 +0100441 inc_irq_stat(IRQEXT_EMS);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200442 /*
443 * handle bit signal external calls
Heiko Carstens39ce0102007-04-27 16:02:00 +0200444 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400445 bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100447 if (test_bit(ec_stop_cpu, &bits))
448 smp_stop_cpu();
449
Peter Zijlstra184748c2011-04-05 17:23:39 +0200450 if (test_bit(ec_schedule, &bits))
451 scheduler_ipi();
452
Heiko Carstens39ce0102007-04-27 16:02:00 +0200453 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100454 generic_smp_call_function_interrupt();
455
456 if (test_bit(ec_call_function_single, &bits))
457 generic_smp_call_function_single_interrupt();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Rusty Russell630cd042009-09-24 09:34:45 -0600461void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100462{
463 int cpu;
464
Rusty Russell630cd042009-09-24 09:34:45 -0600465 for_each_cpu(cpu, mask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400466 pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100467}
468
469void arch_send_call_function_single_ipi(int cpu)
470{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400471 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100472}
473
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800474#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/*
476 * this function sends a 'purge tlb' signal to another CPU.
477 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200478static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200480 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483void smp_ptlb_all(void)
484{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200485 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800488#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/*
491 * this function sends a 'reschedule' IPI to another CPU.
492 * it goes straight through and wastes no time serializing
493 * anything. Worst case is that we lose a reschedule ...
494 */
495void smp_send_reschedule(int cpu)
496{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400497 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/*
501 * parameter area for the set/clear control bit callbacks
502 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200503struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400504 unsigned long orval;
505 unsigned long andval;
506 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200507};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509/*
510 * callback for setting/clearing control bits
511 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200512static void smp_ctl_bit_callback(void *info)
513{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200514 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200516
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200517 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400518 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200519 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Set a bit in a control register of all cpus
524 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200525void smp_ctl_set_bit(int cr, int bit)
526{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400527 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200529 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200531EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533/*
534 * Clear a bit in a control register of all cpus
535 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200536void smp_ctl_clear_bit(int cr, int bit)
537{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400538 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200540 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200542EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Michael Holzheu60a0c682011-10-30 15:16:40 +0100544#if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
Michael Holzheu411ed322007-04-27 16:01:49 +0200545
Heiko Carstensf64ca212010-02-26 22:37:32 +0100546struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200547EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
548
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400549static void __init smp_get_save_area(int cpu, u16 address)
Heiko Carstens08d07962008-01-26 14:10:56 +0100550{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400551 void *lc = pcpu_devices[0].lowcore;
552 struct save_area *save_area;
Heiko Carstens08d07962008-01-26 14:10:56 +0100553
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400554 if (is_kdump_kernel())
555 return;
556 if (!OLDMEM_BASE && (address == boot_cpu_address ||
557 ipl_info.type != IPL_TYPE_FCP_DUMP))
558 return;
559 if (cpu >= NR_CPUS) {
560 pr_warning("CPU %i exceeds the maximum %i and is excluded "
561 "from the dump\n", cpu, NR_CPUS - 1);
562 return;
Heiko Carstens08d07962008-01-26 14:10:56 +0100563 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400564 save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
565 if (!save_area)
566 panic("could not allocate memory for save area\n");
567 zfcpdump_save_areas[cpu] = save_area;
568#ifdef CONFIG_CRASH_DUMP
569 if (address == boot_cpu_address) {
570 /* Copy the registers of the boot cpu. */
571 copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
572 SAVE_AREA_BASE - PAGE_SIZE, 0);
573 return;
574 }
575#endif
576 /* Get the registers of a non-boot cpu. */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200577 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400578 memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
579}
580
581int smp_store_status(int cpu)
582{
583 struct pcpu *pcpu;
584
585 pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200586 if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
587 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400588 return -EIO;
Heiko Carstens08d07962008-01-26 14:10:56 +0100589 return 0;
590}
591
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400592#else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
Heiko Carstens08d07962008-01-26 14:10:56 +0100593
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400594static inline void smp_get_save_area(int cpu, u16 address) { }
Heiko Carstens08d07962008-01-26 14:10:56 +0100595
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400596#endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
597
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200598void smp_cpu_set_polarization(int cpu, int val)
599{
600 pcpu_devices[cpu].polarization = val;
601}
602
603int smp_cpu_get_polarization(int cpu)
604{
605 return pcpu_devices[cpu].polarization;
606}
607
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400608static struct sclp_cpu_info *smp_get_cpu_info(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100609{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400610 static int use_sigp_detection;
Heiko Carstens08d07962008-01-26 14:10:56 +0100611 struct sclp_cpu_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400612 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100613
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400614 info = kzalloc(sizeof(*info), GFP_KERNEL);
615 if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
616 use_sigp_detection = 1;
617 for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200618 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
619 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400620 continue;
621 info->cpu[info->configured].address = address;
622 info->configured++;
623 }
624 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100625 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400626 return info;
Heiko Carstens08d07962008-01-26 14:10:56 +0100627}
628
Heiko Carstenseba61972012-12-27 14:03:36 +0100629static int __cpuinit smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100630
Heiko Carstenseba61972012-12-27 14:03:36 +0100631static int __cpuinit __smp_rescan_cpus(struct sclp_cpu_info *info,
632 int sysfs_add)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400633{
634 struct pcpu *pcpu;
635 cpumask_t avail;
636 int cpu, nr, i;
637
638 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200639 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400640 cpu = cpumask_first(&avail);
641 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
642 if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
643 continue;
644 if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
645 continue;
646 pcpu = pcpu_devices + cpu;
647 pcpu->address = info->cpu[i].address;
Martin Schwidefskya4eeea42013-05-15 16:53:07 +0200648 pcpu->state = (i >= info->configured) ?
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400649 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200650 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400651 set_cpu_present(cpu, true);
652 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
653 set_cpu_present(cpu, false);
654 else
655 nr++;
656 cpu = cpumask_next(cpu, &avail);
657 }
658 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Heiko Carstens48483b32008-01-26 14:11:05 +0100661static void __init smp_detect_cpus(void)
662{
663 unsigned int cpu, c_cpus, s_cpus;
664 struct sclp_cpu_info *info;
Heiko Carstens48483b32008-01-26 14:11:05 +0100665
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400666 info = smp_get_cpu_info();
Heiko Carstens48483b32008-01-26 14:11:05 +0100667 if (!info)
668 panic("smp_detect_cpus failed to allocate memory\n");
Heiko Carstens48483b32008-01-26 14:11:05 +0100669 if (info->has_cpu_type) {
670 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400671 if (info->cpu[cpu].address != boot_cpu_address)
672 continue;
673 /* The boot cpu dictates the cpu type. */
674 boot_cpu_type = info->cpu[cpu].type;
675 break;
Heiko Carstens48483b32008-01-26 14:11:05 +0100676 }
677 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400678 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100679 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400680 if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100681 continue;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400682 if (cpu < info->configured) {
683 smp_get_save_area(c_cpus, info->cpu[cpu].address);
684 c_cpus++;
685 } else
Heiko Carstens48483b32008-01-26 14:11:05 +0100686 s_cpus++;
Heiko Carstens48483b32008-01-26 14:11:05 +0100687 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100688 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100689 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400690 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100691 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400692 kfree(info);
Heiko Carstens48483b32008-01-26 14:11:05 +0100693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200696 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400698static void __cpuinit smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Heiko Carstens1aae0562013-01-30 09:49:40 +0100700 S390_lowcore.last_update_clock = get_tod_clock();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400701 S390_lowcore.restart_stack = (unsigned long) restart_stack;
702 S390_lowcore.restart_fn = (unsigned long) do_restart;
703 S390_lowcore.restart_data = 0;
704 S390_lowcore.restart_source = -1UL;
705 restore_access_regs(S390_lowcore.access_regs_save_area);
706 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
707 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200708 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800709 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200710 init_cpu_timer();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200711 init_cpu_vtimer();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100712 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200713 notify_cpu_starting(smp_processor_id());
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200714 set_cpu_online(smp_processor_id(), true);
Heiko Carstens93f3b2e2013-01-02 16:54:12 +0100715 inc_irq_stat(CPU_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 local_irq_enable();
Thomas Gleixner52c00652013-03-21 22:49:57 +0100717 cpu_startup_entry(CPUHP_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/* Upping and downing of CPUs */
Thomas Gleixner8239c252012-04-20 13:05:42 +0000721int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400723 struct pcpu *pcpu;
724 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400726 pcpu = pcpu_devices + cpu;
727 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100728 return -EIO;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200729 if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
730 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400731 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000732
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400733 rc = pcpu_alloc_lowcore(pcpu, cpu);
734 if (rc)
735 return rc;
736 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000737 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400738 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 while (!cpu_online(cpu))
740 cpu_relax();
741 return 0;
742}
743
Heiko Carstens37a33022006-02-17 13:52:47 -0800744static int __init setup_possible_cpus(char *s)
745{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400746 int max, cpu;
Heiko Carstens48483b32008-01-26 14:11:05 +0100747
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400748 if (kstrtoint(s, 0, &max) < 0)
749 return 0;
Heiko Carstens88e01282009-04-14 15:36:25 +0200750 init_cpu_possible(cpumask_of(0));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400751 for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100752 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800753 return 0;
754}
755early_param("possible_cpus", setup_possible_cpus);
756
Heiko Carstens48483b32008-01-26 14:11:05 +0100757#ifdef CONFIG_HOTPLUG_CPU
758
Heiko Carstens39ce0102007-04-27 16:02:00 +0200759int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400761 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400763 set_cpu_online(smp_processor_id(), false);
764 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100765 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400766 /* Disable interrupt sources via control register. */
767 __ctl_store(cregs, 0, 15);
768 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
769 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
770 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
771 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
773}
774
Heiko Carstens39ce0102007-04-27 16:02:00 +0200775void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400777 struct pcpu *pcpu;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400780 pcpu = pcpu_devices + cpu;
781 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400783 pcpu_free_lowcore(pcpu);
Martin Schwidefsky050eef32010-08-24 09:26:21 +0200784 atomic_dec(&init_mm.context.attach_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Heiko Carstensb456d942011-05-23 10:24:33 +0200787void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 idle_task_exit();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200790 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400791 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Heiko Carstens255acee2006-02-17 13:52:46 -0800794#endif /* CONFIG_HOTPLUG_CPU */
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796void __init smp_prepare_cpus(unsigned int max_cpus)
797{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200798 /* request the 0x1201 emergency signal external interrupt */
799 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
800 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100801 /* request the 0x1202 external call external interrupt */
802 if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
803 panic("Couldn't request external interrupt 0x1202");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400804 smp_detect_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200807void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400809 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400811 boot_cpu_address = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400812 pcpu->state = CPU_STATE_CONFIGURED;
813 pcpu->address = boot_cpu_address;
814 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
Martin Schwidefskydc7ee002013-04-24 10:20:43 +0200815 pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
816 + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
817 pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
818 + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400819 S390_lowcore.percpu_offset = __per_cpu_offset[0];
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200820 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200821 set_cpu_present(0, true);
822 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200825void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Heiko Carstens02beacc2010-01-13 20:44:34 +0100829void __init smp_setup_processor_id(void)
830{
831 S390_lowcore.cpu_nr = 0;
Heiko Carstens02beacc2010-01-13 20:44:34 +0100832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * the frequency of the profiling timer can be changed
836 * by writing a multiplier value into /proc/profile.
837 *
838 * usually you want to run this on all CPUs ;)
839 */
840int setup_profiling_timer(unsigned int multiplier)
841{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Heiko Carstens08d07962008-01-26 14:10:56 +0100845#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800846static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400847 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100848{
849 ssize_t count;
850
851 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400852 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100853 mutex_unlock(&smp_cpu_state_mutex);
854 return count;
855}
856
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800857static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400858 struct device_attribute *attr,
859 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100860{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400861 struct pcpu *pcpu;
862 int cpu, val, rc;
Heiko Carstens08d07962008-01-26 14:10:56 +0100863 char delim;
864
865 if (sscanf(buf, "%d %c", &val, &delim) != 1)
866 return -EINVAL;
867 if (val != 0 && val != 1)
868 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100869 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200870 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100871 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100872 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400873 cpu = dev->id;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100874 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100875 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400876 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100877 rc = 0;
878 switch (val) {
879 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400880 if (pcpu->state != CPU_STATE_CONFIGURED)
881 break;
882 rc = sclp_cpu_deconfigure(pcpu->address);
883 if (rc)
884 break;
885 pcpu->state = CPU_STATE_STANDBY;
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200886 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400887 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100888 break;
889 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400890 if (pcpu->state != CPU_STATE_STANDBY)
891 break;
892 rc = sclp_cpu_configure(pcpu->address);
893 if (rc)
894 break;
895 pcpu->state = CPU_STATE_CONFIGURED;
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200896 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400897 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100898 break;
899 default:
900 break;
901 }
902out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100903 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200904 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100905 return rc ? rc : count;
906}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800907static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +0100908#endif /* CONFIG_HOTPLUG_CPU */
909
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800910static ssize_t show_cpu_address(struct device *dev,
911 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100912{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400913 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +0100914}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800915static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100916
Heiko Carstens08d07962008-01-26 14:10:56 +0100917static struct attribute *cpu_common_attrs[] = {
918#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800919 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100920#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800921 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100922 NULL,
923};
924
925static struct attribute_group cpu_common_attr_group = {
926 .attrs = cpu_common_attrs,
927};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800929static ssize_t show_idle_count(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);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200933 unsigned long long idle_count;
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 sequence = ACCESS_ONCE(idle->sequence);
938 idle_count = ACCESS_ONCE(idle->idle_count);
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200939 if (ACCESS_ONCE(idle->clock_idle_enter))
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400940 idle_count++;
941 } while ((sequence & 1) || (idle->sequence != sequence));
Heiko Carstensfae8b222007-10-22 12:52:39 +0200942 return sprintf(buf, "%llu\n", idle_count);
943}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800944static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200945
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800946static ssize_t show_idle_time(struct device *dev,
947 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200948{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400949 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
950 unsigned long long now, idle_time, idle_enter, idle_exit;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200951 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200952
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400953 do {
Heiko Carstens1aae0562013-01-30 09:49:40 +0100954 now = get_tod_clock();
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400955 sequence = ACCESS_ONCE(idle->sequence);
956 idle_time = ACCESS_ONCE(idle->idle_time);
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200957 idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
958 idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400959 } while ((sequence & 1) || (idle->sequence != sequence));
960 idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100961 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200962}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800963static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200964
Heiko Carstens08d07962008-01-26 14:10:56 +0100965static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800966 &dev_attr_idle_count.attr,
967 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200968 NULL,
969};
970
Heiko Carstens08d07962008-01-26 14:10:56 +0100971static struct attribute_group cpu_online_attr_group = {
972 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200973};
974
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200975static int __cpuinit smp_cpu_notify(struct notifier_block *self,
976 unsigned long action, void *hcpu)
977{
978 unsigned int cpu = (unsigned int)(long)hcpu;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400979 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800980 struct device *s = &c->dev;
Akinobu Mitad882ba62010-05-26 14:43:34 -0700981 int err = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200982
Heiko Carstens1c725922012-08-27 15:43:49 +0200983 switch (action & ~CPU_TASKS_FROZEN) {
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200984 case CPU_ONLINE:
Akinobu Mitad882ba62010-05-26 14:43:34 -0700985 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200986 break;
987 case CPU_DEAD:
Heiko Carstens08d07962008-01-26 14:10:56 +0100988 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200989 break;
990 }
Akinobu Mitad882ba62010-05-26 14:43:34 -0700991 return notifier_from_errno(err);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200992}
993
Heiko Carstenseba61972012-12-27 14:03:36 +0100994static int __cpuinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100995{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400996 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800997 struct device *s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +0100998 int rc;
999
1000 c->hotpluggable = 1;
1001 rc = register_cpu(c, cpu);
1002 if (rc)
1003 goto out;
1004 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1005 if (rc)
1006 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +01001007 if (cpu_online(cpu)) {
1008 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1009 if (rc)
1010 goto out_online;
1011 }
1012 rc = topology_cpu_init(c);
1013 if (rc)
1014 goto out_topology;
1015 return 0;
1016
1017out_topology:
1018 if (cpu_online(cpu))
1019 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1020out_online:
Heiko Carstens08d07962008-01-26 14:10:56 +01001021 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1022out_cpu:
1023#ifdef CONFIG_HOTPLUG_CPU
1024 unregister_cpu(c);
1025#endif
1026out:
1027 return rc;
1028}
1029
1030#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001031
Heiko Carstens67060d92008-05-30 10:03:27 +02001032int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001033{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001034 struct sclp_cpu_info *info;
1035 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001036
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001037 info = smp_get_cpu_info();
1038 if (!info)
1039 return -ENOMEM;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001040 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001041 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001042 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001043 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001044 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001045 kfree(info);
1046 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001047 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001048 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001049}
1050
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001051static ssize_t __ref rescan_store(struct device *dev,
1052 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001053 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001054 size_t count)
1055{
1056 int rc;
1057
1058 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001059 return rc ? rc : count;
1060}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001061static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001062#endif /* CONFIG_HOTPLUG_CPU */
1063
Heiko Carstens83a24e32011-12-27 11:27:09 +01001064static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001065{
Heiko Carstens83a24e32011-12-27 11:27:09 +01001066 int cpu, rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001067
Heiko Carstens7755d6b2012-08-27 15:14:47 +02001068 hotcpu_notifier(smp_cpu_notify, 0);
Heiko Carstens08d07962008-01-26 14:10:56 +01001069#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001070 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001071 if (rc)
1072 return rc;
1073#endif
1074 for_each_present_cpu(cpu) {
1075 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001076 if (rc)
1077 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 return 0;
1080}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001081subsys_initcall(s390_smp_init);