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>
27 #include <linux/spinlock.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/smp_lock.h>
31 #include <linux/delay.h>
32 #include <linux/cache.h>
33 #include <linux/interrupt.h>
34 #include <linux/cpu.h>
37 #include <asm/pgalloc.h>
39 #include <asm/s390_ext.h>
40 #include <asm/cpcmd.h>
41 #include <asm/tlbflush.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];
57 * Reboot, halt and power_off routines for SMP.
59 extern char vmhalt_cmd[];
60 extern char vmpoff_cmd[];
62 static void smp_ext_bitcall(int, ec_bit_sig);
63 static void smp_ext_bitcall_others(ec_bit_sig);
66 5B * Structure and data for smp_call_function(). This is designed to minimise
67 * static memory requirements. It also looks cleaner.
69 static DEFINE_SPINLOCK(call_lock);
71 struct call_data_struct {
72 void (*func) (void *info);
79 static struct call_data_struct * call_data;
82 * 'Call function' interrupt callback
84 static void do_call_function(void)
86 void (*func) (void *info) = call_data->func;
87 void *info = call_data->info;
88 int wait = call_data->wait;
90 atomic_inc(&call_data->started);
93 atomic_inc(&call_data->finished);
97 * this function sends a 'generic call function' IPI to all other CPUs
101 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
104 * [SUMMARY] Run a function on all other CPUs.
105 * <func> The function to run. This must be fast and non-blocking.
106 * <info> An arbitrary pointer to pass to the function.
107 * <nonatomic> currently unused.
108 * <wait> If true, wait (atomically) until function has completed on other CPUs.
109 * [RETURNS] 0 on success, else a negative status code. Does not return until
110 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
112 * You must not call this function with disabled interrupts or from a
113 * hardware interrupt handler or from a bottom half handler.
116 struct call_data_struct data;
117 int cpus = num_online_cpus()-1;
122 /* Can deadlock when called with interrupts disabled */
123 WARN_ON(irqs_disabled());
127 atomic_set(&data.started, 0);
130 atomic_set(&data.finished, 0);
132 spin_lock(&call_lock);
134 /* Send a message to all other CPUs and wait for them to respond */
135 smp_ext_bitcall_others(ec_call_function);
137 /* Wait for response */
138 while (atomic_read(&data.started) != cpus)
142 while (atomic_read(&data.finished) != cpus)
144 spin_unlock(&call_lock);
150 * Call a function on one CPU
151 * cpu : the CPU the function should be executed on
153 * You must not call this function with disabled interrupts or from a
154 * hardware interrupt handler. You may call it from a bottom half.
156 * It is guaranteed that the called function runs on the specified CPU,
157 * preemption is disabled.
159 int smp_call_function_on(void (*func) (void *info), void *info,
160 int nonatomic, int wait, int cpu)
162 struct call_data_struct data;
165 if (!cpu_online(cpu))
168 /* disable preemption for local function call */
169 curr_cpu = get_cpu();
171 if (curr_cpu == cpu) {
172 /* direct call to function */
180 atomic_set(&data.started, 0);
183 atomic_set(&data.finished, 0);
185 spin_lock_bh(&call_lock);
187 smp_ext_bitcall(cpu, ec_call_function);
189 /* Wait for response */
190 while (atomic_read(&data.started) != 1)
194 while (atomic_read(&data.finished) != 1)
197 spin_unlock_bh(&call_lock);
201 EXPORT_SYMBOL(smp_call_function_on);
203 static inline void do_send_stop(void)
207 /* stop all processors */
208 for_each_online_cpu(cpu) {
209 if (cpu == smp_processor_id())
212 rc = signal_processor(cpu, sigp_stop);
213 } while (rc == sigp_busy);
217 static inline void do_store_status(void)
221 /* store status of all processors in their lowcores (real 0) */
222 for_each_online_cpu(cpu) {
223 if (cpu == smp_processor_id())
226 rc = signal_processor_p(
227 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
228 sigp_store_status_at_address);
229 } while(rc == sigp_busy);
233 static inline void do_wait_for_stop(void)
237 /* Wait for all other cpus to enter stopped state */
238 for_each_online_cpu(cpu) {
239 if (cpu == smp_processor_id())
241 while(!smp_cpu_not_running(cpu))
247 * this function sends a 'stop' sigp to all other CPUs in the system.
248 * it goes straight through.
250 void smp_send_stop(void)
252 /* Disable all interrupts/machine checks */
253 __load_psw_mask(PSW_KERNEL_BITS & ~PSW_MASK_MCHECK);
255 /* write magic number to zero page (absolute 0) */
256 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
258 /* stop other processors. */
261 /* wait until other processors are stopped */
264 /* store status of other processors. */
269 * Reboot, halt and power_off routines for SMP.
272 void machine_restart_smp(char * __unused)
278 void machine_halt_smp(void)
281 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
282 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
283 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
287 void machine_power_off_smp(void)
290 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
291 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
292 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
297 * This is the main routine where commands issued by other
301 void do_ext_call_interrupt(__u16 code)
306 * handle bit signal external calls
308 * For the ec_schedule signal we have to do nothing. All the work
309 * is done automatically when we return from the interrupt.
311 bits = xchg(&S390_lowcore.ext_call_fast, 0);
313 if (test_bit(ec_call_function, &bits))
318 * Send an external call sigp to another cpu and return without waiting
319 * for its completion.
321 static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
324 * Set signaling bit in lowcore of target cpu and kick it
326 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
327 while(signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
332 * Send an external call sigp to every other cpu in the system and
333 * return without waiting for its completion.
335 static void smp_ext_bitcall_others(ec_bit_sig sig)
339 for_each_online_cpu(cpu) {
340 if (cpu == smp_processor_id())
343 * Set signaling bit in lowcore of target cpu and kick it
345 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
346 while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
353 * this function sends a 'purge tlb' signal to another CPU.
355 void smp_ptlb_callback(void *info)
360 void smp_ptlb_all(void)
362 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
364 EXPORT_SYMBOL(smp_ptlb_all);
365 #endif /* ! CONFIG_64BIT */
368 * this function sends a 'reschedule' IPI to another CPU.
369 * it goes straight through and wastes no time serializing
370 * anything. Worst case is that we lose a reschedule ...
372 void smp_send_reschedule(int cpu)
374 smp_ext_bitcall(cpu, ec_schedule);
378 * parameter area for the set/clear control bit callbacks
380 struct ec_creg_mask_parms {
381 unsigned long orvals[16];
382 unsigned long andvals[16];
386 * callback for setting/clearing control bits
388 void smp_ctl_bit_callback(void *info) {
389 struct ec_creg_mask_parms *pp = info;
390 unsigned long cregs[16];
393 __ctl_store(cregs, 0, 15);
394 for (i = 0; i <= 15; i++)
395 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
396 __ctl_load(cregs, 0, 15);
400 * Set a bit in a control register of all cpus
402 void smp_ctl_set_bit(int cr, int bit)
404 struct ec_creg_mask_parms parms;
406 memset(&parms.orvals, 0, sizeof(parms.orvals));
407 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
408 parms.orvals[cr] = 1 << bit;
409 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
413 * Clear a bit in a control register of all cpus
415 void smp_ctl_clear_bit(int cr, int bit)
417 struct ec_creg_mask_parms parms;
419 memset(&parms.orvals, 0, sizeof(parms.orvals));
420 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
421 parms.andvals[cr] = ~(1L << bit);
422 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
426 * Lets check how many CPUs we have.
430 __init smp_count_cpus(void)
432 unsigned int cpu, num_cpus;
436 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
439 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
440 current_thread_info()->cpu = 0;
442 for (cpu = 0; cpu <= 65535; cpu++) {
443 if ((__u16) cpu == boot_cpu_addr)
445 __cpu_logical_map[1] = (__u16) cpu;
446 if (signal_processor(1, sigp_sense) ==
447 sigp_not_operational)
452 printk("Detected %d CPU's\n",(int) num_cpus);
453 printk("Boot cpu address %2X\n", boot_cpu_addr);
459 * Activate a secondary processor.
461 extern void init_cpu_timer(void);
462 extern void init_cpu_vtimer(void);
464 int __devinit start_secondary(void *cpuvoid)
469 /* init per CPU timer */
471 #ifdef CONFIG_VIRT_TIMER
474 /* Enable pfault pseudo page faults on this cpu. */
477 /* Mark this cpu as online */
478 cpu_set(smp_processor_id(), cpu_online_map);
479 /* Switch on interrupts */
481 /* Print info about this processor */
482 print_cpu_info(&S390_lowcore.cpu_data);
483 /* cpu_idle will call schedule for us */
488 static void __init smp_create_idle(unsigned int cpu)
490 struct task_struct *p;
493 * don't care about the psw and regs settings since we'll never
494 * reschedule the forked task.
498 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
499 current_set[cpu] = p;
502 /* Reserving and releasing of CPUs */
504 static DEFINE_SPINLOCK(smp_reserve_lock);
505 static int smp_cpu_reserved[NR_CPUS];
508 smp_get_cpu(cpumask_t cpu_mask)
513 spin_lock_irqsave(&smp_reserve_lock, flags);
514 /* Try to find an already reserved cpu. */
515 for_each_cpu_mask(cpu, cpu_mask) {
516 if (smp_cpu_reserved[cpu] != 0) {
517 smp_cpu_reserved[cpu]++;
522 /* Reserve a new cpu from cpu_mask. */
523 for_each_cpu_mask(cpu, cpu_mask) {
524 if (cpu_online(cpu)) {
525 smp_cpu_reserved[cpu]++;
531 spin_unlock_irqrestore(&smp_reserve_lock, flags);
540 spin_lock_irqsave(&smp_reserve_lock, flags);
541 smp_cpu_reserved[cpu]--;
542 spin_unlock_irqrestore(&smp_reserve_lock, flags);
550 /* Check for stopped state */
551 if (signal_processor_ps(&status, 0, cpu, sigp_sense) == sigp_status_stored) {
558 /* Upping and downing of CPUs */
561 __cpu_up(unsigned int cpu)
563 struct task_struct *idle;
564 struct _lowcore *cpu_lowcore;
565 struct stack_frame *sf;
569 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
570 __cpu_logical_map[cpu] = (__u16) curr_cpu;
571 if (cpu_stopped(cpu))
575 if (!cpu_stopped(cpu))
578 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
579 cpu, sigp_set_prefix);
581 printk("sigp_set_prefix failed for cpu %d "
582 "with condition code %d\n",
583 (int) cpu, (int) ccode);
587 idle = current_set[cpu];
588 cpu_lowcore = lowcore_ptr[cpu];
589 cpu_lowcore->kernel_stack = (unsigned long)
590 task_stack_page(idle) + (THREAD_SIZE);
591 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
592 - sizeof(struct pt_regs)
593 - sizeof(struct stack_frame));
594 memset(sf, 0, sizeof(struct stack_frame));
595 sf->gprs[9] = (unsigned long) sf;
596 cpu_lowcore->save_area[15] = (unsigned long) sf;
597 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
600 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
601 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
602 cpu_lowcore->current_task = (unsigned long) idle;
603 cpu_lowcore->cpu_data.cpu_nr = cpu;
606 while (signal_processor(cpu,sigp_restart) == sigp_busy)
609 while (!cpu_online(cpu))
614 static unsigned int __initdata additional_cpus;
615 static unsigned int __initdata possible_cpus;
617 void __init smp_setup_cpu_possible_map(void)
619 unsigned int phy_cpus, pos_cpus, cpu;
621 phy_cpus = smp_count_cpus();
622 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
625 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
627 for (cpu = 0; cpu < pos_cpus; cpu++)
628 cpu_set(cpu, cpu_possible_map);
630 phy_cpus = min(phy_cpus, pos_cpus);
632 for (cpu = 0; cpu < phy_cpus; cpu++)
633 cpu_set(cpu, cpu_present_map);
636 #ifdef CONFIG_HOTPLUG_CPU
638 static int __init setup_additional_cpus(char *s)
640 additional_cpus = simple_strtoul(s, NULL, 0);
643 early_param("additional_cpus", setup_additional_cpus);
645 static int __init setup_possible_cpus(char *s)
647 possible_cpus = simple_strtoul(s, NULL, 0);
650 early_param("possible_cpus", setup_possible_cpus);
656 struct ec_creg_mask_parms cr_parms;
657 int cpu = smp_processor_id();
659 spin_lock_irqsave(&smp_reserve_lock, flags);
660 if (smp_cpu_reserved[cpu] != 0) {
661 spin_unlock_irqrestore(&smp_reserve_lock, flags);
664 cpu_clear(cpu, cpu_online_map);
666 /* Disable pfault pseudo page faults on this cpu. */
669 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
670 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
672 /* disable all external interrupts */
673 cr_parms.orvals[0] = 0;
674 cr_parms.andvals[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
675 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
676 /* disable all I/O interrupts */
677 cr_parms.orvals[6] = 0;
678 cr_parms.andvals[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
679 1<<27 | 1<<26 | 1<<25 | 1<<24);
680 /* disable most machine checks */
681 cr_parms.orvals[14] = 0;
682 cr_parms.andvals[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
684 smp_ctl_bit_callback(&cr_parms);
686 spin_unlock_irqrestore(&smp_reserve_lock, flags);
691 __cpu_die(unsigned int cpu)
693 /* Wait until target cpu is down */
694 while (!smp_cpu_not_running(cpu))
696 printk("Processor %d spun down\n", cpu);
703 signal_processor(smp_processor_id(), sigp_stop);
708 #endif /* CONFIG_HOTPLUG_CPU */
711 * Cycle through the processors and setup structures.
714 void __init smp_prepare_cpus(unsigned int max_cpus)
720 /* request the 0x1201 emergency signal external interrupt */
721 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
722 panic("Couldn't request external interrupt 0x1201");
723 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
725 * Initialize prefix pages and stacks for all possible cpus
727 print_cpu_info(&S390_lowcore.cpu_data);
729 for_each_possible_cpu(i) {
730 lowcore_ptr[i] = (struct _lowcore *)
731 __get_free_pages(GFP_KERNEL|GFP_DMA,
732 sizeof(void*) == 8 ? 1 : 0);
733 stack = __get_free_pages(GFP_KERNEL,ASYNC_ORDER);
734 if (lowcore_ptr[i] == NULL || stack == 0ULL)
735 panic("smp_boot_cpus failed to allocate memory\n");
737 *(lowcore_ptr[i]) = S390_lowcore;
738 lowcore_ptr[i]->async_stack = stack + (ASYNC_SIZE);
739 stack = __get_free_pages(GFP_KERNEL,0);
741 panic("smp_boot_cpus failed to allocate memory\n");
742 lowcore_ptr[i]->panic_stack = stack + (PAGE_SIZE);
744 if (MACHINE_HAS_IEEE) {
745 lowcore_ptr[i]->extended_save_area_addr =
746 (__u32) __get_free_pages(GFP_KERNEL,0);
747 if (lowcore_ptr[i]->extended_save_area_addr == 0)
748 panic("smp_boot_cpus failed to "
749 "allocate memory\n");
754 if (MACHINE_HAS_IEEE)
755 ctl_set_bit(14, 29); /* enable extended save area */
757 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
759 for_each_possible_cpu(cpu)
760 if (cpu != smp_processor_id())
761 smp_create_idle(cpu);
764 void __devinit smp_prepare_boot_cpu(void)
766 BUG_ON(smp_processor_id() != 0);
768 cpu_set(0, cpu_online_map);
769 S390_lowcore.percpu_offset = __per_cpu_offset[0];
770 current_set[0] = current;
773 void smp_cpus_done(unsigned int max_cpus)
775 cpu_present_map = cpu_possible_map;
779 * the frequency of the profiling timer can be changed
780 * by writing a multiplier value into /proc/profile.
782 * usually you want to run this on all CPUs ;)
784 int setup_profiling_timer(unsigned int multiplier)
789 static DEFINE_PER_CPU(struct cpu, cpu_devices);
791 static int __init topology_init(void)
796 for_each_possible_cpu(cpu) {
797 ret = register_cpu(&per_cpu(cpu_devices, cpu), cpu);
799 printk(KERN_WARNING "topology_init: register_cpu %d "
800 "failed (%d)\n", cpu, ret);
805 subsys_initcall(topology_init);
807 EXPORT_SYMBOL(cpu_online_map);
808 EXPORT_SYMBOL(cpu_possible_map);
809 EXPORT_SYMBOL(lowcore_ptr);
810 EXPORT_SYMBOL(smp_ctl_set_bit);
811 EXPORT_SYMBOL(smp_ctl_clear_bit);
812 EXPORT_SYMBOL(smp_call_function);
813 EXPORT_SYMBOL(smp_get_cpu);
814 EXPORT_SYMBOL(smp_put_cpu);