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/acpi.h>
12 #include <linux/arch_topology.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/notifier.h>
20 #include <linux/cpu.h>
21 #include <linux/percpu.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <linux/irq.h>
26 #include <linux/sched/task_stack.h>
27 #include <linux/sched/mm.h>
29 #include <asm/cpufeature.h>
30 #include <asm/cpu_ops.h>
31 #include <asm/cpufeature.h>
33 #include <asm/mmu_context.h>
35 #include <asm/tlbflush.h>
36 #include <asm/sections.h>
38 #include <uapi/asm/hwcap.h>
39 #include <asm/vector.h>
43 static DECLARE_COMPLETION(cpu_running);
45 void __init smp_prepare_cpus(unsigned int max_cpus)
48 unsigned int curr_cpuid;
52 curr_cpuid = smp_processor_id();
53 store_cpu_topology(curr_cpuid);
54 numa_store_cpu_info(curr_cpuid);
55 numa_add_cpu(curr_cpuid);
57 /* This covers non-smp usecase mandated by "nosmp" option */
61 for_each_possible_cpu(cpuid) {
62 if (cpuid == curr_cpuid)
64 set_cpu_present(cpuid, true);
65 numa_store_cpu_info(cpuid);
70 static unsigned int cpu_count = 1;
72 static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const unsigned long end)
75 static bool found_boot_cpu;
76 struct acpi_madt_rintc *processor = (struct acpi_madt_rintc *)header;
79 * Each RINTC structure in MADT will have a flag. If ACPI_MADT_ENABLED
80 * bit in the flag is not enabled, it means OS should not try to enable
81 * the cpu to which RINTC belongs.
83 if (!(processor->flags & ACPI_MADT_ENABLED))
86 if (BAD_MADT_ENTRY(processor, end))
89 acpi_table_print_madt_entry(&header->common);
91 hart = processor->hart_id;
92 if (hart == INVALID_HARTID) {
93 pr_warn("Invalid hartid\n");
97 if (hart == cpuid_to_hartid_map(0)) {
98 BUG_ON(found_boot_cpu);
99 found_boot_cpu = true;
100 early_map_cpu_to_node(0, acpi_numa_get_nid(cpu_count));
104 if (cpu_count >= NR_CPUS) {
105 pr_warn("NR_CPUS is too small for the number of ACPI tables.\n");
109 cpuid_to_hartid_map(cpu_count) = hart;
110 early_map_cpu_to_node(cpu_count, acpi_numa_get_nid(cpu_count));
116 static void __init acpi_parse_and_init_cpus(void)
118 acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_rintc, 0);
121 #define acpi_parse_and_init_cpus(...) do { } while (0)
124 static void __init of_parse_and_init_cpus(void)
126 struct device_node *dn;
128 bool found_boot_cpu = false;
132 for_each_of_cpu_node(dn) {
133 rc = riscv_early_of_processor_hartid(dn, &hart);
137 if (hart == cpuid_to_hartid_map(0)) {
138 BUG_ON(found_boot_cpu);
140 early_map_cpu_to_node(0, of_node_to_nid(dn));
143 if (cpuid >= NR_CPUS) {
144 pr_warn("Invalid cpuid [%d] for hartid [%lu]\n",
149 cpuid_to_hartid_map(cpuid) = hart;
150 early_map_cpu_to_node(cpuid, of_node_to_nid(dn));
154 BUG_ON(!found_boot_cpu);
156 if (cpuid > nr_cpu_ids)
157 pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
161 void __init setup_smp(void)
168 of_parse_and_init_cpus();
170 acpi_parse_and_init_cpus();
172 for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++)
173 if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID)
174 set_cpu_possible(cpuid, true);
177 static int start_secondary_cpu(int cpu, struct task_struct *tidle)
179 if (cpu_ops->cpu_start)
180 return cpu_ops->cpu_start(cpu, tidle);
185 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
188 tidle->thread_info.cpu = cpu;
190 ret = start_secondary_cpu(cpu, tidle);
192 wait_for_completion_timeout(&cpu_running,
193 msecs_to_jiffies(1000));
195 if (!cpu_online(cpu)) {
196 pr_crit("CPU%u: failed to come online\n", cpu);
200 pr_crit("CPU%u: failed to start\n", cpu);
206 void __init smp_cpus_done(unsigned int max_cpus)
211 * C entry point for a secondary processor.
213 asmlinkage __visible void smp_callin(void)
215 struct mm_struct *mm = &init_mm;
216 unsigned int curr_cpuid = smp_processor_id();
218 /* All kernel threads share the same mm context. */
220 current->active_mm = mm;
222 store_cpu_topology(curr_cpuid);
223 notify_cpu_starting(curr_cpuid);
227 numa_add_cpu(curr_cpuid);
228 set_cpu_online(curr_cpuid, 1);
231 if (riscv_v_setup_vsize())
232 elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
235 riscv_user_isa_enable();
238 * Remote TLB flushes are ignored while the CPU is offline, so emit
239 * a local TLB flush right now just in case.
241 local_flush_tlb_all();
242 complete(&cpu_running);
244 * Disable preemption before enabling interrupts, so we don't try to
245 * schedule a CPU that hasn't actually started yet.
248 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);