Skip to contentSkip to conversation

ptp: IEEE 1588 hardware clock support

PATCH Superseded Richard Cochran 9 participants 53 messages Sep 23, 2010 – Oct 2, 2010

Subsystems (14)

v6 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Richard Cochran

author maintainer
#0 lore raw

v6 cover ptp: IEEE 1588 hardware clock support

Here is the sixth version of my patch set adding PTP hardware clock support to the Linux kernel. The main difference to v5 is that the character device interface has been replaced with one based on the posix clock system calls.

The first three patches add necessary background support in the posix clock code. The last five add the new PTP hardware clock features. Previously, I had tried to present the posix clock changes all by themselves, but commentators asked to see the whole context.

What follows is a rather lengthy discussion of the various design issues.

Table of Contents
=================
1 Introduction
2 Previous Discussions
3 Design Issues
3.1 Clock Operations
3.2 Character Device vs System Calls
3.2.1 Using the POSIX Clock API
3.2.2 Tuning a POSIX Clock
3.2.3 Dynamic POSIX Clock IDs
3.3 Synchronizing the Linux System Time
3.4 Ancillary PHC Operations
3.5 User timers
4 Drivers
4.1 Supported Hardware Clocks
4.2 Open Driver Issues
4.2.1 DP83640
4.2.2 IXP465

1 Introduction
~~~~~~~~~~~~~~~

The aim of this patch set is to add support for PTP hardware clocks into the Linux kernel. In the following description, we use the abbreviation "PHC" to mean "PTP hardware clock."

Support for obtaining timestamps from a PHC already exists via the SO_TIMESTAMPING socket option, integrated in kernel version 2.6.30. This patch set completes the picture by allow user space programs to adjust the PHC and to control its ancillary features.

2 Previous Discussions
~~~~~~~~~~~~~~~~~~~~~~~

This patch set previously appeared on the netdev list. Since V5 of the character device patch set, the discussion has moved to the lkml.

