Merge branch 'timers/range-hrtimers' into v28-range-hrtimers-for-linus-v2
[linux-2.6] / 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         notify_cpu_starting(smp_processor_id());
86
87         local_irq_enable();
88
89         cpu = smp_processor_id();
90
91         /* Enable local timers */
92         local_timer_setup(cpu);
93         calibrate_delay();
94
95         smp_store_cpu_info(cpu);
96
97         cpu_set(cpu, cpu_online_map);
98
99         cpu_idle();
100 }
101
102 extern struct {
103         unsigned long sp;
104         unsigned long bss_start;
105         unsigned long bss_end;
106         void *start_kernel_fn;
107         void *cpu_init_fn;
108         void *thread_info;
109 } stack_start;
110
111 int __cpuinit __cpu_up(unsigned int cpu)
112 {
113         struct task_struct *tsk;
114         unsigned long timeout;
115
116         tsk = fork_idle(cpu);
117         if (IS_ERR(tsk)) {
118                 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
119                 return PTR_ERR(tsk);
120         }
121
122         /* Fill in data in head.S for secondary cpus */
123         stack_start.sp = tsk->thread.sp;
124         stack_start.thread_info = tsk->stack;
125         stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
126         stack_start.start_kernel_fn = start_secondary;
127
128         flush_cache_all();
129
130         plat_start_cpu(cpu, (unsigned long)_stext);
131
132         timeout = jiffies + HZ;
133         while (time_before(jiffies, timeout)) {
134                 if (cpu_online(cpu))
135                         break;
136
137                 udelay(10);
138         }
139
140         if (cpu_online(cpu))
141                 return 0;
142
143         return -ENOENT;
144 }
145
146 void __init smp_cpus_done(unsigned int max_cpus)
147 {
148         unsigned long bogosum = 0;
149         int cpu;
150
151         for_each_online_cpu(cpu)
152                 bogosum += cpu_data[cpu].loops_per_jiffy;
153
154         printk(KERN_INFO "SMP: Total of %d processors activated "
155                "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
156                bogosum / (500000/HZ),
157                (bogosum / (5000/HZ)) % 100);
158 }
159
160 void smp_send_reschedule(int cpu)
161 {
162         plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
163 }
164
165 static void stop_this_cpu(void *unused)
166 {
167         cpu_clear(smp_processor_id(), cpu_online_map);
168         local_irq_disable();
169
170         for (;;)
171                 cpu_relax();
172 }
173
174 void smp_send_stop(void)
175 {
176         smp_call_function(stop_this_cpu, 0, 0);
177 }
178
179 void arch_send_call_function_ipi(cpumask_t mask)
180 {
181         int cpu;
182
183         for_each_cpu_mask(cpu, mask)
184                 plat_send_ipi(cpu, SMP_MSG_FUNCTION);
185 }
186
187 void arch_send_call_function_single_ipi(int cpu)
188 {
189         plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
190 }
191
192 void smp_timer_broadcast(cpumask_t mask)
193 {
194         int cpu;
195
196         for_each_cpu_mask(cpu, mask)
197                 plat_send_ipi(cpu, SMP_MSG_TIMER);
198 }
199
200 static void ipi_timer(void)
201 {
202         irq_enter();
203         local_timer_interrupt();
204         irq_exit();
205 }
206
207 void smp_message_recv(unsigned int msg)
208 {
209         switch (msg) {
210         case SMP_MSG_FUNCTION:
211                 generic_smp_call_function_interrupt();
212                 break;
213         case SMP_MSG_RESCHEDULE:
214                 break;
215         case SMP_MSG_FUNCTION_SINGLE:
216                 generic_smp_call_function_single_interrupt();
217                 break;
218         case SMP_MSG_TIMER:
219                 ipi_timer();
220                 break;
221         default:
222                 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
223                        smp_processor_id(), __func__, msg);
224                 break;
225         }
226 }
227
228 /* Not really SMP stuff ... */
229 int setup_profiling_timer(unsigned int multiplier)
230 {
231         return 0;
232 }
233
234 static void flush_tlb_all_ipi(void *info)
235 {
236         local_flush_tlb_all();
237 }
238
239 void flush_tlb_all(void)
240 {
241         on_each_cpu(flush_tlb_all_ipi, 0, 1);
242 }
243
244 static void flush_tlb_mm_ipi(void *mm)
245 {
246         local_flush_tlb_mm((struct mm_struct *)mm);
247 }
248
249 /*
250  * The following tlb flush calls are invoked when old translations are
251  * being torn down, or pte attributes are changing. For single threaded
252  * address spaces, a new context is obtained on the current cpu, and tlb
253  * context on other cpus are invalidated to force a new context allocation
254  * at switch_mm time, should the mm ever be used on other cpus. For
255  * multithreaded address spaces, intercpu interrupts have to be sent.
256  * Another case where intercpu interrupts are required is when the target
257  * mm might be active on another cpu (eg debuggers doing the flushes on
258  * behalf of debugees, kswapd stealing pages from another process etc).
259  * Kanoj 07/00.
260  */
261
262 void flush_tlb_mm(struct mm_struct *mm)
263 {
264         preempt_disable();
265
266         if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
267                 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
268         } else {
269                 int i;
270                 for (i = 0; i < num_online_cpus(); i++)
271                         if (smp_processor_id() != i)
272                                 cpu_context(i, mm) = 0;
273         }
274         local_flush_tlb_mm(mm);
275
276         preempt_enable();
277 }
278
279 struct flush_tlb_data {
280         struct vm_area_struct *vma;
281         unsigned long addr1;
282         unsigned long addr2;
283 };
284
285 static void flush_tlb_range_ipi(void *info)
286 {
287         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
288
289         local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
290 }
291
292 void flush_tlb_range(struct vm_area_struct *vma,
293                      unsigned long start, unsigned long end)
294 {
295         struct mm_struct *mm = vma->vm_mm;
296
297         preempt_disable();
298         if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
299                 struct flush_tlb_data fd;
300
301                 fd.vma = vma;
302                 fd.addr1 = start;
303                 fd.addr2 = end;
304                 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
305         } else {
306                 int i;
307                 for (i = 0; i < num_online_cpus(); i++)
308                         if (smp_processor_id() != i)
309                                 cpu_context(i, mm) = 0;
310         }
311         local_flush_tlb_range(vma, start, end);
312         preempt_enable();
313 }
314
315 static void flush_tlb_kernel_range_ipi(void *info)
316 {
317         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
318
319         local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
320 }
321
322 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
323 {
324         struct flush_tlb_data fd;
325
326         fd.addr1 = start;
327         fd.addr2 = end;
328         on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
329 }
330
331 static void flush_tlb_page_ipi(void *info)
332 {
333         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
334
335         local_flush_tlb_page(fd->vma, fd->addr1);
336 }
337
338 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
339 {
340         preempt_disable();
341         if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
342             (current->mm != vma->vm_mm)) {
343                 struct flush_tlb_data fd;
344
345                 fd.vma = vma;
346                 fd.addr1 = page;
347                 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
348         } else {
349                 int i;
350                 for (i = 0; i < num_online_cpus(); i++)
351                         if (smp_processor_id() != i)
352                                 cpu_context(i, vma->vm_mm) = 0;
353         }
354         local_flush_tlb_page(vma, page);
355         preempt_enable();
356 }
357
358 static void flush_tlb_one_ipi(void *info)
359 {
360         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
361         local_flush_tlb_one(fd->addr1, fd->addr2);
362 }
363
364 void flush_tlb_one(unsigned long asid, unsigned long vaddr)
365 {
366         struct flush_tlb_data fd;
367
368         fd.addr1 = asid;
369         fd.addr2 = vaddr;
370
371         smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
372         local_flush_tlb_one(asid, vaddr);
373 }