2023-10-04 16:37:04

by Amelie Delaunay

[permalink] [raw]
Subject: [PATCH 1/3] dmaengine: stm32-mdma: abort resume if no ongoing transfer

chan->desc can be null, if transfer is terminated when resume is called,
leading to a NULL pointer when retrieving the hwdesc.
To avoid this case, check that chan->desc is not null and channel is
disabled (transfer previously paused or terminated).

Fixes: a4ffb13c8946 ("dmaengine: Add STM32 MDMA driver")
Signed-off-by: Amelie Delaunay <[email protected]>
Cc: [email protected]
---
drivers/dma/stm32-mdma.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/dma/stm32-mdma.c b/drivers/dma/stm32-mdma.c
index 0de234022c6d..cc6f4b00091f 100644
--- a/drivers/dma/stm32-mdma.c
+++ b/drivers/dma/stm32-mdma.c
@@ -1236,6 +1236,10 @@ static int stm32_mdma_resume(struct dma_chan *c)
unsigned long flags;
u32 status, reg;

+ /* Transfer can be terminated */
+ if (!chan->desc || (stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)) & STM32_MDMA_CCR_EN))
+ return -EPERM;
+
hwdesc = chan->desc->node[chan->curr_hwdesc].hwdesc;

spin_lock_irqsave(&chan->vchan.lock, flags);
--
2.25.1


2023-10-04 16:37:58

by Amelie Delaunay

[permalink] [raw]
Subject: [PATCH 2/3] dmaengine: stm32-mdma: use Link Address Register to compute residue

Current implementation relies on curr_hwdesc index. But to keep this index
up to date, Block Transfer interrupt (BTIE) has to be enabled.
If it is not, curr_hwdesc is not updated, and then residue is not reliable.
Rely on Link Address Register instead. And disable BTIE interrupt
in stm32_mdma_setup_xfer() because it is no more needed in case of
_prep_slave_sg() to maintain curr_hwdesc up to date.
It avoids extra interrupts and also ensures a reliable residue. These
improvements are required for STM32 DCMI camera capture use case, which
need STM32 DMA and MDMA chaining for good performance.

Fixes: 696874322771 ("dmaengine: stm32-mdma: add support to be triggered by STM32 DMA")
Signed-off-by: Amelie Delaunay <[email protected]>
Cc: [email protected]
---
drivers/dma/stm32-mdma.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/stm32-mdma.c b/drivers/dma/stm32-mdma.c
index cc6f4b00091f..da73e13b8c9d 100644
--- a/drivers/dma/stm32-mdma.c
+++ b/drivers/dma/stm32-mdma.c
@@ -777,8 +777,6 @@ static int stm32_mdma_setup_xfer(struct stm32_mdma_chan *chan,
/* Enable interrupts */
ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE;
- if (sg_len > 1)
- ccr |= STM32_MDMA_CCR_BTIE;
desc->ccr = ccr;

return 0;
@@ -1324,12 +1322,21 @@ static size_t stm32_mdma_desc_residue(struct stm32_mdma_chan *chan,
{
struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
struct stm32_mdma_hwdesc *hwdesc;
- u32 cbndtr, residue, modulo, burst_size;
+ u32 cisr, clar, cbndtr, residue, modulo, burst_size;
int i;

+ cisr = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
+
residue = 0;
- for (i = curr_hwdesc + 1; i < desc->count; i++) {
+ /* Get the next hw descriptor to process from current transfer */
+ clar = stm32_mdma_read(dmadev, STM32_MDMA_CLAR(chan->id));
+ for (i = desc->count - 1; i >= 0; i--) {
hwdesc = desc->node[i].hwdesc;
+
+ if (hwdesc->clar == clar)
+ break;/* Current transfer found, stop cumulating */
+
+ /* Cumulate residue of unprocessed hw descriptors */
residue += STM32_MDMA_CBNDTR_BNDT(hwdesc->cbndtr);
}
cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
--
2.25.1

2023-10-09 06:13:47

by Vinod Koul

[permalink] [raw]
Subject: Re: [PATCH 1/3] dmaengine: stm32-mdma: abort resume if no ongoing transfer


On Wed, 04 Oct 2023 18:35:28 +0200, Amelie Delaunay wrote:
> chan->desc can be null, if transfer is terminated when resume is called,
> leading to a NULL pointer when retrieving the hwdesc.
> To avoid this case, check that chan->desc is not null and channel is
> disabled (transfer previously paused or terminated).
>
>

Applied, thanks!

[1/3] dmaengine: stm32-mdma: abort resume if no ongoing transfer
commit: 81337b9a72dc58a5fa0ae8a042e8cb59f9bdec4a
[2/3] dmaengine: stm32-mdma: use Link Address Register to compute residue
commit: a4b306eb83579c07b63dc65cd5bae53b7b4019d0
[3/3] dmaengine: stm32-mdma: set in_flight_bytes in case CRQA flag is set
commit: 584970421725b7805db84714b857851fdf7203a9

Best regards,
--
~Vinod