opentitan.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * QEMU RISC-V Board Compatible with OpenTitan FPGA platform
  3. *
  4. * Copyright (c) 2020 Western Digital
  5. *
  6. * Provides a board compatible with the OpenTitan FPGA platform:
  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 "hw/riscv/opentitan.h"
  22. #include "qapi/error.h"
  23. #include "hw/boards.h"
  24. #include "hw/misc/unimp.h"
  25. #include "hw/riscv/boot.h"
  26. #include "exec/address-spaces.h"
  27. #include "qemu/units.h"
  28. #include "sysemu/sysemu.h"
  29. static const struct MemmapEntry {
  30. hwaddr base;
  31. hwaddr size;
  32. } ibex_memmap[] = {
  33. [IBEX_DEV_ROM] = { 0x00008000, 16 * KiB },
  34. [IBEX_DEV_RAM] = { 0x10000000, 0x10000 },
  35. [IBEX_DEV_FLASH] = { 0x20000000, 0x80000 },
  36. [IBEX_DEV_UART] = { 0x40000000, 0x10000 },
  37. [IBEX_DEV_GPIO] = { 0x40010000, 0x10000 },
  38. [IBEX_DEV_SPI] = { 0x40020000, 0x10000 },
  39. [IBEX_DEV_FLASH_CTRL] = { 0x40030000, 0x10000 },
  40. [IBEX_DEV_PINMUX] = { 0x40070000, 0x10000 },
  41. [IBEX_DEV_RV_TIMER] = { 0x40080000, 0x10000 },
  42. [IBEX_DEV_PLIC] = { 0x40090000, 0x10000 },
  43. [IBEX_DEV_PWRMGR] = { 0x400A0000, 0x10000 },
  44. [IBEX_DEV_RSTMGR] = { 0x400B0000, 0x10000 },
  45. [IBEX_DEV_CLKMGR] = { 0x400C0000, 0x10000 },
  46. [IBEX_DEV_AES] = { 0x40110000, 0x10000 },
  47. [IBEX_DEV_HMAC] = { 0x40120000, 0x10000 },
  48. [IBEX_DEV_ALERT_HANDLER] = { 0x40130000, 0x10000 },
  49. [IBEX_DEV_NMI_GEN] = { 0x40140000, 0x10000 },
  50. [IBEX_DEV_USBDEV] = { 0x40150000, 0x10000 },
  51. [IBEX_DEV_PADCTRL] = { 0x40160000, 0x10000 }
  52. };
  53. static void opentitan_board_init(MachineState *machine)
  54. {
  55. const struct MemmapEntry *memmap = ibex_memmap;
  56. OpenTitanState *s = g_new0(OpenTitanState, 1);
  57. MemoryRegion *sys_mem = get_system_memory();
  58. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  59. /* Initialize SoC */
  60. object_initialize_child(OBJECT(machine), "soc", &s->soc,
  61. TYPE_RISCV_IBEX_SOC);
  62. qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
  63. memory_region_init_ram(main_mem, NULL, "riscv.lowrisc.ibex.ram",
  64. memmap[IBEX_DEV_RAM].size, &error_fatal);
  65. memory_region_add_subregion(sys_mem,
  66. memmap[IBEX_DEV_RAM].base, main_mem);
  67. if (machine->firmware) {
  68. riscv_load_firmware(machine->firmware, memmap[IBEX_DEV_RAM].base, NULL);
  69. }
  70. if (machine->kernel_filename) {
  71. riscv_load_kernel(machine->kernel_filename, NULL);
  72. }
  73. }
  74. static void opentitan_machine_init(MachineClass *mc)
  75. {
  76. mc->desc = "RISC-V Board compatible with OpenTitan";
  77. mc->init = opentitan_board_init;
  78. mc->max_cpus = 1;
  79. mc->default_cpu_type = TYPE_RISCV_CPU_IBEX;
  80. }
  81. DEFINE_MACHINE("opentitan", opentitan_machine_init)
  82. static void lowrisc_ibex_soc_init(Object *obj)
  83. {
  84. LowRISCIbexSoCState *s = RISCV_IBEX_SOC(obj);
  85. object_initialize_child(obj, "cpus", &s->cpus, TYPE_RISCV_HART_ARRAY);
  86. object_initialize_child(obj, "plic", &s->plic, TYPE_IBEX_PLIC);
  87. object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
  88. }
  89. static void lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
  90. {
  91. const struct MemmapEntry *memmap = ibex_memmap;
  92. MachineState *ms = MACHINE(qdev_get_machine());
  93. LowRISCIbexSoCState *s = RISCV_IBEX_SOC(dev_soc);
  94. MemoryRegion *sys_mem = get_system_memory();
  95. object_property_set_str(OBJECT(&s->cpus), "cpu-type", ms->cpu_type,
  96. &error_abort);
  97. object_property_set_int(OBJECT(&s->cpus), "num-harts", ms->smp.cpus,
  98. &error_abort);
  99. object_property_set_int(OBJECT(&s->cpus), "resetvec", 0x8090, &error_abort);
  100. sysbus_realize(SYS_BUS_DEVICE(&s->cpus), &error_abort);
  101. /* Boot ROM */
  102. memory_region_init_rom(&s->rom, OBJECT(dev_soc), "riscv.lowrisc.ibex.rom",
  103. memmap[IBEX_DEV_ROM].size, &error_fatal);
  104. memory_region_add_subregion(sys_mem,
  105. memmap[IBEX_DEV_ROM].base, &s->rom);
  106. /* Flash memory */
  107. memory_region_init_rom(&s->flash_mem, OBJECT(dev_soc), "riscv.lowrisc.ibex.flash",
  108. memmap[IBEX_DEV_FLASH].size, &error_fatal);
  109. memory_region_add_subregion(sys_mem, memmap[IBEX_DEV_FLASH].base,
  110. &s->flash_mem);
  111. /* PLIC */
  112. if (!sysbus_realize(SYS_BUS_DEVICE(&s->plic), errp)) {
  113. return;
  114. }
  115. sysbus_mmio_map(SYS_BUS_DEVICE(&s->plic), 0, memmap[IBEX_DEV_PLIC].base);
  116. /* UART */
  117. qdev_prop_set_chr(DEVICE(&(s->uart)), "chardev", serial_hd(0));
  118. if (!sysbus_realize(SYS_BUS_DEVICE(&s->uart), errp)) {
  119. return;
  120. }
  121. sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart), 0, memmap[IBEX_DEV_UART].base);
  122. sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart),
  123. 0, qdev_get_gpio_in(DEVICE(&s->plic),
  124. IBEX_UART_TX_WATERMARK_IRQ));
  125. sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart),
  126. 1, qdev_get_gpio_in(DEVICE(&s->plic),
  127. IBEX_UART_RX_WATERMARK_IRQ));
  128. sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart),
  129. 2, qdev_get_gpio_in(DEVICE(&s->plic),
  130. IBEX_UART_TX_EMPTY_IRQ));
  131. sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart),
  132. 3, qdev_get_gpio_in(DEVICE(&s->plic),
  133. IBEX_UART_RX_OVERFLOW_IRQ));
  134. create_unimplemented_device("riscv.lowrisc.ibex.gpio",
  135. memmap[IBEX_DEV_GPIO].base, memmap[IBEX_DEV_GPIO].size);
  136. create_unimplemented_device("riscv.lowrisc.ibex.spi",
  137. memmap[IBEX_DEV_SPI].base, memmap[IBEX_DEV_SPI].size);
  138. create_unimplemented_device("riscv.lowrisc.ibex.flash_ctrl",
  139. memmap[IBEX_DEV_FLASH_CTRL].base, memmap[IBEX_DEV_FLASH_CTRL].size);
  140. create_unimplemented_device("riscv.lowrisc.ibex.rv_timer",
  141. memmap[IBEX_DEV_RV_TIMER].base, memmap[IBEX_DEV_RV_TIMER].size);
  142. create_unimplemented_device("riscv.lowrisc.ibex.pwrmgr",
  143. memmap[IBEX_DEV_PWRMGR].base, memmap[IBEX_DEV_PWRMGR].size);
  144. create_unimplemented_device("riscv.lowrisc.ibex.rstmgr",
  145. memmap[IBEX_DEV_RSTMGR].base, memmap[IBEX_DEV_RSTMGR].size);
  146. create_unimplemented_device("riscv.lowrisc.ibex.clkmgr",
  147. memmap[IBEX_DEV_CLKMGR].base, memmap[IBEX_DEV_CLKMGR].size);
  148. create_unimplemented_device("riscv.lowrisc.ibex.aes",
  149. memmap[IBEX_DEV_AES].base, memmap[IBEX_DEV_AES].size);
  150. create_unimplemented_device("riscv.lowrisc.ibex.hmac",
  151. memmap[IBEX_DEV_HMAC].base, memmap[IBEX_DEV_HMAC].size);
  152. create_unimplemented_device("riscv.lowrisc.ibex.pinmux",
  153. memmap[IBEX_DEV_PINMUX].base, memmap[IBEX_DEV_PINMUX].size);
  154. create_unimplemented_device("riscv.lowrisc.ibex.alert_handler",
  155. memmap[IBEX_DEV_ALERT_HANDLER].base, memmap[IBEX_DEV_ALERT_HANDLER].size);
  156. create_unimplemented_device("riscv.lowrisc.ibex.nmi_gen",
  157. memmap[IBEX_DEV_NMI_GEN].base, memmap[IBEX_DEV_NMI_GEN].size);
  158. create_unimplemented_device("riscv.lowrisc.ibex.usbdev",
  159. memmap[IBEX_DEV_USBDEV].base, memmap[IBEX_DEV_USBDEV].size);
  160. create_unimplemented_device("riscv.lowrisc.ibex.padctrl",
  161. memmap[IBEX_DEV_PADCTRL].base, memmap[IBEX_DEV_PADCTRL].size);
  162. }
  163. static void lowrisc_ibex_soc_class_init(ObjectClass *oc, void *data)
  164. {
  165. DeviceClass *dc = DEVICE_CLASS(oc);
  166. dc->realize = lowrisc_ibex_soc_realize;
  167. /* Reason: Uses serial_hds in realize function, thus can't be used twice */
  168. dc->user_creatable = false;
  169. }
  170. static const TypeInfo lowrisc_ibex_soc_type_info = {
  171. .name = TYPE_RISCV_IBEX_SOC,
  172. .parent = TYPE_DEVICE,
  173. .instance_size = sizeof(LowRISCIbexSoCState),
  174. .instance_init = lowrisc_ibex_soc_init,
  175. .class_init = lowrisc_ibex_soc_class_init,
  176. };
  177. static void lowrisc_ibex_soc_register_types(void)
  178. {
  179. type_register_static(&lowrisc_ibex_soc_type_info);
  180. }
  181. type_init(lowrisc_ibex_soc_register_types)