Skip to contentSkip to conversation

PtP driver for s390 clocks

PATCH Superseded Sven Schnelle 4 participants 8 messages Oct 15, 2024 – Oct 16, 2024

Subsystems (5)
lore
Share & tools
lore

Sven Schnelle

author maintainer
#0 lore raw

cover PtP driver for s390 clocks

Hi,

these patches add support for using the s390 physical and TOD clock as ptp clock. To do so, the first patch adds a clock id to the s390 TOD clock. The second patch adds sending a udev event when a ptp device is added, so that userspace is able to generate stable device names for virtual ptp devices. The last patch adds the PtP driver itself.

Sven Schnelle (3):
s390/time: Add clocksource id to TOD clock
ptp: Add clock name to uevent
s390/time: Add PtP driver

 MAINTAINERS                     |   6 ++
 arch/s390/include/asm/timex.h   |   8 ++
 arch/s390/kernel/time.c         |   8 ++
 drivers/ptp/Kconfig             |  11 +++
 drivers/ptp/Makefile            |   1 +
 drivers/ptp/ptp_clock.c         |  11 ++-
 drivers/ptp/ptp_s390.c          | 127 ++++++++++++++++++++++++++++++++
 include/linux/clocksource_ids.h |   1 +
 8 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 drivers/ptp/ptp_s390.c
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 ptp: Add clock name to uevent

To allow users to have stable device names with the help of udev, add the name to the udev event that is sent when a new PtP clock is available. The key is called 'PTP_CLOCK_NAME'.

---
 drivers/ptp/ptp_clock.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/ptp/ptp_clock.c +10 -1
index c56cd0f63909..15937acb79c6 100644
@@ -25,9 +25,11 @@
 #define PTP_PPS_EVENT PPS_CAPTUREASSERT
 #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
 
+static int ptp_udev_uevent(const struct device *dev, struct kobj_uevent_env *env);
 const struct class ptp_class = {
 	.name = "ptp",
-	.dev_groups = ptp_groups
+	.dev_groups = ptp_groups,
+	.dev_uevent = ptp_udev_uevent
 };
 
 /* private globals */
@@ -514,6 +516,13 @@ EXPORT_SYMBOL(ptp_cancel_worker_sync);
 
 /* module operations */
 
