mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Frank Li <Frank.li@oss.nxp.com>
To: James Hilliard <james.hilliard1@gmail.com>
Cc: "Viresh Kumar" <vireshk@kernel.org>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Vinod Koul" <vkoul@kernel.org>, "Frank Li" <Frank.Li@kernel.org>,
	"Dan Williams" <djbw@kernel.org>,
	"Serge Semin" <fancer.lancer@gmail.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Jiri Slaby" <jirislaby@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	dmaengine@vger.kernel.org, linux-serial@vger.kernel.org,
	linux-kernel@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>,
	"Lars-Peter Clausen" <lars@metafoo.de>
Subject: Re: [PATCH v12 4/7] dmaengine: dw: use virt-dma for descriptor and callback ownership
Date: Wed, 25 Nov 2026 15:45:27 -0500	[thread overview]
Message-ID: <awdI59DI_mdEk_Ec@lizhi-Precision-Tower-5810> (raw)
In-Reply-To: <20261121-submit-dw-dma-synchronize-v1-v12-4-f51eb635209d@gmail.com>

On Sat, Nov 21, 2026 at 02:03:19PM -0700, James Hilliard wrote:
> The status getter can invoke a client's completion callback while polling
> the hardware. An 8250 RX timeout calls it with the port lock held, and the
> callback takes that same lock. Termination also lacks a synchronization
> hook to wait for callbacks already detached from the channel's queues.
>
> Serge Semin proposed leaving transfer completion to the controller
> tasklet [1]. Clients such as catpt poll with the controller interrupt
> masked, so the status getter still needs to process RAW.XFER and
> advance software-LLP blocks.
>
> Use virt-dma for transaction queues, cookie completion and callback
> delivery. Keep the active transaction and hardware state under vc.lock.
> Run client callbacks only from the per-channel virt-dma tasklet.
>
> An 8250 RX timeout consumes the transfer length minus the reported
> residue. On a bus error, source reads do not establish how many bytes
> reached the destination. Credit only whole blocks whose completion the
> driver observed while advancing a software-linked transfer. For a
> hardware-linked transfer, treat the whole buffer as unconfirmed.
>
> Report the abort and the same residue to result callbacks and status
> pollers, even after another transfer completes. Reserve the error record
> during preparation, when clients can still handle allocation failure.
> Use the normal virt-dma submission path; neither submission nor fault
> reporting needs to allocate. Keep only failed cookies after descriptor
> release, until the cookie is reused or the channel is released.
>
> Submit only the first block of each transaction to virt-dma. Its child
> LLIs remain on the driver's hardware-chain list and are freed together
> after the callback returns. Keep the controller tasklet for hardware
> event processing and error recovery.
>
> Complete cookies under vc.lock before handing descriptors to the callback
> tasklet. Complete terminated cookies in submission order without invoking
> their callbacks, so a concurrent termination cannot advance the completed
> cookie past an older completion that later moves it backwards. Newly
> submitted transactions become eligible to start when issue_pending()
> moves them to the issued queue.
>
> Implement device_synchronize with vchan_synchronize(), which drains only
> the selected channel's callbacks. DMAengine calls it before releasing
> channel resources. Exclude the controller tasklet while clearing channel
> configuration and powering down, and let a pending run return without
> register access after the last channel has been released.
>
> Fixes: 3bfb1d20b547 ("dmaengine: Driver for the Synopsys DesignWare DMA controller")
> Fixes: 69cea5a00d31 ("dmaengine/dw_dmac: Replace spin_lock* with irqsave variants and enable submission from callback")
> Fixes: b36f09c3c441 ("dmaengine: Add transfer termination synchronization support")
> Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Link: https://lore.kernel.org/r/20240911184710.4207-2-fancer.lancer@gmail.com [1]
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>  drivers/dma/dw/Kconfig |   1 +
>  drivers/dma/dw/core.c  | 502 +++++++++++++++++++++++++------------------------
>  drivers/dma/dw/regs.h  |  26 ++-
>  3 files changed, 273 insertions(+), 256 deletions(-)
>
> diff --git a/drivers/dma/dw/Kconfig b/drivers/dma/dw/Kconfig
> index a9828ddd6d06..444702c4a059 100644
> --- a/drivers/dma/dw/Kconfig
> +++ b/drivers/dma/dw/Kconfig
> @@ -7,6 +7,7 @@
>  config DW_DMAC_CORE
>  	tristate
>  	select DMA_ENGINE
> +	select DMA_VIRTUAL_CHANNELS
>
>  config DW_DMAC
>  	tristate "Synopsys DesignWare AHB DMA platform driver"
> diff --git a/drivers/dma/dw/core.c b/drivers/dma/dw/core.c
> index 3df696e41ff2..d00d0d53f6ba 100644
> --- a/drivers/dma/dw/core.c
> +++ b/drivers/dma/dw/core.c
> @@ -8,6 +8,7 @@
>   */
>
>  #include <linux/bitops.h>
> +#include <linux/cleanup.h>
>  #include <linux/delay.h>
>  #include <linux/dmaengine.h>
>  #include <linux/dma-mapping.h>
> @@ -41,35 +42,6 @@
>
>  /*----------------------------------------------------------------------*/
>
> -static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
> -{
> -	return to_dw_desc(dwc->active_list.next);
> -}
> -
> -static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
> -{
> -	struct dw_desc		*desc = txd_to_dw_desc(tx);
> -	struct dw_dma_chan	*dwc = to_dw_dma_chan(tx->chan);
> -	dma_cookie_t		cookie;
> -	unsigned long		flags;
> -
> -	spin_lock_irqsave(&dwc->vc.lock, flags);
> -	cookie = dma_cookie_assign(tx);
> -
> -	/*
> -	 * REVISIT: We should attempt to chain as many descriptors as
> -	 * possible, perhaps even appending to those already submitted
> -	 * for DMA. But this is hard to do in a race-free manner.
> -	 */
> -
> -	list_add_tail(&desc->desc_node, &dwc->queue);
> -	spin_unlock_irqrestore(&dwc->vc.lock, flags);
> -	dev_vdbg(dmaengine_chan_dev(tx->chan), "%s: queued %u\n",
> -		 __func__, desc->txd.cookie);
> -
> -	return cookie;
> -}
> -
>  static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
>  {
>  	struct dw_dma *dw = to_dw_dma(dwc->vc.chan.device);
> @@ -80,12 +52,8 @@ static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
>  	if (!desc)
>  		return NULL;
>
> -	dwc->descs_allocated++;
>  	INIT_LIST_HEAD(&desc->tx_list);
> -	dma_async_tx_descriptor_init(&desc->txd, &dwc->vc.chan);
> -	desc->txd.tx_submit = dwc_tx_submit;
> -	desc->txd.flags = DMA_CTRL_ACK;
> -	desc->txd.phys = phys;
> +	desc->vd.tx.phys = phys;
>  	return desc;
>  }
>
> @@ -97,14 +65,86 @@ static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
>  	if (unlikely(!desc))
>  		return;
>
> -	list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) {
> -		list_del(&child->desc_node);
> -		dma_pool_free(dw->desc_pool, child, child->txd.phys);
> -		dwc->descs_allocated--;
> +	list_for_each_entry_safe(child, _next, &desc->tx_list, vd.node) {
> +		list_del(&child->vd.node);
> +		dma_pool_free(dw->desc_pool, child, child->vd.tx.phys);
>  	}
>
> -	dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
> -	dwc->descs_allocated--;
> +	kfree(desc->error);
> +	dma_pool_free(dw->desc_pool, desc, desc->vd.tx.phys);
> +}
> +
> +static void dwc_desc_free(struct virt_dma_desc *vd)
> +{
> +	struct dw_dma_chan *dwc = to_dw_dma_chan(vd->tx.chan);
> +
> +	dwc_desc_put(dwc, vd_to_dw_desc(vd));
> +}
> +
> +static struct dma_async_tx_descriptor *
> +dwc_tx_prep(struct dw_dma_chan *dwc, struct dw_desc *desc, unsigned long flags)
> +{
> +	if (flags & DMA_CTRL_REUSE)
> +		goto err;
> +
> +	/* Reserve a record now so neither submission nor error completion can fail. */
> +	desc->error = kmalloc_obj(*desc->error, GFP_ATOMIC);
> +	if (desc->error)
> +		return vchan_tx_prep(&dwc->vc, &desc->vd, flags);
> +err:
> +	dwc_desc_put(dwc, desc);
> +	return NULL;
> +}
> +
> +static int dwc_error_cmp(const void *key, const struct rb_node *node)
> +{
> +	const struct dw_dma_error *error = rb_entry(node, struct dw_dma_error, node);
> +
> +	return *(const dma_cookie_t *)key - error->cookie;
> +}
> +
> +static int dwc_error_cmp_nodes(struct rb_node *a, const struct rb_node *b)
> +{
> +	return dwc_error_cmp(&rb_entry(a, struct dw_dma_error, node)->cookie, b);
> +}
> +
> +static void dwc_retire_cookie(struct dw_dma_chan *dwc, struct virt_dma_desc *vd)
> +{
> +	struct dw_desc *desc = vd_to_dw_desc(vd);
> +	struct dw_dma_error *error = desc->error;
> +	struct rb_node *old;
> +
> +	lockdep_assert_held(&dwc->vc.lock);
> +	/* A numeric cookie may still have an error from before it wrapped. */
> +	if (vd->tx_result.result == DMA_TRANS_ABORTED) {
> +		error->cookie = vd->tx.cookie;
> +		error->residue = vd->tx_result.residue;
> +		old = rb_find_add(&error->node, &dwc->errors, dwc_error_cmp_nodes);
> +		if (!old) {
> +			desc->error = NULL;
> +			return;
> +		}
> +		rb_entry(old, struct dw_dma_error, node)->residue = error->residue;
> +	} else {
> +		old = rb_find(&vd->tx.cookie, &dwc->errors, dwc_error_cmp);
> +		if (old) {
> +			rb_erase(old, &dwc->errors);
> +			kfree(rb_entry(old, struct dw_dma_error, node));
> +		}
> +	}
> +}
> +
> +static void dwc_free_errors(struct dw_dma_chan *dwc)
> +{
> +	struct dw_dma_error *error, *next;
> +	struct rb_root errors;
> +
> +	scoped_guard(spinlock_irqsave, &dwc->vc.lock) {
> +		errors = dwc->errors;
> +		dwc->errors = RB_ROOT;
> +	}
> +	rbtree_postorder_for_each_entry_safe(error, next, &errors, node)
> +		kfree(error);
>  }