- PTP hardware clock as a character device V5

    [http://lkml.org/lkml/2010/8/16/90]

- POSIX clock tuning syscall with static clock ids

    [http://lkml.org/lkml/2010/8/23/49]

- POSIX clock tuning syscall with dynamic clock ids

    [http://lkml.org/lkml/2010/9/3/119]

3 Design Issues
~~~~~~~~~~~~~~~~

3.1 Clock Operations
=====================

Based on experience with several commercially available PHCs, we identified a set of essential operations and a set of ancillary operations.

- Basic clock operations

1. Set time
2. Get time
3. Shift the clock by a given offset atomically
4. Adjust clock frequency

- Ancillary clock features

1. Time stamp external events
2. Enable Linux PPS subsystem events
3. Periodic output signals
4. One shot or periodic alarms, with CPU interrupt

The patch set includes examples of the first two ancillary features, and implementing the third point for a particular PHC is fairly straightforward. The fourth point is discussed below.

3.2 Character Device vs System Calls
=====================================

This patch set started out as a class driver that exposes the PHC as a character device with standardized ioctls. Since several clock operations in the ioctl interface mimic the POSIX clock API, the suggestion was made to expose the PHC as a new clockid_t.

POSIX defines the CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, and CLOCK_THREAD_CPUTIME_ID clock ids. As to other possible clock ids, the standard offers the following hint:

An implementation may also support additional clocks. The interpretation of time values for these clocks is unspecified.

So as far as the POSIX standard is concerned, offering a clock id to represent the PHC would be acceptable.

From discussions on the lkml, a repeated wish was to ensure that any changes in the POSIX clock code would be general enough to support other new hardware clocks that might appear in the future, not just the particulars of PHCs.

3.2.1 Using the POSIX Clock API
--------------------------------

Looking at the mapping from PHC operation to the POSIX clock API, we see that two of the basic clock operations, marked with *, have no POSIX equivalent. The items marked NA are peculiar to PHCs and will be discussed separately, below.

Clock Operation POSIX function
-----------------------------+-----------------------------
Set time clock_gettime
Get time clock_settime
Shift the clock *
Adjust clock frequency *
-----------------------------+-----------------------------
Time stamp external events NA
Enable PPS events NA
Periodic output signals NA
One shot or periodic alarms timer_create, timer_settime

In contrast to the standard Linux system clock, a PHC is adjustable in hardware, for example using frequency compensation registers or a VCO. The ability to directly tune the PHC is essential to reap the benefit of hardware timestamping.

3.2.2 Tuning a POSIX Clock
---------------------------

The patch set introduces a new system call which allows tuning of a POSIX clock. The function combines the 'struct timex' from the NTP adjtimex syscall with a POSIX clock id.

clock_adjtime(clockid_t, struct timex *);

Although the timex interface is a bit baroque (John Stultz said:
"a little crufty"), using it as the basis of the new call allows supporting the tried and true NTP semantics. By adding one additional mode flag to the struct timex, the requirements for PHCs are also satisfied. In the future, if new clocks appear that require more elaborate control, then the padding at the end of the struct reserves 44 bytes for new fields.

3.2.3 Dynamic POSIX Clock IDs
------------------------------

The reaction on the list to having a static id like CLOCK_PTP was mostly negative. However, the idea of generating a clock id dynamically seems to have gained acceptance. The general idea is to advertise the available clock ids to user space via sysfs. This patch set implements two different ways:

/sys/class/timesource/<name>/id
/sys/class/ptp/ptp_clock_X/id

Note: I am not too sure that this is exactly what people imagined,
but it is my best understanding so far. I gleaned two different ideas about where to offer the clock id. In order to keep just one way, I will be happy to remove the less popular one.

3.3 Synchronizing the Linux System Time
========================================

One could offer a PHC as a combined clock source and clock event device. The advantage of this approach would be that it obviates the need for synchronization when the PHC is selected as the system timer. However, some PHCs, namely the PHY based clocks, cannot be used in this way.

Instead, the patch set provides a way to offer a Pulse Per Second (PPS) event from the PHC to the Linux PPS subsystem. A user space application can read the PPS events and tune the system clock, just like when using other external time sources like radio clocks or GPS.

3.4 Ancillary PHC Operations
=============================

Most PHCs offer hardware interfaces to the outside world, that is, the "real world". It is important to offer support for these operations, since leaving them out would defeat the utility of having a PHC in the first place. These operations do not map at all to the POSIX clock functions, but one could offer them as a character device or via sysfs.

Fearing a lkml debate on the merits of either one, I went ahead and implemented both cases. Both approaches adequately cover the needed functionality, in my opinion. The code for each is in its own .c file, so it will be easy enough to remove one of them. I don't mind leaving both ways in, either.

3.5 User timers
================

Using the POSIX clock API gived user space the possibility to create and use timers with timer_create and timer_settime. In the current patch set the kernel functionality is not implemented, since there are some issues to consider first. I see two ways to do about this.

1. Implement the functionality anew. This approach might end up duplicating similar code that already exists. Also, looking at the hrtimer code, getting user timers right seems to have a number of gotchas and thorny issues.

2. Reuse the hrtimer code. Since the hrtimer code uses a clock event device under the hood, it might be possible (in theory) to offer capable PHCs as clock event devices. However, the current hrtimers are hard-coded to the event device via a per-cpu global. Perhaps one could associate an event device with a hrtimer via the timer itself.

At this point I am not optimistic about either approach, and I would vote for postponing the timer issue indefinitely. The implementation effort would be high, but the utility low.

If the Linux system time is synchronized to the PHC via the PPS method, then using standard hrtimers would be good enough for most purposes. Consider the time scales involved. The PHC can be synchronized to within 100 nanoseconds of an external time source, while timer wakeup latency (even with rt kernels) is tens of microseconds.

4 Drivers
~~~~~~~~~~

4.1 Supported Hardware Clocks
==============================

+ Standard Linux system timer
This driver exports the standard Linux timer as a PTP clock. Although this duplicates CLOCK_REALTIME, the code serves as a simple example for driver development and lets people who without special hardware try the new API.

+ Freescale eTSEC gianfar
- 2 Time stamp external triggers, programmable polarity (opt. interrupt)
- 2 Alarm registers (optional interrupt)
- 3 Periodic signals (optional interrupt)

+ National Semiconductor DP83640
- 6 GPIOs programmable as inputs or outputs
- 6 GPIOs with dedicated functions (LED/JTAG/clock) can also be used as general inputs or outputs
- GPIO inputs can time stamp external triggers
- GPIO outputs can produce periodic signals
- 1 interrupt pin

+ Intel IXP465
- Auxiliary Slave/Master Mode Snapshot (optional interrupt)
- Target Time (optional interrupt)

4.2 Open Driver Issues
=======================

4.2.1 DP83640
--------------
In order to make this work, one line must be added into the MAC driver. If you have the DP83640 and want to try the driver, you need to add this one line to your MAC driver: In the .ndo_start_xmit function, add skb_tx_timestamp(skb).

4.2.2 IXP465
-------------
I do not know how to correctly choose the timestamp "channel" based on the port identifier:

+#define PORT2CHANNEL(p) 1
+/*
+ * PHYSICAL_ID(p->id) ?
+ * TODO - Figure out correct mapping.
+ */

Krzysztof, can you help?

Richard Cochran (8):
posix clocks: introduce a syscall for clock tuning. posix clocks: dynamic clock ids.
posix clocks: introduce a sysfs presence.
ptp: Added a brand new class driver for ptp clocks.
ptp: Added a simulated PTP hardware clock.
ptp: Added a clock that uses the eTSEC found on the MPC85xx.
ptp: Added a clock driver for the IXP46x.
ptp: Added a clock driver for the National Semiconductor PHYTER.

 Documentation/ABI/testing/sysfs-ptp             |  107 +++
 Documentation/ABI/testing/sysfs-timesource      |   24 +
 Documentation/powerpc/dts-bindings/fsl/tsec.txt |   57 ++
 Documentation/ptp/ptp.txt                       |   94 +++
 Documentation/ptp/testptp.c                     |  358 +++++++++
 Documentation/ptp/testptp.mk                    |   33 +
 arch/arm/include/asm/unistd.h                   |    1 +
 arch/arm/kernel/calls.S                         |    1 +
 arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h   |   78 ++
 arch/blackfin/include/asm/unistd.h              |    3 +-
 arch/blackfin/mach-common/entry.S               |    1 +
 arch/powerpc/boot/dts/mpc8313erdb.dts           |   14 +
 arch/powerpc/boot/dts/mpc8572ds.dts             |   14 +
 arch/powerpc/boot/dts/p2020ds.dts               |   14 +
 arch/powerpc/boot/dts/p2020rdb.dts              |   14 +
 arch/powerpc/include/asm/systbl.h               |    1 +
 arch/powerpc/include/asm/unistd.h               |    3 +-
 arch/x86/ia32/ia32entry.S                       |    1 +
 arch/x86/include/asm/unistd_32.h                |    3 +-
 arch/x86/include/asm/unistd_64.h                |    2 +
 arch/x86/kernel/syscall_table_32.S              |    1 +
 drivers/Kconfig                                 |    2 +
 drivers/Makefile                                |    1 +
 drivers/char/mmtimer.c                          |    1 +
 drivers/net/Makefile                            |    1 +
 drivers/net/arm/ixp4xx_eth.c                    |  191 +++++
 drivers/net/gianfar_ptp.c                       |  447 ++++++++++++
 drivers/net/gianfar_ptp_reg.h                   |  113 +++
 drivers/net/phy/Kconfig                         |   29 +
 drivers/net/phy/Makefile                        |    1 +
 drivers/net/phy/dp83640.c                       |  887 +++++++++++++++++++++++
 drivers/net/phy/dp83640_reg.h                   |  261 +++++++
 drivers/ptp/Kconfig                             |   67 ++
 drivers/ptp/Makefile                            |    8 +
 drivers/ptp/ptp_chardev.c                       |  178 +++++
 drivers/ptp/ptp_clock.c                         |  382 ++++++++++
 drivers/ptp/ptp_ixp46x.c                        |  345 +++++++++
 drivers/ptp/ptp_linux.c                         |  165 +++++
 drivers/ptp/ptp_private.h                       |   64 ++
 drivers/ptp/ptp_sysfs.c                         |  235 ++++++
 include/linux/Kbuild                            |    1 +
 include/linux/posix-timers.h                    |   14 +-
 include/linux/ptp_clock.h                       |   79 ++
 include/linux/ptp_clock_kernel.h                |  139 ++++
 include/linux/syscalls.h                        |    2 +
 include/linux/time.h                            |    2 +
 include/linux/timex.h                           |    3 +-
 kernel/compat.c                                 |  136 +++--
 kernel/posix-cpu-timers.c                       |    6 +
 kernel/posix-timers.c                           |   98 +++-
 kernel/time/ntp.c                               |    2 +
 51 files changed, 4624 insertions(+), 60 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-ptp
 create mode 100644 Documentation/ABI/testing/sysfs-timesource
 create mode 100644 Documentation/ptp/ptp.txt
 create mode 100644 Documentation/ptp/testptp.c
 create mode 100644 Documentation/ptp/testptp.mk
 create mode 100644 arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h
 create mode 100644 drivers/net/gianfar_ptp.c
 create mode 100644 drivers/net/gianfar_ptp_reg.h
 create mode 100644 drivers/net/phy/dp83640.c
 create mode 100644 drivers/net/phy/dp83640_reg.h
 create mode 100644 drivers/ptp/Kconfig
 create mode 100644 drivers/ptp/Makefile
 create mode 100644 drivers/ptp/ptp_chardev.c
 create mode 100644 drivers/ptp/ptp_clock.c
 create mode 100644 drivers/ptp/ptp_ixp46x.c
 create mode 100644 drivers/ptp/ptp_linux.c
 create mode 100644 drivers/ptp/ptp_private.h
 create mode 100644 drivers/ptp/ptp_sysfs.c
 create mode 100644 include/linux/ptp_clock.h
 create mode 100644 include/linux/ptp_clock_kernel.h
author maintainer
#1 lore raw

PATCH v6 1/8 posix clocks: introduce a syscall for clock tuning.

A new syscall is introduced that allows tuning of a POSIX clock. The syscall is implemented for four architectures: arm, blackfin, powerpc, and x86.

The new syscall, clock_adjtime, takes two parameters, the clock ID, and a pointer to a struct timex. The semantics of the timex struct have been expanded by one additional mode flag, which allows an absolute offset correction. When specificied, the clock offset is immediately corrected by adding the given time value to the current time value.

---
 arch/arm/include/asm/unistd.h      |    1 +
 arch/arm/kernel/calls.S            |    1 +
 arch/blackfin/include/asm/unistd.h |    3 +-
 arch/blackfin/mach-common/entry.S  |    1 +
 arch/powerpc/include/asm/systbl.h  |    1 +
 arch/powerpc/include/asm/unistd.h  |    3 +-
 arch/x86/ia32/ia32entry.S          |    1 +
 arch/x86/include/asm/unistd_32.h   |    3 +-
 arch/x86/include/asm/unistd_64.h   |    2 +
 arch/x86/kernel/syscall_table_32.S |    1 +
 include/linux/posix-timers.h       |    3 +
 include/linux/syscalls.h           |    2 +
 include/linux/timex.h              |    3 +-
 kernel/compat.c                    |  136 +++++++++++++++++++++++-------------
 kernel/posix-cpu-timers.c          |    4 +
 kernel/posix-timers.c              |   17 +++++
 16 files changed, 130 insertions(+), 52 deletions(-)
arch/arm/include/asm/unistd.h +1 -0
index c891eb7..f58d881 100644
@@ -396,6 +396,7 @@
 #define __NR_fanotify_init		(__NR_SYSCALL_BASE+367)
 #define __NR_fanotify_mark		(__NR_SYSCALL_BASE+368)
 #define __NR_prlimit64			(__NR_SYSCALL_BASE+369)
+#define __NR_clock_adjtime		(__NR_SYSCALL_BASE+370)
 
 /*
  * The following SWIs are ARM private.
arch/arm/kernel/calls.S +1 -0
index 5c26ecc..430de4c 100644
@@ -379,6 +379,7 @@
 		CALL(sys_fanotify_init)
 		CALL(sys_fanotify_mark)
 		CALL(sys_prlimit64)
+/* 370 */	CALL(sys_clock_adjtime)
 #ifndef syscalls_counted
 .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls
 #define syscalls_counted
arch/blackfin/include/asm/unistd.h +2 -1
index 14fcd25..79ad99b 100644
@@ -392,8 +392,9 @@
 #define __NR_fanotify_init	371
 #define __NR_fanotify_mark	372
 #define __NR_prlimit64		373
+#define __NR_clock_adjtime	374
 
-#define __NR_syscall		374
+#define __NR_syscall		375
 #define NR_syscalls		__NR_syscall
 
 /* Old optional stuff no one actually uses */
arch/blackfin/mach-common/entry.S +1 -0
index af1bffa..ee68730 100644
@@ -1631,6 +1631,7 @@ ENTRY(_sys_call_table)
 	.long _sys_fanotify_init
 	.long _sys_fanotify_mark
 	.long _sys_prlimit64
+	.long _sys_clock_adjtime
 
 	.rept NR_syscalls-(.-_sys_call_table)/4
 	.long _sys_ni_syscall
arch/powerpc/include/asm/systbl.h +1 -0
index 3d21266..2485d8f 100644
@@ -329,3 +329,4 @@ COMPAT_SYS(rt_tgsigqueueinfo)
 SYSCALL(fanotify_init)
 COMPAT_SYS(fanotify_mark)
 SYSCALL_SPU(prlimit64)
+COMPAT_SYS_SPU(clock_adjtime)
arch/powerpc/include/asm/unistd.h +2 -1
index 597e6f9..85d5067 100644
@@ -348,10 +348,11 @@
 #define __NR_fanotify_init	323
 #define __NR_fanotify_mark	324
 #define __NR_prlimit64		325
+#define __NR_clock_adjtime	326
 
 #ifdef __KERNEL__
 
-#define __NR_syscalls		326
+#define __NR_syscalls		327
 
 #define __NR__exit __NR_exit
 #define NR_syscalls	__NR_syscalls
arch/x86/ia32/ia32entry.S +1 -0
index 518bb99..0ed7896 100644
@@ -851,4 +851,5 @@ ia32_sys_call_table:
 	.quad sys_fanotify_init
 	.quad sys32_fanotify_mark
 	.quad sys_prlimit64		/* 340 */
+	.quad compat_sys_clock_adjtime
 ia32_syscall_end:
arch/x86/include/asm/unistd_32.h +2 -1
index b766a5e..b6f73f1 100644
@@ -346,10 +346,11 @@
 #define __NR_fanotify_init	338
 #define __NR_fanotify_mark	339
 #define __NR_prlimit64		340
+#define __NR_clock_adjtime	341
 
 #ifdef __KERNEL__
 
-#define NR_syscalls 341
+#define NR_syscalls 342
 
 #define __ARCH_WANT_IPC_PARSE_VERSION
 #define __ARCH_WANT_OLD_READDIR
arch/x86/include/asm/unistd_64.h +2 -0
index 363e9b8..5ee3085 100644
@@ -669,6 +669,8 @@ __SYSCALL(__NR_fanotify_init, sys_fanotify_init)
 __SYSCALL(__NR_fanotify_mark, sys_fanotify_mark)
 #define __NR_prlimit64				302
 __SYSCALL(__NR_prlimit64, sys_prlimit64)
+#define __NR_clock_adjtime			303
+__SYSCALL(__NR_clock_adjtime, sys_clock_adjtime)
 
 #ifndef __NO_STUBS
 #define __ARCH_WANT_OLD_READDIR
arch/x86/kernel/syscall_table_32.S +1 -0
index b35786d..68c7b9a 100644
@@ -340,3 +340,4 @@ ENTRY(sys_call_table)
 	.long sys_fanotify_init
 	.long sys_fanotify_mark
 	.long sys_prlimit64		/* 340 */
+	.long sys_clock_adjtime
include/linux/posix-timers.h +3 -0
index 3e23844..abf61cc 100644
@@ -4,6 +4,7 @@
 #include <linux/spinlock.h>
 #include <linux/list.h>
 #include <linux/sched.h>
+#include <linux/timex.h>
 
 union cpu_time_count {
 	cputime_t cpu;
@@ -71,6 +72,7 @@ struct k_clock {
 	int (*clock_getres) (const clockid_t which_clock, struct timespec *tp);
 	int (*clock_set) (const clockid_t which_clock, struct timespec * tp);
 	int (*clock_get) (const clockid_t which_clock, struct timespec * tp);
+	int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
 	int (*timer_create) (struct k_itimer *timer);
 	int (*nsleep) (const clockid_t which_clock, int flags,
 		       struct timespec *, struct timespec __user *);
@@ -97,6 +99,7 @@ int posix_timer_event(struct k_itimer *timr, int si_private);
 int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *ts);
 int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *ts);
 int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *ts);
+int posix_cpu_clock_adj(const clockid_t which_clock, struct timex *tx);
 int posix_cpu_timer_create(struct k_itimer *timer);
 int posix_cpu_nsleep(const clockid_t which_clock, int flags,
 		     struct timespec *rqtp, struct timespec __user *rmtp);
include/linux/syscalls.h +2 -0
index e6319d1..0b24775 100644
@@ -313,6 +313,8 @@ asmlinkage long sys_clock_settime(clockid_t which_clock,
 				const struct timespec __user *tp);
 asmlinkage long sys_clock_gettime(clockid_t which_clock,
 				struct timespec __user *tp);
+asmlinkage long sys_clock_adjtime(clockid_t which_clock,
+				struct timex __user *tx);
 asmlinkage long sys_clock_getres(clockid_t which_clock,
 				struct timespec __user *tp);
 asmlinkage long sys_clock_nanosleep(clockid_t which_clock, int flags,
include/linux/timex.h +2 -1
index 32d852f..82d4b24 100644
@@ -73,7 +73,7 @@ struct timex {
 	long tolerance;		/* clock frequency tolerance (ppm)
 				 * (read only)
 				 */
-	struct timeval time;	/* (read only) */
+	struct timeval time;	/* (read only, except for ADJ_SETOFFSET) */
 	long tick;		/* (modified) usecs between clock ticks */
 
 	long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
@@ -101,6 +101,7 @@ struct timex {
 #define ADJ_ESTERROR		0x0008	/* estimated time error */
 #define ADJ_STATUS		0x0010	/* clock status */
 #define ADJ_TIMECONST		0x0020	/* pll time constant */
+#define ADJ_SETOFFSET		0x0040  /* add 'time' to current time */
 #define ADJ_TAI			0x0080	/* set TAI offset */
 #define ADJ_MICRO		0x1000	/* select microsecond resolution */
 #define ADJ_NANO		0x2000	/* select nanosecond resolution */
kernel/compat.c +88 -48

158 lines not on this page · raw message

kernel/posix-cpu-timers.c +4 -0
index 6842eeb..e1c2e7b 100644
@@ -207,6 +207,10 @@ int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
 	return error;
 }
 
+int posix_cpu_clock_adj(const clockid_t which_clock, struct timex *tx)
+{
+	return -EOPNOTSUPP;
+}
 
 /*
  * Sample a per-thread clock for the given task.
kernel/posix-timers.c +17 -0
index 9ca4973..446b566 100644
@@ -197,6 +197,14 @@ static int common_timer_create(struct k_itimer *new_timer)
 	return 0;
 }
 
+static inline int common_clock_adj(const clockid_t which_clock, struct timex *t)
+{
+	if (CLOCK_REALTIME == which_clock)
+		return do_adjtimex(t);
+	else
+		return -EOPNOTSUPP;
+}
+
 static int no_timer_create(struct k_itimer *new_timer)
 {
 	return -EOPNOTSUPP;
@@ -969,6 +977,15 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
 
 }
 
+SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
+		struct timex __user *, tx)
+{
+	if (invalid_clockid(which_clock))
+		return -EINVAL;
+
+	return CLOCK_DISPATCH(which_clock, clock_adj, (which_clock, tx));
+}
+
 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
 		struct timespec __user *, tp)
 {
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

Quoted 10 lines · Richard Cochran

A new syscall is introduced that allows tuning of a POSIX clock. The syscall is implemented for four architectures: arm, blackfin, powerpc, and x86.

The new syscall, clock_adjtime, takes two parameters, the clock ID, and a pointer to a struct timex. The semantics of the timex struct have been expanded by one additional mode flag, which allows an absolute offset correction. When specificied, the clock offset is immediately corrected by adding the given time value to the current time value.

So I'd still split this patch up a little bit more.

1) Patch that implements the ADJ_SETOFFSET (*and its implementation*) in do_adjtimex.

2) Patch that adds the new syscall and clock_id multiplexing.

3) Patches that wire it up to the rest of the architectures (there's still a bunch missing here).

And one little nit in the code:

Quoted 5 lines
kernel/posix-timers.c +0 -0
index 9ca4973..446b566 100644

kernel/posix-timers.c static int common_timer_create(struct k_itimer *new_timer) · line 206

Quoted 10 lines
kernel/posix-timers.c +7 -0
@@ -197,6 +197,14 @@ static int common_timer_create(struct k_itimer *new_timer)
 	return 0;
 }
 
+static inline int common_clock_adj(const clockid_t which_clock, struct timex *t)
+{
+	if (CLOCK_REALTIME == which_clock)
+		return do_adjtimex(t);
+	else
+		return -EOPNOTSUPP;
+}

Would it make sense to point to the do_adjtimex() in the k_clock definition for CLOCK_REALTIME rather then conditionalizing it here?

thanks
-john

author maintainer
#3 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

Quoted 15 lines · john stultz
Quoted 10 lines · Richard Cochran

A new syscall is introduced that allows tuning of a POSIX clock. The syscall is implemented for four architectures: arm, blackfin, powerpc, and x86.

The new syscall, clock_adjtime, takes two parameters, the clock ID, and a pointer to a struct timex. The semantics of the timex struct have been expanded by one additional mode flag, which allows an absolute offset correction. When specificied, the clock offset is immediately corrected by adding the given time value to the current time value.

So I'd still split this patch up a little bit more.

Quoted 7 lines

1) Patch that implements the ADJ_SETOFFSET (*and its implementation*) in do_adjtimex.

2) Patch that adds the new syscall and clock_id multiplexing.

3) Patches that wire it up to the rest of the architectures (there's still a bunch missing here).

I was not sure what the policy is about adding syscalls. Is it the syscall author's responsibility to add it into every arch?

The last time (see a2e2725541fad7) the commit only added half of some archs, and ignored others. In my patch, the syscall *really* works on the archs that are present in the patch.

(Actually, I did not test blackfin, since I don't have one, but I included it since I know they have a PTP hardware clock.)

Quoted 11 lines

kernel/posix-timers.c static int common_timer_create(struct k_itimer *new_timer) · line 206

Quoted 7 lines
kernel/posix-timers.c +7 -0
+static inline int common_clock_adj(const clockid_t which_clock, struct timex *t)
+{
+	if (CLOCK_REALTIME == which_clock)
+		return do_adjtimex(t);
+	else
+		return -EOPNOTSUPP;
+}

Would it make sense to point to the do_adjtimex() in the k_clock definition for CLOCK_REALTIME rather then conditionalizing it here?

But what about CLOCK_MONOTONIC_RAW, for example?

Does it make sense to allow it to be adjusted?

Thanks,
Richard

#4 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

Quoted 14 lines · Richard Cochran
Quoted 9 lines · john stultz

So I'd still split this patch up a little bit more.

1) Patch that implements the ADJ_SETOFFSET (*and its implementation*) in do_adjtimex.

2) Patch that adds the new syscall and clock_id multiplexing.

3) Patches that wire it up to the rest of the architectures (there's still a bunch missing here).

I was not sure what the policy is about adding syscalls. Is it the syscall author's responsibility to add it into every arch?

Quoted 6 lines

The last time (see a2e2725541fad7) the commit only added half of some archs, and ignored others. In my patch, the syscall *really* works on the archs that are present in the patch.

(Actually, I did not test blackfin, since I don't have one, but I included it since I know they have a PTP hardware clock.)

I'm not sure about policy, but I think for completeness sake you should make sure every arch supports a new syscall. You're not expected to be able to test every one, but getting the basic support patch sent to maintainers should be done.

Quoted 12 lines
Quoted 11 lines

kernel/posix-timers.c static int common_timer_create(struct k_itimer *new_timer) · line 206

Quoted 7 lines
kernel/posix-timers.c +7 -0
+static inline int common_clock_adj(const clockid_t which_clock, struct timex *t)
+{
+	if (CLOCK_REALTIME == which_clock)
+		return do_adjtimex(t);
+	else
+		return -EOPNOTSUPP;
+}

Would it make sense to point to the do_adjtimex() in the k_clock definition for CLOCK_REALTIME rather then conditionalizing it here?

Quoted 1 line

But what about CLOCK_MONOTONIC_RAW, for example?

-EOPNOTSUPP

Quoted 1 line

Does it make sense to allow it to be adjusted?

No. I think only CLOCK_REALTIME would make sense of the existing clocks.

I'm just suggesting you conditionalize it from the function pointer, rather then in the common function.

thanks
-john

#5 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

In reply to Richard Cochran
Quoted 10 lines · Richard Cochran

A new syscall is introduced that allows tuning of a POSIX clock. The syscall is implemented for four architectures: arm, blackfin, powerpc, and x86.

The new syscall, clock_adjtime, takes two parameters, the clock ID, and a pointer to a struct timex. The semantics of the timex struct have been expanded by one additional mode flag, which allows an absolute offset correction. When specificied, the clock offset is immediately corrected by adding the given time value to the current time value.

Any reason why you CC'ed device-tree discuss ?

This list is getting way too much unrelated stuff, which I find annoying, it would be nice if we were all a bit more careful here with our CC lists.

Cheers,
Ben.

Quoted 423 lines
---
 arch/arm/include/asm/unistd.h      |    1 +
 arch/arm/kernel/calls.S            |    1 +
 arch/blackfin/include/asm/unistd.h |    3 +-
 arch/blackfin/mach-common/entry.S  |    1 +
 arch/powerpc/include/asm/systbl.h  |    1 +
 arch/powerpc/include/asm/unistd.h  |    3 +-
 arch/x86/ia32/ia32entry.S          |    1 +
 arch/x86/include/asm/unistd_32.h   |    3 +-
 arch/x86/include/asm/unistd_64.h   |    2 +
 arch/x86/kernel/syscall_table_32.S |    1 +
 include/linux/posix-timers.h       |    3 +
 include/linux/syscalls.h           |    2 +
 include/linux/timex.h              |    3 +-
 kernel/compat.c                    |  136 +++++++++++++++++++++++-------------
 kernel/posix-cpu-timers.c          |    4 +
 kernel/posix-timers.c              |   17 +++++
 16 files changed, 130 insertions(+), 52 deletions(-)
arch/arm/include/asm/unistd.h +1 -0
index c891eb7..f58d881 100644
@@ -396,6 +396,7 @@
 #define __NR_fanotify_init		(__NR_SYSCALL_BASE+367)
 #define __NR_fanotify_mark		(__NR_SYSCALL_BASE+368)
 #define __NR_prlimit64			(__NR_SYSCALL_BASE+369)
+#define __NR_clock_adjtime		(__NR_SYSCALL_BASE+370)
 
 /*
  * The following SWIs are ARM private.
arch/arm/kernel/calls.S +1 -0
index 5c26ecc..430de4c 100644
@@ -379,6 +379,7 @@
 		CALL(sys_fanotify_init)
 		CALL(sys_fanotify_mark)
 		CALL(sys_prlimit64)
+/* 370 */	CALL(sys_clock_adjtime)
 #ifndef syscalls_counted
 .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls
 #define syscalls_counted
arch/blackfin/include/asm/unistd.h +2 -1
index 14fcd25..79ad99b 100644
@@ -392,8 +392,9 @@
 #define __NR_fanotify_init	371
 #define __NR_fanotify_mark	372
 #define __NR_prlimit64		373
+#define __NR_clock_adjtime	374
 
-#define __NR_syscall		374
+#define __NR_syscall		375
 #define NR_syscalls		__NR_syscall
 
 /* Old optional stuff no one actually uses */
arch/blackfin/mach-common/entry.S +1 -0
index af1bffa..ee68730 100644
@@ -1631,6 +1631,7 @@ ENTRY(_sys_call_table)
 	.long _sys_fanotify_init
 	.long _sys_fanotify_mark
 	.long _sys_prlimit64
+	.long _sys_clock_adjtime
 
 	.rept NR_syscalls-(.-_sys_call_table)/4
 	.long _sys_ni_syscall
arch/powerpc/include/asm/systbl.h +1 -0
index 3d21266..2485d8f 100644
@@ -329,3 +329,4 @@ COMPAT_SYS(rt_tgsigqueueinfo)
 SYSCALL(fanotify_init)
 COMPAT_SYS(fanotify_mark)
 SYSCALL_SPU(prlimit64)
+COMPAT_SYS_SPU(clock_adjtime)
arch/powerpc/include/asm/unistd.h +2 -1
index 597e6f9..85d5067 100644
@@ -348,10 +348,11 @@
 #define __NR_fanotify_init	323
 #define __NR_fanotify_mark	324
 #define __NR_prlimit64		325
+#define __NR_clock_adjtime	326
 
 #ifdef __KERNEL__
 
-#define __NR_syscalls		326
+#define __NR_syscalls		327
 
 #define __NR__exit __NR_exit
 #define NR_syscalls	__NR_syscalls
arch/x86/ia32/ia32entry.S +1 -0
index 518bb99..0ed7896 100644
@@ -851,4 +851,5 @@ ia32_sys_call_table:
 	.quad sys_fanotify_init
 	.quad sys32_fanotify_mark
 	.quad sys_prlimit64		/* 340 */
+	.quad compat_sys_clock_adjtime
 ia32_syscall_end:
arch/x86/include/asm/unistd_32.h +2 -1
index b766a5e..b6f73f1 100644
@@ -346,10 +346,11 @@
 #define __NR_fanotify_init	338
 #define __NR_fanotify_mark	339
 #define __NR_prlimit64		340
+#define __NR_clock_adjtime	341
 
 #ifdef __KERNEL__
 
-#define NR_syscalls 341
+#define NR_syscalls 342
 
 #define __ARCH_WANT_IPC_PARSE_VERSION
 #define __ARCH_WANT_OLD_READDIR
arch/x86/include/asm/unistd_64.h +2 -0
index 363e9b8..5ee3085 100644
@@ -669,6 +669,8 @@ __SYSCALL(__NR_fanotify_init, sys_fanotify_init)
 __SYSCALL(__NR_fanotify_mark, sys_fanotify_mark)
 #define __NR_prlimit64				302
 __SYSCALL(__NR_prlimit64, sys_prlimit64)
+#define __NR_clock_adjtime			303
+__SYSCALL(__NR_clock_adjtime, sys_clock_adjtime)
 
 #ifndef __NO_STUBS
 #define __ARCH_WANT_OLD_READDIR
arch/x86/kernel/syscall_table_32.S +1 -0
index b35786d..68c7b9a 100644
@@ -340,3 +340,4 @@ ENTRY(sys_call_table)
 	.long sys_fanotify_init
 	.long sys_fanotify_mark
 	.long sys_prlimit64		/* 340 */
+	.long sys_clock_adjtime
include/linux/posix-timers.h +3 -0
index 3e23844..abf61cc 100644
@@ -4,6 +4,7 @@
 #include <linux/spinlock.h>
 #include <linux/list.h>
 #include <linux/sched.h>
+#include <linux/timex.h>
 
 union cpu_time_count {
 	cputime_t cpu;
@@ -71,6 +72,7 @@ struct k_clock {
 	int (*clock_getres) (const clockid_t which_clock, struct timespec *tp);
 	int (*clock_set) (const clockid_t which_clock, struct timespec * tp);
 	int (*clock_get) (const clockid_t which_clock, struct timespec * tp);
+	int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
 	int (*timer_create) (struct k_itimer *timer);
 	int (*nsleep) (const clockid_t which_clock, int flags,
 		       struct timespec *, struct timespec __user *);
@@ -97,6 +99,7 @@ int posix_timer_event(struct k_itimer *timr, int si_private);
 int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *ts);
 int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *ts);
 int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *ts);
+int posix_cpu_clock_adj(const clockid_t which_clock, struct timex *tx);
 int posix_cpu_timer_create(struct k_itimer *timer);
 int posix_cpu_nsleep(const clockid_t which_clock, int flags,
 		     struct timespec *rqtp, struct timespec __user *rmtp);
include/linux/syscalls.h +2 -0
index e6319d1..0b24775 100644
@@ -313,6 +313,8 @@ asmlinkage long sys_clock_settime(clockid_t which_clock,
 				const struct timespec __user *tp);
 asmlinkage long sys_clock_gettime(clockid_t which_clock,
 				struct timespec __user *tp);
+asmlinkage long sys_clock_adjtime(clockid_t which_clock,
+				struct timex __user *tx);
 asmlinkage long sys_clock_getres(clockid_t which_clock,
 				struct timespec __user *tp);
 asmlinkage long sys_clock_nanosleep(clockid_t which_clock, int flags,
include/linux/timex.h +2 -1
index 32d852f..82d4b24 100644
@@ -73,7 +73,7 @@ struct timex {
 	long tolerance;		/* clock frequency tolerance (ppm)
 				 * (read only)
 				 */
-	struct timeval time;	/* (read only) */
+	struct timeval time;	/* (read only, except for ADJ_SETOFFSET) */
 	long tick;		/* (modified) usecs between clock ticks */
 
 	long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
@@ -101,6 +101,7 @@ struct timex {
 #define ADJ_ESTERROR		0x0008	/* estimated time error */
 #define ADJ_STATUS		0x0010	/* clock status */
 #define ADJ_TIMECONST		0x0020	/* pll time constant */
+#define ADJ_SETOFFSET		0x0040  /* add 'time' to current time */
 #define ADJ_TAI			0x0080	/* set TAI offset */
 #define ADJ_MICRO		0x1000	/* select microsecond resolution */
 #define ADJ_NANO		0x2000	/* select nanosecond resolution */
kernel/compat.c +88 -48
index c9e2ec0..38b1d2c 100644
@@ -52,6 +52,64 @@ static int compat_put_timeval(struct compat_timeval __user *o,
 		put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
 }
 
+static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
+{
+	memset(txc, 0, sizeof(struct timex));
+
+	if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
+			__get_user(txc->modes, &utp->modes) ||
+			__get_user(txc->offset, &utp->offset) ||
+			__get_user(txc->freq, &utp->freq) ||
+			__get_user(txc->maxerror, &utp->maxerror) ||
+			__get_user(txc->esterror, &utp->esterror) ||
+			__get_user(txc->status, &utp->status) ||
+			__get_user(txc->constant, &utp->constant) ||
+			__get_user(txc->precision, &utp->precision) ||
+			__get_user(txc->tolerance, &utp->tolerance) ||
+			__get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
+			__get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
+			__get_user(txc->tick, &utp->tick) ||
+			__get_user(txc->ppsfreq, &utp->ppsfreq) ||
+			__get_user(txc->jitter, &utp->jitter) ||
+			__get_user(txc->shift, &utp->shift) ||
+			__get_user(txc->stabil, &utp->stabil) ||
+			__get_user(txc->jitcnt, &utp->jitcnt) ||
+			__get_user(txc->calcnt, &utp->calcnt) ||
+			__get_user(txc->errcnt, &utp->errcnt) ||
+			__get_user(txc->stbcnt, &utp->stbcnt))
+		return -EFAULT;
+
+	return 0;
+}
+
+static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
+{
+	if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
+			__put_user(txc->modes, &utp->modes) ||
+			__put_user(txc->offset, &utp->offset) ||
+			__put_user(txc->freq, &utp->freq) ||
+			__put_user(txc->maxerror, &utp->maxerror) ||
+			__put_user(txc->esterror, &utp->esterror) ||
+			__put_user(txc->status, &utp->status) ||
+			__put_user(txc->constant, &utp->constant) ||
+			__put_user(txc->precision, &utp->precision) ||
+			__put_user(txc->tolerance, &utp->tolerance) ||
+			__put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
+			__put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
+			__put_user(txc->tick, &utp->tick) ||
+			__put_user(txc->ppsfreq, &utp->ppsfreq) ||
+			__put_user(txc->jitter, &utp->jitter) ||
+			__put_user(txc->shift, &utp->shift) ||
+			__put_user(txc->stabil, &utp->stabil) ||
+			__put_user(txc->jitcnt, &utp->jitcnt) ||
+			__put_user(txc->calcnt, &utp->calcnt) ||
+			__put_user(txc->errcnt, &utp->errcnt) ||
+			__put_user(txc->stbcnt, &utp->stbcnt) ||
+			__put_user(txc->tai, &utp->tai))
+		return -EFAULT;
+	return 0;
+}
+
 asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
 		struct timezone __user *tz)
 {
@@ -617,6 +675,29 @@ long compat_sys_clock_gettime(clockid_t which_clock,
 	return err;
 }
 
+long compat_sys_clock_adjtime(clockid_t which_clock,
+		struct compat_timex __user *utp)
+{
+	struct timex txc;
+	mm_segment_t oldfs;
+	int err, ret;
+
+	err = compat_get_timex(&txc, utp);
+	if (err)
+		return err;
+
+	oldfs = get_fs();
+	set_fs(KERNEL_DS);
+	ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
+	set_fs(oldfs);
+
+	err = compat_put_timex(utp, &txc);
+	if (err)
+		return err;
+
+	return ret;
+}
+
 long compat_sys_clock_getres(clockid_t which_clock,
 		struct compat_timespec __user *tp)
 {
@@ -951,58 +1032,17 @@ asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat
 asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
 {
 	struct timex txc;
-	int ret;
-
-	memset(&txc, 0, sizeof(struct timex));
+	int err, ret;
 
-	if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
-			__get_user(txc.modes, &utp->modes) ||
-			__get_user(txc.offset, &utp->offset) ||
-			__get_user(txc.freq, &utp->freq) ||
-			__get_user(txc.maxerror, &utp->maxerror) ||
-			__get_user(txc.esterror, &utp->esterror) ||
-			__get_user(txc.status, &utp->status) ||
-			__get_user(txc.constant, &utp->constant) ||
-			__get_user(txc.precision, &utp->precision) ||
-			__get_user(txc.tolerance, &utp->tolerance) ||
-			__get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
-			__get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
-			__get_user(txc.tick, &utp->tick) ||
-			__get_user(txc.ppsfreq, &utp->ppsfreq) ||
-			__get_user(txc.jitter, &utp->jitter) ||
-			__get_user(txc.shift, &utp->shift) ||
-			__get_user(txc.stabil, &utp->stabil) ||
-			__get_user(txc.jitcnt, &utp->jitcnt) ||
-			__get_user(txc.calcnt, &utp->calcnt) ||
-			__get_user(txc.errcnt, &utp->errcnt) ||
-			__get_user(txc.stbcnt, &utp->stbcnt))
-		return -EFAULT;
+	err = compat_get_timex(&txc, utp);
+	if (err)
+		return err;
 
 	ret = do_adjtimex(&txc);
 
-	if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
-			__put_user(txc.modes, &utp->modes) ||
-			__put_user(txc.offset, &utp->offset) ||
-			__put_user(txc.freq, &utp->freq) ||
-			__put_user(txc.maxerror, &utp->maxerror) ||
-			__put_user(txc.esterror, &utp->esterror) ||
-			__put_user(txc.status, &utp->status) ||
-			__put_user(txc.constant, &utp->constant) ||
-			__put_user(txc.precision, &utp->precision) ||
-			__put_user(txc.tolerance, &utp->tolerance) ||
-			__put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
-			__put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
-			__put_user(txc.tick, &utp->tick) ||
-			__put_user(txc.ppsfreq, &utp->ppsfreq) ||
-			__put_user(txc.jitter, &utp->jitter) ||
-			__put_user(txc.shift, &utp->shift) ||
-			__put_user(txc.stabil, &utp->stabil) ||
-			__put_user(txc.jitcnt, &utp->jitcnt) ||
-			__put_user(txc.calcnt, &utp->calcnt) ||
-			__put_user(txc.errcnt, &utp->errcnt) ||
-			__put_user(txc.stbcnt, &utp->stbcnt) ||
-			__put_user(txc.tai, &utp->tai))
-		ret = -EFAULT;
+	err = compat_put_timex(utp, &txc);
+	if (err)
+		return err;
 
 	return ret;
 }
kernel/posix-cpu-timers.c +4 -0
index 6842eeb..e1c2e7b 100644
@@ -207,6 +207,10 @@ int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
 	return error;
 }
 
+int posix_cpu_clock_adj(const clockid_t which_clock, struct timex *tx)
+{
+	return -EOPNOTSUPP;
+}
 
 /*
  * Sample a per-thread clock for the given task.
kernel/posix-timers.c +17 -0
index 9ca4973..446b566 100644
@@ -197,6 +197,14 @@ static int common_timer_create(struct k_itimer *new_timer)
 	return 0;
 }
 
+static inline int common_clock_adj(const clockid_t which_clock, struct timex *t)
+{
+	if (CLOCK_REALTIME == which_clock)
+		return do_adjtimex(t);
+	else
+		return -EOPNOTSUPP;
+}
+
 static int no_timer_create(struct k_itimer *new_timer)
 {
 	return -EOPNOTSUPP;
@@ -969,6 +977,15 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
 
 }
 
+SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
+		struct timex __user *, tx)
+{
+	if (invalid_clockid(which_clock))
+		return -EINVAL;
+
+	return CLOCK_DISPATCH(which_clock, clock_adj, (which_clock, tx));
+}
+
 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
 		struct timespec __user *, tp)
 {
#6 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

Quoted 8 lines · Benjamin Herrenschmidt
Quoted 6 lines · Richard Cochran

The new syscall, clock_adjtime, takes two parameters, the clock ID, and a pointer to a struct timex. The semantics of the timex struct have been expanded by one additional mode flag, which allows an absolute offset correction. When specificied, the clock offset is immediately corrected by adding the given time value to the current time value.

Quoted 5 lines

Any reason why you CC'ed device-tree discuss ?

This list is getting way too much unrelated stuff, which I find annoying, it would be nice if we were all a bit more careful here with our CC lists.

Says the guy who missed to trim the useless context of the original mail, which made me scroll down all the way just to find out that there is nothing to see.

Thanks,

tglx

#7 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

Quoted 8 lines · Thomas Gleixner
Quoted 2 lines

This list is getting way too much unrelated stuff, which I find annoying, it would be nice if we were all a bit more careful here

with

Quoted 1 line

our CC lists.

Says the guy who missed to trim the useless context of the original mail, which made me scroll down all the way just to find out that there is nothing to see.

Heh, you can usually ignore what's after my signature :-) At least I didn't put my reply all the way down the bottom !

Cheers,
Ben.

author maintainer
#8 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

In reply to Benjamin Herrenschmidt
Quoted 12 lines · Benjamin Herrenschmidt
Quoted 10 lines · Richard Cochran

A new syscall is introduced that allows tuning of a POSIX clock. The syscall is implemented for four architectures: arm, blackfin, powerpc, and x86.

The new syscall, clock_adjtime, takes two parameters, the clock ID, and a pointer to a struct timex. The semantics of the timex struct have been expanded by one additional mode flag, which allows an absolute offset correction. When specificied, the clock offset is immediately corrected by adding the given time value to the current time value.

Quoted 5 lines

Any reason why you CC'ed device-tree discuss ?

This list is getting way too much unrelated stuff, which I find annoying, it would be nice if we were all a bit more careful here with our CC lists.

Sorry, I only added device-tree because some one asked me to do so.

    http://marc.info/?l=linux-netdev&m=127273157912358

I'll leave it off next time.

Thanks,
Richard

#9 lore raw

Re: [PATCH 1/8] posix clocks: introduce a syscall for clock tuning.

Quoted 10 lines
Quoted 4 lines

This list is getting way too much unrelated stuff, which I find annoying, it would be nice if we were all a bit more careful here with our CC lists.

Sorry, I only added device-tree because some one asked me to do so.

    http://marc.info/?l=linux-netdev&m=127273157912358

I'll leave it off next time.

That's allright. I'd rather you just post the bindings there than the whole patch least but no big deal.

I was just fixing my email filters and notice a lot of seemingly unrelated stuff landing there :-)

Cheers,
Ben.

author maintainer
#10 lore raw

PATCH v6 2/8 posix clocks: dynamic clock ids.

In reply to Richard Cochran

This patch augments the POSIX clock code to offer a dynamic clock creation method. Instead of registering a hard coded clock ID, modules may call create_posix_clock(), which returns a new clock ID.

---
 include/linux/posix-timers.h |    7 ++++++-
 include/linux/time.h         |    2 ++
 kernel/posix-timers.c        |   41 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 42 insertions(+), 8 deletions(-)
include/linux/posix-timers.h +6 -1
index abf61cc..08aa4da 100644
@@ -68,6 +68,7 @@ struct k_itimer {
 };
 
 struct k_clock {
+	clockid_t id;
 	int res;		/* in nanoseconds */
 	int (*clock_getres) (const clockid_t which_clock, struct timespec *tp);
 	int (*clock_set) (const clockid_t which_clock, struct timespec * tp);
@@ -86,7 +87,11 @@ struct k_clock {
 			   struct itimerspec * cur_setting);
 };
 
-void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock);
+/* Regsiter a posix clock with a "well known" clock id. */
+int register_posix_clock(const clockid_t id, struct k_clock *clock);
+
+/* Create a new posix clock with a dynamic clock id. */
+clockid_t create_posix_clock(struct k_clock *clock);
 
 /* error handlers for timer_create, nanosleep and settime */
 int do_posix_clock_nonanosleep(const clockid_t, int flags, struct timespec *,
include/linux/time.h +2 -0
index 9f15ac7..914c48d 100644
@@ -299,6 +299,8 @@ struct itimerval {
 #define CLOCKS_MASK			(CLOCK_REALTIME | CLOCK_MONOTONIC)
 #define CLOCKS_MONO			CLOCK_MONOTONIC
 
+#define CLOCK_INVALID			-1
+
 /*
  * The various flags for setting POSIX.1b interval timers:
  */
kernel/posix-timers.c +34 -7

58 lines not on this page · raw message

Signature · 1 line
1.7.0.4
author maintainer
#11 lore raw

PATCH v6 3/8 posix clocks: introduce a sysfs presence.

In reply to Richard Cochran

This patch adds a 'timesource' class into sysfs. Each registered POSIX clock appears by name under /sys/class/timesource. The idea is to expose to user space the dynamic mapping between clock devices and clock IDs.

---
 Documentation/ABI/testing/sysfs-timesource |   24 ++++++++++++++++
 drivers/char/mmtimer.c                     |    1 +
 include/linux/posix-timers.h               |    4 +++
 kernel/posix-cpu-timers.c                  |    2 +
 kernel/posix-timers.c                      |   40 ++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-timesource
Documentation/ABI/testing/sysfs-timesource new file +24 -0
new file mode 100644
index 0000000..f991de2
@@ -0,0 +1,24 @@
+What:		/sys/class/timesource/
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This directory contains files and directories
+		providing a standardized interface to the available
+		time sources.
+
+What:		/sys/class/timesource/<name>/
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This directory contains the attributes of a time
+		source registered with the POSIX clock subsystem.
+
+What:		/sys/class/timesource/<name>/id
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the clock ID (a non-negative
+		integer) of the named time source registered with the
+		POSIX clock subsystem. This value may be passed as the
+		first argument to the POSIX clock and timer system
+		calls. See man CLOCK_GETRES(2) and TIMER_CREATE(2).
drivers/char/mmtimer.c +1 -0
index ea7c99f..e9173e3 100644
@@ -758,6 +758,7 @@ static int sgi_timer_set(struct k_itimer *timr, int flags,
 }
 
 static struct k_clock sgi_clock = {
+	.name = "sgi_cycle",
 	.res = 0,
 	.clock_set = sgi_clock_set,
 	.clock_get = sgi_clock_get,
include/linux/posix-timers.h +4 -0
index 08aa4da..64e6fee 100644
@@ -67,7 +67,11 @@ struct k_itimer {
 	} it;
 };
 
+#define KCLOCK_MAX_NAME 32
+
 struct k_clock {
+	char name[KCLOCK_MAX_NAME];
+	struct device *dev;
 	clockid_t id;
 	int res;		/* in nanoseconds */
 	int (*clock_getres) (const clockid_t which_clock, struct timespec *tp);
kernel/posix-cpu-timers.c +2 -0
index e1c2e7b..df9cbab 100644
@@ -1611,6 +1611,7 @@ static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
 static __init int init_posix_cpu_timers(void)
 {
 	struct k_clock process = {
+		.name = "process_cputime",
 		.clock_getres = process_cpu_clock_getres,
 		.clock_get = process_cpu_clock_get,
 		.clock_set = do_posix_clock_nosettime,
@@ -1619,6 +1620,7 @@ static __init int init_posix_cpu_timers(void)
 		.nsleep_restart = process_cpu_nsleep_restart,
 	};
 	struct k_clock thread = {
+		.name = "thread_cputime",
 		.clock_getres = thread_cpu_clock_getres,
 		.clock_get = thread_cpu_clock_get,
 		.clock_set = do_posix_clock_nosettime,
kernel/posix-timers.c +40 -0
index 67fba5c..719aa11 100644
@@ -46,6 +46,7 @@
 #include <linux/wait.h>
 #include <linux/workqueue.h>
 #include <linux/module.h>
+#include <linux/device.h>
 
 /*
  * Management arrays for POSIX timers.	 Timers are kept in slab memory
@@ -135,6 +136,8 @@ static struct k_clock posix_clocks[MAX_CLOCKS];
 static DECLARE_BITMAP(clocks_map, MAX_CLOCKS);
 static DEFINE_MUTEX(clocks_mux); /* protects 'posix_clocks' and 'clocks_map' */
 
+static struct class *timesource_class;
+
 /*
  * These ones are defined below.
  */
@@ -271,20 +274,40 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp
 	*tp = ktime_to_timespec(KTIME_LOW_RES);
 	return 0;
 }
+
+/*
+ * sysfs attributes
+ */
+
+static ssize_t show_clock_id(struct device *dev,
+			     struct device_attribute *attr, char *page)
+{
+	struct k_clock *kc = dev_get_drvdata(dev);
+	return snprintf(page, PAGE_SIZE-1, "%d\n", kc->id);
+}
+
+static struct device_attribute timesource_dev_attrs[] = {
+	__ATTR(id,   0444, show_clock_id,   NULL),
+	__ATTR_NULL,
+};
+
 /*
  * Initialize everything, well, just everything in Posix clocks/timers ;)
  */
 static __init int init_posix_timers(void)
 {
 	struct k_clock clock_realtime = {
+		.name = "realtime",
 		.clock_getres = hrtimer_get_res,
 	};
 	struct k_clock clock_monotonic = {
+		.name = "monotonic",
 		.clock_getres = hrtimer_get_res,
 		.clock_get = posix_ktime_get_ts,
 		.clock_set = do_posix_clock_nosettime,
 	};
 	struct k_clock clock_monotonic_raw = {
+		.name = "monotonic_raw",
 		.clock_getres = hrtimer_get_res,
 		.clock_get = posix_get_monotonic_raw,
 		.clock_set = do_posix_clock_nosettime,
@@ -292,6 +315,7 @@ static __init int init_posix_timers(void)
 		.nsleep = no_nsleep,
 	};
 	struct k_clock clock_realtime_coarse = {
+		.name = "realtime_coarse",
 		.clock_getres = posix_get_coarse_res,
 		.clock_get = posix_get_realtime_coarse,
 		.clock_set = do_posix_clock_nosettime,
@@ -299,6 +323,7 @@ static __init int init_posix_timers(void)
 		.nsleep = no_nsleep,
 	};
 	struct k_clock clock_monotonic_coarse = {
+		.name = "monotonic_coarse",
 		.clock_getres = posix_get_coarse_res,
 		.clock_get = posix_get_monotonic_coarse,
 		.clock_set = do_posix_clock_nosettime,
@@ -306,6 +331,13 @@ static __init int init_posix_timers(void)
 		.nsleep = no_nsleep,
 	};
 
+	timesource_class = class_create(NULL, "timesource");
+	if (IS_ERR(timesource_class)) {
+		pr_err("posix-timers: failed to allocate timesource class\n");
+		return PTR_ERR(timesource_class);
+	}
+	timesource_class->dev_attrs = timesource_dev_attrs;
+
 	register_posix_clock(CLOCK_REALTIME, &clock_realtime);
 	register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
 	register_posix_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
@@ -500,6 +532,14 @@ int register_posix_clock(const clockid_t id, struct k_clock *clock)
 	kc = &posix_clocks[id];
 	*kc = *clock;
 	kc->id = id;
+	kc->dev = device_create(timesource_class, NULL, MKDEV(0, 0),
+				kc, "%s", kc->name);
+	if (IS_ERR(kc->dev)) {
+		pr_err("failed to create device clock_id %d\n", id);
+		err = PTR_ERR(kc->dev);
+		goto out;
+	}
+	dev_set_drvdata(kc->dev, kc);
 	set_bit(id, clocks_map);
 out:
 	mutex_unlock(&clocks_mux);
Signature · 1 line
1.7.0.4
author maintainer
#12 lore raw

PATCH v6 4/8 ptp: Added a brand new class driver for ptp clocks.

In reply to Richard Cochran

This patch adds an infrastructure for hardware clocks that implement IEEE 1588, the Precision Time Protocol (PTP). A class driver offers a registration method to particular hardware clock drivers. Each clock is presented as a standard POSIX clock.

The ancillary clock features are exposed in two different ways, via the sysfs and by a character device.

---
 Documentation/ABI/testing/sysfs-ptp |  107 ++++++++++
 Documentation/ptp/ptp.txt           |   94 +++++++++
 Documentation/ptp/testptp.c         |  358 ++++++++++++++++++++++++++++++++
 Documentation/ptp/testptp.mk        |   33 +++
 drivers/Kconfig                     |    2 +
 drivers/Makefile                    |    1 +
 drivers/ptp/Kconfig                 |   27 +++
 drivers/ptp/Makefile                |    6 +
 drivers/ptp/ptp_chardev.c           |  178 ++++++++++++++++
 drivers/ptp/ptp_clock.c             |  382 +++++++++++++++++++++++++++++++++++
 drivers/ptp/ptp_private.h           |   64 ++++++
 drivers/ptp/ptp_sysfs.c             |  235 +++++++++++++++++++++
 include/linux/Kbuild                |    1 +
 include/linux/ptp_clock.h           |   79 +++++++
 include/linux/ptp_clock_kernel.h    |  139 +++++++++++++
 15 files changed, 1706 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-ptp
 create mode 100644 Documentation/ptp/ptp.txt
 create mode 100644 Documentation/ptp/testptp.c
 create mode 100644 Documentation/ptp/testptp.mk
 create mode 100644 drivers/ptp/Kconfig
 create mode 100644 drivers/ptp/Makefile
 create mode 100644 drivers/ptp/ptp_chardev.c
 create mode 100644 drivers/ptp/ptp_clock.c
 create mode 100644 drivers/ptp/ptp_private.h
 create mode 100644 drivers/ptp/ptp_sysfs.c
 create mode 100644 include/linux/ptp_clock.h
 create mode 100644 include/linux/ptp_clock_kernel.h
Documentation/ABI/testing/sysfs-ptp new file +107 -0
new file mode 100644
index 0000000..47142ce
@@ -0,0 +1,107 @@
+What:		/sys/class/ptp/
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This directory contains files and directories
+		providing a standardized interface to the ancillary
+		features of PTP hardware clocks.
+
+What:		/sys/class/ptp/ptpN/
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This directory contains the attributes of the Nth PTP
+		hardware clock registered into the PTP class driver
+		subsystem.
+
+What:		/sys/class/ptp/ptpN/clock_id
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the POSIX clock ID (a non-negative
+		integer) corresponding to the PTP hardware clock. This
+		value may be passed as the first argument to the POSIX
+		clock and timer system calls. See man CLOCK_GETRES(2)
+		and TIMER_CREATE(2).
+
+What:		/sys/class/ptp/ptpN/clock_name
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the name of the PTP hardware clock
+		as a human readable string.
+
+What:		/sys/class/ptp/ptpN/max_adjustment
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the PTP hardware clock's maximum
+		frequency adjustment value (a positive integer) in
+		parts per billion.
+
+What:		/sys/class/ptp/ptpN/n_alarms
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the number of periodic or one shot
+		alarms offer by the PTP hardware clock.
+
+What:		/sys/class/ptp/ptpN/n_external_timestamps
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the number of external timestamp
+		channels offered by the PTP hardware clock.
+
+What:		/sys/class/ptp/ptpN/n_periodic_outputs
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file contains the number of programmable periodic
+		output channels offered by the PTP hardware clock.
+
+What:		/sys/class/ptp/ptpN/pps_avaiable
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file indicates whether the PTP hardware clock
+		supports a Pulse Per Second to the host CPU. Reading
+		"1" means that the PPS is supported, while "0" means
+		not supported.
+
+What:		/sys/class/ptp/ptpN/extts_enable
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This write-only file enables or disables external
+		timestamps. To enable external timestamps, write the
+		channel index followed by a "1" into the file.
+		To disable external timestamps, write the channel
+		index followed by a "0" into the file.
+
+What:		/sys/class/ptp/ptpN/fifo
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This file provides timestamps on external events, in
+		the form of three integers: channel index, seconds,
+		and nanoseconds.
+
+What:		/sys/class/ptp/ptpN/period
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This write-only file enables or disables periodic
+		outputs. To enable a periodic output, write three
+		integers into the file: channel index, seconds, and
+		nanoseconds. To disable a periodic output, set seconds
+		and nanoseconds to zero.
+
+What:		/sys/class/ptp/ptpN/pps_enable
+Date:		September 2010
+Contact:	Richard Cochran <[email protected]>
+Description:
+		This write-only file enables or disables delivery of
+		PPS events to the Linux PPS subsystem. To enable PPS
+		events, write a "1" into the file. To disable events,
+		write a "0" into the file.
Documentation/ptp/ptp.txt new file +94 -0

94 lines not on this page · raw message

Documentation/ptp/testptp.c new file +358 -0

358 lines not on this page · raw message

Documentation/ptp/testptp.mk new file +33 -0
new file mode 100644
index 0000000..4ef2d97
@@ -0,0 +1,33 @@
+# PTP 1588 clock support - User space test program
+#
+# Copyright (C) 2010 OMICRON electronics GmbH
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+CC        = $(CROSS_COMPILE)gcc
+INC       = -I$(KBUILD_OUTPUT)/usr/include
+CFLAGS    = -Wall $(INC)
+LDLIBS    = -lrt
+PROGS     = testptp
+
+all: $(PROGS)
+
+testptp: testptp.o
+
+clean:
+	rm -f testptp.o
+
+distclean: clean
+	rm -f $(PROGS)
drivers/Kconfig +2 -0
index a2b902f..774fbd7 100644
@@ -52,6 +52,8 @@ source "drivers/spi/Kconfig"
 
 source "drivers/pps/Kconfig"
 
+source "drivers/ptp/Kconfig"
+
 source "drivers/gpio/Kconfig"
 
 source "drivers/w1/Kconfig"
drivers/Makefile +1 -0
index a2aea53..802d100 100644
@@ -76,6 +76,7 @@ obj-$(CONFIG_I2O)		+= message/
 obj-$(CONFIG_RTC_LIB)		+= rtc/
 obj-y				+= i2c/ media/
 obj-$(CONFIG_PPS)		+= pps/
+obj-$(CONFIG_PTP_1588_CLOCK)	+= ptp/
 obj-$(CONFIG_W1)		+= w1/
 obj-$(CONFIG_POWER_SUPPLY)	+= power/
 obj-$(CONFIG_HWMON)		+= hwmon/
drivers/ptp/Kconfig new file +27 -0
new file mode 100644
index 0000000..17be208
@@ -0,0 +1,27 @@
+#
+# PTP clock support configuration
+#
+
+menu "PTP clock support"
+
+config PTP_1588_CLOCK
+	tristate "PTP clock support"
+	depends on EXPERIMENTAL
+	depends on PPS
+	help
+	  The IEEE 1588 standard defines a method to precisely
+	  synchronize distributed clocks over Ethernet networks. The
+	  standard defines a Precision Time Protocol (PTP), which can
+	  be used to achieve synchronization within a few dozen
+	  microseconds. In addition, with the help of special hardware
+	  time stamping units, it can be possible to achieve
+	  synchronization to within a few hundred nanoseconds.
+
+	  This driver adds support for PTP clocks as character
+	  devices. If you want to use a PTP clock, then you should
+	  also enable at least one clock driver as well.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ptp.
+
+endmenu
drivers/ptp/Makefile new file +6 -0
new file mode 100644
index 0000000..480e2af
@@ -0,0 +1,6 @@
+#
+# Makefile for PTP 1588 clock support.
+#
+
+ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
+obj-$(CONFIG_PTP_1588_CLOCK)		+= ptp.o
drivers/ptp/ptp_chardev.c new file +178 -0

178 lines not on this page · raw message

drivers/ptp/ptp_clock.c new file +382 -0

382 lines not on this page · raw message

drivers/ptp/ptp_private.h new file +64 -0

64 lines not on this page · raw message

drivers/ptp/ptp_sysfs.c new file +235 -0

235 lines not on this page · raw message

include/linux/Kbuild +1 -0
index 626b629..1e2468a 100644
@@ -295,6 +295,7 @@ header-y += ppp-comp.h
 header-y += ppp_defs.h
 header-y += pps.h
 header-y += prctl.h
+header-y += ptp_clock.h
 header-y += ptrace.h
 header-y += qnx4_fs.h
 header-y += qnxtypes.h
include/linux/ptp_clock.h new file +79 -0

79 lines not on this page · raw message

include/linux/ptp_clock_kernel.h new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
1.7.0.4
author maintainer
#13 lore raw

PATCH v6 5/8 ptp: Added a simulated PTP hardware clock.

In reply to Richard Cochran

This patch adds a driver that simulates a PTP hardware clock. The driver serves as a simple example for writing real clock driver and can be used for testing the PTP clock API.

The basic clock operations are implemented using the system clock, and the ancillary clock operations are simulated.

---
 drivers/ptp/Kconfig     |   14 ++++
 drivers/ptp/Makefile    |    1 +
 drivers/ptp/ptp_linux.c |  165 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/time/ntp.c       |    2 +
 4 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 drivers/ptp/ptp_linux.c
drivers/ptp/Kconfig +14 -0
index 17be208..94f329f 100644
@@ -24,4 +24,18 @@ config PTP_1588_CLOCK
 	  To compile this driver as a module, choose M here: the module
 	  will be called ptp.
 
+config PTP_1588_CLOCK_LINUX
+	tristate "Simulated PTP clock"
+	depends on PTP_1588_CLOCK
+	help
+	  This driver adds support for a simulated PTP clock. It
+	  implements the basic clock operations by using the standard
+	  Linux system time. The driver simulates the ancillary clock
+	  operations. This clock can be used to test PTP programs
+	  provided they use software time stamps for the PTP Ethernet
+	  packets.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ptp_linux.
+
 endmenu
drivers/ptp/Makefile +1 -0
index 480e2af..266d4f2 100644
@@ -4,3 +4,4 @@
 
 ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
 obj-$(CONFIG_PTP_1588_CLOCK)		+= ptp.o
+obj-$(CONFIG_PTP_1588_CLOCK_LINUX)	+= ptp_linux.o
drivers/ptp/ptp_linux.c new file +165 -0

165 lines not on this page · raw message

kernel/time/ntp.c +2 -0
index c631168..eba3bcf 100644
@@ -14,6 +14,7 @@
 #include <linux/timex.h>
 #include <linux/time.h>
 #include <linux/mm.h>
+#include <linux/module.h>
 
 /*
  * NTP timekeeping variables:
@@ -535,6 +536,7 @@ int do_adjtimex(struct timex *txc)
 
 	return result;
 }
+EXPORT_SYMBOL(do_adjtimex);
 
 static int __init ntp_tick_adj_setup(char *str)
 {
Signature · 1 line
1.7.0.4
author maintainer
#14 lore raw

PATCH v6 6/8 ptp: Added a clock that uses the eTSEC found on the MPC85xx.

In reply to Richard Cochran

The eTSEC includes a PTP clock with quite a few features. This patch adds support for the basic clock adjustment functions, plus two external time stamps, one alarm, and the PPS callback.

---
 Documentation/powerpc/dts-bindings/fsl/tsec.txt |   57 +++
 arch/powerpc/boot/dts/mpc8313erdb.dts           |   14 +
 arch/powerpc/boot/dts/mpc8572ds.dts             |   14 +
 arch/powerpc/boot/dts/p2020ds.dts               |   14 +
 arch/powerpc/boot/dts/p2020rdb.dts              |   14 +
 drivers/net/Makefile                            |    1 +
 drivers/net/gianfar_ptp.c                       |  447 +++++++++++++++++++++++
 drivers/net/gianfar_ptp_reg.h                   |  113 ++++++
 drivers/ptp/Kconfig                             |   13 +
 9 files changed, 687 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/gianfar_ptp.c
 create mode 100644 drivers/net/gianfar_ptp_reg.h
Documentation/powerpc/dts-bindings/fsl/tsec.txt +57 -0
index edb7ae1..f6edbb8 100644
@@ -74,3 +74,60 @@ Example:
 		interrupt-parent = <&mpic>;
 		phy-handle = <&phy0>
 	};
+
+* Gianfar PTP clock nodes
+
+General Properties:
+
+  - compatible   Should be "fsl,etsec-ptp"
+  - reg          Offset and length of the register set for the device
+  - interrupts   There should be at least two interrupts. Some devices
+                 have as many as four PTP related interrupts.
+
+Clock Properties:
+
+  - tclk-period  Timer reference clock period in nanoseconds.
+  - tmr-prsc     Prescaler, divides the output clock.
+  - tmr-add      Frequency compensation value.
+  - cksel        0= external clock, 1= eTSEC system clock, 3= RTC clock input.
+                 Currently the driver only supports choice "1".
+  - tmr-fiper1   Fixed interval period pulse generator.
+  - tmr-fiper2   Fixed interval period pulse generator.
+  - max-adj      Maximum frequency adjustment in parts per billion.
+
+  These properties set the operational parameters for the PTP
+  clock. You must choose these carefully for the clock to work right.
+  Here is how to figure good values:
+
+  TimerOsc     = system clock               MHz
+  tclk_period  = desired clock period       nanoseconds
+  NominalFreq  = 1000 / tclk_period         MHz
+  FreqDivRatio = TimerOsc / NominalFreq     (must be greater that 1.0)
+  tmr_add      = ceil(2^32 / FreqDivRatio)
+  OutputClock  = NominalFreq / tmr_prsc     MHz
+  PulseWidth   = 1 / OutputClock            microseconds
+  FiperFreq1   = desired frequency in Hz
+  FiperDiv1    = 1000000 * OutputClock / FiperFreq1
+  tmr_fiper1   = tmr_prsc * tclk_period * FiperDiv1 - tclk_period
+  max_adj      = 1000000000 * (FreqDivRatio - 1.0) - 1
+
+  The calculation for tmr_fiper2 is the same as for tmr_fiper1. The
+  driver expects that tmr_fiper1 will be correctly set to produce a 1
+  Pulse Per Second (PPS) signal, since this will be offered to the PPS
+  subsystem to synchronize the Linux clock.
+
+Example:
+
+	ptp_clock@24E00 {
+		compatible = "fsl,etsec-ptp";
+		reg = <0x24E00 0xB0>;
+		interrupts = <12 0x8 13 0x8>;
+		interrupt-parent = < &ipic >;
+		tclk-period = <10>;
+		tmr-prsc    = <100>;
+		tmr-add     = <0x999999A4>;
+		cksel       = <0x1>;
+		tmr-fiper1  = <0x3B9AC9F6>;
+		tmr-fiper2  = <0x00018696>;
+		max-adj     = <659999998>;
+	};
arch/powerpc/boot/dts/mpc8313erdb.dts +14 -0
index 183f2aa..85a7eaa 100644
@@ -208,6 +208,20 @@
 			sleep = <&pmc 0x00300000>;
 		};
 
+		ptp_clock@24E00 {
+			compatible = "fsl,etsec-ptp";
+			reg = <0x24E00 0xB0>;
+			interrupts = <12 0x8 13 0x8>;
+			interrupt-parent = < &ipic >;
+			tclk-period = <10>;
+			tmr-prsc    = <100>;
+			tmr-add     = <0x999999A4>;
+			cksel       = <0x1>;
+			tmr-fiper1  = <0x3B9AC9F6>;
+			tmr-fiper2  = <0x00018696>;
+			max-adj     = <659999998>;
+		};
+
 		enet0: ethernet@24000 {
 			#address-cells = <1>;
 			#size-cells = <1>;
arch/powerpc/boot/dts/mpc8572ds.dts +14 -0
index cafc128..74208cd 100644
@@ -324,6 +324,20 @@
 			};
 		};
 
+		ptp_clock@24E00 {
+			compatible = "fsl,etsec-ptp";
+			reg = <0x24E00 0xB0>;
+			interrupts = <68 2 69 2 70 2 71 2>;
+			interrupt-parent = < &mpic >;
+			tclk-period = <5>;
+			tmr-prsc = <200>;
+			tmr-add = <0xAAAAAAAB>;
+			cksel = <1>;
+			tmr-fiper1 = <0x3B9AC9FB>;
+			tmr-fiper2 = <0x3B9AC9FB>;
+			max-adj = <499999999>;
+		};
+
 		enet0: ethernet@24000 {
 			#address-cells = <1>;
 			#size-cells = <1>;
arch/powerpc/boot/dts/p2020ds.dts +14 -0
index 1101914..39d73bb 100644
@@ -336,6 +336,20 @@
 			phy_type = "ulpi";
 		};
 
+		ptp_clock@24E00 {
+			compatible = "fsl,etsec-ptp";
+			reg = <0x24E00 0xB0>;
+			interrupts = <68 2 69 2 70 2>;
+			interrupt-parent = < &mpic >;
+			tclk-period = <5>;
+			tmr-prsc = <200>;
+			tmr-add = <0xCCCCCCCD>;
+			cksel = <1>;
+			tmr-fiper1 = <0x3B9AC9FB>;
+			tmr-fiper2 = <0x0001869B>;
+			max-adj = <249999999>;
+		};
+
 		enet0: ethernet@24000 {
 			#address-cells = <1>;
 			#size-cells = <1>;
arch/powerpc/boot/dts/p2020rdb.dts +14 -0
index da4cb0d..5498fb9 100644
@@ -396,6 +396,20 @@
 			phy_type = "ulpi";
 		};
 
+		ptp_clock@24E00 {
+			compatible = "fsl,etsec-ptp";
+			reg = <0x24E00 0xB0>;
+			interrupts = <68 2 69 2 70 2>;
+			interrupt-parent = < &mpic >;
+			tclk-period = <5>;
+			tmr-prsc = <200>;
+			tmr-add = <0xCCCCCCCD>;
+			cksel = <1>;
+			tmr-fiper1 = <0x3B9AC9FB>;
+			tmr-fiper2 = <0x0001869B>;
+			max-adj = <249999999>;
+		};
+
 		enet0: ethernet@24000 {
 			#address-cells = <1>;
 			#size-cells = <1>;
drivers/net/Makefile +1 -0
index 3e8f150..46737b3 100644
@@ -29,6 +29,7 @@ obj-$(CONFIG_ATL2) += atlx/
 obj-$(CONFIG_ATL1E) += atl1e/
 obj-$(CONFIG_ATL1C) += atl1c/
 obj-$(CONFIG_GIANFAR) += gianfar_driver.o
+obj-$(CONFIG_PTP_1588_CLOCK_GIANFAR) += gianfar_ptp.o
 obj-$(CONFIG_TEHUTI) += tehuti.o
 obj-$(CONFIG_ENIC) += enic/
 obj-$(CONFIG_JME) += jme.o
drivers/net/gianfar_ptp.c new file +447 -0

447 lines not on this page · raw message

drivers/net/gianfar_ptp_reg.h new file +113 -0

113 lines not on this page · raw message

drivers/ptp/Kconfig +13 -0
index 94f329f..764a844 100644
@@ -38,4 +38,17 @@ config PTP_1588_CLOCK_LINUX
 	  To compile this driver as a module, choose M here: the module
 	  will be called ptp_linux.
 
+config PTP_1588_CLOCK_GIANFAR
+	tristate "Freescale eTSEC as PTP clock"
+	depends on PTP_1588_CLOCK
+	depends on GIANFAR
+	help
+	  This driver adds support for using the eTSEC as a PTP
+	  clock. This clock is only useful if your PTP programs are
+	  getting hardware time stamps on the PTP Ethernet packets
+	  using the SO_TIMESTAMPING API.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called gianfar_ptp.
+
 endmenu
Signature · 1 line
1.7.0.4
#15 lore raw

Re: [PATCH 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx.

Quoted 25 lines · Richard Cochran
Documentation/powerpc/dts-bindings/fsl/tsec.txt +25 -0
+* Gianfar PTP clock nodes
+
+General Properties:
+
+  - compatible   Should be "fsl,etsec-ptp"
+  - reg          Offset and length of the register set for the device
+  - interrupts   There should be at least two interrupts. Some devices
+                 have as many as four PTP related interrupts.
+
+Clock Properties:
+
+  - tclk-period  Timer reference clock period in nanoseconds.
+  - tmr-prsc     Prescaler, divides the output clock.
+  - tmr-add      Frequency compensation value.
+  - cksel        0= external clock, 1= eTSEC system clock, 3= RTC clock input.
+                 Currently the driver only supports choice "1".
+  - tmr-fiper1   Fixed interval period pulse generator.
+  - tmr-fiper2   Fixed interval period pulse generator.
+  - max-adj      Maximum frequency adjustment in parts per billion.
+
+  These properties set the operational parameters for the PTP
+  clock. You must choose these carefully for the clock to work right.
+  Here is how to figure good values:
+
+  TimerOsc     = system clock               MHz

Documentation/powerpc/dts-bindings/fsl/tsec.txt Example: · line 112

Quoted 10 lines
Documentation/powerpc/dts-bindings/fsl/tsec.txt +10 -0
+  tclk_period  = desired clock period       nanoseconds
+  NominalFreq  = 1000 / tclk_period         MHz
+  FreqDivRatio = TimerOsc / NominalFreq     (must be greater that 1.0)
+  tmr_add      = ceil(2^32 / FreqDivRatio)
+  OutputClock  = NominalFreq / tmr_prsc     MHz
+  PulseWidth   = 1 / OutputClock            microseconds
+  FiperFreq1   = desired frequency in Hz
+  FiperDiv1    = 1000000 * OutputClock / FiperFreq1
+  tmr_fiper1   = tmr_prsc * tclk_period * FiperDiv1 - tclk_period
+  max_adj      = 1000000000 * (FreqDivRatio - 1.0) - 1

Great stuff for clock synchronization...

Documentation/powerpc/dts-bindings/fsl/tsec.txt Example: · line 117

Quoted 4 lines
Documentation/powerpc/dts-bindings/fsl/tsec.txt +4 -0
+  The calculation for tmr_fiper2 is the same as for tmr_fiper1. The
+  driver expects that tmr_fiper1 will be correctly set to produce a 1
+  Pulse Per Second (PPS) signal, since this will be offered to the PPS
+  subsystem to synchronize the Linux clock.

Argh. And conceptually completely screwed up. Why go through the PPS subsystem if you can directly tune the system clock based on a number of the cool periodic clock features that you have above? See how the other clocks do that easily? Look into drivers/clocksource. Add it there.

Please do not introduce useless additional layers for clock sync. Load these ptp clocks like the other regular clock modules and make them sync system time like any other clock.

Really guys: I want a PTP solution! Now! And not some idiotic additional kernel layers that just pass bits around because its so much fun and screws up clock accurary in due to the latency noise introduced while having so much fun with the bits.

#16 lore raw

Re: [PATCH 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx.

Quoted 3 lines

Please do not introduce useless additional layers for clock sync. Load these ptp clocks like the other regular clock modules and make them sync system time like any other clock.

I don't think you understand PTP. PTP has masters, a system can need to be honouring multiple conflicting masters at once.

Quoted 4 lines

Really guys: I want a PTP solution! Now! And not some idiotic additional kernel layers that just pass bits around because its so much fun and screws up clock accurary in due to the latency noise introduced while having so much fun with the bits.

There are some interesting complications in putting a PTP sync interface in kernel.

#17 lore raw

Re: [PATCH 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx.

Quoted 6 lines · Alan Cox
Quoted 3 lines

Please do not introduce useless additional layers for clock sync. Load these ptp clocks like the other regular clock modules and make them sync system time like any other clock.

I don't think you understand PTP. PTP has masters, a system can need to be honouring multiple conflicting masters at once.

The upshot of it all has to be some synchronized notion of time regardless of how many other things are going on under the hood. And the spec here suggests a hardware able to generate periodic accurate events that can be used to sync system time.

Quoted 7 lines
Quoted 4 lines

Really guys: I want a PTP solution! Now! And not some idiotic additional kernel layers that just pass bits around because its so much fun and screws up clock accurary in due to the latency noise introduced while having so much fun with the bits.

There are some interesting complications in putting a PTP sync interface in kernel.

If the PTP logic internally has to juggle multiple clocks then that is a complication for the driver ok. In any case the driver ultimately has to provide *one* source of time for the system to sync to.

#18 lore raw

Re: [PATCH 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx.

In reply to Alan Cox
Quoted 6 lines · Alan Cox
Quoted 3 lines

Please do not introduce useless additional layers for clock sync. Load these ptp clocks like the other regular clock modules and make them sync system time like any other clock.

I don't think you understand PTP. PTP has masters, a system can need to be honouring multiple conflicting masters at once.

AFAIK the master's should not be conflicting. The Best Master Clock algorithm (BMC) defined in IEEE1588 selects the best master clock. This clock distributes its notion of time on the network while the other masters, that is the other clocks/nodes that are configured to potentially become a master, keep quiet. So usually we will only have one source of time (the master clock selected by the BMC) and we will steer our single PHC (PTP hardware clock) to follow this master (Of course there may be use-cases that require more than one PTP clock, e.g., for research purposes).

However, if the clock selected by the BMC is switched off, loses its network connection..., the second best clock is selected by the BMC and becomes master. This clock may be less accurate and thus our slave clock has to switch from one notion of time to another. Is that the conflict you mentioned?

Christian

#19 lore raw

Re: [PATCH 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx.

Quoted 5 lines

However, if the clock selected by the BMC is switched off, loses its network connection..., the second best clock is selected by the BMC and becomes master. This clock may be less accurate and thus our slave clock has to switch from one notion of time to another. Is that the conflict you mentioned?

No you get situations where you have policy reasons for trusting particular clocks for particular things.

So you may have a PTP or NTP clock providing basic system time but also have other PTP clocks that are actually being used for synchronization work.

With NTP it's not so far been a big issue - NTP isn't used for industrial high precision control and the cases we end up with multiple NTP clocks it's on a virtualised systems where it is isolated.

With high precision clocks you sometimes want to honour a specific PTP time source and use it rather than try and merge it with your other time sources (which may differ from the equipment elsewhere). What matters is things like all the parts of a several mile long conveyor belt of hot steel slab stopping at the same moment [1].

In lots of control applications you've got assorted different time planes which wish to talk their own time and you have to accept it, so we need to support that kind of use.

I agree entirely the normal boring 'I installed my distro and..' case for PTP or for NTP is merging all the sources, running the algorithm and using the system time for it. Likewise almost all "normal" application code will be watching system time.

Alan
[1] Which was my first encounter with writing Vax/VMS assembly language

author maintainer
#20 lore raw

Re: [PATCH 6/8] ptp: Added a clock that uses the eTSEC found on the MPC85xx.

In reply to Christoph Lameter
Quoted 29 lines · Christoph Lameter
Quoted 5 lines · Richard Cochran
Documentation/powerpc/dts-bindings/fsl/tsec.txt +5 -0
+  These properties set the operational parameters for the PTP
+  clock. You must choose these carefully for the clock to work right.
+  Here is how to figure good values:
+
+  TimerOsc     = system clock               MHz

Documentation/powerpc/dts-bindings/fsl/tsec.txt Example: · line 112

Quoted 10 lines
Documentation/powerpc/dts-bindings/fsl/tsec.txt +10 -0
+  tclk_period  = desired clock period       nanoseconds
+  NominalFreq  = 1000 / tclk_period         MHz
+  FreqDivRatio = TimerOsc / NominalFreq     (must be greater that 1.0)
+  tmr_add      = ceil(2^32 / FreqDivRatio)
+  OutputClock  = NominalFreq / tmr_prsc     MHz
+  PulseWidth   = 1 / OutputClock            microseconds
+  FiperFreq1   = desired frequency in Hz
+  FiperDiv1    = 1000000 * OutputClock / FiperFreq1
+  tmr_fiper1   = tmr_prsc * tclk_period * FiperDiv1 - tclk_period
+  max_adj      = 1000000000 * (FreqDivRatio - 1.0) - 1

Great stuff for clock synchronization...

Documentation/powerpc/dts-bindings/fsl/tsec.txt Example: · line 117

Quoted 4 lines
Documentation/powerpc/dts-bindings/fsl/tsec.txt +4 -0
+  The calculation for tmr_fiper2 is the same as for tmr_fiper1. The
+  driver expects that tmr_fiper1 will be correctly set to produce a 1
+  Pulse Per Second (PPS) signal, since this will be offered to the PPS
+  subsystem to synchronize the Linux clock.

Argh. And conceptually completely screwed up. Why go through the PPS subsystem if you can directly tune the system clock based on a number of the cool periodic clock features that you have above? See how the other clocks do that easily? Look into drivers/clocksource. Add it there.

Quoted 8 lines

Please do not introduce useless additional layers for clock sync. Load these ptp clocks like the other regular clock modules and make them sync system time like any other clock.

Really guys: I want a PTP solution! Now! And not some idiotic additional kernel layers that just pass bits around because its so much fun and screws up clock accurary in due to the latency noise introduced while having so much fun with the bits.

(Sorry if this message comes twice. Mutt/Gmail flaked out again.)

I think you misunderstood this particular patch. The device tree parameters are really just internal driver stuff. When you use the eTSEC, you must make some design choices at the same time as you plan your board. The proper values for some of the eTSEC registers are based on these design choices. Since the Freescale documentation is a bit thin on this, I added a few notes to help my fellow board designers.

Because these values are closely related to the board itself, I think that it is nicer to configure them via the device tree than using either CONFIG_ variables or platform data.

Richard

author maintainer
#21 lore raw

PATCH v6 7/8 ptp: Added a clock driver for the IXP46x.

In reply to Richard Cochran

This patch adds a driver for the hardware time stamping unit found on the IXP465. The basic clock operations and an external trigger are implemented.

---
 arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h |   78 ++++++
 drivers/net/arm/ixp4xx_eth.c                  |  191 ++++++++++++++
 drivers/ptp/Kconfig                           |   13 +
 drivers/ptp/Makefile                          |    1 +
 drivers/ptp/ptp_ixp46x.c                      |  345 +++++++++++++++++++++++++
 5 files changed, 628 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h
 create mode 100644 drivers/ptp/ptp_ixp46x.c
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h new file +78 -0
new file mode 100644
index 0000000..729a6b2
@@ -0,0 +1,78 @@
+/*
+ * PTP 1588 clock using the IXP46X
+ *
+ * Copyright (C) 2010 OMICRON electronics GmbH
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef _IXP46X_TS_H_
+#define _IXP46X_TS_H_
+
+#define DEFAULT_ADDEND 0xF0000029
+#define TICKS_NS_SHIFT 4
+
+struct ixp46x_channel_ctl {
+	u32 Ch_Control; /* 0x40 Time Synchronization Channel Control */
+	u32 Ch_Event;   /* 0x44 Time Synchronization Channel Event */
+	u32 TxSnapLo;   /* 0x48 Transmit Snapshot Low Register */
+	u32 TxSnapHi;   /* 0x4C Transmit Snapshot High Register */
+	u32 RxSnapLo;   /* 0x50 Receive Snapshot Low Register */
+	u32 RxSnapHi;   /* 0x54 Receive Snapshot High Register */
+	u32 SrcUUIDLo;  /* 0x58 Source UUID0 Low Register */
+	u32 SrcUUIDHi;  /* 0x5C Sequence Identifier/Source UUID0 High */
+};
+
+struct ixp46x_ts_regs {
+	u32 Control;     /* 0x00 Time Sync Control Register */
+	u32 Event;       /* 0x04 Time Sync Event Register */
+	u32 Addend;      /* 0x08 Time Sync Addend Register */
+	u32 Accum;       /* 0x0C Time Sync Accumulator Register */
+	u32 Test;        /* 0x10 Time Sync Test Register */
+	u32 Unused;      /* 0x14 */
+	u32 RSysTime_Lo; /* 0x18 RawSystemTime_Low Register */
+	u32 RSysTimeHi;  /* 0x1C RawSystemTime_High Register */
+	u32 SysTimeLo;   /* 0x20 SystemTime_Low Register */
+	u32 SysTimeHi;   /* 0x24 SystemTime_High Register */
+	u32 TrgtLo;      /* 0x28 TargetTime_Low Register */
+	u32 TrgtHi;      /* 0x2C TargetTime_High Register */
+	u32 ASMSLo;      /* 0x30 Auxiliary Slave Mode Snapshot Low  */
+	u32 ASMSHi;      /* 0x34 Auxiliary Slave Mode Snapshot High */
+	u32 AMMSLo;      /* 0x38 Auxiliary Master Mode Snapshot Low */
+	u32 AMMSHi;      /* 0x3C Auxiliary Master Mode Snapshot High */
+
+	struct ixp46x_channel_ctl channel[3];
+};
+
+/* 0x00 Time Sync Control Register Bits */
+#define TSCR_AMM (1<<3)
+#define TSCR_ASM (1<<2)
+#define TSCR_TTM (1<<1)
+#define TSCR_RST (1<<0)
+
+/* 0x04 Time Sync Event Register Bits */
+#define TSER_SNM (1<<3)
+#define TSER_SNS (1<<2)
+#define TTIPEND  (1<<1)
+
+/* 0x40 Time Synchronization Channel Control Register Bits */
+#define MASTER_MODE   (1<<0)
+#define TIMESTAMP_ALL (1<<1)
+
+/* 0x44 Time Synchronization Channel Event Register Bits */
+#define TX_SNAPSHOT_LOCKED (1<<0)
+#define RX_SNAPSHOT_LOCKED (1<<1)
+
+#endif
drivers/net/arm/ixp4xx_eth.c +191 -0

242 lines not on this page · raw message

drivers/ptp/Kconfig +13 -0
index 764a844..e338ded 100644
@@ -51,4 +51,17 @@ config PTP_1588_CLOCK_GIANFAR
 	  To compile this driver as a module, choose M here: the module
 	  will be called gianfar_ptp.
 
+config PTP_1588_CLOCK_IXP46X
+	tristate "Intel IXP46x as PTP clock"
+	depends on PTP_1588_CLOCK
+	depends on IXP4XX_ETH
+	help
+	  This driver adds support for using the IXP46X as a PTP
+	  clock. This clock is only useful if your PTP programs are
+	  getting hardware time stamps on the PTP Ethernet packets
+	  using the SO_TIMESTAMPING API.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ptp_ixp46x.
+
 endmenu
drivers/ptp/Makefile +1 -0
index 266d4f2..8b7848b 100644
@@ -5,3 +5,4 @@
 ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
 obj-$(CONFIG_PTP_1588_CLOCK)		+= ptp.o
 obj-$(CONFIG_PTP_1588_CLOCK_LINUX)	+= ptp_linux.o
+obj-$(CONFIG_PTP_1588_CLOCK_IXP46X)	+= ptp_ixp46x.o
drivers/ptp/ptp_ixp46x.c new file +345 -0

345 lines not on this page · raw message

Signature · 1 line
1.7.0.4
author maintainer
#22 lore raw

PATCH v6 8/8 ptp: Added a clock driver for the National Semiconductor PHYTER.

In reply to Richard Cochran

This patch adds support for the PTP clock found on the DP83640. The basic clock operations and one external time stamp have been implemented.

---
 drivers/net/phy/Kconfig       |   29 ++
 drivers/net/phy/Makefile      |    1 +
 drivers/net/phy/dp83640.c     |  887 +++++++++++++++++++++++++++++++++++++++++
 drivers/net/phy/dp83640_reg.h |  261 ++++++++++++
 4 files changed, 1178 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/phy/dp83640.c
 create mode 100644 drivers/net/phy/dp83640_reg.h
drivers/net/phy/Kconfig +29 -0
index eb799b3..2e6463d 100644
@@ -77,6 +77,35 @@ config NATIONAL_PHY
 	---help---
 	  Currently supports the DP83865 PHY.
 
+config DP83640_PHY
+	tristate "Driver for the National Semiconductor DP83640 PHYTER"
+	depends on PTP_1588_CLOCK
+	depends on NETWORK_PHY_TIMESTAMPING
+	---help---
+	  Supports the DP83640 PHYTER with IEEE 1588 features.
+
+	  This driver adds support for using the DP83640 as a PTP
+	  clock. This clock is only useful if your PTP programs are
+	  getting hardware time stamps on the PTP Ethernet packets
+	  using the SO_TIMESTAMPING API.
+
+	  In order for this to work, your MAC driver must also
+	  implement the skb_tx_timetamp() function.
+
+config DP83640_PHY_STATUS_FRAMES
+	bool "DP83640 Status Frames"
+	default y
+	depends on DP83640_PHY
+	---help---
+	  This option allows the DP83640 PHYTER driver to obtain time
+	  stamps from the PHY via special status frames, rather than
+	  reading over the MDIO bus. Using status frames is therefore
+	  more efficient. However, if enabled, this option will cause
+	  the driver to add a mutlicast address to the MAC.
+
+	  Say Y here, unless your MAC does not support multicast
+	  destination addresses.
+
 config STE10XP
 	depends on PHYLIB
 	tristate "Driver for STMicroelectronics STe10Xp PHYs"
drivers/net/phy/Makefile +1 -0
index 13bebab..2333215 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_FIXED_PHY)		+= fixed.o
 obj-$(CONFIG_MDIO_BITBANG)	+= mdio-bitbang.o
 obj-$(CONFIG_MDIO_GPIO)		+= mdio-gpio.o
 obj-$(CONFIG_NATIONAL_PHY)	+= national.o
+obj-$(CONFIG_DP83640_PHY)	+= dp83640.o
 obj-$(CONFIG_STE10XP)		+= ste10Xp.o
 obj-$(CONFIG_MICREL_PHY)	+= micrel.o
 obj-$(CONFIG_MDIO_OCTEON)	+= mdio-octeon.o
drivers/net/phy/dp83640.c new file +887 -0

887 lines not on this page · raw message

drivers/net/phy/dp83640_reg.h new file +261 -0

261 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#23 lore raw
In reply to Richard Cochran
Quoted 4 lines · Richard Cochran

Support for obtaining timestamps from a PHC already exists via the SO_TIMESTAMPING socket option, integrated in kernel version 2.6.30. This patch set completes the picture by allow user space programs to adjust the PHC and to control its ancillary features.

Is there a way to use the PHC as a system clock? I think the main benefit of PTP is to have syncronized time on multiple machines in a cluster. That may mean getting rid of ntp and using an in kernel PHC based way to sync time.

Quoted 2 lines

So as far as the POSIX standard is concerned, offering a clock id to represent the PHC would be acceptable.

Sure but what would you do with it? HPET timer support has no such need.

Quoted 20 lines

3.2.1 Using the POSIX Clock API
--------------------------------

Looking at the mapping from PHC operation to the POSIX clock API, we see that two of the basic clock operations, marked with *, have no POSIX equivalent. The items marked NA are peculiar to PHCs and will be discussed separately, below.

Clock Operation POSIX function
-----------------------------+-----------------------------
Set time clock_gettime
Get time clock_settime
Shift the clock *
Adjust clock frequency *
-----------------------------+-----------------------------
Time stamp external events NA
Enable PPS events NA
Periodic output signals NA
One shot or periodic alarms timer_create, timer_settime

Quoted 4 lines

In contrast to the standard Linux system clock, a PHC is adjustable in hardware, for example using frequency compensation registers or a VCO. The ability to directly tune the PHC is essential to reap the benefit of hardware timestamping.

There is a reason for not being able to shift posix clocks: The system has one time base. The various clocks are contributing to maintaining that sytem wide time.

I do not understand why you want to maintain different clocks running at different speeds. Certainly interesting for some uses I guess that I do not have the energy to imagine right now. But can we get the PTP killer feature of synchronized accurate system time first?

Quoted 8 lines

3.3 Synchronizing the Linux System Time
========================================

One could offer a PHC as a combined clock source and clock event device. The advantage of this approach would be that it obviates the need for synchronization when the PHC is selected as the system timer. However, some PHCs, namely the PHY based clocks, cannot be used in this way.

Why not? Do PHY based clock not at least provide a counter that increments in synchronized intervals throughout the network?

Quoted 5 lines

Instead, the patch set provides a way to offer a Pulse Per Second (PPS) event from the PHC to the Linux PPS subsystem. A user space application can read the PPS events and tune the system clock, just like when using other external time sources like radio clocks or GPS.

User space is subject to various latencies created by the OS etc. I would that in order to have fine grained (read microsecond) accurary we would have to run the portions that are relevant to obtaining the desired accuracy in the kernel.

#24 lore raw
Quoted 11 lines
Quoted 5 lines

In contrast to the standard Linux system clock, a PHC is adjustable in hardware, for example using frequency compensation registers or a VCO. The ability to directly tune the PHC is essential to reap the benefit of hardware timestamping.

There is a reason for not being able to shift posix clocks: The system has one time base. The various clocks are contributing to maintaining that sytem wide time.

Adjusting clocks is absolutely essential for proper functioning of the PTP

protocol. The slave obtains and calculates the offset from master and uses that in order to adjust the clock properly, The problem is that the timestamps are done via the hardware. We need a method to expose that hardware so that the ptp software can properly adjust those clocks.

Quoted 5 lines

I do not understand why you want to maintain different clocks running at different speeds. Certainly interesting for some uses I guess that I do not have the energy to imagine right now. But can we get the PTP killer feature of synchronized accurate system time first?

The problem is maintaining a hardware clock at the correct speed/frequency and time. The timestamping is done via hardware, and that hardware clock needs to be accurate. We need to be able to modify that clock. Yes, having the system time be the same value would be nice, but the problem comes because we don't want to jump through hoops to keep that hardware clock accurate to the ptp protocol running on the network.

Quoted 7 lines
Quoted 6 lines

Instead, the patch set provides a way to offer a Pulse Per Second (PPS) event from the PHC to the Linux PPS subsystem. A user space application can read the PPS events and tune the system clock, just like when using other external time sources like radio clocks or GPS.

Quoted 5 lines

User space is subject to various latencies created by the OS etc. I would that in order to have fine grained (read microsecond) accurary we would have to run the portions that are relevant to obtaining the desired accuracy in the kernel.

All of the necessary features for microsecond or better accuracy are done via the hardware. You can get accuracy to within <10 mircoseconds while only sending sync packets and such once per second. The reason is because the hardware timestamps are very accurate. But if we can't properly adjust the clocks time and frequency, we cannot maintain the accuracy of the timestamps.

Quoted 2 lines

--

Quoted 4 lines

To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#25 lore raw
Quoted 9 lines · Jacob Keller
Quoted 5 lines

There is a reason for not being able to shift posix clocks: The system has one time base. The various clocks are contributing to maintaining that sytem wide time.

Adjusting clocks is absolutely essential for proper functioning of the PTP

protocol. The slave obtains and calculates the offset from master and uses that in order to adjust the clock properly, The problem is that the timestamps are done via the hardware. We need a method to expose that hardware so that the ptp software can properly adjust those clocks.

There is no way to use that clock directly to avoid all the user space tuning etc? There are already tuning mechanisms in the kernel that do this with system time based on periodic clocks. If you calculate the nanoseconds since the epoch then you should be able to use that to tune system time.

Quoted 6 lines
Quoted 5 lines

I do not understand why you want to maintain different clocks running at different speeds. Certainly interesting for some uses I guess that I do not have the energy to imagine right now. But can we get the PTP killer feature of synchronized accurate system time first?

Quoted 6 lines

The problem is maintaining a hardware clock at the correct speed/frequency and time. The timestamping is done via hardware, and that hardware clock needs to be accurate. We need to be able to modify that clock. Yes, having the system time be the same value would be nice, but the problem comes because we don't want to jump through hoops to keep that hardware clock accurate to the ptp protocol running on the network.

Then allow system time == hardware clock?

Quoted 6 lines

All of the necessary features for microsecond or better accuracy are done via the hardware. You can get accuracy to within <10 mircoseconds while only sending sync packets and such once per second. The reason is because the hardware timestamps are very accurate. But if we can't properly adjust the clocks time and frequency, we cannot maintain the accuracy of the timestamps.

You can already adjust the system time with the existing APIs. Tuning hardware clocks is currently done using device specific controls. But I would think that you do not need to expose this to user space if you can do it all in kernel.

#26 lore raw
In reply to Christoph Lameter
Quoted 6 lines · Christoph Lameter
Quoted 4 lines · Richard Cochran

In contrast to the standard Linux system clock, a PHC is adjustable in hardware, for example using frequency compensation registers or a VCO. The ability to directly tune the PHC is essential to reap the benefit of hardware timestamping.

Quoted 8 lines

There is a reason for not being able to shift posix clocks: The system has one time base. The various clocks are contributing to maintaining that sytem wide time.

I do not understand why you want to maintain different clocks running at different speeds. Certainly interesting for some uses I guess that I do not have the energy to imagine right now. But can we get the PTP killer feature of synchronized accurate system time first?

This was my initial gut reaction as well, but in the end, I agree with Richard that in the case of one or multiple PTP hardware clocks, we really can't abstract over the different time domains.

Quoted 11 lines
Quoted 8 lines

3.3 Synchronizing the Linux System Time
========================================

One could offer a PHC as a combined clock source and clock event device. The advantage of this approach would be that it obviates the need for synchronization when the PHC is selected as the system timer. However, some PHCs, namely the PHY based clocks, cannot be used in this way.

Why not? Do PHY based clock not at least provide a counter that increments in synchronized intervals throughout the network?

I really don't think the PTP clock can be used as a clocksource sanely.

First, the hardware access is much to slow for system timekeeping.

Second, there is the problem that the system time is a software clock, and adjustments made (like freq) are made in the layer that interprets the underlying hardware cycle counter. Adjustments made in PTP (in order to sync the network timestamps) are made at the hardware level.

This would cause a disconnect between the hardware freq understood by the system time management code and the actual hardware freq.

Richard, I'd actually strike this paragraph from the rational, as I feel it has the tendency to confuse as it suggests having the PHC as a clocksource is feasible when really it isn't. Or alternatively, maybe express more clearly why its not feasible, so it doesn't just seem like a minor design choice.

thanks
-john

#27 lore raw
Quoted 3 lines · john stultz

This was my initial gut reaction as well, but in the end, I agree with Richard that in the case of one or multiple PTP hardware clocks, we really can't abstract over the different time domains.

My (arguably still superficial) review of the source does not show anything that would make me reach that conclusion.

Quoted 3 lines

I really don't think the PTP clock can be used as a clocksource sanely.

First, the hardware access is much to slow for system timekeeping.

The HPET or pit timesource are also quite slow these days. You only need access periodically to essentially tune the TSC ratio.

Quoted 6 lines

Second, there is the problem that the system time is a software clock, and adjustments made (like freq) are made in the layer that interprets the underlying hardware cycle counter. Adjustments made in PTP (in order to sync the network timestamps) are made at the hardware level.

From what I can see the PTP clocks are periodic hardware cycle counters

like any other clock that we currently support. If its configurable enough then setup a hardware cycle counter that mimics nanoseconds since the epoch as closely as possible and use that to sync the TSC rate to. Makes it very easy.

Quoted 2 lines

This would cause a disconnect between the hardware freq understood by the system time management code and the actual hardware freq.

We can switch underlying clocks for system time already. We can adapt to a different hw frequency. But then I do not know why adjust the freq? I thought the point was that the periodic clock was network synchronized and can be used as "the" master clock for multiple machines?

Quoted 5 lines

Richard, I'd actually strike this paragraph from the rational, as I feel it has the tendency to confuse as it suggests having the PHC as a clocksource is feasible when really it isn't. Or alternatively, maybe express more clearly why its not feasible, so it doesn't just seem like a minor design choice.

Sorry but I still feel that this is pretty much a misguided approach that creates unnecessary layers in the kernel. The trivial easy approach was not done (copy a driver from drivers/clocksource, modify so that it programs access to a centralized periodic ptp signal and uses it for system sync).

#28 lore raw
Quoted 9 lines · Christoph Lameter
Quoted 3 lines · john stultz

This was my initial gut reaction as well, but in the end, I agree with Richard that in the case of one or multiple PTP hardware clocks, we really can't abstract over the different time domains.

My (arguably still superficial) review of the source does not show anything that would make me reach that conclusion.

Quoted 6 lines
Quoted 3 lines

I really don't think the PTP clock can be used as a clocksource sanely.

First, the hardware access is much to slow for system timekeeping.

The HPET or pit timesource are also quite slow these days. You only need access periodically to essentially tune the TSC ratio.

If we're using the TSC, then we're not using the PTP clock as you suggest. Further the HPET and PIT aren't used to steer the system time when we are using the TSC as a clocksource. Its only used to calibrate the initial constant freq used by the timekeeping code (and if its non-constant, we throw it out).

Quoted 10 lines
Quoted 4 lines

Second, there is the problem that the system time is a software clock, and adjustments made (like freq) are made in the layer that interprets the underlying hardware cycle counter. Adjustments made in PTP (in order to sync the network timestamps) are made at the hardware level.

From what I can see the PTP clocks are periodic hardware cycle counters like any other clock that we currently support. If its configurable enough then setup a hardware cycle counter that mimics nanoseconds since the epoch as closely as possible and use that to sync the TSC rate to. Makes it very easy.

I guess I'm confused by what you're suggesting.
If we're using the TSC, then that's the clocksource timekeeping uses. The original issue seemed to be around the suggestion of using the PTP clock as a clocksource, which I don't think is really feasible.

Again, that's because
1) The PTP access latency is slow (so is the PIT, true enough, but no one should be using the PIT as a clocksource unless they really have no better hardware - its really only useful for 486s and old freq scaling laptops that have no other stable clocksource).

2) The way PTP clocks are steered to sync with network time causes their hardware freq to actually change. Since these adjustments are done on the hardware clock level, and not on the system time level, the adjustments to sync the system time/freq would then be made incorrect by PTP hardware adjustments.

3) Further, the PTP hardware counter can be simply set to a new offset to put it in line with the network time. This could cause trouble with timekeeping much like unsynced TSCs do.

