Skip to contentSkip to conversation

hw/arm: add TI AM64x SoC and am64-virt machine

RFC PATCH In review Wadim Mueller 4 participants 27 messages Aug 20 – Aug 24 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover hw/arm: add TI AM64x SoC and am64-virt machine

Add support for the TI AM64x (Sitara AM6442) SoC and a board around it, ``am64-virt``. The model is complete enough to run the stock TI boot chain end to end:

ROM boot (tiboot3.bin) -> R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux SMP

This is a resend: I posted a first RFC back in May [1] but got no feedback on it. Since then was the series rebased onto actual master, reworked for style and split into reviewable pieces, so I send it out again in the hope of getting some review.

RFC because I would like feedback on the overall approach before polishing further; see the open questions at the end.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

What is modelled
================

The SoC is heterogeneous, and the model reflects that: a Cortex-A53 cluster behind a GICv3 (GIC-500) at the real addresses, the Cortex-R5F MCU boot core, and the Cortex-M4F, each with their own RAT (region address translation) view of the memory map.

The interesting part is the DMSC. On real silicon it is a hidden core running TI's system firmware (SYSFW) which answers TI-SCI requests for clocks, power, firewalls and processor boot control from every other core. Rather than trying to execute the vendor blob, patch 10 models the DMSC as a TI-SCI endpoint sitting on top of the secure proxy model (patch 9). That is what actually starts the A53 and M4F cores when the R5 SPL asks for it, and what TF-A, OP-TEE, U-Boot and Linux talk to for clock/power management.

The remaining devices (patches 4-8) are the supporting cast: the UART, the K3 DMTimer, mailboxes, and register-level stubs for CTRL_MMR (including the DEVSTAT boot pins), GTC, DDRSS, the SDHCI PHY wrapper and the TRNG. The
stubs implement the handshakes, which firmware polls for, not the underlying hardware.

Patches 1-3 are prerequisites that touch existing code:

1-2 give the OMAP I2C model its own Kconfig symbol so it can be reused
outside the OMAP1 SoC, and implement soft reset and NACK reporting
3 fixes SDHCI ADMA chain processing; without it, transfer rate
depends from the virtual clock instead of on the guest's programming. Patch 14 contains a qtest that fails without it.

To patch 3: it is only needed as long as Bin Meng's SDHCI series [2] (on top of the SDMA boundary fix [3]) is not merged. I posted the same fix standalone in August, Bin showed then, that his series already covers the AM64x case, and I have withdrawn it there [4]. It is kept here only so that this series works on actual master - please drop patch 3 together with the pacing qtest in patch 14 as soon as [2] lands. A version of that qtest for the Icicle Kit was posted separately [5].

[2] https://patchwork.ozlabs.org/project/qemu-devel/list/?series=515264
[3] https://patchwork.ozlabs.org/project/qemu-devel/list/?series=513930
[4] https://lore.kernel.org/qemu-devel/[email protected]/
[5] https://lore.kernel.org/qemu-devel/[email protected]/

Testing
=======

Patch 14 adds:

* a unit test for the combined-boot-image parser
* qtests for the machine topology, GICv3 layout, SMP handling and the DMSC TI-SCI request/response path
* an I2C qtest (soft reset, NACK, and a read from an at24c EEPROM which the test attaches with -device)
* the SDHCI ADMA pacing reproducer
* a functional test that builds a synthetic tiboot3.bin containing a bare-metal R5 payload and checks it runs from the certified entry point. Setting QEMU_TEST_TIBOOT3 boots a real image instead.

Migration is not supported yet: none of the new devices has a VMState. I left it out for the RFC, but can add it if you want it before merge.

The series was built and tested on top of ae4f344320. Every patch builds on its own, and checkpatch reports no errors.

Open questions
==============

* pc-bios/dtb/am64-virt.dtb: patch 13 checks in a generated DTB (with its source) for convenience. I am not sure a machine-specific blob belongs in pc-bios; happy to drop it and have the functional test generate it with dtc instead, or drop the Linux-boot part of the test.

* Splitting the DMSC: hw/misc/ti-dmsc.c is large because TI-SCI is large. It could be split per message class (clocks / devices / processor boot / security) if reviewers prefer, at the cost of a non-functional intermediate state.

* Only one of the four R5F cores is modelled. mc->max_cpus reserves room for the rest. Because the SoC realizes M4F/R5F vCPUs in addition to the A53s and every vCPU needs a TCG context slot inside smp.max_cpus, the machine defaults to the full vCPU budget. I would appreciate a better idea than the actual "fail early with an explanatory message" handling when the user passes a too-small -smp.

* The register-level stubs (DDRSS in particular) fake success rather than modelling the hardware. That is deliberate, but tell me if you would rather see them as plain unimplemented-device windows plus firmware quirks elsewhere.

Wadim Mueller (14):
hw/i2c/omap_i2c: add a dedicated CONFIG_OMAP_I2C symbol hw/i2c/omap_i2c: implement soft reset and NACK reporting hw/sd/sdhci: complete non-interrupt ADMA descriptor chains in one pass hw/char: add TI AM64x UART model
hw/timer: add TI K3 DMTimer model
hw/misc: add TI K3 CTRL_MMR, GTC, DDRSS, SDHCI PHY and TRNG models hw/misc: add TI RAT (region address translation) model hw/misc: add TI mailbox (IPC) model
hw/misc: add TI K3 secure proxy model
hw/misc: add TI DMSC (TI-SCI system controller) model hw/arm: add TI K3 combined boot image parser
hw/arm: add TI AM64x SoC model
hw/arm: add the am64-virt machine
tests: add AM64x unit, qtest and functional tests

 MAINTAINERS                                   |   22 +
 docs/system/arm/am64.rst                      |   74 +
 docs/system/target-arm.rst                    |    1 +
 hw/arm/Kconfig                                |   26 +
 hw/arm/am64-virt.c                            |  347 ++++
 hw/arm/k3-bootrom-parse.c                     |  250 +++
 hw/arm/k3-bootrom.c                           |  125 ++
 hw/arm/meson.build                            |    4 +
 hw/arm/ti-am64x.c                             | 1223 ++++++++++++
 hw/arm/trace-events                           |    4 +
 hw/char/Kconfig                               |    4 +
 hw/char/meson.build                           |    1 +
 hw/char/ti-am64-uart.c                        |  109 ++
 hw/i2c/Kconfig                                |    4 +
 hw/i2c/meson.build                            |    2 +-
 hw/i2c/omap_i2c.c                             |  234 ++-
 hw/misc/Kconfig                               |   27 +
 hw/misc/meson.build                           |   10 +
 hw/misc/ti-dmsc.c                             | 1725 +++++++++++++++++
 hw/misc/ti-k3-ctrlmmr.c                       |  103 +
 hw/misc/ti-k3-ddrss.c                         |   98 +
 hw/misc/ti-k3-gtc.c                           |  100 +
 hw/misc/ti-k3-sdhci-phy.c                     |   91 +
 hw/misc/ti-k3-trng.c                          |  139 ++
 hw/misc/ti-mailbox.c                          |  566 ++++++
 hw/misc/ti-rat.c                              |  290 +++
 hw/misc/ti-sec-proxy.c                        |  446 +++++
 hw/misc/trace-events                          |   62 +
 hw/sd/sdhci-internal.h                        |    9 +
 hw/sd/sdhci.c                                 |   23 +-
 hw/timer/Kconfig                              |    3 +
 hw/timer/meson.build                          |    1 +
 hw/timer/ti-k3-dmtimer.c                      |  177 ++
 include/hw/arm/k3-bootrom.h                   |   43 +
 include/hw/arm/ti-am64x.h                     |   86 +
 include/hw/char/ti-am64-uart.h                |   28 +
 include/hw/misc/ti-dmsc.h                     |  661 +++++++
 include/hw/misc/ti-k3-ctrlmmr.h               |   26 +
 include/hw/misc/ti-k3-ddrss.h                 |   26 +
 include/hw/misc/ti-k3-gtc.h                   |   23 +
 include/hw/misc/ti-k3-sdhci-phy.h             |   26 +
 include/hw/misc/ti-k3-trng.h                  |   35 +
 include/hw/misc/ti-mailbox.h                  |   44 +
 include/hw/misc/ti-rat.h                      |   51 +
 include/hw/misc/ti-sec-proxy.h                |  108 ++
 include/hw/timer/ti-k3-dmtimer.h              |   30 +
 pc-bios/dtb/am64-virt.dtb                     |  Bin 0 -> 6116 bytes
 pc-bios/dtb/am64-virt.dts                     |  292 +++
 tests/functional/aarch64/meson.build          |    2 +
 tests/functional/aarch64/test_am64_bootrom.py |  137 ++
 tests/qtest/am64-adma-pacing-test.c           |  243 +++
 tests/qtest/am64-virt-test.c                  |  502 +++++
 tests/qtest/meson.build                       |    1 +
 tests/qtest/ti-am64-i2c-test.c                |  137 ++
 tests/unit/meson.build                        |    1 +
 tests/unit/test-k3-bootrom.c                  |  263 +++
 56 files changed, 9060 insertions(+), 5 deletions(-)
 create mode 100644 docs/system/arm/am64.rst
 create mode 100644 hw/arm/am64-virt.c
 create mode 100644 hw/arm/k3-bootrom-parse.c
 create mode 100644 hw/arm/k3-bootrom.c
 create mode 100644 hw/arm/ti-am64x.c
 create mode 100644 hw/char/ti-am64-uart.c
 create mode 100644 hw/misc/ti-dmsc.c
 create mode 100644 hw/misc/ti-k3-ctrlmmr.c
 create mode 100644 hw/misc/ti-k3-ddrss.c
 create mode 100644 hw/misc/ti-k3-gtc.c
 create mode 100644 hw/misc/ti-k3-sdhci-phy.c
 create mode 100644 hw/misc/ti-k3-trng.c
 create mode 100644 hw/misc/ti-mailbox.c
 create mode 100644 hw/misc/ti-rat.c
 create mode 100644 hw/misc/ti-sec-proxy.c
 create mode 100644 hw/timer/ti-k3-dmtimer.c
 create mode 100644 include/hw/arm/k3-bootrom.h
 create mode 100644 include/hw/arm/ti-am64x.h
 create mode 100644 include/hw/char/ti-am64-uart.h
 create mode 100644 include/hw/misc/ti-dmsc.h
 create mode 100644 include/hw/misc/ti-k3-ctrlmmr.h
 create mode 100644 include/hw/misc/ti-k3-ddrss.h
 create mode 100644 include/hw/misc/ti-k3-gtc.h
 create mode 100644 include/hw/misc/ti-k3-sdhci-phy.h
 create mode 100644 include/hw/misc/ti-k3-trng.h
 create mode 100644 include/hw/misc/ti-mailbox.h
 create mode 100644 include/hw/misc/ti-rat.h
 create mode 100644 include/hw/misc/ti-sec-proxy.h
 create mode 100644 include/hw/timer/ti-k3-dmtimer.h
 create mode 100644 pc-bios/dtb/am64-virt.dtb
 create mode 100644 pc-bios/dtb/am64-virt.dts
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c

base-commit: ae4f3443209ab154b48b706a146e5f557ab147cb

Signature · 1 line
2.43.0
#3 lore raw

RFC PATCH v2 2/14 hw/i2c/omap_i2c: implement soft reset and NACK reporting

Add the pieces of the controller that the TI K3 ROM/SPL and the Linux omap-i2c driver actually exercise:

- OMAP_I2C_SYSC.SRST triggers a soft reset and SYSS.RDONE reports its completion, instead of the register being a plain scratch value.
- A transfer to an address that nobody acknowledges raises STAT.NACK and ends the transfer, rather than being silently completed.

This allows to let a guest probe an I2C bus and correctly conclude that a device is absent.

---
 hw/i2c/omap_i2c.c | 234 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 232 insertions(+), 2 deletions(-)
