Home | History | Annotate | Line # | Download | only in kern
kern_tc.c revision 1.20.2.3
      1  1.20.2.3      matt /* kern_tc.c,v 1.20.2.2 2008/01/09 01:56:11 matt Exp */
      2       1.2    kardel 
      3       1.1    simonb /*-
      4       1.1    simonb  * ----------------------------------------------------------------------------
      5       1.1    simonb  * "THE BEER-WARE LICENSE" (Revision 42):
      6       1.1    simonb  * <phk (at) FreeBSD.ORG> wrote this file.  As long as you retain this notice you
      7       1.1    simonb  * can do whatever you want with this stuff. If we meet some day, and you think
      8       1.1    simonb  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
      9       1.2    kardel  * ---------------------------------------------------------------------------
     10       1.1    simonb  */
     11       1.1    simonb 
     12       1.1    simonb #include <sys/cdefs.h>
     13       1.2    kardel /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
     14  1.20.2.3      matt __KERNEL_RCSID(0, "kern_tc.c,v 1.20.2.2 2008/01/09 01:56:11 matt Exp");
     15       1.1    simonb 
     16       1.1    simonb #include "opt_ntp.h"
     17       1.1    simonb 
     18       1.1    simonb #include <sys/param.h>
     19       1.1    simonb #include <sys/kernel.h>
     20       1.2    kardel #include <sys/reboot.h>	/* XXX just to get AB_VERBOSE */
     21       1.1    simonb #include <sys/sysctl.h>
     22       1.1    simonb #include <sys/syslog.h>
     23       1.1    simonb #include <sys/systm.h>
     24       1.1    simonb #include <sys/timepps.h>
     25       1.1    simonb #include <sys/timetc.h>
     26       1.1    simonb #include <sys/timex.h>
     27       1.2    kardel #include <sys/evcnt.h>
     28       1.2    kardel #include <sys/kauth.h>
     29  1.20.2.2      matt #include <sys/mutex.h>
     30  1.20.2.2      matt #include <sys/atomic.h>
     31       1.2    kardel 
     32       1.2    kardel /*
     33       1.1    simonb  * A large step happens on boot.  This constant detects such steps.
     34       1.1    simonb  * It is relatively small so that ntp_update_second gets called enough
     35       1.1    simonb  * in the typical 'missed a couple of seconds' case, but doesn't loop
     36       1.1    simonb  * forever when the time step is large.
     37       1.1    simonb  */
     38       1.1    simonb #define LARGE_STEP	200
     39       1.1    simonb 
     40       1.1    simonb /*
     41       1.1    simonb  * Implement a dummy timecounter which we can use until we get a real one
     42       1.1    simonb  * in the air.  This allows the console and other early stuff to use
     43       1.1    simonb  * time services.
     44       1.1    simonb  */
     45       1.1    simonb 
     46       1.1    simonb static u_int
     47      1.16      yamt dummy_get_timecount(struct timecounter *tc)
     48       1.1    simonb {
     49       1.1    simonb 	static u_int now;
     50       1.1    simonb 
     51       1.1    simonb 	return (++now);
     52       1.1    simonb }
     53       1.1    simonb 
     54       1.1    simonb static struct timecounter dummy_timecounter = {
     55       1.8  christos 	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000, NULL, NULL,
     56       1.1    simonb };
     57       1.1    simonb 
     58       1.1    simonb struct timehands {
     59       1.1    simonb 	/* These fields must be initialized by the driver. */
     60       1.1    simonb 	struct timecounter	*th_counter;
     61       1.1    simonb 	int64_t			th_adjustment;
     62       1.1    simonb 	u_int64_t		th_scale;
     63       1.1    simonb 	u_int	 		th_offset_count;
     64       1.1    simonb 	struct bintime		th_offset;
     65       1.1    simonb 	struct timeval		th_microtime;
     66       1.1    simonb 	struct timespec		th_nanotime;
     67       1.1    simonb 	/* Fields not to be copied in tc_windup start with th_generation. */
     68       1.1    simonb 	volatile u_int		th_generation;
     69       1.1    simonb 	struct timehands	*th_next;
     70       1.1    simonb };
     71       1.1    simonb 
     72       1.1    simonb static struct timehands th0;
     73      1.10  christos static struct timehands th9 = { .th_next = &th0, };
     74      1.10  christos static struct timehands th8 = { .th_next = &th9, };
     75      1.10  christos static struct timehands th7 = { .th_next = &th8, };
     76      1.10  christos static struct timehands th6 = { .th_next = &th7, };
     77      1.10  christos static struct timehands th5 = { .th_next = &th6, };
     78      1.10  christos static struct timehands th4 = { .th_next = &th5, };
     79      1.10  christos static struct timehands th3 = { .th_next = &th4, };
     80      1.10  christos static struct timehands th2 = { .th_next = &th3, };
     81      1.10  christos static struct timehands th1 = { .th_next = &th2, };
     82       1.1    simonb static struct timehands th0 = {
     83      1.10  christos 	.th_counter = &dummy_timecounter,
     84      1.10  christos 	.th_scale = (uint64_t)-1 / 1000000,
     85      1.10  christos 	.th_offset = { .sec = 1, .frac = 0 },
     86      1.10  christos 	.th_generation = 1,
     87      1.10  christos 	.th_next = &th1,
     88       1.1    simonb };
     89       1.1    simonb 
     90       1.1    simonb static struct timehands *volatile timehands = &th0;
     91       1.1    simonb struct timecounter *timecounter = &dummy_timecounter;
     92       1.1    simonb static struct timecounter *timecounters = &dummy_timecounter;
     93       1.1    simonb 
     94       1.1    simonb time_t time_second = 1;
     95       1.1    simonb time_t time_uptime = 1;
     96       1.1    simonb 
     97       1.4    kardel static struct bintime timebasebin;
     98       1.1    simonb 
     99       1.1    simonb static int timestepwarnings;
    100       1.2    kardel 
    101  1.20.2.2      matt extern kmutex_t time_lock;
    102  1.20.2.2      matt static kmutex_t tc_windup_lock;
    103  1.20.2.2      matt 
    104       1.2    kardel #ifdef __FreeBSD__
    105       1.1    simonb SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
    106       1.1    simonb     &timestepwarnings, 0, "");
    107       1.2    kardel #endif /* __FreeBSD__ */
    108       1.2    kardel 
    109       1.2    kardel /*
    110  1.20.2.2      matt  * sysctl helper routine for kern.timercounter.hardware
    111       1.2    kardel  */
    112       1.2    kardel static int
    113       1.2    kardel sysctl_kern_timecounter_hardware(SYSCTLFN_ARGS)
    114       1.2    kardel {
    115       1.2    kardel 	struct sysctlnode node;
    116       1.2    kardel 	int error;
    117       1.2    kardel 	char newname[MAX_TCNAMELEN];
    118       1.2    kardel 	struct timecounter *newtc, *tc;
    119       1.2    kardel 
    120       1.2    kardel 	tc = timecounter;
    121       1.2    kardel 
    122       1.2    kardel 	strlcpy(newname, tc->tc_name, sizeof(newname));
    123       1.2    kardel 
    124       1.2    kardel 	node = *rnode;
    125       1.2    kardel 	node.sysctl_data = newname;
    126       1.2    kardel 	node.sysctl_size = sizeof(newname);
    127       1.2    kardel 
    128       1.2    kardel 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    129       1.2    kardel 
    130       1.2    kardel 	if (error ||
    131       1.2    kardel 	    newp == NULL ||
    132       1.2    kardel 	    strncmp(newname, tc->tc_name, sizeof(newname)) == 0)
    133       1.2    kardel 		return error;
    134       1.1    simonb 
    135  1.20.2.2      matt 	if (l != NULL && (error = kauth_authorize_system(l->l_cred,
    136  1.20.2.2      matt 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERS, newname,
    137  1.20.2.2      matt 	    NULL, NULL)) != 0)
    138       1.2    kardel 		return (error);
    139       1.2    kardel 
    140  1.20.2.2      matt 	if (!cold)
    141  1.20.2.2      matt 		mutex_enter(&time_lock);
    142  1.20.2.2      matt 	error = EINVAL;
    143       1.2    kardel 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
    144       1.2    kardel 		if (strcmp(newname, newtc->tc_name) != 0)
    145       1.2    kardel 			continue;
    146       1.2    kardel 		/* Warm up new timecounter. */
    147       1.2    kardel 		(void)newtc->tc_get_timecount(newtc);
    148       1.2    kardel 		(void)newtc->tc_get_timecount(newtc);
    149       1.2    kardel 		timecounter = newtc;
    150  1.20.2.2      matt 		error = 0;
    151  1.20.2.2      matt 		break;
    152       1.2    kardel 	}
    153  1.20.2.2      matt 	if (!cold)
    154  1.20.2.2      matt 		mutex_exit(&time_lock);
    155  1.20.2.2      matt 	return error;
    156       1.2    kardel }
    157       1.2    kardel 
    158       1.2    kardel static int
    159       1.2    kardel sysctl_kern_timecounter_choice(SYSCTLFN_ARGS)
    160       1.2    kardel {
    161       1.9    kardel 	char buf[MAX_TCNAMELEN+48];
    162       1.2    kardel 	char *where = oldp;
    163       1.2    kardel 	const char *spc;
    164       1.2    kardel 	struct timecounter *tc;
    165       1.2    kardel 	size_t needed, left, slen;
    166       1.2    kardel 	int error;
    167       1.2    kardel 
    168       1.2    kardel 	if (newp != NULL)
    169       1.2    kardel 		return (EPERM);
    170       1.2    kardel 	if (namelen != 0)
    171       1.2    kardel 		return (EINVAL);
    172       1.2    kardel 
    173       1.2    kardel 	spc = "";
    174       1.2    kardel 	error = 0;
    175       1.2    kardel 	needed = 0;
    176       1.2    kardel 	left = *oldlenp;
    177       1.2    kardel 
    178  1.20.2.2      matt 	mutex_enter(&time_lock);
    179       1.2    kardel 	for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
    180       1.2    kardel 		if (where == NULL) {
    181       1.2    kardel 			needed += sizeof(buf);  /* be conservative */
    182       1.2    kardel 		} else {
    183       1.2    kardel 			slen = snprintf(buf, sizeof(buf), "%s%s(q=%d, f=%" PRId64
    184       1.2    kardel 					" Hz)", spc, tc->tc_name, tc->tc_quality,
    185       1.2    kardel 					tc->tc_frequency);
    186       1.2    kardel 			if (left < slen + 1)
    187       1.2    kardel 				break;
    188       1.2    kardel 			/* XXX use sysctl_copyout? (from sysctl_hw_disknames) */
    189  1.20.2.2      matt 			/* XXX copyout with held lock. */
    190       1.2    kardel 			error = copyout(buf, where, slen + 1);
    191       1.2    kardel 			spc = " ";
    192       1.2    kardel 			where += slen;
    193       1.2    kardel 			needed += slen;
    194       1.2    kardel 			left -= slen;
    195       1.2    kardel 		}
    196       1.2    kardel 	}
    197  1.20.2.2      matt 	mutex_exit(&time_lock);
    198       1.2    kardel 
    199       1.2    kardel 	*oldlenp = needed;
    200       1.2    kardel 	return (error);
    201       1.2    kardel }
    202       1.2    kardel 
    203       1.2    kardel SYSCTL_SETUP(sysctl_timecounter_setup, "sysctl timecounter setup")
    204       1.2    kardel {
    205       1.2    kardel 	const struct sysctlnode *node;
    206       1.2    kardel 
    207       1.2    kardel 	sysctl_createv(clog, 0, NULL, &node,
    208       1.2    kardel 		       CTLFLAG_PERMANENT,
    209       1.2    kardel 		       CTLTYPE_NODE, "timecounter",
    210       1.2    kardel 		       SYSCTL_DESCR("time counter information"),
    211       1.2    kardel 		       NULL, 0, NULL, 0,
    212       1.2    kardel 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    213       1.2    kardel 
    214       1.2    kardel 	if (node != NULL) {
    215       1.2    kardel 		sysctl_createv(clog, 0, NULL, NULL,
    216       1.2    kardel 			       CTLFLAG_PERMANENT,
    217       1.2    kardel 			       CTLTYPE_STRING, "choice",
    218       1.2    kardel 			       SYSCTL_DESCR("available counters"),
    219       1.2    kardel 			       sysctl_kern_timecounter_choice, 0, NULL, 0,
    220       1.2    kardel 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    221       1.2    kardel 
    222       1.2    kardel 		sysctl_createv(clog, 0, NULL, NULL,
    223       1.2    kardel 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    224       1.2    kardel 			       CTLTYPE_STRING, "hardware",
    225       1.2    kardel 			       SYSCTL_DESCR("currently active time counter"),
    226       1.2    kardel 			       sysctl_kern_timecounter_hardware, 0, NULL, MAX_TCNAMELEN,
    227       1.2    kardel 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    228       1.2    kardel 
    229       1.2    kardel 		sysctl_createv(clog, 0, NULL, NULL,
    230       1.2    kardel 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    231       1.2    kardel 			       CTLTYPE_INT, "timestepwarnings",
    232       1.2    kardel 			       SYSCTL_DESCR("log time steps"),
    233       1.2    kardel 			       NULL, 0, &timestepwarnings, 0,
    234       1.2    kardel 			       CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    235       1.2    kardel 	}
    236       1.2    kardel }
    237       1.2    kardel 
    238  1.20.2.3      matt #ifdef TC_COUNTERS
    239       1.2    kardel #define	TC_STATS(name)							\
    240       1.2    kardel static struct evcnt n##name =						\
    241       1.2    kardel     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "timecounter", #name);	\
    242       1.2    kardel EVCNT_ATTACH_STATIC(n##name)
    243       1.2    kardel TC_STATS(binuptime);    TC_STATS(nanouptime);    TC_STATS(microuptime);
    244       1.2    kardel TC_STATS(bintime);      TC_STATS(nanotime);      TC_STATS(microtime);
    245       1.2    kardel TC_STATS(getbinuptime); TC_STATS(getnanouptime); TC_STATS(getmicrouptime);
    246       1.2    kardel TC_STATS(getbintime);   TC_STATS(getnanotime);   TC_STATS(getmicrotime);
    247       1.2    kardel TC_STATS(setclock);
    248  1.20.2.3      matt #define	TC_COUNT(var)	var.ev_count++
    249       1.1    simonb #undef TC_STATS
    250  1.20.2.3      matt #else
    251  1.20.2.3      matt #define	TC_COUNT(var)	/* nothing */
    252  1.20.2.3      matt #endif	/* TC_COUNTERS */
    253       1.1    simonb 
    254       1.1    simonb static void tc_windup(void);
    255       1.1    simonb 
    256       1.1    simonb /*
    257       1.1    simonb  * Return the difference between the timehands' counter value now and what
    258       1.1    simonb  * was when we copied it to the timehands' offset_count.
    259       1.1    simonb  */
    260       1.1    simonb static __inline u_int
    261       1.1    simonb tc_delta(struct timehands *th)
    262       1.1    simonb {
    263       1.1    simonb 	struct timecounter *tc;
    264       1.1    simonb 
    265       1.1    simonb 	tc = th->th_counter;
    266       1.2    kardel 	return ((tc->tc_get_timecount(tc) -
    267       1.2    kardel 		 th->th_offset_count) & tc->tc_counter_mask);
    268       1.1    simonb }
    269       1.1    simonb 
    270       1.1    simonb /*
    271       1.1    simonb  * Functions for reading the time.  We have to loop until we are sure that
    272       1.1    simonb  * the timehands that we operated on was not updated under our feet.  See
    273  1.20.2.1      matt  * the comment in <sys/timevar.h> for a description of these 12 functions.
    274       1.1    simonb  */
    275       1.1    simonb 
    276       1.1    simonb void
    277       1.1    simonb binuptime(struct bintime *bt)
    278       1.1    simonb {
    279       1.1    simonb 	struct timehands *th;
    280       1.1    simonb 	u_int gen;
    281       1.1    simonb 
    282  1.20.2.3      matt 	TC_COUNT(nbinuptime);
    283       1.1    simonb 	do {
    284       1.1    simonb 		th = timehands;
    285       1.1    simonb 		gen = th->th_generation;
    286       1.1    simonb 		*bt = th->th_offset;
    287       1.1    simonb 		bintime_addx(bt, th->th_scale * tc_delta(th));
    288       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    289       1.1    simonb }
    290       1.1    simonb 
    291       1.1    simonb void
    292       1.1    simonb nanouptime(struct timespec *tsp)
    293       1.1    simonb {
    294       1.1    simonb 	struct bintime bt;
    295       1.1    simonb 
    296  1.20.2.3      matt 	TC_COUNT(nnanouptime);
    297       1.1    simonb 	binuptime(&bt);
    298       1.1    simonb 	bintime2timespec(&bt, tsp);
    299       1.1    simonb }
    300       1.1    simonb 
    301       1.1    simonb void
    302       1.1    simonb microuptime(struct timeval *tvp)
    303       1.1    simonb {
    304       1.1    simonb 	struct bintime bt;
    305       1.1    simonb 
    306  1.20.2.3      matt 	TC_COUNT(nmicrouptime);
    307       1.1    simonb 	binuptime(&bt);
    308       1.1    simonb 	bintime2timeval(&bt, tvp);
    309       1.1    simonb }
    310       1.1    simonb 
    311       1.1    simonb void
    312       1.1    simonb bintime(struct bintime *bt)
    313       1.1    simonb {
    314       1.1    simonb 
    315  1.20.2.3      matt 	TC_COUNT(nbintime);
    316       1.1    simonb 	binuptime(bt);
    317       1.4    kardel 	bintime_add(bt, &timebasebin);
    318       1.1    simonb }
    319       1.1    simonb 
    320       1.1    simonb void
    321       1.1    simonb nanotime(struct timespec *tsp)
    322       1.1    simonb {
    323       1.1    simonb 	struct bintime bt;
    324       1.1    simonb 
    325  1.20.2.3      matt 	TC_COUNT(nnanotime);
    326       1.1    simonb 	bintime(&bt);
    327       1.1    simonb 	bintime2timespec(&bt, tsp);
    328       1.1    simonb }
    329       1.1    simonb 
    330       1.1    simonb void
    331       1.1    simonb microtime(struct timeval *tvp)
    332       1.1    simonb {
    333       1.1    simonb 	struct bintime bt;
    334       1.1    simonb 
    335  1.20.2.3      matt 	TC_COUNT(nmicrotime);
    336       1.1    simonb 	bintime(&bt);
    337       1.1    simonb 	bintime2timeval(&bt, tvp);
    338       1.1    simonb }
    339       1.1    simonb 
    340       1.1    simonb void
    341       1.1    simonb getbinuptime(struct bintime *bt)
    342       1.1    simonb {
    343       1.1    simonb 	struct timehands *th;
    344       1.1    simonb 	u_int gen;
    345       1.1    simonb 
    346  1.20.2.3      matt 	TC_COUNT(ngetbinuptime);
    347       1.1    simonb 	do {
    348       1.1    simonb 		th = timehands;
    349       1.1    simonb 		gen = th->th_generation;
    350       1.1    simonb 		*bt = th->th_offset;
    351       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    352       1.1    simonb }
    353       1.1    simonb 
    354       1.1    simonb void
    355       1.1    simonb getnanouptime(struct timespec *tsp)
    356       1.1    simonb {
    357       1.1    simonb 	struct timehands *th;
    358       1.1    simonb 	u_int gen;
    359       1.1    simonb 
    360  1.20.2.3      matt 	TC_COUNT(ngetnanouptime);
    361       1.1    simonb 	do {
    362       1.1    simonb 		th = timehands;
    363       1.1    simonb 		gen = th->th_generation;
    364       1.1    simonb 		bintime2timespec(&th->th_offset, tsp);
    365       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    366       1.1    simonb }
    367       1.1    simonb 
    368       1.1    simonb void
    369       1.1    simonb getmicrouptime(struct timeval *tvp)
    370       1.1    simonb {
    371       1.1    simonb 	struct timehands *th;
    372       1.1    simonb 	u_int gen;
    373       1.1    simonb 
    374  1.20.2.3      matt 	TC_COUNT(ngetmicrouptime);
    375       1.1    simonb 	do {
    376       1.1    simonb 		th = timehands;
    377       1.1    simonb 		gen = th->th_generation;
    378       1.1    simonb 		bintime2timeval(&th->th_offset, tvp);
    379       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    380       1.1    simonb }
    381       1.1    simonb 
    382       1.1    simonb void
    383       1.1    simonb getbintime(struct bintime *bt)
    384       1.1    simonb {
    385       1.1    simonb 	struct timehands *th;
    386       1.1    simonb 	u_int gen;
    387       1.1    simonb 
    388  1.20.2.3      matt 	TC_COUNT(ngetbintime);
    389       1.1    simonb 	do {
    390       1.1    simonb 		th = timehands;
    391       1.1    simonb 		gen = th->th_generation;
    392       1.1    simonb 		*bt = th->th_offset;
    393       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    394       1.4    kardel 	bintime_add(bt, &timebasebin);
    395       1.1    simonb }
    396       1.1    simonb 
    397       1.1    simonb void
    398       1.1    simonb getnanotime(struct timespec *tsp)
    399       1.1    simonb {
    400       1.1    simonb 	struct timehands *th;
    401       1.1    simonb 	u_int gen;
    402       1.1    simonb 
    403  1.20.2.3      matt 	TC_COUNT(ngetnanotime);
    404       1.1    simonb 	do {
    405       1.1    simonb 		th = timehands;
    406       1.1    simonb 		gen = th->th_generation;
    407       1.1    simonb 		*tsp = th->th_nanotime;
    408       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    409       1.1    simonb }
    410       1.1    simonb 
    411       1.1    simonb void
    412       1.1    simonb getmicrotime(struct timeval *tvp)
    413       1.1    simonb {
    414       1.1    simonb 	struct timehands *th;
    415       1.1    simonb 	u_int gen;
    416       1.1    simonb 
    417  1.20.2.3      matt 	TC_COUNT(ngetmicrotime);
    418       1.1    simonb 	do {
    419       1.1    simonb 		th = timehands;
    420       1.1    simonb 		gen = th->th_generation;
    421       1.1    simonb 		*tvp = th->th_microtime;
    422       1.1    simonb 	} while (gen == 0 || gen != th->th_generation);
    423       1.1    simonb }
    424       1.1    simonb 
    425       1.1    simonb /*
    426       1.1    simonb  * Initialize a new timecounter and possibly use it.
    427       1.1    simonb  */
    428       1.1    simonb void
    429       1.1    simonb tc_init(struct timecounter *tc)
    430       1.1    simonb {
    431       1.1    simonb 	u_int u;
    432       1.1    simonb 
    433       1.1    simonb 	u = tc->tc_frequency / tc->tc_counter_mask;
    434       1.1    simonb 	/* XXX: We need some margin here, 10% is a guess */
    435       1.1    simonb 	u *= 11;
    436       1.1    simonb 	u /= 10;
    437       1.1    simonb 	if (u > hz && tc->tc_quality >= 0) {
    438       1.1    simonb 		tc->tc_quality = -2000;
    439      1.18        ad 		aprint_verbose(
    440      1.18        ad 		    "timecounter: Timecounter \"%s\" frequency %ju Hz",
    441       1.7     bjh21 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
    442      1.18        ad 		aprint_verbose(" -- Insufficient hz, needs at least %u\n", u);
    443       1.1    simonb 	} else if (tc->tc_quality >= 0 || bootverbose) {
    444      1.18        ad 		aprint_verbose(
    445      1.18        ad 		    "timecounter: Timecounter \"%s\" frequency %ju Hz "
    446      1.18        ad 		    "quality %d\n", tc->tc_name, (uintmax_t)tc->tc_frequency,
    447       1.7     bjh21 		    tc->tc_quality);
    448       1.1    simonb 	}
    449       1.1    simonb 
    450  1.20.2.2      matt 	mutex_enter(&time_lock);
    451  1.20.2.2      matt 	mutex_spin_enter(&tc_windup_lock);
    452       1.1    simonb 	tc->tc_next = timecounters;
    453       1.1    simonb 	timecounters = tc;
    454       1.1    simonb 	/*
    455       1.1    simonb 	 * Never automatically use a timecounter with negative quality.
    456       1.1    simonb 	 * Even though we run on the dummy counter, switching here may be
    457       1.1    simonb 	 * worse since this timecounter may not be monotonous.
    458       1.1    simonb 	 */
    459  1.20.2.2      matt 	if (tc->tc_quality >= 0 && (tc->tc_quality > timecounter->tc_quality ||
    460  1.20.2.2      matt 	    (tc->tc_quality == timecounter->tc_quality &&
    461  1.20.2.2      matt 	    tc->tc_frequency > timecounter->tc_frequency))) {
    462  1.20.2.2      matt 		(void)tc->tc_get_timecount(tc);
    463  1.20.2.2      matt 		(void)tc->tc_get_timecount(tc);
    464  1.20.2.2      matt 		timecounter = tc;
    465  1.20.2.2      matt 		tc_windup();
    466  1.20.2.2      matt 	}
    467  1.20.2.2      matt 	mutex_spin_exit(&tc_windup_lock);
    468  1.20.2.2      matt 	mutex_exit(&time_lock);
    469  1.20.2.2      matt }
    470  1.20.2.2      matt 
    471  1.20.2.2      matt /*
    472  1.20.2.2      matt  * Stop using a timecounter and remove it from the timecounters list.
    473  1.20.2.2      matt  */
    474  1.20.2.2      matt int
    475  1.20.2.2      matt tc_detach(struct timecounter *target)
    476  1.20.2.2      matt {
    477  1.20.2.2      matt 	struct timecounter *best, *tc;
    478  1.20.2.2      matt 	struct timecounter **tcp = NULL;
    479  1.20.2.2      matt 	int rc = 0;
    480  1.20.2.2      matt 
    481  1.20.2.2      matt 	mutex_enter(&time_lock);
    482  1.20.2.2      matt 	for (tcp = &timecounters, tc = timecounters;
    483  1.20.2.2      matt 	     tc != NULL;
    484  1.20.2.2      matt 	     tcp = &tc->tc_next, tc = tc->tc_next) {
    485  1.20.2.2      matt 		if (tc == target)
    486  1.20.2.2      matt 			break;
    487  1.20.2.2      matt 	}
    488  1.20.2.2      matt 	if (tc == NULL) {
    489  1.20.2.2      matt 		rc = ESRCH;
    490      1.12   tsutsui 		goto out;
    491  1.20.2.2      matt 	}
    492  1.20.2.2      matt 	*tcp = tc->tc_next;
    493  1.20.2.2      matt 
    494  1.20.2.2      matt 	if (timecounter != target)
    495      1.12   tsutsui 		goto out;
    496      1.11    simonb 
    497  1.20.2.2      matt 	for (best = tc = timecounters; tc != NULL; tc = tc->tc_next) {
    498  1.20.2.2      matt 		if (tc->tc_quality > best->tc_quality)
    499  1.20.2.2      matt 			best = tc;
    500  1.20.2.2      matt 		else if (tc->tc_quality < best->tc_quality)
    501  1.20.2.2      matt 			continue;
    502  1.20.2.2      matt 		else if (tc->tc_frequency > best->tc_frequency)
    503  1.20.2.2      matt 			best = tc;
    504  1.20.2.2      matt 	}
    505  1.20.2.2      matt 	mutex_spin_enter(&tc_windup_lock);
    506  1.20.2.2      matt 	(void)best->tc_get_timecount(best);
    507  1.20.2.2      matt 	(void)best->tc_get_timecount(best);
    508  1.20.2.2      matt 	timecounter = best;
    509  1.20.2.2      matt 	tc_windup();
    510  1.20.2.2      matt 	mutex_spin_exit(&tc_windup_lock);
    511  1.20.2.2      matt out:
    512  1.20.2.2      matt 	mutex_exit(&time_lock);
    513  1.20.2.2      matt 	return rc;
    514       1.1    simonb }
    515       1.1    simonb 
    516       1.1    simonb /* Report the frequency of the current timecounter. */
    517       1.1    simonb u_int64_t
    518       1.1    simonb tc_getfrequency(void)
    519       1.1    simonb {
    520       1.1    simonb 
    521       1.1    simonb 	return (timehands->th_counter->tc_frequency);
    522       1.1    simonb }
    523       1.1    simonb 
    524       1.1    simonb /*
    525       1.1    simonb  * Step our concept of UTC.  This is done by modifying our estimate of
    526       1.1    simonb  * when we booted.
    527       1.1    simonb  */
    528       1.1    simonb void
    529       1.1    simonb tc_setclock(struct timespec *ts)
    530       1.1    simonb {
    531       1.1    simonb 	struct timespec ts2;
    532       1.1    simonb 	struct bintime bt, bt2;
    533       1.1    simonb 
    534  1.20.2.2      matt 	mutex_spin_enter(&tc_windup_lock);
    535  1.20.2.3      matt 	TC_COUNT(nsetclock);
    536       1.1    simonb 	binuptime(&bt2);
    537       1.1    simonb 	timespec2bintime(ts, &bt);
    538       1.1    simonb 	bintime_sub(&bt, &bt2);
    539       1.4    kardel 	bintime_add(&bt2, &timebasebin);
    540       1.4    kardel 	timebasebin = bt;
    541       1.1    simonb 	tc_windup();
    542  1.20.2.2      matt 	mutex_spin_exit(&tc_windup_lock);
    543  1.20.2.2      matt 
    544       1.1    simonb 	if (timestepwarnings) {
    545       1.1    simonb 		bintime2timespec(&bt2, &ts2);
    546       1.1    simonb 		log(LOG_INFO, "Time stepped from %jd.%09ld to %jd.%09ld\n",
    547       1.1    simonb 		    (intmax_t)ts2.tv_sec, ts2.tv_nsec,
    548       1.1    simonb 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
    549       1.1    simonb 	}
    550       1.1    simonb }
    551       1.1    simonb 
    552       1.1    simonb /*
    553       1.1    simonb  * Initialize the next struct timehands in the ring and make
    554       1.1    simonb  * it the active timehands.  Along the way we might switch to a different
    555       1.1    simonb  * timecounter and/or do seconds processing in NTP.  Slightly magic.
    556       1.1    simonb  */
    557       1.1    simonb static void
    558       1.1    simonb tc_windup(void)
    559       1.1    simonb {
    560       1.1    simonb 	struct bintime bt;
    561       1.1    simonb 	struct timehands *th, *tho;
    562       1.1    simonb 	u_int64_t scale;
    563       1.1    simonb 	u_int delta, ncount, ogen;
    564      1.13    kardel 	int i, s_update;
    565       1.1    simonb 	time_t t;
    566       1.1    simonb 
    567  1.20.2.2      matt 	KASSERT(mutex_owned(&tc_windup_lock));
    568  1.20.2.2      matt 
    569      1.13    kardel 	s_update = 0;
    570      1.20        ad 
    571       1.1    simonb 	/*
    572       1.1    simonb 	 * Make the next timehands a copy of the current one, but do not
    573       1.1    simonb 	 * overwrite the generation or next pointer.  While we update
    574      1.20        ad 	 * the contents, the generation must be zero.  Ensure global
    575      1.20        ad 	 * visibility of the generation before proceeding.
    576       1.1    simonb 	 */
    577       1.1    simonb 	tho = timehands;
    578       1.1    simonb 	th = tho->th_next;
    579       1.1    simonb 	ogen = th->th_generation;
    580       1.1    simonb 	th->th_generation = 0;
    581  1.20.2.2      matt 	membar_producer();
    582       1.1    simonb 	bcopy(tho, th, offsetof(struct timehands, th_generation));
    583       1.1    simonb 
    584       1.1    simonb 	/*
    585       1.1    simonb 	 * Capture a timecounter delta on the current timecounter and if
    586       1.1    simonb 	 * changing timecounters, a counter value from the new timecounter.
    587       1.1    simonb 	 * Update the offset fields accordingly.
    588       1.1    simonb 	 */
    589       1.1    simonb 	delta = tc_delta(th);
    590       1.1    simonb 	if (th->th_counter != timecounter)
    591       1.1    simonb 		ncount = timecounter->tc_get_timecount(timecounter);
    592       1.1    simonb 	else
    593       1.1    simonb 		ncount = 0;
    594       1.1    simonb 	th->th_offset_count += delta;
    595       1.1    simonb 	th->th_offset_count &= th->th_counter->tc_counter_mask;
    596       1.1    simonb 	bintime_addx(&th->th_offset, th->th_scale * delta);
    597       1.1    simonb 
    598       1.1    simonb 	/*
    599       1.1    simonb 	 * Hardware latching timecounters may not generate interrupts on
    600       1.1    simonb 	 * PPS events, so instead we poll them.  There is a finite risk that
    601       1.1    simonb 	 * the hardware might capture a count which is later than the one we
    602       1.1    simonb 	 * got above, and therefore possibly in the next NTP second which might
    603       1.1    simonb 	 * have a different rate than the current NTP second.  It doesn't
    604       1.1    simonb 	 * matter in practice.
    605       1.1    simonb 	 */
    606       1.1    simonb 	if (tho->th_counter->tc_poll_pps)
    607       1.1    simonb 		tho->th_counter->tc_poll_pps(tho->th_counter);
    608       1.1    simonb 
    609       1.1    simonb 	/*
    610       1.1    simonb 	 * Deal with NTP second processing.  The for loop normally
    611       1.1    simonb 	 * iterates at most once, but in extreme situations it might
    612       1.1    simonb 	 * keep NTP sane if timeouts are not run for several seconds.
    613       1.1    simonb 	 * At boot, the time step can be large when the TOD hardware
    614       1.1    simonb 	 * has been read, so on really large steps, we call
    615       1.1    simonb 	 * ntp_update_second only twice.  We need to call it twice in
    616       1.1    simonb 	 * case we missed a leap second.
    617       1.2    kardel 	 * If NTP is not compiled in ntp_update_second still calculates
    618       1.2    kardel 	 * the adjustment resulting from adjtime() calls.
    619       1.1    simonb 	 */
    620       1.1    simonb 	bt = th->th_offset;
    621       1.4    kardel 	bintime_add(&bt, &timebasebin);
    622       1.1    simonb 	i = bt.sec - tho->th_microtime.tv_sec;
    623       1.1    simonb 	if (i > LARGE_STEP)
    624       1.1    simonb 		i = 2;
    625       1.1    simonb 	for (; i > 0; i--) {
    626       1.1    simonb 		t = bt.sec;
    627       1.1    simonb 		ntp_update_second(&th->th_adjustment, &bt.sec);
    628      1.13    kardel 		s_update = 1;
    629       1.1    simonb 		if (bt.sec != t)
    630       1.4    kardel 			timebasebin.sec += bt.sec - t;
    631       1.1    simonb 	}
    632       1.2    kardel 
    633       1.1    simonb 	/* Update the UTC timestamps used by the get*() functions. */
    634       1.1    simonb 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
    635       1.1    simonb 	bintime2timeval(&bt, &th->th_microtime);
    636       1.1    simonb 	bintime2timespec(&bt, &th->th_nanotime);
    637       1.1    simonb 
    638       1.1    simonb 	/* Now is a good time to change timecounters. */
    639       1.1    simonb 	if (th->th_counter != timecounter) {
    640       1.1    simonb 		th->th_counter = timecounter;
    641       1.1    simonb 		th->th_offset_count = ncount;
    642      1.13    kardel 		s_update = 1;
    643       1.1    simonb 	}
    644       1.1    simonb 
    645       1.1    simonb 	/*-
    646       1.1    simonb 	 * Recalculate the scaling factor.  We want the number of 1/2^64
    647       1.1    simonb 	 * fractions of a second per period of the hardware counter, taking
    648       1.1    simonb 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
    649       1.1    simonb 	 * processing provides us with.
    650       1.1    simonb 	 *
    651       1.1    simonb 	 * The th_adjustment is nanoseconds per second with 32 bit binary
    652       1.1    simonb 	 * fraction and we want 64 bit binary fraction of second:
    653       1.1    simonb 	 *
    654       1.1    simonb 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
    655       1.1    simonb 	 *
    656       1.1    simonb 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
    657       1.1    simonb 	 * we can only multiply by about 850 without overflowing, but that
    658       1.1    simonb 	 * leaves suitably precise fractions for multiply before divide.
    659       1.1    simonb 	 *
    660       1.1    simonb 	 * Divide before multiply with a fraction of 2199/512 results in a
    661       1.1    simonb 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
    662       1.1    simonb 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
    663       1.1    simonb  	 *
    664       1.1    simonb 	 * We happily sacrifice the lowest of the 64 bits of our result
    665       1.1    simonb 	 * to the goddess of code clarity.
    666       1.1    simonb 	 *
    667       1.1    simonb 	 */
    668      1.13    kardel 	if (s_update) {
    669      1.13    kardel 		scale = (u_int64_t)1 << 63;
    670      1.13    kardel 		scale += (th->th_adjustment / 1024) * 2199;
    671      1.13    kardel 		scale /= th->th_counter->tc_frequency;
    672      1.13    kardel 		th->th_scale = scale * 2;
    673      1.13    kardel 	}
    674       1.1    simonb 	/*
    675       1.1    simonb 	 * Now that the struct timehands is again consistent, set the new
    676      1.20        ad 	 * generation number, making sure to not make it zero.  Ensure
    677      1.20        ad 	 * changes are globally visible before changing.
    678       1.1    simonb 	 */
    679       1.1    simonb 	if (++ogen == 0)
    680       1.1    simonb 		ogen = 1;
    681  1.20.2.2      matt 	membar_producer();
    682       1.1    simonb 	th->th_generation = ogen;
    683       1.1    simonb 
    684      1.20        ad 	/*
    685      1.20        ad 	 * Go live with the new struct timehands.  Ensure changes are
    686      1.20        ad 	 * globally visible before changing.
    687      1.20        ad 	 */
    688       1.1    simonb 	time_second = th->th_microtime.tv_sec;
    689       1.1    simonb 	time_uptime = th->th_offset.sec;
    690  1.20.2.2      matt 	membar_producer();
    691       1.1    simonb 	timehands = th;
    692       1.1    simonb 
    693  1.20.2.2      matt 	/*
    694  1.20.2.2      matt 	 * Force users of the old timehand to move on.  This is
    695  1.20.2.2      matt 	 * necessary for MP systems; we need to ensure that the
    696  1.20.2.2      matt 	 * consumers will move away from the old timehand before
    697  1.20.2.2      matt 	 * we begin updating it again when we eventually wrap
    698  1.20.2.2      matt 	 * around.
    699  1.20.2.2      matt 	 */
    700  1.20.2.2      matt 	if (++tho->th_generation == 0)
    701  1.20.2.2      matt 		tho->th_generation = 1;
    702       1.1    simonb }
    703       1.1    simonb 
    704       1.1    simonb /*
    705       1.1    simonb  * RFC 2783 PPS-API implementation.
    706       1.1    simonb  */
    707       1.1    simonb 
    708       1.1    simonb int
    709      1.19  christos pps_ioctl(u_long cmd, void *data, struct pps_state *pps)
    710       1.1    simonb {
    711       1.1    simonb 	pps_params_t *app;
    712       1.2    kardel 	pps_info_t *pipi;
    713       1.1    simonb #ifdef PPS_SYNC
    714       1.2    kardel 	int *epi;
    715       1.1    simonb #endif
    716       1.1    simonb 
    717       1.2    kardel 	KASSERT(pps != NULL); /* XXX ("NULL pps pointer in pps_ioctl") */
    718       1.1    simonb 	switch (cmd) {
    719       1.1    simonb 	case PPS_IOC_CREATE:
    720       1.1    simonb 		return (0);
    721       1.1    simonb 	case PPS_IOC_DESTROY:
    722       1.1    simonb 		return (0);
    723       1.1    simonb 	case PPS_IOC_SETPARAMS:
    724       1.1    simonb 		app = (pps_params_t *)data;
    725       1.1    simonb 		if (app->mode & ~pps->ppscap)
    726       1.1    simonb 			return (EINVAL);
    727       1.1    simonb 		pps->ppsparam = *app;
    728       1.1    simonb 		return (0);
    729       1.1    simonb 	case PPS_IOC_GETPARAMS:
    730       1.1    simonb 		app = (pps_params_t *)data;
    731       1.1    simonb 		*app = pps->ppsparam;
    732       1.1    simonb 		app->api_version = PPS_API_VERS_1;
    733       1.1    simonb 		return (0);
    734       1.1    simonb 	case PPS_IOC_GETCAP:
    735       1.1    simonb 		*(int*)data = pps->ppscap;
    736       1.1    simonb 		return (0);
    737       1.1    simonb 	case PPS_IOC_FETCH:
    738       1.2    kardel 		pipi = (pps_info_t *)data;
    739       1.1    simonb 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
    740       1.2    kardel 		*pipi = pps->ppsinfo;
    741       1.1    simonb 		return (0);
    742       1.1    simonb 	case PPS_IOC_KCBIND:
    743       1.1    simonb #ifdef PPS_SYNC
    744       1.2    kardel 		epi = (int *)data;
    745       1.1    simonb 		/* XXX Only root should be able to do this */
    746       1.2    kardel 		if (*epi & ~pps->ppscap)
    747       1.1    simonb 			return (EINVAL);
    748       1.2    kardel 		pps->kcmode = *epi;
    749       1.1    simonb 		return (0);
    750       1.1    simonb #else
    751       1.1    simonb 		return (EOPNOTSUPP);
    752       1.1    simonb #endif
    753       1.1    simonb 	default:
    754       1.2    kardel 		return (EPASSTHROUGH);
    755       1.1    simonb 	}
    756       1.1    simonb }
    757       1.1    simonb 
    758       1.1    simonb void
    759       1.1    simonb pps_init(struct pps_state *pps)
    760       1.1    simonb {
    761       1.1    simonb 	pps->ppscap |= PPS_TSFMT_TSPEC;
    762       1.1    simonb 	if (pps->ppscap & PPS_CAPTUREASSERT)
    763       1.1    simonb 		pps->ppscap |= PPS_OFFSETASSERT;
    764       1.1    simonb 	if (pps->ppscap & PPS_CAPTURECLEAR)
    765       1.1    simonb 		pps->ppscap |= PPS_OFFSETCLEAR;
    766       1.1    simonb }
    767       1.1    simonb 
    768       1.1    simonb void
    769       1.1    simonb pps_capture(struct pps_state *pps)
    770       1.1    simonb {
    771       1.1    simonb 	struct timehands *th;
    772       1.1    simonb 
    773       1.2    kardel 	KASSERT(pps != NULL); /* XXX ("NULL pps pointer in pps_capture") */
    774       1.1    simonb 	th = timehands;
    775       1.1    simonb 	pps->capgen = th->th_generation;
    776       1.1    simonb 	pps->capth = th;
    777       1.1    simonb 	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
    778       1.1    simonb 	if (pps->capgen != th->th_generation)
    779       1.1    simonb 		pps->capgen = 0;
    780       1.1    simonb }
    781       1.1    simonb 
    782       1.1    simonb void
    783       1.1    simonb pps_event(struct pps_state *pps, int event)
    784       1.1    simonb {
    785       1.1    simonb 	struct bintime bt;
    786       1.1    simonb 	struct timespec ts, *tsp, *osp;
    787       1.1    simonb 	u_int tcount, *pcount;
    788       1.1    simonb 	int foff, fhard;
    789       1.1    simonb 	pps_seq_t *pseq;
    790       1.1    simonb 
    791       1.2    kardel 	KASSERT(pps != NULL); /* XXX ("NULL pps pointer in pps_event") */
    792       1.1    simonb 	/* If the timecounter was wound up underneath us, bail out. */
    793       1.1    simonb 	if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
    794       1.1    simonb 		return;
    795       1.1    simonb 
    796       1.1    simonb 	/* Things would be easier with arrays. */
    797       1.1    simonb 	if (event == PPS_CAPTUREASSERT) {
    798       1.1    simonb 		tsp = &pps->ppsinfo.assert_timestamp;
    799       1.1    simonb 		osp = &pps->ppsparam.assert_offset;
    800       1.1    simonb 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
    801       1.1    simonb 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
    802       1.1    simonb 		pcount = &pps->ppscount[0];
    803       1.1    simonb 		pseq = &pps->ppsinfo.assert_sequence;
    804       1.1    simonb 	} else {
    805       1.1    simonb 		tsp = &pps->ppsinfo.clear_timestamp;
    806       1.1    simonb 		osp = &pps->ppsparam.clear_offset;
    807       1.1    simonb 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
    808       1.1    simonb 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
    809       1.1    simonb 		pcount = &pps->ppscount[1];
    810       1.1    simonb 		pseq = &pps->ppsinfo.clear_sequence;
    811       1.1    simonb 	}
    812       1.1    simonb 
    813       1.1    simonb 	/*
    814       1.1    simonb 	 * If the timecounter changed, we cannot compare the count values, so
    815       1.1    simonb 	 * we have to drop the rest of the PPS-stuff until the next event.
    816       1.1    simonb 	 */
    817       1.1    simonb 	if (pps->ppstc != pps->capth->th_counter) {
    818       1.1    simonb 		pps->ppstc = pps->capth->th_counter;
    819       1.1    simonb 		*pcount = pps->capcount;
    820       1.1    simonb 		pps->ppscount[2] = pps->capcount;
    821       1.1    simonb 		return;
    822       1.1    simonb 	}
    823       1.1    simonb 
    824       1.1    simonb 	/* Convert the count to a timespec. */
    825       1.1    simonb 	tcount = pps->capcount - pps->capth->th_offset_count;
    826       1.1    simonb 	tcount &= pps->capth->th_counter->tc_counter_mask;
    827       1.1    simonb 	bt = pps->capth->th_offset;
    828       1.1    simonb 	bintime_addx(&bt, pps->capth->th_scale * tcount);
    829       1.4    kardel 	bintime_add(&bt, &timebasebin);
    830       1.1    simonb 	bintime2timespec(&bt, &ts);
    831       1.1    simonb 
    832       1.1    simonb 	/* If the timecounter was wound up underneath us, bail out. */
    833       1.1    simonb 	if (pps->capgen != pps->capth->th_generation)
    834       1.1    simonb 		return;
    835       1.1    simonb 
    836       1.1    simonb 	*pcount = pps->capcount;
    837       1.1    simonb 	(*pseq)++;
    838       1.1    simonb 	*tsp = ts;
    839       1.1    simonb 
    840       1.1    simonb 	if (foff) {
    841       1.2    kardel 		timespecadd(tsp, osp, tsp);
    842       1.1    simonb 		if (tsp->tv_nsec < 0) {
    843       1.1    simonb 			tsp->tv_nsec += 1000000000;
    844       1.1    simonb 			tsp->tv_sec -= 1;
    845       1.1    simonb 		}
    846       1.1    simonb 	}
    847       1.1    simonb #ifdef PPS_SYNC
    848       1.1    simonb 	if (fhard) {
    849       1.1    simonb 		u_int64_t scale;
    850       1.1    simonb 
    851       1.1    simonb 		/*
    852       1.1    simonb 		 * Feed the NTP PLL/FLL.
    853       1.1    simonb 		 * The FLL wants to know how many (hardware) nanoseconds
    854       1.1    simonb 		 * elapsed since the previous event.
    855       1.1    simonb 		 */
    856       1.1    simonb 		tcount = pps->capcount - pps->ppscount[2];
    857       1.1    simonb 		pps->ppscount[2] = pps->capcount;
    858       1.1    simonb 		tcount &= pps->capth->th_counter->tc_counter_mask;
    859       1.1    simonb 		scale = (u_int64_t)1 << 63;
    860       1.1    simonb 		scale /= pps->capth->th_counter->tc_frequency;
    861       1.1    simonb 		scale *= 2;
    862       1.1    simonb 		bt.sec = 0;
    863       1.1    simonb 		bt.frac = 0;
    864       1.1    simonb 		bintime_addx(&bt, scale * tcount);
    865       1.1    simonb 		bintime2timespec(&bt, &ts);
    866       1.1    simonb 		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
    867       1.1    simonb 	}
    868       1.1    simonb #endif
    869       1.1    simonb }
    870       1.1    simonb 
    871       1.1    simonb /*
    872       1.1    simonb  * Timecounters need to be updated every so often to prevent the hardware
    873       1.1    simonb  * counter from overflowing.  Updating also recalculates the cached values
    874       1.1    simonb  * used by the get*() family of functions, so their precision depends on
    875       1.1    simonb  * the update frequency.
    876       1.1    simonb  */
    877       1.1    simonb 
    878       1.1    simonb static int tc_tick;
    879       1.1    simonb 
    880       1.1    simonb void
    881       1.1    simonb tc_ticktock(void)
    882       1.1    simonb {
    883       1.1    simonb 	static int count;
    884       1.1    simonb 
    885       1.1    simonb 	if (++count < tc_tick)
    886       1.1    simonb 		return;
    887       1.1    simonb 	count = 0;
    888  1.20.2.2      matt 	mutex_spin_enter(&tc_windup_lock);
    889       1.1    simonb 	tc_windup();
    890  1.20.2.2      matt 	mutex_spin_exit(&tc_windup_lock);
    891       1.1    simonb }
    892       1.1    simonb 
    893       1.2    kardel void
    894       1.2    kardel inittimecounter(void)
    895       1.1    simonb {
    896       1.1    simonb 	u_int p;
    897       1.1    simonb 
    898  1.20.2.2      matt 	mutex_init(&tc_windup_lock, MUTEX_DEFAULT, IPL_SCHED);
    899  1.20.2.2      matt 
    900       1.1    simonb 	/*
    901       1.1    simonb 	 * Set the initial timeout to
    902       1.1    simonb 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
    903       1.1    simonb 	 * People should probably not use the sysctl to set the timeout
    904       1.1    simonb 	 * to smaller than its inital value, since that value is the
    905       1.1    simonb 	 * smallest reasonable one.  If they want better timestamps they
    906       1.1    simonb 	 * should use the non-"get"* functions.
    907       1.1    simonb 	 */
    908       1.1    simonb 	if (hz > 1000)
    909       1.1    simonb 		tc_tick = (hz + 500) / 1000;
    910       1.1    simonb 	else
    911       1.1    simonb 		tc_tick = 1;
    912       1.1    simonb 	p = (tc_tick * 1000000) / hz;
    913      1.18        ad 	aprint_verbose("timecounter: Timecounters tick every %d.%03u msec\n",
    914      1.18        ad 	    p / 1000, p % 1000);
    915       1.1    simonb 
    916       1.1    simonb 	/* warm up new timecounter (again) and get rolling. */
    917       1.1    simonb 	(void)timecounter->tc_get_timecount(timecounter);
    918       1.1    simonb 	(void)timecounter->tc_get_timecount(timecounter);
    919       1.1    simonb }
    920