Now, what you seem to be suggesting is to use the TSC (or whatever clocksource the system time is using) but to steer the system time using the PTP clock. This is actually what is being proposed, however, the steering is done in userland. This is due to the fact that there are two components to the steering, 1) adjusting the PTP clock hardware to network time and 2) adjusting the system time to the PTP hardware. By exposing the PTP clock to userland via the posix clocks interface, we allow this to easily be done.

Quoted 5 lines
Quoted 2 lines

This would cause a disconnect between the hardware freq understood by the system time management code and the actual hardware freq.

We can switch underlying clocks for system time already. We can adapt to a different hw frequency.

Actually no. The timekeeping code requires a fixed freq counter. Dealing with hardware freq changes is difficult, because error is introduced by the latency between when the freq changes and when the timekeeping code is notified of it. So the system treats the hardware counters as fixed freq. Now, hardware does vary freq ever so slightly as thermal conditions change, but this is addressed in userland and corrected via adjtimex.

Quoted 3 lines

But then I do not know why adjust the freq? I
thought the point was that the periodic clock was network synchronized and can be used as "the" master clock for multiple machines?

Not parsing that. What do you mean by periodic clock?

Quoted 8 lines
Quoted 5 lines

Richard, I'd actually strike this paragraph from the rational, as I feel it has the tendency to confuse as it suggests having the PHC as a clocksource is feasible when really it isn't. Or alternatively, maybe express more clearly why its not feasible, so it doesn't just seem like a minor design choice.

