blob: 8f40274126142847f99e7c51f71e13a22813b2cf [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 Mundtb56050a2008-10-21 12:39:24 +090021#include <linux/cpu.h>
Paul Mundtaba10302007-09-21 18:32:32 +090022#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/atomic.h>
24#include <asm/processor.h>
25#include <asm/system.h>
26#include <asm/mmu_context.h>
27#include <asm/smp.h>
Paul Mundtaba10302007-09-21 18:32:32 +090028#include <asm/cacheflush.h>
29#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Paul Mundtaba10302007-09-21 18:32:32 +090031int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
32int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static inline void __init smp_store_cpu_info(unsigned int cpu)
35{
Paul Mundtaba10302007-09-21 18:32:32 +090036 struct sh_cpuinfo *c = cpu_data + cpu;
37
38 c->loops_per_jiffy = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41void __init smp_prepare_cpus(unsigned int max_cpus)
42{
43 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Paul Mundtaba10302007-09-21 18:32:32 +090045 init_new_context(current, &init_mm);
46 current_thread_info()->cpu = cpu;
47 plat_prepare_cpus(max_cpus);
48
49#ifndef CONFIG_HOTPLUG_CPU
50 cpu_present_map = cpu_possible_map;
51#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54void __devinit smp_prepare_boot_cpu(void)
55{
56 unsigned int cpu = smp_processor_id();
57
Paul Mundtaba10302007-09-21 18:32:32 +090058 __cpu_number_map[0] = cpu;
59 __cpu_logical_map[0] = cpu;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 cpu_set(cpu, cpu_online_map);
62 cpu_set(cpu, cpu_possible_map);
63}
64
Paul Mundtaba10302007-09-21 18:32:32 +090065asmlinkage void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Paul Mundtaba10302007-09-21 18:32:32 +090067 unsigned int cpu;
68 struct mm_struct *mm = &init_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Paul Mundtaba10302007-09-21 18:32:32 +090070 atomic_inc(&mm->mm_count);
71 atomic_inc(&mm->mm_users);
72 current->active_mm = mm;
73 BUG_ON(current->mm);
74 enter_lazy_tlb(mm, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Paul Mundtaba10302007-09-21 18:32:32 +090076 per_cpu_trap_init();
77
78 preempt_disable();
79
Manfred Spraule545a612008-09-07 16:57:22 +020080 notify_cpu_starting(smp_processor_id());
81
Paul Mundtaba10302007-09-21 18:32:32 +090082 local_irq_enable();
83
Paul Mundt8c245942008-08-06 18:37:07 +090084 cpu = smp_processor_id();
85
86 /* Enable local timers */
87 local_timer_setup(cpu);
Paul Mundtaba10302007-09-21 18:32:32 +090088 calibrate_delay();
89
Paul Mundtaba10302007-09-21 18:32:32 +090090 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 cpu_set(cpu, cpu_online_map);
93
Paul Mundtaba10302007-09-21 18:32:32 +090094 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Paul Mundtaba10302007-09-21 18:32:32 +090097extern struct {
98 unsigned long sp;
99 unsigned long bss_start;
100 unsigned long bss_end;
101 void *start_kernel_fn;
102 void *cpu_init_fn;
103 void *thread_info;
104} stack_start;
105
106int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Paul Mundtaba10302007-09-21 18:32:32 +0900108 struct task_struct *tsk;
109 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800110
Paul Mundtaba10302007-09-21 18:32:32 +0900111 tsk = fork_idle(cpu);
112 if (IS_ERR(tsk)) {
113 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
114 return PTR_ERR(tsk);
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Paul Mundtaba10302007-09-21 18:32:32 +0900117 /* Fill in data in head.S for secondary cpus */
118 stack_start.sp = tsk->thread.sp;
119 stack_start.thread_info = tsk->stack;
120 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
121 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Paul Mundtaba10302007-09-21 18:32:32 +0900123 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Paul Mundtaba10302007-09-21 18:32:32 +0900125 plat_start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Paul Mundtaba10302007-09-21 18:32:32 +0900127 timeout = jiffies + HZ;
128 while (time_before(jiffies, timeout)) {
129 if (cpu_online(cpu))
130 break;
131
132 udelay(10);
133 }
134
135 if (cpu_online(cpu))
136 return 0;
137
138 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141void __init smp_cpus_done(unsigned int max_cpus)
142{
Paul Mundtaba10302007-09-21 18:32:32 +0900143 unsigned long bogosum = 0;
144 int cpu;
145
146 for_each_online_cpu(cpu)
147 bogosum += cpu_data[cpu].loops_per_jiffy;
148
149 printk(KERN_INFO "SMP: Total of %d processors activated "
150 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
151 bogosum / (500000/HZ),
152 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155void smp_send_reschedule(int cpu)
156{
Paul Mundtaba10302007-09-21 18:32:32 +0900157 plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static void stop_this_cpu(void *unused)
161{
162 cpu_clear(smp_processor_id(), cpu_online_map);
163 local_irq_disable();
164
165 for (;;)
166 cpu_relax();
167}
168
169void smp_send_stop(void)
170{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200171 smp_call_function(stop_this_cpu, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Jens Axboe490f5de2008-06-10 20:52:59 +0200174void arch_send_call_function_ipi(cpumask_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Jens Axboe490f5de2008-06-10 20:52:59 +0200176 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jens Axboe490f5de2008-06-10 20:52:59 +0200178 for_each_cpu_mask(cpu, mask)
179 plat_send_ipi(cpu, SMP_MSG_FUNCTION);
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jens Axboe490f5de2008-06-10 20:52:59 +0200182void arch_send_call_function_single_ipi(int cpu)
183{
184 plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Rusty Russell320ab2b2008-12-13 21:20:26 +1030187void smp_timer_broadcast(const struct cpumask *mask)
Paul Mundt6f527072008-08-06 18:21:03 +0900188{
189 int cpu;
190
Rusty Russell320ab2b2008-12-13 21:20:26 +1030191 for_each_cpu(cpu, mask)
Paul Mundt6f527072008-08-06 18:21:03 +0900192 plat_send_ipi(cpu, SMP_MSG_TIMER);
193}
194
195static void ipi_timer(void)
196{
197 irq_enter();
Paul Mundt8c245942008-08-06 18:37:07 +0900198 local_timer_interrupt();
Paul Mundt6f527072008-08-06 18:21:03 +0900199 irq_exit();
200}
201
Paul Mundt173a44d2008-08-06 18:02:48 +0900202void smp_message_recv(unsigned int msg)
203{
204 switch (msg) {
205 case SMP_MSG_FUNCTION:
206 generic_smp_call_function_interrupt();
207 break;
208 case SMP_MSG_RESCHEDULE:
209 break;
210 case SMP_MSG_FUNCTION_SINGLE:
211 generic_smp_call_function_single_interrupt();
212 break;
Paul Mundt6f527072008-08-06 18:21:03 +0900213 case SMP_MSG_TIMER:
214 ipi_timer();
215 break;
Paul Mundt173a44d2008-08-06 18:02:48 +0900216 default:
217 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
218 smp_processor_id(), __func__, msg);
219 break;
220 }
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* Not really SMP stuff ... */
224int setup_profiling_timer(unsigned int multiplier)
225{
226 return 0;
227}
228
Paul Mundt9964fa82007-09-21 18:09:55 +0900229static void flush_tlb_all_ipi(void *info)
230{
231 local_flush_tlb_all();
232}
233
234void flush_tlb_all(void)
235{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200236 on_each_cpu(flush_tlb_all_ipi, 0, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900237}
238
239static void flush_tlb_mm_ipi(void *mm)
240{
241 local_flush_tlb_mm((struct mm_struct *)mm);
242}
243
244/*
245 * The following tlb flush calls are invoked when old translations are
246 * being torn down, or pte attributes are changing. For single threaded
247 * address spaces, a new context is obtained on the current cpu, and tlb
248 * context on other cpus are invalidated to force a new context allocation
249 * at switch_mm time, should the mm ever be used on other cpus. For
250 * multithreaded address spaces, intercpu interrupts have to be sent.
251 * Another case where intercpu interrupts are required is when the target
252 * mm might be active on another cpu (eg debuggers doing the flushes on
253 * behalf of debugees, kswapd stealing pages from another process etc).
254 * Kanoj 07/00.
255 */
256
257void flush_tlb_mm(struct mm_struct *mm)
258{
259 preempt_disable();
260
261 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Jens Axboe8691e5a2008-06-06 11:18:06 +0200262 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900263 } else {
264 int i;
265 for (i = 0; i < num_online_cpus(); i++)
266 if (smp_processor_id() != i)
267 cpu_context(i, mm) = 0;
268 }
269 local_flush_tlb_mm(mm);
270
271 preempt_enable();
272}
273
274struct flush_tlb_data {
275 struct vm_area_struct *vma;
276 unsigned long addr1;
277 unsigned long addr2;
278};
279
280static void flush_tlb_range_ipi(void *info)
281{
282 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
283
284 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
285}
286
287void flush_tlb_range(struct vm_area_struct *vma,
288 unsigned long start, unsigned long end)
289{
290 struct mm_struct *mm = vma->vm_mm;
291
292 preempt_disable();
293 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
294 struct flush_tlb_data fd;
295
296 fd.vma = vma;
297 fd.addr1 = start;
298 fd.addr2 = end;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200299 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900300 } else {
301 int i;
302 for (i = 0; i < num_online_cpus(); i++)
303 if (smp_processor_id() != i)
304 cpu_context(i, mm) = 0;
305 }
306 local_flush_tlb_range(vma, start, end);
307 preempt_enable();
308}
309
310static void flush_tlb_kernel_range_ipi(void *info)
311{
312 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
313
314 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
315}
316
317void flush_tlb_kernel_range(unsigned long start, unsigned long end)
318{
319 struct flush_tlb_data fd;
320
321 fd.addr1 = start;
322 fd.addr2 = end;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200323 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900324}
325
326static void flush_tlb_page_ipi(void *info)
327{
328 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
329
330 local_flush_tlb_page(fd->vma, fd->addr1);
331}
332
333void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
334{
335 preempt_disable();
336 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
337 (current->mm != vma->vm_mm)) {
338 struct flush_tlb_data fd;
339
340 fd.vma = vma;
341 fd.addr1 = page;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200342 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900343 } else {
344 int i;
345 for (i = 0; i < num_online_cpus(); i++)
346 if (smp_processor_id() != i)
347 cpu_context(i, vma->vm_mm) = 0;
348 }
349 local_flush_tlb_page(vma, page);
350 preempt_enable();
351}
352
353static void flush_tlb_one_ipi(void *info)
354{
355 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
356 local_flush_tlb_one(fd->addr1, fd->addr2);
357}
358
359void flush_tlb_one(unsigned long asid, unsigned long vaddr)
360{
361 struct flush_tlb_data fd;
362
363 fd.addr1 = asid;
364 fd.addr2 = vaddr;
365
Jens Axboe8691e5a2008-06-06 11:18:06 +0200366 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900367 local_flush_tlb_one(asid, vaddr);
368}