Commit 6d8fde79 authored by Ludovic Desroches's avatar Ludovic Desroches Committed by Sasha Levin

dmaengine: at_xdmac: fix residue corruption

[ Upstream commit 53398f48 ]

An unexpected value of CUBC can lead to a corrupted residue. A more
complex sequence is needed to detect an inaccurate value for NCA or CUBC.
Signed-off-by: default avatarLudovic Desroches <ludovic.desroches@atmel.com>
Fixes: e1f7c9ee ("dmaengine: at_xdmac: creation of the atmel
eXtended DMA Controller driver")
Cc: stable@vger.kernel.org #v4.1 and later
Reviewed-by: default avatarNicolas Ferre <nicolas.ferre@atmel.com>
Signed-off-by: default avatarVinod Koul <vinod.koul@intel.com>
Signed-off-by: default avatarSasha Levin <sasha.levin@oracle.com>
parent c6ec15d8
...@@ -930,6 +930,7 @@ at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie, ...@@ -930,6 +930,7 @@ at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
u32 cur_nda, check_nda, cur_ubc, mask, value; u32 cur_nda, check_nda, cur_ubc, mask, value;
u8 dwidth = 0; u8 dwidth = 0;
unsigned long flags; unsigned long flags;
bool initd;
ret = dma_cookie_status(chan, cookie, txstate); ret = dma_cookie_status(chan, cookie, txstate);
if (ret == DMA_COMPLETE) if (ret == DMA_COMPLETE)
...@@ -965,34 +966,43 @@ at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie, ...@@ -965,34 +966,43 @@ at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
} }
/* /*
* When processing the residue, we need to read two registers but we * The easiest way to compute the residue should be to pause the DMA
* can't do it in an atomic way. AT_XDMAC_CNDA is used to find where * but doing this can lead to miss some data as some devices don't
* we stand in the descriptor list and AT_XDMAC_CUBC is used * have FIFO.
* to know how many data are remaining for the current descriptor. * We need to read several registers because:
* Since the dma channel is not paused to not loose data, between the * - DMA is running therefore a descriptor change is possible while
* AT_XDMAC_CNDA and AT_XDMAC_CUBC read, we may have change of * reading these registers
* descriptor. * - When the block transfer is done, the value of the CUBC register
* For that reason, after reading AT_XDMAC_CUBC, we check if we are * is set to its initial value until the fetch of the next descriptor.
* still using the same descriptor by reading a second time * This value will corrupt the residue calculation so we have to skip
* AT_XDMAC_CNDA. If AT_XDMAC_CNDA has changed, it means we have to * it.
* read again AT_XDMAC_CUBC. *
* INITD -------- ------------
* |____________________|
* _______________________ _______________
* NDA @desc2 \/ @desc3
* _______________________/\_______________
* __________ ___________ _______________
* CUBC 0 \/ MAX desc1 \/ MAX desc2
* __________/\___________/\_______________
*
* Since descriptors are aligned on 64 bits, we can assume that
* the update of NDA and CUBC is atomic.
* Memory barriers are used to ensure the read order of the registers. * Memory barriers are used to ensure the read order of the registers.
* A max number of retries is set because unlikely it can never ends if * A max number of retries is set because unlikely it could never ends.
* we are transferring a lot of data with small buffers.
*/ */
cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
rmb();
cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) { for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
rmb();
check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc; check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
rmb();
if (likely(cur_nda == check_nda)) initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD);
break;
cur_nda = check_nda;
rmb(); rmb();
cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC); cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
rmb();
cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
rmb();
if ((check_nda == cur_nda) && initd)
break;
} }
if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) { if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment