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