xref: /linux/arch/s390/kernel/smp.c (revision 968ec3f960d013fc74d3a9bed3ae9ab9a44cb951)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  SMP related functions
4  *
5  *    Copyright IBM Corp. 1999, 2012
6  *    Author(s): Denis Joseph Barrow,
7  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>,
8  *
9  *  based on other smp stuff by
10  *    (c) 1995 Alan Cox, CymruNET Ltd  <alan@cymru.net>
11  *    (c) 1998 Ingo Molnar
12  *
13  * 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.
16  */
17 
18 #define pr_fmt(fmt) "cpu: " fmt
19 
20 #include <linux/cpufeature.h>
21 #include <linux/workqueue.h>
22 #include <linux/memblock.h>
23 #include <linux/export.h>
24 #include <linux/init.h>
25 #include <linux/mm.h>
26 #include <linux/err.h>
27 #include <linux/spinlock.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/irqflags.h>
32 #include <linux/irq_work.h>
33 #include <linux/cpu.h>
34 #include <linux/slab.h>
35 #include <linux/sched/hotplug.h>
36 #include <linux/sched/task_stack.h>
37 #include <linux/crash_dump.h>
38 #include <linux/kprobes.h>
39 #include <asm/access-regs.h>
40 #include <asm/asm-offsets.h>
41 #include <asm/machine.h>
42 #include <asm/ctlreg.h>
43 #include <asm/pfault.h>
44 #include <asm/diag.h>
45 #include <asm/facility.h>
46 #include <asm/fpu.h>
47 #include <asm/ipl.h>
48 #include <asm/setup.h>
49 #include <asm/irq.h>
50 #include <asm/tlbflush.h>
51 #include <asm/vtimer.h>
52 #include <asm/abs_lowcore.h>
53 #include <asm/sclp.h>
54 #include <asm/debug.h>
55 #include <asm/os_info.h>
56 #include <asm/sigp.h>
57 #include <asm/nmi.h>
58 #include <asm/stacktrace.h>
59 #include <asm/topology.h>
60 #include <asm/vdso.h>
61 #include <asm/maccess.h>
62 #include "entry.h"
63 
64 enum {
65 	ec_schedule = 0,
66 	ec_call_function_single,
67 	ec_stop_cpu,
68 	ec_mcck_pending,
69 	ec_irq_work,
70 };
71 
72 enum {
73 	CPU_STATE_STANDBY,
74 	CPU_STATE_CONFIGURED,
75 };
76 
77 static u8 boot_core_type;
78 DEFINE_PER_CPU(struct pcpu, pcpu_devices);
79 /*
80  * Pointer to the pcpu area of the boot CPU. This is required when a restart
81  * interrupt is triggered on an offline CPU. For that case accessing percpu
82  * data with the common primitives does not work, since the percpu offset is
83  * stored in a non existent lowcore.
84  */
85 static struct pcpu *ipl_pcpu;
86 
87 unsigned int smp_cpu_mt_shift;
88 EXPORT_SYMBOL(smp_cpu_mt_shift);
89 
90 unsigned int smp_cpu_mtid;
91 EXPORT_SYMBOL(smp_cpu_mtid);
92 
93 #ifdef CONFIG_CRASH_DUMP
94 __vector128 __initdata boot_cpu_vector_save_area[__NUM_VXRS];
95 #endif
96 
97 static unsigned int smp_max_threads __initdata = -1U;
98 cpumask_t cpu_setup_mask;
99 
100 static int __init early_smt(char *s)
101 {
102 	get_option(&s, &smp_max_threads);
103 	return 0;
104 }
105 early_param("smt", early_smt);
106 
107 /*
108  * The smp_cpu_state_mutex must be held when changing the state or polarization
109  * member of a pcpu data structure within the pcpu_devices array.
110  */
111 DEFINE_MUTEX(smp_cpu_state_mutex);
112 
113 /*
114  * Signal processor helper functions.
115  */
116 static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm)
117 {
118 	int cc;
119 
120 	while (1) {
121 		cc = __pcpu_sigp(addr, order, parm, NULL);
122 		if (cc != SIGP_CC_BUSY)
123 			return cc;
124 		cpu_relax();
125 	}
126 }
127 
128 static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
129 {
130 	int cc, retry;
131 
132 	for (retry = 0; ; retry++) {
133 		cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
134 		if (cc != SIGP_CC_BUSY)
135 			break;
136 		if (retry >= 3)
137 			udelay(10);
138 	}
139 	return cc;
140 }
141 
142 static inline int pcpu_stopped(struct pcpu *pcpu)
143 {
144 	u32 status;
145 
146 	if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
147 			0, &status) != SIGP_CC_STATUS_STORED)
148 		return 0;
149 	return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
150 }
151 
152 static inline int pcpu_running(struct pcpu *pcpu)
153 {
154 	if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
155 			0, NULL) != SIGP_CC_STATUS_STORED)
156 		return 1;
157 	/* Status stored condition code is equivalent to cpu not running. */
158 	return 0;
159 }
160 
161 /*
162  * Find struct pcpu by cpu address.
163  */
164 static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
165 {
166 	int cpu;
167 
168 	for_each_cpu(cpu, mask)
169 		if (per_cpu(pcpu_devices, cpu).address == address)
170 			return &per_cpu(pcpu_devices, cpu);
171 	return NULL;
172 }
173 
174 static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
175 {
176 	if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
177 		return;
178 	pcpu->ec_clk = get_tod_clock_fast();
179 	pcpu_sigp_retry(pcpu, SIGP_EXTERNAL_CALL, 0);
180 }
181 
182 static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
183 {
184 	unsigned long async_stack, nodat_stack, mcck_stack;
185 	struct lowcore *lc;
186 
187 	lc = (struct lowcore *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
188 	nodat_stack = __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
189 	async_stack = stack_alloc();
190 	mcck_stack = stack_alloc();
191 	if (!lc || !nodat_stack || !async_stack || !mcck_stack)
192 		goto out;
193 	memcpy(lc, get_lowcore(), 512);
194 	memset((char *) lc + 512, 0, sizeof(*lc) - 512);
195 	lc->async_stack = async_stack + STACK_INIT_OFFSET;
196 	lc->nodat_stack = nodat_stack + STACK_INIT_OFFSET;
197 	lc->mcck_stack = mcck_stack + STACK_INIT_OFFSET;
198 	lc->cpu_nr = cpu;
199 	lc->spinlock_lockval = arch_spin_lockval(cpu);
200 	lc->spinlock_index = 0;
201 	lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
202 	lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
203 	lc->preempt_count = PREEMPT_DISABLED;
204 	if (nmi_alloc_mcesa(&lc->mcesad))
205 		goto out;
206 	if (abs_lowcore_map(cpu, lc, true))
207 		goto out_mcesa;
208 	lowcore_ptr[cpu] = lc;
209 	pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, __pa(lc));
210 	return 0;
211 
212 out_mcesa:
213 	nmi_free_mcesa(&lc->mcesad);
214 out:
215 	stack_free(mcck_stack);
216 	stack_free(async_stack);
217 	free_pages(nodat_stack, THREAD_SIZE_ORDER);
218 	free_pages((unsigned long) lc, LC_ORDER);
219 	return -ENOMEM;
220 }
221 
222 static void pcpu_free_lowcore(struct pcpu *pcpu, int cpu)
223 {
224 	unsigned long async_stack, nodat_stack, mcck_stack;
225 	struct lowcore *lc;
226 
227 	lc = lowcore_ptr[cpu];
228 	nodat_stack = lc->nodat_stack - STACK_INIT_OFFSET;
229 	async_stack = lc->async_stack - STACK_INIT_OFFSET;
230 	mcck_stack = lc->mcck_stack - STACK_INIT_OFFSET;
231 	pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
232 	lowcore_ptr[cpu] = NULL;
233 	abs_lowcore_unmap(cpu);
234 	nmi_free_mcesa(&lc->mcesad);
235 	stack_free(async_stack);
236 	stack_free(mcck_stack);
237 	free_pages(nodat_stack, THREAD_SIZE_ORDER);
238 	free_pages((unsigned long) lc, LC_ORDER);
239 }
240 
241 static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
242 {
243 	struct lowcore *lc, *abs_lc;
244 
245 	lc = lowcore_ptr[cpu];
246 	cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
247 	cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
248 	lc->cpu_nr = cpu;
249 	lc->pcpu = (unsigned long)pcpu;
250 	lc->restart_flags = RESTART_FLAG_CTLREGS;
251 	lc->spinlock_lockval = arch_spin_lockval(cpu);
252 	lc->spinlock_index = 0;
253 	lc->percpu_offset = __per_cpu_offset[cpu];
254 	lc->kernel_asce = get_lowcore()->kernel_asce;
255 	lc->user_asce = s390_invalid_asce;
256 	lc->user_timer = lc->system_timer =
257 		lc->steal_timer = lc->avg_steal_timer = 0;
258 	abs_lc = get_abs_lowcore();
259 	memcpy(lc->cregs_save_area, abs_lc->cregs_save_area, sizeof(lc->cregs_save_area));
260 	put_abs_lowcore(abs_lc);
261 	lc->cregs_save_area[1] = lc->user_asce;
262 	lc->cregs_save_area[7] = lc->user_asce;
263 	save_access_regs((unsigned int *) lc->access_regs_save_area);
264 	arch_spin_lock_setup(cpu);
265 }
266 
267 static void pcpu_attach_task(int cpu, struct task_struct *tsk)
268 {
269 	struct lowcore *lc;
270 
271 	lc = lowcore_ptr[cpu];
272 	lc->kernel_stack = (unsigned long)task_stack_page(tsk) + STACK_INIT_OFFSET;
273 	lc->current_task = (unsigned long)tsk;
274 	lc->lpp = LPP_MAGIC;
275 	lc->current_pid = tsk->pid;
276 	lc->user_timer = tsk->thread.user_timer;
277 	lc->guest_timer = tsk->thread.guest_timer;
278 	lc->system_timer = tsk->thread.system_timer;
279 	lc->hardirq_timer = tsk->thread.hardirq_timer;
280 	lc->softirq_timer = tsk->thread.softirq_timer;
281 	lc->steal_timer = 0;
282 #ifdef CONFIG_STACKPROTECTOR
283 	lc->stack_canary = tsk->stack_canary;
284 #endif
285 }
286 
287 static void pcpu_start_fn(int cpu, void (*func)(void *), void *data)
288 {
289 	struct lowcore *lc;
290 
291 	lc = lowcore_ptr[cpu];
292 	lc->restart_stack = lc->kernel_stack;
293 	lc->restart_fn = (unsigned long) func;
294 	lc->restart_data = (unsigned long) data;
295 	lc->restart_source = -1U;
296 	pcpu_sigp_retry(per_cpu_ptr(&pcpu_devices, cpu), SIGP_RESTART, 0);
297 }
298 
299 typedef void (pcpu_delegate_fn)(void *);
300 
301 /*
302  * Call function via PSW restart on pcpu and stop the current cpu.
303  */
304 static void __pcpu_delegate(pcpu_delegate_fn *func, void *data)
305 {
306 	func(data);	/* should not return */
307 }
308 
309 static void __noreturn pcpu_delegate(struct pcpu *pcpu, int cpu,
310 				     pcpu_delegate_fn *func,
311 				     void *data, unsigned long stack)
312 {
313 	struct lowcore *lc, *abs_lc;
314 	unsigned int source_cpu;
315 
316 	lc = lowcore_ptr[cpu];
317 	source_cpu = stap();
318 
319 	if (pcpu->address == source_cpu) {
320 		call_on_stack(2, stack, void, __pcpu_delegate,
321 			      pcpu_delegate_fn *, func, void *, data);
322 	}
323 	/* Stop target cpu (if func returns this stops the current cpu). */
324 	pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
325 	pcpu_sigp_retry(pcpu, SIGP_CPU_RESET, 0);
326 	/* Restart func on the target cpu and stop the current cpu. */
327 	if (lc) {
328 		lc->restart_stack = stack;
329 		lc->restart_fn = (unsigned long)func;
330 		lc->restart_data = (unsigned long)data;
331 		lc->restart_source = source_cpu;
332 	} else {
333 		abs_lc = get_abs_lowcore();
334 		abs_lc->restart_stack = stack;
335 		abs_lc->restart_fn = (unsigned long)func;
336 		abs_lc->restart_data = (unsigned long)data;
337 		abs_lc->restart_source = source_cpu;
338 		put_abs_lowcore(abs_lc);
339 	}
340 	asm volatile(
341 		"0:	sigp	0,%0,%2	# sigp restart to target cpu\n"
342 		"	brc	2,0b	# busy, try again\n"
343 		"1:	sigp	0,%1,%3	# sigp stop to current cpu\n"
344 		"	brc	2,1b	# busy, try again"
345 		: : "d" (pcpu->address), "d" (source_cpu),
346 		    "K" (SIGP_RESTART), "K" (SIGP_STOP)
347 		: "0", "1", "cc");
348 	for (;;) ;
349 }
350 
351 /*
352  * Enable additional logical cpus for multi-threading.
353  */
354 static int pcpu_set_smt(unsigned int mtid)
355 {
356 	int cc;
357 
358 	if (smp_cpu_mtid == mtid)
359 		return 0;
360 	cc = __pcpu_sigp(0, SIGP_SET_MULTI_THREADING, mtid, NULL);
361 	if (cc == 0) {
362 		smp_cpu_mtid = mtid;
363 		smp_cpu_mt_shift = 0;
364 		while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
365 			smp_cpu_mt_shift++;
366 		per_cpu(pcpu_devices, 0).address = stap();
367 	}
368 	return cc;
369 }
370 
371 /*
372  * Call function on the ipl CPU.
373  */
374 void __noreturn smp_call_ipl_cpu(void (*func)(void *), void *data)
375 {
376 	struct lowcore *lc = lowcore_ptr[0];
377 
378 	if (ipl_pcpu->address == stap())
379 		lc = get_lowcore();
380 
381 	pcpu_delegate(ipl_pcpu, 0, func, data, lc->nodat_stack);
382 }
383 
384 int smp_find_processor_id(u16 address)
385 {
386 	int cpu;
387 
388 	for_each_present_cpu(cpu)
389 		if (per_cpu(pcpu_devices, cpu).address == address)
390 			return cpu;
391 	return -1;
392 }
393 
394 void schedule_mcck_handler(void)
395 {
396 	pcpu_ec_call(this_cpu_ptr(&pcpu_devices), ec_mcck_pending);
397 }
398 
399 bool notrace arch_vcpu_is_preempted(int cpu)
400 {
401 	if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu))
402 		return false;
403 	if (pcpu_running(per_cpu_ptr(&pcpu_devices, cpu)))
404 		return false;
405 	return true;
406 }
407 EXPORT_SYMBOL(arch_vcpu_is_preempted);
408 
409 void notrace smp_yield_cpu(int cpu)
410 {
411 	if (!machine_has_diag9c())
412 		return;
413 	diag_stat_inc_norecursion(DIAG_STAT_X09C);
414 	asm volatile("diag %0,0,0x9c"
415 		     : : "d" (per_cpu(pcpu_devices, cpu).address));
416 }
417 EXPORT_SYMBOL_GPL(smp_yield_cpu);
418 
419 /*
420  * Send cpus emergency shutdown signal. This gives the cpus the
421  * opportunity to complete outstanding interrupts.
422  */
423 void notrace smp_emergency_stop(void)
424 {
425 	static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;
426 	static cpumask_t cpumask;
427 	u64 end;
428 	int cpu;
429 
430 	arch_spin_lock(&lock);
431 	cpumask_copy(&cpumask, cpu_online_mask);
432 	cpumask_clear_cpu(smp_processor_id(), &cpumask);
433 
434 	end = get_tod_clock_monotonic() + (1000000UL << 12);
435 	for_each_cpu(cpu, &cpumask) {
436 		struct pcpu *pcpu = per_cpu_ptr(&pcpu_devices, cpu);
437 		set_bit(ec_stop_cpu, &pcpu->ec_mask);
438 		while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
439 				   0, NULL) == SIGP_CC_BUSY &&
440 		       get_tod_clock_monotonic() < end)
441 			cpu_relax();
442 	}
443 	while (get_tod_clock_monotonic() < end) {
444 		for_each_cpu(cpu, &cpumask)
445 			if (pcpu_stopped(per_cpu_ptr(&pcpu_devices, cpu)))
446 				cpumask_clear_cpu(cpu, &cpumask);
447 		if (cpumask_empty(&cpumask))
448 			break;
449 		cpu_relax();
450 	}
451 	arch_spin_unlock(&lock);
452 }
453 NOKPROBE_SYMBOL(smp_emergency_stop);
454 
455 /*
456  * Stop all cpus but the current one.
457  */
458 void smp_send_stop(void)
459 {
460 	struct pcpu *pcpu;
461 	int cpu;
462 
463 	/* Disable all interrupts/machine checks */
464 	__load_psw_mask(PSW_KERNEL_BITS);
465 	trace_hardirqs_off();
466 
467 	debug_set_critical();
468 
469 	if (oops_in_progress)
470 		smp_emergency_stop();
471 
472 	/* stop all processors */
473 	for_each_online_cpu(cpu) {
474 		if (cpu == smp_processor_id())
475 			continue;
476 		pcpu = per_cpu_ptr(&pcpu_devices, cpu);
477 		pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
478 		while (!pcpu_stopped(pcpu))
479 			cpu_relax();
480 	}
481 }
482 
483 /*
484  * This is the main routine where commands issued by other
485  * cpus are handled.
486  */
487 static void smp_handle_ext_call(void)
488 {
489 	unsigned long bits;
490 
491 	/* handle bit signal external calls */
492 	bits = this_cpu_xchg(pcpu_devices.ec_mask, 0);
493 	if (test_bit(ec_stop_cpu, &bits))
494 		smp_stop_cpu();
495 	if (test_bit(ec_schedule, &bits))
496 		scheduler_ipi();
497 	if (test_bit(ec_call_function_single, &bits))
498 		generic_smp_call_function_single_interrupt();
499 	if (test_bit(ec_mcck_pending, &bits))
500 		s390_handle_mcck();
501 	if (test_bit(ec_irq_work, &bits))
502 		irq_work_run();
503 }
504 
505 static void do_ext_call_interrupt(struct ext_code ext_code,
506 				  unsigned int param32, unsigned long param64)
507 {
508 	inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
509 	smp_handle_ext_call();
510 }
511 
512 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
513 {
514 	int cpu;
515 
516 	for_each_cpu(cpu, mask)
517 		pcpu_ec_call(per_cpu_ptr(&pcpu_devices, cpu), ec_call_function_single);
518 }
519 
520 void arch_send_call_function_single_ipi(int cpu)
521 {
522 	pcpu_ec_call(per_cpu_ptr(&pcpu_devices, cpu), ec_call_function_single);
523 }
524 
525 /*
526  * this function sends a 'reschedule' IPI to another CPU.
527  * it goes straight through and wastes no time serializing
528  * anything. Worst case is that we lose a reschedule ...
529  */
530 void arch_smp_send_reschedule(int cpu)
531 {
532 	pcpu_ec_call(per_cpu_ptr(&pcpu_devices, cpu), ec_schedule);
533 }
534 
535 #ifdef CONFIG_IRQ_WORK
536 void arch_irq_work_raise(void)
537 {
538 	pcpu_ec_call(this_cpu_ptr(&pcpu_devices), ec_irq_work);
539 }
540 #endif
541 
542 #ifdef CONFIG_CRASH_DUMP
543 
544 int smp_store_status(int cpu)
545 {
546 	struct lowcore *lc;
547 	struct pcpu *pcpu;
548 	unsigned long pa;
549 
550 	pcpu = per_cpu_ptr(&pcpu_devices, cpu);
551 	lc = lowcore_ptr[cpu];
552 	pa = __pa(&lc->floating_pt_save_area);
553 	if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_STATUS_AT_ADDRESS,
554 			      pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
555 		return -EIO;
556 	if (!cpu_has_vx() && !cpu_has_gs())
557 		return 0;
558 	pa = lc->mcesad & MCESA_ORIGIN_MASK;
559 	if (cpu_has_gs())
560 		pa |= lc->mcesad & MCESA_LC_MASK;
561 	if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
562 			      pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
563 		return -EIO;
564 	return 0;
565 }
566 
567 /*
568  * Collect CPU state of the previous, crashed system.
569  * There are three cases:
570  * 1) standard zfcp/nvme dump
571  *    condition: OLDMEM_BASE == NULL && is_ipl_type_dump() == true
572  *    The state for all CPUs except the boot CPU needs to be collected
573  *    with sigp stop-and-store-status. The boot CPU state is located in
574  *    the absolute lowcore of the memory stored in the HSA. The zcore code
575  *    will copy the boot CPU state from the HSA.
576  * 2) stand-alone kdump for SCSI/NVMe (zfcp/nvme dump with swapped memory)
577  *    condition: OLDMEM_BASE != NULL && is_ipl_type_dump() == true
578  *    The state for all CPUs except the boot CPU needs to be collected
579  *    with sigp stop-and-store-status. The firmware or the boot-loader
580  *    stored the registers of the boot CPU in the absolute lowcore in the
581  *    memory of the old system.
582  * 3) kdump or stand-alone kdump for DASD
583  *    condition: OLDMEM_BASE != NULL && is_ipl_type_dump() == false
584  *    The state for all CPUs except the boot CPU needs to be collected
585  *    with sigp stop-and-store-status. The kexec code or the boot-loader
586  *    stored the registers of the boot CPU in the memory of the old system.
587  *
588  * Note that the legacy kdump mode where the old kernel stored the CPU states
589  * does no longer exist: setup_arch() explicitly deactivates the elfcorehdr=
590  * kernel parameter. The is_kdump_kernel() implementation on s390 is independent
591  * of the elfcorehdr= parameter.
592  */
593 static bool dump_available(void)
594 {
595 	return oldmem_data.start || is_ipl_type_dump();
596 }
597 
598 void __init smp_save_dump_ipl_cpu(void)
599 {
600 	struct save_area *sa;
601 	void *regs;
602 
603 	if (!dump_available())
604 		return;
605 	sa = save_area_alloc(true);
606 	regs = memblock_alloc_or_panic(512, 8);
607 	copy_oldmem_kernel(regs, __LC_FPREGS_SAVE_AREA, 512);
608 	save_area_add_regs(sa, regs);
609 	memblock_free(regs, 512);
610 	if (cpu_has_vx())
611 		save_area_add_vxrs(sa, boot_cpu_vector_save_area);
612 }
613 
614 void __init smp_save_dump_secondary_cpus(void)
615 {
616 	int addr, boot_cpu_addr, max_cpu_addr;
617 	struct save_area *sa;
618 	void *page;
619 
620 	if (!dump_available())
621 		return;
622 	/* Allocate a page as dumping area for the store status sigps */
623 	page = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
624 	if (!page)
625 		panic("ERROR: Failed to allocate %lx bytes below %lx\n",
626 		      PAGE_SIZE, 1UL << 31);
627 
628 	/* Set multi-threading state to the previous system. */
629 	pcpu_set_smt(sclp.mtid_prev);
630 	boot_cpu_addr = stap();
631 	max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
632 	for (addr = 0; addr <= max_cpu_addr; addr++) {
633 		if (addr == boot_cpu_addr)
634 			continue;
635 		if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0) ==
636 		    SIGP_CC_NOT_OPERATIONAL)
637 			continue;
638 		sa = save_area_alloc(false);
639 		__pcpu_sigp_relax(addr, SIGP_STORE_STATUS_AT_ADDRESS, __pa(page));
640 		save_area_add_regs(sa, page);
641 		if (cpu_has_vx()) {
642 			__pcpu_sigp_relax(addr, SIGP_STORE_ADDITIONAL_STATUS, __pa(page));
643 			save_area_add_vxrs(sa, page);
644 		}
645 	}
646 	memblock_free(page, PAGE_SIZE);
647 	diag_amode31_ops.diag308_reset();
648 	pcpu_set_smt(0);
649 }
650 #endif /* CONFIG_CRASH_DUMP */
651 
652 void smp_cpu_set_polarization(int cpu, int val)
653 {
654 	per_cpu(pcpu_devices, cpu).polarization = val;
655 }
656 
657 int smp_cpu_get_polarization(int cpu)
658 {
659 	return per_cpu(pcpu_devices, cpu).polarization;
660 }
661 
662 void smp_cpu_set_capacity(int cpu, unsigned long val)
663 {
664 	per_cpu(pcpu_devices, cpu).capacity = val;
665 }
666 
667 unsigned long smp_cpu_get_capacity(int cpu)
668 {
669 	return per_cpu(pcpu_devices, cpu).capacity;
670 }
671 
672 void smp_set_core_capacity(int cpu, unsigned long val)
673 {
674 	int i;
675 
676 	cpu = smp_get_base_cpu(cpu);
677 	for (i = cpu; (i <= cpu + smp_cpu_mtid) && (i < nr_cpu_ids); i++)
678 		smp_cpu_set_capacity(i, val);
679 }
680 
681 int smp_cpu_get_cpu_address(int cpu)
682 {
683 	return per_cpu(pcpu_devices, cpu).address;
684 }
685 
686 static void __ref smp_get_core_info(struct sclp_core_info *info, int early)
687 {
688 	static int use_sigp_detection;
689 	int address;
690 
691 	if (use_sigp_detection || sclp_get_core_info(info, early)) {
692 		use_sigp_detection = 1;
693 		for (address = 0;
694 		     address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
695 		     address += (1U << smp_cpu_mt_shift)) {
696 			if (__pcpu_sigp_relax(address, SIGP_SENSE, 0) ==
697 			    SIGP_CC_NOT_OPERATIONAL)
698 				continue;
699 			info->core[info->configured].core_id =
700 				address >> smp_cpu_mt_shift;
701 			info->core[info->configured].type = boot_core_type;
702 			info->configured++;
703 		}
704 		info->combined = info->configured;
705 	}
706 }
707 
708 static int smp_add_core(struct sclp_core_entry *core, cpumask_t *avail,
709 			bool configured, bool early)
710 {
711 	struct pcpu *pcpu;
712 	int cpu, nr, i;
713 	u16 address;
714 
715 	nr = 0;
716 	if (sclp.has_core_type && core->type != boot_core_type)
717 		return nr;
718 	cpu = cpumask_first(avail);
719 	address = core->core_id << smp_cpu_mt_shift;
720 	for (i = 0; (i <= smp_cpu_mtid) && (cpu < nr_cpu_ids); i++) {
721 		if (pcpu_find_address(cpu_present_mask, address + i))
722 			continue;
723 		pcpu = per_cpu_ptr(&pcpu_devices, cpu);
724 		pcpu->address = address + i;
725 		if (configured)
726 			pcpu->state = CPU_STATE_CONFIGURED;
727 		else
728 			pcpu->state = CPU_STATE_STANDBY;
729 		smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
730 		smp_cpu_set_capacity(cpu, CPU_CAPACITY_HIGH);
731 		set_cpu_present(cpu, true);
732 		if (!early && arch_register_cpu(cpu))
733 			set_cpu_present(cpu, false);
734 		else
735 			nr++;
736 		cpumask_clear_cpu(cpu, avail);
737 		cpu = cpumask_next(cpu, avail);
738 	}
739 	return nr;
740 }
741 
742 static int __smp_rescan_cpus(struct sclp_core_info *info, bool early)
743 {
744 	struct sclp_core_entry *core;
745 	static cpumask_t avail;
746 	bool configured;
747 	u16 core_id;
748 	int nr, i;
749 
750 	cpus_read_lock();
751 	mutex_lock(&smp_cpu_state_mutex);
752 	nr = 0;
753 	cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
754 	/*
755 	 * Add IPL core first (which got logical CPU number 0) to make sure
756 	 * that all SMT threads get subsequent logical CPU numbers.
757 	 */
758 	if (early) {
759 		core_id = per_cpu(pcpu_devices, 0).address >> smp_cpu_mt_shift;
760 		for (i = 0; i < info->configured; i++) {
761 			core = &info->core[i];
762 			if (core->core_id == core_id) {
763 				nr += smp_add_core(core, &avail, true, early);
764 				break;
765 			}
766 		}
767 	}
768 	for (i = 0; i < info->combined; i++) {
769 		configured = i < info->configured;
770 		nr += smp_add_core(&info->core[i], &avail, configured, early);
771 	}
772 	mutex_unlock(&smp_cpu_state_mutex);
773 	cpus_read_unlock();
774 	return nr;
775 }
776 
777 void __init smp_detect_cpus(void)
778 {
779 	unsigned int cpu, mtid, c_cpus, s_cpus;
780 	struct sclp_core_info *info;
781 	u16 address;
782 
783 	/* Get CPU information */
784 	info = memblock_alloc_or_panic(sizeof(*info), 8);
785 	smp_get_core_info(info, 1);
786 	/* Find boot CPU type */
787 	if (sclp.has_core_type) {
788 		address = stap();
789 		for (cpu = 0; cpu < info->combined; cpu++)
790 			if (info->core[cpu].core_id == address) {
791 				/* The boot cpu dictates the cpu type. */
792 				boot_core_type = info->core[cpu].type;
793 				break;
794 			}
795 		if (cpu >= info->combined)
796 			panic("Could not find boot CPU type");
797 	}
798 
799 	/* Set multi-threading state for the current system */
800 	mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
801 	mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
802 	pcpu_set_smt(mtid);
803 	cpu_smt_set_num_threads(smp_cpu_mtid + 1, smp_cpu_mtid + 1);
804 
805 	/* Print number of CPUs */
806 	c_cpus = s_cpus = 0;
807 	for (cpu = 0; cpu < info->combined; cpu++) {
808 		if (sclp.has_core_type &&
809 		    info->core[cpu].type != boot_core_type)
810 			continue;
811 		if (cpu < info->configured)
812 			c_cpus += smp_cpu_mtid + 1;
813 		else
814 			s_cpus += smp_cpu_mtid + 1;
815 	}
816 	pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
817 	memblock_free(info, sizeof(*info));
818 }
819 
820 /*
821  *	Activate a secondary processor.
822  */
823 static void smp_start_secondary(void *cpuvoid)
824 {
825 	struct lowcore *lc = get_lowcore();
826 	int cpu = raw_smp_processor_id();
827 
828 	lc->last_update_clock = get_tod_clock();
829 	lc->restart_stack = (unsigned long)restart_stack;
830 	lc->restart_fn = (unsigned long)do_restart;
831 	lc->restart_data = 0;
832 	lc->restart_source = -1U;
833 	lc->restart_flags = 0;
834 	restore_access_regs(lc->access_regs_save_area);
835 	cpu_init();
836 	rcutree_report_cpu_starting(cpu);
837 	init_cpu_timer();
838 	vtime_init();
839 	vdso_getcpu_init();
840 	pfault_init();
841 	cpumask_set_cpu(cpu, &cpu_setup_mask);
842 	update_cpu_masks();
843 	notify_cpu_starting(cpu);
844 	if (topology_cpu_dedicated(cpu))
845 		set_cpu_flag(CIF_DEDICATED_CPU);
846 	else
847 		clear_cpu_flag(CIF_DEDICATED_CPU);
848 	set_cpu_online(cpu, true);
849 	inc_irq_stat(CPU_RST);
850 	local_irq_enable();
851 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
852 }
853 
854 /* Upping and downing of CPUs */
855 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
856 {
857 	struct pcpu *pcpu = per_cpu_ptr(&pcpu_devices, cpu);
858 	int rc;
859 
860 	if (pcpu->state != CPU_STATE_CONFIGURED)
861 		return -EIO;
862 	if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
863 	    SIGP_CC_ORDER_CODE_ACCEPTED)
864 		return -EIO;
865 
866 	rc = pcpu_alloc_lowcore(pcpu, cpu);
867 	if (rc)
868 		return rc;
869 	/*
870 	 * Make sure global control register contents do not change
871 	 * until new CPU has initialized control registers.
872 	 */
873 	system_ctlreg_lock();
874 	pcpu_prepare_secondary(pcpu, cpu);
875 	pcpu_attach_task(cpu, tidle);
876 	pcpu_start_fn(cpu, smp_start_secondary, NULL);
877 	/* Wait until cpu puts itself in the online & active maps */
878 	while (!cpu_online(cpu))
879 		cpu_relax();
880 	system_ctlreg_unlock();
881 	return 0;
882 }
883 
884 static unsigned int setup_possible_cpus __initdata;
885 
886 static int __init _setup_possible_cpus(char *s)
887 {
888 	get_option(&s, &setup_possible_cpus);
889 	return 0;
890 }
891 early_param("possible_cpus", _setup_possible_cpus);
892 
893 int __cpu_disable(void)
894 {
895 	struct ctlreg cregs[16];
896 	int cpu;
897 
898 	/* Handle possible pending IPIs */
899 	smp_handle_ext_call();
900 	cpu = smp_processor_id();
901 	set_cpu_online(cpu, false);
902 	cpumask_clear_cpu(cpu, &cpu_setup_mask);
903 	update_cpu_masks();
904 	/* Disable pseudo page faults on this cpu. */
905 	pfault_fini();
906 	/* Disable interrupt sources via control register. */
907 	__local_ctl_store(0, 15, cregs);
908 	cregs[0].val  &= ~0x0000ee70UL;	/* disable all external interrupts */
909 	cregs[6].val  &= ~0xff000000UL;	/* disable all I/O interrupts */
910 	cregs[14].val &= ~0x1f000000UL;	/* disable most machine checks */
911 	__local_ctl_load(0, 15, cregs);
912 	clear_cpu_flag(CIF_NOHZ_DELAY);
913 	return 0;
914 }
915 
916 void __cpu_die(unsigned int cpu)
917 {
918 	struct pcpu *pcpu;
919 
920 	/* Wait until target cpu is down */
921 	pcpu = per_cpu_ptr(&pcpu_devices, cpu);
922 	while (!pcpu_stopped(pcpu))
923 		cpu_relax();
924 	pcpu_free_lowcore(pcpu, cpu);
925 	cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
926 	cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
927 	pcpu->flags = 0;
928 }
929 
930 void __noreturn cpu_die(void)
931 {
932 	idle_task_exit();
933 	pcpu_sigp_retry(this_cpu_ptr(&pcpu_devices), SIGP_STOP, 0);
934 	for (;;) ;
935 }
936 
937 void __init smp_fill_possible_mask(void)
938 {
939 	unsigned int possible, sclp_max, cpu;
940 
941 	sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
942 	sclp_max = min(smp_max_threads, sclp_max);
943 	sclp_max = (sclp.max_cores * sclp_max) ?: nr_cpu_ids;
944 	possible = setup_possible_cpus ?: nr_cpu_ids;
945 	possible = min(possible, sclp_max);
946 	for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
947 		set_cpu_possible(cpu, true);
948 }
949 
950 void __init smp_prepare_cpus(unsigned int max_cpus)
951 {
952 	if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
953 		panic("Couldn't request external interrupt 0x1201");
954 	system_ctl_set_bit(0, 14);
955 	if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
956 		panic("Couldn't request external interrupt 0x1202");
957 	system_ctl_set_bit(0, 13);
958 	smp_rescan_cpus(true);
959 }
960 
961 void __init smp_prepare_boot_cpu(void)
962 {
963 	struct lowcore *lc = get_lowcore();
964 
965 	WARN_ON(!cpu_present(0) || !cpu_online(0));
966 	lc->percpu_offset = __per_cpu_offset[0];
967 	ipl_pcpu = per_cpu_ptr(&pcpu_devices, 0);
968 	ipl_pcpu->state = CPU_STATE_CONFIGURED;
969 	lc->pcpu = (unsigned long)ipl_pcpu;
970 	smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
971 	smp_cpu_set_capacity(0, CPU_CAPACITY_HIGH);
972 }
973 
974 void __init smp_setup_processor_id(void)
975 {
976 	struct lowcore *lc = get_lowcore();
977 
978 	lc->cpu_nr = 0;
979 	per_cpu(pcpu_devices, 0).address = stap();
980 	lc->spinlock_lockval = arch_spin_lockval(0);
981 	lc->spinlock_index = 0;
982 }
983 
984 /*
985  * the frequency of the profiling timer can be changed
986  * by writing a multiplier value into /proc/profile.
987  *
988  * usually you want to run this on all CPUs ;)
989  */
990 int setup_profiling_timer(unsigned int multiplier)
991 {
992 	return 0;
993 }
994 
995 static ssize_t cpu_configure_show(struct device *dev,
996 				  struct device_attribute *attr, char *buf)
997 {
998 	ssize_t count;
999 
1000 	mutex_lock(&smp_cpu_state_mutex);
1001 	count = sysfs_emit(buf, "%d\n", per_cpu(pcpu_devices, dev->id).state);
1002 	mutex_unlock(&smp_cpu_state_mutex);
1003 	return count;
1004 }
1005 
1006 static ssize_t cpu_configure_store(struct device *dev,
1007 				   struct device_attribute *attr,
1008 				   const char *buf, size_t count)
1009 {
1010 	struct pcpu *pcpu;
1011 	int cpu, val, rc, i;
1012 	char delim;
1013 
1014 	if (sscanf(buf, "%d %c", &val, &delim) != 1)
1015 		return -EINVAL;
1016 	if (val != 0 && val != 1)
1017 		return -EINVAL;
1018 	cpus_read_lock();
1019 	mutex_lock(&smp_cpu_state_mutex);
1020 	rc = -EBUSY;
1021 	/* disallow configuration changes of online cpus */
1022 	cpu = dev->id;
1023 	cpu = smp_get_base_cpu(cpu);
1024 	for (i = 0; i <= smp_cpu_mtid; i++)
1025 		if (cpu_online(cpu + i))
1026 			goto out;
1027 	pcpu = per_cpu_ptr(&pcpu_devices, cpu);
1028 	rc = 0;
1029 	switch (val) {
1030 	case 0:
1031 		if (pcpu->state != CPU_STATE_CONFIGURED)
1032 			break;
1033 		rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
1034 		if (rc)
1035 			break;
1036 		for (i = 0; i <= smp_cpu_mtid; i++) {
1037 			if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1038 				continue;
1039 			per_cpu(pcpu_devices, cpu + i).state = CPU_STATE_STANDBY;
1040 			smp_cpu_set_polarization(cpu + i,
1041 						 POLARIZATION_UNKNOWN);
1042 		}
1043 		topology_expect_change();
1044 		break;
1045 	case 1:
1046 		if (pcpu->state != CPU_STATE_STANDBY)
1047 			break;
1048 		rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
1049 		if (rc)
1050 			break;
1051 		for (i = 0; i <= smp_cpu_mtid; i++) {
1052 			if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1053 				continue;
1054 			per_cpu(pcpu_devices, cpu + i).state = CPU_STATE_CONFIGURED;
1055 			smp_cpu_set_polarization(cpu + i,
1056 						 POLARIZATION_UNKNOWN);
1057 		}
1058 		topology_expect_change();
1059 		break;
1060 	default:
1061 		break;
1062 	}
1063 out:
1064 	mutex_unlock(&smp_cpu_state_mutex);
1065 	cpus_read_unlock();
1066 	return rc ? rc : count;
1067 }
1068 static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
1069 
1070 static ssize_t show_cpu_address(struct device *dev,
1071 				struct device_attribute *attr, char *buf)
1072 {
1073 	return sysfs_emit(buf, "%d\n", per_cpu(pcpu_devices, dev->id).address);
1074 }
1075 static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
1076 
1077 static struct attribute *cpu_common_attrs[] = {
1078 	&dev_attr_configure.attr,
1079 	&dev_attr_address.attr,
1080 	NULL,
1081 };
1082 
1083 static struct attribute_group cpu_common_attr_group = {
1084 	.attrs = cpu_common_attrs,
1085 };
1086 
1087 bool arch_cpu_is_hotpluggable(int cpu)
1088 {
1089 	return !!cpu;
1090 }
1091 
1092 int arch_register_cpu(int cpu)
1093 {
1094 	struct cpu *c = per_cpu_ptr(&cpu_devices, cpu);
1095 	int rc;
1096 
1097 	c->hotpluggable = arch_cpu_is_hotpluggable(cpu);
1098 	rc = register_cpu(c, cpu);
1099 	if (rc)
1100 		goto out;
1101 	rc = sysfs_create_group(&c->dev.kobj, &cpu_common_attr_group);
1102 	if (rc)
1103 		goto out_cpu;
1104 	rc = topology_cpu_init(c);
1105 	if (rc)
1106 		goto out_topology;
1107 	return 0;
1108 
1109 out_topology:
1110 	sysfs_remove_group(&c->dev.kobj, &cpu_common_attr_group);
1111 out_cpu:
1112 	unregister_cpu(c);
1113 out:
1114 	return rc;
1115 }
1116 
1117 int __ref smp_rescan_cpus(bool early)
1118 {
1119 	struct sclp_core_info *info;
1120 	int nr;
1121 
1122 	info = kzalloc_obj(*info);
1123 	if (!info)
1124 		return -ENOMEM;
1125 	smp_get_core_info(info, 0);
1126 	nr = __smp_rescan_cpus(info, early);
1127 	kfree(info);
1128 	if (nr && !early)
1129 		topology_schedule_update();
1130 	return 0;
1131 }
1132 
1133 static ssize_t __ref rescan_store(struct device *dev,
1134 				  struct device_attribute *attr,
1135 				  const char *buf,
1136 				  size_t count)
1137 {
1138 	int rc;
1139 
1140 	rc = lock_device_hotplug_sysfs();
1141 	if (rc)
1142 		return rc;
1143 	rc = smp_rescan_cpus(false);
1144 	unlock_device_hotplug();
1145 	return rc ? rc : count;
1146 }
1147 static DEVICE_ATTR_WO(rescan);
1148 
1149 static int __init s390_smp_init(void)
1150 {
1151 	struct device *dev_root;
1152 	int rc = 0;
1153 
1154 	dev_root = bus_get_dev_root(&cpu_subsys);
1155 	if (dev_root) {
1156 		rc = device_create_file(dev_root, &dev_attr_rescan);
1157 		put_device(dev_root);
1158 	}
1159 	return rc;
1160 }
1161 subsys_initcall(s390_smp_init);
1162