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) 148a88b9f5SBin Meng * 4) GPIO (General Purpose Input/Output Controller) 158a88b9f5SBin Meng * 5) OTP (One-Time Programmable) memory with stored serial number 168a88b9f5SBin Meng * 6) GEM (Gigabit Ethernet Controller) and management block 17834e027aSBin Meng * 7) DMA (Direct Memory Access Controller) 18a7240d1eSMichael Clark * 19f3d47d58SBin Meng * This board currently generates devicetree dynamically that indicates at least 20ecdfe393SBin Meng * two harts and up to five harts. 21a7240d1eSMichael Clark * 22a7240d1eSMichael Clark * This program is free software; you can redistribute it and/or modify it 23a7240d1eSMichael Clark * under the terms and conditions of the GNU General Public License, 24a7240d1eSMichael Clark * version 2 or later, as published by the Free Software Foundation. 25a7240d1eSMichael Clark * 26a7240d1eSMichael Clark * This program is distributed in the hope it will be useful, but WITHOUT 27a7240d1eSMichael Clark * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 28a7240d1eSMichael Clark * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 29a7240d1eSMichael Clark * more details. 30a7240d1eSMichael Clark * 31a7240d1eSMichael Clark * You should have received a copy of the GNU General Public License along with 32a7240d1eSMichael Clark * this program. If not, see <http://www.gnu.org/licenses/>. 33a7240d1eSMichael Clark */ 34a7240d1eSMichael Clark 35a7240d1eSMichael Clark #include "qemu/osdep.h" 36a7240d1eSMichael Clark #include "qemu/log.h" 37a7240d1eSMichael Clark #include "qemu/error-report.h" 38a7240d1eSMichael Clark #include "qapi/error.h" 393ca109c3SBin Meng #include "qapi/visitor.h" 40a7240d1eSMichael Clark #include "hw/boards.h" 415133ed17SBin Meng #include "hw/irq.h" 42a7240d1eSMichael Clark #include "hw/loader.h" 43a7240d1eSMichael Clark #include "hw/sysbus.h" 44a7240d1eSMichael Clark #include "hw/char/serial.h" 45ecdfe393SBin Meng #include "hw/cpu/cluster.h" 467b6bb66fSBin Meng #include "hw/misc/unimp.h" 47a7240d1eSMichael Clark #include "target/riscv/cpu.h" 48a7240d1eSMichael Clark #include "hw/riscv/riscv_hart.h" 49a7240d1eSMichael Clark #include "hw/riscv/sifive_u.h" 500ac24d56SAlistair Francis #include "hw/riscv/boot.h" 51b609b7e3SBin Meng #include "hw/char/sifive_uart.h" 52406fafd5SBin Meng #include "hw/intc/sifive_clint.h" 5384fcf3c1SBin Meng #include "hw/intc/sifive_plic.h" 54a7240d1eSMichael Clark #include "chardev/char.h" 557b6bb66fSBin Meng #include "net/eth.h" 56a7240d1eSMichael Clark #include "sysemu/arch_init.h" 57a7240d1eSMichael Clark #include "sysemu/device_tree.h" 585133ed17SBin Meng #include "sysemu/runstate.h" 5946517dd4SMarkus Armbruster #include "sysemu/sysemu.h" 60a7240d1eSMichael Clark 615aec3247SMichael Clark #include <libfdt.h> 625aec3247SMichael Clark 63b78c3296SBin Meng #if defined(TARGET_RISCV32) 642cacd841SBin Meng # define BIOS_FILENAME "opensbi-riscv32-generic-fw_dynamic.bin" 65b78c3296SBin Meng #else 662cacd841SBin Meng # define BIOS_FILENAME "opensbi-riscv64-generic-fw_dynamic.bin" 67b78c3296SBin Meng #endif 68fdd1bda4SAlistair Francis 69a7240d1eSMichael Clark static const struct MemmapEntry { 70a7240d1eSMichael Clark hwaddr base; 71a7240d1eSMichael Clark hwaddr size; 72a7240d1eSMichael Clark } sifive_u_memmap[] = { 73*13b8c354SEduardo Habkost [SIFIVE_U_DEV_DEBUG] = { 0x0, 0x100 }, 74*13b8c354SEduardo Habkost [SIFIVE_U_DEV_MROM] = { 0x1000, 0xf000 }, 75*13b8c354SEduardo Habkost [SIFIVE_U_DEV_CLINT] = { 0x2000000, 0x10000 }, 76*13b8c354SEduardo Habkost [SIFIVE_U_DEV_L2CC] = { 0x2010000, 0x1000 }, 77*13b8c354SEduardo Habkost [SIFIVE_U_DEV_PDMA] = { 0x3000000, 0x100000 }, 78*13b8c354SEduardo Habkost [SIFIVE_U_DEV_L2LIM] = { 0x8000000, 0x2000000 }, 79*13b8c354SEduardo Habkost [SIFIVE_U_DEV_PLIC] = { 0xc000000, 0x4000000 }, 80*13b8c354SEduardo Habkost [SIFIVE_U_DEV_PRCI] = { 0x10000000, 0x1000 }, 81*13b8c354SEduardo Habkost [SIFIVE_U_DEV_UART0] = { 0x10010000, 0x1000 }, 82*13b8c354SEduardo Habkost [SIFIVE_U_DEV_UART1] = { 0x10011000, 0x1000 }, 83*13b8c354SEduardo Habkost [SIFIVE_U_DEV_GPIO] = { 0x10060000, 0x1000 }, 84*13b8c354SEduardo Habkost [SIFIVE_U_DEV_OTP] = { 0x10070000, 0x1000 }, 85*13b8c354SEduardo Habkost [SIFIVE_U_DEV_GEM] = { 0x10090000, 0x2000 }, 86*13b8c354SEduardo Habkost [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000, 0x1000 }, 87*13b8c354SEduardo Habkost [SIFIVE_U_DEV_DMC] = { 0x100b0000, 0x10000 }, 88*13b8c354SEduardo Habkost [SIFIVE_U_DEV_FLASH0] = { 0x20000000, 0x10000000 }, 89*13b8c354SEduardo Habkost [SIFIVE_U_DEV_DRAM] = { 0x80000000, 0x0 }, 90a7240d1eSMichael Clark }; 91a7240d1eSMichael Clark 925461c4feSBin Meng #define OTP_SERIAL 1 935a7f76a3SAlistair Francis #define GEM_REVISION 0x10070109 945a7f76a3SAlistair Francis 959f79638eSBin Meng static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, 96a7240d1eSMichael Clark uint64_t mem_size, const char *cmdline) 97a7240d1eSMichael Clark { 98ecdfe393SBin Meng MachineState *ms = MACHINE(qdev_get_machine()); 99a7240d1eSMichael Clark void *fdt; 100a7240d1eSMichael Clark int cpu; 101a7240d1eSMichael Clark uint32_t *cells; 102a7240d1eSMichael Clark char *nodename; 103806c64b7SBin Meng char ethclk_names[] = "pclk\0hclk"; 1045133ed17SBin Meng uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1; 1057b6bb66fSBin Meng uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle; 106a7240d1eSMichael Clark 107a7240d1eSMichael Clark fdt = s->fdt = create_device_tree(&s->fdt_size); 108a7240d1eSMichael Clark if (!fdt) { 109a7240d1eSMichael Clark error_report("create_device_tree() failed"); 110a7240d1eSMichael Clark exit(1); 111a7240d1eSMichael Clark } 112a7240d1eSMichael Clark 113d372e748SBin Meng qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00"); 114d372e748SBin Meng qemu_fdt_setprop_string(fdt, "/", "compatible", 115d372e748SBin Meng "sifive,hifive-unleashed-a00"); 116a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 117a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 118a7240d1eSMichael Clark 119a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/soc"); 120a7240d1eSMichael Clark qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 1212a1a6f6dSAlistair Francis qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); 122a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 123a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 124a7240d1eSMichael Clark 125e1724d09SBin Meng hfclk_phandle = phandle++; 126e1724d09SBin Meng nodename = g_strdup_printf("/hfclk"); 127e1724d09SBin Meng qemu_fdt_add_subnode(fdt, nodename); 128e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle); 129e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk"); 130e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 131e1724d09SBin Meng SIFIVE_U_HFCLK_FREQ); 132e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); 133e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); 134e1724d09SBin Meng g_free(nodename); 135e1724d09SBin Meng 136e1724d09SBin Meng rtcclk_phandle = phandle++; 137e1724d09SBin Meng nodename = g_strdup_printf("/rtcclk"); 138e1724d09SBin Meng qemu_fdt_add_subnode(fdt, nodename); 139e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle); 140e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk"); 141e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 142e1724d09SBin Meng SIFIVE_U_RTCCLK_FREQ); 143e1724d09SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); 144e1724d09SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); 145e1724d09SBin Meng g_free(nodename); 146e1724d09SBin Meng 147a7240d1eSMichael Clark nodename = g_strdup_printf("/memory@%lx", 148*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_DRAM].base); 149a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 150a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 151*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base, 152a7240d1eSMichael Clark mem_size >> 32, mem_size); 153a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 154a7240d1eSMichael Clark g_free(nodename); 155a7240d1eSMichael Clark 156a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/cpus"); 1572a8756edSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 1582a8756edSMichael Clark SIFIVE_CLINT_TIMEBASE_FREQ); 159a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 160a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 161a7240d1eSMichael Clark 162ecdfe393SBin Meng for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) { 163382cb439SBin Meng int cpu_phandle = phandle++; 164a7240d1eSMichael Clark nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 165a7240d1eSMichael Clark char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 166ecdfe393SBin Meng char *isa; 167a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 168ecdfe393SBin Meng /* cpu 0 is the management hart that does not have mmu */ 169ecdfe393SBin Meng if (cpu != 0) { 170e883e992SBin Meng #if defined(TARGET_RISCV32) 171e883e992SBin Meng qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32"); 172e883e992SBin Meng #else 173a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 174e883e992SBin Meng #endif 175ecdfe393SBin Meng isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]); 176ecdfe393SBin Meng } else { 177ecdfe393SBin Meng isa = riscv_isa_string(&s->soc.e_cpus.harts[0]); 178ecdfe393SBin Meng } 179a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 180a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 181a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 182a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 183a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 184a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, intc); 185382cb439SBin Meng qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); 186a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 187a7240d1eSMichael Clark qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 188a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 189a7240d1eSMichael Clark g_free(isa); 190a7240d1eSMichael Clark g_free(intc); 191a7240d1eSMichael Clark g_free(nodename); 192a7240d1eSMichael Clark } 193a7240d1eSMichael Clark 194ecdfe393SBin Meng cells = g_new0(uint32_t, ms->smp.cpus * 4); 195ecdfe393SBin Meng for (cpu = 0; cpu < ms->smp.cpus; cpu++) { 196a7240d1eSMichael Clark nodename = 197a7240d1eSMichael Clark g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 198a7240d1eSMichael Clark uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 199a7240d1eSMichael Clark cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 200a7240d1eSMichael Clark cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 201a7240d1eSMichael Clark cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 202a7240d1eSMichael Clark cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 203a7240d1eSMichael Clark g_free(nodename); 204a7240d1eSMichael Clark } 205a7240d1eSMichael Clark nodename = g_strdup_printf("/soc/clint@%lx", 206*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_CLINT].base); 207a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 208a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 209a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 210*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_CLINT].base, 211*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_CLINT].size); 212a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 213ecdfe393SBin Meng cells, ms->smp.cpus * sizeof(uint32_t) * 4); 214a7240d1eSMichael Clark g_free(cells); 215a7240d1eSMichael Clark g_free(nodename); 216a7240d1eSMichael Clark 217ea85f27dSBin Meng nodename = g_strdup_printf("/soc/otp@%lx", 218*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_OTP].base); 219ea85f27dSBin Meng qemu_fdt_add_subnode(fdt, nodename); 220ea85f27dSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE); 221ea85f27dSBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 222*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_OTP].base, 223*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_OTP].size); 224ea85f27dSBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 225ea85f27dSBin Meng "sifive,fu540-c000-otp"); 226ea85f27dSBin Meng g_free(nodename); 227ea85f27dSBin Meng 228af14c840SBin Meng prci_phandle = phandle++; 229af14c840SBin Meng nodename = g_strdup_printf("/soc/clock-controller@%lx", 230*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_PRCI].base); 231af14c840SBin Meng qemu_fdt_add_subnode(fdt, nodename); 232af14c840SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle); 233af14c840SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1); 234af14c840SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 235af14c840SBin Meng hfclk_phandle, rtcclk_phandle); 236af14c840SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 237*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PRCI].base, 238*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PRCI].size); 239af14c840SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 240af14c840SBin Meng "sifive,fu540-c000-prci"); 241af14c840SBin Meng g_free(nodename); 242af14c840SBin Meng 243382cb439SBin Meng plic_phandle = phandle++; 244ecdfe393SBin Meng cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2); 245ecdfe393SBin Meng for (cpu = 0; cpu < ms->smp.cpus; cpu++) { 246a7240d1eSMichael Clark nodename = 247a7240d1eSMichael Clark g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 248a7240d1eSMichael Clark uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 249ecdfe393SBin Meng /* cpu 0 is the management hart that does not have S-mode */ 250ecdfe393SBin Meng if (cpu == 0) { 251ecdfe393SBin Meng cells[0] = cpu_to_be32(intc_phandle); 252ecdfe393SBin Meng cells[1] = cpu_to_be32(IRQ_M_EXT); 253ecdfe393SBin Meng } else { 254ecdfe393SBin Meng cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle); 255ecdfe393SBin Meng cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT); 256a7240d1eSMichael Clark cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 257ecdfe393SBin Meng cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT); 258ecdfe393SBin Meng } 259a7240d1eSMichael Clark g_free(nodename); 260a7240d1eSMichael Clark } 261a7240d1eSMichael Clark nodename = g_strdup_printf("/soc/interrupt-controller@%lx", 262*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_PLIC].base); 263a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 264a7240d1eSMichael Clark qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); 265a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); 266a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 267a7240d1eSMichael Clark qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 268ecdfe393SBin Meng cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t)); 269a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 270*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PLIC].base, 271*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PLIC].size); 27298ceee7fSAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); 27304e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle); 274a7240d1eSMichael Clark plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 275a7240d1eSMichael Clark g_free(cells); 276a7240d1eSMichael Clark g_free(nodename); 277a7240d1eSMichael Clark 2785133ed17SBin Meng gpio_phandle = phandle++; 2798a88b9f5SBin Meng nodename = g_strdup_printf("/soc/gpio@%lx", 280*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_GPIO].base); 2818a88b9f5SBin Meng qemu_fdt_add_subnode(fdt, nodename); 2825133ed17SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle); 2838a88b9f5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 2848a88b9f5SBin Meng prci_phandle, PRCI_CLK_TLCLK); 2858a88b9f5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2); 2868a88b9f5SBin Meng qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 2878a88b9f5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2); 2888a88b9f5SBin Meng qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0); 2898a88b9f5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 290*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GPIO].base, 291*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GPIO].size); 2928a88b9f5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0, 2938a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3, 2948a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6, 2958a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9, 2968a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12, 2978a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15); 2988a88b9f5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 2998a88b9f5SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0"); 3008a88b9f5SBin Meng g_free(nodename); 3018a88b9f5SBin Meng 3025133ed17SBin Meng nodename = g_strdup_printf("/gpio-restart"); 3035133ed17SBin Meng qemu_fdt_add_subnode(fdt, nodename); 3045133ed17SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1); 3055133ed17SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart"); 3065133ed17SBin Meng g_free(nodename); 3075133ed17SBin Meng 308834e027aSBin Meng nodename = g_strdup_printf("/soc/dma@%lx", 309*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_PDMA].base); 310834e027aSBin Meng qemu_fdt_add_subnode(fdt, nodename); 311834e027aSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1); 312834e027aSBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 313834e027aSBin Meng SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2, 314834e027aSBin Meng SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5, 315834e027aSBin Meng SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7); 316834e027aSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 317834e027aSBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 318*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PDMA].base, 319*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PDMA].size); 320834e027aSBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 321834e027aSBin Meng "sifive,fu540-c000-pdma"); 322834e027aSBin Meng g_free(nodename); 323834e027aSBin Meng 3246eaf9cf5SBin Meng nodename = g_strdup_printf("/soc/cache-controller@%lx", 325*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_L2CC].base); 3266eaf9cf5SBin Meng qemu_fdt_add_subnode(fdt, nodename); 3276eaf9cf5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 328*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_L2CC].base, 329*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_L2CC].size); 3306eaf9cf5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 3316eaf9cf5SBin Meng SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2); 3326eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 3336eaf9cf5SBin Meng qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0); 3346eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152); 3356eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024); 3366eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2); 3376eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64); 3386eaf9cf5SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 3396eaf9cf5SBin Meng "sifive,fu540-c000-ccache"); 3406eaf9cf5SBin Meng g_free(nodename); 3416eaf9cf5SBin Meng 3427b6bb66fSBin Meng phy_phandle = phandle++; 3435a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx", 344*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_GEM].base); 3455a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 3467b6bb66fSBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 3477b6bb66fSBin Meng "sifive,fu540-c000-gem"); 3485a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 349*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM].base, 350*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM].size, 351*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base, 352*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size); 3535a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 3545a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); 3557b6bb66fSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle); 35604e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 35704e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); 358fe93582cSAnup Patel qemu_fdt_setprop_cells(fdt, nodename, "clocks", 359806c64b7SBin Meng prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL); 36004ece4f8SGuenter Roeck qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names, 361fe93582cSAnup Patel sizeof(ethclk_names)); 3627b6bb66fSBin Meng qemu_fdt_setprop(fdt, nodename, "local-mac-address", 3637b6bb66fSBin Meng s->soc.gem.conf.macaddr.a, ETH_ALEN); 36404e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); 36504e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); 366c3a28b5dSBin Meng 367c3a28b5dSBin Meng qemu_fdt_add_subnode(fdt, "/aliases"); 368c3a28b5dSBin Meng qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename); 369c3a28b5dSBin Meng 3705a7f76a3SAlistair Francis g_free(nodename); 3715a7f76a3SAlistair Francis 3725a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", 373*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_GEM].base); 3745a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 3757b6bb66fSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle); 37604e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0); 3775a7f76a3SAlistair Francis g_free(nodename); 3785a7f76a3SAlistair Francis 3795f7134d3SBin Meng nodename = g_strdup_printf("/soc/serial@%lx", 380*13b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_UART0].base); 381a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 382a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 383a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 384*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_UART0].base, 385*13b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_UART0].size); 386806c64b7SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 387806c64b7SBin Meng prci_phandle, PRCI_CLK_TLCLK); 38804e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 38904e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); 390a7240d1eSMichael Clark 391a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/chosen"); 392a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 3937c28f4daSMichael Clark if (cmdline) { 394a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 3957c28f4daSMichael Clark } 39644e6dcd3SGuenter Roeck 39744e6dcd3SGuenter Roeck qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename); 39844e6dcd3SGuenter Roeck 399a7240d1eSMichael Clark g_free(nodename); 400a7240d1eSMichael Clark } 401a7240d1eSMichael Clark 4025133ed17SBin Meng static void sifive_u_machine_reset(void *opaque, int n, int level) 4035133ed17SBin Meng { 4045133ed17SBin Meng /* gpio pin active low triggers reset */ 4055133ed17SBin Meng if (!level) { 4065133ed17SBin Meng qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 4075133ed17SBin Meng } 4085133ed17SBin Meng } 4095133ed17SBin Meng 410523e3464SAlistair Francis static void sifive_u_machine_init(MachineState *machine) 411a7240d1eSMichael Clark { 412a7240d1eSMichael Clark const struct MemmapEntry *memmap = sifive_u_memmap; 413687caef1SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(machine); 4145aec3247SMichael Clark MemoryRegion *system_memory = get_system_memory(); 415a7240d1eSMichael Clark MemoryRegion *main_mem = g_new(MemoryRegion, 1); 4161b3a2308SAlistair Francis MemoryRegion *flash0 = g_new(MemoryRegion, 1); 417*13b8c354SEduardo Habkost target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base; 4188590f536SAtish Patra uint32_t start_addr_hi32 = 0x00000000; 4195aec3247SMichael Clark int i; 42066b1205bSAtish Patra uint32_t fdt_load_addr; 421dc144fe1SAtish Patra uint64_t kernel_entry; 422a7240d1eSMichael Clark 4232308092bSAlistair Francis /* Initialize SoC */ 4249fc7fc4dSMarkus Armbruster object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC); 4255325cc34SMarkus Armbruster object_property_set_uint(OBJECT(&s->soc), "serial", s->serial, 4263ca109c3SBin Meng &error_abort); 427ce189ab2SMarkus Armbruster qdev_realize(DEVICE(&s->soc), NULL, &error_abort); 428a7240d1eSMichael Clark 429a7240d1eSMichael Clark /* register RAM */ 430a7240d1eSMichael Clark memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", 431a7240d1eSMichael Clark machine->ram_size, &error_fatal); 432*13b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base, 433a7240d1eSMichael Clark main_mem); 434a7240d1eSMichael Clark 4351b3a2308SAlistair Francis /* register QSPI0 Flash */ 4361b3a2308SAlistair Francis memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0", 437*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal); 438*13b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base, 4391b3a2308SAlistair Francis flash0); 4401b3a2308SAlistair Francis 4415133ed17SBin Meng /* register gpio-restart */ 4425133ed17SBin Meng qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10, 4435133ed17SBin Meng qemu_allocate_irq(sifive_u_machine_reset, NULL, 0)); 4445133ed17SBin Meng 445a7240d1eSMichael Clark /* create device tree */ 4469f79638eSBin Meng create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 447a7240d1eSMichael Clark 44817aad9f2SBin Meng if (s->start_in_flash) { 44917aad9f2SBin Meng /* 45017aad9f2SBin Meng * If start_in_flash property is given, assign s->msel to a value 45117aad9f2SBin Meng * that representing booting from QSPI0 memory-mapped flash. 45217aad9f2SBin Meng * 45317aad9f2SBin Meng * This also means that when both start_in_flash and msel properties 45417aad9f2SBin Meng * are given, start_in_flash takes the precedence over msel. 45517aad9f2SBin Meng * 45617aad9f2SBin Meng * Note this is to keep backward compatibility not to break existing 45717aad9f2SBin Meng * users that use start_in_flash property. 45817aad9f2SBin Meng */ 45917aad9f2SBin Meng s->msel = MSEL_MEMMAP_QSPI0_FLASH; 46017aad9f2SBin Meng } 46117aad9f2SBin Meng 46217aad9f2SBin Meng switch (s->msel) { 46317aad9f2SBin Meng case MSEL_MEMMAP_QSPI0_FLASH: 464*13b8c354SEduardo Habkost start_addr = memmap[SIFIVE_U_DEV_FLASH0].base; 46517aad9f2SBin Meng break; 46617aad9f2SBin Meng case MSEL_L2LIM_QSPI0_FLASH: 46717aad9f2SBin Meng case MSEL_L2LIM_QSPI2_SD: 468*13b8c354SEduardo Habkost start_addr = memmap[SIFIVE_U_DEV_L2LIM].base; 46917aad9f2SBin Meng break; 47017aad9f2SBin Meng default: 471*13b8c354SEduardo Habkost start_addr = memmap[SIFIVE_U_DEV_DRAM].base; 47217aad9f2SBin Meng break; 47317aad9f2SBin Meng } 47417aad9f2SBin Meng 47517aad9f2SBin Meng riscv_find_and_load_firmware(machine, BIOS_FILENAME, start_addr, NULL); 476b3042223SAlistair Francis 477a7240d1eSMichael Clark if (machine->kernel_filename) { 478dc144fe1SAtish Patra kernel_entry = riscv_load_kernel(machine->kernel_filename, NULL); 4790f8d4462SGuenter Roeck 4800f8d4462SGuenter Roeck if (machine->initrd_filename) { 4810f8d4462SGuenter Roeck hwaddr start; 4820f8d4462SGuenter Roeck hwaddr end = riscv_load_initrd(machine->initrd_filename, 4830f8d4462SGuenter Roeck machine->ram_size, kernel_entry, 4840f8d4462SGuenter Roeck &start); 4859f79638eSBin Meng qemu_fdt_setprop_cell(s->fdt, "/chosen", 4860f8d4462SGuenter Roeck "linux,initrd-start", start); 4879f79638eSBin Meng qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", 4880f8d4462SGuenter Roeck end); 4890f8d4462SGuenter Roeck } 490dc144fe1SAtish Patra } else { 491dc144fe1SAtish Patra /* 492dc144fe1SAtish Patra * If dynamic firmware is used, it doesn't know where is the next mode 493dc144fe1SAtish Patra * if kernel argument is not set. 494dc144fe1SAtish Patra */ 495dc144fe1SAtish Patra kernel_entry = 0; 496a7240d1eSMichael Clark } 497a7240d1eSMichael Clark 49866b1205bSAtish Patra /* Compute the fdt load address in dram */ 499*13b8c354SEduardo Habkost fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base, 50066b1205bSAtish Patra machine->ram_size, s->fdt); 5018590f536SAtish Patra #if defined(TARGET_RISCV64) 5028590f536SAtish Patra start_addr_hi32 = start_addr >> 32; 5038590f536SAtish Patra #endif 50466b1205bSAtish Patra 505a7240d1eSMichael Clark /* reset vector */ 50666b1205bSAtish Patra uint32_t reset_vec[11] = { 50717aad9f2SBin Meng s->msel, /* MSEL pin state */ 508dc144fe1SAtish Patra 0x00000297, /* 1: auipc t0, %pcrel_hi(fw_dyn) */ 509dc144fe1SAtish Patra 0x02828613, /* addi a2, t0, %pcrel_lo(1b) */ 510a7240d1eSMichael Clark 0xf1402573, /* csrr a0, mhartid */ 511a7240d1eSMichael Clark #if defined(TARGET_RISCV32) 51266b1205bSAtish Patra 0x0202a583, /* lw a1, 32(t0) */ 513a7240d1eSMichael Clark 0x0182a283, /* lw t0, 24(t0) */ 514a7240d1eSMichael Clark #elif defined(TARGET_RISCV64) 51566b1205bSAtish Patra 0x0202b583, /* ld a1, 32(t0) */ 51666b1205bSAtish Patra 0x0182b283, /* ld t0, 24(t0) */ 517a7240d1eSMichael Clark #endif 518a7240d1eSMichael Clark 0x00028067, /* jr t0 */ 519fc41ae23SAlistair Francis start_addr, /* start: .dword */ 5208590f536SAtish Patra start_addr_hi32, 52166b1205bSAtish Patra fdt_load_addr, /* fdt_laddr: .dword */ 52266b1205bSAtish Patra 0x00000000, 523dc144fe1SAtish Patra /* fw_dyn: */ 524a7240d1eSMichael Clark }; 525a7240d1eSMichael Clark 5265aec3247SMichael Clark /* copy in the reset vector in little_endian byte order */ 52766b1205bSAtish Patra for (i = 0; i < ARRAY_SIZE(reset_vec); i++) { 5285aec3247SMichael Clark reset_vec[i] = cpu_to_le32(reset_vec[i]); 5295aec3247SMichael Clark } 5305aec3247SMichael Clark rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), 531*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory); 532dc144fe1SAtish Patra 533*13b8c354SEduardo Habkost riscv_rom_copy_firmware_info(memmap[SIFIVE_U_DEV_MROM].base, 534*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_MROM].size, 535dc144fe1SAtish Patra sizeof(reset_vec), kernel_entry); 5362308092bSAlistair Francis } 5372308092bSAlistair Francis 538523e3464SAlistair Francis static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp) 539523e3464SAlistair Francis { 540523e3464SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(obj); 541523e3464SAlistair Francis 542523e3464SAlistair Francis return s->start_in_flash; 543523e3464SAlistair Francis } 544523e3464SAlistair Francis 545523e3464SAlistair Francis static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp) 546523e3464SAlistair Francis { 547523e3464SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(obj); 548523e3464SAlistair Francis 549523e3464SAlistair Francis s->start_in_flash = value; 550523e3464SAlistair Francis } 551523e3464SAlistair Francis 5523e9667cdSBin Meng static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v, 5533e9667cdSBin Meng const char *name, void *opaque, 5543e9667cdSBin Meng Error **errp) 5553ca109c3SBin Meng { 5563ca109c3SBin Meng visit_type_uint32(v, name, (uint32_t *)opaque, errp); 5573ca109c3SBin Meng } 5583ca109c3SBin Meng 5593e9667cdSBin Meng static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v, 5603e9667cdSBin Meng const char *name, void *opaque, 5613e9667cdSBin Meng Error **errp) 5623ca109c3SBin Meng { 5633ca109c3SBin Meng visit_type_uint32(v, name, (uint32_t *)opaque, errp); 5643ca109c3SBin Meng } 5653ca109c3SBin Meng 566523e3464SAlistair Francis static void sifive_u_machine_instance_init(Object *obj) 567523e3464SAlistair Francis { 568523e3464SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(obj); 569523e3464SAlistair Francis 570523e3464SAlistair Francis s->start_in_flash = false; 571d2623129SMarkus Armbruster object_property_add_bool(obj, "start-in-flash", 572d2623129SMarkus Armbruster sifive_u_machine_get_start_in_flash, 573d2623129SMarkus Armbruster sifive_u_machine_set_start_in_flash); 574523e3464SAlistair Francis object_property_set_description(obj, "start-in-flash", 575523e3464SAlistair Francis "Set on to tell QEMU's ROM to jump to " 57617aad9f2SBin Meng "flash. Otherwise QEMU will jump to DRAM " 57717aad9f2SBin Meng "or L2LIM depending on the msel value"); 5783ca109c3SBin Meng 579cfa32630SBin Meng s->msel = 0; 580cfa32630SBin Meng object_property_add(obj, "msel", "uint32", 581cfa32630SBin Meng sifive_u_machine_get_uint32_prop, 582cfa32630SBin Meng sifive_u_machine_set_uint32_prop, NULL, &s->msel); 583cfa32630SBin Meng object_property_set_description(obj, "msel", 584cfa32630SBin Meng "Mode Select (MSEL[3:0]) pin state"); 585cfa32630SBin Meng 5863ca109c3SBin Meng s->serial = OTP_SERIAL; 587d2623129SMarkus Armbruster object_property_add(obj, "serial", "uint32", 5883e9667cdSBin Meng sifive_u_machine_get_uint32_prop, 5893e9667cdSBin Meng sifive_u_machine_set_uint32_prop, NULL, &s->serial); 5907eecec7dSMarkus Armbruster object_property_set_description(obj, "serial", "Board serial number"); 591523e3464SAlistair Francis } 592523e3464SAlistair Francis 593523e3464SAlistair Francis static void sifive_u_machine_class_init(ObjectClass *oc, void *data) 594523e3464SAlistair Francis { 595523e3464SAlistair Francis MachineClass *mc = MACHINE_CLASS(oc); 596523e3464SAlistair Francis 597523e3464SAlistair Francis mc->desc = "RISC-V Board compatible with SiFive U SDK"; 598523e3464SAlistair Francis mc->init = sifive_u_machine_init; 599523e3464SAlistair Francis mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; 600523e3464SAlistair Francis mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; 601523e3464SAlistair Francis mc->default_cpus = mc->min_cpus; 602523e3464SAlistair Francis } 603523e3464SAlistair Francis 604523e3464SAlistair Francis static const TypeInfo sifive_u_machine_typeinfo = { 605523e3464SAlistair Francis .name = MACHINE_TYPE_NAME("sifive_u"), 606523e3464SAlistair Francis .parent = TYPE_MACHINE, 607523e3464SAlistair Francis .class_init = sifive_u_machine_class_init, 608523e3464SAlistair Francis .instance_init = sifive_u_machine_instance_init, 609523e3464SAlistair Francis .instance_size = sizeof(SiFiveUState), 610523e3464SAlistair Francis }; 611523e3464SAlistair Francis 612523e3464SAlistair Francis static void sifive_u_machine_init_register_types(void) 613523e3464SAlistair Francis { 614523e3464SAlistair Francis type_register_static(&sifive_u_machine_typeinfo); 615523e3464SAlistair Francis } 616523e3464SAlistair Francis 617523e3464SAlistair Francis type_init(sifive_u_machine_init_register_types) 618523e3464SAlistair Francis 619139177b1SBin Meng static void sifive_u_soc_instance_init(Object *obj) 6202308092bSAlistair Francis { 621c4473127SLike Xu MachineState *ms = MACHINE(qdev_get_machine()); 6222308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(obj); 6232308092bSAlistair Francis 6249fc7fc4dSMarkus Armbruster object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER); 625ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); 626ecdfe393SBin Meng 627db873cc5SMarkus Armbruster object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus, 62875a6ed87SMarkus Armbruster TYPE_RISCV_HART_ARRAY); 629ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); 630ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); 631ecdfe393SBin Meng qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); 63273f6ed97SBin Meng qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004); 633ecdfe393SBin Meng 6349fc7fc4dSMarkus Armbruster object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER); 635ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); 636ecdfe393SBin Meng 637db873cc5SMarkus Armbruster object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus, 63875a6ed87SMarkus Armbruster TYPE_RISCV_HART_ARRAY); 639ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); 640ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); 641ecdfe393SBin Meng qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", SIFIVE_U_CPU); 64273f6ed97SBin Meng qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004); 6435a7f76a3SAlistair Francis 644db873cc5SMarkus Armbruster object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI); 645db873cc5SMarkus Armbruster object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP); 646db873cc5SMarkus Armbruster object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM); 6478a88b9f5SBin Meng object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO); 648834e027aSBin Meng object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA); 6492308092bSAlistair Francis } 6502308092bSAlistair Francis 651139177b1SBin Meng static void sifive_u_soc_realize(DeviceState *dev, Error **errp) 6522308092bSAlistair Francis { 653c4473127SLike Xu MachineState *ms = MACHINE(qdev_get_machine()); 6542308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(dev); 6552308092bSAlistair Francis const struct MemmapEntry *memmap = sifive_u_memmap; 6562308092bSAlistair Francis MemoryRegion *system_memory = get_system_memory(); 6572308092bSAlistair Francis MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 658a6902ef0SAlistair Francis MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1); 65905446f41SBin Meng char *plic_hart_config; 66005446f41SBin Meng size_t plic_hart_config_len; 6615a7f76a3SAlistair Francis int i; 6625a7f76a3SAlistair Francis NICInfo *nd = &nd_table[0]; 6632308092bSAlistair Francis 664db873cc5SMarkus Armbruster sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort); 665db873cc5SMarkus Armbruster sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort); 666ecdfe393SBin Meng /* 667ecdfe393SBin Meng * The cluster must be realized after the RISC-V hart array container, 668ecdfe393SBin Meng * as the container's CPU object is only created on realize, and the 669ecdfe393SBin Meng * CPU must exist and have been parented into the cluster before the 670ecdfe393SBin Meng * cluster is realized. 671ecdfe393SBin Meng */ 672ce189ab2SMarkus Armbruster qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort); 673ce189ab2SMarkus Armbruster qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort); 6742308092bSAlistair Francis 6752308092bSAlistair Francis /* boot rom */ 676414c47d2SPhilippe Mathieu-Daudé memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom", 677*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_MROM].size, &error_fatal); 678*13b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base, 6792308092bSAlistair Francis mask_rom); 680a7240d1eSMichael Clark 681a6902ef0SAlistair Francis /* 682a6902ef0SAlistair Francis * Add L2-LIM at reset size. 683a6902ef0SAlistair Francis * This should be reduced in size as the L2 Cache Controller WayEnable 684a6902ef0SAlistair Francis * register is incremented. Unfortunately I don't see a nice (or any) way 685a6902ef0SAlistair Francis * to handle reducing or blocking out the L2 LIM while still allowing it 686a6902ef0SAlistair Francis * be re returned to all enabled after a reset. For the time being, just 687a6902ef0SAlistair Francis * leave it enabled all the time. This won't break anything, but will be 688a6902ef0SAlistair Francis * too generous to misbehaving guests. 689a6902ef0SAlistair Francis */ 690a6902ef0SAlistair Francis memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim", 691*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal); 692*13b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base, 693a6902ef0SAlistair Francis l2lim_mem); 694a6902ef0SAlistair Francis 69505446f41SBin Meng /* create PLIC hart topology configuration string */ 696c4473127SLike Xu plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * 697c4473127SLike Xu ms->smp.cpus; 69805446f41SBin Meng plic_hart_config = g_malloc0(plic_hart_config_len); 699c4473127SLike Xu for (i = 0; i < ms->smp.cpus; i++) { 70005446f41SBin Meng if (i != 0) { 701ef965ce2SBin Meng strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG, 70205446f41SBin Meng plic_hart_config_len); 703ef965ce2SBin Meng } else { 704ef965ce2SBin Meng strncat(plic_hart_config, "M", plic_hart_config_len); 705ef965ce2SBin Meng } 70605446f41SBin Meng plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); 70705446f41SBin Meng } 70805446f41SBin Meng 709a7240d1eSMichael Clark /* MMIO */ 710*13b8c354SEduardo Habkost s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base, 711c9270e10SAnup Patel plic_hart_config, 0, 712a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_SOURCES, 713a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_PRIORITIES, 714a7240d1eSMichael Clark SIFIVE_U_PLIC_PRIORITY_BASE, 715a7240d1eSMichael Clark SIFIVE_U_PLIC_PENDING_BASE, 716a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_BASE, 717a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_STRIDE, 718a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_BASE, 719a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_STRIDE, 720*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_PLIC].size); 721bb8136dfSPan Nengyuan g_free(plic_hart_config); 722*13b8c354SEduardo Habkost sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base, 723647a70a1SAlistair Francis serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); 724*13b8c354SEduardo Habkost sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base, 725194eef09SMichael Clark serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); 726*13b8c354SEduardo Habkost sifive_clint_create(memmap[SIFIVE_U_DEV_CLINT].base, 727*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_CLINT].size, 0, ms->smp.cpus, 728a47ef6e9SBin Meng SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE, 729a47ef6e9SBin Meng SIFIVE_CLINT_TIMEBASE_FREQ, false); 7305a7f76a3SAlistair Francis 731cbe3a8c5SMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) { 732cbe3a8c5SMarkus Armbruster return; 733cbe3a8c5SMarkus Armbruster } 734*13b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base); 735af14c840SBin Meng 7368a88b9f5SBin Meng qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16); 737cbe3a8c5SMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) { 738cbe3a8c5SMarkus Armbruster return; 739cbe3a8c5SMarkus Armbruster } 740*13b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base); 7418a88b9f5SBin Meng 7428a88b9f5SBin Meng /* Pass all GPIOs to the SOC layer so they are available to the board */ 7438a88b9f5SBin Meng qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL); 7448a88b9f5SBin Meng 7458a88b9f5SBin Meng /* Connect GPIO interrupts to the PLIC */ 7468a88b9f5SBin Meng for (i = 0; i < 16; i++) { 7478a88b9f5SBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i, 7488a88b9f5SBin Meng qdev_get_gpio_in(DEVICE(s->plic), 7498a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ0 + i)); 7508a88b9f5SBin Meng } 7518a88b9f5SBin Meng 752834e027aSBin Meng /* PDMA */ 753834e027aSBin Meng sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp); 754*13b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base); 755834e027aSBin Meng 756834e027aSBin Meng /* Connect PDMA interrupts to the PLIC */ 757834e027aSBin Meng for (i = 0; i < SIFIVE_PDMA_IRQS; i++) { 758834e027aSBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i, 759834e027aSBin Meng qdev_get_gpio_in(DEVICE(s->plic), 760834e027aSBin Meng SIFIVE_U_PDMA_IRQ0 + i)); 761834e027aSBin Meng } 762834e027aSBin Meng 763fda5b000SAlistair Francis qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial); 764cbe3a8c5SMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) { 765cbe3a8c5SMarkus Armbruster return; 766cbe3a8c5SMarkus Armbruster } 767*13b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base); 7685461c4feSBin Meng 7697ad36e2eSMarkus Armbruster /* FIXME use qdev NIC properties instead of nd_table[] */ 7705a7f76a3SAlistair Francis if (nd->used) { 7715a7f76a3SAlistair Francis qemu_check_nic_model(nd, TYPE_CADENCE_GEM); 7725a7f76a3SAlistair Francis qdev_set_nic_properties(DEVICE(&s->gem), nd); 7735a7f76a3SAlistair Francis } 7745325cc34SMarkus Armbruster object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION, 7755a7f76a3SAlistair Francis &error_abort); 776668f62ecSMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) { 7775a7f76a3SAlistair Francis return; 7785a7f76a3SAlistair Francis } 779*13b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base); 7805a7f76a3SAlistair Francis sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, 7815874f0a7SBin Meng qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ)); 7827b6bb66fSBin Meng 7837b6bb66fSBin Meng create_unimplemented_device("riscv.sifive.u.gem-mgmt", 784*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size); 7853eaea6ebSBin Meng 7863eaea6ebSBin Meng create_unimplemented_device("riscv.sifive.u.dmc", 787*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size); 7886eaf9cf5SBin Meng 7896eaf9cf5SBin Meng create_unimplemented_device("riscv.sifive.u.l2cc", 790*13b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size); 791a7240d1eSMichael Clark } 792a7240d1eSMichael Clark 793139177b1SBin Meng static Property sifive_u_soc_props[] = { 794fda5b000SAlistair Francis DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL), 795fda5b000SAlistair Francis DEFINE_PROP_END_OF_LIST() 796fda5b000SAlistair Francis }; 797fda5b000SAlistair Francis 798139177b1SBin Meng static void sifive_u_soc_class_init(ObjectClass *oc, void *data) 7992308092bSAlistair Francis { 8002308092bSAlistair Francis DeviceClass *dc = DEVICE_CLASS(oc); 8012308092bSAlistair Francis 802139177b1SBin Meng device_class_set_props(dc, sifive_u_soc_props); 803139177b1SBin Meng dc->realize = sifive_u_soc_realize; 8042308092bSAlistair Francis /* Reason: Uses serial_hds in realize function, thus can't be used twice */ 8052308092bSAlistair Francis dc->user_creatable = false; 8062308092bSAlistair Francis } 8072308092bSAlistair Francis 808139177b1SBin Meng static const TypeInfo sifive_u_soc_type_info = { 8092308092bSAlistair Francis .name = TYPE_RISCV_U_SOC, 8102308092bSAlistair Francis .parent = TYPE_DEVICE, 8112308092bSAlistair Francis .instance_size = sizeof(SiFiveUSoCState), 812139177b1SBin Meng .instance_init = sifive_u_soc_instance_init, 813139177b1SBin Meng .class_init = sifive_u_soc_class_init, 8142308092bSAlistair Francis }; 8152308092bSAlistair Francis 816139177b1SBin Meng static void sifive_u_soc_register_types(void) 8172308092bSAlistair Francis { 818139177b1SBin Meng type_register_static(&sifive_u_soc_type_info); 8192308092bSAlistair Francis } 8202308092bSAlistair Francis 821139177b1SBin Meng type_init(sifive_u_soc_register_types) 822