1 /* $OpenBSD: sbi.c,v 1.8 2024/03/29 22:11:34 kettenis Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
33
34 #include <machine/sbi.h>
35
36 #include <dev/cons.h>
37
38 extern void (*cpuresetfn)(void);
39 extern void (*powerdownfn)(void);
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 struct sbi_ret
sbi_get_spec_version(void)50 sbi_get_spec_version(void)
51 {
52 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
53 }
54
55 static struct sbi_ret
sbi_get_impl_id(void)56 sbi_get_impl_id(void)
57 {
58 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
59 }
60
61 static struct sbi_ret
sbi_get_impl_version(void)62 sbi_get_impl_version(void)
63 {
64 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
65 }
66
67 void
sbi_print_version(void)68 sbi_print_version(void)
69 {
70 u_int major;
71 u_int minor;
72
73 /* For legacy SBI implementations. */
74 if (sbi_spec_version == 0) {
75 printf("SBI: Unknown (Legacy) Implementation\n");
76 printf("SBI Specification Version: 0.1\n");
77 return;
78 }
79
80 switch (sbi_impl_id) {
81 case (SBI_IMPL_ID_BBL):
82 printf("SBI: Berkely Boot Loader %lu", sbi_impl_version);
83 break;
84 case (SBI_IMPL_ID_OPENSBI):
85 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
86 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
87 printf("SBI: OpenSBI v%u.%u", major, minor);
88 break;
89 default:
90 printf("SBI: Unrecognized Implementation: %lu", sbi_impl_id);
91 break;
92 }
93
94 major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
95 SBI_SPEC_VERS_MAJOR_OFFSET;
96 minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
97 printf(", SBI Specification Version %u.%u\n", major, minor);
98 }
99
100 #ifdef MULTIPROCESSOR
101
102 int
sbi_hsm_hart_start(u_long hart,u_long start_addr,u_long priv)103 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
104 {
105 struct sbi_ret ret;
106
107 ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr,
108 priv);
109 return (ret.error != 0 ? (int)ret.error : 0);
110 }
111
112 void
sbi_hsm_hart_stop(void)113 sbi_hsm_hart_stop(void)
114 {
115 (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
116 }
117
118 int
sbi_hsm_hart_status(u_long hart)119 sbi_hsm_hart_status(u_long hart)
120 {
121 struct sbi_ret ret;
122
123 ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
124
125 return (ret.error != 0 ? (int)ret.error : (int)ret.value);
126 }
127
128 #endif
129
130 void
sbi_reset(void)131 sbi_reset(void)
132 {
133 SBI_CALL2(SBI_EXT_ID_SRST, SBI_SRST_RESET,
134 SBI_SRST_RESET_WARM_REBOOT, 0);
135 }
136
137 void
sbi_powerdown(void)138 sbi_powerdown(void)
139 {
140 SBI_CALL2(SBI_EXT_ID_SRST, SBI_SRST_RESET,
141 SBI_SRST_RESET_SHUTDOWN, 0);
142 }
143
144 void
sbi_init(void)145 sbi_init(void)
146 {
147 struct sbi_ret sret;
148
149 /*
150 * Get the spec version. For legacy SBI implementations this will
151 * return an error, otherwise it is guaranteed to succeed.
152 */
153 sret = sbi_get_spec_version();
154 if (sret.error != 0) {
155 /* We are running a legacy SBI implementation. */
156 sbi_spec_version = 0;
157 return;
158 }
159
160 /* Set the SBI implementation info. */
161 sbi_spec_version = sret.value;
162 sbi_impl_id = sbi_get_impl_id().value;
163 sbi_impl_version = sbi_get_impl_version().value;
164
165 /*
166 * Probe for legacy extensions. Currently we rely on all of them
167 * to be implemented, but this is not guaranteed by the spec.
168 */
169 KASSERTMSG(sbi_probe_extension(SBI_SET_TIMER) != 0,
170 "SBI doesn't implement sbi_set_timer()");
171 KASSERTMSG(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
172 "SBI doesn't implement sbi_console_putchar()");
173 KASSERTMSG(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
174 "SBI doesn't implement sbi_console_getchar()");
175 KASSERTMSG(sbi_probe_extension(SBI_CLEAR_IPI) != 0,
176 "SBI doesn't implement sbi_clear_ipi()");
177 KASSERTMSG(sbi_probe_extension(SBI_SEND_IPI) != 0,
178 "SBI doesn't implement sbi_send_ipi()");
179 KASSERTMSG(sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
180 "SBI doesn't implement sbi_remote_fence_i()");
181 KASSERTMSG(sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
182 "SBI doesn't implement sbi_remote_sfence_vma()");
183 KASSERTMSG(sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
184 "SBI doesn't implement sbi_remote_sfence_vma_asid()");
185 KASSERTMSG(sbi_probe_extension(SBI_SHUTDOWN) != 0,
186 "SBI doesn't implement sbi_shutdown()");
187
188 /*
189 * Implement reboot and power down if the System Reset
190 * Extension is implemented.
191 */
192 if (sbi_probe_extension(SBI_EXT_ID_SRST) != 0) {
193 cpuresetfn = sbi_reset;
194 powerdownfn = sbi_powerdown;
195 }
196 }
197
198 /*
199 * Early console implementation based on the Console Putchar and
200 * Console Getchar legacy extensions. These extensions are deprecated
201 * but extremely useful for bringing up new boards.
202 */
203
204 void
sbi_cnprobe(struct consdev * cd)205 sbi_cnprobe(struct consdev *cd)
206 {
207 }
208
209 void
sbi_cninit(struct consdev * cd)210 sbi_cninit(struct consdev *cd)
211 {
212 }
213
214 int
sbi_cngetc(dev_t dev)215 sbi_cngetc(dev_t dev)
216 {
217 int c;
218
219 for (;;) {
220 c = sbi_console_getchar();
221 if (c != -1)
222 return c;
223 }
224 }
225
226 void
sbi_cnputc(dev_t dev,int c)227 sbi_cnputc(dev_t dev, int c)
228 {
229 sbi_console_putchar(c);
230 }
231
232 void
sbi_cnpollc(dev_t dev,int on)233 sbi_cnpollc(dev_t dev, int on)
234 {
235 }
236
237 struct consdev sbi_consdev = {
238 .cn_probe = sbi_cnprobe,
239 .cn_init = sbi_cninit,
240 .cn_getc = sbi_cngetc,
241 .cn_putc = sbi_cnputc,
242 .cn_pollc = sbi_cnpollc,
243 };
244
245 struct consdev *cn_tab = &sbi_consdev;
246