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