blob: cc87830ace7f75c077ee58dea9c1b0ea4f28c02f [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 Mundt3366e352010-03-30 12:38:01 +09006 * Copyright (C) 2002 - 2010 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
Paul Mundt3366e352010-03-30 12:38:01 +090034struct plat_smp_ops *mp_ops = NULL;
35
36void __cpuinit register_smp_ops(struct plat_smp_ops *ops)
37{
38 if (mp_ops)
39 printk(KERN_WARNING "Overriding previously set SMP ops\n");
40
41 mp_ops = ops;
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static inline void __init smp_store_cpu_info(unsigned int cpu)
45{
Paul Mundtaba10302007-09-21 18:32:32 +090046 struct sh_cpuinfo *c = cpu_data + cpu;
47
Paul Mundta66c2ed2009-10-14 14:14:30 +090048 memcpy(c, &boot_cpu_data, sizeof(struct sh_cpuinfo));
49
Paul Mundtaba10302007-09-21 18:32:32 +090050 c->loops_per_jiffy = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53void __init smp_prepare_cpus(unsigned int max_cpus)
54{
55 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Paul Mundtaba10302007-09-21 18:32:32 +090057 init_new_context(current, &init_mm);
58 current_thread_info()->cpu = cpu;
Paul Mundt3366e352010-03-30 12:38:01 +090059 mp_ops->prepare_cpus(max_cpus);
Paul Mundtaba10302007-09-21 18:32:32 +090060
61#ifndef CONFIG_HOTPLUG_CPU
Rusty Russelle09377b2009-06-12 22:33:14 +093062 init_cpu_present(&cpu_possible_map);
Paul Mundtaba10302007-09-21 18:32:32 +090063#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66void __devinit smp_prepare_boot_cpu(void)
67{
68 unsigned int cpu = smp_processor_id();
69
Paul Mundtaba10302007-09-21 18:32:32 +090070 __cpu_number_map[0] = cpu;
71 __cpu_logical_map[0] = cpu;
72
Rusty Russelle09377b2009-06-12 22:33:14 +093073 set_cpu_online(cpu, true);
74 set_cpu_possible(cpu, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Paul Mundtaba10302007-09-21 18:32:32 +090077asmlinkage void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Paul Mundtaba10302007-09-21 18:32:32 +090079 unsigned int cpu;
80 struct mm_struct *mm = &init_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Matt Fleming4bea3412010-03-28 20:08:25 +000082 enable_mmu();
Paul Mundtaba10302007-09-21 18:32:32 +090083 atomic_inc(&mm->mm_count);
84 atomic_inc(&mm->mm_users);
85 current->active_mm = mm;
86 BUG_ON(current->mm);
87 enter_lazy_tlb(mm, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Paul Mundtaba10302007-09-21 18:32:32 +090089 per_cpu_trap_init();
90
91 preempt_disable();
92
Manfred Spraule545a612008-09-07 16:57:22 +020093 notify_cpu_starting(smp_processor_id());
94
Paul Mundtaba10302007-09-21 18:32:32 +090095 local_irq_enable();
96
Paul Mundt8c245942008-08-06 18:37:07 +090097 cpu = smp_processor_id();
98
99 /* Enable local timers */
100 local_timer_setup(cpu);
Paul Mundtaba10302007-09-21 18:32:32 +0900101 calibrate_delay();
102
Paul Mundtaba10302007-09-21 18:32:32 +0900103 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Paul Mundtf0ccf272010-04-26 18:39:50 +0900105 set_cpu_online(cpu, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Paul Mundtaba10302007-09-21 18:32:32 +0900107 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Paul Mundtaba10302007-09-21 18:32:32 +0900110extern struct {
111 unsigned long sp;
112 unsigned long bss_start;
113 unsigned long bss_end;
114 void *start_kernel_fn;
115 void *cpu_init_fn;
116 void *thread_info;
117} stack_start;
118
119int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Paul Mundtaba10302007-09-21 18:32:32 +0900121 struct task_struct *tsk;
122 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800123
Paul Mundtaba10302007-09-21 18:32:32 +0900124 tsk = fork_idle(cpu);
125 if (IS_ERR(tsk)) {
126 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
127 return PTR_ERR(tsk);
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Paul Mundtaba10302007-09-21 18:32:32 +0900130 /* Fill in data in head.S for secondary cpus */
131 stack_start.sp = tsk->thread.sp;
132 stack_start.thread_info = tsk->stack;
133 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
134 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Paul Mundtd7806132009-10-14 11:51:28 +0900136 flush_icache_range((unsigned long)&stack_start,
137 (unsigned long)&stack_start + sizeof(stack_start));
138 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Paul Mundt3366e352010-03-30 12:38:01 +0900140 mp_ops->start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Paul Mundtaba10302007-09-21 18:32:32 +0900142 timeout = jiffies + HZ;
143 while (time_before(jiffies, timeout)) {
144 if (cpu_online(cpu))
145 break;
146
147 udelay(10);
148 }
149
150 if (cpu_online(cpu))
151 return 0;
152
153 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156void __init smp_cpus_done(unsigned int max_cpus)
157{
Paul Mundtaba10302007-09-21 18:32:32 +0900158 unsigned long bogosum = 0;
159 int cpu;
160
161 for_each_online_cpu(cpu)
162 bogosum += cpu_data[cpu].loops_per_jiffy;
163
164 printk(KERN_INFO "SMP: Total of %d processors activated "
165 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
166 bogosum / (500000/HZ),
167 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170void smp_send_reschedule(int cpu)
171{
Paul Mundt3366e352010-03-30 12:38:01 +0900172 mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175void smp_send_stop(void)
176{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200177 smp_call_function(stop_this_cpu, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Rusty Russell819807d2009-06-12 22:32:35 +0930180void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Jens Axboe490f5de2008-06-10 20:52:59 +0200182 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Rusty Russell819807d2009-06-12 22:32:35 +0930184 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900185 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
Jens Axboe490f5de2008-06-10 20:52:59 +0200186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Jens Axboe490f5de2008-06-10 20:52:59 +0200188void arch_send_call_function_single_ipi(int cpu)
189{
Paul Mundt3366e352010-03-30 12:38:01 +0900190 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Rusty Russell320ab2b2008-12-13 21:20:26 +1030193void smp_timer_broadcast(const struct cpumask *mask)
Paul Mundt6f527072008-08-06 18:21:03 +0900194{
195 int cpu;
196
Rusty Russell320ab2b2008-12-13 21:20:26 +1030197 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900198 mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
Paul Mundt6f527072008-08-06 18:21:03 +0900199}
200
201static void ipi_timer(void)
202{
203 irq_enter();
Paul Mundt8c245942008-08-06 18:37:07 +0900204 local_timer_interrupt();
Paul Mundt6f527072008-08-06 18:21:03 +0900205 irq_exit();
206}
207
Paul Mundt173a44d2008-08-06 18:02:48 +0900208void smp_message_recv(unsigned int msg)
209{
210 switch (msg) {
211 case SMP_MSG_FUNCTION:
212 generic_smp_call_function_interrupt();
213 break;
214 case SMP_MSG_RESCHEDULE:
215 break;
216 case SMP_MSG_FUNCTION_SINGLE:
217 generic_smp_call_function_single_interrupt();
218 break;
Paul Mundt6f527072008-08-06 18:21:03 +0900219 case SMP_MSG_TIMER:
220 ipi_timer();
221 break;
Paul Mundt173a44d2008-08-06 18:02:48 +0900222 default:
223 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
224 smp_processor_id(), __func__, msg);
225 break;
226 }
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Not really SMP stuff ... */
230int setup_profiling_timer(unsigned int multiplier)
231{
232 return 0;
233}
234
Paul Mundt9964fa82007-09-21 18:09:55 +0900235static void flush_tlb_all_ipi(void *info)
236{
237 local_flush_tlb_all();
238}
239
240void flush_tlb_all(void)
241{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200242 on_each_cpu(flush_tlb_all_ipi, 0, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900243}
244
245static void flush_tlb_mm_ipi(void *mm)
246{
247 local_flush_tlb_mm((struct mm_struct *)mm);
248}
249
250/*
251 * The following tlb flush calls are invoked when old translations are
252 * being torn down, or pte attributes are changing. For single threaded
253 * address spaces, a new context is obtained on the current cpu, and tlb
254 * context on other cpus are invalidated to force a new context allocation
255 * at switch_mm time, should the mm ever be used on other cpus. For
256 * multithreaded address spaces, intercpu interrupts have to be sent.
257 * Another case where intercpu interrupts are required is when the target
258 * mm might be active on another cpu (eg debuggers doing the flushes on
259 * behalf of debugees, kswapd stealing pages from another process etc).
260 * Kanoj 07/00.
261 */
262
263void flush_tlb_mm(struct mm_struct *mm)
264{
265 preempt_disable();
266
267 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Jens Axboe8691e5a2008-06-06 11:18:06 +0200268 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900269 } else {
270 int i;
271 for (i = 0; i < num_online_cpus(); i++)
272 if (smp_processor_id() != i)
273 cpu_context(i, mm) = 0;
274 }
275 local_flush_tlb_mm(mm);
276
277 preempt_enable();
278}
279
280struct flush_tlb_data {
281 struct vm_area_struct *vma;
282 unsigned long addr1;
283 unsigned long addr2;
284};
285
286static void flush_tlb_range_ipi(void *info)
287{
288 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
289
290 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
291}
292
293void flush_tlb_range(struct vm_area_struct *vma,
294 unsigned long start, unsigned long end)
295{
296 struct mm_struct *mm = vma->vm_mm;
297
298 preempt_disable();
299 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
300 struct flush_tlb_data fd;
301
302 fd.vma = vma;
303 fd.addr1 = start;
304 fd.addr2 = end;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200305 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900306 } else {
307 int i;
308 for (i = 0; i < num_online_cpus(); i++)
309 if (smp_processor_id() != i)
310 cpu_context(i, mm) = 0;
311 }
312 local_flush_tlb_range(vma, start, end);
313 preempt_enable();
314}
315
316static void flush_tlb_kernel_range_ipi(void *info)
317{
318 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
319
320 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
321}
322
323void flush_tlb_kernel_range(unsigned long start, unsigned long end)
324{
325 struct flush_tlb_data fd;
326
327 fd.addr1 = start;
328 fd.addr2 = end;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200329 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900330}
331
332static void flush_tlb_page_ipi(void *info)
333{
334 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
335
336 local_flush_tlb_page(fd->vma, fd->addr1);
337}
338
339void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
340{
341 preempt_disable();
342 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
343 (current->mm != vma->vm_mm)) {
344 struct flush_tlb_data fd;
345
346 fd.vma = vma;
347 fd.addr1 = page;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200348 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900349 } else {
350 int i;
351 for (i = 0; i < num_online_cpus(); i++)
352 if (smp_processor_id() != i)
353 cpu_context(i, vma->vm_mm) = 0;
354 }
355 local_flush_tlb_page(vma, page);
356 preempt_enable();
357}
358
359static void flush_tlb_one_ipi(void *info)
360{
361 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
362 local_flush_tlb_one(fd->addr1, fd->addr2);
363}
364
365void flush_tlb_one(unsigned long asid, unsigned long vaddr)
366{
367 struct flush_tlb_data fd;
368
369 fd.addr1 = asid;
370 fd.addr2 = vaddr;
371
Jens Axboe8691e5a2008-06-06 11:18:06 +0200372 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900373 local_flush_tlb_one(asid, vaddr);
374}