Sorry but I still feel that this is pretty much a misguided approach that creates unnecessary layers in the kernel.

Unnecessary layers? Where? This approach has less in-kernel layers, as it exposes the PTP clock to userland, instead of trying to layer things on top of it and stretching the system time abstraction to cover it.

Quoted 4 lines

The trivial easy approach was
not done (copy a driver from drivers/clocksource, modify so that it programs access to a centralized periodic ptp signal and uses it for system sync).

I disagree.

I've argued through the approach trying to keep it all internal to the kernel, but to do so would be anything but trivial. Further, there's the case of master-clocks, where the PTP hardware must be synced to system time, instead of the other way around. And then there's the case of boundary-clocks, which may have multiple PTP hardware clocks that have to be synced.

I think exposing this through the posix clock interface is really the best approach. Its not a static clockid, so its not something most apps will ever have to deal with, but it allows the few apps that really need to have access to the PTP clock hardware can do so in a clean way.

And credits to Richard for having to slowly explain this to me (and others) many times over, before I got it.

thanks
-john

#29 lore raw
Quoted 8 lines · john stultz
Quoted 2 lines

The HPET or pit timesource are also quite slow these days. You only need access periodically to essentially tune the TSC ratio.

If we're using the TSC, then we're not using the PTP clock as you suggest. Further the HPET and PIT aren't used to steer the system time when we are using the TSC as a clocksource. Its only used to calibrate the initial constant freq used by the timekeeping code (and if its non-constant, we throw it out).

