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$");
13 #include "opt_compat.h"
16 #include <sys/param.h>
17 #include <sys/kernel.h>
18 #include <sys/sysctl.h>
19 #include <sys/syslog.h>
20 #include <sys/systm.h>
21 #include <sys/timepps.h>
22 #include <sys/timetc.h>
23 #include <sys/timex.h>
27 * A large step happens on boot. This constant detects such steps.
28 * It is relatively small so that ntp_update_second gets called enough
29 * in the typical 'missed a couple of seconds' case, but doesn't loop
30 * forever when the time step is large.
32 #define LARGE_STEP 200
35 * Implement a dummy timecounter which we can use until we get a real one
36 * in the air. This allows the console and other early stuff to use
41 dummy_get_timecount(struct timecounter *tc)
48 static struct timecounter dummy_timecounter = {
49 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
53 /* These fields must be initialized by the driver. */
54 struct timecounter *th_counter;
55 int64_t th_adjustment;
57 u_int th_offset_count;
58 struct bintime th_offset;
59 struct timeval th_microtime;
60 struct timespec th_nanotime;
61 /* Fields not to be copied in tc_windup start with th_generation. */
62 volatile u_int th_generation;
63 struct timehands *th_next;
66 static struct timehands th0;
67 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
68 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
69 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
70 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
71 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
72 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
73 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
74 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
75 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
76 static struct timehands th0 = {
79 (uint64_t)-1 / 1000000,
88 static struct timehands *volatile timehands = &th0;
89 struct timecounter *timecounter = &dummy_timecounter;
90 static struct timecounter *timecounters = &dummy_timecounter;
92 int tc_min_ticktock_freq = 1;
94 volatile time_t time_second = 1;
95 volatile time_t time_uptime = 1;
97 struct bintime boottimebin;
98 struct timeval boottime;
99 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
100 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
101 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
103 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
104 static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
106 static int timestepwarnings;
107 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
108 ×tepwarnings, 0, "Log time steps");
110 static void tc_windup(void);
111 static void cpu_tick_calibrate(int);
113 void dtrace_getnanotime(struct timespec *tsp);
116 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
121 if (req->flags & SCTL_MASK32) {
122 tv[0] = boottime.tv_sec;
123 tv[1] = boottime.tv_usec;
124 return SYSCTL_OUT(req, tv, sizeof(tv));
127 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
131 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
134 struct timecounter *tc = arg1;
136 ncount = tc->tc_get_timecount(tc);
137 return sysctl_handle_int(oidp, &ncount, 0, req);
141 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
144 struct timecounter *tc = arg1;
146 freq = tc->tc_frequency;
147 return sysctl_handle_64(oidp, &freq, 0, req);
151 * Return the difference between the timehands' counter value now and what
152 * was when we copied it to the timehands' offset_count.
154 static __inline u_int
155 tc_delta(struct timehands *th)
157 struct timecounter *tc;
160 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
161 tc->tc_counter_mask);
165 * Functions for reading the time. We have to loop until we are sure that
166 * the timehands that we operated on was not updated under our feet. See
167 * the comment in <sys/time.h> for a description of these 12 functions.
171 binuptime(struct bintime *bt)
173 struct timehands *th;
178 gen = th->th_generation;
180 bintime_addx(bt, th->th_scale * tc_delta(th));
181 } while (gen == 0 || gen != th->th_generation);
185 nanouptime(struct timespec *tsp)
190 bintime2timespec(&bt, tsp);
194 microuptime(struct timeval *tvp)
199 bintime2timeval(&bt, tvp);
203 bintime(struct bintime *bt)
207 bintime_add(bt, &boottimebin);
211 nanotime(struct timespec *tsp)
216 bintime2timespec(&bt, tsp);
220 microtime(struct timeval *tvp)
225 bintime2timeval(&bt, tvp);
229 getbinuptime(struct bintime *bt)
231 struct timehands *th;
236 gen = th->th_generation;
238 } while (gen == 0 || gen != th->th_generation);
242 getnanouptime(struct timespec *tsp)
244 struct timehands *th;
249 gen = th->th_generation;
250 bintime2timespec(&th->th_offset, tsp);
251 } while (gen == 0 || gen != th->th_generation);
255 getmicrouptime(struct timeval *tvp)
257 struct timehands *th;
262 gen = th->th_generation;
263 bintime2timeval(&th->th_offset, tvp);
264 } while (gen == 0 || gen != th->th_generation);
268 getbintime(struct bintime *bt)
270 struct timehands *th;
275 gen = th->th_generation;
277 } while (gen == 0 || gen != th->th_generation);
278 bintime_add(bt, &boottimebin);
282 getnanotime(struct timespec *tsp)
284 struct timehands *th;
289 gen = th->th_generation;
290 *tsp = th->th_nanotime;
291 } while (gen == 0 || gen != th->th_generation);
295 getmicrotime(struct timeval *tvp)
297 struct timehands *th;
302 gen = th->th_generation;
303 *tvp = th->th_microtime;
304 } while (gen == 0 || gen != th->th_generation);
308 * This is a clone of getnanotime and used for walltimestamps.
309 * The dtrace_ prefix prevents fbt from creating probes for
310 * it so walltimestamp can be safely used in all fbt probes.
313 dtrace_getnanotime(struct timespec *tsp)
315 struct timehands *th;
320 gen = th->th_generation;
321 *tsp = th->th_nanotime;
322 } while (gen == 0 || gen != th->th_generation);
326 * Initialize a new timecounter and possibly use it.
329 tc_init(struct timecounter *tc)
332 struct sysctl_oid *tc_root;
334 u = tc->tc_frequency / tc->tc_counter_mask;
335 /* XXX: We need some margin here, 10% is a guess */
338 if (u > hz && tc->tc_quality >= 0) {
339 tc->tc_quality = -2000;
341 printf("Timecounter \"%s\" frequency %ju Hz",
342 tc->tc_name, (uintmax_t)tc->tc_frequency);
343 printf(" -- Insufficient hz, needs at least %u\n", u);
345 } else if (tc->tc_quality >= 0 || bootverbose) {
346 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
347 tc->tc_name, (uintmax_t)tc->tc_frequency,
351 tc->tc_next = timecounters;
354 * Set up sysctl tree for this counter.
356 tc_root = SYSCTL_ADD_NODE(NULL,
357 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
358 CTLFLAG_RW, 0, "timecounter description");
359 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
360 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
361 "mask for implemented bits");
362 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
363 "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
364 sysctl_kern_timecounter_get, "IU", "current timecounter value");
365 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
366 "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
367 sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
368 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
369 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
370 "goodness of time counter");
372 * Never automatically use a timecounter with negative quality.
373 * Even though we run on the dummy counter, switching here may be
374 * worse since this timecounter may not be monotonous.
376 if (tc->tc_quality < 0)
378 if (tc->tc_quality < timecounter->tc_quality)
380 if (tc->tc_quality == timecounter->tc_quality &&
381 tc->tc_frequency < timecounter->tc_frequency)
383 (void)tc->tc_get_timecount(tc);
384 (void)tc->tc_get_timecount(tc);
388 /* Report the frequency of the current timecounter. */
390 tc_getfrequency(void)
393 return (timehands->th_counter->tc_frequency);
397 * Step our concept of UTC. This is done by modifying our estimate of
402 tc_setclock(struct timespec *ts)
404 struct timespec tbef, taft;
405 struct bintime bt, bt2;
407 cpu_tick_calibrate(1);
409 timespec2bintime(ts, &bt);
411 bintime_sub(&bt, &bt2);
412 bintime_add(&bt2, &boottimebin);
414 bintime2timeval(&bt, &boottime);
416 /* XXX fiddle all the little crinkly bits around the fiords... */
419 if (timestepwarnings) {
421 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
422 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
423 (intmax_t)taft.tv_sec, taft.tv_nsec,
424 (intmax_t)ts->tv_sec, ts->tv_nsec);
426 cpu_tick_calibrate(1);
430 * Initialize the next struct timehands in the ring and make
431 * it the active timehands. Along the way we might switch to a different
432 * timecounter and/or do seconds processing in NTP. Slightly magic.
438 struct timehands *th, *tho;
440 u_int delta, ncount, ogen;
445 * Make the next timehands a copy of the current one, but do not
446 * overwrite the generation or next pointer. While we update
447 * the contents, the generation must be zero.
451 ogen = th->th_generation;
452 th->th_generation = 0;
453 bcopy(tho, th, offsetof(struct timehands, th_generation));
456 * Capture a timecounter delta on the current timecounter and if
457 * changing timecounters, a counter value from the new timecounter.
458 * Update the offset fields accordingly.
460 delta = tc_delta(th);
461 if (th->th_counter != timecounter)
462 ncount = timecounter->tc_get_timecount(timecounter);
465 th->th_offset_count += delta;
466 th->th_offset_count &= th->th_counter->tc_counter_mask;
467 while (delta > th->th_counter->tc_frequency) {
468 /* Eat complete unadjusted seconds. */
469 delta -= th->th_counter->tc_frequency;
472 if ((delta > th->th_counter->tc_frequency / 2) &&
473 (th->th_scale * delta < ((uint64_t)1 << 63))) {
474 /* The product th_scale * delta just barely overflows. */
477 bintime_addx(&th->th_offset, th->th_scale * delta);
480 * Hardware latching timecounters may not generate interrupts on
481 * PPS events, so instead we poll them. There is a finite risk that
482 * the hardware might capture a count which is later than the one we
483 * got above, and therefore possibly in the next NTP second which might
484 * have a different rate than the current NTP second. It doesn't
485 * matter in practice.
487 if (tho->th_counter->tc_poll_pps)
488 tho->th_counter->tc_poll_pps(tho->th_counter);
491 * Deal with NTP second processing. The for loop normally
492 * iterates at most once, but in extreme situations it might
493 * keep NTP sane if timeouts are not run for several seconds.
494 * At boot, the time step can be large when the TOD hardware
495 * has been read, so on really large steps, we call
496 * ntp_update_second only twice. We need to call it twice in
497 * case we missed a leap second.
500 bintime_add(&bt, &boottimebin);
501 i = bt.sec - tho->th_microtime.tv_sec;
506 ntp_update_second(&th->th_adjustment, &bt.sec);
508 boottimebin.sec += bt.sec - t;
510 /* Update the UTC timestamps used by the get*() functions. */
511 /* XXX shouldn't do this here. Should force non-`get' versions. */
512 bintime2timeval(&bt, &th->th_microtime);
513 bintime2timespec(&bt, &th->th_nanotime);
515 /* Now is a good time to change timecounters. */
516 if (th->th_counter != timecounter) {
518 if ((timecounter->tc_flags & TC_FLAGS_C3STOP) != 0)
519 cpu_disable_deep_sleep++;
520 if ((th->th_counter->tc_flags & TC_FLAGS_C3STOP) != 0)
521 cpu_disable_deep_sleep--;
523 th->th_counter = timecounter;
524 th->th_offset_count = ncount;
525 tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
526 (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
530 * Recalculate the scaling factor. We want the number of 1/2^64
531 * fractions of a second per period of the hardware counter, taking
532 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
533 * processing provides us with.
535 * The th_adjustment is nanoseconds per second with 32 bit binary
536 * fraction and we want 64 bit binary fraction of second:
538 * x = a * 2^32 / 10^9 = a * 4.294967296
540 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
541 * we can only multiply by about 850 without overflowing, that
542 * leaves no suitably precise fractions for multiply before divide.
544 * Divide before multiply with a fraction of 2199/512 results in a
545 * systematic undercompensation of 10PPM of th_adjustment. On a
546 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
548 * We happily sacrifice the lowest of the 64 bits of our result
549 * to the goddess of code clarity.
552 scale = (uint64_t)1 << 63;
553 scale += (th->th_adjustment / 1024) * 2199;
554 scale /= th->th_counter->tc_frequency;
555 th->th_scale = scale * 2;
558 * Now that the struct timehands is again consistent, set the new
559 * generation number, making sure to not make it zero.
563 th->th_generation = ogen;
565 /* Go live with the new struct timehands. */
566 time_second = th->th_microtime.tv_sec;
567 time_uptime = th->th_offset.sec;
569 timekeep_push_vdso();
572 /* Report or change the active timecounter hardware. */
574 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
577 struct timecounter *newtc, *tc;
581 strlcpy(newname, tc->tc_name, sizeof(newname));
583 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
584 if (error != 0 || req->newptr == NULL ||
585 strcmp(newname, tc->tc_name) == 0)
587 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
588 if (strcmp(newname, newtc->tc_name) != 0)
591 /* Warm up new timecounter. */
592 (void)newtc->tc_get_timecount(newtc);
593 (void)newtc->tc_get_timecount(newtc);
596 timekeep_push_vdso();
602 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
603 0, 0, sysctl_kern_timecounter_hardware, "A",
604 "Timecounter hardware selected");
607 /* Report or change the active timecounter hardware. */
609 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
612 struct timecounter *tc;
617 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
618 sprintf(buf, "%s%s(%d)",
619 spc, tc->tc_name, tc->tc_quality);
620 error = SYSCTL_OUT(req, buf, strlen(buf));
626 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
627 0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
630 * RFC 2783 PPS-API implementation.
634 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
637 struct pps_fetch_args *fapi;
639 struct pps_kcbind_args *kapi;
642 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
646 case PPS_IOC_DESTROY:
648 case PPS_IOC_SETPARAMS:
649 app = (pps_params_t *)data;
650 if (app->mode & ~pps->ppscap)
652 pps->ppsparam = *app;
654 case PPS_IOC_GETPARAMS:
655 app = (pps_params_t *)data;
656 *app = pps->ppsparam;
657 app->api_version = PPS_API_VERS_1;
660 *(int*)data = pps->ppscap;
663 fapi = (struct pps_fetch_args *)data;
664 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
666 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
668 pps->ppsinfo.current_mode = pps->ppsparam.mode;
669 fapi->pps_info_buf = pps->ppsinfo;
673 kapi = (struct pps_kcbind_args *)data;
674 /* XXX Only root should be able to do this */
675 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
677 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
679 if (kapi->edge & ~pps->ppscap)
681 pps->kcmode = kapi->edge;
692 pps_init(struct pps_state *pps)
694 pps->ppscap |= PPS_TSFMT_TSPEC;
695 if (pps->ppscap & PPS_CAPTUREASSERT)
696 pps->ppscap |= PPS_OFFSETASSERT;
697 if (pps->ppscap & PPS_CAPTURECLEAR)
698 pps->ppscap |= PPS_OFFSETCLEAR;
702 pps_capture(struct pps_state *pps)
704 struct timehands *th;
706 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
708 pps->capgen = th->th_generation;
710 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
711 if (pps->capgen != th->th_generation)
716 pps_event(struct pps_state *pps, int event)
719 struct timespec ts, *tsp, *osp;
720 u_int tcount, *pcount;
724 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
725 /* If the timecounter was wound up underneath us, bail out. */
726 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
729 /* Things would be easier with arrays. */
730 if (event == PPS_CAPTUREASSERT) {
731 tsp = &pps->ppsinfo.assert_timestamp;
732 osp = &pps->ppsparam.assert_offset;
733 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
734 fhard = pps->kcmode & PPS_CAPTUREASSERT;
735 pcount = &pps->ppscount[0];
736 pseq = &pps->ppsinfo.assert_sequence;
738 tsp = &pps->ppsinfo.clear_timestamp;
739 osp = &pps->ppsparam.clear_offset;
740 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
741 fhard = pps->kcmode & PPS_CAPTURECLEAR;
742 pcount = &pps->ppscount[1];
743 pseq = &pps->ppsinfo.clear_sequence;
747 * If the timecounter changed, we cannot compare the count values, so
748 * we have to drop the rest of the PPS-stuff until the next event.
750 if (pps->ppstc != pps->capth->th_counter) {
751 pps->ppstc = pps->capth->th_counter;
752 *pcount = pps->capcount;
753 pps->ppscount[2] = pps->capcount;
757 /* Convert the count to a timespec. */
758 tcount = pps->capcount - pps->capth->th_offset_count;
759 tcount &= pps->capth->th_counter->tc_counter_mask;
760 bt = pps->capth->th_offset;
761 bintime_addx(&bt, pps->capth->th_scale * tcount);
762 bintime_add(&bt, &boottimebin);
763 bintime2timespec(&bt, &ts);
765 /* If the timecounter was wound up underneath us, bail out. */
766 if (pps->capgen != pps->capth->th_generation)
769 *pcount = pps->capcount;
774 timespecadd(tsp, osp);
775 if (tsp->tv_nsec < 0) {
776 tsp->tv_nsec += 1000000000;
785 * Feed the NTP PLL/FLL.
786 * The FLL wants to know how many (hardware) nanoseconds
787 * elapsed since the previous event.
789 tcount = pps->capcount - pps->ppscount[2];
790 pps->ppscount[2] = pps->capcount;
791 tcount &= pps->capth->th_counter->tc_counter_mask;
792 scale = (uint64_t)1 << 63;
793 scale /= pps->capth->th_counter->tc_frequency;
797 bintime_addx(&bt, scale * tcount);
798 bintime2timespec(&bt, &ts);
799 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
805 * Timecounters need to be updated every so often to prevent the hardware
806 * counter from overflowing. Updating also recalculates the cached values
807 * used by the get*() family of functions, so their precision depends on
808 * the update frequency.
812 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
813 "Approximate number of hardclock ticks in a millisecond");
828 inittimecounter(void *dummy)
833 * Set the initial timeout to
834 * max(1, <approx. number of hardclock ticks in a millisecond>).
835 * People should probably not use the sysctl to set the timeout
836 * to smaller than its inital value, since that value is the
837 * smallest reasonable one. If they want better timestamps they
838 * should use the non-"get"* functions.
841 tc_tick = (hz + 500) / 1000;
844 p = (tc_tick * 1000000) / hz;
845 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
847 /* warm up new timecounter (again) and get rolling. */
848 (void)timecounter->tc_get_timecount(timecounter);
849 (void)timecounter->tc_get_timecount(timecounter);
853 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
855 /* Cpu tick handling -------------------------------------------------*/
857 static int cpu_tick_variable;
858 static uint64_t cpu_tick_frequency;
863 static uint64_t base;
864 static unsigned last;
866 struct timecounter *tc;
868 tc = timehands->th_counter;
869 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
871 base += (uint64_t)tc->tc_counter_mask + 1;
877 cpu_tick_calibration(void)
879 static time_t last_calib;
881 if (time_uptime != last_calib && !(time_uptime & 0xf)) {
882 cpu_tick_calibrate(0);
883 last_calib = time_uptime;
888 * This function gets called every 16 seconds on only one designated
889 * CPU in the system from hardclock() via cpu_tick_calibration()().
891 * Whenever the real time clock is stepped we get called with reset=1
892 * to make sure we handle suspend/resume and similar events correctly.
896 cpu_tick_calibrate(int reset)
898 static uint64_t c_last;
899 uint64_t c_this, c_delta;
900 static struct bintime t_last;
901 struct bintime t_this, t_delta;
905 /* The clock was stepped, abort & reset */
910 /* we don't calibrate fixed rate cputicks */
911 if (!cpu_tick_variable)
914 getbinuptime(&t_this);
915 c_this = cpu_ticks();
916 if (t_last.sec != 0) {
917 c_delta = c_this - c_last;
919 bintime_sub(&t_delta, &t_last);
922 * 2^(64-20) / 16[s] =
924 * 17.592.186.044.416 / 16 =
925 * 1.099.511.627.776 [Hz]
927 divi = t_delta.sec << 20;
928 divi |= t_delta.frac >> (64 - 20);
931 if (c_delta > cpu_tick_frequency) {
932 if (0 && bootverbose)
933 printf("cpu_tick increased to %ju Hz\n",
935 cpu_tick_frequency = c_delta;
943 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
947 cpu_ticks = tc_cpu_ticks;
949 cpu_tick_frequency = freq;
950 cpu_tick_variable = var;
959 if (cpu_ticks == tc_cpu_ticks)
960 return (tc_getfrequency());
961 return (cpu_tick_frequency);
965 * We need to be slightly careful converting cputicks to microseconds.
966 * There is plenty of margin in 64 bits of microseconds (half a million
967 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
968 * before divide conversion (to retain precision) we find that the
969 * margin shrinks to 1.5 hours (one millionth of 146y).
970 * With a three prong approach we never lose significant bits, no
971 * matter what the cputick rate and length of timeinterval is.
975 cputick2usec(uint64_t tick)
978 if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */
979 return (tick / (cpu_tickrate() / 1000000LL));
980 else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */
981 return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
983 return ((tick * 1000000LL) / cpu_tickrate());
986 cpu_tick_f *cpu_ticks = tc_cpu_ticks;
988 static int vdso_th_enable = 1;
990 sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
992 int old_vdso_th_enable, error;
994 old_vdso_th_enable = vdso_th_enable;
995 error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
998 vdso_th_enable = old_vdso_th_enable;
999 timekeep_push_vdso();
1002 SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
1003 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1004 NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
1007 tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
1009 struct timehands *th;
1013 vdso_th->th_algo = VDSO_TH_ALGO_1;
1014 vdso_th->th_scale = th->th_scale;
1015 vdso_th->th_offset_count = th->th_offset_count;
1016 vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
1017 vdso_th->th_offset = th->th_offset;
1018 vdso_th->th_boottime = boottimebin;
1019 enabled = cpu_fill_vdso_timehands(vdso_th);
1020 if (!vdso_th_enable)
1025 #ifdef COMPAT_FREEBSD32
1027 tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
1029 struct timehands *th;
1033 vdso_th32->th_algo = VDSO_TH_ALGO_1;
1034 *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
1035 vdso_th32->th_offset_count = th->th_offset_count;
1036 vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
1037 vdso_th32->th_offset.sec = th->th_offset.sec;
1038 *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
1039 vdso_th32->th_boottime.sec = boottimebin.sec;
1040 *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
1041 enabled = cpu_fill_vdso_timehands32(vdso_th32);
1042 if (!vdso_th_enable)