Skip to contentSkip to conversation

clocksource: Add NVIDIA Tegra186 timers support

PATCH Superseded Thierry Reding 2 participants 14 messages Mar 31, 2020 – Apr 3, 2020

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover clocksource: Add NVIDIA Tegra186 timers support

From: Thierry Reding <[email protected]>

This set of patches adds the device tree bindings and a simplified driver for the timers found on Tegra186 and later SoCs. The driver itself currently only exposes a couple of clock sources and a watchdog, which in turn relies on the counter provided by one of the timers. The timers themselves are not used at the moment because the SoC also has an architected timer and there's currently no use for the additional timers provided by this hardware block.

I suggest that patches 1 & 2 go through the clocksource tree, while I can carry the DTS changes in the Tegra tree for v5.8.

Changes in v2:
- address review comments on the bindings
- make the driver buildable as a module
- support various clock sources

Thierry

Thierry Reding (7):
dt-bindings: timer: Add bindings for NVIDIA Tegra186 timers
clocksource: Add Tegra186 timers support
arm64: tegra: Order nodes by unit-address on Tegra194
arm64: tegra: Add native timer support on Tegra186
arm64: tegra: Enable native timers on Jetson TX2
arm64: tegra: Add native timer support on Tegra194
arm64: tegra: Enable native timers on Jetson AGX Xavier

 .../bindings/timer/nvidia,tegra186-timer.yaml |  61 +++
 .../arm64/boot/dts/nvidia/tegra186-p3310.dtsi |   4 +
 arch/arm64/boot/dts/nvidia/tegra186.dtsi      |  16 +
 .../arm64/boot/dts/nvidia/tegra194-p2888.dtsi |   4 +
 arch/arm64/boot/dts/nvidia/tegra194.dtsi      |  81 +--
 drivers/clocksource/Kconfig                   |   8 +
 drivers/clocksource/Makefile                  |   1 +
 drivers/clocksource/timer-tegra186.c          | 505 ++++++++++++++++++
 8 files changed, 648 insertions(+), 32 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/timer/nvidia,tegra186-timer.yaml
 create mode 100644 drivers/clocksource/timer-tegra186.c
Signature · 1 line
2.24.1
#1 lore raw

PATCH v2 1/7 dt-bindings: timer: Add bindings for NVIDIA Tegra186 timers

From: Thierry Reding <[email protected]>

The NVIDIA Tegra186 SoC contains an IP block that provides a register interface for ten timers with a 29-bit counter that can generate one- shot, periodic or watchdog interrupts.

---
Changes in v2:
- add required properties section
- add additionalProperties: false
- do not show status in example

 .../bindings/timer/nvidia,tegra186-timer.yaml | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/timer/nvidia,tegra186-timer.yaml
Documentation/devicetree/bindings/timer/nvidia,tegra186-timer.yaml new file +61 -0
new file mode 100644
index 000000000000..d722cd267bf9
@@ -0,0 +1,61 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/timer/nvidia,tegra186-timer.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NVIDIA Tegra186 timers
+
+maintainers:
+  - Thierry Reding <[email protected]>
+  - Jonathan Hunter <[email protected]>
+
+description: |
+  The Tegra186 timer provides ten 29-bit timer counters and one 32-bit TSC
+  (timestamp counter). The timers run at either a fixed 1 MHz clock rate
+  derived from the oscillator clock. Each timer can be programmed to raise
+  one-shot, periodic, or watchdog interrupts.
+
+properties:
+  compatible:
+    oneOf:
+      - description: NVIDIA Tegra186
+        items:
+          - const: nvidia,tegra186-timer
+
+      - description: NVIDIA Tegra194
+        items:
+          - const: nvidia,tegra194-timer
+          - const: nvidia,tegra186-timer
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 10
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    timer@3010000 {
+        compatible = "nvidia,tegra186-timer";
+        reg = <0x03010000 0x000e0000>;
+        interrupts = <GIC_SPI 0 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 5 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 7 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 8 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SPI 9 IRQ_TYPE_LEVEL_HIGH>;
+    };
Signature · 1 line
2.24.1
#2 lore raw

