[PATCH] hw/arm/virt: Add optional secure PL022 SPI controller with SPI NOR flash

Bhaskar Valaboju posted 1 patch 4 days, 7 hours ago
docs/system/arm/virt.rst |  11 ++++
hw/arm/Kconfig           |   2 +
hw/arm/virt.c            | 140 +++++++++++++++++++++++++++++++++++++++++++++++
include/hw/arm/virt.h    |   3 +
4 files changed, 156 insertions(+)
[PATCH] hw/arm/virt: Add optional secure PL022 SPI controller with SPI NOR flash
Posted by Bhaskar Valaboju 4 days, 7 hours ago
Secure-World firmware on virt has no flash it can own: the secure
flash is a CFI pflash bank holding the boot firmware. Firmware such
as OP-TEE that keeps its data in a SPI NOR flash cannot be tested on
virt with a real SPI controller and flash model.

Add a "secure-spi" machine property (default off) that gives the
Secure World a PL022 SPI controller with a 32MB Winbond W25Q256 SPI NOR
flash on chip select 0. The PL022 has no chip select output, so the
flash CS# is driven by line 0 of a dedicated PL061 GPIO controller.
Like the secure UART and GPIO, both devices are mapped in the secure
address space only, and their DT nodes are marked status = "disabled"
and secure-status = "okay". The flash contents are backed by
"-drive if=mtd,index=0".

secure-spi=on requires secure=on. With the default of off, the machine
is unchanged.

Signed-off-by: Bhaskar Valaboju <bhaskar.valaboju@oss.qualcomm.com>
---
This adds a SPI controller with a SPI NOR flash to the virt machine,
for Secure-World firmware, so SPI NOR flash drivers can be developed
and tested against a real controller and flash model.

Tested on qemu-system-arm with OP-TEE (vexpress-qemu_virt) and
-machine virt,secure=on,secure-spi=on, using OP-TEE's existing PL022
and PL061 drivers and its SPI NOR secure storage, under review at
https://github.com/OP-TEE/optee_os/pull/8318: the flash is probed
through the PL022 with the PL061 chip select (JEDEC ID ef 40 19,
SFDP), OP-TEE secure storage on the flash passes xtest, and the
contents persist across reboots with
-drive if=mtd,index=0,format=raw,file=flash.img. secure-spi=on without
secure=on is rejected.
---
 docs/system/arm/virt.rst |  11 ++++
 hw/arm/Kconfig           |   2 +
 hw/arm/virt.c            | 140 +++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/arm/virt.h    |   3 +
 4 files changed, 156 insertions(+)

diff --git a/docs/system/arm/virt.rst b/docs/system/arm/virt.rst
index 5f6dd17978..1f28b37496 100644
--- a/docs/system/arm/virt.rst
+++ b/docs/system/arm/virt.rst
@@ -56,6 +56,8 @@ The virt board supports:
   - A second PL061 GPIO controller, with GPIO lines for triggering
     a system reset or system poweroff
   - A secure flash memory
+  - An optional PL022 SPI controller with a SPI NOR flash
+    (see ``secure-spi``)
   - 16MB of secure RAM
 
 The second NonSecure UART only exists if a backend is configured
@@ -112,6 +114,15 @@ secure
   Set ``on``/``off`` to enable/disable emulating a guest CPU which implements the
   Arm Security Extensions (TrustZone). The default is ``off``.
 
+secure-spi
+  Set ``on``/``off`` to enable/disable a Secure-World-only PL022 SPI
+  controller with a 32MB Winbond W25Q256 SPI NOR flash. The PL022 has no
+  chip select output, so a dedicated Secure-World-only PL061 GPIO
+  controller drives the flash chip select on its line 0. The flash
+  contents are backed by ``-drive if=mtd,index=0``; without it the flash
+  starts erased and its contents are lost at exit. Requires ``secure=on``.
+  The default is ``off``.
+
 virtualization
   Set ``on``/``off`` to enable/disable emulating a guest CPU which implements the
   Arm Virtualization Extensions. The default is ``off``.
diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
index e9f796f718..4f4bfdaf42 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -21,6 +21,8 @@ config ARM_VIRT
     select PL011 # UART
     select PL031 # RTC
     select PL061 # GPIO
+    select PL022 # SPI
+    select SSI_M25P80
     select GPIO_PWR
     select PLATFORM_BUS
     select SMBIOS
diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 0b11f7e328..204726c06a 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -62,6 +62,10 @@
 #include "hw/core/sysbus-fdt.h"
 #include "hw/core/platform-bus.h"
 #include "hw/core/qdev-properties.h"
+#include "hw/core/qdev-properties-system.h"
+#include "hw/ssi/ssi.h"
+#include "system/blockdev.h"
+#include "system/block-backend.h"
 #include "hw/arm/fdt.h"
 #include "hw/intc/arm_gic.h"
 #include "hw/intc/arm_gicv3_common.h"
@@ -210,6 +214,8 @@ static const MemMapEntry base_memmap[] = {
     [VIRT_PVTIME] =             { 0x090a0000, 0x00010000 },
     [VIRT_SECURE_GPIO] =        { 0x090b0000, 0x00001000 },
     [VIRT_ACPI_PCIHP] =         { 0x090c0000, ACPI_PCIHP_SIZE },
+    [VIRT_SECURE_SPI] =         { 0x090d0000, 0x00001000 },
+    [VIRT_SECURE_SPI_GPIO] =    { 0x090e0000, 0x00001000 },
     [VIRT_MMIO] =               { 0x0a000000, 0x00000200 },
     /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
     [VIRT_PLATFORM_BUS] =       { 0x0c000000, 0x02000000 },
@@ -267,6 +273,8 @@ static const int a15irqmap[] = {
     [VIRT_UART1] = 8,
     [VIRT_ACPI_GED] = 9,
     [VIRT_GWDT_WS0] = 10,
+    [VIRT_SECURE_SPI] = 11,
+    [VIRT_SECURE_SPI_GPIO] = 12,
     [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
     [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
     [VIRT_SMMU] = 74,    /* ...to 74 + NUM_SMMU_IRQS - 1 */
@@ -1718,6 +1726,109 @@ static void create_gpio_devices(const VirtMachineState *vms, int gpio,
     }
 }
 
+/* Line of the secure SPI PL061 that drives the flash CS# */
+#define SECURE_SPI_GPIO_CS 0
+
+/*
+ * Secure-World-only PL022 SPI controller with a SPI NOR flash, for
+ * firmware such as OP-TEE that keeps its data in flash. The PL022 has
+ * no chip select output, so the flash CS# is driven by a dedicated PL061
+ * GPIO controller. The flash contents come from "-drive if=mtd,index=0".
+ */
+static void create_secure_spi(const VirtMachineState *vms, MemoryRegion *mem)
+{
+    hwaddr base = vms->memmap[VIRT_SECURE_SPI].base;
+    hwaddr size = vms->memmap[VIRT_SECURE_SPI].size;
+    hwaddr gpio_base = vms->memmap[VIRT_SECURE_SPI_GPIO].base;
+    hwaddr gpio_size = vms->memmap[VIRT_SECURE_SPI_GPIO].size;
+    int irq = vms->irqmap[VIRT_SECURE_SPI];
+    int gpio_irq = vms->irqmap[VIRT_SECURE_SPI_GPIO];
+    const char gpio_compat[] = "arm,pl061\0arm,primecell";
+    const char spi_compat[] = "arm,pl022\0arm,primecell";
+    const char spi_clocknames[] = "sspclk\0apb_pclk";
+    MachineState *ms = MACHINE(vms);
+    DeviceState *pl022, *pl061, *flash;
+    DriveInfo *dinfo;
+    SysBusDevice *s;
+    SSIBus *bus;
+    uint32_t gpio_phandle;
+    char *gpio_node, *spi_node, *flash_node;
+
+    pl061 = qdev_new("pl061");
+    /* Keep CS# deasserted until the guest drives it */
+    qdev_prop_set_uint8(pl061, "pullups", BIT(SECURE_SPI_GPIO_CS));
+    qdev_prop_set_uint8(pl061, "pulldowns", 0xff & ~BIT(SECURE_SPI_GPIO_CS));
+    s = SYS_BUS_DEVICE(pl061);
+    sysbus_realize_and_unref(s, &error_fatal);
+    memory_region_add_subregion(mem, gpio_base, sysbus_mmio_get_region(s, 0));
+    sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, gpio_irq));
+
+    pl022 = qdev_new("pl022");
+    s = SYS_BUS_DEVICE(pl022);
+    sysbus_realize_and_unref(s, &error_fatal);
+    memory_region_add_subregion(mem, base, sysbus_mmio_get_region(s, 0));
+    sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
+    bus = (SSIBus *)qdev_get_child_bus(pl022, "ssi");
+
+    flash = qdev_new("w25q256");
+    dinfo = drive_get(IF_MTD, 0, 0);
+    if (dinfo) {
+        qdev_prop_set_drive_err(flash, "drive", blk_by_legacy_dinfo(dinfo),
+                                &error_fatal);
+    }
+    ssi_realize_and_unref(flash, bus, &error_fatal);
+    qdev_connect_gpio_out(pl061, SECURE_SPI_GPIO_CS,
+                          qdev_get_gpio_in_named(flash, SSI_GPIO_CS, 0));
+
+    gpio_phandle = qemu_fdt_alloc_phandle(ms->fdt);
+    gpio_node = g_strdup_printf("/pl061@%" PRIx64, gpio_base);
+    qemu_fdt_add_subnode(ms->fdt, gpio_node);
+    qemu_fdt_setprop_sized_cells(ms->fdt, gpio_node, "reg",
+                                 2, gpio_base, 2, gpio_size);
+    qemu_fdt_setprop(ms->fdt, gpio_node, "compatible",
+                     gpio_compat, sizeof(gpio_compat));
+    qemu_fdt_setprop_cell(ms->fdt, gpio_node, "#gpio-cells", 2);
+    qemu_fdt_setprop(ms->fdt, gpio_node, "gpio-controller", NULL, 0);
+    qemu_fdt_setprop_cells(ms->fdt, gpio_node, "interrupts",
+                           gic_fdt_irq_type_spi(vms), gpio_irq,
+                           GIC_FDT_IRQ_FLAGS_LEVEL_HI);
+    qemu_fdt_setprop_cell(ms->fdt, gpio_node, "clocks", vms->clock_phandle);
+    qemu_fdt_setprop_string(ms->fdt, gpio_node, "clock-names", "apb_pclk");
+    qemu_fdt_setprop_cell(ms->fdt, gpio_node, "phandle", gpio_phandle);
+    qemu_fdt_setprop_string(ms->fdt, gpio_node, "status", "disabled");
+    qemu_fdt_setprop_string(ms->fdt, gpio_node, "secure-status", "okay");
+
+    spi_node = g_strdup_printf("/spi@%" PRIx64, base);
+    qemu_fdt_add_subnode(ms->fdt, spi_node);
+    qemu_fdt_setprop_sized_cells(ms->fdt, spi_node, "reg", 2, base, 2, size);
+    qemu_fdt_setprop(ms->fdt, spi_node, "compatible",
+                     spi_compat, sizeof(spi_compat));
+    qemu_fdt_setprop_cells(ms->fdt, spi_node, "interrupts",
+                           gic_fdt_irq_type_spi(vms), irq,
+                           GIC_FDT_IRQ_FLAGS_LEVEL_HI);
+    qemu_fdt_setprop_cells(ms->fdt, spi_node, "clocks",
+                           vms->clock_phandle, vms->clock_phandle);
+    qemu_fdt_setprop(ms->fdt, spi_node, "clock-names",
+                     spi_clocknames, sizeof(spi_clocknames));
+    qemu_fdt_setprop_cell(ms->fdt, spi_node, "#address-cells", 1);
+    qemu_fdt_setprop_cell(ms->fdt, spi_node, "#size-cells", 0);
+    /* CS# is active low */
+    qemu_fdt_setprop_cells(ms->fdt, spi_node, "cs-gpios",
+                           gpio_phandle, SECURE_SPI_GPIO_CS, 1);
+    qemu_fdt_setprop_string(ms->fdt, spi_node, "status", "disabled");
+    qemu_fdt_setprop_string(ms->fdt, spi_node, "secure-status", "okay");
+
+    flash_node = g_strdup_printf("%s/flash@0", spi_node);
+    qemu_fdt_add_subnode(ms->fdt, flash_node);
+    qemu_fdt_setprop_string(ms->fdt, flash_node, "compatible", "jedec,spi-nor");
+    qemu_fdt_setprop_cell(ms->fdt, flash_node, "reg", 0);
+    qemu_fdt_setprop_cell(ms->fdt, flash_node, "spi-max-frequency", 12000000);
+
+    g_free(flash_node);
+    g_free(spi_node);
+    g_free(gpio_node);
+}
+
 static void create_virtio_devices(const VirtMachineState *vms)
 {
     int i;
@@ -3235,6 +3346,14 @@ static void machvirt_init(MachineState *machine)
         create_gpio_devices(vms, VIRT_SECURE_GPIO, secure_sysmem);
     }
 
+    if (vms->secure_spi) {
+        if (!vms->secure) {
+            error_report("mach-virt: secure-spi=on requires secure=on");
+            exit(1);
+        }
+        create_secure_spi(vms, secure_sysmem);
+    }
+
      /* connect powerdown request */
      vms->powerdown_notifier.notify = virt_powerdown_req;
      qemu_register_powerdown_notifier(&vms->powerdown_notifier);
@@ -3289,6 +3408,20 @@ static void virt_set_secure(Object *obj, bool value, Error **errp)
     vms->secure = value;
 }
 
