]>
Commit | Line | Data |
---|---|---|
a7240d1e MC |
1 | /* |
2 | * QEMU RISC-V Board Compatible with SiFive Freedom U SDK | |
3 | * | |
4 | * Copyright (c) 2016-2017 Sagar Karandikar, [email protected] | |
5 | * Copyright (c) 2017 SiFive, Inc. | |
7b6bb66f | 6 | * Copyright (c) 2019 Bin Meng <[email protected]> |
a7240d1e MC |
7 | * |
8 | * Provides a board compatible with the SiFive Freedom U SDK: | |
9 | * | |
10 | * 0) UART | |
11 | * 1) CLINT (Core Level Interruptor) | |
12 | * 2) PLIC (Platform Level Interrupt Controller) | |
af14c840 | 13 | * 3) PRCI (Power, Reset, Clock, Interrupt) |
5461c4fe | 14 | * 4) OTP (One-Time Programmable) memory with stored serial number |
7b6bb66f | 15 | * 5) GEM (Gigabit Ethernet Controller) and management block |
a7240d1e | 16 | * |
f3d47d58 | 17 | * This board currently generates devicetree dynamically that indicates at least |
ecdfe393 | 18 | * two harts and up to five harts. |
a7240d1e MC |
19 | * |
20 | * This program is free software; you can redistribute it and/or modify it | |
21 | * under the terms and conditions of the GNU General Public License, | |
22 | * version 2 or later, as published by the Free Software Foundation. | |
23 | * | |
24 | * This program is distributed in the hope it will be useful, but WITHOUT | |
25 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
26 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
27 | * more details. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License along with | |
30 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
31 | */ | |
32 | ||
33 | #include "qemu/osdep.h" | |
34 | #include "qemu/log.h" | |
35 | #include "qemu/error-report.h" | |
36 | #include "qapi/error.h" | |
3ca109c3 | 37 | #include "qapi/visitor.h" |
a7240d1e MC |
38 | #include "hw/boards.h" |
39 | #include "hw/loader.h" | |
40 | #include "hw/sysbus.h" | |
41 | #include "hw/char/serial.h" | |
ecdfe393 | 42 | #include "hw/cpu/cluster.h" |
7b6bb66f | 43 | #include "hw/misc/unimp.h" |
a7240d1e MC |
44 | #include "target/riscv/cpu.h" |
45 | #include "hw/riscv/riscv_hart.h" | |
46 | #include "hw/riscv/sifive_plic.h" | |
47 | #include "hw/riscv/sifive_clint.h" | |
48 | #include "hw/riscv/sifive_uart.h" | |
a7240d1e | 49 | #include "hw/riscv/sifive_u.h" |
0ac24d56 | 50 | #include "hw/riscv/boot.h" |
a7240d1e | 51 | #include "chardev/char.h" |
7b6bb66f | 52 | #include "net/eth.h" |
a7240d1e MC |
53 | #include "sysemu/arch_init.h" |
54 | #include "sysemu/device_tree.h" | |
46517dd4 | 55 | #include "sysemu/sysemu.h" |
a7240d1e | 56 | #include "exec/address-spaces.h" |
a7240d1e | 57 | |
5aec3247 MC |
58 | #include <libfdt.h> |
59 | ||
b78c3296 BM |
60 | #if defined(TARGET_RISCV32) |
61 | # define BIOS_FILENAME "opensbi-riscv32-sifive_u-fw_jump.bin" | |
62 | #else | |
63 | # define BIOS_FILENAME "opensbi-riscv64-sifive_u-fw_jump.bin" | |
64 | #endif | |
fdd1bda4 | 65 | |
a7240d1e MC |
66 | static const struct MemmapEntry { |
67 | hwaddr base; | |
68 | hwaddr size; | |
69 | } sifive_u_memmap[] = { | |
70 | [SIFIVE_U_DEBUG] = { 0x0, 0x100 }, | |
5aec3247 | 71 | [SIFIVE_U_MROM] = { 0x1000, 0x11000 }, |
a7240d1e | 72 | [SIFIVE_U_CLINT] = { 0x2000000, 0x10000 }, |
a6902ef0 | 73 | [SIFIVE_U_L2LIM] = { 0x8000000, 0x2000000 }, |
a7240d1e | 74 | [SIFIVE_U_PLIC] = { 0xc000000, 0x4000000 }, |
af14c840 | 75 | [SIFIVE_U_PRCI] = { 0x10000000, 0x1000 }, |
4b55bc2b BM |
76 | [SIFIVE_U_UART0] = { 0x10010000, 0x1000 }, |
77 | [SIFIVE_U_UART1] = { 0x10011000, 0x1000 }, | |
5461c4fe | 78 | [SIFIVE_U_OTP] = { 0x10070000, 0x1000 }, |
1b3a2308 | 79 | [SIFIVE_U_FLASH0] = { 0x20000000, 0x10000000 }, |
a7240d1e | 80 | [SIFIVE_U_DRAM] = { 0x80000000, 0x0 }, |
7b6bb66f BM |
81 | [SIFIVE_U_GEM] = { 0x10090000, 0x2000 }, |
82 | [SIFIVE_U_GEM_MGMT] = { 0x100a0000, 0x1000 }, | |
a7240d1e MC |
83 | }; |
84 | ||
5461c4fe | 85 | #define OTP_SERIAL 1 |
5a7f76a3 AF |
86 | #define GEM_REVISION 0x10070109 |
87 | ||
9f79638e | 88 | static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, |
a7240d1e MC |
89 | uint64_t mem_size, const char *cmdline) |
90 | { | |
ecdfe393 | 91 | MachineState *ms = MACHINE(qdev_get_machine()); |
a7240d1e MC |
92 | void *fdt; |
93 | int cpu; | |
94 | uint32_t *cells; | |
95 | char *nodename; | |
806c64b7 | 96 | char ethclk_names[] = "pclk\0hclk"; |
81e94379 | 97 | uint32_t plic_phandle, prci_phandle, phandle = 1; |
7b6bb66f | 98 | uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle; |
a7240d1e MC |
99 | |
100 | fdt = s->fdt = create_device_tree(&s->fdt_size); | |
101 | if (!fdt) { | |
102 | error_report("create_device_tree() failed"); | |
103 | exit(1); | |
104 | } | |
105 | ||
d372e748 BM |
106 | qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00"); |
107 | qemu_fdt_setprop_string(fdt, "/", "compatible", | |
108 | "sifive,hifive-unleashed-a00"); | |
a7240d1e MC |
109 | qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); |
110 | qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); | |
111 | ||
112 | qemu_fdt_add_subnode(fdt, "/soc"); | |
113 | qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); | |
2a1a6f6d | 114 | qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); |
a7240d1e MC |
115 | qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); |
116 | qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); | |
117 | ||
e1724d09 BM |
118 | hfclk_phandle = phandle++; |
119 | nodename = g_strdup_printf("/hfclk"); | |
120 | qemu_fdt_add_subnode(fdt, nodename); | |
121 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle); | |
122 | qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk"); | |
123 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
124 | SIFIVE_U_HFCLK_FREQ); | |
125 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
126 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
127 | g_free(nodename); | |
128 | ||
129 | rtcclk_phandle = phandle++; | |
130 | nodename = g_strdup_printf("/rtcclk"); | |
131 | qemu_fdt_add_subnode(fdt, nodename); | |
132 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle); | |
133 | qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk"); | |
134 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
135 | SIFIVE_U_RTCCLK_FREQ); | |
136 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
137 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
138 | g_free(nodename); | |
139 | ||
a7240d1e MC |
140 | nodename = g_strdup_printf("/memory@%lx", |
141 | (long)memmap[SIFIVE_U_DRAM].base); | |
142 | qemu_fdt_add_subnode(fdt, nodename); | |
143 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
144 | memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base, | |
145 | mem_size >> 32, mem_size); | |
146 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); | |
147 | g_free(nodename); | |
148 | ||
149 | qemu_fdt_add_subnode(fdt, "/cpus"); | |
2a8756ed MC |
150 | qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", |
151 | SIFIVE_CLINT_TIMEBASE_FREQ); | |
a7240d1e MC |
152 | qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); |
153 | qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); | |
154 | ||
ecdfe393 | 155 | for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) { |
382cb439 | 156 | int cpu_phandle = phandle++; |
a7240d1e MC |
157 | nodename = g_strdup_printf("/cpus/cpu@%d", cpu); |
158 | char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
ecdfe393 | 159 | char *isa; |
a7240d1e | 160 | qemu_fdt_add_subnode(fdt, nodename); |
ecdfe393 BM |
161 | /* cpu 0 is the management hart that does not have mmu */ |
162 | if (cpu != 0) { | |
e883e992 BM |
163 | #if defined(TARGET_RISCV32) |
164 | qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32"); | |
165 | #else | |
ecdfe393 | 166 | qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); |
e883e992 | 167 | #endif |
ecdfe393 BM |
168 | isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]); |
169 | } else { | |
170 | isa = riscv_isa_string(&s->soc.e_cpus.harts[0]); | |
171 | } | |
a7240d1e MC |
172 | qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); |
173 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); | |
174 | qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); | |
175 | qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); | |
176 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); | |
177 | qemu_fdt_add_subnode(fdt, intc); | |
382cb439 | 178 | qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); |
a7240d1e MC |
179 | qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); |
180 | qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); | |
181 | qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); | |
182 | g_free(isa); | |
183 | g_free(intc); | |
184 | g_free(nodename); | |
185 | } | |
186 | ||
ecdfe393 BM |
187 | cells = g_new0(uint32_t, ms->smp.cpus * 4); |
188 | for (cpu = 0; cpu < ms->smp.cpus; cpu++) { | |
a7240d1e MC |
189 | nodename = |
190 | g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
191 | uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); | |
192 | cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); | |
193 | cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); | |
194 | cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); | |
195 | cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); | |
196 | g_free(nodename); | |
197 | } | |
198 | nodename = g_strdup_printf("/soc/clint@%lx", | |
199 | (long)memmap[SIFIVE_U_CLINT].base); | |
200 | qemu_fdt_add_subnode(fdt, nodename); | |
201 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); | |
202 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
203 | 0x0, memmap[SIFIVE_U_CLINT].base, | |
204 | 0x0, memmap[SIFIVE_U_CLINT].size); | |
205 | qemu_fdt_setprop(fdt, nodename, "interrupts-extended", | |
ecdfe393 | 206 | cells, ms->smp.cpus * sizeof(uint32_t) * 4); |
a7240d1e MC |
207 | g_free(cells); |
208 | g_free(nodename); | |
209 | ||
af14c840 BM |
210 | prci_phandle = phandle++; |
211 | nodename = g_strdup_printf("/soc/clock-controller@%lx", | |
212 | (long)memmap[SIFIVE_U_PRCI].base); | |
213 | qemu_fdt_add_subnode(fdt, nodename); | |
214 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle); | |
215 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1); | |
216 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", | |
217 | hfclk_phandle, rtcclk_phandle); | |
218 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
219 | 0x0, memmap[SIFIVE_U_PRCI].base, | |
220 | 0x0, memmap[SIFIVE_U_PRCI].size); | |
221 | qemu_fdt_setprop_string(fdt, nodename, "compatible", | |
222 | "sifive,fu540-c000-prci"); | |
223 | g_free(nodename); | |
224 | ||
382cb439 | 225 | plic_phandle = phandle++; |
ecdfe393 BM |
226 | cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2); |
227 | for (cpu = 0; cpu < ms->smp.cpus; cpu++) { | |
a7240d1e MC |
228 | nodename = |
229 | g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
230 | uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); | |
ecdfe393 BM |
231 | /* cpu 0 is the management hart that does not have S-mode */ |
232 | if (cpu == 0) { | |
233 | cells[0] = cpu_to_be32(intc_phandle); | |
234 | cells[1] = cpu_to_be32(IRQ_M_EXT); | |
235 | } else { | |
236 | cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle); | |
237 | cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT); | |
238 | cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); | |
239 | cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT); | |
240 | } | |
a7240d1e MC |
241 | g_free(nodename); |
242 | } | |
243 | nodename = g_strdup_printf("/soc/interrupt-controller@%lx", | |
244 | (long)memmap[SIFIVE_U_PLIC].base); | |
245 | qemu_fdt_add_subnode(fdt, nodename); | |
246 | qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); | |
247 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); | |
248 | qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); | |
249 | qemu_fdt_setprop(fdt, nodename, "interrupts-extended", | |
ecdfe393 | 250 | cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t)); |
a7240d1e MC |
251 | qemu_fdt_setprop_cells(fdt, nodename, "reg", |
252 | 0x0, memmap[SIFIVE_U_PLIC].base, | |
253 | 0x0, memmap[SIFIVE_U_PLIC].size); | |
98ceee7f | 254 | qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); |
04e7edd1 | 255 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle); |
a7240d1e MC |
256 | plic_phandle = qemu_fdt_get_phandle(fdt, nodename); |
257 | g_free(cells); | |
258 | g_free(nodename); | |
259 | ||
7b6bb66f | 260 | phy_phandle = phandle++; |
5a7f76a3 AF |
261 | nodename = g_strdup_printf("/soc/ethernet@%lx", |
262 | (long)memmap[SIFIVE_U_GEM].base); | |
263 | qemu_fdt_add_subnode(fdt, nodename); | |
7b6bb66f BM |
264 | qemu_fdt_setprop_string(fdt, nodename, "compatible", |
265 | "sifive,fu540-c000-gem"); | |
5a7f76a3 AF |
266 | qemu_fdt_setprop_cells(fdt, nodename, "reg", |
267 | 0x0, memmap[SIFIVE_U_GEM].base, | |
7b6bb66f BM |
268 | 0x0, memmap[SIFIVE_U_GEM].size, |
269 | 0x0, memmap[SIFIVE_U_GEM_MGMT].base, | |
270 | 0x0, memmap[SIFIVE_U_GEM_MGMT].size); | |
5a7f76a3 AF |
271 | qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); |
272 | qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); | |
7b6bb66f | 273 | qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle); |
04e7edd1 BM |
274 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); |
275 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); | |
fe93582c | 276 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", |
806c64b7 | 277 | prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL); |
04ece4f8 | 278 | qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names, |
fe93582c | 279 | sizeof(ethclk_names)); |
7b6bb66f BM |
280 | qemu_fdt_setprop(fdt, nodename, "local-mac-address", |
281 | s->soc.gem.conf.macaddr.a, ETH_ALEN); | |
04e7edd1 BM |
282 | qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); |
283 | qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); | |
c3a28b5d BM |
284 | |
285 | qemu_fdt_add_subnode(fdt, "/aliases"); | |
286 | qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename); | |
287 | ||
5a7f76a3 AF |
288 | g_free(nodename); |
289 | ||
290 | nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", | |
291 | (long)memmap[SIFIVE_U_GEM].base); | |
292 | qemu_fdt_add_subnode(fdt, nodename); | |
7b6bb66f | 293 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle); |
04e7edd1 | 294 | qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0); |
5a7f76a3 AF |
295 | g_free(nodename); |
296 | ||
5f7134d3 | 297 | nodename = g_strdup_printf("/soc/serial@%lx", |
a7240d1e MC |
298 | (long)memmap[SIFIVE_U_UART0].base); |
299 | qemu_fdt_add_subnode(fdt, nodename); | |
300 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); | |
301 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
302 | 0x0, memmap[SIFIVE_U_UART0].base, | |
303 | 0x0, memmap[SIFIVE_U_UART0].size); | |
806c64b7 BM |
304 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", |
305 | prci_phandle, PRCI_CLK_TLCLK); | |
04e7edd1 BM |
306 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); |
307 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); | |
a7240d1e MC |
308 | |
309 | qemu_fdt_add_subnode(fdt, "/chosen"); | |
310 | qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); | |
7c28f4da MC |
311 | if (cmdline) { |
312 | qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); | |
313 | } | |
44e6dcd3 | 314 | |
44e6dcd3 GR |
315 | qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename); |
316 | ||
a7240d1e MC |
317 | g_free(nodename); |
318 | } | |
319 | ||
523e3464 | 320 | static void sifive_u_machine_init(MachineState *machine) |
a7240d1e MC |
321 | { |
322 | const struct MemmapEntry *memmap = sifive_u_memmap; | |
687caef1 | 323 | SiFiveUState *s = RISCV_U_MACHINE(machine); |
5aec3247 | 324 | MemoryRegion *system_memory = get_system_memory(); |
a7240d1e | 325 | MemoryRegion *main_mem = g_new(MemoryRegion, 1); |
1b3a2308 | 326 | MemoryRegion *flash0 = g_new(MemoryRegion, 1); |
fc41ae23 | 327 | target_ulong start_addr = memmap[SIFIVE_U_DRAM].base; |
5aec3247 | 328 | int i; |
a7240d1e | 329 | |
2308092b | 330 | /* Initialize SoC */ |
4eea9d7d AF |
331 | object_initialize_child(OBJECT(machine), "soc", &s->soc, |
332 | sizeof(s->soc), TYPE_RISCV_U_SOC, | |
333 | &error_abort, NULL); | |
3ca109c3 BM |
334 | object_property_set_uint(OBJECT(&s->soc), s->serial, "serial", |
335 | &error_abort); | |
a7240d1e MC |
336 | object_property_set_bool(OBJECT(&s->soc), true, "realized", |
337 | &error_abort); | |
338 | ||
339 | /* register RAM */ | |
340 | memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", | |
341 | machine->ram_size, &error_fatal); | |
5aec3247 | 342 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base, |
2308092b | 343 | main_mem); |
a7240d1e | 344 | |
1b3a2308 AF |
345 | /* register QSPI0 Flash */ |
346 | memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0", | |
347 | memmap[SIFIVE_U_FLASH0].size, &error_fatal); | |
348 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_FLASH0].base, | |
349 | flash0); | |
350 | ||
a7240d1e | 351 | /* create device tree */ |
9f79638e | 352 | create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); |
a7240d1e | 353 | |
fdd1bda4 | 354 | riscv_find_and_load_firmware(machine, BIOS_FILENAME, |
02777ac3 | 355 | memmap[SIFIVE_U_DRAM].base, NULL); |
b3042223 | 356 | |
a7240d1e | 357 | if (machine->kernel_filename) { |
6478dd74 ZSDKN |
358 | uint64_t kernel_entry = riscv_load_kernel(machine->kernel_filename, |
359 | NULL); | |
0f8d4462 GR |
360 | |
361 | if (machine->initrd_filename) { | |
362 | hwaddr start; | |
363 | hwaddr end = riscv_load_initrd(machine->initrd_filename, | |
364 | machine->ram_size, kernel_entry, | |
365 | &start); | |
9f79638e | 366 | qemu_fdt_setprop_cell(s->fdt, "/chosen", |
0f8d4462 | 367 | "linux,initrd-start", start); |
9f79638e | 368 | qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", |
0f8d4462 GR |
369 | end); |
370 | } | |
a7240d1e MC |
371 | } |
372 | ||
fc41ae23 AF |
373 | if (s->start_in_flash) { |
374 | start_addr = memmap[SIFIVE_U_FLASH0].base; | |
375 | } | |
376 | ||
a7240d1e MC |
377 | /* reset vector */ |
378 | uint32_t reset_vec[8] = { | |
379 | 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ | |
380 | 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ | |
381 | 0xf1402573, /* csrr a0, mhartid */ | |
382 | #if defined(TARGET_RISCV32) | |
383 | 0x0182a283, /* lw t0, 24(t0) */ | |
384 | #elif defined(TARGET_RISCV64) | |
385 | 0x0182b283, /* ld t0, 24(t0) */ | |
386 | #endif | |
387 | 0x00028067, /* jr t0 */ | |
388 | 0x00000000, | |
fc41ae23 | 389 | start_addr, /* start: .dword */ |
a7240d1e MC |
390 | 0x00000000, |
391 | /* dtb: */ | |
392 | }; | |
393 | ||
5aec3247 MC |
394 | /* copy in the reset vector in little_endian byte order */ |
395 | for (i = 0; i < sizeof(reset_vec) >> 2; i++) { | |
396 | reset_vec[i] = cpu_to_le32(reset_vec[i]); | |
397 | } | |
398 | rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), | |
399 | memmap[SIFIVE_U_MROM].base, &address_space_memory); | |
a7240d1e MC |
400 | |
401 | /* copy in the device tree */ | |
5aec3247 MC |
402 | if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) > |
403 | memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) { | |
404 | error_report("not enough space to store device-tree"); | |
405 | exit(1); | |
406 | } | |
407 | qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt)); | |
408 | rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt), | |
409 | memmap[SIFIVE_U_MROM].base + sizeof(reset_vec), | |
410 | &address_space_memory); | |
2308092b AF |
411 | } |
412 | ||
523e3464 AF |
413 | static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp) |
414 | { | |
415 | SiFiveUState *s = RISCV_U_MACHINE(obj); | |
416 | ||
417 | return s->start_in_flash; | |
418 | } | |
419 | ||
420 | static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp) | |
421 | { | |
422 | SiFiveUState *s = RISCV_U_MACHINE(obj); | |
423 | ||
424 | s->start_in_flash = value; | |
425 | } | |
426 | ||
3ca109c3 BM |
427 | static void sifive_u_machine_get_serial(Object *obj, Visitor *v, const char *name, |
428 | void *opaque, Error **errp) | |
429 | { | |
430 | visit_type_uint32(v, name, (uint32_t *)opaque, errp); | |
431 | } | |
432 | ||
433 | static void sifive_u_machine_set_serial(Object *obj, Visitor *v, const char *name, | |
434 | void *opaque, Error **errp) | |
435 | { | |
436 | visit_type_uint32(v, name, (uint32_t *)opaque, errp); | |
437 | } | |
438 | ||
523e3464 AF |
439 | static void sifive_u_machine_instance_init(Object *obj) |
440 | { | |
441 | SiFiveUState *s = RISCV_U_MACHINE(obj); | |
442 | ||
443 | s->start_in_flash = false; | |
d2623129 MA |
444 | object_property_add_bool(obj, "start-in-flash", |
445 | sifive_u_machine_get_start_in_flash, | |
446 | sifive_u_machine_set_start_in_flash); | |
523e3464 AF |
447 | object_property_set_description(obj, "start-in-flash", |
448 | "Set on to tell QEMU's ROM to jump to " | |
7eecec7d | 449 | "flash. Otherwise QEMU will jump to DRAM"); |
3ca109c3 BM |
450 | |
451 | s->serial = OTP_SERIAL; | |
d2623129 MA |
452 | object_property_add(obj, "serial", "uint32", |
453 | sifive_u_machine_get_serial, | |
454 | sifive_u_machine_set_serial, NULL, &s->serial); | |
7eecec7d | 455 | object_property_set_description(obj, "serial", "Board serial number"); |
523e3464 AF |
456 | } |
457 | ||
458 | static void sifive_u_machine_class_init(ObjectClass *oc, void *data) | |
459 | { | |
460 | MachineClass *mc = MACHINE_CLASS(oc); | |
461 | ||
462 | mc->desc = "RISC-V Board compatible with SiFive U SDK"; | |
463 | mc->init = sifive_u_machine_init; | |
464 | mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; | |
465 | mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; | |
466 | mc->default_cpus = mc->min_cpus; | |
467 | } | |
468 | ||
469 | static const TypeInfo sifive_u_machine_typeinfo = { | |
470 | .name = MACHINE_TYPE_NAME("sifive_u"), | |
471 | .parent = TYPE_MACHINE, | |
472 | .class_init = sifive_u_machine_class_init, | |
473 | .instance_init = sifive_u_machine_instance_init, | |
474 | .instance_size = sizeof(SiFiveUState), | |
475 | }; | |
476 | ||
477 | static void sifive_u_machine_init_register_types(void) | |
478 | { | |
479 | type_register_static(&sifive_u_machine_typeinfo); | |
480 | } | |
481 | ||
482 | type_init(sifive_u_machine_init_register_types) | |
483 | ||
2308092b AF |
484 | static void riscv_sifive_u_soc_init(Object *obj) |
485 | { | |
c4473127 | 486 | MachineState *ms = MACHINE(qdev_get_machine()); |
2308092b AF |
487 | SiFiveUSoCState *s = RISCV_U_SOC(obj); |
488 | ||
ecdfe393 BM |
489 | object_initialize_child(obj, "e-cluster", &s->e_cluster, |
490 | sizeof(s->e_cluster), TYPE_CPU_CLUSTER, | |
491 | &error_abort, NULL); | |
492 | qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); | |
493 | ||
494 | object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", | |
495 | &s->e_cpus, sizeof(s->e_cpus), | |
496 | TYPE_RISCV_HART_ARRAY, &error_abort, | |
497 | NULL); | |
498 | qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); | |
499 | qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); | |
500 | qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); | |
501 | ||
502 | object_initialize_child(obj, "u-cluster", &s->u_cluster, | |
503 | sizeof(s->u_cluster), TYPE_CPU_CLUSTER, | |
504 | &error_abort, NULL); | |
505 | qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); | |
506 | ||
507 | object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", | |
508 | &s->u_cpus, sizeof(s->u_cpus), | |
509 | TYPE_RISCV_HART_ARRAY, &error_abort, | |
510 | NULL); | |
511 | qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); | |
512 | qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); | |
513 | qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", SIFIVE_U_CPU); | |
5a7f76a3 | 514 | |
af14c840 BM |
515 | sysbus_init_child_obj(obj, "prci", &s->prci, sizeof(s->prci), |
516 | TYPE_SIFIVE_U_PRCI); | |
5461c4fe BM |
517 | sysbus_init_child_obj(obj, "otp", &s->otp, sizeof(s->otp), |
518 | TYPE_SIFIVE_U_OTP); | |
4eea9d7d AF |
519 | sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem), |
520 | TYPE_CADENCE_GEM); | |
2308092b AF |
521 | } |
522 | ||
523 | static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp) | |
524 | { | |
c4473127 | 525 | MachineState *ms = MACHINE(qdev_get_machine()); |
2308092b AF |
526 | SiFiveUSoCState *s = RISCV_U_SOC(dev); |
527 | const struct MemmapEntry *memmap = sifive_u_memmap; | |
528 | MemoryRegion *system_memory = get_system_memory(); | |
529 | MemoryRegion *mask_rom = g_new(MemoryRegion, 1); | |
a6902ef0 | 530 | MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1); |
5a7f76a3 | 531 | qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES]; |
05446f41 BM |
532 | char *plic_hart_config; |
533 | size_t plic_hart_config_len; | |
5a7f76a3 AF |
534 | int i; |
535 | Error *err = NULL; | |
536 | NICInfo *nd = &nd_table[0]; | |
2308092b | 537 | |
ecdfe393 BM |
538 | object_property_set_bool(OBJECT(&s->e_cpus), true, "realized", |
539 | &error_abort); | |
540 | object_property_set_bool(OBJECT(&s->u_cpus), true, "realized", | |
541 | &error_abort); | |
542 | /* | |
543 | * The cluster must be realized after the RISC-V hart array container, | |
544 | * as the container's CPU object is only created on realize, and the | |
545 | * CPU must exist and have been parented into the cluster before the | |
546 | * cluster is realized. | |
547 | */ | |
548 | object_property_set_bool(OBJECT(&s->e_cluster), true, "realized", | |
549 | &error_abort); | |
550 | object_property_set_bool(OBJECT(&s->u_cluster), true, "realized", | |
2308092b AF |
551 | &error_abort); |
552 | ||
553 | /* boot rom */ | |
414c47d2 | 554 | memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom", |
2308092b AF |
555 | memmap[SIFIVE_U_MROM].size, &error_fatal); |
556 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base, | |
557 | mask_rom); | |
a7240d1e | 558 | |
a6902ef0 AF |
559 | /* |
560 | * Add L2-LIM at reset size. | |
561 | * This should be reduced in size as the L2 Cache Controller WayEnable | |
562 | * register is incremented. Unfortunately I don't see a nice (or any) way | |
563 | * to handle reducing or blocking out the L2 LIM while still allowing it | |
564 | * be re returned to all enabled after a reset. For the time being, just | |
565 | * leave it enabled all the time. This won't break anything, but will be | |
566 | * too generous to misbehaving guests. | |
567 | */ | |
568 | memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim", | |
569 | memmap[SIFIVE_U_L2LIM].size, &error_fatal); | |
570 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_L2LIM].base, | |
571 | l2lim_mem); | |
572 | ||
05446f41 | 573 | /* create PLIC hart topology configuration string */ |
c4473127 LX |
574 | plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * |
575 | ms->smp.cpus; | |
05446f41 | 576 | plic_hart_config = g_malloc0(plic_hart_config_len); |
c4473127 | 577 | for (i = 0; i < ms->smp.cpus; i++) { |
05446f41 | 578 | if (i != 0) { |
ef965ce2 BM |
579 | strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG, |
580 | plic_hart_config_len); | |
581 | } else { | |
582 | strncat(plic_hart_config, "M", plic_hart_config_len); | |
05446f41 | 583 | } |
05446f41 BM |
584 | plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); |
585 | } | |
586 | ||
a7240d1e MC |
587 | /* MMIO */ |
588 | s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, | |
05446f41 | 589 | plic_hart_config, |
a7240d1e MC |
590 | SIFIVE_U_PLIC_NUM_SOURCES, |
591 | SIFIVE_U_PLIC_NUM_PRIORITIES, | |
592 | SIFIVE_U_PLIC_PRIORITY_BASE, | |
593 | SIFIVE_U_PLIC_PENDING_BASE, | |
594 | SIFIVE_U_PLIC_ENABLE_BASE, | |
595 | SIFIVE_U_PLIC_ENABLE_STRIDE, | |
596 | SIFIVE_U_PLIC_CONTEXT_BASE, | |
597 | SIFIVE_U_PLIC_CONTEXT_STRIDE, | |
598 | memmap[SIFIVE_U_PLIC].size); | |
bb8136df | 599 | g_free(plic_hart_config); |
5aec3247 | 600 | sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base, |
647a70a1 | 601 | serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); |
194eef09 MC |
602 | sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base, |
603 | serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); | |
a7240d1e | 604 | sifive_clint_create(memmap[SIFIVE_U_CLINT].base, |
c4473127 | 605 | memmap[SIFIVE_U_CLINT].size, ms->smp.cpus, |
5f3616cc | 606 | SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE, false); |
5a7f76a3 | 607 | |
af14c840 BM |
608 | object_property_set_bool(OBJECT(&s->prci), true, "realized", &err); |
609 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_PRCI].base); | |
610 | ||
fda5b000 | 611 | qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial); |
5461c4fe BM |
612 | object_property_set_bool(OBJECT(&s->otp), true, "realized", &err); |
613 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_OTP].base); | |
614 | ||
5a7f76a3 AF |
615 | for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) { |
616 | plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i); | |
617 | } | |
618 | ||
619 | if (nd->used) { | |
620 | qemu_check_nic_model(nd, TYPE_CADENCE_GEM); | |
621 | qdev_set_nic_properties(DEVICE(&s->gem), nd); | |
622 | } | |
623 | object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision", | |
624 | &error_abort); | |
625 | object_property_set_bool(OBJECT(&s->gem), true, "realized", &err); | |
626 | if (err) { | |
627 | error_propagate(errp, err); | |
628 | return; | |
629 | } | |
630 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base); | |
631 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, | |
632 | plic_gpios[SIFIVE_U_GEM_IRQ]); | |
7b6bb66f BM |
633 | |
634 | create_unimplemented_device("riscv.sifive.u.gem-mgmt", | |
635 | memmap[SIFIVE_U_GEM_MGMT].base, memmap[SIFIVE_U_GEM_MGMT].size); | |
a7240d1e MC |
636 | } |
637 | ||
fda5b000 AF |
638 | static Property riscv_sifive_u_soc_props[] = { |
639 | DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL), | |
640 | DEFINE_PROP_END_OF_LIST() | |
641 | }; | |
642 | ||
2308092b AF |
643 | static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data) |
644 | { | |
645 | DeviceClass *dc = DEVICE_CLASS(oc); | |
646 | ||
fda5b000 | 647 | device_class_set_props(dc, riscv_sifive_u_soc_props); |
2308092b AF |
648 | dc->realize = riscv_sifive_u_soc_realize; |
649 | /* Reason: Uses serial_hds in realize function, thus can't be used twice */ | |
650 | dc->user_creatable = false; | |
651 | } | |
652 | ||
653 | static const TypeInfo riscv_sifive_u_soc_type_info = { | |
654 | .name = TYPE_RISCV_U_SOC, | |
655 | .parent = TYPE_DEVICE, | |
656 | .instance_size = sizeof(SiFiveUSoCState), | |
657 | .instance_init = riscv_sifive_u_soc_init, | |
658 | .class_init = riscv_sifive_u_soc_class_init, | |
659 | }; | |
660 | ||
661 | static void riscv_sifive_u_soc_register_types(void) | |
662 | { | |
663 | type_register_static(&riscv_sifive_u_soc_type_info); | |
664 | } | |
665 | ||
666 | type_init(riscv_sifive_u_soc_register_types) |