hw/i2c/omap_i2c.c +232 -2
index d6e28a1a89..e56f75d6f5 100644
@@ -37,6 +37,7 @@ struct OMAPI2CState {
     I2CBus *bus;
 
     uint8_t revision;
+    uint8_t mmio_version;
     void *iclk;
     void *fclk;
 
@@ -58,6 +59,61 @@ struct OMAPI2CState {
 #define OMAP2_INTR_REV  0x34
 #define OMAP2_GC_REV    0x34
 
+/*
+ * MMIO register layout selector.  The classic OMAP1/OMAP2 "IP V1" map is the
+ * default and is what every existing OMAP board relies on.  "IP V2" is the
+ * OMAP4-and-later layout ("ti,omap4-i2c" / "ti,am64-i2c"), which relocates the
+ * registers and adds the IRQSTATUS_RAW / IRQENABLE_SET / IRQENABLE_CLR set.
+ * The transfer/reset/NACK engine is shared; only the address decode differs.
+ */
+#define OMAP_I2C_MMIO_V1  0
+#define OMAP_I2C_MMIO_V2  2
+
+/* IP V2 register offsets, as used from OMAP4 onwards. */
+#define OMAP_I2C_V2_REVNB_LO       0x00
+#define OMAP_I2C_V2_REVNB_HI       0x04
+#define OMAP_I2C_V2_SYSC           0x10
+#define OMAP_I2C_V2_IRQSTATUS_RAW  0x24
+#define OMAP_I2C_V2_IRQSTATUS      0x28
+#define OMAP_I2C_V2_IRQENABLE_SET  0x2c
+#define OMAP_I2C_V2_IRQENABLE_CLR  0x30
+#define OMAP_I2C_V2_WE             0x34
+#define OMAP_I2C_V2_SYSS           0x90
+#define OMAP_I2C_V2_BUF            0x94
+#define OMAP_I2C_V2_CNT            0x98
+#define OMAP_I2C_V2_DATA           0x9c
+#define OMAP_I2C_V2_CON            0xa4
+#define OMAP_I2C_V2_OA             0xa8
+#define OMAP_I2C_V2_SA             0xac
+#define OMAP_I2C_V2_PSC            0xb0
+#define OMAP_I2C_V2_SCLL           0xb4
+#define OMAP_I2C_V2_SCLH           0xb8
+#define OMAP_I2C_V2_SYSTEST        0xbc
+#define OMAP_I2C_V2_BUFSTAT        0xc0
+
+/*
+ * Translate an IP-V2 offset for a register whose semantics are identical to
+ * the V1 model into the V1 offset the shared read/write switch decodes.
+ * Returns -1 for offsets that have no direct V1 equivalent (those are handled
+ * inline by the V2 front-end).
+ */
+static int omap_i2c_v2_to_v1(int offset)
+{
+    switch (offset) {
+    case OMAP_I2C_V2_BUF:     return 0x14;
+    case OMAP_I2C_V2_CNT:     return 0x18;
+    /* DATA (0x9c) is handled inline byte-wise by the V2 front-end. */
+    case OMAP_I2C_V2_CON:     return 0x24;
+    case OMAP_I2C_V2_OA:      return 0x28;
+    case OMAP_I2C_V2_SA:      return 0x2c;
+    case OMAP_I2C_V2_PSC:     return 0x30;
+    case OMAP_I2C_V2_SCLL:    return 0x34;
+    case OMAP_I2C_V2_SCLH:    return 0x38;
+    case OMAP_I2C_V2_SYSTEST: return 0x3c;
+    default:                  return -1;
+    }
+}
+
 static void omap_i2c_interrupts_update(OMAPI2CState *s)
 {
     qemu_set_irq(s->irq, s->stat & s->mask);
@@ -162,6 +218,83 @@ static uint32_t omap_i2c_read(void *opaque, hwaddr addr)
     int offset = addr & OMAP_MPUI_REG_MASK;
     uint16_t ret;
 
+    if (s->mmio_version == OMAP_I2C_MMIO_V2) {
+        switch (offset) {
+        case OMAP_I2C_V2_REVNB_LO:
+            return s->revision;
+        case OMAP_I2C_V2_REVNB_HI:
+            return 0;
+        case OMAP_I2C_V2_SYSC:
+            return 0;
+        case OMAP_I2C_V2_IRQSTATUS_RAW:
+        case OMAP_I2C_V2_IRQSTATUS:     /* STAT mirrors IRQSTATUS_RAW */
+            return s->stat | (i2c_bus_busy(s->bus) << 12);
+        case OMAP_I2C_V2_IRQENABLE_SET:
+        case OMAP_I2C_V2_IRQENABLE_CLR:
+            return s->mask;
+        case OMAP_I2C_V2_WE:
+            return 0;
+        case OMAP_I2C_V2_SYSS:
+            /* reset is instantaneous in the model: RDONE always reads set */
+            return 1;
+        case OMAP_I2C_V2_BUFSTAT:
+            return 0;
+        case OMAP_I2C_V2_DATA: {
+            /*
+             * The OMAP4/AM64x driver accesses the DATA register one byte per
+             * MMIO access (readw of a single byte), unlike the classic V1
+             * 16-bit FIFO convention.  Pop exactly one byte (oldest first,
+             * FIFO is filled LSB-first by omap_i2c_fifo_run()).
+             */
+            uint8_t b = s->fifo & 0xff;
+            if (s->rxlen > 0) {
+                s->fifo >>= 8;
+                s->rxlen--;
+            }
+            /*
+             * Refill from the slave while the transfer is still live (this
+             * may complete count_cur and issue the STOP, leaving the last
+             * few prefetched bytes buffered in the FIFO with the bus idle).
+             */
+            omap_i2c_fifo_run(s);
+            /*
+             * Drive the RRDY/ARDY handshake directly off the FIFO drain
+             * state so it keeps working after the bus has gone idle: RRDY
+             * stays asserted while buffered bytes remain, and ARDY is raised
+             * once the last byte has been consumed (master-receive).
+             */
+            if (s->rxlen > 0) {
+                s->stat |= 1 << 3;                          /* RRDY */
+            } else {
+                s->stat &= ~(1 << 3);                       /* RRDY */
+                if (((s->control >> 10) & 1) &&             /* MST */
+                    ((~s->control >> 9) & 1)) {             /* TRX (receive) */
+                    s->stat |= 1 << 2;                      /* ARDY */
+                    s->control &= ~(1 << 10);               /* MST */
+                    /*
+                     * DCOUNT decrements to 0 on real hardware and stays
+                     * there; leave it at 0 so a following address-only probe
+                     * (which programs no CNT) starts from 0 and completes
+                     * with ARDY instead of waiting for phantom TX bytes.
+                     */
+                    s->count = 0;
+                    s->count_cur = 0;
+                }
+            }
+            s->stat &= ~(1 << 11);                          /* ROVR */
+            omap_i2c_interrupts_update(s);
+            return b;
+        }
+        default:
+            offset = omap_i2c_v2_to_v1(offset);
+            if (offset < 0) {
+                OMAP_BAD_REG(addr);
+                return 0;
+            }
+            break;
+        }
+    }
+
     switch (offset) {
     case 0x00:  /* I2C_REV */
         return s->revision;                     /* REV */
@@ -266,6 +399,83 @@ static void omap_i2c_write(void *opaque, hwaddr addr,
     int offset = addr & OMAP_MPUI_REG_MASK;
     int nack;
 
+    if (s->mmio_version == OMAP_I2C_MMIO_V2) {
+        switch (offset) {
+        case OMAP_I2C_V2_SYSC:
+            if (value & 2) {                    /* SRST */
+                omap_i2c_reset(DEVICE(s));
+            }
+            return;
+        case OMAP_I2C_V2_DATA:
+            /*
+             * The OMAP4/AM64x driver writes the DATA register one byte per
+             * MMIO access (writew of a single byte).  Push exactly one byte,
+             * mirroring the classic 8-bit FIFO path (omap_i2c_writeb()).
+             */
+            if (s->txlen <= 2) {
+                s->fifo <<= 8;
+                s->txlen += 1;
+                s->fifo |= value & 0xff;
+                s->stat &= ~(1 << 10);                  /* XUDF */
+                if (s->txlen > 2) {
+                    s->stat &= ~(1 << 4);               /* XRDY */
+                }
+                omap_i2c_fifo_run(s);
+                /*
+                 * If the transmit finished and issued its STOP, leave DCOUNT
+                 * at 0 (see the DATA read path) so the next probe/transfer
+                 * that does not reprogram CNT is not tricked into expecting
+                 * stale phantom bytes.
+                 */
+                if (!i2c_bus_busy(s->bus)) {
+                    s->count = 0;
+                    s->count_cur = 0;
+                }
+                omap_i2c_interrupts_update(s);
+            }
+            return;
+        case OMAP_I2C_V2_IRQSTATUS_RAW:
+        case OMAP_I2C_V2_IRQSTATUS:             /* write-1-to-clear */
+            s->stat &= ~(value & 0x7fff);
+            /*
+             * XRDY/RRDY are level events: after the driver clears them it
+             * expects them to re-assert while the transfer still has room /
+             * data.  Re-run the FIFO engine for a live transfer, then
+             * re-assert RRDY if bytes remain buffered even after the bus has
+             * gone idle (the tail of a receive drains from the FIFO).
+             */
+            omap_i2c_fifo_run(s);
+            if (s->rxlen > 0) {
+                s->stat |= 1 << 3;              /* RRDY */
+            }
+            omap_i2c_interrupts_update(s);
+            return;
+        case OMAP_I2C_V2_IRQENABLE_SET:
+            s->mask |= value & 0xff;
+            omap_i2c_interrupts_update(s);
+            return;
+        case OMAP_I2C_V2_IRQENABLE_CLR:
+            s->mask &= ~(value & 0xff);
+            omap_i2c_interrupts_update(s);
+            return;
+        case OMAP_I2C_V2_WE:
+            return;                             /* wakeup enable: ignored */
+        case OMAP_I2C_V2_REVNB_LO:
+        case OMAP_I2C_V2_REVNB_HI:
+        case OMAP_I2C_V2_SYSS:
+        case OMAP_I2C_V2_BUFSTAT:
+            OMAP_RO_REG(addr);
+            return;
+        default:
+            offset = omap_i2c_v2_to_v1(offset);
+            if (offset < 0) {
+                OMAP_BAD_REG(addr);
+                return;
+            }
+            break;
+        }
+    }
+
     switch (offset) {
     case 0x00:  /* I2C_REV */
     case 0x0c:  /* I2C_IV */
@@ -412,6 +622,10 @@ static void omap_i2c_writeb(void *opaque, hwaddr addr,
     OMAPI2CState *s = opaque;
     int offset = addr & OMAP_MPUI_REG_MASK;
 
+    if (s->mmio_version == OMAP_I2C_MMIO_V2 && offset == OMAP_I2C_V2_DATA) {
+        offset = 0x1c;                          /* I2C_DATA */
+    }
+
     switch (offset) {
     case 0x1c:  /* I2C_DATA */
         if (s->txlen > 2) {
@@ -489,10 +703,24 @@ static void omap_i2c_init(Object *obj)
 static void omap_i2c_realize(DeviceState *dev, Error **errp)
 {
     OMAPI2CState *s = OMAP_I2C(dev);
+    uint64_t size;
 
+    if (s->mmio_version == OMAP_I2C_MMIO_V2) {
+        size = 0x100;                           /* AM64x main_i2c reg length */
+    } else {
+        size = (s->revision < OMAP2_INTR_REV) ? 0x800 : 0x1000;
+    }
     memory_region_init_io(&s->iomem, OBJECT(dev), &omap_i2c_ops, s, "omap.i2c",
-                          (s->revision < OMAP2_INTR_REV) ? 0x800 : 0x1000);
-
+                          size);
+
+    /*
+     * The IP-V2 wiring (e.g. TI AM64x) drives the module from the SoC clock
+     * tree rather than the legacy omap_clk pointer stubs, so the fclk/iclk
+     * requirement only applies to the classic OMAP boards.
+     */
+    if (s->mmio_version == OMAP_I2C_MMIO_V2) {
+        return;
+    }
     if (!s->fclk) {
         error_setg(errp, "omap_i2c: fclk not connected");
         return;
@@ -516,6 +744,8 @@ void omap_i2c_set_fclk(OMAPI2CState *i2c, omap_clk clk)
 
 static const Property omap_i2c_properties[] = {
     DEFINE_PROP_UINT8("revision", OMAPI2CState, revision, 0),
+    DEFINE_PROP_UINT8("mmio-version", OMAPI2CState, mmio_version,
+                      OMAP_I2C_MMIO_V1),
 };
 
 static void omap_i2c_class_init(ObjectClass *klass, const void *data)
Signature · 1 line
2.43.0
#7 lore raw

RFC PATCH v2 5/14 hw/timer: add TI K3 DMTimer model

In reply to Wadim Mueller

Add a model of the K3 DMTimer used, among others, by the R5 SPL for its udelay() implementation. It is a free-running counter with load/reload and overflow interrupt.

---
 hw/timer/Kconfig                 |   3 +
 hw/timer/meson.build             |   1 +
 hw/timer/ti-k3-dmtimer.c         | 177 +++++++++++++++++++++++++++++++
 include/hw/timer/ti-k3-dmtimer.h |  30 ++++++
 4 files changed, 211 insertions(+)
 create mode 100644 hw/timer/ti-k3-dmtimer.c
 create mode 100644 include/hw/timer/ti-k3-dmtimer.h
hw/timer/Kconfig +3 -0
index e1b751a54a..fac5ba5a7e 100644
@@ -68,3 +68,6 @@ config AVR_TIMER16
 
 config HEX_QTIMER
     bool
+
+config TI_K3_DMTIMER
+    bool
hw/timer/meson.build +1 -0
index 8323efaf46..4cf49ef6d6 100644
@@ -32,6 +32,7 @@ system_ss.add(when: 'CONFIG_STM32F2XX_TIMER', if_true: files('stm32f2xx_timer.c'
 system_ss.add(when: 'CONFIG_XILINX', if_true: files('xilinx_timer.c'))
 specific_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_timer.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_PWM', if_true: files('sifive_pwm.c'))
+system_ss.add(when: 'CONFIG_TI_K3_DMTIMER', if_true: files('ti-k3-dmtimer.c'))
 
 system_ss.add(when: 'CONFIG_AVR_TIMER16', if_true: files('avr_timer16.c'))
 
hw/timer/ti-k3-dmtimer.c new file +177 -0

177 lines not on this page · raw message

include/hw/timer/ti-k3-dmtimer.h new file +30 -0
new file mode 100644
index 0000000000..04c3925e2d
@@ -0,0 +1,30 @@
+/*
+ * TI K3 DM timer (am654 layout)
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_TIMER_TI_K3_DMTIMER_H
+#define HW_TIMER_TI_K3_DMTIMER_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_TI_K3_DMTIMER "ti-k3-dmtimer"
+OBJECT_DECLARE_SIMPLE_TYPE(TIK3DmTimerState, TI_K3_DMTIMER)
+
+struct TIK3DmTimerState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+    uint32_t freq_hz;
+    uint32_t tclr;
+    uint32_t tldr;
+    /* counter value latched at last write/start */
+    uint32_t tcrr_base;
+    int64_t base_ns;
+    bool running;
+};
+
+#endif
Signature · 1 line
2.43.0
#16 lore raw

RFC PATCH v2 14/14 tests: add AM64x unit, qtest and functional tests

In reply to Wadim Mueller

Cover the new code:

- tests/unit/test-k3-bootrom: combined boot image parser, including malformed input.
- tests/qtest/am64-virt-test: machine topology, GICv3 redistributor layout, SMP handling, DMSC TI-SCI request/response handling.
- tests/qtest/ti-am64-i2c-test: I2C soft reset, NACK on an absent address and a transfer against the on-SoM EEPROM.
- tests/qtest/am64-adma-pacing-test: reproducer for the SDHCI ADMA pacing fix earlier in this series; without the fix it fails.
- tests/functional/aarch64/test_am64_bootrom: builds a synthetic combined boot image with a bare-metal R5 payload and checks that the ROM boot path runs it. If QEMU_TEST_TIBOOT3 points at a real tiboot3.bin, that is booted as well.

---
 tests/functional/aarch64/meson.build          |   2 +
 tests/functional/aarch64/test_am64_bootrom.py | 137 +++++
 tests/qtest/am64-adma-pacing-test.c           | 243 +++++++++
 tests/qtest/am64-virt-test.c                  | 502 ++++++++++++++++++
 tests/qtest/meson.build                       |   1 +
 tests/qtest/ti-am64-i2c-test.c                | 137 +++++
 tests/unit/meson.build                        |   1 +
 tests/unit/test-k3-bootrom.c                  | 263 +++++++++
 8 files changed, 1286 insertions(+)
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c
tests/functional/aarch64/meson.build +2 -0
index e81afd6c39..6962ae1da1 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 test_aarch64_timeouts = {
+  'am64_bootrom' : 300,
   'aspeed_ast2700a1' : 600,
   'aspeed_ast2700a2' : 600,
   'aspeed_ast2700fc' : 600,
@@ -26,6 +27,7 @@ tests_aarch64_system_quick = [
 ]
 
 tests_aarch64_system_thorough = [
+  'am64_bootrom',
   'aspeed_ast2700a1',
   'aspeed_ast2700a2',
   'aspeed_ast2700fc',
tests/functional/aarch64/test_am64_bootrom.py new file +137 -0

137 lines not on this page · raw message

tests/qtest/am64-adma-pacing-test.c new file +243 -0

243 lines not on this page · raw message

tests/qtest/am64-virt-test.c new file +502 -0

502 lines not on this page · raw message

tests/qtest/meson.build +1 -0
index f7c7d06620..d915fb4199 100644
@@ -274,6 +274,7 @@ qtests_aarch64 = \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_ARM_VIRT') ?
    ['iommu-smmuv3-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_AM64_VIRT') ? ['am64-virt-test', 'ti-am64-i2c-test', 'am64-adma-pacing-test'] : []) + \
   qtests_cxl +                                                                                  \
   ['arm-cpu-features',
    'numa-test',
tests/qtest/ti-am64-i2c-test.c new file +137 -0

137 lines not on this page · raw message

tests/unit/meson.build +1 -0
index dc3fb954c0..be2aab4274 100644
@@ -41,6 +41,7 @@ tests = {
   'test-bitops': [],
   'test-bitcnt': [],
   'test-qgraph': ['../qtest/libqos/qgraph.c'],
+  'test-k3-bootrom': ['../../hw/arm/k3-bootrom-parse.c'],
   'check-qom-interface': [qom],
   'check-qom-proplist': [qom],
   'test-qemu-opts': [],
tests/unit/test-k3-bootrom.c new file +263 -0

263 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

RFC PATCH v2 4/14 hw/char: add TI AM64x UART model

In reply to Wadim Mueller

The AM64x UART is an 8250-compatible core with a TI-specific mode-select register (MDR1) that has to be taken out of "disabled" state before the UART transmits, plus a power/emulation management register. Wrap the generic 16550 model to provide these.

---
 hw/char/Kconfig                |   4 ++
 hw/char/meson.build            |   1 +
 hw/char/ti-am64-uart.c         | 109 +++++++++++++++++++++++++++++++++
 include/hw/char/ti-am64-uart.h |  28 +++++++++
 4 files changed, 142 insertions(+)
 create mode 100644 hw/char/ti-am64-uart.c
 create mode 100644 include/hw/char/ti-am64-uart.h
hw/char/Kconfig +4 -0
index 020c0a84bb..c06811130a 100644
@@ -31,6 +31,10 @@ config SERIAL_MM
     bool
     select SERIAL
 
+config AM64_UART
+    bool
+    select SERIAL
+
 config SERIAL_PCI
     bool
     default y if PCI_DEVICES
hw/char/meson.build +1 -0
index fc3d7ee506..71c35af357 100644
@@ -22,6 +22,7 @@ system_ss.add(when: 'CONFIG_VIRTIO_SERIAL', if_true: files('virtio-console.c'))
 system_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen_console.c'))
 system_ss.add(when: 'CONFIG_XILINX', if_true: files('xilinx_uartlite.c'))
 system_ss.add(when: 'CONFIG_DIVA_GSP', if_true: files('diva-gsp.c'))
+system_ss.add(when: 'CONFIG_AM64_UART', if_true: files('ti-am64-uart.c'))
 
 system_ss.add(when: 'CONFIG_AVR_USART', if_true: files('avr_usart.c'))
 system_ss.add(when: 'CONFIG_COLDFIRE', if_true: files('mcf_uart.c'))
hw/char/ti-am64-uart.c new file +109 -0

109 lines not on this page · raw message

include/hw/char/ti-am64-uart.h new file +28 -0
new file mode 100644
index 0000000000..5cf820dc20
@@ -0,0 +1,28 @@
+/*
+ * TI AM64x UART emulation
+ *
+ * Copyright (c) 2025 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_SERIAL_TI_AM64_H
+#define HW_SERIAL_TI_AM64_H
+
+#include "hw/char/serial.h"
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_AM64_UART "ti-am64-uart"
+OBJECT_DECLARE_SIMPLE_TYPE(AM64Uart, AM64_UART)
+
+struct AM64Uart {
+    SysBusDevice parent;
+
+    SerialState serial;
+
+    uint8_t regshift;
+};
+
+#endif
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH v2 1/14 hw/i2c/omap_i2c: add a dedicated CONFIG_OMAP_I2C symbol

In reply to Wadim Mueller

The OMAP I2C controller model was only buildable as part of the full OMAP1 SoC (CONFIG_OMAP). The same IP is used by the TI K3 family (AM64x and friends), which shares nothing else with OMAP1.

Give the device its own Kconfig symbol and select it from CONFIG_OMAP, so that other SoCs can reuse the model without pulling in OMAP1.

No functional change.

---
 hw/arm/Kconfig     | 1 +
 hw/i2c/Kconfig     | 4 ++++
 hw/i2c/meson.build | 2 +-
 3 files changed, 6 insertions(+), 1 deletion(-)
hw/arm/Kconfig +1 -0
index 260d2f0751..b2dc2714c0 100644
@@ -137,6 +137,7 @@ config OMAP
     bool
     select FRAMEBUFFER
     select I2C
+    select OMAP_I2C
     select PFLASH_CFI01
     select SD
     select SERIAL_MM
hw/i2c/Kconfig +4 -0
index 0766130b59..e7c0e2f8bf 100644
@@ -54,3 +54,7 @@ config PMBUS
 config BCM2835_I2C
     bool
     select I2C
+
+config OMAP_I2C
+    bool
+    select I2C
hw/i2c/meson.build +1 -1
index 88aea35662..760724e7bb 100644
@@ -14,7 +14,7 @@ i2c_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_smbus.c'))
 i2c_ss.add(when: 'CONFIG_DESIGNWARE_I2C', if_true: files('designware_i2c.c'))
 i2c_ss.add(when: 'CONFIG_SMBUS_EEPROM', if_true: files('smbus_eeprom.c'))
 i2c_ss.add(when: 'CONFIG_ARM_SBCON_I2C', if_true: files('arm_sbcon_i2c.c'))
-i2c_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_i2c.c'))
+i2c_ss.add(when: 'CONFIG_OMAP_I2C', if_true: files('omap_i2c.c'))
 i2c_ss.add(when: 'CONFIG_PPC4XX', if_true: files('ppc4xx_i2c.c'))
 i2c_ss.add(when: 'CONFIG_PCA954X', if_true: files('i2c_mux_pca954x.c'))
 i2c_ss.add(when: 'CONFIG_PMBUS', if_true: files('pmbus_device.c'))
Signature · 1 line
2.43.0
#4 lore raw

RFC PATCH v2 3/14 hw/sd/sdhci: complete non-interrupt ADMA descriptor chains in one pass

In reply to Wadim Mueller

sdhci_do_adma() returns to the main loop after every descriptor and relies on a timer re-entry to pick up the next one. For a descriptor chain whose entries do not request an interrupt this makes the transfer rate depend from the virtual clock rather than on the guest's programming, which significantly slows down large transfers and makes guest-visible timing depend on host timer behaviour.

Keep processing the chain in the same invocation while no descriptor asks for an interrupt and the transfer has not finished, and only fall back to the deferred path when the guest actually requested a notification.

A qtest reproducer is added later in this series.

---
This patch can be dropped once Bin Meng's SDHCI series
https://patchwork.ozlabs.org/project/qemu-devel/list/?series=515264
(which needs series=513930 applied first) is merged - it covers the same AM64x failure, see
https://lore.kernel.org/qemu-devel/[email protected]/
It is included here only so that the series works on actual master.

 hw/sd/sdhci-internal.h |  9 +++++++++
 hw/sd/sdhci.c          | 23 +++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)
hw/sd/sdhci-internal.h +9 -0
index 4aeed120bf..e6ce12617e 100644
@@ -277,6 +277,15 @@ FIELD(SDHC_MAXCURR, V18_VDD2,         32, 8); /* since v4.20 */
 #define SDHC_INSERTION_DELAY            (NANOSECONDS_PER_SECOND)
 #define SDHC_TRANSFER_DELAY             100
 #define SDHC_ADMA_DESCS_PER_DELAY       5
+/*
+ * Upper bound on ADMA2 descriptors handled in a single sdhci_do_adma()
+ * call, as a safety valve against a malformed or circular descriptor
+ * list. A well-formed transfer terminates far below this via END or
+ * blkcnt == 0 (even a 4 GiB transfer built from 64 KiB TRAN descriptors
+ * is only ~64K descriptors); the bound merely guarantees the loop makes
+ * a decision instead of spinning forever.
+ */
+#define SDHC_ADMA_MAX_DESCRIPTORS       (1 << 20)
 #define SDHC_CMD_RESPONSE               (3 << 0)
 
 enum {
hw/sd/sdhci.c +21 -2
index e58a610397..9a5dd1d93f 100644
@@ -780,7 +780,6 @@ static void sdhci_do_adma(SDHCIState *s)
     const MemTxAttrs attrs = { .memory = true };
     ADMADescr dscr = {};
     MemTxResult res = MEMTX_ERROR;
-    int i;
 
     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN && !s->blkcnt) {
         /* Stop Multiple Transfer */
@@ -788,7 +787,27 @@ static void sdhci_do_adma(SDHCIState *s)
         return;
     }
 
-    for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
+    /*
+     * Process the descriptor chain to completion (END or blkcnt == 0),
+     * yielding to the guest only for a descriptor carrying the INT
+     * attribute (a DMA-boundary interrupt, handled at the end of the loop).
+     *
+     * Historically at most SDHC_ADMA_DESCS_PER_DELAY descriptors were
+     * handled per call before rescheduling SDHC_TRANSFER_DELAY ns later on
+     * QEMU_CLOCK_VIRTUAL. That pacing is only needed so a guest can observe
+     * the intermediate DMA-interrupt state; a bulk transfer that requests
+     * no interrupt does not need slicing, and throttling it across many
+     * virtual-clock round-trips can make it race a guest-side transfer
+     * timeout. Run such chains to completion in one call instead.
+     *
+     * SDHC_ADMA_MAX_DESCRIPTORS bounds the loop so a malformed or circular
+     * chain cannot spin here forever; on overflow, break to the reschedule
+     * path so the main loop stays responsive.
+     */
+    for (unsigned int adma_descs = 0; ; adma_descs++) {
+        if (adma_descs >= SDHC_ADMA_MAX_DESCRIPTORS) {
+            break;
+        }
         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
 
         get_adma_description(s, &dscr);
Signature · 1 line
2.43.0
#10 lore raw

RFC PATCH v2 8/14 hw/misc: add TI mailbox (IPC) model

In reply to Wadim Mueller

Add the K3 mailbox IP: per-mailbox message FIFOs and per-user interrupt status/enable registers, used for inter-core IPC, for example between the A53 cluster and the M4F.

---
 hw/misc/Kconfig              |   3 +
 hw/misc/meson.build          |   1 +
 hw/misc/ti-mailbox.c         | 566 +++++++++++++++++++++++++++++++++++
 hw/misc/trace-events         |  13 +
 include/hw/misc/ti-mailbox.h |  44 +++
 5 files changed, 627 insertions(+)
 create mode 100644 hw/misc/ti-mailbox.c
 create mode 100644 include/hw/misc/ti-mailbox.h
hw/misc/Kconfig +3 -0
index c176d6d6c7..e80fdc3214 100644
@@ -161,6 +161,9 @@ config TI_K3_SDHCI_PHY
 config TI_K3_TRNG
     bool
 
+config TI_MAILBOX
+    bool
+
 config TZ_MPC
     bool
 
hw/misc/meson.build +1 -0
index 952b7b7c2f..d190d8608a 100644
@@ -129,6 +129,7 @@ system_ss.add(when: 'CONFIG_TI_K3_GTC', if_true: files('ti-k3-gtc.c'))
 system_ss.add(when: 'CONFIG_TI_K3_DDRSS', if_true: files('ti-k3-ddrss.c'))
 system_ss.add(when: 'CONFIG_TI_K3_SDHCI_PHY', if_true: files('ti-k3-sdhci-phy.c'))
 system_ss.add(when: 'CONFIG_TI_K3_TRNG', if_true: files('ti-k3-trng.c'))
+system_ss.add(when: 'CONFIG_TI_MAILBOX', if_true: files('ti-mailbox.c'))
 system_ss.add(when: 'CONFIG_TZ_MPC', if_true: files('tz-mpc.c'))
 system_ss.add(when: 'CONFIG_TZ_MSC', if_true: files('tz-msc.c'))
 system_ss.add(when: 'CONFIG_TZ_PPC', if_true: files('tz-ppc.c'))
hw/misc/ti-mailbox.c new file +566 -0

566 lines not on this page · raw message

hw/misc/trace-events +13 -0
index fae1f90ee2..ea7cde021e 100644
@@ -69,6 +69,19 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "Read diagnostic %"PRId64"= 0x%0
 # empty_slot.c
 empty_slot_write(uint64_t addr, unsigned width, uint64_t value, unsigned size, const char *name) "wr addr:0x%04"PRIx64" data:0x%0*"PRIx64" size %u [%s]"
 
+# ti-mailbox.c
+ti_mailbox_read(uint64_t offset, uint32_t value, unsigned size, const char *reg, uint8_t mailbox_id, const char *cpu_str, const char *mbox_str, const char *user_str, const char *irq_bits_str) "off=0x%" PRIx64 " value=0x%08x size=%u reg=%s mailbox_id=%u%s%s%s%s"
+ti_mailbox_write(uint64_t offset, uint32_t value, unsigned size, const char *reg, uint8_t mailbox_id, const char *cpu_str, const char *mbox_str, const char *user_str, const char *irq_bits_str) "off=0x%" PRIx64 " value=0x%08x size=%u reg=%s mailbox_id=%u%s%s%s%s"
+ti_mailbox_irq(uint8_t mailbox_id, uint8_t user, int level, uint32_t raw, uint32_t enable, uint32_t masked) "mailbox_id=%u user=%u level=%d raw=0x%08x enable=0x%08x masked=0x%08x"
+ti_mailbox_irq_eval(uint8_t mailbox_id, uint8_t user, uint32_t hw_raw, uint32_t raw, uint32_t enable, uint32_t masked) "mailbox_id=%u user=%u hw_raw=0x%08x raw=0x%08x enable=0x%08x masked=0x%08x"
+ti_mailbox_fifo_push(uint8_t mailbox_id, int mbox, uint8_t depth, uint32_t used_before, uint32_t val, int ok) "mailbox_id=%u mbox=%d depth=%u used_before=%u val=0x%08x ok=%d"
+ti_mailbox_fifo_pop(uint8_t mailbox_id, int mbox, uint8_t depth, uint32_t used_before, uint32_t val, int ok) "mailbox_id=%u mbox=%d depth=%u used_before=%u val=0x%08x ok=%d"
+ti_mailbox_reset_state(uint8_t mailbox_id) "mailbox_id=%u reset"
+ti_mailbox_raw_set(uint8_t mailbox_id, uint8_t user, uint32_t mask, uint32_t raw_set) "mailbox_id=%u user=%u mask=0x%08x raw_set=0x%08x"
+ti_mailbox_raw_clear(uint8_t mailbox_id, uint8_t user, uint32_t mask, uint32_t raw_set, uint32_t raw_clear_mask) "mailbox_id=%u user=%u mask=0x%08x raw_set=0x%08x raw_clear_mask=0x%08x"
+ti_mailbox_irq_enable(uint8_t mailbox_id, uint8_t user, int set, uint32_t mask, uint32_t irq_enable) "mailbox_id=%u user=%u set=%d mask=0x%08x irq_enable=0x%08x"
+ti_mailbox_msg_write_pc(uint8_t mailbox_id, int mbox, uint64_t pc, const char *cpu_str) "mailbox_id=%u mbox=%d pc=0x%" PRIx64 "%s"
+
 # slavio_misc.c
 slavio_misc_update_irq_raise(void) "Raise IRQ"
 slavio_misc_update_irq_lower(void) "Lower IRQ"
include/hw/misc/ti-mailbox.h new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.43.0
#12 lore raw

RFC PATCH v2 10/14 hw/misc: add TI DMSC (TI-SCI system controller) model

In reply to Wadim Mueller

On real K3 silicon the device management and security controller runs the TI system firmware (SYSFW) and answers TI-SCI requests from all other cores. Rather than executing the vendor firmware blob, model the DMSC as a TI-SCI endpoint on top of the secure proxy.

Implemented message classes:

- version and firmware capability queries
- device state, device reset and clock management (set/get state, get/set frequency, query frequency, clock parents)
- processor boot control: request/release, set config, set control, boot vector capture, wait_proc_boot_status and handover, which is what actually starts the A53 and M4F cores
- firewall configuration and SA2UL/secure-revision queries needed by TF-A and OP-TEE
- system reset

Unknown messages are NAKed and traced, which keeps guests that probe for optional features working.

---
 hw/misc/Kconfig           |    3 +
 hw/misc/meson.build       |    1 +
 hw/misc/ti-dmsc.c         | 1725 +++++++++++++++++++++++++++++++++++++
 hw/misc/trace-events      |   32 +
 include/hw/misc/ti-dmsc.h |  661 ++++++++++++++
 5 files changed, 2422 insertions(+)
 create mode 100644 hw/misc/ti-dmsc.c
 create mode 100644 include/hw/misc/ti-dmsc.h
hw/misc/Kconfig +3 -0
index bea8e9341f..ebdbda1962 100644
@@ -143,6 +143,9 @@ config MPS2_SCC
     bool
     select LED
 
+config TI_DMSC
+    bool
+
 config TI_RAT
     bool
 
hw/misc/meson.build +1 -0
index a8a40ea2a7..124d55502d 100644
@@ -123,6 +123,7 @@ system_ss.add(when: 'CONFIG_STM32L4X5_RCC', if_true: files('stm32l4x5_rcc.c'))
 system_ss.add(when: 'CONFIG_MPS2_FPGAIO', if_true: files('mps2-fpgaio.c'))
 system_ss.add(when: 'CONFIG_MPS2_SCC', if_true: files('mps2-scc.c'))
 
+system_ss.add(when: 'CONFIG_TI_DMSC', if_true: files('ti-dmsc.c'))
 system_ss.add(when: 'CONFIG_TI_RAT', if_true: files('ti-rat.c'))
 system_ss.add(when: 'CONFIG_TI_K3_CTRLMMR', if_true: files('ti-k3-ctrlmmr.c'))
 system_ss.add(when: 'CONFIG_TI_K3_GTC', if_true: files('ti-k3-gtc.c'))
hw/misc/ti-dmsc.c new file +1725 -0

1,725 lines not on this page · raw message

hw/misc/trace-events +32 -0
index dfa2d04117..d6fac538b4 100644
@@ -456,6 +456,38 @@ iommu_testdev_dma_verify(uint32_t expected, uint32_t actual) "expected=0x%x actu
 iommu_testdev_dma_result(uint32_t result) "DMA completed result=0x%x"
 iommu_testdev_dma_armed(bool armed) "armed=%d"
 
+# ti-dmsc.c
+dmsc_handle_set_clock(const char *type, const char *host_name, const char *device_name, int clk) "%s received from host %s, device %s, clk: %i"
+dmsc_handle_get_device(const char *type, const char *host_name, const char *device_name, uint8_t prog_state, uint8_t current_state) "%s received from host %s, device %s, response: prog_state: %i, current_state: %i"
+dmsc_handle_get_clock(const char *type, const char *host_name, const char *device_name, uint8_t clk, uint32_t clk32) "%s received from host %s, device %s, clk %u, clk32 %u"
+dmsc_handle_get_clock_parents(const char *type, const char *host_name, const char *device_name, uint8_t clk, uint32_t clk32) "%s received from host %s, device %s, clk %u, clk32 %u"
+dmsc_handle_set_clock_parent(const char *type, const char *host_name, const char *device_name, uint8_t clk_id, uint8_t parent_id) "%s received from host %s, device %s, clk_id %u, parent_id %u"
+dmsc_handle_query_freq(const char *type, const char *host_name, const char *device_name, uint8_t clk, uint32_t clk32, uint64_t target_freq) "%s received from host %s, device %s, clk %u, clk32 %u, target_frequency %"PRIu64
+dmsc_handle_get_freq(const char *type, const char *host_name, const char *device_name, uint8_t clk) "%s received from host %s, device %s, clk %u"
+dmsc_handle_proc_set_config(const char *type, const char *host_name) "%s received from host %s"
+dmsc_handle_set_device_state(const char *type, const char *host_name, const char *device_name, const char *state) "%s received from host %s, device %s, state %s"
+dmsc_handle_set_device_resets(const char *type, const char *host_name, const char *device_name, uint32_t resets) "%s received from host %s, device %s, resets 0x%08x"
+dmsc_handle_get_status(const char *type, const char *host_name, const char *proc_name, uint8_t proc_id) "%s received from host %s, proc %s (%u)"
+dmsc_handle_wait_proc_boot_status(const char *type, const char *host_name, const char *proc_name, uint8_t proc_id) "%s received from host %s, proc %s (%u)"
+dmsc_get_status_resp(const char *proc_name, uint8_t proc_id, uint32_t status_flags, int m4_running) "GET_STATUS response for %s (%u): status_flags=0x%08x, m4_running=%d"
+dmsc_unsupported_message(const char *msg, int type, const char *host, int thread_id) "No handler for message type=%s (%i) from host %s ... (thread id: %i) dropping"
+dmsc_new_message_received(int type, const char *msg, const char *host, int thread_id) "Handling message type=0x%04x (%s) from host=%s, thread=%u"
+dmsc_get_version(const char* msg, const char *host, const char *description) "Get Version received %s from host %s. Description returned %s"
+dmsc_get_fw_caps(const char* msg, const char *host) "Get FW Caps Request received %s from host %s"
+dmsc_start_proc(const char* proc_name, int proc_id, const char *host) "Requested to start Processor %s (%i) from host %s"
+dmsc_stop_proc(const char* proc_name, int proc_id, const char *host) "Requested to stop Processor %s (%i) from host %s"
+dmsc_handover_proc(const char* proc_name, int proc_id, const char *new_host, const char *host) "Requested to hand over Processor %s (%i) to host %s, from host %s"
+dmsc_a53_bootvector(uint8_t proc_id, uint64_t bootvector) "Captured boot vector for A53 proc %u: 0x%" PRIx64
+dmsc_a53_start(int core, uint64_t entry) "Cold-starting A53 core %d at entry 0x%" PRIx64 " (EL3/AArch64)"
+dmsc_a53_stop(int core) "Powering off A53 core %d"
+dmsc_handle_sys_reset(const char *type, const char *host_name) "%s received from host %s, requesting machine reset"
+dmsc_handle_fwl_set(const char *type, const char *host_name, uint16_t fwl_id, uint16_t region) "%s received from host %s, fwl_id %u, region %u"
+dmsc_handle_fwl_get(const char *type, const char *host_name, uint16_t fwl_id, uint16_t region) "%s received from host %s, fwl_id %u, region %u"
+dmsc_handle_fwl_change_owner(const char *type, const char *host_name, uint16_t fwl_id, uint16_t region, uint8_t owner_index) "%s received from host %s, fwl_id %u, region %u, owner_index %u"
+dmsc_handle_sa2ul_get_dkek(const char *type, const char *host_name, uint8_t sa2ul_instance) "%s received from host %s, sa2ul_instance %u (returning zeroed DKEK)"
+dmsc_handle_read_swrev(const char *type, const char *host_name) "%s received from host %s (returning swrev=0)"
+dmsc_handle_read_keycnt_keyrev(const char *type, const char *host_name) "%s received from host %s (returning keycnt=keyrev=0)"
+
 # ti-rat.c
 rat_enable_region(int idx, uint64_t size, uint64_t source, uint64_t dest) "Enabling RAT Region %u: size 0x%"PRIx64" map 0x%"PRIx64" -> 0x%"PRIx64
 rat_disable_region(int idx) "Disabling RAT Region %u"
include/hw/misc/ti-dmsc.h new file +661 -0

661 lines not on this page · raw message

Signature · 1 line
2.43.0
#15 lore raw

RFC PATCH v2 13/14 hw/arm: add the am64-virt machine

In reply to Wadim Mueller

Add a board around the AM64x SoC model. Besides the SoC it wires up a PL011 pair, a PL031 RTC, PL061 GPIO, CFI flash, a GPEX PCIe host bridge and an SD card, so that it is usable both for direct kernel boot and for running the full TI boot chain from a .wic image via -bios.

A generated device tree matching the machine is added under pc-bios/dtb for convenience, together with documentation and a MAINTAINERS entry.

---
 MAINTAINERS                |  22 +++
 docs/system/arm/am64.rst   |  74 ++++++++
 docs/system/target-arm.rst |   1 +
 hw/arm/Kconfig             |   9 +
 hw/arm/am64-virt.c         | 347 +++++++++++++++++++++++++++++++++++++
 hw/arm/meson.build         |   1 +
 pc-bios/dtb/am64-virt.dtb  | Bin 0 -> 6116 bytes
 pc-bios/dtb/am64-virt.dts  | 292 +++++++++++++++++++++++++++++++
 8 files changed, 746 insertions(+)
 create mode 100644 docs/system/arm/am64.rst
 create mode 100644 hw/arm/am64-virt.c
 create mode 100644 pc-bios/dtb/am64-virt.dtb
 create mode 100644 pc-bios/dtb/am64-virt.dts
MAINTAINERS +22 -0
index b51f5c3e60..dd0a3ee66a 100644
@@ -1266,6 +1266,28 @@ F: hw/arm/msf2-som.c
 F: docs/system/arm/emcraft-sf2.rst
 F: tests/functional/arm/test_emcraft_sf2.py
 
+TI AM64x / K3
+M: Wadim Mueller <[email protected]>
+L: [email protected]
+S: Maintained
+F: hw/arm/am64-virt.c
+F: hw/arm/ti-am64x.c
+F: hw/arm/k3-bootrom*.c
+F: hw/char/ti-am64-uart.c
+F: hw/misc/ti-*.c
+F: hw/timer/ti-k3-dmtimer.c
+F: include/hw/arm/ti-am64x.h
+F: include/hw/arm/k3-bootrom.h
+F: include/hw/char/ti-am64-uart.h
+F: include/hw/misc/ti-*.h
+F: include/hw/timer/ti-k3-dmtimer.h
+F: pc-bios/dtb/am64-virt.dt[sb]
+F: tests/functional/aarch64/test_am64_bootrom.py
+F: tests/qtest/am64-*.c
+F: tests/qtest/ti-am64-i2c-test.c
+F: tests/unit/test-k3-bootrom.c
+F: docs/system/arm/am64.rst
+
 ASPEED BMCs
 M: Cédric Le Goater <[email protected]>
 M: Peter Maydell <[email protected]>
docs/system/arm/am64.rst new file +74 -0

74 lines not on this page · raw message

docs/system/target-arm.rst +1 -0
index e34492402f..2fd41111e4 100644
@@ -71,6 +71,7 @@ Board-specific documentation
 .. toctree::
    :maxdepth: 1
 
+   arm/am64
    arm/max78000
    arm/integratorcp
    arm/mps2
hw/arm/Kconfig +9 -0
index 2172fc803b..3802170287 100644
@@ -38,6 +38,15 @@ config ARM_VIRT
     select ACPI_HMAT
     select WDT_SBSA
 
+config AM64_VIRT
+    bool
+    default y
+    depends on TCG && ARM
+    select AT24C  # I2C EEPROMs can be attached with -device
+    select TI_AM64X
+    select TI_DMSC
+    select TI_SEC_PROXY
+
 config CUBIEBOARD
     bool
     default y
hw/arm/am64-virt.c new file +347 -0

347 lines not on this page · raw message

hw/arm/meson.build +1 -0
index 139692dd11..cb6e881d01 100644
@@ -112,6 +112,7 @@ arm_common_ss.add(when: 'CONFIG_VEXPRESS', if_true: files('vexpress.c'))
 arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('ti-am64x.c'))
 arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('k3-bootrom-parse.c'))
 arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('k3-bootrom.c'))
+arm_common_ss.add(when: 'CONFIG_AM64_VIRT', if_true: files('am64-virt.c'))
 
 arm_common_ss.add(when: ['CONFIG_AXIADO_SOC', 'TARGET_AARCH64'], if_true: files(
   'ax3000-soc.c'))
pc-bios/dtb/am64-virt.dtb new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..b9cf2dd0b494ade59cd8cd4e4dc7b4d1434f7724
GIT binary patch
pc-bios/dtb/am64-virt.dts new file +292 -0

292 lines not on this page · raw message

Signature · 1 line
2.43.0
#13 lore raw

RFC PATCH v2 11/14 hw/arm: add TI K3 combined boot image parser

In reply to Wadim Mueller

The K3 boot ROM consumes a "combined boot image" (tiboot3.bin): an X.509 certificate carrying a TI-specific boot extension that describes the individual components (SYSFW, board config blobs, the R5 SPL), followed by the component data.

Add a standalone parser for that container. It is deliberately free of any device or machine state so it can be unit tested; the loader, which acts on the result, comes with the SoC model.

---
 hw/arm/k3-bootrom-parse.c   | 250 ++++++++++++++++++++++++++++++++++++
 hw/arm/meson.build          |   1 +
 include/hw/arm/k3-bootrom.h |  43 +++++++
 3 files changed, 294 insertions(+)
 create mode 100644 hw/arm/k3-bootrom-parse.c
 create mode 100644 include/hw/arm/k3-bootrom.h
hw/arm/k3-bootrom-parse.c new file +250 -0
new file mode 100644
index 0000000000..e3e428b6b1
@@ -0,0 +1,250 @@
+/*
+ * TI K3 boot-ROM emulation: X.509 combined boot image parser
+ *
+ * Parses the DER wrapper and ext_boot_info extension
+ * (OID 1.3.6.1.4.1.294.1.9) for payload type, destination and size.
+ * No signature verification, since QEMU models a GP device.
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/arm/k3-bootrom.h"
+
+typedef struct DerSlice {
+    const uint8_t *p;
+    const uint8_t *end;
+} DerSlice;
+
+static bool der_read_tlv(DerSlice *s, uint8_t *tag, DerSlice *content,
+                         Error **errp)
+{
+    uint64_t len;
+
+    if (s->end - s->p < 2) {
+        error_setg(errp, "k3-bootrom: truncated DER structure");
+        return false;
+    }
+    *tag = *s->p++;
+    len = *s->p++;
+    if (len & 0x80) {
+        unsigned n = len & 0x7f;
+
+        if (n == 0 || n > 4 || (size_t)(s->end - s->p) < n) {
+            error_setg(errp, "k3-bootrom: bad DER length encoding");
+            return false;
+        }
+        len = 0;
+        while (n--) {
+            len = (len << 8) | *s->p++;
+        }
+    }
+    if ((uint64_t)(s->end - s->p) < len) {
+        error_setg(errp, "k3-bootrom: DER length exceeds buffer");
+        return false;
+    }
+    content->p = s->p;
+    content->end = s->p + len;
+    s->p += len;
+    return true;
+}
+
+static bool der_read_uint(DerSlice *s, uint64_t *out, Error **errp)
+{
+    DerSlice c;
+    uint8_t tag;
+    uint64_t v = 0;
+
+    if (!der_read_tlv(s, &tag, &c, errp)) {
+        return false;
+    }
+    if (tag != 0x02) {
+        error_setg(errp, "k3-bootrom: expected INTEGER, got tag 0x%02x",
+                   tag);
+        return false;
+    }
+    if (c.p == c.end) {
+        error_setg(errp, "k3-bootrom: empty INTEGER");
+        return false;
+    }
+    if (c.end - c.p > 9 || (c.end - c.p == 9 && c.p[0] != 0)) {
+        error_setg(errp, "k3-bootrom: INTEGER too large");
+        return false;
+    }
+    for (const uint8_t *q = c.p; q < c.end; q++) {
+        v = (v << 8) | *q;
+    }
+    *out = v;
+    return true;
+}
+
+static bool der_read_u32(DerSlice *s, uint32_t *out, Error **errp)
+{
+    uint64_t v;
+
+    if (!der_read_uint(s, &v, errp)) {
+        return false;
+    }
+    if (v > UINT32_MAX) {
+        error_setg(errp, "k3-bootrom: integer field %" PRIu64
+                   " exceeds 32 bits", v);
+        return false;
+    }
+    *out = v;
+    return true;
+}
+
+/* Big-endian OCTET STRING (<= 8 bytes), as uint64. */
+static bool der_read_addr(DerSlice *s, uint64_t *out, Error **errp)
+{
+    DerSlice c;
+    uint8_t tag;
+    uint64_t v = 0;
+
+    if (!der_read_tlv(s, &tag, &c, errp)) {
+        return false;
+    }
+    if (tag != 0x04 || c.end - c.p > 8) {
+        error_setg(errp, "k3-bootrom: bad destAddr field (tag 0x%02x)",
+                   tag);
+        return false;
+    }
+    for (const uint8_t *q = c.p; q < c.end; q++) {
+        v = (v << 8) | *q;
+    }
+    *out = v;
+    return true;
+}
+
+/* DER TLV for TI ext_boot_info OID 1.3.6.1.4.1.294.1.9. */
+static const uint8_t k3_ext_boot_oid[] = {
+    0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x26, 0x01, 0x09
+};
+
+static const uint8_t *find_bytes(const uint8_t *hay, size_t hay_len,
+                                 const uint8_t *needle, size_t needle_len)
+{
+    if (hay_len < needle_len) {
+        return NULL;
+    }
+    for (size_t i = 0; i + needle_len <= hay_len; i++) {
+        if (memcmp(hay + i, needle, needle_len) == 0) {
+            return hay + i;
+        }
+    }
+    return NULL;
+}
+
+bool k3_bootrom_parse(const uint8_t *buf, size_t len, K3BootImage *out,
+                      Error **errp)
+{
+    DerSlice top = { buf, buf + len };
+    DerSlice cert, rest, octets, info;
+    const uint8_t *oid;
+    uint8_t tag;
+    uint64_t v;
+    uint64_t payload_off;
+
+    memset(out, 0, sizeof(*out));
+
+    if (!der_read_tlv(&top, &tag, &cert, errp)) {
+        return false;
+    }
+    if (tag != 0x30) {
+        error_setg(errp,
+                   "k3-bootrom: not an X.509 boot image (tag 0x%02x)", tag);
+        return false;
+    }
+    out->cert_len = cert.end - buf;
+
+    oid = find_bytes(cert.p, cert.end - cert.p, k3_ext_boot_oid,
+                     sizeof(k3_ext_boot_oid));
+    if (!oid) {
+        error_setg(errp, "k3-bootrom: ext_boot_info extension "
+                   "(OID 1.3.6.1.4.1.294.1.9) not found");
+        return false;
+    }
+    rest.p = oid + sizeof(k3_ext_boot_oid);
+    rest.end = cert.end;
+
+    /* Optional BOOLEAN 'critical', between OID and extnValue. */
+    if (rest.p < rest.end && rest.p[0] == 0x01) {
+        DerSlice skip;
+
+        if (!der_read_tlv(&rest, &tag, &skip, errp)) {
+            return false;
+        }
+    }
+    if (!der_read_tlv(&rest, &tag, &octets, errp)) {
+        return false;
+    }
+    if (tag != 0x04) {
+        error_setg(errp, "k3-bootrom: extension value is not an "
+                   "OCTET STRING (tag 0x%02x)", tag);
+        return false;
+    }
+    if (!der_read_tlv(&octets, &tag, &info, errp)) {
+        return false;
+    }
+    if (tag != 0x30) {
+        error_setg(errp, "k3-bootrom: ext_boot_info is not a SEQUENCE");
+        return false;
+    }
+
+    if (!der_read_uint(&info, &out->ext_img_size, errp)) {
+        return false;
+    }
+    if (!der_read_uint(&info, &v, errp)) {
+        return false;
+    }
+    if (v == 0 || v > K3_BOOTROM_MAX_COMPS) {
+        error_setg(errp, "k3-bootrom: unsupported component count %"
+                   PRIu64, v);
+        return false;
+    }
+    out->num_comps = v;
+
+    payload_off = out->cert_len;
+    for (uint32_t i = 0; i < out->num_comps; i++) {
+        K3BootComponent *c = &out->comps[i];
+        DerSlice comp;
+
+        if (!der_read_tlv(&info, &tag, &comp, errp)) {
+            return false;
+        }
+        if (tag != 0x30) {
+            error_setg(errp, "k3-bootrom: component %u is not a SEQUENCE",
+                       i);
+            return false;
+        }
+        if (!der_read_u32(&comp, &c->comp_type, errp)) {
+            return false;
+        }
+        if (!der_read_u32(&comp, &c->boot_core, errp)) {
+            return false;
+        }
+        if (!der_read_u32(&comp, &c->comp_opts, errp)) {
+            return false;
+        }
+        if (!der_read_addr(&comp, &c->dest_addr, errp)) {
+            return false;
+        }
+        if (!der_read_u32(&comp, &c->comp_size, errp)) {
+            return false;
+        }
+        /* shaType / shaValue are not needed for the loading. */
+        /* payload_off is bounded by len below, so it fits into size_t. */
+        c->payload_offset = payload_off;
+        payload_off += c->comp_size;
+        if (payload_off > len) {
+            error_setg(errp, "k3-bootrom: image truncated (components "
+                       "need %" PRIu64 " bytes, file has %zu)",
+                       payload_off, len);
+            return false;
+        }
+    }
+    return true;
+}
hw/arm/meson.build +1 -0
index 8ee5307a91..22691afdd8 100644
@@ -109,6 +109,7 @@ arm_common_ss.add(when: 'CONFIG_STRONGARM', if_true: files('strongarm.c'))
 arm_common_ss.add(when: 'CONFIG_SX1', if_true: files('omap_sx1.c'))
 arm_common_ss.add(when: 'CONFIG_VERSATILE', if_true: files('versatilepb.c'))
 arm_common_ss.add(when: 'CONFIG_VEXPRESS', if_true: files('vexpress.c'))
+arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('k3-bootrom-parse.c'))
 
 arm_common_ss.add(when: ['CONFIG_AXIADO_SOC', 'TARGET_AARCH64'], if_true: files(
   'ax3000-soc.c'))
include/hw/arm/k3-bootrom.h new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.43.0
#14 lore raw

RFC PATCH v2 12/14 hw/arm: add TI AM64x SoC model

In reply to Wadim Mueller

Add a model of the TI AM64x (AM6442) Sitara SoC tying the previous patches together:

- Cortex-A53 cluster with a GICv3 (GIC-500) at the real addresses
- Cortex-R5F MCU boot core and a Cortex-M4F, each behind their own RAT
- MCU and MAIN domain UARTs, mailboxes, secure proxy instances and the DMSC endpoint
- CTRL_MMR, GTC, DDRSS, DMTimer, TRNG, SDHCI controllers with the K3 PHY wrapper and the MAIN domain I2C0
- OCSRAM and DDR windows

Also add the ROM boot loader, which parses a combined boot image, places the components and starts the R5F boot core at the certified entry point, mirroring what the on-chip boot ROM does.

---
 hw/arm/Kconfig            |   16 +
 hw/arm/k3-bootrom.c       |  125 ++++
 hw/arm/meson.build        |    2 +
 hw/arm/ti-am64x.c         | 1223 +++++++++++++++++++++++++++++++++++++
 hw/arm/trace-events       |    4 +
 include/hw/arm/ti-am64x.h |   86 +++
 6 files changed, 1456 insertions(+)
 create mode 100644 hw/arm/k3-bootrom.c
 create mode 100644 hw/arm/ti-am64x.c
 create mode 100644 include/hw/arm/ti-am64x.h
hw/arm/Kconfig +16 -0
index b2dc2714c0..2172fc803b 100644
@@ -415,6 +415,22 @@ config STM32L4X5_SOC
     select STM32L4X5_GPIO
     select STM32L4X5_USART
 
+config TI_AM64X
+    bool
+    select ARM_V7M
+    select ARM_GIC
+    select OMAP_I2C
+    select TI_MAILBOX
+    select TI_RAT
+    select TI_K3_CTRLMMR
+    select TI_K3_GTC
+    select TI_K3_DDRSS
+    select TI_K3_DMTIMER
+    select TI_K3_SDHCI_PHY
+    select TI_K3_TRNG
+    select SDHCI
+    select AM64_UART
+
 config XLNX_ZYNQMP_ARM
     bool
     default y if PIXMAN
hw/arm/k3-bootrom.c new file +125 -0

125 lines not on this page · raw message

hw/arm/meson.build +2 -0
index 22691afdd8..139692dd11 100644
@@ -109,7 +109,9 @@ arm_common_ss.add(when: 'CONFIG_STRONGARM', if_true: files('strongarm.c'))
 arm_common_ss.add(when: 'CONFIG_SX1', if_true: files('omap_sx1.c'))
 arm_common_ss.add(when: 'CONFIG_VERSATILE', if_true: files('versatilepb.c'))
 arm_common_ss.add(when: 'CONFIG_VEXPRESS', if_true: files('vexpress.c'))
+arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('ti-am64x.c'))
 arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('k3-bootrom-parse.c'))
+arm_common_ss.add(when: 'CONFIG_TI_AM64X', if_true: files('k3-bootrom.c'))
 
 arm_common_ss.add(when: ['CONFIG_AXIADO_SOC', 'TARGET_AARCH64'], if_true: files(
   'ax3000-soc.c'))
hw/arm/ti-am64x.c new file +1223 -0

1,223 lines not on this page · raw message

hw/arm/trace-events +4 -0
index 1b16f710fe..605b71dd62 100644
@@ -96,3 +96,7 @@ z2_aer915_event(int8_t event, int8_t len) "i2c event =0x%x len=%d bytes"
 
 # bcm2838.c
 bcm2838_gic_set_irq(int irq, int level) "gic irq:%d lvl:%d"
+
+# k3-bootrom.c
+k3_bootrom_component(uint32_t comp_type, uint64_t dest, uint32_t size) "comp_type %u dest 0x%" PRIx64 " size 0x%x"
+k3_bootrom_boot(uint64_t entry) "starting R5F0_0 at 0x%" PRIx64
include/hw/arm/ti-am64x.h new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

RFC PATCH v2 9/14 hw/misc: add TI K3 secure proxy model

In reply to Wadim Mueller

The secure proxy is the message transport between the K3 host cores and the device management and security controller (DMSC). Model the per-thread RT/SCFG register banks and the message buffers, and provide a callback interface, which the DMSC model plugs into.

---
 hw/misc/Kconfig                |   3 +
 hw/misc/meson.build            |   1 +
 hw/misc/ti-sec-proxy.c         | 446 +++++++++++++++++++++++++++++++++
 hw/misc/trace-events           |   8 +
 include/hw/misc/ti-sec-proxy.h | 108 ++++++++
 5 files changed, 566 insertions(+)
 create mode 100644 hw/misc/ti-sec-proxy.c
 create mode 100644 include/hw/misc/ti-sec-proxy.h
hw/misc/Kconfig +3 -0
index e80fdc3214..bea8e9341f 100644
@@ -161,6 +161,9 @@ config TI_K3_SDHCI_PHY
 config TI_K3_TRNG
     bool
 
+config TI_SEC_PROXY
+    bool
+
 config TI_MAILBOX
     bool
 
hw/misc/meson.build +1 -0
index d190d8608a..a8a40ea2a7 100644
@@ -129,6 +129,7 @@ system_ss.add(when: 'CONFIG_TI_K3_GTC', if_true: files('ti-k3-gtc.c'))
 system_ss.add(when: 'CONFIG_TI_K3_DDRSS', if_true: files('ti-k3-ddrss.c'))
 system_ss.add(when: 'CONFIG_TI_K3_SDHCI_PHY', if_true: files('ti-k3-sdhci-phy.c'))
 system_ss.add(when: 'CONFIG_TI_K3_TRNG', if_true: files('ti-k3-trng.c'))
+system_ss.add(when: 'CONFIG_TI_SEC_PROXY', if_true: files('ti-sec-proxy.c'))
 system_ss.add(when: 'CONFIG_TI_MAILBOX', if_true: files('ti-mailbox.c'))
 system_ss.add(when: 'CONFIG_TZ_MPC', if_true: files('tz-mpc.c'))
 system_ss.add(when: 'CONFIG_TZ_MSC', if_true: files('tz-msc.c'))
hw/misc/ti-sec-proxy.c new file +446 -0

446 lines not on this page · raw message

hw/misc/trace-events +8 -0
index ea7cde021e..dfa2d04117 100644
@@ -461,6 +461,14 @@ rat_enable_region(int idx, uint64_t size, uint64_t source, uint64_t dest) "Enabl
 rat_disable_region(int idx) "Disabling RAT Region %u"
 rat_read_entry(int entry, int rel_offset, uint64_t offset) "Reading Entry %i at offset %i: offset: 0x%"PRIx64
 
+# ti-sec-proxy.c
+ti_sec_proxy_read_scfg(const char *name, int reg, uint64_t addr) "thread: %s, reg: %i. read from SCFG addr 0x%" PRIx64
+ti_sec_proxy_write_scfg(const char *name, int reg, uint64_t addr, uint64_t value) "thread: %s, reg: %i. write 0x%" PRIx64 "0x%" PRIx64
+ti_sec_proxy_complete_read(const char *name, int num) "thread: %s completed read message %i. Resetting number of messages"
+ti_sec_proxy_complete_write(const char *name, int num) "thread: %s completed write message %i. Resetting number of messages"
+ti_sec_proxy_announce_callback(const char *name) "invoce callback for thread: %s"
+ti_sec_proxy_read_rt(const char *name, int reg, uint64_t addr) "thread: %s, reg: 0x%x, read from RT addr 0x%" PRIx64
+
 # ti-k3-trng.c
 ti_k3_trng_read(uint64_t addr, uint32_t val) "offset 0x%" PRIx64 " -> 0x%08x"
 ti_k3_trng_write(uint64_t addr, uint64_t val) "offset 0x%" PRIx64 " <- 0x%" PRIx64
include/hw/misc/ti-sec-proxy.h new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

RFC PATCH v2 7/14 hw/misc: add TI RAT (region address translation) model

In reply to Wadim Mueller

The R5F and M4F subsystems of the K3 devices reach the 64-bit SoC address space through a RAT, which maps windows of the core's 32-bit view onto system addresses. Model it as a set of translation regions layered as memory region aliases into the core's address space.

---
 hw/misc/Kconfig          |   3 +
 hw/misc/meson.build      |   1 +
 hw/misc/ti-rat.c         | 290 +++++++++++++++++++++++++++++++++++++++
 hw/misc/trace-events     |   5 +
 include/hw/misc/ti-rat.h |  51 +++++++
 5 files changed, 350 insertions(+)
 create mode 100644 hw/misc/ti-rat.c
 create mode 100644 include/hw/misc/ti-rat.h
hw/misc/Kconfig +3 -0
index 3e499a902d..c176d6d6c7 100644
@@ -143,6 +143,9 @@ config MPS2_SCC
     bool
     select LED
 
+config TI_RAT
+    bool
+
 config TI_K3_CTRLMMR
     bool
 
hw/misc/meson.build +1 -0
index e973d7e8a4..952b7b7c2f 100644
@@ -123,6 +123,7 @@ system_ss.add(when: 'CONFIG_STM32L4X5_RCC', if_true: files('stm32l4x5_rcc.c'))
 system_ss.add(when: 'CONFIG_MPS2_FPGAIO', if_true: files('mps2-fpgaio.c'))
 system_ss.add(when: 'CONFIG_MPS2_SCC', if_true: files('mps2-scc.c'))
 
+system_ss.add(when: 'CONFIG_TI_RAT', if_true: files('ti-rat.c'))
 system_ss.add(when: 'CONFIG_TI_K3_CTRLMMR', if_true: files('ti-k3-ctrlmmr.c'))
 system_ss.add(when: 'CONFIG_TI_K3_GTC', if_true: files('ti-k3-gtc.c'))
 system_ss.add(when: 'CONFIG_TI_K3_DDRSS', if_true: files('ti-k3-ddrss.c'))
hw/misc/ti-rat.c new file +290 -0

290 lines not on this page · raw message

hw/misc/trace-events +5 -0
index b32ce80ea9..fae1f90ee2 100644
@@ -443,6 +443,11 @@ iommu_testdev_dma_verify(uint32_t expected, uint32_t actual) "expected=0x%x actu
 iommu_testdev_dma_result(uint32_t result) "DMA completed result=0x%x"
 iommu_testdev_dma_armed(bool armed) "armed=%d"
 
+# ti-rat.c
+rat_enable_region(int idx, uint64_t size, uint64_t source, uint64_t dest) "Enabling RAT Region %u: size 0x%"PRIx64" map 0x%"PRIx64" -> 0x%"PRIx64
+rat_disable_region(int idx) "Disabling RAT Region %u"
+rat_read_entry(int entry, int rel_offset, uint64_t offset) "Reading Entry %i at offset %i: offset: 0x%"PRIx64
+
 # ti-k3-trng.c
 ti_k3_trng_read(uint64_t addr, uint32_t val) "offset 0x%" PRIx64 " -> 0x%08x"
 ti_k3_trng_write(uint64_t addr, uint64_t val) "offset 0x%" PRIx64 " <- 0x%" PRIx64
include/hw/misc/ti-rat.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.43.0
#8 lore raw

RFC PATCH v2 6/14 hw/misc: add TI K3 CTRL_MMR, GTC, DDRSS, SDHCI PHY and TRNG models

In reply to Wadim Mueller

Add the small K3 support blocks the boot chain touches:

- ti-k3-ctrlmmr: control module registers, including the DEVSTAT boot pins, which the ROM and SPL use to select the boot device.
- ti-k3-gtc: global timebase counter, read by TF-A.
- ti-k3-ddrss: DDR subsystem stub that reports a successful DRAM init (including BIST/ECC priming status) to the R5 SPL.
- ti-k3-sdhci-phy: PHY control/calibration registers of the K3 SDHCI wrapper, reporting calibration as done.
- ti-k3-trng: TRNG stub used by OP-TEE.

These are register-level stubs: they implement the handshakes that firmware polls for, not the underlying hardware behaviour.

---
 hw/misc/Kconfig                   |  15 ++++
 hw/misc/meson.build               |   6 ++
 hw/misc/ti-k3-ctrlmmr.c           | 103 ++++++++++++++++++++++
 hw/misc/ti-k3-ddrss.c             |  98 +++++++++++++++++++++
 hw/misc/ti-k3-gtc.c               | 100 +++++++++++++++++++++
 hw/misc/ti-k3-sdhci-phy.c         |  91 +++++++++++++++++++
 hw/misc/ti-k3-trng.c              | 139 ++++++++++++++++++++++++++++++
 hw/misc/trace-events              |   4 +
 include/hw/misc/ti-k3-ctrlmmr.h   |  26 ++++++
 include/hw/misc/ti-k3-ddrss.h     |  26 ++++++
 include/hw/misc/ti-k3-gtc.h       |  23 +++++
 include/hw/misc/ti-k3-sdhci-phy.h |  26 ++++++
 include/hw/misc/ti-k3-trng.h      |  35 ++++++++
 13 files changed, 692 insertions(+)
 create mode 100644 hw/misc/ti-k3-ctrlmmr.c
 create mode 100644 hw/misc/ti-k3-ddrss.c
 create mode 100644 hw/misc/ti-k3-gtc.c
 create mode 100644 hw/misc/ti-k3-sdhci-phy.c
 create mode 100644 hw/misc/ti-k3-trng.c
 create mode 100644 include/hw/misc/ti-k3-ctrlmmr.h
 create mode 100644 include/hw/misc/ti-k3-ddrss.h
 create mode 100644 include/hw/misc/ti-k3-gtc.h
 create mode 100644 include/hw/misc/ti-k3-sdhci-phy.h
 create mode 100644 include/hw/misc/ti-k3-trng.h
hw/misc/Kconfig +15 -0
index b8860dd3e7..3e499a902d 100644
@@ -143,6 +143,21 @@ config MPS2_SCC
     bool
     select LED
 
+config TI_K3_CTRLMMR
+    bool
+
+config TI_K3_GTC
+    bool
+
+config TI_K3_DDRSS
+    bool
+
+config TI_K3_SDHCI_PHY
+    bool
+
+config TI_K3_TRNG
+    bool
+
 config TZ_MPC
     bool
 
hw/misc/meson.build +6 -0
index e86d9ad6b3..e973d7e8a4 100644
@@ -96,6 +96,7 @@ system_ss.add(when: 'CONFIG_RASPI', if_true: files(
   'bcm2835_cprman.c',
   'bcm2835_powermgt.c',
 ))
+
 system_ss.add(when: 'CONFIG_SLAVIO', if_true: files('slavio_misc.c'))
 system_ss.add(when: 'CONFIG_ZYNQ', if_true: files('zynq_slcr.c'))
 system_ss.add(when: 'CONFIG_XLNX_ZYNQ_DDRC', if_true: files('xlnx-zynq-ddrc.c'))
@@ -122,6 +123,11 @@ system_ss.add(when: 'CONFIG_STM32L4X5_RCC', if_true: files('stm32l4x5_rcc.c'))
 system_ss.add(when: 'CONFIG_MPS2_FPGAIO', if_true: files('mps2-fpgaio.c'))
 system_ss.add(when: 'CONFIG_MPS2_SCC', if_true: files('mps2-scc.c'))
 
+system_ss.add(when: 'CONFIG_TI_K3_CTRLMMR', if_true: files('ti-k3-ctrlmmr.c'))
+system_ss.add(when: 'CONFIG_TI_K3_GTC', if_true: files('ti-k3-gtc.c'))
+system_ss.add(when: 'CONFIG_TI_K3_DDRSS', if_true: files('ti-k3-ddrss.c'))
+system_ss.add(when: 'CONFIG_TI_K3_SDHCI_PHY', if_true: files('ti-k3-sdhci-phy.c'))
+system_ss.add(when: 'CONFIG_TI_K3_TRNG', if_true: files('ti-k3-trng.c'))
 system_ss.add(when: 'CONFIG_TZ_MPC', if_true: files('tz-mpc.c'))
 system_ss.add(when: 'CONFIG_TZ_MSC', if_true: files('tz-msc.c'))
 system_ss.add(when: 'CONFIG_TZ_PPC', if_true: files('tz-ppc.c'))
hw/misc/ti-k3-ctrlmmr.c new file +103 -0

103 lines not on this page · raw message

hw/misc/ti-k3-ddrss.c new file +98 -0

98 lines not on this page · raw message

hw/misc/ti-k3-gtc.c new file +100 -0

100 lines not on this page · raw message

hw/misc/ti-k3-sdhci-phy.c new file +91 -0

91 lines not on this page · raw message

hw/misc/ti-k3-trng.c new file +139 -0

139 lines not on this page · raw message

hw/misc/trace-events +4 -0
index c9a868b3ef..b32ce80ea9 100644
@@ -442,3 +442,7 @@ iommu_testdev_dma_read(uint64_t gva, uint32_t len) "gva=0x%" PRIx64 " len=%u"
 iommu_testdev_dma_verify(uint32_t expected, uint32_t actual) "expected=0x%x actual=0x%x"
 iommu_testdev_dma_result(uint32_t result) "DMA completed result=0x%x"
 iommu_testdev_dma_armed(bool armed) "armed=%d"
+
+# ti-k3-trng.c
+ti_k3_trng_read(uint64_t addr, uint32_t val) "offset 0x%" PRIx64 " -> 0x%08x"
+ti_k3_trng_write(uint64_t addr, uint64_t val) "offset 0x%" PRIx64 " <- 0x%" PRIx64
include/hw/misc/ti-k3-ctrlmmr.h new file +26 -0
new file mode 100644
index 0000000000..01fb71bc74
@@ -0,0 +1,26 @@
+/*
+ * TI K3 CTRL_MMR stub
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_TI_K3_CTRLMMR_H
+#define HW_MISC_TI_K3_CTRLMMR_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_TI_K3_CTRLMMR "ti-k3-ctrlmmr"
+OBJECT_DECLARE_SIMPLE_TYPE(TIK3CtrlMmrState, TI_K3_CTRLMMR)
+
+struct TIK3CtrlMmrState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+    uint32_t devstat;
+    uint32_t rst_src;
+    uint32_t sec_mgr_sys_status;
+};
+
+#endif
include/hw/misc/ti-k3-ddrss.h new file +26 -0
new file mode 100644
index 0000000000..ed0f54e5c4
@@ -0,0 +1,26 @@
+/*
+ * TI K3 DDRSS register-file stub (AM64x)
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_TI_K3_DDRSS_H
+#define HW_MISC_TI_K3_DDRSS_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_TI_K3_DDRSS "ti-k3-ddrss"
+OBJECT_DECLARE_SIMPLE_TYPE(TIK3DdrssState, TI_K3_DDRSS)
+
+#define TI_K3_DDRSS_CFG_SIZE 0x8000
+
+struct TIK3DdrssState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+    uint32_t regs[TI_K3_DDRSS_CFG_SIZE / 4];
+};
+
+#endif
include/hw/misc/ti-k3-gtc.h new file +23 -0
new file mode 100644
index 0000000000..a2a22afbf6
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ */
+#ifndef HW_MISC_TI_K3_GTC_H
+#define HW_MISC_TI_K3_GTC_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_TI_K3_GTC "ti-k3-gtc"
+OBJECT_DECLARE_SIMPLE_TYPE(TIK3GtcState, TI_K3_GTC)
+
+struct TIK3GtcState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+
+    uint32_t cntcr;    /* GTC control: bit0 EN. Read back enabled. */
+    uint32_t cntfid0;  /* GTC frequency id 0, in Hz. */
+};
+
+#endif /* HW_MISC_TI_K3_GTC_H */
include/hw/misc/ti-k3-sdhci-phy.h new file +26 -0
new file mode 100644
index 0000000000..2a7296fc6f
@@ -0,0 +1,26 @@
+/*
+ * TI K3 SDHCI PHY register-file stub (AM64x)
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_TI_K3_SDHCI_PHY_H
+#define HW_MISC_TI_K3_SDHCI_PHY_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_TI_K3_SDHCI_PHY "ti-k3-sdhci-phy"
+OBJECT_DECLARE_SIMPLE_TYPE(TIK3SdhciPhyState, TI_K3_SDHCI_PHY)
+
+#define TI_K3_SDHCI_PHY_SIZE 0x400
+
+struct TIK3SdhciPhyState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+    uint32_t regs[TI_K3_SDHCI_PHY_SIZE / 4];
+};
+
+#endif
include/hw/misc/ti-k3-trng.h new file +35 -0
new file mode 100644
index 0000000000..b9c33b589f
@@ -0,0 +1,35 @@
+/*
+ * TI K3 SA2UL TRNG (EIP-76) stub (AM64x)
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_TI_K3_TRNG_H
+#define HW_MISC_TI_K3_TRNG_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_TI_K3_TRNG "ti-k3-trng"
+OBJECT_DECLARE_SIMPLE_TYPE(TIK3TrngState, TI_K3_TRNG)
+
+/* EIP-76 register window: offsets 0x00..0x7c. */
+#define TI_K3_TRNG_REGS_SIZE 0x80
+
+struct TIK3TrngState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+
+    /* Nonzero xorshift64 generator state. */
+    uint64_t rng_state;
+    /* Cached pairs for OUTPUT_0/1 and OUTPUT_2/3. */
+    uint64_t pair_a;
+    uint64_t pair_b;
+
+    /* RAM-backed registers except OUTPUT_x and STATUS. */
+    uint32_t regs[TI_K3_TRNG_REGS_SIZE / 4];
+};
+
+#endif
Signature · 1 line
2.43.0
#17 lore raw

Re: [RFC PATCH v2 14/14] tests: add AM64x unit, qtest and functional tests

In reply to Wadim Mueller
Quoted 14 lines · Wadim Mueller

Cover the new code:

- tests/unit/test-k3-bootrom: combined boot image parser, including malformed input.
- tests/qtest/am64-virt-test: machine topology, GICv3 redistributor layout, SMP handling, DMSC TI-SCI request/response handling.
- tests/qtest/ti-am64-i2c-test: I2C soft reset, NACK on an absent address and a transfer against the on-SoM EEPROM.
- tests/qtest/am64-adma-pacing-test: reproducer for the SDHCI ADMA pacing fix earlier in this series; without the fix it fails.
- tests/functional/aarch64/test_am64_bootrom: builds a synthetic combined boot image with a bare-metal R5 payload and checks that the ROM boot path runs it. If QEMU_TEST_TIBOOT3 points at a real tiboot3.bin, that is booted as well.

I commend you for including tests. One of the challenges of the myriad of board models we have is not having any way to know if they are used. It certainly helps if there are known images and tests to validate this.

Quoted 11 lines
---
 tests/functional/aarch64/meson.build          |   2 +
 tests/functional/aarch64/test_am64_bootrom.py | 137 +++++
 tests/qtest/am64-adma-pacing-test.c           | 243 +++++++++
 tests/qtest/am64-virt-test.c                  | 502 ++++++++++++++++++
 tests/qtest/meson.build                       |   1 +
 tests/qtest/ti-am64-i2c-test.c                | 137 +++++
 tests/unit/meson.build                        |   1 +
 tests/unit/test-k3-bootrom.c                  | 263 +++++++++

That said I think splitting the unit, qtest and functional tests into separate commits would be worthwhile here.

Quoted 125 lines
 8 files changed, 1286 insertions(+)
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c
tests/functional/aarch64/meson.build +2 -0
index e81afd6c39..6962ae1da1 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 test_aarch64_timeouts = {
+  'am64_bootrom' : 300,
   'aspeed_ast2700a1' : 600,
   'aspeed_ast2700a2' : 600,
   'aspeed_ast2700fc' : 600,
@@ -26,6 +27,7 @@ tests_aarch64_system_quick = [
 ]
 
 tests_aarch64_system_thorough = [
+  'am64_bootrom',
   'aspeed_ast2700a1',
   'aspeed_ast2700a2',
   'aspeed_ast2700fc',
tests/functional/aarch64/test_am64_bootrom.py new file +92 -0
new file mode 100644
index 0000000000..2203ec7585
@@ -0,0 +1,137 @@
+#!/usr/bin/env python3
+#
+# Boot-ROM emulation test for am64-virt machine: build a synthetic TI
+# combined boot image and check the R5 payload runs from the certified
+# entry.  Optionally boots a vendor tiboot3.bin via QEMU_TEST_TIBOOT3.
+#
+# Copyright (c) 2026 CMBLU Energy AG
+# Author: Wadim Mueller <[email protected]>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+import struct
+
+from qemu_test import Asset, QemuSystemTest, wait_for_console_pattern
+from unittest import skipUnless
+
+# Checked-in DTB path; pc-bios/dtb/am64-virt.dtb does not get copied into
+# the build directory.
+SOURCE_DIR = os.path.normpath(
+    os.path.join(os.path.dirname(__file__), '..', '..', '..'))
+
+
+def der(tag, payload):
+    n = len(payload)
+    if n < 0x80:
+        hdr = bytes([tag, n])
+    else:
+        hdr = bytes([tag, 0x82, n >> 8, n & 0xff])
+    return hdr + payload
+
+
+def der_int(v):
+    out = v.to_bytes((v.bit_length() + 7) // 8 or 1, 'big')
+    if out[0] & 0x80:
+        out = b'\x00' + out
+    return der(0x02, out)
+
+
+EXT_BOOT_OID = bytes.fromhex('06092b0601040182260109')
+SHA512_OID = bytes.fromhex('0609608648016503040203')
+
+
+def component(ctype, core, opts, dest, size):
+    return der(0x30,
+               der_int(ctype) + der_int(core) + der_int(opts) +
+               der(0x04, dest.to_bytes(4, 'big')) + der_int(size) +
+               SHA512_OID + der(0x04, bytes(64)))
+
+
+# Bare-metal A32 stub, linked at 0x70000000: prints a magic string on
+# main UART0 (0x02800000, 16550 THR at offset 0), then parks.
+SBL_STUB = struct.pack(
+    '<10I',
+    0xe59f001c,  # ldr r0, [pc, #0x1c]   ; r0 = 0x02800000
+    0xe28f101c,  # add r1, pc, #0x1c     ; r1 = msg
+    0xe4d12001,  # loop: ldrb r2, [r1], #1
+    0xe3520000,  # cmp r2, #0
+    0x0a000001,  # beq hang
+    0xe5802000,  # str r2, [r0]
+    0xeafffffa,  # b loop
+    0xeafffffe,  # hang: b hang
+    0x00000000,  # (pad)
+    0x02800000,  # UART0 literal
+) + b'K3BOOTROM-OK\r\n\x00'
+
+
+def make_tiboot3():
+    sbl = SBL_STUB
+    sysfw = b'FAKE-SYSFW-PAYLOAD'
+    cfg = b'FAKE-CFG'
+    info = der(0x30,
+               der_int(len(sbl) + len(sysfw) + len(cfg)) + der_int(3) +
+               component(1, 16, 0, 0x70000000, len(sbl)) +
+               component(2, 0, 0, 0x44000, len(sysfw)) +
+               component(18, 0, 0, 0x7b000, len(cfg)))
+    ext = der(0x30, EXT_BOOT_OID + der(0x04, info))
+    cert = der(0x30, ext)
+    return cert + sbl + sysfw + cfg
+
+
+class Am64BootRom(QemuSystemTest):
+
+    # The gated Linux boot subtest can need about 90 s under TCG on a
+    # development host. 300 s leaves room for slower CI machines.
+    timeout = 300
+
+    def boot_bios(self, path):
+        self.set_machine('am64-virt')
+        self.vm.set_console()
+        self.vm.add_args('-bios', path)
+        self.vm.launch()

tests/functional/aarch64/test_am64_bootrom.py line 102

Quoted 10 lines
tests/functional/aarch64/test_am64_bootrom.py new file +10 -0
+
+    def test_synthetic_image(self):
+        path = os.path.join(self.workdir, 'tiboot3-synth.bin')
+        with open(path, 'wb') as f:
+            f.write(make_tiboot3())
+        self.boot_bios(path)
+        wait_for_console_pattern(self, 'K3BOOTROM-OK')
+
+    @skipUnless(os.getenv('QEMU_TEST_TIBOOT3'),
+                'set QEMU_TEST_TIBOOT3=<path to tiboot3.bin>')

Are there any publicly available images we could boot on this board? I'm sure for you QEMU_TEST_TIBOOT3 works well - but having a public known good image means other devs can check things haven't broken on a refactor.

Quoted 1,228 lines
tests/functional/aarch64/test_am64_bootrom.py new file +35 -0
+    def test_vendor_tiboot3(self):
+        self.boot_bios(os.getenv('QEMU_TEST_TIBOOT3'))
+        # The SYSFW ABI line also proves boot notification plus TISCI
+        # VERSION before execution reaches the unmodelled DDR init.
+        wait_for_console_pattern(self, 'U-Boot SPL')
+        wait_for_console_pattern(self, 'SYSFW ABI:')
+
+    # Standalone arm64 netboot kernel.  It has PL011 and GICv3 drivers but
+    # no initramfs, so expected rootfs panic comes after the milestones,
+    # which this test cares about.
+    ASSET_KERNEL = Asset(
+        ('http://ports.ubuntu.com/ubuntu-ports/dists/bionic-updates/main/'
+         'installer-arm64/20101020ubuntu543.19/images/netboot/'
+         'ubuntu-installer/arm64/linux'),
+        'ce54f74ab0b15cfd13d1a293f2d27ffd79d8a85b7bb9bf21093ae9513864ac79')
+
+    def test_linux_gicv3(self):
+        kernel_path = self.ASSET_KERNEL.fetch()
+        dtb = os.path.join(SOURCE_DIR, 'pc-bios', 'dtb', 'am64-virt.dtb')
+        self.set_machine('am64-virt')
+        self.vm.set_console()
+        self.vm.add_args('-kernel', kernel_path,
+                         '-dtb', dtb,
+                         '-append', 'console=ttyAMA0 earlycon')
+        self.vm.launch()
+        wait_for_console_pattern(
+            self,
+            'GICv3: CPU0: found redistributor 0 region '
+            '0:0x0000000001840000')
+        wait_for_console_pattern(self, 'CPU1: Booted secondary processor')
+        wait_for_console_pattern(self, 'ttyAMA0')
+
+
+if __name__ == '__main__':
+    QemuSystemTest.main()
tests/qtest/am64-adma-pacing-test.c new file +243 -0
new file mode 100644
index 0000000000..7b503d86ed
@@ -0,0 +1,243 @@
+/*
+ * QTest for AM64x SDHCI ADMA2 virtual-clock pacing
+ *
+ * CMD18 over a 12-descriptor ADMA2 chain has to complete synchronously
+ * when no DMA-boundary interrupt is requested.  A sliced transfer needs
+ * at least one SDHC_TRANSFER_DELAY virtual-clock step.
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/*
+ * Keep the small SDHCI subset local; sdhci-internal.h is not usable from
+ * qtests, since it exposes SDHCIState internals.
+ */
+#define SDHC_ARGUMENT              0x08
+#define SDHC_TRNMOD                0x0C
+#define SDHC_TRNS_DMA              0x0001
+#define SDHC_TRNS_BLK_CNT_EN       0x0002
+#define SDHC_TRNS_READ             0x0010
+#define SDHC_TRNS_MULTI            0x0020
+#define SDHC_CMDREG                0x0E
+#define SDHC_CMD_DATA_PRESENT      (1 << 5)
+#define SDHC_RSPREG0               0x10
+#define SDHC_PRNSTS                0x24
+#define SDHC_CARD_PRESENT          0x00010000
+#define SDHC_HOSTCTL               0x28
+#define SDHC_CTRL_ADMA2_32         0x10
+#define SDHC_BLKSIZE               0x04
+#define SDHC_CLKCON                0x2C
+#define SDHC_CLOCK_INT_EN          0x0001
+#define SDHC_CLOCK_SDCLK_EN        (1 << 2)
+#define SDHC_SWRST                 0x2F
+#define SDHC_RESET_ALL             0x01
+#define SDHC_NORINTSTS             0x30
+#define SDHC_NIS_ERR               0x8000
+#define SDHC_NIS_CMDCMP            0x0001
+#define SDHC_NIS_TRSCMP            0x0002
+#define SDHC_NORINTSTSEN           0x34
+#define SDHC_ERRINTSTSEN           0x36
+#define SDHC_ADMASYSADDR           0x58
+#define SDHC_ADMA_ATTR_ACT_TRAN    (1 << 5)
+#define SDHC_ADMA_ATTR_END         (1 << 1)
+#define SDHC_ADMA_ATTR_VALID       (1 << 0)
+#define SDHC_TRANSFER_DELAY        100
+/* NDESC stays above SDHC_ADMA_DESCS_PER_DELAY (5). */
+
+/*
+ * Probe both AM64x SDHCI instances.  The first free sd-bus gets the
+ * test card, so the base with CARD_PRESENT depends from realize order.
+ */
+#define SDHCI_SD_BASE     0x0fa00000ULL
+#define SDHCI_EMMC_BASE   0x0fa10000ULL
+
+/* Scratch DDR: ADMA descriptor table plus target buffer. */
+#define ADMA_TABLE_ADDR   0x82000000ULL
+#define ADMA_BUF_ADDR     0x82100000ULL
+
+#define BLK_LEN           512
+#define NDESC             12      /* > SDHC_ADMA_DESCS_PER_DELAY (5) */
+
+/* SDHCI command-register response-type encodings (CMDREG bits [1:0]). */
+#define RESP_NONE         0x0000
+#define RESP_R2           0x0001  /* 136-bit (CID/CSD) */
+#define RESP_R48          0x0002  /* 48-bit  (R1/R3/R6/R7) */
+#define RESP_R1B          0x0003  /* 48-bit with busy (R1b) */
+
+/* ACMD41 argument: HCS (bit30) + a 3.3-3.6V voltage window (non-enquiry). */
+#define ACMD41_ARG        0x40FF8000u
+/* CMD8 argument: VHS = 2.7-3.6V + recommended check pattern 0xAA. */
+#define CMD8_ARG          0x000001AAu
+
+/* Bounded command-complete poll; returns actual latched NORINTSTS. */
+static uint16_t sd_cmd(QTestState *qts, uint64_t base, uint8_t cmd,
+                       uint32_t arg, uint16_t flags)
+{
+    uint16_t sts = 0;
+    int i;
+
+    qtest_writel(qts, base + SDHC_ARGUMENT, arg);
+    qtest_writew(qts, base + SDHC_CMDREG, ((uint16_t)cmd << 8) | flags);
+
+    /* CMDREG write runs synchronously sdhci_send_command(). */
+    for (i = 0; i < 1000; i++) {
+        sts = qtest_readw(qts, base + SDHC_NORINTSTS);
+        if (sts & SDHC_NIS_CMDCMP) {
+            break;
+        }
+    }
+    g_assert_cmphex(sts & SDHC_NIS_CMDCMP, ==, SDHC_NIS_CMDCMP);
+
+    /* Clear all latched normal-interrupt status bits (write-1-to-clear). */
+    qtest_writew(qts, base + SDHC_NORINTSTS, 0xffff);
+    return sts;
+}
+
+/* Return the controller base whose PRNSTS reports a card inserted. */
+static uint64_t find_card_base(QTestState *qts)
+{
+    uint32_t sd_sts = qtest_readl(qts, SDHCI_SD_BASE + SDHC_PRNSTS);
+    uint32_t emmc_sts = qtest_readl(qts, SDHCI_EMMC_BASE + SDHC_PRNSTS);
+
+    if (sd_sts & SDHC_CARD_PRESENT) {
+        return SDHCI_SD_BASE;
+    }
+    if (emmc_sts & SDHC_CARD_PRESENT) {
+        return SDHCI_EMMC_BASE;
+    }
+    g_assert_not_reached();
+}
+
+/* Bring SD card to transfer state. */
+static void sd_bring_up_card(QTestState *qts, uint64_t base)
+{
+    uint32_t rca;
+
+    /* Host reset, clock on (INT_EN sets INT_STABLE), latch all status. */
+    qtest_writeb(qts, base + SDHC_SWRST, SDHC_RESET_ALL);
+    qtest_writew(qts, base + SDHC_CLKCON,
+                 SDHC_CLOCK_INT_EN | SDHC_CLOCK_SDCLK_EN);
+    qtest_writew(qts, base + SDHC_NORINTSTSEN, 0xffff);
+    qtest_writew(qts, base + SDHC_ERRINTSTSEN, 0xffff);
+
+    sd_cmd(qts, base, 0,  0x00000000, RESP_NONE);  /* CMD0  GO_IDLE_STATE   */
+    sd_cmd(qts, base, 8,  CMD8_ARG,   RESP_R48);    /* CMD8  SEND_IF_COND    */
+
+    /* ACMD41: CMD55 (APP_CMD) then CMD41 (SEND_OP_COND), until powered up. */
+    for (int i = 0; i < 100; i++) {
+        sd_cmd(qts, base, 55, 0x00000000, RESP_R48);
+        sd_cmd(qts, base, 41, ACMD41_ARG, RESP_R48);
+        /* Non-enquiry ACMD41 powers up the model; the loop is a guard. */
+        break;
+    }
+
+    sd_cmd(qts, base, 2, 0x00000000, RESP_R2);     /* CMD2  ALL_SEND_CID    */
+    sd_cmd(qts, base, 3, 0x00000000, RESP_R48);    /* CMD3  SEND_RELATIVE   */
+
+    /* R6 packs the assigned RCA in the upper 16 bits of RSPREG0. */
+    rca = qtest_readl(qts, base + SDHC_RSPREG0) >> 16;
+
+    sd_cmd(qts, base, 7, rca << 16, RESP_R1B);     /* CMD7  SELECT_CARD     */
+    sd_cmd(qts, base, 16, BLK_LEN,  RESP_R48);     /* CMD16 SET_BLOCKLEN    */
+}
+
+/*
+ * 32-bit ADMA2 entries are little-endian 64-bit words: attr in bits
+ * [6:0], length in [31:16], address in [63:32].  No INT bit is set,
+ * so we expect the whole chain in one call.
+ */
+static void build_adma_table(QTestState *qts)
+{
+    for (int i = 0; i < NDESC; i++) {
+        uint8_t attr = SDHC_ADMA_ATTR_VALID | SDHC_ADMA_ATTR_ACT_TRAN;
+        uint32_t addr = (uint32_t)(ADMA_BUF_ADDR + (uint64_t)i * BLK_LEN);
+
+        if (i == NDESC - 1) {
+            attr |= SDHC_ADMA_ATTR_END;
+        }
+        qtest_writel(qts, ADMA_TABLE_ADDR + (uint64_t)i * 8,
+                     ((uint32_t)BLK_LEN << 16) | attr);
+        qtest_writel(qts, ADMA_TABLE_ADDR + (uint64_t)i * 8 + 4, addr);
+    }
+}
+
+static void test_adma_pacing(void)
+{
+    char *tmp = NULL;
+    int fd;
+    GError *err = NULL;
+    uint64_t base;
+    QTestState *qts;
+    uint16_t sts;
+    int steps = 0;
+
+    /* 1 MiB raw backing file, filled with zero. */
+    fd = g_file_open_tmp("am64-adma-XXXXXX.raw", &tmp, &err);
+    g_assert_no_error(err);
+    g_assert_cmpint(fd, >=, 0);
+    g_assert_cmpint(ftruncate(fd, 1 * 1024 * 1024), ==, 0);
+    close(fd);
+
+    qts = qtest_initf("-machine am64-virt -accel qtest -display none "
+                      "-drive if=none,id=sd0,file=%s,format=raw "
+                      "-device sd-card,drive=sd0", tmp);
+
+    base = find_card_base(qts);
+    sd_bring_up_card(qts, base);
+
+    /* Select ADMA2 32-bit as DMA engine (Host Control 1 bits [4:3]). */
+    qtest_writeb(qts, base + SDHC_HOSTCTL, SDHC_CTRL_ADMA2_32);
+
+    build_adma_table(qts);
+
+    /* Program descriptor/geometry and clear old status. */
+    qtest_writel(qts, base + SDHC_ADMASYSADDR, (uint32_t)ADMA_TABLE_ADDR);
+    qtest_writel(qts, base + SDHC_ADMASYSADDR + 4,
+                 (uint32_t)(ADMA_TABLE_ADDR >> 32));
+    qtest_writel(qts, base + SDHC_BLKSIZE, BLK_LEN | (NDESC << 16));
+    qtest_writew(qts, base + SDHC_TRNMOD,
+                 SDHC_TRNS_DMA | SDHC_TRNS_BLK_CNT_EN |
+                 SDHC_TRNS_READ | SDHC_TRNS_MULTI);
+    qtest_writel(qts, base + SDHC_ARGUMENT, 0);  /* start block 0 */
+    qtest_writew(qts, base + SDHC_NORINTSTS, 0xffff);
+
+    /* CMD18 READ_MULTIPLE_BLOCK kicks off ADMA. */
+    qtest_writew(qts, base + SDHC_CMDREG,
+                 (18 << 8) | SDHC_CMD_DATA_PRESENT | RESP_R48);
+
+    /*
+     * Transfer Complete has to be already set.  A batched 12-descriptor
+     * chain needs at least one SDHC_TRANSFER_DELAY step.
+     */
+    while (!((sts = qtest_readw(qts, base + SDHC_NORINTSTS)) &
+             SDHC_NIS_TRSCMP)) {
+        qtest_clock_step(qts, SDHC_TRANSFER_DELAY);
+        steps++;
+        g_assert_cmpint(steps, <, 1000);
+    }
+
+    /* Completion has to be Transfer Complete with no error interrupt. */
+    g_assert_cmphex(sts & SDHC_NIS_TRSCMP, ==, SDHC_NIS_TRSCMP);
+    g_assert_cmphex(sts & SDHC_NIS_ERR, ==, 0);
+
+    g_test_message("ADMA %d descriptors: %d clock steps", NDESC, steps);
+
+    /* The fixed path completes whole chain in one pass: no clock steps. */
+    g_assert_cmpint(steps, ==, 0);
+
+    qtest_quit(qts);
+    unlink(tmp);
+    g_free(tmp);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64/sdhci/adma-pacing", test_adma_pacing);
+    return g_test_run();
+}
tests/qtest/am64-virt-test.c new file +502 -0
new file mode 100644
index 0000000000..c0a9a26275
@@ -0,0 +1,502 @@
+/*
+ * QTests for AM64 virt machine
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "qobject/qdict.h"
+#include "qobject/qlist.h"
+
+#define OCSRAM_BASE 0x70000000ULL
+#define OCSRAM_SIZE (2 * 1024 * 1024)
+#define MAIN_UART0_BASE 0x02800000ULL
+
+static void test_ocsram_rw(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    qtest_writel(qts, OCSRAM_BASE, 0xdeadbeef);
+    g_assert_cmphex(qtest_readl(qts, OCSRAM_BASE), ==, 0xdeadbeef);
+    qtest_writel(qts, OCSRAM_BASE + OCSRAM_SIZE - 4, 0x12345678);
+    g_assert_cmphex(qtest_readl(qts, OCSRAM_BASE + OCSRAM_SIZE - 4), ==,
+                    0x12345678);
+    /* Boot parameters must stay in OCSRAM. */
+    qtest_writel(qts, 0x701bebfc, 0x0);
+    g_assert_cmphex(qtest_readl(qts, 0x701bebfc), ==, 0x0);
+    qtest_quit(qts);
+}
+
+static void test_main_uart0_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* Idle 16550: transmitter empty bits are set. */
+    g_assert_cmphex(qtest_readl(qts, MAIN_UART0_BASE + (5 << 2)) & 0x60,
+                    ==, 0x60);
+    qtest_quit(qts);
+}
+
+static void test_r5f_cpu_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    QDict *resp = qtest_qmp(qts, "{'execute': 'query-cpus-fast'}");
+    QList *cpus = qdict_get_qlist(resp, "return");
+
+    /* 2x A53 + 1x M4 + 1x R5F */
+    g_assert_cmpint(qlist_size(cpus), ==, 4);
+    qobject_unref(resp);
+    qtest_quit(qts);
+}
+
+static void test_devstat(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* CTRLMMR_MAIN_DEVSTAT: primary bootmode = eMMC (0x9 << 3). */
+    g_assert_cmphex(qtest_readl(qts, 0x43000030), ==, 0x48);
+    /* mmr_unlock() kick writes have to be accepted. */
+    qtest_writel(qts, 0x43008008, 0x68ef3490);
+    qtest_writel(qts, 0x4300800c, 0xd172bc5a);
+    qtest_quit(qts);
+}
+
+#define DDRSS_CFG_BASE    0x0f308000ULL
+#define SP_TARGET(thread) (0x4D000000ULL + (thread) * 0x1000)
+#define SP_RT(thread)     (0x4A600000ULL + (thread) * 0x1000)
+
+static void test_dmsc_r5_version(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    /*
+     * R5 secure-host TISCI VERSION request.  The zero secure prefix
+     * has to be skipped before parsing the TISCI header.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230002);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    /* Commit with the last data word. */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* Response has to land on RX thread 0. */
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmpuint(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    /* Secure prefix, echoed VERSION type, ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x0002);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+static void test_dmsc_r5_get_freq(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * R5 secure-host GET_FREQ (0x010e) for MMCSD0 clock 1.  This
+     * controls packed request layout and nonzero frequency response.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23010e);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 57);
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 1);
+    /* commit by writing the last data word */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* response has to land on RX thread 0 (message count > 0) */
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmpuint(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    /* Secure prefix, echoed GET_FREQ type, ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x010e);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    /* freq_hz follows the 8-byte TISCI header and has to be nonzero. */
+    g_assert_cmpuint(qtest_readl(qts, SP_TARGET(0) + 0x10), !=, 0);
+    qtest_quit(qts);
+}
+
+/*
+ * Requests without TISCI_MSG_FLAG_AOP must get no reply.  Else a stale
+ * message in the single-slot RX thread breaks later request/response
+ * pairing, so keep RX count at zero for both no-response messages.
+ */
+static void test_dmsc_r5_no_response_flag(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * Drain the reset-time boot notification.  Reading register 15 clears
+     * the inbound thread message count again.
+     */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /* WAIT_PROC_BOOT_STATUS (0xc401), hdr.flags = 0: no response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c401);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    g_usleep(50 * 1000);
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, ==, 0);
+
+    /* SET_DEVICE (0x0200), hdr.flags = 0: no response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230200);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 121);   /* device id */
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0);     /* state off */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    g_usleep(50 * 1000);
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, ==, 0);
+
+    /* AOP messages still get a response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230002);   /* VERSION */
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+    qtest_quit(qts);
+}
+
+/*
+ * TISCI_MSG_SYS_RESET (0x0005) is a no-response request, but it still
+ * triggers a full machine reset.
+ */
+static void test_dmsc_r5_sys_reset(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* Drain the reset-time boot notification. */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /* SYS_RESET from host 35, with secure prefix and hdr.flags = 0. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230005);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    /* commit: write the last data word */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* The DMSC reset request appears as QEMU RESET event. */
+    qtest_qmp_eventwait(qts, "RESET");
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_counts(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /* TCLR.ST is safe also when the model free-runs. */
+    qtest_writel(qts, 0x02400038, 1);
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000); /* +1 ms */
+    t1 = qtest_readl(qts, 0x0240003c);
+    /* 20 MHz -> 1 ms = 20000 ticks */
+    g_assert_cmpuint(t1 - t0, ==, 20000);
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_prescaler(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /*
+     * PTV=2 with PRE_EN gives 20 MHz / (2 << 2), i.e. 1 ms is
+     * 2500 ticks.
+     */
+    qtest_writel(qts, 0x02400038, 0x2b);
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000); /* +1 ms virtual time */
+    t1 = qtest_readl(qts, 0x0240003c);
+    g_assert_cmpuint(t1 - t0, ==, 2500);
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_reconfigure(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /*
+     * Changing the prescaler while the timer runs must only affect time
+     * after the TCLR write, never rescale already-elapsed ticks.
+     */
+    qtest_writel(qts, 0x02400038, 1);           /* ST, no prescaler */
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000);             /* +1 ms @ 20 MHz  */
+    qtest_writel(qts, 0x02400038, 0x2b);        /* PTV=2, PRE_EN, AR, ST */
+    qtest_clock_step(qts, 1000000);             /* +1 ms @ 2.5 MHz */
+    t1 = qtest_readl(qts, 0x0240003c);
+    g_assert_cmpuint(t1 - t0, ==, 20000 + 2500);
+    qtest_quit(qts);
+}
+
+#define GICD_BASE 0x01800000ULL
+#define GICR_BASE 0x01840000ULL
+#define GIC_PIDR2 0xffe8
+
+static void test_gicv3_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* GICD_PIDR2.ArchRev must report a GICv3 distributor. */
+    g_assert_cmphex((qtest_readl(qts, GICD_BASE + GIC_PIDR2) >> 4) & 0xf,
+                    ==, 3);
+    /* first redistributor frame at actual AM64x GICR base */
+    g_assert_cmphex((qtest_readl(qts, GICR_BASE + GIC_PIDR2) >> 4) & 0xf,
+                    ==, 3);
+    qtest_quit(qts);
+}
+
+static void test_ddrss_stub(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* DENALI_CTL_0 writes must persist, including dram_class DDR4. */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x0, 0x00000A00);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x0), ==, 0x00000A00);
+
+    /* Done bits are ORed into status reads, also after writes. */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x214C, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x214C) & 0x1, ==, 0x1);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x538, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x538) & (1u << 13),
+                    ==, 1u << 13);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x558, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x558) & (1u << 25),
+                    ==, 1u << 25);
+
+    /*
+     * ECC priming needs both BIST_DONE latches: INT_STATUS_MASTER bit 8
+     * and INT_STATUS_BIST bit 0, resp. CTL_341 raw bit 16.
+     */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x538, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x538) & (1u << 8),
+                    ==, 1u << 8);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x554, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x554) & (1u << 16),
+                    ==, 1u << 16);
+    qtest_quit(qts);
+}
+
+/*
+ * SET_CONFIG (0xc100) records the A53 bootvector, and PROC_GET_STATUS
+ * (0xc400) must echo it.  The packed payload has processor_id at byte 0
+ * and bootvector_low at bytes 1..4, so 0x701c0000 is written as
+ * 0x1c000020 / 0x00000070.
+ */
+static void test_dmsc_r5_bootvector_capture(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t reg4, reg5, bootvector_lo;
+
+    /* drain boot notification pre-queued on thread 0 at reset */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);      /* sec hdr */
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c100);      /* hdr */
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);      /* AOP */
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 0x1c000020);      /* id+bv */
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0x00000070);
+    qtest_writel(qts, SP_TARGET(1) + 0x18, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x1c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100 && !(qtest_readl(qts, SP_RT(0)) & 0xff); i++) {
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_readl(qts, SP_TARGET(0) + 0x3c);                    /* drain */
+
+    /* PROC_GET_STATUS (0xc400), proc 32: bootvector_low has to echo. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c400);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 32);               /* proc_id */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100 && !(qtest_readl(qts, SP_RT(0)) & 0xff); i++) {
+        g_usleep(10 * 1000);
+    }
+
+    /*
+     * Secure responses carry a zero prefix at +0x04.  processor_id is
+     * response byte 8, so bootvector_lo starts one byte into register 4
+     * and has to be rebuilt from registers 4 and 5.
+     */
+    reg4 = qtest_readl(qts, SP_TARGET(0) + 0x10);
+    reg5 = qtest_readl(qts, SP_TARGET(0) + 0x14);
+    bootvector_lo = (reg4 >> 8) | ((reg5 & 0xff) << 24);
+    g_assert_cmphex(bootvector_lo, ==, 0x701c0000);
+    qtest_quit(qts);
+}
+
+#define SDHCI_SD_BASE   0x0fa00000ULL
+#define SDHCI_EMMC_BASE 0x0fa10000ULL
+
+static void test_sdhci_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * CAPAB bit 28 advertises 64-bit system-bus support, which is needed
+     * for A53 SPL ADMA2-64 transfers.
+     */
+    g_assert_cmphex(qtest_readl(qts, SDHCI_SD_BASE + 0x40), ==, 0x157c34b4);
+    g_assert_cmphex(qtest_readl(qts, SDHCI_EMMC_BASE + 0x40), ==, 0x157c34b4);
+    /* Host controller version (0xFE): SDHCI spec 3.00. */
+    g_assert_cmphex(qtest_readw(qts, SDHCI_SD_BASE + 0xFE) & 0xff, ==, 2);
+    /* PHY window: PHY_STAT1 reads CALDONE|DLLRDY */
+    g_assert_cmphex(qtest_readl(qts, 0x0fa08000ULL + 0x130) & 0x3, ==, 0x3);
+    g_assert_cmphex(qtest_readl(qts, 0x0fa18000ULL + 0x130) & 0x3, ==, 0x3);
+    qtest_quit(qts);
+}
+
+#define TRNG_BASE 0x40910000ULL
+
+static void test_trng_stub(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* readiness bit is permanently set */
+    g_assert_cmphex(qtest_readl(qts, TRNG_BASE + 0x10) & 0x1, ==, 0x1);
+    /* Output words must be nonzero and change between reads. */
+    uint32_t a = qtest_readl(qts, TRNG_BASE + 0x00);
+    uint32_t b = qtest_readl(qts, TRNG_BASE + 0x00);
+
+    g_assert_cmpuint(a, !=, 0);
+    g_assert_true(a != b || qtest_readl(qts, TRNG_BASE + 0x04) != a);
+    /* INTACK writes are accepted. */
+    qtest_writel(qts, TRNG_BASE + 0x10, 0x1);
+    /* CONTROL is RAM-backed: read back actual written value */
+    qtest_writel(qts, TRNG_BASE + 0x14, 0x400);
+    g_assert_cmphex(qtest_readl(qts, TRNG_BASE + 0x14), ==, 0x400);
+    qtest_quit(qts);
+}
+
+/*
+ * A53_0 uses threads 9/8 with the same secure prefix as the R5 secure
+ * pair.  If it is classified non-secure, the request header shifts and
+ * response prefix is missing, leaving BL31 waiting forever.
+ */
+static void test_dmsc_a53_secure_version(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    if (qtest_readl(qts, SP_RT(8)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(8) + 0x3c);
+    }
+
+    /*
+     * A53_0 secure-host VERSION request: zero secure prefix, host 10,
+     * and AOP is set.
+     */
+    qtest_writel(qts, SP_TARGET(9) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(9) + 0x08, 0x010a0002);
+    qtest_writel(qts, SP_TARGET(9) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(9) + 0x3c, 0x00000000);
+
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(8)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(8)) & 0xff, >, 0);
+
+    /* VERSION type echoed and ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(8) + 0x08) & 0xffff,
+                    ==, 0x0002);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(8) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+/*
+ * TISCI_MSG_FWL_SET (0x9000) is a bare-ACK request.  Without handler
+ * it falls through to the unknown-message NAK path.
+ */
+static void test_dmsc_fwl_set_ack(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /*
+     * R5 secure FWL_SET: type 0x9000, host 35, AOP set, fwl_id 0x23,
+     * region 3, one permission register.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0b239000);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 0x00030023);
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0x00000001);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x9000);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64-virt/ocsram", test_ocsram_rw);
+    qtest_add_func("/am64-virt/main-uart0", test_main_uart0_present);
+    qtest_add_func("/am64-virt/r5f-present", test_r5f_cpu_present);
+    qtest_add_func("/am64-virt/devstat", test_devstat);
+    qtest_add_func("/am64-virt/dmsc-r5-version", test_dmsc_r5_version);
+    qtest_add_func("/am64-virt/dmsc-r5-get-freq", test_dmsc_r5_get_freq);
+    qtest_add_func("/am64-virt/dmsc-no-response",
+                   test_dmsc_r5_no_response_flag);
+    qtest_add_func("/am64-virt/dmsc-sys-reset", test_dmsc_r5_sys_reset);
+    qtest_add_func("/am64-virt/dmsc-bootvector",
+                   test_dmsc_r5_bootvector_capture);
+    qtest_add_func("/am64-virt/dmtimer", test_dmtimer_counts);
+    qtest_add_func("/am64-virt/dmtimer-prescaler", test_dmtimer_prescaler);
+    qtest_add_func("/am64-virt/dmtimer-reconfigure", test_dmtimer_reconfigure);
+    qtest_add_func("/am64-virt/gicv3", test_gicv3_present);
+    qtest_add_func("/am64-virt/ddrss-stub", test_ddrss_stub);
+    qtest_add_func("/am64-virt/sdhci", test_sdhci_present);
+    qtest_add_func("/am64-virt/trng", test_trng_stub);
+    qtest_add_func("/am64-virt/dmsc-a53-secure-version",
+                   test_dmsc_a53_secure_version);
+    qtest_add_func("/am64-virt/dmsc-fwl-set", test_dmsc_fwl_set_ack);
+    return g_test_run();
+}
tests/qtest/meson.build +1 -0
index f7c7d06620..d915fb4199 100644
@@ -274,6 +274,7 @@ qtests_aarch64 = \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_ARM_VIRT') ?
    ['iommu-smmuv3-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_AM64_VIRT') ? ['am64-virt-test', 'ti-am64-i2c-test', 'am64-adma-pacing-test'] : []) + \
   qtests_cxl +                                                                                  \
   ['arm-cpu-features',
    'numa-test',
tests/qtest/ti-am64-i2c-test.c new file +137 -0
new file mode 100644
index 0000000000..eda8d8032f
@@ -0,0 +1,137 @@
+/*
+ * QTests for AM64x main_i2c0 controller
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/* main_i2c0 window: i2c@20000000, len 0x100. */
+#define I2C0_BASE 0x20000000ULL
+
+/* OMAP I2C V2 register offsets. */
+#define I2C_V2_SYSC          0x10
+#define I2C_V2_IRQSTATUS_RAW 0x24
+#define I2C_V2_IRQSTATUS     0x28
+#define I2C_V2_SYSS          0x90
+#define I2C_V2_CNT           0x98
+#define I2C_V2_DATA          0x9c
+#define I2C_V2_CON           0xa4
+#define I2C_V2_SA            0xac
+
+/* Register bits. */
+#define I2C_SYSC_SRST   (1 << 1)  /* SYSCONFIG soft reset */
+#define I2C_SYSS_RDONE  (1 << 0)  /* reset done */
+#define I2C_STAT_NACK   (1 << 1)  /* no acknowledgement */
+#define I2C_STAT_ARDY   (1 << 2)  /* register access ready */
+#define I2C_STAT_RRDY   (1 << 3)  /* receive data ready */
+
+#define I2C_CON_EN  (1 << 15)     /* module enable */
+#define I2C_CON_MST (1 << 10)     /* master mode */
+#define I2C_CON_STP (1 << 1)      /* stop condition */
+#define I2C_CON_STT (1 << 0)      /* start condition */
+
+/*
+ * SYSCONFIG.SRST must make SYSS.RDONE assert, or the firmware spins in
+ * the soft-reset poll.
+ */
+static void test_soft_reset_completes(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t syss = 0;
+
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+
+    for (int i = 0; i < 16; i++) {
+        syss = qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+        if (syss & I2C_SYSS_RDONE) {
+            break;
+        }
+    }
+    g_assert_cmphex(syss & I2C_SYSS_RDONE, ==, I2C_SYSS_RDONE);
+    qtest_quit(qts);
+}
+
+/*
+ * Reads from an address without a slave must raise NACK promptly, since a
+ * driver would otherwise wait forever for an I2C event. No slave is
+ * attached here, so any address does.
+ */
+static void test_nack_on_absent_slave(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t stat;
+
+    /* Bring the controller out of reset as firmware does. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+    (void)qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+
+    /* Address 0x51 has no slave: one-byte master read. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SA, 0x51);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CNT, 1);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CON,
+                 I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
+
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, I2C_STAT_NACK);
+
+    /* IRQSTATUS mirrors NACK and is write-1-to-clear. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, I2C_STAT_NACK);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_IRQSTATUS, I2C_STAT_NACK);
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, 0);
+
+    qtest_quit(qts);
+}
+
+/*
+ * With a slave on the bus a one-byte master read has to ACK, return DATA
+ * and then finish with ARDY. An at24c EEPROM at 0x50 serves as the slave;
+ * it comes up erased, so offset 0 reads back as 0x00.
+ */
+static void test_eeprom_read_first_byte(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt "
+                                 "-device at24c-eeprom,address=0x50,"
+                                 "rom-size=4096");
+    uint32_t stat;
+    uint8_t b;
+
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+    (void)qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+
+    /* One-byte read from 0x50; address pointer starts by 0. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SA, 0x50);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CNT, 1);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CON,
+                 I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
+
+    /* Slave ACKed and data are ready. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, 0);
+    g_assert_cmphex(stat & I2C_STAT_RRDY, ==, I2C_STAT_RRDY);
+
+    /* Offset 0 of the erased EEPROM reads as 0x00. */
+    b = qtest_readw(qts, I2C0_BASE + I2C_V2_DATA) & 0xff;
+    g_assert_cmphex(b, ==, 0x00);
+
+    /* Single-byte transfer complete: ARDY, no more RRDY. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_ARDY, ==, I2C_STAT_ARDY);
+    g_assert_cmphex(stat & I2C_STAT_RRDY, ==, 0);
+
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64/i2c/soft-reset", test_soft_reset_completes);
+    qtest_add_func("/am64/i2c/nack", test_nack_on_absent_slave);
+    qtest_add_func("/am64/i2c/eeprom-read", test_eeprom_read_first_byte);
+    return g_test_run();
+}
tests/unit/meson.build +1 -0
index dc3fb954c0..be2aab4274 100644
@@ -41,6 +41,7 @@ tests = {
   'test-bitops': [],
   'test-bitcnt': [],
   'test-qgraph': ['../qtest/libqos/qgraph.c'],
+  'test-k3-bootrom': ['../../hw/arm/k3-bootrom-parse.c'],
   'check-qom-interface': [qom],
   'check-qom-proplist': [qom],
   'test-qemu-opts': [],
tests/unit/test-k3-bootrom.c new file +263 -0
new file mode 100644
index 0000000000..b030d7df9b
@@ -0,0 +1,263 @@
+/*
+ * Unit tests for K3 boot-ROM combined-image parser
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/arm/k3-bootrom.h"
+
+/* Minimal DER emitters. */
+
+static GByteArray *der_tlv(uint8_t tag, const uint8_t *data, size_t len)
+{
+    GByteArray *a = g_byte_array_new();
+
+    g_byte_array_append(a, &tag, 1);
+    if (len < 0x80) {
+        uint8_t l = len;
+        g_byte_array_append(a, &l, 1);
+    } else if (len <= 0xffff) {
+        uint8_t l[3] = { 0x82, len >> 8, len & 0xff };
+        g_byte_array_append(a, l, 3);
+    } else {
+        g_assert_not_reached();
+    }
+    if (data) {
+        g_byte_array_append(a, data, len);
+    }
+    return a;
+}
+
+static GByteArray *der_wrap(uint8_t tag, GByteArray *inner)
+{
+    GByteArray *a = der_tlv(tag, inner->data, inner->len);
+    g_byte_array_unref(inner);
+    return a;
+}
+
+static void der_append(GByteArray *dst, GByteArray *src)
+{
+    g_byte_array_append(dst, src->data, src->len);
+    g_byte_array_unref(src);
+}
+
+static GByteArray *der_uint(uint64_t v)
+{
+    uint8_t buf[9];
+    int n = 0;
+    uint64_t t = v;
+
+    do {
+        n++;
+        t >>= 8;
+    } while (t);
+    if (v >> (n * 8 - 1) & 1) {
+        n++; /* leading zero keeps it positive */
+    }
+    for (int i = 0; i < n; i++) {
+        buf[i] = v >> ((n - 1 - i) * 8);
+    }
+    return der_tlv(0x02, buf, n);
+}
+
+/* OID 1.3.6.1.4.1.294.1.9 (ext_boot_info), pre-encoded TLV */
+static const uint8_t ext_boot_oid[] = {
+    0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x26, 0x01, 0x09
+};
+/* OID 2.16.840.1.101.3.4.2.3 (sha512), pre-encoded TLV */
+static const uint8_t sha512_oid[] = {
+    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static GByteArray *der_component(uint32_t ctype, uint32_t core,
+                                 uint32_t opts, uint32_t dest, uint64_t size)
+{
+    GByteArray *seq = g_byte_array_new();
+    uint8_t dest_be[4] = { dest >> 24, dest >> 16, dest >> 8, dest };
+    uint8_t sha[64] = { 0 };
+
+    der_append(seq, der_uint(ctype));
+    der_append(seq, der_uint(core));
+    der_append(seq, der_uint(opts));
+    der_append(seq, der_tlv(0x04, dest_be, sizeof(dest_be)));
+    der_append(seq, der_uint(size));
+    g_byte_array_append(seq, sha512_oid, sizeof(sha512_oid));
+    der_append(seq, der_tlv(0x04, sha, sizeof(sha)));
+    return der_wrap(0x30, seq);
+}
+
+/*
+ * Layout follows u-boot tools/binman/btool/openssl.py
+ * x509_cert_rom_combined(): SBL, SYSFW, SYSFW-DATA payloads are after
+ * the certificate.
+ */
+static GByteArray *make_image(const uint8_t *sbl, size_t sbl_len)
+{
+    static const uint8_t sysfw_blob[16] = "SYSFW-payload";
+    static const uint8_t cfg_blob[8] = "BCFG";
+    GByteArray *info = g_byte_array_new();
+    GByteArray *ext, *cert, *img;
+
+    der_append(info, der_uint(sbl_len + sizeof(sysfw_blob)
+                              + sizeof(cfg_blob)));      /* extImgSize */
+    der_append(info, der_uint(3));                       /* numComp */
+    der_append(info, der_component(K3_COMP_TYPE_SBL, 16, 0,
+                                   0x70000000, sbl_len));
+    der_append(info, der_component(K3_COMP_TYPE_SYSFW, 0, 0,
+                                   0x44000, sizeof(sysfw_blob)));
+    der_append(info, der_component(K3_COMP_TYPE_SYSFW_DATA, 0, 0,
+                                   0x7b000, sizeof(cfg_blob)));
+    info = der_wrap(0x30, info);
+
+    /* extension is SEQ { OID, OCTETSTRING { info } } */
+    ext = g_byte_array_new();
+    g_byte_array_append(ext, ext_boot_oid, sizeof(ext_boot_oid));
+    der_append(ext, der_wrap(0x04, info));
+    ext = der_wrap(0x30, ext);
+
+    /* fake cert: top-level SEQUENCE around the extension */
+    cert = der_wrap(0x30, ext);
+
+    img = g_byte_array_new();
+    g_byte_array_append(img, cert->data, cert->len);
+    g_byte_array_unref(cert);
+    g_byte_array_append(img, sbl, sbl_len);
+    g_byte_array_append(img, sysfw_blob, sizeof(sysfw_blob));
+    g_byte_array_append(img, cfg_blob, sizeof(cfg_blob));
+    return img;
+}
+
+/* Certificate-only image with caller given component sizes. */
+static GByteArray *make_cert_with_sizes(const uint64_t *sizes, size_t n)
+{
+    GByteArray *info = g_byte_array_new();
+    GByteArray *ext;
+    uint64_t total = 0;
+
+    for (size_t i = 0; i < n; i++) {
+        total += sizes[i];
+    }
+    der_append(info, der_uint(total));                   /* extImgSize */
+    der_append(info, der_uint(n));                       /* numComp */
+    for (size_t i = 0; i < n; i++) {
+        der_append(info, der_component(K3_COMP_TYPE_SBL, 16, 0,
+                                       0x70000000, sizes[i]));
+    }
+    info = der_wrap(0x30, info);
+
+    ext = g_byte_array_new();
+    g_byte_array_append(ext, ext_boot_oid, sizeof(ext_boot_oid));
+    der_append(ext, der_wrap(0x04, info));
+    ext = der_wrap(0x30, ext);
+    return der_wrap(0x30, ext);
+}
+
+static void test_parse_ok(void)
+{
+    static const uint8_t sbl[32] = "SBL-payload";
+    GByteArray *img = make_image(sbl, sizeof(sbl));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_true(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_null(err);
+    g_assert_cmpuint(out.num_comps, ==, 3);
+    g_assert_cmpuint(out.comps[0].comp_type, ==, K3_COMP_TYPE_SBL);
+    g_assert_cmphex(out.comps[0].dest_addr, ==, 0x70000000);
+    g_assert_cmpuint(out.comps[0].comp_size, ==, sizeof(sbl));
+    g_assert_cmpuint(out.comps[0].payload_offset, ==, out.cert_len);
+    g_assert_cmphex(out.comps[1].dest_addr, ==, 0x44000);
+    g_assert_cmpuint(out.comps[2].payload_offset, ==,
+                     out.cert_len + sizeof(sbl) + 16);
+    g_assert_cmpint(memcmp(img->data + out.comps[0].payload_offset,
+                           sbl, sizeof(sbl)), ==, 0);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_not_der(void)
+{
+    static const uint8_t junk[64] = { 0xff, 0x00, 0x41 };
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(junk, sizeof(junk), &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+}
+
+static void test_parse_no_extension(void)
+{
+    /* Valid DER SEQUENCE, but no ext_boot_info OID. */
+    static const uint8_t seq[] = { 0x30, 0x03, 0x02, 0x01, 0x05 };
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(seq, sizeof(seq), &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+}
+
+static void test_parse_truncated_payload(void)
+{
+    static const uint8_t sbl[32] = "SBL-payload";
+    GByteArray *img = make_image(sbl, sizeof(sbl));
+    K3BootImage out;
+    Error *err = NULL;
+
+    /* Truncated payload: comp_size claims are beyond file. */
+    g_assert_false(k3_bootrom_parse(img->data, img->len - 20, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_size_exceeds_u32(void)
+{
+    /* comp_size wider than 32 bits must not truncate silently. */
+    static const uint64_t sizes[1] = { UINT32_MAX + 1ull };
+    GByteArray *img = make_cert_with_sizes(sizes, G_N_ELEMENTS(sizes));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_size_sum_wraps_32bit(void)
+{
+    /*
+     * Each single size fits in 32 bits, but their sum must not wrap the
+     * running total.
+     */
+    static const uint64_t sizes[3] = {
+        0xf0000000, 0xf0000000, 0xf0000000
+    };
+    GByteArray *img = make_cert_with_sizes(sizes, G_N_ELEMENTS(sizes));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    g_test_add_func("/k3-bootrom/parse-ok", test_parse_ok);
+    g_test_add_func("/k3-bootrom/not-der", test_parse_not_der);
+    g_test_add_func("/k3-bootrom/no-extension", test_parse_no_extension);
+    g_test_add_func("/k3-bootrom/truncated", test_parse_truncated_payload);
+    g_test_add_func("/k3-bootrom/size-exceeds-u32",
+                    test_parse_size_exceeds_u32);
+    g_test_add_func("/k3-bootrom/size-sum-wraps-32bit",
+                    test_parse_size_sum_wraps_32bit);
+    return g_test_run();
+}
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#20 lore raw
In reply to Wadim Mueller
Quoted 18 lines · Wadim Mueller

Add support for the TI AM64x (Sitara AM6442) SoC and a board around it, ``am64-virt``. The model is complete enough to run the stock TI boot chain end to end:

ROM boot (tiboot3.bin) -> R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux SMP

This is a resend: I posted a first RFC back in May [1] but got no feedback on it. Since then was the series rebased onto actual master, reworked for style and split into reviewable pieces, so I send it out again in the hope of getting some review.

RFC because I would like feedback on the overall approach before polishing further; see the open questions at the end.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

What is modelled
================

<snip>

Quoted 20 lines

Open questions
==============

* pc-bios/dtb/am64-virt.dtb: patch 13 checks in a generated DTB (with its source) for convenience. I am not sure a machine-specific blob belongs in pc-bios; happy to drop it and have the functional test generate it with dtc instead, or drop the Linux-boot part of the test.

* Splitting the DMSC: hw/misc/ti-dmsc.c is large because TI-SCI is large. It could be split per message class (clocks / devices / processor boot / security) if reviewers prefer, at the cost of a non-functional intermediate state.

* Only one of the four R5F cores is modelled. mc->max_cpus reserves room for the rest. Because the SoC realizes M4F/R5F vCPUs in addition to the A53s and every vCPU needs a TCG context slot inside smp.max_cpus, the machine defaults to the full vCPU budget. I would appreciate a better idea than the actual "fail early with an explanatory message" handling when the user passes a too-small -smp.

Quoted 4 lines

* The register-level stubs (DDRSS in particular) fake success rather than modelling the hardware. That is deliberate, but tell me if you would rather see them as plain unimplemented-device windows plus firmware quirks elsewhere.

I would add one more which is who is committing to maintain this model if it is merged?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#24 lore raw
In reply to Wadim Mueller

Hi Wadim,

Quoted 4 lines · Wadim Mueller

Add support for the TI AM64x (Sitara AM6442) SoC and a board around it, ``am64-virt``. The model is complete enough to run the stock TI boot chain end to end:

Should the machine be named as something that matches an existing board from TI, instead of am64-virt, which makes me think it's just something we created just for emulation purpose?

Quoted 215 lines

ROM boot (tiboot3.bin) -> R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux SMP

This is a resend: I posted a first RFC back in May [1] but got no feedback on it. Since then was the series rebased onto actual master, reworked for style and split into reviewable pieces, so I send it out again in the hope of getting some review.

RFC because I would like feedback on the overall approach before polishing further; see the open questions at the end.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

What is modelled
================

The SoC is heterogeneous, and the model reflects that: a Cortex-A53 cluster behind a GICv3 (GIC-500) at the real addresses, the Cortex-R5F MCU boot core, and the Cortex-M4F, each with their own RAT (region address translation) view of the memory map.

The interesting part is the DMSC. On real silicon it is a hidden core running TI's system firmware (SYSFW) which answers TI-SCI requests for clocks, power, firewalls and processor boot control from every other core. Rather than trying to execute the vendor blob, patch 10 models the DMSC as a TI-SCI endpoint sitting on top of the secure proxy model (patch 9). That is what actually starts the A53 and M4F cores when the R5 SPL asks for it, and what TF-A, OP-TEE, U-Boot and Linux talk to for clock/power management.

The remaining devices (patches 4-8) are the supporting cast: the UART, the K3 DMTimer, mailboxes, and register-level stubs for CTRL_MMR (including the DEVSTAT boot pins), GTC, DDRSS, the SDHCI PHY wrapper and the TRNG. The
stubs implement the handshakes, which firmware polls for, not the underlying hardware.

Patches 1-3 are prerequisites that touch existing code:

1-2 give the OMAP I2C model its own Kconfig symbol so it can be reused
outside the OMAP1 SoC, and implement soft reset and NACK reporting
3 fixes SDHCI ADMA chain processing; without it, transfer rate
depends from the virtual clock instead of on the guest's programming. Patch 14 contains a qtest that fails without it.

To patch 3: it is only needed as long as Bin Meng's SDHCI series [2] (on top of the SDMA boundary fix [3]) is not merged. I posted the same fix standalone in August, Bin showed then, that his series already covers the AM64x case, and I have withdrawn it there [4]. It is kept here only so that this series works on actual master - please drop patch 3 together with the pacing qtest in patch 14 as soon as [2] lands. A version of that qtest for the Icicle Kit was posted separately [5].

[2] https://patchwork.ozlabs.org/project/qemu-devel/list/?series=515264
[3] https://patchwork.ozlabs.org/project/qemu-devel/list/?series=513930
[4] https://lore.kernel.org/qemu-devel/[email protected]/
[5] https://lore.kernel.org/qemu-devel/[email protected]/

Testing
=======

Patch 14 adds:

* a unit test for the combined-boot-image parser
* qtests for the machine topology, GICv3 layout, SMP handling and the DMSC TI-SCI request/response path
* an I2C qtest (soft reset, NACK, and a read from an at24c EEPROM which the test attaches with -device)
* the SDHCI ADMA pacing reproducer
* a functional test that builds a synthetic tiboot3.bin containing a bare-metal R5 payload and checks it runs from the certified entry point. Setting QEMU_TEST_TIBOOT3 boots a real image instead.

Migration is not supported yet: none of the new devices has a VMState. I left it out for the RFC, but can add it if you want it before merge.

The series was built and tested on top of ae4f344320. Every patch builds on its own, and checkpatch reports no errors.

Open questions
==============

* pc-bios/dtb/am64-virt.dtb: patch 13 checks in a generated DTB (with its source) for convenience. I am not sure a machine-specific blob belongs in pc-bios; happy to drop it and have the functional test generate it with dtc instead, or drop the Linux-boot part of the test.

* Splitting the DMSC: hw/misc/ti-dmsc.c is large because TI-SCI is large. It could be split per message class (clocks / devices / processor boot / security) if reviewers prefer, at the cost of a non-functional intermediate state.

* Only one of the four R5F cores is modelled. mc->max_cpus reserves room for the rest. Because the SoC realizes M4F/R5F vCPUs in addition to the A53s and every vCPU needs a TCG context slot inside smp.max_cpus, the machine defaults to the full vCPU budget. I would appreciate a better idea than the actual "fail early with an explanatory message" handling when the user passes a too-small -smp.

* The register-level stubs (DDRSS in particular) fake success rather than modelling the hardware. That is deliberate, but tell me if you would rather see them as plain unimplemented-device windows plus firmware quirks elsewhere.

Wadim Mueller (14):
hw/i2c/omap_i2c: add a dedicated CONFIG_OMAP_I2C symbol hw/i2c/omap_i2c: implement soft reset and NACK reporting hw/sd/sdhci: complete non-interrupt ADMA descriptor chains in one pass hw/char: add TI AM64x UART model
hw/timer: add TI K3 DMTimer model
hw/misc: add TI K3 CTRL_MMR, GTC, DDRSS, SDHCI PHY and TRNG models hw/misc: add TI RAT (region address translation) model hw/misc: add TI mailbox (IPC) model
hw/misc: add TI K3 secure proxy model
hw/misc: add TI DMSC (TI-SCI system controller) model hw/arm: add TI K3 combined boot image parser
hw/arm: add TI AM64x SoC model
hw/arm: add the am64-virt machine
tests: add AM64x unit, qtest and functional tests

 MAINTAINERS                                   |   22 +
 docs/system/arm/am64.rst                      |   74 +
 docs/system/target-arm.rst                    |    1 +
 hw/arm/Kconfig                                |   26 +
 hw/arm/am64-virt.c                            |  347 ++++
 hw/arm/k3-bootrom-parse.c                     |  250 +++
 hw/arm/k3-bootrom.c                           |  125 ++
 hw/arm/meson.build                            |    4 +
 hw/arm/ti-am64x.c                             | 1223 ++++++++++++
 hw/arm/trace-events                           |    4 +
 hw/char/Kconfig                               |    4 +
 hw/char/meson.build                           |    1 +
 hw/char/ti-am64-uart.c                        |  109 ++
 hw/i2c/Kconfig                                |    4 +
 hw/i2c/meson.build                            |    2 +-
 hw/i2c/omap_i2c.c                             |  234 ++-
 hw/misc/Kconfig                               |   27 +
 hw/misc/meson.build                           |   10 +
 hw/misc/ti-dmsc.c                             | 1725 +++++++++++++++++
 hw/misc/ti-k3-ctrlmmr.c                       |  103 +
 hw/misc/ti-k3-ddrss.c                         |   98 +
 hw/misc/ti-k3-gtc.c                           |  100 +
 hw/misc/ti-k3-sdhci-phy.c                     |   91 +
 hw/misc/ti-k3-trng.c                          |  139 ++
 hw/misc/ti-mailbox.c                          |  566 ++++++
 hw/misc/ti-rat.c                              |  290 +++
 hw/misc/ti-sec-proxy.c                        |  446 +++++
 hw/misc/trace-events                          |   62 +
 hw/sd/sdhci-internal.h                        |    9 +
 hw/sd/sdhci.c                                 |   23 +-
 hw/timer/Kconfig                              |    3 +
 hw/timer/meson.build                          |    1 +
 hw/timer/ti-k3-dmtimer.c                      |  177 ++
 include/hw/arm/k3-bootrom.h                   |   43 +
 include/hw/arm/ti-am64x.h                     |   86 +
 include/hw/char/ti-am64-uart.h                |   28 +
 include/hw/misc/ti-dmsc.h                     |  661 +++++++
 include/hw/misc/ti-k3-ctrlmmr.h               |   26 +
 include/hw/misc/ti-k3-ddrss.h                 |   26 +
 include/hw/misc/ti-k3-gtc.h                   |   23 +
 include/hw/misc/ti-k3-sdhci-phy.h             |   26 +
 include/hw/misc/ti-k3-trng.h                  |   35 +
 include/hw/misc/ti-mailbox.h                  |   44 +
 include/hw/misc/ti-rat.h                      |   51 +
 include/hw/misc/ti-sec-proxy.h                |  108 ++
 include/hw/timer/ti-k3-dmtimer.h              |   30 +
 pc-bios/dtb/am64-virt.dtb                     |  Bin 0 -> 6116 bytes
 pc-bios/dtb/am64-virt.dts                     |  292 +++
 tests/functional/aarch64/meson.build          |    2 +
 tests/functional/aarch64/test_am64_bootrom.py |  137 ++
 tests/qtest/am64-adma-pacing-test.c           |  243 +++
 tests/qtest/am64-virt-test.c                  |  502 +++++
 tests/qtest/meson.build                       |    1 +
 tests/qtest/ti-am64-i2c-test.c                |  137 ++
 tests/unit/meson.build                        |    1 +
 tests/unit/test-k3-bootrom.c                  |  263 +++
 56 files changed, 9060 insertions(+), 5 deletions(-)
 create mode 100644 docs/system/arm/am64.rst
 create mode 100644 hw/arm/am64-virt.c
 create mode 100644 hw/arm/k3-bootrom-parse.c
 create mode 100644 hw/arm/k3-bootrom.c
 create mode 100644 hw/arm/ti-am64x.c
 create mode 100644 hw/char/ti-am64-uart.c
 create mode 100644 hw/misc/ti-dmsc.c
 create mode 100644 hw/misc/ti-k3-ctrlmmr.c
 create mode 100644 hw/misc/ti-k3-ddrss.c
 create mode 100644 hw/misc/ti-k3-gtc.c
 create mode 100644 hw/misc/ti-k3-sdhci-phy.c
 create mode 100644 hw/misc/ti-k3-trng.c
 create mode 100644 hw/misc/ti-mailbox.c
 create mode 100644 hw/misc/ti-rat.c
 create mode 100644 hw/misc/ti-sec-proxy.c
 create mode 100644 hw/timer/ti-k3-dmtimer.c
 create mode 100644 include/hw/arm/k3-bootrom.h
 create mode 100644 include/hw/arm/ti-am64x.h
 create mode 100644 include/hw/char/ti-am64-uart.h
 create mode 100644 include/hw/misc/ti-dmsc.h
 create mode 100644 include/hw/misc/ti-k3-ctrlmmr.h
 create mode 100644 include/hw/misc/ti-k3-ddrss.h
 create mode 100644 include/hw/misc/ti-k3-gtc.h
 create mode 100644 include/hw/misc/ti-k3-sdhci-phy.h
 create mode 100644 include/hw/misc/ti-k3-trng.h
 create mode 100644 include/hw/misc/ti-mailbox.h
 create mode 100644 include/hw/misc/ti-rat.h
 create mode 100644 include/hw/misc/ti-sec-proxy.h
 create mode 100644 include/hw/timer/ti-k3-dmtimer.h
 create mode 100644 pc-bios/dtb/am64-virt.dtb
 create mode 100644 pc-bios/dtb/am64-virt.dts
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c
Quoted 1 line

base-commit: ae4f3443209ab154b48b706a146e5f557ab147cb

Regards,
Bin

#18 lore raw

Re: [RFC PATCH v2 14/14] tests: add AM64x unit, qtest and functional tests

In reply to Alex Bennée

On 2026-08-20 17:25, Alex Bennée wrote:

Hi Alex,

Quoted 17 lines
Quoted 14 lines · Wadim Mueller

Cover the new code:

- tests/unit/test-k3-bootrom: combined boot image parser, including malformed input.
- tests/qtest/am64-virt-test: machine topology, GICv3 redistributor layout, SMP handling, DMSC TI-SCI request/response handling.
- tests/qtest/ti-am64-i2c-test: I2C soft reset, NACK on an absent address and a transfer against the on-SoM EEPROM.
- tests/qtest/am64-adma-pacing-test: reproducer for the SDHCI ADMA pacing fix earlier in this series; without the fix it fails.
- tests/functional/aarch64/test_am64_bootrom: builds a synthetic combined boot image with a bare-metal R5 payload and checks that the ROM boot path runs it. If QEMU_TEST_TIBOOT3 points at a real tiboot3.bin, that is booted as well.

Quoted 4 lines

I commend you for including tests. One of the challenges of the myriad of board models we have is not having any way to know if they are used. It certainly helps if there are known images and tests to validate this.

Thanks a lot for looking at the series.

I agree, and this is the weakest part of my series at the moment. What is there today:

test_synthetic_image runs in CI, but the tiboot3.bin is built by the
test itself, so it only proves the ROM parser and that the R5 starts at the certified entry
test_linux_gicv3 runs in CI, fetches a kernel with Asset(),
covers GICv3 and SMP bringup
test_vendor_tiboot3 skipUnless(QEMU_TEST_TIBOOT3), so it never runs
in CI

So exactly the interesting path, R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux, which is the reason why this model exists at all, is the one which is hidden behind an environment variable. This is also the part which would break silently, because nothing else exercises the DMSC and the secure proxy in a realistic way.

I would prefer to make this a real Asset(url, sha256), so that CI boots actual firmware. The firmware I test with is a vendor BSP build for the board I have here (a phyCORE-AM64x SoM).

Before I do this, I would like to know your opinion: is a vendor BSP artifact acceptable as an asset for a functional test, or do you need something with better availability guarantees than a vendor download area? The alternative would be that I build a minimal boot chain from upstream U-Boot and TF-A and host it somewhere stable. This is more reproducible, but then CI tests my own build and not a shipping image, so I am not sure if this is really better.

Quoted 12 lines
Quoted 11 lines
---
 tests/functional/aarch64/meson.build          |   2 +
 tests/functional/aarch64/test_am64_bootrom.py | 137 +++++
 tests/qtest/am64-adma-pacing-test.c           | 243 +++++++++
 tests/qtest/am64-virt-test.c                  | 502 ++++++++++++++++++
 tests/qtest/meson.build                       |   1 +
 tests/qtest/ti-am64-i2c-test.c                | 137 +++++
 tests/unit/meson.build                        |   1 +
 tests/unit/test-k3-bootrom.c                  | 263 +++++++++
Quoted 3 lines

That said I think splitting the unit, qtest and functional tests into separate commits would be worthwhile here.

Yes, that makes sense. I will split it in v3 like this:

tests/unit: test-k3-bootrom.c
tests/qtest: am64-virt-test.c, ti-am64-i2c-test.c
tests/functional: test_am64_bootrom.py

The only one I would keep out of this is am64-adma-pacing-test.c. It belongs to patch 3, because it is the reproducer for exactly that fix, so both can be dropped together as soon as Bin's SDHCI series is merged.

Best regards,
Wadim

Quoted 1,373 lines
Quoted 125 lines
 8 files changed, 1286 insertions(+)
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c
tests/functional/aarch64/meson.build +2 -0
index e81afd6c39..6962ae1da1 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 test_aarch64_timeouts = {
+  'am64_bootrom' : 300,
   'aspeed_ast2700a1' : 600,
   'aspeed_ast2700a2' : 600,
   'aspeed_ast2700fc' : 600,
@@ -26,6 +27,7 @@ tests_aarch64_system_quick = [
 ]
 
 tests_aarch64_system_thorough = [
+  'am64_bootrom',
   'aspeed_ast2700a1',
   'aspeed_ast2700a2',
   'aspeed_ast2700fc',
tests/functional/aarch64/test_am64_bootrom.py new file +92 -0
new file mode 100644
index 0000000000..2203ec7585
@@ -0,0 +1,137 @@
+#!/usr/bin/env python3
+#
+# Boot-ROM emulation test for am64-virt machine: build a synthetic TI
+# combined boot image and check the R5 payload runs from the certified
+# entry.  Optionally boots a vendor tiboot3.bin via QEMU_TEST_TIBOOT3.
+#
+# Copyright (c) 2026 CMBLU Energy AG
+# Author: Wadim Mueller <[email protected]>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+import struct
+
+from qemu_test import Asset, QemuSystemTest, wait_for_console_pattern
+from unittest import skipUnless
+
+# Checked-in DTB path; pc-bios/dtb/am64-virt.dtb does not get copied into
+# the build directory.
+SOURCE_DIR = os.path.normpath(
+    os.path.join(os.path.dirname(__file__), '..', '..', '..'))
+
+
+def der(tag, payload):
+    n = len(payload)
+    if n < 0x80:
+        hdr = bytes([tag, n])
+    else:
+        hdr = bytes([tag, 0x82, n >> 8, n & 0xff])
+    return hdr + payload
+
+
+def der_int(v):
+    out = v.to_bytes((v.bit_length() + 7) // 8 or 1, 'big')
+    if out[0] & 0x80:
+        out = b'\x00' + out
+    return der(0x02, out)
+
+
+EXT_BOOT_OID = bytes.fromhex('06092b0601040182260109')
+SHA512_OID = bytes.fromhex('0609608648016503040203')
+
+
+def component(ctype, core, opts, dest, size):
+    return der(0x30,
+               der_int(ctype) + der_int(core) + der_int(opts) +
+               der(0x04, dest.to_bytes(4, 'big')) + der_int(size) +
+               SHA512_OID + der(0x04, bytes(64)))
+
+
+# Bare-metal A32 stub, linked at 0x70000000: prints a magic string on
+# main UART0 (0x02800000, 16550 THR at offset 0), then parks.
+SBL_STUB = struct.pack(
+    '<10I',
+    0xe59f001c,  # ldr r0, [pc, #0x1c]   ; r0 = 0x02800000
+    0xe28f101c,  # add r1, pc, #0x1c     ; r1 = msg
+    0xe4d12001,  # loop: ldrb r2, [r1], #1
+    0xe3520000,  # cmp r2, #0
+    0x0a000001,  # beq hang
+    0xe5802000,  # str r2, [r0]
+    0xeafffffa,  # b loop
+    0xeafffffe,  # hang: b hang
+    0x00000000,  # (pad)
+    0x02800000,  # UART0 literal
+) + b'K3BOOTROM-OK\r\n\x00'
+
+
+def make_tiboot3():
+    sbl = SBL_STUB
+    sysfw = b'FAKE-SYSFW-PAYLOAD'
+    cfg = b'FAKE-CFG'
+    info = der(0x30,
+               der_int(len(sbl) + len(sysfw) + len(cfg)) + der_int(3) +
+               component(1, 16, 0, 0x70000000, len(sbl)) +
+               component(2, 0, 0, 0x44000, len(sysfw)) +
+               component(18, 0, 0, 0x7b000, len(cfg)))
+    ext = der(0x30, EXT_BOOT_OID + der(0x04, info))
+    cert = der(0x30, ext)
+    return cert + sbl + sysfw + cfg
+
+
+class Am64BootRom(QemuSystemTest):
+
+    # The gated Linux boot subtest can need about 90 s under TCG on a
+    # development host. 300 s leaves room for slower CI machines.
+    timeout = 300
+
+    def boot_bios(self, path):
+        self.set_machine('am64-virt')
+        self.vm.set_console()
+        self.vm.add_args('-bios', path)
+        self.vm.launch()

tests/functional/aarch64/test_am64_bootrom.py line 102

Quoted 10 lines
tests/functional/aarch64/test_am64_bootrom.py new file +10 -0
+
+    def test_synthetic_image(self):
+        path = os.path.join(self.workdir, 'tiboot3-synth.bin')
+        with open(path, 'wb') as f:
+            f.write(make_tiboot3())
+        self.boot_bios(path)
+        wait_for_console_pattern(self, 'K3BOOTROM-OK')
+
+    @skipUnless(os.getenv('QEMU_TEST_TIBOOT3'),
+                'set QEMU_TEST_TIBOOT3=<path to tiboot3.bin>')

Are there any publicly available images we could boot on this board? I'm sure for you QEMU_TEST_TIBOOT3 works well - but having a public known good image means other devs can check things haven't broken on a refactor.

Quoted 1,228 lines
tests/functional/aarch64/test_am64_bootrom.py new file +35 -0
+    def test_vendor_tiboot3(self):
+        self.boot_bios(os.getenv('QEMU_TEST_TIBOOT3'))
+        # The SYSFW ABI line also proves boot notification plus TISCI
+        # VERSION before execution reaches the unmodelled DDR init.
+        wait_for_console_pattern(self, 'U-Boot SPL')
+        wait_for_console_pattern(self, 'SYSFW ABI:')
+
+    # Standalone arm64 netboot kernel.  It has PL011 and GICv3 drivers but
+    # no initramfs, so expected rootfs panic comes after the milestones,
+    # which this test cares about.
+    ASSET_KERNEL = Asset(
+        ('http://ports.ubuntu.com/ubuntu-ports/dists/bionic-updates/main/'
+         'installer-arm64/20101020ubuntu543.19/images/netboot/'
+         'ubuntu-installer/arm64/linux'),
+        'ce54f74ab0b15cfd13d1a293f2d27ffd79d8a85b7bb9bf21093ae9513864ac79')
+
+    def test_linux_gicv3(self):
+        kernel_path = self.ASSET_KERNEL.fetch()
+        dtb = os.path.join(SOURCE_DIR, 'pc-bios', 'dtb', 'am64-virt.dtb')
+        self.set_machine('am64-virt')
+        self.vm.set_console()
+        self.vm.add_args('-kernel', kernel_path,
+                         '-dtb', dtb,
+                         '-append', 'console=ttyAMA0 earlycon')
+        self.vm.launch()
+        wait_for_console_pattern(
+            self,
+            'GICv3: CPU0: found redistributor 0 region '
+            '0:0x0000000001840000')
+        wait_for_console_pattern(self, 'CPU1: Booted secondary processor')
+        wait_for_console_pattern(self, 'ttyAMA0')
+
+
+if __name__ == '__main__':
+    QemuSystemTest.main()
tests/qtest/am64-adma-pacing-test.c new file +243 -0
new file mode 100644
index 0000000000..7b503d86ed
@@ -0,0 +1,243 @@
+/*
+ * QTest for AM64x SDHCI ADMA2 virtual-clock pacing
+ *
+ * CMD18 over a 12-descriptor ADMA2 chain has to complete synchronously
+ * when no DMA-boundary interrupt is requested.  A sliced transfer needs
+ * at least one SDHC_TRANSFER_DELAY virtual-clock step.
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/*
+ * Keep the small SDHCI subset local; sdhci-internal.h is not usable from
+ * qtests, since it exposes SDHCIState internals.
+ */
+#define SDHC_ARGUMENT              0x08
+#define SDHC_TRNMOD                0x0C
+#define SDHC_TRNS_DMA              0x0001
+#define SDHC_TRNS_BLK_CNT_EN       0x0002
+#define SDHC_TRNS_READ             0x0010
+#define SDHC_TRNS_MULTI            0x0020
+#define SDHC_CMDREG                0x0E
+#define SDHC_CMD_DATA_PRESENT      (1 << 5)
+#define SDHC_RSPREG0               0x10
+#define SDHC_PRNSTS                0x24
+#define SDHC_CARD_PRESENT          0x00010000
+#define SDHC_HOSTCTL               0x28
+#define SDHC_CTRL_ADMA2_32         0x10
+#define SDHC_BLKSIZE               0x04
+#define SDHC_CLKCON                0x2C
+#define SDHC_CLOCK_INT_EN          0x0001
+#define SDHC_CLOCK_SDCLK_EN        (1 << 2)
+#define SDHC_SWRST                 0x2F
+#define SDHC_RESET_ALL             0x01
+#define SDHC_NORINTSTS             0x30
+#define SDHC_NIS_ERR               0x8000
+#define SDHC_NIS_CMDCMP            0x0001
+#define SDHC_NIS_TRSCMP            0x0002
+#define SDHC_NORINTSTSEN           0x34
+#define SDHC_ERRINTSTSEN           0x36
+#define SDHC_ADMASYSADDR           0x58
+#define SDHC_ADMA_ATTR_ACT_TRAN    (1 << 5)
+#define SDHC_ADMA_ATTR_END         (1 << 1)
+#define SDHC_ADMA_ATTR_VALID       (1 << 0)
+#define SDHC_TRANSFER_DELAY        100
+/* NDESC stays above SDHC_ADMA_DESCS_PER_DELAY (5). */
+
+/*
+ * Probe both AM64x SDHCI instances.  The first free sd-bus gets the
+ * test card, so the base with CARD_PRESENT depends from realize order.
+ */
+#define SDHCI_SD_BASE     0x0fa00000ULL
+#define SDHCI_EMMC_BASE   0x0fa10000ULL
+
+/* Scratch DDR: ADMA descriptor table plus target buffer. */
+#define ADMA_TABLE_ADDR   0x82000000ULL
+#define ADMA_BUF_ADDR     0x82100000ULL
+
+#define BLK_LEN           512
+#define NDESC             12      /* > SDHC_ADMA_DESCS_PER_DELAY (5) */
+
+/* SDHCI command-register response-type encodings (CMDREG bits [1:0]). */
+#define RESP_NONE         0x0000
+#define RESP_R2           0x0001  /* 136-bit (CID/CSD) */
+#define RESP_R48          0x0002  /* 48-bit  (R1/R3/R6/R7) */
+#define RESP_R1B          0x0003  /* 48-bit with busy (R1b) */
+
+/* ACMD41 argument: HCS (bit30) + a 3.3-3.6V voltage window (non-enquiry). */
+#define ACMD41_ARG        0x40FF8000u
+/* CMD8 argument: VHS = 2.7-3.6V + recommended check pattern 0xAA. */
+#define CMD8_ARG          0x000001AAu
+
+/* Bounded command-complete poll; returns actual latched NORINTSTS. */
+static uint16_t sd_cmd(QTestState *qts, uint64_t base, uint8_t cmd,
+                       uint32_t arg, uint16_t flags)
+{
+    uint16_t sts = 0;
+    int i;
+
+    qtest_writel(qts, base + SDHC_ARGUMENT, arg);
+    qtest_writew(qts, base + SDHC_CMDREG, ((uint16_t)cmd << 8) | flags);
+
+    /* CMDREG write runs synchronously sdhci_send_command(). */
+    for (i = 0; i < 1000; i++) {
+        sts = qtest_readw(qts, base + SDHC_NORINTSTS);
+        if (sts & SDHC_NIS_CMDCMP) {
+            break;
+        }
+    }
+    g_assert_cmphex(sts & SDHC_NIS_CMDCMP, ==, SDHC_NIS_CMDCMP);
+
+    /* Clear all latched normal-interrupt status bits (write-1-to-clear). */
+    qtest_writew(qts, base + SDHC_NORINTSTS, 0xffff);
+    return sts;
+}
+
+/* Return the controller base whose PRNSTS reports a card inserted. */
+static uint64_t find_card_base(QTestState *qts)
+{
+    uint32_t sd_sts = qtest_readl(qts, SDHCI_SD_BASE + SDHC_PRNSTS);
+    uint32_t emmc_sts = qtest_readl(qts, SDHCI_EMMC_BASE + SDHC_PRNSTS);
+
+    if (sd_sts & SDHC_CARD_PRESENT) {
+        return SDHCI_SD_BASE;
+    }
+    if (emmc_sts & SDHC_CARD_PRESENT) {
+        return SDHCI_EMMC_BASE;
+    }
+    g_assert_not_reached();
+}
+
+/* Bring SD card to transfer state. */
+static void sd_bring_up_card(QTestState *qts, uint64_t base)
+{
+    uint32_t rca;
+
+    /* Host reset, clock on (INT_EN sets INT_STABLE), latch all status. */
+    qtest_writeb(qts, base + SDHC_SWRST, SDHC_RESET_ALL);
+    qtest_writew(qts, base + SDHC_CLKCON,
+                 SDHC_CLOCK_INT_EN | SDHC_CLOCK_SDCLK_EN);
+    qtest_writew(qts, base + SDHC_NORINTSTSEN, 0xffff);
+    qtest_writew(qts, base + SDHC_ERRINTSTSEN, 0xffff);
+
+    sd_cmd(qts, base, 0,  0x00000000, RESP_NONE);  /* CMD0  GO_IDLE_STATE   */
+    sd_cmd(qts, base, 8,  CMD8_ARG,   RESP_R48);    /* CMD8  SEND_IF_COND    */
+
+    /* ACMD41: CMD55 (APP_CMD) then CMD41 (SEND_OP_COND), until powered up. */
+    for (int i = 0; i < 100; i++) {
+        sd_cmd(qts, base, 55, 0x00000000, RESP_R48);
+        sd_cmd(qts, base, 41, ACMD41_ARG, RESP_R48);
+        /* Non-enquiry ACMD41 powers up the model; the loop is a guard. */
+        break;
+    }
+
+    sd_cmd(qts, base, 2, 0x00000000, RESP_R2);     /* CMD2  ALL_SEND_CID    */
+    sd_cmd(qts, base, 3, 0x00000000, RESP_R48);    /* CMD3  SEND_RELATIVE   */
+
+    /* R6 packs the assigned RCA in the upper 16 bits of RSPREG0. */
+    rca = qtest_readl(qts, base + SDHC_RSPREG0) >> 16;
+
+    sd_cmd(qts, base, 7, rca << 16, RESP_R1B);     /* CMD7  SELECT_CARD     */
+    sd_cmd(qts, base, 16, BLK_LEN,  RESP_R48);     /* CMD16 SET_BLOCKLEN    */
+}
+
+/*
+ * 32-bit ADMA2 entries are little-endian 64-bit words: attr in bits
+ * [6:0], length in [31:16], address in [63:32].  No INT bit is set,
+ * so we expect the whole chain in one call.
+ */
+static void build_adma_table(QTestState *qts)
+{
+    for (int i = 0; i < NDESC; i++) {
+        uint8_t attr = SDHC_ADMA_ATTR_VALID | SDHC_ADMA_ATTR_ACT_TRAN;
+        uint32_t addr = (uint32_t)(ADMA_BUF_ADDR + (uint64_t)i * BLK_LEN);
+
+        if (i == NDESC - 1) {
+            attr |= SDHC_ADMA_ATTR_END;
+        }
+        qtest_writel(qts, ADMA_TABLE_ADDR + (uint64_t)i * 8,
+                     ((uint32_t)BLK_LEN << 16) | attr);
+        qtest_writel(qts, ADMA_TABLE_ADDR + (uint64_t)i * 8 + 4, addr);
+    }
+}
+
+static void test_adma_pacing(void)
+{
+    char *tmp = NULL;
+    int fd;
+    GError *err = NULL;
+    uint64_t base;
+    QTestState *qts;
+    uint16_t sts;
+    int steps = 0;
+
+    /* 1 MiB raw backing file, filled with zero. */
+    fd = g_file_open_tmp("am64-adma-XXXXXX.raw", &tmp, &err);
+    g_assert_no_error(err);
+    g_assert_cmpint(fd, >=, 0);
+    g_assert_cmpint(ftruncate(fd, 1 * 1024 * 1024), ==, 0);
+    close(fd);
+
+    qts = qtest_initf("-machine am64-virt -accel qtest -display none "
+                      "-drive if=none,id=sd0,file=%s,format=raw "
+                      "-device sd-card,drive=sd0", tmp);
+
+    base = find_card_base(qts);
+    sd_bring_up_card(qts, base);
+
+    /* Select ADMA2 32-bit as DMA engine (Host Control 1 bits [4:3]). */
+    qtest_writeb(qts, base + SDHC_HOSTCTL, SDHC_CTRL_ADMA2_32);
+
+    build_adma_table(qts);
+
+    /* Program descriptor/geometry and clear old status. */
+    qtest_writel(qts, base + SDHC_ADMASYSADDR, (uint32_t)ADMA_TABLE_ADDR);
+    qtest_writel(qts, base + SDHC_ADMASYSADDR + 4,
+                 (uint32_t)(ADMA_TABLE_ADDR >> 32));
+    qtest_writel(qts, base + SDHC_BLKSIZE, BLK_LEN | (NDESC << 16));
+    qtest_writew(qts, base + SDHC_TRNMOD,
+                 SDHC_TRNS_DMA | SDHC_TRNS_BLK_CNT_EN |
+                 SDHC_TRNS_READ | SDHC_TRNS_MULTI);
+    qtest_writel(qts, base + SDHC_ARGUMENT, 0);  /* start block 0 */
+    qtest_writew(qts, base + SDHC_NORINTSTS, 0xffff);
+
+    /* CMD18 READ_MULTIPLE_BLOCK kicks off ADMA. */
+    qtest_writew(qts, base + SDHC_CMDREG,
+                 (18 << 8) | SDHC_CMD_DATA_PRESENT | RESP_R48);
+
+    /*
+     * Transfer Complete has to be already set.  A batched 12-descriptor
+     * chain needs at least one SDHC_TRANSFER_DELAY step.
+     */
+    while (!((sts = qtest_readw(qts, base + SDHC_NORINTSTS)) &
+             SDHC_NIS_TRSCMP)) {
+        qtest_clock_step(qts, SDHC_TRANSFER_DELAY);
+        steps++;
+        g_assert_cmpint(steps, <, 1000);
+    }
+
+    /* Completion has to be Transfer Complete with no error interrupt. */
+    g_assert_cmphex(sts & SDHC_NIS_TRSCMP, ==, SDHC_NIS_TRSCMP);
+    g_assert_cmphex(sts & SDHC_NIS_ERR, ==, 0);
+
+    g_test_message("ADMA %d descriptors: %d clock steps", NDESC, steps);
+
+    /* The fixed path completes whole chain in one pass: no clock steps. */
+    g_assert_cmpint(steps, ==, 0);
+
+    qtest_quit(qts);
+    unlink(tmp);
+    g_free(tmp);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64/sdhci/adma-pacing", test_adma_pacing);
+    return g_test_run();
+}
tests/qtest/am64-virt-test.c new file +502 -0
new file mode 100644
index 0000000000..c0a9a26275
@@ -0,0 +1,502 @@
+/*
+ * QTests for AM64 virt machine
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "qobject/qdict.h"
+#include "qobject/qlist.h"
+
+#define OCSRAM_BASE 0x70000000ULL
+#define OCSRAM_SIZE (2 * 1024 * 1024)
+#define MAIN_UART0_BASE 0x02800000ULL
+
+static void test_ocsram_rw(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    qtest_writel(qts, OCSRAM_BASE, 0xdeadbeef);
+    g_assert_cmphex(qtest_readl(qts, OCSRAM_BASE), ==, 0xdeadbeef);
+    qtest_writel(qts, OCSRAM_BASE + OCSRAM_SIZE - 4, 0x12345678);
+    g_assert_cmphex(qtest_readl(qts, OCSRAM_BASE + OCSRAM_SIZE - 4), ==,
+                    0x12345678);
+    /* Boot parameters must stay in OCSRAM. */
+    qtest_writel(qts, 0x701bebfc, 0x0);
+    g_assert_cmphex(qtest_readl(qts, 0x701bebfc), ==, 0x0);
+    qtest_quit(qts);
+}
+
+static void test_main_uart0_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* Idle 16550: transmitter empty bits are set. */
+    g_assert_cmphex(qtest_readl(qts, MAIN_UART0_BASE + (5 << 2)) & 0x60,
+                    ==, 0x60);
+    qtest_quit(qts);
+}
+
+static void test_r5f_cpu_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    QDict *resp = qtest_qmp(qts, "{'execute': 'query-cpus-fast'}");
+    QList *cpus = qdict_get_qlist(resp, "return");
+
+    /* 2x A53 + 1x M4 + 1x R5F */
+    g_assert_cmpint(qlist_size(cpus), ==, 4);
+    qobject_unref(resp);
+    qtest_quit(qts);
+}
+
+static void test_devstat(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* CTRLMMR_MAIN_DEVSTAT: primary bootmode = eMMC (0x9 << 3). */
+    g_assert_cmphex(qtest_readl(qts, 0x43000030), ==, 0x48);
+    /* mmr_unlock() kick writes have to be accepted. */
+    qtest_writel(qts, 0x43008008, 0x68ef3490);
+    qtest_writel(qts, 0x4300800c, 0xd172bc5a);
+    qtest_quit(qts);
+}
+
+#define DDRSS_CFG_BASE    0x0f308000ULL
+#define SP_TARGET(thread) (0x4D000000ULL + (thread) * 0x1000)
+#define SP_RT(thread)     (0x4A600000ULL + (thread) * 0x1000)
+
+static void test_dmsc_r5_version(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    /*
+     * R5 secure-host TISCI VERSION request.  The zero secure prefix
+     * has to be skipped before parsing the TISCI header.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230002);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    /* Commit with the last data word. */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* Response has to land on RX thread 0. */
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmpuint(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    /* Secure prefix, echoed VERSION type, ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x0002);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+static void test_dmsc_r5_get_freq(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * R5 secure-host GET_FREQ (0x010e) for MMCSD0 clock 1.  This
+     * controls packed request layout and nonzero frequency response.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23010e);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 57);
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 1);
+    /* commit by writing the last data word */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* response has to land on RX thread 0 (message count > 0) */
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmpuint(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    /* Secure prefix, echoed GET_FREQ type, ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x010e);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    /* freq_hz follows the 8-byte TISCI header and has to be nonzero. */
+    g_assert_cmpuint(qtest_readl(qts, SP_TARGET(0) + 0x10), !=, 0);
+    qtest_quit(qts);
+}
+
+/*
+ * Requests without TISCI_MSG_FLAG_AOP must get no reply.  Else a stale
+ * message in the single-slot RX thread breaks later request/response
+ * pairing, so keep RX count at zero for both no-response messages.
+ */
+static void test_dmsc_r5_no_response_flag(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * Drain the reset-time boot notification.  Reading register 15 clears
+     * the inbound thread message count again.
+     */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /* WAIT_PROC_BOOT_STATUS (0xc401), hdr.flags = 0: no response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c401);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    g_usleep(50 * 1000);
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, ==, 0);
+
+    /* SET_DEVICE (0x0200), hdr.flags = 0: no response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230200);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 121);   /* device id */
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0);     /* state off */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    g_usleep(50 * 1000);
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, ==, 0);
+
+    /* AOP messages still get a response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230002);   /* VERSION */
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+    qtest_quit(qts);
+}
+
+/*
+ * TISCI_MSG_SYS_RESET (0x0005) is a no-response request, but it still
+ * triggers a full machine reset.
+ */
+static void test_dmsc_r5_sys_reset(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* Drain the reset-time boot notification. */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /* SYS_RESET from host 35, with secure prefix and hdr.flags = 0. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230005);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    /* commit: write the last data word */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* The DMSC reset request appears as QEMU RESET event. */
+    qtest_qmp_eventwait(qts, "RESET");
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_counts(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /* TCLR.ST is safe also when the model free-runs. */
+    qtest_writel(qts, 0x02400038, 1);
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000); /* +1 ms */
+    t1 = qtest_readl(qts, 0x0240003c);
+    /* 20 MHz -> 1 ms = 20000 ticks */
+    g_assert_cmpuint(t1 - t0, ==, 20000);
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_prescaler(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /*
+     * PTV=2 with PRE_EN gives 20 MHz / (2 << 2), i.e. 1 ms is
+     * 2500 ticks.
+     */
+    qtest_writel(qts, 0x02400038, 0x2b);
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000); /* +1 ms virtual time */
+    t1 = qtest_readl(qts, 0x0240003c);
+    g_assert_cmpuint(t1 - t0, ==, 2500);
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_reconfigure(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /*
+     * Changing the prescaler while the timer runs must only affect time
+     * after the TCLR write, never rescale already-elapsed ticks.
+     */
+    qtest_writel(qts, 0x02400038, 1);           /* ST, no prescaler */
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000);             /* +1 ms @ 20 MHz  */
+    qtest_writel(qts, 0x02400038, 0x2b);        /* PTV=2, PRE_EN, AR, ST */
+    qtest_clock_step(qts, 1000000);             /* +1 ms @ 2.5 MHz */
+    t1 = qtest_readl(qts, 0x0240003c);
+    g_assert_cmpuint(t1 - t0, ==, 20000 + 2500);
+    qtest_quit(qts);
+}
+
+#define GICD_BASE 0x01800000ULL
+#define GICR_BASE 0x01840000ULL
+#define GIC_PIDR2 0xffe8
+
+static void test_gicv3_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* GICD_PIDR2.ArchRev must report a GICv3 distributor. */
+    g_assert_cmphex((qtest_readl(qts, GICD_BASE + GIC_PIDR2) >> 4) & 0xf,
+                    ==, 3);
+    /* first redistributor frame at actual AM64x GICR base */
+    g_assert_cmphex((qtest_readl(qts, GICR_BASE + GIC_PIDR2) >> 4) & 0xf,
+                    ==, 3);
+    qtest_quit(qts);
+}
+
+static void test_ddrss_stub(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* DENALI_CTL_0 writes must persist, including dram_class DDR4. */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x0, 0x00000A00);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x0), ==, 0x00000A00);
+
+    /* Done bits are ORed into status reads, also after writes. */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x214C, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x214C) & 0x1, ==, 0x1);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x538, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x538) & (1u << 13),
+                    ==, 1u << 13);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x558, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x558) & (1u << 25),
+                    ==, 1u << 25);
+
+    /*
+     * ECC priming needs both BIST_DONE latches: INT_STATUS_MASTER bit 8
+     * and INT_STATUS_BIST bit 0, resp. CTL_341 raw bit 16.
+     */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x538, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x538) & (1u << 8),
+                    ==, 1u << 8);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x554, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x554) & (1u << 16),
+                    ==, 1u << 16);
+    qtest_quit(qts);
+}
+
+/*
+ * SET_CONFIG (0xc100) records the A53 bootvector, and PROC_GET_STATUS
+ * (0xc400) must echo it.  The packed payload has processor_id at byte 0
+ * and bootvector_low at bytes 1..4, so 0x701c0000 is written as
+ * 0x1c000020 / 0x00000070.
+ */
+static void test_dmsc_r5_bootvector_capture(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t reg4, reg5, bootvector_lo;
+
+    /* drain boot notification pre-queued on thread 0 at reset */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);      /* sec hdr */
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c100);      /* hdr */
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);      /* AOP */
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 0x1c000020);      /* id+bv */
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0x00000070);
+    qtest_writel(qts, SP_TARGET(1) + 0x18, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x1c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100 && !(qtest_readl(qts, SP_RT(0)) & 0xff); i++) {
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_readl(qts, SP_TARGET(0) + 0x3c);                    /* drain */
+
+    /* PROC_GET_STATUS (0xc400), proc 32: bootvector_low has to echo. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c400);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 32);               /* proc_id */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100 && !(qtest_readl(qts, SP_RT(0)) & 0xff); i++) {
+        g_usleep(10 * 1000);
+    }
+
+    /*
+     * Secure responses carry a zero prefix at +0x04.  processor_id is
+     * response byte 8, so bootvector_lo starts one byte into register 4
+     * and has to be rebuilt from registers 4 and 5.
+     */
+    reg4 = qtest_readl(qts, SP_TARGET(0) + 0x10);
+    reg5 = qtest_readl(qts, SP_TARGET(0) + 0x14);
+    bootvector_lo = (reg4 >> 8) | ((reg5 & 0xff) << 24);
+    g_assert_cmphex(bootvector_lo, ==, 0x701c0000);
+    qtest_quit(qts);
+}
+
+#define SDHCI_SD_BASE   0x0fa00000ULL
+#define SDHCI_EMMC_BASE 0x0fa10000ULL
+
+static void test_sdhci_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * CAPAB bit 28 advertises 64-bit system-bus support, which is needed
+     * for A53 SPL ADMA2-64 transfers.
+     */
+    g_assert_cmphex(qtest_readl(qts, SDHCI_SD_BASE + 0x40), ==, 0x157c34b4);
+    g_assert_cmphex(qtest_readl(qts, SDHCI_EMMC_BASE + 0x40), ==, 0x157c34b4);
+    /* Host controller version (0xFE): SDHCI spec 3.00. */
+    g_assert_cmphex(qtest_readw(qts, SDHCI_SD_BASE + 0xFE) & 0xff, ==, 2);
+    /* PHY window: PHY_STAT1 reads CALDONE|DLLRDY */
+    g_assert_cmphex(qtest_readl(qts, 0x0fa08000ULL + 0x130) & 0x3, ==, 0x3);
+    g_assert_cmphex(qtest_readl(qts, 0x0fa18000ULL + 0x130) & 0x3, ==, 0x3);
+    qtest_quit(qts);
+}
+
+#define TRNG_BASE 0x40910000ULL
+
+static void test_trng_stub(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* readiness bit is permanently set */
+    g_assert_cmphex(qtest_readl(qts, TRNG_BASE + 0x10) & 0x1, ==, 0x1);
+    /* Output words must be nonzero and change between reads. */
+    uint32_t a = qtest_readl(qts, TRNG_BASE + 0x00);
+    uint32_t b = qtest_readl(qts, TRNG_BASE + 0x00);
+
+    g_assert_cmpuint(a, !=, 0);
+    g_assert_true(a != b || qtest_readl(qts, TRNG_BASE + 0x04) != a);
+    /* INTACK writes are accepted. */
+    qtest_writel(qts, TRNG_BASE + 0x10, 0x1);
+    /* CONTROL is RAM-backed: read back actual written value */
+    qtest_writel(qts, TRNG_BASE + 0x14, 0x400);
+    g_assert_cmphex(qtest_readl(qts, TRNG_BASE + 0x14), ==, 0x400);
+    qtest_quit(qts);
+}
+
+/*
+ * A53_0 uses threads 9/8 with the same secure prefix as the R5 secure
+ * pair.  If it is classified non-secure, the request header shifts and
+ * response prefix is missing, leaving BL31 waiting forever.
+ */
+static void test_dmsc_a53_secure_version(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    if (qtest_readl(qts, SP_RT(8)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(8) + 0x3c);
+    }
+
+    /*
+     * A53_0 secure-host VERSION request: zero secure prefix, host 10,
+     * and AOP is set.
+     */
+    qtest_writel(qts, SP_TARGET(9) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(9) + 0x08, 0x010a0002);
+    qtest_writel(qts, SP_TARGET(9) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(9) + 0x3c, 0x00000000);
+
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(8)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(8)) & 0xff, >, 0);
+
+    /* VERSION type echoed and ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(8) + 0x08) & 0xffff,
+                    ==, 0x0002);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(8) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+/*
+ * TISCI_MSG_FWL_SET (0x9000) is a bare-ACK request.  Without handler
+ * it falls through to the unknown-message NAK path.
+ */
+static void test_dmsc_fwl_set_ack(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /*
+     * R5 secure FWL_SET: type 0x9000, host 35, AOP set, fwl_id 0x23,
+     * region 3, one permission register.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0b239000);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 0x00030023);
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0x00000001);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x9000);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64-virt/ocsram", test_ocsram_rw);
+    qtest_add_func("/am64-virt/main-uart0", test_main_uart0_present);
+    qtest_add_func("/am64-virt/r5f-present", test_r5f_cpu_present);
+    qtest_add_func("/am64-virt/devstat", test_devstat);
+    qtest_add_func("/am64-virt/dmsc-r5-version", test_dmsc_r5_version);
+    qtest_add_func("/am64-virt/dmsc-r5-get-freq", test_dmsc_r5_get_freq);
+    qtest_add_func("/am64-virt/dmsc-no-response",
+                   test_dmsc_r5_no_response_flag);
+    qtest_add_func("/am64-virt/dmsc-sys-reset", test_dmsc_r5_sys_reset);
+    qtest_add_func("/am64-virt/dmsc-bootvector",
+                   test_dmsc_r5_bootvector_capture);
+    qtest_add_func("/am64-virt/dmtimer", test_dmtimer_counts);
+    qtest_add_func("/am64-virt/dmtimer-prescaler", test_dmtimer_prescaler);
+    qtest_add_func("/am64-virt/dmtimer-reconfigure", test_dmtimer_reconfigure);
+    qtest_add_func("/am64-virt/gicv3", test_gicv3_present);
+    qtest_add_func("/am64-virt/ddrss-stub", test_ddrss_stub);
+    qtest_add_func("/am64-virt/sdhci", test_sdhci_present);
+    qtest_add_func("/am64-virt/trng", test_trng_stub);
+    qtest_add_func("/am64-virt/dmsc-a53-secure-version",
+                   test_dmsc_a53_secure_version);
+    qtest_add_func("/am64-virt/dmsc-fwl-set", test_dmsc_fwl_set_ack);
+    return g_test_run();
+}
tests/qtest/meson.build +1 -0
index f7c7d06620..d915fb4199 100644
@@ -274,6 +274,7 @@ qtests_aarch64 = \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_ARM_VIRT') ?
    ['iommu-smmuv3-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_AM64_VIRT') ? ['am64-virt-test', 'ti-am64-i2c-test', 'am64-adma-pacing-test'] : []) + \
   qtests_cxl +                                                                                  \
   ['arm-cpu-features',
    'numa-test',
tests/qtest/ti-am64-i2c-test.c new file +137 -0
new file mode 100644
index 0000000000..eda8d8032f
@@ -0,0 +1,137 @@
+/*
+ * QTests for AM64x main_i2c0 controller
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/* main_i2c0 window: i2c@20000000, len 0x100. */
+#define I2C0_BASE 0x20000000ULL
+
+/* OMAP I2C V2 register offsets. */
+#define I2C_V2_SYSC          0x10
+#define I2C_V2_IRQSTATUS_RAW 0x24
+#define I2C_V2_IRQSTATUS     0x28
+#define I2C_V2_SYSS          0x90
+#define I2C_V2_CNT           0x98
+#define I2C_V2_DATA          0x9c
+#define I2C_V2_CON           0xa4
+#define I2C_V2_SA            0xac
+
+/* Register bits. */
+#define I2C_SYSC_SRST   (1 << 1)  /* SYSCONFIG soft reset */
+#define I2C_SYSS_RDONE  (1 << 0)  /* reset done */
+#define I2C_STAT_NACK   (1 << 1)  /* no acknowledgement */
+#define I2C_STAT_ARDY   (1 << 2)  /* register access ready */
+#define I2C_STAT_RRDY   (1 << 3)  /* receive data ready */
+
+#define I2C_CON_EN  (1 << 15)     /* module enable */
+#define I2C_CON_MST (1 << 10)     /* master mode */
+#define I2C_CON_STP (1 << 1)      /* stop condition */
+#define I2C_CON_STT (1 << 0)      /* start condition */
+
+/*
+ * SYSCONFIG.SRST must make SYSS.RDONE assert, or the firmware spins in
+ * the soft-reset poll.
+ */
+static void test_soft_reset_completes(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t syss = 0;
+
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+
+    for (int i = 0; i < 16; i++) {
+        syss = qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+        if (syss & I2C_SYSS_RDONE) {
+            break;
+        }
+    }
+    g_assert_cmphex(syss & I2C_SYSS_RDONE, ==, I2C_SYSS_RDONE);
+    qtest_quit(qts);
+}
+
+/*
+ * Reads from an address without a slave must raise NACK promptly, since a
+ * driver would otherwise wait forever for an I2C event. No slave is
+ * attached here, so any address does.
+ */
+static void test_nack_on_absent_slave(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t stat;
+
+    /* Bring the controller out of reset as firmware does. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+    (void)qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+
+    /* Address 0x51 has no slave: one-byte master read. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SA, 0x51);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CNT, 1);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CON,
+                 I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
+
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, I2C_STAT_NACK);
+
+    /* IRQSTATUS mirrors NACK and is write-1-to-clear. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, I2C_STAT_NACK);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_IRQSTATUS, I2C_STAT_NACK);
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, 0);
+
+    qtest_quit(qts);
+}
+
+/*
+ * With a slave on the bus a one-byte master read has to ACK, return DATA
+ * and then finish with ARDY. An at24c EEPROM at 0x50 serves as the slave;
+ * it comes up erased, so offset 0 reads back as 0x00.
+ */
+static void test_eeprom_read_first_byte(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt "
+                                 "-device at24c-eeprom,address=0x50,"
+                                 "rom-size=4096");
+    uint32_t stat;
+    uint8_t b;
+
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+    (void)qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+
+    /* One-byte read from 0x50; address pointer starts by 0. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SA, 0x50);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CNT, 1);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CON,
+                 I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
+
+    /* Slave ACKed and data are ready. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, 0);
+    g_assert_cmphex(stat & I2C_STAT_RRDY, ==, I2C_STAT_RRDY);
+
+    /* Offset 0 of the erased EEPROM reads as 0x00. */
+    b = qtest_readw(qts, I2C0_BASE + I2C_V2_DATA) & 0xff;
+    g_assert_cmphex(b, ==, 0x00);
+
+    /* Single-byte transfer complete: ARDY, no more RRDY. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_ARDY, ==, I2C_STAT_ARDY);
+    g_assert_cmphex(stat & I2C_STAT_RRDY, ==, 0);
+
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64/i2c/soft-reset", test_soft_reset_completes);
+    qtest_add_func("/am64/i2c/nack", test_nack_on_absent_slave);
+    qtest_add_func("/am64/i2c/eeprom-read", test_eeprom_read_first_byte);
+    return g_test_run();
+}
tests/unit/meson.build +1 -0
index dc3fb954c0..be2aab4274 100644
@@ -41,6 +41,7 @@ tests = {
   'test-bitops': [],
   'test-bitcnt': [],
   'test-qgraph': ['../qtest/libqos/qgraph.c'],
+  'test-k3-bootrom': ['../../hw/arm/k3-bootrom-parse.c'],
   'check-qom-interface': [qom],
   'check-qom-proplist': [qom],
   'test-qemu-opts': [],
tests/unit/test-k3-bootrom.c new file +263 -0
new file mode 100644
index 0000000000..b030d7df9b
@@ -0,0 +1,263 @@
+/*
+ * Unit tests for K3 boot-ROM combined-image parser
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/arm/k3-bootrom.h"
+
+/* Minimal DER emitters. */
+
+static GByteArray *der_tlv(uint8_t tag, const uint8_t *data, size_t len)
+{
+    GByteArray *a = g_byte_array_new();
+
+    g_byte_array_append(a, &tag, 1);
+    if (len < 0x80) {
+        uint8_t l = len;
+        g_byte_array_append(a, &l, 1);
+    } else if (len <= 0xffff) {
+        uint8_t l[3] = { 0x82, len >> 8, len & 0xff };
+        g_byte_array_append(a, l, 3);
+    } else {
+        g_assert_not_reached();
+    }
+    if (data) {
+        g_byte_array_append(a, data, len);
+    }
+    return a;
+}
+
+static GByteArray *der_wrap(uint8_t tag, GByteArray *inner)
+{
+    GByteArray *a = der_tlv(tag, inner->data, inner->len);
+    g_byte_array_unref(inner);
+    return a;
+}
+
+static void der_append(GByteArray *dst, GByteArray *src)
+{
+    g_byte_array_append(dst, src->data, src->len);
+    g_byte_array_unref(src);
+}
+
+static GByteArray *der_uint(uint64_t v)
+{
+    uint8_t buf[9];
+    int n = 0;
+    uint64_t t = v;
+
+    do {
+        n++;
+        t >>= 8;
+    } while (t);
+    if (v >> (n * 8 - 1) & 1) {
+        n++; /* leading zero keeps it positive */
+    }
+    for (int i = 0; i < n; i++) {
+        buf[i] = v >> ((n - 1 - i) * 8);
+    }
+    return der_tlv(0x02, buf, n);
+}
+
+/* OID 1.3.6.1.4.1.294.1.9 (ext_boot_info), pre-encoded TLV */
+static const uint8_t ext_boot_oid[] = {
+    0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x26, 0x01, 0x09
+};
+/* OID 2.16.840.1.101.3.4.2.3 (sha512), pre-encoded TLV */
+static const uint8_t sha512_oid[] = {
+    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static GByteArray *der_component(uint32_t ctype, uint32_t core,
+                                 uint32_t opts, uint32_t dest, uint64_t size)
+{
+    GByteArray *seq = g_byte_array_new();
+    uint8_t dest_be[4] = { dest >> 24, dest >> 16, dest >> 8, dest };
+    uint8_t sha[64] = { 0 };
+
+    der_append(seq, der_uint(ctype));
+    der_append(seq, der_uint(core));
+    der_append(seq, der_uint(opts));
+    der_append(seq, der_tlv(0x04, dest_be, sizeof(dest_be)));
+    der_append(seq, der_uint(size));
+    g_byte_array_append(seq, sha512_oid, sizeof(sha512_oid));
+    der_append(seq, der_tlv(0x04, sha, sizeof(sha)));
+    return der_wrap(0x30, seq);
+}
+
+/*
+ * Layout follows u-boot tools/binman/btool/openssl.py
+ * x509_cert_rom_combined(): SBL, SYSFW, SYSFW-DATA payloads are after
+ * the certificate.
+ */
+static GByteArray *make_image(const uint8_t *sbl, size_t sbl_len)
+{
+    static const uint8_t sysfw_blob[16] = "SYSFW-payload";
+    static const uint8_t cfg_blob[8] = "BCFG";
+    GByteArray *info = g_byte_array_new();
+    GByteArray *ext, *cert, *img;
+
+    der_append(info, der_uint(sbl_len + sizeof(sysfw_blob)
+                              + sizeof(cfg_blob)));      /* extImgSize */
+    der_append(info, der_uint(3));                       /* numComp */
+    der_append(info, der_component(K3_COMP_TYPE_SBL, 16, 0,
+                                   0x70000000, sbl_len));
+    der_append(info, der_component(K3_COMP_TYPE_SYSFW, 0, 0,
+                                   0x44000, sizeof(sysfw_blob)));
+    der_append(info, der_component(K3_COMP_TYPE_SYSFW_DATA, 0, 0,
+                                   0x7b000, sizeof(cfg_blob)));
+    info = der_wrap(0x30, info);
+
+    /* extension is SEQ { OID, OCTETSTRING { info } } */
+    ext = g_byte_array_new();
+    g_byte_array_append(ext, ext_boot_oid, sizeof(ext_boot_oid));
+    der_append(ext, der_wrap(0x04, info));
+    ext = der_wrap(0x30, ext);
+
+    /* fake cert: top-level SEQUENCE around the extension */
+    cert = der_wrap(0x30, ext);
+
+    img = g_byte_array_new();
+    g_byte_array_append(img, cert->data, cert->len);
+    g_byte_array_unref(cert);
+    g_byte_array_append(img, sbl, sbl_len);
+    g_byte_array_append(img, sysfw_blob, sizeof(sysfw_blob));
+    g_byte_array_append(img, cfg_blob, sizeof(cfg_blob));
+    return img;
+}
+
+/* Certificate-only image with caller given component sizes. */
+static GByteArray *make_cert_with_sizes(const uint64_t *sizes, size_t n)
+{
+    GByteArray *info = g_byte_array_new();
+    GByteArray *ext;
+    uint64_t total = 0;
+
+    for (size_t i = 0; i < n; i++) {
+        total += sizes[i];
+    }
+    der_append(info, der_uint(total));                   /* extImgSize */
+    der_append(info, der_uint(n));                       /* numComp */
+    for (size_t i = 0; i < n; i++) {
+        der_append(info, der_component(K3_COMP_TYPE_SBL, 16, 0,
+                                       0x70000000, sizes[i]));
+    }
+    info = der_wrap(0x30, info);
+
+    ext = g_byte_array_new();
+    g_byte_array_append(ext, ext_boot_oid, sizeof(ext_boot_oid));
+    der_append(ext, der_wrap(0x04, info));
+    ext = der_wrap(0x30, ext);
+    return der_wrap(0x30, ext);
+}
+
+static void test_parse_ok(void)
+{
+    static const uint8_t sbl[32] = "SBL-payload";
+    GByteArray *img = make_image(sbl, sizeof(sbl));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_true(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_null(err);
+    g_assert_cmpuint(out.num_comps, ==, 3);
+    g_assert_cmpuint(out.comps[0].comp_type, ==, K3_COMP_TYPE_SBL);
+    g_assert_cmphex(out.comps[0].dest_addr, ==, 0x70000000);
+    g_assert_cmpuint(out.comps[0].comp_size, ==, sizeof(sbl));
+    g_assert_cmpuint(out.comps[0].payload_offset, ==, out.cert_len);
+    g_assert_cmphex(out.comps[1].dest_addr, ==, 0x44000);
+    g_assert_cmpuint(out.comps[2].payload_offset, ==,
+                     out.cert_len + sizeof(sbl) + 16);
+    g_assert_cmpint(memcmp(img->data + out.comps[0].payload_offset,
+                           sbl, sizeof(sbl)), ==, 0);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_not_der(void)
+{
+    static const uint8_t junk[64] = { 0xff, 0x00, 0x41 };
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(junk, sizeof(junk), &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+}
+
+static void test_parse_no_extension(void)
+{
+    /* Valid DER SEQUENCE, but no ext_boot_info OID. */
+    static const uint8_t seq[] = { 0x30, 0x03, 0x02, 0x01, 0x05 };
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(seq, sizeof(seq), &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+}
+
+static void test_parse_truncated_payload(void)
+{
+    static const uint8_t sbl[32] = "SBL-payload";
+    GByteArray *img = make_image(sbl, sizeof(sbl));
+    K3BootImage out;
+    Error *err = NULL;
+
+    /* Truncated payload: comp_size claims are beyond file. */
+    g_assert_false(k3_bootrom_parse(img->data, img->len - 20, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_size_exceeds_u32(void)
+{
+    /* comp_size wider than 32 bits must not truncate silently. */
+    static const uint64_t sizes[1] = { UINT32_MAX + 1ull };
+    GByteArray *img = make_cert_with_sizes(sizes, G_N_ELEMENTS(sizes));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_size_sum_wraps_32bit(void)
+{
+    /*
+     * Each single size fits in 32 bits, but their sum must not wrap the
+     * running total.
+     */
+    static const uint64_t sizes[3] = {
+        0xf0000000, 0xf0000000, 0xf0000000
+    };
+    GByteArray *img = make_cert_with_sizes(sizes, G_N_ELEMENTS(sizes));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    g_test_add_func("/k3-bootrom/parse-ok", test_parse_ok);
+    g_test_add_func("/k3-bootrom/not-der", test_parse_not_der);
+    g_test_add_func("/k3-bootrom/no-extension", test_parse_no_extension);
+    g_test_add_func("/k3-bootrom/truncated", test_parse_truncated_payload);
+    g_test_add_func("/k3-bootrom/size-exceeds-u32",
+                    test_parse_size_exceeds_u32);
+    g_test_add_func("/k3-bootrom/size-sum-wraps-32bit",
+                    test_parse_size_sum_wraps_32bit);
+    return g_test_run();
+}
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#21 lore raw
In reply to Alex Bennée
Quoted 41 lines · Alex Bennée
Quoted 18 lines · Wadim Mueller

Add support for the TI AM64x (Sitara AM6442) SoC and a board around it, ``am64-virt``. The model is complete enough to run the stock TI boot chain end to end:

ROM boot (tiboot3.bin) -> R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux SMP

This is a resend: I posted a first RFC back in May [1] but got no feedback on it. Since then was the series rebased onto actual master, reworked for style and split into reviewable pieces, so I send it out again in the hope of getting some review.

RFC because I would like feedback on the overall approach before polishing further; see the open questions at the end.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

What is modelled
================

<snip>

Quoted 20 lines

Open questions
==============

* pc-bios/dtb/am64-virt.dtb: patch 13 checks in a generated DTB (with its source) for convenience. I am not sure a machine-specific blob belongs in pc-bios; happy to drop it and have the functional test generate it with dtc instead, or drop the Linux-boot part of the test.

* Splitting the DMSC: hw/misc/ti-dmsc.c is large because TI-SCI is large. It could be split per message class (clocks / devices / processor boot / security) if reviewers prefer, at the cost of a non-functional intermediate state.

* Only one of the four R5F cores is modelled. mc->max_cpus reserves room for the rest. Because the SoC realizes M4F/R5F vCPUs in addition to the A53s and every vCPU needs a TCG context slot inside smp.max_cpus, the machine defaults to the full vCPU budget. I would appreciate a better idea than the actual "fail early with an explanatory message" handling when the user passes a too-small -smp.

Quoted 8 lines
Quoted 4 lines

* The register-level stubs (DDRSS in particular) fake success rather than modelling the hardware. That is deliberate, but tell me if you would rather see them as plain unimplemented-device windows plus firmware quirks elsewhere.

I would add one more which is who is committing to maintain this model if it is merged?

That would be me. The series already adds a MAINTAINERS entry with
S: Maintained. In v3 I will make the F: patterns narrower, because hw/misc/ti-*.c currently claims every future TI misc device and not only the K3 ones.

I develop and test firmware on
real AM64x silicon (a phyCORE-AM64x SoM), and I use this model every day for this work, so I have both the hardware and the vendor boot chain here to reproduce regressions. I also intend to review patches which touch these files.

The testing side of the same question I have answered in the thread of patch 14, where you raised it.

Best regards,
Wadim

Quoted 3 lines
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#23 lore raw
In reply to Nick Huang
Quoted 4 lines · Nick Huang

I think this is a good approach. Thanks for working on this!

Regards,
Nick

Thanks Nick, much appreciated!

Best Regards
Wadim

#25 lore raw
In reply to Bin Meng
Quoted 4 lines · Bin Meng

Hi Wadim,

Quoted 1 line · Wadim Mueller
Quoted 8 lines
Quoted 3 lines

Add support for the TI AM64x (Sitara AM6442) SoC and a board around it, ``am64-virt``. The model is complete enough to run the stock TI boot chain end to end:

Should the machine be named as something that matches an existing board from TI, instead of am64-virt, which makes me think it's just something we created just for emulation purpose?

Yeah, the name is misleading, and it also undersells what is there.

The SoC side is not really virt-like: it is the real AM64x memory map, GICv3 at the silicon addresses, the real UART / I2C / timer / secure-proxy offsets, and it boots stock unmodified vendor firmware through the complete chain ROM -> R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux.

What I did not mention in the cover letter is that my development tree already models the board identity as well: an M24C32 at i3c address 0x50, preloaded with a valid PHYTEC api-v2 identity blob
with the correct size and CRC so that
mainline u-boot's accepts it. This is what the vendor
U-Boot reads to detect the SoM. I left it out of v2 because I was not sure if board specific identity data is wanted upstream, but I think now this was the wrong decision, because it is exactly the proof that this is a board model and not something synthetic.

So IMHO the options are:

a) am64-phycore, which matches the phyCORE-AM64x SoM that I boot and test every day, and the identity EEPROM moves from the SoC model into the board file, where it belongs anyway
b) am64-evm or am64-sk, after TMDS64GPEVM or SK-AM64B, which means doing the board identity again against TI's EEPROM format and getting that firmware

I would prefer a), because this is the configuration I have hardware for and which I can keep testing for regressions, so the maintenance story is realistic.

One caveat: not every peripheral is modelled. CPSW
is only an unimplemented-device stub and there is no ethernet node in the DT. For booting this does not matter, because the whole chain runs from SD/eMMC, but a machine with a board name should probably document what is there and what is not. docs/system/arm/am64.rst does this already, and in v3 I will make the name and the documented scope match.

So my question back: does a non-TI board around the SoC (a SoM from PHYTEC) fulfill your "matches an existing board", or would you rather see a TI branded one?

Best regards,
Wadim

Quoted 219 lines
Quoted 215 lines

ROM boot (tiboot3.bin) -> R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux SMP

This is a resend: I posted a first RFC back in May [1] but got no feedback on it. Since then was the series rebased onto actual master, reworked for style and split into reviewable pieces, so I send it out again in the hope of getting some review.

RFC because I would like feedback on the overall approach before polishing further; see the open questions at the end.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

What is modelled
================

The SoC is heterogeneous, and the model reflects that: a Cortex-A53 cluster behind a GICv3 (GIC-500) at the real addresses, the Cortex-R5F MCU boot core, and the Cortex-M4F, each with their own RAT (region address translation) view of the memory map.

The interesting part is the DMSC. On real silicon it is a hidden core running TI's system firmware (SYSFW) which answers TI-SCI requests for clocks, power, firewalls and processor boot control from every other core. Rather than trying to execute the vendor blob, patch 10 models the DMSC as a TI-SCI endpoint sitting on top of the secure proxy model (patch 9). That is what actually starts the A53 and M4F cores when the R5 SPL asks for it, and what TF-A, OP-TEE, U-Boot and Linux talk to for clock/power management.

The remaining devices (patches 4-8) are the supporting cast: the UART, the K3 DMTimer, mailboxes, and register-level stubs for CTRL_MMR (including the DEVSTAT boot pins), GTC, DDRSS, the SDHCI PHY wrapper and the TRNG. The
stubs implement the handshakes, which firmware polls for, not the underlying hardware.

Patches 1-3 are prerequisites that touch existing code:

1-2 give the OMAP I2C model its own Kconfig symbol so it can be reused
outside the OMAP1 SoC, and implement soft reset and NACK reporting
3 fixes SDHCI ADMA chain processing; without it, transfer rate
depends from the virtual clock instead of on the guest's programming. Patch 14 contains a qtest that fails without it.

To patch 3: it is only needed as long as Bin Meng's SDHCI series [2] (on top of the SDMA boundary fix [3]) is not merged. I posted the same fix standalone in August, Bin showed then, that his series already covers the AM64x case, and I have withdrawn it there [4]. It is kept here only so that this series works on actual master - please drop patch 3 together with the pacing qtest in patch 14 as soon as [2] lands. A version of that qtest for the Icicle Kit was posted separately [5].

[2] https://patchwork.ozlabs.org/project/qemu-devel/list/?series=515264
[3] https://patchwork.ozlabs.org/project/qemu-devel/list/?series=513930
[4] https://lore.kernel.org/qemu-devel/[email protected]/
[5] https://lore.kernel.org/qemu-devel/[email protected]/

Testing
=======

Patch 14 adds:

* a unit test for the combined-boot-image parser
* qtests for the machine topology, GICv3 layout, SMP handling and the DMSC TI-SCI request/response path
* an I2C qtest (soft reset, NACK, and a read from an at24c EEPROM which the test attaches with -device)
* the SDHCI ADMA pacing reproducer
* a functional test that builds a synthetic tiboot3.bin containing a bare-metal R5 payload and checks it runs from the certified entry point. Setting QEMU_TEST_TIBOOT3 boots a real image instead.

Migration is not supported yet: none of the new devices has a VMState. I left it out for the RFC, but can add it if you want it before merge.

The series was built and tested on top of ae4f344320. Every patch builds on its own, and checkpatch reports no errors.

Open questions
==============

* pc-bios/dtb/am64-virt.dtb: patch 13 checks in a generated DTB (with its source) for convenience. I am not sure a machine-specific blob belongs in pc-bios; happy to drop it and have the functional test generate it with dtc instead, or drop the Linux-boot part of the test.

* Splitting the DMSC: hw/misc/ti-dmsc.c is large because TI-SCI is large. It could be split per message class (clocks / devices / processor boot / security) if reviewers prefer, at the cost of a non-functional intermediate state.

* Only one of the four R5F cores is modelled. mc->max_cpus reserves room for the rest. Because the SoC realizes M4F/R5F vCPUs in addition to the A53s and every vCPU needs a TCG context slot inside smp.max_cpus, the machine defaults to the full vCPU budget. I would appreciate a better idea than the actual "fail early with an explanatory message" handling when the user passes a too-small -smp.

* The register-level stubs (DDRSS in particular) fake success rather than modelling the hardware. That is deliberate, but tell me if you would rather see them as plain unimplemented-device windows plus firmware quirks elsewhere.

Wadim Mueller (14):
hw/i2c/omap_i2c: add a dedicated CONFIG_OMAP_I2C symbol hw/i2c/omap_i2c: implement soft reset and NACK reporting hw/sd/sdhci: complete non-interrupt ADMA descriptor chains in one pass hw/char: add TI AM64x UART model
hw/timer: add TI K3 DMTimer model
hw/misc: add TI K3 CTRL_MMR, GTC, DDRSS, SDHCI PHY and TRNG models hw/misc: add TI RAT (region address translation) model hw/misc: add TI mailbox (IPC) model
hw/misc: add TI K3 secure proxy model
hw/misc: add TI DMSC (TI-SCI system controller) model hw/arm: add TI K3 combined boot image parser
hw/arm: add TI AM64x SoC model
hw/arm: add the am64-virt machine
tests: add AM64x unit, qtest and functional tests

 MAINTAINERS                                   |   22 +
 docs/system/arm/am64.rst                      |   74 +
 docs/system/target-arm.rst                    |    1 +
 hw/arm/Kconfig                                |   26 +
 hw/arm/am64-virt.c                            |  347 ++++
 hw/arm/k3-bootrom-parse.c                     |  250 +++
 hw/arm/k3-bootrom.c                           |  125 ++
 hw/arm/meson.build                            |    4 +
 hw/arm/ti-am64x.c                             | 1223 ++++++++++++
 hw/arm/trace-events                           |    4 +
 hw/char/Kconfig                               |    4 +
 hw/char/meson.build                           |    1 +
 hw/char/ti-am64-uart.c                        |  109 ++
 hw/i2c/Kconfig                                |    4 +
 hw/i2c/meson.build                            |    2 +-
 hw/i2c/omap_i2c.c                             |  234 ++-
 hw/misc/Kconfig                               |   27 +
 hw/misc/meson.build                           |   10 +
 hw/misc/ti-dmsc.c                             | 1725 +++++++++++++++++
 hw/misc/ti-k3-ctrlmmr.c                       |  103 +
 hw/misc/ti-k3-ddrss.c                         |   98 +
 hw/misc/ti-k3-gtc.c                           |  100 +
 hw/misc/ti-k3-sdhci-phy.c                     |   91 +
 hw/misc/ti-k3-trng.c                          |  139 ++
 hw/misc/ti-mailbox.c                          |  566 ++++++
 hw/misc/ti-rat.c                              |  290 +++
 hw/misc/ti-sec-proxy.c                        |  446 +++++
 hw/misc/trace-events                          |   62 +
 hw/sd/sdhci-internal.h                        |    9 +
 hw/sd/sdhci.c                                 |   23 +-
 hw/timer/Kconfig                              |    3 +
 hw/timer/meson.build                          |    1 +
 hw/timer/ti-k3-dmtimer.c                      |  177 ++
 include/hw/arm/k3-bootrom.h                   |   43 +
 include/hw/arm/ti-am64x.h                     |   86 +
 include/hw/char/ti-am64-uart.h                |   28 +
 include/hw/misc/ti-dmsc.h                     |  661 +++++++
 include/hw/misc/ti-k3-ctrlmmr.h               |   26 +
 include/hw/misc/ti-k3-ddrss.h                 |   26 +
 include/hw/misc/ti-k3-gtc.h                   |   23 +
 include/hw/misc/ti-k3-sdhci-phy.h             |   26 +
 include/hw/misc/ti-k3-trng.h                  |   35 +
 include/hw/misc/ti-mailbox.h                  |   44 +
 include/hw/misc/ti-rat.h                      |   51 +
 include/hw/misc/ti-sec-proxy.h                |  108 ++
 include/hw/timer/ti-k3-dmtimer.h              |   30 +
 pc-bios/dtb/am64-virt.dtb                     |  Bin 0 -> 6116 bytes
 pc-bios/dtb/am64-virt.dts                     |  292 +++
 tests/functional/aarch64/meson.build          |    2 +
 tests/functional/aarch64/test_am64_bootrom.py |  137 ++
 tests/qtest/am64-adma-pacing-test.c           |  243 +++
 tests/qtest/am64-virt-test.c                  |  502 +++++
 tests/qtest/meson.build                       |    1 +
 tests/qtest/ti-am64-i2c-test.c                |  137 ++
 tests/unit/meson.build                        |    1 +
 tests/unit/test-k3-bootrom.c                  |  263 +++
 56 files changed, 9060 insertions(+), 5 deletions(-)
 create mode 100644 docs/system/arm/am64.rst
 create mode 100644 hw/arm/am64-virt.c
 create mode 100644 hw/arm/k3-bootrom-parse.c
 create mode 100644 hw/arm/k3-bootrom.c
 create mode 100644 hw/arm/ti-am64x.c
 create mode 100644 hw/char/ti-am64-uart.c
 create mode 100644 hw/misc/ti-dmsc.c
 create mode 100644 hw/misc/ti-k3-ctrlmmr.c
 create mode 100644 hw/misc/ti-k3-ddrss.c
 create mode 100644 hw/misc/ti-k3-gtc.c
 create mode 100644 hw/misc/ti-k3-sdhci-phy.c
 create mode 100644 hw/misc/ti-k3-trng.c
 create mode 100644 hw/misc/ti-mailbox.c
 create mode 100644 hw/misc/ti-rat.c
 create mode 100644 hw/misc/ti-sec-proxy.c
 create mode 100644 hw/timer/ti-k3-dmtimer.c
 create mode 100644 include/hw/arm/k3-bootrom.h
 create mode 100644 include/hw/arm/ti-am64x.h
 create mode 100644 include/hw/char/ti-am64-uart.h
 create mode 100644 include/hw/misc/ti-dmsc.h
 create mode 100644 include/hw/misc/ti-k3-ctrlmmr.h
 create mode 100644 include/hw/misc/ti-k3-ddrss.h
 create mode 100644 include/hw/misc/ti-k3-gtc.h
 create mode 100644 include/hw/misc/ti-k3-sdhci-phy.h
 create mode 100644 include/hw/misc/ti-k3-trng.h
 create mode 100644 include/hw/misc/ti-mailbox.h
 create mode 100644 include/hw/misc/ti-rat.h
 create mode 100644 include/hw/misc/ti-sec-proxy.h
 create mode 100644 include/hw/timer/ti-k3-dmtimer.h
 create mode 100644 pc-bios/dtb/am64-virt.dtb
 create mode 100644 pc-bios/dtb/am64-virt.dts
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c
Quoted 1 line

base-commit: ae4f3443209ab154b48b706a146e5f557ab147cb

Regards,
Bin

Reviewed-by on v2 1/14
#2 lore raw

Re: [RFC PATCH v2 01/14] hw/i2c/omap_i2c: add a dedicated CONFIG_OMAP_I2C symbol

In reply to Wadim Mueller
Quoted 10 lines · Wadim Mueller

The OMAP I2C controller model was only buildable as part of the full OMAP1 SoC (CONFIG_OMAP). The same IP is used by the TI K3 family (AM64x and friends), which shares nothing else with OMAP1.

Give the device its own Kconfig symbol and select it from CONFIG_OMAP, so that other SoCs can reuse the model without pulling in OMAP1.

No functional change.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#19 lore raw

Re: [RFC PATCH v2 14/14] tests: add AM64x unit, qtest and functional tests

In reply to Wadim Mueller
Quoted 45 lines · Wadim Mueller

On 2026-08-20 17:25, Alex Bennée wrote:

Hi Alex,

Quoted 17 lines
Quoted 14 lines · Wadim Mueller

Cover the new code:

- tests/unit/test-k3-bootrom: combined boot image parser, including malformed input.
- tests/qtest/am64-virt-test: machine topology, GICv3 redistributor layout, SMP handling, DMSC TI-SCI request/response handling.
- tests/qtest/ti-am64-i2c-test: I2C soft reset, NACK on an absent address and a transfer against the on-SoM EEPROM.
- tests/qtest/am64-adma-pacing-test: reproducer for the SDHCI ADMA pacing fix earlier in this series; without the fix it fails.
- tests/functional/aarch64/test_am64_bootrom: builds a synthetic combined boot image with a bare-metal R5 payload and checks that the ROM boot path runs it. If QEMU_TEST_TIBOOT3 points at a real tiboot3.bin, that is booted as well.

Quoted 4 lines

I commend you for including tests. One of the challenges of the myriad of board models we have is not having any way to know if they are used. It certainly helps if there are known images and tests to validate this.

Thanks a lot for looking at the series.

I agree, and this is the weakest part of my series at the moment. What is there today:

test_synthetic_image runs in CI, but the tiboot3.bin is built by the
test itself, so it only proves the ROM parser and that the R5 starts at the certified entry
test_linux_gicv3 runs in CI, fetches a kernel with Asset(),
covers GICv3 and SMP bringup
test_vendor_tiboot3 skipUnless(QEMU_TEST_TIBOOT3), so it never runs
in CI

So exactly the interesting path, R5 SPL -> TF-A/OP-TEE -> U-Boot -> Linux, which is the reason why this model exists at all, is the one which is hidden behind an environment variable. This is also the part which would break silently, because nothing else exercises the DMSC and the secure proxy in a realistic way.

Quoted 8 lines

I would prefer to make this a real Asset(url, sha256), so that CI boots actual firmware. The firmware I test with is a vendor BSP build for the board I have here (a phyCORE-AM64x SoM).

Before I do this, I would like to know your opinion: is a vendor BSP artifact acceptable as an asset for a functional test, or do you need something with better availability guarantees than a vendor download area?

So the QEMU project itself tries to avoid hosting binaries itself for governance reasons (it's a lot of work to verify full-and-corresponding source for all test artefacts). So we distribute the problem by relying on individual contributors and companies. I think in order the preference is:

- a build asset from the upstream source repo with repeatable build instructions
- a binary build asset at a stable "official" URL
- a build asset from a source repo with repeatable build instructions
- a binary blob from a "known" maintainers website/forge location
- a random blob from a sketchy URL with no contact details

We don't need blobs to be based on open source but if the manufacturer makes it publicly available from their site we can be reasonably certain the CI system is allowed to use it.

Quoted 4 lines

The alternative would be that I build a minimal boot chain from upstream U-Boot and TF-A and host it somewhere stable. This is more reproducible, but then CI tests my own build and not a shipping image, so I am not sure if this is really better.

If the documentation describes the emulation as capable of running software that is in the field then it makes sense to test that. If you need a special version of software we should make that clear in the docs.

Quoted 1,404 lines
Quoted 13 lines
Quoted 12 lines
---
 tests/functional/aarch64/meson.build          |   2 +
 tests/functional/aarch64/test_am64_bootrom.py | 137 +++++
 tests/qtest/am64-adma-pacing-test.c           | 243 +++++++++
 tests/qtest/am64-virt-test.c                  | 502 ++++++++++++++++++
 tests/qtest/meson.build                       |   1 +
 tests/qtest/ti-am64-i2c-test.c                | 137 +++++
 tests/unit/meson.build                        |   1 +
 tests/unit/test-k3-bootrom.c                  | 263 +++++++++
Quoted 3 lines

That said I think splitting the unit, qtest and functional tests into separate commits would be worthwhile here.

Yes, that makes sense. I will split it in v3 like this:

tests/unit: test-k3-bootrom.c
tests/qtest: am64-virt-test.c, ti-am64-i2c-test.c
tests/functional: test_am64_bootrom.py

The only one I would keep out of this is am64-adma-pacing-test.c. It belongs to patch 3, because it is the reproducer for exactly that fix, so both can be dropped together as soon as Bin's SDHCI series is merged.

Best regards,
Wadim

Quoted 1,373 lines
Quoted 125 lines
 8 files changed, 1286 insertions(+)
 create mode 100644 tests/functional/aarch64/test_am64_bootrom.py
 create mode 100644 tests/qtest/am64-adma-pacing-test.c
 create mode 100644 tests/qtest/am64-virt-test.c
 create mode 100644 tests/qtest/ti-am64-i2c-test.c
 create mode 100644 tests/unit/test-k3-bootrom.c
tests/functional/aarch64/meson.build +2 -0
index e81afd6c39..6962ae1da1 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 test_aarch64_timeouts = {
+  'am64_bootrom' : 300,
   'aspeed_ast2700a1' : 600,
   'aspeed_ast2700a2' : 600,
   'aspeed_ast2700fc' : 600,
@@ -26,6 +27,7 @@ tests_aarch64_system_quick = [
 ]
 
 tests_aarch64_system_thorough = [
+  'am64_bootrom',
   'aspeed_ast2700a1',
   'aspeed_ast2700a2',
   'aspeed_ast2700fc',
tests/functional/aarch64/test_am64_bootrom.py new file +92 -0
new file mode 100644
index 0000000000..2203ec7585
@@ -0,0 +1,137 @@
+#!/usr/bin/env python3
+#
+# Boot-ROM emulation test for am64-virt machine: build a synthetic TI
+# combined boot image and check the R5 payload runs from the certified
+# entry.  Optionally boots a vendor tiboot3.bin via QEMU_TEST_TIBOOT3.
+#
+# Copyright (c) 2026 CMBLU Energy AG
+# Author: Wadim Mueller <[email protected]>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+import struct
+
+from qemu_test import Asset, QemuSystemTest, wait_for_console_pattern
+from unittest import skipUnless
+
+# Checked-in DTB path; pc-bios/dtb/am64-virt.dtb does not get copied into
+# the build directory.
+SOURCE_DIR = os.path.normpath(
+    os.path.join(os.path.dirname(__file__), '..', '..', '..'))
+
+
+def der(tag, payload):
+    n = len(payload)
+    if n < 0x80:
+        hdr = bytes([tag, n])
+    else:
+        hdr = bytes([tag, 0x82, n >> 8, n & 0xff])
+    return hdr + payload
+
+
+def der_int(v):
+    out = v.to_bytes((v.bit_length() + 7) // 8 or 1, 'big')
+    if out[0] & 0x80:
+        out = b'\x00' + out
+    return der(0x02, out)
+
+
+EXT_BOOT_OID = bytes.fromhex('06092b0601040182260109')
+SHA512_OID = bytes.fromhex('0609608648016503040203')
+
+
+def component(ctype, core, opts, dest, size):
+    return der(0x30,
+               der_int(ctype) + der_int(core) + der_int(opts) +
+               der(0x04, dest.to_bytes(4, 'big')) + der_int(size) +
+               SHA512_OID + der(0x04, bytes(64)))
+
+
+# Bare-metal A32 stub, linked at 0x70000000: prints a magic string on
+# main UART0 (0x02800000, 16550 THR at offset 0), then parks.
+SBL_STUB = struct.pack(
+    '<10I',
+    0xe59f001c,  # ldr r0, [pc, #0x1c]   ; r0 = 0x02800000
+    0xe28f101c,  # add r1, pc, #0x1c     ; r1 = msg
+    0xe4d12001,  # loop: ldrb r2, [r1], #1
+    0xe3520000,  # cmp r2, #0
+    0x0a000001,  # beq hang
+    0xe5802000,  # str r2, [r0]
+    0xeafffffa,  # b loop
+    0xeafffffe,  # hang: b hang
+    0x00000000,  # (pad)
+    0x02800000,  # UART0 literal
+) + b'K3BOOTROM-OK\r\n\x00'
+
+
+def make_tiboot3():
+    sbl = SBL_STUB
+    sysfw = b'FAKE-SYSFW-PAYLOAD'
+    cfg = b'FAKE-CFG'
+    info = der(0x30,
+               der_int(len(sbl) + len(sysfw) + len(cfg)) + der_int(3) +
+               component(1, 16, 0, 0x70000000, len(sbl)) +
+               component(2, 0, 0, 0x44000, len(sysfw)) +
+               component(18, 0, 0, 0x7b000, len(cfg)))
+    ext = der(0x30, EXT_BOOT_OID + der(0x04, info))
+    cert = der(0x30, ext)
+    return cert + sbl + sysfw + cfg
+
+
+class Am64BootRom(QemuSystemTest):
+
+    # The gated Linux boot subtest can need about 90 s under TCG on a
+    # development host. 300 s leaves room for slower CI machines.
+    timeout = 300
+
+    def boot_bios(self, path):
+        self.set_machine('am64-virt')
+        self.vm.set_console()
+        self.vm.add_args('-bios', path)
+        self.vm.launch()

tests/functional/aarch64/test_am64_bootrom.py line 102

Quoted 10 lines
tests/functional/aarch64/test_am64_bootrom.py new file +10 -0
+
+    def test_synthetic_image(self):
+        path = os.path.join(self.workdir, 'tiboot3-synth.bin')
+        with open(path, 'wb') as f:
+            f.write(make_tiboot3())
+        self.boot_bios(path)
+        wait_for_console_pattern(self, 'K3BOOTROM-OK')
+
+    @skipUnless(os.getenv('QEMU_TEST_TIBOOT3'),
+                'set QEMU_TEST_TIBOOT3=<path to tiboot3.bin>')

Are there any publicly available images we could boot on this board? I'm sure for you QEMU_TEST_TIBOOT3 works well - but having a public known good image means other devs can check things haven't broken on a refactor.

Quoted 1,228 lines
tests/functional/aarch64/test_am64_bootrom.py new file +35 -0
+    def test_vendor_tiboot3(self):
+        self.boot_bios(os.getenv('QEMU_TEST_TIBOOT3'))
+        # The SYSFW ABI line also proves boot notification plus TISCI
+        # VERSION before execution reaches the unmodelled DDR init.
+        wait_for_console_pattern(self, 'U-Boot SPL')
+        wait_for_console_pattern(self, 'SYSFW ABI:')
+
+    # Standalone arm64 netboot kernel.  It has PL011 and GICv3 drivers but
+    # no initramfs, so expected rootfs panic comes after the milestones,
+    # which this test cares about.
+    ASSET_KERNEL = Asset(
+        ('http://ports.ubuntu.com/ubuntu-ports/dists/bionic-updates/main/'
+         'installer-arm64/20101020ubuntu543.19/images/netboot/'
+         'ubuntu-installer/arm64/linux'),
+        'ce54f74ab0b15cfd13d1a293f2d27ffd79d8a85b7bb9bf21093ae9513864ac79')
+
+    def test_linux_gicv3(self):
+        kernel_path = self.ASSET_KERNEL.fetch()
+        dtb = os.path.join(SOURCE_DIR, 'pc-bios', 'dtb', 'am64-virt.dtb')
+        self.set_machine('am64-virt')
+        self.vm.set_console()
+        self.vm.add_args('-kernel', kernel_path,
+                         '-dtb', dtb,
+                         '-append', 'console=ttyAMA0 earlycon')
+        self.vm.launch()
+        wait_for_console_pattern(
+            self,
+            'GICv3: CPU0: found redistributor 0 region '
+            '0:0x0000000001840000')
+        wait_for_console_pattern(self, 'CPU1: Booted secondary processor')
+        wait_for_console_pattern(self, 'ttyAMA0')
+
+
+if __name__ == '__main__':
+    QemuSystemTest.main()
tests/qtest/am64-adma-pacing-test.c new file +243 -0
new file mode 100644
index 0000000000..7b503d86ed
@@ -0,0 +1,243 @@
+/*
+ * QTest for AM64x SDHCI ADMA2 virtual-clock pacing
+ *
+ * CMD18 over a 12-descriptor ADMA2 chain has to complete synchronously
+ * when no DMA-boundary interrupt is requested.  A sliced transfer needs
+ * at least one SDHC_TRANSFER_DELAY virtual-clock step.
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/*
+ * Keep the small SDHCI subset local; sdhci-internal.h is not usable from
+ * qtests, since it exposes SDHCIState internals.
+ */
+#define SDHC_ARGUMENT              0x08
+#define SDHC_TRNMOD                0x0C
+#define SDHC_TRNS_DMA              0x0001
+#define SDHC_TRNS_BLK_CNT_EN       0x0002
+#define SDHC_TRNS_READ             0x0010
+#define SDHC_TRNS_MULTI            0x0020
+#define SDHC_CMDREG                0x0E
+#define SDHC_CMD_DATA_PRESENT      (1 << 5)
+#define SDHC_RSPREG0               0x10
+#define SDHC_PRNSTS                0x24
+#define SDHC_CARD_PRESENT          0x00010000
+#define SDHC_HOSTCTL               0x28
+#define SDHC_CTRL_ADMA2_32         0x10
+#define SDHC_BLKSIZE               0x04
+#define SDHC_CLKCON                0x2C
+#define SDHC_CLOCK_INT_EN          0x0001
+#define SDHC_CLOCK_SDCLK_EN        (1 << 2)
+#define SDHC_SWRST                 0x2F
+#define SDHC_RESET_ALL             0x01
+#define SDHC_NORINTSTS             0x30
+#define SDHC_NIS_ERR               0x8000
+#define SDHC_NIS_CMDCMP            0x0001
+#define SDHC_NIS_TRSCMP            0x0002
+#define SDHC_NORINTSTSEN           0x34
+#define SDHC_ERRINTSTSEN           0x36
+#define SDHC_ADMASYSADDR           0x58
+#define SDHC_ADMA_ATTR_ACT_TRAN    (1 << 5)
+#define SDHC_ADMA_ATTR_END         (1 << 1)
+#define SDHC_ADMA_ATTR_VALID       (1 << 0)
+#define SDHC_TRANSFER_DELAY        100
+/* NDESC stays above SDHC_ADMA_DESCS_PER_DELAY (5). */
+
+/*
+ * Probe both AM64x SDHCI instances.  The first free sd-bus gets the
+ * test card, so the base with CARD_PRESENT depends from realize order.
+ */
+#define SDHCI_SD_BASE     0x0fa00000ULL
+#define SDHCI_EMMC_BASE   0x0fa10000ULL
+
+/* Scratch DDR: ADMA descriptor table plus target buffer. */
+#define ADMA_TABLE_ADDR   0x82000000ULL
+#define ADMA_BUF_ADDR     0x82100000ULL
+
+#define BLK_LEN           512
+#define NDESC             12      /* > SDHC_ADMA_DESCS_PER_DELAY (5) */
+
+/* SDHCI command-register response-type encodings (CMDREG bits [1:0]). */
+#define RESP_NONE         0x0000
+#define RESP_R2           0x0001  /* 136-bit (CID/CSD) */
+#define RESP_R48          0x0002  /* 48-bit  (R1/R3/R6/R7) */
+#define RESP_R1B          0x0003  /* 48-bit with busy (R1b) */
+
+/* ACMD41 argument: HCS (bit30) + a 3.3-3.6V voltage window (non-enquiry). */
+#define ACMD41_ARG        0x40FF8000u
+/* CMD8 argument: VHS = 2.7-3.6V + recommended check pattern 0xAA. */
+#define CMD8_ARG          0x000001AAu
+
+/* Bounded command-complete poll; returns actual latched NORINTSTS. */
+static uint16_t sd_cmd(QTestState *qts, uint64_t base, uint8_t cmd,
+                       uint32_t arg, uint16_t flags)
+{
+    uint16_t sts = 0;
+    int i;
+
+    qtest_writel(qts, base + SDHC_ARGUMENT, arg);
+    qtest_writew(qts, base + SDHC_CMDREG, ((uint16_t)cmd << 8) | flags);
+
+    /* CMDREG write runs synchronously sdhci_send_command(). */
+    for (i = 0; i < 1000; i++) {
+        sts = qtest_readw(qts, base + SDHC_NORINTSTS);
+        if (sts & SDHC_NIS_CMDCMP) {
+            break;
+        }
+    }
+    g_assert_cmphex(sts & SDHC_NIS_CMDCMP, ==, SDHC_NIS_CMDCMP);
+
+    /* Clear all latched normal-interrupt status bits (write-1-to-clear). */
+    qtest_writew(qts, base + SDHC_NORINTSTS, 0xffff);
+    return sts;
+}
+
+/* Return the controller base whose PRNSTS reports a card inserted. */
+static uint64_t find_card_base(QTestState *qts)
+{
+    uint32_t sd_sts = qtest_readl(qts, SDHCI_SD_BASE + SDHC_PRNSTS);
+    uint32_t emmc_sts = qtest_readl(qts, SDHCI_EMMC_BASE + SDHC_PRNSTS);
+
+    if (sd_sts & SDHC_CARD_PRESENT) {
+        return SDHCI_SD_BASE;
+    }
+    if (emmc_sts & SDHC_CARD_PRESENT) {
+        return SDHCI_EMMC_BASE;
+    }
+    g_assert_not_reached();
+}
+
+/* Bring SD card to transfer state. */
+static void sd_bring_up_card(QTestState *qts, uint64_t base)
+{
+    uint32_t rca;
+
+    /* Host reset, clock on (INT_EN sets INT_STABLE), latch all status. */
+    qtest_writeb(qts, base + SDHC_SWRST, SDHC_RESET_ALL);
+    qtest_writew(qts, base + SDHC_CLKCON,
+                 SDHC_CLOCK_INT_EN | SDHC_CLOCK_SDCLK_EN);
+    qtest_writew(qts, base + SDHC_NORINTSTSEN, 0xffff);
+    qtest_writew(qts, base + SDHC_ERRINTSTSEN, 0xffff);
+
+    sd_cmd(qts, base, 0,  0x00000000, RESP_NONE);  /* CMD0  GO_IDLE_STATE   */
+    sd_cmd(qts, base, 8,  CMD8_ARG,   RESP_R48);    /* CMD8  SEND_IF_COND    */
+
+    /* ACMD41: CMD55 (APP_CMD) then CMD41 (SEND_OP_COND), until powered up. */
+    for (int i = 0; i < 100; i++) {
+        sd_cmd(qts, base, 55, 0x00000000, RESP_R48);
+        sd_cmd(qts, base, 41, ACMD41_ARG, RESP_R48);
+        /* Non-enquiry ACMD41 powers up the model; the loop is a guard. */
+        break;
+    }
+
+    sd_cmd(qts, base, 2, 0x00000000, RESP_R2);     /* CMD2  ALL_SEND_CID    */
+    sd_cmd(qts, base, 3, 0x00000000, RESP_R48);    /* CMD3  SEND_RELATIVE   */
+
+    /* R6 packs the assigned RCA in the upper 16 bits of RSPREG0. */
+    rca = qtest_readl(qts, base + SDHC_RSPREG0) >> 16;
+
+    sd_cmd(qts, base, 7, rca << 16, RESP_R1B);     /* CMD7  SELECT_CARD     */
+    sd_cmd(qts, base, 16, BLK_LEN,  RESP_R48);     /* CMD16 SET_BLOCKLEN    */
+}
+
+/*
+ * 32-bit ADMA2 entries are little-endian 64-bit words: attr in bits
+ * [6:0], length in [31:16], address in [63:32].  No INT bit is set,
+ * so we expect the whole chain in one call.
+ */
+static void build_adma_table(QTestState *qts)
+{
+    for (int i = 0; i < NDESC; i++) {
+        uint8_t attr = SDHC_ADMA_ATTR_VALID | SDHC_ADMA_ATTR_ACT_TRAN;
+        uint32_t addr = (uint32_t)(ADMA_BUF_ADDR + (uint64_t)i * BLK_LEN);
+
+        if (i == NDESC - 1) {
+            attr |= SDHC_ADMA_ATTR_END;
+        }
+        qtest_writel(qts, ADMA_TABLE_ADDR + (uint64_t)i * 8,
+                     ((uint32_t)BLK_LEN << 16) | attr);
+        qtest_writel(qts, ADMA_TABLE_ADDR + (uint64_t)i * 8 + 4, addr);
+    }
+}
+
+static void test_adma_pacing(void)
+{
+    char *tmp = NULL;
+    int fd;
+    GError *err = NULL;
+    uint64_t base;
+    QTestState *qts;
+    uint16_t sts;
+    int steps = 0;
+
+    /* 1 MiB raw backing file, filled with zero. */
+    fd = g_file_open_tmp("am64-adma-XXXXXX.raw", &tmp, &err);
+    g_assert_no_error(err);
+    g_assert_cmpint(fd, >=, 0);
+    g_assert_cmpint(ftruncate(fd, 1 * 1024 * 1024), ==, 0);
+    close(fd);
+
+    qts = qtest_initf("-machine am64-virt -accel qtest -display none "
+                      "-drive if=none,id=sd0,file=%s,format=raw "
+                      "-device sd-card,drive=sd0", tmp);
+
+    base = find_card_base(qts);
+    sd_bring_up_card(qts, base);
+
+    /* Select ADMA2 32-bit as DMA engine (Host Control 1 bits [4:3]). */
+    qtest_writeb(qts, base + SDHC_HOSTCTL, SDHC_CTRL_ADMA2_32);
+
+    build_adma_table(qts);
+
+    /* Program descriptor/geometry and clear old status. */
+    qtest_writel(qts, base + SDHC_ADMASYSADDR, (uint32_t)ADMA_TABLE_ADDR);
+    qtest_writel(qts, base + SDHC_ADMASYSADDR + 4,
+                 (uint32_t)(ADMA_TABLE_ADDR >> 32));
+    qtest_writel(qts, base + SDHC_BLKSIZE, BLK_LEN | (NDESC << 16));
+    qtest_writew(qts, base + SDHC_TRNMOD,
+                 SDHC_TRNS_DMA | SDHC_TRNS_BLK_CNT_EN |
+                 SDHC_TRNS_READ | SDHC_TRNS_MULTI);
+    qtest_writel(qts, base + SDHC_ARGUMENT, 0);  /* start block 0 */
+    qtest_writew(qts, base + SDHC_NORINTSTS, 0xffff);
+
+    /* CMD18 READ_MULTIPLE_BLOCK kicks off ADMA. */
+    qtest_writew(qts, base + SDHC_CMDREG,
+                 (18 << 8) | SDHC_CMD_DATA_PRESENT | RESP_R48);
+
+    /*
+     * Transfer Complete has to be already set.  A batched 12-descriptor
+     * chain needs at least one SDHC_TRANSFER_DELAY step.
+     */
+    while (!((sts = qtest_readw(qts, base + SDHC_NORINTSTS)) &
+             SDHC_NIS_TRSCMP)) {
+        qtest_clock_step(qts, SDHC_TRANSFER_DELAY);
+        steps++;
+        g_assert_cmpint(steps, <, 1000);
+    }
+
+    /* Completion has to be Transfer Complete with no error interrupt. */
+    g_assert_cmphex(sts & SDHC_NIS_TRSCMP, ==, SDHC_NIS_TRSCMP);
+    g_assert_cmphex(sts & SDHC_NIS_ERR, ==, 0);
+
+    g_test_message("ADMA %d descriptors: %d clock steps", NDESC, steps);
+
+    /* The fixed path completes whole chain in one pass: no clock steps. */
+    g_assert_cmpint(steps, ==, 0);
+
+    qtest_quit(qts);
+    unlink(tmp);
+    g_free(tmp);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64/sdhci/adma-pacing", test_adma_pacing);
+    return g_test_run();
+}
tests/qtest/am64-virt-test.c new file +502 -0
new file mode 100644
index 0000000000..c0a9a26275
@@ -0,0 +1,502 @@
+/*
+ * QTests for AM64 virt machine
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "qobject/qdict.h"
+#include "qobject/qlist.h"
+
+#define OCSRAM_BASE 0x70000000ULL
+#define OCSRAM_SIZE (2 * 1024 * 1024)
+#define MAIN_UART0_BASE 0x02800000ULL
+
+static void test_ocsram_rw(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    qtest_writel(qts, OCSRAM_BASE, 0xdeadbeef);
+    g_assert_cmphex(qtest_readl(qts, OCSRAM_BASE), ==, 0xdeadbeef);
+    qtest_writel(qts, OCSRAM_BASE + OCSRAM_SIZE - 4, 0x12345678);
+    g_assert_cmphex(qtest_readl(qts, OCSRAM_BASE + OCSRAM_SIZE - 4), ==,
+                    0x12345678);
+    /* Boot parameters must stay in OCSRAM. */
+    qtest_writel(qts, 0x701bebfc, 0x0);
+    g_assert_cmphex(qtest_readl(qts, 0x701bebfc), ==, 0x0);
+    qtest_quit(qts);
+}
+
+static void test_main_uart0_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* Idle 16550: transmitter empty bits are set. */
+    g_assert_cmphex(qtest_readl(qts, MAIN_UART0_BASE + (5 << 2)) & 0x60,
+                    ==, 0x60);
+    qtest_quit(qts);
+}
+
+static void test_r5f_cpu_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    QDict *resp = qtest_qmp(qts, "{'execute': 'query-cpus-fast'}");
+    QList *cpus = qdict_get_qlist(resp, "return");
+
+    /* 2x A53 + 1x M4 + 1x R5F */
+    g_assert_cmpint(qlist_size(cpus), ==, 4);
+    qobject_unref(resp);
+    qtest_quit(qts);
+}
+
+static void test_devstat(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* CTRLMMR_MAIN_DEVSTAT: primary bootmode = eMMC (0x9 << 3). */
+    g_assert_cmphex(qtest_readl(qts, 0x43000030), ==, 0x48);
+    /* mmr_unlock() kick writes have to be accepted. */
+    qtest_writel(qts, 0x43008008, 0x68ef3490);
+    qtest_writel(qts, 0x4300800c, 0xd172bc5a);
+    qtest_quit(qts);
+}
+
+#define DDRSS_CFG_BASE    0x0f308000ULL
+#define SP_TARGET(thread) (0x4D000000ULL + (thread) * 0x1000)
+#define SP_RT(thread)     (0x4A600000ULL + (thread) * 0x1000)
+
+static void test_dmsc_r5_version(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    /*
+     * R5 secure-host TISCI VERSION request.  The zero secure prefix
+     * has to be skipped before parsing the TISCI header.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230002);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    /* Commit with the last data word. */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* Response has to land on RX thread 0. */
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmpuint(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    /* Secure prefix, echoed VERSION type, ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x0002);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+static void test_dmsc_r5_get_freq(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * R5 secure-host GET_FREQ (0x010e) for MMCSD0 clock 1.  This
+     * controls packed request layout and nonzero frequency response.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23010e);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 57);
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 1);
+    /* commit by writing the last data word */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* response has to land on RX thread 0 (message count > 0) */
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmpuint(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    /* Secure prefix, echoed GET_FREQ type, ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x010e);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    /* freq_hz follows the 8-byte TISCI header and has to be nonzero. */
+    g_assert_cmpuint(qtest_readl(qts, SP_TARGET(0) + 0x10), !=, 0);
+    qtest_quit(qts);
+}
+
+/*
+ * Requests without TISCI_MSG_FLAG_AOP must get no reply.  Else a stale
+ * message in the single-slot RX thread breaks later request/response
+ * pairing, so keep RX count at zero for both no-response messages.
+ */
+static void test_dmsc_r5_no_response_flag(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * Drain the reset-time boot notification.  Reading register 15 clears
+     * the inbound thread message count again.
+     */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /* WAIT_PROC_BOOT_STATUS (0xc401), hdr.flags = 0: no response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c401);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    g_usleep(50 * 1000);
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, ==, 0);
+
+    /* SET_DEVICE (0x0200), hdr.flags = 0: no response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230200);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 121);   /* device id */
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0);     /* state off */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    g_usleep(50 * 1000);
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, ==, 0);
+
+    /* AOP messages still get a response. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230002);   /* VERSION */
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+    qtest_quit(qts);
+}
+
+/*
+ * TISCI_MSG_SYS_RESET (0x0005) is a no-response request, but it still
+ * triggers a full machine reset.
+ */
+static void test_dmsc_r5_sys_reset(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* Drain the reset-time boot notification. */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /* SYS_RESET from host 35, with secure prefix and hdr.flags = 0. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a230005);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000000);
+    /* commit: write the last data word */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    /* The DMSC reset request appears as QEMU RESET event. */
+    qtest_qmp_eventwait(qts, "RESET");
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_counts(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /* TCLR.ST is safe also when the model free-runs. */
+    qtest_writel(qts, 0x02400038, 1);
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000); /* +1 ms */
+    t1 = qtest_readl(qts, 0x0240003c);
+    /* 20 MHz -> 1 ms = 20000 ticks */
+    g_assert_cmpuint(t1 - t0, ==, 20000);
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_prescaler(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /*
+     * PTV=2 with PRE_EN gives 20 MHz / (2 << 2), i.e. 1 ms is
+     * 2500 ticks.
+     */
+    qtest_writel(qts, 0x02400038, 0x2b);
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000); /* +1 ms virtual time */
+    t1 = qtest_readl(qts, 0x0240003c);
+    g_assert_cmpuint(t1 - t0, ==, 2500);
+    qtest_quit(qts);
+}
+
+static void test_dmtimer_reconfigure(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t t0, t1;
+
+    /*
+     * Changing the prescaler while the timer runs must only affect time
+     * after the TCLR write, never rescale already-elapsed ticks.
+     */
+    qtest_writel(qts, 0x02400038, 1);           /* ST, no prescaler */
+    t0 = qtest_readl(qts, 0x0240003c);
+    qtest_clock_step(qts, 1000000);             /* +1 ms @ 20 MHz  */
+    qtest_writel(qts, 0x02400038, 0x2b);        /* PTV=2, PRE_EN, AR, ST */
+    qtest_clock_step(qts, 1000000);             /* +1 ms @ 2.5 MHz */
+    t1 = qtest_readl(qts, 0x0240003c);
+    g_assert_cmpuint(t1 - t0, ==, 20000 + 2500);
+    qtest_quit(qts);
+}
+
+#define GICD_BASE 0x01800000ULL
+#define GICR_BASE 0x01840000ULL
+#define GIC_PIDR2 0xffe8
+
+static void test_gicv3_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* GICD_PIDR2.ArchRev must report a GICv3 distributor. */
+    g_assert_cmphex((qtest_readl(qts, GICD_BASE + GIC_PIDR2) >> 4) & 0xf,
+                    ==, 3);
+    /* first redistributor frame at actual AM64x GICR base */
+    g_assert_cmphex((qtest_readl(qts, GICR_BASE + GIC_PIDR2) >> 4) & 0xf,
+                    ==, 3);
+    qtest_quit(qts);
+}
+
+static void test_ddrss_stub(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* DENALI_CTL_0 writes must persist, including dram_class DDR4. */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x0, 0x00000A00);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x0), ==, 0x00000A00);
+
+    /* Done bits are ORed into status reads, also after writes. */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x214C, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x214C) & 0x1, ==, 0x1);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x538, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x538) & (1u << 13),
+                    ==, 1u << 13);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x558, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x558) & (1u << 25),
+                    ==, 1u << 25);
+
+    /*
+     * ECC priming needs both BIST_DONE latches: INT_STATUS_MASTER bit 8
+     * and INT_STATUS_BIST bit 0, resp. CTL_341 raw bit 16.
+     */
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x538, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x538) & (1u << 8),
+                    ==, 1u << 8);
+    qtest_writel(qts, DDRSS_CFG_BASE + 0x554, 0x0);
+    g_assert_cmphex(qtest_readl(qts, DDRSS_CFG_BASE + 0x554) & (1u << 16),
+                    ==, 1u << 16);
+    qtest_quit(qts);
+}
+
+/*
+ * SET_CONFIG (0xc100) records the A53 bootvector, and PROC_GET_STATUS
+ * (0xc400) must echo it.  The packed payload has processor_id at byte 0
+ * and bootvector_low at bytes 1..4, so 0x701c0000 is written as
+ * 0x1c000020 / 0x00000070.
+ */
+static void test_dmsc_r5_bootvector_capture(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t reg4, reg5, bootvector_lo;
+
+    /* drain boot notification pre-queued on thread 0 at reset */
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);      /* sec hdr */
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c100);      /* hdr */
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);      /* AOP */
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 0x1c000020);      /* id+bv */
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0x00000070);
+    qtest_writel(qts, SP_TARGET(1) + 0x18, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x1c, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100 && !(qtest_readl(qts, SP_RT(0)) & 0xff); i++) {
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_readl(qts, SP_TARGET(0) + 0x3c);                    /* drain */
+
+    /* PROC_GET_STATUS (0xc400), proc 32: bootvector_low has to echo. */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0a23c400);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 32);               /* proc_id */
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+    for (int i = 0; i < 100 && !(qtest_readl(qts, SP_RT(0)) & 0xff); i++) {
+        g_usleep(10 * 1000);
+    }
+
+    /*
+     * Secure responses carry a zero prefix at +0x04.  processor_id is
+     * response byte 8, so bootvector_lo starts one byte into register 4
+     * and has to be rebuilt from registers 4 and 5.
+     */
+    reg4 = qtest_readl(qts, SP_TARGET(0) + 0x10);
+    reg5 = qtest_readl(qts, SP_TARGET(0) + 0x14);
+    bootvector_lo = (reg4 >> 8) | ((reg5 & 0xff) << 24);
+    g_assert_cmphex(bootvector_lo, ==, 0x701c0000);
+    qtest_quit(qts);
+}
+
+#define SDHCI_SD_BASE   0x0fa00000ULL
+#define SDHCI_EMMC_BASE 0x0fa10000ULL
+
+static void test_sdhci_present(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /*
+     * CAPAB bit 28 advertises 64-bit system-bus support, which is needed
+     * for A53 SPL ADMA2-64 transfers.
+     */
+    g_assert_cmphex(qtest_readl(qts, SDHCI_SD_BASE + 0x40), ==, 0x157c34b4);
+    g_assert_cmphex(qtest_readl(qts, SDHCI_EMMC_BASE + 0x40), ==, 0x157c34b4);
+    /* Host controller version (0xFE): SDHCI spec 3.00. */
+    g_assert_cmphex(qtest_readw(qts, SDHCI_SD_BASE + 0xFE) & 0xff, ==, 2);
+    /* PHY window: PHY_STAT1 reads CALDONE|DLLRDY */
+    g_assert_cmphex(qtest_readl(qts, 0x0fa08000ULL + 0x130) & 0x3, ==, 0x3);
+    g_assert_cmphex(qtest_readl(qts, 0x0fa18000ULL + 0x130) & 0x3, ==, 0x3);
+    qtest_quit(qts);
+}
+
+#define TRNG_BASE 0x40910000ULL
+
+static void test_trng_stub(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    /* readiness bit is permanently set */
+    g_assert_cmphex(qtest_readl(qts, TRNG_BASE + 0x10) & 0x1, ==, 0x1);
+    /* Output words must be nonzero and change between reads. */
+    uint32_t a = qtest_readl(qts, TRNG_BASE + 0x00);
+    uint32_t b = qtest_readl(qts, TRNG_BASE + 0x00);
+
+    g_assert_cmpuint(a, !=, 0);
+    g_assert_true(a != b || qtest_readl(qts, TRNG_BASE + 0x04) != a);
+    /* INTACK writes are accepted. */
+    qtest_writel(qts, TRNG_BASE + 0x10, 0x1);
+    /* CONTROL is RAM-backed: read back actual written value */
+    qtest_writel(qts, TRNG_BASE + 0x14, 0x400);
+    g_assert_cmphex(qtest_readl(qts, TRNG_BASE + 0x14), ==, 0x400);
+    qtest_quit(qts);
+}
+
+/*
+ * A53_0 uses threads 9/8 with the same secure prefix as the R5 secure
+ * pair.  If it is classified non-secure, the request header shifts and
+ * response prefix is missing, leaving BL31 waiting forever.
+ */
+static void test_dmsc_a53_secure_version(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    if (qtest_readl(qts, SP_RT(8)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(8) + 0x3c);
+    }
+
+    /*
+     * A53_0 secure-host VERSION request: zero secure prefix, host 10,
+     * and AOP is set.
+     */
+    qtest_writel(qts, SP_TARGET(9) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(9) + 0x08, 0x010a0002);
+    qtest_writel(qts, SP_TARGET(9) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(9) + 0x3c, 0x00000000);
+
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(8)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(8)) & 0xff, >, 0);
+
+    /* VERSION type echoed and ACK bit is set. */
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(8) + 0x08) & 0xffff,
+                    ==, 0x0002);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(8) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+/*
+ * TISCI_MSG_FWL_SET (0x9000) is a bare-ACK request.  Without handler
+ * it falls through to the unknown-message NAK path.
+ */
+static void test_dmsc_fwl_set_ack(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+
+    if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+        qtest_readl(qts, SP_TARGET(0) + 0x3c);
+    }
+
+    /*
+     * R5 secure FWL_SET: type 0x9000, host 35, AOP set, fwl_id 0x23,
+     * region 3, one permission register.
+     */
+    qtest_writel(qts, SP_TARGET(1) + 0x04, 0x00000000);
+    qtest_writel(qts, SP_TARGET(1) + 0x08, 0x0b239000);
+    qtest_writel(qts, SP_TARGET(1) + 0x0c, 0x00000002);
+    qtest_writel(qts, SP_TARGET(1) + 0x10, 0x00030023);
+    qtest_writel(qts, SP_TARGET(1) + 0x14, 0x00000001);
+    qtest_writel(qts, SP_TARGET(1) + 0x3c, 0x00000000);
+
+    for (int i = 0; i < 100; i++) {
+        if (qtest_readl(qts, SP_RT(0)) & 0xff) {
+            break;
+        }
+        g_usleep(10 * 1000);
+    }
+    g_assert_cmphex(qtest_readl(qts, SP_RT(0)) & 0xff, >, 0);
+
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x08) & 0xffff,
+                    ==, 0x9000);
+    g_assert_cmphex(qtest_readl(qts, SP_TARGET(0) + 0x0c) & 0x2, ==, 0x2);
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64-virt/ocsram", test_ocsram_rw);
+    qtest_add_func("/am64-virt/main-uart0", test_main_uart0_present);
+    qtest_add_func("/am64-virt/r5f-present", test_r5f_cpu_present);
+    qtest_add_func("/am64-virt/devstat", test_devstat);
+    qtest_add_func("/am64-virt/dmsc-r5-version", test_dmsc_r5_version);
+    qtest_add_func("/am64-virt/dmsc-r5-get-freq", test_dmsc_r5_get_freq);
+    qtest_add_func("/am64-virt/dmsc-no-response",
+                   test_dmsc_r5_no_response_flag);
+    qtest_add_func("/am64-virt/dmsc-sys-reset", test_dmsc_r5_sys_reset);
+    qtest_add_func("/am64-virt/dmsc-bootvector",
+                   test_dmsc_r5_bootvector_capture);
+    qtest_add_func("/am64-virt/dmtimer", test_dmtimer_counts);
+    qtest_add_func("/am64-virt/dmtimer-prescaler", test_dmtimer_prescaler);
+    qtest_add_func("/am64-virt/dmtimer-reconfigure", test_dmtimer_reconfigure);
+    qtest_add_func("/am64-virt/gicv3", test_gicv3_present);
+    qtest_add_func("/am64-virt/ddrss-stub", test_ddrss_stub);
+    qtest_add_func("/am64-virt/sdhci", test_sdhci_present);
+    qtest_add_func("/am64-virt/trng", test_trng_stub);
+    qtest_add_func("/am64-virt/dmsc-a53-secure-version",
+                   test_dmsc_a53_secure_version);
+    qtest_add_func("/am64-virt/dmsc-fwl-set", test_dmsc_fwl_set_ack);
+    return g_test_run();
+}
tests/qtest/meson.build +1 -0
index f7c7d06620..d915fb4199 100644
@@ -274,6 +274,7 @@ qtests_aarch64 = \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_ARM_VIRT') ?
    ['iommu-smmuv3-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_AM64_VIRT') ? ['am64-virt-test', 'ti-am64-i2c-test', 'am64-adma-pacing-test'] : []) + \
   qtests_cxl +                                                                                  \
   ['arm-cpu-features',
    'numa-test',
tests/qtest/ti-am64-i2c-test.c new file +137 -0
new file mode 100644
index 0000000000..eda8d8032f
@@ -0,0 +1,137 @@
+/*
+ * QTests for AM64x main_i2c0 controller
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "libqtest.h"
+
+/* main_i2c0 window: i2c@20000000, len 0x100. */
+#define I2C0_BASE 0x20000000ULL
+
+/* OMAP I2C V2 register offsets. */
+#define I2C_V2_SYSC          0x10
+#define I2C_V2_IRQSTATUS_RAW 0x24
+#define I2C_V2_IRQSTATUS     0x28
+#define I2C_V2_SYSS          0x90
+#define I2C_V2_CNT           0x98
+#define I2C_V2_DATA          0x9c
+#define I2C_V2_CON           0xa4
+#define I2C_V2_SA            0xac
+
+/* Register bits. */
+#define I2C_SYSC_SRST   (1 << 1)  /* SYSCONFIG soft reset */
+#define I2C_SYSS_RDONE  (1 << 0)  /* reset done */
+#define I2C_STAT_NACK   (1 << 1)  /* no acknowledgement */
+#define I2C_STAT_ARDY   (1 << 2)  /* register access ready */
+#define I2C_STAT_RRDY   (1 << 3)  /* receive data ready */
+
+#define I2C_CON_EN  (1 << 15)     /* module enable */
+#define I2C_CON_MST (1 << 10)     /* master mode */
+#define I2C_CON_STP (1 << 1)      /* stop condition */
+#define I2C_CON_STT (1 << 0)      /* start condition */
+
+/*
+ * SYSCONFIG.SRST must make SYSS.RDONE assert, or the firmware spins in
+ * the soft-reset poll.
+ */
+static void test_soft_reset_completes(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t syss = 0;
+
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+
+    for (int i = 0; i < 16; i++) {
+        syss = qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+        if (syss & I2C_SYSS_RDONE) {
+            break;
+        }
+    }
+    g_assert_cmphex(syss & I2C_SYSS_RDONE, ==, I2C_SYSS_RDONE);
+    qtest_quit(qts);
+}
+
+/*
+ * Reads from an address without a slave must raise NACK promptly, since a
+ * driver would otherwise wait forever for an I2C event. No slave is
+ * attached here, so any address does.
+ */
+static void test_nack_on_absent_slave(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt");
+    uint32_t stat;
+
+    /* Bring the controller out of reset as firmware does. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+    (void)qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+
+    /* Address 0x51 has no slave: one-byte master read. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SA, 0x51);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CNT, 1);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CON,
+                 I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
+
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, I2C_STAT_NACK);
+
+    /* IRQSTATUS mirrors NACK and is write-1-to-clear. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, I2C_STAT_NACK);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_IRQSTATUS, I2C_STAT_NACK);
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, 0);
+
+    qtest_quit(qts);
+}
+
+/*
+ * With a slave on the bus a one-byte master read has to ACK, return DATA
+ * and then finish with ARDY. An at24c EEPROM at 0x50 serves as the slave;
+ * it comes up erased, so offset 0 reads back as 0x00.
+ */
+static void test_eeprom_read_first_byte(void)
+{
+    QTestState *qts = qtest_init("-machine am64-virt "
+                                 "-device at24c-eeprom,address=0x50,"
+                                 "rom-size=4096");
+    uint32_t stat;
+    uint8_t b;
+
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SYSC, I2C_SYSC_SRST);
+    (void)qtest_readw(qts, I2C0_BASE + I2C_V2_SYSS);
+
+    /* One-byte read from 0x50; address pointer starts by 0. */
+    qtest_writew(qts, I2C0_BASE + I2C_V2_SA, 0x50);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CNT, 1);
+    qtest_writew(qts, I2C0_BASE + I2C_V2_CON,
+                 I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
+
+    /* Slave ACKed and data are ready. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_NACK, ==, 0);
+    g_assert_cmphex(stat & I2C_STAT_RRDY, ==, I2C_STAT_RRDY);
+
+    /* Offset 0 of the erased EEPROM reads as 0x00. */
+    b = qtest_readw(qts, I2C0_BASE + I2C_V2_DATA) & 0xff;
+    g_assert_cmphex(b, ==, 0x00);
+
+    /* Single-byte transfer complete: ARDY, no more RRDY. */
+    stat = qtest_readw(qts, I2C0_BASE + I2C_V2_IRQSTATUS_RAW);
+    g_assert_cmphex(stat & I2C_STAT_ARDY, ==, I2C_STAT_ARDY);
+    g_assert_cmphex(stat & I2C_STAT_RRDY, ==, 0);
+
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/am64/i2c/soft-reset", test_soft_reset_completes);
+    qtest_add_func("/am64/i2c/nack", test_nack_on_absent_slave);
+    qtest_add_func("/am64/i2c/eeprom-read", test_eeprom_read_first_byte);
+    return g_test_run();
+}
tests/unit/meson.build +1 -0
index dc3fb954c0..be2aab4274 100644
@@ -41,6 +41,7 @@ tests = {
   'test-bitops': [],
   'test-bitcnt': [],
   'test-qgraph': ['../qtest/libqos/qgraph.c'],
+  'test-k3-bootrom': ['../../hw/arm/k3-bootrom-parse.c'],
   'check-qom-interface': [qom],
   'check-qom-proplist': [qom],
   'test-qemu-opts': [],
tests/unit/test-k3-bootrom.c new file +263 -0
new file mode 100644
index 0000000000..b030d7df9b
@@ -0,0 +1,263 @@
+/*
+ * Unit tests for K3 boot-ROM combined-image parser
+ *
+ * Copyright (c) 2026 CMBLU Energy AG
+ * Author: Wadim Mueller <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/arm/k3-bootrom.h"
+
+/* Minimal DER emitters. */
+
+static GByteArray *der_tlv(uint8_t tag, const uint8_t *data, size_t len)
+{
+    GByteArray *a = g_byte_array_new();
+
+    g_byte_array_append(a, &tag, 1);
+    if (len < 0x80) {
+        uint8_t l = len;
+        g_byte_array_append(a, &l, 1);
+    } else if (len <= 0xffff) {
+        uint8_t l[3] = { 0x82, len >> 8, len & 0xff };
+        g_byte_array_append(a, l, 3);
+    } else {
+        g_assert_not_reached();
+    }
+    if (data) {
+        g_byte_array_append(a, data, len);
+    }
+    return a;
+}
+
+static GByteArray *der_wrap(uint8_t tag, GByteArray *inner)
+{
+    GByteArray *a = der_tlv(tag, inner->data, inner->len);
+    g_byte_array_unref(inner);
+    return a;
+}
+
+static void der_append(GByteArray *dst, GByteArray *src)
+{
+    g_byte_array_append(dst, src->data, src->len);
+    g_byte_array_unref(src);
+}
+
+static GByteArray *der_uint(uint64_t v)
+{
+    uint8_t buf[9];
+    int n = 0;
+    uint64_t t = v;
+
+    do {
+        n++;
+        t >>= 8;
+    } while (t);
+    if (v >> (n * 8 - 1) & 1) {
+        n++; /* leading zero keeps it positive */
+    }
+    for (int i = 0; i < n; i++) {
+        buf[i] = v >> ((n - 1 - i) * 8);
+    }
+    return der_tlv(0x02, buf, n);
+}
+
+/* OID 1.3.6.1.4.1.294.1.9 (ext_boot_info), pre-encoded TLV */
+static const uint8_t ext_boot_oid[] = {
+    0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x26, 0x01, 0x09
+};
+/* OID 2.16.840.1.101.3.4.2.3 (sha512), pre-encoded TLV */
+static const uint8_t sha512_oid[] = {
+    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
+};
+
+static GByteArray *der_component(uint32_t ctype, uint32_t core,
+                                 uint32_t opts, uint32_t dest, uint64_t size)
+{
+    GByteArray *seq = g_byte_array_new();
+    uint8_t dest_be[4] = { dest >> 24, dest >> 16, dest >> 8, dest };
+    uint8_t sha[64] = { 0 };
+
+    der_append(seq, der_uint(ctype));
+    der_append(seq, der_uint(core));
+    der_append(seq, der_uint(opts));
+    der_append(seq, der_tlv(0x04, dest_be, sizeof(dest_be)));
+    der_append(seq, der_uint(size));
+    g_byte_array_append(seq, sha512_oid, sizeof(sha512_oid));
+    der_append(seq, der_tlv(0x04, sha, sizeof(sha)));
+    return der_wrap(0x30, seq);
+}
+
+/*
+ * Layout follows u-boot tools/binman/btool/openssl.py
+ * x509_cert_rom_combined(): SBL, SYSFW, SYSFW-DATA payloads are after
+ * the certificate.
+ */
+static GByteArray *make_image(const uint8_t *sbl, size_t sbl_len)
+{
+    static const uint8_t sysfw_blob[16] = "SYSFW-payload";
+    static const uint8_t cfg_blob[8] = "BCFG";
+    GByteArray *info = g_byte_array_new();
+    GByteArray *ext, *cert, *img;
+
+    der_append(info, der_uint(sbl_len + sizeof(sysfw_blob)
+                              + sizeof(cfg_blob)));      /* extImgSize */
+    der_append(info, der_uint(3));                       /* numComp */
+    der_append(info, der_component(K3_COMP_TYPE_SBL, 16, 0,
+                                   0x70000000, sbl_len));
+    der_append(info, der_component(K3_COMP_TYPE_SYSFW, 0, 0,
+                                   0x44000, sizeof(sysfw_blob)));
+    der_append(info, der_component(K3_COMP_TYPE_SYSFW_DATA, 0, 0,
+                                   0x7b000, sizeof(cfg_blob)));
+    info = der_wrap(0x30, info);
+
+    /* extension is SEQ { OID, OCTETSTRING { info } } */
+    ext = g_byte_array_new();
+    g_byte_array_append(ext, ext_boot_oid, sizeof(ext_boot_oid));
+    der_append(ext, der_wrap(0x04, info));
+    ext = der_wrap(0x30, ext);
+
+    /* fake cert: top-level SEQUENCE around the extension */
+    cert = der_wrap(0x30, ext);
+
+    img = g_byte_array_new();
+    g_byte_array_append(img, cert->data, cert->len);
+    g_byte_array_unref(cert);
+    g_byte_array_append(img, sbl, sbl_len);
+    g_byte_array_append(img, sysfw_blob, sizeof(sysfw_blob));
+    g_byte_array_append(img, cfg_blob, sizeof(cfg_blob));
+    return img;
+}
+
+/* Certificate-only image with caller given component sizes. */
+static GByteArray *make_cert_with_sizes(const uint64_t *sizes, size_t n)
+{
+    GByteArray *info = g_byte_array_new();
+    GByteArray *ext;
+    uint64_t total = 0;
+
+    for (size_t i = 0; i < n; i++) {
+        total += sizes[i];
+    }
+    der_append(info, der_uint(total));                   /* extImgSize */
+    der_append(info, der_uint(n));                       /* numComp */
+    for (size_t i = 0; i < n; i++) {
+        der_append(info, der_component(K3_COMP_TYPE_SBL, 16, 0,
+                                       0x70000000, sizes[i]));
+    }
+    info = der_wrap(0x30, info);
+
+    ext = g_byte_array_new();
+    g_byte_array_append(ext, ext_boot_oid, sizeof(ext_boot_oid));
+    der_append(ext, der_wrap(0x04, info));
+    ext = der_wrap(0x30, ext);
+    return der_wrap(0x30, ext);
+}
+
+static void test_parse_ok(void)
+{
+    static const uint8_t sbl[32] = "SBL-payload";
+    GByteArray *img = make_image(sbl, sizeof(sbl));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_true(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_null(err);
+    g_assert_cmpuint(out.num_comps, ==, 3);
+    g_assert_cmpuint(out.comps[0].comp_type, ==, K3_COMP_TYPE_SBL);
+    g_assert_cmphex(out.comps[0].dest_addr, ==, 0x70000000);
+    g_assert_cmpuint(out.comps[0].comp_size, ==, sizeof(sbl));
+    g_assert_cmpuint(out.comps[0].payload_offset, ==, out.cert_len);
+    g_assert_cmphex(out.comps[1].dest_addr, ==, 0x44000);
+    g_assert_cmpuint(out.comps[2].payload_offset, ==,
+                     out.cert_len + sizeof(sbl) + 16);
+    g_assert_cmpint(memcmp(img->data + out.comps[0].payload_offset,
+                           sbl, sizeof(sbl)), ==, 0);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_not_der(void)
+{
+    static const uint8_t junk[64] = { 0xff, 0x00, 0x41 };
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(junk, sizeof(junk), &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+}
+
+static void test_parse_no_extension(void)
+{
+    /* Valid DER SEQUENCE, but no ext_boot_info OID. */
+    static const uint8_t seq[] = { 0x30, 0x03, 0x02, 0x01, 0x05 };
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(seq, sizeof(seq), &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+}
+
+static void test_parse_truncated_payload(void)
+{
+    static const uint8_t sbl[32] = "SBL-payload";
+    GByteArray *img = make_image(sbl, sizeof(sbl));
+    K3BootImage out;
+    Error *err = NULL;
+
+    /* Truncated payload: comp_size claims are beyond file. */
+    g_assert_false(k3_bootrom_parse(img->data, img->len - 20, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_size_exceeds_u32(void)
+{
+    /* comp_size wider than 32 bits must not truncate silently. */
+    static const uint64_t sizes[1] = { UINT32_MAX + 1ull };
+    GByteArray *img = make_cert_with_sizes(sizes, G_N_ELEMENTS(sizes));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+static void test_parse_size_sum_wraps_32bit(void)
+{
+    /*
+     * Each single size fits in 32 bits, but their sum must not wrap the
+     * running total.
+     */
+    static const uint64_t sizes[3] = {
+        0xf0000000, 0xf0000000, 0xf0000000
+    };
+    GByteArray *img = make_cert_with_sizes(sizes, G_N_ELEMENTS(sizes));
+    K3BootImage out;
+    Error *err = NULL;
+
+    g_assert_false(k3_bootrom_parse(img->data, img->len, &out, &err));
+    g_assert_nonnull(err);
+    error_free(err);
+    g_byte_array_unref(img);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    g_test_add_func("/k3-bootrom/parse-ok", test_parse_ok);
+    g_test_add_func("/k3-bootrom/not-der", test_parse_not_der);
+    g_test_add_func("/k3-bootrom/no-extension", test_parse_no_extension);
+    g_test_add_func("/k3-bootrom/truncated", test_parse_truncated_payload);
+    g_test_add_func("/k3-bootrom/size-exceeds-u32",
+                    test_parse_size_exceeds_u32);
+    g_test_add_func("/k3-bootrom/size-sum-wraps-32bit",
+                    test_parse_size_sum_wraps_32bit);
+    return g_test_run();
+}
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw

Re: [RFC PATCH v2 03/14] hw/sd/sdhci: complete non-interrupt ADMA descriptor chains in one pass

In reply to Wadim Mueller
Quoted 69 lines · Wadim Mueller

sdhci_do_adma() returns to the main loop after every descriptor and relies on a timer re-entry to pick up the next one. For a descriptor chain whose entries do not request an interrupt this makes the transfer rate depend from the virtual clock rather than on the guest's programming, which significantly slows down large transfers and makes guest-visible timing depend on host timer behaviour.

Keep processing the chain in the same invocation while no descriptor asks for an interrupt and the transfer has not finished, and only fall back to the deferred path when the guest actually requested a notification.

A qtest reproducer is added later in this series.

---
This patch can be dropped once Bin Meng's SDHCI series
https://patchwork.ozlabs.org/project/qemu-devel/list/?series=515264
(which needs series=513930 applied first) is merged - it covers the same AM64x failure, see
https://lore.kernel.org/qemu-devel/[email protected]/
It is included here only so that the series works on actual master.

 hw/sd/sdhci-internal.h |  9 +++++++++
 hw/sd/sdhci.c          | 23 +++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)
hw/sd/sdhci-internal.h +9 -0
index 4aeed120bf..e6ce12617e 100644
@@ -277,6 +277,15 @@ FIELD(SDHC_MAXCURR, V18_VDD2,         32, 8); /* since v4.20 */
 #define SDHC_INSERTION_DELAY            (NANOSECONDS_PER_SECOND)
 #define SDHC_TRANSFER_DELAY             100
 #define SDHC_ADMA_DESCS_PER_DELAY       5
+/*
+ * Upper bound on ADMA2 descriptors handled in a single sdhci_do_adma()
+ * call, as a safety valve against a malformed or circular descriptor
+ * list. A well-formed transfer terminates far below this via END or
+ * blkcnt == 0 (even a 4 GiB transfer built from 64 KiB TRAN descriptors
+ * is only ~64K descriptors); the bound merely guarantees the loop makes
+ * a decision instead of spinning forever.
+ */
+#define SDHC_ADMA_MAX_DESCRIPTORS       (1 << 20)
 #define SDHC_CMD_RESPONSE               (3 << 0)
 
 enum {
hw/sd/sdhci.c +6 -2
index e58a610397..9a5dd1d93f 100644
@@ -780,7 +780,6 @@ static void sdhci_do_adma(SDHCIState *s)
     const MemTxAttrs attrs = { .memory = true };
     ADMADescr dscr = {};
     MemTxResult res = MEMTX_ERROR;
-    int i;
 
     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN && !s->blkcnt) {
         /* Stop Multiple Transfer */
@@ -788,7 +787,27 @@ static void sdhci_do_adma(SDHCIState *s)
         return;
     }
 
-    for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
+    /*
+     * Process the descriptor chain to completion (END or blkcnt == 0),
+     * yielding to the guest only for a descriptor carrying the INT
+     * attribute (a DMA-boundary interrupt, handled at the end of the loop).
+     *
+     * Historically at most SDHC_ADMA_DESCS_PER_DELAY descriptors were

hw/sd/sdhci.c static void sdhci_do_adma(SDHCIState *s) · line 805

Quoted 10 lines
hw/sd/sdhci.c +10 -0
+     * handled per call before rescheduling SDHC_TRANSFER_DELAY ns later on
+     * QEMU_CLOCK_VIRTUAL. That pacing is only needed so a guest can observe
+     * the intermediate DMA-interrupt state; a bulk transfer that requests
+     * no interrupt does not need slicing, and throttling it across many
+     * virtual-clock round-trips can make it race a guest-side transfer
+     * timeout. Run such chains to completion in one call instead.
+     *
+     * SDHC_ADMA_MAX_DESCRIPTORS bounds the loop so a malformed or circular
+     * chain cannot spin here forever; on overflow, break to the reschedule
+     * path so the main loop stays responsive.

Hmm SDHC_ADMA_MAX_DESCRIPTORS is (1 << 20) which seems an awful lot to handle in dma session especially if this is a guest controlled thing.

hw/sd/sdhci.c static void sdhci_do_adma(SDHCIState *s) · line 810

Quoted 5 lines
hw/sd/sdhci.c +5 -0
+     */
+    for (unsigned int adma_descs = 0; ; adma_descs++) {
+        if (adma_descs >= SDHC_ADMA_MAX_DESCRIPTORS) {
+            break;
+        }

Seems an odd formulation when you have the for loop construct already. Although I see sdhci_do_adma already has multiple exit paths that skip the final timer reset so it is hard to follow the control flow anyway.

I wonder if there is a better way to structure this logic so its easier to see where irq's are set, transfers ended and timers scheduled for continuation?

Quoted 3 lines
hw/sd/sdhci.c +0 -0
         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
 
         get_adma_description(s, &dscr);
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro