diff mbox series

[RFC,2/2] virtio_rtc: Support PTP_SYS_OFFSET_STAT ioctl

Message ID 20241219204208.3160-3-quic_philber@quicinc.com (mailing list archive)
State New
Headers show
Series ptp: add PTP_SYS_OFFSET_STAT ioctl, support it in virtio_rtc | expand

Commit Message

Peter Hilber Dec. 19, 2024, 8:42 p.m. UTC
Support the new PTP_SYS_OFFSET_STAT ioctl. The virtio-rtc cross-timestamp
status information is aligned with the PTP_SYS_OFFSET_STAT output, so the
conversion is trivial.

Drop the getcrosststamp op implementation, for which the PTP clock core
will insert a getstattstamp wrapper.

Signed-off-by: Peter Hilber <quic_philber@quicinc.com>
---
 drivers/virtio/Kconfig               |   4 +-
 drivers/virtio/virtio_rtc_driver.c   | 122 ++++++++++++++++++++++++++-
 drivers/virtio/virtio_rtc_internal.h |   3 +-
 drivers/virtio/virtio_rtc_ptp.c      |  25 +++---
 4 files changed, 140 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 6db5235a7693..7cb8b761eaa1 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -230,8 +230,8 @@  config VIRTIO_RTC_ARM
 	 This enables Virtio RTC cross-timestamping using the Arm Generic Timer.
 	 It only has an effect if the Virtio RTC device also supports this. The
 	 cross-timestamp is available through the PTP clock driver precise
-	 cross-timestamp ioctl (PTP_SYS_OFFSET_PRECISE2 aka
-	 PTP_SYS_OFFSET_PRECISE).
+	 cross-timestamp ioctls (PTP_SYS_OFFSET_PRECISE2 aka
+	 PTP_SYS_OFFSET_PRECISE, PTP_SYS_OFFSET_STAT).
 
 	 If unsure, say Y.
 
diff --git a/drivers/virtio/virtio_rtc_driver.c b/drivers/virtio/virtio_rtc_driver.c
index f8b890afc528..055aa1166519 100644
--- a/drivers/virtio/virtio_rtc_driver.c
+++ b/drivers/virtio/virtio_rtc_driver.c
@@ -13,6 +13,7 @@ 
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/pm.h>
+#include <linux/ptp_clock.h>
 
 #include <uapi/linux/virtio_rtc.h>
 
@@ -617,16 +618,22 @@  int viortc_read(struct viortc_dev *viortc, u16 vio_clk_id, u64 *reading)
  * @hw_counter: virtio_rtc HW counter type
  * @reading: clock reading [ns]
  * @cycles: HW counter cycles during clock reading
+ * @stat_extra: extra information, if non-NULL
  *
  * Context: Process context.
  * Return: Zero on success, negative error code otherwise.
  */
 int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
-		      u64 *reading, u64 *cycles)
+		      u64 *reading, u64 *cycles,
+		      struct ptp_stat_extra *stat_extra)
 {
 	VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ_CROSS,
 				       struct virtio_rtc_req_read_cross,
 				       struct virtio_rtc_resp_read_cross);
+	struct ptp_clock_accuracy *accuracy;
+	u8 flags, leap, clock_status;
+	u32 smear_offset_nsec;
+	u16 tai_offset_sec;
 	int ret;
 
 	ret = VIORTC_MSG_INIT(hdl, viortc);
@@ -647,6 +654,119 @@  int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
 	VIORTC_MSG_READ(hdl, clock_reading, reading);
 	VIORTC_MSG_READ(hdl, counter_cycles, cycles);
 
