Skip to content

Commit 131ee27

Browse files
fancergregkh
authored andcommitted
dmaengine: dw: Add memory bus width verification
[ Upstream commit d04b21b ] Currently in case of the DEV_TO_MEM or MEM_TO_DEV DMA transfers the memory data width (single transfer width) is determined based on the buffer length, buffer base address or DMA master-channel max address width capability. It isn't enough in case of the channel disabling prior the block transfer is finished. Here is what DW AHB DMA IP-core databook says regarding the port suspension (DMA-transfer pause) implementation in the controller: "When CTLx.SRC_TR_WIDTH < CTLx.DST_TR_WIDTH and the CFGx.CH_SUSP bit is high, the CFGx.FIFO_EMPTY is asserted once the contents of the FIFO do not permit a single word of CTLx.DST_TR_WIDTH to be formed. However, there may still be data in the channel FIFO, but not enough to form a single transfer of CTLx.DST_TR_WIDTH. In this scenario, once the channel is disabled, the remaining data in the channel FIFO is not transferred to the destination peripheral." So in case if the port gets to be suspended and then disabled it's possible to have the data silently discarded even though the controller reported that FIFO is empty and the CTLx.BLOCK_TS indicated the dropped data already received from the source device. This looks as if the data somehow got lost on a way from the peripheral device to memory and causes problems for instance in the DW APB UART driver, which pauses and disables the DMA-transfer as soon as the recv data timeout happens. Here is the way it looks: Memory <------- DMA FIFO <------ UART FIFO <---------------- UART DST_TR_WIDTH -+--------| | | | | | | No more data Current lvl -+--------| |---------+- DMA-burst lvl | | |---------+- Leftover data | | |---------+- SRC_TR_WIDTH -+--------+-------+---------+ In the example above: no more data is getting received over the UART port and BLOCK_TS is not even close to be fully received; some data is left in the UART FIFO, but not enough to perform a bursted DMA-xfer to the DMA FIFO; some data is left in the DMA FIFO, but not enough to be passed further to the system memory in a single transfer. In this situation the 8250 UART driver catches the recv timeout interrupt, pauses the DMA-transfer and terminates it completely, after which the IRQ handler manually fetches the leftover data from the UART FIFO into the recv-buffer. But since the DMA-channel has been disabled with the data left in the DMA FIFO, that data will be just discarded and the recv-buffer will have a gap of the "current lvl" size in the recv-buffer at the tail of the lately received data portion. So the data will be lost just due to the misconfigured DMA transfer. Note this is only relevant for the case of the transfer suspension and _disabling_. No problem will happen if the transfer will be re-enabled afterwards or the block transfer is fully completed. In the later case the "FIFO flush mode" will be executed at the transfer final stage in order to push out the data left in the DMA FIFO. In order to fix the denoted problem the DW AHB DMA-engine driver needs to make sure that the _bursted_ source transfer width is greater or equal to the single destination transfer (note the HW databook describes more strict constraint than actually required). Since the peripheral-device side is prescribed by the client driver logic, the memory-side can be only used for that. The solution can be easily implemented for the DEV_TO_MEM transfers just by adjusting the memory-channel address width. Sadly it's not that easy for the MEM_TO_DEV transfers since the mem-to-dma burst size is normally dynamically determined by the controller. So the only thing that can be done is to make sure that memory-side address width is greater than the peripheral device address width. Fixes: a098200 ("dw_dmac: autoconfigure data_width or get it via platform data") Signed-off-by: Serge Semin <[email protected]> Acked-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent e219cf9 commit 131ee27

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

drivers/dma/dw/core.c

