[PATCH v6 05/10] dmaengine: dw: calculate residue in the status getter

From: James Hilliard

Date: Tue Nov 03 2026 - 17:27:38 EST


From: Serge Semin <fancer.lancer@xxxxxxxxx>

Calculate hardware-LLP residue while looking up the requested cookie under
the channel lock. The scanner and the status getter currently take that
lock separately, allowing a descriptor to start between the scanner's
residue update and the status query.

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: 3bfb1d20b547 ("dmaengine: Driver for the Synopsys DesignWare DMA controller")
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 | 64 +++++++++++++++------------------------------------
1 file changed, 19 insertions(+), 45 deletions(-)

diff --git a/drivers/dma/dw/core.c b/drivers/dma/dw/core.c
index 5a1caca7ad68..d9127337c849 100644
--- a/drivers/dma/dw/core.c
+++ b/drivers/dma/dw/core.c
@@ -267,41 +267,34 @@ static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
return dw->block2bytes(dwc, ctlhi, ctllo >> 4 & 7);
}

-static bool 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;

- /* Initial residue value */
- desc->residue = desc->total_len;
+ llp = channel_readl(dwc, LLP);

- /* Check first descriptors addr */
+ /* The first descriptor may still be waiting to be fetched. */
if (desc->txd.phys == DWC_LLP_LOC(llp))
- return true;
+ return residue;

- /* Check first descriptors llp */
- if (lli_read(desc, llp) == llp) {
- /* This one is currently in progress */
- desc->residue -= dwc_get_sent(dwc);
- return true;
- }
+ 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, desc_node) {
- if (lli_read(child, llp) == llp) {
- /* Currently in progress */
- desc->residue -= dwc_get_sent(dwc);
- return true;
- }
- desc->residue -= child->len;
+ if (lli_read(child, llp) == llp)
+ return residue - dwc_get_sent(dwc);
+
+ residue -= child->len;
}

- return false;
+ return residue;
}

static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
{
- dma_addr_t llp;
struct dw_desc *desc, *_desc;
struct dw_desc *child;
u32 status_xfer;
@@ -309,7 +302,6 @@ static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
LIST_HEAD(list);

spin_lock_irqsave(&dwc->lock, flags);
- llp = channel_readl(dwc, LLP);
status_xfer = dma_readl(dw, RAW.XFER);

if (status_xfer & dwc->mask) {
@@ -372,27 +364,6 @@ static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
return;
}

- if (list_empty(&dwc->active_list)) {
- spin_unlock_irqrestore(&dwc->lock, flags);
- return;
- }
-
- if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
- dev_vdbg(dmaengine_chan_dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
- spin_unlock_irqrestore(&dwc->lock, flags);
- return;
- }
-
- dev_vdbg(dmaengine_chan_dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
-
- list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
- if (dwc_get_hard_llp_desc_residue(dwc, desc, llp)) {
- spin_unlock_irqrestore(&dwc->lock, flags);
- return;
- }
- }
-
- /* Residue describes source reads; only RAW.XFER completes a transfer. */
spin_unlock_irqrestore(&dwc->lock, flags);
}

@@ -987,9 +958,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_first_active(dwc)) {
- 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