uml-patch-2.6.6-1
[linux-2.6.git] / arch / um / kernel / smp.c
1 /* 
2  * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/config.h"
7 #include "linux/percpu.h"
8 #include "asm/pgalloc.h"
9 #include "asm/tlb.h"
10
11 /* For some reason, mmu_gathers are referenced when CONFIG_SMP is off. */
12 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
13
14 #ifdef CONFIG_SMP
15
16 #include "linux/sched.h"
17 #include "linux/module.h"
18 #include "linux/threads.h"
19 #include "linux/interrupt.h"
20 #include "linux/err.h"
21 #include "asm/smp.h"
22 #include "asm/processor.h"
23 #include "asm/spinlock.h"
24 #include "asm/hardirq.h"
25 #include "user_util.h"
26 #include "kern_util.h"
27 #include "kern.h"
28 #include "irq_user.h"
29 #include "os.h"
30
31 /* CPU online map, set by smp_boot_cpus */
32 unsigned long cpu_online_map = CPU_MASK_NONE;
33
34 EXPORT_SYMBOL(cpu_online_map);
35
36 /* Per CPU bogomips and other parameters
37  * The only piece used here is the ipi pipe, which is set before SMP is
38  * started and never changed.
39  */
40 struct cpuinfo_um cpu_data[NR_CPUS];
41
42 spinlock_t um_bh_lock = SPIN_LOCK_UNLOCKED;
43
44 atomic_t global_bh_count;
45
46 /* Not used by UML */
47 unsigned char global_irq_holder = NO_PROC_ID;
48 unsigned volatile long global_irq_lock;
49
50 /* Set when the idlers are all forked */
51 int smp_threads_ready = 0;
52
53 /* A statistic, can be a little off */
54 int num_reschedules_sent = 0;
55
56 /* Small, random number, never changed */
57 unsigned long cache_decay_ticks = 5;
58
59 /* Not changed after boot */
60 struct task_struct *idle_threads[NR_CPUS];
61
62 void smp_send_reschedule(int cpu)
63 {
64         os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
65         num_reschedules_sent++;
66 }
67
68 static void show(char * str)
69 {
70         int cpu = smp_processor_id();
71
72         printk(KERN_INFO "\n%s, CPU %d:\n", str, cpu);
73 }
74         
75 #define MAXCOUNT 100000000
76
77 static inline void wait_on_bh(void)
78 {
79         int count = MAXCOUNT;
80         do {
81                 if (!--count) {
82                         show("wait_on_bh");
83                         count = ~0;
84                 }
85                 /* nothing .. wait for the other bh's to go away */
86         } while (atomic_read(&global_bh_count) != 0);
87 }
88
89 /*
90  * This is called when we want to synchronize with
91  * bottom half handlers. We need to wait until
92  * no other CPU is executing any bottom half handler.
93  *
94  * Don't wait if we're already running in an interrupt
95  * context or are inside a bh handler. 
96  */
97 void synchronize_bh(void)
98 {
99         if (atomic_read(&global_bh_count) && !in_interrupt())
100                 wait_on_bh();
101 }
102
103 void smp_send_stop(void)
104 {
105         int i;
106
107         printk(KERN_INFO "Stopping all CPUs...");
108         for(i = 0; i < num_online_cpus(); i++){
109                 if(i == current_thread->cpu)
110                         continue;
111                 os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
112         }
113         printk("done\n");
114 }
115
116 static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
117 static cpumask_t cpu_callin_map = CPU_MASK_NONE;
118
119 static int idle_proc(void *cpup)
120 {
121         int cpu = (int) cpup, err;
122
123         err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
124         if(err < 0)
125                 panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);
126
127         activate_ipi(cpu_data[cpu].ipi_pipe[0], 
128                      current->thread.mode.tt.extern_pid);
129  
130         wmb();
131         if (cpu_test_and_set(cpu, cpu_callin_map)) {
132                 printk("huh, CPU#%d already present??\n", cpu);
133                 BUG();
134         }
135
136         while (!cpu_isset(cpu, smp_commenced_mask))
137                 cpu_relax();
138
139         cpu_set(cpu, cpu_online_map);
140         default_idle();
141         return(0);
142 }
143
144 static struct task_struct *idle_thread(int cpu)
145 {
146         struct task_struct *new_task;
147         unsigned char c;
148
149         current->thread.request.u.thread.proc = idle_proc;
150         current->thread.request.u.thread.arg = (void *) cpu;
151         new_task = copy_process(CLONE_VM | CLONE_IDLETASK, 0, NULL, 0, NULL, 
152                                 NULL);
153         if(IS_ERR(new_task)) 
154                 panic("copy_process failed in idle_thread, error = %ld",
155                       PTR_ERR(new_task));
156
157         cpu_tasks[cpu] = ((struct cpu_task) 
158                           { .pid =      new_task->thread.mode.tt.extern_pid,
159                             .task =     new_task } );
160         idle_threads[cpu] = new_task;
161         CHOOSE_MODE(os_write_file(new_task->thread.mode.tt.switch_pipe[1], &c, 
162                           sizeof(c)),
163                     ({ panic("skas mode doesn't support SMP"); }));
164         wake_up_forked_process(new_task);
165         return(new_task);
166 }
167
168 void smp_prepare_cpus(unsigned int maxcpus)
169 {
170         struct task_struct *idle;
171         unsigned long waittime;
172         int err, cpu, me = smp_processor_id();
173
174         cpu_clear(me, cpu_online_map);
175         cpu_set(me, cpu_online_map);
176         cpu_set(me, cpu_callin_map);
177
178         err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
179         if(err < 0)
180                 panic("CPU#0 failed to create IPI pipe, errno = %d", -err);
181
182         activate_ipi(cpu_data[me].ipi_pipe[0], 
183                      current->thread.mode.tt.extern_pid);
184
185         for(cpu = 1; cpu < ncpus; cpu++){
186                 printk("Booting processor %d...\n", cpu);
187                 
188                 idle = idle_thread(cpu);
189
190                 init_idle(idle, cpu);
191                 unhash_process(idle);
192
193                 waittime = 200000000;
194                 while (waittime-- && !cpu_isset(cpu, cpu_callin_map))
195                         cpu_relax();
196
197                 if (cpu_isset(cpu, cpu_callin_map))
198                         printk("done\n");
199                 else printk("failed\n");
200         }
201 }
202
203 void smp_prepare_boot_cpu(void)
204 {
205         cpu_set(smp_processor_id(), cpu_online_map);
206 }
207
208 int __cpu_up(unsigned int cpu)
209 {
210         cpu_set(cpu, smp_commenced_mask);
211         while (!cpu_isset(cpu, cpu_online_map))
212                 mb();
213         return(0);
214 }
215
216 int setup_profiling_timer(unsigned int multiplier)
217 {
218         printk(KERN_INFO "setup_profiling_timer\n");
219         return(0);
220 }
221
222 void smp_call_function_slave(int cpu);
223
224 void IPI_handler(int cpu)
225 {
226         unsigned char c;
227         int fd;
228
229         fd = cpu_data[cpu].ipi_pipe[0];
230         while (os_read_file(fd, &c, 1) == 1) {
231                 switch (c) {
232                 case 'C':
233                         smp_call_function_slave(cpu);
234                         break;
235
236                 case 'R':
237                         set_tsk_need_resched(current);
238                         break;
239
240                 case 'S':
241                         printk("CPU#%d stopping\n", cpu);
242                         while(1)
243                                 pause();
244                         break;
245
246                 default:
247                         printk("CPU#%d received unknown IPI [%c]!\n", cpu, c);
248                         break;
249                 }
250         }
251 }
252
253 int hard_smp_processor_id(void)
254 {
255         return(pid_to_processor_id(os_getpid()));
256 }
257
258 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
259 static atomic_t scf_started;
260 static atomic_t scf_finished;
261 static void (*func)(void *info);
262 static void *info;
263
264 void smp_call_function_slave(int cpu)
265 {
266         atomic_inc(&scf_started);
267         (*func)(info);
268         atomic_inc(&scf_finished);
269 }
270
271 int smp_call_function(void (*_func)(void *info), void *_info, int nonatomic, 
272                       int wait)
273 {
274         int cpus = num_online_cpus() - 1;
275         int i;
276
277         if (!cpus)
278                 return 0;
279
280         spin_lock_bh(&call_lock);
281         atomic_set(&scf_started, 0);
282         atomic_set(&scf_finished, 0);
283         func = _func;
284         info = _info;
285
286         for (i=0;i<NR_CPUS;i++)
287                 if((i != current_thread->cpu) && 
288                    cpu_isset(i, cpu_online_map))
289                         os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
290
291         while (atomic_read(&scf_started) != cpus)
292                 barrier();
293
294         if (wait)
295                 while (atomic_read(&scf_finished) != cpus)
296                         barrier();
297
298         spin_unlock_bh(&call_lock);
299         return 0;
300 }
301
302 #endif
303
304 /*
305  * Overrides for Emacs so that we follow Linus's tabbing style.
306  * Emacs will notice this stuff at the end of the file and automatically
307  * adjust the settings for this buffer only.  This must remain at the end
308  * of the file.
309  * ---------------------------------------------------------------------------
310  * Local variables:
311  * c-file-style: "linux"
312  * End:
313  */