mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jelly Jia <Jelly.Jia@cixtech.com>
To: vkoul@kernel.org, Frank.Li@kernel.org, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org, robin.murphy@arm.com
Cc: devicetree@vger.kernel.org, cix-kernel-upstream@cixtech.com,
	dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, Jelly.Jia@cixtech.com
Subject: [PATCH v7 04/10] dmaengine: arm-dma350: Serialize IRQ and termination state
Date: Tue, 17 Nov 2026 17:43:29 +0800	[thread overview]
Message-ID: <20261117094335.1393735-5-Jelly.Jia@cixtech.com> (raw)
In-Reply-To: <20261117094335.1393735-1-Jelly.Jia@cixtech.com>

The interrupt handler samples status before locking and assumes an active
descriptor, while termination can detach that descriptor concurrently.
Completion can also leave a stale active pointer or overwrite the status of
a failed cookie with another transfer.

Pair status sampling and descriptor handling under the channel lock,
acknowledge only the W1C completion/error status, and detach completed
descriptors. Preserve errors until termination resets the channel state.
Wait for STOPCMD and synchronize the IRQ before releasing terminated
descriptors, including when channel resources are freed.

The existing DMAengine terminate/synchronize contract remains unchanged.
STOP timeout handling remains bounded and best-effort, as previously
agreed; no hardware-stop guarantee or new recovery policy is claimed.

Use scoped guards consistently for channel state locking, retaining the
original critical-section boundaries and keeping stop waits and resource
cleanup outside the channel lock.

Fixes: 5d099706449d ("dmaengine: Add Arm DMA-350 driver")
Signed-off-by: Jelly Jia <Jelly.Jia@cixtech.com>
---
 drivers/dma/arm-dma350.c | 133 +++++++++++++++++++++++++--------------
 1 file changed, 87 insertions(+), 46 deletions(-)

diff --git a/drivers/dma/arm-dma350.c b/drivers/dma/arm-dma350.c
index 266df7923d7f..af1b030b955f 100644
--- a/drivers/dma/arm-dma350.c
+++ b/drivers/dma/arm-dma350.c
@@ -3,9 +3,11 @@
 // Arm DMA-350 driver
 
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/dmaengine.h>
 #include <linux/dma-mapping.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/of.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -47,6 +49,8 @@
 #define CH_CMD_CLEAR		BIT(1)
 #define CH_CMD_ENABLE		BIT(0)
 
+#define D350_STOP_TIMEOUT_US	10000
+
 #define CH_STATUS		0x04
 #define CH_STAT_RESUMEWAIT	BIT(21)
 #define CH_STAT_PAUSED		BIT(20)
@@ -127,7 +131,6 @@
 #define CH_CFG_HAS_TRIGIN	BIT(5)
 #define CH_CFG_HAS_WRAP		BIT(1)
 
-
 #define LINK_REGCLEAR		BIT(0)
 #define LINK_INTREN		BIT(2)
 #define LINK_CTRL		BIT(3)
@@ -147,7 +150,6 @@
 #define LINK_LINKADDR		BIT(30)
 #define LINK_LINKADDRHI		BIT(31)
 
