xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision fe93582cf52ee67f6ab5a59051d354344010cfdc)
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 
68a7240d1eSMichael Clark static uint64_t load_kernel(const char *kernel_filename)
69a7240d1eSMichael Clark {
70a7240d1eSMichael Clark     uint64_t kernel_entry, kernel_high;
71a7240d1eSMichael Clark 
72b7938980SMichael Clark     if (load_elf(kernel_filename, 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;
88*fe93582cSAnup Patel     char ethclk_names[] = "pclk\0hclk\0tx_clk";
89*fe93582cSAnup Patel     uint32_t plic_phandle, ethclk_phandle;
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--) {
124a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
125a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
1262308092bSAlistair Francis         char *isa = riscv_isa_string(&s->soc.cpus.harts[cpu]);
127a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, nodename);
1282a8756edSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
1292a8756edSMichael Clark                               SIFIVE_U_CLOCK_FREQ);
130a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
131a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
132a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
133a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
134a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
135a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
136a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
137a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "phandle", 1);
138a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", 1);
139a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
140a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
141a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
142a7240d1eSMichael Clark         g_free(isa);
143a7240d1eSMichael Clark         g_free(intc);
144a7240d1eSMichael Clark         g_free(nodename);
145a7240d1eSMichael Clark     }
146a7240d1eSMichael Clark 
1472308092bSAlistair Francis     cells =  g_new0(uint32_t, s->soc.cpus.num_harts * 4);
1482308092bSAlistair Francis     for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) {
149a7240d1eSMichael Clark         nodename =
150a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
151a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
152a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
153a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
154a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
155a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
156a7240d1eSMichael Clark         g_free(nodename);
157a7240d1eSMichael Clark     }
158a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
159a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_CLINT].base);
160a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
161a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
162a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
163a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_CLINT].base,
164a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_CLINT].size);
165a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
1662308092bSAlistair Francis         cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4);
167a7240d1eSMichael Clark     g_free(cells);
168a7240d1eSMichael Clark     g_free(nodename);
169a7240d1eSMichael Clark 
1702308092bSAlistair Francis     cells =  g_new0(uint32_t, s->soc.cpus.num_harts * 4);
1712308092bSAlistair Francis     for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) {
172a7240d1eSMichael Clark         nodename =
173a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
174a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
175a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
176a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT);
177a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
178a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT);
179a7240d1eSMichael Clark         g_free(nodename);
180a7240d1eSMichael Clark     }
181a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
182a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_PLIC].base);
183a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
184a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
185a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0");
186a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
187a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
1882308092bSAlistair Francis         cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4);
189a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
190a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_PLIC].base,
191a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_PLIC].size);
192a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
193a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7);
19498ceee7fSAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
195a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "phandle", 2);
196a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", 2);
197a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
198a7240d1eSMichael Clark     g_free(cells);
199a7240d1eSMichael Clark     g_free(nodename);
200a7240d1eSMichael Clark 
201*fe93582cSAnup Patel     nodename = g_strdup_printf("/soc/ethclk");
202*fe93582cSAnup Patel     qemu_fdt_add_subnode(fdt, nodename);
203*fe93582cSAnup Patel     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
204*fe93582cSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
205*fe93582cSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
206*fe93582cSAnup Patel         SIFIVE_U_GEM_CLOCK_FREQ);
207*fe93582cSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "phandle", 3);
208*fe93582cSAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "linux,phandle", 3);
209*fe93582cSAnup Patel     ethclk_phandle = qemu_fdt_get_phandle(fdt, nodename);
210*fe93582cSAnup Patel     g_free(nodename);
211*fe93582cSAnup Patel 
2125a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx",
2135a7f76a3SAlistair Francis         (long)memmap[SIFIVE_U_GEM].base);
2145a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
2155a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "compatible", "cdns,macb");
2165a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
2175a7f76a3SAlistair Francis         0x0, memmap[SIFIVE_U_GEM].base,
2185a7f76a3SAlistair Francis         0x0, memmap[SIFIVE_U_GEM].size);
2195a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
2205a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
2215a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle);
2225a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
223*fe93582cSAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
224*fe93582cSAnup Patel         ethclk_phandle, ethclk_phandle, ethclk_phandle);
225*fe93582cSAnup Patel     qemu_fdt_setprop(fdt, nodename, "clocks-names", ethclk_names,
226*fe93582cSAnup Patel         sizeof(ethclk_names));
2275a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "#address-cells", 1);
2285a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "#size-cells", 0);
2295a7f76a3SAlistair Francis     g_free(nodename);
2305a7f76a3SAlistair Francis 
2315a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
2325a7f76a3SAlistair Francis         (long)memmap[SIFIVE_U_GEM].base);
2335a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
2345a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg", 0x0);
2355a7f76a3SAlistair Francis     g_free(nodename);
2365a7f76a3SAlistair Francis 
237bde3ab9aSAlistair Francis     nodename = g_strdup_printf("/soc/uart@%lx",
238a7240d1eSMichael Clark         (long)memmap[SIFIVE_U_UART0].base);
239a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
240a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
241a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
242a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_UART0].base,
243a7240d1eSMichael Clark         0x0, memmap[SIFIVE_U_UART0].size);
244a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle);
245a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 1);
246a7240d1eSMichael Clark 
247a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
248a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
2497c28f4daSMichael Clark     if (cmdline) {
250a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
2517c28f4daSMichael Clark     }
252a7240d1eSMichael Clark     g_free(nodename);
253a7240d1eSMichael Clark }
254a7240d1eSMichael Clark 
255a7240d1eSMichael Clark static void riscv_sifive_u_init(MachineState *machine)
256a7240d1eSMichael Clark {
257a7240d1eSMichael Clark     const struct MemmapEntry *memmap = sifive_u_memmap;
258a7240d1eSMichael Clark 
259a7240d1eSMichael Clark     SiFiveUState *s = g_new0(SiFiveUState, 1);
2605aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
261a7240d1eSMichael Clark     MemoryRegion *main_mem = g_new(MemoryRegion, 1);
2625aec3247SMichael Clark     int i;
263a7240d1eSMichael Clark 
2642308092bSAlistair Francis     /* Initialize SoC */
2654eea9d7dSAlistair Francis     object_initialize_child(OBJECT(machine), "soc", &s->soc,
2664eea9d7dSAlistair Francis                             sizeof(s->soc), TYPE_RISCV_U_SOC,
2674eea9d7dSAlistair Francis                             &error_abort, NULL);
268a7240d1eSMichael Clark     object_property_set_bool(OBJECT(&s->soc), true, "realized",
269a7240d1eSMichael Clark                             &error_abort);
270a7240d1eSMichael Clark 
271a7240d1eSMichael Clark     /* register RAM */
272a7240d1eSMichael Clark     memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
273a7240d1eSMichael Clark                            machine->ram_size, &error_fatal);
2745aec3247SMichael Clark     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base,
275a7240d1eSMichael Clark                                 main_mem);
276a7240d1eSMichael Clark 
277a7240d1eSMichael Clark     /* create device tree */
278a7240d1eSMichael Clark     create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
279a7240d1eSMichael Clark 
280a7240d1eSMichael Clark     if (machine->kernel_filename) {
281a7240d1eSMichael Clark         load_kernel(machine->kernel_filename);
282a7240d1eSMichael Clark     }
283a7240d1eSMichael Clark 
284a7240d1eSMichael Clark     /* reset vector */
285a7240d1eSMichael Clark     uint32_t reset_vec[8] = {
286a7240d1eSMichael Clark         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(dtb) */
287a7240d1eSMichael Clark         0x02028593,                    /*     addi   a1, t0, %pcrel_lo(1b) */
288a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
289a7240d1eSMichael Clark #if defined(TARGET_RISCV32)
290a7240d1eSMichael Clark         0x0182a283,                    /*     lw     t0, 24(t0) */
291a7240d1eSMichael Clark #elif defined(TARGET_RISCV64)
292a7240d1eSMichael Clark         0x0182b283,                    /*     ld     t0, 24(t0) */
293a7240d1eSMichael Clark #endif
294a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
295a7240d1eSMichael Clark         0x00000000,
296a7240d1eSMichael Clark         memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */
297a7240d1eSMichael Clark         0x00000000,
298a7240d1eSMichael Clark                                        /* dtb: */
299a7240d1eSMichael Clark     };
300a7240d1eSMichael Clark 
3015aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
3025aec3247SMichael Clark     for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
3035aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
3045aec3247SMichael Clark     }
3055aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
3065aec3247SMichael Clark                           memmap[SIFIVE_U_MROM].base, &address_space_memory);
307a7240d1eSMichael Clark 
308a7240d1eSMichael Clark     /* copy in the device tree */
3095aec3247SMichael Clark     if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) >
3105aec3247SMichael Clark             memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) {
3115aec3247SMichael Clark         error_report("not enough space to store device-tree");
3125aec3247SMichael Clark         exit(1);
3135aec3247SMichael Clark     }
3145aec3247SMichael Clark     qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt));
3155aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt),
3165aec3247SMichael Clark                           memmap[SIFIVE_U_MROM].base + sizeof(reset_vec),
3175aec3247SMichael Clark                           &address_space_memory);
3182308092bSAlistair Francis }
3192308092bSAlistair Francis 
3202308092bSAlistair Francis static void riscv_sifive_u_soc_init(Object *obj)
3212308092bSAlistair Francis {
3222308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
3232308092bSAlistair Francis 
3244eea9d7dSAlistair Francis     object_initialize_child(obj, "cpus", &s->cpus, sizeof(s->cpus),
3254eea9d7dSAlistair Francis                             TYPE_RISCV_HART_ARRAY, &error_abort, NULL);
3262308092bSAlistair Francis     object_property_set_str(OBJECT(&s->cpus), SIFIVE_U_CPU, "cpu-type",
3272308092bSAlistair Francis                             &error_abort);
3282308092bSAlistair Francis     object_property_set_int(OBJECT(&s->cpus), smp_cpus, "num-harts",
3292308092bSAlistair Francis                             &error_abort);
3305a7f76a3SAlistair Francis 
3314eea9d7dSAlistair Francis     sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem),
3324eea9d7dSAlistair Francis                           TYPE_CADENCE_GEM);
3332308092bSAlistair Francis }
3342308092bSAlistair Francis 
3352308092bSAlistair Francis static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp)
3362308092bSAlistair Francis {
3372308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
3382308092bSAlistair Francis     const struct MemmapEntry *memmap = sifive_u_memmap;
3392308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
3402308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
3415a7f76a3SAlistair Francis     qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES];
3425a7f76a3SAlistair Francis     int i;
3435a7f76a3SAlistair Francis     Error *err = NULL;
3445a7f76a3SAlistair Francis     NICInfo *nd = &nd_table[0];
3452308092bSAlistair Francis 
3462308092bSAlistair Francis     object_property_set_bool(OBJECT(&s->cpus), true, "realized",
3472308092bSAlistair Francis                              &error_abort);
3482308092bSAlistair Francis 
3492308092bSAlistair Francis     /* boot rom */
3502308092bSAlistair Francis     memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom",
3512308092bSAlistair Francis                            memmap[SIFIVE_U_MROM].size, &error_fatal);
3522308092bSAlistair Francis     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base,
3532308092bSAlistair Francis                                 mask_rom);
354a7240d1eSMichael Clark 
355a7240d1eSMichael Clark     /* MMIO */
356a7240d1eSMichael Clark     s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base,
357a7240d1eSMichael Clark         (char *)SIFIVE_U_PLIC_HART_CONFIG,
358a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
359a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
360a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
361a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
362a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
363a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
364a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
365a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
366a7240d1eSMichael Clark         memmap[SIFIVE_U_PLIC].size);
3675aec3247SMichael Clark     sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base,
368647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
3695aec3247SMichael Clark     /* sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base,
370647a70a1SAlistair Francis         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic),
371647a70a1SAlistair Francis                                        SIFIVE_U_UART1_IRQ)); */
372a7240d1eSMichael Clark     sifive_clint_create(memmap[SIFIVE_U_CLINT].base,
373a7240d1eSMichael Clark         memmap[SIFIVE_U_CLINT].size, smp_cpus,
374a7240d1eSMichael Clark         SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
3755a7f76a3SAlistair Francis 
3765a7f76a3SAlistair Francis     for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) {
3775a7f76a3SAlistair Francis         plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i);
3785a7f76a3SAlistair Francis     }
3795a7f76a3SAlistair Francis 
3805a7f76a3SAlistair Francis     if (nd->used) {
3815a7f76a3SAlistair Francis         qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
3825a7f76a3SAlistair Francis         qdev_set_nic_properties(DEVICE(&s->gem), nd);
3835a7f76a3SAlistair Francis     }
3845a7f76a3SAlistair Francis     object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision",
3855a7f76a3SAlistair Francis                             &error_abort);
3865a7f76a3SAlistair Francis     object_property_set_bool(OBJECT(&s->gem), true, "realized", &err);
3875a7f76a3SAlistair Francis     if (err) {
3885a7f76a3SAlistair Francis         error_propagate(errp, err);
3895a7f76a3SAlistair Francis         return;
3905a7f76a3SAlistair Francis     }
3915a7f76a3SAlistair Francis     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base);
3925a7f76a3SAlistair Francis     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
3935a7f76a3SAlistair Francis                        plic_gpios[SIFIVE_U_GEM_IRQ]);
394a7240d1eSMichael Clark }
395a7240d1eSMichael Clark 
396a7240d1eSMichael Clark static void riscv_sifive_u_machine_init(MachineClass *mc)
397a7240d1eSMichael Clark {
398a7240d1eSMichael Clark     mc->desc = "RISC-V Board compatible with SiFive U SDK";
399a7240d1eSMichael Clark     mc->init = riscv_sifive_u_init;
400a7240d1eSMichael Clark     mc->max_cpus = 1;
401a7240d1eSMichael Clark }
402a7240d1eSMichael Clark 
403a7240d1eSMichael Clark DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init)
4042308092bSAlistair Francis 
4052308092bSAlistair Francis static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data)
4062308092bSAlistair Francis {
4072308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
4082308092bSAlistair Francis 
4092308092bSAlistair Francis     dc->realize = riscv_sifive_u_soc_realize;
4102308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
4112308092bSAlistair Francis     dc->user_creatable = false;
4122308092bSAlistair Francis }
4132308092bSAlistair Francis 
4142308092bSAlistair Francis static const TypeInfo riscv_sifive_u_soc_type_info = {
4152308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
4162308092bSAlistair Francis     .parent = TYPE_DEVICE,
4172308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
4182308092bSAlistair Francis     .instance_init = riscv_sifive_u_soc_init,
4192308092bSAlistair Francis     .class_init = riscv_sifive_u_soc_class_init,
4202308092bSAlistair Francis };
4212308092bSAlistair Francis 
4222308092bSAlistair Francis static void riscv_sifive_u_soc_register_types(void)
4232308092bSAlistair Francis {
4242308092bSAlistair Francis     type_register_static(&riscv_sifive_u_soc_type_info);
4252308092bSAlistair Francis }
4262308092bSAlistair Francis 
4272308092bSAlistair Francis type_init(riscv_sifive_u_soc_register_types)
428