blob: 0b04e0eae3ab5bfe0e9ff714e6ebef512b83dafd [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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 Scullb4b6d4a2019-01-02 15:54:55 +00009 */
10
David Brazdil0f672f62019-12-10 10:32:29 +000011#include <linux/arch_topology.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#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 Brazdil0f672f62019-12-10 10:32:29 +000026#include <linux/sched/mm.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020027#include <asm/cpu_ops.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028#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 Brazdil0f672f62019-12-10 10:32:29 +000033#include <asm/smp.h>
34
35#include "head.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036
David Brazdil0f672f62019-12-10 10:32:29 +000037static DECLARE_COMPLETION(cpu_running);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038
39void __init smp_prepare_boot_cpu(void)
40{
David Brazdil0f672f62019-12-10 10:32:29 +000041 init_cpu_topology();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042}
43
44void __init smp_prepare_cpus(unsigned int max_cpus)
45{
David Brazdil0f672f62019-12-10 10:32:29 +000046 int cpuid;
Olivier Deprez157378f2022-04-04 15:47:50 +020047 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +000048
49 /* This covers non-smp usecase mandated by "nosmp" option */
50 if (max_cpus == 0)
51 return;
52
53 for_each_possible_cpu(cpuid) {
54 if (cpuid == smp_processor_id())
55 continue;
Olivier Deprez157378f2022-04-04 15:47:50 +020056 if (cpu_ops[cpuid]->cpu_prepare) {
57 ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
58 if (ret)
59 continue;
60 }
David Brazdil0f672f62019-12-10 10:32:29 +000061 set_cpu_present(cpuid, true);
62 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063}
64
65void __init setup_smp(void)
66{
David Brazdil0f672f62019-12-10 10:32:29 +000067 struct device_node *dn;
68 int hart;
69 bool found_boot_cpu = false;
70 int cpuid = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071
Olivier Deprez157378f2022-04-04 15:47:50 +020072 cpu_set_ops(0);
73
David Brazdil0f672f62019-12-10 10:32:29 +000074 for_each_of_cpu_node(dn) {
75 hart = riscv_of_processor_hartid(dn);
76 if (hart < 0)
77 continue;
78
79 if (hart == cpuid_to_hartid_map(0)) {
80 BUG_ON(found_boot_cpu);
81 found_boot_cpu = 1;
82 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 }
David Brazdil0f672f62019-12-10 10:32:29 +000084 if (cpuid >= NR_CPUS) {
85 pr_warn("Invalid cpuid [%d] for hartid [%d]\n",
86 cpuid, hart);
87 break;
88 }
89
90 cpuid_to_hartid_map(cpuid) = hart;
91 cpuid++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092 }
93
David Brazdil0f672f62019-12-10 10:32:29 +000094 BUG_ON(!found_boot_cpu);
95
96 if (cpuid > nr_cpu_ids)
97 pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
98 cpuid, nr_cpu_ids);
99
100 for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200101 if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {
102 cpu_set_ops(cpuid);
David Brazdil0f672f62019-12-10 10:32:29 +0000103 set_cpu_possible(cpuid, true);
Olivier Deprez157378f2022-04-04 15:47:50 +0200104 }
David Brazdil0f672f62019-12-10 10:32:29 +0000105 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106}
107
Olivier Deprez157378f2022-04-04 15:47:50 +0200108static int start_secondary_cpu(int cpu, struct task_struct *tidle)
109{
110 if (cpu_ops[cpu]->cpu_start)
111 return cpu_ops[cpu]->cpu_start(cpu, tidle);
112
113 return -EOPNOTSUPP;
114}
115
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116int __cpu_up(unsigned int cpu, struct task_struct *tidle)
117{
David Brazdil0f672f62019-12-10 10:32:29 +0000118 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119 tidle->thread_info.cpu = cpu;
120
Olivier Deprez157378f2022-04-04 15:47:50 +0200121 ret = start_secondary_cpu(cpu, tidle);
122 if (!ret) {
123 wait_for_completion_timeout(&cpu_running,
David Brazdil0f672f62019-12-10 10:32:29 +0000124 msecs_to_jiffies(1000));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125
Olivier Deprez157378f2022-04-04 15:47:50 +0200126 if (!cpu_online(cpu)) {
127 pr_crit("CPU%u: failed to come online\n", cpu);
128 ret = -EIO;
129 }
130 } else {
131 pr_crit("CPU%u: failed to start\n", cpu);
David Brazdil0f672f62019-12-10 10:32:29 +0000132 }
133
134 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135}
136
137void __init smp_cpus_done(unsigned int max_cpus)
138{
139}
140
141/*
142 * C entry point for a secondary processor.
143 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200144asmlinkage __visible void smp_callin(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145{
146 struct mm_struct *mm = &init_mm;
Olivier Deprez157378f2022-04-04 15:47:50 +0200147 unsigned int curr_cpuid = smp_processor_id();
148
149 riscv_clear_ipi();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150
151 /* All kernel threads share the same mm context. */
David Brazdil0f672f62019-12-10 10:32:29 +0000152 mmgrab(mm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 current->active_mm = mm;
154
Olivier Deprez157378f2022-04-04 15:47:50 +0200155 notify_cpu_starting(curr_cpuid);
156 update_siblings_masks(curr_cpuid);
157 set_cpu_online(curr_cpuid, 1);
158
David Brazdil0f672f62019-12-10 10:32:29 +0000159 /*
160 * Remote TLB flushes are ignored while the CPU is offline, so emit
161 * a local TLB flush right now just in case.
162 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163 local_flush_tlb_all();
David Brazdil0f672f62019-12-10 10:32:29 +0000164 complete(&cpu_running);
165 /*
166 * Disable preemption before enabling interrupts, so we don't try to
167 * schedule a CPU that hasn't actually started yet.
168 */
David Brazdil0f672f62019-12-10 10:32:29 +0000169 local_irq_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000170 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
171}