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