spike.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #include "qemu/osdep.h"
  26. #include "qemu/log.h"
  27. #include "qemu/error-report.h"
  28. #include "qapi/error.h"
  29. #include "hw/boards.h"
  30. #include "hw/loader.h"
  31. #include "hw/sysbus.h"
  32. #include "target/riscv/cpu.h"
  33. #include "hw/riscv/riscv_htif.h"
  34. #include "hw/riscv/riscv_hart.h"
  35. #include "hw/riscv/sifive_clint.h"
  36. #include "hw/riscv/spike.h"
  37. #include "hw/riscv/boot.h"
  38. #include "chardev/char.h"
  39. #include "sysemu/arch_init.h"
  40. #include "sysemu/device_tree.h"
  41. #include "sysemu/qtest.h"
  42. #include "sysemu/sysemu.h"
  43. #include "exec/address-spaces.h"
  44. #include <libfdt.h>
  45. static const struct MemmapEntry {
  46. hwaddr base;
  47. hwaddr size;
  48. } spike_memmap[] = {
  49. [SPIKE_MROM] = { 0x1000, 0x11000 },
  50. [SPIKE_CLINT] = { 0x2000000, 0x10000 },
  51. [SPIKE_DRAM] = { 0x80000000, 0x0 },
  52. };
  53. static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap,
  54. uint64_t mem_size, const char *cmdline)
  55. {
  56. void *fdt;
  57. int cpu;
  58. uint32_t *cells;
  59. char *nodename;
  60. fdt = s->fdt = create_device_tree(&s->fdt_size);
  61. if (!fdt) {
  62. error_report("create_device_tree() failed");
  63. exit(1);
  64. }
  65. qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
  66. qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
  67. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  68. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  69. qemu_fdt_add_subnode(fdt, "/htif");
  70. qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0");
  71. qemu_fdt_add_subnode(fdt, "/soc");
  72. qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
  73. qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
  74. qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
  75. qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
  76. nodename = g_strdup_printf("/memory@%lx",
  77. (long)memmap[SPIKE_DRAM].base);
  78. qemu_fdt_add_subnode(fdt, nodename);
  79. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  80. memmap[SPIKE_DRAM].base >> 32, memmap[SPIKE_DRAM].base,
  81. mem_size >> 32, mem_size);
  82. qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
  83. g_free(nodename);
  84. qemu_fdt_add_subnode(fdt, "/cpus");
  85. qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
  86. SIFIVE_CLINT_TIMEBASE_FREQ);
  87. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
  88. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
  89. for (cpu = s->soc.num_harts - 1; cpu >= 0; cpu--) {
  90. nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
  91. char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
  92. char *isa = riscv_isa_string(&s->soc.harts[cpu]);
  93. qemu_fdt_add_subnode(fdt, nodename);
  94. qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
  95. qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
  96. qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
  97. qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
  98. qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
  99. qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
  100. qemu_fdt_add_subnode(fdt, intc);
  101. qemu_fdt_setprop_cell(fdt, intc, "phandle", 1);
  102. qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
  103. qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
  104. qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
  105. g_free(isa);
  106. g_free(intc);
  107. g_free(nodename);
  108. }
  109. cells = g_new0(uint32_t, s->soc.num_harts * 4);
  110. for (cpu = 0; cpu < s->soc.num_harts; cpu++) {
  111. nodename =
  112. g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
  113. uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
  114. cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  115. cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
  116. cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
  117. cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
  118. g_free(nodename);
  119. }
  120. nodename = g_strdup_printf("/soc/clint@%lx",
  121. (long)memmap[SPIKE_CLINT].base);
  122. qemu_fdt_add_subnode(fdt, nodename);
  123. qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
  124. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  125. 0x0, memmap[SPIKE_CLINT].base,
  126. 0x0, memmap[SPIKE_CLINT].size);
  127. qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
  128. cells, s->soc.num_harts * sizeof(uint32_t) * 4);
  129. g_free(cells);
  130. g_free(nodename);
  131. if (cmdline) {
  132. qemu_fdt_add_subnode(fdt, "/chosen");
  133. qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
  134. }
  135. }
  136. static void spike_board_init(MachineState *machine)
  137. {
  138. const struct MemmapEntry *memmap = spike_memmap;
  139. SpikeState *s = g_new0(SpikeState, 1);
  140. MemoryRegion *system_memory = get_system_memory();
  141. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  142. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  143. int i;
  144. unsigned int smp_cpus = machine->smp.cpus;
  145. /* Initialize SOC */
  146. object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc),
  147. TYPE_RISCV_HART_ARRAY, &error_abort, NULL);
  148. object_property_set_str(OBJECT(&s->soc), machine->cpu_type, "cpu-type",
  149. &error_abort);
  150. object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts",
  151. &error_abort);
  152. object_property_set_bool(OBJECT(&s->soc), true, "realized",
  153. &error_abort);
  154. /* register system main memory (actual RAM) */
  155. memory_region_init_ram(main_mem, NULL, "riscv.spike.ram",
  156. machine->ram_size, &error_fatal);
  157. memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
  158. main_mem);
  159. /* create device tree */
  160. create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
  161. /* boot rom */
  162. memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
  163. memmap[SPIKE_MROM].size, &error_fatal);
  164. memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
  165. mask_rom);
  166. if (machine->kernel_filename) {
  167. riscv_load_kernel(machine->kernel_filename, htif_symbol_callback);
  168. }
  169. /* reset vector */
  170. uint32_t reset_vec[8] = {
  171. 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */
  172. 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */
  173. 0xf1402573, /* csrr a0, mhartid */
  174. #if defined(TARGET_RISCV32)
  175. 0x0182a283, /* lw t0, 24(t0) */
  176. #elif defined(TARGET_RISCV64)
  177. 0x0182b283, /* ld t0, 24(t0) */
  178. #endif
  179. 0x00028067, /* jr t0 */
  180. 0x00000000,
  181. memmap[SPIKE_DRAM].base, /* start: .dword DRAM_BASE */
  182. 0x00000000,
  183. /* dtb: */
  184. };
  185. /* copy in the reset vector in little_endian byte order */
  186. for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
  187. reset_vec[i] = cpu_to_le32(reset_vec[i]);
  188. }
  189. rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
  190. memmap[SPIKE_MROM].base, &address_space_memory);
  191. /* copy in the device tree */
  192. if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) >
  193. memmap[SPIKE_MROM].size - sizeof(reset_vec)) {
  194. error_report("not enough space to store device-tree");
  195. exit(1);
  196. }
  197. qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt));
  198. rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt),
  199. memmap[SPIKE_MROM].base + sizeof(reset_vec),
  200. &address_space_memory);
  201. /* initialize HTIF using symbols found in load_kernel */
  202. htif_mm_init(system_memory, mask_rom, &s->soc.harts[0].env, serial_hd(0));
  203. /* Core Local Interruptor (timer and IPI) */
  204. sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size,
  205. smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
  206. }
  207. static void spike_v1_10_0_board_init(MachineState *machine)
  208. {
  209. const struct MemmapEntry *memmap = spike_memmap;
  210. SpikeState *s = g_new0(SpikeState, 1);
  211. MemoryRegion *system_memory = get_system_memory();
  212. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  213. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  214. int i;
  215. unsigned int smp_cpus = machine->smp.cpus;
  216. if (!qtest_enabled()) {
  217. info_report("The Spike v1.10.0 machine has been deprecated. "
  218. "Please use the generic spike machine and specify the ISA "
  219. "versions using -cpu.");
  220. }
  221. /* Initialize SOC */
  222. object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc),
  223. TYPE_RISCV_HART_ARRAY, &error_abort, NULL);
  224. object_property_set_str(OBJECT(&s->soc), SPIKE_V1_10_0_CPU, "cpu-type",
  225. &error_abort);
  226. object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts",
  227. &error_abort);
  228. object_property_set_bool(OBJECT(&s->soc), true, "realized",
  229. &error_abort);
  230. /* register system main memory (actual RAM) */
  231. memory_region_init_ram(main_mem, NULL, "riscv.spike.ram",
  232. machine->ram_size, &error_fatal);
  233. memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
  234. main_mem);
  235. /* create device tree */
  236. create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
  237. /* boot rom */
  238. memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
  239. memmap[SPIKE_MROM].size, &error_fatal);
  240. memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
  241. mask_rom);
  242. if (machine->kernel_filename) {
  243. riscv_load_kernel(machine->kernel_filename, htif_symbol_callback);
  244. }
  245. /* reset vector */
  246. uint32_t reset_vec[8] = {
  247. 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */
  248. 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */
  249. 0xf1402573, /* csrr a0, mhartid */
  250. #if defined(TARGET_RISCV32)
  251. 0x0182a283, /* lw t0, 24(t0) */
  252. #elif defined(TARGET_RISCV64)
  253. 0x0182b283, /* ld t0, 24(t0) */
  254. #endif
  255. 0x00028067, /* jr t0 */
  256. 0x00000000,
  257. memmap[SPIKE_DRAM].base, /* start: .dword DRAM_BASE */
  258. 0x00000000,
  259. /* dtb: */
  260. };
  261. /* copy in the reset vector in little_endian byte order */
  262. for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
  263. reset_vec[i] = cpu_to_le32(reset_vec[i]);
  264. }
  265. rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
  266. memmap[SPIKE_MROM].base, &address_space_memory);
  267. /* copy in the device tree */
  268. if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) >
  269. memmap[SPIKE_MROM].size - sizeof(reset_vec)) {
  270. error_report("not enough space to store device-tree");
  271. exit(1);
  272. }
  273. qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt));
  274. rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt),
  275. memmap[SPIKE_MROM].base + sizeof(reset_vec),
  276. &address_space_memory);
  277. /* initialize HTIF using symbols found in load_kernel */
  278. htif_mm_init(system_memory, mask_rom, &s->soc.harts[0].env, serial_hd(0));
  279. /* Core Local Interruptor (timer and IPI) */
  280. sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size,
  281. smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
  282. }
  283. static void spike_v1_09_1_board_init(MachineState *machine)
  284. {
  285. const struct MemmapEntry *memmap = spike_memmap;
  286. SpikeState *s = g_new0(SpikeState, 1);
  287. MemoryRegion *system_memory = get_system_memory();
  288. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  289. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  290. int i;
  291. unsigned int smp_cpus = machine->smp.cpus;
  292. if (!qtest_enabled()) {
  293. info_report("The Spike v1.09.1 machine has been deprecated. "
  294. "Please use the generic spike machine and specify the ISA "
  295. "versions using -cpu.");
  296. }
  297. /* Initialize SOC */
  298. object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc),
  299. TYPE_RISCV_HART_ARRAY, &error_abort, NULL);
  300. object_property_set_str(OBJECT(&s->soc), SPIKE_V1_09_1_CPU, "cpu-type",
  301. &error_abort);
  302. object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts",
  303. &error_abort);
  304. object_property_set_bool(OBJECT(&s->soc), true, "realized",
  305. &error_abort);
  306. /* register system main memory (actual RAM) */
  307. memory_region_init_ram(main_mem, NULL, "riscv.spike.ram",
  308. machine->ram_size, &error_fatal);
  309. memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
  310. main_mem);
  311. /* boot rom */
  312. memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
  313. memmap[SPIKE_MROM].size, &error_fatal);
  314. memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
  315. mask_rom);
  316. if (machine->kernel_filename) {
  317. riscv_load_kernel(machine->kernel_filename, htif_symbol_callback);
  318. }
  319. /* reset vector */
  320. uint32_t reset_vec[8] = {
  321. 0x297 + memmap[SPIKE_DRAM].base - memmap[SPIKE_MROM].base, /* lui */
  322. 0x00028067, /* jump to DRAM_BASE */
  323. 0x00000000, /* reserved */
  324. memmap[SPIKE_MROM].base + sizeof(reset_vec), /* config string pointer */
  325. 0, 0, 0, 0 /* trap vector */
  326. };
  327. /* part one of config string - before memory size specified */
  328. const char *config_string_tmpl =
  329. "platform {\n"
  330. " vendor ucb;\n"
  331. " arch spike;\n"
  332. "};\n"
  333. "rtc {\n"
  334. " addr 0x%" PRIx64 "x;\n"
  335. "};\n"
  336. "ram {\n"
  337. " 0 {\n"
  338. " addr 0x%" PRIx64 "x;\n"
  339. " size 0x%" PRIx64 "x;\n"
  340. " };\n"
  341. "};\n"
  342. "core {\n"
  343. " 0" " {\n"
  344. " " "0 {\n"
  345. " isa %s;\n"
  346. " timecmp 0x%" PRIx64 "x;\n"
  347. " ipi 0x%" PRIx64 "x;\n"
  348. " };\n"
  349. " };\n"
  350. "};\n";
  351. /* build config string with supplied memory size */
  352. char *isa = riscv_isa_string(&s->soc.harts[0]);
  353. char *config_string = g_strdup_printf(config_string_tmpl,
  354. (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_TIME_BASE,
  355. (uint64_t)memmap[SPIKE_DRAM].base,
  356. (uint64_t)ram_size, isa,
  357. (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_TIMECMP_BASE,
  358. (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_SIP_BASE);
  359. g_free(isa);
  360. size_t config_string_len = strlen(config_string);
  361. /* copy in the reset vector in little_endian byte order */
  362. for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
  363. reset_vec[i] = cpu_to_le32(reset_vec[i]);
  364. }
  365. rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
  366. memmap[SPIKE_MROM].base, &address_space_memory);
  367. /* copy in the config string */
  368. rom_add_blob_fixed_as("mrom.reset", config_string, config_string_len,
  369. memmap[SPIKE_MROM].base + sizeof(reset_vec),
  370. &address_space_memory);
  371. /* initialize HTIF using symbols found in load_kernel */
  372. htif_mm_init(system_memory, mask_rom, &s->soc.harts[0].env, serial_hd(0));
  373. /* Core Local Interruptor (timer and IPI) */
  374. sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size,
  375. smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
  376. g_free(config_string);
  377. }
  378. static void spike_v1_09_1_machine_init(MachineClass *mc)
  379. {
  380. mc->desc = "RISC-V Spike Board (Privileged ISA v1.9.1)";
  381. mc->init = spike_v1_09_1_board_init;
  382. mc->max_cpus = 1;
  383. }
  384. static void spike_v1_10_0_machine_init(MachineClass *mc)
  385. {
  386. mc->desc = "RISC-V Spike Board (Privileged ISA v1.10)";
  387. mc->init = spike_v1_10_0_board_init;
  388. mc->max_cpus = 1;
  389. }
  390. static void spike_machine_init(MachineClass *mc)
  391. {
  392. mc->desc = "RISC-V Spike Board";
  393. mc->init = spike_board_init;
  394. mc->max_cpus = 1;
  395. mc->is_default = 1;
  396. mc->default_cpu_type = SPIKE_V1_10_0_CPU;
  397. }
  398. DEFINE_MACHINE("spike_v1.9.1", spike_v1_09_1_machine_init)
  399. DEFINE_MACHINE("spike_v1.10", spike_v1_10_0_machine_init)
  400. DEFINE_MACHINE("spike", spike_machine_init)