diff mbox series

watchdog: da9063: wake up parent ahead of reboot

Message ID 20200901161756.28100-1-uli+renesas@fpond.eu (mailing list archive)
State Under Review
Delegated to: Geert Uytterhoeven
Headers show
Series watchdog: da9063: wake up parent ahead of reboot | expand

Commit Message

Ulrich Hecht Sept. 1, 2020, 4:17 p.m. UTC
This patch ensures our parent is awake before a reboot takes place. This
prevents situations in which the I2C host has been suspended and cannot
be safely woken up anymore when it needs to talk to us.

Signed-off-by: Ulrich Hecht <uli+renesas@fpond.eu>
---

Hi!

This is supposed to resolve the issue that came up in the review of
"[PATCH v2] i2c: sh_mobile: implement atomic transfers" that the parent
controller may be suspended when the restart method is called. See
https://www.spinics.net/lists/linux-i2c/msg46367.html for details.

CU
Uli


 drivers/watchdog/da9063_wdt.c   | 21 +++++++++++++++++++++
 include/linux/mfd/da9063/core.h |  2 ++
 2 files changed, 23 insertions(+)

Comments

Guenter Roeck Sept. 1, 2020, 4:48 p.m. UTC | #1
On 9/1/20 9:17 AM, Ulrich Hecht wrote:
> This patch ensures our parent is awake before a reboot takes place. This
> prevents situations in which the I2C host has been suspended and cannot
> be safely woken up anymore when it needs to talk to us.
> 

Why not call pm_runtime_get_sync() in da9063_wdt_restart() ? What
guarantees that the local notifier is called before the watchdog core's
restart notifier ? And what is the point of using the watchdog core's
reboot handler if it is bypassed anyway ?

Also, why is it not necessary to call pm functions when the watchdog is
started, when it is stopped, and during suspend/resume ?

Thanks,
Guenter

> Signed-off-by: Ulrich Hecht <uli+renesas@fpond.eu>
> ---
> 
> Hi!
> 
> This is supposed to resolve the issue that came up in the review of
> "[PATCH v2] i2c: sh_mobile: implement atomic transfers" that the parent
> controller may be suspended when the restart method is called. See
> https://www.spinics.net/lists/linux-i2c/msg46367.html for details.
> 
> CU
> Uli
> 
> 
>  drivers/watchdog/da9063_wdt.c   | 21 +++++++++++++++++++++
>  include/linux/mfd/da9063/core.h |  2 ++
>  2 files changed, 23 insertions(+)
> 
> diff --git a/drivers/watchdog/da9063_wdt.c b/drivers/watchdog/da9063_wdt.c
> index 423584252606..89718733491e 100644
> --- a/drivers/watchdog/da9063_wdt.c
> +++ b/drivers/watchdog/da9063_wdt.c
> @@ -18,6 +18,8 @@
>  #include <linux/mfd/da9063/registers.h>
>  #include <linux/mfd/da9063/core.h>
>  #include <linux/regmap.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/reboot.h>
>  
>  /*
>   * Watchdog selector to timeout in seconds.
> @@ -158,6 +160,21 @@ static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
>  	return ret;
>  }
>  
> +static int da9063_reboot_notifier(struct notifier_block *nb,
> +				  unsigned long code, void *unused)
> +{
> +	struct da9063 *da9063 = container_of(nb, struct da9063, reboot_nb);
> +
> +	/*
> +	 * Make sure parent device is running. This cannot be done in the
> +	 * restart handler because it is no longer safe to do runtime PM
> +	 * there.
> +	 */
> +	pm_runtime_get_sync(da9063->dev->parent);
> +
> +	return NOTIFY_DONE;
> +}
> +
>  static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
>  			      void *data)
>  {
> @@ -233,6 +250,10 @@ static int da9063_wdt_probe(struct platform_device *pdev)
>  		set_bit(WDOG_HW_RUNNING, &wdd->status);
>  	}
>  
> +	/* Get early notification of reboot so we can wake up the parent. */
> +	da9063->reboot_nb.notifier_call = da9063_reboot_notifier;
> +	devm_register_reboot_notifier(dev, &da9063->reboot_nb);
> +
>  	return devm_watchdog_register_device(dev, wdd);
>  }
>  
> diff --git a/include/linux/mfd/da9063/core.h b/include/linux/mfd/da9063/core.h
> index fa7a43f02f27..9a3283d488b7 100644
> --- a/include/linux/mfd/da9063/core.h
> +++ b/include/linux/mfd/da9063/core.h
> @@ -85,6 +85,8 @@ struct da9063 {
>  	int		chip_irq;
>  	unsigned int	irq_base;
>  	struct regmap_irq_chip_data *regmap_irq;
> +
> +	struct notifier_block reboot_nb;
>  };
>  
>  int da9063_device_init(struct da9063 *da9063, unsigned int irq);
>
Ulrich Hecht Sept. 7, 2020, 9:19 p.m. UTC | #2
> On 09/01/2020 6:48 PM Guenter Roeck <linux@roeck-us.net> wrote:
> 
>  
> On 9/1/20 9:17 AM, Ulrich Hecht wrote:
> > This patch ensures our parent is awake before a reboot takes place. This
> > prevents situations in which the I2C host has been suspended and cannot
> > be safely woken up anymore when it needs to talk to us.
> > 
> 
> Why not call pm_runtime_get_sync() in da9063_wdt_restart() ?

It's no longer safe to do so at that point because the restart handler runs in atomic context. The unpleasant details can be found at https://www.spinics.net/lists/linux-i2c/msg46367.html

> What
> guarantees that the local notifier is called before the watchdog core's
> restart notifier ?

It would seem to me that a reboot notifier that triggers after the restart notifier (which, if successfully handled, does not return) would be rather pointless.

> And what is the point of using the watchdog core's
> reboot handler if it is bypassed anyway ?

I don't think it's bypassed, it should be possible to register several reboot notifiers per device.

It would also be possible to do this stuff in the core's reboot handler, but I have chosen not to do so because the core seems to avoid dealing with PM, which I assume is a design decision. Also, it would affect all other drivers with a restart handler, with (to me) unforeseeable effects. What do you think?

> Also, why is it not necessary to call pm functions when the watchdog is
> started, when it is stopped, and during suspend/resume ?

These cases are handled automatically, AFAIK.

CU
Uli
diff mbox series

Patch

diff --git a/drivers/watchdog/da9063_wdt.c b/drivers/watchdog/da9063_wdt.c
index 423584252606..89718733491e 100644
--- a/drivers/watchdog/da9063_wdt.c
+++ b/drivers/watchdog/da9063_wdt.c
@@ -18,6 +18,8 @@ 
 #include <linux/mfd/da9063/registers.h>
 #include <linux/mfd/da9063/core.h>
 #include <linux/regmap.h>
+#include <linux/pm_runtime.h>
+#include <linux/reboot.h>
 
 /*
  * Watchdog selector to timeout in seconds.
@@ -158,6 +160,21 @@  static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
 	return ret;
 }
 
+static int da9063_reboot_notifier(struct notifier_block *nb,
+				  unsigned long code, void *unused)
+{
+	struct da9063 *da9063 = container_of(nb, struct da9063, reboot_nb);
+
+	/*
+	 * Make sure parent device is running. This cannot be done in the
+	 * restart handler because it is no longer safe to do runtime PM
+	 * there.
+	 */
+	pm_runtime_get_sync(da9063->dev->parent);
+
+	return NOTIFY_DONE;
+}
+
 static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
 			      void *data)
 {
@@ -233,6 +250,10 @@  static int da9063_wdt_probe(struct platform_device *pdev)
 		set_bit(WDOG_HW_RUNNING, &wdd->status);
 	}
 
+	/* Get early notification of reboot so we can wake up the parent. */
+	da9063->reboot_nb.notifier_call = da9063_reboot_notifier;
+	devm_register_reboot_notifier(dev, &da9063->reboot_nb);
+
 	return devm_watchdog_register_device(dev, wdd);
 }
 
diff --git a/include/linux/mfd/da9063/core.h b/include/linux/mfd/da9063/core.h
index fa7a43f02f27..9a3283d488b7 100644
--- a/include/linux/mfd/da9063/core.h
+++ b/include/linux/mfd/da9063/core.h
@@ -85,6 +85,8 @@  struct da9063 {
 	int		chip_irq;
 	unsigned int	irq_base;
 	struct regmap_irq_chip_data *regmap_irq;
+
+	struct notifier_block reboot_nb;
 };
 
 int da9063_device_init(struct da9063 *da9063, unsigned int irq);