-
Notifications
You must be signed in to change notification settings - Fork 49
/
device.h
280 lines (248 loc) · 8.29 KB
/
device.h
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once
#include "netdev.h"
#include "riscv.h"
#include "virtio.h"
/* RAM */
#define RAM_SIZE (512 * 1024 * 1024)
#define DTB_SIZE (1 * 1024 * 1024)
#define INITRD_SIZE (8 * 1024 * 1024)
void ram_read(hart_t *core,
uint32_t *mem,
const uint32_t addr,
const uint8_t width,
uint32_t *value);
void ram_write(hart_t *core,
uint32_t *mem,
const uint32_t addr,
const uint8_t width,
const uint32_t value);
/* PLIC */
typedef struct {
uint32_t masked;
uint32_t ip; /* support 32 interrupt sources only */
uint32_t ie[32]; /* support 32 sources to 32 contexts only */
/* state of input interrupt lines (level-triggered), set by environment */
uint32_t active;
} plic_state_t;
void plic_update_interrupts(vm_t *vm, plic_state_t *plic);
void plic_read(hart_t *core,
plic_state_t *plic,
uint32_t addr,
uint8_t width,
uint32_t *value);
void plic_write(hart_t *core,
plic_state_t *plic,
uint32_t addr,
uint8_t width,
uint32_t value);
/* UART */
#define IRQ_UART 1
#define IRQ_UART_BIT (1 << IRQ_UART)
typedef struct {
uint8_t dll, dlh; /**< divisor (ignored) */
uint8_t lcr; /**< UART config */
uint8_t ier; /**< interrupt config */
uint8_t current_int, pending_ints; /**< interrupt status */
/* other output signals, loopback mode (ignored) */
uint8_t mcr;
/* I/O handling */
int in_fd, out_fd;
bool in_ready;
} u8250_state_t;
void u8250_update_interrupts(u8250_state_t *uart);
void u8250_read(hart_t *core,
u8250_state_t *uart,
uint32_t addr,
uint8_t width,
uint32_t *value);
void u8250_write(hart_t *core,
u8250_state_t *uart,
uint32_t addr,
uint8_t width,
uint32_t value);
void u8250_check_ready(u8250_state_t *uart);
void capture_keyboard_input();
/* virtio-net */
#if SEMU_HAS(VIRTIONET)
#define IRQ_VNET 2
#define IRQ_VNET_BIT (1 << IRQ_VNET)
typedef struct {
uint32_t QueueNum;
uint32_t QueueDesc;
uint32_t QueueAvail;
uint32_t QueueUsed;
uint16_t last_avail;
bool ready;
bool fd_ready;
} virtio_net_queue_t;
typedef struct {
/* feature negotiation */
uint32_t DeviceFeaturesSel;
uint32_t DriverFeatures;
uint32_t DriverFeaturesSel;
/* queue config */
uint32_t QueueSel;
virtio_net_queue_t queues[2];
/* status */
uint32_t Status;
uint32_t InterruptStatus;
/* supplied by environment */
netdev_t peer;
uint32_t *ram;
/* implementation-specific */
void *priv;
} virtio_net_state_t;
void virtio_net_read(hart_t *core,
virtio_net_state_t *vnet,
uint32_t addr,
uint8_t width,
uint32_t *value);
void virtio_net_write(hart_t *core,
virtio_net_state_t *vnet,
uint32_t addr,
uint8_t width,
uint32_t value);
void virtio_net_refresh_queue(virtio_net_state_t *vnet);
bool virtio_net_init(virtio_net_state_t *vnet, const char *name);
#endif /* SEMU_HAS(VIRTIONET) */
/* VirtIO-Block */
#if SEMU_HAS(VIRTIOBLK)
#define IRQ_VBLK 3
#define IRQ_VBLK_BIT (1 << IRQ_VBLK)
typedef struct {
uint32_t QueueNum;
uint32_t QueueDesc;
uint32_t QueueAvail;
uint32_t QueueUsed;
uint16_t last_avail;
bool ready;
} virtio_blk_queue_t;
typedef struct {
/* feature negotiation */
uint32_t DeviceFeaturesSel;
uint32_t DriverFeatures;
uint32_t DriverFeaturesSel;
/* queue config */
uint32_t QueueSel;
virtio_blk_queue_t queues[2];
/* status */
uint32_t Status;
uint32_t InterruptStatus;
/* supplied by environment */
uint32_t *ram;
uint32_t *disk;
/* implementation-specific */
void *priv;
} virtio_blk_state_t;
void virtio_blk_read(hart_t *vm,
virtio_blk_state_t *vblk,
uint32_t addr,
uint8_t width,
uint32_t *value);
void virtio_blk_write(hart_t *vm,
virtio_blk_state_t *vblk,
uint32_t addr,
uint8_t width,
uint32_t value);
uint32_t *virtio_blk_init(virtio_blk_state_t *vblk, char *disk_file);
#endif /* SEMU_HAS(VIRTIOBLK) */
/* ACLINT MTIMER */
typedef struct {
/* A MTIMER device has two separate base addresses: one for the MTIME
* register and another for the MTIMECMP registers.
*
* The MTIME register is a 64-bit read-write register that contains the
* number of cycles counted based on a fixed reference frequency.
*
* The MTIMECMP registers are 'per-HART' 64-bit read-write registers. It
* contains the MTIME register value at which machine-level timer interrupt
* is to be triggered for the corresponding HART.
*
* Up to 4095 MTIMECMP registers can exist, corresponding to 4095 HARTs in
* the system.
*
* For more details, please refer to the register map at:
* https://github.com/riscv/riscv-aclint/blob/main/riscv-aclint.adoc#21-register-map
*/
uint64_t *mtimecmp;
semu_timer_t mtime;
} mtimer_state_t;
void aclint_mtimer_update_interrupts(hart_t *hart, mtimer_state_t *mtimer);
void aclint_mtimer_read(hart_t *hart,
mtimer_state_t *mtimer,
uint32_t addr,
uint8_t width,
uint32_t *value);
void aclint_mtimer_write(hart_t *hart,
mtimer_state_t *mtimer,
uint32_t addr,
uint8_t width,
uint32_t value);
/* ACLINT MSWI */
typedef struct {
/* The MSWI device provides machine-level IPI functionality for a set of
* HARTs on a RISC-V platform. It has an IPI register (MSIP) for each HART
* connected to the MSWI device.
*
* Up to 4095 MSIP registers can be used, corresponding to 4095 HARTs in the
* system. The 4096th MSIP register is reserved for future use.
*
* For more details, please refer to the register map at:
* https://github.com/riscv/riscv-aclint/blob/main/riscv-aclint.adoc#31-register-map
*/
uint32_t *msip;
} mswi_state_t;
void aclint_mswi_update_interrupts(hart_t *hart, mswi_state_t *mswi);
void aclint_mswi_read(hart_t *hart,
mswi_state_t *mswi,
uint32_t addr,
uint8_t width,
uint32_t *value);
void aclint_mswi_write(hart_t *hart,
mswi_state_t *mswi,
uint32_t addr,
uint8_t width,
uint32_t value);
/* ACLINT SSWI */
typedef struct {
/* The SSWI device provides supervisor-level IPI functionality for a set of
* HARTs on a RISC-V platform. It provides a register to set an IPI
* (SETSSIP) for each HART connected to the SSWI device.
*
* Up to 4095 SETSSIP registers can be used, corresponding to 4095 HARTs in
* the system. The 4096th SETSSIP register is reserved for future use.
*
* For more details, please refer to the register map at:
* https://github.com/riscv/riscv-aclint/blob/main/riscv-aclint.adoc#41-register-map
*/
uint32_t *ssip;
} sswi_state_t;
void aclint_sswi_update_interrupts(hart_t *hart, sswi_state_t *sswi);
void aclint_sswi_read(hart_t *hart,
sswi_state_t *sswi,
uint32_t addr,
uint8_t width,
uint32_t *value);
void aclint_sswi_write(hart_t *hart,
sswi_state_t *sswi,
uint32_t addr,
uint8_t width,
uint32_t value);
/* memory mapping */
typedef struct {
bool stopped;
uint32_t *ram;
uint32_t *disk;
plic_state_t plic;
u8250_state_t uart;
#if SEMU_HAS(VIRTIONET)
virtio_net_state_t vnet;
#endif
#if SEMU_HAS(VIRTIOBLK)
virtio_blk_state_t vblk;
#endif
/* ACLINT */
mtimer_state_t mtimer;
mswi_state_t mswi;
sswi_state_t sswi;
} emu_state_t;