Skip to contentSkip to conversation

clocksource: fix Tegra234 SoC Watchdog Timer.

PATCH Superseded Robert Lin 4 participants 16 messages May 2, 2025 – May 5, 2025

Subsystems (2)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Robert Lin

author
#0 lore raw

v7 cover clocksource: fix Tegra234 SoC Watchdog Timer.

From: robelin <[email protected]>

This set of patches includes a fix for watchdog for it may not bark due to self-pinging and adds WDIOC_GETTIMELEFT support.

--
V7:
- Fix formatting
- Consider overflow and warn if happens

V6:
- Fix timeleft value addition using unmatched time unit
- Use u64 type to maintain the microseconds value in case of overflow

V5:
- Print warning message if get unexpected value from the register

V4:
- Improve the precision of timeleft value
- Fix the unused variable warning

V3:
- Improve comment description
- Refactor to fit codeline within 80 columns
- Remove unused if(0) blocks

V2:
- Fix a compilation error, a warning and updates copyright --

Pohsun Su (2):
clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support clocksource/drivers/timer-tegra186: fix watchdog self-pinging

robelin (1):
clocksource/drivers/timer-tegra186: Remove unused bits

 drivers/clocksource/timer-tegra186.c | 100 +++++++++++++++++----------
 1 file changed, 63 insertions(+), 37 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v7 1/3 clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
 drivers/clocksource/timer-tegra186.c | 64 +++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +63 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
  */
 
+#include <linux/bitfield.h>
 #include <linux/clocksource.h>
 #include <linux/module.h>
 #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
 
 #define TMRSR 0x004
 #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
 
 #define TMRCSSR 0x008
 #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
 #define  WDTCR_TIMER_SOURCE_MASK 0xf
 #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
 
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
 #define WDTCMDR 0x008
 #define  WDTCMDR_DISABLE_COUNTER BIT(1)
 #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
 	return 0;
 }
 
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+	u32 expiration, val;
+	u64 timeleft;
+
+	if (!watchdog_active(&wdt->base)) {
+		/* return zero if the watchdog timer is not activated. */
+		return 0;
+	}
+
+	/*
+	 * Reset occurs on the fifth expiration of the
+	 * watchdog timer and so when the watchdog timer is configured,
+	 * the actual value programmed into the counter is 1/5 of the
+	 * timeout value. Once the counter reaches 0, expiration count
+	 * will be increased by 1 and the down counter restarts.
+	 * Hence to get the time left before system reset we must
+	 * combine 2 parts:
+	 * 1. value of the current down counter
+	 * 2. (number of counter expirations remaining) * (timeout/5)
+	 */
+
+	/* Get the current number of counter expirations. Should be a
+	 * value between 0 and 4
+	 */
+	val = readl_relaxed(wdt->regs + WDTSR);
+	expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+	if (WARN_ON(expiration > 4))
+		return 0;
+
+	/* Get the current counter value in microsecond. */
+	val = readl_relaxed(wdt->tmr->regs + TMRSR);
+	timeleft = FIELD_GET(TMRSR_PCV, val);
+
+	/*
+	 * Calculate the time remaining by adding the time for the
+	 * counter value to the time of the counter expirations that
+	 * remain.
+	 */
+	timeleft += (((u64)wdt->base.timeout * USEC_PER_SEC) / 5) * (4 - expiration);
+
+	/*
+	 * Convert the current counter value to seconds,
+	 * rounding up to the nearest second. Cast u64 to
+	 * u32 under the assumption that no overflow happens
+	 * when coverting to seconds.
+	 */
+	timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;
+
+	if (WARN_ON(timeleft > U32_MAX))
+		return U32_MAX;
+
+	return lower_32_bits(timeleft);
+}
+
 static const struct watchdog_ops tegra186_wdt_ops = {
 	.owner = THIS_MODULE,
 	.start = tegra186_wdt_start,
 	.stop = tegra186_wdt_stop,
 	.ping = tegra186_wdt_ping,
 	.set_timeout = tegra186_wdt_set_timeout,
+	.get_timeleft = tegra186_wdt_get_timeleft,
 };
 
 static struct tegra186_wdt *tegra186_wdt_create(struct tegra186_timer *tegra,
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 69 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
 drivers/clocksource/timer-tegra186.c | 64 +++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +26 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
  */
 
+#include <linux/bitfield.h>
 #include <linux/clocksource.h>
 #include <linux/module.h>
 #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
 
 #define TMRSR 0x004
 #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
 
 #define TMRCSSR 0x008
 #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
 #define  WDTCR_TIMER_SOURCE_MASK 0xf
 #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
 
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
 #define WDTCMDR 0x008
 #define  WDTCMDR_DISABLE_COUNTER BIT(1)
 #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
 	return 0;
 }
 
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+	u32 expiration, val;
+	u64 timeleft;
+
+	if (!watchdog_active(&wdt->base)) {
+		/* return zero if the watchdog timer is not activated. */
+		return 0;
+	}
+
+	/*
+	 * Reset occurs on the fifth expiration of the
+	 * watchdog timer and so when the watchdog timer is configured,
+	 * the actual value programmed into the counter is 1/5 of the
+	 * timeout value. Once the counter reaches 0, expiration count
+	 * will be increased by 1 and the down counter restarts.
+	 * Hence to get the time left before system reset we must
+	 * combine 2 parts:
+	 * 1. value of the current down counter

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 10 lines
drivers/clocksource/timer-tegra186.c +10 -0
+	 * 2. (number of counter expirations remaining) * (timeout/5)
+	 */
+
+	/* Get the current number of counter expirations. Should be a
+	 * value between 0 and 4
+	 */
+	val = readl_relaxed(wdt->regs + WDTSR);
+	expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+	if (WARN_ON(expiration > 4))
+		return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

