David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11 | #include <linux/arch_topology.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 26 | #include <linux/sched/mm.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 27 | #include <asm/cpu_ops.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | #include <asm/irq.h> |
| 29 | #include <asm/mmu_context.h> |
| 30 | #include <asm/tlbflush.h> |
| 31 | #include <asm/sections.h> |
| 32 | #include <asm/sbi.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 33 | #include <asm/smp.h> |
| 34 | |
| 35 | #include "head.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 36 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 37 | static DECLARE_COMPLETION(cpu_running); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 38 | |
| 39 | void __init smp_prepare_boot_cpu(void) |
| 40 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 41 | init_cpu_topology(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void __init smp_prepare_cpus(unsigned int max_cpus) |
| 45 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 46 | int cpuid; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 47 | int ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 48 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 49 | store_cpu_topology(smp_processor_id()); |
| 50 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 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; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 58 | if (cpu_ops[cpuid]->cpu_prepare) { |
| 59 | ret = cpu_ops[cpuid]->cpu_prepare(cpuid); |
| 60 | if (ret) |
| 61 | continue; |
| 62 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 63 | set_cpu_present(cpuid, true); |
| 64 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void __init setup_smp(void) |
| 68 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 69 | struct device_node *dn; |
| 70 | int hart; |
| 71 | bool found_boot_cpu = false; |
| 72 | int cpuid = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 73 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 74 | cpu_set_ops(0); |
| 75 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 76 | for_each_of_cpu_node(dn) { |
| 77 | hart = riscv_of_processor_hartid(dn); |
| 78 | if (hart < 0) |
| 79 | continue; |
| 80 | |
| 81 | if (hart == cpuid_to_hartid_map(0)) { |
| 82 | BUG_ON(found_boot_cpu); |
| 83 | found_boot_cpu = 1; |
| 84 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 85 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 86 | if (cpuid >= NR_CPUS) { |
| 87 | pr_warn("Invalid cpuid [%d] for hartid [%d]\n", |
| 88 | cpuid, hart); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | cpuid_to_hartid_map(cpuid) = hart; |
| 93 | cpuid++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 96 | BUG_ON(!found_boot_cpu); |
| 97 | |
| 98 | if (cpuid > nr_cpu_ids) |
| 99 | pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n", |
| 100 | cpuid, nr_cpu_ids); |
| 101 | |
| 102 | for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 103 | if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) { |
| 104 | cpu_set_ops(cpuid); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 105 | set_cpu_possible(cpuid, true); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 106 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 107 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 110 | static int start_secondary_cpu(int cpu, struct task_struct *tidle) |
| 111 | { |
| 112 | if (cpu_ops[cpu]->cpu_start) |
| 113 | return cpu_ops[cpu]->cpu_start(cpu, tidle); |
| 114 | |
| 115 | return -EOPNOTSUPP; |
| 116 | } |
| 117 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | int __cpu_up(unsigned int cpu, struct task_struct *tidle) |
| 119 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 120 | int ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 121 | tidle->thread_info.cpu = cpu; |
| 122 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 123 | ret = start_secondary_cpu(cpu, tidle); |
| 124 | if (!ret) { |
| 125 | wait_for_completion_timeout(&cpu_running, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 126 | msecs_to_jiffies(1000)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 127 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 128 | if (!cpu_online(cpu)) { |
| 129 | pr_crit("CPU%u: failed to come online\n", cpu); |
| 130 | ret = -EIO; |
| 131 | } |
| 132 | } else { |
| 133 | pr_crit("CPU%u: failed to start\n", cpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void __init smp_cpus_done(unsigned int max_cpus) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * C entry point for a secondary processor. |
| 145 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 146 | asmlinkage __visible void smp_callin(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 147 | { |
| 148 | struct mm_struct *mm = &init_mm; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 149 | unsigned int curr_cpuid = smp_processor_id(); |
| 150 | |
| 151 | riscv_clear_ipi(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 152 | |
| 153 | /* All kernel threads share the same mm context. */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 154 | mmgrab(mm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 155 | current->active_mm = mm; |
| 156 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 157 | store_cpu_topology(curr_cpuid); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 158 | notify_cpu_starting(curr_cpuid); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 159 | set_cpu_online(curr_cpuid, 1); |
| 160 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 161 | /* |
| 162 | * Remote TLB flushes are ignored while the CPU is offline, so emit |
| 163 | * a local TLB flush right now just in case. |
| 164 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 165 | local_flush_tlb_all(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 166 | complete(&cpu_running); |
| 167 | /* |
| 168 | * Disable preemption before enabling interrupts, so we don't try to |
| 169 | * schedule a CPU that hasn't actually started yet. |
| 170 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 171 | local_irq_enable(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 172 | cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); |
| 173 | } |