diff mbox series

[v6,12/16] dmaengine: tegra-apb: Clean up suspend-resume

Message ID 20200130043804.32243-13-digetx@gmail.com (mailing list archive)
State Superseded
Headers show
Series NVIDIA Tegra APB DMA driver fixes and improvements | expand

Commit Message

Dmitry Osipenko Jan. 30, 2020, 4:38 a.m. UTC
It is enough to check whether hardware is busy on suspend and to reset
it across of suspend-resume because channel's configuration is fully
re-programmed on each DMA transaction anyways and because save-restore
of an active channel won't end up well without pausing transfer prior to
saving of the state (note that all channels shall be idling at the time of
suspend, so save-restore is not needed at all).

Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
---
 drivers/dma/tegra20-apb-dma.c | 133 ++++++++++++++++++----------------
 1 file changed, 69 insertions(+), 64 deletions(-)

Comments

Jon Hunter Jan. 30, 2020, 2:09 p.m. UTC | #1
On 30/01/2020 04:38, Dmitry Osipenko wrote:
> It is enough to check whether hardware is busy on suspend and to reset
> it across of suspend-resume because channel's configuration is fully
> re-programmed on each DMA transaction anyways and because save-restore
> of an active channel won't end up well without pausing transfer prior to
> saving of the state (note that all channels shall be idling at the time of
> suspend, so save-restore is not needed at all).

I guess if we ever wanted to support SNDRV_PCM_INFO_PAUSE for audio and
support the pause callback, then saving and restoring the channels could
be needed. Right now I believe that it will just terminate_all transfers
for audio on entering suspend. Any value in keeping this?

Jon
Dmitry Osipenko Jan. 30, 2020, 4:08 p.m. UTC | #2
30.01.2020 17:09, Jon Hunter пишет:
> 
> On 30/01/2020 04:38, Dmitry Osipenko wrote:
>> It is enough to check whether hardware is busy on suspend and to reset
>> it across of suspend-resume because channel's configuration is fully
>> re-programmed on each DMA transaction anyways and because save-restore
>> of an active channel won't end up well without pausing transfer prior to
>> saving of the state (note that all channels shall be idling at the time of
>> suspend, so save-restore is not needed at all).
> 
> I guess if we ever wanted to support SNDRV_PCM_INFO_PAUSE for audio and
> support the pause callback, then saving and restoring the channels could
> be needed. Right now I believe that it will just terminate_all transfers
> for audio on entering suspend. Any value in keeping this?

Indeed, looks like [1] pauses DMA during suspend if SNDRV_PCM_INFO_PAUSE
is supported.

[1]
https://elixir.bootlin.com/linux/v5.5/source/sound/core/pcm_dmaengine.c#L199

So we'll need to save-restore context only if DMA is in a paused state
during suspend, I'll adjust this patch to do that and will see if
enabling SNDRV_PCM_INFO_PAUSE works.
Dmitry Osipenko Jan. 30, 2020, 6:06 p.m. UTC | #3
30.01.2020 19:08, Dmitry Osipenko пишет:
> 30.01.2020 17:09, Jon Hunter пишет:
>>
>> On 30/01/2020 04:38, Dmitry Osipenko wrote:
>>> It is enough to check whether hardware is busy on suspend and to reset
>>> it across of suspend-resume because channel's configuration is fully
>>> re-programmed on each DMA transaction anyways and because save-restore
>>> of an active channel won't end up well without pausing transfer prior to
>>> saving of the state (note that all channels shall be idling at the time of
>>> suspend, so save-restore is not needed at all).
>>
>> I guess if we ever wanted to support SNDRV_PCM_INFO_PAUSE for audio and
>> support the pause callback, then saving and restoring the channels could
>> be needed. Right now I believe that it will just terminate_all transfers
>> for audio on entering suspend. Any value in keeping this?
> 
> Indeed, looks like [1] pauses DMA during suspend if SNDRV_PCM_INFO_PAUSE
> is supported.
> 
> [1]
> https://elixir.bootlin.com/linux/v5.5/source/sound/core/pcm_dmaengine.c#L199
> 
> So we'll need to save-restore context only if DMA is in a paused state
> during suspend, I'll adjust this patch to do that and will see if
> enabling SNDRV_PCM_INFO_PAUSE works.

