1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-05-18 Bernard port from FreeBSD
9 */
10
11 /*-
12 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
13 *
14 * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "sbi.h"
39 #include <rtthread.h>
40 #include <stdbool.h>
41
42 /* SBI Implementation-Specific Definitions */
43 #define OPENSBI_VERSION_MAJOR_OFFSET 16
44 #define OPENSBI_VERSION_MINOR_MASK 0xFFFF
45
46 unsigned long sbi_spec_version;
47 unsigned long sbi_impl_id;
48 unsigned long sbi_impl_version;
49
50 static bool has_time_extension = false;
51 static bool has_ipi_extension = false;
52 static bool has_rfnc_extension = false;
53
sbi_get_spec_version(void)54 static struct sbi_ret sbi_get_spec_version(void)
55 {
56 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
57 }
58
sbi_get_impl_id(void)59 static struct sbi_ret sbi_get_impl_id(void)
60 {
61 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
62 }
63
sbi_get_impl_version(void)64 static struct sbi_ret sbi_get_impl_version(void)
65 {
66 return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
67 }
68
sbi_print_version(void)69 void sbi_print_version(void)
70 {
71 uint32_t major;
72 uint32_t minor;
73
74 /* For legacy SBI implementations. */
75 if (sbi_spec_version == 0)
76 {
77 rt_kprintf("SBI: Unknown (Legacy) Implementation\n");
78 rt_kprintf("SBI Specification Version: 0.1\n");
79 return;
80 }
81
82 switch (sbi_impl_id)
83 {
84 case (SBI_IMPL_ID_BBL):
85 rt_kprintf("SBI: Berkely Boot Loader %lu\n", sbi_impl_version);
86 break;
87 case (SBI_IMPL_ID_XVISOR):
88 rt_kprintf("SBI: eXtensible Versatile hypervISOR %lu\n",
89 sbi_impl_version);
90 break;
91 case (SBI_IMPL_ID_KVM):
92 rt_kprintf("SBI: Kernel-based Virtual Machine %lu\n", sbi_impl_version);
93 break;
94 case (SBI_IMPL_ID_RUSTSBI):
95 rt_kprintf("SBI: RustSBI %lu\n", sbi_impl_version);
96 break;
97 case (SBI_IMPL_ID_DIOSIX):
98 rt_kprintf("SBI: Diosix %lu\n", sbi_impl_version);
99 break;
100 case (SBI_IMPL_ID_OPENSBI):
101 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
102 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
103 rt_kprintf("SBI: OpenSBI v%u.%u\n", major, minor);
104 break;
105 default:
106 rt_kprintf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
107 break;
108 }
109
110 major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
111 SBI_SPEC_VERS_MAJOR_OFFSET;
112 minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
113 rt_kprintf("SBI Specification Version: %u.%u\n", major, minor);
114 }
115
sbi_set_timer(uint64_t val)116 void sbi_set_timer(uint64_t val)
117 {
118 struct sbi_ret ret;
119
120 /* Use the TIME legacy replacement extension, if available. */
121 if (has_time_extension)
122 {
123 ret = SBI_CALL1(SBI_EXT_ID_TIME, SBI_TIME_SET_TIMER, val);
124 RT_ASSERT(ret.error == SBI_SUCCESS);
125 }
126 else
127 {
128 (void)SBI_CALL1(SBI_SET_TIMER, 0, val);
129 }
130 }
131
sbi_send_ipi(const unsigned long * hart_mask)132 void sbi_send_ipi(const unsigned long *hart_mask)
133 {
134 struct sbi_ret ret;
135
136 /* Use the IPI legacy replacement extension, if available. */
137 if (has_ipi_extension)
138 {
139 ret = SBI_CALL2(SBI_EXT_ID_IPI, SBI_IPI_SEND_IPI, *hart_mask, 0);
140 RT_ASSERT(ret.error == SBI_SUCCESS);
141 }
142 else
143 {
144 (void)SBI_CALL1(SBI_SEND_IPI, 0, (uint64_t)hart_mask);
145 }
146 }
147
sbi_remote_fence_i(const unsigned long * hart_mask)148 void sbi_remote_fence_i(const unsigned long *hart_mask)
149 {
150 struct sbi_ret ret;
151
152 /* Use the RFENCE legacy replacement extension, if available. */
153 if (has_rfnc_extension)
154 {
155 ret =
156 SBI_CALL2(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_FENCE_I, *hart_mask, 0);
157 RT_ASSERT(ret.error == SBI_SUCCESS);
158 }
159 else
160 {
161 (void)SBI_CALL1(SBI_REMOTE_FENCE_I, 0, (uint64_t)hart_mask);
162 }
163 }
164
sbi_remote_sfence_vma(const unsigned long * hart_mask,const unsigned long hart_mask_base,unsigned long start,unsigned long size)165 int sbi_remote_sfence_vma(const unsigned long *hart_mask,
166 const unsigned long hart_mask_base,
167 unsigned long start, unsigned long size)
168 {
169 struct sbi_ret ret = {.error = SBI_SUCCESS};
170
171 /* Use the RFENCE legacy replacement extension, if available. */
172 if (has_rfnc_extension)
173 {
174 ret = SBI_CALL4(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA, *hart_mask,
175 hart_mask_base, start, size);
176 }
177 else
178 {
179 (void)SBI_CALL3(SBI_REMOTE_SFENCE_VMA, 0, (uint64_t)hart_mask, start,
180 size);
181 }
182 return ret.error;
183 }
184
sbi_remote_sfence_vma_asid(const unsigned long * hart_mask,unsigned long start,unsigned long size,unsigned long asid)185 void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
186 unsigned long start, unsigned long size,
187 unsigned long asid)
188 {
189 struct sbi_ret ret;
190
191 /* Use the RFENCE legacy replacement extension, if available. */
192 if (has_rfnc_extension)
193 {
194 ret = SBI_CALL5(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA_ASID,
195 *hart_mask, 0, start, size, asid);
196 RT_ASSERT(ret.error == SBI_SUCCESS);
197 }
198 else
199 {
200 (void)SBI_CALL4(SBI_REMOTE_SFENCE_VMA_ASID, 0, (uint64_t)hart_mask,
201 start, size, asid);
202 }
203 }
204
sbi_hsm_hart_start(unsigned long hart,unsigned long start_addr,unsigned long priv)205 int sbi_hsm_hart_start(unsigned long hart, unsigned long start_addr,
206 unsigned long priv)
207 {
208 struct sbi_ret ret;
209
210 ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr, priv);
211 return (ret.error != 0 ? (int)ret.error : 0);
212 }
213
sbi_hsm_hart_stop(void)214 void sbi_hsm_hart_stop(void)
215 {
216 (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
217 }
218
sbi_hsm_hart_status(unsigned long hart)219 int sbi_hsm_hart_status(unsigned long hart)
220 {
221 struct sbi_ret ret;
222
223 ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
224
225 return (ret.error != 0 ? (int)ret.error : (int)ret.value);
226 }
227
sbi_init(void)228 void sbi_init(void)
229 {
230 struct sbi_ret sret;
231
232 /*
233 * Get the spec version. For legacy SBI implementations this will
234 * return an error, otherwise it is guaranteed to succeed.
235 */
236 sret = sbi_get_spec_version();
237 if (sret.error != 0)
238 {
239 /* We are running a legacy SBI implementation. */
240 sbi_spec_version = 0;
241 return;
242 }
243
244 /* Set the SBI implementation info. */
245 sbi_spec_version = sret.value;
246 sbi_impl_id = sbi_get_impl_id().value;
247 sbi_impl_version = sbi_get_impl_version().value;
248
249 /* Probe for legacy replacement extensions. */
250 if (sbi_probe_extension(SBI_EXT_ID_TIME) != 0)
251 has_time_extension = true;
252 if (sbi_probe_extension(SBI_EXT_ID_IPI) != 0)
253 has_ipi_extension = true;
254 if (sbi_probe_extension(SBI_EXT_ID_RFNC) != 0)
255 has_rfnc_extension = true;
256 }
257
rt_hw_console_output(const char * str)258 void rt_hw_console_output(const char *str)
259 {
260 while (*str)
261 {
262 sbi_console_putchar(*str++);
263 }
264 }
265