Home | History | Annotate | Line # | Download | only in kern
kern_tc.c revision 1.65
      1 /* $NetBSD: kern_tc.c,v 1.65 2023/07/17 13:35:07 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * ----------------------------------------------------------------------------
     34  * "THE BEER-WARE LICENSE" (Revision 42):
     35  * <phk (at) FreeBSD.ORG> wrote this file.  As long as you retain this notice you
     36  * can do whatever you want with this stuff. If we meet some day, and you think
     37  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
     38  * ---------------------------------------------------------------------------
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
     43 __KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.65 2023/07/17 13:35:07 riastradh Exp $");
     44 
     45 #ifdef _KERNEL_OPT
     46 #include "opt_ntp.h"
     47 #endif
     48 
     49 #include <sys/param.h>
     50 
     51 #include <sys/atomic.h>
     52 #include <sys/evcnt.h>
     53 #include <sys/kauth.h>
     54 #include <sys/kernel.h>
     55 #include <sys/lock.h>
     56 #include <sys/mutex.h>
     57 #include <sys/reboot.h>	/* XXX just to get AB_VERBOSE */
     58 #include <sys/sysctl.h>
     59 #include <sys/syslog.h>
     60 #include <sys/systm.h>
     61 #include <sys/timepps.h>
     62 #include <sys/timetc.h>
     63 #include <sys/timex.h>
     64 #include <sys/xcall.h>
     65 
     66 /*
     67  * A large step happens on boot.  This constant detects such steps.
     68  * It is relatively small so that ntp_update_second gets called enough
     69  * in the typical 'missed a couple of seconds' case, but doesn't loop
     70  * forever when the time step is large.
     71  */
     72 #define LARGE_STEP	200
     73 
     74 /*
     75  * Implement a dummy timecounter which we can use until we get a real one
     76  * in the air.  This allows the console and other early stuff to use
     77  * time services.
     78  */
     79 
     80 static u_int
     81 dummy_get_timecount(struct timecounter *tc)
     82 {
     83 	static u_int now;
     84 
     85 	return ++now;
     86 }
     87 
     88 static struct timecounter dummy_timecounter = {
     89 	.tc_get_timecount	= dummy_get_timecount,
     90 	.tc_counter_mask	= ~0u,
     91 	.tc_frequency		= 1000000,
     92 	.tc_name		= "dummy",
     93 	.tc_quality		= -1000000,
     94 	.tc_priv		= NULL,
     95 };
     96 
     97 struct timehands {
     98 	/* These fields must be initialized by the driver. */
     99 	struct timecounter	*th_counter;     /* active timecounter */
    100 	int64_t			th_adjustment;   /* frequency adjustment */
    101 						 /* (NTP/adjtime) */
    102 	uint64_t		th_scale;        /* scale factor (counter */
    103 						 /* tick->time) */
    104 	uint64_t 		th_offset_count; /* offset at last time */
    105 						 /* update (tc_windup()) */
    106 	struct bintime		th_offset;       /* bin (up)time at windup */
    107 	struct timeval		th_microtime;    /* cached microtime */
    108 	struct timespec		th_nanotime;     /* cached nanotime */
    109 	/* Fields not to be copied in tc_windup start with th_generation. */
    110 	volatile u_int		th_generation;   /* current genration */
    111 	struct timehands	*th_next;        /* next timehand */
    112 };
    113 
    114 static struct timehands th0;
    115 static struct timehands th9 = { .th_next = &th0, };
    116 static struct timehands th8 = { .th_next = &th9, };
    117 static struct timehands th7 = { .th_next = &th8, };
    118 static struct timehands th6 = { .th_next = &th7, };
    119 static struct timehands th5 = { .th_next = &th6, };
    120 static struct timehands th4 = { .th_next = &th5, };
    121 static struct timehands th3 = { .th_next = &th4, };
    122 static struct timehands th2 = { .th_next = &th3, };
    123 static struct timehands th1 = { .th_next = &th2, };
    124 static struct timehands th0 = {
    125 	.th_counter = &dummy_timecounter,
    126 	.th_scale = (uint64_t)-1 / 1000000,
    127 	.th_offset = { .sec = 1, .frac = 0 },
    128 	.th_generation = 1,
    129 	.th_next = &th1,
    130 };
    131 
    132 static struct timehands *volatile timehands = &th0;
    133 struct timecounter *timecounter = &dummy_timecounter;
    134 static struct timecounter *timecounters = &dummy_timecounter;
    135 
    136 volatile time_t time__second __cacheline_aligned = 1;
    137 volatile time_t time__uptime __cacheline_aligned = 1;
    138 
    139 #ifndef __HAVE_ATOMIC64_LOADSTORE
    140 static volatile struct {
    141 	uint32_t lo, hi;
    142 } time__uptime32 __cacheline_aligned = {
    143 	.lo = 1,
    144 }, time__second32 __cacheline_aligned = {
    145 	.lo = 1,
    146 };
    147 #endif
    148 
    149 static struct bintime timebasebin;
    150 
    151 static int timestepwarnings;
    152 
    153 kmutex_t timecounter_lock;
    154 static u_int timecounter_mods;
    155 static volatile int timecounter_removals = 1;
    156 static u_int timecounter_bad;
    157 
    158 #ifdef __HAVE_ATOMIC64_LOADSTORE
    159 
    160 static inline void
    161 setrealuptime(time_t second, time_t uptime)
    162 {
    163 
    164 	atomic_store_relaxed(&time__second, second);
    165 	atomic_store_relaxed(&time__uptime, uptime);
    166 }
    167 
    168 #else
    169 
    170 static inline void
    171 setrealuptime(time_t second, time_t uptime)
    172 {
    173 	uint32_t seclo = second & 0xffffffff, sechi = second >> 32;
    174 	uint32_t uplo = uptime & 0xffffffff, uphi = uptime >> 32;
    175 	int s;
    176 
    177 	KDASSERT(mutex_owned(&timecounter_lock));
    178 
    179 	/*
    180 	 * Fast path -- no wraparound, just updating the low bits, so
    181 	 * no need for seqlocked access.
    182 	 */
    183 	if (__predict_true(sechi == time__second32.hi) &&
    184 	    __predict_true(uphi == time__uptime32.hi)) {
    185 		atomic_store_relaxed(&time__second32.lo, seclo);
    186 		atomic_store_relaxed(&time__uptime32.lo, uplo);
    187 		return;
    188 	}
    189 
    190 	s = splhigh();
    191 	atomic_store_relaxed(&time__second32.hi, 0xffffffff);
    192 	atomic_store_relaxed(&time__uptime32.hi, 0xffffffff);
    193 	membar_producer();
    194 	atomic_store_relaxed(&time__second32.lo, seclo);
    195 	atomic_store_relaxed(&time__uptime32.lo, uplo);
    196 	membar_producer();
    197 	atomic_store_relaxed(&time__second32.hi, sechi);
    198 	atomic_store_relaxed(&time__uptime32.hi, uphi);
    199 	splx(s);
    200 }
    201 
    202 time_t
    203 getrealtime(void)
    204 {
    205 	uint32_t lo, hi;
    206 
    207 	do {
    208 		for (;;) {
    209 			hi = atomic_load_relaxed(&time__second32.hi);
    210 			if (__predict_true(hi != 0xffffffff))
    211 				break;
    212 			SPINLOCK_BACKOFF_HOOK;
    213 		}
    214 		membar_consumer();
    215 		lo = atomic_load_relaxed(&time__second32.lo);
    216 		membar_consumer();
    217 	} while (hi != atomic_load_relaxed(&time__second32.hi));
    218 
    219 	return ((time_t)hi << 32) | lo;
    220 }
    221 
    222 time_t
    223 getuptime(void)
    224 {
    225 	uint32_t lo, hi;
    226 
    227 	do {
    228 		for (;;) {
    229 			hi = atomic_load_relaxed(&time__uptime32.hi);
    230 			if (__predict_true(hi != 0xffffffff))
    231 				break;
    232 			SPINLOCK_BACKOFF_HOOK;
    233 		}
    234 		membar_consumer();
    235 		lo = atomic_load_relaxed(&time__uptime32.lo);
    236 		membar_consumer();
    237 	} while (hi != atomic_load_relaxed(&time__uptime32.hi));
    238 
    239 	return ((time_t)hi << 32) | lo;
    240 }
    241 
    242 time_t
    243 getboottime(void)
    244 {
    245 
    246 	return getrealtime() - getuptime();
    247 }
    248 
    249 uint32_t
    250 getuptime32(void)
    251 {
    252 
    253 	return atomic_load_relaxed(&time__uptime32.lo);
    254 }
    255 
    256 #endif	/* !defined(__HAVE_ATOMIC64_LOADSTORE) */
    257 
    258 /*
    259  * sysctl helper routine for kern.timercounter.hardware
    260  */
    261 static int
    262 sysctl_kern_timecounter_hardware(SYSCTLFN_ARGS)
    263 {
    264 	struct sysctlnode node;
    265 	int error;
    266 	char newname[MAX_TCNAMELEN];
    267 	struct timecounter *newtc, *tc;
    268 
    269 	tc = timecounter;
    270 
    271 	strlcpy(newname, tc->tc_name, sizeof(newname));
    272 
    273 	node = *rnode;
    274 	node.sysctl_data = newname;
    275 	node.sysctl_size = sizeof(newname);
    276 
    277 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    278 
    279 	if (error ||
    280 	    newp == NULL ||
    281 	    strncmp(newname, tc->tc_name, sizeof(newname)) == 0)
    282 		return error;
    283 
    284 	if (l != NULL && (error = kauth_authorize_system(l->l_cred,
    285 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERS, newname,
    286 	    NULL, NULL)) != 0)
    287 		return error;
    288 
    289 	if (!cold)
    290 		mutex_spin_enter(&timecounter_lock);
    291 	error = EINVAL;
    292 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
    293 		if (strcmp(newname, newtc->tc_name) != 0)
    294 			continue;
    295 		/* Warm up new timecounter. */
    296 		(void)newtc->tc_get_timecount(newtc);
    297 		(void)newtc->tc_get_timecount(newtc);
    298 		timecounter = newtc;
    299 		error = 0;
    300 		break;
    301 	}
    302 	if (!cold)
    303 		mutex_spin_exit(&timecounter_lock);
    304 	return error;
    305 }
    306 
    307 static int
    308 sysctl_kern_timecounter_choice(SYSCTLFN_ARGS)
    309 {
    310 	char buf[MAX_TCNAMELEN+48];
    311 	char *where;
    312 	const char *spc;
    313 	struct timecounter *tc;
    314 	size_t needed, left, slen;
    315 	int error, mods;
    316 
    317 	if (newp != NULL)
    318 		return EPERM;
    319 	if (namelen != 0)
    320 		return EINVAL;
    321 
    322 	mutex_spin_enter(&timecounter_lock);
    323  retry:
    324 	spc = "";
    325 	error = 0;
    326 	needed = 0;
    327 	left = *oldlenp;
    328 	where = oldp;
    329 	for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
    330 		if (where == NULL) {
    331 			needed += sizeof(buf);  /* be conservative */
    332 		} else {
    333 			slen = snprintf(buf, sizeof(buf), "%s%s(q=%d, f=%" PRId64
    334 					" Hz)", spc, tc->tc_name, tc->tc_quality,
    335 					tc->tc_frequency);
    336 			if (left < slen + 1)
    337 				break;
    338 		 	mods = timecounter_mods;
    339 			mutex_spin_exit(&timecounter_lock);
    340 			error = copyout(buf, where, slen + 1);
    341 			mutex_spin_enter(&timecounter_lock);
    342 			if (mods != timecounter_mods) {
    343 				goto retry;
    344 			}
    345 			spc = " ";
    346 			where += slen;
    347 			needed += slen;
    348 			left -= slen;
    349 		}
    350 	}
    351 	mutex_spin_exit(&timecounter_lock);
    352 
    353 	*oldlenp = needed;
    354 	return error;
    355 }
    356 
    357 SYSCTL_SETUP(sysctl_timecounter_setup, "sysctl timecounter setup")
    358 {
    359 	const struct sysctlnode *node;
    360 
    361 	sysctl_createv(clog, 0, NULL, &node,
    362 		       CTLFLAG_PERMANENT,
    363 		       CTLTYPE_NODE, "timecounter",
    364 		       SYSCTL_DESCR("time counter information"),
    365 		       NULL, 0, NULL, 0,
    366 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    367 
    368 	if (node != NULL) {
    369 		sysctl_createv(clog, 0, NULL, NULL,
    370 			       CTLFLAG_PERMANENT,
    371 			       CTLTYPE_STRING, "choice",
    372 			       SYSCTL_DESCR("available counters"),
    373 			       sysctl_kern_timecounter_choice, 0, NULL, 0,
    374 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    375 
    376 		sysctl_createv(clog, 0, NULL, NULL,
    377 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    378 			       CTLTYPE_STRING, "hardware",
    379 			       SYSCTL_DESCR("currently active time counter"),
    380 			       sysctl_kern_timecounter_hardware, 0, NULL, MAX_TCNAMELEN,
    381 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    382 
    383 		sysctl_createv(clog, 0, NULL, NULL,
    384 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    385 			       CTLTYPE_INT, "timestepwarnings",
    386 			       SYSCTL_DESCR("log time steps"),
    387 			       NULL, 0, &timestepwarnings, 0,
    388 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    389 	}
    390 }
    391 
    392 #ifdef TC_COUNTERS
    393 #define	TC_STATS(name)							\
    394 static struct evcnt n##name =						\
    395     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "timecounter", #name);	\
    396 EVCNT_ATTACH_STATIC(n##name)
    397 TC_STATS(binuptime);    TC_STATS(nanouptime);    TC_STATS(microuptime);
    398 TC_STATS(bintime);      TC_STATS(nanotime);      TC_STATS(microtime);
    399 TC_STATS(getbinuptime); TC_STATS(getnanouptime); TC_STATS(getmicrouptime);
    400 TC_STATS(getbintime);   TC_STATS(getnanotime);   TC_STATS(getmicrotime);
    401 TC_STATS(setclock);
    402 #define	TC_COUNT(var)	var.ev_count++
    403 #undef TC_STATS
    404 #else
    405 #define	TC_COUNT(var)	/* nothing */
    406 #endif	/* TC_COUNTERS */
    407 
    408 static void tc_windup(void);
    409 
    410 /*
    411  * Return the difference between the timehands' counter value now and what
    412  * was when we copied it to the timehands' offset_count.
    413  */
    414 static inline u_int
    415 tc_delta(struct timehands *th)
    416 {
    417 	struct timecounter *tc;
    418 
    419 	tc = th->th_counter;
    420 	return (tc->tc_get_timecount(tc) -
    421 		 th->th_offset_count) & tc->tc_counter_mask;
    422 }
    423 
    424 /*
    425  * Functions for reading the time.  We have to loop until we are sure that
    426  * the timehands that we operated on was not updated under our feet.  See
    427  * the comment in <sys/timevar.h> for a description of these 12 functions.
    428  */
    429 
    430 void
    431 binuptime(struct bintime *bt)
    432 {
    433 	struct timehands *th;
    434 	lwp_t *l;
    435 	u_int lgen, gen;
    436 
    437 	TC_COUNT(nbinuptime);
    438 
    439 	/*
    440 	 * Provide exclusion against tc_detach().
    441 	 *
    442 	 * We record the number of timecounter removals before accessing
    443 	 * timecounter state.  Note that the LWP can be using multiple
    444 	 * "generations" at once, due to interrupts (interrupted while in
    445 	 * this function).  Hardware interrupts will borrow the interrupted
    446 	 * LWP's l_tcgen value for this purpose, and can themselves be
    447 	 * interrupted by higher priority interrupts.  In this case we need
    448 	 * to ensure that the oldest generation in use is recorded.
    449 	 *
    450 	 * splsched() is too expensive to use, so we take care to structure
    451 	 * this code in such a way that it is not required.  Likewise, we
    452 	 * do not disable preemption.
    453 	 *
    454 	 * Memory barriers are also too expensive to use for such a
    455 	 * performance critical function.  The good news is that we do not
    456 	 * need memory barriers for this type of exclusion, as the thread
    457 	 * updating timecounter_removals will issue a broadcast cross call
    458 	 * before inspecting our l_tcgen value (this elides memory ordering
    459 	 * issues).
    460 	 */
    461 	l = curlwp;
    462 	lgen = l->l_tcgen;
    463 	if (__predict_true(lgen == 0)) {
    464 		l->l_tcgen = timecounter_removals;
    465 	}
    466 	__insn_barrier();
    467 
    468 	do {
    469 		th = timehands;
    470 		gen = th->th_generation;
    471 		*bt = th->th_offset;
    472 		bintime_addx(bt, th->th_scale * tc_delta(th));
    473 	} while (gen == 0 || gen != th->th_generation);
    474 
    475 	__insn_barrier();
    476 	l->l_tcgen = lgen;
    477 }
    478 
    479 void
    480 nanouptime(struct timespec *tsp)
    481 {
    482 	struct bintime bt;
    483 
    484 	TC_COUNT(nnanouptime);
    485 	binuptime(&bt);
    486 	bintime2timespec(&bt, tsp);
    487 }
    488 
    489 void
    490 microuptime(struct timeval *tvp)
    491 {
    492 	struct bintime bt;
    493 
    494 	TC_COUNT(nmicrouptime);
    495 	binuptime(&bt);
    496 	bintime2timeval(&bt, tvp);
    497 }
    498 
    499 void
    500 bintime(struct bintime *bt)
    501 {
    502 
    503 	TC_COUNT(nbintime);
    504 	binuptime(bt);
    505 	bintime_add(bt, &timebasebin);
    506 }
    507 
    508 void
    509 nanotime(struct timespec *tsp)
    510 {
    511 	struct bintime bt;
    512 
    513 	TC_COUNT(nnanotime);
    514 	bintime(&bt);
    515 	bintime2timespec(&bt, tsp);
    516 }
    517 
    518 void
    519 microtime(struct timeval *tvp)
    520 {
    521 	struct bintime bt;
    522 
    523 	TC_COUNT(nmicrotime);
    524 	bintime(&bt);
    525 	bintime2timeval(&bt, tvp);
    526 }
    527 
    528 void
    529 getbinuptime(struct bintime *bt)
    530 {
    531 	struct timehands *th;
    532 	u_int gen;
    533 
    534 	TC_COUNT(ngetbinuptime);
    535 	do {
    536 		th = timehands;
    537 		gen = th->th_generation;
    538 		*bt = th->th_offset;
    539 	} while (gen == 0 || gen != th->th_generation);
    540 }
    541 
    542 void
    543 getnanouptime(struct timespec *tsp)
    544 {
    545 	struct timehands *th;
    546 	u_int gen;
    547 
    548 	TC_COUNT(ngetnanouptime);
    549 	do {
    550 		th = timehands;
    551 		gen = th->th_generation;
    552 		bintime2timespec(&th->th_offset, tsp);
    553 	} while (gen == 0 || gen != th->th_generation);
    554 }
    555 
    556 void
    557 getmicrouptime(struct timeval *tvp)
    558 {
    559 	struct timehands *th;
    560 	u_int gen;
    561 
    562 	TC_COUNT(ngetmicrouptime);
    563 	do {
    564 		th = timehands;
    565 		gen = th->th_generation;
    566 		bintime2timeval(&th->th_offset, tvp);
    567 	} while (gen == 0 || gen != th->th_generation);
    568 }
    569 
    570 void
    571 getbintime(struct bintime *bt)
    572 {
    573 	struct timehands *th;
    574 	u_int gen;
    575 
    576 	TC_COUNT(ngetbintime);
    577 	do {
    578 		th = timehands;
    579 		gen = th->th_generation;
    580 		*bt = th->th_offset;
    581 	} while (gen == 0 || gen != th->th_generation);
    582 	bintime_add(bt, &timebasebin);
    583 }
    584 
    585 static inline void
    586 dogetnanotime(struct timespec *tsp)
    587 {
    588 	struct timehands *th;
    589 	u_int gen;
    590 
    591 	TC_COUNT(ngetnanotime);
    592 	do {
    593 		th = timehands;
    594 		gen = th->th_generation;
    595 		*tsp = th->th_nanotime;
    596 	} while (gen == 0 || gen != th->th_generation);
    597 }
    598 
    599 void
    600 getnanotime(struct timespec *tsp)
    601 {
    602 
    603 	dogetnanotime(tsp);
    604 }
    605 
    606 void dtrace_getnanotime(struct timespec *tsp);
    607 
    608 void
    609 dtrace_getnanotime(struct timespec *tsp)
    610 {
    611 
    612 	dogetnanotime(tsp);
    613 }
    614 
    615 void
    616 getmicrotime(struct timeval *tvp)
    617 {
    618 	struct timehands *th;
    619 	u_int gen;
    620 
    621 	TC_COUNT(ngetmicrotime);
    622 	do {
    623 		th = timehands;
    624 		gen = th->th_generation;
    625 		*tvp = th->th_microtime;
    626 	} while (gen == 0 || gen != th->th_generation);
    627 }
    628 
    629 void
    630 getnanoboottime(struct timespec *tsp)
    631 {
    632 	struct bintime bt;
    633 
    634 	getbinboottime(&bt);
    635 	bintime2timespec(&bt, tsp);
    636 }
    637 
    638 void
    639 getmicroboottime(struct timeval *tvp)
    640 {
    641 	struct bintime bt;
    642 
    643 	getbinboottime(&bt);
    644 	bintime2timeval(&bt, tvp);
    645 }
    646 
    647 void
    648 getbinboottime(struct bintime *bt)
    649 {
    650 
    651 	/*
    652 	 * XXX Need lockless read synchronization around timebasebin
    653 	 * (and not just here).
    654 	 */
    655 	*bt = timebasebin;
    656 }
    657 
    658 /*
    659  * Initialize a new timecounter and possibly use it.
    660  */
    661 void
    662 tc_init(struct timecounter *tc)
    663 {
    664 	u_int u;
    665 
    666 	KASSERTMSG(tc->tc_next == NULL, "timecounter %s already initialised",
    667 	    tc->tc_name);
    668 
    669 	u = tc->tc_frequency / tc->tc_counter_mask;
    670 	/* XXX: We need some margin here, 10% is a guess */
    671 	u *= 11;
    672 	u /= 10;
    673 	if (u > hz && tc->tc_quality >= 0) {
    674 		tc->tc_quality = -2000;
    675 		aprint_verbose(
    676 		    "timecounter: Timecounter \"%s\" frequency %ju Hz",
    677 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
    678 		aprint_verbose(" -- Insufficient hz, needs at least %u\n", u);
    679 	} else if (tc->tc_quality >= 0 || bootverbose) {
    680 		aprint_verbose(
    681 		    "timecounter: Timecounter \"%s\" frequency %ju Hz "
    682 		    "quality %d\n", tc->tc_name, (uintmax_t)tc->tc_frequency,
    683 		    tc->tc_quality);
    684 	}
    685 
    686 	mutex_spin_enter(&timecounter_lock);
    687 	tc->tc_next = timecounters;
    688 	timecounters = tc;
    689 	timecounter_mods++;
    690 	/*
    691 	 * Never automatically use a timecounter with negative quality.
    692 	 * Even though we run on the dummy counter, switching here may be
    693 	 * worse since this timecounter may not be monotonous.
    694 	 */
    695 	if (tc->tc_quality >= 0 && (tc->tc_quality > timecounter->tc_quality ||
    696 	    (tc->tc_quality == timecounter->tc_quality &&
    697 	    tc->tc_frequency > timecounter->tc_frequency))) {
    698 		(void)tc->tc_get_timecount(tc);
    699 		(void)tc->tc_get_timecount(tc);
    700 		timecounter = tc;
    701 		tc_windup();
    702 	}
    703 	mutex_spin_exit(&timecounter_lock);
    704 }
    705 
    706 /*
    707  * Pick a new timecounter due to the existing counter going bad.
    708  */
    709 static void
    710 tc_pick(void)
    711 {
    712 	struct timecounter *best, *tc;
    713 
    714 	KASSERT(mutex_owned(&timecounter_lock));
    715 
    716 	for (best = tc = timecounters; tc != NULL; tc = tc->tc_next) {
    717 		if (tc->tc_quality > best->tc_quality)
    718 			best = tc;
    719 		else if (tc->tc_quality < best->tc_quality)
    720 			continue;
    721 		else if (tc->tc_frequency > best->tc_frequency)
    722 			best = tc;
    723 	}
    724 	(void)best->tc_get_timecount(best);
    725 	(void)best->tc_get_timecount(best);
    726 	timecounter = best;
    727 }
    728 
    729 /*
    730  * A timecounter has gone bad, arrange to pick a new one at the next
    731  * clock tick.
    732  */
    733 void
    734 tc_gonebad(struct timecounter *tc)
    735 {
    736 
    737 	tc->tc_quality = -100;
    738 	membar_producer();
    739 	atomic_inc_uint(&timecounter_bad);
    740 }
    741 
    742 /*
    743  * Stop using a timecounter and remove it from the timecounters list.
    744  */
    745 int
    746 tc_detach(struct timecounter *target)
    747 {
    748 	struct timecounter *tc;
    749 	struct timecounter **tcp = NULL;
    750 	int removals;
    751 	lwp_t *l;
    752 
    753 	/* First, find the timecounter. */
    754 	mutex_spin_enter(&timecounter_lock);
    755 	for (tcp = &timecounters, tc = timecounters;
    756 	     tc != NULL;
    757 	     tcp = &tc->tc_next, tc = tc->tc_next) {
    758 		if (tc == target)
    759 			break;
    760 	}
    761 	if (tc == NULL) {
    762 		mutex_spin_exit(&timecounter_lock);
    763 		return ESRCH;
    764 	}
    765 
    766 	/* And now, remove it. */
    767 	*tcp = tc->tc_next;
    768 	if (timecounter == target) {
    769 		tc_pick();
    770 		tc_windup();
    771 	}
    772 	timecounter_mods++;
    773 	removals = timecounter_removals++;
    774 	mutex_spin_exit(&timecounter_lock);
    775 
    776 	/*
    777 	 * We now have to determine if any threads in the system are still
    778 	 * making use of this timecounter.
    779 	 *
    780 	 * We issue a broadcast cross call to elide memory ordering issues,
    781 	 * then scan all LWPs in the system looking at each's timecounter
    782 	 * generation number.  We need to see a value of zero (not actively
    783 	 * using a timecounter) or a value greater than our removal value.
    784 	 *
    785 	 * We may race with threads that read `timecounter_removals' and
    786 	 * and then get preempted before updating `l_tcgen'.  This is not
    787 	 * a problem, since it means that these threads have not yet started
    788 	 * accessing timecounter state.  All we do need is one clean
    789 	 * snapshot of the system where every thread appears not to be using
    790 	 * old timecounter state.
    791 	 */
    792 	for (;;) {
    793 		xc_barrier(0);
    794 
    795 		mutex_enter(&proc_lock);
    796 		LIST_FOREACH(l, &alllwp, l_list) {
    797 			if (l->l_tcgen == 0 || l->l_tcgen > removals) {
    798 				/*
    799 				 * Not using timecounter or old timecounter
    800 				 * state at time of our xcall or later.
    801 				 */
    802 				continue;
    803 			}
    804 			break;
    805 		}
    806 		mutex_exit(&proc_lock);
    807 
    808 		/*
    809 		 * If the timecounter is still in use, wait at least 10ms
    810 		 * before retrying.
    811 		 */
    812 		if (l == NULL) {
    813 			break;
    814 		}
    815 		(void)kpause("tcdetach", false, mstohz(10), NULL);
    816 	}
    817 
    818 	tc->tc_next = NULL;
    819 	return 0;
    820 }
    821 
    822 /* Report the frequency of the current timecounter. */
    823 uint64_t
    824 tc_getfrequency(void)
    825 {
    826 
    827 	return timehands->th_counter->tc_frequency;
    828 }
    829 
    830 /*
    831  * Step our concept of UTC.  This is done by modifying our estimate of
    832  * when we booted.
    833  */
    834 void
    835 tc_setclock(const struct timespec *ts)
    836 {
    837 	struct timespec ts2;
    838 	struct bintime bt, bt2;
    839 
    840 	mutex_spin_enter(&timecounter_lock);
    841 	TC_COUNT(nsetclock);
    842 	binuptime(&bt2);
    843 	timespec2bintime(ts, &bt);
    844 	bintime_sub(&bt, &bt2);
    845 	bintime_add(&bt2, &timebasebin);
    846 	timebasebin = bt;
    847 	tc_windup();
    848 	mutex_spin_exit(&timecounter_lock);
    849 
    850 	if (timestepwarnings) {
    851 		bintime2timespec(&bt2, &ts2);
    852 		log(LOG_INFO,
    853 		    "Time stepped from %lld.%09ld to %lld.%09ld\n",
    854 		    (long long)ts2.tv_sec, ts2.tv_nsec,
    855 		    (long long)ts->tv_sec, ts->tv_nsec);
    856 	}
    857 }
    858 
    859 /*
    860  * Initialize the next struct timehands in the ring and make
    861  * it the active timehands.  Along the way we might switch to a different
    862  * timecounter and/or do seconds processing in NTP.  Slightly magic.
    863  */
    864 static void
    865 tc_windup(void)
    866 {
    867 	struct bintime bt;
    868 	struct timehands *th, *tho;
    869 	uint64_t scale;
    870 	u_int delta, ncount, ogen;
    871 	int i, s_update;
    872 	time_t t;
    873 
    874 	KASSERT(mutex_owned(&timecounter_lock));
    875 
    876 	s_update = 0;
    877 
    878 	/*
    879 	 * Make the next timehands a copy of the current one, but do not
    880 	 * overwrite the generation or next pointer.  While we update
    881 	 * the contents, the generation must be zero.  Ensure global
    882 	 * visibility of the generation before proceeding.
    883 	 */
    884 	tho = timehands;
    885 	th = tho->th_next;
    886 	ogen = th->th_generation;
    887 	th->th_generation = 0;
    888 	membar_producer();
    889 	bcopy(tho, th, offsetof(struct timehands, th_generation));
    890 
    891 	/*
    892 	 * Capture a timecounter delta on the current timecounter and if
    893 	 * changing timecounters, a counter value from the new timecounter.
    894 	 * Update the offset fields accordingly.
    895 	 */
    896 	delta = tc_delta(th);
    897 	if (th->th_counter != timecounter)
    898 		ncount = timecounter->tc_get_timecount(timecounter);
    899 	else
    900 		ncount = 0;
    901 	th->th_offset_count += delta;
    902 	bintime_addx(&th->th_offset, th->th_scale * delta);
    903 
    904 	/*
    905 	 * Hardware latching timecounters may not generate interrupts on
    906 	 * PPS events, so instead we poll them.  There is a finite risk that
    907 	 * the hardware might capture a count which is later than the one we
    908 	 * got above, and therefore possibly in the next NTP second which might
    909 	 * have a different rate than the current NTP second.  It doesn't
    910 	 * matter in practice.
    911 	 */
    912 	if (tho->th_counter->tc_poll_pps)
    913 		tho->th_counter->tc_poll_pps(tho->th_counter);
    914 
    915 	/*
    916 	 * Deal with NTP second processing.  The for loop normally
    917 	 * iterates at most once, but in extreme situations it might
    918 	 * keep NTP sane if timeouts are not run for several seconds.
    919 	 * At boot, the time step can be large when the TOD hardware
    920 	 * has been read, so on really large steps, we call
    921 	 * ntp_update_second only twice.  We need to call it twice in
    922 	 * case we missed a leap second.
    923 	 * If NTP is not compiled in ntp_update_second still calculates
    924 	 * the adjustment resulting from adjtime() calls.
    925 	 */
    926 	bt = th->th_offset;
    927 	bintime_add(&bt, &timebasebin);
    928 	i = bt.sec - tho->th_microtime.tv_sec;
    929 	if (i > LARGE_STEP)
    930 		i = 2;
    931 	for (; i > 0; i--) {
    932 		t = bt.sec;
    933 		ntp_update_second(&th->th_adjustment, &bt.sec);
    934 		s_update = 1;
    935 		if (bt.sec != t)
    936 			timebasebin.sec += bt.sec - t;
    937 	}
    938 
    939 	/* Update the UTC timestamps used by the get*() functions. */
    940 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
    941 	bintime2timeval(&bt, &th->th_microtime);
    942 	bintime2timespec(&bt, &th->th_nanotime);
    943 	/* Now is a good time to change timecounters. */
    944 	if (th->th_counter != timecounter) {
    945 		th->th_counter = timecounter;
    946 		th->th_offset_count = ncount;
    947 		s_update = 1;
    948 	}
    949 
    950 	/*-
    951 	 * Recalculate the scaling factor.  We want the number of 1/2^64
    952 	 * fractions of a second per period of the hardware counter, taking
    953 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
    954 	 * processing provides us with.
    955 	 *
    956 	 * The th_adjustment is nanoseconds per second with 32 bit binary
    957 	 * fraction and we want 64 bit binary fraction of second:
    958 	 *
    959 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
    960 	 *
    961 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
    962 	 * we can only multiply by about 850 without overflowing, but that
    963 	 * leaves suitably precise fractions for multiply before divide.
    964 	 *
    965 	 * Divide before multiply with a fraction of 2199/512 results in a
    966 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
    967 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
    968  	 *
    969 	 * We happily sacrifice the lowest of the 64 bits of our result
    970 	 * to the goddess of code clarity.
    971 	 *
    972 	 */
    973 	if (s_update) {
    974 		scale = (uint64_t)1 << 63;
    975 		scale += (th->th_adjustment / 1024) * 2199;
    976 		scale /= th->th_counter->tc_frequency;
    977 		th->th_scale = scale * 2;
    978 	}
    979 	/*
    980 	 * Now that the struct timehands is again consistent, set the new
    981 	 * generation number, making sure to not make it zero.  Ensure
    982 	 * changes are globally visible before changing.
    983 	 */
    984 	if (++ogen == 0)
    985 		ogen = 1;
    986 	membar_producer();
    987 	th->th_generation = ogen;
    988 
    989 	/*
    990 	 * Go live with the new struct timehands.  Ensure changes are
    991 	 * globally visible before changing.
    992 	 */
    993 	setrealuptime(th->th_microtime.tv_sec, th->th_offset.sec);
    994 	membar_producer();
    995 	timehands = th;
    996 
    997 	/*
    998 	 * Force users of the old timehand to move on.  This is
    999 	 * necessary for MP systems; we need to ensure that the
   1000 	 * consumers will move away from the old timehand before
   1001 	 * we begin updating it again when we eventually wrap
   1002 	 * around.
   1003 	 */
   1004 	if (++tho->th_generation == 0)
   1005 		tho->th_generation = 1;
   1006 }
   1007 
   1008 /*
   1009  * RFC 2783 PPS-API implementation.
   1010  */
   1011 
   1012 int
   1013 pps_ioctl(u_long cmd, void *data, struct pps_state *pps)
   1014 {
   1015 	pps_params_t *app;
   1016 	pps_info_t *pipi;
   1017 #ifdef PPS_SYNC
   1018 	int *epi;
   1019 #endif
   1020 
   1021 	KASSERT(mutex_owned(&timecounter_lock));
   1022 
   1023 	KASSERT(pps != NULL);
   1024 
   1025 	switch (cmd) {
   1026 	case PPS_IOC_CREATE:
   1027 		return 0;
   1028 	case PPS_IOC_DESTROY:
   1029 		return 0;
   1030 	case PPS_IOC_SETPARAMS:
   1031 		app = (pps_params_t *)data;
   1032 		if (app->mode & ~pps->ppscap)
   1033 			return EINVAL;
   1034 		pps->ppsparam = *app;
   1035 		return 0;
   1036 	case PPS_IOC_GETPARAMS:
   1037 		app = (pps_params_t *)data;
   1038 		*app = pps->ppsparam;
   1039 		app->api_version = PPS_API_VERS_1;
   1040 		return 0;
   1041 	case PPS_IOC_GETCAP:
   1042 		*(int*)data = pps->ppscap;
   1043 		return 0;
   1044 	case PPS_IOC_FETCH:
   1045 		pipi = (pps_info_t *)data;
   1046 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
   1047 		*pipi = pps->ppsinfo;
   1048 		return 0;
   1049 	case PPS_IOC_KCBIND:
   1050 #ifdef PPS_SYNC
   1051 		epi = (int *)data;
   1052 		/* XXX Only root should be able to do this */
   1053 		if (*epi & ~pps->ppscap)
   1054 			return EINVAL;
   1055 		pps->kcmode = *epi;
   1056 		return 0;
   1057 #else
   1058 		return EOPNOTSUPP;
   1059 #endif
   1060 	default:
   1061 		return EPASSTHROUGH;
   1062 	}
   1063 }
   1064 
   1065 void
   1066 pps_init(struct pps_state *pps)
   1067 {
   1068 
   1069 	KASSERT(mutex_owned(&timecounter_lock));
   1070 
   1071 	pps->ppscap |= PPS_TSFMT_TSPEC;
   1072 	if (pps->ppscap & PPS_CAPTUREASSERT)
   1073 		pps->ppscap |= PPS_OFFSETASSERT;
   1074 	if (pps->ppscap & PPS_CAPTURECLEAR)
   1075 		pps->ppscap |= PPS_OFFSETCLEAR;
   1076 }
   1077 
   1078 /*
   1079  * capture a timetamp in the pps structure
   1080  */
   1081 void
   1082 pps_capture(struct pps_state *pps)
   1083 {
   1084 	struct timehands *th;
   1085 
   1086 	KASSERT(mutex_owned(&timecounter_lock));
   1087 	KASSERT(pps != NULL);
   1088 
   1089 	th = timehands;
   1090 	pps->capgen = th->th_generation;
   1091 	pps->capth = th;
   1092 	pps->capcount = (uint64_t)tc_delta(th) + th->th_offset_count;
   1093 	if (pps->capgen != th->th_generation)
   1094 		pps->capgen = 0;
   1095 }
   1096 
   1097 #ifdef PPS_DEBUG
   1098 int ppsdebug = 0;
   1099 #endif
   1100 
   1101 /*
   1102  * process a pps_capture()ed event
   1103  */
   1104 void
   1105 pps_event(struct pps_state *pps, int event)
   1106 {
   1107 	pps_ref_event(pps, event, NULL, PPS_REFEVNT_PPS|PPS_REFEVNT_CAPTURE);
   1108 }
   1109 
   1110 /*
   1111  * extended pps api /  kernel pll/fll entry point
   1112  *
   1113  * feed reference time stamps to PPS engine
   1114  *
   1115  * will simulate a PPS event and feed
   1116  * the NTP PLL/FLL if requested.
   1117  *
   1118  * the ref time stamps should be roughly once
   1119  * a second but do not need to be exactly in phase
   1120  * with the UTC second but should be close to it.
   1121  * this relaxation of requirements allows callout
   1122  * driven timestamping mechanisms to feed to pps
   1123  * capture/kernel pll logic.
   1124  *
   1125  * calling pattern is:
   1126  *  pps_capture() (for PPS_REFEVNT_{CAPTURE|CAPCUR})
   1127  *  read timestamp from reference source
   1128  *  pps_ref_event()
   1129  *
   1130  * supported refmodes:
   1131  *  PPS_REFEVNT_CAPTURE
   1132  *    use system timestamp of pps_capture()
   1133  *  PPS_REFEVNT_CURRENT
   1134  *    use system timestamp of this call
   1135  *  PPS_REFEVNT_CAPCUR
   1136  *    use average of read capture and current system time stamp
   1137  *  PPS_REFEVNT_PPS
   1138  *    assume timestamp on second mark - ref_ts is ignored
   1139  *
   1140  */
   1141 
   1142 void
   1143 pps_ref_event(struct pps_state *pps,
   1144 	      int event,
   1145 	      struct bintime *ref_ts,
   1146 	      int refmode
   1147 	)
   1148 {
   1149 	struct bintime bt;	/* current time */
   1150 	struct bintime btd;	/* time difference */
   1151 	struct bintime bt_ref;	/* reference time */
   1152 	struct timespec ts, *tsp, *osp;
   1153 	struct timehands *th;
   1154 	uint64_t tcount, acount, dcount, *pcount;
   1155 	int foff, gen;
   1156 #ifdef PPS_SYNC
   1157 	int fhard;
   1158 #endif
   1159 	pps_seq_t *pseq;
   1160 
   1161 	KASSERT(mutex_owned(&timecounter_lock));
   1162 
   1163 	KASSERT(pps != NULL);
   1164 
   1165         /* pick up current time stamp if needed */
   1166 	if (refmode & (PPS_REFEVNT_CURRENT|PPS_REFEVNT_CAPCUR)) {
   1167 		/* pick up current time stamp */
   1168 		th = timehands;
   1169 		gen = th->th_generation;
   1170 		tcount = (uint64_t)tc_delta(th) + th->th_offset_count;
   1171 		if (gen != th->th_generation)
   1172 			gen = 0;
   1173 
   1174 		/* If the timecounter was wound up underneath us, bail out. */
   1175 		if (pps->capgen == 0 ||
   1176 		    pps->capgen != pps->capth->th_generation ||
   1177 		    gen == 0 ||
   1178 		    gen != pps->capgen) {
   1179 #ifdef PPS_DEBUG
   1180 			if (ppsdebug & 0x1) {
   1181 				log(LOG_DEBUG,
   1182 				    "pps_ref_event(pps=%p, event=%d, ...): DROP (wind-up)\n",
   1183 				    pps, event);
   1184 			}
   1185 #endif
   1186 			return;
   1187 		}
   1188 	} else {
   1189 		tcount = 0;	/* keep GCC happy */
   1190 	}
   1191 
   1192 #ifdef PPS_DEBUG
   1193 	if (ppsdebug & 0x1) {
   1194 		struct timespec tmsp;
   1195 
   1196 		if (ref_ts == NULL) {
   1197 			tmsp.tv_sec = 0;
   1198 			tmsp.tv_nsec = 0;
   1199 		} else {
   1200 			bintime2timespec(ref_ts, &tmsp);
   1201 		}
   1202 
   1203 		log(LOG_DEBUG,
   1204 		    "pps_ref_event(pps=%p, event=%d, ref_ts=%"PRIi64
   1205 		    ".%09"PRIi32", refmode=0x%1x)\n",
   1206 		    pps, event, tmsp.tv_sec, (int32_t)tmsp.tv_nsec, refmode);
   1207 	}
   1208 #endif
   1209 
   1210 	/* setup correct event references */
   1211 	if (event == PPS_CAPTUREASSERT) {
   1212 		tsp = &pps->ppsinfo.assert_timestamp;
   1213 		osp = &pps->ppsparam.assert_offset;
   1214 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
   1215 #ifdef PPS_SYNC
   1216 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
   1217 #endif
   1218 		pcount = &pps->ppscount[0];
   1219 		pseq = &pps->ppsinfo.assert_sequence;
   1220 	} else {
   1221 		tsp = &pps->ppsinfo.clear_timestamp;
   1222 		osp = &pps->ppsparam.clear_offset;
   1223 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
   1224 #ifdef PPS_SYNC
   1225 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
   1226 #endif
   1227 		pcount = &pps->ppscount[1];
   1228 		pseq = &pps->ppsinfo.clear_sequence;
   1229 	}
   1230 
   1231 	/* determine system time stamp according to refmode */
   1232 	dcount = 0;		/* keep GCC happy */
   1233 	switch (refmode & PPS_REFEVNT_RMASK) {
   1234 	case PPS_REFEVNT_CAPTURE:
   1235 		acount = pps->capcount;	/* use capture timestamp */
   1236 		break;
   1237 
   1238 	case PPS_REFEVNT_CURRENT:
   1239 		acount = tcount; /* use current timestamp */
   1240 		break;
   1241 
   1242 	case PPS_REFEVNT_CAPCUR:
   1243 		/*
   1244 		 * calculate counter value between pps_capture() and
   1245 		 * pps_ref_event()
   1246 		 */
   1247 		dcount = tcount - pps->capcount;
   1248 		acount = (dcount / 2) + pps->capcount;
   1249 		break;
   1250 
   1251 	default:		/* ignore call error silently */
   1252 		return;
   1253 	}
   1254 
   1255 	/*
   1256 	 * If the timecounter changed, we cannot compare the count values, so
   1257 	 * we have to drop the rest of the PPS-stuff until the next event.
   1258 	 */
   1259 	if (pps->ppstc != pps->capth->th_counter) {
   1260 		pps->ppstc = pps->capth->th_counter;
   1261 		pps->capcount = acount;
   1262 		*pcount = acount;
   1263 		pps->ppscount[2] = acount;
   1264 #ifdef PPS_DEBUG
   1265 		if (ppsdebug & 0x1) {
   1266 			log(LOG_DEBUG,
   1267 			    "pps_ref_event(pps=%p, event=%d, ...): DROP (time-counter change)\n",
   1268 			    pps, event);
   1269 		}
   1270 #endif
   1271 		return;
   1272 	}
   1273 
   1274 	pps->capcount = acount;
   1275 
   1276 	/* Convert the count to a bintime. */
   1277 	bt = pps->capth->th_offset;
   1278 	bintime_addx(&bt, pps->capth->th_scale * (acount - pps->capth->th_offset_count));
   1279 	bintime_add(&bt, &timebasebin);
   1280 
   1281 	if ((refmode & PPS_REFEVNT_PPS) == 0) {
   1282 		/* determine difference to reference time stamp */
   1283 		bt_ref = *ref_ts;
   1284 
   1285 		btd = bt;
   1286 		bintime_sub(&btd, &bt_ref);
   1287 
   1288 		/*
   1289 		 * simulate a PPS timestamp by dropping the fraction
   1290 		 * and applying the offset
   1291 		 */
   1292 		if (bt.frac >= (uint64_t)1<<63)	/* skip to nearest second */
   1293 			bt.sec++;
   1294 		bt.frac = 0;
   1295 		bintime_add(&bt, &btd);
   1296 	} else {
   1297 		/*
   1298 		 * create ref_ts from current time -
   1299 		 * we are supposed to be called on
   1300 		 * the second mark
   1301 		 */
   1302 		bt_ref = bt;
   1303 		if (bt_ref.frac >= (uint64_t)1<<63)	/* skip to nearest second */
   1304 			bt_ref.sec++;
   1305 		bt_ref.frac = 0;
   1306 	}
   1307 
   1308 	/* convert bintime to timestamp */
   1309 	bintime2timespec(&bt, &ts);
   1310 
   1311 	/* If the timecounter was wound up underneath us, bail out. */
   1312 	if (pps->capgen != pps->capth->th_generation)
   1313 		return;
   1314 
   1315 	/* store time stamp */
   1316 	*pcount = pps->capcount;
   1317 	(*pseq)++;
   1318 	*tsp = ts;
   1319 
   1320 	/* add offset correction */
   1321 	if (foff) {
   1322 		timespecadd(tsp, osp, tsp);
   1323 		if (tsp->tv_nsec < 0) {
   1324 			tsp->tv_nsec += 1000000000;
   1325 			tsp->tv_sec -= 1;
   1326 		}
   1327 	}
   1328 
   1329 #ifdef PPS_DEBUG
   1330 	if (ppsdebug & 0x2) {
   1331 		struct timespec ts2;
   1332 		struct timespec ts3;
   1333 
   1334 		bintime2timespec(&bt_ref, &ts2);
   1335 
   1336 		bt.sec = 0;
   1337 		bt.frac = 0;
   1338 
   1339 		if (refmode & PPS_REFEVNT_CAPCUR) {
   1340 			    bintime_addx(&bt, pps->capth->th_scale * dcount);
   1341 		}
   1342 		bintime2timespec(&bt, &ts3);
   1343 
   1344 		log(LOG_DEBUG, "ref_ts=%"PRIi64".%09"PRIi32
   1345 		    ", ts=%"PRIi64".%09"PRIi32", read latency=%"PRIi64" ns\n",
   1346 		    ts2.tv_sec, (int32_t)ts2.tv_nsec,
   1347 		    tsp->tv_sec, (int32_t)tsp->tv_nsec,
   1348 		    timespec2ns(&ts3));
   1349 	}
   1350 #endif
   1351 
   1352 #ifdef PPS_SYNC
   1353 	if (fhard) {
   1354 		uint64_t scale;
   1355 		uint64_t div;
   1356 
   1357 		/*
   1358 		 * Feed the NTP PLL/FLL.
   1359 		 * The FLL wants to know how many (hardware) nanoseconds
   1360 		 * elapsed since the previous event (mod 1 second) thus
   1361 		 * we are actually looking at the frequency difference scaled
   1362 		 * in nsec.
   1363 		 * As the counter time stamps are not truly at 1Hz
   1364 		 * we need to scale the count by the elapsed
   1365 		 * reference time.
   1366 		 * valid sampling interval: [0.5..2[ sec
   1367 		 */
   1368 
   1369 		/* calculate elapsed raw count */
   1370 		tcount = pps->capcount - pps->ppscount[2];
   1371 		pps->ppscount[2] = pps->capcount;
   1372 		tcount &= pps->capth->th_counter->tc_counter_mask;
   1373 
   1374 		/* calculate elapsed ref time */
   1375 		btd = bt_ref;
   1376 		bintime_sub(&btd, &pps->ref_time);
   1377 		pps->ref_time = bt_ref;
   1378 
   1379 		/* check that we stay below 2 sec */
   1380 		if (btd.sec < 0 || btd.sec > 1)
   1381 			return;
   1382 
   1383 		/* we want at least 0.5 sec between samples */
   1384 		if (btd.sec == 0 && btd.frac < (uint64_t)1<<63)
   1385 			return;
   1386 
   1387 		/*
   1388 		 * calculate cycles per period by multiplying
   1389 		 * the frequency with the elapsed period
   1390 		 * we pick a fraction of 30 bits
   1391 		 * ~1ns resolution for elapsed time
   1392 		 */
   1393 		div   = (uint64_t)btd.sec << 30;
   1394 		div  |= (btd.frac >> 34) & (((uint64_t)1 << 30) - 1);
   1395 		div  *= pps->capth->th_counter->tc_frequency;
   1396 		div >>= 30;
   1397 
   1398 		if (div == 0)	/* safeguard */
   1399 			return;
   1400 
   1401 		scale = (uint64_t)1 << 63;
   1402 		scale /= div;
   1403 		scale *= 2;
   1404 
   1405 		bt.sec = 0;
   1406 		bt.frac = 0;
   1407 		bintime_addx(&bt, scale * tcount);
   1408 		bintime2timespec(&bt, &ts);
   1409 
   1410 #ifdef PPS_DEBUG
   1411 		if (ppsdebug & 0x4) {
   1412 			struct timespec ts2;
   1413 			int64_t df;
   1414 
   1415 			bintime2timespec(&bt_ref, &ts2);
   1416 			df = timespec2ns(&ts);
   1417 			if (df > 500000000)
   1418 				df -= 1000000000;
   1419 			log(LOG_DEBUG, "hardpps: ref_ts=%"PRIi64
   1420 			    ".%09"PRIi32", ts=%"PRIi64".%09"PRIi32
   1421 			    ", freqdiff=%"PRIi64" ns/s\n",
   1422 			    ts2.tv_sec, (int32_t)ts2.tv_nsec,
   1423 			    tsp->tv_sec, (int32_t)tsp->tv_nsec,
   1424 			    df);
   1425 		}
   1426 #endif
   1427 
   1428 		hardpps(tsp, timespec2ns(&ts));
   1429 	}
   1430 #endif
   1431 }
   1432 
   1433 /*
   1434  * Timecounters need to be updated every so often to prevent the hardware
   1435  * counter from overflowing.  Updating also recalculates the cached values
   1436  * used by the get*() family of functions, so their precision depends on
   1437  * the update frequency.
   1438  */
   1439 
   1440 static int tc_tick;
   1441 
   1442 void
   1443 tc_ticktock(void)
   1444 {
   1445 	static int count;
   1446 
   1447 	if (++count < tc_tick)
   1448 		return;
   1449 	count = 0;
   1450 	mutex_spin_enter(&timecounter_lock);
   1451 	if (__predict_false(timecounter_bad != 0)) {
   1452 		/* An existing timecounter has gone bad, pick a new one. */
   1453 		(void)atomic_swap_uint(&timecounter_bad, 0);
   1454 		if (timecounter->tc_quality < 0) {
   1455 			tc_pick();
   1456 		}
   1457 	}
   1458 	tc_windup();
   1459 	mutex_spin_exit(&timecounter_lock);
   1460 }
   1461 
   1462 void
   1463 inittimecounter(void)
   1464 {
   1465 	u_int p;
   1466 
   1467 	mutex_init(&timecounter_lock, MUTEX_DEFAULT, IPL_HIGH);
   1468 
   1469 	/*
   1470 	 * Set the initial timeout to
   1471 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
   1472 	 * People should probably not use the sysctl to set the timeout
   1473 	 * to smaller than its initial value, since that value is the
   1474 	 * smallest reasonable one.  If they want better timestamps they
   1475 	 * should use the non-"get"* functions.
   1476 	 */
   1477 	if (hz > 1000)
   1478 		tc_tick = (hz + 500) / 1000;
   1479 	else
   1480 		tc_tick = 1;
   1481 	p = (tc_tick * 1000000) / hz;
   1482 	aprint_verbose("timecounter: Timecounters tick every %d.%03u msec\n",
   1483 	    p / 1000, p % 1000);
   1484 
   1485 	/* warm up new timecounter (again) and get rolling. */
   1486 	(void)timecounter->tc_get_timecount(timecounter);
   1487 	(void)timecounter->tc_get_timecount(timecounter);
   1488 }
   1489