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" 440ac24d56SAlistair Francis #include "hw/riscv/boot.h" 45a7240d1eSMichael Clark #include "chardev/char.h" 46a7240d1eSMichael Clark #include "sysemu/arch_init.h" 47a7240d1eSMichael Clark #include "sysemu/device_tree.h" 48a7240d1eSMichael Clark #include "exec/address-spaces.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 void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, 69a7240d1eSMichael Clark uint64_t mem_size, const char *cmdline) 70a7240d1eSMichael Clark { 71a7240d1eSMichael Clark void *fdt; 72a7240d1eSMichael Clark int cpu; 73a7240d1eSMichael Clark uint32_t *cells; 74a7240d1eSMichael Clark char *nodename; 75fe93582cSAnup Patel char ethclk_names[] = "pclk\0hclk\0tx_clk"; 76382cb439SBin Meng uint32_t plic_phandle, ethclk_phandle, phandle = 1; 77a7240d1eSMichael Clark 78a7240d1eSMichael Clark fdt = s->fdt = create_device_tree(&s->fdt_size); 79a7240d1eSMichael Clark if (!fdt) { 80a7240d1eSMichael Clark error_report("create_device_tree() failed"); 81a7240d1eSMichael Clark exit(1); 82a7240d1eSMichael Clark } 83a7240d1eSMichael Clark 84a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu"); 85a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev"); 86a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 87a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 88a7240d1eSMichael Clark 89a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/soc"); 90a7240d1eSMichael Clark qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 912a1a6f6dSAlistair Francis qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); 92a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 93a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 94a7240d1eSMichael Clark 95a7240d1eSMichael Clark nodename = g_strdup_printf("/memory@%lx", 96a7240d1eSMichael Clark (long)memmap[SIFIVE_U_DRAM].base); 97a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 98a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 99a7240d1eSMichael Clark memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base, 100a7240d1eSMichael Clark mem_size >> 32, mem_size); 101a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 102a7240d1eSMichael Clark g_free(nodename); 103a7240d1eSMichael Clark 104a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/cpus"); 1052a8756edSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 1062a8756edSMichael Clark SIFIVE_CLINT_TIMEBASE_FREQ); 107a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 108a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 109a7240d1eSMichael Clark 1102308092bSAlistair Francis for (cpu = s->soc.cpus.num_harts - 1; cpu >= 0; cpu--) { 111382cb439SBin Meng int cpu_phandle = phandle++; 112a7240d1eSMichael Clark nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 113a7240d1eSMichael Clark char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 1142308092bSAlistair Francis char *isa = riscv_isa_string(&s->soc.cpus.harts[cpu]); 115a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 1162a8756edSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 1172a8756edSMichael Clark SIFIVE_U_CLOCK_FREQ); 118a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 119a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 120a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 121a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 122a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 123a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 124a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, intc); 125382cb439SBin Meng qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); 126382cb439SBin Meng qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", cpu_phandle); 127a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 128a7240d1eSMichael Clark qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 129a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 130a7240d1eSMichael Clark g_free(isa); 131a7240d1eSMichael Clark g_free(intc); 132a7240d1eSMichael Clark g_free(nodename); 133a7240d1eSMichael Clark } 134a7240d1eSMichael Clark 1352308092bSAlistair Francis cells = g_new0(uint32_t, s->soc.cpus.num_harts * 4); 1362308092bSAlistair Francis for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) { 137a7240d1eSMichael Clark nodename = 138a7240d1eSMichael Clark g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 139a7240d1eSMichael Clark uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 140a7240d1eSMichael Clark cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 141a7240d1eSMichael Clark cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 142a7240d1eSMichael Clark cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 143a7240d1eSMichael Clark cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 144a7240d1eSMichael Clark g_free(nodename); 145a7240d1eSMichael Clark } 146a7240d1eSMichael Clark nodename = g_strdup_printf("/soc/clint@%lx", 147a7240d1eSMichael Clark (long)memmap[SIFIVE_U_CLINT].base); 148a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 149a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 150a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 151a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_CLINT].base, 152a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_CLINT].size); 153a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 1542308092bSAlistair Francis cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4); 155a7240d1eSMichael Clark g_free(cells); 156a7240d1eSMichael Clark g_free(nodename); 157a7240d1eSMichael Clark 158382cb439SBin Meng plic_phandle = phandle++; 1592308092bSAlistair Francis cells = g_new0(uint32_t, s->soc.cpus.num_harts * 4); 1602308092bSAlistair Francis for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) { 161a7240d1eSMichael Clark nodename = 162a7240d1eSMichael Clark g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 163a7240d1eSMichael Clark uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 164a7240d1eSMichael Clark cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 165a7240d1eSMichael Clark cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT); 166a7240d1eSMichael Clark cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 167a7240d1eSMichael Clark cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT); 168a7240d1eSMichael Clark g_free(nodename); 169a7240d1eSMichael Clark } 170a7240d1eSMichael Clark nodename = g_strdup_printf("/soc/interrupt-controller@%lx", 171a7240d1eSMichael Clark (long)memmap[SIFIVE_U_PLIC].base); 172a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 173a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); 174a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); 175a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 176a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 1772308092bSAlistair Francis cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4); 178a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 179a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_PLIC].base, 180a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_PLIC].size); 181a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 182a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7); 18398ceee7fSAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); 184382cb439SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "phandle", plic_phandle); 185382cb439SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", plic_phandle); 186a7240d1eSMichael Clark plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 187a7240d1eSMichael Clark g_free(cells); 188a7240d1eSMichael Clark g_free(nodename); 189a7240d1eSMichael Clark 190382cb439SBin Meng ethclk_phandle = phandle++; 191fe93582cSAnup Patel nodename = g_strdup_printf("/soc/ethclk"); 192fe93582cSAnup Patel qemu_fdt_add_subnode(fdt, nodename); 193fe93582cSAnup Patel qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); 194fe93582cSAnup Patel qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); 195fe93582cSAnup Patel qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 196fe93582cSAnup Patel SIFIVE_U_GEM_CLOCK_FREQ); 197382cb439SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", ethclk_phandle); 198382cb439SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "linux,phandle", ethclk_phandle); 199fe93582cSAnup Patel ethclk_phandle = qemu_fdt_get_phandle(fdt, nodename); 200fe93582cSAnup Patel g_free(nodename); 201fe93582cSAnup Patel 2025a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx", 2035a7f76a3SAlistair Francis (long)memmap[SIFIVE_U_GEM].base); 2045a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 2055a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "compatible", "cdns,macb"); 2065a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 2075a7f76a3SAlistair Francis 0x0, memmap[SIFIVE_U_GEM].base, 2085a7f76a3SAlistair Francis 0x0, memmap[SIFIVE_U_GEM].size); 2095a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 2105a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); 2115a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 2125a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); 213fe93582cSAnup Patel qemu_fdt_setprop_cells(fdt, nodename, "clocks", 214fe93582cSAnup Patel ethclk_phandle, ethclk_phandle, ethclk_phandle); 215fe93582cSAnup Patel qemu_fdt_setprop(fdt, nodename, "clocks-names", ethclk_names, 216fe93582cSAnup Patel sizeof(ethclk_names)); 2175a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "#address-cells", 1); 2185a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "#size-cells", 0); 2195a7f76a3SAlistair Francis g_free(nodename); 2205a7f76a3SAlistair Francis 2215a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", 2225a7f76a3SAlistair Francis (long)memmap[SIFIVE_U_GEM].base); 2235a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 2245a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 0x0); 2255a7f76a3SAlistair Francis g_free(nodename); 2265a7f76a3SAlistair Francis 227bde3ab9aSAlistair Francis nodename = g_strdup_printf("/soc/uart@%lx", 228a7240d1eSMichael Clark (long)memmap[SIFIVE_U_UART0].base); 229a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 230a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 231a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 232a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_UART0].base, 233a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_UART0].size); 2346c60757eSAnup Patel qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 2356c60757eSAnup Patel SIFIVE_U_CLOCK_FREQ / 2); 236a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 237a9ec1c76SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); 238a7240d1eSMichael Clark 239a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/chosen"); 240a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 2417c28f4daSMichael Clark if (cmdline) { 242a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 2437c28f4daSMichael Clark } 244a7240d1eSMichael Clark g_free(nodename); 245a7240d1eSMichael Clark } 246a7240d1eSMichael Clark 247a7240d1eSMichael Clark static void riscv_sifive_u_init(MachineState *machine) 248a7240d1eSMichael Clark { 249a7240d1eSMichael Clark const struct MemmapEntry *memmap = sifive_u_memmap; 250a7240d1eSMichael Clark 251a7240d1eSMichael Clark SiFiveUState *s = g_new0(SiFiveUState, 1); 2525aec3247SMichael Clark MemoryRegion *system_memory = get_system_memory(); 253a7240d1eSMichael Clark MemoryRegion *main_mem = g_new(MemoryRegion, 1); 2545aec3247SMichael Clark int i; 255a7240d1eSMichael Clark 2562308092bSAlistair Francis /* Initialize SoC */ 2574eea9d7dSAlistair Francis object_initialize_child(OBJECT(machine), "soc", &s->soc, 2584eea9d7dSAlistair Francis sizeof(s->soc), TYPE_RISCV_U_SOC, 2594eea9d7dSAlistair Francis &error_abort, NULL); 260a7240d1eSMichael Clark object_property_set_bool(OBJECT(&s->soc), true, "realized", 261a7240d1eSMichael Clark &error_abort); 262a7240d1eSMichael Clark 263a7240d1eSMichael Clark /* register RAM */ 264a7240d1eSMichael Clark memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", 265a7240d1eSMichael Clark machine->ram_size, &error_fatal); 2665aec3247SMichael Clark memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base, 267a7240d1eSMichael Clark main_mem); 268a7240d1eSMichael Clark 269a7240d1eSMichael Clark /* create device tree */ 270a7240d1eSMichael Clark create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 271a7240d1eSMichael Clark 272*b3042223SAlistair Francis if (machine->firmware) { 273*b3042223SAlistair Francis riscv_load_firmware(machine->firmware, memmap[SIFIVE_U_DRAM].base); 274*b3042223SAlistair Francis } 275*b3042223SAlistair Francis 276a7240d1eSMichael Clark if (machine->kernel_filename) { 2770ac24d56SAlistair Francis riscv_load_kernel(machine->kernel_filename); 278a7240d1eSMichael Clark } 279a7240d1eSMichael Clark 280a7240d1eSMichael Clark /* reset vector */ 281a7240d1eSMichael Clark uint32_t reset_vec[8] = { 282a7240d1eSMichael Clark 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 283a7240d1eSMichael Clark 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 284a7240d1eSMichael Clark 0xf1402573, /* csrr a0, mhartid */ 285a7240d1eSMichael Clark #if defined(TARGET_RISCV32) 286a7240d1eSMichael Clark 0x0182a283, /* lw t0, 24(t0) */ 287a7240d1eSMichael Clark #elif defined(TARGET_RISCV64) 288a7240d1eSMichael Clark 0x0182b283, /* ld t0, 24(t0) */ 289a7240d1eSMichael Clark #endif 290a7240d1eSMichael Clark 0x00028067, /* jr t0 */ 291a7240d1eSMichael Clark 0x00000000, 292a7240d1eSMichael Clark memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */ 293a7240d1eSMichael Clark 0x00000000, 294a7240d1eSMichael Clark /* dtb: */ 295a7240d1eSMichael Clark }; 296a7240d1eSMichael Clark 2975aec3247SMichael Clark /* copy in the reset vector in little_endian byte order */ 2985aec3247SMichael Clark for (i = 0; i < sizeof(reset_vec) >> 2; i++) { 2995aec3247SMichael Clark reset_vec[i] = cpu_to_le32(reset_vec[i]); 3005aec3247SMichael Clark } 3015aec3247SMichael Clark rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), 3025aec3247SMichael Clark memmap[SIFIVE_U_MROM].base, &address_space_memory); 303a7240d1eSMichael Clark 304a7240d1eSMichael Clark /* copy in the device tree */ 3055aec3247SMichael Clark if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) > 3065aec3247SMichael Clark memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) { 3075aec3247SMichael Clark error_report("not enough space to store device-tree"); 3085aec3247SMichael Clark exit(1); 3095aec3247SMichael Clark } 3105aec3247SMichael Clark qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt)); 3115aec3247SMichael Clark rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt), 3125aec3247SMichael Clark memmap[SIFIVE_U_MROM].base + sizeof(reset_vec), 3135aec3247SMichael Clark &address_space_memory); 3142308092bSAlistair Francis } 3152308092bSAlistair Francis 3162308092bSAlistair Francis static void riscv_sifive_u_soc_init(Object *obj) 3172308092bSAlistair Francis { 3182308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(obj); 3192308092bSAlistair Francis 3204eea9d7dSAlistair Francis object_initialize_child(obj, "cpus", &s->cpus, sizeof(s->cpus), 3214eea9d7dSAlistair Francis TYPE_RISCV_HART_ARRAY, &error_abort, NULL); 3222308092bSAlistair Francis object_property_set_str(OBJECT(&s->cpus), SIFIVE_U_CPU, "cpu-type", 3232308092bSAlistair Francis &error_abort); 3242308092bSAlistair Francis object_property_set_int(OBJECT(&s->cpus), smp_cpus, "num-harts", 3252308092bSAlistair Francis &error_abort); 3265a7f76a3SAlistair Francis 3274eea9d7dSAlistair Francis sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem), 3284eea9d7dSAlistair Francis TYPE_CADENCE_GEM); 3292308092bSAlistair Francis } 3302308092bSAlistair Francis 3312308092bSAlistair Francis static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp) 3322308092bSAlistair Francis { 3332308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(dev); 3342308092bSAlistair Francis const struct MemmapEntry *memmap = sifive_u_memmap; 3352308092bSAlistair Francis MemoryRegion *system_memory = get_system_memory(); 3362308092bSAlistair Francis MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 3375a7f76a3SAlistair Francis qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES]; 33805446f41SBin Meng char *plic_hart_config; 33905446f41SBin Meng size_t plic_hart_config_len; 3405a7f76a3SAlistair Francis int i; 3415a7f76a3SAlistair Francis Error *err = NULL; 3425a7f76a3SAlistair Francis NICInfo *nd = &nd_table[0]; 3432308092bSAlistair Francis 3442308092bSAlistair Francis object_property_set_bool(OBJECT(&s->cpus), true, "realized", 3452308092bSAlistair Francis &error_abort); 3462308092bSAlistair Francis 3472308092bSAlistair Francis /* boot rom */ 3482308092bSAlistair Francis memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom", 3492308092bSAlistair Francis memmap[SIFIVE_U_MROM].size, &error_fatal); 3502308092bSAlistair Francis memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base, 3512308092bSAlistair Francis mask_rom); 352a7240d1eSMichael Clark 35305446f41SBin Meng /* create PLIC hart topology configuration string */ 35405446f41SBin Meng plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * smp_cpus; 35505446f41SBin Meng plic_hart_config = g_malloc0(plic_hart_config_len); 35605446f41SBin Meng for (i = 0; i < smp_cpus; i++) { 35705446f41SBin Meng if (i != 0) { 35805446f41SBin Meng strncat(plic_hart_config, ",", plic_hart_config_len); 35905446f41SBin Meng } 36005446f41SBin Meng strncat(plic_hart_config, SIFIVE_U_PLIC_HART_CONFIG, 36105446f41SBin Meng plic_hart_config_len); 36205446f41SBin Meng plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); 36305446f41SBin Meng } 36405446f41SBin Meng 365a7240d1eSMichael Clark /* MMIO */ 366a7240d1eSMichael Clark s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, 36705446f41SBin Meng plic_hart_config, 368a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_SOURCES, 369a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_PRIORITIES, 370a7240d1eSMichael Clark SIFIVE_U_PLIC_PRIORITY_BASE, 371a7240d1eSMichael Clark SIFIVE_U_PLIC_PENDING_BASE, 372a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_BASE, 373a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_STRIDE, 374a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_BASE, 375a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_STRIDE, 376a7240d1eSMichael Clark memmap[SIFIVE_U_PLIC].size); 3775aec3247SMichael Clark sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base, 378647a70a1SAlistair Francis serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); 379194eef09SMichael Clark sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base, 380194eef09SMichael Clark serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); 381a7240d1eSMichael Clark sifive_clint_create(memmap[SIFIVE_U_CLINT].base, 382a7240d1eSMichael Clark memmap[SIFIVE_U_CLINT].size, smp_cpus, 383a7240d1eSMichael Clark SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 3845a7f76a3SAlistair Francis 3855a7f76a3SAlistair Francis for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) { 3865a7f76a3SAlistair Francis plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i); 3875a7f76a3SAlistair Francis } 3885a7f76a3SAlistair Francis 3895a7f76a3SAlistair Francis if (nd->used) { 3905a7f76a3SAlistair Francis qemu_check_nic_model(nd, TYPE_CADENCE_GEM); 3915a7f76a3SAlistair Francis qdev_set_nic_properties(DEVICE(&s->gem), nd); 3925a7f76a3SAlistair Francis } 3935a7f76a3SAlistair Francis object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision", 3945a7f76a3SAlistair Francis &error_abort); 3955a7f76a3SAlistair Francis object_property_set_bool(OBJECT(&s->gem), true, "realized", &err); 3965a7f76a3SAlistair Francis if (err) { 3975a7f76a3SAlistair Francis error_propagate(errp, err); 3985a7f76a3SAlistair Francis return; 3995a7f76a3SAlistair Francis } 4005a7f76a3SAlistair Francis sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base); 4015a7f76a3SAlistair Francis sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, 4025a7f76a3SAlistair Francis plic_gpios[SIFIVE_U_GEM_IRQ]); 403a7240d1eSMichael Clark } 404a7240d1eSMichael Clark 405a7240d1eSMichael Clark static void riscv_sifive_u_machine_init(MachineClass *mc) 406a7240d1eSMichael Clark { 407a7240d1eSMichael Clark mc->desc = "RISC-V Board compatible with SiFive U SDK"; 408a7240d1eSMichael Clark mc->init = riscv_sifive_u_init; 4098b1d0714SAlistair Francis /* The real hardware has 5 CPUs, but one of them is a small embedded power 4108b1d0714SAlistair Francis * management CPU. 4118b1d0714SAlistair Francis */ 4128b1d0714SAlistair Francis mc->max_cpus = 4; 413a7240d1eSMichael Clark } 414a7240d1eSMichael Clark 415a7240d1eSMichael Clark DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init) 4162308092bSAlistair Francis 4172308092bSAlistair Francis static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data) 4182308092bSAlistair Francis { 4192308092bSAlistair Francis DeviceClass *dc = DEVICE_CLASS(oc); 4202308092bSAlistair Francis 4212308092bSAlistair Francis dc->realize = riscv_sifive_u_soc_realize; 4222308092bSAlistair Francis /* Reason: Uses serial_hds in realize function, thus can't be used twice */ 4232308092bSAlistair Francis dc->user_creatable = false; 4242308092bSAlistair Francis } 4252308092bSAlistair Francis 4262308092bSAlistair Francis static const TypeInfo riscv_sifive_u_soc_type_info = { 4272308092bSAlistair Francis .name = TYPE_RISCV_U_SOC, 4282308092bSAlistair Francis .parent = TYPE_DEVICE, 4292308092bSAlistair Francis .instance_size = sizeof(SiFiveUSoCState), 4302308092bSAlistair Francis .instance_init = riscv_sifive_u_soc_init, 4312308092bSAlistair Francis .class_init = riscv_sifive_u_soc_class_init, 4322308092bSAlistair Francis }; 4332308092bSAlistair Francis 4342308092bSAlistair Francis static void riscv_sifive_u_soc_register_types(void) 4352308092bSAlistair Francis { 4362308092bSAlistair Francis type_register_static(&riscv_sifive_u_soc_type_info); 4372308092bSAlistair Francis } 4382308092bSAlistair Francis 4392308092bSAlistair Francis type_init(riscv_sifive_u_soc_register_types) 440