smp.c 29.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
M
Martin Schwidefsky 已提交
2
 *  SMP related functions
L
Linus Torvalds 已提交
3
 *
4
 *    Copyright IBM Corp. 1999, 2012
M
Martin Schwidefsky 已提交
5 6 7
 *    Author(s): Denis Joseph Barrow,
 *		 Martin Schwidefsky <schwidefsky@de.ibm.com>,
 *		 Heiko Carstens <heiko.carstens@de.ibm.com>,
L
Linus Torvalds 已提交
8
 *
9
 *  based on other smp stuff by
L
Linus Torvalds 已提交
10 11 12
 *    (c) 1995 Alan Cox, CymruNET Ltd  <alan@cymru.net>
 *    (c) 1998 Ingo Molnar
 *
M
Martin Schwidefsky 已提交
13 14 15
 * The code outside of smp.c uses logical cpu numbers, only smp.c does
 * the translation of logical to physical cpu ids. All new code that
 * operates on physical cpu numbers needs to go into smp.c.
L
Linus Torvalds 已提交
16 17
 */

18 19 20
#define KMSG_COMPONENT "cpu"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

21
#include <linux/workqueue.h>
L
Linus Torvalds 已提交
22 23 24
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
A
Alexey Dobriyan 已提交
25
#include <linux/err.h>
L
Linus Torvalds 已提交
26 27 28 29
#include <linux/spinlock.h>
#include <linux/kernel_stat.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
30
#include <linux/irqflags.h>
L
Linus Torvalds 已提交
31
#include <linux/cpu.h>
32
#include <linux/slab.h>
M
Michael Holzheu 已提交
33
#include <linux/crash_dump.h>
34
#include <linux/memblock.h>
35
#include <asm/asm-offsets.h>
36
#include <asm/diag.h>
37 38
#include <asm/switch_to.h>
#include <asm/facility.h>
M
Michael Holzheu 已提交
39
#include <asm/ipl.h>
40
#include <asm/setup.h>
L
Linus Torvalds 已提交
41 42
#include <asm/irq.h>
#include <asm/tlbflush.h>
43
#include <asm/vtimer.h>
M
Michael Holzheu 已提交
44
#include <asm/lowcore.h>
45
#include <asm/sclp.h>
46
#include <asm/vdso.h>
47
#include <asm/debug.h>
48
#include <asm/os_info.h>
49
#include <asm/sigp.h>
50
#include <asm/idle.h>
51
#include "entry.h"
L
Linus Torvalds 已提交
52

M
Martin Schwidefsky 已提交
53 54 55 56 57
enum {
	ec_schedule = 0,
	ec_call_function_single,
	ec_stop_cpu,
};
58

M
Martin Schwidefsky 已提交
59
enum {
60 61 62 63
	CPU_STATE_STANDBY,
	CPU_STATE_CONFIGURED,
};

64 65
static DEFINE_PER_CPU(struct cpu *, cpu_device);

M
Martin Schwidefsky 已提交
66
struct pcpu {
67
	struct lowcore *lowcore;	/* lowcore page(s) for the cpu */
M
Martin Schwidefsky 已提交
68
	unsigned long ec_mask;		/* bit mask for ec_xxx functions */
69
	unsigned long ec_clk;		/* sigp timestamp for ec_xxx */
70 71
	signed char state;		/* physical cpu state */
	signed char polarization;	/* physical polarization */
M
Martin Schwidefsky 已提交
72 73 74
	u16 address;			/* physical cpu address */
};

75
static u8 boot_core_type;
M
Martin Schwidefsky 已提交
76 77
static struct pcpu pcpu_devices[NR_CPUS];

M
Martin Schwidefsky 已提交
78 79 80 81 82 83
unsigned int smp_cpu_mt_shift;
EXPORT_SYMBOL(smp_cpu_mt_shift);

unsigned int smp_cpu_mtid;
EXPORT_SYMBOL(smp_cpu_mtid);

84 85 86 87
#ifdef CONFIG_CRASH_DUMP
__vector128 __initdata boot_cpu_vector_save_area[__NUM_VXRS];
#endif

M
Martin Schwidefsky 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static unsigned int smp_max_threads __initdata = -1U;

static int __init early_nosmt(char *s)
{
	smp_max_threads = 1;
	return 0;
}
early_param("nosmt", early_nosmt);

static int __init early_smt(char *s)
{
	get_option(&s, &smp_max_threads);
	return 0;
}
early_param("smt", early_smt);

104 105 106 107
/*
 * The smp_cpu_state_mutex must be held when changing the state or polarization
 * member of a pcpu data structure within the pcpu_devices arreay.
 */
108
DEFINE_MUTEX(smp_cpu_state_mutex);
109

M
Martin Schwidefsky 已提交
110 111 112
/*
 * Signal processor helper functions.
 */
113
static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm)
114
{
M
Martin Schwidefsky 已提交
115
	int cc;
116

M
Martin Schwidefsky 已提交
117
	while (1) {
118
		cc = __pcpu_sigp(addr, order, parm, NULL);
119
		if (cc != SIGP_CC_BUSY)
M
Martin Schwidefsky 已提交
120 121
			return cc;
		cpu_relax();
122 123 124
	}
}

M
Martin Schwidefsky 已提交
125
static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
H
Heiko Carstens 已提交
126
{
M
Martin Schwidefsky 已提交
127 128 129
	int cc, retry;

	for (retry = 0; ; retry++) {
130
		cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
131
		if (cc != SIGP_CC_BUSY)
M
Martin Schwidefsky 已提交
132 133 134 135 136 137 138 139 140
			break;
		if (retry >= 3)
			udelay(10);
	}
	return cc;
}

static inline int pcpu_stopped(struct pcpu *pcpu)
{
141
	u32 uninitialized_var(status);
142

143
	if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
144
			0, &status) != SIGP_CC_STATUS_STORED)
M
Martin Schwidefsky 已提交
145
		return 0;
146
	return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
M
Martin Schwidefsky 已提交
147 148 149
}

static inline int pcpu_running(struct pcpu *pcpu)
H
Heiko Carstens 已提交
150
{
151
	if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
152
			0, NULL) != SIGP_CC_STATUS_STORED)
M
Martin Schwidefsky 已提交
153
		return 1;
