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