hw: interrupt, reset and divider fixes for BCM2835 peripherals
Subsystems (4)
cover hw: interrupt, reset and divider fixes for BCM2835 peripherals
Four fixes for BCM2835 peripheral models that the raspi3b and raspi4b machines use, each with a qtest that fails without the fix:
- bcm2835_i2c: writing 1 to DONE in S did not lower the interrupt.
- bcm2835_spi: a reset left the interrupt line raised.
- bcm2835_systmr: a reset left raised interrupts raised, and a compare set before the reset still fired after it.
- bcm2835_cprman: a PLL channel divider of 0 divided by 255, not 256.
Patch 1 adds the BCM283x qtests to the Raspberry Pi section ofMAINTAINERS: the three that exist have no entry, and patches 3 to 5 add three more.
The fixes are independent bug fixes and apply to master.
I found these bugs while running Raspberry Pi OS images on a model of the Compute Module 4: https://github.com/LorandMarton/boardtwin. Its QEMU changes are on the boardtwin-develop branch of
https://github.com/LorandMarton/qemu. I intend to send the board model (raspicm4) after the 11.2 release.
Checked: each patch builds and passes its qtests; each new test fails with only its fix reverted; the aarch64 qtests pass (35, 3 skipped:
the slow cdrom test and the two swtpm tests).
Lorand Marton (5):
MAINTAINERS: list the BCM283x qtests under Raspberry Pi hw/i2c/bcm2835_i2c: update the interrupt when the status is cleared hw/ssi/bcm2835_spi: lower the interrupt on reset hw/timer/bcm2835_systmr: cancel the compares and lower the IRQs on
reset
hw/misc/bcm2835_cprman: a PLL channel divider of 0 divides by 256
MAINTAINERS | 1 + hw/i2c/bcm2835_i2c.c | 1 + hw/misc/bcm2835_cprman.c | 6 ++-- hw/ssi/bcm2835_spi.c | 2 ++ hw/timer/bcm2835_systmr.c | 4 +++ tests/qtest/bcm2835-cprman-test.c | 49 ++++++++++++++++++++++++++++ tests/qtest/bcm2835-i2c-test.c | 24 ++++++++++++++ tests/qtest/bcm2835-spi-test.c | 42 ++++++++++++++++++++++++ tests/qtest/bcm2835-systmr-test.c | 54 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 4 ++- 10 files changed, 183 insertions(+), 4 deletions(-) create mode 100644 tests/qtest/bcm2835-cprman-test.c create mode 100644 tests/qtest/bcm2835-spi-test.c create mode 100644 tests/qtest/bcm2835-systmr-test.c
base-commit: 56afd82bd4db9688f1c1c9d0e2ea163b8cde3283
Signature · 1 line
2.56.0
PATCH 1/5 MAINTAINERS: list the BCM283x qtests under Raspberry Pi
tests/qtest/bcm2835-dma-test.c, bcm2835-i2c-test.c and bcm2835-ic-test.c test Raspberry Pi devices, but no MAINTAINERS section lists them, so get_maintainer.pl names no one for a change to them. Add them, and the BCM283x qtests that follow, to the Raspberry Pi section.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1046,6 +1046,7 @@ F: include/hw/*/bcm283* F: docs/system/arm/raspi.rst F: tests/functional/arm/test_raspi2.py F: tests/functional/aarch64/test_raspi*.py +F: tests/qtest/bcm283*-test.c Real View M: Peter Maydell <[email protected]>
Signature · 1 line
2.56.0
Re: [PATCH 1/5] MAINTAINERS: list the BCM283x qtests under Raspberry Pi
Quoted 24 lines · Lorand Marton
tests/qtest/bcm2835-dma-test.c,bcm2835-i2c-test.candbcm2835-ic-test.ctest Raspberry Pi devices, but noMAINTAINERSsection lists them, so get_maintainer.pl names no one for a change to them. Add them, and the BCM283x qtests that follow, to the Raspberry Pi section.Signed-off-by Lorand Marton <[email protected]>--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)MAINTAINERS +1
-1@@ -1046,6 +1046,7 @@ F: include/hw/*/bcm283* F: docs/system/arm/raspi.rst F: tests/functional/arm/test_raspi2.py F: tests/functional/aarch64/test_raspi*.py +F: tests/qtest/bcm283*-test.c Real View M: Peter Maydell <[email protected]> --
Quoted 1 line
2.56.0
thanks
-- PMM
PATCH 2/5 hw/i2c/bcm2835_i2c: update the interrupt when the status is cleared
In reply to Lorand MartonWriting 1 to DONE, ERR or CLKT in the S register clears the bit, but bcm2835_i2c_write() did not recompute the interrupt, so with INTD set the DONE interrupt stayed raised after the guest acknowledged it. Linux i2c-bcm2835 clears C before it clears S and so lowers the line anyway; a driver that clears DONE in S and keeps INTD set took the interrupt again and again.
Update the interrupt after the write, as the C and FIFO writes do.
Test that clearing DONE lowers the interrupt.
--- hw/i2c/bcm2835_i2c.c | 1 + tests/qtest/bcm2835-i2c-test.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
hw/i2c/bcm2835_i2c.c +1 -0
@@ -166,6 +166,7 @@ static void bcm2835_i2c_write(void *opaque, hwaddr addr,/* Clear DONE, CLKT and ERR by writing 1 */ s->s &= ~(writeval & (BCM2835_I2C_S_DONE |BCM2835_I2C_S_ERR |BCM2835_I2C_S_CLKT )); +bcm2835_i2c_update_interrupt (s);break ;case BCM2835_I2C_DLEN : s->dlen = writeval;
tests/qtest/bcm2835-i2c-test.c +24 -0
@@ -89,6 +89,29 @@ static void test_i2c_read_write(gconstpointer data) } +/* Clearing DONE in the status register lowers the DONE interrupt */ +static void test_i2c_irq (void ) +{ +uint32_t base_addr = bsc_base_addrs[0 ]; + +qtest_irq_intercept_out_named (global_qtest, +"/machine/soc/peripherals/bcm2835-i2c0" , +"sysbus-irq" ); + +writel (base_addr +BCM2835_I2C_A ,0x50 ); +writel (base_addr +BCM2835_I2C_DLEN ,1 ); +writel (base_addr +BCM2835_I2C_C ,BCM2835_I2C_C_I2CEN | +BCM2835_I2C_C_INTD |BCM2835_I2C_C_ST |BCM2835_I2C_C_CLEAR ); +writel (base_addr +BCM2835_I2C_FIFO ,TMP105_REG_T_HIGH ); +g_assert_cmphex (readl (base_addr +BCM2835_I2C_S ) &BCM2835_I2C_S_DONE , + ==, BCM2835_I2C_S_DONE); +g_assert_true (get_irq (0 )); + +writel (base_addr +BCM2835_I2C_S ,BCM2835_I2C_S_DONE ); +g_assert_false (get_irq (0 )); +writel (base_addr +BCM2835_I2C_C ,0 ); +} +int main (int argc,char **argv) {int ret;
@@ -102,6 +125,7 @@ int main(int argc, char **argv)qtest_add_data_func (test_name, (void *)(intptr_t ) i, test_i2c_read_write); } +qtest_add_func ("/bcm2835/bcm2835-i2c0/irq" , test_i2c_irq);/* Run I2C tests with TMP105 slaves on all three buses */ qtest_start("-M raspi3b "
Signature · 1 line
2.56.0
Re: [PATCH 2/5] hw/i2c/bcm2835_i2c: update the interrupt when the status is cleared
Quoted 8 lines · Lorand Marton
Writing 1 to DONE, ERR or CLKT in the S register clears the bit, but
bcm2835_i2c_write()did not recompute the interrupt, so with INTD set the DONE interrupt stayed raised after the guest acknowledged it. Linux i2c-bcm2835 clears C before it clears S and so lowers the line anyway; a driver that clears DONE in S and keeps INTD set took the interrupt again and again.
Quoted 7 lines
Update the interrupt after the write, as the C and FIFO writes do.
Test that clearing DONE lowers the interrupt.
Fixes 9cf3bc65afdb ("hw/i2c: Implement Broadcom Serial Controller (BSC)")Signed-off-by Lorand Marton <[email protected]>---
thanks
-- PMM
PATCH 3/5 hw/ssi/bcm2835_spi: lower the interrupt on reset
In reply to Lorand Martonbcm2835_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.
--- 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
hw/ssi/bcm2835_spi.c +2 -0
@@ -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 = {
tests/qtest/bcm2835-spi-test.c new file +42 -0
42 lines not on this page · raw message
tests/qtest/meson.build +2 -1
@@ -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 : []) + \
Signature · 1 line
2.56.0
Re: [PATCH 3/5] hw/ssi/bcm2835_spi: lower the interrupt on reset
Quoted 23 lines · Lorand Marton
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.chw/ssi/bcm2835_spi.c +0
-0
hw/ssi/bcm2835_spi.c static void bcm2835_spi_reset(DeviceState *dev) · line 251
Quoted 6 lines
hw/ssi/bcm2835_spi.c +2
-0@@ -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); }
No, device reset methods should not call qemu_set_irq on their outbound IRQ lines. The qemu_irq itself has no state, so on reset the expectation is that this device resets its internal state, and the device it's connecting to will also reset its internal state (which will be to the "signal is not asserted" state)". Calling qemu_set_irq() from reset runs into problems with ordering of reset between different devices.
Quoted 4 lines
hw/ssi/bcm2835_spi.c +4
-0+/* A reset in the middle of a transfer drops the interrupt */ +static void test_reset_irq (void ) +{ +QTestState *qts =qtest_init ("-M raspi3b" );
hw/ssi/bcm2835_spi.c static void bcm2835_spi_reset(DeviceState *dev)
Quoted 10 lines
hw/ssi/bcm2835_spi.c +10
-0+ +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 ));
This test is really running into the fact that qtest's irq state for intercepted IRQs is not cleaned up on reset; so you get a bogus answer if you call qtest_get_irq() after qtest_system_reset().
-- PMM
PATCH 4/5 hw/timer/bcm2835_systmr: cancel the compares and lower the IRQs on reset
In reply to Lorand Martonbcm2835_systmr_reset() cleared the match bits and the compare registers, but a channel whose match had raised its interrupt kept the line raised, and a compare armed before the reset still fired after it, setting its match bit and raising the interrupt in a guest that had not armed it. Both happen when a guest reboots with a system timer channel in use (the firmware or a bare-metal guest's tick).
Delete the timers and lower the interrupts in the reset, as the register values now say.
Test that a reset drops a raised channel and that a compare set before it does not fire.
--- hw/timer/bcm2835_systmr.c | 4 +++ tests/qtest/bcm2835-systmr-test.c | 54 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 +- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/bcm2835-systmr-test.c
hw/timer/bcm2835_systmr.c +4 -0
@@ -123,6 +123,10 @@ static void bcm2835_systmr_reset(DeviceState *dev)BCM2835SystemTimerState *s =BCM2835_SYSTIMER (dev);memset (&s->reg,0 ,sizeof (s->reg)); +for (size_t i =0 ; i <ARRAY_SIZE (s->tmr); i++) { +timer_del (&s->tmr[i].timer); +qemu_set_irq (s->tmr[i].irq,0 ); + } }static void bcm2835_systmr_realize (DeviceState *dev,Error **errp)
tests/qtest/bcm2835-systmr-test.c new file +54 -0
54 lines not on this page · raw message
tests/qtest/meson.build +1 -1
@@ -286,7 +286,7 @@ qtests_aarch64 = \ (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', - 'bcm2835-spi-test'] : []) + \ + 'bcm2835-spi-test', 'bcm2835-systmr-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 : []) + \
Signature · 1 line
2.56.0
Re: [PATCH 4/5] hw/timer/bcm2835_systmr: cancel the compares and lower the IRQs on reset
Quoted 28 lines · Lorand Marton
bcm2835_systmr_reset()cleared the match bits and the compare registers, but a channel whose match had raised its interrupt kept the line raised, and a compare armed before the reset still fired after it, setting its match bit and raising the interrupt in a guest that had not armed it. Both happen when a guest reboots with a system timer channel in use (the firmware or a bare-metal guest's tick).Delete the timers and lower the interrupts in the reset, as the register values now say.
Test that a reset drops a raised channel and that a compare set before it does not fire.
Fixes be95dffa326a ("hw/timer/bcm2835: Support the timer COMPARE registers")Signed-off-by Lorand Marton <[email protected]>--- hw/timer/bcm2835_systmr.c | 4 +++ tests/qtest/bcm2835-systmr-test.c | 54 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 +- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/bcm2835-systmr-test.chw/timer/bcm2835_systmr.c +0
-0
hw/timer/bcm2835_systmr.c static void bcm2835_systmr_reset(DeviceState *dev) · line 130
Quoted 8 lines
hw/timer/bcm2835_systmr.c +4
-0@@ -123,6 +123,10 @@ static void bcm2835_systmr_reset(DeviceState *dev)BCM2835SystemTimerState *s =BCM2835_SYSTIMER (dev);memset (&s->reg,0 ,sizeof (s->reg)); +for (size_t i =0 ; i <ARRAY_SIZE (s->tmr); i++) { +timer_del (&s->tmr[i].timer); +qemu_set_irq (s->tmr[i].irq,0 ); + } }
The timer_del() is correct, the qemu_set_irq() is not.
-- PMM
PATCH 5/5 hw/misc/bcm2835_cprman: a PLL channel divider of 0 divides by 256
In reply to Lorand MartonThe 8-bit DIV field of an A2W PLL channel divides by 256 when it is 0, as Linux clk-bcm2835 describes it (CLK_DIVIDER_MAX_AT_ZERO on the PLL
dividers: 2^width at 0). pll_channel_update() took 0 as 255, so the channel and every clock fed from it ran 0.4% fast, and QEMU and the guest disagreed on its rate.
Test the rate of PLLD_PER against PLLD with the divider at 0.
--- hw/misc/bcm2835_cprman.c | 6 ++-- tests/qtest/bcm2835-cprman-test.c | 49 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 3 +- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/qtest/bcm2835-cprman-test.c
hw/misc/bcm2835_cprman.c +3 -3
@@ -184,10 +184,10 @@ static void pll_channel_update(CprmanPllChannelState *channel)if (!div) {/* -* It seems that when the divider value is 0, it is considered as -* being maximum by the hardware (see the Linux driver). +* A divider of 0 divides by 256, one more than the largest value +* the field holds (Linux clk-bcm2835 uses CLK_DIVIDER_MAX_AT_ZERO). */ - div = R_A2W_PLLx_CHANNELy_DIV_MASK; + div =1 << R_A2W_PLLx_CHANNELy_DIV_LENGTH; }/* Some channels have an additional fixed divider */
tests/qtest/bcm2835-cprman-test.c new file +49 -0
49 lines not on this page · raw message
tests/qtest/meson.build +2 -1
@@ -286,7 +286,8 @@ qtests_aarch64 = \ (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', - 'bcm2835-spi-test', 'bcm2835-systmr-test'] : []) + \ + 'bcm2835-cprman-test', 'bcm2835-spi-test', + 'bcm2835-systmr-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 : []) + \
Signature · 1 line
2.56.0
Re: [PATCH 5/5] hw/misc/bcm2835_cprman: a PLL channel divider of 0 divides by 256
Quoted 23 lines · Lorand Marton
The 8-bit DIV field of an A2W PLL channel divides by 256 when it is 0, as Linux clk-bcm2835 describes it (
CLK_DIVIDER_MAX_AT_ZEROon the PLL
dividers: 2^width at 0).pll_channel_update()took 0 as 255, so the channel and every clock fed from it ran 0.4% fast, and QEMU and the guest disagreed on its rate.Test the rate of
PLLD_PERagainst PLLD with the divider at 0.Fixes 957458111280 ("hw/misc/bcm2835_cprman: implement PLL channels behaviour")Signed-off-by Lorand Marton <[email protected]>--- hw/misc/bcm2835_cprman.c | 6 ++-- tests/qtest/bcm2835-cprman-test.c | 49 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 3 +- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/qtest/bcm2835-cprman-test.chw/misc/bcm2835_cprman.c +0
-0
hw/misc/bcm2835_cprman.c static void pll_channel_update(CprmanPllChannelState *channel) · line 190
Quoted 10 lines
hw/misc/bcm2835_cprman.c +3
-3@@ -184,10 +184,10 @@ static void pll_channel_update(CprmanPllChannelState *channel)if (!div) {/* -* It seems that when the divider value is 0, it is considered as -* being maximum by the hardware (see the Linux driver). +* A divider of 0 divides by 256, one more than the largest value +* the field holds (Linux clk-bcm2835 uses CLK_DIVIDER_MAX_AT_ZERO). */ - div = R_A2W_PLLx_CHANNELy_DIV_MASK; + div =1 << R_A2W_PLLx_CHANNELy_DIV_LENGTH;
This kind of change should reference the hardware documentation, not the Linux kernel behaviour. What does the actual hardware do?
thanks
-- PMM