Linux userland API discussions
 help / color / mirror / Atom feed
* [PATCH 0/3] hte, gpio: HTE timestamps in CLOCK_REALTIME on NVIDIA Tegra
@ 2026-11-30 14:24 Eli Gavril
  2026-11-30 14:24 ` [PATCH 1/3] hte: add CLOCK_REALTIME-correlated timestamp to hte_ts_data Eli Gavril
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eli Gavril @ 2026-11-30 14:24 UTC (permalink / raw)
  To: Dipen Patel, Linus Walleij, Bartosz Golaszewski
  Cc: Kent Gibson, Thierry Reding, Jonathan Hunter, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, timestamp, linux-gpio, linux-tegra,
	linux-doc, linux-api

HTE supports the NVIDIA Tegra Generic Timestamping Engine (GTE), but
only in the GTE's own counter time, not in CLOCK_REALTIME, so it cannot
be used as is to timestamp a GNSS PPS. NVIDIA instead uses its
out-of-tree nvpps driver [1], which reads the GTE through HTE but
reports through its own character device rather than the GPIO API. This
series adds CLOCK_REALTIME timestamps to HTE and the GPIO API.

Patch 1 adds a CLOCK_REALTIME-correlated time to struct hte_ts_data,
patch 2 fills it in the Tegra194 GTE driver, and patch 3 returns it when
user space requests HTE and REALTIME together, which is rejected today.
If a separate flag is preferred over combining the two, I can change it.

Tested on a Jetson Orin NX with a 1 Hz PPS for one hour, on 6.18 with
this series and eba302268a01 ("timekeeping: Provide
ktime_get_snapshot_id()"): no pulse was missed, and consecutive
intervals differed by at most one 32 ns GTE tick in 91% of cases and by
at most 96 ns overall. HTE alone, REALTIME alone and no clock flag
behave as before, both there and with gpio-sim in QEMU.

The series was developed with an LLM coding assistant (Claude Code,
model Claude Opus 5.5), which drafted the code and messages; I reviewed
and tested all of it.

[1] https://gitlab.com/nvidia/nv-tegra/linux-nv-oot/-/tree/jetson_39.2.2_GA/drivers/nvpps

Eli Gavril (3):
  hte: add CLOCK_REALTIME-correlated timestamp to hte_ts_data
  hte: tegra194: correlate captured timestamps to CLOCK_REALTIME
  gpiolib: cdev: report HTE timestamps in the CLOCK_REALTIME domain

 .../gpio/gpio-v2-get-line-ioctl.rst           |  3 ++-
 drivers/gpio/gpiolib-cdev.c                   | 21 ++++++++-----------
 drivers/hte/hte-tegra194.c                    | 12 ++++++++++-
 include/linux/hte.h                           |  3 +++
 include/uapi/linux/gpio.h                     |  4 +++-
 5 files changed, 28 insertions(+), 15 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] hte: add CLOCK_REALTIME-correlated timestamp to hte_ts_data
  2026-11-30 14:24 [PATCH 0/3] hte, gpio: HTE timestamps in CLOCK_REALTIME on NVIDIA Tegra Eli Gavril
@ 2026-11-30 14:24 ` Eli Gavril
  2026-11-30 14:24 ` [PATCH 2/3] hte: tegra194: correlate captured timestamps to CLOCK_REALTIME Eli Gavril
  2026-11-30 14:24 ` [PATCH 3/3] gpiolib: cdev: report HTE timestamps in the CLOCK_REALTIME domain Eli Gavril
  2 siblings, 0 replies; 4+ messages in thread
From: Eli Gavril @ 2026-11-30 14:24 UTC (permalink / raw)
  To: Dipen Patel, Linus Walleij, Bartosz Golaszewski
  Cc: Kent Gibson, Thierry Reding, Jonathan Hunter, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, timestamp, linux-gpio, linux-tegra,
	linux-doc, linux-api

HTE providers report timestamps in their own counter's epoch, which
consumers cannot convert to wall-clock time. Add an optional
realtime_ns field for the timestamp correlated to CLOCK_REALTIME;
providers that cannot provide it leave it 0.

struct hte_ts_data is allocated and used only inside the kernel, so
the user-space ABI does not change.

Signed-off-by: Eli Gavril <eli.gavril@gmail.com>
---
 include/linux/hte.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/linux/hte.h b/include/linux/hte.h
