2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
6 #include "linux/percpu.h"
7 #include "asm/pgalloc.h"
10 /* For some reason, mmu_gathers are referenced when CONFIG_SMP is off. */
11 DEFINE_PER_CPU(struct mmu_gather
, mmu_gathers
);
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"
22 #include "asm/processor.h"
23 #include "asm/spinlock.h"
24 #include "user_util.h"
25 #include "kern_util.h"
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
;
34 EXPORT_SYMBOL(cpu_online_map
);
35 EXPORT_SYMBOL(cpu_possible_map
);
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.
41 struct cpuinfo_um cpu_data
[NR_CPUS
];
43 /* A statistic, can be a little off */
44 int num_reschedules_sent
= 0;
46 /* Not changed after boot */
47 struct task_struct
*idle_threads
[NR_CPUS
];
49 void smp_send_reschedule(int cpu
)
51 os_write_file(cpu_data
[cpu
].ipi_pipe
[1], "R", 1);
52 num_reschedules_sent
++;
55 void smp_send_stop(void)
59 printk(KERN_INFO
"Stopping all CPUs...");
60 for(i
= 0; i
< num_online_cpus(); i
++){
61 if(i
== current_thread
->cpu
)
63 os_write_file(cpu_data
[i
].ipi_pipe
[1], "S", 1);
68 static cpumask_t smp_commenced_mask
= CPU_MASK_NONE
;
69 static cpumask_t cpu_callin_map
= CPU_MASK_NONE
;
71 static int idle_proc(void *cpup
)
73 int cpu
= (int) cpup
, err
;
75 err
= os_pipe(cpu_data
[cpu
].ipi_pipe
, 1, 1);
77 panic("CPU#%d failed to create IPI pipe, err = %d", cpu
, -err
);
79 os_set_fd_async(cpu_data
[cpu
].ipi_pipe
[0],
80 current
->thread
.mode
.tt
.extern_pid
);
83 if (cpu_test_and_set(cpu
, cpu_callin_map
)) {
84 printk("huh, CPU#%d already present??\n", cpu
);
88 while (!cpu_isset(cpu
, smp_commenced_mask
))
91 cpu_set(cpu
, cpu_online_map
);
96 static struct task_struct
*idle_thread(int cpu
)
98 struct task_struct
*new_task
;
101 current
->thread
.request
.u
.thread
.proc
= idle_proc
;
102 current
->thread
.request
.u
.thread
.arg
= (void *) cpu
;
103 new_task
= fork_idle(cpu
);
105 panic("copy_process failed in idle_thread, error = %ld",
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
,
114 ({ panic("skas mode doesn't support SMP"); }));
118 void smp_prepare_cpus(unsigned int maxcpus
)
120 struct task_struct
*idle
;
121 unsigned long waittime
;
122 int err
, cpu
, me
= smp_processor_id();
125 for (i
= 0; i
< ncpus
; ++i
)
126 cpu_set(i
, cpu_possible_map
);
128 cpu_clear(me
, cpu_online_map
);
129 cpu_set(me
, cpu_online_map
);
130 cpu_set(me
, cpu_callin_map
);
132 err
= os_pipe(cpu_data
[me
].ipi_pipe
, 1, 1);
134 panic("CPU#0 failed to create IPI pipe, errno = %d", -err
);
136 os_set_fd_async(cpu_data
[me
].ipi_pipe
[0],
137 current
->thread
.mode
.tt
.extern_pid
);
139 for(cpu
= 1; cpu
< ncpus
; cpu
++){
140 printk("Booting processor %d...\n", cpu
);
142 idle
= idle_thread(cpu
);
144 init_idle(idle
, cpu
);
146 waittime
= 200000000;
147 while (waittime
-- && !cpu_isset(cpu
, cpu_callin_map
))
150 if (cpu_isset(cpu
, cpu_callin_map
))
152 else printk("failed\n");
156 void smp_prepare_boot_cpu(void)
158 cpu_set(smp_processor_id(), cpu_online_map
);
161 int __cpu_up(unsigned int cpu
)
163 cpu_set(cpu
, smp_commenced_mask
);
164 while (!cpu_isset(cpu
, cpu_online_map
))
169 int setup_profiling_timer(unsigned int multiplier
)
171 printk(KERN_INFO
"setup_profiling_timer\n");
175 void smp_call_function_slave(int cpu
);
177 void IPI_handler(int cpu
)
182 fd
= cpu_data
[cpu
].ipi_pipe
[0];
183 while (os_read_file(fd
, &c
, 1) == 1) {
186 smp_call_function_slave(cpu
);
190 set_tsk_need_resched(current
);
194 printk("CPU#%d stopping\n", cpu
);
200 printk("CPU#%d received unknown IPI [%c]!\n", cpu
, c
);
206 int hard_smp_processor_id(void)
208 return(pid_to_processor_id(os_getpid()));
211 static DEFINE_SPINLOCK(call_lock
);
212 static atomic_t scf_started
;
213 static atomic_t scf_finished
;
214 static void (*func
)(void *info
);
217 void smp_call_function_slave(int cpu
)
219 atomic_inc(&scf_started
);
221 atomic_inc(&scf_finished
);
224 int smp_call_function(void (*_func
)(void *info
), void *_info
, int nonatomic
,
227 int cpus
= num_online_cpus() - 1;
233 /* Can deadlock when called with interrupts disabled */
234 WARN_ON(irqs_disabled());
236 spin_lock_bh(&call_lock
);
237 atomic_set(&scf_started
, 0);
238 atomic_set(&scf_finished
, 0);
242 for_each_online_cpu(i
)
243 os_write_file(cpu_data
[i
].ipi_pipe
[1], "C", 1);
245 while (atomic_read(&scf_started
) != cpus
)
249 while (atomic_read(&scf_finished
) != cpus
)
252 spin_unlock_bh(&call_lock
);
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
263 * ---------------------------------------------------------------------------
265 * c-file-style: "linux"