pps: time synchronization over LPT
cover pps: time synchronization over LPT
This patchset adds a complete solution (well, the kernel part) for fast high-precision time synchronization between computers using their LPT ports. My tests show that it is able to achieve synchronization in the order of 1-2 microseconds and keep it for as long as needed. We are using this facility in my company for distributed real-time simulation.
The idea is to have a single master computer whose local clock is used as a standard. It then provides time information through both PPS high-precision signal over LPT and NTP over network. Then any other computer can combine these two time sources (this is what ntpd can do). Client-side ntpd constantly checks that the local time divergence from the master's time is less then 128ms. If this is true it uses PPS signal to further decrease the difference. ntpd can try to do itself but it's too slow and imprecise. This is really a job for the kernel.
All stuff related to the PPS signals in Linux kernel is handled by the code from LinuxPPS project. It can be controlled from user-space through PPSAPI (RFC 2783) and ntpd can use it to pass control over synchronization to the kernel. Unfortunately this is an optional feature of PPSAPI and it was not implemented. Also Linux code from NTPv4 reference model was missing all the pieces that deal with PPS. This patchset closes both gaps to achieve the highest precision and speed. It:
* adds missing hardpps() function to the NTPv4 Linux kernel code
* adds getnstime_raw_and_real() to atomically get both MONOTONIC_RAW and REALTIME clock timestamps for hardpps()
* adds missing optional PPSAPI method to bind the particular PPS source to feed information to the time subsystem through hardpps()
Also the actual parallel port PPS generator and client were added. (I know that not yet mainlined LinuxPPS patches provide an own parallel port client but it is actually a hack on lp driver which I didn't like at all.)
This patchset is tested against the vanilla 2.6.32.7 kernel. But we are actually using it on 2.6.31.12-rt20 rt-preempt kernel most of the time. Also there is a version which should be applied on top of LinuxPPS out of tree patches (i.e. all clients and low-level irq timestamps stuff). Those who are interested in other versions of the patchset can find them in my git repository:
http://lvk.cs.msu.su/~lasaine/timesync/linux-2.6-timesync.git
Alexander Gordeev (5):
ntp: add hardpps implementation
pps: capture MONOTONIC_RAW timestamps as well
pps: add kernel consumer support
pps: add parallel port PPS signal generator
pps: add parallel port PPS client
Documentation/ioctl/ioctl-number.txt | 2 +- drivers/pps/Kconfig | 5 + drivers/pps/Makefile | 1 + drivers/pps/clients/Kconfig | 16 ++ drivers/pps/clients/Makefile | 9 + drivers/pps/clients/pps_parport.c | 211 ++++++++++++++++++ drivers/pps/generators/Kconfig | 17 ++ drivers/pps/generators/Makefile | 9 + drivers/pps/generators/pps_gen_parport.c | 263 +++++++++++++++++++++++ drivers/pps/kapi.c | 45 ++++- drivers/pps/pps.c | 67 ++++++- include/linux/pps.h | 7 + include/linux/pps_kernel.h | 39 ++++- include/linux/time.h | 2 + include/linux/timex.h | 1 + kernel/time/Kconfig | 7 + kernel/time/ntp.c | 346 +++++++++++++++++++++++++++++- kernel/time/timekeeping.c | 34 +++ 18 files changed, 1065 insertions(+), 16 deletions(-) create mode 100644 drivers/pps/clients/Kconfig create mode 100644 drivers/pps/clients/Makefile create mode 100644 drivers/pps/clients/pps_parport.c create mode 100644 drivers/pps/generators/Kconfig create mode 100644 drivers/pps/generators/Makefile create mode 100644 drivers/pps/generators/pps_gen_parport.c
PATCH 1/5 ntp: add hardpps implementation
This commit adds hardpps() implementation based upon the original one from the NTPv4 reference kernel code. However, it is highly optimized towards very fast syncronization and maximum stickness to PPS signal. The typical error is less then a microsecond.
To make it sync faster I had to throw away exponential phase filter so that the full phase offset is corrected immediately. Then I also had to throw away median phase filter because it gives a bigger error itself if used without exponential filter.
Maybe we will find an appropriate filtering scheme in the future but it's not necessary if the signal quality is ok.
--- drivers/pps/Kconfig | 1 + include/linux/timex.h | 1 + kernel/time/Kconfig | 7 + kernel/time/ntp.c | 346 ++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 349 insertions(+), 6 deletions(-)
drivers/pps/Kconfig +1 -0
@@ -7,6 +7,7 @@ menu "PPS support" config PPS tristate "PPS support" depends on EXPERIMENTAL + select NTP_PPS ---help--- PPS (Pulse Per Second) is a special pulse provided by some GPS antennae. Userland can use it to get a high-precision time
include/linux/timex.h +1 -0
@@ -274,6 +274,7 @@ extern u64 tick_length;extern void second_overflow (void );extern void update_ntp_one_tick (void );extern int do_adjtimex (struct timex *); +extern void hardpps (const struct timespec *,s64 );/* Don't use! Compatibility define for existing users. */ #define tickadj (500/HZ ? : 1)
kernel/time/Kconfig +7 -0
@@ -27,3 +27,10 @@ config GENERIC_CLOCKEVENTS_BUILD default y depends on GENERIC_CLOCKEVENTS || GENERIC_CLOCKEVENTS_MIGR +config NTP_PPS + bool "PPS kernel consumer support" + depends on PPS + help + This option adds support for direct in-kernel time + syncronization using an external PPS signal. +
kernel/time/ntp.c +340 -6
412 lines not on this page · raw message
Signature · 1 line
1.6.5
Re: [PATCH 1/5] ntp: add hardpps implementation
Quoted 11 lines · Alexander Gordeev
This commit adds
hardpps()implementation based upon the original one from the NTPv4 reference kernel code. However, it is highly optimized towards very fast syncronization and maximum stickness to PPS signal. The typical error is less then a microsecond.
To make it sync faster I had to throw away exponential phase filter so that the full phase offset is corrected immediately. Then I also had to throw away median phase filter because it gives a bigger error itself if used without exponential filter.
Maybe we will find an appropriate filtering scheme in the future but it's not necessary if the signal quality is ok.
Quoted 1 line
Signed-off-by Alexander Gordeev <[email protected]>
Very cool! Bunch of style comments below.
One other thing: From the comments in the code, it looks like this may have come straight from the reference implementation. You might want to provide some extra documentation or comment providing proper credit, and clarifying that the code is in fact GPLv2 compatible.
Also please review Section 12 of Documentation/SubmittingPatches
thanks
-john
[snip]
Quoted 8 lines
kernel/time/ntp.c +8
-0+long pps_tf[3 ];/* phase median filter */ +s64 pps_freq;/* scaled frequency offset (ns/s) */ +s64 pps_fcount;/* frequency accumulator */ +long pps_jitter;/* nominal jitter (ns) */ +long pps_stabil;/* nominal stability (scaled ns/s) */ +int pps_valid;/* signal watchdog counter */ +int pps_shift;/* nominal interval duration (s) (shift) */ +int pps_shiftmax;/* max interval duration (s) (shift) */
kernel/time/ntp.c long time_adjust; · line 109
Quoted 10 lines
kernel/time/ntp.c +10
-0+int pps_intcnt;/* interval counter */ + +/* +* PPS signal quality monitors +*/ +long pps_calcnt;/* calibration intervals */ +long pps_jitcnt;/* jitter limit exceeded */ +long pps_stbcnt;/* stability limit exceeded */ +long pps_errcnt;/* calibration errors */ +
Shouldn't all of the above values be made static?
[snip]
Quoted 20 lines
/*
kernel/time/ntp.c +3
-5@@ -233,8 +277,6 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)*/ void second_overflow (void ) { -s64 delta; -/* Bump the maxerror field */ time_maxerror +=MAXFREQ /NSEC_PER_USEC ;if (time_maxerror >NTP_PHASE_LIMIT ) {@@ -248,9 +290,27 @@ void second_overflow(void)*/ tick_length = tick_length_base; - delta =shift_right (time_offset,SHIFT_PLL + time_constant); - time_offset -= delta; - tick_length += delta; +#ifdef CONFIG_NTP_PPS +if (time_status &STA_PPSTIME && time_status &STA_PPSSIGNAL ) { + tick_length += time_offset;
kernel/time/ntp.c void second_overflow(void) · line 305
Quoted 10 lines
kernel/time/ntp.c +10
-0+ time_offset =0 ; + } else +#endif /* CONFIG_NTP_PPS */ + { +s64 delta; + delta = +shift_right (time_offset,SHIFT_PLL + time_constant); + time_offset -= delta; + tick_length += delta; + }
Ugh. Surely there's a better way to do this then using the ifdefs in code?
Maybe something like:
#ifdef CONFIG_NTP_PPS
/* Comment about how pps consumes the whole offset */ static inline s64 ntp_offset_chunk(s64 offset)
{
if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
return offset
else
return shift_right(offset, SHIFT_PLL + time_constant);
}
#else
static inline s64 ntp_offset_chunk(s64 offset)
{
return shift_right(offset, SHIFT_PLL + time_constant);
}#endif
Then in second_overflow():
delta = ntp_offset_chunk(time_offset);time_offset -= delta;tick_length += delta;
Feel free to use a better name then ntp_offset_chunk(), but this keeps the logic from being obfuscated by all the #ifdefs
kernel/time/ntp.c void second_overflow(void) · line 313
Quoted 7 lines
kernel/time/ntp.c +7
-0+#ifdef CONFIG_NTP_PPS +if (pps_valid >0 ) + pps_valid--; +else + time_status &= ~(STA_PPSSIGNAL |STA_PPSJITTER | +STA_PPSWANDER |STA_PPSERROR ); +#endif /* CONFIG_NTP_PPS */
Similarly, can some sort of pps_dec_valid() inline function be used here?
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 650
Quoted 13 lines
kernel/time/ntp.c +13
-0+#ifdef CONFIG_NTP_PPS + +/* normalize the timestamp so that nsec is in the +( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval */ +static inline struct timespec pps_normalize_ts (struct timespec ts) +{ +if (ts.tv_nsec > (NSEC_PER_SEC >>1 )) { + ts.tv_nsec -=NSEC_PER_SEC ; + ts.tv_sec++; + } + +return ts; +}
Hrmm.. So by converting a timespec into a second + [-NSEC_PER_SEC/2, NSEC_PER_SEC/2] value, you aren't really using a timespec anymore, right? I mean, the timepsec_valid() would fail in many cases, for instance.
I realize its pretty close, and you *can* use a timespec this way, but to me it seems reasonable to introduce a new structure (pps_normtime?) so that its clear you shouldn't exchange timespec values and pps_normtime values directly?
This is sort of a nit, and maybe others disagree, so not the most critical issue. But it seems like you're abusing the structure a bit.
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 674
Quoted 5 lines
kernel/time/ntp.c +5
-0+/* decrease frequency calibration interval length */ +static inline void pps_dec_freq_interval (void ) +{ +if (--pps_intcnt <=-4 ) { + pps_intcnt =-4 ;
Is -4 a magic value?
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 742
Quoted 3 lines
kernel/time/ntp.c +3
-0+ }else {/* good sample */ +if (++pps_intcnt >=4 ) { + pps_intcnt =4 ;
Again, the magic 4?
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 755
Quoted 3 lines
kernel/time/ntp.c +3
-0+ pps_stabil += (div_s64 (((s64 )delta_mod) << + (NTP_SCALE_SHIFT -SHIFT_USEC ), +NSEC_PER_USEC ) - pps_stabil) >>PPS_FAVG ;
Hmm. Two 64bit divides in this path? This code would run each second, right? It would probably be good to see if this could be optimized a little bit more, but I guess its similar to ntp_update_frequency() which is called on adjtimex() calls (every 16 seconds at most with networked ntp).
Quoted 7 lines
kernel/time/ntp.c +7
-0+void hardpps (const struct timespec *p_ts,s64 nsec) +{ +struct timespec ts_norm, freq_norm; + + ts_norm =pps_normalize_ts (*p_ts); + freq_norm =pps_normalize_ts (ns_to_timespec (nsec)); +
[snip]
Review
Quoted 2 lines
(quoted diff) +2
-0+ + write_seqlock_irq(&xtime_lock);
This is called possibly in irq context, right? So you probably want to use write_seqlock_irqsave(), no?
thanks
-john
Re: [PATCH 1/5] ntp: add hardpps implementation
Thanks for the review!
В Wed, 03 Feb 2010 14:08:50 -0800
john stultz <[email protected]> пишет:
Quoted 14 lines · Alexander Gordeev
Quoted 11 lines
This commit adds
hardpps()implementation based upon the original one from the NTPv4 reference kernel code. However, it is highly optimized towards very fast syncronization and maximum stickness to PPS signal. The typical error is less then a microsecond. To make it sync faster I had to throw away exponential phase filter so that the full phase offset is corrected immediately. Then I also had to throw away median phase filter because it gives a bigger error itself if used without exponential filter.
Maybe we will find an appropriate filtering scheme in the future but it's not necessary if the signal quality is ok.Quoted 1 line
Signed-off-by Alexander Gordeev <[email protected]>
Quoted 8 lines
Very cool! Bunch of style comments below.
One other thing: From the comments in the code, it looks like this may have come straight from the reference implementation. You might want to provide some extra documentation or comment providing proper credit, and clarifying that the code is in fact GPLv2 compatible.
Also please review Section 12 of
Documentation/SubmittingPatches
Indeed, this code is a direct descendant of the reference implementation. However it is mostly rewritten because the original code was too hard to read. But I thought it is not an issue because the whole ntp.c seems to have a lot of code from it... Well, I'm not quite good in license compatibility. :)
Here is the copyright notice from the original code for reference:
/***********************************************************************
* * * Copyright (c) David L. Mills 1993-2001 * * * * Permission to use, copy, modify, and distribute this software and * * its documentation for any purpose and without fee is hereby * * granted, provided that the above copyright notice appears in all * * copies and that both the copyright notice and this permission * * notice appear in supporting documentation, and that the name * * University of Delaware not be used in advertising or publicity * * pertaining to distribution of the software without specific, * * written prior permission. The University of Delaware makes no * * representations about the suitability this software for any * * purpose. It is provided "as is" without express or implied * * warranty. * * *
**********************************************************************/
Maybe adding a comment like the one above second_overflow() is enough?
Quoted 13 lines
[snip]
Quoted 12 lines
(quoted diff) +2
-0+long pps_tf[3]; /* phase median filter */ +s64 pps_freq; /* scaled frequency offset(ns/s) */ +s64
pps_fcount; /* frequency
accumulator */ +longpps_jitter; /* nominal jitter
(ns) */ +longpps_stabil; /* nominal stability
(scaled ns/s) */ +intpps_valid; /* signal
watchdog counter */ +intpps_shift; /*
nominal interval duration (s) (shift) */ +intpps_shiftmax; /* max interval duration (s) (shift)
*/ +intpps_intcnt; /* interval counter */ +
+/*
+ * PPS signal quality monitors
Quoted 8 lines
kernel/time/ntp.c long time_adjust; · line 109
Quoted 6 lines
+ */
kernel/time/ntp.c +5
-0+long pps_calcnt;/* calibration intervals */ +long pps_jitcnt;/* jitter limit exceeded */ +long pps_stbcnt;/* stability limit exceeded */ +long pps_errcnt;/* calibration errors */ +Shouldn't all of the above values be made static?
Sure, thanks.
Quoted 67 lines
[snip]
Quoted 23 lines
/*
(quoted diff) +0
-0@@ -233,8 +277,6 @@ static enum hrtimer_restart
ntp_leap_second(struct hrtimer *timer)*/
voidsecond_overflow(void)
{kernel/time/ntp.c +5
-5-s64 delta; -/* Bump the maxerror field */ time_maxerror +=MAXFREQ /NSEC_PER_USEC ;if (time_maxerror >NTP_PHASE_LIMIT ) {@@ -248,9 +290,27 @@ void second_overflow(void)*/ tick_length = tick_length_base; - delta = shift_right(time_offset, SHIFT_PLL + time_constant); - time_offset -= delta; - tick_length += delta; +#ifdef CONFIG_NTP_PPS +if (time_status &STA_PPSTIME && time_status &STA_PPSSIGNAL ) { + tick_length += time_offset; + time_offset =0 ;kernel/time/ntp.c void second_overflow(void) · line 308
Quoted 10 lines
kernel/time/ntp.c +9
-0+ } else +#endif /* CONFIG_NTP_PPS */ + { +s64 delta; + delta = +shift_right (time_offset,SHIFT_PLL + time_constant); + time_offset -= delta; + tick_length += delta; + }Ugh. Surely there's a better way to do this then using the ifdefs in code?
Maybe something like:
#ifdef
CONFIG_NTP_PPS
/* Comment about how pps consumes the whole offset */ static inline s64ntp_offset_chunk(s64 offset){ if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL) return offset else return shift_right(offset, SHIFT_PLL + time_constant); } #else static inline s64 ntp_offset_chunk(s64 offset) { return shift_right(offset, SHIFT_PLL + time_constant); }#endif
Then in
second_overflow():delta =
ntp_offset_chunk(time_offset);time_offset-= delta;tick_length+= delta;Feel free to use a better name then
ntp_offset_chunk(), but this keeps the logic from being obfuscated by all the #ifdefs
Quoted 10 lines
kernel/time/ntp.c void second_overflow(void) · line 313
Quoted 7 lines
kernel/time/ntp.c +7
-0+#ifdef CONFIG_NTP_PPS +if (pps_valid >0 ) + pps_valid--; +else + time_status &= ~(STA_PPSSIGNAL |STA_PPSJITTER | +STA_PPSWANDER |STA_PPSERROR ); +#endif /* CONFIG_NTP_PPS */ Similarly, can some sort of
pps_dec_valid()inline function be used here?
Ok, it is better indeed.
Quoted 21 lines
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 650
Quoted 13 lines
kernel/time/ntp.c +13
-0+#ifdef CONFIG_NTP_PPS + +/* normalize the timestamp so that nsec is in the +( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval */ +static inline struct timespec pps_normalize_ts (struct timespec ts) +{ +if (ts.tv_nsec > (NSEC_PER_SEC >>1 )) { + ts.tv_nsec -=NSEC_PER_SEC ; + ts.tv_sec++; + } + +return ts; +}Hrmm.. So by converting a timespec into a second + [-NSEC_PER_SEC/2, NSEC_PER_SEC/2] value, you aren't really using a timespec anymore, right? I mean, the
timepsec_valid()would fail in many cases, for instance.
Quoted 7 lines
I realize its pretty close, and you *can* use a timespec this way, but to me it seems reasonable to introduce a new structure (
pps_normtime?) so that its clear you shouldn't exchange timespec values andpps_normtimevalues directly?This is sort of a nit, and maybe others disagree, so not the most critical issue. But it seems like you're abusing the structure a bit.
Well, I don't mind adding a structure for this. Seems like timespec is not expected to be used this way.
Quoted 9 lines
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 674
Quoted 5 lines
kernel/time/ntp.c +5
-0+/* decrease frequency calibration interval length */ +static inline void pps_dec_freq_interval (void ) +{ +if (--pps_intcnt <=-4 ) { + pps_intcnt =-4 ;Is -4 a magic value?
Quoted 6 lines
[snip]
kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 742
Quoted 3 lines
kernel/time/ntp.c +3
-0+ }else {/* good sample */ +if (++pps_intcnt >=4 ) { + pps_intcnt =4 ;Again, the magic 4?
Yep :)
The values are from the reference implementation. Frequency calculation is mostly the same as in the original code because it works good. I'll add a define for these values.
Quoted 11 lines
[snip]
Quoted 4 lines
kernel/time/ntp.c +3
-0+ pps_stabil += (div_s64 (((s64 )delta_mod) << + (NTP_SCALE_SHIFT -SHIFT_USEC ), + NSEC_PER_USEC) - pps_stabil) >>
PPS_FAVG;Hmm. Two 64bit divides in this path? This code would run each second, right? It would probably be good to see if this could be optimized a little bit more, but I guess its similar to
ntp_update_frequency()which is called onadjtimex()calls (every 16 seconds at most with networked ntp).
Well, this is not quite right. The interval is at least 4 seconds (1 << PPS_FAVG) and increases if the signal is good. Maximum interval length is 256 seconds (1 << PPS_FAVGDEF).
I don't see how it can be optimized right now.
Quoted 8 lines
Quoted 7 lines
kernel/time/ntp.c +7
-0+void hardpps (const struct timespec *p_ts,s64 nsec) +{ +struct timespec ts_norm, freq_norm; + + ts_norm =pps_normalize_ts (*p_ts); + freq_norm =pps_normalize_ts (ns_to_timespec (nsec)); +[snip]
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + write_seqlock_irq(&xtime_lock);This is called possibly in irq context, right? So you probably want to use
write_seqlock_irqsave(), no?
Right, thanks, it's bug. However, I think of moving it to a worqueue.
Signature · 1 line
Alexander
PATCH 2/5 pps: capture MONOTONIC_RAW timestamps as well
In reply to Alexander GordeevMONOTONIC_RAW clock timestamps are ideally suited for frequency calculation and also fit well into the original NTP hardpps design. Now phase and frequency can be adjusted separately: the former based on REALTIME clock and the latter based on MONOTONIC_RAW clock. A new function getnstime_raw_and_real is added to timekeeping subsystem to capture both timestamps at the same time and atomically.
--- drivers/pps/kapi.c | 17 +++++++++++------ include/linux/pps_kernel.h | 33 +++++++++++++++++++++++++++++++-- include/linux/time.h | 2 ++ kernel/time/timekeeping.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 8 deletions(-)
drivers/pps/kapi.c +11 -6
@@ -267,7 +267,7 @@ EXPORT_SYMBOL(pps_unregister_source);* pps->info.echo(source, event, data); */ -void pps_event (int source,struct pps_ktime *ts,int event,void *data) +void pps_event (int source,struct pps_event_time *ts,int event,void *data) {struct pps_device *pps;unsigned long flags;
@@ -284,7 +284,8 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)return ;pr_debug ("PPS event on source %d at %llu.%06u\n" , - pps->id, (unsigned long long ) ts->sec, ts->nsec); + pps->id, (unsigned long long ) ts->ts_real.sec, + ts->ts_real.nsec);spin_lock_irqsave (&pps->lock, flags);
@@ -298,10 +299,12 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data) (pps->params.mode & PPS_CAPTUREASSERT)) {/* We have to add an offset? */ if (pps->params.mode &PPS_OFFSETASSERT ) -pps_add_offset (ts, &pps->params.assert_off_tu); +pps_add_offset (&ts->ts_real, + &pps->params.assert_off_tu);/* Save the time stamp */ - pps->assert_tu = *ts; + pps->assert_tu = ts->ts_real; + pps->assert_tu_raw = ts->ts_raw; pps->assert_sequence++;pr_debug ("capture assert seq #%u for source %d\n" , pps->assert_sequence, source);
@@ -312,10 +315,12 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data) (pps->params.mode & PPS_CAPTURECLEAR)) {/* We have to add an offset? */ if (pps->params.mode &PPS_OFFSETCLEAR ) -pps_add_offset (ts, &pps->params.clear_off_tu); +pps_add_offset (&ts->ts_real, + &pps->params.clear_off_tu);/* Save the time stamp */ - pps->clear_tu = *ts; + pps->clear_tu = ts->ts_real; + pps->clear_tu_raw = ts->ts_raw; pps->clear_sequence++;pr_debug ("capture clear seq #%u for source %d\n" , pps->clear_sequence, source);
include/linux/pps_kernel.h +31 -2
@@ -18,6 +18,10 @@* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#ifndef LINUX_PPS_KERNEL_H +#define LINUX_PPS_KERNEL_H + +#include <linux/pps.h> #include <linux/cdev.h>
@@ -40,6 +44,11 @@ struct pps_source_info {struct device *dev; }; +struct pps_event_time { +struct timespec ts_raw; +struct pps_ktime ts_real; +}; +/* The main struct */ struct pps_device {struct pps_source_info info;/* PSS source info */
@@ -48,6 +57,8 @@ struct pps_device {__u32 assert_sequence;/* PPS' assert event seq # */ __u32 clear_sequence;/* PPS' clear event seq # */ +struct timespec assert_tu_raw; +struct timespec clear_tu_raw;struct pps_ktime assert_tu;struct pps_ktime clear_tu;int current_mode;/* PPS mode at event time */
@@ -71,7 +82,6 @@ struct pps_device {extern spinlock_t pps_idr_lock;extern struct idr pps_idr; -extern struct timespec pps_irq_ts[];extern struct device_attribute pps_attrs[];
@@ -86,4 +96,23 @@ extern int pps_register_source(struct pps_source_info *info,extern void pps_unregister_source (int source);extern int pps_register_cdev (struct pps_device *pps);extern void pps_unregister_cdev (struct pps_device *pps); -extern void pps_event (int source,struct pps_ktime *ts,int event,void *data); +extern void pps_event (int source,struct pps_event_time *ts,int event, +void *data); + +static inline void timespec_to_pps_ktime (struct pps_ktime *kt, +struct timespec ts) +{ + kt->sec = ts.tv_sec; + kt->nsec = ts.tv_nsec; +} + +static inline void pps_get_ts (struct pps_event_time *ts) +{ +struct timespec ts_real; + +getnstime_raw_and_real (&ts->ts_raw, &ts_real); +timespec_to_pps_ktime (&ts->ts_real, ts_real); +} + +#endif /* LINUX_PPS_KERNEL_H */ +
include/linux/time.h +2 -0
@@ -143,6 +143,8 @@ extern unsigned int alarm_setitimer(unsigned int seconds);extern int do_getitimer (int which,struct itimerval *value);extern void getnstimeofday (struct timespec *tv);extern void getrawmonotonic (struct timespec *ts); +extern void getnstime_raw_and_real (struct timespec *ts_raw, +struct timespec *ts_real);extern void getboottime (struct timespec *ts);extern void monotonic_to_bootbased (struct timespec *ts);
kernel/time/timekeeping.c +34 -0
@@ -293,6 +293,40 @@ void ktime_get_ts(struct timespec *ts)EXPORT_SYMBOL_GPL (ktime_get_ts);/** +* getnstime_raw_and_real - Returns both the time of day an raw +* monotonic time in a timespec format +* @ts_mono_raw: pointer to the timespec to be set to raw +* monotonic time +* @ts_real: pointer to the timespec to be set to the time +* of day +*/ +void getnstime_raw_and_real (struct timespec *ts_raw,struct timespec *ts_real) +{ +unsigned long seq; +s64 nsecs_raw, nsecs_real; + +WARN_ON (timekeeping_suspended); + +do { + seq =read_seqbegin (&xtime_lock); + + *ts_raw = raw_time; + *ts_real = xtime; + + nsecs_raw =timekeeping_get_ns_raw (); + nsecs_real =timekeeping_get_ns (); + +/* If arch requires, add in gettimeoffset() */ + nsecs_real +=arch_gettimeoffset (); + + }while (read_seqretry (&xtime_lock, seq)); + +timespec_add_ns (ts_raw, nsecs_raw); +timespec_add_ns (ts_real, nsecs_real); +} +EXPORT_SYMBOL (getnstime_raw_and_real); + +/** * do_gettimeofday - Returns the time of day in a timeval * @tv: pointer to the timeval to be set *
Signature · 1 line
1.6.5
Re: [PATCH 2/5] pps: capture MONOTONIC_RAW timestamps as well
Quoted 6 lines · Alexander Gordeev
MONOTONIC_RAWclock timestamps are ideally suited for frequency calculation and also fit well into the original NTP hardpps design. Now phase and frequency can be adjusted separately: the former based on REALTIME clock and the latter based onMONOTONIC_RAWclock. A new functiongetnstime_raw_and_realis added to timekeeping subsystem to capture both timestamps at the same time and atomically.
Hrmm. So while I understand the need for it, the getnstime_raw_and_real() makes me cringe a little. Part of the issue is that there are multiple CLOCK_IDs and the current interface allows for accesses to only one at a time. There's a similar hack in the hrtimer code to get the CLOCK_REALTIME and CLOCK_MONOTONIC values at the same time. Next I worry that folks will want a getnstime_mono_and_raw() or a getnstime_real_mono_and_raw(), then a getnstime_real_and_realcoarse(), etc..
I'm almost thinking the way to handle this would be a better abstraction, like a get_two_times(CLOCKID, timepsec*, CLOCKID, timespec*). But that might need some further discussion. Anyone else have thoughts here?
So yea not opposed to this patch, but maybe try to avoid exporting the symbol, so modules don't end up using it and we can change it fairly easily later.
thanks
-john
Re: [PATCH 2/5] pps: capture MONOTONIC_RAW timestamps as well
В Wed, 03 Feb 2010 14:26:16 -0800
john stultz <[email protected]> пишет:
Quoted 18 lines · Alexander Gordeev
Quoted 7 lines
MONOTONIC_RAWclock timestamps are ideally suited for frequency calculation and also fit well into the original NTP hardpps design. Now phase and frequency can be adjusted separately: the former based on REALTIME clock and the latter based onMONOTONIC_RAWclock. A new functiongetnstime_raw_and_realis added to timekeeping subsystem to capture both timestamps at the same time and atomically.Hrmm. So while I understand the need for it, the
getnstime_raw_and_real()makes me cringe a little. Part of the issue is that there are multipleCLOCK_IDsand the current interface allows for accesses to only one at a time. There's a similar hack in the hrtimer code to get theCLOCK_REALTIMEandCLOCK_MONOTONICvalues at the same time. Next I worry that folks will want agetnstime_mono_and_raw()or agetnstime_real_mono_and_raw(), then agetnstime_real_and_realcoarse(), etc..
Quoted 4 lines
I'm almost thinking the way to handle this would be a better abstraction, like a
get_two_times(CLOCKID, timepsec*, CLOCKID, timespec*). But that might need some further discussion. Anyone else have thoughts here?
Agreed with you completely. I don't like the approach but failed to invent anything better.
Quoted 3 lines
So yea not opposed to this patch, but maybe try to avoid exporting the symbol, so modules don't end up using it and we can change it fairly easily later.
Well, I'd like to, but how? getnstime_raw_and_real() is used in module added in one of the next commits.
Signature · 1 line
Alexander
PATCH 3/5 pps: add kernel consumer support
In reply to Alexander GordeevAdd an optional feature of PPSAPI, kernel consumer support, which uses the added hardpps() function.
--- Documentation/ioctl/ioctl-number.txt | 2 +- drivers/pps/kapi.c | 28 ++++++++++++++ drivers/pps/pps.c | 67 +++++++++++++++++++++++++++++++++- include/linux/pps.h | 7 ++++ include/linux/pps_kernel.h | 6 +++ 5 files changed, 108 insertions(+), 2 deletions(-)
Documentation/ioctl/ioctl-number.txt +1 -1
@@ -152,7 +152,7 @@ Code Seq# Include File Comments 'p' 40-7F linux/nvram.h 'p' 80-9F user-space parport <mailto:[email protected]> -'p' a1-a4 linux/pps.h LinuxPPS +'p' a1-a5 linux/pps.h LinuxPPS <mailto:[email protected]> 'q' 00-1F linux/serio.h 'q' 80-FF Internet PhoneJACK, Internet LineJACK
drivers/pps/kapi.c +28 -0
@@ -25,6 +25,7 @@#include <linux/init.h> #include <linux/sched.h> #include <linux/time.h> +#include <linux/timex.h> #include <linux/spinlock.h> #include <linux/idr.h> #include <linux/fs.h>
@@ -37,6 +38,12 @@DEFINE_SPINLOCK (pps_idr_lock);DEFINE_IDR (pps_idr); +/* state variables to bind kernel consumer */ +/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */ +DEFINE_SPINLOCK (pps_kc_hardpps_lock); +void *pps_kc_hardpps_dev;/* some unique pointer to device */ +int pps_kc_hardpps_mode;/* mode bits for kernel consumer */ +/* * Local functions */
@@ -272,6 +279,7 @@ void pps_event(int source, struct pps_event_time *ts, int event, void *data)struct pps_device *pps;unsigned long flags;int captured =0 ; +s64 nsec =0 ;if ((event & (PPS_CAPTUREASSERT |PPS_CAPTURECLEAR )) ==0 ) { printk(KERN_ERR "pps: unknown event (%x) for source %d\n",
@@ -304,6 +312,11 @@ void pps_event(int source, struct pps_event_time *ts, int event, void *data)/* Save the time stamp */ pps->assert_tu = ts->ts_real; +if (likely (pps->assert_tu_raw.tv_sec !=0 )) { +struct timespec d = +timespec_sub (ts->ts_raw, pps->assert_tu_raw); + nsec =timespec_to_ns (&d); + } pps->assert_tu_raw = ts->ts_raw; pps->assert_sequence++; pr_debug("capture assert seq #%u for source %d\n",
@@ -320,6 +333,11 @@ void pps_event(int source, struct pps_event_time *ts, int event, void *data)/* Save the time stamp */ pps->clear_tu = ts->ts_real; +if (likely (pps->clear_tu_raw.tv_sec !=0 )) { +struct timespec d = +timespec_sub (ts->ts_raw, pps->clear_tu_raw); + nsec =timespec_to_ns (&d); + } pps->clear_tu_raw = ts->ts_raw; pps->clear_sequence++; pr_debug("capture clear seq #%u for source %d\n",
@@ -327,6 +345,16 @@ void pps_event(int source, struct pps_event_time *ts, int event, void *data) captured = ~0 ; } +spin_lock (&pps_kc_hardpps_lock); +if (pps == pps_kc_hardpps_dev && event & pps_kc_hardpps_mode) { +struct timespec p_ts = { + .tv_sec = ts->ts_real.sec, + .tv_nsec = ts->ts_real.nsec + }; + +hardpps (&p_ts, nsec); + } +spin_unlock (&pps_kc_hardpps_lock);/* Wake up iif captured somthing */ if (captured) {
drivers/pps/pps.c +66 -1
81 lines not on this page · raw message
include/linux/pps.h +7 -0
@@ -114,11 +114,18 @@ struct pps_fdata { struct pps_ktime timeout; }; +struct pps_bind_args { +int tsformat;/* format of time stamps */ +int edge;/* selected event type */ +int consumer;/* selected kernel consumer */ +}; +#include <linux/ioctl.h> #define PPS_GETPARAMS _IOR('p', 0xa1, struct pps_kparams *)#define PPS_SETPARAMS _IOW('p', 0xa2, struct pps_kparams *)#define PPS_GETCAP _IOR('p', 0xa3, int *)#define PPS_FETCH _IOWR('p', 0xa4, struct pps_fdata *) +#define PPS_KC_BIND _IOW('p', 0xa5, struct pps_bind_args *)#endif /* _PPS_H_ */
include/linux/pps_kernel.h +6 -0
@@ -85,6 +85,12 @@ extern struct idr pps_idr;extern struct device_attribute pps_attrs[]; +/* state variables to bind kernel consumer */ +/* PPS API (RFC 2783): current source and mode for ``kernel consumer'' */ +extern spinlock_t pps_kc_hardpps_lock; +extern void *pps_kc_hardpps_dev;/* some unique pointer to device */ +extern int pps_kc_hardpps_mode;/* mode bits for kernel consumer */ +/* * Exported functions */
Signature · 1 line
1.6.5
PATCH 4/5 pps: add parallel port PPS signal generator
In reply to Alexander GordeevAdd PPS signal generator which utilizes STROBE pin of a parallel port to send PPS signals. It uses parport abstraction layer and hrtimers to precisely control the signal.
--- drivers/pps/Kconfig | 2 + drivers/pps/Makefile | 1 + drivers/pps/generators/Kconfig | 17 ++ drivers/pps/generators/Makefile | 9 + drivers/pps/generators/pps_gen_parport.c | 263 ++++++++++++++++++++++++++++++ 5 files changed, 292 insertions(+), 0 deletions(-) create mode 100644 drivers/pps/generators/Kconfig create mode 100644 drivers/pps/generators/Makefile create mode 100644 drivers/pps/generators/pps_gen_parport.c
drivers/pps/Kconfig +2 -0
@@ -31,4 +31,6 @@ config PPS_DEBUG messages to the system log. Select this if you are having a problem with PPS support and want to see more of what is going on. +source drivers/pps/generators/Kconfig + endmenu
drivers/pps/Makefile +1 -0
@@ -4,5 +4,6 @@ pps_core-y := pps.o kapi.o sysfs.o obj-$(CONFIG_PPS) := pps_core.o +obj-y += generators/ ccflags-$(CONFIG_PPS_DEBUG) := -DDEBUG
drivers/pps/generators/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# +# PPS clients configuration +# + +if PPS + +comment "PPS generators support" + +config PPS_GENERATOR_PARPORT + tristate "Parallel port PPS signal generator" + depends on PARPORT != n && GENERIC_TIME + help + If you say yes here you get support for a PPS signal generator which + utilizes STROBE pin of a parallel port to send PPS signals. It uses + parport abstraction layer and hrtimers to precisely control the signal. + +endif
drivers/pps/generators/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# +# Makefile for PPS generators. +# + +obj-$(CONFIG_PPS_GENERATOR_PARPORT) += pps_gen_parport.o + +ifeq ($(CONFIG_PPS_DEBUG),y) +EXTRA_CFLAGS += -DDEBUG +endif
drivers/pps/generators/pps_gen_parport.c new file +263 -0
263 lines not on this page · raw message
Signature · 1 line
1.6.5
Rodolfo Giometti
maintainerRe: [PATCH 4/5] pps: add parallel port PPS signal generator
Quoted 3 lines · Alexander Gordeev
Add PPS signal generator which utilizes STROBE pin of a parallel port to send PPS signals. It uses parport abstraction layer and hrtimers to precisely control the signal.
What this generators are used for?
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
Re: [LinuxPPS] [PATCH 4/5] pps: add parallel port PPS signal generator
Hi Rodolfo,
I've accidentally found your email in the archives of linuxpps list. Very strange that it didn't come to me directly because I was subscribed...
В Fri, 5 Feb 2010 11:39:14 +0100
giometti at enneenne.com (Rodolfo Giometti) пишет:
Quoted 6 lines · Alexander Gordeev
Quoted 3 lines
Add PPS signal generator which utilizes STROBE pin of a parallel port to send PPS signals. It uses parport abstraction layer and hrtimers to precisely control the signal.
What this generators are used for?
This generator sends PPS signals over a parallel port from one computer to another. So it's just doing what every PPS-capable GPS device does. I use it to precisely synchronize time on several computers for distributed real-time simulation. It has very strict timing conditions.
Signature · 1 line
Alexander
PATCH 5/5 pps: add parallel port PPS client
In reply to Alexander GordeevAdd parallel port PPS client. It uses a standard method for capturing timestamps for assert edge transitions: getting a timestamp soon after an interrupt has happened. This is not a very precise source of time information due to interrupt handling delays. However, timestamps for clear edge transitions are much more precise because the interrupt handler continuously polls hardware port until the transition is done. Hardware port operations require only about 1us so the maximum error should not exceed this value. This was my primary goal when developing this client.
--- drivers/pps/Kconfig | 2 + drivers/pps/Makefile | 2 +- drivers/pps/clients/Kconfig | 16 +++ drivers/pps/clients/Makefile | 9 ++ drivers/pps/clients/pps_parport.c | 211 +++++++++++++++++++++++++++++++++++++ 5 files changed, 239 insertions(+), 1 deletions(-) create mode 100644 drivers/pps/clients/Kconfig create mode 100644 drivers/pps/clients/Makefile create mode 100644 drivers/pps/clients/pps_parport.c
drivers/pps/Kconfig +2 -0
@@ -31,6 +31,8 @@ config PPS_DEBUG messages to the system log. Select this if you are having a problem with PPS support and want to see more of what is going on. +source drivers/pps/clients/Kconfig + source drivers/pps/generators/Kconfig endmenu
drivers/pps/Makefile +1 -1
@@ -4,6 +4,6 @@ pps_core-y := pps.o kapi.o sysfs.o obj-$(CONFIG_PPS) := pps_core.o -obj-y += generators/ +obj-y += clients/ generators/ ccflags-$(CONFIG_PPS_DEBUG) := -DDEBUG
drivers/pps/clients/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +# +# PPS clients configuration +# + +if PPS + +comment "PPS clients support" + +config PPS_CLIENT_PARPORT + tristate "Parallel port PPS client" + depends on PPS && PARPORT + help + If you say yes here you get support for a PPS source connected + with the interrupt pin of your parallel port. + +endif
drivers/pps/clients/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# +# Makefile for PPS clients. +# + +obj-$(CONFIG_PPS_CLIENT_PARPORT) += pps_parport.o + +ifeq ($(CONFIG_PPS_DEBUG),y) +EXTRA_CFLAGS += -DDEBUG +endif
drivers/pps/clients/pps_parport.c new file +211 -0
211 lines not on this page · raw message
Signature · 1 line
1.6.5