There is no other scalable time source available for fast timer access than the time stamp counter in the cpu. Other time source require memory accesses which is inherently slower.

An accurate other time source is used to adjust this clock. NTP does that via the clock interfaces from user space which has its problems with accuracy. PTP can provide the network synced time access
that would a more accurate calibration of the time.

Quoted 5 lines

2) The way PTP clocks are steered to sync with network time causes their hardware freq to actually change. Since these adjustments are done on the hardware clock level, and not on the system time level, the adjustments to sync the system time/freq would then be made incorrect by PTP hardware adjustments.

Right. So use these as a way to fine tune the TSC clock (and thereby the system time).

Quoted 3 lines

3) Further, the PTP hardware counter can be simply set to a new offset to put it in line with the network time. This could cause trouble with timekeeping much like unsynced TSCs do.

You can do the same for system time.

Quoted 8 lines

Now, what you seem to be suggesting is to use the TSC (or whatever clocksource the system time is using) but to steer the system time using the PTP clock. This is actually what is being proposed, however, the steering is done in userland. This is due to the fact that there are two components to the steering, 1) adjusting the PTP clock hardware to network time and 2) adjusting the system time to the PTP hardware. By exposing the PTP clock to userland via the posix clocks interface, we allow this to easily be done.

Userland code would introduce latencies that would make sub microsecond time sync very difficult.

