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/cacheflush.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;
102 if (cpu_count >= NR_CPUS) {
103 pr_warn("NR_CPUS is too small for the number of ACPI tables.\n");
107 cpuid_to_hartid_map(cpu_count) = hart;
113 static void __init acpi_parse_and_init_cpus(void)
115 acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_rintc, 0);
118 #define acpi_parse_and_init_cpus(...) do { } while (0)
121 static void __init of_parse_and_init_cpus(void)
123 struct device_node *dn;
125 bool found_boot_cpu = false;
129 for_each_of_cpu_node(dn) {
130 rc = riscv_early_of_processor_hartid(dn, &hart);
134 if (hart == cpuid_to_hartid_map(0)) {
135 BUG_ON(found_boot_cpu);
137 early_map_cpu_to_node(0, of_node_to_nid(dn));
140 if (cpuid >= NR_CPUS) {
141 pr_warn("Invalid cpuid [%d] for hartid [%lu]\n",
146 cpuid_to_hartid_map(cpuid) = hart;
147 early_map_cpu_to_node(cpuid, of_node_to_nid(dn));
151 BUG_ON(!found_boot_cpu);
153 if (cpuid > nr_cpu_ids)
154 pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
158 void __init setup_smp(void)
165 of_parse_and_init_cpus();
167 acpi_parse_and_init_cpus();
169 for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++)
170 if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID)
171 set_cpu_possible(cpuid, true);
174 static int start_secondary_cpu(int cpu, struct task_struct *tidle)
176 if (cpu_ops->cpu_start)
177 return cpu_ops->cpu_start(cpu, tidle);
182 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
185 tidle->thread_info.cpu = cpu;
187 ret = start_secondary_cpu(cpu, tidle);
189 wait_for_completion_timeout(&cpu_running,
190 msecs_to_jiffies(1000));
192 if (!cpu_online(cpu)) {
193 pr_crit("CPU%u: failed to come online\n", cpu);
197 pr_crit("CPU%u: failed to start\n", cpu);
203 void __init smp_cpus_done(unsigned int max_cpus)
208 * C entry point for a secondary processor.
210 asmlinkage __visible void smp_callin(void)
212 struct mm_struct *mm = &init_mm;
213 unsigned int curr_cpuid = smp_processor_id();
217 * Return as early as possible so the hart with a mismatching
220 if (riscv_v_setup_vsize())
224 /* All kernel threads share the same mm context. */
226 current->active_mm = mm;
228 store_cpu_topology(curr_cpuid);
229 notify_cpu_starting(curr_cpuid);
233 numa_add_cpu(curr_cpuid);
234 set_cpu_online(curr_cpuid, true);
236 riscv_user_isa_enable();
239 * Remote cache and TLB flushes are ignored while the CPU is offline,
240 * so flush them both right now just in case.
242 local_flush_icache_all();
243 local_flush_tlb_all();
244 complete(&cpu_running);
246 * Disable preemption before enabling interrupts, so we don't try to
247 * schedule a CPU that hasn't actually started yet.
250 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);