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