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