2 * Xtensa SMP support functions.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2008 - 2013 Tensilica Inc.
10 * Chris Zankel <chris@zankel.net>
11 * Joe Taylor <joe@tensilica.com>
12 * Pete Delaney <piet@tensilica.com
15 #include <linux/cpu.h>
16 #include <linux/cpumask.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/irqdomain.h>
21 #include <linux/irq.h>
22 #include <linux/kdebug.h>
23 #include <linux/module.h>
24 #include <linux/reboot.h>
25 #include <linux/seq_file.h>
26 #include <linux/smp.h>
27 #include <linux/thread_info.h>
29 #include <asm/cacheflush.h>
30 #include <asm/kdebug.h>
31 #include <asm/mmu_context.h>
32 #include <asm/mxregs.h>
33 #include <asm/platform.h>
34 #include <asm/tlbflush.h>
35 #include <asm/traps.h>
38 # if XCHAL_HAVE_S32C1I == 0
39 # error "The S32C1I option is required for SMP."
43 static void system_invalidate_dcache_range(unsigned long start,
45 static void system_flush_invalidate_dcache_range(unsigned long start,
48 /* IPI (Inter Process Interrupt) */
52 static irqreturn_t ipi_interrupt(int irq, void *dev_id);
53 static struct irqaction ipi_irqaction = {
54 .handler = ipi_interrupt,
61 unsigned irq = irq_create_mapping(NULL, IPI_IRQ);
62 setup_irq(irq, &ipi_irqaction);
65 static inline unsigned int get_core_count(void)
67 /* Bits 18..21 of SYSCFGID contain the core count minus 1. */
68 unsigned int syscfgid = get_er(SYSCFGID);
69 return ((syscfgid >> 18) & 0xf) + 1;
72 static inline int get_core_id(void)
74 /* Bits 0...18 of SYSCFGID contain the core id */
75 unsigned int core_id = get_er(SYSCFGID);
76 return core_id & 0x3fff;
79 void __init smp_prepare_cpus(unsigned int max_cpus)
83 for_each_possible_cpu(i)
84 set_cpu_present(i, true);
87 void __init smp_init_cpus(void)
90 unsigned int ncpus = get_core_count();
91 unsigned int core_id = get_core_id();
93 pr_info("%s: Core Count = %d\n", __func__, ncpus);
94 pr_info("%s: Core Id = %d\n", __func__, core_id);
96 if (ncpus > NR_CPUS) {
98 pr_info("%s: limiting core count by %d\n", __func__, ncpus);
101 for (i = 0; i < ncpus; ++i)
102 set_cpu_possible(i, true);
105 void __init smp_prepare_boot_cpu(void)
107 unsigned int cpu = smp_processor_id();
109 cpu_asid_cache(cpu) = ASID_USER_FIRST;
112 void __init smp_cpus_done(unsigned int max_cpus)
116 static int boot_secondary_processors = 1; /* Set with xt-gdb via .xt-gdb */
117 static DECLARE_COMPLETION(cpu_running);
119 void secondary_start_kernel(void)
121 struct mm_struct *mm = &init_mm;
122 unsigned int cpu = smp_processor_id();
126 #ifdef CONFIG_DEBUG_KERNEL
127 if (boot_secondary_processors == 0) {
128 pr_debug("%s: boot_secondary_processors:%d; Hanging cpu:%d\n",
129 __func__, boot_secondary_processors, cpu);
131 __asm__ __volatile__ ("waiti " __stringify(LOCKLEVEL));
134 pr_debug("%s: boot_secondary_processors:%d; Booting cpu:%d\n",
135 __func__, boot_secondary_processors, cpu);
139 secondary_trap_init();
141 /* All kernel threads share the same mm context. */
143 atomic_inc(&mm->mm_users);
144 atomic_inc(&mm->mm_count);
145 current->active_mm = mm;
146 cpumask_set_cpu(cpu, mm_cpumask(mm));
147 enter_lazy_tlb(mm, current);
150 trace_hardirqs_off();
154 notify_cpu_starting(cpu);
156 secondary_init_irq();
157 local_timer_setup(cpu);
159 set_cpu_online(cpu, true);
163 complete(&cpu_running);
165 cpu_startup_entry(CPUHP_ONLINE);
168 static void mx_cpu_start(void *p)
170 unsigned cpu = (unsigned)p;
171 unsigned long run_stall_mask = get_er(MPSCORE);
173 set_er(run_stall_mask & ~(1u << cpu), MPSCORE);
174 pr_debug("%s: cpu: %d, run_stall_mask: %lx ---> %lx\n",
175 __func__, cpu, run_stall_mask, get_er(MPSCORE));
178 static void mx_cpu_stop(void *p)
180 unsigned cpu = (unsigned)p;
181 unsigned long run_stall_mask = get_er(MPSCORE);
183 set_er(run_stall_mask | (1u << cpu), MPSCORE);
184 pr_debug("%s: cpu: %d, run_stall_mask: %lx ---> %lx\n",
185 __func__, cpu, run_stall_mask, get_er(MPSCORE));
188 #ifdef CONFIG_HOTPLUG_CPU
189 unsigned long cpu_start_id __cacheline_aligned;
191 unsigned long cpu_start_ccount;
193 static int boot_secondary(unsigned int cpu, struct task_struct *ts)
195 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
196 unsigned long ccount;
199 #ifdef CONFIG_HOTPLUG_CPU
200 WRITE_ONCE(cpu_start_id, cpu);
201 /* Pairs with the third memw in the cpu_restart */
203 system_flush_invalidate_dcache_range((unsigned long)&cpu_start_id,
204 sizeof(cpu_start_id));
206 smp_call_function_single(0, mx_cpu_start, (void *)cpu, 1);
208 for (i = 0; i < 2; ++i) {
210 ccount = get_ccount();
213 WRITE_ONCE(cpu_start_ccount, ccount);
217 * Pairs with the first two memws in the
221 ccount = READ_ONCE(cpu_start_ccount);
222 } while (ccount && time_before(jiffies, timeout));
225 smp_call_function_single(0, mx_cpu_stop,
227 WRITE_ONCE(cpu_start_ccount, 0);
234 int __cpu_up(unsigned int cpu, struct task_struct *idle)
238 if (cpu_asid_cache(cpu) == 0)
239 cpu_asid_cache(cpu) = ASID_USER_FIRST;
241 start_info.stack = (unsigned long)task_pt_regs(idle);
244 pr_debug("%s: Calling wakeup_secondary(cpu:%d, idle:%p, sp: %08lx)\n",
245 __func__, cpu, idle, start_info.stack);
247 init_completion(&cpu_running);
248 ret = boot_secondary(cpu, idle);
250 wait_for_completion_timeout(&cpu_running,
251 msecs_to_jiffies(1000));
252 if (!cpu_online(cpu))
257 pr_err("CPU %u failed to boot\n", cpu);
262 #ifdef CONFIG_HOTPLUG_CPU
265 * __cpu_disable runs on the processor to be shutdown.
267 int __cpu_disable(void)
269 unsigned int cpu = smp_processor_id();
272 * Take this CPU offline. Once we clear this, we can't return,
273 * and we must not schedule until we're ready to give up the cpu.
275 set_cpu_online(cpu, false);
278 * OK - migrate IRQs away from this CPU
283 * Flush user cache and TLB mappings, and then remove this CPU
284 * from the vm mask set of all processes.
286 local_flush_cache_all();
287 local_flush_tlb_all();
288 invalidate_page_directory();
290 clear_tasks_mm_cpumask(cpu);
295 static void platform_cpu_kill(unsigned int cpu)
297 smp_call_function_single(0, mx_cpu_stop, (void *)cpu, true);
301 * called on the thread which is asking for a CPU to be shutdown -
302 * waits until shutdown has completed, or it is timed out.
304 void __cpu_die(unsigned int cpu)
306 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
307 while (time_before(jiffies, timeout)) {
308 system_invalidate_dcache_range((unsigned long)&cpu_start_id,
309 sizeof(cpu_start_id));
310 /* Pairs with the second memw in the cpu_restart */
312 if (READ_ONCE(cpu_start_id) == -cpu) {
313 platform_cpu_kill(cpu);
317 pr_err("CPU%u: unable to kill\n", cpu);
320 void arch_cpu_idle_dead(void)
325 * Called from the idle thread for the CPU which has been shutdown.
327 * Note that we disable IRQs here, but do not re-enable them
328 * before returning to the caller. This is also the behaviour
329 * of the other hotplug-cpu capable cores, so presumably coming
330 * out of idle fixes this.
332 void __ref cpu_die(void)
336 __asm__ __volatile__(
337 " movi a2, cpu_restart\n"
341 #endif /* CONFIG_HOTPLUG_CPU */
350 static const struct {
351 const char *short_text;
352 const char *long_text;
354 { .short_text = "RES", .long_text = "Rescheduling interrupts" },
355 { .short_text = "CAL", .long_text = "Function call interrupts" },
356 { .short_text = "DIE", .long_text = "CPU shutdown interrupts" },
360 unsigned long ipi_count[IPI_MAX];
363 static DEFINE_PER_CPU(struct ipi_data, ipi_data);
365 static void send_ipi_message(const struct cpumask *callmask,
366 enum ipi_msg_type msg_id)
369 unsigned long mask = 0;
371 for_each_cpu(index, callmask)
372 if (index != smp_processor_id())
375 set_er(mask, MIPISET(msg_id));
378 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
380 send_ipi_message(mask, IPI_CALL_FUNC);
383 void arch_send_call_function_single_ipi(int cpu)
385 send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
388 void smp_send_reschedule(int cpu)
390 send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
393 void smp_send_stop(void)
395 struct cpumask targets;
397 cpumask_copy(&targets, cpu_online_mask);
398 cpumask_clear_cpu(smp_processor_id(), &targets);
399 send_ipi_message(&targets, IPI_CPU_STOP);
402 static void ipi_cpu_stop(unsigned int cpu)
404 set_cpu_online(cpu, false);
408 irqreturn_t ipi_interrupt(int irq, void *dev_id)
410 unsigned int cpu = smp_processor_id();
411 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
415 msg = get_er(MIPICAUSE(cpu));
416 for (i = 0; i < IPI_MAX; i++)
417 if (msg & (1 << i)) {
418 set_er(1 << i, MIPICAUSE(cpu));
422 if (msg & (1 << IPI_RESCHEDULE))
424 if (msg & (1 << IPI_CALL_FUNC))
425 generic_smp_call_function_interrupt();
426 if (msg & (1 << IPI_CPU_STOP))
432 void show_ipi_list(struct seq_file *p, int prec)
437 for (i = 0; i < IPI_MAX; ++i) {
438 seq_printf(p, "%*s:", prec, ipi_text[i].short_text);
439 for_each_online_cpu(cpu)
440 seq_printf(p, " %10lu",
441 per_cpu(ipi_data, cpu).ipi_count[i]);
442 seq_printf(p, " %s\n", ipi_text[i].long_text);
446 int setup_profiling_timer(unsigned int multiplier)
448 pr_debug("setup_profiling_timer %d\n", multiplier);
452 /* TLB flush functions */
455 struct vm_area_struct *vma;
460 static void ipi_flush_tlb_all(void *arg)
462 local_flush_tlb_all();
465 void flush_tlb_all(void)
467 on_each_cpu(ipi_flush_tlb_all, NULL, 1);
470 static void ipi_flush_tlb_mm(void *arg)
472 local_flush_tlb_mm(arg);
475 void flush_tlb_mm(struct mm_struct *mm)
477 on_each_cpu(ipi_flush_tlb_mm, mm, 1);
480 static void ipi_flush_tlb_page(void *arg)
482 struct flush_data *fd = arg;
483 local_flush_tlb_page(fd->vma, fd->addr1);
486 void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
488 struct flush_data fd = {
492 on_each_cpu(ipi_flush_tlb_page, &fd, 1);
495 static void ipi_flush_tlb_range(void *arg)
497 struct flush_data *fd = arg;
498 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
501 void flush_tlb_range(struct vm_area_struct *vma,
502 unsigned long start, unsigned long end)
504 struct flush_data fd = {
509 on_each_cpu(ipi_flush_tlb_range, &fd, 1);
512 static void ipi_flush_tlb_kernel_range(void *arg)
514 struct flush_data *fd = arg;
515 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
518 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
520 struct flush_data fd = {
524 on_each_cpu(ipi_flush_tlb_kernel_range, &fd, 1);
527 /* Cache flush functions */
529 static void ipi_flush_cache_all(void *arg)
531 local_flush_cache_all();
534 void flush_cache_all(void)
536 on_each_cpu(ipi_flush_cache_all, NULL, 1);
539 static void ipi_flush_cache_page(void *arg)
541 struct flush_data *fd = arg;
542 local_flush_cache_page(fd->vma, fd->addr1, fd->addr2);
545 void flush_cache_page(struct vm_area_struct *vma,
546 unsigned long address, unsigned long pfn)
548 struct flush_data fd = {
553 on_each_cpu(ipi_flush_cache_page, &fd, 1);
556 static void ipi_flush_cache_range(void *arg)
558 struct flush_data *fd = arg;
559 local_flush_cache_range(fd->vma, fd->addr1, fd->addr2);
562 void flush_cache_range(struct vm_area_struct *vma,
563 unsigned long start, unsigned long end)
565 struct flush_data fd = {
570 on_each_cpu(ipi_flush_cache_range, &fd, 1);
573 static void ipi_flush_icache_range(void *arg)
575 struct flush_data *fd = arg;
576 local_flush_icache_range(fd->addr1, fd->addr2);
579 void flush_icache_range(unsigned long start, unsigned long end)
581 struct flush_data fd = {
585 on_each_cpu(ipi_flush_icache_range, &fd, 1);
587 EXPORT_SYMBOL(flush_icache_range);
589 /* ------------------------------------------------------------------------- */
591 static void ipi_invalidate_dcache_range(void *arg)
593 struct flush_data *fd = arg;
594 __invalidate_dcache_range(fd->addr1, fd->addr2);
597 static void system_invalidate_dcache_range(unsigned long start,
600 struct flush_data fd = {
604 on_each_cpu(ipi_invalidate_dcache_range, &fd, 1);
607 static void ipi_flush_invalidate_dcache_range(void *arg)
609 struct flush_data *fd = arg;
610 __flush_invalidate_dcache_range(fd->addr1, fd->addr2);
613 static void system_flush_invalidate_dcache_range(unsigned long start,
616 struct flush_data fd = {
620 on_each_cpu(ipi_flush_invalidate_dcache_range, &fd, 1);