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> |
| 13 | #include <linux/timex.h> |
| 14 | |
| 15 | #include <asm/div64.h> |
| 16 | #include <asm/timex.h> |
| 17 | |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 18 | /* |
| 19 | * Timekeeping variables |
| 20 | */ |
| 21 | unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */ |
| 22 | unsigned long tick_nsec; /* ACTHZ period (nsec) */ |
| 23 | static u64 tick_length, tick_length_base; |
| 24 | |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 25 | #define MAX_TICKADJ 500 /* microsecs */ |
| 26 | #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \ |
| 27 | TICK_LENGTH_SHIFT) / HZ) |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * phase-lock loop variables |
| 31 | */ |
| 32 | /* TIME_ERROR prevents overwriting the CMOS clock */ |
| 33 | int time_state = TIME_OK; /* clock synchronization status */ |
| 34 | int time_status = STA_UNSYNC; /* clock status bits */ |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 35 | long time_offset; /* time adjustment (ns) */ |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 36 | long time_constant = 2; /* pll time constant */ |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 37 | long time_precision = 1; /* clock precision (us) */ |
| 38 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ |
| 39 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ |
Roman Zippel | dc6a43e | 2006-09-30 23:28:24 -0700 | [diff] [blame] | 40 | long time_freq; /* frequency offset (scaled ppm)*/ |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 41 | long time_reftime; /* time at last adjustment (s) */ |
| 42 | long time_adjust; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 43 | |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 44 | /** |
| 45 | * ntp_clear - Clears the NTP state variables |
| 46 | * |
| 47 | * Must be called while holding a write on the xtime_lock |
| 48 | */ |
| 49 | void ntp_clear(void) |
| 50 | { |
| 51 | time_adjust = 0; /* stop active adjtime() */ |
| 52 | time_status |= STA_UNSYNC; |
| 53 | time_maxerror = NTP_PHASE_LIMIT; |
| 54 | time_esterror = NTP_PHASE_LIMIT; |
| 55 | |
| 56 | ntp_update_frequency(); |
| 57 | |
| 58 | tick_length = tick_length_base; |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 59 | time_offset = 0; |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE) |
| 63 | #define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / (s64)CLOCK_TICK_RATE) |
| 64 | |
| 65 | void ntp_update_frequency(void) |
| 66 | { |
| 67 | tick_length_base = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << TICK_LENGTH_SHIFT; |
| 68 | tick_length_base += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT; |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 69 | tick_length_base += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC); |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 70 | |
| 71 | do_div(tick_length_base, HZ); |
| 72 | |
| 73 | tick_nsec = tick_length_base >> TICK_LENGTH_SHIFT; |
| 74 | } |
| 75 | |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 76 | /* |
| 77 | * this routine handles the overflow of the microsecond field |
| 78 | * |
| 79 | * The tricky bits of code to handle the accurate clock support |
| 80 | * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. |
| 81 | * They were originally developed for SUN and DEC kernels. |
| 82 | * All the kudos should go to Dave for this stuff. |
| 83 | */ |
| 84 | void second_overflow(void) |
| 85 | { |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 86 | long time_adj; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 87 | |
| 88 | /* Bump the maxerror field */ |
Roman Zippel | 97eebe1 | 2006-09-30 23:28:26 -0700 | [diff] [blame] | 89 | time_maxerror += MAXFREQ >> SHIFT_USEC; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 90 | if (time_maxerror > NTP_PHASE_LIMIT) { |
| 91 | time_maxerror = NTP_PHASE_LIMIT; |
| 92 | time_status |= STA_UNSYNC; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Leap second processing. If in leap-insert state at the end of the |
| 97 | * day, the system clock is set back one second; if in leap-delete |
| 98 | * state, the system clock is set ahead one second. The microtime() |
| 99 | * routine or external clock driver will insure that reported time is |
| 100 | * always monotonic. The ugly divides should be replaced. |
| 101 | */ |
| 102 | switch (time_state) { |
| 103 | case TIME_OK: |
| 104 | if (time_status & STA_INS) |
| 105 | time_state = TIME_INS; |
| 106 | else if (time_status & STA_DEL) |
| 107 | time_state = TIME_DEL; |
| 108 | break; |
| 109 | case TIME_INS: |
| 110 | if (xtime.tv_sec % 86400 == 0) { |
| 111 | xtime.tv_sec--; |
| 112 | wall_to_monotonic.tv_sec++; |
| 113 | /* |
| 114 | * The timer interpolator will make time change |
| 115 | * gradually instead of an immediate jump by one second |
| 116 | */ |
| 117 | time_interpolator_update(-NSEC_PER_SEC); |
| 118 | time_state = TIME_OOP; |
| 119 | clock_was_set(); |
| 120 | printk(KERN_NOTICE "Clock: inserting leap second " |
| 121 | "23:59:60 UTC\n"); |
| 122 | } |
| 123 | break; |
| 124 | case TIME_DEL: |
| 125 | if ((xtime.tv_sec + 1) % 86400 == 0) { |
| 126 | xtime.tv_sec++; |
| 127 | wall_to_monotonic.tv_sec--; |
| 128 | /* |
| 129 | * Use of time interpolator for a gradual change of |
| 130 | * time |
| 131 | */ |
| 132 | time_interpolator_update(NSEC_PER_SEC); |
| 133 | time_state = TIME_WAIT; |
| 134 | clock_was_set(); |
| 135 | printk(KERN_NOTICE "Clock: deleting leap second " |
| 136 | "23:59:59 UTC\n"); |
| 137 | } |
| 138 | break; |
| 139 | case TIME_OOP: |
| 140 | time_state = TIME_WAIT; |
| 141 | break; |
| 142 | case TIME_WAIT: |
| 143 | if (!(time_status & (STA_INS | STA_DEL))) |
| 144 | time_state = TIME_OK; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Compute the phase adjustment for the next second. In PLL mode, the |
| 149 | * offset is reduced by a fixed factor times the time constant. In FLL |
| 150 | * mode the offset is used directly. In either mode, the maximum phase |
| 151 | * adjustment for each second is clamped so as to spread the adjustment |
| 152 | * over not more than the number of seconds between updates. |
| 153 | */ |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 154 | tick_length = tick_length_base; |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 155 | time_adj = time_offset; |
| 156 | if (!(time_status & STA_FLL)) |
| 157 | time_adj = shift_right(time_adj, SHIFT_KG + time_constant); |
| 158 | time_adj = min(time_adj, -((MAXPHASE / HZ) << SHIFT_UPDATE) / MINSEC); |
| 159 | time_adj = max(time_adj, ((MAXPHASE / HZ) << SHIFT_UPDATE) / MINSEC); |
| 160 | time_offset -= time_adj; |
| 161 | tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 162 | |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 163 | if (unlikely(time_adjust)) { |
| 164 | if (time_adjust > MAX_TICKADJ) { |
| 165 | time_adjust -= MAX_TICKADJ; |
| 166 | tick_length += MAX_TICKADJ_SCALED; |
| 167 | } else if (time_adjust < -MAX_TICKADJ) { |
| 168 | time_adjust += MAX_TICKADJ; |
| 169 | tick_length -= MAX_TICKADJ_SCALED; |
| 170 | } else { |
| 171 | time_adjust = 0; |
| 172 | tick_length += (s64)(time_adjust * NSEC_PER_USEC / |
| 173 | HZ) << TICK_LENGTH_SHIFT; |
| 174 | } |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Return how long ticks are at the moment, that is, how much time |
| 180 | * update_wall_time_one_tick will add to xtime next time we call it |
| 181 | * (assuming no calls to do_adjtimex in the meantime). |
| 182 | * The return value is in fixed-point nanoseconds shifted by the |
| 183 | * specified number of bits to the right of the binary point. |
| 184 | * This function has no side-effects. |
| 185 | */ |
| 186 | u64 current_tick_length(void) |
| 187 | { |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 188 | return tick_length; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | void __attribute__ ((weak)) notify_arch_cmos_timer(void) |
| 193 | { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | /* adjtimex mainly allows reading (and writing, if superuser) of |
| 198 | * kernel time-keeping variables. used by xntpd. |
| 199 | */ |
| 200 | int do_adjtimex(struct timex *txc) |
| 201 | { |
| 202 | long ltemp, mtemp, save_adjust; |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 203 | s64 freq_adj; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 204 | int result; |
| 205 | |
| 206 | /* In order to modify anything, you gotta be super-user! */ |
| 207 | if (txc->modes && !capable(CAP_SYS_TIME)) |
| 208 | return -EPERM; |
| 209 | |
| 210 | /* Now we validate the data before disabling interrupts */ |
| 211 | |
| 212 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) |
| 213 | /* singleshot must not be used with any other mode bits */ |
| 214 | if (txc->modes != ADJ_OFFSET_SINGLESHOT) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET)) |
| 218 | /* adjustment Offset limited to +- .512 seconds */ |
| 219 | if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE ) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | /* if the quartz is off by more than 10% something is VERY wrong ! */ |
| 223 | if (txc->modes & ADJ_TICK) |
| 224 | if (txc->tick < 900000/USER_HZ || |
| 225 | txc->tick > 1100000/USER_HZ) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | write_seqlock_irq(&xtime_lock); |
| 229 | result = time_state; /* mostly `TIME_OK' */ |
| 230 | |
| 231 | /* Save for later - semantics of adjtime is to return old value */ |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 232 | save_adjust = time_adjust; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 233 | |
| 234 | #if 0 /* STA_CLOCKERR is never set yet */ |
| 235 | time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */ |
| 236 | #endif |
| 237 | /* If there are input parameters, then process them */ |
| 238 | if (txc->modes) |
| 239 | { |
| 240 | if (txc->modes & ADJ_STATUS) /* only set allowed bits */ |
| 241 | time_status = (txc->status & ~STA_RONLY) | |
| 242 | (time_status & STA_RONLY); |
| 243 | |
| 244 | if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */ |
| 245 | if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) { |
| 246 | result = -EINVAL; |
| 247 | goto leave; |
| 248 | } |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 249 | time_freq = ((s64)txc->freq * NSEC_PER_USEC) >> (SHIFT_USEC - SHIFT_NSEC); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (txc->modes & ADJ_MAXERROR) { |
| 253 | if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) { |
| 254 | result = -EINVAL; |
| 255 | goto leave; |
| 256 | } |
| 257 | time_maxerror = txc->maxerror; |
| 258 | } |
| 259 | |
| 260 | if (txc->modes & ADJ_ESTERROR) { |
| 261 | if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) { |
| 262 | result = -EINVAL; |
| 263 | goto leave; |
| 264 | } |
| 265 | time_esterror = txc->esterror; |
| 266 | } |
| 267 | |
| 268 | if (txc->modes & ADJ_TIMECONST) { /* p. 24 */ |
| 269 | if (txc->constant < 0) { /* NTP v4 uses values > 6 */ |
| 270 | result = -EINVAL; |
| 271 | goto leave; |
| 272 | } |
| 273 | time_constant = txc->constant; |
| 274 | } |
| 275 | |
| 276 | if (txc->modes & ADJ_OFFSET) { /* values checked earlier */ |
| 277 | if (txc->modes == ADJ_OFFSET_SINGLESHOT) { |
| 278 | /* adjtime() is independent from ntp_adjtime() */ |
Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 279 | time_adjust = txc->offset; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 280 | } |
| 281 | else if (time_status & STA_PLL) { |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 282 | ltemp = txc->offset * NSEC_PER_USEC; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Scale the phase adjustment and |
| 286 | * clamp to the operating range. |
| 287 | */ |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 288 | time_offset = min(ltemp, MAXPHASE * NSEC_PER_USEC); |
| 289 | time_offset = max(time_offset, -MAXPHASE * NSEC_PER_USEC); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * Select whether the frequency is to be controlled |
| 293 | * and in which mode (PLL or FLL). Clamp to the operating |
| 294 | * range. Ugly multiply/divide should be replaced someday. |
| 295 | */ |
| 296 | |
| 297 | if (time_status & STA_FREQHOLD || time_reftime == 0) |
| 298 | time_reftime = xtime.tv_sec; |
| 299 | mtemp = xtime.tv_sec - time_reftime; |
| 300 | time_reftime = xtime.tv_sec; |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 301 | freq_adj = 0; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 302 | if (time_status & STA_FLL) { |
| 303 | if (mtemp >= MINSEC) { |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 304 | freq_adj = (s64)time_offset << (SHIFT_NSEC - SHIFT_KH); |
| 305 | if (time_offset < 0) { |
| 306 | freq_adj = -freq_adj; |
| 307 | do_div(freq_adj, mtemp); |
| 308 | freq_adj = -freq_adj; |
| 309 | } else |
| 310 | do_div(freq_adj, mtemp); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 311 | } else /* calibration interval too short (p. 12) */ |
| 312 | result = TIME_ERROR; |
| 313 | } else { /* PLL mode */ |
| 314 | if (mtemp < MAXSEC) { |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 315 | freq_adj = (s64)ltemp * mtemp; |
| 316 | freq_adj = shift_right(freq_adj,(time_constant + |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 317 | time_constant + |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 318 | SHIFT_KF - SHIFT_NSEC)); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 319 | } else /* calibration interval too long (p. 12) */ |
| 320 | result = TIME_ERROR; |
| 321 | } |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 322 | freq_adj += time_freq; |
| 323 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); |
| 324 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); |
| 325 | time_offset = (time_offset / HZ) << SHIFT_UPDATE; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 326 | } /* STA_PLL */ |
| 327 | } /* txc->modes & ADJ_OFFSET */ |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 328 | if (txc->modes & ADJ_TICK) |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 329 | tick_usec = txc->tick; |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 330 | |
Roman Zippel | dc6a43e | 2006-09-30 23:28:24 -0700 | [diff] [blame] | 331 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) |
Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 332 | ntp_update_frequency(); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 333 | } /* txc->modes */ |
| 334 | leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0) |
| 335 | result = TIME_ERROR; |
| 336 | |
| 337 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) |
| 338 | txc->offset = save_adjust; |
Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 339 | else |
| 340 | txc->offset = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000; |
Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame^] | 341 | txc->freq = (time_freq / NSEC_PER_USEC) << (SHIFT_USEC - SHIFT_NSEC); |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 342 | txc->maxerror = time_maxerror; |
| 343 | txc->esterror = time_esterror; |
| 344 | txc->status = time_status; |
| 345 | txc->constant = time_constant; |
| 346 | txc->precision = time_precision; |
Roman Zippel | 97eebe1 | 2006-09-30 23:28:26 -0700 | [diff] [blame] | 347 | txc->tolerance = MAXFREQ; |
john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 348 | txc->tick = tick_usec; |
| 349 | |
| 350 | /* PPS is not implemented, so these are zero */ |
| 351 | txc->ppsfreq = 0; |
| 352 | txc->jitter = 0; |
| 353 | txc->shift = 0; |
| 354 | txc->stabil = 0; |
| 355 | txc->jitcnt = 0; |
| 356 | txc->calcnt = 0; |
| 357 | txc->errcnt = 0; |
| 358 | txc->stbcnt = 0; |
| 359 | write_sequnlock_irq(&xtime_lock); |
| 360 | do_gettimeofday(&txc->time); |
| 361 | notify_arch_cmos_timer(); |
| 362 | return(result); |
| 363 | } |