blob: 21e7f8a9f3e47ff5aeee349391d3b11681e0f816 [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
Paul Mundt9715b8c2010-04-26 18:49:58 +090036/* State of each CPU */
37DEFINE_PER_CPU(int, cpu_state) = { 0 };
38
Paul Mundt3366e352010-03-30 12:38:01 +090039void __cpuinit register_smp_ops(struct plat_smp_ops *ops)
40{
41 if (mp_ops)
42 printk(KERN_WARNING "Overriding previously set SMP ops\n");
43
44 mp_ops = ops;
45}
46
Paul Mundt1cfa1e82010-04-26 18:55:01 +090047static inline void __cpuinit smp_store_cpu_info(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Paul Mundtaba10302007-09-21 18:32:32 +090049 struct sh_cpuinfo *c = cpu_data + cpu;
50
Paul Mundta66c2ed2009-10-14 14:14:30 +090051 memcpy(c, &boot_cpu_data, sizeof(struct sh_cpuinfo));
52
Paul Mundtaba10302007-09-21 18:32:32 +090053 c->loops_per_jiffy = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56void __init smp_prepare_cpus(unsigned int max_cpus)
57{
58 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paul Mundtaba10302007-09-21 18:32:32 +090060 init_new_context(current, &init_mm);
61 current_thread_info()->cpu = cpu;
Paul Mundt3366e352010-03-30 12:38:01 +090062 mp_ops->prepare_cpus(max_cpus);
Paul Mundtaba10302007-09-21 18:32:32 +090063
64#ifndef CONFIG_HOTPLUG_CPU
Rusty Russelle09377b2009-06-12 22:33:14 +093065 init_cpu_present(&cpu_possible_map);
Paul Mundtaba10302007-09-21 18:32:32 +090066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Paul Mundt1cfa1e82010-04-26 18:55:01 +090069void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 unsigned int cpu = smp_processor_id();
72
Paul Mundtaba10302007-09-21 18:32:32 +090073 __cpu_number_map[0] = cpu;
74 __cpu_logical_map[0] = cpu;
75
Rusty Russelle09377b2009-06-12 22:33:14 +093076 set_cpu_online(cpu, true);
77 set_cpu_possible(cpu, true);
Paul Mundt9715b8c2010-04-26 18:49:58 +090078
79 per_cpu(cpu_state, cpu) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Paul Mundtaba10302007-09-21 18:32:32 +090082asmlinkage void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Paul Mundt9715b8c2010-04-26 18:49:58 +090084 unsigned int cpu = smp_processor_id();
Paul Mundtaba10302007-09-21 18:32:32 +090085 struct mm_struct *mm = &init_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Matt Fleming4bea3412010-03-28 20:08:25 +000087 enable_mmu();
Paul Mundtaba10302007-09-21 18:32:32 +090088 atomic_inc(&mm->mm_count);
89 atomic_inc(&mm->mm_users);
90 current->active_mm = mm;
91 BUG_ON(current->mm);
92 enter_lazy_tlb(mm, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Paul Mundtaba10302007-09-21 18:32:32 +090094 per_cpu_trap_init();
95
96 preempt_disable();
97
Paul Mundt9715b8c2010-04-26 18:49:58 +090098 notify_cpu_starting(cpu);
Manfred Spraule545a612008-09-07 16:57:22 +020099
Paul Mundtaba10302007-09-21 18:32:32 +0900100 local_irq_enable();
101
Paul Mundt8c245942008-08-06 18:37:07 +0900102 /* Enable local timers */
103 local_timer_setup(cpu);
Paul Mundtaba10302007-09-21 18:32:32 +0900104 calibrate_delay();
105
Paul Mundtaba10302007-09-21 18:32:32 +0900106 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Paul Mundtf0ccf272010-04-26 18:39:50 +0900108 set_cpu_online(cpu, true);
Paul Mundt9715b8c2010-04-26 18:49:58 +0900109 per_cpu(cpu_state, cpu) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Paul Mundtaba10302007-09-21 18:32:32 +0900111 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Paul Mundtaba10302007-09-21 18:32:32 +0900114extern struct {
115 unsigned long sp;
116 unsigned long bss_start;
117 unsigned long bss_end;
118 void *start_kernel_fn;
119 void *cpu_init_fn;
120 void *thread_info;
121} stack_start;
122
123int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Paul Mundtaba10302007-09-21 18:32:32 +0900125 struct task_struct *tsk;
126 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800127
Paul Mundt8db2bc42010-04-26 18:59:47 +0900128 tsk = cpu_data[cpu].idle;
129 if (!tsk) {
130 tsk = fork_idle(cpu);
131 if (IS_ERR(tsk)) {
132 pr_err("Failed forking idle task for cpu %d\n", cpu);
133 return PTR_ERR(tsk);
134 }
135
136 cpu_data[cpu].idle = tsk;
Paul Mundtaba10302007-09-21 18:32:32 +0900137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Paul Mundt9715b8c2010-04-26 18:49:58 +0900139 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
140
Paul Mundtaba10302007-09-21 18:32:32 +0900141 /* Fill in data in head.S for secondary cpus */
142 stack_start.sp = tsk->thread.sp;
143 stack_start.thread_info = tsk->stack;
144 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
145 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Paul Mundtd7806132009-10-14 11:51:28 +0900147 flush_icache_range((unsigned long)&stack_start,
148 (unsigned long)&stack_start + sizeof(stack_start));
149 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Paul Mundt3366e352010-03-30 12:38:01 +0900151 mp_ops->start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Paul Mundtaba10302007-09-21 18:32:32 +0900153 timeout = jiffies + HZ;
154 while (time_before(jiffies, timeout)) {
155 if (cpu_online(cpu))
156 break;
157
158 udelay(10);
159 }
160
161 if (cpu_online(cpu))
162 return 0;
163
164 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167void __init smp_cpus_done(unsigned int max_cpus)
168{
Paul Mundtaba10302007-09-21 18:32:32 +0900169 unsigned long bogosum = 0;
170 int cpu;
171
172 for_each_online_cpu(cpu)
173 bogosum += cpu_data[cpu].loops_per_jiffy;
174
175 printk(KERN_INFO "SMP: Total of %d processors activated "
176 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
177 bogosum / (500000/HZ),
178 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181void smp_send_reschedule(int cpu)
182{
Paul Mundt3366e352010-03-30 12:38:01 +0900183 mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186void smp_send_stop(void)
187{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200188 smp_call_function(stop_this_cpu, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Rusty Russell819807d2009-06-12 22:32:35 +0930191void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Jens Axboe490f5de2008-06-10 20:52:59 +0200193 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Rusty Russell819807d2009-06-12 22:32:35 +0930195 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900196 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
Jens Axboe490f5de2008-06-10 20:52:59 +0200197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jens Axboe490f5de2008-06-10 20:52:59 +0200199void arch_send_call_function_single_ipi(int cpu)
200{
Paul Mundt3366e352010-03-30 12:38:01 +0900201 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Rusty Russell320ab2b2008-12-13 21:20:26 +1030204void smp_timer_broadcast(const struct cpumask *mask)
Paul Mundt6f527072008-08-06 18:21:03 +0900205{
206 int cpu;
207
Rusty Russell320ab2b2008-12-13 21:20:26 +1030208 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900209 mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
Paul Mundt6f527072008-08-06 18:21:03 +0900210}
211
212static void ipi_timer(void)
213{
214 irq_enter();
Paul Mundt8c245942008-08-06 18:37:07 +0900215 local_timer_interrupt();
Paul Mundt6f527072008-08-06 18:21:03 +0900216 irq_exit();
217}
218
Paul Mundt173a44d2008-08-06 18:02:48 +0900219void smp_message_recv(unsigned int msg)
220{
221 switch (msg) {
222 case SMP_MSG_FUNCTION:
223 generic_smp_call_function_interrupt();
224 break;
225 case SMP_MSG_RESCHEDULE:
226 break;
227 case SMP_MSG_FUNCTION_SINGLE:
228 generic_smp_call_function_single_interrupt();
229 break;
Paul Mundt6f527072008-08-06 18:21:03 +0900230 case SMP_MSG_TIMER:
231 ipi_timer();
232 break;
Paul Mundt173a44d2008-08-06 18:02:48 +0900233 default:
234 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
235 smp_processor_id(), __func__, msg);
236 break;
237 }
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/* Not really SMP stuff ... */
241int setup_profiling_timer(unsigned int multiplier)
242{
243 return 0;
244}
245
Paul Mundt9964fa82007-09-21 18:09:55 +0900246static void flush_tlb_all_ipi(void *info)
247{
248 local_flush_tlb_all();
249}
250
251void flush_tlb_all(void)
252{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200253 on_each_cpu(flush_tlb_all_ipi, 0, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900254}
255
256static void flush_tlb_mm_ipi(void *mm)
257{
258 local_flush_tlb_mm((struct mm_struct *)mm);
259}
260
261/*
262 * The following tlb flush calls are invoked when old translations are
263 * being torn down, or pte attributes are changing. For single threaded
264 * address spaces, a new context is obtained on the current cpu, and tlb
265 * context on other cpus are invalidated to force a new context allocation
266 * at switch_mm time, should the mm ever be used on other cpus. For
267 * multithreaded address spaces, intercpu interrupts have to be sent.
268 * Another case where intercpu interrupts are required is when the target
269 * mm might be active on another cpu (eg debuggers doing the flushes on
270 * behalf of debugees, kswapd stealing pages from another process etc).
271 * Kanoj 07/00.
272 */
273
274void flush_tlb_mm(struct mm_struct *mm)
275{
276 preempt_disable();
277
278 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Jens Axboe8691e5a2008-06-06 11:18:06 +0200279 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900280 } else {
281 int i;
282 for (i = 0; i < num_online_cpus(); i++)
283 if (smp_processor_id() != i)
284 cpu_context(i, mm) = 0;
285 }
286 local_flush_tlb_mm(mm);
287
288 preempt_enable();
289}
290
291struct flush_tlb_data {
292 struct vm_area_struct *vma;
293 unsigned long addr1;
294 unsigned long addr2;
295};
296
297static void flush_tlb_range_ipi(void *info)
298{
299 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
300
301 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
302}
303
304void flush_tlb_range(struct vm_area_struct *vma,
305 unsigned long start, unsigned long end)
306{
307 struct mm_struct *mm = vma->vm_mm;
308
309 preempt_disable();
310 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
311 struct flush_tlb_data fd;
312
313 fd.vma = vma;
314 fd.addr1 = start;
315 fd.addr2 = end;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200316 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900317 } else {
318 int i;
319 for (i = 0; i < num_online_cpus(); i++)
320 if (smp_processor_id() != i)
321 cpu_context(i, mm) = 0;
322 }
323 local_flush_tlb_range(vma, start, end);
324 preempt_enable();
325}
326
327static void flush_tlb_kernel_range_ipi(void *info)
328{
329 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
330
331 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
332}
333
334void flush_tlb_kernel_range(unsigned long start, unsigned long end)
335{
336 struct flush_tlb_data fd;
337
338 fd.addr1 = start;
339 fd.addr2 = end;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200340 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900341}
342
343static void flush_tlb_page_ipi(void *info)
344{
345 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
346
347 local_flush_tlb_page(fd->vma, fd->addr1);
348}
349
350void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
351{
352 preempt_disable();
353 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
354 (current->mm != vma->vm_mm)) {
355 struct flush_tlb_data fd;
356
357 fd.vma = vma;
358 fd.addr1 = page;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200359 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900360 } else {
361 int i;
362 for (i = 0; i < num_online_cpus(); i++)
363 if (smp_processor_id() != i)
364 cpu_context(i, vma->vm_mm) = 0;
365 }
366 local_flush_tlb_page(vma, page);
367 preempt_enable();
368}
369
370static void flush_tlb_one_ipi(void *info)
371{
372 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
373 local_flush_tlb_one(fd->addr1, fd->addr2);
374}
375
376void flush_tlb_one(unsigned long asid, unsigned long vaddr)
377{
378 struct flush_tlb_data fd;
379
380 fd.addr1 = asid;
381 fd.addr2 = vaddr;
382
Jens Axboe8691e5a2008-06-06 11:18:06 +0200383 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900384 local_flush_tlb_one(asid, vaddr);
385}