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