dmaengine: dw-axi-dmac: report paused state and residue in tx_status
Subsystems (2)
PATCH v7 1/1 dmaengine: dw-axi-dmac: report paused state and residue in tx_status
The driver implements device_pause/device_resume, but device_tx_status keeps reporting DMA_IN_PROGRESS for a paused channel, so clients cannot tell a paused channel apart from a running one. Report DMA_PAUSED when the channel is paused and the cookie is still in flight, including for callers that pass a NULL dma_tx_state, and clear the stale is_paused flag in dma_chan_terminate_all(), which disables the channel and thus implicitly cancels the paused state.
Also, the residue of an in-flight transfer is currently derived from the number of completed LLI blocks, so it only advances in block-size steps and stays stale for the duration of a large block. Read the current hardware pointer (CH_SAR for MEM_TO_DEV and MEM_TO_MEM, CH_DAR for DEV_TO_MEM) and walk the descriptor's LLIs to compute how many bytes have actually been transferred. The 64-bit pointer is sampled with a tearing-safe double read, as the transfer may be running concurrently. The pointer is only consulted for the descriptor most recently programmed into the hardware, tracked in the previously unused chan->desc field; any other descriptor has not been started yet and keeps reporting its full length. When the transfer has just completed but the descriptor has not been reaped yet, the pointer sits at the end of the last block and the residue naturally reads as 0. The pointer registers are pre-loaded with the first block's addresses when the channel is programmed, so a stale pointer left by a previous transfer (e.g. when its buffer is reused) can never be observed before the first LLI fetch completes. Cyclic descriptors keep the block-granular accounting.
Tested on an FPGA platform.
---
Changes in v7:
- Rebase onto dmaengine/next at 2e02fb103be0 ("Linux 7.4-rc1").
- Link to v6: https://lore.kernel.org/dmaengine/[email protected]/
Changes in v6:
- Pre-load CH_SAR/CH_DAR with the first block's addresses when
programming the channel, so a stale pointer from a previous transfer
can never be observed before the first LLI fetch, even if its buffer
is reused for the new one (discussed with Frank Li).
- Link to v5: https://lore.kernel.org/dmaengine/[email protected]/
Changes in v5 (thanks to Frank Li's review):
- Rewrite the tearing-safe register read as a do/while loop.
- Drop the "never report full completion while the channel is enabled"
clamp: chan->desc matching already keeps never-started descriptors
away from the stale pointer, and the residual stale-read window is bounded by the first LLI fetch after channel enable.
- Use min() instead of min_t().
- Link to v4: https://lore.kernel.org/dmaengine/[email protected]/
Changes in v4:
- Track which descriptor the hardware pointer registers belong to via
the (previously unused) chan->desc field instead of testing the head
of desc_issued: a queued-but-never-started descriptor must not be
matched against the stale pointer left by a previous transfer, which could otherwise falsely report full completion when its buffer is
reused.
- Clear chan->desc when its descriptor is reaped or the channel is terminated.
- Link to v3: https://lore.kernel.org/dmaengine/[email protected]/
Changes in v3:
- Report DMA_PAUSED also to callers passing a NULL dma_tx_state.
- Never report full completion while the channel enable bit is still
set, so a stale pointer (e.g. a new transfer reusing the buffer of a just-finished one) is not mistaken for completion.
- Link to v2: https://lore.kernel.org/dmaengine/[email protected]/
Changes in v2:
- Sample the 64-bit SAR/DAR with a tearing-safe double read instead of lo_hi_readq(), avoiding a torn pointer when the low half wraps.
- Read the hardware pointer for the in-flight descriptor even after the hardware has just completed it (channel enable self-cleared, IRQ not
yet handled), so the residue reads 0 instead of jumping back to the
full length.
- Link to v1: https://lore.kernel.org/dmaengine/[email protected]/
--- .../dma/dw-axi-dmac/dw-axi-dmac-platform.c | 113 ++++++++++++++++-- 1 file changed, 105 insertions(+), 8 deletions(-)
drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c +105 -8
@@ -355,33 +355,110 @@ static void vchan_desc_put(struct virt_dma_desc *vdesc)axi_desc_put (vd_to_axi_desc (vdesc)); } +/* +* Read a 64-bit channel address register while the transfer may be +* running. The two 32-bit halves cannot be read atomically on all +* platforms, so sample the high half before and after the low half +* and retry if it moved (the low half wrapped in between). +*/ +static u64 axi_chan_readq (struct axi_dma_chan *chan,u32 reg) +{ +u32 hi, lo, hi2; + +do { + hi =readl (chan->chan_regs + reg +4 ); + lo =readl (chan->chan_regs + reg); + hi2 =readl (chan->chan_regs + reg +4 ); + }while (unlikely (hi != hi2)); + +return (u64 )hi <<32 | lo; +} + +/* +* Return the number of bytes already transferred by the descriptor +* currently on the hardware (running, paused or just completed), based +* on the current read or write position: CH_SAR for MEM_TO_DEV and +* MEM_TO_MEM, CH_DAR for DEV_TO_MEM. Must be called with vc.lock held. +*/ +static u32 axi_chan_get_xferred (struct axi_dma_chan *chan, +struct axi_dma_desc *desc) +{ +struct axi_dma_hw_desc *hw_desc; +bool dst = chan->direction ==DMA_DEV_TO_MEM ; +u64 pos, start; +u32 xferred =0 ; +int i; + + pos =axi_chan_readq (chan, dst ?CH_DAR :CH_SAR ); + +for (i =0 ; i < desc->nr_hw_descs; i++) { + hw_desc = &desc->hw_desc[i]; + start =le64_to_cpu (dst ? hw_desc->lli->dar : hw_desc->lli->sar); + +/* Current position is inside this block: partial progress */ +if (pos >= start && pos <= start + hw_desc->len) +return xferred + (u32)(pos - start); + + xferred += hw_desc->len; + } + +/* Position doesn't match any block, be conservative */ +return 0 ; +} +static enum dma_status dma_chan_tx_status (struct dma_chan *dchan,dma_cookie_t cookie,struct dma_tx_state *txstate) {struct axi_dma_chan *chan =dchan_to_axi_dma_chan (dchan);struct virt_dma_desc *vdesc; +struct axi_dma_desc *desc;enum dma_status status;u32 completed_length;unsigned long flags; -u32 completed_blocks;size_t bytes =0 ;u32 length; -u32 len; status =dma_cookie_status (dchan, cookie, txstate); -if (status ==DMA_COMPLETE || !txstate) +if (status ==DMA_COMPLETE )return status;spin_lock_irqsave (&chan->vc.lock, flags); +if (chan->is_paused && status ==DMA_IN_PROGRESS ) + status =DMA_PAUSED ; + +if (!txstate) { +spin_unlock_irqrestore (&chan->vc.lock, flags); +return status; + } + vdesc =vchan_find_desc (&chan->vc, cookie);if (vdesc) { - length =vd_to_axi_desc (vdesc)->length; - completed_blocks =vd_to_axi_desc (vdesc)->completed_blocks; - len =vd_to_axi_desc (vdesc)->hw_desc[0 ].len; - completed_length = completed_blocks * len; - bytes = length - completed_length; + desc =vd_to_axi_desc (vdesc); + length = desc->length; + +if (chan->cyclic) { + completed_length = desc->completed_blocks * + desc->hw_desc[0 ].len; + }else if (desc == chan->desc) { +/* +* chan->desc is the descriptor last programmed into +* the hardware, so the pointer registers belong to +* it; never match a queued-but-never-started +* descriptor against the stale pointer left by a +* previous transfer. If the transfer has just +* finished but the interrupt has not reaped the +* descriptor yet, the pointer sits at the end and +* the residue reads 0. +*/ + completed_length =axi_chan_get_xferred (chan, desc); + }else { +/* Still queued, nothing transferred yet */ + completed_length =0 ; + } + + bytes = length -min (completed_length, length); }spin_unlock_irqrestore (&chan->vc.lock, flags);
@@ -430,6 +507,7 @@ static void axi_chan_block_xfer_start(struct axi_dma_chan *chan, {u32 priority = chan->chip->dw->hdata->priority[chan->id];struct axi_dma_chan_config config = {}; +struct axi_dma_lli *lli = first->hw_desc[0 ].lli;u32 irq_mask;u8 lms =0 ;/* Select AXI0 master for LLI fetching */
@@ -471,6 +549,19 @@ static void axi_chan_block_xfer_start(struct axi_dma_chan *chan, }axi_chan_config_write (chan, &config); +/* +* Pre-load the pointer registers with the first block's addresses. +* The hardware reloads them with the same values once it fetches +* the first LLI; in between, a reader observes the start of the +* first block instead of a stale pointer left by a previous +* transfer (which may alias the new one if the buffer is reused). +*/ +axi_chan_iowrite64 (chan,CH_SAR ,le64_to_cpu (lli->sar)); +axi_chan_iowrite64 (chan,CH_DAR ,le64_to_cpu (lli->dar)); + +/* The hardware pointer registers now belong to this descriptor */ + chan->desc = first; +write_chan_llp (chan, first->hw_desc[0 ].llp | lms); irq_mask =DWAXIDMAC_IRQ_DMA_TRF |DWAXIDMAC_IRQ_ALL_ERR ;
@@ -1076,6 +1167,8 @@ static noinline void axi_chan_handle_err(struct axi_dma_chan *chan, u32 status) }/* Remove the completed descriptor from issued list */ list_del (&vd->node); +if (chan->desc ==vd_to_axi_desc (vd)) + chan->desc =NULL ;/* WARN about bad descriptor */ dev_err(vchan_chan_dev(&chan->vc),
@@ -1139,6 +1232,8 @@ static void axi_chan_block_xfer_complete(struct axi_dma_chan *chan) }else {/* Remove the completed descriptor from issued list before completing */ list_del (&vd->node); +if (chan->desc ==vd_to_axi_desc (vd)) + chan->desc =NULL ;vchan_cookie_complete (vd); }
@@ -1204,7 +1299,9 @@ static int dma_chan_terminate_all(struct dma_chan *dchan)vchan_get_all_descriptors (&chan->vc, &head); + chan->desc =NULL ; chan->cyclic = false; + chan->is_paused = false;spin_unlock_irqrestore (&chan->vc.lock, flags);vchan_dma_desc_free_list (&chan->vc, &head);
base-commit: 2e02fb103be058cf8b2fb24112a74a2715e090f0
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dmaengine: dw-axi-dmac: wrong block match in axi_chan_get_xferred() - [Low] dmaengine…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] dmaengine: dw-axi-dmac: wrong block match in axi_chan_get_xferred()
- [Low] dmaengine: dw-axi-dmac: missing Fixes trailer for commit 8e55444da65c
--
commit c8d0c69cc29c111365ef5036a266d8a0e8d86f5b
Author: Rui Wang <[email protected]>
Quoted 29 lines
dmaengine: dw-axi-dmac: report paused state and residue in
tx_statusThe driver implements device_pause/device_resume, but
device_tx_statuskeeps reportingDMA_IN_PROGRESSfor a paused channel, so clients cannot tell a paused channel apart from a running one. ReportDMA_PAUSEDwhen the channel is paused and the cookie is still in flight, including for callers that pass a NULLdma_tx_state, and clear the staleis_pausedflag indma_chan_terminate_all(), which disables the channel and thus implicitly cancels the paused state.Also, the residue of an in-flight transfer is currently derived from the number of completed LLI blocks, so it only advances in block-size steps and stays stale for the duration of a large block. Read the current hardware pointer (
CH_SARforMEM_TO_DEVandMEM_TO_MEM,CH_DARforDEV_TO_MEM) and walk the descriptor's LLIs to compute how many bytes have actually been transferred. The 64-bit pointer is sampled with a tearing-safe double read, as the transfer may be running concurrently. The pointer is only consulted for the descriptor most recently programmed into the hardware, tracked in the previously unusedchan->descfield; any other descriptor has not been started yet and keeps reporting its full length. When the transfer has just completed but the descriptor has not been reaped yet, the pointer sits at the end of the last block and the residue naturally reads as 0. The pointer registers are pre-loaded with the first block's addresses when the channel is programmed, so a stale pointer left by a previous transfer (e.g. when its buffer is reused) can never be observed before the first LLI fetch completes. Cyclic descriptors keep the block-granular accounting.
Quoted 4 lines
Tested on an FPGA platform.
Signed-off-by Rui Wang <[email protected]>Reviewed-by Frank Li <[email protected]>
[Severity: Low]
This restores the DMA_PAUSED check and fixes the burst-granularity residue reporting in dma_chan_tx_status(). Should this include:
Quoted 41 lines
drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c +33
-0@@ -355,33 +355,110 @@ static void vchan_desc_put(struct virt_dma_desc *vdesc)axi_desc_put (vd_to_axi_desc (vdesc)); } +/* +* Read a 64-bit channel address register while the transfer may be +* running. The two 32-bit halves cannot be read atomically on all +* platforms, so sample the high half before and after the low half +* and retry if it moved (the low half wrapped in between). +*/ +static u64 axi_chan_readq (struct axi_dma_chan *chan,u32 reg) +{ +u32 hi, lo, hi2; + +do { + hi =readl (chan->chan_regs + reg +4 ); + lo =readl (chan->chan_regs + reg); + hi2 =readl (chan->chan_regs + reg +4 ); + }while (unlikely (hi != hi2)); + +return (u64 )hi <<32 | lo; +} + +/* +* Return the number of bytes already transferred by the descriptor +* currently on the hardware (running, paused or just completed), based +* on the current read or write position: CH_SAR for MEM_TO_DEV and +* MEM_TO_MEM, CH_DAR for DEV_TO_MEM. Must be called with vc.lock held. +*/ +static u32 axi_chan_get_xferred (struct axi_dma_chan *chan, +struct axi_dma_desc *desc) +{ +struct axi_dma_hw_desc *hw_desc; +bool dst = chan->direction ==DMA_DEV_TO_MEM ; +u64 pos, start; +u32 xferred =0 ; +int i;
drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c static void vchan_desc_put(struct virt_dma_desc *vdesc) · line 400
Quoted 10 lines
drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c +10
-0+ + pos =axi_chan_readq (chan, dst ?CH_DAR :CH_SAR ); + +for (i =0 ; i < desc->nr_hw_descs; i++) { + hw_desc = &desc->hw_desc[i]; + start =le64_to_cpu (dst ? hw_desc->lli->dar : hw_desc->lli->sar); + +/* Current position is inside this block: partial progress */ +if (pos >= start && pos <= start + hw_desc->len) +return xferred + (u32)(pos - start);
[Severity: Medium]
Can axi_chan_get_xferred() match the wrong LLI block when a scatter-gather transfer reuses the same physical buffer across multiple SG entries (such as SPI dummy transfers using a shared zero or scratch page) or when a later SG block sits immediately below an earlier one in physical memory so its end address equals the earlier block's start?
Because the loop checks only the address range without consulting CH_LLP to identify the active LLI, pos matches the earlier entry in desc->hw_desc[] and reports a smaller transferred byte count, causing the reported DMA residue to jump backwards.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1