blob: c88fe8569e62e35f8db5550f71b16e8227ca0224 [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 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040069 u16 address; /* physical cpu address */
70};
71
72static u8 boot_cpu_type;
73static u16 boot_cpu_address;
74static struct pcpu pcpu_devices[NR_CPUS];
75
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020076DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +010077
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040078/*
79 * Signal processor helper functions.
80 */
81static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstens5c0b9122009-09-11 10:29:05 +020082{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040083 register unsigned int reg1 asm ("1") = parm;
84 int cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020085
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040086 asm volatile(
87 " sigp %1,%2,0(%3)\n"
88 " ipm %0\n"
89 " srl %0,28\n"
90 : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
91 if (status && cc == 1)
92 *status = reg1;
93 return cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020094}
95
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040096static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010097{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040098 int cc;
99
100 while (1) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200101 cc = __pcpu_sigp(addr, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200102 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400103 return cc;
104 cpu_relax();
105 }
106}
107
108static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
109{
110 int cc, retry;
111
112 for (retry = 0; ; retry++) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200113 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200114 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400115 break;
116 if (retry >= 3)
117 udelay(10);
118 }
119 return cc;
120}
121
122static inline int pcpu_stopped(struct pcpu *pcpu)
123{
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200124 u32 status;
125
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200126 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200127 0, &status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400128 return 0;
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200129 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400130}
131
132static inline int pcpu_running(struct pcpu *pcpu)
133{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200134 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200135 0, NULL) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400136 return 1;
Heiko Carstens524b24a2012-06-04 12:11:41 +0200137 /* Status stored condition code is equivalent to cpu not running. */
138 return 0;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100139}
140
Michael Holzheu1943f532011-10-30 15:16:38 +0100141/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400142 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100143 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400144static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100145{
146 int cpu;
147
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400148 for_each_cpu(cpu, mask)
149 if (pcpu_devices[cpu].address == address)
150 return pcpu_devices + cpu;
151 return NULL;
152}
153
154static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
155{
156 int order;
157
158 set_bit(ec_bit, &pcpu->ec_mask);
159 order = pcpu_running(pcpu) ?
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200160 SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400161 pcpu_sigp_retry(pcpu, order, 0);
162}
163
164static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
165{
166 struct _lowcore *lc;
167
168 if (pcpu != &pcpu_devices[0]) {
169 pcpu->lowcore = (struct _lowcore *)
170 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
171 pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
172 pcpu->panic_stack = __get_free_page(GFP_KERNEL);
173 if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
174 goto out;
Michael Holzheu1943f532011-10-30 15:16:38 +0100175 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400176 lc = pcpu->lowcore;
177 memcpy(lc, &S390_lowcore, 512);
178 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
179 lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
180 lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
181 lc->cpu_nr = cpu;
182#ifndef CONFIG_64BIT
183 if (MACHINE_HAS_IEEE) {
184 lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
185 if (!lc->extended_save_area_addr)
186 goto out;
187 }
188#else
189 if (vdso_alloc_per_cpu(lc))
190 goto out;
191#endif
192 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200193 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400194 return 0;
195out:
196 if (pcpu != &pcpu_devices[0]) {
197 free_page(pcpu->panic_stack);
198 free_pages(pcpu->async_stack, ASYNC_ORDER);
199 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
200 }
201 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100202}
203
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200204#ifdef CONFIG_HOTPLUG_CPU
205
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400206static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100207{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200208 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400209 lowcore_ptr[pcpu - pcpu_devices] = NULL;
210#ifndef CONFIG_64BIT
211 if (MACHINE_HAS_IEEE) {
212 struct _lowcore *lc = pcpu->lowcore;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100213
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400214 free_page((unsigned long) lc->extended_save_area_addr);
215 lc->extended_save_area_addr = 0;
216 }
217#else
218 vdso_free_per_cpu(pcpu->lowcore);
219#endif
220 if (pcpu != &pcpu_devices[0]) {
221 free_page(pcpu->panic_stack);
222 free_pages(pcpu->async_stack, ASYNC_ORDER);
223 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
224 }
Heiko Carstens2c2df112010-02-26 22:37:34 +0100225}
226
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200227#endif /* CONFIG_HOTPLUG_CPU */
228
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400229static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100230{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400231 struct _lowcore *lc = pcpu->lowcore;
232
233 atomic_inc(&init_mm.context.attach_count);
234 lc->cpu_nr = cpu;
235 lc->percpu_offset = __per_cpu_offset[cpu];
236 lc->kernel_asce = S390_lowcore.kernel_asce;
237 lc->machine_flags = S390_lowcore.machine_flags;
238 lc->ftrace_func = S390_lowcore.ftrace_func;
239 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
240 __ctl_store(lc->cregs_save_area, 0, 15);
241 save_access_regs((unsigned int *) lc->access_regs_save_area);
242 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
243 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100244}
245
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400246static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
247{
248 struct _lowcore *lc = pcpu->lowcore;
249 struct thread_info *ti = task_thread_info(tsk);
250
251 lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
252 lc->thread_info = (unsigned long) task_thread_info(tsk);
253 lc->current_task = (unsigned long) tsk;
254 lc->user_timer = ti->user_timer;
255 lc->system_timer = ti->system_timer;
256 lc->steal_timer = 0;
257}
258
259static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
260{
261 struct _lowcore *lc = pcpu->lowcore;
262
263 lc->restart_stack = lc->kernel_stack;
264 lc->restart_fn = (unsigned long) func;
265 lc->restart_data = (unsigned long) data;
266 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200267 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400268}
269
270/*
271 * Call function via PSW restart on pcpu and stop the current cpu.
272 */
273static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
274 void *data, unsigned long stack)
275{
Michael Holzheu061da3d2012-05-24 14:38:26 +0200276 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
Heiko Carstensfbe76562012-06-05 09:59:52 +0200277 unsigned long source_cpu = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400278
279 __load_psw_mask(psw_kernel_bits);
Heiko Carstensfbe76562012-06-05 09:59:52 +0200280 if (pcpu->address == source_cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400281 func(data); /* should not return */
282 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200283 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400284 /* Restart func on the target cpu and stop the current cpu. */
Heiko Carstensfbe76562012-06-05 09:59:52 +0200285 mem_assign_absolute(lc->restart_stack, stack);
286 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
287 mem_assign_absolute(lc->restart_data, (unsigned long) data);
288 mem_assign_absolute(lc->restart_source, source_cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400289 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200290 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400291 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200292 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400293 " brc 2,1b # busy, try again\n"
Heiko Carstensfbe76562012-06-05 09:59:52 +0200294 : : "d" (pcpu->address), "d" (source_cpu),
Heiko Carstenseb546192012-06-04 15:05:43 +0200295 "K" (SIGP_RESTART), "K" (SIGP_STOP)
296 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400297 for (;;) ;
298}
299
300/*
301 * Call function on an online CPU.
302 */
303void smp_call_online_cpu(void (*func)(void *), void *data)
304{
305 struct pcpu *pcpu;
306
307 /* Use the current cpu if it is online. */
308 pcpu = pcpu_find_address(cpu_online_mask, stap());
309 if (!pcpu)
310 /* Use the first online cpu. */
311 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
312 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
313}
314
315/*
316 * Call function on the ipl CPU.
317 */
318void smp_call_ipl_cpu(void (*func)(void *), void *data)
319{
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400320 pcpu_delegate(&pcpu_devices[0], func, data,
321 pcpu_devices->panic_stack + PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400322}
323
324int smp_find_processor_id(u16 address)
325{
326 int cpu;
327
328 for_each_present_cpu(cpu)
329 if (pcpu_devices[cpu].address == address)
330 return cpu;
331 return -1;
332}
333
334int smp_vcpu_scheduled(int cpu)
335{
336 return pcpu_running(pcpu_devices + cpu);
337}
338
339void smp_yield(void)
340{
341 if (MACHINE_HAS_DIAG44)
342 asm volatile("diag 0,0,0x44");
343}
344
345void smp_yield_cpu(int cpu)
346{
347 if (MACHINE_HAS_DIAG9C)
348 asm volatile("diag %0,0,0x9c"
349 : : "d" (pcpu_devices[cpu].address));
350 else if (MACHINE_HAS_DIAG44)
351 asm volatile("diag 0,0,0x44");
352}
353
354/*
355 * Send cpus emergency shutdown signal. This gives the cpus the
356 * opportunity to complete outstanding interrupts.
357 */
358void smp_emergency_stop(cpumask_t *cpumask)
359{
360 u64 end;
361 int cpu;
362
363 end = get_clock() + (1000000UL << 12);
364 for_each_cpu(cpu, cpumask) {
365 struct pcpu *pcpu = pcpu_devices + cpu;
366 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200367 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
368 0, NULL) == SIGP_CC_BUSY &&
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400369 get_clock() < end)
370 cpu_relax();
371 }
372 while (get_clock() < end) {
373 for_each_cpu(cpu, cpumask)
374 if (pcpu_stopped(pcpu_devices + cpu))
375 cpumask_clear_cpu(cpu, cpumask);
376 if (cpumask_empty(cpumask))
377 break;
378 cpu_relax();
379 }
380}
381
382/*
383 * Stop all cpus but the current one.
384 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100385void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100387 cpumask_t cpumask;
388 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Heiko Carstens677d7622007-11-20 11:13:37 +0100390 /* Disable all interrupts/machine checks */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100391 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100392 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100393
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400394 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100395 cpumask_copy(&cpumask, cpu_online_mask);
396 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400398 if (oops_in_progress)
399 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100400
401 /* stop all processors */
402 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400403 struct pcpu *pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200404 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400405 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100406 cpu_relax();
407 }
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400411 * Stop the current cpu.
412 */
413void smp_stop_cpu(void)
414{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200415 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400416 for (;;) ;
417}
418
419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * This is the main routine where commands issued by other
421 * cpus are handled.
422 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400423static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200424 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200426 unsigned long bits;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400427 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400429 cpu = smp_processor_id();
Heiko Carstensfde15c32012-03-11 11:59:31 -0400430 if (ext_code.code == 0x1202)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400431 kstat_cpu(cpu).irqs[EXTINT_EXC]++;
Heiko Carstens2a3a2d62011-10-30 15:17:19 +0100432 else
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400433 kstat_cpu(cpu).irqs[EXTINT_EMS]++;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200434 /*
435 * handle bit signal external calls
Heiko Carstens39ce0102007-04-27 16:02:00 +0200436 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400437 bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100439 if (test_bit(ec_stop_cpu, &bits))
440 smp_stop_cpu();
441
Peter Zijlstra184748c2011-04-05 17:23:39 +0200442 if (test_bit(ec_schedule, &bits))
443 scheduler_ipi();
444
Heiko Carstens39ce0102007-04-27 16:02:00 +0200445 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100446 generic_smp_call_function_interrupt();
447
448 if (test_bit(ec_call_function_single, &bits))
449 generic_smp_call_function_single_interrupt();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Rusty Russell630cd042009-09-24 09:34:45 -0600453void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100454{
455 int cpu;
456
Rusty Russell630cd042009-09-24 09:34:45 -0600457 for_each_cpu(cpu, mask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400458 pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100459}
460
461void arch_send_call_function_single_ipi(int cpu)
462{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400463 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100464}
465
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800466#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
468 * this function sends a 'purge tlb' signal to another CPU.
469 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200470static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200472 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475void smp_ptlb_all(void)
476{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200477 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800480#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482/*
483 * this function sends a 'reschedule' IPI to another CPU.
484 * it goes straight through and wastes no time serializing
485 * anything. Worst case is that we lose a reschedule ...
486 */
487void smp_send_reschedule(int cpu)
488{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400489 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
493 * parameter area for the set/clear control bit callbacks
494 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200495struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400496 unsigned long orval;
497 unsigned long andval;
498 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200499};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501/*
502 * callback for setting/clearing control bits
503 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200504static void smp_ctl_bit_callback(void *info)
505{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200506 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200508
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200509 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400510 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200511 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/*
515 * Set a bit in a control register of all cpus
516 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200517void smp_ctl_set_bit(int cr, int bit)
518{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400519 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200521 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200523EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525/*
526 * Clear a bit in a control register of all cpus
527 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200528void smp_ctl_clear_bit(int cr, int bit)
529{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400530 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200532 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200534EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Michael Holzheu60a0c682011-10-30 15:16:40 +0100536#if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
Michael Holzheu411ed322007-04-27 16:01:49 +0200537
Heiko Carstensf64ca212010-02-26 22:37:32 +0100538struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200539EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
540
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400541static void __init smp_get_save_area(int cpu, u16 address)
Heiko Carstens08d07962008-01-26 14:10:56 +0100542{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400543 void *lc = pcpu_devices[0].lowcore;
544 struct save_area *save_area;
Heiko Carstens08d07962008-01-26 14:10:56 +0100545
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400546 if (is_kdump_kernel())
547 return;
548 if (!OLDMEM_BASE && (address == boot_cpu_address ||
549 ipl_info.type != IPL_TYPE_FCP_DUMP))
550 return;
551 if (cpu >= NR_CPUS) {
552 pr_warning("CPU %i exceeds the maximum %i and is excluded "
553 "from the dump\n", cpu, NR_CPUS - 1);
554 return;
Heiko Carstens08d07962008-01-26 14:10:56 +0100555 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400556 save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
557 if (!save_area)
558 panic("could not allocate memory for save area\n");
559 zfcpdump_save_areas[cpu] = save_area;
560#ifdef CONFIG_CRASH_DUMP
561 if (address == boot_cpu_address) {
562 /* Copy the registers of the boot cpu. */
563 copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
564 SAVE_AREA_BASE - PAGE_SIZE, 0);
565 return;
566 }
567#endif
568 /* Get the registers of a non-boot cpu. */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200569 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400570 memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
571}
572
573int smp_store_status(int cpu)
574{
575 struct pcpu *pcpu;
576
577 pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200578 if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
579 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400580 return -EIO;
Heiko Carstens08d07962008-01-26 14:10:56 +0100581 return 0;
582}
583
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400584#else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
Heiko Carstens08d07962008-01-26 14:10:56 +0100585
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400586static inline void smp_get_save_area(int cpu, u16 address) { }
Heiko Carstens08d07962008-01-26 14:10:56 +0100587
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400588#endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
589
590static struct sclp_cpu_info *smp_get_cpu_info(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100591{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400592 static int use_sigp_detection;
Heiko Carstens08d07962008-01-26 14:10:56 +0100593 struct sclp_cpu_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400594 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100595
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400596 info = kzalloc(sizeof(*info), GFP_KERNEL);
597 if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
598 use_sigp_detection = 1;
599 for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200600 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
601 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400602 continue;
603 info->cpu[info->configured].address = address;
604 info->configured++;
605 }
606 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100607 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400608 return info;
Heiko Carstens08d07962008-01-26 14:10:56 +0100609}
610
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400611static int __devinit smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100612
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400613static int __devinit __smp_rescan_cpus(struct sclp_cpu_info *info,
614 int sysfs_add)
615{
616 struct pcpu *pcpu;
617 cpumask_t avail;
618 int cpu, nr, i;
619
620 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200621 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400622 cpu = cpumask_first(&avail);
623 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
624 if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
625 continue;
626 if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
627 continue;
628 pcpu = pcpu_devices + cpu;
629 pcpu->address = info->cpu[i].address;
630 pcpu->state = (cpu >= info->configured) ?
631 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
632 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
633 set_cpu_present(cpu, true);
634 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
635 set_cpu_present(cpu, false);
636 else
637 nr++;
638 cpu = cpumask_next(cpu, &avail);
639 }
640 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Heiko Carstens48483b32008-01-26 14:11:05 +0100643static void __init smp_detect_cpus(void)
644{
645 unsigned int cpu, c_cpus, s_cpus;
646 struct sclp_cpu_info *info;
Heiko Carstens48483b32008-01-26 14:11:05 +0100647
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400648 info = smp_get_cpu_info();
Heiko Carstens48483b32008-01-26 14:11:05 +0100649 if (!info)
650 panic("smp_detect_cpus failed to allocate memory\n");
Heiko Carstens48483b32008-01-26 14:11:05 +0100651 if (info->has_cpu_type) {
652 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400653 if (info->cpu[cpu].address != boot_cpu_address)
654 continue;
655 /* The boot cpu dictates the cpu type. */
656 boot_cpu_type = info->cpu[cpu].type;
657 break;
Heiko Carstens48483b32008-01-26 14:11:05 +0100658 }
659 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400660 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100661 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400662 if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100663 continue;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400664 if (cpu < info->configured) {
665 smp_get_save_area(c_cpus, info->cpu[cpu].address);
666 c_cpus++;
667 } else
Heiko Carstens48483b32008-01-26 14:11:05 +0100668 s_cpus++;
Heiko Carstens48483b32008-01-26 14:11:05 +0100669 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100670 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100671 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400672 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100673 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400674 kfree(info);
Heiko Carstens48483b32008-01-26 14:11:05 +0100675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200678 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400680static void __cpuinit smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400682 S390_lowcore.last_update_clock = get_clock();
683 S390_lowcore.restart_stack = (unsigned long) restart_stack;
684 S390_lowcore.restart_fn = (unsigned long) do_restart;
685 S390_lowcore.restart_data = 0;
686 S390_lowcore.restart_source = -1UL;
687 restore_access_regs(S390_lowcore.access_regs_save_area);
688 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
689 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200690 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800691 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200692 init_cpu_timer();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200693 init_cpu_vtimer();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100694 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200695 notify_cpu_starting(smp_processor_id());
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200696 set_cpu_online(smp_processor_id(), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 local_irq_enable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200698 /* cpu_idle will call schedule for us */
699 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* Upping and downing of CPUs */
Thomas Gleixner8239c252012-04-20 13:05:42 +0000703int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400705 struct pcpu *pcpu;
706 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400708 pcpu = pcpu_devices + cpu;
709 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100710 return -EIO;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200711 if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
712 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400713 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000714
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400715 rc = pcpu_alloc_lowcore(pcpu, cpu);
716 if (rc)
717 return rc;
718 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000719 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400720 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 while (!cpu_online(cpu))
722 cpu_relax();
723 return 0;
724}
725
Heiko Carstens37a33022006-02-17 13:52:47 -0800726static int __init setup_possible_cpus(char *s)
727{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400728 int max, cpu;
Heiko Carstens48483b32008-01-26 14:11:05 +0100729
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400730 if (kstrtoint(s, 0, &max) < 0)
731 return 0;
Heiko Carstens88e01282009-04-14 15:36:25 +0200732 init_cpu_possible(cpumask_of(0));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400733 for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100734 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800735 return 0;
736}
737early_param("possible_cpus", setup_possible_cpus);
738
Heiko Carstens48483b32008-01-26 14:11:05 +0100739#ifdef CONFIG_HOTPLUG_CPU
740
Heiko Carstens39ce0102007-04-27 16:02:00 +0200741int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400743 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400745 set_cpu_online(smp_processor_id(), false);
746 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100747 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400748 /* Disable interrupt sources via control register. */
749 __ctl_store(cregs, 0, 15);
750 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
751 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
752 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
753 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Heiko Carstens39ce0102007-04-27 16:02:00 +0200757void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400759 struct pcpu *pcpu;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400762 pcpu = pcpu_devices + cpu;
763 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400765 pcpu_free_lowcore(pcpu);
Martin Schwidefsky050eef32010-08-24 09:26:21 +0200766 atomic_dec(&init_mm.context.attach_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Heiko Carstensb456d942011-05-23 10:24:33 +0200769void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 idle_task_exit();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200772 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400773 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Heiko Carstens255acee2006-02-17 13:52:46 -0800776#endif /* CONFIG_HOTPLUG_CPU */
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778void __init smp_prepare_cpus(unsigned int max_cpus)
779{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200780 /* request the 0x1201 emergency signal external interrupt */
781 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
782 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100783 /* request the 0x1202 external call external interrupt */
784 if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
785 panic("Couldn't request external interrupt 0x1202");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400786 smp_detect_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200789void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400791 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400793 boot_cpu_address = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400794 pcpu->state = CPU_STATE_CONFIGURED;
795 pcpu->address = boot_cpu_address;
796 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
797 pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
798 pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
799 S390_lowcore.percpu_offset = __per_cpu_offset[0];
800 cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200801 set_cpu_present(0, true);
802 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200805void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Heiko Carstens02beacc2010-01-13 20:44:34 +0100809void __init smp_setup_processor_id(void)
810{
811 S390_lowcore.cpu_nr = 0;
Heiko Carstens02beacc2010-01-13 20:44:34 +0100812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
815 * the frequency of the profiling timer can be changed
816 * by writing a multiplier value into /proc/profile.
817 *
818 * usually you want to run this on all CPUs ;)
819 */
820int setup_profiling_timer(unsigned int multiplier)
821{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Heiko Carstens08d07962008-01-26 14:10:56 +0100825#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800826static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400827 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100828{
829 ssize_t count;
830
831 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400832 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100833 mutex_unlock(&smp_cpu_state_mutex);
834 return count;
835}
836
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800837static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400838 struct device_attribute *attr,
839 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100840{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400841 struct pcpu *pcpu;
842 int cpu, val, rc;
Heiko Carstens08d07962008-01-26 14:10:56 +0100843 char delim;
844
845 if (sscanf(buf, "%d %c", &val, &delim) != 1)
846 return -EINVAL;
847 if (val != 0 && val != 1)
848 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100849 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200850 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100851 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100852 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400853 cpu = dev->id;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100854 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100855 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400856 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100857 rc = 0;
858 switch (val) {
859 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400860 if (pcpu->state != CPU_STATE_CONFIGURED)
861 break;
862 rc = sclp_cpu_deconfigure(pcpu->address);
863 if (rc)
864 break;
865 pcpu->state = CPU_STATE_STANDBY;
866 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
867 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100868 break;
869 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400870 if (pcpu->state != CPU_STATE_STANDBY)
871 break;
872 rc = sclp_cpu_configure(pcpu->address);
873 if (rc)
874 break;
875 pcpu->state = CPU_STATE_CONFIGURED;
876 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
877 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100878 break;
879 default:
880 break;
881 }
882out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100883 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200884 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100885 return rc ? rc : count;
886}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800887static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +0100888#endif /* CONFIG_HOTPLUG_CPU */
889
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800890static ssize_t show_cpu_address(struct device *dev,
891 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100892{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400893 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +0100894}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800895static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100896
Heiko Carstens08d07962008-01-26 14:10:56 +0100897static struct attribute *cpu_common_attrs[] = {
898#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800899 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100900#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800901 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100902 NULL,
903};
904
905static struct attribute_group cpu_common_attr_group = {
906 .attrs = cpu_common_attrs,
907};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800909static ssize_t show_idle_count(struct device *dev,
910 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200911{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400912 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200913 unsigned long long idle_count;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200914 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200915
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400916 do {
917 sequence = ACCESS_ONCE(idle->sequence);
918 idle_count = ACCESS_ONCE(idle->idle_count);
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200919 if (ACCESS_ONCE(idle->clock_idle_enter))
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400920 idle_count++;
921 } while ((sequence & 1) || (idle->sequence != sequence));
Heiko Carstensfae8b222007-10-22 12:52:39 +0200922 return sprintf(buf, "%llu\n", idle_count);
923}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800924static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200925
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800926static ssize_t show_idle_time(struct device *dev,
927 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200928{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400929 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
930 unsigned long long now, idle_time, idle_enter, idle_exit;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200931 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200932
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400933 do {
934 now = get_clock();
935 sequence = ACCESS_ONCE(idle->sequence);
936 idle_time = ACCESS_ONCE(idle->idle_time);
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200937 idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
938 idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400939 } while ((sequence & 1) || (idle->sequence != sequence));
940 idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100941 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200942}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800943static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200944
Heiko Carstens08d07962008-01-26 14:10:56 +0100945static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800946 &dev_attr_idle_count.attr,
947 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200948 NULL,
949};
950
Heiko Carstens08d07962008-01-26 14:10:56 +0100951static struct attribute_group cpu_online_attr_group = {
952 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200953};
954
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200955static int __cpuinit smp_cpu_notify(struct notifier_block *self,
956 unsigned long action, void *hcpu)
957{
958 unsigned int cpu = (unsigned int)(long)hcpu;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400959 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800960 struct device *s = &c->dev;
Akinobu Mitad882ba62010-05-26 14:43:34 -0700961 int err = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200962
Heiko Carstens1c725922012-08-27 15:43:49 +0200963 switch (action & ~CPU_TASKS_FROZEN) {
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200964 case CPU_ONLINE:
Akinobu Mitad882ba62010-05-26 14:43:34 -0700965 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200966 break;
967 case CPU_DEAD:
Heiko Carstens08d07962008-01-26 14:10:56 +0100968 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200969 break;
970 }
Akinobu Mitad882ba62010-05-26 14:43:34 -0700971 return notifier_from_errno(err);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200972}
973
Heiko Carstens2bc89b52008-02-05 16:50:40 +0100974static int __devinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100975{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400976 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800977 struct device *s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +0100978 int rc;
979
980 c->hotpluggable = 1;
981 rc = register_cpu(c, cpu);
982 if (rc)
983 goto out;
984 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
985 if (rc)
986 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +0100987 if (cpu_online(cpu)) {
988 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
989 if (rc)
990 goto out_online;
991 }
992 rc = topology_cpu_init(c);
993 if (rc)
994 goto out_topology;
995 return 0;
996
997out_topology:
998 if (cpu_online(cpu))
999 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1000out_online:
Heiko Carstens08d07962008-01-26 14:10:56 +01001001 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1002out_cpu:
1003#ifdef CONFIG_HOTPLUG_CPU
1004 unregister_cpu(c);
1005#endif
1006out:
1007 return rc;
1008}
1009
1010#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001011
Heiko Carstens67060d92008-05-30 10:03:27 +02001012int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001013{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001014 struct sclp_cpu_info *info;
1015 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001016
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001017 info = smp_get_cpu_info();
1018 if (!info)
1019 return -ENOMEM;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001020 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001021 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001022 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001023 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001024 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001025 kfree(info);
1026 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001027 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001028 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001029}
1030
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001031static ssize_t __ref rescan_store(struct device *dev,
1032 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001033 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001034 size_t count)
1035{
1036 int rc;
1037
1038 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001039 return rc ? rc : count;
1040}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001041static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001042#endif /* CONFIG_HOTPLUG_CPU */
1043
Heiko Carstens83a24e32011-12-27 11:27:09 +01001044static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001045{
Heiko Carstens83a24e32011-12-27 11:27:09 +01001046 int cpu, rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001047
Heiko Carstens7755d6b2012-08-27 15:14:47 +02001048 hotcpu_notifier(smp_cpu_notify, 0);
Heiko Carstens08d07962008-01-26 14:10:56 +01001049#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001050 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001051 if (rc)
1052 return rc;
1053#endif
1054 for_each_present_cpu(cpu) {
1055 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001056 if (rc)
1057 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 return 0;
1060}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001061subsys_initcall(s390_smp_init);