Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[linux-2.6] / arch / um / kernel / smp.c
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.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 "kern.h"
25 #include "irq_user.h"
26 #include "os.h"
27
28 /* CPU online map, set by smp_boot_cpus */
29 cpumask_t cpu_online_map = CPU_MASK_NONE;
30 cpumask_t cpu_possible_map = CPU_MASK_NONE;
31
32 EXPORT_SYMBOL(cpu_online_map);
33 EXPORT_SYMBOL(cpu_possible_map);
34
35 /* Per CPU bogomips and other parameters
36  * The only piece used here is the ipi pipe, which is set before SMP is
37  * started and never changed.
38  */
39 struct cpuinfo_um cpu_data[NR_CPUS];
40
41 /* A statistic, can be a little off */
42 int num_reschedules_sent = 0;
43
44 /* Not changed after boot */
45 struct task_struct *idle_threads[NR_CPUS];
46
47 void smp_send_reschedule(int cpu)
48 {
49         os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
50         num_reschedules_sent++;
51 }
52
53 void smp_send_stop(void)
54 {
55         int i;
56
57         printk(KERN_INFO "Stopping all CPUs...");
58         for (i = 0; i < num_online_cpus(); i++) {
59                 if (i == current_thread->cpu)
60                         continue;
61                 os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
62         }
63         printk(KERN_CONT "done\n");
64 }
65
66 static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
67 static cpumask_t cpu_callin_map = CPU_MASK_NONE;
68
69 static int idle_proc(void *cpup)
70 {
71         int cpu = (int) cpup, err;
72
73         err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
74         if (err < 0)
75                 panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);
76
77         os_set_fd_async(cpu_data[cpu].ipi_pipe[0]);
78
79         wmb();
80         if (cpu_test_and_set(cpu, cpu_callin_map)) {
81                 printk(KERN_ERR "huh, CPU#%d already present??\n", cpu);
82                 BUG();
83         }
84
85         while (!cpu_isset(cpu, smp_commenced_mask))
86                 cpu_relax();
87
88         cpu_set(cpu, cpu_online_map);
89         default_idle();
90         return 0;
91 }
92
93 static struct task_struct *idle_thread(int cpu)
94 {
95         struct task_struct *new_task;
96
97         current->thread.request.u.thread.proc = idle_proc;
98         current->thread.request.u.thread.arg = (void *) cpu;
99         new_task = fork_idle(cpu);
100         if (IS_ERR(new_task))
101                 panic("copy_process failed in idle_thread, error = %ld",
102                       PTR_ERR(new_task));
103
104         cpu_tasks[cpu] = ((struct cpu_task)
105                           { .pid =      new_task->thread.mode.tt.extern_pid,
106                             .task =     new_task } );
107         idle_threads[cpu] = new_task;
108         panic("skas mode doesn't support SMP");
109         return new_task;
110 }
111
112 void smp_prepare_cpus(unsigned int maxcpus)
113 {
114         struct task_struct *idle;
115         unsigned long waittime;
116         int err, cpu, me = smp_processor_id();
117         int i;
118
119         for (i = 0; i < ncpus; ++i)
120                 cpu_set(i, cpu_possible_map);
121
122         cpu_clear(me, cpu_online_map);
123         cpu_set(me, cpu_online_map);
124         cpu_set(me, cpu_callin_map);
125
126         err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
127         if (err < 0)
128                 panic("CPU#0 failed to create IPI pipe, errno = %d", -err);
129
130         os_set_fd_async(cpu_data[me].ipi_pipe[0]);
131
132         for (cpu = 1; cpu < ncpus; cpu++) {
133                 printk(KERN_INFO "Booting processor %d...\n", cpu);
134
135                 idle = idle_thread(cpu);
136
137                 init_idle(idle, cpu);
138
139                 waittime = 200000000;
140                 while (waittime-- && !cpu_isset(cpu, cpu_callin_map))
141                         cpu_relax();
142
143                 printk(KERN_INFO "%s\n",
144                        cpu_isset(cpu, cpu_calling_map) ? "done" : "failed");
145         }
146 }
147
148 void smp_prepare_boot_cpu(void)
149 {
150         cpu_set(smp_processor_id(), cpu_online_map);
151 }
152
153 int __cpu_up(unsigned int cpu)
154 {
155         cpu_set(cpu, smp_commenced_mask);
156         while (!cpu_isset(cpu, cpu_online_map))
157                 mb();
158         return 0;
159 }
160
161 int setup_profiling_timer(unsigned int multiplier)
162 {
163         printk(KERN_INFO "setup_profiling_timer\n");
164         return 0;
165 }
166
167 void smp_call_function_slave(int cpu);
168
169 void IPI_handler(int cpu)
170 {
171         unsigned char c;
172         int fd;
173
174         fd = cpu_data[cpu].ipi_pipe[0];
175         while (os_read_file(fd, &c, 1) == 1) {
176                 switch (c) {
177                 case 'C':
178                         smp_call_function_slave(cpu);
179                         break;
180
181                 case 'R':
182                         set_tsk_need_resched(current);
183                         break;
184
185                 case 'S':
186                         printk(KERN_INFO "CPU#%d stopping\n", cpu);
187                         while (1)
188                                 pause();
189                         break;
190
191                 default:
192                         printk(KERN_ERR "CPU#%d received unknown IPI [%c]!\n",
193                                cpu, c);
194                         break;
195                 }
196         }
197 }
198
199 int hard_smp_processor_id(void)
200 {
201         return pid_to_processor_id(os_getpid());
202 }
203
204 static DEFINE_SPINLOCK(call_lock);
205 static atomic_t scf_started;
206 static atomic_t scf_finished;
207 static void (*func)(void *info);
208 static void *info;
209
210 void smp_call_function_slave(int cpu)
211 {
212         atomic_inc(&scf_started);
213         (*func)(info);
214         atomic_inc(&scf_finished);
215 }
216
217 int smp_call_function(void (*_func)(void *info), void *_info, int wait)
218 {
219         int cpus = num_online_cpus() - 1;
220         int i;
221
222         if (!cpus)
223                 return 0;
224
225         /* Can deadlock when called with interrupts disabled */
226         WARN_ON(irqs_disabled());
227
228         spin_lock_bh(&call_lock);
229         atomic_set(&scf_started, 0);
230         atomic_set(&scf_finished, 0);
231         func = _func;
232         info = _info;
233
234         for_each_online_cpu(i)
235                 os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
236
237         while (atomic_read(&scf_started) != cpus)
238                 barrier();
239
240         if (wait)
241                 while (atomic_read(&scf_finished) != cpus)
242                         barrier();
243
244         spin_unlock_bh(&call_lock);
245         return 0;
246 }
247
248 #endif