154 155
	/* Status stored condition code is equivalent to cpu not running. */
	return 0;
H
Heiko Carstens 已提交
156 157
}

158
/*
M
Martin Schwidefsky 已提交
159
 * Find struct pcpu by cpu address.
160
 */
M
Martin Schwidefsky 已提交
161
static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
162 163 164
{
	int cpu;

M
Martin Schwidefsky 已提交
165 166 167 168 169 170 171 172 173 174
	for_each_cpu(cpu, mask)
		if (pcpu_devices[cpu].address == address)
			return pcpu_devices + cpu;
	return NULL;
}

static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
{
	int order;

175 176 177
	if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
		return;
	order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
178
	pcpu->ec_clk = get_tod_clock_fast();
M
Martin Schwidefsky 已提交
179 180 181
	pcpu_sigp_retry(pcpu, order, 0);
}

182 183 184
#define ASYNC_FRAME_OFFSET (ASYNC_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
#define PANIC_FRAME_OFFSET (PAGE_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)

185
static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
M
Martin Schwidefsky 已提交
186
{
187
	unsigned long async_stack, panic_stack;
188
	struct lowcore *lc;
M
Martin Schwidefsky 已提交
189 190

	if (pcpu != &pcpu_devices[0]) {
191
		pcpu->lowcore =	(struct lowcore *)
M
Martin Schwidefsky 已提交
192
			__get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
193 194 195
		async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
		panic_stack = __get_free_page(GFP_KERNEL);
		if (!pcpu->lowcore || !panic_stack || !async_stack)
M
Martin Schwidefsky 已提交
196
			goto out;
197 198 199
	} else {
		async_stack = pcpu->lowcore->async_stack - ASYNC_FRAME_OFFSET;
		panic_stack = pcpu->lowcore->panic_stack - PANIC_FRAME_OFFSET;
200
	}
M
Martin Schwidefsky 已提交
201 202 203
	lc = pcpu->lowcore;
	memcpy(lc, &S390_lowcore, 512);
	memset((char *) lc + 512, 0, sizeof(*lc) - 512);
204 205
	lc->async_stack = async_stack + ASYNC_FRAME_OFFSET;
	lc->panic_stack = panic_stack + PANIC_FRAME_OFFSET;
M
Martin Schwidefsky 已提交
206
	lc->cpu_nr = cpu;
207
	lc->spinlock_lockval = arch_spin_lockval(cpu);
208 209 210
	if (MACHINE_HAS_VX)
		lc->vector_save_area_addr =
			(unsigned long) &lc->vector_save_area;
M
Martin Schwidefsky 已提交
211 212 213
	if (vdso_alloc_per_cpu(lc))
		goto out;
	lowcore_ptr[cpu] = lc;
214
	pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
M
Martin Schwidefsky 已提交
215 216 217
	return 0;
out:
	if (pcpu != &pcpu_devices[0]) {
218 219
		free_page(panic_stack);
		free_pages(async_stack, ASYNC_ORDER);
M
Martin Schwidefsky 已提交
220 221 222
		free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
	}
	return -ENOMEM;
223 224
}

225 226
#ifdef CONFIG_HOTPLUG_CPU

M
Martin Schwidefsky 已提交
227
static void pcpu_free_lowcore(struct pcpu *pcpu)
228
{
229
	pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
M
Martin Schwidefsky 已提交
230 231
	lowcore_ptr[pcpu - pcpu_devices] = NULL;
	vdso_free_per_cpu(pcpu->lowcore);
232 233 234 235 236
	if (pcpu == &pcpu_devices[0])
		return;
	free_page(pcpu->lowcore->panic_stack-PANIC_FRAME_OFFSET);
	free_pages(pcpu->lowcore->async_stack-ASYNC_FRAME_OFFSET, ASYNC_ORDER);
	free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
M
Martin Schwidefsky 已提交
237 238
}

239 240
#endif /* CONFIG_HOTPLUG_CPU */

M
Martin Schwidefsky 已提交
241 242
static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
{
243
	struct lowcore *lc = pcpu->lowcore;
M
Martin Schwidefsky 已提交
244

245
	cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
246
	cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
M
Martin Schwidefsky 已提交
247
	lc->cpu_nr = cpu;
248
	lc->spinlock_lockval = arch_spin_lockval(cpu);
M
Martin Schwidefsky 已提交
249 250 251 252 253 254 255 256 257 258 259 260
	lc->percpu_offset = __per_cpu_offset[cpu];
	lc->kernel_asce = S390_lowcore.kernel_asce;
	lc->machine_flags = S390_lowcore.machine_flags;
	lc->user_timer = lc->system_timer = lc->steal_timer = 0;
	__ctl_store(lc->cregs_save_area, 0, 15);
	save_access_regs((unsigned int *) lc->access_regs_save_area);
	memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
	       MAX_FACILITY_BIT/8);
}

static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
{
261
	struct lowcore *lc = pcpu->lowcore;
M
Martin Schwidefsky 已提交
262 263
	struct thread_info *ti = task_thread_info(tsk);

264 265
	lc->kernel_stack = (unsigned long) task_stack_page(tsk)
		+ THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
M
Martin Schwidefsky 已提交
266 267
	lc->thread_info = (unsigned long) task_thread_info(tsk);
	lc->current_task = (unsigned long) tsk;
268 269
	lc->lpp = LPP_MAGIC;
	lc->current_pid = tsk->pid;
M
Martin Schwidefsky 已提交
270 271 272 273 274 275 276
	lc->user_timer = ti->user_timer;
	lc->system_timer = ti->system_timer;
	lc->steal_timer = 0;
}

static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
{
277
	struct lowcore *lc = pcpu->lowcore;
M
Martin Schwidefsky 已提交
278 279 280 281 282

	lc->restart_stack = lc->kernel_stack;
	lc->restart_fn = (unsigned long) func;
	lc->restart_data = (unsigned long) data;
	lc->restart_source = -1UL;
283
	pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
M
Martin Schwidefsky 已提交
284 285 286 287 288 289 290 291
}

/*
 * Call function via PSW restart on pcpu and stop the current cpu.
 */
