spike.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2 or later, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/error-report.h"
  26. #include "qapi/error.h"
  27. #include "hw/boards.h"
  28. #include "hw/loader.h"
  29. #include "hw/sysbus.h"
  30. #include "target/riscv/cpu.h"
  31. #include "hw/riscv/riscv_hart.h"
  32. #include "hw/riscv/spike.h"
  33. #include "hw/riscv/boot.h"
  34. #include "hw/riscv/numa.h"
  35. #include "hw/char/riscv_htif.h"
  36. #include "hw/intc/riscv_aclint.h"
  37. #include "chardev/char.h"
  38. #include "system/device_tree.h"
  39. #include "system/system.h"
  40. #include <libfdt.h>
  41. static const MemMapEntry spike_memmap[] = {
  42. [SPIKE_MROM] = { 0x1000, 0xf000 },
  43. [SPIKE_HTIF] = { 0x1000000, 0x1000 },
  44. [SPIKE_CLINT] = { 0x2000000, 0x10000 },
  45. [SPIKE_DRAM] = { 0x80000000, 0x0 },
  46. };
  47. static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
  48. bool is_32_bit, bool htif_custom_base)
  49. {
  50. void *fdt;
  51. int fdt_size;
  52. uint64_t addr, size;
  53. unsigned long clint_addr;
  54. int cpu, socket;
  55. MachineState *ms = MACHINE(s);
  56. uint32_t *clint_cells;
  57. uint32_t cpu_phandle, intc_phandle, phandle = 1;
  58. char *mem_name, *clint_name, *clust_name;
  59. char *core_name, *cpu_name, *intc_name;
  60. static const char * const clint_compat[2] = {
  61. "sifive,clint0", "riscv,clint0"
  62. };
  63. fdt = ms->fdt = create_device_tree(&fdt_size);
  64. if (!fdt) {
  65. error_report("create_device_tree() failed");
  66. exit(1);
  67. }
  68. qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
  69. qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
  70. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  71. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  72. qemu_fdt_add_subnode(fdt, "/htif");
  73. qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0");
  74. if (htif_custom_base) {
  75. qemu_fdt_setprop_cells(fdt, "/htif", "reg",
  76. 0x0, memmap[SPIKE_HTIF].base, 0x0, memmap[SPIKE_HTIF].size);
  77. }
  78. qemu_fdt_add_subnode(fdt, "/soc");
  79. qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
  80. qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
  81. qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
  82. qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
  83. qemu_fdt_add_subnode(fdt, "/cpus");
  84. qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
  85. RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
  86. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
  87. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
  88. qemu_fdt_add_subnode(fdt, "/cpus/cpu-map");
  89. for (socket = (riscv_socket_count(ms) - 1); socket >= 0; socket--) {
  90. clust_name = g_strdup_printf("/cpus/cpu-map/cluster%d", socket);
  91. qemu_fdt_add_subnode(fdt, clust_name);
  92. clint_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
  93. for (cpu = s->soc[socket].num_harts - 1; cpu >= 0; cpu--) {
  94. cpu_phandle = phandle++;
  95. cpu_name = g_strdup_printf("/cpus/cpu@%d",
  96. s->soc[socket].hartid_base + cpu);
  97. qemu_fdt_add_subnode(fdt, cpu_name);
  98. if (is_32_bit) {
  99. qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv32");
  100. } else {
  101. qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv48");
  102. }
  103. riscv_isa_write_fdt(&s->soc[socket].harts[cpu], fdt, cpu_name);
  104. qemu_fdt_setprop_string(fdt, cpu_name, "compatible", "riscv");
  105. qemu_fdt_setprop_string(fdt, cpu_name, "status", "okay");
  106. qemu_fdt_setprop_cell(fdt, cpu_name, "reg",
  107. s->soc[socket].hartid_base + cpu);
  108. qemu_fdt_setprop_string(fdt, cpu_name, "device_type", "cpu");
  109. riscv_socket_fdt_write_id(ms, cpu_name, socket);
  110. qemu_fdt_setprop_cell(fdt, cpu_name, "phandle", cpu_phandle);
  111. intc_name = g_strdup_printf("%s/interrupt-controller", cpu_name);
  112. qemu_fdt_add_subnode(fdt, intc_name);
  113. intc_phandle = phandle++;
  114. qemu_fdt_setprop_cell(fdt, intc_name, "phandle", intc_phandle);
  115. qemu_fdt_setprop_string(fdt, intc_name, "compatible",
  116. "riscv,cpu-intc");
  117. qemu_fdt_setprop(fdt, intc_name, "interrupt-controller", NULL, 0);
  118. qemu_fdt_setprop_cell(fdt, intc_name, "#interrupt-cells", 1);
  119. clint_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  120. clint_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
  121. clint_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
  122. clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
  123. core_name = g_strdup_printf("%s/core%d", clust_name, cpu);
  124. qemu_fdt_add_subnode(fdt, core_name);
  125. qemu_fdt_setprop_cell(fdt, core_name, "cpu", cpu_phandle);
  126. g_free(core_name);
  127. g_free(intc_name);
  128. g_free(cpu_name);
  129. }
  130. addr = memmap[SPIKE_DRAM].base + riscv_socket_mem_offset(ms, socket);
  131. size = riscv_socket_mem_size(ms, socket);
  132. mem_name = g_strdup_printf("/memory@%lx", (long)addr);
  133. qemu_fdt_add_subnode(fdt, mem_name);
  134. qemu_fdt_setprop_cells(fdt, mem_name, "reg",
  135. addr >> 32, addr, size >> 32, size);
  136. qemu_fdt_setprop_string(fdt, mem_name, "device_type", "memory");
  137. riscv_socket_fdt_write_id(ms, mem_name, socket);
  138. g_free(mem_name);
  139. clint_addr = memmap[SPIKE_CLINT].base +
  140. (memmap[SPIKE_CLINT].size * socket);
  141. clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr);
  142. qemu_fdt_add_subnode(fdt, clint_name);
  143. qemu_fdt_setprop_string_array(fdt, clint_name, "compatible",
  144. (char **)&clint_compat, ARRAY_SIZE(clint_compat));
  145. qemu_fdt_setprop_cells(fdt, clint_name, "reg",
  146. 0x0, clint_addr, 0x0, memmap[SPIKE_CLINT].size);
  147. qemu_fdt_setprop(fdt, clint_name, "interrupts-extended",
  148. clint_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4);
  149. riscv_socket_fdt_write_id(ms, clint_name, socket);
  150. g_free(clint_name);
  151. g_free(clint_cells);
  152. g_free(clust_name);
  153. }
  154. riscv_socket_fdt_write_distance_matrix(ms);
  155. qemu_fdt_add_subnode(fdt, "/chosen");
  156. qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", "/htif");
  157. }
  158. static bool spike_test_elf_image(char *filename)
  159. {
  160. Error *err = NULL;
  161. load_elf_hdr(filename, NULL, NULL, &err);
  162. if (err) {
  163. error_free(err);
  164. return false;
  165. } else {
  166. return true;
  167. }
  168. }
  169. static void spike_board_init(MachineState *machine)
  170. {
  171. const MemMapEntry *memmap = spike_memmap;
  172. SpikeState *s = SPIKE_MACHINE(machine);
  173. MemoryRegion *system_memory = get_system_memory();
  174. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  175. target_ulong firmware_end_addr = memmap[SPIKE_DRAM].base;
  176. hwaddr firmware_load_addr = memmap[SPIKE_DRAM].base;
  177. target_ulong kernel_start_addr;
  178. char *firmware_name;
  179. uint64_t fdt_load_addr;
  180. uint64_t kernel_entry;
  181. char *soc_name;
  182. int i, base_hartid, hart_count;
  183. bool htif_custom_base = false;
  184. RISCVBootInfo boot_info;
  185. /* Check socket count limit */
  186. if (SPIKE_SOCKETS_MAX < riscv_socket_count(machine)) {
  187. error_report("number of sockets/nodes should be less than %d",
  188. SPIKE_SOCKETS_MAX);
  189. exit(1);
  190. }
  191. /* Initialize sockets */
  192. for (i = 0; i < riscv_socket_count(machine); i++) {
  193. if (!riscv_socket_check_hartids(machine, i)) {
  194. error_report("discontinuous hartids in socket%d", i);
  195. exit(1);
  196. }
  197. base_hartid = riscv_socket_first_hartid(machine, i);
  198. if (base_hartid < 0) {
  199. error_report("can't find hartid base for socket%d", i);
  200. exit(1);
  201. }
  202. hart_count = riscv_socket_hart_count(machine, i);
  203. if (hart_count < 0) {
  204. error_report("can't find hart count for socket%d", i);
  205. exit(1);
  206. }
  207. soc_name = g_strdup_printf("soc%d", i);
  208. object_initialize_child(OBJECT(machine), soc_name, &s->soc[i],
  209. TYPE_RISCV_HART_ARRAY);
  210. g_free(soc_name);
  211. object_property_set_str(OBJECT(&s->soc[i]), "cpu-type",
  212. machine->cpu_type, &error_abort);
  213. object_property_set_int(OBJECT(&s->soc[i]), "hartid-base",
  214. base_hartid, &error_abort);
  215. object_property_set_int(OBJECT(&s->soc[i]), "num-harts",
  216. hart_count, &error_abort);
  217. sysbus_realize(SYS_BUS_DEVICE(&s->soc[i]), &error_fatal);
  218. /* Core Local Interruptor (timer and IPI) for each socket */
  219. riscv_aclint_swi_create(
  220. memmap[SPIKE_CLINT].base + i * memmap[SPIKE_CLINT].size,
  221. base_hartid, hart_count, false);
  222. riscv_aclint_mtimer_create(
  223. memmap[SPIKE_CLINT].base + i * memmap[SPIKE_CLINT].size +
  224. RISCV_ACLINT_SWI_SIZE,
  225. RISCV_ACLINT_DEFAULT_MTIMER_SIZE, base_hartid, hart_count,
  226. RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME,
  227. RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ, false);
  228. }
  229. /* register system main memory (actual RAM) */
  230. memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
  231. machine->ram);
  232. /* boot rom */
  233. memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
  234. memmap[SPIKE_MROM].size, &error_fatal);
  235. memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
  236. mask_rom);
  237. /* Find firmware */
  238. firmware_name = riscv_find_firmware(machine->firmware,
  239. riscv_default_firmware_name(&s->soc[0]));
  240. /*
  241. * Test the given firmware or kernel file to see if it is an ELF image.
  242. * If it is an ELF, we assume it contains the symbols required for
  243. * the HTIF console, otherwise we fall back to use the custom base
  244. * passed from device tree for the HTIF console.
  245. */
  246. if (!firmware_name && !machine->kernel_filename) {
  247. htif_custom_base = true;
  248. } else {
  249. if (firmware_name) {
  250. htif_custom_base = !spike_test_elf_image(firmware_name);
  251. }
  252. if (!htif_custom_base && machine->kernel_filename) {
  253. htif_custom_base = !spike_test_elf_image(machine->kernel_filename);
  254. }
  255. }
  256. /* Load firmware */
  257. if (firmware_name) {
  258. firmware_end_addr = riscv_load_firmware(firmware_name,
  259. &firmware_load_addr,
  260. htif_symbol_callback);
  261. g_free(firmware_name);
  262. }
  263. /* Create device tree */
  264. create_fdt(s, memmap, riscv_is_32bit(&s->soc[0]), htif_custom_base);
  265. /* Load kernel */
  266. riscv_boot_info_init(&boot_info, &s->soc[0]);
  267. if (machine->kernel_filename) {
  268. kernel_start_addr = riscv_calc_kernel_start_addr(&boot_info,
  269. firmware_end_addr);
  270. riscv_load_kernel(machine, &boot_info, kernel_start_addr,
  271. true, htif_symbol_callback);
  272. kernel_entry = boot_info.image_low_addr;
  273. } else {
  274. /*
  275. * If dynamic firmware is used, it doesn't know where is the next mode
  276. * if kernel argument is not set.
  277. */
  278. kernel_entry = 0;
  279. }
  280. fdt_load_addr = riscv_compute_fdt_addr(memmap[SPIKE_DRAM].base,
  281. memmap[SPIKE_DRAM].size,
  282. machine, &boot_info);
  283. riscv_load_fdt(fdt_load_addr, machine->fdt);
  284. /* load the reset vector */
  285. riscv_setup_rom_reset_vec(machine, &s->soc[0], firmware_load_addr,
  286. memmap[SPIKE_MROM].base,
  287. memmap[SPIKE_MROM].size, kernel_entry,
  288. fdt_load_addr);
  289. /* initialize HTIF using symbols found in load_kernel */
  290. htif_mm_init(system_memory, serial_hd(0), memmap[SPIKE_HTIF].base,
  291. htif_custom_base);
  292. }
  293. static void spike_set_signature(Object *obj, const char *val, Error **errp)
  294. {
  295. sig_file = g_strdup(val);
  296. }
  297. static void spike_machine_instance_init(Object *obj)
  298. {
  299. }
  300. static void spike_machine_class_init(ObjectClass *oc, void *data)
  301. {
  302. MachineClass *mc = MACHINE_CLASS(oc);
  303. mc->desc = "RISC-V Spike board";
  304. mc->init = spike_board_init;
  305. mc->max_cpus = SPIKE_CPUS_MAX;
  306. mc->is_default = true;
  307. mc->default_cpu_type = TYPE_RISCV_CPU_BASE;
  308. mc->possible_cpu_arch_ids = riscv_numa_possible_cpu_arch_ids;
  309. mc->cpu_index_to_instance_props = riscv_numa_cpu_index_to_props;
  310. mc->get_default_cpu_node_id = riscv_numa_get_default_cpu_node_id;
  311. mc->numa_mem_supported = true;
  312. /* platform instead of architectural choice */
  313. mc->cpu_cluster_has_numa_boundary = true;
  314. mc->default_ram_id = "riscv.spike.ram";
  315. object_class_property_add_str(oc, "signature", NULL, spike_set_signature);
  316. object_class_property_set_description(oc, "signature",
  317. "File to write ACT test signature");
  318. object_class_property_add_uint8_ptr(oc, "signature-granularity",
  319. &line_size, OBJ_PROP_FLAG_WRITE);
  320. object_class_property_set_description(oc, "signature-granularity",
  321. "Size of each line in ACT signature "
  322. "file");
  323. }
  324. static const TypeInfo spike_machine_typeinfo = {
  325. .name = MACHINE_TYPE_NAME("spike"),
  326. .parent = TYPE_MACHINE,
  327. .class_init = spike_machine_class_init,
  328. .instance_init = spike_machine_instance_init,
  329. .instance_size = sizeof(SpikeState),
  330. };
  331. static void spike_machine_init_register_types(void)
  332. {
  333. type_register_static(&spike_machine_typeinfo);
  334. }
  335. type_init(spike_machine_init_register_types)