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 * 7 * Provides a board compatible with the SiFive Freedom U SDK: 8 * 9 * 0) UART 10 * 1) CLINT (Core Level Interruptor) 11 * 2) PLIC (Platform Level Interrupt Controller) 12 * 13 * This board currently uses a hardcoded devicetree that indicates one hart. 14 * 15 * This program is free software; you can redistribute it and/or modify it 16 * under the terms and conditions of the GNU General Public License, 17 * version 2 or later, as published by the Free Software Foundation. 18 * 19 * This program is distributed in the hope it will be useful, but WITHOUT 20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 22 * more details. 23 * 24 * You should have received a copy of the GNU General Public License along with 25 * this program. If not, see <http://www.gnu.org/licenses/>. 26 */ 27 28 #include "qemu/osdep.h" 29 #include "qemu/log.h" 30 #include "qemu/error-report.h" 31 #include "qapi/error.h" 32 #include "hw/boards.h" 33 #include "hw/loader.h" 34 #include "hw/sysbus.h" 35 #include "hw/char/serial.h" 36 #include "target/riscv/cpu.h" 37 #include "hw/riscv/riscv_hart.h" 38 #include "hw/riscv/sifive_plic.h" 39 #include "hw/riscv/sifive_clint.h" 40 #include "hw/riscv/sifive_uart.h" 41 #include "hw/riscv/sifive_prci.h" 42 #include "hw/riscv/sifive_u.h" 43 #include "hw/riscv/boot.h" 44 #include "chardev/char.h" 45 #include "sysemu/arch_init.h" 46 #include "sysemu/device_tree.h" 47 #include "exec/address-spaces.h" 48 49 #include <libfdt.h> 50 51 #define BIOS_FILENAME "opensbi-riscv64-sifive_u-fw_jump.bin" 52 53 static const struct MemmapEntry { 54 hwaddr base; 55 hwaddr size; 56 } sifive_u_memmap[] = { 57 [SIFIVE_U_DEBUG] = { 0x0, 0x100 }, 58 [SIFIVE_U_MROM] = { 0x1000, 0x11000 }, 59 [SIFIVE_U_CLINT] = { 0x2000000, 0x10000 }, 60 [SIFIVE_U_PLIC] = { 0xc000000, 0x4000000 }, 61 [SIFIVE_U_UART0] = { 0x10013000, 0x1000 }, 62 [SIFIVE_U_UART1] = { 0x10023000, 0x1000 }, 63 [SIFIVE_U_DRAM] = { 0x80000000, 0x0 }, 64 [SIFIVE_U_GEM] = { 0x100900FC, 0x2000 }, 65 }; 66 67 #define GEM_REVISION 0x10070109 68 69 static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, 70 uint64_t mem_size, const char *cmdline) 71 { 72 void *fdt; 73 int cpu; 74 uint32_t *cells; 75 char *nodename; 76 char ethclk_names[] = "pclk\0hclk\0tx_clk"; 77 uint32_t plic_phandle, ethclk_phandle, phandle = 1; 78 79 fdt = s->fdt = create_device_tree(&s->fdt_size); 80 if (!fdt) { 81 error_report("create_device_tree() failed"); 82 exit(1); 83 } 84 85 qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu"); 86 qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev"); 87 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 88 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 89 90 qemu_fdt_add_subnode(fdt, "/soc"); 91 qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 92 qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); 93 qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 94 qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 95 96 nodename = g_strdup_printf("/memory@%lx", 97 (long)memmap[SIFIVE_U_DRAM].base); 98 qemu_fdt_add_subnode(fdt, nodename); 99 qemu_fdt_setprop_cells(fdt, nodename, "reg", 100 memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base, 101 mem_size >> 32, mem_size); 102 qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 103 g_free(nodename); 104 105 qemu_fdt_add_subnode(fdt, "/cpus"); 106 qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 107 SIFIVE_CLINT_TIMEBASE_FREQ); 108 qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 109 qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 110 111 for (cpu = s->soc.cpus.num_harts - 1; cpu >= 0; cpu--) { 112 int cpu_phandle = phandle++; 113 nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 114 char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 115 char *isa = riscv_isa_string(&s->soc.cpus.harts[cpu]); 116 qemu_fdt_add_subnode(fdt, nodename); 117 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 118 SIFIVE_U_CLOCK_FREQ); 119 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 120 qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 121 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 122 qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 123 qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 124 qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 125 qemu_fdt_add_subnode(fdt, intc); 126 qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); 127 qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", cpu_phandle); 128 qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 129 qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 130 qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 131 g_free(isa); 132 g_free(intc); 133 g_free(nodename); 134 } 135 136 cells = g_new0(uint32_t, s->soc.cpus.num_harts * 4); 137 for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) { 138 nodename = 139 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 140 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 141 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 142 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 143 cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 144 cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 145 g_free(nodename); 146 } 147 nodename = g_strdup_printf("/soc/clint@%lx", 148 (long)memmap[SIFIVE_U_CLINT].base); 149 qemu_fdt_add_subnode(fdt, nodename); 150 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 151 qemu_fdt_setprop_cells(fdt, nodename, "reg", 152 0x0, memmap[SIFIVE_U_CLINT].base, 153 0x0, memmap[SIFIVE_U_CLINT].size); 154 qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 155 cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4); 156 g_free(cells); 157 g_free(nodename); 158 159 plic_phandle = phandle++; 160 cells = g_new0(uint32_t, s->soc.cpus.num_harts * 4); 161 for (cpu = 0; cpu < s->soc.cpus.num_harts; cpu++) { 162 nodename = 163 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 164 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 165 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 166 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT); 167 cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 168 cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT); 169 g_free(nodename); 170 } 171 nodename = g_strdup_printf("/soc/interrupt-controller@%lx", 172 (long)memmap[SIFIVE_U_PLIC].base); 173 qemu_fdt_add_subnode(fdt, nodename); 174 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); 175 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); 176 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 177 qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 178 cells, s->soc.cpus.num_harts * sizeof(uint32_t) * 4); 179 qemu_fdt_setprop_cells(fdt, nodename, "reg", 180 0x0, memmap[SIFIVE_U_PLIC].base, 181 0x0, memmap[SIFIVE_U_PLIC].size); 182 qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 183 qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7); 184 qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); 185 qemu_fdt_setprop_cells(fdt, nodename, "phandle", plic_phandle); 186 qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", plic_phandle); 187 plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 188 g_free(cells); 189 g_free(nodename); 190 191 ethclk_phandle = phandle++; 192 nodename = g_strdup_printf("/soc/ethclk"); 193 qemu_fdt_add_subnode(fdt, nodename); 194 qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); 195 qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); 196 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 197 SIFIVE_U_GEM_CLOCK_FREQ); 198 qemu_fdt_setprop_cell(fdt, nodename, "phandle", ethclk_phandle); 199 qemu_fdt_setprop_cell(fdt, nodename, "linux,phandle", ethclk_phandle); 200 ethclk_phandle = qemu_fdt_get_phandle(fdt, nodename); 201 g_free(nodename); 202 203 nodename = g_strdup_printf("/soc/ethernet@%lx", 204 (long)memmap[SIFIVE_U_GEM].base); 205 qemu_fdt_add_subnode(fdt, nodename); 206 qemu_fdt_setprop_string(fdt, nodename, "compatible", "cdns,macb"); 207 qemu_fdt_setprop_cells(fdt, nodename, "reg", 208 0x0, memmap[SIFIVE_U_GEM].base, 209 0x0, memmap[SIFIVE_U_GEM].size); 210 qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 211 qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); 212 qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 213 qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); 214 qemu_fdt_setprop_cells(fdt, nodename, "clocks", 215 ethclk_phandle, ethclk_phandle, ethclk_phandle); 216 qemu_fdt_setprop(fdt, nodename, "clocks-names", ethclk_names, 217 sizeof(ethclk_names)); 218 qemu_fdt_setprop_cells(fdt, nodename, "#address-cells", 1); 219 qemu_fdt_setprop_cells(fdt, nodename, "#size-cells", 0); 220 g_free(nodename); 221 222 nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", 223 (long)memmap[SIFIVE_U_GEM].base); 224 qemu_fdt_add_subnode(fdt, nodename); 225 qemu_fdt_setprop_cells(fdt, nodename, "reg", 0x0); 226 g_free(nodename); 227 228 nodename = g_strdup_printf("/soc/uart@%lx", 229 (long)memmap[SIFIVE_U_UART0].base); 230 qemu_fdt_add_subnode(fdt, nodename); 231 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 232 qemu_fdt_setprop_cells(fdt, nodename, "reg", 233 0x0, memmap[SIFIVE_U_UART0].base, 234 0x0, memmap[SIFIVE_U_UART0].size); 235 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 236 SIFIVE_U_CLOCK_FREQ / 2); 237 qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 238 qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); 239 240 qemu_fdt_add_subnode(fdt, "/chosen"); 241 qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 242 if (cmdline) { 243 qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 244 } 245 g_free(nodename); 246 } 247 248 static void riscv_sifive_u_init(MachineState *machine) 249 { 250 const struct MemmapEntry *memmap = sifive_u_memmap; 251 252 SiFiveUState *s = g_new0(SiFiveUState, 1); 253 MemoryRegion *system_memory = get_system_memory(); 254 MemoryRegion *main_mem = g_new(MemoryRegion, 1); 255 int i; 256 257 /* Initialize SoC */ 258 object_initialize_child(OBJECT(machine), "soc", &s->soc, 259 sizeof(s->soc), TYPE_RISCV_U_SOC, 260 &error_abort, NULL); 261 object_property_set_bool(OBJECT(&s->soc), true, "realized", 262 &error_abort); 263 264 /* register RAM */ 265 memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", 266 machine->ram_size, &error_fatal); 267 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base, 268 main_mem); 269 270 /* create device tree */ 271 create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 272 273 riscv_find_and_load_firmware(machine, BIOS_FILENAME, 274 memmap[SIFIVE_U_DRAM].base); 275 276 if (machine->kernel_filename) { 277 riscv_load_kernel(machine->kernel_filename); 278 } 279 280 /* reset vector */ 281 uint32_t reset_vec[8] = { 282 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 283 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 284 0xf1402573, /* csrr a0, mhartid */ 285 #if defined(TARGET_RISCV32) 286 0x0182a283, /* lw t0, 24(t0) */ 287 #elif defined(TARGET_RISCV64) 288 0x0182b283, /* ld t0, 24(t0) */ 289 #endif 290 0x00028067, /* jr t0 */ 291 0x00000000, 292 memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */ 293 0x00000000, 294 /* dtb: */ 295 }; 296 297 /* copy in the reset vector in little_endian byte order */ 298 for (i = 0; i < sizeof(reset_vec) >> 2; i++) { 299 reset_vec[i] = cpu_to_le32(reset_vec[i]); 300 } 301 rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), 302 memmap[SIFIVE_U_MROM].base, &address_space_memory); 303 304 /* copy in the device tree */ 305 if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) > 306 memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) { 307 error_report("not enough space to store device-tree"); 308 exit(1); 309 } 310 qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt)); 311 rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt), 312 memmap[SIFIVE_U_MROM].base + sizeof(reset_vec), 313 &address_space_memory); 314 } 315 316 static void riscv_sifive_u_soc_init(Object *obj) 317 { 318 MachineState *ms = MACHINE(qdev_get_machine()); 319 SiFiveUSoCState *s = RISCV_U_SOC(obj); 320 321 object_initialize_child(obj, "cpus", &s->cpus, sizeof(s->cpus), 322 TYPE_RISCV_HART_ARRAY, &error_abort, NULL); 323 object_property_set_str(OBJECT(&s->cpus), SIFIVE_U_CPU, "cpu-type", 324 &error_abort); 325 object_property_set_int(OBJECT(&s->cpus), ms->smp.cpus, "num-harts", 326 &error_abort); 327 328 sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem), 329 TYPE_CADENCE_GEM); 330 } 331 332 static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp) 333 { 334 MachineState *ms = MACHINE(qdev_get_machine()); 335 SiFiveUSoCState *s = RISCV_U_SOC(dev); 336 const struct MemmapEntry *memmap = sifive_u_memmap; 337 MemoryRegion *system_memory = get_system_memory(); 338 MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 339 qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES]; 340 char *plic_hart_config; 341 size_t plic_hart_config_len; 342 int i; 343 Error *err = NULL; 344 NICInfo *nd = &nd_table[0]; 345 346 object_property_set_bool(OBJECT(&s->cpus), true, "realized", 347 &error_abort); 348 349 /* boot rom */ 350 memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom", 351 memmap[SIFIVE_U_MROM].size, &error_fatal); 352 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base, 353 mask_rom); 354 355 /* create PLIC hart topology configuration string */ 356 plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * 357 ms->smp.cpus; 358 plic_hart_config = g_malloc0(plic_hart_config_len); 359 for (i = 0; i < ms->smp.cpus; i++) { 360 if (i != 0) { 361 strncat(plic_hart_config, ",", plic_hart_config_len); 362 } 363 strncat(plic_hart_config, SIFIVE_U_PLIC_HART_CONFIG, 364 plic_hart_config_len); 365 plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); 366 } 367 368 /* MMIO */ 369 s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, 370 plic_hart_config, 371 SIFIVE_U_PLIC_NUM_SOURCES, 372 SIFIVE_U_PLIC_NUM_PRIORITIES, 373 SIFIVE_U_PLIC_PRIORITY_BASE, 374 SIFIVE_U_PLIC_PENDING_BASE, 375 SIFIVE_U_PLIC_ENABLE_BASE, 376 SIFIVE_U_PLIC_ENABLE_STRIDE, 377 SIFIVE_U_PLIC_CONTEXT_BASE, 378 SIFIVE_U_PLIC_CONTEXT_STRIDE, 379 memmap[SIFIVE_U_PLIC].size); 380 sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base, 381 serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); 382 sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base, 383 serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); 384 sifive_clint_create(memmap[SIFIVE_U_CLINT].base, 385 memmap[SIFIVE_U_CLINT].size, ms->smp.cpus, 386 SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 387 388 for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) { 389 plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i); 390 } 391 392 if (nd->used) { 393 qemu_check_nic_model(nd, TYPE_CADENCE_GEM); 394 qdev_set_nic_properties(DEVICE(&s->gem), nd); 395 } 396 object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision", 397 &error_abort); 398 object_property_set_bool(OBJECT(&s->gem), true, "realized", &err); 399 if (err) { 400 error_propagate(errp, err); 401 return; 402 } 403 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base); 404 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, 405 plic_gpios[SIFIVE_U_GEM_IRQ]); 406 } 407 408 static void riscv_sifive_u_machine_init(MachineClass *mc) 409 { 410 mc->desc = "RISC-V Board compatible with SiFive U SDK"; 411 mc->init = riscv_sifive_u_init; 412 /* The real hardware has 5 CPUs, but one of them is a small embedded power 413 * management CPU. 414 */ 415 mc->max_cpus = 4; 416 } 417 418 DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init) 419 420 static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data) 421 { 422 DeviceClass *dc = DEVICE_CLASS(oc); 423 424 dc->realize = riscv_sifive_u_soc_realize; 425 /* Reason: Uses serial_hds in realize function, thus can't be used twice */ 426 dc->user_creatable = false; 427 } 428 429 static const TypeInfo riscv_sifive_u_soc_type_info = { 430 .name = TYPE_RISCV_U_SOC, 431 .parent = TYPE_DEVICE, 432 .instance_size = sizeof(SiFiveUSoCState), 433 .instance_init = riscv_sifive_u_soc_init, 434 .class_init = riscv_sifive_u_soc_class_init, 435 }; 436 437 static void riscv_sifive_u_soc_register_types(void) 438 { 439 type_register_static(&riscv_sifive_u_soc_type_info); 440 } 441 442 type_init(riscv_sifive_u_soc_register_types) 443