+static bool virt_get_secure_spi(Object *obj, Error **errp)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    return vms->secure_spi;
+}
+
+static void virt_set_secure_spi(Object *obj, bool value, Error **errp)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    vms->secure_spi = value;
+}
+
 static bool virt_get_virt(Object *obj, Error **errp)
 {
     VirtMachineState *vms = VIRT_MACHINE(obj);
@@ -4228,6 +4361,13 @@ static void virt_machine_class_init(ObjectClass *oc, const void *data)
                                                 "Set on/off to enable/disable the ARM "
                                                 "Security Extensions (TrustZone)");
 
+    object_class_property_add_bool(oc, "secure-spi", virt_get_secure_spi,
+                                   virt_set_secure_spi);
+    object_class_property_set_description(oc, "secure-spi",
+                                          "Set on/off to add a Secure-World-only "
+                                          "PL022 SPI controller with a SPI NOR "
+                                          "flash (requires secure=on)");
+
     object_class_property_add_bool(oc, "virtualization", virt_get_virt,
                                    virt_set_virt);
     object_class_property_set_description(oc, "virtualization",
diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h
index 22e66d1a11..94cf7c44c8 100644
--- a/include/hw/arm/virt.h
+++ b/include/hw/arm/virt.h
@@ -100,6 +100,8 @@ enum {
     VIRT_GWDT_WS0,
     VIRT_GWDT_REFRESH,
     VIRT_GWDT_CONTROL,
+    VIRT_SECURE_SPI,
+    VIRT_SECURE_SPI_GPIO,
     VIRT_LOWMEMMAP_LAST,
 };
 
@@ -166,6 +168,7 @@ struct VirtMachineState {
     FWCfgState *fw_cfg;
     PFlashCFI01 *flash[2];
     bool secure;
+    bool secure_spi;
     bool highmem;
     bool highmem_compact;
     bool highmem_cxl;

---
base-commit: 3c061218fcf37a98c944ecb88cff56f94f5c0a43
change-id: 20261202-virt-secure-spi-ad89c3b7dfea

Best regards,
--  
Bhaskar Valaboju <bhaskar.valaboju@oss.qualcomm.com>