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