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