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