Skip to contentSkip to conversation

sdhci: fix SDMA boundary bug

PATCH Superseded Tao Ding 2 participants 4 messages Jun 29 – Jun 30

Subsystems (2)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Tao Ding

author
#0 lore raw

v2 cover sdhci: fix SDMA boundary bug

Hi all,

This series fixes an SDMA multi-block transfer bug in the generic SDHCI controller.

v1 of this series was posted here:
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks

Changes in v1->v2:
* Reword the commit message of patch 1/2.
* Add a new qtest patch to cover the SDHCI SDMA case.
Based on the xilinx-zynq-a9 board, which is more compatible with generic sdhci, rather than the npcm7xx board.

Tao Ding (2):
fixed sdma boundary bug
qtest: add xilinx-zynq SDHCI sdma test

 hw/sd/sdhci.c                        |   2 +
 tests/qtest/meson.build              |   1 +
 tests/qtest/xilinx-zynq-sdhci-test.c | 178 +++++++++++++++++++++++++++
 3 files changed, 181 insertions(+)
 create mode 100644 tests/qtest/xilinx-zynq-sdhci-test.c

base-commit: 608ad9307d5b4797d2254ee3a4bad2bc8c8bcbae

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 fixed sdma boundary bug

During SDMA transfers, the controller raises an interrupt at buffer boundaries to request a system address update. But the emulation fails to clear the SDHC_DOING_READ/WRITE flags after DMA completion, so writes to SDHC_SYSAD are ignored. Clear these flags when the transfer ends.
(according to PartA2_SD_Host_Controller_Simplified_Specification_Ver2.00.pdf section 2.2.1)

---
Uboot test:
1. Prepare zImage, U-Boot, and rootfs.cpio.gz, and make sure
the U-Boot configuration enables MMC_SDHCI_SDMA.
More details can be found at:
https://lore.kernel.org/qemu-devel/[email protected]/T/#mcdf231e6bd762903f8393bf93253cbcb12ececcf
2. ./qemu-system-aarch64 -M xilinx-zynq-a9 -machine boot-mode=sd -m 1024 -display none -serial null -serial stdio -monitor none \
-device loader,file=u-boot-dtb.bin,addr=0x04000000,cpu-num=0 \
-drive file=zynq-sd.img,format=raw,if=sd -D qemu.log

After applying the fix, success load sd card:

U-Boot 2026.07-rc4-g1e80ee41441c (Jun 15 2026 - 19:05:05 +0800)

Model: Xilinx ZC702 board
DRAM:  ECC disabled 1 GiB
Core:  33 devices, 21 uclasses, devicetree: board
Flash: 0 Bytes
NAND:  0 MiB
MMC:   mmc@e0100000: 0

Loading Environment from FAT... *** Error - No Valid Environment Area found *** Warning - bad env area, using default environment

In:    serial@e0001000
Out:   serial@e0001000
Err:   serial@e0001000

Net:
ZYNQ GEM: e000b000, mdio bus e000b000, phyaddr 7, interface rgmii-id

Warning: ethernet@e000b000 (eth0) using random MAC address - 4a:63:5a:a4:d3:6b
eth0: ethernet@e000b000
Hit any key to stop autoboot: 0
Zynq> fatload mmc 0 ${kernel_addr_r} zImage
10838528 bytes read in 4316 ms (2.4 MiB/s)
Zynq> fatload mmc 0 ${ramdisk_addr_r} rootfs-arm32.cpio.gz
406531 bytes read in 199 ms (1.9 MiB/s)
Zynq> setenv ramdisk_size ${filesize}
Zynq> fatload mmc 0 ${fdt_addr_r} zynq-zc702.dtb
15754 bytes read in 21 ms (732.4 KiB/s)
Zynq> setenv bootargs console=ttyPS0,115200 earlycon ignore_loglevel rdinit=/init Zynq> bootz ${kernel_addr_r} ${ramdisk_addr_r}:${ramdisk_size} ${fdt_addr_r} Kernel image @ 0x2000000 [ 0x000000 - 0xa56200 ]
## Flattened Device Tree blob at 01f00000
Booting using the fdt blob at 0x1f00000
Working FDT set to 1f00000
Loading Ramdisk to 2ff9c000, end 2ffff403 ... OK
Loading Device Tree to 2ff95000, end 2ff9bd89 ... OK
Working FDT set to 2ff95000