Quoted 8 lines
drivers/clocksource/timer-tegra186.c +8 -0
+	/* Get the current counter value in microsecond. */
+	val = readl_relaxed(wdt->tmr->regs + TMRSR);
+	timeleft = FIELD_GET(TMRSR_PCV, val);
+
+	/*
+	 * Calculate the time remaining by adding the time for the
+	 * counter value to the time of the counter expirations that
+	 * remain.

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 291

Quoted 10 lines
drivers/clocksource/timer-tegra186.c +10 -0
+	 */
+	timeleft += (((u64)wdt->base.timeout * USEC_PER_SEC) / 5) * (4 - expiration);
+
+	/*
+	 * Convert the current counter value to seconds,
+	 * rounding up to the nearest second. Cast u64 to
+	 * u32 under the assumption that no overflow happens
+	 * when coverting to seconds.
+	 */
+	timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;

Did you check there is a macro fitting the need in math.h ?

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 293

Quoted 1 line
drivers/clocksource/timer-tegra186.c +1 -0
+	if (WARN_ON(timeleft > U32_MAX))

s/WARN_ON/pr_err/

Quoted 18 lines
drivers/clocksource/timer-tegra186.c +6 -0
+		return U32_MAX;
+
+	return lower_32_bits(timeleft);
+}
+
 static const struct watchdog_ops tegra186_wdt_ops = {
 	.owner = THIS_MODULE,
 	.start = tegra186_wdt_start,
 	.stop = tegra186_wdt_stop,
 	.ping = tegra186_wdt_ping,
 	.set_timeout = tegra186_wdt_set_timeout,
+	.get_timeleft = tegra186_wdt_get_timeleft,
 };
 
 static struct tegra186_wdt *tegra186_wdt_create(struct tegra186_timer *tegra,
Signature · 1 line
2.34.1
Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
maintainer
#3 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 75 lines · Daniel Lezcano
Quoted 74 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
  drivers/clocksource/timer-tegra186.c | 64 +++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +31 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
  // SPDX-License-Identifier: GPL-2.0-only
  /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
   */
 
+#include <linux/bitfield.h>
  #include <linux/clocksource.h>
  #include <linux/module.h>
  #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
 
  #define TMRSR 0x004
  #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
 
  #define TMRCSSR 0x008
  #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
  #define  WDTCR_TIMER_SOURCE_MASK 0xf
  #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
 
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
  #define WDTCMDR 0x008
  #define  WDTCMDR_DISABLE_COUNTER BIT(1)
  #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
  	return 0;
  }
 
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+	u32 expiration, val;
+	u64 timeleft;
+
+	if (!watchdog_active(&wdt->base)) {
+		/* return zero if the watchdog timer is not activated. */
+		return 0;
+	}
+
+	/*
+	 * Reset occurs on the fifth expiration of the
+	 * watchdog timer and so when the watchdog timer is configured,
+	 * the actual value programmed into the counter is 1/5 of the
+	 * timeout value. Once the counter reaches 0, expiration count
+	 * will be increased by 1 and the down counter restarts.
+	 * Hence to get the time left before system reset we must
+	 * combine 2 parts:
+	 * 1. value of the current down counter
+	 * 2. (number of counter expirations remaining) * (timeout/5)
+	 */
+
+	/* Get the current number of counter expirations. Should be a
+	 * value between 0 and 4
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 5 lines
drivers/clocksource/timer-tegra186.c +5 -0
+	 */
+	val = readl_relaxed(wdt->regs + WDTSR);
+	expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+	if (WARN_ON(expiration > 4))
+		return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

I prefer the WARN. This should never happen. If we do change this, then dev_WARN() might be more appropriate, but I think that this is fine too.

Quoted 13 lines
Quoted 13 lines
drivers/clocksource/timer-tegra186.c +12 -0
+	/* Get the current counter value in microsecond. */
+	val = readl_relaxed(wdt->tmr->regs + TMRSR);
+	timeleft = FIELD_GET(TMRSR_PCV, val);
+
+	/*
+	 * Calculate the time remaining by adding the time for the
+	 * counter value to the time of the counter expirations that
+	 * remain.
+	 */
+	timeleft += (((u64)wdt->base.timeout * USEC_PER_SEC) / 5) * (4 - expiration);
+
+	/*
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 291

Quoted 6 lines
drivers/clocksource/timer-tegra186.c +6 -0
+	 * Convert the current counter value to seconds,
+	 * rounding up to the nearest second. Cast u64 to
+	 * u32 under the assumption that no overflow happens
+	 * when coverting to seconds.
+	 */
+	timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;

Did you check there is a macro fitting the need in math.h ?

I had a quick look, but looking again, I see we can use DIV_ROUND_CLOSEST_ULL() here.

Quoted 4 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 293

Quoted 2 lines
drivers/clocksource/timer-tegra186.c +1 -0
+	if (WARN_ON(timeleft > U32_MAX))

s/WARN_ON/pr_err/

Why? Again this is not expected. I think that this is fine.

