drivers: spi: stm32: use DMA with buffers in cacheable memory (e.g. external PSRAM)
Summary
spi_stm32 does not use DMA for a buffer that is not in non-cacheable memory:
transceive()falls back to interrupt or polling mode since #116569 (spi_stm32.c#L2092-L2104);- the RTIO path,
spi_stm32_iodev_msg_start(), fails the transfer with-EINVAL(spi_stm32.c#L1029-L1033).
A buffer is accepted only if stm32_buf_in_nocache()
(stm32_cache.c#L12-L55)
places it in the image's internal SRAM, in __nocache, in a devicetree region marked
DT_MEM_ARM_MPU_RAM_NOCACHE, or in rodata.
External RAM behind the data cache never qualifies — for example PSRAM or SDRAM on OCTOSPI/HSPI/FMC with DCACHE1 on STM32U5. That leaves two poor options:
- keep the external RAM cached and give up SPI DMA for every buffer placed there, so SPI transfers become CPU-bound;
- make the external RAM, or a region of it, non-cacheable, so every CPU access to it is uncached. External serial memory is much slower without the cache (numbers below).
On Cortex-M7 parts the same check sends every buffer outside __nocache to the fallback,
stack buffers included (see the display case reported in #116569).
In #116569 @mathieuchopstm asked "Wouldn't flushing/invalidating D$ be simpler...?". This issue proposes exactly that.
Describe the solution you'd like
Maintain the cache in the driver, as other STM32 drivers in the tree already do:
- I2C:
i2c_stm32_xfer_will_use_dma()(i2c_stm32_common.c#L38) flushes a TX buffer and uses DMA; for RX it uses DMA when the address and the length are multiples ofCONFIG_DCACHE_LINE_SIZE, anddma_finish()(i2c_stm32_v2.c#L236) invalidates it afterwards; - SDMMC: clean and invalidate before the RX DMA (sdmmc_stm32.c#L566), invalidate after it (#L584).
For spi_stm32, in both the transceive() and the RTIO paths:
| Buffer | Action |
|---|---|
| in non-cacheable memory | as today, no cache maintenance |
| TX, cacheable | sys_cache_data_flush_range() over the bytes the DMA reads, then DMA |
| RX, cacheable, start and length aligned to the cache line | sys_cache_data_flush_and_invd_range() before the transfer (a dirty line evicted during the transfer would overwrite data the DMA has already written), sys_cache_data_invd_range() after it |
| RX, cacheable, not aligned | keep the fallback from #116569 |
Question for the maintainers: would an opt-in (Kconfig) for unaligned RX buffers be
acceptable? With it, the application guarantees that the CPU does not write the cache lines at
the ends of an RX buffer while the transfer runs. An example is net_buf pools with
CONFIG_NET_BUF_ALIGNMENT equal to the cache line size: each buffer owns whole lines, but a
transfer into it may start or end in the middle of a line.
This depends on #120455. Until then, every cache_stm32 range operation also covers the line
after the range, so the invalidate after an RX drops an unrelated neighbouring line.
I can open a PR once the direction is agreed.
Alternatives
- Non-cacheable MPU region in external memory for DMA buffers. Works with today's driver. But every CPU access to that region is uncached, and the application has to place each buffer or pool that can reach SPI into that region.
- Bounce buffer in internal SRAM inside the driver. Every transfer pays a copy, and the buffer takes internal SRAM for the largest transfer.
- Status quo (the fallback). SPI transfers from external memory are CPU-bound (numbers below).
Additional Context
Hardware. STM32U585 with 8 MB QPI PSRAM, memory-mapped at 0x70000000 through OCTOSPI,
DCACHE1 enabled. SPI2 runs at 40 MHz and is shared by a W5500 (Ethernet) and an ESP32-C6
(Wi-Fi over esp_hosted). The application (Matter over Wi-Fi and Ethernet) keeps a large part
of its data in PSRAM, including network buffers.
What SPI DMA is worth on this bus. Buffers in internal SRAM in both columns; the only
change is dmas on SPI2. Mbit/s; UDP is the zperf upload ceiling (1000-byte datagrams, 10 s),
TCP is zperf download.
| SPI2 without DMA | SPI2 with DMA | |
|---|---|---|
| UDP TX, Ethernet | 2.15 | 3.16–3.17 |
| UDP TX, Wi-Fi | 1.66 | 3.72–3.76 |
| TCP RX, Ethernet | 1.52–1.63 | 1.81–1.82 |
| TCP RX, Wi-Fi | 0.64–0.77 | 1.84–2.27 |
Without DMA the transfer is CPU-bound: thread statistics show about 0 % idle during the UDP transfer (measured with the buffers in PSRAM). With a buffer in PSRAM, today's driver takes the left column.
What the cache is worth for external memory. DCACHE1 on STM32U5 caches only external memory. Enabling it gave, on the same board and the same test:
| DCACHE1 off | DCACHE1 on | |
|---|---|---|
| LittleFS file read | 779 KB/s | 1.53 MB/s |
fs_statvfs() |
27 ms | 5.5 ms |
Range maintenance, tested on hardware. We run spi_stm32 with range maintenance for
buffers in cacheable memory, together with #120455, and SPI2 DMA directly into PSRAM. Mbit/s:
| PSRAM, SPI2 without DMA | PSRAM, SPI2 with DMA | |
|---|---|---|
| UDP TX, Ethernet | 2.07 | 2.48–2.49 |
| UDP TX, Wi-Fi | 1.47 | 2.25–2.26 |
| TCP RX, Ethernet | 1.41–1.49 | 1.40–1.47 |
| TCP RX, Wi-Fi | 0.89–1.06 | 1.64–1.65 |
0 SPI errors and 0 UDP loss. Besides DMA, the right column differs in
CONFIG_NET_BUF_ALIGNMENT=16 (the DCACHE1 line size) and in two separate fixes for the
spi_stm32 DMA path, which I will report on their own: an RX overrun during a DMA transfer is
not detected and the transfer ends in the 1 s timeout, and master receiver auto-suspend
(MASRX). DMA into slower memory is what made the overrun show up.
Why #120455 is a prerequisite. Without it, the invalidate after an esp_hosted RX into
PSRAM also dropped the first cache line of the next object, which holds the wait queue of the
driver's rx_sem. Under TCP load the kernel then asserted in pended_on_thread() from the
data-ready interrupt.
Source: zephyrproject-rtos/zephyr