-
 enum ch_ctrl_donetype {
 	CH_CTRL_DONETYPE_NONE = 0,
 	CH_CTRL_DONETYPE_CMD = 1,
@@ -294,14 +296,13 @@ static struct dma_async_tx_descriptor *d350_prep_memset(struct dma_chan *chan,
 static int d350_pause(struct dma_chan *chan)
 {
 	struct d350_chan *dch = to_d350_chan(chan);
-	unsigned long flags;
 
-	spin_lock_irqsave(&dch->vc.lock, flags);
+	guard(spinlock_irqsave)(&dch->vc.lock);
+
 	if (dch->status == DMA_IN_PROGRESS) {
 		writel_relaxed(CH_CMD_PAUSE, dch->base + CH_CMD);
 		dch->status = DMA_PAUSED;
 	}
-	spin_unlock_irqrestore(&dch->vc.lock, flags);
 
 	return 0;
 }
@@ -309,14 +310,13 @@ static int d350_pause(struct dma_chan *chan)
 static int d350_resume(struct dma_chan *chan)
 {
 	struct d350_chan *dch = to_d350_chan(chan);
-	unsigned long flags;
 
-	spin_lock_irqsave(&dch->vc.lock, flags);
+	guard(spinlock_irqsave)(&dch->vc.lock);
+
 	if (dch->status == DMA_PAUSED) {
 		writel_relaxed(CH_CMD_RESUME, dch->base + CH_CMD);
 		dch->status = DMA_IN_PROGRESS;
 	}
-	spin_unlock_irqrestore(&dch->vc.lock, flags);
 
 	return 0;
 }
@@ -342,28 +342,46 @@ static u32 d350_get_residue(struct d350_chan *dch)
 static int d350_terminate_all(struct dma_chan *chan)
 {
 	struct d350_chan *dch = to_d350_chan(chan);
-	unsigned long flags;
 	LIST_HEAD(list);
 
-	spin_lock_irqsave(&dch->vc.lock, flags);
+	guard(spinlock_irqsave)(&dch->vc.lock);
+
 	writel_relaxed(CH_CMD_STOP, dch->base + CH_CMD);
 	if (dch->desc) {
 		if (dch->status != DMA_ERROR)
 			vchan_terminate_vdesc(&dch->desc->vd);
 		dch->desc = NULL;
-		dch->status = DMA_COMPLETE;
 	}
+	dch->status = DMA_COMPLETE;
 	vchan_get_all_descriptors(&dch->vc, &list);
 	list_splice_tail(&list, &dch->vc.desc_terminated);
-	spin_unlock_irqrestore(&dch->vc.lock, flags);
 
 	return 0;
 }
 
+static int d350_wait_for_stop(struct d350_chan *dch)
+{
+	u32 cmd;
+	int ret;
+
+	/* STOPCMD clears only after the channel has stopped using commands. */
+	ret = readl_poll_timeout(dch->base + CH_CMD, cmd,
+				 !(cmd & CH_CMD_STOP), 10,
+				 D350_STOP_TIMEOUT_US);
+	if (ret)
+		dev_warn(dch->vc.chan.device->dev,
+			 "channel %d failed to stop\n", dch->vc.chan.chan_id);
+
+	return ret;
+}
+
 static void d350_synchronize(struct dma_chan *chan)
 {
 	struct d350_chan *dch = to_d350_chan(chan);
 
+	/* A stop timeout must not skip IRQ or callback synchronization. */
+	d350_wait_for_stop(dch);
+	synchronize_irq(dch->irq);
 	vchan_synchronize(&dch->vc);
 }
 
@@ -378,24 +396,26 @@ static enum dma_status d350_tx_status(struct dma_chan *chan, dma_cookie_t cookie
 	struct d350_chan *dch = to_d350_chan(chan);
 	struct virt_dma_desc *vd;
 	enum dma_status status;
-	unsigned long flags;
 	u32 residue = 0;
 
 	status = dma_cookie_status(chan, cookie, state);
 
-	spin_lock_irqsave(&dch->vc.lock, flags);
-	if (cookie == dch->cookie) {
-		status = dch->status;
-		if (status == DMA_IN_PROGRESS || status == DMA_PAUSED)
-			dch->residue = d350_get_residue(dch);
-		residue = dch->residue;
-	} else if ((vd = vchan_find_desc(&dch->vc, cookie))) {
-		residue = d350_desc_bytes(to_d350_desc(vd));
-	} else if (status == DMA_IN_PROGRESS) {
-		/* Somebody else terminated it? */
-		status = DMA_ERROR;
+	scoped_guard(spinlock_irqsave, &dch->vc.lock) {
+		if (cookie == dch->cookie) {
+			status = dch->status;
+			if (status == DMA_IN_PROGRESS || status == DMA_PAUSED)
+				dch->residue = d350_get_residue(dch);
+			residue = dch->residue;
+		} else {
+			vd = vchan_find_desc(&dch->vc, cookie);
+			if (vd) {
+				residue = d350_desc_bytes(to_d350_desc(vd));
+			} else if (status == DMA_IN_PROGRESS) {
+				/* Somebody else terminated it? */
+				status = DMA_ERROR;
+			}
+		}
 	}
-	spin_unlock_irqrestore(&dch->vc.lock, flags);
 
 	dma_set_residue(state, residue);
 	return status;
@@ -405,6 +425,10 @@ static void d350_start_next(struct d350_chan *dch)
 {
 	u32 hdr, *reg;
 
+	/* Preserve the failed cookie's status until the channel is terminated. */
+	if (dch->status == DMA_ERROR)
+		return;
+
 	dch->desc = to_d350_desc(vchan_next_desc(&dch->vc));
 	if (!dch->desc)
 		return;
@@ -458,28 +482,44 @@ static void d350_start_next(struct d350_chan *dch)
 static void d350_issue_pending(struct dma_chan *chan)
 {
 	struct d350_chan *dch = to_d350_chan(chan);
-	unsigned long flags;
 
-	spin_lock_irqsave(&dch->vc.lock, flags);
+	guard(spinlock_irqsave)(&dch->vc.lock);
+
 	if (vchan_issue_pending(&dch->vc) && !dch->desc)
 		d350_start_next(dch);
-	spin_unlock_irqrestore(&dch->vc.lock, flags);
 }
 
 static irqreturn_t d350_irq(int irq, void *data)
 {
 	struct d350_chan *dch = data;
-	struct device *dev = dch->vc.chan.device->dev;
-	struct virt_dma_desc *vd = &dch->desc->vd;
+	struct virt_dma_desc *vd;
+	struct d350_desc *desc;
+	u32 residue;
 	u32 ch_status;
+	u32 irq_status;
+	u32 errinfo = 0;
+
+	/* Keep the sampled status paired with the active descriptor. */
+	guard(spinlock)(&dch->vc.lock);
 
 	ch_status = readl(dch->base + CH_STATUS);
-	if (!ch_status)
+	irq_status = ch_status & (CH_STAT_INTR_DONE | CH_STAT_INTR_ERR);
+	if (!irq_status)
 		return IRQ_NONE;
 
-	if (ch_status & CH_STAT_INTR_ERR) {
-		u32 errinfo = readl_relaxed(dch->base + CH_ERRINFO);
+	/* INTR bits are read-only; clearing W1C STAT_ERR also clears ERRINFO. */
+	if (irq_status & CH_STAT_INTR_ERR)
+		errinfo = readl_relaxed(dch->base + CH_ERRINFO);
+
+	writel_relaxed(ch_status & (CH_STAT_DONE | CH_STAT_ERR),
+		       dch->base + CH_STATUS);
 
+	desc = dch->desc;
+	if (!desc)
+		return IRQ_HANDLED;
+
+	vd = &desc->vd;
+	if (irq_status & CH_STAT_INTR_ERR) {
 		if (errinfo & (CH_ERRINFO_AXIRDPOISERR | CH_ERRINFO_AXIRDRESPERR))
 			vd->tx_result.result = DMA_TRANS_READ_FAILED;
 		else if (errinfo & CH_ERRINFO_AXIWRRESPERR)
@@ -487,23 +527,19 @@ static irqreturn_t d350_irq(int irq, void *data)
 		else
 			vd->tx_result.result = DMA_TRANS_ABORTED;
 
-		vd->tx_result.residue = d350_get_residue(dch);
-	} else if (!(ch_status & CH_STAT_INTR_DONE)) {
-		dev_warn(dev, "Unexpected IRQ source? 0x%08x\n", ch_status);
-	}
-	writel_relaxed(ch_status, dch->base + CH_STATUS);
-
-	spin_lock(&dch->vc.lock);
-	vchan_cookie_complete(vd);
-	if (ch_status & CH_STAT_INTR_DONE) {
+		residue = d350_get_residue(dch);
+		vd->tx_result.residue = residue;
+		dch->status = DMA_ERROR;
+		dch->residue = residue;
+		dch->desc = NULL;
+		vchan_cookie_complete(vd);
+	} else {
 		dch->status = DMA_COMPLETE;
 		dch->residue = 0;
+		dch->desc = NULL;
+		vchan_cookie_complete(vd);
 		d350_start_next(dch);
-	} else {
-		dch->status = DMA_ERROR;
-		dch->residue = vd->tx_result.residue;
 	}
-	spin_unlock(&dch->vc.lock);
 
 	return IRQ_HANDLED;
 }
@@ -513,6 +549,7 @@ static int d350_alloc_chan_resources(struct dma_chan *chan)
 	struct d350_chan *dch = to_d350_chan(chan);
 	int ret = request_irq(dch->irq, d350_irq, IRQF_SHARED,
 			      vchan_chan_name(&dch->vc), dch);
+
 	if (!ret)
 		writel_relaxed(CH_INTREN_DONE | CH_INTREN_ERR, dch->base + CH_INTREN);
 
@@ -524,7 +561,11 @@ static void d350_free_chan_resources(struct dma_chan *chan)
 	struct d350_chan *dch = to_d350_chan(chan);
 
 	writel_relaxed(0, dch->base + CH_INTREN);
+	d350_terminate_all(chan);
+	d350_wait_for_stop(dch);
+
 	free_irq(dch->irq, dch);
+	vchan_synchronize(&dch->vc);
 	vchan_free_chan_resources(&dch->vc);
 }
 
-- 
2.54.0


  parent reply	other threads:[~2026-11-17  9:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  9:43 [PATCH v7 00/10] dmaengine: arm-dma350: Add slave support and Sky1 integration Jelly Jia
2026-11-17  9:43 ` [PATCH v7 01/10] dmaengine: arm-dma350: Fix source trigger bit Jelly Jia
2026-11-17  9:43 ` [PATCH v7 02/10] dmaengine: arm-dma350: Check DMA mask setup errors Jelly Jia
2026-11-17  9:43 ` [PATCH v7 03/10] dmaengine: arm-dma350: Initialize channel coherency Jelly Jia
2026-11-17  9:43 ` Jelly Jia [this message]
2026-11-17  9:43 ` [PATCH v7 05/10] dmaengine: arm-dma350: Report submitted descriptor status Jelly Jia
2026-11-17  9:43 ` [PATCH v7 06/10] dmaengine: arm-dma350: Use managed device registration Jelly Jia
2026-11-17  9:43 ` [PATCH v7 07/10] dmaengine: arm-dma350: Track descriptor byte lengths explicitly Jelly Jia
2026-11-17  9:43 ` [PATCH v7 08/10] dmaengine: arm-dma350: Add slave and cyclic transfer support Jelly Jia
2026-11-17  9:43 ` [PATCH v7 09/10] dt-bindings: dma: arm,dma-350: Describe the CIX Sky1 integration Jelly Jia
2026-11-17  9:43 ` [PATCH v7 10/10] dmaengine: arm-dma350: Support integration clocks and resets Jelly Jia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117094335.1393735-5-Jelly.Jia@cixtech.com \
    --to=jelly.jia@cixtech.com \
    --cc=Frank.Li@kernel.org \
    --cc=cix-kernel-upstream@cixtech.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=vkoul@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®