Quoted 20 lines
Quoted 19 lines
drivers/clocksource/timer-tegra186.c +6 -0
+		return U32_MAX;
+
+	return lower_32_bits(timeleft);
+}
+
  static const struct watchdog_ops tegra186_wdt_ops = {
  	.owner = THIS_MODULE,
  	.start = tegra186_wdt_start,
  	.stop = tegra186_wdt_stop,
  	.ping = tegra186_wdt_ping,
  	.set_timeout = tegra186_wdt_set_timeout,
+	.get_timeleft = tegra186_wdt_get_timeleft,
  };
 
  static struct tegra186_wdt *tegra186_wdt_create(struct tegra186_timer *tegra,
Signature · 1 line
2.34.1
Signature · 1 line
nvpublic
#4 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 82 lines · Jon Hunter
Quoted 70 lines · Daniel Lezcano
Quoted 69 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
  drivers/clocksource/timer-tegra186.c | 64 +++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +31 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
  // SPDX-License-Identifier: GPL-2.0-only
  /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
   */
+#include <linux/bitfield.h>
  #include <linux/clocksource.h>
  #include <linux/module.h>
  #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
  #define TMRSR 0x004
  #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
  #define TMRCSSR 0x008
  #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
  #define  WDTCR_TIMER_SOURCE_MASK 0xf
  #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
  #define WDTCMDR 0x008
  #define  WDTCMDR_DISABLE_COUNTER BIT(1)
  #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
  	return 0;
  }
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+	u32 expiration, val;
+	u64 timeleft;
+
+	if (!watchdog_active(&wdt->base)) {
+		/* return zero if the watchdog timer is not activated. */
+		return 0;
+	}
+
+	/*
+	 * Reset occurs on the fifth expiration of the
+	 * watchdog timer and so when the watchdog timer is configured,
+	 * the actual value programmed into the counter is 1/5 of the
+	 * timeout value. Once the counter reaches 0, expiration count
+	 * will be increased by 1 and the down counter restarts.
+	 * Hence to get the time left before system reset we must
+	 * combine 2 parts:
+	 * 1. value of the current down counter
+	 * 2. (number of counter expirations remaining) * (timeout/5)
+	 */
+
+	/* Get the current number of counter expirations. Should be a
+	 * value between 0 and 4
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 5 lines
drivers/clocksource/timer-tegra186.c +5 -0
+	 */
+	val = readl_relaxed(wdt->regs + WDTSR);
+	expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+	if (WARN_ON(expiration > 4))
+		return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

Quoted 2 lines

I prefer the WARN. This should never happen. If we do change this, then dev_WARN() might be more appropriate, but I think that this is fine too.

The function tegra186_wdt_get_timeleft() is triggered from userspace via an ioctl or sysfs. The documentation process/coding-style.rst says:

"""
Do not WARN lightly
*******************

WARN*() is intended for unexpected, this-should-never-happen situations. WARN*() macros are not to be used for anything that is expected to happen during normal operation. These are not pre- or post-condition asserts, for example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. """

I understand it is important to check the return value in order to have a consistent result when computing the remaining time but that should not trigger a warning each time.

[ ... ]

Quoted 10 lines
Quoted 9 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 291

Quoted 7 lines
drivers/clocksource/timer-tegra186.c +7 -0
+	/*
+	 * Convert the current counter value to seconds,
+	 * rounding up to the nearest second. Cast u64 to
+	 * u32 under the assumption that no overflow happens
+	 * when coverting to seconds.
+	 */
+	timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;

Did you check there is a macro fitting the need in math.h ?

Quoted 2 lines

I had a quick look, but looking again, I see we can use DIV_ROUND_CLOSEST_ULL() here.

What about 'roundup()' ?

Quoted 5 lines
Quoted 3 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 293

Quoted 1 line
drivers/clocksource/timer-tegra186.c +1 -0
+	if (WARN_ON(timeleft > U32_MAX))

s/WARN_ON/pr_err/

Why? Again this is not expected. I think that this is fine.

[ ... ]

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
maintainer
#5 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 101 lines · Daniel Lezcano
Quoted 82 lines · Jon Hunter
Quoted 70 lines · Daniel Lezcano
Quoted 69 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
   drivers/clocksource/timer-tegra186.c | 64 +++++++++++++++++++++++++++-
   1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +31 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
   // SPDX-License-Identifier: GPL-2.0-only
   /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
    */
+#include <linux/bitfield.h>
   #include <linux/clocksource.h>
   #include <linux/module.h>
   #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
   #define TMRSR 0x004
   #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
   #define TMRCSSR 0x008
   #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
   #define  WDTCR_TIMER_SOURCE_MASK 0xf
   #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
   #define WDTCMDR 0x008
   #define  WDTCMDR_DISABLE_COUNTER BIT(1)
   #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
   	return 0;
   }
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+	u32 expiration, val;
+	u64 timeleft;
+
+	if (!watchdog_active(&wdt->base)) {
+		/* return zero if the watchdog timer is not activated. */
+		return 0;
+	}
+
+	/*
+	 * Reset occurs on the fifth expiration of the
+	 * watchdog timer and so when the watchdog timer is configured,
+	 * the actual value programmed into the counter is 1/5 of the
+	 * timeout value. Once the counter reaches 0, expiration count
+	 * will be increased by 1 and the down counter restarts.
+	 * Hence to get the time left before system reset we must
+	 * combine 2 parts:
+	 * 1. value of the current down counter
+	 * 2. (number of counter expirations remaining) * (timeout/5)
+	 */
+
+	/* Get the current number of counter expirations. Should be a
+	 * value between 0 and 4
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 5 lines
drivers/clocksource/timer-tegra186.c +5 -0
+	 */
+	val = readl_relaxed(wdt->regs + WDTSR);
+	expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+	if (WARN_ON(expiration > 4))
+		return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

Quoted 2 lines

I prefer the WARN. This should never happen. If we do change this, then dev_WARN() might be more appropriate, but I think that this is fine too.

The function tegra186_wdt_get_timeleft() is triggered from userspace via an ioctl or sysfs. The documentation process/coding-style.rst says:

"""
Do not WARN lightly
*******************

WARN*() is intended for unexpected, this-should-never-happen situations. WARN*() macros are not to be used for anything that is expected to happen during normal operation. These are not pre- or post-condition asserts, for example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. """

