Skip to contentSkip to conversation

STM32 DMA driver fixes and improvements

PATCH Applied Amelie Delaunay 2 participants 10 messages Jan 29, 2020 – Feb 25, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 5989b0893521..136deabd1aa3 100644
@@ -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
#2 lore raw

PATCH 2/8 dmaengine: stm32-dma: use reset controller only at probe time

In reply to Amelie Delaunay

From: 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
index 136deabd1aa3..e31414796ec4 100644
@@ -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
#3 lore raw

PATCH 3/8 dmaengine: stm32-dma: driver defers probe for reset

In reply to Amelie Delaunay

From: 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
index e31414796ec4..c8bbe08b8e32 100644
@@ -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
#4 lore raw

PATCH 4/8 dmaengine: stm32-dma: enable descriptor_reuse

In reply to Amelie Delaunay

From: 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
index c8bbe08b8e32..25f7281932bd 100644
@@ -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
#5 lore raw

PATCH 5/8 dmaengine: stm32-dma: use dma_set_max_seg_size to set the sg limit

In reply to Amelie Delaunay

This 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
index 25f7281932bd..b7e18cfcd439 100644
@@ -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
#6 lore raw

PATCH 6/8 dmaengine: stm32-dma: add copy_align constraint

In reply to Amelie Delaunay

This 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
index b7e18cfcd439..01a2374ae03a 100644
@@ -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
#7 lore raw

PATCH 7/8 dmaengine: stm32-dma: fix sleeping function called from invalid context

In reply to Amelie Delaunay

This 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
index 01a2374ae03a..b585e11c2168 100644
@@ -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
#8 lore raw

PATCH 8/8 dmaengine: stm32-dma: use vchan_terminate_vdesc() in .terminate_all

In reply to Amelie Delaunay

To 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
index b585e11c2168..0ddbaa4b4f0b 100644
@@ -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
maintainer Applied
#9 lore raw
In reply to Amelie Delaunay
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_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().

Applied, thanks

Signature · 1 line
~Vinod