* [PATCH v2 0/3] dmaengine: xilinx_dma: Fix AXI DMA descriptor ring ordering
@ 2026-11-16 3:43 Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 1/3] dmaengine: xilinx_dma: Reject transfers that map no data Jeff C. Jensen via B4 Relay
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Jeff C. Jensen via B4 Relay @ 2026-11-16 3:43 UTC (permalink / raw)
To: Vinod Koul, dmaengine
Cc: Frank Li, Michal Simek, Suraj Gupta, Radhey Shyam Pandey,
Alex Bereza, Folker Schwesinger, Tomi Valkeinen,
linux-arm-kernel, linux-kernel, Jeff C. Jensen
This series fixes descriptor handling problems in the AXI DMA driver
that I ran into while streaming from programmable logic into a single
S2MM channel on a Zynq-7020.
Patches 1 and 2 are small fixes to the preparation paths. A transfer
whose entries are all zero length built a descriptor with no segments,
and the cyclic period count could be truncated on 64-bit builds.
Patch 3 is the main change. The driver hands out segments from a free
list, while the engine follows the descriptor ring in pool order, so the
two must stay in step. Commit cee9c863ee68 fixed one way they drift
apart. The Sashiko review of that commit [1] pointed out three more, and
patch 3 removes them all by allocating consecutive ring positions
instead of free list entries.
The series is based on v7.4-rc1, the dmaengine fixes branch at
6628e7d4c6b2, and builds on cee9c863ee68 and 7ed1e3070c9b.
Testing
=======
Every patch builds for arm (multi_v7_defconfig) and arm64 (defconfig),
with the driver built in and as a module, with W=1 and sparse, and
passes checkpatch --strict. I tested the series on hardware as a
backport to the AMD vendor kernel, described below; the patches as
posted, against v7.4-rc1, are build-tested only.
The backport ran on the AMD 6.12.40 vendor kernel together with
aa99c4d1d63b, cee9c863ee68 and 7ed1e3070c9b, and with 38433a6fdfb7 so
that the patches apply as posted, on a Zynq-7020 with one AXI DMA v7.1
S2MM channel: scatter-gather, 32-bit, no DRE and the 512-segment pool.
No client in these tests uses the dma_vec path, and the vendor kernel
lacks 607869003479, so the dma_vec path and the transfer chaining that
commit adds are build-tested only with these changes.
- A test client streamed 640128-byte packets at 80 per second from a
pattern generator, refilled the channel from the completion callback
and from a thread on the other CPU, and checked every sample. Two 60
s runs passed, each running out of segments about 15000 times. Cases
for pool capacity, a zero-length scatterlist, the buffer-length
boundary and cyclic followed by slave-sg also passed.
- Our production client streamed nine sessions, more than 160000
packets, with every sample checked on the receiving host.
To reproduce what patch 3 fixes: without the series, the test client ran
for 10 minutes without an error, both on the vendor kernel with the
three backports and on the vendor kernel with aa99c4d1d63b alone. The
windows are small, so I added a test-only 500 us busy-wait between the
completion callback and the free in xilinx_dma_chan_desc_cleanup(). The
test client terminates the channel once at the start of each run while
packets keep arriving. On the vendor kernel with the backports, all
three 20 s runs failed, one with a DMA internal error and two with the
channel stalled. With patches 1-3 added, the same three runs passed, and
so did one with a 2000 us busy-wait. I could not reproduce the
partial-preparation case: the test client serializes its preparations
against its own callback, which leaves that window open only between the
callback and the free.
Not tested: MM2S, two-channel controllers, CDMA, VDMA and MCDMA.
Known limitations
=================
- The AXI MCDMA pool keeps its free list, so the terminate and
partial-preparation problems remain there. My design does not use AXI
MCDMA, and I did not test it. Extending the reservation to MCDMA
would be possible future work.
- The other two issues in the Sashiko review, the chan->terminating
race with submit and dma_run_dependencies() under chan->lock, are not
addressed here.
This series was developed with the help of Claude Code, an LLM coding
assistant; I reviewed all of the code and text.
[1] https://lore.kernel.org/all/20260817094004.A74621F000E9@smtp.kernel.org/
---
Changes in v2:
- Drop patch 4, which withheld an engine whose reset did not complete,
and RFC patch 5, which recovered it: a reset timeout most likely
means a hardware fault (Frank Li)
- Rebase onto v7.4-rc1 and collect Reviewed-by tags; patches 1-3 are
otherwise unchanged
- Drop the Assisted-by tags after 1e035297ffda ("docs: remove
Assisted-by references"); the cover letter states the tool use
- Link to v1: https://lore.kernel.org/r/20261022-xilinx-dma-work-v1-0-20faea2e7a80@nullptr.com
---
Jeff C. Jensen (3):
dmaengine: xilinx_dma: Reject transfers that map no data
dmaengine: xilinx_dma: Fix cyclic period count truncation
dmaengine: xilinx_dma: Allocate AXI DMA segments in ring order
drivers/dma/xilinx/xilinx_dma.c | 285 ++++++++++++++++++++++++++++++++--------
1 file changed, 232 insertions(+), 53 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261022-xilinx-dma-work-4f2fad787d15
Best regards,
--
Jeff C. Jensen <kernel@nullptr.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/3] dmaengine: xilinx_dma: Reject transfers that map no data
2026-11-16 3:43 [PATCH v2 0/3] dmaengine: xilinx_dma: Fix AXI DMA descriptor ring ordering Jeff C. Jensen via B4 Relay
@ 2026-11-16 3:43 ` Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 2/3] dmaengine: xilinx_dma: Fix cyclic period count truncation Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 3/3] dmaengine: xilinx_dma: Allocate AXI DMA segments in ring order Jeff C. Jensen via B4 Relay
2 siblings, 0 replies; 4+ messages in thread
From: Jeff C. Jensen via B4 Relay @ 2026-11-16 3:43 UTC (permalink / raw)
To: Vinod Koul, dmaengine
Cc: Frank Li, Michal Simek, Suraj Gupta, Radhey Shyam Pandey,
Alex Bereza, Folker Schwesinger, Tomi Valkeinen,
linux-arm-kernel, linux-kernel, Jeff C. Jensen
From: "Jeff C. Jensen" <kernel@nullptr.com>
If every entry of a scatterlist or a DMA vector array has zero length,
the preparation loops in xilinx_dma_prep_slave_sg(),
xilinx_dma_prep_peripheral_dma_vec() and xilinx_mcdma_prep_slave_sg()
allocate no segments. Each function then takes list_first_entry() of the
empty segment list anyway, so desc->async_tx.phys, which is later
programmed as the current descriptor, is derived from the list head
rather than from a segment. For DMA_MEM_TO_DEV, the SOP and EOP bits are
also set through pointers that do not point at a segment.
Fail the preparation when no segment was allocated.
Fixes: c0bba3a99f07 ("dmaengine: vdma: Add Support for Xilinx AXI Direct Memory Access Engine")
Fixes: 6ccd692bfb7f ("dmaengine: xilinx_dma: Add Xilinx AXI MCDMA Engine driver support")
Fixes: 38433a6fdfb7 ("dmaengine: xilinx_dma: Support descriptor setup from dma_vecs")
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Reviewed-by: Suraj Gupta <suraj.gupta2@amd.com>
Signed-off-by: Jeff C. Jensen <kernel@nullptr.com>
---
drivers/dma/xilinx/xilinx_dma.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/dma/xilinx/xilinx_dma.c b/drivers/dma/xilinx/xilinx_dma.c
index 0336941d4095..6f00ab41b143 100644
--- a/drivers/dma/xilinx/xilinx_dma.c
+++ b/drivers/dma/xilinx/xilinx_dma.c
@@ -2362,6 +2362,9 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_peripheral_dma_vec(
}
}
+ if (list_empty(&desc->segments))
+ goto error;
+
head = list_first_entry(&desc->segments, struct xilinx_axidma_tx_segment, node);
desc->async_tx.phys = head->phys;
@@ -2463,6 +2466,9 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
}
}
+ if (list_empty(&desc->segments))
+ goto error;
+
segment = list_first_entry(&desc->segments,
struct xilinx_axidma_tx_segment, node);
desc->async_tx.phys = segment->phys;
@@ -2670,6 +2676,9 @@ xilinx_mcdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
}
}
+ if (list_empty(&desc->segments))
+ goto error;
+
segment = list_first_entry(&desc->segments,
struct xilinx_aximcdma_tx_segment, node);
desc->async_tx.phys = segment->phys;
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 2/3] dmaengine: xilinx_dma: Fix cyclic period count truncation
2026-11-16 3:43 [PATCH v2 0/3] dmaengine: xilinx_dma: Fix AXI DMA descriptor ring ordering Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 1/3] dmaengine: xilinx_dma: Reject transfers that map no data Jeff C. Jensen via B4 Relay
@ 2026-11-16 3:43 ` Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 3/3] dmaengine: xilinx_dma: Allocate AXI DMA segments in ring order Jeff C. Jensen via B4 Relay
2 siblings, 0 replies; 4+ messages in thread
From: Jeff C. Jensen via B4 Relay @ 2026-11-16 3:43 UTC (permalink / raw)
To: Vinod Koul, dmaengine
Cc: Frank Li, Michal Simek, Suraj Gupta, Radhey Shyam Pandey,
Alex Bereza, Folker Schwesinger, Tomi Valkeinen,
linux-arm-kernel, linux-kernel, Jeff C. Jensen
From: "Jeff C. Jensen" <kernel@nullptr.com>
xilinx_dma_prep_dma_cyclic() stores buf_len / period_len, a size_t
quotient, in an unsigned int. On 64-bit builds the count can be
truncated: a buffer of 2^32 + 1 bytes split into 1-byte periods becomes
a single period, and the transfer covers one byte of the buffer the
client asked for.
Compute the quotient as a size_t and reject it if it exceeds
XILINX_DMA_NUM_DESCS before narrowing it. Each period needs at least one
hardware descriptor, so a larger count could never be prepared from the
pool anyway.
Fixes: 92d794dfb699 ("dmaengine: vdma: Add support for cyclic dma mode")
Reviewed-by: Suraj Gupta <suraj.gupta2@amd.com>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Jeff C. Jensen <kernel@nullptr.com>
---
drivers/dma/xilinx/xilinx_dma.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/drivers/dma/xilinx/xilinx_dma.c b/drivers/dma/xilinx/xilinx_dma.c
index 6f00ab41b143..ba4fa1aafc50 100644
--- a/drivers/dma/xilinx/xilinx_dma.c
+++ b/drivers/dma/xilinx/xilinx_dma.c
@@ -2511,7 +2511,7 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
struct xilinx_dma_tx_descriptor *desc;
struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
- size_t copy, sg_used;
+ size_t copy, sg_used, periods;
unsigned int num_periods;
int i;
u32 reg;
@@ -2519,10 +2519,11 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
if (!period_len)
return NULL;
- num_periods = buf_len / period_len;
-
- if (!num_periods)
+ /* a period takes at least one descriptor */
+ periods = buf_len / period_len;
+ if (!periods || periods > XILINX_DMA_NUM_DESCS)
return NULL;
+ num_periods = periods;
if (!is_slave_direction(direction))
return NULL;
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 3/3] dmaengine: xilinx_dma: Allocate AXI DMA segments in ring order
2026-11-16 3:43 [PATCH v2 0/3] dmaengine: xilinx_dma: Fix AXI DMA descriptor ring ordering Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 1/3] dmaengine: xilinx_dma: Reject transfers that map no data Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 2/3] dmaengine: xilinx_dma: Fix cyclic period count truncation Jeff C. Jensen via B4 Relay
@ 2026-11-16 3:43 ` Jeff C. Jensen via B4 Relay
2 siblings, 0 replies; 4+ messages in thread
From: Jeff C. Jensen via B4 Relay @ 2026-11-16 3:43 UTC (permalink / raw)
To: Vinod Koul, dmaengine
Cc: Frank Li, Michal Simek, Suraj Gupta, Radhey Shyam Pandey,
Alex Bereza, Folker Schwesinger, Tomi Valkeinen,
linux-arm-kernel, linux-kernel, Jeff C. Jensen
From: "Jeff C. Jensen" <kernel@nullptr.com>
The AXI DMA driver builds a static ring of hardware descriptors
(segments) when the channel is allocated, each linked to the next in
pool order, and the engine follows those links.
xilinx_dma_prep_slave_sg() writes no next pointers within a descriptor,
and a descriptor appended while the engine is running is reached through
the next pointer of the previous tail. Segments are handed out from
free_seg_list, so this only works while the free list stays in pool
order.
Commit cee9c863ee68 ("dmaengine: xilinx_dma: Fix hardware buffer
descriptor reuse order") fixed one way the free list falls out of that
order. As the Sashiko review of that patch pointed out [1], others
remain:
- xilinx_dma_chan_desc_cleanup() drops chan->lock while it runs a
completion callback, and frees the descriptor afterwards. If the
channel is terminated in that window, the newer segments are returned
first and the oldest descriptor's segments end up at the tail of the
free list.
- A preparation that runs out of segments returns its partial
allocation to the tail of the free list, possibly after a completion
on another CPU has freed newer ones. It does so without taking
chan->lock, so it can also corrupt the list.
- A submission rejected because a cyclic transfer is running frees its
segments while older ones are still in use.
Once the free list and the ring disagree, a later descriptor gets
segments that are not adjacent in the ring, and the engine follows a
next pointer into a cleaned segment with a zero length field. With a
delay added to widen the first window, a receive stream fails right
after a terminate:
xilinx-vdma 40400000.dma: Channel 7a90d35b has errors 10, cdr 3f053780 tdr 3f059a00
Rather than keeping the free list in order on every path, allocate by
ring position. Count the segments a transaction needs, then reserve that
many consecutive positions, starting at a cursor, under chan->lock: all
of them or none. A bitmap tracks the positions in use. Allocation no
longer depends on the order in which segments are freed, so neither the
terminate path nor the completion path can disturb it. The count uses
xilinx_dma_calc_copysize(), like the preparation loops, so the two
always agree.
The slave-sg, cyclic and dma_vec paths all count and reserve before
building a descriptor, so a failed preparation leaves the channel
unchanged. A submission rejected on a busy cyclic channel cancels its
reservation and rewinds the cursor under the same lock.
This relies on commit 7ed1e3070c9b ("dmaengine: xilinx_dma: Fix hardware
buffer descriptor chain after cyclic DMA") to restore the segment links
after a cyclic transfer. The AXI MCDMA pool keeps its free list and is
not changed.
Link: https://lore.kernel.org/all/20260817094004.A74621F000E9@smtp.kernel.org/ [1]
Fixes: 23059408b6a3 ("dmaengine: xilinx_dma: Fix race condition in the driver for multiple descriptor scenario")
Fixes: c0bba3a99f07 ("dmaengine: vdma: Add Support for Xilinx AXI Direct Memory Access Engine")
Fixes: 92d794dfb699 ("dmaengine: vdma: Add support for cyclic dma mode")
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Jeff C. Jensen <kernel@nullptr.com>
---
drivers/dma/xilinx/xilinx_dma.c | 279 ++++++++++++++++++++++++++++++++--------
1 file changed, 224 insertions(+), 55 deletions(-)
diff --git a/drivers/dma/xilinx/xilinx_dma.c b/drivers/dma/xilinx/xilinx_dma.c
index ba4fa1aafc50..69f61ba1652b 100644
--- a/drivers/dma/xilinx/xilinx_dma.c
+++ b/drivers/dma/xilinx/xilinx_dma.c
@@ -33,6 +33,7 @@
*
*/
+#include <linux/bitmap.h>
#include <linux/bitops.h>
#include <linux/dmapool.h>
#include <linux/dma/xilinx_dma.h>
@@ -400,7 +401,9 @@ struct xilinx_dma_tx_descriptor {
* @pending_list: Descriptors waiting
* @active_list: Descriptors ready to submit
* @done_list: Complete descriptors
- * @free_seg_list: Free descriptors
+ * @free_seg_list: Free AXI MCDMA descriptors
+ * @seg_used: AXI DMA segments in use, indexed by ring position
+ * @seg_next: Ring position the next AXI DMA reservation starts at
* @common: DMA common channel
* @desc_pool: Descriptors pool
* @dev: The dma device
@@ -440,6 +443,8 @@ struct xilinx_dma_chan {
struct list_head active_list;
struct list_head done_list;
struct list_head free_seg_list;
+ DECLARE_BITMAP(seg_used, XILINX_DMA_NUM_DESCS);
+ u32 seg_next;
struct dma_chan common;
struct dma_pool *desc_pool;
struct device *dev;
@@ -750,30 +755,53 @@ xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
}
/**
- * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
+ * xilinx_axidma_reserve_segments - Reserve a run of AXI DMA segments
* @chan: Driver specific DMA channel
+ * @count: Segments needed, at most XILINX_DMA_NUM_DESCS
+ * @start: Ring position of the first reserved segment, set on success
*
- * Return: The allocated segment on success and NULL on failure.
+ * Reserves @count consecutive ring positions starting at @chan->seg_next,
+ * wrapping at the pool end. The engine fetches descriptors in ring order, so
+ * the run cannot be placed anywhere else.
+ *
+ * Return: 0 on success, -ENOMEM when the run is not free. A failure leaves the
+ * ownership bitmap and @chan->seg_next unchanged.
*/
-static struct xilinx_axidma_tx_segment *
-xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
+static int xilinx_axidma_reserve_segments(struct xilinx_dma_chan *chan,
+ u32 count, u32 *start)
{
- struct xilinx_axidma_tx_segment *segment = NULL;
unsigned long flags;
+ int ret = -ENOMEM;
+ u32 i;
spin_lock_irqsave(&chan->lock, flags);
- if (!list_empty(&chan->free_seg_list)) {
- segment = list_first_entry(&chan->free_seg_list,
- struct xilinx_axidma_tx_segment,
- node);
- list_del(&segment->node);
+
+ for (i = 0; i < count; i++) {
+ if (test_bit((chan->seg_next + i) % XILINX_DMA_NUM_DESCS,
+ chan->seg_used))
+ goto out;
}
+
+ *start = chan->seg_next;
+ for (i = 0; i < count; i++)
+ __set_bit((chan->seg_next + i) % XILINX_DMA_NUM_DESCS,
+ chan->seg_used);
+ chan->seg_next = (chan->seg_next + count) % XILINX_DMA_NUM_DESCS;
+ ret = 0;
+
+out:
spin_unlock_irqrestore(&chan->lock, flags);
- if (!segment)
- dev_dbg(chan->dev, "Could not find free tx segment\n");
+ if (ret)
+ dev_dbg(chan->dev, "no run of %u free tx segments\n", count);
- return segment;
+ return ret;
+}
+
+static struct xilinx_axidma_tx_segment *
+xilinx_axidma_run_segment(struct xilinx_dma_chan *chan, u32 start, u32 i)
+{
+ return &chan->seg_v[(start + i) % XILINX_DMA_NUM_DESCS];
}
/**
@@ -836,13 +864,18 @@ static void xilinx_mcdma_clean_hw_desc(struct xilinx_aximcdma_desc_hw *hw)
* xilinx_dma_free_tx_segment - Free transaction segment
* @chan: Driver specific DMA channel
* @segment: DMA transaction segment
+ *
+ * Called with chan->lock held, so no reservation sees the segment between its
+ * links being restored and its ownership being released.
*/
static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
struct xilinx_axidma_tx_segment *segment)
{
+ lockdep_assert_held(&chan->lock);
+
xilinx_dma_clean_hw_desc(chan, segment);
- list_add_tail(&segment->node, &chan->free_seg_list);
+ __clear_bit(segment - chan->seg_v, chan->seg_used);
}
/**
@@ -946,6 +979,34 @@ xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
kfree(desc);
}
+/**
+ * xilinx_dma_cancel_tx_descriptor - Discard a descriptor before submission
+ * @chan: Driver specific DMA channel
+ * @desc: Prepared descriptor
+ *
+ * Called with chan->lock held. On an AXI DMA channel, releases the reservation
+ * of @desc and rewinds @chan->seg_next to its first segment, so that no
+ * cleaned segment is left between two live descriptors. @desc must hold the
+ * most recent reservation on the channel.
+ */
+static void xilinx_dma_cancel_tx_descriptor(struct xilinx_dma_chan *chan,
+ struct xilinx_dma_tx_descriptor *desc)
+{
+ struct xilinx_axidma_tx_segment *first;
+
+ lockdep_assert_held(&chan->lock);
+
+ if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
+ first = list_first_entry_or_null(&desc->segments,
+ struct xilinx_axidma_tx_segment,
+ node);
+ if (first)
+ chan->seg_next = first - chan->seg_v;
+ }
+
+ xilinx_dma_free_tx_descriptor(chan, desc);
+}
+
/* Required functions */
/**
@@ -995,10 +1056,6 @@ static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
xilinx_dma_free_descriptors(chan);
if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
- spin_lock_irqsave(&chan->lock, flags);
- INIT_LIST_HEAD(&chan->free_seg_list);
- spin_unlock_irqrestore(&chan->lock, flags);
-
/* Free memory that is allocated for BD */
dma_free_coherent(chan->dev, sizeof(*chan->seg_v) *
XILINX_DMA_NUM_DESCS, chan->seg_v,
@@ -1241,9 +1298,9 @@ static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
((i + 1) % XILINX_DMA_NUM_DESCS));
chan->seg_v[i].phys = chan->seg_p +
sizeof(*chan->seg_v) * i;
- list_add_tail(&chan->seg_v[i].node,
- &chan->free_seg_list);
}
+ bitmap_zero(chan->seg_used, XILINX_DMA_NUM_DESCS);
+ chan->seg_next = 0;
} else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
/* Allocate the buffer descriptors. */
chan->seg_mv = dma_alloc_coherent(chan->dev,
@@ -1328,6 +1385,98 @@ static int xilinx_dma_calc_copysize(struct xilinx_dma_chan *chan,
return copy;
}
+/**
+ * xilinx_axidma_count_split - Count the descriptors a length splits into
+ * @chan: Driver specific DMA channel
+ * @len: Length in bytes
+ *
+ * Splits @len with xilinx_dma_calc_copysize(), so the count equals the number
+ * of segments a preparation builds for @len. Intermediate chunks are rounded
+ * down for alignment, so the count depends on the channel alignment as well as
+ * on @len.
+ *
+ * Return: The descriptor count, which is zero for a zero length, or -EINVAL if
+ * a split makes no progress or @len needs more descriptors than the pool holds.
+ */
+static int xilinx_axidma_count_split(struct xilinx_dma_chan *chan, size_t len)
+{
+ size_t used = 0;
+ int count = 0;
+
+ while (used < len) {
+ size_t copy = xilinx_dma_calc_copysize(chan, len, used);
+
+ if (!copy || count == XILINX_DMA_NUM_DESCS)
+ return -EINVAL;
+
+ used += copy;
+ count++;
+ }
+
+ return count;
+}
+
+/**
+ * xilinx_axidma_count_sg - Count the descriptors a mapped scatterlist needs
+ * @chan: Driver specific DMA channel
+ * @sgl: Mapped scatterlist
+ * @sg_len: Number of mapped entries in @sgl
+ *
+ * Return: The descriptor count, or -EINVAL if the list splits into no
+ * descriptors at all or into more than the pool holds.
+ */
+static int xilinx_axidma_count_sg(struct xilinx_dma_chan *chan,
+ struct scatterlist *sgl, unsigned int sg_len)
+{
+ struct scatterlist *sg;
+ unsigned int i;
+ int total = 0;
+
+ for_each_sg(sgl, sg, sg_len, i) {
+ int count = xilinx_axidma_count_split(chan, sg_dma_len(sg));
+
+ if (count < 0 || count > XILINX_DMA_NUM_DESCS - total)
+ return -EINVAL;
+
+ total += count;
+ }
+
+ if (!total)
+ return -EINVAL;
+
+ return total;
+}
+
+/**
+ * xilinx_axidma_count_vecs - Count the descriptors an array of DMA vectors needs
+ * @chan: Driver specific DMA channel
+ * @vecs: DMA vectors
+ * @nb: Entry count
+ *
+ * Return: The descriptor count, or -EINVAL if the vectors split into no
+ * descriptors at all or into more than the pool holds.
+ */
+static int xilinx_axidma_count_vecs(struct xilinx_dma_chan *chan,
+ const struct dma_vec *vecs, size_t nb)
+{
+ size_t i;
+ int total = 0;
+
+ for (i = 0; i < nb; i++) {
+ int count = xilinx_axidma_count_split(chan, vecs[i].len);
+
+ if (count < 0 || count > XILINX_DMA_NUM_DESCS - total)
+ return -EINVAL;
+
+ total += count;
+ }
+
+ if (!total)
+ return -EINVAL;
+
+ return total;
+}
+
/**
* xilinx_dma_tx_status - Get DMA transaction status
* @dchan: DMA channel
@@ -2107,6 +2256,10 @@ static void append_desc_queue(struct xilinx_dma_chan *chan,
* xilinx_dma_tx_submit - Submit DMA transaction
* @tx: Async transaction descriptor
*
+ * An AXI DMA channel reserves segment runs in ring order, so a transaction must
+ * be submitted, or its rejection handled, before the next one is prepared on
+ * the same channel.
+ *
* Return: cookie value on success and failure value on error
*/
static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
@@ -2117,8 +2270,11 @@ static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
unsigned long flags;
int err;
+ spin_lock_irqsave(&chan->lock, flags);
+
if (chan->cyclic) {
- xilinx_dma_free_tx_descriptor(chan, desc);
+ xilinx_dma_cancel_tx_descriptor(chan, desc);
+ spin_unlock_irqrestore(&chan->lock, flags);
return -EBUSY;
}
@@ -2127,13 +2283,13 @@ static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
* If reset fails, need to hard reset the system.
* Channel is no longer functional
*/
+ spin_unlock_irqrestore(&chan->lock, flags);
err = xilinx_dma_chan_reset(chan);
if (err < 0)
return err;
+ spin_lock_irqsave(&chan->lock, flags);
}
- spin_lock_irqsave(&chan->lock, flags);
-
cookie = dma_cookie_assign(tx);
/* Put this transaction onto the tail of the pending queue */
@@ -2311,15 +2467,26 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_peripheral_dma_vec(
struct xilinx_axidma_tx_segment *segment, *head, *prev = NULL;
size_t copy;
size_t sg_used;
+ u32 start = 0, taken = 0;
unsigned int i;
+ int needed;
if (!is_slave_direction(direction) || direction != chan->direction)
return NULL;
+ needed = xilinx_axidma_count_vecs(chan, vecs, nb);
+ if (needed < 0)
+ return NULL;
+
desc = xilinx_dma_alloc_tx_descriptor(chan);
if (!desc)
return NULL;
+ if (xilinx_axidma_reserve_segments(chan, needed, &start)) {
+ xilinx_dma_free_tx_descriptor(chan, desc);
+ return NULL;
+ }
+
dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
desc->async_tx.tx_submit = xilinx_dma_tx_submit;
@@ -2331,10 +2498,8 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_peripheral_dma_vec(
while (sg_used < vecs[i].len) {
struct xilinx_axidma_desc_hw *hw;
- /* Get a free segment */
- segment = xilinx_axidma_alloc_tx_segment(chan);
- if (!segment)
- goto error;
+ segment = xilinx_axidma_run_segment(chan, start,
+ taken++);
/*
* Calculate the maximum number of bytes to transfer,
@@ -2362,9 +2527,6 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_peripheral_dma_vec(
}
}
- if (list_empty(&desc->segments))
- goto error;
-
head = list_first_entry(&desc->segments, struct xilinx_axidma_tx_segment, node);
desc->async_tx.phys = head->phys;
@@ -2381,10 +2543,6 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_peripheral_dma_vec(
desc->async_tx.metadata_ops = &xilinx_dma_metadata_ops;
return &desc->async_tx;
-
-error:
- xilinx_dma_free_tx_descriptor(chan, desc);
- return NULL;
}
/**
@@ -2410,16 +2568,28 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
struct scatterlist *sg;
size_t copy;
size_t sg_used;
+ u32 start = 0, taken = 0;
unsigned int i;
+ int needed;
if (!is_slave_direction(direction))
return NULL;
+ /* reserve exactly the segments the loop below builds */
+ needed = xilinx_axidma_count_sg(chan, sgl, sg_len);
+ if (needed < 0)
+ return NULL;
+
/* Allocate a transaction descriptor. */
desc = xilinx_dma_alloc_tx_descriptor(chan);
if (!desc)
return NULL;
+ if (xilinx_axidma_reserve_segments(chan, needed, &start)) {
+ xilinx_dma_free_tx_descriptor(chan, desc);
+ return NULL;
+ }
+
dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
desc->async_tx.tx_submit = xilinx_dma_tx_submit;
@@ -2431,10 +2601,8 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
while (sg_used < sg_dma_len(sg)) {
struct xilinx_axidma_desc_hw *hw;
- /* Get a free segment */
- segment = xilinx_axidma_alloc_tx_segment(chan);
- if (!segment)
- goto error;
+ segment = xilinx_axidma_run_segment(chan, start,
+ taken++);
/*
* Calculate the maximum number of bytes to transfer,
@@ -2466,9 +2634,6 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
}
}
- if (list_empty(&desc->segments))
- goto error;
-
segment = list_first_entry(&desc->segments,
struct xilinx_axidma_tx_segment, node);
desc->async_tx.phys = segment->phys;
@@ -2486,10 +2651,6 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
desc->async_tx.metadata_ops = &xilinx_dma_metadata_ops;
return &desc->async_tx;
-
-error:
- xilinx_dma_free_tx_descriptor(chan, desc);
- return NULL;
}
/**
@@ -2513,7 +2674,8 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
size_t copy, sg_used, periods;
unsigned int num_periods;
- int i;
+ u32 start = 0, taken = 0;
+ int i, per_period;
u32 reg;
if (!period_len)
@@ -2528,11 +2690,24 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
if (!is_slave_direction(direction))
return NULL;
+ /* the division bounds per_period * num_periods without overflow */
+ per_period = xilinx_axidma_count_split(chan, period_len);
+ if (per_period <= 0)
+ return NULL;
+ if ((u32)per_period > XILINX_DMA_NUM_DESCS / num_periods)
+ return NULL;
+
/* Allocate a transaction descriptor. */
desc = xilinx_dma_alloc_tx_descriptor(chan);
if (!desc)
return NULL;
+ if (xilinx_axidma_reserve_segments(chan, per_period * num_periods,
+ &start)) {
+ xilinx_dma_free_tx_descriptor(chan, desc);
+ return NULL;
+ }
+
chan->direction = direction;
dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
desc->async_tx.tx_submit = xilinx_dma_tx_submit;
@@ -2543,10 +2718,8 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
while (sg_used < period_len) {
struct xilinx_axidma_desc_hw *hw;
- /* Get a free segment */
- segment = xilinx_axidma_alloc_tx_segment(chan);
- if (!segment)
- goto error;
+ segment = xilinx_axidma_run_segment(chan, start,
+ taken++);
/*
* Calculate the maximum number of bytes to transfer,
@@ -2594,10 +2767,6 @@ static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
}
return &desc->async_tx;
-
-error:
- xilinx_dma_free_tx_descriptor(chan, desc);
- return NULL;
}
/**
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-16 3:43 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 3:43 [PATCH v2 0/3] dmaengine: xilinx_dma: Fix AXI DMA descriptor ring ordering Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 1/3] dmaengine: xilinx_dma: Reject transfers that map no data Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 2/3] dmaengine: xilinx_dma: Fix cyclic period count truncation Jeff C. Jensen via B4 Relay
2026-11-16 3:43 ` [PATCH v2 3/3] dmaengine: xilinx_dma: Allocate AXI DMA segments in ring order Jeff C. Jensen via B4 Relay
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®