sifive_u.c 25 KB

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