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