2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include <linux/percpu.h>
7 #include <asm/pgalloc.h>
12 #include <linux/sched.h>
13 #include <linux/module.h>
14 #include <linux/threads.h>
15 #include <linux/interrupt.h>
16 #include <linux/err.h>
17 #include <linux/hardirq.h>
19 #include <asm/processor.h>
20 #include <asm/spinlock.h>
25 /* Per CPU bogomips and other parameters
26 * The only piece used here is the ipi pipe, which is set before SMP is
27 * started and never changed.
29 struct cpuinfo_um cpu_data
[NR_CPUS
];
31 /* A statistic, can be a little off */
32 int num_reschedules_sent
= 0;
34 /* Not changed after boot */
35 struct task_struct
*idle_threads
[NR_CPUS
];
37 void smp_send_reschedule(int cpu
)
39 os_write_file(cpu_data
[cpu
].ipi_pipe
[1], "R", 1);
40 num_reschedules_sent
++;
43 void smp_send_stop(void)
47 printk(KERN_INFO
"Stopping all CPUs...");
48 for (i
= 0; i
< num_online_cpus(); i
++) {
49 if (i
== current_thread
->cpu
)
51 os_write_file(cpu_data
[i
].ipi_pipe
[1], "S", 1);
53 printk(KERN_CONT
"done\n");
56 static cpumask_t smp_commenced_mask
= CPU_MASK_NONE
;
57 static cpumask_t cpu_callin_map
= CPU_MASK_NONE
;
59 static int idle_proc(void *cpup
)
61 int cpu
= (int) cpup
, err
;
63 err
= os_pipe(cpu_data
[cpu
].ipi_pipe
, 1, 1);
65 panic("CPU#%d failed to create IPI pipe, err = %d", cpu
, -err
);
67 os_set_fd_async(cpu_data
[cpu
].ipi_pipe
[0]);
70 if (cpu_test_and_set(cpu
, cpu_callin_map
)) {
71 printk(KERN_ERR
"huh, CPU#%d already present??\n", cpu
);
75 while (!cpu_isset(cpu
, smp_commenced_mask
))
78 notify_cpu_starting(cpu
);
79 set_cpu_online(cpu
, true);
84 static struct task_struct
*idle_thread(int cpu
)
86 struct task_struct
*new_task
;
88 current
->thread
.request
.u
.thread
.proc
= idle_proc
;
89 current
->thread
.request
.u
.thread
.arg
= (void *) cpu
;
90 new_task
= fork_idle(cpu
);
92 panic("copy_process failed in idle_thread, error = %ld",
95 cpu_tasks
[cpu
] = ((struct cpu_task
)
96 { .pid
= new_task
->thread
.mode
.tt
.extern_pid
,
98 idle_threads
[cpu
] = new_task
;
99 panic("skas mode doesn't support SMP");
103 void smp_prepare_cpus(unsigned int maxcpus
)
105 struct task_struct
*idle
;
106 unsigned long waittime
;
107 int err
, cpu
, me
= smp_processor_id();
110 for (i
= 0; i
< ncpus
; ++i
)
111 set_cpu_possible(i
, true);
113 set_cpu_online(me
, true);
114 cpu_set(me
, cpu_callin_map
);
116 err
= os_pipe(cpu_data
[me
].ipi_pipe
, 1, 1);
118 panic("CPU#0 failed to create IPI pipe, errno = %d", -err
);
120 os_set_fd_async(cpu_data
[me
].ipi_pipe
[0]);
122 for (cpu
= 1; cpu
< ncpus
; cpu
++) {
123 printk(KERN_INFO
"Booting processor %d...\n", cpu
);
125 idle
= idle_thread(cpu
);
127 init_idle(idle
, cpu
);
129 waittime
= 200000000;
130 while (waittime
-- && !cpu_isset(cpu
, cpu_callin_map
))
133 printk(KERN_INFO
"%s\n",
134 cpu_isset(cpu
, cpu_calling_map
) ? "done" : "failed");
138 void smp_prepare_boot_cpu(void)
140 set_cpu_online(smp_processor_id(), true);
143 int __cpu_up(unsigned int cpu
, struct task_struct
*tidle
)
145 cpu_set(cpu
, smp_commenced_mask
);
146 while (!cpu_online(cpu
))
151 int setup_profiling_timer(unsigned int multiplier
)
153 printk(KERN_INFO
"setup_profiling_timer\n");
157 void smp_call_function_slave(int cpu
);
159 void IPI_handler(int cpu
)
164 fd
= cpu_data
[cpu
].ipi_pipe
[0];
165 while (os_read_file(fd
, &c
, 1) == 1) {
168 smp_call_function_slave(cpu
);
176 printk(KERN_INFO
"CPU#%d stopping\n", cpu
);
182 printk(KERN_ERR
"CPU#%d received unknown IPI [%c]!\n",
189 int hard_smp_processor_id(void)
191 return pid_to_processor_id(os_getpid());
194 static DEFINE_SPINLOCK(call_lock
);
195 static atomic_t scf_started
;
196 static atomic_t scf_finished
;
197 static void (*func
)(void *info
);
200 void smp_call_function_slave(int cpu
)
202 atomic_inc(&scf_started
);
204 atomic_inc(&scf_finished
);
207 int smp_call_function(void (*_func
)(void *info
), void *_info
, int wait
)
209 int cpus
= num_online_cpus() - 1;
215 /* Can deadlock when called with interrupts disabled */
216 WARN_ON(irqs_disabled());
218 spin_lock_bh(&call_lock
);
219 atomic_set(&scf_started
, 0);
220 atomic_set(&scf_finished
, 0);
224 for_each_online_cpu(i
)
225 os_write_file(cpu_data
[i
].ipi_pipe
[1], "C", 1);
227 while (atomic_read(&scf_started
) != cpus
)
231 while (atomic_read(&scf_finished
) != cpus
)
234 spin_unlock_bh(&call_lock
);