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 /* init per CPU timer */
462 #ifdef CONFIG_VIRT_TIMER
465 /* Enable pfault pseudo page faults on this cpu. */
468 /* Mark this cpu as online */
469 cpu_set(smp_processor_id(), cpu_online_map
);
470 /* Switch on interrupts */
472 /* Print info about this processor */
473 print_cpu_info(&S390_lowcore
.cpu_data
);
474 /* cpu_idle will call schedule for us */
479 static void __init
smp_create_idle(unsigned int cpu
)
481 struct task_struct
*p
;
484 * don't care about the psw and regs settings since we'll never
485 * reschedule the forked task.
489 panic("failed fork for CPU %u: %li", cpu
, PTR_ERR(p
));
490 current_set
[cpu
] = p
;
493 /* Reserving and releasing of CPUs */
495 static DEFINE_SPINLOCK(smp_reserve_lock
);
496 static int smp_cpu_reserved
[NR_CPUS
];
499 smp_get_cpu(cpumask_t cpu_mask
)
504 spin_lock_irqsave(&smp_reserve_lock
, flags
);
505 /* Try to find an already reserved cpu. */
506 for_each_cpu_mask(cpu
, cpu_mask
) {
507 if (smp_cpu_reserved
[cpu
] != 0) {
508 smp_cpu_reserved
[cpu
]++;
513 /* Reserve a new cpu from cpu_mask. */
514 for_each_cpu_mask(cpu
, cpu_mask
) {
515 if (cpu_online(cpu
)) {
516 smp_cpu_reserved
[cpu
]++;
522 spin_unlock_irqrestore(&smp_reserve_lock
, flags
);
531 spin_lock_irqsave(&smp_reserve_lock
, flags
);
532 smp_cpu_reserved
[cpu
]--;
533 spin_unlock_irqrestore(&smp_reserve_lock
, flags
);
541 /* Check for stopped state */
542 if (signal_processor_ps(&status
, 0, cpu
, sigp_sense
) == sigp_status_stored
) {
549 /* Upping and downing of CPUs */
552 __cpu_up(unsigned int cpu
)
554 struct task_struct
*idle
;
555 struct _lowcore
*cpu_lowcore
;
556 struct stack_frame
*sf
;
560 for (curr_cpu
= 0; curr_cpu
<= 65535; curr_cpu
++) {
561 __cpu_logical_map
[cpu
] = (__u16
) curr_cpu
;
562 if (cpu_stopped(cpu
))
566 if (!cpu_stopped(cpu
))
569 ccode
= signal_processor_p((__u32
)(unsigned long)(lowcore_ptr
[cpu
]),
570 cpu
, sigp_set_prefix
);
572 printk("sigp_set_prefix failed for cpu %d "
573 "with condition code %d\n",
574 (int) cpu
, (int) ccode
);
578 idle
= current_set
[cpu
];
579 cpu_lowcore
= lowcore_ptr
[cpu
];
580 cpu_lowcore
->kernel_stack
= (unsigned long)
581 task_stack_page(idle
) + (THREAD_SIZE
);
582 sf
= (struct stack_frame
*) (cpu_lowcore
->kernel_stack
583 - sizeof(struct pt_regs
)
584 - sizeof(struct stack_frame
));
585 memset(sf
, 0, sizeof(struct stack_frame
));
586 sf
->gprs
[9] = (unsigned long) sf
;
587 cpu_lowcore
->save_area
[15] = (unsigned long) sf
;
588 __ctl_store(cpu_lowcore
->cregs_save_area
[0], 0, 15);
591 : : "a" (&cpu_lowcore
->access_regs_save_area
) : "memory");
592 cpu_lowcore
->percpu_offset
= __per_cpu_offset
[cpu
];
593 cpu_lowcore
->current_task
= (unsigned long) idle
;
594 cpu_lowcore
->cpu_data
.cpu_nr
= cpu
;
597 while (signal_processor(cpu
,sigp_restart
) == sigp_busy
)
600 while (!cpu_online(cpu
))
605 static unsigned int __initdata additional_cpus
;
606 static unsigned int __initdata possible_cpus
;
608 void __init
smp_setup_cpu_possible_map(void)
610 unsigned int phy_cpus
, pos_cpus
, cpu
;
612 phy_cpus
= smp_count_cpus();
613 pos_cpus
= min(phy_cpus
+ additional_cpus
, (unsigned int) NR_CPUS
);
616 pos_cpus
= min(possible_cpus
, (unsigned int) NR_CPUS
);
618 for (cpu
= 0; cpu
< pos_cpus
; cpu
++)
619 cpu_set(cpu
, cpu_possible_map
);
621 phy_cpus
= min(phy_cpus
, pos_cpus
);
623 for (cpu
= 0; cpu
< phy_cpus
; cpu
++)
624 cpu_set(cpu
, cpu_present_map
);
627 #ifdef CONFIG_HOTPLUG_CPU
629 static int __init
setup_additional_cpus(char *s
)
631 additional_cpus
= simple_strtoul(s
, NULL
, 0);
634 early_param("additional_cpus", setup_additional_cpus
);
636 static int __init
setup_possible_cpus(char *s
)
638 possible_cpus
= simple_strtoul(s
, NULL
, 0);
641 early_param("possible_cpus", setup_possible_cpus
);
647 struct ec_creg_mask_parms cr_parms
;
648 int cpu
= smp_processor_id();
650 spin_lock_irqsave(&smp_reserve_lock
, flags
);
651 if (smp_cpu_reserved
[cpu
] != 0) {
652 spin_unlock_irqrestore(&smp_reserve_lock
, flags
);
655 cpu_clear(cpu
, cpu_online_map
);
657 /* Disable pfault pseudo page faults on this cpu. */
660 memset(&cr_parms
.orvals
, 0, sizeof(cr_parms
.orvals
));
661 memset(&cr_parms
.andvals
, 0xff, sizeof(cr_parms
.andvals
));
663 /* disable all external interrupts */
664 cr_parms
.orvals
[0] = 0;
665 cr_parms
.andvals
[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
666 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
667 /* disable all I/O interrupts */
668 cr_parms
.orvals
[6] = 0;
669 cr_parms
.andvals
[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
670 1<<27 | 1<<26 | 1<<25 | 1<<24);
671 /* disable most machine checks */
672 cr_parms
.orvals
[14] = 0;
673 cr_parms
.andvals
[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
675 smp_ctl_bit_callback(&cr_parms
);
677 spin_unlock_irqrestore(&smp_reserve_lock
, flags
);
682 __cpu_die(unsigned int cpu
)
684 /* Wait until target cpu is down */
685 while (!smp_cpu_not_running(cpu
))
687 printk("Processor %d spun down\n", cpu
);
694 signal_processor(smp_processor_id(), sigp_stop
);
699 #endif /* CONFIG_HOTPLUG_CPU */
702 * Cycle through the processors and setup structures.
705 void __init
smp_prepare_cpus(unsigned int max_cpus
)
711 /* request the 0x1201 emergency signal external interrupt */
712 if (register_external_interrupt(0x1201, do_ext_call_interrupt
) != 0)
713 panic("Couldn't request external interrupt 0x1201");
714 memset(lowcore_ptr
,0,sizeof(lowcore_ptr
));
716 * Initialize prefix pages and stacks for all possible cpus
718 print_cpu_info(&S390_lowcore
.cpu_data
);
720 for_each_possible_cpu(i
) {
721 lowcore_ptr
[i
] = (struct _lowcore
*)
722 __get_free_pages(GFP_KERNEL
|GFP_DMA
,
723 sizeof(void*) == 8 ? 1 : 0);
724 stack
= __get_free_pages(GFP_KERNEL
,ASYNC_ORDER
);
725 if (lowcore_ptr
[i
] == NULL
|| stack
== 0ULL)
726 panic("smp_boot_cpus failed to allocate memory\n");
728 *(lowcore_ptr
[i
]) = S390_lowcore
;
729 lowcore_ptr
[i
]->async_stack
= stack
+ (ASYNC_SIZE
);
730 stack
= __get_free_pages(GFP_KERNEL
,0);
732 panic("smp_boot_cpus failed to allocate memory\n");
733 lowcore_ptr
[i
]->panic_stack
= stack
+ (PAGE_SIZE
);
735 if (MACHINE_HAS_IEEE
) {
736 lowcore_ptr
[i
]->extended_save_area_addr
=
737 (__u32
) __get_free_pages(GFP_KERNEL
,0);
738 if (lowcore_ptr
[i
]->extended_save_area_addr
== 0)
739 panic("smp_boot_cpus failed to "
740 "allocate memory\n");
745 if (MACHINE_HAS_IEEE
)
746 ctl_set_bit(14, 29); /* enable extended save area */
748 set_prefix((u32
)(unsigned long) lowcore_ptr
[smp_processor_id()]);
750 for_each_possible_cpu(cpu
)
751 if (cpu
!= smp_processor_id())
752 smp_create_idle(cpu
);
755 void __devinit
smp_prepare_boot_cpu(void)
757 BUG_ON(smp_processor_id() != 0);
759 cpu_set(0, cpu_online_map
);
760 S390_lowcore
.percpu_offset
= __per_cpu_offset
[0];
761 current_set
[0] = current
;
764 void smp_cpus_done(unsigned int max_cpus
)
766 cpu_present_map
= cpu_possible_map
;
770 * the frequency of the profiling timer can be changed
771 * by writing a multiplier value into /proc/profile.
773 * usually you want to run this on all CPUs ;)
775 int setup_profiling_timer(unsigned int multiplier
)
780 static DEFINE_PER_CPU(struct cpu
, cpu_devices
);
782 static int __init
topology_init(void)
787 for_each_possible_cpu(cpu
) {
788 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
791 ret
= register_cpu(c
, cpu
);
793 printk(KERN_WARNING
"topology_init: register_cpu %d "
794 "failed (%d)\n", cpu
, ret
);
799 subsys_initcall(topology_init
);
801 EXPORT_SYMBOL(cpu_online_map
);
802 EXPORT_SYMBOL(cpu_possible_map
);
803 EXPORT_SYMBOL(lowcore_ptr
);
804 EXPORT_SYMBOL(smp_ctl_set_bit
);
805 EXPORT_SYMBOL(smp_ctl_clear_bit
);
806 EXPORT_SYMBOL(smp_call_function
);
807 EXPORT_SYMBOL(smp_get_cpu
);
808 EXPORT_SYMBOL(smp_put_cpu
);