Starting kernel ...

 hw/sd/sdhci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/sd/sdhci.c +2 -0
index c86dfa281f..0d87289ca4 100644
@@ -644,6 +644,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
                 s->data_count = 0;
             }
             if (page_aligned && boundary_count == 0) {
+                s->prnsts &= ~SDHC_DOING_READ;
                 break;
             }
         }
@@ -669,6 +670,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
                 }
             }
             if (page_aligned && boundary_count == 0) {
+                s->prnsts &= ~SDHC_DOING_WRITE;
                 break;
             }
         }
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] fixed sdma boundary bug

Quoted 8 lines · Tao Ding

During SDMA transfers, the controller raises an interrupt at buffer boundaries to request a system address update. But the emulation fails to clear the SDHC_DOING_READ/WRITE flags after DMA completion, so writes to SDHC_SYSAD are ignored. Clear these flags when the transfer ends.
(according to PartA2_SD_Host_Controller_Simplified_Specification_Ver2.00.pdf section 2.2.1)

The commit summary should be something like:

hw/sd: sdhci: Fix SDMA boundary bug

Quoted 68 lines

---
Uboot test:
1. Prepare zImage, U-Boot, and rootfs.cpio.gz, and make sure
the U-Boot configuration enables MMC_SDHCI_SDMA.
More details can be found at:
https://lore.kernel.org/qemu-devel/[email protected]/T/#mcdf231e6bd762903f8393bf93253cbcb12ececcf
2. ./qemu-system-aarch64 -M xilinx-zynq-a9 -machine boot-mode=sd -m 1024 -display none -serial null -serial stdio -monitor none \
-device loader,file=u-boot-dtb.bin,addr=0x04000000,cpu-num=0 \
-drive file=zynq-sd.img,format=raw,if=sd -D qemu.log

After applying the fix, success load sd card:

U-Boot 2026.07-rc4-g1e80ee41441c (Jun 15 2026 - 19:05:05 +0800)

Model: Xilinx ZC702 board
DRAM:  ECC disabled 1 GiB
Core:  33 devices, 21 uclasses, devicetree: board
Flash: 0 Bytes
NAND:  0 MiB
MMC:   mmc@e0100000: 0

Loading Environment from FAT... *** Error - No Valid Environment Area found *** Warning - bad env area, using default environment

In:    serial@e0001000
Out:   serial@e0001000
Err:   serial@e0001000

Net:
ZYNQ GEM: e000b000, mdio bus e000b000, phyaddr 7, interface rgmii-id

Warning: ethernet@e000b000 (eth0) using random MAC address - 4a:63:5a:a4:d3:6b
eth0: ethernet@e000b000
Hit any key to stop autoboot: 0
Zynq> fatload mmc 0 ${kernel_addr_r} zImage
10838528 bytes read in 4316 ms (2.4 MiB/s)
Zynq> fatload mmc 0 ${ramdisk_addr_r} rootfs-arm32.cpio.gz
406531 bytes read in 199 ms (1.9 MiB/s)
Zynq> setenv ramdisk_size ${filesize}
Zynq> fatload mmc 0 ${fdt_addr_r} zynq-zc702.dtb
15754 bytes read in 21 ms (732.4 KiB/s)
Zynq> setenv bootargs console=ttyPS0,115200 earlycon ignore_loglevel rdinit=/init Zynq> bootz ${kernel_addr_r} ${ramdisk_addr_r}:${ramdisk_size} ${fdt_addr_r} Kernel image @ 0x2000000 [ 0x000000 - 0xa56200 ]
## Flattened Device Tree blob at 01f00000
Booting using the fdt blob at 0x1f00000
Working FDT set to 1f00000
Loading Ramdisk to 2ff9c000, end 2ffff403 ... OK
Loading Device Tree to 2ff95000, end 2ff9bd89 ... OK
Working FDT set to 2ff95000