PATCH v2 2/7 clocksource: Add Tegra186 timers support

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

Currently this only supports a single watchdog, which uses a timer in the background for countdown. Eventually the timers could be used for various time-keeping tasks, but by default the architected timer will already provide that functionality.

---
Changes in v2:
- add dependency on WATCHDOG && WATCHDOG_CORE
- expose TSC, OSC and USEC clocksources
- implement suspend/resume support
- make driver tristate

 drivers/clocksource/Kconfig          |   8 +
 drivers/clocksource/Makefile         |   1 +
 drivers/clocksource/timer-tegra186.c | 505 +++++++++++++++++++++++++++
 3 files changed, 514 insertions(+)
 create mode 100644 drivers/clocksource/timer-tegra186.c
drivers/clocksource/Kconfig +8 -0
index f2142e6bbea3..385573c215d8 100644
@@ -149,6 +149,14 @@ config TEGRA_TIMER
 	help
 	  Enables support for the Tegra driver.
 
+config TEGRA186_TIMER
+	tristate "NVIDIA Tegra186 timer driver"
+	depends on ARCH_TEGRA || COMPILE_TEST
+	depends on WATCHDOG && WATCHDOG_CORE
+	help
+	  Enables support for the timers and watchdogs found on NVIDIA
+	  Tegra186 and later SoCs.
+
 config VT8500_TIMER
 	bool "VT8500 timer driver" if COMPILE_TEST
 	depends on HAS_IOMEM
drivers/clocksource/Makefile +1 -0
index 641ba5383ab5..ffa7950f4b7c 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_SUN4I_TIMER)	+= timer-sun4i.o
 obj-$(CONFIG_SUN5I_HSTIMER)	+= timer-sun5i.o
 obj-$(CONFIG_MESON6_TIMER)	+= timer-meson6.o
 obj-$(CONFIG_TEGRA_TIMER)	+= timer-tegra.o
+obj-$(CONFIG_TEGRA186_TIMER)	+= timer-tegra186.o
 obj-$(CONFIG_VT8500_TIMER)	+= timer-vt8500.o
 obj-$(CONFIG_NSPIRE_TIMER)	+= timer-zevio.o
 obj-$(CONFIG_BCM_KONA_TIMER)	+= bcm_kona_timer.o
drivers/clocksource/timer-tegra186.c new file +505 -0

505 lines not on this page · raw message

Signature · 1 line
2.24.1
#9 lore raw

PATCH v2 3/7 arm64: tegra: Order nodes by unit-address on Tegra194

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

The pin controller device tree node was accidentally added in the wrong place. Move it to the correct location to keep nodes ordered by unit- address.

---
 arch/arm64/boot/dts/nvidia/tegra194.dtsi | 64 ++++++++++++------------
 1 file changed, 32 insertions(+), 32 deletions(-)
arch/arm64/boot/dts/nvidia/tegra194.dtsi +32 -32
index f4ede86e32b4..019f66f03a97 100644
@@ -45,6 +45,38 @@ gpio: gpio@2200000 {
 			gpio-controller;
 		};
 
