ptp: IEEE 1588 hardware clock support
Subsystems (15)
Richard Cochran
maintainerv7 cover ptp: IEEE 1588 hardware clock support
Here comes PTP Hardware Clock (PHC) support, version 7, and hopefully everyone will like it.
The first four patches provide infrastructure supporting dynamic POSIX clock devices. This new code will be useful for other kinds of new clocks, not just PHCs. The last four patches implement the PHC code.
Table of Contents
=================
1 Introduction
2 Patch Set Overview
3 Previous Discussions
4 Design Issues
4.1 Clock Operations
4.2 System Calls for Clock Tuning
4.2.1 Using the POSIX Clock API
4.2.2 Tuning a POSIX Clock
4.2.3 Dynamic POSIX Clock IDs
4.3 Synchronizing the Linux System Time
4.4 Ancillary PHC Operations
4.5 User timers
5 Drivers
5.1 Supported Hardware Clocks
5.2 Open Driver Issues
5.2.1 DP83640
5.2.2 IXP465
6 Diff Stat
1 Introduction
~~~~~~~~~~~~~~~
The aim of this patch set is to add support for PTP Hardware Clocks (PHCs) into the Linux kernel. 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 Patch Set Overview
~~~~~~~~~~~~~~~~~~~~~
- Patch 1 adds an ADJ_SETOFFSET mode bit to the NTP timex structure. The new bit allows a correction of a time offset.
- Patch 2 adds a new system call, clock_adjtime(), which is like the NTP adjtimex call for POSIX clocks.
- Patches 3 and 4 add dynamic POSIX clocks.
- The remaining patches add the core PHC code, complete with three drivers.
3 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.
- IEEE 1588 hardware clock support [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]
- IEEE 1588 hardware clock support [V6]
[http://lkml.org/lkml/2010/9/23/310]
- Dynamic clock devices [RFC]
[http://lkml.org/lkml/2010/11/4/290]
4 Design Issues
~~~~~~~~~~~~~~~~
The following treatment of the design issues previously appeared with the V6 version of the patch set. The text has been updated to reflect the current patch set.
4.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.
4.2 System Calls for Clock Tuning
==================================
4.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.
4.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 *);
Using the timex interface 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.
4.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 represent the clocks as character devices. After opening the character device, the file descriptor may be also used as a clock id by applying a simple transformation.
4.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.
4.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.
4.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.
5 Drivers
~~~~~~~~~~
5.1 Supported Hardware Clocks
==============================
+ 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)
5.2 Open Driver Issues
=======================
5.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).
5.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?
6 Diff Stat
~~~~~~~~~~~
Richard Cochran (8):
ntp: add ADJ_SETOFFSET mode bit
posix clocks: introduce a syscall for clock tuning. posix clocks: introduce dynamic clocks
posix clocks: hook dynamic clocks into system calls
ptp: Added a brand new class driver for ptp clocks.
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/powerpc/dts-bindings/fsl/tsec.txt | 57 ++ Documentation/ptp/ptp.txt | 94 +++ Documentation/ptp/testptp.c | 352 +++++++++ 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 | 444 +++++++++++ drivers/net/gianfar_ptp_reg.h | 113 +++ drivers/net/phy/Kconfig | 29 + drivers/net/phy/Makefile | 1 + drivers/net/phy/dp83640.c | 890 +++++++++++++++++++++++ drivers/net/phy/dp83640_reg.h | 261 +++++++ drivers/ptp/Kconfig | 53 ++ drivers/ptp/Makefile | 7 + drivers/ptp/ptp_chardev.c | 144 ++++ drivers/ptp/ptp_clock.c | 317 ++++++++ drivers/ptp/ptp_ixp46x.c | 342 +++++++++ drivers/ptp/ptp_private.h | 68 ++ drivers/ptp/ptp_sysfs.c | 230 ++++++ include/linux/Kbuild | 1 + include/linux/posix-clock.h | 136 ++++ include/linux/posix-timers.h | 32 +- 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 | 151 ++++- kernel/time/Makefile | 3 +- kernel/time/ntp.c | 26 + kernel/time/posix-clock.c | 376 ++++++++++ 52 files changed, 4911 insertions(+), 70 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 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_private.h create mode 100644 drivers/ptp/ptp_sysfs.c create mode 100644 include/linux/posix-clock.h create mode 100644 include/linux/ptp_clock.h create mode 100644 include/linux/ptp_clock_kernel.h create mode 100644 kernel/time/posix-clock.c
Richard Cochran
maintainerPATCH v7 1/8 ntp: add ADJ_SETOFFSET mode bit
This patch adds a new mode bit into the timex structure. When set, the bit instructs the kernel to add the given time value to the current time.
--- include/linux/timex.h | 3 ++- kernel/time/ntp.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletions(-)
include/linux/timex.h +2 -1
@@ -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/time/ntp.c +26 -0
@@ -454,6 +454,7 @@ static inline void process_adjtimex_modes(struct timex *txc, struct timespec *tsint do_adjtimex (struct timex *txc) {struct timespec ts; +ktime_t delta, kt;int result;/* Validate the data before disabling interrupts */
@@ -482,8 +483,33 @@ int do_adjtimex(struct timex *txc)hrtimer_cancel (&leap_timer); } +if (txc->modes &ADJ_SETOFFSET ) { +/* Validate the delta value. */ +if (txc->time.tv_sec && txc->time.tv_usec <0 ) +return -EINVAL ; + +if (txc->modes &ADJ_NANO ) { +struct timespec tmp; + tmp.tv_sec = txc->time.tv_sec; + tmp.tv_nsec = txc->time.tv_usec; + delta =timespec_to_ktime (tmp); + }else + delta =timeval_to_ktime (txc->time); + +/* Adding the delta should be an "atomic" operation. */ +local_irq_disable (); + } +getnstimeofday (&ts); +if (txc->modes &ADJ_SETOFFSET ) { + kt =timespec_to_ktime (ts); + kt =ktime_add (kt, delta); + ts =ktime_to_timespec (kt); +do_settimeofday (&ts); +local_irq_enable (); + } +write_seqlock_irq (&xtime_lock);if (txc->modes &ADJ_ADJTIME ) {
Signature · 1 line
1.7.0.4
Richard Cochran
maintainerPATCH v7 2/8 posix clocks: introduce a syscall for clock tuning.
In reply to Richard CochranA 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. Any ADJTIMEX(2) operation may be requested via this system call, but various POSIX clocks may or may not support tuning.
--- 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 + drivers/char/mmtimer.c | 1 + include/linux/posix-timers.h | 4 + include/linux/syscalls.h | 2 + kernel/compat.c | 136 +++++++++++++++++++++++------------- kernel/posix-cpu-timers.c | 6 ++ kernel/posix-timers.c | 35 +++++++++ 16 files changed, 150 insertions(+), 51 deletions(-)
arch/arm/include/asm/unistd.h +1 -0
@@ -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
@@ -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
@@ -393,8 +393,9 @@#define __NR_fanotify_mark 372#define __NR_prlimit64 373#define __NR_cacheflush 374 +#define __NR_clock_adjtime 375 -#define __NR_syscall 375 +#define __NR_syscall 376#define NR_syscalls __NR_syscall/* Old optional stuff no one actually uses */
arch/blackfin/mach-common/entry.S +1 -0
@@ -1738,6 +1738,7 @@ ENTRY(_sys_call_table) .long _sys_fanotify_mark .long _sys_prlimit64 .long _sys_cacheflush + .long _sys_clock_adjtime /* 375 */ .rept NR_syscalls-(.-_sys_call_table)/4 .long _sys_ni_syscall
arch/powerpc/include/asm/systbl.h +1 -0
@@ -348,3 +348,4 @@ COMPAT_SYS_SPU(sendmsg) COMPAT_SYS_SPU(recvmsg) COMPAT_SYS_SPU(recvmmsg) SYSCALL_SPU(accept4) +COMPAT_SYS_SPU(clock_adjtime)
arch/powerpc/include/asm/unistd.h +2 -1
@@ -367,10 +367,11 @@#define __NR_recvmsg 342#define __NR_recvmmsg 343#define __NR_accept4 344 +#define __NR_clock_adjtime 345#ifdef __KERNEL__ -#define __NR_syscalls 345 +#define __NR_syscalls 346#define __NR__exit __NR_exit#define NR_syscalls __NR_syscalls
arch/x86/ia32/ia32entry.S +1 -0
@@ -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
@@ -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
@@ -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
@@ -340,3 +340,4 @@ ENTRY(sys_call_table) .long sys_fanotify_init .long sys_fanotify_mark .long sys_prlimit64 /* 340 */ + .long sys_clock_adjtime
drivers/char/mmtimer.c +1 -0
@@ -767,6 +767,7 @@ static struct k_clock sgi_clock = { .res = 0, .clock_set = sgi_clock_set, .clock_get = sgi_clock_get, + .clock_adj = do_posix_clock_noadjtime, .timer_create = sgi_timer_create, .nsleep = do_posix_clock_nonanosleep, .timer_set = sgi_timer_set,
include/linux/posix-timers.h +4 -0
@@ -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 *);
@@ -90,6 +92,7 @@ void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock);int do_posix_clock_nonanosleep (const clockid_t ,int flags,struct timespec *,struct timespec __user *);int do_posix_clock_nosettime (const clockid_t ,struct timespec *tp); +int do_posix_clock_noadjtime (const clockid_t ,struct timex *tx);/* function to call to trigger timer event */ int posix_timer_event (struct k_itimer *timr,int si_private);
@@ -97,6 +100,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
@@ -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,
kernel/compat.c +88 -48
158 lines not on this page · raw message
kernel/posix-cpu-timers.c +6 -0
@@ -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.
@@ -1610,6 +1614,7 @@ static __init int init_posix_cpu_timers(void) .clock_getres = process_cpu_clock_getres, .clock_get = process_cpu_clock_get, .clock_set = do_posix_clock_nosettime, + .clock_adj = do_posix_clock_noadjtime, .timer_create = process_cpu_timer_create, .nsleep = process_cpu_nsleep, .nsleep_restart = process_cpu_nsleep_restart,
@@ -1618,6 +1623,7 @@ static __init int init_posix_cpu_timers(void) .clock_getres = thread_cpu_clock_getres, .clock_get = thread_cpu_clock_get, .clock_set = do_posix_clock_nosettime, + .clock_adj = do_posix_clock_noadjtime, .timer_create = thread_cpu_timer_create, .nsleep = thread_cpu_nsleep, .nsleep_restart = thread_cpu_nsleep_restart,
kernel/posix-timers.c +35 -0
@@ -197,6 +197,11 @@ 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) +{ +return do_adjtimex (t); +} +static int no_timer_create (struct k_itimer *new_timer) {return -EOPNOTSUPP ;
@@ -273,11 +278,13 @@ static __init int init_posix_timers(void) .clock_getres = hrtimer_get_res, .clock_get = posix_ktime_get_ts, .clock_set = do_posix_clock_nosettime, + .clock_adj = do_posix_clock_noadjtime, };struct k_clock clock_monotonic_raw = { .clock_getres = hrtimer_get_res, .clock_get = posix_get_monotonic_raw, .clock_set = do_posix_clock_nosettime, + .clock_adj = do_posix_clock_noadjtime, .timer_create = no_timer_create, .nsleep = no_nsleep, };
@@ -285,6 +292,7 @@ static __init int init_posix_timers(void) .clock_getres = posix_get_coarse_res, .clock_get = posix_get_realtime_coarse, .clock_set = do_posix_clock_nosettime, + .clock_adj = do_posix_clock_noadjtime, .timer_create = no_timer_create, .nsleep = no_nsleep, };
@@ -292,6 +300,7 @@ static __init int init_posix_timers(void) .clock_getres = posix_get_coarse_res, .clock_get = posix_get_monotonic_coarse, .clock_set = do_posix_clock_nosettime, + .clock_adj = do_posix_clock_noadjtime, .timer_create = no_timer_create, .nsleep = no_nsleep, };
@@ -928,6 +937,12 @@ int do_posix_clock_nosettime(const clockid_t clockid, struct timespec *tp) }EXPORT_SYMBOL_GPL (do_posix_clock_nosettime); +int do_posix_clock_noadjtime (const clockid_t which_clock,struct timex *t) +{ +return -EOPNOTSUPP ; +} +EXPORT_SYMBOL_GPL (do_posix_clock_noadjtime); +int do_posix_clock_nonanosleep (const clockid_t clock,int flags,struct timespec *t,struct timespec __user *r) {
@@ -969,6 +984,26 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, } +SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, + struct timex __user *, utx) +{ +struct timex ktx; +int err; + +if (copy_from_user (&ktx, utx,sizeof (ktx))) +return -EFAULT ; + +if (invalid_clockid (which_clock)) +return -EINVAL ; + + err =CLOCK_DISPATCH (which_clock, clock_adj, (which_clock, &ktx)); + +if (copy_to_user (utx, &ktx,sizeof (ktx))) +return -EFAULT ; + +return err; +} + SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct timespec __user *, tp) {
Signature · 1 line
1.7.0.4
Richard Cochran
maintainerPATCH v7 3/8 posix clocks: introduce dynamic clocks
In reply to Richard CochranThis patch adds support for adding and removing posix clocks. The clock lifetime cycle is patterned after usb devices. Each clock is represented by a standard character device. In addition, the driver may optionally implemented custom character device operations.
The dynamic posix clocks do not yet do anything useful. This patch merely provides some needed infrastructure.
--- include/linux/posix-clock.h | 111 ++++++++++++++++++++++ kernel/time/Makefile | 3 +- kernel/time/posix-clock.c | 217 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 330 insertions(+), 1 deletions(-) create mode 100644 include/linux/posix-clock.h create mode 100644 kernel/time/posix-clock.c
include/linux/posix-clock.h new file +111 -0
@@ -0,0 +1,111 @@ +/* +* posix-clock.h - support for dynamic clock devices +* +* 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 _LINUX_POSIX_CLOCK_H_ +#define _LINUX_POSIX_CLOCK_H_ + +#include <linux/fs.h> +#include <linux/poll.h> +#include <linux/posix-timers.h> + +/** +* struct posix_clock_fops - character device operations +* +* Every posix clock is represented by a character device. Drivers may +* optionally offer extended capabilities by implementing these +* functions. The character device file operations are first handled +* by the clock device layer, then passed on to the driver by calling +* these functions. +* +* The clock device layer already uses fp->private_data, so drivers +* are provided their private data via the 'priv' paramenter. +*/ +void *posix_clock_private (struct file *fp); + +struct posix_clock_fops { +int (*fasync) (void *priv,int fd,struct file *file,int on); +int (*mmap) (void *priv,struct vm_area_struct *vma); +int (*open) (void *priv,fmode_t f_mode); +int (*release) (void *priv); +long (*ioctl) (void *priv,unsigned int cmd,unsigned long arg); +long (*compat_ioctl) (void *priv,unsigned int cmd,unsigned long arg); +ssize_t (*read) (void *priv,uint flags,char __user *buf,size_t cnt); +unsigned int (*poll) (void *priv,struct file *file,poll_table *wait); +}; + +/** +* struct posix_clock_operations - functional interface to the clock +* @owner: The clock driver should set to THIS_MODULE. +* @fops: Optional custom character device operations +* @clock_adjtime: Adjust the clock +* @clock_gettime: Read the current time +* @clock_getres: Get the clock resolution +* @clock_settime: Set the current time value +* @timer_create: Create a new timer +* @timer_delete: Remove a previously created timer +* @timer_gettime: Get remaining time and interval of a timer +* @timer_setttime: Set a timer's initial expiration and interval +*/ +struct posix_clock_operations { +struct module *owner; +struct posix_clock_fops fops; +int (*clock_adjtime)(void *priv,struct timex *tx); +int (*clock_gettime)(void *priv,struct timespec *ts); +int (*clock_getres) (void *priv,struct timespec *ts); +int (*clock_settime)(void *priv,const struct timespec *ts); +int (*timer_create) (void *priv,struct k_itimer *kit); +int (*timer_delete) (void *priv,struct k_itimer *kit); +void (*timer_gettime)(void *priv,struct k_itimer *kit, +struct itimerspec *tsp); +int (*timer_settime)(void *priv,struct k_itimer *kit,int flags, +struct itimerspec *tsp,struct itimerspec *old); +}; + +/** +* struct posix_clock - an opaque type +*/ +struct posix_clock ; + +/** +* posix_clock_create() - register a new clock +* @cops: Pointer to the clock's interface +* @devid: Allocated device id +* @priv: Private data passed back to the driver via the interface functions +* +* A clock driver calls this function to register itself with the +* clock device subsystem. The 'cops' argument must point to +* persistent data, so the caller should pass a static global. +* +* Returns a pointer to a new clock device, or PTR_ERR on error. +*/ +struct posix_clock *posix_clock_create (struct posix_clock_operations *cops, +dev_t devid,void *priv); + +/** +* posix_clock_destroy() - unregister a clock +* @clk: Pointer obtained via posix_clock_create() +* +* A clock driver calls this function to remove itself from the clock +* device subsystem. The posix_clock itself will remain (in an +* inactive state) until its reference count drops to zero, at which +* point it will be deallocated. +*/ +void posix_clock_destroy (struct posix_clock *clk); + +#endif
kernel/time/Makefile +2 -1
@@ -1,4 +1,5 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o \ +timecompare.o timeconv.o posix-clock.o obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
kernel/time/posix-clock.c new file +217 -0
217 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Richard Cochran
maintainerPATCH v7 4/8 posix clocks: hook dynamic clocks into system calls
In reply to Richard CochranThis patch lets the various posix clock and timer system calls use dynamic posix clocks, as well as the traditional static clocks.
For the performance critical calls (eg clock_gettime) the code path from the traditional static clocks is preserved.
The following system calls are affected:
- clock_adjtime (brand new syscall)
- clock_gettime
- clock_getres
- clock_settime
- timer_create
- timer_delete
- timer_gettime
- timer_settime
--- include/linux/posix-clock.h | 25 +++++++ include/linux/posix-timers.h | 28 +++++++- include/linux/time.h | 2 + kernel/posix-timers.c | 122 +++++++++++++++++++++++++++----- kernel/time/posix-clock.c | 159 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 316 insertions(+), 20 deletions(-)
include/linux/posix-clock.h +25 -0
@@ -108,4 +108,29 @@ struct posix_clock *posix_clock_create(struct posix_clock_operations *cops,*/ void posix_clock_destroy (struct posix_clock *clk); +/** +* clockid_to_posix_clock() - obtain clock device pointer from a clock id +* @id: user space clock id +* +* Returns a pointer to the clock device, or NULL if the id is not a +* dynamic clock id. +*/ +struct posix_clock *clockid_to_posix_clock (clockid_t id); + +/* +* The following functions are only to be called from posix-timers.c +*/ +int pc_clock_adjtime (struct posix_clock *clk,struct timex *tx); +int pc_clock_gettime (struct posix_clock *clk,struct timespec *ts); +int pc_clock_getres (struct posix_clock *clk,struct timespec *ts); +int pc_clock_settime (struct posix_clock *clk,const struct timespec *ts); + +int pc_timer_create (struct posix_clock *clk,struct k_itimer *kit); +int pc_timer_delete (struct posix_clock *clk,struct k_itimer *kit); + +void pc_timer_gettime (struct posix_clock *clk,struct k_itimer *kit, +struct itimerspec *ts); +int pc_timer_settime (struct posix_clock *clk,struct k_itimer *kit, +int flags,struct itimerspec *ts,struct itimerspec *old); +#endif
include/linux/posix-timers.h +27 -1
@@ -18,10 +18,22 @@ struct cpu_timer_list {int firing; }; +/* Bit fields within a clockid: +* +* The most significant 29 bits hold either a pid or a file descriptor. +* +* Bit 2 indicates whether a cpu clock refers to a thread or a process. +* +* Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3. +* +* A clockid is invalid if bits 2, 1, and 0 all set (see also CLOCK_INVALID +* in include/linux/time.h) +*/ +#define CPUCLOCK_PID (clock) ((pid_t) ~((clock) >> 3))#define CPUCLOCK_PERTHREAD (clock) \ (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0) -#define CPUCLOCK_PID_MASK 7 +#define CPUCLOCK_PERTHREAD_MASK 4#define CPUCLOCK_WHICH (clock) ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)#define CPUCLOCK_CLOCK_MASK 3
@@ -29,12 +41,26 @@ struct cpu_timer_list {#define CPUCLOCK_VIRT 1#define CPUCLOCK_SCHED 2#define CPUCLOCK_MAX 3 +#define CLOCKFD CPUCLOCK_MAX +#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)#define MAKE_PROCESS_CPUCLOCK (pid, clock) \ ((~(clockid_t) (pid) << 3) | (clockid_t) (clock))#define MAKE_THREAD_CPUCLOCK (tid, clock) \ MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK) +#define FD_TO_CLOCKID (fd) ((clockid_t) (fd << 3) | CLOCKFD) +#define CLOCKID_TO_FD (clk) (((unsigned int) clk) >> 3) + +static inline bool clock_is_static (const clockid_t id) +{ +if (0 == (id & ~CLOCKFD_MASK )) +return true; +if (CLOCKFD == (id &CLOCKFD_MASK )) +return false; +return true; +} +/* POSIX.1b interval timer structure. */ struct k_itimer {struct list_head list;/* free/ allocate list */
include/linux/time.h +2 -0
@@ -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 +103 -19
217 lines not on this page · raw message
kernel/time/posix-clock.c +159 -0
171 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Richard Cochran
maintainerPATCH v7 5/8 ptp: Added a brand new class driver for ptp clocks.
In reply to Richard CochranThis 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 | 352 +++++++++++++++++++++++++++++++++++ Documentation/ptp/testptp.mk | 33 ++++ drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/ptp/Kconfig | 27 +++ drivers/ptp/Makefile | 6 + drivers/ptp/ptp_chardev.c | 144 ++++++++++++++ drivers/ptp/ptp_clock.c | 317 +++++++++++++++++++++++++++++++ drivers/ptp/ptp_private.h | 68 +++++++ drivers/ptp/ptp_sysfs.c | 230 +++++++++++++++++++++++ include/linux/Kbuild | 1 + include/linux/ptp_clock.h | 79 ++++++++ include/linux/ptp_clock_kernel.h | 139 ++++++++++++++ 15 files changed, 1600 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
@@ -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 +352 -0
352 lines not on this page · raw message
Documentation/ptp/testptp.mk new file +33 -0
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 +144 -0
144 lines not on this page · raw message
drivers/ptp/ptp_clock.c new file +317 -0
317 lines not on this page · raw message
drivers/ptp/ptp_private.h new file +68 -0
68 lines not on this page · raw message
drivers/ptp/ptp_sysfs.c new file +230 -0
230 lines not on this page · raw message
include/linux/Kbuild +1 -0
@@ -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
Richard Cochran
maintainerPATCH v7 6/8 ptp: Added a clock that uses the eTSEC found on the MPC85xx.
In reply to Richard CochranThe 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 | 444 +++++++++++++++++++++++ drivers/net/gianfar_ptp_reg.h | 113 ++++++ drivers/ptp/Kconfig | 13 + 9 files changed, 684 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -31,6 +31,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 +444 -0
444 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
@@ -24,4 +24,17 @@ config PTP_1588_CLOCK To compile this driver as a module, choose M here: the module will be called ptp. +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
Richard Cochran
maintainerPATCH v7 7/8 ptp: Added a clock driver for the IXP46x.
In reply to Richard CochranThis 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 | 342 +++++++++++++++++++++++++ 5 files changed, 625 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
@@ -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
@@ -37,4 +37,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
@@ -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_IXP46X) += ptp_ixp46x.o
drivers/ptp/ptp_ixp46x.c new file +342 -0
342 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Richard Cochran
maintainerPATCH v7 8/8 ptp: Added a clock driver for the National Semiconductor PHYTER.
In reply to Richard CochranThis 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 | 890 +++++++++++++++++++++++++++++++++++++++++ drivers/net/phy/dp83640_reg.h | 261 ++++++++++++ 4 files changed, 1181 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
@@ -76,6 +76,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
@@ -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 +890 -0
890 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
Re: [PATCH V7 2/8] posix clocks: introduce a syscall for clock tuning.
In reply to Richard CochranQuoted 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. Any ADJTIMEX(2) operation may be requested via this system call, but various POSIX clocks may or may not support tuning.
The syscall implementation looks good to me.
It would be good to add it to include/asm-generic/unistd.h as well, so that tile and future architectures get it, too.
Arnd Bergmann
maintainerRe: [PATCH V7 5/8] ptp: Added a brand new class driver for ptp clocks.
In reply to Richard CochranQuoted 9 lines · 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.
Signed-off-by Richard Cochran <[email protected]>
Looks fine to me, with one tiny comment:
drivers/ptp/ptp_clock.c line 42
Quoted 4 lines
drivers/ptp/ptp_clock.c new file +4
-0+ +/* private globals */ + +extern struct device_attribute ptp_dev_attrs[];/* see ptp_sysfs.c */
The declaration is in a C file, better move it into a header in order to make sure the definition matches the declaration.
Arnd
Re: [PATCH V7 5/8] ptp: Added a brand new class driver for ptp clocks.
In reply to Richard CochranQuoted 9 lines · 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.
Signed-off-by Richard Cochran <[email protected]>
Regarding the PPS subsystem it looks ok for me.
Ciao,
Rodolfo
Signature · 5 lines
GNU/Linux Solutions e-mail: [email protected] Linux Device Driver [email protected] Embedded Systems phone: +39 349 2432127 UNIX programming skype: rodolfo.giometti Freelance ICT Italia - Consulente ICT Italia - www.consulenti-ict.it
Arnd Bergmann
maintainerRe: [PATCH V7 3/8] posix clocks: introduce dynamic clocks
In reply to Richard CochranQuoted 10 lines · Richard Cochran
This patch adds support for adding and removing posix clocks. The clock lifetime cycle is patterned after usb devices. Each clock is represented by a standard character device. In addition, the driver may optionally implemented custom character device operations.
The dynamic posix clocks do not yet do anything useful. This patch merely provides some needed infrastructure.
include/linux/posix-clock.h line 50
Quoted 10 lines
include/linux/posix-clock.h new file +10
-0+struct posix_clock_fops { +int (*fasync) (void *priv,int fd,struct file *file,int on); +int (*mmap) (void *priv,struct vm_area_struct *vma); +int (*open) (void *priv,fmode_t f_mode); +int (*release) (void *priv); +long (*ioctl) (void *priv,unsigned int cmd,unsigned long arg); +long (*compat_ioctl) (void *priv,unsigned int cmd,unsigned long arg); +ssize_t (*read) (void *priv,uint flags,char __user *buf,size_t cnt); +unsigned int (*poll) (void *priv,struct file *file,poll_table *wait); +};
Thanks for the change to a private ops structure. Three more suggestions for this:
* I would recommend starting without a compat_ioctl operation if you can. Just mandate that all ioctls for posix clocks are compatible and call fops->ioctl from the posix_clock_compat_ioctl() function.
If you ever need compat_ioctl handling, it can still be added later.
* Instead of passing a void pointer, why not pass a struct posix_clock pointer to the lower device and use container_of? That follows what we do in other subsystems and allows you save one allocation, by including the posix_clock into the specific clock struct like ptp_clock.
include/linux/posix-clock.h line 68
Quoted 4 lines
include/linux/posix-clock.h new file +4
-0+struct posix_clock_operations { +struct module *owner; +struct posix_clock_fops fops; +int (*clock_adjtime)(void *priv,struct timex *tx);
You can easily combine the two structures and get rid of the extra struct posix_clock_fops by moving its operations into posix_clock_operations.
Looks really good otherwise.
Arnd
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to Richard CochranQuoted 5 lines · Richard Cochran
kernel/time/ntp.c +5
-0+if (txc->modes &ADJ_SETOFFSET ) { +/* Validate the delta value. */ +if (txc->time.tv_sec && txc->time.tv_usec <0 ) +return -EINVAL ; +
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 500
Quoted 10 lines
kernel/time/ntp.c +10
-0+if (txc->modes &ADJ_NANO ) { +struct timespec tmp; + tmp.tv_sec = txc->time.tv_sec; + tmp.tv_nsec = txc->time.tv_usec; + delta =timespec_to_ktime (tmp); + }else + delta =timeval_to_ktime (txc->time); + +/* Adding the delta should be an "atomic" operation. */ +local_irq_disable ();
I really do not like that conditional irq_disable(), especially as we disable them further down again and we only safe the getnstimeofday() call in the non ADJSETOFFSET code path.
So we really better do that unconditionally before getnstimeofday() with an appropriate comment and change the write_seqlock_irq() to write_seqlock().
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 513
Quoted 13 lines
kernel/time/ntp.c +10
-0+ } +getnstimeofday (&ts); +if (txc->modes &ADJ_SETOFFSET ) { + kt =timespec_to_ktime (ts); + kt =ktime_add (kt, delta); + ts =ktime_to_timespec (kt); +do_settimeofday (&ts); +local_irq_enable (); + } +write_seqlock_irq (&xtime_lock);
Thanks,
tglx
Quoted 1 line
Re: [PATCH V7 2/8] posix clocks: introduce a syscall for clock tuning.
In reply to Richard CochranQuoted 3 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.
Can you please split that patch into one providing the syscall itself and for each architecture a separate one ?
Looks good otherwise.
Re: [PATCH V7 3/8] posix clocks: introduce dynamic clocks
In reply to Richard CochranQuoted 5 lines · Richard Cochran
include/linux/posix-clock.h new file +3
-0+/** +* struct posix_clock_fops - character device operations +*
include/linux/posix-clock.h line 39
Quoted 10 lines
include/linux/posix-clock.h new file +10
-0+* Every posix clock is represented by a character device. Drivers may +* optionally offer extended capabilities by implementing these +* functions. The character device file operations are first handled +* by the clock device layer, then passed on to the driver by calling +* these functions. +* +* The clock device layer already uses fp->private_data, so drivers +* are provided their private data via the 'priv' paramenter. +*/ +void *posix_clock_private (struct file *fp);
Leftover ? There is neither a caller nor an implementation
include/linux/posix-clock.h line 47
Quoted 7 lines
include/linux/posix-clock.h new file +7
-0+struct posix_clock_fops { +int (*fasync) (void *priv,int fd,struct file *file,int on); +int (*mmap) (void *priv,struct vm_area_struct *vma); +int (*open) (void *priv,fmode_t f_mode); +int (*release) (void *priv); +long (*ioctl) (void *priv,unsigned int cmd,unsigned long arg); +long (*compat_ioctl) (void *priv,unsigned int cmd,unsigned long arg);
Do we really need a compat_ioctl ?
kernel/time/Makefile line 2
Quoted 9 lines
include/linux/posix-clock.h new file +2
-0+ssize_t (*read) (void *priv,uint flags,char __user *buf,size_t cnt); +unsigned int (*poll) (void *priv,struct file *file,poll_table *wait);kernel/time/Makefile +2
-1@@ -1,4 +1,5 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o \ +timecompare.o timeconv.o posix-clock.o
Please start a new obj-y += line instead
kernel/time/posix-clock.c line 26
Quoted 4 lines
kernel/time/posix-clock.c new file +2
-0+ +#define MAX_CLKDEV BITS_PER_LONG
Please put some more space between the MAX_CLKDEV and BITS_PER_LONG. I had to look three times to not read it as a single word.
kernel/time/posix-clock.c line 28
Quoted 2 lines
kernel/time/posix-clock.c new file +2
-0+static DECLARE_BITMAP(clocks_map, MAX_CLKDEV); +static DEFINE_MUTEX (clocks_mutex);/* protects 'clocks_map' */
Please avoid tail comments
kernel/time/posix-clock.c line 35
Quoted 6 lines
kernel/time/posix-clock.c new file +6
-0+struct posix_clock { +struct posix_clock_operations *ops; +struct cdev cdev; +struct kref kref; +struct mutex mux; +void *priv;
You can get away with that private pointer and all the void * arguments to the various posix_clock_operations, if you mandate that the posix_clock_operations are embedded into a clock specific data structure.
So void *priv would become struct posix_clock_operations *clkops and you can get your private data in the clock implementation with container_of().
kernel/time/posix-clock.c line 37
Quoted 2 lines
kernel/time/posix-clock.c new file +2
-0+int index; +bool zombie;
Ths field is only set, but nowhere else used. What's the purpose ? Leftover ?
kernel/time/posix-clock.c line 113
Quoted 13 lines
kernel/time/posix-clock.c new file +12
-0+}; + +static void delete_clock (struct kref *kref); + + +static int posix_clock_open (struct inode *inode,struct file *fp) +{ +struct posix_clock *clk = + container_of(inode->i_cdev, struct posix_clock, cdev); + +kref_get (&clk->kref); + fp->private_data = clk;
fp->private_data should only be set on success. And this will leak a ref count when the clock open function fails.
What's that kref protecting here ?
kernel/time/posix-clock.c line 124
Quoted 11 lines
kernel/time/posix-clock.c new file +11
-0+ +if (clk->ops->fops.open) +return clk->ops->fops.open (clk->priv, fp->f_mode); +else +return 0 ; +} + +static int posix_clock_release (struct inode *inode,struct file *fp) +{ +struct posix_clock *clk = fp->private_data; +int err =0 ;
fp->private_data should be set to NULL in the release function.
Quoted 14 lines
kernel/time/posix-clock.c new file +13
-0+if (clk->ops->fops.release) + err = clk->ops->fops.release (clk->priv); + +kref_put (&clk->kref, delete_clock); +struct posix_clock *posix_clock_create (struct posix_clock_operations *cops, +dev_t devid,void *priv) +{ +struct posix_clock *clk; +int err; + +mutex_lock (&clocks_mutex); + + err = -ENOMEM ;
kernel/time/posix-clock.c line 167
Quoted 10 lines
kernel/time/posix-clock.c new file +10
-0+ clk =kzalloc (sizeof (*clk),GFP_KERNEL ); +if (!clk) + goto no_memory; + + err = -EBUSY ; + clk->index =find_first_zero_bit (clocks_map,MAX_CLKDEV ); +if (clk->index <MAX_CLKDEV ) +set_bit (clk->index, clocks_map); +else + goto no_index;
if (clk->index >= MAX_CLKDEV) goto no_index; set_bit(clk->index, clocks_map);
Makes it better readable.
Quoted 12 lines
kernel/time/posix-clock.c new file +12
-0+static void delete_clock (struct kref *kref) +{ +struct posix_clock *clk = + container_of(kref, struct posix_clock, kref); + +mutex_lock (&clocks_mutex); +clear_bit (clk->index, clocks_map); +mutex_unlock (&clocks_mutex); + +mutex_destroy (&clk->mux); +kfree (clk); +}
kernel/time/posix-clock.c line 215
Quoted 10 lines
kernel/time/posix-clock.c new file +10
-0+ +void posix_clock_destroy (struct posix_clock *clk) +{ +cdev_del (&clk->cdev); + +mutex_lock (&clk->mux); + clk->zombie = true; +mutex_unlock (&clk->mux); + +kref_put (&clk->kref, delete_clock);
I still have some headache to understand that kref / delete_clock magic here.
Thanks,
tglx
Re: [PATCH V7 4/8] posix clocks: hook dynamic clocks into system calls
In reply to Richard CochranOn Thu, 16 Dec 2010, Richard Cochran wrote:
Can you please split this into infrastructure and conversion of posix-timer.c ?
Quoted 10 lines
include/linux/posix-clock.h +2
-0@@ -108,4 +108,29 @@ struct posix_clock *posix_clock_create(struct posix_clock_operations *cops,*/ void posix_clock_destroy (struct posix_clock *clk); +/** +* clockid_to_posix_clock() - obtain clock device pointer from a clock id
include/linux/posix-clock.h struct posix_clock *posix_clock_create(struct posix_clock_operations *cops, · line 122
Quoted 10 lines
include/linux/posix-clock.h +10
-0+* @id: user space clock id +* +* Returns a pointer to the clock device, or NULL if the id is not a +* dynamic clock id. +*/ +struct posix_clock *clockid_to_posix_clock (clockid_t id); + +/* +* The following functions are only to be called from posix-timers.c +*/
Then they should be in a header file which is not consumable by the general public. e.g. kernel/time/posix-clock.h
Quoted 11 lines
include/linux/posix-clock.h +11
-0+int pc_clock_adjtime (struct posix_clock *clk,struct timex *tx); +int pc_clock_gettime (struct posix_clock *clk,struct timespec *ts); +int pc_clock_getres (struct posix_clock *clk,struct timespec *ts); +int pc_clock_settime (struct posix_clock *clk,const struct timespec *ts); + +int pc_timer_create (struct posix_clock *clk,struct k_itimer *kit); +int pc_timer_delete (struct posix_clock *clk,struct k_itimer *kit); + +void pc_timer_gettime (struct posix_clock *clk,struct k_itimer *kit, +struct itimerspec *ts); +int pc_timer_settime(struct posix_clock *clk, struct k_itimer *kit,
include/linux/posix-clock.h struct posix_clock *posix_clock_create(struct posix_clock_operations *cops,
Quoted 10 lines
include/linux/posix-clock.h +9
-0+ int flags, struct itimerspec *ts, struct itimerspec *old); +#endif + +static inline bool clock_is_static (const clockid_t id) +{ +if (0 == (id & ~CLOCKFD_MASK )) +return true; +if (CLOCKFD == (id &CLOCKFD_MASK )) +return false;
Please use the usual kernel notation.
Quoted 27 lines
include/linux/posix-clock.h +3
-0+return true; +} +/* POSIX.1b interval timer structure. */ struct k_itimer {struct list_head list;/* free/ allocate list */ include/linux/time.h +3
-0@@ -299,6 +299,8 @@ struct itimerval {#define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC)#define CLOCKS_MONO CLOCK_MONOTONIC +#define CLOCK_INVALID -1 +@@ -712,6 +720,7 @@ common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)SYSCALL_DEFINE2 (timer_gettime, timer_t, timer_id, struct itimerspec __user *, setting) { +struct posix_clock *clk_dev;struct k_itimer *timr;struct itimerspec cur_setting;unsigned long flags;@@ -720,7 +729,15 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,if (!timr)return -EINVAL ;
include/linux/time.h SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, · line 739
Quoted 10 lines
include/linux/time.h +8
-1-CLOCK_DISPATCH (timr->it_clock, timer_get, (timr, &cur_setting)); +if (likely (clock_is_static (timr->it_clock))) { + +CLOCK_DISPATCH (timr->it_clock, timer_get, (timr, &cur_setting)); + + }else { + clk_dev =clockid_to_posix_clock (timr->it_clock); +if (clk_dev) +pc_timer_gettime (clk_dev, timr, &cur_setting);
Why this extra step ? Why can't you call
pc_timer_gettime(timr, &cur_setting);
You already established that the timer is fd based, so let the pc_timer_* functions deal with it.
Quoted 20 lines
include/linux/time.h +4
-2+ }unlock_timer (timr, flags);@@ -811,6 +828,7 @@ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, const struct itimerspec __user *, new_setting, struct itimerspec __user *, old_setting) { + struct posix_clock *clk_dev;struct k_itimer *timr;struct itimerspec new_spec, old_spec;int error =0 ;@@ -831,8 +849,19 @@ retry:if (!timr)return -EINVAL ; - error =CLOCK_DISPATCH (timr->it_clock, timer_set, - (timr, flags, &new_spec, rtn)); +if (likely (clock_is_static (timr->it_clock))) { +
include/linux/time.h retry: · line 863
Quoted 10 lines
include/linux/time.h +10
-0+ error =CLOCK_DISPATCH (timr->it_clock, timer_set, + (timr, flags, &new_spec, rtn)); + + }else { + clk_dev =clockid_to_posix_clock (timr->it_clock); +if (clk_dev) + error =pc_timer_settime (clk_dev, timr, + flags, &new_spec, rtn); +else + error = -EINVAL ;
Ditto. pc_timer_settime() can return -EINVAL when there is no valid fd.
Quoted 8 lines
include/linux/time.h +1
-1@@ -957,26 +993,51 @@ EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep); SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, const struct timespec __user *, tp) { +struct posix_clock *clk_dev =NULL ;struct timespec new_tp; -if (invalid_clockid (which_clock))
include/linux/time.h EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep); · line 1007
Quoted 10 lines
include/linux/time.h +9
-1-return -EINVAL ; +if (likely (clock_is_static (which_clock))) { + +if (invalid_clockid (which_clock)) +return -EINVAL ; + }else { + clk_dev =clockid_to_posix_clock (which_clock); +if (!clk_dev) +return -EINVAL ; + }
It's not a problem to check the validity of that clock fd after the copy_from_user. That's an error case and we don't care about whether we return EINVAL here or later. And with your current code this can happen anyway as you don't hold a reference to the fd. And we do the same thing for posix_cpu_timers as well. See invalid_clockid()
include/linux/time.h EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep); · line 1014
Quoted 7 lines
include/linux/time.h +3
-0if (copy_from_user (&new_tp, tp,sizeof (*tp)))return -EFAULT ; +if (unlikely (clk_dev)) +return pc_clock_settime (clk_dev, &new_tp); +return CLOCK_DISPATCH (which_clock, clock_set, (which_clock, &new_tp));
I really start to wonder whether we should cleanup that whole CLOCK_DISPATCH macro magic and provide real inline functions for each of the clock functions instead.
static inline int dispatch_clock_settime(const clockid_t id, struct timespec *tp)
{
if (id >= 0) {
return posix_clocks[id].clock_set ?
posic_clocks[id].clock_set(id, tp) : -EINVAL;
}
if (posix_cpu_clock(id))
return -EINVAL;
return pc_timers_clock_set(id, tp);
}That is a bit of work, but the code will be simpler and we just do the normal thing of function pointer structures. Stuff which is not implemented does not magically become called via some common function. There is no point in doing that. We just have to fill in the various k_clock structs with the correct pointers in the first place and let the NULL case return a sensible error value. The data structure does not become larger that way. It's a little bit more init code, but that's fine if we make the code better in general. In that case it's not even more init code, it's just filling the data structures which we register.
That needs to be done in two steps:
1) cleanup CLOCK_DISPATCH
2) add the pc_timer_* extras
That will make the thing way more palatable than working around the restrictions of CLOCK_DISPATCH and adding the hell to each syscall.
The second step would be a patch consisting of exactly nine lines:
{
if (id >= 0)
return .....
if (posix_cpu_clock_id(id))
return ....- return -EINVAL;
+ return pc_timer_xxx(...);
}
Please don't tell me now that we could even hack this into CLOCK_DISPATCH. *shudder*
Quoted 17 lines
include/linux/time.h +17
-0+ +struct posix_clock *clockid_to_posix_clock (const clockid_t id) +{ +struct posix_clock *clk =NULL ; +struct file *fp; + +if (clock_is_static (id)) +return NULL ; + + fp =fget (CLOCKID_TO_FD (id)); +if (!fp) +return NULL ; + +if (fp->f_op->open == posix_clock_open) + clk = fp->private_data; + +fput (fp);
include/linux/time.h EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep); · line 1041
Quoted 10 lines
include/linux/time.h +10
-0+return clk; +} + +int pc_clock_adjtime (struct posix_clock *clk,struct timex *tx) +{ +int err; + +mutex_lock (&clk->mux); + +if (clk->zombie)
Uuurgh. That explains the zombie thing. That's really horrible and we can be more clever. When we leave the file lookup to the pc_timer_* functions, then we can simply do:
struct posix_clock_descr {
struct file *fp;
struct posix_clock *clk;
};static int get_posix_clock(const clockid_t id, struct posix_clock_descr *cd)
{
struct file *fp = fget(CLOCKID_TO_FD(id));
if (!fp || fp->f_op->open != posix_clock_open || !fp->private_data)
return -ENODEV;
cd->fp = fp;
cd->clk = fp->private_data;
return 0;
}static void put_posix_clock(struct posix_clock_descr *cd)
{
fput(cd->fp);
}int pc_timer_****(....)
{
struct posix_clock_descr cd;
ret = -EOPNOTSUPP;
if (get_posix_clock(id, &cd))
return -ENODEV;
if (cd.clk->ops->whatever)
ret = cd.clk->ops->whatever(....);
put_posix_clock(&cd);
return ret;
}That get's rid of your life time problems, of clk->mutex, clock->zombie and makes the code simpler and more robust. And it avoids the whole mess in posix-timers.c as well.
include/linux/time.h EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
Quoted 9 lines
include/linux/time.h +9
-0+ err = -ENODEV ; + else if (!clk->ops->clock_adjtime) + err = -EOPNOTSUPP ; + else + err = clk->ops->clock_adjtime (clk->priv, tx); + +mutex_unlock (&clk->mux); +return err; +}
Though all this feels still a bit backwards.
The whole point of this exercise is to provide dynamic clock ids and make them available via the standard posix timer interface and aside of that have standard file operations for these clocks to provide special purpose ioctls, mmap and whatever. And to make it a bit more complex these must be removable modules.
I need to read back the previous discussions, but maybe someone can fill me in why we can't make these dynamic things not just live in posix_clocks[MAX_CLOCKS ... MAX_DYNAMIC_CLOCKS] (there is no requirement to have an unlimited number of those) and just get a mapping from the fd to the clock_id? That creates a different set of life time problems, but no real horrible ones.
Thanks,
tglx
Richard Cochran
maintainerRe: [PATCH V7 3/8] posix clocks: introduce dynamic clocks
In reply to Thomas GleixnerQuoted 3 lines · Thomas Gleixner
Quoted 1 line
+void *posix_clock_private(struct file *fp);
Leftover ? There is neither a caller nor an implementation
Yes, you are right. Sorry.
This patch series has been through several contortions, and this current one won't be the last!
As to the other comments, thanks for your careful review. I will adapt the patch set accordingly.
Richard
Richard Cochran
maintainerRe: [PATCH V7 4/8] posix clocks: hook dynamic clocks into system calls
In reply to Thomas GleixnerQuoted 10 lines · Thomas Gleixner
include/linux/posix-timers.h struct cpu_timer_list { · line 60
Quoted 7 lines · Richard Cochran
include/linux/posix-timers.h +7
-0+ +static inline bool clock_is_static (const clockid_t id) +{ +if (0 == (id & ~CLOCKFD_MASK )) +return true; +if (CLOCKFD == (id &CLOCKFD_MASK )) +return false;Please use the usual kernel notation.
Sorry, what do you mean?
Quoted 5 lines
Quoted 1 line
return
CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));I really start to wonder whether we should cleanup that whole
CLOCK_DISPATCHmacro magic and provide real inline functions for each of the clock functions instead.
I'm glad you suggested that.
IMHO, the CLOCK_DISPATCH thing is calling out to be eliminated, but I didn't dare to take that upon myself.
Quoted 2 lines
Please don't tell me now that we could even hack this into
CLOCK_DISPATCH. *shudder*
;^)
Quoted 11 lines
kernel/time/posix-clock.c void posix_clock_destroy(struct posix_clock *clk) · line 247
Quoted 7 lines
kernel/time/posix-clock.c +7
-0+int pc_clock_adjtime (struct posix_clock *clk,struct timex *tx) +{ +int err; + +mutex_lock (&clk->mux); + +if (clk->zombie)Uuurgh. That explains the zombie thing. That's really horrible and we can be more clever. When we leave the file lookup to the pc_timer_* functions, then we can simply do:
...
Quoted 3 lines
That get's rid of your life time problems, of
clk->mutex,clock->zombieand makes the code simpler and more robust. And it avoids the whole mess inposix-timers.cas well.
The code in the patch set is modeled after a USB driver, namely drivers/usb/class/usbtmc.c. Alan Cox had brought up the example of a PTP Hardware Clock appearing as a USB device. So the device might suddenly disappear, and the zombie field is supposed to cover the case where the hardware no longer exists, but the file pointer is still valid.
I'll take a closer look at your suggestion...
Quoted 5 lines
The whole point of this exercise is to provide dynamic clock ids and make them available via the standard posix timer interface and aside of that have standard file operations for these clocks to provide special purpose ioctls, mmap and whatever. And to make it a bit more complex these must be removable modules.
Yes, and hot pluggable, too.
Quoted 6 lines
I need to read back the previous discussions, but maybe someone can fill me in why we can't make these dynamic things not just live in posix_clocks[MAX_CLOCKS ...
MAX_DYNAMIC_CLOCKS] (there is no requirement to have an unlimited number of those) and just get a mapping from the fd to theclock_id? That creates a different set of life time problems, but no real horrible ones.
I would summarize the discussion like this:
Alan Cox was strongly in favor of using a regular file descriptor as the reference to the dynamic clock.
John Stultz thought it wouldn't be too bad to cycle though a number of static ids, being careful not to every assign the same static id (in series) to two different clocks.
I implemented Alan's idea, since it seemed like maintaining the mapping between clocks and static ids would be extra work, but without any real benefit.
Thanks again for your review,
Richard
Re: [PATCH V7 4/8] posix clocks: hook dynamic clocks into system calls
Quoted 11 lines · Richard Cochran
Quoted 8 lines · Thomas Gleixner
include/linux/posix-timers.h struct cpu_timer_list { · line 60
Quoted 6 lines
include/linux/posix-timers.h +6
-0+static inline bool clock_is_static (const clockid_t id) +{ +if (0 == (id & ~CLOCKFD_MASK )) +return true; +if (CLOCKFD == (id &CLOCKFD_MASK )) +return false;Please use the usual kernel notation.
Sorry, what do you mean?
if (!id & ~CLOCKFD_MASK)
if ((id & CLOCKFD_MASK) == CLOCKFD)
Not a real problem. I just always stumble over that coding style.
Quoted 6 lines
The code in the patch set is modeled after a USB driver, namely
drivers/usb/class/usbtmc.c. Alan Cox had brought up the example of a PTP Hardware Clock appearing as a USB device. So the device might suddenly disappear, and the zombie field is supposed to cover the case where the hardware no longer exists, but the file pointer is still valid.
Hmm ok, so you use clk->mutex to prevent the underlying device from going away while you call a function and clk->zombie indicates that it's gone and clk just kept around for an open file descriptor. Now it makes sense, but that wants a big fat comment in the code. :)
Doesn't the same problem exist for the file operations of patch 3? I think you want the very same protection there unless you want to do the same magic in your USB driver as well.
So you could do the following:
static int get_fd_clk(struct posix_clock_descr *cd, struct file *fp)
{
cd->fp = fp;
cd->clk = fp->private_data;
mutex_lock(&cd->clk->mutex);
if (!cd->clk->zombie)
return 0;
mutex_unlock(&cd->clk->mutex);
return -ENODEV;
}static void put_fd_clk(struct posix_clock_descr *cd)
{
mutex_unlock(&cd->clk->mutex);
}static int get_posix_clock(const clockid_t id, struct posix_clock_descr *cd)
{
struct file *fp = fget(CLOCKID_TO_FD(id));
int ret;
if (!fp || fp->f_op->open != posix_clock_open || !fp->private_data)
return -ENODEV;
ret = get_fd_clk(cd, fp);
if (ret)
fput(fp);
return ret;
}static void put_posix_clock(struct posix_clock_descr *cd)
{
put_fd_clk(cd);
fput(cd->fp);
}So your fops would call get_fd_clk() and put_fd_clk() and the pc_timer ones get_posix_clock() and put_posix_clock() or whatever sensible function names you come up with.
Thoughts ?
Quoted 5 lines
I would summarize the discussion like this:
Alan Cox was strongly in favor of using a regular file descriptor as the reference to the dynamic clock.
Quoted 7 lines
John Stultz thought it wouldn't be too bad to cycle though a number of static ids, being careful not to every assign the same static id (in series) to two different clocks.
I implemented Alan's idea, since it seemed like maintaining the mapping between clocks and static ids would be extra work, but without any real benefit.
Yes, he's right. Was too tired to think about the clock ids assingment when devices are removed and plugged. The fd mapping makes that all go away.
Thanks,
tglx
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to Richard CochranQuoted 3 lines · Richard Cochran
This patch adds a new mode bit into the timex structure. When set, the bit instructs the kernel to add the given time value to the current time.
The proposed new control mode, ADJ_SETOFFSET, is logically the same as ADJ_OFFSET with timex.constant == -INFINITY. So it is possible to do the same thing without risking forward compatibility. (I mean by "risking forward compatibility" that the mode bit 0x0040 may be defined differently by the upstream maintainer anytime in the future.)
Richard Cochran
maintainerRe: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
Quoted 10 lines · Kuwahara,T.
Quoted 3 lines · Richard Cochran
This patch adds a new mode bit into the timex structure. When set, the bit instructs the kernel to add the given time value to the current time.
The proposed new control mode,
ADJ_SETOFFSET, is logically the same asADJ_OFFSETwith timex.constant == -INFINITY. So it is possible to do the same thing without risking forward compatibility. (I mean by "risking forward compatibility" that the mode bit0x0040may be defined differently by the upstream maintainer anytime in the future.)
Can you please elaborate?
I don't see any way to use timex.constant with ADJ_OFFSET in order to correct a time offset. The 'time_constant' in kernel/time/ntp.c is restricted to the interval [0..MAXTC], and MAXTC is 10 in timex.h.
Richard
Richard Cochran
maintainerRe: [PATCH V7 4/8] posix clocks: hook dynamic clocks into system calls
In reply to Thomas GleixnerQuoted 4 lines · Thomas Gleixner
Doesn't the same problem exist for the file operations of patch 3? I think you want the very same protection there unless you want to do the same magic in your USB driver as well.
Yes, you are right. I overlooked that.
Quoted 1 line
So you could do the following:
...
Quoted 5 lines
So your fops would call
get_fd_clk()andput_fd_clk()and thepc_timeronesget_posix_clock()andput_posix_clock()or whatever sensible function names you come up with.Thoughts ?
It is a good, concrete suggestion. I will try it that way.
Thanks,
Richard
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to Kuwahara,T.Quoted 7 lines · Kuwahara,T.
Quoted 3 lines · Richard Cochran
This patch adds a new mode bit into the timex structure. When set, the bit instructs the kernel to add the given time value to the current time.
The proposed new control mode,
ADJ_SETOFFSET, is logically the same asADJ_OFFSETwith timex.constant == -INFINITY.
I'm not sure if this is correct. Its more like settimeofday, only giving a relative offset to jump the clock, rather then an absolute time. It does not slew the clock over time like ADJ_OFFSET does.
Quoted 4 lines
So it is possible to do
the same thing without risking forward compatibility. (I mean by "risking forward compatibility" that the mode bit0x0040may be defined differently by the upstream maintainer anytime in the future.)
adjtimex is a linux specific interface, which is compatible but not identical to the ntp specified interfaces. The ntp client code already has Linux specific modifications, so I don't think we have to worry about 0x40 specifically being reserved by the NTP client.
thanks
-john
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to Richard CochranQuoted 1 line · Richard Cochran
Can you please elaborate?
The timex.constant is defined as equal to the binary logarithm of the reciprocal of
the natural frequency minus SHIFT_PLL. In other words, the following equation holds:
log2(natural frequency) + time_constant + SHIFT_PLL = 0,
which means that decreasing time_constant increases natural frequency exponentially.
And since a larger natural frequency gives a smaller settling time, a sufficiently
large negative time_constant results in immediate time step, at least in theory.
Quoted 2 lines
I don't see any way to use timex.constant with
ADJ_OFFSETin order to correct a time offset.
How about this?
if (txc->modes & ADJ_OFFSET) {
if (txc->constant == INT32_MIN) {
/* step time */
} else {
/* slew time */
}
}Quoted 2 lines
The '
time_constant' inkernel/time/ntp.cis
restricted to the interval [0..MAXTC], and MAXTC is 10 intimex.h.
Then let's just ignore the restriction. (It's possible by setting the timex.constant
without setting the ADJ_TIMECONST flag.)
That said, I'm somehow against the idea of using the adjtimex syscall for that purpose.
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to john stultzQuoted 4 lines · john stultz
adjtimex is a linux specific interface, which is compatible but not identical to the ntp specified interfaces. The ntp client code already has Linux specific modifications, so I don't think we have to worry about
0x40specifically being reserved by the NTP client.
But struct timex is not linux-specific...
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
Quoted 7 lines · Kuwahara,T.
Quoted 4 lines · john stultz
adjtimex is a linux specific interface, which is compatible but not identical to the ntp specified interfaces. The ntp client code already has Linux specific modifications, so I don't think we have to worry about
0x40specifically being reserved by the NTP client.But struct timex is not linux-specific...
It is if you're compiling against linux's timex.h file.
We already have a number of differences compared with BSD's timex mode definitions:
We have ADJ_TICK: 0x4000, which is MOD_CLKB in FreeBSD.
We also have ADJ_OFFSET_SINGLESHOT and ADJ_OFFSET_SS_READ which allow adjtimex act like the original ntp_adjtime.
The key bit is that we map the shared MOD_* definitions that the NTP client uses to the linux specific ADJ_* values in the linux timex.h
However, your concern does bring up a good point: 0x40 is MOD_PPSMAX in BSD, and we should at-least check to make sure that the PPS code that is currently floating around on the lists and is in akpm's tree hasn't already reserved that bit.
Rodolfo, Alexander: Any comments here?
thanks
-john
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to Kuwahara,T.Quoted 9 lines · Kuwahara,T.
How about this?
if (txc->modes & ADJ_OFFSET) { if (txc->constant == INT32_MIN) { /* step time */ } else { /* slew time */ } }
This looks like magic behavior. Sort of a "knock twice and then say the password" interface. I don't see why that would be better then adding a clear new mode flag?
Quoted 2 lines
That said, I'm somehow against the idea of using the adjtimex syscall for that purpose.
Could you expand on why you don't like this?
thanks
-john
Richard Cochran
maintainerRe: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to john stultzQuoted 4 lines · john stultz
However, your concern does bring up a good point:
0x40isMOD_PPSMAXin BSD, and we should at-least check to make sure that the PPS code that is currently floating around on the lists and is in akpm's tree hasn't already reserved that bit.
If the concern is only about that bit, then we can choose another one from the range 0x0f00 instead.
It seems to me that the ability to jump the clock is a very useful feature, at least when implementing a clock servo for PTP in user space, if not in other cases, too. I wonder why that option is missing from the interface in the first place.
I am not aware of any serious attempt to get any kind of standardized PTP clock support into any other OS. As far as compatibility with NTP is concerned, why can't we let Linux set an example and let NTP/BSD/*nix follow us?
Richard
Richard Cochran
maintainerRe: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to john stultzQuoted 12 lines · john stultz
Quoted 9 lines · Kuwahara,T.
How about this?
if (txc->modes & ADJ_OFFSET) { if (txc->constant == INT32_MIN) { /* step time */ } else { /* slew time */ } }
Quoted 3 lines
This looks like magic behavior. Sort of a "knock twice and then say the password" interface. I don't see why that would be better then adding a clear new mode flag?
I have to agree with John on this one. Looks very hacky to me.
Richard
Richard Cochran
maintainerRe: [PATCH V7 4/8] posix clocks: hook dynamic clocks into system calls
In reply to Thomas GleixnerQuoted 1 line · Thomas Gleixner
So you could do the following:
...
Quoted 12 lines
static int
get_posix_clock(const clockid_t id, struct posix_clock_descr *cd){ struct file *fp = fget(CLOCKID_TO_FD(id)); int ret; if (!fp || fp->f_op->open != posix_clock_open || !fp->private_data) return -ENODEV; ret = get_fd_clk(cd, fp); if (ret) fput(fp); return ret; }
In order to avoid leaking a fp reference, shouldn't this go like:
static int get_posix_clock(const clockid_t id, struct posix_clock_desc *cd)
{
struct file *fp = fget(CLOCKID_TO_FD(id));
int ret = -EINVAL;
if (!fp)
return ret;
if (fp->f_op->open != posix_clock_open || !fp->private_data)
goto out;
ret = get_fd_clk(cd, fp);
out:
if (ret)
fput(fp);
return ret;
}Thanks again for your help,
Richard
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to john stultzВ Tue, 21 Dec 2010 13:59:44 -0800
john stultz <[email protected]> пишет:
Quoted 20 lines · Kuwahara,T.
Quoted 7 lines · john stultz
Quoted 4 lines
adjtimex is a linux specific interface, which is compatible but not identical to the ntp specified interfaces. The ntp client code already has Linux specific modifications, so I don't think we have to worry about
0x40specifically being reserved by the NTP client.But struct timex is not linux-specific...
It is if you're compiling against linux's
timex.hfile.We already have a number of differences compared with BSD's timex mode definitions:
We haveADJ_TICK:0x4000, which isMOD_CLKBin FreeBSD.
We also haveADJ_OFFSET_SINGLESHOTandADJ_OFFSET_SS_READwhich allow adjtimex act like the originalntp_adjtime.The key bit is that we map the shared MOD_* definitions that the NTP client uses to the linux specific ADJ_* values in the linux
timex.h
Quoted 6 lines
However, your concern does bring up a good point:
0x40isMOD_PPSMAXin BSD, and we should at-least check to make sure that the PPS code that is currently floating around on the lists and is in akpm's tree hasn't already reserved that bit.Rodolfo, Alexander: Any comments here?
No, it is used neither in my patches nor in the original code. The only change my patches do to timex.h is adding hardpps().
Signature · 1 line
Alexander
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to john stultzQuoted 2 lines · john stultz
I don't see why that would be better then adding a clear new mode flag?
In short, time step is a special case of time slew. Those are the same, only different in one parameter, as is shown in my previous post. That's why I said there's no need for adding a new mode.
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
Quoted 7 lines · Kuwahara,T.
Quoted 2 lines · john stultz
I don't see why that would be better then adding a clear new mode flag?
In short, time step is a special case of time slew. Those are the same, only different in one parameter, as is shown in my previous post. That's why I said there's no need for adding a new mode.
Sure, I get that a instantaneous offset adjustment could be represented as a instantaneous slew of the same amount. But what is the benefit of doing this? The ADJ_OFFSET isn't really a continuous function like you describe. For instance, you can't slew time backwards. The amount you can slow or speed up the clock is limited, so time will always increase. So to have a magic value way outside the bound of normal operation that does something special seems a bit obfuscated.
ADJ_SETOFFSET isn't slewing the clock. So I think its much clearer to add a new mode, rather then to try to wedge the functionality into a corner case of ADJ_OFFSET slewing just because one could mathematically represent it the same way.
I see the cost of adding it as a special case as adding extra complexity to an already complex subsystem. Doing things that make the code easier to understand and read makes it more maintainable. And I don't see the gain (other then saving a bit in the bit flag) to offset the complexity of trying to squeeze this functionality into the ADJ_OFFSET mode.
thanks
-john
Richard Cochran
maintainerRe: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
In reply to Kuwahara,T.Quoted 7 lines · Kuwahara,T.
Quoted 2 lines · john stultz
I don't see why that would be better then adding a clear new mode flag?
In short, time step is a special case of time slew. Those are the same, only different in one parameter, as is shown in my previous post. That's why I said there's no need for adding a new mode.
Well, in addition to the objections raised by John, your suggested implementation is also shortsighted. The field timex.constant is copied into time_constant in some code paths. Obviously, this would be a bad thing when timex.constant==-huge.
So, you need to clarify the interaction between ADJ_OFFSET, ADJ_TIMECONST, ADJ_TAI, timex.constant, time_constant, and MAXTC.
If you would fully implement your idea, I expect it would become obvious that it a bit of a hack, both in the kernel code and in the user space interface. But, if you disagree, please just post a patch with the complete implementation...
Thanks,
Richard
Re: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
After all, I'd prefer your earlier patchset. Leaving aside the compatibility issue, there's no particular reason we have to re-use the struct timex, which requires otherwise unnecessary conditional branches as well as unit conversions. Don't you agree?
Richard Cochran
maintainerRe: [PATCH V7 1/8] ntp: add ADJ_SETOFFSET mode bit
Quoted 4 lines · Kuwahara,T.
After all, I'd prefer your earlier patchset. Leaving aside the compatibility issue, there's no particular reason we have to re-use the struct timex, which requires otherwise unnecessary conditional branches as well as unit conversions. Don't you agree?
Well, from my point of view of wanting to allow a user space clock servo to be able to adjust a hardware clock, it would be sufficient to offer a way to jump the clock and to adjust the frequency via one or perhaps two new system calls.
That is indeed what I first suggested, and I still think it would be a clean and simple interface. The NTP call is quite gross, since it multiplexes a whole bunch of different functions through the timex structure.
However, several reviewers on the lkml prefered to keep with the NTP interface. It offers the needed functionality and is already well established, despite its ugliness.
To me, the proposed ADJ_SETOFFSET is a simple and logical extenstion of the NTP interface. Also, the implementation is straightforward. In contrast, using a special -INF value seems a bit obtuse to me.
Richard
Re: [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x.
In reply to Richard CochranHi!!
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h line 31
Quoted 5 lines
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h new file +5
-0+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 */
CouldWeGetRidOfCamelCase?
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Richard Cochran
maintainerRe: [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x.
Quoted 9 lines · Pavel Machek
Hi!!
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h line 31
Quoted 5 lines
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h new file +5
-0+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 */ CouldWeGetRidOfCamelCase?
I agree that CamelCase is ugly and in bad taste. However, I make an exception when the register level programmer's manual uses this style.
IMHO, it is better to use the exact same mnemonics as in the manual. That way, when the next person comes along to make a change to the code, with manual in hand, he will immediately know what is what.
Thanks,
Richard
Richard Cochran
maintainerRe: [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x.
In reply to Pavel MachekBTW, I posted an updated V8 of this patch series on December 31.
Richard
Re: [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x.
In reply to Richard CochranQuoted 11 lines · Richard Cochran
Quoted 9 lines · Pavel Machek
Hi!!
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h line 31
Quoted 5 lines
arch/arm/mach-ixp4xx/include/mach/ixp46x_ts.h new file +5
-0+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 */ CouldWeGetRidOfCamelCase?
Quoted 5 lines
I agree that CamelCase is ugly and in bad taste. However, I make an exception when the register level programmer's manual uses this style.
IMHO, it is better to use the exact same mnemonics as in the manual. That way, when the next person comes along to make a change to
Given the comments -- does manual really use camelCase crap? And... the identifiers actually combine camelCase with _. Better fix it.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Richard Cochran
maintainerRe: [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x.
Quoted 3 lines · Pavel Machek
Given the comments -- does manual really use camelCase crap? And... the identifiers actually combine camelCase with _. Better fix it.
Thats the way Intel likes it:
http://download.intel.com/design/network/manuals/30626204.pdf
Page 837
Re: [PATCH V7 7/8] ptp: Added a clock driver for the IXP46x.
Hi!
Quoted 7 lines
Quoted 3 lines
Given the comments -- does manual really use camelCase crap? And... the identifiers actually combine camelCase with _. Better fix it.
Thats the way Intel likes it:
http://download.intel.com/design/network/manuals/30626204.pdf
Ok... I'd say that it is so ugly that it should be replaced, but it is not huge deal.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html