diff options
Diffstat (limited to 'kernel/time/ntp.c')
| -rw-r--r-- | kernel/time/ntp.c | 398 |
1 files changed, 218 insertions, 180 deletions
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c index 5fd9b9469770..5125ddd8196b 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c | |||
| @@ -15,7 +15,8 @@ | |||
| 15 | #include <linux/jiffies.h> | 15 | #include <linux/jiffies.h> |
| 16 | #include <linux/hrtimer.h> | 16 | #include <linux/hrtimer.h> |
| 17 | #include <linux/capability.h> | 17 | #include <linux/capability.h> |
| 18 | #include <asm/div64.h> | 18 | #include <linux/math64.h> |
| 19 | #include <linux/clocksource.h> | ||
| 19 | #include <asm/timex.h> | 20 | #include <asm/timex.h> |
| 20 | 21 | ||
| 21 | /* | 22 | /* |
| @@ -23,11 +24,14 @@ | |||
| 23 | */ | 24 | */ |
| 24 | unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */ | 25 | unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */ |
| 25 | unsigned long tick_nsec; /* ACTHZ period (nsec) */ | 26 | unsigned long tick_nsec; /* ACTHZ period (nsec) */ |
| 26 | static u64 tick_length, tick_length_base; | 27 | u64 tick_length; |
| 28 | static u64 tick_length_base; | ||
| 29 | |||
| 30 | static struct hrtimer leap_timer; | ||
| 27 | 31 | ||
| 28 | #define MAX_TICKADJ 500 /* microsecs */ | 32 | #define MAX_TICKADJ 500 /* microsecs */ |
| 29 | #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \ | 33 | #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \ |
| 30 | TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ) | 34 | NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ) |
| 31 | 35 | ||
| 32 | /* | 36 | /* |
| 33 | * phase-lock loop variables | 37 | * phase-lock loop variables |
| @@ -35,11 +39,12 @@ static u64 tick_length, tick_length_base; | |||
| 35 | /* TIME_ERROR prevents overwriting the CMOS clock */ | 39 | /* TIME_ERROR prevents overwriting the CMOS clock */ |
| 36 | static int time_state = TIME_OK; /* clock synchronization status */ | 40 | static int time_state = TIME_OK; /* clock synchronization status */ |
| 37 | int time_status = STA_UNSYNC; /* clock status bits */ | 41 | int time_status = STA_UNSYNC; /* clock status bits */ |
| 38 | static s64 time_offset; /* time adjustment (ns) */ | 42 | static long time_tai; /* TAI offset (s) */ |
| 43 | static s64 time_offset; /* time adjustment (ns) */ | ||
| 39 | static long time_constant = 2; /* pll time constant */ | 44 | static long time_constant = 2; /* pll time constant */ |
| 40 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ | 45 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ |
| 41 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ | 46 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ |
| 42 | long time_freq; /* frequency offset (scaled ppm)*/ | 47 | static s64 time_freq; /* frequency offset (scaled ns/s)*/ |
| 43 | static long time_reftime; /* time at last adjustment (s) */ | 48 | static long time_reftime; /* time at last adjustment (s) */ |
| 44 | long time_adjust; | 49 | long time_adjust; |
| 45 | static long ntp_tick_adj; | 50 | static long ntp_tick_adj; |
| @@ -47,16 +52,56 @@ static long ntp_tick_adj; | |||
| 47 | static void ntp_update_frequency(void) | 52 | static void ntp_update_frequency(void) |
| 48 | { | 53 | { |
| 49 | u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) | 54 | u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) |
| 50 | << TICK_LENGTH_SHIFT; | 55 | << NTP_SCALE_SHIFT; |
| 51 | second_length += (s64)ntp_tick_adj << TICK_LENGTH_SHIFT; | 56 | second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT; |
| 52 | second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC); | 57 | second_length += time_freq; |
| 53 | 58 | ||
| 54 | tick_length_base = second_length; | 59 | tick_length_base = second_length; |
| 55 | 60 | ||
| 56 | do_div(second_length, HZ); | 61 | tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT; |
| 57 | tick_nsec = second_length >> TICK_LENGTH_SHIFT; | 62 | tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ); |
| 63 | } | ||
| 64 | |||
| 65 | static void ntp_update_offset(long offset) | ||
| 66 | { | ||
| 67 | long mtemp; | ||
| 68 | s64 freq_adj; | ||
| 69 | |||
| 70 | if (!(time_status & STA_PLL)) | ||
| 71 | return; | ||
| 58 | 72 | ||
| 59 | do_div(tick_length_base, NTP_INTERVAL_FREQ); | 73 | if (!(time_status & STA_NANO)) |
| 74 | offset *= NSEC_PER_USEC; | ||
| 75 | |||
| 76 | /* | ||
| 77 | * Scale the phase adjustment and | ||
| 78 | * clamp to the operating range. | ||
| 79 | */ | ||
| 80 | offset = min(offset, MAXPHASE); | ||
| 81 | offset = max(offset, -MAXPHASE); | ||
| 82 | |||
| 83 | /* | ||
| 84 | * Select how the frequency is to be controlled | ||
| 85 | * and in which mode (PLL or FLL). | ||
| 86 | */ | ||
| 87 | if (time_status & STA_FREQHOLD || time_reftime == 0) | ||
| 88 | time_reftime = xtime.tv_sec; | ||
| 89 | mtemp = xtime.tv_sec - time_reftime; | ||
| 90 | time_reftime = xtime.tv_sec; | ||
| 91 | |||
| 92 | freq_adj = (s64)offset * mtemp; | ||
| 93 | freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant); | ||
| 94 | time_status &= ~STA_MODE; | ||
| 95 | if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) { | ||
| 96 | freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL), | ||
| 97 | mtemp); | ||
| 98 | time_status |= STA_MODE; | ||
| 99 | } | ||
| 100 | freq_adj += time_freq; | ||
| 101 | freq_adj = min(freq_adj, MAXFREQ_SCALED); | ||
| 102 | time_freq = max(freq_adj, -MAXFREQ_SCALED); | ||
| 103 | |||
| 104 | time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ); | ||
| 60 | } | 105 | } |
| 61 | 106 | ||
| 62 | /** | 107 | /** |
| @@ -78,62 +123,70 @@ void ntp_clear(void) | |||
| 78 | } | 123 | } |
| 79 | 124 | ||
| 80 | /* | 125 | /* |
| 81 | * this routine handles the overflow of the microsecond field | 126 | * Leap second processing. If in leap-insert state at the end of the |
| 82 | * | 127 | * day, the system clock is set back one second; if in leap-delete |
| 83 | * The tricky bits of code to handle the accurate clock support | 128 | * state, the system clock is set ahead one second. |
| 84 | * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. | ||
| 85 | * They were originally developed for SUN and DEC kernels. | ||
| 86 | * All the kudos should go to Dave for this stuff. | ||
| 87 | */ | 129 | */ |
| 88 | void second_overflow(void) | 130 | static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer) |
| 89 | { | 131 | { |
| 90 | long time_adj; | 132 | enum hrtimer_restart res = HRTIMER_NORESTART; |
| 91 | 133 | ||
| 92 | /* Bump the maxerror field */ | 134 | write_seqlock_irq(&xtime_lock); |
| 93 | time_maxerror += MAXFREQ >> SHIFT_USEC; | ||
| 94 | if (time_maxerror > NTP_PHASE_LIMIT) { | ||
| 95 | time_maxerror = NTP_PHASE_LIMIT; | ||
| 96 | time_status |= STA_UNSYNC; | ||
| 97 | } | ||
| 98 | 135 | ||
| 99 | /* | ||
| 100 | * Leap second processing. If in leap-insert state at the end of the | ||
| 101 | * day, the system clock is set back one second; if in leap-delete | ||
| 102 | * state, the system clock is set ahead one second. The microtime() | ||
| 103 | * routine or external clock driver will insure that reported time is | ||
| 104 | * always monotonic. The ugly divides should be replaced. | ||
| 105 | */ | ||
| 106 | switch (time_state) { | 136 | switch (time_state) { |
| 107 | case TIME_OK: | 137 | case TIME_OK: |
| 108 | if (time_status & STA_INS) | ||
| 109 | time_state = TIME_INS; | ||
| 110 | else if (time_status & STA_DEL) | ||
| 111 | time_state = TIME_DEL; | ||
| 112 | break; | 138 | break; |
| 113 | case TIME_INS: | 139 | case TIME_INS: |
| 114 | if (xtime.tv_sec % 86400 == 0) { | 140 | xtime.tv_sec--; |
| 115 | xtime.tv_sec--; | 141 | wall_to_monotonic.tv_sec++; |
| 116 | wall_to_monotonic.tv_sec++; | 142 | time_state = TIME_OOP; |
| 117 | time_state = TIME_OOP; | 143 | printk(KERN_NOTICE "Clock: " |
| 118 | printk(KERN_NOTICE "Clock: inserting leap second " | 144 | "inserting leap second 23:59:60 UTC\n"); |
| 119 | "23:59:60 UTC\n"); | 145 | leap_timer.expires = ktime_add_ns(leap_timer.expires, |
| 120 | } | 146 | NSEC_PER_SEC); |
| 147 | res = HRTIMER_RESTART; | ||
| 121 | break; | 148 | break; |
| 122 | case TIME_DEL: | 149 | case TIME_DEL: |
| 123 | if ((xtime.tv_sec + 1) % 86400 == 0) { | 150 | xtime.tv_sec++; |
| 124 | xtime.tv_sec++; | 151 | time_tai--; |
| 125 | wall_to_monotonic.tv_sec--; | 152 | wall_to_monotonic.tv_sec--; |
| 126 | time_state = TIME_WAIT; | 153 | time_state = TIME_WAIT; |
| 127 | printk(KERN_NOTICE "Clock: deleting leap second " | 154 | printk(KERN_NOTICE "Clock: " |
| 128 | "23:59:59 UTC\n"); | 155 | "deleting leap second 23:59:59 UTC\n"); |
| 129 | } | ||
| 130 | break; | 156 | break; |
| 131 | case TIME_OOP: | 157 | case TIME_OOP: |
| 158 | time_tai++; | ||
| 132 | time_state = TIME_WAIT; | 159 | time_state = TIME_WAIT; |
| 133 | break; | 160 | /* fall through */ |
| 134 | case TIME_WAIT: | 161 | case TIME_WAIT: |
| 135 | if (!(time_status & (STA_INS | STA_DEL))) | 162 | if (!(time_status & (STA_INS | STA_DEL))) |
| 136 | time_state = TIME_OK; | 163 | time_state = TIME_OK; |
| 164 | break; | ||
| 165 | } | ||
| 166 | update_vsyscall(&xtime, clock); | ||
| 167 | |||
| 168 | write_sequnlock_irq(&xtime_lock); | ||
| 169 | |||
| 170 | return res; | ||
| 171 | } | ||
| 172 | |||
| 173 | /* | ||
| 174 | * this routine handles the overflow of the microsecond field | ||
| 175 | * | ||
| 176 | * The tricky bits of code to handle the accurate clock support | ||
| 177 | * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. | ||
| 178 | * They were originally developed for SUN and DEC kernels. | ||
| 179 | * All the kudos should go to Dave for this stuff. | ||
| 180 | */ | ||
| 181 | void second_overflow(void) | ||
| 182 | { | ||
| 183 | s64 time_adj; | ||
| 184 | |||
| 185 | /* Bump the maxerror field */ | ||
| 186 | time_maxerror += MAXFREQ / NSEC_PER_USEC; | ||
| 187 | if (time_maxerror > NTP_PHASE_LIMIT) { | ||
| 188 | time_maxerror = NTP_PHASE_LIMIT; | ||
| 189 | time_status |= STA_UNSYNC; | ||
| 137 | } | 190 | } |
| 138 | 191 | ||
| 139 | /* | 192 | /* |
| @@ -143,7 +196,7 @@ void second_overflow(void) | |||
| 143 | tick_length = tick_length_base; | 196 | tick_length = tick_length_base; |
| 144 | time_adj = shift_right(time_offset, SHIFT_PLL + time_constant); | 197 | time_adj = shift_right(time_offset, SHIFT_PLL + time_constant); |
| 145 | time_offset -= time_adj; | 198 | time_offset -= time_adj; |
| 146 | tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE); | 199 | tick_length += time_adj; |
| 147 | 200 | ||
| 148 | if (unlikely(time_adjust)) { | 201 | if (unlikely(time_adjust)) { |
| 149 | if (time_adjust > MAX_TICKADJ) { | 202 | if (time_adjust > MAX_TICKADJ) { |
| @@ -154,25 +207,12 @@ void second_overflow(void) | |||
| 154 | tick_length -= MAX_TICKADJ_SCALED; | 207 | tick_length -= MAX_TICKADJ_SCALED; |
| 155 | } else { | 208 | } else { |
| 156 | tick_length += (s64)(time_adjust * NSEC_PER_USEC / | 209 | tick_length += (s64)(time_adjust * NSEC_PER_USEC / |
| 157 | NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT; | 210 | NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT; |
| 158 | time_adjust = 0; | 211 | time_adjust = 0; |
| 159 | } | 212 | } |
| 160 | } | 213 | } |
| 161 | } | 214 | } |
| 162 | 215 | ||
| 163 | /* | ||
| 164 | * Return how long ticks are at the moment, that is, how much time | ||
| 165 | * update_wall_time_one_tick will add to xtime next time we call it | ||
| 166 | * (assuming no calls to do_adjtimex in the meantime). | ||
| 167 | * The return value is in fixed-point nanoseconds shifted by the | ||
| 168 | * specified number of bits to the right of the binary point. | ||
| 169 | * This function has no side-effects. | ||
| 170 | */ | ||
| 171 | u64 current_tick_length(void) | ||
| 172 | { | ||
| 173 | return tick_length; | ||
| 174 | } | ||
| 175 | |||
| 176 | #ifdef CONFIG_GENERIC_CMOS_UPDATE | 216 | #ifdef CONFIG_GENERIC_CMOS_UPDATE |
| 177 | 217 | ||
| 178 | /* Disable the cmos update - used by virtualization and embedded */ | 218 | /* Disable the cmos update - used by virtualization and embedded */ |
| @@ -236,8 +276,8 @@ static inline void notify_cmos_timer(void) { } | |||
| 236 | */ | 276 | */ |
| 237 | int do_adjtimex(struct timex *txc) | 277 | int do_adjtimex(struct timex *txc) |
| 238 | { | 278 | { |
| 239 | long mtemp, save_adjust, rem; | 279 | struct timespec ts; |
| 240 | s64 freq_adj, temp64; | 280 | long save_adjust, sec; |
| 241 | int result; | 281 | int result; |
| 242 | 282 | ||
| 243 | /* In order to modify anything, you gotta be super-user! */ | 283 | /* In order to modify anything, you gotta be super-user! */ |
| @@ -247,147 +287,132 @@ int do_adjtimex(struct timex *txc) | |||
| 247 | /* Now we validate the data before disabling interrupts */ | 287 | /* Now we validate the data before disabling interrupts */ |
| 248 | 288 | ||
| 249 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) { | 289 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) { |
| 250 | /* singleshot must not be used with any other mode bits */ | 290 | /* singleshot must not be used with any other mode bits */ |
| 251 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && | 291 | if (txc->modes & ~ADJ_OFFSET_SS_READ) |
| 252 | txc->modes != ADJ_OFFSET_SS_READ) | ||
| 253 | return -EINVAL; | 292 | return -EINVAL; |
| 254 | } | 293 | } |
| 255 | 294 | ||
| 256 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET)) | ||
| 257 | /* adjustment Offset limited to +- .512 seconds */ | ||
| 258 | if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE ) | ||
| 259 | return -EINVAL; | ||
| 260 | |||
| 261 | /* if the quartz is off by more than 10% something is VERY wrong ! */ | 295 | /* if the quartz is off by more than 10% something is VERY wrong ! */ |
| 262 | if (txc->modes & ADJ_TICK) | 296 | if (txc->modes & ADJ_TICK) |
| 263 | if (txc->tick < 900000/USER_HZ || | 297 | if (txc->tick < 900000/USER_HZ || |
| 264 | txc->tick > 1100000/USER_HZ) | 298 | txc->tick > 1100000/USER_HZ) |
| 265 | return -EINVAL; | 299 | return -EINVAL; |
| 266 | 300 | ||
| 301 | if (time_state != TIME_OK && txc->modes & ADJ_STATUS) | ||
| 302 | hrtimer_cancel(&leap_timer); | ||
| 303 | getnstimeofday(&ts); | ||
| 304 | |||
| 267 | write_seqlock_irq(&xtime_lock); | 305 | write_seqlock_irq(&xtime_lock); |
| 268 | result = time_state; /* mostly `TIME_OK' */ | ||
| 269 | 306 | ||
| 270 | /* Save for later - semantics of adjtime is to return old value */ | 307 | /* Save for later - semantics of adjtime is to return old value */ |
| 271 | save_adjust = time_adjust; | 308 | save_adjust = time_adjust; |
| 272 | 309 | ||
| 273 | #if 0 /* STA_CLOCKERR is never set yet */ | ||
| 274 | time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */ | ||
| 275 | #endif | ||
| 276 | /* If there are input parameters, then process them */ | 310 | /* If there are input parameters, then process them */ |
| 277 | if (txc->modes) | 311 | if (txc->modes) { |
| 278 | { | 312 | if (txc->modes & ADJ_STATUS) { |
| 279 | if (txc->modes & ADJ_STATUS) /* only set allowed bits */ | 313 | if ((time_status & STA_PLL) && |
| 280 | time_status = (txc->status & ~STA_RONLY) | | 314 | !(txc->status & STA_PLL)) { |
| 281 | (time_status & STA_RONLY); | 315 | time_state = TIME_OK; |
| 282 | 316 | time_status = STA_UNSYNC; | |
| 283 | if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */ | 317 | } |
| 284 | if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) { | 318 | /* only set allowed bits */ |
| 285 | result = -EINVAL; | 319 | time_status &= STA_RONLY; |
| 286 | goto leave; | 320 | time_status |= txc->status & ~STA_RONLY; |
| 287 | } | 321 | |
| 288 | time_freq = ((s64)txc->freq * NSEC_PER_USEC) | 322 | switch (time_state) { |
| 289 | >> (SHIFT_USEC - SHIFT_NSEC); | 323 | case TIME_OK: |
| 290 | } | 324 | start_timer: |
| 291 | 325 | sec = ts.tv_sec; | |
| 292 | if (txc->modes & ADJ_MAXERROR) { | 326 | if (time_status & STA_INS) { |
| 293 | if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) { | 327 | time_state = TIME_INS; |
| 294 | result = -EINVAL; | 328 | sec += 86400 - sec % 86400; |
| 295 | goto leave; | 329 | hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS); |
| 330 | } else if (time_status & STA_DEL) { | ||
| 331 | time_state = TIME_DEL; | ||
| 332 | sec += 86400 - (sec + 1) % 86400; | ||
| 333 | hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS); | ||
| 334 | } | ||
| 335 | break; | ||
| 336 | case TIME_INS: | ||
| 337 | case TIME_DEL: | ||
| 338 | time_state = TIME_OK; | ||
| 339 | goto start_timer; | ||
| 340 | break; | ||
| 341 | case TIME_WAIT: | ||
| 342 | if (!(time_status & (STA_INS | STA_DEL))) | ||
| 343 | time_state = TIME_OK; | ||
| 344 | break; | ||
| 345 | case TIME_OOP: | ||
| 346 | hrtimer_restart(&leap_timer); | ||
| 347 | break; | ||
| 348 | } | ||
| 296 | } | 349 | } |
| 297 | time_maxerror = txc->maxerror; | ||
| 298 | } | ||
| 299 | 350 | ||
| 300 | if (txc->modes & ADJ_ESTERROR) { | 351 | if (txc->modes & ADJ_NANO) |
| 301 | if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) { | 352 | time_status |= STA_NANO; |
| 302 | result = -EINVAL; | 353 | if (txc->modes & ADJ_MICRO) |
| 303 | goto leave; | 354 | time_status &= ~STA_NANO; |
| 355 | |||
| 356 | if (txc->modes & ADJ_FREQUENCY) { | ||
| 357 | time_freq = (s64)txc->freq * PPM_SCALE; | ||
| 358 | time_freq = min(time_freq, MAXFREQ_SCALED); | ||
| 359 | time_freq = max(time_freq, -MAXFREQ_SCALED); | ||
| 304 | } | 360 | } |
| 305 | time_esterror = txc->esterror; | ||
| 306 | } | ||
| 307 | 361 | ||
| 308 | if (txc->modes & ADJ_TIMECONST) { /* p. 24 */ | 362 | if (txc->modes & ADJ_MAXERROR) |
| 309 | if (txc->constant < 0) { /* NTP v4 uses values > 6 */ | 363 | time_maxerror = txc->maxerror; |
| 310 | result = -EINVAL; | 364 | if (txc->modes & ADJ_ESTERROR) |
| 311 | goto leave; | 365 | time_esterror = txc->esterror; |
| 366 | |||
| 367 | if (txc->modes & ADJ_TIMECONST) { | ||
| 368 | time_constant = txc->constant; | ||
| 369 | if (!(time_status & STA_NANO)) | ||
| 370 | time_constant += 4; | ||
| 371 | time_constant = min(time_constant, (long)MAXTC); | ||
| 372 | time_constant = max(time_constant, 0l); | ||
| 312 | } | 373 | } |
| 313 | time_constant = min(txc->constant + 4, (long)MAXTC); | ||
| 314 | } | ||
| 315 | 374 | ||
| 316 | if (txc->modes & ADJ_OFFSET) { /* values checked earlier */ | 375 | if (txc->modes & ADJ_TAI && txc->constant > 0) |
| 317 | if (txc->modes == ADJ_OFFSET_SINGLESHOT) { | 376 | time_tai = txc->constant; |
| 318 | /* adjtime() is independent from ntp_adjtime() */ | 377 | |
| 319 | time_adjust = txc->offset; | 378 | if (txc->modes & ADJ_OFFSET) { |
| 379 | if (txc->modes == ADJ_OFFSET_SINGLESHOT) | ||
| 380 | /* adjtime() is independent from ntp_adjtime() */ | ||
| 381 | time_adjust = txc->offset; | ||
| 382 | else | ||
| 383 | ntp_update_offset(txc->offset); | ||
| 320 | } | 384 | } |
| 321 | else if (time_status & STA_PLL) { | 385 | if (txc->modes & ADJ_TICK) |
| 322 | time_offset = txc->offset * NSEC_PER_USEC; | 386 | tick_usec = txc->tick; |
| 323 | 387 | ||
| 324 | /* | 388 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) |
| 325 | * Scale the phase adjustment and | 389 | ntp_update_frequency(); |
| 326 | * clamp to the operating range. | 390 | } |
| 327 | */ | 391 | |
| 328 | time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC); | 392 | result = time_state; /* mostly `TIME_OK' */ |
| 329 | time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC); | 393 | if (time_status & (STA_UNSYNC|STA_CLOCKERR)) |
| 330 | |||
| 331 | /* | ||
| 332 | * Select whether the frequency is to be controlled | ||
| 333 | * and in which mode (PLL or FLL). Clamp to the operating | ||
| 334 | * range. Ugly multiply/divide should be replaced someday. | ||
| 335 | */ | ||
| 336 | |||
| 337 | if (time_status & STA_FREQHOLD || time_reftime == 0) | ||
| 338 | time_reftime = xtime.tv_sec; | ||
| 339 | mtemp = xtime.tv_sec - time_reftime; | ||
| 340 | time_reftime = xtime.tv_sec; | ||
| 341 | |||
| 342 | freq_adj = time_offset * mtemp; | ||
| 343 | freq_adj = shift_right(freq_adj, time_constant * 2 + | ||
| 344 | (SHIFT_PLL + 2) * 2 - SHIFT_NSEC); | ||
| 345 | if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) { | ||
| 346 | u64 utemp64; | ||
| 347 | temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL); | ||
| 348 | if (time_offset < 0) { | ||
| 349 | utemp64 = -temp64; | ||
| 350 | do_div(utemp64, mtemp); | ||
| 351 | freq_adj -= utemp64; | ||
| 352 | } else { | ||
| 353 | utemp64 = temp64; | ||
| 354 | do_div(utemp64, mtemp); | ||
| 355 | freq_adj += utemp64; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | freq_adj += time_freq; | ||
| 359 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); | ||
| 360 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); | ||
| 361 | time_offset = div_long_long_rem_signed(time_offset, | ||
| 362 | NTP_INTERVAL_FREQ, | ||
| 363 | &rem); | ||
| 364 | time_offset <<= SHIFT_UPDATE; | ||
| 365 | } /* STA_PLL */ | ||
| 366 | } /* txc->modes & ADJ_OFFSET */ | ||
| 367 | if (txc->modes & ADJ_TICK) | ||
| 368 | tick_usec = txc->tick; | ||
| 369 | |||
| 370 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) | ||
| 371 | ntp_update_frequency(); | ||
| 372 | } /* txc->modes */ | ||
| 373 | leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0) | ||
| 374 | result = TIME_ERROR; | 394 | result = TIME_ERROR; |
| 375 | 395 | ||
| 376 | if ((txc->modes == ADJ_OFFSET_SINGLESHOT) || | 396 | if ((txc->modes == ADJ_OFFSET_SINGLESHOT) || |
| 377 | (txc->modes == ADJ_OFFSET_SS_READ)) | 397 | (txc->modes == ADJ_OFFSET_SS_READ)) |
| 378 | txc->offset = save_adjust; | 398 | txc->offset = save_adjust; |
| 379 | else | 399 | else { |
| 380 | txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) * | 400 | txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ, |
| 381 | NTP_INTERVAL_FREQ / 1000; | 401 | NTP_SCALE_SHIFT); |
| 382 | txc->freq = (time_freq / NSEC_PER_USEC) << | 402 | if (!(time_status & STA_NANO)) |
| 383 | (SHIFT_USEC - SHIFT_NSEC); | 403 | txc->offset /= NSEC_PER_USEC; |
| 404 | } | ||
| 405 | txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) * | ||
| 406 | (s64)PPM_SCALE_INV, | ||
| 407 | NTP_SCALE_SHIFT); | ||
| 384 | txc->maxerror = time_maxerror; | 408 | txc->maxerror = time_maxerror; |
| 385 | txc->esterror = time_esterror; | 409 | txc->esterror = time_esterror; |
| 386 | txc->status = time_status; | 410 | txc->status = time_status; |
| 387 | txc->constant = time_constant; | 411 | txc->constant = time_constant; |
| 388 | txc->precision = 1; | 412 | txc->precision = 1; |
| 389 | txc->tolerance = MAXFREQ; | 413 | txc->tolerance = MAXFREQ_SCALED / PPM_SCALE; |
| 390 | txc->tick = tick_usec; | 414 | txc->tick = tick_usec; |
| 415 | txc->tai = time_tai; | ||
| 391 | 416 | ||
| 392 | /* PPS is not implemented, so these are zero */ | 417 | /* PPS is not implemented, so these are zero */ |
| 393 | txc->ppsfreq = 0; | 418 | txc->ppsfreq = 0; |
| @@ -399,9 +424,15 @@ leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0) | |||
| 399 | txc->errcnt = 0; | 424 | txc->errcnt = 0; |
| 400 | txc->stbcnt = 0; | 425 | txc->stbcnt = 0; |
| 401 | write_sequnlock_irq(&xtime_lock); | 426 | write_sequnlock_irq(&xtime_lock); |
| 402 | do_gettimeofday(&txc->time); | 427 | |
| 428 | txc->time.tv_sec = ts.tv_sec; | ||
| 429 | txc->time.tv_usec = ts.tv_nsec; | ||
| 430 | if (!(time_status & STA_NANO)) | ||
| 431 | txc->time.tv_usec /= NSEC_PER_USEC; | ||
| 432 | |||
| 403 | notify_cmos_timer(); | 433 | notify_cmos_timer(); |
| 404 | return(result); | 434 | |
| 435 | return result; | ||
| 405 | } | 436 | } |
| 406 | 437 | ||
| 407 | static int __init ntp_tick_adj_setup(char *str) | 438 | static int __init ntp_tick_adj_setup(char *str) |
| @@ -411,3 +442,10 @@ static int __init ntp_tick_adj_setup(char *str) | |||
| 411 | } | 442 | } |
| 412 | 443 | ||
| 413 | __setup("ntp_tick_adj=", ntp_tick_adj_setup); | 444 | __setup("ntp_tick_adj=", ntp_tick_adj_setup); |
| 445 | |||
| 446 | void __init ntp_init(void) | ||
| 447 | { | ||
| 448 | ntp_clear(); | ||
| 449 | hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); | ||
| 450 | leap_timer.function = ntp_leap_second; | ||
| 451 | } | ||