I started to look at it and found that the .device_pause() hook isn't
implemented by the driver. So, it's fine to remove the context's
save-restore for now.

Jon, what about to keep this patch as-is? Later on I'll take a look at
implementing the proper pausing functionality and try to cleanup code a
bit further (remove the free list usage, etc).
Dmitry Osipenko Jan. 30, 2020, 6:26 p.m. UTC | #4
30.01.2020 21:06, Dmitry Osipenko пишет:
> 30.01.2020 19:08, Dmitry Osipenko пишет:
>> 30.01.2020 17:09, Jon Hunter пишет:
>>>
>>> On 30/01/2020 04:38, Dmitry Osipenko wrote:
>>>> It is enough to check whether hardware is busy on suspend and to reset
>>>> it across of suspend-resume because channel's configuration is fully
>>>> re-programmed on each DMA transaction anyways and because save-restore
>>>> of an active channel won't end up well without pausing transfer prior to
>>>> saving of the state (note that all channels shall be idling at the time of
>>>> suspend, so save-restore is not needed at all).
>>>
>>> I guess if we ever wanted to support SNDRV_PCM_INFO_PAUSE for audio and
>>> support the pause callback, then saving and restoring the channels could
>>> be needed. Right now I believe that it will just terminate_all transfers
>>> for audio on entering suspend. Any value in keeping this?
>>
>> Indeed, looks like [1] pauses DMA during suspend if SNDRV_PCM_INFO_PAUSE
>> is supported.
>>
>> [1]
>> https://elixir.bootlin.com/linux/v5.5/source/sound/core/pcm_dmaengine.c#L199
>>
>> So we'll need to save-restore context only if DMA is in a paused state
>> during suspend, I'll adjust this patch to do that and will see if
>> enabling SNDRV_PCM_INFO_PAUSE works.
> 
> I started to look at it and found that the .device_pause() hook isn't
> implemented by the driver. So, it's fine to remove the context's
> save-restore for now.
> 
> Jon, what about to keep this patch as-is? Later on I'll take a look at
> implementing the proper pausing functionality and try to cleanup code a
> bit further (remove the free list usage, etc).

Ah, only T114+ supports the per-channel pausing. So, I won't care about
implementing the device_pause(), let's leave it to somebody else :)
Jon Hunter Jan. 30, 2020, 6:58 p.m. UTC | #5
On 30/01/2020 18:26, Dmitry Osipenko wrote:
> 30.01.2020 21:06, Dmitry Osipenko пишет:
>> 30.01.2020 19:08, Dmitry Osipenko пишет:
>>> 30.01.2020 17:09, Jon Hunter пишет:
>>>>
>>>> On 30/01/2020 04:38, Dmitry Osipenko wrote:
>>>>> It is enough to check whether hardware is busy on suspend and to reset
>>>>> it across of suspend-resume because channel's configuration is fully
>>>>> re-programmed on each DMA transaction anyways and because save-restore
>>>>> of an active channel won't end up well without pausing transfer prior to
>>>>> saving of the state (note that all channels shall be idling at the time of
>>>>> suspend, so save-restore is not needed at all).
>>>>
>>>> I guess if we ever wanted to support SNDRV_PCM_INFO_PAUSE for audio and
>>>> support the pause callback, then saving and restoring the channels could
>>>> be needed. Right now I believe that it will just terminate_all transfers
>>>> for audio on entering suspend. Any value in keeping this?
>>>
>>> Indeed, looks like [1] pauses DMA during suspend if SNDRV_PCM_INFO_PAUSE
>>> is supported.
>>>
>>> [1]
>>> https://elixir.bootlin.com/linux/v5.5/source/sound/core/pcm_dmaengine.c#L199
>>>
>>> So we'll need to save-restore context only if DMA is in a paused state
>>> during suspend, I'll adjust this patch to do that and will see if
>>> enabling SNDRV_PCM_INFO_PAUSE works.
>>
>> I started to look at it and found that the .device_pause() hook isn't
>> implemented by the driver. So, it's fine to remove the context's
>> save-restore for now.
>>
>> Jon, what about to keep this patch as-is? Later on I'll take a look at
>> implementing the proper pausing functionality and try to cleanup code a
>> bit further (remove the free list usage, etc).
> 
> Ah, only T114+ supports the per-channel pausing. So, I won't care about
> implementing the device_pause(), let's leave it to somebody else :)