+static int ptp_udev_uevent(const struct device *dev, struct kobj_uevent_env *env)
+{
+	struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
+
+	return add_uevent_var(env, "PTP_CLOCK_NAME=%s", ptp->info->name);
+}
+
 static void __exit ptp_exit(void)
 {
 	class_unregister(&ptp_class);
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/3 s390/time: Add clocksource id to TOD clock

In reply to Sven Schnelle

To allow specifying the clock source in the upcoming PtP driver, add a clocksource ID to the s390 TOD clock.

---
 arch/s390/kernel/time.c         | 1 +
 include/linux/clocksource_ids.h | 1 +
 2 files changed, 2 insertions(+)
arch/s390/kernel/time.c +1 -0
index b713effe0579..4214901c3ab0 100644
@@ -255,6 +255,7 @@ static struct clocksource clocksource_tod = {
 	.shift		= 24,
 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
 	.vdso_clock_mode = VDSO_CLOCKMODE_TOD,
+	.id		= CSID_S390_TOD,
 };
 
 struct clocksource * __init clocksource_default_clock(void)
include/linux/clocksource_ids.h +1 -0
index 2bb4d8c2f1b0..c4ef4ae2eded 100644
@@ -6,6 +6,7 @@
 enum clocksource_ids {
 	CSID_GENERIC		= 0,
 	CSID_ARM_ARCH_COUNTER,
+	CSID_S390_TOD,
 	CSID_X86_TSC_EARLY,
 	CSID_X86_TSC,
 	CSID_X86_KVM_CLK,
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH 3/3 s390/time: Add PtP driver

In reply to Sven Schnelle

Add a small PtP driver which allows user space to get the values of the physical and tod clock. This allows programs like chrony to use STP as clock source and steer the kernel clock. The physical clock can be used as a debugging aid to get the clock without any additional offsets like STP steering or LPAR offset.

---
 MAINTAINERS                   |   6 ++
 arch/s390/include/asm/timex.h |   8 +++
 arch/s390/kernel/time.c       |   7 ++
 drivers/ptp/Kconfig           |  11 +++
 drivers/ptp/Makefile          |   1 +
 drivers/ptp/ptp_s390.c        | 127 ++++++++++++++++++++++++++++++++++
 6 files changed, 160 insertions(+)
 create mode 100644 drivers/ptp/ptp_s390.c
MAINTAINERS +6 -0
index 7ad507f49324..94793c935e86 100644
@@ -20328,6 +20328,12 @@ F:	Documentation/arch/s390/pci.rst
 F:	arch/s390/pci/
 F:	drivers/pci/hotplug/s390_pci_hpc.c
 
+S390 PTP DRIVER
+M:	Sven Schnelle <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/ptp/ptp_s390.c
+
 S390 SCM DRIVER
 M:	Vineeth Vijayan <[email protected]>
 L:	[email protected]
arch/s390/include/asm/timex.h +8 -0
index 640901f2fbc3..4fc88a1493f0 100644
@@ -93,6 +93,7 @@ extern unsigned char ptff_function_mask[16];
 #define PTFF_QAF	0x00	/* query available functions */
 #define PTFF_QTO	0x01	/* query tod offset */
 #define PTFF_QSI	0x02	/* query steering information */
+#define PTFF_QPT	0x03	/* query physical clock */
 #define PTFF_QUI	0x04	/* query UTC information */
 #define PTFF_ATO	0x40	/* adjust tod offset */
 #define PTFF_STO	0x41	/* set tod offset */
@@ -250,6 +251,12 @@ static __always_inline unsigned long tod_to_ns(unsigned long todval)
 	return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
 }
 
+static __always_inline unsigned long eitod_to_ns(union tod_clock *clk)
+{
+	clk->eitod -= TOD_UNIX_EPOCH;
+	return ((clk->eitod >> 9) * 125) + (((clk->eitod & 0x1ff) * 125) >> 9);
+}
+
 /**
  * tod_after - compare two 64 bit TOD values
  * @a: first 64 bit TOD timestamp
@@ -278,4 +285,5 @@ static inline int tod_after_eq(unsigned long a, unsigned long b)
 	return a >= b;
 }
 
+bool stp_enabled(void);
 #endif
arch/s390/kernel/time.c +7 -0
index 4214901c3ab0..47b20235953c 100644
@@ -469,6 +469,13 @@ static void __init stp_reset(void)
 	}
 }
 
+bool stp_enabled(void)
+{
+	return test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags) &&
+		stp_online;
+}
+EXPORT_SYMBOL(stp_enabled);
+
 static void stp_timeout(struct timer_list *unused)
 {
 	queue_work(time_sync_wq, &stp_work);
drivers/ptp/Kconfig +11 -0
index 604541dcb320..907330413ef8 100644
@@ -224,4 +224,15 @@ config PTP_DFL_TOD
 	  To compile this driver as a module, choose M here: the module
 	  will be called ptp_dfl_tod.
 
+config PTP_S390
+	tristate "S390 PTP driver"
+	depends on PTP_1588_CLOCK
+	default y
+	help
+	  This driver adds support for S390 time steering via the PtP
+	  interface. This works by adding a in-kernel clock delta value,
+	  which is always added to time values used in the kernel. The PtP
+	  driver provides the raw clock value without the delta to
+	  userspace. That way userspace programs like chrony could steer
+	  the kernel clock.
 endmenu
drivers/ptp/Makefile +1 -0
index 68bf02078053..4dd9f35eb0cf 100644
@@ -21,3 +21,4 @@ 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
+obj-$(CONFIG_PTP_S390)			+= ptp_s390.o
drivers/ptp/ptp_s390.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/3] ptp: Add clock name to uevent

In reply to Sven Schnelle
Quoted 2 lines

+static int ptp_udev_uevent(const struct device *dev, struct kobj_uevent_env *env);

No forward declarations please. Put the code in the correct order.

Quoted 9 lines

const struct class ptp_class = {

drivers/ptp/ptp_clock.c +2 -1
 	.name = "ptp",
-	.dev_groups = ptp_groups
+	.dev_groups = ptp_groups,
+	.dev_uevent = ptp_udev_uevent
 };
 
 /* private globals */

drivers/ptp/ptp_clock.c EXPORT_SYMBOL(ptp_cancel_worker_sync); · line 524

Quoted 9 lines
drivers/ptp/ptp_clock.c +6 -0
@@ -514,6 +516,13 @@ EXPORT_SYMBOL(ptp_cancel_worker_sync);
 
 /* module operations */
 
+static int ptp_udev_uevent(const struct device *dev, struct kobj_uevent_env *env)
+{
+	struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
+
+	return add_uevent_var(env, "PTP_CLOCK_NAME=%s", ptp->info->name);
+}

https://elixir.bootlin.com/linux/v6.11.3/source/include/linux/ptp_clock_kernel.h#L60

 * @name:      A short "friendly name" to identify the clock and to
 *             help distinguish PHY based devices from MAC based ones.
 *             The string is not meant to be a unique id.

If the name is not unique, you probably should not be using it for udev naming.

Andrew

#7 lore raw

Re: [PATCH 3/3] s390/time: Add PtP driver

In reply to Sven Schnelle

On 10/15/24 01:47, Sven Schnelle wrote:
...

drivers/ptp/ptp_s390.c line 126

Quoted 5 lines
drivers/ptp/ptp_s390.c new file +5 -0
+module_init(ptp_s390_init);
+module_exit(ptp_s390_exit);
+
+MODULE_AUTHOR("Sven Schnelle <[email protected]>");
+MODULE_LICENSE("GPL");

Since commit 1fffe7a34c89 ("script: modpost: emit a warning when the description is missing"), a module without a MODULE_DESCRIPTION() will result in a warning when built with make W=1. Recently, multiple developers have been eradicating these warnings treewide, and very few (if any) are left, so please don't introduce a new one :)

Please add the missing MODULE_DESCRIPTION()

/jeff

maintainer
#4 lore raw

Re: [PATCH 2/3] ptp: Add clock name to uevent

In reply to Andrew Lunn
Quoted 6 lines · Andrew Lunn
 * @name:      A short "friendly name" to identify the clock and to
 *             help distinguish PHY based devices from MAC based ones.
 *             The string is not meant to be a unique id.

If the name is not unique, you probably should not be using it for udev naming.

+1

Maybe the name is unique for s390, but it will not be in general.

Thanks,
Richard

author maintainer
#5 lore raw

Re: [PATCH 2/3] ptp: Add clock name to uevent

Quoted 11 lines · Richard Cochran
Quoted 6 lines · Andrew Lunn
 * @name:      A short "friendly name" to identify the clock and to
 *             help distinguish PHY based devices from MAC based ones.
 *             The string is not meant to be a unique id.

If the name is not unique, you probably should not be using it for udev naming.

+1

Maybe the name is unique for s390, but it will not be in general.

As already written to Greg, i will drop this Patch. The name is unique, i was just not aware that the clock_name attribute is present in sysfs.