1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
5 * Copyright (c) 2021 Jessica Clarke <jrtc27@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/reboot.h>
38
39 #include <machine/md_var.h>
40 #include <machine/sbi.h>
41
42 /* SBI Implementation-Specific Definitions */
43 #define OPENSBI_VERSION_MAJOR_OFFSET 16
44 #define OPENSBI_VERSION_MINOR_MASK 0xFFFF
45
46 struct sbi_softc {
47 device_t dev;
48 };
49
50 struct sbi_devinfo {
51 struct resource_list rl;
52 };
53
54 static struct sbi_softc *sbi_softc = NULL;
55
56 static u_long sbi_spec_version;
57 static u_long sbi_impl_id;
58 static u_long sbi_impl_version;
59
60 static bool has_time_extension = false;
61 static bool has_ipi_extension = false;
62 static bool has_rfnc_extension = false;
63 static bool has_srst_extension = false;
64
65 static struct sbi_ret
sbi_get_spec_version(void)66 sbi_get_spec_version(void)
67 {
68 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
69 }
70
71 static struct sbi_ret
sbi_get_impl_id(void)72 sbi_get_impl_id(void)
73 {
74 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
75 }
76
77 static struct sbi_ret
sbi_get_impl_version(void)78 sbi_get_impl_version(void)
79 {
80 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
81 }
82
83 static struct sbi_ret
sbi_get_mvendorid(void)84 sbi_get_mvendorid(void)
85 {
86 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID));
87 }
88
89 static struct sbi_ret
sbi_get_marchid(void)90 sbi_get_marchid(void)
91 {
92 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID));
93 }
94
95 static struct sbi_ret
sbi_get_mimpid(void)96 sbi_get_mimpid(void)
97 {
98 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID));
99 }
100
101 static void
sbi_shutdown_final(void * dummy __unused,int howto)102 sbi_shutdown_final(void *dummy __unused, int howto)
103 {
104 if ((howto & RB_POWEROFF) != 0)
105 sbi_system_reset(SBI_SRST_TYPE_SHUTDOWN, SBI_SRST_REASON_NONE);
106 }
107
108 void
sbi_system_reset(u_long reset_type,u_long reset_reason)109 sbi_system_reset(u_long reset_type, u_long reset_reason)
110 {
111 /* Use the SRST extension, if available. */
112 if (has_srst_extension) {
113 (void)SBI_CALL2(SBI_EXT_ID_SRST, SBI_SRST_SYSTEM_RESET,
114 reset_type, reset_reason);
115 }
116 (void)SBI_CALL0(SBI_SHUTDOWN, 0);
117 }
118
119 void
sbi_print_version(void)120 sbi_print_version(void)
121 {
122 u_int major;
123 u_int minor;
124
125 /* For legacy SBI implementations. */
126 if (sbi_spec_version == 0) {
127 printf("SBI: Unknown (Legacy) Implementation\n");
128 printf("SBI Specification Version: 0.1\n");
129 return;
130 }
131
132 switch (sbi_impl_id) {
133 case (SBI_IMPL_ID_BBL):
134 printf("SBI: Berkely Boot Loader %lu\n", sbi_impl_version);
135 break;
136 case (SBI_IMPL_ID_XVISOR):
137 printf("SBI: eXtensible Versatile hypervISOR %lu\n",
138 sbi_impl_version);
139 break;
140 case (SBI_IMPL_ID_KVM):
141 printf("SBI: Kernel-based Virtual Machine %lu\n",
142 sbi_impl_version);
143 break;
144 case (SBI_IMPL_ID_RUSTSBI):
145 printf("SBI: RustSBI %lu\n", sbi_impl_version);
146 break;
147 case (SBI_IMPL_ID_DIOSIX):
148 printf("SBI: Diosix %lu\n", sbi_impl_version);
149 break;
150 case (SBI_IMPL_ID_OPENSBI):
151 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
152 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
153 printf("SBI: OpenSBI v%u.%u\n", major, minor);
154 break;
155 default:
156 printf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
157 break;
158 }
159
160 major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
161 SBI_SPEC_VERS_MAJOR_OFFSET;
162 minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
163 printf("SBI Specification Version: %u.%u\n", major, minor);
164 }
165
166 void
sbi_set_timer(uint64_t val)167 sbi_set_timer(uint64_t val)
168 {
169 struct sbi_ret ret __diagused;
170
171 /* Use the TIME legacy replacement extension, if available. */
172 if (has_time_extension) {
173 ret = SBI_CALL1(SBI_EXT_ID_TIME, SBI_TIME_SET_TIMER, val);
174 MPASS(ret.error == SBI_SUCCESS);
175 } else {
176 (void)SBI_CALL1(SBI_SET_TIMER, 0, val);
177 }
178 }
179
180 void
sbi_send_ipi(const u_long * hart_mask)181 sbi_send_ipi(const u_long *hart_mask)
182 {
183 struct sbi_ret ret __diagused;
184
185 /* Use the IPI legacy replacement extension, if available. */
186 if (has_ipi_extension) {
187 ret = SBI_CALL2(SBI_EXT_ID_IPI, SBI_IPI_SEND_IPI,
188 *hart_mask, 0);
189 MPASS(ret.error == SBI_SUCCESS);
190 } else {
191 (void)SBI_CALL1(SBI_SEND_IPI, 0, (uint64_t)hart_mask);
192 }
193 }
194
195 void
sbi_remote_fence_i(const u_long * hart_mask)196 sbi_remote_fence_i(const u_long *hart_mask)
197 {
198 struct sbi_ret ret __diagused;
199
200 /* Use the RFENCE legacy replacement extension, if available. */
201 if (has_rfnc_extension) {
202 ret = SBI_CALL2(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_FENCE_I,
203 *hart_mask, 0);
204 MPASS(ret.error == SBI_SUCCESS);
205 } else {
206 (void)SBI_CALL1(SBI_REMOTE_FENCE_I, 0, (uint64_t)hart_mask);
207 }
208 }
209
210 void
sbi_remote_sfence_vma(const u_long * hart_mask,u_long start,u_long size)211 sbi_remote_sfence_vma(const u_long *hart_mask, u_long start, u_long size)
212 {
213 struct sbi_ret ret __diagused;
214
215 /* Use the RFENCE legacy replacement extension, if available. */
216 if (has_rfnc_extension) {
217 ret = SBI_CALL4(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA,
218 *hart_mask, 0, start, size);
219 MPASS(ret.error == SBI_SUCCESS);
220 } else {
221 (void)SBI_CALL3(SBI_REMOTE_SFENCE_VMA, 0, (uint64_t)hart_mask,
222 start, size);
223 }
224 }
225
226 void
sbi_remote_sfence_vma_asid(const u_long * hart_mask,u_long start,u_long size,u_long asid)227 sbi_remote_sfence_vma_asid(const u_long *hart_mask, u_long start, u_long size,
228 u_long asid)
229 {
230 struct sbi_ret ret __diagused;
231
232 /* Use the RFENCE legacy replacement extension, if available. */
233 if (has_rfnc_extension) {
234 ret = SBI_CALL5(SBI_EXT_ID_RFNC,
235 SBI_RFNC_REMOTE_SFENCE_VMA_ASID, *hart_mask, 0, start,
236 size, asid);
237 MPASS(ret.error == SBI_SUCCESS);
238 } else {
239 (void)SBI_CALL4(SBI_REMOTE_SFENCE_VMA_ASID, 0,
240 (uint64_t)hart_mask, start, size, asid);
241 }
242 }
243
244 int
sbi_hsm_hart_start(u_long hart,u_long start_addr,u_long priv)245 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
246 {
247 struct sbi_ret ret;
248
249 ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr,
250 priv);
251 return (ret.error != 0 ? (int)ret.error : 0);
252 }
253
254 void
sbi_hsm_hart_stop(void)255 sbi_hsm_hart_stop(void)
256 {
257 (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
258 }
259
260 int
sbi_hsm_hart_status(u_long hart)261 sbi_hsm_hart_status(u_long hart)
262 {
263 struct sbi_ret ret;
264
265 ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
266
267 return (ret.error != 0 ? (int)ret.error : (int)ret.value);
268 }
269
270 void
sbi_init(void)271 sbi_init(void)
272 {
273 struct sbi_ret sret;
274
275 /*
276 * Get the spec version. For legacy SBI implementations this will
277 * return an error, otherwise it is guaranteed to succeed.
278 */
279 sret = sbi_get_spec_version();
280 if (sret.error != 0) {
281 /* We are running a legacy SBI implementation. */
282 sbi_spec_version = 0;
283 return;
284 }
285
286 /* Set the SBI implementation info. */
287 sbi_spec_version = sret.value;
288 sbi_impl_id = sbi_get_impl_id().value;
289 sbi_impl_version = sbi_get_impl_version().value;
290
291 /* Set the hardware implementation info. */
292 mvendorid = sbi_get_mvendorid().value;
293 marchid = sbi_get_marchid().value;
294 mimpid = sbi_get_mimpid().value;
295
296 /* Probe for legacy replacement extensions. */
297 if (sbi_probe_extension(SBI_EXT_ID_TIME) != 0)
298 has_time_extension = true;
299 if (sbi_probe_extension(SBI_EXT_ID_IPI) != 0)
300 has_ipi_extension = true;
301 if (sbi_probe_extension(SBI_EXT_ID_RFNC) != 0)
302 has_rfnc_extension = true;
303 if (sbi_probe_extension(SBI_EXT_ID_SRST) != 0)
304 has_srst_extension = true;
305
306 /*
307 * Probe for legacy extensions. We still rely on many of them to be
308 * implemented, but this is not guaranteed by the spec.
309 */
310 KASSERT(has_time_extension || sbi_probe_extension(SBI_SET_TIMER) != 0,
311 ("SBI doesn't implement sbi_set_timer()"));
312 KASSERT(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
313 ("SBI doesn't implement sbi_console_putchar()"));
314 KASSERT(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
315 ("SBI doesn't implement sbi_console_getchar()"));
316 KASSERT(has_ipi_extension || sbi_probe_extension(SBI_SEND_IPI) != 0,
317 ("SBI doesn't implement sbi_send_ipi()"));
318 KASSERT(has_rfnc_extension ||
319 sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
320 ("SBI doesn't implement sbi_remote_fence_i()"));
321 KASSERT(has_rfnc_extension ||
322 sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
323 ("SBI doesn't implement sbi_remote_sfence_vma()"));
324 KASSERT(has_rfnc_extension ||
325 sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
326 ("SBI doesn't implement sbi_remote_sfence_vma_asid()"));
327 KASSERT(has_srst_extension || sbi_probe_extension(SBI_SHUTDOWN) != 0,
328 ("SBI doesn't implement a shutdown or reset extension"));
329 }
330
331 static void
sbi_identify(driver_t * driver,device_t parent)332 sbi_identify(driver_t *driver, device_t parent)
333 {
334 device_t dev;
335
336 if (device_find_child(parent, "sbi", -1) != NULL)
337 return;
338
339 dev = BUS_ADD_CHILD(parent, 0, "sbi", -1);
340 if (dev == NULL)
341 device_printf(parent, "Can't add sbi child\n");
342 }
343
344 static int
sbi_probe(device_t dev)345 sbi_probe(device_t dev)
346 {
347 device_set_desc(dev, "RISC-V Supervisor Binary Interface");
348
349 return (BUS_PROBE_NOWILDCARD);
350 }
351
352 static int
sbi_attach(device_t dev)353 sbi_attach(device_t dev)
354 {
355 struct sbi_softc *sc;
356 #ifdef SMP
357 device_t child;
358 struct sbi_devinfo *di;
359 #endif
360
361 if (sbi_softc != NULL)
362 return (ENXIO);
363
364 sc = device_get_softc(dev);
365 sc->dev = dev;
366 sbi_softc = sc;
367
368 EVENTHANDLER_REGISTER(shutdown_final, sbi_shutdown_final, NULL,
369 SHUTDOWN_PRI_LAST);
370
371 #ifdef SMP
372 di = malloc(sizeof(*di), M_DEVBUF, M_WAITOK | M_ZERO);
373 resource_list_init(&di->rl);
374 child = device_add_child(dev, "sbi_ipi", -1);
375 if (child == NULL) {
376 device_printf(dev, "Could not add sbi_ipi child\n");
377 return (ENXIO);
378 }
379
380 device_set_ivars(child, di);
381 #endif
382
383 return (0);
384 }
385
386 static struct resource_list *
sbi_get_resource_list(device_t bus,device_t child)387 sbi_get_resource_list(device_t bus, device_t child)
388 {
389 struct sbi_devinfo *di;
390
391 di = device_get_ivars(child);
392 KASSERT(di != NULL, ("%s: No devinfo", __func__));
393
394 return (&di->rl);
395 }
396
397 static device_method_t sbi_methods[] = {
398 /* Device interface */
399 DEVMETHOD(device_identify, sbi_identify),
400 DEVMETHOD(device_probe, sbi_probe),
401 DEVMETHOD(device_attach, sbi_attach),
402
403 /* Bus interface */
404 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
405 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
406 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
407 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
408 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
409 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
410 DEVMETHOD(bus_get_resource_list, sbi_get_resource_list),
411 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
412 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
413
414 DEVMETHOD_END
415 };
416
417 DEFINE_CLASS_0(sbi, sbi_driver, sbi_methods, sizeof(struct sbi_softc));
418 EARLY_DRIVER_MODULE(sbi, nexus, sbi_driver, 0, 0,
419 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
420