index 8289055061ab..076ad0a4ba84 100644
--- a/include/linux/hte.h
+++ b/include/linux/hte.h
@@ -44,11 +44,14 @@ enum hte_return {
  * @seq: Sequence counter of the timestamps.
  * @raw_level: Level of the line at the timestamp if provider supports it,
  * -1 otherwise.
+ * @realtime_ns: Event time correlated to CLOCK_REALTIME in nanoseconds if the
+ * provider supports it, 0 otherwise.
  */
 struct hte_ts_data {
 	u64 tsc;
 	u64 seq;
 	int raw_level;
+	u64 realtime_ns;
 };
 
 /**
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] hte: tegra194: correlate captured timestamps to CLOCK_REALTIME
  2026-11-30 14:24 [PATCH 0/3] hte, gpio: HTE timestamps in CLOCK_REALTIME on NVIDIA Tegra Eli Gavril
  2026-11-30 14:24 ` [PATCH 1/3] hte: add CLOCK_REALTIME-correlated timestamp to hte_ts_data Eli Gavril
@ 2026-11-30 14:24 ` Eli Gavril
  2026-11-30 14:24 ` [PATCH 3/3] gpiolib: cdev: report HTE timestamps in the CLOCK_REALTIME domain Eli Gavril
  2 siblings, 0 replies; 4+ messages in thread
From: Eli Gavril @ 2026-11-30 14:24 UTC (permalink / raw)
  To: Dipen Patel, Linus Walleij, Bartosz Golaszewski
  Cc: Kent Gibson, Thierry Reding, Jonathan Hunter, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, timestamp, linux-gpio, linux-tegra,
	linux-doc, linux-api

The GTE timestamps with the arch counter, which also backs the
arm_arch_timer clocksource. Take one ktime_get_snapshot_id() per FIFO
entry and subtract the time since the captured edge to fill in
realtime_ns. A single snapshot gives the counter value and
CLOCK_REALTIME from the same read, so no gap between two reads biases
the result.

Leave realtime_ns 0 when the snapshot is invalid or the arch timer is
not the clocksource, as its cycles are then in other units.

Signed-off-by: Eli Gavril <eli.gavril@gmail.com>
---
 drivers/hte/hte-tegra194.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/hte/hte-tegra194.c b/drivers/hte/hte-tegra194.c
index dea5f9e79005..72ae7bbbb295 100644
--- a/drivers/hte/hte-tegra194.c
+++ b/drivers/hte/hte-tegra194.c
@@ -14,6 +14,7 @@
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/hte.h>
+#include <linux/timekeeping.h>
 #include <linux/uaccess.h>
 #include <linux/gpio/driver.h>
 #include <linux/gpio/consumer.h>
@@ -722,6 +723,7 @@ static void tegra_hte_read_fifo(struct tegra_hte_soc *gs)
 	u32 tsh, tsl, src, pv, cv, acv, slice, bit_index, line_id;
 	u64 tsc;
 	u8 tsc_ns_shift;
+	struct system_time_snapshot snap;
 	struct hte_ts_data el;
 
 	tsc_ns_shift = __builtin_ctz(gs->prov_data->tsc_clkrate_ns);
@@ -739,10 +741,18 @@ static void tegra_hte_read_fifo(struct tegra_hte_soc *gs)
 		pv = tegra_hte_readl(gs, HTE_TEPCV);
 		cv = tegra_hte_readl(gs, HTE_TECCV);
 		acv = pv ^ cv;
+
+		el.tsc = tsc << tsc_ns_shift;
+		ktime_get_snapshot_id(CLOCK_REALTIME, &snap);
+		if (snap.valid && snap.cs_id == CSID_ARM_ARCH_COUNTER)
+			el.realtime_ns = ktime_to_ns(snap.systime) -
+				(((u64)snap.cycles << tsc_ns_shift) - el.tsc);
+		else
+			el.realtime_ns = 0;
+
 		while (acv) {
 			bit_index = __builtin_ctz(acv);
 			line_id = bit_index + (slice << 5);
-			el.tsc = tsc << tsc_ns_shift;
 			el.raw_level = tegra_hte_get_level(gs, line_id);
 			hte_push_ts_ns(gs->chip, line_id, &el);
 			acv &= ~BIT(bit_index);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] gpiolib: cdev: report HTE timestamps in the CLOCK_REALTIME domain
  2026-11-30 14:24 [PATCH 0/3] hte, gpio: HTE timestamps in CLOCK_REALTIME on NVIDIA Tegra Eli Gavril
  2026-11-30 14:24 ` [PATCH 1/3] hte: add CLOCK_REALTIME-correlated timestamp to hte_ts_data Eli Gavril
  2026-11-30 14:24 ` [PATCH 2/3] hte: tegra194: correlate captured timestamps to CLOCK_REALTIME Eli Gavril
@ 2026-11-30 14:24 ` Eli Gavril
  2 siblings, 0 replies; 4+ messages in thread
From: Eli Gavril @ 2026-11-30 14:24 UTC (permalink / raw)
  To: Dipen Patel, Linus Walleij, Bartosz Golaszewski
  Cc: Kent Gibson, Thierry Reding, Jonathan Hunter, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, timestamp, linux-gpio, linux-tegra,
	linux-doc, linux-api

HTE timestamps are in the provider's counter epoch, which user space
cannot convert to wall-clock time.

Requesting GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE together with
GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME is rejected with -EINVAL today.
Accept it and report the HTE timestamp correlated to CLOCK_REALTIME; a
provider that cannot correlate reports 0.

Signed-off-by: Eli Gavril <eli.gavril@gmail.com>
---
 .../gpio/gpio-v2-get-line-ioctl.rst           |  3 ++-
 drivers/gpio/gpiolib-cdev.c                   | 21 ++++++++-----------
 include/uapi/linux/gpio.h                     |  4 +++-
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/Documentation/userspace-api/gpio/gpio-v2-get-line-ioctl.rst b/Documentation/userspace-api/gpio/gpio-v2-get-line-ioctl.rst
index 6615d6ced755..b05c5b8f374e 100644
--- a/Documentation/userspace-api/gpio/gpio-v2-get-line-ioctl.rst
+++ b/Documentation/userspace-api/gpio/gpio-v2-get-line-ioctl.rst
@@ -77,7 +77,8 @@ The edge flags, ``GPIO_V2_LINE_FLAG_EDGE_xxx``, require
 and falling edges.  Requesting edge detection from a line that does not support
 it is an error (**ENXIO**).
 
-Only one event clock flag, ``GPIO_V2_LINE_FLAG_EVENT_CLOCK_xxx``, may be set.
+Only one event clock flag, ``GPIO_V2_LINE_FLAG_EVENT_CLOCK_xxx``, may be set,
+except HTE with REALTIME, which gives HTE timestamps in ``CLOCK_REALTIME``.
 If none are set then the event clock defaults to ``CLOCK_MONOTONIC``.
 The ``GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE`` flag requires supporting hardware
 and a kernel with ``CONFIG_HTE`` set.  Requesting HTE from a device that
diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index ceecce572a52..efec7fdb20ab 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -567,11 +567,11 @@ static void linereq_put_event(struct linereq *lr,
 
 static u64 line_event_timestamp(struct line *line)
 {
-	if (test_bit(GPIOD_FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
-		return ktime_get_real_ns();
-	else if (IS_ENABLED(CONFIG_HTE) &&
-		 test_bit(GPIOD_FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
+	if (IS_ENABLED(CONFIG_HTE) &&
+	    test_bit(GPIOD_FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
 		return line->timestamp_ns;
+	else if (test_bit(GPIOD_FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
+		return ktime_get_real_ns();
 
 	return ktime_get_ns();
 }
@@ -680,7 +680,10 @@ static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
 		return HTE_CB_HANDLED;
 
 	line = p;
-	line->timestamp_ns = ts->tsc;
+	if (test_bit(GPIOD_FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
+		line->timestamp_ns = ts->realtime_ns;
+	else
+		line->timestamp_ns = ts->tsc;
 	line->raw_level = ts->raw_level;
 	lr = line->req;
 
@@ -1176,12 +1179,6 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
 		return -EINVAL;
 
-	/* Only allow one event clock source */
-	if (IS_ENABLED(CONFIG_HTE) &&
-	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
-	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
-		return -EINVAL;
-
 	/* Edge detection requires explicit input. */
 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
@@ -2319,7 +2316,7 @@ static int gpio_desc_to_lineinfo(struct gpio_desc *desc,
 
 	if (test_bit(GPIOD_FLAG_EVENT_CLOCK_REALTIME, &dflags))
 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
-	else if (test_bit(GPIOD_FLAG_EVENT_CLOCK_HTE, &dflags))
+	if (test_bit(GPIOD_FLAG_EVENT_CLOCK_HTE, &dflags))
 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
 
 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h
index 5515b718886e..eef4e388b2ea 100644
--- a/include/uapi/linux/gpio.h
+++ b/include/uapi/linux/gpio.h
@@ -67,7 +67,9 @@ struct gpiochip_info {
  * @GPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabled
  * @GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME: line events contain REALTIME timestamps
  * @GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE: line events contain timestamps from
- * the hardware timestamping engine (HTE) subsystem
+ * the hardware timestamping engine (HTE) subsystem. If combined with
+ * GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME, the HTE timestamp is correlated
+ * to CLOCK_REALTIME when the provider supports it.
  */
 enum gpio_v2_line_flag {
 	GPIO_V2_LINE_FLAG_USED			= _BITULL(0),
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 14:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 14:24 [PATCH 0/3] hte, gpio: HTE timestamps in CLOCK_REALTIME on NVIDIA Tegra Eli Gavril
2026-11-30 14:24 ` [PATCH 1/3] hte: add CLOCK_REALTIME-correlated timestamp to hte_ts_data Eli Gavril
2026-11-30 14:24 ` [PATCH 2/3] hte: tegra194: correlate captured timestamps to CLOCK_REALTIME Eli Gavril
2026-11-30 14:24 ` [PATCH 3/3] gpiolib: cdev: report HTE timestamps in the CLOCK_REALTIME domain Eli Gavril

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox