xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision 382cb4392ff8c3fa911d10da959eb8b0cbee15af)
1a7240d1eSMichael Clark /*
2a7240d1eSMichael Clark  * QEMU RISC-V Board Compatible with SiFive Freedom U SDK
3a7240d1eSMichael Clark  *
4a7240d1eSMichael Clark  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5a7240d1eSMichael Clark  * Copyright (c) 2017 SiFive, Inc.
6a7240d1eSMichael Clark  *
7a7240d1eSMichael Clark  * Provides a board compatible with the SiFive Freedom U SDK:
8a7240d1eSMichael Clark  *
9a7240d1eSMichael Clark  * 0) UART
10a7240d1eSMichael Clark  * 1) CLINT (Core Level Interruptor)
11a7240d1eSMichael Clark  * 2) PLIC (Platform Level Interrupt Controller)
12a7240d1eSMichael Clark  *
13a7240d1eSMichael Clark  * This board currently uses a hardcoded devicetree that indicates one hart.
14a7240d1eSMichael Clark  *
15a7240d1eSMichael Clark  * This program is free software; you can redistribute it and/or modify it
16a7240d1eSMichael Clark  * under the terms and conditions of the GNU General Public License,
17a7240d1eSMichael Clark  * version 2 or later, as published by the Free Software Foundation.
18a7240d1eSMichael Clark  *
19a7240d1eSMichael Clark  * This program is distributed in the hope it will be useful, but WITHOUT
20a7240d1eSMichael Clark  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21a7240d1eSMichael Clark  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
22a7240d1eSMichael Clark  * more details.
23a7240d1eSMichael Clark  *
24a7240d1eSMichael Clark  * You should have received a copy of the GNU General Public License along with
25a7240d1eSMichael Clark  * this program.  If not, see <http://www.gnu.org/licenses/>.
26a7240d1eSMichael Clark  */
27a7240d1eSMichael Clark 
28a7240d1eSMichael Clark #include "qemu/osdep.h"
29a7240d1eSMichael Clark #include "qemu/log.h"
30a7240d1eSMichael Clark #include "qemu/error-report.h"
31a7240d1eSMichael Clark #include "qapi/error.h"
32a7240d1eSMichael Clark #include "hw/hw.h"
33a7240d1eSMichael Clark #include "hw/boards.h"
34a7240d1eSMichael Clark #include "hw/loader.h"
35a7240d1eSMichael Clark #include "hw/sysbus.h"
36a7240d1eSMichael Clark #include "hw/char/serial.h"
37a7240d1eSMichael Clark #include "target/riscv/cpu.h"
38a7240d1eSMichael Clark #include "hw/riscv/riscv_hart.h"
39a7240d1eSMichael Clark #include "hw/riscv/sifive_plic.h"
40a7240d1eSMichael Clark #include "hw/riscv/sifive_clint.h"
41a7240d1eSMichael Clark #include "hw/riscv/sifive_uart.h"
42a7240d1eSMichael Clark #include "hw/riscv/sifive_prci.h"
43a7240d1eSMichael Clark #include "hw/riscv/sifive_u.h"
44a7240d1eSMichael Clark #include "chardev/char.h"
45a7240d1eSMichael Clark #include "sysemu/arch_init.h"
46a7240d1eSMichael Clark #include "sysemu/device_tree.h"
47a7240d1eSMichael Clark #include "exec/address-spaces.h"
48a7240d1eSMichael Clark #include "elf.h"
49a7240d1eSMichael Clark 
505aec3247SMichael Clark #include <libfdt.h>
515aec3247SMichael Clark 
52a7240d1eSMichael Clark static const struct MemmapEntry {
53a7240d1eSMichael Clark     hwaddr base;
54a7240d1eSMichael Clark     hwaddr size;
55a7240d1eSMichael Clark } sifive_u_memmap[] = {
56a7240d1eSMichael Clark     [SIFIVE_U_DEBUG] =    {        0x0,      0x100 },
575aec3247SMichael Clark     [SIFIVE_U_MROM] =     {     0x1000,    0x11000 },
58a7240d1eSMichael Clark     [SIFIVE_U_CLINT] =    {  0x2000000,    0x10000 },
59a7240d1eSMichael Clark     [SIFIVE_U_PLIC] =     {  0xc000000,  0x4000000 },
60a7240d1eSMichael Clark     [SIFIVE_U_UART0] =    { 0x10013000,     0x1000 },
61a7240d1eSMichael Clark     [SIFIVE_U_UART1] =    { 0x10023000,     0x1000 },
62a7240d1eSMichael Clark     [SIFIVE_U_DRAM] =     { 0x80000000,        0x0 },
635a7f76a3SAlistair Francis     [SIFIVE_U_GEM] =      { 0x100900FC,     0x2000 },
64a7240d1eSMichael Clark };
65a7240d1eSMichael Clark 
665a7f76a3SAlistair Francis #define GEM_REVISION        0x10070109
675a7f76a3SAlistair Francis 
6840e46e51SAlistair Francis static target_ulong load_kernel(const char *kernel_filename)
69a7240d1eSMichael Clark {
70a7240d1eSMichael Clark     uint64_t kernel_entry, kernel_high;
71a7240d1eSMichael Clark 
724366e1dbSLiam Merwick     if (load_elf(kernel_filename, NULL, NULL, NULL,
73a7240d1eSMichael Clark                  &kernel_entry, NULL, &kernel_high,
7489854803SMichael Clark                  0, EM_RISCV, 1, 0) < 0) {
75371b74e2SMao Zhongyi         error_report("could not load kernel '%s'", kernel_filename);
76a7240d1eSMichael Clark         exit(1);
77a7240d1eSMichael Clark     }
78a7240d1eSMichael Clark     return kernel_entry;
79a7240d1eSMichael Clark }
80a7240d1eSMichael Clark 
81a7240d1eSMichael Clark static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,
82a7240d1eSMichael Clark     uint64_t mem_size, const char *cmdline)
83a7240d1eSMichael Clark {
84a7240d1eSMichael Clark     void *fdt;
85a7240d1eSMichael Clark     int cpu;
86a7240d1eSMichael Clark     uint32_t *cells;
87a7240d1eSMichael Clark     char *nodename;
88fe93582cSAnup Patel     char ethclk_names[] = "pclk\0hclk\0tx_clk";
89*382cb439SBin Meng     uint32_t plic_phandle, ethclk_phandle, phandle = 1;
90a7240d1eSMichael Clark 
91a7240d1eSMichael Clark     fdt = s->fdt = create_device_tree(&s->fdt_size);
92a7240d1eSMichael Clark     if (!fdt) {
93a7240d1eSMichael Clark         error_report("create_device_tree() failed");
94a7240d1eSMichael Clark         exit(1);
95a7240d1eSMichael Clark     }
96a7240d1eSMichael Clark 
97a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
98a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
99a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
100a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
101a7240d1eSMichael Clark 
102a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/soc");
103a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
1042a1a6f6dSAlistair Francis     qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
105a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
106a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
107a7240d1eSMichael Clark 
108a7240d1eSMichael Clark     nodename = g_strdup_printf("/memory@%lx",
109a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_DRAM].base);
110a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
111a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
112a7240d1eSMichael Clark         memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base,
113a7240d1eSMichael Clark         mem_size >> 32, mem_size);
114a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
115a7240d1eSMichael Clark     g_free(nodename);
116a7240d1eSMichael Clark 
117a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/cpus");
1182a8756edSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
1192a8756edSMichael Clark         SIFIVE_CLINT_TIMEBASE_FREQ);
120a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
121a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
122a7240d1eSMichael Clark 
1232308092bSAlistair Francis     for (cpu = s->soc.cpus.num_harts - 1; cpu >= 0; cpu--) {
124*382cb439SBin Meng         int cpu_phandle = phandle++;
125a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
126a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
1272308092bSAlistair Francis         char *isa = riscv_isa_string(&s->soc.cpus.harts[cpu]);
128a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, nodename);
1292a8756edSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
1302a8756edSMichael Clark                               SIFIVE_U_CLOCK_FREQ);
131a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
132a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
133a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
134a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
135a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
136a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
137a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
138*382cb439SBin Meng         qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
139*382cb439SBin Meng         qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", cpu_phandle);
140a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
141a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
142a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
143a7240d1eSMichael Clark         g_free(isa);
144a7240d1eSMichael Clark         g_free(intc);
145a7240d1eSMichael Clark         g_free(nodename);
146a7240d1eSMichael Clark     }
147a7240d1eSMichael Clark 
1482308092bSAlistair Francis     cells =  g_new0(uint32_t, s->soc.cpus.num_harts * 4);
1492308092bSAlistair Francis     for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) {
150a7240d1eSMichael Clark         nodename =
151a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
152a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
153a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
154a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
155a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
156a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
157a7240d1eSMichael Clark         g_free(nodename);
158a7240d1eSMichael Clark     }
159a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
160a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_CLINT].base);
161a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
162a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
163a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
164a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_CLINT].base,
165a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_CLINT].size);
166a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
1672308092bSAlistair Francis         cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4);
168a7240d1eSMichael Clark     g_free(cells);
169a7240d1eSMichael Clark     g_free(nodename);
170a7240d1eSMichael Clark 
171*382cb439SBin Meng     plic_phandle = phandle++;
1722308092bSAlistair Francis     cells =  g_new0(uint32_t, s->soc.cpus.num_harts * 4);
1732308092bSAlistair Francis     for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) {
174a7240d1eSMichael Clark         nodename =
175a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
176a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
177a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
178a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT);
179a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
180a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT);
181a7240d1eSMichael Clark         g_free(nodename);
182a7240d1eSMichael Clark     }
183a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
184a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_PLIC].base);
185a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
186a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
187a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0");
188a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
189a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
1902308092bSAlistair Francis         cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4);
191a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
192a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_PLIC].base,
193a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_PLIC].size);
194a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
195a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7);
19698ceee7fSAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
197*382cb439SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "phandle", plic_phandle);
198*382cb439SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", plic_phandle);
199a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
200a7240d1eSMichael Clark     g_free(cells);
201a7240d1eSMichael Clark     g_free(nodename);
202a7240d1eSMichael Clark 
203*382cb439SBin Meng     ethclk_phandle = phandle++;
204fe93582cSAnup Patel     nodename = g_strdup_printf("/soc/ethclk");
205fe93582cSAnup Patel     qemu_fdt_add_subnode(fdt, nodename);
206fe93582cSAnup Patel     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
207fe93582cSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
208fe93582cSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
209fe93582cSAnup Patel         SIFIVE_U_GEM_CLOCK_FREQ);
210*382cb439SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", ethclk_phandle);
211*382cb439SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "linux,phandle", ethclk_phandle);
212fe93582cSAnup Patel     ethclk_phandle = qemu_fdt_get_phandle(fdt, nodename);
213fe93582cSAnup Patel     g_free(nodename);
214fe93582cSAnup Patel 
2155a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx",
2165a7f76a3SAlistair Francis         (long)memmap[SIFIVE_U_GEM].base);
2175a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
2185a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "compatible", "cdns,macb");
2195a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
2205a7f76a3SAlistair Francis         0x0, memmap[SIFIVE_U_GEM].base,
2215a7f76a3SAlistair Francis         0x0, memmap[SIFIVE_U_GEM].size);
2225a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
2235a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
2245a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle);
2255a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
226fe93582cSAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
227fe93582cSAnup Patel         ethclk_phandle, ethclk_phandle, ethclk_phandle);
228fe93582cSAnup Patel     qemu_fdt_setprop(fdt, nodename, "clocks-names", ethclk_names,
229fe93582cSAnup Patel         sizeof(ethclk_names));
2305a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "#address-cells", 1);
2315a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "#size-cells", 0);
2325a7f76a3SAlistair Francis     g_free(nodename);
2335a7f76a3SAlistair Francis 
2345a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
2355a7f76a3SAlistair Francis         (long)memmap[SIFIVE_U_GEM].base);
2365a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
2375a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg", 0x0);
2385a7f76a3SAlistair Francis     g_free(nodename);
2395a7f76a3SAlistair Francis 
240bde3ab9aSAlistair Francis     nodename = g_strdup_printf("/soc/uart@%lx",
241a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_UART0].base);
242a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
243a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
244a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
245a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_UART0].base,
246a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_UART0].size);
2476c60757eSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
2486c60757eSAnup Patel                           SIFIVE_U_CLOCK_FREQ / 2);
249a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle);
250a9ec1c76SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
251a7240d1eSMichael Clark 
252a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
253a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
2547c28f4daSMichael Clark     if (cmdline) {
255a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
2567c28f4daSMichael Clark     }
257a7240d1eSMichael Clark     g_free(nodename);
258a7240d1eSMichael Clark }
259a7240d1eSMichael Clark 
260a7240d1eSMichael Clark static void riscv_sifive_u_init(MachineState *machine)
261a7240d1eSMichael Clark {
262a7240d1eSMichael Clark     const struct MemmapEntry *memmap = sifive_u_memmap;
263a7240d1eSMichael Clark 
264a7240d1eSMichael Clark     SiFiveUState *s = g_new0(SiFiveUState, 1);
2655aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
266a7240d1eSMichael Clark     MemoryRegion *main_mem = g_new(MemoryRegion, 1);
2675aec3247SMichael Clark     int i;
268a7240d1eSMichael Clark 
2692308092bSAlistair Francis     /* Initialize SoC */
2704eea9d7dSAlistair Francis     object_initialize_child(OBJECT(machine), "soc", &s->soc,
2714eea9d7dSAlistair Francis                             sizeof(s->soc), TYPE_RISCV_U_SOC,
2724eea9d7dSAlistair Francis                             &error_abort, NULL);
273a7240d1eSMichael Clark     object_property_set_bool(OBJECT(&s->soc), true, "realized",
274a7240d1eSMichael Clark                             &error_abort);
275a7240d1eSMichael Clark 
276a7240d1eSMichael Clark     /* register RAM */
277a7240d1eSMichael Clark     memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
278a7240d1eSMichael Clark                            machine->ram_size, &error_fatal);
2795aec3247SMichael Clark     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base,
280a7240d1eSMichael Clark                                 main_mem);
281a7240d1eSMichael Clark 
282a7240d1eSMichael Clark     /* create device tree */
283a7240d1eSMichael Clark     create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
284a7240d1eSMichael Clark 
285a7240d1eSMichael Clark     if (machine->kernel_filename) {
286a7240d1eSMichael Clark         load_kernel(machine->kernel_filename);
287a7240d1eSMichael Clark     }
288a7240d1eSMichael Clark 
289a7240d1eSMichael Clark     /* reset vector */
290a7240d1eSMichael Clark     uint32_t reset_vec[8] = {
291a7240d1eSMichael Clark         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(dtb) */
292a7240d1eSMichael Clark         0x02028593,                    /*     addi   a1, t0, %pcrel_lo(1b) */
293a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
294a7240d1eSMichael Clark #if defined(TARGET_RISCV32)
295a7240d1eSMichael Clark         0x0182a283,                    /*     lw     t0, 24(t0) */
296a7240d1eSMichael Clark #elif defined(TARGET_RISCV64)
297a7240d1eSMichael Clark         0x0182b283,                    /*     ld     t0, 24(t0) */
298a7240d1eSMichael Clark #endif
299a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
300a7240d1eSMichael Clark         0x00000000,
301a7240d1eSMichael Clark         memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */
302a7240d1eSMichael Clark         0x00000000,
303a7240d1eSMichael Clark                                        /* dtb: */
304a7240d1eSMichael Clark     };
305a7240d1eSMichael Clark 
3065aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
3075aec3247SMichael Clark     for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
3085aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
3095aec3247SMichael Clark     }
3105aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
3115aec3247SMichael Clark                           memmap[SIFIVE_U_MROM].base, &address_space_memory);
312a7240d1eSMichael Clark 
313a7240d1eSMichael Clark     /* copy in the device tree */
3145aec3247SMichael Clark     if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) >
3155aec3247SMichael Clark             memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) {
3165aec3247SMichael Clark         error_report("not enough space to store device-tree");
3175aec3247SMichael Clark         exit(1);
3185aec3247SMichael Clark     }
3195aec3247SMichael Clark     qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt));
3205aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt),
3215aec3247SMichael Clark                           memmap[SIFIVE_U_MROM].base + sizeof(reset_vec),
3225aec3247SMichael Clark                           &address_space_memory);
3232308092bSAlistair Francis }
3242308092bSAlistair Francis 
3252308092bSAlistair Francis static void riscv_sifive_u_soc_init(Object *obj)
3262308092bSAlistair Francis {
3272308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
3282308092bSAlistair Francis 
3294eea9d7dSAlistair Francis     object_initialize_child(obj, "cpus", &s->cpus, sizeof(s->cpus),
3304eea9d7dSAlistair Francis                             TYPE_RISCV_HART_ARRAY, &error_abort, NULL);
3312308092bSAlistair Francis     object_property_set_str(OBJECT(&s->cpus), SIFIVE_U_CPU, "cpu-type",
3322308092bSAlistair Francis                             &error_abort);
3332308092bSAlistair Francis     object_property_set_int(OBJECT(&s->cpus), smp_cpus, "num-harts",
3342308092bSAlistair Francis                             &error_abort);
3355a7f76a3SAlistair Francis 
3364eea9d7dSAlistair Francis     sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem),
3374eea9d7dSAlistair Francis                           TYPE_CADENCE_GEM);
3382308092bSAlistair Francis }
3392308092bSAlistair Francis 
3402308092bSAlistair Francis static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp)
3412308092bSAlistair Francis {
3422308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
3432308092bSAlistair Francis     const struct MemmapEntry *memmap = sifive_u_memmap;
3442308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
3452308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
3465a7f76a3SAlistair Francis     qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES];
3475a7f76a3SAlistair Francis     int i;
3485a7f76a3SAlistair Francis     Error *err = NULL;
3495a7f76a3SAlistair Francis     NICInfo *nd = &nd_table[0];
3502308092bSAlistair Francis 
3512308092bSAlistair Francis     object_property_set_bool(OBJECT(&s->cpus), true, "realized",
3522308092bSAlistair Francis                              &error_abort);
3532308092bSAlistair Francis 
3542308092bSAlistair Francis     /* boot rom */
3552308092bSAlistair Francis     memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom",
3562308092bSAlistair Francis                            memmap[SIFIVE_U_MROM].size, &error_fatal);
3572308092bSAlistair Francis     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base,
3582308092bSAlistair Francis                                 mask_rom);
359a7240d1eSMichael Clark 
360a7240d1eSMichael Clark     /* MMIO */
361a7240d1eSMichael Clark     s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base,
362a7240d1eSMichael Clark         (char *)SIFIVE_U_PLIC_HART_CONFIG,
363a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
364a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
365a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
366a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
367a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
368a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
369a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
370a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
371a7240d1eSMichael Clark         memmap[SIFIVE_U_PLIC].size);
3725aec3247SMichael Clark     sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base,
373647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
374194eef09SMichael Clark     sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base,
375194eef09SMichael Clark         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
376a7240d1eSMichael Clark     sifive_clint_create(memmap[SIFIVE_U_CLINT].base,
377a7240d1eSMichael Clark         memmap[SIFIVE_U_CLINT].size, smp_cpus,
378a7240d1eSMichael Clark         SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
3795a7f76a3SAlistair Francis 
3805a7f76a3SAlistair Francis     for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) {
3815a7f76a3SAlistair Francis         plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i);
3825a7f76a3SAlistair Francis     }
3835a7f76a3SAlistair Francis 
3845a7f76a3SAlistair Francis     if (nd->used) {
3855a7f76a3SAlistair Francis         qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
3865a7f76a3SAlistair Francis         qdev_set_nic_properties(DEVICE(&s->gem), nd);
3875a7f76a3SAlistair Francis     }
3885a7f76a3SAlistair Francis     object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision",
3895a7f76a3SAlistair Francis                             &error_abort);
3905a7f76a3SAlistair Francis     object_property_set_bool(OBJECT(&s->gem), true, "realized", &err);
3915a7f76a3SAlistair Francis     if (err) {
3925a7f76a3SAlistair Francis         error_propagate(errp, err);
3935a7f76a3SAlistair Francis         return;
3945a7f76a3SAlistair Francis     }
3955a7f76a3SAlistair Francis     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base);
3965a7f76a3SAlistair Francis     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
3975a7f76a3SAlistair Francis                        plic_gpios[SIFIVE_U_GEM_IRQ]);
398a7240d1eSMichael Clark }
399a7240d1eSMichael Clark 
400a7240d1eSMichael Clark static void riscv_sifive_u_machine_init(MachineClass *mc)
401a7240d1eSMichael Clark {
402a7240d1eSMichael Clark     mc->desc = "RISC-V Board compatible with SiFive U SDK";
403a7240d1eSMichael Clark     mc->init = riscv_sifive_u_init;
4048b1d0714SAlistair Francis     /* The real hardware has 5 CPUs, but one of them is a small embedded power
4058b1d0714SAlistair Francis      * management CPU.
4068b1d0714SAlistair Francis      */
4078b1d0714SAlistair Francis     mc->max_cpus = 4;
408a7240d1eSMichael Clark }
409a7240d1eSMichael Clark 
410a7240d1eSMichael Clark DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init)
4112308092bSAlistair Francis 
4122308092bSAlistair Francis static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data)
4132308092bSAlistair Francis {
4142308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
4152308092bSAlistair Francis 
4162308092bSAlistair Francis     dc->realize = riscv_sifive_u_soc_realize;
4172308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
4182308092bSAlistair Francis     dc->user_creatable = false;
4192308092bSAlistair Francis }
4202308092bSAlistair Francis 
4212308092bSAlistair Francis static const TypeInfo riscv_sifive_u_soc_type_info = {
4222308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
4232308092bSAlistair Francis     .parent = TYPE_DEVICE,
4242308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
4252308092bSAlistair Francis     .instance_init = riscv_sifive_u_soc_init,
4262308092bSAlistair Francis     .class_init = riscv_sifive_u_soc_class_init,
4272308092bSAlistair Francis };
4282308092bSAlistair Francis 
4292308092bSAlistair Francis static void riscv_sifive_u_soc_register_types(void)
4302308092bSAlistair Francis {
4312308092bSAlistair Francis     type_register_static(&riscv_sifive_u_soc_type_info);
4322308092bSAlistair Francis }
4332308092bSAlistair Francis 
4342308092bSAlistair Francis type_init(riscv_sifive_u_soc_register_types)
435