xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision 2a1a6f6d47f192a12a3765a3558a11d619d25237)
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 },
63a7240d1eSMichael Clark };
64a7240d1eSMichael Clark 
65a7240d1eSMichael Clark static uint64_t load_kernel(const char *kernel_filename)
66a7240d1eSMichael Clark {
67a7240d1eSMichael Clark     uint64_t kernel_entry, kernel_high;
68a7240d1eSMichael Clark 
69b7938980SMichael Clark     if (load_elf(kernel_filename, NULL, NULL,
70a7240d1eSMichael Clark                  &kernel_entry, NULL, &kernel_high,
7189854803SMichael Clark                  0, EM_RISCV, 1, 0) < 0) {
72a7240d1eSMichael Clark         error_report("qemu: could not load kernel '%s'", kernel_filename);
73a7240d1eSMichael Clark         exit(1);
74a7240d1eSMichael Clark     }
75a7240d1eSMichael Clark     return kernel_entry;
76a7240d1eSMichael Clark }
77a7240d1eSMichael Clark 
78a7240d1eSMichael Clark static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,
79a7240d1eSMichael Clark     uint64_t mem_size, const char *cmdline)
80a7240d1eSMichael Clark {
81a7240d1eSMichael Clark     void *fdt;
82a7240d1eSMichael Clark     int cpu;
83a7240d1eSMichael Clark     uint32_t *cells;
84a7240d1eSMichael Clark     char *nodename;
85a7240d1eSMichael Clark     uint32_t plic_phandle;
86a7240d1eSMichael Clark 
87a7240d1eSMichael Clark     fdt = s->fdt = create_device_tree(&s->fdt_size);
88a7240d1eSMichael Clark     if (!fdt) {
89a7240d1eSMichael Clark         error_report("create_device_tree() failed");
90a7240d1eSMichael Clark         exit(1);
91a7240d1eSMichael Clark     }
92a7240d1eSMichael Clark 
93a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
94a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
95a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
96a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
97a7240d1eSMichael Clark 
98a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/soc");
99a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
100*2a1a6f6dSAlistair Francis     qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
101a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
102a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
103a7240d1eSMichael Clark 
104a7240d1eSMichael Clark     nodename = g_strdup_printf("/memory@%lx",
105a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_DRAM].base);
106a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
107a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
108a7240d1eSMichael Clark         memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base,
109a7240d1eSMichael Clark         mem_size >> 32, mem_size);
110a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
111a7240d1eSMichael Clark     g_free(nodename);
112a7240d1eSMichael Clark 
113a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/cpus");
1142a8756edSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
1152a8756edSMichael Clark         SIFIVE_CLINT_TIMEBASE_FREQ);
116a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
117a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
118a7240d1eSMichael Clark 
1192308092bSAlistair Francis     for (cpu = s->soc.cpus.num_harts - 1; cpu >= 0; cpu--) {
120a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
121a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
1222308092bSAlistair Francis         char *isa = riscv_isa_string(&s->soc.cpus.harts[cpu]);
123a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, nodename);
1242a8756edSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
1252a8756edSMichael Clark                               SIFIVE_U_CLOCK_FREQ);
126a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
127a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
128a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
129a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
130a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
131a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
132a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
133a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "phandle", 1);
134a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", 1);
135a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
136a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
137a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
138a7240d1eSMichael Clark         g_free(isa);
139a7240d1eSMichael Clark         g_free(intc);
140a7240d1eSMichael Clark         g_free(nodename);
141a7240d1eSMichael Clark     }
142a7240d1eSMichael Clark 
1432308092bSAlistair Francis     cells =  g_new0(uint32_t, s->soc.cpus.num_harts * 4);
1442308092bSAlistair Francis     for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) {
145a7240d1eSMichael Clark         nodename =
146a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
147a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
148a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
149a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
150a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
151a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
152a7240d1eSMichael Clark         g_free(nodename);
153a7240d1eSMichael Clark     }
154a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
155a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_CLINT].base);
156a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
157a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
158a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
159a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_CLINT].base,
160a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_CLINT].size);
161a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
1622308092bSAlistair Francis         cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4);
163a7240d1eSMichael Clark     g_free(cells);
164a7240d1eSMichael Clark     g_free(nodename);
165a7240d1eSMichael Clark 
1662308092bSAlistair Francis     cells =  g_new0(uint32_t, s->soc.cpus.num_harts * 4);
1672308092bSAlistair Francis     for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) {
168a7240d1eSMichael Clark         nodename =
169a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
170a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
171a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
172a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT);
173a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
174a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT);
175a7240d1eSMichael Clark         g_free(nodename);
176a7240d1eSMichael Clark     }
177a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
178a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_PLIC].base);
179a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
180a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
181a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0");
182a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
183a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
1842308092bSAlistair Francis         cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4);
185a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
186a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_PLIC].base,
187a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_PLIC].size);
188a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
189a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7);
190a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 4);
191a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "phandle", 2);
192a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", 2);
193a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
194a7240d1eSMichael Clark     g_free(cells);
195a7240d1eSMichael Clark     g_free(nodename);
196a7240d1eSMichael Clark 
197a7240d1eSMichael Clark     nodename = g_strdup_printf("/uart@%lx",
198a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_UART0].base);
199a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
200a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
201a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
202a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_UART0].base,
203a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_UART0].size);
204a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle);
205a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 1);
206a7240d1eSMichael Clark 
207a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
208a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
209a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
210a7240d1eSMichael Clark     g_free(nodename);
211a7240d1eSMichael Clark }
212a7240d1eSMichael Clark 
213a7240d1eSMichael Clark static void riscv_sifive_u_init(MachineState *machine)
214a7240d1eSMichael Clark {
215a7240d1eSMichael Clark     const struct MemmapEntry *memmap = sifive_u_memmap;
216a7240d1eSMichael Clark 
217a7240d1eSMichael Clark     SiFiveUState *s = g_new0(SiFiveUState, 1);
2185aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
219a7240d1eSMichael Clark     MemoryRegion *main_mem = g_new(MemoryRegion, 1);
2205aec3247SMichael Clark     int i;
221a7240d1eSMichael Clark 
2222308092bSAlistair Francis     /* Initialize SoC */
2232308092bSAlistair Francis     object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_U_SOC);
224a7240d1eSMichael Clark     object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc),
225a7240d1eSMichael Clark                               &error_abort);
226a7240d1eSMichael Clark     object_property_set_bool(OBJECT(&s->soc), true, "realized",
227a7240d1eSMichael Clark                             &error_abort);
228a7240d1eSMichael Clark 
229a7240d1eSMichael Clark     /* register RAM */
230a7240d1eSMichael Clark     memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
231a7240d1eSMichael Clark                            machine->ram_size, &error_fatal);
2325aec3247SMichael Clark     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base,
233a7240d1eSMichael Clark                                 main_mem);
234a7240d1eSMichael Clark 
235a7240d1eSMichael Clark     /* create device tree */
236a7240d1eSMichael Clark     create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
237a7240d1eSMichael Clark 
238a7240d1eSMichael Clark     if (machine->kernel_filename) {
239a7240d1eSMichael Clark         load_kernel(machine->kernel_filename);
240a7240d1eSMichael Clark     }
241a7240d1eSMichael Clark 
242a7240d1eSMichael Clark     /* reset vector */
243a7240d1eSMichael Clark     uint32_t reset_vec[8] = {
244a7240d1eSMichael Clark         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(dtb) */
245a7240d1eSMichael Clark         0x02028593,                    /*     addi   a1, t0, %pcrel_lo(1b) */
246a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
247a7240d1eSMichael Clark #if defined(TARGET_RISCV32)
248a7240d1eSMichael Clark         0x0182a283,                    /*     lw     t0, 24(t0) */
249a7240d1eSMichael Clark #elif defined(TARGET_RISCV64)
250a7240d1eSMichael Clark         0x0182b283,                    /*     ld     t0, 24(t0) */
251a7240d1eSMichael Clark #endif
252a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
253a7240d1eSMichael Clark         0x00000000,
254a7240d1eSMichael Clark         memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */
255a7240d1eSMichael Clark         0x00000000,
256a7240d1eSMichael Clark                                        /* dtb: */
257a7240d1eSMichael Clark     };
258a7240d1eSMichael Clark 
2595aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
2605aec3247SMichael Clark     for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
2615aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
2625aec3247SMichael Clark     }
2635aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
2645aec3247SMichael Clark                           memmap[SIFIVE_U_MROM].base, &address_space_memory);
265a7240d1eSMichael Clark 
266a7240d1eSMichael Clark     /* copy in the device tree */
2675aec3247SMichael Clark     if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) >
2685aec3247SMichael Clark             memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) {
2695aec3247SMichael Clark         error_report("not enough space to store device-tree");
2705aec3247SMichael Clark         exit(1);
2715aec3247SMichael Clark     }
2725aec3247SMichael Clark     qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt));
2735aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt),
2745aec3247SMichael Clark                           memmap[SIFIVE_U_MROM].base + sizeof(reset_vec),
2755aec3247SMichael Clark                           &address_space_memory);
2762308092bSAlistair Francis }
2772308092bSAlistair Francis 
2782308092bSAlistair Francis static void riscv_sifive_u_soc_init(Object *obj)
2792308092bSAlistair Francis {
2802308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
2812308092bSAlistair Francis 
2822308092bSAlistair Francis     object_initialize(&s->cpus, sizeof(s->cpus), TYPE_RISCV_HART_ARRAY);
2832308092bSAlistair Francis     object_property_add_child(obj, "cpus", OBJECT(&s->cpus),
2842308092bSAlistair Francis                               &error_abort);
2852308092bSAlistair Francis     object_property_set_str(OBJECT(&s->cpus), SIFIVE_U_CPU, "cpu-type",
2862308092bSAlistair Francis                             &error_abort);
2872308092bSAlistair Francis     object_property_set_int(OBJECT(&s->cpus), smp_cpus, "num-harts",
2882308092bSAlistair Francis                             &error_abort);
2892308092bSAlistair Francis }
2902308092bSAlistair Francis 
2912308092bSAlistair Francis static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp)
2922308092bSAlistair Francis {
2932308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
2942308092bSAlistair Francis     const struct MemmapEntry *memmap = sifive_u_memmap;
2952308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
2962308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
2972308092bSAlistair Francis 
2982308092bSAlistair Francis     object_property_set_bool(OBJECT(&s->cpus), true, "realized",
2992308092bSAlistair Francis                              &error_abort);
3002308092bSAlistair Francis 
3012308092bSAlistair Francis     /* boot rom */
3022308092bSAlistair Francis     memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom",
3032308092bSAlistair Francis                            memmap[SIFIVE_U_MROM].size, &error_fatal);
3042308092bSAlistair Francis     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base,
3052308092bSAlistair Francis                                 mask_rom);
306a7240d1eSMichael Clark 
307a7240d1eSMichael Clark     /* MMIO */
308a7240d1eSMichael Clark     s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base,
309a7240d1eSMichael Clark         (char *)SIFIVE_U_PLIC_HART_CONFIG,
310a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
311a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
312a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
313a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
314a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
315a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
316a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
317a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
318a7240d1eSMichael Clark         memmap[SIFIVE_U_PLIC].size);
3195aec3247SMichael Clark     sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base,
320647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
3215aec3247SMichael Clark     /* sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base,
322647a70a1SAlistair Francis         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic),
323647a70a1SAlistair Francis                                        SIFIVE_U_UART1_IRQ)); */
324a7240d1eSMichael Clark     sifive_clint_create(memmap[SIFIVE_U_CLINT].base,
325a7240d1eSMichael Clark         memmap[SIFIVE_U_CLINT].size, smp_cpus,
326a7240d1eSMichael Clark         SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
327a7240d1eSMichael Clark }
328a7240d1eSMichael Clark 
329a7240d1eSMichael Clark static void riscv_sifive_u_machine_init(MachineClass *mc)
330a7240d1eSMichael Clark {
331a7240d1eSMichael Clark     mc->desc = "RISC-V Board compatible with SiFive U SDK";
332a7240d1eSMichael Clark     mc->init = riscv_sifive_u_init;
333a7240d1eSMichael Clark     mc->max_cpus = 1;
334a7240d1eSMichael Clark }
335a7240d1eSMichael Clark 
336a7240d1eSMichael Clark DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init)
3372308092bSAlistair Francis 
3382308092bSAlistair Francis static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data)
3392308092bSAlistair Francis {
3402308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
3412308092bSAlistair Francis 
3422308092bSAlistair Francis     dc->realize = riscv_sifive_u_soc_realize;
3432308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
3442308092bSAlistair Francis     dc->user_creatable = false;
3452308092bSAlistair Francis }
3462308092bSAlistair Francis 
3472308092bSAlistair Francis static const TypeInfo riscv_sifive_u_soc_type_info = {
3482308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
3492308092bSAlistair Francis     .parent = TYPE_DEVICE,
3502308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
3512308092bSAlistair Francis     .instance_init = riscv_sifive_u_soc_init,
3522308092bSAlistair Francis     .class_init = riscv_sifive_u_soc_class_init,
3532308092bSAlistair Francis };
3542308092bSAlistair Francis 
3552308092bSAlistair Francis static void riscv_sifive_u_soc_register_types(void)
3562308092bSAlistair Francis {
3572308092bSAlistair Francis     type_register_static(&riscv_sifive_u_soc_type_info);
3582308092bSAlistair Francis }
3592308092bSAlistair Francis 
3602308092bSAlistair Francis type_init(riscv_sifive_u_soc_register_types)
361