blob: 6207561ea34a93ca315589469b3a77257711254f [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>
Peter Zijlstra184748c2011-04-05 17:23:39 +020023#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/atomic.h>
25#include <asm/processor.h>
26#include <asm/system.h>
27#include <asm/mmu_context.h>
28#include <asm/smp.h>
Paul Mundtaba10302007-09-21 18:32:32 +090029#include <asm/cacheflush.h>
30#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Paul Mundtaba10302007-09-21 18:32:32 +090032int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
33int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Paul Mundt3366e352010-03-30 12:38:01 +090035struct plat_smp_ops *mp_ops = NULL;
36
Paul Mundt9715b8c2010-04-26 18:49:58 +090037/* State of each CPU */
38DEFINE_PER_CPU(int, cpu_state) = { 0 };
39
Paul Mundt3366e352010-03-30 12:38:01 +090040void __cpuinit register_smp_ops(struct plat_smp_ops *ops)
41{
42 if (mp_ops)
43 printk(KERN_WARNING "Overriding previously set SMP ops\n");
44
45 mp_ops = ops;
46}
47
Paul Mundt1cfa1e82010-04-26 18:55:01 +090048static inline void __cpuinit smp_store_cpu_info(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Paul Mundtaba10302007-09-21 18:32:32 +090050 struct sh_cpuinfo *c = cpu_data + cpu;
51
Paul Mundta66c2ed2009-10-14 14:14:30 +090052 memcpy(c, &boot_cpu_data, sizeof(struct sh_cpuinfo));
53
Paul Mundtaba10302007-09-21 18:32:32 +090054 c->loops_per_jiffy = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57void __init smp_prepare_cpus(unsigned int max_cpus)
58{
59 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Paul Mundtaba10302007-09-21 18:32:32 +090061 init_new_context(current, &init_mm);
62 current_thread_info()->cpu = cpu;
Paul Mundt3366e352010-03-30 12:38:01 +090063 mp_ops->prepare_cpus(max_cpus);
Paul Mundtaba10302007-09-21 18:32:32 +090064
65#ifndef CONFIG_HOTPLUG_CPU
Rusty Russelle09377b2009-06-12 22:33:14 +093066 init_cpu_present(&cpu_possible_map);
Paul Mundtaba10302007-09-21 18:32:32 +090067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Paul Mundt1cfa1e82010-04-26 18:55:01 +090070void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 unsigned int cpu = smp_processor_id();
73
Paul Mundtaba10302007-09-21 18:32:32 +090074 __cpu_number_map[0] = cpu;
75 __cpu_logical_map[0] = cpu;
76
Rusty Russelle09377b2009-06-12 22:33:14 +093077 set_cpu_online(cpu, true);
78 set_cpu_possible(cpu, true);
Paul Mundt9715b8c2010-04-26 18:49:58 +090079
80 per_cpu(cpu_state, cpu) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Paul Mundt763142d2010-04-26 19:08:55 +090083#ifdef CONFIG_HOTPLUG_CPU
84void native_cpu_die(unsigned int cpu)
85{
86 unsigned int i;
87
88 for (i = 0; i < 10; i++) {
89 smp_rmb();
90 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
91 if (system_state == SYSTEM_RUNNING)
92 pr_info("CPU %u is now offline\n", cpu);
93
94 return;
95 }
96
97 msleep(100);
98 }
99
100 pr_err("CPU %u didn't die...\n", cpu);
101}
102
103int native_cpu_disable(unsigned int cpu)
104{
105 return cpu == 0 ? -EPERM : 0;
106}
107
108void play_dead_common(void)
109{
110 idle_task_exit();
111 irq_ctx_exit(raw_smp_processor_id());
112 mb();
113
114 __get_cpu_var(cpu_state) = CPU_DEAD;
115 local_irq_disable();
116}
117
118void native_play_dead(void)
119{
120 play_dead_common();
121}
122
123int __cpu_disable(void)
124{
125 unsigned int cpu = smp_processor_id();
126 struct task_struct *p;
127 int ret;
128
129 ret = mp_ops->cpu_disable(cpu);
130 if (ret)
131 return ret;
132
133 /*
134 * Take this CPU offline. Once we clear this, we can't return,
135 * and we must not schedule until we're ready to give up the cpu.
136 */
137 set_cpu_online(cpu, false);
138
139 /*
140 * OK - migrate IRQs away from this CPU
141 */
142 migrate_irqs();
143
144 /*
145 * Stop the local timer for this CPU.
146 */
147 local_timer_stop(cpu);
148
149 /*
150 * Flush user cache and TLB mappings, and then remove this CPU
151 * from the vm mask set of all processes.
152 */
153 flush_cache_all();
154 local_flush_tlb_all();
155
156 read_lock(&tasklist_lock);
157 for_each_process(p)
158 if (p->mm)
159 cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
160 read_unlock(&tasklist_lock);
161
162 return 0;
163}
164#else /* ... !CONFIG_HOTPLUG_CPU */
Matt Fleming1483fea2010-04-28 11:11:30 +0000165int native_cpu_disable(unsigned int cpu)
Paul Mundt763142d2010-04-26 19:08:55 +0900166{
167 return -ENOSYS;
168}
169
170void native_cpu_die(unsigned int cpu)
171{
172 /* We said "no" in __cpu_disable */
173 BUG();
174}
175
176void native_play_dead(void)
177{
178 BUG();
179}
180#endif
181
Paul Mundtaba10302007-09-21 18:32:32 +0900182asmlinkage void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Paul Mundt9715b8c2010-04-26 18:49:58 +0900184 unsigned int cpu = smp_processor_id();
Paul Mundtaba10302007-09-21 18:32:32 +0900185 struct mm_struct *mm = &init_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Matt Fleming4bea3412010-03-28 20:08:25 +0000187 enable_mmu();
Paul Mundtaba10302007-09-21 18:32:32 +0900188 atomic_inc(&mm->mm_count);
189 atomic_inc(&mm->mm_users);
190 current->active_mm = mm;
Paul Mundtaba10302007-09-21 18:32:32 +0900191 enter_lazy_tlb(mm, current);
Paul Mundt763142d2010-04-26 19:08:55 +0900192 local_flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Paul Mundtaba10302007-09-21 18:32:32 +0900194 per_cpu_trap_init();
195
196 preempt_disable();
197
Paul Mundt9715b8c2010-04-26 18:49:58 +0900198 notify_cpu_starting(cpu);
Manfred Spraule545a612008-09-07 16:57:22 +0200199
Paul Mundtaba10302007-09-21 18:32:32 +0900200 local_irq_enable();
201
Paul Mundt8c245942008-08-06 18:37:07 +0900202 /* Enable local timers */
203 local_timer_setup(cpu);
Paul Mundtaba10302007-09-21 18:32:32 +0900204 calibrate_delay();
205
Paul Mundtaba10302007-09-21 18:32:32 +0900206 smp_store_cpu_info(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Paul Mundtf0ccf272010-04-26 18:39:50 +0900208 set_cpu_online(cpu, true);
Paul Mundt9715b8c2010-04-26 18:49:58 +0900209 per_cpu(cpu_state, cpu) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Paul Mundtaba10302007-09-21 18:32:32 +0900211 cpu_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Paul Mundtaba10302007-09-21 18:32:32 +0900214extern struct {
215 unsigned long sp;
216 unsigned long bss_start;
217 unsigned long bss_end;
218 void *start_kernel_fn;
219 void *cpu_init_fn;
220 void *thread_info;
221} stack_start;
222
223int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Paul Mundtaba10302007-09-21 18:32:32 +0900225 struct task_struct *tsk;
226 unsigned long timeout;
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800227
Paul Mundt8db2bc42010-04-26 18:59:47 +0900228 tsk = cpu_data[cpu].idle;
229 if (!tsk) {
230 tsk = fork_idle(cpu);
231 if (IS_ERR(tsk)) {
232 pr_err("Failed forking idle task for cpu %d\n", cpu);
233 return PTR_ERR(tsk);
234 }
235
236 cpu_data[cpu].idle = tsk;
Paul Mundtaba10302007-09-21 18:32:32 +0900237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Paul Mundt9715b8c2010-04-26 18:49:58 +0900239 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
240
Paul Mundtaba10302007-09-21 18:32:32 +0900241 /* Fill in data in head.S for secondary cpus */
242 stack_start.sp = tsk->thread.sp;
243 stack_start.thread_info = tsk->stack;
244 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
245 stack_start.start_kernel_fn = start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Paul Mundtd7806132009-10-14 11:51:28 +0900247 flush_icache_range((unsigned long)&stack_start,
248 (unsigned long)&stack_start + sizeof(stack_start));
249 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Paul Mundt3366e352010-03-30 12:38:01 +0900251 mp_ops->start_cpu(cpu, (unsigned long)_stext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Paul Mundtaba10302007-09-21 18:32:32 +0900253 timeout = jiffies + HZ;
254 while (time_before(jiffies, timeout)) {
255 if (cpu_online(cpu))
256 break;
257
258 udelay(10);
Paul Mundt763142d2010-04-26 19:08:55 +0900259 barrier();
Paul Mundtaba10302007-09-21 18:32:32 +0900260 }
261
262 if (cpu_online(cpu))
263 return 0;
264
265 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268void __init smp_cpus_done(unsigned int max_cpus)
269{
Paul Mundtaba10302007-09-21 18:32:32 +0900270 unsigned long bogosum = 0;
271 int cpu;
272
273 for_each_online_cpu(cpu)
274 bogosum += cpu_data[cpu].loops_per_jiffy;
275
276 printk(KERN_INFO "SMP: Total of %d processors activated "
277 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
278 bogosum / (500000/HZ),
279 (bogosum / (5000/HZ)) % 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282void smp_send_reschedule(int cpu)
283{
Paul Mundt3366e352010-03-30 12:38:01 +0900284 mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287void smp_send_stop(void)
288{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200289 smp_call_function(stop_this_cpu, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Rusty Russell819807d2009-06-12 22:32:35 +0930292void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Jens Axboe490f5de2008-06-10 20:52:59 +0200294 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Rusty Russell819807d2009-06-12 22:32:35 +0930296 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900297 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
Jens Axboe490f5de2008-06-10 20:52:59 +0200298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Jens Axboe490f5de2008-06-10 20:52:59 +0200300void arch_send_call_function_single_ipi(int cpu)
301{
Paul Mundt3366e352010-03-30 12:38:01 +0900302 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Rusty Russell320ab2b2008-12-13 21:20:26 +1030305void smp_timer_broadcast(const struct cpumask *mask)
Paul Mundt6f527072008-08-06 18:21:03 +0900306{
307 int cpu;
308
Rusty Russell320ab2b2008-12-13 21:20:26 +1030309 for_each_cpu(cpu, mask)
Paul Mundt3366e352010-03-30 12:38:01 +0900310 mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
Paul Mundt6f527072008-08-06 18:21:03 +0900311}
312
313static void ipi_timer(void)
314{
315 irq_enter();
Paul Mundt8c245942008-08-06 18:37:07 +0900316 local_timer_interrupt();
Paul Mundt6f527072008-08-06 18:21:03 +0900317 irq_exit();
318}
319
Paul Mundt173a44d2008-08-06 18:02:48 +0900320void smp_message_recv(unsigned int msg)
321{
322 switch (msg) {
323 case SMP_MSG_FUNCTION:
324 generic_smp_call_function_interrupt();
325 break;
326 case SMP_MSG_RESCHEDULE:
Peter Zijlstra184748c2011-04-05 17:23:39 +0200327 scheduler_ipi();
Paul Mundt173a44d2008-08-06 18:02:48 +0900328 break;
329 case SMP_MSG_FUNCTION_SINGLE:
330 generic_smp_call_function_single_interrupt();
331 break;
Paul Mundt6f527072008-08-06 18:21:03 +0900332 case SMP_MSG_TIMER:
333 ipi_timer();
334 break;
Paul Mundt173a44d2008-08-06 18:02:48 +0900335 default:
336 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
337 smp_processor_id(), __func__, msg);
338 break;
339 }
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* Not really SMP stuff ... */
343int setup_profiling_timer(unsigned int multiplier)
344{
345 return 0;
346}
347
Paul Mundt9964fa82007-09-21 18:09:55 +0900348static void flush_tlb_all_ipi(void *info)
349{
350 local_flush_tlb_all();
351}
352
353void flush_tlb_all(void)
354{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200355 on_each_cpu(flush_tlb_all_ipi, 0, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900356}
357
358static void flush_tlb_mm_ipi(void *mm)
359{
360 local_flush_tlb_mm((struct mm_struct *)mm);
361}
362
363/*
364 * The following tlb flush calls are invoked when old translations are
365 * being torn down, or pte attributes are changing. For single threaded
366 * address spaces, a new context is obtained on the current cpu, and tlb
367 * context on other cpus are invalidated to force a new context allocation
368 * at switch_mm time, should the mm ever be used on other cpus. For
369 * multithreaded address spaces, intercpu interrupts have to be sent.
370 * Another case where intercpu interrupts are required is when the target
371 * mm might be active on another cpu (eg debuggers doing the flushes on
372 * behalf of debugees, kswapd stealing pages from another process etc).
373 * Kanoj 07/00.
374 */
Paul Mundt9964fa82007-09-21 18:09:55 +0900375void flush_tlb_mm(struct mm_struct *mm)
376{
377 preempt_disable();
378
379 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Jens Axboe8691e5a2008-06-06 11:18:06 +0200380 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900381 } else {
382 int i;
383 for (i = 0; i < num_online_cpus(); i++)
384 if (smp_processor_id() != i)
385 cpu_context(i, mm) = 0;
386 }
387 local_flush_tlb_mm(mm);
388
389 preempt_enable();
390}
391
392struct flush_tlb_data {
393 struct vm_area_struct *vma;
394 unsigned long addr1;
395 unsigned long addr2;
396};
397
398static void flush_tlb_range_ipi(void *info)
399{
400 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
401
402 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
403}
404
405void flush_tlb_range(struct vm_area_struct *vma,
406 unsigned long start, unsigned long end)
407{
408 struct mm_struct *mm = vma->vm_mm;
409
410 preempt_disable();
411 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
412 struct flush_tlb_data fd;
413
414 fd.vma = vma;
415 fd.addr1 = start;
416 fd.addr2 = end;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200417 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900418 } else {
419 int i;
420 for (i = 0; i < num_online_cpus(); i++)
421 if (smp_processor_id() != i)
422 cpu_context(i, mm) = 0;
423 }
424 local_flush_tlb_range(vma, start, end);
425 preempt_enable();
426}
427
428static void flush_tlb_kernel_range_ipi(void *info)
429{
430 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
431
432 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
433}
434
435void flush_tlb_kernel_range(unsigned long start, unsigned long end)
436{
437 struct flush_tlb_data fd;
438
439 fd.addr1 = start;
440 fd.addr2 = end;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200441 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900442}
443
444static void flush_tlb_page_ipi(void *info)
445{
446 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
447
448 local_flush_tlb_page(fd->vma, fd->addr1);
449}
450
451void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
452{
453 preempt_disable();
454 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
455 (current->mm != vma->vm_mm)) {
456 struct flush_tlb_data fd;
457
458 fd.vma = vma;
459 fd.addr1 = page;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200460 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900461 } else {
462 int i;
463 for (i = 0; i < num_online_cpus(); i++)
464 if (smp_processor_id() != i)
465 cpu_context(i, vma->vm_mm) = 0;
466 }
467 local_flush_tlb_page(vma, page);
468 preempt_enable();
469}
470
471static void flush_tlb_one_ipi(void *info)
472{
473 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
474 local_flush_tlb_one(fd->addr1, fd->addr2);
475}
476
477void flush_tlb_one(unsigned long asid, unsigned long vaddr)
478{
479 struct flush_tlb_data fd;
480
481 fd.addr1 = asid;
482 fd.addr2 = vaddr;
483
Jens Axboe8691e5a2008-06-06 11:18:06 +0200484 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
Paul Mundt9964fa82007-09-21 18:09:55 +0900485 local_flush_tlb_one(asid, vaddr);
486}