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