Skip to content

Commit fdfdb90

Browse files
authored
Merge pull request #1 from BernardXiong/lee_rv64
[bsp] Use sbi from freebsd
2 parents 5036816 + f960128 commit fdfdb90

File tree

4 files changed

+483
-117
lines changed

4 files changed

+483
-117
lines changed

bsp/qemu-riscv-virt64/driver/board.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,13 @@
2222
#include "riscv.h"
2323
#include "stack.h"
2424

25-
void init_bss(void)
26-
{
27-
unsigned int *dst;
28-
29-
dst = &__bss_start;
30-
while (dst < &__bss_end)
31-
{
32-
*dst++ = 0;
33-
}
34-
}
35-
3625
void primary_cpu_entry(void)
3726
{
3827
extern void entry(void);
3928

4029
/* disable global interrupt */
41-
init_bss();
30+
rt_memset(&__bss_start, 0x0, (rt_uint8_t*)&__bss_end - (rt_uint8_t*)&__bss_start);
31+
4232
rt_hw_interrupt_disable();
4333
entry();
4434
}
@@ -76,7 +66,7 @@ void rt_hw_board_init(void)
7666

7767
void rt_hw_cpu_reset(void)
7868
{
79-
SBI_CALL_0(SBI_SHUTDOWN);
69+
sbi_shutdown();
8070
while(1);
8171
}
8272
MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reset machine);

bsp/qemu-riscv-virt64/driver/drv_uart.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,18 @@ static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg
9898

9999
static int drv_uart_putc(struct rt_serial_device *serial, char c)
100100
{
101-
SBI_CALL_1(SBI_CONSOLE_PUTCHAR, c);
101+
sbi_console_putchar(c);
102102
return (1);
103103
}
104104

105105
static int drv_uart_getc(struct rt_serial_device *serial)
106106
{
107-
return SBI_CALL_0(SBI_CONSOLE_GETCHAR);
108-
}
109-
110-
void drv_uart_puts(char *str)
111-
{
112-
sbi_console_putstr(str);
107+
return sbi_console_getchar();
113108
}
114109

115110
char rt_hw_console_getchar(void)
116111
{
117-
return SBI_CALL_0(SBI_CONSOLE_GETCHAR);
112+
return sbi_console_getchar();
118113
}
119114

120115
static void uart_rx(void *param)

