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