+		pinmux: pinmux@2430000 {
+			compatible = "nvidia,tegra194-pinmux";
+			reg = <0x2430000 0x17000
+			       0xc300000 0x4000>;
+
+			status = "okay";
+
+			pex_rst_c5_out_state: pex_rst_c5_out {
+				pex_rst {
+					nvidia,pins = "pex_l5_rst_n_pgg1";
+					nvidia,schmitt = <TEGRA_PIN_DISABLE>;
+					nvidia,lpdr = <TEGRA_PIN_ENABLE>;
+					nvidia,enable-input = <TEGRA_PIN_DISABLE>;
+					nvidia,io-high-voltage = <TEGRA_PIN_ENABLE>;
+					nvidia,tristate = <TEGRA_PIN_DISABLE>;
+					nvidia,pull = <TEGRA_PIN_PULL_NONE>;
+				};
+			};
+
+			clkreq_c5_bi_dir_state: clkreq_c5_bi_dir {
+				clkreq {
+					nvidia,pins = "pex_l5_clkreq_n_pgg0";
+					nvidia,schmitt = <TEGRA_PIN_DISABLE>;
+					nvidia,lpdr = <TEGRA_PIN_ENABLE>;
+					nvidia,enable-input = <TEGRA_PIN_ENABLE>;
+					nvidia,io-high-voltage = <TEGRA_PIN_ENABLE>;
+					nvidia,tristate = <TEGRA_PIN_DISABLE>;
+					nvidia,pull = <TEGRA_PIN_PULL_NONE>;
+				};
+			};
+		};
+
 		ethernet@2490000 {
 			compatible = "nvidia,tegra194-eqos",
 				     "nvidia,tegra186-eqos",
@@ -139,38 +171,6 @@ agic: interrupt-controller@2a40000 {
 			};
 		};
 
-		pinmux: pinmux@2430000 {
-			compatible = "nvidia,tegra194-pinmux";
-			reg = <0x2430000 0x17000
-			       0xc300000 0x4000>;
-
-			status = "okay";
-
-			pex_rst_c5_out_state: pex_rst_c5_out {
-				pex_rst {
-					nvidia,pins = "pex_l5_rst_n_pgg1";
-					nvidia,schmitt = <TEGRA_PIN_DISABLE>;
-					nvidia,lpdr = <TEGRA_PIN_ENABLE>;
-					nvidia,enable-input = <TEGRA_PIN_DISABLE>;
-					nvidia,io-high-voltage = <TEGRA_PIN_ENABLE>;
-					nvidia,tristate = <TEGRA_PIN_DISABLE>;
-					nvidia,pull = <TEGRA_PIN_PULL_NONE>;
-				};
-			};
-
-			clkreq_c5_bi_dir_state: clkreq_c5_bi_dir {
-				clkreq {
-					nvidia,pins = "pex_l5_clkreq_n_pgg0";
-					nvidia,schmitt = <TEGRA_PIN_DISABLE>;
-					nvidia,lpdr = <TEGRA_PIN_ENABLE>;
-					nvidia,enable-input = <TEGRA_PIN_ENABLE>;
-					nvidia,io-high-voltage = <TEGRA_PIN_ENABLE>;
-					nvidia,tristate = <TEGRA_PIN_DISABLE>;
-					nvidia,pull = <TEGRA_PIN_PULL_NONE>;
-				};
-			};
-		};
-
 		mc: memory-controller@2c00000 {
 			compatible = "nvidia,tegra194-mc";
 			reg = <0x02c00000 0x100000>,
Signature · 1 line
2.24.1
#10 lore raw

PATCH v2 4/7 arm64: tegra: Add native timer support on Tegra186

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

The native timers IP block found on NVIDIA Tegra SoCs implements a watchdog timer that can be used to recover from system hangs. Add the device tree node on Tegra186.

---
 arch/arm64/boot/dts/nvidia/tegra186.dtsi | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/arm64/boot/dts/nvidia/tegra186.dtsi +16 -0
index 58100fb9cd8b..4dfa70e93693 100644
@@ -167,6 +167,22 @@ emc: external-memory-controller@2c60000 {
 		};
 	};
 
+	timer@3010000 {
+		compatible = "nvidia,tegra186-timer";
+		reg = <0x0 0x03010000 0x0 0x000e0000>;
+		interrupts = <GIC_SPI 0 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 5 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 7 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 8 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 9 IRQ_TYPE_LEVEL_HIGH>;
+		status = "disabled";
+	};
+
 	uarta: serial@3100000 {
 		compatible = "nvidia,tegra186-uart", "nvidia,tegra20-uart";
 		reg = <0x0 0x03100000 0x0 0x40>;
Signature · 1 line
2.24.1
#11 lore raw

PATCH v2 5/7 arm64: tegra: Enable native timers on Jetson TX2

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

Enable the native timers on Jetson TX2 to allow using the watchdog functionality to recover from system hangs, for example.

---
 arch/arm64/boot/dts/nvidia/tegra186-p3310.dtsi | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/boot/dts/nvidia/tegra186-p3310.dtsi +4 -0
index da96de04d003..9aa17744c4a0 100644
@@ -58,6 +58,10 @@ memory-controller@2c00000 {
 		status = "okay";
 	};
 
+	timer@3010000 {
+		status = "okay";
+	};
+
 	serial@3100000 {
 		status = "okay";
 	};
Signature · 1 line
2.24.1
#12 lore raw

PATCH v2 6/7 arm64: tegra: Add native timer support on Tegra194

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

The native timers IP block found on NVIDIA Tegra SoCs implements a watchdog timer that can be used to recover from system hangs. Add the device tree node on Tegra194.

---
 arch/arm64/boot/dts/nvidia/tegra194.dtsi | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/arm64/boot/dts/nvidia/tegra194.dtsi +17 -0
index 019f66f03a97..a0a5b44ff9bb 100644
@@ -213,6 +213,23 @@ emc: external-memory-controller@2c60000 {
 			};
 		};
 
