]> git.proxmox.com Git - mirror_qemu.git/blame - hw/riscv/spike.c
target/riscv: Use TARGET_FMT_lx for env->mhartid
[mirror_qemu.git] / hw / riscv / spike.c
CommitLineData
5b4beba1
MC
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)
5b4beba1
MC
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
25#include "qemu/osdep.h"
5b4beba1
MC
26#include "qemu/error-report.h"
27#include "qapi/error.h"
5b4beba1
MC
28#include "hw/boards.h"
29#include "hw/loader.h"
30#include "hw/sysbus.h"
31#include "target/riscv/cpu.h"
5b4beba1 32#include "hw/riscv/riscv_hart.h"
5b4beba1 33#include "hw/riscv/spike.h"
0ac24d56 34#include "hw/riscv/boot.h"
a7172791 35#include "hw/riscv/numa.h"
70eb9f9c 36#include "hw/char/riscv_htif.h"
cc63a182 37#include "hw/intc/riscv_aclint.h"
5b4beba1 38#include "chardev/char.h"
5b4beba1 39#include "sysemu/device_tree.h"
46517dd4 40#include "sysemu/sysemu.h"
5aec3247 41
719b718c
DHB
42#include <libfdt.h>
43
73261285 44static const MemMapEntry spike_memmap[] = {
9eb8b14a 45 [SPIKE_MROM] = { 0x1000, 0xf000 },
8d8897ac 46 [SPIKE_HTIF] = { 0x1000000, 0x1000 },
5b4beba1
MC
47 [SPIKE_CLINT] = { 0x2000000, 0x10000 },
48 [SPIKE_DRAM] = { 0x80000000, 0x0 },
49};
50
73261285 51static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
71d68c48
BM
52 uint64_t mem_size, const char *cmdline,
53 bool is_32_bit, bool htif_custom_base)
5b4beba1
MC
54{
55 void *fdt;
3139929d 56 int fdt_size;
a7172791
AP
57 uint64_t addr, size;
58 unsigned long clint_addr;
59 int cpu, socket;
60 MachineState *mc = MACHINE(s);
61 uint32_t *clint_cells;
62 uint32_t cpu_phandle, intc_phandle, phandle = 1;
63 char *name, *mem_name, *clint_name, *clust_name;
64 char *core_name, *cpu_name, *intc_name;
7cfbb17f
BM
65 static const char * const clint_compat[2] = {
66 "sifive,clint0", "riscv,clint0"
67 };
5b4beba1 68
3139929d 69 fdt = mc->fdt = create_device_tree(&fdt_size);
5b4beba1
MC
70 if (!fdt) {
71 error_report("create_device_tree() failed");
72 exit(1);
73 }
74
75 qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
76 qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
77 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
78 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
79
80 qemu_fdt_add_subnode(fdt, "/htif");
81 qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0");
71d68c48 82 if (htif_custom_base) {
8d8897ac
AP
83 qemu_fdt_setprop_cells(fdt, "/htif", "reg",
84 0x0, memmap[SPIKE_HTIF].base, 0x0, memmap[SPIKE_HTIF].size);
85 }
5b4beba1
MC
86
87 qemu_fdt_add_subnode(fdt, "/soc");
88 qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
117caacf 89 qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
5b4beba1
MC
90 qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
91 qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
92
5b4beba1 93 qemu_fdt_add_subnode(fdt, "/cpus");
2a8756ed 94 qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
b8fb878a 95 RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
5b4beba1
MC
96 qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
97 qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
a7172791
AP
98 qemu_fdt_add_subnode(fdt, "/cpus/cpu-map");
99
100 for (socket = (riscv_socket_count(mc) - 1); socket >= 0; socket--) {
101 clust_name = g_strdup_printf("/cpus/cpu-map/cluster%d", socket);
102 qemu_fdt_add_subnode(fdt, clust_name);
103
104 clint_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
5b4beba1 105
a7172791
AP
106 for (cpu = s->soc[socket].num_harts - 1; cpu >= 0; cpu--) {
107 cpu_phandle = phandle++;
108
109 cpu_name = g_strdup_printf("/cpus/cpu@%d",
110 s->soc[socket].hartid_base + cpu);
111 qemu_fdt_add_subnode(fdt, cpu_name);
bd62c13e
AF
112 if (is_32_bit) {
113 qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv32");
114 } else {
115 qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv48");
116 }
a7172791
AP
117 name = riscv_isa_string(&s->soc[socket].harts[cpu]);
118 qemu_fdt_setprop_string(fdt, cpu_name, "riscv,isa", name);
119 g_free(name);
120 qemu_fdt_setprop_string(fdt, cpu_name, "compatible", "riscv");
121 qemu_fdt_setprop_string(fdt, cpu_name, "status", "okay");
122 qemu_fdt_setprop_cell(fdt, cpu_name, "reg",
123 s->soc[socket].hartid_base + cpu);
124 qemu_fdt_setprop_string(fdt, cpu_name, "device_type", "cpu");
125 riscv_socket_fdt_write_id(mc, fdt, cpu_name, socket);
126 qemu_fdt_setprop_cell(fdt, cpu_name, "phandle", cpu_phandle);
5b4beba1 127
a7172791
AP
128 intc_name = g_strdup_printf("%s/interrupt-controller", cpu_name);
129 qemu_fdt_add_subnode(fdt, intc_name);
130 intc_phandle = phandle++;
131 qemu_fdt_setprop_cell(fdt, intc_name, "phandle", intc_phandle);
132 qemu_fdt_setprop_string(fdt, intc_name, "compatible",
133 "riscv,cpu-intc");
134 qemu_fdt_setprop(fdt, intc_name, "interrupt-controller", NULL, 0);
135 qemu_fdt_setprop_cell(fdt, intc_name, "#interrupt-cells", 1);
136
137 clint_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
138 clint_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
139 clint_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
140 clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
141
142 core_name = g_strdup_printf("%s/core%d", clust_name, cpu);
143 qemu_fdt_add_subnode(fdt, core_name);
144 qemu_fdt_setprop_cell(fdt, core_name, "cpu", cpu_phandle);
145
146 g_free(core_name);
147 g_free(intc_name);
148 g_free(cpu_name);
149 }
150
151 addr = memmap[SPIKE_DRAM].base + riscv_socket_mem_offset(mc, socket);
152 size = riscv_socket_mem_size(mc, socket);
153 mem_name = g_strdup_printf("/memory@%lx", (long)addr);
154 qemu_fdt_add_subnode(fdt, mem_name);
155 qemu_fdt_setprop_cells(fdt, mem_name, "reg",
156 addr >> 32, addr, size >> 32, size);
157 qemu_fdt_setprop_string(fdt, mem_name, "device_type", "memory");
158 riscv_socket_fdt_write_id(mc, fdt, mem_name, socket);
159 g_free(mem_name);
160
161 clint_addr = memmap[SPIKE_CLINT].base +
162 (memmap[SPIKE_CLINT].size * socket);
163 clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr);
164 qemu_fdt_add_subnode(fdt, clint_name);
7cfbb17f
BM
165 qemu_fdt_setprop_string_array(fdt, clint_name, "compatible",
166 (char **)&clint_compat, ARRAY_SIZE(clint_compat));
a7172791
AP
167 qemu_fdt_setprop_cells(fdt, clint_name, "reg",
168 0x0, clint_addr, 0x0, memmap[SPIKE_CLINT].size);
169 qemu_fdt_setprop(fdt, clint_name, "interrupts-extended",
170 clint_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4);
171 riscv_socket_fdt_write_id(mc, fdt, clint_name, socket);
172
173 g_free(clint_name);
174 g_free(clint_cells);
175 g_free(clust_name);
5b4beba1 176 }
a7172791
AP
177
178 riscv_socket_fdt_write_distance_matrix(mc, fdt);
5b4beba1 179
6d3b9c02
BM
180 qemu_fdt_add_subnode(fdt, "/chosen");
181 qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", "/htif");
cd69e3a6
AF
182}
183
71d68c48
BM
184static bool spike_test_elf_image(char *filename)
185{
186 Error *err = NULL;
187
188 load_elf_hdr(filename, NULL, NULL, &err);
189 if (err) {
190 error_free(err);
191 return false;
192 } else {
193 return true;
194 }
195}
196
cd69e3a6
AF
197static void spike_board_init(MachineState *machine)
198{
73261285 199 const MemMapEntry *memmap = spike_memmap;
a7172791 200 SpikeState *s = SPIKE_MACHINE(machine);
cd69e3a6 201 MemoryRegion *system_memory = get_system_memory();
cd69e3a6 202 MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
71d68c48
BM
203 target_ulong firmware_end_addr = memmap[SPIKE_DRAM].base;
204 target_ulong kernel_start_addr;
205 char *firmware_name;
66b1205b 206 uint32_t fdt_load_addr;
dc144fe1 207 uint64_t kernel_entry;
a7172791
AP
208 char *soc_name;
209 int i, base_hartid, hart_count;
71d68c48 210 bool htif_custom_base = false;
cd69e3a6 211
a7172791
AP
212 /* Check socket count limit */
213 if (SPIKE_SOCKETS_MAX < riscv_socket_count(machine)) {
214 error_report("number of sockets/nodes should be less than %d",
215 SPIKE_SOCKETS_MAX);
216 exit(1);
217 }
218
219 /* Initialize sockets */
220 for (i = 0; i < riscv_socket_count(machine); i++) {
221 if (!riscv_socket_check_hartids(machine, i)) {
222 error_report("discontinuous hartids in socket%d", i);
223 exit(1);
224 }
225
226 base_hartid = riscv_socket_first_hartid(machine, i);
227 if (base_hartid < 0) {
228 error_report("can't find hartid base for socket%d", i);
229 exit(1);
230 }
231
232 hart_count = riscv_socket_hart_count(machine, i);
233 if (hart_count < 0) {
234 error_report("can't find hart count for socket%d", i);
235 exit(1);
236 }
237
238 soc_name = g_strdup_printf("soc%d", i);
239 object_initialize_child(OBJECT(machine), soc_name, &s->soc[i],
240 TYPE_RISCV_HART_ARRAY);
241 g_free(soc_name);
242 object_property_set_str(OBJECT(&s->soc[i]), "cpu-type",
243 machine->cpu_type, &error_abort);
244 object_property_set_int(OBJECT(&s->soc[i]), "hartid-base",
245 base_hartid, &error_abort);
246 object_property_set_int(OBJECT(&s->soc[i]), "num-harts",
247 hart_count, &error_abort);
4bcfc391 248 sysbus_realize(SYS_BUS_DEVICE(&s->soc[i]), &error_fatal);
a7172791
AP
249
250 /* Core Local Interruptor (timer and IPI) for each socket */
b8fb878a 251 riscv_aclint_swi_create(
a7172791 252 memmap[SPIKE_CLINT].base + i * memmap[SPIKE_CLINT].size,
b8fb878a
AP
253 base_hartid, hart_count, false);
254 riscv_aclint_mtimer_create(
255 memmap[SPIKE_CLINT].base + i * memmap[SPIKE_CLINT].size +
256 RISCV_ACLINT_SWI_SIZE,
257 RISCV_ACLINT_DEFAULT_MTIMER_SIZE, base_hartid, hart_count,
258 RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME,
259 RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ, false);
a7172791 260 }
cd69e3a6
AF
261
262 /* register system main memory (actual RAM) */
cd69e3a6 263 memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
11ec06f9 264 machine->ram);
cd69e3a6 265
cd69e3a6
AF
266 /* boot rom */
267 memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
268 memmap[SPIKE_MROM].size, &error_fatal);
269 memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
270 mask_rom);
271
71d68c48
BM
272 /* Find firmware */
273 firmware_name = riscv_find_firmware(machine->firmware,
274 riscv_default_firmware_name(&s->soc[0]));
275
276 /*
277 * Test the given firmware or kernel file to see if it is an ELF image.
278 * If it is an ELF, we assume it contains the symbols required for
279 * the HTIF console, otherwise we fall back to use the custom base
280 * passed from device tree for the HTIF console.
281 */
282 if (!firmware_name && !machine->kernel_filename) {
283 htif_custom_base = true;
284 } else {
285 if (firmware_name) {
286 htif_custom_base = !spike_test_elf_image(firmware_name);
287 }
288 if (!htif_custom_base && machine->kernel_filename) {
289 htif_custom_base = !spike_test_elf_image(machine->kernel_filename);
290 }
291 }
292
293 /* Load firmware */
294 if (firmware_name) {
295 firmware_end_addr = riscv_load_firmware(firmware_name,
296 memmap[SPIKE_DRAM].base,
297 htif_symbol_callback);
298 g_free(firmware_name);
299 }
5b8a9863 300
c44df400
DHB
301 /* Create device tree */
302 create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline,
303 riscv_is_32bit(&s->soc[0]), htif_custom_base);
304
8d8897ac 305 /* Load kernel */
cd69e3a6 306 if (machine->kernel_filename) {
a8259b53 307 kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc[0],
38bc4e34
AF
308 firmware_end_addr);
309
60c1f05e 310 kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
dc144fe1 311 htif_symbol_callback);
c44df400
DHB
312
313 if (machine->initrd_filename) {
1f991461 314 riscv_load_initrd(machine, kernel_entry);
c44df400 315 }
b1f19f23
DHB
316
317 if (machine->kernel_cmdline && *machine->kernel_cmdline) {
318 qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
319 machine->kernel_cmdline);
320 }
dc144fe1
AP
321 } else {
322 /*
323 * If dynamic firmware is used, it doesn't know where is the next mode
324 * if kernel argument is not set.
325 */
326 kernel_entry = 0;
cd69e3a6
AF
327 }
328
66b1205b
AP
329 /* Compute the fdt load address in dram */
330 fdt_load_addr = riscv_load_fdt(memmap[SPIKE_DRAM].base,
3139929d 331 machine->ram_size, machine->fdt);
719b718c 332
43cf723a 333 /* load the reset vector */
a8259b53 334 riscv_setup_rom_reset_vec(machine, &s->soc[0], memmap[SPIKE_DRAM].base,
78936771 335 memmap[SPIKE_MROM].base,
dc144fe1 336 memmap[SPIKE_MROM].size, kernel_entry,
6934f15b 337 fdt_load_addr);
cd69e3a6
AF
338
339 /* initialize HTIF using symbols found in load_kernel */
71d68c48
BM
340 htif_mm_init(system_memory, serial_hd(0), memmap[SPIKE_HTIF].base,
341 htif_custom_base);
a7172791 342}
cd69e3a6 343
a7172791
AP
344static void spike_machine_instance_init(Object *obj)
345{
cd69e3a6 346}
5b4beba1 347
a7172791 348static void spike_machine_class_init(ObjectClass *oc, void *data)
cd69e3a6 349{
a7172791
AP
350 MachineClass *mc = MACHINE_CLASS(oc);
351
352 mc->desc = "RISC-V Spike board";
cd69e3a6 353 mc->init = spike_board_init;
a7172791 354 mc->max_cpus = SPIKE_CPUS_MAX;
ea0ac7f6 355 mc->is_default = true;
dc4d4aae 356 mc->default_cpu_type = TYPE_RISCV_CPU_BASE;
a7172791
AP
357 mc->possible_cpu_arch_ids = riscv_numa_possible_cpu_arch_ids;
358 mc->cpu_index_to_instance_props = riscv_numa_cpu_index_to_props;
359 mc->get_default_cpu_node_id = riscv_numa_get_default_cpu_node_id;
360 mc->numa_mem_supported = true;
11ec06f9 361 mc->default_ram_id = "riscv.spike.ram";
a7172791
AP
362}
363
364static const TypeInfo spike_machine_typeinfo = {
365 .name = MACHINE_TYPE_NAME("spike"),
366 .parent = TYPE_MACHINE,
367 .class_init = spike_machine_class_init,
368 .instance_init = spike_machine_instance_init,
369 .instance_size = sizeof(SpikeState),
370};
371
372static void spike_machine_init_register_types(void)
373{
374 type_register_static(&spike_machine_typeinfo);
5b4beba1
MC
375}
376
a7172791 377type_init(spike_machine_init_register_types)