john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/time/ntp.c |
| 3 | * |
| 4 | * NTP state machine interfaces and logic. |
| 5 | * |
| 6 | * This code was mainly moved from kernel/timer.c and kernel/time.c |
| 7 | * Please see those files for relevant copyright info and historical |
| 8 | * changelogs. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/time.h> |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 13 | #include <linux/timer.h> |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 14 | #include <linux/timex.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 15 | #include <linux/jiffies.h> |
| 16 | #include <linux/hrtimer.h> |
Alexey Dobriyan | aa0ac36 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 17 | #include <linux/capability.h> |
Roman Zippel | 71abb3a | 2008-05-01 04:34:26 -0700 | [diff] [blame] | 18 | #include <linux/math64.h> |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 19 | #include <asm/timex.h> |
| 20 | |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 21 | /* |
| 22 | * Timekeeping variables |
| 23 | */ |
| 24 | unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */ |
| 25 | unsigned long tick_nsec; /* ACTHZ period (nsec) */ |
| 26 | static u64 tick_length, tick_length_base; |
| 27 | |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 28 | #define MAX_TICKADJ 500 /* microsecs */ |
| 29 | #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \ |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 30 | TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ) |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * phase-lock loop variables |
| 34 | */ |
| 35 | /* TIME_ERROR prevents overwriting the CMOS clock */ |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 36 | static int time_state = TIME_OK; /* clock synchronization status */ |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 37 | int time_status = STA_UNSYNC; /* clock status bits */ |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 38 | static s64 time_offset; /* time adjustment (ns) */ |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 39 | static long time_constant = 2; /* pll time constant */ |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 40 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ |
| 41 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ |
Roman Zippel | dc6a43e | 2006-09-30 23:28:24 -0700 | [diff] [blame] | 42 | long time_freq; /* frequency offset (scaled ppm)*/ |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 43 | static long time_reftime; /* time at last adjustment (s) */ |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 44 | long time_adjust; |
Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 45 | static long ntp_tick_adj; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 46 | |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 47 | static void ntp_update_frequency(void) |
| 48 | { |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 49 | u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) |
| 50 | << TICK_LENGTH_SHIFT; |
Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 51 | second_length += (s64)ntp_tick_adj << TICK_LENGTH_SHIFT; |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 52 | second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC); |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 53 | |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 54 | tick_length_base = second_length; |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 55 | |
Roman Zippel | 71abb3a | 2008-05-01 04:34:26 -0700 | [diff] [blame] | 56 | tick_nsec = div_u64(second_length, HZ) >> TICK_LENGTH_SHIFT; |
| 57 | tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ); |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 60 | static void ntp_update_offset(long offset) |
| 61 | { |
| 62 | long mtemp; |
| 63 | s64 freq_adj; |
| 64 | |
| 65 | if (!(time_status & STA_PLL)) |
| 66 | return; |
| 67 | |
| 68 | time_offset = offset * NSEC_PER_USEC; |
| 69 | |
| 70 | /* |
| 71 | * Scale the phase adjustment and |
| 72 | * clamp to the operating range. |
| 73 | */ |
| 74 | time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC); |
| 75 | time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC); |
| 76 | |
| 77 | /* |
| 78 | * Select how the frequency is to be controlled |
| 79 | * and in which mode (PLL or FLL). |
| 80 | */ |
| 81 | if (time_status & STA_FREQHOLD || time_reftime == 0) |
| 82 | time_reftime = xtime.tv_sec; |
| 83 | mtemp = xtime.tv_sec - time_reftime; |
| 84 | time_reftime = xtime.tv_sec; |
| 85 | |
| 86 | freq_adj = time_offset * mtemp; |
| 87 | freq_adj = shift_right(freq_adj, time_constant * 2 + |
| 88 | (SHIFT_PLL + 2) * 2 - SHIFT_NSEC); |
| 89 | if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) |
| 90 | freq_adj += div_s64(time_offset << (SHIFT_NSEC - SHIFT_FLL), mtemp); |
| 91 | freq_adj += time_freq; |
| 92 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); |
| 93 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); |
| 94 | time_offset = div_s64(time_offset, NTP_INTERVAL_FREQ); |
| 95 | time_offset <<= SHIFT_UPDATE; |
| 96 | } |
| 97 | |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 98 | /** |
| 99 | * ntp_clear - Clears the NTP state variables |
| 100 | * |
| 101 | * Must be called while holding a write on the xtime_lock |
| 102 | */ |
| 103 | void ntp_clear(void) |
| 104 | { |
| 105 | time_adjust = 0; /* stop active adjtime() */ |
| 106 | time_status |= STA_UNSYNC; |
| 107 | time_maxerror = NTP_PHASE_LIMIT; |
| 108 | time_esterror = NTP_PHASE_LIMIT; |
| 109 | |
| 110 | ntp_update_frequency(); |
| 111 | |
| 112 | tick_length = tick_length_base; |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 113 | time_offset = 0; |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 114 | } |
| 115 | |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 116 | /* |
| 117 | * this routine handles the overflow of the microsecond field |
| 118 | * |
| 119 | * The tricky bits of code to handle the accurate clock support |
| 120 | * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. |
| 121 | * They were originally developed for SUN and DEC kernels. |
| 122 | * All the kudos should go to Dave for this stuff. |
| 123 | */ |
| 124 | void second_overflow(void) |
| 125 | { |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 126 | long time_adj; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 127 | |
| 128 | /* Bump the maxerror field */ |
Roman Zippel | 97eebe1 | 2006-09-30 23:28:26 -0700 | [diff] [blame] | 129 | time_maxerror += MAXFREQ >> SHIFT_USEC; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 130 | if (time_maxerror > NTP_PHASE_LIMIT) { |
| 131 | time_maxerror = NTP_PHASE_LIMIT; |
| 132 | time_status |= STA_UNSYNC; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Leap second processing. If in leap-insert state at the end of the |
| 137 | * day, the system clock is set back one second; if in leap-delete |
| 138 | * state, the system clock is set ahead one second. The microtime() |
| 139 | * routine or external clock driver will insure that reported time is |
| 140 | * always monotonic. The ugly divides should be replaced. |
| 141 | */ |
| 142 | switch (time_state) { |
| 143 | case TIME_OK: |
| 144 | if (time_status & STA_INS) |
| 145 | time_state = TIME_INS; |
| 146 | else if (time_status & STA_DEL) |
| 147 | time_state = TIME_DEL; |
| 148 | break; |
| 149 | case TIME_INS: |
| 150 | if (xtime.tv_sec % 86400 == 0) { |
| 151 | xtime.tv_sec--; |
| 152 | wall_to_monotonic.tv_sec++; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 153 | time_state = TIME_OOP; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 154 | printk(KERN_NOTICE "Clock: inserting leap second " |
| 155 | "23:59:60 UTC\n"); |
| 156 | } |
| 157 | break; |
| 158 | case TIME_DEL: |
| 159 | if ((xtime.tv_sec + 1) % 86400 == 0) { |
| 160 | xtime.tv_sec++; |
| 161 | wall_to_monotonic.tv_sec--; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 162 | time_state = TIME_WAIT; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 163 | printk(KERN_NOTICE "Clock: deleting leap second " |
| 164 | "23:59:59 UTC\n"); |
| 165 | } |
| 166 | break; |
| 167 | case TIME_OOP: |
| 168 | time_state = TIME_WAIT; |
| 169 | break; |
| 170 | case TIME_WAIT: |
| 171 | if (!(time_status & (STA_INS | STA_DEL))) |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 172 | time_state = TIME_OK; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 176 | * Compute the phase adjustment for the next second. The offset is |
| 177 | * reduced by a fixed factor times the time constant. |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 178 | */ |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 179 | tick_length = tick_length_base; |
Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 180 | time_adj = shift_right(time_offset, SHIFT_PLL + time_constant); |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 181 | time_offset -= time_adj; |
| 182 | tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 183 | |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 184 | if (unlikely(time_adjust)) { |
| 185 | if (time_adjust > MAX_TICKADJ) { |
| 186 | time_adjust -= MAX_TICKADJ; |
| 187 | tick_length += MAX_TICKADJ_SCALED; |
| 188 | } else if (time_adjust < -MAX_TICKADJ) { |
| 189 | time_adjust += MAX_TICKADJ; |
| 190 | tick_length -= MAX_TICKADJ_SCALED; |
| 191 | } else { |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 192 | tick_length += (s64)(time_adjust * NSEC_PER_USEC / |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 193 | NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT; |
Jim Houston | bb1d860 | 2006-10-28 10:38:56 -0700 | [diff] [blame] | 194 | time_adjust = 0; |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 195 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Return how long ticks are at the moment, that is, how much time |
| 201 | * update_wall_time_one_tick will add to xtime next time we call it |
| 202 | * (assuming no calls to do_adjtimex in the meantime). |
| 203 | * The return value is in fixed-point nanoseconds shifted by the |
| 204 | * specified number of bits to the right of the binary point. |
| 205 | * This function has no side-effects. |
| 206 | */ |
| 207 | u64 current_tick_length(void) |
| 208 | { |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 209 | return tick_length; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 212 | #ifdef CONFIG_GENERIC_CMOS_UPDATE |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 213 | |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 214 | /* Disable the cmos update - used by virtualization and embedded */ |
| 215 | int no_sync_cmos_clock __read_mostly; |
| 216 | |
| 217 | static void sync_cmos_clock(unsigned long dummy); |
| 218 | |
| 219 | static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0); |
| 220 | |
| 221 | static void sync_cmos_clock(unsigned long dummy) |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 222 | { |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 223 | struct timespec now, next; |
| 224 | int fail = 1; |
| 225 | |
| 226 | /* |
| 227 | * If we have an externally synchronized Linux clock, then update |
| 228 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be |
| 229 | * called as close as possible to 500 ms before the new second starts. |
| 230 | * This code is run on a timer. If the clock is set, that timer |
| 231 | * may not expire at the correct time. Thus, we adjust... |
| 232 | */ |
| 233 | if (!ntp_synced()) |
| 234 | /* |
| 235 | * Not synced, exit, do not restart a timer (if one is |
| 236 | * running, let it run out). |
| 237 | */ |
| 238 | return; |
| 239 | |
| 240 | getnstimeofday(&now); |
David P. Reed | fa6a1a5 | 2007-11-14 17:49:21 -0500 | [diff] [blame] | 241 | if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2) |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 242 | fail = update_persistent_clock(now); |
| 243 | |
| 244 | next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec; |
| 245 | if (next.tv_nsec <= 0) |
| 246 | next.tv_nsec += NSEC_PER_SEC; |
| 247 | |
| 248 | if (!fail) |
| 249 | next.tv_sec = 659; |
| 250 | else |
| 251 | next.tv_sec = 0; |
| 252 | |
| 253 | if (next.tv_nsec >= NSEC_PER_SEC) { |
| 254 | next.tv_sec++; |
| 255 | next.tv_nsec -= NSEC_PER_SEC; |
| 256 | } |
| 257 | mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next)); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 260 | static void notify_cmos_timer(void) |
| 261 | { |
Tony Breeds | 298a5df | 2007-09-11 15:24:03 -0700 | [diff] [blame] | 262 | if (!no_sync_cmos_clock) |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 263 | mod_timer(&sync_cmos_timer, jiffies + 1); |
| 264 | } |
| 265 | |
| 266 | #else |
| 267 | static inline void notify_cmos_timer(void) { } |
| 268 | #endif |
| 269 | |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 270 | /* adjtimex mainly allows reading (and writing, if superuser) of |
| 271 | * kernel time-keeping variables. used by xntpd. |
| 272 | */ |
| 273 | int do_adjtimex(struct timex *txc) |
| 274 | { |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 275 | long save_adjust; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 276 | int result; |
| 277 | |
| 278 | /* In order to modify anything, you gotta be super-user! */ |
| 279 | if (txc->modes && !capable(CAP_SYS_TIME)) |
| 280 | return -EPERM; |
| 281 | |
| 282 | /* Now we validate the data before disabling interrupts */ |
| 283 | |
John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 284 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) { |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 285 | /* singleshot must not be used with any other mode bits */ |
John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 286 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && |
| 287 | txc->modes != ADJ_OFFSET_SS_READ) |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 288 | return -EINVAL; |
John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 289 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 290 | |
| 291 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET)) |
| 292 | /* adjustment Offset limited to +- .512 seconds */ |
| 293 | if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE ) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | /* if the quartz is off by more than 10% something is VERY wrong ! */ |
| 297 | if (txc->modes & ADJ_TICK) |
| 298 | if (txc->tick < 900000/USER_HZ || |
| 299 | txc->tick > 1100000/USER_HZ) |
| 300 | return -EINVAL; |
| 301 | |
| 302 | write_seqlock_irq(&xtime_lock); |
| 303 | result = time_state; /* mostly `TIME_OK' */ |
| 304 | |
| 305 | /* Save for later - semantics of adjtime is to return old value */ |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 306 | save_adjust = time_adjust; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 307 | |
| 308 | #if 0 /* STA_CLOCKERR is never set yet */ |
| 309 | time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */ |
| 310 | #endif |
| 311 | /* If there are input parameters, then process them */ |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 312 | if (txc->modes) { |
| 313 | if (txc->modes & ADJ_STATUS) /* only set allowed bits */ |
| 314 | time_status = (txc->status & ~STA_RONLY) | |
| 315 | (time_status & STA_RONLY); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 316 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 317 | if (txc->modes & ADJ_FREQUENCY) { |
| 318 | if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) { |
| 319 | result = -EINVAL; |
| 320 | goto leave; |
| 321 | } |
| 322 | time_freq = ((s64)txc->freq * NSEC_PER_USEC) |
| 323 | >> (SHIFT_USEC - SHIFT_NSEC); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 324 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 325 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 326 | if (txc->modes & ADJ_MAXERROR) { |
| 327 | if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) { |
| 328 | result = -EINVAL; |
| 329 | goto leave; |
| 330 | } |
| 331 | time_maxerror = txc->maxerror; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 332 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 333 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 334 | if (txc->modes & ADJ_ESTERROR) { |
| 335 | if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) { |
| 336 | result = -EINVAL; |
| 337 | goto leave; |
| 338 | } |
| 339 | time_esterror = txc->esterror; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 340 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 341 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 342 | if (txc->modes & ADJ_TIMECONST) { |
| 343 | if (txc->constant < 0) { /* NTP v4 uses values > 6 */ |
| 344 | result = -EINVAL; |
| 345 | goto leave; |
| 346 | } |
| 347 | time_constant = min(txc->constant + 4, (long)MAXTC); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 348 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 349 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 350 | if (txc->modes & ADJ_OFFSET) { |
| 351 | if (txc->modes == ADJ_OFFSET_SINGLESHOT) |
| 352 | /* adjtime() is independent from ntp_adjtime() */ |
| 353 | time_adjust = txc->offset; |
| 354 | else |
| 355 | ntp_update_offset(txc->offset); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 356 | } |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 357 | if (txc->modes & ADJ_TICK) |
| 358 | tick_usec = txc->tick; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 359 | |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 360 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) |
| 361 | ntp_update_frequency(); |
| 362 | } |
| 363 | leave: |
| 364 | if (time_status & (STA_UNSYNC|STA_CLOCKERR)) |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 365 | result = TIME_ERROR; |
| 366 | |
John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 367 | if ((txc->modes == ADJ_OFFSET_SINGLESHOT) || |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 368 | (txc->modes == ADJ_OFFSET_SS_READ)) |
john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 369 | txc->offset = save_adjust; |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 370 | else |
john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 371 | txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) * |
| 372 | NTP_INTERVAL_FREQ / 1000; |
| 373 | txc->freq = (time_freq / NSEC_PER_USEC) << |
| 374 | (SHIFT_USEC - SHIFT_NSEC); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 375 | txc->maxerror = time_maxerror; |
| 376 | txc->esterror = time_esterror; |
| 377 | txc->status = time_status; |
| 378 | txc->constant = time_constant; |
Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 379 | txc->precision = 1; |
Roman Zippel | 97eebe1 | 2006-09-30 23:28:26 -0700 | [diff] [blame] | 380 | txc->tolerance = MAXFREQ; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 381 | txc->tick = tick_usec; |
| 382 | |
| 383 | /* PPS is not implemented, so these are zero */ |
| 384 | txc->ppsfreq = 0; |
| 385 | txc->jitter = 0; |
| 386 | txc->shift = 0; |
| 387 | txc->stabil = 0; |
| 388 | txc->jitcnt = 0; |
| 389 | txc->calcnt = 0; |
| 390 | txc->errcnt = 0; |
| 391 | txc->stbcnt = 0; |
| 392 | write_sequnlock_irq(&xtime_lock); |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 393 | |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 394 | do_gettimeofday(&txc->time); |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 395 | |
Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 396 | notify_cmos_timer(); |
Roman Zippel | ee9851b | 2008-05-01 04:34:32 -0700 | [diff] [blame^] | 397 | |
| 398 | return result; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 399 | } |
Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 400 | |
| 401 | static int __init ntp_tick_adj_setup(char *str) |
| 402 | { |
| 403 | ntp_tick_adj = simple_strtol(str, NULL, 0); |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | __setup("ntp_tick_adj=", ntp_tick_adj_setup); |