That's fine. We have lived with out it for this long.

Jon
Jon Hunter Jan. 30, 2020, 7 p.m. UTC | #6
On 30/01/2020 04:38, Dmitry Osipenko wrote:
> It is enough to check whether hardware is busy on suspend and to reset
> it across of suspend-resume because channel's configuration is fully
> re-programmed on each DMA transaction anyways and because save-restore
> of an active channel won't end up well without pausing transfer prior to
> saving of the state (note that all channels shall be idling at the time of
> suspend, so save-restore is not needed at all).

Maybe just update the commit message to state that the pause callback is
not implemented and channel pause is not supported prior to T114. And
then ...

Acked-by: Jon Hunter <jonathanh@nvidia.com>

Cheers
Jon
Dmitry Osipenko Jan. 30, 2020, 8:06 p.m. UTC | #7
30.01.2020 22:00, Jon Hunter пишет:
> 
> On 30/01/2020 04:38, Dmitry Osipenko wrote:
>> It is enough to check whether hardware is busy on suspend and to reset
>> it across of suspend-resume because channel's configuration is fully
>> re-programmed on each DMA transaction anyways and because save-restore
>> of an active channel won't end up well without pausing transfer prior to
>> saving of the state (note that all channels shall be idling at the time of
>> suspend, so save-restore is not needed at all).
> 
> Maybe just update the commit message to state that the pause callback is
> not implemented and channel pause is not supported prior to T114. And
> then ...
> 
> Acked-by: Jon Hunter <jonathanh@nvidia.com>

Thanks, I'll update the message.
diff mbox series

Patch

diff --git a/drivers/dma/tegra20-apb-dma.c b/drivers/dma/tegra20-apb-dma.c
index 0ee28d8e3c96..4a371891b7c4 100644
--- a/drivers/dma/tegra20-apb-dma.c
+++ b/drivers/dma/tegra20-apb-dma.c
@@ -1401,6 +1401,36 @@  static const struct tegra_dma_chip_data tegra148_dma_chip_data = {
 	.support_separate_wcount_reg = true,
 };
 
