patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / sh / kernel / smp.c
1 /*
2  * arch/sh/kernel/smp.c
3  *
4  * SMP support for the SuperH processors.
5  *
6  * Copyright (C) 2002, 2003 Paul Mundt
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  */
13 #include <linux/config.h>
14 #include <linux/cache.h>
15 #include <linux/cpumask.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/spinlock.h>
20 #include <linux/threads.h>
21 #include <linux/module.h>
22 #include <linux/time.h>
23 #include <linux/timex.h>
24 #include <linux/sched.h>
25
26 #include <asm/atomic.h>
27 #include <asm/processor.h>
28 #include <asm/system.h>
29 #include <asm/hardirq.h>
30 #include <asm/mmu_context.h>
31 #include <asm/smp.h>
32
33 /*
34  * This was written with the Sega Saturn (SMP SH-2 7604) in mind,
35  * but is designed to be usable regardless if there's an MMU
36  * present or not.
37  */
38 int smp_threads_ready = 0;
39 struct sh_cpuinfo cpu_data[NR_CPUS];
40
41 extern int cpu_idle(void *unused);
42 extern void per_cpu_trap_init(void);
43
44 cpumask_t cpu_possible_map;
45 cpumask_t cpu_online_map;
46 unsigned long cache_decay_ticks = HZ / 100;
47 static atomic_t cpus_booted = ATOMIC_INIT(0);
48
49 /* These are defined by the board-specific code. */
50
51 /*
52  * Cause the function described by call_data to be executed on the passed
53  * cpu.  When the function has finished, increment the finished field of
54  * call_data.
55  */
56 void __smp_send_ipi(unsigned int cpu, unsigned int action);
57
58 /*
59  * Find the number of available processors
60  */
61 unsigned int __smp_probe_cpus(void);
62
63 /*
64  * Start a particular processor
65  */
66 void __smp_slave_init(unsigned int cpu);
67
68 /*
69  * Run specified function on a particular processor.
70  */
71 void __smp_call_function(unsigned int cpu);
72
73 static inline void __init smp_store_cpu_info(unsigned int cpu)
74 {
75         cpu_data[cpu].loops_per_jiffy = loops_per_jiffy;
76 }
77
78 void __init smp_prepare_cpus(unsigned int max_cpus)
79 {
80         unsigned int cpu = smp_processor_id();
81         int i;
82
83         atomic_set(&cpus_booted, 1);
84         smp_store_cpu_info(cpu);
85         
86         for (i = 0; i < __smp_probe_cpus(); i++)
87                 cpu_set(i, cpu_possible_map);
88 }
89
90 void __devinit smp_prepare_boot_cpu(void)
91 {
92         unsigned int cpu = smp_processor_id();
93
94         cpu_set(cpu, cpu_online_map);
95         cpu_set(cpu, cpu_possible_map);
96 }
97
98 int __cpu_up(unsigned int cpu)
99 {
100         struct task_struct *tsk;
101         struct pt_regs regs;
102
103         memset(&regs, 0, sizeof(struct pt_regs));
104         tsk = copy_process(CLONE_VM | CLONE_IDLETASK, 0, &regs, 0, 0, 0);
105
106         if (IS_ERR(tsk))
107                 panic("Failed forking idle task for cpu %d\n", cpu);
108         
109         wake_up_forked_process(tsk);
110
111         init_idle(tsk, cpu);
112         unhash_process(tsk);
113         
114         tsk->thread_info->cpu = cpu;
115
116         cpu_set(cpu, cpu_online_map);
117
118         return 0;
119 }
120
121 int start_secondary(void *unused)
122 {
123         unsigned int cpu = smp_processor_id();
124
125         atomic_inc(&init_mm.mm_count);
126         current->active_mm = &init_mm;
127
128         smp_store_cpu_info(cpu);
129
130         __smp_slave_init(cpu);
131         per_cpu_trap_init();
132         
133         atomic_inc(&cpus_booted);
134
135         return cpu_idle(0);
136 }
137
138 void __init smp_cpus_done(unsigned int max_cpus)
139 {
140         smp_threads_ready = 1;
141         smp_mb();
142 }
143
144 void smp_send_reschedule(int cpu)
145 {
146         __smp_send_ipi(cpu, SMP_MSG_RESCHEDULE);
147 }
148
149 static void stop_this_cpu(void *unused)
150 {
151         cpu_clear(smp_processor_id(), cpu_online_map);
152         local_irq_disable();
153
154         for (;;)
155                 cpu_relax();
156 }
157
158 void smp_send_stop(void)
159 {
160         smp_call_function(stop_this_cpu, 0, 1, 0);
161 }
162
163
164 struct smp_fn_call_struct smp_fn_call = {
165         .lock           = SPIN_LOCK_UNLOCKED,
166         .finished       = ATOMIC_INIT(0),
167 };
168
169 /*
170  * The caller of this wants the passed function to run on every cpu.  If wait
171  * is set, wait until all cpus have finished the function before returning.
172  * The lock is here to protect the call structure.
173  * You must not call this function with disabled interrupts or from a
174  * hardware interrupt handler or from a bottom half handler.
175  */
176 int smp_call_function(void (*func)(void *info), void *info, int retry, int wait)
177 {
178         unsigned int nr_cpus = atomic_read(&cpus_booted);
179         int i;
180
181         if (nr_cpus < 2)
182                 return 0;
183
184         /* Can deadlock when called with interrupts disabled */
185         WARN_ON(irqs_disabled());
186
187         spin_lock(&smp_fn_call.lock);
188
189         atomic_set(&smp_fn_call.finished, 0);
190         smp_fn_call.fn = func;
191         smp_fn_call.data = info;
192
193         for (i = 0; i < nr_cpus; i++)
194                 if (i != smp_processor_id())
195                         __smp_call_function(i);
196
197         if (wait)
198                 while (atomic_read(&smp_fn_call.finished) != (nr_cpus - 1));
199
200         spin_unlock(&smp_fn_call.lock);
201
202         return 0;
203 }
204
205 /* Not really SMP stuff ... */
206 int setup_profiling_timer(unsigned int multiplier)
207 {
208         return 0;
209 }
210