forked from apache/nuttx
/
qemu_rv_start.c
244 lines (181 loc) · 6.04 KB
/
qemu_rv_start.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
* arch/risc-v/src/qemu-rv/qemu_rv_start.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/init.h>
#include <nuttx/arch.h>
#include <nuttx/serial/uart_16550.h>
#include <arch/board/board.h>
#include "riscv_internal.h"
#include "chip.h"
#ifdef CONFIG_BUILD_KERNEL
# include "qemu_rv_mm_init.h"
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_DEBUG_FEATURES
#define showprogress(c) up_putc(c)
#else
#define showprogress(c)
#endif
#if defined (CONFIG_BUILD_KERNEL) && !defined (CONFIG_ARCH_USE_S_MODE)
# error "Target requires kernel in S-mode, enable CONFIG_ARCH_USE_S_MODE"
#endif
/****************************************************************************
* Extern Function Declarations
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
extern void __trap_vec(void);
extern void __trap_vec_m(void);
extern void up_mtimer_initialize(void);
#endif
/****************************************************************************
* Name: qemu_rv_clear_bss
****************************************************************************/
void qemu_rv_clear_bss(void)
{
uint32_t *dest;
/* Clear .bss. We'll do this inline (vs. calling memset) just to be
* certain that there are no issues with the state of global variables.
*/
for (dest = (uint32_t *)_sbss; dest < (uint32_t *)_ebss; )
{
*dest++ = 0;
}
}
/****************************************************************************
* Public Data
****************************************************************************/
/* NOTE: g_idle_topstack needs to point the top of the idle stack
* for CPU0 and this value is used in up_initial_state()
*/
uintptr_t g_idle_topstack = QEMU_RV_IDLESTACK_TOP;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: qemu_rv_start
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
void qemu_rv_start_s(int mhartid)
#else
void qemu_rv_start(int mhartid)
#endif
{
/* Configure FPU */
*(volatile uint8_t *)0x10000000 = 'D';////
riscv_fpuconfig();
if (mhartid > 0)
{
goto cpux;
}
#ifndef CONFIG_BUILD_KERNEL
*(volatile uint8_t *)0x10000000 = 'E';////
qemu_rv_clear_bss();
#endif
*(volatile uint8_t *)0x10000000 = 'F';////
showprogress('A');
#ifdef USE_EARLYSERIALINIT
*(volatile uint8_t *)0x10000000 = 'G';////
riscv_earlyserialinit();
#endif
*(volatile uint8_t *)0x10000000 = 'H';////
showprogress('B');
/* Do board initialization */
showprogress('C');
#ifdef CONFIG_BUILD_KERNEL
/* Setup page tables for kernel and enable MMU */
qemu_rv_mm_init();
#endif
/* Call nx_start() */
*(volatile uint8_t *)0x10000000 = 'I';////
nx_start();
cpux:
#ifdef CONFIG_SMP
*(volatile uint8_t *)0x10000000 = 'J';////
riscv_cpu_boot(mhartid);
#endif
*(volatile uint8_t *)0x10000000 = 'K';////
while (true)
{
asm("WFI");
}
}
#ifdef CONFIG_BUILD_KERNEL
/****************************************************************************
* Name: qemu_rv_start
****************************************************************************/
void qemu_rv_start(int mhartid)
{
DEBUGASSERT(mhartid == 0); ////
/* NOTE: still in M-mode */
if (0 == mhartid)
{
qemu_rv_clear_bss();
/* Initialize the per CPU areas */
riscv_percpu_add_hart(mhartid);
}
/* Disable MMU and enable PMP */
WRITE_CSR(satp, 0x0);
////WRITE_CSR(pmpaddr0, 0x3fffffffffffffull);
////WRITE_CSR(pmpcfg0, 0xf);
/* Set exception and interrupt delegation for S-mode */
////WRITE_CSR(medeleg, 0xffff);
////WRITE_CSR(mideleg, 0xffff);
/* Allow to write satp from S-mode */
////CLEAR_CSR(mstatus, MSTATUS_TVM);
/* Set mstatus to S-mode and enable SUM */
////CLEAR_CSR(mstatus, ~MSTATUS_MPP_MASK);
////SET_CSR(mstatus, MSTATUS_MPPS | SSTATUS_SUM);
/* Set the trap vector for S-mode */
WRITE_CSR(stvec, (uintptr_t)__trap_vec);
/* Set the trap vector for M-mode */
////WRITE_CSR(mtvec, (uintptr_t)__trap_vec_m);
if (0 == mhartid)
{
/* Only the primary CPU needs to initialize mtimer
* before entering to S-mode
*/
//// TODO
////up_mtimer_initialize();
}
/* Set mepc to the entry */
////WRITE_CSR(mepc, (uintptr_t)qemu_rv_start_s);
/* Set a0 to mhartid explicitly and enter to S-mode */
////asm volatile (
//// "mv a0, %0 \n"
//// "mret \n"
//// :: "r" (mhartid)
////);
//// Jump to S-Mode Init ourselves
qemu_rv_start_s(mhartid); ////
}
#endif
void riscv_earlyserialinit(void)
{
u16550_earlyserialinit();
}
void riscv_serialinit(void)
{
u16550_serialinit();
}