blob: 3b9e5c9f4c0bf32dbcf3f0b2928584580d6c0427 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04002 * SMP related functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04004 * Copyright IBM Corp. 1999,2012
5 * Author(s): Denis Joseph Barrow,
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Heiko Carstens39ce0102007-04-27 16:02:00 +02009 * based on other smp stuff by
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
11 * (c) 1998 Ingo Molnar
12 *
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040013 * The code outside of smp.c uses logical cpu numbers, only smp.c does
14 * the translation of logical to physical cpu ids. All new code that
15 * operates on physical cpu numbers needs to go into smp.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Martin Schwidefsky395d31d2008-12-25 13:39:50 +010018#define KMSG_COMPONENT "cpu"
19#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
Heiko Carstensf2308862011-01-05 12:48:08 +010021#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040025#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/spinlock.h>
27#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
Christian Borntraeger3324e602009-03-26 15:23:56 +010030#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010033#include <linux/crash_dump.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010034#include <asm/asm-offsets.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010035#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010036#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010039#include <asm/timer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020040#include <asm/lowcore.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010041#include <asm/sclp.h>
Martin Schwidefskyc742b312008-12-31 15:11:42 +010042#include <asm/vdso.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040043#include <asm/debug.h>
Michael Holzheu4857d4b2012-03-11 11:59:34 -040044#include <asm/os_info.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020045#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040047enum {
48 sigp_sense = 1,
49 sigp_external_call = 2,
50 sigp_emergency_signal = 3,
51 sigp_start = 4,
52 sigp_stop = 5,
53 sigp_restart = 6,
54 sigp_stop_and_store_status = 9,
55 sigp_initial_cpu_reset = 11,
56 sigp_cpu_reset = 12,
57 sigp_set_prefix = 13,
58 sigp_store_status_at_address = 14,
59 sigp_store_extended_status_at_address = 15,
60 sigp_set_architecture = 18,
61 sigp_conditional_emergency_signal = 19,
62 sigp_sense_running = 21,
63};
Heiko Carstensfb380aa2010-01-13 20:44:37 +010064
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040065enum {
66 sigp_order_code_accepted = 0,
67 sigp_status_stored = 1,
68 sigp_busy = 2,
69 sigp_not_operational = 3,
70};
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040072enum {
73 ec_schedule = 0,
74 ec_call_function,
75 ec_call_function_single,
76 ec_stop_cpu,
77};
Heiko Carstens08d07962008-01-26 14:10:56 +010078
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040079enum {
Heiko Carstens08d07962008-01-26 14:10:56 +010080 CPU_STATE_STANDBY,
81 CPU_STATE_CONFIGURED,
82};
83
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040084struct pcpu {
85 struct cpu cpu;
86 struct task_struct *idle; /* idle process for the cpu */
87 struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
88 unsigned long async_stack; /* async stack for the cpu */
89 unsigned long panic_stack; /* panic stack for the cpu */
90 unsigned long ec_mask; /* bit mask for ec_xxx functions */
91 int state; /* physical cpu state */
92 u32 status; /* last status received via sigp */
93 u16 address; /* physical cpu address */
94};
95
96static u8 boot_cpu_type;
97static u16 boot_cpu_address;
98static struct pcpu pcpu_devices[NR_CPUS];
99
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200100DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100101
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400102/*
103 * Signal processor helper functions.
104 */
105static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200106{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400107 register unsigned int reg1 asm ("1") = parm;
108 int cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200109
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400110 asm volatile(
111 " sigp %1,%2,0(%3)\n"
112 " ipm %0\n"
113 " srl %0,28\n"
114 : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
115 if (status && cc == 1)
116 *status = reg1;
117 return cc;
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200118}
119
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400120static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100121{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400122 int cc;
123
124 while (1) {
125 cc = __pcpu_sigp(addr, order, parm, status);
126 if (cc != sigp_busy)
127 return cc;
128 cpu_relax();
129 }
130}
131
132static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
133{
134 int cc, retry;
135
136 for (retry = 0; ; retry++) {
137 cc = __pcpu_sigp(pcpu->address, order, parm, &pcpu->status);
138 if (cc != sigp_busy)
139 break;
140 if (retry >= 3)
141 udelay(10);
142 }
143 return cc;
144}
145
146static inline int pcpu_stopped(struct pcpu *pcpu)
147{
148 if (__pcpu_sigp(pcpu->address, sigp_sense,
149 0, &pcpu->status) != sigp_status_stored)
150 return 0;
151 /* Check for stopped and check stop state */
152 return !!(pcpu->status & 0x50);
153}
154
155static inline int pcpu_running(struct pcpu *pcpu)
156{
157 if (__pcpu_sigp(pcpu->address, sigp_sense_running,
158 0, &pcpu->status) != sigp_status_stored)
159 return 1;
160 /* Check for running status */
161 return !(pcpu->status & 0x400);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100162}
163
Michael Holzheu1943f532011-10-30 15:16:38 +0100164/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400165 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100166 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400167static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100168{
169 int cpu;
170
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400171 for_each_cpu(cpu, mask)
172 if (pcpu_devices[cpu].address == address)
173 return pcpu_devices + cpu;
174 return NULL;
175}
176
177static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
178{
179 int order;
180
181 set_bit(ec_bit, &pcpu->ec_mask);
182 order = pcpu_running(pcpu) ?
183 sigp_external_call : sigp_emergency_signal;
184 pcpu_sigp_retry(pcpu, order, 0);
185}
186
187static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
188{
189 struct _lowcore *lc;
190
191 if (pcpu != &pcpu_devices[0]) {
192 pcpu->lowcore = (struct _lowcore *)
193 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
194 pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
195 pcpu->panic_stack = __get_free_page(GFP_KERNEL);
196 if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
197 goto out;
Michael Holzheu1943f532011-10-30 15:16:38 +0100198 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400199 lc = pcpu->lowcore;
200 memcpy(lc, &S390_lowcore, 512);
201 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
202 lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
203 lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
204 lc->cpu_nr = cpu;
205#ifndef CONFIG_64BIT
206 if (MACHINE_HAS_IEEE) {
207 lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
208 if (!lc->extended_save_area_addr)
209 goto out;
210 }
211#else
212 if (vdso_alloc_per_cpu(lc))
213 goto out;
214#endif
215 lowcore_ptr[cpu] = lc;
216 pcpu_sigp_retry(pcpu, sigp_set_prefix, (u32)(unsigned long) lc);
217 return 0;
218out:
219 if (pcpu != &pcpu_devices[0]) {
220 free_page(pcpu->panic_stack);
221 free_pages(pcpu->async_stack, ASYNC_ORDER);
222 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
223 }
224 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100225}
226
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400227static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100228{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400229 pcpu_sigp_retry(pcpu, sigp_set_prefix, 0);
230 lowcore_ptr[pcpu - pcpu_devices] = NULL;
231#ifndef CONFIG_64BIT
232 if (MACHINE_HAS_IEEE) {
233 struct _lowcore *lc = pcpu->lowcore;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100234
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400235 free_page((unsigned long) lc->extended_save_area_addr);
236 lc->extended_save_area_addr = 0;
237 }
238#else
239 vdso_free_per_cpu(pcpu->lowcore);
240#endif
241 if (pcpu != &pcpu_devices[0]) {
242 free_page(pcpu->panic_stack);
243 free_pages(pcpu->async_stack, ASYNC_ORDER);
244 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
245 }
Heiko Carstens2c2df112010-02-26 22:37:34 +0100246}
247
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400248static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100249{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400250 struct _lowcore *lc = pcpu->lowcore;
251
252 atomic_inc(&init_mm.context.attach_count);
253 lc->cpu_nr = cpu;
254 lc->percpu_offset = __per_cpu_offset[cpu];
255 lc->kernel_asce = S390_lowcore.kernel_asce;
256 lc->machine_flags = S390_lowcore.machine_flags;
257 lc->ftrace_func = S390_lowcore.ftrace_func;
258 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
259 __ctl_store(lc->cregs_save_area, 0, 15);
260 save_access_regs((unsigned int *) lc->access_regs_save_area);
261 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
262 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100263}
264
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400265static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
266{
267 struct _lowcore *lc = pcpu->lowcore;
268 struct thread_info *ti = task_thread_info(tsk);
269
270 lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
271 lc->thread_info = (unsigned long) task_thread_info(tsk);
272 lc->current_task = (unsigned long) tsk;
273 lc->user_timer = ti->user_timer;
274 lc->system_timer = ti->system_timer;
275 lc->steal_timer = 0;
276}
277
278static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
279{
280 struct _lowcore *lc = pcpu->lowcore;
281
282 lc->restart_stack = lc->kernel_stack;
283 lc->restart_fn = (unsigned long) func;
284 lc->restart_data = (unsigned long) data;
285 lc->restart_source = -1UL;
286 pcpu_sigp_retry(pcpu, sigp_restart, 0);
287}
288
289/*
290 * Call function via PSW restart on pcpu and stop the current cpu.
291 */
292static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
293 void *data, unsigned long stack)
294{
295 struct _lowcore *lc = pcpu->lowcore;
296 unsigned short this_cpu;
297
298 __load_psw_mask(psw_kernel_bits);
299 this_cpu = stap();
300 if (pcpu->address == this_cpu)
301 func(data); /* should not return */
302 /* Stop target cpu (if func returns this stops the current cpu). */
303 pcpu_sigp_retry(pcpu, sigp_stop, 0);
304 /* Restart func on the target cpu and stop the current cpu. */
305 lc->restart_stack = stack;
306 lc->restart_fn = (unsigned long) func;
307 lc->restart_data = (unsigned long) data;
308 lc->restart_source = (unsigned long) this_cpu;
309 asm volatile(
310 "0: sigp 0,%0,6 # sigp restart to target cpu\n"
311 " brc 2,0b # busy, try again\n"
312 "1: sigp 0,%1,5 # sigp stop to current cpu\n"
313 " brc 2,1b # busy, try again\n"
314 : : "d" (pcpu->address), "d" (this_cpu) : "0", "1", "cc");
315 for (;;) ;
316}
317
318/*
319 * Call function on an online CPU.
320 */
321void smp_call_online_cpu(void (*func)(void *), void *data)
322{
323 struct pcpu *pcpu;
324
325 /* Use the current cpu if it is online. */
326 pcpu = pcpu_find_address(cpu_online_mask, stap());
327 if (!pcpu)
328 /* Use the first online cpu. */
329 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
330 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
331}
332
333/*
334 * Call function on the ipl CPU.
335 */
336void smp_call_ipl_cpu(void (*func)(void *), void *data)
337{
338 pcpu_delegate(&pcpu_devices[0], func, data, pcpu_devices->panic_stack);
339}
340
341int smp_find_processor_id(u16 address)
342{
343 int cpu;
344
345 for_each_present_cpu(cpu)
346 if (pcpu_devices[cpu].address == address)
347 return cpu;
348 return -1;
349}
350
351int smp_vcpu_scheduled(int cpu)
352{
353 return pcpu_running(pcpu_devices + cpu);
354}
355
356void smp_yield(void)
357{
358 if (MACHINE_HAS_DIAG44)
359 asm volatile("diag 0,0,0x44");
360}
361
362void smp_yield_cpu(int cpu)
363{
364 if (MACHINE_HAS_DIAG9C)
365 asm volatile("diag %0,0,0x9c"
366 : : "d" (pcpu_devices[cpu].address));
367 else if (MACHINE_HAS_DIAG44)
368 asm volatile("diag 0,0,0x44");
369}
370
371/*
372 * Send cpus emergency shutdown signal. This gives the cpus the
373 * opportunity to complete outstanding interrupts.
374 */
375void smp_emergency_stop(cpumask_t *cpumask)
376{
377 u64 end;
378 int cpu;
379
380 end = get_clock() + (1000000UL << 12);
381 for_each_cpu(cpu, cpumask) {
382 struct pcpu *pcpu = pcpu_devices + cpu;
383 set_bit(ec_stop_cpu, &pcpu->ec_mask);
384 while (__pcpu_sigp(pcpu->address, sigp_emergency_signal,
385 0, NULL) == sigp_busy &&
386 get_clock() < end)
387 cpu_relax();
388 }
389 while (get_clock() < end) {
390 for_each_cpu(cpu, cpumask)
391 if (pcpu_stopped(pcpu_devices + cpu))
392 cpumask_clear_cpu(cpu, cpumask);
393 if (cpumask_empty(cpumask))
394 break;
395 cpu_relax();
396 }
397}
398
399/*
400 * Stop all cpus but the current one.
401 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100402void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100404 cpumask_t cpumask;
405 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Heiko Carstens677d7622007-11-20 11:13:37 +0100407 /* Disable all interrupts/machine checks */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100408 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100409 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100410
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400411 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100412 cpumask_copy(&cpumask, cpu_online_mask);
413 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400415 if (oops_in_progress)
416 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100417
418 /* stop all processors */
419 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400420 struct pcpu *pcpu = pcpu_devices + cpu;
421 pcpu_sigp_retry(pcpu, sigp_stop, 0);
422 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100423 cpu_relax();
424 }
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400428 * Stop the current cpu.
429 */
430void smp_stop_cpu(void)
431{
432 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), sigp_stop, 0);
433 for (;;) ;
434}
435
436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * This is the main routine where commands issued by other
438 * cpus are handled.
439 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400440static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200441 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200443 unsigned long bits;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400444 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400446 cpu = smp_processor_id();
Heiko Carstensfde15c32012-03-11 11:59:31 -0400447 if (ext_code.code == 0x1202)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400448 kstat_cpu(cpu).irqs[EXTINT_EXC]++;
Heiko Carstens2a3a2d62011-10-30 15:17:19 +0100449 else
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400450 kstat_cpu(cpu).irqs[EXTINT_EMS]++;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200451 /*
452 * handle bit signal external calls
Heiko Carstens39ce0102007-04-27 16:02:00 +0200453 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400454 bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100456 if (test_bit(ec_stop_cpu, &bits))
457 smp_stop_cpu();
458
Peter Zijlstra184748c2011-04-05 17:23:39 +0200459 if (test_bit(ec_schedule, &bits))
460 scheduler_ipi();
461
Heiko Carstens39ce0102007-04-27 16:02:00 +0200462 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100463 generic_smp_call_function_interrupt();
464
465 if (test_bit(ec_call_function_single, &bits))
466 generic_smp_call_function_single_interrupt();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Rusty Russell630cd042009-09-24 09:34:45 -0600470void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100471{
472 int cpu;
473
Rusty Russell630cd042009-09-24 09:34:45 -0600474 for_each_cpu(cpu, mask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400475 pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100476}
477
478void arch_send_call_function_single_ipi(int cpu)
479{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400480 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100481}
482
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800483#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * this function sends a 'purge tlb' signal to another CPU.
486 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200487static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200489 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492void smp_ptlb_all(void)
493{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200494 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800497#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499/*
500 * this function sends a 'reschedule' IPI to another CPU.
501 * it goes straight through and wastes no time serializing
502 * anything. Worst case is that we lose a reschedule ...
503 */
504void smp_send_reschedule(int cpu)
505{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400506 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/*
510 * parameter area for the set/clear control bit callbacks
511 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200512struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400513 unsigned long orval;
514 unsigned long andval;
515 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200516};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518/*
519 * callback for setting/clearing control bits
520 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200521static void smp_ctl_bit_callback(void *info)
522{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200523 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200525
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200526 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400527 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200528 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * Set a bit in a control register of all cpus
533 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200534void smp_ctl_set_bit(int cr, int bit)
535{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400536 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200538 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200540EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542/*
543 * Clear a bit in a control register of all cpus
544 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200545void smp_ctl_clear_bit(int cr, int bit)
546{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400547 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200549 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200551EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Michael Holzheu60a0c682011-10-30 15:16:40 +0100553#if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
Michael Holzheu411ed322007-04-27 16:01:49 +0200554
Heiko Carstensf64ca212010-02-26 22:37:32 +0100555struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200556EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
557
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400558static void __init smp_get_save_area(int cpu, u16 address)
Heiko Carstens08d07962008-01-26 14:10:56 +0100559{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400560 void *lc = pcpu_devices[0].lowcore;
561 struct save_area *save_area;
Heiko Carstens08d07962008-01-26 14:10:56 +0100562
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400563 if (is_kdump_kernel())
564 return;
565 if (!OLDMEM_BASE && (address == boot_cpu_address ||
566 ipl_info.type != IPL_TYPE_FCP_DUMP))
567 return;
568 if (cpu >= NR_CPUS) {
569 pr_warning("CPU %i exceeds the maximum %i and is excluded "
570 "from the dump\n", cpu, NR_CPUS - 1);
571 return;
Heiko Carstens08d07962008-01-26 14:10:56 +0100572 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400573 save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
574 if (!save_area)
575 panic("could not allocate memory for save area\n");
576 zfcpdump_save_areas[cpu] = save_area;
577#ifdef CONFIG_CRASH_DUMP
578 if (address == boot_cpu_address) {
579 /* Copy the registers of the boot cpu. */
580 copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
581 SAVE_AREA_BASE - PAGE_SIZE, 0);
582 return;
583 }
584#endif
585 /* Get the registers of a non-boot cpu. */
586 __pcpu_sigp_relax(address, sigp_stop_and_store_status, 0, NULL);
587 memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
588}
589
590int smp_store_status(int cpu)
591{
592 struct pcpu *pcpu;
593
594 pcpu = pcpu_devices + cpu;
595 if (__pcpu_sigp_relax(pcpu->address, sigp_stop_and_store_status,
596 0, NULL) != sigp_order_code_accepted)
597 return -EIO;
Heiko Carstens08d07962008-01-26 14:10:56 +0100598 return 0;
599}
600
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400601#else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
Heiko Carstens08d07962008-01-26 14:10:56 +0100602
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400603static inline void smp_get_save_area(int cpu, u16 address) { }
Heiko Carstens08d07962008-01-26 14:10:56 +0100604
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400605#endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
606
607static struct sclp_cpu_info *smp_get_cpu_info(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100608{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400609 static int use_sigp_detection;
Heiko Carstens08d07962008-01-26 14:10:56 +0100610 struct sclp_cpu_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400611 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100612
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400613 info = kzalloc(sizeof(*info), GFP_KERNEL);
614 if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
615 use_sigp_detection = 1;
616 for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
617 if (__pcpu_sigp_relax(address, sigp_sense, 0, NULL) ==
618 sigp_not_operational)
619 continue;
620 info->cpu[info->configured].address = address;
621 info->configured++;
622 }
623 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100624 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400625 return info;
Heiko Carstens08d07962008-01-26 14:10:56 +0100626}
627
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400628static int __devinit smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100629
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400630static int __devinit __smp_rescan_cpus(struct sclp_cpu_info *info,
631 int sysfs_add)
632{
633 struct pcpu *pcpu;
634 cpumask_t avail;
635 int cpu, nr, i;
636
637 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200638 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400639 cpu = cpumask_first(&avail);
640 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
641 if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
642 continue;
643 if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
644 continue;
645 pcpu = pcpu_devices + cpu;
646 pcpu->address = info->cpu[i].address;
647 pcpu->state = (cpu >= info->configured) ?
648 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
649 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
650 set_cpu_present(cpu, true);
651 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
652 set_cpu_present(cpu, false);
653 else
654 nr++;
655 cpu = cpumask_next(cpu, &avail);
656 }
657 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Heiko Carstens48483b32008-01-26 14:11:05 +0100660static void __init smp_detect_cpus(void)
661{
662 unsigned int cpu, c_cpus, s_cpus;
663 struct sclp_cpu_info *info;
Heiko Carstens48483b32008-01-26 14:11:05 +0100664
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400665 info = smp_get_cpu_info();
Heiko Carstens48483b32008-01-26 14:11:05 +0100666 if (!info)
667 panic("smp_detect_cpus failed to allocate memory\n");
Heiko Carstens48483b32008-01-26 14:11:05 +0100668 if (info->has_cpu_type) {
669 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400670 if (info->cpu[cpu].address != boot_cpu_address)
671 continue;
672 /* The boot cpu dictates the cpu type. */
673 boot_cpu_type = info->cpu[cpu].type;
674 break;
Heiko Carstens48483b32008-01-26 14:11:05 +0100675 }
676 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400677 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100678 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400679 if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100680 continue;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400681 if (cpu < info->configured) {
682 smp_get_save_area(c_cpus, info->cpu[cpu].address);
683 c_cpus++;
684 } else
Heiko Carstens48483b32008-01-26 14:11:05 +0100685 s_cpus++;
Heiko Carstens48483b32008-01-26 14:11:05 +0100686 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100687 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100688 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400689 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100690 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400691 kfree(info);
Heiko Carstens48483b32008-01-26 14:11:05 +0100692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200695 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400697static void __cpuinit smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400699 S390_lowcore.last_update_clock = get_clock();
700 S390_lowcore.restart_stack = (unsigned long) restart_stack;
701 S390_lowcore.restart_fn = (unsigned long) do_restart;
702 S390_lowcore.restart_data = 0;
703 S390_lowcore.restart_source = -1UL;
704 restore_access_regs(S390_lowcore.access_regs_save_area);
705 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
706 __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200707 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800708 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200709 init_cpu_timer();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200710 init_cpu_vtimer();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100711 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200712 notify_cpu_starting(smp_processor_id());
Heiko Carstensca9fc752008-12-25 13:38:39 +0100713 ipi_call_lock();
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200714 set_cpu_online(smp_processor_id(), true);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100715 ipi_call_unlock();
Heiko Carstenscc343212011-08-03 16:44:27 +0200716 /*
717 * Wait until the cpu which brought this one up marked it
718 * active before enabling interrupts.
719 */
720 while (!cpumask_test_cpu(smp_processor_id(), cpu_active_mask))
721 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 local_irq_enable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200723 /* cpu_idle will call schedule for us */
724 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Heiko Carstensf2308862011-01-05 12:48:08 +0100727struct create_idle {
728 struct work_struct work;
729 struct task_struct *idle;
730 struct completion done;
731 int cpu;
732};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Heiko Carstensf2308862011-01-05 12:48:08 +0100734static void __cpuinit smp_fork_idle(struct work_struct *work)
735{
736 struct create_idle *c_idle;
737
738 c_idle = container_of(work, struct create_idle, work);
739 c_idle->idle = fork_idle(c_idle->cpu);
740 complete(&c_idle->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/* Upping and downing of CPUs */
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100744int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Heiko Carstensf2308862011-01-05 12:48:08 +0100746 struct create_idle c_idle;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400747 struct pcpu *pcpu;
748 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400750 pcpu = pcpu_devices + cpu;
751 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100752 return -EIO;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400753 if (pcpu_sigp_retry(pcpu, sigp_initial_cpu_reset, 0) !=
754 sigp_order_code_accepted)
755 return -EIO;
756 if (!pcpu->idle) {
Heiko Carstensf2308862011-01-05 12:48:08 +0100757 c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
758 INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
759 c_idle.cpu = cpu;
760 schedule_work(&c_idle.work);
761 wait_for_completion(&c_idle.done);
762 if (IS_ERR(c_idle.idle))
763 return PTR_ERR(c_idle.idle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400764 pcpu->idle = c_idle.idle;
Heiko Carstensf2308862011-01-05 12:48:08 +0100765 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400766 init_idle(pcpu->idle, cpu);
767 rc = pcpu_alloc_lowcore(pcpu, cpu);
768 if (rc)
769 return rc;
770 pcpu_prepare_secondary(pcpu, cpu);
771 pcpu_attach_task(pcpu, pcpu->idle);
772 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 while (!cpu_online(cpu))
774 cpu_relax();
775 return 0;
776}
777
Heiko Carstens37a33022006-02-17 13:52:47 -0800778static int __init setup_possible_cpus(char *s)
779{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400780 int max, cpu;
Heiko Carstens48483b32008-01-26 14:11:05 +0100781
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400782 if (kstrtoint(s, 0, &max) < 0)
783 return 0;
Heiko Carstens88e01282009-04-14 15:36:25 +0200784 init_cpu_possible(cpumask_of(0));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400785 for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100786 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800787 return 0;
788}
789early_param("possible_cpus", setup_possible_cpus);
790
Heiko Carstens48483b32008-01-26 14:11:05 +0100791#ifdef CONFIG_HOTPLUG_CPU
792
Heiko Carstens39ce0102007-04-27 16:02:00 +0200793int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400795 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400797 set_cpu_online(smp_processor_id(), false);
798 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100799 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400800 /* Disable interrupt sources via control register. */
801 __ctl_store(cregs, 0, 15);
802 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
803 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
804 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
805 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 0;
807}
808
Heiko Carstens39ce0102007-04-27 16:02:00 +0200809void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400811 struct pcpu *pcpu;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400814 pcpu = pcpu_devices + cpu;
815 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400817 pcpu_free_lowcore(pcpu);
Martin Schwidefsky050eef32010-08-24 09:26:21 +0200818 atomic_dec(&init_mm.context.attach_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Heiko Carstensb456d942011-05-23 10:24:33 +0200821void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 idle_task_exit();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400824 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), sigp_stop, 0);
825 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Heiko Carstens255acee2006-02-17 13:52:46 -0800828#endif /* CONFIG_HOTPLUG_CPU */
829
Michael Holzheu4857d4b2012-03-11 11:59:34 -0400830static void smp_call_os_info_init_fn(void)
831{
832 int (*init_fn)(void);
833 unsigned long size;
834
835 init_fn = os_info_old_entry(OS_INFO_INIT_FN, &size);
836 if (!init_fn)
837 return;
838 init_fn();
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841void __init smp_prepare_cpus(unsigned int max_cpus)
842{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200843 /* request the 0x1201 emergency signal external interrupt */
844 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
845 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100846 /* request the 0x1202 external call external interrupt */
847 if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
848 panic("Couldn't request external interrupt 0x1202");
Michael Holzheu4857d4b2012-03-11 11:59:34 -0400849 smp_call_os_info_init_fn();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400850 smp_detect_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200853void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400855 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400857 boot_cpu_address = stap();
858 pcpu->idle = current;
859 pcpu->state = CPU_STATE_CONFIGURED;
860 pcpu->address = boot_cpu_address;
861 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
862 pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
863 pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
864 S390_lowcore.percpu_offset = __per_cpu_offset[0];
865 cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200866 set_cpu_present(0, true);
867 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200870void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Heiko Carstens02beacc2010-01-13 20:44:34 +0100874void __init smp_setup_processor_id(void)
875{
876 S390_lowcore.cpu_nr = 0;
Heiko Carstens02beacc2010-01-13 20:44:34 +0100877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/*
880 * the frequency of the profiling timer can be changed
881 * by writing a multiplier value into /proc/profile.
882 *
883 * usually you want to run this on all CPUs ;)
884 */
885int setup_profiling_timer(unsigned int multiplier)
886{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Heiko Carstens08d07962008-01-26 14:10:56 +0100890#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800891static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400892 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100893{
894 ssize_t count;
895
896 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400897 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100898 mutex_unlock(&smp_cpu_state_mutex);
899 return count;
900}
901
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800902static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400903 struct device_attribute *attr,
904 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100905{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400906 struct pcpu *pcpu;
907 int cpu, val, rc;
Heiko Carstens08d07962008-01-26 14:10:56 +0100908 char delim;
909
910 if (sscanf(buf, "%d %c", &val, &delim) != 1)
911 return -EINVAL;
912 if (val != 0 && val != 1)
913 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100914 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200915 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100916 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100917 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400918 cpu = dev->id;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100919 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100920 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400921 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100922 rc = 0;
923 switch (val) {
924 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400925 if (pcpu->state != CPU_STATE_CONFIGURED)
926 break;
927 rc = sclp_cpu_deconfigure(pcpu->address);
928 if (rc)
929 break;
930 pcpu->state = CPU_STATE_STANDBY;
931 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
932 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100933 break;
934 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400935 if (pcpu->state != CPU_STATE_STANDBY)
936 break;
937 rc = sclp_cpu_configure(pcpu->address);
938 if (rc)
939 break;
940 pcpu->state = CPU_STATE_CONFIGURED;
941 cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
942 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100943 break;
944 default:
945 break;
946 }
947out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100948 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200949 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100950 return rc ? rc : count;
951}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800952static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +0100953#endif /* CONFIG_HOTPLUG_CPU */
954
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800955static ssize_t show_cpu_address(struct device *dev,
956 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100957{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400958 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +0100959}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800960static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100961
Heiko Carstens08d07962008-01-26 14:10:56 +0100962static struct attribute *cpu_common_attrs[] = {
963#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800964 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100965#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800966 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100967 NULL,
968};
969
970static struct attribute_group cpu_common_attr_group = {
971 .attrs = cpu_common_attrs,
972};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800974static ssize_t show_capability(struct device *dev,
975 struct device_attribute *attr, char *buf)
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200976{
977 unsigned int capability;
978 int rc;
979
980 rc = get_cpu_capability(&capability);
981 if (rc)
982 return rc;
983 return sprintf(buf, "%u\n", capability);
984}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800985static DEVICE_ATTR(capability, 0444, show_capability, NULL);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200986
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800987static ssize_t show_idle_count(struct device *dev,
988 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200989{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400990 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200991 unsigned long long idle_count;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200992 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200993
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -0400994 do {
995 sequence = ACCESS_ONCE(idle->sequence);
996 idle_count = ACCESS_ONCE(idle->idle_count);
997 if (ACCESS_ONCE(idle->idle_enter))
998 idle_count++;
999 } while ((sequence & 1) || (idle->sequence != sequence));
Heiko Carstensfae8b222007-10-22 12:52:39 +02001000 return sprintf(buf, "%llu\n", idle_count);
1001}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001002static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001003
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001004static ssize_t show_idle_time(struct device *dev,
1005 struct device_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +02001006{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -04001007 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
1008 unsigned long long now, idle_time, idle_enter, idle_exit;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +02001009 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +02001010
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -04001011 do {
1012 now = get_clock();
1013 sequence = ACCESS_ONCE(idle->sequence);
1014 idle_time = ACCESS_ONCE(idle->idle_time);
1015 idle_enter = ACCESS_ONCE(idle->idle_enter);
1016 idle_exit = ACCESS_ONCE(idle->idle_exit);
1017 } while ((sequence & 1) || (idle->sequence != sequence));
1018 idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
Martin Schwidefsky6f430922008-12-31 15:11:40 +01001019 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001020}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001021static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001022
Heiko Carstens08d07962008-01-26 14:10:56 +01001023static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001024 &dev_attr_capability.attr,
1025 &dev_attr_idle_count.attr,
1026 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001027 NULL,
1028};
1029
Heiko Carstens08d07962008-01-26 14:10:56 +01001030static struct attribute_group cpu_online_attr_group = {
1031 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001032};
1033
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001034static int __cpuinit smp_cpu_notify(struct notifier_block *self,
1035 unsigned long action, void *hcpu)
1036{
1037 unsigned int cpu = (unsigned int)(long)hcpu;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001038 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001039 struct device *s = &c->dev;
Heiko Carstensfae8b222007-10-22 12:52:39 +02001040 struct s390_idle_data *idle;
Akinobu Mitad882ba62010-05-26 14:43:34 -07001041 int err = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001042
1043 switch (action) {
1044 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001045 case CPU_ONLINE_FROZEN:
Heiko Carstensfae8b222007-10-22 12:52:39 +02001046 idle = &per_cpu(s390_idle, cpu);
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +02001047 memset(idle, 0, sizeof(struct s390_idle_data));
Akinobu Mitad882ba62010-05-26 14:43:34 -07001048 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001049 break;
1050 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001051 case CPU_DEAD_FROZEN:
Heiko Carstens08d07962008-01-26 14:10:56 +01001052 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001053 break;
1054 }
Akinobu Mitad882ba62010-05-26 14:43:34 -07001055 return notifier_from_errno(err);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001056}
1057
1058static struct notifier_block __cpuinitdata smp_cpu_nb = {
Heiko Carstens39ce0102007-04-27 16:02:00 +02001059 .notifier_call = smp_cpu_notify,
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001060};
1061
Heiko Carstens2bc89b52008-02-05 16:50:40 +01001062static int __devinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +01001063{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001064 struct cpu *c = &pcpu_devices[cpu].cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001065 struct device *s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +01001066 int rc;
1067
1068 c->hotpluggable = 1;
1069 rc = register_cpu(c, cpu);
1070 if (rc)
1071 goto out;
1072 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1073 if (rc)
1074 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +01001075 if (cpu_online(cpu)) {
1076 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1077 if (rc)
1078 goto out_online;
1079 }
1080 rc = topology_cpu_init(c);
1081 if (rc)
1082 goto out_topology;
1083 return 0;
1084
1085out_topology:
1086 if (cpu_online(cpu))
1087 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1088out_online:
Heiko Carstens08d07962008-01-26 14:10:56 +01001089 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1090out_cpu:
1091#ifdef CONFIG_HOTPLUG_CPU
1092 unregister_cpu(c);
1093#endif
1094out:
1095 return rc;
1096}
1097
1098#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001099
Heiko Carstens67060d92008-05-30 10:03:27 +02001100int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001101{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001102 struct sclp_cpu_info *info;
1103 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001104
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001105 info = smp_get_cpu_info();
1106 if (!info)
1107 return -ENOMEM;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001108 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001109 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001110 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001111 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001112 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001113 kfree(info);
1114 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001115 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001116 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001117}
1118
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001119static ssize_t __ref rescan_store(struct device *dev,
1120 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001121 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001122 size_t count)
1123{
1124 int rc;
1125
1126 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001127 return rc ? rc : count;
1128}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001129static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001130#endif /* CONFIG_HOTPLUG_CPU */
1131
Heiko Carstens83a24e32011-12-27 11:27:09 +01001132static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001133{
Heiko Carstens83a24e32011-12-27 11:27:09 +01001134 int cpu, rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001135
1136 register_cpu_notifier(&smp_cpu_nb);
Heiko Carstens08d07962008-01-26 14:10:56 +01001137#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001138 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001139 if (rc)
1140 return rc;
1141#endif
1142 for_each_present_cpu(cpu) {
1143 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001144 if (rc)
1145 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 return 0;
1148}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001149subsys_initcall(s390_smp_init);