Skip to contentSkip to conversation

hw/i386: Add MTD controller on ICH9

PATCH In review Patrick Rudolph 2 participants 5 messages Oct 18, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover hw/i386: Add MTD controller on ICH9

This series implements a feature complete MTD SPI flash controller as found on real ICH9, adds cache coherent BIOS MMAPing and implements a few SMM bits to allow x86 firmware build for real ICH9 to operate on the flash controller to store (UEFI) variables or do firmware updates.

Patrick Rudolph (3):
hw/isa/ich9: Add SPI controller
hw/isa/lpc_ich9: Implement SMI_STS for APMC hw/i386/pc_sysfw: Do not create BIOS region if MTD is present

 hw/acpi/ich9.c                    |   5 +
 hw/acpi/ich9_tco.c                |   5 +
 hw/i386/pc_sysfw.c                |   6 +
 hw/isa/Kconfig                    |   2 +
 hw/isa/lpc_ich9.c                 |   4 +
 hw/isa/meson.build                |   2 +-
 hw/isa/spi_ich9.c                 | 448 ++++++++++++++++++++++++++++++
 include/hw/southbridge/ich9.h     |   6 +
 include/hw/southbridge/ich9_spi.h |  80 ++++++
 9 files changed, 557 insertions(+), 1 deletion(-)
 create mode 100644 hw/isa/spi_ich9.c
 create mode 100644 include/hw/southbridge/ich9_spi.h
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/3 hw/isa/ich9: Add SPI controller

Implement a feature complete SPI controller that resides in the LPC RCRB-MMIO space as on real ICH9.

Besides having a memory-mapped flash at top of 4GiB, the ICH9 SPI controller allows to work in two modes: HW sequencing and SW sequencing. In HW sequencing, which only works in "descriptor mode", the user doesn't need any knowledge of the SPI flash.
The SPI controller issues the correct commands were needed.

This commit introduces SW sequencing, as "descriptor mode" does require a 4 KiB proprietary data structure at the start of the SPI flash. In SW sequencing the user must identify the SPI flash and generate commands the SPI flash understands.

The SPIBAR is placed at offset 0x3800 on RCRB-MMIO space on the LPC PCI device.

As BIOS is mmaped on ICH9 enable BIOS emulation and synchronization when the SPI flash was modified. This is required for UEFI variable store operating on the newly introduced SPI controller.

When no MTD device is given a empty SPI flash is emulated that is not MMAPed, thus the existing mechanisms all continue to work.

To enable BIOS MMAP and use the SPI flash as boot device add the argument:
-drive if=mtd,format=raw,file=...

TEST: Run "flashrom -p internal" inside the VM.
flashrom is able to read the virtual flash.

TEST: Boot coreboot build for real ICH9 and be able to use UEFI
variable store on top of the implemented ICH9 SPI controller.

---
 hw/isa/Kconfig                    |   2 +
 hw/isa/lpc_ich9.c                 |   3 +
 hw/isa/meson.build                |   2 +-
 hw/isa/spi_ich9.c                 | 448 ++++++++++++++++++++++++++++++
 include/hw/southbridge/ich9.h     |   4 +
 include/hw/southbridge/ich9_spi.h |  80 ++++++
 6 files changed, 538 insertions(+), 1 deletion(-)
 create mode 100644 hw/isa/spi_ich9.c
 create mode 100644 include/hw/southbridge/ich9_spi.h
hw/isa/Kconfig +2 -0
index c10cbc5fc1..92f6dd5e5d 100644
@@ -81,3 +81,5 @@ config LPC_ICH9
     select ISA_BUS
     select ACPI_ICH9
     select MC146818RTC
+    select SSI
+    select SSI_M25P80
hw/isa/lpc_ich9.c +3 -0
index 3f59980aa0..01d6a46c3d 100644
@@ -41,6 +41,7 @@
 #include "hw/isa/apm.h"
 #include "hw/pci/pci.h"
 #include "hw/southbridge/ich9.h"
+#include "hw/southbridge/ich9_spi.h"
 #include "hw/i386/pc.h"
 #include "hw/acpi/acpi.h"
 #include "hw/acpi/ich9.h"
@@ -751,6 +752,7 @@ static void ich9_lpc_realize(PCIDevice *d, Error **errp)
     pci_bus_set_route_irq_fn(pci_bus, ich9_route_intx_pin_to_irq);
 
     ich9_lpc_pm_init(lpc);
+    ich9_spi_init(PCI_DEVICE(lpc), &lpc->spi, &lpc->rcrb_mem);
 }
 
 static bool ich9_rst_cnt_needed(void *opaque)
