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"
21 #include "linux/hardirq.h"
23 #include "asm/processor.h"
24 #include "asm/spinlock.h"
25 #include "user_util.h"
26 #include "kern_util.h"
31 /* CPU online map, set by smp_boot_cpus */
32 cpumask_t cpu_online_map = CPU_MASK_NONE;
33 cpumask_t cpu_possible_map = CPU_MASK_NONE;
35 EXPORT_SYMBOL(cpu_online_map);
36 EXPORT_SYMBOL(cpu_possible_map);
38 /* Per CPU bogomips and other parameters
39 * The only piece used here is the ipi pipe, which is set before SMP is
40 * started and never changed.
42 struct cpuinfo_um cpu_data[NR_CPUS];
44 /* A statistic, can be a little off */
45 int num_reschedules_sent = 0;
47 /* Not changed after boot */
48 struct task_struct *idle_threads[NR_CPUS];
50 void smp_send_reschedule(int cpu)
52 os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
53 num_reschedules_sent++;
56 void smp_send_stop(void)
60 printk(KERN_INFO "Stopping all CPUs...");
61 for(i = 0; i < num_online_cpus(); i++){
62 if(i == current_thread->cpu)
64 os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
69 static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
70 static cpumask_t cpu_callin_map = CPU_MASK_NONE;
72 static int idle_proc(void *cpup)
74 int cpu = (int) cpup, err;
76 err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
78 panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);
80 activate_ipi(cpu_data[cpu].ipi_pipe[0],
81 current->thread.mode.tt.extern_pid);
84 if (cpu_test_and_set(cpu, cpu_callin_map)) {
85 printk("huh, CPU#%d already present??\n", cpu);
89 while (!cpu_isset(cpu, smp_commenced_mask))
92 cpu_set(cpu, cpu_online_map);
97 static struct task_struct *idle_thread(int cpu)
99 struct task_struct *new_task;
102 current->thread.request.u.thread.proc = idle_proc;
103 current->thread.request.u.thread.arg = (void *) cpu;
104 new_task = fork_idle(cpu);
106 panic("copy_process failed in idle_thread, error = %ld",
109 cpu_tasks[cpu] = ((struct cpu_task)
110 { .pid = new_task->thread.mode.tt.extern_pid,
111 .task = new_task } );
112 idle_threads[cpu] = new_task;
113 CHOOSE_MODE(os_write_file(new_task->thread.mode.tt.switch_pipe[1], &c,
115 ({ panic("skas mode doesn't support SMP"); }));
119 void smp_prepare_cpus(unsigned int maxcpus)
121 struct task_struct *idle;
122 unsigned long waittime;
123 int err, cpu, me = smp_processor_id();
126 for (i = 0; i < ncpus; ++i)
127 cpu_set(i, cpu_possible_map);
129 cpu_clear(me, cpu_online_map);
130 cpu_set(me, cpu_online_map);
131 cpu_set(me, cpu_callin_map);
133 err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
135 panic("CPU#0 failed to create IPI pipe, errno = %d", -err);
137 activate_ipi(cpu_data[me].ipi_pipe[0],
138 current->thread.mode.tt.extern_pid);
140 for(cpu = 1; cpu < ncpus; cpu++){
141 printk("Booting processor %d...\n", cpu);
143 idle = idle_thread(cpu);
145 init_idle(idle, cpu);
146 unhash_process(idle);
148 waittime = 200000000;
149 while (waittime-- && !cpu_isset(cpu, cpu_callin_map))
152 if (cpu_isset(cpu, cpu_callin_map))
154 else printk("failed\n");
158 void smp_prepare_boot_cpu(void)
160 cpu_set(smp_processor_id(), cpu_online_map);
163 int __cpu_up(unsigned int cpu)
165 cpu_set(cpu, smp_commenced_mask);
166 while (!cpu_isset(cpu, cpu_online_map))
171 int setup_profiling_timer(unsigned int multiplier)
173 printk(KERN_INFO "setup_profiling_timer\n");
177 void smp_call_function_slave(int cpu);
179 void IPI_handler(int cpu)
184 fd = cpu_data[cpu].ipi_pipe[0];
185 while (os_read_file(fd, &c, 1) == 1) {
188 smp_call_function_slave(cpu);
192 set_tsk_need_resched(current);
196 printk("CPU#%d stopping\n", cpu);
202 printk("CPU#%d received unknown IPI [%c]!\n", cpu, c);
208 int hard_smp_processor_id(void)
210 return(pid_to_processor_id(os_getpid()));
213 static DEFINE_SPINLOCK(call_lock);
214 static atomic_t scf_started;
215 static atomic_t scf_finished;
216 static void (*func)(void *info);
219 void smp_call_function_slave(int cpu)
221 atomic_inc(&scf_started);
223 atomic_inc(&scf_finished);
226 int smp_call_function(void (*_func)(void *info), void *_info, int nonatomic,
229 int cpus = num_online_cpus() - 1;
235 /* Can deadlock when called with interrupts disabled */
236 WARN_ON(irqs_disabled());
238 spin_lock_bh(&call_lock);
239 atomic_set(&scf_started, 0);
240 atomic_set(&scf_finished, 0);
244 for_each_online_cpu(i)
245 os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
247 while (atomic_read(&scf_started) != cpus)
251 while (atomic_read(&scf_finished) != cpus)
254 spin_unlock_bh(&call_lock);
261 * Overrides for Emacs so that we follow Linus's tabbing style.
262 * Emacs will notice this stuff at the end of the file and automatically
263 * adjust the settings for this buffer only. This must remain at the end
265 * ---------------------------------------------------------------------------
267 * c-file-style: "linux"