STM32 DMA driver fixes and improvements
cover STM32 DMA driver fixes and improvements
This series brings improvements to the STM32 DMA driver, with support of power management and descriptor reuse. Probe function gets a cleanup and properties like copy_align and max_segment_size are set.
A "sleeping function called from invalid context" bug is also fixed. And to avoid a race with vchan_complete, driver now adopts vchan_terminate_vdesc().
Amelie Delaunay (4):
dmaengine: stm32-dma: use dma_set_max_seg_size to set the sg limit
dmaengine: stm32-dma: add copy_align constraint
dmaengine: stm32-dma: fix sleeping function called from invalid
context
dmaengine: stm32-dma: use vchan_terminate_vdesc() in .terminate_all
Etienne Carriere (2):
dmaengine: stm32-dma: use reset controller only at probe time
dmaengine: stm32-dma: driver defers probe for reset
Pierre-Yves MORDRET (2):
dmaengine: stm32-dma: add suspend/resume power management support
dmaengine: stm32-dma: enable descriptor_reuse
drivers/dma/stm32-dma.c | 96 ++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 29 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/8 dmaengine: stm32-dma: add suspend/resume power management support
From: Pierre-Yves MORDRET <[email protected]>
Add suspend/resume power management relying on PM Runtime engine.
--- drivers/dma/stm32-dma.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
drivers/dma/stm32-dma.c +32 -0
@@ -1427,7 +1427,39 @@ static int stm32_dma_runtime_resume(struct device *dev) }#endif +#ifdef CONFIG_PM_SLEEP +static int stm32_dma_suspend (struct device *dev) +{ +struct stm32_dma_device *dmadev =dev_get_drvdata (dev); +int id, ret, scr; + + ret =pm_runtime_get_sync (dev); +if (ret <0 ) +return ret; + +for (id =0 ; id <STM32_DMA_MAX_CHANNELS ; id++) { + scr =stm32_dma_read (dmadev,STM32_DMA_SCR (id)); +if (scr &STM32_DMA_SCR_EN ) { +dev_warn (dev,"Suspend is prevented by Chan %i\n" , id); +return -EBUSY ; + } + } + +pm_runtime_put_sync (dev); + +pm_runtime_force_suspend (dev); + +return 0 ; +} + +static int stm32_dma_resume (struct device *dev) +{ +return pm_runtime_force_resume (dev); +} +#endif +static const struct dev_pm_ops stm32_dma_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(stm32_dma_suspend, stm32_dma_resume)SET_RUNTIME_PM_OPS (stm32_dma_runtime_suspend, stm32_dma_runtime_resume,NULL ) };
Signature · 1 line
2.17.1
PATCH 2/8 dmaengine: stm32-dma: use reset controller only at probe time
In reply to Amelie DelaunayFrom: Etienne Carriere <[email protected]>
Remove reset controller reference from device instance since it is used only at probe time.
--- drivers/dma/stm32-dma.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/dma/stm32-dma.c +5 -5
@@ -207,7 +207,6 @@ struct stm32_dma_device {struct dma_device ddev;void __iomem *base;struct clk *clk; -struct reset_control *rst;bool mem2mem;struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS ]; };
@@ -1275,6 +1274,7 @@ static int stm32_dma_probe(struct platform_device *pdev)struct dma_device *dd;const struct of_device_id *match;struct resource *res; +struct reset_control *rst;int i, ret; match =of_match_device (stm32_dma_of_match, &pdev->dev);
@@ -1309,11 +1309,11 @@ static int stm32_dma_probe(struct platform_device *pdev) dmadev->mem2mem =of_property_read_bool (pdev->dev.of_node,"st,mem2mem" ); - dmadev->rst =devm_reset_control_get (&pdev->dev,NULL ); -if (!IS_ERR (dmadev->rst)) { -reset_control_assert (dmadev->rst); + rst =devm_reset_control_get (&pdev->dev,NULL ); +if (!IS_ERR (rst)) { +reset_control_assert (rst);udelay (2 ); -reset_control_deassert (dmadev->rst); +reset_control_deassert (rst); }dma_cap_set (DMA_SLAVE , dd->cap_mask);
Signature · 1 line
2.17.1
PATCH 3/8 dmaengine: stm32-dma: driver defers probe for reset
In reply to Amelie DelaunayFrom: Etienne Carriere <[email protected]>
Change STM32 DMA driver to defer its probe operation when reset controller is expected but has not been probed yet when DMA device is probed.
Changes error traces when failing to get a system resource so that it is not printed on failure with deferred probing.
--- drivers/dma/stm32-dma.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/dma/stm32-dma.c +11 -4
@@ -1296,8 +1296,10 @@ static int stm32_dma_probe(struct platform_device *pdev) dmadev->clk =devm_clk_get (&pdev->dev,NULL );if (IS_ERR (dmadev->clk)) { -dev_err (&pdev->dev,"Error: Missing controller clock\n" ); -return PTR_ERR (dmadev->clk); + ret =PTR_ERR (dmadev->clk); +if (ret != -EPROBE_DEFER ) +dev_err (&pdev->dev,"Can't get clock\n" ); +return ret; } ret =clk_prepare_enable (dmadev->clk);
@@ -1310,7 +1312,11 @@ static int stm32_dma_probe(struct platform_device *pdev) "st,mem2mem"); rst =devm_reset_control_get (&pdev->dev,NULL ); -if (!IS_ERR (rst)) { +if (IS_ERR (rst)) { + ret =PTR_ERR (rst); +if (ret == -EPROBE_DEFER ) + goto clk_free; + }else {reset_control_assert (rst);udelay (2 );reset_control_deassert (rst);
@@ -1470,10 +1476,11 @@ static struct platform_driver stm32_dma_driver = { .of_match_table = stm32_dma_of_match, .pm = &stm32_dma_pm_ops, }, + .probe = stm32_dma_probe, };static int __init stm32_dma_init (void ) { -return platform_driver_probe (&stm32_dma_driver, stm32_dma_probe); +return platform_driver_register (&stm32_dma_driver); }subsys_initcall (stm32_dma_init);
Signature · 1 line
2.17.1
PATCH 4/8 dmaengine: stm32-dma: enable descriptor_reuse
In reply to Amelie DelaunayFrom: Pierre-Yves MORDRET <[email protected]>
Enable client to resubmit already processed descriptors in order to save descriptor creation time.
--- drivers/dma/stm32-dma.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/dma/stm32-dma.c +2 -0
@@ -554,6 +554,7 @@ static void stm32_dma_start_transfer(struct stm32_dma_chan *chan) sg_req = &chan->desc->sg_req[chan->next_sg]; reg = &sg_req->chan_reg; + reg->dma_scr &= ~STM32_DMA_SCR_EN ;stm32_dma_write (dmadev,STM32_DMA_SCR (chan->id), reg->dma_scr);stm32_dma_write (dmadev,STM32_DMA_SPAR (chan->id), reg->dma_spar);stm32_dma_write (dmadev,STM32_DMA_SM0AR (chan->id), reg->dma_sm0ar);
@@ -1343,6 +1344,7 @@ static int stm32_dma_probe(struct platform_device *pdev) dd->directions =BIT (DMA_DEV_TO_MEM ) |BIT (DMA_MEM_TO_DEV ); dd->residue_granularity =DMA_RESIDUE_GRANULARITY_BURST ; dd->max_burst =STM32_DMA_MAX_BURST ; + dd->descriptor_reuse = true; dd->dev = &pdev->dev;INIT_LIST_HEAD (&dd->channels);
Signature · 1 line
2.17.1
PATCH 5/8 dmaengine: stm32-dma: use dma_set_max_seg_size to set the sg limit
In reply to Amelie DelaunayThis patch adds dma_set_max_seg_size to define sg dma constraint. This constraint may be taken into account by client to scatter/gather its buffer.
--- drivers/dma/stm32-dma.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/dma/stm32-dma.c +2 -0
@@ -1323,6 +1323,8 @@ static int stm32_dma_probe(struct platform_device *pdev)reset_control_deassert (rst); } +dma_set_max_seg_size (&pdev->dev,STM32_DMA_ALIGNED_MAX_DATA_ITEMS ); +dma_cap_set (DMA_SLAVE , dd->cap_mask);dma_cap_set (DMA_PRIVATE , dd->cap_mask);dma_cap_set (DMA_CYCLIC , dd->cap_mask);
Signature · 1 line
2.17.1
PATCH 6/8 dmaengine: stm32-dma: add copy_align constraint
In reply to Amelie DelaunayThis patch adds copy_align property in accordance with hardware restriction.
--- drivers/dma/stm32-dma.c | 1 + 1 file changed, 1 insertion(+)
drivers/dma/stm32-dma.c +1 -0
@@ -1345,6 +1345,7 @@ static int stm32_dma_probe(struct platform_device *pdev)BIT (DMA_SLAVE_BUSWIDTH_4_BYTES ); dd->directions =BIT (DMA_DEV_TO_MEM ) |BIT (DMA_MEM_TO_DEV ); dd->residue_granularity =DMA_RESIDUE_GRANULARITY_BURST ; + dd->copy_align =DMAENGINE_ALIGN_32_BYTES ; dd->max_burst =STM32_DMA_MAX_BURST ; dd->descriptor_reuse = true; dd->dev = &pdev->dev;
Signature · 1 line
2.17.1
PATCH 7/8 dmaengine: stm32-dma: fix sleeping function called from invalid context
In reply to Amelie DelaunayThis patch fixes BUG: sleeping function called from invalid context in stm32_dma_disable_chan function.
The goal of this function is to force channel disable if it has not been disabled by hardware. This consists in clearing STM32_DMA_SCR_EN bit and read it as 0 to ensure the channel is well disabled and the last transfer is over.
In previous implementation, the waiting loop was based on a do...while (1) with a call to cond_resched to give the scheduler a chance to run a higher priority process.
But in some conditions, stm32_dma_disable_chan can be called while preemption is disabled, on a stm32_dma_stop call for example. So cond_resched must not be used.
To avoid this, use readl_relaxed_poll_timeout_atomic to poll STM32_DMA_SCR_EN bit cleared.
--- drivers/dma/stm32-dma.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
drivers/dma/stm32-dma.c +9 -18
@@ -15,6 +15,7 @@#include <linux/dma-mapping.h> #include <linux/err.h> #include <linux/init.h> +#include <linux/iopoll.h> #include <linux/jiffies.h> #include <linux/list.h> #include <linux/module.h>
@@ -421,29 +422,19 @@ static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags)static int stm32_dma_disable_chan (struct stm32_dma_chan *chan) {struct stm32_dma_device *dmadev =stm32_dma_get_dev (chan); -unsigned long timeout = jiffies +msecs_to_jiffies (5000 ); -u32 dma_scr, id; +u32 dma_scr, id, reg; id = chan->id; - dma_scr =stm32_dma_read (dmadev,STM32_DMA_SCR (id)); + reg =STM32_DMA_SCR (id); + dma_scr =stm32_dma_read (dmadev, reg);if (dma_scr &STM32_DMA_SCR_EN ) { dma_scr &= ~STM32_DMA_SCR_EN ; -stm32_dma_write (dmadev,STM32_DMA_SCR (id), dma_scr); - -do { - dma_scr =stm32_dma_read (dmadev,STM32_DMA_SCR (id)); - dma_scr &=STM32_DMA_SCR_EN ; -if (!dma_scr) -break ; - -if (time_after_eq (jiffies, timeout)) { -dev_err (chan2dev (chan),"%s: timeout!\n" , - __func__); -return -EBUSY ; - } -cond_resched (); - }while (1 ); +stm32_dma_write (dmadev, reg, dma_scr); + +return readl_relaxed_poll_timeout_atomic (dmadev->base + reg, + dma_scr, !(dma_scr &STM32_DMA_SCR_EN ), +10 ,1000000 ); }return 0 ;
Signature · 1 line
2.17.1
PATCH 8/8 dmaengine: stm32-dma: use vchan_terminate_vdesc() in .terminate_all
In reply to Amelie DelaunayTo avoid race with vchan_complete, use the race free way to terminate running transfer.
Move vdesc->node list_del in stm32_dma_start_transfer instead of in stm32_mdma_chan_complete to avoid another race in vchan_dma_desc_free_list.
--- drivers/dma/stm32-dma.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/dma/stm32-dma.c +6 -3
@@ -478,8 +478,10 @@ static int stm32_dma_terminate_all(struct dma_chan *c)spin_lock_irqsave (&chan->vchan.lock, flags); -if (chan->busy) { -stm32_dma_stop (chan); +if (chan->desc) { +vchan_terminate_vdesc (&chan->desc->vdesc); +if (chan->busy) +stm32_dma_stop (chan); chan->desc =NULL ; }
@@ -535,6 +537,8 @@ static void stm32_dma_start_transfer(struct stm32_dma_chan *chan)if (!vdesc)return ; +list_del (&vdesc->node); + chan->desc =to_stm32_dma_desc (vdesc); chan->next_sg =0 ; }
@@ -613,7 +617,6 @@ static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan) }else { chan->busy = false;if (chan->next_sg == chan->desc->num_sgs) { -list_del (&chan->desc->vdesc.node);vchan_cookie_complete (&chan->desc->vdesc); chan->desc =NULL ; }
Signature · 1 line
2.17.1
Quoted 6 lines · Amelie Delaunay
This series brings improvements to the STM32 DMA driver, with support of power management and descriptor reuse. Probe function gets a cleanup and properties like
copy_alignandmax_segment_sizeare set.
A "sleeping function called from invalid context" bug is also fixed. And to avoid a race withvchan_complete, driver now adoptsvchan_terminate_vdesc().
Applied, thanks
Signature · 1 line
~Vinod