]>
Commit | Line | Data |
---|---|---|
a7240d1e MC |
1 | /* |
2 | * QEMU RISC-V Board Compatible with SiFive Freedom U SDK | |
3 | * | |
4 | * Copyright (c) 2016-2017 Sagar Karandikar, [email protected] | |
5 | * Copyright (c) 2017 SiFive, Inc. | |
6 | * | |
7 | * Provides a board compatible with the SiFive Freedom U SDK: | |
8 | * | |
9 | * 0) UART | |
10 | * 1) CLINT (Core Level Interruptor) | |
11 | * 2) PLIC (Platform Level Interrupt Controller) | |
12 | * | |
f3d47d58 | 13 | * This board currently generates devicetree dynamically that indicates at least |
ecdfe393 | 14 | * two harts and up to five harts. |
a7240d1e MC |
15 | * |
16 | * This program is free software; you can redistribute it and/or modify it | |
17 | * under the terms and conditions of the GNU General Public License, | |
18 | * version 2 or later, as published by the Free Software Foundation. | |
19 | * | |
20 | * This program is distributed in the hope it will be useful, but WITHOUT | |
21 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
22 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
23 | * more details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License along with | |
26 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
27 | */ | |
28 | ||
29 | #include "qemu/osdep.h" | |
30 | #include "qemu/log.h" | |
31 | #include "qemu/error-report.h" | |
32 | #include "qapi/error.h" | |
a7240d1e MC |
33 | #include "hw/boards.h" |
34 | #include "hw/loader.h" | |
35 | #include "hw/sysbus.h" | |
36 | #include "hw/char/serial.h" | |
ecdfe393 | 37 | #include "hw/cpu/cluster.h" |
a7240d1e MC |
38 | #include "target/riscv/cpu.h" |
39 | #include "hw/riscv/riscv_hart.h" | |
40 | #include "hw/riscv/sifive_plic.h" | |
41 | #include "hw/riscv/sifive_clint.h" | |
42 | #include "hw/riscv/sifive_uart.h" | |
a7240d1e | 43 | #include "hw/riscv/sifive_u.h" |
0ac24d56 | 44 | #include "hw/riscv/boot.h" |
a7240d1e MC |
45 | #include "chardev/char.h" |
46 | #include "sysemu/arch_init.h" | |
47 | #include "sysemu/device_tree.h" | |
46517dd4 | 48 | #include "sysemu/sysemu.h" |
a7240d1e | 49 | #include "exec/address-spaces.h" |
a7240d1e | 50 | |
5aec3247 MC |
51 | #include <libfdt.h> |
52 | ||
fdd1bda4 AF |
53 | #define BIOS_FILENAME "opensbi-riscv64-sifive_u-fw_jump.bin" |
54 | ||
a7240d1e MC |
55 | static const struct MemmapEntry { |
56 | hwaddr base; | |
57 | hwaddr size; | |
58 | } sifive_u_memmap[] = { | |
59 | [SIFIVE_U_DEBUG] = { 0x0, 0x100 }, | |
5aec3247 | 60 | [SIFIVE_U_MROM] = { 0x1000, 0x11000 }, |
a7240d1e MC |
61 | [SIFIVE_U_CLINT] = { 0x2000000, 0x10000 }, |
62 | [SIFIVE_U_PLIC] = { 0xc000000, 0x4000000 }, | |
63 | [SIFIVE_U_UART0] = { 0x10013000, 0x1000 }, | |
64 | [SIFIVE_U_UART1] = { 0x10023000, 0x1000 }, | |
65 | [SIFIVE_U_DRAM] = { 0x80000000, 0x0 }, | |
5a7f76a3 | 66 | [SIFIVE_U_GEM] = { 0x100900FC, 0x2000 }, |
a7240d1e MC |
67 | }; |
68 | ||
5a7f76a3 AF |
69 | #define GEM_REVISION 0x10070109 |
70 | ||
9f79638e | 71 | static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, |
a7240d1e MC |
72 | uint64_t mem_size, const char *cmdline) |
73 | { | |
ecdfe393 | 74 | MachineState *ms = MACHINE(qdev_get_machine()); |
a7240d1e MC |
75 | void *fdt; |
76 | int cpu; | |
77 | uint32_t *cells; | |
78 | char *nodename; | |
fe93582c | 79 | char ethclk_names[] = "pclk\0hclk\0tx_clk"; |
382cb439 | 80 | uint32_t plic_phandle, ethclk_phandle, phandle = 1; |
44e6dcd3 | 81 | uint32_t uartclk_phandle; |
e1724d09 | 82 | uint32_t hfclk_phandle, rtcclk_phandle; |
a7240d1e MC |
83 | |
84 | fdt = s->fdt = create_device_tree(&s->fdt_size); | |
85 | if (!fdt) { | |
86 | error_report("create_device_tree() failed"); | |
87 | exit(1); | |
88 | } | |
89 | ||
90 | qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu"); | |
91 | qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev"); | |
92 | qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); | |
93 | qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); | |
94 | ||
95 | qemu_fdt_add_subnode(fdt, "/soc"); | |
96 | qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); | |
2a1a6f6d | 97 | qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); |
a7240d1e MC |
98 | qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); |
99 | qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); | |
100 | ||
e1724d09 BM |
101 | hfclk_phandle = phandle++; |
102 | nodename = g_strdup_printf("/hfclk"); | |
103 | qemu_fdt_add_subnode(fdt, nodename); | |
104 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle); | |
105 | qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk"); | |
106 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
107 | SIFIVE_U_HFCLK_FREQ); | |
108 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
109 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
110 | g_free(nodename); | |
111 | ||
112 | rtcclk_phandle = phandle++; | |
113 | nodename = g_strdup_printf("/rtcclk"); | |
114 | qemu_fdt_add_subnode(fdt, nodename); | |
115 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle); | |
116 | qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk"); | |
117 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
118 | SIFIVE_U_RTCCLK_FREQ); | |
119 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
120 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
121 | g_free(nodename); | |
122 | ||
a7240d1e MC |
123 | nodename = g_strdup_printf("/memory@%lx", |
124 | (long)memmap[SIFIVE_U_DRAM].base); | |
125 | qemu_fdt_add_subnode(fdt, nodename); | |
126 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
127 | memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base, | |
128 | mem_size >> 32, mem_size); | |
129 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); | |
130 | g_free(nodename); | |
131 | ||
132 | qemu_fdt_add_subnode(fdt, "/cpus"); | |
2a8756ed MC |
133 | qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", |
134 | SIFIVE_CLINT_TIMEBASE_FREQ); | |
a7240d1e MC |
135 | qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); |
136 | qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); | |
137 | ||
ecdfe393 | 138 | for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) { |
382cb439 | 139 | int cpu_phandle = phandle++; |
a7240d1e MC |
140 | nodename = g_strdup_printf("/cpus/cpu@%d", cpu); |
141 | char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
ecdfe393 | 142 | char *isa; |
a7240d1e | 143 | qemu_fdt_add_subnode(fdt, nodename); |
2a8756ed MC |
144 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", |
145 | SIFIVE_U_CLOCK_FREQ); | |
ecdfe393 BM |
146 | /* cpu 0 is the management hart that does not have mmu */ |
147 | if (cpu != 0) { | |
148 | qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); | |
149 | isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]); | |
150 | } else { | |
151 | isa = riscv_isa_string(&s->soc.e_cpus.harts[0]); | |
152 | } | |
a7240d1e MC |
153 | qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); |
154 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); | |
155 | qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); | |
156 | qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); | |
157 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); | |
158 | qemu_fdt_add_subnode(fdt, intc); | |
382cb439 | 159 | qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); |
a7240d1e MC |
160 | qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); |
161 | qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); | |
162 | qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); | |
163 | g_free(isa); | |
164 | g_free(intc); | |
165 | g_free(nodename); | |
166 | } | |
167 | ||
ecdfe393 BM |
168 | cells = g_new0(uint32_t, ms->smp.cpus * 4); |
169 | for (cpu = 0; cpu < ms->smp.cpus; cpu++) { | |
a7240d1e MC |
170 | nodename = |
171 | g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
172 | uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); | |
173 | cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); | |
174 | cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); | |
175 | cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); | |
176 | cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); | |
177 | g_free(nodename); | |
178 | } | |
179 | nodename = g_strdup_printf("/soc/clint@%lx", | |
180 | (long)memmap[SIFIVE_U_CLINT].base); | |
181 | qemu_fdt_add_subnode(fdt, nodename); | |
182 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); | |
183 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
184 | 0x0, memmap[SIFIVE_U_CLINT].base, | |
185 | 0x0, memmap[SIFIVE_U_CLINT].size); | |
186 | qemu_fdt_setprop(fdt, nodename, "interrupts-extended", | |
ecdfe393 | 187 | cells, ms->smp.cpus * sizeof(uint32_t) * 4); |
a7240d1e MC |
188 | g_free(cells); |
189 | g_free(nodename); | |
190 | ||
382cb439 | 191 | plic_phandle = phandle++; |
ecdfe393 BM |
192 | cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2); |
193 | for (cpu = 0; cpu < ms->smp.cpus; cpu++) { | |
a7240d1e MC |
194 | nodename = |
195 | g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
196 | uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); | |
ecdfe393 BM |
197 | /* cpu 0 is the management hart that does not have S-mode */ |
198 | if (cpu == 0) { | |
199 | cells[0] = cpu_to_be32(intc_phandle); | |
200 | cells[1] = cpu_to_be32(IRQ_M_EXT); | |
201 | } else { | |
202 | cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle); | |
203 | cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT); | |
204 | cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); | |
205 | cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT); | |
206 | } | |
a7240d1e MC |
207 | g_free(nodename); |
208 | } | |
209 | nodename = g_strdup_printf("/soc/interrupt-controller@%lx", | |
210 | (long)memmap[SIFIVE_U_PLIC].base); | |
211 | qemu_fdt_add_subnode(fdt, nodename); | |
212 | qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); | |
213 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); | |
214 | qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); | |
215 | qemu_fdt_setprop(fdt, nodename, "interrupts-extended", | |
ecdfe393 | 216 | cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t)); |
a7240d1e MC |
217 | qemu_fdt_setprop_cells(fdt, nodename, "reg", |
218 | 0x0, memmap[SIFIVE_U_PLIC].base, | |
219 | 0x0, memmap[SIFIVE_U_PLIC].size); | |
98ceee7f | 220 | qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); |
04e7edd1 | 221 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle); |
a7240d1e MC |
222 | plic_phandle = qemu_fdt_get_phandle(fdt, nodename); |
223 | g_free(cells); | |
224 | g_free(nodename); | |
225 | ||
382cb439 | 226 | ethclk_phandle = phandle++; |
fe93582c AP |
227 | nodename = g_strdup_printf("/soc/ethclk"); |
228 | qemu_fdt_add_subnode(fdt, nodename); | |
229 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
230 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
231 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
232 | SIFIVE_U_GEM_CLOCK_FREQ); | |
382cb439 | 233 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", ethclk_phandle); |
fe93582c AP |
234 | ethclk_phandle = qemu_fdt_get_phandle(fdt, nodename); |
235 | g_free(nodename); | |
236 | ||
5a7f76a3 AF |
237 | nodename = g_strdup_printf("/soc/ethernet@%lx", |
238 | (long)memmap[SIFIVE_U_GEM].base); | |
239 | qemu_fdt_add_subnode(fdt, nodename); | |
240 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "cdns,macb"); | |
241 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
242 | 0x0, memmap[SIFIVE_U_GEM].base, | |
243 | 0x0, memmap[SIFIVE_U_GEM].size); | |
244 | qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); | |
245 | qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); | |
04e7edd1 BM |
246 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); |
247 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); | |
fe93582c AP |
248 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", |
249 | ethclk_phandle, ethclk_phandle, ethclk_phandle); | |
04ece4f8 | 250 | qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names, |
fe93582c | 251 | sizeof(ethclk_names)); |
04e7edd1 BM |
252 | qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); |
253 | qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); | |
5a7f76a3 AF |
254 | g_free(nodename); |
255 | ||
256 | nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", | |
257 | (long)memmap[SIFIVE_U_GEM].base); | |
258 | qemu_fdt_add_subnode(fdt, nodename); | |
04e7edd1 | 259 | qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0); |
5a7f76a3 AF |
260 | g_free(nodename); |
261 | ||
44e6dcd3 GR |
262 | uartclk_phandle = phandle++; |
263 | nodename = g_strdup_printf("/soc/uartclk"); | |
264 | qemu_fdt_add_subnode(fdt, nodename); | |
265 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
266 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
267 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 3686400); | |
268 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", uartclk_phandle); | |
44e6dcd3 GR |
269 | uartclk_phandle = qemu_fdt_get_phandle(fdt, nodename); |
270 | g_free(nodename); | |
271 | ||
bde3ab9a | 272 | nodename = g_strdup_printf("/soc/uart@%lx", |
a7240d1e MC |
273 | (long)memmap[SIFIVE_U_UART0].base); |
274 | qemu_fdt_add_subnode(fdt, nodename); | |
275 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); | |
276 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
277 | 0x0, memmap[SIFIVE_U_UART0].base, | |
278 | 0x0, memmap[SIFIVE_U_UART0].size); | |
04e7edd1 BM |
279 | qemu_fdt_setprop_cell(fdt, nodename, "clocks", uartclk_phandle); |
280 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
281 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); | |
a7240d1e MC |
282 | |
283 | qemu_fdt_add_subnode(fdt, "/chosen"); | |
284 | qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); | |
7c28f4da MC |
285 | if (cmdline) { |
286 | qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); | |
287 | } | |
44e6dcd3 GR |
288 | |
289 | qemu_fdt_add_subnode(fdt, "/aliases"); | |
290 | qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename); | |
291 | ||
a7240d1e MC |
292 | g_free(nodename); |
293 | } | |
294 | ||
295 | static void riscv_sifive_u_init(MachineState *machine) | |
296 | { | |
297 | const struct MemmapEntry *memmap = sifive_u_memmap; | |
298 | ||
299 | SiFiveUState *s = g_new0(SiFiveUState, 1); | |
5aec3247 | 300 | MemoryRegion *system_memory = get_system_memory(); |
a7240d1e | 301 | MemoryRegion *main_mem = g_new(MemoryRegion, 1); |
5aec3247 | 302 | int i; |
a7240d1e | 303 | |
2308092b | 304 | /* Initialize SoC */ |
4eea9d7d AF |
305 | object_initialize_child(OBJECT(machine), "soc", &s->soc, |
306 | sizeof(s->soc), TYPE_RISCV_U_SOC, | |
307 | &error_abort, NULL); | |
a7240d1e MC |
308 | object_property_set_bool(OBJECT(&s->soc), true, "realized", |
309 | &error_abort); | |
310 | ||
311 | /* register RAM */ | |
312 | memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", | |
313 | machine->ram_size, &error_fatal); | |
5aec3247 | 314 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DRAM].base, |
2308092b | 315 | main_mem); |
a7240d1e MC |
316 | |
317 | /* create device tree */ | |
9f79638e | 318 | create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); |
a7240d1e | 319 | |
fdd1bda4 AF |
320 | riscv_find_and_load_firmware(machine, BIOS_FILENAME, |
321 | memmap[SIFIVE_U_DRAM].base); | |
b3042223 | 322 | |
a7240d1e | 323 | if (machine->kernel_filename) { |
0f8d4462 GR |
324 | uint64_t kernel_entry = riscv_load_kernel(machine->kernel_filename); |
325 | ||
326 | if (machine->initrd_filename) { | |
327 | hwaddr start; | |
328 | hwaddr end = riscv_load_initrd(machine->initrd_filename, | |
329 | machine->ram_size, kernel_entry, | |
330 | &start); | |
9f79638e | 331 | qemu_fdt_setprop_cell(s->fdt, "/chosen", |
0f8d4462 | 332 | "linux,initrd-start", start); |
9f79638e | 333 | qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", |
0f8d4462 GR |
334 | end); |
335 | } | |
a7240d1e MC |
336 | } |
337 | ||
338 | /* reset vector */ | |
339 | uint32_t reset_vec[8] = { | |
340 | 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ | |
341 | 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ | |
342 | 0xf1402573, /* csrr a0, mhartid */ | |
343 | #if defined(TARGET_RISCV32) | |
344 | 0x0182a283, /* lw t0, 24(t0) */ | |
345 | #elif defined(TARGET_RISCV64) | |
346 | 0x0182b283, /* ld t0, 24(t0) */ | |
347 | #endif | |
348 | 0x00028067, /* jr t0 */ | |
349 | 0x00000000, | |
350 | memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */ | |
351 | 0x00000000, | |
352 | /* dtb: */ | |
353 | }; | |
354 | ||
5aec3247 MC |
355 | /* copy in the reset vector in little_endian byte order */ |
356 | for (i = 0; i < sizeof(reset_vec) >> 2; i++) { | |
357 | reset_vec[i] = cpu_to_le32(reset_vec[i]); | |
358 | } | |
359 | rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), | |
360 | memmap[SIFIVE_U_MROM].base, &address_space_memory); | |
a7240d1e MC |
361 | |
362 | /* copy in the device tree */ | |
5aec3247 MC |
363 | if (fdt_pack(s->fdt) || fdt_totalsize(s->fdt) > |
364 | memmap[SIFIVE_U_MROM].size - sizeof(reset_vec)) { | |
365 | error_report("not enough space to store device-tree"); | |
366 | exit(1); | |
367 | } | |
368 | qemu_fdt_dumpdtb(s->fdt, fdt_totalsize(s->fdt)); | |
369 | rom_add_blob_fixed_as("mrom.fdt", s->fdt, fdt_totalsize(s->fdt), | |
370 | memmap[SIFIVE_U_MROM].base + sizeof(reset_vec), | |
371 | &address_space_memory); | |
2308092b AF |
372 | } |
373 | ||
374 | static void riscv_sifive_u_soc_init(Object *obj) | |
375 | { | |
c4473127 | 376 | MachineState *ms = MACHINE(qdev_get_machine()); |
2308092b AF |
377 | SiFiveUSoCState *s = RISCV_U_SOC(obj); |
378 | ||
ecdfe393 BM |
379 | object_initialize_child(obj, "e-cluster", &s->e_cluster, |
380 | sizeof(s->e_cluster), TYPE_CPU_CLUSTER, | |
381 | &error_abort, NULL); | |
382 | qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); | |
383 | ||
384 | object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", | |
385 | &s->e_cpus, sizeof(s->e_cpus), | |
386 | TYPE_RISCV_HART_ARRAY, &error_abort, | |
387 | NULL); | |
388 | qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); | |
389 | qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); | |
390 | qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); | |
391 | ||
392 | object_initialize_child(obj, "u-cluster", &s->u_cluster, | |
393 | sizeof(s->u_cluster), TYPE_CPU_CLUSTER, | |
394 | &error_abort, NULL); | |
395 | qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); | |
396 | ||
397 | object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", | |
398 | &s->u_cpus, sizeof(s->u_cpus), | |
399 | TYPE_RISCV_HART_ARRAY, &error_abort, | |
400 | NULL); | |
401 | qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); | |
402 | qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); | |
403 | qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", SIFIVE_U_CPU); | |
5a7f76a3 | 404 | |
4eea9d7d AF |
405 | sysbus_init_child_obj(obj, "gem", &s->gem, sizeof(s->gem), |
406 | TYPE_CADENCE_GEM); | |
2308092b AF |
407 | } |
408 | ||
409 | static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp) | |
410 | { | |
c4473127 | 411 | MachineState *ms = MACHINE(qdev_get_machine()); |
2308092b AF |
412 | SiFiveUSoCState *s = RISCV_U_SOC(dev); |
413 | const struct MemmapEntry *memmap = sifive_u_memmap; | |
414 | MemoryRegion *system_memory = get_system_memory(); | |
415 | MemoryRegion *mask_rom = g_new(MemoryRegion, 1); | |
5a7f76a3 | 416 | qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES]; |
05446f41 BM |
417 | char *plic_hart_config; |
418 | size_t plic_hart_config_len; | |
5a7f76a3 AF |
419 | int i; |
420 | Error *err = NULL; | |
421 | NICInfo *nd = &nd_table[0]; | |
2308092b | 422 | |
ecdfe393 BM |
423 | object_property_set_bool(OBJECT(&s->e_cpus), true, "realized", |
424 | &error_abort); | |
425 | object_property_set_bool(OBJECT(&s->u_cpus), true, "realized", | |
426 | &error_abort); | |
427 | /* | |
428 | * The cluster must be realized after the RISC-V hart array container, | |
429 | * as the container's CPU object is only created on realize, and the | |
430 | * CPU must exist and have been parented into the cluster before the | |
431 | * cluster is realized. | |
432 | */ | |
433 | object_property_set_bool(OBJECT(&s->e_cluster), true, "realized", | |
434 | &error_abort); | |
435 | object_property_set_bool(OBJECT(&s->u_cluster), true, "realized", | |
2308092b AF |
436 | &error_abort); |
437 | ||
438 | /* boot rom */ | |
439 | memory_region_init_rom(mask_rom, NULL, "riscv.sifive.u.mrom", | |
440 | memmap[SIFIVE_U_MROM].size, &error_fatal); | |
441 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_MROM].base, | |
442 | mask_rom); | |
a7240d1e | 443 | |
05446f41 | 444 | /* create PLIC hart topology configuration string */ |
c4473127 LX |
445 | plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * |
446 | ms->smp.cpus; | |
05446f41 | 447 | plic_hart_config = g_malloc0(plic_hart_config_len); |
c4473127 | 448 | for (i = 0; i < ms->smp.cpus; i++) { |
05446f41 | 449 | if (i != 0) { |
ef965ce2 BM |
450 | strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG, |
451 | plic_hart_config_len); | |
452 | } else { | |
453 | strncat(plic_hart_config, "M", plic_hart_config_len); | |
05446f41 | 454 | } |
05446f41 BM |
455 | plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); |
456 | } | |
457 | ||
a7240d1e MC |
458 | /* MMIO */ |
459 | s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, | |
05446f41 | 460 | plic_hart_config, |
a7240d1e MC |
461 | SIFIVE_U_PLIC_NUM_SOURCES, |
462 | SIFIVE_U_PLIC_NUM_PRIORITIES, | |
463 | SIFIVE_U_PLIC_PRIORITY_BASE, | |
464 | SIFIVE_U_PLIC_PENDING_BASE, | |
465 | SIFIVE_U_PLIC_ENABLE_BASE, | |
466 | SIFIVE_U_PLIC_ENABLE_STRIDE, | |
467 | SIFIVE_U_PLIC_CONTEXT_BASE, | |
468 | SIFIVE_U_PLIC_CONTEXT_STRIDE, | |
469 | memmap[SIFIVE_U_PLIC].size); | |
5aec3247 | 470 | sifive_uart_create(system_memory, memmap[SIFIVE_U_UART0].base, |
647a70a1 | 471 | serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); |
194eef09 MC |
472 | sifive_uart_create(system_memory, memmap[SIFIVE_U_UART1].base, |
473 | serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); | |
a7240d1e | 474 | sifive_clint_create(memmap[SIFIVE_U_CLINT].base, |
c4473127 | 475 | memmap[SIFIVE_U_CLINT].size, ms->smp.cpus, |
a7240d1e | 476 | SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); |
5a7f76a3 AF |
477 | |
478 | for (i = 0; i < SIFIVE_U_PLIC_NUM_SOURCES; i++) { | |
479 | plic_gpios[i] = qdev_get_gpio_in(DEVICE(s->plic), i); | |
480 | } | |
481 | ||
482 | if (nd->used) { | |
483 | qemu_check_nic_model(nd, TYPE_CADENCE_GEM); | |
484 | qdev_set_nic_properties(DEVICE(&s->gem), nd); | |
485 | } | |
486 | object_property_set_int(OBJECT(&s->gem), GEM_REVISION, "revision", | |
487 | &error_abort); | |
488 | object_property_set_bool(OBJECT(&s->gem), true, "realized", &err); | |
489 | if (err) { | |
490 | error_propagate(errp, err); | |
491 | return; | |
492 | } | |
493 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_GEM].base); | |
494 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, | |
495 | plic_gpios[SIFIVE_U_GEM_IRQ]); | |
a7240d1e MC |
496 | } |
497 | ||
a7240d1e MC |
498 | static void riscv_sifive_u_machine_init(MachineClass *mc) |
499 | { | |
500 | mc->desc = "RISC-V Board compatible with SiFive U SDK"; | |
501 | mc->init = riscv_sifive_u_init; | |
ecdfe393 | 502 | mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; |
f3d47d58 BM |
503 | mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; |
504 | mc->default_cpus = mc->min_cpus; | |
a7240d1e MC |
505 | } |
506 | ||
507 | DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init) | |
2308092b AF |
508 | |
509 | static void riscv_sifive_u_soc_class_init(ObjectClass *oc, void *data) | |
510 | { | |
511 | DeviceClass *dc = DEVICE_CLASS(oc); | |
512 | ||
513 | dc->realize = riscv_sifive_u_soc_realize; | |
514 | /* Reason: Uses serial_hds in realize function, thus can't be used twice */ | |
515 | dc->user_creatable = false; | |
516 | } | |
517 | ||
518 | static const TypeInfo riscv_sifive_u_soc_type_info = { | |
519 | .name = TYPE_RISCV_U_SOC, | |
520 | .parent = TYPE_DEVICE, | |
521 | .instance_size = sizeof(SiFiveUSoCState), | |
522 | .instance_init = riscv_sifive_u_soc_init, | |
523 | .class_init = riscv_sifive_u_soc_class_init, | |
524 | }; | |
525 | ||
526 | static void riscv_sifive_u_soc_register_types(void) | |
527 | { | |
528 | type_register_static(&riscv_sifive_u_soc_type_info); | |
529 | } | |
530 | ||
531 | type_init(riscv_sifive_u_soc_register_types) |