@@ -803,6 +805,7 @@ static const VMStateDescription vmstate_ich9_lpc = {
         VMSTATE_PCI_DEVICE(d, ICH9LPCState),
         VMSTATE_STRUCT(apm, ICH9LPCState, 0, vmstate_apm, APMState),
         VMSTATE_STRUCT(pm, ICH9LPCState, 0, vmstate_ich9_pm, ICH9LPCPMRegs),
+        VMSTATE_STRUCT(spi, ICH9LPCState, 0, vmstate_ich9_spi, ICH9SPIState),
         VMSTATE_UINT8_ARRAY(chip_config, ICH9LPCState, ICH9_CC_SIZE),
         VMSTATE_UINT32(sci_level, ICH9LPCState),
         VMSTATE_END_OF_LIST()
hw/isa/meson.build +1 -1
index b855e81276..d27289a693 100644
@@ -8,4 +8,4 @@ system_ss.add(when: 'CONFIG_PIIX4', if_true: files('piix4.c'))
 system_ss.add(when: 'CONFIG_SMC37C669', if_true: files('smc37c669-superio.c'))
 system_ss.add(when: 'CONFIG_VT82C686', if_true: files('vt82c686.c'))
 
-specific_ss.add(when: 'CONFIG_LPC_ICH9', if_true: files('lpc_ich9.c'))
+specific_ss.add(when: 'CONFIG_LPC_ICH9', if_true: files('lpc_ich9.c', 'spi_ich9.c'))
hw/isa/spi_ich9.c new file +448 -0

448 lines not on this page · raw message

include/hw/southbridge/ich9.h +4 -0
index fd01649d04..1281eb654c 100644
@@ -7,6 +7,8 @@
 #include "hw/pci/pci.h"
 #include "hw/pci/pci_device.h"
 #include "hw/rtc/mc146818rtc.h"
+#include "hw/sysbus.h"
+#include "hw/southbridge/ich9_spi.h"
 #include "exec/memory.h"
 #include "qemu/notify.h"
 #include "qom/object.h"
@@ -34,6 +36,7 @@ struct ICH9LPCState {
     MC146818RtcState rtc;
     APMState apm;
     ICH9LPCPMRegs pm;
+    ICH9SPIState spi;
     uint32_t sci_level; /* track sci level */
     uint8_t sci_gsi;
 
@@ -97,6 +100,7 @@ struct ICH9LPCState {
 #define ICH9_CC_GCS                             0x3410
 #define ICH9_CC_GCS_DEFAULT                     0x00000020
 #define ICH9_CC_GCS_NO_REBOOT                   (1 << 5)
+#define ICH9_LPC_RCBA_SPIBAR                    0x3800
 
 /* D28:F[0-5] */
 #define ICH9_PCIE_DEV                           28
include/hw/southbridge/ich9_spi.h new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.41.0
#2 lore raw

PATCH 2/3 hw/isa/lpc_ich9: Implement SMI_STS for APMC

In reply to Patrick Rudolph

The guest SMI handler wants to know the cause for the SMI, thus properly emulate the RWC APMC and TCO bit in the SMI_STS register.

TEST: coreboot generic ICH9 SMI handler is able to determine
the source for the SMI and will invoke the APM callback.

---
 hw/acpi/ich9.c                | 5 +++++
 hw/acpi/ich9_tco.c            | 5 +++++
 hw/isa/lpc_ich9.c             | 1 +
 include/hw/southbridge/ich9.h | 2 ++
 4 files changed, 13 insertions(+)
hw/acpi/ich9.c +5 -0
index 25e2c7243e..74d1824595 100644
@@ -98,6 +98,7 @@ static void ich9_smi_writel(void *opaque, hwaddr addr, uint64_t val,
     ICH9LPCPMRegs *pm = opaque;
     TCOIORegs *tr = &pm->tco_regs;
     uint64_t tco_en;
+    uint32_t mask;
 
     switch (addr) {
     case 0:
@@ -109,6 +110,10 @@ static void ich9_smi_writel(void *opaque, hwaddr addr, uint64_t val,
         pm->smi_en &= ~pm->smi_en_wmask;
         pm->smi_en |= (val & pm->smi_en_wmask);
         break;
+    case 4:
+        mask = pm->smi_sts & ~val;
+        pm->smi_sts &= ~(ICH9_PMIO_SMI_STS_TCO | ICH9_PMIO_SMI_STS_APMC);
+        pm->smi_sts |= mask & (ICH9_PMIO_SMI_STS_TCO | ICH9_PMIO_SMI_STS_APMC);
     }
 }
 
hw/acpi/ich9_tco.c +5 -0
index 1540f4fd46..0f1cb19864 100644
@@ -71,6 +71,7 @@ static void tco_timer_expired(void *opaque)
     }
 
     if (pm->smi_en & ICH9_PMIO_SMI_EN_TCO_EN) {
+        lpc->pm.smi_sts |= ICH9_PMIO_SMI_STS_TCO;
         ich9_generate_smi();
     }
     tr->tco.rld = tr->tco.tmr;
@@ -139,6 +140,9 @@ static uint32_t tco_ioport_readw(TCOIORegs *tr, uint32_t addr)
 
 static void tco_ioport_writew(TCOIORegs *tr, uint32_t addr, uint32_t val)
 {
+    ICH9LPCPMRegs *pm = container_of(tr, ICH9LPCPMRegs, tco_regs);
+    ICH9LPCState *lpc = container_of(pm, ICH9LPCState, pm);
+
     trace_tco_io_write(addr, val);
     switch (addr) {
     case TCO_RLD:
@@ -153,6 +157,7 @@ static void tco_ioport_writew(TCOIORegs *tr, uint32_t addr, uint32_t val)
     case TCO_DAT_IN:
         tr->tco.din = val;
         tr->tco.sts1 |= SW_TCO_SMI;
+        lpc->pm.smi_sts |= ICH9_PMIO_SMI_STS_TCO;
         ich9_generate_smi();
         break;
     case TCO_DAT_OUT:
hw/isa/lpc_ich9.c +1 -0
index 01d6a46c3d..eb25e6429e 100644
@@ -471,6 +471,7 @@ static void ich9_apm_ctrl_changed(uint32_t val, void *arg)
 
     /* SMI_EN = PMBASE + 30. SMI control and enable register */
     if (lpc->pm.smi_en & ICH9_PMIO_SMI_EN_APMC_EN) {
+        lpc->pm.smi_sts |= ICH9_PMIO_SMI_STS_APMC;
         if (lpc->smi_negotiated_features &
             (UINT64_C(1) << ICH9_LPC_SMI_F_BROADCAST_BIT)) {
             CPUState *cs;
include/hw/southbridge/ich9.h +2 -0
index 1281eb654c..4f5d6cefaa 100644
@@ -202,6 +202,8 @@ struct ICH9LPCState {
 #define ICH9_PMIO_SMI_EN_APMC_EN                (1 << 5)
 #define ICH9_PMIO_SMI_EN_TCO_EN                 (1 << 13)
 #define ICH9_PMIO_SMI_STS                       0x34
+#define ICH9_PMIO_SMI_STS_APMC                  (1 << 5)
+#define ICH9_PMIO_SMI_STS_TCO                   (1 << 13)
 #define ICH9_PMIO_TCO_RLD                       0x60
 #define ICH9_PMIO_TCO_LEN                       32
 
Signature · 1 line
2.41.0
#3 lore raw

PATCH 3/3 hw/i386/pc_sysfw: Do not create BIOS region if MTD is present

In reply to Patrick Rudolph

The newly introduced ICH9 SPI MTD controller creates the BIOS region itself as it needs to update the mmaped BIOS region as well when the MTD device is updated though the SPI controller.

---
 hw/i386/pc_sysfw.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/i386/pc_sysfw.c +6 -0
index c8d9e71b88..b98077422c 100644
@@ -211,6 +211,12 @@ void pc_system_firmware_init(PCMachineState *pcms,
         x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true);
         return;
     }
+    DriveInfo *dinfo = drive_get(IF_MTD, 0, 0);
+    if (dinfo) {
+        /* SPI flash and BIOS will be emulated by MTD controller */
+        pc_system_flash_cleanup_unused(pcms);
+        return;
+    }
 
     /* Map legacy -drive if=pflash to machine properties */
     for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
Signature · 1 line
2.41.0
#4 lore raw
In reply to Patrick Rudolph
Quoted 9 lines · Patrick Rudolph

This series implements a feature complete MTD SPI flash controller as found on real ICH9, adds cache coherent BIOS MMAPing and implements a few SMM bits to allow x86 firmware build for real ICH9 to operate on the flash controller to store (UEFI) variables or do firmware updates.

Patrick Rudolph (3):
hw/isa/ich9: Add SPI controller
hw/isa/lpc_ich9: Implement SMI_STS for APMC hw/i386/pc_sysfw: Do not create BIOS region if MTD is present

Given all this is guest-visible, one thing we need for sure is a flag disabling this new stuff, and tied to existing machine types.

Quoted 15 lines
 hw/acpi/ich9.c                    |   5 +
 hw/acpi/ich9_tco.c                |   5 +
 hw/i386/pc_sysfw.c                |   6 +
 hw/isa/Kconfig                    |   2 +
 hw/isa/lpc_ich9.c                 |   4 +
 hw/isa/meson.build                |   2 +-
 hw/isa/spi_ich9.c                 | 448 ++++++++++++++++++++++++++++++
 include/hw/southbridge/ich9.h     |   6 +
 include/hw/southbridge/ich9_spi.h |  80 ++++++
 9 files changed, 557 insertions(+), 1 deletion(-)
 create mode 100644 hw/isa/spi_ich9.c
 create mode 100644 include/hw/southbridge/ich9_spi.h
Signature · 1 line
2.41.0