diff mbox series

[2/4] hw/m68k/virt: Add fw_cfg controller

Message ID 20240527-m68k-bios-v1-2-6de26552fa77@flygoat.com
State New
Headers show
Series hw/m68k/virt: Add some devices | expand

Commit Message

Jiaxun Yang May 27, 2024, 5:15 p.m. UTC
Add a fw_cfg controller to pass essential information
to firmware.

Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
 hw/m68k/Kconfig                                   |  1 +
 hw/m68k/virt.c                                    | 25 ++++++++++++++++++++++-
 include/standard-headers/asm-m68k/bootinfo-virt.h |  1 +
 3 files changed, 26 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/hw/m68k/Kconfig b/hw/m68k/Kconfig
index 66e63cd60b57..4501da56ff6d 100644
--- a/hw/m68k/Kconfig
+++ b/hw/m68k/Kconfig
@@ -40,6 +40,7 @@  config M68K_VIRT
     default y
     depends on M68K
     select M68K_IRQC
+    select FW_CFG_DMA
     select VIRT_CTRL
     select GOLDFISH_PIC
     select GOLDFISH_TTY
diff --git a/hw/m68k/virt.c b/hw/m68k/virt.c
index 7b3917dcbd2b..7590e6515ac3 100644
--- a/hw/m68k/virt.c
+++ b/hw/m68k/virt.c
@@ -42,7 +42,8 @@ 
  * CPU IRQ #1 -> PIC #1
  *               IRQ #1 -> virt-ctrl
  *               IRQ #2 -> xhci
- *               IRQ #3 to IRQ #31 -> unused
+ *               IRQ #3 -> fw_cfg
+ *               IRQ #4 to IRQ #31 -> unused
  *               IRQ #32 -> goldfish-tty
  * CPU IRQ #2 -> PIC #2
  *               IRQ #1 to IRQ #32 -> virtio-mmio from 1 to 32
@@ -80,6 +81,10 @@ 
 #define VIRT_CTRL_MMIO_BASE 0xff009000    /* MMIO: 0xff009000 - 0xff009fff */
 #define VIRT_CTRL_IRQ_BASE  PIC_IRQ(1, 1) /* PIC: #1, IRQ: #1 */
 
+/* 1 fw_cfg */
+#define VIRT_FW_CFG_MMIO_BASE 0xff00a000    /* MMIO: 0xff00a000 - 0xff00afff */
+#define VIRT_FW_CFG_IRQ_BASE  PIC_IRQ(1, 3) /* PIC: #1, IRQ: #3 */
+
 /*
  * virtio-mmio size is 0x200 bytes
  * we use 4 goldfish-pic to attach them,
@@ -116,6 +121,12 @@  static void rerandomize_rng_seed(void *opaque)
                                 be16_to_cpu(*(uint16_t *)rng_seed->data));
 }
 
+static void fw_cfg_boot_set(void *opaque, const char *boot_device,
+                            Error **errp)
+{
+    fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
+}
+
 static void virt_init(MachineState *machine)
 {
     M68kCPU *cpu = NULL;
@@ -134,6 +145,7 @@  static void virt_init(MachineState *machine)
     SysBusDevice *sysbus;
     hwaddr io_base;
     int i;
+    FWCfgState *fw_cfg;
     ResetInfo *reset_info;
     uint8_t rng_seed[32];
 
@@ -210,6 +222,15 @@  static void virt_init(MachineState *machine)
     dev = sysbus_create_simple(TYPE_VIRT_CTRL, VIRT_CTRL_MMIO_BASE,
                                PIC_GPIO(VIRT_CTRL_IRQ_BASE));
 
+    /* fw_cfg */
+    fw_cfg = fw_cfg_init_mem_wide(VIRT_FW_CFG_MMIO_BASE + 8,
+                                  VIRT_FW_CFG_MMIO_BASE, 8,
+                                  VIRT_FW_CFG_MMIO_BASE + 16,
+                                  &address_space_memory);
+    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
+    qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
+    rom_set_fw(fw_cfg);
+
     /* virtio-mmio */
     io_base = VIRT_VIRTIO_MMIO_BASE;
     for (i = 0; i < 128; i++) {
@@ -288,6 +309,8 @@  static void virt_init(MachineState *machine)
             BOOTINFO2(param_ptr, BI_VIRT_XHCI_BASE,
                     VIRT_XHCI_MMIO_BASE, VIRT_XHCI_IRQ_BASE);
         }
+        BOOTINFO2(param_ptr, BI_VIRT_FW_CFG_BASE,
+                  VIRT_FW_CFG_MMIO_BASE, VIRT_FW_CFG_IRQ_BASE);
 
         if (kernel_cmdline) {
             BOOTINFOSTR(param_ptr, BI_COMMAND_LINE,
diff --git a/include/standard-headers/asm-m68k/bootinfo-virt.h b/include/standard-headers/asm-m68k/bootinfo-virt.h
index 1700c3ada765..7f90be1aa7bd 100644
--- a/include/standard-headers/asm-m68k/bootinfo-virt.h
+++ b/include/standard-headers/asm-m68k/bootinfo-virt.h
@@ -17,6 +17,7 @@ 
  * #define BI_VIRT_RNG_SEED	0x8006 */
 
 #define BI_VIRT_XHCI_BASE	0x8007
+#define BI_VIRT_FW_CFG_BASE	0x8008
 
 #define VIRT_BOOTI_VERSION	MK_BI_VERSION(2, 0)