Starting kernel ...

 hw/sd/sdhci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/sd/sdhci.c +1 -0
index c86dfa281f..0d87289ca4 100644
@@ -644,6 +644,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
                 s->data_count = 0;
             }
             if (page_aligned && boundary_count == 0) {
+                s->prnsts &= ~SDHC_DOING_READ;
                 break;
             }
         }

hw/sd/sdhci.c static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)

Quoted 8 lines
hw/sd/sdhci.c +1 -1
@@ -669,6 +670,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
                 }
             }
             if (page_aligned && boundary_count == 0) {
+                s->prnsts &= ~SDHC_DOING_WRITE;
                 break;
             }
         }
--

I reconsidered this, and now think we should keep these bits on here. This is because BLKSIZE/BLKCNT register is also protected by !TRANSFERRING_DATA(prnsts) and if we clear the bits, that will accidentally allow overwriting LKSIZE/BLKCNT settings during a paused SDMA transfer due to boundary hit, which is definitely not we want. Even normal guest drivers won't do such things, QEMU shouldn't expose such a possibility to the guest.

Instead we should add another state to indicate that SDMA transfer is paused due to hitting the boundary and only with such state can we write the SYSAD register.

Regards,
Bin

author
#3 lore raw

PATCH v2 2/2 qtest: add xilinx-zynq SDHCI sdma test

In reply to Tao Ding

Thanks for the review comments.

Add a standalone qtest for the SDHCI controller on the
xilinx-zynq-a9 machine. This test covers the bug fixed by patch 1/2 in this series.

The test covers the SDMA boundary continuation case for a multi-block read. After the controller stops at a boundary, software updates SYSAD to continue the transfer. This verifies the generic SDHCI behavior that previously failed when the continuation SYSAD write was ignored and the transfer stalled.

---
Build:
mkdir build
cd build
../configure --target-list="arm-softmmu" --disable-slirp --enable-debug --disable-werror make -j4

Run:
QTEST_QEMU_BINARY=./qemu-system-arm \
tests/qtest/xilinx-zynq-sdhci-test

Before the fix, the test fails with a BLKCNT assertion:
ERROR:../tests/qtest/xilinx-zynq-sdhci-test.c:90:wait_for_block_count:
assertion failed (qtest_readw(qts, XILINX_ZYNQ_MMC_BA + SDHC_BLKCNT) == expected): (1 == 0)

After applying the fix, the test passes:
ok 1 /arm/xilinx-zynq-sdhci/sdma/read-boundary-continue

 tests/qtest/meson.build              |   1 +
 tests/qtest/xilinx-zynq-sdhci-test.c | 178 +++++++++++++++++++++++++++
 2 files changed, 179 insertions(+)
 create mode 100644 tests/qtest/xilinx-zynq-sdhci-test.c
tests/qtest/meson.build +1 -0
index 2ae720156f..c12a35721d 100644
@@ -244,6 +244,7 @@ qtests_arm = \
    config_all_devices.has_key('CONFIG_MUSICPAL') ? ['pflash-cfi02-test'] : []) + \
   (config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed : []) + \
   (config_all_devices.has_key('CONFIG_NPCM7XX') ? qtests_npcm7xx : []) + \
+  (config_all_devices.has_key('CONFIG_ZYNQ') ? ['xilinx-zynq-sdhci-test'] : []) + \
   (config_all_devices.has_key('CONFIG_GENERIC_LOADER') ? ['hexloader-test'] : []) + \
   (config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test', 'tpm-tis-i2c-swtpm-test'] : []) + \
   (config_all_devices.has_key('CONFIG_VEXPRESS') ? ['test-arm-mptimer'] : []) + \
tests/qtest/xilinx-zynq-sdhci-test.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.43.0