linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: add runtime PM for transfer_one_message
@ 2020-11-02 11:22 Chunyan Zhang
  2020-11-02 18:16 ` Mark Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Chunyan Zhang @ 2020-11-02 11:22 UTC (permalink / raw)
  To: Mark Brown
  Cc: linux-spi, linux-kernel, Orson Zhai, Baolin Wang, Chunyan Zhang,
	Chunyan Zhang, Bangzheng Liu

From: Chunyan Zhang <chunyan.zhang@unisoc.com>

Before transfer message, spi devices probably have been in runtime suspended,
that would cause the kernel crash on some platforms once access spi
registers, such as on Unisoc's SoCs. The spi devices can be suspended
until message transfer completed.

Also this patch move the API spi_idle_runtime_pm() above to
spi_transfer_one_message() which need to call that API.

Fixes: b158935f70b9 ("spi: Provide common spi_message processing loop")
Reported-by: Bangzheng Liu <bangzheng.liu@unisoc.com>
Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>
---
 drivers/spi/spi.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 0cab239d8e7f..63f7ebea7076 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1224,6 +1224,14 @@ static void _spi_transfer_cs_change_delay(struct spi_message *msg,
 	}
 }
 
+static void spi_idle_runtime_pm(struct spi_controller *ctlr)
+{
+	if (ctlr->auto_runtime_pm) {
+		pm_runtime_mark_last_busy(ctlr->dev.parent);
+		pm_runtime_put_autosuspend(ctlr->dev.parent);
+	}
+}
+
 /*
  * spi_transfer_one_message - Default implementation of transfer_one_message()
  *
@@ -1240,6 +1248,16 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
 	struct spi_statistics *statm = &ctlr->statistics;
 	struct spi_statistics *stats = &msg->spi->statistics;
 
+	if (ctlr->auto_runtime_pm) {
+		ret = pm_runtime_get_sync(ctlr->dev.parent);
+		if (ret < 0) {
+			pm_runtime_put_noidle(ctlr->dev.parent);
+			dev_err(&ctlr->dev, "Failed to power device: %d\n",
+				ret);
+			return ret;
+		}
+	}
+
 	spi_set_cs(msg->spi, true);
 
 	SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
@@ -1329,6 +1347,8 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
 
 	spi_finalize_current_message(ctlr);
 
+	spi_idle_runtime_pm(ctlr);
+
 	return ret;
 }
 
@@ -1346,14 +1366,6 @@ void spi_finalize_current_transfer(struct spi_controller *ctlr)
 }
 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
 
-static void spi_idle_runtime_pm(struct spi_controller *ctlr)
-{
-	if (ctlr->auto_runtime_pm) {
-		pm_runtime_mark_last_busy(ctlr->dev.parent);
-		pm_runtime_put_autosuspend(ctlr->dev.parent);
-	}
-}
-
 /**
  * __spi_pump_messages - function which processes spi message queue
  * @ctlr: controller to process queue for
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] spi: add runtime PM for transfer_one_message
  2020-11-02 11:22 [PATCH] spi: add runtime PM for transfer_one_message Chunyan Zhang
@ 2020-11-02 18:16 ` Mark Brown
  2020-11-03  7:30   ` Chunyan Zhang
  0 siblings, 1 reply; 4+ messages in thread
From: Mark Brown @ 2020-11-02 18:16 UTC (permalink / raw)
  To: Chunyan Zhang
  Cc: linux-spi, linux-kernel, Orson Zhai, Baolin Wang, Chunyan Zhang,
	Bangzheng Liu

[-- Attachment #1: Type: text/plain, Size: 1205 bytes --]

On Mon, Nov 02, 2020 at 07:22:39PM +0800, Chunyan Zhang wrote:
> From: Chunyan Zhang <chunyan.zhang@unisoc.com>

> Before transfer message, spi devices probably have been in runtime suspended,
> that would cause the kernel crash on some platforms once access spi
> registers, such as on Unisoc's SoCs. The spi devices can be suspended
> until message transfer completed.

This commit message is a bit hard to follow so I don't really understand
what the issue is.  We only ever call transfer_one_message() from within
__spi_pump_messages() which already handles auto_runtime_pm so I'm not
seeing the situation where we might get to transfer_one_message()
without having already runtime resumed the controller.  What exactly is
the error situation here?  This code has been around for a while and I'm
not aware of reports of issues here and I can't see anything unusual
that the Spreadtrum driver is doing.

Also why are we doing this in transfer_one_message() where it will only
work for controllers using that?  If we're missing runtime PM in some
paths then presumably controllers with a custom implementation are also
going to be affected as well, auto_runtime_pm is supposed to work for
them as well.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] spi: add runtime PM for transfer_one_message
  2020-11-02 18:16 ` Mark Brown
