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