kern_tc.c revision 1.1.1.1.2.3 1 /* $NetBSD: kern_tc.c,v 1.1.1.1.2.3 2006/02/04 08:19:04 simonb Exp $ */
2
3 /*-
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk (at) FreeBSD.ORG> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
10 */
11
12 #include <sys/cdefs.h>
13 /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
14 __KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.1.1.1.2.3 2006/02/04 08:19:04 simonb Exp $");
15
16 #include "opt_ntp.h"
17
18 #include <sys/param.h>
19 #include <sys/kernel.h>
20 #include <sys/sysctl.h>
21 #include <sys/syslog.h>
22 #include <sys/systm.h>
23 #include <sys/timepps.h>
24 #include <sys/timetc.h>
25 #include <sys/timex.h>
26
27 /*
28 * A large step happens on boot. This constant detects such steps.
29 * It is relatively small so that ntp_update_second gets called enough
30 * in the typical 'missed a couple of seconds' case, but doesn't loop
31 * forever when the time step is large.
32 */
33 #define LARGE_STEP 200
34
35 /*
36 * Implement a dummy timecounter which we can use until we get a real one
37 * in the air. This allows the console and other early stuff to use
38 * time services.
39 */
40
41 static u_int
42 dummy_get_timecount(struct timecounter *tc)
43 {
44 static u_int now;
45
46 return (++now);
47 }
48
49 static struct timecounter dummy_timecounter = {
50 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
51 };
52
53 struct timehands {
54 /* These fields must be initialized by the driver. */
55 struct timecounter *th_counter;
56 int64_t th_adjustment;
57 u_int64_t th_scale;
58 u_int th_offset_count;
59 struct bintime th_offset;
60 struct timeval th_microtime;
61 struct timespec th_nanotime;
62 /* Fields not to be copied in tc_windup start with th_generation. */
63 volatile u_int th_generation;
64 struct timehands *th_next;
65 };
66
67 static struct timehands th0;
68 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
69 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
70 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
71 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
72 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
73 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
74 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
75 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
76 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
77 static struct timehands th0 = {
78 &dummy_timecounter,
79 0,
80 (uint64_t)-1 / 1000000,
81 0,
82 {1, 0},
83 {0, 0},
84 {0, 0},
85 1,
86 &th1
87 };
88
89 static struct timehands *volatile timehands = &th0;
90 struct timecounter *timecounter = &dummy_timecounter;
91 static struct timecounter *timecounters = &dummy_timecounter;
92
93 time_t time_second = 1;
94 time_t time_uptime = 1;
95
96 static struct bintime boottimebin;
97 struct timeval boottime;
98 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
99 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
100 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
101
102 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
103
104 static int timestepwarnings;
105 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
106 ×tepwarnings, 0, "");
107
108 #define TC_STATS(foo) \
109 static u_int foo; \
110 SYSCTL_UINT(_kern_timecounter, OID_AUTO, foo, CTLFLAG_RD, &foo, 0, "");\
111 struct __hack
112
113 TC_STATS(nbinuptime); TC_STATS(nnanouptime); TC_STATS(nmicrouptime);
114 TC_STATS(nbintime); TC_STATS(nnanotime); TC_STATS(nmicrotime);
115 TC_STATS(ngetbinuptime); TC_STATS(ngetnanouptime); TC_STATS(ngetmicrouptime);
116 TC_STATS(ngetbintime); TC_STATS(ngetnanotime); TC_STATS(ngetmicrotime);
117 TC_STATS(nsetclock);
118
119 #undef TC_STATS
120
121 static void tc_windup(void);
122
123 static int
124 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
125 {
126 #ifdef SCTL_MASK32
127 int tv[2];
128
129 if (req->flags & SCTL_MASK32) {
130 tv[0] = boottime.tv_sec;
131 tv[1] = boottime.tv_usec;
132 return SYSCTL_OUT(req, tv, sizeof(tv));
133 } else
134 #endif
135 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
136 }
137 /*
138 * Return the difference between the timehands' counter value now and what
139 * was when we copied it to the timehands' offset_count.
140 */
141 static __inline u_int
142 tc_delta(struct timehands *th)
143 {
144 struct timecounter *tc;
145
146 tc = th->th_counter;
147 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
148 tc->tc_counter_mask);
149 }
150
151 /*
152 * Functions for reading the time. We have to loop until we are sure that
153 * the timehands that we operated on was not updated under our feet. See
154 * the comment in <sys/time.h> for a description of these 12 functions.
155 */
156
157 void
158 binuptime(struct bintime *bt)
159 {
160 struct timehands *th;
161 u_int gen;
162
163 nbinuptime++;
164 do {
165 th = timehands;
166 gen = th->th_generation;
167 *bt = th->th_offset;
168 bintime_addx(bt, th->th_scale * tc_delta(th));
169 } while (gen == 0 || gen != th->th_generation);
170 }
171
172 void
173 nanouptime(struct timespec *tsp)
174 {
175 struct bintime bt;
176
177 nnanouptime++;
178 binuptime(&bt);
179 bintime2timespec(&bt, tsp);
180 }
181
182 void
183 microuptime(struct timeval *tvp)
184 {
185 struct bintime bt;
186
187 nmicrouptime++;
188 binuptime(&bt);
189 bintime2timeval(&bt, tvp);
190 }
191
192 void
193 bintime(struct bintime *bt)
194 {
195
196 nbintime++;
197 binuptime(bt);
198 bintime_add(bt, &boottimebin);
199 }
200
201 void
202 nanotime(struct timespec *tsp)
203 {
204 struct bintime bt;
205
206 nnanotime++;
207 bintime(&bt);
208 bintime2timespec(&bt, tsp);
209 }
210
211 void
212 microtime(struct timeval *tvp)
213 {
214 struct bintime bt;
215
216 nmicrotime++;
217 bintime(&bt);
218 bintime2timeval(&bt, tvp);
219 }
220
221 void
222 getbinuptime(struct bintime *bt)
223 {
224 struct timehands *th;
225 u_int gen;
226
227 ngetbinuptime++;
228 do {
229 th = timehands;
230 gen = th->th_generation;
231 *bt = th->th_offset;
232 } while (gen == 0 || gen != th->th_generation);
233 }
234
235 void
236 getnanouptime(struct timespec *tsp)
237 {
238 struct timehands *th;
239 u_int gen;
240
241 ngetnanouptime++;
242 do {
243 th = timehands;
244 gen = th->th_generation;
245 bintime2timespec(&th->th_offset, tsp);
246 } while (gen == 0 || gen != th->th_generation);
247 }
248
249 void
250 getmicrouptime(struct timeval *tvp)
251 {
252 struct timehands *th;
253 u_int gen;
254
255 ngetmicrouptime++;
256 do {
257 th = timehands;
258 gen = th->th_generation;
259 bintime2timeval(&th->th_offset, tvp);
260 } while (gen == 0 || gen != th->th_generation);
261 }
262
263 void
264 getbintime(struct bintime *bt)
265 {
266 struct timehands *th;
267 u_int gen;
268
269 ngetbintime++;
270 do {
271 th = timehands;
272 gen = th->th_generation;
273 *bt = th->th_offset;
274 } while (gen == 0 || gen != th->th_generation);
275 bintime_add(bt, &boottimebin);
276 }
277
278 void
279 getnanotime(struct timespec *tsp)
280 {
281 struct timehands *th;
282 u_int gen;
283
284 ngetnanotime++;
285 do {
286 th = timehands;
287 gen = th->th_generation;
288 *tsp = th->th_nanotime;
289 } while (gen == 0 || gen != th->th_generation);
290 }
291
292 void
293 getmicrotime(struct timeval *tvp)
294 {
295 struct timehands *th;
296 u_int gen;
297
298 ngetmicrotime++;
299 do {
300 th = timehands;
301 gen = th->th_generation;
302 *tvp = th->th_microtime;
303 } while (gen == 0 || gen != th->th_generation);
304 }
305
306 /*
307 * Initialize a new timecounter and possibly use it.
308 */
309 void
310 tc_init(struct timecounter *tc)
311 {
312 u_int u;
313
314 u = tc->tc_frequency / tc->tc_counter_mask;
315 /* XXX: We need some margin here, 10% is a guess */
316 u *= 11;
317 u /= 10;
318 if (u > hz && tc->tc_quality >= 0) {
319 tc->tc_quality = -2000;
320 if (bootverbose) {
321 printf("Timecounter \"%s\" frequency %ju Hz",
322 tc->tc_name, (uintmax_t)tc->tc_frequency);
323 printf(" -- Insufficient hz, needs at least %u\n", u);
324 }
325 } else if (tc->tc_quality >= 0 || bootverbose) {
326 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
327 tc->tc_name, (uintmax_t)tc->tc_frequency,
328 tc->tc_quality);
329 }
330
331 tc->tc_next = timecounters;
332 timecounters = tc;
333 /*
334 * Never automatically use a timecounter with negative quality.
335 * Even though we run on the dummy counter, switching here may be
336 * worse since this timecounter may not be monotonous.
337 */
338 if (tc->tc_quality < 0)
339 return;
340 if (tc->tc_quality < timecounter->tc_quality)
341 return;
342 if (tc->tc_quality == timecounter->tc_quality &&
343 tc->tc_frequency < timecounter->tc_frequency)
344 return;
345 (void)tc->tc_get_timecount(tc);
346 (void)tc->tc_get_timecount(tc);
347 timecounter = tc;
348 }
349
350 /* Report the frequency of the current timecounter. */
351 u_int64_t
352 tc_getfrequency(void)
353 {
354
355 return (timehands->th_counter->tc_frequency);
356 }
357
358 /*
359 * Step our concept of UTC. This is done by modifying our estimate of
360 * when we booted.
361 * XXX: not locked.
362 */
363 void
364 tc_setclock(struct timespec *ts)
365 {
366 struct timespec ts2;
367 struct bintime bt, bt2;
368
369 nsetclock++;
370 binuptime(&bt2);
371 timespec2bintime(ts, &bt);
372 bintime_sub(&bt, &bt2);
373 bintime_add(&bt2, &boottimebin);
374 boottimebin = bt;
375 bintime2timeval(&bt, &boottime);
376
377 /* XXX fiddle all the little crinkly bits around the fiords... */
378 tc_windup();
379 if (timestepwarnings) {
380 bintime2timespec(&bt2, &ts2);
381 log(LOG_INFO, "Time stepped from %jd.%09ld to %jd.%09ld\n",
382 (intmax_t)ts2.tv_sec, ts2.tv_nsec,
383 (intmax_t)ts->tv_sec, ts->tv_nsec);
384 }
385 }
386
387 /*
388 * Initialize the next struct timehands in the ring and make
389 * it the active timehands. Along the way we might switch to a different
390 * timecounter and/or do seconds processing in NTP. Slightly magic.
391 */
392 static void
393 tc_windup(void)
394 {
395 struct bintime bt;
396 struct timehands *th, *tho;
397 u_int64_t scale;
398 u_int delta, ncount, ogen;
399 int i;
400 time_t t;
401
402 /*
403 * Make the next timehands a copy of the current one, but do not
404 * overwrite the generation or next pointer. While we update
405 * the contents, the generation must be zero.
406 */
407 tho = timehands;
408 th = tho->th_next;
409 ogen = th->th_generation;
410 th->th_generation = 0;
411 bcopy(tho, th, offsetof(struct timehands, th_generation));
412
413 /*
414 * Capture a timecounter delta on the current timecounter and if
415 * changing timecounters, a counter value from the new timecounter.
416 * Update the offset fields accordingly.
417 */
418 delta = tc_delta(th);
419 if (th->th_counter != timecounter)
420 ncount = timecounter->tc_get_timecount(timecounter);
421 else
422 ncount = 0;
423 th->th_offset_count += delta;
424 th->th_offset_count &= th->th_counter->tc_counter_mask;
425 bintime_addx(&th->th_offset, th->th_scale * delta);
426
427 /*
428 * Hardware latching timecounters may not generate interrupts on
429 * PPS events, so instead we poll them. There is a finite risk that
430 * the hardware might capture a count which is later than the one we
431 * got above, and therefore possibly in the next NTP second which might
432 * have a different rate than the current NTP second. It doesn't
433 * matter in practice.
434 */
435 if (tho->th_counter->tc_poll_pps)
436 tho->th_counter->tc_poll_pps(tho->th_counter);
437
438 /*
439 * Deal with NTP second processing. The for loop normally
440 * iterates at most once, but in extreme situations it might
441 * keep NTP sane if timeouts are not run for several seconds.
442 * At boot, the time step can be large when the TOD hardware
443 * has been read, so on really large steps, we call
444 * ntp_update_second only twice. We need to call it twice in
445 * case we missed a leap second.
446 */
447 bt = th->th_offset;
448 bintime_add(&bt, &boottimebin);
449 i = bt.sec - tho->th_microtime.tv_sec;
450 if (i > LARGE_STEP)
451 i = 2;
452 for (; i > 0; i--) {
453 t = bt.sec;
454 ntp_update_second(&th->th_adjustment, &bt.sec);
455 if (bt.sec != t)
456 boottimebin.sec += bt.sec - t;
457 }
458 /* Update the UTC timestamps used by the get*() functions. */
459 /* XXX shouldn't do this here. Should force non-`get' versions. */
460 bintime2timeval(&bt, &th->th_microtime);
461 bintime2timespec(&bt, &th->th_nanotime);
462
463 /* Now is a good time to change timecounters. */
464 if (th->th_counter != timecounter) {
465 th->th_counter = timecounter;
466 th->th_offset_count = ncount;
467 }
468
469 /*-
470 * Recalculate the scaling factor. We want the number of 1/2^64
471 * fractions of a second per period of the hardware counter, taking
472 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
473 * processing provides us with.
474 *
475 * The th_adjustment is nanoseconds per second with 32 bit binary
476 * fraction and we want 64 bit binary fraction of second:
477 *
478 * x = a * 2^32 / 10^9 = a * 4.294967296
479 *
480 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
481 * we can only multiply by about 850 without overflowing, but that
482 * leaves suitably precise fractions for multiply before divide.
483 *
484 * Divide before multiply with a fraction of 2199/512 results in a
485 * systematic undercompensation of 10PPM of th_adjustment. On a
486 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
487 *
488 * We happily sacrifice the lowest of the 64 bits of our result
489 * to the goddess of code clarity.
490 *
491 */
492 scale = (u_int64_t)1 << 63;
493 scale += (th->th_adjustment / 1024) * 2199;
494 scale /= th->th_counter->tc_frequency;
495 th->th_scale = scale * 2;
496
497 /*
498 * Now that the struct timehands is again consistent, set the new
499 * generation number, making sure to not make it zero.
500 */
501 if (++ogen == 0)
502 ogen = 1;
503 th->th_generation = ogen;
504
505 /* Go live with the new struct timehands. */
506 time_second = th->th_microtime.tv_sec;
507 time_uptime = th->th_offset.sec;
508 timehands = th;
509 }
510
511 /* Report or change the active timecounter hardware. */
512 static int
513 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
514 {
515 char newname[32];
516 struct timecounter *newtc, *tc;
517 int error;
518
519 tc = timecounter;
520 strlcpy(newname, tc->tc_name, sizeof(newname));
521
522 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
523 if (error != 0 || req->newptr == NULL ||
524 strcmp(newname, tc->tc_name) == 0)
525 return (error);
526 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
527 if (strcmp(newname, newtc->tc_name) != 0)
528 continue;
529
530 /* Warm up new timecounter. */
531 (void)newtc->tc_get_timecount(newtc);
532 (void)newtc->tc_get_timecount(newtc);
533
534 timecounter = newtc;
535 return (0);
536 }
537 return (EINVAL);
538 }
539
540 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
541 0, 0, sysctl_kern_timecounter_hardware, "A", "");
542
543
544 /* Report or change the active timecounter hardware. */
545 static int
546 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
547 {
548 char buf[32], *spc;
549 struct timecounter *tc;
550 int error;
551
552 spc = "";
553 error = 0;
554 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
555 sprintf(buf, "%s%s(%d)",
556 spc, tc->tc_name, tc->tc_quality);
557 error = SYSCTL_OUT(req, buf, strlen(buf));
558 spc = " ";
559 }
560 return (error);
561 }
562
563 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
564 0, 0, sysctl_kern_timecounter_choice, "A", "");
565
566 /*
567 * RFC 2783 PPS-API implementation.
568 */
569
570 int
571 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
572 {
573 pps_params_t *app;
574 struct pps_fetch_args *fapi;
575 #ifdef PPS_SYNC
576 struct pps_kcbind_args *kapi;
577 #endif
578
579 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
580 switch (cmd) {
581 case PPS_IOC_CREATE:
582 return (0);
583 case PPS_IOC_DESTROY:
584 return (0);
585 case PPS_IOC_SETPARAMS:
586 app = (pps_params_t *)data;
587 if (app->mode & ~pps->ppscap)
588 return (EINVAL);
589 pps->ppsparam = *app;
590 return (0);
591 case PPS_IOC_GETPARAMS:
592 app = (pps_params_t *)data;
593 *app = pps->ppsparam;
594 app->api_version = PPS_API_VERS_1;
595 return (0);
596 case PPS_IOC_GETCAP:
597 *(int*)data = pps->ppscap;
598 return (0);
599 case PPS_IOC_FETCH:
600 fapi = (struct pps_fetch_args *)data;
601 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
602 return (EINVAL);
603 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
604 return (EOPNOTSUPP);
605 pps->ppsinfo.current_mode = pps->ppsparam.mode;
606 fapi->pps_info_buf = pps->ppsinfo;
607 return (0);
608 case PPS_IOC_KCBIND:
609 #ifdef PPS_SYNC
610 kapi = (struct pps_kcbind_args *)data;
611 /* XXX Only root should be able to do this */
612 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
613 return (EINVAL);
614 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
615 return (EINVAL);
616 if (kapi->edge & ~pps->ppscap)
617 return (EINVAL);
618 pps->kcmode = kapi->edge;
619 return (0);
620 #else
621 return (EOPNOTSUPP);
622 #endif
623 default:
624 return (ENOIOCTL);
625 }
626 }
627
628 void
629 pps_init(struct pps_state *pps)
630 {
631 pps->ppscap |= PPS_TSFMT_TSPEC;
632 if (pps->ppscap & PPS_CAPTUREASSERT)
633 pps->ppscap |= PPS_OFFSETASSERT;
634 if (pps->ppscap & PPS_CAPTURECLEAR)
635 pps->ppscap |= PPS_OFFSETCLEAR;
636 }
637
638 void
639 pps_capture(struct pps_state *pps)
640 {
641 struct timehands *th;
642
643 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
644 th = timehands;
645 pps->capgen = th->th_generation;
646 pps->capth = th;
647 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
648 if (pps->capgen != th->th_generation)
649 pps->capgen = 0;
650 }
651
652 void
653 pps_event(struct pps_state *pps, int event)
654 {
655 struct bintime bt;
656 struct timespec ts, *tsp, *osp;
657 u_int tcount, *pcount;
658 int foff, fhard;
659 pps_seq_t *pseq;
660
661 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
662 /* If the timecounter was wound up underneath us, bail out. */
663 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
664 return;
665
666 /* Things would be easier with arrays. */
667 if (event == PPS_CAPTUREASSERT) {
668 tsp = &pps->ppsinfo.assert_timestamp;
669 osp = &pps->ppsparam.assert_offset;
670 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
671 fhard = pps->kcmode & PPS_CAPTUREASSERT;
672 pcount = &pps->ppscount[0];
673 pseq = &pps->ppsinfo.assert_sequence;
674 } else {
675 tsp = &pps->ppsinfo.clear_timestamp;
676 osp = &pps->ppsparam.clear_offset;
677 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
678 fhard = pps->kcmode & PPS_CAPTURECLEAR;
679 pcount = &pps->ppscount[1];
680 pseq = &pps->ppsinfo.clear_sequence;
681 }
682
683 /*
684 * If the timecounter changed, we cannot compare the count values, so
685 * we have to drop the rest of the PPS-stuff until the next event.
686 */
687 if (pps->ppstc != pps->capth->th_counter) {
688 pps->ppstc = pps->capth->th_counter;
689 *pcount = pps->capcount;
690 pps->ppscount[2] = pps->capcount;
691 return;
692 }
693
694 /* Convert the count to a timespec. */
695 tcount = pps->capcount - pps->capth->th_offset_count;
696 tcount &= pps->capth->th_counter->tc_counter_mask;
697 bt = pps->capth->th_offset;
698 bintime_addx(&bt, pps->capth->th_scale * tcount);
699 bintime_add(&bt, &boottimebin);
700 bintime2timespec(&bt, &ts);
701
702 /* If the timecounter was wound up underneath us, bail out. */
703 if (pps->capgen != pps->capth->th_generation)
704 return;
705
706 *pcount = pps->capcount;
707 (*pseq)++;
708 *tsp = ts;
709
710 if (foff) {
711 timespecadd(tsp, osp);
712 if (tsp->tv_nsec < 0) {
713 tsp->tv_nsec += 1000000000;
714 tsp->tv_sec -= 1;
715 }
716 }
717 #ifdef PPS_SYNC
718 if (fhard) {
719 u_int64_t scale;
720
721 /*
722 * Feed the NTP PLL/FLL.
723 * The FLL wants to know how many (hardware) nanoseconds
724 * elapsed since the previous event.
725 */
726 tcount = pps->capcount - pps->ppscount[2];
727 pps->ppscount[2] = pps->capcount;
728 tcount &= pps->capth->th_counter->tc_counter_mask;
729 scale = (u_int64_t)1 << 63;
730 scale /= pps->capth->th_counter->tc_frequency;
731 scale *= 2;
732 bt.sec = 0;
733 bt.frac = 0;
734 bintime_addx(&bt, scale * tcount);
735 bintime2timespec(&bt, &ts);
736 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
737 }
738 #endif
739 }
740
741 /*
742 * Timecounters need to be updated every so often to prevent the hardware
743 * counter from overflowing. Updating also recalculates the cached values
744 * used by the get*() family of functions, so their precision depends on
745 * the update frequency.
746 */
747
748 static int tc_tick;
749 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "");
750
751 void
752 tc_ticktock(void)
753 {
754 static int count;
755
756 if (++count < tc_tick)
757 return;
758 count = 0;
759 tc_windup();
760 }
761
762 static void
763 inittimecounter(void *dummy)
764 {
765 u_int p;
766
767 /*
768 * Set the initial timeout to
769 * max(1, <approx. number of hardclock ticks in a millisecond>).
770 * People should probably not use the sysctl to set the timeout
771 * to smaller than its inital value, since that value is the
772 * smallest reasonable one. If they want better timestamps they
773 * should use the non-"get"* functions.
774 */
775 if (hz > 1000)
776 tc_tick = (hz + 500) / 1000;
777 else
778 tc_tick = 1;
779 p = (tc_tick * 1000000) / hz;
780 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
781
782 /* warm up new timecounter (again) and get rolling. */
783 (void)timecounter->tc_get_timecount(timecounter);
784 (void)timecounter->tc_get_timecount(timecounter);
785 }
786
787 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL)
788