2
0

sifive_u.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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) GPIO (General Purpose Input/Output Controller)
  15. * 5) OTP (One-Time Programmable) memory with stored serial number
  16. * 6) GEM (Gigabit Ethernet Controller) and management block
  17. * 7) DMA (Direct Memory Access Controller)
  18. *
  19. * This board currently generates devicetree dynamically that indicates at least
  20. * two harts and up to five harts.
  21. *
  22. * This program is free software; you can redistribute it and/or modify it
  23. * under the terms and conditions of the GNU General Public License,
  24. * version 2 or later, as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope it will be useful, but WITHOUT
  27. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  28. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  29. * more details.
  30. *
  31. * You should have received a copy of the GNU General Public License along with
  32. * this program. If not, see <http://www.gnu.org/licenses/>.
  33. */
  34. #include "qemu/osdep.h"
  35. #include "qemu/log.h"
  36. #include "qemu/error-report.h"
  37. #include "qapi/error.h"
  38. #include "qapi/visitor.h"
  39. #include "hw/boards.h"
  40. #include "hw/irq.h"
  41. #include "hw/loader.h"
  42. #include "hw/sysbus.h"
  43. #include "hw/char/serial.h"
  44. #include "hw/cpu/cluster.h"
  45. #include "hw/misc/unimp.h"
  46. #include "target/riscv/cpu.h"
  47. #include "hw/riscv/riscv_hart.h"
  48. #include "hw/riscv/sifive_u.h"
  49. #include "hw/riscv/boot.h"
  50. #include "hw/char/sifive_uart.h"
  51. #include "hw/intc/sifive_clint.h"
  52. #include "hw/intc/sifive_plic.h"
  53. #include "chardev/char.h"
  54. #include "net/eth.h"
  55. #include "sysemu/arch_init.h"
  56. #include "sysemu/device_tree.h"
  57. #include "sysemu/runstate.h"
  58. #include "sysemu/sysemu.h"
  59. #include <libfdt.h>
  60. #if defined(TARGET_RISCV32)
  61. # define BIOS_FILENAME "opensbi-riscv32-generic-fw_dynamic.bin"
  62. #else
  63. # define BIOS_FILENAME "opensbi-riscv64-generic-fw_dynamic.bin"
  64. #endif
  65. static const struct MemmapEntry {
  66. hwaddr base;
  67. hwaddr size;
  68. } sifive_u_memmap[] = {
  69. [SIFIVE_U_DEV_DEBUG] = { 0x0, 0x100 },
  70. [SIFIVE_U_DEV_MROM] = { 0x1000, 0xf000 },
  71. [SIFIVE_U_DEV_CLINT] = { 0x2000000, 0x10000 },
  72. [SIFIVE_U_DEV_L2CC] = { 0x2010000, 0x1000 },
  73. [SIFIVE_U_DEV_PDMA] = { 0x3000000, 0x100000 },
  74. [SIFIVE_U_DEV_L2LIM] = { 0x8000000, 0x2000000 },
  75. [SIFIVE_U_DEV_PLIC] = { 0xc000000, 0x4000000 },
  76. [SIFIVE_U_DEV_PRCI] = { 0x10000000, 0x1000 },
  77. [SIFIVE_U_DEV_UART0] = { 0x10010000, 0x1000 },
  78. [SIFIVE_U_DEV_UART1] = { 0x10011000, 0x1000 },
  79. [SIFIVE_U_DEV_GPIO] = { 0x10060000, 0x1000 },
  80. [SIFIVE_U_DEV_OTP] = { 0x10070000, 0x1000 },
  81. [SIFIVE_U_DEV_GEM] = { 0x10090000, 0x2000 },
  82. [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000, 0x1000 },
  83. [SIFIVE_U_DEV_DMC] = { 0x100b0000, 0x10000 },
  84. [SIFIVE_U_DEV_FLASH0] = { 0x20000000, 0x10000000 },
  85. [SIFIVE_U_DEV_DRAM] = { 0x80000000, 0x0 },
  86. };
  87. #define OTP_SERIAL 1
  88. #define GEM_REVISION 0x10070109
  89. static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,
  90. uint64_t mem_size, const char *cmdline)
  91. {
  92. MachineState *ms = MACHINE(qdev_get_machine());
  93. void *fdt;
  94. int cpu;
  95. uint32_t *cells;
  96. char *nodename;
  97. char ethclk_names[] = "pclk\0hclk";
  98. uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
  99. uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
  100. fdt = s->fdt = create_device_tree(&s->fdt_size);
  101. if (!fdt) {
  102. error_report("create_device_tree() failed");
  103. exit(1);
  104. }
  105. qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
  106. qemu_fdt_setprop_string(fdt, "/", "compatible",
  107. "sifive,hifive-unleashed-a00");
  108. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  109. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  110. qemu_fdt_add_subnode(fdt, "/soc");
  111. qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
  112. qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
  113. qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
  114. qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
  115. hfclk_phandle = phandle++;
  116. nodename = g_strdup_printf("/hfclk");
  117. qemu_fdt_add_subnode(fdt, nodename);
  118. qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
  119. qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
  120. qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
  121. SIFIVE_U_HFCLK_FREQ);
  122. qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
  123. qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
  124. g_free(nodename);
  125. rtcclk_phandle = phandle++;
  126. nodename = g_strdup_printf("/rtcclk");
  127. qemu_fdt_add_subnode(fdt, nodename);
  128. qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
  129. qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
  130. qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
  131. SIFIVE_U_RTCCLK_FREQ);
  132. qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
  133. qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
  134. g_free(nodename);
  135. nodename = g_strdup_printf("/memory@%lx",
  136. (long)memmap[SIFIVE_U_DEV_DRAM].base);
  137. qemu_fdt_add_subnode(fdt, nodename);
  138. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  139. memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
  140. mem_size >> 32, mem_size);
  141. qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
  142. g_free(nodename);
  143. qemu_fdt_add_subnode(fdt, "/cpus");
  144. qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
  145. SIFIVE_CLINT_TIMEBASE_FREQ);
  146. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
  147. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
  148. for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
  149. int cpu_phandle = phandle++;
  150. nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
  151. char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
  152. char *isa;
  153. qemu_fdt_add_subnode(fdt, nodename);
  154. /* cpu 0 is the management hart that does not have mmu */
  155. if (cpu != 0) {
  156. #if defined(TARGET_RISCV32)
  157. qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
  158. #else
  159. qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
  160. #endif
  161. isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]);
  162. } else {
  163. isa = riscv_isa_string(&s->soc.e_cpus.harts[0]);
  164. }
  165. qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
  166. qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
  167. qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
  168. qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
  169. qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
  170. qemu_fdt_add_subnode(fdt, intc);
  171. qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
  172. qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
  173. qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
  174. qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
  175. g_free(isa);
  176. g_free(intc);
  177. g_free(nodename);
  178. }
  179. cells = g_new0(uint32_t, ms->smp.cpus * 4);
  180. for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
  181. nodename =
  182. g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
  183. uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
  184. cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  185. cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
  186. cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
  187. cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
  188. g_free(nodename);
  189. }
  190. nodename = g_strdup_printf("/soc/clint@%lx",
  191. (long)memmap[SIFIVE_U_DEV_CLINT].base);
  192. qemu_fdt_add_subnode(fdt, nodename);
  193. qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
  194. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  195. 0x0, memmap[SIFIVE_U_DEV_CLINT].base,
  196. 0x0, memmap[SIFIVE_U_DEV_CLINT].size);
  197. qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
  198. cells, ms->smp.cpus * sizeof(uint32_t) * 4);
  199. g_free(cells);
  200. g_free(nodename);
  201. nodename = g_strdup_printf("/soc/otp@%lx",
  202. (long)memmap[SIFIVE_U_DEV_OTP].base);
  203. qemu_fdt_add_subnode(fdt, nodename);
  204. qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
  205. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  206. 0x0, memmap[SIFIVE_U_DEV_OTP].base,
  207. 0x0, memmap[SIFIVE_U_DEV_OTP].size);
  208. qemu_fdt_setprop_string(fdt, nodename, "compatible",
  209. "sifive,fu540-c000-otp");
  210. g_free(nodename);
  211. prci_phandle = phandle++;
  212. nodename = g_strdup_printf("/soc/clock-controller@%lx",
  213. (long)memmap[SIFIVE_U_DEV_PRCI].base);
  214. qemu_fdt_add_subnode(fdt, nodename);
  215. qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
  216. qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
  217. qemu_fdt_setprop_cells(fdt, nodename, "clocks",
  218. hfclk_phandle, rtcclk_phandle);
  219. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  220. 0x0, memmap[SIFIVE_U_DEV_PRCI].base,
  221. 0x0, memmap[SIFIVE_U_DEV_PRCI].size);
  222. qemu_fdt_setprop_string(fdt, nodename, "compatible",
  223. "sifive,fu540-c000-prci");
  224. g_free(nodename);
  225. plic_phandle = phandle++;
  226. cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2);
  227. for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
  228. nodename =
  229. g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
  230. uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
  231. /* cpu 0 is the management hart that does not have S-mode */
  232. if (cpu == 0) {
  233. cells[0] = cpu_to_be32(intc_phandle);
  234. cells[1] = cpu_to_be32(IRQ_M_EXT);
  235. } else {
  236. cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
  237. cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
  238. cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  239. cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
  240. }
  241. g_free(nodename);
  242. }
  243. nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
  244. (long)memmap[SIFIVE_U_DEV_PLIC].base);
  245. qemu_fdt_add_subnode(fdt, nodename);
  246. qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
  247. qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0");
  248. qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
  249. qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
  250. cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t));
  251. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  252. 0x0, memmap[SIFIVE_U_DEV_PLIC].base,
  253. 0x0, memmap[SIFIVE_U_DEV_PLIC].size);
  254. qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
  255. qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
  256. plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
  257. g_free(cells);
  258. g_free(nodename);
  259. gpio_phandle = phandle++;
  260. nodename = g_strdup_printf("/soc/gpio@%lx",
  261. (long)memmap[SIFIVE_U_DEV_GPIO].base);
  262. qemu_fdt_add_subnode(fdt, nodename);
  263. qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
  264. qemu_fdt_setprop_cells(fdt, nodename, "clocks",
  265. prci_phandle, PRCI_CLK_TLCLK);
  266. qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
  267. qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
  268. qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
  269. qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
  270. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  271. 0x0, memmap[SIFIVE_U_DEV_GPIO].base,
  272. 0x0, memmap[SIFIVE_U_DEV_GPIO].size);
  273. qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
  274. SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
  275. SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
  276. SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
  277. SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
  278. SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
  279. qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
  280. qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
  281. g_free(nodename);
  282. nodename = g_strdup_printf("/gpio-restart");
  283. qemu_fdt_add_subnode(fdt, nodename);
  284. qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
  285. qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
  286. g_free(nodename);
  287. nodename = g_strdup_printf("/soc/dma@%lx",
  288. (long)memmap[SIFIVE_U_DEV_PDMA].base);
  289. qemu_fdt_add_subnode(fdt, nodename);
  290. qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
  291. qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
  292. SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
  293. SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
  294. SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
  295. qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
  296. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  297. 0x0, memmap[SIFIVE_U_DEV_PDMA].base,
  298. 0x0, memmap[SIFIVE_U_DEV_PDMA].size);
  299. qemu_fdt_setprop_string(fdt, nodename, "compatible",
  300. "sifive,fu540-c000-pdma");
  301. g_free(nodename);
  302. nodename = g_strdup_printf("/soc/cache-controller@%lx",
  303. (long)memmap[SIFIVE_U_DEV_L2CC].base);
  304. qemu_fdt_add_subnode(fdt, nodename);
  305. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  306. 0x0, memmap[SIFIVE_U_DEV_L2CC].base,
  307. 0x0, memmap[SIFIVE_U_DEV_L2CC].size);
  308. qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
  309. SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
  310. qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
  311. qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
  312. qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
  313. qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
  314. qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
  315. qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
  316. qemu_fdt_setprop_string(fdt, nodename, "compatible",
  317. "sifive,fu540-c000-ccache");
  318. g_free(nodename);
  319. phy_phandle = phandle++;
  320. nodename = g_strdup_printf("/soc/ethernet@%lx",
  321. (long)memmap[SIFIVE_U_DEV_GEM].base);
  322. qemu_fdt_add_subnode(fdt, nodename);
  323. qemu_fdt_setprop_string(fdt, nodename, "compatible",
  324. "sifive,fu540-c000-gem");
  325. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  326. 0x0, memmap[SIFIVE_U_DEV_GEM].base,
  327. 0x0, memmap[SIFIVE_U_DEV_GEM].size,
  328. 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
  329. 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
  330. qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
  331. qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
  332. qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
  333. qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
  334. qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
  335. qemu_fdt_setprop_cells(fdt, nodename, "clocks",
  336. prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
  337. qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names,
  338. sizeof(ethclk_names));
  339. qemu_fdt_setprop(fdt, nodename, "local-mac-address",
  340. s->soc.gem.conf.macaddr.a, ETH_ALEN);
  341. qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
  342. qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
  343. qemu_fdt_add_subnode(fdt, "/aliases");
  344. qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
  345. g_free(nodename);
  346. nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
  347. (long)memmap[SIFIVE_U_DEV_GEM].base);
  348. qemu_fdt_add_subnode(fdt, nodename);
  349. qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
  350. qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
  351. g_free(nodename);
  352. nodename = g_strdup_printf("/soc/serial@%lx",
  353. (long)memmap[SIFIVE_U_DEV_UART0].base);
  354. qemu_fdt_add_subnode(fdt, nodename);
  355. qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
  356. qemu_fdt_setprop_cells(fdt, nodename, "reg",
  357. 0x0, memmap[SIFIVE_U_DEV_UART0].base,
  358. 0x0, memmap[SIFIVE_U_DEV_UART0].size);
  359. qemu_fdt_setprop_cells(fdt, nodename, "clocks",
  360. prci_phandle, PRCI_CLK_TLCLK);
  361. qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
  362. qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
  363. qemu_fdt_add_subnode(fdt, "/chosen");
  364. qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
  365. if (cmdline) {
  366. qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
  367. }
  368. qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
  369. g_free(nodename);
  370. }
  371. static void sifive_u_machine_reset(void *opaque, int n, int level)
  372. {
  373. /* gpio pin active low triggers reset */
  374. if (!level) {
  375. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  376. }
  377. }
  378. static void sifive_u_machine_init(MachineState *machine)
  379. {
  380. const struct MemmapEntry *memmap = sifive_u_memmap;
  381. SiFiveUState *s = RISCV_U_MACHINE(machine);
  382. MemoryRegion *system_memory = get_system_memory();
  383. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  384. MemoryRegion *flash0 = g_new(MemoryRegion, 1);
  385. target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
  386. uint32_t start_addr_hi32 = 0x00000000;
  387. int i;
  388. uint32_t fdt_load_addr;
  389. uint64_t kernel_entry;
  390. /* Initialize SoC */
  391. object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
  392. object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
  393. &error_abort);
  394. qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
  395. /* register RAM */
  396. memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
  397. machine->ram_size, &error_fatal);
  398. memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
  399. main_mem);
  400. /* register QSPI0 Flash */
  401. memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
  402. memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
  403. memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
  404. flash0);
  405. /* register gpio-restart */
  406. qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
  407. qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
  408. /* create device tree */
  409. create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
  410. if (s->start_in_flash) {
  411. /*
  412. * If start_in_flash property is given, assign s->msel to a value
  413. * that representing booting from QSPI0 memory-mapped flash.
  414. *
  415. * This also means that when both start_in_flash and msel properties
  416. * are given, start_in_flash takes the precedence over msel.
  417. *
  418. * Note this is to keep backward compatibility not to break existing
  419. * users that use start_in_flash property.
  420. */
  421. s->msel = MSEL_MEMMAP_QSPI0_FLASH;
  422. }
  423. switch (s->msel) {
  424. case MSEL_MEMMAP_QSPI0_FLASH:
  425. start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
  426. break;
  427. case MSEL_L2LIM_QSPI0_FLASH:
  428. case MSEL_L2LIM_QSPI2_SD:
  429. start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
  430. break;
  431. default:
  432. start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
  433. break;
  434. }
  435. riscv_find_and_load_firmware(machine, BIOS_FILENAME, start_addr, NULL);
  436. if (machine->kernel_filename) {
  437. kernel_entry = riscv_load_kernel(machine->kernel_filename, NULL);
  438. if (machine->initrd_filename) {
  439. hwaddr start;
  440. hwaddr end = riscv_load_initrd(machine->initrd_filename,
  441. machine->ram_size, kernel_entry,
  442. &start);
  443. qemu_fdt_setprop_cell(s->fdt, "/chosen",
  444. "linux,initrd-start", start);
  445. qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
  446. end);
  447. }
  448. } else {
  449. /*
  450. * If dynamic firmware is used, it doesn't know where is the next mode
  451. * if kernel argument is not set.
  452. */
  453. kernel_entry = 0;
  454. }
  455. /* Compute the fdt load address in dram */
  456. fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base,
  457. machine->ram_size, s->fdt);
  458. #if defined(TARGET_RISCV64)
  459. start_addr_hi32 = start_addr >> 32;
  460. #endif
  461. /* reset vector */
  462. uint32_t reset_vec[11] = {
  463. s->msel, /* MSEL pin state */
  464. 0x00000297, /* 1: auipc t0, %pcrel_hi(fw_dyn) */
  465. 0x02828613, /* addi a2, t0, %pcrel_lo(1b) */
  466. 0xf1402573, /* csrr a0, mhartid */
  467. #if defined(TARGET_RISCV32)
  468. 0x0202a583, /* lw a1, 32(t0) */
  469. 0x0182a283, /* lw t0, 24(t0) */
  470. #elif defined(TARGET_RISCV64)
  471. 0x0202b583, /* ld a1, 32(t0) */
  472. 0x0182b283, /* ld t0, 24(t0) */
  473. #endif
  474. 0x00028067, /* jr t0 */
  475. start_addr, /* start: .dword */
  476. start_addr_hi32,
  477. fdt_load_addr, /* fdt_laddr: .dword */
  478. 0x00000000,
  479. /* fw_dyn: */
  480. };
  481. /* copy in the reset vector in little_endian byte order */
  482. for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
  483. reset_vec[i] = cpu_to_le32(reset_vec[i]);
  484. }
  485. rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
  486. memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
  487. riscv_rom_copy_firmware_info(memmap[SIFIVE_U_DEV_MROM].base,
  488. memmap[SIFIVE_U_DEV_MROM].size,
  489. sizeof(reset_vec), kernel_entry);
  490. }
  491. static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
  492. {
  493. SiFiveUState *s = RISCV_U_MACHINE(obj);
  494. return s->start_in_flash;
  495. }
  496. static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
  497. {
  498. SiFiveUState *s = RISCV_U_MACHINE(obj);
  499. s->start_in_flash = value;
  500. }
  501. static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v,
  502. const char *name, void *opaque,
  503. Error **errp)
  504. {
  505. visit_type_uint32(v, name, (uint32_t *)opaque, errp);
  506. }
  507. static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v,
  508. const char *name, void *opaque,
  509. Error **errp)
  510. {
  511. visit_type_uint32(v, name, (uint32_t *)opaque, errp);
  512. }
  513. static void sifive_u_machine_instance_init(Object *obj)
  514. {
  515. SiFiveUState *s = RISCV_U_MACHINE(obj);
  516. s->start_in_flash = false;
  517. s->msel = 0;
  518. object_property_add(obj, "msel", "uint32",
  519. sifive_u_machine_get_uint32_prop,
  520. sifive_u_machine_set_uint32_prop, NULL, &s->msel);
  521. object_property_set_description(obj, "msel",
  522. "Mode Select (MSEL[3:0]) pin state");
  523. s->serial = OTP_SERIAL;
  524. object_property_add(obj, "serial", "uint32",
  525. sifive_u_machine_get_uint32_prop,
  526. sifive_u_machine_set_uint32_prop, NULL, &s->serial);
  527. object_property_set_description(obj, "serial", "Board serial number");
  528. }
  529. static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
  530. {
  531. MachineClass *mc = MACHINE_CLASS(oc);
  532. mc->desc = "RISC-V Board compatible with SiFive U SDK";
  533. mc->init = sifive_u_machine_init;
  534. mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
  535. mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
  536. mc->default_cpus = mc->min_cpus;
  537. object_class_property_add_bool(oc, "start-in-flash",
  538. sifive_u_machine_get_start_in_flash,
  539. sifive_u_machine_set_start_in_flash);
  540. object_class_property_set_description(oc, "start-in-flash",
  541. "Set on to tell QEMU's ROM to jump to "
  542. "flash. Otherwise QEMU will jump to DRAM "
  543. "or L2LIM depending on the msel value");
  544. }
  545. static const TypeInfo sifive_u_machine_typeinfo = {
  546. .name = MACHINE_TYPE_NAME("sifive_u"),
  547. .parent = TYPE_MACHINE,
  548. .class_init = sifive_u_machine_class_init,
  549. .instance_init = sifive_u_machine_instance_init,
  550. .instance_size = sizeof(SiFiveUState),
  551. };
  552. static void sifive_u_machine_init_register_types(void)
  553. {
  554. type_register_static(&sifive_u_machine_typeinfo);
  555. }
  556. type_init(sifive_u_machine_init_register_types)
  557. static void sifive_u_soc_instance_init(Object *obj)
  558. {
  559. MachineState *ms = MACHINE(qdev_get_machine());
  560. SiFiveUSoCState *s = RISCV_U_SOC(obj);
  561. object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
  562. qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
  563. object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
  564. TYPE_RISCV_HART_ARRAY);
  565. qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
  566. qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
  567. qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
  568. qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
  569. object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
  570. qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
  571. object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
  572. TYPE_RISCV_HART_ARRAY);
  573. qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
  574. qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
  575. qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", SIFIVE_U_CPU);
  576. qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
  577. object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
  578. object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
  579. object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
  580. object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
  581. object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
  582. }
  583. static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
  584. {
  585. MachineState *ms = MACHINE(qdev_get_machine());
  586. SiFiveUSoCState *s = RISCV_U_SOC(dev);
  587. const struct MemmapEntry *memmap = sifive_u_memmap;
  588. MemoryRegion *system_memory = get_system_memory();
  589. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  590. MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
  591. char *plic_hart_config;
  592. size_t plic_hart_config_len;
  593. int i;
  594. NICInfo *nd = &nd_table[0];
  595. sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort);
  596. sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort);
  597. /*
  598. * The cluster must be realized after the RISC-V hart array container,
  599. * as the container's CPU object is only created on realize, and the
  600. * CPU must exist and have been parented into the cluster before the
  601. * cluster is realized.
  602. */
  603. qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
  604. qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
  605. /* boot rom */
  606. memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
  607. memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
  608. memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
  609. mask_rom);
  610. /*
  611. * Add L2-LIM at reset size.
  612. * This should be reduced in size as the L2 Cache Controller WayEnable
  613. * register is incremented. Unfortunately I don't see a nice (or any) way
  614. * to handle reducing or blocking out the L2 LIM while still allowing it
  615. * be re returned to all enabled after a reset. For the time being, just
  616. * leave it enabled all the time. This won't break anything, but will be
  617. * too generous to misbehaving guests.
  618. */
  619. memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
  620. memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
  621. memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
  622. l2lim_mem);
  623. /* create PLIC hart topology configuration string */
  624. plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) *
  625. ms->smp.cpus;
  626. plic_hart_config = g_malloc0(plic_hart_config_len);
  627. for (i = 0; i < ms->smp.cpus; i++) {
  628. if (i != 0) {
  629. strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG,
  630. plic_hart_config_len);
  631. } else {
  632. strncat(plic_hart_config, "M", plic_hart_config_len);
  633. }
  634. plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1);
  635. }
  636. /* MMIO */
  637. s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
  638. plic_hart_config, 0,
  639. SIFIVE_U_PLIC_NUM_SOURCES,
  640. SIFIVE_U_PLIC_NUM_PRIORITIES,
  641. SIFIVE_U_PLIC_PRIORITY_BASE,
  642. SIFIVE_U_PLIC_PENDING_BASE,
  643. SIFIVE_U_PLIC_ENABLE_BASE,
  644. SIFIVE_U_PLIC_ENABLE_STRIDE,
  645. SIFIVE_U_PLIC_CONTEXT_BASE,
  646. SIFIVE_U_PLIC_CONTEXT_STRIDE,
  647. memmap[SIFIVE_U_DEV_PLIC].size);
  648. g_free(plic_hart_config);
  649. sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
  650. serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
  651. sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
  652. serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
  653. sifive_clint_create(memmap[SIFIVE_U_DEV_CLINT].base,
  654. memmap[SIFIVE_U_DEV_CLINT].size, 0, ms->smp.cpus,
  655. SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
  656. SIFIVE_CLINT_TIMEBASE_FREQ, false);
  657. if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
  658. return;
  659. }
  660. sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
  661. qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
  662. if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
  663. return;
  664. }
  665. sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
  666. /* Pass all GPIOs to the SOC layer so they are available to the board */
  667. qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
  668. /* Connect GPIO interrupts to the PLIC */
  669. for (i = 0; i < 16; i++) {
  670. sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
  671. qdev_get_gpio_in(DEVICE(s->plic),
  672. SIFIVE_U_GPIO_IRQ0 + i));
  673. }
  674. /* PDMA */
  675. sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
  676. sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
  677. /* Connect PDMA interrupts to the PLIC */
  678. for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
  679. sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
  680. qdev_get_gpio_in(DEVICE(s->plic),
  681. SIFIVE_U_PDMA_IRQ0 + i));
  682. }
  683. qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
  684. if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
  685. return;
  686. }
  687. sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
  688. /* FIXME use qdev NIC properties instead of nd_table[] */
  689. if (nd->used) {
  690. qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
  691. qdev_set_nic_properties(DEVICE(&s->gem), nd);
  692. }
  693. object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
  694. &error_abort);
  695. if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
  696. return;
  697. }
  698. sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
  699. sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
  700. qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
  701. create_unimplemented_device("riscv.sifive.u.gem-mgmt",
  702. memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
  703. create_unimplemented_device("riscv.sifive.u.dmc",
  704. memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
  705. create_unimplemented_device("riscv.sifive.u.l2cc",
  706. memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
  707. }
  708. static Property sifive_u_soc_props[] = {
  709. DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
  710. DEFINE_PROP_END_OF_LIST()
  711. };
  712. static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
  713. {
  714. DeviceClass *dc = DEVICE_CLASS(oc);
  715. device_class_set_props(dc, sifive_u_soc_props);
  716. dc->realize = sifive_u_soc_realize;
  717. /* Reason: Uses serial_hds in realize function, thus can't be used twice */
  718. dc->user_creatable = false;
  719. }
  720. static const TypeInfo sifive_u_soc_type_info = {
  721. .name = TYPE_RISCV_U_SOC,
  722. .parent = TYPE_DEVICE,
  723. .instance_size = sizeof(SiFiveUSoCState),
  724. .instance_init = sifive_u_soc_instance_init,
  725. .class_init = sifive_u_soc_class_init,
  726. };
  727. static void sifive_u_soc_register_types(void)
  728. {
  729. type_register_static(&sifive_u_soc_type_info);
  730. }
  731. type_init(sifive_u_soc_register_types)