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