netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: netdev@vger.kernel.org,
	"Richard Cochran" <richardcochran@gmail.com>,
	"Christopher Hall" <christopher.s.hall@intel.com>,
	"David Zage" <david.zage@intel.com>,
	"John Stultz" <jstultz@google.com>,
	"Frederic Weisbecker" <frederic@kernel.org>,
	"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
	"Miroslav Lichvar" <mlichvar@redhat.com>,
	"Werner Abt" <werner.abt@meinberg-usa.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	"Kurt Kanzenbach" <kurt@linutronix.de>,
	"Nam Cao" <namcao@linutronix.de>,
	"Alex Gieringer" <gieri@linutronix.de>
Subject: [patch 17/26] timekeeping: Provide time getters for PTP clocks
Date: Tue, 13 May 2025 17:13:26 +0200 (CEST)	[thread overview]
Message-ID: <20250513145137.681496427@linutronix.de> (raw)
In-Reply-To: 20250513144615.252881431@linutronix.de

Provide interfaces similar to the ktime_get*() family which provide access
to the independent PTP clocks.

These interfaces have a boolean return value, which indicates whether the
accessed clock is valid or not.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 include/linux/timekeeping.h |   17 ++++++++++++
 kernel/time/timekeeping.c   |   62 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)
---
--- a/include/linux/timekeeping.h
+++ b/include/linux/timekeeping.h
@@ -60,6 +60,17 @@ extern time64_t __ktime_get_real_seconds
 extern time64_t ktime_get_real_seconds(void);
 
 /*
+ * PTP clock interfaces
+ */
+#ifdef CONFIG_POSIX_PTP_CLOCKS
+extern bool ktime_get_ptp(clockid_t id, ktime_t *kt);
+extern bool ktime_get_ptp_ts64(clockid_t id, struct timespec64 *kt);
+#else
+static inline bool ktime_get_ptp(clockid_t id, ktime_t *kt) { return false; }
+static inline bool ktime_get_ptp_ts64(clockid_t id, struct timespec64 *kt) { return false; }
+#endif
+
+/*
  * ktime_t based interfaces
  */
 