Quoted 3 lines

I understand it is important to check the return value in order to have a consistent result when computing the remaining time but that should not trigger a warning each time.

Yes so WARN sounds appropriate. It should never happen. I don't see the issue.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 291

Quoted 7 lines
drivers/clocksource/timer-tegra186.c +7 -0
+	/*
+	 * Convert the current counter value to seconds,
+	 * rounding up to the nearest second. Cast u64 to
+	 * u32 under the assumption that no overflow happens
+	 * when coverting to seconds.
+	 */
+	timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;

Did you check there is a macro fitting the need in math.h ?

Quoted 4 lines
Quoted 2 lines

I had a quick look, but looking again, I see we can use DIV_ROUND_CLOSEST_ULL() here.

What about 'roundup()' ?

'roundup' does not look the same as what is being done above. However, DIV_ROUND_CLOSEST_ULL() does.

Signature · 1 line
nvpublic
#6 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 107 lines · Jon Hunter
Quoted 104 lines · Daniel Lezcano
Quoted 85 lines · Jon Hunter
Quoted 73 lines · Daniel Lezcano
Quoted 72 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
   drivers/clocksource/timer-tegra186.c | 64 ++++++++++++++++++++++
+++++-

   1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +31 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
    // SPDX-License-Identifier: GPL-2.0-only
    /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
     */
+#include <linux/bitfield.h>
    #include <linux/clocksource.h>
    #include <linux/module.h>
    #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
    #define TMRSR 0x004
    #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
    #define TMRCSSR 0x008
    #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
    #define  WDTCR_TIMER_SOURCE_MASK 0xf
    #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
    #define WDTCMDR 0x008
    #define  WDTCMDR_DISABLE_COUNTER BIT(1)
    #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct 
 watchdog_device *wdd,
        return 0;
    }
+static unsigned int tegra186_wdt_get_timeleft(struct 
 watchdog_device *wdd)
