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. 67b6bb66fSBin Meng * Copyright (c) 2019 Bin Meng <bmeng.cn@gmail.com> 7a7240d1eSMichael Clark * 8a7240d1eSMichael Clark * Provides a board compatible with the SiFive Freedom U SDK: 9a7240d1eSMichael Clark * 10a7240d1eSMichael Clark * 0) UART 11a7240d1eSMichael Clark * 1) CLINT (Core Level Interruptor) 12a7240d1eSMichael Clark * 2) PLIC (Platform Level Interrupt Controller) 13af14c840SBin Meng * 3) PRCI (Power, Reset, Clock, Interrupt) 145461c4feSBin Meng * 4) OTP (One-Time Programmable) memory with stored serial number 157b6bb66fSBin Meng * 5) GEM (Gigabit Ethernet Controller) and management block 16a7240d1eSMichael Clark * 17f3d47d58SBin Meng * This board currently generates devicetree dynamically that indicates at least 18ecdfe393SBin Meng * two harts and up to five harts. 19a7240d1eSMichael Clark * 20a7240d1eSMichael Clark * This program is free software; you can redistribute it and/or modify it 21a7240d1eSMichael Clark * under the terms and conditions of the GNU General Public License, 22a7240d1eSMichael Clark * version 2 or later, as published by the Free Software Foundation. 23a7240d1eSMichael Clark * 24a7240d1eSMichael Clark * This program is distributed in the hope it will be useful, but WITHOUT 25a7240d1eSMichael Clark * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 26a7240d1eSMichael Clark * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 27a7240d1eSMichael Clark * more details. 28a7240d1eSMichael Clark * 29a7240d1eSMichael Clark * You should have received a copy of the GNU General Public License along with 30a7240d1eSMichael Clark * this program. If not, see <http://www.gnu.org/licenses/>. 31a7240d1eSMichael Clark */ 32a7240d1eSMichael Clark 33a7240d1eSMichael Clark #include "qemu/osdep.h" 34a7240d1eSMichael Clark #include "qemu/log.h" 35a7240d1eSMichael Clark #include "qemu/error-report.h" 36a7240d1eSMichael Clark #include "qapi/error.h" 37a7240d1eSMichael Clark #include "hw/boards.h" 38a7240d1eSMichael Clark #include "hw/loader.h" 39a7240d1eSMichael Clark #include "hw/sysbus.h" 40a7240d1eSMichael Clark #include "hw/char/serial.h" 41ecdfe393SBin Meng #include "hw/cpu/cluster.h" 427b6bb66fSBin Meng #include "hw/misc/unimp.h" 43a7240d1eSMichael Clark #include "target/riscv/cpu.h" 44a7240d1eSMichael Clark #include "hw/riscv/riscv_hart.h" 45a7240d1eSMichael Clark #include "hw/riscv/sifive_plic.h" 46a7240d1eSMichael Clark #include "hw/riscv/sifive_clint.h" 47a7240d1eSMichael Clark #include "hw/riscv/sifive_uart.h" 48a7240d1eSMichael Clark #include "hw/riscv/sifive_u.h" 490ac24d56SAlistair Francis #include "hw/riscv/boot.h" 50a7240d1eSMichael Clark #include "chardev/char.h" 517b6bb66fSBin Meng #include "net/eth.h" 52a7240d1eSMichael Clark #include "sysemu/arch_init.h" 53a7240d1eSMichael Clark #include "sysemu/device_tree.h" 5446517dd4SMarkus Armbruster #include "sysemu/sysemu.h" 55a7240d1eSMichael Clark #include "exec/address-spaces.h" 56a7240d1eSMichael Clark 575aec3247SMichael Clark #include <libfdt.h> 585aec3247SMichael Clark 59fdd1bda4SAlistair Francis #define BIOS_FILENAME "opensbi-riscv64-sifive_u-fw_jump.bin" 60fdd1bda4SAlistair Francis 61a7240d1eSMichael Clark static const struct MemmapEntry { 62a7240d1eSMichael Clark hwaddr base; 63a7240d1eSMichael Clark hwaddr size; 64a7240d1eSMichael Clark } sifive_u_memmap[] = { 65a7240d1eSMichael Clark [SIFIVE_U_DEBUG] = { 0x0, 0x100 }, 665aec3247SMichael Clark [SIFIVE_U_MROM] = { 0x1000, 0x11000 }, 67a7240d1eSMichael Clark [SIFIVE_U_CLINT] = { 0x2000000, 0x10000 }, 68a6902ef0SAlistair Francis [SIFIVE_U_L2LIM] = { 0x8000000, 0x2000000 }, 69a7240d1eSMichael Clark [SIFIVE_U_PLIC] = { 0xc000000, 0x4000000 }, 70af14c840SBin Meng [SIFIVE_U_PRCI] = { 0x10000000, 0x1000 }, 714b55bc2bSBin Meng [SIFIVE_U_UART0] = { 0x10010000, 0x1000 }, 724b55bc2bSBin Meng [SIFIVE_U_UART1] = { 0x10011000, 0x1000 }, 735461c4feSBin Meng [SIFIVE_U_OTP] = { 0x10070000, 0x1000 }, 74*1b3a2308SAlistair Francis [SIFIVE_U_FLASH0] = { 0x20000000, 0x10000000 }, 75a7240d1eSMichael Clark [SIFIVE_U_DRAM] = { 0x80000000, 0x0 }, 767b6bb66fSBin Meng [SIFIVE_U_GEM] = { 0x10090000, 0x2000 }, 777b6bb66fSBin Meng [SIFIVE_U_GEM_MGMT] = { 0x100a0000, 0x1000 }, 78a7240d1eSMichael Clark }; 79a7240d1eSMichael Clark 805461c4feSBin Meng #define OTP_SERIAL 1 815a7f76a3SAlistair Francis #define GEM_REVISION 0x10070109 825a7f76a3SAlistair Francis 839f79638eSBin Meng static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, 84a7240d1eSMichael Clark uint64_t mem_size, const char *cmdline) 85a7240d1eSMichael Clark { 86ecdfe393SBin Meng MachineState *ms = MACHINE(qdev_get_machine()); 87a7240d1eSMichael Clark void *fdt; 88a7240d1eSMichael Clark int cpu; 89a7240d1eSMichael Clark uint32_t *cells; 90a7240d1eSMichael Clark char *nodename; 91806c64b7SBin Meng char ethclk_names[] = "pclk\0hclk"; 9281e94379SBin Meng uint32_t plic_phandle, prci_phandle, phandle = 1; 937b6bb66fSBin Meng uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle; 94a7240d1eSMichael Clark 95a7240d1eSMichael Clark fdt = s->fdt = create_device_tree(&s->fdt_size); 96a7240d1eSMichael Clark if (!fdt) { 97a7240d1eSMichael Clark error_report("create_device_tree() failed"); 98a7240d1eSMichael Clark exit(1); 99a7240d1eSMichael Clark } 100a7240d1eSMichael Clark 101d372e748SBin Meng qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00"); 102d372e748SBin Meng qemu_fdt_setprop_string(fdt, "/", "compatible", 103d372e748SBin Meng "sifive,hifive-unleashed-a00"); 104a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 105a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 106a7240d1eSMichael Clark 107a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/soc"); 108a7240d1eSMichael Clark qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 1092a1a6f6dSAlistair Francis qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); 110a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 111a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 112a7240d1eSMichael Clark 113e1724d09SBin Meng hfclk_phandle = phandle++; 114e1724d09SBin Meng nodename = g_strdup_printf("/hfclk"); 115e1724d09SBin Meng qemu_fdt_add_subnode(fdt, nodename); 116e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle); 117e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk"); 118e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 119e1724d09SBin Meng SIFIVE_U_HFCLK_FREQ); 120e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); 121e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); 122e1724d09SBin Meng g_free(nodename); 123e1724d09SBin Meng 124e1724d09SBin Meng rtcclk_phandle = phandle++; 125e1724d09SBin Meng nodename = g_strdup_printf("/rtcclk"); 126e1724d09SBin Meng qemu_fdt_add_subnode(fdt, nodename); 127e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle); 128e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk"); 129e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 130e1724d09SBin Meng SIFIVE_U_RTCCLK_FREQ); 131e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); 132e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); 133e1724d09SBin Meng g_free(nodename); 134e1724d09SBin Meng 135a7240d1eSMichael Clark nodename = g_strdup_printf("/memory@%lx", 136a7240d1eSMichael Clark (long)memmap[SIFIVE_U_DRAM].base); 137a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 138a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 139a7240d1eSMichael Clark memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base, 140a7240d1eSMichael Clark mem_size >> 32, mem_size); 141a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 142a7240d1eSMichael Clark g_free(nodename); 143a7240d1eSMichael Clark 144a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/cpus"); 1452a8756edSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 1462a8756edSMichael Clark SIFIVE_CLINT_TIMEBASE_FREQ); 147a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 148a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 149a7240d1eSMichael Clark 150ecdfe393SBin Meng for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) { 151382cb439SBin Meng int cpu_phandle = phandle++; 152a7240d1eSMichael Clark nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 153a7240d1eSMichael Clark char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 154ecdfe393SBin Meng char *isa; 155a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 156ecdfe393SBin Meng /* cpu 0 is the management hart that does not have mmu */ 157ecdfe393SBin Meng if (cpu != 0) { 158a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 159ecdfe393SBin Meng isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]); 160ecdfe393SBin Meng } else { 161ecdfe393SBin Meng isa = riscv_isa_string(&s->soc.e_cpus.harts[0]); 162ecdfe393SBin Meng } 163a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 164a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 165a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 166a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 167a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 168a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, intc); 169382cb439SBin Meng qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); 170a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 171a7240d1eSMichael Clark qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 172a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 173a7240d1eSMichael Clark g_free(isa); 174a7240d1eSMichael Clark g_free(intc); 175a7240d1eSMichael Clark g_free(nodename); 176a7240d1eSMichael Clark } 177a7240d1eSMichael Clark 178ecdfe393SBin Meng cells = g_new0(uint32_t, ms->smp.cpus * 4); 179ecdfe393SBin Meng for (cpu = 0; cpu < ms->smp.cpus; cpu++) { 180a7240d1eSMichael Clark nodename = 181a7240d1eSMichael Clark g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 182a7240d1eSMichael Clark uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 183a7240d1eSMichael Clark cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 184a7240d1eSMichael Clark cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 185a7240d1eSMichael Clark cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 186a7240d1eSMichael Clark cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 187a7240d1eSMichael Clark g_free(nodename); 188a7240d1eSMichael Clark } 189a7240d1eSMichael Clark nodename = g_strdup_printf("/soc/clint@%lx", 190a7240d1eSMichael Clark (long)memmap[SIFIVE_U_CLINT].base); 191a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 192a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 193a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 194a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_CLINT].base, 195a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_CLINT].size); 196a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 197ecdfe393SBin Meng cells, ms->smp.cpus * sizeof(uint32_t) * 4); 198a7240d1eSMichael Clark g_free(cells); 199a7240d1eSMichael Clark g_free(nodename); 200a7240d1eSMichael Clark 201af14c840SBin Meng prci_phandle = phandle++; 202af14c840SBin Meng nodename = g_strdup_printf("/soc/clock-controller@%lx", 203af14c840SBin Meng (long)memmap[SIFIVE_U_PRCI].base); 204af14c840SBin Meng qemu_fdt_add_subnode(fdt, nodename); 205af14c840SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle); 206af14c840SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1); 207af14c840SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 208af14c840SBin Meng hfclk_phandle, rtcclk_phandle); 209af14c840SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 210af14c840SBin Meng 0x0, memmap[SIFIVE_U_PRCI].base, 211af14c840SBin Meng 0x0, memmap[SIFIVE_U_PRCI].size); 212af14c840SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 213af14c840SBin Meng "sifive,fu540-c000-prci"); 214af14c840SBin Meng g_free(nodename); 215af14c840SBin Meng 216382cb439SBin Meng plic_phandle = phandle++; 217ecdfe393SBin Meng cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2); 218ecdfe393SBin Meng for (cpu = 0; cpu < ms->smp.cpus; cpu++) { 219a7240d1eSMichael Clark nodename = 220a7240d1eSMichael Clark g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 221a7240d1eSMichael Clark uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 222ecdfe393SBin Meng /* cpu 0 is the management hart that does not have S-mode */ 223ecdfe393SBin Meng if (cpu == 0) { 224ecdfe393SBin Meng cells[0] = cpu_to_be32(intc_phandle); 225ecdfe393SBin Meng cells[1] = cpu_to_be32(IRQ_M_EXT); 226ecdfe393SBin Meng } else { 227ecdfe393SBin Meng cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle); 228ecdfe393SBin Meng cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT); 229a7240d1eSMichael Clark cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 230ecdfe393SBin Meng cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT); 231ecdfe393SBin Meng } 232a7240d1eSMichael Clark g_free(nodename); 233a7240d1eSMichael Clark } 234a7240d1eSMichael Clark nodename = g_strdup_printf("/soc/interrupt-controller@%lx", 235a7240d1eSMichael Clark (long)memmap[SIFIVE_U_PLIC].base); 236a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 237a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); 238a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); 239a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 240a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 241ecdfe393SBin Meng cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t)); 242a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 243a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_PLIC].base, 244a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_PLIC].size); 24598ceee7fSAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); 24604e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle); 247a7240d1eSMichael Clark plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 248a7240d1eSMichael Clark g_free(cells); 249a7240d1eSMichael Clark g_free(nodename); 250a7240d1eSMichael Clark 2517b6bb66fSBin Meng phy_phandle = phandle++; 2525a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx", 2535a7f76a3SAlistair Francis (long)memmap[SIFIVE_U_GEM].base); 2545a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 2557b6bb66fSBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 2567b6bb66fSBin Meng "sifive,fu540-c000-gem"); 2575a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 2585a7f76a3SAlistair Francis 0x0, memmap[SIFIVE_U_GEM].base, 2597b6bb66fSBin Meng 0x0, memmap[SIFIVE_U_GEM].size, 2607b6bb66fSBin Meng 0x0, memmap[SIFIVE_U_GEM_MGMT].base, 2617b6bb66fSBin Meng 0x0, memmap[SIFIVE_U_GEM_MGMT].size); 2625a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 2635a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); 2647b6bb66fSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle); 26504e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 26604e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); 267fe93582cSAnup Patel qemu_fdt_setprop_cells(fdt, nodename, "clocks", 268806c64b7SBin Meng prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL); 26904ece4f8SGuenter Roeck qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names, 270fe93582cSAnup Patel sizeof(ethclk_names)); 2717b6bb66fSBin Meng qemu_fdt_setprop(fdt, nodename, "local-mac-address", 2727b6bb66fSBin Meng s->soc.gem.conf.macaddr.a, ETH_ALEN); 27304e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); 27404e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); 275c3a28b5dSBin Meng 276c3a28b5dSBin Meng qemu_fdt_add_subnode(fdt, "/aliases"); 277c3a28b5dSBin Meng qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename); 278c3a28b5dSBin Meng 2795a7f76a3SAlistair Francis g_free(nodename); 2805a7f76a3SAlistair Francis 2815a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", 2825a7f76a3SAlistair Francis (long)memmap[SIFIVE_U_GEM].base); 2835a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 2847b6bb66fSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle); 28504e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0); 2865a7f76a3SAlistair Francis g_free(nodename); 2875a7f76a3SAlistair Francis 2885f7134d3SBin Meng nodename = g_strdup_printf("/soc/serial@%lx", 289a7240d1eSMichael Clark (long)memmap[SIFIVE_U_UART0].base); 290a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 291a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 292a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 293a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_UART0].base, 294a7240d1eSMichael Clark 0x0, memmap[SIFIVE_U_UART0].size); 295806c64b7SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 296806c64b7SBin Meng prci_phandle, PRCI_CLK_TLCLK); 29704e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 29804e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); 299a7240d1eSMichael Clark 300a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/chosen"); 301a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 3027c28f4daSMichael Clark if (cmdline) { 303a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 3047c28f4daSMichael Clark } 30544e6dcd3SGuenter Roeck 30644e6dcd3SGuenter Roeck qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename); 30744e6dcd3SGuenter Roeck 308a7240d1eSMichael Clark g_free(nodename); 309a7240d1eSMichael Clark } 310a7240d1eSMichael Clark 311a7240d1eSMichael Clark static void riscv_sifive_u_init(MachineState *machine) 312a7240d1eSMichael Clark { 313a7240d1eSMichael Clark const struct MemmapEntry *memmap = sifive_u_memmap; 314a7240d1eSMichael Clark 315a7240d1eSMichael Clark SiFiveUState *s = g_new0(SiFiveUState, 1); 3165aec3247SMichael Clark MemoryRegion *system_memory = get_system_memory(); 317a7240d1eSMichael Clark MemoryRegion *main_mem = g_new(MemoryRegion, 1); 318*1b3a2308SAlistair Francis MemoryRegion *flash0 = g_new(MemoryRegion, 1); 3195aec3247SMichael Clark int i; 320a7240d1eSMichael Clark 3212308092bSAlistair Francis /* Initialize SoC */ 3224eea9d7dSAlistair Francis object_initialize_child(OBJECT(machine), "soc", &s->soc, 3234eea9d7dSAlistair Francis sizeof(s->soc), TYPE_RISCV_U_SOC, 3244eea9d7dSAlistair Francis &error_abort, NULL); 325a7240d1eSMichael Clark object_property_set_bool(OBJECT(&s->soc), true, "realized", 326a7240d1eSMichael Clark &error_abort); 327a7240d1eSMichael Clark 328a7240d1eSMichael Clark /* register RAM */ 329a7240d1eSMichael Clark memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", 330a7240d1eSMichael Clark machine->ram_size, &error_fatal); 3315aec3247SMichael Clark memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base, 332a7240d1eSMichael Clark main_mem); 333a7240d1eSMichael Clark 334*1b3a2308SAlistair Francis /* register QSPI0 Flash */ 335*1b3a2308SAlistair Francis memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0", 336*1b3a2308SAlistair Francis memmap[SIFIVE_U_FLASH0].size, &error_fatal); 337*1b3a2308SAlistair Francis memory_region_add_subregion(system_memory, memmap[SIFIVE_U_FLASH0].base, 338*1b3a2308SAlistair Francis flash0); 339*1b3a2308SAlistair Francis 340a7240d1eSMichael Clark /* create device tree */ 3419f79638eSBin Meng create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 342a7240d1eSMichael Clark 343fdd1bda4SAlistair Francis riscv_find_and_load_firmware(machine, BIOS_FILENAME, 344fdd1bda4SAlistair Francis memmap[SIFIVE_U_DRAM].base); 345b3042223SAlistair Francis 346a7240d1eSMichael Clark if (machine->kernel_filename) { 3470f8d4462SGuenter Roeck uint64_t kernel_entry = riscv_load_kernel(machine->kernel_filename); 3480f8d4462SGuenter Roeck 3490f8d4462SGuenter Roeck if (machine->initrd_filename) { 3500f8d4462SGuenter Roeck hwaddr start; 3510f8d4462SGuenter Roeck hwaddr end = riscv_load_initrd(machine->initrd_filename, 3520f8d4462SGuenter Roeck machine->ram_size, kernel_entry, 3530f8d4462SGuenter Roeck &start); 3549f79638eSBin Meng qemu_fdt_setprop_cell(s->fdt, "/chosen", 3550f8d4462SGuenter Roeck "linux,initrd-start", start); 3569f79638eSBin Meng qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", 3570f8d4462SGuenter Roeck end); 3580f8d4462SGuenter Roeck } 359a7240d1eSMichael Clark } 360a7240d1eSMichael Clark 361a7240d1eSMichael Clark /* reset vector */ 362a7240d1eSMichael Clark uint32_t reset_vec[8] = { 363a7240d1eSMichael Clark 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 364a7240d1eSMichael Clark 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 365a7240d1eSMichael Clark 0xf1402573, /* csrr a0, mhartid */ 366a7240d1eSMichael Clark #if defined(TARGET_RISCV32) 367a7240d1eSMichael Clark 0x0182a283, /* lw t0, 24(t0) */ 368a7240d1eSMichael Clark #elif defined(TARGET_RISCV64) 369a7240d1eSMichael Clark 0x0182b283, /* ld t0, 24(t0) */ 370a7240d1eSMichael Clark #endif 371a7240d1eSMichael Clark 0x00028067, /* jr t0 */ 372a7240d1eSMichael Clark 0x00000000, 373a7240d1eSMichael Clark memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */ 374a7240d1eSMichael Clark 0x00000000, 375a7240d1eSMichael Clark /* dtb: */ 376a7240d1eSMichael Clark }; 377a7240d1eSMichael Clark 3785aec3247SMichael Clark /* copy in the reset vector in little_endian byte order */ 3795aec3247SMichael Clark for (i = 0; i < sizeof(reset_vec) >> 2; i++) { 3805aec3247SMichael Clark reset_vec[i] = cpu_to_le32(reset_vec[i]); 3815aec3247SMichael Clark } 3825aec3247SMichael Clark rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), 3835aec3247SMichael Clark memmap[SIFIVE_U_MROM].base, &address_space_memory); 384a7240d1eSMichael Clark 385a7240d1eSMichael Clark /* copy in the device tree */ 3865aec3247SMichael Clark if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) > 3875aec3247SMichael Clark memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) { 3885aec3247SMichael Clark error_report("not enough space to store device-tree"); 3895aec3247SMichael Clark exit(1); 3905aec3247SMichael Clark } 3915aec3247SMichael Clark qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt)); 3925aec3247SMichael Clark rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt), 3935aec3247SMichael Clark memmap[SIFIVE_U_MROM].base + sizeof(reset_vec), 3945aec3247SMichael Clark &address_space_memory); 3952308092bSAlistair Francis } 3962308092bSAlistair Francis 3972308092bSAlistair Francis static void riscv_sifive_u_soc_init(Object *obj) 3982308092bSAlistair Francis { 399c4473127SLike Xu MachineState *ms = MACHINE(qdev_get_machine()); 4002308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(obj); 4012308092bSAlistair Francis 402ecdfe393SBin Meng object_initialize_child(obj, "e-cluster", &s->e_cluster, 403ecdfe393SBin Meng sizeof(s->e_cluster), TYPE_CPU_CLUSTER, 404ecdfe393SBin Meng &error_abort, NULL); 405ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); 406ecdfe393SBin Meng 407ecdfe393SBin Meng object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", 408ecdfe393SBin Meng &s->e_cpus, sizeof(s->e_cpus), 409ecdfe393SBin Meng TYPE_RISCV_HART_ARRAY, &error_abort, 410ecdfe393SBin Meng NULL); 411ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); 412ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); 413ecdfe393SBin Meng qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); 414ecdfe393SBin Meng 415ecdfe393SBin Meng object_initialize_child(obj, "u-cluster", &s->u_cluster, 416ecdfe393SBin Meng sizeof(s->u_cluster), TYPE_CPU_CLUSTER, 417ecdfe393SBin Meng &error_abort, NULL); 418ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); 419ecdfe393SBin Meng 420ecdfe393SBin Meng object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", 421ecdfe393SBin Meng &s->u_cpus, sizeof(s->u_cpus), 422ecdfe393SBin Meng TYPE_RISCV_HART_ARRAY, &error_abort, 423ecdfe393SBin Meng NULL); 424ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); 425ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); 426ecdfe393SBin Meng qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", SIFIVE_U_CPU); 4275a7f76a3SAlistair Francis 428af14c840SBin Meng sysbus_init_child_obj(obj, "prci", &s->prci, sizeof(s->prci), 429af14c840SBin Meng TYPE_SIFIVE_U_PRCI); 4305461c4feSBin Meng sysbus_init_child_obj(obj, "otp", &s->otp, sizeof(s->otp), 4315461c4feSBin Meng TYPE_SIFIVE_U_OTP); 4325461c4feSBin Meng qdev_prop_set_uint32(DEVICE(&s->otp), "serial", OTP_SERIAL); 4334eea9d7dSAlistair Francis sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem), 4344eea9d7dSAlistair Francis TYPE_CADENCE_GEM); 4352308092bSAlistair Francis } 4362308092bSAlistair Francis 4372308092bSAlistair Francis static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp) 4382308092bSAlistair Francis { 439c4473127SLike Xu MachineState *ms = MACHINE(qdev_get_machine()); 4402308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(dev); 4412308092bSAlistair Francis const struct MemmapEntry *memmap = sifive_u_memmap; 4422308092bSAlistair Francis MemoryRegion *system_memory = get_system_memory(); 4432308092bSAlistair Francis MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 444a6902ef0SAlistair Francis MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1); 4455a7f76a3SAlistair Francis qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES]; 44605446f41SBin Meng char *plic_hart_config; 44705446f41SBin Meng size_t plic_hart_config_len; 4485a7f76a3SAlistair Francis int i; 4495a7f76a3SAlistair Francis Error *err = NULL; 4505a7f76a3SAlistair Francis NICInfo *nd = &nd_table[0]; 4512308092bSAlistair Francis 452ecdfe393SBin Meng object_property_set_bool(OBJECT(&s->e_cpus), true, "realized", 453ecdfe393SBin Meng &error_abort); 454ecdfe393SBin Meng object_property_set_bool(OBJECT(&s->u_cpus), true, "realized", 455ecdfe393SBin Meng &error_abort); 456ecdfe393SBin Meng /* 457ecdfe393SBin Meng * The cluster must be realized after the RISC-V hart array container, 458ecdfe393SBin Meng * as the container's CPU object is only created on realize, and the 459ecdfe393SBin Meng * CPU must exist and have been parented into the cluster before the 460ecdfe393SBin Meng * cluster is realized. 461ecdfe393SBin Meng */ 462ecdfe393SBin Meng object_property_set_bool(OBJECT(&s->e_cluster), true, "realized", 463ecdfe393SBin Meng &error_abort); 464ecdfe393SBin Meng object_property_set_bool(OBJECT(&s->u_cluster), true, "realized", 4652308092bSAlistair Francis &error_abort); 4662308092bSAlistair Francis 4672308092bSAlistair Francis /* boot rom */ 4682308092bSAlistair Francis memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom", 4692308092bSAlistair Francis memmap[SIFIVE_U_MROM].size, &error_fatal); 4702308092bSAlistair Francis memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base, 4712308092bSAlistair Francis mask_rom); 472a7240d1eSMichael Clark 473a6902ef0SAlistair Francis /* 474a6902ef0SAlistair Francis * Add L2-LIM at reset size. 475a6902ef0SAlistair Francis * This should be reduced in size as the L2 Cache Controller WayEnable 476a6902ef0SAlistair Francis * register is incremented. Unfortunately I don't see a nice (or any) way 477a6902ef0SAlistair Francis * to handle reducing or blocking out the L2 LIM while still allowing it 478a6902ef0SAlistair Francis * be re returned to all enabled after a reset. For the time being, just 479a6902ef0SAlistair Francis * leave it enabled all the time. This won't break anything, but will be 480a6902ef0SAlistair Francis * too generous to misbehaving guests. 481a6902ef0SAlistair Francis */ 482a6902ef0SAlistair Francis memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim", 483a6902ef0SAlistair Francis memmap[SIFIVE_U_L2LIM].size, &error_fatal); 484a6902ef0SAlistair Francis memory_region_add_subregion(system_memory, memmap[SIFIVE_U_L2LIM].base, 485a6902ef0SAlistair Francis l2lim_mem); 486a6902ef0SAlistair Francis 48705446f41SBin Meng /* create PLIC hart topology configuration string */ 488c4473127SLike Xu plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * 489c4473127SLike Xu ms->smp.cpus; 49005446f41SBin Meng plic_hart_config = g_malloc0(plic_hart_config_len); 491c4473127SLike Xu for (i = 0; i < ms->smp.cpus; i++) { 49205446f41SBin Meng if (i != 0) { 493ef965ce2SBin Meng strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG, 49405446f41SBin Meng plic_hart_config_len); 495ef965ce2SBin Meng } else { 496ef965ce2SBin Meng strncat(plic_hart_config, "M", plic_hart_config_len); 497ef965ce2SBin Meng } 49805446f41SBin Meng plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); 49905446f41SBin Meng } 50005446f41SBin Meng 501a7240d1eSMichael Clark /* MMIO */ 502a7240d1eSMichael Clark s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, 50305446f41SBin Meng plic_hart_config, 504a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_SOURCES, 505a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_PRIORITIES, 506a7240d1eSMichael Clark SIFIVE_U_PLIC_PRIORITY_BASE, 507a7240d1eSMichael Clark SIFIVE_U_PLIC_PENDING_BASE, 508a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_BASE, 509a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_STRIDE, 510a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_BASE, 511a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_STRIDE, 512a7240d1eSMichael Clark memmap[SIFIVE_U_PLIC].size); 5135aec3247SMichael Clark sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base, 514647a70a1SAlistair Francis serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); 515194eef09SMichael Clark sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base, 516194eef09SMichael Clark serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); 517a7240d1eSMichael Clark sifive_clint_create(memmap[SIFIVE_U_CLINT].base, 518c4473127SLike Xu memmap[SIFIVE_U_CLINT].size, ms->smp.cpus, 519a7240d1eSMichael Clark SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 5205a7f76a3SAlistair Francis 521af14c840SBin Meng object_property_set_bool(OBJECT(&s->prci), true, "realized", &err); 522af14c840SBin Meng sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_PRCI].base); 523af14c840SBin Meng 5245461c4feSBin Meng object_property_set_bool(OBJECT(&s->otp), true, "realized", &err); 5255461c4feSBin Meng sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_OTP].base); 5265461c4feSBin Meng 5275a7f76a3SAlistair Francis for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) { 5285a7f76a3SAlistair Francis plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i); 5295a7f76a3SAlistair Francis } 5305a7f76a3SAlistair Francis 5315a7f76a3SAlistair Francis if (nd->used) { 5325a7f76a3SAlistair Francis qemu_check_nic_model(nd, TYPE_CADENCE_GEM); 5335a7f76a3SAlistair Francis qdev_set_nic_properties(DEVICE(&s->gem), nd); 5345a7f76a3SAlistair Francis } 5355a7f76a3SAlistair Francis object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision", 5365a7f76a3SAlistair Francis &error_abort); 5375a7f76a3SAlistair Francis object_property_set_bool(OBJECT(&s->gem), true, "realized", &err); 5385a7f76a3SAlistair Francis if (err) { 5395a7f76a3SAlistair Francis error_propagate(errp, err); 5405a7f76a3SAlistair Francis return; 5415a7f76a3SAlistair Francis } 5425a7f76a3SAlistair Francis sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base); 5435a7f76a3SAlistair Francis sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, 5445a7f76a3SAlistair Francis plic_gpios[SIFIVE_U_GEM_IRQ]); 5457b6bb66fSBin Meng 5467b6bb66fSBin Meng create_unimplemented_device("riscv.sifive.u.gem-mgmt", 5477b6bb66fSBin Meng memmap[SIFIVE_U_GEM_MGMT].base, memmap[SIFIVE_U_GEM_MGMT].size); 548a7240d1eSMichael Clark } 549a7240d1eSMichael Clark 550a7240d1eSMichael Clark static void riscv_sifive_u_machine_init(MachineClass *mc) 551a7240d1eSMichael Clark { 552a7240d1eSMichael Clark mc->desc = "RISC-V Board compatible with SiFive U SDK"; 553a7240d1eSMichael Clark mc->init = riscv_sifive_u_init; 554ecdfe393SBin Meng mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; 555f3d47d58SBin Meng mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; 556f3d47d58SBin Meng mc->default_cpus = mc->min_cpus; 557a7240d1eSMichael Clark } 558a7240d1eSMichael Clark 559a7240d1eSMichael Clark DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init) 5602308092bSAlistair Francis 5612308092bSAlistair Francis static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data) 5622308092bSAlistair Francis { 5632308092bSAlistair Francis DeviceClass *dc = DEVICE_CLASS(oc); 5642308092bSAlistair Francis 5652308092bSAlistair Francis dc->realize = riscv_sifive_u_soc_realize; 5662308092bSAlistair Francis /* Reason: Uses serial_hds in realize function, thus can't be used twice */ 5672308092bSAlistair Francis dc->user_creatable = false; 5682308092bSAlistair Francis } 5692308092bSAlistair Francis 5702308092bSAlistair Francis static const TypeInfo riscv_sifive_u_soc_type_info = { 5712308092bSAlistair Francis .name = TYPE_RISCV_U_SOC, 5722308092bSAlistair Francis .parent = TYPE_DEVICE, 5732308092bSAlistair Francis .instance_size = sizeof(SiFiveUSoCState), 5742308092bSAlistair Francis .instance_init = riscv_sifive_u_soc_init, 5752308092bSAlistair Francis .class_init = riscv_sifive_u_soc_class_init, 5762308092bSAlistair Francis }; 5772308092bSAlistair Francis 5782308092bSAlistair Francis static void riscv_sifive_u_soc_register_types(void) 5792308092bSAlistair Francis { 5802308092bSAlistair Francis type_register_static(&riscv_sifive_u_soc_type_info); 5812308092bSAlistair Francis } 5822308092bSAlistair Francis 5832308092bSAlistair Francis type_init(riscv_sifive_u_soc_register_types) 584