Re: [PATCH V5] watchdog: mtk: support dual mode when the bark irq is available

From: Guenter Roeck
Date: Wed Apr 21 2021 - 10:11:37 EST


On 4/21/21 1:05 AM, Wang Qing wrote:
> Support using irq handling wdt bark first instead of directly resetting.
>
> When the watchdog timer expires in dual mode, an interrupt will be
> triggered first, then the timing restarts. The reset signal will be
> initiated when the timer expires again.
>
> The dual mode is disabled by default.
>

This means the real timeout is now timeout * 2. This is not what
is supposed to happen. The hard watchdog timeout needs to happen at
'timeout'.

If you want to do this, it needs to be done using pre-timeout,
only the pre-timeout time (and thus the watchdog timeout written
into the chip) must be limited to timeout / 2. Pre-timeout must
by default be disabled and only be supported if an interrupt was
provided.

I don't see a need for an additional module parameter. Providing
an interrupt implies that pre-timeout support is wanted. This needs
to be documented accordingly.

I am not lot looking at the errors reported by 0-day. Please address those.

Guenter

> V2:
> - panic() by default if WATCHDOG_PRETIMEOUT_GOV is not enabled.
>
> V3:
> - Modify the pretimeout behavior, manually reset after the pretimeout
> - is processed and wait until timeout.
>
> V4:
> - Remove pretimeout related processing.
> - Add dual mode control separately.
>
> V5:
> - Fix some formatting and printing problems.
>
> Signed-off-by: Wang Qing <wangqing@xxxxxxxx>
> ---
> drivers/watchdog/mtk_wdt.c | 36 ++++++++++++++++++++++++++++++++----
> 1 file changed, 32 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/watchdog/mtk_wdt.c b/drivers/watchdog/mtk_wdt.c
> index 97ca993..40122f8
> --- a/drivers/watchdog/mtk_wdt.c
> +++ b/drivers/watchdog/mtk_wdt.c
> @@ -25,6 +25,7 @@
> #include <linux/reset-controller.h>
> #include <linux/types.h>
> #include <linux/watchdog.h>
> +#include <linux/interrupt.h>
>
> #define WDT_MAX_TIMEOUT 31
> #define WDT_MIN_TIMEOUT 1
> @@ -57,6 +58,7 @@
>
> static bool nowayout = WATCHDOG_NOWAYOUT;
> static unsigned int timeout;
> +static bool dual_mode;
>
> struct mtk_wdt_dev {
> struct watchdog_device wdt_dev;
> @@ -239,13 +241,23 @@ static int mtk_wdt_start(struct watchdog_device *wdt_dev)
> return ret;
>
> reg = ioread32(wdt_base + WDT_MODE);
> - reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
> + if (dual_mode)
> + reg |= (WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
> + else
> + reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
> reg |= (WDT_MODE_EN | WDT_MODE_KEY);
> iowrite32(reg, wdt_base + WDT_MODE);
>
> return 0;
> }
>
> +static irqreturn_t mtk_wdt_isr(int irq, void *arg)
> +{
> + panic("wdt bark!\n");
> +
> + return IRQ_HANDLED;
> +}
> +
> static const struct watchdog_info mtk_wdt_info = {
> .identity = DRV_NAME,
> .options = WDIOF_SETTIMEOUT |
> @@ -267,7 +279,7 @@ static int mtk_wdt_probe(struct platform_device *pdev)
> struct device *dev = &pdev->dev;
> struct mtk_wdt_dev *mtk_wdt;
> const struct mtk_wdt_data *wdt_data;
> - int err;
> + int err, irq;
>
> mtk_wdt = devm_kzalloc(dev, sizeof(*mtk_wdt), GFP_KERNEL);
> if (!mtk_wdt)
> @@ -279,6 +291,19 @@ static int mtk_wdt_probe(struct platform_device *pdev)
> if (IS_ERR(mtk_wdt->wdt_base))
> return PTR_ERR(mtk_wdt->wdt_base);
>
> + if (dual_mode) {
> + irq = platform_get_irq(pdev, 0);
> + if (irq > 0) {
> + err = devm_request_irq(&pdev->dev, irq, mtk_wdt_isr, 0, "wdt_bark",
> + &mtk_wdt->wdt_dev);
> + if (err)
> + return err;
> + } else {
> + dual_mode = 0;
> + dev_info(&pdev->dev, "couldn't get wdt irq, set dual_mode = 0\n");
> + }
> + }
> +
> mtk_wdt->wdt_dev.info = &mtk_wdt_info;
> mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
> mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
> @@ -299,8 +324,8 @@ static int mtk_wdt_probe(struct platform_device *pdev)
> if (unlikely(err))
> return err;
>
> - dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
> - mtk_wdt->wdt_dev.timeout, nowayout);
> + dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d,
> + dual_mode=%d)\n", mtk_wdt->wdt_dev.timeout, nowayout, dual_mode);
>
> wdt_data = of_device_get_match_data(dev);
> if (wdt_data) {
> @@ -368,6 +393,9 @@ module_param(nowayout, bool, 0);
> MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
> __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
>
> +module_param(dual_mode, bool, 0);
> +MODULE_PARM_DESC(dual_mode, "Dual mode triggers irq before reset (default=0)");
> +
> MODULE_LICENSE("GPL");
> MODULE_AUTHOR("Matthias Brugger <matthias.bgg@xxxxxxxxx>");
> MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
>