1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2000, 2001 Kanoj Sarcar
5 * Copyright (C) 2000, 2001 Ralf Baechle
6 * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
7 * Copyright (C) 2000, 2001, 2003 Broadcom Corporation
9 #include <linux/cache.h>
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/smp.h>
14 #include <linux/spinlock.h>
15 #include <linux/threads.h>
16 #include <linux/export.h>
17 #include <linux/time.h>
18 #include <linux/timex.h>
19 #include <linux/sched/mm.h>
20 #include <linux/cpumask.h>
21 #include <linux/cpu.h>
22 #include <linux/err.h>
23 #include <linux/ftrace.h>
24 #include <linux/irqdomain.h>
26 #include <linux/of_irq.h>
28 #include <linux/atomic.h>
30 #include <asm/ginvt.h>
31 #include <asm/processor.h>
33 #include <asm/r4k-timer.h>
34 #include <asm/mips-cps.h>
35 #include <asm/mmu_context.h>
37 #include <asm/setup.h>
40 int __cpu_number_map[CONFIG_MIPS_NR_CPU_NR_MAP]; /* Map physical to logical */
41 EXPORT_SYMBOL(__cpu_number_map);
43 int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
44 EXPORT_SYMBOL(__cpu_logical_map);
46 /* Number of TCs (or siblings in Intel speak) per CPU core */
47 int smp_num_siblings = 1;
48 EXPORT_SYMBOL(smp_num_siblings);
50 /* representing the TCs (or siblings in Intel speak) of each logical CPU */
51 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
52 EXPORT_SYMBOL(cpu_sibling_map);
54 /* representing the core map of multi-core chips of each logical CPU */
55 cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
56 EXPORT_SYMBOL(cpu_core_map);
58 static DECLARE_COMPLETION(cpu_starting);
59 static DECLARE_COMPLETION(cpu_running);
62 * A logical cpu mask containing only one VPE per core to
63 * reduce the number of IPIs on large MT systems.
65 cpumask_t cpu_foreign_map[NR_CPUS] __read_mostly;
66 EXPORT_SYMBOL(cpu_foreign_map);
68 /* representing cpus for which sibling maps can be computed */
69 static cpumask_t cpu_sibling_setup_map;
71 /* representing cpus for which core maps can be computed */
72 static cpumask_t cpu_core_setup_map;
74 cpumask_t cpu_coherent_mask;
76 unsigned int smp_max_threads __initdata = UINT_MAX;
78 static int __init early_nosmt(char *s)
83 early_param("nosmt", early_nosmt);
85 static int __init early_smt(char *s)
87 get_option(&s, &smp_max_threads);
88 /* Ensure at least one thread is available */
89 smp_max_threads = clamp_val(smp_max_threads, 1U, UINT_MAX);
92 early_param("smt", early_smt);
94 #ifdef CONFIG_GENERIC_IRQ_IPI
95 static struct irq_desc *call_desc;
96 static struct irq_desc *sched_desc;
99 static inline void set_cpu_sibling_map(int cpu)
103 cpumask_set_cpu(cpu, &cpu_sibling_setup_map);
105 if (smp_num_siblings > 1) {
106 for_each_cpu(i, &cpu_sibling_setup_map) {
107 if (cpus_are_siblings(cpu, i)) {
108 cpumask_set_cpu(i, &cpu_sibling_map[cpu]);
109 cpumask_set_cpu(cpu, &cpu_sibling_map[i]);
113 cpumask_set_cpu(cpu, &cpu_sibling_map[cpu]);
116 static inline void set_cpu_core_map(int cpu)
120 cpumask_set_cpu(cpu, &cpu_core_setup_map);
122 for_each_cpu(i, &cpu_core_setup_map) {
123 if (cpu_data[cpu].package == cpu_data[i].package) {
124 cpumask_set_cpu(i, &cpu_core_map[cpu]);
125 cpumask_set_cpu(cpu, &cpu_core_map[i]);
131 * Calculate a new cpu_foreign_map mask whenever a
132 * new cpu appears or disappears.
134 void calculate_cpu_foreign_map(void)
136 int i, k, core_present;
137 cpumask_t temp_foreign_map;
139 /* Re-calculate the mask */
140 cpumask_clear(&temp_foreign_map);
141 for_each_online_cpu(i) {
143 for_each_cpu(k, &temp_foreign_map)
144 if (cpus_are_siblings(i, k))
147 cpumask_set_cpu(i, &temp_foreign_map);
150 for_each_online_cpu(i)
151 cpumask_andnot(&cpu_foreign_map[i],
152 &temp_foreign_map, &cpu_sibling_map[i]);
155 const struct plat_smp_ops *mp_ops;
156 EXPORT_SYMBOL(mp_ops);
158 void register_smp_ops(const struct plat_smp_ops *ops)
161 printk(KERN_WARNING "Overriding previously set SMP ops\n");
166 #ifdef CONFIG_GENERIC_IRQ_IPI
167 void mips_smp_send_ipi_single(int cpu, unsigned int action)
169 mips_smp_send_ipi_mask(cpumask_of(cpu), action);
172 void mips_smp_send_ipi_mask(const struct cpumask *mask, unsigned int action)
178 local_irq_save(flags);
181 case SMP_CALL_FUNCTION:
182 __ipi_send_mask(call_desc, mask);
185 case SMP_RESCHEDULE_YOURSELF:
186 __ipi_send_mask(sched_desc, mask);
193 if (mips_cpc_present()) {
194 for_each_cpu(cpu, mask) {
195 if (cpus_are_siblings(cpu, smp_processor_id()))
198 core = cpu_core(&cpu_data[cpu]);
200 while (!cpumask_test_cpu(cpu, &cpu_coherent_mask)) {
201 mips_cm_lock_other_cpu(cpu, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
202 mips_cpc_lock_other(core);
203 write_cpc_co_cmd(CPC_Cx_CMD_PWRUP);
204 mips_cpc_unlock_other();
205 mips_cm_unlock_other();
210 local_irq_restore(flags);
214 static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
221 static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
223 generic_smp_call_function_interrupt();
228 static void smp_ipi_init_one(unsigned int virq, const char *name,
229 irq_handler_t handler)
233 irq_set_handler(virq, handle_percpu_irq);
234 ret = request_irq(virq, handler, IRQF_PERCPU, name, NULL);
238 static unsigned int call_virq, sched_virq;
240 int mips_smp_ipi_allocate(const struct cpumask *mask)
243 struct irq_domain *ipidomain;
244 struct device_node *node;
246 node = of_irq_find_parent(of_root);
247 ipidomain = irq_find_matching_host(node, DOMAIN_BUS_IPI);
250 * Some platforms have half DT setup. So if we found irq node but
251 * didn't find an ipidomain, try to search for one that is not in the
254 if (node && !ipidomain)
255 ipidomain = irq_find_matching_host(NULL, DOMAIN_BUS_IPI);
258 * There are systems which use IPI IRQ domains, but only have one
259 * registered when some runtime condition is met. For example a Malta
260 * kernel may include support for GIC & CPU interrupt controller IPI
261 * IRQ domains, but if run on a system with no GIC & no MT ASE then
262 * neither will be supported or registered.
264 * We only have a problem if we're actually using multiple CPUs so fail
265 * loudly if that is the case. Otherwise simply return, skipping IPI
266 * setup, if we're running with only a single CPU.
269 BUG_ON(num_present_cpus() > 1);
273 virq = irq_reserve_ipi(ipidomain, mask);
278 virq = irq_reserve_ipi(ipidomain, mask);
283 if (irq_domain_is_ipi_per_cpu(ipidomain)) {
286 for_each_cpu(cpu, mask) {
287 smp_ipi_init_one(call_virq + cpu, "IPI call",
289 smp_ipi_init_one(sched_virq + cpu, "IPI resched",
290 ipi_resched_interrupt);
293 smp_ipi_init_one(call_virq, "IPI call", ipi_call_interrupt);
294 smp_ipi_init_one(sched_virq, "IPI resched",
295 ipi_resched_interrupt);
301 int mips_smp_ipi_free(const struct cpumask *mask)
303 struct irq_domain *ipidomain;
304 struct device_node *node;
306 node = of_irq_find_parent(of_root);
307 ipidomain = irq_find_matching_host(node, DOMAIN_BUS_IPI);
310 * Some platforms have half DT setup. So if we found irq node but
311 * didn't find an ipidomain, try to search for one that is not in the
314 if (node && !ipidomain)
315 ipidomain = irq_find_matching_host(NULL, DOMAIN_BUS_IPI);
319 if (irq_domain_is_ipi_per_cpu(ipidomain)) {
322 for_each_cpu(cpu, mask) {
323 free_irq(call_virq + cpu, NULL);
324 free_irq(sched_virq + cpu, NULL);
327 irq_destroy_ipi(call_virq, mask);
328 irq_destroy_ipi(sched_virq, mask);
333 static int __init mips_smp_ipi_init(void)
335 if (num_possible_cpus() == 1)
338 mips_smp_ipi_allocate(cpu_possible_mask);
340 call_desc = irq_to_desc(call_virq);
341 sched_desc = irq_to_desc(sched_virq);
345 early_initcall(mips_smp_ipi_init);
349 * First C code run on the secondary CPUs after being started up by
352 asmlinkage void start_secondary(void)
354 unsigned int cpu = raw_smp_processor_id();
357 per_cpu_trap_init(false);
358 rcutree_report_cpu_starting(cpu);
359 mips_clockevent_init();
360 mp_ops->init_secondary();
365 * XXX parity protection should be folded in here when it's converted
366 * to an option instead of something based on .cputype
370 cpu_data[cpu].udelay_val = loops_per_jiffy;
372 set_cpu_sibling_map(cpu);
373 set_cpu_core_map(cpu);
375 cpumask_set_cpu(cpu, &cpu_coherent_mask);
376 notify_cpu_starting(cpu);
378 /* Notify boot CPU that we're starting & ready to sync counters */
379 complete(&cpu_starting);
381 synchronise_count_slave(cpu);
383 /* The CPU is running and counters synchronised, now mark it online */
384 set_cpu_online(cpu, true);
386 calculate_cpu_foreign_map();
389 * Notify boot CPU that we're up & online and it can safely return
392 complete(&cpu_running);
395 * irq will be enabled in ->smp_finish(), enabling it too early
398 WARN_ON_ONCE(!irqs_disabled());
399 mp_ops->smp_finish();
401 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
404 static void stop_this_cpu(void *dummy)
410 set_cpu_online(smp_processor_id(), false);
411 calculate_cpu_foreign_map();
416 void smp_send_stop(void)
418 smp_call_function(stop_this_cpu, NULL, 0);
421 void __init smp_cpus_done(unsigned int max_cpus)
425 /* called from main before smp_init() */
426 void __init smp_prepare_cpus(unsigned int max_cpus)
428 init_new_context(current, &init_mm);
429 current_thread_info()->cpu = 0;
430 mp_ops->prepare_cpus(max_cpus);
431 set_cpu_sibling_map(0);
433 calculate_cpu_foreign_map();
434 #ifndef CONFIG_HOTPLUG_CPU
435 init_cpu_present(cpu_possible_mask);
437 cpumask_copy(&cpu_coherent_mask, cpu_possible_mask);
440 /* preload SMP state for boot cpu */
441 void smp_prepare_boot_cpu(void)
443 if (mp_ops->prepare_boot_cpu)
444 mp_ops->prepare_boot_cpu();
445 set_cpu_possible(0, true);
446 set_cpu_online(0, true);
449 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
453 err = mp_ops->boot_secondary(cpu, tidle);
457 /* Wait for CPU to start and be ready to sync counters */
458 if (!wait_for_completion_timeout(&cpu_starting,
459 msecs_to_jiffies(1000))) {
460 pr_crit("CPU%u: failed to start\n", cpu);
464 synchronise_count_master(cpu);
466 /* Wait for CPU to finish startup & mark itself online before return */
467 wait_for_completion(&cpu_running);
471 /* Not really SMP stuff ... */
472 int setup_profiling_timer(unsigned int multiplier)
477 static void flush_tlb_all_ipi(void *info)
479 local_flush_tlb_all();
482 void flush_tlb_all(void)
488 instruction_hazard();
493 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
496 static void flush_tlb_mm_ipi(void *mm)
498 drop_mmu_context((struct mm_struct *)mm);
502 * Special Variant of smp_call_function for use by TLB functions:
505 * o collapses to normal function call on UP kernels
506 * o collapses to normal function call on systems with a single shared
509 static inline void smp_on_other_tlbs(void (*func) (void *info), void *info)
511 smp_call_function(func, info, 1);
514 static inline void smp_on_each_tlb(void (*func) (void *info), void *info)
518 smp_on_other_tlbs(func, info);
525 * The following tlb flush calls are invoked when old translations are
526 * being torn down, or pte attributes are changing. For single threaded
527 * address spaces, a new context is obtained on the current cpu, and tlb
528 * context on other cpus are invalidated to force a new context allocation
529 * at switch_mm time, should the mm ever be used on other cpus. For
530 * multithreaded address spaces, inter-CPU interrupts have to be sent.
531 * Another case where inter-CPU interrupts are required is when the target
532 * mm might be active on another cpu (eg debuggers doing the flushes on
533 * behalf of debugees, kswapd stealing pages from another process etc).
537 void flush_tlb_mm(struct mm_struct *mm)
542 if (atomic_read(&mm->mm_users) == 0)
543 return; /* happens as a result of exit_mmap() */
549 * No need to worry about other CPUs - the ginvt in
550 * drop_mmu_context() will be globalized.
552 } else if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
553 smp_on_other_tlbs(flush_tlb_mm_ipi, mm);
557 for_each_online_cpu(cpu) {
558 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
559 set_cpu_context(cpu, mm, 0);
562 drop_mmu_context(mm);
567 struct flush_tlb_data {
568 struct vm_area_struct *vma;
573 static void flush_tlb_range_ipi(void *info)
575 struct flush_tlb_data *fd = info;
577 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
580 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
582 struct mm_struct *mm = vma->vm_mm;
589 old_mmid = read_c0_memorymapid();
590 write_c0_memorymapid(cpu_asid(0, mm));
592 addr = round_down(start, PAGE_SIZE * 2);
593 end = round_up(end, PAGE_SIZE * 2);
597 addr += PAGE_SIZE * 2;
598 } while (addr < end);
599 write_c0_memorymapid(old_mmid);
600 instruction_hazard();
602 } else if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
603 struct flush_tlb_data fd = {
609 smp_on_other_tlbs(flush_tlb_range_ipi, &fd);
610 local_flush_tlb_range(vma, start, end);
613 int exec = vma->vm_flags & VM_EXEC;
615 for_each_online_cpu(cpu) {
617 * flush_cache_range() will only fully flush icache if
618 * the VMA is executable, otherwise we must invalidate
619 * ASID without it appearing to has_valid_asid() as if
620 * mm has been completely unused by that CPU.
622 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
623 set_cpu_context(cpu, mm, !exec);
625 local_flush_tlb_range(vma, start, end);
630 static void flush_tlb_kernel_range_ipi(void *info)
632 struct flush_tlb_data *fd = info;
634 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
637 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
639 struct flush_tlb_data fd = {
644 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
647 static void flush_tlb_page_ipi(void *info)
649 struct flush_tlb_data *fd = info;
651 local_flush_tlb_page(fd->vma, fd->addr1);
654 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
661 old_mmid = read_c0_memorymapid();
662 write_c0_memorymapid(cpu_asid(0, vma->vm_mm));
666 write_c0_memorymapid(old_mmid);
667 instruction_hazard();
669 } else if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
670 (current->mm != vma->vm_mm)) {
671 struct flush_tlb_data fd = {
676 smp_on_other_tlbs(flush_tlb_page_ipi, &fd);
677 local_flush_tlb_page(vma, page);
681 for_each_online_cpu(cpu) {
683 * flush_cache_page() only does partial flushes, so
684 * invalidate ASID without it appearing to
685 * has_valid_asid() as if mm has been completely unused
688 if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
689 set_cpu_context(cpu, vma->vm_mm, 1);
691 local_flush_tlb_page(vma, page);
696 static void flush_tlb_one_ipi(void *info)
698 unsigned long vaddr = (unsigned long) info;
700 local_flush_tlb_one(vaddr);
703 void flush_tlb_one(unsigned long vaddr)
705 smp_on_each_tlb(flush_tlb_one_ipi, (void *) vaddr);
708 EXPORT_SYMBOL(flush_tlb_page);
709 EXPORT_SYMBOL(flush_tlb_one);
711 #ifdef CONFIG_HOTPLUG_CORE_SYNC_DEAD
712 void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)
714 if (mp_ops->cleanup_dead_cpu)
715 mp_ops->cleanup_dead_cpu(cpu);
719 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
721 static void tick_broadcast_callee(void *info)
723 tick_receive_broadcast();
726 static DEFINE_PER_CPU(call_single_data_t, tick_broadcast_csd) =
727 CSD_INIT(tick_broadcast_callee, NULL);
729 void tick_broadcast(const struct cpumask *mask)
731 call_single_data_t *csd;
734 for_each_cpu(cpu, mask) {
735 csd = &per_cpu(tick_broadcast_csd, cpu);
736 smp_call_function_single_async(cpu, csd);
740 #endif /* CONFIG_GENERIC_CLOCKEVENTS_BROADCAST */