Quoted 10 lines
Quoted 2 lines

We can switch underlying clocks for system time already. We can adapt to a different hw frequency.

Actually no. The timekeeping code requires a fixed freq counter. Dealing with hardware freq changes is difficult, because error is introduced by the latency between when the freq changes and when the timekeeping code is notified of it. So the system treats the hardware counters as fixed freq. Now, hardware does vary freq ever so slightly as thermal conditions change, but this is addressed in userland and corrected via adjtimex.

Acadmic hair splitting? I have repeatedly switched between different clocks on various systems. So its difficult but we do it?

Quoted 3 lines

Unnecessary layers? Where? This approach has less in-kernel layers, as it exposes the PTP clock to userland, instead of trying to layer things on top of it and stretching the system time abstraction to cover it.

You dont need the user APIs if you directly use the PTP time source to steer the system clock. In fact I think you have to do it in kernel space since user space latencies will degrade accuracy otherwise.

Quoted 6 lines

I've argued through the approach trying to keep it all internal to the kernel, but to do so would be anything but trivial. Further, there's the case of master-clocks, where the PTP hardware must be synced to system time, instead of the other way around. And then there's the case of boundary-clocks, which may have multiple PTP hardware clocks that have to be synced.

Ok maybe we need some sort of control interface to manage the clock like the others have.

Quoted 4 lines

I think exposing this through the posix clock interface is really the best approach. Its not a static clockid, so its not something most apps will ever have to deal with, but it allows the few apps that really need to have access to the PTP clock hardware can do so in a clean way.

It implies clock tuning in userspace for a potential sub microsecond accurate clock. The clock accuracy will be limited by user space latencies and noise. You wont be able to discipline the system clock accurately.

The posix clocks today assumes one notion of real "time" in the kernel. All clocks increase in lockstep (aside from offset updates). This approach here result in multiple notions of "time" increasing at various speeds. And it implies that someone is user space is trying to tinker around with extremely low latencies using system call APIs that take much longer than these intervals to process the data.

#30 lore raw
Quoted 3 lines

There is no other scalable time source available for fast timer access than the time stamp counter in the cpu. Other time source require memory accesses which is inherently slower.

On what hardware ?

Quoted 4 lines

An accurate other time source is used to adjust this clock. NTP does that via the clock interfaces from user space which has its problems with accuracy. PTP can provide the network synced time access
that would a more accurate calibration of the time.

Accuracy does not require speed of access. Accuracy requires predictible latency of access.

Quoted 2 lines

Userland code would introduce latencies that would make sub microsecond time sync very difficult.

You can take a multiple micro-second I/O stall or SMI trap on a PC so you already lost the battle on the platform you seem to be discussing.

Quoted 3 lines

You dont need the user APIs if you directly use the PTP time source to steer the system clock. In fact I think you have to do it in kernel space since user space latencies will degrade accuracy otherwise.

PTP is not a 'time source' it is one or more source of time. The distinction is rather important.

Quoted 4 lines

It implies clock tuning in userspace for a potential sub microsecond accurate clock. The clock accuracy will be limited by user space latencies and noise. You wont be able to discipline the system clock accurately.

Noise matters, latency doesn't. And the kernel is getting more and more real time support all the time.

#31 lore raw
Quoted 6 lines · Alan Cox
Quoted 4 lines

It implies clock tuning in userspace for a potential sub microsecond accurate clock. The clock accuracy will be limited by user space latencies and noise. You wont be able to discipline the system clock accurately.

Noise matters, latency doesn't.

Well put! That's why we need hardware support for PTP timestamping to reduce the noise, but get along well with the clock servo that is steering the PHC in user space.

Christian

#32 lore raw
Quoted 10 lines · Christian Riesch
Quoted 6 lines
Quoted 4 lines

It implies clock tuning in userspace for a potential sub microsecond accurate clock. The clock accuracy will be limited by user space latencies and noise. You wont be able to discipline the system clock accurately.

Noise matters, latency doesn't.

Well put! That's why we need hardware support for PTP timestamping to reduce the noise, but get along well with the clock servo that is steering the PHC in user space.

Even if I buy into the catch phrase above: User space is subject to noise that the in kernel code is not. If you do the tuning over long intervals then it hopefully averages out but it still causes jitter effects that affects the degree of accuracy (or sync) that you can reach. And the noise varies with the load on the system.

#33 lore raw

Quoting Christoph Lameter <[email protected]>:

Quoted 13 lines · Christian Riesch
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

It implies clock tuning in userspace for a potential sub microsecond accurate clock. The clock accuracy will be limited by user space latencies and noise. You wont be able to discipline the system clock accurately.

Noise matters, latency doesn't.

Well put! That's why we need hardware support for PTP timestamping to reduce the noise, but get along well with the clock servo that is steering the PHC in user space.

Quoted 5 lines

Even if I buy into the catch phrase above: User space is subject to noise that the in kernel code is not. If you do the tuning over long intervals then it hopefully averages out but it still causes jitter effects that affects the degree of accuracy (or sync) that you can reach. And the noise varies with the load on the system.

Yes and no. If you regard it as a control system: The latencies of the operating system are a dead time in the control system. The sampling time is quite large, one second, maybe around 100ms or 10ms in telecommunication applications, but that is still large compared to the latencies you expect to have in the operating system. Hence, this latencies (=dead time) can be neglected and the important thing that remains is the noise that you introduce in the measurements of the time stamps, which is therefore done in hardware. I admit that my short statement above is not completely correct, I should have mentioned the rather large sampling time we are dealing with here.

Christian

#34 lore raw
In reply to Christoph Lameter

In message: <[email protected]>
Christoph Lameter <[email protected]> writes:
: On Thu, 23 Sep 2010, Christian Riesch wrote:
:
: > > > It implies clock tuning in userspace for a potential sub microsecond : > > > accurate clock. The clock accuracy will be limited by user space : > > > latencies and noise. You wont be able to discipline the system clock : > > > accurately.
: > >
: > > Noise matters, latency doesn't.
: >
: > Well put! That's why we need hardware support for PTP timestamping to reduce : > the noise, but get along well with the clock servo that is steering the PHC in : > user space.
:
: Even if I buy into the catch phrase above: User space is subject to noise : that the in kernel code is not. If you do the tuning over long intervals : then it hopefully averages out but it still causes jitter effects that : affects the degree of accuracy (or sync) that you can reach. And the noise : varies with the load on the system.