+	if (stat_extra) {
+		accuracy = &stat_extra->accuracy;
+
+		VIORTC_MSG_READ(hdl, perf.freq_esterror,
+				&accuracy->freq_esterror);
+		VIORTC_MSG_READ(hdl, perf.freq_maxerror,
+				&accuracy->freq_maxerror);
+		VIORTC_MSG_READ(hdl, perf.time_esterror,
+				&accuracy->time_esterror);
+		VIORTC_MSG_READ(hdl, perf.time_maxerror,
+				&accuracy->time_maxerror);
+
+		VIORTC_MSG_READ(hdl, perf.flags, &flags);
+
+		accuracy->flags = 0;
+
+		if (flags & VIRTIO_RTC_FLAG_FREQ_ESTERROR_VALID)
+			accuracy->flags |= PTP_CLOCK_FREQ_EST_VALID;
+
+		if (flags & VIRTIO_RTC_FLAG_FREQ_MAXERROR_VALID)
+			accuracy->flags |= PTP_CLOCK_FREQ_MAX_VALID;
+
+		if (flags & VIRTIO_RTC_FLAG_TIME_ESTERROR_VALID)
+			accuracy->flags |= PTP_CLOCK_TIME_EST_VALID;
+
+		if (flags & VIRTIO_RTC_FLAG_TIME_MAXERROR_VALID)
+			accuracy->flags |= PTP_CLOCK_TIME_MAX_VALID;
+
+		VIORTC_MSG_READ(hdl, perf.clock_status, &clock_status);
+
+		switch (clock_status) {
+		case VIRTIO_RTC_STATUS_INITIALIZING:
+			accuracy->clock_status = PTP_CLOCK_STATUS_INITIALIZING;
+			break;
+		case VIRTIO_RTC_STATUS_SYNCHRONIZED:
+			accuracy->clock_status = PTP_CLOCK_STATUS_SYNCHRONIZED;
+			break;
+		case VIRTIO_RTC_STATUS_FREERUNNING:
+			accuracy->clock_status = PTP_CLOCK_STATUS_FREERUNNING;
+			break;
+		case VIRTIO_RTC_STATUS_UNRELIABLE:
+			accuracy->clock_status = PTP_CLOCK_STATUS_UNRELIABLE;
+			break;
+		default:
+			accuracy->clock_status = PTP_CLOCK_STATUS_UNKNOWN;
+			break;
+		}
+
+		VIORTC_MSG_READ(hdl, leap_info.flags, &flags);
+
+		stat_extra->flags = 0;
+
+		if (flags & VIRTIO_RTC_FLAG_LEAP_VALID)
+			stat_extra->flags |= PTP_CLOCK_LEAP_VALID;
+
+		if (flags & VIRTIO_RTC_FLAG_TAI_OFFSET_VALID)
+			stat_extra->flags |= PTP_CLOCK_TAI_OFFSET_VALID;
+
+		if (flags & VIRTIO_RTC_FLAG_SMEAR_OFFSET_VALID)
+			stat_extra->flags |= PTP_CLOCK_SMEAR_OFFSET_VALID;
+
+		VIORTC_MSG_READ(hdl, leap_info.leap, &leap);
+
+		switch (leap) {
+		case VIRTIO_RTC_LEAP_NONE:
+			stat_extra->leap = PTP_LEAP_NONE;
+			break;
+		case VIRTIO_RTC_LEAP_PRE_POS:
+			stat_extra->leap = PTP_LEAP_PRE_POS;
+			break;
+		case VIRTIO_RTC_LEAP_PRE_NEG:
+			stat_extra->leap = PTP_LEAP_PRE_NEG;
+			break;
+		case VIRTIO_RTC_LEAP_POS:
+			stat_extra->leap = PTP_LEAP_POS;
+			break;
+		case VIRTIO_RTC_LEAP_POST_POS:
+			stat_extra->leap = PTP_LEAP_POST_POS;
+			break;
+		case VIRTIO_RTC_LEAP_POST_NEG:
+			stat_extra->leap = PTP_LEAP_POST_NEG;
+			break;
+		case VIRTIO_RTC_LEAP_SMEAR_PRE_POS:
+			stat_extra->leap = PTP_LEAP_SMEAR_PRE_POS;
+			break;
+		case VIRTIO_RTC_LEAP_SMEAR_PRE_NEG:
+			stat_extra->leap = PTP_LEAP_SMEAR_PRE_NEG;
+			break;
+		case VIRTIO_RTC_LEAP_SMEAR_POS:
+			stat_extra->leap = PTP_LEAP_SMEAR_POS;
+			break;
+		case VIRTIO_RTC_LEAP_SMEAR_NEG:
+			stat_extra->leap = PTP_LEAP_SMEAR_NEG;
+			break;
+		case VIRTIO_RTC_LEAP_SMEAR_POST_POS:
+			stat_extra->leap = PTP_LEAP_SMEAR_POST_POS;
+			break;
+		case VIRTIO_RTC_LEAP_SMEAR_POST_NEG:
+			stat_extra->leap = PTP_LEAP_SMEAR_POST_NEG;
+			break;
+		default:
+			ret = -EINVAL;
+			goto out_release;
+		}
+
+		VIORTC_MSG_READ(hdl, leap_info.tai_offset_sec, &tai_offset_sec);
+		stat_extra->tai_offset_sec = (s16)tai_offset_sec;
+
+		VIORTC_MSG_READ(hdl, leap_info.smear_offset_nsec,
+				&smear_offset_nsec);
+		stat_extra->smear_offset_nsec = (s32)smear_offset_nsec;
+	}
+
 out_release:
 	viortc_msg_release(VIORTC_MSG(hdl));
 
diff --git a/drivers/virtio/virtio_rtc_internal.h b/drivers/virtio/virtio_rtc_internal.h
index e7f865259afd..ab998e033f07 100644
--- a/drivers/virtio/virtio_rtc_internal.h
+++ b/drivers/virtio/virtio_rtc_internal.h
@@ -20,7 +20,8 @@  struct viortc_dev;
 
 int viortc_read(struct viortc_dev *viortc, u16 vio_clk_id, u64 *reading);
 int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
