blob: 47a973b5b4f184adfa3855828d042bd73d33e61c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04002 * SMP related functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 1999, 2012
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04005 * Author(s): Denis Joseph Barrow,
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Heiko Carstens39ce0102007-04-27 16:02:00 +02009 * based on other smp stuff by
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
11 * (c) 1998 Ingo Molnar
12 *
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040013 * The code outside of smp.c uses logical cpu numbers, only smp.c does
14 * the translation of logical to physical cpu ids. All new code that
15 * operates on physical cpu numbers needs to go into smp.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Martin Schwidefsky395d31d2008-12-25 13:39:50 +010018#define KMSG_COMPONENT "cpu"
19#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
Heiko Carstensf2308862011-01-05 12:48:08 +010021#include <linux/workqueue.h>
Heiko Carstensaf51160e2016-12-03 09:48:01 +010022#include <linux/bootmem.h>
Paul Gortmaker3994a522017-02-09 15:20:23 -050023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040026#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/spinlock.h>
28#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/interrupt.h>
Christian Borntraeger3324e602009-03-26 15:23:56 +010031#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +010034#include <linux/sched/hotplug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010035#include <linux/sched/task_stack.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010036#include <linux/crash_dump.h>
Michael Holzheu1592a8e2015-05-26 19:05:23 +020037#include <linux/memblock.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010038#include <asm/asm-offsets.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020039#include <asm/diag.h>
Heiko Carstens1e3cab22012-03-30 09:40:55 +020040#include <asm/switch_to.h>
41#include <asm/facility.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010042#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010043#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/tlbflush.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020046#include <asm/vtimer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020047#include <asm/lowcore.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010048#include <asm/sclp.h>
Martin Schwidefskyc742b312008-12-31 15:11:42 +010049#include <asm/vdso.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040050#include <asm/debug.h>
Michael Holzheu4857d4b2012-03-11 11:59:34 -040051#include <asm/os_info.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020052#include <asm/sigp.h>
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020053#include <asm/idle.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020054#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040056enum {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040057 ec_schedule = 0,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040058 ec_call_function_single,
59 ec_stop_cpu,
60};
Heiko Carstens08d07962008-01-26 14:10:56 +010061
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040062enum {
Heiko Carstens08d07962008-01-26 14:10:56 +010063 CPU_STATE_STANDBY,
64 CPU_STATE_CONFIGURED,
65};
66
Heiko Carstens2f859d02015-02-11 12:31:03 +010067static DEFINE_PER_CPU(struct cpu *, cpu_device);
68
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040069struct pcpu {
Heiko Carstensc667aea2015-12-31 10:29:00 +010070 struct lowcore *lowcore; /* lowcore page(s) for the cpu */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040071 unsigned long ec_mask; /* bit mask for ec_xxx functions */
Heiko Carstens3dbc78d2015-12-08 14:10:12 +010072 unsigned long ec_clk; /* sigp timestamp for ec_xxx */
Heiko Carstens2f859d02015-02-11 12:31:03 +010073 signed char state; /* physical cpu state */
74 signed char polarization; /* physical polarization */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040075 u16 address; /* physical cpu address */
76};
77
Martin Schwidefskyd08d9432015-06-18 14:23:00 +020078static u8 boot_core_type;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040079static struct pcpu pcpu_devices[NR_CPUS];
80
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010081unsigned int smp_cpu_mt_shift;
82EXPORT_SYMBOL(smp_cpu_mt_shift);
83
84unsigned int smp_cpu_mtid;
85EXPORT_SYMBOL(smp_cpu_mtid);
86
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010087#ifdef CONFIG_CRASH_DUMP
88__vector128 __initdata boot_cpu_vector_save_area[__NUM_VXRS];
89#endif
90
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010091static unsigned int smp_max_threads __initdata = -1U;
92
93static int __init early_nosmt(char *s)
94{
95 smp_max_threads = 1;
96 return 0;
97}
98early_param("nosmt", early_nosmt);
99
100static int __init early_smt(char *s)
101{
102 get_option(&s, &smp_max_threads);
103 return 0;
104}
105early_param("smt", early_smt);
106
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200107/*
108 * The smp_cpu_state_mutex must be held when changing the state or polarization
109 * member of a pcpu data structure within the pcpu_devices arreay.
110 */
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200111DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100112
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400113/*
114 * Signal processor helper functions.
115 */
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100116static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100117{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400118 int cc;
119
120 while (1) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200121 cc = __pcpu_sigp(addr, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200122 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400123 return cc;
124 cpu_relax();
125 }
126}
127
128static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
129{
130 int cc, retry;
131
132 for (retry = 0; ; retry++) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200133 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200134 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400135 break;
136 if (retry >= 3)
137 udelay(10);
138 }
139 return cc;
140}
141
142static inline int pcpu_stopped(struct pcpu *pcpu)
143{
Heiko Carstens41459d36cf2012-09-14 11:09:52 +0200144 u32 uninitialized_var(status);
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200145
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200146 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200147 0, &status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400148 return 0;
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200149 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400150}
151
152static inline int pcpu_running(struct pcpu *pcpu)
153{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200154 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200155 0, NULL) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400156 return 1;
Heiko Carstens524b24a2012-06-04 12:11:41 +0200157 /* Status stored condition code is equivalent to cpu not running. */
158 return 0;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100159}
160
Michael Holzheu1943f532011-10-30 15:16:38 +0100161/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400162 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100163 */
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100164static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100165{
166 int cpu;
167
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400168 for_each_cpu(cpu, mask)
169 if (pcpu_devices[cpu].address == address)
170 return pcpu_devices + cpu;
171 return NULL;
172}
173
174static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
175{
176 int order;
177
Heiko Carstensdea24192013-12-03 10:06:29 +0100178 if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
179 return;
180 order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
Heiko Carstens3dbc78d2015-12-08 14:10:12 +0100181 pcpu->ec_clk = get_tod_clock_fast();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400182 pcpu_sigp_retry(pcpu, order, 0);
183}
184
Heiko Carstens2f859d02015-02-11 12:31:03 +0100185#define ASYNC_FRAME_OFFSET (ASYNC_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
186#define PANIC_FRAME_OFFSET (PAGE_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
187
Paul Gortmakere2741f12013-06-18 17:04:52 -0400188static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400189{
Heiko Carstens2f859d02015-02-11 12:31:03 +0100190 unsigned long async_stack, panic_stack;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100191 struct lowcore *lc;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400192
193 if (pcpu != &pcpu_devices[0]) {
Heiko Carstensc667aea2015-12-31 10:29:00 +0100194 pcpu->lowcore = (struct lowcore *)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400195 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100196 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
197 panic_stack = __get_free_page(GFP_KERNEL);
198 if (!pcpu->lowcore || !panic_stack || !async_stack)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400199 goto out;
Heiko Carstens2f859d02015-02-11 12:31:03 +0100200 } else {
201 async_stack = pcpu->lowcore->async_stack - ASYNC_FRAME_OFFSET;
202 panic_stack = pcpu->lowcore->panic_stack - PANIC_FRAME_OFFSET;
Michael Holzheu1943f532011-10-30 15:16:38 +0100203 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400204 lc = pcpu->lowcore;
205 memcpy(lc, &S390_lowcore, 512);
206 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100207 lc->async_stack = async_stack + ASYNC_FRAME_OFFSET;
208 lc->panic_stack = panic_stack + PANIC_FRAME_OFFSET;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400209 lc->cpu_nr = cpu;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200210 lc->spinlock_lockval = arch_spin_lockval(cpu);
Martin Schwidefsky80703612014-10-06 17:53:53 +0200211 if (MACHINE_HAS_VX)
212 lc->vector_save_area_addr =
213 (unsigned long) &lc->vector_save_area;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400214 if (vdso_alloc_per_cpu(lc))
215 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400216 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200217 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400218 return 0;
219out:
220 if (pcpu != &pcpu_devices[0]) {
Heiko Carstens2f859d02015-02-11 12:31:03 +0100221 free_page(panic_stack);
222 free_pages(async_stack, ASYNC_ORDER);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400223 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
224 }
225 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100226}
227
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200228#ifdef CONFIG_HOTPLUG_CPU
229
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400230static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100231{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200232 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400233 lowcore_ptr[pcpu - pcpu_devices] = NULL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400234 vdso_free_per_cpu(pcpu->lowcore);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100235 if (pcpu == &pcpu_devices[0])
236 return;
237 free_page(pcpu->lowcore->panic_stack-PANIC_FRAME_OFFSET);
238 free_pages(pcpu->lowcore->async_stack-ASYNC_FRAME_OFFSET, ASYNC_ORDER);
239 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100240}
241
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200242#endif /* CONFIG_HOTPLUG_CPU */
243
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400244static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100245{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100246 struct lowcore *lc = pcpu->lowcore;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400247
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200248 cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200249 cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400250 lc->cpu_nr = cpu;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200251 lc->spinlock_lockval = arch_spin_lockval(cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400252 lc->percpu_offset = __per_cpu_offset[cpu];
253 lc->kernel_asce = S390_lowcore.kernel_asce;
254 lc->machine_flags = S390_lowcore.machine_flags;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400255 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
256 __ctl_store(lc->cregs_save_area, 0, 15);
257 save_access_regs((unsigned int *) lc->access_regs_save_area);
258 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
259 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100260}
261
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400262static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
263{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100264 struct lowcore *lc = pcpu->lowcore;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400265
Martin Schwidefskydc7ee002013-04-24 10:20:43 +0200266 lc->kernel_stack = (unsigned long) task_stack_page(tsk)
267 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400268 lc->current_task = (unsigned long) tsk;
Christian Borntraegere22cf8c2015-10-06 18:06:15 +0200269 lc->lpp = LPP_MAGIC;
270 lc->current_pid = tsk->pid;
Martin Schwidefsky90c53e62016-11-08 12:15:59 +0100271 lc->user_timer = tsk->thread.user_timer;
272 lc->system_timer = tsk->thread.system_timer;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400273 lc->steal_timer = 0;
274}
275
276static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
277{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100278 struct lowcore *lc = pcpu->lowcore;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400279
280 lc->restart_stack = lc->kernel_stack;
281 lc->restart_fn = (unsigned long) func;
282 lc->restart_data = (unsigned long) data;
283 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200284 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400285}
286
287/*
288 * Call function via PSW restart on pcpu and stop the current cpu.
289 */
290static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
291 void *data, unsigned long stack)
292{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100293 struct lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
Heiko Carstensfbe76562012-06-05 09:59:52 +0200294 unsigned long source_cpu = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400295
Martin Schwidefskye258d712013-09-24 09:14:56 +0200296 __load_psw_mask(PSW_KERNEL_BITS);
Heiko Carstensfbe76562012-06-05 09:59:52 +0200297 if (pcpu->address == source_cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400298 func(data); /* should not return */
299 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200300 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400301 /* Restart func on the target cpu and stop the current cpu. */
Heiko Carstensfbe76562012-06-05 09:59:52 +0200302 mem_assign_absolute(lc->restart_stack, stack);
303 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
304 mem_assign_absolute(lc->restart_data, (unsigned long) data);
305 mem_assign_absolute(lc->restart_source, source_cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400306 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200307 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400308 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200309 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400310 " brc 2,1b # busy, try again\n"
Heiko Carstensfbe76562012-06-05 09:59:52 +0200311 : : "d" (pcpu->address), "d" (source_cpu),
Heiko Carstenseb546192012-06-04 15:05:43 +0200312 "K" (SIGP_RESTART), "K" (SIGP_STOP)
313 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400314 for (;;) ;
315}
316
317/*
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100318 * Enable additional logical cpus for multi-threading.
319 */
320static int pcpu_set_smt(unsigned int mtid)
321{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100322 int cc;
323
324 if (smp_cpu_mtid == mtid)
325 return 0;
Heiko Carstens80a60f62016-06-20 14:04:17 +0200326 cc = __pcpu_sigp(0, SIGP_SET_MULTI_THREADING, mtid, NULL);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100327 if (cc == 0) {
328 smp_cpu_mtid = mtid;
329 smp_cpu_mt_shift = 0;
330 while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
331 smp_cpu_mt_shift++;
332 pcpu_devices[0].address = stap();
333 }
334 return cc;
335}
336
337/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400338 * Call function on an online CPU.
339 */
340void smp_call_online_cpu(void (*func)(void *), void *data)
341{
342 struct pcpu *pcpu;
343
344 /* Use the current cpu if it is online. */
345 pcpu = pcpu_find_address(cpu_online_mask, stap());
346 if (!pcpu)
347 /* Use the first online cpu. */
348 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
349 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
350}
351
352/*
353 * Call function on the ipl CPU.
354 */
355void smp_call_ipl_cpu(void (*func)(void *), void *data)
356{
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400357 pcpu_delegate(&pcpu_devices[0], func, data,
Heiko Carstens2f859d02015-02-11 12:31:03 +0100358 pcpu_devices->lowcore->panic_stack -
359 PANIC_FRAME_OFFSET + PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400360}
361
362int smp_find_processor_id(u16 address)
363{
364 int cpu;
365
366 for_each_present_cpu(cpu)
367 if (pcpu_devices[cpu].address == address)
368 return cpu;
369 return -1;
370}
371
Christian Borntraeger760928c2016-11-02 05:08:32 -0400372bool arch_vcpu_is_preempted(int cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400373{
Christian Borntraeger760928c2016-11-02 05:08:32 -0400374 if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu))
375 return false;
376 if (pcpu_running(pcpu_devices + cpu))
377 return false;
378 return true;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400379}
Christian Borntraeger760928c2016-11-02 05:08:32 -0400380EXPORT_SYMBOL(arch_vcpu_is_preempted);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400381
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400382void smp_yield_cpu(int cpu)
383{
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200384 if (MACHINE_HAS_DIAG9C) {
Martin Schwidefskyb5a6b712015-08-21 16:05:32 +0200385 diag_stat_inc_norecursion(DIAG_STAT_X09C);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400386 asm volatile("diag %0,0,0x9c"
387 : : "d" (pcpu_devices[cpu].address));
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200388 } else if (MACHINE_HAS_DIAG44) {
Martin Schwidefskyb5a6b712015-08-21 16:05:32 +0200389 diag_stat_inc_norecursion(DIAG_STAT_X044);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400390 asm volatile("diag 0,0,0x44");
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200391 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400392}
393
394/*
395 * Send cpus emergency shutdown signal. This gives the cpus the
396 * opportunity to complete outstanding interrupts.
397 */
Heiko Carstens63df41d62013-09-06 19:10:48 +0200398static void smp_emergency_stop(cpumask_t *cpumask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400399{
400 u64 end;
401 int cpu;
402
Heiko Carstens1aae0562013-01-30 09:49:40 +0100403 end = get_tod_clock() + (1000000UL << 12);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400404 for_each_cpu(cpu, cpumask) {
405 struct pcpu *pcpu = pcpu_devices + cpu;
406 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200407 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
408 0, NULL) == SIGP_CC_BUSY &&
Heiko Carstens1aae0562013-01-30 09:49:40 +0100409 get_tod_clock() < end)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400410 cpu_relax();
411 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100412 while (get_tod_clock() < end) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400413 for_each_cpu(cpu, cpumask)
414 if (pcpu_stopped(pcpu_devices + cpu))
415 cpumask_clear_cpu(cpu, cpumask);
416 if (cpumask_empty(cpumask))
417 break;
418 cpu_relax();
419 }
420}
421
422/*
423 * Stop all cpus but the current one.
424 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100425void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100427 cpumask_t cpumask;
428 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Heiko Carstens677d7622007-11-20 11:13:37 +0100430 /* Disable all interrupts/machine checks */
Martin Schwidefskye258d712013-09-24 09:14:56 +0200431 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100432 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100433
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400434 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100435 cpumask_copy(&cpumask, cpu_online_mask);
436 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400438 if (oops_in_progress)
439 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100440
441 /* stop all processors */
442 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400443 struct pcpu *pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200444 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400445 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100446 cpu_relax();
447 }
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * This is the main routine where commands issued by other
452 * cpus are handled.
453 */
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200454static void smp_handle_ext_call(void)
455{
456 unsigned long bits;
457
458 /* handle bit signal external calls */
459 bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
460 if (test_bit(ec_stop_cpu, &bits))
461 smp_stop_cpu();
462 if (test_bit(ec_schedule, &bits))
463 scheduler_ipi();
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200464 if (test_bit(ec_call_function_single, &bits))
465 generic_smp_call_function_single_interrupt();
466}
467
Heiko Carstensfde15c32012-03-11 11:59:31 -0400468static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200469 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200471 inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
472 smp_handle_ext_call();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Rusty Russell630cd042009-09-24 09:34:45 -0600475void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100476{
477 int cpu;
478
Rusty Russell630cd042009-09-24 09:34:45 -0600479 for_each_cpu(cpu, mask)
Heiko Carstensb6ed49e2013-05-21 15:34:56 +0200480 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100481}
482
483void arch_send_call_function_single_ipi(int cpu)
484{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400485 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
489 * this function sends a 'reschedule' IPI to another CPU.
490 * it goes straight through and wastes no time serializing
491 * anything. Worst case is that we lose a reschedule ...
492 */
493void smp_send_reschedule(int cpu)
494{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400495 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * parameter area for the set/clear control bit callbacks
500 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200501struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400502 unsigned long orval;
503 unsigned long andval;
504 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200505};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507/*
508 * callback for setting/clearing control bits
509 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200510static void smp_ctl_bit_callback(void *info)
511{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200512 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200514
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200515 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400516 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200517 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520/*
521 * Set a bit in a control register of all cpus
522 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200523void smp_ctl_set_bit(int cr, int bit)
524{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400525 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200527 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200529EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531/*
532 * Clear a bit in a control register of all cpus
533 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200534void smp_ctl_clear_bit(int cr, int bit)
535{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400536 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200538 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200540EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Michael Holzheubf28a592014-04-14 10:38:05 +0200542#ifdef CONFIG_CRASH_DUMP
Michael Holzheu411ed322007-04-27 16:01:49 +0200543
Heiko Carstens1af135a2015-06-26 12:10:49 +0200544int smp_store_status(int cpu)
545{
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100546 struct pcpu *pcpu = pcpu_devices + cpu;
547 unsigned long pa;
Heiko Carstens1af135a2015-06-26 12:10:49 +0200548
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100549 pa = __pa(&pcpu->lowcore->floating_pt_save_area);
550 if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_STATUS_AT_ADDRESS,
551 pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
Heiko Carstens1af135a2015-06-26 12:10:49 +0200552 return -EIO;
553 if (!MACHINE_HAS_VX)
554 return 0;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100555 pa = __pa(pcpu->lowcore->vector_save_area_addr);
556 if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
557 pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
558 return -EIO;
Heiko Carstens1af135a2015-06-26 12:10:49 +0200559 return 0;
560}
561
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100562/*
563 * Collect CPU state of the previous, crashed system.
564 * There are four cases:
565 * 1) standard zfcp dump
566 * condition: OLDMEM_BASE == NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
567 * The state for all CPUs except the boot CPU needs to be collected
568 * with sigp stop-and-store-status. The boot CPU state is located in
569 * the absolute lowcore of the memory stored in the HSA. The zcore code
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100570 * will copy the boot CPU state from the HSA.
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100571 * 2) stand-alone kdump for SCSI (zfcp dump with swapped memory)
572 * condition: OLDMEM_BASE != NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
573 * The state for all CPUs except the boot CPU needs to be collected
574 * with sigp stop-and-store-status. The firmware or the boot-loader
575 * stored the registers of the boot CPU in the absolute lowcore in the
576 * memory of the old system.
577 * 3) kdump and the old kernel did not store the CPU state,
578 * or stand-alone kdump for DASD
579 * condition: OLDMEM_BASE != NULL && !is_kdump_kernel()
580 * The state for all CPUs except the boot CPU needs to be collected
581 * with sigp stop-and-store-status. The kexec code or the boot-loader
582 * stored the registers of the boot CPU in the memory of the old system.
583 * 4) kdump and the old kernel stored the CPU state
584 * condition: OLDMEM_BASE != NULL && is_kdump_kernel()
Martin Schwidefsky8a07dd02015-10-14 15:53:06 +0200585 * This case does not exist for s390 anymore, setup_arch explicitly
586 * deactivates the elfcorehdr= kernel parameter
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100587 */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100588static __init void smp_save_cpu_vxrs(struct save_area *sa, u16 addr,
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100589 bool is_boot_cpu, unsigned long page)
590{
591 __vector128 *vxrs = (__vector128 *) page;
592
593 if (is_boot_cpu)
594 vxrs = boot_cpu_vector_save_area;
595 else
596 __pcpu_sigp_relax(addr, SIGP_STORE_ADDITIONAL_STATUS, page);
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100597 save_area_add_vxrs(sa, vxrs);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100598}
599
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100600static __init void smp_save_cpu_regs(struct save_area *sa, u16 addr,
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100601 bool is_boot_cpu, unsigned long page)
602{
603 void *regs = (void *) page;
604
605 if (is_boot_cpu)
606 copy_oldmem_kernel(regs, (void *) __LC_FPREGS_SAVE_AREA, 512);
607 else
608 __pcpu_sigp_relax(addr, SIGP_STORE_STATUS_AT_ADDRESS, page);
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100609 save_area_add_regs(sa, regs);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100610}
611
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200612void __init smp_save_dump_cpus(void)
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100613{
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100614 int addr, boot_cpu_addr, max_cpu_addr;
615 struct save_area *sa;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100616 unsigned long page;
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200617 bool is_boot_cpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100618
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100619 if (!(OLDMEM_BASE || ipl_info.type == IPL_TYPE_FCP_DUMP))
620 /* No previous system present, normal boot. */
621 return;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100622 /* Allocate a page as dumping area for the store status sigps */
623 page = memblock_alloc_base(PAGE_SIZE, PAGE_SIZE, 1UL << 31);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100624 /* Set multi-threading state to the previous system. */
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200625 pcpu_set_smt(sclp.mtid_prev);
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200626 boot_cpu_addr = stap();
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100627 max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
628 for (addr = 0; addr <= max_cpu_addr; addr++) {
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100629 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0) ==
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200630 SIGP_CC_NOT_OPERATIONAL)
631 continue;
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200632 is_boot_cpu = (addr == boot_cpu_addr);
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100633 /* Allocate save area */
634 sa = save_area_alloc(is_boot_cpu);
635 if (!sa)
636 panic("could not allocate memory for save area\n");
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100637 if (MACHINE_HAS_VX)
638 /* Get the vector registers */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100639 smp_save_cpu_vxrs(sa, addr, is_boot_cpu, page);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100640 /*
641 * For a zfcp dump OLDMEM_BASE == NULL and the registers
642 * of the boot CPU are stored in the HSA. To retrieve
643 * these registers an SCLP request is required which is
644 * done by drivers/s390/char/zcore.c:init_cpu_info()
645 */
646 if (!is_boot_cpu || OLDMEM_BASE)
647 /* Get the CPU registers */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100648 smp_save_cpu_regs(sa, addr, is_boot_cpu, page);
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200649 }
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100650 memblock_free(page, PAGE_SIZE);
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200651 diag308_reset();
652 pcpu_set_smt(0);
Heiko Carstens1af135a2015-06-26 12:10:49 +0200653}
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100654#endif /* CONFIG_CRASH_DUMP */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400655
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200656void smp_cpu_set_polarization(int cpu, int val)
657{
658 pcpu_devices[cpu].polarization = val;
659}
660
661int smp_cpu_get_polarization(int cpu)
662{
663 return pcpu_devices[cpu].polarization;
664}
665
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100666static void __ref smp_get_core_info(struct sclp_core_info *info, int early)
Heiko Carstens08d07962008-01-26 14:10:56 +0100667{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400668 static int use_sigp_detection;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400669 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100670
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100671 if (use_sigp_detection || sclp_get_core_info(info, early)) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400672 use_sigp_detection = 1;
Martin Schwidefskye7086eb2015-06-17 10:45:31 +0200673 for (address = 0;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200674 address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100675 address += (1U << smp_cpu_mt_shift)) {
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100676 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0) ==
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200677 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400678 continue;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200679 info->core[info->configured].core_id =
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100680 address >> smp_cpu_mt_shift;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400681 info->configured++;
682 }
683 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100684 }
Heiko Carstens08d07962008-01-26 14:10:56 +0100685}
686
Paul Gortmakere2741f12013-06-18 17:04:52 -0400687static int smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100688
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200689static int __smp_rescan_cpus(struct sclp_core_info *info, int sysfs_add)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400690{
691 struct pcpu *pcpu;
692 cpumask_t avail;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100693 int cpu, nr, i, j;
694 u16 address;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400695
696 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200697 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400698 cpu = cpumask_first(&avail);
699 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200700 if (sclp.has_core_type && info->core[i].type != boot_core_type)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400701 continue;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200702 address = info->core[i].core_id << smp_cpu_mt_shift;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100703 for (j = 0; j <= smp_cpu_mtid; j++) {
704 if (pcpu_find_address(cpu_present_mask, address + j))
705 continue;
706 pcpu = pcpu_devices + cpu;
707 pcpu->address = address + j;
708 pcpu->state =
709 (cpu >= info->configured*(smp_cpu_mtid + 1)) ?
710 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
711 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
712 set_cpu_present(cpu, true);
713 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
714 set_cpu_present(cpu, false);
715 else
716 nr++;
717 cpu = cpumask_next(cpu, &avail);
718 if (cpu >= nr_cpu_ids)
719 break;
720 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400721 }
722 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100725void __init smp_detect_cpus(void)
Heiko Carstens48483b32008-01-26 14:11:05 +0100726{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100727 unsigned int cpu, mtid, c_cpus, s_cpus;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200728 struct sclp_core_info *info;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100729 u16 address;
Heiko Carstens48483b32008-01-26 14:11:05 +0100730
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100731 /* Get CPU information */
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100732 info = memblock_virt_alloc(sizeof(*info), 8);
733 smp_get_core_info(info, 1);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100734 /* Find boot CPU type */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200735 if (sclp.has_core_type) {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100736 address = stap();
737 for (cpu = 0; cpu < info->combined; cpu++)
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200738 if (info->core[cpu].core_id == address) {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100739 /* The boot cpu dictates the cpu type. */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200740 boot_core_type = info->core[cpu].type;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100741 break;
742 }
743 if (cpu >= info->combined)
744 panic("Could not find boot CPU type");
Heiko Carstens48483b32008-01-26 14:11:05 +0100745 }
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100746
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100747 /* Set multi-threading state for the current system */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200748 mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100749 mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
750 pcpu_set_smt(mtid);
751
752 /* Print number of CPUs */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400753 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100754 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200755 if (sclp.has_core_type &&
756 info->core[cpu].type != boot_core_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100757 continue;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100758 if (cpu < info->configured)
759 c_cpus += smp_cpu_mtid + 1;
760 else
761 s_cpus += smp_cpu_mtid + 1;
Heiko Carstens48483b32008-01-26 14:11:05 +0100762 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100763 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100764
765 /* Add CPUs present at boot */
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100766 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400767 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100768 put_online_cpus();
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100769 memblock_free_early((unsigned long)info, sizeof(*info));
Heiko Carstens48483b32008-01-26 14:11:05 +0100770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200773 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Paul Gortmakere2741f12013-06-18 17:04:52 -0400775static void smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Heiko Carstens1aae0562013-01-30 09:49:40 +0100777 S390_lowcore.last_update_clock = get_tod_clock();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400778 S390_lowcore.restart_stack = (unsigned long) restart_stack;
779 S390_lowcore.restart_fn = (unsigned long) do_restart;
780 S390_lowcore.restart_data = 0;
781 S390_lowcore.restart_source = -1UL;
782 restore_access_regs(S390_lowcore.access_regs_save_area);
783 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
Martin Schwidefskye258d712013-09-24 09:14:56 +0200784 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200785 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800786 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200787 init_cpu_timer();
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +0200788 vtime_init();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100789 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200790 notify_cpu_starting(smp_processor_id());
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200791 set_cpu_online(smp_processor_id(), true);
Heiko Carstens93f3b2e2013-01-02 16:54:12 +0100792 inc_irq_stat(CPU_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 local_irq_enable();
Thomas Gleixnerfc6d73d2016-02-26 18:43:40 +0000794 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/* Upping and downing of CPUs */
Paul Gortmakere2741f12013-06-18 17:04:52 -0400798int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400800 struct pcpu *pcpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100801 int base, i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400803 pcpu = pcpu_devices + cpu;
804 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100805 return -EIO;
Heiko Carstens54231452016-12-05 21:18:58 +0100806 base = smp_get_base_cpu(cpu);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100807 for (i = 0; i <= smp_cpu_mtid; i++) {
808 if (base + i < nr_cpu_ids)
809 if (cpu_online(base + i))
810 break;
811 }
812 /*
813 * If this is the first CPU of the core to get online
814 * do an initial CPU reset.
815 */
816 if (i > smp_cpu_mtid &&
817 pcpu_sigp_retry(pcpu_devices + base, SIGP_INITIAL_CPU_RESET, 0) !=
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200818 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400819 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000820
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400821 rc = pcpu_alloc_lowcore(pcpu, cpu);
822 if (rc)
823 return rc;
824 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000825 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400826 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Heiko Carstensa1307bb2015-03-30 12:51:42 +0200827 /* Wait until cpu puts itself in the online & active maps */
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +0100828 while (!cpu_online(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 cpu_relax();
830 return 0;
831}
832
Heiko Carstensd80512f2013-12-16 14:31:26 +0100833static unsigned int setup_possible_cpus __initdata;
Heiko Carstens48483b32008-01-26 14:11:05 +0100834
Heiko Carstensd80512f2013-12-16 14:31:26 +0100835static int __init _setup_possible_cpus(char *s)
836{
837 get_option(&s, &setup_possible_cpus);
Heiko Carstens37a33022006-02-17 13:52:47 -0800838 return 0;
839}
Heiko Carstensd80512f2013-12-16 14:31:26 +0100840early_param("possible_cpus", _setup_possible_cpus);
Heiko Carstens37a33022006-02-17 13:52:47 -0800841
Heiko Carstens48483b32008-01-26 14:11:05 +0100842#ifdef CONFIG_HOTPLUG_CPU
843
Heiko Carstens39ce0102007-04-27 16:02:00 +0200844int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400846 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200848 /* Handle possible pending IPIs */
849 smp_handle_ext_call();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400850 set_cpu_online(smp_processor_id(), false);
851 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100852 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400853 /* Disable interrupt sources via control register. */
854 __ctl_store(cregs, 0, 15);
855 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
856 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
857 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
858 __ctl_load(cregs, 0, 15);
Martin Schwidefskyfe0f4972014-09-30 17:37:52 +0200859 clear_cpu_flag(CIF_NOHZ_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
Heiko Carstens39ce0102007-04-27 16:02:00 +0200863void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400865 struct pcpu *pcpu;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400868 pcpu = pcpu_devices + cpu;
869 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400871 pcpu_free_lowcore(pcpu);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200872 cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200873 cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Heiko Carstensb456d942011-05-23 10:24:33 +0200876void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 idle_task_exit();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200879 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400880 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Heiko Carstens255acee2006-02-17 13:52:46 -0800883#endif /* CONFIG_HOTPLUG_CPU */
884
Heiko Carstensd80512f2013-12-16 14:31:26 +0100885void __init smp_fill_possible_mask(void)
886{
David Hildenbrand9747bc42015-05-06 09:29:53 +0200887 unsigned int possible, sclp_max, cpu;
Heiko Carstensd80512f2013-12-16 14:31:26 +0100888
David Hildenbrand3a9f3fe2015-05-06 13:19:29 +0200889 sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
890 sclp_max = min(smp_max_threads, sclp_max);
Dan Carpenter61282af2016-07-18 09:02:56 +0200891 sclp_max = (sclp.max_cores * sclp_max) ?: nr_cpu_ids;
Heiko Carstenscf813db2014-03-10 14:50:16 +0100892 possible = setup_possible_cpus ?: nr_cpu_ids;
David Hildenbrand9747bc42015-05-06 09:29:53 +0200893 possible = min(possible, sclp_max);
Heiko Carstensd80512f2013-12-16 14:31:26 +0100894 for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
895 set_cpu_possible(cpu, true);
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898void __init smp_prepare_cpus(unsigned int max_cpus)
899{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200900 /* request the 0x1201 emergency signal external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200901 if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
Heiko Carstens39ce0102007-04-27 16:02:00 +0200902 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100903 /* request the 0x1202 external call external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200904 if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100905 panic("Couldn't request external interrupt 0x1202");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200908void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400910 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400912 pcpu->state = CPU_STATE_CONFIGURED;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100913 pcpu->address = stap();
Heiko Carstensc667aea2015-12-31 10:29:00 +0100914 pcpu->lowcore = (struct lowcore *)(unsigned long) store_prefix();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400915 S390_lowcore.percpu_offset = __per_cpu_offset[0];
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200916 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200917 set_cpu_present(0, true);
918 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200921void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Heiko Carstens02beacc2010-01-13 20:44:34 +0100925void __init smp_setup_processor_id(void)
926{
927 S390_lowcore.cpu_nr = 0;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200928 S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
Heiko Carstens02beacc2010-01-13 20:44:34 +0100929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * the frequency of the profiling timer can be changed
933 * by writing a multiplier value into /proc/profile.
934 *
935 * usually you want to run this on all CPUs ;)
936 */
937int setup_profiling_timer(unsigned int multiplier)
938{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Heiko Carstens08d07962008-01-26 14:10:56 +0100942#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800943static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400944 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100945{
946 ssize_t count;
947
948 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400949 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100950 mutex_unlock(&smp_cpu_state_mutex);
951 return count;
952}
953
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800954static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400955 struct device_attribute *attr,
956 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100957{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400958 struct pcpu *pcpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100959 int cpu, val, rc, i;
Heiko Carstens08d07962008-01-26 14:10:56 +0100960 char delim;
961
962 if (sscanf(buf, "%d %c", &val, &delim) != 1)
963 return -EINVAL;
964 if (val != 0 && val != 1)
965 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100966 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200967 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100968 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100969 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400970 cpu = dev->id;
Heiko Carstens54231452016-12-05 21:18:58 +0100971 cpu = smp_get_base_cpu(cpu);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100972 if (cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100973 goto out;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100974 for (i = 0; i <= smp_cpu_mtid; i++)
975 if (cpu_online(cpu + i))
976 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400977 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100978 rc = 0;
979 switch (val) {
980 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400981 if (pcpu->state != CPU_STATE_CONFIGURED)
982 break;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200983 rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400984 if (rc)
985 break;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100986 for (i = 0; i <= smp_cpu_mtid; i++) {
987 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
988 continue;
989 pcpu[i].state = CPU_STATE_STANDBY;
990 smp_cpu_set_polarization(cpu + i,
991 POLARIZATION_UNKNOWN);
992 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400993 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +0100994 break;
995 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400996 if (pcpu->state != CPU_STATE_STANDBY)
997 break;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200998 rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400999 if (rc)
1000 break;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001001 for (i = 0; i <= smp_cpu_mtid; i++) {
1002 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1003 continue;
1004 pcpu[i].state = CPU_STATE_CONFIGURED;
1005 smp_cpu_set_polarization(cpu + i,
1006 POLARIZATION_UNKNOWN);
1007 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001008 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +01001009 break;
1010 default:
1011 break;
1012 }
1013out:
Heiko Carstens08d07962008-01-26 14:10:56 +01001014 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001015 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001016 return rc ? rc : count;
1017}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001018static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001019#endif /* CONFIG_HOTPLUG_CPU */
1020
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001021static ssize_t show_cpu_address(struct device *dev,
1022 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +01001023{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001024 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +01001025}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001026static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +01001027
Heiko Carstens08d07962008-01-26 14:10:56 +01001028static struct attribute *cpu_common_attrs[] = {
1029#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001030 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +01001031#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001032 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +01001033 NULL,
1034};
1035
1036static struct attribute_group cpu_common_attr_group = {
1037 .attrs = cpu_common_attrs,
1038};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Heiko Carstens08d07962008-01-26 14:10:56 +01001040static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001041 &dev_attr_idle_count.attr,
1042 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001043 NULL,
1044};
1045
Heiko Carstens08d07962008-01-26 14:10:56 +01001046static struct attribute_group cpu_online_attr_group = {
1047 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001048};
1049
Sebastian Andrzej Siewiordfbbd862016-11-04 15:45:03 +01001050static int smp_cpu_online(unsigned int cpu)
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001051{
Heiko Carstens2f859d02015-02-11 12:31:03 +01001052 struct device *s = &per_cpu(cpu_device, cpu)->dev;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001053
Sebastian Andrzej Siewiordfbbd862016-11-04 15:45:03 +01001054 return sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1055}
1056static int smp_cpu_pre_down(unsigned int cpu)
1057{
1058 struct device *s = &per_cpu(cpu_device, cpu)->dev;
1059
1060 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1061 return 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001062}
1063
Paul Gortmakere2741f12013-06-18 17:04:52 -04001064static int smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +01001065{
Heiko Carstens96619fc2013-12-05 12:42:09 +01001066 struct device *s;
1067 struct cpu *c;
Heiko Carstens08d07962008-01-26 14:10:56 +01001068 int rc;
1069
Heiko Carstens96619fc2013-12-05 12:42:09 +01001070 c = kzalloc(sizeof(*c), GFP_KERNEL);
1071 if (!c)
1072 return -ENOMEM;
Heiko Carstens2f859d02015-02-11 12:31:03 +01001073 per_cpu(cpu_device, cpu) = c;
Heiko Carstens96619fc2013-12-05 12:42:09 +01001074 s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +01001075 c->hotpluggable = 1;
1076 rc = register_cpu(c, cpu);
1077 if (rc)
1078 goto out;
1079 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1080 if (rc)
1081 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +01001082 rc = topology_cpu_init(c);
1083 if (rc)
1084 goto out_topology;
1085 return 0;
1086
1087out_topology:
Heiko Carstens08d07962008-01-26 14:10:56 +01001088 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1089out_cpu:
1090#ifdef CONFIG_HOTPLUG_CPU
1091 unregister_cpu(c);
1092#endif
1093out:
1094 return rc;
1095}
1096
1097#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001098
Heiko Carstens67060d92008-05-30 10:03:27 +02001099int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001100{
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001101 struct sclp_core_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001102 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001103
Heiko Carstensaf51160e2016-12-03 09:48:01 +01001104 info = kzalloc(sizeof(*info), GFP_KERNEL);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001105 if (!info)
1106 return -ENOMEM;
Heiko Carstensaf51160e2016-12-03 09:48:01 +01001107 smp_get_core_info(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001108 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001109 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001110 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001111 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001112 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001113 kfree(info);
1114 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001115 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001116 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001117}
1118
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001119static ssize_t __ref rescan_store(struct device *dev,
1120 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001121 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001122 size_t count)
1123{
1124 int rc;
1125
1126 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001127 return rc ? rc : count;
1128}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001129static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001130#endif /* CONFIG_HOTPLUG_CPU */
1131
Heiko Carstens83a24e32011-12-27 11:27:09 +01001132static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001133{
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301134 int cpu, rc = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001135
Heiko Carstens08d07962008-01-26 14:10:56 +01001136#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001137 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001138 if (rc)
1139 return rc;
1140#endif
1141 for_each_present_cpu(cpu) {
1142 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001143 if (rc)
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301146
Sebastian Andrzej Siewiordfbbd862016-11-04 15:45:03 +01001147 rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "s390/smp:online",
1148 smp_cpu_online, smp_cpu_pre_down);
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301149out:
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301150 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001152subsys_initcall(s390_smp_init);