2 * Copyright (c) 1997, 1998 Poul-Henning Kamp <phk@FreeBSD.org>
3 * Copyright (c) 1982, 1986, 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 * (c) UNIX System Laboratories, Inc.
6 * All or some portions of this file are derived from material licensed
7 * to the University of California by American Telephone and Telegraph
8 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9 * the permission of UNIX System Laboratories, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
40 * $FreeBSD: src/sys/kern/kern_clock.c,v 1.105.2.10 2002/10/17 13:19:40 maxim Exp $
41 * $DragonFly: src/sys/kern/kern_clock.c,v 1.12 2003/10/17 07:30:42 dillon Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/dkstat.h>
49 #include <sys/callout.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
55 #include <sys/timex.h>
56 #include <sys/timepps.h>
60 #include <vm/vm_map.h>
61 #include <sys/sysctl.h>
63 #include <machine/cpu.h>
64 #include <machine/limits.h>
65 #include <machine/smp.h>
72 extern void init_device_poll(void);
73 extern void hardclock_device_poll(void);
74 #endif /* DEVICE_POLLING */
77 * Number of timecounters used to implement stable storage
80 #define NTIMECOUNTER 5
83 static MALLOC_DEFINE(M_TIMECOUNTER, "timecounter",
84 "Timecounter stable storage");
86 static void initclocks (void *dummy);
87 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
89 static void tco_forward (int force);
90 static void tco_setscales (struct timecounter *tc);
91 static __inline unsigned tco_delta (struct timecounter *tc);
94 * Some of these don't belong here, but it's easiest to concentrate them.
95 * Note that cp_time[] counts in microseconds, but most userland programs
96 * just compare relative times against the total by delta.
98 long cp_time[CPUSTATES];
100 SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
101 "LU", "CPU time statistics");
110 struct timeval boottime;
111 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
112 &boottime, timeval, "System boottime");
115 * Which update policy to use.
116 * 0 - every tick, bad hardware may fail with "calcru negative..."
117 * 1 - more resistent to the above hardware, but less efficient.
119 static int tco_method;
122 * Implement a dummy timecounter which we can use until we get a real one
123 * in the air. This allows the console and other early stuff to use
128 dummy_get_timecount(struct timecounter *tc)
134 static struct timecounter dummy_timecounter = {
142 struct timecounter *timecounter = &dummy_timecounter;
145 * Clock handling routines.
147 * This code is written to operate with two timers that run independently of
150 * The main timer, running hz times per second, is used to trigger interval
151 * timers, timeouts and rescheduling as needed.
153 * The second timer handles kernel and user profiling,
154 * and does resource use estimation. If the second timer is programmable,
155 * it is randomized to avoid aliasing between the two clocks. For example,
156 * the randomization prevents an adversary from always giving up the cpu
157 * just before its quantum expires. Otherwise, it would never accumulate
158 * cpu ticks. The mean frequency of the second timer is stathz.
160 * If no second timer exists, stathz will be zero; in this case we drive
161 * profiling and statistics off the main clock. This WILL NOT be accurate;
162 * do not do it unless absolutely necessary.
164 * The statistics clock may (or may not) be run at a higher rate while
165 * profiling. This profile clock runs at profhz. We require that profhz
166 * be an integral multiple of stathz.
168 * If the statistics clock is running fast, it must be divided by the ratio
169 * profhz/stathz for statistics. (For profiling, every tick counts.)
171 * Time-of-day is maintained using a "timecounter", which may or may
172 * not be related to the hardware generating the above mentioned
178 static int profprocs;
180 static int psticks; /* profiler ticks */
181 static int psdiv; /* prof / stat divider */
182 int psratio; /* ratio: prof * 100 / stat */
185 * Initialize clock frequencies and start both clocks running.
195 * Set divisors to 1 (normal case) and let the machine-specific
201 #ifdef DEVICE_POLLING
206 * Compute profhz/stathz, and fix profhz if needed.
208 i = stathz ? stathz : hz;
211 psratio = profhz / i;
215 * The real-time timer, interrupting hz times per second. This is implemented
216 * as a FAST interrupt so it is in the context of the thread it interrupted,
217 * and not in an interrupt thread. YYY needs help.
221 struct clockframe *frame;
227 struct pstats *pstats;
230 * Run current process's virtual and profile time, as needed.
233 if (CLKF_USERMODE(frame) &&
234 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
235 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
236 psignal(p, SIGVTALRM);
237 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
238 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
242 #if 0 /* SMP and BETTER_CLOCK */
243 forward_hardclock(pscnt);
247 * If no separate statistics clock is available, run it from here.
255 #ifdef DEVICE_POLLING
256 hardclock_device_poll(); /* this is very short and quick */
257 #endif /* DEVICE_POLLING */
260 * Process callouts at a very low cpu priority, so we don't keep the
261 * relatively high clock interrupt priority any longer than necessary.
263 if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
265 } else if (softticks + 1 == ticks) {
271 * Compute number of ticks in the specified amount of time.
281 * If the number of usecs in the whole seconds part of the time
282 * difference fits in a long, then the total number of usecs will
283 * fit in an unsigned long. Compute the total and convert it to
284 * ticks, rounding up and adding 1 to allow for the current tick
285 * to expire. Rounding also depends on unsigned long arithmetic
288 * Otherwise, if the number of ticks in the whole seconds part of
289 * the time difference fits in a long, then convert the parts to
290 * ticks separately and add, using similar rounding methods and
291 * overflow avoidance. This method would work in the previous
292 * case but it is slightly slower and assumes that hz is integral.
294 * Otherwise, round the time difference down to the maximum
295 * representable value.
297 * If ints have 32 bits, then the maximum value for any timeout in
298 * 10ms ticks is 248 days.
312 printf("tvotohz: negative time difference %ld sec %ld usec\n",
316 } else if (sec <= LONG_MAX / 1000000)
317 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
319 else if (sec <= LONG_MAX / hz)
321 + ((unsigned long)usec + (tick - 1)) / tick + 1;
330 * Start profiling on a process.
332 * Kernel profiling passes proc0 which never exits and hence
333 * keeps the profile clock running constantly.
341 if ((p->p_flag & P_PROFIL) == 0) {
342 p->p_flag |= P_PROFIL;
343 if (++profprocs == 1 && stathz != 0) {
346 setstatclockrate(profhz);
353 * Stop profiling on a process.
361 if (p->p_flag & P_PROFIL) {
362 p->p_flag &= ~P_PROFIL;
363 if (--profprocs == 0 && stathz != 0) {
366 setstatclockrate(stathz);
373 * Statistics clock. Grab profile sample, and if divider reaches 0,
374 * do process and kernel statistics. Most of the statistics are only
375 * used by user-level statistics programs. The main exceptions are
376 * p->p_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
378 * The statclock should be called from an exclusive, fast interrupt,
379 * so the context should be the thread/process that got interrupted and
380 * not an interrupt thread.
384 struct clockframe *frame;
391 struct pstats *pstats;
401 * How big was our timeslice relative to the last time
404 stv = &mycpu->gd_stattv;
405 if (stv->tv_sec == 0) {
408 bump = tv.tv_usec - stv->tv_usec +
409 (tv.tv_sec - stv->tv_sec) * 1000000;
420 if (CLKF_USERMODE(frame)) {
422 * Came from userland, handle user time and deal with
425 if (p && (p->p_flag & P_PROFIL))
426 addupc_intr(p, CLKF_PC(frame), 1);
427 #if 0 /* SMP and BETTER_CLOCK */
429 forward_statclock(pscnt);
431 td->td_uticks += bump;
434 * Charge the time as appropriate
436 if (p && p->p_nice > NZERO)
437 cp_time[CP_NICE] += bump;
439 cp_time[CP_USER] += bump;
443 * Kernel statistics are just like addupc_intr, only easier.
446 if (g->state == GMON_PROF_ON) {
447 i = CLKF_PC(frame) - g->lowpc;
448 if (i < g->textsize) {
449 i /= HISTFRACTION * sizeof(*g->kcount);
454 #if 0 /* SMP and BETTER_CLOCK */
456 forward_statclock(pscnt);
459 * Came from kernel mode, so we were:
460 * - handling an interrupt,
461 * - doing syscall or trap work on behalf of the current
463 * - spinning in the idle loop.
464 * Whichever it is, charge the time as appropriate.
465 * Note that we charge interrupts to the current process,
466 * regardless of whether they are ``for'' that process,
467 * so that we know how much of its real time was spent
468 * in ``non-process'' (i.e., interrupt) work.
470 if (CLKF_INTR(frame))
471 td->td_iticks += bump;
473 td->td_sticks += bump;
475 if (CLKF_INTR(frame)) {
476 cp_time[CP_INTR] += bump;
478 if (td == &mycpu->gd_idlethread)
479 cp_time[CP_IDLE] += bump;
481 cp_time[CP_SYS] += bump;
486 * bump psticks and check against gd_psticks. When we hit the
487 * 1*hz mark (psdiv ticks) we do the more expensive stuff. If
488 * psdiv changes we reset everything to avoid confusion.
491 if (psticks < mycpu->gd_psticks && psdiv == mycpu->gd_psdiv)
494 mycpu->gd_psdiv = psdiv;
495 mycpu->gd_psticks = psticks + psdiv;
498 * XXX YYY DragonFly... need to rewrite all of this,
499 * only schedclock is distributed at the moment
503 if (smp_started && invltlb_ok && !cold && !panicstr) /* YYY */
504 lwkt_send_ipiq_mask(mycpu->gd_other_cpus, schedclock, NULL);
508 /* Update resource usage integrals and maximums. */
509 if ((pstats = p->p_stats) != NULL &&
510 (ru = &pstats->p_ru) != NULL &&
511 (vm = p->p_vmspace) != NULL) {
512 ru->ru_ixrss += pgtok(vm->vm_tsize);
513 ru->ru_idrss += pgtok(vm->vm_dsize);
514 ru->ru_isrss += pgtok(vm->vm_ssize);
515 rss = pgtok(vmspace_resident_count(vm));
516 if (ru->ru_maxrss < rss)
523 * Return information about system clocks.
526 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
528 struct clockinfo clkinfo;
530 * Construct clockinfo structure.
534 clkinfo.tickadj = tickadj;
535 clkinfo.profhz = profhz;
536 clkinfo.stathz = stathz ? stathz : hz;
537 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
540 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
541 0, 0, sysctl_kern_clockrate, "S,clockinfo","");
543 static __inline unsigned
544 tco_delta(struct timecounter *tc)
547 return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) &
548 tc->tc_counter_mask);
552 * We have eight functions for looking at the clock, four for
553 * microseconds and four for nanoseconds. For each there is fast
554 * but less precise version "get{nano|micro}[up]time" which will
555 * return a time which is up to 1/HZ previous to the call, whereas
556 * the raw version "{nano|micro}[up]time" will return a timestamp
557 * which is as precise as possible. The "up" variants return the
558 * time relative to system boot, these are well suited for time
559 * interval measurements.
563 getmicrotime(struct timeval *tvp)
565 struct timecounter *tc;
569 *tvp = tc->tc_microtime;
576 getnanotime(struct timespec *tsp)
578 struct timecounter *tc;
582 *tsp = tc->tc_nanotime;
589 microtime(struct timeval *tv)
591 struct timecounter *tc;
594 tv->tv_sec = tc->tc_offset_sec;
595 tv->tv_usec = tc->tc_offset_micro;
596 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
597 tv->tv_usec += boottime.tv_usec;
598 tv->tv_sec += boottime.tv_sec;
599 while (tv->tv_usec < 0) {
600 tv->tv_usec += 1000000;
604 while (tv->tv_usec >= 1000000) {
605 tv->tv_usec -= 1000000;
611 nanotime(struct timespec *ts)
615 struct timecounter *tc;
618 ts->tv_sec = tc->tc_offset_sec;
619 count = tco_delta(tc);
620 delta = tc->tc_offset_nano;
621 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
623 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
624 delta += boottime.tv_usec * 1000;
625 ts->tv_sec += boottime.tv_sec;
631 while (delta >= 1000000000) {
639 getmicrouptime(struct timeval *tvp)
641 struct timecounter *tc;
645 tvp->tv_sec = tc->tc_offset_sec;
646 tvp->tv_usec = tc->tc_offset_micro;
653 getnanouptime(struct timespec *tsp)
655 struct timecounter *tc;
659 tsp->tv_sec = tc->tc_offset_sec;
660 tsp->tv_nsec = tc->tc_offset_nano >> 32;
667 microuptime(struct timeval *tv)
669 struct timecounter *tc;
672 tv->tv_sec = tc->tc_offset_sec;
673 tv->tv_usec = tc->tc_offset_micro;
674 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
675 while (tv->tv_usec < 0) {
676 tv->tv_usec += 1000000;
680 while (tv->tv_usec >= 1000000) {
681 tv->tv_usec -= 1000000;
687 nanouptime(struct timespec *ts)
691 struct timecounter *tc;
694 ts->tv_sec = tc->tc_offset_sec;
695 count = tco_delta(tc);
696 delta = tc->tc_offset_nano;
697 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
699 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
705 while (delta >= 1000000000) {
713 tco_setscales(struct timecounter *tc)
717 scale = 1000000000LL << 32;
718 scale += tc->tc_adjustment;
719 scale /= tc->tc_tweak->tc_frequency;
720 tc->tc_scale_micro = scale / 1000;
721 tc->tc_scale_nano_f = scale & 0xffffffff;
722 tc->tc_scale_nano_i = scale >> 32;
726 update_timecounter(struct timecounter *tc)
732 init_timecounter(struct timecounter *tc)
735 struct timecounter *t1, *t2, *t3;
739 u = tc->tc_frequency / tc->tc_counter_mask;
741 printf("Timecounter \"%s\" frequency %lu Hz"
742 " -- Insufficient hz, needs at least %u\n",
743 tc->tc_name, (u_long) tc->tc_frequency, u);
747 tc->tc_adjustment = 0;
750 tc->tc_offset_count = tc->tc_get_timecount(tc);
751 if (timecounter == &dummy_timecounter)
754 tc->tc_avail = timecounter->tc_tweak->tc_avail;
755 timecounter->tc_tweak->tc_avail = tc;
757 MALLOC(t1, struct timecounter *, sizeof *t1, M_TIMECOUNTER, M_WAITOK);
761 for (i = 1; i < NTIMECOUNTER; i++) {
762 MALLOC(t3, struct timecounter *, sizeof *t3,
763 M_TIMECOUNTER, M_WAITOK);
771 printf("Timecounter \"%s\" frequency %lu Hz\n",
772 tc->tc_name, (u_long)tc->tc_frequency);
774 /* XXX: For now always start using the counter. */
775 tc->tc_offset_count = tc->tc_get_timecount(tc);
777 tc->tc_offset_nano = (u_int64_t)ts1.tv_nsec << 32;
778 tc->tc_offset_micro = ts1.tv_nsec / 1000;
779 tc->tc_offset_sec = ts1.tv_sec;
784 set_timecounter(struct timespec *ts)
789 boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
790 boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
791 if (boottime.tv_usec < 0) {
792 boottime.tv_usec += 1000000;
795 /* fiddle all the little crinkly bits around the fiords... */
800 switch_timecounter(struct timecounter *newtc)
803 struct timecounter *tc;
808 if (newtc->tc_tweak == tc->tc_tweak) {
812 newtc = newtc->tc_tweak->tc_other;
814 newtc->tc_offset_sec = ts.tv_sec;
815 newtc->tc_offset_nano = (u_int64_t)ts.tv_nsec << 32;
816 newtc->tc_offset_micro = ts.tv_nsec / 1000;
817 newtc->tc_offset_count = newtc->tc_get_timecount(newtc);
818 tco_setscales(newtc);
823 static struct timecounter *
824 sync_other_counter(void)
826 struct timecounter *tc, *tcn, *tco;
834 delta = tco_delta(tc);
835 tc->tc_offset_count += delta;
836 tc->tc_offset_count &= tc->tc_counter_mask;
837 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_f;
838 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_i << 32;
843 tco_forward(int force)
845 struct timecounter *tc, *tco;
849 tc = sync_other_counter();
851 * We may be inducing a tiny error here, the tc_poll_pps() may
852 * process a latched count which happens after the tco_delta()
853 * in sync_other_counter(), which would extend the previous
854 * counters parameters into the domain of this new one.
855 * Since the timewindow is very small for this, the error is
856 * going to be only a few weenieseconds (as Dave Mills would
857 * say), so lets just not talk more about it, OK ?
859 if (tco->tc_poll_pps)
860 tco->tc_poll_pps(tco);
861 if (timedelta != 0) {
863 tvt.tv_usec += tickdelta;
864 if (tvt.tv_usec >= 1000000) {
866 tvt.tv_usec -= 1000000;
867 } else if (tvt.tv_usec < 0) {
869 tvt.tv_usec += 1000000;
872 timedelta -= tickdelta;
875 while (tc->tc_offset_nano >= 1000000000ULL << 32) {
876 tc->tc_offset_nano -= 1000000000ULL << 32;
878 ntp_update_second(tc); /* XXX only needed if xntpd runs */
883 if (tco_method && !force)
886 tc->tc_offset_micro = (tc->tc_offset_nano / 1000) >> 32;
888 /* Figure out the wall-clock time */
889 tc->tc_nanotime.tv_sec = tc->tc_offset_sec + boottime.tv_sec;
890 tc->tc_nanotime.tv_nsec =
891 (tc->tc_offset_nano >> 32) + boottime.tv_usec * 1000;
892 tc->tc_microtime.tv_usec = tc->tc_offset_micro + boottime.tv_usec;
893 while (tc->tc_nanotime.tv_nsec >= 1000000000) {
894 tc->tc_nanotime.tv_nsec -= 1000000000;
895 tc->tc_microtime.tv_usec -= 1000000;
896 tc->tc_nanotime.tv_sec++;
898 time_second = tc->tc_microtime.tv_sec = tc->tc_nanotime.tv_sec;
903 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
905 SYSCTL_INT(_kern_timecounter, OID_AUTO, method, CTLFLAG_RW, &tco_method, 0,
906 "This variable determines the method used for updating timecounters. "
907 "If the default algorithm (0) fails with \"calcru negative...\" messages "
908 "try the alternate algorithm (1) which handles bad hardware better."
913 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
916 struct timecounter *newtc, *tc;
919 tc = timecounter->tc_tweak;
920 strncpy(newname, tc->tc_name, sizeof(newname));
921 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
922 if (error == 0 && req->newptr != NULL &&
923 strcmp(newname, tc->tc_name) != 0) {
924 for (newtc = tc->tc_avail; newtc != tc;
925 newtc = newtc->tc_avail) {
926 if (strcmp(newname, newtc->tc_name) == 0) {
927 /* Warm up new timecounter. */
928 (void)newtc->tc_get_timecount(newtc);
930 switch_timecounter(newtc);
939 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
940 0, 0, sysctl_kern_timecounter_hardware, "A", "");
944 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
947 struct pps_fetch_args *fapi;
949 struct pps_kcbind_args *kapi;
955 case PPS_IOC_DESTROY:
957 case PPS_IOC_SETPARAMS:
958 app = (pps_params_t *)data;
959 if (app->mode & ~pps->ppscap)
961 pps->ppsparam = *app;
963 case PPS_IOC_GETPARAMS:
964 app = (pps_params_t *)data;
965 *app = pps->ppsparam;
966 app->api_version = PPS_API_VERS_1;
969 *(int*)data = pps->ppscap;
972 fapi = (struct pps_fetch_args *)data;
973 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
975 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
977 pps->ppsinfo.current_mode = pps->ppsparam.mode;
978 fapi->pps_info_buf = pps->ppsinfo;
982 kapi = (struct pps_kcbind_args *)data;
983 /* XXX Only root should be able to do this */
984 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
986 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
988 if (kapi->edge & ~pps->ppscap)
990 pps->kcmode = kapi->edge;
1001 pps_init(struct pps_state *pps)
1003 pps->ppscap |= PPS_TSFMT_TSPEC;
1004 if (pps->ppscap & PPS_CAPTUREASSERT)
1005 pps->ppscap |= PPS_OFFSETASSERT;
1006 if (pps->ppscap & PPS_CAPTURECLEAR)
1007 pps->ppscap |= PPS_OFFSETCLEAR;
1011 pps_event(struct pps_state *pps, struct timecounter *tc, unsigned count, int event)
1013 struct timespec ts, *tsp, *osp;
1015 unsigned tcount, *pcount;
1019 /* Things would be easier with arrays... */
1020 if (event == PPS_CAPTUREASSERT) {
1021 tsp = &pps->ppsinfo.assert_timestamp;
1022 osp = &pps->ppsparam.assert_offset;
1023 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1024 fhard = pps->kcmode & PPS_CAPTUREASSERT;
1025 pcount = &pps->ppscount[0];
1026 pseq = &pps->ppsinfo.assert_sequence;
1028 tsp = &pps->ppsinfo.clear_timestamp;
1029 osp = &pps->ppsparam.clear_offset;
1030 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1031 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1032 pcount = &pps->ppscount[1];
1033 pseq = &pps->ppsinfo.clear_sequence;
1036 /* The timecounter changed: bail */
1038 pps->ppstc->tc_name != tc->tc_name ||
1039 tc->tc_name != timecounter->tc_name) {
1045 /* Nothing really happened */
1046 if (*pcount == count)
1051 /* Convert the count to timespec */
1052 ts.tv_sec = tc->tc_offset_sec;
1053 tcount = count - tc->tc_offset_count;
1054 tcount &= tc->tc_counter_mask;
1055 delta = tc->tc_offset_nano;
1056 delta += ((u_int64_t)tcount * tc->tc_scale_nano_f);
1058 delta += ((u_int64_t)tcount * tc->tc_scale_nano_i);
1059 delta += boottime.tv_usec * 1000;
1060 ts.tv_sec += boottime.tv_sec;
1061 while (delta >= 1000000000) {
1062 delta -= 1000000000;
1071 timespecadd(tsp, osp);
1072 if (tsp->tv_nsec < 0) {
1073 tsp->tv_nsec += 1000000000;
1079 /* magic, at its best... */
1080 tcount = count - pps->ppscount[2];
1081 pps->ppscount[2] = count;
1082 tcount &= tc->tc_counter_mask;
1083 delta = ((u_int64_t)tcount * tc->tc_tweak->tc_scale_nano_f);
1085 delta += ((u_int64_t)tcount * tc->tc_tweak->tc_scale_nano_i);
1086 hardpps(tsp, delta);