bcm2835_spi_reset() cleared CS, so neither DONE nor RXR is set
afterwards, but left the interrupt line as it was: a reset in the
middle of an interrupt-driven transfer (a guest reboot while
spi-bcm2835 waits for DONE) kept the line raised into the next boot.
Update the interrupt from the new register values.

Test that a reset during a transfer with INTD set drops the line.

Fixes: 28004fb74134 ("hw/ssi: Implement BCM2835 SPI Controller")
Signed-off-by: Lorand Marton <[email protected]>
---
 hw/ssi/bcm2835_spi.c           |  2 ++
 tests/qtest/bcm2835-spi-test.c | 42 ++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build        |  3 ++-
 3 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/bcm2835-spi-test.c

diff --git a/hw/ssi/bcm2835_spi.c b/hw/ssi/bcm2835_spi.c
index 01763c458c..4ab47787a4 100644
--- a/hw/ssi/bcm2835_spi.c
+++ b/hw/ssi/bcm2835_spi.c
@@ -246,6 +246,8 @@ static void bcm2835_spi_reset(DeviceState *dev)
     s->dlen = 0;
     s->ltoh = 0x1;
     s->dc = 0x30201020;
+
+    bcm2835_spi_update_int(s);
 }
 
 static const VMStateDescription vmstate_bcm2835_spi = {
diff --git a/tests/qtest/bcm2835-spi-test.c b/tests/qtest/bcm2835-spi-test.c
new file mode 100644
index 0000000000..4888171e5b
--- /dev/null
+++ b/tests/qtest/bcm2835-spi-test.c
@@ -0,0 +1,42 @@
+/*
+ * QTest testcase for the BCM2835 SPI controller
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/bitops.h"
+#include "libqtest.h"
+
+#define SPI0_BASE       0x3f204000
+#define SPI_CS          0x00
+#define SPI_FIFO        0x04
+
+#define CS_CLEAR_RXTX   (3 << 4)
+#define CS_TA           BIT(7)
+#define CS_INTD         BIT(9)
+
+/* A reset in the middle of a transfer drops the interrupt */
+static void test_reset_irq(void)
+{
+    QTestState *qts = qtest_init("-M raspi3b");
+
+    qtest_irq_intercept_out_named(qts, "/machine/soc/peripherals/bcm2835-spi0",
+                                  "sysbus-irq");
+    qtest_writel(qts, SPI0_BASE + SPI_CS, CS_CLEAR_RXTX | CS_TA);
+    qtest_writel(qts, SPI0_BASE + SPI_FIFO, 0xa5);
+    qtest_writel(qts, SPI0_BASE + SPI_CS, CS_TA | CS_INTD);
+    g_assert_true(qtest_get_irq(qts, 0));
+
+    qtest_system_reset(qts);
+    g_assert_false(qtest_get_irq(qts, 0));
+
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/bcm2835-spi/reset-irq", test_reset_irq);
+    return g_test_run();
+}
diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index 6f74b519c7..d4a20cd408 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -285,7 +285,8 @@ qtests_aarch64 = \
     ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) +               
                          \
   (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 
'fuzz-xlnx-dp-test'] : []) + \
   (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 
'xlnx-versal-trng-test'] : []) + \
-  (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test', 
'bcm2835-i2c-test', 'bcm2835-ic-test'] : []) +  \
+  (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test', 
'bcm2835-i2c-test', 'bcm2835-ic-test',
+                                                    'bcm2835-spi-test'] : []) 
+  \
   (config_all_accel.has_key('CONFIG_TCG') and                                  
          \
    config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test', 
'tpm-tis-i2c-swtpm-test'] : []) + \
   (config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed64 : []) + \
-- 
2.56.0


Reply via email to