| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 1 | /* | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 2 | * NTP state machine interfaces and logic. | 
|  | 3 | * | 
|  | 4 | * This code was mainly moved from kernel/timer.c and kernel/time.c | 
|  | 5 | * Please see those files for relevant copyright info and historical | 
|  | 6 | * changelogs. | 
|  | 7 | */ | 
| Alexey Dobriyan | aa0ac36 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 8 | #include <linux/capability.h> | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 9 | #include <linux/clocksource.h> | 
| Maciej W. Rozycki | eb3f938 | 2008-09-22 14:42:40 -0700 | [diff] [blame] | 10 | #include <linux/workqueue.h> | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 11 | #include <linux/hrtimer.h> | 
|  | 12 | #include <linux/jiffies.h> | 
|  | 13 | #include <linux/math64.h> | 
|  | 14 | #include <linux/timex.h> | 
|  | 15 | #include <linux/time.h> | 
|  | 16 | #include <linux/mm.h> | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 17 |  | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 18 | /* | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 19 | * NTP timekeeping variables: | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 20 | */ | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 21 |  | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 22 | /* USER_HZ period (usecs): */ | 
|  | 23 | unsigned long			tick_usec = TICK_USEC; | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 24 |  | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 25 | /* ACTHZ period (nsecs): */ | 
|  | 26 | unsigned long			tick_nsec; | 
|  | 27 |  | 
|  | 28 | u64				tick_length; | 
|  | 29 | static u64			tick_length_base; | 
|  | 30 |  | 
|  | 31 | static struct hrtimer		leap_timer; | 
|  | 32 |  | 
| Ingo Molnar | bbd1267 | 2009-02-22 12:11:11 +0100 | [diff] [blame] | 33 | #define MAX_TICKADJ		500LL		/* usecs */ | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 34 | #define MAX_TICKADJ_SCALED \ | 
| Ingo Molnar | bbd1267 | 2009-02-22 12:11:11 +0100 | [diff] [blame] | 35 | (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | /* | 
|  | 38 | * phase-lock loop variables | 
|  | 39 | */ | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 40 |  | 
|  | 41 | /* | 
|  | 42 | * clock synchronization status | 
|  | 43 | * | 
|  | 44 | * (TIME_ERROR prevents overwriting the CMOS clock) | 
|  | 45 | */ | 
|  | 46 | static int			time_state = TIME_OK; | 
|  | 47 |  | 
|  | 48 | /* clock status bits:							*/ | 
|  | 49 | int				time_status = STA_UNSYNC; | 
|  | 50 |  | 
|  | 51 | /* TAI offset (secs):							*/ | 
|  | 52 | static long			time_tai; | 
|  | 53 |  | 
|  | 54 | /* time adjustment (nsecs):						*/ | 
|  | 55 | static s64			time_offset; | 
|  | 56 |  | 
|  | 57 | /* pll time constant:							*/ | 
|  | 58 | static long			time_constant = 2; | 
|  | 59 |  | 
|  | 60 | /* maximum error (usecs):						*/ | 
| john stultz | 1f5b8f8 | 2010-01-28 15:02:41 -0800 | [diff] [blame] | 61 | static long			time_maxerror = NTP_PHASE_LIMIT; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 62 |  | 
|  | 63 | /* estimated error (usecs):						*/ | 
| john stultz | 1f5b8f8 | 2010-01-28 15:02:41 -0800 | [diff] [blame] | 64 | static long			time_esterror = NTP_PHASE_LIMIT; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 65 |  | 
|  | 66 | /* frequency offset (scaled nsecs/secs):				*/ | 
|  | 67 | static s64			time_freq; | 
|  | 68 |  | 
|  | 69 | /* time at last adjustment (secs):					*/ | 
|  | 70 | static long			time_reftime; | 
|  | 71 |  | 
| John Stultz | e1292ba | 2010-03-18 20:19:27 -0700 | [diff] [blame] | 72 | static long			time_adjust; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 73 |  | 
| Ingo Molnar | 069569e | 2009-02-22 16:03:37 +0100 | [diff] [blame] | 74 | /* constant (boot-param configurable) NTP tick adjustment (upscaled)	*/ | 
|  | 75 | static s64			ntp_tick_adj; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 76 |  | 
|  | 77 | /* | 
|  | 78 | * NTP methods: | 
|  | 79 | */ | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 80 |  | 
| Ingo Molnar | 9ce616a | 2009-02-22 12:42:59 +0100 | [diff] [blame] | 81 | /* | 
|  | 82 | * Update (tick_length, tick_length_base, tick_nsec), based | 
|  | 83 | * on (tick_usec, ntp_tick_adj, time_freq): | 
|  | 84 | */ | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 85 | static void ntp_update_frequency(void) | 
|  | 86 | { | 
| Ingo Molnar | 9ce616a | 2009-02-22 12:42:59 +0100 | [diff] [blame] | 87 | u64 second_length; | 
| Ingo Molnar | bc26c31 | 2009-02-22 12:17:36 +0100 | [diff] [blame] | 88 | u64 new_base; | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 89 |  | 
| Ingo Molnar | 9ce616a | 2009-02-22 12:42:59 +0100 | [diff] [blame] | 90 | second_length		 = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) | 
|  | 91 | << NTP_SCALE_SHIFT; | 
|  | 92 |  | 
| Ingo Molnar | 069569e | 2009-02-22 16:03:37 +0100 | [diff] [blame] | 93 | second_length		+= ntp_tick_adj; | 
| Ingo Molnar | 9ce616a | 2009-02-22 12:42:59 +0100 | [diff] [blame] | 94 | second_length		+= time_freq; | 
|  | 95 |  | 
| Ingo Molnar | 9ce616a | 2009-02-22 12:42:59 +0100 | [diff] [blame] | 96 | tick_nsec		 = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT; | 
| Ingo Molnar | bc26c31 | 2009-02-22 12:17:36 +0100 | [diff] [blame] | 97 | new_base		 = div_u64(second_length, NTP_INTERVAL_FREQ); | 
| john stultz | fdcedf7 | 2009-02-18 16:02:22 -0800 | [diff] [blame] | 98 |  | 
|  | 99 | /* | 
|  | 100 | * Don't wait for the next second_overflow, apply | 
| Ingo Molnar | bc26c31 | 2009-02-22 12:17:36 +0100 | [diff] [blame] | 101 | * the change to the tick length immediately: | 
| john stultz | fdcedf7 | 2009-02-18 16:02:22 -0800 | [diff] [blame] | 102 | */ | 
| Ingo Molnar | bc26c31 | 2009-02-22 12:17:36 +0100 | [diff] [blame] | 103 | tick_length		+= new_base - tick_length_base; | 
|  | 104 | tick_length_base	 = new_base; | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Ingo Molnar | 478b7aa | 2009-02-22 13:22:23 +0100 | [diff] [blame] | 107 | static inline s64 ntp_update_offset_fll(s64 offset64, long secs) | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 108 | { | 
|  | 109 | time_status &= ~STA_MODE; | 
|  | 110 |  | 
|  | 111 | if (secs < MINSEC) | 
| Ingo Molnar | 478b7aa | 2009-02-22 13:22:23 +0100 | [diff] [blame] | 112 | return 0; | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 113 |  | 
|  | 114 | if (!(time_status & STA_FLL) && (secs <= MAXSEC)) | 
| Ingo Molnar | 478b7aa | 2009-02-22 13:22:23 +0100 | [diff] [blame] | 115 | return 0; | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 116 |  | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 117 | time_status |= STA_MODE; | 
|  | 118 |  | 
| Ingo Molnar | 478b7aa | 2009-02-22 13:22:23 +0100 | [diff] [blame] | 119 | return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs); | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 122 | static void ntp_update_offset(long offset) | 
|  | 123 | { | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 124 | s64 freq_adj; | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 125 | s64 offset64; | 
|  | 126 | long secs; | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 127 |  | 
|  | 128 | if (!(time_status & STA_PLL)) | 
|  | 129 | return; | 
|  | 130 |  | 
| Roman Zippel | eea83d8 | 2008-05-01 04:34:33 -0700 | [diff] [blame] | 131 | if (!(time_status & STA_NANO)) | 
| Roman Zippel | 9f14f66 | 2008-05-01 04:34:36 -0700 | [diff] [blame] | 132 | offset *= NSEC_PER_USEC; | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | /* | 
|  | 135 | * Scale the phase adjustment and | 
|  | 136 | * clamp to the operating range. | 
|  | 137 | */ | 
| Roman Zippel | 9f14f66 | 2008-05-01 04:34:36 -0700 | [diff] [blame] | 138 | offset = min(offset, MAXPHASE); | 
|  | 139 | offset = max(offset, -MAXPHASE); | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | /* | 
|  | 142 | * Select how the frequency is to be controlled | 
|  | 143 | * and in which mode (PLL or FLL). | 
|  | 144 | */ | 
| John Stultz | 7e1b584 | 2010-01-28 20:20:44 -0800 | [diff] [blame] | 145 | secs = get_seconds() - time_reftime; | 
| Ingo Molnar | 10dd31a | 2009-02-22 13:38:40 +0100 | [diff] [blame] | 146 | if (unlikely(time_status & STA_FREQHOLD)) | 
| Ingo Molnar | c7986ac | 2009-02-22 13:29:09 +0100 | [diff] [blame] | 147 | secs = 0; | 
|  | 148 |  | 
| John Stultz | 7e1b584 | 2010-01-28 20:20:44 -0800 | [diff] [blame] | 149 | time_reftime = get_seconds(); | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 150 |  | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 151 | offset64    = offset; | 
| Miroslav Lichvar | 8af3c15 | 2010-09-07 16:43:46 +0200 | [diff] [blame] | 152 | freq_adj    = ntp_update_offset_fll(offset64, secs); | 
| Roman Zippel | 9f14f66 | 2008-05-01 04:34:36 -0700 | [diff] [blame] | 153 |  | 
| Miroslav Lichvar | 8af3c15 | 2010-09-07 16:43:46 +0200 | [diff] [blame] | 154 | /* | 
|  | 155 | * Clamp update interval to reduce PLL gain with low | 
|  | 156 | * sampling rate (e.g. intermittent network connection) | 
|  | 157 | * to avoid instability. | 
|  | 158 | */ | 
|  | 159 | if (unlikely(secs > 1 << (SHIFT_PLL + 1 + time_constant))) | 
|  | 160 | secs = 1 << (SHIFT_PLL + 1 + time_constant); | 
|  | 161 |  | 
|  | 162 | freq_adj    += (offset64 * secs) << | 
|  | 163 | (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant)); | 
| Ingo Molnar | f939890 | 2009-02-22 12:57:49 +0100 | [diff] [blame] | 164 |  | 
|  | 165 | freq_adj    = min(freq_adj + time_freq, MAXFREQ_SCALED); | 
|  | 166 |  | 
|  | 167 | time_freq   = max(freq_adj, -MAXFREQ_SCALED); | 
|  | 168 |  | 
|  | 169 | time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ); | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 172 | /** | 
|  | 173 | * ntp_clear - Clears the NTP state variables | 
|  | 174 | * | 
|  | 175 | * Must be called while holding a write on the xtime_lock | 
|  | 176 | */ | 
|  | 177 | void ntp_clear(void) | 
|  | 178 | { | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 179 | time_adjust	= 0;		/* stop active adjtime() */ | 
|  | 180 | time_status	|= STA_UNSYNC; | 
|  | 181 | time_maxerror	= NTP_PHASE_LIMIT; | 
|  | 182 | time_esterror	= NTP_PHASE_LIMIT; | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 183 |  | 
|  | 184 | ntp_update_frequency(); | 
|  | 185 |  | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 186 | tick_length	= tick_length_base; | 
|  | 187 | time_offset	= 0; | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 190 | /* | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 191 | * Leap second processing. If in leap-insert state at the end of the | 
|  | 192 | * day, the system clock is set back one second; if in leap-delete | 
|  | 193 | * state, the system clock is set ahead one second. | 
|  | 194 | */ | 
|  | 195 | static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer) | 
|  | 196 | { | 
|  | 197 | enum hrtimer_restart res = HRTIMER_NORESTART; | 
|  | 198 |  | 
| Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 199 | write_seqlock(&xtime_lock); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | switch (time_state) { | 
|  | 202 | case TIME_OK: | 
|  | 203 | break; | 
|  | 204 | case TIME_INS: | 
| John Stultz | 31089c1 | 2009-08-14 15:47:18 +0200 | [diff] [blame] | 205 | timekeeping_leap_insert(-1); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 206 | time_state = TIME_OOP; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 207 | printk(KERN_NOTICE | 
|  | 208 | "Clock: inserting leap second 23:59:60 UTC\n"); | 
| Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 209 | hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 210 | res = HRTIMER_RESTART; | 
|  | 211 | break; | 
|  | 212 | case TIME_DEL: | 
| John Stultz | 31089c1 | 2009-08-14 15:47:18 +0200 | [diff] [blame] | 213 | timekeeping_leap_insert(1); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 214 | time_tai--; | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 215 | time_state = TIME_WAIT; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 216 | printk(KERN_NOTICE | 
|  | 217 | "Clock: deleting leap second 23:59:59 UTC\n"); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 218 | break; | 
|  | 219 | case TIME_OOP: | 
|  | 220 | time_tai++; | 
|  | 221 | time_state = TIME_WAIT; | 
|  | 222 | /* fall through */ | 
|  | 223 | case TIME_WAIT: | 
|  | 224 | if (!(time_status & (STA_INS | STA_DEL))) | 
|  | 225 | time_state = TIME_OK; | 
|  | 226 | break; | 
|  | 227 | } | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 228 |  | 
| Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 229 | write_sequnlock(&xtime_lock); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | return res; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | /* | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 235 | * this routine handles the overflow of the microsecond field | 
|  | 236 | * | 
|  | 237 | * The tricky bits of code to handle the accurate clock support | 
|  | 238 | * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. | 
|  | 239 | * They were originally developed for SUN and DEC kernels. | 
|  | 240 | * All the kudos should go to Dave for this stuff. | 
|  | 241 | */ | 
|  | 242 | void second_overflow(void) | 
|  | 243 | { | 
| Ingo Molnar | 39854fe | 2009-02-22 16:06:58 +0100 | [diff] [blame] | 244 | s64 delta; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 245 |  | 
|  | 246 | /* Bump the maxerror field */ | 
| Roman Zippel | 074b3b8 | 2008-05-01 04:34:34 -0700 | [diff] [blame] | 247 | time_maxerror += MAXFREQ / NSEC_PER_USEC; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 248 | if (time_maxerror > NTP_PHASE_LIMIT) { | 
|  | 249 | time_maxerror = NTP_PHASE_LIMIT; | 
|  | 250 | time_status |= STA_UNSYNC; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | /* | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 254 | * Compute the phase adjustment for the next second. The offset is | 
|  | 255 | * reduced by a fixed factor times the time constant. | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 256 | */ | 
| Ingo Molnar | 39854fe | 2009-02-22 16:06:58 +0100 | [diff] [blame] | 257 | tick_length	 = tick_length_base; | 
|  | 258 |  | 
|  | 259 | delta		 = shift_right(time_offset, SHIFT_PLL + time_constant); | 
|  | 260 | time_offset	-= delta; | 
|  | 261 | tick_length	+= delta; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 262 |  | 
| Ingo Molnar | 3c972c2 | 2009-02-22 12:06:57 +0100 | [diff] [blame] | 263 | if (!time_adjust) | 
|  | 264 | return; | 
|  | 265 |  | 
|  | 266 | if (time_adjust > MAX_TICKADJ) { | 
|  | 267 | time_adjust -= MAX_TICKADJ; | 
|  | 268 | tick_length += MAX_TICKADJ_SCALED; | 
|  | 269 | return; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 270 | } | 
| Ingo Molnar | 3c972c2 | 2009-02-22 12:06:57 +0100 | [diff] [blame] | 271 |  | 
|  | 272 | if (time_adjust < -MAX_TICKADJ) { | 
|  | 273 | time_adjust += MAX_TICKADJ; | 
|  | 274 | tick_length -= MAX_TICKADJ_SCALED; | 
|  | 275 | return; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ) | 
|  | 279 | << NTP_SCALE_SHIFT; | 
|  | 280 | time_adjust = 0; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 283 | #ifdef CONFIG_GENERIC_CMOS_UPDATE | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 284 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 285 | /* Disable the cmos update - used by virtualization and embedded */ | 
|  | 286 | int no_sync_cmos_clock  __read_mostly; | 
|  | 287 |  | 
| Maciej W. Rozycki | eb3f938 | 2008-09-22 14:42:40 -0700 | [diff] [blame] | 288 | static void sync_cmos_clock(struct work_struct *work); | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 289 |  | 
| Maciej W. Rozycki | eb3f938 | 2008-09-22 14:42:40 -0700 | [diff] [blame] | 290 | static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock); | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 291 |  | 
| Maciej W. Rozycki | eb3f938 | 2008-09-22 14:42:40 -0700 | [diff] [blame] | 292 | static void sync_cmos_clock(struct work_struct *work) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 293 | { | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 294 | struct timespec now, next; | 
|  | 295 | int fail = 1; | 
|  | 296 |  | 
|  | 297 | /* | 
|  | 298 | * If we have an externally synchronized Linux clock, then update | 
|  | 299 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be | 
|  | 300 | * called as close as possible to 500 ms before the new second starts. | 
|  | 301 | * This code is run on a timer.  If the clock is set, that timer | 
|  | 302 | * may not expire at the correct time.  Thus, we adjust... | 
|  | 303 | */ | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 304 | if (!ntp_synced()) { | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 305 | /* | 
|  | 306 | * Not synced, exit, do not restart a timer (if one is | 
|  | 307 | * running, let it run out). | 
|  | 308 | */ | 
|  | 309 | return; | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 310 | } | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 311 |  | 
|  | 312 | getnstimeofday(&now); | 
| David P. Reed | fa6a1a5 | 2007-11-14 17:49:21 -0500 | [diff] [blame] | 313 | if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2) | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 314 | fail = update_persistent_clock(now); | 
|  | 315 |  | 
| Maciej W. Rozycki | 4ff4b9e | 2008-09-05 14:05:31 -0700 | [diff] [blame] | 316 | next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2); | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 317 | if (next.tv_nsec <= 0) | 
|  | 318 | next.tv_nsec += NSEC_PER_SEC; | 
|  | 319 |  | 
|  | 320 | if (!fail) | 
|  | 321 | next.tv_sec = 659; | 
|  | 322 | else | 
|  | 323 | next.tv_sec = 0; | 
|  | 324 |  | 
|  | 325 | if (next.tv_nsec >= NSEC_PER_SEC) { | 
|  | 326 | next.tv_sec++; | 
|  | 327 | next.tv_nsec -= NSEC_PER_SEC; | 
|  | 328 | } | 
| Maciej W. Rozycki | eb3f938 | 2008-09-22 14:42:40 -0700 | [diff] [blame] | 329 | schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next)); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 330 | } | 
|  | 331 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 332 | static void notify_cmos_timer(void) | 
|  | 333 | { | 
| Tony Breeds | 298a5df | 2007-09-11 15:24:03 -0700 | [diff] [blame] | 334 | if (!no_sync_cmos_clock) | 
| Maciej W. Rozycki | eb3f938 | 2008-09-22 14:42:40 -0700 | [diff] [blame] | 335 | schedule_delayed_work(&sync_cmos_work, 0); | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 336 | } | 
|  | 337 |  | 
|  | 338 | #else | 
|  | 339 | static inline void notify_cmos_timer(void) { } | 
|  | 340 | #endif | 
|  | 341 |  | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 342 | /* | 
|  | 343 | * Start the leap seconds timer: | 
|  | 344 | */ | 
|  | 345 | static inline void ntp_start_leap_timer(struct timespec *ts) | 
|  | 346 | { | 
|  | 347 | long now = ts->tv_sec; | 
|  | 348 |  | 
|  | 349 | if (time_status & STA_INS) { | 
|  | 350 | time_state = TIME_INS; | 
|  | 351 | now += 86400 - now % 86400; | 
|  | 352 | hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS); | 
|  | 353 |  | 
|  | 354 | return; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | if (time_status & STA_DEL) { | 
|  | 358 | time_state = TIME_DEL; | 
|  | 359 | now += 86400 - (now + 1) % 86400; | 
|  | 360 | hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS); | 
|  | 361 | } | 
|  | 362 | } | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 363 |  | 
|  | 364 | /* | 
|  | 365 | * Propagate a new txc->status value into the NTP state: | 
|  | 366 | */ | 
|  | 367 | static inline void process_adj_status(struct timex *txc, struct timespec *ts) | 
|  | 368 | { | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 369 | if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) { | 
|  | 370 | time_state = TIME_OK; | 
|  | 371 | time_status = STA_UNSYNC; | 
|  | 372 | } | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 373 |  | 
|  | 374 | /* | 
|  | 375 | * If we turn on PLL adjustments then reset the | 
|  | 376 | * reference time to current time. | 
|  | 377 | */ | 
|  | 378 | if (!(time_status & STA_PLL) && (txc->status & STA_PLL)) | 
| John Stultz | 7e1b584 | 2010-01-28 20:20:44 -0800 | [diff] [blame] | 379 | time_reftime = get_seconds(); | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 380 |  | 
| John Stultz | a2a5ac8 | 2009-02-26 09:46:14 -0800 | [diff] [blame] | 381 | /* only set allowed bits */ | 
|  | 382 | time_status &= STA_RONLY; | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 383 | time_status |= txc->status & ~STA_RONLY; | 
|  | 384 |  | 
|  | 385 | switch (time_state) { | 
|  | 386 | case TIME_OK: | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 387 | ntp_start_leap_timer(ts); | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 388 | break; | 
|  | 389 | case TIME_INS: | 
|  | 390 | case TIME_DEL: | 
|  | 391 | time_state = TIME_OK; | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 392 | ntp_start_leap_timer(ts); | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 393 | case TIME_WAIT: | 
|  | 394 | if (!(time_status & (STA_INS | STA_DEL))) | 
|  | 395 | time_state = TIME_OK; | 
|  | 396 | break; | 
|  | 397 | case TIME_OOP: | 
|  | 398 | hrtimer_restart(&leap_timer); | 
|  | 399 | break; | 
|  | 400 | } | 
|  | 401 | } | 
|  | 402 | /* | 
|  | 403 | * Called with the xtime lock held, so we can access and modify | 
|  | 404 | * all the global NTP state: | 
|  | 405 | */ | 
|  | 406 | static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts) | 
|  | 407 | { | 
|  | 408 | if (txc->modes & ADJ_STATUS) | 
|  | 409 | process_adj_status(txc, ts); | 
|  | 410 |  | 
|  | 411 | if (txc->modes & ADJ_NANO) | 
|  | 412 | time_status |= STA_NANO; | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 413 |  | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 414 | if (txc->modes & ADJ_MICRO) | 
|  | 415 | time_status &= ~STA_NANO; | 
|  | 416 |  | 
|  | 417 | if (txc->modes & ADJ_FREQUENCY) { | 
| Ingo Molnar | 2b9d149 | 2009-02-22 15:48:43 +0100 | [diff] [blame] | 418 | time_freq = txc->freq * PPM_SCALE; | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 419 | time_freq = min(time_freq, MAXFREQ_SCALED); | 
|  | 420 | time_freq = max(time_freq, -MAXFREQ_SCALED); | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | if (txc->modes & ADJ_MAXERROR) | 
|  | 424 | time_maxerror = txc->maxerror; | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 425 |  | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 426 | if (txc->modes & ADJ_ESTERROR) | 
|  | 427 | time_esterror = txc->esterror; | 
|  | 428 |  | 
|  | 429 | if (txc->modes & ADJ_TIMECONST) { | 
|  | 430 | time_constant = txc->constant; | 
|  | 431 | if (!(time_status & STA_NANO)) | 
|  | 432 | time_constant += 4; | 
|  | 433 | time_constant = min(time_constant, (long)MAXTC); | 
|  | 434 | time_constant = max(time_constant, 0l); | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | if (txc->modes & ADJ_TAI && txc->constant > 0) | 
|  | 438 | time_tai = txc->constant; | 
|  | 439 |  | 
|  | 440 | if (txc->modes & ADJ_OFFSET) | 
|  | 441 | ntp_update_offset(txc->offset); | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 442 |  | 
| Ingo Molnar | 80f2257 | 2009-02-22 15:15:32 +0100 | [diff] [blame] | 443 | if (txc->modes & ADJ_TICK) | 
|  | 444 | tick_usec = txc->tick; | 
|  | 445 |  | 
|  | 446 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) | 
|  | 447 | ntp_update_frequency(); | 
|  | 448 | } | 
|  | 449 |  | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 450 | /* | 
|  | 451 | * adjtimex mainly allows reading (and writing, if superuser) of | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 452 | * kernel time-keeping variables. used by xntpd. | 
|  | 453 | */ | 
|  | 454 | int do_adjtimex(struct timex *txc) | 
|  | 455 | { | 
| Roman Zippel | eea83d8 | 2008-05-01 04:34:33 -0700 | [diff] [blame] | 456 | struct timespec ts; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 457 | int result; | 
|  | 458 |  | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 459 | /* Validate the data before disabling interrupts */ | 
|  | 460 | if (txc->modes & ADJ_ADJTIME) { | 
| Roman Zippel | eea83d8 | 2008-05-01 04:34:33 -0700 | [diff] [blame] | 461 | /* singleshot must not be used with any other mode bits */ | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 462 | if (!(txc->modes & ADJ_OFFSET_SINGLESHOT)) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 463 | return -EINVAL; | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 464 | if (!(txc->modes & ADJ_OFFSET_READONLY) && | 
|  | 465 | !capable(CAP_SYS_TIME)) | 
|  | 466 | return -EPERM; | 
|  | 467 | } else { | 
|  | 468 | /* In order to modify anything, you gotta be super-user! */ | 
|  | 469 | if (txc->modes && !capable(CAP_SYS_TIME)) | 
|  | 470 | return -EPERM; | 
|  | 471 |  | 
| Ingo Molnar | 53bbfa9 | 2008-02-20 07:58:42 +0100 | [diff] [blame] | 472 | /* | 
|  | 473 | * if the quartz is off by more than 10% then | 
|  | 474 | * something is VERY wrong! | 
|  | 475 | */ | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 476 | if (txc->modes & ADJ_TICK && | 
|  | 477 | (txc->tick <  900000/USER_HZ || | 
|  | 478 | txc->tick > 1100000/USER_HZ)) | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 479 | return -EINVAL; | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 480 |  | 
|  | 481 | if (txc->modes & ADJ_STATUS && time_state != TIME_OK) | 
|  | 482 | hrtimer_cancel(&leap_timer); | 
| John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 483 | } | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 484 |  | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 485 | getnstimeofday(&ts); | 
|  | 486 |  | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 487 | write_seqlock_irq(&xtime_lock); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 488 |  | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 489 | if (txc->modes & ADJ_ADJTIME) { | 
|  | 490 | long save_adjust = time_adjust; | 
|  | 491 |  | 
|  | 492 | if (!(txc->modes & ADJ_OFFSET_READONLY)) { | 
|  | 493 | /* adjtime() is independent from ntp_adjtime() */ | 
|  | 494 | time_adjust = txc->offset; | 
|  | 495 | ntp_update_frequency(); | 
|  | 496 | } | 
|  | 497 | txc->offset = save_adjust; | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 498 | } else { | 
|  | 499 |  | 
|  | 500 | /* If there are input parameters, then process them: */ | 
|  | 501 | if (txc->modes) | 
|  | 502 | process_adjtimex_modes(txc, &ts); | 
|  | 503 |  | 
|  | 504 | txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ, | 
|  | 505 | NTP_SCALE_SHIFT); | 
|  | 506 | if (!(time_status & STA_NANO)) | 
|  | 507 | txc->offset /= NSEC_PER_USEC; | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 508 | } | 
| Roman Zippel | 916c7a8 | 2008-08-20 16:46:08 -0700 | [diff] [blame] | 509 |  | 
| Roman Zippel | eea83d8 | 2008-05-01 04:34:33 -0700 | [diff] [blame] | 510 | result = time_state;	/* mostly `TIME_OK' */ | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 511 | if (time_status & (STA_UNSYNC|STA_CLOCKERR)) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 512 | result = TIME_ERROR; | 
|  | 513 |  | 
| Roman Zippel | d40e944 | 2008-09-22 14:42:44 -0700 | [diff] [blame] | 514 | txc->freq	   = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) * | 
| Ingo Molnar | 2b9d149 | 2009-02-22 15:48:43 +0100 | [diff] [blame] | 515 | PPM_SCALE_INV, NTP_SCALE_SHIFT); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 516 | txc->maxerror	   = time_maxerror; | 
|  | 517 | txc->esterror	   = time_esterror; | 
|  | 518 | txc->status	   = time_status; | 
|  | 519 | txc->constant	   = time_constant; | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 520 | txc->precision	   = 1; | 
| Roman Zippel | 074b3b8 | 2008-05-01 04:34:34 -0700 | [diff] [blame] | 521 | txc->tolerance	   = MAXFREQ_SCALED / PPM_SCALE; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 522 | txc->tick	   = tick_usec; | 
| Roman Zippel | 153b5d0 | 2008-05-01 04:34:37 -0700 | [diff] [blame] | 523 | txc->tai	   = time_tai; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 524 |  | 
|  | 525 | /* PPS is not implemented, so these are zero */ | 
|  | 526 | txc->ppsfreq	   = 0; | 
|  | 527 | txc->jitter	   = 0; | 
|  | 528 | txc->shift	   = 0; | 
|  | 529 | txc->stabil	   = 0; | 
|  | 530 | txc->jitcnt	   = 0; | 
|  | 531 | txc->calcnt	   = 0; | 
|  | 532 | txc->errcnt	   = 0; | 
|  | 533 | txc->stbcnt	   = 0; | 
| Ingo Molnar | e962916 | 2009-02-22 15:35:18 +0100 | [diff] [blame] | 534 |  | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 535 | write_sequnlock_irq(&xtime_lock); | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 536 |  | 
| Roman Zippel | eea83d8 | 2008-05-01 04:34:33 -0700 | [diff] [blame] | 537 | txc->time.tv_sec = ts.tv_sec; | 
|  | 538 | txc->time.tv_usec = ts.tv_nsec; | 
|  | 539 | if (!(time_status & STA_NANO)) | 
|  | 540 | txc->time.tv_usec /= NSEC_PER_USEC; | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 541 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 542 | notify_cmos_timer(); | 
| Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame] | 543 |  | 
|  | 544 | return result; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 545 | } | 
| Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 546 |  | 
|  | 547 | static int __init ntp_tick_adj_setup(char *str) | 
|  | 548 | { | 
|  | 549 | ntp_tick_adj = simple_strtol(str, NULL, 0); | 
| Ingo Molnar | 069569e | 2009-02-22 16:03:37 +0100 | [diff] [blame] | 550 | ntp_tick_adj <<= NTP_SCALE_SHIFT; | 
|  | 551 |  | 
| Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 552 | return 1; | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | __setup("ntp_tick_adj=", ntp_tick_adj_setup); | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 556 |  | 
|  | 557 | void __init ntp_init(void) | 
|  | 558 | { | 
|  | 559 | ntp_clear(); | 
|  | 560 | hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); | 
|  | 561 | leap_timer.function = ntp_leap_second; | 
|  | 562 | } |