[S390] smp_call_function/smp_call_function_on locking.
[linux-2.6/openmoko-kernel.git] / arch / s390 / kernel / smp.c
blob83a4ea6e3d605fb6637a845924887583a1a08910
1 /*
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>
25 #include <linux/mm.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>
35 #include <asm/sigp.h>
36 #include <asm/pgalloc.h>
37 #include <asm/irq.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 * 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);
67 void *info;
68 atomic_t started;
69 atomic_t finished;
70 int wait;
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);
85 (*func)(info);
86 if (wait)
87 atomic_inc(&call_data->finished);
91 * this function sends a 'generic call function' IPI to all other CPUs
92 * in the system.
95 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
96 int wait)
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.
110 struct call_data_struct data;
111 int cpus = num_online_cpus()-1;
113 if (cpus <= 0)
114 return 0;
116 /* Can deadlock when interrupts are disabled or if in wrong context */
117 WARN_ON(irqs_disabled() || in_irq());
119 data.func = func;
120 data.info = info;
121 atomic_set(&data.started, 0);
122 data.wait = wait;
123 if (wait)
124 atomic_set(&data.finished, 0);
126 spin_lock_bh(&call_lock);
127 call_data = &data;
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)
133 cpu_relax();
135 if (wait)
136 while (atomic_read(&data.finished) != cpus)
137 cpu_relax();
138 spin_unlock_bh(&call_lock);
140 return 0;
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;
157 int curr_cpu;
159 if (!cpu_online(cpu))
160 return -EINVAL;
162 /* Can deadlock when interrupts are disabled or if in wrong context */
163 WARN_ON(irqs_disabled() || in_irq());
165 /* disable preemption for local function call */
166 curr_cpu = get_cpu();
168 if (curr_cpu == cpu) {
169 /* direct call to function */
170 func(info);
171 put_cpu();
172 return 0;
175 data.func = func;
176 data.info = info;
177 atomic_set(&data.started, 0);
178 data.wait = wait;
179 if (wait)
180 atomic_set(&data.finished, 0);
182 spin_lock_bh(&call_lock);
183 call_data = &data;
184 smp_ext_bitcall(cpu, ec_call_function);
186 /* Wait for response */
187 while (atomic_read(&data.started) != 1)
188 cpu_relax();
190 if (wait)
191 while (atomic_read(&data.finished) != 1)
192 cpu_relax();
194 spin_unlock_bh(&call_lock);
195 put_cpu();
196 return 0;
198 EXPORT_SYMBOL(smp_call_function_on);
200 static void do_send_stop(void)
202 int cpu, rc;
204 /* stop all processors */
205 for_each_online_cpu(cpu) {
206 if (cpu == smp_processor_id())
207 continue;
208 do {
209 rc = signal_processor(cpu, sigp_stop);
210 } while (rc == sigp_busy);
214 static void do_store_status(void)
216 int cpu, rc;
218 /* store status of all processors in their lowcores (real 0) */
219 for_each_online_cpu(cpu) {
220 if (cpu == smp_processor_id())
221 continue;
222 do {
223 rc = signal_processor_p(
224 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
225 sigp_store_status_at_address);
226 } while(rc == sigp_busy);
230 static void do_wait_for_stop(void)
232 int cpu;
234 /* Wait for all other cpus to enter stopped state */
235 for_each_online_cpu(cpu) {
236 if (cpu == smp_processor_id())
237 continue;
238 while(!smp_cpu_not_running(cpu))
239 cpu_relax();
244 * this function sends a 'stop' sigp to all other CPUs in the system.
245 * it goes straight through.
247 void smp_send_stop(void)
249 /* Disable all interrupts/machine checks */
250 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
252 /* write magic number to zero page (absolute 0) */
253 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
255 /* stop other processors. */
256 do_send_stop();
258 /* wait until other processors are stopped */
259 do_wait_for_stop();
261 /* store status of other processors. */
262 do_store_status();
266 * Reboot, halt and power_off routines for SMP.
269 void machine_restart_smp(char * __unused)
271 smp_send_stop();
272 do_reipl();
275 void machine_halt_smp(void)
277 smp_send_stop();
278 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
279 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
280 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
281 for (;;);
284 void machine_power_off_smp(void)
286 smp_send_stop();
287 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
288 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
289 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
290 for (;;);
294 * This is the main routine where commands issued by other
295 * cpus are handled.
298 static void do_ext_call_interrupt(__u16 code)
300 unsigned long bits;
303 * handle bit signal external calls
305 * For the ec_schedule signal we have to do nothing. All the work
306 * is done automatically when we return from the interrupt.
308 bits = xchg(&S390_lowcore.ext_call_fast, 0);
310 if (test_bit(ec_call_function, &bits))
311 do_call_function();
315 * Send an external call sigp to another cpu and return without waiting
316 * for its completion.
318 static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
321 * Set signaling bit in lowcore of target cpu and kick it
323 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
324 while(signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
325 udelay(10);
329 * Send an external call sigp to every other cpu in the system and
330 * return without waiting for its completion.
332 static void smp_ext_bitcall_others(ec_bit_sig sig)
334 int cpu;
336 for_each_online_cpu(cpu) {
337 if (cpu == smp_processor_id())
338 continue;
340 * Set signaling bit in lowcore of target cpu and kick it
342 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
343 while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
344 udelay(10);
348 #ifndef CONFIG_64BIT
350 * this function sends a 'purge tlb' signal to another CPU.
352 void smp_ptlb_callback(void *info)
354 local_flush_tlb();
357 void smp_ptlb_all(void)
359 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
361 EXPORT_SYMBOL(smp_ptlb_all);
362 #endif /* ! CONFIG_64BIT */
365 * this function sends a 'reschedule' IPI to another CPU.
366 * it goes straight through and wastes no time serializing
367 * anything. Worst case is that we lose a reschedule ...
369 void smp_send_reschedule(int cpu)
371 smp_ext_bitcall(cpu, ec_schedule);
375 * parameter area for the set/clear control bit callbacks
377 struct ec_creg_mask_parms {
378 unsigned long orvals[16];
379 unsigned long andvals[16];
383 * callback for setting/clearing control bits
385 static void smp_ctl_bit_callback(void *info) {
386 struct ec_creg_mask_parms *pp = info;
387 unsigned long cregs[16];
388 int i;
390 __ctl_store(cregs, 0, 15);
391 for (i = 0; i <= 15; i++)
392 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
393 __ctl_load(cregs, 0, 15);
397 * Set a bit in a control register of all cpus
399 void smp_ctl_set_bit(int cr, int bit)
401 struct ec_creg_mask_parms parms;
403 memset(&parms.orvals, 0, sizeof(parms.orvals));
404 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
405 parms.orvals[cr] = 1 << bit;
406 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
410 * Clear a bit in a control register of all cpus
412 void smp_ctl_clear_bit(int cr, int bit)
414 struct ec_creg_mask_parms parms;
416 memset(&parms.orvals, 0, sizeof(parms.orvals));
417 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
418 parms.andvals[cr] = ~(1L << bit);
419 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
423 * Lets check how many CPUs we have.
426 static unsigned int
427 __init smp_count_cpus(void)
429 unsigned int cpu, num_cpus;
430 __u16 boot_cpu_addr;
433 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
436 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
437 current_thread_info()->cpu = 0;
438 num_cpus = 1;
439 for (cpu = 0; cpu <= 65535; cpu++) {
440 if ((__u16) cpu == boot_cpu_addr)
441 continue;
442 __cpu_logical_map[1] = (__u16) cpu;
443 if (signal_processor(1, sigp_sense) ==
444 sigp_not_operational)
445 continue;
446 num_cpus++;
449 printk("Detected %d CPU's\n",(int) num_cpus);
450 printk("Boot cpu address %2X\n", boot_cpu_addr);
452 return num_cpus;
456 * Activate a secondary processor.
458 int __devinit start_secondary(void *cpuvoid)
460 /* Setup the cpu */
461 cpu_init();
462 preempt_disable();
463 /* Enable TOD clock interrupts on the secondary cpu. */
464 init_cpu_timer();
465 #ifdef CONFIG_VIRT_TIMER
466 /* Enable cpu timer interrupts on the secondary cpu. */
467 init_cpu_vtimer();
468 #endif
469 /* Enable pfault pseudo page faults on this cpu. */
470 pfault_init();
472 /* Mark this cpu as online */
473 cpu_set(smp_processor_id(), cpu_online_map);
474 /* Switch on interrupts */
475 local_irq_enable();
476 /* Print info about this processor */
477 print_cpu_info(&S390_lowcore.cpu_data);
478 /* cpu_idle will call schedule for us */
479 cpu_idle();
480 return 0;
483 static void __init smp_create_idle(unsigned int cpu)
485 struct task_struct *p;
488 * don't care about the psw and regs settings since we'll never
489 * reschedule the forked task.
491 p = fork_idle(cpu);
492 if (IS_ERR(p))
493 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
494 current_set[cpu] = p;
497 /* Reserving and releasing of CPUs */
499 static DEFINE_SPINLOCK(smp_reserve_lock);
500 static int smp_cpu_reserved[NR_CPUS];
503 smp_get_cpu(cpumask_t cpu_mask)
505 unsigned long flags;
506 int cpu;
508 spin_lock_irqsave(&smp_reserve_lock, flags);
509 /* Try to find an already reserved cpu. */
510 for_each_cpu_mask(cpu, cpu_mask) {
511 if (smp_cpu_reserved[cpu] != 0) {
512 smp_cpu_reserved[cpu]++;
513 /* Found one. */
514 goto out;
517 /* Reserve a new cpu from cpu_mask. */
518 for_each_cpu_mask(cpu, cpu_mask) {
519 if (cpu_online(cpu)) {
520 smp_cpu_reserved[cpu]++;
521 goto out;
524 cpu = -ENODEV;
525 out:
526 spin_unlock_irqrestore(&smp_reserve_lock, flags);
527 return cpu;
530 void
531 smp_put_cpu(int cpu)
533 unsigned long flags;
535 spin_lock_irqsave(&smp_reserve_lock, flags);
536 smp_cpu_reserved[cpu]--;
537 spin_unlock_irqrestore(&smp_reserve_lock, flags);
540 static int
541 cpu_stopped(int cpu)
543 __u32 status;
545 /* Check for stopped state */
546 if (signal_processor_ps(&status, 0, cpu, sigp_sense) == sigp_status_stored) {
547 if (status & 0x40)
548 return 1;
550 return 0;
553 /* Upping and downing of CPUs */
556 __cpu_up(unsigned int cpu)
558 struct task_struct *idle;
559 struct _lowcore *cpu_lowcore;
560 struct stack_frame *sf;
561 sigp_ccode ccode;
562 int curr_cpu;
564 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
565 __cpu_logical_map[cpu] = (__u16) curr_cpu;
566 if (cpu_stopped(cpu))
567 break;
570 if (!cpu_stopped(cpu))
571 return -ENODEV;
573 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
574 cpu, sigp_set_prefix);
575 if (ccode){
576 printk("sigp_set_prefix failed for cpu %d "
577 "with condition code %d\n",
578 (int) cpu, (int) ccode);
579 return -EIO;
582 idle = current_set[cpu];
583 cpu_lowcore = lowcore_ptr[cpu];
584 cpu_lowcore->kernel_stack = (unsigned long)
585 task_stack_page(idle) + (THREAD_SIZE);
586 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
587 - sizeof(struct pt_regs)
588 - sizeof(struct stack_frame));
589 memset(sf, 0, sizeof(struct stack_frame));
590 sf->gprs[9] = (unsigned long) sf;
591 cpu_lowcore->save_area[15] = (unsigned long) sf;
592 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
593 asm volatile(
594 " stam 0,15,0(%0)"
595 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
596 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
597 cpu_lowcore->current_task = (unsigned long) idle;
598 cpu_lowcore->cpu_data.cpu_nr = cpu;
599 eieio();
601 while (signal_processor(cpu,sigp_restart) == sigp_busy)
602 udelay(10);
604 while (!cpu_online(cpu))
605 cpu_relax();
606 return 0;
609 static unsigned int __initdata additional_cpus;
610 static unsigned int __initdata possible_cpus;
612 void __init smp_setup_cpu_possible_map(void)
614 unsigned int phy_cpus, pos_cpus, cpu;
616 phy_cpus = smp_count_cpus();
617 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
619 if (possible_cpus)
620 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
622 for (cpu = 0; cpu < pos_cpus; cpu++)
623 cpu_set(cpu, cpu_possible_map);
625 phy_cpus = min(phy_cpus, pos_cpus);
627 for (cpu = 0; cpu < phy_cpus; cpu++)
628 cpu_set(cpu, cpu_present_map);
631 #ifdef CONFIG_HOTPLUG_CPU
633 static int __init setup_additional_cpus(char *s)
635 additional_cpus = simple_strtoul(s, NULL, 0);
636 return 0;
638 early_param("additional_cpus", setup_additional_cpus);
640 static int __init setup_possible_cpus(char *s)
642 possible_cpus = simple_strtoul(s, NULL, 0);
643 return 0;
645 early_param("possible_cpus", setup_possible_cpus);
648 __cpu_disable(void)
650 unsigned long flags;
651 struct ec_creg_mask_parms cr_parms;
652 int cpu = smp_processor_id();
654 spin_lock_irqsave(&smp_reserve_lock, flags);
655 if (smp_cpu_reserved[cpu] != 0) {
656 spin_unlock_irqrestore(&smp_reserve_lock, flags);
657 return -EBUSY;
659 cpu_clear(cpu, cpu_online_map);
661 /* Disable pfault pseudo page faults on this cpu. */
662 pfault_fini();
664 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
665 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
667 /* disable all external interrupts */
668 cr_parms.orvals[0] = 0;
669 cr_parms.andvals[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
670 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
671 /* disable all I/O interrupts */
672 cr_parms.orvals[6] = 0;
673 cr_parms.andvals[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
674 1<<27 | 1<<26 | 1<<25 | 1<<24);
675 /* disable most machine checks */
676 cr_parms.orvals[14] = 0;
677 cr_parms.andvals[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
679 smp_ctl_bit_callback(&cr_parms);
681 spin_unlock_irqrestore(&smp_reserve_lock, flags);
682 return 0;
685 void
686 __cpu_die(unsigned int cpu)
688 /* Wait until target cpu is down */
689 while (!smp_cpu_not_running(cpu))
690 cpu_relax();
691 printk("Processor %d spun down\n", cpu);
694 void
695 cpu_die(void)
697 idle_task_exit();
698 signal_processor(smp_processor_id(), sigp_stop);
699 BUG();
700 for(;;);
703 #endif /* CONFIG_HOTPLUG_CPU */
706 * Cycle through the processors and setup structures.
709 void __init smp_prepare_cpus(unsigned int max_cpus)
711 unsigned long stack;
712 unsigned int cpu;
713 int i;
715 /* request the 0x1201 emergency signal external interrupt */
716 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
717 panic("Couldn't request external interrupt 0x1201");
718 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
720 * Initialize prefix pages and stacks for all possible cpus
722 print_cpu_info(&S390_lowcore.cpu_data);
724 for_each_possible_cpu(i) {
725 lowcore_ptr[i] = (struct _lowcore *)
726 __get_free_pages(GFP_KERNEL|GFP_DMA,
727 sizeof(void*) == 8 ? 1 : 0);
728 stack = __get_free_pages(GFP_KERNEL,ASYNC_ORDER);
729 if (lowcore_ptr[i] == NULL || stack == 0ULL)
730 panic("smp_boot_cpus failed to allocate memory\n");
732 *(lowcore_ptr[i]) = S390_lowcore;
733 lowcore_ptr[i]->async_stack = stack + (ASYNC_SIZE);
734 stack = __get_free_pages(GFP_KERNEL,0);
735 if (stack == 0ULL)
736 panic("smp_boot_cpus failed to allocate memory\n");
737 lowcore_ptr[i]->panic_stack = stack + (PAGE_SIZE);
738 #ifndef CONFIG_64BIT
739 if (MACHINE_HAS_IEEE) {
740 lowcore_ptr[i]->extended_save_area_addr =
741 (__u32) __get_free_pages(GFP_KERNEL,0);
742 if (lowcore_ptr[i]->extended_save_area_addr == 0)
743 panic("smp_boot_cpus failed to "
744 "allocate memory\n");
746 #endif
748 #ifndef CONFIG_64BIT
749 if (MACHINE_HAS_IEEE)
750 ctl_set_bit(14, 29); /* enable extended save area */
751 #endif
752 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
754 for_each_possible_cpu(cpu)
755 if (cpu != smp_processor_id())
756 smp_create_idle(cpu);
759 void __devinit smp_prepare_boot_cpu(void)
761 BUG_ON(smp_processor_id() != 0);
763 cpu_set(0, cpu_online_map);
764 S390_lowcore.percpu_offset = __per_cpu_offset[0];
765 current_set[0] = current;
768 void smp_cpus_done(unsigned int max_cpus)
770 cpu_present_map = cpu_possible_map;
774 * the frequency of the profiling timer can be changed
775 * by writing a multiplier value into /proc/profile.
777 * usually you want to run this on all CPUs ;)
779 int setup_profiling_timer(unsigned int multiplier)
781 return 0;
784 static DEFINE_PER_CPU(struct cpu, cpu_devices);
786 static int __init topology_init(void)
788 int cpu;
789 int ret;
791 for_each_possible_cpu(cpu) {
792 struct cpu *c = &per_cpu(cpu_devices, cpu);
794 c->hotpluggable = 1;
795 ret = register_cpu(c, cpu);
796 if (ret)
797 printk(KERN_WARNING "topology_init: register_cpu %d "
798 "failed (%d)\n", cpu, ret);
800 return 0;
803 subsys_initcall(topology_init);
805 EXPORT_SYMBOL(cpu_online_map);
806 EXPORT_SYMBOL(cpu_possible_map);
807 EXPORT_SYMBOL(lowcore_ptr);
808 EXPORT_SYMBOL(smp_ctl_set_bit);
809 EXPORT_SYMBOL(smp_ctl_clear_bit);
810 EXPORT_SYMBOL(smp_call_function);
811 EXPORT_SYMBOL(smp_get_cpu);
812 EXPORT_SYMBOL(smp_put_cpu);