static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
			  void *data, unsigned long stack)
{
292
	struct lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
293
	unsigned long source_cpu = stap();
M
Martin Schwidefsky 已提交
294

295
	__load_psw_mask(PSW_KERNEL_BITS);
296
	if (pcpu->address == source_cpu)
M
Martin Schwidefsky 已提交
297 298
		func(data);	/* should not return */
	/* Stop target cpu (if func returns this stops the current cpu). */
299
	pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
M
Martin Schwidefsky 已提交
300
	/* Restart func on the target cpu and stop the current cpu. */
301 302 303 304
	mem_assign_absolute(lc->restart_stack, stack);
	mem_assign_absolute(lc->restart_fn, (unsigned long) func);
	mem_assign_absolute(lc->restart_data, (unsigned long) data);
	mem_assign_absolute(lc->restart_source, source_cpu);
M
Martin Schwidefsky 已提交
305
	asm volatile(
306
		"0:	sigp	0,%0,%2	# sigp restart to target cpu\n"
M
Martin Schwidefsky 已提交
307
		"	brc	2,0b	# busy, try again\n"
308
		"1:	sigp	0,%1,%3	# sigp stop to current cpu\n"
M
Martin Schwidefsky 已提交
309
		"	brc	2,1b	# busy, try again\n"
310
		: : "d" (pcpu->address), "d" (source_cpu),
311 312
		    "K" (SIGP_RESTART), "K" (SIGP_STOP)
		: "0", "1", "cc");
M
Martin Schwidefsky 已提交
313 314 315
	for (;;) ;
}

M
Martin Schwidefsky 已提交
316 317 318 319 320 321 322 323 324
/*
 * Enable additional logical cpus for multi-threading.
 */
static int pcpu_set_smt(unsigned int mtid)
{
	int cc;

	if (smp_cpu_mtid == mtid)
		return 0;
325
	cc = __pcpu_sigp(0, SIGP_SET_MULTI_THREADING, mtid, NULL);
M
Martin Schwidefsky 已提交
326 327 328 329 330 331 332 333 334 335
	if (cc == 0) {
		smp_cpu_mtid = mtid;
		smp_cpu_mt_shift = 0;
		while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
			smp_cpu_mt_shift++;
		pcpu_devices[0].address = stap();
	}
	return cc;
}

M
Martin Schwidefsky 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/*
 * Call function on an online CPU.
 */
void smp_call_online_cpu(void (*func)(void *), void *data)
{
	struct pcpu *pcpu;

	/* Use the current cpu if it is online. */
	pcpu = pcpu_find_address(cpu_online_mask, stap());
	if (!pcpu)
		/* Use the first online cpu. */
		pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
	pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
}

/*
 * Call function on the ipl CPU.
 */
void smp_call_ipl_cpu(void (*func)(void *), void *data)
{
356
	pcpu_delegate(&pcpu_devices[0], func, data,
357 358
		      pcpu_devices->lowcore->panic_stack -
		      PANIC_FRAME_OFFSET + PAGE_SIZE);
M
Martin Schwidefsky 已提交
359 360 361 362 363 364 365 366 367 368
}

int smp_find_processor_id(u16 address)
{
	int cpu;

	for_each_present_cpu(cpu)
		if (pcpu_devices[cpu].address == address)
			return cpu;
	return -1;
369 370
}

371
bool arch_vcpu_is_preempted(int cpu)
372
{
373 374 375 376 377
	if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu))
		return false;
	if (pcpu_running(pcpu_devices + cpu))
		return false;
	return true;
M
Martin Schwidefsky 已提交
378
}
379
EXPORT_SYMBOL(arch_vcpu_is_preempted);
M
Martin Schwidefsky 已提交
380 381

void smp_yield_cpu(int cpu)
382
{
383
	if (MACHINE_HAS_DIAG9C) {
384
		diag_stat_inc_norecursion(DIAG_STAT_X09C);
M
Martin Schwidefsky 已提交
385 386
		asm volatile("diag %0,0,0x9c"
			     : : "d" (pcpu_devices[cpu].address));
387
	} else if (MACHINE_HAS_DIAG44) {
388
		diag_stat_inc_norecursion(DIAG_STAT_X044);
M
Martin Schwidefsky 已提交
389
		asm volatile("diag 0,0,0x44");
390
	}
M
Martin Schwidefsky 已提交
391 392 393 394 395 396
}

/*
 * Send cpus emergency shutdown signal. This gives the cpus the
 * opportunity to complete outstanding interrupts.
 */
397
static void smp_emergency_stop(cpumask_t *cpumask)
M
Martin Schwidefsky 已提交
398 399 400 401
{
	u64 end;
	int cpu;

402
	end = get_tod_clock() + (1000000UL << 12);
M
Martin Schwidefsky 已提交
403 404 405
	for_each_cpu(cpu, cpumask) {
		struct pcpu *pcpu = pcpu_devices + cpu;
		set_bit(ec_stop_cpu, &pcpu->ec_mask);
406 407
		while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
				   0, NULL) == SIGP_CC_BUSY &&
408
		       get_tod_clock() < end)
M
Martin Schwidefsky 已提交
409 410
			cpu_relax();
	}
411
	while (get_tod_clock() < end) {
M
Martin Schwidefsky 已提交
412 413 414 415 416
		for_each_cpu(cpu, cpumask)
			if (pcpu_stopped(pcpu_devices + cpu))
				cpumask_clear_cpu(cpu, cpumask);
		if (cpumask_empty(cpumask))
			break;
417
		cpu_relax();
M
Martin Schwidefsky 已提交
418
	}
419 420
}

M
Martin Schwidefsky 已提交
421 422 423
/*
 * Stop all cpus but the current one.
 */
424
void smp_send_stop(void)
L
Linus Torvalds 已提交
425
{
426 427
	cpumask_t cpumask;
	int cpu;
L
Linus Torvalds 已提交
428

429
	/* Disable all interrupts/machine checks */
430
	__load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
431
	trace_hardirqs_off();
L
Linus Torvalds 已提交
432

433
	debug_set_critical();
434 435 436
	cpumask_copy(&cpumask, cpu_online_mask);
	cpumask_clear_cpu(smp_processor_id(), &cpumask);

M
Martin Schwidefsky 已提交
437 438
	if (oops_in_progress)
		smp_emergency_stop(&cpumask);
L
Linus Torvalds 已提交
439

440 441
	/* stop all processors */
	for_each_cpu(cpu, &cpumask) {
M
Martin Schwidefsky 已提交
442
		struct pcpu *pcpu = pcpu_devices + cpu;
443
		pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
M
Martin Schwidefsky 已提交
444
		while (!pcpu_stopped(pcpu))
H
Heiko Carstens 已提交
445 446 447 448
			cpu_relax();
	}
}

