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