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