+44-7
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,10 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
625625
struct dw_desc *prev;
626626
struct dw_desc *first;
627627
u32 ctllo, ctlhi;
628-
u8 m_master = dwc->dws.m_master;
629-
u8 lms = DWC_LLP_LMS(m_master);
628+
u8 lms = DWC_LLP_LMS(dwc->dws.m_master);
630629
dma_addr_t reg;
631630
unsigned int reg_width;
632631
unsigned int mem_width;
633-
unsigned int data_width = dw->pdata->data_width[m_master];
634632
unsigned int i;
635633
struct scatterlist *sg;
636634
size_t total_len = 0;
@@ -664,7 +662,7 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
664662
mem = sg_dma_address(sg);
665663
len = sg_dma_len(sg);
666664

667-
mem_width = __ffs(data_width | mem | len);
665+
mem_width = __ffs(sconfig->src_addr_width | mem | len);
668666

669667
slave_sg_todev_fill_desc:
670668
desc = dwc_desc_get(dwc);
@@ -724,7 +722,7 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
724722
lli_write(desc, sar, reg);
725723
lli_write(desc, dar, mem);
726724
lli_write(desc, ctlhi, ctlhi);
727-
mem_width = __ffs(data_width | mem);
725+
mem_width = __ffs(sconfig->dst_addr_width | mem);
728726
lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width));
729727
desc->len = dlen;
730728

@@ -816,6 +814,41 @@ static int dwc_verify_p_buswidth(struct dma_chan *chan)
816814
return 0;
817815
}
818816

817+
static int dwc_verify_m_buswidth(struct dma_chan *chan)
818+
{
819+
struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
820+
struct dw_dma *dw = to_dw_dma(chan->device);
821+
u32 reg_width, reg_burst, mem_width;
822+
823+
mem_width = dw->pdata->data_width[dwc->dws.m_master];
824+
825+
/*
826+
* It's possible to have a data portion locked in the DMA FIFO in case
827+
* of the channel suspension. Subsequent channel disabling will cause
828+
* that data silent loss. In order to prevent that maintain the src and
829+
* dst transfer widths coherency by means of the relation:
830+
* (CTLx.SRC_TR_WIDTH * CTLx.SRC_MSIZE >= CTLx.DST_TR_WIDTH)
831+
* Look for the details in the commit message that brings this change.
832+
*
833+
* Note the DMA configs utilized in the calculations below must have
834+
* been verified to have correct values by this method call.
835+
*/
836+
if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV) {
837+
reg_width = dwc->dma_sconfig.dst_addr_width;
838+
if (mem_width < reg_width)
839+
return -EINVAL;
840+
841+
dwc->dma_sconfig.src_addr_width = mem_width;
842+
} else if (dwc->dma_sconfig.direction == DMA_DEV_TO_MEM) {
843+
reg_width = dwc->dma_sconfig.src_addr_width;
844+
reg_burst = rounddown_pow_of_two(dwc->dma_sconfig.src_maxburst);
845+
846+
dwc->dma_sconfig.dst_addr_width = min(mem_width, reg_width * reg_burst);
847+
}
848+
849+
return 0;
850+
}
851+
819852
static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
820853
{
821854
struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
@@ -825,14 +858,18 @@ static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
825858
memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
826859

827860
dwc->dma_sconfig.src_maxburst =
828-
clamp(dwc->dma_sconfig.src_maxburst, 0U, dwc->max_burst);
861+
clamp(dwc->dma_sconfig.src_maxburst, 1U, dwc->max_burst);
829862
dwc->dma_sconfig.dst_maxburst =
830-
clamp(dwc->dma_sconfig.dst_maxburst, 0U, dwc->max_burst);
863+
clamp(dwc->dma_sconfig.dst_maxburst, 1U, dwc->max_burst);
831864

832865
ret = dwc_verify_p_buswidth(chan);
833866
if (ret)
834867
return ret;
835868

869+
ret = dwc_verify_m_buswidth(chan);
870+
if (ret)
871+
return ret;
872+
836873
dw->encode_maxburst(dwc, &dwc->dma_sconfig.src_maxburst);
837874
dw->encode_maxburst(dwc, &dwc->dma_sconfig.dst_maxburst);
838875

0 commit comments

Comments
 (0)