[PATCH v8 5/6] dmaengine: dw: calculate residue in the status getter
From: James Hilliard
Date: Mon Nov 16 2026 - 05:53:34 EST
From: Serge Semin <fancer.lancer@xxxxxxxxx>
Calculate hardware-LLP residue while looking up the requested cookie under
the channel lock. The descriptor scanner updates cached residue only when
no transfer-complete event is pending. If a status query completes the
current transaction and starts the next one, that new transaction's cached
residue has not been initialized and a query for its cookie reports zero.
Have dwc_get_hard_llp_desc_residue() return a local residue value from a
fresh LLP snapshot. Keep the persistent residue field for software-LLP
block accounting, subtracting the current block's progress when reporting
status. The descriptor scanner continues to advance software-LLP blocks
and complete cookies from RAW.XFER, including for polled transfers.
Fixes: 4702d5244ca9 ("dw_dmac: return proper residue value")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Serge Semin <fancer.lancer@xxxxxxxxx>
[james: retain polled completion and software-LLP progress]
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/dma/dw/core.c | 49 ++++++++++++++++++++++---------------------------
1 file changed, 22 insertions(+), 27 deletions(-)
diff --git a/drivers/dma/dw/core.c b/drivers/dma/dw/core.c
index 504b6ddeccd4..565d01c9a29a 100644
--- a/drivers/dma/dw/core.c
+++ b/drivers/dma/dw/core.c
@@ -214,41 +214,41 @@ static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
return dw->block2bytes(dwc, ctlhi, ctllo >> 4 & 7);
}
-static void dwc_get_hard_llp_desc_residue(struct dw_dma_chan *dwc,
- struct dw_desc *desc, dma_addr_t llp)
+static u32 dwc_get_hard_llp_desc_residue(struct dw_dma_chan *dwc, struct dw_desc *desc)
{
+ u32 residue = desc->total_len;
struct dw_desc *child;
+ dma_addr_t llp;
+
+ llp = channel_readl(dwc, LLP);
- desc->residue = desc->total_len;
+ /* The first descriptor may still be waiting to be fetched. */
if (desc->vd.tx.phys == DWC_LLP_LOC(llp))
- return;
+ return residue;
- if (lli_read(desc, llp) == llp) {
- desc->residue -= dwc_get_sent(dwc);
- return;
- }
+ if (lli_read(desc, llp) == llp)
+ return residue - dwc_get_sent(dwc);
- desc->residue -= desc->len;
+ residue -= desc->len;
list_for_each_entry(child, &desc->tx_list, vd.node) {
- if (lli_read(child, llp) == llp) {
- desc->residue -= dwc_get_sent(dwc);
- return;
- }
- desc->residue -= child->len;
+ if (lli_read(child, llp) == llp)
+ return residue - dwc_get_sent(dwc);
+
+ residue -= child->len;
}
+
+ return residue;
}
static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
{
struct dw_desc *desc;
struct dw_desc *child;
- dma_addr_t llp;
lockdep_assert_held(&dwc->vc.lock);
- llp = channel_readl(dwc, LLP);
if (!(dma_readl(dw, RAW.XFER) & dwc->mask))
- goto residue;
+ return;
dma_writel(dw, CLEAR.XFER, dwc->mask);
if (!dwc->desc)
@@ -287,14 +287,6 @@ static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
dwc->desc = NULL;
vchan_cookie_complete(&desc->vd);
dwc_dostart_first_queued(dwc);
- return;
-
-residue:
- desc = dwc->desc;
- if (!desc || test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags))
- return;
-
- dwc_get_hard_llp_desc_residue(dwc, desc, llp);
}
static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc)
@@ -892,9 +884,12 @@ static u32 dwc_get_residue_and_status(struct dw_dma_chan *dwc, dma_cookie_t cook
desc = dwc_find_desc(dwc, cookie);
if (desc) {
if (desc == dwc->desc) {
- residue = desc->residue;
- if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
+ if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
+ residue = desc->residue;
residue -= dwc_get_sent(dwc);
+ } else {
+ residue = dwc_get_hard_llp_desc_residue(dwc, desc);
+ }
if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags))
*status = DMA_PAUSED;
} else {
--
2.53.0