L
Linus Torvalds 已提交
449 450 451 452
/*
 * This is the main routine where commands issued by other
 * cpus are handled.
 */
453
static void smp_handle_ext_call(void)
L
Linus Torvalds 已提交
454
{
455
	unsigned long bits;
L
Linus Torvalds 已提交
456

457 458
	/* handle bit signal external calls */
	bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
459 460
	if (test_bit(ec_stop_cpu, &bits))
		smp_stop_cpu();
461 462
	if (test_bit(ec_schedule, &bits))
		scheduler_ipi();
463 464
	if (test_bit(ec_call_function_single, &bits))
		generic_smp_call_function_single_interrupt();
465
}
466

467 468 469 470 471
static void do_ext_call_interrupt(struct ext_code ext_code,
				  unsigned int param32, unsigned long param64)
{
	inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
	smp_handle_ext_call();
L
Linus Torvalds 已提交
472 473
}

474
void arch_send_call_function_ipi_mask(const struct cpumask *mask)
475 476 477
{
	int cpu;

478
	for_each_cpu(cpu, mask)
479
		pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
480 481 482 483
}

void arch_send_call_function_single_ipi(int cpu)
{
M
Martin Schwidefsky 已提交
484
	pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
485 486
}

L
Linus Torvalds 已提交
487 488 489 490 491 492 493
/*
 * this function sends a 'reschedule' IPI to another CPU.
 * it goes straight through and wastes no time serializing
 * anything. Worst case is that we lose a reschedule ...
 */
void smp_send_reschedule(int cpu)
{
M
Martin Schwidefsky 已提交
494
	pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
L
Linus Torvalds 已提交
495 496 497 498 499
}

/*
 * parameter area for the set/clear control bit callbacks
 */
500
struct ec_creg_mask_parms {
M
Martin Schwidefsky 已提交
501 502 503
	unsigned long orval;
	unsigned long andval;
	int cr;
504
};
L
Linus Torvalds 已提交
505 506 507 508

/*
 * callback for setting/clearing control bits
 */
509 510
static void smp_ctl_bit_callback(void *info)
{
511
	struct ec_creg_mask_parms *pp = info;
L
Linus Torvalds 已提交
512
	unsigned long cregs[16];
513

514
	__ctl_store(cregs, 0, 15);
M
Martin Schwidefsky 已提交
515
	cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
516
	__ctl_load(cregs, 0, 15);
L
Linus Torvalds 已提交
517 518 519 520 521
}

/*
 * Set a bit in a control register of all cpus
 */
522 523
void smp_ctl_set_bit(int cr, int bit)
{
M
Martin Schwidefsky 已提交
524
	struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
L
Linus Torvalds 已提交
525

526
	on_each_cpu(smp_ctl_bit_callback, &parms, 1);
L
Linus Torvalds 已提交
527
}
528
EXPORT_SYMBOL(smp_ctl_set_bit);
L
Linus Torvalds 已提交
529 530 531 532

/*
 * Clear a bit in a control register of all cpus
 */
533 534
void smp_ctl_clear_bit(int cr, int bit)
{
M
Martin Schwidefsky 已提交
535
	struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
L
Linus Torvalds 已提交
536

537
	on_each_cpu(smp_ctl_bit_callback, &parms, 1);
L
Linus Torvalds 已提交
538
}
539
EXPORT_SYMBOL(smp_ctl_clear_bit);
L
Linus Torvalds 已提交
540

541
#ifdef CONFIG_CRASH_DUMP
M
Michael Holzheu 已提交
542

543 544
int smp_store_status(int cpu)
{
545 546
	struct pcpu *pcpu = pcpu_devices + cpu;
	unsigned long pa;
547

548 549 550
	pa = __pa(&pcpu->lowcore->floating_pt_save_area);
	if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_STATUS_AT_ADDRESS,
			      pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
551 552 553
		return -EIO;
	if (!MACHINE_HAS_VX)
		return 0;
554 555 556 557
	pa = __pa(pcpu->lowcore->vector_save_area_addr);
	if (__pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
			      pa) != SIGP_CC_ORDER_CODE_ACCEPTED)
		return -EIO;
558 559 560
	return 0;
}

M
Martin Schwidefsky 已提交
561 562 563 564 565 566 567 568
/*
 * Collect CPU state of the previous, crashed system.
 * There are four cases:
 * 1) standard zfcp dump
 *    condition: OLDMEM_BASE == NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
 *    The state for all CPUs except the boot CPU needs to be collected
 *    with sigp stop-and-store-status. The boot CPU state is located in
 *    the absolute lowcore of the memory stored in the HSA. The zcore code
569
 *    will copy the boot CPU state from the HSA.
M
Martin Schwidefsky 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583
 * 2) stand-alone kdump for SCSI (zfcp dump with swapped memory)
 *    condition: OLDMEM_BASE != NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
 *    The state for all CPUs except the boot CPU needs to be collected
 *    with sigp stop-and-store-status. The firmware or the boot-loader
 *    stored the registers of the boot CPU in the absolute lowcore in the
 *    memory of the old system.
 * 3) kdump and the old kernel did not store the CPU state,
 *    or stand-alone kdump for DASD
 *    condition: OLDMEM_BASE != NULL && !is_kdump_kernel()
 *    The state for all CPUs except the boot CPU needs to be collected
 *    with sigp stop-and-store-status. The kexec code or the boot-loader
 *    stored the registers of the boot CPU in the memory of the old system.
 * 4) kdump and the old kernel stored the CPU state
 *    condition: OLDMEM_BASE != NULL && is_kdump_kernel()
584 585
 *    This case does not exist for s390 anymore, setup_arch explicitly
 *    deactivates the elfcorehdr= kernel parameter
M
Martin Schwidefsky 已提交
586
 */