+		timer@3010000 {
+			compatible = "nvidia,tegra194-timer",
+				     "nvidia,tegra186-timer";
+			reg = <0x03010000 0x000e0000>;
+			interrupts = <GIC_SPI 0 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 5 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 7 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 8 IRQ_TYPE_LEVEL_HIGH>,
+				     <GIC_SPI 9 IRQ_TYPE_LEVEL_HIGH>;
+			status = "disabled";
+		};
+
 		uarta: serial@3100000 {
 			compatible = "nvidia,tegra194-uart", "nvidia,tegra20-uart";
 			reg = <0x03100000 0x40>;
Signature · 1 line
2.24.1
#13 lore raw

PATCH v2 7/7 arm64: tegra: Enable native timers on Jetson AGX Xavier

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

Enable the native timers on Jetson AGX Xavier to allow using the watchdog functionality to recover from system hangs, for example.

---
 arch/arm64/boot/dts/nvidia/tegra194-p2888.dtsi | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/boot/dts/nvidia/tegra194-p2888.dtsi +4 -0
index 623f7d7d216b..d68588f2709e 100644
@@ -52,6 +52,10 @@ memory-controller@2c00000 {
 			status = "okay";
 		};
 
+		timer@3010000 {
+			status = "okay";
+		};
+
 		serial@3110000 {
 			status = "okay";
 		};
Signature · 1 line
2.24.1
#3 lore raw

Re: [PATCH v2 2/7] clocksource: Add Tegra186 timers support

In reply to Thierry Reding

01.04.2020 01:19, Thierry Reding пишет:
...

drivers/clocksource/timer-tegra186.c line 97

Quoted 3 lines
drivers/clocksource/timer-tegra186.c new file +3 -0
+static void tmr_writel(struct tegra186_tmr *tmr, u32 value, unsigned int offset)
+{
+	writel(value, tmr->regs + offset);

relaxed?

drivers/clocksource/timer-tegra186.c line 102

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+}
+
+static void wdt_writel(struct tegra186_wdt *wdt, u32 value, unsigned int offset)
+{
+	writel(value, wdt->regs + offset);

relaxed?

drivers/clocksource/timer-tegra186.c line 107

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+}
+
+static u32 wdt_readl(struct tegra186_wdt *wdt, unsigned int offset)
+{
+	return readl(wdt->regs + offset);

relaxed?

Quoted 1 line

+}

...

drivers/clocksource/timer-tegra186.c line 370

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+static irqreturn_t tegra186_timer_irq(int irq, void *data)
+{
+	struct tegra186_timer *tegra = data;
+
+	if (tegra->wdt) {

Why this check is needed? Please see more below in regards to devm_request_irq().

Quoted 18 lines
drivers/clocksource/timer-tegra186.c new file +18 -0
+		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;
+	int err;
+
+	tegra = devm_kzalloc(dev, sizeof(*tegra), GFP_KERNEL);
+	if (!tegra)
+		return -ENOMEM;
+
+	tegra->soc = of_device_get_match_data(dev);

drivers/clocksource/timer-tegra186.c line 398

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+	dev_set_drvdata(dev, tegra);
+	tegra->dev = dev;
+
+	tegra->regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tegra->regs))
+		return PTR_ERR(tegra->regs);
+
+	err = platform_get_irq(pdev, 0);
+	if (err < 0) {
+		dev_err(dev, "failed to get interrupt #0: %d\n", err);

Duplicated error message isn't needed for platform_get_irq().

drivers/clocksource/timer-tegra186.c line 405

Quoted 7 lines
drivers/clocksource/timer-tegra186.c new file +7 -0
+		return err;
+	}
+
+	tegra->irq = err;
+
+	err = devm_request_irq(dev, tegra->irq, tegra186_timer_irq,
+			       IRQF_ONESHOT | IRQF_TRIGGER_HIGH,

Why IRQF_ONESHOT?

And IRQF_TRIGGER_HIGH?.. the interrupt-level should come from the device-tree.

drivers/clocksource/timer-tegra186.c line 410

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+			       "tegra186-timer", tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to request IRQ#%u: %d\n", tegra->irq, err);
+		return err;
+	}

Interrupt should be requested at the end of tegra186_timer_probe(), otherwise probe order isn't correct, leading to a potential race conditions.

Quoted 22 lines
drivers/clocksource/timer-tegra186.c new file +22 -0
+	/* create a watchdog using a preconfigured timer */
+	tegra->wdt = tegra186_wdt_create(tegra, 0);
+	if (IS_ERR(tegra->wdt)) {
+		err = PTR_ERR(tegra->wdt);
+		dev_err(dev, "failed to create WDT: %d\n", err);
+		return err;
+	}
+
+	err = tegra186_timer_tsc_init(tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to register TSC counter: %d\n", err);
+		return err;
+	}
+
+	err = tegra186_timer_osc_init(tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to register OSC counter: %d\n", err);
+		goto unregister_tsc;
+	}
+
+	err = tegra186_timer_usec_init(tegra);
+	if (err < 0) {

drivers/clocksource/timer-tegra186.c line 443

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+		dev_err(dev, "failed to register USEC counter: %d\n", err);
+		goto unregister_osc;
+	}
+
+	return 0;
+
+unregister_osc:
+	clocksource_unregister(&tegra->osc);
+unregister_tsc:
+	clocksource_unregister(&tegra->tsc);

Looks like there is an opportunity for devm_clocksource_register_hz().

Quoted 19 lines
drivers/clocksource/timer-tegra186.c new file +19 -0
+	return err;
+}
+
+static int tegra186_timer_remove(struct platform_device *pdev)
+{
+	struct tegra186_timer *tegra = platform_get_drvdata(pdev);
+
+	clocksource_unregister(&tegra->usec);
+	clocksource_unregister(&tegra->osc);
+	clocksource_unregister(&tegra->tsc);
+
+	return 0;
+}
+
+static int __maybe_unused tegra186_timer_suspend(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)

drivers/clocksource/timer-tegra186.c line 472

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+		tegra186_wdt_disable(tegra->wdt);
+
+	return 0;
+}
+
+static int __maybe_unused tegra186_timer_resume(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)

Could tegra->wdt ever be NULL?

Quoted 4 lines
drivers/clocksource/timer-tegra186.c new file +4 -0
+		tegra186_wdt_enable(tegra->wdt);
+
+	return 0;
+}
#5 lore raw

Re: [PATCH v2 2/7] clocksource: Add Tegra186 timers support

In reply to Thierry Reding

