1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SMP initialisation and IPI support 4 * Based on arch/arm64/kernel/smp.c 5 * 6 * Copyright (C) 2012 ARM Ltd. 7 * Copyright (C) 2015 Regents of the University of California 8 * Copyright (C) 2017 SiFive 9 */ 10 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> 16 #include <linux/mm.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> 25 #include <linux/of.h> 26 #include <linux/sched/task_stack.h> 27 #include <linux/sched/mm.h> 28 29 #include <asm/cpufeature.h> 30 #include <asm/cpu_ops.h> 31 #include <asm/cpufeature.h> 32 #include <asm/irq.h> 33 #include <asm/mmu_context.h> 34 #include <asm/numa.h> 35 #include <asm/tlbflush.h> 36 #include <asm/sections.h> 37 #include <asm/smp.h> 38 #include <uapi/asm/hwcap.h> 39 #include <asm/vector.h> 40 41 #include "head.h" 42 43 static DECLARE_COMPLETION(cpu_running); 44 45 void __init smp_prepare_cpus(unsigned int max_cpus) 46 { 47 int cpuid; 48 unsigned int curr_cpuid; 49 50 init_cpu_topology(); 51 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); 56 57 /* This covers non-smp usecase mandated by "nosmp" option */ 58 if (max_cpus == 0) 59 return; 60 61 for_each_possible_cpu(cpuid) { 62 if (cpuid == curr_cpuid) 63 continue; 64 set_cpu_present(cpuid, true); 65 numa_store_cpu_info(cpuid); 66 } 67 } 68 69 #ifdef CONFIG_ACPI 70 static unsigned int cpu_count = 1; 71 72 static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const unsigned long end) 73 { 74 unsigned long hart; 75 static bool found_boot_cpu; 76 struct acpi_madt_rintc *processor = (struct acpi_madt_rintc *)header; 77 78 /* 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. 82 */ 83 if (!(processor->flags & ACPI_MADT_ENABLED)) 84 return 0; 85 86 if (BAD_MADT_ENTRY(processor, end)) 87 return -EINVAL; 88 89 acpi_table_print_madt_entry(&header->common); 90 91 hart = processor->hart_id; 92 if (hart == INVALID_HARTID) { 93 pr_warn("Invalid hartid\n"); 94 return 0; 95 } 96 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)); 101 return 0; 102 } 103 104 if (cpu_count >= NR_CPUS) { 105 pr_warn("NR_CPUS is too small for the number of ACPI tables.\n"); 106 return 0; 107 } 108 109 cpuid_to_hartid_map(cpu_count) = hart; 110 early_map_cpu_to_node(cpu_count, acpi_numa_get_nid(cpu_count)); 111 cpu_count++; 112 113 return 0; 114 } 115 116 static void __init acpi_parse_and_init_cpus(void) 117 { 118 acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_rintc, 0); 119 } 120 #else 121 #define acpi_parse_and_init_cpus(...) do { } while (0) 122 #endif 123 124 static void __init of_parse_and_init_cpus(void) 125 { 126 struct device_node *dn; 127 unsigned long hart; 128 bool found_boot_cpu = false; 129 int cpuid = 1; 130 int rc; 131 132 for_each_of_cpu_node(dn) { 133 rc = riscv_early_of_processor_hartid(dn, &hart); 134 if (rc < 0) 135 continue; 136 137 if (hart == cpuid_to_hartid_map(0)) { 138 BUG_ON(found_boot_cpu); 139 found_boot_cpu = 1; 140 early_map_cpu_to_node(0, of_node_to_nid(dn)); 141 continue; 142 } 143 if (cpuid >= NR_CPUS) { 144 pr_warn("Invalid cpuid [%d] for hartid [%lu]\n", 145 cpuid, hart); 146 continue; 147 } 148 149 cpuid_to_hartid_map(cpuid) = hart; 150 early_map_cpu_to_node(cpuid, of_node_to_nid(dn)); 151 cpuid++; 152 } 153 154 BUG_ON(!found_boot_cpu); 155 156 if (cpuid > nr_cpu_ids) 157 pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n", 158 cpuid, nr_cpu_ids); 159 } 160 161 void __init setup_smp(void) 162 { 163 int cpuid; 164 165 cpu_set_ops(); 166 167 if (acpi_disabled) 168 of_parse_and_init_cpus(); 169 else 170 acpi_parse_and_init_cpus(); 171 172 for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) 173 if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) 174 set_cpu_possible(cpuid, true); 175 } 176 177 static int start_secondary_cpu(int cpu, struct task_struct *tidle) 178 { 179 if (cpu_ops->cpu_start) 180 return cpu_ops->cpu_start(cpu, tidle); 181 182 return -EOPNOTSUPP; 183 } 184 185 int __cpu_up(unsigned int cpu, struct task_struct *tidle) 186 { 187 int ret = 0; 188 tidle->thread_info.cpu = cpu; 189 190 ret = start_secondary_cpu(cpu, tidle); 191 if (!ret) { 192 wait_for_completion_timeout(&cpu_running, 193 msecs_to_jiffies(1000)); 194 195 if (!cpu_online(cpu)) { 196 pr_crit("CPU%u: failed to come online\n", cpu); 197 ret = -EIO; 198 } 199 } else { 200 pr_crit("CPU%u: failed to start\n", cpu); 201 } 202 203 return ret; 204 } 205 206 void __init smp_cpus_done(unsigned int max_cpus) 207 { 208 } 209 210 /* 211 * C entry point for a secondary processor. 212 */ 213 asmlinkage __visible void smp_callin(void) 214 { 215 struct mm_struct *mm = &init_mm; 216 unsigned int curr_cpuid = smp_processor_id(); 217 218 /* All kernel threads share the same mm context. */ 219 mmgrab(mm); 220 current->active_mm = mm; 221 222 store_cpu_topology(curr_cpuid); 223 notify_cpu_starting(curr_cpuid); 224 225 riscv_ipi_enable(); 226 227 numa_add_cpu(curr_cpuid); 228 set_cpu_online(curr_cpuid, 1); 229 230 if (has_vector()) { 231 if (riscv_v_setup_vsize()) 232 elf_hwcap &= ~COMPAT_HWCAP_ISA_V; 233 } 234 235 riscv_user_isa_enable(); 236 237 /* 238 * Remote TLB flushes are ignored while the CPU is offline, so emit 239 * a local TLB flush right now just in case. 240 */ 241 local_flush_tlb_all(); 242 complete(&cpu_running); 243 /* 244 * Disable preemption before enabling interrupts, so we don't try to 245 * schedule a CPU that hasn't actually started yet. 246 */ 247 local_irq_enable(); 248 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 249 } 250