blob: 6f54c175f5c9012b5cc00f28d3d793c2bfc56ec5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040025#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/spinlock.h>
27#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
Christian Borntraeger3324e602009-03-26 15:23:56 +010030#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010033#include <linux/crash_dump.h>
Michael Holzheu1592a8e2015-05-26 19:05:23 +020034#include <linux/memblock.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010035#include <asm/asm-offsets.h>
Heiko Carstens1e3cab22012-03-30 09:40:55 +020036#include <asm/switch_to.h>
37#include <asm/facility.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010038#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010039#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/tlbflush.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020042#include <asm/vtimer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020043#include <asm/lowcore.h>
Heiko Carstens08d07962008-01-26 14:10:56 +010044#include <asm/sclp.h>
Martin Schwidefskyc742b312008-12-31 15:11:42 +010045#include <asm/vdso.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040046#include <asm/debug.h>
Michael Holzheu4857d4b2012-03-11 11:59:34 -040047#include <asm/os_info.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020048#include <asm/sigp.h>
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020049#include <asm/idle.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020050#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040052enum {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040053 ec_schedule = 0,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040054 ec_call_function_single,
55 ec_stop_cpu,
56};
Heiko Carstens08d07962008-01-26 14:10:56 +010057
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040058enum {
Heiko Carstens08d07962008-01-26 14:10:56 +010059 CPU_STATE_STANDBY,
60 CPU_STATE_CONFIGURED,
61};
62
Heiko Carstens2f859d02015-02-11 12:31:03 +010063static DEFINE_PER_CPU(struct cpu *, cpu_device);
64
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040065struct pcpu {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040066 struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040067 unsigned long ec_mask; /* bit mask for ec_xxx functions */
Heiko Carstens2f859d02015-02-11 12:31:03 +010068 signed char state; /* physical cpu state */
69 signed char polarization; /* physical polarization */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040070 u16 address; /* physical cpu address */
71};
72
Martin Schwidefskyd08d9432015-06-18 14:23:00 +020073static u8 boot_core_type;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040074static struct pcpu pcpu_devices[NR_CPUS];
75
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010076unsigned int smp_cpu_mt_shift;
77EXPORT_SYMBOL(smp_cpu_mt_shift);
78
79unsigned int smp_cpu_mtid;
80EXPORT_SYMBOL(smp_cpu_mtid);
81
82static unsigned int smp_max_threads __initdata = -1U;
83
84static int __init early_nosmt(char *s)
85{
86 smp_max_threads = 1;
87 return 0;
88}
89early_param("nosmt", early_nosmt);
90
91static int __init early_smt(char *s)
92{
93 get_option(&s, &smp_max_threads);
94 return 0;
95}
96early_param("smt", early_smt);
97
Heiko Carstens50ab9a92012-09-04 17:36:16 +020098/*
99 * The smp_cpu_state_mutex must be held when changing the state or polarization
100 * member of a pcpu data structure within the pcpu_devices arreay.
101 */
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200102DEFINE_MUTEX(smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100103
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400104/*
105 * Signal processor helper functions.
106 */
Michael Holzheua62bc072014-10-06 17:57:43 +0200107static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm,
108 u32 *status)
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100109{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400110 int cc;
111
112 while (1) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200113 cc = __pcpu_sigp(addr, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200114 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400115 return cc;
116 cpu_relax();
117 }
118}
119
120static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
121{
122 int cc, retry;
123
124 for (retry = 0; ; retry++) {
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200125 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200126 if (cc != SIGP_CC_BUSY)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400127 break;
128 if (retry >= 3)
129 udelay(10);
130 }
131 return cc;
132}
133
134static inline int pcpu_stopped(struct pcpu *pcpu)
135{
Heiko Carstens41459d36cf2012-09-14 11:09:52 +0200136 u32 uninitialized_var(status);
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200137
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200138 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200139 0, &status) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400140 return 0;
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200141 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400142}
143
144static inline int pcpu_running(struct pcpu *pcpu)
145{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200146 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
Heiko Carstensc5e3acd2012-08-30 14:24:42 +0200147 0, NULL) != SIGP_CC_STATUS_STORED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400148 return 1;
Heiko Carstens524b24a2012-06-04 12:11:41 +0200149 /* Status stored condition code is equivalent to cpu not running. */
150 return 0;
Heiko Carstensa93b8ec2010-02-26 22:37:35 +0100151}
152
Michael Holzheu1943f532011-10-30 15:16:38 +0100153/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400154 * Find struct pcpu by cpu address.
Michael Holzheu1943f532011-10-30 15:16:38 +0100155 */
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100156static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
Michael Holzheu1943f532011-10-30 15:16:38 +0100157{
158 int cpu;
159
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400160 for_each_cpu(cpu, mask)
161 if (pcpu_devices[cpu].address == address)
162 return pcpu_devices + cpu;
163 return NULL;
164}
165
166static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
167{
168 int order;
169
Heiko Carstensdea24192013-12-03 10:06:29 +0100170 if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
171 return;
172 order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400173 pcpu_sigp_retry(pcpu, order, 0);
174}
175
Heiko Carstens2f859d02015-02-11 12:31:03 +0100176#define ASYNC_FRAME_OFFSET (ASYNC_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
177#define PANIC_FRAME_OFFSET (PAGE_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
178
Paul Gortmakere2741f12013-06-18 17:04:52 -0400179static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400180{
Heiko Carstens2f859d02015-02-11 12:31:03 +0100181 unsigned long async_stack, panic_stack;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400182 struct _lowcore *lc;
183
184 if (pcpu != &pcpu_devices[0]) {
185 pcpu->lowcore = (struct _lowcore *)
186 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100187 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
188 panic_stack = __get_free_page(GFP_KERNEL);
189 if (!pcpu->lowcore || !panic_stack || !async_stack)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400190 goto out;
Heiko Carstens2f859d02015-02-11 12:31:03 +0100191 } else {
192 async_stack = pcpu->lowcore->async_stack - ASYNC_FRAME_OFFSET;
193 panic_stack = pcpu->lowcore->panic_stack - PANIC_FRAME_OFFSET;
Michael Holzheu1943f532011-10-30 15:16:38 +0100194 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400195 lc = pcpu->lowcore;
196 memcpy(lc, &S390_lowcore, 512);
197 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100198 lc->async_stack = async_stack + ASYNC_FRAME_OFFSET;
199 lc->panic_stack = panic_stack + PANIC_FRAME_OFFSET;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400200 lc->cpu_nr = cpu;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200201 lc->spinlock_lockval = arch_spin_lockval(cpu);
Martin Schwidefsky80703612014-10-06 17:53:53 +0200202 if (MACHINE_HAS_VX)
203 lc->vector_save_area_addr =
204 (unsigned long) &lc->vector_save_area;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400205 if (vdso_alloc_per_cpu(lc))
206 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400207 lowcore_ptr[cpu] = lc;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200208 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400209 return 0;
210out:
211 if (pcpu != &pcpu_devices[0]) {
Heiko Carstens2f859d02015-02-11 12:31:03 +0100212 free_page(panic_stack);
213 free_pages(async_stack, ASYNC_ORDER);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400214 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
215 }
216 return -ENOMEM;
Michael Holzheu1943f532011-10-30 15:16:38 +0100217}
218
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200219#ifdef CONFIG_HOTPLUG_CPU
220
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400221static void pcpu_free_lowcore(struct pcpu *pcpu)
Heiko Carstens2c2df112010-02-26 22:37:34 +0100222{
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200223 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400224 lowcore_ptr[pcpu - pcpu_devices] = NULL;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400225 vdso_free_per_cpu(pcpu->lowcore);
Heiko Carstens2f859d02015-02-11 12:31:03 +0100226 if (pcpu == &pcpu_devices[0])
227 return;
228 free_page(pcpu->lowcore->panic_stack-PANIC_FRAME_OFFSET);
229 free_pages(pcpu->lowcore->async_stack-ASYNC_FRAME_OFFSET, ASYNC_ORDER);
230 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100231}
232
Heiko Carstens9d0f46a2012-05-09 16:27:35 +0200233#endif /* CONFIG_HOTPLUG_CPU */
234
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400235static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100236{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400237 struct _lowcore *lc = pcpu->lowcore;
238
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200239 if (MACHINE_HAS_TLB_LC)
240 cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
241 cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400242 atomic_inc(&init_mm.context.attach_count);
243 lc->cpu_nr = cpu;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200244 lc->spinlock_lockval = arch_spin_lockval(cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400245 lc->percpu_offset = __per_cpu_offset[cpu];
246 lc->kernel_asce = S390_lowcore.kernel_asce;
247 lc->machine_flags = S390_lowcore.machine_flags;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400248 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
249 __ctl_store(lc->cregs_save_area, 0, 15);
250 save_access_regs((unsigned int *) lc->access_regs_save_area);
251 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
252 MAX_FACILITY_BIT/8);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100253}
254
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400255static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
256{
257 struct _lowcore *lc = pcpu->lowcore;
258 struct thread_info *ti = task_thread_info(tsk);
259
Martin Schwidefskydc7ee002013-04-24 10:20:43 +0200260 lc->kernel_stack = (unsigned long) task_stack_page(tsk)
261 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400262 lc->thread_info = (unsigned long) task_thread_info(tsk);
263 lc->current_task = (unsigned long) tsk;
264 lc->user_timer = ti->user_timer;
265 lc->system_timer = ti->system_timer;
266 lc->steal_timer = 0;
267}
268
269static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
270{
271 struct _lowcore *lc = pcpu->lowcore;
272
273 lc->restart_stack = lc->kernel_stack;
274 lc->restart_fn = (unsigned long) func;
275 lc->restart_data = (unsigned long) data;
276 lc->restart_source = -1UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200277 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400278}
279
280/*
281 * Call function via PSW restart on pcpu and stop the current cpu.
282 */
283static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
284 void *data, unsigned long stack)
285{
Michael Holzheu061da3d2012-05-24 14:38:26 +0200286 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
Heiko Carstensfbe76562012-06-05 09:59:52 +0200287 unsigned long source_cpu = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400288
Martin Schwidefskye258d712013-09-24 09:14:56 +0200289 __load_psw_mask(PSW_KERNEL_BITS);
Heiko Carstensfbe76562012-06-05 09:59:52 +0200290 if (pcpu->address == source_cpu)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400291 func(data); /* should not return */
292 /* Stop target cpu (if func returns this stops the current cpu). */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200293 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400294 /* Restart func on the target cpu and stop the current cpu. */
Heiko Carstensfbe76562012-06-05 09:59:52 +0200295 mem_assign_absolute(lc->restart_stack, stack);
296 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
297 mem_assign_absolute(lc->restart_data, (unsigned long) data);
298 mem_assign_absolute(lc->restart_source, source_cpu);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400299 asm volatile(
Heiko Carstenseb546192012-06-04 15:05:43 +0200300 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400301 " brc 2,0b # busy, try again\n"
Heiko Carstenseb546192012-06-04 15:05:43 +0200302 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400303 " brc 2,1b # busy, try again\n"
Heiko Carstensfbe76562012-06-05 09:59:52 +0200304 : : "d" (pcpu->address), "d" (source_cpu),
Heiko Carstenseb546192012-06-04 15:05:43 +0200305 "K" (SIGP_RESTART), "K" (SIGP_STOP)
306 : "0", "1", "cc");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400307 for (;;) ;
308}
309
310/*
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100311 * Enable additional logical cpus for multi-threading.
312 */
313static int pcpu_set_smt(unsigned int mtid)
314{
315 register unsigned long reg1 asm ("1") = (unsigned long) mtid;
316 int cc;
317
318 if (smp_cpu_mtid == mtid)
319 return 0;
320 asm volatile(
321 " sigp %1,0,%2 # sigp set multi-threading\n"
322 " ipm %0\n"
323 " srl %0,28\n"
324 : "=d" (cc) : "d" (reg1), "K" (SIGP_SET_MULTI_THREADING)
325 : "cc");
326 if (cc == 0) {
327 smp_cpu_mtid = mtid;
328 smp_cpu_mt_shift = 0;
329 while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
330 smp_cpu_mt_shift++;
331 pcpu_devices[0].address = stap();
332 }
333 return cc;
334}
335
336/*
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400337 * Call function on an online CPU.
338 */
339void smp_call_online_cpu(void (*func)(void *), void *data)
340{
341 struct pcpu *pcpu;
342
343 /* Use the current cpu if it is online. */
344 pcpu = pcpu_find_address(cpu_online_mask, stap());
345 if (!pcpu)
346 /* Use the first online cpu. */
347 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
348 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
349}
350
351/*
352 * Call function on the ipl CPU.
353 */
354void smp_call_ipl_cpu(void (*func)(void *), void *data)
355{
Michael Holzheuc6da39f2012-03-13 11:25:08 -0400356 pcpu_delegate(&pcpu_devices[0], func, data,
Heiko Carstens2f859d02015-02-11 12:31:03 +0100357 pcpu_devices->lowcore->panic_stack -
358 PANIC_FRAME_OFFSET + PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400359}
360
361int smp_find_processor_id(u16 address)
362{
363 int cpu;
364
365 for_each_present_cpu(cpu)
366 if (pcpu_devices[cpu].address == address)
367 return cpu;
368 return -1;
369}
370
371int smp_vcpu_scheduled(int cpu)
372{
373 return pcpu_running(pcpu_devices + cpu);
374}
375
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400376void smp_yield_cpu(int cpu)
377{
378 if (MACHINE_HAS_DIAG9C)
379 asm volatile("diag %0,0,0x9c"
380 : : "d" (pcpu_devices[cpu].address));
381 else if (MACHINE_HAS_DIAG44)
382 asm volatile("diag 0,0,0x44");
383}
384
385/*
386 * Send cpus emergency shutdown signal. This gives the cpus the
387 * opportunity to complete outstanding interrupts.
388 */
Heiko Carstens63df41d62013-09-06 19:10:48 +0200389static void smp_emergency_stop(cpumask_t *cpumask)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400390{
391 u64 end;
392 int cpu;
393
Heiko Carstens1aae0562013-01-30 09:49:40 +0100394 end = get_tod_clock() + (1000000UL << 12);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400395 for_each_cpu(cpu, cpumask) {
396 struct pcpu *pcpu = pcpu_devices + cpu;
397 set_bit(ec_stop_cpu, &pcpu->ec_mask);
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200398 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
399 0, NULL) == SIGP_CC_BUSY &&
Heiko Carstens1aae0562013-01-30 09:49:40 +0100400 get_tod_clock() < end)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400401 cpu_relax();
402 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100403 while (get_tod_clock() < end) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400404 for_each_cpu(cpu, cpumask)
405 if (pcpu_stopped(pcpu_devices + cpu))
406 cpumask_clear_cpu(cpu, cpumask);
407 if (cpumask_empty(cpumask))
408 break;
409 cpu_relax();
410 }
411}
412
413/*
414 * Stop all cpus but the current one.
415 */
Heiko Carstens677d7622007-11-20 11:13:37 +0100416void smp_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100418 cpumask_t cpumask;
419 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Heiko Carstens677d7622007-11-20 11:13:37 +0100421 /* Disable all interrupts/machine checks */
Martin Schwidefskye258d712013-09-24 09:14:56 +0200422 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Christian Borntraeger3324e602009-03-26 15:23:56 +0100423 trace_hardirqs_off();
Heiko Carstens677d7622007-11-20 11:13:37 +0100424
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400425 debug_set_critical();
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100426 cpumask_copy(&cpumask, cpu_online_mask);
427 cpumask_clear_cpu(smp_processor_id(), &cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400429 if (oops_in_progress)
430 smp_emergency_stop(&cpumask);
Martin Schwidefsky85ac7ca2011-12-27 11:27:22 +0100431
432 /* stop all processors */
433 for_each_cpu(cpu, &cpumask) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400434 struct pcpu *pcpu = pcpu_devices + cpu;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200435 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400436 while (!pcpu_stopped(pcpu))
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100437 cpu_relax();
438 }
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * This is the main routine where commands issued by other
443 * cpus are handled.
444 */
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200445static void smp_handle_ext_call(void)
446{
447 unsigned long bits;
448
449 /* handle bit signal external calls */
450 bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
451 if (test_bit(ec_stop_cpu, &bits))
452 smp_stop_cpu();
453 if (test_bit(ec_schedule, &bits))
454 scheduler_ipi();
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200455 if (test_bit(ec_call_function_single, &bits))
456 generic_smp_call_function_single_interrupt();
457}
458
Heiko Carstensfde15c32012-03-11 11:59:31 -0400459static void do_ext_call_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200460 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200462 inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
463 smp_handle_ext_call();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Rusty Russell630cd042009-09-24 09:34:45 -0600466void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Heiko Carstensca9fc752008-12-25 13:38:39 +0100467{
468 int cpu;
469
Rusty Russell630cd042009-09-24 09:34:45 -0600470 for_each_cpu(cpu, mask)
Heiko Carstensb6ed49e2013-05-21 15:34:56 +0200471 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100472}
473
474void arch_send_call_function_single_ipi(int cpu)
475{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400476 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
Heiko Carstensca9fc752008-12-25 13:38:39 +0100477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
480 * this function sends a 'reschedule' IPI to another CPU.
481 * it goes straight through and wastes no time serializing
482 * anything. Worst case is that we lose a reschedule ...
483 */
484void smp_send_reschedule(int cpu)
485{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400486 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * parameter area for the set/clear control bit callbacks
491 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200492struct ec_creg_mask_parms {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400493 unsigned long orval;
494 unsigned long andval;
495 int cr;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200496};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498/*
499 * callback for setting/clearing control bits
500 */
Heiko Carstens39ce0102007-04-27 16:02:00 +0200501static void smp_ctl_bit_callback(void *info)
502{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200503 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long cregs[16];
Heiko Carstens39ce0102007-04-27 16:02:00 +0200505
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200506 __ctl_store(cregs, 0, 15);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400507 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200508 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*
512 * Set a bit in a control register of all cpus
513 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200514void smp_ctl_set_bit(int cr, int bit)
515{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400516 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200518 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200520EXPORT_SYMBOL(smp_ctl_set_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522/*
523 * Clear a bit in a control register of all cpus
524 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200525void smp_ctl_clear_bit(int cr, int bit)
526{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400527 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200529 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
Heiko Carstens39ce0102007-04-27 16:02:00 +0200531EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Michael Holzheubf28a592014-04-14 10:38:05 +0200533#ifdef CONFIG_CRASH_DUMP
Michael Holzheu411ed322007-04-27 16:01:49 +0200534
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200535static void __smp_store_cpu_state(struct save_area_ext *sa_ext, u16 address,
536 int is_boot_cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +0100537{
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200538 void *lc = (void *)(unsigned long) store_prefix();
Michael Holzheua62bc072014-10-06 17:57:43 +0200539 unsigned long vx_sa;
Heiko Carstens08d07962008-01-26 14:10:56 +0100540
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100541 if (is_boot_cpu) {
542 /* Copy the registers of the boot CPU. */
Michael Holzheua62bc072014-10-06 17:57:43 +0200543 copy_oldmem_page(1, (void *) &sa_ext->sa, sizeof(sa_ext->sa),
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400544 SAVE_AREA_BASE - PAGE_SIZE, 0);
Michael Holzheua62bc072014-10-06 17:57:43 +0200545 if (MACHINE_HAS_VX)
546 save_vx_regs_safe(sa_ext->vx_regs);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400547 return;
548 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400549 /* Get the registers of a non-boot cpu. */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200550 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
Michael Holzheua62bc072014-10-06 17:57:43 +0200551 memcpy_real(&sa_ext->sa, lc + SAVE_AREA_BASE, sizeof(sa_ext->sa));
552 if (!MACHINE_HAS_VX)
553 return;
554 /* Get the VX registers */
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200555 vx_sa = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
Michael Holzheua62bc072014-10-06 17:57:43 +0200556 if (!vx_sa)
557 panic("could not allocate memory for VX save area\n");
558 __pcpu_sigp_relax(address, SIGP_STORE_ADDITIONAL_STATUS, vx_sa, NULL);
559 memcpy(sa_ext->vx_regs, (void *) vx_sa, sizeof(sa_ext->vx_regs));
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200560 memblock_free(vx_sa, PAGE_SIZE);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400561}
562
Heiko Carstens1af135a2015-06-26 12:10:49 +0200563int smp_store_status(int cpu)
564{
565 unsigned long vx_sa;
566 struct pcpu *pcpu;
567
568 pcpu = pcpu_devices + cpu;
569 if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
570 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
571 return -EIO;
572 if (!MACHINE_HAS_VX)
573 return 0;
574 vx_sa = __pa(pcpu->lowcore->vector_save_area_addr);
575 __pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
576 vx_sa, NULL);
577 return 0;
578}
579
580#endif /* CONFIG_CRASH_DUMP */
581
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100582/*
583 * Collect CPU state of the previous, crashed system.
584 * There are four cases:
585 * 1) standard zfcp dump
586 * condition: OLDMEM_BASE == NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
587 * The state for all CPUs except the boot CPU needs to be collected
588 * with sigp stop-and-store-status. The boot CPU state is located in
589 * the absolute lowcore of the memory stored in the HSA. The zcore code
590 * will allocate the save area and copy the boot CPU state from the HSA.
591 * 2) stand-alone kdump for SCSI (zfcp dump with swapped memory)
592 * condition: OLDMEM_BASE != NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
593 * The state for all CPUs except the boot CPU needs to be collected
594 * with sigp stop-and-store-status. The firmware or the boot-loader
595 * stored the registers of the boot CPU in the absolute lowcore in the
596 * memory of the old system.
597 * 3) kdump and the old kernel did not store the CPU state,
598 * or stand-alone kdump for DASD
599 * condition: OLDMEM_BASE != NULL && !is_kdump_kernel()
600 * The state for all CPUs except the boot CPU needs to be collected
601 * with sigp stop-and-store-status. The kexec code or the boot-loader
602 * stored the registers of the boot CPU in the memory of the old system.
603 * 4) kdump and the old kernel stored the CPU state
604 * condition: OLDMEM_BASE != NULL && is_kdump_kernel()
605 * The state of all CPUs is stored in ELF sections in the memory of the
606 * old system. The ELF sections are picked up by the crash_dump code
607 * via elfcorehdr_addr.
608 */
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200609void __init smp_save_dump_cpus(void)
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100610{
Heiko Carstens1af135a2015-06-26 12:10:49 +0200611#ifdef CONFIG_CRASH_DUMP
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200612 int addr, cpu, boot_cpu_addr, max_cpu_addr;
613 struct save_area_ext *sa_ext;
614 bool is_boot_cpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100615
616 if (is_kdump_kernel())
617 /* Previous system stored the CPU states. Nothing to do. */
618 return;
619 if (!(OLDMEM_BASE || ipl_info.type == IPL_TYPE_FCP_DUMP))
620 /* No previous system present, normal boot. */
621 return;
622 /* Set multi-threading state to the previous system. */
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200623 pcpu_set_smt(sclp.mtid_prev);
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200624 max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
625 for (cpu = 0, addr = 0; addr <= max_cpu_addr; addr++) {
626 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0, NULL) ==
627 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100628 continue;
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200629 cpu += 1;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100630 }
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200631 dump_save_areas.areas = (void *)memblock_alloc(sizeof(void *) * cpu, 8);
632 dump_save_areas.count = cpu;
633 boot_cpu_addr = stap();
634 for (cpu = 0, addr = 0; addr <= max_cpu_addr; addr++) {
635 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0, NULL) ==
636 SIGP_CC_NOT_OPERATIONAL)
637 continue;
638 sa_ext = (void *) memblock_alloc(sizeof(*sa_ext), 8);
639 dump_save_areas.areas[cpu] = sa_ext;
640 if (!sa_ext)
641 panic("could not allocate memory for save area\n");
642 is_boot_cpu = (addr == boot_cpu_addr);
643 cpu += 1;
644 if (is_boot_cpu && !OLDMEM_BASE)
645 /* Skip boot CPU for standard zfcp dump. */
646 continue;
647 /* Get state for this CPU. */
648 __smp_store_cpu_state(sa_ext, addr, is_boot_cpu);
649 }
650 diag308_reset();
651 pcpu_set_smt(0);
Michael Holzheubf28a592014-04-14 10:38:05 +0200652#endif /* CONFIG_CRASH_DUMP */
Heiko Carstens1af135a2015-06-26 12:10:49 +0200653}
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400654
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200655void smp_cpu_set_polarization(int cpu, int val)
656{
657 pcpu_devices[cpu].polarization = val;
658}
659
660int smp_cpu_get_polarization(int cpu)
661{
662 return pcpu_devices[cpu].polarization;
663}
664
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200665static struct sclp_core_info *smp_get_core_info(void)
Heiko Carstens08d07962008-01-26 14:10:56 +0100666{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400667 static int use_sigp_detection;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200668 struct sclp_core_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400669 int address;
Heiko Carstens08d07962008-01-26 14:10:56 +0100670
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400671 info = kzalloc(sizeof(*info), GFP_KERNEL);
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200672 if (info && (use_sigp_detection || sclp_get_core_info(info))) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400673 use_sigp_detection = 1;
Martin Schwidefskye7086eb2015-06-17 10:45:31 +0200674 for (address = 0;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200675 address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100676 address += (1U << smp_cpu_mt_shift)) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200677 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
678 SIGP_CC_NOT_OPERATIONAL)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400679 continue;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200680 info->core[info->configured].core_id =
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100681 address >> smp_cpu_mt_shift;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400682 info->configured++;
683 }
684 info->combined = info->configured;
Heiko Carstens08d07962008-01-26 14:10:56 +0100685 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400686 return info;
Heiko Carstens08d07962008-01-26 14:10:56 +0100687}
688
Paul Gortmakere2741f12013-06-18 17:04:52 -0400689static int smp_add_present_cpu(int cpu);
Heiko Carstens08d07962008-01-26 14:10:56 +0100690
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200691static int __smp_rescan_cpus(struct sclp_core_info *info, int sysfs_add)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400692{
693 struct pcpu *pcpu;
694 cpumask_t avail;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100695 int cpu, nr, i, j;
696 u16 address;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400697
698 nr = 0;
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200699 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400700 cpu = cpumask_first(&avail);
701 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200702 if (sclp.has_core_type && info->core[i].type != boot_core_type)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400703 continue;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200704 address = info->core[i].core_id << smp_cpu_mt_shift;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100705 for (j = 0; j <= smp_cpu_mtid; j++) {
706 if (pcpu_find_address(cpu_present_mask, address + j))
707 continue;
708 pcpu = pcpu_devices + cpu;
709 pcpu->address = address + j;
710 pcpu->state =
711 (cpu >= info->configured*(smp_cpu_mtid + 1)) ?
712 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
713 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
714 set_cpu_present(cpu, true);
715 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
716 set_cpu_present(cpu, false);
717 else
718 nr++;
719 cpu = cpumask_next(cpu, &avail);
720 if (cpu >= nr_cpu_ids)
721 break;
722 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400723 }
724 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Heiko Carstens48483b32008-01-26 14:11:05 +0100727static void __init smp_detect_cpus(void)
728{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100729 unsigned int cpu, mtid, c_cpus, s_cpus;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200730 struct sclp_core_info *info;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100731 u16 address;
Heiko Carstens48483b32008-01-26 14:11:05 +0100732
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100733 /* Get CPU information */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200734 info = smp_get_core_info();
Heiko Carstens48483b32008-01-26 14:11:05 +0100735 if (!info)
736 panic("smp_detect_cpus failed to allocate memory\n");
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100737
738 /* Find boot CPU type */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200739 if (sclp.has_core_type) {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100740 address = stap();
741 for (cpu = 0; cpu < info->combined; cpu++)
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200742 if (info->core[cpu].core_id == address) {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100743 /* The boot cpu dictates the cpu type. */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200744 boot_core_type = info->core[cpu].type;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100745 break;
746 }
747 if (cpu >= info->combined)
748 panic("Could not find boot CPU type");
Heiko Carstens48483b32008-01-26 14:11:05 +0100749 }
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100750
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100751 /* Set multi-threading state for the current system */
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200752 mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100753 mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
754 pcpu_set_smt(mtid);
755
756 /* Print number of CPUs */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400757 c_cpus = s_cpus = 0;
Heiko Carstens48483b32008-01-26 14:11:05 +0100758 for (cpu = 0; cpu < info->combined; cpu++) {
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200759 if (sclp.has_core_type &&
760 info->core[cpu].type != boot_core_type)
Heiko Carstens48483b32008-01-26 14:11:05 +0100761 continue;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100762 if (cpu < info->configured)
763 c_cpus += smp_cpu_mtid + 1;
764 else
765 s_cpus += smp_cpu_mtid + 1;
Heiko Carstens48483b32008-01-26 14:11:05 +0100766 }
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100767 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100768
769 /* Add CPUs present at boot */
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100770 get_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400771 __smp_rescan_cpus(info, 0);
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100772 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400773 kfree(info);
Heiko Carstens48483b32008-01-26 14:11:05 +0100774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/*
Heiko Carstens39ce0102007-04-27 16:02:00 +0200777 * Activate a secondary processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Paul Gortmakere2741f12013-06-18 17:04:52 -0400779static void smp_start_secondary(void *cpuvoid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Heiko Carstens1aae0562013-01-30 09:49:40 +0100781 S390_lowcore.last_update_clock = get_tod_clock();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400782 S390_lowcore.restart_stack = (unsigned long) restart_stack;
783 S390_lowcore.restart_fn = (unsigned long) do_restart;
784 S390_lowcore.restart_data = 0;
785 S390_lowcore.restart_source = -1UL;
786 restore_access_regs(S390_lowcore.access_regs_save_area);
787 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
Martin Schwidefskye258d712013-09-24 09:14:56 +0200788 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
Heiko Carstens39ce0102007-04-27 16:02:00 +0200789 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800790 preempt_disable();
Heiko Carstens39ce0102007-04-27 16:02:00 +0200791 init_cpu_timer();
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +0200792 vtime_init();
Heiko Carstens29b08d22006-12-04 15:40:40 +0100793 pfault_init();
Manfred Spraule545a612008-09-07 16:57:22 +0200794 notify_cpu_starting(smp_processor_id());
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200795 set_cpu_online(smp_processor_id(), true);
Heiko Carstens93f3b2e2013-01-02 16:54:12 +0100796 inc_irq_stat(CPU_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 local_irq_enable();
Thomas Gleixner52c00652013-03-21 22:49:57 +0100798 cpu_startup_entry(CPUHP_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/* Upping and downing of CPUs */
Paul Gortmakere2741f12013-06-18 17:04:52 -0400802int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400804 struct pcpu *pcpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100805 int base, i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400807 pcpu = pcpu_devices + cpu;
808 if (pcpu->state != CPU_STATE_CONFIGURED)
Heiko Carstens08d07962008-01-26 14:10:56 +0100809 return -EIO;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100810 base = cpu - (cpu % (smp_cpu_mtid + 1));
811 for (i = 0; i <= smp_cpu_mtid; i++) {
812 if (base + i < nr_cpu_ids)
813 if (cpu_online(base + i))
814 break;
815 }
816 /*
817 * If this is the first CPU of the core to get online
818 * do an initial CPU reset.
819 */
820 if (i > smp_cpu_mtid &&
821 pcpu_sigp_retry(pcpu_devices + base, SIGP_INITIAL_CPU_RESET, 0) !=
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200822 SIGP_CC_ORDER_CODE_ACCEPTED)
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400823 return -EIO;
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000824
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400825 rc = pcpu_alloc_lowcore(pcpu, cpu);
826 if (rc)
827 return rc;
828 pcpu_prepare_secondary(pcpu, cpu);
Thomas Gleixnere80e7812012-04-20 13:05:52 +0000829 pcpu_attach_task(pcpu, tidle);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400830 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
Heiko Carstensa1307bb2015-03-30 12:51:42 +0200831 /* Wait until cpu puts itself in the online & active maps */
832 while (!cpu_online(cpu) || !cpu_active(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 cpu_relax();
834 return 0;
835}
836
Heiko Carstensd80512f2013-12-16 14:31:26 +0100837static unsigned int setup_possible_cpus __initdata;
Heiko Carstens48483b32008-01-26 14:11:05 +0100838
Heiko Carstensd80512f2013-12-16 14:31:26 +0100839static int __init _setup_possible_cpus(char *s)
840{
841 get_option(&s, &setup_possible_cpus);
Heiko Carstens37a33022006-02-17 13:52:47 -0800842 return 0;
843}
Heiko Carstensd80512f2013-12-16 14:31:26 +0100844early_param("possible_cpus", _setup_possible_cpus);
Heiko Carstens37a33022006-02-17 13:52:47 -0800845
Heiko Carstens48483b32008-01-26 14:11:05 +0100846#ifdef CONFIG_HOTPLUG_CPU
847
Heiko Carstens39ce0102007-04-27 16:02:00 +0200848int __cpu_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400850 unsigned long cregs[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Heiko Carstens9acf73b2013-05-22 10:24:37 +0200852 /* Handle possible pending IPIs */
853 smp_handle_ext_call();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400854 set_cpu_online(smp_processor_id(), false);
855 /* Disable pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100856 pfault_fini();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400857 /* Disable interrupt sources via control register. */
858 __ctl_store(cregs, 0, 15);
859 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
860 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
861 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
862 __ctl_load(cregs, 0, 15);
Martin Schwidefskyfe0f4972014-09-30 17:37:52 +0200863 clear_cpu_flag(CIF_NOHZ_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
866
Heiko Carstens39ce0102007-04-27 16:02:00 +0200867void __cpu_die(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400869 struct pcpu *pcpu;
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Wait until target cpu is down */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400872 pcpu = pcpu_devices + cpu;
873 while (!pcpu_stopped(pcpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 cpu_relax();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400875 pcpu_free_lowcore(pcpu);
Martin Schwidefsky050eef32010-08-24 09:26:21 +0200876 atomic_dec(&init_mm.context.attach_count);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200877 cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
878 if (MACHINE_HAS_TLB_LC)
879 cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Heiko Carstensb456d942011-05-23 10:24:33 +0200882void __noreturn cpu_die(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 idle_task_exit();
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200885 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400886 for (;;) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Heiko Carstens255acee2006-02-17 13:52:46 -0800889#endif /* CONFIG_HOTPLUG_CPU */
890
Heiko Carstensd80512f2013-12-16 14:31:26 +0100891void __init smp_fill_possible_mask(void)
892{
David Hildenbrand9747bc42015-05-06 09:29:53 +0200893 unsigned int possible, sclp_max, cpu;
Heiko Carstensd80512f2013-12-16 14:31:26 +0100894
David Hildenbrand3a9f3fe2015-05-06 13:19:29 +0200895 sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
896 sclp_max = min(smp_max_threads, sclp_max);
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200897 sclp_max = sclp.max_cores * sclp_max ?: nr_cpu_ids;
Heiko Carstenscf813db2014-03-10 14:50:16 +0100898 possible = setup_possible_cpus ?: nr_cpu_ids;
David Hildenbrand9747bc42015-05-06 09:29:53 +0200899 possible = min(possible, sclp_max);
Heiko Carstensd80512f2013-12-16 14:31:26 +0100900 for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
901 set_cpu_possible(cpu, true);
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904void __init smp_prepare_cpus(unsigned int max_cpus)
905{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200906 /* request the 0x1201 emergency signal external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200907 if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
Heiko Carstens39ce0102007-04-27 16:02:00 +0200908 panic("Couldn't request external interrupt 0x1201");
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100909 /* request the 0x1202 external call external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200910 if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
Martin Schwidefskyd98e19c2011-10-30 15:16:58 +0100911 panic("Couldn't request external interrupt 0x1202");
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400912 smp_detect_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200915void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400917 struct pcpu *pcpu = pcpu_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400919 pcpu->state = CPU_STATE_CONFIGURED;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100920 pcpu->address = stap();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400921 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400922 S390_lowcore.percpu_offset = __per_cpu_offset[0];
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200923 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200924 set_cpu_present(0, true);
925 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Heiko Carstensea1f4ee2007-05-31 17:38:05 +0200928void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Heiko Carstens02beacc2010-01-13 20:44:34 +0100932void __init smp_setup_processor_id(void)
933{
934 S390_lowcore.cpu_nr = 0;
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +0200935 S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
Heiko Carstens02beacc2010-01-13 20:44:34 +0100936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
939 * the frequency of the profiling timer can be changed
940 * by writing a multiplier value into /proc/profile.
941 *
942 * usually you want to run this on all CPUs ;)
943 */
944int setup_profiling_timer(unsigned int multiplier)
945{
Heiko Carstens39ce0102007-04-27 16:02:00 +0200946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Heiko Carstens08d07962008-01-26 14:10:56 +0100949#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800950static ssize_t cpu_configure_show(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400951 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +0100952{
953 ssize_t count;
954
955 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400956 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
Heiko Carstens08d07962008-01-26 14:10:56 +0100957 mutex_unlock(&smp_cpu_state_mutex);
958 return count;
959}
960
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800961static ssize_t cpu_configure_store(struct device *dev,
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400962 struct device_attribute *attr,
963 const char *buf, size_t count)
Heiko Carstens08d07962008-01-26 14:10:56 +0100964{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400965 struct pcpu *pcpu;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100966 int cpu, val, rc, i;
Heiko Carstens08d07962008-01-26 14:10:56 +0100967 char delim;
968
969 if (sscanf(buf, "%d %c", &val, &delim) != 1)
970 return -EINVAL;
971 if (val != 0 && val != 1)
972 return -EINVAL;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +0100973 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +0200974 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens08d07962008-01-26 14:10:56 +0100975 rc = -EBUSY;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100976 /* disallow configuration changes of online cpus and cpu 0 */
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400977 cpu = dev->id;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100978 cpu -= cpu % (smp_cpu_mtid + 1);
979 if (cpu == 0)
Heiko Carstens08d07962008-01-26 14:10:56 +0100980 goto out;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100981 for (i = 0; i <= smp_cpu_mtid; i++)
982 if (cpu_online(cpu + i))
983 goto out;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400984 pcpu = pcpu_devices + cpu;
Heiko Carstens08d07962008-01-26 14:10:56 +0100985 rc = 0;
986 switch (val) {
987 case 0:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400988 if (pcpu->state != CPU_STATE_CONFIGURED)
989 break;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +0200990 rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400991 if (rc)
992 break;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100993 for (i = 0; i <= smp_cpu_mtid; i++) {
994 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
995 continue;
996 pcpu[i].state = CPU_STATE_STANDBY;
997 smp_cpu_set_polarization(cpu + i,
998 POLARIZATION_UNKNOWN);
999 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001000 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +01001001 break;
1002 case 1:
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001003 if (pcpu->state != CPU_STATE_STANDBY)
1004 break;
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001005 rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001006 if (rc)
1007 break;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +01001008 for (i = 0; i <= smp_cpu_mtid; i++) {
1009 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1010 continue;
1011 pcpu[i].state = CPU_STATE_CONFIGURED;
1012 smp_cpu_set_polarization(cpu + i,
1013 POLARIZATION_UNKNOWN);
1014 }
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001015 topology_expect_change();
Heiko Carstens08d07962008-01-26 14:10:56 +01001016 break;
1017 default:
1018 break;
1019 }
1020out:
Heiko Carstens08d07962008-01-26 14:10:56 +01001021 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001022 put_online_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001023 return rc ? rc : count;
1024}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001025static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001026#endif /* CONFIG_HOTPLUG_CPU */
1027
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001028static ssize_t show_cpu_address(struct device *dev,
1029 struct device_attribute *attr, char *buf)
Heiko Carstens08d07962008-01-26 14:10:56 +01001030{
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001031 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
Heiko Carstens08d07962008-01-26 14:10:56 +01001032}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001033static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
Heiko Carstens08d07962008-01-26 14:10:56 +01001034
Heiko Carstens08d07962008-01-26 14:10:56 +01001035static struct attribute *cpu_common_attrs[] = {
1036#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001037 &dev_attr_configure.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +01001038#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001039 &dev_attr_address.attr,
Heiko Carstens08d07962008-01-26 14:10:56 +01001040 NULL,
1041};
1042
1043static struct attribute_group cpu_common_attr_group = {
1044 .attrs = cpu_common_attrs,
1045};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Heiko Carstens08d07962008-01-26 14:10:56 +01001047static struct attribute *cpu_online_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001048 &dev_attr_idle_count.attr,
1049 &dev_attr_idle_time_us.attr,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001050 NULL,
1051};
1052
Heiko Carstens08d07962008-01-26 14:10:56 +01001053static struct attribute_group cpu_online_attr_group = {
1054 .attrs = cpu_online_attrs,
Heiko Carstensfae8b222007-10-22 12:52:39 +02001055};
1056
Paul Gortmakere2741f12013-06-18 17:04:52 -04001057static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
1058 void *hcpu)
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001059{
1060 unsigned int cpu = (unsigned int)(long)hcpu;
Heiko Carstens2f859d02015-02-11 12:31:03 +01001061 struct device *s = &per_cpu(cpu_device, cpu)->dev;
Akinobu Mitad882ba62010-05-26 14:43:34 -07001062 int err = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001063
Heiko Carstens1c725922012-08-27 15:43:49 +02001064 switch (action & ~CPU_TASKS_FROZEN) {
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001065 case CPU_ONLINE:
Akinobu Mitad882ba62010-05-26 14:43:34 -07001066 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001067 break;
1068 case CPU_DEAD:
Heiko Carstens08d07962008-01-26 14:10:56 +01001069 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001070 break;
1071 }
Akinobu Mitad882ba62010-05-26 14:43:34 -07001072 return notifier_from_errno(err);
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001073}
1074
Paul Gortmakere2741f12013-06-18 17:04:52 -04001075static int smp_add_present_cpu(int cpu)
Heiko Carstens08d07962008-01-26 14:10:56 +01001076{
Heiko Carstens96619fc2013-12-05 12:42:09 +01001077 struct device *s;
1078 struct cpu *c;
Heiko Carstens08d07962008-01-26 14:10:56 +01001079 int rc;
1080
Heiko Carstens96619fc2013-12-05 12:42:09 +01001081 c = kzalloc(sizeof(*c), GFP_KERNEL);
1082 if (!c)
1083 return -ENOMEM;
Heiko Carstens2f859d02015-02-11 12:31:03 +01001084 per_cpu(cpu_device, cpu) = c;
Heiko Carstens96619fc2013-12-05 12:42:09 +01001085 s = &c->dev;
Heiko Carstens08d07962008-01-26 14:10:56 +01001086 c->hotpluggable = 1;
1087 rc = register_cpu(c, cpu);
1088 if (rc)
1089 goto out;
1090 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1091 if (rc)
1092 goto out_cpu;
Heiko Carstens83a24e32011-12-27 11:27:09 +01001093 if (cpu_online(cpu)) {
1094 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1095 if (rc)
1096 goto out_online;
1097 }
1098 rc = topology_cpu_init(c);
1099 if (rc)
1100 goto out_topology;
1101 return 0;
1102
1103out_topology:
1104 if (cpu_online(cpu))
1105 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1106out_online:
Heiko Carstens08d07962008-01-26 14:10:56 +01001107 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1108out_cpu:
1109#ifdef CONFIG_HOTPLUG_CPU
1110 unregister_cpu(c);
1111#endif
1112out:
1113 return rc;
1114}
1115
1116#ifdef CONFIG_HOTPLUG_CPU
Heiko Carstens1e489512008-04-30 13:38:37 +02001117
Heiko Carstens67060d92008-05-30 10:03:27 +02001118int __ref smp_rescan_cpus(void)
Heiko Carstens08d07962008-01-26 14:10:56 +01001119{
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001120 struct sclp_core_info *info;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001121 int nr;
Heiko Carstens08d07962008-01-26 14:10:56 +01001122
Martin Schwidefskyd08d9432015-06-18 14:23:00 +02001123 info = smp_get_core_info();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001124 if (!info)
1125 return -ENOMEM;
Martin Schwidefsky9d40d2e2008-01-26 14:11:31 +01001126 get_online_cpus();
Heiko Carstens0b18d312008-04-30 13:38:36 +02001127 mutex_lock(&smp_cpu_state_mutex);
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001128 nr = __smp_rescan_cpus(info, 1);
Heiko Carstens08d07962008-01-26 14:10:56 +01001129 mutex_unlock(&smp_cpu_state_mutex);
Heiko Carstens0b18d312008-04-30 13:38:36 +02001130 put_online_cpus();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001131 kfree(info);
1132 if (nr)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001133 topology_schedule_update();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -04001134 return 0;
Heiko Carstens1e489512008-04-30 13:38:37 +02001135}
1136
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001137static ssize_t __ref rescan_store(struct device *dev,
1138 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001139 const char *buf,
Heiko Carstens1e489512008-04-30 13:38:37 +02001140 size_t count)
1141{
1142 int rc;
1143
1144 rc = smp_rescan_cpus();
Heiko Carstens08d07962008-01-26 14:10:56 +01001145 return rc ? rc : count;
1146}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001147static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
Heiko Carstens08d07962008-01-26 14:10:56 +01001148#endif /* CONFIG_HOTPLUG_CPU */
1149
Heiko Carstens83a24e32011-12-27 11:27:09 +01001150static int __init s390_smp_init(void)
Heiko Carstensc10fde02008-04-17 07:46:13 +02001151{
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301152 int cpu, rc = 0;
Heiko Carstens2fc2d1e2007-04-27 16:01:56 +02001153
Heiko Carstens08d07962008-01-26 14:10:56 +01001154#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001155 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
Heiko Carstens08d07962008-01-26 14:10:56 +01001156 if (rc)
1157 return rc;
1158#endif
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301159 cpu_notifier_register_begin();
Heiko Carstens08d07962008-01-26 14:10:56 +01001160 for_each_present_cpu(cpu) {
1161 rc = smp_add_present_cpu(cpu);
Heiko Carstensfae8b222007-10-22 12:52:39 +02001162 if (rc)
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Srivatsa S. Bhatf4edbcd2014-03-11 02:05:58 +05301165
1166 __hotcpu_notifier(smp_cpu_notify, 0);
1167
1168out:
1169 cpu_notifier_register_done();
1170 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
Heiko Carstens83a24e32011-12-27 11:27:09 +01001172subsys_initcall(s390_smp_init);