@ 2020-11-03  7:30   ` Chunyan Zhang
  2020-11-03 13:18     ` Mark Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Chunyan Zhang @ 2020-11-03  7:30 UTC (permalink / raw)
  To: Mark Brown
  Cc: linux-spi, Linux Kernel Mailing List, Orson Zhai, Baolin Wang,
	Chunyan Zhang, Bangzheng Liu

On Tue, 3 Nov 2020 at 02:17, Mark Brown <broonie@kernel.org> wrote:
>
> On Mon, Nov 02, 2020 at 07:22:39PM +0800, Chunyan Zhang wrote:
> > From: Chunyan Zhang <chunyan.zhang@unisoc.com>
>
> > Before transfer message, spi devices probably have been in runtime suspended,
> > that would cause the kernel crash on some platforms once access spi
> > registers, such as on Unisoc's SoCs. The spi devices can be suspended
> > until message transfer completed.
>
> This commit message is a bit hard to follow so I don't really understand
> what the issue is.  We only ever call transfer_one_message() from within
> __spi_pump_messages() which already handles auto_runtime_pm so I'm not
> seeing the situation where we might get to transfer_one_message()
> without having already runtime resumed the controller.  What exactly is
> the error situation here?  This code has been around for a while and I'm
> not aware of reports of issues here and I can't see anything unusual
> that the Spreadtrum driver is doing.

With further tests and looking into this part of code, the problem we
found recently on our platform which runs kernel 4.14 can be fixed by
this commit [1].
In a word,  there's no issue here indeed, this patch should be
ignored, sorry for the noise and thanks for your review.

Chunyan

[1] https://lkml.org/lkml/2019/10/30/173

>
> Also why are we doing this in transfer_one_message() where it will only
> work for controllers using that?  If we're missing runtime PM in some
> paths then presumably controllers with a custom implementation are also
> going to be affected as well, auto_runtime_pm is supposed to work for
> them as well.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] spi: add runtime PM for transfer_one_message
  2020-11-03  7:30   ` Chunyan Zhang
@ 2020-11-03 13:18     ` Mark Brown
  0 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2020-11-03 13:18 UTC (permalink / raw)
  To: Chunyan Zhang
  Cc: linux-spi, Linux Kernel Mailing List, Orson Zhai, Baolin Wang,
	Chunyan Zhang, Bangzheng Liu

[-- Attachment #1: Type: text/plain, Size: 644 bytes --]

On Tue, Nov 03, 2020 at 03:30:24PM +0800, Chunyan Zhang wrote:
> On Tue, 3 Nov 2020 at 02:17, Mark Brown <broonie@kernel.org> wrote:

> > the error situation here?  This code has been around for a while and I'm
> > not aware of reports of issues here and I can't see anything unusual
> > that the Spreadtrum driver is doing.

> With further tests and looking into this part of code, the problem we
> found recently on our platform which runs kernel 4.14 can be fixed by
> this commit [1].
> In a word,  there's no issue here indeed, this patch should be
> ignored, sorry for the noise and thanks for your review.

Ah, great - glad it's sorted.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-03 13:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02 11:22 [PATCH] spi: add runtime PM for transfer_one_message Chunyan Zhang
2020-11-02 18:16 ` Mark Brown
2020-11-03  7:30   ` Chunyan Zhang
2020-11-03 13:18     ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).