| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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/arch_topology.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/mm.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> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/sched/task_stack.h> |
| 26 | #include <linux/sched/mm.h> |
| 27 | #include <asm/irq.h> |
| 28 | #include <asm/mmu_context.h> |
| 29 | #include <asm/tlbflush.h> |
| 30 | #include <asm/sections.h> |
| 31 | #include <asm/sbi.h> |
| 32 | #include <asm/smp.h> |
| 33 | |
| 34 | #include "head.h" |
| 35 | |
| 36 | void *__cpu_up_stack_pointer[NR_CPUS]; |
| 37 | void *__cpu_up_task_pointer[NR_CPUS]; |
| 38 | static DECLARE_COMPLETION(cpu_running); |
| 39 | |
| 40 | void __init smp_prepare_boot_cpu(void) |
| 41 | { |
| 42 | init_cpu_topology(); |
| 43 | } |
| 44 | |
| 45 | void __init smp_prepare_cpus(unsigned int max_cpus) |
| 46 | { |
| 47 | int cpuid; |
| 48 | |
| 49 | store_cpu_topology(smp_processor_id()); |
| 50 | |
| 51 | /* This covers non-smp usecase mandated by "nosmp" option */ |
| 52 | if (max_cpus == 0) |
| 53 | return; |
| 54 | |
| 55 | for_each_possible_cpu(cpuid) { |
| 56 | if (cpuid == smp_processor_id()) |
| 57 | continue; |
| 58 | set_cpu_present(cpuid, true); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void __init setup_smp(void) |
| 63 | { |
| 64 | struct device_node *dn; |
| 65 | int hart; |
| 66 | bool found_boot_cpu = false; |
| 67 | int cpuid = 1; |
| 68 | |
| 69 | for_each_of_cpu_node(dn) { |
| 70 | hart = riscv_of_processor_hartid(dn); |
| 71 | if (hart < 0) |
| 72 | continue; |
| 73 | |
| 74 | if (hart == cpuid_to_hartid_map(0)) { |
| 75 | BUG_ON(found_boot_cpu); |
| 76 | found_boot_cpu = 1; |
| 77 | continue; |
| 78 | } |
| 79 | if (cpuid >= NR_CPUS) { |
| 80 | pr_warn("Invalid cpuid [%d] for hartid [%d]\n", |
| 81 | cpuid, hart); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | cpuid_to_hartid_map(cpuid) = hart; |
| 86 | cpuid++; |
| 87 | } |
| 88 | |
| 89 | BUG_ON(!found_boot_cpu); |
| 90 | |
| 91 | if (cpuid > nr_cpu_ids) |
| 92 | pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n", |
| 93 | cpuid, nr_cpu_ids); |
| 94 | |
| 95 | for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) { |
| 96 | if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) |
| 97 | set_cpu_possible(cpuid, true); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int __cpu_up(unsigned int cpu, struct task_struct *tidle) |
| 102 | { |
| 103 | int ret = 0; |
| 104 | int hartid = cpuid_to_hartid_map(cpu); |
| 105 | tidle->thread_info.cpu = cpu; |
| 106 | |
| 107 | /* |
| 108 | * On RISC-V systems, all harts boot on their own accord. Our _start |
| 109 | * selects the first hart to boot the kernel and causes the remainder |
| 110 | * of the harts to spin in a loop waiting for their stack pointer to be |
| 111 | * setup by that main hart. Writing __cpu_up_stack_pointer signals to |
| 112 | * the spinning harts that they can continue the boot process. |
| 113 | */ |
| 114 | smp_mb(); |
| 115 | WRITE_ONCE(__cpu_up_stack_pointer[hartid], |
| 116 | task_stack_page(tidle) + THREAD_SIZE); |
| 117 | WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle); |
| 118 | |
| 119 | lockdep_assert_held(&cpu_running); |
| 120 | wait_for_completion_timeout(&cpu_running, |
| 121 | msecs_to_jiffies(1000)); |
| 122 | |
| 123 | if (!cpu_online(cpu)) { |
| 124 | pr_crit("CPU%u: failed to come online\n", cpu); |
| 125 | ret = -EIO; |
| 126 | } |
| 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | void __init smp_cpus_done(unsigned int max_cpus) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * C entry point for a secondary processor. |
| 137 | */ |
| 138 | asmlinkage __visible void __init smp_callin(void) |
| 139 | { |
| 140 | struct mm_struct *mm = &init_mm; |
| 141 | |
| 142 | /* All kernel threads share the same mm context. */ |
| 143 | mmgrab(mm); |
| 144 | current->active_mm = mm; |
| 145 | |
| 146 | trap_init(); |
| 147 | store_cpu_topology(smp_processor_id()); |
| 148 | notify_cpu_starting(smp_processor_id()); |
| 149 | set_cpu_online(smp_processor_id(), 1); |
| 150 | /* |
| 151 | * Remote TLB flushes are ignored while the CPU is offline, so emit |
| 152 | * a local TLB flush right now just in case. |
| 153 | */ |
| 154 | local_flush_tlb_all(); |
| 155 | complete(&cpu_running); |
| 156 | /* |
| 157 | * Disable preemption before enabling interrupts, so we don't try to |
| 158 | * schedule a CPU that hasn't actually started yet. |
| 159 | */ |
| 160 | preempt_disable(); |
| 161 | local_irq_enable(); |
| 162 | cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); |
| 163 | } |