1 // SPDX-License-Identifier: GPL-2.0-only
3 * SMP initialisation and IPI support
4 * Based on arch/arm64/kernel/smp.c
6 * Copyright (C) 2012 ARM Ltd.
7 * Copyright (C) 2015 Regents of the University of California
8 * Copyright (C) 2017 SiFive
11 #include <linux/arch_topology.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/notifier.h>
19 #include <linux/cpu.h>
20 #include <linux/percpu.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/irq.h>
25 #include <linux/sched/task_stack.h>
26 #include <linux/sched/mm.h>
27 #include <asm/cpu_ops.h>
29 #include <asm/mmu_context.h>
31 #include <asm/tlbflush.h>
32 #include <asm/sections.h>
34 #include <uapi/asm/hwcap.h>
35 #include <asm/vector.h>
39 static DECLARE_COMPLETION(cpu_running);
41 void __init smp_prepare_boot_cpu(void)
45 void __init smp_prepare_cpus(unsigned int max_cpus)
49 unsigned int curr_cpuid;
53 curr_cpuid = smp_processor_id();
54 store_cpu_topology(curr_cpuid);
55 numa_store_cpu_info(curr_cpuid);
56 numa_add_cpu(curr_cpuid);
58 /* This covers non-smp usecase mandated by "nosmp" option */
62 for_each_possible_cpu(cpuid) {
63 if (cpuid == curr_cpuid)
65 if (cpu_ops[cpuid]->cpu_prepare) {
66 ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
70 set_cpu_present(cpuid, true);
71 numa_store_cpu_info(cpuid);
75 void __init setup_smp(void)
77 struct device_node *dn;
79 bool found_boot_cpu = false;
85 for_each_of_cpu_node(dn) {
86 rc = riscv_of_processor_hartid(dn, &hart);
90 if (hart == cpuid_to_hartid_map(0)) {
91 BUG_ON(found_boot_cpu);
93 early_map_cpu_to_node(0, of_node_to_nid(dn));
96 if (cpuid >= NR_CPUS) {
97 pr_warn("Invalid cpuid [%d] for hartid [%lu]\n",
102 cpuid_to_hartid_map(cpuid) = hart;
103 early_map_cpu_to_node(cpuid, of_node_to_nid(dn));
107 BUG_ON(!found_boot_cpu);
109 if (cpuid > nr_cpu_ids)
110 pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
113 for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) {
114 if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {
116 set_cpu_possible(cpuid, true);
121 static int start_secondary_cpu(int cpu, struct task_struct *tidle)
123 if (cpu_ops[cpu]->cpu_start)
124 return cpu_ops[cpu]->cpu_start(cpu, tidle);
129 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
132 tidle->thread_info.cpu = cpu;
134 ret = start_secondary_cpu(cpu, tidle);
136 wait_for_completion_timeout(&cpu_running,
137 msecs_to_jiffies(1000));
139 if (!cpu_online(cpu)) {
140 pr_crit("CPU%u: failed to come online\n", cpu);
144 pr_crit("CPU%u: failed to start\n", cpu);
150 void __init smp_cpus_done(unsigned int max_cpus)
155 * C entry point for a secondary processor.
157 asmlinkage __visible void smp_callin(void)
159 struct mm_struct *mm = &init_mm;
160 unsigned int curr_cpuid = smp_processor_id();
162 /* All kernel threads share the same mm context. */
164 current->active_mm = mm;
168 store_cpu_topology(curr_cpuid);
169 notify_cpu_starting(curr_cpuid);
170 numa_add_cpu(curr_cpuid);
171 set_cpu_online(curr_cpuid, 1);
172 probe_vendor_features(curr_cpuid);
175 if (riscv_v_setup_vsize())
176 elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
180 * Remote TLB flushes are ignored while the CPU is offline, so emit
181 * a local TLB flush right now just in case.
183 local_flush_tlb_all();
184 complete(&cpu_running);
186 * Disable preemption before enabling interrupts, so we don't try to
187 * schedule a CPU that hasn't actually started yet.
190 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);