blob: 10dbb12eb14d2677b964037d0e99fe1ccfd3ecd1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04003 * SMP related functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 1999, 2012
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04006 * Author(s): Denis Joseph Barrow,
7 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
8 * Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Heiko Carstens39ce0102007-04-27 16:02:00 +020010 * based on other smp stuff by
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
12 * (c) 1998 Ingo Molnar
13 *
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040014 * The code outside of smp.c uses logical cpu numbers, only smp.c does
15 * the translation of logical to physical cpu ids. All new code that
16 * operates on physical cpu numbers needs to go into smp.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Martin Schwidefsky395d31d2008-12-25 13:39:50 +010019#define KMSG_COMPONENT "cpu"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
Heiko Carstensf2308862011-01-05 12:48:08 +010022#include <linux/workqueue.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070023#include <linux/memblock.h>
Paul Gortmaker3994a522017-02-09 15:20:23 -050024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040027#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/spinlock.h>
29#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/interrupt.h>
Christian Borntraeger3324e602009-03-26 15:23:56 +010032#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +010035#include <linux/sched/hotplug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010036#include <linux/sched/task_stack.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010037#include <linux/crash_dump.h>
Martin Schwidefsky00a8f882017-09-15 16:24:31 +020038#include <linux/kprobes.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010039#include <asm/asm-offsets.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020040#include <asm/diag.h>
Heiko Carstens1e3cab22012-03-30 09:40:55 +020041#include <asm/switch_to.h>
42#include <asm/facility.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010043#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010044#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/tlbflush.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020047#include <asm/vtimer.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 Schwidefskyc742b312008-12-31 15:11:42 +010050#include <asm/vdso.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040051#include <asm/debug.h>
Michael Holzheu4857d4b2012-03-11 11:59:34 -040052#include <asm/os_info.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020053#include <asm/sigp.h>
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020054#include <asm/idle.h>
Martin Schwidefsky916cda12016-01-26 14:10:34 +010055#include <asm/nmi.h>
Martin Schwidefsky78c98f92019-01-28 08:33:08 +010056#include <asm/stacktrace.h>
Thomas Richter38389ec2017-11-16 14:26:36 +010057#include <asm/topology.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020058#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040060enum {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040061 ec_schedule = 0,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040062 ec_call_function_single,
63 ec_stop_cpu,
64};
Heiko Carstens08d07962008-01-26 14:10:56 +010065
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040066enum {
Heiko Carstens08d07962008-01-26 14:10:56 +010067 CPU_STATE_STANDBY,
68 CPU_STATE_CONFIGURED,
69};
70
Heiko Carstens2f859d02015-02-11 12:31:03 +010071static DEFINE_PER_CPU(struct cpu *, cpu_device);
72
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040073struct pcpu {
Heiko Carstensc667aea2015-12-31 10:29:00 +010074 struct lowcore *lowcore; /* lowcore page(s) for the cpu */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040075 unsigned long ec_mask; /* bit mask for ec_xxx functions */
Heiko Carstens3dbc78d2015-12-08 14:10:12 +010076 unsigned long ec_clk; /* sigp timestamp for ec_xxx */
Heiko Carstens2f859d02015-02-11 12:31:03 +010077 signed char state; /* physical cpu state */
78 signed char polarization; /* physical polarization */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040079 u16 address; /* physical cpu address */
80};
81
Martin Schwidefskyd08d9432015-06-18 14:23:00 +020082static u8 boot_core_type;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040083static struct pcpu pcpu_devices[NR_CPUS];
84
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010085unsigned int smp_cpu_mt_shift;
86EXPORT_SYMBOL(smp_cpu_mt_shift);
87
88unsigned int smp_cpu_mtid;
89EXPORT_SYMBOL(smp_cpu_mtid);
90
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010091#ifdef CONFIG_CRASH_DUMP
92__vector128 __initdata boot_cpu_vector_save_area[__NUM_VXRS];
93#endif
94
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010095static unsigned int smp_max_threads __initdata = -1U;
96
97static int __init early_nosmt(char *s)
98{
99 smp_max_threads = 1;
100 return 0;
101}
102early_param("nosmt", early_nosmt);
103
104static int __init early_smt(char *s)
105{
106 get_option(&s, &smp_max_threads);
107 return 0;
108}
109early_param("smt", early_smt);
110
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200111/*
112 * The smp_cpu_state_mutex must be held when changing the state or polarization
113 * member of a pcpu data structure within the pcpu_devices arreay.
114 */
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200115DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100116
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400117/*
118 * Signal processor helper functions.
119 */
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100120static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100121{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400122 int cc;
123
124 while (1) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200125 cc = __pcpu_sigp(addr, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200126 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400127 return cc;
128 cpu_relax();
129 }
130}
131
132static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
133{
134 int cc, retry;
135
136 for (retry = 0; ; retry++) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200137 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200138 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400139 break;
140 if (retry >= 3)
141 udelay(10);
142 }
143 return cc;
144}
145
146static inline int pcpu_stopped(struct pcpu *pcpu)
147{
Heiko Carstens41459d36cf2012-09-14 11:09:52 +0200148 u32 uninitialized_var(status);
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200149
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200150 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200151 0, &status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400152 return 0;
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200153 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400154}
155
156static inline int pcpu_running(struct pcpu *pcpu)
157{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200158 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200159 0, NULL) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400160 return 1;
Heiko Carstens524b24ad2012-06-04 12:11:41 +0200161 /* Status stored condition code is equivalent to cpu not running. */
162 return 0;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100163}
164
Michael Holzheu1943f532011-10-30 15:16:38 +0100165/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400166 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100167 */
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100168static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100169{
170 int cpu;
171
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400172 for_each_cpu(cpu, mask)
173 if (pcpu_devices[cpu].address == address)
174 return pcpu_devices + cpu;
175 return NULL;
176}
177
178static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
179{
180 int order;
181
Heiko Carstensdea24192013-12-03 10:06:29 +0100182 if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
183 return;
184 order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
Heiko Carstens3dbc78d2015-12-08 14:10:12 +0100185 pcpu->ec_clk = get_tod_clock_fast();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400186 pcpu_sigp_retry(pcpu, order, 0);
187}
188
Paul Gortmakere2741f12013-06-18 17:04:52 -0400189static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400190{
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200191 unsigned long async_stack, nodat_stack;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100192 struct lowcore *lc;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400193
194 if (pcpu != &pcpu_devices[0]) {
Heiko Carstensc667aea2015-12-31 10:29:00 +0100195 pcpu->lowcore = (struct lowcore *)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400196 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Vasily Gorbik32ce55a2018-09-18 18:23:40 +0200197 nodat_stack = __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200198 if (!pcpu->lowcore || !nodat_stack)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400199 goto out;
Heiko Carstens2f859d02015-02-11 12:31:03 +0100200 } else {
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200201 nodat_stack = pcpu->lowcore->nodat_stack - STACK_INIT_OFFSET;
Michael Holzheu1943f532011-10-30 15:16:38 +0100202 }
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200203 async_stack = stack_alloc();
204 if (!async_stack)
205 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400206 lc = pcpu->lowcore;
207 memcpy(lc, &S390_lowcore, 512);
208 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200209 lc->async_stack = async_stack + STACK_INIT_OFFSET;
210 lc->nodat_stack = nodat_stack + STACK_INIT_OFFSET;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400211 lc->cpu_nr = cpu;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200212 lc->spinlock_lockval = arch_spin_lockval(cpu);
Martin Schwidefskyb96f7d82017-03-24 17:25:02 +0100213 lc->spinlock_index = 0;
Martin Schwidefskyf19fbd52018-01-26 12:46:47 +0100214 lc->br_r1_trampoline = 0x07f1; /* br %r1 */
Sven Schnelle0b38b5e2020-01-22 13:38:22 +0100215 lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
216 lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200217 if (nmi_alloc_per_cpu(lc))
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200218 goto out_async;
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200219 if (vdso_alloc_per_cpu(lc))
220 goto out_mcesa;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400221 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200222 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400223 return 0;
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200224
225out_mcesa:
226 nmi_free_per_cpu(lc);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200227out_async:
228 stack_free(async_stack);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400229out:
230 if (pcpu != &pcpu_devices[0]) {
Vasily Gorbik32ce55a2018-09-18 18:23:40 +0200231 free_pages(nodat_stack, THREAD_SIZE_ORDER);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400232 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
233 }
234 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100235}
236
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400237static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100238{
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200239 unsigned long async_stack, nodat_stack, lowcore;
240
241 nodat_stack = pcpu->lowcore->nodat_stack - STACK_INIT_OFFSET;
242 async_stack = pcpu->lowcore->async_stack - STACK_INIT_OFFSET;
243 lowcore = (unsigned long) pcpu->lowcore;
244
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200245 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400246 lowcore_ptr[pcpu - pcpu_devices] = NULL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400247 vdso_free_per_cpu(pcpu->lowcore);
Martin Schwidefsky6c815112017-10-12 13:24:47 +0200248 nmi_free_per_cpu(pcpu->lowcore);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200249 stack_free(async_stack);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100250 if (pcpu == &pcpu_devices[0])
251 return;
Vasily Gorbik32ce55a2018-09-18 18:23:40 +0200252 free_pages(nodat_stack, THREAD_SIZE_ORDER);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200253 free_pages(lowcore, LC_ORDER);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100254}
255
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400256static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100257{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100258 struct lowcore *lc = pcpu->lowcore;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400259
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200260 cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200261 cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400262 lc->cpu_nr = cpu;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200263 lc->spinlock_lockval = arch_spin_lockval(cpu);
Martin Schwidefskyb96f7d82017-03-24 17:25:02 +0100264 lc->spinlock_index = 0;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400265 lc->percpu_offset = __per_cpu_offset[cpu];
266 lc->kernel_asce = S390_lowcore.kernel_asce;
Heiko Carstensa2308c12019-11-18 13:09:52 +0100267 lc->user_asce = S390_lowcore.kernel_asce;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400268 lc->machine_flags = S390_lowcore.machine_flags;
Martin Schwidefsky152e9b82019-03-06 13:31:21 +0200269 lc->user_timer = lc->system_timer =
270 lc->steal_timer = lc->avg_steal_timer = 0;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400271 __ctl_store(lc->cregs_save_area, 0, 15);
Heiko Carstensa2308c12019-11-18 13:09:52 +0100272 lc->cregs_save_area[1] = lc->kernel_asce;
273 lc->cregs_save_area[7] = lc->vdso_asce;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400274 save_access_regs((unsigned int *) lc->access_regs_save_area);
275 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
Martin Schwidefskycf148992018-01-16 07:03:44 +0100276 sizeof(lc->stfle_fac_list));
277 memcpy(lc->alt_stfle_fac_list, S390_lowcore.alt_stfle_fac_list,
278 sizeof(lc->alt_stfle_fac_list));
Martin Schwidefskyb96f7d82017-03-24 17:25:02 +0100279 arch_spin_lock_setup(cpu);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100280}
281
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400282static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
283{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100284 struct lowcore *lc = pcpu->lowcore;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400285
Martin Schwidefskydc7ee002013-04-24 10:20:43 +0200286 lc->kernel_stack = (unsigned long) task_stack_page(tsk)
287 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400288 lc->current_task = (unsigned long) tsk;
Christian Borntraegere22cf8c2015-10-06 18:06:15 +0200289 lc->lpp = LPP_MAGIC;
290 lc->current_pid = tsk->pid;
Martin Schwidefsky90c53e62016-11-08 12:15:59 +0100291 lc->user_timer = tsk->thread.user_timer;
Christian Borntraegerb7662ee2017-10-04 14:46:17 +0200292 lc->guest_timer = tsk->thread.guest_timer;
Martin Schwidefsky90c53e62016-11-08 12:15:59 +0100293 lc->system_timer = tsk->thread.system_timer;
Christian Borntraegerb7662ee2017-10-04 14:46:17 +0200294 lc->hardirq_timer = tsk->thread.hardirq_timer;
295 lc->softirq_timer = tsk->thread.softirq_timer;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400296 lc->steal_timer = 0;
297}
298
299static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
300{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100301 struct lowcore *lc = pcpu->lowcore;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400302
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200303 lc->restart_stack = lc->nodat_stack;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400304 lc->restart_fn = (unsigned long) func;
305 lc->restart_data = (unsigned long) data;
306 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200307 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400308}
309
310/*
311 * Call function via PSW restart on pcpu and stop the current cpu.
312 */
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200313static void __pcpu_delegate(void (*func)(void*), void *data)
314{
315 func(data); /* should not return */
316}
317
Vasily Gorbikac1256f2017-11-20 12:15:10 +0100318static void __no_sanitize_address pcpu_delegate(struct pcpu *pcpu,
319 void (*func)(void *),
320 void *data, unsigned long stack)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400321{
Heiko Carstensc667aea2015-12-31 10:29:00 +0100322 struct lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
Heiko Carstensfbe76562012-06-05 09:59:52 +0200323 unsigned long source_cpu = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400324
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200325 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Heiko Carstensfbe76562012-06-05 09:59:52 +0200326 if (pcpu->address == source_cpu)
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200327 CALL_ON_STACK(__pcpu_delegate, stack, 2, func, data);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400328 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200329 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400330 /* Restart func on the target cpu and stop the current cpu. */
Heiko Carstensfbe76562012-06-05 09:59:52 +0200331 mem_assign_absolute(lc->restart_stack, stack);
332 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
333 mem_assign_absolute(lc->restart_data, (unsigned long) data);
334 mem_assign_absolute(lc->restart_source, source_cpu);
Martin Schwidefskyd768bd82018-01-16 07:11:45 +0100335 __bpon();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400336 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200337 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400338 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200339 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400340 " brc 2,1b # busy, try again\n"
Heiko Carstensfbe76562012-06-05 09:59:52 +0200341 : : "d" (pcpu->address), "d" (source_cpu),
Heiko Carstenseb546192012-06-04 15:05:43 +0200342 "K" (SIGP_RESTART), "K" (SIGP_STOP)
343 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400344 for (;;) ;
345}
346
347/*
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100348 * Enable additional logical cpus for multi-threading.
349 */
350static int pcpu_set_smt(unsigned int mtid)
351{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100352 int cc;
353
354 if (smp_cpu_mtid == mtid)
355 return 0;
Heiko Carstens80a60f62016-06-20 14:04:17 +0200356 cc = __pcpu_sigp(0, SIGP_SET_MULTI_THREADING, mtid, NULL);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100357 if (cc == 0) {
358 smp_cpu_mtid = mtid;
359 smp_cpu_mt_shift = 0;
360 while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
361 smp_cpu_mt_shift++;
362 pcpu_devices[0].address = stap();
363 }
364 return cc;
365}
366
367/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400368 * Call function on an online CPU.
369 */
370void smp_call_online_cpu(void (*func)(void *), void *data)
371{
372 struct pcpu *pcpu;
373
374 /* Use the current cpu if it is online. */
375 pcpu = pcpu_find_address(cpu_online_mask, stap());
376 if (!pcpu)
377 /* Use the first online cpu. */
378 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
379 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
380}
381
382/*
383 * Call function on the ipl CPU.
384 */
385void smp_call_ipl_cpu(void (*func)(void *), void *data)
386{
David Hildenbrand60f1bf22019-01-11 15:18:22 +0100387 struct lowcore *lc = pcpu_devices->lowcore;
388
389 if (pcpu_devices[0].address == stap())
390 lc = &S390_lowcore;
391
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400392 pcpu_delegate(&pcpu_devices[0], func, data,
David Hildenbrand60f1bf22019-01-11 15:18:22 +0100393 lc->nodat_stack);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400394}
395
396int smp_find_processor_id(u16 address)
397{
398 int cpu;
399
400 for_each_present_cpu(cpu)
401 if (pcpu_devices[cpu].address == address)
402 return cpu;
403 return -1;
404}
405
Philipp Rudo8ebf6da2020-04-06 14:47:48 +0200406bool notrace arch_vcpu_is_preempted(int cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400407{
Christian Borntraeger760928c2016-11-02 05:08:32 -0400408 if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu))
409 return false;
410 if (pcpu_running(pcpu_devices + cpu))
411 return false;
412 return true;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400413}
Christian Borntraeger760928c2016-11-02 05:08:32 -0400414EXPORT_SYMBOL(arch_vcpu_is_preempted);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400415
Philipp Rudo8ebf6da2020-04-06 14:47:48 +0200416void notrace smp_yield_cpu(int cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400417{
Heiko Carstens1b68ac82019-11-29 12:59:59 +0100418 if (!MACHINE_HAS_DIAG9C)
419 return;
420 diag_stat_inc_norecursion(DIAG_STAT_X09C);
421 asm volatile("diag %0,0,0x9c"
422 : : "d" (pcpu_devices[cpu].address));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400423}
424
425/*
426 * Send cpus emergency shutdown signal. This gives the cpus the
427 * opportunity to complete outstanding interrupts.
428 */
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200429void notrace smp_emergency_stop(void)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400430{
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200431 cpumask_t cpumask;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400432 u64 end;
433 int cpu;
434
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200435 cpumask_copy(&cpumask, cpu_online_mask);
436 cpumask_clear_cpu(smp_processor_id(), &cpumask);
437
Heiko Carstens1aae0562013-01-30 09:49:40 +0100438 end = get_tod_clock() + (1000000UL << 12);
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200439 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400440 struct pcpu *pcpu = pcpu_devices + cpu;
441 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200442 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
443 0, NULL) == SIGP_CC_BUSY &&
Heiko Carstens1aae0562013-01-30 09:49:40 +0100444 get_tod_clock() < end)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400445 cpu_relax();
446 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100447 while (get_tod_clock() < end) {
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200448 for_each_cpu(cpu, &cpumask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400449 if (pcpu_stopped(pcpu_devices + cpu))
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200450 cpumask_clear_cpu(cpu, &cpumask);
451 if (cpumask_empty(&cpumask))
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400452 break;
453 cpu_relax();
454 }
455}
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200456NOKPROBE_SYMBOL(smp_emergency_stop);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400457
458/*
459 * Stop all cpus but the current one.
460 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100461void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100463 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Heiko Carstens677d7622007-11-20 11:13:37 +0100465 /* Disable all interrupts/machine checks */
Martin Schwidefskye258d712013-09-24 09:14:56 +0200466 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100467 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100468
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400469 debug_set_critical();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400471 if (oops_in_progress)
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200472 smp_emergency_stop();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100473
474 /* stop all processors */
Martin Schwidefsky00a8f882017-09-15 16:24:31 +0200475 for_each_online_cpu(cpu) {
476 if (cpu == smp_processor_id())
477 continue;
478 pcpu_sigp_retry(pcpu_devices + cpu, SIGP_STOP, 0);
479 while (!pcpu_stopped(pcpu_devices + cpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100480 cpu_relax();
481 }
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * This is the main routine where commands issued by other
486 * cpus are handled.
487 */
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200488static void smp_handle_ext_call(void)
489{
490 unsigned long bits;
491
492 /* handle bit signal external calls */
493 bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
494 if (test_bit(ec_stop_cpu, &bits))
495 smp_stop_cpu();
496 if (test_bit(ec_schedule, &bits))
497 scheduler_ipi();
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200498 if (test_bit(ec_call_function_single, &bits))
499 generic_smp_call_function_single_interrupt();
500}
501
Heiko Carstensfde15c32012-03-11 11:59:31 -0400502static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200503 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200505 inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
506 smp_handle_ext_call();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Rusty Russell630cd042009-09-24 09:34:45 -0600509void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100510{
511 int cpu;
512
Rusty Russell630cd042009-09-24 09:34:45 -0600513 for_each_cpu(cpu, mask)
Heiko Carstensb6ed49e2013-05-21 15:34:56 +0200514 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100515}
516
517void arch_send_call_function_single_ipi(int cpu)
518{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400519 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * this function sends a 'reschedule' IPI to another CPU.
524 * it goes straight through and wastes no time serializing
525 * anything. Worst case is that we lose a reschedule ...
526 */
527void smp_send_reschedule(int cpu)
528{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400529 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532/*
533 * parameter area for the set/clear control bit callbacks
534 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200535struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400536 unsigned long orval;
537 unsigned long andval;
538 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200539};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541/*
542 * callback for setting/clearing control bits
543 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200544static void smp_ctl_bit_callback(void *info)
545{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200546 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200548
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200549 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400550 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200551 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
555 * Set a bit in a control register of all cpus
556 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200557void smp_ctl_set_bit(int cr, int bit)
558{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400559 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200561 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200563EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565/*
566 * Clear a bit in a control register of all cpus
567 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200568void smp_ctl_clear_bit(int cr, int bit)
569{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400570 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200572 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200574EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Michael Holzheubf28a592014-04-14 10:38:05 +0200576#ifdef CONFIG_CRASH_DUMP
Michael Holzheu411ed322007-04-27 16:01:49 +0200577
Heiko Carstens1af135a2015-06-26 12:10:49 +0200578int smp_store_status(int cpu)
579{
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100580 struct pcpu *pcpu = pcpu_devices + cpu;
581 unsigned long pa;
Heiko Carstens1af135a2015-06-26 12:10:49 +0200582
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100583 pa = __pa(&pcpu->lowcore->floating_pt_save_area);
584 if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_STATUS_AT_ADDRESS,
585 pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
Heiko Carstens1af135a2015-06-26 12:10:49 +0200586 return -EIO;
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100587 if (!MACHINE_HAS_VX && !MACHINE_HAS_GS)
Heiko Carstens1af135a2015-06-26 12:10:49 +0200588 return 0;
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100589 pa = __pa(pcpu->lowcore->mcesad & MCESA_ORIGIN_MASK);
590 if (MACHINE_HAS_GS)
591 pa |= pcpu->lowcore->mcesad & MCESA_LC_MASK;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100592 if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
593 pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
594 return -EIO;
Heiko Carstens1af135a2015-06-26 12:10:49 +0200595 return 0;
596}
597
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100598/*
599 * Collect CPU state of the previous, crashed system.
600 * There are four cases:
601 * 1) standard zfcp dump
602 * condition: OLDMEM_BASE == NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
603 * The state for all CPUs except the boot CPU needs to be collected
604 * with sigp stop-and-store-status. The boot CPU state is located in
605 * the absolute lowcore of the memory stored in the HSA. The zcore code
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100606 * will copy the boot CPU state from the HSA.
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100607 * 2) stand-alone kdump for SCSI (zfcp dump with swapped memory)
608 * condition: OLDMEM_BASE != NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
609 * The state for all CPUs except the boot CPU needs to be collected
610 * with sigp stop-and-store-status. The firmware or the boot-loader
611 * stored the registers of the boot CPU in the absolute lowcore in the
612 * memory of the old system.
613 * 3) kdump and the old kernel did not store the CPU state,
614 * or stand-alone kdump for DASD
615 * condition: OLDMEM_BASE != NULL && !is_kdump_kernel()
616 * The state for all CPUs except the boot CPU needs to be collected
617 * with sigp stop-and-store-status. The kexec code or the boot-loader
618 * stored the registers of the boot CPU in the memory of the old system.
619 * 4) kdump and the old kernel stored the CPU state
620 * condition: OLDMEM_BASE != NULL && is_kdump_kernel()
Martin Schwidefsky8a07dd02015-10-14 15:53:06 +0200621 * This case does not exist for s390 anymore, setup_arch explicitly
622 * deactivates the elfcorehdr= kernel parameter
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100623 */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100624static __init void smp_save_cpu_vxrs(struct save_area *sa, u16 addr,
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100625 bool is_boot_cpu, unsigned long page)
626{
627 __vector128 *vxrs = (__vector128 *) page;
628
629 if (is_boot_cpu)
630 vxrs = boot_cpu_vector_save_area;
631 else
632 __pcpu_sigp_relax(addr, SIGP_STORE_ADDITIONAL_STATUS, page);
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100633 save_area_add_vxrs(sa, vxrs);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100634}
635
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100636static __init void smp_save_cpu_regs(struct save_area *sa, u16 addr,
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100637 bool is_boot_cpu, unsigned long page)
638{
639 void *regs = (void *) page;
640
641 if (is_boot_cpu)
642 copy_oldmem_kernel(regs, (void *) __LC_FPREGS_SAVE_AREA, 512);
643 else
644 __pcpu_sigp_relax(addr, SIGP_STORE_STATUS_AT_ADDRESS, page);
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100645 save_area_add_regs(sa, regs);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100646}
647
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200648void __init smp_save_dump_cpus(void)
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100649{
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100650 int addr, boot_cpu_addr, max_cpu_addr;
651 struct save_area *sa;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100652 unsigned long page;
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200653 bool is_boot_cpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100654
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100655 if (!(OLDMEM_BASE || ipl_info.type == IPL_TYPE_FCP_DUMP))
656 /* No previous system present, normal boot. */
657 return;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100658 /* Allocate a page as dumping area for the store status sigps */
Mike Rapoport0ba9e6e2019-03-11 23:29:35 -0700659 page = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0, 1UL << 31);
660 if (!page)
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700661 panic("ERROR: Failed to allocate %lx bytes below %lx\n",
Mike Rapoport0ba9e6e2019-03-11 23:29:35 -0700662 PAGE_SIZE, 1UL << 31);
663
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100664 /* Set multi-threading state to the previous system. */
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200665 pcpu_set_smt(sclp.mtid_prev);
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200666 boot_cpu_addr = stap();
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100667 max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
668 for (addr = 0; addr <= max_cpu_addr; addr++) {
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100669 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0) ==
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200670 SIGP_CC_NOT_OPERATIONAL)
671 continue;
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200672 is_boot_cpu = (addr == boot_cpu_addr);
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100673 /* Allocate save area */
674 sa = save_area_alloc(is_boot_cpu);
675 if (!sa)
676 panic("could not allocate memory for save area\n");
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100677 if (MACHINE_HAS_VX)
678 /* Get the vector registers */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100679 smp_save_cpu_vxrs(sa, addr, is_boot_cpu, page);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100680 /*
681 * For a zfcp dump OLDMEM_BASE == NULL and the registers
682 * of the boot CPU are stored in the HSA. To retrieve
683 * these registers an SCLP request is required which is
684 * done by drivers/s390/char/zcore.c:init_cpu_info()
685 */
686 if (!is_boot_cpu || OLDMEM_BASE)
687 /* Get the CPU registers */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100688 smp_save_cpu_regs(sa, addr, is_boot_cpu, page);
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200689 }
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100690 memblock_free(page, PAGE_SIZE);
Gerald Schaefera80313f2019-02-03 21:37:20 +0100691 diag_dma_ops.diag308_reset();
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200692 pcpu_set_smt(0);
Heiko Carstens1af135a2015-06-26 12:10:49 +0200693}
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100694#endif /* CONFIG_CRASH_DUMP */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400695
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200696void smp_cpu_set_polarization(int cpu, int val)
697{
698 pcpu_devices[cpu].polarization = val;
699}
700
701int smp_cpu_get_polarization(int cpu)
702{
703 return pcpu_devices[cpu].polarization;
704}
705
Alexander Gordeev42d211a2020-03-11 14:18:05 +0100706int smp_cpu_get_cpu_address(int cpu)
707{
708 return pcpu_devices[cpu].address;
709}
710
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100711static void __ref smp_get_core_info(struct sclp_core_info *info, int early)
Heiko Carstens08d07962008-01-26 14:10:56 +0100712{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400713 static int use_sigp_detection;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400714 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100715
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100716 if (use_sigp_detection || sclp_get_core_info(info, early)) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400717 use_sigp_detection = 1;
Martin Schwidefskye7086eb2015-06-17 10:45:31 +0200718 for (address = 0;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200719 address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100720 address += (1U << smp_cpu_mt_shift)) {
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100721 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0) ==
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200722 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400723 continue;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200724 info->core[info->configured].core_id =
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100725 address >> smp_cpu_mt_shift;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400726 info->configured++;
727 }
728 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100729 }
Heiko Carstens08d07962008-01-26 14:10:56 +0100730}
731
Paul Gortmakere2741f12013-06-18 17:04:52 -0400732static int smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100733
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100734static int smp_add_core(struct sclp_core_entry *core, cpumask_t *avail,
735 bool configured, bool early)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400736{
737 struct pcpu *pcpu;
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100738 int cpu, nr, i;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100739 u16 address;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400740
741 nr = 0;
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100742 if (sclp.has_core_type && core->type != boot_core_type)
743 return nr;
744 cpu = cpumask_first(avail);
745 address = core->core_id << smp_cpu_mt_shift;
746 for (i = 0; (i <= smp_cpu_mtid) && (cpu < nr_cpu_ids); i++) {
747 if (pcpu_find_address(cpu_present_mask, address + i))
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400748 continue;
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100749 pcpu = pcpu_devices + cpu;
750 pcpu->address = address + i;
751 if (configured)
752 pcpu->state = CPU_STATE_CONFIGURED;
753 else
754 pcpu->state = CPU_STATE_STANDBY;
755 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
756 set_cpu_present(cpu, true);
757 if (!early && smp_add_present_cpu(cpu) != 0)
758 set_cpu_present(cpu, false);
759 else
760 nr++;
761 cpumask_clear_cpu(cpu, avail);
762 cpu = cpumask_next(cpu, avail);
763 }
764 return nr;
765}
766
767static int __smp_rescan_cpus(struct sclp_core_info *info, bool early)
768{
769 struct sclp_core_entry *core;
770 cpumask_t avail;
771 bool configured;
772 u16 core_id;
773 int nr, i;
774
775 nr = 0;
776 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
777 /*
778 * Add IPL core first (which got logical CPU number 0) to make sure
779 * that all SMT threads get subsequent logical CPU numbers.
780 */
781 if (early) {
782 core_id = pcpu_devices[0].address >> smp_cpu_mt_shift;
783 for (i = 0; i < info->configured; i++) {
784 core = &info->core[i];
785 if (core->core_id == core_id) {
786 nr += smp_add_core(core, &avail, true, early);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100787 break;
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100788 }
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100789 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400790 }
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100791 for (i = 0; i < info->combined; i++) {
792 configured = i < info->configured;
793 nr += smp_add_core(&info->core[i], &avail, configured, early);
794 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400795 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100798void __init smp_detect_cpus(void)
Heiko Carstens48483b32008-01-26 14:11:05 +0100799{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100800 unsigned int cpu, mtid, c_cpus, s_cpus;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200801 struct sclp_core_info *info;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100802 u16 address;
Heiko Carstens48483b32008-01-26 14:11:05 +0100803
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100804 /* Get CPU information */
Mike Rapoporteb31d552018-10-30 15:08:04 -0700805 info = memblock_alloc(sizeof(*info), 8);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700806 if (!info)
807 panic("%s: Failed to allocate %zu bytes align=0x%x\n",
808 __func__, sizeof(*info), 8);
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100809 smp_get_core_info(info, 1);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100810 /* Find boot CPU type */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200811 if (sclp.has_core_type) {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100812 address = stap();
813 for (cpu = 0; cpu < info->combined; cpu++)
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200814 if (info->core[cpu].core_id == address) {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100815 /* The boot cpu dictates the cpu type. */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200816 boot_core_type = info->core[cpu].type;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100817 break;
818 }
819 if (cpu >= info->combined)
820 panic("Could not find boot CPU type");
Heiko Carstens48483b32008-01-26 14:11:05 +0100821 }
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100822
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100823 /* Set multi-threading state for the current system */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200824 mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100825 mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
826 pcpu_set_smt(mtid);
827
828 /* Print number of CPUs */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400829 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100830 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200831 if (sclp.has_core_type &&
832 info->core[cpu].type != boot_core_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100833 continue;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100834 if (cpu < info->configured)
835 c_cpus += smp_cpu_mtid + 1;
836 else
837 s_cpus += smp_cpu_mtid + 1;
Heiko Carstens48483b32008-01-26 14:11:05 +0100838 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100839 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100840
841 /* Add CPUs present at boot */
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100842 get_online_cpus();
Heiko Carstens72a81ad2019-11-17 14:55:38 +0100843 __smp_rescan_cpus(info, true);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100844 put_online_cpus();
Heiko Carstensaf51160e2016-12-03 09:48:01 +0100845 memblock_free_early((unsigned long)info, sizeof(*info));
Heiko Carstens48483b32008-01-26 14:11:05 +0100846}
847
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200848static void smp_init_secondary(void)
849{
850 int cpu = smp_processor_id();
851
Vasily Gorbik9e8df6d2017-11-20 12:13:52 +0100852 S390_lowcore.last_update_clock = get_tod_clock();
853 restore_access_regs(S390_lowcore.access_regs_save_area);
Heiko Carstensa2308c12019-11-18 13:09:52 +0100854 set_cpu_flag(CIF_ASCE_PRIMARY);
855 set_cpu_flag(CIF_ASCE_SECONDARY);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200856 cpu_init();
857 preempt_disable();
858 init_cpu_timer();
859 vtime_init();
860 pfault_init();
Alexander Gordeev52aeda72020-03-12 11:32:23 +0100861 notify_cpu_starting(cpu);
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200862 if (topology_cpu_dedicated(cpu))
863 set_cpu_flag(CIF_DEDICATED_CPU);
864 else
865 clear_cpu_flag(CIF_DEDICATED_CPU);
Alexander Gordeev52aeda72020-03-12 11:32:23 +0100866 set_cpu_online(cpu, true);
867 update_cpu_masks();
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200868 inc_irq_stat(CPU_RST);
869 local_irq_enable();
870 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200874 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Vasily Gorbik9e8df6d2017-11-20 12:13:52 +0100876static void __no_sanitize_address smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400878 S390_lowcore.restart_stack = (unsigned long) restart_stack;
879 S390_lowcore.restart_fn = (unsigned long) do_restart;
880 S390_lowcore.restart_data = 0;
881 S390_lowcore.restart_source = -1UL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400882 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
Martin Schwidefskye258d712013-09-24 09:14:56 +0200883 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Vasily Gorbik7bcaad12019-11-22 13:12:57 +0100884 CALL_ON_STACK_NORETURN(smp_init_secondary, S390_lowcore.kernel_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/* Upping and downing of CPUs */
Paul Gortmakere2741f12013-06-18 17:04:52 -0400888int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400890 struct pcpu *pcpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100891 int base, i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400893 pcpu = pcpu_devices + cpu;
894 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100895 return -EIO;
Heiko Carstens54231452016-12-05 21:18:58 +0100896 base = smp_get_base_cpu(cpu);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100897 for (i = 0; i <= smp_cpu_mtid; i++) {
898 if (base + i < nr_cpu_ids)
899 if (cpu_online(base + i))
900 break;
901 }
902 /*
903 * If this is the first CPU of the core to get online
904 * do an initial CPU reset.
905 */
906 if (i > smp_cpu_mtid &&
907 pcpu_sigp_retry(pcpu_devices + base, SIGP_INITIAL_CPU_RESET, 0) !=
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200908 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400909 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000910
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400911 rc = pcpu_alloc_lowcore(pcpu, cpu);
912 if (rc)
913 return rc;
914 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000915 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400916 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Heiko Carstensa1307bb2015-03-30 12:51:42 +0200917 /* Wait until cpu puts itself in the online & active maps */
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +0100918 while (!cpu_online(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 cpu_relax();
920 return 0;
921}
922
Heiko Carstensd80512f2013-12-16 14:31:26 +0100923static unsigned int setup_possible_cpus __initdata;
Heiko Carstens48483b32008-01-26 14:11:05 +0100924
Heiko Carstensd80512f2013-12-16 14:31:26 +0100925static int __init _setup_possible_cpus(char *s)
926{
927 get_option(&s, &setup_possible_cpus);
Heiko Carstens37a33022006-02-17 13:52:47 -0800928 return 0;
929}
Heiko Carstensd80512f2013-12-16 14:31:26 +0100930early_param("possible_cpus", _setup_possible_cpus);
Heiko Carstens37a33022006-02-17 13:52:47 -0800931
Heiko Carstens39ce0102007-04-27 16:02:00 +0200932int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400934 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200936 /* Handle possible pending IPIs */
937 smp_handle_ext_call();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400938 set_cpu_online(smp_processor_id(), false);
Alexander Gordeev52aeda72020-03-12 11:32:23 +0100939 update_cpu_masks();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400940 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100941 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400942 /* Disable interrupt sources via control register. */
943 __ctl_store(cregs, 0, 15);
944 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
945 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
946 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
947 __ctl_load(cregs, 0, 15);
Martin Schwidefskyfe0f4972014-09-30 17:37:52 +0200948 clear_cpu_flag(CIF_NOHZ_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950}
951
Heiko Carstens39ce0102007-04-27 16:02:00 +0200952void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400954 struct pcpu *pcpu;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400957 pcpu = pcpu_devices + cpu;
958 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400960 pcpu_free_lowcore(pcpu);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200961 cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200962 cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Heiko Carstensb456d942011-05-23 10:24:33 +0200965void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 idle_task_exit();
Martin Schwidefskyd768bd82018-01-16 07:11:45 +0100968 __bpon();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200969 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400970 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Heiko Carstensd80512f2013-12-16 14:31:26 +0100973void __init smp_fill_possible_mask(void)
974{
David Hildenbrand9747bc42015-05-06 09:29:53 +0200975 unsigned int possible, sclp_max, cpu;
Heiko Carstensd80512f2013-12-16 14:31:26 +0100976
David Hildenbrand3a9f3fe2015-05-06 13:19:29 +0200977 sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
978 sclp_max = min(smp_max_threads, sclp_max);
Dan Carpenter61282af2016-07-18 09:02:56 +0200979 sclp_max = (sclp.max_cores * sclp_max) ?: nr_cpu_ids;
Heiko Carstenscf813db2014-03-10 14:50:16 +0100980 possible = setup_possible_cpus ?: nr_cpu_ids;
David Hildenbrand9747bc42015-05-06 09:29:53 +0200981 possible = min(possible, sclp_max);
Heiko Carstensd80512f2013-12-16 14:31:26 +0100982 for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
983 set_cpu_possible(cpu, true);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986void __init smp_prepare_cpus(unsigned int max_cpus)
987{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200988 /* request the 0x1201 emergency signal external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200989 if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
Heiko Carstens39ce0102007-04-27 16:02:00 +0200990 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100991 /* request the 0x1202 external call external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200992 if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100993 panic("Couldn't request external interrupt 0x1202");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200996void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400998 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Heiko Carstens0861b5a2017-03-16 11:02:36 +01001000 WARN_ON(!cpu_present(0) || !cpu_online(0));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001001 pcpu->state = CPU_STATE_CONFIGURED;
Heiko Carstensc667aea2015-12-31 10:29:00 +01001002 pcpu->lowcore = (struct lowcore *)(unsigned long) store_prefix();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001003 S390_lowcore.percpu_offset = __per_cpu_offset[0];
Heiko Carstens50ab9a92012-09-04 17:36:16 +02001004 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Heiko Carstensea1f4ee2007-05-31 17:38:05 +02001007void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Heiko Carstens02beacc2010-01-13 20:44:34 +01001011void __init smp_setup_processor_id(void)
1012{
Heiko Carstens0861b5a2017-03-16 11:02:36 +01001013 pcpu_devices[0].address = stap();
Heiko Carstens02beacc2010-01-13 20:44:34 +01001014 S390_lowcore.cpu_nr = 0;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +02001015 S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
Martin Schwidefskyb96f7d82017-03-24 17:25:02 +01001016 S390_lowcore.spinlock_index = 0;
Heiko Carstens02beacc2010-01-13 20:44:34 +01001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * the frequency of the profiling timer can be changed
1021 * by writing a multiplier value into /proc/profile.
1022 *
1023 * usually you want to run this on all CPUs ;)
1024 */
1025int setup_profiling_timer(unsigned int multiplier)
1026{
Heiko Carstens39ce0102007-04-27 16:02:00 +02001027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001030static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001031 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +01001032{
1033 ssize_t count;
1034
1035 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001036 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +01001037 mutex_unlock(&smp_cpu_state_mutex);
1038 return count;
1039}
1040
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001041static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001042 struct device_attribute *attr,
1043 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +01001044{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001045 struct pcpu *pcpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001046 int cpu, val, rc, i;
Heiko Carstens08d07962008-01-26 14:10:56 +01001047 char delim;
1048
1049 if (sscanf(buf, "%d %c", &val, &delim) != 1)
1050 return -EINVAL;
1051 if (val != 0 && val != 1)
1052 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001053 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001054 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +01001055 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +01001056 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001057 cpu = dev->id;
Heiko Carstens54231452016-12-05 21:18:58 +01001058 cpu = smp_get_base_cpu(cpu);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001059 if (cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +01001060 goto out;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001061 for (i = 0; i <= smp_cpu_mtid; i++)
1062 if (cpu_online(cpu + i))
1063 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001064 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +01001065 rc = 0;
1066 switch (val) {
1067 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001068 if (pcpu->state != CPU_STATE_CONFIGURED)
1069 break;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001070 rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001071 if (rc)
1072 break;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001073 for (i = 0; i <= smp_cpu_mtid; i++) {
1074 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1075 continue;
1076 pcpu[i].state = CPU_STATE_STANDBY;
1077 smp_cpu_set_polarization(cpu + i,
1078 POLARIZATION_UNKNOWN);
1079 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001080 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +01001081 break;
1082 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001083 if (pcpu->state != CPU_STATE_STANDBY)
1084 break;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001085 rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001086 if (rc)
1087 break;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001088 for (i = 0; i <= smp_cpu_mtid; i++) {
1089 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1090 continue;
1091 pcpu[i].state = CPU_STATE_CONFIGURED;
1092 smp_cpu_set_polarization(cpu + i,
1093 POLARIZATION_UNKNOWN);
1094 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001095 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +01001096 break;
1097 default:
1098 break;
1099 }
1100out:
Heiko Carstens08d07962008-01-26 14:10:56 +01001101 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001102 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001103 return rc ? rc : count;
1104}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001105static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001106
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001107static ssize_t show_cpu_address(struct device *dev,
1108 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +01001109{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001110 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +01001111}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001112static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +01001113
Heiko Carstens08d07962008-01-26 14:10:56 +01001114static struct attribute *cpu_common_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001115 &dev_attr_configure.attr,
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001116 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +01001117 NULL,
1118};
1119
1120static struct attribute_group cpu_common_attr_group = {
1121 .attrs = cpu_common_attrs,
1122};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Heiko Carstens08d07962008-01-26 14:10:56 +01001124static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001125 &dev_attr_idle_count.attr,
1126 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001127 NULL,
1128};
1129
Heiko Carstens08d07962008-01-26 14:10:56 +01001130static struct attribute_group cpu_online_attr_group = {
1131 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001132};
1133
Sebastian Andrzej Siewiordfbbd862016-11-04 15:45:03 +01001134static int smp_cpu_online(unsigned int cpu)
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001135{
Heiko Carstens2f859d02015-02-11 12:31:03 +01001136 struct device *s = &per_cpu(cpu_device, cpu)->dev;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001137
Sebastian Andrzej Siewiordfbbd862016-11-04 15:45:03 +01001138 return sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1139}
1140static int smp_cpu_pre_down(unsigned int cpu)
1141{
1142 struct device *s = &per_cpu(cpu_device, cpu)->dev;
1143
1144 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1145 return 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001146}
1147
Paul Gortmakere2741f12013-06-18 17:04:52 -04001148static int smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +01001149{
Heiko Carstens96619fc2013-12-05 12:42:09 +01001150 struct device *s;
1151 struct cpu *c;
Heiko Carstens08d07962008-01-26 14:10:56 +01001152 int rc;
1153
Heiko Carstens96619fc2013-12-05 12:42:09 +01001154 c = kzalloc(sizeof(*c), GFP_KERNEL);
1155 if (!c)
1156 return -ENOMEM;
Heiko Carstens2f859d02015-02-11 12:31:03 +01001157 per_cpu(cpu_device, cpu) = c;
Heiko Carstens96619fc2013-12-05 12:42:09 +01001158 s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +01001159 c->hotpluggable = 1;
1160 rc = register_cpu(c, cpu);
1161 if (rc)
1162 goto out;
1163 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1164 if (rc)
1165 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +01001166 rc = topology_cpu_init(c);
1167 if (rc)
1168 goto out_topology;
1169 return 0;
1170
1171out_topology:
Heiko Carstens08d07962008-01-26 14:10:56 +01001172 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1173out_cpu:
Heiko Carstens08d07962008-01-26 14:10:56 +01001174 unregister_cpu(c);
Heiko Carstens08d07962008-01-26 14:10:56 +01001175out:
1176 return rc;
1177}
1178
Heiko Carstens67060d92008-05-30 10:03:27 +02001179int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001180{
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001181 struct sclp_core_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001182 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001183
Heiko Carstensaf51160e2016-12-03 09:48:01 +01001184 info = kzalloc(sizeof(*info), GFP_KERNEL);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001185 if (!info)
1186 return -ENOMEM;
Heiko Carstensaf51160e2016-12-03 09:48:01 +01001187 smp_get_core_info(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001188 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001189 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens72a81ad2019-11-17 14:55:38 +01001190 nr = __smp_rescan_cpus(info, false);
Heiko Carstens08d07962008-01-26 14:10:56 +01001191 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001192 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001193 kfree(info);
1194 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001195 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001196 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001197}
1198
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001199static ssize_t __ref rescan_store(struct device *dev,
1200 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001201 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001202 size_t count)
1203{
1204 int rc;
1205
Gerald Schaeferb7cb7072019-01-09 13:00:03 +01001206 rc = lock_device_hotplug_sysfs();
1207 if (rc)
1208 return rc;
Heiko Carstens1e489512008-04-30 13:38:37 +02001209 rc = smp_rescan_cpus();
Gerald Schaeferb7cb7072019-01-09 13:00:03 +01001210 unlock_device_hotplug();
Heiko Carstens08d07962008-01-26 14:10:56 +01001211 return rc ? rc : count;
1212}
Joe Perches6cbaefb2017-12-19 10:15:09 -08001213static DEVICE_ATTR_WO(rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001214
Heiko Carstens83a24e32011-12-27 11:27:09 +01001215static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001216{
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301217 int cpu, rc = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001218
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001219 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001220 if (rc)
1221 return rc;
Heiko Carstens08d07962008-01-26 14:10:56 +01001222 for_each_present_cpu(cpu) {
1223 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001224 if (rc)
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301227
Sebastian Andrzej Siewiordfbbd862016-11-04 15:45:03 +01001228 rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "s390/smp:online",
1229 smp_cpu_online, smp_cpu_pre_down);
Heiko Carstense1108e82017-08-14 07:54:32 +02001230 rc = rc <= 0 ? rc : 0;
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301231out:
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301232 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001234subsys_initcall(s390_smp_init);