2
0

spike.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_hart.h"
  34. #include "hw/riscv/spike.h"
  35. #include "hw/riscv/boot.h"
  36. #include "hw/riscv/numa.h"
  37. #include "hw/char/riscv_htif.h"
  38. #include "hw/intc/sifive_clint.h"
  39. #include "chardev/char.h"
  40. #include "sysemu/arch_init.h"
  41. #include "sysemu/device_tree.h"
  42. #include "sysemu/qtest.h"
  43. #include "sysemu/sysemu.h"
  44. /*
  45. * Not like other RISC-V machines that use plain binary bios images,
  46. * keeping ELF files here was intentional because BIN files don't work
  47. * for the Spike machine as HTIF emulation depends on ELF parsing.
  48. */
  49. #if defined(TARGET_RISCV32)
  50. # define BIOS_FILENAME "opensbi-riscv32-generic-fw_dynamic.elf"
  51. #else
  52. # define BIOS_FILENAME "opensbi-riscv64-generic-fw_dynamic.elf"
  53. #endif
  54. static const struct MemmapEntry {
  55. hwaddr base;
  56. hwaddr size;
  57. } spike_memmap[] = {
  58. [SPIKE_MROM] = { 0x1000, 0xf000 },
  59. [SPIKE_CLINT] = { 0x2000000, 0x10000 },
  60. [SPIKE_DRAM] = { 0x80000000, 0x0 },
  61. };
  62. static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap,
  63. uint64_t mem_size, const char *cmdline)
  64. {
  65. void *fdt;
  66. uint64_t addr, size;
  67. unsigned long clint_addr;
  68. int cpu, socket;
  69. MachineState *mc = MACHINE(s);
  70. uint32_t *clint_cells;
  71. uint32_t cpu_phandle, intc_phandle, phandle = 1;
  72. char *name, *mem_name, *clint_name, *clust_name;
  73. char *core_name, *cpu_name, *intc_name;
  74. fdt = s->fdt = create_device_tree(&s->fdt_size);
  75. if (!fdt) {
  76. error_report("create_device_tree() failed");
  77. exit(1);
  78. }
  79. qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
  80. qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
  81. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  82. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  83. qemu_fdt_add_subnode(fdt, "/htif");
  84. qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0");
  85. qemu_fdt_add_subnode(fdt, "/soc");
  86. qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
  87. qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
  88. qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
  89. qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
  90. qemu_fdt_add_subnode(fdt, "/cpus");
  91. qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
  92. SIFIVE_CLINT_TIMEBASE_FREQ);
  93. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
  94. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
  95. qemu_fdt_add_subnode(fdt, "/cpus/cpu-map");
  96. for (socket = (riscv_socket_count(mc) - 1); socket >= 0; socket--) {
  97. clust_name = g_strdup_printf("/cpus/cpu-map/cluster%d", socket);
  98. qemu_fdt_add_subnode(fdt, clust_name);
  99. clint_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
  100. for (cpu = s->soc[socket].num_harts - 1; cpu >= 0; cpu--) {
  101. cpu_phandle = phandle++;
  102. cpu_name = g_strdup_printf("/cpus/cpu@%d",
  103. s->soc[socket].hartid_base + cpu);
  104. qemu_fdt_add_subnode(fdt, cpu_name);
  105. #if defined(TARGET_RISCV32)
  106. qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv32");
  107. #else
  108. qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv48");
  109. #endif
  110. name = riscv_isa_string(&s->soc[socket].harts[cpu]);
  111. qemu_fdt_setprop_string(fdt, cpu_name, "riscv,isa", name);
  112. g_free(name);
  113. qemu_fdt_setprop_string(fdt, cpu_name, "compatible", "riscv");
  114. qemu_fdt_setprop_string(fdt, cpu_name, "status", "okay");
  115. qemu_fdt_setprop_cell(fdt, cpu_name, "reg",
  116. s->soc[socket].hartid_base + cpu);
  117. qemu_fdt_setprop_string(fdt, cpu_name, "device_type", "cpu");
  118. riscv_socket_fdt_write_id(mc, fdt, cpu_name, socket);
  119. qemu_fdt_setprop_cell(fdt, cpu_name, "phandle", cpu_phandle);
  120. intc_name = g_strdup_printf("%s/interrupt-controller", cpu_name);
  121. qemu_fdt_add_subnode(fdt, intc_name);
  122. intc_phandle = phandle++;
  123. qemu_fdt_setprop_cell(fdt, intc_name, "phandle", intc_phandle);
  124. qemu_fdt_setprop_string(fdt, intc_name, "compatible",
  125. "riscv,cpu-intc");
  126. qemu_fdt_setprop(fdt, intc_name, "interrupt-controller", NULL, 0);
  127. qemu_fdt_setprop_cell(fdt, intc_name, "#interrupt-cells", 1);
  128. clint_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  129. clint_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
  130. clint_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
  131. clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
  132. core_name = g_strdup_printf("%s/core%d", clust_name, cpu);
  133. qemu_fdt_add_subnode(fdt, core_name);
  134. qemu_fdt_setprop_cell(fdt, core_name, "cpu", cpu_phandle);
  135. g_free(core_name);
  136. g_free(intc_name);
  137. g_free(cpu_name);
  138. }
  139. addr = memmap[SPIKE_DRAM].base + riscv_socket_mem_offset(mc, socket);
  140. size = riscv_socket_mem_size(mc, socket);
  141. mem_name = g_strdup_printf("/memory@%lx", (long)addr);
  142. qemu_fdt_add_subnode(fdt, mem_name);
  143. qemu_fdt_setprop_cells(fdt, mem_name, "reg",
  144. addr >> 32, addr, size >> 32, size);
  145. qemu_fdt_setprop_string(fdt, mem_name, "device_type", "memory");
  146. riscv_socket_fdt_write_id(mc, fdt, mem_name, socket);
  147. g_free(mem_name);
  148. clint_addr = memmap[SPIKE_CLINT].base +
  149. (memmap[SPIKE_CLINT].size * socket);
  150. clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr);
  151. qemu_fdt_add_subnode(fdt, clint_name);
  152. qemu_fdt_setprop_string(fdt, clint_name, "compatible", "riscv,clint0");
  153. qemu_fdt_setprop_cells(fdt, clint_name, "reg",
  154. 0x0, clint_addr, 0x0, memmap[SPIKE_CLINT].size);
  155. qemu_fdt_setprop(fdt, clint_name, "interrupts-extended",
  156. clint_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4);
  157. riscv_socket_fdt_write_id(mc, fdt, clint_name, socket);
  158. g_free(clint_name);
  159. g_free(clint_cells);
  160. g_free(clust_name);
  161. }
  162. riscv_socket_fdt_write_distance_matrix(mc, fdt);
  163. if (cmdline) {
  164. qemu_fdt_add_subnode(fdt, "/chosen");
  165. qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
  166. }
  167. }
  168. static void spike_board_init(MachineState *machine)
  169. {
  170. const struct MemmapEntry *memmap = spike_memmap;
  171. SpikeState *s = SPIKE_MACHINE(machine);
  172. MemoryRegion *system_memory = get_system_memory();
  173. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  174. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  175. uint32_t fdt_load_addr;
  176. uint64_t kernel_entry;
  177. char *soc_name;
  178. int i, base_hartid, hart_count;
  179. /* Check socket count limit */
  180. if (SPIKE_SOCKETS_MAX < riscv_socket_count(machine)) {
  181. error_report("number of sockets/nodes should be less than %d",
  182. SPIKE_SOCKETS_MAX);
  183. exit(1);
  184. }
  185. /* Initialize sockets */
  186. for (i = 0; i < riscv_socket_count(machine); i++) {
  187. if (!riscv_socket_check_hartids(machine, i)) {
  188. error_report("discontinuous hartids in socket%d", i);
  189. exit(1);
  190. }
  191. base_hartid = riscv_socket_first_hartid(machine, i);
  192. if (base_hartid < 0) {
  193. error_report("can't find hartid base for socket%d", i);
  194. exit(1);
  195. }
  196. hart_count = riscv_socket_hart_count(machine, i);
  197. if (hart_count < 0) {
  198. error_report("can't find hart count for socket%d", i);
  199. exit(1);
  200. }
  201. soc_name = g_strdup_printf("soc%d", i);
  202. object_initialize_child(OBJECT(machine), soc_name, &s->soc[i],
  203. TYPE_RISCV_HART_ARRAY);
  204. g_free(soc_name);
  205. object_property_set_str(OBJECT(&s->soc[i]), "cpu-type",
  206. machine->cpu_type, &error_abort);
  207. object_property_set_int(OBJECT(&s->soc[i]), "hartid-base",
  208. base_hartid, &error_abort);
  209. object_property_set_int(OBJECT(&s->soc[i]), "num-harts",
  210. hart_count, &error_abort);
  211. sysbus_realize(SYS_BUS_DEVICE(&s->soc[i]), &error_abort);
  212. /* Core Local Interruptor (timer and IPI) for each socket */
  213. sifive_clint_create(
  214. memmap[SPIKE_CLINT].base + i * memmap[SPIKE_CLINT].size,
  215. memmap[SPIKE_CLINT].size, base_hartid, hart_count,
  216. SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
  217. SIFIVE_CLINT_TIMEBASE_FREQ, false);
  218. }
  219. /* register system main memory (actual RAM) */
  220. memory_region_init_ram(main_mem, NULL, "riscv.spike.ram",
  221. machine->ram_size, &error_fatal);
  222. memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
  223. main_mem);
  224. /* create device tree */
  225. create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
  226. /* boot rom */
  227. memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
  228. memmap[SPIKE_MROM].size, &error_fatal);
  229. memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
  230. mask_rom);
  231. riscv_find_and_load_firmware(machine, BIOS_FILENAME,
  232. memmap[SPIKE_DRAM].base,
  233. htif_symbol_callback);
  234. if (machine->kernel_filename) {
  235. kernel_entry = riscv_load_kernel(machine->kernel_filename,
  236. htif_symbol_callback);
  237. if (machine->initrd_filename) {
  238. hwaddr start;
  239. hwaddr end = riscv_load_initrd(machine->initrd_filename,
  240. machine->ram_size, kernel_entry,
  241. &start);
  242. qemu_fdt_setprop_cell(s->fdt, "/chosen",
  243. "linux,initrd-start", start);
  244. qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
  245. end);
  246. }
  247. } else {
  248. /*
  249. * If dynamic firmware is used, it doesn't know where is the next mode
  250. * if kernel argument is not set.
  251. */
  252. kernel_entry = 0;
  253. }
  254. /* Compute the fdt load address in dram */
  255. fdt_load_addr = riscv_load_fdt(memmap[SPIKE_DRAM].base,
  256. machine->ram_size, s->fdt);
  257. /* load the reset vector */
  258. riscv_setup_rom_reset_vec(memmap[SPIKE_DRAM].base, memmap[SPIKE_MROM].base,
  259. memmap[SPIKE_MROM].size, kernel_entry,
  260. fdt_load_addr, s->fdt);
  261. /* initialize HTIF using symbols found in load_kernel */
  262. htif_mm_init(system_memory, mask_rom,
  263. &s->soc[0].harts[0].env, serial_hd(0));
  264. }
  265. static void spike_machine_instance_init(Object *obj)
  266. {
  267. }
  268. static void spike_machine_class_init(ObjectClass *oc, void *data)
  269. {
  270. MachineClass *mc = MACHINE_CLASS(oc);
  271. mc->desc = "RISC-V Spike board";
  272. mc->init = spike_board_init;
  273. mc->max_cpus = SPIKE_CPUS_MAX;
  274. mc->is_default = true;
  275. mc->default_cpu_type = SPIKE_V1_10_0_CPU;
  276. mc->possible_cpu_arch_ids = riscv_numa_possible_cpu_arch_ids;
  277. mc->cpu_index_to_instance_props = riscv_numa_cpu_index_to_props;
  278. mc->get_default_cpu_node_id = riscv_numa_get_default_cpu_node_id;
  279. mc->numa_mem_supported = true;
  280. }
  281. static const TypeInfo spike_machine_typeinfo = {
  282. .name = MACHINE_TYPE_NAME("spike"),
  283. .parent = TYPE_MACHINE,
  284. .class_init = spike_machine_class_init,
  285. .instance_init = spike_machine_instance_init,
  286. .instance_size = sizeof(SpikeState),
  287. };
  288. static void spike_machine_init_register_types(void)
  289. {
  290. type_register_static(&spike_machine_typeinfo);
  291. }
  292. type_init(spike_machine_init_register_types)