vserver 1.9.5.x5
[linux-2.6.git] / arch / sh / kernel / smp.c
1 /*
2  * arch/sh/kernel/smp.c
3  *
4  * SMP support for the SuperH processors.
5  *
6  * Copyright (C) 2002, 2003 Paul Mundt
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  */
13 #include <linux/config.h>
14 #include <linux/cache.h>
15 #include <linux/cpumask.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/spinlock.h>
20 #include <linux/threads.h>
21 #include <linux/module.h>
22 #include <linux/time.h>
23 #include <linux/timex.h>
24 #include <linux/sched.h>
25
26 #include <asm/atomic.h>
27 #include <asm/processor.h>
28 #include <asm/system.h>
29 #include <asm/mmu_context.h>
30 #include <asm/smp.h>
31
32 /*
33  * This was written with the Sega Saturn (SMP SH-2 7604) in mind,
34  * but is designed to be usable regardless if there's an MMU
35  * present or not.
36  */
37 int smp_threads_ready = 0;
38 struct sh_cpuinfo cpu_data[NR_CPUS];
39
40 extern void per_cpu_trap_init(void);
41
42 cpumask_t cpu_possible_map;
43 cpumask_t cpu_online_map;
44 unsigned long cache_decay_ticks = HZ / 100;
45 static atomic_t cpus_booted = ATOMIC_INIT(0);
46
47 /* These are defined by the board-specific code. */
48
49 /*
50  * Cause the function described by call_data to be executed on the passed
51  * cpu.  When the function has finished, increment the finished field of
52  * call_data.
53  */
54 void __smp_send_ipi(unsigned int cpu, unsigned int action);
55
56 /*
57  * Find the number of available processors
58  */
59 unsigned int __smp_probe_cpus(void);
60
61 /*
62  * Start a particular processor
63  */
64 void __smp_slave_init(unsigned int cpu);
65
66 /*
67  * Run specified function on a particular processor.
68  */
69 void __smp_call_function(unsigned int cpu);
70
71 static inline void __init smp_store_cpu_info(unsigned int cpu)
72 {
73         cpu_data[cpu].loops_per_jiffy = loops_per_jiffy;
74 }
75
76 void __init smp_prepare_cpus(unsigned int max_cpus)
77 {
78         unsigned int cpu = smp_processor_id();
79         int i;
80
81         atomic_set(&cpus_booted, 1);
82         smp_store_cpu_info(cpu);
83         
84         for (i = 0; i < __smp_probe_cpus(); i++)
85                 cpu_set(i, cpu_possible_map);
86 }
87
88 void __devinit smp_prepare_boot_cpu(void)
89 {
90         unsigned int cpu = smp_processor_id();
91
92         cpu_set(cpu, cpu_online_map);
93         cpu_set(cpu, cpu_possible_map);
94 }
95
96 int __cpu_up(unsigned int cpu)
97 {
98         struct task_struct *tsk;
99
100         tsk = fork_idle(cpu);
101
102         if (IS_ERR(tsk))
103                 panic("Failed forking idle task for cpu %d\n", cpu);
104         
105         tsk->thread_info->cpu = cpu;
106
107         cpu_set(cpu, cpu_online_map);
108
109         return 0;
110 }
111
112 int start_secondary(void *unused)
113 {
114         unsigned int cpu = smp_processor_id();
115
116         atomic_inc(&init_mm.mm_count);
117         current->active_mm = &init_mm;
118
119         smp_store_cpu_info(cpu);
120
121         __smp_slave_init(cpu);
122         per_cpu_trap_init();
123         
124         atomic_inc(&cpus_booted);
125
126         cpu_idle();
127         return 0;
128 }
129
130 void __init smp_cpus_done(unsigned int max_cpus)
131 {
132         smp_threads_ready = 1;
133         smp_mb();
134 }
135
136 void smp_send_reschedule(int cpu)
137 {
138         __smp_send_ipi(cpu, SMP_MSG_RESCHEDULE);
139 }
140
141 static void stop_this_cpu(void *unused)
142 {
143         cpu_clear(smp_processor_id(), cpu_online_map);
144         local_irq_disable();
145
146         for (;;)
147                 cpu_relax();
148 }
149
150 void smp_send_stop(void)
151 {
152         smp_call_function(stop_this_cpu, 0, 1, 0);
153 }
154
155
156 struct smp_fn_call_struct smp_fn_call = {
157         .lock           = SPIN_LOCK_UNLOCKED,
158         .finished       = ATOMIC_INIT(0),
159 };
160
161 /*
162  * The caller of this wants the passed function to run on every cpu.  If wait
163  * is set, wait until all cpus have finished the function before returning.
164  * The lock is here to protect the call structure.
165  * You must not call this function with disabled interrupts or from a
166  * hardware interrupt handler or from a bottom half handler.
167  */
168 int smp_call_function(void (*func)(void *info), void *info, int retry, int wait)
169 {
170         unsigned int nr_cpus = atomic_read(&cpus_booted);
171         int i;
172
173         if (nr_cpus < 2)
174                 return 0;
175
176         /* Can deadlock when called with interrupts disabled */
177         WARN_ON(irqs_disabled());
178
179         spin_lock(&smp_fn_call.lock);
180
181         atomic_set(&smp_fn_call.finished, 0);
182         smp_fn_call.fn = func;
183         smp_fn_call.data = info;
184
185         for (i = 0; i < nr_cpus; i++)
186                 if (i != smp_processor_id())
187                         __smp_call_function(i);
188
189         if (wait)
190                 while (atomic_read(&smp_fn_call.finished) != (nr_cpus - 1));
191
192         spin_unlock(&smp_fn_call.lock);
193
194         return 0;
195 }
196
197 /* Not really SMP stuff ... */
198 int setup_profiling_timer(unsigned int multiplier)
199 {
200         return 0;
201 }
202