+{
+    struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+    u32 expiration, val;
+    u64 timeleft;
+
+    if (!watchdog_active(&wdt->base)) {
+        /* return zero if the watchdog timer is not activated. */
+        return 0;
+    }
+
+    /*
+     * Reset occurs on the fifth expiration of the
+     * watchdog timer and so when the watchdog timer is configured,
+     * the actual value programmed into the counter is 1/5 of the
+     * timeout value. Once the counter reaches 0, expiration count
+     * will be increased by 1 and the down counter restarts.
+     * Hence to get the time left before system reset we must
+     * combine 2 parts:
+     * 1. value of the current down counter
+     * 2. (number of counter expirations remaining) * (timeout/5)
+     */
+
+    /* Get the current number of counter expirations. Should be a
+     * value between 0 and 4
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 5 lines
drivers/clocksource/timer-tegra186.c +5 -0
+     */
+    val = readl_relaxed(wdt->regs + WDTSR);
+    expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+    if (WARN_ON(expiration > 4))
+        return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

Quoted 2 lines

I prefer the WARN. This should never happen. If we do change this, then dev_WARN() might be more appropriate, but I think that this is fine too.

The function tegra186_wdt_get_timeleft() is triggered from userspace via an ioctl or sysfs. The documentation process/coding-style.rst says:

"""
Do not WARN lightly
*******************

WARN*() is intended for unexpected, this-should-never-happen situations. WARN*() macros are not to be used for anything that is expected to happen during normal operation. These are not pre- or post-condition asserts, for example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. """

Quoted 5 lines
Quoted 3 lines

I understand it is important to check the return value in order to have a consistent result when computing the remaining time but that should not trigger a warning each time.

Yes so WARN sounds appropriate. It should never happen. I don't see the issue.

The issue is if there is an userspace application reading the ioctl and or the sysfs, then the warning will be emitted each time if the never-happen condition exists. Preferably replace the WARN_ON by pr_warn_once() as suggested if the bug must be reported.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/clocksource/timer-tegra186.c +7 -0
+    /*
+     * Convert the current counter value to seconds,
+     * rounding up to the nearest second. Cast u64 to
+     * u32 under the assumption that no overflow happens
+     * when coverting to seconds.
+     */
+    timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Did you check there is a macro fitting the need in math.h ?

I had a quick look, but looking again, I see we can use DIV_ROUND_CLOSEST_ULL() here.

What about 'roundup()' ?

'roundup' does not look the same as what is being done above. However, DIV_ROUND_CLOSEST_ULL() does.

Ok, thanks for checking

-- Daniel

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
maintainer
#7 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 108 lines · Daniel Lezcano
Quoted 107 lines · Jon Hunter
Quoted 104 lines · Daniel Lezcano
Quoted 85 lines · Jon Hunter
Quoted 73 lines · Daniel Lezcano
Quoted 72 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
   drivers/clocksource/timer-tegra186.c | 64 +++++++++++++++++++++
+ +++++-

   1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +31 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
    // SPDX-License-Identifier: GPL-2.0-only
    /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
     */
+#include <linux/bitfield.h>
    #include <linux/clocksource.h>
    #include <linux/module.h>
    #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
    #define TMRSR 0x004
    #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
    #define TMRCSSR 0x008
    #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
    #define  WDTCR_TIMER_SOURCE_MASK 0xf
    #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
    #define WDTCMDR 0x008
    #define  WDTCMDR_DISABLE_COUNTER BIT(1)
    #define  WDTCMDR_START_COUNTER BIT(0)
@@ -235,12 +240,69 @@ static int tegra186_wdt_set_timeout(struct 
 watchdog_device *wdd,
        return 0;
    }
+static unsigned int tegra186_wdt_get_timeleft(struct 
 watchdog_device *wdd)
+{
+    struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+    u32 expiration, val;
+    u64 timeleft;
+
+    if (!watchdog_active(&wdt->base)) {
+        /* return zero if the watchdog timer is not activated. */
+        return 0;
+    }
+
+    /*
+     * Reset occurs on the fifth expiration of the
+     * watchdog timer and so when the watchdog timer is configured,
+     * the actual value programmed into the counter is 1/5 of the
+     * timeout value. Once the counter reaches 0, expiration count
+     * will be increased by 1 and the down counter restarts.
+     * Hence to get the time left before system reset we must
+     * combine 2 parts:
+     * 1. value of the current down counter
+     * 2. (number of counter expirations remaining) * (timeout/5)
+     */
+
+    /* Get the current number of counter expirations. Should be a
+     * value between 0 and 4
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 5 lines
drivers/clocksource/timer-tegra186.c +5 -0
+     */
+    val = readl_relaxed(wdt->regs + WDTSR);
+    expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+    if (WARN_ON(expiration > 4))
+        return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

Quoted 2 lines

I prefer the WARN. This should never happen. If we do change this, then dev_WARN() might be more appropriate, but I think that this is fine too.

The function tegra186_wdt_get_timeleft() is triggered from userspace via an ioctl or sysfs. The documentation process/coding-style.rst says:

"""
Do not WARN lightly
*******************

WARN*() is intended for unexpected, this-should-never-happen situations. WARN*() macros are not to be used for anything that is expected to happen during normal operation. These are not pre- or post-condition asserts, for example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. """

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I understand it is important to check the return value in order to have a consistent result when computing the remaining time but that should not trigger a warning each time.

Yes so WARN sounds appropriate. It should never happen. I don't see the issue.

The issue is if there is an userspace application reading the ioctl and or the sysfs, then the warning will be emitted each time if the never- happen condition exists. Preferably replace the WARN_ON by pr_warn_once() as suggested if the bug must be reported.

Sounds a bit funny 'if the never-happen condition exists' :-)

However, I will be fine with WARN_ON_ONCE(). I think that this warrants more of a large WARN splat than pr_warn() because it should never happen.

Jon

Signature · 1 line
nvpublic
author
#8 lore raw

RE: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 145 lines

-----Original Message-----
From: Jon Hunter <[email protected]>
Sent: Friday, May 2, 2025 8:37 PM
To: Daniel Lezcano <[email protected]>
Cc: Robert Lin <[email protected]>; [email protected]; [email protected]; Pohsun Su <[email protected]>; linux- [email protected]; [email protected]; Sumit Gupta <[email protected]>
Subject: Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 44 lines · Daniel Lezcano
Quoted 43 lines · Jon Hunter
Quoted 40 lines · Daniel Lezcano
Quoted 39 lines · Jon Hunter
Quoted 36 lines · Daniel Lezcano
Quoted 35 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
   drivers/clocksource/timer-tegra186.c | 64
+++++++++++++++++++++
+ +++++-

   1 file changed, 63 insertions(+), 1 deletion(-)
drivers/ renamed from drivers/clocksource/timer-tegra186.c +0 -0

clocksource/timer-tegra186.c index ea742889ee06..8d5698770cbd 100644

drivers/clocksource/timer-tegra186.c +3 -1
@@ -1,8 +1,9 @@
    // SPDX-License-Identifier: GPL-2.0-only
    /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
     */
+#include <linux/bitfield.h>
    #include <linux/clocksource.h>
    #include <linux/module.h>
    #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
    #define TMRSR 0x004
    #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
    #define TMRCSSR 0x008
    #define  TMRCSSR_SRC_USEC (0 << 0) @@ -46,6 +48,9 @@
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/clocksource/timer-tegra186.c +3 -0
    #define  WDTCR_TIMER_SOURCE_MASK 0xf
    #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+

   #define WDTCMDR 0x008
   #define  WDTCMDR_DISABLE_COUNTER BIT(1)
   #define  WDTCMDR_START_COUNTER BIT(0) @@ -235,12 +240,69

@@

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, return 0;
   }
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)

drivers/clocksource/timer-tegra186.c +25 -0
+{
+    struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+    u32 expiration, val;
+    u64 timeleft;
+
+    if (!watchdog_active(&wdt->base)) {
+        /* return zero if the watchdog timer is not activated.
+*/
+        return 0;
+    }
+
+    /*
+     * Reset occurs on the fifth expiration of the
+     * watchdog timer and so when the watchdog timer is
+configured,
+     * the actual value programmed into the counter is 1/5 of
+the
+     * timeout value. Once the counter reaches 0, expiration
+count
+     * will be increased by 1 and the down counter restarts.
+     * Hence to get the time left before system reset we must
+     * combine 2 parts:
+     * 1. value of the current down counter
+     * 2. (number of counter expirations remaining) *
+(timeout/5)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/clocksource/timer-tegra186.c

Quoted 8 lines

+     */

drivers/clocksource/timer-tegra186.c +7 -0
+
+    /* Get the current number of counter expirations. Should be
+a
+     * value between 0 and 4
+     */
+    val = readl_relaxed(wdt->regs + WDTSR);
+    expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT,

val);

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

+    if (WARN_ON(expiration > 4))
+        return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

I prefer the WARN. This should never happen. If we do change this, then
dev_WARN() might be more appropriate, but I think that this is fine too.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

The function tegra186_wdt_get_timeleft() is triggered from userspace via an ioctl or sysfs. The documentation process/coding-style.rst says:

"""
Do not WARN lightly
*******************

WARN*() is intended for unexpected, this-should-never-happen

situations.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

WARN*() macros are not to be used for anything that is expected to happen during normal operation. These are not pre- or post-condition asserts, for example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem.
"""

