Home | History | Annotate | Line # | Download | only in kern
kern_tc.c revision 1.71
      1 /* $NetBSD: kern_tc.c,v 1.71 2023/07/17 21:51: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.71 2023/07/17 21:51: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 #ifdef __HAVE_ATOMIC64_LOADSTORE
    137 volatile time_t time__second __cacheline_aligned = 1;
    138 volatile time_t time__uptime __cacheline_aligned = 1;
    139 #else
    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 {
    150 	struct bintime bin;
    151 	volatile unsigned gen;	/* even when stable, odd when changing */
    152 } timebase __cacheline_aligned;
    153 
    154 static int timestepwarnings;
    155 
    156 kmutex_t timecounter_lock;
    157 static u_int timecounter_mods;
    158 static volatile int timecounter_removals = 1;
    159 static u_int timecounter_bad;
    160 
    161 #ifdef __HAVE_ATOMIC64_LOADSTORE
    162 
    163 static inline void
    164 setrealuptime(time_t second, time_t uptime)
    165 {
    166 
    167 	atomic_store_relaxed(&time__second, second);
    168 	atomic_store_relaxed(&time__uptime, uptime);
    169 }
    170 
    171 #else
    172 
    173 static inline void
    174 setrealuptime(time_t second, time_t uptime)
    175 {
    176 	uint32_t seclo = second & 0xffffffff, sechi = second >> 32;
    177 	uint32_t uplo = uptime & 0xffffffff, uphi = uptime >> 32;
    178 
    179 	KDASSERT(mutex_owned(&timecounter_lock));
    180 
    181 	/*
    182 	 * Fast path -- no wraparound, just updating the low bits, so
    183 	 * no need for seqlocked access.
    184 	 */
    185 	if (__predict_true(sechi == time__second32.hi) &&
    186 	    __predict_true(uphi == time__uptime32.hi)) {
    187 		atomic_store_relaxed(&time__second32.lo, seclo);
    188 		atomic_store_relaxed(&time__uptime32.lo, uplo);
    189 		return;
    190 	}
    191 
    192 	atomic_store_relaxed(&time__second32.hi, 0xffffffff);
    193 	atomic_store_relaxed(&time__uptime32.hi, 0xffffffff);
    194 	membar_producer();
    195 	atomic_store_relaxed(&time__second32.lo, seclo);
    196 	atomic_store_relaxed(&time__uptime32.lo, uplo);
    197 	membar_producer();
    198 	atomic_store_relaxed(&time__second32.hi, sechi);
    199 	atomic_store_relaxed(&time__uptime32.hi, uphi);
    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 	struct bintime boottime;
    503 
    504 	TC_COUNT(nbintime);
    505 	binuptime(bt);
    506 	getbinboottime(&boottime);
    507 	bintime_add(bt, &boottime);
    508 }
    509 
    510 void
    511 nanotime(struct timespec *tsp)
    512 {
    513 	struct bintime bt;
    514 
    515 	TC_COUNT(nnanotime);
    516 	bintime(&bt);
    517 	bintime2timespec(&bt, tsp);
    518 }
    519 
    520 void
    521 microtime(struct timeval *tvp)
    522 {
    523 	struct bintime bt;
    524 
    525 	TC_COUNT(nmicrotime);
    526 	bintime(&bt);
    527 	bintime2timeval(&bt, tvp);
    528 }
    529 
    530 void
    531 getbinuptime(struct bintime *bt)
    532 {
    533 	struct timehands *th;
    534 	u_int gen;
    535 
    536 	TC_COUNT(ngetbinuptime);
    537 	do {
    538 		th = timehands;
    539 		gen = th->th_generation;
    540 		*bt = th->th_offset;
    541 	} while (gen == 0 || gen != th->th_generation);
    542 }
    543 
    544 void
    545 getnanouptime(struct timespec *tsp)
    546 {
    547 	struct timehands *th;
    548 	u_int gen;
    549 
    550 	TC_COUNT(ngetnanouptime);
    551 	do {
    552 		th = timehands;
    553 		gen = th->th_generation;
    554 		bintime2timespec(&th->th_offset, tsp);
    555 	} while (gen == 0 || gen != th->th_generation);
    556 }
    557 
    558 void
    559 getmicrouptime(struct timeval *tvp)
    560 {
    561 	struct timehands *th;
    562 	u_int gen;
    563 
    564 	TC_COUNT(ngetmicrouptime);
    565 	do {
    566 		th = timehands;
    567 		gen = th->th_generation;
    568 		bintime2timeval(&th->th_offset, tvp);
    569 	} while (gen == 0 || gen != th->th_generation);
    570 }
    571 
    572 void
    573 getbintime(struct bintime *bt)
    574 {
    575 	struct timehands *th;
    576 	struct bintime boottime;
    577 	u_int gen;
    578 
    579 	TC_COUNT(ngetbintime);
    580 	do {
    581 		th = timehands;
    582 		gen = th->th_generation;
    583 		*bt = th->th_offset;
    584 	} while (gen == 0 || gen != th->th_generation);
    585 	getbinboottime(&boottime);
    586 	bintime_add(bt, &boottime);
    587 }
    588 
    589 static inline void
    590 dogetnanotime(struct timespec *tsp)
    591 {
    592 	struct timehands *th;
    593 	u_int gen;
    594 
    595 	TC_COUNT(ngetnanotime);
    596 	do {
    597 		th = timehands;
    598 		gen = th->th_generation;
    599 		*tsp = th->th_nanotime;
    600 	} while (gen == 0 || gen != th->th_generation);
    601 }
    602 
    603 void
    604 getnanotime(struct timespec *tsp)
    605 {
    606 
    607 	dogetnanotime(tsp);
    608 }
    609 
    610 void dtrace_getnanotime(struct timespec *tsp);
    611 
    612 void
    613 dtrace_getnanotime(struct timespec *tsp)
    614 {
    615 
    616 	dogetnanotime(tsp);
    617 }
    618 
    619 void
    620 getmicrotime(struct timeval *tvp)
    621 {
    622 	struct timehands *th;
    623 	u_int gen;
    624 
    625 	TC_COUNT(ngetmicrotime);
    626 	do {
    627 		th = timehands;
    628 		gen = th->th_generation;
    629 		*tvp = th->th_microtime;
    630 	} while (gen == 0 || gen != th->th_generation);
    631 }
    632 
    633 void
    634 getnanoboottime(struct timespec *tsp)
    635 {
    636 	struct bintime bt;
    637 
    638 	getbinboottime(&bt);
    639 	bintime2timespec(&bt, tsp);
    640 }
    641 
    642 void
    643 getmicroboottime(struct timeval *tvp)
    644 {
    645 	struct bintime bt;
    646 
    647 	getbinboottime(&bt);
    648 	bintime2timeval(&bt, tvp);
    649 }
    650 
    651 void
    652 getbinboottime(struct bintime *basep)
    653 {
    654 	struct bintime base;
    655 	unsigned gen;
    656 
    657 	do {
    658 		/* Spin until the timebase isn't changing.  */
    659 		while ((gen = atomic_load_relaxed(&timebase.gen)) & 1)
    660 			SPINLOCK_BACKOFF_HOOK;
    661 
    662 		/* Read out a snapshot of the timebase.  */
    663 		membar_consumer();
    664 		base = timebase.bin;
    665 		membar_consumer();
    666 
    667 		/* Restart if it changed while we were reading.  */
    668 	} while (gen != atomic_load_relaxed(&timebase.gen));
    669 
    670 	*basep = base;
    671 }
    672 
    673 /*
    674  * Initialize a new timecounter and possibly use it.
    675  */
    676 void
    677 tc_init(struct timecounter *tc)
    678 {
    679 	u_int u;
    680 
    681 	KASSERTMSG(tc->tc_next == NULL, "timecounter %s already initialised",
    682 	    tc->tc_name);
    683 
    684 	u = tc->tc_frequency / tc->tc_counter_mask;
    685 	/* XXX: We need some margin here, 10% is a guess */
    686 	u *= 11;
    687 	u /= 10;
    688 	if (u > hz && tc->tc_quality >= 0) {
    689 		tc->tc_quality = -2000;
    690 		aprint_verbose(
    691 		    "timecounter: Timecounter \"%s\" frequency %ju Hz",
    692 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
    693 		aprint_verbose(" -- Insufficient hz, needs at least %u\n", u);
    694 	} else if (tc->tc_quality >= 0 || bootverbose) {
    695 		aprint_verbose(
    696 		    "timecounter: Timecounter \"%s\" frequency %ju Hz "
    697 		    "quality %d\n", tc->tc_name, (uintmax_t)tc->tc_frequency,
    698 		    tc->tc_quality);
    699 	}
    700 
    701 	mutex_spin_enter(&timecounter_lock);
    702 	tc->tc_next = timecounters;
    703 	timecounters = tc;
    704 	timecounter_mods++;
    705 	/*
    706 	 * Never automatically use a timecounter with negative quality.
    707 	 * Even though we run on the dummy counter, switching here may be
    708 	 * worse since this timecounter may not be monotonous.
    709 	 */
    710 	if (tc->tc_quality >= 0 && (tc->tc_quality > timecounter->tc_quality ||
    711 	    (tc->tc_quality == timecounter->tc_quality &&
    712 	    tc->tc_frequency > timecounter->tc_frequency))) {
    713 		(void)tc->tc_get_timecount(tc);
    714 		(void)tc->tc_get_timecount(tc);
    715 		timecounter = tc;
    716 		tc_windup();
    717 	}
    718 	mutex_spin_exit(&timecounter_lock);
    719 }
    720 
    721 /*
    722  * Pick a new timecounter due to the existing counter going bad.
    723  */
    724 static void
    725 tc_pick(void)
    726 {
    727 	struct timecounter *best, *tc;
    728 
    729 	KASSERT(mutex_owned(&timecounter_lock));
    730 
    731 	for (best = tc = timecounters; tc != NULL; tc = tc->tc_next) {
    732 		if (tc->tc_quality > best->tc_quality)
    733 			best = tc;
    734 		else if (tc->tc_quality < best->tc_quality)
    735 			continue;
    736 		else if (tc->tc_frequency > best->tc_frequency)
    737 			best = tc;
    738 	}
    739 	(void)best->tc_get_timecount(best);
    740 	(void)best->tc_get_timecount(best);
    741 	timecounter = best;
    742 }
    743 
    744 /*
    745  * A timecounter has gone bad, arrange to pick a new one at the next
    746  * clock tick.
    747  */
    748 void
    749 tc_gonebad(struct timecounter *tc)
    750 {
    751 
    752 	tc->tc_quality = -100;
    753 	membar_producer();
    754 	atomic_inc_uint(&timecounter_bad);
    755 }
    756 
    757 /*
    758  * Stop using a timecounter and remove it from the timecounters list.
    759  */
    760 int
    761 tc_detach(struct timecounter *target)
    762 {
    763 	struct timecounter *tc;
    764 	struct timecounter **tcp = NULL;
    765 	int removals;
    766 	lwp_t *l;
    767 
    768 	/* First, find the timecounter. */
    769 	mutex_spin_enter(&timecounter_lock);
    770 	for (tcp = &timecounters, tc = timecounters;
    771 	     tc != NULL;
    772 	     tcp = &tc->tc_next, tc = tc->tc_next) {
    773 		if (tc == target)
    774 			break;
    775 	}
    776 	if (tc == NULL) {
    777 		mutex_spin_exit(&timecounter_lock);
    778 		return ESRCH;
    779 	}
    780 
    781 	/* And now, remove it. */
    782 	*tcp = tc->tc_next;
    783 	if (timecounter == target) {
    784 		tc_pick();
    785 		tc_windup();
    786 	}
    787 	timecounter_mods++;
    788 	removals = timecounter_removals++;
    789 	mutex_spin_exit(&timecounter_lock);
    790 
    791 	/*
    792 	 * We now have to determine if any threads in the system are still
    793 	 * making use of this timecounter.
    794 	 *
    795 	 * We issue a broadcast cross call to elide memory ordering issues,
    796 	 * then scan all LWPs in the system looking at each's timecounter
    797 	 * generation number.  We need to see a value of zero (not actively
    798 	 * using a timecounter) or a value greater than our removal value.
    799 	 *
    800 	 * We may race with threads that read `timecounter_removals' and
    801 	 * and then get preempted before updating `l_tcgen'.  This is not
    802 	 * a problem, since it means that these threads have not yet started
    803 	 * accessing timecounter state.  All we do need is one clean
    804 	 * snapshot of the system where every thread appears not to be using
    805 	 * old timecounter state.
    806 	 */
    807 	for (;;) {
    808 		xc_barrier(0);
    809 
    810 		mutex_enter(&proc_lock);
    811 		LIST_FOREACH(l, &alllwp, l_list) {
    812 			if (l->l_tcgen == 0 || l->l_tcgen > removals) {
    813 				/*
    814 				 * Not using timecounter or old timecounter
    815 				 * state at time of our xcall or later.
    816 				 */
    817 				continue;
    818 			}
    819 			break;
    820 		}
    821 		mutex_exit(&proc_lock);
    822 
    823 		/*
    824 		 * If the timecounter is still in use, wait at least 10ms
    825 		 * before retrying.
    826 		 */
    827 		if (l == NULL) {
    828 			break;
    829 		}
    830 		(void)kpause("tcdetach", false, mstohz(10), NULL);
    831 	}
    832 
    833 	tc->tc_next = NULL;
    834 	return 0;
    835 }
    836 
    837 /* Report the frequency of the current timecounter. */
    838 uint64_t
    839 tc_getfrequency(void)
    840 {
    841 
    842 	return timehands->th_counter->tc_frequency;
    843 }
    844 
    845 /*
    846  * Step our concept of UTC.  This is done by modifying our estimate of
    847  * when we booted.
    848  */
    849 void
    850 tc_setclock(const struct timespec *ts)
    851 {
    852 	struct timespec ts2;
    853 	struct bintime bt, bt2;
    854 
    855 	mutex_spin_enter(&timecounter_lock);
    856 	TC_COUNT(nsetclock);
    857 	binuptime(&bt2);
    858 	timespec2bintime(ts, &bt);
    859 	bintime_sub(&bt, &bt2);
    860 	bintime_add(&bt2, &timebase.bin);
    861 	timebase.gen |= 1;	/* change in progress */
    862 	membar_producer();
    863 	timebase.bin = bt;
    864 	membar_producer();
    865 	timebase.gen++;		/* commit change */
    866 	tc_windup();
    867 	mutex_spin_exit(&timecounter_lock);
    868 
    869 	if (timestepwarnings) {
    870 		bintime2timespec(&bt2, &ts2);
    871 		log(LOG_INFO,
    872 		    "Time stepped from %lld.%09ld to %lld.%09ld\n",
    873 		    (long long)ts2.tv_sec, ts2.tv_nsec,
    874 		    (long long)ts->tv_sec, ts->tv_nsec);
    875 	}
    876 }
    877 
    878 /*
    879  * Initialize the next struct timehands in the ring and make
    880  * it the active timehands.  Along the way we might switch to a different
    881  * timecounter and/or do seconds processing in NTP.  Slightly magic.
    882  */
    883 static void
    884 tc_windup(void)
    885 {
    886 	struct bintime bt;
    887 	struct timehands *th, *tho;
    888 	uint64_t scale;
    889 	u_int delta, ncount, ogen;
    890 	int i, s_update;
    891 	time_t t;
    892 
    893 	KASSERT(mutex_owned(&timecounter_lock));
    894 
    895 	s_update = 0;
    896 
    897 	/*
    898 	 * Make the next timehands a copy of the current one, but do not
    899 	 * overwrite the generation or next pointer.  While we update
    900 	 * the contents, the generation must be zero.  Ensure global
    901 	 * visibility of the generation before proceeding.
    902 	 */
    903 	tho = timehands;
    904 	th = tho->th_next;
    905 	ogen = th->th_generation;
    906 	th->th_generation = 0;
    907 	membar_producer();
    908 	bcopy(tho, th, offsetof(struct timehands, th_generation));
    909 
    910 	/*
    911 	 * Capture a timecounter delta on the current timecounter and if
    912 	 * changing timecounters, a counter value from the new timecounter.
    913 	 * Update the offset fields accordingly.
    914 	 */
    915 	delta = tc_delta(th);
    916 	if (th->th_counter != timecounter)
    917 		ncount = timecounter->tc_get_timecount(timecounter);
    918 	else
    919 		ncount = 0;
    920 	th->th_offset_count += delta;
    921 	bintime_addx(&th->th_offset, th->th_scale * delta);
    922 
    923 	/*
    924 	 * Hardware latching timecounters may not generate interrupts on
    925 	 * PPS events, so instead we poll them.  There is a finite risk that
    926 	 * the hardware might capture a count which is later than the one we
    927 	 * got above, and therefore possibly in the next NTP second which might
    928 	 * have a different rate than the current NTP second.  It doesn't
    929 	 * matter in practice.
    930 	 */
    931 	if (tho->th_counter->tc_poll_pps)
    932 		tho->th_counter->tc_poll_pps(tho->th_counter);
    933 
    934 	/*
    935 	 * Deal with NTP second processing.  The for loop normally
    936 	 * iterates at most once, but in extreme situations it might
    937 	 * keep NTP sane if timeouts are not run for several seconds.
    938 	 * At boot, the time step can be large when the TOD hardware
    939 	 * has been read, so on really large steps, we call
    940 	 * ntp_update_second only twice.  We need to call it twice in
    941 	 * case we missed a leap second.
    942 	 * If NTP is not compiled in ntp_update_second still calculates
    943 	 * the adjustment resulting from adjtime() calls.
    944 	 */
    945 	bt = th->th_offset;
    946 	bintime_add(&bt, &timebase.bin);
    947 	i = bt.sec - tho->th_microtime.tv_sec;
    948 	if (i > LARGE_STEP)
    949 		i = 2;
    950 	for (; i > 0; i--) {
    951 		t = bt.sec;
    952 		ntp_update_second(&th->th_adjustment, &bt.sec);
    953 		s_update = 1;
    954 		if (bt.sec != t) {
    955 			timebase.gen |= 1;	/* change in progress */
    956 			membar_producer();
    957 			timebase.bin.sec += bt.sec - t;
    958 			membar_producer();
    959 			timebase.gen++;		/* commit change */
    960 		}
    961 	}
    962 
    963 	/* Update the UTC timestamps used by the get*() functions. */
    964 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
    965 	bintime2timeval(&bt, &th->th_microtime);
    966 	bintime2timespec(&bt, &th->th_nanotime);
    967 	/* Now is a good time to change timecounters. */
    968 	if (th->th_counter != timecounter) {
    969 		th->th_counter = timecounter;
    970 		th->th_offset_count = ncount;
    971 		s_update = 1;
    972 	}
    973 
    974 	/*-
    975 	 * Recalculate the scaling factor.  We want the number of 1/2^64
    976 	 * fractions of a second per period of the hardware counter, taking
    977 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
    978 	 * processing provides us with.
    979 	 *
    980 	 * The th_adjustment is nanoseconds per second with 32 bit binary
    981 	 * fraction and we want 64 bit binary fraction of second:
    982 	 *
    983 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
    984 	 *
    985 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
    986 	 * we can only multiply by about 850 without overflowing, but that
    987 	 * leaves suitably precise fractions for multiply before divide.
    988 	 *
    989 	 * Divide before multiply with a fraction of 2199/512 results in a
    990 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
    991 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
    992  	 *
    993 	 * We happily sacrifice the lowest of the 64 bits of our result
    994 	 * to the goddess of code clarity.
    995 	 *
    996 	 */
    997 	if (s_update) {
    998 		scale = (uint64_t)1 << 63;
    999 		scale += (th->th_adjustment / 1024) * 2199;
   1000 		scale /= th->th_counter->tc_frequency;
   1001 		th->th_scale = scale * 2;
   1002 	}
   1003 	/*
   1004 	 * Now that the struct timehands is again consistent, set the new
   1005 	 * generation number, making sure to not make it zero.  Ensure
   1006 	 * changes are globally visible before changing.
   1007 	 */
   1008 	if (++ogen == 0)
   1009 		ogen = 1;
   1010 	membar_producer();
   1011 	th->th_generation = ogen;
   1012 
   1013 	/*
   1014 	 * Go live with the new struct timehands.  Ensure changes are
   1015 	 * globally visible before changing.
   1016 	 */
   1017 	setrealuptime(th->th_microtime.tv_sec, th->th_offset.sec);
   1018 	membar_producer();
   1019 	timehands = th;
   1020 
   1021 	/*
   1022 	 * Force users of the old timehand to move on.  This is
   1023 	 * necessary for MP systems; we need to ensure that the
   1024 	 * consumers will move away from the old timehand before
   1025 	 * we begin updating it again when we eventually wrap
   1026 	 * around.
   1027 	 */
   1028 	if (++tho->th_generation == 0)
   1029 		tho->th_generation = 1;
   1030 }
   1031 
   1032 /*
   1033  * RFC 2783 PPS-API implementation.
   1034  */
   1035 
   1036 int
   1037 pps_ioctl(u_long cmd, void *data, struct pps_state *pps)
   1038 {
   1039 	pps_params_t *app;
   1040 	pps_info_t *pipi;
   1041 #ifdef PPS_SYNC
   1042 	int *epi;
   1043 #endif
   1044 
   1045 	KASSERT(mutex_owned(&timecounter_lock));
   1046 
   1047 	KASSERT(pps != NULL);
   1048 
   1049 	switch (cmd) {
   1050 	case PPS_IOC_CREATE:
   1051 		return 0;
   1052 	case PPS_IOC_DESTROY:
   1053 		return 0;
   1054 	case PPS_IOC_SETPARAMS:
   1055 		app = (pps_params_t *)data;
   1056 		if (app->mode & ~pps->ppscap)
   1057 			return EINVAL;
   1058 		pps->ppsparam = *app;
   1059 		return 0;
   1060 	case PPS_IOC_GETPARAMS:
   1061 		app = (pps_params_t *)data;
   1062 		*app = pps->ppsparam;
   1063 		app->api_version = PPS_API_VERS_1;
   1064 		return 0;
   1065 	case PPS_IOC_GETCAP:
   1066 		*(int*)data = pps->ppscap;
   1067 		return 0;
   1068 	case PPS_IOC_FETCH:
   1069 		pipi = (pps_info_t *)data;
   1070 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
   1071 		*pipi = pps->ppsinfo;
   1072 		return 0;
   1073 	case PPS_IOC_KCBIND:
   1074 #ifdef PPS_SYNC
   1075 		epi = (int *)data;
   1076 		/* XXX Only root should be able to do this */
   1077 		if (*epi & ~pps->ppscap)
   1078 			return EINVAL;
   1079 		pps->kcmode = *epi;
   1080 		return 0;
   1081 #else
   1082 		return EOPNOTSUPP;
   1083 #endif
   1084 	default:
   1085 		return EPASSTHROUGH;
   1086 	}
   1087 }
   1088 
   1089 void
   1090 pps_init(struct pps_state *pps)
   1091 {
   1092 
   1093 	KASSERT(mutex_owned(&timecounter_lock));
   1094 
   1095 	pps->ppscap |= PPS_TSFMT_TSPEC;
   1096 	if (pps->ppscap & PPS_CAPTUREASSERT)
   1097 		pps->ppscap |= PPS_OFFSETASSERT;
   1098 	if (pps->ppscap & PPS_CAPTURECLEAR)
   1099 		pps->ppscap |= PPS_OFFSETCLEAR;
   1100 }
   1101 
   1102 /*
   1103  * capture a timetamp in the pps structure
   1104  */
   1105 void
   1106 pps_capture(struct pps_state *pps)
   1107 {
   1108 	struct timehands *th;
   1109 
   1110 	KASSERT(mutex_owned(&timecounter_lock));
   1111 	KASSERT(pps != NULL);
   1112 
   1113 	th = timehands;
   1114 	pps->capgen = th->th_generation;
   1115 	pps->capth = th;
   1116 	pps->capcount = (uint64_t)tc_delta(th) + th->th_offset_count;
   1117 	if (pps->capgen != th->th_generation)
   1118 		pps->capgen = 0;
   1119 }
   1120 
   1121 #ifdef PPS_DEBUG
   1122 int ppsdebug = 0;
   1123 #endif
   1124 
   1125 /*
   1126  * process a pps_capture()ed event
   1127  */
   1128 void
   1129 pps_event(struct pps_state *pps, int event)
   1130 {
   1131 	pps_ref_event(pps, event, NULL, PPS_REFEVNT_PPS|PPS_REFEVNT_CAPTURE);
   1132 }
   1133 
   1134 /*
   1135  * extended pps api /  kernel pll/fll entry point
   1136  *
   1137  * feed reference time stamps to PPS engine
   1138  *
   1139  * will simulate a PPS event and feed
   1140  * the NTP PLL/FLL if requested.
   1141  *
   1142  * the ref time stamps should be roughly once
   1143  * a second but do not need to be exactly in phase
   1144  * with the UTC second but should be close to it.
   1145  * this relaxation of requirements allows callout
   1146  * driven timestamping mechanisms to feed to pps
   1147  * capture/kernel pll logic.
   1148  *
   1149  * calling pattern is:
   1150  *  pps_capture() (for PPS_REFEVNT_{CAPTURE|CAPCUR})
   1151  *  read timestamp from reference source
   1152  *  pps_ref_event()
   1153  *
   1154  * supported refmodes:
   1155  *  PPS_REFEVNT_CAPTURE
   1156  *    use system timestamp of pps_capture()
   1157  *  PPS_REFEVNT_CURRENT
   1158  *    use system timestamp of this call
   1159  *  PPS_REFEVNT_CAPCUR
   1160  *    use average of read capture and current system time stamp
   1161  *  PPS_REFEVNT_PPS
   1162  *    assume timestamp on second mark - ref_ts is ignored
   1163  *
   1164  */
   1165 
   1166 void
   1167 pps_ref_event(struct pps_state *pps,
   1168 	      int event,
   1169 	      struct bintime *ref_ts,
   1170 	      int refmode
   1171 	)
   1172 {
   1173 	struct bintime bt;	/* current time */
   1174 	struct bintime btd;	/* time difference */
   1175 	struct bintime bt_ref;	/* reference time */
   1176 	struct timespec ts, *tsp, *osp;
   1177 	struct timehands *th;
   1178 	uint64_t tcount, acount, dcount, *pcount;
   1179 	int foff, gen;
   1180 #ifdef PPS_SYNC
   1181 	int fhard;
   1182 #endif
   1183 	pps_seq_t *pseq;
   1184 
   1185 	KASSERT(mutex_owned(&timecounter_lock));
   1186 
   1187 	KASSERT(pps != NULL);
   1188 
   1189         /* pick up current time stamp if needed */
   1190 	if (refmode & (PPS_REFEVNT_CURRENT|PPS_REFEVNT_CAPCUR)) {
   1191 		/* pick up current time stamp */
   1192 		th = timehands;
   1193 		gen = th->th_generation;
   1194 		tcount = (uint64_t)tc_delta(th) + th->th_offset_count;
   1195 		if (gen != th->th_generation)
   1196 			gen = 0;
   1197 
   1198 		/* If the timecounter was wound up underneath us, bail out. */
   1199 		if (pps->capgen == 0 ||
   1200 		    pps->capgen != pps->capth->th_generation ||
   1201 		    gen == 0 ||
   1202 		    gen != pps->capgen) {
   1203 #ifdef PPS_DEBUG
   1204 			if (ppsdebug & 0x1) {
   1205 				log(LOG_DEBUG,
   1206 				    "pps_ref_event(pps=%p, event=%d, ...): DROP (wind-up)\n",
   1207 				    pps, event);
   1208 			}
   1209 #endif
   1210 			return;
   1211 		}
   1212 	} else {
   1213 		tcount = 0;	/* keep GCC happy */
   1214 	}
   1215 
   1216 #ifdef PPS_DEBUG
   1217 	if (ppsdebug & 0x1) {
   1218 		struct timespec tmsp;
   1219 
   1220 		if (ref_ts == NULL) {
   1221 			tmsp.tv_sec = 0;
   1222 			tmsp.tv_nsec = 0;
   1223 		} else {
   1224 			bintime2timespec(ref_ts, &tmsp);
   1225 		}
   1226 
   1227 		log(LOG_DEBUG,
   1228 		    "pps_ref_event(pps=%p, event=%d, ref_ts=%"PRIi64
   1229 		    ".%09"PRIi32", refmode=0x%1x)\n",
   1230 		    pps, event, tmsp.tv_sec, (int32_t)tmsp.tv_nsec, refmode);
   1231 	}
   1232 #endif
   1233 
   1234 	/* setup correct event references */
   1235 	if (event == PPS_CAPTUREASSERT) {
   1236 		tsp = &pps->ppsinfo.assert_timestamp;
   1237 		osp = &pps->ppsparam.assert_offset;
   1238 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
   1239 #ifdef PPS_SYNC
   1240 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
   1241 #endif
   1242 		pcount = &pps->ppscount[0];
   1243 		pseq = &pps->ppsinfo.assert_sequence;
   1244 	} else {
   1245 		tsp = &pps->ppsinfo.clear_timestamp;
   1246 		osp = &pps->ppsparam.clear_offset;
   1247 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
   1248 #ifdef PPS_SYNC
   1249 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
   1250 #endif
   1251 		pcount = &pps->ppscount[1];
   1252 		pseq = &pps->ppsinfo.clear_sequence;
   1253 	}
   1254 
   1255 	/* determine system time stamp according to refmode */
   1256 	dcount = 0;		/* keep GCC happy */
   1257 	switch (refmode & PPS_REFEVNT_RMASK) {
   1258 	case PPS_REFEVNT_CAPTURE:
   1259 		acount = pps->capcount;	/* use capture timestamp */
   1260 		break;
   1261 
   1262 	case PPS_REFEVNT_CURRENT:
   1263 		acount = tcount; /* use current timestamp */
   1264 		break;
   1265 
   1266 	case PPS_REFEVNT_CAPCUR:
   1267 		/*
   1268 		 * calculate counter value between pps_capture() and
   1269 		 * pps_ref_event()
   1270 		 */
   1271 		dcount = tcount - pps->capcount;
   1272 		acount = (dcount / 2) + pps->capcount;
   1273 		break;
   1274 
   1275 	default:		/* ignore call error silently */
   1276 		return;
   1277 	}
   1278 
   1279 	/*
   1280 	 * If the timecounter changed, we cannot compare the count values, so
   1281 	 * we have to drop the rest of the PPS-stuff until the next event.
   1282 	 */
   1283 	if (pps->ppstc != pps->capth->th_counter) {
   1284 		pps->ppstc = pps->capth->th_counter;
   1285 		pps->capcount = acount;
   1286 		*pcount = acount;
   1287 		pps->ppscount[2] = acount;
   1288 #ifdef PPS_DEBUG
   1289 		if (ppsdebug & 0x1) {
   1290 			log(LOG_DEBUG,
   1291 			    "pps_ref_event(pps=%p, event=%d, ...): DROP (time-counter change)\n",
   1292 			    pps, event);
   1293 		}
   1294 #endif
   1295 		return;
   1296 	}
   1297 
   1298 	pps->capcount = acount;
   1299 
   1300 	/* Convert the count to a bintime. */
   1301 	bt = pps->capth->th_offset;
   1302 	bintime_addx(&bt, pps->capth->th_scale * (acount - pps->capth->th_offset_count));
   1303 	bintime_add(&bt, &timebase.bin);
   1304 
   1305 	if ((refmode & PPS_REFEVNT_PPS) == 0) {
   1306 		/* determine difference to reference time stamp */
   1307 		bt_ref = *ref_ts;
   1308 
   1309 		btd = bt;
   1310 		bintime_sub(&btd, &bt_ref);
   1311 
   1312 		/*
   1313 		 * simulate a PPS timestamp by dropping the fraction
   1314 		 * and applying the offset
   1315 		 */
   1316 		if (bt.frac >= (uint64_t)1<<63)	/* skip to nearest second */
   1317 			bt.sec++;
   1318 		bt.frac = 0;
   1319 		bintime_add(&bt, &btd);
   1320 	} else {
   1321 		/*
   1322 		 * create ref_ts from current time -
   1323 		 * we are supposed to be called on
   1324 		 * the second mark
   1325 		 */
   1326 		bt_ref = bt;
   1327 		if (bt_ref.frac >= (uint64_t)1<<63)	/* skip to nearest second */
   1328 			bt_ref.sec++;
   1329 		bt_ref.frac = 0;
   1330 	}
   1331 
   1332 	/* convert bintime to timestamp */
   1333 	bintime2timespec(&bt, &ts);
   1334 
   1335 	/* If the timecounter was wound up underneath us, bail out. */
   1336 	if (pps->capgen != pps->capth->th_generation)
   1337 		return;
   1338 
   1339 	/* store time stamp */
   1340 	*pcount = pps->capcount;
   1341 	(*pseq)++;
   1342 	*tsp = ts;
   1343 
   1344 	/* add offset correction */
   1345 	if (foff) {
   1346 		timespecadd(tsp, osp, tsp);
   1347 		if (tsp->tv_nsec < 0) {
   1348 			tsp->tv_nsec += 1000000000;
   1349 			tsp->tv_sec -= 1;
   1350 		}
   1351 	}
   1352 
   1353 #ifdef PPS_DEBUG
   1354 	if (ppsdebug & 0x2) {
   1355 		struct timespec ts2;
   1356 		struct timespec ts3;
   1357 
   1358 		bintime2timespec(&bt_ref, &ts2);
   1359 
   1360 		bt.sec = 0;
   1361 		bt.frac = 0;
   1362 
   1363 		if (refmode & PPS_REFEVNT_CAPCUR) {
   1364 			    bintime_addx(&bt, pps->capth->th_scale * dcount);
   1365 		}
   1366 		bintime2timespec(&bt, &ts3);
   1367 
   1368 		log(LOG_DEBUG, "ref_ts=%"PRIi64".%09"PRIi32
   1369 		    ", ts=%"PRIi64".%09"PRIi32", read latency=%"PRIi64" ns\n",
   1370 		    ts2.tv_sec, (int32_t)ts2.tv_nsec,
   1371 		    tsp->tv_sec, (int32_t)tsp->tv_nsec,
   1372 		    timespec2ns(&ts3));
   1373 	}
   1374 #endif
   1375 
   1376 #ifdef PPS_SYNC
   1377 	if (fhard) {
   1378 		uint64_t scale;
   1379 		uint64_t div;
   1380 
   1381 		/*
   1382 		 * Feed the NTP PLL/FLL.
   1383 		 * The FLL wants to know how many (hardware) nanoseconds
   1384 		 * elapsed since the previous event (mod 1 second) thus
   1385 		 * we are actually looking at the frequency difference scaled
   1386 		 * in nsec.
   1387 		 * As the counter time stamps are not truly at 1Hz
   1388 		 * we need to scale the count by the elapsed
   1389 		 * reference time.
   1390 		 * valid sampling interval: [0.5..2[ sec
   1391 		 */
   1392 
   1393 		/* calculate elapsed raw count */
   1394 		tcount = pps->capcount - pps->ppscount[2];
   1395 		pps->ppscount[2] = pps->capcount;
   1396 		tcount &= pps->capth->th_counter->tc_counter_mask;
   1397 
   1398 		/* calculate elapsed ref time */
   1399 		btd = bt_ref;
   1400 		bintime_sub(&btd, &pps->ref_time);
   1401 		pps->ref_time = bt_ref;
   1402 
   1403 		/* check that we stay below 2 sec */
   1404 		if (btd.sec < 0 || btd.sec > 1)
   1405 			return;
   1406 
   1407 		/* we want at least 0.5 sec between samples */
   1408 		if (btd.sec == 0 && btd.frac < (uint64_t)1<<63)
   1409 			return;
   1410 
   1411 		/*
   1412 		 * calculate cycles per period by multiplying
   1413 		 * the frequency with the elapsed period
   1414 		 * we pick a fraction of 30 bits
   1415 		 * ~1ns resolution for elapsed time
   1416 		 */
   1417 		div   = (uint64_t)btd.sec << 30;
   1418 		div  |= (btd.frac >> 34) & (((uint64_t)1 << 30) - 1);
   1419 		div  *= pps->capth->th_counter->tc_frequency;
   1420 		div >>= 30;
   1421 
   1422 		if (div == 0)	/* safeguard */
   1423 			return;
   1424 
   1425 		scale = (uint64_t)1 << 63;
   1426 		scale /= div;
   1427 		scale *= 2;
   1428 
   1429 		bt.sec = 0;
   1430 		bt.frac = 0;
   1431 		bintime_addx(&bt, scale * tcount);
   1432 		bintime2timespec(&bt, &ts);
   1433 
   1434 #ifdef PPS_DEBUG
   1435 		if (ppsdebug & 0x4) {
   1436 			struct timespec ts2;
   1437 			int64_t df;
   1438 
   1439 			bintime2timespec(&bt_ref, &ts2);
   1440 			df = timespec2ns(&ts);
   1441 			if (df > 500000000)
   1442 				df -= 1000000000;
   1443 			log(LOG_DEBUG, "hardpps: ref_ts=%"PRIi64
   1444 			    ".%09"PRIi32", ts=%"PRIi64".%09"PRIi32
   1445 			    ", freqdiff=%"PRIi64" ns/s\n",
   1446 			    ts2.tv_sec, (int32_t)ts2.tv_nsec,
   1447 			    tsp->tv_sec, (int32_t)tsp->tv_nsec,
   1448 			    df);
   1449 		}
   1450 #endif
   1451 
   1452 		hardpps(tsp, timespec2ns(&ts));
   1453 	}
   1454 #endif
   1455 }
   1456 
   1457 /*
   1458  * Timecounters need to be updated every so often to prevent the hardware
   1459  * counter from overflowing.  Updating also recalculates the cached values
   1460  * used by the get*() family of functions, so their precision depends on
   1461  * the update frequency.
   1462  */
   1463 
   1464 static int tc_tick;
   1465 
   1466 void
   1467 tc_ticktock(void)
   1468 {
   1469 	static int count;
   1470 
   1471 	if (++count < tc_tick)
   1472 		return;
   1473 	count = 0;
   1474 	mutex_spin_enter(&timecounter_lock);
   1475 	if (__predict_false(timecounter_bad != 0)) {
   1476 		/* An existing timecounter has gone bad, pick a new one. */
   1477 		(void)atomic_swap_uint(&timecounter_bad, 0);
   1478 		if (timecounter->tc_quality < 0) {
   1479 			tc_pick();
   1480 		}
   1481 	}
   1482 	tc_windup();
   1483 	mutex_spin_exit(&timecounter_lock);
   1484 }
   1485 
   1486 void
   1487 inittimecounter(void)
   1488 {
   1489 	u_int p;
   1490 
   1491 	mutex_init(&timecounter_lock, MUTEX_DEFAULT, IPL_HIGH);
   1492 
   1493 	/*
   1494 	 * Set the initial timeout to
   1495 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
   1496 	 * People should probably not use the sysctl to set the timeout
   1497 	 * to smaller than its initial value, since that value is the
   1498 	 * smallest reasonable one.  If they want better timestamps they
   1499 	 * should use the non-"get"* functions.
   1500 	 */
   1501 	if (hz > 1000)
   1502 		tc_tick = (hz + 500) / 1000;
   1503 	else
   1504 		tc_tick = 1;
   1505 	p = (tc_tick * 1000000) / hz;
   1506 	aprint_verbose("timecounter: Timecounters tick every %d.%03u msec\n",
   1507 	    p / 1000, p % 1000);
   1508 
   1509 	/* warm up new timecounter (again) and get rolling. */
   1510 	(void)timecounter->tc_get_timecount(timecounter);
   1511 	(void)timecounter->tc_get_timecount(timecounter);
   1512 }
   1513