1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6 * Copyright (C) 2000, 2001 Kanoj Sarcar
7 * Copyright (C) 2000, 2001 Ralf Baechle
8 * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
9 * Copyright (C) 2000, 2001, 2003 Broadcom Corporation
11 #include <linux/acpi.h>
12 #include <linux/cpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/profile.h>
17 #include <linux/seq_file.h>
18 #include <linux/smp.h>
19 #include <linux/threads.h>
20 #include <linux/export.h>
21 #include <linux/syscore_ops.h>
22 #include <linux/time.h>
23 #include <linux/tracepoint.h>
24 #include <linux/sched/hotplug.h>
25 #include <linux/sched/task_stack.h>
29 #include <asm/loongson.h>
30 #include <asm/mmu_context.h>
32 #include <asm/processor.h>
33 #include <asm/setup.h>
36 int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
37 EXPORT_SYMBOL(__cpu_number_map);
39 int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
40 EXPORT_SYMBOL(__cpu_logical_map);
42 /* Representing the threads (siblings) of each logical CPU */
43 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
44 EXPORT_SYMBOL(cpu_sibling_map);
46 /* Representing the core map of multi-core chips of each logical CPU */
47 cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
48 EXPORT_SYMBOL(cpu_core_map);
50 static DECLARE_COMPLETION(cpu_starting);
51 static DECLARE_COMPLETION(cpu_running);
54 * A logcal cpu mask containing only one VPE per core to
55 * reduce the number of IPIs on large MT systems.
57 cpumask_t cpu_foreign_map[NR_CPUS] __read_mostly;
58 EXPORT_SYMBOL(cpu_foreign_map);
60 /* representing cpus for which sibling maps can be computed */
61 static cpumask_t cpu_sibling_setup_map;
63 /* representing cpus for which core maps can be computed */
64 static cpumask_t cpu_core_setup_map;
66 struct secondary_data cpuboot_data;
67 static DEFINE_PER_CPU(int, cpu_state);
74 static const char *ipi_types[NR_IPI] __tracepoint_string = {
75 [IPI_RESCHEDULE] = "Rescheduling interrupts",
76 [IPI_CALL_FUNCTION] = "Function call interrupts",
79 void show_ipi_list(struct seq_file *p, int prec)
83 for (i = 0; i < NR_IPI; i++) {
84 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i, prec >= 4 ? " " : "");
85 for_each_online_cpu(cpu)
86 seq_printf(p, "%10u ", per_cpu(irq_stat, cpu).ipi_irqs[i]);
87 seq_printf(p, " LoongArch %d %s\n", i + 1, ipi_types[i]);
91 /* Send mailbox buffer via Mail_Send */
92 static void csr_mail_send(uint64_t data, int cpu, int mailbox)
96 /* Send high 32 bits */
97 val = IOCSR_MBUF_SEND_BLOCKING;
98 val |= (IOCSR_MBUF_SEND_BOX_HI(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
99 val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
100 val |= (data & IOCSR_MBUF_SEND_H32_MASK);
101 iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
103 /* Send low 32 bits */
104 val = IOCSR_MBUF_SEND_BLOCKING;
105 val |= (IOCSR_MBUF_SEND_BOX_LO(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
106 val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
107 val |= (data << IOCSR_MBUF_SEND_BUF_SHIFT);
108 iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
111 static u32 ipi_read_clear(int cpu)
115 /* Load the ipi register to figure out what we're supposed to do */
116 action = iocsr_read32(LOONGARCH_IOCSR_IPI_STATUS);
117 /* Clear the ipi register to clear the interrupt */
118 iocsr_write32(action, LOONGARCH_IOCSR_IPI_CLEAR);
124 static void ipi_write_action(int cpu, u32 action)
126 unsigned int irq = 0;
128 while ((irq = ffs(action))) {
129 uint32_t val = IOCSR_IPI_SEND_BLOCKING;
132 val |= (cpu << IOCSR_IPI_SEND_CPU_SHIFT);
133 iocsr_write32(val, LOONGARCH_IOCSR_IPI_SEND);
134 action &= ~BIT(irq - 1);
138 void loongson_send_ipi_single(int cpu, unsigned int action)
140 ipi_write_action(cpu_logical_map(cpu), (u32)action);
143 void loongson_send_ipi_mask(const struct cpumask *mask, unsigned int action)
147 for_each_cpu(i, mask)
148 ipi_write_action(cpu_logical_map(i), (u32)action);
152 * This function sends a 'reschedule' IPI to another CPU.
153 * it goes straight through and wastes no time serializing
154 * anything. Worst case is that we lose a reschedule ...
156 void arch_smp_send_reschedule(int cpu)
158 loongson_send_ipi_single(cpu, SMP_RESCHEDULE);
160 EXPORT_SYMBOL_GPL(arch_smp_send_reschedule);
162 irqreturn_t loongson_ipi_interrupt(int irq, void *dev)
165 unsigned int cpu = smp_processor_id();
167 action = ipi_read_clear(cpu_logical_map(cpu));
169 if (action & SMP_RESCHEDULE) {
171 per_cpu(irq_stat, cpu).ipi_irqs[IPI_RESCHEDULE]++;
174 if (action & SMP_CALL_FUNCTION) {
175 generic_smp_call_function_interrupt();
176 per_cpu(irq_stat, cpu).ipi_irqs[IPI_CALL_FUNCTION]++;
182 static void __init fdt_smp_setup(void)
185 unsigned int cpu, cpuid;
186 struct device_node *node = NULL;
188 for_each_of_cpu_node(node) {
189 if (!of_device_is_available(node))
192 cpuid = of_get_cpu_hwid(node, 0);
193 if (cpuid >= nr_cpu_ids)
196 if (cpuid == loongson_sysconf.boot_cpu_id) {
200 cpu = cpumask_next_zero(-1, cpu_present_mask);
204 set_cpu_possible(cpu, true);
205 set_cpu_present(cpu, true);
206 __cpu_number_map[cpuid] = cpu;
207 __cpu_logical_map[cpu] = cpuid;
210 loongson_sysconf.nr_cpus = num_processors;
211 set_bit(0, loongson_sysconf.cores_io_master);
215 void __init loongson_smp_setup(void)
219 if (loongson_sysconf.cores_per_package == 0)
220 loongson_sysconf.cores_per_package = num_processors;
222 cpu_data[0].core = cpu_logical_map(0) % loongson_sysconf.cores_per_package;
223 cpu_data[0].package = cpu_logical_map(0) / loongson_sysconf.cores_per_package;
225 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
226 pr_info("Detected %i available CPU(s)\n", loongson_sysconf.nr_cpus);
229 void __init loongson_prepare_cpus(unsigned int max_cpus)
233 parse_acpi_topology();
235 for (i = 0; i < loongson_sysconf.nr_cpus; i++) {
236 set_cpu_present(i, true);
237 csr_mail_send(0, __cpu_logical_map[i], 0);
238 cpu_data[i].global_id = __cpu_logical_map[i];
241 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
245 * Setup the PC, SP, and TP of a secondary processor and start it running!
247 void loongson_boot_secondary(int cpu, struct task_struct *idle)
251 pr_info("Booting CPU#%d...\n", cpu);
253 entry = __pa_symbol((unsigned long)&smpboot_entry);
254 cpuboot_data.stack = (unsigned long)__KSTK_TOS(idle);
255 cpuboot_data.thread_info = (unsigned long)task_thread_info(idle);
257 csr_mail_send(entry, cpu_logical_map(cpu), 0);
259 loongson_send_ipi_single(cpu, SMP_BOOT_CPU);
263 * SMP init and finish on secondary CPUs
265 void loongson_init_secondary(void)
267 unsigned int cpu = smp_processor_id();
268 unsigned int imask = ECFGF_IP0 | ECFGF_IP1 | ECFGF_IP2 |
269 ECFGF_IPI | ECFGF_PMC | ECFGF_TIMER;
271 change_csr_ecfg(ECFG0_IM, imask);
273 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
278 per_cpu(cpu_state, cpu) = CPU_ONLINE;
279 cpu_data[cpu].package =
280 cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
281 cpu_data[cpu].core = pptt_enabled ? cpu_data[cpu].core :
282 cpu_logical_map(cpu) % loongson_sysconf.cores_per_package;
285 void loongson_smp_finish(void)
288 iocsr_write64(0, LOONGARCH_IOCSR_MBUF0);
289 pr_info("CPU#%d finished\n", smp_processor_id());
292 #ifdef CONFIG_HOTPLUG_CPU
294 int loongson_cpu_disable(void)
297 unsigned int cpu = smp_processor_id();
303 numa_remove_cpu(cpu);
305 set_cpu_online(cpu, false);
306 calculate_cpu_foreign_map();
307 local_irq_save(flags);
308 irq_migrate_all_off_this_cpu();
309 clear_csr_ecfg(ECFG0_IM);
310 local_irq_restore(flags);
311 local_flush_tlb_all();
316 void loongson_cpu_die(unsigned int cpu)
318 while (per_cpu(cpu_state, cpu) != CPU_DEAD)
324 void __noreturn arch_cpu_idle_dead(void)
326 register uint64_t addr;
327 register void (*init_fn)(void);
331 set_csr_ecfg(ECFGF_IPI);
332 __this_cpu_write(cpu_state, CPU_DEAD);
336 __asm__ __volatile__("idle 0\n\t");
337 addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
340 init_fn = (void *)TO_CACHE(addr);
341 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
354 static int loongson_ipi_suspend(void)
359 static void loongson_ipi_resume(void)
361 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
364 static struct syscore_ops loongson_ipi_syscore_ops = {
365 .resume = loongson_ipi_resume,
366 .suspend = loongson_ipi_suspend,
370 * Enable boot cpu ipi before enabling nonboot cpus
371 * during syscore_resume.
373 static int __init ipi_pm_init(void)
375 register_syscore_ops(&loongson_ipi_syscore_ops);
379 core_initcall(ipi_pm_init);
382 static inline void set_cpu_sibling_map(int cpu)
386 cpumask_set_cpu(cpu, &cpu_sibling_setup_map);
388 for_each_cpu(i, &cpu_sibling_setup_map) {
389 if (cpus_are_siblings(cpu, i)) {
390 cpumask_set_cpu(i, &cpu_sibling_map[cpu]);
391 cpumask_set_cpu(cpu, &cpu_sibling_map[i]);
396 static inline void set_cpu_core_map(int cpu)
400 cpumask_set_cpu(cpu, &cpu_core_setup_map);
402 for_each_cpu(i, &cpu_core_setup_map) {
403 if (cpu_data[cpu].package == cpu_data[i].package) {
404 cpumask_set_cpu(i, &cpu_core_map[cpu]);
405 cpumask_set_cpu(cpu, &cpu_core_map[i]);
411 * Calculate a new cpu_foreign_map mask whenever a
412 * new cpu appears or disappears.
414 void calculate_cpu_foreign_map(void)
416 int i, k, core_present;
417 cpumask_t temp_foreign_map;
419 /* Re-calculate the mask */
420 cpumask_clear(&temp_foreign_map);
421 for_each_online_cpu(i) {
423 for_each_cpu(k, &temp_foreign_map)
424 if (cpus_are_siblings(i, k))
427 cpumask_set_cpu(i, &temp_foreign_map);
430 for_each_online_cpu(i)
431 cpumask_andnot(&cpu_foreign_map[i],
432 &temp_foreign_map, &cpu_sibling_map[i]);
435 /* Preload SMP state for boot cpu */
436 void smp_prepare_boot_cpu(void)
438 unsigned int cpu, node, rr_node;
440 set_cpu_possible(0, true);
441 set_cpu_online(0, true);
442 set_my_cpu_offset(per_cpu_offset(0));
444 rr_node = first_node(node_online_map);
445 for_each_possible_cpu(cpu) {
446 node = early_cpu_to_node(cpu);
449 * The mapping between present cpus and nodes has been
450 * built during MADT and SRAT parsing.
452 * If possible cpus = present cpus here, early_cpu_to_node
453 * will return valid node.
455 * If possible cpus > present cpus here (e.g. some possible
456 * cpus will be added by cpu-hotplug later), for possible but
457 * not present cpus, early_cpu_to_node will return NUMA_NO_NODE,
458 * and we just map them to online nodes in round-robin way.
459 * Once hotplugged, new correct mapping will be built for them.
461 if (node != NUMA_NO_NODE)
462 set_cpu_numa_node(cpu, node);
464 set_cpu_numa_node(cpu, rr_node);
465 rr_node = next_node_in(rr_node, node_online_map);
470 /* called from main before smp_init() */
471 void __init smp_prepare_cpus(unsigned int max_cpus)
473 init_new_context(current, &init_mm);
474 current_thread_info()->cpu = 0;
475 loongson_prepare_cpus(max_cpus);
476 set_cpu_sibling_map(0);
478 calculate_cpu_foreign_map();
479 #ifndef CONFIG_HOTPLUG_CPU
480 init_cpu_present(cpu_possible_mask);
484 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
486 loongson_boot_secondary(cpu, tidle);
488 /* Wait for CPU to start and be ready to sync counters */
489 if (!wait_for_completion_timeout(&cpu_starting,
490 msecs_to_jiffies(5000))) {
491 pr_crit("CPU%u: failed to start\n", cpu);
495 /* Wait for CPU to finish startup & mark itself online before return */
496 wait_for_completion(&cpu_running);
502 * First C code run on the secondary CPUs after being started up by
505 asmlinkage void start_secondary(void)
510 cpu = raw_smp_processor_id();
511 set_my_cpu_offset(per_cpu_offset(cpu));
514 constant_clockevent_init();
515 loongson_init_secondary();
517 set_cpu_sibling_map(cpu);
518 set_cpu_core_map(cpu);
520 notify_cpu_starting(cpu);
522 /* Notify boot CPU that we're starting */
523 complete(&cpu_starting);
525 /* The CPU is running, now mark it online */
526 set_cpu_online(cpu, true);
528 calculate_cpu_foreign_map();
531 * Notify boot CPU that we're up & online and it can safely return
534 complete(&cpu_running);
537 * irq will be enabled in loongson_smp_finish(), enabling it too
538 * early is dangerous.
540 WARN_ON_ONCE(!irqs_disabled());
541 loongson_smp_finish();
543 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
546 void __init smp_cpus_done(unsigned int max_cpus)
550 static void stop_this_cpu(void *dummy)
552 set_cpu_online(smp_processor_id(), false);
553 calculate_cpu_foreign_map();
558 void smp_send_stop(void)
560 smp_call_function(stop_this_cpu, NULL, 0);
563 #ifdef CONFIG_PROFILING
564 int setup_profiling_timer(unsigned int multiplier)
570 static void flush_tlb_all_ipi(void *info)
572 local_flush_tlb_all();
575 void flush_tlb_all(void)
577 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
580 static void flush_tlb_mm_ipi(void *mm)
582 local_flush_tlb_mm((struct mm_struct *)mm);
585 void flush_tlb_mm(struct mm_struct *mm)
587 if (atomic_read(&mm->mm_users) == 0)
588 return; /* happens as a result of exit_mmap() */
592 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
593 on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
597 for_each_online_cpu(cpu) {
598 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
599 cpu_context(cpu, mm) = 0;
601 local_flush_tlb_mm(mm);
607 struct flush_tlb_data {
608 struct vm_area_struct *vma;
613 static void flush_tlb_range_ipi(void *info)
615 struct flush_tlb_data *fd = info;
617 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
620 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
622 struct mm_struct *mm = vma->vm_mm;
625 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
626 struct flush_tlb_data fd = {
632 on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
636 for_each_online_cpu(cpu) {
637 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
638 cpu_context(cpu, mm) = 0;
640 local_flush_tlb_range(vma, start, end);
645 static void flush_tlb_kernel_range_ipi(void *info)
647 struct flush_tlb_data *fd = info;
649 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
652 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
654 struct flush_tlb_data fd = {
659 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
662 static void flush_tlb_page_ipi(void *info)
664 struct flush_tlb_data *fd = info;
666 local_flush_tlb_page(fd->vma, fd->addr1);
669 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
672 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
673 struct flush_tlb_data fd = {
678 on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
682 for_each_online_cpu(cpu) {
683 if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
684 cpu_context(cpu, vma->vm_mm) = 0;
686 local_flush_tlb_page(vma, page);
690 EXPORT_SYMBOL(flush_tlb_page);
692 static void flush_tlb_one_ipi(void *info)
694 unsigned long vaddr = (unsigned long) info;
696 local_flush_tlb_one(vaddr);
699 void flush_tlb_one(unsigned long vaddr)
701 on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
703 EXPORT_SYMBOL(flush_tlb_one);