xref: /openbmc/qemu/hw/riscv/spike.c (revision 65a117da)
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 
26 #include "qemu/osdep.h"
27 #include "qemu/log.h"
28 #include "qemu/error-report.h"
29 #include "qapi/error.h"
30 #include "hw/boards.h"
31 #include "hw/loader.h"
32 #include "hw/sysbus.h"
33 #include "target/riscv/cpu.h"
34 #include "hw/riscv/riscv_htif.h"
35 #include "hw/riscv/riscv_hart.h"
36 #include "hw/riscv/sifive_clint.h"
37 #include "hw/riscv/spike.h"
38 #include "hw/riscv/boot.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 #include "exec/address-spaces.h"
45 
46 #include <libfdt.h>
47 
48 #if defined(TARGET_RISCV32)
49 # define BIOS_FILENAME "opensbi-riscv32-spike-fw_jump.elf"
50 #else
51 # define BIOS_FILENAME "opensbi-riscv64-spike-fw_jump.elf"
52 #endif
53 
54 static const struct MemmapEntry {
55     hwaddr base;
56     hwaddr size;
57 } spike_memmap[] = {
58     [SPIKE_MROM] =     {     0x1000,    0x11000 },
59     [SPIKE_CLINT] =    {  0x2000000,    0x10000 },
60     [SPIKE_DRAM] =     { 0x80000000,        0x0 },
61 };
62 
63 static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap,
64     uint64_t mem_size, const char *cmdline)
65 {
66     void *fdt;
67     int cpu;
68     uint32_t *cells;
69     char *nodename;
70 
71     fdt = s->fdt = create_device_tree(&s->fdt_size);
72     if (!fdt) {
73         error_report("create_device_tree() failed");
74         exit(1);
75     }
76 
77     qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu");
78     qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev");
79     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
80     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
81 
82     qemu_fdt_add_subnode(fdt, "/htif");
83     qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0");
84 
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 
91     nodename = g_strdup_printf("/memory@%lx",
92         (long)memmap[SPIKE_DRAM].base);
93     qemu_fdt_add_subnode(fdt, nodename);
94     qemu_fdt_setprop_cells(fdt, nodename, "reg",
95         memmap[SPIKE_DRAM].base >> 32, memmap[SPIKE_DRAM].base,
96         mem_size >> 32, mem_size);
97     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
98     g_free(nodename);
99 
100     qemu_fdt_add_subnode(fdt, "/cpus");
101     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
102         SIFIVE_CLINT_TIMEBASE_FREQ);
103     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
104     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
105 
106     for (cpu = s->soc.num_harts - 1; cpu >= 0; cpu--) {
107         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
108         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
109         char *isa = riscv_isa_string(&s->soc.harts[cpu]);
110         qemu_fdt_add_subnode(fdt, nodename);
111 #if defined(TARGET_RISCV32)
112         qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
113 #else
114         qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
115 #endif
116         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
117         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
118         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
119         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
120         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
121         qemu_fdt_add_subnode(fdt, intc);
122         qemu_fdt_setprop_cell(fdt, intc, "phandle", 1);
123         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
124         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
125         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
126         g_free(isa);
127         g_free(intc);
128         g_free(nodename);
129     }
130 
131     cells =  g_new0(uint32_t, s->soc.num_harts * 4);
132     for (cpu = 0; cpu < s->soc.num_harts; cpu++) {
133         nodename =
134             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
135         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
136         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
137         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
138         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
139         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
140         g_free(nodename);
141     }
142     nodename = g_strdup_printf("/soc/clint@%lx",
143         (long)memmap[SPIKE_CLINT].base);
144     qemu_fdt_add_subnode(fdt, nodename);
145     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
146     qemu_fdt_setprop_cells(fdt, nodename, "reg",
147         0x0, memmap[SPIKE_CLINT].base,
148         0x0, memmap[SPIKE_CLINT].size);
149     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
150         cells, s->soc.num_harts * sizeof(uint32_t) * 4);
151     g_free(cells);
152     g_free(nodename);
153 
154     if (cmdline) {
155         qemu_fdt_add_subnode(fdt, "/chosen");
156         qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
157     }
158 }
159 
160 static void spike_board_init(MachineState *machine)
161 {
162     const struct MemmapEntry *memmap = spike_memmap;
163 
164     SpikeState *s = g_new0(SpikeState, 1);
165     MemoryRegion *system_memory = get_system_memory();
166     MemoryRegion *main_mem = g_new(MemoryRegion, 1);
167     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
168     int i;
169     unsigned int smp_cpus = machine->smp.cpus;
170 
171     /* Initialize SOC */
172     object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc),
173                             TYPE_RISCV_HART_ARRAY, &error_abort, NULL);
174     object_property_set_str(OBJECT(&s->soc), machine->cpu_type, "cpu-type",
175                             &error_abort);
176     object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts",
177                             &error_abort);
178     object_property_set_bool(OBJECT(&s->soc), true, "realized",
179                             &error_abort);
180 
181     /* register system main memory (actual RAM) */
182     memory_region_init_ram(main_mem, NULL, "riscv.spike.ram",
183                            machine->ram_size, &error_fatal);
184     memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base,
185         main_mem);
186 
187     /* create device tree */
188     create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
189 
190     /* boot rom */
191     memory_region_init_rom(mask_rom, NULL, "riscv.spike.mrom",
192                            memmap[SPIKE_MROM].size, &error_fatal);
193     memory_region_add_subregion(system_memory, memmap[SPIKE_MROM].base,
194                                 mask_rom);
195 
196     riscv_find_and_load_firmware(machine, BIOS_FILENAME,
197                                  memmap[SPIKE_DRAM].base,
198                                  htif_symbol_callback);
199 
200     if (machine->kernel_filename) {
201         uint64_t kernel_entry = riscv_load_kernel(machine->kernel_filename,
202                                                   htif_symbol_callback);
203 
204         if (machine->initrd_filename) {
205             hwaddr start;
206             hwaddr end = riscv_load_initrd(machine->initrd_filename,
207                                            machine->ram_size, kernel_entry,
208                                            &start);
209             qemu_fdt_setprop_cell(s->fdt, "/chosen",
210                                   "linux,initrd-start", start);
211             qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
212                                   end);
213         }
214     }
215 
216     /* reset vector */
217     uint32_t reset_vec[8] = {
218         0x00000297,                  /* 1:  auipc  t0, %pcrel_hi(dtb) */
219         0x02028593,                  /*     addi   a1, t0, %pcrel_lo(1b) */
220         0xf1402573,                  /*     csrr   a0, mhartid  */
221 #if defined(TARGET_RISCV32)
222         0x0182a283,                  /*     lw     t0, 24(t0) */
223 #elif defined(TARGET_RISCV64)
224         0x0182b283,                  /*     ld     t0, 24(t0) */
225 #endif
226         0x00028067,                  /*     jr     t0 */
227         0x00000000,
228         memmap[SPIKE_DRAM].base,     /* start: .dword DRAM_BASE */
229         0x00000000,
230                                      /* dtb: */
231     };
232 
233     /* copy in the reset vector in little_endian byte order */
234     for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
235         reset_vec[i] = cpu_to_le32(reset_vec[i]);
236     }
237     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
238                           memmap[SPIKE_MROM].base, &address_space_memory);
239 
240     /* copy in the device tree */
241     if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) >
242             memmap[SPIKE_MROM].size - sizeof(reset_vec)) {
243         error_report("not enough space to store device-tree");
244         exit(1);
245     }
246     qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt));
247     rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt),
248                           memmap[SPIKE_MROM].base + sizeof(reset_vec),
249                           &address_space_memory);
250 
251     /* initialize HTIF using symbols found in load_kernel */
252     htif_mm_init(system_memory, mask_rom, &s->soc.harts[0].env, serial_hd(0));
253 
254     /* Core Local Interruptor (timer and IPI) */
255     sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size,
256         smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
257         false);
258 }
259 
260 static void spike_machine_init(MachineClass *mc)
261 {
262     mc->desc = "RISC-V Spike Board";
263     mc->init = spike_board_init;
264     mc->max_cpus = 8;
265     mc->is_default = true;
266     mc->default_cpu_type = SPIKE_V1_10_0_CPU;
267 }
268 
269 DEFINE_MACHINE("spike", spike_machine_init)
270