1 /* 2 * QEMU RISC-V Spike Board 3 * 4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 * Copyright (c) 2017-2018 SiFive, Inc. 6 * 7 * This provides a RISC-V Board with the following devices: 8 * 9 * 0) HTIF Console and Poweroff 10 * 1) CLINT (Timer and IPI) 11 * 2) PLIC (Platform Level Interrupt Controller) 12 * 13 * This program is free software; you can redistribute it and/or modify it 14 * under the terms and conditions of the GNU General Public License, 15 * version 2 or later, as published by the Free Software Foundation. 16 * 17 * This program is distributed in the hope it will be useful, but WITHOUT 18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 20 * more details. 21 * 22 * You should have received a copy of the GNU General Public License along with 23 * this program. If not, see <http://www.gnu.org/licenses/>. 24 */ 25 26 #include "qemu/osdep.h" 27 #include "qemu/log.h" 28 #include "qemu/error-report.h" 29 #include "qapi/error.h" 30 #include "hw/hw.h" 31 #include "hw/boards.h" 32 #include "hw/loader.h" 33 #include "hw/sysbus.h" 34 #include "target/riscv/cpu.h" 35 #include "hw/riscv/riscv_htif.h" 36 #include "hw/riscv/riscv_hart.h" 37 #include "hw/riscv/sifive_clint.h" 38 #include "hw/riscv/spike.h" 39 #include "chardev/char.h" 40 #include "sysemu/arch_init.h" 41 #include "sysemu/device_tree.h" 42 #include "exec/address-spaces.h" 43 #include "elf.h" 44 45 static const struct MemmapEntry { 46 hwaddr base; 47 hwaddr size; 48 } spike_memmap[] = { 49 [SPIKE_MROM] = { 0x1000, 0x2000 }, 50 [SPIKE_CLINT] = { 0x2000000, 0x10000 }, 51 [SPIKE_DRAM] = { 0x80000000, 0x0 }, 52 }; 53 54 static void copy_le32_to_phys(hwaddr pa, uint32_t *rom, size_t len) 55 { 56 int i; 57 for (i = 0; i < (len >> 2); i++) { 58 stl_phys(&address_space_memory, pa + (i << 2), rom[i]); 59 } 60 } 61 62 static uint64_t identity_translate(void *opaque, uint64_t addr) 63 { 64 return addr; 65 } 66 67 static uint64_t load_kernel(const char *kernel_filename) 68 { 69 uint64_t kernel_entry, kernel_high; 70 71 if (load_elf_ram_sym(kernel_filename, identity_translate, NULL, 72 &kernel_entry, NULL, &kernel_high, 0, ELF_MACHINE, 1, 0, 73 NULL, true, htif_symbol_callback) < 0) { 74 error_report("qemu: could not load kernel '%s'", kernel_filename); 75 exit(1); 76 } 77 return kernel_entry; 78 } 79 80 static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap, 81 uint64_t mem_size, const char *cmdline) 82 { 83 void *fdt; 84 int cpu; 85 uint32_t *cells; 86 char *nodename; 87 88 fdt = s->fdt = create_device_tree(&s->fdt_size); 89 if (!fdt) { 90 error_report("create_device_tree() failed"); 91 exit(1); 92 } 93 94 qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu"); 95 qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev"); 96 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 97 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 98 99 qemu_fdt_add_subnode(fdt, "/htif"); 100 qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0"); 101 102 qemu_fdt_add_subnode(fdt, "/soc"); 103 qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 104 qemu_fdt_setprop_string(fdt, "/soc", "compatible", "ucbbar,spike-bare-soc"); 105 qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 106 qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 107 108 nodename = g_strdup_printf("/memory@%lx", 109 (long)memmap[SPIKE_DRAM].base); 110 qemu_fdt_add_subnode(fdt, nodename); 111 qemu_fdt_setprop_cells(fdt, nodename, "reg", 112 memmap[SPIKE_DRAM].base >> 32, memmap[SPIKE_DRAM].base, 113 mem_size >> 32, mem_size); 114 qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 115 g_free(nodename); 116 117 qemu_fdt_add_subnode(fdt, "/cpus"); 118 qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 119 SIFIVE_CLINT_TIMEBASE_FREQ); 120 qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 121 qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 122 123 for (cpu = s->soc.num_harts - 1; cpu >= 0; cpu--) { 124 nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 125 char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 126 char *isa = riscv_isa_string(&s->soc.harts[cpu]); 127 qemu_fdt_add_subnode(fdt, nodename); 128 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 129 SPIKE_CLOCK_FREQ); 130 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 131 qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 132 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 133 qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 134 qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 135 qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 136 qemu_fdt_add_subnode(fdt, intc); 137 qemu_fdt_setprop_cell(fdt, intc, "phandle", 1); 138 qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", 1); 139 qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 140 qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 141 qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 142 g_free(isa); 143 g_free(intc); 144 g_free(nodename); 145 } 146 147 cells = g_new0(uint32_t, s->soc.num_harts * 4); 148 for (cpu = 0; cpu < s->soc.num_harts; cpu++) { 149 nodename = 150 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 151 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 152 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 153 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 154 cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 155 cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 156 g_free(nodename); 157 } 158 nodename = g_strdup_printf("/soc/clint@%lx", 159 (long)memmap[SPIKE_CLINT].base); 160 qemu_fdt_add_subnode(fdt, nodename); 161 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 162 qemu_fdt_setprop_cells(fdt, nodename, "reg", 163 0x0, memmap[SPIKE_CLINT].base, 164 0x0, memmap[SPIKE_CLINT].size); 165 qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 166 cells, s->soc.num_harts * sizeof(uint32_t) * 4); 167 g_free(cells); 168 g_free(nodename); 169 170 qemu_fdt_add_subnode(fdt, "/chosen"); 171 qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 172 } 173 174 static void spike_v1_10_0_board_init(MachineState *machine) 175 { 176 const struct MemmapEntry *memmap = spike_memmap; 177 178 SpikeState *s = g_new0(SpikeState, 1); 179 MemoryRegion *system_memory = get_system_memory(); 180 MemoryRegion *main_mem = g_new(MemoryRegion, 1); 181 MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 182 183 /* Initialize SOC */ 184 object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 185 object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 186 &error_abort); 187 object_property_set_str(OBJECT(&s->soc), SPIKE_V1_10_0_CPU, "cpu-type", 188 &error_abort); 189 object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 190 &error_abort); 191 object_property_set_bool(OBJECT(&s->soc), true, "realized", 192 &error_abort); 193 194 /* register system main memory (actual RAM) */ 195 memory_region_init_ram(main_mem, NULL, "riscv.spike.ram", 196 machine->ram_size, &error_fatal); 197 memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base, 198 main_mem); 199 200 /* create device tree */ 201 create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 202 203 /* boot rom */ 204 memory_region_init_ram(boot_rom, NULL, "riscv.spike.bootrom", 205 s->fdt_size + 0x2000, &error_fatal); 206 memory_region_add_subregion(system_memory, 0x0, boot_rom); 207 208 if (machine->kernel_filename) { 209 load_kernel(machine->kernel_filename); 210 } 211 212 /* reset vector */ 213 uint32_t reset_vec[8] = { 214 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 215 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 216 0xf1402573, /* csrr a0, mhartid */ 217 #if defined(TARGET_RISCV32) 218 0x0182a283, /* lw t0, 24(t0) */ 219 #elif defined(TARGET_RISCV64) 220 0x0182b283, /* ld t0, 24(t0) */ 221 #endif 222 0x00028067, /* jr t0 */ 223 0x00000000, 224 memmap[SPIKE_DRAM].base, /* start: .dword DRAM_BASE */ 225 0x00000000, 226 /* dtb: */ 227 }; 228 229 /* copy in the reset vector */ 230 copy_le32_to_phys(memmap[SPIKE_MROM].base, reset_vec, sizeof(reset_vec)); 231 232 /* copy in the device tree */ 233 qemu_fdt_dumpdtb(s->fdt, s->fdt_size); 234 cpu_physical_memory_write(memmap[SPIKE_MROM].base + sizeof(reset_vec), 235 s->fdt, s->fdt_size); 236 237 /* initialize HTIF using symbols found in load_kernel */ 238 htif_mm_init(system_memory, boot_rom, &s->soc.harts[0].env, serial_hd(0)); 239 240 /* Core Local Interruptor (timer and IPI) */ 241 sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size, 242 smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 243 } 244 245 static void spike_v1_09_1_board_init(MachineState *machine) 246 { 247 const struct MemmapEntry *memmap = spike_memmap; 248 249 SpikeState *s = g_new0(SpikeState, 1); 250 MemoryRegion *system_memory = get_system_memory(); 251 MemoryRegion *main_mem = g_new(MemoryRegion, 1); 252 MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 253 254 /* Initialize SOC */ 255 object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 256 object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 257 &error_abort); 258 object_property_set_str(OBJECT(&s->soc), SPIKE_V1_09_1_CPU, "cpu-type", 259 &error_abort); 260 object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 261 &error_abort); 262 object_property_set_bool(OBJECT(&s->soc), true, "realized", 263 &error_abort); 264 265 /* register system main memory (actual RAM) */ 266 memory_region_init_ram(main_mem, NULL, "riscv.spike.ram", 267 machine->ram_size, &error_fatal); 268 memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base, 269 main_mem); 270 271 /* boot rom */ 272 memory_region_init_ram(boot_rom, NULL, "riscv.spike.bootrom", 273 0x40000, &error_fatal); 274 memory_region_add_subregion(system_memory, 0x0, boot_rom); 275 276 if (machine->kernel_filename) { 277 load_kernel(machine->kernel_filename); 278 } 279 280 /* reset vector */ 281 uint32_t reset_vec[8] = { 282 0x297 + memmap[SPIKE_DRAM].base - memmap[SPIKE_MROM].base, /* lui */ 283 0x00028067, /* jump to DRAM_BASE */ 284 0x00000000, /* reserved */ 285 memmap[SPIKE_MROM].base + sizeof(reset_vec), /* config string pointer */ 286 0, 0, 0, 0 /* trap vector */ 287 }; 288 289 /* part one of config string - before memory size specified */ 290 const char *config_string_tmpl = 291 "platform {\n" 292 " vendor ucb;\n" 293 " arch spike;\n" 294 "};\n" 295 "rtc {\n" 296 " addr 0x%" PRIx64 "x;\n" 297 "};\n" 298 "ram {\n" 299 " 0 {\n" 300 " addr 0x%" PRIx64 "x;\n" 301 " size 0x%" PRIx64 "x;\n" 302 " };\n" 303 "};\n" 304 "core {\n" 305 " 0" " {\n" 306 " " "0 {\n" 307 " isa %s;\n" 308 " timecmp 0x%" PRIx64 "x;\n" 309 " ipi 0x%" PRIx64 "x;\n" 310 " };\n" 311 " };\n" 312 "};\n"; 313 314 /* build config string with supplied memory size */ 315 char *isa = riscv_isa_string(&s->soc.harts[0]); 316 size_t config_string_size = strlen(config_string_tmpl) + 48; 317 char *config_string = malloc(config_string_size); 318 snprintf(config_string, config_string_size, config_string_tmpl, 319 (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_TIME_BASE, 320 (uint64_t)memmap[SPIKE_DRAM].base, 321 (uint64_t)ram_size, isa, 322 (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_TIMECMP_BASE, 323 (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_SIP_BASE); 324 g_free(isa); 325 size_t config_string_len = strlen(config_string); 326 327 /* copy in the reset vector */ 328 copy_le32_to_phys(memmap[SPIKE_MROM].base, reset_vec, sizeof(reset_vec)); 329 330 /* copy in the config string */ 331 cpu_physical_memory_write(memmap[SPIKE_MROM].base + sizeof(reset_vec), 332 config_string, config_string_len); 333 334 /* initialize HTIF using symbols found in load_kernel */ 335 htif_mm_init(system_memory, boot_rom, &s->soc.harts[0].env, serial_hd(0)); 336 337 /* Core Local Interruptor (timer and IPI) */ 338 sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size, 339 smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 340 } 341 342 static const TypeInfo spike_v_1_09_1_device = { 343 .name = TYPE_RISCV_SPIKE_V1_09_1_BOARD, 344 .parent = TYPE_SYS_BUS_DEVICE, 345 .instance_size = sizeof(SpikeState), 346 }; 347 348 static const TypeInfo spike_v_1_10_0_device = { 349 .name = TYPE_RISCV_SPIKE_V1_10_0_BOARD, 350 .parent = TYPE_SYS_BUS_DEVICE, 351 .instance_size = sizeof(SpikeState), 352 }; 353 354 static void spike_v1_09_1_machine_init(MachineClass *mc) 355 { 356 mc->desc = "RISC-V Spike Board (Privileged ISA v1.9.1)"; 357 mc->init = spike_v1_09_1_board_init; 358 mc->max_cpus = 1; 359 } 360 361 static void spike_v1_10_0_machine_init(MachineClass *mc) 362 { 363 mc->desc = "RISC-V Spike Board (Privileged ISA v1.10)"; 364 mc->init = spike_v1_10_0_board_init; 365 mc->max_cpus = 1; 366 mc->is_default = 1; 367 } 368 369 DEFINE_MACHINE("spike_v1.9.1", spike_v1_09_1_machine_init) 370 DEFINE_MACHINE("spike_v1.10", spike_v1_10_0_machine_init) 371 372 static void riscv_spike_board_register_types(void) 373 { 374 type_register_static(&spike_v_1_09_1_device); 375 type_register_static(&spike_v_1_10_0_device); 376 } 377 378 type_init(riscv_spike_board_register_types); 379