Please see the earlier posts in this thread about why this doesn't matter as much as you might think. What matters is the measurements (which are done in hardware and the results buffered), not the latency in processing those messages through your servo. This is due to the fact that the errors that even long latencies introduce are proportional to the change in fractional frequency[*] of the clock being steered. This change is usually on the order of a part per million. Even with 10ms of latency would mean that you're introducing on the order of sub-nanoseconds of phase error that will be measured in the next cycle and steered out.

That's why latency doesn't matter. Do you have other math to show that it does?

Warner

[*] abs(1 - (clock_freq_old / clock_freq_new)) where clock_freq_old is the old estimate of the clock and clock_freq_new is the new frequency estimate of the clock. Second to second, these change on the order of a part per million or less...

#35 lore raw
In reply to Christoph Lameter
Quoted 11 lines · Christoph Lameter
Quoted 8 lines · john stultz
Quoted 2 lines

The HPET or pit timesource are also quite slow these days. You only need access periodically to essentially tune the TSC ratio.

If we're using the TSC, then we're not using the PTP clock as you suggest. Further the HPET and PIT aren't used to steer the system time when we are using the TSC as a clocksource. Its only used to calibrate the initial constant freq used by the timekeeping code (and if its non-constant, we throw it out).

Quoted 3 lines

There is no other scalable time source available for fast timer access than the time stamp counter in the cpu. Other time source require memory accesses which is inherently slower.

Right, but no one likes the HPET or ACPI PM for a clocksource, its just the TSC isn't usable in some cases, so they have to be used.

We don't want to force folks to decide between closely sycned time and fast time reads. So that is part of the reason why PTP as a clocksource isn't a good idea.

Quoted 4 lines

An accurate other time source is used to adjust this clock. NTP does that via the clock interfaces from user space which has its problems with accuracy. PTP can provide the network synced time access
that would a more accurate calibration of the time.

Calibration isn't whats needed here (it is an issue, but a separate one
- and I've got some patches if you're interested!) as its a one-time source of error and can be corrected by ntp today without trouble. Adjustments to the system time is something that has to be done continuously to handle for variable thermal drift over time.

Quoted 8 lines
Quoted 5 lines

2) The way PTP clocks are steered to sync with network time causes their hardware freq to actually change. Since these adjustments are done on the hardware clock level, and not on the system time level, the adjustments to sync the system time/freq would then be made incorrect by PTP hardware adjustments.

Right. So use these as a way to fine tune the TSC clock (and thereby the system time).

So you're then not suggesting to "use the PTP as a clocksource".

Using the PTP hardware to adjust the system time freq is exactly whats being proposed.

Quoted 5 lines
Quoted 3 lines

3) Further, the PTP hardware counter can be simply set to a new offset to put it in line with the network time. This could cause trouble with timekeeping much like unsynced TSCs do.

You can do the same for system time.

Settimeofday does allow CLOCK_REALTIME to jump, but the CLOCK_MONOTONIC time cannot jump around. Having a clocksource that is non-monotonic would break this.

Quoted 11 lines
Quoted 8 lines

Now, what you seem to be suggesting is to use the TSC (or whatever clocksource the system time is using) but to steer the system time using the PTP clock. This is actually what is being proposed, however, the steering is done in userland. This is due to the fact that there are two components to the steering, 1) adjusting the PTP clock hardware to network time and 2) adjusting the system time to the PTP hardware. By exposing the PTP clock to userland via the posix clocks interface, we allow this to easily be done.

Userland code would introduce latencies that would make sub microsecond time sync very difficult.

The design actually avoids most userland induced latency.

1) On the PTP hardware syncing point, the reference packet gets timestamped with the PTP hardware time on arrival. This allows the offset calculation to be done in userland without introducing latency.

2) On the system syncing side, the proposal for the PPS interrupt allows the PTP hardware to trigger an interrupt on the second boundary that would take a timestamp of the system time. Then the pps interface allows for the timestamp to be read from userland allowing the offset to be calculated without introducing additional latency.

Quoted 11 lines
Quoted 10 lines
Quoted 2 lines

We can switch underlying clocks for system time already. We can adapt to a different hw frequency.

Actually no. The timekeeping code requires a fixed freq counter. Dealing with hardware freq changes is difficult, because error is introduced by the latency between when the freq changes and when the timekeeping code is notified of it. So the system treats the hardware counters as fixed freq. Now, hardware does vary freq ever so slightly as thermal conditions change, but this is addressed in userland and corrected via adjtimex.

Quoted 2 lines

Acadmic hair splitting? I have repeatedly switched between different clocks on various systems. So its difficult but we do it?

Sure, we handle the fairly-rare case of switching clocksources. And that introduces a bit of error each time. But one doesn't expect to be switching clock-sources every second and still keep synced time.

Quoted 7 lines
Quoted 3 lines

Unnecessary layers? Where? This approach has less in-kernel layers, as it exposes the PTP clock to userland, instead of trying to layer things on top of it and stretching the system time abstraction to cover it.

You dont need the user APIs if you directly use the PTP time source to steer the system clock. In fact I think you have to do it in kernel space since user space latencies will degrade accuracy otherwise.

Via the PPS interface, this can be done easily without large latency.

Additionally, even just in userland, it would be easy to bracket two reads of the system time around one read of the PTP clock to bound any userland latency fairly well. It may not be as good as the PPS interface (although that depends on the interrupt latency), but if the accesses are all local, it probably could get fairly close.

Quoted 9 lines
Quoted 6 lines

I've argued through the approach trying to keep it all internal to the kernel, but to do so would be anything but trivial. Further, there's the case of master-clocks, where the PTP hardware must be synced to system time, instead of the other way around. And then there's the case of boundary-clocks, which may have multiple PTP hardware clocks that have to be synced.

Ok maybe we need some sort of control interface to manage the clock like the others have.

That's what the clock_adjtime call provides.

Quoted 9 lines
Quoted 4 lines

I think exposing this through the posix clock interface is really the best approach. Its not a static clockid, so its not something most apps will ever have to deal with, but it allows the few apps that really need to have access to the PTP clock hardware can do so in a clean way.

It implies clock tuning in userspace for a potential sub microsecond accurate clock. The clock accuracy will be limited by user space latencies and noise. You wont be able to discipline the system clock accurately.

I think you'll find that the userspace latency issue is fairly well addressed by the existing design.

Quoted 2 lines

The posix clocks today assumes one notion of real "time" in the kernel. All clocks increase in lockstep (aside from offset updates).

Not true. The cputime clockids do not increment at the same rate (as the apps don't always run). Further CLOCK_MONOTONIC_RAW provides a non-freq corrected view of CLOCK_MONOTONIC, so it increments at a slightly different rate.

Quoted 2 lines

This approach
here result in multiple notions of "time" increasing at various speeds.

Yes. I found this initially to be distasteful as well. But the more I realized its just a reality of the hardware, and that as long as its not advertised along side of CLOCK_REALTIME and CLOCK_MONOTONIC (the ptp clock ids are dynamic and somewhat hidden in the sysfs tree), so userland developers don't get confused, its not so bad.

Especially since driver writers will just expose the same stuff via a chardev ioctl in a less standard way and its likely no one will notice. Re-using the fairly nice (Alan of course disagrees :) posix interface seems at least a little better for application developers who actually have to use the hardware.

thanks
-john

#36 lore raw
Quoted 9 lines · john stultz
Quoted 5 lines
Quoted 3 lines

3) Further, the PTP hardware counter can be simply set to a new offset to put it in line with the network time. This could cause trouble with timekeeping much like unsynced TSCs do.

You can do the same for system time.

Settimeofday does allow CLOCK_REALTIME to jump, but the CLOCK_MONOTONIC time cannot jump around. Having a clocksource that is non-monotonic would break this.

Currently time runs at the same speed. CLOCK_MONOTONIC runs at a offset to CLOCK_REALTIME. We are creating APIs here that allow time to run at different speeds.

Quoted 5 lines

The design actually avoids most userland induced latency.

1) On the PTP hardware syncing point, the reference packet gets timestamped with the PTP hardware time on arrival. This allows the offset calculation to be done in userland without introducing latency.

The timestamps allows the calculation of the network transmission time I guess and therefore its more accurate to calculate that effect out. Ok but then the overhead of getting to code in user space (that does the proper clock adjustments) is resulting in the addition of a relatively long time that is subject to OS scheduling latencies and noises.

Quoted 5 lines

2) On the system syncing side, the proposal for the PPS interrupt allows the PTP hardware to trigger an interrupt on the second boundary that would take a timestamp of the system time. Then the pps interface allows for the timestamp to be read from userland allowing the offset to be calculated without introducing additional latency.

Sorry dont really get the whole picture here it seems. Sounds like one is going through additional unnecessary layers. Why would the PTP hardware triggger an interrupt? I thought the PTP messages came in via timestamping and are then processed by software. Then the software is issuing a hardware interrupt that then triggers the PPS subsystem. And that is supposed to be better than directly interfacing with the PTP?

Quoted 5 lines

Additionally, even just in userland, it would be easy to bracket two reads of the system time around one read of the PTP clock to bound any userland latency fairly well. It may not be as good as the PPS interface (although that depends on the interrupt latency), but if the accesses are all local, it probably could get fairly close.

That sounds hacky.

Quoted 4 lines
Quoted 2 lines

Ok maybe we need some sort of control interface to manage the clock like the others have.

That's what the clock_adjtime call provides.

Ummm... You are managing a hardware device with hardware (driver) specific settings. That is currently being done via ioctls. Why generalize it?

Quoted 7 lines
Quoted 2 lines

The posix clocks today assumes one notion of real "time" in the kernel. All clocks increase in lockstep (aside from offset updates).

Not true. The cputime clockids do not increment at the same rate (as the apps don't always run). Further CLOCK_MONOTONIC_RAW provides a non-freq corrected view of CLOCK_MONOTONIC, so it increments at a slightly different rate.

cputime clockids are not tracking time but cpu resource use.

Quoted 3 lines

Re-using the fairly nice (Alan of course disagrees :) posix interface seems at least a little better for application developers who actually have to use the hardware.

Well it may also be confusing for others. The application developers also will have a hard time using a generic clock interface to control PTP device specific things like frequencies, rates etc etc. So you always need to ioctl/device specific control interface regardless.

#37 lore raw

In message: <[email protected]>
Christoph Lameter <[email protected]> writes:
: On Thu, 23 Sep 2010, john stultz wrote:
: > The design actually avoids most userland induced latency.
: >
: > 1) On the PTP hardware syncing point, the reference packet gets : > timestamped with the PTP hardware time on arrival. This allows the : > offset calculation to be done in userland without introducing latency. :
: The timestamps allows the calculation of the network transmission time I : guess and therefore its more accurate to calculate that effect out. Ok but : then the overhead of getting to code in user space (that does the proper : clock adjustments) is resulting in the addition of a relatively long time : that is subject to OS scheduling latencies and noises.

The timestamps at the hardware level allow you to factor out variation caused by OS Scheduling, OS network stack delay and internal buffering on the NIC. Variation in measurements is what kills accuracy.

When steering a clock by making an error measurement of the phase and frequency of it, the latency induced by OS scheduling tends to be unimportant. It is far more important to know when you steered the clock (called adjtime or friends) than to steer it at any fixed latency to when the data for the measurements was made. Measuring the time of steer can tolerate errors in the range of OS scheduling latencies easily, since that tends to produce a very small effect. It introduces an error in your expected phase for the next measurement on the order of the product of the time of steer error times the change in fractional frequency (abs( 1 - (nu_new / nu_old))). Even if the estimate is really bad at 100ms, most steers are on the order about one part per million. This leads to a sub-nanosecond phase error estimate in the next measurement cycle (a non-accumulating error). A 1ms error leads to maybe tens of picoseconds of estimate error.

This is a common error that I've seen repeated in this thread. The only reason that it has historically been important is because when you are doing timestamping in software based on an interrupt, that stuff does matter.

Warner

author maintainer
#38 lore raw
Quoted 5 lines · M. Warner Losh

This is a common error that I've seen repeated in this thread. The only reason that it has historically been important is because when you are doing timestamping in software based on an interrupt, that stuff does matter.

Yes, thanks for your helpful explanation.

To further illustrate how small the effect of running the servo in user space is, consider the following.

It is true that delays and jitter in the time to process the received packet negatively affect the clock servo loop. However, the effect in our case will be quite small.

John Eidson's book [1] presents a rigorous servo model that includes an analysis of the delay from the time the samples (timestamps) are taken until the corrective action is performed by the PTP software.

For PTP, the sample time is at the sender (the remote host, the master clock). The master sends *two* packets, where the second one (the so called follow-up packet) contains the hardware time stamp of the first one. So, the computational delay includes the time spent by the sender in its PTP stack preparing the second packet, the time the packet spends in transit, and the time in the receiver's PTP stack and servo.

According to Eidson's analysis, a delay of up to 10 milliseconds would be acceptable, even with a sample rate of 10 Hz. Therefore, saving a few dozen microseconds by placing the servo (and PTP stack) into the kernel is not worth the effort.

Richard

[1] title =     {Measurement, control, and communication using IEEE 1588},
    author =    {J. C. Eidson},
    year =      2006,

publisher = {Springer-Verlag},

author maintainer
#39 lore raw
In reply to Christoph Lameter
Quoted 4 lines · Christoph Lameter
Quoted 3 lines · Richard Cochran

3.3 Synchronizing the Linux System Time
========================================

Quoted 8 lines
Quoted 5 lines

One could offer a PHC as a combined clock source and clock event device. The advantage of this approach would be that it obviates the need for synchronization when the PHC is selected as the system timer. However, some PHCs, namely the PHY based clocks, cannot be used in this way.

Why not? Do PHY based clock not at least provide a counter that increments in synchronized intervals throughout the network?

The counter in the PHY is accessed via the MDIO bus. One 16 bit read takes anywhere from 25 to 40 microseconds. Reading the 64 bit time value requires four reads, so we're talking about 100 to 160 microseconds, just for a single time reading.

In addition to that, reading MDIO bus can sleep. So, we can't (in general) to offer PHCs as clock sources.

Quoted 10 lines
Quoted 5 lines

Instead, the patch set provides a way to offer a Pulse Per Second (PPS) event from the PHC to the Linux PPS subsystem. A user space application can read the PPS events and tune the system clock, just like when using other external time sources like radio clocks or GPS.

User space is subject to various latencies created by the OS etc. I would that in order to have fine grained (read microsecond) accurary we would have to run the portions that are relevant to obtaining the desired accuracy in the kernel.

The time-critical operations are all performed in hardware (packet timestamp), or in kernel space (input PPS timestamp). User space only runs the servo (using hardware or kernel timestamps as input) and performs the clock correction. With a sample rate of 1 PPS, the small user space induced delay (a few dozen microseconds) between sample time and clock correction is not an issue.

Thanks,
Richard

#40 lore raw
In reply to Richard Cochran
Quoted 9 lines · Richard Cochran

Here is the sixth version of my patch set adding PTP hardware clock support to the Linux kernel. The main difference to v5 is that the character device interface has been replaced with one based on the posix clock system calls.

The first three patches add necessary background support in the posix clock code. The last five add the new PTP hardware clock features. Previously, I had tried to present the posix clock changes all by themselves, but commentators asked to see the whole context.

Richard,
Its great to see this work continue and the patch set is shaping up
nicely! There's still a few details to work out, but I think the remaining issues are relatively small.

Quoted 9 lines

3.2.3 Dynamic POSIX Clock IDs
------------------------------

The reaction on the list to having a static id like CLOCK_PTP was mostly negative. However, the idea of generating a clock id dynamically seems to have gained acceptance. The general idea is to advertise the available clock ids to user space via sysfs. This patch set implements two different ways:

Quoted 8 lines

/sys/class/timesource/<name>/id
/sys/class/ptp/ptp_clock_X/id

Note: I am not too sure that this is exactly what people imagined,
but it is my best understanding so far. I gleaned two different ideas about where to offer the clock id. In order to keep just one way, I will be happy to remove the less popular one.

So yea, I'm not a fan of the "timesource" sysfs interface. One, I think the name is poor (posix_clocks or something a little more specific would be an improvement), and second, I don't like the dictionary interface, where one looks up the clock by name.

Instead, I think having the id hanging off the class driver is much better, as it allows mapping the actual hardware to the id more clearly.

So I'd drop the "timesource" listing. And maybe change "id" to "clock_id" so its a little more clear what the id is for.

Quoted 8 lines

3.3 Synchronizing the Linux System Time
========================================

One could offer a PHC as a combined clock source and clock event device. The advantage of this approach would be that it obviates the need for synchronization when the PHC is selected as the system timer. However, some PHCs, namely the PHY based clocks, cannot be used in this way.

Again, I'd scratch this.

What I think you might want to mention is that an application like NTP could use the PTP clockid much like NTP currently can be configured to use the RTC to steer the system time.

Possibly the PTPd could just do this, reducing the number of deamons and avoiding mixing NTP up in what is really a different sync algorithm.

Quoted 5 lines

Instead, the patch set provides a way to offer a Pulse Per Second (PPS) event from the PHC to the Linux PPS subsystem. A user space application can read the PPS events and tune the system clock, just like when using other external time sources like radio clocks or GPS.

Forgive me for a bit of a tangent here:
So while I think this PPS method is a neat idea, I'm a little curious
how much of a difference the PPS method for syncing the clock would be over just a simple reading of the two clocks and correcting the offset.

