blob: d8a0b115c7c1f61989b90c65e189531cd21b2238 [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>
Michael Holzheu46b05d22007-02-21 10:55:21 +010039#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010040#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/sigp.h>
42#include <asm/pgalloc.h>
43#include <asm/irq.h>
44#include <asm/s390_ext.h>
45#include <asm/cpcmd.h>
46#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010047#include <asm/timer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020048#include <asm/lowcore.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010049#include <asm/sclp.h>
Martin Schwidefsky76d4e002009-06-12 10:26:21 +020050#include <asm/cputime.h>
Martin Schwidefskyc742b312008-12-31 15:11:42 +010051#include <asm/vdso.h>
Heiko Carstens4bb5e072009-09-11 10:29:03 +020052#include <asm/cpu.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020053#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Heiko Carstensfb380aa2010-01-13 20:44:37 +010055/* logical cpu to cpu address */
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010056unsigned short __cpu_logical_map[NR_CPUS];
Heiko Carstensfb380aa2010-01-13 20:44:37 +010057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct task_struct *current_set[NR_CPUS];
59
Heiko Carstens08d07962008-01-26 14:10:56 +010060static u8 smp_cpu_type;
61static int smp_use_sigp_detection;
62
63enum s390_cpu_state {
64 CPU_STATE_STANDBY,
65 CPU_STATE_CONFIGURED,
66};
67
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020068DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstensc10fde02008-04-17 07:46:13 +020069int smp_cpu_polarization[NR_CPUS];
Heiko Carstens08d07962008-01-26 14:10:56 +010070static int smp_cpu_state[NR_CPUS];
Heiko Carstensc10fde02008-04-17 07:46:13 +020071static int cpu_management;
Heiko Carstens08d07962008-01-26 14:10:56 +010072
73static DEFINE_PER_CPU(struct cpu, cpu_devices);
Heiko Carstens08d07962008-01-26 14:10:56 +010074
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010075static void smp_ext_bitcall(int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010077static int raw_cpu_stopped(int cpu)
Heiko Carstens5c0b9122009-09-11 10:29:05 +020078{
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010079 u32 status;
Heiko Carstens5c0b9122009-09-11 10:29:05 +020080
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010081 switch (raw_sigp_ps(&status, 0, cpu, sigp_sense)) {
Heiko Carstens5c0b9122009-09-11 10:29:05 +020082 case sigp_status_stored:
83 /* Check for stopped and check stop state */
84 if (status & 0x50)
85 return 1;
86 break;
87 default:
88 break;
89 }
90 return 0;
91}
92
Heiko Carstensa93b8ec2010-02-26 22:37:35 +010093static inline int cpu_stopped(int cpu)
94{
95 return raw_cpu_stopped(cpu_logical_map(cpu));
96}
97
Heiko Carstens2c2df112010-02-26 22:37:34 +010098void smp_switch_to_ipl_cpu(void (*func)(void *), void *data)
99{
100 struct _lowcore *lc, *current_lc;
101 struct stack_frame *sf;
102 struct pt_regs *regs;
103 unsigned long sp;
104
105 if (smp_processor_id() == 0)
106 func(data);
107 __load_psw_mask(PSW_BASE_BITS | PSW_DEFAULT_KEY);
108 /* Disable lowcore protection */
109 __ctl_clear_bit(0, 28);
110 current_lc = lowcore_ptr[smp_processor_id()];
111 lc = lowcore_ptr[0];
112 if (!lc)
113 lc = current_lc;
114 lc->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
115 lc->restart_psw.addr = PSW_ADDR_AMODE | (unsigned long) smp_restart_cpu;
116 if (!cpu_online(0))
117 smp_switch_to_cpu(func, data, 0, stap(), __cpu_logical_map[0]);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100118 while (sigp(0, sigp_stop_and_store_status) == sigp_busy)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100119 cpu_relax();
120 sp = lc->panic_stack;
121 sp -= sizeof(struct pt_regs);
122 regs = (struct pt_regs *) sp;
123 memcpy(&regs->gprs, &current_lc->gpregs_save_area, sizeof(regs->gprs));
124 memcpy(&regs->psw, &current_lc->st_status_fixed_logout, sizeof(psw_t));
125 sp -= STACK_FRAME_OVERHEAD;
126 sf = (struct stack_frame *) sp;
127 sf->back_chain = regs->gprs[15];
128 smp_switch_to_cpu(func, data, sp, stap(), __cpu_logical_map[0]);
129}
130
Heiko Carstens677d7622007-11-20 11:13:37 +0100131void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200133 int cpu, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Heiko Carstens677d7622007-11-20 11:13:37 +0100135 /* Disable all interrupts/machine checks */
136 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100137 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100138
Heiko Carstens39ce0102007-04-27 16:02:00 +0200139 /* stop all processors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 for_each_online_cpu(cpu) {
141 if (cpu == smp_processor_id())
142 continue;
143 do {
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100144 rc = sigp(cpu, sigp_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 } while (rc == sigp_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200147 while (!cpu_stopped(cpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100148 cpu_relax();
149 }
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * This is the main routine where commands issued by other
154 * cpus are handled.
155 */
156
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100157static void do_ext_call_interrupt(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200159 unsigned long bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Heiko Carstens39ce0102007-04-27 16:02:00 +0200161 /*
162 * handle bit signal external calls
163 *
164 * For the ec_schedule signal we have to do nothing. All the work
165 * is done automatically when we return from the interrupt.
166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 bits = xchg(&S390_lowcore.ext_call_fast, 0);
168
Heiko Carstens39ce0102007-04-27 16:02:00 +0200169 if (test_bit(ec_call_function, &bits))
Heiko Carstensca9fc752008-12-25 13:38:39 +0100170 generic_smp_call_function_interrupt();
171
172 if (test_bit(ec_call_function_single, &bits))
173 generic_smp_call_function_single_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * Send an external call sigp to another cpu and return without waiting
178 * for its completion.
179 */
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100180static void smp_ext_bitcall(int cpu, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200182 /*
183 * Set signaling bit in lowcore of target cpu and kick it
184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100186 while (sigp(cpu, sigp_emergency_signal) == sigp_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 udelay(10);
188}
189
Rusty Russell630cd042009-09-24 09:34:45 -0600190void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100191{
192 int cpu;
193
Rusty Russell630cd042009-09-24 09:34:45 -0600194 for_each_cpu(cpu, mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100195 smp_ext_bitcall(cpu, ec_call_function);
196}
197
198void arch_send_call_function_single_ipi(int cpu)
199{
200 smp_ext_bitcall(cpu, ec_call_function_single);
201}
202
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800203#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * this function sends a 'purge tlb' signal to another CPU.
206 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200207static void smp_ptlb_callback(void *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200209 __tlb_flush_local();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212void smp_ptlb_all(void)
213{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200214 on_each_cpu(smp_ptlb_callback, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800217#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/*
220 * this function sends a 'reschedule' IPI to another CPU.
221 * it goes straight through and wastes no time serializing
222 * anything. Worst case is that we lose a reschedule ...
223 */
224void smp_send_reschedule(int cpu)
225{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200226 smp_ext_bitcall(cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229/*
230 * parameter area for the set/clear control bit callbacks
231 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200232struct ec_creg_mask_parms {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned long orvals[16];
234 unsigned long andvals[16];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200235};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237/*
238 * callback for setting/clearing control bits
239 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200240static void smp_ctl_bit_callback(void *info)
241{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200242 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long cregs[16];
244 int i;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200245
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200246 __ctl_store(cregs, 0, 15);
247 for (i = 0; i <= 15; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200249 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * Set a bit in a control register of all cpus
254 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200255void smp_ctl_set_bit(int cr, int bit)
256{
257 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200259 memset(&parms.orvals, 0, sizeof(parms.orvals));
260 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 parms.orvals[cr] = 1 << bit;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200262 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200264EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266/*
267 * Clear a bit in a control register of all cpus
268 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200269void smp_ctl_clear_bit(int cr, int bit)
270{
271 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200273 memset(&parms.orvals, 0, sizeof(parms.orvals));
274 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 parms.andvals[cr] = ~(1L << bit);
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200276 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200278EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Michael Holzheu59f2e692009-03-26 15:24:46 +0100280#ifdef CONFIG_ZFCPDUMP
Michael Holzheu411ed322007-04-27 16:01:49 +0200281
Heiko Carstens285f6722007-07-10 11:24:13 +0200282static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
Michael Holzheu411ed322007-04-27 16:01:49 +0200283{
Michael Holzheu411ed322007-04-27 16:01:49 +0200284 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
285 return;
Heiko Carstens285f6722007-07-10 11:24:13 +0200286 if (cpu >= NR_CPUS) {
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100287 pr_warning("CPU %i exceeds the maximum %i and is excluded from "
288 "the dump\n", cpu, NR_CPUS - 1);
Heiko Carstens285f6722007-07-10 11:24:13 +0200289 return;
Michael Holzheu411ed322007-04-27 16:01:49 +0200290 }
Heiko Carstensf64ca212010-02-26 22:37:32 +0100291 zfcpdump_save_areas[cpu] = kmalloc(sizeof(struct save_area), GFP_KERNEL);
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100292 while (raw_sigp(phy_cpu, sigp_stop_and_store_status) == sigp_busy)
Heiko Carstens285f6722007-07-10 11:24:13 +0200293 cpu_relax();
294 memcpy(zfcpdump_save_areas[cpu],
295 (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
Heiko Carstensf64ca212010-02-26 22:37:32 +0100296 sizeof(struct save_area));
Michael Holzheu411ed322007-04-27 16:01:49 +0200297}
298
Heiko Carstensf64ca212010-02-26 22:37:32 +0100299struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
Michael Holzheu411ed322007-04-27 16:01:49 +0200300EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
301
302#else
Heiko Carstens285f6722007-07-10 11:24:13 +0200303
304static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
305
Michael Holzheu59f2e692009-03-26 15:24:46 +0100306#endif /* CONFIG_ZFCPDUMP */
Michael Holzheu411ed322007-04-27 16:01:49 +0200307
Heiko Carstens08d07962008-01-26 14:10:56 +0100308static int cpu_known(int cpu_id)
309{
310 int cpu;
311
312 for_each_present_cpu(cpu) {
313 if (__cpu_logical_map[cpu] == cpu_id)
314 return 1;
315 }
316 return 0;
317}
318
319static int smp_rescan_cpus_sigp(cpumask_t avail)
320{
321 int cpu_id, logical_cpu;
322
Rusty Russell93632d12009-03-26 15:24:59 +0100323 logical_cpu = cpumask_first(&avail);
324 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100325 return 0;
Heiko Carstens4bb5e072009-09-11 10:29:03 +0200326 for (cpu_id = 0; cpu_id <= MAX_CPU_ADDRESS; cpu_id++) {
Heiko Carstens08d07962008-01-26 14:10:56 +0100327 if (cpu_known(cpu_id))
328 continue;
329 __cpu_logical_map[logical_cpu] = cpu_id;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200330 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
Heiko Carstens08d07962008-01-26 14:10:56 +0100331 if (!cpu_stopped(logical_cpu))
332 continue;
333 cpu_set(logical_cpu, cpu_present_map);
334 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
Rusty Russell93632d12009-03-26 15:24:59 +0100335 logical_cpu = cpumask_next(logical_cpu, &avail);
336 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100337 break;
338 }
339 return 0;
340}
341
Heiko Carstens48483b32008-01-26 14:11:05 +0100342static int smp_rescan_cpus_sclp(cpumask_t avail)
Heiko Carstens08d07962008-01-26 14:10:56 +0100343{
344 struct sclp_cpu_info *info;
345 int cpu_id, logical_cpu, cpu;
346 int rc;
347
Rusty Russell93632d12009-03-26 15:24:59 +0100348 logical_cpu = cpumask_first(&avail);
349 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100350 return 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100351 info = kmalloc(sizeof(*info), GFP_KERNEL);
Heiko Carstens08d07962008-01-26 14:10:56 +0100352 if (!info)
353 return -ENOMEM;
354 rc = sclp_get_cpu_info(info);
355 if (rc)
356 goto out;
357 for (cpu = 0; cpu < info->combined; cpu++) {
358 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
359 continue;
360 cpu_id = info->cpu[cpu].address;
361 if (cpu_known(cpu_id))
362 continue;
363 __cpu_logical_map[logical_cpu] = cpu_id;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200364 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
Heiko Carstens08d07962008-01-26 14:10:56 +0100365 cpu_set(logical_cpu, cpu_present_map);
366 if (cpu >= info->configured)
367 smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
368 else
369 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
Rusty Russell93632d12009-03-26 15:24:59 +0100370 logical_cpu = cpumask_next(logical_cpu, &avail);
371 if (logical_cpu >= nr_cpu_ids)
Heiko Carstens08d07962008-01-26 14:10:56 +0100372 break;
373 }
374out:
Heiko Carstens48483b32008-01-26 14:11:05 +0100375 kfree(info);
Heiko Carstens08d07962008-01-26 14:10:56 +0100376 return rc;
377}
378
Heiko Carstens1e489512008-04-30 13:38:37 +0200379static int __smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100380{
381 cpumask_t avail;
382
Heiko Carstens48483b32008-01-26 14:11:05 +0100383 cpus_xor(avail, cpu_possible_map, cpu_present_map);
Heiko Carstens08d07962008-01-26 14:10:56 +0100384 if (smp_use_sigp_detection)
385 return smp_rescan_cpus_sigp(avail);
386 else
387 return smp_rescan_cpus_sclp(avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Heiko Carstens48483b32008-01-26 14:11:05 +0100390static void __init smp_detect_cpus(void)
391{
392 unsigned int cpu, c_cpus, s_cpus;
393 struct sclp_cpu_info *info;
394 u16 boot_cpu_addr, cpu_addr;
395
396 c_cpus = 1;
397 s_cpus = 0;
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100398 boot_cpu_addr = __cpu_logical_map[0];
Heiko Carstens48483b32008-01-26 14:11:05 +0100399 info = kmalloc(sizeof(*info), GFP_KERNEL);
400 if (!info)
401 panic("smp_detect_cpus failed to allocate memory\n");
402 /* Use sigp detection algorithm if sclp doesn't work. */
403 if (sclp_get_cpu_info(info)) {
404 smp_use_sigp_detection = 1;
Heiko Carstens4bb5e072009-09-11 10:29:03 +0200405 for (cpu = 0; cpu <= MAX_CPU_ADDRESS; cpu++) {
Heiko Carstens48483b32008-01-26 14:11:05 +0100406 if (cpu == boot_cpu_addr)
407 continue;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100408 if (!raw_cpu_stopped(cpu))
Heiko Carstens48483b32008-01-26 14:11:05 +0100409 continue;
410 smp_get_save_area(c_cpus, cpu);
411 c_cpus++;
412 }
413 goto out;
414 }
415
416 if (info->has_cpu_type) {
417 for (cpu = 0; cpu < info->combined; cpu++) {
418 if (info->cpu[cpu].address == boot_cpu_addr) {
419 smp_cpu_type = info->cpu[cpu].type;
420 break;
421 }
422 }
423 }
424
425 for (cpu = 0; cpu < info->combined; cpu++) {
426 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
427 continue;
428 cpu_addr = info->cpu[cpu].address;
429 if (cpu_addr == boot_cpu_addr)
430 continue;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100431 if (!raw_cpu_stopped(cpu_addr)) {
Heiko Carstens48483b32008-01-26 14:11:05 +0100432 s_cpus++;
433 continue;
434 }
435 smp_get_save_area(c_cpus, cpu_addr);
436 c_cpus++;
437 }
438out:
439 kfree(info);
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100440 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100441 get_online_cpus();
Heiko Carstens1e489512008-04-30 13:38:37 +0200442 __smp_rescan_cpus();
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100443 put_online_cpus();
Heiko Carstens48483b32008-01-26 14:11:05 +0100444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200447 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200449int __cpuinit start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200451 /* Setup the cpu */
452 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800453 preempt_disable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100454 /* Enable TOD clock interrupts on the secondary cpu. */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200455 init_cpu_timer();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100456 /* Enable cpu timer interrupts on the secondary cpu. */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200457 init_cpu_vtimer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* Enable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100459 pfault_init();
460
Manfred Spraule545a612008-09-07 16:57:22 +0200461 /* call cpu notifiers */
462 notify_cpu_starting(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* Mark this cpu as online */
Heiko Carstensca9fc752008-12-25 13:38:39 +0100464 ipi_call_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 cpu_set(smp_processor_id(), cpu_online_map);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100466 ipi_call_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Switch on interrupts */
468 local_irq_enable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200469 /* Print info about this processor */
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100470 print_cpu_info();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200471 /* cpu_idle will call schedule for us */
472 cpu_idle();
473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static void __init smp_create_idle(unsigned int cpu)
477{
478 struct task_struct *p;
479
480 /*
481 * don't care about the psw and regs settings since we'll never
482 * reschedule the forked task.
483 */
484 p = fork_idle(cpu);
485 if (IS_ERR(p))
486 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
487 current_set[cpu] = p;
488}
489
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100490static int __cpuinit smp_alloc_lowcore(int cpu)
491{
492 unsigned long async_stack, panic_stack;
493 struct _lowcore *lowcore;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100494
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200495 lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100496 if (!lowcore)
497 return -ENOMEM;
498 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100499 panic_stack = __get_free_page(GFP_KERNEL);
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100500 if (!panic_stack || !async_stack)
501 goto out;
Heiko Carstens98c7b382008-03-05 12:37:09 +0100502 memcpy(lowcore, &S390_lowcore, 512);
503 memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100504 lowcore->async_stack = async_stack + ASYNC_SIZE;
505 lowcore->panic_stack = panic_stack + PAGE_SIZE;
506
507#ifndef CONFIG_64BIT
508 if (MACHINE_HAS_IEEE) {
509 unsigned long save_area;
510
511 save_area = get_zeroed_page(GFP_KERNEL);
512 if (!save_area)
Martin Schwidefsky33b1d092008-12-25 13:39:27 +0100513 goto out;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100514 lowcore->extended_save_area_addr = (u32) save_area;
515 }
Martin Schwidefskyc742b312008-12-31 15:11:42 +0100516#else
517 if (vdso_alloc_per_cpu(cpu, lowcore))
518 goto out;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100519#endif
520 lowcore_ptr[cpu] = lowcore;
521 return 0;
522
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100523out:
Martin Schwidefsky33b1d092008-12-25 13:39:27 +0100524 free_page(panic_stack);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100525 free_pages(async_stack, ASYNC_ORDER);
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200526 free_pages((unsigned long) lowcore, LC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100527 return -ENOMEM;
528}
529
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100530static void smp_free_lowcore(int cpu)
531{
532 struct _lowcore *lowcore;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100533
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100534 lowcore = lowcore_ptr[cpu];
535#ifndef CONFIG_64BIT
536 if (MACHINE_HAS_IEEE)
537 free_page((unsigned long) lowcore->extended_save_area_addr);
Martin Schwidefskyc742b312008-12-31 15:11:42 +0100538#else
539 vdso_free_per_cpu(cpu, lowcore);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100540#endif
541 free_page(lowcore->panic_stack - PAGE_SIZE);
542 free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200543 free_pages((unsigned long) lowcore, LC_ORDER);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100544 lowcore_ptr[cpu] = NULL;
545}
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/* Upping and downing of CPUs */
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100548int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200550 struct _lowcore *cpu_lowcore;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100551 struct task_struct *idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct stack_frame *sf;
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100553 u32 lowcore;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100554 int ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Heiko Carstens08d07962008-01-26 14:10:56 +0100556 if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
557 return -EIO;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100558 if (smp_alloc_lowcore(cpu))
559 return -ENOMEM;
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100560 do {
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100561 ccode = sigp(cpu, sigp_initial_cpu_reset);
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100562 if (ccode == sigp_busy)
563 udelay(10);
564 if (ccode == sigp_not_operational)
565 goto err_out;
566 } while (ccode == sigp_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100568 lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100569 while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100570 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 idle = current_set[cpu];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200573 cpu_lowcore = lowcore_ptr[cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 cpu_lowcore->kernel_stack = (unsigned long)
Heiko Carstens39ce0102007-04-27 16:02:00 +0200575 task_stack_page(idle) + THREAD_SIZE;
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100576 cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
578 - sizeof(struct pt_regs)
579 - sizeof(struct stack_frame));
580 memset(sf, 0, sizeof(struct stack_frame));
581 sf->gprs[9] = (unsigned long) sf;
582 cpu_lowcore->save_area[15] = (unsigned long) sf;
Segher Boessenkool24d3e212008-06-10 10:03:23 +0200583 __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200584 asm volatile(
585 " stam 0,15,0(%0)"
586 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200588 cpu_lowcore->current_task = (unsigned long) idle;
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100589 cpu_lowcore->cpu_nr = cpu;
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100590 cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
Christian Ehrhardt25097bf2009-04-14 15:36:16 +0200591 cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200592 cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 eieio();
Michael Ryan699ff132006-03-24 03:15:17 -0800594
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100595 while (sigp(cpu, sigp_restart) == sigp_busy)
Michael Ryan699ff132006-03-24 03:15:17 -0800596 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 while (!cpu_online(cpu))
599 cpu_relax();
600 return 0;
Heiko Carstensd0d3cdf2009-03-26 15:24:53 +0100601
602err_out:
603 smp_free_lowcore(cpu);
604 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Heiko Carstens37a33022006-02-17 13:52:47 -0800607static int __init setup_possible_cpus(char *s)
608{
Heiko Carstens48483b32008-01-26 14:11:05 +0100609 int pcpus, cpu;
610
611 pcpus = simple_strtoul(s, NULL, 0);
Heiko Carstens88e01282009-04-14 15:36:25 +0200612 init_cpu_possible(cpumask_of(0));
613 for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
Rusty Russelldef6cfb2009-03-26 15:25:00 +0100614 set_cpu_possible(cpu, true);
Heiko Carstens37a33022006-02-17 13:52:47 -0800615 return 0;
616}
617early_param("possible_cpus", setup_possible_cpus);
618
Heiko Carstens48483b32008-01-26 14:11:05 +0100619#ifdef CONFIG_HOTPLUG_CPU
620
Heiko Carstens39ce0102007-04-27 16:02:00 +0200621int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200623 struct ec_creg_mask_parms cr_parms;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700624 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700626 cpu_clear(cpu, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* Disable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100629 pfault_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200631 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
632 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200634 /* disable all external interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 cr_parms.orvals[0] = 0;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200636 cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
637 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* disable all I/O interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 cr_parms.orvals[6] = 0;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200640 cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
641 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* disable most machine checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 cr_parms.orvals[14] = 0;
Heiko Carstens39ce0102007-04-27 16:02:00 +0200644 cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
645 1 << 25 | 1 << 24);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 smp_ctl_bit_callback(&cr_parms);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650}
651
Heiko Carstens39ce0102007-04-27 16:02:00 +0200652void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 /* Wait until target cpu is down */
Heiko Carstens5c0b9122009-09-11 10:29:05 +0200655 while (!cpu_stopped(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 cpu_relax();
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100657 while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
Heiko Carstens4f8048e2009-10-29 15:04:09 +0100658 udelay(10);
Heiko Carstens1cb6bb42008-01-26 14:11:14 +0100659 smp_free_lowcore(cpu);
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100660 pr_info("Processor %d stopped\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Heiko Carstens39ce0102007-04-27 16:02:00 +0200663void cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 idle_task_exit();
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100666 while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
Heiko Carstensf8501ba2009-10-29 15:04:13 +0100667 cpu_relax();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200668 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Heiko Carstens255acee2006-02-17 13:52:46 -0800671#endif /* CONFIG_HOTPLUG_CPU */
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673void __init smp_prepare_cpus(unsigned int max_cpus)
674{
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100675#ifndef CONFIG_64BIT
676 unsigned long save_area = 0;
677#endif
678 unsigned long async_stack, panic_stack;
679 struct _lowcore *lowcore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Heiko Carstens48483b32008-01-26 14:11:05 +0100682 smp_detect_cpus();
683
Heiko Carstens39ce0102007-04-27 16:02:00 +0200684 /* request the 0x1201 emergency signal external interrupt */
685 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
686 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100687 print_cpu_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100689 /* Reallocate current lowcore, but keep its contents. */
Heiko Carstens3fd26a72009-09-22 22:58:49 +0200690 lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100691 panic_stack = __get_free_page(GFP_KERNEL);
692 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
Martin Schwidefskyc742b312008-12-31 15:11:42 +0100693 BUG_ON(!lowcore || !panic_stack || !async_stack);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800694#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700695 if (MACHINE_HAS_IEEE)
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100696 save_area = get_zeroed_page(GFP_KERNEL);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700697#endif
Heiko Carstens591bb4f2008-02-19 15:29:29 +0100698 local_irq_disable();
699 local_mcck_disable();
700 lowcore_ptr[smp_processor_id()] = lowcore;
701 *lowcore = S390_lowcore;
702 lowcore->panic_stack = panic_stack + PAGE_SIZE;
703 lowcore->async_stack = async_stack + ASYNC_SIZE;
704#ifndef CONFIG_64BIT
705 if (MACHINE_HAS_IEEE)
706 lowcore->extended_save_area_addr = (u32) save_area;
707#endif
708 set_prefix((u32)(unsigned long) lowcore);
709 local_mcck_enable();
710 local_irq_enable();
Heiko Carstens3a6ba462009-07-24 12:39:51 +0200711#ifdef CONFIG_64BIT
712 if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
713 BUG();
714#endif
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800715 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (cpu != smp_processor_id())
717 smp_create_idle(cpu);
718}
719
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200720void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 BUG_ON(smp_processor_id() != 0);
723
Heiko Carstens48483b32008-01-26 14:11:05 +0100724 current_thread_info()->cpu = 0;
725 cpu_set(0, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 cpu_set(0, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 S390_lowcore.percpu_offset = __per_cpu_offset[0];
728 current_set[0] = current;
Heiko Carstens08d07962008-01-26 14:10:56 +0100729 smp_cpu_state[0] = CPU_STATE_CONFIGURED;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200730 smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200733void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Heiko Carstens02beacc2010-01-13 20:44:34 +0100737void __init smp_setup_processor_id(void)
738{
739 S390_lowcore.cpu_nr = 0;
740 __cpu_logical_map[0] = stap();
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/*
744 * the frequency of the profiling timer can be changed
745 * by writing a multiplier value into /proc/profile.
746 *
747 * usually you want to run this on all CPUs ;)
748 */
749int setup_profiling_timer(unsigned int multiplier)
750{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Heiko Carstens08d07962008-01-26 14:10:56 +0100754#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200755static ssize_t cpu_configure_show(struct sys_device *dev,
756 struct sysdev_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100757{
758 ssize_t count;
759
760 mutex_lock(&smp_cpu_state_mutex);
761 count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
762 mutex_unlock(&smp_cpu_state_mutex);
763 return count;
764}
765
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200766static ssize_t cpu_configure_store(struct sys_device *dev,
767 struct sysdev_attribute *attr,
768 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100769{
770 int cpu = dev->id;
771 int val, rc;
772 char delim;
773
774 if (sscanf(buf, "%d %c", &val, &delim) != 1)
775 return -EINVAL;
776 if (val != 0 && val != 1)
777 return -EINVAL;
778
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100779 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200780 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100781 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100782 /* disallow configuration changes of online cpus and cpu 0 */
783 if (cpu_online(cpu) || cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100784 goto out;
785 rc = 0;
786 switch (val) {
787 case 0:
788 if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
789 rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200790 if (!rc) {
Heiko Carstens08d07962008-01-26 14:10:56 +0100791 smp_cpu_state[cpu] = CPU_STATE_STANDBY;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200792 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
793 }
Heiko Carstens08d07962008-01-26 14:10:56 +0100794 }
795 break;
796 case 1:
797 if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
798 rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200799 if (!rc) {
Heiko Carstens08d07962008-01-26 14:10:56 +0100800 smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200801 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
802 }
Heiko Carstens08d07962008-01-26 14:10:56 +0100803 }
804 break;
805 default:
806 break;
807 }
808out:
Heiko Carstens08d07962008-01-26 14:10:56 +0100809 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +0200810 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +0100811 return rc ? rc : count;
812}
813static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
814#endif /* CONFIG_HOTPLUG_CPU */
815
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200816static ssize_t cpu_polarization_show(struct sys_device *dev,
817 struct sysdev_attribute *attr, char *buf)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200818{
819 int cpu = dev->id;
820 ssize_t count;
821
822 mutex_lock(&smp_cpu_state_mutex);
823 switch (smp_cpu_polarization[cpu]) {
824 case POLARIZATION_HRZ:
825 count = sprintf(buf, "horizontal\n");
826 break;
827 case POLARIZATION_VL:
828 count = sprintf(buf, "vertical:low\n");
829 break;
830 case POLARIZATION_VM:
831 count = sprintf(buf, "vertical:medium\n");
832 break;
833 case POLARIZATION_VH:
834 count = sprintf(buf, "vertical:high\n");
835 break;
836 default:
837 count = sprintf(buf, "unknown\n");
838 break;
839 }
840 mutex_unlock(&smp_cpu_state_mutex);
841 return count;
842}
843static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
844
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200845static ssize_t show_cpu_address(struct sys_device *dev,
846 struct sysdev_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100847{
848 return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
849}
850static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
851
852
853static struct attribute *cpu_common_attrs[] = {
854#ifdef CONFIG_HOTPLUG_CPU
855 &attr_configure.attr,
856#endif
857 &attr_address.attr,
Heiko Carstensc10fde02008-04-17 07:46:13 +0200858 &attr_polarization.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +0100859 NULL,
860};
861
862static struct attribute_group cpu_common_attr_group = {
863 .attrs = cpu_common_attrs,
864};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200866static ssize_t show_capability(struct sys_device *dev,
867 struct sysdev_attribute *attr, char *buf)
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200868{
869 unsigned int capability;
870 int rc;
871
872 rc = get_cpu_capability(&capability);
873 if (rc)
874 return rc;
875 return sprintf(buf, "%u\n", capability);
876}
877static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
878
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200879static ssize_t show_idle_count(struct sys_device *dev,
880 struct sysdev_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200881{
882 struct s390_idle_data *idle;
883 unsigned long long idle_count;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200884 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200885
886 idle = &per_cpu(s390_idle, dev->id);
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200887repeat:
888 sequence = idle->sequence;
889 smp_rmb();
890 if (sequence & 1)
891 goto repeat;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200892 idle_count = idle->idle_count;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100893 if (idle->idle_enter)
894 idle_count++;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200895 smp_rmb();
896 if (idle->sequence != sequence)
897 goto repeat;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200898 return sprintf(buf, "%llu\n", idle_count);
899}
900static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
901
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200902static ssize_t show_idle_time(struct sys_device *dev,
903 struct sysdev_attribute *attr, char *buf)
Heiko Carstensfae8b222007-10-22 12:52:39 +0200904{
905 struct s390_idle_data *idle;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100906 unsigned long long now, idle_time, idle_enter;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200907 unsigned int sequence;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200908
909 idle = &per_cpu(s390_idle, dev->id);
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100910 now = get_clock();
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200911repeat:
912 sequence = idle->sequence;
913 smp_rmb();
914 if (sequence & 1)
915 goto repeat;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100916 idle_time = idle->idle_time;
917 idle_enter = idle->idle_enter;
918 if (idle_enter != 0ULL && idle_enter < now)
919 idle_time += now - idle_enter;
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200920 smp_rmb();
921 if (idle->sequence != sequence)
922 goto repeat;
Martin Schwidefsky6f430922008-12-31 15:11:40 +0100923 return sprintf(buf, "%llu\n", idle_time >> 12);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200924}
Heiko Carstens69d39d62007-11-05 11:10:13 +0100925static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
Heiko Carstensfae8b222007-10-22 12:52:39 +0200926
Heiko Carstens08d07962008-01-26 14:10:56 +0100927static struct attribute *cpu_online_attrs[] = {
Heiko Carstensfae8b222007-10-22 12:52:39 +0200928 &attr_capability.attr,
929 &attr_idle_count.attr,
Heiko Carstens69d39d62007-11-05 11:10:13 +0100930 &attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200931 NULL,
932};
933
Heiko Carstens08d07962008-01-26 14:10:56 +0100934static struct attribute_group cpu_online_attr_group = {
935 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +0200936};
937
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200938static int __cpuinit smp_cpu_notify(struct notifier_block *self,
939 unsigned long action, void *hcpu)
940{
941 unsigned int cpu = (unsigned int)(long)hcpu;
942 struct cpu *c = &per_cpu(cpu_devices, cpu);
943 struct sys_device *s = &c->sysdev;
Heiko Carstensfae8b222007-10-22 12:52:39 +0200944 struct s390_idle_data *idle;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200945
946 switch (action) {
947 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700948 case CPU_ONLINE_FROZEN:
Heiko Carstensfae8b222007-10-22 12:52:39 +0200949 idle = &per_cpu(s390_idle, cpu);
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200950 memset(idle, 0, sizeof(struct s390_idle_data));
Heiko Carstens08d07962008-01-26 14:10:56 +0100951 if (sysfs_create_group(&s->kobj, &cpu_online_attr_group))
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200952 return NOTIFY_BAD;
953 break;
954 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700955 case CPU_DEAD_FROZEN:
Heiko Carstens08d07962008-01-26 14:10:56 +0100956 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200957 break;
958 }
959 return NOTIFY_OK;
960}
961
962static struct notifier_block __cpuinitdata smp_cpu_nb = {
Heiko Carstens39ce0102007-04-27 16:02:00 +0200963 .notifier_call = smp_cpu_notify,
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +0200964};
965
Heiko Carstens2bc89b52008-02-05 16:50:40 +0100966static int __devinit smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100967{
968 struct cpu *c = &per_cpu(cpu_devices, cpu);
969 struct sys_device *s = &c->sysdev;
970 int rc;
971
972 c->hotpluggable = 1;
973 rc = register_cpu(c, cpu);
974 if (rc)
975 goto out;
976 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
977 if (rc)
978 goto out_cpu;
979 if (!cpu_online(cpu))
980 goto out;
981 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
982 if (!rc)
983 return 0;
984 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
985out_cpu:
986#ifdef CONFIG_HOTPLUG_CPU
987 unregister_cpu(c);
988#endif
989out:
990 return rc;
991}
992
993#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +0200994
Heiko Carstens67060d92008-05-30 10:03:27 +0200995int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100996{
997 cpumask_t newcpus;
998 int cpu;
999 int rc;
1000
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001001 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001002 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +01001003 newcpus = cpu_present_map;
Heiko Carstens1e489512008-04-30 13:38:37 +02001004 rc = __smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001005 if (rc)
1006 goto out;
1007 cpus_andnot(newcpus, cpu_present_map, newcpus);
1008 for_each_cpu_mask(cpu, newcpus) {
1009 rc = smp_add_present_cpu(cpu);
1010 if (rc)
1011 cpu_clear(cpu, cpu_present_map);
1012 }
1013 rc = 0;
1014out:
Heiko Carstens08d07962008-01-26 14:10:56 +01001015 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001016 put_online_cpus();
Heiko Carstensc10fde02008-04-17 07:46:13 +02001017 if (!cpus_empty(newcpus))
1018 topology_schedule_update();
Heiko Carstens1e489512008-04-30 13:38:37 +02001019 return rc;
1020}
1021
Heiko Carstensda5aae72008-10-28 11:10:16 +01001022static ssize_t __ref rescan_store(struct sysdev_class *class, const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001023 size_t count)
1024{
1025 int rc;
1026
1027 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001028 return rc ? rc : count;
1029}
Heiko Carstensda5aae72008-10-28 11:10:16 +01001030static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001031#endif /* CONFIG_HOTPLUG_CPU */
1032
Heiko Carstensda5aae72008-10-28 11:10:16 +01001033static ssize_t dispatching_show(struct sysdev_class *class, char *buf)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001034{
1035 ssize_t count;
1036
1037 mutex_lock(&smp_cpu_state_mutex);
1038 count = sprintf(buf, "%d\n", cpu_management);
1039 mutex_unlock(&smp_cpu_state_mutex);
1040 return count;
1041}
1042
Heiko Carstensda5aae72008-10-28 11:10:16 +01001043static ssize_t dispatching_store(struct sysdev_class *dev, const char *buf,
1044 size_t count)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001045{
1046 int val, rc;
1047 char delim;
1048
1049 if (sscanf(buf, "%d %c", &val, &delim) != 1)
1050 return -EINVAL;
1051 if (val != 0 && val != 1)
1052 return -EINVAL;
1053 rc = 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +02001054 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001055 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstensc10fde02008-04-17 07:46:13 +02001056 if (cpu_management == val)
1057 goto out;
1058 rc = topology_set_cpu_management(val);
1059 if (!rc)
1060 cpu_management = val;
1061out:
Heiko Carstensc10fde02008-04-17 07:46:13 +02001062 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001063 put_online_cpus();
Heiko Carstensc10fde02008-04-17 07:46:13 +02001064 return rc ? rc : count;
1065}
Heiko Carstensda5aae72008-10-28 11:10:16 +01001066static SYSDEV_CLASS_ATTR(dispatching, 0644, dispatching_show,
1067 dispatching_store);
Heiko Carstensc10fde02008-04-17 07:46:13 +02001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069static int __init topology_init(void)
1070{
1071 int cpu;
Heiko Carstensfae8b222007-10-22 12:52:39 +02001072 int rc;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001073
1074 register_cpu_notifier(&smp_cpu_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Heiko Carstens08d07962008-01-26 14:10:56 +01001076#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstensda5aae72008-10-28 11:10:16 +01001077 rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001078 if (rc)
1079 return rc;
1080#endif
Heiko Carstensda5aae72008-10-28 11:10:16 +01001081 rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_dispatching);
Heiko Carstensc10fde02008-04-17 07:46:13 +02001082 if (rc)
1083 return rc;
Heiko Carstens08d07962008-01-26 14:10:56 +01001084 for_each_present_cpu(cpu) {
1085 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001086 if (rc)
1087 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 return 0;
1090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091subsys_initcall(topology_init);