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/err.h>
27 #include <linux/spinlock.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/delay.h>
30 #include <linux/cache.h>
31 #include <linux/interrupt.h>
32 #include <linux/cpu.h>
33 #include <linux/timex.h>
34 #include <linux/bootmem.h>
36 #include <asm/setup.h>
38 #include <asm/pgalloc.h>
40 #include <asm/s390_ext.h>
41 #include <asm/cpcmd.h>
42 #include <asm/tlbflush.h>
43 #include <asm/timer.h>
44 #include <asm/lowcore.h>
48 * An array with a pointer the lowcore of every CPU.
50 struct _lowcore *lowcore_ptr[NR_CPUS];
51 EXPORT_SYMBOL(lowcore_ptr);
53 cpumask_t cpu_online_map = CPU_MASK_NONE;
54 EXPORT_SYMBOL(cpu_online_map);
56 cpumask_t cpu_possible_map = CPU_MASK_NONE;
57 EXPORT_SYMBOL(cpu_possible_map);
59 static struct task_struct *current_set[NR_CPUS];
61 static void smp_ext_bitcall(int, ec_bit_sig);
64 * Structure and data for __smp_call_function_map(). This is designed to
65 * minimise static memory requirements. It also looks cleaner.
67 static DEFINE_SPINLOCK(call_lock);
69 struct call_data_struct {
70 void (*func) (void *info);
77 static struct call_data_struct *call_data;
80 * 'Call function' interrupt callback
82 static void do_call_function(void)
84 void (*func) (void *info) = call_data->func;
85 void *info = call_data->info;
86 int wait = call_data->wait;
88 cpu_set(smp_processor_id(), call_data->started);
91 cpu_set(smp_processor_id(), call_data->finished);;
94 static void __smp_call_function_map(void (*func) (void *info), void *info,
95 int nonatomic, int wait, cpumask_t map)
97 struct call_data_struct data;
101 * Can deadlock when interrupts are disabled or if in wrong context.
103 WARN_ON(irqs_disabled() || in_irq());
106 * Check for local function call. We have to have the same call order
107 * as in on_each_cpu() because of machine_restart_smp().
109 if (cpu_isset(smp_processor_id(), map)) {
111 cpu_clear(smp_processor_id(), map);
114 cpus_and(map, map, cpu_online_map);
120 data.started = CPU_MASK_NONE;
123 data.finished = CPU_MASK_NONE;
125 spin_lock(&call_lock);
128 for_each_cpu_mask(cpu, map)
129 smp_ext_bitcall(cpu, ec_call_function);
131 /* Wait for response */
132 while (!cpus_equal(map, data.started))
135 while (!cpus_equal(map, data.finished))
137 spin_unlock(&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_single:
174 * @cpu: the CPU where func should run
175 * @func: the function to run; this must be fast and non-blocking
176 * @info: an arbitrary pointer to pass to the function
178 * @wait: if true, wait (atomically) until function has completed on other CPUs
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_single(int cpu, void (*func) (void *info), void *info,
186 int nonatomic, int wait)
189 __smp_call_function_map(func, info, nonatomic, wait,
190 cpumask_of_cpu(cpu));
194 EXPORT_SYMBOL(smp_call_function_single);
196 void smp_send_stop(void)
200 /* Disable all interrupts/machine checks */
201 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
203 /* write magic number to zero page (absolute 0) */
204 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
206 /* stop all processors */
207 for_each_online_cpu(cpu) {
208 if (cpu == smp_processor_id())
211 rc = signal_processor(cpu, sigp_stop);
212 } while (rc == sigp_busy);
214 while (!smp_cpu_not_running(cpu))
220 * Reboot, halt and power_off routines for SMP.
222 void machine_restart_smp(char *__unused)
228 void machine_halt_smp(void)
231 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
232 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
233 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
237 void machine_power_off_smp(void)
240 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
241 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
242 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
247 * This is the main routine where commands issued by other
251 static void do_ext_call_interrupt(__u16 code)
256 * handle bit signal external calls
258 * For the ec_schedule signal we have to do nothing. All the work
259 * is done automatically when we return from the interrupt.
261 bits = xchg(&S390_lowcore.ext_call_fast, 0);
263 if (test_bit(ec_call_function, &bits))
268 * Send an external call sigp to another cpu and return without waiting
269 * for its completion.
271 static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
274 * Set signaling bit in lowcore of target cpu and kick it
276 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
277 while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
283 * this function sends a 'purge tlb' signal to another CPU.
285 void smp_ptlb_callback(void *info)
290 void smp_ptlb_all(void)
292 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
294 EXPORT_SYMBOL(smp_ptlb_all);
295 #endif /* ! CONFIG_64BIT */
298 * this function sends a 'reschedule' IPI to another CPU.
299 * it goes straight through and wastes no time serializing
300 * anything. Worst case is that we lose a reschedule ...
302 void smp_send_reschedule(int cpu)
304 smp_ext_bitcall(cpu, ec_schedule);
308 * parameter area for the set/clear control bit callbacks
310 struct ec_creg_mask_parms {
311 unsigned long orvals[16];
312 unsigned long andvals[16];
316 * callback for setting/clearing control bits
318 static void smp_ctl_bit_callback(void *info)
320 struct ec_creg_mask_parms *pp = info;
321 unsigned long cregs[16];
324 __ctl_store(cregs, 0, 15);
325 for (i = 0; i <= 15; i++)
326 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
327 __ctl_load(cregs, 0, 15);
331 * Set a bit in a control register of all cpus
333 void smp_ctl_set_bit(int cr, int bit)
335 struct ec_creg_mask_parms parms;
337 memset(&parms.orvals, 0, sizeof(parms.orvals));
338 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
339 parms.orvals[cr] = 1 << bit;
340 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
342 EXPORT_SYMBOL(smp_ctl_set_bit);
345 * Clear a bit in a control register of all cpus
347 void smp_ctl_clear_bit(int cr, int bit)
349 struct ec_creg_mask_parms parms;
351 memset(&parms.orvals, 0, sizeof(parms.orvals));
352 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
353 parms.andvals[cr] = ~(1L << bit);
354 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
356 EXPORT_SYMBOL(smp_ctl_clear_bit);
358 #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_ZFCPDUMP_MODULE)
361 * zfcpdump_prefix_array holds prefix registers for the following scenario:
362 * 64 bit zfcpdump kernel and 31 bit kernel which is to be dumped. We have to
363 * save its prefix registers, since they get lost, when switching from 31 bit
366 unsigned int zfcpdump_prefix_array[NR_CPUS + 1] \
367 __attribute__((__section__(".data")));
369 static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
371 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
373 if (cpu >= NR_CPUS) {
374 printk(KERN_WARNING "Registers for cpu %i not saved since dump "
375 "kernel was compiled with NR_CPUS=%i\n", cpu, NR_CPUS);
378 zfcpdump_save_areas[cpu] = alloc_bootmem(sizeof(union save_area));
379 __cpu_logical_map[1] = (__u16) phy_cpu;
380 while (signal_processor(1, sigp_stop_and_store_status) == sigp_busy)
382 memcpy(zfcpdump_save_areas[cpu],
383 (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
386 /* copy original prefix register */
387 zfcpdump_save_areas[cpu]->s390x.pref_reg = zfcpdump_prefix_array[cpu];
391 union save_area *zfcpdump_save_areas[NR_CPUS + 1];
392 EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
396 static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
398 #endif /* CONFIG_ZFCPDUMP || CONFIG_ZFCPDUMP_MODULE */
401 * Lets check how many CPUs we have.
403 static unsigned int __init smp_count_cpus(void)
405 unsigned int cpu, num_cpus;
409 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
411 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
412 current_thread_info()->cpu = 0;
414 for (cpu = 0; cpu <= 65535; cpu++) {
415 if ((__u16) cpu == boot_cpu_addr)
417 __cpu_logical_map[1] = (__u16) cpu;
418 if (signal_processor(1, sigp_sense) == sigp_not_operational)
420 smp_get_save_area(num_cpus, cpu);
423 printk("Detected %d CPU's\n", (int) num_cpus);
424 printk("Boot cpu address %2X\n", boot_cpu_addr);
429 * Activate a secondary processor.
431 int __cpuinit start_secondary(void *cpuvoid)
436 /* Enable TOD clock interrupts on the secondary cpu. */
438 #ifdef CONFIG_VIRT_TIMER
439 /* Enable cpu timer interrupts on the secondary cpu. */
442 /* Enable pfault pseudo page faults on this cpu. */
445 /* Mark this cpu as online */
446 cpu_set(smp_processor_id(), cpu_online_map);
447 /* Switch on interrupts */
449 /* Print info about this processor */
450 print_cpu_info(&S390_lowcore.cpu_data);
451 /* cpu_idle will call schedule for us */
456 DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
458 static void __init smp_create_idle(unsigned int cpu)
460 struct task_struct *p;
463 * don't care about the psw and regs settings since we'll never
464 * reschedule the forked task.
468 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
469 current_set[cpu] = p;
470 spin_lock_init(&(&per_cpu(s390_idle, cpu))->lock);
473 static int cpu_stopped(int cpu)
477 /* Check for stopped state */
478 if (signal_processor_ps(&status, 0, cpu, sigp_sense) ==
479 sigp_status_stored) {
486 /* Upping and downing of CPUs */
488 int __cpu_up(unsigned int cpu)
490 struct task_struct *idle;
491 struct _lowcore *cpu_lowcore;
492 struct stack_frame *sf;
496 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
497 __cpu_logical_map[cpu] = (__u16) curr_cpu;
498 if (cpu_stopped(cpu))
502 if (!cpu_stopped(cpu))
505 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
506 cpu, sigp_set_prefix);
508 printk("sigp_set_prefix failed for cpu %d "
509 "with condition code %d\n",
510 (int) cpu, (int) ccode);
514 idle = current_set[cpu];
515 cpu_lowcore = lowcore_ptr[cpu];
516 cpu_lowcore->kernel_stack = (unsigned long)
517 task_stack_page(idle) + THREAD_SIZE;
518 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
519 - sizeof(struct pt_regs)
520 - sizeof(struct stack_frame));
521 memset(sf, 0, sizeof(struct stack_frame));
522 sf->gprs[9] = (unsigned long) sf;
523 cpu_lowcore->save_area[15] = (unsigned long) sf;
524 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
527 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
528 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
529 cpu_lowcore->current_task = (unsigned long) idle;
530 cpu_lowcore->cpu_data.cpu_nr = cpu;
533 while (signal_processor(cpu, sigp_restart) == sigp_busy)
536 while (!cpu_online(cpu))
541 static unsigned int __initdata additional_cpus;
542 static unsigned int __initdata possible_cpus;
544 void __init smp_setup_cpu_possible_map(void)
546 unsigned int phy_cpus, pos_cpus, cpu;
548 phy_cpus = smp_count_cpus();
549 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
552 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
554 for (cpu = 0; cpu < pos_cpus; cpu++)
555 cpu_set(cpu, cpu_possible_map);
557 phy_cpus = min(phy_cpus, pos_cpus);
559 for (cpu = 0; cpu < phy_cpus; cpu++)
560 cpu_set(cpu, cpu_present_map);
563 #ifdef CONFIG_HOTPLUG_CPU
565 static int __init setup_additional_cpus(char *s)
567 additional_cpus = simple_strtoul(s, NULL, 0);
570 early_param("additional_cpus", setup_additional_cpus);
572 static int __init setup_possible_cpus(char *s)
574 possible_cpus = simple_strtoul(s, NULL, 0);
577 early_param("possible_cpus", setup_possible_cpus);
579 int __cpu_disable(void)
581 struct ec_creg_mask_parms cr_parms;
582 int cpu = smp_processor_id();
584 cpu_clear(cpu, cpu_online_map);
586 /* Disable pfault pseudo page faults on this cpu. */
589 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
590 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
592 /* disable all external interrupts */
593 cr_parms.orvals[0] = 0;
594 cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
595 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
596 /* disable all I/O interrupts */
597 cr_parms.orvals[6] = 0;
598 cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
599 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
600 /* disable most machine checks */
601 cr_parms.orvals[14] = 0;
602 cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
605 smp_ctl_bit_callback(&cr_parms);
610 void __cpu_die(unsigned int cpu)
612 /* Wait until target cpu is down */
613 while (!smp_cpu_not_running(cpu))
615 printk("Processor %d spun down\n", cpu);
621 signal_processor(smp_processor_id(), sigp_stop);
626 #endif /* CONFIG_HOTPLUG_CPU */
629 * Cycle through the processors and setup structures.
632 void __init smp_prepare_cpus(unsigned int max_cpus)
638 /* request the 0x1201 emergency signal external interrupt */
639 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
640 panic("Couldn't request external interrupt 0x1201");
641 memset(lowcore_ptr, 0, sizeof(lowcore_ptr));
643 * Initialize prefix pages and stacks for all possible cpus
645 print_cpu_info(&S390_lowcore.cpu_data);
647 for_each_possible_cpu(i) {
648 lowcore_ptr[i] = (struct _lowcore *)
649 __get_free_pages(GFP_KERNEL | GFP_DMA,
650 sizeof(void*) == 8 ? 1 : 0);
651 stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
652 if (!lowcore_ptr[i] || !stack)
653 panic("smp_boot_cpus failed to allocate memory\n");
655 *(lowcore_ptr[i]) = S390_lowcore;
656 lowcore_ptr[i]->async_stack = stack + ASYNC_SIZE;
657 stack = __get_free_pages(GFP_KERNEL, 0);
659 panic("smp_boot_cpus failed to allocate memory\n");
660 lowcore_ptr[i]->panic_stack = stack + PAGE_SIZE;
662 if (MACHINE_HAS_IEEE) {
663 lowcore_ptr[i]->extended_save_area_addr =
664 (__u32) __get_free_pages(GFP_KERNEL, 0);
665 if (!lowcore_ptr[i]->extended_save_area_addr)
666 panic("smp_boot_cpus failed to "
667 "allocate memory\n");
672 if (MACHINE_HAS_IEEE)
673 ctl_set_bit(14, 29); /* enable extended save area */
675 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
677 for_each_possible_cpu(cpu)
678 if (cpu != smp_processor_id())
679 smp_create_idle(cpu);
682 void __init smp_prepare_boot_cpu(void)
684 BUG_ON(smp_processor_id() != 0);
686 cpu_set(0, cpu_online_map);
687 S390_lowcore.percpu_offset = __per_cpu_offset[0];
688 current_set[0] = current;
689 spin_lock_init(&(&__get_cpu_var(s390_idle))->lock);
692 void __init smp_cpus_done(unsigned int max_cpus)
694 cpu_present_map = cpu_possible_map;
698 * the frequency of the profiling timer can be changed
699 * by writing a multiplier value into /proc/profile.
701 * usually you want to run this on all CPUs ;)
703 int setup_profiling_timer(unsigned int multiplier)
708 static DEFINE_PER_CPU(struct cpu, cpu_devices);
710 static ssize_t show_capability(struct sys_device *dev, char *buf)
712 unsigned int capability;
715 rc = get_cpu_capability(&capability);
718 return sprintf(buf, "%u\n", capability);
720 static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
722 static ssize_t show_idle_count(struct sys_device *dev, char *buf)
724 struct s390_idle_data *idle;
725 unsigned long long idle_count;
727 idle = &per_cpu(s390_idle, dev->id);
728 spin_lock_irq(&idle->lock);
729 idle_count = idle->idle_count;
730 spin_unlock_irq(&idle->lock);
731 return sprintf(buf, "%llu\n", idle_count);
733 static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
735 static ssize_t show_idle_time(struct sys_device *dev, char *buf)
737 struct s390_idle_data *idle;
738 unsigned long long new_time;
740 idle = &per_cpu(s390_idle, dev->id);
741 spin_lock_irq(&idle->lock);
743 new_time = get_clock();
744 idle->idle_time += new_time - idle->idle_enter;
745 idle->idle_enter = new_time;
747 new_time = idle->idle_time;
748 spin_unlock_irq(&idle->lock);
749 return sprintf(buf, "%llu\n", new_time >> 12);
751 static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
753 static struct attribute *cpu_attrs[] = {
754 &attr_capability.attr,
755 &attr_idle_count.attr,
756 &attr_idle_time_us.attr,
760 static struct attribute_group cpu_attr_group = {
764 static int __cpuinit smp_cpu_notify(struct notifier_block *self,
765 unsigned long action, void *hcpu)
767 unsigned int cpu = (unsigned int)(long)hcpu;
768 struct cpu *c = &per_cpu(cpu_devices, cpu);
769 struct sys_device *s = &c->sysdev;
770 struct s390_idle_data *idle;
774 case CPU_ONLINE_FROZEN:
775 idle = &per_cpu(s390_idle, cpu);
776 spin_lock_irq(&idle->lock);
777 idle->idle_enter = 0;
779 idle->idle_count = 0;
780 spin_unlock_irq(&idle->lock);
781 if (sysfs_create_group(&s->kobj, &cpu_attr_group))
785 case CPU_DEAD_FROZEN:
786 sysfs_remove_group(&s->kobj, &cpu_attr_group);
792 static struct notifier_block __cpuinitdata smp_cpu_nb = {
793 .notifier_call = smp_cpu_notify,
796 static int __init topology_init(void)
801 register_cpu_notifier(&smp_cpu_nb);
803 for_each_possible_cpu(cpu) {
804 struct cpu *c = &per_cpu(cpu_devices, cpu);
805 struct sys_device *s = &c->sysdev;
808 register_cpu(c, cpu);
809 if (!cpu_online(cpu))
812 rc = sysfs_create_group(&s->kobj, &cpu_attr_group);
818 subsys_initcall(topology_init);