]> git.proxmox.com Git - mirror_qemu.git/blame - hw/riscv/sifive_u.c
MAINTAINERS: Add a SiFive machine section
[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
834e027a 17 * 7) DMA (Direct Memory Access Controller)
145b2991 18 * 8) SPI0 connected to an SPI flash
722f1352 19 * 9) SPI2 connected to an SD card
a7240d1e 20 *
f3d47d58 21 * This board currently generates devicetree dynamically that indicates at least
ecdfe393 22 * two harts and up to five harts.
a7240d1e
MC
23 *
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms and conditions of the GNU General Public License,
26 * version 2 or later, as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope it will be useful, but WITHOUT
29 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 * more details.
32 *
33 * You should have received a copy of the GNU General Public License along with
34 * this program. If not, see <http://www.gnu.org/licenses/>.
35 */
36
37#include "qemu/osdep.h"
38#include "qemu/log.h"
39#include "qemu/error-report.h"
40#include "qapi/error.h"
3ca109c3 41#include "qapi/visitor.h"
a7240d1e 42#include "hw/boards.h"
5133ed17 43#include "hw/irq.h"
a7240d1e
MC
44#include "hw/loader.h"
45#include "hw/sysbus.h"
46#include "hw/char/serial.h"
ecdfe393 47#include "hw/cpu/cluster.h"
7b6bb66f 48#include "hw/misc/unimp.h"
145b2991 49#include "hw/ssi/ssi.h"
a7240d1e
MC
50#include "target/riscv/cpu.h"
51#include "hw/riscv/riscv_hart.h"
a7240d1e 52#include "hw/riscv/sifive_u.h"
0ac24d56 53#include "hw/riscv/boot.h"
b609b7e3 54#include "hw/char/sifive_uart.h"
406fafd5 55#include "hw/intc/sifive_clint.h"
84fcf3c1 56#include "hw/intc/sifive_plic.h"
a7240d1e 57#include "chardev/char.h"
7b6bb66f 58#include "net/eth.h"
a7240d1e
MC
59#include "sysemu/arch_init.h"
60#include "sysemu/device_tree.h"
5133ed17 61#include "sysemu/runstate.h"
46517dd4 62#include "sysemu/sysemu.h"
a7240d1e 63
5aec3247
MC
64#include <libfdt.h>
65
a7240d1e
MC
66static const struct MemmapEntry {
67 hwaddr base;
68 hwaddr size;
69} sifive_u_memmap[] = {
13b8c354
EH
70 [SIFIVE_U_DEV_DEBUG] = { 0x0, 0x100 },
71 [SIFIVE_U_DEV_MROM] = { 0x1000, 0xf000 },
72 [SIFIVE_U_DEV_CLINT] = { 0x2000000, 0x10000 },
73 [SIFIVE_U_DEV_L2CC] = { 0x2010000, 0x1000 },
74 [SIFIVE_U_DEV_PDMA] = { 0x3000000, 0x100000 },
75 [SIFIVE_U_DEV_L2LIM] = { 0x8000000, 0x2000000 },
76 [SIFIVE_U_DEV_PLIC] = { 0xc000000, 0x4000000 },
77 [SIFIVE_U_DEV_PRCI] = { 0x10000000, 0x1000 },
78 [SIFIVE_U_DEV_UART0] = { 0x10010000, 0x1000 },
79 [SIFIVE_U_DEV_UART1] = { 0x10011000, 0x1000 },
145b2991 80 [SIFIVE_U_DEV_QSPI0] = { 0x10040000, 0x1000 },
722f1352 81 [SIFIVE_U_DEV_QSPI2] = { 0x10050000, 0x1000 },
13b8c354
EH
82 [SIFIVE_U_DEV_GPIO] = { 0x10060000, 0x1000 },
83 [SIFIVE_U_DEV_OTP] = { 0x10070000, 0x1000 },
84 [SIFIVE_U_DEV_GEM] = { 0x10090000, 0x2000 },
85 [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000, 0x1000 },
86 [SIFIVE_U_DEV_DMC] = { 0x100b0000, 0x10000 },
87 [SIFIVE_U_DEV_FLASH0] = { 0x20000000, 0x10000000 },
88 [SIFIVE_U_DEV_DRAM] = { 0x80000000, 0x0 },
a7240d1e
MC
89};
90
5461c4fe 91#define OTP_SERIAL 1
5a7f76a3
AF
92#define GEM_REVISION 0x10070109
93
9f79638e 94static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,
2206ffa6 95 uint64_t mem_size, const char *cmdline, bool is_32_bit)
a7240d1e 96{
ecdfe393 97 MachineState *ms = MACHINE(qdev_get_machine());
a7240d1e
MC
98 void *fdt;
99 int cpu;
100 uint32_t *cells;
101 char *nodename;
806c64b7 102 char ethclk_names[] = "pclk\0hclk";
5133ed17 103 uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
7b6bb66f 104 uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
a7240d1e 105
f2ce39b4
PB
106 if (ms->dtb) {
107 fdt = s->fdt = load_device_tree(ms->dtb, &s->fdt_size);
d5c90cf3
AP
108 if (!fdt) {
109 error_report("load_device_tree() failed");
110 exit(1);
111 }
112 goto update_bootargs;
113 } else {
114 fdt = s->fdt = create_device_tree(&s->fdt_size);
115 if (!fdt) {
116 error_report("create_device_tree() failed");
117 exit(1);
118 }
a7240d1e
MC
119 }
120
d372e748
BM
121 qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
122 qemu_fdt_setprop_string(fdt, "/", "compatible",
123 "sifive,hifive-unleashed-a00");
a7240d1e
MC
124 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
125 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
126
127 qemu_fdt_add_subnode(fdt, "/soc");
128 qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
2a1a6f6d 129 qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
a7240d1e
MC
130 qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
131 qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
132
e1724d09
BM
133 hfclk_phandle = phandle++;
134 nodename = g_strdup_printf("/hfclk");
135 qemu_fdt_add_subnode(fdt, nodename);
136 qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
137 qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
138 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
139 SIFIVE_U_HFCLK_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
144 rtcclk_phandle = phandle++;
145 nodename = g_strdup_printf("/rtcclk");
146 qemu_fdt_add_subnode(fdt, nodename);
147 qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
148 qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
149 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
150 SIFIVE_U_RTCCLK_FREQ);
151 qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
152 qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
153 g_free(nodename);
154
a7240d1e 155 nodename = g_strdup_printf("/memory@%lx",
13b8c354 156 (long)memmap[SIFIVE_U_DEV_DRAM].base);
a7240d1e
MC
157 qemu_fdt_add_subnode(fdt, nodename);
158 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354 159 memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
a7240d1e
MC
160 mem_size >> 32, mem_size);
161 qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
162 g_free(nodename);
163
164 qemu_fdt_add_subnode(fdt, "/cpus");
2a8756ed
MC
165 qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
166 SIFIVE_CLINT_TIMEBASE_FREQ);
a7240d1e
MC
167 qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
168 qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
169
ecdfe393 170 for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
382cb439 171 int cpu_phandle = phandle++;
a7240d1e
MC
172 nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
173 char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
ecdfe393 174 char *isa;
a7240d1e 175 qemu_fdt_add_subnode(fdt, nodename);
ecdfe393
BM
176 /* cpu 0 is the management hart that does not have mmu */
177 if (cpu != 0) {
2206ffa6
AF
178 if (is_32_bit) {
179 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
180 } else {
181 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
182 }
ecdfe393
BM
183 isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]);
184 } else {
185 isa = riscv_isa_string(&s->soc.e_cpus.harts[0]);
186 }
a7240d1e
MC
187 qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
188 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
189 qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
190 qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
191 qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
192 qemu_fdt_add_subnode(fdt, intc);
382cb439 193 qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
a7240d1e
MC
194 qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
195 qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
196 qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
197 g_free(isa);
198 g_free(intc);
199 g_free(nodename);
200 }
201
ecdfe393
BM
202 cells = g_new0(uint32_t, ms->smp.cpus * 4);
203 for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
a7240d1e
MC
204 nodename =
205 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
206 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
207 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
208 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
209 cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
210 cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
211 g_free(nodename);
212 }
213 nodename = g_strdup_printf("/soc/clint@%lx",
13b8c354 214 (long)memmap[SIFIVE_U_DEV_CLINT].base);
a7240d1e
MC
215 qemu_fdt_add_subnode(fdt, nodename);
216 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
217 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
218 0x0, memmap[SIFIVE_U_DEV_CLINT].base,
219 0x0, memmap[SIFIVE_U_DEV_CLINT].size);
a7240d1e 220 qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
ecdfe393 221 cells, ms->smp.cpus * sizeof(uint32_t) * 4);
a7240d1e
MC
222 g_free(cells);
223 g_free(nodename);
224
ea85f27d 225 nodename = g_strdup_printf("/soc/otp@%lx",
13b8c354 226 (long)memmap[SIFIVE_U_DEV_OTP].base);
ea85f27d
BM
227 qemu_fdt_add_subnode(fdt, nodename);
228 qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
229 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
230 0x0, memmap[SIFIVE_U_DEV_OTP].base,
231 0x0, memmap[SIFIVE_U_DEV_OTP].size);
ea85f27d
BM
232 qemu_fdt_setprop_string(fdt, nodename, "compatible",
233 "sifive,fu540-c000-otp");
234 g_free(nodename);
235
af14c840
BM
236 prci_phandle = phandle++;
237 nodename = g_strdup_printf("/soc/clock-controller@%lx",
13b8c354 238 (long)memmap[SIFIVE_U_DEV_PRCI].base);
af14c840
BM
239 qemu_fdt_add_subnode(fdt, nodename);
240 qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
241 qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
242 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
243 hfclk_phandle, rtcclk_phandle);
244 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
245 0x0, memmap[SIFIVE_U_DEV_PRCI].base,
246 0x0, memmap[SIFIVE_U_DEV_PRCI].size);
af14c840
BM
247 qemu_fdt_setprop_string(fdt, nodename, "compatible",
248 "sifive,fu540-c000-prci");
249 g_free(nodename);
250
382cb439 251 plic_phandle = phandle++;
ecdfe393
BM
252 cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2);
253 for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
a7240d1e
MC
254 nodename =
255 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
256 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
ecdfe393
BM
257 /* cpu 0 is the management hart that does not have S-mode */
258 if (cpu == 0) {
259 cells[0] = cpu_to_be32(intc_phandle);
260 cells[1] = cpu_to_be32(IRQ_M_EXT);
261 } else {
262 cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
263 cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
264 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
265 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
266 }
a7240d1e
MC
267 g_free(nodename);
268 }
269 nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
13b8c354 270 (long)memmap[SIFIVE_U_DEV_PLIC].base);
a7240d1e
MC
271 qemu_fdt_add_subnode(fdt, nodename);
272 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
273 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0");
274 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
275 qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
ecdfe393 276 cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t));
a7240d1e 277 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
278 0x0, memmap[SIFIVE_U_DEV_PLIC].base,
279 0x0, memmap[SIFIVE_U_DEV_PLIC].size);
98ceee7f 280 qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
04e7edd1 281 qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
a7240d1e
MC
282 plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
283 g_free(cells);
284 g_free(nodename);
285
5133ed17 286 gpio_phandle = phandle++;
8a88b9f5 287 nodename = g_strdup_printf("/soc/gpio@%lx",
13b8c354 288 (long)memmap[SIFIVE_U_DEV_GPIO].base);
8a88b9f5 289 qemu_fdt_add_subnode(fdt, nodename);
5133ed17 290 qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
8a88b9f5
BM
291 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
292 prci_phandle, PRCI_CLK_TLCLK);
293 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
294 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
295 qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
296 qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
297 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
298 0x0, memmap[SIFIVE_U_DEV_GPIO].base,
299 0x0, memmap[SIFIVE_U_DEV_GPIO].size);
8a88b9f5
BM
300 qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
301 SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
302 SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
303 SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
304 SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
305 SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
306 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
307 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
308 g_free(nodename);
309
5133ed17
BM
310 nodename = g_strdup_printf("/gpio-restart");
311 qemu_fdt_add_subnode(fdt, nodename);
312 qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
313 qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
314 g_free(nodename);
315
834e027a 316 nodename = g_strdup_printf("/soc/dma@%lx",
13b8c354 317 (long)memmap[SIFIVE_U_DEV_PDMA].base);
834e027a
BM
318 qemu_fdt_add_subnode(fdt, nodename);
319 qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
320 qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
321 SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
322 SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
323 SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
324 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
325 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
326 0x0, memmap[SIFIVE_U_DEV_PDMA].base,
327 0x0, memmap[SIFIVE_U_DEV_PDMA].size);
834e027a
BM
328 qemu_fdt_setprop_string(fdt, nodename, "compatible",
329 "sifive,fu540-c000-pdma");
330 g_free(nodename);
331
6eaf9cf5 332 nodename = g_strdup_printf("/soc/cache-controller@%lx",
13b8c354 333 (long)memmap[SIFIVE_U_DEV_L2CC].base);
6eaf9cf5
BM
334 qemu_fdt_add_subnode(fdt, nodename);
335 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
336 0x0, memmap[SIFIVE_U_DEV_L2CC].base,
337 0x0, memmap[SIFIVE_U_DEV_L2CC].size);
6eaf9cf5
BM
338 qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
339 SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
340 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
341 qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
342 qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
343 qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
344 qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
345 qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
346 qemu_fdt_setprop_string(fdt, nodename, "compatible",
347 "sifive,fu540-c000-ccache");
348 g_free(nodename);
349
722f1352
BM
350 nodename = g_strdup_printf("/soc/spi@%lx",
351 (long)memmap[SIFIVE_U_DEV_QSPI2].base);
352 qemu_fdt_add_subnode(fdt, nodename);
353 qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
354 qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
355 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
356 prci_phandle, PRCI_CLK_TLCLK);
357 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ);
358 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
359 qemu_fdt_setprop_cells(fdt, nodename, "reg",
360 0x0, memmap[SIFIVE_U_DEV_QSPI2].base,
361 0x0, memmap[SIFIVE_U_DEV_QSPI2].size);
362 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
363 g_free(nodename);
364
365 nodename = g_strdup_printf("/soc/spi@%lx/mmc@0",
366 (long)memmap[SIFIVE_U_DEV_QSPI2].base);
367 qemu_fdt_add_subnode(fdt, nodename);
368 qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0);
369 qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300);
370 qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000);
371 qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
372 qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot");
373 g_free(nodename);
374
145b2991
BM
375 nodename = g_strdup_printf("/soc/spi@%lx",
376 (long)memmap[SIFIVE_U_DEV_QSPI0].base);
377 qemu_fdt_add_subnode(fdt, nodename);
378 qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
379 qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
380 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
381 prci_phandle, PRCI_CLK_TLCLK);
382 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ);
383 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
384 qemu_fdt_setprop_cells(fdt, nodename, "reg",
385 0x0, memmap[SIFIVE_U_DEV_QSPI0].base,
386 0x0, memmap[SIFIVE_U_DEV_QSPI0].size);
387 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
388 g_free(nodename);
389
390 nodename = g_strdup_printf("/soc/spi@%lx/flash@0",
391 (long)memmap[SIFIVE_U_DEV_QSPI0].base);
392 qemu_fdt_add_subnode(fdt, nodename);
393 qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4);
394 qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4);
395 qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0);
396 qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000);
397 qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
398 qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor");
399 g_free(nodename);
400
7b6bb66f 401 phy_phandle = phandle++;
5a7f76a3 402 nodename = g_strdup_printf("/soc/ethernet@%lx",
13b8c354 403 (long)memmap[SIFIVE_U_DEV_GEM].base);
5a7f76a3 404 qemu_fdt_add_subnode(fdt, nodename);
7b6bb66f
BM
405 qemu_fdt_setprop_string(fdt, nodename, "compatible",
406 "sifive,fu540-c000-gem");
5a7f76a3 407 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
408 0x0, memmap[SIFIVE_U_DEV_GEM].base,
409 0x0, memmap[SIFIVE_U_DEV_GEM].size,
410 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
411 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
5a7f76a3
AF
412 qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
413 qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
7b6bb66f 414 qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
04e7edd1
BM
415 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
416 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
fe93582c 417 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
806c64b7 418 prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
04ece4f8 419 qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names,
fe93582c 420 sizeof(ethclk_names));
7b6bb66f
BM
421 qemu_fdt_setprop(fdt, nodename, "local-mac-address",
422 s->soc.gem.conf.macaddr.a, ETH_ALEN);
04e7edd1
BM
423 qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
424 qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
c3a28b5d
BM
425
426 qemu_fdt_add_subnode(fdt, "/aliases");
427 qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
428
5a7f76a3
AF
429 g_free(nodename);
430
431 nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
13b8c354 432 (long)memmap[SIFIVE_U_DEV_GEM].base);
5a7f76a3 433 qemu_fdt_add_subnode(fdt, nodename);
7b6bb66f 434 qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
04e7edd1 435 qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
5a7f76a3
AF
436 g_free(nodename);
437
10b43754
AP
438 nodename = g_strdup_printf("/soc/serial@%lx",
439 (long)memmap[SIFIVE_U_DEV_UART1].base);
440 qemu_fdt_add_subnode(fdt, nodename);
441 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
442 qemu_fdt_setprop_cells(fdt, nodename, "reg",
443 0x0, memmap[SIFIVE_U_DEV_UART1].base,
444 0x0, memmap[SIFIVE_U_DEV_UART1].size);
445 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
446 prci_phandle, PRCI_CLK_TLCLK);
447 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
448 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ);
449
450 qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename);
451 g_free(nodename);
452
5f7134d3 453 nodename = g_strdup_printf("/soc/serial@%lx",
13b8c354 454 (long)memmap[SIFIVE_U_DEV_UART0].base);
a7240d1e
MC
455 qemu_fdt_add_subnode(fdt, nodename);
456 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
457 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
458 0x0, memmap[SIFIVE_U_DEV_UART0].base,
459 0x0, memmap[SIFIVE_U_DEV_UART0].size);
806c64b7
BM
460 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
461 prci_phandle, PRCI_CLK_TLCLK);
04e7edd1
BM
462 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
463 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
a7240d1e
MC
464
465 qemu_fdt_add_subnode(fdt, "/chosen");
466 qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
44e6dcd3
GR
467 qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
468
a7240d1e 469 g_free(nodename);
d5c90cf3
AP
470
471update_bootargs:
472 if (cmdline) {
473 qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
474 }
a7240d1e
MC
475}
476
5133ed17
BM
477static void sifive_u_machine_reset(void *opaque, int n, int level)
478{
479 /* gpio pin active low triggers reset */
480 if (!level) {
481 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
482 }
483}
484
523e3464 485static void sifive_u_machine_init(MachineState *machine)
a7240d1e
MC
486{
487 const struct MemmapEntry *memmap = sifive_u_memmap;
687caef1 488 SiFiveUState *s = RISCV_U_MACHINE(machine);
5aec3247 489 MemoryRegion *system_memory = get_system_memory();
a7240d1e 490 MemoryRegion *main_mem = g_new(MemoryRegion, 1);
1b3a2308 491 MemoryRegion *flash0 = g_new(MemoryRegion, 1);
13b8c354 492 target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
38bc4e34 493 target_ulong firmware_end_addr, kernel_start_addr;
8590f536 494 uint32_t start_addr_hi32 = 0x00000000;
5aec3247 495 int i;
66b1205b 496 uint32_t fdt_load_addr;
dc144fe1 497 uint64_t kernel_entry;
145b2991 498 DriveInfo *dinfo;
722f1352
BM
499 DeviceState *flash_dev, *sd_dev;
500 qemu_irq flash_cs, sd_cs;
a7240d1e 501
2308092b 502 /* Initialize SoC */
9fc7fc4d 503 object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
5325cc34
MA
504 object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
505 &error_abort);
099be035
AF
506 object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type,
507 &error_abort);
ce189ab2 508 qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
a7240d1e
MC
509
510 /* register RAM */
511 memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
512 machine->ram_size, &error_fatal);
13b8c354 513 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
2308092b 514 main_mem);
a7240d1e 515
1b3a2308
AF
516 /* register QSPI0 Flash */
517 memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
13b8c354
EH
518 memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
519 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
1b3a2308
AF
520 flash0);
521
5133ed17
BM
522 /* register gpio-restart */
523 qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
524 qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
525
a7240d1e 526 /* create device tree */
2206ffa6 527 create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline,
a8259b53 528 riscv_is_32bit(&s->soc.u_cpus));
a7240d1e 529
17aad9f2
BM
530 if (s->start_in_flash) {
531 /*
532 * If start_in_flash property is given, assign s->msel to a value
533 * that representing booting from QSPI0 memory-mapped flash.
534 *
535 * This also means that when both start_in_flash and msel properties
536 * are given, start_in_flash takes the precedence over msel.
537 *
538 * Note this is to keep backward compatibility not to break existing
539 * users that use start_in_flash property.
540 */
541 s->msel = MSEL_MEMMAP_QSPI0_FLASH;
542 }
543
544 switch (s->msel) {
545 case MSEL_MEMMAP_QSPI0_FLASH:
13b8c354 546 start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
17aad9f2
BM
547 break;
548 case MSEL_L2LIM_QSPI0_FLASH:
549 case MSEL_L2LIM_QSPI2_SD:
13b8c354 550 start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
17aad9f2
BM
551 break;
552 default:
13b8c354 553 start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
17aad9f2
BM
554 break;
555 }
556
a8259b53 557 if (riscv_is_32bit(&s->soc.u_cpus)) {
2206ffa6
AF
558 firmware_end_addr = riscv_find_and_load_firmware(machine,
559 "opensbi-riscv32-generic-fw_dynamic.bin",
560 start_addr, NULL);
561 } else {
562 firmware_end_addr = riscv_find_and_load_firmware(machine,
563 "opensbi-riscv64-generic-fw_dynamic.bin",
564 start_addr, NULL);
565 }
b3042223 566
a7240d1e 567 if (machine->kernel_filename) {
a8259b53 568 kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
38bc4e34
AF
569 firmware_end_addr);
570
571 kernel_entry = riscv_load_kernel(machine->kernel_filename,
572 kernel_start_addr, NULL);
0f8d4462
GR
573
574 if (machine->initrd_filename) {
575 hwaddr start;
576 hwaddr end = riscv_load_initrd(machine->initrd_filename,
577 machine->ram_size, kernel_entry,
578 &start);
9f79638e 579 qemu_fdt_setprop_cell(s->fdt, "/chosen",
0f8d4462 580 "linux,initrd-start", start);
9f79638e 581 qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
0f8d4462
GR
582 end);
583 }
dc144fe1
AP
584 } else {
585 /*
586 * If dynamic firmware is used, it doesn't know where is the next mode
587 * if kernel argument is not set.
588 */
589 kernel_entry = 0;
a7240d1e
MC
590 }
591
66b1205b 592 /* Compute the fdt load address in dram */
13b8c354 593 fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base,
66b1205b 594 machine->ram_size, s->fdt);
a8259b53 595 if (!riscv_is_32bit(&s->soc.u_cpus)) {
2206ffa6
AF
596 start_addr_hi32 = (uint64_t)start_addr >> 32;
597 }
66b1205b 598
a7240d1e 599 /* reset vector */
66b1205b 600 uint32_t reset_vec[11] = {
17aad9f2 601 s->msel, /* MSEL pin state */
dc144fe1
AP
602 0x00000297, /* 1: auipc t0, %pcrel_hi(fw_dyn) */
603 0x02828613, /* addi a2, t0, %pcrel_lo(1b) */
a7240d1e 604 0xf1402573, /* csrr a0, mhartid */
2206ffa6
AF
605 0,
606 0,
a7240d1e 607 0x00028067, /* jr t0 */
fc41ae23 608 start_addr, /* start: .dword */
8590f536 609 start_addr_hi32,
66b1205b
AP
610 fdt_load_addr, /* fdt_laddr: .dword */
611 0x00000000,
dc144fe1 612 /* fw_dyn: */
a7240d1e 613 };
a8259b53 614 if (riscv_is_32bit(&s->soc.u_cpus)) {
2206ffa6
AF
615 reset_vec[4] = 0x0202a583; /* lw a1, 32(t0) */
616 reset_vec[5] = 0x0182a283; /* lw t0, 24(t0) */
617 } else {
618 reset_vec[4] = 0x0202b583; /* ld a1, 32(t0) */
619 reset_vec[5] = 0x0182b283; /* ld t0, 24(t0) */
620 }
621
a7240d1e 622
5aec3247 623 /* copy in the reset vector in little_endian byte order */
66b1205b 624 for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
5aec3247
MC
625 reset_vec[i] = cpu_to_le32(reset_vec[i]);
626 }
627 rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
13b8c354 628 memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
dc144fe1 629
78936771 630 riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base,
13b8c354 631 memmap[SIFIVE_U_DEV_MROM].size,
dc144fe1 632 sizeof(reset_vec), kernel_entry);
145b2991
BM
633
634 /* Connect an SPI flash to SPI0 */
635 flash_dev = qdev_new("is25wp256");
636 dinfo = drive_get_next(IF_MTD);
637 if (dinfo) {
638 qdev_prop_set_drive_err(flash_dev, "drive",
639 blk_by_legacy_dinfo(dinfo),
640 &error_fatal);
641 }
642 qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal);
643
644 flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0);
645 sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
722f1352
BM
646
647 /* Connect an SD card to SPI2 */
648 sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd");
649
650 sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
651 sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
2308092b
AF
652}
653
523e3464
AF
654static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
655{
656 SiFiveUState *s = RISCV_U_MACHINE(obj);
657
658 return s->start_in_flash;
659}
660
661static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
662{
663 SiFiveUState *s = RISCV_U_MACHINE(obj);
664
665 s->start_in_flash = value;
666}
667
3e9667cd
BM
668static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v,
669 const char *name, void *opaque,
670 Error **errp)
3ca109c3
BM
671{
672 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
673}
674
3e9667cd
BM
675static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v,
676 const char *name, void *opaque,
677 Error **errp)
3ca109c3
BM
678{
679 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
680}
681
523e3464
AF
682static void sifive_u_machine_instance_init(Object *obj)
683{
684 SiFiveUState *s = RISCV_U_MACHINE(obj);
685
686 s->start_in_flash = false;
cfa32630
BM
687 s->msel = 0;
688 object_property_add(obj, "msel", "uint32",
689 sifive_u_machine_get_uint32_prop,
690 sifive_u_machine_set_uint32_prop, NULL, &s->msel);
691 object_property_set_description(obj, "msel",
692 "Mode Select (MSEL[3:0]) pin state");
693
3ca109c3 694 s->serial = OTP_SERIAL;
d2623129 695 object_property_add(obj, "serial", "uint32",
3e9667cd
BM
696 sifive_u_machine_get_uint32_prop,
697 sifive_u_machine_set_uint32_prop, NULL, &s->serial);
7eecec7d 698 object_property_set_description(obj, "serial", "Board serial number");
523e3464
AF
699}
700
701static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
702{
703 MachineClass *mc = MACHINE_CLASS(oc);
704
705 mc->desc = "RISC-V Board compatible with SiFive U SDK";
706 mc->init = sifive_u_machine_init;
707 mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
708 mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
1eaada8a 709 mc->default_cpu_type = SIFIVE_U_CPU;
523e3464 710 mc->default_cpus = mc->min_cpus;
418b473e
EH
711
712 object_class_property_add_bool(oc, "start-in-flash",
713 sifive_u_machine_get_start_in_flash,
714 sifive_u_machine_set_start_in_flash);
715 object_class_property_set_description(oc, "start-in-flash",
716 "Set on to tell QEMU's ROM to jump to "
717 "flash. Otherwise QEMU will jump to DRAM "
718 "or L2LIM depending on the msel value");
523e3464
AF
719}
720
721static const TypeInfo sifive_u_machine_typeinfo = {
722 .name = MACHINE_TYPE_NAME("sifive_u"),
723 .parent = TYPE_MACHINE,
724 .class_init = sifive_u_machine_class_init,
725 .instance_init = sifive_u_machine_instance_init,
726 .instance_size = sizeof(SiFiveUState),
727};
728
729static void sifive_u_machine_init_register_types(void)
730{
731 type_register_static(&sifive_u_machine_typeinfo);
732}
733
734type_init(sifive_u_machine_init_register_types)
735
139177b1 736static void sifive_u_soc_instance_init(Object *obj)
2308092b
AF
737{
738 SiFiveUSoCState *s = RISCV_U_SOC(obj);
739
9fc7fc4d 740 object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
ecdfe393
BM
741 qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
742
db873cc5
MA
743 object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
744 TYPE_RISCV_HART_ARRAY);
ecdfe393
BM
745 qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
746 qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
747 qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
73f6ed97 748 qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
ecdfe393 749
9fc7fc4d 750 object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
ecdfe393
BM
751 qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
752
db873cc5
MA
753 object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
754 TYPE_RISCV_HART_ARRAY);
5a7f76a3 755
db873cc5
MA
756 object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
757 object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
758 object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
8a88b9f5 759 object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
834e027a 760 object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
145b2991 761 object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI);
722f1352 762 object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI);
2308092b
AF
763}
764
139177b1 765static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
2308092b 766{
c4473127 767 MachineState *ms = MACHINE(qdev_get_machine());
2308092b
AF
768 SiFiveUSoCState *s = RISCV_U_SOC(dev);
769 const struct MemmapEntry *memmap = sifive_u_memmap;
770 MemoryRegion *system_memory = get_system_memory();
771 MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
a6902ef0 772 MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
05446f41
BM
773 char *plic_hart_config;
774 size_t plic_hart_config_len;
5a7f76a3 775 int i;
5a7f76a3 776 NICInfo *nd = &nd_table[0];
2308092b 777
099be035
AF
778 qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
779 qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
780 qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type);
781 qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
782
db873cc5
MA
783 sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort);
784 sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort);
ecdfe393
BM
785 /*
786 * The cluster must be realized after the RISC-V hart array container,
787 * as the container's CPU object is only created on realize, and the
788 * CPU must exist and have been parented into the cluster before the
789 * cluster is realized.
790 */
ce189ab2
MA
791 qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
792 qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
2308092b
AF
793
794 /* boot rom */
414c47d2 795 memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
13b8c354
EH
796 memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
797 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
2308092b 798 mask_rom);
a7240d1e 799
a6902ef0
AF
800 /*
801 * Add L2-LIM at reset size.
802 * This should be reduced in size as the L2 Cache Controller WayEnable
803 * register is incremented. Unfortunately I don't see a nice (or any) way
804 * to handle reducing or blocking out the L2 LIM while still allowing it
805 * be re returned to all enabled after a reset. For the time being, just
806 * leave it enabled all the time. This won't break anything, but will be
807 * too generous to misbehaving guests.
808 */
809 memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
13b8c354
EH
810 memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
811 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
a6902ef0
AF
812 l2lim_mem);
813
05446f41 814 /* create PLIC hart topology configuration string */
c4473127
LX
815 plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) *
816 ms->smp.cpus;
05446f41 817 plic_hart_config = g_malloc0(plic_hart_config_len);
c4473127 818 for (i = 0; i < ms->smp.cpus; i++) {
05446f41 819 if (i != 0) {
ef965ce2
BM
820 strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG,
821 plic_hart_config_len);
822 } else {
823 strncat(plic_hart_config, "M", plic_hart_config_len);
05446f41 824 }
05446f41
BM
825 plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1);
826 }
827
a7240d1e 828 /* MMIO */
13b8c354 829 s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
c9270e10 830 plic_hart_config, 0,
a7240d1e
MC
831 SIFIVE_U_PLIC_NUM_SOURCES,
832 SIFIVE_U_PLIC_NUM_PRIORITIES,
833 SIFIVE_U_PLIC_PRIORITY_BASE,
834 SIFIVE_U_PLIC_PENDING_BASE,
835 SIFIVE_U_PLIC_ENABLE_BASE,
836 SIFIVE_U_PLIC_ENABLE_STRIDE,
837 SIFIVE_U_PLIC_CONTEXT_BASE,
838 SIFIVE_U_PLIC_CONTEXT_STRIDE,
13b8c354 839 memmap[SIFIVE_U_DEV_PLIC].size);
bb8136df 840 g_free(plic_hart_config);
13b8c354 841 sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
647a70a1 842 serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
13b8c354 843 sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
194eef09 844 serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
13b8c354
EH
845 sifive_clint_create(memmap[SIFIVE_U_DEV_CLINT].base,
846 memmap[SIFIVE_U_DEV_CLINT].size, 0, ms->smp.cpus,
a47ef6e9
BM
847 SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
848 SIFIVE_CLINT_TIMEBASE_FREQ, false);
5a7f76a3 849
cbe3a8c5
MA
850 if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
851 return;
852 }
13b8c354 853 sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
af14c840 854
8a88b9f5 855 qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
cbe3a8c5
MA
856 if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
857 return;
858 }
13b8c354 859 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
8a88b9f5
BM
860
861 /* Pass all GPIOs to the SOC layer so they are available to the board */
862 qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
863
864 /* Connect GPIO interrupts to the PLIC */
865 for (i = 0; i < 16; i++) {
866 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
867 qdev_get_gpio_in(DEVICE(s->plic),
868 SIFIVE_U_GPIO_IRQ0 + i));
834e027a
BM
869 }
870
871 /* PDMA */
872 sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
13b8c354 873 sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
834e027a
BM
874
875 /* Connect PDMA interrupts to the PLIC */
876 for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
877 sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
878 qdev_get_gpio_in(DEVICE(s->plic),
879 SIFIVE_U_PDMA_IRQ0 + i));
8a88b9f5
BM
880 }
881
fda5b000 882 qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
cbe3a8c5
MA
883 if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
884 return;
885 }
13b8c354 886 sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
5461c4fe 887
7ad36e2e 888 /* FIXME use qdev NIC properties instead of nd_table[] */
5a7f76a3
AF
889 if (nd->used) {
890 qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
891 qdev_set_nic_properties(DEVICE(&s->gem), nd);
892 }
5325cc34 893 object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
5a7f76a3 894 &error_abort);
668f62ec 895 if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
5a7f76a3
AF
896 return;
897 }
13b8c354 898 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
5a7f76a3 899 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
5874f0a7 900 qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
7b6bb66f
BM
901
902 create_unimplemented_device("riscv.sifive.u.gem-mgmt",
13b8c354 903 memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
3eaea6eb
BM
904
905 create_unimplemented_device("riscv.sifive.u.dmc",
13b8c354 906 memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
6eaf9cf5
BM
907
908 create_unimplemented_device("riscv.sifive.u.l2cc",
13b8c354 909 memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
145b2991
BM
910
911 sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp);
912 sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0,
913 memmap[SIFIVE_U_DEV_QSPI0].base);
914 sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0,
915 qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ));
722f1352
BM
916 sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp);
917 sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0,
918 memmap[SIFIVE_U_DEV_QSPI2].base);
919 sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0,
920 qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ));
a7240d1e
MC
921}
922
139177b1 923static Property sifive_u_soc_props[] = {
fda5b000 924 DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
099be035 925 DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type),
fda5b000
AF
926 DEFINE_PROP_END_OF_LIST()
927};
928
139177b1 929static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
2308092b
AF
930{
931 DeviceClass *dc = DEVICE_CLASS(oc);
932
139177b1
BM
933 device_class_set_props(dc, sifive_u_soc_props);
934 dc->realize = sifive_u_soc_realize;
2308092b
AF
935 /* Reason: Uses serial_hds in realize function, thus can't be used twice */
936 dc->user_creatable = false;
937}
938
139177b1 939static const TypeInfo sifive_u_soc_type_info = {
2308092b
AF
940 .name = TYPE_RISCV_U_SOC,
941 .parent = TYPE_DEVICE,
942 .instance_size = sizeof(SiFiveUSoCState),
139177b1
BM
943 .instance_init = sifive_u_soc_instance_init,
944 .class_init = sifive_u_soc_class_init,
2308092b
AF
945};
946
139177b1 947static void sifive_u_soc_register_types(void)
2308092b 948{
139177b1 949 type_register_static(&sifive_u_soc_type_info);
2308092b
AF
950}
951
139177b1 952type_init(sifive_u_soc_register_types)