2 * arch/s390/kernel/smp.c
4 * Copyright IBM Corp. 1999,2007
5 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
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 * We work with logical cpu numbering everywhere we can. The only
14 * functions using the real cpu address (got from STAP) are the sigp
15 * functions. For all other functions we use the identity mapping.
16 * That means that cpu_number_map[i] == i for every cpu. cpu_number_map is
17 * used e.g. to find the idle task belonging to a logical cpu. Every array
18 * in the kernel is sorted by the logical cpu number and not by the physical
19 * one which is causing all the confusion with __cpu_logical_map and
20 * cpu_number_map in other architectures.
23 #include <linux/module.h>
24 #include <linux/init.h>
26 #include <linux/spinlock.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/delay.h>
29 #include <linux/cache.h>
30 #include <linux/interrupt.h>
31 #include <linux/cpu.h>
32 #include <linux/timex.h>
33 #include <linux/bootmem.h>
35 #include <asm/setup.h>
37 #include <asm/pgalloc.h>
39 #include <asm/s390_ext.h>
40 #include <asm/cpcmd.h>
41 #include <asm/tlbflush.h>
42 #include <asm/timer.h>
43 #include <asm/lowcore.h>
46 * An array with a pointer the lowcore of every CPU.
48 struct _lowcore *lowcore_ptr[NR_CPUS];
49 EXPORT_SYMBOL(lowcore_ptr);
51 cpumask_t cpu_online_map = CPU_MASK_NONE;
52 EXPORT_SYMBOL(cpu_online_map);
54 cpumask_t cpu_possible_map = CPU_MASK_NONE;
55 EXPORT_SYMBOL(cpu_possible_map);
57 static struct task_struct *current_set[NR_CPUS];
59 static void smp_ext_bitcall(int, ec_bit_sig);
62 * Structure and data for __smp_call_function_map(). This is designed to
63 * minimise static memory requirements. It also looks cleaner.
65 static DEFINE_SPINLOCK(call_lock);
67 struct call_data_struct {
68 void (*func) (void *info);
75 static struct call_data_struct *call_data;
78 * 'Call function' interrupt callback
80 static void do_call_function(void)
82 void (*func) (void *info) = call_data->func;
83 void *info = call_data->info;
84 int wait = call_data->wait;
86 cpu_set(smp_processor_id(), call_data->started);
89 cpu_set(smp_processor_id(), call_data->finished);;
92 static void __smp_call_function_map(void (*func) (void *info), void *info,
93 int nonatomic, int wait, cpumask_t map)
95 struct call_data_struct data;
99 * Can deadlock when interrupts are disabled or if in wrong context.
101 WARN_ON(irqs_disabled() || in_irq());
104 * Check for local function call. We have to have the same call order
105 * as in on_each_cpu() because of machine_restart_smp().
107 if (cpu_isset(smp_processor_id(), map)) {
109 cpu_clear(smp_processor_id(), map);
112 cpus_and(map, map, cpu_online_map);
118 data.started = CPU_MASK_NONE;
121 data.finished = CPU_MASK_NONE;
123 spin_lock_bh(&call_lock);
126 for_each_cpu_mask(cpu, map)
127 smp_ext_bitcall(cpu, ec_call_function);
129 /* Wait for response */
130 while (!cpus_equal(map, data.started))
134 while (!cpus_equal(map, data.finished))
137 spin_unlock_bh(&call_lock);
148 * @func: the function to run; this must be fast and non-blocking
149 * @info: an arbitrary pointer to pass to the function
151 * @wait: if true, wait (atomically) until function has completed on other CPUs
153 * Run a function on all other CPUs.
155 * You must not call this function with disabled interrupts, from a
156 * hardware interrupt handler or from a bottom half.
158 int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
164 map = cpu_online_map;
165 cpu_clear(smp_processor_id(), map);
166 __smp_call_function_map(func, info, nonatomic, wait, map);
170 EXPORT_SYMBOL(smp_call_function);
173 * smp_call_function_on:
174 * @func: the function to run; this must be fast and non-blocking
175 * @info: an arbitrary pointer to pass to the function
177 * @wait: if true, wait (atomically) until function has completed on other CPUs
178 * @cpu: the CPU where func should run
180 * Run a function on one processor.
182 * You must not call this function with disabled interrupts, from a
183 * hardware interrupt handler or from a bottom half.
185 int smp_call_function_on(void (*func) (void *info), void *info, int nonatomic,
188 cpumask_t map = CPU_MASK_NONE;
192 __smp_call_function_map(func, info, nonatomic, wait, map);
196 EXPORT_SYMBOL(smp_call_function_on);
198 static void do_send_stop(void)
202 /* stop all processors */
203 for_each_online_cpu(cpu) {
204 if (cpu == smp_processor_id())
207 rc = signal_processor(cpu, sigp_stop);
208 } while (rc == sigp_busy);
212 static void do_store_status(void)
216 /* store status of all processors in their lowcores (real 0) */
217 for_each_online_cpu(cpu) {
218 if (cpu == smp_processor_id())
221 rc = signal_processor_p(
222 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
223 sigp_store_status_at_address);
224 } while (rc == sigp_busy);
228 static void do_wait_for_stop(void)
232 /* Wait for all other cpus to enter stopped state */
233 for_each_online_cpu(cpu) {
234 if (cpu == smp_processor_id())
236 while (!smp_cpu_not_running(cpu))
242 * this function sends a 'stop' sigp to all other CPUs in the system.
243 * it goes straight through.
245 void smp_send_stop(void)
247 /* Disable all interrupts/machine checks */
248 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
250 /* write magic number to zero page (absolute 0) */
251 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
253 /* stop other processors. */
256 /* wait until other processors are stopped */
259 /* store status of other processors. */
264 * Reboot, halt and power_off routines for SMP.
266 void machine_restart_smp(char *__unused)
272 void machine_halt_smp(void)
275 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
276 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
277 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
281 void machine_power_off_smp(void)
284 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
285 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
286 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
291 * This is the main routine where commands issued by other
295 static void do_ext_call_interrupt(__u16 code)
300 * handle bit signal external calls
302 * For the ec_schedule signal we have to do nothing. All the work
303 * is done automatically when we return from the interrupt.
305 bits = xchg(&S390_lowcore.ext_call_fast, 0);
307 if (test_bit(ec_call_function, &bits))
312 * Send an external call sigp to another cpu and return without waiting
313 * for its completion.
315 static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
318 * Set signaling bit in lowcore of target cpu and kick it
320 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
321 while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
327 * this function sends a 'purge tlb' signal to another CPU.
329 void smp_ptlb_callback(void *info)
334 void smp_ptlb_all(void)
336 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
338 EXPORT_SYMBOL(smp_ptlb_all);
339 #endif /* ! CONFIG_64BIT */
342 * this function sends a 'reschedule' IPI to another CPU.
343 * it goes straight through and wastes no time serializing
344 * anything. Worst case is that we lose a reschedule ...
346 void smp_send_reschedule(int cpu)
348 smp_ext_bitcall(cpu, ec_schedule);
352 * parameter area for the set/clear control bit callbacks
354 struct ec_creg_mask_parms {
355 unsigned long orvals[16];
356 unsigned long andvals[16];
360 * callback for setting/clearing control bits
362 static void smp_ctl_bit_callback(void *info)
364 struct ec_creg_mask_parms *pp = info;
365 unsigned long cregs[16];
368 __ctl_store(cregs, 0, 15);
369 for (i = 0; i <= 15; i++)
370 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
371 __ctl_load(cregs, 0, 15);
375 * Set a bit in a control register of all cpus
377 void smp_ctl_set_bit(int cr, int bit)
379 struct ec_creg_mask_parms parms;
381 memset(&parms.orvals, 0, sizeof(parms.orvals));
382 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
383 parms.orvals[cr] = 1 << bit;
384 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
386 EXPORT_SYMBOL(smp_ctl_set_bit);
389 * Clear a bit in a control register of all cpus
391 void smp_ctl_clear_bit(int cr, int bit)
393 struct ec_creg_mask_parms parms;
395 memset(&parms.orvals, 0, sizeof(parms.orvals));
396 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
397 parms.andvals[cr] = ~(1L << bit);
398 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
400 EXPORT_SYMBOL(smp_ctl_clear_bit);
402 #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_ZFCPDUMP_MODULE)
405 * zfcpdump_prefix_array holds prefix registers for the following scenario:
406 * 64 bit zfcpdump kernel and 31 bit kernel which is to be dumped. We have to
407 * save its prefix registers, since they get lost, when switching from 31 bit
410 unsigned int zfcpdump_prefix_array[NR_CPUS + 1] \
411 __attribute__((__section__(".data")));
413 static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
415 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
417 if (cpu >= NR_CPUS) {
418 printk(KERN_WARNING "Registers for cpu %i not saved since dump "
419 "kernel was compiled with NR_CPUS=%i\n", cpu, NR_CPUS);
422 zfcpdump_save_areas[cpu] = alloc_bootmem(sizeof(union save_area));
423 __cpu_logical_map[1] = (__u16) phy_cpu;
424 while (signal_processor(1, sigp_stop_and_store_status) == sigp_busy)
426 memcpy(zfcpdump_save_areas[cpu],
427 (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
430 /* copy original prefix register */
431 zfcpdump_save_areas[cpu]->s390x.pref_reg = zfcpdump_prefix_array[cpu];
435 union save_area *zfcpdump_save_areas[NR_CPUS + 1];
436 EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
440 static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
442 #endif /* CONFIG_ZFCPDUMP || CONFIG_ZFCPDUMP_MODULE */
445 * Lets check how many CPUs we have.
447 static unsigned int __init smp_count_cpus(void)
449 unsigned int cpu, num_cpus;
453 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
455 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
456 current_thread_info()->cpu = 0;
458 for (cpu = 0; cpu <= 65535; cpu++) {
459 if ((__u16) cpu == boot_cpu_addr)
461 __cpu_logical_map[1] = (__u16) cpu;
462 if (signal_processor(1, sigp_sense) == sigp_not_operational)
464 smp_get_save_area(num_cpus, cpu);
467 printk("Detected %d CPU's\n", (int) num_cpus);
468 printk("Boot cpu address %2X\n", boot_cpu_addr);
473 * Activate a secondary processor.
475 int __cpuinit start_secondary(void *cpuvoid)
480 /* Enable TOD clock interrupts on the secondary cpu. */
482 #ifdef CONFIG_VIRT_TIMER
483 /* Enable cpu timer interrupts on the secondary cpu. */
486 /* Enable pfault pseudo page faults on this cpu. */
489 /* Mark this cpu as online */
490 cpu_set(smp_processor_id(), cpu_online_map);
491 /* Switch on interrupts */
493 /* Print info about this processor */
494 print_cpu_info(&S390_lowcore.cpu_data);
495 /* cpu_idle will call schedule for us */
500 static void __init smp_create_idle(unsigned int cpu)
502 struct task_struct *p;
505 * don't care about the psw and regs settings since we'll never
506 * reschedule the forked task.
510 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
511 current_set[cpu] = p;
514 static int cpu_stopped(int cpu)
518 /* Check for stopped state */
519 if (signal_processor_ps(&status, 0, cpu, sigp_sense) ==
520 sigp_status_stored) {
527 /* Upping and downing of CPUs */
529 int __cpu_up(unsigned int cpu)
531 struct task_struct *idle;
532 struct _lowcore *cpu_lowcore;
533 struct stack_frame *sf;
537 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
538 __cpu_logical_map[cpu] = (__u16) curr_cpu;
539 if (cpu_stopped(cpu))
543 if (!cpu_stopped(cpu))
546 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
547 cpu, sigp_set_prefix);
549 printk("sigp_set_prefix failed for cpu %d "
550 "with condition code %d\n",
551 (int) cpu, (int) ccode);
555 idle = current_set[cpu];
556 cpu_lowcore = lowcore_ptr[cpu];
557 cpu_lowcore->kernel_stack = (unsigned long)
558 task_stack_page(idle) + THREAD_SIZE;
559 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
560 - sizeof(struct pt_regs)
561 - sizeof(struct stack_frame));
562 memset(sf, 0, sizeof(struct stack_frame));
563 sf->gprs[9] = (unsigned long) sf;
564 cpu_lowcore->save_area[15] = (unsigned long) sf;
565 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
568 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
569 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
570 cpu_lowcore->current_task = (unsigned long) idle;
571 cpu_lowcore->cpu_data.cpu_nr = cpu;
574 while (signal_processor(cpu, sigp_restart) == sigp_busy)
577 while (!cpu_online(cpu))
582 static unsigned int __initdata additional_cpus;
583 static unsigned int __initdata possible_cpus;
585 void __init smp_setup_cpu_possible_map(void)
587 unsigned int phy_cpus, pos_cpus, cpu;
589 phy_cpus = smp_count_cpus();
590 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
593 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
595 for (cpu = 0; cpu < pos_cpus; cpu++)
596 cpu_set(cpu, cpu_possible_map);
598 phy_cpus = min(phy_cpus, pos_cpus);
600 for (cpu = 0; cpu < phy_cpus; cpu++)
601 cpu_set(cpu, cpu_present_map);
604 #ifdef CONFIG_HOTPLUG_CPU
606 static int __init setup_additional_cpus(char *s)
608 additional_cpus = simple_strtoul(s, NULL, 0);
611 early_param("additional_cpus", setup_additional_cpus);
613 static int __init setup_possible_cpus(char *s)
615 possible_cpus = simple_strtoul(s, NULL, 0);
618 early_param("possible_cpus", setup_possible_cpus);
620 int __cpu_disable(void)
622 struct ec_creg_mask_parms cr_parms;
623 int cpu = smp_processor_id();
625 cpu_clear(cpu, cpu_online_map);
627 /* Disable pfault pseudo page faults on this cpu. */
630 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
631 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
633 /* disable all external interrupts */
634 cr_parms.orvals[0] = 0;
635 cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
636 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
637 /* disable all I/O interrupts */
638 cr_parms.orvals[6] = 0;
639 cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
640 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
641 /* disable most machine checks */
642 cr_parms.orvals[14] = 0;
643 cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
646 smp_ctl_bit_callback(&cr_parms);
651 void __cpu_die(unsigned int cpu)
653 /* Wait until target cpu is down */
654 while (!smp_cpu_not_running(cpu))
656 printk("Processor %d spun down\n", cpu);
662 signal_processor(smp_processor_id(), sigp_stop);
667 #endif /* CONFIG_HOTPLUG_CPU */
670 * Cycle through the processors and setup structures.
673 void __init smp_prepare_cpus(unsigned int max_cpus)
679 /* request the 0x1201 emergency signal external interrupt */
680 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
681 panic("Couldn't request external interrupt 0x1201");
682 memset(lowcore_ptr, 0, sizeof(lowcore_ptr));
684 * Initialize prefix pages and stacks for all possible cpus
686 print_cpu_info(&S390_lowcore.cpu_data);
688 for_each_possible_cpu(i) {
689 lowcore_ptr[i] = (struct _lowcore *)
690 __get_free_pages(GFP_KERNEL | GFP_DMA,
691 sizeof(void*) == 8 ? 1 : 0);
692 stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
693 if (!lowcore_ptr[i] || !stack)
694 panic("smp_boot_cpus failed to allocate memory\n");
696 *(lowcore_ptr[i]) = S390_lowcore;
697 lowcore_ptr[i]->async_stack = stack + ASYNC_SIZE;
698 stack = __get_free_pages(GFP_KERNEL, 0);
700 panic("smp_boot_cpus failed to allocate memory\n");
701 lowcore_ptr[i]->panic_stack = stack + PAGE_SIZE;
703 if (MACHINE_HAS_IEEE) {
704 lowcore_ptr[i]->extended_save_area_addr =
705 (__u32) __get_free_pages(GFP_KERNEL, 0);
706 if (!lowcore_ptr[i]->extended_save_area_addr)
707 panic("smp_boot_cpus failed to "
708 "allocate memory\n");
713 if (MACHINE_HAS_IEEE)
714 ctl_set_bit(14, 29); /* enable extended save area */
716 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
718 for_each_possible_cpu(cpu)
719 if (cpu != smp_processor_id())
720 smp_create_idle(cpu);
723 void __init smp_prepare_boot_cpu(void)
725 BUG_ON(smp_processor_id() != 0);
727 cpu_set(0, cpu_online_map);
728 S390_lowcore.percpu_offset = __per_cpu_offset[0];
729 current_set[0] = current;
732 void __init smp_cpus_done(unsigned int max_cpus)
734 cpu_present_map = cpu_possible_map;
738 * the frequency of the profiling timer can be changed
739 * by writing a multiplier value into /proc/profile.
741 * usually you want to run this on all CPUs ;)
743 int setup_profiling_timer(unsigned int multiplier)
748 static DEFINE_PER_CPU(struct cpu, cpu_devices);
750 static ssize_t show_capability(struct sys_device *dev, char *buf)
752 unsigned int capability;
755 rc = get_cpu_capability(&capability);
758 return sprintf(buf, "%u\n", capability);
760 static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
762 static int __cpuinit smp_cpu_notify(struct notifier_block *self,
763 unsigned long action, void *hcpu)
765 unsigned int cpu = (unsigned int)(long)hcpu;
766 struct cpu *c = &per_cpu(cpu_devices, cpu);
767 struct sys_device *s = &c->sysdev;
771 case CPU_ONLINE_FROZEN:
772 if (sysdev_create_file(s, &attr_capability))
776 case CPU_DEAD_FROZEN:
777 sysdev_remove_file(s, &attr_capability);
783 static struct notifier_block __cpuinitdata smp_cpu_nb = {
784 .notifier_call = smp_cpu_notify,
787 static int __init topology_init(void)
791 register_cpu_notifier(&smp_cpu_nb);
793 for_each_possible_cpu(cpu) {
794 struct cpu *c = &per_cpu(cpu_devices, cpu);
795 struct sys_device *s = &c->sysdev;
798 register_cpu(c, cpu);
799 if (!cpu_online(cpu))
802 sysdev_create_file(s, &attr_capability);
806 subsys_initcall(topology_init);