587
static __init void smp_save_cpu_vxrs(struct save_area *sa, u16 addr,
588 589 590 591 592 593 594 595
				     bool is_boot_cpu, unsigned long page)
{
	__vector128 *vxrs = (__vector128 *) page;

	if (is_boot_cpu)
		vxrs = boot_cpu_vector_save_area;
	else
		__pcpu_sigp_relax(addr, SIGP_STORE_ADDITIONAL_STATUS, page);
596
	save_area_add_vxrs(sa, vxrs);
597 598
}

599
static __init void smp_save_cpu_regs(struct save_area *sa, u16 addr,
600 601 602 603 604 605 606 607
				     bool is_boot_cpu, unsigned long page)
{
	void *regs = (void *) page;

	if (is_boot_cpu)
		copy_oldmem_kernel(regs, (void *) __LC_FPREGS_SAVE_AREA, 512);
	else
		__pcpu_sigp_relax(addr, SIGP_STORE_STATUS_AT_ADDRESS, page);
608
	save_area_add_regs(sa, regs);
609 610
}

611
void __init smp_save_dump_cpus(void)
M
Martin Schwidefsky 已提交
612
{
613 614
	int addr, boot_cpu_addr, max_cpu_addr;
	struct save_area *sa;
615
	unsigned long page;
616
	bool is_boot_cpu;
M
Martin Schwidefsky 已提交
617 618 619 620

	if (!(OLDMEM_BASE || ipl_info.type == IPL_TYPE_FCP_DUMP))
		/* No previous system present, normal boot. */
		return;
621 622
	/* Allocate a page as dumping area for the store status sigps */
	page = memblock_alloc_base(PAGE_SIZE, PAGE_SIZE, 1UL << 31);
M
Martin Schwidefsky 已提交
623
	/* Set multi-threading state to the previous system. */
624
	pcpu_set_smt(sclp.mtid_prev);
625
	boot_cpu_addr = stap();
626 627
	max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
	for (addr = 0; addr <= max_cpu_addr; addr++) {
628
		if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0) ==
629 630 631
		    SIGP_CC_NOT_OPERATIONAL)
			continue;
		is_boot_cpu = (addr == boot_cpu_addr);
632 633 634 635
		/* Allocate save area */
		sa = save_area_alloc(is_boot_cpu);
		if (!sa)
			panic("could not allocate memory for save area\n");
636 637
		if (MACHINE_HAS_VX)
			/* Get the vector registers */
638
			smp_save_cpu_vxrs(sa, addr, is_boot_cpu, page);
639 640 641 642 643 644 645 646
		/*
		 * For a zfcp dump OLDMEM_BASE == NULL and the registers
		 * of the boot CPU are stored in the HSA. To retrieve
		 * these registers an SCLP request is required which is
		 * done by drivers/s390/char/zcore.c:init_cpu_info()
		 */
		if (!is_boot_cpu || OLDMEM_BASE)
			/* Get the CPU registers */
647
			smp_save_cpu_regs(sa, addr, is_boot_cpu, page);
M
Martin Schwidefsky 已提交
648
	}
649
	memblock_free(page, PAGE_SIZE);
650 651
	diag308_reset();
	pcpu_set_smt(0);
652
}
653
#endif /* CONFIG_CRASH_DUMP */
654

655 656 657 658 659 660 661 662 663 664
void smp_cpu_set_polarization(int cpu, int val)
{
	pcpu_devices[cpu].polarization = val;
}

int smp_cpu_get_polarization(int cpu)
{
	return pcpu_devices[cpu].polarization;
}

665
static struct sclp_core_info *smp_get_core_info(void)
666
{
M
Martin Schwidefsky 已提交
667
	static int use_sigp_detection;
668
	struct sclp_core_info *info;
M
Martin Schwidefsky 已提交
669 670 671
	int address;

	info = kzalloc(sizeof(*info), GFP_KERNEL);
672
	if (info && (use_sigp_detection || sclp_get_core_info(info))) {
M
Martin Schwidefsky 已提交
673
		use_sigp_detection = 1;
674
		for (address = 0;
675
		     address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
M
Martin Schwidefsky 已提交
676
		     address += (1U << smp_cpu_mt_shift)) {
677
			if (__pcpu_sigp_relax(address, SIGP_SENSE, 0) ==
678
			    SIGP_CC_NOT_OPERATIONAL)
M
Martin Schwidefsky 已提交
679
				continue;
680
			info->core[info->configured].core_id =
M
Martin Schwidefsky 已提交
681
				address >> smp_cpu_mt_shift;
M
Martin Schwidefsky 已提交
682 683 684
			info->configured++;
		}
		info->combined = info->configured;
685
	}
M
Martin Schwidefsky 已提交
686
	return info;
687 688
}

689
static int smp_add_present_cpu(int cpu);
M
Martin Schwidefsky 已提交
690

691
static int __smp_rescan_cpus(struct sclp_core_info *info, int sysfs_add)
692
{
M
Martin Schwidefsky 已提交
693
	struct pcpu *pcpu;
694
	cpumask_t avail;
M
Martin Schwidefsky 已提交
695 696
	int cpu, nr, i, j;
	u16 address;
697

M
Martin Schwidefsky 已提交
698
	nr = 0;
699
	cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
M
Martin Schwidefsky 已提交
700 701
	cpu = cpumask_first(&avail);
	for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
702
		if (sclp.has_core_type && info->core[i].type != boot_core_type)
M
Martin Schwidefsky 已提交
703
			continue;
704
		address = info->core[i].core_id << smp_cpu_mt_shift;
M
Martin Schwidefsky 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		for (j = 0; j <= smp_cpu_mtid; j++) {
			if (pcpu_find_address(cpu_present_mask, address + j))
				continue;
			pcpu = pcpu_devices + cpu;
			pcpu->address = address + j;
			pcpu->state =
				(cpu >= info->configured*(smp_cpu_mtid + 1)) ?
				CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
			smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
			set_cpu_present(cpu, true);
			if (sysfs_add && smp_add_present_cpu(cpu) != 0)
				set_cpu_present(cpu, false);
			else
				nr++;
			cpu = cpumask_next(cpu, &avail);
			if (cpu >= nr_cpu_ids)
				break;
		}
M
Martin Schwidefsky 已提交
723 724
	}
	return nr;
