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>
32 #include <asm/mmu_context.h>
34 #include <asm/tlbflush.h>
35 #include <asm/sections.h>
37 #include <uapi/asm/hwcap.h>
38 #include <asm/vector.h>
42 static DECLARE_COMPLETION(cpu_running);
44 void __init smp_prepare_cpus(unsigned int max_cpus)
47 unsigned int curr_cpuid;
51 curr_cpuid = smp_processor_id();
52 store_cpu_topology(curr_cpuid);
53 numa_store_cpu_info(curr_cpuid);
54 numa_add_cpu(curr_cpuid);
56 /* This covers non-smp usecase mandated by "nosmp" option */
60 for_each_possible_cpu(cpuid) {
61 if (cpuid == curr_cpuid)
63 set_cpu_present(cpuid, true);
64 numa_store_cpu_info(cpuid);
69 static unsigned int cpu_count = 1;
71 static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const unsigned long end)
74 static bool found_boot_cpu;
75 struct acpi_madt_rintc *processor = (struct acpi_madt_rintc *)header;
78 * Each RINTC structure in MADT will have a flag. If ACPI_MADT_ENABLED
79 * bit in the flag is not enabled, it means OS should not try to enable
80 * the cpu to which RINTC belongs.
82 if (!(processor->flags & ACPI_MADT_ENABLED))
85 if (BAD_MADT_ENTRY(processor, end))
88 acpi_table_print_madt_entry(&header->common);
90 hart = processor->hart_id;
91 if (hart == INVALID_HARTID) {
92 pr_warn("Invalid hartid\n");
96 if (hart == cpuid_to_hartid_map(0)) {
97 BUG_ON(found_boot_cpu);
98 found_boot_cpu = true;
99 early_map_cpu_to_node(0, acpi_numa_get_nid(cpu_count));
103 if (cpu_count >= NR_CPUS) {
104 pr_warn("NR_CPUS is too small for the number of ACPI tables.\n");
108 cpuid_to_hartid_map(cpu_count) = hart;
109 early_map_cpu_to_node(cpu_count, acpi_numa_get_nid(cpu_count));
115 static void __init acpi_parse_and_init_cpus(void)
117 acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_rintc, 0);
120 #define acpi_parse_and_init_cpus(...) do { } while (0)
123 static void __init of_parse_and_init_cpus(void)
125 struct device_node *dn;
127 bool found_boot_cpu = false;
131 for_each_of_cpu_node(dn) {
132 rc = riscv_early_of_processor_hartid(dn, &hart);
136 if (hart == cpuid_to_hartid_map(0)) {
137 BUG_ON(found_boot_cpu);
139 early_map_cpu_to_node(0, of_node_to_nid(dn));
142 if (cpuid >= NR_CPUS) {
143 pr_warn("Invalid cpuid [%d] for hartid [%lu]\n",
148 cpuid_to_hartid_map(cpuid) = hart;
149 early_map_cpu_to_node(cpuid, of_node_to_nid(dn));
153 BUG_ON(!found_boot_cpu);
155 if (cpuid > nr_cpu_ids)
156 pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
160 void __init setup_smp(void)
167 of_parse_and_init_cpus();
169 acpi_parse_and_init_cpus();
171 for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++)
172 if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID)
173 set_cpu_possible(cpuid, true);
176 static int start_secondary_cpu(int cpu, struct task_struct *tidle)
178 if (cpu_ops->cpu_start)
179 return cpu_ops->cpu_start(cpu, tidle);
184 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
187 tidle->thread_info.cpu = cpu;
189 ret = start_secondary_cpu(cpu, tidle);
191 wait_for_completion_timeout(&cpu_running,
192 msecs_to_jiffies(1000));
194 if (!cpu_online(cpu)) {
195 pr_crit("CPU%u: failed to come online\n", cpu);
199 pr_crit("CPU%u: failed to start\n", cpu);
205 void __init smp_cpus_done(unsigned int max_cpus)
210 * C entry point for a secondary processor.
212 asmlinkage __visible void smp_callin(void)
214 struct mm_struct *mm = &init_mm;
215 unsigned int curr_cpuid = smp_processor_id();
217 /* All kernel threads share the same mm context. */
219 current->active_mm = mm;
221 store_cpu_topology(curr_cpuid);
222 notify_cpu_starting(curr_cpuid);
226 numa_add_cpu(curr_cpuid);
227 set_cpu_online(curr_cpuid, 1);
230 if (riscv_v_setup_vsize())
231 elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
234 riscv_user_isa_enable();
237 * Remote TLB flushes are ignored while the CPU is offline, so emit
238 * a local TLB flush right now just in case.
240 local_flush_tlb_all();
241 complete(&cpu_running);
243 * Disable preemption before enabling interrupts, so we don't try to
244 * schedule a CPU that hasn't actually started yet.
247 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);