2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
15 #include <sys/param.h>
16 #include <sys/kernel.h>
17 #include <sys/sysctl.h>
18 #include <sys/syslog.h>
19 #include <sys/systm.h>
20 #include <sys/timepps.h>
21 #include <sys/timetc.h>
22 #include <sys/timex.h>
25 * A large step happens on boot. This constant detects such steps.
26 * It is relatively small so that ntp_update_second gets called enough
27 * in the typical 'missed a couple of seconds' case, but doesn't loop
28 * forever when the time step is large.
30 #define LARGE_STEP 200
33 * Implement a dummy timecounter which we can use until we get a real one
34 * in the air. This allows the console and other early stuff to use
39 dummy_get_timecount(struct timecounter *tc)
46 static struct timecounter dummy_timecounter = {
47 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
51 /* These fields must be initialized by the driver. */
52 struct timecounter *th_counter;
53 int64_t th_adjustment;
55 u_int th_offset_count;
56 struct bintime th_offset;
57 struct timeval th_microtime;
58 struct timespec th_nanotime;
59 /* Fields not to be copied in tc_windup start with th_generation. */
60 volatile u_int th_generation;
61 struct timehands *th_next;
64 static struct timehands th0;
65 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
66 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
67 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
68 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
69 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
70 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
71 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
72 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
73 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
74 static struct timehands th0 = {
77 (uint64_t)-1 / 1000000,
86 static struct timehands *volatile timehands = &th0;
87 struct timecounter *timecounter = &dummy_timecounter;
88 static struct timecounter *timecounters = &dummy_timecounter;
90 time_t time_second = 1;
91 time_t time_uptime = 1;
93 static struct bintime boottimebin;
94 struct timeval boottime;
95 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
96 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
97 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
99 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
101 static int timestepwarnings;
102 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
103 ×tepwarnings, 0, "");
105 #define TC_STATS(foo) \
107 SYSCTL_UINT(_kern_timecounter, OID_AUTO, foo, CTLFLAG_RD, &foo, 0, "");\
110 TC_STATS(nbinuptime); TC_STATS(nnanouptime); TC_STATS(nmicrouptime);
111 TC_STATS(nbintime); TC_STATS(nnanotime); TC_STATS(nmicrotime);
112 TC_STATS(ngetbinuptime); TC_STATS(ngetnanouptime); TC_STATS(ngetmicrouptime);
113 TC_STATS(ngetbintime); TC_STATS(ngetnanotime); TC_STATS(ngetmicrotime);
118 static void tc_windup(void);
121 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
126 if (req->flags & SCTL_MASK32) {
127 tv[0] = boottime.tv_sec;
128 tv[1] = boottime.tv_usec;
129 return SYSCTL_OUT(req, tv, sizeof(tv));
132 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
135 * Return the difference between the timehands' counter value now and what
136 * was when we copied it to the timehands' offset_count.
138 static __inline u_int
139 tc_delta(struct timehands *th)
141 struct timecounter *tc;
144 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
145 tc->tc_counter_mask);
149 * Functions for reading the time. We have to loop until we are sure that
150 * the timehands that we operated on was not updated under our feet. See
151 * the comment in <sys/time.h> for a description of these 12 functions.
155 binuptime(struct bintime *bt)
157 struct timehands *th;
163 gen = th->th_generation;
165 bintime_addx(bt, th->th_scale * tc_delta(th));
166 } while (gen == 0 || gen != th->th_generation);
170 nanouptime(struct timespec *tsp)
176 bintime2timespec(&bt, tsp);
180 microuptime(struct timeval *tvp)
186 bintime2timeval(&bt, tvp);
190 bintime(struct bintime *bt)
195 bintime_add(bt, &boottimebin);
199 nanotime(struct timespec *tsp)
205 bintime2timespec(&bt, tsp);
209 microtime(struct timeval *tvp)
215 bintime2timeval(&bt, tvp);
219 getbinuptime(struct bintime *bt)
221 struct timehands *th;
227 gen = th->th_generation;
229 } while (gen == 0 || gen != th->th_generation);
233 getnanouptime(struct timespec *tsp)
235 struct timehands *th;
241 gen = th->th_generation;
242 bintime2timespec(&th->th_offset, tsp);
243 } while (gen == 0 || gen != th->th_generation);
247 getmicrouptime(struct timeval *tvp)
249 struct timehands *th;
255 gen = th->th_generation;
256 bintime2timeval(&th->th_offset, tvp);
257 } while (gen == 0 || gen != th->th_generation);
261 getbintime(struct bintime *bt)
263 struct timehands *th;
269 gen = th->th_generation;
271 } while (gen == 0 || gen != th->th_generation);
272 bintime_add(bt, &boottimebin);
276 getnanotime(struct timespec *tsp)
278 struct timehands *th;
284 gen = th->th_generation;
285 *tsp = th->th_nanotime;
286 } while (gen == 0 || gen != th->th_generation);
290 getmicrotime(struct timeval *tvp)
292 struct timehands *th;
298 gen = th->th_generation;
299 *tvp = th->th_microtime;
300 } while (gen == 0 || gen != th->th_generation);
304 * Initialize a new timecounter and possibly use it.
307 tc_init(struct timecounter *tc)
311 u = tc->tc_frequency / tc->tc_counter_mask;
312 /* XXX: We need some margin here, 10% is a guess */
315 if (u > hz && tc->tc_quality >= 0) {
316 tc->tc_quality = -2000;
318 printf("Timecounter \"%s\" frequency %ju Hz",
319 tc->tc_name, (uintmax_t)tc->tc_frequency);
320 printf(" -- Insufficient hz, needs at least %u\n", u);
322 } else if (tc->tc_quality >= 0 || bootverbose) {
323 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
324 tc->tc_name, (uintmax_t)tc->tc_frequency,
328 tc->tc_next = timecounters;
331 * Never automatically use a timecounter with negative quality.
332 * Even though we run on the dummy counter, switching here may be
333 * worse since this timecounter may not be monotonous.
335 if (tc->tc_quality < 0)
337 if (tc->tc_quality < timecounter->tc_quality)
339 if (tc->tc_quality == timecounter->tc_quality &&
340 tc->tc_frequency < timecounter->tc_frequency)
342 (void)tc->tc_get_timecount(tc);
343 (void)tc->tc_get_timecount(tc);
347 /* Report the frequency of the current timecounter. */
349 tc_getfrequency(void)
352 return (timehands->th_counter->tc_frequency);
356 * Step our concept of UTC. This is done by modifying our estimate of
361 tc_setclock(struct timespec *ts)
364 struct bintime bt, bt2;
368 timespec2bintime(ts, &bt);
369 bintime_sub(&bt, &bt2);
370 bintime_add(&bt2, &boottimebin);
372 bintime2timeval(&bt, &boottime);
374 /* XXX fiddle all the little crinkly bits around the fiords... */
376 if (timestepwarnings) {
377 bintime2timespec(&bt2, &ts2);
378 log(LOG_INFO, "Time stepped from %jd.%09ld to %jd.%09ld\n",
379 (intmax_t)ts2.tv_sec, ts2.tv_nsec,
380 (intmax_t)ts->tv_sec, ts->tv_nsec);
385 * Initialize the next struct timehands in the ring and make
386 * it the active timehands. Along the way we might switch to a different
387 * timecounter and/or do seconds processing in NTP. Slightly magic.
393 struct timehands *th, *tho;
395 u_int delta, ncount, ogen;
400 * Make the next timehands a copy of the current one, but do not
401 * overwrite the generation or next pointer. While we update
402 * the contents, the generation must be zero.
406 ogen = th->th_generation;
407 th->th_generation = 0;
408 bcopy(tho, th, offsetof(struct timehands, th_generation));
411 * Capture a timecounter delta on the current timecounter and if
412 * changing timecounters, a counter value from the new timecounter.
413 * Update the offset fields accordingly.
415 delta = tc_delta(th);
416 if (th->th_counter != timecounter)
417 ncount = timecounter->tc_get_timecount(timecounter);
420 th->th_offset_count += delta;
421 th->th_offset_count &= th->th_counter->tc_counter_mask;
422 bintime_addx(&th->th_offset, th->th_scale * delta);
425 * Hardware latching timecounters may not generate interrupts on
426 * PPS events, so instead we poll them. There is a finite risk that
427 * the hardware might capture a count which is later than the one we
428 * got above, and therefore possibly in the next NTP second which might
429 * have a different rate than the current NTP second. It doesn't
430 * matter in practice.
432 if (tho->th_counter->tc_poll_pps)
433 tho->th_counter->tc_poll_pps(tho->th_counter);
436 * Deal with NTP second processing. The for loop normally
437 * iterates at most once, but in extreme situations it might
438 * keep NTP sane if timeouts are not run for several seconds.
439 * At boot, the time step can be large when the TOD hardware
440 * has been read, so on really large steps, we call
441 * ntp_update_second only twice. We need to call it twice in
442 * case we missed a leap second.
445 bintime_add(&bt, &boottimebin);
446 i = bt.sec - tho->th_microtime.tv_sec;
451 ntp_update_second(&th->th_adjustment, &bt.sec);
453 boottimebin.sec += bt.sec - t;
455 /* Update the UTC timestamps used by the get*() functions. */
456 /* XXX shouldn't do this here. Should force non-`get' versions. */
457 bintime2timeval(&bt, &th->th_microtime);
458 bintime2timespec(&bt, &th->th_nanotime);
460 /* Now is a good time to change timecounters. */
461 if (th->th_counter != timecounter) {
462 th->th_counter = timecounter;
463 th->th_offset_count = ncount;
467 * Recalculate the scaling factor. We want the number of 1/2^64
468 * fractions of a second per period of the hardware counter, taking
469 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
470 * processing provides us with.
472 * The th_adjustment is nanoseconds per second with 32 bit binary
473 * fraction and we want 64 bit binary fraction of second:
475 * x = a * 2^32 / 10^9 = a * 4.294967296
477 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
478 * we can only multiply by about 850 without overflowing, but that
479 * leaves suitably precise fractions for multiply before divide.
481 * Divide before multiply with a fraction of 2199/512 results in a
482 * systematic undercompensation of 10PPM of th_adjustment. On a
483 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
485 * We happily sacrifice the lowest of the 64 bits of our result
486 * to the goddess of code clarity.
489 scale = (u_int64_t)1 << 63;
490 scale += (th->th_adjustment / 1024) * 2199;
491 scale /= th->th_counter->tc_frequency;
492 th->th_scale = scale * 2;
495 * Now that the struct timehands is again consistent, set the new
496 * generation number, making sure to not make it zero.
500 th->th_generation = ogen;
502 /* Go live with the new struct timehands. */
503 time_second = th->th_microtime.tv_sec;
504 time_uptime = th->th_offset.sec;
508 /* Report or change the active timecounter hardware. */
510 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
513 struct timecounter *newtc, *tc;
517 strlcpy(newname, tc->tc_name, sizeof(newname));
519 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
520 if (error != 0 || req->newptr == NULL ||
521 strcmp(newname, tc->tc_name) == 0)
523 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
524 if (strcmp(newname, newtc->tc_name) != 0)
527 /* Warm up new timecounter. */
528 (void)newtc->tc_get_timecount(newtc);
529 (void)newtc->tc_get_timecount(newtc);
537 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
538 0, 0, sysctl_kern_timecounter_hardware, "A", "");
541 /* Report or change the active timecounter hardware. */
543 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
546 struct timecounter *tc;
551 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
552 sprintf(buf, "%s%s(%d)",
553 spc, tc->tc_name, tc->tc_quality);
554 error = SYSCTL_OUT(req, buf, strlen(buf));
560 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
561 0, 0, sysctl_kern_timecounter_choice, "A", "");
564 * RFC 2783 PPS-API implementation.
568 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
571 struct pps_fetch_args *fapi;
573 struct pps_kcbind_args *kapi;
576 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
580 case PPS_IOC_DESTROY:
582 case PPS_IOC_SETPARAMS:
583 app = (pps_params_t *)data;
584 if (app->mode & ~pps->ppscap)
586 pps->ppsparam = *app;
588 case PPS_IOC_GETPARAMS:
589 app = (pps_params_t *)data;
590 *app = pps->ppsparam;
591 app->api_version = PPS_API_VERS_1;
594 *(int*)data = pps->ppscap;
597 fapi = (struct pps_fetch_args *)data;
598 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
600 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
602 pps->ppsinfo.current_mode = pps->ppsparam.mode;
603 fapi->pps_info_buf = pps->ppsinfo;
607 kapi = (struct pps_kcbind_args *)data;
608 /* XXX Only root should be able to do this */
609 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
611 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
613 if (kapi->edge & ~pps->ppscap)
615 pps->kcmode = kapi->edge;
626 pps_init(struct pps_state *pps)
628 pps->ppscap |= PPS_TSFMT_TSPEC;
629 if (pps->ppscap & PPS_CAPTUREASSERT)
630 pps->ppscap |= PPS_OFFSETASSERT;
631 if (pps->ppscap & PPS_CAPTURECLEAR)
632 pps->ppscap |= PPS_OFFSETCLEAR;
636 pps_capture(struct pps_state *pps)
638 struct timehands *th;
640 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
642 pps->capgen = th->th_generation;
644 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
645 if (pps->capgen != th->th_generation)
650 pps_event(struct pps_state *pps, int event)
653 struct timespec ts, *tsp, *osp;
654 u_int tcount, *pcount;
658 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
659 /* If the timecounter was wound up underneath us, bail out. */
660 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
663 /* Things would be easier with arrays. */
664 if (event == PPS_CAPTUREASSERT) {
665 tsp = &pps->ppsinfo.assert_timestamp;
666 osp = &pps->ppsparam.assert_offset;
667 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
668 fhard = pps->kcmode & PPS_CAPTUREASSERT;
669 pcount = &pps->ppscount[0];
670 pseq = &pps->ppsinfo.assert_sequence;
672 tsp = &pps->ppsinfo.clear_timestamp;
673 osp = &pps->ppsparam.clear_offset;
674 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
675 fhard = pps->kcmode & PPS_CAPTURECLEAR;
676 pcount = &pps->ppscount[1];
677 pseq = &pps->ppsinfo.clear_sequence;
681 * If the timecounter changed, we cannot compare the count values, so
682 * we have to drop the rest of the PPS-stuff until the next event.
684 if (pps->ppstc != pps->capth->th_counter) {
685 pps->ppstc = pps->capth->th_counter;
686 *pcount = pps->capcount;
687 pps->ppscount[2] = pps->capcount;
691 /* Convert the count to a timespec. */
692 tcount = pps->capcount - pps->capth->th_offset_count;
693 tcount &= pps->capth->th_counter->tc_counter_mask;
694 bt = pps->capth->th_offset;
695 bintime_addx(&bt, pps->capth->th_scale * tcount);
696 bintime_add(&bt, &boottimebin);
697 bintime2timespec(&bt, &ts);
699 /* If the timecounter was wound up underneath us, bail out. */
700 if (pps->capgen != pps->capth->th_generation)
703 *pcount = pps->capcount;
708 timespecadd(tsp, osp);
709 if (tsp->tv_nsec < 0) {
710 tsp->tv_nsec += 1000000000;
719 * Feed the NTP PLL/FLL.
720 * The FLL wants to know how many (hardware) nanoseconds
721 * elapsed since the previous event.
723 tcount = pps->capcount - pps->ppscount[2];
724 pps->ppscount[2] = pps->capcount;
725 tcount &= pps->capth->th_counter->tc_counter_mask;
726 scale = (u_int64_t)1 << 63;
727 scale /= pps->capth->th_counter->tc_frequency;
731 bintime_addx(&bt, scale * tcount);
732 bintime2timespec(&bt, &ts);
733 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
739 * Timecounters need to be updated every so often to prevent the hardware
740 * counter from overflowing. Updating also recalculates the cached values
741 * used by the get*() family of functions, so their precision depends on
742 * the update frequency.
746 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "");
753 if (++count < tc_tick)
760 inittimecounter(void *dummy)
765 * Set the initial timeout to
766 * max(1, <approx. number of hardclock ticks in a millisecond>).
767 * People should probably not use the sysctl to set the timeout
768 * to smaller than its inital value, since that value is the
769 * smallest reasonable one. If they want better timestamps they
770 * should use the non-"get"* functions.
773 tc_tick = (hz + 500) / 1000;
776 p = (tc_tick * 1000000) / hz;
777 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
779 /* warm up new timecounter (again) and get rolling. */
780 (void)timecounter->tc_get_timecount(timecounter);
781 (void)timecounter->tc_get_timecount(timecounter);
784 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL)