spi: Make core DMA mapping functions generate scatterlists
Subsystems (3)
PATCH 1/1 spi: Make core DMA mapping functions generate scatterlists
From: Mark Brown <[email protected]>
We cannot unconditionally use dma_map_single() to map data for use with SPI since transfers may exceed a page and virtual addresses may not be provided with physically contiguous pages. Further, addresses allocated using vmalloc() need to be mapped differently to other addresses.
Currently only the MXS driver handles all this, a few drivers do handle the possibility that buffers may not be physically contiguous which is the main potential problem but many don't even do that. Factoring this out into the core will make it easier for drivers to do a good job so if the driver is using the core DMA code then generate a scatterlist instead of mapping to a single address so do that.
This code is mainly based on a combination of the existing code in the MXS and PXA2xx drivers. In future we should be able to extend it to allow the core to concatenate adjacent transfers if they are compatible, improving performance.
Currently for simplicity clients are not allowed to use the scatterlist when they do DMA mapping, in the future the existing single address mappings will be replaced with use of the scatterlist most likely as part of pre-verifying transfers.
This change makes it mandatory to use scatterlists when using the core DMA mapping so update the s3c64xx driver to do this when used with dmaengine. Doing so makes the code more ugly but it is expected that the old s3c-dma code can be removed very soon.
--- drivers/spi/spi-s3c64xx.c | 14 +++++-- drivers/spi/spi.c | 101 ++++++++++++++++++++++++++++++++++------------ include/linux/spi/spi.h | 7 ++++ 3 files changed, 94 insertions(+), 28 deletions(-)
drivers/spi/spi-s3c64xx.c +11 -3
@@ -470,7 +470,7 @@ static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd, }static void prepare_dma (struct s3c64xx_spi_dma_data *dma, -unsigned len,dma_addr_t buf) +struct sg_table *sgt) {struct s3c64xx_spi_driver_data *sdd;struct dma_slave_config config;
@@ -496,8 +496,8 @@ static void prepare_dma(struct s3c64xx_spi_dma_data *dma,dmaengine_slave_config (dma->ch, &config); } - desc =dmaengine_prep_slave_single (dma->ch, buf, len, - dma->direction,DMA_PREP_INTERRUPT ); + desc =dmaengine_prep_slave_sg (dma->ch, sgt->sgl, sgt->nents, + dma->direction,DMA_PREP_INTERRUPT ); desc->callback = s3c64xx_spi_dmacb; desc->callback_param = dma;
@@ -616,7 +616,11 @@ static void enable_datapath(struct s3c64xx_spi_driver_data *sdd, chcfg |=S3C64XX_SPI_CH_TXCH_ON ;if (dma_mode) { modecfg |=S3C64XX_SPI_MODE_TXDMA_ON ; +#ifndef CONFIG_S3C_DMA +prepare_dma (&sdd->tx_dma, &xfer->tx_sg); +#else prepare_dma (&sdd->tx_dma, xfer->len, xfer->tx_dma); +#endif }else {switch (sdd->cur_bpw) {case 32 :
@@ -648,7 +652,11 @@ static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,writel (((xfer->len *8 / sdd->cur_bpw) &0xffff ) |S3C64XX_SPI_PACKET_CNT_EN , regs +S3C64XX_SPI_PACKET_CNT ); +#ifndef CONFIG_S3C_DMA +prepare_dma (&sdd->rx_dma, &xfer->rx_sg); +#else prepare_dma (&sdd->rx_dma, xfer->len, xfer->rx_dma); +#endif } }
drivers/spi/spi.c +76 -25
146 lines not on this page · raw message
include/linux/spi/spi.h +7 -0
@@ -24,6 +24,7 @@#include <linux/slab.h> #include <linux/kthread.h> #include <linux/completion.h> +#include <linux/scatterlist.h> struct dma_chan ;
@@ -268,6 +269,7 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)* @auto_runtime_pm: the core should ensure a runtime PM reference is held * while the hardware is prepared, using the parent * device for the spidev +* @max_dma_len: Maximum length of a DMA transfer for the device. * @prepare_transfer_hardware: a message will soon arrive from the queue * so the subsystem requests the driver to prepare the transfer hardware * by issuing this call
@@ -424,6 +426,7 @@ struct spi_master {bool cur_msg_prepared;bool cur_msg_mapped;struct completion xfer_completion; +size_t max_dma_len;int (*prepare_transfer_hardware)(struct spi_master *master); int (*transfer_one_message)(struct spi_master *master,
@@ -536,6 +539,8 @@ extern struct spi_master *spi_busnum_to_master(u16 busnum);* (optionally) changing the chipselect status, then starting * the next transfer or completing this @spi_message. * @transfer_list: transfers are sequenced through @spi_message.transfers +* @tx_sg: Scatterlist for transmit, currently not for client use +* @rx_sg: Scatterlist for receive, currently not for client use * * SPI transfers always write the same number of bytes as they read. * Protocol drivers should always provide @rx_buf and/or @tx_buf.
@@ -603,6 +608,8 @@ struct spi_transfer {dma_addr_t tx_dma;dma_addr_t rx_dma; +struct sg_table tx_sg; +struct sg_table rx_sg; unsigned cs_change:1; unsigned tx_nbits:3;
Signature · 6 lines
1.9.rc1 -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 1391349172-27668-1-git-send-email-broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
On Sunday, February 02, 2014 at 02:52:52 PM, Mark Brown wrote:
Hi, thanks for preparing this patch!
I have just a few very minor nitpicks, ignore if you please.
[...]
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 590
Quoted 6 lines
drivers/spi/spi.c +6
-0+static int spi_map_buf (struct spi_master *master,struct device *dev, +struct sg_table *sgt,void *buf,size_t len, +enum dma_data_direction dir) +{ +const bool vmalloced_buf =is_vmalloc_addr (buf); +const int desc_len = vmalloced_buf ?PAGE_SIZE : master->max_dma_len;
You might want to rename this to "sg_chunk_max_size" or something, "desc_len" doesn't make much sense here. The variable describes the maximum size of one single scatterlist element.
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 591
Quoted 1 line
drivers/spi/spi.c +1
-0+const int sgs =DIV_ROUND_UP (len, desc_len);
Looking at this, the variables could generally use a more meaningful name. I think it'd be clearer to call this "num_sg_chunks" or so ?
Quoted 4 lines
drivers/spi/spi.c +4
-0+struct page *vm_page; +void *sg_buf; +size_t min; +int i, ret;
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 605
Quoted 10 lines
drivers/spi/spi.c +10
-0+ + ret =sg_alloc_table (sgt, sgs,GFP_KERNEL ); +if (ret !=0 ) +return ret; + +for (i =0 ; i < sgs; i++) { + min =min_t (size_t, len, desc_len); + +if (vmalloced_buf) { + vm_page =vmalloc_to_page (buf);
Just curious, but shouldn't we check if buf != NULL right at the begining of this function?
[...]
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 636
Quoted 4 lines
drivers/spi/spi.c +4
-0+static void spi_unmap_buf (struct spi_master *master,struct device *dev, +struct sg_table *sgt,enum dma_data_direction dir) +{ +if (sgt->orig_nents) {
I don't want to nag, but why not use if (!sgt->...) return; ? This would cut down one level of indent.
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 641
Quoted 5 lines
drivers/spi/spi.c +5
-0+dma_unmap_sg (dev, sgt->sgl, sgt->orig_nents, dir); +sg_free_table (sgt); + } +} +
[...]
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Mark Brown
maintainerQuoted 12 lines · Marek Vasut
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 590
Quoted 6 lines · Mark Brown
drivers/spi/spi.c +6
-0+static int spi_map_buf (struct spi_master *master,struct device *dev, +struct sg_table *sgt,void *buf,size_t len, +enum dma_data_direction dir) +{ +const bool vmalloced_buf =is_vmalloc_addr (buf); +const int desc_len = vmalloced_buf ?PAGE_SIZE : master->max_dma_len;You might want to rename this to "
sg_chunk_max_size" or something, "desc_len" doesn't make much sense here. The variable describes the maximum size of one single scatterlist element.
A scatterlist entry is pretty much an abstract descriptor though. I seem to remember looking at the name and thinking it was good that it was something less easily applicable to the length of the table but it doesn't make much odds.
Quoted 4 lines
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 591
Quoted 1 line
drivers/spi/spi.c +1
-0+const int sgs =DIV_ROUND_UP (len, desc_len);Looking at this, the variables could generally use a more meaningful name. I think it'd be clearer to call this "
num_sg_chunks" or so ?
You do know where I lifted most of these variable names from, right? :P Looking at the code again everything seems idiomatic with the naming of the fields inside the sg_table - I probably would apply a patch to rename but I wouldn't write one.
Quoted 7 lines
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 605
Quoted 4 lines
drivers/spi/spi.c +4
-0+ min =min_t (size_t, len, desc_len); + +if (vmalloced_buf) { + vm_page =vmalloc_to_page (buf);Just curious, but shouldn't we check if buf != NULL right at the begining of this function?
No need, the check is outside the function along with the check that the controller is OK with DMAing on this transfer and so on.
Quoted 7 lines
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 636
Quoted 4 lines
drivers/spi/spi.c +4
-0+static void spi_unmap_buf (struct spi_master *master,struct device *dev, +struct sg_table *sgt,enum dma_data_direction dir) +{ +if (sgt->orig_nents) {I don't want to nag, but why not use if (!sgt->...) return; ? This would cut down one level of indent.
I was looking at some stuff which might add a bit more in here if it's not just the core doing mappings. Not sure that's sensible though so it might never materialise.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 13 lines · Mark Brown
Quoted 11 lines · Marek Vasut
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 590
Quoted 6 lines · Mark Brown
drivers/spi/spi.c +6
-0+static int spi_map_buf (struct spi_master *master,struct device *dev, +struct sg_table *sgt,void *buf,size_t len, +enum dma_data_direction dir) +{ +const bool vmalloced_buf =is_vmalloc_addr (buf); +const int desc_len = vmalloced_buf ?PAGE_SIZE : master->max_dma_len;You might want to rename this to "
sg_chunk_max_size" or something, "desc_len" doesn't make much sense here. The variable describes the maximum size of one single scatterlist element.
Quoted 4 lines
A scatterlist entry is pretty much an abstract descriptor though. I seem to remember looking at the name and thinking it was good that it was something less easily applicable to the length of the table but it doesn't make much odds.
It's the length of the entry, but I see your point.
Quoted 6 lines
Quoted 4 lines
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 591
Quoted 1 line
drivers/spi/spi.c +1
-0+const int sgs =DIV_ROUND_UP (len, desc_len);Looking at this, the variables could generally use a more meaningful name. I think it'd be clearer to call this "
num_sg_chunks" or so ?You do know where I lifted most of these variable names from, right? :P
Yeah, I don't like looking at my old code, it's always so frustrating ;-) Still, it's a good source for learning how to NOT do things again :)
Quoted 3 lines
Looking at the code again everything seems idiomatic with the naming of the fields inside the
sg_table- I probably would apply a patch to rename but I wouldn't write one.
OK, makes sense.
Quoted 10 lines
Quoted 7 lines
drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable) · line 605
Quoted 4 lines
drivers/spi/spi.c +4
-0+ min =min_t (size_t, len, desc_len); + +if (vmalloced_buf) { + vm_page =vmalloc_to_page (buf);Just curious, but shouldn't we check if buf != NULL right at the begining of this function?
No need, the check is outside the function along with the check that the controller is OK with DMAing on this transfer and so on.
OK, thank you for checking this.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
+static void spi_unmap_buf(struct
spi_master*master, struct device
*dev, + structsg_table*sgt, enumdma_data_direction
dir)
Quoted 9 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+{ + if (sgt->orig_nents) {I don't want to nag, but why not use if (!sgt->...) return; ? This would cut down one level of indent.
I was looking at some stuff which might add a bit more in here if it's not just the core doing mappings. Not sure that's sensible though so it might never materialise.
OK.
Best regards,
Marek Vasut
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html