I understand it is important to check the return value in order to have a consistent result when computing the remaining time but that should not trigger a warning each time.

Quoted 7 lines
Quoted 2 lines

Yes so WARN sounds appropriate. It should never happen. I don't see the issue.

The issue is if there is an userspace application reading the ioctl and or the sysfs, then the warning will be emitted each time if the never- happen condition exists. Preferably replace the WARN_ON by pr_warn_once() as suggested if the bug must be reported.

Quoted 5 lines

Sounds a bit funny 'if the never-happen condition exists' :-)

However, I will be fine with WARN_ON_ONCE(). I think that this warrants more of a large WARN splat than pr_warn() because it should never happen.

I believe the two WARN_ON I used here are the "this-should-never-happen" situations. In my opinion, we are good to keep. But I am also good with WARN_ON_ONCE for compromise. I wonder if Daniel is good with this then I can fix them.

Quoted 4 lines

Jon

--
nvpublic

#9 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

On 05/05/2025 07:34, Robert Lin wrote:

[ ... ]

Quoted 16 lines
Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

Yes so WARN sounds appropriate. It should never happen. I don't see the issue.

The issue is if there is an userspace application reading the ioctl and or the sysfs, then the warning will be emitted each time if the never- happen condition exists. Preferably replace the WARN_ON by pr_warn_once() as suggested if the bug must be reported.

Quoted 6 lines

Sounds a bit funny 'if the never-happen condition exists' :-)

However, I will be fine with WARN_ON_ONCE(). I think that this warrants more of a large WARN splat than pr_warn() because it should never happen.

Quoted 4 lines

I believe the two WARN_ON I used here are the "this-should-never- happen" situations. In my opinion, we are good to keep. But I am also good with WARN_ON_ONCE for compromise. I wonder if Daniel is good with this then I can fix them.

I'm fine with this

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#10 lore raw

RE: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

In reply to Jon Hunter
Quoted 129 lines

-----Original Message-----
From: Jon Hunter <[email protected]>
Sent: Friday, May 2, 2025 6:16 PM
To: Daniel Lezcano <[email protected]>; Robert Lin <[email protected]>
Cc: [email protected]; [email protected]; Pohsun Su <[email protected]>; [email protected]; linux- [email protected]; Sumit Gupta <[email protected]>
Subject: Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

Quoted 11 lines · Daniel Lezcano
Quoted 10 lines · Robert Lin

From: Pohsun Su <[email protected]>

This change adds support for WDIOC_GETTIMELEFT so userspace programs can get the number of seconds before system reset by the watchdog timer via ioctl.

---
  drivers/clocksource/timer-tegra186.c | 64

+++++++++++++++++++++++++++-

Quoted 30 lines
Quoted 30 lines
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +3 -1
index ea742889ee06..8d5698770cbd 100644
@@ -1,8 +1,9 @@
  // SPDX-License-Identifier: GPL-2.0-only
  /*
- * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
+ * Copyright (c) 2019-2025 NVIDIA Corporation. All rights reserved.
   */
 
+#include <linux/bitfield.h>
  #include <linux/clocksource.h>
  #include <linux/module.h>
  #include <linux/interrupt.h>
@@ -30,6 +31,7 @@
 
  #define TMRSR 0x004
  #define  TMRSR_INTR_CLR BIT(30)
+#define  TMRSR_PCV GENMASK(28, 0)
 
  #define TMRCSSR 0x008
  #define  TMRCSSR_SRC_USEC (0 << 0)
@@ -46,6 +48,9 @@
  #define  WDTCR_TIMER_SOURCE_MASK 0xf
  #define  WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
 
Quoted 6 lines

drivers/clocksource/timer-tegra186.c

Quoted 6 lines
drivers/clocksource/timer-tegra186.c +3 -0
+#define WDTSR 0x004
+#define  WDTSR_CURRENT_EXPIRATION_COUNT GENMASK(14, 12)
+
  #define WDTCMDR 0x008
  #define  WDTCMDR_DISABLE_COUNTER BIT(1)
  #define  WDTCMDR_START_COUNTER BIT(0) @@ -235,12 +240,69 @@

static

Quoted 29 lines
Quoted 29 lines

int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
return 0;
}

