2 * SMP related functions
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>,
9 * based on other smp stuff by
10 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
11 * (c) 1998 Ingo Molnar
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.
18 #define KMSG_COMPONENT "cpu"
19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21 #include <linux/workqueue.h>
22 #include <linux/module.h>
23 #include <linux/init.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 <linux/memblock.h>
35 #include <asm/asm-offsets.h>
37 #include <asm/switch_to.h>
38 #include <asm/facility.h>
40 #include <asm/setup.h>
42 #include <asm/tlbflush.h>
43 #include <asm/vtimer.h>
44 #include <asm/lowcore.h>
47 #include <asm/debug.h>
48 #include <asm/os_info.h>
55 ec_call_function_single,
64 static DEFINE_PER_CPU(struct cpu *, cpu_device);
67 struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
68 unsigned long ec_mask; /* bit mask for ec_xxx functions */
69 signed char state; /* physical cpu state */
70 signed char polarization; /* physical polarization */
71 u16 address; /* physical cpu address */
74 static u8 boot_core_type;
75 static struct pcpu pcpu_devices[NR_CPUS];
77 unsigned int smp_cpu_mt_shift;
78 EXPORT_SYMBOL(smp_cpu_mt_shift);
80 unsigned int smp_cpu_mtid;
81 EXPORT_SYMBOL(smp_cpu_mtid);
83 static unsigned int smp_max_threads __initdata = -1U;
85 static int __init early_nosmt(char *s)
90 early_param("nosmt", early_nosmt);
92 static int __init early_smt(char *s)
94 get_option(&s, &smp_max_threads);
97 early_param("smt", early_smt);
100 * The smp_cpu_state_mutex must be held when changing the state or polarization
101 * member of a pcpu data structure within the pcpu_devices arreay.
103 DEFINE_MUTEX(smp_cpu_state_mutex);
106 * Signal processor helper functions.
108 static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm,
114 cc = __pcpu_sigp(addr, order, parm, NULL);
115 if (cc != SIGP_CC_BUSY)
121 static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
125 for (retry = 0; ; retry++) {
126 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
127 if (cc != SIGP_CC_BUSY)
135 static inline int pcpu_stopped(struct pcpu *pcpu)
137 u32 uninitialized_var(status);
139 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
140 0, &status) != SIGP_CC_STATUS_STORED)
142 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
145 static inline int pcpu_running(struct pcpu *pcpu)
147 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
148 0, NULL) != SIGP_CC_STATUS_STORED)
150 /* Status stored condition code is equivalent to cpu not running. */
155 * Find struct pcpu by cpu address.
157 static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
161 for_each_cpu(cpu, mask)
162 if (pcpu_devices[cpu].address == address)
163 return pcpu_devices + cpu;
167 static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
171 if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
173 order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
174 pcpu_sigp_retry(pcpu, order, 0);
177 #define ASYNC_FRAME_OFFSET (ASYNC_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
178 #define PANIC_FRAME_OFFSET (PAGE_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
180 static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
182 unsigned long async_stack, panic_stack;
185 if (pcpu != &pcpu_devices[0]) {
186 pcpu->lowcore = (struct _lowcore *)
187 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
188 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
189 panic_stack = __get_free_page(GFP_KERNEL);
190 if (!pcpu->lowcore || !panic_stack || !async_stack)
193 async_stack = pcpu->lowcore->async_stack - ASYNC_FRAME_OFFSET;
194 panic_stack = pcpu->lowcore->panic_stack - PANIC_FRAME_OFFSET;
197 memcpy(lc, &S390_lowcore, 512);
198 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
199 lc->async_stack = async_stack + ASYNC_FRAME_OFFSET;
200 lc->panic_stack = panic_stack + PANIC_FRAME_OFFSET;
202 lc->spinlock_lockval = arch_spin_lockval(cpu);
203 lc->br_r1_trampoline = 0x07f1; /* br %r1 */
205 lc->vector_save_area_addr =
206 (unsigned long) &lc->vector_save_area;
207 if (vdso_alloc_per_cpu(lc))
209 lowcore_ptr[cpu] = lc;
210 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
213 if (pcpu != &pcpu_devices[0]) {
214 free_page(panic_stack);
215 free_pages(async_stack, ASYNC_ORDER);
216 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
221 #ifdef CONFIG_HOTPLUG_CPU
223 static void pcpu_free_lowcore(struct pcpu *pcpu)
225 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
226 lowcore_ptr[pcpu - pcpu_devices] = NULL;
227 vdso_free_per_cpu(pcpu->lowcore);
228 if (pcpu == &pcpu_devices[0])
230 free_page(pcpu->lowcore->panic_stack-PANIC_FRAME_OFFSET);
231 free_pages(pcpu->lowcore->async_stack-ASYNC_FRAME_OFFSET, ASYNC_ORDER);
232 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
235 #endif /* CONFIG_HOTPLUG_CPU */
237 static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
239 struct _lowcore *lc = pcpu->lowcore;
241 if (MACHINE_HAS_TLB_LC)
242 cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
243 cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
244 atomic_inc(&init_mm.context.attach_count);
246 lc->spinlock_lockval = arch_spin_lockval(cpu);
247 lc->percpu_offset = __per_cpu_offset[cpu];
248 lc->kernel_asce = S390_lowcore.kernel_asce;
249 lc->machine_flags = S390_lowcore.machine_flags;
250 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
251 __ctl_store(lc->cregs_save_area, 0, 15);
252 save_access_regs((unsigned int *) lc->access_regs_save_area);
253 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
254 sizeof(lc->stfle_fac_list));
255 memcpy(lc->alt_stfle_fac_list, S390_lowcore.alt_stfle_fac_list,
256 sizeof(lc->alt_stfle_fac_list));
259 static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
261 struct _lowcore *lc = pcpu->lowcore;
262 struct thread_info *ti = task_thread_info(tsk);
264 lc->kernel_stack = (unsigned long) task_stack_page(tsk)
265 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
266 lc->thread_info = (unsigned long) task_thread_info(tsk);
267 lc->current_task = (unsigned long) tsk;
269 lc->current_pid = tsk->pid;
270 lc->user_timer = ti->user_timer;
271 lc->system_timer = ti->system_timer;
275 static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
277 struct _lowcore *lc = pcpu->lowcore;
279 lc->restart_stack = lc->kernel_stack;
280 lc->restart_fn = (unsigned long) func;
281 lc->restart_data = (unsigned long) data;
282 lc->restart_source = -1UL;
283 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
287 * Call function via PSW restart on pcpu and stop the current cpu.
289 static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
290 void *data, unsigned long stack)
292 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
293 unsigned long source_cpu = stap();
295 __load_psw_mask(PSW_KERNEL_BITS);
296 if (pcpu->address == source_cpu)
297 func(data); /* should not return */
298 /* Stop target cpu (if func returns this stops the current cpu). */
299 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
300 /* Restart func on the target cpu and stop the current cpu. */
301 mem_assign_absolute(lc->restart_stack, stack);
302 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
303 mem_assign_absolute(lc->restart_data, (unsigned long) data);
304 mem_assign_absolute(lc->restart_source, source_cpu);
307 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
308 " brc 2,0b # busy, try again\n"
309 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
310 " brc 2,1b # busy, try again\n"
311 : : "d" (pcpu->address), "d" (source_cpu),
312 "K" (SIGP_RESTART), "K" (SIGP_STOP)
318 * Enable additional logical cpus for multi-threading.
320 static int pcpu_set_smt(unsigned int mtid)
322 register unsigned long reg1 asm ("1") = (unsigned long) mtid;
325 if (smp_cpu_mtid == mtid)
328 " sigp %1,0,%2 # sigp set multi-threading\n"
331 : "=d" (cc) : "d" (reg1), "K" (SIGP_SET_MULTI_THREADING)
335 smp_cpu_mt_shift = 0;
336 while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
338 pcpu_devices[0].address = stap();
344 * Call function on an online CPU.
346 void smp_call_online_cpu(void (*func)(void *), void *data)
350 /* Use the current cpu if it is online. */
351 pcpu = pcpu_find_address(cpu_online_mask, stap());
353 /* Use the first online cpu. */
354 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
355 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
359 * Call function on the ipl CPU.
361 void smp_call_ipl_cpu(void (*func)(void *), void *data)
363 struct _lowcore *lc = pcpu_devices->lowcore;
365 if (pcpu_devices[0].address == stap())
368 pcpu_delegate(&pcpu_devices[0], func, data,
369 lc->panic_stack - PANIC_FRAME_OFFSET + PAGE_SIZE);
372 int smp_find_processor_id(u16 address)
376 for_each_present_cpu(cpu)
377 if (pcpu_devices[cpu].address == address)
382 int smp_vcpu_scheduled(int cpu)
384 return pcpu_running(pcpu_devices + cpu);
387 void smp_yield_cpu(int cpu)
389 if (MACHINE_HAS_DIAG9C) {
390 diag_stat_inc_norecursion(DIAG_STAT_X09C);
391 asm volatile("diag %0,0,0x9c"
392 : : "d" (pcpu_devices[cpu].address));
393 } else if (MACHINE_HAS_DIAG44) {
394 diag_stat_inc_norecursion(DIAG_STAT_X044);
395 asm volatile("diag 0,0,0x44");
400 * Send cpus emergency shutdown signal. This gives the cpus the
401 * opportunity to complete outstanding interrupts.
403 static void smp_emergency_stop(cpumask_t *cpumask)
408 end = get_tod_clock() + (1000000UL << 12);
409 for_each_cpu(cpu, cpumask) {
410 struct pcpu *pcpu = pcpu_devices + cpu;
411 set_bit(ec_stop_cpu, &pcpu->ec_mask);
412 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
413 0, NULL) == SIGP_CC_BUSY &&
414 get_tod_clock() < end)
417 while (get_tod_clock() < end) {
418 for_each_cpu(cpu, cpumask)
419 if (pcpu_stopped(pcpu_devices + cpu))
420 cpumask_clear_cpu(cpu, cpumask);
421 if (cpumask_empty(cpumask))
428 * Stop all cpus but the current one.
430 void smp_send_stop(void)
435 /* Disable all interrupts/machine checks */
436 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
437 trace_hardirqs_off();
439 debug_set_critical();
440 cpumask_copy(&cpumask, cpu_online_mask);
441 cpumask_clear_cpu(smp_processor_id(), &cpumask);
443 if (oops_in_progress)
444 smp_emergency_stop(&cpumask);
446 /* stop all processors */
447 for_each_cpu(cpu, &cpumask) {
448 struct pcpu *pcpu = pcpu_devices + cpu;
449 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
450 while (!pcpu_stopped(pcpu))
456 * This is the main routine where commands issued by other
459 static void smp_handle_ext_call(void)
463 /* handle bit signal external calls */
464 bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
465 if (test_bit(ec_stop_cpu, &bits))
467 if (test_bit(ec_schedule, &bits))
469 if (test_bit(ec_call_function_single, &bits))
470 generic_smp_call_function_single_interrupt();
473 static void do_ext_call_interrupt(struct ext_code ext_code,
474 unsigned int param32, unsigned long param64)
476 inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
477 smp_handle_ext_call();
480 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
484 for_each_cpu(cpu, mask)
485 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
488 void arch_send_call_function_single_ipi(int cpu)
490 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
494 * this function sends a 'reschedule' IPI to another CPU.
495 * it goes straight through and wastes no time serializing
496 * anything. Worst case is that we lose a reschedule ...
498 void smp_send_reschedule(int cpu)
500 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
504 * parameter area for the set/clear control bit callbacks
506 struct ec_creg_mask_parms {
508 unsigned long andval;
513 * callback for setting/clearing control bits
515 static void smp_ctl_bit_callback(void *info)
517 struct ec_creg_mask_parms *pp = info;
518 unsigned long cregs[16];
520 __ctl_store(cregs, 0, 15);
521 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
522 __ctl_load(cregs, 0, 15);
526 * Set a bit in a control register of all cpus
528 void smp_ctl_set_bit(int cr, int bit)
530 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
532 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
534 EXPORT_SYMBOL(smp_ctl_set_bit);
537 * Clear a bit in a control register of all cpus
539 void smp_ctl_clear_bit(int cr, int bit)
541 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
543 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
545 EXPORT_SYMBOL(smp_ctl_clear_bit);
547 #ifdef CONFIG_CRASH_DUMP
549 static void __init __smp_store_cpu_state(struct save_area_ext *sa_ext,
550 u16 address, int is_boot_cpu)
552 void *lc = (void *)(unsigned long) store_prefix();
556 /* Copy the registers of the boot CPU. */
557 copy_oldmem_page(1, (void *) &sa_ext->sa, sizeof(sa_ext->sa),
558 SAVE_AREA_BASE - PAGE_SIZE, 0);
560 save_vx_regs_safe(sa_ext->vx_regs);
563 /* Get the registers of a non-boot cpu. */
564 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
565 memcpy_real(&sa_ext->sa, lc + SAVE_AREA_BASE, sizeof(sa_ext->sa));
568 /* Get the VX registers */
569 vx_sa = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
571 panic("could not allocate memory for VX save area\n");
572 __pcpu_sigp_relax(address, SIGP_STORE_ADDITIONAL_STATUS, vx_sa, NULL);
573 memcpy(sa_ext->vx_regs, (void *) vx_sa, sizeof(sa_ext->vx_regs));
574 memblock_free(vx_sa, PAGE_SIZE);
577 int smp_store_status(int cpu)
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)
588 vx_sa = __pa(pcpu->lowcore->vector_save_area_addr);
589 __pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
594 #endif /* CONFIG_CRASH_DUMP */
597 * Collect CPU state of the previous, crashed system.
598 * There are four cases:
599 * 1) standard zfcp dump
600 * condition: OLDMEM_BASE == NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
601 * The state for all CPUs except the boot CPU needs to be collected
602 * with sigp stop-and-store-status. The boot CPU state is located in
603 * the absolute lowcore of the memory stored in the HSA. The zcore code
604 * will allocate the save area and copy the boot CPU state from the HSA.
605 * 2) stand-alone kdump for SCSI (zfcp dump with swapped memory)
606 * condition: OLDMEM_BASE != NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
607 * The state for all CPUs except the boot CPU needs to be collected
608 * with sigp stop-and-store-status. The firmware or the boot-loader
609 * stored the registers of the boot CPU in the absolute lowcore in the
610 * memory of the old system.
611 * 3) kdump and the old kernel did not store the CPU state,
612 * or stand-alone kdump for DASD
613 * condition: OLDMEM_BASE != NULL && !is_kdump_kernel()
614 * The state for all CPUs except the boot CPU needs to be collected
615 * with sigp stop-and-store-status. The kexec code or the boot-loader
616 * stored the registers of the boot CPU in the memory of the old system.
617 * 4) kdump and the old kernel stored the CPU state
618 * condition: OLDMEM_BASE != NULL && is_kdump_kernel()
619 * The state of all CPUs is stored in ELF sections in the memory of the
620 * old system. The ELF sections are picked up by the crash_dump code
621 * via elfcorehdr_addr.
623 void __init smp_save_dump_cpus(void)
625 #ifdef CONFIG_CRASH_DUMP
626 int addr, cpu, boot_cpu_addr, max_cpu_addr;
627 struct save_area_ext *sa_ext;
630 if (is_kdump_kernel())
631 /* Previous system stored the CPU states. Nothing to do. */
633 if (!(OLDMEM_BASE || ipl_info.type == IPL_TYPE_FCP_DUMP))
634 /* No previous system present, normal boot. */
636 /* Set multi-threading state to the previous system. */
637 pcpu_set_smt(sclp.mtid_prev);
638 max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
639 for (cpu = 0, addr = 0; addr <= max_cpu_addr; addr++) {
640 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0, NULL) ==
641 SIGP_CC_NOT_OPERATIONAL)
645 dump_save_areas.areas = (void *)memblock_alloc(sizeof(void *) * cpu, 8);
646 dump_save_areas.count = cpu;
647 boot_cpu_addr = stap();
648 for (cpu = 0, addr = 0; addr <= max_cpu_addr; addr++) {
649 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0, NULL) ==
650 SIGP_CC_NOT_OPERATIONAL)
652 sa_ext = (void *) memblock_alloc(sizeof(*sa_ext), 8);
653 dump_save_areas.areas[cpu] = sa_ext;
655 panic("could not allocate memory for save area\n");
656 is_boot_cpu = (addr == boot_cpu_addr);
658 if (is_boot_cpu && !OLDMEM_BASE)
659 /* Skip boot CPU for standard zfcp dump. */
661 /* Get state for this CPU. */
662 __smp_store_cpu_state(sa_ext, addr, is_boot_cpu);
666 #endif /* CONFIG_CRASH_DUMP */
669 void smp_cpu_set_polarization(int cpu, int val)
671 pcpu_devices[cpu].polarization = val;
674 int smp_cpu_get_polarization(int cpu)
676 return pcpu_devices[cpu].polarization;
679 static struct sclp_core_info *smp_get_core_info(void)
681 static int use_sigp_detection;
682 struct sclp_core_info *info;
685 info = kzalloc(sizeof(*info), GFP_KERNEL);
686 if (info && (use_sigp_detection || sclp_get_core_info(info))) {
687 use_sigp_detection = 1;
689 address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
690 address += (1U << smp_cpu_mt_shift)) {
691 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
692 SIGP_CC_NOT_OPERATIONAL)
694 info->core[info->configured].core_id =
695 address >> smp_cpu_mt_shift;
698 info->combined = info->configured;
703 static int smp_add_present_cpu(int cpu);
705 static int smp_add_core(struct sclp_core_entry *core, cpumask_t *avail,
706 bool configured, bool early)
713 if (sclp.has_core_type && core->type != boot_core_type)
715 cpu = cpumask_first(avail);
716 address = core->core_id << smp_cpu_mt_shift;
717 for (i = 0; (i <= smp_cpu_mtid) && (cpu < nr_cpu_ids); i++) {
718 if (pcpu_find_address(cpu_present_mask, address + i))
720 pcpu = pcpu_devices + cpu;
721 pcpu->address = address + i;
723 pcpu->state = CPU_STATE_CONFIGURED;
725 pcpu->state = CPU_STATE_STANDBY;
726 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
727 set_cpu_present(cpu, true);
728 if (!early && smp_add_present_cpu(cpu) != 0)
729 set_cpu_present(cpu, false);
732 cpumask_clear_cpu(cpu, avail);
733 cpu = cpumask_next(cpu, avail);
738 static int __smp_rescan_cpus(struct sclp_core_info *info, bool early)
740 struct sclp_core_entry *core;
741 static cpumask_t avail;
747 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
749 * Add IPL core first (which got logical CPU number 0) to make sure
750 * that all SMT threads get subsequent logical CPU numbers.
753 core_id = pcpu_devices[0].address >> smp_cpu_mt_shift;
754 for (i = 0; i < info->configured; i++) {
755 core = &info->core[i];
756 if (core->core_id == core_id) {
757 nr += smp_add_core(core, &avail, true, early);
762 for (i = 0; i < info->combined; i++) {
763 configured = i < info->configured;
764 nr += smp_add_core(&info->core[i], &avail, configured, early);
769 static void __init smp_detect_cpus(void)
771 unsigned int cpu, mtid, c_cpus, s_cpus;
772 struct sclp_core_info *info;
775 /* Get CPU information */
776 info = smp_get_core_info();
778 panic("smp_detect_cpus failed to allocate memory\n");
780 /* Find boot CPU type */
781 if (sclp.has_core_type) {
783 for (cpu = 0; cpu < info->combined; cpu++)
784 if (info->core[cpu].core_id == address) {
785 /* The boot cpu dictates the cpu type. */
786 boot_core_type = info->core[cpu].type;
789 if (cpu >= info->combined)
790 panic("Could not find boot CPU type");
793 /* Set multi-threading state for the current system */
794 mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
795 mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
798 /* Print number of CPUs */
800 for (cpu = 0; cpu < info->combined; cpu++) {
801 if (sclp.has_core_type &&
802 info->core[cpu].type != boot_core_type)
804 if (cpu < info->configured)
805 c_cpus += smp_cpu_mtid + 1;
807 s_cpus += smp_cpu_mtid + 1;
809 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
811 /* Add CPUs present at boot */
813 __smp_rescan_cpus(info, true);
819 * Activate a secondary processor.
821 static void smp_start_secondary(void *cpuvoid)
823 S390_lowcore.last_update_clock = get_tod_clock();
824 S390_lowcore.restart_stack = (unsigned long) restart_stack;
825 S390_lowcore.restart_fn = (unsigned long) do_restart;
826 S390_lowcore.restart_data = 0;
827 S390_lowcore.restart_source = -1UL;
828 restore_access_regs(S390_lowcore.access_regs_save_area);
829 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
830 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
836 notify_cpu_starting(smp_processor_id());
837 set_cpu_online(smp_processor_id(), true);
838 inc_irq_stat(CPU_RST);
840 cpu_startup_entry(CPUHP_ONLINE);
843 /* Upping and downing of CPUs */
844 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
849 pcpu = pcpu_devices + cpu;
850 if (pcpu->state != CPU_STATE_CONFIGURED)
852 base = cpu - (cpu % (smp_cpu_mtid + 1));
853 for (i = 0; i <= smp_cpu_mtid; i++) {
854 if (base + i < nr_cpu_ids)
855 if (cpu_online(base + i))
859 * If this is the first CPU of the core to get online
860 * do an initial CPU reset.
862 if (i > smp_cpu_mtid &&
863 pcpu_sigp_retry(pcpu_devices + base, SIGP_INITIAL_CPU_RESET, 0) !=
864 SIGP_CC_ORDER_CODE_ACCEPTED)
867 rc = pcpu_alloc_lowcore(pcpu, cpu);
870 pcpu_prepare_secondary(pcpu, cpu);
871 pcpu_attach_task(pcpu, tidle);
872 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
873 /* Wait until cpu puts itself in the online & active maps */
874 while (!cpu_online(cpu) || !cpu_active(cpu))
879 static unsigned int setup_possible_cpus __initdata;
881 static int __init _setup_possible_cpus(char *s)
883 get_option(&s, &setup_possible_cpus);
886 early_param("possible_cpus", _setup_possible_cpus);
888 #ifdef CONFIG_HOTPLUG_CPU
890 int __cpu_disable(void)
892 unsigned long cregs[16];
894 /* Handle possible pending IPIs */
895 smp_handle_ext_call();
896 set_cpu_online(smp_processor_id(), false);
897 /* Disable pseudo page faults on this cpu. */
899 /* Disable interrupt sources via control register. */
900 __ctl_store(cregs, 0, 15);
901 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
902 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
903 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
904 __ctl_load(cregs, 0, 15);
905 clear_cpu_flag(CIF_NOHZ_DELAY);
909 void __cpu_die(unsigned int cpu)
913 /* Wait until target cpu is down */
914 pcpu = pcpu_devices + cpu;
915 while (!pcpu_stopped(pcpu))
917 pcpu_free_lowcore(pcpu);
918 atomic_dec(&init_mm.context.attach_count);
919 cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
920 if (MACHINE_HAS_TLB_LC)
921 cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
924 void __noreturn cpu_die(void)
928 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
932 #endif /* CONFIG_HOTPLUG_CPU */
934 void __init smp_fill_possible_mask(void)
936 unsigned int possible, sclp_max, cpu;
938 sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
939 sclp_max = min(smp_max_threads, sclp_max);
940 sclp_max = sclp.max_cores * sclp_max ?: nr_cpu_ids;
941 possible = setup_possible_cpus ?: nr_cpu_ids;
942 possible = min(possible, sclp_max);
943 for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
944 set_cpu_possible(cpu, true);
947 void __init smp_prepare_cpus(unsigned int max_cpus)
949 /* request the 0x1201 emergency signal external interrupt */
950 if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
951 panic("Couldn't request external interrupt 0x1201");
952 /* request the 0x1202 external call external interrupt */
953 if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
954 panic("Couldn't request external interrupt 0x1202");
958 void __init smp_prepare_boot_cpu(void)
960 struct pcpu *pcpu = pcpu_devices;
962 pcpu->state = CPU_STATE_CONFIGURED;
963 pcpu->address = stap();
964 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
965 S390_lowcore.percpu_offset = __per_cpu_offset[0];
966 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
967 set_cpu_present(0, true);
968 set_cpu_online(0, true);
971 void __init smp_cpus_done(unsigned int max_cpus)
975 void __init smp_setup_processor_id(void)
977 S390_lowcore.cpu_nr = 0;
978 S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
982 * the frequency of the profiling timer can be changed
983 * by writing a multiplier value into /proc/profile.
985 * usually you want to run this on all CPUs ;)
987 int setup_profiling_timer(unsigned int multiplier)
992 #ifdef CONFIG_HOTPLUG_CPU
993 static ssize_t cpu_configure_show(struct device *dev,
994 struct device_attribute *attr, char *buf)
998 mutex_lock(&smp_cpu_state_mutex);
999 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
1000 mutex_unlock(&smp_cpu_state_mutex);
1004 static ssize_t cpu_configure_store(struct device *dev,
1005 struct device_attribute *attr,
1006 const char *buf, size_t count)
1009 int cpu, val, rc, i;
1012 if (sscanf(buf, "%d %c", &val, &delim) != 1)
1014 if (val != 0 && val != 1)
1017 mutex_lock(&smp_cpu_state_mutex);
1019 /* disallow configuration changes of online cpus and cpu 0 */
1021 cpu -= cpu % (smp_cpu_mtid + 1);
1024 for (i = 0; i <= smp_cpu_mtid; i++)
1025 if (cpu_online(cpu + i))
1027 pcpu = pcpu_devices + cpu;
1031 if (pcpu->state != CPU_STATE_CONFIGURED)
1033 rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
1036 for (i = 0; i <= smp_cpu_mtid; i++) {
1037 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1039 pcpu[i].state = CPU_STATE_STANDBY;
1040 smp_cpu_set_polarization(cpu + i,
1041 POLARIZATION_UNKNOWN);
1043 topology_expect_change();
1046 if (pcpu->state != CPU_STATE_STANDBY)
1048 rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
1051 for (i = 0; i <= smp_cpu_mtid; i++) {
1052 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1054 pcpu[i].state = CPU_STATE_CONFIGURED;
1055 smp_cpu_set_polarization(cpu + i,
1056 POLARIZATION_UNKNOWN);
1058 topology_expect_change();
1064 mutex_unlock(&smp_cpu_state_mutex);
1066 return rc ? rc : count;
1068 static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
1069 #endif /* CONFIG_HOTPLUG_CPU */
1071 static ssize_t show_cpu_address(struct device *dev,
1072 struct device_attribute *attr, char *buf)
1074 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
1076 static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
1078 static struct attribute *cpu_common_attrs[] = {
1079 #ifdef CONFIG_HOTPLUG_CPU
1080 &dev_attr_configure.attr,
1082 &dev_attr_address.attr,
1086 static struct attribute_group cpu_common_attr_group = {
1087 .attrs = cpu_common_attrs,
1090 static struct attribute *cpu_online_attrs[] = {
1091 &dev_attr_idle_count.attr,
1092 &dev_attr_idle_time_us.attr,
1096 static struct attribute_group cpu_online_attr_group = {
1097 .attrs = cpu_online_attrs,
1100 static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
1103 unsigned int cpu = (unsigned int)(long)hcpu;
1104 struct device *s = &per_cpu(cpu_device, cpu)->dev;
1107 switch (action & ~CPU_TASKS_FROZEN) {
1109 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1112 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1115 return notifier_from_errno(err);
1118 static int smp_add_present_cpu(int cpu)
1124 c = kzalloc(sizeof(*c), GFP_KERNEL);
1127 per_cpu(cpu_device, cpu) = c;
1129 c->hotpluggable = 1;
1130 rc = register_cpu(c, cpu);
1133 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1136 if (cpu_online(cpu)) {
1137 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1141 rc = topology_cpu_init(c);
1147 if (cpu_online(cpu))
1148 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1150 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1152 #ifdef CONFIG_HOTPLUG_CPU
1159 #ifdef CONFIG_HOTPLUG_CPU
1161 int __ref smp_rescan_cpus(void)
1163 struct sclp_core_info *info;
1166 info = smp_get_core_info();
1170 mutex_lock(&smp_cpu_state_mutex);
1171 nr = __smp_rescan_cpus(info, false);
1172 mutex_unlock(&smp_cpu_state_mutex);
1176 topology_schedule_update();
1180 static ssize_t __ref rescan_store(struct device *dev,
1181 struct device_attribute *attr,
1187 rc = lock_device_hotplug_sysfs();
1190 rc = smp_rescan_cpus();
1191 unlock_device_hotplug();
1192 return rc ? rc : count;
1194 static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
1195 #endif /* CONFIG_HOTPLUG_CPU */
1197 static int __init s390_smp_init(void)
1201 #ifdef CONFIG_HOTPLUG_CPU
1202 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
1206 cpu_notifier_register_begin();
1207 for_each_present_cpu(cpu) {
1208 rc = smp_add_present_cpu(cpu);
1213 __hotcpu_notifier(smp_cpu_notify, 0);
1216 cpu_notifier_register_done();
1219 subsys_initcall(s390_smp_init);