L
Linus Torvalds 已提交
725 726
}

727 728
static void __init smp_detect_cpus(void)
{
M
Martin Schwidefsky 已提交
729
	unsigned int cpu, mtid, c_cpus, s_cpus;
730
	struct sclp_core_info *info;
M
Martin Schwidefsky 已提交
731
	u16 address;
732

M
Martin Schwidefsky 已提交
733
	/* Get CPU information */
734
	info = smp_get_core_info();
735 736
	if (!info)
		panic("smp_detect_cpus failed to allocate memory\n");
M
Martin Schwidefsky 已提交
737 738

	/* Find boot CPU type */
739
	if (sclp.has_core_type) {
M
Martin Schwidefsky 已提交
740 741
		address = stap();
		for (cpu = 0; cpu < info->combined; cpu++)
742
			if (info->core[cpu].core_id == address) {
M
Martin Schwidefsky 已提交
743
				/* The boot cpu dictates the cpu type. */
744
				boot_core_type = info->core[cpu].type;
M
Martin Schwidefsky 已提交
745 746 747 748
				break;
			}
		if (cpu >= info->combined)
			panic("Could not find boot CPU type");
749
	}
M
Martin Schwidefsky 已提交
750 751

	/* Set multi-threading state for the current system */
752
	mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
M
Martin Schwidefsky 已提交
753 754 755 756
	mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
	pcpu_set_smt(mtid);

	/* Print number of CPUs */
M
Martin Schwidefsky 已提交
757
	c_cpus = s_cpus = 0;
758
	for (cpu = 0; cpu < info->combined; cpu++) {
759 760
		if (sclp.has_core_type &&
		    info->core[cpu].type != boot_core_type)
761
			continue;
M
Martin Schwidefsky 已提交
762 763 764 765
		if (cpu < info->configured)
			c_cpus += smp_cpu_mtid + 1;
		else
			s_cpus += smp_cpu_mtid + 1;
766
	}
767
	pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
M
Martin Schwidefsky 已提交
768 769

	/* Add CPUs present at boot */
770
	get_online_cpus();
M
Martin Schwidefsky 已提交
771
	__smp_rescan_cpus(info, 0);
772
	put_online_cpus();
M
Martin Schwidefsky 已提交
773
	kfree(info);
774 775
}

L
Linus Torvalds 已提交
776
/*
777
 *	Activate a secondary processor.
L
Linus Torvalds 已提交
778
 */
779
static void smp_start_secondary(void *cpuvoid)
L
Linus Torvalds 已提交
780
{
781
	S390_lowcore.last_update_clock = get_tod_clock();
M
Martin Schwidefsky 已提交
782 783 784 785 786 787
	S390_lowcore.restart_stack = (unsigned long) restart_stack;
	S390_lowcore.restart_fn = (unsigned long) do_restart;
	S390_lowcore.restart_data = 0;
	S390_lowcore.restart_source = -1UL;
	restore_access_regs(S390_lowcore.access_regs_save_area);
	__ctl_load(S390_lowcore.cregs_save_area, 0, 15);
788
	__load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
789
	cpu_init();
790
	preempt_disable();
791
	init_cpu_timer();
792
	vtime_init();
H
Heiko Carstens 已提交
793
	pfault_init();
794
	notify_cpu_starting(smp_processor_id());
795
	set_cpu_online(smp_processor_id(), true);
796
	inc_irq_stat(CPU_RST);
L
Linus Torvalds 已提交
797
	local_irq_enable();
798
	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
L
Linus Torvalds 已提交
799 800 801
}

/* Upping and downing of CPUs */
802
int __cpu_up(unsigned int cpu, struct task_struct *tidle)
L
Linus Torvalds 已提交
803
{
M
Martin Schwidefsky 已提交
804
	struct pcpu *pcpu;
M
Martin Schwidefsky 已提交
805
	int base, i, rc;
L
Linus Torvalds 已提交
806

M
Martin Schwidefsky 已提交
807 808
	pcpu = pcpu_devices + cpu;
	if (pcpu->state != CPU_STATE_CONFIGURED)
809
		return -EIO;
M
Martin Schwidefsky 已提交
810 811 812 813 814 815 816 817 818 819 820 821
	base = cpu - (cpu % (smp_cpu_mtid + 1));
	for (i = 0; i <= smp_cpu_mtid; i++) {
		if (base + i < nr_cpu_ids)
			if (cpu_online(base + i))
				break;
	}
	/*
	 * If this is the first CPU of the core to get online
	 * do an initial CPU reset.
	 */
	if (i > smp_cpu_mtid &&
	    pcpu_sigp_retry(pcpu_devices + base, SIGP_INITIAL_CPU_RESET, 0) !=
822
	    SIGP_CC_ORDER_CODE_ACCEPTED)
823
		return -EIO;
824

M
Martin Schwidefsky 已提交
825 826 827 828
	rc = pcpu_alloc_lowcore(pcpu, cpu);
	if (rc)
		return rc;
	pcpu_prepare_secondary(pcpu, cpu);
829
	pcpu_attach_task(pcpu, tidle);
M
Martin Schwidefsky 已提交
830
	pcpu_start_fn(pcpu, smp_start_secondary, NULL);
831
	/* Wait until cpu puts itself in the online & active maps */
832
	while (!cpu_online(cpu))
L
Linus Torvalds 已提交
833 834 835 836
		cpu_relax();
	return 0;
}

837
static unsigned int setup_possible_cpus __initdata;
838

839 840 841
static int __init _setup_possible_cpus(char *s)
{
	get_option(&s, &setup_possible_cpus);
842 843
	return 0;
}
844
early_param("possible_cpus", _setup_possible_cpus);
845

846 847
#ifdef CONFIG_HOTPLUG_CPU