@@ -263,6 +274,12 @@ extern bool timekeeping_rtc_skipresume(v
 
 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
 
+/*
+ * PTP clocks
+ */
+bool ktime_get_ptp(clockid_t ptp_clock_id, ktime_t *ts);
+bool ktime_get_ptp_ts64(clockid_t ptp_clock_id, struct timespec64 *ts);
+
 /**
  * struct system_time_snapshot - simultaneous raw/real time capture with
  *				 counter value
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -2661,6 +2661,23 @@ EXPORT_SYMBOL(hardpps);
 /* Bitmap for the activated PTP timekeepers */
 static unsigned long ptp_timekeepers;
 
+static inline bool ptp_valid_clockid(clockid_t id)
+{
+	return id >= CLOCK_PTP && id <= CLOCK_PTP_LAST;
+}
+
+static inline unsigned int clockid_to_tkid(unsigned int id)
+{
+	return TIMEKEEPER_PTP + id - CLOCK_PTP;
+}
+
+static inline struct tk_data *ptp_get_tk_data(clockid_t id)
+{
+	if (!ptp_valid_clockid(id))
+		return NULL;
+	return &timekeeper_data[clockid_to_tkid(id)];
+}
+
 /* Invoked from timekeeping after a clocksource change */
 static void tk_ptp_update_clocksource(void)
 {
@@ -2681,6 +2698,51 @@ static void tk_ptp_update_clocksource(vo
 	}
 }
 
+/**
+ * ktime_get_ptp - Get TAI time for a PTP clock
+ * @id:	ID of the clock to read (CLOCK_PTP...)
+ * @kt:	Pointer to ktime_t to store the time stamp
+ *
+ * Returns: True if the timestamp is valid, false otherwise
+ */
+bool ktime_get_ptp(clockid_t id, ktime_t *kt)
+{
+	struct tk_data *tkd = ptp_get_tk_data(id);
+	struct timekeeper *tk;
+	unsigned int seq;
+	ktime_t base;
+	u64 nsecs;
+
+	WARN_ON(timekeeping_suspended);
+
+	if (!tkd)
+		return false;
+
+	tk = &tkd->timekeeper;
+	do {
+		seq = read_seqcount_begin(&tkd->seq);
+		if (!tk->clock_valid)
+			return false;
+
+		base = ktime_add(tk->tkr_mono.base, tk->offs_ptp);
+		nsecs = timekeeping_get_ns(&tk->tkr_mono);
+	} while (read_seqcount_retry(&tkd->seq, seq));
+
+	*kt = ktime_add_ns(base, nsecs);
+	return true;
+}
+EXPORT_SYMBOL_GPL(ktime_get_ptp);
+
+bool ktime_get_ptp_ts64(clockid_t id, struct timespec64 *ts)
+{
+	ktime_t now;
+
+	if (!ktime_get_ptp(id, &now))
+		return false;
+	*ts = ktime_to_timespec64(now);
+	return true;
+}
+
 static __init void tk_ptp_setup(void)
 {
 	for (int i = TIMEKEEPER_PTP; i <= TIMEKEEPER_PTP_LAST; i++)


  parent reply	other threads:[~2025-05-13 15:13 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-13 15:12 [patch 00/26] timekeeping: Provide support for independent PTP timekeepers Thomas Gleixner
2025-05-13 15:12 ` [patch 01/26] timekeeping: Remove hardcoded access to tk_core Thomas Gleixner
2025-05-13 15:12 ` [patch 02/26] timekeeping: Cleanup kernel doc of __ktime_get_real_seconds() Thomas Gleixner
2025-05-13 15:13 ` [patch 03/26] timekeeping: Avoid double notification in do_adjtimex() Thomas Gleixner
2025-05-13 15:13 ` [patch 04/26] timekeeping: Introduce timekeeper ID Thomas Gleixner
2025-05-13 15:13 ` [patch 05/26] time: Introduce PTP clocks Thomas Gleixner
2025-05-13 15:13 ` [patch 06/26] ntp: Add support for PTP timekeepers Thomas Gleixner
2025-05-13 15:13 ` [patch 07/26] ntp: Add timekeeper ID arguments to public functions Thomas Gleixner
2025-05-13 15:13 ` [patch 08/26] ntp: Rename __do_adjtimex() to ntp_adjtimex() Thomas Gleixner
2025-05-13 15:13 ` [patch 09/26] timekeeping: Make __timekeeping_advance() reusable Thomas Gleixner
2025-05-13 15:13 ` [patch 10/26] timekeeping: Prepare timekeeping_update_from_shadow() Thomas Gleixner
2025-05-13 15:13 ` [patch 11/26] timekeeping: Add clock_valid flag to timekeeper Thomas Gleixner
2025-05-13 15:13 ` [patch 12/26] timekeeping: Introduce PTP time keepers Thomas Gleixner
2025-05-13 15:13 ` [patch 13/26] timekeeping: Provide ktime_get_ntp_seconds() Thomas Gleixner
2025-05-13 15:13 ` [patch 14/26] ntp: Use ktime_get_ntp_seconds() Thomas Gleixner
2025-05-13 15:13 ` [patch 15/26] timekeeping: Add PTP offset to timekeeper Thomas Gleixner
2025-05-13 15:13 ` [patch 16/26] timekeeping: Update PTP timekeepers on clocksource change Thomas Gleixner
2025-05-13 15:13 ` Thomas Gleixner [this message]
2025-05-13 15:13 ` [patch 18/26] timekeeping: Add minimal posix-timers support for PTP clocks Thomas Gleixner
2025-05-13 15:13 ` [patch 19/26] timekeeping: Provide time setter " Thomas Gleixner
2025-05-13 15:13 ` [patch 20/26] timekeeping: Make timekeeping_inject_offset() reusable Thomas Gleixner
2025-05-13 15:13 ` [patch 21/26] timekeeping: Add PTP clock support to __timekeeping_inject_offset() Thomas Gleixner
2025-05-13 15:13 ` [patch 22/26] timekeeping: Make do_adjtimex() reusable Thomas Gleixner
2025-05-13 15:13 ` [patch 23/26] timekeeping: Prepare do_adtimex() for PTP clocks Thomas Gleixner
2025-05-13 15:13 ` [patch 24/26] timekeeping: Provide adjtimex() " Thomas Gleixner
2025-05-13 15:13 ` [patch 25/26] timekeeping: Provide update for PTP timekeepers Thomas Gleixner
2025-05-13 15:13 ` [patch 26/26] timekeeping: Provide interface to control independent PTP clocks Thomas Gleixner
2025-05-14  8:07   ` Antoine Tenart
2025-05-14  8:37     ` Thomas Gleixner
2025-05-14  7:12 ` [patch 00/26] timekeeping: Provide support for independent PTP timekeepers Miroslav Lichvar
2025-05-14  8:54   ` Thomas Gleixner
2025-05-14 15:58     ` Richard Cochran

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250513145137.681496427@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=anna-maria@linutronix.de \
    --cc=christopher.s.hall@intel.com \
    --cc=david.zage@intel.com \
    --cc=dwmw2@infradead.org \
    --cc=frederic@kernel.org \
    --cc=gieri@linutronix.de \
    --cc=jstultz@google.com \
    --cc=kurt@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=namcao@linutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.com \
    --cc=sboyd@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=werner.abt@meinberg-usa.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).