virt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * QEMU RISC-V VirtIO Board
  3. *
  4. * Copyright (c) 2017 SiFive, Inc.
  5. *
  6. * RISC-V machine with 16550a UART and VirtIO MMIO
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2 or later, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/units.h"
  22. #include "qemu/log.h"
  23. #include "qemu/error-report.h"
  24. #include "qapi/error.h"
  25. #include "hw/boards.h"
  26. #include "hw/loader.h"
  27. #include "hw/sysbus.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/char/serial.h"
  30. #include "target/riscv/cpu.h"
  31. #include "hw/riscv/riscv_hart.h"
  32. #include "hw/riscv/virt.h"
  33. #include "hw/riscv/boot.h"
  34. #include "hw/riscv/numa.h"
  35. #include "hw/intc/sifive_clint.h"
  36. #include "hw/intc/sifive_plic.h"
  37. #include "hw/misc/sifive_test.h"
  38. #include "chardev/char.h"
  39. #include "sysemu/arch_init.h"
  40. #include "sysemu/device_tree.h"
  41. #include "sysemu/sysemu.h"
  42. #include "hw/pci/pci.h"
  43. #include "hw/pci-host/gpex.h"
  44. #if defined(TARGET_RISCV32)
  45. # define BIOS_FILENAME "opensbi-riscv32-generic-fw_dynamic.bin"
  46. #else
  47. # define BIOS_FILENAME "opensbi-riscv64-generic-fw_dynamic.bin"
  48. #endif
  49. static const struct MemmapEntry {
  50. hwaddr base;
  51. hwaddr size;
  52. } virt_memmap[] = {
  53. [VIRT_DEBUG] = { 0x0, 0x100 },
  54. [VIRT_MROM] = { 0x1000, 0xf000 },
  55. [VIRT_TEST] = { 0x100000, 0x1000 },
  56. [VIRT_RTC] = { 0x101000, 0x1000 },
  57. [VIRT_CLINT] = { 0x2000000, 0x10000 },
  58. [VIRT_PCIE_PIO] = { 0x3000000, 0x10000 },
  59. [VIRT_PLIC] = { 0xc000000, VIRT_PLIC_SIZE(VIRT_CPUS_MAX * 2) },
  60. [VIRT_UART0] = { 0x10000000, 0x100 },
  61. [VIRT_VIRTIO] = { 0x10001000, 0x1000 },
  62. [VIRT_FLASH] = { 0x20000000, 0x4000000 },
  63. [VIRT_PCIE_ECAM] = { 0x30000000, 0x10000000 },
  64. [VIRT_PCIE_MMIO] = { 0x40000000, 0x40000000 },
  65. [VIRT_DRAM] = { 0x80000000, 0x0 },
  66. };
  67. #define VIRT_FLASH_SECTOR_SIZE (256 * KiB)
  68. static PFlashCFI01 *virt_flash_create1(RISCVVirtState *s,
  69. const char *name,
  70. const char *alias_prop_name)
  71. {
  72. /*
  73. * Create a single flash device. We use the same parameters as
  74. * the flash devices on the ARM virt board.
  75. */
  76. DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
  77. qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE);
  78. qdev_prop_set_uint8(dev, "width", 4);
  79. qdev_prop_set_uint8(dev, "device-width", 2);
  80. qdev_prop_set_bit(dev, "big-endian", false);
  81. qdev_prop_set_uint16(dev, "id0", 0x89);
  82. qdev_prop_set_uint16(dev, "id1", 0x18);
  83. qdev_prop_set_uint16(dev, "id2", 0x00);
  84. qdev_prop_set_uint16(dev, "id3", 0x00);
  85. qdev_prop_set_string(dev, "name", name);
  86. object_property_add_child(OBJECT(s), name, OBJECT(dev));
  87. object_property_add_alias(OBJECT(s), alias_prop_name,
  88. OBJECT(dev), "drive");
  89. return PFLASH_CFI01(dev);
  90. }
  91. static void virt_flash_create(RISCVVirtState *s)
  92. {
  93. s->flash[0] = virt_flash_create1(s, "virt.flash0", "pflash0");
  94. s->flash[1] = virt_flash_create1(s, "virt.flash1", "pflash1");
  95. }
  96. static void virt_flash_map1(PFlashCFI01 *flash,
  97. hwaddr base, hwaddr size,
  98. MemoryRegion *sysmem)
  99. {
  100. DeviceState *dev = DEVICE(flash);
  101. assert(QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE));
  102. assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
  103. qdev_prop_set_uint32(dev, "num-blocks", size / VIRT_FLASH_SECTOR_SIZE);
  104. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  105. memory_region_add_subregion(sysmem, base,
  106. sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
  107. 0));
  108. }
  109. static void virt_flash_map(RISCVVirtState *s,
  110. MemoryRegion *sysmem)
  111. {
  112. hwaddr flashsize = virt_memmap[VIRT_FLASH].size / 2;
  113. hwaddr flashbase = virt_memmap[VIRT_FLASH].base;
  114. virt_flash_map1(s->flash[0], flashbase, flashsize,
  115. sysmem);
  116. virt_flash_map1(s->flash[1], flashbase + flashsize, flashsize,
  117. sysmem);
  118. }
  119. static void create_pcie_irq_map(void *fdt, char *nodename,
  120. uint32_t plic_phandle)
  121. {
  122. int pin, dev;
  123. uint32_t
  124. full_irq_map[GPEX_NUM_IRQS * GPEX_NUM_IRQS * FDT_INT_MAP_WIDTH] = {};
  125. uint32_t *irq_map = full_irq_map;
  126. /* This code creates a standard swizzle of interrupts such that
  127. * each device's first interrupt is based on it's PCI_SLOT number.
  128. * (See pci_swizzle_map_irq_fn())
  129. *
  130. * We only need one entry per interrupt in the table (not one per
  131. * possible slot) seeing the interrupt-map-mask will allow the table
  132. * to wrap to any number of devices.
  133. */
  134. for (dev = 0; dev < GPEX_NUM_IRQS; dev++) {
  135. int devfn = dev * 0x8;
  136. for (pin = 0; pin < GPEX_NUM_IRQS; pin++) {
  137. int irq_nr = PCIE_IRQ + ((pin + PCI_SLOT(devfn)) % GPEX_NUM_IRQS);
  138. int i = 0;
  139. irq_map[i] = cpu_to_be32(devfn << 8);
  140. i += FDT_PCI_ADDR_CELLS;
  141. irq_map[i] = cpu_to_be32(pin + 1);
  142. i += FDT_PCI_INT_CELLS;
  143. irq_map[i++] = cpu_to_be32(plic_phandle);
  144. i += FDT_PLIC_ADDR_CELLS;
  145. irq_map[i] = cpu_to_be32(irq_nr);
  146. irq_map += FDT_INT_MAP_WIDTH;
  147. }
  148. }
  149. qemu_fdt_setprop(fdt, nodename, "interrupt-map",
  150. full_irq_map, sizeof(full_irq_map));
  151. qemu_fdt_setprop_cells(fdt, nodename, "interrupt-map-mask",
  152. 0x1800, 0, 0, 0x7);
  153. }
  154. static void create_fdt(RISCVVirtState *s, const struct MemmapEntry *memmap,
  155. uint64_t mem_size, const char *cmdline)
  156. {
  157. void *fdt;
  158. int i, cpu, socket;
  159. MachineState *mc = MACHINE(s);
  160. uint64_t addr, size;
  161. uint32_t *clint_cells, *plic_cells;
  162. unsigned long clint_addr, plic_addr;
  163. uint32_t plic_phandle[MAX_NODES];
  164. uint32_t cpu_phandle, intc_phandle, test_phandle;
  165. uint32_t phandle = 1, plic_mmio_phandle = 1;
  166. uint32_t plic_pcie_phandle = 1, plic_virtio_phandle = 1;
  167. char *mem_name, *cpu_name, *core_name, *intc_name;
  168. char *name, *clint_name, *plic_name, *clust_name;
  169. hwaddr flashsize = virt_memmap[VIRT_FLASH].size / 2;
  170. hwaddr flashbase = virt_memmap[VIRT_FLASH].base;
  171. fdt = s->fdt = create_device_tree(&s->fdt_size);
  172. if (!fdt) {
  173. error_report("create_device_tree() failed");
  174. exit(1);
  175. }
  176. qemu_fdt_setprop_string(fdt, "/", "model", "riscv-virtio,qemu");
  177. qemu_fdt_setprop_string(fdt, "/", "compatible", "riscv-virtio");
  178. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  179. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  180. qemu_fdt_add_subnode(fdt, "/soc");
  181. qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
  182. qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
  183. qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
  184. qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
  185. qemu_fdt_add_subnode(fdt, "/cpus");
  186. qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
  187. SIFIVE_CLINT_TIMEBASE_FREQ);
  188. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
  189. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
  190. qemu_fdt_add_subnode(fdt, "/cpus/cpu-map");
  191. for (socket = (riscv_socket_count(mc) - 1); socket >= 0; socket--) {
  192. clust_name = g_strdup_printf("/cpus/cpu-map/cluster%d", socket);
  193. qemu_fdt_add_subnode(fdt, clust_name);
  194. plic_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
  195. clint_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
  196. for (cpu = s->soc[socket].num_harts - 1; cpu >= 0; cpu--) {
  197. cpu_phandle = phandle++;
  198. cpu_name = g_strdup_printf("/cpus/cpu@%d",
  199. s->soc[socket].hartid_base + cpu);
  200. qemu_fdt_add_subnode(fdt, cpu_name);
  201. #if defined(TARGET_RISCV32)
  202. qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv32");
  203. #else
  204. qemu_fdt_setprop_string(fdt, cpu_name, "mmu-type", "riscv,sv48");
  205. #endif
  206. name = riscv_isa_string(&s->soc[socket].harts[cpu]);
  207. qemu_fdt_setprop_string(fdt, cpu_name, "riscv,isa", name);
  208. g_free(name);
  209. qemu_fdt_setprop_string(fdt, cpu_name, "compatible", "riscv");
  210. qemu_fdt_setprop_string(fdt, cpu_name, "status", "okay");
  211. qemu_fdt_setprop_cell(fdt, cpu_name, "reg",
  212. s->soc[socket].hartid_base + cpu);
  213. qemu_fdt_setprop_string(fdt, cpu_name, "device_type", "cpu");
  214. riscv_socket_fdt_write_id(mc, fdt, cpu_name, socket);
  215. qemu_fdt_setprop_cell(fdt, cpu_name, "phandle", cpu_phandle);
  216. intc_name = g_strdup_printf("%s/interrupt-controller", cpu_name);
  217. qemu_fdt_add_subnode(fdt, intc_name);
  218. intc_phandle = phandle++;
  219. qemu_fdt_setprop_cell(fdt, intc_name, "phandle", intc_phandle);
  220. qemu_fdt_setprop_string(fdt, intc_name, "compatible",
  221. "riscv,cpu-intc");
  222. qemu_fdt_setprop(fdt, intc_name, "interrupt-controller", NULL, 0);
  223. qemu_fdt_setprop_cell(fdt, intc_name, "#interrupt-cells", 1);
  224. clint_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  225. clint_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
  226. clint_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
  227. clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
  228. plic_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
  229. plic_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT);
  230. plic_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
  231. plic_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT);
  232. core_name = g_strdup_printf("%s/core%d", clust_name, cpu);
  233. qemu_fdt_add_subnode(fdt, core_name);
  234. qemu_fdt_setprop_cell(fdt, core_name, "cpu", cpu_phandle);
  235. g_free(core_name);
  236. g_free(intc_name);
  237. g_free(cpu_name);
  238. }
  239. addr = memmap[VIRT_DRAM].base + riscv_socket_mem_offset(mc, socket);
  240. size = riscv_socket_mem_size(mc, socket);
  241. mem_name = g_strdup_printf("/memory@%lx", (long)addr);
  242. qemu_fdt_add_subnode(fdt, mem_name);
  243. qemu_fdt_setprop_cells(fdt, mem_name, "reg",
  244. addr >> 32, addr, size >> 32, size);
  245. qemu_fdt_setprop_string(fdt, mem_name, "device_type", "memory");
  246. riscv_socket_fdt_write_id(mc, fdt, mem_name, socket);
  247. g_free(mem_name);
  248. clint_addr = memmap[VIRT_CLINT].base +
  249. (memmap[VIRT_CLINT].size * socket);
  250. clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr);
  251. qemu_fdt_add_subnode(fdt, clint_name);
  252. qemu_fdt_setprop_string(fdt, clint_name, "compatible", "riscv,clint0");
  253. qemu_fdt_setprop_cells(fdt, clint_name, "reg",
  254. 0x0, clint_addr, 0x0, memmap[VIRT_CLINT].size);
  255. qemu_fdt_setprop(fdt, clint_name, "interrupts-extended",
  256. clint_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4);
  257. riscv_socket_fdt_write_id(mc, fdt, clint_name, socket);
  258. g_free(clint_name);
  259. plic_phandle[socket] = phandle++;
  260. plic_addr = memmap[VIRT_PLIC].base + (memmap[VIRT_PLIC].size * socket);
  261. plic_name = g_strdup_printf("/soc/plic@%lx", plic_addr);
  262. qemu_fdt_add_subnode(fdt, plic_name);
  263. qemu_fdt_setprop_cell(fdt, plic_name,
  264. "#address-cells", FDT_PLIC_ADDR_CELLS);
  265. qemu_fdt_setprop_cell(fdt, plic_name,
  266. "#interrupt-cells", FDT_PLIC_INT_CELLS);
  267. qemu_fdt_setprop_string(fdt, plic_name, "compatible", "riscv,plic0");
  268. qemu_fdt_setprop(fdt, plic_name, "interrupt-controller", NULL, 0);
  269. qemu_fdt_setprop(fdt, plic_name, "interrupts-extended",
  270. plic_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4);
  271. qemu_fdt_setprop_cells(fdt, plic_name, "reg",
  272. 0x0, plic_addr, 0x0, memmap[VIRT_PLIC].size);
  273. qemu_fdt_setprop_cell(fdt, plic_name, "riscv,ndev", VIRTIO_NDEV);
  274. riscv_socket_fdt_write_id(mc, fdt, plic_name, socket);
  275. qemu_fdt_setprop_cell(fdt, plic_name, "phandle", plic_phandle[socket]);
  276. g_free(plic_name);
  277. g_free(clint_cells);
  278. g_free(plic_cells);
  279. g_free(clust_name);
  280. }
  281. for (socket = 0; socket < riscv_socket_count(mc); socket++) {
  282. if (socket == 0) {
  283. plic_mmio_phandle = plic_phandle[socket];
  284. plic_virtio_phandle = plic_phandle[socket];
  285. plic_pcie_phandle = plic_phandle[socket];
  286. }
  287. if (socket == 1) {
  288. plic_virtio_phandle = plic_phandle[socket];
  289. plic_pcie_phandle = plic_phandle[socket];
  290. }
  291. if (socket == 2) {
  292. plic_pcie_phandle = plic_phandle[socket];
  293. }
  294. }
  295. riscv_socket_fdt_write_distance_matrix(mc, fdt);
  296. for (i = 0; i < VIRTIO_COUNT; i++) {
  297. name = g_strdup_printf("/soc/virtio_mmio@%lx",
  298. (long)(memmap[VIRT_VIRTIO].base + i * memmap[VIRT_VIRTIO].size));
  299. qemu_fdt_add_subnode(fdt, name);
  300. qemu_fdt_setprop_string(fdt, name, "compatible", "virtio,mmio");
  301. qemu_fdt_setprop_cells(fdt, name, "reg",
  302. 0x0, memmap[VIRT_VIRTIO].base + i * memmap[VIRT_VIRTIO].size,
  303. 0x0, memmap[VIRT_VIRTIO].size);
  304. qemu_fdt_setprop_cell(fdt, name, "interrupt-parent",
  305. plic_virtio_phandle);
  306. qemu_fdt_setprop_cell(fdt, name, "interrupts", VIRTIO_IRQ + i);
  307. g_free(name);
  308. }
  309. name = g_strdup_printf("/soc/pci@%lx",
  310. (long) memmap[VIRT_PCIE_ECAM].base);
  311. qemu_fdt_add_subnode(fdt, name);
  312. qemu_fdt_setprop_cell(fdt, name, "#address-cells", FDT_PCI_ADDR_CELLS);
  313. qemu_fdt_setprop_cell(fdt, name, "#interrupt-cells", FDT_PCI_INT_CELLS);
  314. qemu_fdt_setprop_cell(fdt, name, "#size-cells", 0x2);
  315. qemu_fdt_setprop_string(fdt, name, "compatible", "pci-host-ecam-generic");
  316. qemu_fdt_setprop_string(fdt, name, "device_type", "pci");
  317. qemu_fdt_setprop_cell(fdt, name, "linux,pci-domain", 0);
  318. qemu_fdt_setprop_cells(fdt, name, "bus-range", 0,
  319. memmap[VIRT_PCIE_ECAM].size / PCIE_MMCFG_SIZE_MIN - 1);
  320. qemu_fdt_setprop(fdt, name, "dma-coherent", NULL, 0);
  321. qemu_fdt_setprop_cells(fdt, name, "reg", 0,
  322. memmap[VIRT_PCIE_ECAM].base, 0, memmap[VIRT_PCIE_ECAM].size);
  323. qemu_fdt_setprop_sized_cells(fdt, name, "ranges",
  324. 1, FDT_PCI_RANGE_IOPORT, 2, 0,
  325. 2, memmap[VIRT_PCIE_PIO].base, 2, memmap[VIRT_PCIE_PIO].size,
  326. 1, FDT_PCI_RANGE_MMIO,
  327. 2, memmap[VIRT_PCIE_MMIO].base,
  328. 2, memmap[VIRT_PCIE_MMIO].base, 2, memmap[VIRT_PCIE_MMIO].size);
  329. create_pcie_irq_map(fdt, name, plic_pcie_phandle);
  330. g_free(name);
  331. test_phandle = phandle++;
  332. name = g_strdup_printf("/soc/test@%lx",
  333. (long)memmap[VIRT_TEST].base);
  334. qemu_fdt_add_subnode(fdt, name);
  335. {
  336. const char compat[] = "sifive,test1\0sifive,test0\0syscon";
  337. qemu_fdt_setprop(fdt, name, "compatible", compat, sizeof(compat));
  338. }
  339. qemu_fdt_setprop_cells(fdt, name, "reg",
  340. 0x0, memmap[VIRT_TEST].base,
  341. 0x0, memmap[VIRT_TEST].size);
  342. qemu_fdt_setprop_cell(fdt, name, "phandle", test_phandle);
  343. test_phandle = qemu_fdt_get_phandle(fdt, name);
  344. g_free(name);
  345. name = g_strdup_printf("/soc/reboot");
  346. qemu_fdt_add_subnode(fdt, name);
  347. qemu_fdt_setprop_string(fdt, name, "compatible", "syscon-reboot");
  348. qemu_fdt_setprop_cell(fdt, name, "regmap", test_phandle);
  349. qemu_fdt_setprop_cell(fdt, name, "offset", 0x0);
  350. qemu_fdt_setprop_cell(fdt, name, "value", FINISHER_RESET);
  351. g_free(name);
  352. name = g_strdup_printf("/soc/poweroff");
  353. qemu_fdt_add_subnode(fdt, name);
  354. qemu_fdt_setprop_string(fdt, name, "compatible", "syscon-poweroff");
  355. qemu_fdt_setprop_cell(fdt, name, "regmap", test_phandle);
  356. qemu_fdt_setprop_cell(fdt, name, "offset", 0x0);
  357. qemu_fdt_setprop_cell(fdt, name, "value", FINISHER_PASS);
  358. g_free(name);
  359. name = g_strdup_printf("/soc/uart@%lx", (long)memmap[VIRT_UART0].base);
  360. qemu_fdt_add_subnode(fdt, name);
  361. qemu_fdt_setprop_string(fdt, name, "compatible", "ns16550a");
  362. qemu_fdt_setprop_cells(fdt, name, "reg",
  363. 0x0, memmap[VIRT_UART0].base,
  364. 0x0, memmap[VIRT_UART0].size);
  365. qemu_fdt_setprop_cell(fdt, name, "clock-frequency", 3686400);
  366. qemu_fdt_setprop_cell(fdt, name, "interrupt-parent", plic_mmio_phandle);
  367. qemu_fdt_setprop_cell(fdt, name, "interrupts", UART0_IRQ);
  368. qemu_fdt_add_subnode(fdt, "/chosen");
  369. qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", name);
  370. if (cmdline) {
  371. qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
  372. }
  373. g_free(name);
  374. name = g_strdup_printf("/soc/rtc@%lx", (long)memmap[VIRT_RTC].base);
  375. qemu_fdt_add_subnode(fdt, name);
  376. qemu_fdt_setprop_string(fdt, name, "compatible", "google,goldfish-rtc");
  377. qemu_fdt_setprop_cells(fdt, name, "reg",
  378. 0x0, memmap[VIRT_RTC].base,
  379. 0x0, memmap[VIRT_RTC].size);
  380. qemu_fdt_setprop_cell(fdt, name, "interrupt-parent", plic_mmio_phandle);
  381. qemu_fdt_setprop_cell(fdt, name, "interrupts", RTC_IRQ);
  382. g_free(name);
  383. name = g_strdup_printf("/soc/flash@%" PRIx64, flashbase);
  384. qemu_fdt_add_subnode(s->fdt, name);
  385. qemu_fdt_setprop_string(s->fdt, name, "compatible", "cfi-flash");
  386. qemu_fdt_setprop_sized_cells(s->fdt, name, "reg",
  387. 2, flashbase, 2, flashsize,
  388. 2, flashbase + flashsize, 2, flashsize);
  389. qemu_fdt_setprop_cell(s->fdt, name, "bank-width", 4);
  390. g_free(name);
  391. }
  392. static inline DeviceState *gpex_pcie_init(MemoryRegion *sys_mem,
  393. hwaddr ecam_base, hwaddr ecam_size,
  394. hwaddr mmio_base, hwaddr mmio_size,
  395. hwaddr pio_base,
  396. DeviceState *plic, bool link_up)
  397. {
  398. DeviceState *dev;
  399. MemoryRegion *ecam_alias, *ecam_reg;
  400. MemoryRegion *mmio_alias, *mmio_reg;
  401. qemu_irq irq;
  402. int i;
  403. dev = qdev_new(TYPE_GPEX_HOST);
  404. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  405. ecam_alias = g_new0(MemoryRegion, 1);
  406. ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
  407. memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
  408. ecam_reg, 0, ecam_size);
  409. memory_region_add_subregion(get_system_memory(), ecam_base, ecam_alias);
  410. mmio_alias = g_new0(MemoryRegion, 1);
  411. mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
  412. memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
  413. mmio_reg, mmio_base, mmio_size);
  414. memory_region_add_subregion(get_system_memory(), mmio_base, mmio_alias);
  415. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, pio_base);
  416. for (i = 0; i < GPEX_NUM_IRQS; i++) {
  417. irq = qdev_get_gpio_in(plic, PCIE_IRQ + i);
  418. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
  419. gpex_set_irq_num(GPEX_HOST(dev), i, PCIE_IRQ + i);
  420. }
  421. return dev;
  422. }
  423. static void virt_machine_init(MachineState *machine)
  424. {
  425. const struct MemmapEntry *memmap = virt_memmap;
  426. RISCVVirtState *s = RISCV_VIRT_MACHINE(machine);
  427. MemoryRegion *system_memory = get_system_memory();
  428. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  429. MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
  430. char *plic_hart_config, *soc_name;
  431. size_t plic_hart_config_len;
  432. target_ulong start_addr = memmap[VIRT_DRAM].base;
  433. uint32_t fdt_load_addr;
  434. uint64_t kernel_entry;
  435. DeviceState *mmio_plic, *virtio_plic, *pcie_plic;
  436. int i, j, base_hartid, hart_count;
  437. /* Check socket count limit */
  438. if (VIRT_SOCKETS_MAX < riscv_socket_count(machine)) {
  439. error_report("number of sockets/nodes should be less than %d",
  440. VIRT_SOCKETS_MAX);
  441. exit(1);
  442. }
  443. /* Initialize sockets */
  444. mmio_plic = virtio_plic = pcie_plic = NULL;
  445. for (i = 0; i < riscv_socket_count(machine); i++) {
  446. if (!riscv_socket_check_hartids(machine, i)) {
  447. error_report("discontinuous hartids in socket%d", i);
  448. exit(1);
  449. }
  450. base_hartid = riscv_socket_first_hartid(machine, i);
  451. if (base_hartid < 0) {
  452. error_report("can't find hartid base for socket%d", i);
  453. exit(1);
  454. }
  455. hart_count = riscv_socket_hart_count(machine, i);
  456. if (hart_count < 0) {
  457. error_report("can't find hart count for socket%d", i);
  458. exit(1);
  459. }
  460. soc_name = g_strdup_printf("soc%d", i);
  461. object_initialize_child(OBJECT(machine), soc_name, &s->soc[i],
  462. TYPE_RISCV_HART_ARRAY);
  463. g_free(soc_name);
  464. object_property_set_str(OBJECT(&s->soc[i]), "cpu-type",
  465. machine->cpu_type, &error_abort);
  466. object_property_set_int(OBJECT(&s->soc[i]), "hartid-base",
  467. base_hartid, &error_abort);
  468. object_property_set_int(OBJECT(&s->soc[i]), "num-harts",
  469. hart_count, &error_abort);
  470. sysbus_realize(SYS_BUS_DEVICE(&s->soc[i]), &error_abort);
  471. /* Per-socket CLINT */
  472. sifive_clint_create(
  473. memmap[VIRT_CLINT].base + i * memmap[VIRT_CLINT].size,
  474. memmap[VIRT_CLINT].size, base_hartid, hart_count,
  475. SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
  476. SIFIVE_CLINT_TIMEBASE_FREQ, true);
  477. /* Per-socket PLIC hart topology configuration string */
  478. plic_hart_config_len =
  479. (strlen(VIRT_PLIC_HART_CONFIG) + 1) * hart_count;
  480. plic_hart_config = g_malloc0(plic_hart_config_len);
  481. for (j = 0; j < hart_count; j++) {
  482. if (j != 0) {
  483. strncat(plic_hart_config, ",", plic_hart_config_len);
  484. }
  485. strncat(plic_hart_config, VIRT_PLIC_HART_CONFIG,
  486. plic_hart_config_len);
  487. plic_hart_config_len -= (strlen(VIRT_PLIC_HART_CONFIG) + 1);
  488. }
  489. /* Per-socket PLIC */
  490. s->plic[i] = sifive_plic_create(
  491. memmap[VIRT_PLIC].base + i * memmap[VIRT_PLIC].size,
  492. plic_hart_config, base_hartid,
  493. VIRT_PLIC_NUM_SOURCES,
  494. VIRT_PLIC_NUM_PRIORITIES,
  495. VIRT_PLIC_PRIORITY_BASE,
  496. VIRT_PLIC_PENDING_BASE,
  497. VIRT_PLIC_ENABLE_BASE,
  498. VIRT_PLIC_ENABLE_STRIDE,
  499. VIRT_PLIC_CONTEXT_BASE,
  500. VIRT_PLIC_CONTEXT_STRIDE,
  501. memmap[VIRT_PLIC].size);
  502. g_free(plic_hart_config);
  503. /* Try to use different PLIC instance based device type */
  504. if (i == 0) {
  505. mmio_plic = s->plic[i];
  506. virtio_plic = s->plic[i];
  507. pcie_plic = s->plic[i];
  508. }
  509. if (i == 1) {
  510. virtio_plic = s->plic[i];
  511. pcie_plic = s->plic[i];
  512. }
  513. if (i == 2) {
  514. pcie_plic = s->plic[i];
  515. }
  516. }
  517. /* register system main memory (actual RAM) */
  518. memory_region_init_ram(main_mem, NULL, "riscv_virt_board.ram",
  519. machine->ram_size, &error_fatal);
  520. memory_region_add_subregion(system_memory, memmap[VIRT_DRAM].base,
  521. main_mem);
  522. /* create device tree */
  523. create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline);
  524. /* boot rom */
  525. memory_region_init_rom(mask_rom, NULL, "riscv_virt_board.mrom",
  526. memmap[VIRT_MROM].size, &error_fatal);
  527. memory_region_add_subregion(system_memory, memmap[VIRT_MROM].base,
  528. mask_rom);
  529. riscv_find_and_load_firmware(machine, BIOS_FILENAME,
  530. memmap[VIRT_DRAM].base, NULL);
  531. if (machine->kernel_filename) {
  532. kernel_entry = riscv_load_kernel(machine->kernel_filename, NULL);
  533. if (machine->initrd_filename) {
  534. hwaddr start;
  535. hwaddr end = riscv_load_initrd(machine->initrd_filename,
  536. machine->ram_size, kernel_entry,
  537. &start);
  538. qemu_fdt_setprop_cell(s->fdt, "/chosen",
  539. "linux,initrd-start", start);
  540. qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
  541. end);
  542. }
  543. } else {
  544. /*
  545. * If dynamic firmware is used, it doesn't know where is the next mode
  546. * if kernel argument is not set.
  547. */
  548. kernel_entry = 0;
  549. }
  550. if (drive_get(IF_PFLASH, 0, 0)) {
  551. /*
  552. * Pflash was supplied, let's overwrite the address we jump to after
  553. * reset to the base of the flash.
  554. */
  555. start_addr = virt_memmap[VIRT_FLASH].base;
  556. }
  557. /* Compute the fdt load address in dram */
  558. fdt_load_addr = riscv_load_fdt(memmap[VIRT_DRAM].base,
  559. machine->ram_size, s->fdt);
  560. /* load the reset vector */
  561. riscv_setup_rom_reset_vec(start_addr, virt_memmap[VIRT_MROM].base,
  562. virt_memmap[VIRT_MROM].size, kernel_entry,
  563. fdt_load_addr, s->fdt);
  564. /* SiFive Test MMIO device */
  565. sifive_test_create(memmap[VIRT_TEST].base);
  566. /* VirtIO MMIO devices */
  567. for (i = 0; i < VIRTIO_COUNT; i++) {
  568. sysbus_create_simple("virtio-mmio",
  569. memmap[VIRT_VIRTIO].base + i * memmap[VIRT_VIRTIO].size,
  570. qdev_get_gpio_in(DEVICE(virtio_plic), VIRTIO_IRQ + i));
  571. }
  572. gpex_pcie_init(system_memory,
  573. memmap[VIRT_PCIE_ECAM].base,
  574. memmap[VIRT_PCIE_ECAM].size,
  575. memmap[VIRT_PCIE_MMIO].base,
  576. memmap[VIRT_PCIE_MMIO].size,
  577. memmap[VIRT_PCIE_PIO].base,
  578. DEVICE(pcie_plic), true);
  579. serial_mm_init(system_memory, memmap[VIRT_UART0].base,
  580. 0, qdev_get_gpio_in(DEVICE(mmio_plic), UART0_IRQ), 399193,
  581. serial_hd(0), DEVICE_LITTLE_ENDIAN);
  582. sysbus_create_simple("goldfish_rtc", memmap[VIRT_RTC].base,
  583. qdev_get_gpio_in(DEVICE(mmio_plic), RTC_IRQ));
  584. virt_flash_create(s);
  585. for (i = 0; i < ARRAY_SIZE(s->flash); i++) {
  586. /* Map legacy -drive if=pflash to machine properties */
  587. pflash_cfi01_legacy_drive(s->flash[i],
  588. drive_get(IF_PFLASH, 0, i));
  589. }
  590. virt_flash_map(s, system_memory);
  591. }
  592. static void virt_machine_instance_init(Object *obj)
  593. {
  594. }
  595. static void virt_machine_class_init(ObjectClass *oc, void *data)
  596. {
  597. MachineClass *mc = MACHINE_CLASS(oc);
  598. mc->desc = "RISC-V VirtIO board";
  599. mc->init = virt_machine_init;
  600. mc->max_cpus = VIRT_CPUS_MAX;
  601. mc->default_cpu_type = VIRT_CPU;
  602. mc->pci_allow_0_address = true;
  603. mc->possible_cpu_arch_ids = riscv_numa_possible_cpu_arch_ids;
  604. mc->cpu_index_to_instance_props = riscv_numa_cpu_index_to_props;
  605. mc->get_default_cpu_node_id = riscv_numa_get_default_cpu_node_id;
  606. mc->numa_mem_supported = true;
  607. }
  608. static const TypeInfo virt_machine_typeinfo = {
  609. .name = MACHINE_TYPE_NAME("virt"),
  610. .parent = TYPE_MACHINE,
  611. .class_init = virt_machine_class_init,
  612. .instance_init = virt_machine_instance_init,
  613. .instance_size = sizeof(RISCVVirtState),
  614. };
  615. static void virt_machine_init_register_types(void)
  616. {
  617. type_register_static(&virt_machine_typeinfo);
  618. }
  619. type_init(virt_machine_init_register_types)