2 * arch/s390/kernel/smp.c
4 * Copyright (C) IBM Corp. 1999,2006
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/smp_lock.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 <asm/setup.h>
36 #include <asm/pgalloc.h>
38 #include <asm/s390_ext.h>
39 #include <asm/cpcmd.h>
40 #include <asm/tlbflush.h>
41 #include <asm/timer.h>
43 extern volatile int __cpu_logical_map[];
46 * An array with a pointer the lowcore of every CPU.
49 struct _lowcore *lowcore_ptr[NR_CPUS];
51 cpumask_t cpu_online_map = CPU_MASK_NONE;
52 cpumask_t cpu_possible_map = CPU_MASK_NONE;
54 static struct task_struct *current_set[NR_CPUS];
56 static void smp_ext_bitcall(int, ec_bit_sig);
57 static void smp_ext_bitcall_others(ec_bit_sig);
60 5B * Structure and data for smp_call_function(). This is designed to minimise
61 * static memory requirements. It also looks cleaner.
63 static DEFINE_SPINLOCK(call_lock);
65 struct call_data_struct {
66 void (*func) (void *info);
73 static struct call_data_struct * call_data;
76 * 'Call function' interrupt callback
78 static void do_call_function(void)
80 void (*func) (void *info) = call_data->func;
81 void *info = call_data->info;
82 int wait = call_data->wait;
84 atomic_inc(&call_data->started);
87 atomic_inc(&call_data->finished);
91 * this function sends a 'generic call function' IPI to all other CPUs
95 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
98 * [SUMMARY] Run a function on all other CPUs.
99 * <func> The function to run. This must be fast and non-blocking.
100 * <info> An arbitrary pointer to pass to the function.
101 * <nonatomic> currently unused.
102 * <wait> If true, wait (atomically) until function has completed on other CPUs.
103 * [RETURNS] 0 on success, else a negative status code. Does not return until
104 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
106 * You must not call this function with disabled interrupts or from a
107 * hardware interrupt handler or from a bottom half handler.
110 struct call_data_struct data;
111 int cpus = num_online_cpus()-1;
116 /* Can deadlock when called with interrupts disabled */
117 WARN_ON(irqs_disabled());
121 atomic_set(&data.started, 0);
124 atomic_set(&data.finished, 0);
126 spin_lock(&call_lock);
128 /* Send a message to all other CPUs and wait for them to respond */
129 smp_ext_bitcall_others(ec_call_function);
131 /* Wait for response */
132 while (atomic_read(&data.started) != cpus)
136 while (atomic_read(&data.finished) != cpus)
138 spin_unlock(&call_lock);
144 * Call a function on one CPU
145 * cpu : the CPU the function should be executed on
147 * You must not call this function with disabled interrupts or from a
148 * hardware interrupt handler. You may call it from a bottom half.
150 * It is guaranteed that the called function runs on the specified CPU,
151 * preemption is disabled.
153 int smp_call_function_on(void (*func) (void *info), void *info,
154 int nonatomic, int wait, int cpu)
156 struct call_data_struct data;
159 if (!cpu_online(cpu))
162 /* disable preemption for local function call */
163 curr_cpu = get_cpu();
165 if (curr_cpu == cpu) {
166 /* direct call to function */
174 atomic_set(&data.started, 0);
177 atomic_set(&data.finished, 0);
179 spin_lock_bh(&call_lock);
181 smp_ext_bitcall(cpu, ec_call_function);
183 /* Wait for response */
184 while (atomic_read(&data.started) != 1)
188 while (atomic_read(&data.finished) != 1)
191 spin_unlock_bh(&call_lock);
195 EXPORT_SYMBOL(smp_call_function_on);
197 static inline void do_send_stop(void)
201 /* stop all processors */
202 for_each_online_cpu(cpu) {
203 if (cpu == smp_processor_id())
206 rc = signal_processor(cpu, sigp_stop);
207 } while (rc == sigp_busy);
211 static inline void do_store_status(void)
215 /* store status of all processors in their lowcores (real 0) */
216 for_each_online_cpu(cpu) {
217 if (cpu == smp_processor_id())
220 rc = signal_processor_p(
221 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
222 sigp_store_status_at_address);
223 } while(rc == sigp_busy);
227 static inline void do_wait_for_stop(void)
231 /* Wait for all other cpus to enter stopped state */
232 for_each_online_cpu(cpu) {
233 if (cpu == smp_processor_id())
235 while(!smp_cpu_not_running(cpu))
241 * this function sends a 'stop' sigp to all other CPUs in the system.
242 * it goes straight through.
244 void smp_send_stop(void)
246 /* Disable all interrupts/machine checks */
247 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
249 /* write magic number to zero page (absolute 0) */
250 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
252 /* stop other processors. */
255 /* wait until other processors are stopped */
258 /* store status of other processors. */
263 * 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)
326 * Send an external call sigp to every other cpu in the system and
327 * return without waiting for its completion.
329 static void smp_ext_bitcall_others(ec_bit_sig sig)
333 for_each_online_cpu(cpu) {
334 if (cpu == smp_processor_id())
337 * Set signaling bit in lowcore of target cpu and kick it
339 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
340 while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
347 * this function sends a 'purge tlb' signal to another CPU.
349 void smp_ptlb_callback(void *info)
354 void smp_ptlb_all(void)
356 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
358 EXPORT_SYMBOL(smp_ptlb_all);
359 #endif /* ! CONFIG_64BIT */
362 * this function sends a 'reschedule' IPI to another CPU.
363 * it goes straight through and wastes no time serializing
364 * anything. Worst case is that we lose a reschedule ...
366 void smp_send_reschedule(int cpu)
368 smp_ext_bitcall(cpu, ec_schedule);
372 * parameter area for the set/clear control bit callbacks
374 struct ec_creg_mask_parms {
375 unsigned long orvals[16];
376 unsigned long andvals[16];
380 * callback for setting/clearing control bits
382 static void smp_ctl_bit_callback(void *info) {
383 struct ec_creg_mask_parms *pp = info;
384 unsigned long cregs[16];
387 __ctl_store(cregs, 0, 15);
388 for (i = 0; i <= 15; i++)
389 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
390 __ctl_load(cregs, 0, 15);
394 * Set a bit in a control register of all cpus
396 void smp_ctl_set_bit(int cr, int bit)
398 struct ec_creg_mask_parms parms;
400 memset(&parms.orvals, 0, sizeof(parms.orvals));
401 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
402 parms.orvals[cr] = 1 << bit;
403 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
407 * Clear a bit in a control register of all cpus
409 void smp_ctl_clear_bit(int cr, int bit)
411 struct ec_creg_mask_parms parms;
413 memset(&parms.orvals, 0, sizeof(parms.orvals));
414 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
415 parms.andvals[cr] = ~(1L << bit);
416 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
420 * Lets check how many CPUs we have.
424 __init smp_count_cpus(void)
426 unsigned int cpu, num_cpus;
430 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
433 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
434 current_thread_info()->cpu = 0;
436 for (cpu = 0; cpu <= 65535; cpu++) {
437 if ((__u16) cpu == boot_cpu_addr)
439 __cpu_logical_map[1] = (__u16) cpu;
440 if (signal_processor(1, sigp_sense) ==
441 sigp_not_operational)
446 printk("Detected %d CPU's\n",(int) num_cpus);
447 printk("Boot cpu address %2X\n", boot_cpu_addr);
453 * Activate a secondary processor.
455 int __devinit start_secondary(void *cpuvoid)
460 /* Enable TOD clock interrupts on the secondary cpu. */
462 #ifdef CONFIG_VIRT_TIMER
463 /* Enable cpu timer interrupts on the secondary cpu. */
466 /* Enable pfault pseudo page faults on this cpu. */
469 /* Mark this cpu as online */
470 cpu_set(smp_processor_id(), cpu_online_map);
471 /* Switch on interrupts */
473 /* Print info about this processor */
474 print_cpu_info(&S390_lowcore.cpu_data);
475 /* cpu_idle will call schedule for us */
480 static void __init smp_create_idle(unsigned int cpu)
482 struct task_struct *p;
485 * don't care about the psw and regs settings since we'll never
486 * reschedule the forked task.
490 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
491 current_set[cpu] = p;
494 /* Reserving and releasing of CPUs */
496 static DEFINE_SPINLOCK(smp_reserve_lock);
497 static int smp_cpu_reserved[NR_CPUS];
500 smp_get_cpu(cpumask_t cpu_mask)
505 spin_lock_irqsave(&smp_reserve_lock, flags);
506 /* Try to find an already reserved cpu. */
507 for_each_cpu_mask(cpu, cpu_mask) {
508 if (smp_cpu_reserved[cpu] != 0) {
509 smp_cpu_reserved[cpu]++;
514 /* Reserve a new cpu from cpu_mask. */
515 for_each_cpu_mask(cpu, cpu_mask) {
516 if (cpu_online(cpu)) {
517 smp_cpu_reserved[cpu]++;
523 spin_unlock_irqrestore(&smp_reserve_lock, flags);
532 spin_lock_irqsave(&smp_reserve_lock, flags);
533 smp_cpu_reserved[cpu]--;
534 spin_unlock_irqrestore(&smp_reserve_lock, flags);
542 /* Check for stopped state */
543 if (signal_processor_ps(&status, 0, cpu, sigp_sense) == sigp_status_stored) {
550 /* Upping and downing of CPUs */
553 __cpu_up(unsigned int cpu)
555 struct task_struct *idle;
556 struct _lowcore *cpu_lowcore;
557 struct stack_frame *sf;
561 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
562 __cpu_logical_map[cpu] = (__u16) curr_cpu;
563 if (cpu_stopped(cpu))
567 if (!cpu_stopped(cpu))
570 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
571 cpu, sigp_set_prefix);
573 printk("sigp_set_prefix failed for cpu %d "
574 "with condition code %d\n",
575 (int) cpu, (int) ccode);
579 idle = current_set[cpu];
580 cpu_lowcore = lowcore_ptr[cpu];
581 cpu_lowcore->kernel_stack = (unsigned long)
582 task_stack_page(idle) + (THREAD_SIZE);
583 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
584 - sizeof(struct pt_regs)
585 - sizeof(struct stack_frame));
586 memset(sf, 0, sizeof(struct stack_frame));
587 sf->gprs[9] = (unsigned long) sf;
588 cpu_lowcore->save_area[15] = (unsigned long) sf;
589 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
592 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
593 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
594 cpu_lowcore->current_task = (unsigned long) idle;
595 cpu_lowcore->cpu_data.cpu_nr = cpu;
598 while (signal_processor(cpu,sigp_restart) == sigp_busy)
601 while (!cpu_online(cpu))
606 static unsigned int __initdata additional_cpus;
607 static unsigned int __initdata possible_cpus;
609 void __init smp_setup_cpu_possible_map(void)
611 unsigned int phy_cpus, pos_cpus, cpu;
613 phy_cpus = smp_count_cpus();
614 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
617 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
619 for (cpu = 0; cpu < pos_cpus; cpu++)
620 cpu_set(cpu, cpu_possible_map);
622 phy_cpus = min(phy_cpus, pos_cpus);
624 for (cpu = 0; cpu < phy_cpus; cpu++)
625 cpu_set(cpu, cpu_present_map);
628 #ifdef CONFIG_HOTPLUG_CPU
630 static int __init setup_additional_cpus(char *s)
632 additional_cpus = simple_strtoul(s, NULL, 0);
635 early_param("additional_cpus", setup_additional_cpus);
637 static int __init setup_possible_cpus(char *s)
639 possible_cpus = simple_strtoul(s, NULL, 0);
642 early_param("possible_cpus", setup_possible_cpus);
648 struct ec_creg_mask_parms cr_parms;
649 int cpu = smp_processor_id();
651 spin_lock_irqsave(&smp_reserve_lock, flags);
652 if (smp_cpu_reserved[cpu] != 0) {
653 spin_unlock_irqrestore(&smp_reserve_lock, flags);
656 cpu_clear(cpu, cpu_online_map);
658 /* Disable pfault pseudo page faults on this cpu. */
661 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
662 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
664 /* disable all external interrupts */
665 cr_parms.orvals[0] = 0;
666 cr_parms.andvals[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
667 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
668 /* disable all I/O interrupts */
669 cr_parms.orvals[6] = 0;
670 cr_parms.andvals[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
671 1<<27 | 1<<26 | 1<<25 | 1<<24);
672 /* disable most machine checks */
673 cr_parms.orvals[14] = 0;
674 cr_parms.andvals[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
676 smp_ctl_bit_callback(&cr_parms);
678 spin_unlock_irqrestore(&smp_reserve_lock, flags);
683 __cpu_die(unsigned int cpu)
685 /* Wait until target cpu is down */
686 while (!smp_cpu_not_running(cpu))
688 printk("Processor %d spun down\n", cpu);
695 signal_processor(smp_processor_id(), sigp_stop);
700 #endif /* CONFIG_HOTPLUG_CPU */
703 * Cycle through the processors and setup structures.
706 void __init smp_prepare_cpus(unsigned int max_cpus)
712 /* request the 0x1201 emergency signal external interrupt */
713 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
714 panic("Couldn't request external interrupt 0x1201");
715 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
717 * Initialize prefix pages and stacks for all possible cpus
719 print_cpu_info(&S390_lowcore.cpu_data);
721 for_each_possible_cpu(i) {
722 lowcore_ptr[i] = (struct _lowcore *)
723 __get_free_pages(GFP_KERNEL|GFP_DMA,
724 sizeof(void*) == 8 ? 1 : 0);
725 stack = __get_free_pages(GFP_KERNEL,ASYNC_ORDER);
726 if (lowcore_ptr[i] == NULL || stack == 0ULL)
727 panic("smp_boot_cpus failed to allocate memory\n");
729 *(lowcore_ptr[i]) = S390_lowcore;
730 lowcore_ptr[i]->async_stack = stack + (ASYNC_SIZE);
731 stack = __get_free_pages(GFP_KERNEL,0);
733 panic("smp_boot_cpus failed to allocate memory\n");
734 lowcore_ptr[i]->panic_stack = stack + (PAGE_SIZE);
736 if (MACHINE_HAS_IEEE) {
737 lowcore_ptr[i]->extended_save_area_addr =
738 (__u32) __get_free_pages(GFP_KERNEL,0);
739 if (lowcore_ptr[i]->extended_save_area_addr == 0)
740 panic("smp_boot_cpus failed to "
741 "allocate memory\n");
746 if (MACHINE_HAS_IEEE)
747 ctl_set_bit(14, 29); /* enable extended save area */
749 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
751 for_each_possible_cpu(cpu)
752 if (cpu != smp_processor_id())
753 smp_create_idle(cpu);
756 void __devinit smp_prepare_boot_cpu(void)
758 BUG_ON(smp_processor_id() != 0);
760 cpu_set(0, cpu_online_map);
761 S390_lowcore.percpu_offset = __per_cpu_offset[0];
762 current_set[0] = current;
765 void smp_cpus_done(unsigned int max_cpus)
767 cpu_present_map = cpu_possible_map;
771 * the frequency of the profiling timer can be changed
772 * by writing a multiplier value into /proc/profile.
774 * usually you want to run this on all CPUs ;)
776 int setup_profiling_timer(unsigned int multiplier)
781 static DEFINE_PER_CPU(struct cpu, cpu_devices);
783 static int __init topology_init(void)
788 for_each_possible_cpu(cpu) {
789 struct cpu *c = &per_cpu(cpu_devices, cpu);
792 ret = register_cpu(c, cpu);
794 printk(KERN_WARNING "topology_init: register_cpu %d "
795 "failed (%d)\n", cpu, ret);
800 subsys_initcall(topology_init);
802 EXPORT_SYMBOL(cpu_online_map);
803 EXPORT_SYMBOL(cpu_possible_map);
804 EXPORT_SYMBOL(lowcore_ptr);
805 EXPORT_SYMBOL(smp_ctl_set_bit);
806 EXPORT_SYMBOL(smp_ctl_clear_bit);
807 EXPORT_SYMBOL(smp_call_function);
808 EXPORT_SYMBOL(smp_get_cpu);
809 EXPORT_SYMBOL(smp_put_cpu);