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