2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/eventhandler.h>
36 #include <sys/reboot.h>
38 #include <machine/md_var.h>
39 #include <machine/sbi.h>
41 /* SBI Implementation-Specific Definitions */
42 #define OPENSBI_VERSION_MAJOR_OFFSET 16
43 #define OPENSBI_VERSION_MINOR_MASK 0xFFFF
45 u_long sbi_spec_version;
47 u_long sbi_impl_version;
49 static bool has_time_extension = false;
50 static bool has_ipi_extension = false;
51 static bool has_rfnc_extension = false;
54 sbi_get_spec_version(void)
56 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
62 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
66 sbi_get_impl_version(void)
68 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
72 sbi_get_mvendorid(void)
74 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID));
80 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID));
86 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID));
90 sbi_shutdown_final(void *dummy __unused, int howto)
92 if ((howto & RB_POWEROFF) != 0)
97 sbi_print_version(void)
102 /* For legacy SBI implementations. */
103 if (sbi_spec_version == 0) {
104 printf("SBI: Unknown (Legacy) Implementation\n");
105 printf("SBI Specification Version: 0.1\n");
109 switch (sbi_impl_id) {
110 case (SBI_IMPL_ID_BBL):
111 printf("SBI: Berkely Boot Loader %lu\n", sbi_impl_version);
113 case (SBI_IMPL_ID_OPENSBI):
114 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
115 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
116 printf("SBI: OpenSBI v%u.%u\n", major, minor);
119 printf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
123 major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
124 SBI_SPEC_VERS_MAJOR_OFFSET;
125 minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
126 printf("SBI Specification Version: %u.%u\n", major, minor);
130 sbi_set_timer(uint64_t val)
134 /* Use the TIME legacy replacement extension, if available. */
135 if (has_time_extension) {
136 ret = SBI_CALL1(SBI_EXT_ID_TIME, SBI_TIME_SET_TIMER, val);
137 MPASS(ret.error == SBI_SUCCESS);
139 (void)SBI_CALL1(SBI_SET_TIMER, 0, val);
144 sbi_send_ipi(const u_long *hart_mask)
148 /* Use the IPI legacy replacement extension, if available. */
149 if (has_ipi_extension) {
150 ret = SBI_CALL2(SBI_EXT_ID_IPI, SBI_IPI_SEND_IPI,
152 MPASS(ret.error == SBI_SUCCESS);
154 (void)SBI_CALL1(SBI_SEND_IPI, 0, (uint64_t)hart_mask);
159 sbi_remote_fence_i(const u_long *hart_mask)
163 /* Use the RFENCE legacy replacement extension, if available. */
164 if (has_rfnc_extension) {
165 ret = SBI_CALL2(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_FENCE_I,
167 MPASS(ret.error == SBI_SUCCESS);
169 (void)SBI_CALL1(SBI_REMOTE_FENCE_I, 0, (uint64_t)hart_mask);
174 sbi_remote_sfence_vma(const u_long *hart_mask, u_long start, u_long size)
178 /* Use the RFENCE legacy replacement extension, if available. */
179 if (has_rfnc_extension) {
180 ret = SBI_CALL4(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA,
181 *hart_mask, 0, start, size);
182 MPASS(ret.error == SBI_SUCCESS);
184 (void)SBI_CALL3(SBI_REMOTE_SFENCE_VMA, 0, (uint64_t)hart_mask,
190 sbi_remote_sfence_vma_asid(const u_long *hart_mask, u_long start, u_long size,
195 /* Use the RFENCE legacy replacement extension, if available. */
196 if (has_rfnc_extension) {
197 ret = SBI_CALL5(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA_ASID,
198 *hart_mask, 0, start, size, asid);
199 MPASS(ret.error == SBI_SUCCESS);
201 (void)SBI_CALL4(SBI_REMOTE_SFENCE_VMA_ASID, 0,
202 (uint64_t)hart_mask, start, size, asid);
207 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
211 ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr, priv);
212 return (ret.error != 0 ? (int)ret.error : 0);
216 sbi_hsm_hart_stop(void)
218 (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
222 sbi_hsm_hart_status(u_long hart)
226 ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
228 return (ret.error != 0 ? (int)ret.error : (int)ret.value);
237 * Get the spec version. For legacy SBI implementations this will
238 * return an error, otherwise it is guaranteed to succeed.
240 sret = sbi_get_spec_version();
241 if (sret.error != 0) {
242 /* We are running a legacy SBI implementation. */
243 sbi_spec_version = 0;
247 /* Set the SBI implementation info. */
248 sbi_spec_version = sret.value;
249 sbi_impl_id = sbi_get_impl_id().value;
250 sbi_impl_version = sbi_get_impl_version().value;
252 /* Set the hardware implementation info. */
253 mvendorid = sbi_get_mvendorid().value;
254 marchid = sbi_get_marchid().value;
255 mimpid = sbi_get_mimpid().value;
257 /* Probe for legacy replacement extensions. */
258 if (sbi_probe_extension(SBI_EXT_ID_TIME) != 0)
259 has_time_extension = true;
260 if (sbi_probe_extension(SBI_EXT_ID_IPI) != 0)
261 has_ipi_extension = true;
262 if (sbi_probe_extension(SBI_EXT_ID_RFNC) != 0)
263 has_rfnc_extension = true;
266 * Probe for legacy extensions. We still rely on many of them to be
267 * implemented, but this is not guaranteed by the spec.
269 KASSERT(has_time_extension || sbi_probe_extension(SBI_SET_TIMER) != 0,
270 ("SBI doesn't implement sbi_set_timer()"));
271 KASSERT(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
272 ("SBI doesn't implement sbi_console_putchar()"));
273 KASSERT(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
274 ("SBI doesn't implement sbi_console_getchar()"));
275 KASSERT(has_ipi_extension || sbi_probe_extension(SBI_SEND_IPI) != 0,
276 ("SBI doesn't implement sbi_send_ipi()"));
277 KASSERT(has_rfnc_extension ||
278 sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
279 ("SBI doesn't implement sbi_remote_fence_i()"));
280 KASSERT(has_rfnc_extension ||
281 sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
282 ("SBI doesn't implement sbi_remote_sfence_vma()"));
283 KASSERT(has_rfnc_extension ||
284 sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
285 ("SBI doesn't implement sbi_remote_sfence_vma_asid()"));
286 KASSERT(sbi_probe_extension(SBI_SHUTDOWN) != 0,
287 ("SBI doesn't implement sbi_shutdown()"));
291 sbi_late_init(void *dummy __unused)
293 EVENTHANDLER_REGISTER(shutdown_final, sbi_shutdown_final, NULL,
297 SYSINIT(sbi, SI_SUB_KLD, SI_ORDER_ANY, sbi_late_init, NULL);