blob: c55d314166c79bd8802464d1023fdd931e6967cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/kernel/smp.c
3 *
4 * SMP support for the SuperH processors.
5 *
Paul Mundt173a44d2008-08-06 18:02:48 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Paul Mundtaba10302007-09-21 18:32:32 +09007 * Copyright (C) 2006 - 2007 Akio Idehara
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Paul Mundtaba10302007-09-21 18:32:32 +09009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
Evgeniy Polyakov66c52272007-05-31 13:46:21 +090013#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/cache.h>
15#include <linux/cpumask.h>
16#include <linux/delay.h>
17#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/spinlock.h>
Paul Mundtaba10302007-09-21 18:32:32 +090019#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Paul Mundtaba10302007-09-21 18:32:32 +090021#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Paul Mundtaba10302007-09-21 18:32:32 +090027#include <asm/cacheflush.h>
28#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Paul Mundtaba10302007-09-21 18:32:32 +090030int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
31int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33cpumask_t cpu_possible_map;
David S. Millerc8923c62005-10-13 14:41:23 -070034EXPORT_SYMBOL(cpu_possible_map);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036cpumask_t cpu_online_map;
Greg Bankse16b38f2006-10-02 02:17:40 -070037EXPORT_SYMBOL(cpu_online_map);
Paul Mundtaba10302007-09-21 18:32:32 +090038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static inline void __init smp_store_cpu_info(unsigned int cpu)
40{
Paul Mundtaba10302007-09-21 18:32:32 +090041 struct sh_cpuinfo *c = cpu_data + cpu;
42
43 c->loops_per_jiffy = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46void __init smp_prepare_cpus(unsigned int max_cpus)
47{
48 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Paul Mundtaba10302007-09-21 18:32:32 +090050 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59void __devinit smp_prepare_boot_cpu(void)
60{
61 unsigned int cpu = smp_processor_id();
62
Paul Mundtaba10302007-09-21 18:32:32 +090063 __cpu_number_map[0] = cpu;
64 __cpu_logical_map[0] = cpu;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 cpu_set(cpu, cpu_online_map);
67 cpu_set(cpu, cpu_possible_map);
68}
69
Paul Mundtaba10302007-09-21 18:32:32 +090070asmlinkage void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Paul Mundtaba10302007-09-21 18:32:32 +090072 unsigned int cpu;
73 struct mm_struct *mm = &init_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Paul Mundtaba10302007-09-21 18:32:32 +090075 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Paul Mundtaba10302007-09-21 18:32:32 +090081 per_cpu_trap_init();
82
83 preempt_disable();
84
85 local_irq_enable();
86
Paul Mundt8c245942008-08-06 18:37:07 +090087 cpu = smp_processor_id();
88
89 /* Enable local timers */
90 local_timer_setup(cpu);
Paul Mundtaba10302007-09-21 18:32:32 +090091 calibrate_delay();
92
Paul Mundtaba10302007-09-21 18:32:32 +090093 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 cpu_set(cpu, cpu_online_map);
96
Paul Mundtaba10302007-09-21 18:32:32 +090097 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Paul Mundtaba10302007-09-21 18:32:32 +0900100extern 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
109int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Paul Mundtaba10302007-09-21 18:32:32 +0900111 struct task_struct *tsk;
112 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800113
Paul Mundtaba10302007-09-21 18:32:32 +0900114 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Paul Mundtaba10302007-09-21 18:32:32 +0900120 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Paul Mundtaba10302007-09-21 18:32:32 +0900126 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Paul Mundtaba10302007-09-21 18:32:32 +0900128 plat_start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Paul Mundtaba10302007-09-21 18:32:32 +0900130 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144void __init smp_cpus_done(unsigned int max_cpus)
145{
Paul Mundtaba10302007-09-21 18:32:32 +0900146 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158void smp_send_reschedule(int cpu)
159{
Paul Mundtaba10302007-09-21 18:32:32 +0900160 plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static 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
172void smp_send_stop(void)
173{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200174 smp_call_function(stop_this_cpu, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jens Axboe490f5de2008-06-10 20:52:59 +0200177void arch_send_call_function_ipi(cpumask_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Jens Axboe490f5de2008-06-10 20:52:59 +0200179 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jens Axboe490f5de2008-06-10 20:52:59 +0200181 for_each_cpu_mask(cpu, mask)
182 plat_send_ipi(cpu, SMP_MSG_FUNCTION);
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jens Axboe490f5de2008-06-10 20:52:59 +0200185void arch_send_call_function_single_ipi(int cpu)
186{
187 plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Paul Mundt6f527072008-08-06 18:21:03 +0900190void 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
198static void ipi_timer(void)
199{
200 irq_enter();
Paul Mundt8c245942008-08-06 18:37:07 +0900201 local_timer_interrupt();
Paul Mundt6f527072008-08-06 18:21:03 +0900202 irq_exit();
203}
204
Paul Mundt173a44d2008-08-06 18:02:48 +0900205void 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;
Paul Mundt6f527072008-08-06 18:21:03 +0900216 case SMP_MSG_TIMER:
217 ipi_timer();
218 break;
Paul Mundt173a44d2008-08-06 18:02:48 +0900219 default:
220 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
221 smp_processor_id(), __func__, msg);
222 break;
223 }
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Not really SMP stuff ... */
227int setup_profiling_timer(unsigned int multiplier)
228{
229 return 0;
230}
231
Paul Mundt9964fa82007-09-21 18:09:55 +0900232static void flush_tlb_all_ipi(void *info)
233{
234 local_flush_tlb_all();
235}
236
237void flush_tlb_all(void)
238{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200239 on_each_cpu(flush_tlb_all_ipi, 0, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900240}
241
242static 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
260void flush_tlb_mm(struct mm_struct *mm)
261{
262 preempt_disable();
263
264 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Jens Axboe8691e5a2008-06-06 11:18:06 +0200265 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900266 } 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
277struct flush_tlb_data {
278 struct vm_area_struct *vma;
279 unsigned long addr1;
280 unsigned long addr2;
281};
282
283static 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
290void 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;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200302 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900303 } 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
313static 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
320void 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;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200326 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900327}
328
329static 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
336void 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;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200345 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900346 } 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
356static 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
362void 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
Jens Axboe8691e5a2008-06-06 11:18:06 +0200369 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900370 local_flush_tlb_one(asid, vaddr);
371}