2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
6 #include "linux/config.h"
7 #include "linux/percpu.h"
8 #include "asm/pgalloc.h"
11 /* For some reason, mmu_gathers are referenced when CONFIG_SMP is off. */
12 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
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"
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"
31 /* CPU online map, set by smp_boot_cpus */
32 unsigned long cpu_online_map = CPU_MASK_NONE;
34 EXPORT_SYMBOL(cpu_online_map);
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.
40 struct cpuinfo_um cpu_data[NR_CPUS];
42 spinlock_t um_bh_lock = SPIN_LOCK_UNLOCKED;
44 atomic_t global_bh_count;
47 unsigned char global_irq_holder = NO_PROC_ID;
48 unsigned volatile long global_irq_lock;
50 /* Set when the idlers are all forked */
51 int smp_threads_ready = 0;
53 /* A statistic, can be a little off */
54 int num_reschedules_sent = 0;
56 /* Small, random number, never changed */
57 unsigned long cache_decay_ticks = 5;
59 /* Not changed after boot */
60 struct task_struct *idle_threads[NR_CPUS];
62 void smp_send_reschedule(int cpu)
64 os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
65 num_reschedules_sent++;
68 static void show(char * str)
70 int cpu = smp_processor_id();
72 printk(KERN_INFO "\n%s, CPU %d:\n", str, cpu);
75 #define MAXCOUNT 100000000
77 static inline void wait_on_bh(void)
85 /* nothing .. wait for the other bh's to go away */
86 } while (atomic_read(&global_bh_count) != 0);
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.
94 * Don't wait if we're already running in an interrupt
95 * context or are inside a bh handler.
97 void synchronize_bh(void)
99 if (atomic_read(&global_bh_count) && !in_interrupt())
103 void smp_send_stop(void)
107 printk(KERN_INFO "Stopping all CPUs...");
108 for(i = 0; i < num_online_cpus(); i++){
109 if(i == current_thread->cpu)
111 os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
116 static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
117 static cpumask_t cpu_callin_map = CPU_MASK_NONE;
119 static int idle_proc(void *cpup)
121 int cpu = (int) cpup, err;
123 err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
125 panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);
127 activate_ipi(cpu_data[cpu].ipi_pipe[0],
128 current->thread.mode.tt.extern_pid);
131 if (cpu_test_and_set(cpu, cpu_callin_map)) {
132 printk("huh, CPU#%d already present??\n", cpu);
136 while (!cpu_isset(cpu, smp_commenced_mask))
139 cpu_set(cpu, cpu_online_map);
144 static struct task_struct *idle_thread(int cpu)
146 struct task_struct *new_task;
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,
154 panic("copy_process failed in idle_thread, error = %ld",
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,
163 ({ panic("skas mode doesn't support SMP"); }));
164 wake_up_forked_process(new_task);
168 void smp_prepare_cpus(unsigned int maxcpus)
170 struct task_struct *idle;
171 unsigned long waittime;
172 int err, cpu, me = smp_processor_id();
174 cpu_clear(me, cpu_online_map);
175 cpu_set(me, cpu_online_map);
176 cpu_set(me, cpu_callin_map);
178 err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
180 panic("CPU#0 failed to create IPI pipe, errno = %d", -err);
182 activate_ipi(cpu_data[me].ipi_pipe[0],
183 current->thread.mode.tt.extern_pid);
185 for(cpu = 1; cpu < ncpus; cpu++){
186 printk("Booting processor %d...\n", cpu);
188 idle = idle_thread(cpu);
190 init_idle(idle, cpu);
191 unhash_process(idle);
193 waittime = 200000000;
194 while (waittime-- && !cpu_isset(cpu, cpu_callin_map))
197 if (cpu_isset(cpu, cpu_callin_map))
199 else printk("failed\n");
203 void smp_prepare_boot_cpu(void)
205 cpu_set(smp_processor_id(), cpu_online_map);
208 int __cpu_up(unsigned int cpu)
210 cpu_set(cpu, smp_commenced_mask);
211 while (!cpu_isset(cpu, cpu_online_map))
216 int setup_profiling_timer(unsigned int multiplier)
218 printk(KERN_INFO "setup_profiling_timer\n");
222 void smp_call_function_slave(int cpu);
224 void IPI_handler(int cpu)
229 fd = cpu_data[cpu].ipi_pipe[0];
230 while (os_read_file(fd, &c, 1) == 1) {
233 smp_call_function_slave(cpu);
237 set_tsk_need_resched(current);
241 printk("CPU#%d stopping\n", cpu);
247 printk("CPU#%d received unknown IPI [%c]!\n", cpu, c);
253 int hard_smp_processor_id(void)
255 return(pid_to_processor_id(os_getpid()));
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);
264 void smp_call_function_slave(int cpu)
266 atomic_inc(&scf_started);
268 atomic_inc(&scf_finished);
271 int smp_call_function(void (*_func)(void *info), void *_info, int nonatomic,
274 int cpus = num_online_cpus() - 1;
280 /* Can deadlock when called with interrupts disabled */
281 WARN_ON(irqs_disabled());
283 spin_lock_bh(&call_lock);
284 atomic_set(&scf_started, 0);
285 atomic_set(&scf_finished, 0);
289 for (i=0;i<NR_CPUS;i++)
290 if((i != current_thread->cpu) &&
291 cpu_isset(i, cpu_online_map))
292 os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
294 while (atomic_read(&scf_started) != cpus)
298 while (atomic_read(&scf_finished) != cpus)
301 spin_unlock_bh(&call_lock);
308 * Overrides for Emacs so that we follow Linus's tabbing style.
309 * Emacs will notice this stuff at the end of the file and automatically
310 * adjust the settings for this buffer only. This must remain at the end
312 * ---------------------------------------------------------------------------
314 * c-file-style: "linux"