diff mbox

[v5,3/4] amba: Don't unprepare the clocks if device driver wants IRQ safe runtime PM

Message ID 1410872360-27029-4-git-send-email-k.kozlowski@samsung.com (mailing list archive)
State Superseded
Headers show

Commit Message

Krzysztof Kozlowski Sept. 16, 2014, 12:59 p.m. UTC
The AMBA bus driver defines runtime Power Management functions which
disable and unprepare AMBA bus clock. This is problematic for runtime PM
because unpreparing a clock might sleep so it is not interrupt safe.

However some drivers may want to implement runtime PM functions in
interrupt-safe way (see pm_runtime_irq_safe()). In such case the AMBA
bus driver should only disable/enable the clock in runtime suspend and
resume callbacks.

Detect the device driver behavior after calling its probe function and
store it. During runtime suspend/resume deal with clocks according to
stored value.

Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
---
 drivers/amba/bus.c       | 29 +++++++++++++++++++++++++----
 include/linux/amba/bus.h |  1 +
 2 files changed, 26 insertions(+), 4 deletions(-)

Comments

Ulf Hansson Sept. 16, 2014, 6:25 p.m. UTC | #1
On 16 September 2014 14:59, Krzysztof Kozlowski <k.kozlowski@samsung.com> wrote:
> The AMBA bus driver defines runtime Power Management functions which
> disable and unprepare AMBA bus clock. This is problematic for runtime PM
> because unpreparing a clock might sleep so it is not interrupt safe.
>
> However some drivers may want to implement runtime PM functions in
> interrupt-safe way (see pm_runtime_irq_safe()). In such case the AMBA
> bus driver should only disable/enable the clock in runtime suspend and
> resume callbacks.
>
> Detect the device driver behavior after calling its probe function and
> store it. During runtime suspend/resume deal with clocks according to
> stored value.
>
> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> ---
>  drivers/amba/bus.c       | 29 +++++++++++++++++++++++++----
>  include/linux/amba/bus.h |  1 +
>  2 files changed, 26 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
> index 3cf61a127ee5..e8fd5706954f 100644
> --- a/drivers/amba/bus.c
> +++ b/drivers/amba/bus.c
> @@ -94,8 +94,18 @@ static int amba_pm_runtime_suspend(struct device *dev)
>         struct amba_device *pcdev = to_amba_device(dev);
>         int ret = pm_generic_runtime_suspend(dev);
>
> -       if (ret == 0 && dev->driver)
> -               clk_disable_unprepare(pcdev->pclk);
> +       if (ret == 0 && dev->driver) {
> +               /*
> +                * Drivers should not change pm_runtime_irq_safe()
> +                * after probe.
> +                */
> +               WARN_ON(pcdev->irq_safe != pm_runtime_is_irq_safe(dev));

Do we really need a WARN_ON here. Driver shouldn't update their
irq_safe value dynamically, right!?

> +
> +               if (pcdev->irq_safe)
> +                       clk_disable(pcdev->pclk);

Since the irq_safe flag, could be considered as a special case, an
option for these cases - could be to leave the clock to be entirely
handled from the driver's runtime PM callback instead.

I wonder if that could simplify both for the driver and for the amba bus?

Russell, what do you think? Is it a bad idea?

> +               else
> +                       clk_disable_unprepare(pcdev->pclk);
> +       }
>
>         return ret;
>  }
> @@ -106,7 +116,16 @@ static int amba_pm_runtime_resume(struct device *dev)
>         int ret;
>
>         if (dev->driver) {
> -               ret = clk_prepare_enable(pcdev->pclk);
> +               /*
> +                * Drivers should not change pm_runtime_irq_safe()
> +                * after probe.
> +                */
> +               WARN_ON(pcdev->irq_safe != pm_runtime_is_irq_safe(dev));
> +
> +               if (pcdev->irq_safe)
> +                       ret = clk_enable(pcdev->pclk);
> +               else
> +                       ret = clk_prepare_enable(pcdev->pclk);
>                 /* Failure is probably fatal to the system, but... */
>                 if (ret)
>                         return ret;
> @@ -191,8 +210,10 @@ static int amba_probe(struct device *dev)
>                 pm_runtime_enable(dev);
>
>                 ret = pcdrv->probe(pcdev, id);
> -               if (ret == 0)
> +               if (ret == 0) {
> +                       pcdev->irq_safe = pm_runtime_is_irq_safe(dev);

I suggest to remove the local copy of this flag and to use
pm_runtime_is_irq_safe(dev) directly instead.

>                         break;
> +               }
>
>                 pm_runtime_disable(dev);
>                 pm_runtime_set_suspended(dev);
> diff --git a/include/linux/amba/bus.h b/include/linux/amba/bus.h
> index ad52027a9cbf..ce101e4497d6 100644
> --- a/include/linux/amba/bus.h
> +++ b/include/linux/amba/bus.h
> @@ -32,6 +32,7 @@ struct amba_device {
>         struct clk              *pclk;
>         unsigned int            periphid;
>         unsigned int            irq[AMBA_NR_IRQS];
> +       unsigned int            irq_safe:1;
>  };
>
>  struct amba_driver {
> --
> 1.9.1
>

Kind regards
Uffe
--
To unsubscribe from this list: send the line "unsubscribe dmaengine" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Russell King - ARM Linux Sept. 16, 2014, 7:52 p.m. UTC | #2
On Tue, Sep 16, 2014 at 08:25:25PM +0200, Ulf Hansson wrote:
> On 16 September 2014 14:59, Krzysztof Kozlowski <k.kozlowski@samsung.com> wrote:
> > The AMBA bus driver defines runtime Power Management functions which
> > disable and unprepare AMBA bus clock. This is problematic for runtime PM
> > because unpreparing a clock might sleep so it is not interrupt safe.
> >
> > However some drivers may want to implement runtime PM functions in
> > interrupt-safe way (see pm_runtime_irq_safe()). In such case the AMBA
> > bus driver should only disable/enable the clock in runtime suspend and
> > resume callbacks.
> >
> > Detect the device driver behavior after calling its probe function and
> > store it. During runtime suspend/resume deal with clocks according to
> > stored value.
> >
> > Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> > ---
> >  drivers/amba/bus.c       | 29 +++++++++++++++++++++++++----
> >  include/linux/amba/bus.h |  1 +
> >  2 files changed, 26 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
> > index 3cf61a127ee5..e8fd5706954f 100644
> > --- a/drivers/amba/bus.c
> > +++ b/drivers/amba/bus.c
> > @@ -94,8 +94,18 @@ static int amba_pm_runtime_suspend(struct device *dev)
> >         struct amba_device *pcdev = to_amba_device(dev);
> >         int ret = pm_generic_runtime_suspend(dev);
> >
> > -       if (ret == 0 && dev->driver)
> > -               clk_disable_unprepare(pcdev->pclk);
> > +       if (ret == 0 && dev->driver) {
> > +               /*
> > +                * Drivers should not change pm_runtime_irq_safe()
> > +                * after probe.
> > +                */
> > +               WARN_ON(pcdev->irq_safe != pm_runtime_is_irq_safe(dev));
> 
> Do we really need a WARN_ON here. Driver shouldn't update their
> irq_safe value dynamically, right!?

The driver shouldn't update it dynamically, and this makes sure *that*
is enforced since we end up depending on that property.  Hence the
check is sensible (and I even suggested it.)

> > +
> > +               if (pcdev->irq_safe)
> > +                       clk_disable(pcdev->pclk);
> 
> Since the irq_safe flag, could be considered as a special case, an
> option for these cases - could be to leave the clock to be entirely
> handled from the driver's runtime PM callback instead.

Too many sub-clauses to make much sense of that statement.

I don't want drivers messing around with this stuff.  This is the /bus/
clock, not a device specific clock.
Ulf Hansson Sept. 17, 2014, 5:59 p.m. UTC | #3
On 16 September 2014 21:52, Russell King - ARM Linux
<linux@arm.linux.org.uk> wrote:
> On Tue, Sep 16, 2014 at 08:25:25PM +0200, Ulf Hansson wrote:
>> On 16 September 2014 14:59, Krzysztof Kozlowski <k.kozlowski@samsung.com> wrote:
>> > The AMBA bus driver defines runtime Power Management functions which
>> > disable and unprepare AMBA bus clock. This is problematic for runtime PM
>> > because unpreparing a clock might sleep so it is not interrupt safe.
>> >
>> > However some drivers may want to implement runtime PM functions in
>> > interrupt-safe way (see pm_runtime_irq_safe()). In such case the AMBA
>> > bus driver should only disable/enable the clock in runtime suspend and
>> > resume callbacks.
>> >
>> > Detect the device driver behavior after calling its probe function and
>> > store it. During runtime suspend/resume deal with clocks according to
>> > stored value.
>> >
>> > Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
>> > ---
>> >  drivers/amba/bus.c       | 29 +++++++++++++++++++++++++----
>> >  include/linux/amba/bus.h |  1 +
>> >  2 files changed, 26 insertions(+), 4 deletions(-)
>> >
>> > diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
>> > index 3cf61a127ee5..e8fd5706954f 100644
>> > --- a/drivers/amba/bus.c
>> > +++ b/drivers/amba/bus.c
>> > @@ -94,8 +94,18 @@ static int amba_pm_runtime_suspend(struct device *dev)
>> >         struct amba_device *pcdev = to_amba_device(dev);
>> >         int ret = pm_generic_runtime_suspend(dev);
>> >
>> > -       if (ret == 0 && dev->driver)
>> > -               clk_disable_unprepare(pcdev->pclk);
>> > +       if (ret == 0 && dev->driver) {
>> > +               /*
>> > +                * Drivers should not change pm_runtime_irq_safe()
>> > +                * after probe.
>> > +                */
>> > +               WARN_ON(pcdev->irq_safe != pm_runtime_is_irq_safe(dev));
>>
>> Do we really need a WARN_ON here. Driver shouldn't update their
>> irq_safe value dynamically, right!?
>
> The driver shouldn't update it dynamically, and this makes sure *that*
> is enforced since we end up depending on that property.  Hence the
> check is sensible (and I even suggested it.)

The WARN_ON could be nice to have, but I think this is a task for PM
core to handle.

Copying flags shouldn't be needed for each an every instance of a
driver/bus that manage irq_safe devices. That's my main point.

>
>> > +
>> > +               if (pcdev->irq_safe)
>> > +                       clk_disable(pcdev->pclk);
>>
>> Since the irq_safe flag, could be considered as a special case, an
>> option for these cases - could be to leave the clock to be entirely
>> handled from the driver's runtime PM callback instead.
>
> Too many sub-clauses to make much sense of that statement.

Sorry, agree. :-)

>
> I don't want drivers messing around with this stuff.  This is the /bus/
> clock, not a device specific clock.

For irq_safe devices, driver's will need to handle the
clk_prepare|unprepare during system PM anyway. That's the reason to
why I suggested this.

On the other hand I agree with you, it's a bus clock...

Kind regards
Uffe
--
To unsubscribe from this list: send the line "unsubscribe dmaengine" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
index 3cf61a127ee5..e8fd5706954f 100644
--- a/drivers/amba/bus.c
+++ b/drivers/amba/bus.c
@@ -94,8 +94,18 @@  static int amba_pm_runtime_suspend(struct device *dev)
 	struct amba_device *pcdev = to_amba_device(dev);
 	int ret = pm_generic_runtime_suspend(dev);
 
-	if (ret == 0 && dev->driver)
-		clk_disable_unprepare(pcdev->pclk);
+	if (ret == 0 && dev->driver) {
+		/*
+		 * Drivers should not change pm_runtime_irq_safe()
+		 * after probe.
+		 */
+		WARN_ON(pcdev->irq_safe != pm_runtime_is_irq_safe(dev));
+
+		if (pcdev->irq_safe)
+			clk_disable(pcdev->pclk);
+		else
+			clk_disable_unprepare(pcdev->pclk);
+	}
 
 	return ret;
 }
@@ -106,7 +116,16 @@  static int amba_pm_runtime_resume(struct device *dev)
 	int ret;
 
 	if (dev->driver) {
-		ret = clk_prepare_enable(pcdev->pclk);
+		/*
+		 * Drivers should not change pm_runtime_irq_safe()
+		 * after probe.
+		 */
+		WARN_ON(pcdev->irq_safe != pm_runtime_is_irq_safe(dev));
+
+		if (pcdev->irq_safe)
+			ret = clk_enable(pcdev->pclk);
+		else
+			ret = clk_prepare_enable(pcdev->pclk);
 		/* Failure is probably fatal to the system, but... */
 		if (ret)
 			return ret;
@@ -191,8 +210,10 @@  static int amba_probe(struct device *dev)
 		pm_runtime_enable(dev);
 
 		ret = pcdrv->probe(pcdev, id);
-		if (ret == 0)
+		if (ret == 0) {
+			pcdev->irq_safe = pm_runtime_is_irq_safe(dev);
 			break;
+		}
 
 		pm_runtime_disable(dev);
 		pm_runtime_set_suspended(dev);
diff --git a/include/linux/amba/bus.h b/include/linux/amba/bus.h
index ad52027a9cbf..ce101e4497d6 100644
--- a/include/linux/amba/bus.h
+++ b/include/linux/amba/bus.h
@@ -32,6 +32,7 @@  struct amba_device {
 	struct clk		*pclk;
 	unsigned int		periphid;
 	unsigned int		irq[AMBA_NR_IRQS];
+	unsigned int		irq_safe:1;
 };
 
 struct amba_driver {