blob: e2121099f03b581bc8145e1986d268ea0fb7c8c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/smp.c
3 *
Hans-Joachim Picht155af2f2009-06-16 10:30:52 +02004 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
Heiko Carstens39ce0102007-04-27 16:02:00 +02006 * 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 *
13 * We work with logical cpu numbering everywhere we can. The only
14 * functions using the real cpu address (got from STAP) are the sigp
15 * functions. For all other functions we use the identity mapping.
16 * That means that cpu_number_map[i] == i for every cpu. cpu_number_map is
17 * used e.g. to find the idle task belonging to a logical cpu. Every array
18 * in the kernel is sorted by the logical cpu number and not by the physical
19 * one which is causing all the confusion with __cpu_logical_map and
20 * cpu_number_map in other architectures.
21 */
22
Martin Schwidefsky395d31d2008-12-25 13:39:50 +010023#define KMSG_COMPONENT "cpu"
24#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040029#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/spinlock.h>
31#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33#include <linux/cache.h>
34#include <linux/interrupt.h>
Christian Borntraeger3324e602009-03-26 15:23:56 +010035#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010037#include <linux/timex.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020038#include <linux/bootmem.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010039#include <asm/asm-offsets.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010040#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010041#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/sigp.h>
43#include <asm/pgalloc.h>
44#include <asm/irq.h>
45#include <asm/s390_ext.h>
46#include <asm/cpcmd.h>
47#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010048#include <asm/timer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020049#include <asm/lowcore.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010050#include <asm/sclp.h>
Martin Schwidefsky76d4e002009-06-12 10:26:21 +020051#include <asm/cputime.h>
Martin Schwidefskyc742b312008-12-31 15:11:42 +010052#include <asm/vdso.h>
Heiko Carstens4bb5e072009-09-11 10:29:03 +020053#include <asm/cpu.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020054#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Heiko Carstensfb380aa2010-01-13 20:44:37 +010056/* logical cpu to cpu address */
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010057unsigned short __cpu_logical_map[NR_CPUS];
Heiko Carstensfb380aa2010-01-13 20:44:37 +010058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static struct task_struct *current_set[NR_CPUS];
60
Heiko Carstens08d07962008-01-26 14:10:56 +010061static u8 smp_cpu_type;
62static int smp_use_sigp_detection;
63
64enum s390_cpu_state {
65 CPU_STATE_STANDBY,
66 CPU_STATE_CONFIGURED,
67};
68
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020069DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstensc10fde02008-04-17 07:46:13 +020070int smp_cpu_polarization[NR_CPUS];
Heiko Carstens08d07962008-01-26 14:10:56 +010071static int smp_cpu_state[NR_CPUS];
Heiko Carstensc10fde02008-04-17 07:46:13 +020072static int cpu_management;
Heiko Carstens08d07962008-01-26 14:10:56 +010073
74static DEFINE_PER_CPU(struct cpu, cpu_devices);
Heiko Carstens08d07962008-01-26 14:10:56 +010075
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010076static void smp_ext_bitcall(int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010078static int raw_cpu_stopped(int cpu)
Heiko Carstens5c0b9122009-09-11 10:29:05 +020079{
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010080 u32 status;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020081
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010082 switch (raw_sigp_ps(&status, 0, cpu, sigp_sense)) {
Heiko Carstens5c0b9122009-09-11 10:29:05 +020083 case sigp_status_stored:
84 /* Check for stopped and check stop state */
85 if (status & 0x50)
86 return 1;
87 break;
88 default:
89 break;
90 }
91 return 0;
92}
93
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010094static inline int cpu_stopped(int cpu)
95{
96 return raw_cpu_stopped(cpu_logical_map(cpu));
97}
98
Heiko Carstens2c2df112010-02-26 22:37:34 +010099void smp_switch_to_ipl_cpu(void (*func)(void *), void *data)
100{
101 struct _lowcore *lc, *current_lc;
102 struct stack_frame *sf;
103 struct pt_regs *regs;
104 unsigned long sp;
105
106 if (smp_processor_id() == 0)
107 func(data);
108 __load_psw_mask(PSW_BASE_BITS | PSW_DEFAULT_KEY);
109 /* Disable lowcore protection */
110 __ctl_clear_bit(0, 28);
111 current_lc = lowcore_ptr[smp_processor_id()];
112 lc = lowcore_ptr[0];
113 if (!lc)
114 lc = current_lc;
115 lc->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
116 lc->restart_psw.addr = PSW_ADDR_AMODE | (unsigned long) smp_restart_cpu;
117 if (!cpu_online(0))
118 smp_switch_to_cpu(func, data, 0, stap(), __cpu_logical_map[0]);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100119 while (sigp(0, sigp_stop_and_store_status) == sigp_busy)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100120 cpu_relax();
121 sp = lc->panic_stack;
122 sp -= sizeof(struct pt_regs);
123 regs = (struct pt_regs *) sp;
124 memcpy(&regs->gprs, &current_lc->gpregs_save_area, sizeof(regs->gprs));
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100125 regs->psw = lc->psw_save_area;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100126 sp -= STACK_FRAME_OVERHEAD;
127 sf = (struct stack_frame *) sp;
128 sf->back_chain = regs->gprs[15];
129 smp_switch_to_cpu(func, data, sp, stap(), __cpu_logical_map[0]);
130}
131
Heiko Carstens677d7622007-11-20 11:13:37 +0100132void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200134 int cpu, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Heiko Carstens677d7622007-11-20 11:13:37 +0100136 /* Disable all interrupts/machine checks */
137 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100138 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100139
Heiko Carstens39ce0102007-04-27 16:02:00 +0200140 /* stop all processors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 for_each_online_cpu(cpu) {
142 if (cpu == smp_processor_id())
143 continue;
144 do {
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100145 rc = sigp(cpu, sigp_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 } while (rc == sigp_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200148 while (!cpu_stopped(cpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100149 cpu_relax();
150 }
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * This is the main routine where commands issued by other
155 * cpus are handled.
156 */
157
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100158static void do_ext_call_interrupt(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200160 unsigned long bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Heiko Carstens39ce0102007-04-27 16:02:00 +0200162 /*
163 * handle bit signal external calls
164 *
165 * For the ec_schedule signal we have to do nothing. All the work
166 * is done automatically when we return from the interrupt.
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 bits = xchg(&S390_lowcore.ext_call_fast, 0);
169
Heiko Carstens39ce0102007-04-27 16:02:00 +0200170 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100171 generic_smp_call_function_interrupt();
172
173 if (test_bit(ec_call_function_single, &bits))
174 generic_smp_call_function_single_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/*
178 * Send an external call sigp to another cpu and return without waiting
179 * for its completion.
180 */
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100181static void smp_ext_bitcall(int cpu, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200183 /*
184 * Set signaling bit in lowcore of target cpu and kick it
185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100187 while (sigp(cpu, sigp_emergency_signal) == sigp_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 udelay(10);
189}
190
Rusty Russell630cd042009-09-24 09:34:45 -0600191void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100192{
193 int cpu;
194
Rusty Russell630cd042009-09-24 09:34:45 -0600195 for_each_cpu(cpu, mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100196 smp_ext_bitcall(cpu, ec_call_function);
197}
198
199void arch_send_call_function_single_ipi(int cpu)
200{
201 smp_ext_bitcall(cpu, ec_call_function_single);
202}
203
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800204#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * this function sends a 'purge tlb' signal to another CPU.
207 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200208static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200210 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213void smp_ptlb_all(void)
214{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200215 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800218#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/*
221 * this function sends a 'reschedule' IPI to another CPU.
222 * it goes straight through and wastes no time serializing
223 * anything. Worst case is that we lose a reschedule ...
224 */
225void smp_send_reschedule(int cpu)
226{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200227 smp_ext_bitcall(cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/*
231 * parameter area for the set/clear control bit callbacks
232 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200233struct ec_creg_mask_parms {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned long orvals[16];
235 unsigned long andvals[16];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200236};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/*
239 * callback for setting/clearing control bits
240 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200241static void smp_ctl_bit_callback(void *info)
242{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200243 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned long cregs[16];
245 int i;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200246
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200247 __ctl_store(cregs, 0, 15);
248 for (i = 0; i <= 15; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200250 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * Set a bit in a control register of all cpus
255 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200256void smp_ctl_set_bit(int cr, int bit)
257{
258 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200260 memset(&parms.orvals, 0, sizeof(parms.orvals));
261 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 parms.orvals[cr] = 1 << bit;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200263 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200265EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267/*
268 * Clear a bit in a control register of all cpus
269 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200270void smp_ctl_clear_bit(int cr, int bit)
271{
272 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200274 memset(&parms.orvals, 0, sizeof(parms.orvals));
275 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 parms.andvals[cr] = ~(1L << bit);
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200277 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200279EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Michael Holzheu59f2e692009-03-26 15:24:46 +0100281#ifdef CONFIG_ZFCPDUMP
Michael Holzheu411ed322007-04-27 16:01:49 +0200282
Heiko Carstens285f6722007-07-10 11:24:13 +0200283static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
Michael Holzheu411ed322007-04-27 16:01:49 +0200284{
Michael Holzheu411ed322007-04-27 16:01:49 +0200285 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
286 return;
Heiko Carstens285f6722007-07-10 11:24:13 +0200287 if (cpu >= NR_CPUS) {
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100288 pr_warning("CPU %i exceeds the maximum %i and is excluded from "
289 "the dump\n", cpu, NR_CPUS - 1);
Heiko Carstens285f6722007-07-10 11:24:13 +0200290 return;
Michael Holzheu411ed322007-04-27 16:01:49 +0200291 }
Heiko Carstensf64ca212010-02-26 22:37:32 +0100292 zfcpdump_save_areas[cpu] = kmalloc(sizeof(struct save_area), GFP_KERNEL);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100293 while (raw_sigp(phy_cpu, sigp_stop_and_store_status) == sigp_busy)
Heiko Carstens285f6722007-07-10 11:24:13 +0200294 cpu_relax();
295 memcpy(zfcpdump_save_areas[cpu],
296 (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
Heiko Carstensf64ca212010-02-26 22:37:32 +0100297 sizeof(struct save_area));
Michael Holzheu411ed322007-04-27 16:01:49 +0200298}
299
Heiko Carstensf64ca212010-02-26 22:37:32 +0100300struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200301EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
302
303#else
Heiko Carstens285f6722007-07-10 11:24:13 +0200304
305static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
306
Michael Holzheu59f2e692009-03-26 15:24:46 +0100307#endif /* CONFIG_ZFCPDUMP */
Michael Holzheu411ed322007-04-27 16:01:49 +0200308
Heiko Carstens08d07962008-01-26 14:10:56 +0100309static int cpu_known(int cpu_id)
310{
311 int cpu;
312
313 for_each_present_cpu(cpu) {
314 if (__cpu_logical_map[cpu] == cpu_id)
315 return 1;
316 }
317 return 0;
318}
319
320static int smp_rescan_cpus_sigp(cpumask_t avail)
321{
322 int cpu_id, logical_cpu;
323
Rusty Russell93632d12009-03-26 15:24:59 +0100324 logical_cpu = cpumask_first(&avail);
325 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100326 return 0;
Heiko Carstens4bb5e072009-09-11 10:29:03 +0200327 for (cpu_id = 0; cpu_id <= MAX_CPU_ADDRESS; cpu_id++) {
Heiko Carstens08d07962008-01-26 14:10:56 +0100328 if (cpu_known(cpu_id))
329 continue;
330 __cpu_logical_map[logical_cpu] = cpu_id;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200331 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
Heiko Carstens08d07962008-01-26 14:10:56 +0100332 if (!cpu_stopped(logical_cpu))
333 continue;
334 cpu_set(logical_cpu, cpu_present_map);
335 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
Rusty Russell93632d12009-03-26 15:24:59 +0100336 logical_cpu = cpumask_next(logical_cpu, &avail);
337 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100338 break;
339 }
340 return 0;
341}
342
Heiko Carstens48483b32008-01-26 14:11:05 +0100343static int smp_rescan_cpus_sclp(cpumask_t avail)
Heiko Carstens08d07962008-01-26 14:10:56 +0100344{
345 struct sclp_cpu_info *info;
346 int cpu_id, logical_cpu, cpu;
347 int rc;
348
Rusty Russell93632d12009-03-26 15:24:59 +0100349 logical_cpu = cpumask_first(&avail);
350 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100351 return 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100352 info = kmalloc(sizeof(*info), GFP_KERNEL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100353 if (!info)
354 return -ENOMEM;
355 rc = sclp_get_cpu_info(info);
356 if (rc)
357 goto out;
358 for (cpu = 0; cpu < info->combined; cpu++) {
359 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
360 continue;
361 cpu_id = info->cpu[cpu].address;
362 if (cpu_known(cpu_id))
363 continue;
364 __cpu_logical_map[logical_cpu] = cpu_id;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200365 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
Heiko Carstens08d07962008-01-26 14:10:56 +0100366 cpu_set(logical_cpu, cpu_present_map);
367 if (cpu >= info->configured)
368 smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
369 else
370 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
Rusty Russell93632d12009-03-26 15:24:59 +0100371 logical_cpu = cpumask_next(logical_cpu, &avail);
372 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100373 break;
374 }
375out:
Heiko Carstens48483b32008-01-26 14:11:05 +0100376 kfree(info);
Heiko Carstens08d07962008-01-26 14:10:56 +0100377 return rc;
378}
379
Heiko Carstens1e489512008-04-30 13:38:37 +0200380static int __smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100381{
382 cpumask_t avail;
383
Heiko Carstens48483b32008-01-26 14:11:05 +0100384 cpus_xor(avail, cpu_possible_map, cpu_present_map);
Heiko Carstens08d07962008-01-26 14:10:56 +0100385 if (smp_use_sigp_detection)
386 return smp_rescan_cpus_sigp(avail);
387 else
388 return smp_rescan_cpus_sclp(avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Heiko Carstens48483b32008-01-26 14:11:05 +0100391static void __init smp_detect_cpus(void)
392{
393 unsigned int cpu, c_cpus, s_cpus;
394 struct sclp_cpu_info *info;
395 u16 boot_cpu_addr, cpu_addr;
396
397 c_cpus = 1;
398 s_cpus = 0;
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100399 boot_cpu_addr = __cpu_logical_map[0];
Heiko Carstens48483b32008-01-26 14:11:05 +0100400 info = kmalloc(sizeof(*info), GFP_KERNEL);
401 if (!info)
402 panic("smp_detect_cpus failed to allocate memory\n");
403 /* Use sigp detection algorithm if sclp doesn't work. */
404 if (sclp_get_cpu_info(info)) {
405 smp_use_sigp_detection = 1;
Heiko Carstens4bb5e072009-09-11 10:29:03 +0200406 for (cpu = 0; cpu <= MAX_CPU_ADDRESS; cpu++) {
Heiko Carstens48483b32008-01-26 14:11:05 +0100407 if (cpu == boot_cpu_addr)
408 continue;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100409 if (!raw_cpu_stopped(cpu))
Heiko Carstens48483b32008-01-26 14:11:05 +0100410 continue;
411 smp_get_save_area(c_cpus, cpu);
412 c_cpus++;
413 }
414 goto out;
415 }
416
417 if (info->has_cpu_type) {
418 for (cpu = 0; cpu < info->combined; cpu++) {
419 if (info->cpu[cpu].address == boot_cpu_addr) {
420 smp_cpu_type = info->cpu[cpu].type;
421 break;
422 }
423 }
424 }
425
426 for (cpu = 0; cpu < info->combined; cpu++) {
427 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
428 continue;
429 cpu_addr = info->cpu[cpu].address;
430 if (cpu_addr == boot_cpu_addr)
431 continue;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100432 if (!raw_cpu_stopped(cpu_addr)) {
Heiko Carstens48483b32008-01-26 14:11:05 +0100433 s_cpus++;
434 continue;
435 }
436 smp_get_save_area(c_cpus, cpu_addr);
437 c_cpus++;
438 }
439out:
440 kfree(info);
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100441 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100442 get_online_cpus();
Heiko Carstens1e489512008-04-30 13:38:37 +0200443 __smp_rescan_cpus();
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100444 put_online_cpus();
Heiko Carstens48483b32008-01-26 14:11:05 +0100445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200448 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200450int __cpuinit start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200452 /* Setup the cpu */
453 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800454 preempt_disable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100455 /* Enable TOD clock interrupts on the secondary cpu. */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200456 init_cpu_timer();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100457 /* Enable cpu timer interrupts on the secondary cpu. */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200458 init_cpu_vtimer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* Enable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100460 pfault_init();
461
Manfred Spraule545a612008-09-07 16:57:22 +0200462 /* call cpu notifiers */
463 notify_cpu_starting(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Mark this cpu as online */
Heiko Carstensca9fc752008-12-25 13:38:39 +0100465 ipi_call_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 cpu_set(smp_processor_id(), cpu_online_map);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100467 ipi_call_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Switch on interrupts */
469 local_irq_enable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200470 /* Print info about this processor */
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100471 print_cpu_info();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200472 /* cpu_idle will call schedule for us */
473 cpu_idle();
474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static void __init smp_create_idle(unsigned int cpu)
478{
479 struct task_struct *p;
480
481 /*
482 * don't care about the psw and regs settings since we'll never
483 * reschedule the forked task.
484 */
485 p = fork_idle(cpu);
486 if (IS_ERR(p))
487 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
488 current_set[cpu] = p;
489}
490
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100491static int __cpuinit smp_alloc_lowcore(int cpu)
492{
493 unsigned long async_stack, panic_stack;
494 struct _lowcore *lowcore;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100495
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200496 lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100497 if (!lowcore)
498 return -ENOMEM;
499 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100500 panic_stack = __get_free_page(GFP_KERNEL);
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100501 if (!panic_stack || !async_stack)
502 goto out;
Heiko Carstens98c7b382008-03-05 12:37:09 +0100503 memcpy(lowcore, &S390_lowcore, 512);
504 memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100505 lowcore->async_stack = async_stack + ASYNC_SIZE;
506 lowcore->panic_stack = panic_stack + PAGE_SIZE;
507
508#ifndef CONFIG_64BIT
509 if (MACHINE_HAS_IEEE) {
510 unsigned long save_area;
511
512 save_area = get_zeroed_page(GFP_KERNEL);
513 if (!save_area)
Martin Schwidefsky33b1d092008-12-25 13:39:27 +0100514 goto out;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100515 lowcore->extended_save_area_addr = (u32) save_area;
516 }
Martin Schwidefskyc742b312008-12-31 15:11:42 +0100517#else
518 if (vdso_alloc_per_cpu(cpu, lowcore))
519 goto out;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100520#endif
521 lowcore_ptr[cpu] = lowcore;
522 return 0;
523
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100524out:
Martin Schwidefsky33b1d092008-12-25 13:39:27 +0100525 free_page(panic_stack);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100526 free_pages(async_stack, ASYNC_ORDER);
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200527 free_pages((unsigned long) lowcore, LC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100528 return -ENOMEM;
529}
530
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100531static void smp_free_lowcore(int cpu)
532{
533 struct _lowcore *lowcore;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100534
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100535 lowcore = lowcore_ptr[cpu];
536#ifndef CONFIG_64BIT
537 if (MACHINE_HAS_IEEE)
538 free_page((unsigned long) lowcore->extended_save_area_addr);
Martin Schwidefskyc742b312008-12-31 15:11:42 +0100539#else
540 vdso_free_per_cpu(cpu, lowcore);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100541#endif
542 free_page(lowcore->panic_stack - PAGE_SIZE);
543 free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200544 free_pages((unsigned long) lowcore, LC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100545 lowcore_ptr[cpu] = NULL;
546}
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/* Upping and downing of CPUs */
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100549int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200551 struct _lowcore *cpu_lowcore;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100552 struct task_struct *idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct stack_frame *sf;
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100554 u32 lowcore;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100555 int ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Heiko Carstens08d07962008-01-26 14:10:56 +0100557 if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
558 return -EIO;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100559 if (smp_alloc_lowcore(cpu))
560 return -ENOMEM;
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100561 do {
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100562 ccode = sigp(cpu, sigp_initial_cpu_reset);
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100563 if (ccode == sigp_busy)
564 udelay(10);
565 if (ccode == sigp_not_operational)
566 goto err_out;
567 } while (ccode == sigp_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100569 lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100570 while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100571 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 idle = current_set[cpu];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200574 cpu_lowcore = lowcore_ptr[cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 cpu_lowcore->kernel_stack = (unsigned long)
Heiko Carstens39ce0102007-04-27 16:02:00 +0200576 task_stack_page(idle) + THREAD_SIZE;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100577 cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
579 - sizeof(struct pt_regs)
580 - sizeof(struct stack_frame));
581 memset(sf, 0, sizeof(struct stack_frame));
582 sf->gprs[9] = (unsigned long) sf;
583 cpu_lowcore->save_area[15] = (unsigned long) sf;
Segher Boessenkool24d3e212008-06-10 10:03:23 +0200584 __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200585 asm volatile(
586 " stam 0,15,0(%0)"
587 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200589 cpu_lowcore->current_task = (unsigned long) idle;
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100590 cpu_lowcore->cpu_nr = cpu;
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100591 cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
Christian Ehrhardt25097bf2009-04-14 15:36:16 +0200592 cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200593 cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 eieio();
Michael Ryan699ff132006-03-24 03:15:17 -0800595
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100596 while (sigp(cpu, sigp_restart) == sigp_busy)
Michael Ryan699ff132006-03-24 03:15:17 -0800597 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 while (!cpu_online(cpu))
600 cpu_relax();
601 return 0;
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100602
603err_out:
604 smp_free_lowcore(cpu);
605 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Heiko Carstens37a33022006-02-17 13:52:47 -0800608static int __init setup_possible_cpus(char *s)
609{
Heiko Carstens48483b32008-01-26 14:11:05 +0100610 int pcpus, cpu;
611
612 pcpus = simple_strtoul(s, NULL, 0);
Heiko Carstens88e01282009-04-14 15:36:25 +0200613 init_cpu_possible(cpumask_of(0));
614 for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100615 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800616 return 0;
617}
618early_param("possible_cpus", setup_possible_cpus);
619
Heiko Carstens48483b32008-01-26 14:11:05 +0100620#ifdef CONFIG_HOTPLUG_CPU
621
Heiko Carstens39ce0102007-04-27 16:02:00 +0200622int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200624 struct ec_creg_mask_parms cr_parms;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700625 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700627 cpu_clear(cpu, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Disable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100630 pfault_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200632 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
633 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200635 /* disable all external interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 cr_parms.orvals[0] = 0;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200637 cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
638 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* disable all I/O interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 cr_parms.orvals[6] = 0;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200641 cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
642 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* disable most machine checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 cr_parms.orvals[14] = 0;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200645 cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
646 1 << 25 | 1 << 24);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 smp_ctl_bit_callback(&cr_parms);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651}
652
Heiko Carstens39ce0102007-04-27 16:02:00 +0200653void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 /* Wait until target cpu is down */
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200656 while (!cpu_stopped(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 cpu_relax();
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100658 while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
Heiko Carstens4f8048e2009-10-29 15:04:09 +0100659 udelay(10);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100660 smp_free_lowcore(cpu);
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100661 pr_info("Processor %d stopped\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Heiko Carstens39ce0102007-04-27 16:02:00 +0200664void cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 idle_task_exit();
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100667 while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
Heiko Carstensf8501ba2009-10-29 15:04:13 +0100668 cpu_relax();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200669 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Heiko Carstens255acee2006-02-17 13:52:46 -0800672#endif /* CONFIG_HOTPLUG_CPU */
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674void __init smp_prepare_cpus(unsigned int max_cpus)
675{
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100676#ifndef CONFIG_64BIT
677 unsigned long save_area = 0;
678#endif
679 unsigned long async_stack, panic_stack;
680 struct _lowcore *lowcore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Heiko Carstens48483b32008-01-26 14:11:05 +0100683 smp_detect_cpus();
684
Heiko Carstens39ce0102007-04-27 16:02:00 +0200685 /* request the 0x1201 emergency signal external interrupt */
686 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
687 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100688 print_cpu_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100690 /* Reallocate current lowcore, but keep its contents. */
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200691 lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100692 panic_stack = __get_free_page(GFP_KERNEL);
693 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
Martin Schwidefskyc742b312008-12-31 15:11:42 +0100694 BUG_ON(!lowcore || !panic_stack || !async_stack);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800695#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700696 if (MACHINE_HAS_IEEE)
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100697 save_area = get_zeroed_page(GFP_KERNEL);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700698#endif
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100699 local_irq_disable();
700 local_mcck_disable();
701 lowcore_ptr[smp_processor_id()] = lowcore;
702 *lowcore = S390_lowcore;
703 lowcore->panic_stack = panic_stack + PAGE_SIZE;
704 lowcore->async_stack = async_stack + ASYNC_SIZE;
705#ifndef CONFIG_64BIT
706 if (MACHINE_HAS_IEEE)
707 lowcore->extended_save_area_addr = (u32) save_area;
708#endif
709 set_prefix((u32)(unsigned long) lowcore);
710 local_mcck_enable();
711 local_irq_enable();
Heiko Carstens3a6ba462009-07-24 12:39:51 +0200712#ifdef CONFIG_64BIT
713 if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
714 BUG();
715#endif
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800716 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (cpu != smp_processor_id())
718 smp_create_idle(cpu);
719}
720
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200721void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 BUG_ON(smp_processor_id() != 0);
724
Heiko Carstens48483b32008-01-26 14:11:05 +0100725 current_thread_info()->cpu = 0;
726 cpu_set(0, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 cpu_set(0, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 S390_lowcore.percpu_offset = __per_cpu_offset[0];
729 current_set[0] = current;
Heiko Carstens08d07962008-01-26 14:10:56 +0100730 smp_cpu_state[0] = CPU_STATE_CONFIGURED;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200731 smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200734void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Heiko Carstens02beacc2010-01-13 20:44:34 +0100738void __init smp_setup_processor_id(void)
739{
740 S390_lowcore.cpu_nr = 0;
741 __cpu_logical_map[0] = stap();
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/*
745 * the frequency of the profiling timer can be changed
746 * by writing a multiplier value into /proc/profile.
747 *
748 * usually you want to run this on all CPUs ;)
749 */
750int setup_profiling_timer(unsigned int multiplier)
751{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Heiko Carstens08d07962008-01-26 14:10:56 +0100755#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200756static ssize_t cpu_configure_show(struct sys_device *dev,
757 struct sysdev_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100758{
759 ssize_t count;
760
761 mutex_lock(&smp_cpu_state_mutex);
762 count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
763 mutex_unlock(&smp_cpu_state_mutex);
764 return count;
765}
766
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200767static ssize_t cpu_configure_store(struct sys_device *dev,
768 struct sysdev_attribute *attr,
769 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100770{
771 int cpu = dev->id;
772 int val, rc;
773 char delim;
774
775 if (sscanf(buf, "%d %c", &val, &delim) != 1)
776 return -EINVAL;
777 if (val != 0 && val != 1)
778 return -EINVAL;
779
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100780 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200781 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100782 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100783 /* disallow configuration changes of online cpus and cpu 0 */
784 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100785 goto out;
786 rc = 0;
787 switch (val) {
788 case 0:
789 if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
790 rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200791 if (!rc) {
Heiko Carstens08d07962008-01-26 14:10:56 +0100792 smp_cpu_state[cpu] = CPU_STATE_STANDBY;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200793 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
794 }
Heiko Carstens08d07962008-01-26 14:10:56 +0100795 }
796 break;
797 case 1:
798 if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
799 rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200800 if (!rc) {
Heiko Carstens08d07962008-01-26 14:10:56 +0100801 smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200802 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
803 }
Heiko Carstens08d07962008-01-26 14:10:56 +0100804 }
805 break;
806 default:
807 break;
808 }
809out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100810 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200811 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100812 return rc ? rc : count;
813}
814static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
815#endif /* CONFIG_HOTPLUG_CPU */
816
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200817static ssize_t cpu_polarization_show(struct sys_device *dev,
818 struct sysdev_attribute *attr, char *buf)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200819{
820 int cpu = dev->id;
821 ssize_t count;
822
823 mutex_lock(&smp_cpu_state_mutex);
824 switch (smp_cpu_polarization[cpu]) {
825 case POLARIZATION_HRZ:
826 count = sprintf(buf, "horizontal\n");
827 break;
828 case POLARIZATION_VL:
829 count = sprintf(buf, "vertical:low\n");
830 break;
831 case POLARIZATION_VM:
832 count = sprintf(buf, "vertical:medium\n");
833 break;
834 case POLARIZATION_VH:
835 count = sprintf(buf, "vertical:high\n");
836 break;
837 default:
838 count = sprintf(buf, "unknown\n");
839 break;
840 }
841 mutex_unlock(&smp_cpu_state_mutex);
842 return count;
843}
844static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
845
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200846static ssize_t show_cpu_address(struct sys_device *dev,
847 struct sysdev_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100848{
849 return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
850}
851static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
852
853
854static struct attribute *cpu_common_attrs[] = {
855#ifdef CONFIG_HOTPLUG_CPU
856 &attr_configure.attr,
857#endif
858 &attr_address.attr,
Heiko Carstensc10fde02008-04-17 07:46:13 +0200859 &attr_polarization.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100860 NULL,
861};
862
863static struct attribute_group cpu_common_attr_group = {
864 .attrs = cpu_common_attrs,
865};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200867static ssize_t show_capability(struct sys_device *dev,
868 struct sysdev_attribute *attr, char *buf)
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200869{
870 unsigned int capability;
871 int rc;
872
873 rc = get_cpu_capability(&capability);
874 if (rc)
875 return rc;
876 return sprintf(buf, "%u\n", capability);
877}
878static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
879
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200880static ssize_t show_idle_count(struct sys_device *dev,
881 struct sysdev_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200882{
883 struct s390_idle_data *idle;
884 unsigned long long idle_count;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200885 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200886
887 idle = &per_cpu(s390_idle, dev->id);
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200888repeat:
889 sequence = idle->sequence;
890 smp_rmb();
891 if (sequence & 1)
892 goto repeat;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200893 idle_count = idle->idle_count;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100894 if (idle->idle_enter)
895 idle_count++;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200896 smp_rmb();
897 if (idle->sequence != sequence)
898 goto repeat;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200899 return sprintf(buf, "%llu\n", idle_count);
900}
901static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
902
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200903static ssize_t show_idle_time(struct sys_device *dev,
904 struct sysdev_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200905{
906 struct s390_idle_data *idle;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100907 unsigned long long now, idle_time, idle_enter;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200908 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200909
910 idle = &per_cpu(s390_idle, dev->id);
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100911 now = get_clock();
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200912repeat:
913 sequence = idle->sequence;
914 smp_rmb();
915 if (sequence & 1)
916 goto repeat;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100917 idle_time = idle->idle_time;
918 idle_enter = idle->idle_enter;
919 if (idle_enter != 0ULL && idle_enter < now)
920 idle_time += now - idle_enter;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200921 smp_rmb();
922 if (idle->sequence != sequence)
923 goto repeat;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100924 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200925}
Heiko Carstens69d39d62007-11-05 11:10:13 +0100926static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200927
Heiko Carstens08d07962008-01-26 14:10:56 +0100928static struct attribute *cpu_online_attrs[] = {
Heiko Carstensfae8b222007-10-22 12:52:39 +0200929 &attr_capability.attr,
930 &attr_idle_count.attr,
Heiko Carstens69d39d62007-11-05 11:10:13 +0100931 &attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200932 NULL,
933};
934
Heiko Carstens08d07962008-01-26 14:10:56 +0100935static struct attribute_group cpu_online_attr_group = {
936 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200937};
938
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200939static int __cpuinit smp_cpu_notify(struct notifier_block *self,
940 unsigned long action, void *hcpu)
941{
942 unsigned int cpu = (unsigned int)(long)hcpu;
943 struct cpu *c = &per_cpu(cpu_devices, cpu);
944 struct sys_device *s = &c->sysdev;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200945 struct s390_idle_data *idle;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200946
947 switch (action) {
948 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700949 case CPU_ONLINE_FROZEN:
Heiko Carstensfae8b222007-10-22 12:52:39 +0200950 idle = &per_cpu(s390_idle, cpu);
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200951 memset(idle, 0, sizeof(struct s390_idle_data));
Heiko Carstens08d07962008-01-26 14:10:56 +0100952 if (sysfs_create_group(&s->kobj, &cpu_online_attr_group))
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200953 return NOTIFY_BAD;
954 break;
955 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700956 case CPU_DEAD_FROZEN:
Heiko Carstens08d07962008-01-26 14:10:56 +0100957 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200958 break;
959 }
960 return NOTIFY_OK;
961}
962
963static struct notifier_block __cpuinitdata smp_cpu_nb = {
Heiko Carstens39ce0102007-04-27 16:02:00 +0200964 .notifier_call = smp_cpu_notify,
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200965};
966
Heiko Carstens2bc89b52008-02-05 16:50:40 +0100967static int __devinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100968{
969 struct cpu *c = &per_cpu(cpu_devices, cpu);
970 struct sys_device *s = &c->sysdev;
971 int rc;
972
973 c->hotpluggable = 1;
974 rc = register_cpu(c, cpu);
975 if (rc)
976 goto out;
977 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
978 if (rc)
979 goto out_cpu;
980 if (!cpu_online(cpu))
981 goto out;
982 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
983 if (!rc)
984 return 0;
985 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
986out_cpu:
987#ifdef CONFIG_HOTPLUG_CPU
988 unregister_cpu(c);
989#endif
990out:
991 return rc;
992}
993
994#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +0200995
Heiko Carstens67060d92008-05-30 10:03:27 +0200996int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100997{
998 cpumask_t newcpus;
999 int cpu;
1000 int rc;
1001
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001002 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001003 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +01001004 newcpus = cpu_present_map;
Heiko Carstens1e489512008-04-30 13:38:37 +02001005 rc = __smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001006 if (rc)
1007 goto out;
1008 cpus_andnot(newcpus, cpu_present_map, newcpus);
1009 for_each_cpu_mask(cpu, newcpus) {
1010 rc = smp_add_present_cpu(cpu);
1011 if (rc)
1012 cpu_clear(cpu, cpu_present_map);
1013 }
1014 rc = 0;
1015out:
Heiko Carstens08d07962008-01-26 14:10:56 +01001016 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001017 put_online_cpus();
Heiko Carstensc10fde02008-04-17 07:46:13 +02001018 if (!cpus_empty(newcpus))
1019 topology_schedule_update();
Heiko Carstens1e489512008-04-30 13:38:37 +02001020 return rc;
1021}
1022
Andi Kleenc9be0a32010-01-05 12:47:58 +01001023static ssize_t __ref rescan_store(struct sysdev_class *class,
1024 struct sysdev_class_attribute *attr,
1025 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001026 size_t count)
1027{
1028 int rc;
1029
1030 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001031 return rc ? rc : count;
1032}
Heiko Carstensda5aae72008-10-28 11:10:16 +01001033static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001034#endif /* CONFIG_HOTPLUG_CPU */
1035
Heiko Carstensda5aae72008-10-28 11:10:16 +01001036static ssize_t dispatching_show(struct sysdev_class *class, char *buf)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001037{
1038 ssize_t count;
1039
1040 mutex_lock(&smp_cpu_state_mutex);
1041 count = sprintf(buf, "%d\n", cpu_management);
1042 mutex_unlock(&smp_cpu_state_mutex);
1043 return count;
1044}
1045
Andi Kleenc9be0a32010-01-05 12:47:58 +01001046static ssize_t dispatching_store(struct sysdev_class *dev,
1047 struct sysdev_class_attribute *attr,
1048 const char *buf,
Heiko Carstensda5aae72008-10-28 11:10:16 +01001049 size_t count)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001050{
1051 int val, rc;
1052 char delim;
1053
1054 if (sscanf(buf, "%d %c", &val, &delim) != 1)
1055 return -EINVAL;
1056 if (val != 0 && val != 1)
1057 return -EINVAL;
1058 rc = 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +02001059 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001060 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstensc10fde02008-04-17 07:46:13 +02001061 if (cpu_management == val)
1062 goto out;
1063 rc = topology_set_cpu_management(val);
1064 if (!rc)
1065 cpu_management = val;
1066out:
Heiko Carstensc10fde02008-04-17 07:46:13 +02001067 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001068 put_online_cpus();
Heiko Carstensc10fde02008-04-17 07:46:13 +02001069 return rc ? rc : count;
1070}
Heiko Carstensda5aae72008-10-28 11:10:16 +01001071static SYSDEV_CLASS_ATTR(dispatching, 0644, dispatching_show,
1072 dispatching_store);
Heiko Carstensc10fde02008-04-17 07:46:13 +02001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static int __init topology_init(void)
1075{
1076 int cpu;
Heiko Carstensfae8b222007-10-22 12:52:39 +02001077 int rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001078
1079 register_cpu_notifier(&smp_cpu_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Heiko Carstens08d07962008-01-26 14:10:56 +01001081#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstensda5aae72008-10-28 11:10:16 +01001082 rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001083 if (rc)
1084 return rc;
1085#endif
Heiko Carstensda5aae72008-10-28 11:10:16 +01001086 rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_dispatching);
Heiko Carstensc10fde02008-04-17 07:46:13 +02001087 if (rc)
1088 return rc;
Heiko Carstens08d07962008-01-26 14:10:56 +01001089 for_each_present_cpu(cpu) {
1090 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001091 if (rc)
1092 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 return 0;
1095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096subsys_initcall(topology_init);