xref: /linux/arch/s390/kernel/smp.c (revision 54cbac81881ded7fa487a9304e337d9acdbe4364)
1 /*
2  *  SMP related functions
3  *
4  *    Copyright IBM Corp. 1999, 2012
5  *    Author(s): Denis Joseph Barrow,
6  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>,
7  *		 Heiko Carstens <heiko.carstens@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 KMSG_COMPONENT "cpu"
19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20 
21 #include <linux/workqueue.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/mm.h>
25 #include <linux/err.h>
26 #include <linux/spinlock.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/irqflags.h>
31 #include <linux/cpu.h>
32 #include <linux/slab.h>
33 #include <linux/crash_dump.h>
34 #include <asm/asm-offsets.h>
35 #include <asm/switch_to.h>
36 #include <asm/facility.h>
37 #include <asm/ipl.h>
38 #include <asm/setup.h>
39 #include <asm/irq.h>
40 #include <asm/tlbflush.h>
41 #include <asm/vtimer.h>
42 #include <asm/lowcore.h>
43 #include <asm/sclp.h>
44 #include <asm/vdso.h>
45 #include <asm/debug.h>
46 #include <asm/os_info.h>
47 #include <asm/sigp.h>
48 #include "entry.h"
49 
50 enum {
51 	ec_schedule = 0,
52 	ec_call_function,
53 	ec_call_function_single,
54 	ec_stop_cpu,
55 };
56 
57 enum {
58 	CPU_STATE_STANDBY,
59 	CPU_STATE_CONFIGURED,
60 };
61 
62 struct pcpu {
63 	struct cpu cpu;
64 	struct _lowcore *lowcore;	/* lowcore page(s) for the cpu */
65 	unsigned long async_stack;	/* async stack for the cpu */
66 	unsigned long panic_stack;	/* panic stack for the cpu */
67 	unsigned long ec_mask;		/* bit mask for ec_xxx functions */
68 	int state;			/* physical cpu state */
69 	int polarization;		/* physical polarization */
70 	u16 address;			/* physical cpu address */
71 };
72 
73 static u8 boot_cpu_type;
74 static u16 boot_cpu_address;
75 static struct pcpu pcpu_devices[NR_CPUS];
76 
77 /*
78  * The smp_cpu_state_mutex must be held when changing the state or polarization
79  * member of a pcpu data structure within the pcpu_devices arreay.
80  */
81 DEFINE_MUTEX(smp_cpu_state_mutex);
82 
83 /*
84  * Signal processor helper functions.
85  */
86 static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
87 {
88 	register unsigned int reg1 asm ("1") = parm;
89 	int cc;
90 
91 	asm volatile(
92 		"	sigp	%1,%2,0(%3)\n"
93 		"	ipm	%0\n"
94 		"	srl	%0,28\n"
95 		: "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
96 	if (status && cc == 1)
97 		*status = reg1;
98 	return cc;
99 }
100 
101 static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
102 {
103 	int cc;
104 
105 	while (1) {
106 		cc = __pcpu_sigp(addr, order, parm, NULL);
107 		if (cc != SIGP_CC_BUSY)
108 			return cc;
109 		cpu_relax();
110 	}
111 }
112 
113 static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
114 {
115 	int cc, retry;
116 
117 	for (retry = 0; ; retry++) {
118 		cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
119 		if (cc != SIGP_CC_BUSY)
120 			break;
121 		if (retry >= 3)
122 			udelay(10);
123 	}
124 	return cc;
125 }
126 
127 static inline int pcpu_stopped(struct pcpu *pcpu)
128 {
129 	u32 uninitialized_var(status);
130 
131 	if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
132 			0, &status) != SIGP_CC_STATUS_STORED)
133 		return 0;
134 	return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
135 }
136 
137 static inline int pcpu_running(struct pcpu *pcpu)
138 {
139 	if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
140 			0, NULL) != SIGP_CC_STATUS_STORED)
141 		return 1;
142 	/* Status stored condition code is equivalent to cpu not running. */
143 	return 0;
144 }
145 
146 /*
147  * Find struct pcpu by cpu address.
148  */
149 static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
150 {
151 	int cpu;
152 
153 	for_each_cpu(cpu, mask)
154 		if (pcpu_devices[cpu].address == address)
155 			return pcpu_devices + cpu;
156 	return NULL;
157 }
158 
159 static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
160 {
161 	int order;
162 
163 	set_bit(ec_bit, &pcpu->ec_mask);
164 	order = pcpu_running(pcpu) ?
165 		SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
166 	pcpu_sigp_retry(pcpu, order, 0);
167 }
168 
169 static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
170 {
171 	struct _lowcore *lc;
172 
173 	if (pcpu != &pcpu_devices[0]) {
174 		pcpu->lowcore =	(struct _lowcore *)
175 			__get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
176 		pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
177 		pcpu->panic_stack = __get_free_page(GFP_KERNEL);
178 		if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
179 			goto out;
180 	}
181 	lc = pcpu->lowcore;
182 	memcpy(lc, &S390_lowcore, 512);
183 	memset((char *) lc + 512, 0, sizeof(*lc) - 512);
184 	lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
185 	lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
186 	lc->cpu_nr = cpu;
187 #ifndef CONFIG_64BIT
188 	if (MACHINE_HAS_IEEE) {
189 		lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
190 		if (!lc->extended_save_area_addr)
191 			goto out;
192 	}
193 #else
194 	if (vdso_alloc_per_cpu(lc))
195 		goto out;
196 #endif
197 	lowcore_ptr[cpu] = lc;
198 	pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
199 	return 0;
200 out:
201 	if (pcpu != &pcpu_devices[0]) {
202 		free_page(pcpu->panic_stack);
203 		free_pages(pcpu->async_stack, ASYNC_ORDER);
204 		free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
205 	}
206 	return -ENOMEM;
207 }
208 
209 #ifdef CONFIG_HOTPLUG_CPU
210 
211 static void pcpu_free_lowcore(struct pcpu *pcpu)
212 {
213 	pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
214 	lowcore_ptr[pcpu - pcpu_devices] = NULL;
215 #ifndef CONFIG_64BIT
216 	if (MACHINE_HAS_IEEE) {
217 		struct _lowcore *lc = pcpu->lowcore;
218 
219 		free_page((unsigned long) lc->extended_save_area_addr);
220 		lc->extended_save_area_addr = 0;
221 	}
222 #else
223 	vdso_free_per_cpu(pcpu->lowcore);
224 #endif
225 	if (pcpu != &pcpu_devices[0]) {
226 		free_page(pcpu->panic_stack);
227 		free_pages(pcpu->async_stack, ASYNC_ORDER);
228 		free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
229 	}
230 }
231 
232 #endif /* CONFIG_HOTPLUG_CPU */
233 
234 static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
235 {
236 	struct _lowcore *lc = pcpu->lowcore;
237 
238 	atomic_inc(&init_mm.context.attach_count);
239 	lc->cpu_nr = cpu;
240 	lc->percpu_offset = __per_cpu_offset[cpu];
241 	lc->kernel_asce = S390_lowcore.kernel_asce;
242 	lc->machine_flags = S390_lowcore.machine_flags;
243 	lc->ftrace_func = S390_lowcore.ftrace_func;
244 	lc->user_timer = lc->system_timer = lc->steal_timer = 0;
245 	__ctl_store(lc->cregs_save_area, 0, 15);
246 	save_access_regs((unsigned int *) lc->access_regs_save_area);
247 	memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
248 	       MAX_FACILITY_BIT/8);
249 }
250 
251 static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
252 {
253 	struct _lowcore *lc = pcpu->lowcore;
254 	struct thread_info *ti = task_thread_info(tsk);
255 
256 	lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
257 	lc->thread_info = (unsigned long) task_thread_info(tsk);
258 	lc->current_task = (unsigned long) tsk;
259 	lc->user_timer = ti->user_timer;
260 	lc->system_timer = ti->system_timer;
261 	lc->steal_timer = 0;
262 }
263 
264 static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
265 {
266 	struct _lowcore *lc = pcpu->lowcore;
267 
268 	lc->restart_stack = lc->kernel_stack;
269 	lc->restart_fn = (unsigned long) func;
270 	lc->restart_data = (unsigned long) data;
271 	lc->restart_source = -1UL;
272 	pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
273 }
274 
275 /*
276  * Call function via PSW restart on pcpu and stop the current cpu.
277  */
278 static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
279 			  void *data, unsigned long stack)
280 {
281 	struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
282 	unsigned long source_cpu = stap();
283 
284 	__load_psw_mask(psw_kernel_bits);
285 	if (pcpu->address == source_cpu)
286 		func(data);	/* should not return */
287 	/* Stop target cpu (if func returns this stops the current cpu). */
288 	pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
289 	/* Restart func on the target cpu and stop the current cpu. */
290 	mem_assign_absolute(lc->restart_stack, stack);
291 	mem_assign_absolute(lc->restart_fn, (unsigned long) func);
292 	mem_assign_absolute(lc->restart_data, (unsigned long) data);
293 	mem_assign_absolute(lc->restart_source, source_cpu);
294 	asm volatile(
295 		"0:	sigp	0,%0,%2	# sigp restart to target cpu\n"
296 		"	brc	2,0b	# busy, try again\n"
297 		"1:	sigp	0,%1,%3	# sigp stop to current cpu\n"
298 		"	brc	2,1b	# busy, try again\n"
299 		: : "d" (pcpu->address), "d" (source_cpu),
300 		    "K" (SIGP_RESTART), "K" (SIGP_STOP)
301 		: "0", "1", "cc");
302 	for (;;) ;
303 }
304 
305 /*
306  * Call function on an online CPU.
307  */
308 void smp_call_online_cpu(void (*func)(void *), void *data)
309 {
310 	struct pcpu *pcpu;
311 
312 	/* Use the current cpu if it is online. */
313 	pcpu = pcpu_find_address(cpu_online_mask, stap());
314 	if (!pcpu)
315 		/* Use the first online cpu. */
316 		pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
317 	pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
318 }
319 
320 /*
321  * Call function on the ipl CPU.
322  */
323 void smp_call_ipl_cpu(void (*func)(void *), void *data)
324 {
325 	pcpu_delegate(&pcpu_devices[0], func, data,
326 		      pcpu_devices->panic_stack + PAGE_SIZE);
327 }
328 
329 int smp_find_processor_id(u16 address)
330 {
331 	int cpu;
332 
333 	for_each_present_cpu(cpu)
334 		if (pcpu_devices[cpu].address == address)
335 			return cpu;
336 	return -1;
337 }
338 
339 int smp_vcpu_scheduled(int cpu)
340 {
341 	return pcpu_running(pcpu_devices + cpu);
342 }
343 
344 void smp_yield(void)
345 {
346 	if (MACHINE_HAS_DIAG44)
347 		asm volatile("diag 0,0,0x44");
348 }
349 
350 void smp_yield_cpu(int cpu)
351 {
352 	if (MACHINE_HAS_DIAG9C)
353 		asm volatile("diag %0,0,0x9c"
354 			     : : "d" (pcpu_devices[cpu].address));
355 	else if (MACHINE_HAS_DIAG44)
356 		asm volatile("diag 0,0,0x44");
357 }
358 
359 /*
360  * Send cpus emergency shutdown signal. This gives the cpus the
361  * opportunity to complete outstanding interrupts.
362  */
363 void smp_emergency_stop(cpumask_t *cpumask)
364 {
365 	u64 end;
366 	int cpu;
367 
368 	end = get_clock() + (1000000UL << 12);
369 	for_each_cpu(cpu, cpumask) {
370 		struct pcpu *pcpu = pcpu_devices + cpu;
371 		set_bit(ec_stop_cpu, &pcpu->ec_mask);
372 		while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
373 				   0, NULL) == SIGP_CC_BUSY &&
374 		       get_clock() < end)
375 			cpu_relax();
376 	}
377 	while (get_clock() < end) {
378 		for_each_cpu(cpu, cpumask)
379 			if (pcpu_stopped(pcpu_devices + cpu))
380 				cpumask_clear_cpu(cpu, cpumask);
381 		if (cpumask_empty(cpumask))
382 			break;
383 		cpu_relax();
384 	}
385 }
386 
387 /*
388  * Stop all cpus but the current one.
389  */
390 void smp_send_stop(void)
391 {
392 	cpumask_t cpumask;
393 	int cpu;
394 
395 	/* Disable all interrupts/machine checks */
396 	__load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
397 	trace_hardirqs_off();
398 
399 	debug_set_critical();
400 	cpumask_copy(&cpumask, cpu_online_mask);
401 	cpumask_clear_cpu(smp_processor_id(), &cpumask);
402 
403 	if (oops_in_progress)
404 		smp_emergency_stop(&cpumask);
405 
406 	/* stop all processors */
407 	for_each_cpu(cpu, &cpumask) {
408 		struct pcpu *pcpu = pcpu_devices + cpu;
409 		pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
410 		while (!pcpu_stopped(pcpu))
411 			cpu_relax();
412 	}
413 }
414 
415 /*
416  * Stop the current cpu.
417  */
418 void smp_stop_cpu(void)
419 {
420 	pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
421 	for (;;) ;
422 }
423 
424 /*
425  * This is the main routine where commands issued by other
426  * cpus are handled.
427  */
428 static void do_ext_call_interrupt(struct ext_code ext_code,
429 				  unsigned int param32, unsigned long param64)
430 {
431 	unsigned long bits;
432 	int cpu;
433 
434 	cpu = smp_processor_id();
435 	if (ext_code.code == 0x1202)
436 		kstat_cpu(cpu).irqs[EXTINT_EXC]++;
437 	else
438 		kstat_cpu(cpu).irqs[EXTINT_EMS]++;
439 	/*
440 	 * handle bit signal external calls
441 	 */
442 	bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
443 
444 	if (test_bit(ec_stop_cpu, &bits))
445 		smp_stop_cpu();
446 
447 	if (test_bit(ec_schedule, &bits))
448 		scheduler_ipi();
449 
450 	if (test_bit(ec_call_function, &bits))
451 		generic_smp_call_function_interrupt();
452 
453 	if (test_bit(ec_call_function_single, &bits))
454 		generic_smp_call_function_single_interrupt();
455 
456 }
457 
458 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
459 {
460 	int cpu;
461 
462 	for_each_cpu(cpu, mask)
463 		pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
464 }
465 
466 void arch_send_call_function_single_ipi(int cpu)
467 {
468 	pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
469 }
470 
471 #ifndef CONFIG_64BIT
472 /*
473  * this function sends a 'purge tlb' signal to another CPU.
474  */
475 static void smp_ptlb_callback(void *info)
476 {
477 	__tlb_flush_local();
478 }
479 
480 void smp_ptlb_all(void)
481 {
482 	on_each_cpu(smp_ptlb_callback, NULL, 1);
483 }
484 EXPORT_SYMBOL(smp_ptlb_all);
485 #endif /* ! CONFIG_64BIT */
486 
487 /*
488  * this function sends a 'reschedule' IPI to another CPU.
489  * it goes straight through and wastes no time serializing
490  * anything. Worst case is that we lose a reschedule ...
491  */
492 void smp_send_reschedule(int cpu)
493 {
494 	pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
495 }
496 
497 /*
498  * parameter area for the set/clear control bit callbacks
499  */
500 struct ec_creg_mask_parms {
501 	unsigned long orval;
502 	unsigned long andval;
503 	int cr;
504 };
505 
506 /*
507  * callback for setting/clearing control bits
508  */
509 static void smp_ctl_bit_callback(void *info)
510 {
511 	struct ec_creg_mask_parms *pp = info;
512 	unsigned long cregs[16];
513 
514 	__ctl_store(cregs, 0, 15);
515 	cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
516 	__ctl_load(cregs, 0, 15);
517 }
518 
519 /*
520  * Set a bit in a control register of all cpus
521  */
522 void smp_ctl_set_bit(int cr, int bit)
523 {
524 	struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
525 
526 	on_each_cpu(smp_ctl_bit_callback, &parms, 1);
527 }
528 EXPORT_SYMBOL(smp_ctl_set_bit);
529 
530 /*
531  * Clear a bit in a control register of all cpus
532  */
533 void smp_ctl_clear_bit(int cr, int bit)
534 {
535 	struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
536 
537 	on_each_cpu(smp_ctl_bit_callback, &parms, 1);
538 }
539 EXPORT_SYMBOL(smp_ctl_clear_bit);
540 
541 #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
542 
543 struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
544 EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
545 
546 static void __init smp_get_save_area(int cpu, u16 address)
547 {
548 	void *lc = pcpu_devices[0].lowcore;
549 	struct save_area *save_area;
550 
551 	if (is_kdump_kernel())
552 		return;
553 	if (!OLDMEM_BASE && (address == boot_cpu_address ||
554 			     ipl_info.type != IPL_TYPE_FCP_DUMP))
555 		return;
556 	if (cpu >= NR_CPUS) {
557 		pr_warning("CPU %i exceeds the maximum %i and is excluded "
558 			   "from the dump\n", cpu, NR_CPUS - 1);
559 		return;
560 	}
561 	save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
562 	if (!save_area)
563 		panic("could not allocate memory for save area\n");
564 	zfcpdump_save_areas[cpu] = save_area;
565 #ifdef CONFIG_CRASH_DUMP
566 	if (address == boot_cpu_address) {
567 		/* Copy the registers of the boot cpu. */
568 		copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
569 				 SAVE_AREA_BASE - PAGE_SIZE, 0);
570 		return;
571 	}
572 #endif
573 	/* Get the registers of a non-boot cpu. */
574 	__pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
575 	memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
576 }
577 
578 int smp_store_status(int cpu)
579 {
580 	struct pcpu *pcpu;
581 
582 	pcpu = pcpu_devices + cpu;
583 	if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
584 			      0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
585 		return -EIO;
586 	return 0;
587 }
588 
589 #else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
590 
591 static inline void smp_get_save_area(int cpu, u16 address) { }
592 
593 #endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
594 
595 void smp_cpu_set_polarization(int cpu, int val)
596 {
597 	pcpu_devices[cpu].polarization = val;
598 }
599 
600 int smp_cpu_get_polarization(int cpu)
601 {
602 	return pcpu_devices[cpu].polarization;
603 }
604 
605 static struct sclp_cpu_info *smp_get_cpu_info(void)
606 {
607 	static int use_sigp_detection;
608 	struct sclp_cpu_info *info;
609 	int address;
610 
611 	info = kzalloc(sizeof(*info), GFP_KERNEL);
612 	if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
613 		use_sigp_detection = 1;
614 		for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
615 			if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
616 			    SIGP_CC_NOT_OPERATIONAL)
617 				continue;
618 			info->cpu[info->configured].address = address;
619 			info->configured++;
620 		}
621 		info->combined = info->configured;
622 	}
623 	return info;
624 }
625 
626 static int smp_add_present_cpu(int cpu);
627 
628 static int __smp_rescan_cpus(struct sclp_cpu_info *info, int sysfs_add)
629 {
630 	struct pcpu *pcpu;
631 	cpumask_t avail;
632 	int cpu, nr, i;
633 
634 	nr = 0;
635 	cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
636 	cpu = cpumask_first(&avail);
637 	for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
638 		if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
639 			continue;
640 		if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
641 			continue;
642 		pcpu = pcpu_devices + cpu;
643 		pcpu->address = info->cpu[i].address;
644 		pcpu->state = (cpu >= info->configured) ?
645 			CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
646 		smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
647 		set_cpu_present(cpu, true);
648 		if (sysfs_add && smp_add_present_cpu(cpu) != 0)
649 			set_cpu_present(cpu, false);
650 		else
651 			nr++;
652 		cpu = cpumask_next(cpu, &avail);
653 	}
654 	return nr;
655 }
656 
657 static void __init smp_detect_cpus(void)
658 {
659 	unsigned int cpu, c_cpus, s_cpus;
660 	struct sclp_cpu_info *info;
661 
662 	info = smp_get_cpu_info();
663 	if (!info)
664 		panic("smp_detect_cpus failed to allocate memory\n");
665 	if (info->has_cpu_type) {
666 		for (cpu = 0; cpu < info->combined; cpu++) {
667 			if (info->cpu[cpu].address != boot_cpu_address)
668 				continue;
669 			/* The boot cpu dictates the cpu type. */
670 			boot_cpu_type = info->cpu[cpu].type;
671 			break;
672 		}
673 	}
674 	c_cpus = s_cpus = 0;
675 	for (cpu = 0; cpu < info->combined; cpu++) {
676 		if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
677 			continue;
678 		if (cpu < info->configured) {
679 			smp_get_save_area(c_cpus, info->cpu[cpu].address);
680 			c_cpus++;
681 		} else
682 			s_cpus++;
683 	}
684 	pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
685 	get_online_cpus();
686 	__smp_rescan_cpus(info, 0);
687 	put_online_cpus();
688 	kfree(info);
689 }
690 
691 /*
692  *	Activate a secondary processor.
693  */
694 static void __cpuinit smp_start_secondary(void *cpuvoid)
695 {
696 	S390_lowcore.last_update_clock = get_clock();
697 	S390_lowcore.restart_stack = (unsigned long) restart_stack;
698 	S390_lowcore.restart_fn = (unsigned long) do_restart;
699 	S390_lowcore.restart_data = 0;
700 	S390_lowcore.restart_source = -1UL;
701 	restore_access_regs(S390_lowcore.access_regs_save_area);
702 	__ctl_load(S390_lowcore.cregs_save_area, 0, 15);
703 	__load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
704 	cpu_init();
705 	preempt_disable();
706 	init_cpu_timer();
707 	init_cpu_vtimer();
708 	pfault_init();
709 	notify_cpu_starting(smp_processor_id());
710 	set_cpu_online(smp_processor_id(), true);
711 	local_irq_enable();
712 	/* cpu_idle will call schedule for us */
713 	cpu_idle();
714 }
715 
716 /* Upping and downing of CPUs */
717 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
718 {
719 	struct pcpu *pcpu;
720 	int rc;
721 
722 	pcpu = pcpu_devices + cpu;
723 	if (pcpu->state != CPU_STATE_CONFIGURED)
724 		return -EIO;
725 	if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
726 	    SIGP_CC_ORDER_CODE_ACCEPTED)
727 		return -EIO;
728 
729 	rc = pcpu_alloc_lowcore(pcpu, cpu);
730 	if (rc)
731 		return rc;
732 	pcpu_prepare_secondary(pcpu, cpu);
733 	pcpu_attach_task(pcpu, tidle);
734 	pcpu_start_fn(pcpu, smp_start_secondary, NULL);
735 	while (!cpu_online(cpu))
736 		cpu_relax();
737 	return 0;
738 }
739 
740 static int __init setup_possible_cpus(char *s)
741 {
742 	int max, cpu;
743 
744 	if (kstrtoint(s, 0, &max) < 0)
745 		return 0;
746 	init_cpu_possible(cpumask_of(0));
747 	for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
748 		set_cpu_possible(cpu, true);
749 	return 0;
750 }
751 early_param("possible_cpus", setup_possible_cpus);
752 
753 #ifdef CONFIG_HOTPLUG_CPU
754 
755 int __cpu_disable(void)
756 {
757 	unsigned long cregs[16];
758 
759 	set_cpu_online(smp_processor_id(), false);
760 	/* Disable pseudo page faults on this cpu. */
761 	pfault_fini();
762 	/* Disable interrupt sources via control register. */
763 	__ctl_store(cregs, 0, 15);
764 	cregs[0]  &= ~0x0000ee70UL;	/* disable all external interrupts */
765 	cregs[6]  &= ~0xff000000UL;	/* disable all I/O interrupts */
766 	cregs[14] &= ~0x1f000000UL;	/* disable most machine checks */
767 	__ctl_load(cregs, 0, 15);
768 	return 0;
769 }
770 
771 void __cpu_die(unsigned int cpu)
772 {
773 	struct pcpu *pcpu;
774 
775 	/* Wait until target cpu is down */
776 	pcpu = pcpu_devices + cpu;
777 	while (!pcpu_stopped(pcpu))
778 		cpu_relax();
779 	pcpu_free_lowcore(pcpu);
780 	atomic_dec(&init_mm.context.attach_count);
781 }
782 
783 void __noreturn cpu_die(void)
784 {
785 	idle_task_exit();
786 	pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
787 	for (;;) ;
788 }
789 
790 #endif /* CONFIG_HOTPLUG_CPU */
791 
792 void __init smp_prepare_cpus(unsigned int max_cpus)
793 {
794 	/* request the 0x1201 emergency signal external interrupt */
795 	if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
796 		panic("Couldn't request external interrupt 0x1201");
797 	/* request the 0x1202 external call external interrupt */
798 	if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
799 		panic("Couldn't request external interrupt 0x1202");
800 	smp_detect_cpus();
801 }
802 
803 void __init smp_prepare_boot_cpu(void)
804 {
805 	struct pcpu *pcpu = pcpu_devices;
806 
807 	boot_cpu_address = stap();
808 	pcpu->state = CPU_STATE_CONFIGURED;
809 	pcpu->address = boot_cpu_address;
810 	pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
811 	pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
812 	pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
813 	S390_lowcore.percpu_offset = __per_cpu_offset[0];
814 	smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
815 	set_cpu_present(0, true);
816 	set_cpu_online(0, true);
817 }
818 
819 void __init smp_cpus_done(unsigned int max_cpus)
820 {
821 }
822 
823 void __init smp_setup_processor_id(void)
824 {
825 	S390_lowcore.cpu_nr = 0;
826 }
827 
828 /*
829  * the frequency of the profiling timer can be changed
830  * by writing a multiplier value into /proc/profile.
831  *
832  * usually you want to run this on all CPUs ;)
833  */
834 int setup_profiling_timer(unsigned int multiplier)
835 {
836 	return 0;
837 }
838 
839 #ifdef CONFIG_HOTPLUG_CPU
840 static ssize_t cpu_configure_show(struct device *dev,
841 				  struct device_attribute *attr, char *buf)
842 {
843 	ssize_t count;
844 
845 	mutex_lock(&smp_cpu_state_mutex);
846 	count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
847 	mutex_unlock(&smp_cpu_state_mutex);
848 	return count;
849 }
850 
851 static ssize_t cpu_configure_store(struct device *dev,
852 				   struct device_attribute *attr,
853 				   const char *buf, size_t count)
854 {
855 	struct pcpu *pcpu;
856 	int cpu, val, rc;
857 	char delim;
858 
859 	if (sscanf(buf, "%d %c", &val, &delim) != 1)
860 		return -EINVAL;
861 	if (val != 0 && val != 1)
862 		return -EINVAL;
863 	get_online_cpus();
864 	mutex_lock(&smp_cpu_state_mutex);
865 	rc = -EBUSY;
866 	/* disallow configuration changes of online cpus and cpu 0 */
867 	cpu = dev->id;
868 	if (cpu_online(cpu) || cpu == 0)
869 		goto out;
870 	pcpu = pcpu_devices + cpu;
871 	rc = 0;
872 	switch (val) {
873 	case 0:
874 		if (pcpu->state != CPU_STATE_CONFIGURED)
875 			break;
876 		rc = sclp_cpu_deconfigure(pcpu->address);
877 		if (rc)
878 			break;
879 		pcpu->state = CPU_STATE_STANDBY;
880 		smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
881 		topology_expect_change();
882 		break;
883 	case 1:
884 		if (pcpu->state != CPU_STATE_STANDBY)
885 			break;
886 		rc = sclp_cpu_configure(pcpu->address);
887 		if (rc)
888 			break;
889 		pcpu->state = CPU_STATE_CONFIGURED;
890 		smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
891 		topology_expect_change();
892 		break;
893 	default:
894 		break;
895 	}
896 out:
897 	mutex_unlock(&smp_cpu_state_mutex);
898 	put_online_cpus();
899 	return rc ? rc : count;
900 }
901 static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
902 #endif /* CONFIG_HOTPLUG_CPU */
903 
904 static ssize_t show_cpu_address(struct device *dev,
905 				struct device_attribute *attr, char *buf)
906 {
907 	return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
908 }
909 static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
910 
911 static struct attribute *cpu_common_attrs[] = {
912 #ifdef CONFIG_HOTPLUG_CPU
913 	&dev_attr_configure.attr,
914 #endif
915 	&dev_attr_address.attr,
916 	NULL,
917 };
918 
919 static struct attribute_group cpu_common_attr_group = {
920 	.attrs = cpu_common_attrs,
921 };
922 
923 static ssize_t show_idle_count(struct device *dev,
924 				struct device_attribute *attr, char *buf)
925 {
926 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
927 	unsigned long long idle_count;
928 	unsigned int sequence;
929 
930 	do {
931 		sequence = ACCESS_ONCE(idle->sequence);
932 		idle_count = ACCESS_ONCE(idle->idle_count);
933 		if (ACCESS_ONCE(idle->clock_idle_enter))
934 			idle_count++;
935 	} while ((sequence & 1) || (idle->sequence != sequence));
936 	return sprintf(buf, "%llu\n", idle_count);
937 }
938 static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
939 
940 static ssize_t show_idle_time(struct device *dev,
941 				struct device_attribute *attr, char *buf)
942 {
943 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
944 	unsigned long long now, idle_time, idle_enter, idle_exit;
945 	unsigned int sequence;
946 
947 	do {
948 		now = get_clock();
949 		sequence = ACCESS_ONCE(idle->sequence);
950 		idle_time = ACCESS_ONCE(idle->idle_time);
951 		idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
952 		idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
953 	} while ((sequence & 1) || (idle->sequence != sequence));
954 	idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
955 	return sprintf(buf, "%llu\n", idle_time >> 12);
956 }
957 static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
958 
959 static struct attribute *cpu_online_attrs[] = {
960 	&dev_attr_idle_count.attr,
961 	&dev_attr_idle_time_us.attr,
962 	NULL,
963 };
964 
965 static struct attribute_group cpu_online_attr_group = {
966 	.attrs = cpu_online_attrs,
967 };
968 
969 static int __cpuinit smp_cpu_notify(struct notifier_block *self,
970 				    unsigned long action, void *hcpu)
971 {
972 	unsigned int cpu = (unsigned int)(long)hcpu;
973 	struct cpu *c = &pcpu_devices[cpu].cpu;
974 	struct device *s = &c->dev;
975 	int err = 0;
976 
977 	switch (action & ~CPU_TASKS_FROZEN) {
978 	case CPU_ONLINE:
979 		err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
980 		break;
981 	case CPU_DEAD:
982 		sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
983 		break;
984 	}
985 	return notifier_from_errno(err);
986 }
987 
988 static int smp_add_present_cpu(int cpu)
989 {
990 	struct cpu *c = &pcpu_devices[cpu].cpu;
991 	struct device *s = &c->dev;
992 	int rc;
993 
994 	c->hotpluggable = 1;
995 	rc = register_cpu(c, cpu);
996 	if (rc)
997 		goto out;
998 	rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
999 	if (rc)
1000 		goto out_cpu;
1001 	if (cpu_online(cpu)) {
1002 		rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1003 		if (rc)
1004 			goto out_online;
1005 	}
1006 	rc = topology_cpu_init(c);
1007 	if (rc)
1008 		goto out_topology;
1009 	return 0;
1010 
1011 out_topology:
1012 	if (cpu_online(cpu))
1013 		sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1014 out_online:
1015 	sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1016 out_cpu:
1017 #ifdef CONFIG_HOTPLUG_CPU
1018 	unregister_cpu(c);
1019 #endif
1020 out:
1021 	return rc;
1022 }
1023 
1024 #ifdef CONFIG_HOTPLUG_CPU
1025 
1026 int __ref smp_rescan_cpus(void)
1027 {
1028 	struct sclp_cpu_info *info;
1029 	int nr;
1030 
1031 	info = smp_get_cpu_info();
1032 	if (!info)
1033 		return -ENOMEM;
1034 	get_online_cpus();
1035 	mutex_lock(&smp_cpu_state_mutex);
1036 	nr = __smp_rescan_cpus(info, 1);
1037 	mutex_unlock(&smp_cpu_state_mutex);
1038 	put_online_cpus();
1039 	kfree(info);
1040 	if (nr)
1041 		topology_schedule_update();
1042 	return 0;
1043 }
1044 
1045 static ssize_t __ref rescan_store(struct device *dev,
1046 				  struct device_attribute *attr,
1047 				  const char *buf,
1048 				  size_t count)
1049 {
1050 	int rc;
1051 
1052 	rc = smp_rescan_cpus();
1053 	return rc ? rc : count;
1054 }
1055 static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
1056 #endif /* CONFIG_HOTPLUG_CPU */
1057 
1058 static int __init s390_smp_init(void)
1059 {
1060 	int cpu, rc;
1061 
1062 	hotcpu_notifier(smp_cpu_notify, 0);
1063 #ifdef CONFIG_HOTPLUG_CPU
1064 	rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
1065 	if (rc)
1066 		return rc;
1067 #endif
1068 	for_each_present_cpu(cpu) {
1069 		rc = smp_add_present_cpu(cpu);
1070 		if (rc)
1071 			return rc;
1072 	}
1073 	return 0;
1074 }
1075 subsys_initcall(s390_smp_init);
1076