blob: 3c5ad1660bbcdd6b0366934167b3dc879cdb026d [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
34cpumask_t cpu_possible_map;
David S. Millerc8923c62005-10-13 14:41:23 -070035EXPORT_SYMBOL(cpu_possible_map);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037cpumask_t cpu_online_map;
Greg Bankse16b38f2006-10-02 02:17:40 -070038EXPORT_SYMBOL(cpu_online_map);
Paul Mundtaba10302007-09-21 18:32:32 +090039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static inline void __init smp_store_cpu_info(unsigned int cpu)
41{
Paul Mundtaba10302007-09-21 18:32:32 +090042 struct sh_cpuinfo *c = cpu_data + cpu;
43
44 c->loops_per_jiffy = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47void __init smp_prepare_cpus(unsigned int max_cpus)
48{
49 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Paul Mundtaba10302007-09-21 18:32:32 +090051 init_new_context(current, &init_mm);
52 current_thread_info()->cpu = cpu;
53 plat_prepare_cpus(max_cpus);
54
55#ifndef CONFIG_HOTPLUG_CPU
56 cpu_present_map = cpu_possible_map;
57#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60void __devinit smp_prepare_boot_cpu(void)
61{
62 unsigned int cpu = smp_processor_id();
63
Paul Mundtaba10302007-09-21 18:32:32 +090064 __cpu_number_map[0] = cpu;
65 __cpu_logical_map[0] = cpu;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 cpu_set(cpu, cpu_online_map);
68 cpu_set(cpu, cpu_possible_map);
69}
70
Paul Mundtaba10302007-09-21 18:32:32 +090071asmlinkage void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Paul Mundtaba10302007-09-21 18:32:32 +090073 unsigned int cpu;
74 struct mm_struct *mm = &init_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Paul Mundtaba10302007-09-21 18:32:32 +090076 atomic_inc(&mm->mm_count);
77 atomic_inc(&mm->mm_users);
78 current->active_mm = mm;
79 BUG_ON(current->mm);
80 enter_lazy_tlb(mm, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Paul Mundtaba10302007-09-21 18:32:32 +090082 per_cpu_trap_init();
83
84 preempt_disable();
85
Manfred Spraule545a612008-09-07 16:57:22 +020086 notify_cpu_starting(smp_processor_id());
87
Paul Mundtaba10302007-09-21 18:32:32 +090088 local_irq_enable();
89
Paul Mundt8c245942008-08-06 18:37:07 +090090 cpu = smp_processor_id();
91
92 /* Enable local timers */
93 local_timer_setup(cpu);
Paul Mundtaba10302007-09-21 18:32:32 +090094 calibrate_delay();
95
Paul Mundtaba10302007-09-21 18:32:32 +090096 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 cpu_set(cpu, cpu_online_map);
99
Paul Mundtaba10302007-09-21 18:32:32 +0900100 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Paul Mundtaba10302007-09-21 18:32:32 +0900103extern struct {
104 unsigned long sp;
105 unsigned long bss_start;
106 unsigned long bss_end;
107 void *start_kernel_fn;
108 void *cpu_init_fn;
109 void *thread_info;
110} stack_start;
111
112int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Paul Mundtaba10302007-09-21 18:32:32 +0900114 struct task_struct *tsk;
115 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800116
Paul Mundtaba10302007-09-21 18:32:32 +0900117 tsk = fork_idle(cpu);
118 if (IS_ERR(tsk)) {
119 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
120 return PTR_ERR(tsk);
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Paul Mundtaba10302007-09-21 18:32:32 +0900123 /* Fill in data in head.S for secondary cpus */
124 stack_start.sp = tsk->thread.sp;
125 stack_start.thread_info = tsk->stack;
126 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
127 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Paul Mundtaba10302007-09-21 18:32:32 +0900129 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Paul Mundtaba10302007-09-21 18:32:32 +0900131 plat_start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Paul Mundtaba10302007-09-21 18:32:32 +0900133 timeout = jiffies + HZ;
134 while (time_before(jiffies, timeout)) {
135 if (cpu_online(cpu))
136 break;
137
138 udelay(10);
139 }
140
141 if (cpu_online(cpu))
142 return 0;
143
144 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147void __init smp_cpus_done(unsigned int max_cpus)
148{
Paul Mundtaba10302007-09-21 18:32:32 +0900149 unsigned long bogosum = 0;
150 int cpu;
151
152 for_each_online_cpu(cpu)
153 bogosum += cpu_data[cpu].loops_per_jiffy;
154
155 printk(KERN_INFO "SMP: Total of %d processors activated "
156 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
157 bogosum / (500000/HZ),
158 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void smp_send_reschedule(int cpu)
162{
Paul Mundtaba10302007-09-21 18:32:32 +0900163 plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166static void stop_this_cpu(void *unused)
167{
168 cpu_clear(smp_processor_id(), cpu_online_map);
169 local_irq_disable();
170
171 for (;;)
172 cpu_relax();
173}
174
175void 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
Jens Axboe490f5de2008-06-10 20:52:59 +0200180void arch_send_call_function_ipi(cpumask_t 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
Jens Axboe490f5de2008-06-10 20:52:59 +0200184 for_each_cpu_mask(cpu, mask)
185 plat_send_ipi(cpu, SMP_MSG_FUNCTION);
186}
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{
190 plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Paul Mundt6f527072008-08-06 18:21:03 +0900193void smp_timer_broadcast(cpumask_t mask)
194{
195 int cpu;
196
197 for_each_cpu_mask(cpu, mask)
198 plat_send_ipi(cpu, SMP_MSG_TIMER);
199}
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}