I can't understand why need rb tree for errors? if error happen, whole
desc should return error.

Frank

  reply	other threads:[~2026-11-25 20:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 21:03 [PATCH v12 0/7] dmaengine: dw: serialize completion and termination James Hilliard
2026-11-21 21:03 ` [PATCH v12 1/7] dmaengine: dw: Fix XFER bit set, but channel not idle error James Hilliard
2026-11-21 21:03 ` [PATCH v12 2/7] dmaengine: dw: serialize error recovery with channel termination James Hilliard
2026-11-21 21:03 ` [PATCH v12 3/7] dmaengine: dw: embed a virt-dma channel James Hilliard
2026-11-25 20:38   ` Frank Li
2026-11-21 21:03 ` [PATCH v12 4/7] dmaengine: dw: use virt-dma for descriptor and callback ownership James Hilliard
2026-11-25 20:45   ` Frank Li [this message]
2026-11-21 21:03 ` [PATCH v12 5/7] dmaengine: dw: extract hardware-LLP residue calculation James Hilliard
2026-11-25 20:50   ` Frank Li
2026-11-21 21:03 ` [PATCH v12 6/7] dmaengine: dw: calculate residue in the status getter James Hilliard
2026-11-21 21:03 ` [PATCH v12 7/7] dmaengine: dw: report residue for queued transfers James Hilliard

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=awdI59DI_mdEk_Ec@lizhi-Precision-Tower-5810 \
    --to=frank.li@oss.nxp.com \
    --cc=Frank.Li@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=djbw@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=fancer.lancer@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=james.hilliard1@gmail.com \
    --cc=jirislaby@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=vireshk@kernel.org \
    --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®