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_OPENSBI):
136 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
137 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
138 printf("SBI: OpenSBI v%u.%u\n", major, minor);
139 break;
140 case (SBI_IMPL_ID_XVISOR):
141 printf("SBI: eXtensible Versatile hypervISOR %lu\n",
142 sbi_impl_version);
143 break;
144 case (SBI_IMPL_ID_KVM):
145 printf("SBI: Kernel-based Virtual Machine %lu\n",
146 sbi_impl_version);
147 break;
148 case (SBI_IMPL_ID_RUSTSBI):
149 printf("SBI: RustSBI %lu\n", sbi_impl_version);
150 break;
151 case (SBI_IMPL_ID_DIOSIX):
152 printf("SBI: Diosix %lu\n", sbi_impl_version);
153 break;
154 case (SBI_IMPL_ID_COFFER):
155 printf("SBI: Coffer %lu\n", sbi_impl_version);
156 break;
157 case (SBI_IMPL_ID_XEN_PROJECT):
158 printf("SBI: Xen Project %lu\n", sbi_impl_version);
159 break;
160 case (SBI_IMPL_ID_POLARFIRE_HSS):
161 printf("SBI: PolarFire Hart Software Services %lu\n",
162 sbi_impl_version);
163 break;
164 case (SBI_IMPL_ID_COREBOOT):
165 printf("SBI: coreboot %lu\n", sbi_impl_version);
166 break;
167 case (SBI_IMPL_ID_OREBOOT):
168 printf("SBI: oreboot %lu\n", sbi_impl_version);
169 break;
170 case (SBI_IMPL_ID_BHYVE):
171 printf("SBI: bhyve %lu\n", sbi_impl_version);
172 break;
173 default:
174 printf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
175 break;
176 }
177
178 major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
179 SBI_SPEC_VERS_MAJOR_OFFSET;
180 minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
181 printf("SBI Specification Version: %u.%u\n", major, minor);
182 }
183
184 void
sbi_set_timer(uint64_t val)185 sbi_set_timer(uint64_t val)
186 {
187 struct sbi_ret ret __diagused;
188
189 /* Use the TIME legacy replacement extension, if available. */
190 if (has_time_extension) {
191 ret = SBI_CALL1(SBI_EXT_ID_TIME, SBI_TIME_SET_TIMER, val);
192 MPASS(ret.error == SBI_SUCCESS);
193 } else {
194 (void)SBI_CALL1(SBI_SET_TIMER, 0, val);
195 }
196 }
197
198 void
sbi_send_ipi(const u_long * hart_mask)199 sbi_send_ipi(const u_long *hart_mask)
200 {
201 struct sbi_ret ret __diagused;
202
203 /* Use the IPI legacy replacement extension, if available. */
204 if (has_ipi_extension) {
205 ret = SBI_CALL2(SBI_EXT_ID_IPI, SBI_IPI_SEND_IPI,
206 *hart_mask, 0);
207 MPASS(ret.error == SBI_SUCCESS);
208 } else {
209 (void)SBI_CALL1(SBI_SEND_IPI, 0, (uint64_t)hart_mask);
210 }
211 }
212
213 void
sbi_remote_fence_i(const u_long * hart_mask)214 sbi_remote_fence_i(const u_long *hart_mask)
215 {
216 struct sbi_ret ret __diagused;
217
218 /* Use the RFENCE legacy replacement extension, if available. */
219 if (has_rfnc_extension) {
220 ret = SBI_CALL2(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_FENCE_I,
221 *hart_mask, 0);
222 MPASS(ret.error == SBI_SUCCESS);
223 } else {
224 (void)SBI_CALL1(SBI_REMOTE_FENCE_I, 0, (uint64_t)hart_mask);
225 }
226 }
227
228 void
sbi_remote_sfence_vma(const u_long * hart_mask,u_long start,u_long size)229 sbi_remote_sfence_vma(const u_long *hart_mask, u_long start, u_long size)
230 {
231 struct sbi_ret ret __diagused;
232
233 /* Use the RFENCE legacy replacement extension, if available. */
234 if (has_rfnc_extension) {
235 ret = SBI_CALL4(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA,
236 *hart_mask, 0, start, size);
237 MPASS(ret.error == SBI_SUCCESS);
238 } else {
239 (void)SBI_CALL3(SBI_REMOTE_SFENCE_VMA, 0, (uint64_t)hart_mask,
240 start, size);
241 }
242 }
243
244 void
sbi_remote_sfence_vma_asid(const u_long * hart_mask,u_long start,u_long size,u_long asid)245 sbi_remote_sfence_vma_asid(const u_long *hart_mask, u_long start, u_long size,
246 u_long asid)
247 {
248 struct sbi_ret ret __diagused;
249
250 /* Use the RFENCE legacy replacement extension, if available. */
251 if (has_rfnc_extension) {
252 ret = SBI_CALL5(SBI_EXT_ID_RFNC,
253 SBI_RFNC_REMOTE_SFENCE_VMA_ASID, *hart_mask, 0, start,
254 size, asid);
255 MPASS(ret.error == SBI_SUCCESS);
256 } else {
257 (void)SBI_CALL4(SBI_REMOTE_SFENCE_VMA_ASID, 0,
258 (uint64_t)hart_mask, start, size, asid);
259 }
260 }
261
262 int
sbi_hsm_hart_start(u_long hart,u_long start_addr,u_long priv)263 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
264 {
265 struct sbi_ret ret;
266
267 ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr,
268 priv);
269 return (ret.error != 0 ? (int)ret.error : 0);
270 }
271
272 void
sbi_hsm_hart_stop(void)273 sbi_hsm_hart_stop(void)
274 {
275 (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
276 }
277
278 int
sbi_hsm_hart_status(u_long hart)279 sbi_hsm_hart_status(u_long hart)
280 {
281 struct sbi_ret ret;
282
283 ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
284
285 return (ret.error != 0 ? (int)ret.error : (int)ret.value);
286 }
287
288 void
sbi_init(void)289 sbi_init(void)
290 {
291 struct sbi_ret sret;
292
293 /*
294 * Get the spec version. For legacy SBI implementations this will
295 * return an error, otherwise it is guaranteed to succeed.
296 */
297 sret = sbi_get_spec_version();
298 if (sret.error != 0) {
299 /* We are running a legacy SBI implementation. */
300 sbi_spec_version = 0;
301 return;
302 }
303
304 /* Set the SBI implementation info. */
305 sbi_spec_version = sret.value;
306 sbi_impl_id = sbi_get_impl_id().value;
307 sbi_impl_version = sbi_get_impl_version().value;
308
309 /* Set the hardware implementation info. */
310 mvendorid = sbi_get_mvendorid().value;
311 marchid = sbi_get_marchid().value;
312 mimpid = sbi_get_mimpid().value;
313
314 /* Probe for legacy replacement extensions. */
315 if (sbi_probe_extension(SBI_EXT_ID_TIME) != 0)
316 has_time_extension = true;
317 if (sbi_probe_extension(SBI_EXT_ID_IPI) != 0)
318 has_ipi_extension = true;
319 if (sbi_probe_extension(SBI_EXT_ID_RFNC) != 0)
320 has_rfnc_extension = true;
321 if (sbi_probe_extension(SBI_EXT_ID_SRST) != 0)
322 has_srst_extension = true;
323
324 /*
325 * Probe for legacy extensions. We still rely on many of them to be
326 * implemented, but this is not guaranteed by the spec.
327 */
328 KASSERT(has_time_extension || sbi_probe_extension(SBI_SET_TIMER) != 0,
329 ("SBI doesn't implement sbi_set_timer()"));
330 KASSERT(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
331 ("SBI doesn't implement sbi_console_putchar()"));
332 KASSERT(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
333 ("SBI doesn't implement sbi_console_getchar()"));
334 KASSERT(has_ipi_extension || sbi_probe_extension(SBI_SEND_IPI) != 0,
335 ("SBI doesn't implement sbi_send_ipi()"));
336 KASSERT(has_rfnc_extension ||
337 sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
338 ("SBI doesn't implement sbi_remote_fence_i()"));
339 KASSERT(has_rfnc_extension ||
340 sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
341 ("SBI doesn't implement sbi_remote_sfence_vma()"));
342 KASSERT(has_rfnc_extension ||
343 sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
344 ("SBI doesn't implement sbi_remote_sfence_vma_asid()"));
345 KASSERT(has_srst_extension || sbi_probe_extension(SBI_SHUTDOWN) != 0,
346 ("SBI doesn't implement a shutdown or reset extension"));
347 }
348
349 static void
sbi_identify(driver_t * driver,device_t parent)350 sbi_identify(driver_t *driver, device_t parent)
351 {
352 device_t dev;
353
354 if (device_find_child(parent, "sbi", DEVICE_UNIT_ANY) != NULL)
355 return;
356
357 dev = BUS_ADD_CHILD(parent, 0, "sbi", DEVICE_UNIT_ANY);
358 if (dev == NULL)
359 device_printf(parent, "Can't add sbi child\n");
360 }
361
362 static int
sbi_probe(device_t dev)363 sbi_probe(device_t dev)
364 {
365 device_set_desc(dev, "RISC-V Supervisor Binary Interface");
366
367 return (BUS_PROBE_NOWILDCARD);
368 }
369
370 static int
sbi_attach(device_t dev)371 sbi_attach(device_t dev)
372 {
373 struct sbi_softc *sc;
374 #ifdef SMP
375 device_t child;
376 struct sbi_devinfo *di;
377 #endif
378
379 if (sbi_softc != NULL)
380 return (ENXIO);
381
382 sc = device_get_softc(dev);
383 sc->dev = dev;
384 sbi_softc = sc;
385
386 EVENTHANDLER_REGISTER(shutdown_final, sbi_shutdown_final, NULL,
387 SHUTDOWN_PRI_LAST);
388
389 #ifdef SMP
390 di = malloc(sizeof(*di), M_DEVBUF, M_WAITOK | M_ZERO);
391 resource_list_init(&di->rl);
392 child = device_add_child(dev, "sbi_ipi", DEVICE_UNIT_ANY);
393 if (child == NULL) {
394 device_printf(dev, "Could not add sbi_ipi child\n");
395 return (ENXIO);
396 }
397
398 device_set_ivars(child, di);
399 #endif
400
401 return (0);
402 }
403
404 static struct resource_list *
sbi_get_resource_list(device_t bus,device_t child)405 sbi_get_resource_list(device_t bus, device_t child)
406 {
407 struct sbi_devinfo *di;
408
409 di = device_get_ivars(child);
410 KASSERT(di != NULL, ("%s: No devinfo", __func__));
411
412 return (&di->rl);
413 }
414
415 static device_method_t sbi_methods[] = {
416 /* Device interface */
417 DEVMETHOD(device_identify, sbi_identify),
418 DEVMETHOD(device_probe, sbi_probe),
419 DEVMETHOD(device_attach, sbi_attach),
420
421 /* Bus interface */
422 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
423 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
424 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
425 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
426 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
427 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
428 DEVMETHOD(bus_get_resource_list, sbi_get_resource_list),
429 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
430 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
431
432 DEVMETHOD_END
433 };
434
435 DEFINE_CLASS_0(sbi, sbi_driver, sbi_methods, sizeof(struct sbi_softc));
436 EARLY_DRIVER_MODULE(sbi, nexus, sbi_driver, 0, 0,
437 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
438