dmaengine: introduce sg_nents_for_dma() and convert users
Subsystems (11)
v5 cover dmaengine: introduce sg_nents_for_dma() and convert users
A handful of the DMAengine drivers use same routine to calculate the number of SG entries needed for the given DMA transfer. Provide a common helper for them and convert.
I left the new helper on SG level of API because brief grepping shows potential candidates outside of DMA engine, e.g.:
drivers/crypto/chelsio/chcr_algo.c:154: nents += DIV_ROUND_UP(less, entlen); drivers/spi/spi-stm32.c:1495: /* Count the number of entries needed */
Changelog v5:
- fixed W=1 warning (Vinod)
- make W=1 the whole folder in allyesconfig and allmodconfig configurations
v4: <[email protected]>
Changelog v4:
- fixed compilation errors (Vinod)
v3: <[email protected]>
Changelog v3:
- added missed EXPORT_SYMBOL() (Bjorn)
- left the return type as signed int (as agreed with Bjorn)
- collected tags (Bjorn)
v2: <[email protected]>
Changelog v2:
- dropped outdated patches (only 9 years passed :-)
- rebased on top of the current kernel
- left API SG wide It might
v1: https://patchwork.kernel.org/project/linux-dmaengine/patch/[email protected]/
*** BLURB HERE ***
Andy Shevchenko (13):
scatterlist: introduce sg_nents_for_dma() helper
dmaengine: altera-msgdma: use sg_nents_for_dma() helper
dmaengine: axi-dmac: use sg_nents_for_dma() helper
dmaengine: bcm2835-dma: use sg_nents_for_dma() helper
dmaengine: dw-axi-dmac: use sg_nents_for_dma() helper
dmaengine: k3dma: use sg_nents_for_dma() helper
dmaengine: lgm: use sg_nents_for_dma() helper
dmaengine: pxa-dma: use sg_nents_for_dma() helper
dmaengine: qcom: adm: use sg_nents_for_dma() helper
dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper
dmaengine: sa11x0: use sg_nents_for_dma() helper
dmaengine: sh: use sg_nents_for_dma() helper
dmaengine: xilinx: xdma: use sg_nents_for_dma() helper
drivers/dma/altera-msgdma.c | 6 ++--- drivers/dma/bcm2835-dma.c | 19 +------------- drivers/dma/dma-axi-dmac.c | 5 +--- .../dma/dw-axi-dmac/dw-axi-dmac-platform.c | 6 ++--- drivers/dma/k3dma.c | 9 ++----- drivers/dma/lgm/lgm-dma.c | 9 ++----- drivers/dma/pxa_dma.c | 5 ++-- drivers/dma/qcom/bam_dma.c | 9 ++----- drivers/dma/qcom/qcom_adm.c | 9 +++---- drivers/dma/sa11x0-dma.c | 6 ++--- drivers/dma/sh/shdma-base.c | 5 ++-- drivers/dma/xilinx/xdma.c | 6 ++--- include/linux/scatterlist.h | 2 ++ lib/scatterlist.c | 26 +++++++++++++++++++ 14 files changed, 52 insertions(+), 70 deletions(-)
Signature · 1 line
2.50.1
PATCH v5 1/13 scatterlist: introduce sg_nents_for_dma() helper
Sometimes the user needs to split each entry on the mapped scatter list due to DMA length constrains. This helper returns a number of entities assuming that each of them is not bigger than supplied maximum length.
--- include/linux/scatterlist.h | 2 ++ lib/scatterlist.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+)
include/linux/scatterlist.h +2 -0
@@ -441,6 +441,8 @@ static inline void sg_init_marker(struct scatterlist *sgl,int sg_nents (struct scatterlist *sg);int sg_nents_for_len (struct scatterlist *sg,u64 len); +int sg_nents_for_dma (struct scatterlist *sgl,unsigned int sglen,size_t len); +struct scatterlist *sg_last (struct scatterlist *s,unsigned int );void sg_init_table (struct scatterlist *,unsigned int );void sg_init_one (struct scatterlist *,const void *,unsigned int );
lib/scatterlist.c +26 -0
@@ -64,6 +64,32 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len) }EXPORT_SYMBOL (sg_nents_for_len); +/** +* sg_nents_for_dma - return the count of DMA-capable entries in scatterlist +* @sgl: The scatterlist +* @sglen: The current number of entries +* @len: The maximum length of DMA-capable block +* +* Description: +* Determines the number of entries in @sgl which would be permitted in +* DMA-capable transfer if list had been split accordingly, taking into +* account chaining as well. +* +* Returns: +* the number of sgl entries needed +* +**/ +int sg_nents_for_dma (struct scatterlist *sgl,unsigned int sglen,size_t len) +{ +struct scatterlist *sg; +int i, nents =0 ; + +for_each_sg (sgl, sg, sglen, i) + nents +=DIV_ROUND_UP (sg_dma_len (sg), len); +return nents; +} +EXPORT_SYMBOL (sg_nents_for_dma); +/** * sg_last - return the last scatterlist entry in a list * @sgl: First entry in the scatterlist
Signature · 1 line
2.50.1
PATCH v5 2/13 dmaengine: altera-msgdma: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/altera-msgdma.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/altera-msgdma.c +2 -4
@@ -396,13 +396,11 @@ msgdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,void *desc =NULL ;size_t len, avail;dma_addr_t dma_dst, dma_src; -u32 desc_cnt =0 , i; -struct scatterlist *sg; +u32 desc_cnt;u32 stride;unsigned long irqflags; -for_each_sg (sgl, sg, sg_len, i) - desc_cnt +=DIV_ROUND_UP (sg_dma_len (sg),MSGDMA_MAX_TRANS_LEN ); + desc_cnt =sg_nents_for_dma (sgl, sg_len,MSGDMA_MAX_TRANS_LEN );spin_lock_irqsave (&mdev->lock, irqflags);if (desc_cnt > mdev->desc_free_cnt) {
Signature · 1 line
2.50.1
PATCH v5 3/13 dmaengine: axi-dmac: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/dma-axi-dmac.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/dma/dma-axi-dmac.c +1 -4
@@ -677,10 +677,7 @@ static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg(if (direction != chan->direction)return NULL ; - num_sgs =0 ; -for_each_sg (sgl, sg, sg_len, i) - num_sgs +=DIV_ROUND_UP (sg_dma_len (sg), chan->max_length); - + num_sgs =sg_nents_for_dma (sgl, sg_len, chan->max_length); desc =axi_dmac_alloc_desc (chan, num_sgs);if (!desc)return NULL ;
Signature · 1 line
2.50.1
Re: [PATCH v5 03/13] dmaengine: axi-dmac: use sg_nents_for_dma() helper
Quoted 5 lines · Andy Shevchenko
Instead of open coded variant let's use recently introduced helper.
Reviewed-by Bjorn Andersson <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>---
Quoted 19 lines
drivers/dma/dma-axi-dmac.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)drivers/dma/dma-axi-dmac.c +0
-0@@ -677,10 +677,7 @@ static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg( if (direction != chan->direction) return NULL;drivers/dma/dma-axi-dmac.c +1
-4- num_sgs =0 ; -for_each_sg (sgl, sg, sg_len, i) - num_sgs +=DIV_ROUND_UP (sg_dma_len (sg), chan->max_length); - + num_sgs =sg_nents_for_dma (sgl, sg_len, chan->max_length);desc =
axi_dmac_alloc_desc(chan, num_sgs); if (!desc)
return NULL;
PATCH v5 4/13 dmaengine: bcm2835-dma: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/bcm2835-dma.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-)
drivers/dma/bcm2835-dma.c +1 -18
@@ -260,23 +260,6 @@ static void bcm2835_dma_create_cb_set_length( control_block->info |= finalextrainfo; } -static inline size_t bcm2835_dma_count_frames_for_sg ( -struct bcm2835_chan *c, -struct scatterlist *sgl, -unsigned int sg_len) -{ -size_t frames =0 ; -struct scatterlist *sgent; -unsigned int i; -size_t plength =bcm2835_dma_max_frame_length (c); - -for_each_sg (sgl, sgent, sg_len, i) - frames +=bcm2835_dma_frames_for_length ( -sg_dma_len (sgent), plength); - -return frames; -} -/** * bcm2835_dma_create_cb_chain - create a control block and fills data in *
@@ -672,7 +655,7 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg( }/* count frames in sg list */ - frames =bcm2835_dma_count_frames_for_sg (c, sgl, sg_len); + frames =sg_nents_for_dma (sgl, sg_len,bcm2835_dma_max_frame_length (c));/* allocate the CB chain */ d = bcm2835_dma_create_cb_chain(chan, direction, false,
Signature · 1 line
2.50.1
PATCH v5 5/13 dmaengine: dw-axi-dmac: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c +2 -4
@@ -850,7 +850,7 @@ dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,unsigned int loop =0 ;struct scatterlist *sg;size_t axi_block_len; -u32 len, num_sgs =0 ; +u32 len, num_sgs;unsigned int i;dma_addr_t mem;int status;
@@ -867,9 +867,7 @@ dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,if (axi_block_len ==0 )return NULL ; -for_each_sg (sgl, sg, sg_len, i) - num_sgs +=DIV_ROUND_UP (sg_dma_len (sg), axi_block_len); - + num_sgs =sg_nents_for_dma (sgl, sg_len, axi_block_len); desc =axi_desc_alloc (num_sgs);if (unlikely (!desc)) goto err_desc_get;
Signature · 1 line
2.50.1
PATCH v5 6/13 dmaengine: k3dma: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/k3dma.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/k3dma.c +2 -7
@@ -536,19 +536,14 @@ static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(size_t len, avail, total =0 ;struct scatterlist *sg;dma_addr_t addr, src =0 , dst =0 ; -int num = sglen, i; +int num, i;if (sgl ==NULL )return NULL ; c->cyclic =0 ; -for_each_sg (sgl, sg, sglen, i) { - avail =sg_dma_len (sg); -if (avail >DMA_MAX_SIZE ) - num +=DIV_ROUND_UP (avail,DMA_MAX_SIZE ) -1 ; - } - + num =sg_nents_for_dma (sgl, sglen,DMA_MAX_SIZE ); ds =k3_dma_alloc_desc_resource (num, chan);if (!ds)return NULL ;
Signature · 1 line
2.50.1
PATCH v5 7/13 dmaengine: lgm: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/lgm/lgm-dma.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/lgm/lgm-dma.c +2 -7
@@ -1164,8 +1164,8 @@ ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,struct dw2_desc *hw_ds;struct dw2_desc_sw *ds;struct scatterlist *sg; -int num = sglen, i;dma_addr_t addr; +int num, i;if (!sgl)return NULL ;
@@ -1173,12 +1173,7 @@ ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,if (d->ver >DMA_VER22 )return ldma_chan_desc_cfg (chan, sgl->dma_address, sglen); -for_each_sg (sgl, sg, sglen, i) { - avail =sg_dma_len (sg); -if (avail >DMA_MAX_SIZE ) - num +=DIV_ROUND_UP (avail,DMA_MAX_SIZE ) -1 ; - } - + num =sg_nents_for_dma (sgl, sglen,DMA_MAX_SIZE ); ds =dma_alloc_desc_resource (num, c);if (!ds)return NULL ;
Signature · 1 line
2.50.1
PATCH v5 8/13 dmaengine: pxa-dma: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/pxa_dma.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dma/pxa_dma.c +2 -3
@@ -970,7 +970,7 @@ pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,struct scatterlist *sg;dma_addr_t dma;u32 dcmd, dsadr =0 , dtadr =0 ; -unsigned int nb_desc =0 , i, j =0 ; +unsigned int nb_desc, i, j =0 ;if ((sgl ==NULL ) || (sg_len ==0 ))return NULL ;
@@ -979,8 +979,7 @@ pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,dev_dbg (&chan->vc.chan.dev->device,"%s(): dir=%d flags=%lx\n" , __func__, dir, flags); -for_each_sg (sgl, sg, sg_len, i) - nb_desc +=DIV_ROUND_UP (sg_dma_len (sg),PDMA_MAX_DESC_BYTES ); + nb_desc =sg_nents_for_dma (sgl, sg_len,PDMA_MAX_DESC_BYTES ); sw_desc =pxad_alloc_desc (chan, nb_desc +1 );if (!sw_desc)return NULL ;
Signature · 1 line
2.50.1
PATCH v5 9/13 dmaengine: qcom: adm: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/qcom/qcom_adm.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/dma/qcom/qcom_adm.c +4 -5
@@ -390,16 +390,15 @@ static struct dma_async_tx_descriptor *adm_prep_slave_sg(struct dma_chan *chan, }/* iterate through sgs and compute allocation size of structures */ -for_each_sg (sgl, sg, sg_len, i) { -if (achan->slave.device_fc) { +if (achan->slave.device_fc) { +for_each_sg (sgl, sg, sg_len, i) { box_count +=DIV_ROUND_UP (sg_dma_len (sg) / burst,ADM_MAX_ROWS );if (sg_dma_len (sg) % burst) single_count++; - }else { - single_count +=DIV_ROUND_UP (sg_dma_len (sg), -ADM_MAX_XFER ); } + }else { + single_count =sg_nents_for_dma (sgl, sg_len,ADM_MAX_XFER ); } async_desc =kzalloc (sizeof (*async_desc),GFP_NOWAIT );
Signature · 1 line
2.50.1
PATCH v5 10/13 dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/qcom/bam_dma.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/qcom/bam_dma.c +2 -7
@@ -653,22 +653,17 @@ static struct dma_async_tx_descriptor *bam_prep_slave_sg(struct dma_chan *chan,struct scatterlist *sg;u32 i;struct bam_desc_hw *desc; -unsigned int num_alloc =0 ; - +unsigned int num_alloc;if (!is_slave_direction (direction)) {dev_err (bdev->dev,"invalid dma direction\n" );return NULL ; } -/* calculate number of required entries */ -for_each_sg (sgl, sg, sg_len, i) - num_alloc +=DIV_ROUND_UP (sg_dma_len (sg),BAM_FIFO_SIZE ); -/* allocate enough room to accommodate the number of entries */ + num_alloc =sg_nents_for_dma (sgl, sg_len,BAM_FIFO_SIZE ); async_desc =kzalloc (struct_size (async_desc, desc, num_alloc),GFP_NOWAIT ); -if (!async_desc)return NULL ;
Signature · 1 line
2.50.1
PATCH v5 11/13 dmaengine: sa11x0: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/sa11x0-dma.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/sa11x0-dma.c +2 -4
@@ -526,7 +526,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(struct sa11x0_dma_chan *c =to_sa11x0_dma_chan (chan);struct sa11x0_dma_desc *txd;struct scatterlist *sgent; -unsigned i, j = sglen; +unsigned int i, j;size_t size =0 ;/* SA11x0 channels can only operate in their native direction */
@@ -542,10 +542,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(for_each_sg (sg, sgent, sglen, i) {dma_addr_t addr =sg_dma_address (sgent); -unsigned int len =sg_dma_len (sgent); -if (len >DMA_MAX_SIZE ) - j +=DIV_ROUND_UP (len,DMA_MAX_SIZE & ~DMA_ALIGN ) -1 ;if (addr &DMA_ALIGN ) {dev_dbg (chan->device->dev,"vchan %p: bad buffer alignment: %pad\n" , &c->vc, &addr);
@@ -553,6 +550,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg( } } + j =sg_nents_for_dma (sg, sglen,DMA_MAX_SIZE & ~DMA_ALIGN ); txd =kzalloc (struct_size (txd, sg, j),GFP_ATOMIC );if (!txd) {dev_dbg (chan->device->dev,"vchan %p: kzalloc failed\n" , &c->vc);
Signature · 1 line
2.50.1
PATCH v5 12/13 dmaengine: sh: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/sh/shdma-base.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dma/sh/shdma-base.c +2 -3
@@ -577,12 +577,11 @@ static struct dma_async_tx_descriptor *shdma_prep_sg(struct shdma_chan *schan,struct scatterlist *sg;struct shdma_desc *first =NULL , *new =NULL /* compiler... */ ;LIST_HEAD (tx_list); -int chunks =0 ; +int chunks;unsigned long irq_flags;int i; -for_each_sg (sgl, sg, sg_len, i) - chunks +=DIV_ROUND_UP (sg_dma_len (sg), schan->max_xfer_len); + chunks =sg_nents_for_dma (sgl, sg_len, schan->max_xfer_len);/* Have to lock the whole loop to protect against concurrent release */ spin_lock_irqsave (&schan->chan_lock, irq_flags);
Signature · 1 line
2.50.1
PATCH v5 13/13 dmaengine: xilinx: xdma: use sg_nents_for_dma() helper
In reply to Andy ShevchenkoInstead of open coded variant let's use recently introduced helper.
--- drivers/dma/xilinx/xdma.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/xilinx/xdma.c +2 -4
@@ -605,13 +605,11 @@ xdma_prep_device_sg(struct dma_chan *chan, struct scatterlist *sgl,struct xdma_chan *xdma_chan =to_xdma_chan (chan);struct dma_async_tx_descriptor *tx_desc;struct xdma_desc *sw_desc; -u32 desc_num =0 , i;u64 addr, dev_addr, *src, *dst; +u32 desc_num, i;struct scatterlist *sg; -for_each_sg (sgl, sg, sg_len, i) - desc_num +=DIV_ROUND_UP (sg_dma_len (sg),XDMA_DESC_BLEN_MAX ); - + desc_num =sg_nents_for_dma (sgl, sg_len,XDMA_DESC_BLEN_MAX ); sw_desc =xdma_alloc_desc (xdma_chan, desc_num, false);if (!sw_desc)return NULL ;
Signature · 1 line
2.50.1
Quoted 8 lines · Andy Shevchenko
A handful of the DMAengine drivers use same routine to calculate the number of SG entries needed for the given DMA transfer. Provide a common helper for them and convert.
I left the new helper on SG level of API because brief grepping shows potential candidates outside of DMA engine, e.g.:
[...]
Applied, thanks!
[01/13] scatterlist: introduce sg_nents_for_dma() helper
commit: 80c70bfb95cdbe0c644070f4ca4754a60f0a4830
[02/13] dmaengine: altera-msgdma: use sg_nents_for_dma() helper
commit: 47f5cb7878cc62ed95981c5d02862b253eddb590
[03/13] dmaengine: axi-dmac: use sg_nents_for_dma() helper
commit: 024ae9d3092c425f3ea6eae92086a2001ca2e0c7
[04/13] dmaengine: bcm2835-dma: use sg_nents_for_dma() helper
commit: 39110c68500a149664bafb0c174baef0d42e2129
[05/13] dmaengine: dw-axi-dmac: use sg_nents_for_dma() helper
commit: 5d6ceb254fa9ac1f50932c42a0a9a8bedaa3190d
[06/13] dmaengine: k3dma: use sg_nents_for_dma() helper
commit: 3fc49d21f3a46866724ff8ef8a79c6e2cd9d7676
[07/13] dmaengine: lgm: use sg_nents_for_dma() helper
commit: f9b0274f53a2d464e71f37e8f4d0d0dc41321259
[08/13] dmaengine: pxa-dma: use sg_nents_for_dma() helper
commit: 068942eaa232ba752b744d98ff8ab22b26c8bff4
[09/13] dmaengine: qcom: adm: use sg_nents_for_dma() helper
commit: 425f871d7acdb521d67c2578e6ae688a751d1e80
[10/13] dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper
commit: 107fdf0c4e944030bf544aea98e8ae8537914177
[11/13] dmaengine: sa11x0: use sg_nents_for_dma() helper
commit: d7785661010e2fe113aec2500f988a8e73ac3e7b
[12/13] dmaengine: sh: use sg_nents_for_dma() helper
commit: ac326dca6870f0d8e0787e94b1d9c2c91bb358d7
[13/13] dmaengine: xilinx: xdma: use sg_nents_for_dma() helper
commit: 3c8a86ed002ab8fb287ee4ec92f0fd6ac5b291d2Best regards,
Signature · 1 line
~Vinod