01.04.2020 01:19, Thierry Reding пишет:
...

drivers/clocksource/timer-tegra186.c line 230

Quoted 8 lines
drivers/clocksource/timer-tegra186.c new file +8 -0
+static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
+				    unsigned int timeout)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+
+	tegra186_wdt_disable(wdt);
+	wdt->base.timeout = timeout;
+	tegra186_wdt_enable(wdt);

Why changing timeout enables the watchdog?

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+	return 0;
+}
+
+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,
+};

...

Quoted 6 lines
drivers/clocksource/timer-tegra186.c new file +6 -0
+static int __maybe_unused tegra186_timer_suspend(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)
+		tegra186_wdt_disable(tegra->wdt);

drivers/clocksource/timer-tegra186.c line 473

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+
+	return 0;
+}
+
+static int __maybe_unused tegra186_timer_resume(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)
+		tegra186_wdt_enable(tegra->wdt);

What if watchdog is in a stopped state? Why it's enabled unconditionally?

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}
#7 lore raw

Re: [PATCH v2 2/7] clocksource: Add Tegra186 timers support

In reply to Thierry Reding

01.04.2020 01:19, Thierry Reding пишет:
...

Quoted 19 lines
drivers/clocksource/timer-tegra186.c new file +19 -0
+static int tegra186_timer_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct tegra186_timer *tegra;
+	int err;
+
+	tegra = devm_kzalloc(dev, sizeof(*tegra), GFP_KERNEL);
+	if (!tegra)
+		return -ENOMEM;
+
+	tegra->soc = of_device_get_match_data(dev);
+	dev_set_drvdata(dev, tegra);
+	tegra->dev = dev;
+
+	tegra->regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tegra->regs))
+		return PTR_ERR(tegra->regs);
+
+	err = platform_get_irq(pdev, 0);

drivers/clocksource/timer-tegra186.c line 406

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+	if (err < 0) {
+		dev_err(dev, "failed to get interrupt #0: %d\n", err);
+		return err;
+	}
+
+	tegra->irq = err;
+
+	err = devm_request_irq(dev, tegra->irq, tegra186_timer_irq,
+			       IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
+			       "tegra186-timer", tegra);

Looks like there is no need to store tegra->irq in the struct tegra186_timer.

#4 lore raw

Re: [PATCH v2 2/7] clocksource: Add Tegra186 timers support

In reply to Dmitry Osipenko
Quoted 16 lines · Dmitry Osipenko

01.04.2020 01:19, Thierry Reding пишет:
...

drivers/clocksource/timer-tegra186.c line 97

Quoted 3 lines
drivers/clocksource/timer-tegra186.c new file +3 -0
+static void tmr_writel(struct tegra186_tmr *tmr, u32 value, unsigned int offset)
+{
+	writel(value, tmr->regs + offset);

relaxed?

drivers/clocksource/timer-tegra186.c line 102

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+}
+
+static void wdt_writel(struct tegra186_wdt *wdt, u32 value, unsigned int offset)
+{
+	writel(value, wdt->regs + offset);

relaxed?

Quoted 7 lines

drivers/clocksource/timer-tegra186.c line 107

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+}
+
+static u32 wdt_readl(struct tegra186_wdt *wdt, unsigned int offset)
+{
+	return readl(wdt->regs + offset);

relaxed?

Done.

Quoted 4 lines
Quoted 2 lines

+}

