blob: 3711a76a18e077d84f48cf35b188e546e68d4f31 [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 Mundtaba10302007-09-21 18:32:32 +0900128 tsk = fork_idle(cpu);
129 if (IS_ERR(tsk)) {
130 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
131 return PTR_ERR(tsk);
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Paul Mundt9715b8c2010-04-26 18:49:58 +0900134 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
135
Paul Mundtaba10302007-09-21 18:32:32 +0900136 /* Fill in data in head.S for secondary cpus */
137 stack_start.sp = tsk->thread.sp;
138 stack_start.thread_info = tsk->stack;
139 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
140 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Paul Mundtd7806132009-10-14 11:51:28 +0900142 flush_icache_range((unsigned long)&stack_start,
143 (unsigned long)&stack_start + sizeof(stack_start));
144 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Paul Mundt3366e352010-03-30 12:38:01 +0900146 mp_ops->start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Paul Mundtaba10302007-09-21 18:32:32 +0900148 timeout = jiffies + HZ;
149 while (time_before(jiffies, timeout)) {
150 if (cpu_online(cpu))
151 break;
152
153 udelay(10);
154 }
155
156 if (cpu_online(cpu))
157 return 0;
158
159 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162void __init smp_cpus_done(unsigned int max_cpus)
163{
Paul Mundtaba10302007-09-21 18:32:32 +0900164 unsigned long bogosum = 0;
165 int cpu;
166
167 for_each_online_cpu(cpu)
168 bogosum += cpu_data[cpu].loops_per_jiffy;
169
170 printk(KERN_INFO "SMP: Total of %d processors activated "
171 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
172 bogosum / (500000/HZ),
173 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176void smp_send_reschedule(int cpu)
177{
Paul Mundt3366e352010-03-30 12:38:01 +0900178 mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181void smp_send_stop(void)
182{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200183 smp_call_function(stop_this_cpu, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Rusty Russell819807d2009-06-12 22:32:35 +0930186void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Jens Axboe490f5de2008-06-10 20:52:59 +0200188 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Rusty Russell819807d2009-06-12 22:32:35 +0930190 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900191 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
Jens Axboe490f5de2008-06-10 20:52:59 +0200192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jens Axboe490f5de2008-06-10 20:52:59 +0200194void arch_send_call_function_single_ipi(int cpu)
195{
Paul Mundt3366e352010-03-30 12:38:01 +0900196 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Rusty Russell320ab2b2008-12-13 21:20:26 +1030199void smp_timer_broadcast(const struct cpumask *mask)
Paul Mundt6f527072008-08-06 18:21:03 +0900200{
201 int cpu;
202
Rusty Russell320ab2b2008-12-13 21:20:26 +1030203 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900204 mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
Paul Mundt6f527072008-08-06 18:21:03 +0900205}
206
207static void ipi_timer(void)
208{
209 irq_enter();
Paul Mundt8c245942008-08-06 18:37:07 +0900210 local_timer_interrupt();
Paul Mundt6f527072008-08-06 18:21:03 +0900211 irq_exit();
212}
213
Paul Mundt173a44d2008-08-06 18:02:48 +0900214void smp_message_recv(unsigned int msg)
215{
216 switch (msg) {
217 case SMP_MSG_FUNCTION:
218 generic_smp_call_function_interrupt();
219 break;
220 case SMP_MSG_RESCHEDULE:
221 break;
222 case SMP_MSG_FUNCTION_SINGLE:
223 generic_smp_call_function_single_interrupt();
224 break;
Paul Mundt6f527072008-08-06 18:21:03 +0900225 case SMP_MSG_TIMER:
226 ipi_timer();
227 break;
Paul Mundt173a44d2008-08-06 18:02:48 +0900228 default:
229 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
230 smp_processor_id(), __func__, msg);
231 break;
232 }
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/* Not really SMP stuff ... */
236int setup_profiling_timer(unsigned int multiplier)
237{
238 return 0;
239}
240
Paul Mundt9964fa82007-09-21 18:09:55 +0900241static void flush_tlb_all_ipi(void *info)
242{
243 local_flush_tlb_all();
244}
245
246void flush_tlb_all(void)
247{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200248 on_each_cpu(flush_tlb_all_ipi, 0, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900249}
250
251static void flush_tlb_mm_ipi(void *mm)
252{
253 local_flush_tlb_mm((struct mm_struct *)mm);
254}
255
256/*
257 * The following tlb flush calls are invoked when old translations are
258 * being torn down, or pte attributes are changing. For single threaded
259 * address spaces, a new context is obtained on the current cpu, and tlb
260 * context on other cpus are invalidated to force a new context allocation
261 * at switch_mm time, should the mm ever be used on other cpus. For
262 * multithreaded address spaces, intercpu interrupts have to be sent.
263 * Another case where intercpu interrupts are required is when the target
264 * mm might be active on another cpu (eg debuggers doing the flushes on
265 * behalf of debugees, kswapd stealing pages from another process etc).
266 * Kanoj 07/00.
267 */
268
269void flush_tlb_mm(struct mm_struct *mm)
270{
271 preempt_disable();
272
273 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Jens Axboe8691e5a2008-06-06 11:18:06 +0200274 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900275 } else {
276 int i;
277 for (i = 0; i < num_online_cpus(); i++)
278 if (smp_processor_id() != i)
279 cpu_context(i, mm) = 0;
280 }
281 local_flush_tlb_mm(mm);
282
283 preempt_enable();
284}
285
286struct flush_tlb_data {
287 struct vm_area_struct *vma;
288 unsigned long addr1;
289 unsigned long addr2;
290};
291
292static void flush_tlb_range_ipi(void *info)
293{
294 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
295
296 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
297}
298
299void flush_tlb_range(struct vm_area_struct *vma,
300 unsigned long start, unsigned long end)
301{
302 struct mm_struct *mm = vma->vm_mm;
303
304 preempt_disable();
305 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
306 struct flush_tlb_data fd;
307
308 fd.vma = vma;
309 fd.addr1 = start;
310 fd.addr2 = end;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200311 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900312 } else {
313 int i;
314 for (i = 0; i < num_online_cpus(); i++)
315 if (smp_processor_id() != i)
316 cpu_context(i, mm) = 0;
317 }
318 local_flush_tlb_range(vma, start, end);
319 preempt_enable();
320}
321
322static void flush_tlb_kernel_range_ipi(void *info)
323{
324 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
325
326 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
327}
328
329void flush_tlb_kernel_range(unsigned long start, unsigned long end)
330{
331 struct flush_tlb_data fd;
332
333 fd.addr1 = start;
334 fd.addr2 = end;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200335 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900336}
337
338static void flush_tlb_page_ipi(void *info)
339{
340 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
341
342 local_flush_tlb_page(fd->vma, fd->addr1);
343}
344
345void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
346{
347 preempt_disable();
348 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
349 (current->mm != vma->vm_mm)) {
350 struct flush_tlb_data fd;
351
352 fd.vma = vma;
353 fd.addr1 = page;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200354 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900355 } else {
356 int i;
357 for (i = 0; i < num_online_cpus(); i++)
358 if (smp_processor_id() != i)
359 cpu_context(i, vma->vm_mm) = 0;
360 }
361 local_flush_tlb_page(vma, page);
362 preempt_enable();
363}
364
365static void flush_tlb_one_ipi(void *info)
366{
367 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
368 local_flush_tlb_one(fd->addr1, fd->addr2);
369}
370
371void flush_tlb_one(unsigned long asid, unsigned long vaddr)
372{
373 struct flush_tlb_data fd;
374
375 fd.addr1 = asid;
376 fd.addr2 = vaddr;
377
Jens Axboe8691e5a2008-06-06 11:18:06 +0200378 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900379 local_flush_tlb_one(asid, vaddr);
380}