bsp/qemu-riscv-virt64/driver/sbi.c

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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 <stdbool.h>
40+
#include <rtthread.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+
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+
void
73+
sbi_print_version(void)
74+
{
75+
u_int major;
76+
u_int minor;
77+
78+
/* For legacy SBI implementations. */
79+
if (sbi_spec_version == 0)
80+
{
81+
rt_kprintf("SBI: Unknown (Legacy) Implementation\n");
82+
rt_kprintf("SBI Specification Version: 0.1\n");
83+
return;
84+
}
85+
86+
switch (sbi_impl_id)
87+
{
88+
case (SBI_IMPL_ID_BBL):
89+
rt_kprintf("SBI: Berkely Boot Loader %lu\n", sbi_impl_version);
90+
break;
91+
case (SBI_IMPL_ID_XVISOR):
92+
rt_kprintf("SBI: eXtensible Versatile hypervISOR %lu\n", sbi_impl_version);
93+
break;
94+
case (SBI_IMPL_ID_KVM):
95+
rt_kprintf("SBI: Kernel-based Virtual Machine %lu\n", sbi_impl_version);
96+
break;
97+
case (SBI_IMPL_ID_RUSTSBI):
98+
rt_kprintf("SBI: RustSBI %lu\n", sbi_impl_version);
99+
break;
100+
case (SBI_IMPL_ID_DIOSIX):
101+
rt_kprintf("SBI: Diosix %lu\n", sbi_impl_version);
102+
break;
103+
case (SBI_IMPL_ID_OPENSBI):
104+
major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
105+
minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
106+
rt_kprintf("SBI: OpenSBI v%u.%u\n", major, minor);
107+
break;
108+
default:
109+
rt_kprintf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
110+
break;
111+
}
112+
113+
major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
114+
SBI_SPEC_VERS_MAJOR_OFFSET;
115+
minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
116+
rt_kprintf("SBI Specification Version: %u.%u\n", major, minor);
117+
}
118+
119+
void
120+
sbi_set_timer(uint64_t val)
121+
{
122+
struct sbi_ret ret;
123+
124+
/* Use the TIME legacy replacement extension, if available. */
125+
if (has_time_extension)
126+
{
127+
ret = SBI_CALL1(SBI_EXT_ID_TIME, SBI_TIME_SET_TIMER, val);
128+
RT_ASSERT(ret.error == SBI_SUCCESS);
129+
}
130+
else
131+
{
132+
(void)SBI_CALL1(SBI_SET_TIMER, 0, val);
133+
}
134+
}
135+
136+
void
137+
sbi_send_ipi(const unsigned long *hart_mask)
138+
{
139+
struct sbi_ret ret;
140+
141+
/* Use the IPI legacy replacement extension, if available. */
142+
if (has_ipi_extension)
143+
{
144+
ret = SBI_CALL2(SBI_EXT_ID_IPI, SBI_IPI_SEND_IPI,
145+
*hart_mask, 0);
146+
RT_ASSERT(ret.error == SBI_SUCCESS);
147+
}
148+
else
149+
{
150+
(void)SBI_CALL1(SBI_SEND_IPI, 0, (uint64_t)hart_mask);
151+
}
152+
}
153+
154+
void
155+
sbi_remote_fence_i(const unsigned long *hart_mask)
156+
{
157+
struct sbi_ret ret;
158+
159+
/* Use the RFENCE legacy replacement extension, if available. */
160+
if (has_rfnc_extension)
161+
{
162+
ret = SBI_CALL2(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_FENCE_I,
163+
*hart_mask, 0);
164+
RT_ASSERT(ret.error == SBI_SUCCESS);
165+
}
166+
else
167+
{
168+
(void)SBI_CALL1(SBI_REMOTE_FENCE_I, 0, (uint64_t)hart_mask);
169+
}
170+
}
171+
172+
void
173+
sbi_remote_sfence_vma(const unsigned long *hart_mask, unsigned long start, unsigned long size)
174+
{
175+
struct sbi_ret ret;
176+
177+
/* Use the RFENCE legacy replacement extension, if available. */
178+
if (has_rfnc_extension)
179+
{
180+
ret = SBI_CALL4(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA,
181+
*hart_mask, 0, start, size);
182+
RT_ASSERT(ret.error == SBI_SUCCESS);
183+
}
184+
else
185+
{
186+
(void)SBI_CALL3(SBI_REMOTE_SFENCE_VMA, 0, (uint64_t)hart_mask,
187+
start, size);
188+
}
189+
}
190+
191+
void
192+
sbi_remote_sfence_vma_asid(const unsigned long *hart_mask, unsigned long start, unsigned long size,
193+
unsigned long asid)
194+
{
195+
struct sbi_ret ret;
196+
197+
/* Use the RFENCE legacy replacement extension, if available. */
198+
if (has_rfnc_extension)
199+
{
200+
ret = SBI_CALL5(SBI_EXT_ID_RFNC, SBI_RFNC_REMOTE_SFENCE_VMA_ASID,
201+
*hart_mask, 0, start, size, asid);
202+
RT_ASSERT(ret.error == SBI_SUCCESS);
203+
}
204+
else
205+
{
206+
(void)SBI_CALL4(SBI_REMOTE_SFENCE_VMA_ASID, 0,
207+
(uint64_t)hart_mask, start, size, asid);
208+
}
209+
}
210+
211+
int
212+
sbi_hsm_hart_start(unsigned long hart, unsigned long start_addr, unsigned long priv)
213+
{
214+
struct sbi_ret ret;
215+
216+
ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr, priv);
217+
return (ret.error != 0 ? (int)ret.error : 0);
218+
}
219+
220+
void
221+
sbi_hsm_hart_stop(void)
222+
{
223+
(void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
224+
}
225+
226+
int
227+
sbi_hsm_hart_status(unsigned long hart)
228+
{
229+
struct sbi_ret ret;
230+
231+
ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
232+
233+
return (ret.error != 0 ? (int)ret.error : (int)ret.value);
234+
}
235+
236+
void
237+
sbi_init(void)
238+
{
239+
struct sbi_ret sret;
240+
241+
/*
242+
* Get the spec version. For legacy SBI implementations this will
243+
* return an error, otherwise it is guaranteed to succeed.
244+
*/
245+
sret = sbi_get_spec_version();
246+
if (sret.error != 0)
247+
{
248+
/* We are running a legacy SBI implementation. */
249+
sbi_spec_version = 0;
250+
return;
251+
}
252+
253+
/* Set the SBI implementation info. */
254+
sbi_spec_version = sret.value;
255+
sbi_impl_id = sbi_get_impl_id().value;
256+
sbi_impl_version = sbi_get_impl_version().value;
257+
258+
/* Probe for legacy replacement extensions. */
259+
if (sbi_probe_extension(SBI_EXT_ID_TIME) != 0)
260+
has_time_extension = true;
261+
if (sbi_probe_extension(SBI_EXT_ID_IPI) != 0)
262+
has_ipi_extension = true;
263+
if (sbi_probe_extension(SBI_EXT_ID_RFNC) != 0)
264+
has_rfnc_extension = true;
265+
}

0 commit comments

Comments
 (0)