...

Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 370

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+static irqreturn_t tegra186_timer_irq(int irq, void *data)
+{
+	struct tegra186_timer *tegra = data;
+
+	if (tegra->wdt) {

Why this check is needed? Please see more below in regards to devm_request_irq().

We don't need it. However, I've changed all of these instances to watchdog_active() calls instead to make sure we only ping the WDT when we need to.

Quoted 23 lines
Quoted 23 lines
drivers/clocksource/timer-tegra186.c new file +22 -0
+		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;
+	int err;
+
+	tegra = devm_kzalloc(dev, sizeof(*tegra), GFP_KERNEL);
+	if (!tegra)
+		return -ENOMEM;
+
+	tegra->soc = of_device_get_match_data(dev);
+	dev_set_drvdata(dev, tegra);
+	tegra->dev = dev;
+
+	tegra->regs = devm_platform_ioremap_resource(pdev, 0);
Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 398

Quoted 6 lines
drivers/clocksource/timer-tegra186.c new file +6 -0
+	if (IS_ERR(tegra->regs))
+		return PTR_ERR(tegra->regs);
+
+	err = platform_get_irq(pdev, 0);
+	if (err < 0) {
+		dev_err(dev, "failed to get interrupt #0: %d\n", err);

Duplicated error message isn't needed for platform_get_irq().

Dropped.

Quoted 10 lines

drivers/clocksource/timer-tegra186.c line 405

Quoted 7 lines
drivers/clocksource/timer-tegra186.c new file +7 -0
+		return err;
+	}
+
+	tegra->irq = err;
+
+	err = devm_request_irq(dev, tegra->irq, tegra186_timer_irq,
+			       IRQF_ONESHOT | IRQF_TRIGGER_HIGH,

Why IRQF_ONESHOT?

Quoted 2 lines

And IRQF_TRIGGER_HIGH?.. the interrupt-level should come from the device-tree.

Yeah, I don't know how I came up with these. Probably copied them from somewhere else. I've dropped both of these since they aren't needed.

Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 410

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+			       "tegra186-timer", tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to request IRQ#%u: %d\n", tegra->irq, err);
+		return err;
+	}

Interrupt should be requested at the end of tegra186_timer_probe(), otherwise probe order isn't correct, leading to a potential race conditions.

I don't think there's an actual issue here because the watchdog that is initialized below is disabled by default and won't be enabled until the userspace explicitly asks it to. Since the watchdog is the only one to currently generate an interrupt, this should be fine.

That said, you're right and it's safer to initialize the interrupt as late as possible, so I've moved this to the end of the function.

Quoted 21 lines
Quoted 21 lines
drivers/clocksource/timer-tegra186.c new file +20 -0
+	/* create a watchdog using a preconfigured timer */
+	tegra->wdt = tegra186_wdt_create(tegra, 0);
+	if (IS_ERR(tegra->wdt)) {
+		err = PTR_ERR(tegra->wdt);
+		dev_err(dev, "failed to create WDT: %d\n", err);
+		return err;
+	}
+
+	err = tegra186_timer_tsc_init(tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to register TSC counter: %d\n", err);
+		return err;
+	}
+
+	err = tegra186_timer_osc_init(tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to register OSC counter: %d\n", err);
+		goto unregister_tsc;
+	}
+
Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 439

Quoted 8 lines
drivers/clocksource/timer-tegra186.c new file +8 -0
+	err = tegra186_timer_usec_init(tegra);
+	if (err < 0) {
+		dev_err(dev, "failed to register USEC counter: %d\n", err);
+		goto unregister_osc;
+	}
+
+	return 0;
+

And added an unregister_usec: label here to clean up the USEC clocksource if we fail to request the IRQ.

Quoted 6 lines

drivers/clocksource/timer-tegra186.c line 443

Quoted 4 lines
drivers/clocksource/timer-tegra186.c new file +4 -0
+unregister_osc:
+	clocksource_unregister(&tegra->osc);
+unregister_tsc:
+	clocksource_unregister(&tegra->tsc);

Looks like there is an opportunity for devm_clocksource_register_hz().

Yeah, I guess I could follow up with a patch to do that. I suspect that there's no such implementation because very few drivers actually end up unregistering their clocksources. A quick grep shows that only about one fifth of the users unregister the clocksource.

If Daniel and Thomas think this is a good idea I can look at adding that and converting some of the users.

Quoted 23 lines
Quoted 23 lines
drivers/clocksource/timer-tegra186.c new file +23 -0
+	return err;
+}
+
+static int tegra186_timer_remove(struct platform_device *pdev)
+{
+	struct tegra186_timer *tegra = platform_get_drvdata(pdev);
+
+	clocksource_unregister(&tegra->usec);
+	clocksource_unregister(&tegra->osc);
+	clocksource_unregister(&tegra->tsc);
+
+	return 0;
+}
+
+static int __maybe_unused tegra186_timer_suspend(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)
+		tegra186_wdt_disable(tegra->wdt);
+
+	return 0;
+}
Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 472

