]> git.itanic.dy.fi Git - linux-stable/commitdiff
net: ptp: create a mock-up PTP Hardware Clock driver
authorVladimir Oltean <vladimir.oltean@nxp.com>
Mon, 7 Aug 2023 19:33:19 +0000 (22:33 +0300)
committerJakub Kicinski <kuba@kernel.org>
Wed, 9 Aug 2023 22:59:21 +0000 (15:59 -0700)
There are several cases where virtual net devices may benefit from
having a PTP clock, and these have to do with testing. I can see at
least netdevsim and veth as potential users of a common mock-up PTP
hardware clock driver.

The proposed idea is to create an object which emulates PTP clock
operations on top of the unadjustable CLOCK_MONOTONIC_RAW plus a
software-controlled time domain via a timecounter/cyclecounter and then
link that PHC to the netdevsim device.

The driver is fully functional for its intended purpose, and it
successfully passes the PTP selftests.

$ cd tools/testing/selftests/ptp/
$ ./phc.sh /dev/ptp2
TEST: settime                          [ OK ]
TEST: adjtime                          [ OK ]
TEST: adjfreq                          [ OK ]

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://lore.kernel.org/r/20230807193324.4128292-7-vladimir.oltean@nxp.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
MAINTAINERS
drivers/ptp/Kconfig
drivers/ptp/Makefile
drivers/ptp/ptp_mock.c [new file with mode: 0644]
include/linux/ptp_mock.h [new file with mode: 0644]

