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 qdev_realize(DEVICE(&s->soc), NULL, &error_abort); 335 336 /* register RAM */ 337 memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", 338 machine->ram_size, &error_fatal); 339 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base, 340 main_mem); 341 342 /* register QSPI0 Flash */ 343 memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0", 344 memmap[SIFIVE_U_FLASH0].size, &error_fatal); 345 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_FLASH0].base, 346 flash0); 347 348 /* create device tree */ 349 create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 350 351 riscv_find_and_load_firmware(machine, BIOS_FILENAME, 352 memmap[SIFIVE_U_DRAM].base, NULL); 353 354 if (machine->kernel_filename) { 355 uint64_t kernel_entry = riscv_load_kernel(machine->kernel_filename, 356 NULL); 357 358 if (machine->initrd_filename) { 359 hwaddr start; 360 hwaddr end = riscv_load_initrd(machine->initrd_filename, 361 machine->ram_size, kernel_entry, 362 &start); 363 qemu_fdt_setprop_cell(s->fdt, "/chosen", 364 "linux,initrd-start", start); 365 qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", 366 end); 367 } 368 } 369 370 if (s->start_in_flash) { 371 start_addr = memmap[SIFIVE_U_FLASH0].base; 372 } 373 374 /* reset vector */ 375 uint32_t reset_vec[8] = { 376 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 377 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 378 0xf1402573, /* csrr a0, mhartid */ 379 #if defined(TARGET_RISCV32) 380 0x0182a283, /* lw t0, 24(t0) */ 381 #elif defined(TARGET_RISCV64) 382 0x0182b283, /* ld t0, 24(t0) */ 383 #endif 384 0x00028067, /* jr t0 */ 385 0x00000000, 386 start_addr, /* start: .dword */ 387 0x00000000, 388 /* dtb: */ 389 }; 390 391 /* copy in the reset vector in little_endian byte order */ 392 for (i = 0; i < sizeof(reset_vec) >> 2; i++) { 393 reset_vec[i] = cpu_to_le32(reset_vec[i]); 394 } 395 rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), 396 memmap[SIFIVE_U_MROM].base, &address_space_memory); 397 398 /* copy in the device tree */ 399 if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) > 400 memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) { 401 error_report("not enough space to store device-tree"); 402 exit(1); 403 } 404 qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt)); 405 rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt), 406 memmap[SIFIVE_U_MROM].base + sizeof(reset_vec), 407 &address_space_memory); 408 } 409 410 static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp) 411 { 412 SiFiveUState *s = RISCV_U_MACHINE(obj); 413 414 return s->start_in_flash; 415 } 416 417 static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp) 418 { 419 SiFiveUState *s = RISCV_U_MACHINE(obj); 420 421 s->start_in_flash = value; 422 } 423 424 static void sifive_u_machine_get_serial(Object *obj, Visitor *v, const char *name, 425 void *opaque, Error **errp) 426 { 427 visit_type_uint32(v, name, (uint32_t *)opaque, errp); 428 } 429 430 static void sifive_u_machine_set_serial(Object *obj, Visitor *v, const char *name, 431 void *opaque, Error **errp) 432 { 433 visit_type_uint32(v, name, (uint32_t *)opaque, errp); 434 } 435 436 static void sifive_u_machine_instance_init(Object *obj) 437 { 438 SiFiveUState *s = RISCV_U_MACHINE(obj); 439 440 s->start_in_flash = false; 441 object_property_add_bool(obj, "start-in-flash", 442 sifive_u_machine_get_start_in_flash, 443 sifive_u_machine_set_start_in_flash); 444 object_property_set_description(obj, "start-in-flash", 445 "Set on to tell QEMU's ROM to jump to " 446 "flash. Otherwise QEMU will jump to DRAM"); 447 448 s->serial = OTP_SERIAL; 449 object_property_add(obj, "serial", "uint32", 450 sifive_u_machine_get_serial, 451 sifive_u_machine_set_serial, NULL, &s->serial); 452 object_property_set_description(obj, "serial", "Board serial number"); 453 } 454 455 static void sifive_u_machine_class_init(ObjectClass *oc, void *data) 456 { 457 MachineClass *mc = MACHINE_CLASS(oc); 458 459 mc->desc = "RISC-V Board compatible with SiFive U SDK"; 460 mc->init = sifive_u_machine_init; 461 mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; 462 mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; 463 mc->default_cpus = mc->min_cpus; 464 } 465 466 static const TypeInfo sifive_u_machine_typeinfo = { 467 .name = MACHINE_TYPE_NAME("sifive_u"), 468 .parent = TYPE_MACHINE, 469 .class_init = sifive_u_machine_class_init, 470 .instance_init = sifive_u_machine_instance_init, 471 .instance_size = sizeof(SiFiveUState), 472 }; 473 474 static void sifive_u_machine_init_register_types(void) 475 { 476 type_register_static(&sifive_u_machine_typeinfo); 477 } 478 479 type_init(sifive_u_machine_init_register_types) 480 481 static void sifive_u_soc_instance_init(Object *obj) 482 { 483 MachineState *ms = MACHINE(qdev_get_machine()); 484 SiFiveUSoCState *s = RISCV_U_SOC(obj); 485 486 object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER); 487 qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); 488 489 object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus, 490 TYPE_RISCV_HART_ARRAY); 491 qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); 492 qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); 493 qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); 494 495 object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER); 496 qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); 497 498 object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus, 499 TYPE_RISCV_HART_ARRAY); 500 qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); 501 qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); 502 qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", SIFIVE_U_CPU); 503 504 object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI); 505 object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP); 506 object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM); 507 } 508 509 static void sifive_u_soc_realize(DeviceState *dev, Error **errp) 510 { 511 MachineState *ms = MACHINE(qdev_get_machine()); 512 SiFiveUSoCState *s = RISCV_U_SOC(dev); 513 const struct MemmapEntry *memmap = sifive_u_memmap; 514 MemoryRegion *system_memory = get_system_memory(); 515 MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 516 MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1); 517 qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES]; 518 char *plic_hart_config; 519 size_t plic_hart_config_len; 520 int i; 521 Error *err = NULL; 522 NICInfo *nd = &nd_table[0]; 523 524 sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort); 525 sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort); 526 /* 527 * The cluster must be realized after the RISC-V hart array container, 528 * as the container's CPU object is only created on realize, and the 529 * CPU must exist and have been parented into the cluster before the 530 * cluster is realized. 531 */ 532 qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort); 533 qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort); 534 535 /* boot rom */ 536 memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom", 537 memmap[SIFIVE_U_MROM].size, &error_fatal); 538 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base, 539 mask_rom); 540 541 /* 542 * Add L2-LIM at reset size. 543 * This should be reduced in size as the L2 Cache Controller WayEnable 544 * register is incremented. Unfortunately I don't see a nice (or any) way 545 * to handle reducing or blocking out the L2 LIM while still allowing it 546 * be re returned to all enabled after a reset. For the time being, just 547 * leave it enabled all the time. This won't break anything, but will be 548 * too generous to misbehaving guests. 549 */ 550 memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim", 551 memmap[SIFIVE_U_L2LIM].size, &error_fatal); 552 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_L2LIM].base, 553 l2lim_mem); 554 555 /* create PLIC hart topology configuration string */ 556 plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * 557 ms->smp.cpus; 558 plic_hart_config = g_malloc0(plic_hart_config_len); 559 for (i = 0; i < ms->smp.cpus; i++) { 560 if (i != 0) { 561 strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG, 562 plic_hart_config_len); 563 } else { 564 strncat(plic_hart_config, "M", plic_hart_config_len); 565 } 566 plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); 567 } 568 569 /* MMIO */ 570 s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, 571 plic_hart_config, 572 SIFIVE_U_PLIC_NUM_SOURCES, 573 SIFIVE_U_PLIC_NUM_PRIORITIES, 574 SIFIVE_U_PLIC_PRIORITY_BASE, 575 SIFIVE_U_PLIC_PENDING_BASE, 576 SIFIVE_U_PLIC_ENABLE_BASE, 577 SIFIVE_U_PLIC_ENABLE_STRIDE, 578 SIFIVE_U_PLIC_CONTEXT_BASE, 579 SIFIVE_U_PLIC_CONTEXT_STRIDE, 580 memmap[SIFIVE_U_PLIC].size); 581 g_free(plic_hart_config); 582 sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base, 583 serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); 584 sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base, 585 serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); 586 sifive_clint_create(memmap[SIFIVE_U_CLINT].base, 587 memmap[SIFIVE_U_CLINT].size, ms->smp.cpus, 588 SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE, false); 589 590 sysbus_realize(SYS_BUS_DEVICE(&s->prci), &err); 591 sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_PRCI].base); 592 593 qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial); 594 sysbus_realize(SYS_BUS_DEVICE(&s->otp), &err); 595 sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_OTP].base); 596 597 for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) { 598 plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i); 599 } 600 601 if (nd->used) { 602 qemu_check_nic_model(nd, TYPE_CADENCE_GEM); 603 qdev_set_nic_properties(DEVICE(&s->gem), nd); 604 } 605 object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision", 606 &error_abort); 607 sysbus_realize(SYS_BUS_DEVICE(&s->gem), &err); 608 if (err) { 609 error_propagate(errp, err); 610 return; 611 } 612 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base); 613 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, 614 plic_gpios[SIFIVE_U_GEM_IRQ]); 615 616 create_unimplemented_device("riscv.sifive.u.gem-mgmt", 617 memmap[SIFIVE_U_GEM_MGMT].base, memmap[SIFIVE_U_GEM_MGMT].size); 618 } 619 620 static Property sifive_u_soc_props[] = { 621 DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL), 622 DEFINE_PROP_END_OF_LIST() 623 }; 624 625 static void sifive_u_soc_class_init(ObjectClass *oc, void *data) 626 { 627 DeviceClass *dc = DEVICE_CLASS(oc); 628 629 device_class_set_props(dc, sifive_u_soc_props); 630 dc->realize = sifive_u_soc_realize; 631 /* Reason: Uses serial_hds in realize function, thus can't be used twice */ 632 dc->user_creatable = false; 633 } 634 635 static const TypeInfo sifive_u_soc_type_info = { 636 .name = TYPE_RISCV_U_SOC, 637 .parent = TYPE_DEVICE, 638 .instance_size = sizeof(SiFiveUSoCState), 639 .instance_init = sifive_u_soc_instance_init, 640 .class_init = sifive_u_soc_class_init, 641 }; 642 643 static void sifive_u_soc_register_types(void) 644 { 645 type_register_static(&sifive_u_soc_type_info); 646 } 647 648 type_init(sifive_u_soc_register_types) 649