xref: /freebsd-13-stable/sys/riscv/riscv/sbi.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
33 #include <sys/eventhandler.h>
34 #include <sys/reboot.h>
35 
36 #include <machine/md_var.h>
37 #include <machine/sbi.h>
38 
39 /* SBI Implementation-Specific Definitions */
40 #define	OPENSBI_VERSION_MAJOR_OFFSET	16
41 #define	OPENSBI_VERSION_MINOR_MASK	0xFFFF
42 
43 u_long sbi_spec_version;
44 u_long sbi_impl_id;
45 u_long sbi_impl_version;
46 
47 static bool has_time_extension = false;
48 static bool has_ipi_extension = false;
49 static bool has_rfnc_extension = false;
50 static bool has_srst_extension = false;
51 
52 static struct sbi_ret
sbi_get_spec_version(void)53 sbi_get_spec_version(void)
54 {
55 	return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
56 }
57 
58 static struct sbi_ret
sbi_get_impl_id(void)59 sbi_get_impl_id(void)
60 {
61 	return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
62 }
63 
64 static struct sbi_ret
sbi_get_impl_version(void)65 sbi_get_impl_version(void)
66 {
67 	return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
68 }
69 
70 static struct sbi_ret
sbi_get_mvendorid(void)71 sbi_get_mvendorid(void)
72 {
73 	return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID));
74 }
75 
76 static struct sbi_ret
sbi_get_marchid(void)77 sbi_get_marchid(void)
78 {
79 	return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID));
80 }
81 
82 static struct sbi_ret
sbi_get_mimpid(void)83 sbi_get_mimpid(void)
84 {
85 	return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID));
86 }
87 
88 static void
sbi_shutdown_final(void * dummy __unused,int howto)89 sbi_shutdown_final(void *dummy __unused, int howto)
90 {
91 	if ((howto & RB_POWEROFF) != 0)
92 		sbi_system_reset(SBI_SRST_TYPE_SHUTDOWN, SBI_SRST_REASON_NONE);
93 }
94 
95 void
sbi_system_reset(u_long reset_type,u_long reset_reason)96 sbi_system_reset(u_long reset_type, u_long reset_reason)
97 {
98 	/* Use the SRST extension, if available. */
99 	if (has_srst_extension) {
100 		(void)SBI_CALL2(SBI_EXT_ID_SRST, SBI_SRST_SYSTEM_RESET,
101 		    reset_type, reset_reason);
102 	}
103 	(void)SBI_CALL0(SBI_SHUTDOWN, 0);
104 }
105 
106 void
sbi_print_version(void)107 sbi_print_version(void)
108 {
109 	u_int major;
110 	u_int minor;
111 
112 	/* For legacy SBI implementations. */
113 	if (sbi_spec_version == 0) {
114 		printf("SBI: Unknown (Legacy) Implementation\n");
115 		printf("SBI Specification Version: 0.1\n");
116 		return;
117 	}
118 
119 	switch (sbi_impl_id) {
120 	case (SBI_IMPL_ID_BBL):
121 		printf("SBI: Berkely Boot Loader %lu\n", sbi_impl_version);
122 		break;
123 	case (SBI_IMPL_ID_XVISOR):
124 		printf("SBI: eXtensible Versatile hypervISOR %lu\n",
125 		    sbi_impl_version);
126 		break;
127 	case (SBI_IMPL_ID_KVM):
128 		printf("SBI: Kernel-based Virtual Machine %lu\n",
129 		    sbi_impl_version);
130 		break;
131 	case (SBI_IMPL_ID_RUSTSBI):
132 		printf("SBI: RustSBI %lu\n", sbi_impl_version);
133 		break;
134 	case (SBI_IMPL_ID_DIOSIX):
135 		printf("SBI: Diosix %lu\n", sbi_impl_version);
136 		break;
137 	case (SBI_IMPL_ID_OPENSBI):
138 		major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
139 		minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
140 		printf("SBI: OpenSBI v%u.%u\n", major, minor);
141 		break;
142 	default:
143 		printf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
144 		break;
145 	}
146 
147 	major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
148 	    SBI_SPEC_VERS_MAJOR_OFFSET;
149 	minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
150 	printf("SBI Specification Version: %u.%u\n", major, minor);
151 }
152 
153 void
sbi_set_timer(uint64_t val)154 sbi_set_timer(uint64_t val)
155 {
156 	struct sbi_ret ret;
157 
158 	/* Use the TIME legacy replacement extension, if available. */
159 	if (has_time_extension) {
160 		ret = SBI_CALL1(SBI_EXT_ID_TIME, SBI_TIME_SET_TIMER, val);
161 		MPASS(ret.error == SBI_SUCCESS);
162 	} else {
163 		(void)SBI_CALL1(SBI_SET_TIMER, 0, val);
164 	}
165 }
166 
167 void
sbi_send_ipi(const u_long * hart_mask)168 sbi_send_ipi(const u_long *hart_mask)
169 {
170 	struct sbi_ret ret;
171 
172 	/* Use the IPI legacy replacement extension, if available. */
173 	if (has_ipi_extension) {
174 		ret = SBI_CALL2(SBI_EXT_ID_IPI, SBI_IPI_SEND_IPI,
175 		    *hart_mask, 0);
176 		MPASS(ret.error == SBI_SUCCESS);
177 	} else {
178 		(void)SBI_CALL1(SBI_SEND_IPI, 0, (uint64_t)hart_mask);
179 	}
180 }
181 
182 void
sbi_remote_fence_i(const u_long * hart_mask)183 sbi_remote_fence_i(const u_long *hart_mask)
184 {
185 	struct sbi_ret ret;
186 
187 	/* Use the RFENCE legacy replacement extension, if available. */
188 	if (has_rfnc_extension) {
189 		ret = SBI_CALL2(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_FENCE_I,
190 		    *hart_mask, 0);
191 		MPASS(ret.error == SBI_SUCCESS);
192 	} else {
193 		(void)SBI_CALL1(SBI_REMOTE_FENCE_I, 0, (uint64_t)hart_mask);
194 	}
195 }
196 
197 void
sbi_remote_sfence_vma(const u_long * hart_mask,u_long start,u_long size)198 sbi_remote_sfence_vma(const u_long *hart_mask, u_long start, u_long size)
199 {
200 	struct sbi_ret ret;
201 
202 	/* Use the RFENCE legacy replacement extension, if available. */
203 	if (has_rfnc_extension) {
204 		ret = SBI_CALL4(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA,
205 		    *hart_mask, 0, start, size);
206 		MPASS(ret.error == SBI_SUCCESS);
207 	} else {
208 		(void)SBI_CALL3(SBI_REMOTE_SFENCE_VMA, 0, (uint64_t)hart_mask,
209 		    start, size);
210 	}
211 }
212 
213 void
sbi_remote_sfence_vma_asid(const u_long * hart_mask,u_long start,u_long size,u_long asid)214 sbi_remote_sfence_vma_asid(const u_long *hart_mask, u_long start, u_long size,
215     u_long asid)
216 {
217 	struct sbi_ret ret;
218 
219 	/* Use the RFENCE legacy replacement extension, if available. */
220 	if (has_rfnc_extension) {
221 		ret = SBI_CALL5(SBI_EXT_ID_RFNC,
222 		    SBI_RFNC_REMOTE_SFENCE_VMA_ASID, *hart_mask, 0, start,
223 		    size, asid);
224 		MPASS(ret.error == SBI_SUCCESS);
225 	} else {
226 		(void)SBI_CALL4(SBI_REMOTE_SFENCE_VMA_ASID, 0,
227 		    (uint64_t)hart_mask, start, size, asid);
228 	}
229 }
230 
231 int
sbi_hsm_hart_start(u_long hart,u_long start_addr,u_long priv)232 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
233 {
234 	struct sbi_ret ret;
235 
236 	ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr,
237 	    priv);
238 	return (ret.error != 0 ? (int)ret.error : 0);
239 }
240 
241 void
sbi_hsm_hart_stop(void)242 sbi_hsm_hart_stop(void)
243 {
244 	(void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
245 }
246 
247 int
sbi_hsm_hart_status(u_long hart)248 sbi_hsm_hart_status(u_long hart)
249 {
250 	struct sbi_ret ret;
251 
252 	ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
253 
254 	return (ret.error != 0 ? (int)ret.error : (int)ret.value);
255 }
256 
257 void
sbi_init(void)258 sbi_init(void)
259 {
260 	struct sbi_ret sret;
261 
262 	/*
263 	 * Get the spec version. For legacy SBI implementations this will
264 	 * return an error, otherwise it is guaranteed to succeed.
265 	 */
266 	sret = sbi_get_spec_version();
267 	if (sret.error != 0) {
268 		/* We are running a legacy SBI implementation. */
269 		sbi_spec_version = 0;
270 		return;
271 	}
272 
273 	/* Set the SBI implementation info. */
274 	sbi_spec_version = sret.value;
275 	sbi_impl_id = sbi_get_impl_id().value;
276 	sbi_impl_version = sbi_get_impl_version().value;
277 
278 	/* Set the hardware implementation info. */
279 	mvendorid = sbi_get_mvendorid().value;
280 	marchid = sbi_get_marchid().value;
281 	mimpid = sbi_get_mimpid().value;
282 
283 	/* Probe for legacy replacement extensions. */
284 	if (sbi_probe_extension(SBI_EXT_ID_TIME) != 0)
285 		has_time_extension = true;
286 	if (sbi_probe_extension(SBI_EXT_ID_IPI) != 0)
287 		has_ipi_extension = true;
288 	if (sbi_probe_extension(SBI_EXT_ID_RFNC) != 0)
289 		has_rfnc_extension = true;
290 	if (sbi_probe_extension(SBI_EXT_ID_SRST) != 0)
291 		has_srst_extension = true;
292 
293 	/*
294 	 * Probe for legacy extensions. We still rely on many of them to be
295 	 * implemented, but this is not guaranteed by the spec.
296 	 */
297 	KASSERT(has_time_extension || sbi_probe_extension(SBI_SET_TIMER) != 0,
298 	    ("SBI doesn't implement sbi_set_timer()"));
299 	KASSERT(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
300 	    ("SBI doesn't implement sbi_console_putchar()"));
301 	KASSERT(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
302 	    ("SBI doesn't implement sbi_console_getchar()"));
303 	KASSERT(has_ipi_extension || sbi_probe_extension(SBI_SEND_IPI) != 0,
304 	    ("SBI doesn't implement sbi_send_ipi()"));
305 	KASSERT(has_rfnc_extension ||
306 	    sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
307 	    ("SBI doesn't implement sbi_remote_fence_i()"));
308 	KASSERT(has_rfnc_extension ||
309 	    sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
310 	    ("SBI doesn't implement sbi_remote_sfence_vma()"));
311 	KASSERT(has_rfnc_extension ||
312 	    sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
313 	    ("SBI doesn't implement sbi_remote_sfence_vma_asid()"));
314 	KASSERT(has_srst_extension || sbi_probe_extension(SBI_SHUTDOWN) != 0,
315 	    ("SBI doesn't implement a shutdown or reset extension"));
316 }
317 
318 static void
sbi_late_init(void * dummy __unused)319 sbi_late_init(void *dummy __unused)
320 {
321 	EVENTHANDLER_REGISTER(shutdown_final, sbi_shutdown_final, NULL,
322 	    SHUTDOWN_PRI_LAST);
323 }
324 
325 SYSINIT(sbi, SI_SUB_KLD, SI_ORDER_ANY, sbi_late_init, NULL);
326