Home | History | Annotate | Line # | Download | only in kern
kern_tc.c revision 1.36.2.1
      1 /* $NetBSD: kern_tc.c,v 1.36.2.1 2008/07/28 14:37:36 simonb 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.36.2.1 2008/07/28 14:37:36 simonb 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 				goto retry;
    222 			}
    223 			spc = " ";
    224 			where += slen;
    225 			needed += slen;
    226 			left -= slen;
    227 		}
    228 	}
    229 	mutex_spin_exit(&timecounter_lock);
    230 
    231 	*oldlenp = needed;
    232 	return (error);
    233 }
    234 
    235 SYSCTL_SETUP(sysctl_timecounter_setup, "sysctl timecounter setup")
    236 {
    237 	const struct sysctlnode *node;
    238 
    239 	sysctl_createv(clog, 0, NULL, &node,
    240 		       CTLFLAG_PERMANENT,
    241 		       CTLTYPE_NODE, "timecounter",
    242 		       SYSCTL_DESCR("time counter information"),
    243 		       NULL, 0, NULL, 0,
    244 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    245 
    246 	if (node != NULL) {
    247 		sysctl_createv(clog, 0, NULL, NULL,
    248 			       CTLFLAG_PERMANENT,
    249 			       CTLTYPE_STRING, "choice",
    250 			       SYSCTL_DESCR("available counters"),
    251 			       sysctl_kern_timecounter_choice, 0, NULL, 0,
    252 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    253 
    254 		sysctl_createv(clog, 0, NULL, NULL,
    255 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    256 			       CTLTYPE_STRING, "hardware",
    257 			       SYSCTL_DESCR("currently active time counter"),
    258 			       sysctl_kern_timecounter_hardware, 0, NULL, MAX_TCNAMELEN,
    259 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    260 
    261 		sysctl_createv(clog, 0, NULL, NULL,
    262 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    263 			       CTLTYPE_INT, "timestepwarnings",
    264 			       SYSCTL_DESCR("log time steps"),
    265 			       NULL, 0, &timestepwarnings, 0,
    266 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    267 	}
    268 }
    269 
    270 #ifdef TC_COUNTERS
    271 #define	TC_STATS(name)							\
    272 static struct evcnt n##name =						\
    273     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "timecounter", #name);	\
    274 EVCNT_ATTACH_STATIC(n##name)
    275 TC_STATS(binuptime);    TC_STATS(nanouptime);    TC_STATS(microuptime);
    276 TC_STATS(bintime);      TC_STATS(nanotime);      TC_STATS(microtime);
    277 TC_STATS(getbinuptime); TC_STATS(getnanouptime); TC_STATS(getmicrouptime);
    278 TC_STATS(getbintime);   TC_STATS(getnanotime);   TC_STATS(getmicrotime);
    279 TC_STATS(setclock);
    280 #define	TC_COUNT(var)	var.ev_count++
    281 #undef TC_STATS
    282 #else
    283 #define	TC_COUNT(var)	/* nothing */
    284 #endif	/* TC_COUNTERS */
    285 
    286 static void tc_windup(void);
    287 
    288 /*
    289  * Return the difference between the timehands' counter value now and what
    290  * was when we copied it to the timehands' offset_count.
    291  */
    292 static __inline u_int
    293 tc_delta(struct timehands *th)
    294 {
    295 	struct timecounter *tc;
    296 
    297 	tc = th->th_counter;
    298 	return ((tc->tc_get_timecount(tc) -
    299 		 th->th_offset_count) & tc->tc_counter_mask);
    300 }
    301 
    302 /*
    303  * Functions for reading the time.  We have to loop until we are sure that
    304  * the timehands that we operated on was not updated under our feet.  See
    305  * the comment in <sys/timevar.h> for a description of these 12 functions.
    306  */
    307 
    308 void
    309 binuptime(struct bintime *bt)
    310 {
    311 	struct timehands *th;
    312 	u_int gen;
    313 
    314 	TC_COUNT(nbinuptime);
    315 	do {
    316 		th = timehands;
    317 		gen = th->th_generation;
    318 		*bt = th->th_offset;
    319 		bintime_addx(bt, th->th_scale * tc_delta(th));
    320 	} while (gen == 0 || gen != th->th_generation);
    321 }
    322 
    323 void
    324 nanouptime(struct timespec *tsp)
    325 {
    326 	struct bintime bt;
    327 
    328 	TC_COUNT(nnanouptime);
    329 	binuptime(&bt);
    330 	bintime2timespec(&bt, tsp);
    331 }
    332 
    333 void
    334 microuptime(struct timeval *tvp)
    335 {
    336 	struct bintime bt;
    337 
    338 	TC_COUNT(nmicrouptime);
    339 	binuptime(&bt);
    340 	bintime2timeval(&bt, tvp);
    341 }
    342 
    343 void
    344 bintime(struct bintime *bt)
    345 {
    346 
    347 	TC_COUNT(nbintime);
    348 	binuptime(bt);
    349 	bintime_add(bt, &timebasebin);
    350 }
    351 
    352 void
    353 nanotime(struct timespec *tsp)
    354 {
    355 	struct bintime bt;
    356 
    357 	TC_COUNT(nnanotime);
    358 	bintime(&bt);
    359 	bintime2timespec(&bt, tsp);
    360 }
    361 
    362 void
    363 microtime(struct timeval *tvp)
    364 {
    365 	struct bintime bt;
    366 
    367 	TC_COUNT(nmicrotime);
    368 	bintime(&bt);
    369 	bintime2timeval(&bt, tvp);
    370 }
    371 
    372 void
    373 getbinuptime(struct bintime *bt)
    374 {
    375 	struct timehands *th;
    376 	u_int gen;
    377 
    378 	TC_COUNT(ngetbinuptime);
    379 	do {
    380 		th = timehands;
    381 		gen = th->th_generation;
    382 		*bt = th->th_offset;
    383 	} while (gen == 0 || gen != th->th_generation);
    384 }
    385 
    386 void
    387 getnanouptime(struct timespec *tsp)
    388 {
    389 	struct timehands *th;
    390 	u_int gen;
    391 
    392 	TC_COUNT(ngetnanouptime);
    393 	do {
    394 		th = timehands;
    395 		gen = th->th_generation;
    396 		bintime2timespec(&th->th_offset, tsp);
    397 	} while (gen == 0 || gen != th->th_generation);
    398 }
    399 
    400 void
    401 getmicrouptime(struct timeval *tvp)
    402 {
    403 	struct timehands *th;
    404 	u_int gen;
    405 
    406 	TC_COUNT(ngetmicrouptime);
    407 	do {
    408 		th = timehands;
    409 		gen = th->th_generation;
    410 		bintime2timeval(&th->th_offset, tvp);
    411 	} while (gen == 0 || gen != th->th_generation);
    412 }
    413 
    414 void
    415 getbintime(struct bintime *bt)
    416 {
    417 	struct timehands *th;
    418 	u_int gen;
    419 
    420 	TC_COUNT(ngetbintime);
    421 	do {
    422 		th = timehands;
    423 		gen = th->th_generation;
    424 		*bt = th->th_offset;
    425 	} while (gen == 0 || gen != th->th_generation);
    426 	bintime_add(bt, &timebasebin);
    427 }
    428 
    429 void
    430 getnanotime(struct timespec *tsp)
    431 {
    432 	struct timehands *th;
    433 	u_int gen;
    434 
    435 	TC_COUNT(ngetnanotime);
    436 	do {
    437 		th = timehands;
    438 		gen = th->th_generation;
    439 		*tsp = th->th_nanotime;
    440 	} while (gen == 0 || gen != th->th_generation);
    441 }
    442 
    443 void
    444 getmicrotime(struct timeval *tvp)
    445 {
    446 	struct timehands *th;
    447 	u_int gen;
    448 
    449 	TC_COUNT(ngetmicrotime);
    450 	do {
    451 		th = timehands;
    452 		gen = th->th_generation;
    453 		*tvp = th->th_microtime;
    454 	} while (gen == 0 || gen != th->th_generation);
    455 }
    456 
    457 /*
    458  * Initialize a new timecounter and possibly use it.
    459  */
    460 void
    461 tc_init(struct timecounter *tc)
    462 {
    463 	u_int u;
    464 
    465 	u = tc->tc_frequency / tc->tc_counter_mask;
    466 	/* XXX: We need some margin here, 10% is a guess */
    467 	u *= 11;
    468 	u /= 10;
    469 	if (u > hz && tc->tc_quality >= 0) {
    470 		tc->tc_quality = -2000;
    471 		aprint_verbose(
    472 		    "timecounter: Timecounter \"%s\" frequency %ju Hz",
    473 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
    474 		aprint_verbose(" -- Insufficient hz, needs at least %u\n", u);
    475 	} else if (tc->tc_quality >= 0 || bootverbose) {
    476 		aprint_verbose(
    477 		    "timecounter: Timecounter \"%s\" frequency %ju Hz "
    478 		    "quality %d\n", tc->tc_name, (uintmax_t)tc->tc_frequency,
    479 		    tc->tc_quality);
    480 	}
    481 
    482 	mutex_spin_enter(&timecounter_lock);
    483 	tc->tc_next = timecounters;
    484 	timecounters = tc;
    485 	timecounter_mods++;
    486 	/*
    487 	 * Never automatically use a timecounter with negative quality.
    488 	 * Even though we run on the dummy counter, switching here may be
    489 	 * worse since this timecounter may not be monotonous.
    490 	 */
    491 	if (tc->tc_quality >= 0 && (tc->tc_quality > timecounter->tc_quality ||
    492 	    (tc->tc_quality == timecounter->tc_quality &&
    493 	    tc->tc_frequency > timecounter->tc_frequency))) {
    494 		(void)tc->tc_get_timecount(tc);
    495 		(void)tc->tc_get_timecount(tc);
    496 		timecounter = tc;
    497 		tc_windup();
    498 	}
    499 	mutex_spin_exit(&timecounter_lock);
    500 }
    501 
    502 /*
    503  * Pick a new timecounter due to the existing counter going bad.
    504  */
    505 static void
    506 tc_pick(void)
    507 {
    508 	struct timecounter *best, *tc;
    509 
    510 	KASSERT(mutex_owned(&timecounter_lock));
    511 
    512 	for (best = tc = timecounters; tc != NULL; tc = tc->tc_next) {
    513 		if (tc->tc_quality > best->tc_quality)
    514 			best = tc;
    515 		else if (tc->tc_quality < best->tc_quality)
    516 			continue;
    517 		else if (tc->tc_frequency > best->tc_frequency)
    518 			best = tc;
    519 	}
    520 	(void)best->tc_get_timecount(best);
    521 	(void)best->tc_get_timecount(best);
    522 	timecounter = best;
    523 }
    524 
    525 /*
    526  * A timecounter has gone bad, arrange to pick a new one at the next
    527  * clock tick.
    528  */
    529 void
    530 tc_gonebad(struct timecounter *tc)
    531 {
    532 
    533 	tc->tc_quality = -100;
    534 	membar_producer();
    535 	atomic_inc_uint(&timecounter_bad);
    536 }
    537 
    538 /*
    539  * Stop using a timecounter and remove it from the timecounters list.
    540  */
    541 int
    542 tc_detach(struct timecounter *target)
    543 {
    544 	struct timecounter *tc;
    545 	struct timecounter **tcp = NULL;
    546 	int rc = 0;
    547 
    548 	mutex_spin_enter(&timecounter_lock);
    549 	for (tcp = &timecounters, tc = timecounters;
    550 	     tc != NULL;
    551 	     tcp = &tc->tc_next, tc = tc->tc_next) {
    552 		if (tc == target)
    553 			break;
    554 	}
    555 	if (tc == NULL) {
    556 		rc = ESRCH;
    557 	} else {
    558 		*tcp = tc->tc_next;
    559 		if (timecounter == target) {
    560 			tc_pick();
    561 			tc_windup();
    562 		}
    563 		timecounter_mods++;
    564 	}
    565 	mutex_spin_exit(&timecounter_lock);
    566 	return rc;
    567 }
    568 
    569 /* Report the frequency of the current timecounter. */
    570 u_int64_t
    571 tc_getfrequency(void)
    572 {
    573 
    574 	return (timehands->th_counter->tc_frequency);
    575 }
    576 
    577 /*
    578  * Step our concept of UTC.  This is done by modifying our estimate of
    579  * when we booted.
    580  */
    581 void
    582 tc_setclock(struct timespec *ts)
    583 {
    584 	struct timespec ts2;
    585 	struct bintime bt, bt2;
    586 
    587 	mutex_spin_enter(&timecounter_lock);
    588 	TC_COUNT(nsetclock);
    589 	binuptime(&bt2);
    590 	timespec2bintime(ts, &bt);
    591 	bintime_sub(&bt, &bt2);
    592 	bintime_add(&bt2, &timebasebin);
    593 	timebasebin = bt;
    594 	tc_windup();
    595 	mutex_spin_exit(&timecounter_lock);
    596 
    597 	if (timestepwarnings) {
    598 		bintime2timespec(&bt2, &ts2);
    599 		log(LOG_INFO, "Time stepped from %jd.%09ld to %jd.%09ld\n",
    600 		    (intmax_t)ts2.tv_sec, ts2.tv_nsec,
    601 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
    602 	}
    603 }
    604 
    605 /*
    606  * Initialize the next struct timehands in the ring and make
    607  * it the active timehands.  Along the way we might switch to a different
    608  * timecounter and/or do seconds processing in NTP.  Slightly magic.
    609  */
    610 static void
    611 tc_windup(void)
    612 {
    613 	struct bintime bt;
    614 	struct timehands *th, *tho;
    615 	u_int64_t scale;
    616 	u_int delta, ncount, ogen;
    617 	int i, s_update;
    618 	time_t t;
    619 
    620 	KASSERT(mutex_owned(&timecounter_lock));
    621 
    622 	s_update = 0;
    623 
    624 	/*
    625 	 * Make the next timehands a copy of the current one, but do not
    626 	 * overwrite the generation or next pointer.  While we update
    627 	 * the contents, the generation must be zero.  Ensure global
    628 	 * visibility of the generation before proceeding.
    629 	 */
    630 	tho = timehands;
    631 	th = tho->th_next;
    632 	ogen = th->th_generation;
    633 	th->th_generation = 0;
    634 	membar_producer();
    635 	bcopy(tho, th, offsetof(struct timehands, th_generation));
    636 
    637 	/*
    638 	 * Capture a timecounter delta on the current timecounter and if
    639 	 * changing timecounters, a counter value from the new timecounter.
    640 	 * Update the offset fields accordingly.
    641 	 */
    642 	delta = tc_delta(th);
    643 	if (th->th_counter != timecounter)
    644 		ncount = timecounter->tc_get_timecount(timecounter);
    645 	else
    646 		ncount = 0;
    647 	th->th_offset_count += delta;
    648 	th->th_offset_count &= th->th_counter->tc_counter_mask;
    649 	bintime_addx(&th->th_offset, th->th_scale * delta);
    650 
    651 	/*
    652 	 * Hardware latching timecounters may not generate interrupts on
    653 	 * PPS events, so instead we poll them.  There is a finite risk that
    654 	 * the hardware might capture a count which is later than the one we
    655 	 * got above, and therefore possibly in the next NTP second which might
    656 	 * have a different rate than the current NTP second.  It doesn't
    657 	 * matter in practice.
    658 	 */
    659 	if (tho->th_counter->tc_poll_pps)
    660 		tho->th_counter->tc_poll_pps(tho->th_counter);
    661 
    662 	/*
    663 	 * Deal with NTP second processing.  The for loop normally
    664 	 * iterates at most once, but in extreme situations it might
    665 	 * keep NTP sane if timeouts are not run for several seconds.
    666 	 * At boot, the time step can be large when the TOD hardware
    667 	 * has been read, so on really large steps, we call
    668 	 * ntp_update_second only twice.  We need to call it twice in
    669 	 * case we missed a leap second.
    670 	 * If NTP is not compiled in ntp_update_second still calculates
    671 	 * the adjustment resulting from adjtime() calls.
    672 	 */
    673 	bt = th->th_offset;
    674 	bintime_add(&bt, &timebasebin);
    675 	i = bt.sec - tho->th_microtime.tv_sec;
    676 	if (i > LARGE_STEP)
    677 		i = 2;
    678 	for (; i > 0; i--) {
    679 		t = bt.sec;
    680 		ntp_update_second(&th->th_adjustment, &bt.sec);
    681 		s_update = 1;
    682 		if (bt.sec != t)
    683 			timebasebin.sec += bt.sec - t;
    684 	}
    685 
    686 	/* Update the UTC timestamps used by the get*() functions. */
    687 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
    688 	bintime2timeval(&bt, &th->th_microtime);
    689 	bintime2timespec(&bt, &th->th_nanotime);
    690 
    691 	/* Now is a good time to change timecounters. */
    692 	if (th->th_counter != timecounter) {
    693 		th->th_counter = timecounter;
    694 		th->th_offset_count = ncount;
    695 		s_update = 1;
    696 	}
    697 
    698 	/*-
    699 	 * Recalculate the scaling factor.  We want the number of 1/2^64
    700 	 * fractions of a second per period of the hardware counter, taking
    701 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
    702 	 * processing provides us with.
    703 	 *
    704 	 * The th_adjustment is nanoseconds per second with 32 bit binary
    705 	 * fraction and we want 64 bit binary fraction of second:
    706 	 *
    707 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
    708 	 *
    709 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
    710 	 * we can only multiply by about 850 without overflowing, but that
    711 	 * leaves suitably precise fractions for multiply before divide.
    712 	 *
    713 	 * Divide before multiply with a fraction of 2199/512 results in a
    714 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
    715 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
    716  	 *
    717 	 * We happily sacrifice the lowest of the 64 bits of our result
    718 	 * to the goddess of code clarity.
    719 	 *
    720 	 */
    721 	if (s_update) {
    722 		scale = (u_int64_t)1 << 63;
    723 		scale += (th->th_adjustment / 1024) * 2199;
    724 		scale /= th->th_counter->tc_frequency;
    725 		th->th_scale = scale * 2;
    726 	}
    727 	/*
    728 	 * Now that the struct timehands is again consistent, set the new
    729 	 * generation number, making sure to not make it zero.  Ensure
    730 	 * changes are globally visible before changing.
    731 	 */
    732 	if (++ogen == 0)
    733 		ogen = 1;
    734 	membar_producer();
    735 	th->th_generation = ogen;
    736 
    737 	/*
    738 	 * Go live with the new struct timehands.  Ensure changes are
    739 	 * globally visible before changing.
    740 	 */
    741 	time_second = th->th_microtime.tv_sec;
    742 	time_uptime = th->th_offset.sec;
    743 	membar_producer();
    744 	timehands = th;
    745 
    746 	/*
    747 	 * Force users of the old timehand to move on.  This is
    748 	 * necessary for MP systems; we need to ensure that the
    749 	 * consumers will move away from the old timehand before
    750 	 * we begin updating it again when we eventually wrap
    751 	 * around.
    752 	 */
    753 	if (++tho->th_generation == 0)
    754 		tho->th_generation = 1;
    755 }
    756 
    757 /*
    758  * RFC 2783 PPS-API implementation.
    759  */
    760 
    761 int
    762 pps_ioctl(u_long cmd, void *data, struct pps_state *pps)
    763 {
    764 	pps_params_t *app;
    765 	pps_info_t *pipi;
    766 #ifdef PPS_SYNC
    767 	int *epi;
    768 #endif
    769 
    770 	KASSERT(mutex_owned(&timecounter_lock));
    771 
    772 	KASSERT(pps != NULL); /* XXX ("NULL pps pointer in pps_ioctl") */
    773 	switch (cmd) {
    774 	case PPS_IOC_CREATE:
    775 		return (0);
    776 	case PPS_IOC_DESTROY:
    777 		return (0);
    778 	case PPS_IOC_SETPARAMS:
    779 		app = (pps_params_t *)data;
    780 		if (app->mode & ~pps->ppscap)
    781 			return (EINVAL);
    782 		pps->ppsparam = *app;
    783 		return (0);
    784 	case PPS_IOC_GETPARAMS:
    785 		app = (pps_params_t *)data;
    786 		*app = pps->ppsparam;
    787 		app->api_version = PPS_API_VERS_1;
    788 		return (0);
    789 	case PPS_IOC_GETCAP:
    790 		*(int*)data = pps->ppscap;
    791 		return (0);
    792 	case PPS_IOC_FETCH:
    793 		pipi = (pps_info_t *)data;
    794 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
    795 		*pipi = pps->ppsinfo;
    796 		return (0);
    797 	case PPS_IOC_KCBIND:
    798 #ifdef PPS_SYNC
    799 		epi = (int *)data;
    800 		/* XXX Only root should be able to do this */
    801 		if (*epi & ~pps->ppscap)
    802 			return (EINVAL);
    803 		pps->kcmode = *epi;
    804 		return (0);
    805 #else
    806 		return (EOPNOTSUPP);
    807 #endif
    808 	default:
    809 		return (EPASSTHROUGH);
    810 	}
    811 }
    812 
    813 void
    814 pps_init(struct pps_state *pps)
    815 {
    816 
    817 	KASSERT(mutex_owned(&timecounter_lock));
    818 
    819 	pps->ppscap |= PPS_TSFMT_TSPEC;
    820 	if (pps->ppscap & PPS_CAPTUREASSERT)
    821 		pps->ppscap |= PPS_OFFSETASSERT;
    822 	if (pps->ppscap & PPS_CAPTURECLEAR)
    823 		pps->ppscap |= PPS_OFFSETCLEAR;
    824 }
    825 
    826 void
    827 pps_capture(struct pps_state *pps)
    828 {
    829 	struct timehands *th;
    830 
    831 	KASSERT(mutex_owned(&timecounter_lock));
    832 	KASSERT(pps != NULL);
    833 
    834 	th = timehands;
    835 	pps->capgen = th->th_generation;
    836 	pps->capth = th;
    837 	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
    838 	if (pps->capgen != th->th_generation)
    839 		pps->capgen = 0;
    840 }
    841 
    842 void
    843 pps_event(struct pps_state *pps, int event)
    844 {
    845 	struct bintime bt;
    846 	struct timespec ts, *tsp, *osp;
    847 	u_int tcount, *pcount;
    848 	int foff, fhard;
    849 	pps_seq_t *pseq;
    850 
    851 	KASSERT(mutex_owned(&timecounter_lock));
    852 
    853 	KASSERT(pps != NULL); /* XXX ("NULL pps pointer in pps_event") */
    854 	/* If the timecounter was wound up underneath us, bail out. */
    855 	if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
    856 		return;
    857 
    858 	/* Things would be easier with arrays. */
    859 	if (event == PPS_CAPTUREASSERT) {
    860 		tsp = &pps->ppsinfo.assert_timestamp;
    861 		osp = &pps->ppsparam.assert_offset;
    862 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
    863 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
    864 		pcount = &pps->ppscount[0];
    865 		pseq = &pps->ppsinfo.assert_sequence;
    866 	} else {
    867 		tsp = &pps->ppsinfo.clear_timestamp;
    868 		osp = &pps->ppsparam.clear_offset;
    869 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
    870 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
    871 		pcount = &pps->ppscount[1];
    872 		pseq = &pps->ppsinfo.clear_sequence;
    873 	}
    874 
    875 	/*
    876 	 * If the timecounter changed, we cannot compare the count values, so
    877 	 * we have to drop the rest of the PPS-stuff until the next event.
    878 	 */
    879 	if (pps->ppstc != pps->capth->th_counter) {
    880 		pps->ppstc = pps->capth->th_counter;
    881 		*pcount = pps->capcount;
    882 		pps->ppscount[2] = pps->capcount;
    883 		return;
    884 	}
    885 
    886 	/* Convert the count to a timespec. */
    887 	tcount = pps->capcount - pps->capth->th_offset_count;
    888 	tcount &= pps->capth->th_counter->tc_counter_mask;
    889 	bt = pps->capth->th_offset;
    890 	bintime_addx(&bt, pps->capth->th_scale * tcount);
    891 	bintime_add(&bt, &timebasebin);
    892 	bintime2timespec(&bt, &ts);
    893 
    894 	/* If the timecounter was wound up underneath us, bail out. */
    895 	if (pps->capgen != pps->capth->th_generation)
    896 		return;
    897 
    898 	*pcount = pps->capcount;
    899 	(*pseq)++;
    900 	*tsp = ts;
    901 
    902 	if (foff) {
    903 		timespecadd(tsp, osp, tsp);
    904 		if (tsp->tv_nsec < 0) {
    905 			tsp->tv_nsec += 1000000000;
    906 			tsp->tv_sec -= 1;
    907 		}
    908 	}
    909 #ifdef PPS_SYNC
    910 	if (fhard) {
    911 		u_int64_t scale;
    912 
    913 		/*
    914 		 * Feed the NTP PLL/FLL.
    915 		 * The FLL wants to know how many (hardware) nanoseconds
    916 		 * elapsed since the previous event.
    917 		 */
    918 		tcount = pps->capcount - pps->ppscount[2];
    919 		pps->ppscount[2] = pps->capcount;
    920 		tcount &= pps->capth->th_counter->tc_counter_mask;
    921 		scale = (u_int64_t)1 << 63;
    922 		scale /= pps->capth->th_counter->tc_frequency;
    923 		scale *= 2;
    924 		bt.sec = 0;
    925 		bt.frac = 0;
    926 		bintime_addx(&bt, scale * tcount);
    927 		bintime2timespec(&bt, &ts);
    928 		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
    929 	}
    930 #endif
    931 }
    932 
    933 /*
    934  * Timecounters need to be updated every so often to prevent the hardware
    935  * counter from overflowing.  Updating also recalculates the cached values
    936  * used by the get*() family of functions, so their precision depends on
    937  * the update frequency.
    938  */
    939 
    940 static int tc_tick;
    941 
    942 void
    943 tc_ticktock(void)
    944 {
    945 	static int count;
    946 
    947 	if (++count < tc_tick)
    948 		return;
    949 	count = 0;
    950 	mutex_spin_enter(&timecounter_lock);
    951 	if (timecounter_bad != 0) {
    952 		/* An existing timecounter has gone bad, pick a new one. */
    953 		(void)atomic_swap_uint(&timecounter_bad, 0);
    954 		if (timecounter->tc_quality < 0) {
    955 			tc_pick();
    956 		}
    957 	}
    958 	tc_windup();
    959 	mutex_spin_exit(&timecounter_lock);
    960 }
    961 
    962 void
    963 inittimecounter(void)
    964 {
    965 	u_int p;
    966 
    967 	mutex_init(&timecounter_lock, MUTEX_DEFAULT, IPL_HIGH);
    968 
    969 	/*
    970 	 * Set the initial timeout to
    971 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
    972 	 * People should probably not use the sysctl to set the timeout
    973 	 * to smaller than its inital value, since that value is the
    974 	 * smallest reasonable one.  If they want better timestamps they
    975 	 * should use the non-"get"* functions.
    976 	 */
    977 	if (hz > 1000)
    978 		tc_tick = (hz + 500) / 1000;
    979 	else
    980 		tc_tick = 1;
    981 	p = (tc_tick * 1000000) / hz;
    982 	aprint_verbose("timecounter: Timecounters tick every %d.%03u msec\n",
    983 	    p / 1000, p % 1000);
    984 
    985 	/* warm up new timecounter (again) and get rolling. */
    986 	(void)timecounter->tc_get_timecount(timecounter);
    987 	(void)timecounter->tc_get_timecount(timecounter);
    988 }
    989