index 6efcd87136829ec70a16516c44bfca186864f3b5..49f032556cf160c46da39b726ac9a93b13611d9c 100644 (file)
@@ -17170,6 +17170,13 @@ F:     drivers/ptp/*
 F:     include/linux/ptp_cl*
 K:     (?:\b|_)ptp(?:\b|_)
 
+PTP MOCKUP CLOCK SUPPORT
+M:     Vladimir Oltean <vladimir.oltean@nxp.com>
+L:     netdev@vger.kernel.org
+S:     Maintained
+F:     drivers/ptp/ptp_mock.c
+F:     include/linux/ptp_mock.h
+
 PTP VIRTUAL CLOCK SUPPORT
 M:     Yangbo Lu <yangbo.lu@nxp.com>
 L:     netdev@vger.kernel.org
index 32dff1b4f891b0a6b73c63cab3d3f643d5bc24da..ed9d97a032f1cc64faacc32fcc102603af66504e 100644 (file)
@@ -155,6 +155,17 @@ config PTP_1588_CLOCK_IDTCM
          To compile this driver as a module, choose M here: the module
          will be called ptp_clockmatrix.
 
+config PTP_1588_CLOCK_MOCK
+       tristate "Mock-up PTP clock"
+       depends on PTP_1588_CLOCK
+       help
+         This driver offers a set of PTP clock manipulation operations over
+         the system monotonic time. It can be used by virtual network device
+         drivers to emulate PTP capabilities.
+
+         To compile this driver as a module, choose M here: the module
+         will be called ptp_mock.
+
 config PTP_1588_CLOCK_VMW
        tristate "VMware virtual PTP clock"
        depends on ACPI && HYPERVISOR_GUEST && X86
index 553f18bf3c8357a8c3d8e6203c5c2fb76e39a057..dea0cebd2303ed52f5eaf9afd85cfe15b9853cc0 100644 (file)
@@ -16,6 +16,7 @@ ptp-qoriq-y                           += ptp_qoriq.o
 ptp-qoriq-$(CONFIG_DEBUG_FS)           += ptp_qoriq_debugfs.o
 obj-$(CONFIG_PTP_1588_CLOCK_IDTCM)     += ptp_clockmatrix.o
 obj-$(CONFIG_PTP_1588_CLOCK_IDT82P33)  += ptp_idt82p33.o
+obj-$(CONFIG_PTP_1588_CLOCK_MOCK)      += ptp_mock.o
 obj-$(CONFIG_PTP_1588_CLOCK_VMW)       += ptp_vmw.o
 obj-$(CONFIG_PTP_1588_CLOCK_OCP)       += ptp_ocp.o
 obj-$(CONFIG_PTP_DFL_TOD)              += ptp_dfl_tod.o
diff --git a/drivers/ptp/ptp_mock.c b/drivers/ptp/ptp_mock.c
new file mode 100644 (file)
index 0000000..e7b459c
--- /dev/null
@@ -0,0 +1,175 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2023 NXP
+ *
+ * Mock-up PTP Hardware Clock driver for virtual network devices
+ *
+ * Create a PTP clock which offers PTP time manipulation operations
+ * using a timecounter/cyclecounter on top of CLOCK_MONOTONIC_RAW.
+ */
+
+#include <linux/ptp_clock_kernel.h>
+#include <linux/ptp_mock.h>
+#include <linux/timecounter.h>
+
+/* Clamp scaled_ppm between -2,097,152,000 and 2,097,152,000,
+ * and thus "adj" between -68,719,476 and 68,719,476
+ */
+#define MOCK_PHC_MAX_ADJ_PPB           32000000
+/* Timestamps from ktime_get_raw() have 1 ns resolution, so the scale factor
+ * (MULT >> SHIFT) needs to be 1. Pick SHIFT as 31 bits, which translates
+ * MULT(freq 0) into 0x80000000.
+ */
+#define MOCK_PHC_CC_SHIFT              31
+#define MOCK_PHC_CC_MULT               (1 << MOCK_PHC_CC_SHIFT)
+#define MOCK_PHC_FADJ_SHIFT            9
+#define MOCK_PHC_FADJ_DENOMINATOR      15625ULL
+
+/* The largest cycle_delta that timecounter_read_delta() can handle without a
+ * 64-bit overflow during the multiplication with cc->mult, given the max "adj"
+ * we permit, is ~8.3 seconds. Make sure readouts are more frequent than that.
+ */
+#define MOCK_PHC_REFRESH_INTERVAL      (HZ * 5)
+
+#define info_to_phc(d) container_of((d), struct mock_phc, info)
+
+struct mock_phc {
+       struct ptp_clock_info info;
+       struct ptp_clock *clock;
+       struct timecounter tc;
+       struct cyclecounter cc;
+       spinlock_t lock;
+};
+
+static u64 mock_phc_cc_read(const struct cyclecounter *cc)
+{
+       return ktime_get_raw_ns();
+}
+
+static int mock_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm)
+{
+       struct mock_phc *phc = info_to_phc(info);
+       s64 adj;
+
+       adj = (s64)scaled_ppm << MOCK_PHC_FADJ_SHIFT;
+       adj = div_s64(adj, MOCK_PHC_FADJ_DENOMINATOR);
+
+       spin_lock(&phc->lock);
+       timecounter_read(&phc->tc);
+       phc->cc.mult = MOCK_PHC_CC_MULT + adj;
+       spin_unlock(&phc->lock);
+
+       return 0;
+}
+
+static int mock_phc_adjtime(struct ptp_clock_info *info, s64 delta)
+{
+       struct mock_phc *phc = info_to_phc(info);
+
+       spin_lock(&phc->lock);
+       timecounter_adjtime(&phc->tc, delta);
+       spin_unlock(&phc->lock);
+
+       return 0;
+}
+
+static int mock_phc_settime64(struct ptp_clock_info *info,
+                             const struct timespec64 *ts)
+{
+       struct mock_phc *phc = info_to_phc(info);
+       u64 ns = timespec64_to_ns(ts);
+
+       spin_lock(&phc->lock);
+       timecounter_init(&phc->tc, &phc->cc, ns);
+       spin_unlock(&phc->lock);
+
+       return 0;
+}
+
+static int mock_phc_gettime64(struct ptp_clock_info *info, struct timespec64 *ts)
+{
+       struct mock_phc *phc = info_to_phc(info);
+       u64 ns;
+
+       spin_lock(&phc->lock);
+       ns = timecounter_read(&phc->tc);
+       spin_unlock(&phc->lock);
+
+       *ts = ns_to_timespec64(ns);
+
+       return 0;
+}
+
+static long mock_phc_refresh(struct ptp_clock_info *info)
+{
+       struct timespec64 ts;
+
+       mock_phc_gettime64(info, &ts);
+
+       return MOCK_PHC_REFRESH_INTERVAL;
+}
+
+int mock_phc_index(struct mock_phc *phc)
+{
+       return ptp_clock_index(phc->clock);
+}
+EXPORT_SYMBOL_GPL(mock_phc_index);
+
+struct mock_phc *mock_phc_create(struct device *dev)
+{
+       struct mock_phc *phc;
+       int err;
+
+       phc = kzalloc(sizeof(*phc), GFP_KERNEL);
+       if (!phc) {
+               err = -ENOMEM;
+               goto out;
+       }
+
+       phc->info = (struct ptp_clock_info) {
+               .owner          = THIS_MODULE,
+               .name           = "Mock-up PTP clock",
+               .max_adj        = MOCK_PHC_MAX_ADJ_PPB,
+               .adjfine        = mock_phc_adjfine,
+               .adjtime        = mock_phc_adjtime,
+               .gettime64      = mock_phc_gettime64,
+               .settime64      = mock_phc_settime64,
+               .do_aux_work    = mock_phc_refresh,
+       };
+
+       phc->cc = (struct cyclecounter) {
+               .read   = mock_phc_cc_read,
+               .mask   = CYCLECOUNTER_MASK(64),
+               .mult   = MOCK_PHC_CC_MULT,
+               .shift  = MOCK_PHC_CC_SHIFT,
+       };
+
+       spin_lock_init(&phc->lock);
+       timecounter_init(&phc->tc, &phc->cc, 0);
+
+       phc->clock = ptp_clock_register(&phc->info, dev);
+       if (IS_ERR(phc->clock)) {
+               err = PTR_ERR(phc->clock);
+               goto out_free_phc;
+       }
+
+       ptp_schedule_worker(phc->clock, MOCK_PHC_REFRESH_INTERVAL);
+
+       return phc;
+
+out_free_phc:
+       kfree(phc);
+out:
+       return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(mock_phc_create);
+
+void mock_phc_destroy(struct mock_phc *phc)
+{
+       ptp_clock_unregister(phc->clock);
+       kfree(phc);
+}
+EXPORT_SYMBOL_GPL(mock_phc_destroy);
+
+MODULE_DESCRIPTION("Mock-up PTP Hardware Clock driver");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/ptp_mock.h b/include/linux/ptp_mock.h
new file mode 100644 (file)
index 0000000..72eb401
--- /dev/null
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Mock-up PTP Hardware Clock driver for virtual network devices
+ *
+ * Copyright 2023 NXP
+ */
+
+#ifndef _PTP_MOCK_H_
+#define _PTP_MOCK_H_
+
+struct device;
+struct mock_phc;
+
+#if IS_ENABLED(CONFIG_PTP_1588_CLOCK_MOCK)
+
+struct mock_phc *mock_phc_create(struct device *dev);
+void mock_phc_destroy(struct mock_phc *phc);
+int mock_phc_index(struct mock_phc *phc);
+
+#else
+
+static inline struct mock_phc *mock_phc_create(struct device *dev)
+{
+       return NULL;
+}
+
+static inline void mock_phc_destroy(struct mock_phc *phc)
+{
+}
+
+static inline int mock_phc_index(struct mock_phc *phc)
+{
+       return -1;
+}
+
+#endif
+
+#endif /* _PTP_MOCK_H_ */