Quoted 6 lines
drivers/clocksource/timer-tegra186.c new file +6 -0
+
+static int __maybe_unused tegra186_timer_resume(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)

Could tegra->wdt ever be NULL?

No, it can't. But as above, I've used watchdog_active() here to make sure we only enable the watchdog when we should.

Thierry

#6 lore raw

Re: [PATCH v2 2/7] clocksource: Add Tegra186 timers support

In reply to Dmitry Osipenko
Quoted 12 lines · Dmitry Osipenko

01.04.2020 01:19, Thierry Reding пишет:
...

drivers/clocksource/timer-tegra186.c line 230

Quoted 8 lines
drivers/clocksource/timer-tegra186.c new file +8 -0
+static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
+				    unsigned int timeout)
+{
+	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
+
+	tegra186_wdt_disable(wdt);
+	wdt->base.timeout = timeout;
+	tegra186_wdt_enable(wdt);

Why changing timeout enables the watchdog?

We have to disable and enable to make it accept the new settings and to restart the counter. But you're right, we should only do that when the WDT is actually active, so I've conditionalized the disable/enable calls on watchdog_active().

Quoted 23 lines
Quoted 11 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+	return 0;
+}
+
+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,
+};

...

Quoted 10 lines
drivers/clocksource/timer-tegra186.c new file +10 -0
+static int __maybe_unused tegra186_timer_suspend(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)
+		tegra186_wdt_disable(tegra->wdt);
+
+	return 0;
+}
+
Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 473

Quoted 6 lines
drivers/clocksource/timer-tegra186.c new file +6 -0
+static int __maybe_unused tegra186_timer_resume(struct device *dev)
+{
+	struct tegra186_timer *tegra = dev_get_drvdata(dev);
+
+	if (tegra->wdt)
+		tegra186_wdt_enable(tegra->wdt);

What if watchdog is in a stopped state? Why it's enabled unconditionally?

Same as above.

Thierry

#8 lore raw

Re: [PATCH v2 2/7] clocksource: Add Tegra186 timers support

In reply to Dmitry Osipenko
Quoted 26 lines · Dmitry Osipenko

01.04.2020 01:19, Thierry Reding пишет:
...

Quoted 24 lines
drivers/clocksource/timer-tegra186.c new file +24 -0
+static int tegra186_timer_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct tegra186_timer *tegra;
+	int err;
+
+	tegra = devm_kzalloc(dev, sizeof(*tegra), GFP_KERNEL);
+	if (!tegra)
+		return -ENOMEM;
+
+	tegra->soc = of_device_get_match_data(dev);
+	dev_set_drvdata(dev, tegra);
+	tegra->dev = dev;
+
+	tegra->regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tegra->regs))
+		return PTR_ERR(tegra->regs);
+
+	err = platform_get_irq(pdev, 0);
+	if (err < 0) {
+		dev_err(dev, "failed to get interrupt #0: %d\n", err);
+		return err;
+	}
+
Quoted 8 lines

drivers/clocksource/timer-tegra186.c line 406

Quoted 5 lines
drivers/clocksource/timer-tegra186.c new file +5 -0
+	tegra->irq = err;
+
+	err = devm_request_irq(dev, tegra->irq, tegra186_timer_irq,
+			       IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
+			       "tegra186-timer", tegra);

Looks like there is no need to store tegra->irq in the struct tegra186_timer.

After moving devm_request_irq() to a later point in ->probe() we now have to store it somewhere because err is being reused. But I can store it in a local variable instead.

Thierry