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

From: James Hilliard

Date: Mon Nov 02 2026 - 16:54:51 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>

---

Changelog RFC:
- Instead of just dropping the dwc_scan_descriptors() method invocation
calculate the residue in the Tx-status getter.
---
drivers/dma/dw/core.c | 88 ++++++++++++++++++---------------------------------
1 file changed, 31 insertions(+), 57 deletions(-)

diff --git a/drivers/dma/dw/core.c b/drivers/dma/dw/core.c
index 5a1caca7ad68..0d6b5bd8ea6b 100644
--- a/drivers/dma/dw/core.c
+++ b/drivers/dma/dw/core.c
@@ -267,41 +267,8 @@ 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)
-{
- struct dw_desc *child;
-
- /* Initial residue value */
- desc->residue = desc->total_len;
-
- /* Check first descriptors addr */
- if (desc->txd.phys == DWC_LLP_LOC(llp))
- return true;
-
- /* Check first descriptors llp */
- if (lli_read(desc, llp) == llp) {
- /* This one is currently in progress */
- desc->residue -= dwc_get_sent(dwc);
- return true;
- }
-
- desc->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;
- }
-
- return false;
-}
-
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 +276,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 +338,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);
}

@@ -975,6 +920,32 @@ static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c)
return NULL;
}

+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);
+
+ /* The first descriptor may still be waiting to be fetched. */
+ if (desc->txd.phys == DWC_LLP_LOC(llp))
+ return residue;
+
+ if (lli_read(desc, llp) == llp)
+ return residue - dwc_get_sent(dwc);
+
+ residue -= desc->len;
+ list_for_each_entry(child, &desc->tx_list, desc_node) {
+ if (lli_read(child, llp) == llp)
+ return residue - dwc_get_sent(dwc);
+
+ residue -= child->len;
+ }
+
+ return residue;
+}
+
static u32 dwc_get_residue_and_status(struct dw_dma_chan *dwc, dma_cookie_t cookie,
enum dma_status *status)
{
@@ -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