1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/* $NetBSD: cpu_fdt.c,v 1.2 2023/06/12 19:04:13 skrll Exp $ */
/*-
* Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "opt_multiprocessor.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cpu_fdt.c,v 1.2 2023/06/12 19:04:13 skrll Exp $");
#include <sys/param.h>
#include <sys/cpu.h>
#include <dev/fdt/fdtvar.h>
#include <riscv/cpufunc.h>
#include <riscv/cpuvar.h>
#include <riscv/machdep.h>
#include <riscv/sbi.h>
#include <riscv/fdt/riscv_fdtvar.h>
#ifdef MULTIPROCESSOR
static bool
riscv_fdt_cpu_okay(const int child)
{
const char *s;
s = fdtbus_get_string(child, "device_type");
if (!s || strcmp(s, "cpu") != 0)
return false;
s = fdtbus_get_string(child, "status");
if (s) {
if (strcmp(s, "okay") == 0)
return true;
if (strcmp(s, "disabled") == 0)
return false;
return false;
} else {
return true;
}
}
#endif /* MULTIPROCESSOR */
void
riscv_fdt_cpu_bootstrap(void)
{
#ifdef MULTIPROCESSOR
const int cpus = OF_finddevice("/cpus");
if (cpus == -1) {
aprint_error("%s: no /cpus node found\n", __func__);
riscv_cpu_max = 1;
return;
}
/* Count harts and add hart IDs to to cpu_hartid array */
size_t cpuindex = 1;
for (int child = OF_child(cpus); child; child = OF_peer(child)) {
if (!riscv_fdt_cpu_okay(child))
continue;
riscv_cpu_max++;
uint64_t reg;
if (fdtbus_get_reg64(child, 0, ®, NULL) != 0)
continue;
const cpuid_t hartid = reg;
struct sbiret sbiret = sbi_hart_get_status(hartid);
switch (sbiret.error) {
case SBI_ERR_INVALID_PARAM:
aprint_error("Unknown hart id %lx", hartid);
continue;
case SBI_SUCCESS:
break;
default:
aprint_error("Unexpected error (%ld) from get_status",
sbiret.error);
}
/* Assume the BP is the only one started. */
if (sbiret.value == SBI_HART_STARTED) {
if (cpu_hartid[0] != -1) {
panic("more than 1 hart started");
}
cpu_hartid[0] = hartid;
continue;
}
KASSERT(cpuindex < MAXCPUS);
cpu_hartid[cpuindex] = hartid;
cpu_dcache_wb_range((vaddr_t)&cpu_hartid[cpuindex],
sizeof(cpu_hartid[cpuindex]));
cpuindex++;
}
#endif
}
int
riscv_fdt_cpu_mpstart(void)
{
int ret = 0;
#ifdef MULTIPROCESSOR
const int cpus = OF_finddevice("/cpus");
if (cpus == -1) {
aprint_error("%s: no /cpus node found\n", __func__);
return 0;
}
// riscv_fdt_cpu_bootstrap put the boot hart id in cpu_hartid[0]
const cpuid_t bp_hartid = cpu_hartid[0];
/* BootAPs */
size_t cpuindex = 1;
for (int child = OF_child(cpus); child; child = OF_peer(child)) {
if (!riscv_fdt_cpu_okay(child))
continue;
uint64_t reg;
if (fdtbus_get_reg64(child, 0, ®, NULL) != 0)
continue;
cpuid_t hartid = reg;
if (hartid == bp_hartid)
continue; /* BP already started */
const paddr_t entry = KERN_VTOPHYS(cpu_mpstart);
struct sbiret sbiret = sbi_hart_start(hartid, entry, 0);
switch (sbiret.error) {
case SBI_SUCCESS:
break;
case SBI_ERR_INVALID_ADDRESS:
break;
case SBI_ERR_INVALID_PARAM:
break;
case SBI_ERR_ALREADY_AVAILABLE:
break;
case SBI_ERR_FAILED:
break;
default:
aprint_error("%s: failed to enable CPU %#lx\n",
__func__, hartid);
}
size_t i;
/* Wait for AP to start */
for (i = 0x10000000; i > 0; i--) {
if (cpu_hatched_p(cpuindex))
break;
}
if (i == 0) {
ret++;
aprint_error("cpu%zu: WARNING: AP failed to start\n",
cpuindex);
}
cpuindex++;
}
#else
aprint_normal("%s: kernel compiled without MULTIPROCESSOR\n", __func__);
#endif /* MULTIPROCESSOR */
return ret;
}
static int
cpu_fdt_match(device_t parent, cfdata_t cf, void *aux)
{
struct fdt_attach_args * const faa = aux;
const int phandle = faa->faa_phandle;
const char *device_type;
device_type = fdtbus_get_string(phandle, "device_type");
return device_type != NULL && strcmp(device_type, "cpu") == 0;
}
static void
cpu_fdt_attach(device_t parent, device_t self, void *aux)
{
struct fdt_attach_args * const faa = aux;
const int phandle = faa->faa_phandle;
bus_addr_t cpuid;
if (fdtbus_get_reg(phandle, 0, &cpuid, NULL) != 0)
cpuid = 0;
/* Attach the CPU */
cpu_attach(self, cpuid);
fdt_add_bus(self, phandle, faa);
}
CFATTACH_DECL_NEW(cpu_fdt, 0, cpu_fdt_match, cpu_fdt_attach, NULL, NULL);
|