drivers/clocksource/timer-tegra186.c +25 -0
+static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device
+*wdd) {
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+	u32 expiration, val;
+	u64 timeleft;
+
+	if (!watchdog_active(&wdt->base)) {
+		/* return zero if the watchdog timer is not activated. */
+		return 0;
+	}
+
+	/*
+	 * Reset occurs on the fifth expiration of the
+	 * watchdog timer and so when the watchdog timer is configured,
+	 * the actual value programmed into the counter is 1/5 of the
+	 * timeout value. Once the counter reaches 0, expiration count
+	 * will be increased by 1 and the down counter restarts.
+	 * Hence to get the time left before system reset we must
+	 * combine 2 parts:
+	 * 1. value of the current down counter
+	 * 2. (number of counter expirations remaining) * (timeout/5)
+	 */
+
+	/* Get the current number of counter expirations. Should be a
+	 * value between 0 and 4
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 272

Quoted 5 lines
drivers/clocksource/timer-tegra186.c +5 -0
+	 */
+	val = readl_relaxed(wdt->regs + WDTSR);
+	expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
+	if (WARN_ON(expiration > 4))
+		return 0;

Each call will generate a big warning in the message. May be simpler to add a pr_err() with an explicit message.

I prefer the WARN. This should never happen. If we do change this, then dev_WARN() might be more appropriate, but I think that this is fine too.

Quoted 14 lines
Quoted 14 lines
drivers/clocksource/timer-tegra186.c +13 -0
+	/* Get the current counter value in microsecond. */
+	val = readl_relaxed(wdt->tmr->regs + TMRSR);
+	timeleft = FIELD_GET(TMRSR_PCV, val);
+
+	/*
+	 * Calculate the time remaining by adding the time for the
+	 * counter value to the time of the counter expirations that
+	 * remain.
+	 */
+	timeleft += (((u64)wdt->base.timeout * USEC_PER_SEC) / 5) * (4 -
+expiration);
+
+	/*
Quoted 8 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 291

Quoted 6 lines
drivers/clocksource/timer-tegra186.c +6 -0
+	 * Convert the current counter value to seconds,
+	 * rounding up to the nearest second. Cast u64 to
+	 * u32 under the assumption that no overflow happens
+	 * when coverting to seconds.
+	 */
+	timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;

Did you check there is a macro fitting the need in math.h ?

Quoted 2 lines

I had a quick look, but looking again, I see we can use DIV_ROUND_CLOSEST_ULL() here.

I'll use the macro to refactor. Thanks.

Quoted 32 lines
Quoted 5 lines

drivers/clocksource/timer-tegra186.c static int tegra186_wdt_set_timeout(struct watchdog_device *wdd, · line 293

Quoted 3 lines
drivers/clocksource/timer-tegra186.c +1 -0
+	if (WARN_ON(timeleft > U32_MAX))

s/WARN_ON/pr_err/

Why? Again this is not expected. I think that this is fine.

Quoted 15 lines
Quoted 15 lines
drivers/clocksource/timer-tegra186.c +6 -0
+		return U32_MAX;
+
+	return lower_32_bits(timeleft);
+}
+
  static const struct watchdog_ops tegra186_wdt_ops = {
  	.owner = THIS_MODULE,
  	.start = tegra186_wdt_start,
  	.stop = tegra186_wdt_stop,
  	.ping = tegra186_wdt_ping,
  	.set_timeout = tegra186_wdt_set_timeout,
+	.get_timeleft = tegra186_wdt_get_timeleft,
  };
Quoted 6 lines
Quoted 5 lines

static struct tegra186_wdt *tegra186_wdt_create(struct
tegra186_timer *tegra,
--
2.34.1

--
nvpublic

#11 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

In reply to Robert Lin
kernel test robot noticed the following build errors:

Hi Robert,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/timers/core]
[also build test ERROR on linus/master v6.15-rc4]
[cannot apply to daniel-lezcano/clockevents/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Robert-Lin/clocksource-drivers-timer-tegra186-add-WDIOC_GETTIMELEFT-support/20250502-123914
base:   tip/timers/core
patch link:    https://lore.kernel.org/r/20250502043727.396896-2-robelin%40nvidia.com
patch subject: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support
config: arm-randconfig-002-20250502 (https://download.01.org/0day-ci/archive/20250502/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 7.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250502/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: drivers/clocksource/timer-tegra186.o: in function `tegra186_wdt_get_timeleft':

>> drivers/clocksource/timer-tegra186.c:282: undefined reference to `__aeabi_uldivmod'
>> arm-linux-gnueabi-ld: drivers/clocksource/timer-tegra186.c:290: undefined reference to `__aeabi_uldivmod'

vim +282 drivers/clocksource/timer-tegra186.c

241
242 static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
243 {
244 struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
245 u32 expiration, val;
246 u64 timeleft;
247
248 if (!watchdog_active(&wdt->base)) {
249 /* return zero if the watchdog timer is not activated. */
250 return 0;
251 }
252
253 /*
254 * Reset occurs on the fifth expiration of the
255 * watchdog timer and so when the watchdog timer is configured,
256 * the actual value programmed into the counter is 1/5 of the
257 * timeout value. Once the counter reaches 0, expiration count
258 * will be increased by 1 and the down counter restarts.
259 * Hence to get the time left before system reset we must
260 * combine 2 parts:
261 * 1. value of the current down counter
262 * 2. (number of counter expirations remaining) * (timeout/5)
263 */
264
265 /* Get the current number of counter expirations. Should be a
266 * value between 0 and 4
267 */
268 val = readl_relaxed(wdt->regs + WDTSR);
269 expiration = FIELD_GET(WDTSR_CURRENT_EXPIRATION_COUNT, val);
270 if (WARN_ON(expiration > 4))
271 return 0;
272
273 /* Get the current counter value in microsecond. */
274 val = readl_relaxed(wdt->tmr->regs + TMRSR);
275 timeleft = FIELD_GET(TMRSR_PCV, val);
276
277 /*
278 * Calculate the time remaining by adding the time for the
279 * counter value to the time of the counter expirations that
280 * remain.
281 */

 > 282		timeleft += (((u64)wdt->base.timeout * USEC_PER_SEC) / 5) * (4 - expiration);
   283	
   284		/*
   285		 * Convert the current counter value to seconds,
   286		 * rounding up to the nearest second. Cast u64 to
   287		 * u32 under the assumption that no overflow happens
   288		 * when coverting to seconds.
   289		 */
 > 290		timeleft = (timeleft + USEC_PER_SEC / 2) / USEC_PER_SEC;

291
292 if (WARN_ON(timeleft > U32_MAX))
293 return U32_MAX;
294
295 return lower_32_bits(timeleft);
296 }
297

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#12 lore raw

Re: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support

In reply to Robert Lin
kernel test robot noticed the following build errors:

Hi Robert,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/timers/core]
[also build test ERROR on linus/master v6.15-rc5 next-20250505] [cannot apply to daniel-lezcano/clockevents/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Robert-Lin/clocksource-drivers-timer-tegra186-add-WDIOC_GETTIMELEFT-support/20250502-123914
base:   tip/timers/core
patch link:    https://lore.kernel.org/r/20250502043727.396896-2-robelin%40nvidia.com
patch subject: [PATCH v7 1/3] clocksource/drivers/timer-tegra186: add WDIOC_GETTIMELEFT support
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20250506/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250506/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

m68k-linux-ld: drivers/clocksource/timer-tegra186.o: in function `tegra186_wdt_get_timeleft':

>> timer-tegra186.c:(.text+0x174): undefined reference to `__udivdi3'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#13 lore raw

PATCH v7 2/3 clocksource/drivers/timer-tegra186: fix watchdog self-pinging

In reply to Robert Lin

From: Pohsun Su <[email protected]>

This change removes watchdog self-pinging behavior.

The timer irq handler is triggered due to the 1st expiration, the handler disables and enables watchdog but also implicitly clears the expiration count so the count can only be 0 or 1.

Since this watchdog supports opened, configured, or pinged by systemd, We remove this behavior or the watchdog may not bark when systemd crashes since the 5th expiration never comes.

---
 drivers/clocksource/timer-tegra186.c | 27 ---------------------------
 1 file changed, 27 deletions(-)
drivers/clocksource/timer-tegra186.c +0 -27
index 8d5698770cbd..8c8d83ceb1dd 100644
@@ -175,9 +175,6 @@ static void tegra186_wdt_enable(struct tegra186_wdt *wdt)
 		value &= ~WDTCR_PERIOD_MASK;
 		value |= WDTCR_PERIOD(1);
 
-		/* enable local interrupt for WDT petting */
-		value |= WDTCR_LOCAL_INT_ENABLE;
-
 		/* enable local FIQ and remote interrupt for debug dump */
 		if (0)
 			value |= WDTCR_REMOTE_INT_ENABLE |
@@ -428,23 +425,10 @@ static int tegra186_timer_usec_init(struct tegra186_timer *tegra)
 	return clocksource_register_hz(&tegra->usec, USEC_PER_SEC);
 }
 
-static irqreturn_t tegra186_timer_irq(int irq, void *data)
-{
-	struct tegra186_timer *tegra = data;
-
-	if (watchdog_active(&tegra->wdt->base)) {
-		tegra186_wdt_disable(tegra->wdt);
-		tegra186_wdt_enable(tegra->wdt);
-	}
-
-	return IRQ_HANDLED;
-}
-
 static int tegra186_timer_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct tegra186_timer *tegra;
-	unsigned int irq;
 	int err;
 
 	tegra = devm_kzalloc(dev, sizeof(*tegra), GFP_KERNEL);
@@ -463,8 +447,6 @@ static int tegra186_timer_probe(struct platform_device *pdev)
 	if (err < 0)
 		return err;
 
-	irq = err;
-
 	/* create a watchdog using a preconfigured timer */
 	tegra->wdt = tegra186_wdt_create(tegra, 0);
 	if (IS_ERR(tegra->wdt)) {
@@ -491,17 +473,8 @@ static int tegra186_timer_probe(struct platform_device *pdev)
 		goto unregister_osc;
 	}
 
-	err = devm_request_irq(dev, irq, tegra186_timer_irq, 0,
-			       "tegra186-timer", tegra);
-	if (err < 0) {
-		dev_err(dev, "failed to request IRQ#%u: %d\n", irq, err);
-		goto unregister_usec;
-	}
-
 	return 0;
 
-unregister_usec:
-	clocksource_unregister(&tegra->usec);
 unregister_osc:
 	clocksource_unregister(&tegra->osc);
 unregister_tsc:
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH v7 3/3 clocksource/drivers/timer-tegra186: Remove unused bits

In reply to Robert Lin

From: robelin <[email protected]>

The intention to keep the unsed if(0) block is gone now. Remove them for clean codes.

---
 drivers/clocksource/timer-tegra186.c | 9 ---------
 1 file changed, 9 deletions(-)
drivers/clocksource/timer-tegra186.c +0 -9
index 8c8d83ceb1dd..afbf180c1baf 100644
@@ -175,15 +175,6 @@ static void tegra186_wdt_enable(struct tegra186_wdt *wdt)
 		value &= ~WDTCR_PERIOD_MASK;
 		value |= WDTCR_PERIOD(1);
 
-		/* enable local FIQ and remote interrupt for debug dump */
-		if (0)
-			value |= WDTCR_REMOTE_INT_ENABLE |
-				 WDTCR_LOCAL_FIQ_ENABLE;
-
-		/* enable system debug reset (doesn't properly reboot) */
-		if (0)
-			value |= WDTCR_SYSTEM_DEBUG_RESET_ENABLE;
-
 		/* enable system POR reset */
 		value |= WDTCR_SYSTEM_POR_RESET_ENABLE;
 
Signature · 1 line
2.34.1
#15 lore raw
In reply to Robert Lin
Quoted 6 lines · Robert Lin

From: robelin <[email protected]>

This set of patches includes a fix for watchdog for it may not bark due to self-pinging and adds WDIOC_GETTIMELEFT support.

--

When sending a new version, could you please Cc: the watchdog maintainers ?

Thanks
-- Daniel

Quoted 24 lines

V7:
- Fix formatting
- Consider overflow and warn if happens

V6:
- Fix timeleft value addition using unmatched time unit
- Use u64 type to maintain the microseconds value in case of overflow

V5:
- Print warning message if get unexpected value from the register

V4:
- Improve the precision of timeleft value
- Fix the unused variable warning

V3:
- Improve comment description
- Refactor to fit codeline within 80 columns
- Remove unused if(0) blocks

V2:
- Fix a compilation error, a warning and updates copyright --

[ ... ]

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog