Skip to contentSkip to conversation

watchdog: mtk: support pre-timeout when the bark irq is available

PATCH Superseded Wang Qing 2 participants 4 messages Apr 14, 2021 – Apr 15, 2021

Subsystems (2)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Wang Qing

author
#0 lore raw

PATCH v3 1/1 watchdog: mtk: support pre-timeout when the bark irq is available

Use the bark interrupt as the pretimeout notifier if available.

By default, the pretimeout notification shall occur one second earlier than the timeout.

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.

---
 drivers/watchdog/mtk_wdt.c | 62 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 57 insertions(+), 5 deletions(-)
drivers/watchdog/mtk_wdt.c +57 -5
index 97ca993..7bef1e3
@@ -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
@@ -234,18 +235,46 @@ static int mtk_wdt_start(struct watchdog_device *wdt_dev)
 	void __iomem *wdt_base = mtk_wdt->wdt_base;
 	int ret;
 
-	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
+	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout - wdt_dev->pretimeout);
 	if (ret < 0)
 		return ret;
 
 	reg = ioread32(wdt_base + WDT_MODE);
-	reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
+	reg &= ~WDT_MODE_IRQ_EN;
+	if (wdt_dev->pretimeout)
+		reg |= WDT_MODE_IRQ_EN;
+	else
+		reg &= ~WDT_MODE_IRQ_EN;
 	reg |= (WDT_MODE_EN | WDT_MODE_KEY);
 	iowrite32(reg, wdt_base + WDT_MODE);
 
 	return 0;
 }
 
+static int mtk_wdt_set_pretimeout(struct watchdog_device *wdd,
+				   unsigned int timeout)
+{
+	wdd->pretimeout = timeout;
+	return mtk_wdt_start(wdd);
+}
+
+static irqreturn_t mtk_wdt_isr(int irq, void *arg)
+{
+	struct watchdog_device *wdd = arg;
+	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdd);
+	void __iomem *wdt_base = mtk_wdt->wdt_base;
+
+	watchdog_notify_pretimeout(wdd);
+	/*
+	 * Guaranteed to be reset when the timeout
+	 * expires under any situations
+	 */
+	mdelay(1000*wdd->pretimeout);
+	writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
+
+	return IRQ_HANDLED;
+}
+
 static const struct watchdog_info mtk_wdt_info = {
 	.identity	= DRV_NAME,
 	.options	= WDIOF_SETTIMEOUT |
@@ -253,12 +282,21 @@ static const struct watchdog_info mtk_wdt_info = {
 			  WDIOF_MAGICCLOSE,
 };
 
+static const struct watchdog_info mtk_wdt_pt_info = {
+	.identity	= DRV_NAME,
+	.options	= WDIOF_SETTIMEOUT |
+			  WDIOF_PRETIMEOUT |
+			  WDIOF_KEEPALIVEPING |
+			  WDIOF_MAGICCLOSE,
+};
+
 static const struct watchdog_ops mtk_wdt_ops = {
 	.owner		= THIS_MODULE,
 	.start		= mtk_wdt_start,
 	.stop		= mtk_wdt_stop,
 	.ping		= mtk_wdt_ping,
 	.set_timeout	= mtk_wdt_set_timeout,
+	.set_pretimeout	= mtk_wdt_set_pretimeout,
 	.restart	= mtk_wdt_restart,
 };
 
@@ -267,7 +305,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,7 +317,22 @@ static int mtk_wdt_probe(struct platform_device *pdev)
 	if (IS_ERR(mtk_wdt->wdt_base))
 		return PTR_ERR(mtk_wdt->wdt_base);
 
-	mtk_wdt->wdt_dev.info = &mtk_wdt_info;
+	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;
+
+		mtk_wdt->wdt_dev.info = &mtk_wdt_pt_info;
+		mtk_wdt->wdt_dev.pretimeout = 1;
+	} else {
+		if (irq == -EPROBE_DEFER)
+			return -EPROBE_DEFER;
+
+		mtk_wdt->wdt_dev.info = &mtk_wdt_info;
+	}
+
 	mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
 	mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
 	mtk_wdt->wdt_dev.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT * 1000;
@@ -360,7 +413,6 @@ static struct platform_driver mtk_wdt_driver = {
 };
 
 module_platform_driver(mtk_wdt_driver);
-
 module_param(timeout, uint, 0);
 MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
 
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 46 lines · Wang Qing

Use the bark interrupt as the pretimeout notifier if available.

By default, the pretimeout notification shall occur one second earlier than the timeout.

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.

---
 drivers/watchdog/mtk_wdt.c | 62 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 57 insertions(+), 5 deletions(-)
drivers/watchdog/mtk_wdt.c +7 -2
index 97ca993..7bef1e3
@@ -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
@@ -234,18 +235,46 @@ static int mtk_wdt_start(struct watchdog_device *wdt_dev)
 	void __iomem *wdt_base = mtk_wdt->wdt_base;
 	int ret;
 
-	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
+	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout - wdt_dev->pretimeout);
 	if (ret < 0)
 		return ret;
 
 	reg = ioread32(wdt_base + WDT_MODE);
-	reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
+	reg &= ~WDT_MODE_IRQ_EN;
+	if (wdt_dev->pretimeout)
+		reg |= WDT_MODE_IRQ_EN;
+	else
+		reg &= ~WDT_MODE_IRQ_EN;
 	reg |= (WDT_MODE_EN | WDT_MODE_KEY);

drivers/watchdog/mtk_wdt.c static int mtk_wdt_start(struct watchdog_device *wdt_dev) · line 258

Quoted 10 lines
drivers/watchdog/mtk_wdt.c +5 -0
 	iowrite32(reg, wdt_base + WDT_MODE);
 
 	return 0;
 }
 
+static int mtk_wdt_set_pretimeout(struct watchdog_device *wdd,
+				   unsigned int timeout)
+{
+	wdd->pretimeout = timeout;
+	return mtk_wdt_start(wdd);

The watchdog is not necessarily active here.

Quoted 4 lines
drivers/watchdog/mtk_wdt.c +4 -0
+}
+
+static irqreturn_t mtk_wdt_isr(int irq, void *arg)
+{

drivers/watchdog/mtk_wdt.c static int mtk_wdt_start(struct watchdog_device *wdt_dev) · line 272

Quoted 10 lines
drivers/watchdog/mtk_wdt.c +10 -0
+	struct watchdog_device *wdd = arg;
+	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdd);
+	void __iomem *wdt_base = mtk_wdt->wdt_base;
+
+	watchdog_notify_pretimeout(wdd);
+	/*
+	 * Guaranteed to be reset when the timeout
+	 * expires under any situations
+	 */
+	mdelay(1000*wdd->pretimeout);

That is not how this is supposed to work. The idea with a pretimeout is that the real watchdog reset will happen under all circumstances, and that executing the pretimeout (and changing some hardware registers) is not a prerequisite for the real timeout to happen. After all, the system could be stuck hard, with interrupts disabled.

On top of that, just sleeping here while waiting for the real timeout and then resetting the system isn't the idea either. On a single core system this will just hang. On a multi-core system, who knows if userspace managed to ping the watchdog in the meantime.

Unless there is a means to trigger the watchdog twice, without intervention, the first time generating an interrupt and the second time resetting the system, there is no way for this to work. I don't see how this chip really supports pretimeout. It seems that it supports either a hard reset or generating an interrupt on watchdog timeout, and there is only a single timeout.

If you have a use case for generating an interrupt and resetting the system via software (ie panic) _instead_ of having it generate a hard reset, please feel free to submit a patch along that line, together with a description of its use case.

Thanks,
Guenter

Quoted 72 lines
drivers/watchdog/mtk_wdt.c +31 -3
+	writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
+
+	return IRQ_HANDLED;
+}
+
 static const struct watchdog_info mtk_wdt_info = {
 	.identity	= DRV_NAME,
 	.options	= WDIOF_SETTIMEOUT |
@@ -253,12 +282,21 @@ static const struct watchdog_info mtk_wdt_info = {
 			  WDIOF_MAGICCLOSE,
 };
 
+static const struct watchdog_info mtk_wdt_pt_info = {
+	.identity	= DRV_NAME,
+	.options	= WDIOF_SETTIMEOUT |
+			  WDIOF_PRETIMEOUT |
+			  WDIOF_KEEPALIVEPING |
+			  WDIOF_MAGICCLOSE,
+};
+
 static const struct watchdog_ops mtk_wdt_ops = {
 	.owner		= THIS_MODULE,
 	.start		= mtk_wdt_start,
 	.stop		= mtk_wdt_stop,
 	.ping		= mtk_wdt_ping,
 	.set_timeout	= mtk_wdt_set_timeout,
+	.set_pretimeout	= mtk_wdt_set_pretimeout,
 	.restart	= mtk_wdt_restart,
 };
 
@@ -267,7 +305,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,7 +317,22 @@ static int mtk_wdt_probe(struct platform_device *pdev)
 	if (IS_ERR(mtk_wdt->wdt_base))
 		return PTR_ERR(mtk_wdt->wdt_base);
 
-	mtk_wdt->wdt_dev.info = &mtk_wdt_info;
+	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;
+
+		mtk_wdt->wdt_dev.info = &mtk_wdt_pt_info;
+		mtk_wdt->wdt_dev.pretimeout = 1;
+	} else {
+		if (irq == -EPROBE_DEFER)
+			return -EPROBE_DEFER;
+
+		mtk_wdt->wdt_dev.info = &mtk_wdt_info;
+	}
+
 	mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
 	mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
 	mtk_wdt->wdt_dev.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT * 1000;
@@ -360,7 +413,6 @@ static struct platform_driver mtk_wdt_driver = {
 };
 
 module_platform_driver(mtk_wdt_driver);
-
 module_param(timeout, uint, 0);
 MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
 
author
#2 lore raw
Quoted 93 lines · Wang Qing
Quoted 46 lines

Use the bark interrupt as the pretimeout notifier if available.

By default, the pretimeout notification shall occur one second earlier than the timeout.

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.

---
 drivers/watchdog/mtk_wdt.c | 62 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 57 insertions(+), 5 deletions(-)
drivers/watchdog/mtk_wdt.c +7 -2
index 97ca993..7bef1e3
@@ -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
@@ -234,18 +235,46 @@ static int mtk_wdt_start(struct watchdog_device *wdt_dev)
 	void __iomem *wdt_base = mtk_wdt->wdt_base;
 	int ret;
 
-	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
+	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout - wdt_dev->pretimeout);
 	if (ret < 0)
 		return ret;
 
 	reg = ioread32(wdt_base + WDT_MODE);
-	reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
+	reg &= ~WDT_MODE_IRQ_EN;
+	if (wdt_dev->pretimeout)
+		reg |= WDT_MODE_IRQ_EN;
+	else
+		reg &= ~WDT_MODE_IRQ_EN;
 	reg |= (WDT_MODE_EN | WDT_MODE_KEY);

drivers/watchdog/mtk_wdt.c static int mtk_wdt_start(struct watchdog_device *wdt_dev) · line 258

Quoted 10 lines
drivers/watchdog/mtk_wdt.c +5 -0
 	iowrite32(reg, wdt_base + WDT_MODE);
 
 	return 0;
 }
 
+static int mtk_wdt_set_pretimeout(struct watchdog_device *wdd,
+				   unsigned int timeout)
+{
+	wdd->pretimeout = timeout;
+	return mtk_wdt_start(wdd);

The watchdog is not necessarily active here.

Quoted 4 lines
drivers/watchdog/mtk_wdt.c +4 -0
+}
+
+static irqreturn_t mtk_wdt_isr(int irq, void *arg)
+{

drivers/watchdog/mtk_wdt.c static int mtk_wdt_start(struct watchdog_device *wdt_dev) · line 272

Quoted 10 lines
drivers/watchdog/mtk_wdt.c +10 -0
+	struct watchdog_device *wdd = arg;
+	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdd);
+	void __iomem *wdt_base = mtk_wdt->wdt_base;
+
+	watchdog_notify_pretimeout(wdd);
+	/*
+	 * Guaranteed to be reset when the timeout
+	 * expires under any situations
+	 */
+	mdelay(1000*wdd->pretimeout);

That is not how this is supposed to work. The idea with a pretimeout is that the real watchdog reset will happen under all circumstances, and that executing the pretimeout (and changing some hardware registers) is not a prerequisite for the real timeout to happen. After all, the system could be stuck hard, with interrupts disabled.

On top of that, just sleeping here while waiting for the real timeout and then resetting the system isn't the idea either. On a single core system this will just hang. On a multi-core system, who knows if userspace managed to ping the watchdog in the meantime.

Unless there is a means to trigger the watchdog twice, without intervention, the first time generating an interrupt and the second time resetting the system, there is no way for this to work. I don't see how this chip really supports pretimeout. It seems that it supports either a hard reset or generating an interrupt on watchdog timeout, and there is only a single timeout.

Quoted 8 lines

If you have a use case for generating an interrupt and resetting the system via software (ie panic) _instead_ of having it generate a hard reset, please feel free to submit a patch along that line, together with a description of its use case.

Thanks,
Guenter

Yes, as mentioned before, the behavior of WDT_MODE_IRQ_EN is use irq instead of reset, so we must use WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN if like you said "the first time generating an interrupt and the second time resetting the system" .

The Dual mode is mentioned in the MTK datasheet:
In this mode, the watchdog will be AUTO-RESTART after interrupt is triggered. AP need to clear WDT_STA after receiving interrupt from TOPRGU, or system reset will be triggered after watchdog timer expires.
Instructions for use:
Set wdt_en = 1'b1.
Set dual_mode = 1'b1.
Set wdt_irq = 1'b1.

We can use Dual mode to achieve pretimeout behavior, only in this way can we get more information during pretimeout processing, instead of directly resetting.

Thanks,
Qing

maintainer
#3 lore raw
Quoted 14 lines · 王擎

Yes, as mentioned before, the behavior of WDT_MODE_IRQ_EN is use irq instead of reset, so we must use WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN if like you said "the first time generating an interrupt and the second time resetting the system" .

The Dual mode is mentioned in the MTK datasheet:
In this mode, the watchdog will be AUTO-RESTART after interrupt is triggered. AP need to clear WDT_STA after receiving interrupt from TOPRGU, or system reset will be triggered after watchdog timer expires.
Instructions for use:
Set wdt_en = 1'b1.
Set dual_mode = 1'b1.
Set wdt_irq = 1'b1.

Quoted 3 lines

We can use Dual mode to achieve pretimeout behavior, only in this way can we get more information during pretimeout processing, instead of directly resetting.

If so, the pretimeout would always be half of the full timeout, and the timeout to configure would be timeout / 2. The code would have to adjust the pretimeout time accordingly. Also, to avoid unexpected and changed behavior, the prefimeout should by default be disabled.

Guenter