[PATCH] ntp: prescale time_offset
[pandora-kernel.git] / kernel / time / ntp.c
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>
14
15 #include <asm/div64.h>
16 #include <asm/timex.h>
17
18 /*
19  * Timekeeping variables
20  */
21 unsigned long tick_usec = TICK_USEC;            /* USER_HZ period (usec) */
22 unsigned long tick_nsec;                        /* ACTHZ period (nsec) */
23 static u64 tick_length, tick_length_base;
24
25 /* Don't completely fail for HZ > 500.  */
26 int tickadj = 500/HZ ? : 1;             /* microsecs */
27
28 /*
29  * phase-lock loop variables
30  */
31 /* TIME_ERROR prevents overwriting the CMOS clock */
32 int time_state = TIME_OK;               /* clock synchronization status */
33 int time_status = STA_UNSYNC;           /* clock status bits            */
34 long time_offset;                       /* time adjustment (ns)         */
35 long time_constant = 2;                 /* pll time constant            */
36 long time_tolerance = MAXFREQ;          /* frequency tolerance (ppm)    */
37 long time_precision = 1;                /* clock precision (us)         */
38 long time_maxerror = NTP_PHASE_LIMIT;   /* maximum error (us)           */
39 long time_esterror = NTP_PHASE_LIMIT;   /* estimated error (us)         */
40 long time_freq;                         /* frequency offset (scaled ppm)*/
41 long time_reftime;                      /* time at last adjustment (s)  */
42 long time_adjust;
43 long time_next_adjust;
44
45 /**
46  * ntp_clear - Clears the NTP state variables
47  *
48  * Must be called while holding a write on the xtime_lock
49  */
50 void ntp_clear(void)
51 {
52         time_adjust = 0;                /* stop active adjtime() */
53         time_status |= STA_UNSYNC;
54         time_maxerror = NTP_PHASE_LIMIT;
55         time_esterror = NTP_PHASE_LIMIT;
56
57         ntp_update_frequency();
58
59         tick_length = tick_length_base;
60         time_offset = 0;
61 }
62
63 #define CLOCK_TICK_OVERFLOW     (LATCH * HZ - CLOCK_TICK_RATE)
64 #define CLOCK_TICK_ADJUST       (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / (s64)CLOCK_TICK_RATE)
65
66 void ntp_update_frequency(void)
67 {
68         tick_length_base = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << TICK_LENGTH_SHIFT;
69         tick_length_base += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
70         tick_length_base += ((s64)time_freq * NSEC_PER_USEC) << (TICK_LENGTH_SHIFT - SHIFT_USEC);
71
72         do_div(tick_length_base, HZ);
73
74         tick_nsec = tick_length_base >> TICK_LENGTH_SHIFT;
75 }
76
77 /*
78  * this routine handles the overflow of the microsecond field
79  *
80  * The tricky bits of code to handle the accurate clock support
81  * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
82  * They were originally developed for SUN and DEC kernels.
83  * All the kudos should go to Dave for this stuff.
84  */
85 void second_overflow(void)
86 {
87         long time_adj;
88
89         /* Bump the maxerror field */
90         time_maxerror += time_tolerance >> SHIFT_USEC;
91         if (time_maxerror > NTP_PHASE_LIMIT) {
92                 time_maxerror = NTP_PHASE_LIMIT;
93                 time_status |= STA_UNSYNC;
94         }
95
96         /*
97          * Leap second processing. If in leap-insert state at the end of the
98          * day, the system clock is set back one second; if in leap-delete
99          * state, the system clock is set ahead one second. The microtime()
100          * routine or external clock driver will insure that reported time is
101          * always monotonic. The ugly divides should be replaced.
102          */
103         switch (time_state) {
104         case TIME_OK:
105                 if (time_status & STA_INS)
106                         time_state = TIME_INS;
107                 else if (time_status & STA_DEL)
108                         time_state = TIME_DEL;
109                 break;
110         case TIME_INS:
111                 if (xtime.tv_sec % 86400 == 0) {
112                         xtime.tv_sec--;
113                         wall_to_monotonic.tv_sec++;
114                         /*
115                          * The timer interpolator will make time change
116                          * gradually instead of an immediate jump by one second
117                          */
118                         time_interpolator_update(-NSEC_PER_SEC);
119                         time_state = TIME_OOP;
120                         clock_was_set();
121                         printk(KERN_NOTICE "Clock: inserting leap second "
122                                         "23:59:60 UTC\n");
123                 }
124                 break;
125         case TIME_DEL:
126                 if ((xtime.tv_sec + 1) % 86400 == 0) {
127                         xtime.tv_sec++;
128                         wall_to_monotonic.tv_sec--;
129                         /*
130                          * Use of time interpolator for a gradual change of
131                          * time
132                          */
133                         time_interpolator_update(NSEC_PER_SEC);
134                         time_state = TIME_WAIT;
135                         clock_was_set();
136                         printk(KERN_NOTICE "Clock: deleting leap second "
137                                         "23:59:59 UTC\n");
138                 }
139                 break;
140         case TIME_OOP:
141                 time_state = TIME_WAIT;
142                 break;
143         case TIME_WAIT:
144                 if (!(time_status & (STA_INS | STA_DEL)))
145                 time_state = TIME_OK;
146         }
147
148         /*
149          * Compute the phase adjustment for the next second. In PLL mode, the
150          * offset is reduced by a fixed factor times the time constant. In FLL
151          * mode the offset is used directly. In either mode, the maximum phase
152          * adjustment for each second is clamped so as to spread the adjustment
153          * over not more than the number of seconds between updates.
154          */
155         tick_length = tick_length_base;
156         time_adj = time_offset;
157         if (!(time_status & STA_FLL))
158                 time_adj = shift_right(time_adj, SHIFT_KG + time_constant);
159         time_adj = min(time_adj, -((MAXPHASE / HZ) << SHIFT_UPDATE) / MINSEC);
160         time_adj = max(time_adj, ((MAXPHASE / HZ) << SHIFT_UPDATE) / MINSEC);
161         time_offset -= time_adj;
162         tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
163 }
164
165 /*
166  * Returns how many microseconds we need to add to xtime this tick
167  * in doing an adjustment requested with adjtime.
168  */
169 static long adjtime_adjustment(void)
170 {
171         long time_adjust_step;
172
173         time_adjust_step = time_adjust;
174         if (time_adjust_step) {
175                 /*
176                  * We are doing an adjtime thing.  Prepare time_adjust_step to
177                  * be within bounds.  Note that a positive time_adjust means we
178                  * want the clock to run faster.
179                  *
180                  * Limit the amount of the step to be in the range
181                  * -tickadj .. +tickadj
182                  */
183                 time_adjust_step = min(time_adjust_step, (long)tickadj);
184                 time_adjust_step = max(time_adjust_step, (long)-tickadj);
185         }
186         return time_adjust_step;
187 }
188
189 /* in the NTP reference this is called "hardclock()" */
190 void update_ntp_one_tick(void)
191 {
192         long time_adjust_step;
193
194         time_adjust_step = adjtime_adjustment();
195         if (time_adjust_step)
196                 /* Reduce by this step the amount of time left  */
197                 time_adjust -= time_adjust_step;
198
199         /* Changes by adjtime() do not take effect till next tick. */
200         if (time_next_adjust != 0) {
201                 time_adjust = time_next_adjust;
202                 time_next_adjust = 0;
203         }
204 }
205
206 /*
207  * Return how long ticks are at the moment, that is, how much time
208  * update_wall_time_one_tick will add to xtime next time we call it
209  * (assuming no calls to do_adjtimex in the meantime).
210  * The return value is in fixed-point nanoseconds shifted by the
211  * specified number of bits to the right of the binary point.
212  * This function has no side-effects.
213  */
214 u64 current_tick_length(void)
215 {
216         u64 ret;
217
218         /* calculate the finest interval NTP will allow.
219          */
220         ret = tick_length;
221         ret += (u64)(adjtime_adjustment() * 1000) << TICK_LENGTH_SHIFT;
222
223         return ret;
224 }
225
226
227 void __attribute__ ((weak)) notify_arch_cmos_timer(void)
228 {
229         return;
230 }
231
232 /* adjtimex mainly allows reading (and writing, if superuser) of
233  * kernel time-keeping variables. used by xntpd.
234  */
235 int do_adjtimex(struct timex *txc)
236 {
237         long ltemp, mtemp, save_adjust;
238         int result;
239
240         /* In order to modify anything, you gotta be super-user! */
241         if (txc->modes && !capable(CAP_SYS_TIME))
242                 return -EPERM;
243
244         /* Now we validate the data before disabling interrupts */
245
246         if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
247           /* singleshot must not be used with any other mode bits */
248                 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
249                         return -EINVAL;
250
251         if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
252           /* adjustment Offset limited to +- .512 seconds */
253                 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
254                         return -EINVAL;
255
256         /* if the quartz is off by more than 10% something is VERY wrong ! */
257         if (txc->modes & ADJ_TICK)
258                 if (txc->tick <  900000/USER_HZ ||
259                     txc->tick > 1100000/USER_HZ)
260                         return -EINVAL;
261
262         write_seqlock_irq(&xtime_lock);
263         result = time_state;    /* mostly `TIME_OK' */
264
265         /* Save for later - semantics of adjtime is to return old value */
266         save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
267
268 #if 0   /* STA_CLOCKERR is never set yet */
269         time_status &= ~STA_CLOCKERR;           /* reset STA_CLOCKERR */
270 #endif
271         /* If there are input parameters, then process them */
272         if (txc->modes)
273         {
274             if (txc->modes & ADJ_STATUS)        /* only set allowed bits */
275                 time_status =  (txc->status & ~STA_RONLY) |
276                               (time_status & STA_RONLY);
277
278             if (txc->modes & ADJ_FREQUENCY) {   /* p. 22 */
279                 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
280                     result = -EINVAL;
281                     goto leave;
282                 }
283                 time_freq = txc->freq;
284             }
285
286             if (txc->modes & ADJ_MAXERROR) {
287                 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
288                     result = -EINVAL;
289                     goto leave;
290                 }
291                 time_maxerror = txc->maxerror;
292             }
293
294             if (txc->modes & ADJ_ESTERROR) {
295                 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
296                     result = -EINVAL;
297                     goto leave;
298                 }
299                 time_esterror = txc->esterror;
300             }
301
302             if (txc->modes & ADJ_TIMECONST) {   /* p. 24 */
303                 if (txc->constant < 0) {        /* NTP v4 uses values > 6 */
304                     result = -EINVAL;
305                     goto leave;
306                 }
307                 time_constant = txc->constant;
308             }
309
310             if (txc->modes & ADJ_OFFSET) {      /* values checked earlier */
311                 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
312                     /* adjtime() is independent from ntp_adjtime() */
313                     if ((time_next_adjust = txc->offset) == 0)
314                          time_adjust = 0;
315                 }
316                 else if (time_status & STA_PLL) {
317                     ltemp = txc->offset;
318
319                     /*
320                      * Scale the phase adjustment and
321                      * clamp to the operating range.
322                      */
323                     time_offset = min(ltemp, MAXPHASE);
324                     time_offset = max(time_offset, -MAXPHASE);
325
326                     /*
327                      * Select whether the frequency is to be controlled
328                      * and in which mode (PLL or FLL). Clamp to the operating
329                      * range. Ugly multiply/divide should be replaced someday.
330                      */
331
332                     if (time_status & STA_FREQHOLD || time_reftime == 0)
333                         time_reftime = xtime.tv_sec;
334                     mtemp = xtime.tv_sec - time_reftime;
335                     time_reftime = xtime.tv_sec;
336                     if (time_status & STA_FLL) {
337                         if (mtemp >= MINSEC) {
338                             ltemp = ((time_offset << 12) / mtemp) << (SHIFT_USEC - 12);
339                             time_freq += shift_right(ltemp, SHIFT_KH);
340                         } else /* calibration interval too short (p. 12) */
341                                 result = TIME_ERROR;
342                     } else {    /* PLL mode */
343                         if (mtemp < MAXSEC) {
344                             ltemp *= mtemp;
345                             time_freq += shift_right(ltemp,(time_constant +
346                                                        time_constant +
347                                                        SHIFT_KF - SHIFT_USEC));
348                         } else /* calibration interval too long (p. 12) */
349                                 result = TIME_ERROR;
350                     }
351                     time_freq = min(time_freq, time_tolerance);
352                     time_freq = max(time_freq, -time_tolerance);
353                     time_offset = (time_offset * NSEC_PER_USEC / HZ) << SHIFT_UPDATE;
354                 } /* STA_PLL */
355             } /* txc->modes & ADJ_OFFSET */
356             if (txc->modes & ADJ_TICK)
357                 tick_usec = txc->tick;
358
359             if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
360                     ntp_update_frequency();
361         } /* txc->modes */
362 leave:  if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
363                 result = TIME_ERROR;
364
365         if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
366             txc->offset    = save_adjust;
367         else
368             txc->offset    = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000;
369         txc->freq          = time_freq;
370         txc->maxerror      = time_maxerror;
371         txc->esterror      = time_esterror;
372         txc->status        = time_status;
373         txc->constant      = time_constant;
374         txc->precision     = time_precision;
375         txc->tolerance     = time_tolerance;
376         txc->tick          = tick_usec;
377
378         /* PPS is not implemented, so these are zero */
379         txc->ppsfreq       = 0;
380         txc->jitter        = 0;
381         txc->shift         = 0;
382         txc->stabil        = 0;
383         txc->jitcnt        = 0;
384         txc->calcnt        = 0;
385         txc->errcnt        = 0;
386         txc->stbcnt        = 0;
387         write_sequnlock_irq(&xtime_lock);
388         do_gettimeofday(&txc->time);
389         notify_arch_cmos_timer();
390         return(result);
391 }