848
int __cpu_disable(void)
L
Linus Torvalds 已提交
849
{
M
Martin Schwidefsky 已提交
850
	unsigned long cregs[16];
L
Linus Torvalds 已提交
851

852 853
	/* Handle possible pending IPIs */
	smp_handle_ext_call();
M
Martin Schwidefsky 已提交
854 855
	set_cpu_online(smp_processor_id(), false);
	/* Disable pseudo page faults on this cpu. */
H
Heiko Carstens 已提交
856
	pfault_fini();
M
Martin Schwidefsky 已提交
857 858 859 860 861 862
	/* Disable interrupt sources via control register. */
	__ctl_store(cregs, 0, 15);
	cregs[0]  &= ~0x0000ee70UL;	/* disable all external interrupts */
	cregs[6]  &= ~0xff000000UL;	/* disable all I/O interrupts */
	cregs[14] &= ~0x1f000000UL;	/* disable most machine checks */
	__ctl_load(cregs, 0, 15);
863
	clear_cpu_flag(CIF_NOHZ_DELAY);
L
Linus Torvalds 已提交
864 865 866
	return 0;
}

867
void __cpu_die(unsigned int cpu)
L
Linus Torvalds 已提交
868
{
M
Martin Schwidefsky 已提交
869 870
	struct pcpu *pcpu;

L
Linus Torvalds 已提交
871
	/* Wait until target cpu is down */
M
Martin Schwidefsky 已提交
872 873
	pcpu = pcpu_devices + cpu;
	while (!pcpu_stopped(pcpu))
L
Linus Torvalds 已提交
874
		cpu_relax();
M
Martin Schwidefsky 已提交
875
	pcpu_free_lowcore(pcpu);
876
	cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
877
	cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
L
Linus Torvalds 已提交
878 879
}

880
void __noreturn cpu_die(void)
L
Linus Torvalds 已提交
881 882
{
	idle_task_exit();
883
	pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
M
Martin Schwidefsky 已提交
884
	for (;;) ;
L
Linus Torvalds 已提交
885 886
}

887 888
#endif /* CONFIG_HOTPLUG_CPU */

889 890
void __init smp_fill_possible_mask(void)
{
891
	unsigned int possible, sclp_max, cpu;
892

893 894
	sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
	sclp_max = min(smp_max_threads, sclp_max);
D
Dan Carpenter 已提交
895
	sclp_max = (sclp.max_cores * sclp_max) ?: nr_cpu_ids;
896
	possible = setup_possible_cpus ?: nr_cpu_ids;
897
	possible = min(possible, sclp_max);
898 899 900 901
	for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
		set_cpu_possible(cpu, true);
}

L
Linus Torvalds 已提交
902 903
void __init smp_prepare_cpus(unsigned int max_cpus)
{
904
	/* request the 0x1201 emergency signal external interrupt */
905
	if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
906
		panic("Couldn't request external interrupt 0x1201");
907
	/* request the 0x1202 external call external interrupt */
908
	if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
909
		panic("Couldn't request external interrupt 0x1202");
M
Martin Schwidefsky 已提交
910
	smp_detect_cpus();
L
Linus Torvalds 已提交
911 912
}

H
Heiko Carstens 已提交
913
void __init smp_prepare_boot_cpu(void)
L
Linus Torvalds 已提交
914
{
M
Martin Schwidefsky 已提交
915 916 917
	struct pcpu *pcpu = pcpu_devices;

	pcpu->state = CPU_STATE_CONFIGURED;
M
Martin Schwidefsky 已提交
918
	pcpu->address = stap();
919
	pcpu->lowcore = (struct lowcore *)(unsigned long) store_prefix();
L
Linus Torvalds 已提交
920
	S390_lowcore.percpu_offset = __per_cpu_offset[0];
921
	smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
M
Martin Schwidefsky 已提交
922 923
	set_cpu_present(0, true);
	set_cpu_online(0, true);
L
Linus Torvalds 已提交
924 925
}

H
Heiko Carstens 已提交
926
void __init smp_cpus_done(unsigned int max_cpus)
L
Linus Torvalds 已提交
927 928 929
{
}

930 931 932
void __init smp_setup_processor_id(void)
{
	S390_lowcore.cpu_nr = 0;
933
	S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
934 935
}

L
Linus Torvalds 已提交
936 937 938 939 940 941 942 943
/*
 * the frequency of the profiling timer can be changed
 * by writing a multiplier value into /proc/profile.
 *
 * usually you want to run this on all CPUs ;)
 */
int setup_profiling_timer(unsigned int multiplier)
{
944
	return 0;
L
Linus Torvalds 已提交
945 946
}

947
#ifdef CONFIG_HOTPLUG_CPU
948
static ssize_t cpu_configure_show(struct device *dev,
M
Martin Schwidefsky 已提交
949
				  struct device_attribute *attr, char *buf)
950 951 952 953
{
	ssize_t count;

	mutex_lock(&smp_cpu_state_mutex);
M
Martin Schwidefsky 已提交
954
	count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
955 956 957 958
	mutex_unlock(&smp_cpu_state_mutex);
	return count;
}

959
static ssize_t cpu_configure_store(struct device *dev,
M
Martin Schwidefsky 已提交
960 961
				   struct device_attribute *attr,
				   const char *buf, size_t count)