It seems much of it depends on the read latency of the PTP hardware vs the interrupt latency. Also the PTP clock granularity would effect the read accuracy (like on the RTC, you don't really know how close to the second boundary you are).

Have you done any such measurements between the two methods? I just wonder if it would actually be something noticeable, and if its not, how much lighter this patch-set would be without the PPS connection.

Again, this isn't super critical, just trying to make sure we don't end up adding a bunch of code that doesn't end up being used. Also PPS interrupts are awfully frequent, so systems concerned with power-saving and deep idles probably would like something that could be done at a more coarse interval.

Quoted 21 lines

3.5 User timers
================

Using the POSIX clock API gived user space the possibility to create and use timers with timer_create and timer_settime. In the current patch set the kernel functionality is not implemented, since there are some issues to consider first. I see two ways to do about this.

1. Implement the functionality anew. This approach might end up duplicating similar code that already exists. Also, looking at the hrtimer code, getting user timers right seems to have a number of gotchas and thorny issues.

2. Reuse the hrtimer code. Since the hrtimer code uses a clock event device under the hood, it might be possible (in theory) to offer capable PHCs as clock event devices. However, the current hrtimers are hard-coded to the event device via a per-cpu global. Perhaps one could associate an event device with a hrtimer via the timer itself.

Quoted 3 lines

At this point I am not optimistic about either approach, and I would vote for postponing the timer issue indefinitely. The implementation effort would be high, but the utility low.

So I'm working on this! I've pulled out the basic rbtree based timer list manipulation out of the hrtimer code and into its own lib code.

I'll send those patches out for RFC shortly.

Quoted 6 lines

If the Linux system time is synchronized to the PHC via the PPS method, then using standard hrtimers would be good enough for most purposes. Consider the time scales involved. The PHC can be synchronized to within 100 nanoseconds of an external time source, while timer wakeup latency (even with rt kernels) is tens of microseconds.

Hmm. I think this point touches on my earlier tangent.

Quoted 11 lines

4 Drivers
~~~~~~~~~~

4.1 Supported Hardware Clocks
==============================

+ Standard Linux system timer
This driver exports the standard Linux timer as a PTP clock. Although this duplicates CLOCK_REALTIME, the code serves as a simple example for driver development and lets people who without special hardware try the new API.

Still not a fan of this one, figure the app should handle the special case where there are no PTP clocks and just use CLOCK_REALTIME rather then funneling CLOCK_REALTIME through the PTP interface.

thanks
-john

author maintainer
#41 lore raw
Quoted 9 lines · john stultz
Quoted 3 lines · Richard Cochran

/sys/class/timesource/<name>/id
/sys/class/ptp/ptp_clock_X/id

So yea, I'm not a fan of the "timesource" sysfs interface. One, I think the name is poor (posix_clocks or something a little more specific would be an improvement), and second, I don't like the dictionary interface, where one looks up the clock by name.

Quoted 5 lines

Instead, I think having the id hanging off the class driver is much better, as it allows mapping the actual hardware to the id more clearly.

So I'd drop the "timesource" listing. And maybe change "id" to "clock_id" so its a little more clear what the id is for.

Okay, I will drop /sys/class/timesource (hope Alan Cox agrees :)

I threw it out there mostly for the sake of discussion. I imagined that there could be other properties in that directory, like time scale (TAI, UTC, etc). But it seems like we don't really need anything in that direction.

Quoted 10 lines
Quoted 8 lines

3.3 Synchronizing the Linux System Time
========================================

One could offer a PHC as a combined clock source and clock event device. The advantage of this approach would be that it obviates the need for synchronization when the PHC is selected as the system timer. However, some PHCs, namely the PHY based clocks, cannot be used in this way.

Again, I'd scratch this.

Okay, I only wanted to preempt the question which people are asking all the time: why can't it work with the system clock transparently?

Quoted 11 lines
Quoted 5 lines

Instead, the patch set provides a way to offer a Pulse Per Second (PPS) event from the PHC to the Linux PPS subsystem. A user space application can read the PPS events and tune the system clock, just like when using other external time sources like radio clocks or GPS.

Forgive me for a bit of a tangent here:
So while I think this PPS method is a neat idea, I'm a little curious
how much of a difference the PPS method for syncing the clock would be over just a simple reading of the two clocks and correcting the offset.

Quoted 6 lines

It seems much of it depends on the read latency of the PTP hardware vs the interrupt latency. Also the PTP clock granularity would effect the read accuracy (like on the RTC, you don't really know how close to the second boundary you are).

Have you done any such measurements between the two methods?

I have not yet tested how well the PPS method works, but I expect at least as good results as when using a GPS.

Quoted 3 lines

I just
wonder if it would actually be something noticeable, and if its not, how much lighter this patch-set would be without the PPS connection.

As you say, the problem with just reading two clocks at nearly the same time is that you have two uncertain operations. If you use a PPS, then there is only one clock to read, and that clock is the system clock, which hopefully is not too slow to read!

In addition, PHY reads can sleep, and that surely won't work. Even with MAC PHCs, reading outside of interrupt context makes you vulnerable to other interrupts.

Quoted 2 lines

Again, this isn't super critical, just trying to make sure we don't end up adding a bunch of code that doesn't end up being used.

The PPS hooks are really only just a few lines of code.

The great advantage of a PPS approach over and ad-hoc "read two clocks and compare", is that, with a steady, known sample rate, you can analyze and predict your control loop behavior. There is lots of literature available on how to do it. IMHO, that is the big weakness of the timecompare.c stuff used in the current IGB driver.

Quoted 4 lines

Also PPS
interrupts are awfully frequent, so systems concerned with power-saving and deep idles probably would like something that could be done at a more coarse interval.

We could always make the pulse rate programmable, for power-saving applications.

Quoted 9 lines
Quoted 8 lines

4.1 Supported Hardware Clocks
==============================

+ Standard Linux system timer
This driver exports the standard Linux timer as a PTP clock. Although this duplicates CLOCK_REALTIME, the code serves as a simple example for driver development and lets people who without special hardware try the new API.

Quoted 3 lines

Still not a fan of this one, figure the app should handle the special case where there are no PTP clocks and just use CLOCK_REALTIME rather then funneling CLOCK_REALTIME through the PTP interface.

It is really just as an example and for people who want to test driver the API. It can surely be removed before the final version...

Thanks for your comments,

Richard

#42 lore raw
Quoted 7 lines
Quoted 5 lines

Instead, I think having the id hanging off the class driver is much better, as it allows mapping the actual hardware to the id more clearly.

So I'd drop the "timesource" listing. And maybe change "id" to "clock_id" so its a little more clear what the id is for.

Okay, I will drop /sys/class/timesource (hope Alan Cox agrees :)

It makes sense to hang anything off the physical id

Quoted 4 lines

I threw it out there mostly for the sake of discussion. I imagined that there could be other properties in that directory, like time scale (TAI, UTC, etc). But it seems like we don't really need anything in that direction.

They can still hang off the physical device. Thats really a detail

Quoted 6 lines
Quoted 3 lines

interrupts are awfully frequent, so systems concerned with power-saving and deep idles probably would like something that could be done at a more coarse interval.

We could always make the pulse rate programmable, for power-saving applications.

I would expect the kernel drivers to be responsible for
- Turning off when they can
- Picking rates that are power optimal for the requirement

The latter is a bit interesting as I don't see anything in any of the timer APIs to express accuracy (a problem we have in kernel too). Historically it simply hasn't mattered.

#43 lore raw
In reply to Richard Cochran
Quoted 2 lines

So as far as the POSIX standard is concerned, offering a clock id to represent the PHC would be acceptable.

But completely useless as you may have more than one entirely different time managed by PTP and in which you are not master but must work with the timebases provided.

Quoted 8 lines

/sys/class/timesource/<name>/id
/sys/class/ptp/ptp_clock_X/id

Note: I am not too sure that this is exactly what people imagined,
but it is my best understanding so far. I gleaned two different ideas about where to offer the clock id. In order to keep just one way, I will be happy to remove the less popular one.

I see no fix proposed for the race condition I pointed out. This doesn't work.

Quoted 1 line

If the Linux system time is synchronized to the PHC via the PPS

To which PHC we can have several

Quoted 3 lines

+ Intel IXP465
- Auxiliary Slave/Master Mode Snapshot (optional interrupt)
- Target Time (optional interrupt)

And about 40 already supported by char driver interface clocks and rtcs in the kernel...

I'd say the inability to have multiple clocks and the race condition because of the clockid stuff leaves the proposal dead in the water.

It also ignores the existing APIs we have floating around attached to devices.

You need to make one small important change. You need to take the POSIX crap about enumerating things out and shoot it, bury it at a crossroads and sprinkle holy water on it.

Drop the clockid_t and swap it for a file handle like a proper Unix or Linux interface. The rest is much the same

fd = open /sys/class/timesource/[whatever]

various queries you may want to do to check the name etc

fclock_adjtime(fd, ...)

The posix interface is fundamentally flawed. It only works for staticly enumerable objects. Unix avoided that forty years ago by making the identifier a handle which immediately cures all your object lifetime problems in one swoop.

Namespace -> file handle translations are dynamic, but once you have it open you hold on to the same object, which means you can check what you have.

Alan

#44 lore raw
Quoted 6 lines · Alan Cox
Quoted 2 lines

So as far as the POSIX standard is concerned, offering a clock id to represent the PHC would be acceptable.

But completely useless as you may have more than one entirely different time managed by PTP and in which you are not master but must work with the timebases provided.

I don't see how this is a problem, as it exposes the multiple hardware clocks via different posix clock ids. So in the boundary clock case, you can configure which side is the client and which side is the master in a config file and the PTPd will appropriately steer them individually.

Quoted 4 lines
Quoted 4 lines

/sys/class/timesource/<name>/id
/sys/class/ptp/ptp_clock_X/id

Quoted 8 lines
Quoted 5 lines

Note: I am not too sure that this is exactly what people imagined,
but it is my best understanding so far. I gleaned two different ideas about where to offer the clock id. In order to keep just one way, I will be happy to remove the less popular one.

I see no fix proposed for the race condition I pointed out. This doesn't work.

So, if I recall this was: "How do you keep the module from unloading while its being used?"

There may need to be proper locking for unregistering the posix clock_id on module unload, but I don't think we need a use-count to prevent the module from being unloaded.

My question would be: How do we handle a USB network device ($14.99 now with PTP!) being unplugged? We can't say "Sorry! That's in use!". So we note the hardware is gone, and return the proper error code.

Or am I missing something else?

Quoted 10 lines
Quoted 1 line

If the Linux system time is synchronized to the PHC via the PPS

To which PHC we can have several

Quoted 3 lines

+ Intel IXP465
- Auxiliary Slave/Master Mode Snapshot (optional interrupt)
- Target Time (optional interrupt)

And about 40 already supported by char driver interface clocks and rtcs in the kernel...

And those char driver interfaces are all subtly different.

I actually recently submitted an RFC to expose the RTC devices via the posix clock/timer interface, because working with the RTC hardware device directly is terrible for managing alarm interrupts.

For instance, you easily run into the case where your TV recording application programs an alarm to record your favorite show at 8pm. Then your backup script programs an alarm to wake up at 2am to do your nightly backups. Your box suspends and the next morning, you're missing your favorite show!

Quoted 9 lines

I'd say the inability to have multiple clocks and the race condition because of the clockid stuff leaves the proposal dead in the water.

It also ignores the existing APIs we have floating around attached to devices.

You need to make one small important change. You need to take the POSIX crap about enumerating things out and shoot it, bury it at a crossroads and sprinkle holy water on it.

We agree the list-by-name stuff isn't the way to go. :)

Quoted 7 lines

Drop the clockid_t and swap it for a file handle like a proper Unix or Linux interface. The rest is much the same

fd = open /sys/class/timesource/[whatever]

various queries you may want to do to check the name etc

Quoted 7 lines

fclock_adjtime(fd, ...)

The posix interface is fundamentally flawed. It only works for staticly enumerable objects. Unix avoided that forty years ago by making the identifier a handle which immediately cures all your object lifetime problems in one swoop.

So, I don't really see how that's so different from what is being proposed. The clock_id is dynamically assigned per registered clock, and exposed via the sysfs interface from ptp hardware entry.

The only difference is the open/close reference counting, which I don't think is necessary here (since we can't always keep the hardware from going away).

thanks
-john

#45 lore raw

O> I don't see how this is a problem, as it exposes the multiple hardware

Quoted 3 lines

clocks via different posix clock ids. So in the boundary clock case, you can configure which side is the client and which side is the master in a config file and the PTPd will appropriately steer them individually.

They may all be slaves - that means you can't treat them as part of system time.

Quoted 10 lines

on module unload, but I don't think we need a use-count to prevent the module from being unloaded.

My question would be: How do we handle a USB network device ($14.99 now with PTP!) being unplugged? We can't say "Sorry! That's in use!". So we note the hardware is gone, and return the proper error code.

Or am I missing something else?

Open list
Oh number 31 appears to be the device I want
Close list

USB unplugged
Random other device plugged

clock_op(31, ....)

Oh bugger I've just reprogrammed the wrong time source.

We don't have stop the device being removed, instead of a disaster you get

clock_op(fd, blah)
-ENODEV

which btw is how just about everything else USB works when you pull the hardware.

Quoted 8 lines
Quoted 2 lines

And about 40 already supported by char driver interface clocks and rtcs in the kernel...

And those char driver interfaces are all subtly different.

I actually recently submitted an RFC to expose the RTC devices via the posix clock/timer interface, because working with the RTC hardware device directly is terrible for managing alarm interrupts.

Given that driver interfaces are sane and posix clock/timer interfaces have totally broken enumeration maybe you have it backwards. But if you follow through to my proposal maybe there is a saner answer still

Quoted 5 lines

For instance, you easily run into the case where your TV recording application programs an alarm to record your favorite show at 8pm. Then your backup script programs an alarm to wake up at 2am to do your nightly backups. Your box suspends and the next morning, you're missing your favorite show!

Poor resource management, and yes I'd agree you want a sensible interface.

Quoted 15 lines
Quoted 7 lines

Drop the clockid_t and swap it for a file handle like a proper Unix or Linux interface. The rest is much the same

fd = open /sys/class/timesource/[whatever]

various queries you may want to do to check the name etc

Quoted 7 lines

fclock_adjtime(fd, ...)

The posix interface is fundamentally flawed. It only works for staticly enumerable objects. Unix avoided that forty years ago by making the identifier a handle which immediately cures all your object lifetime problems in one swoop.

Quoted 7 lines

So, I don't really see how that's so different from what is being proposed. The clock_id is dynamically assigned per registered clock, and exposed via the sysfs interface from ptp hardware entry.

The only difference is the open/close reference counting, which I don't think is necessary here (since we can't always keep the hardware from going away).

It is absolutely neccessary in order that you can be sure that two calls actually relate to the *same* device. It's as fundamental as the difference betweeh chmod and fchmod although with the added ugliness of some random numeric identifier stuck in the middle.

It also btw makes it much easier to fix up the existing random collection of /dev/rtc devices - because you can open them and issue fclock_adjtime if we are careful how we do it and it makes sense.

Alan

#46 lore raw
Quoted 7 lines · Alan Cox

O> I don't see how this is a problem, as it exposes the multiple hardware

Quoted 3 lines

clocks via different posix clock ids. So in the boundary clock case, you can configure which side is the client and which side is the master in a config file and the PTPd will appropriately steer them individually.

They may all be slaves - that means you can't treat them as part of system time.

Sure, and that's something one would configure. So I'm not sure I see how exposing the different hardware bits via a clock_id is problematic. They're just clocks that are being exposed. The steering of system time to PTP or PTP to system time (or just PTP to other PTP clocks).

Quoted 13 lines
Quoted 8 lines

on module unload, but I don't think we need a use-count to prevent the module from being unloaded.

My question would be: How do we handle a USB network device ($14.99 now with PTP!) being unplugged? We can't say "Sorry! That's in use!". So we note the hardware is gone, and return the proper error code.

Or am I missing something else?

Open list
Oh number 31 appears to be the device I want
Close list

Quoted 6 lines

USB unplugged
Random other device plugged

clock_op(31, ....)

Oh bugger I've just reprogrammed the wrong time source.

Ok. So its just the issue of clock_id reuse. I was confusing it with some sort of module use counting issue. And yea, I can see how it might be easier to re-use the file descriptor then re-implementing the reuse logic in the posix-clock registration.

Quoted 7 lines

We don't have stop the device being removed, instead of a disaster you get

clock_op(fd, blah)
-ENODEV

which btw is how just about everything else USB works when you pull the hardware.

Right, which was what I was thinking as well, but assuming we didn't re-use clockids quickly.

Quoted 8 lines
Quoted 7 lines

So, I don't really see how that's so different from what is being proposed. The clock_id is dynamically assigned per registered clock, and exposed via the sysfs interface from ptp hardware entry.

The only difference is the open/close reference counting, which I don't think is necessary here (since we can't always keep the hardware from going away).

Quoted 8 lines

It is absolutely neccessary in order that you can be sure that two calls actually relate to the *same* device. It's as fundamental as the difference betweeh chmod and fchmod although with the added ugliness of some random numeric identifier stuck in the middle.

It also btw makes it much easier to fix up the existing random collection of /dev/rtc devices - because you can open them and issue fclock_adjtime if we are careful how we do it and it makes sense.

Wait, you're suggesting we add new fclock_* calls that duplicate the posix interface? That doesn't sound great to me.

What did you think of Kyle Moffett's suggestion of utilizing the fd to map to the clock_id which could then be used by the posix clocks interface?

Although I'm still not sure if it wouldn't be so hard to just simply increment the id on each registration and index to a clock through a reasonably small hash table. I suspect that would solve the enumeration/reuse issue without much trouble (but again, I'm open to being corrected if I'm missing something larger).

But yes, in summary, this is an issue to be addressed one way or another.

thanks
-john

author maintainer
#47 lore raw
In reply to Alan Cox
Quoted 8 lines · Alan Cox

Drop the clockid_t and swap it for a file handle like a proper Unix or Linux interface. The rest is much the same

fd = open /sys/class/timesource/[whatever]

various queries you may want to do to check the name etc

fclock_adjtime(fd, ...)

Okay, but lets extend the story:

	clock_getttime(fd, ...);

	clock_settime(fd, ...);

	timer_create(fd, ...);

Can you agree to that as well?

(We would need to ensure that 'fd' avoids the range 0 to MAX_CLOCKS).

Richard

#48 lore raw
Quoted 18 lines · On Fri, 24 Sep 2010 15:14:07 +0200 Richard Cochran <[email protected]>…
Quoted 8 lines · Alan Cox

Drop the clockid_t and swap it for a file handle like a proper Unix or Linux interface. The rest is much the same

fd = open /sys/class/timesource/[whatever]

various queries you may want to do to check the name etc

fclock_adjtime(fd, ...)

Okay, but lets extend the story:

	clock_getttime(fd, ...);

	clock_settime(fd, ...);

	timer_create(fd, ...);
Quoted 3 lines

Can you agree to that as well?

(We would need to ensure that 'fd' avoids the range 0 to MAX_CLOCKS).

You can't do that avoiding as you might like because the behaviour of file handle numbering is defined by the standards. Hence the "f*" versions of the calls (and of lots of other stuff)

Whether you add new syscalls or do the fd passing using flags and hide the ugly bits in glibc is another question.

#49 lore raw
Quoted 6 lines

You can't do that avoiding as you might like because the behaviour of file handle numbering is defined by the standards. Hence the "f*" versions of the calls (and of lots of other stuff)

Whether you add new syscalls or do the fd passing using flags and hide the ugly bits in glibc is another question.

To add an example of what I mean you might end up defining "CLOCK_FD" to indicate to use the fd in the struct, but given syscalls are trivial codewise and would end up as

	fclock_foo(int fd, blah)
	{
		clock = fd_to_clock(fd);
		if (error)
			return error
		clock_do_foo(clock, blah);
		clock_put(clock);
	}

and

	clock_foo(int posixid, blah)
	{
		clock = posix_to_clock(posixid)
		...
		rest same
	}

as wrappers it seems hardly worth adding ugly hacks

#50 lore raw
In reply to Alan Cox
Quoted 2 lines · Alan Cox

Whether you add new syscalls or do the fd passing using flags and hide the ugly bits in glibc is another question.

Use device specific ioctls instead of syscalls?

#51 lore raw
Quoted 7 lines · On Mon, 27 Sep 2010 10:56:09 -0500 (CDT) Christoph Lameter <[email protected]> wrote:
Quoted 2 lines · Alan Cox

Whether you add new syscalls or do the fd passing using flags and hide the ugly bits in glibc is another question.

Use device specific ioctls instead of syscalls?

Some of the ioctls are probably not device specific, the job of the OS in part is to present a unified interface. We already have a mess of HPET and RTC driver ioctls.

Some of it undoubtedly is device specific.

Alan

author maintainer
#52 lore raw
Quoted 6 lines · Alan Cox
Quoted 3 lines · On Mon, 27 Sep 2010 10:56:09 -0500 (CDT) Christoph Lameter <[email protected]> wrote:

On Fri, 24 Sep 2010, Alan Cox wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Whether you add new syscalls or do the fd passing using flags and hide the ugly bits in glibc is another question.

Use device specific ioctls instead of syscalls?

Some of the ioctls are probably not device specific, the job of the OS in part is to present a unified interface. We already have a mess of HPET and RTC driver ioctls.

Yes, and the whole point of introducing a PTP hardare clock API was to avoid each new clock driver introducing yet another ioctl interface.

I had proposed a standard ioctl interface for PTP hardware clocks, but that interface was rightly criticized for duplicating the posix clock API. It does not make sense to have multiple interfaces with the exact same functionality.

It is impossible to support every last feature of every possible hardware clock with a generic interface, so there will always need to be special ioctls for such features.

However, some clock functions *are* completely generic and apply to every clock:

- set time
- get time
- adjust the frequency by N ppb
- shift the time by a given offset

The first two are provided by the posix clock interface, the third by the NTP adjtimex call, which also can support (by a single mode extension) the last item.

Richard