c55d314166c79bd8802464d1023fdd931e6967cf
[cascardo/linux.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 - 2008 Paul Mundt
7  * Copyright (C) 2006 - 2007 Akio Idehara
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file "COPYING" in the main directory of this archive
11  * for more details.
12  */
13 #include <linux/err.h>
14 #include <linux/cache.h>
15 #include <linux/cpumask.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <asm/atomic.h>
23 #include <asm/processor.h>
24 #include <asm/system.h>
25 #include <asm/mmu_context.h>
26 #include <asm/smp.h>
27 #include <asm/cacheflush.h>
28 #include <asm/sections.h>
29
30 int __cpu_number_map[NR_CPUS];          /* Map physical to logical */
31 int __cpu_logical_map[NR_CPUS];         /* Map logical to physical */
32
33 cpumask_t cpu_possible_map;
34 EXPORT_SYMBOL(cpu_possible_map);
35
36 cpumask_t cpu_online_map;
37 EXPORT_SYMBOL(cpu_online_map);
38
39 static inline void __init smp_store_cpu_info(unsigned int cpu)
40 {
41         struct sh_cpuinfo *c = cpu_data + cpu;
42
43         c->loops_per_jiffy = loops_per_jiffy;
44 }
45
46 void __init smp_prepare_cpus(unsigned int max_cpus)
47 {
48         unsigned int cpu = smp_processor_id();
49
50         init_new_context(current, &init_mm);
51         current_thread_info()->cpu = cpu;
52         plat_prepare_cpus(max_cpus);
53
54 #ifndef CONFIG_HOTPLUG_CPU
55         cpu_present_map = cpu_possible_map;
56 #endif
57 }
58
59 void __devinit smp_prepare_boot_cpu(void)
60 {
61         unsigned int cpu = smp_processor_id();
62
63         __cpu_number_map[0] = cpu;
64         __cpu_logical_map[0] = cpu;
65
66         cpu_set(cpu, cpu_online_map);
67         cpu_set(cpu, cpu_possible_map);
68 }
69
70 asmlinkage void __cpuinit start_secondary(void)
71 {
72         unsigned int cpu;
73         struct mm_struct *mm = &init_mm;
74
75         atomic_inc(&mm->mm_count);
76         atomic_inc(&mm->mm_users);
77         current->active_mm = mm;
78         BUG_ON(current->mm);
79         enter_lazy_tlb(mm, current);
80
81         per_cpu_trap_init();
82
83         preempt_disable();
84
85         local_irq_enable();
86
87         cpu = smp_processor_id();
88
89         /* Enable local timers */
90         local_timer_setup(cpu);
91         calibrate_delay();
92
93         smp_store_cpu_info(cpu);
94
95         cpu_set(cpu, cpu_online_map);
96
97         cpu_idle();
98 }
99
100 extern struct {
101         unsigned long sp;
102         unsigned long bss_start;
103         unsigned long bss_end;
104         void *start_kernel_fn;
105         void *cpu_init_fn;
106         void *thread_info;
107 } stack_start;
108
109 int __cpuinit __cpu_up(unsigned int cpu)
110 {
111         struct task_struct *tsk;
112         unsigned long timeout;
113
114         tsk = fork_idle(cpu);
115         if (IS_ERR(tsk)) {
116                 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
117                 return PTR_ERR(tsk);
118         }
119
120         /* Fill in data in head.S for secondary cpus */
121         stack_start.sp = tsk->thread.sp;
122         stack_start.thread_info = tsk->stack;
123         stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
124         stack_start.start_kernel_fn = start_secondary;
125
126         flush_cache_all();
127
128         plat_start_cpu(cpu, (unsigned long)_stext);
129
130         timeout = jiffies + HZ;
131         while (time_before(jiffies, timeout)) {
132                 if (cpu_online(cpu))
133                         break;
134
135                 udelay(10);
136         }
137
138         if (cpu_online(cpu))
139                 return 0;
140
141         return -ENOENT;
142 }
143
144 void __init smp_cpus_done(unsigned int max_cpus)
145 {
146         unsigned long bogosum = 0;
147         int cpu;
148
149         for_each_online_cpu(cpu)
150                 bogosum += cpu_data[cpu].loops_per_jiffy;
151
152         printk(KERN_INFO "SMP: Total of %d processors activated "
153                "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
154                bogosum / (500000/HZ),
155                (bogosum / (5000/HZ)) % 100);
156 }
157
158 void smp_send_reschedule(int cpu)
159 {
160         plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
161 }
162
163 static void stop_this_cpu(void *unused)
164 {
165         cpu_clear(smp_processor_id(), cpu_online_map);
166         local_irq_disable();
167
168         for (;;)
169                 cpu_relax();
170 }
171
172 void smp_send_stop(void)
173 {
174         smp_call_function(stop_this_cpu, 0, 0);
175 }
176
177 void arch_send_call_function_ipi(cpumask_t mask)
178 {
179         int cpu;
180
181         for_each_cpu_mask(cpu, mask)
182                 plat_send_ipi(cpu, SMP_MSG_FUNCTION);
183 }
184
185 void arch_send_call_function_single_ipi(int cpu)
186 {
187         plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
188 }
189
190 void smp_timer_broadcast(cpumask_t mask)
191 {
192         int cpu;
193
194         for_each_cpu_mask(cpu, mask)
195                 plat_send_ipi(cpu, SMP_MSG_TIMER);
196 }
197
198 static void ipi_timer(void)
199 {
200         irq_enter();
201         local_timer_interrupt();
202         irq_exit();
203 }
204
205 void smp_message_recv(unsigned int msg)
206 {
207         switch (msg) {
208         case SMP_MSG_FUNCTION:
209                 generic_smp_call_function_interrupt();
210                 break;
211         case SMP_MSG_RESCHEDULE:
212                 break;
213         case SMP_MSG_FUNCTION_SINGLE:
214                 generic_smp_call_function_single_interrupt();
215                 break;
216         case SMP_MSG_TIMER:
217                 ipi_timer();
218                 break;
219         default:
220                 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
221                        smp_processor_id(), __func__, msg);
222                 break;
223         }
224 }
225
226 /* Not really SMP stuff ... */
227 int setup_profiling_timer(unsigned int multiplier)
228 {
229         return 0;
230 }
231
232 static void flush_tlb_all_ipi(void *info)
233 {
234         local_flush_tlb_all();
235 }
236
237 void flush_tlb_all(void)
238 {
239         on_each_cpu(flush_tlb_all_ipi, 0, 1);
240 }
241
242 static void flush_tlb_mm_ipi(void *mm)
243 {
244         local_flush_tlb_mm((struct mm_struct *)mm);
245 }
246
247 /*
248  * The following tlb flush calls are invoked when old translations are
249  * being torn down, or pte attributes are changing. For single threaded
250  * address spaces, a new context is obtained on the current cpu, and tlb
251  * context on other cpus are invalidated to force a new context allocation
252  * at switch_mm time, should the mm ever be used on other cpus. For
253  * multithreaded address spaces, intercpu interrupts have to be sent.
254  * Another case where intercpu interrupts are required is when the target
255  * mm might be active on another cpu (eg debuggers doing the flushes on
256  * behalf of debugees, kswapd stealing pages from another process etc).
257  * Kanoj 07/00.
258  */
259
260 void flush_tlb_mm(struct mm_struct *mm)
261 {
262         preempt_disable();
263
264         if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
265                 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
266         } else {
267                 int i;
268                 for (i = 0; i < num_online_cpus(); i++)
269                         if (smp_processor_id() != i)
270                                 cpu_context(i, mm) = 0;
271         }
272         local_flush_tlb_mm(mm);
273
274         preempt_enable();
275 }
276
277 struct flush_tlb_data {
278         struct vm_area_struct *vma;
279         unsigned long addr1;
280         unsigned long addr2;
281 };
282
283 static void flush_tlb_range_ipi(void *info)
284 {
285         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
286
287         local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
288 }
289
290 void flush_tlb_range(struct vm_area_struct *vma,
291                      unsigned long start, unsigned long end)
292 {
293         struct mm_struct *mm = vma->vm_mm;
294
295         preempt_disable();
296         if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
297                 struct flush_tlb_data fd;
298
299                 fd.vma = vma;
300                 fd.addr1 = start;
301                 fd.addr2 = end;
302                 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
303         } else {
304                 int i;
305                 for (i = 0; i < num_online_cpus(); i++)
306                         if (smp_processor_id() != i)
307                                 cpu_context(i, mm) = 0;
308         }
309         local_flush_tlb_range(vma, start, end);
310         preempt_enable();
311 }
312
313 static void flush_tlb_kernel_range_ipi(void *info)
314 {
315         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
316
317         local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
318 }
319
320 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
321 {
322         struct flush_tlb_data fd;
323
324         fd.addr1 = start;
325         fd.addr2 = end;
326         on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
327 }
328
329 static void flush_tlb_page_ipi(void *info)
330 {
331         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
332
333         local_flush_tlb_page(fd->vma, fd->addr1);
334 }
335
336 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
337 {
338         preempt_disable();
339         if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
340             (current->mm != vma->vm_mm)) {
341                 struct flush_tlb_data fd;
342
343                 fd.vma = vma;
344                 fd.addr1 = page;
345                 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
346         } else {
347                 int i;
348                 for (i = 0; i < num_online_cpus(); i++)
349                         if (smp_processor_id() != i)
350                                 cpu_context(i, vma->vm_mm) = 0;
351         }
352         local_flush_tlb_page(vma, page);
353         preempt_enable();
354 }
355
356 static void flush_tlb_one_ipi(void *info)
357 {
358         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
359         local_flush_tlb_one(fd->addr1, fd->addr2);
360 }
361
362 void flush_tlb_one(unsigned long asid, unsigned long vaddr)
363 {
364         struct flush_tlb_data fd;
365
366         fd.addr1 = asid;
367         fd.addr2 = vaddr;
368
369         smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
370         local_flush_tlb_one(asid, vaddr);
371 }