+static int tegra_dma_init_hw(struct tegra_dma *tdma)
+{
+	int err;
+
+	err = reset_control_assert(tdma->rst);
+	if (err) {
+		dev_err(tdma->dev, "failed to assert reset: %d\n", err);
+		return err;
+	}
+
+	err = clk_enable(tdma->dma_clk);
+	if (err) {
+		dev_err(tdma->dev, "failed to enable clk: %d\n", err);
+		return err;
+	}
+
+	/* reset DMA controller */
+	udelay(2);
+	reset_control_deassert(tdma->rst);
+
+	/* enable global DMA registers */
+	tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
+	tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
+	tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFF);
+
+	clk_disable(tdma->dma_clk);
+
+	return 0;
+}
+
 static int tegra_dma_probe(struct platform_device *pdev)
 {
 	const struct tegra_dma_chip_data *cdata;
@@ -1442,25 +1472,13 @@  static int tegra_dma_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	ret = tegra_dma_init_hw(tdma);
+	if (ret)
+		goto err_clk_unprepare;
+
 	pm_runtime_irq_safe(&pdev->dev);
 	pm_runtime_enable(&pdev->dev);
 
-	ret = pm_runtime_get_sync(&pdev->dev);
-	if (ret < 0)
-		goto err_pm_disable;
-
-	/* Reset DMA controller */
-	reset_control_assert(tdma->rst);
-	udelay(2);
-	reset_control_deassert(tdma->rst);
-
-	/* Enable global DMA registers */
-	tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
-	tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
-	tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
-
-	pm_runtime_put(&pdev->dev);
-
 	INIT_LIST_HEAD(&tdma->dma_dev.channels);
 	for (i = 0; i < cdata->nr_channels; i++) {
 		struct tegra_dma_channel *tdc = &tdma->channels[i];
@@ -1558,6 +1576,8 @@  static int tegra_dma_probe(struct platform_device *pdev)
 
 err_pm_disable:
 	pm_runtime_disable(&pdev->dev);
+
+err_clk_unprepare:
 	clk_unprepare(tdma->dma_clk);
 
 	return ret;
@@ -1577,26 +1597,6 @@  static int tegra_dma_remove(struct platform_device *pdev)
 static int tegra_dma_runtime_suspend(struct device *dev)
 {
 	struct tegra_dma *tdma = dev_get_drvdata(dev);
-	unsigned int i;
-
-	tdma->reg_gen = tdma_read(tdma, TEGRA_APBDMA_GENERAL);
-	for (i = 0; i < tdma->chip_data->nr_channels; i++) {
-		struct tegra_dma_channel *tdc = &tdma->channels[i];
-		struct tegra_dma_channel_regs *ch_reg = &tdc->channel_reg;
-
-		/* Only save the state of DMA channels that are in use */
-		if (!tdc->config_init)
-			continue;
-
-		ch_reg->csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
-		ch_reg->ahb_ptr = tdc_read(tdc, TEGRA_APBDMA_CHAN_AHBPTR);
-		ch_reg->apb_ptr = tdc_read(tdc, TEGRA_APBDMA_CHAN_APBPTR);
-		ch_reg->ahb_seq = tdc_read(tdc, TEGRA_APBDMA_CHAN_AHBSEQ);
-		ch_reg->apb_seq = tdc_read(tdc, TEGRA_APBDMA_CHAN_APBSEQ);
-		if (tdma->chip_data->support_separate_wcount_reg)
-			ch_reg->wcount = tdc_read(tdc,
-						  TEGRA_APBDMA_CHAN_WCOUNT);
-	}
 
 	clk_disable(tdma->dma_clk);
 
@@ -1606,46 +1606,51 @@  static int tegra_dma_runtime_suspend(struct device *dev)
 static int tegra_dma_runtime_resume(struct device *dev)
 {
 	struct tegra_dma *tdma = dev_get_drvdata(dev);
-	unsigned int i;
-	int ret;
 
-	ret = clk_enable(tdma->dma_clk);
-	if (ret < 0) {
-		dev_err(dev, "clk_enable failed: %d\n", ret);
-		return ret;
-	}
+	return clk_enable(tdma->dma_clk);
+}
 
-	tdma_write(tdma, TEGRA_APBDMA_GENERAL, tdma->reg_gen);
-	tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
-	tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
+static int __maybe_unused tegra_dma_dev_suspend(struct device *dev)
+{
+	struct tegra_dma *tdma = dev_get_drvdata(dev);
+	unsigned long flags;
+	unsigned int i;
+	bool busy;
 
 	for (i = 0; i < tdma->chip_data->nr_channels; i++) {
 		struct tegra_dma_channel *tdc = &tdma->channels[i];
-		struct tegra_dma_channel_regs *ch_reg = &tdc->channel_reg;
-
-		/* Only restore the state of DMA channels that are in use */
-		if (!tdc->config_init)
-			continue;
-
-		if (tdma->chip_data->support_separate_wcount_reg)
-			tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
-				  ch_reg->wcount);
-		tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_reg->apb_seq);
-		tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_reg->apb_ptr);
-		tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_reg->ahb_seq);
-		tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_reg->ahb_ptr);
-		tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
-			  ch_reg->csr & ~TEGRA_APBDMA_CSR_ENB);
+
+		tasklet_kill(&tdc->tasklet);
+
+		spin_lock_irqsave(&tdc->lock, flags);
+		busy = tdc->busy;
+		spin_unlock_irqrestore(&tdc->lock, flags);
+
+		if (busy) {
+			dev_err(tdma->dev, "channel %u busy\n", i);
+			return -EBUSY;
+		}
 	}
 
-	return 0;
+	return pm_runtime_force_suspend(dev);
+}
+
+static int __maybe_unused tegra_dma_dev_resume(struct device *dev)
+{
+	struct tegra_dma *tdma = dev_get_drvdata(dev);
+	int err;
+
+	err = tegra_dma_init_hw(tdma);
+	if (err)
+		return err;
+
+	return pm_runtime_force_resume(dev);
 }
 
 static const struct dev_pm_ops tegra_dma_dev_pm_ops = {
 	SET_RUNTIME_PM_OPS(tegra_dma_runtime_suspend, tegra_dma_runtime_resume,
 			   NULL)
-	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
-				pm_runtime_force_resume)
+	SET_SYSTEM_SLEEP_PM_OPS(tegra_dma_dev_suspend, tegra_dma_dev_resume)
 };
 
 static const struct of_device_id tegra_dma_of_match[] = {