962
{
M
Martin Schwidefsky 已提交
963
	struct pcpu *pcpu;
M
Martin Schwidefsky 已提交
964
	int cpu, val, rc, i;
965 966 967 968 969 970
	char delim;

	if (sscanf(buf, "%d %c", &val, &delim) != 1)
		return -EINVAL;
	if (val != 0 && val != 1)
		return -EINVAL;
971
	get_online_cpus();
H
Heiko Carstens 已提交
972
	mutex_lock(&smp_cpu_state_mutex);
973
	rc = -EBUSY;
974
	/* disallow configuration changes of online cpus and cpu 0 */
M
Martin Schwidefsky 已提交
975
	cpu = dev->id;
M
Martin Schwidefsky 已提交
976 977
	cpu -= cpu % (smp_cpu_mtid + 1);
	if (cpu == 0)
978
		goto out;
M
Martin Schwidefsky 已提交
979 980 981
	for (i = 0; i <= smp_cpu_mtid; i++)
		if (cpu_online(cpu + i))
			goto out;
M
Martin Schwidefsky 已提交
982
	pcpu = pcpu_devices + cpu;
983 984 985
	rc = 0;
	switch (val) {
	case 0:
M
Martin Schwidefsky 已提交
986 987
		if (pcpu->state != CPU_STATE_CONFIGURED)
			break;
988
		rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
M
Martin Schwidefsky 已提交
989 990
		if (rc)
			break;
M
Martin Schwidefsky 已提交
991 992 993 994 995 996 997
		for (i = 0; i <= smp_cpu_mtid; i++) {
			if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
				continue;
			pcpu[i].state = CPU_STATE_STANDBY;
			smp_cpu_set_polarization(cpu + i,
						 POLARIZATION_UNKNOWN);
		}
M
Martin Schwidefsky 已提交
998
		topology_expect_change();
999 1000
		break;
	case 1:
M
Martin Schwidefsky 已提交
1001 1002
		if (pcpu->state != CPU_STATE_STANDBY)
			break;
1003
		rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
M
Martin Schwidefsky 已提交
1004 1005
		if (rc)
			break;
M
Martin Schwidefsky 已提交
1006 1007 1008 1009 1010 1011 1012
		for (i = 0; i <= smp_cpu_mtid; i++) {
			if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
				continue;
			pcpu[i].state = CPU_STATE_CONFIGURED;
			smp_cpu_set_polarization(cpu + i,
						 POLARIZATION_UNKNOWN);
		}
M
Martin Schwidefsky 已提交
1013
		topology_expect_change();
1014 1015 1016 1017 1018 1019
		break;
	default:
		break;
	}
out:
	mutex_unlock(&smp_cpu_state_mutex);
H
Heiko Carstens 已提交
1020
	put_online_cpus();
1021 1022
	return rc ? rc : count;
}
1023
static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
1024 1025
#endif /* CONFIG_HOTPLUG_CPU */

1026 1027
static ssize_t show_cpu_address(struct device *dev,
				struct device_attribute *attr, char *buf)
1028
{
M
Martin Schwidefsky 已提交
1029
	return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
1030
}
1031
static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
1032 1033 1034

static struct attribute *cpu_common_attrs[] = {
#ifdef CONFIG_HOTPLUG_CPU
1035
	&dev_attr_configure.attr,
1036
#endif
1037
	&dev_attr_address.attr,
1038 1039 1040 1041 1042 1043
	NULL,
};

static struct attribute_group cpu_common_attr_group = {
	.attrs = cpu_common_attrs,
};
L
Linus Torvalds 已提交
1044

1045
static struct attribute *cpu_online_attrs[] = {
1046 1047
	&dev_attr_idle_count.attr,
	&dev_attr_idle_time_us.attr,
1048 1049 1050
	NULL,
};

1051 1052
static struct attribute_group cpu_online_attr_group = {
	.attrs = cpu_online_attrs,
1053 1054
};

1055 1056
static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
			  void *hcpu)
1057 1058
{
	unsigned int cpu = (unsigned int)(long)hcpu;
1059
	struct device *s = &per_cpu(cpu_device, cpu)->dev;
1060
	int err = 0;
1061

1062
	switch (action & ~CPU_TASKS_FROZEN) {
1063
	case CPU_ONLINE:
1064
		err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1065 1066
		break;
	case CPU_DEAD:
1067
		sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1068 1069
		break;
	}
1070
	return notifier_from_errno(err);
1071 1072
}

1073
static int smp_add_present_cpu(int cpu)
1074
{
1075 1076
	struct device *s;
	struct cpu *c;
1077 1078
	int rc;

1079 1080 1081
	c = kzalloc(sizeof(*c), GFP_KERNEL);
	if (!c)
		return -ENOMEM;
1082
	per_cpu(cpu_device, cpu) = c;
1083
	s = &c->dev;
1084 1085 1086 1087 1088 1089 1090
	c->hotpluggable = 1;
	rc = register_cpu(c, cpu);
	if (rc)
		goto out;
	rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
	if (rc)
		goto out_cpu;
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	if (cpu_online(cpu)) {
		rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
		if (rc)
			goto out_online;
	}
	rc = topology_cpu_init(c);
	if (rc)
		goto out_topology;
	return 0;

out_topology:
	if (cpu_online(cpu))
		sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
out_online:
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
out_cpu:
#ifdef CONFIG_HOTPLUG_CPU
	unregister_cpu(c);
#endif
out:
	return rc;
}

#ifdef CONFIG_HOTPLUG_CPU
1115

1116
int __ref smp_rescan_cpus(void)
1117
{
1118
	struct sclp_core_info *info;
M
Martin Schwidefsky 已提交
1119
	int nr;
1120

1121
	info = smp_get_core_info();
M
Martin Schwidefsky 已提交
1122 1123
	if (!info)
		return -ENOMEM;
1124
	get_online_cpus();
H
Heiko Carstens 已提交
1125
	mutex_lock(&smp_cpu_state_mutex);
M
Martin Schwidefsky 已提交
1126
	nr = __smp_rescan_cpus(info, 1);
1127
	mutex_unlock(&smp_cpu_state_mutex);
H
Heiko Carstens 已提交
1128
	put_online_cpus();
M
Martin Schwidefsky 已提交
1129 1130
	kfree(info);
	if (nr)
H
Heiko Carstens 已提交
1131
		topology_schedule_update();
M
Martin Schwidefsky 已提交
1132
	return 0;
1133 1134
}

1135 1136
static ssize_t __ref rescan_store(struct device *dev,
				  struct device_attribute *attr,
1137
				  const char *buf,
1138 1139 1140 1141 1142
				  size_t count)
{
	int rc;

	rc = smp_rescan_cpus();
1143 1144
	return rc ? rc : count;
}
1145
static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
1146 1147
#endif /* CONFIG_HOTPLUG_CPU */

1148
static int __init s390_smp_init(void)
L
Linus Torvalds 已提交
1149
{
1150
	int cpu, rc = 0;
1151

1152
#ifdef CONFIG_HOTPLUG_CPU
1153
	rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
1154 1155 1156
	if (rc)
		return rc;
#endif
1157
	cpu_notifier_register_begin();
1158 1159
	for_each_present_cpu(cpu) {
		rc = smp_add_present_cpu(cpu);
1160
		if (rc)
1161
			goto out;
L
Linus Torvalds 已提交
1162
	}
1163 1164 1165 1166 1167 1168

	__hotcpu_notifier(smp_cpu_notify, 0);

out:
	cpu_notifier_register_done();
	return rc;
L
Linus Torvalds 已提交
1169
}
1170
subsys_initcall(s390_smp_init);