hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/arch/riscv/kernel/smpboot.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * SMP initialisation and IPI support
34 * Based on arch/arm64/kernel/smp.c
....@@ -5,17 +6,9 @@
56 * Copyright (C) 2012 ARM Ltd.
67 * Copyright (C) 2015 Regents of the University of California
78 * Copyright (C) 2017 SiFive
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License version 2 as
11
- * published by the Free Software Foundation.
12
- *
13
- * This program is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- * GNU General Public License for more details.
179 */
1810
11
+#include <linux/arch_topology.h>
1912 #include <linux/module.h>
2013 #include <linux/init.h>
2114 #include <linux/kernel.h>
....@@ -30,62 +23,117 @@
3023 #include <linux/irq.h>
3124 #include <linux/of.h>
3225 #include <linux/sched/task_stack.h>
26
+#include <linux/sched/mm.h>
27
+#include <asm/cpu_ops.h>
3328 #include <asm/irq.h>
3429 #include <asm/mmu_context.h>
3530 #include <asm/tlbflush.h>
3631 #include <asm/sections.h>
3732 #include <asm/sbi.h>
33
+#include <asm/smp.h>
3834
39
-void *__cpu_up_stack_pointer[NR_CPUS];
40
-void *__cpu_up_task_pointer[NR_CPUS];
35
+#include "head.h"
36
+
37
+static DECLARE_COMPLETION(cpu_running);
4138
4239 void __init smp_prepare_boot_cpu(void)
4340 {
41
+ init_cpu_topology();
4442 }
4543
4644 void __init smp_prepare_cpus(unsigned int max_cpus)
4745 {
46
+ int cpuid;
47
+ int ret;
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
+ if (cpu_ops[cpuid]->cpu_prepare) {
59
+ ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
60
+ if (ret)
61
+ continue;
62
+ }
63
+ set_cpu_present(cpuid, true);
64
+ }
4865 }
4966
5067 void __init setup_smp(void)
5168 {
52
- struct device_node *dn = NULL;
53
- int hart, im_okay_therefore_i_am = 0;
69
+ struct device_node *dn;
70
+ int hart;
71
+ bool found_boot_cpu = false;
72
+ int cpuid = 1;
5473
55
- while ((dn = of_find_node_by_type(dn, "cpu"))) {
56
- hart = riscv_of_processor_hart(dn);
57
- if (hart >= 0) {
58
- set_cpu_possible(hart, true);
59
- set_cpu_present(hart, true);
60
- if (hart == smp_processor_id()) {
61
- BUG_ON(im_okay_therefore_i_am);
62
- im_okay_therefore_i_am = 1;
63
- }
74
+ cpu_set_ops(0);
75
+
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;
6485 }
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++;
6594 }
6695
67
- BUG_ON(!im_okay_therefore_i_am);
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++) {
103
+ if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {
104
+ cpu_set_ops(cpuid);
105
+ set_cpu_possible(cpuid, true);
106
+ }
107
+ }
108
+}
109
+
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;
68116 }
69117
70118 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
71119 {
120
+ int ret = 0;
72121 tidle->thread_info.cpu = cpu;
73122
74
- /*
75
- * On RISC-V systems, all harts boot on their own accord. Our _start
76
- * selects the first hart to boot the kernel and causes the remainder
77
- * of the harts to spin in a loop waiting for their stack pointer to be
78
- * setup by that main hart. Writing __cpu_up_stack_pointer signals to
79
- * the spinning harts that they can continue the boot process.
80
- */
81
- smp_mb();
82
- __cpu_up_stack_pointer[cpu] = task_stack_page(tidle) + THREAD_SIZE;
83
- __cpu_up_task_pointer[cpu] = tidle;
123
+ ret = start_secondary_cpu(cpu, tidle);
124
+ if (!ret) {
125
+ wait_for_completion_timeout(&cpu_running,
126
+ msecs_to_jiffies(1000));
84127
85
- while (!cpu_online(cpu))
86
- cpu_relax();
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);
134
+ }
87135
88
- return 0;
136
+ return ret;
89137 }
90138
91139 void __init smp_cpus_done(unsigned int max_cpus)
....@@ -95,19 +143,31 @@
95143 /*
96144 * C entry point for a secondary processor.
97145 */
98
-asmlinkage void __init smp_callin(void)
146
+asmlinkage __visible void smp_callin(void)
99147 {
100148 struct mm_struct *mm = &init_mm;
149
+ unsigned int curr_cpuid = smp_processor_id();
150
+
151
+ riscv_clear_ipi();
101152
102153 /* All kernel threads share the same mm context. */
103
- atomic_inc(&mm->mm_count);
154
+ mmgrab(mm);
104155 current->active_mm = mm;
105156
106
- trap_init();
107
- notify_cpu_starting(smp_processor_id());
108
- set_cpu_online(smp_processor_id(), 1);
157
+ store_cpu_topology(curr_cpuid);
158
+ notify_cpu_starting(curr_cpuid);
159
+ set_cpu_online(curr_cpuid, 1);
160
+
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
+ */
109165 local_flush_tlb_all();
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
+ */
110171 local_irq_enable();
111
- preempt_disable();
112172 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
113173 }