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