-		      u64 *reading, u64 *cycles);
+		      u64 *reading, u64 *cycles,
+		      struct ptp_stat_extra *stat_extra);
 int viortc_cross_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
 		     bool *supported);
 int viortc_read_alarm(struct viortc_dev *viortc, u16 vio_clk_id,
diff --git a/drivers/virtio/virtio_rtc_ptp.c b/drivers/virtio/virtio_rtc_ptp.c
index 09f5a9adf2e4..1a02ee3121d9 100644
--- a/drivers/virtio/virtio_rtc_ptp.c
+++ b/drivers/virtio/virtio_rtc_ptp.c
@@ -79,6 +79,7 @@  static int viortc_ptp_get_time_fn(ktime_t *device_time,
  * @hw_counter: virtio_rtc HW counter type
  * @cs_id: clocksource id corresponding to hw_counter
  * @ctx: context for get_device_system_crosststamp()
+ * @stat_extra: extra information, if non-NULL
  *
  * Reads HW-specific crosststamp from device.
  *
@@ -87,7 +88,8 @@  static int viortc_ptp_get_time_fn(ktime_t *device_time,
  */
 static int viortc_ptp_do_xtstamp(struct viortc_ptp_clock *vio_ptp,
 				 u8 hw_counter, enum clocksource_ids cs_id,
-				 struct viortc_ptp_cross_ctx *ctx)
+				 struct viortc_ptp_cross_ctx *ctx,
+				 struct ptp_stat_extra *stat_extra)
 {
 	u64 ns;
 	u64 max_ns;
@@ -96,8 +98,8 @@  static int viortc_ptp_do_xtstamp(struct viortc_ptp_clock *vio_ptp,
 	ctx->system_counterval.cs_id = cs_id;
 
 	ret = viortc_read_cross(vio_ptp->viortc, vio_ptp->vio_clk_id,
-				hw_counter, &ns,
-				&ctx->system_counterval.cycles);
+				hw_counter, &ns, &ctx->system_counterval.cycles,
+				stat_extra);
 	if (ret)
 		return ret;
 
@@ -115,15 +117,17 @@  static int viortc_ptp_do_xtstamp(struct viortc_ptp_clock *vio_ptp,
  */
 
 /**
- * viortc_ptp_getcrosststamp() - PTP clock getcrosststamp op
+ * viortc_ptp_getstattstamp() - PTP clock getcrosststamp with extras op
  * @ptp: PTP clock info
  * @xtstamp: crosststamp
+ * @stat_extra: extra information, if non-NULL
  *
  * Context: Process context.
  * Return: Zero on success, negative error code otherwise.
  */
-static int viortc_ptp_getcrosststamp(struct ptp_clock_info *ptp,
-				     struct system_device_crosststamp *xtstamp)
+static int viortc_ptp_getstattstamp(struct ptp_clock_info *ptp,
+				    struct system_device_crosststamp *xtstamp,
+				    struct ptp_stat_extra *stat_extra)
 {
 	struct viortc_ptp_clock *vio_ptp =
 		container_of(ptp, struct viortc_ptp_clock, ptp_info);
@@ -152,7 +156,8 @@  static int viortc_ptp_getcrosststamp(struct ptp_clock_info *ptp,
 	 *
 	 * So, get the actual cross-timestamp first.
 	 */
-	ret = viortc_ptp_do_xtstamp(vio_ptp, hw_counter, cs_id, &ctx);
+	ret = viortc_ptp_do_xtstamp(vio_ptp, hw_counter, cs_id, &ctx,
+				    stat_extra);
 	if (ret)
 		return ret;
 
@@ -225,7 +230,7 @@  static int viortc_ptp_enable(struct ptp_clock_info *ptp,
  *
  * The .name member will be set for individual virtio_rtc PTP clocks.
  *
- * The .getcrosststamp member will be cleared for PTP clocks not supporting
+ * The .getstattstamp member will be cleared for PTP clocks not supporting
  * crosststamp.
  */
 static const struct ptp_clock_info viortc_ptp_info_template = {
@@ -236,7 +241,7 @@  static const struct ptp_clock_info viortc_ptp_info_template = {
 	.gettimex64 = viortc_ptp_gettimex64,
 	.settime64 = viortc_ptp_settime64,
 	.enable = viortc_ptp_enable,
-	.getcrosststamp = viortc_ptp_getcrosststamp,
+	.getstattstamp = viortc_ptp_getstattstamp,
 };
 
 /**
@@ -329,7 +334,7 @@  struct viortc_ptp_clock *viortc_ptp_register(struct viortc_dev *viortc,
 		goto err_free_dev;
 
 	if (!vio_ptp->have_cross)
-		vio_ptp->ptp_info.getcrosststamp = NULL;
+		vio_ptp->ptp_info.getstattstamp = NULL;
 
 	ptp_clock = ptp_clock_register(&vio_ptp->ptp_info, parent_dev);
 	if (IS_ERR(ptp_clock))