blob: 5d039d168f57a4630336e555073d4767246016c1 [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 Mundtaba10302007-09-21 18:32:32 +09006 * Copyright (C) 2002 - 2007 Paul Mundt
7 * 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 Mundtaba10302007-09-21 18:32:32 +090021#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/atomic.h>
23#include <asm/processor.h>
24#include <asm/system.h>
25#include <asm/mmu_context.h>
26#include <asm/smp.h>
Paul Mundtaba10302007-09-21 18:32:32 +090027#include <asm/cacheflush.h>
28#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Paul Mundtaba10302007-09-21 18:32:32 +090030int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
31int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33cpumask_t cpu_possible_map;
David S. Millerc8923c62005-10-13 14:41:23 -070034EXPORT_SYMBOL(cpu_possible_map);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036cpumask_t cpu_online_map;
Greg Bankse16b38f2006-10-02 02:17:40 -070037EXPORT_SYMBOL(cpu_online_map);
Paul Mundtaba10302007-09-21 18:32:32 +090038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static atomic_t cpus_booted = ATOMIC_INIT(0);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * Run specified function on a particular processor.
43 */
44void __smp_call_function(unsigned int cpu);
45
46static inline void __init smp_store_cpu_info(unsigned int cpu)
47{
Paul Mundtaba10302007-09-21 18:32:32 +090048 struct sh_cpuinfo *c = cpu_data + cpu;
49
50 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;
59 plat_prepare_cpus(max_cpus);
60
61#ifndef CONFIG_HOTPLUG_CPU
62 cpu_present_map = cpu_possible_map;
63#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 cpu_set(cpu, cpu_online_map);
74 cpu_set(cpu, cpu_possible_map);
75}
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
Paul Mundtaba10302007-09-21 18:32:32 +090082 atomic_inc(&mm->mm_count);
83 atomic_inc(&mm->mm_users);
84 current->active_mm = mm;
85 BUG_ON(current->mm);
86 enter_lazy_tlb(mm, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Paul Mundtaba10302007-09-21 18:32:32 +090088 per_cpu_trap_init();
89
90 preempt_disable();
91
92 local_irq_enable();
93
94 calibrate_delay();
95
96 cpu = smp_processor_id();
97 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 cpu_set(cpu, cpu_online_map);
100
Paul Mundtaba10302007-09-21 18:32:32 +0900101 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Paul Mundtaba10302007-09-21 18:32:32 +0900104extern struct {
105 unsigned long sp;
106 unsigned long bss_start;
107 unsigned long bss_end;
108 void *start_kernel_fn;
109 void *cpu_init_fn;
110 void *thread_info;
111} stack_start;
112
113int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Paul Mundtaba10302007-09-21 18:32:32 +0900115 struct task_struct *tsk;
116 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800117
Paul Mundtaba10302007-09-21 18:32:32 +0900118 tsk = fork_idle(cpu);
119 if (IS_ERR(tsk)) {
120 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
121 return PTR_ERR(tsk);
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Paul Mundtaba10302007-09-21 18:32:32 +0900124 /* Fill in data in head.S for secondary cpus */
125 stack_start.sp = tsk->thread.sp;
126 stack_start.thread_info = tsk->stack;
127 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
128 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Paul Mundtaba10302007-09-21 18:32:32 +0900130 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Paul Mundtaba10302007-09-21 18:32:32 +0900132 plat_start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Paul Mundtaba10302007-09-21 18:32:32 +0900134 timeout = jiffies + HZ;
135 while (time_before(jiffies, timeout)) {
136 if (cpu_online(cpu))
137 break;
138
139 udelay(10);
140 }
141
142 if (cpu_online(cpu))
143 return 0;
144
145 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148void __init smp_cpus_done(unsigned int max_cpus)
149{
Paul Mundtaba10302007-09-21 18:32:32 +0900150 unsigned long bogosum = 0;
151 int cpu;
152
153 for_each_online_cpu(cpu)
154 bogosum += cpu_data[cpu].loops_per_jiffy;
155
156 printk(KERN_INFO "SMP: Total of %d processors activated "
157 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
158 bogosum / (500000/HZ),
159 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162void smp_send_reschedule(int cpu)
163{
Paul Mundtaba10302007-09-21 18:32:32 +0900164 plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static void stop_this_cpu(void *unused)
168{
169 cpu_clear(smp_processor_id(), cpu_online_map);
170 local_irq_disable();
171
172 for (;;)
173 cpu_relax();
174}
175
176void smp_send_stop(void)
177{
178 smp_call_function(stop_this_cpu, 0, 1, 0);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181struct smp_fn_call_struct smp_fn_call = {
Robert P. J. Day40f75872008-03-15 12:49:10 -0400182 .lock = __SPIN_LOCK_UNLOCKED(smp_fn_call.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .finished = ATOMIC_INIT(0),
184};
185
186/*
187 * The caller of this wants the passed function to run on every cpu. If wait
188 * is set, wait until all cpus have finished the function before returning.
189 * The lock is here to protect the call structure.
190 * You must not call this function with disabled interrupts or from a
191 * hardware interrupt handler or from a bottom half handler.
192 */
193int smp_call_function(void (*func)(void *info), void *info, int retry, int wait)
194{
195 unsigned int nr_cpus = atomic_read(&cpus_booted);
196 int i;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Can deadlock when called with interrupts disabled */
199 WARN_ON(irqs_disabled());
200
201 spin_lock(&smp_fn_call.lock);
202
203 atomic_set(&smp_fn_call.finished, 0);
204 smp_fn_call.fn = func;
205 smp_fn_call.data = info;
206
207 for (i = 0; i < nr_cpus; i++)
208 if (i != smp_processor_id())
Paul Mundtaba10302007-09-21 18:32:32 +0900209 plat_send_ipi(i, SMP_MSG_FUNCTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 if (wait)
212 while (atomic_read(&smp_fn_call.finished) != (nr_cpus - 1));
213
214 spin_unlock(&smp_fn_call.lock);
215
216 return 0;
217}
218
219/* Not really SMP stuff ... */
220int setup_profiling_timer(unsigned int multiplier)
221{
222 return 0;
223}
224
Paul Mundt9964fa82007-09-21 18:09:55 +0900225static void flush_tlb_all_ipi(void *info)
226{
227 local_flush_tlb_all();
228}
229
230void flush_tlb_all(void)
231{
232 on_each_cpu(flush_tlb_all_ipi, 0, 1, 1);
233}
234
235static void flush_tlb_mm_ipi(void *mm)
236{
237 local_flush_tlb_mm((struct mm_struct *)mm);
238}
239
240/*
241 * The following tlb flush calls are invoked when old translations are
242 * being torn down, or pte attributes are changing. For single threaded
243 * address spaces, a new context is obtained on the current cpu, and tlb
244 * context on other cpus are invalidated to force a new context allocation
245 * at switch_mm time, should the mm ever be used on other cpus. For
246 * multithreaded address spaces, intercpu interrupts have to be sent.
247 * Another case where intercpu interrupts are required is when the target
248 * mm might be active on another cpu (eg debuggers doing the flushes on
249 * behalf of debugees, kswapd stealing pages from another process etc).
250 * Kanoj 07/00.
251 */
252
253void flush_tlb_mm(struct mm_struct *mm)
254{
255 preempt_disable();
256
257 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
258 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1, 1);
259 } else {
260 int i;
261 for (i = 0; i < num_online_cpus(); i++)
262 if (smp_processor_id() != i)
263 cpu_context(i, mm) = 0;
264 }
265 local_flush_tlb_mm(mm);
266
267 preempt_enable();
268}
269
270struct flush_tlb_data {
271 struct vm_area_struct *vma;
272 unsigned long addr1;
273 unsigned long addr2;
274};
275
276static void flush_tlb_range_ipi(void *info)
277{
278 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
279
280 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
281}
282
283void flush_tlb_range(struct vm_area_struct *vma,
284 unsigned long start, unsigned long end)
285{
286 struct mm_struct *mm = vma->vm_mm;
287
288 preempt_disable();
289 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
290 struct flush_tlb_data fd;
291
292 fd.vma = vma;
293 fd.addr1 = start;
294 fd.addr2 = end;
295 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1, 1);
296 } else {
297 int i;
298 for (i = 0; i < num_online_cpus(); i++)
299 if (smp_processor_id() != i)
300 cpu_context(i, mm) = 0;
301 }
302 local_flush_tlb_range(vma, start, end);
303 preempt_enable();
304}
305
306static void flush_tlb_kernel_range_ipi(void *info)
307{
308 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
309
310 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
311}
312
313void flush_tlb_kernel_range(unsigned long start, unsigned long end)
314{
315 struct flush_tlb_data fd;
316
317 fd.addr1 = start;
318 fd.addr2 = end;
319 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1, 1);
320}
321
322static void flush_tlb_page_ipi(void *info)
323{
324 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
325
326 local_flush_tlb_page(fd->vma, fd->addr1);
327}
328
329void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
330{
331 preempt_disable();
332 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
333 (current->mm != vma->vm_mm)) {
334 struct flush_tlb_data fd;
335
336 fd.vma = vma;
337 fd.addr1 = page;
338 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1, 1);
339 } else {
340 int i;
341 for (i = 0; i < num_online_cpus(); i++)
342 if (smp_processor_id() != i)
343 cpu_context(i, vma->vm_mm) = 0;
344 }
345 local_flush_tlb_page(vma, page);
346 preempt_enable();
347}
348
349static void flush_tlb_one_ipi(void *info)
350{
351 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
352 local_flush_tlb_one(fd->addr1, fd->addr2);
353}
354
355void flush_tlb_one(unsigned long asid, unsigned long vaddr)
356{
357 struct flush_tlb_data fd;
358
359 fd.addr1 = asid;
360 fd.addr2 = vaddr;
361
362 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1, 1);
363 local_flush_tlb_one(asid, vaddr);
364}