ARM: OMAP: add SoSSI clock (call propagate_rate for childrens)
[linux-2.6] / 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/percpu.h"
7 #include "asm/pgalloc.h"
8 #include "asm/tlb.h"
9
10 /* For some reason, mmu_gathers are referenced when CONFIG_SMP is off. */
11 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
12
13 #ifdef CONFIG_SMP
14
15 #include "linux/sched.h"
16 #include "linux/module.h"
17 #include "linux/threads.h"
18 #include "linux/interrupt.h"
19 #include "linux/err.h"
20 #include "linux/hardirq.h"
21 #include "asm/smp.h"
22 #include "asm/processor.h"
23 #include "asm/spinlock.h"
24 #include "user_util.h"
25 #include "kern_util.h"
26 #include "kern.h"
27 #include "irq_user.h"
28 #include "os.h"
29
30 /* CPU online map, set by smp_boot_cpus */
31 cpumask_t cpu_online_map = CPU_MASK_NONE;
32 cpumask_t cpu_possible_map = CPU_MASK_NONE;
33
34 EXPORT_SYMBOL(cpu_online_map);
35 EXPORT_SYMBOL(cpu_possible_map);
36
37 /* Per CPU bogomips and other parameters
38  * The only piece used here is the ipi pipe, which is set before SMP is
39  * started and never changed.
40  */
41 struct cpuinfo_um cpu_data[NR_CPUS];
42
43 /* A statistic, can be a little off */
44 int num_reschedules_sent = 0;
45
46 /* Not changed after boot */
47 struct task_struct *idle_threads[NR_CPUS];
48
49 void smp_send_reschedule(int cpu)
50 {
51         os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
52         num_reschedules_sent++;
53 }
54
55 void smp_send_stop(void)
56 {
57         int i;
58
59         printk(KERN_INFO "Stopping all CPUs...");
60         for(i = 0; i < num_online_cpus(); i++){
61                 if(i == current_thread->cpu)
62                         continue;
63                 os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
64         }
65         printk("done\n");
66 }
67
68 static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
69 static cpumask_t cpu_callin_map = CPU_MASK_NONE;
70
71 static int idle_proc(void *cpup)
72 {
73         int cpu = (int) cpup, err;
74
75         err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
76         if(err < 0)
77                 panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);
78
79         os_set_fd_async(cpu_data[cpu].ipi_pipe[0],
80                      current->thread.mode.tt.extern_pid);
81
82         wmb();
83         if (cpu_test_and_set(cpu, cpu_callin_map)) {
84                 printk("huh, CPU#%d already present??\n", cpu);
85                 BUG();
86         }
87
88         while (!cpu_isset(cpu, smp_commenced_mask))
89                 cpu_relax();
90
91         cpu_set(cpu, cpu_online_map);
92         default_idle();
93         return(0);
94 }
95
96 static struct task_struct *idle_thread(int cpu)
97 {
98         struct task_struct *new_task;
99         unsigned char c;
100
101         current->thread.request.u.thread.proc = idle_proc;
102         current->thread.request.u.thread.arg = (void *) cpu;
103         new_task = fork_idle(cpu);
104         if(IS_ERR(new_task))
105                 panic("copy_process failed in idle_thread, error = %ld",
106                       PTR_ERR(new_task));
107
108         cpu_tasks[cpu] = ((struct cpu_task)
109                           { .pid =      new_task->thread.mode.tt.extern_pid,
110                             .task =     new_task } );
111         idle_threads[cpu] = new_task;
112         CHOOSE_MODE(os_write_file(new_task->thread.mode.tt.switch_pipe[1], &c,
113                           sizeof(c)),
114                     ({ panic("skas mode doesn't support SMP"); }));
115         return(new_task);
116 }
117
118 void smp_prepare_cpus(unsigned int maxcpus)
119 {
120         struct task_struct *idle;
121         unsigned long waittime;
122         int err, cpu, me = smp_processor_id();
123         int i;
124
125         for (i = 0; i < ncpus; ++i)
126                 cpu_set(i, cpu_possible_map);
127
128         cpu_clear(me, cpu_online_map);
129         cpu_set(me, cpu_online_map);
130         cpu_set(me, cpu_callin_map);
131
132         err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
133         if(err < 0)
134                 panic("CPU#0 failed to create IPI pipe, errno = %d", -err);
135
136         os_set_fd_async(cpu_data[me].ipi_pipe[0],
137                      current->thread.mode.tt.extern_pid);
138
139         for(cpu = 1; cpu < ncpus; cpu++){
140                 printk("Booting processor %d...\n", cpu);
141
142                 idle = idle_thread(cpu);
143
144                 init_idle(idle, cpu);
145
146                 waittime = 200000000;
147                 while (waittime-- && !cpu_isset(cpu, cpu_callin_map))
148                         cpu_relax();
149
150                 if (cpu_isset(cpu, cpu_callin_map))
151                         printk("done\n");
152                 else printk("failed\n");
153         }
154 }
155
156 void smp_prepare_boot_cpu(void)
157 {
158         cpu_set(smp_processor_id(), cpu_online_map);
159 }
160
161 int __cpu_up(unsigned int cpu)
162 {
163         cpu_set(cpu, smp_commenced_mask);
164         while (!cpu_isset(cpu, cpu_online_map))
165                 mb();
166         return(0);
167 }
168
169 int setup_profiling_timer(unsigned int multiplier)
170 {
171         printk(KERN_INFO "setup_profiling_timer\n");
172         return(0);
173 }
174
175 void smp_call_function_slave(int cpu);
176
177 void IPI_handler(int cpu)
178 {
179         unsigned char c;
180         int fd;
181
182         fd = cpu_data[cpu].ipi_pipe[0];
183         while (os_read_file(fd, &c, 1) == 1) {
184                 switch (c) {
185                 case 'C':
186                         smp_call_function_slave(cpu);
187                         break;
188
189                 case 'R':
190                         set_tsk_need_resched(current);
191                         break;
192
193                 case 'S':
194                         printk("CPU#%d stopping\n", cpu);
195                         while(1)
196                                 pause();
197                         break;
198
199                 default:
200                         printk("CPU#%d received unknown IPI [%c]!\n", cpu, c);
201                         break;
202                 }
203         }
204 }
205
206 int hard_smp_processor_id(void)
207 {
208         return(pid_to_processor_id(os_getpid()));
209 }
210
211 static DEFINE_SPINLOCK(call_lock);
212 static atomic_t scf_started;
213 static atomic_t scf_finished;
214 static void (*func)(void *info);
215 static void *info;
216
217 void smp_call_function_slave(int cpu)
218 {
219         atomic_inc(&scf_started);
220         (*func)(info);
221         atomic_inc(&scf_finished);
222 }
223
224 int smp_call_function(void (*_func)(void *info), void *_info, int nonatomic,
225                       int wait)
226 {
227         int cpus = num_online_cpus() - 1;
228         int i;
229
230         if (!cpus)
231                 return 0;
232
233         /* Can deadlock when called with interrupts disabled */
234         WARN_ON(irqs_disabled());
235
236         spin_lock_bh(&call_lock);
237         atomic_set(&scf_started, 0);
238         atomic_set(&scf_finished, 0);
239         func = _func;
240         info = _info;
241
242         for_each_online_cpu(i)
243                 os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
244
245         while (atomic_read(&scf_started) != cpus)
246                 barrier();
247
248         if (wait)
249                 while (atomic_read(&scf_finished) != cpus)
250                         barrier();
251
252         spin_unlock_bh(&call_lock);
253         return 0;
254 }
255
256 #endif
257
258 /*
259  * Overrides for Emacs so that we follow Linus's tabbing style.
260  * Emacs will notice this stuff at the end of the file and automatically
261  * adjust the settings for this buffer only.  This must remain at the end
262  * of the file.
263  * ---------------------------------------------------------------------------
264  * Local variables:
265  * c-file-style: "linux"
266  * End:
267  */