4 * SMP support for the SuperH processors.
6 * Copyright (C) 2002 - 2010 Paul Mundt
7 * Copyright (C) 2006 - 2007 Akio Idehara
9 * 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
13 #include <linux/err.h>
14 #include <linux/cache.h>
15 #include <linux/cpumask.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
20 #include <linux/module.h>
21 #include <linux/cpu.h>
22 #include <linux/interrupt.h>
23 #include <linux/sched.h>
24 #include <linux/atomic.h>
25 #include <asm/processor.h>
26 #include <asm/system.h>
27 #include <asm/mmu_context.h>
29 #include <asm/cacheflush.h>
30 #include <asm/sections.h>
32 int __cpu_number_map
[NR_CPUS
]; /* Map physical to logical */
33 int __cpu_logical_map
[NR_CPUS
]; /* Map logical to physical */
35 struct plat_smp_ops
*mp_ops
= NULL
;
37 /* State of each CPU */
38 DEFINE_PER_CPU(int, cpu_state
) = { 0 };
40 void __cpuinit
register_smp_ops(struct plat_smp_ops
*ops
)
43 printk(KERN_WARNING
"Overriding previously set SMP ops\n");
48 static inline void __cpuinit
smp_store_cpu_info(unsigned int cpu
)
50 struct sh_cpuinfo
*c
= cpu_data
+ cpu
;
52 memcpy(c
, &boot_cpu_data
, sizeof(struct sh_cpuinfo
));
54 c
->loops_per_jiffy
= loops_per_jiffy
;
57 void __init
smp_prepare_cpus(unsigned int max_cpus
)
59 unsigned int cpu
= smp_processor_id();
61 init_new_context(current
, &init_mm
);
62 current_thread_info()->cpu
= cpu
;
63 mp_ops
->prepare_cpus(max_cpus
);
65 #ifndef CONFIG_HOTPLUG_CPU
66 init_cpu_present(&cpu_possible_map
);
70 void __init
smp_prepare_boot_cpu(void)
72 unsigned int cpu
= smp_processor_id();
74 __cpu_number_map
[0] = cpu
;
75 __cpu_logical_map
[0] = cpu
;
77 set_cpu_online(cpu
, true);
78 set_cpu_possible(cpu
, true);
80 per_cpu(cpu_state
, cpu
) = CPU_ONLINE
;
83 #ifdef CONFIG_HOTPLUG_CPU
84 void native_cpu_die(unsigned int cpu
)
88 for (i
= 0; i
< 10; i
++) {
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
);
100 pr_err("CPU %u didn't die...\n", cpu
);
103 int native_cpu_disable(unsigned int cpu
)
105 return cpu
== 0 ? -EPERM
: 0;
108 void play_dead_common(void)
111 irq_ctx_exit(raw_smp_processor_id());
114 __get_cpu_var(cpu_state
) = CPU_DEAD
;
118 void native_play_dead(void)
123 int __cpu_disable(void)
125 unsigned int cpu
= smp_processor_id();
126 struct task_struct
*p
;
129 ret
= mp_ops
->cpu_disable(cpu
);
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.
137 set_cpu_online(cpu
, false);
140 * OK - migrate IRQs away from this CPU
145 * Stop the local timer for this CPU.
147 local_timer_stop(cpu
);
150 * Flush user cache and TLB mappings, and then remove this CPU
151 * from the vm mask set of all processes.
154 local_flush_tlb_all();
156 read_lock(&tasklist_lock
);
159 cpumask_clear_cpu(cpu
, mm_cpumask(p
->mm
));
160 read_unlock(&tasklist_lock
);
164 #else /* ... !CONFIG_HOTPLUG_CPU */
165 int native_cpu_disable(unsigned int cpu
)
170 void native_cpu_die(unsigned int cpu
)
172 /* We said "no" in __cpu_disable */
176 void native_play_dead(void)
182 asmlinkage
void __cpuinit
start_secondary(void)
184 unsigned int cpu
= smp_processor_id();
185 struct mm_struct
*mm
= &init_mm
;
188 atomic_inc(&mm
->mm_count
);
189 atomic_inc(&mm
->mm_users
);
190 current
->active_mm
= mm
;
191 enter_lazy_tlb(mm
, current
);
192 local_flush_tlb_all();
198 notify_cpu_starting(cpu
);
202 /* Enable local timers */
203 local_timer_setup(cpu
);
206 smp_store_cpu_info(cpu
);
208 set_cpu_online(cpu
, true);
209 per_cpu(cpu_state
, cpu
) = CPU_ONLINE
;
216 unsigned long bss_start
;
217 unsigned long bss_end
;
218 void *start_kernel_fn
;
223 int __cpuinit
__cpu_up(unsigned int cpu
)
225 struct task_struct
*tsk
;
226 unsigned long timeout
;
228 tsk
= cpu_data
[cpu
].idle
;
230 tsk
= fork_idle(cpu
);
232 pr_err("Failed forking idle task for cpu %d\n", cpu
);
236 cpu_data
[cpu
].idle
= tsk
;
239 per_cpu(cpu_state
, cpu
) = CPU_UP_PREPARE
;
241 /* 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
;
247 flush_icache_range((unsigned long)&stack_start
,
248 (unsigned long)&stack_start
+ sizeof(stack_start
));
251 mp_ops
->start_cpu(cpu
, (unsigned long)_stext
);
253 timeout
= jiffies
+ HZ
;
254 while (time_before(jiffies
, timeout
)) {
268 void __init
smp_cpus_done(unsigned int max_cpus
)
270 unsigned long bogosum
= 0;
273 for_each_online_cpu(cpu
)
274 bogosum
+= cpu_data
[cpu
].loops_per_jiffy
;
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);
282 void smp_send_reschedule(int cpu
)
284 mp_ops
->send_ipi(cpu
, SMP_MSG_RESCHEDULE
);
287 void smp_send_stop(void)
289 smp_call_function(stop_this_cpu
, 0, 0);
292 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
296 for_each_cpu(cpu
, mask
)
297 mp_ops
->send_ipi(cpu
, SMP_MSG_FUNCTION
);
300 void arch_send_call_function_single_ipi(int cpu
)
302 mp_ops
->send_ipi(cpu
, SMP_MSG_FUNCTION_SINGLE
);
305 void smp_timer_broadcast(const struct cpumask
*mask
)
309 for_each_cpu(cpu
, mask
)
310 mp_ops
->send_ipi(cpu
, SMP_MSG_TIMER
);
313 static void ipi_timer(void)
316 local_timer_interrupt();
320 void smp_message_recv(unsigned int msg
)
323 case SMP_MSG_FUNCTION
:
324 generic_smp_call_function_interrupt();
326 case SMP_MSG_RESCHEDULE
:
329 case SMP_MSG_FUNCTION_SINGLE
:
330 generic_smp_call_function_single_interrupt();
336 printk(KERN_WARNING
"SMP %d: %s(): unknown IPI %d\n",
337 smp_processor_id(), __func__
, msg
);
342 /* Not really SMP stuff ... */
343 int setup_profiling_timer(unsigned int multiplier
)
348 static void flush_tlb_all_ipi(void *info
)
350 local_flush_tlb_all();
353 void flush_tlb_all(void)
355 on_each_cpu(flush_tlb_all_ipi
, 0, 1);
358 static void flush_tlb_mm_ipi(void *mm
)
360 local_flush_tlb_mm((struct mm_struct
*)mm
);
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).
375 void flush_tlb_mm(struct mm_struct
*mm
)
379 if ((atomic_read(&mm
->mm_users
) != 1) || (current
->mm
!= mm
)) {
380 smp_call_function(flush_tlb_mm_ipi
, (void *)mm
, 1);
383 for (i
= 0; i
< num_online_cpus(); i
++)
384 if (smp_processor_id() != i
)
385 cpu_context(i
, mm
) = 0;
387 local_flush_tlb_mm(mm
);
392 struct flush_tlb_data
{
393 struct vm_area_struct
*vma
;
398 static void flush_tlb_range_ipi(void *info
)
400 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
402 local_flush_tlb_range(fd
->vma
, fd
->addr1
, fd
->addr2
);
405 void flush_tlb_range(struct vm_area_struct
*vma
,
406 unsigned long start
, unsigned long end
)
408 struct mm_struct
*mm
= vma
->vm_mm
;
411 if ((atomic_read(&mm
->mm_users
) != 1) || (current
->mm
!= mm
)) {
412 struct flush_tlb_data fd
;
417 smp_call_function(flush_tlb_range_ipi
, (void *)&fd
, 1);
420 for (i
= 0; i
< num_online_cpus(); i
++)
421 if (smp_processor_id() != i
)
422 cpu_context(i
, mm
) = 0;
424 local_flush_tlb_range(vma
, start
, end
);
428 static void flush_tlb_kernel_range_ipi(void *info
)
430 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
432 local_flush_tlb_kernel_range(fd
->addr1
, fd
->addr2
);
435 void flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
437 struct flush_tlb_data fd
;
441 on_each_cpu(flush_tlb_kernel_range_ipi
, (void *)&fd
, 1);
444 static void flush_tlb_page_ipi(void *info
)
446 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
448 local_flush_tlb_page(fd
->vma
, fd
->addr1
);
451 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
454 if ((atomic_read(&vma
->vm_mm
->mm_users
) != 1) ||
455 (current
->mm
!= vma
->vm_mm
)) {
456 struct flush_tlb_data fd
;
460 smp_call_function(flush_tlb_page_ipi
, (void *)&fd
, 1);
463 for (i
= 0; i
< num_online_cpus(); i
++)
464 if (smp_processor_id() != i
)
465 cpu_context(i
, vma
->vm_mm
) = 0;
467 local_flush_tlb_page(vma
, page
);
471 static void flush_tlb_one_ipi(void *info
)
473 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
474 local_flush_tlb_one(fd
->addr1
, fd
->addr2
);
477 void flush_tlb_one(unsigned long asid
, unsigned long vaddr
)
479 struct flush_tlb_data fd
;
484 smp_call_function(flush_tlb_one_ipi
, (void *)&fd
, 1);
485 local_flush_tlb_one(asid
, vaddr
);