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/mm.h>
24 #include <linux/sched/hotplug.h>
25 #include <linux/atomic.h>
26 #include <linux/clockchips.h>
27 #include <asm/processor.h>
28 #include <asm/mmu_context.h>
30 #include <asm/cacheflush.h>
31 #include <asm/sections.h>
32 #include <asm/setup.h>
34 int __cpu_number_map
[NR_CPUS
]; /* Map physical to logical */
35 int __cpu_logical_map
[NR_CPUS
]; /* Map logical to physical */
37 struct plat_smp_ops
*mp_ops
= NULL
;
39 /* State of each CPU */
40 DEFINE_PER_CPU(int, cpu_state
) = { 0 };
42 void register_smp_ops(struct plat_smp_ops
*ops
)
45 printk(KERN_WARNING
"Overriding previously set SMP ops\n");
50 static inline void smp_store_cpu_info(unsigned int cpu
)
52 struct sh_cpuinfo
*c
= cpu_data
+ cpu
;
54 memcpy(c
, &boot_cpu_data
, sizeof(struct sh_cpuinfo
));
56 c
->loops_per_jiffy
= loops_per_jiffy
;
59 void __init
smp_prepare_cpus(unsigned int max_cpus
)
61 unsigned int cpu
= smp_processor_id();
63 init_new_context(current
, &init_mm
);
64 current_thread_info()->cpu
= cpu
;
65 mp_ops
->prepare_cpus(max_cpus
);
67 #ifndef CONFIG_HOTPLUG_CPU
68 init_cpu_present(cpu_possible_mask
);
72 void __init
smp_prepare_boot_cpu(void)
74 unsigned int cpu
= smp_processor_id();
76 __cpu_number_map
[0] = cpu
;
77 __cpu_logical_map
[0] = cpu
;
79 set_cpu_online(cpu
, true);
80 set_cpu_possible(cpu
, true);
82 per_cpu(cpu_state
, cpu
) = CPU_ONLINE
;
85 #ifdef CONFIG_HOTPLUG_CPU
86 void native_cpu_die(unsigned int cpu
)
90 for (i
= 0; i
< 10; i
++) {
92 if (per_cpu(cpu_state
, cpu
) == CPU_DEAD
) {
93 if (system_state
== SYSTEM_RUNNING
)
94 pr_info("CPU %u is now offline\n", cpu
);
102 pr_err("CPU %u didn't die...\n", cpu
);
105 int native_cpu_disable(unsigned int cpu
)
107 return cpu
== 0 ? -EPERM
: 0;
110 void play_dead_common(void)
113 irq_ctx_exit(raw_smp_processor_id());
116 __this_cpu_write(cpu_state
, CPU_DEAD
);
120 void native_play_dead(void)
125 int __cpu_disable(void)
127 unsigned int cpu
= smp_processor_id();
130 ret
= mp_ops
->cpu_disable(cpu
);
135 * Take this CPU offline. Once we clear this, we can't return,
136 * and we must not schedule until we're ready to give up the cpu.
138 set_cpu_online(cpu
, false);
141 * OK - migrate IRQs away from this CPU
146 * Flush user cache and TLB mappings, and then remove this CPU
147 * from the vm mask set of all processes.
151 local_flush_tlb_all();
154 clear_tasks_mm_cpumask(cpu
);
158 #else /* ... !CONFIG_HOTPLUG_CPU */
159 int native_cpu_disable(unsigned int cpu
)
164 void native_cpu_die(unsigned int cpu
)
166 /* We said "no" in __cpu_disable */
170 void native_play_dead(void)
176 asmlinkage
void start_secondary(void)
178 unsigned int cpu
= smp_processor_id();
179 struct mm_struct
*mm
= &init_mm
;
184 current
->active_mm
= mm
;
186 enter_lazy_tlb(mm
, current
);
187 local_flush_tlb_all();
194 notify_cpu_starting(cpu
);
200 smp_store_cpu_info(cpu
);
202 set_cpu_online(cpu
, true);
203 per_cpu(cpu_state
, cpu
) = CPU_ONLINE
;
205 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE
);
210 unsigned long bss_start
;
211 unsigned long bss_end
;
212 void *start_kernel_fn
;
217 int __cpu_up(unsigned int cpu
, struct task_struct
*tsk
)
219 unsigned long timeout
;
221 per_cpu(cpu_state
, cpu
) = CPU_UP_PREPARE
;
223 /* Fill in data in head.S for secondary cpus */
224 stack_start
.sp
= tsk
->thread
.sp
;
225 stack_start
.thread_info
= tsk
->stack
;
226 stack_start
.bss_start
= 0; /* don't clear bss for secondary cpus */
227 stack_start
.start_kernel_fn
= start_secondary
;
229 flush_icache_range((unsigned long)&stack_start
,
230 (unsigned long)&stack_start
+ sizeof(stack_start
));
233 mp_ops
->start_cpu(cpu
, (unsigned long)_stext
);
235 timeout
= jiffies
+ HZ
;
236 while (time_before(jiffies
, timeout
)) {
250 void __init
smp_cpus_done(unsigned int max_cpus
)
252 unsigned long bogosum
= 0;
255 for_each_online_cpu(cpu
)
256 bogosum
+= cpu_data
[cpu
].loops_per_jiffy
;
258 printk(KERN_INFO
"SMP: Total of %d processors activated "
259 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
260 bogosum
/ (500000/HZ
),
261 (bogosum
/ (5000/HZ
)) % 100);
264 void smp_send_reschedule(int cpu
)
266 mp_ops
->send_ipi(cpu
, SMP_MSG_RESCHEDULE
);
269 void smp_send_stop(void)
271 smp_call_function(stop_this_cpu
, 0, 0);
274 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
278 for_each_cpu(cpu
, mask
)
279 mp_ops
->send_ipi(cpu
, SMP_MSG_FUNCTION
);
282 void arch_send_call_function_single_ipi(int cpu
)
284 mp_ops
->send_ipi(cpu
, SMP_MSG_FUNCTION_SINGLE
);
287 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
288 void tick_broadcast(const struct cpumask
*mask
)
292 for_each_cpu(cpu
, mask
)
293 mp_ops
->send_ipi(cpu
, SMP_MSG_TIMER
);
296 static void ipi_timer(void)
299 tick_receive_broadcast();
304 void smp_message_recv(unsigned int msg
)
307 case SMP_MSG_FUNCTION
:
308 generic_smp_call_function_interrupt();
310 case SMP_MSG_RESCHEDULE
:
313 case SMP_MSG_FUNCTION_SINGLE
:
314 generic_smp_call_function_single_interrupt();
316 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
322 printk(KERN_WARNING
"SMP %d: %s(): unknown IPI %d\n",
323 smp_processor_id(), __func__
, msg
);
328 /* Not really SMP stuff ... */
329 int setup_profiling_timer(unsigned int multiplier
)
336 static void flush_tlb_all_ipi(void *info
)
338 local_flush_tlb_all();
341 void flush_tlb_all(void)
343 on_each_cpu(flush_tlb_all_ipi
, 0, 1);
346 static void flush_tlb_mm_ipi(void *mm
)
348 local_flush_tlb_mm((struct mm_struct
*)mm
);
352 * The following tlb flush calls are invoked when old translations are
353 * being torn down, or pte attributes are changing. For single threaded
354 * address spaces, a new context is obtained on the current cpu, and tlb
355 * context on other cpus are invalidated to force a new context allocation
356 * at switch_mm time, should the mm ever be used on other cpus. For
357 * multithreaded address spaces, intercpu interrupts have to be sent.
358 * Another case where intercpu interrupts are required is when the target
359 * mm might be active on another cpu (eg debuggers doing the flushes on
360 * behalf of debugees, kswapd stealing pages from another process etc).
363 void flush_tlb_mm(struct mm_struct
*mm
)
367 if ((atomic_read(&mm
->mm_users
) != 1) || (current
->mm
!= mm
)) {
368 smp_call_function(flush_tlb_mm_ipi
, (void *)mm
, 1);
371 for_each_online_cpu(i
)
372 if (smp_processor_id() != i
)
373 cpu_context(i
, mm
) = 0;
375 local_flush_tlb_mm(mm
);
380 struct flush_tlb_data
{
381 struct vm_area_struct
*vma
;
386 static void flush_tlb_range_ipi(void *info
)
388 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
390 local_flush_tlb_range(fd
->vma
, fd
->addr1
, fd
->addr2
);
393 void flush_tlb_range(struct vm_area_struct
*vma
,
394 unsigned long start
, unsigned long end
)
396 struct mm_struct
*mm
= vma
->vm_mm
;
399 if ((atomic_read(&mm
->mm_users
) != 1) || (current
->mm
!= mm
)) {
400 struct flush_tlb_data fd
;
405 smp_call_function(flush_tlb_range_ipi
, (void *)&fd
, 1);
408 for_each_online_cpu(i
)
409 if (smp_processor_id() != i
)
410 cpu_context(i
, mm
) = 0;
412 local_flush_tlb_range(vma
, start
, end
);
416 static void flush_tlb_kernel_range_ipi(void *info
)
418 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
420 local_flush_tlb_kernel_range(fd
->addr1
, fd
->addr2
);
423 void flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
425 struct flush_tlb_data fd
;
429 on_each_cpu(flush_tlb_kernel_range_ipi
, (void *)&fd
, 1);
432 static void flush_tlb_page_ipi(void *info
)
434 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
436 local_flush_tlb_page(fd
->vma
, fd
->addr1
);
439 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
442 if ((atomic_read(&vma
->vm_mm
->mm_users
) != 1) ||
443 (current
->mm
!= vma
->vm_mm
)) {
444 struct flush_tlb_data fd
;
448 smp_call_function(flush_tlb_page_ipi
, (void *)&fd
, 1);
451 for_each_online_cpu(i
)
452 if (smp_processor_id() != i
)
453 cpu_context(i
, vma
->vm_mm
) = 0;
455 local_flush_tlb_page(vma
, page
);
459 static void flush_tlb_one_ipi(void *info
)
461 struct flush_tlb_data
*fd
= (struct flush_tlb_data
*)info
;
462 local_flush_tlb_one(fd
->addr1
, fd
->addr2
);
465 void flush_tlb_one(unsigned long asid
, unsigned long vaddr
)
467 struct flush_tlb_data fd
;
472 smp_call_function(flush_tlb_one_ipi
, (void *)&fd
, 1);
473 local_flush_tlb_one(asid
, vaddr
);