xref: /onnv-gate/usr/src/uts/common/os/clock.c (revision 11066:cebb50cbe4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52036Swentaoy  * Common Development and Distribution License (the "License").
62036Swentaoy  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
258566SMadhavan.Venkataraman@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/tuneable.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/lgrp.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/proc.h>
390Sstevel@tonic-gate #include <sys/callo.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/var.h>
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate #include <sys/swap.h>
440Sstevel@tonic-gate #include <sys/vmsystm.h>
450Sstevel@tonic-gate #include <sys/class.h>
460Sstevel@tonic-gate #include <sys/time.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/vtrace.h>
490Sstevel@tonic-gate #include <sys/spl.h>
500Sstevel@tonic-gate #include <sys/atomic.h>
510Sstevel@tonic-gate #include <sys/dumphdr.h>
520Sstevel@tonic-gate #include <sys/archsystm.h>
530Sstevel@tonic-gate #include <sys/fs/swapnode.h>
540Sstevel@tonic-gate #include <sys/panic.h>
550Sstevel@tonic-gate #include <sys/disp.h>
560Sstevel@tonic-gate #include <sys/msacct.h>
570Sstevel@tonic-gate #include <sys/mem_cage.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <vm/page.h>
600Sstevel@tonic-gate #include <vm/anon.h>
610Sstevel@tonic-gate #include <vm/rm.h>
620Sstevel@tonic-gate #include <sys/cyclic.h>
630Sstevel@tonic-gate #include <sys/cpupart.h>
640Sstevel@tonic-gate #include <sys/rctl.h>
650Sstevel@tonic-gate #include <sys/task.h>
660Sstevel@tonic-gate #include <sys/sdt.h>
675107Seota #include <sys/ddi_timer.h>
6810696SDavid.Hollister@Sun.COM #include <sys/random.h>
6910696SDavid.Hollister@Sun.COM #include <sys/modctl.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * for NTP support
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate #include <sys/timex.h>
750Sstevel@tonic-gate #include <sys/inttypes.h>
760Sstevel@tonic-gate 
77*11066Srafael.vanoni@sun.com #include <sys/sunddi.h>
78*11066Srafael.vanoni@sun.com #include <sys/clock_impl.h>
79*11066Srafael.vanoni@sun.com 
800Sstevel@tonic-gate /*
813792Sakolb  * clock() is called straight from the clock cyclic; see clock_init().
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Functions:
840Sstevel@tonic-gate  *	reprime clock
850Sstevel@tonic-gate  *	maintain date
860Sstevel@tonic-gate  *	jab the scheduler
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate extern kcondvar_t	fsflush_cv;
900Sstevel@tonic-gate extern sysinfo_t	sysinfo;
910Sstevel@tonic-gate extern vminfo_t	vminfo;
920Sstevel@tonic-gate extern int	idleswtch;	/* flag set while idle in pswtch() */
9310696SDavid.Hollister@Sun.COM extern hrtime_t volatile devinfo_freeze;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * high-precision avenrun values.  These are needed to make the
970Sstevel@tonic-gate  * regular avenrun values accurate.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static uint64_t hp_avenrun[3];
1000Sstevel@tonic-gate int	avenrun[3];		/* FSCALED average run queue lengths */
1010Sstevel@tonic-gate time_t	time;	/* time in seconds since 1970 - for compatibility only */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static struct loadavg_s loadavg;
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Phase/frequency-lock loop (PLL/FLL) definitions
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * The following variables are read and set by the ntp_adjtime() system
1080Sstevel@tonic-gate  * call.
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * time_state shows the state of the system clock, with values defined
1110Sstevel@tonic-gate  * in the timex.h header file.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * time_status shows the status of the system clock, with bits defined
1140Sstevel@tonic-gate  * in the timex.h header file.
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  * time_offset is used by the PLL/FLL to adjust the system time in small
1170Sstevel@tonic-gate  * increments.
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  * time_constant determines the bandwidth or "stiffness" of the PLL.
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * time_tolerance determines maximum frequency error or tolerance of the
1220Sstevel@tonic-gate  * CPU clock oscillator and is a property of the architecture; however,
1230Sstevel@tonic-gate  * in principle it could change as result of the presence of external
1240Sstevel@tonic-gate  * discipline signals, for instance.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  * time_precision is usually equal to the kernel tick variable; however,
1270Sstevel@tonic-gate  * in cases where a precision clock counter or external clock is
1280Sstevel@tonic-gate  * available, the resolution can be much less than this and depend on
1290Sstevel@tonic-gate  * whether the external clock is working or not.
1300Sstevel@tonic-gate  *
1310Sstevel@tonic-gate  * time_maxerror is initialized by a ntp_adjtime() call and increased by
1320Sstevel@tonic-gate  * the kernel once each second to reflect the maximum error bound
1330Sstevel@tonic-gate  * growth.
1340Sstevel@tonic-gate  *
1350Sstevel@tonic-gate  * time_esterror is set and read by the ntp_adjtime() call, but
1360Sstevel@tonic-gate  * otherwise not used by the kernel.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate int32_t time_state = TIME_OK;	/* clock state */
1390Sstevel@tonic-gate int32_t time_status = STA_UNSYNC;	/* clock status bits */
1400Sstevel@tonic-gate int32_t time_offset = 0;		/* time offset (us) */
1410Sstevel@tonic-gate int32_t time_constant = 0;		/* pll time constant */
1420Sstevel@tonic-gate int32_t time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
1430Sstevel@tonic-gate int32_t time_precision = 1;	/* clock precision (us) */
1440Sstevel@tonic-gate int32_t time_maxerror = MAXPHASE;	/* maximum error (us) */
1450Sstevel@tonic-gate int32_t time_esterror = MAXPHASE;	/* estimated error (us) */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * The following variables establish the state of the PLL/FLL and the
1490Sstevel@tonic-gate  * residual time and frequency offset of the local clock. The scale
1500Sstevel@tonic-gate  * factors are defined in the timex.h header file.
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  * time_phase and time_freq are the phase increment and the frequency
1530Sstevel@tonic-gate  * increment, respectively, of the kernel time variable.
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  * time_freq is set via ntp_adjtime() from a value stored in a file when
1560Sstevel@tonic-gate  * the synchronization daemon is first started. Its value is retrieved
1570Sstevel@tonic-gate  * via ntp_adjtime() and written to the file about once per hour by the
1580Sstevel@tonic-gate  * daemon.
1590Sstevel@tonic-gate  *
1600Sstevel@tonic-gate  * time_adj is the adjustment added to the value of tick at each timer
1610Sstevel@tonic-gate  * interrupt and is recomputed from time_phase and time_freq at each
1620Sstevel@tonic-gate  * seconds rollover.
1630Sstevel@tonic-gate  *
1640Sstevel@tonic-gate  * time_reftime is the second's portion of the system time at the last
1650Sstevel@tonic-gate  * call to ntp_adjtime(). It is used to adjust the time_freq variable
1660Sstevel@tonic-gate  * and to increase the time_maxerror as the time since last update
1670Sstevel@tonic-gate  * increases.
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate int32_t time_phase = 0;		/* phase offset (scaled us) */
1700Sstevel@tonic-gate int32_t time_freq = 0;		/* frequency offset (scaled ppm) */
1710Sstevel@tonic-gate int32_t time_adj = 0;		/* tick adjust (scaled 1 / hz) */
1720Sstevel@tonic-gate int32_t time_reftime = 0;		/* time at last adjustment (s) */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * The scale factors of the following variables are defined in the
1760Sstevel@tonic-gate  * timex.h header file.
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  * pps_time contains the time at each calibration interval, as read by
1790Sstevel@tonic-gate  * microtime(). pps_count counts the seconds of the calibration
1800Sstevel@tonic-gate  * interval, the duration of which is nominally pps_shift in powers of
1810Sstevel@tonic-gate  * two.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * pps_offset is the time offset produced by the time median filter
1840Sstevel@tonic-gate  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
1850Sstevel@tonic-gate  * this filter.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  * pps_freq is the frequency offset produced by the frequency median
1880Sstevel@tonic-gate  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
1890Sstevel@tonic-gate  * by this filter.
1900Sstevel@tonic-gate  *
1910Sstevel@tonic-gate  * pps_usec is latched from a high resolution counter or external clock
1920Sstevel@tonic-gate  * at pps_time. Here we want the hardware counter contents only, not the
1930Sstevel@tonic-gate  * contents plus the time_tv.usec as usual.
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  * pps_valid counts the number of seconds since the last PPS update. It
1960Sstevel@tonic-gate  * is used as a watchdog timer to disable the PPS discipline should the
1970Sstevel@tonic-gate  * PPS signal be lost.
1980Sstevel@tonic-gate  *
1990Sstevel@tonic-gate  * pps_glitch counts the number of seconds since the beginning of an
2000Sstevel@tonic-gate  * offset burst more than tick/2 from current nominal offset. It is used
2010Sstevel@tonic-gate  * mainly to suppress error bursts due to priority conflicts between the
2020Sstevel@tonic-gate  * PPS interrupt and timer interrupt.
2030Sstevel@tonic-gate  *
2040Sstevel@tonic-gate  * pps_intcnt counts the calibration intervals for use in the interval-
2050Sstevel@tonic-gate  * adaptation algorithm. It's just too complicated for words.
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate struct timeval pps_time;	/* kernel time at last interval */
2080Sstevel@tonic-gate int32_t pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
2090Sstevel@tonic-gate int32_t pps_offset = 0;		/* pps time offset (us) */
2100Sstevel@tonic-gate int32_t pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
2110Sstevel@tonic-gate int32_t pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
2120Sstevel@tonic-gate int32_t pps_freq = 0;		/* frequency offset (scaled ppm) */
2130Sstevel@tonic-gate int32_t pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
2140Sstevel@tonic-gate int32_t pps_usec = 0;		/* microsec counter at last interval */
2150Sstevel@tonic-gate int32_t pps_valid = PPS_VALID;	/* pps signal watchdog counter */
2160Sstevel@tonic-gate int32_t pps_glitch = 0;		/* pps signal glitch counter */
2170Sstevel@tonic-gate int32_t pps_count = 0;		/* calibration interval counter (s) */
2180Sstevel@tonic-gate int32_t pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
2190Sstevel@tonic-gate int32_t pps_intcnt = 0;		/* intervals at current duration */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * PPS signal quality monitors
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * pps_jitcnt counts the seconds that have been discarded because the
2250Sstevel@tonic-gate  * jitter measured by the time median filter exceeds the limit MAXTIME
2260Sstevel@tonic-gate  * (100 us).
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  * pps_calcnt counts the frequency calibration intervals, which are
2290Sstevel@tonic-gate  * variable from 4 s to 256 s.
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * pps_errcnt counts the calibration intervals which have been discarded
2320Sstevel@tonic-gate  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
2330Sstevel@tonic-gate  * calibration interval jitter exceeds two ticks.
2340Sstevel@tonic-gate  *
2350Sstevel@tonic-gate  * pps_stbcnt counts the calibration intervals that have been discarded
2360Sstevel@tonic-gate  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate int32_t pps_jitcnt = 0;		/* jitter limit exceeded */
2390Sstevel@tonic-gate int32_t pps_calcnt = 0;		/* calibration intervals */
2400Sstevel@tonic-gate int32_t pps_errcnt = 0;		/* calibration errors */
2410Sstevel@tonic-gate int32_t pps_stbcnt = 0;		/* stability limit exceeded */
2420Sstevel@tonic-gate 
243*11066Srafael.vanoni@sun.com kcondvar_t lbolt_cv;
2440Sstevel@tonic-gate 
245*11066Srafael.vanoni@sun.com /*
246*11066Srafael.vanoni@sun.com  * Hybrid lbolt implementation:
247*11066Srafael.vanoni@sun.com  *
248*11066Srafael.vanoni@sun.com  * The service historically provided by the lbolt and lbolt64 variables has
249*11066Srafael.vanoni@sun.com  * been replaced by the ddi_get_lbolt() and ddi_get_lbolt64() routines, and the
250*11066Srafael.vanoni@sun.com  * original symbols removed from the system. The once clock driven variables are
251*11066Srafael.vanoni@sun.com  * now implemented in an event driven fashion, backed by gethrtime() coarsed to
252*11066Srafael.vanoni@sun.com  * the appropriate clock resolution. The default event driven implementation is
253*11066Srafael.vanoni@sun.com  * complemented by a cyclic driven one, active only during periods of intense
254*11066Srafael.vanoni@sun.com  * activity around the DDI lbolt routines, when a lbolt specific cyclic is
255*11066Srafael.vanoni@sun.com  * reprogramed to fire at a clock tick interval to serve consumers of lbolt who
256*11066Srafael.vanoni@sun.com  * rely on the original low cost of consulting a memory position.
257*11066Srafael.vanoni@sun.com  *
258*11066Srafael.vanoni@sun.com  * The implementation uses the number of calls to these routines and the
259*11066Srafael.vanoni@sun.com  * frequency of these to determine when to transition from event to cyclic
260*11066Srafael.vanoni@sun.com  * driven and vice-versa. These values are kept on a per CPU basis for
261*11066Srafael.vanoni@sun.com  * scalability reasons and to prevent CPUs from constantly invalidating a single
262*11066Srafael.vanoni@sun.com  * cache line when modifying a global variable. The transition from event to
263*11066Srafael.vanoni@sun.com  * cyclic mode happens once the thresholds are crossed, and activity on any CPU
264*11066Srafael.vanoni@sun.com  * can cause such transition.
265*11066Srafael.vanoni@sun.com  *
266*11066Srafael.vanoni@sun.com  * The lbolt_hybrid function pointer is called by ddi_get_lbolt() and
267*11066Srafael.vanoni@sun.com  * ddi_get_lbolt64(), and will point to lbolt_event_driven() or
268*11066Srafael.vanoni@sun.com  * lbolt_cyclic_driven() according to the current mode. When the thresholds
269*11066Srafael.vanoni@sun.com  * are exceeded, lbolt_event_driven() will reprogram the lbolt cyclic to
270*11066Srafael.vanoni@sun.com  * fire at a nsec_per_tick interval and increment an internal variable at
271*11066Srafael.vanoni@sun.com  * each firing. lbolt_hybrid will then point to lbolt_cyclic_driven(), which
272*11066Srafael.vanoni@sun.com  * will simply return the value of such variable. lbolt_cyclic() will attempt
273*11066Srafael.vanoni@sun.com  * to shut itself off at each threshold interval (sampling period for calls
274*11066Srafael.vanoni@sun.com  * to the DDI lbolt routines), and return to the event driven mode, but will
275*11066Srafael.vanoni@sun.com  * be prevented from doing so if lbolt_cyclic_driven() is being heavily used.
276*11066Srafael.vanoni@sun.com  *
277*11066Srafael.vanoni@sun.com  * lbolt_bootstrap is used during boot to serve lbolt consumers who don't wait
278*11066Srafael.vanoni@sun.com  * for the cyclic subsystem to be intialized.
279*11066Srafael.vanoni@sun.com  *
280*11066Srafael.vanoni@sun.com  */
281*11066Srafael.vanoni@sun.com static int64_t lbolt_bootstrap(void);
282*11066Srafael.vanoni@sun.com int64_t lbolt_event_driven(void);
283*11066Srafael.vanoni@sun.com int64_t lbolt_cyclic_driven(void);
284*11066Srafael.vanoni@sun.com int64_t (*lbolt_hybrid)(void) = lbolt_bootstrap;
285*11066Srafael.vanoni@sun.com uint_t lbolt_ev_to_cyclic(caddr_t, caddr_t);
286*11066Srafael.vanoni@sun.com 
287*11066Srafael.vanoni@sun.com /*
288*11066Srafael.vanoni@sun.com  * lbolt's cyclic, installed by clock_init().
289*11066Srafael.vanoni@sun.com  */
290*11066Srafael.vanoni@sun.com static void lbolt_cyclic(void);
291*11066Srafael.vanoni@sun.com 
292*11066Srafael.vanoni@sun.com /*
293*11066Srafael.vanoni@sun.com  * Tunable to keep lbolt in cyclic driven mode. This will prevent the system
294*11066Srafael.vanoni@sun.com  * from switching back to event driven, once it reaches cyclic mode.
295*11066Srafael.vanoni@sun.com  */
296*11066Srafael.vanoni@sun.com static boolean_t lbolt_cyc_only = B_FALSE;
297*11066Srafael.vanoni@sun.com 
298*11066Srafael.vanoni@sun.com /*
299*11066Srafael.vanoni@sun.com  * Cache aligned, per CPU structure with lbolt usage statistics.
300*11066Srafael.vanoni@sun.com  */
301*11066Srafael.vanoni@sun.com static lbolt_cpu_t *lb_cpu;
302*11066Srafael.vanoni@sun.com 
303*11066Srafael.vanoni@sun.com /*
304*11066Srafael.vanoni@sun.com  * Single, cache aligned, structure with all the information required by
305*11066Srafael.vanoni@sun.com  * the lbolt implementation.
306*11066Srafael.vanoni@sun.com  */
307*11066Srafael.vanoni@sun.com lbolt_info_t *lb_info;
308*11066Srafael.vanoni@sun.com 
309*11066Srafael.vanoni@sun.com 
3100Sstevel@tonic-gate int one_sec = 1; /* turned on once every second */
3110Sstevel@tonic-gate static int fsflushcnt;	/* counter for t_fsflushr */
3120Sstevel@tonic-gate int	dosynctodr = 1;	/* patchable; enable/disable sync to TOD chip */
3130Sstevel@tonic-gate int	tod_needsync = 0;	/* need to sync tod chip with software time */
3140Sstevel@tonic-gate static int tod_broken = 0;	/* clock chip doesn't work */
3150Sstevel@tonic-gate time_t	boot_time = 0;		/* Boot time in seconds since 1970 */
3160Sstevel@tonic-gate cyclic_id_t clock_cyclic;	/* clock()'s cyclic_id */
3170Sstevel@tonic-gate cyclic_id_t deadman_cyclic;	/* deadman()'s cyclic_id */
3185265Seota cyclic_id_t ddi_timer_cyclic;	/* cyclic_timer()'s cyclic_id */
3190Sstevel@tonic-gate 
3205788Smv143129 extern void	clock_tick_schedule(int);
3215788Smv143129 
3220Sstevel@tonic-gate static int lgrp_ticks;		/* counter to schedule lgrp load calcs */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * for tod fault detection
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate #define	TOD_REF_FREQ		((longlong_t)(NANOSEC))
3280Sstevel@tonic-gate #define	TOD_STALL_THRESHOLD	(TOD_REF_FREQ * 3 / 2)
3290Sstevel@tonic-gate #define	TOD_JUMP_THRESHOLD	(TOD_REF_FREQ / 2)
3300Sstevel@tonic-gate #define	TOD_FILTER_N		4
3310Sstevel@tonic-gate #define	TOD_FILTER_SETTLE	(4 * TOD_FILTER_N)
3320Sstevel@tonic-gate static int tod_faulted = TOD_NOFAULT;
3330Sstevel@tonic-gate static int tod_fault_reset_flag = 0;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /* patchable via /etc/system */
3360Sstevel@tonic-gate int tod_validate_enable = 1;
3370Sstevel@tonic-gate 
33810696SDavid.Hollister@Sun.COM /* Diagnose/Limit messages about delay(9F) called from interrupt context */
33910696SDavid.Hollister@Sun.COM int			delay_from_interrupt_diagnose = 0;
34010696SDavid.Hollister@Sun.COM volatile uint32_t	delay_from_interrupt_msg = 20;
34110696SDavid.Hollister@Sun.COM 
3420Sstevel@tonic-gate /*
343950Ssethg  * On non-SPARC systems, TOD validation must be deferred until gethrtime
344950Ssethg  * returns non-zero values (after mach_clkinit's execution).
345950Ssethg  * On SPARC systems, it must be deferred until after hrtime_base
346950Ssethg  * and hres_last_tick are set (in the first invocation of hres_tick).
347950Ssethg  * Since in both cases the prerequisites occur before the invocation of
348950Ssethg  * tod_get() in clock(), the deferment is lifted there.
349950Ssethg  */
350950Ssethg static boolean_t tod_validate_deferred = B_TRUE;
351950Ssethg 
352950Ssethg /*
3530Sstevel@tonic-gate  * tod_fault_table[] must be aligned with
3540Sstevel@tonic-gate  * enum tod_fault_type in systm.h
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate static char *tod_fault_table[] = {
3570Sstevel@tonic-gate 	"Reversed",			/* TOD_REVERSED */
3580Sstevel@tonic-gate 	"Stalled",			/* TOD_STALLED */
3590Sstevel@tonic-gate 	"Jumped",			/* TOD_JUMPED */
3605084Sjohnlev 	"Changed in Clock Rate",	/* TOD_RATECHANGED */
3615084Sjohnlev 	"Is Read-Only"			/* TOD_RDONLY */
3620Sstevel@tonic-gate 	/*
3630Sstevel@tonic-gate 	 * no strings needed for TOD_NOFAULT
3640Sstevel@tonic-gate 	 */
3650Sstevel@tonic-gate };
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate  * test hook for tod broken detection in tod_validate
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate int tod_unit_test = 0;
3710Sstevel@tonic-gate time_t tod_test_injector;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate #define	CLOCK_ADJ_HIST_SIZE	4
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate static int	adj_hist_entry;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate int64_t clock_adj_hist[CLOCK_ADJ_HIST_SIZE];
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate static void calcloadavg(int, uint64_t *);
3800Sstevel@tonic-gate static int genloadavg(struct loadavg_s *);
3810Sstevel@tonic-gate static void loadavg_update();
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate void (*cmm_clock_callout)() = NULL;
3843792Sakolb void (*cpucaps_clock_callout)() = NULL;
3850Sstevel@tonic-gate 
3865788Smv143129 extern clock_t clock_tick_proc_max;
3875788Smv143129 
388*11066Srafael.vanoni@sun.com static int64_t deadman_counter = 0;
389*11066Srafael.vanoni@sun.com 
3900Sstevel@tonic-gate static void
3910Sstevel@tonic-gate clock(void)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	kthread_t	*t;
3945788Smv143129 	uint_t	nrunnable;
3950Sstevel@tonic-gate 	uint_t	w_io;
3960Sstevel@tonic-gate 	cpu_t	*cp;
3970Sstevel@tonic-gate 	cpupart_t *cpupart;
3980Sstevel@tonic-gate 	extern void set_anoninfo();
3990Sstevel@tonic-gate 	extern	void	set_freemem();
4000Sstevel@tonic-gate 	void	(*funcp)();
4010Sstevel@tonic-gate 	int32_t ltemp;
4020Sstevel@tonic-gate 	int64_t lltemp;
4030Sstevel@tonic-gate 	int s;
4040Sstevel@tonic-gate 	int do_lgrp_load;
4050Sstevel@tonic-gate 	int i;
406*11066Srafael.vanoni@sun.com 	clock_t now = LBOLT_NO_ACCOUNT;	/* current tick */
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (panicstr)
4090Sstevel@tonic-gate 		return;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	set_anoninfo();
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * Make sure that 'freemem' do not drift too far from the truth
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	set_freemem();
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Before the section which is repeated is executed, we do
4200Sstevel@tonic-gate 	 * the time delta processing which occurs every clock tick
4210Sstevel@tonic-gate 	 *
4220Sstevel@tonic-gate 	 * There is additional processing which happens every time
4230Sstevel@tonic-gate 	 * the nanosecond counter rolls over which is described
4240Sstevel@tonic-gate 	 * below - see the section which begins with : if (one_sec)
4250Sstevel@tonic-gate 	 *
4260Sstevel@tonic-gate 	 * This section marks the beginning of the precision-kernel
4270Sstevel@tonic-gate 	 * code fragment.
4280Sstevel@tonic-gate 	 *
4290Sstevel@tonic-gate 	 * First, compute the phase adjustment. If the low-order bits
4300Sstevel@tonic-gate 	 * (time_phase) of the update overflow, bump the higher order
4310Sstevel@tonic-gate 	 * bits (time_update).
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	time_phase += time_adj;
4340Sstevel@tonic-gate 	if (time_phase <= -FINEUSEC) {
4350Sstevel@tonic-gate 		ltemp = -time_phase / SCALE_PHASE;
4360Sstevel@tonic-gate 		time_phase += ltemp * SCALE_PHASE;
4370Sstevel@tonic-gate 		s = hr_clock_lock();
4380Sstevel@tonic-gate 		timedelta -= ltemp * (NANOSEC/MICROSEC);
4390Sstevel@tonic-gate 		hr_clock_unlock(s);
4400Sstevel@tonic-gate 	} else if (time_phase >= FINEUSEC) {
4410Sstevel@tonic-gate 		ltemp = time_phase / SCALE_PHASE;
4420Sstevel@tonic-gate 		time_phase -= ltemp * SCALE_PHASE;
4430Sstevel@tonic-gate 		s = hr_clock_lock();
4440Sstevel@tonic-gate 		timedelta += ltemp * (NANOSEC/MICROSEC);
4450Sstevel@tonic-gate 		hr_clock_unlock(s);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * End of precision-kernel code fragment which is processed
4500Sstevel@tonic-gate 	 * every timer interrupt.
4510Sstevel@tonic-gate 	 *
4520Sstevel@tonic-gate 	 * Continue with the interrupt processing as scheduled.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	/*
4550Sstevel@tonic-gate 	 * Count the number of runnable threads and the number waiting
4560Sstevel@tonic-gate 	 * for some form of I/O to complete -- gets added to
4570Sstevel@tonic-gate 	 * sysinfo.waiting.  To know the state of the system, must add
4580Sstevel@tonic-gate 	 * wait counts from all CPUs.  Also add up the per-partition
4590Sstevel@tonic-gate 	 * statistics.
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	w_io = 0;
4620Sstevel@tonic-gate 	nrunnable = 0;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * keep track of when to update lgrp/part loads
4660Sstevel@tonic-gate 	 */
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	do_lgrp_load = 0;
4690Sstevel@tonic-gate 	if (lgrp_ticks++ >= hz / 10) {
4700Sstevel@tonic-gate 		lgrp_ticks = 0;
4710Sstevel@tonic-gate 		do_lgrp_load = 1;
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
474*11066Srafael.vanoni@sun.com 	if (one_sec) {
4750Sstevel@tonic-gate 		loadavg_update();
476*11066Srafael.vanoni@sun.com 		deadman_counter++;
477*11066Srafael.vanoni@sun.com 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * First count the threads waiting on kpreempt queues in each
4810Sstevel@tonic-gate 	 * CPU partition.
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	cpupart = cp_list_head;
4850Sstevel@tonic-gate 	do {
4860Sstevel@tonic-gate 		uint_t cpupart_nrunnable = cpupart->cp_kp_queue.disp_nrunnable;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		cpupart->cp_updates++;
4890Sstevel@tonic-gate 		nrunnable += cpupart_nrunnable;
4900Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpupart_nrunnable;
4910Sstevel@tonic-gate 		if (one_sec) {
4920Sstevel@tonic-gate 			cpupart->cp_nrunning = 0;
4930Sstevel@tonic-gate 			cpupart->cp_nrunnable = cpupart_nrunnable;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/* Now count the per-CPU statistics. */
4990Sstevel@tonic-gate 	cp = cpu_list;
5000Sstevel@tonic-gate 	do {
5010Sstevel@tonic-gate 		uint_t cpu_nrunnable = cp->cpu_disp->disp_nrunnable;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		nrunnable += cpu_nrunnable;
5040Sstevel@tonic-gate 		cpupart = cp->cpu_part;
5050Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpu_nrunnable;
5063446Smrj 		if (one_sec) {
5070Sstevel@tonic-gate 			cpupart->cp_nrunnable += cpu_nrunnable;
5083446Smrj 			/*
5095788Smv143129 			 * Update user, system, and idle cpu times.
5105788Smv143129 			 */
5115788Smv143129 			cpupart->cp_nrunning++;
5125788Smv143129 			/*
5133446Smrj 			 * w_io is used to update sysinfo.waiting during
5143446Smrj 			 * one_second processing below.  Only gather w_io
5153446Smrj 			 * information when we walk the list of cpus if we're
5163446Smrj 			 * going to perform one_second processing.
5173446Smrj 			 */
5183446Smrj 			w_io += CPU_STATS(cp, sys.iowait);
5195076Smishra 		}
5203446Smrj 
5215076Smishra 		if (one_sec && (cp->cpu_flags & CPU_EXISTS)) {
5225076Smishra 			int i, load, change;
5235076Smishra 			hrtime_t intracct, intrused;
5245076Smishra 			const hrtime_t maxnsec = 1000000000;
5255076Smishra 			const int precision = 100;
5265076Smishra 
5275076Smishra 			/*
5285076Smishra 			 * Estimate interrupt load on this cpu each second.
5295076Smishra 			 * Computes cpu_intrload as %utilization (0-99).
5305076Smishra 			 */
5315076Smishra 
5325076Smishra 			/* add up interrupt time from all micro states */
5335076Smishra 			for (intracct = 0, i = 0; i < NCMSTATES; i++)
5345076Smishra 				intracct += cp->cpu_intracct[i];
5355076Smishra 			scalehrtime(&intracct);
5365076Smishra 
5375076Smishra 			/* compute nsec used in the past second */
5385076Smishra 			intrused = intracct - cp->cpu_intrlast;
5395076Smishra 			cp->cpu_intrlast = intracct;
5405076Smishra 
5415076Smishra 			/* limit the value for safety (and the first pass) */
5425076Smishra 			if (intrused >= maxnsec)
5435076Smishra 				intrused = maxnsec - 1;
5445076Smishra 
5455076Smishra 			/* calculate %time in interrupt */
5465076Smishra 			load = (precision * intrused) / maxnsec;
5475076Smishra 			ASSERT(load >= 0 && load < precision);
5485076Smishra 			change = cp->cpu_intrload - load;
5495076Smishra 
5505076Smishra 			/* jump to new max, or decay the old max */
5515076Smishra 			if (change < 0)
5525076Smishra 				cp->cpu_intrload = load;
5535076Smishra 			else if (change > 0)
5545076Smishra 				cp->cpu_intrload -= (change + 3) / 4;
5555076Smishra 
5565076Smishra 			DTRACE_PROBE3(cpu_intrload,
5575076Smishra 			    cpu_t *, cp,
5585076Smishra 			    hrtime_t, intracct,
5595076Smishra 			    hrtime_t, intrused);
5603446Smrj 		}
5615076Smishra 
5620Sstevel@tonic-gate 		if (do_lgrp_load &&
5630Sstevel@tonic-gate 		    (cp->cpu_flags & CPU_EXISTS)) {
5640Sstevel@tonic-gate 			/*
5650Sstevel@tonic-gate 			 * When updating the lgroup's load average,
5660Sstevel@tonic-gate 			 * account for the thread running on the CPU.
5670Sstevel@tonic-gate 			 * If the CPU is the current one, then we need
5680Sstevel@tonic-gate 			 * to account for the underlying thread which
5690Sstevel@tonic-gate 			 * got the clock interrupt not the thread that is
5700Sstevel@tonic-gate 			 * handling the interrupt and caculating the load
5710Sstevel@tonic-gate 			 * average
5720Sstevel@tonic-gate 			 */
5730Sstevel@tonic-gate 			t = cp->cpu_thread;
5740Sstevel@tonic-gate 			if (CPU == cp)
5750Sstevel@tonic-gate 				t = t->t_intr;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 			/*
5780Sstevel@tonic-gate 			 * Account for the load average for this thread if
5790Sstevel@tonic-gate 			 * it isn't the idle thread or it is on the interrupt
5800Sstevel@tonic-gate 			 * stack and not the current CPU handling the clock
5810Sstevel@tonic-gate 			 * interrupt
5820Sstevel@tonic-gate 			 */
5830Sstevel@tonic-gate 			if ((t && t != cp->cpu_idle_thread) || (CPU != cp &&
5840Sstevel@tonic-gate 			    CPU_ON_INTR(cp))) {
5850Sstevel@tonic-gate 				if (t->t_lpl == cp->cpu_lpl) {
5860Sstevel@tonic-gate 					/* local thread */
5870Sstevel@tonic-gate 					cpu_nrunnable++;
5880Sstevel@tonic-gate 				} else {
5890Sstevel@tonic-gate 					/*
5900Sstevel@tonic-gate 					 * This is a remote thread, charge it
5910Sstevel@tonic-gate 					 * against its home lgroup.  Note that
5920Sstevel@tonic-gate 					 * we notice that a thread is remote
5930Sstevel@tonic-gate 					 * only if it's currently executing.
5940Sstevel@tonic-gate 					 * This is a reasonable approximation,
5950Sstevel@tonic-gate 					 * since queued remote threads are rare.
5960Sstevel@tonic-gate 					 * Note also that if we didn't charge
5970Sstevel@tonic-gate 					 * it to its home lgroup, remote
5980Sstevel@tonic-gate 					 * execution would often make a system
5990Sstevel@tonic-gate 					 * appear balanced even though it was
6000Sstevel@tonic-gate 					 * not, and thread placement/migration
6010Sstevel@tonic-gate 					 * would often not be done correctly.
6020Sstevel@tonic-gate 					 */
6030Sstevel@tonic-gate 					lgrp_loadavg(t->t_lpl,
6040Sstevel@tonic-gate 					    LGRP_LOADAVG_IN_THREAD_MAX, 0);
6050Sstevel@tonic-gate 				}
6060Sstevel@tonic-gate 			}
6070Sstevel@tonic-gate 			lgrp_loadavg(cp->cpu_lpl,
6080Sstevel@tonic-gate 			    cpu_nrunnable * LGRP_LOADAVG_IN_THREAD_MAX, 1);
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
6110Sstevel@tonic-gate 
6125788Smv143129 	clock_tick_schedule(one_sec);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * Check for a callout that needs be called from the clock
6160Sstevel@tonic-gate 	 * thread to support the membership protocol in a clustered
6170Sstevel@tonic-gate 	 * system.  Copy the function pointer so that we can reset
6180Sstevel@tonic-gate 	 * this to NULL if needed.
6190Sstevel@tonic-gate 	 */
6200Sstevel@tonic-gate 	if ((funcp = cmm_clock_callout) != NULL)
6210Sstevel@tonic-gate 		(*funcp)();
6220Sstevel@tonic-gate 
6233792Sakolb 	if ((funcp = cpucaps_clock_callout) != NULL)
6243792Sakolb 		(*funcp)();
6253792Sakolb 
6260Sstevel@tonic-gate 	/*
6270Sstevel@tonic-gate 	 * Wakeup the cageout thread waiters once per second.
6280Sstevel@tonic-gate 	 */
6290Sstevel@tonic-gate 	if (one_sec)
6300Sstevel@tonic-gate 		kcage_tick();
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (one_sec) {
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		int drift, absdrift;
6350Sstevel@tonic-gate 		timestruc_t tod;
6360Sstevel@tonic-gate 		int s;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * Beginning of precision-kernel code fragment executed
6400Sstevel@tonic-gate 		 * every second.
6410Sstevel@tonic-gate 		 *
6420Sstevel@tonic-gate 		 * On rollover of the second the phase adjustment to be
6430Sstevel@tonic-gate 		 * used for the next second is calculated.  Also, the
6440Sstevel@tonic-gate 		 * maximum error is increased by the tolerance.  If the
6450Sstevel@tonic-gate 		 * PPS frequency discipline code is present, the phase is
6460Sstevel@tonic-gate 		 * increased to compensate for the CPU clock oscillator
6470Sstevel@tonic-gate 		 * frequency error.
6480Sstevel@tonic-gate 		 *
6490Sstevel@tonic-gate 		 * On a 32-bit machine and given parameters in the timex.h
6500Sstevel@tonic-gate 		 * header file, the maximum phase adjustment is +-512 ms
6510Sstevel@tonic-gate 		 * and maximum frequency offset is (a tad less than)
6520Sstevel@tonic-gate 		 * +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 		time_maxerror += time_tolerance / SCALE_USEC;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		/*
6570Sstevel@tonic-gate 		 * Leap second processing. If in leap-insert state at
6580Sstevel@tonic-gate 		 * the end of the day, the system clock is set back one
6590Sstevel@tonic-gate 		 * second; if in leap-delete state, the system clock is
6600Sstevel@tonic-gate 		 * set ahead one second. The microtime() routine or
6610Sstevel@tonic-gate 		 * external clock driver will insure that reported time
6620Sstevel@tonic-gate 		 * is always monotonic. The ugly divides should be
6630Sstevel@tonic-gate 		 * replaced.
6640Sstevel@tonic-gate 		 */
6650Sstevel@tonic-gate 		switch (time_state) {
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		case TIME_OK:
6680Sstevel@tonic-gate 			if (time_status & STA_INS)
6690Sstevel@tonic-gate 				time_state = TIME_INS;
6700Sstevel@tonic-gate 			else if (time_status & STA_DEL)
6710Sstevel@tonic-gate 				time_state = TIME_DEL;
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		case TIME_INS:
6750Sstevel@tonic-gate 			if (hrestime.tv_sec % 86400 == 0) {
6760Sstevel@tonic-gate 				s = hr_clock_lock();
6770Sstevel@tonic-gate 				hrestime.tv_sec--;
6780Sstevel@tonic-gate 				hr_clock_unlock(s);
6790Sstevel@tonic-gate 				time_state = TIME_OOP;
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		case TIME_DEL:
6840Sstevel@tonic-gate 			if ((hrestime.tv_sec + 1) % 86400 == 0) {
6850Sstevel@tonic-gate 				s = hr_clock_lock();
6860Sstevel@tonic-gate 				hrestime.tv_sec++;
6870Sstevel@tonic-gate 				hr_clock_unlock(s);
6880Sstevel@tonic-gate 				time_state = TIME_WAIT;
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 			break;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 		case TIME_OOP:
6930Sstevel@tonic-gate 			time_state = TIME_WAIT;
6940Sstevel@tonic-gate 			break;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		case TIME_WAIT:
6970Sstevel@tonic-gate 			if (!(time_status & (STA_INS | STA_DEL)))
6980Sstevel@tonic-gate 				time_state = TIME_OK;
6990Sstevel@tonic-gate 		default:
7000Sstevel@tonic-gate 			break;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		/*
7040Sstevel@tonic-gate 		 * Compute the phase adjustment for the next second. In
7050Sstevel@tonic-gate 		 * PLL mode, the offset is reduced by a fixed factor
7060Sstevel@tonic-gate 		 * times the time constant. In FLL mode the offset is
7070Sstevel@tonic-gate 		 * used directly. In either mode, the maximum phase
7080Sstevel@tonic-gate 		 * adjustment for each second is clamped so as to spread
7090Sstevel@tonic-gate 		 * the adjustment over not more than the number of
7100Sstevel@tonic-gate 		 * seconds between updates.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		if (time_offset == 0)
7130Sstevel@tonic-gate 			time_adj = 0;
7140Sstevel@tonic-gate 		else if (time_offset < 0) {
7150Sstevel@tonic-gate 			lltemp = -time_offset;
7160Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
7170Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
7180Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
7190Sstevel@tonic-gate 					    SCALE_KG;
7200Sstevel@tonic-gate 				else
7210Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
7220Sstevel@tonic-gate 					    time_constant;
7230Sstevel@tonic-gate 			}
7240Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7250Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7260Sstevel@tonic-gate 			time_offset += lltemp;
7270Sstevel@tonic-gate 			time_adj = -(lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7280Sstevel@tonic-gate 		} else {
7290Sstevel@tonic-gate 			lltemp = time_offset;
7300Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
7310Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
7320Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
7330Sstevel@tonic-gate 					    SCALE_KG;
7340Sstevel@tonic-gate 				else
7350Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
7360Sstevel@tonic-gate 					    time_constant;
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7390Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7400Sstevel@tonic-gate 			time_offset -= lltemp;
7410Sstevel@tonic-gate 			time_adj = (lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		/*
7450Sstevel@tonic-gate 		 * Compute the frequency estimate and additional phase
7460Sstevel@tonic-gate 		 * adjustment due to frequency error for the next
7470Sstevel@tonic-gate 		 * second. When the PPS signal is engaged, gnaw on the
7480Sstevel@tonic-gate 		 * watchdog counter and update the frequency computed by
7490Sstevel@tonic-gate 		 * the pll and the PPS signal.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		pps_valid++;
7520Sstevel@tonic-gate 		if (pps_valid == PPS_VALID) {
7530Sstevel@tonic-gate 			pps_jitter = MAXTIME;
7540Sstevel@tonic-gate 			pps_stabil = MAXFREQ;
7550Sstevel@tonic-gate 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
7560Sstevel@tonic-gate 			    STA_PPSWANDER | STA_PPSERROR);
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 		lltemp = time_freq + pps_freq;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (lltemp)
7610Sstevel@tonic-gate 			time_adj += (lltemp * SCALE_PHASE) / (SCALE_USEC * hz);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		/*
7640Sstevel@tonic-gate 		 * End of precision kernel-code fragment
7650Sstevel@tonic-gate 		 *
7660Sstevel@tonic-gate 		 * The section below should be modified if we are planning
7670Sstevel@tonic-gate 		 * to use NTP for synchronization.
7680Sstevel@tonic-gate 		 *
7690Sstevel@tonic-gate 		 * Note: the clock synchronization code now assumes
7700Sstevel@tonic-gate 		 * the following:
7710Sstevel@tonic-gate 		 *   - if dosynctodr is 1, then compute the drift between
7720Sstevel@tonic-gate 		 *	the tod chip and software time and adjust one or
7730Sstevel@tonic-gate 		 *	the other depending on the circumstances
7740Sstevel@tonic-gate 		 *
7750Sstevel@tonic-gate 		 *   - if dosynctodr is 0, then the tod chip is independent
7760Sstevel@tonic-gate 		 *	of the software clock and should not be adjusted,
7770Sstevel@tonic-gate 		 *	but allowed to free run.  this allows NTP to sync.
7780Sstevel@tonic-gate 		 *	hrestime without any interference from the tod chip.
7790Sstevel@tonic-gate 		 */
7800Sstevel@tonic-gate 
781950Ssethg 		tod_validate_deferred = B_FALSE;
7820Sstevel@tonic-gate 		mutex_enter(&tod_lock);
7830Sstevel@tonic-gate 		tod = tod_get();
7840Sstevel@tonic-gate 		drift = tod.tv_sec - hrestime.tv_sec;
7850Sstevel@tonic-gate 		absdrift = (drift >= 0) ? drift : -drift;
7860Sstevel@tonic-gate 		if (tod_needsync || absdrift > 1) {
7870Sstevel@tonic-gate 			int s;
7880Sstevel@tonic-gate 			if (absdrift > 2) {
7890Sstevel@tonic-gate 				if (!tod_broken && tod_faulted == TOD_NOFAULT) {
7900Sstevel@tonic-gate 					s = hr_clock_lock();
7910Sstevel@tonic-gate 					hrestime = tod;
7920Sstevel@tonic-gate 					membar_enter();	/* hrestime visible */
7930Sstevel@tonic-gate 					timedelta = 0;
7944123Sdm120769 					timechanged++;
7950Sstevel@tonic-gate 					tod_needsync = 0;
7960Sstevel@tonic-gate 					hr_clock_unlock(s);
7978048SMadhavan.Venkataraman@Sun.COM 					callout_hrestime();
7988048SMadhavan.Venkataraman@Sun.COM 
7990Sstevel@tonic-gate 				}
8000Sstevel@tonic-gate 			} else {
8010Sstevel@tonic-gate 				if (tod_needsync || !dosynctodr) {
8020Sstevel@tonic-gate 					gethrestime(&tod);
8030Sstevel@tonic-gate 					tod_set(tod);
8040Sstevel@tonic-gate 					s = hr_clock_lock();
8050Sstevel@tonic-gate 					if (timedelta == 0)
8060Sstevel@tonic-gate 						tod_needsync = 0;
8070Sstevel@tonic-gate 					hr_clock_unlock(s);
8080Sstevel@tonic-gate 				} else {
8090Sstevel@tonic-gate 					/*
8100Sstevel@tonic-gate 					 * If the drift is 2 seconds on the
8110Sstevel@tonic-gate 					 * money, then the TOD is adjusting
8120Sstevel@tonic-gate 					 * the clock;  record that.
8130Sstevel@tonic-gate 					 */
8140Sstevel@tonic-gate 					clock_adj_hist[adj_hist_entry++ %
815*11066Srafael.vanoni@sun.com 					    CLOCK_ADJ_HIST_SIZE] = now;
8160Sstevel@tonic-gate 					s = hr_clock_lock();
8170Sstevel@tonic-gate 					timedelta = (int64_t)drift*NANOSEC;
8180Sstevel@tonic-gate 					hr_clock_unlock(s);
8190Sstevel@tonic-gate 				}
8200Sstevel@tonic-gate 			}
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 		one_sec = 0;
8230Sstevel@tonic-gate 		time = gethrestime_sec();  /* for crusty old kmem readers */
8240Sstevel@tonic-gate 		mutex_exit(&tod_lock);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * Some drivers still depend on this... XXX
8280Sstevel@tonic-gate 		 */
8290Sstevel@tonic-gate 		cv_broadcast(&lbolt_cv);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		sysinfo.updates++;
8320Sstevel@tonic-gate 		vminfo.freemem += freemem;
8330Sstevel@tonic-gate 		{
8340Sstevel@tonic-gate 			pgcnt_t maxswap, resv, free;
8350Sstevel@tonic-gate 			pgcnt_t avail =
8360Sstevel@tonic-gate 			    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
8370Sstevel@tonic-gate 
8385076Smishra 			maxswap = k_anoninfo.ani_mem_resv +
8395076Smishra 			    k_anoninfo.ani_max +avail;
8400Sstevel@tonic-gate 			free = k_anoninfo.ani_free + avail;
8410Sstevel@tonic-gate 			resv = k_anoninfo.ani_phys_resv +
8420Sstevel@tonic-gate 			    k_anoninfo.ani_mem_resv;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 			vminfo.swap_resv += resv;
8450Sstevel@tonic-gate 			/* number of reserved and allocated pages */
8460Sstevel@tonic-gate #ifdef	DEBUG
8470Sstevel@tonic-gate 			if (maxswap < free)
8480Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < free");
8490Sstevel@tonic-gate 			if (maxswap < resv)
8500Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < resv");
8510Sstevel@tonic-gate #endif
8520Sstevel@tonic-gate 			vminfo.swap_alloc += maxswap - free;
8530Sstevel@tonic-gate 			vminfo.swap_avail += maxswap - resv;
8540Sstevel@tonic-gate 			vminfo.swap_free += free;
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 		if (nrunnable) {
8570Sstevel@tonic-gate 			sysinfo.runque += nrunnable;
8580Sstevel@tonic-gate 			sysinfo.runocc++;
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 		if (nswapped) {
8610Sstevel@tonic-gate 			sysinfo.swpque += nswapped;
8620Sstevel@tonic-gate 			sysinfo.swpocc++;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 		sysinfo.waiting += w_io;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		/*
8670Sstevel@tonic-gate 		 * Wake up fsflush to write out DELWRI
8680Sstevel@tonic-gate 		 * buffers, dirty pages and other cached
8690Sstevel@tonic-gate 		 * administrative data, e.g. inodes.
8700Sstevel@tonic-gate 		 */
8710Sstevel@tonic-gate 		if (--fsflushcnt <= 0) {
8720Sstevel@tonic-gate 			fsflushcnt = tune.t_fsflushr;
8730Sstevel@tonic-gate 			cv_signal(&fsflush_cv);
8740Sstevel@tonic-gate 		}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		vmmeter();
8770Sstevel@tonic-gate 		calcloadavg(genloadavg(&loadavg), hp_avenrun);
8780Sstevel@tonic-gate 		for (i = 0; i < 3; i++)
8790Sstevel@tonic-gate 			/*
8800Sstevel@tonic-gate 			 * At the moment avenrun[] can only hold 31
8810Sstevel@tonic-gate 			 * bits of load average as it is a signed
8820Sstevel@tonic-gate 			 * int in the API. We need to ensure that
8830Sstevel@tonic-gate 			 * hp_avenrun[i] >> (16 - FSHIFT) will not be
8840Sstevel@tonic-gate 			 * too large. If it is, we put the largest value
8850Sstevel@tonic-gate 			 * that we can use into avenrun[i]. This is
8860Sstevel@tonic-gate 			 * kludgey, but about all we can do until we
8870Sstevel@tonic-gate 			 * avenrun[] is declared as an array of uint64[]
8880Sstevel@tonic-gate 			 */
8890Sstevel@tonic-gate 			if (hp_avenrun[i] < ((uint64_t)1<<(31+16-FSHIFT)))
8900Sstevel@tonic-gate 				avenrun[i] = (int32_t)(hp_avenrun[i] >>
8910Sstevel@tonic-gate 				    (16 - FSHIFT));
8920Sstevel@tonic-gate 			else
8930Sstevel@tonic-gate 				avenrun[i] = 0x7fffffff;
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		cpupart = cp_list_head;
8960Sstevel@tonic-gate 		do {
8970Sstevel@tonic-gate 			calcloadavg(genloadavg(&cpupart->cp_loadavg),
8980Sstevel@tonic-gate 			    cpupart->cp_hp_avenrun);
8990Sstevel@tonic-gate 		} while ((cpupart = cpupart->cp_next) != cp_list_head);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		/*
9020Sstevel@tonic-gate 		 * Wake up the swapper thread if necessary.
9030Sstevel@tonic-gate 		 */
9040Sstevel@tonic-gate 		if (runin ||
9050Sstevel@tonic-gate 		    (runout && (avefree < desfree || wake_sched_sec))) {
9060Sstevel@tonic-gate 			t = &t0;
9070Sstevel@tonic-gate 			thread_lock(t);
9080Sstevel@tonic-gate 			if (t->t_state == TS_STOPPED) {
9090Sstevel@tonic-gate 				runin = runout = 0;
9100Sstevel@tonic-gate 				wake_sched_sec = 0;
9110Sstevel@tonic-gate 				t->t_whystop = 0;
9120Sstevel@tonic-gate 				t->t_whatstop = 0;
9130Sstevel@tonic-gate 				t->t_schedflag &= ~TS_ALLSTART;
9140Sstevel@tonic-gate 				THREAD_TRANSITION(t);
9150Sstevel@tonic-gate 				setfrontdq(t);
9160Sstevel@tonic-gate 			}
9170Sstevel@tonic-gate 			thread_unlock(t);
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	/*
9220Sstevel@tonic-gate 	 * Wake up the swapper if any high priority swapped-out threads
9230Sstevel@tonic-gate 	 * became runable during the last tick.
9240Sstevel@tonic-gate 	 */
9250Sstevel@tonic-gate 	if (wake_sched) {
9260Sstevel@tonic-gate 		t = &t0;
9270Sstevel@tonic-gate 		thread_lock(t);
9280Sstevel@tonic-gate 		if (t->t_state == TS_STOPPED) {
9290Sstevel@tonic-gate 			runin = runout = 0;
9300Sstevel@tonic-gate 			wake_sched = 0;
9310Sstevel@tonic-gate 			t->t_whystop = 0;
9320Sstevel@tonic-gate 			t->t_whatstop = 0;
9330Sstevel@tonic-gate 			t->t_schedflag &= ~TS_ALLSTART;
9340Sstevel@tonic-gate 			THREAD_TRANSITION(t);
9350Sstevel@tonic-gate 			setfrontdq(t);
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 		thread_unlock(t);
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate void
9420Sstevel@tonic-gate clock_init(void)
9430Sstevel@tonic-gate {
944*11066Srafael.vanoni@sun.com 	cyc_handler_t clk_hdlr, timer_hdlr, lbolt_hdlr;
945*11066Srafael.vanoni@sun.com 	cyc_time_t clk_when, lbolt_when;
946*11066Srafael.vanoni@sun.com 	int i, sz;
947*11066Srafael.vanoni@sun.com 	intptr_t buf;
9480Sstevel@tonic-gate 
949*11066Srafael.vanoni@sun.com 	/*
950*11066Srafael.vanoni@sun.com 	 * Setup handler and timer for the clock cyclic.
951*11066Srafael.vanoni@sun.com 	 */
952*11066Srafael.vanoni@sun.com 	clk_hdlr.cyh_func = (cyc_func_t)clock;
953*11066Srafael.vanoni@sun.com 	clk_hdlr.cyh_level = CY_LOCK_LEVEL;
954*11066Srafael.vanoni@sun.com 	clk_hdlr.cyh_arg = NULL;
9550Sstevel@tonic-gate 
956*11066Srafael.vanoni@sun.com 	clk_when.cyt_when = 0;
957*11066Srafael.vanoni@sun.com 	clk_when.cyt_interval = nsec_per_tick;
9585107Seota 
9595107Seota 	/*
9605107Seota 	 * cyclic_timer is dedicated to the ddi interface, which
9615107Seota 	 * uses the same clock resolution as the system one.
9625107Seota 	 */
963*11066Srafael.vanoni@sun.com 	timer_hdlr.cyh_func = (cyc_func_t)cyclic_timer;
964*11066Srafael.vanoni@sun.com 	timer_hdlr.cyh_level = CY_LOCK_LEVEL;
965*11066Srafael.vanoni@sun.com 	timer_hdlr.cyh_arg = NULL;
966*11066Srafael.vanoni@sun.com 
967*11066Srafael.vanoni@sun.com 	/*
968*11066Srafael.vanoni@sun.com 	 * Setup the necessary structures for the lbolt cyclic and add the
969*11066Srafael.vanoni@sun.com 	 * soft interrupt which will switch from event to cyclic mode when
970*11066Srafael.vanoni@sun.com 	 * under high pil.
971*11066Srafael.vanoni@sun.com 	 */
972*11066Srafael.vanoni@sun.com 	lbolt_hdlr.cyh_func = (cyc_func_t)lbolt_cyclic;
973*11066Srafael.vanoni@sun.com 	lbolt_hdlr.cyh_level = CY_LOCK_LEVEL;
974*11066Srafael.vanoni@sun.com 	lbolt_hdlr.cyh_arg = NULL;
975*11066Srafael.vanoni@sun.com 
976*11066Srafael.vanoni@sun.com 	lbolt_when.cyt_interval = nsec_per_tick;
977*11066Srafael.vanoni@sun.com 
978*11066Srafael.vanoni@sun.com 	if (lbolt_cyc_only) {
979*11066Srafael.vanoni@sun.com 		lbolt_when.cyt_when = 0;
980*11066Srafael.vanoni@sun.com 		lbolt_hybrid = lbolt_cyclic_driven;
981*11066Srafael.vanoni@sun.com 	} else {
982*11066Srafael.vanoni@sun.com 		lbolt_when.cyt_when = CY_INFINITY;
983*11066Srafael.vanoni@sun.com 		lbolt_hybrid = lbolt_event_driven;
984*11066Srafael.vanoni@sun.com 	}
9855107Seota 
986*11066Srafael.vanoni@sun.com 	/*
987*11066Srafael.vanoni@sun.com 	 * Allocate cache line aligned space for the per CPU lbolt data and
988*11066Srafael.vanoni@sun.com 	 * lb_info structure. We also initialize these structures with their
989*11066Srafael.vanoni@sun.com 	 * default values and install the softint to change from event to
990*11066Srafael.vanoni@sun.com 	 * cyclic driven mode.
991*11066Srafael.vanoni@sun.com 	 */
992*11066Srafael.vanoni@sun.com 	sz = sizeof (lbolt_info_t) + CPU_CACHE_COHERENCE_SIZE;
993*11066Srafael.vanoni@sun.com 	buf = (intptr_t)kmem_zalloc(sz, KM_SLEEP);
994*11066Srafael.vanoni@sun.com 	lb_info = (lbolt_info_t *)P2ROUNDUP(buf, CPU_CACHE_COHERENCE_SIZE);
995*11066Srafael.vanoni@sun.com 
996*11066Srafael.vanoni@sun.com 	if (hz != HZ_DEFAULT)
997*11066Srafael.vanoni@sun.com 		lb_info->lbi_thresh_interval = LBOLT_THRESH_INTERVAL *
998*11066Srafael.vanoni@sun.com 		    hz/HZ_DEFAULT;
999*11066Srafael.vanoni@sun.com 	else
1000*11066Srafael.vanoni@sun.com 		lb_info->lbi_thresh_interval = LBOLT_THRESH_INTERVAL;
1001*11066Srafael.vanoni@sun.com 
1002*11066Srafael.vanoni@sun.com 	lb_info->lbi_thresh_calls = LBOLT_THRESH_CALLS;
1003*11066Srafael.vanoni@sun.com 
1004*11066Srafael.vanoni@sun.com 	sz = (sizeof (lbolt_info_t) * max_ncpus) + CPU_CACHE_COHERENCE_SIZE;
1005*11066Srafael.vanoni@sun.com 	buf = (intptr_t)kmem_zalloc(sz, KM_SLEEP);
1006*11066Srafael.vanoni@sun.com 	lb_cpu = (lbolt_cpu_t *)P2ROUNDUP(buf, CPU_CACHE_COHERENCE_SIZE);
1007*11066Srafael.vanoni@sun.com 
1008*11066Srafael.vanoni@sun.com 	for (i = 0; i < max_ncpus; i++)
1009*11066Srafael.vanoni@sun.com 		lb_cpu[i].lbc_counter = lb_info->lbi_thresh_calls;
1010*11066Srafael.vanoni@sun.com 
1011*11066Srafael.vanoni@sun.com 	lbolt_softint_add();
1012*11066Srafael.vanoni@sun.com 
1013*11066Srafael.vanoni@sun.com 	/*
1014*11066Srafael.vanoni@sun.com 	 * Grab cpu_lock and install all three cyclics.
1015*11066Srafael.vanoni@sun.com 	 */
10165107Seota 	mutex_enter(&cpu_lock);
1017*11066Srafael.vanoni@sun.com 
1018*11066Srafael.vanoni@sun.com 	clock_cyclic = cyclic_add(&clk_hdlr, &clk_when);
1019*11066Srafael.vanoni@sun.com 	ddi_timer_cyclic = cyclic_add(&timer_hdlr, &clk_when);
1020*11066Srafael.vanoni@sun.com 	lb_info->lbi_cyclic_id = cyclic_add(&lbolt_hdlr, &lbolt_when);
1021*11066Srafael.vanoni@sun.com 
10225107Seota 	mutex_exit(&cpu_lock);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate  * Called before calcloadavg to get 10-sec moving loadavg together
10270Sstevel@tonic-gate  */
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate static int
10300Sstevel@tonic-gate genloadavg(struct loadavg_s *avgs)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate 	int avg;
10330Sstevel@tonic-gate 	int spos; /* starting position */
10340Sstevel@tonic-gate 	int cpos; /* moving current position */
10350Sstevel@tonic-gate 	int i;
10360Sstevel@tonic-gate 	int slen;
10370Sstevel@tonic-gate 	hrtime_t hr_avg;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	/* 10-second snapshot, calculate first positon */
10400Sstevel@tonic-gate 	if (avgs->lg_len == 0) {
10410Sstevel@tonic-gate 		return (0);
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 	slen = avgs->lg_len < S_MOVAVG_SZ ? avgs->lg_len : S_MOVAVG_SZ;
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	spos = (avgs->lg_cur - 1) >= 0 ? avgs->lg_cur - 1 :
10460Sstevel@tonic-gate 	    S_LOADAVG_SZ + (avgs->lg_cur - 1);
10470Sstevel@tonic-gate 	for (i = hr_avg = 0; i < slen; i++) {
10480Sstevel@tonic-gate 		cpos = (spos - i) >= 0 ? spos - i : S_LOADAVG_SZ + (spos - i);
10490Sstevel@tonic-gate 		hr_avg += avgs->lg_loads[cpos];
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	hr_avg = hr_avg / slen;
10530Sstevel@tonic-gate 	avg = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	return (avg);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate  * Run every second from clock () to update the loadavg count available to the
10600Sstevel@tonic-gate  * system and cpu-partitions.
10610Sstevel@tonic-gate  *
10620Sstevel@tonic-gate  * This works by sampling the previous usr, sys, wait time elapsed,
10630Sstevel@tonic-gate  * computing a delta, and adding that delta to the elapsed usr, sys,
10640Sstevel@tonic-gate  * wait increase.
10650Sstevel@tonic-gate  */
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate static void
10680Sstevel@tonic-gate loadavg_update()
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	cpu_t *cp;
10710Sstevel@tonic-gate 	cpupart_t *cpupart;
10720Sstevel@tonic-gate 	hrtime_t cpu_total;
10730Sstevel@tonic-gate 	int prev;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	cp = cpu_list;
10760Sstevel@tonic-gate 	loadavg.lg_total = 0;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	/*
10790Sstevel@tonic-gate 	 * first pass totals up per-cpu statistics for system and cpu
10800Sstevel@tonic-gate 	 * partitions
10810Sstevel@tonic-gate 	 */
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	do {
10840Sstevel@tonic-gate 		struct loadavg_s *lavg;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		lavg = &cp->cpu_loadavg;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		cpu_total = cp->cpu_acct[CMS_USER] +
10890Sstevel@tonic-gate 		    cp->cpu_acct[CMS_SYSTEM] + cp->cpu_waitrq;
10900Sstevel@tonic-gate 		/* compute delta against last total */
10910Sstevel@tonic-gate 		scalehrtime(&cpu_total);
10920Sstevel@tonic-gate 		prev = (lavg->lg_cur - 1) >= 0 ? lavg->lg_cur - 1 :
10930Sstevel@tonic-gate 		    S_LOADAVG_SZ + (lavg->lg_cur - 1);
10940Sstevel@tonic-gate 		if (lavg->lg_loads[prev] <= 0) {
10950Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
10960Sstevel@tonic-gate 			cpu_total = 0;
10970Sstevel@tonic-gate 		} else {
10980Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
10990Sstevel@tonic-gate 			cpu_total = cpu_total - lavg->lg_loads[prev];
11000Sstevel@tonic-gate 			if (cpu_total < 0)
11010Sstevel@tonic-gate 				cpu_total = 0;
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11050Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11060Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 		loadavg.lg_total += cpu_total;
11090Sstevel@tonic-gate 		cp->cpu_part->cp_loadavg.lg_total += cpu_total;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	loadavg.lg_loads[loadavg.lg_cur] = loadavg.lg_total;
11140Sstevel@tonic-gate 	loadavg.lg_cur = (loadavg.lg_cur + 1) % S_LOADAVG_SZ;
11150Sstevel@tonic-gate 	loadavg.lg_len = (loadavg.lg_len + 1) < S_LOADAVG_SZ ?
11160Sstevel@tonic-gate 	    loadavg.lg_len + 1 : S_LOADAVG_SZ;
11170Sstevel@tonic-gate 	/*
11180Sstevel@tonic-gate 	 * Second pass updates counts
11190Sstevel@tonic-gate 	 */
11200Sstevel@tonic-gate 	cpupart = cp_list_head;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	do {
11230Sstevel@tonic-gate 		struct loadavg_s *lavg;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 		lavg = &cpupart->cp_loadavg;
11260Sstevel@tonic-gate 		lavg->lg_loads[lavg->lg_cur] = lavg->lg_total;
11270Sstevel@tonic-gate 		lavg->lg_total = 0;
11280Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11290Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11300Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate  * clock_update() - local clock update
11380Sstevel@tonic-gate  *
11390Sstevel@tonic-gate  * This routine is called by ntp_adjtime() to update the local clock
11400Sstevel@tonic-gate  * phase and frequency. The implementation is of an
11410Sstevel@tonic-gate  * adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
11420Sstevel@tonic-gate  * routine computes new time and frequency offset estimates for each
11430Sstevel@tonic-gate  * call.  The PPS signal itself determines the new time offset,
11440Sstevel@tonic-gate  * instead of the calling argument.  Presumably, calls to
11450Sstevel@tonic-gate  * ntp_adjtime() occur only when the caller believes the local clock
11460Sstevel@tonic-gate  * is valid within some bound (+-128 ms with NTP). If the caller's
11470Sstevel@tonic-gate  * time is far different than the PPS time, an argument will ensue,
11480Sstevel@tonic-gate  * and it's not clear who will lose.
11490Sstevel@tonic-gate  *
11500Sstevel@tonic-gate  * For uncompensated quartz crystal oscillatores and nominal update
11510Sstevel@tonic-gate  * intervals less than 1024 s, operation should be in phase-lock mode
11520Sstevel@tonic-gate  * (STA_FLL = 0), where the loop is disciplined to phase. For update
11530Sstevel@tonic-gate  * intervals greater than this, operation should be in frequency-lock
11540Sstevel@tonic-gate  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
11550Sstevel@tonic-gate  *
11560Sstevel@tonic-gate  * Note: mutex(&tod_lock) is in effect.
11570Sstevel@tonic-gate  */
11580Sstevel@tonic-gate void
11590Sstevel@tonic-gate clock_update(int offset)
11600Sstevel@tonic-gate {
11610Sstevel@tonic-gate 	int ltemp, mtemp, s;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
11660Sstevel@tonic-gate 		return;
11670Sstevel@tonic-gate 	ltemp = offset;
11680Sstevel@tonic-gate 	if ((time_status & STA_PPSTIME) && (time_status & STA_PPSSIGNAL))
11690Sstevel@tonic-gate 		ltemp = pps_offset;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * Scale the phase adjustment and clamp to the operating range.
11730Sstevel@tonic-gate 	 */
11740Sstevel@tonic-gate 	if (ltemp > MAXPHASE)
11750Sstevel@tonic-gate 		time_offset = MAXPHASE * SCALE_UPDATE;
11760Sstevel@tonic-gate 	else if (ltemp < -MAXPHASE)
11770Sstevel@tonic-gate 		time_offset = -(MAXPHASE * SCALE_UPDATE);
11780Sstevel@tonic-gate 	else
11790Sstevel@tonic-gate 		time_offset = ltemp * SCALE_UPDATE;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	/*
11820Sstevel@tonic-gate 	 * Select whether the frequency is to be controlled and in which
11830Sstevel@tonic-gate 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
11840Sstevel@tonic-gate 	 * multiply/divide should be replaced someday.
11850Sstevel@tonic-gate 	 */
11860Sstevel@tonic-gate 	if (time_status & STA_FREQHOLD || time_reftime == 0)
11870Sstevel@tonic-gate 		time_reftime = hrestime.tv_sec;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	mtemp = hrestime.tv_sec - time_reftime;
11900Sstevel@tonic-gate 	time_reftime = hrestime.tv_sec;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (time_status & STA_FLL) {
11930Sstevel@tonic-gate 		if (mtemp >= MINSEC) {
11940Sstevel@tonic-gate 			ltemp = ((time_offset / mtemp) * (SCALE_USEC /
11950Sstevel@tonic-gate 			    SCALE_UPDATE));
11960Sstevel@tonic-gate 			if (ltemp)
11970Sstevel@tonic-gate 				time_freq += ltemp / SCALE_KH;
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate 	} else {
12000Sstevel@tonic-gate 		if (mtemp < MAXSEC) {
12010Sstevel@tonic-gate 			ltemp *= mtemp;
12020Sstevel@tonic-gate 			if (ltemp)
12030Sstevel@tonic-gate 				time_freq += (int)(((int64_t)ltemp *
12040Sstevel@tonic-gate 				    SCALE_USEC) / SCALE_KF)
12050Sstevel@tonic-gate 				    / (1 << (time_constant * 2));
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 	if (time_freq > time_tolerance)
12090Sstevel@tonic-gate 		time_freq = time_tolerance;
12100Sstevel@tonic-gate 	else if (time_freq < -time_tolerance)
12110Sstevel@tonic-gate 		time_freq = -time_tolerance;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	s = hr_clock_lock();
12140Sstevel@tonic-gate 	tod_needsync = 1;
12150Sstevel@tonic-gate 	hr_clock_unlock(s);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate  * ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
12200Sstevel@tonic-gate  *
12210Sstevel@tonic-gate  * This routine is called at each PPS interrupt in order to discipline
12220Sstevel@tonic-gate  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
12230Sstevel@tonic-gate  * and leaves it in a handy spot for the clock() routine. It
12240Sstevel@tonic-gate  * integrates successive PPS phase differences and calculates the
12250Sstevel@tonic-gate  * frequency offset. This is used in clock() to discipline the CPU
12260Sstevel@tonic-gate  * clock oscillator so that intrinsic frequency error is cancelled out.
12270Sstevel@tonic-gate  * The code requires the caller to capture the time and hardware counter
12280Sstevel@tonic-gate  * value at the on-time PPS signal transition.
12290Sstevel@tonic-gate  *
12300Sstevel@tonic-gate  * Note that, on some Unix systems, this routine runs at an interrupt
12310Sstevel@tonic-gate  * priority level higher than the timer interrupt routine clock().
12320Sstevel@tonic-gate  * Therefore, the variables used are distinct from the clock()
12330Sstevel@tonic-gate  * variables, except for certain exceptions: The PPS frequency pps_freq
12340Sstevel@tonic-gate  * and phase pps_offset variables are determined by this routine and
12350Sstevel@tonic-gate  * updated atomically. The time_tolerance variable can be considered a
12360Sstevel@tonic-gate  * constant, since it is infrequently changed, and then only when the
12370Sstevel@tonic-gate  * PPS signal is disabled. The watchdog counter pps_valid is updated
12380Sstevel@tonic-gate  * once per second by clock() and is atomically cleared in this
12390Sstevel@tonic-gate  * routine.
12400Sstevel@tonic-gate  *
12410Sstevel@tonic-gate  * tvp is the time of the last tick; usec is a microsecond count since the
12420Sstevel@tonic-gate  * last tick.
12430Sstevel@tonic-gate  *
12440Sstevel@tonic-gate  * Note: In Solaris systems, the tick value is actually given by
12450Sstevel@tonic-gate  *       usec_per_tick.  This is called from the serial driver cdintr(),
12460Sstevel@tonic-gate  *	 or equivalent, at a high PIL.  Because the kernel keeps a
12470Sstevel@tonic-gate  *	 highresolution time, the following code can accept either
12480Sstevel@tonic-gate  *	 the traditional argument pair, or the current highres timestamp
12490Sstevel@tonic-gate  *       in tvp and zero in usec.
12500Sstevel@tonic-gate  */
12510Sstevel@tonic-gate void
12520Sstevel@tonic-gate ddi_hardpps(struct timeval *tvp, int usec)
12530Sstevel@tonic-gate {
12540Sstevel@tonic-gate 	int u_usec, v_usec, bigtick;
12550Sstevel@tonic-gate 	time_t cal_sec;
12560Sstevel@tonic-gate 	int cal_usec;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	/*
12590Sstevel@tonic-gate 	 * An occasional glitch can be produced when the PPS interrupt
12600Sstevel@tonic-gate 	 * occurs in the clock() routine before the time variable is
12610Sstevel@tonic-gate 	 * updated. Here the offset is discarded when the difference
12620Sstevel@tonic-gate 	 * between it and the last one is greater than tick/2, but not
12630Sstevel@tonic-gate 	 * if the interval since the first discard exceeds 30 s.
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	time_status |= STA_PPSSIGNAL;
12660Sstevel@tonic-gate 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
12670Sstevel@tonic-gate 	pps_valid = 0;
12680Sstevel@tonic-gate 	u_usec = -tvp->tv_usec;
12690Sstevel@tonic-gate 	if (u_usec < -(MICROSEC/2))
12700Sstevel@tonic-gate 		u_usec += MICROSEC;
12710Sstevel@tonic-gate 	v_usec = pps_offset - u_usec;
12720Sstevel@tonic-gate 	if (v_usec < 0)
12730Sstevel@tonic-gate 		v_usec = -v_usec;
12740Sstevel@tonic-gate 	if (v_usec > (usec_per_tick >> 1)) {
12750Sstevel@tonic-gate 		if (pps_glitch > MAXGLITCH) {
12760Sstevel@tonic-gate 			pps_glitch = 0;
12770Sstevel@tonic-gate 			pps_tf[2] = u_usec;
12780Sstevel@tonic-gate 			pps_tf[1] = u_usec;
12790Sstevel@tonic-gate 		} else {
12800Sstevel@tonic-gate 			pps_glitch++;
12810Sstevel@tonic-gate 			u_usec = pps_offset;
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 	} else
12840Sstevel@tonic-gate 		pps_glitch = 0;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/*
12870Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
12880Sstevel@tonic-gate 	 * time. The median sample becomes the time offset estimate; the
12890Sstevel@tonic-gate 	 * difference between the other two samples becomes the time
12900Sstevel@tonic-gate 	 * dispersion (jitter) estimate.
12910Sstevel@tonic-gate 	 */
12920Sstevel@tonic-gate 	pps_tf[2] = pps_tf[1];
12930Sstevel@tonic-gate 	pps_tf[1] = pps_tf[0];
12940Sstevel@tonic-gate 	pps_tf[0] = u_usec;
12950Sstevel@tonic-gate 	if (pps_tf[0] > pps_tf[1]) {
12960Sstevel@tonic-gate 		if (pps_tf[1] > pps_tf[2]) {
12970Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 0 1 2 */
12980Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[2];
12990Sstevel@tonic-gate 		} else if (pps_tf[2] > pps_tf[0]) {
13000Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 2 0 1 */
13010Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[1];
13020Sstevel@tonic-gate 		} else {
13030Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 0 2 1 */
13040Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[1];
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	} else {
13070Sstevel@tonic-gate 		if (pps_tf[1] < pps_tf[2]) {
13080Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 2 1 0 */
13090Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[0];
13100Sstevel@tonic-gate 		} else  if (pps_tf[2] < pps_tf[0]) {
13110Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 1 0 2 */
13120Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[2];
13130Sstevel@tonic-gate 		} else {
13140Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 1 2 0 */
13150Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[0];
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 	if (v_usec > MAXTIME)
13190Sstevel@tonic-gate 		pps_jitcnt++;
13200Sstevel@tonic-gate 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
13210Sstevel@tonic-gate 	pps_jitter += v_usec / (1 << PPS_AVG);
13220Sstevel@tonic-gate 	if (pps_jitter > (MAXTIME >> 1))
13230Sstevel@tonic-gate 		time_status |= STA_PPSJITTER;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/*
13260Sstevel@tonic-gate 	 * During the calibration interval adjust the starting time when
13270Sstevel@tonic-gate 	 * the tick overflows. At the end of the interval compute the
13280Sstevel@tonic-gate 	 * duration of the interval and the difference of the hardware
13290Sstevel@tonic-gate 	 * counters at the beginning and end of the interval. This code
13300Sstevel@tonic-gate 	 * is deliciously complicated by the fact valid differences may
13310Sstevel@tonic-gate 	 * exceed the value of tick when using long calibration
13320Sstevel@tonic-gate 	 * intervals and small ticks. Note that the counter can be
13330Sstevel@tonic-gate 	 * greater than tick if caught at just the wrong instant, but
13340Sstevel@tonic-gate 	 * the values returned and used here are correct.
13350Sstevel@tonic-gate 	 */
13360Sstevel@tonic-gate 	bigtick = (int)usec_per_tick * SCALE_USEC;
13370Sstevel@tonic-gate 	pps_usec -= pps_freq;
13380Sstevel@tonic-gate 	if (pps_usec >= bigtick)
13390Sstevel@tonic-gate 		pps_usec -= bigtick;
13400Sstevel@tonic-gate 	if (pps_usec < 0)
13410Sstevel@tonic-gate 		pps_usec += bigtick;
13420Sstevel@tonic-gate 	pps_time.tv_sec++;
13430Sstevel@tonic-gate 	pps_count++;
13440Sstevel@tonic-gate 	if (pps_count < (1 << pps_shift))
13450Sstevel@tonic-gate 		return;
13460Sstevel@tonic-gate 	pps_count = 0;
13470Sstevel@tonic-gate 	pps_calcnt++;
13480Sstevel@tonic-gate 	u_usec = usec * SCALE_USEC;
13490Sstevel@tonic-gate 	v_usec = pps_usec - u_usec;
13500Sstevel@tonic-gate 	if (v_usec >= bigtick >> 1)
13510Sstevel@tonic-gate 		v_usec -= bigtick;
13520Sstevel@tonic-gate 	if (v_usec < -(bigtick >> 1))
13530Sstevel@tonic-gate 		v_usec += bigtick;
13540Sstevel@tonic-gate 	if (v_usec < 0)
13550Sstevel@tonic-gate 		v_usec = -(-v_usec >> pps_shift);
13560Sstevel@tonic-gate 	else
13570Sstevel@tonic-gate 		v_usec = v_usec >> pps_shift;
13580Sstevel@tonic-gate 	pps_usec = u_usec;
13590Sstevel@tonic-gate 	cal_sec = tvp->tv_sec;
13600Sstevel@tonic-gate 	cal_usec = tvp->tv_usec;
13610Sstevel@tonic-gate 	cal_sec -= pps_time.tv_sec;
13620Sstevel@tonic-gate 	cal_usec -= pps_time.tv_usec;
13630Sstevel@tonic-gate 	if (cal_usec < 0) {
13640Sstevel@tonic-gate 		cal_usec += MICROSEC;
13650Sstevel@tonic-gate 		cal_sec--;
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 	pps_time = *tvp;
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	/*
13700Sstevel@tonic-gate 	 * Check for lost interrupts, noise, excessive jitter and
13710Sstevel@tonic-gate 	 * excessive frequency error. The number of timer ticks during
13720Sstevel@tonic-gate 	 * the interval may vary +-1 tick. Add to this a margin of one
13730Sstevel@tonic-gate 	 * tick for the PPS signal jitter and maximum frequency
13740Sstevel@tonic-gate 	 * deviation. If the limits are exceeded, the calibration
13750Sstevel@tonic-gate 	 * interval is reset to the minimum and we start over.
13760Sstevel@tonic-gate 	 */
13770Sstevel@tonic-gate 	u_usec = (int)usec_per_tick << 1;
13780Sstevel@tonic-gate 	if (!((cal_sec == -1 && cal_usec > (MICROSEC - u_usec)) ||
13790Sstevel@tonic-gate 	    (cal_sec == 0 && cal_usec < u_usec)) ||
13800Sstevel@tonic-gate 	    v_usec > time_tolerance || v_usec < -time_tolerance) {
13810Sstevel@tonic-gate 		pps_errcnt++;
13820Sstevel@tonic-gate 		pps_shift = PPS_SHIFT;
13830Sstevel@tonic-gate 		pps_intcnt = 0;
13840Sstevel@tonic-gate 		time_status |= STA_PPSERROR;
13850Sstevel@tonic-gate 		return;
13860Sstevel@tonic-gate 	}
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	/*
13890Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
13900Sstevel@tonic-gate 	 * frequency. The median sample becomes the frequency offset
13910Sstevel@tonic-gate 	 * estimate; the difference between the other two samples
13920Sstevel@tonic-gate 	 * becomes the frequency dispersion (stability) estimate.
13930Sstevel@tonic-gate 	 */
13940Sstevel@tonic-gate 	pps_ff[2] = pps_ff[1];
13950Sstevel@tonic-gate 	pps_ff[1] = pps_ff[0];
13960Sstevel@tonic-gate 	pps_ff[0] = v_usec;
13970Sstevel@tonic-gate 	if (pps_ff[0] > pps_ff[1]) {
13980Sstevel@tonic-gate 		if (pps_ff[1] > pps_ff[2]) {
13990Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 0 1 2 */
14000Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[2];
14010Sstevel@tonic-gate 		} else if (pps_ff[2] > pps_ff[0]) {
14020Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 2 0 1 */
14030Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[1];
14040Sstevel@tonic-gate 		} else {
14050Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 0 2 1 */
14060Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[1];
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 	} else {
14090Sstevel@tonic-gate 		if (pps_ff[1] < pps_ff[2]) {
14100Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 2 1 0 */
14110Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[0];
14120Sstevel@tonic-gate 		} else  if (pps_ff[2] < pps_ff[0]) {
14130Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 1 0 2 */
14140Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[2];
14150Sstevel@tonic-gate 		} else {
14160Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 1 2 0 */
14170Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[0];
14180Sstevel@tonic-gate 		}
14190Sstevel@tonic-gate 	}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	/*
14220Sstevel@tonic-gate 	 * Here the frequency dispersion (stability) is updated. If it
14230Sstevel@tonic-gate 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
14240Sstevel@tonic-gate 	 * offset is updated as well, but clamped to the tolerance. It
14250Sstevel@tonic-gate 	 * will be processed later by the clock() routine.
14260Sstevel@tonic-gate 	 */
14270Sstevel@tonic-gate 	v_usec = (v_usec >> 1) - pps_stabil;
14280Sstevel@tonic-gate 	if (v_usec < 0)
14290Sstevel@tonic-gate 		pps_stabil -= -v_usec >> PPS_AVG;
14300Sstevel@tonic-gate 	else
14310Sstevel@tonic-gate 		pps_stabil += v_usec >> PPS_AVG;
14320Sstevel@tonic-gate 	if (pps_stabil > MAXFREQ >> 2) {
14330Sstevel@tonic-gate 		pps_stbcnt++;
14340Sstevel@tonic-gate 		time_status |= STA_PPSWANDER;
14350Sstevel@tonic-gate 		return;
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 	if (time_status & STA_PPSFREQ) {
14380Sstevel@tonic-gate 		if (u_usec < 0) {
14390Sstevel@tonic-gate 			pps_freq -= -u_usec >> PPS_AVG;
14400Sstevel@tonic-gate 			if (pps_freq < -time_tolerance)
14410Sstevel@tonic-gate 				pps_freq = -time_tolerance;
14420Sstevel@tonic-gate 			u_usec = -u_usec;
14430Sstevel@tonic-gate 		} else {
14440Sstevel@tonic-gate 			pps_freq += u_usec >> PPS_AVG;
14450Sstevel@tonic-gate 			if (pps_freq > time_tolerance)
14460Sstevel@tonic-gate 				pps_freq = time_tolerance;
14470Sstevel@tonic-gate 		}
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	/*
14510Sstevel@tonic-gate 	 * Here the calibration interval is adjusted. If the maximum
14520Sstevel@tonic-gate 	 * time difference is greater than tick / 4, reduce the interval
14530Sstevel@tonic-gate 	 * by half. If this is not the case for four consecutive
14540Sstevel@tonic-gate 	 * intervals, double the interval.
14550Sstevel@tonic-gate 	 */
14560Sstevel@tonic-gate 	if (u_usec << pps_shift > bigtick >> 2) {
14570Sstevel@tonic-gate 		pps_intcnt = 0;
14580Sstevel@tonic-gate 		if (pps_shift > PPS_SHIFT)
14590Sstevel@tonic-gate 			pps_shift--;
14600Sstevel@tonic-gate 	} else if (pps_intcnt >= 4) {
14610Sstevel@tonic-gate 		pps_intcnt = 0;
14620Sstevel@tonic-gate 		if (pps_shift < PPS_SHIFTMAX)
14630Sstevel@tonic-gate 			pps_shift++;
14640Sstevel@tonic-gate 	} else
14650Sstevel@tonic-gate 		pps_intcnt++;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	/*
14680Sstevel@tonic-gate 	 * If recovering from kmdb, then make sure the tod chip gets resynced.
14690Sstevel@tonic-gate 	 * If we took an early exit above, then we don't yet have a stable
14700Sstevel@tonic-gate 	 * calibration signal to lock onto, so don't mark the tod for sync
14710Sstevel@tonic-gate 	 * until we get all the way here.
14720Sstevel@tonic-gate 	 */
14730Sstevel@tonic-gate 	{
14740Sstevel@tonic-gate 		int s = hr_clock_lock();
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		tod_needsync = 1;
14770Sstevel@tonic-gate 		hr_clock_unlock(s);
14780Sstevel@tonic-gate 	}
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate /*
14820Sstevel@tonic-gate  * Handle clock tick processing for a thread.
14830Sstevel@tonic-gate  * Check for timer action, enforce CPU rlimit, do profiling etc.
14840Sstevel@tonic-gate  */
14850Sstevel@tonic-gate void
14865788Smv143129 clock_tick(kthread_t *t, int pending)
14870Sstevel@tonic-gate {
14880Sstevel@tonic-gate 	struct proc *pp;
14890Sstevel@tonic-gate 	klwp_id_t    lwp;
14900Sstevel@tonic-gate 	struct as *as;
14915788Smv143129 	clock_t	ticks;
14920Sstevel@tonic-gate 	int	poke = 0;		/* notify another CPU */
14930Sstevel@tonic-gate 	int	user_mode;
14940Sstevel@tonic-gate 	size_t	 rss;
14955788Smv143129 	int i, total_usec, usec;
14965788Smv143129 	rctl_qty_t secs;
14975788Smv143129 
14985788Smv143129 	ASSERT(pending > 0);
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	/* Must be operating on a lwp/thread */
15010Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL) {
15020Sstevel@tonic-gate 		panic("clock_tick: no lwp");
15030Sstevel@tonic-gate 		/*NOTREACHED*/
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate 
15065788Smv143129 	for (i = 0; i < pending; i++) {
15075788Smv143129 		CL_TICK(t);	/* Class specific tick processing */
15085788Smv143129 		DTRACE_SCHED1(tick, kthread_t *, t);
15095788Smv143129 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	pp = ttoproc(t);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	/* pp->p_lock makes sure that the thread does not exit */
15140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	user_mode = (lwp->lwp_state == LWP_USER);
15170Sstevel@tonic-gate 
15185788Smv143129 	ticks = (pp->p_utime + pp->p_stime) % hz;
15190Sstevel@tonic-gate 	/*
15200Sstevel@tonic-gate 	 * Update process times. Should use high res clock and state
15210Sstevel@tonic-gate 	 * changes instead of statistical sampling method. XXX
15220Sstevel@tonic-gate 	 */
15230Sstevel@tonic-gate 	if (user_mode) {
15245788Smv143129 		pp->p_utime += pending;
15250Sstevel@tonic-gate 	} else {
15265788Smv143129 		pp->p_stime += pending;
15270Sstevel@tonic-gate 	}
15285788Smv143129 
15295788Smv143129 	pp->p_ttime += pending;
15300Sstevel@tonic-gate 	as = pp->p_as;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * Update user profiling statistics. Get the pc from the
15340Sstevel@tonic-gate 	 * lwp when the AST happens.
15350Sstevel@tonic-gate 	 */
15360Sstevel@tonic-gate 	if (pp->p_prof.pr_scale) {
15375788Smv143129 		atomic_add_32(&lwp->lwp_oweupc, (int32_t)pending);
15380Sstevel@tonic-gate 		if (user_mode) {
15390Sstevel@tonic-gate 			poke = 1;
15400Sstevel@tonic-gate 			aston(t);
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 
15445788Smv143129 	/*
15455788Smv143129 	 * If CPU was in user state, process lwp-virtual time
15465788Smv143129 	 * interval timer. The value passed to itimerdecr() has to be
15475788Smv143129 	 * in microseconds and has to be less than one second. Hence
15485788Smv143129 	 * this loop.
15495788Smv143129 	 */
15505788Smv143129 	total_usec = usec_per_tick * pending;
15515788Smv143129 	while (total_usec > 0) {
15525788Smv143129 		usec = MIN(total_usec, (MICROSEC - 1));
15535788Smv143129 		if (user_mode &&
15545788Smv143129 		    timerisset(&lwp->lwp_timer[ITIMER_VIRTUAL].it_value) &&
15555788Smv143129 		    itimerdecr(&lwp->lwp_timer[ITIMER_VIRTUAL], usec) == 0) {
15565788Smv143129 			poke = 1;
15575788Smv143129 			sigtoproc(pp, t, SIGVTALRM);
15585788Smv143129 		}
15595788Smv143129 		total_usec -= usec;
15605788Smv143129 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/*
15635788Smv143129 	 * If CPU was in user state, process lwp-profile
15640Sstevel@tonic-gate 	 * interval timer.
15650Sstevel@tonic-gate 	 */
15665788Smv143129 	total_usec = usec_per_tick * pending;
15675788Smv143129 	while (total_usec > 0) {
15685788Smv143129 		usec = MIN(total_usec, (MICROSEC - 1));
15695788Smv143129 		if (timerisset(&lwp->lwp_timer[ITIMER_PROF].it_value) &&
15705788Smv143129 		    itimerdecr(&lwp->lwp_timer[ITIMER_PROF], usec) == 0) {
15715788Smv143129 			poke = 1;
15725788Smv143129 			sigtoproc(pp, t, SIGPROF);
15735788Smv143129 		}
15745788Smv143129 		total_usec -= usec;
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/*
15780Sstevel@tonic-gate 	 * Enforce CPU resource controls:
15790Sstevel@tonic-gate 	 *   (a) process.max-cpu-time resource control
15805788Smv143129 	 *
15815788Smv143129 	 * Perform the check only if we have accumulated more a second.
15820Sstevel@tonic-gate 	 */
15835788Smv143129 	if ((ticks + pending) >= hz) {
15845788Smv143129 		(void) rctl_test(rctlproc_legacy[RLIMIT_CPU], pp->p_rctls, pp,
15855788Smv143129 		    (pp->p_utime + pp->p_stime)/hz, RCA_UNSAFE_SIGINFO);
15865788Smv143129 	}
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	/*
15890Sstevel@tonic-gate 	 *   (b) task.max-cpu-time resource control
15905788Smv143129 	 *
15915788Smv143129 	 * If we have accumulated enough ticks, increment the task CPU
15925788Smv143129 	 * time usage and test for the resource limit. This minimizes the
15935788Smv143129 	 * number of calls to the rct_test(). The task CPU time mutex
15945788Smv143129 	 * is highly contentious as many processes can be sharing a task.
15950Sstevel@tonic-gate 	 */
15965788Smv143129 	if (pp->p_ttime >= clock_tick_proc_max) {
15975788Smv143129 		secs = task_cpu_time_incr(pp->p_task, pp->p_ttime);
15985788Smv143129 		pp->p_ttime = 0;
15995788Smv143129 		if (secs) {
16005788Smv143129 			(void) rctl_test(rc_task_cpu_time, pp->p_task->tk_rctls,
16015788Smv143129 			    pp, secs, RCA_UNSAFE_SIGINFO);
16025788Smv143129 		}
16035788Smv143129 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	/*
16060Sstevel@tonic-gate 	 * Update memory usage for the currently running process.
16070Sstevel@tonic-gate 	 */
16080Sstevel@tonic-gate 	rss = rm_asrss(as);
16090Sstevel@tonic-gate 	PTOU(pp)->u_mem += rss;
16100Sstevel@tonic-gate 	if (rss > PTOU(pp)->u_mem_max)
16110Sstevel@tonic-gate 		PTOU(pp)->u_mem_max = rss;
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	/*
16140Sstevel@tonic-gate 	 * Notify the CPU the thread is running on.
16150Sstevel@tonic-gate 	 */
16160Sstevel@tonic-gate 	if (poke && t->t_cpu != CPU)
16170Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate void
16210Sstevel@tonic-gate profil_tick(uintptr_t upc)
16220Sstevel@tonic-gate {
16230Sstevel@tonic-gate 	int ticks;
16240Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
16250Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
16260Sstevel@tonic-gate 	struct prof *pr = &p->p_prof;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	do {
16290Sstevel@tonic-gate 		ticks = lwp->lwp_oweupc;
16300Sstevel@tonic-gate 	} while (cas32(&lwp->lwp_oweupc, ticks, 0) != ticks);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
16330Sstevel@tonic-gate 	if (pr->pr_scale >= 2 && upc >= pr->pr_off) {
16340Sstevel@tonic-gate 		/*
16350Sstevel@tonic-gate 		 * Old-style profiling
16360Sstevel@tonic-gate 		 */
16370Sstevel@tonic-gate 		uint16_t *slot = pr->pr_base;
16380Sstevel@tonic-gate 		uint16_t old, new;
16390Sstevel@tonic-gate 		if (pr->pr_scale != 2) {
16400Sstevel@tonic-gate 			uintptr_t delta = upc - pr->pr_off;
16410Sstevel@tonic-gate 			uintptr_t byteoff = ((delta >> 16) * pr->pr_scale) +
16420Sstevel@tonic-gate 			    (((delta & 0xffff) * pr->pr_scale) >> 16);
16430Sstevel@tonic-gate 			if (byteoff >= (uintptr_t)pr->pr_size) {
16440Sstevel@tonic-gate 				mutex_exit(&p->p_pflock);
16450Sstevel@tonic-gate 				return;
16460Sstevel@tonic-gate 			}
16470Sstevel@tonic-gate 			slot += byteoff / sizeof (uint16_t);
16480Sstevel@tonic-gate 		}
16490Sstevel@tonic-gate 		if (fuword16(slot, &old) < 0 ||
16500Sstevel@tonic-gate 		    (new = old + ticks) > SHRT_MAX ||
16510Sstevel@tonic-gate 		    suword16(slot, new) < 0) {
16520Sstevel@tonic-gate 			pr->pr_scale = 0;
16530Sstevel@tonic-gate 		}
16540Sstevel@tonic-gate 	} else if (pr->pr_scale == 1) {
16550Sstevel@tonic-gate 		/*
16560Sstevel@tonic-gate 		 * PC Sampling
16570Sstevel@tonic-gate 		 */
16580Sstevel@tonic-gate 		model_t model = lwp_getdatamodel(lwp);
16590Sstevel@tonic-gate 		int result;
16600Sstevel@tonic-gate #ifdef __lint
16610Sstevel@tonic-gate 		model = model;
16620Sstevel@tonic-gate #endif
16630Sstevel@tonic-gate 		while (ticks-- > 0) {
16640Sstevel@tonic-gate 			if (pr->pr_samples == pr->pr_size) {
16650Sstevel@tonic-gate 				/* buffer full, turn off sampling */
16660Sstevel@tonic-gate 				pr->pr_scale = 0;
16670Sstevel@tonic-gate 				break;
16680Sstevel@tonic-gate 			}
16690Sstevel@tonic-gate 			switch (SIZEOF_PTR(model)) {
16700Sstevel@tonic-gate 			case sizeof (uint32_t):
16710Sstevel@tonic-gate 				result = suword32(pr->pr_base, (uint32_t)upc);
16720Sstevel@tonic-gate 				break;
16730Sstevel@tonic-gate #ifdef _LP64
16740Sstevel@tonic-gate 			case sizeof (uint64_t):
16750Sstevel@tonic-gate 				result = suword64(pr->pr_base, (uint64_t)upc);
16760Sstevel@tonic-gate 				break;
16770Sstevel@tonic-gate #endif
16780Sstevel@tonic-gate 			default:
16790Sstevel@tonic-gate 				cmn_err(CE_WARN, "profil_tick: unexpected "
16800Sstevel@tonic-gate 				    "data model");
16810Sstevel@tonic-gate 				result = -1;
16820Sstevel@tonic-gate 				break;
16830Sstevel@tonic-gate 			}
16840Sstevel@tonic-gate 			if (result != 0) {
16850Sstevel@tonic-gate 				pr->pr_scale = 0;
16860Sstevel@tonic-gate 				break;
16870Sstevel@tonic-gate 			}
16880Sstevel@tonic-gate 			pr->pr_base = (caddr_t)pr->pr_base + SIZEOF_PTR(model);
16890Sstevel@tonic-gate 			pr->pr_samples++;
16900Sstevel@tonic-gate 		}
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate static void
16960Sstevel@tonic-gate delay_wakeup(void *arg)
16970Sstevel@tonic-gate {
169810696SDavid.Hollister@Sun.COM 	kthread_t	*t = arg;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
17010Sstevel@tonic-gate 	cv_signal(&t->t_delay_cv);
17020Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
170510696SDavid.Hollister@Sun.COM /*
170610696SDavid.Hollister@Sun.COM  * The delay(9F) man page indicates that it can only be called from user or
170710696SDavid.Hollister@Sun.COM  * kernel context - detect and diagnose bad calls. The following macro will
170810696SDavid.Hollister@Sun.COM  * produce a limited number of messages identifying bad callers.  This is done
170910696SDavid.Hollister@Sun.COM  * in a macro so that caller() is meaningful. When a bad caller is identified,
171010696SDavid.Hollister@Sun.COM  * switching to 'drv_usecwait(TICK_TO_USEC(ticks));' may be appropriate.
171110696SDavid.Hollister@Sun.COM  */
171210696SDavid.Hollister@Sun.COM #define	DELAY_CONTEXT_CHECK()	{					\
171310696SDavid.Hollister@Sun.COM 	uint32_t	m;						\
171410696SDavid.Hollister@Sun.COM 	char		*f;						\
171510696SDavid.Hollister@Sun.COM 	ulong_t		off;						\
171610696SDavid.Hollister@Sun.COM 									\
171710696SDavid.Hollister@Sun.COM 	m = delay_from_interrupt_msg;					\
171810696SDavid.Hollister@Sun.COM 	if (delay_from_interrupt_diagnose && servicing_interrupt() &&	\
171910696SDavid.Hollister@Sun.COM 	    !panicstr && !devinfo_freeze &&				\
172010696SDavid.Hollister@Sun.COM 	    atomic_cas_32(&delay_from_interrupt_msg, m ? m : 1, m-1)) {	\
172110696SDavid.Hollister@Sun.COM 		f = modgetsymname((uintptr_t)caller(), &off);		\
172210696SDavid.Hollister@Sun.COM 		cmn_err(CE_WARN, "delay(9F) called from "		\
172310696SDavid.Hollister@Sun.COM 		    "interrupt context: %s`%s",				\
172410696SDavid.Hollister@Sun.COM 		    mod_containing_pc(caller()), f ? f : "...");	\
172510696SDavid.Hollister@Sun.COM 	}								\
172610696SDavid.Hollister@Sun.COM }
172710696SDavid.Hollister@Sun.COM 
172810696SDavid.Hollister@Sun.COM /*
172910696SDavid.Hollister@Sun.COM  * delay_common: common delay code.
173010696SDavid.Hollister@Sun.COM  */
173110696SDavid.Hollister@Sun.COM static void
173210696SDavid.Hollister@Sun.COM delay_common(clock_t ticks)
173310696SDavid.Hollister@Sun.COM {
173410696SDavid.Hollister@Sun.COM 	kthread_t	*t = curthread;
173510696SDavid.Hollister@Sun.COM 	clock_t		deadline;
173610696SDavid.Hollister@Sun.COM 	clock_t		timeleft;
173710696SDavid.Hollister@Sun.COM 	callout_id_t	id;
173810696SDavid.Hollister@Sun.COM 
173910696SDavid.Hollister@Sun.COM 	/* If timeouts aren't running all we can do is spin. */
174010696SDavid.Hollister@Sun.COM 	if (panicstr || devinfo_freeze) {
174110696SDavid.Hollister@Sun.COM 		/* Convert delay(9F) call into drv_usecwait(9F) call. */
174210696SDavid.Hollister@Sun.COM 		if (ticks > 0)
174310696SDavid.Hollister@Sun.COM 			drv_usecwait(TICK_TO_USEC(ticks));
174410696SDavid.Hollister@Sun.COM 		return;
174510696SDavid.Hollister@Sun.COM 	}
174610696SDavid.Hollister@Sun.COM 
1747*11066Srafael.vanoni@sun.com 	deadline = ddi_get_lbolt() + ticks;
1748*11066Srafael.vanoni@sun.com 	while ((timeleft = deadline - ddi_get_lbolt()) > 0) {
174910696SDavid.Hollister@Sun.COM 		mutex_enter(&t->t_delay_lock);
175010696SDavid.Hollister@Sun.COM 		id = timeout_default(delay_wakeup, t, timeleft);
175110696SDavid.Hollister@Sun.COM 		cv_wait(&t->t_delay_cv, &t->t_delay_lock);
175210696SDavid.Hollister@Sun.COM 		mutex_exit(&t->t_delay_lock);
175310696SDavid.Hollister@Sun.COM 		(void) untimeout_default(id, 0);
175410696SDavid.Hollister@Sun.COM 	}
175510696SDavid.Hollister@Sun.COM }
175610696SDavid.Hollister@Sun.COM 
175710696SDavid.Hollister@Sun.COM /*
175810696SDavid.Hollister@Sun.COM  * Delay specified number of clock ticks.
175910696SDavid.Hollister@Sun.COM  */
17600Sstevel@tonic-gate void
17610Sstevel@tonic-gate delay(clock_t ticks)
17620Sstevel@tonic-gate {
176310696SDavid.Hollister@Sun.COM 	DELAY_CONTEXT_CHECK();
176410696SDavid.Hollister@Sun.COM 
176510696SDavid.Hollister@Sun.COM 	delay_common(ticks);
176610696SDavid.Hollister@Sun.COM }
17670Sstevel@tonic-gate 
176810696SDavid.Hollister@Sun.COM /*
176910696SDavid.Hollister@Sun.COM  * Delay a random number of clock ticks between 1 and ticks.
177010696SDavid.Hollister@Sun.COM  */
177110696SDavid.Hollister@Sun.COM void
177210696SDavid.Hollister@Sun.COM delay_random(clock_t ticks)
177310696SDavid.Hollister@Sun.COM {
177410696SDavid.Hollister@Sun.COM 	int	r;
17750Sstevel@tonic-gate 
177610696SDavid.Hollister@Sun.COM 	DELAY_CONTEXT_CHECK();
177710696SDavid.Hollister@Sun.COM 
177810696SDavid.Hollister@Sun.COM 	(void) random_get_pseudo_bytes((void *)&r, sizeof (r));
177910696SDavid.Hollister@Sun.COM 	if (ticks == 0)
178010696SDavid.Hollister@Sun.COM 		ticks = 1;
178110696SDavid.Hollister@Sun.COM 	ticks = (r % ticks) + 1;
178210696SDavid.Hollister@Sun.COM 	delay_common(ticks);
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate /*
17860Sstevel@tonic-gate  * Like delay, but interruptible by a signal.
17870Sstevel@tonic-gate  */
17880Sstevel@tonic-gate int
17890Sstevel@tonic-gate delay_sig(clock_t ticks)
17900Sstevel@tonic-gate {
179110696SDavid.Hollister@Sun.COM 	kthread_t	*t = curthread;
179210696SDavid.Hollister@Sun.COM 	clock_t		deadline;
179310696SDavid.Hollister@Sun.COM 	clock_t		rc;
17940Sstevel@tonic-gate 
179510696SDavid.Hollister@Sun.COM 	/* If timeouts aren't running all we can do is spin. */
179610696SDavid.Hollister@Sun.COM 	if (panicstr || devinfo_freeze) {
179710696SDavid.Hollister@Sun.COM 		if (ticks > 0)
179810696SDavid.Hollister@Sun.COM 			drv_usecwait(TICK_TO_USEC(ticks));
179910696SDavid.Hollister@Sun.COM 		return (0);
180010696SDavid.Hollister@Sun.COM 	}
180110696SDavid.Hollister@Sun.COM 
1802*11066Srafael.vanoni@sun.com 	deadline = ddi_get_lbolt() + ticks;
180310696SDavid.Hollister@Sun.COM 	mutex_enter(&t->t_delay_lock);
18040Sstevel@tonic-gate 	do {
180510696SDavid.Hollister@Sun.COM 		rc = cv_timedwait_sig(&t->t_delay_cv,
180610696SDavid.Hollister@Sun.COM 		    &t->t_delay_lock, deadline);
180710696SDavid.Hollister@Sun.COM 		/* loop until past deadline or signaled */
18080Sstevel@tonic-gate 	} while (rc > 0);
180910696SDavid.Hollister@Sun.COM 	mutex_exit(&t->t_delay_lock);
18100Sstevel@tonic-gate 	if (rc == 0)
18110Sstevel@tonic-gate 		return (EINTR);
18120Sstevel@tonic-gate 	return (0);
18130Sstevel@tonic-gate }
18140Sstevel@tonic-gate 
181510696SDavid.Hollister@Sun.COM 
18160Sstevel@tonic-gate #define	SECONDS_PER_DAY 86400
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate  * Initialize the system time based on the TOD chip.  approx is used as
18200Sstevel@tonic-gate  * an approximation of time (e.g. from the filesystem) in the event that
18210Sstevel@tonic-gate  * the TOD chip has been cleared or is unresponsive.  An approx of -1
18220Sstevel@tonic-gate  * means the filesystem doesn't keep time.
18230Sstevel@tonic-gate  */
18240Sstevel@tonic-gate void
18250Sstevel@tonic-gate clkset(time_t approx)
18260Sstevel@tonic-gate {
18270Sstevel@tonic-gate 	timestruc_t ts;
18280Sstevel@tonic-gate 	int spl;
18290Sstevel@tonic-gate 	int set_clock = 0;
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	mutex_enter(&tod_lock);
18320Sstevel@tonic-gate 	ts = tod_get();
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	if (ts.tv_sec > 365 * SECONDS_PER_DAY) {
18350Sstevel@tonic-gate 		/*
18360Sstevel@tonic-gate 		 * If the TOD chip is reporting some time after 1971,
18370Sstevel@tonic-gate 		 * then it probably didn't lose power or become otherwise
18380Sstevel@tonic-gate 		 * cleared in the recent past;  check to assure that
18390Sstevel@tonic-gate 		 * the time coming from the filesystem isn't in the future
18400Sstevel@tonic-gate 		 * according to the TOD chip.
18410Sstevel@tonic-gate 		 */
18420Sstevel@tonic-gate 		if (approx != -1 && approx > ts.tv_sec) {
18430Sstevel@tonic-gate 			cmn_err(CE_WARN, "Last shutdown is later "
18440Sstevel@tonic-gate 			    "than time on time-of-day chip; check date.");
18450Sstevel@tonic-gate 		}
18460Sstevel@tonic-gate 	} else {
18470Sstevel@tonic-gate 		/*
18489158SKrishnendu.Sadhukhan@Sun.COM 		 * If the TOD chip isn't giving correct time, set it to the
18499158SKrishnendu.Sadhukhan@Sun.COM 		 * greater of i) approx and ii) 1987. That way if approx
18509158SKrishnendu.Sadhukhan@Sun.COM 		 * is negative or is earlier than 1987, we set the clock
18519158SKrishnendu.Sadhukhan@Sun.COM 		 * back to a time when Oliver North, ALF and Dire Straits
18529158SKrishnendu.Sadhukhan@Sun.COM 		 * were all on the collective brain:  1987.
18530Sstevel@tonic-gate 		 */
18540Sstevel@tonic-gate 		timestruc_t tmp;
18559158SKrishnendu.Sadhukhan@Sun.COM 		time_t diagnose_date = (1987 - 1970) * 365 * SECONDS_PER_DAY;
18569158SKrishnendu.Sadhukhan@Sun.COM 		ts.tv_sec = (approx > diagnose_date ? approx : diagnose_date);
18570Sstevel@tonic-gate 		ts.tv_nsec = 0;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		/*
18600Sstevel@tonic-gate 		 * Attempt to write the new time to the TOD chip.  Set spl high
18610Sstevel@tonic-gate 		 * to avoid getting preempted between the tod_set and tod_get.
18620Sstevel@tonic-gate 		 */
18630Sstevel@tonic-gate 		spl = splhi();
18640Sstevel@tonic-gate 		tod_set(ts);
18650Sstevel@tonic-gate 		tmp = tod_get();
18660Sstevel@tonic-gate 		splx(spl);
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		if (tmp.tv_sec != ts.tv_sec && tmp.tv_sec != ts.tv_sec + 1) {
18690Sstevel@tonic-gate 			tod_broken = 1;
18700Sstevel@tonic-gate 			dosynctodr = 0;
18719158SKrishnendu.Sadhukhan@Sun.COM 			cmn_err(CE_WARN, "Time-of-day chip unresponsive.");
18720Sstevel@tonic-gate 		} else {
18730Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip had "
18740Sstevel@tonic-gate 			    "incorrect date; check and reset.");
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 		set_clock = 1;
18770Sstevel@tonic-gate 	}
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	if (!boot_time) {
18800Sstevel@tonic-gate 		boot_time = ts.tv_sec;
18810Sstevel@tonic-gate 		set_clock = 1;
18820Sstevel@tonic-gate 	}
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 	if (set_clock)
18850Sstevel@tonic-gate 		set_hrestime(&ts);
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	mutex_exit(&tod_lock);
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate 
18904123Sdm120769 int	timechanged;	/* for testing if the system time has been reset */
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate void
18930Sstevel@tonic-gate set_hrestime(timestruc_t *ts)
18940Sstevel@tonic-gate {
18950Sstevel@tonic-gate 	int spl = hr_clock_lock();
18960Sstevel@tonic-gate 	hrestime = *ts;
18974123Sdm120769 	membar_enter();	/* hrestime must be visible before timechanged++ */
18980Sstevel@tonic-gate 	timedelta = 0;
18994123Sdm120769 	timechanged++;
19000Sstevel@tonic-gate 	hr_clock_unlock(spl);
19018048SMadhavan.Venkataraman@Sun.COM 	callout_hrestime();
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate static uint_t deadman_seconds;
19050Sstevel@tonic-gate static uint32_t deadman_panics;
19060Sstevel@tonic-gate static int deadman_enabled = 0;
19070Sstevel@tonic-gate static int deadman_panic_timers = 1;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate static void
19100Sstevel@tonic-gate deadman(void)
19110Sstevel@tonic-gate {
19120Sstevel@tonic-gate 	if (panicstr) {
19130Sstevel@tonic-gate 		/*
19140Sstevel@tonic-gate 		 * During panic, other CPUs besides the panic
19150Sstevel@tonic-gate 		 * master continue to handle cyclics and some other
19160Sstevel@tonic-gate 		 * interrupts.  The code below is intended to be
19170Sstevel@tonic-gate 		 * single threaded, so any CPU other than the master
19180Sstevel@tonic-gate 		 * must keep out.
19190Sstevel@tonic-gate 		 */
19200Sstevel@tonic-gate 		if (CPU->cpu_id != panic_cpu.cpu_id)
19210Sstevel@tonic-gate 			return;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 		if (!deadman_panic_timers)
19240Sstevel@tonic-gate 			return; /* allow all timers to be manually disabled */
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 		/*
19270Sstevel@tonic-gate 		 * If we are generating a crash dump or syncing filesystems and
19280Sstevel@tonic-gate 		 * the corresponding timer is set, decrement it and re-enter
19290Sstevel@tonic-gate 		 * the panic code to abort it and advance to the next state.
19300Sstevel@tonic-gate 		 * The panic states and triggers are explained in panic.c.
19310Sstevel@tonic-gate 		 */
19320Sstevel@tonic-gate 		if (panic_dump) {
19330Sstevel@tonic-gate 			if (dump_timeleft && (--dump_timeleft == 0)) {
19340Sstevel@tonic-gate 				panic("panic dump timeout");
19350Sstevel@tonic-gate 				/*NOTREACHED*/
19360Sstevel@tonic-gate 			}
19370Sstevel@tonic-gate 		} else if (panic_sync) {
19380Sstevel@tonic-gate 			if (sync_timeleft && (--sync_timeleft == 0)) {
19390Sstevel@tonic-gate 				panic("panic sync timeout");
19400Sstevel@tonic-gate 				/*NOTREACHED*/
19410Sstevel@tonic-gate 			}
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 		return;
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 
1947*11066Srafael.vanoni@sun.com 	if (deadman_counter != CPU->cpu_deadman_counter) {
1948*11066Srafael.vanoni@sun.com 		CPU->cpu_deadman_counter = deadman_counter;
19490Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
19500Sstevel@tonic-gate 		return;
19510Sstevel@tonic-gate 	}
19520Sstevel@tonic-gate 
19536054Svb160487 	if (--CPU->cpu_deadman_countdown > 0)
19540Sstevel@tonic-gate 		return;
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	/*
19570Sstevel@tonic-gate 	 * Regardless of whether or not we actually bring the system down,
19580Sstevel@tonic-gate 	 * bump the deadman_panics variable.
19590Sstevel@tonic-gate 	 *
19600Sstevel@tonic-gate 	 * N.B. deadman_panics is incremented once for each CPU that
19610Sstevel@tonic-gate 	 * passes through here.  It's expected that all the CPUs will
19620Sstevel@tonic-gate 	 * detect this condition within one second of each other, so
19630Sstevel@tonic-gate 	 * when deadman_enabled is off, deadman_panics will
19640Sstevel@tonic-gate 	 * typically be a multiple of the total number of CPUs in
19650Sstevel@tonic-gate 	 * the system.
19660Sstevel@tonic-gate 	 */
19670Sstevel@tonic-gate 	atomic_add_32(&deadman_panics, 1);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	if (!deadman_enabled) {
19700Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
19710Sstevel@tonic-gate 		return;
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	/*
19750Sstevel@tonic-gate 	 * If we're here, we want to bring the system down.
19760Sstevel@tonic-gate 	 */
19770Sstevel@tonic-gate 	panic("deadman: timed out after %d seconds of clock "
19780Sstevel@tonic-gate 	    "inactivity", deadman_seconds);
19790Sstevel@tonic-gate 	/*NOTREACHED*/
19800Sstevel@tonic-gate }
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate /*ARGSUSED*/
19830Sstevel@tonic-gate static void
19840Sstevel@tonic-gate deadman_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
19850Sstevel@tonic-gate {
1986*11066Srafael.vanoni@sun.com 	cpu->cpu_deadman_counter = 0;
19870Sstevel@tonic-gate 	cpu->cpu_deadman_countdown = deadman_seconds;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	hdlr->cyh_func = (cyc_func_t)deadman;
19900Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
19910Sstevel@tonic-gate 	hdlr->cyh_arg = NULL;
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	/*
19940Sstevel@tonic-gate 	 * Stagger the CPUs so that they don't all run deadman() at
19950Sstevel@tonic-gate 	 * the same time.  Simplest reason to do this is to make it
19960Sstevel@tonic-gate 	 * more likely that only one CPU will panic in case of a
19970Sstevel@tonic-gate 	 * timeout.  This is (strictly speaking) an aesthetic, not a
19980Sstevel@tonic-gate 	 * technical consideration.
19990Sstevel@tonic-gate 	 */
20000Sstevel@tonic-gate 	when->cyt_when = cpu->cpu_id * (NANOSEC / NCPU);
20010Sstevel@tonic-gate 	when->cyt_interval = NANOSEC;
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate void
20060Sstevel@tonic-gate deadman_init(void)
20070Sstevel@tonic-gate {
20080Sstevel@tonic-gate 	cyc_omni_handler_t hdlr;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	if (deadman_seconds == 0)
20110Sstevel@tonic-gate 		deadman_seconds = snoop_interval / MICROSEC;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	if (snooping)
20140Sstevel@tonic-gate 		deadman_enabled = 1;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	hdlr.cyo_online = deadman_online;
20170Sstevel@tonic-gate 	hdlr.cyo_offline = NULL;
20180Sstevel@tonic-gate 	hdlr.cyo_arg = NULL;
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
20210Sstevel@tonic-gate 	deadman_cyclic = cyclic_add_omni(&hdlr);
20220Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate /*
20260Sstevel@tonic-gate  * tod_fault() is for updating tod validate mechanism state:
20270Sstevel@tonic-gate  * (1) TOD_NOFAULT: for resetting the state to 'normal'.
20280Sstevel@tonic-gate  *     currently used for debugging only
20290Sstevel@tonic-gate  * (2) The following four cases detected by tod validate mechanism:
20300Sstevel@tonic-gate  *       TOD_REVERSED: current tod value is less than previous value.
20310Sstevel@tonic-gate  *       TOD_STALLED: current tod value hasn't advanced.
20320Sstevel@tonic-gate  *       TOD_JUMPED: current tod value advanced too far from previous value.
20330Sstevel@tonic-gate  *       TOD_RATECHANGED: the ratio between average tod delta and
20340Sstevel@tonic-gate  *       average tick delta has changed.
20355084Sjohnlev  * (3) TOD_RDONLY: when the TOD clock is not writeable e.g. because it is
20365084Sjohnlev  *     a virtual TOD provided by a hypervisor.
20370Sstevel@tonic-gate  */
20380Sstevel@tonic-gate enum tod_fault_type
20390Sstevel@tonic-gate tod_fault(enum tod_fault_type ftype, int off)
20400Sstevel@tonic-gate {
20410Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	if (tod_faulted != ftype) {
20440Sstevel@tonic-gate 		switch (ftype) {
20450Sstevel@tonic-gate 		case TOD_NOFAULT:
204678Sae112802 			plat_tod_fault(TOD_NOFAULT);
20470Sstevel@tonic-gate 			cmn_err(CE_NOTE, "Restarted tracking "
20485076Smishra 			    "Time of Day clock.");
20490Sstevel@tonic-gate 			tod_faulted = ftype;
20500Sstevel@tonic-gate 			break;
20510Sstevel@tonic-gate 		case TOD_REVERSED:
20520Sstevel@tonic-gate 		case TOD_JUMPED:
20530Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
205478Sae112802 				plat_tod_fault(ftype);
20550Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
20560Sstevel@tonic-gate 				    "reason [%s by 0x%x]. -- "
20570Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
20580Sstevel@tonic-gate 				    tod_fault_table[ftype], off);
20590Sstevel@tonic-gate 				tod_faulted = ftype;
20600Sstevel@tonic-gate 			}
20610Sstevel@tonic-gate 			break;
20620Sstevel@tonic-gate 		case TOD_STALLED:
20630Sstevel@tonic-gate 		case TOD_RATECHANGED:
20640Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
206578Sae112802 				plat_tod_fault(ftype);
20660Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
20670Sstevel@tonic-gate 				    "reason [%s]. -- "
20680Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
20690Sstevel@tonic-gate 				    tod_fault_table[ftype]);
20700Sstevel@tonic-gate 				tod_faulted = ftype;
20710Sstevel@tonic-gate 			}
20720Sstevel@tonic-gate 			break;
20735084Sjohnlev 		case TOD_RDONLY:
20745084Sjohnlev 			if (tod_faulted == TOD_NOFAULT) {
20755084Sjohnlev 				plat_tod_fault(ftype);
20765084Sjohnlev 				cmn_err(CE_NOTE, "!Time of Day clock is "
20775084Sjohnlev 				    "Read-Only; set of Date/Time will not "
20785084Sjohnlev 				    "persist across reboot.");
20795084Sjohnlev 				tod_faulted = ftype;
20805084Sjohnlev 			}
20815084Sjohnlev 			break;
20820Sstevel@tonic-gate 		default:
20830Sstevel@tonic-gate 			break;
20840Sstevel@tonic-gate 		}
20850Sstevel@tonic-gate 	}
20860Sstevel@tonic-gate 	return (tod_faulted);
20870Sstevel@tonic-gate }
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate void
20900Sstevel@tonic-gate tod_fault_reset()
20910Sstevel@tonic-gate {
20920Sstevel@tonic-gate 	tod_fault_reset_flag = 1;
20930Sstevel@tonic-gate }
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate /*
20970Sstevel@tonic-gate  * tod_validate() is used for checking values returned by tod_get().
20980Sstevel@tonic-gate  * Four error cases can be detected by this routine:
20990Sstevel@tonic-gate  *   TOD_REVERSED: current tod value is less than previous.
21000Sstevel@tonic-gate  *   TOD_STALLED: current tod value hasn't advanced.
21010Sstevel@tonic-gate  *   TOD_JUMPED: current tod value advanced too far from previous value.
21020Sstevel@tonic-gate  *   TOD_RATECHANGED: the ratio between average tod delta and
21030Sstevel@tonic-gate  *   average tick delta has changed.
21040Sstevel@tonic-gate  */
21050Sstevel@tonic-gate time_t
21060Sstevel@tonic-gate tod_validate(time_t tod)
21070Sstevel@tonic-gate {
21080Sstevel@tonic-gate 	time_t diff_tod;
21090Sstevel@tonic-gate 	hrtime_t diff_tick;
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	long dtick;
21120Sstevel@tonic-gate 	int dtick_delta;
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	int off = 0;
21150Sstevel@tonic-gate 	enum tod_fault_type tod_bad = TOD_NOFAULT;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	static int firsttime = 1;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	static time_t prev_tod = 0;
21200Sstevel@tonic-gate 	static hrtime_t prev_tick = 0;
21210Sstevel@tonic-gate 	static long dtick_avg = TOD_REF_FREQ;
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	hrtime_t tick = gethrtime();
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	/*
21280Sstevel@tonic-gate 	 * tod_validate_enable is patchable via /etc/system.
2129950Ssethg 	 * If TOD is already faulted, or if TOD validation is deferred,
2130950Ssethg 	 * there is nothing to do.
21310Sstevel@tonic-gate 	 */
2132950Ssethg 	if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
2133950Ssethg 	    tod_validate_deferred) {
21340Sstevel@tonic-gate 		return (tod);
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	/*
21380Sstevel@tonic-gate 	 * Update prev_tod and prev_tick values for first run
21390Sstevel@tonic-gate 	 */
21400Sstevel@tonic-gate 	if (firsttime) {
21410Sstevel@tonic-gate 		firsttime = 0;
21420Sstevel@tonic-gate 		prev_tod = tod;
21430Sstevel@tonic-gate 		prev_tick = tick;
21440Sstevel@tonic-gate 		return (tod);
21450Sstevel@tonic-gate 	}
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	/*
21480Sstevel@tonic-gate 	 * For either of these conditions, we need to reset ourself
21490Sstevel@tonic-gate 	 * and start validation from zero since each condition
21500Sstevel@tonic-gate 	 * indicates that the TOD will be updated with new value
21510Sstevel@tonic-gate 	 * Also, note that tod_needsync will be reset in clock()
21520Sstevel@tonic-gate 	 */
21530Sstevel@tonic-gate 	if (tod_needsync || tod_fault_reset_flag) {
21540Sstevel@tonic-gate 		firsttime = 1;
21550Sstevel@tonic-gate 		prev_tod = 0;
21560Sstevel@tonic-gate 		prev_tick = 0;
21570Sstevel@tonic-gate 		dtick_avg = TOD_REF_FREQ;
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 		if (tod_fault_reset_flag)
21600Sstevel@tonic-gate 			tod_fault_reset_flag = 0;
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 		return (tod);
21630Sstevel@tonic-gate 	}
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 	/* test hook */
21660Sstevel@tonic-gate 	switch (tod_unit_test) {
21670Sstevel@tonic-gate 	case 1: /* for testing jumping tod */
21680Sstevel@tonic-gate 		tod += tod_test_injector;
21690Sstevel@tonic-gate 		tod_unit_test = 0;
21700Sstevel@tonic-gate 		break;
21710Sstevel@tonic-gate 	case 2:	/* for testing stuck tod bit */
21720Sstevel@tonic-gate 		tod |= 1 << tod_test_injector;
21730Sstevel@tonic-gate 		tod_unit_test = 0;
21740Sstevel@tonic-gate 		break;
21750Sstevel@tonic-gate 	case 3:	/* for testing stalled tod */
21760Sstevel@tonic-gate 		tod = prev_tod;
21770Sstevel@tonic-gate 		tod_unit_test = 0;
21780Sstevel@tonic-gate 		break;
21790Sstevel@tonic-gate 	case 4:	/* reset tod fault status */
21800Sstevel@tonic-gate 		(void) tod_fault(TOD_NOFAULT, 0);
21810Sstevel@tonic-gate 		tod_unit_test = 0;
21820Sstevel@tonic-gate 		break;
21830Sstevel@tonic-gate 	default:
21840Sstevel@tonic-gate 		break;
21850Sstevel@tonic-gate 	}
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	diff_tod = tod - prev_tod;
21880Sstevel@tonic-gate 	diff_tick = tick - prev_tick;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	ASSERT(diff_tick >= 0);
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	if (diff_tod < 0) {
21930Sstevel@tonic-gate 		/* ERROR - tod reversed */
21940Sstevel@tonic-gate 		tod_bad = TOD_REVERSED;
21950Sstevel@tonic-gate 		off = (int)(prev_tod - tod);
21960Sstevel@tonic-gate 	} else if (diff_tod == 0) {
21970Sstevel@tonic-gate 		/* tod did not advance */
21980Sstevel@tonic-gate 		if (diff_tick > TOD_STALL_THRESHOLD) {
21990Sstevel@tonic-gate 			/* ERROR - tod stalled */
22000Sstevel@tonic-gate 			tod_bad = TOD_STALLED;
22010Sstevel@tonic-gate 		} else {
22020Sstevel@tonic-gate 			/*
22030Sstevel@tonic-gate 			 * Make sure we don't update prev_tick
22040Sstevel@tonic-gate 			 * so that diff_tick is calculated since
22050Sstevel@tonic-gate 			 * the first diff_tod == 0
22060Sstevel@tonic-gate 			 */
22070Sstevel@tonic-gate 			return (tod);
22080Sstevel@tonic-gate 		}
22090Sstevel@tonic-gate 	} else {
22100Sstevel@tonic-gate 		/* calculate dtick */
22110Sstevel@tonic-gate 		dtick = diff_tick / diff_tod;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		/* update dtick averages */
22140Sstevel@tonic-gate 		dtick_avg += ((dtick - dtick_avg) / TOD_FILTER_N);
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 		/*
22170Sstevel@tonic-gate 		 * Calculate dtick_delta as
22180Sstevel@tonic-gate 		 * variation from reference freq in quartiles
22190Sstevel@tonic-gate 		 */
22200Sstevel@tonic-gate 		dtick_delta = (dtick_avg - TOD_REF_FREQ) /
22215076Smishra 		    (TOD_REF_FREQ >> 2);
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 		/*
22240Sstevel@tonic-gate 		 * Even with a perfectly functioning TOD device,
22250Sstevel@tonic-gate 		 * when the number of elapsed seconds is low the
22260Sstevel@tonic-gate 		 * algorithm can calculate a rate that is beyond
22270Sstevel@tonic-gate 		 * tolerance, causing an error.  The algorithm is
22280Sstevel@tonic-gate 		 * inaccurate when elapsed time is low (less than
22290Sstevel@tonic-gate 		 * 5 seconds).
22300Sstevel@tonic-gate 		 */
22310Sstevel@tonic-gate 		if (diff_tod > 4) {
22320Sstevel@tonic-gate 			if (dtick < TOD_JUMP_THRESHOLD) {
22330Sstevel@tonic-gate 				/* ERROR - tod jumped */
22340Sstevel@tonic-gate 				tod_bad = TOD_JUMPED;
22350Sstevel@tonic-gate 				off = (int)diff_tod;
22360Sstevel@tonic-gate 			} else if (dtick_delta) {
22370Sstevel@tonic-gate 				/* ERROR - change in clock rate */
22380Sstevel@tonic-gate 				tod_bad = TOD_RATECHANGED;
22390Sstevel@tonic-gate 			}
22400Sstevel@tonic-gate 		}
22410Sstevel@tonic-gate 	}
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	if (tod_bad != TOD_NOFAULT) {
22440Sstevel@tonic-gate 		(void) tod_fault(tod_bad, off);
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 		/*
22470Sstevel@tonic-gate 		 * Disable dosynctodr since we are going to fault
22480Sstevel@tonic-gate 		 * the TOD chip anyway here
22490Sstevel@tonic-gate 		 */
22500Sstevel@tonic-gate 		dosynctodr = 0;
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 		/*
22530Sstevel@tonic-gate 		 * Set tod to the correct value from hrestime
22540Sstevel@tonic-gate 		 */
22550Sstevel@tonic-gate 		tod = hrestime.tv_sec;
22560Sstevel@tonic-gate 	}
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	prev_tod = tod;
22590Sstevel@tonic-gate 	prev_tick = tick;
22600Sstevel@tonic-gate 	return (tod);
22610Sstevel@tonic-gate }
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate static void
22640Sstevel@tonic-gate calcloadavg(int nrun, uint64_t *hp_ave)
22650Sstevel@tonic-gate {
22660Sstevel@tonic-gate 	static int64_t f[3] = { 135, 27, 9 };
22670Sstevel@tonic-gate 	uint_t i;
22680Sstevel@tonic-gate 	int64_t q, r;
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	/*
22710Sstevel@tonic-gate 	 * Compute load average over the last 1, 5, and 15 minutes
22720Sstevel@tonic-gate 	 * (60, 300, and 900 seconds).  The constants in f[3] are for
22730Sstevel@tonic-gate 	 * exponential decay:
22740Sstevel@tonic-gate 	 * (1 - exp(-1/60)) << 13 = 135,
22750Sstevel@tonic-gate 	 * (1 - exp(-1/300)) << 13 = 27,
22760Sstevel@tonic-gate 	 * (1 - exp(-1/900)) << 13 = 9.
22770Sstevel@tonic-gate 	 */
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	/*
22800Sstevel@tonic-gate 	 * a little hoop-jumping to avoid integer overflow
22810Sstevel@tonic-gate 	 */
22820Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
22830Sstevel@tonic-gate 		q = (hp_ave[i]  >> 16) << 7;
22840Sstevel@tonic-gate 		r = (hp_ave[i]  & 0xffff) << 7;
22850Sstevel@tonic-gate 		hp_ave[i] += ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
22860Sstevel@tonic-gate 	}
22870Sstevel@tonic-gate }
2288*11066Srafael.vanoni@sun.com 
2289*11066Srafael.vanoni@sun.com /*
2290*11066Srafael.vanoni@sun.com  * lbolt_hybrid() is used by ddi_get_lbolt() and ddi_get_lbolt64() to
2291*11066Srafael.vanoni@sun.com  * calculate the value of lbolt according to the current mode. In the event
2292*11066Srafael.vanoni@sun.com  * driven mode (the default), lbolt is calculated by dividing the current hires
2293*11066Srafael.vanoni@sun.com  * time by the number of nanoseconds per clock tick. In the cyclic driven mode
2294*11066Srafael.vanoni@sun.com  * an internal variable is incremented at each firing of the lbolt cyclic
2295*11066Srafael.vanoni@sun.com  * and returned by lbolt_cyclic_driven().
2296*11066Srafael.vanoni@sun.com  *
2297*11066Srafael.vanoni@sun.com  * The system will transition from event to cyclic driven mode when the number
2298*11066Srafael.vanoni@sun.com  * of calls to lbolt_event_driven() exceeds the (per CPU) threshold within a
2299*11066Srafael.vanoni@sun.com  * window of time. It does so by reprograming lbolt_cyclic from CY_INFINITY to
2300*11066Srafael.vanoni@sun.com  * nsec_per_tick. The lbolt cyclic will remain ON while at least one CPU is
2301*11066Srafael.vanoni@sun.com  * causing enough activity to cross the thresholds.
2302*11066Srafael.vanoni@sun.com  */
2303*11066Srafael.vanoni@sun.com static int64_t
2304*11066Srafael.vanoni@sun.com lbolt_bootstrap(void)
2305*11066Srafael.vanoni@sun.com {
2306*11066Srafael.vanoni@sun.com 	return (0);
2307*11066Srafael.vanoni@sun.com }
2308*11066Srafael.vanoni@sun.com 
2309*11066Srafael.vanoni@sun.com /* ARGSUSED */
2310*11066Srafael.vanoni@sun.com uint_t
2311*11066Srafael.vanoni@sun.com lbolt_ev_to_cyclic(caddr_t arg1, caddr_t arg2)
2312*11066Srafael.vanoni@sun.com {
2313*11066Srafael.vanoni@sun.com 	hrtime_t ts, exp;
2314*11066Srafael.vanoni@sun.com 	int ret;
2315*11066Srafael.vanoni@sun.com 
2316*11066Srafael.vanoni@sun.com 	ASSERT(lbolt_hybrid != lbolt_cyclic_driven);
2317*11066Srafael.vanoni@sun.com 
2318*11066Srafael.vanoni@sun.com 	kpreempt_disable();
2319*11066Srafael.vanoni@sun.com 
2320*11066Srafael.vanoni@sun.com 	ts = gethrtime();
2321*11066Srafael.vanoni@sun.com 	lb_info->lbi_internal = (ts/nsec_per_tick);
2322*11066Srafael.vanoni@sun.com 
2323*11066Srafael.vanoni@sun.com 	/*
2324*11066Srafael.vanoni@sun.com 	 * Align the next expiration to a clock tick boundary.
2325*11066Srafael.vanoni@sun.com 	 */
2326*11066Srafael.vanoni@sun.com 	exp = ts + nsec_per_tick - 1;
2327*11066Srafael.vanoni@sun.com 	exp = (exp/nsec_per_tick) * nsec_per_tick;
2328*11066Srafael.vanoni@sun.com 
2329*11066Srafael.vanoni@sun.com 	ret = cyclic_reprogram(lb_info->lbi_cyclic_id, exp);
2330*11066Srafael.vanoni@sun.com 	ASSERT(ret);
2331*11066Srafael.vanoni@sun.com 
2332*11066Srafael.vanoni@sun.com 	lbolt_hybrid = lbolt_cyclic_driven;
2333*11066Srafael.vanoni@sun.com 	lb_info->lbi_cyc_deactivate = B_FALSE;
2334*11066Srafael.vanoni@sun.com 	lb_info->lbi_cyc_deac_start = lb_info->lbi_internal;
2335*11066Srafael.vanoni@sun.com 
2336*11066Srafael.vanoni@sun.com 	kpreempt_enable();
2337*11066Srafael.vanoni@sun.com 
2338*11066Srafael.vanoni@sun.com 	ret = atomic_dec_32_nv(&lb_info->lbi_token);
2339*11066Srafael.vanoni@sun.com 	ASSERT(ret == 0);
2340*11066Srafael.vanoni@sun.com 
2341*11066Srafael.vanoni@sun.com 	return (1);
2342*11066Srafael.vanoni@sun.com }
2343*11066Srafael.vanoni@sun.com 
2344*11066Srafael.vanoni@sun.com int64_t
2345*11066Srafael.vanoni@sun.com lbolt_event_driven(void)
2346*11066Srafael.vanoni@sun.com {
2347*11066Srafael.vanoni@sun.com 	hrtime_t ts;
2348*11066Srafael.vanoni@sun.com 	int64_t lb;
2349*11066Srafael.vanoni@sun.com 	int ret, cpu = CPU->cpu_seqid;
2350*11066Srafael.vanoni@sun.com 
2351*11066Srafael.vanoni@sun.com 	ts = gethrtime();
2352*11066Srafael.vanoni@sun.com 	ASSERT(ts > 0);
2353*11066Srafael.vanoni@sun.com 
2354*11066Srafael.vanoni@sun.com 	ASSERT(nsec_per_tick > 0);
2355*11066Srafael.vanoni@sun.com 	lb = (ts/nsec_per_tick);
2356*11066Srafael.vanoni@sun.com 
2357*11066Srafael.vanoni@sun.com 	/*
2358*11066Srafael.vanoni@sun.com 	 * Switch to cyclic mode if the number of calls to this routine
2359*11066Srafael.vanoni@sun.com 	 * has reached the threshold within the interval.
2360*11066Srafael.vanoni@sun.com 	 */
2361*11066Srafael.vanoni@sun.com 	if ((lb - lb_cpu[cpu].lbc_cnt_start) < lb_info->lbi_thresh_interval) {
2362*11066Srafael.vanoni@sun.com 
2363*11066Srafael.vanoni@sun.com 		if (--lb_cpu[cpu].lbc_counter == 0) {
2364*11066Srafael.vanoni@sun.com 			/*
2365*11066Srafael.vanoni@sun.com 			 * Reached the threshold within the interval, reset
2366*11066Srafael.vanoni@sun.com 			 * the usage statistics.
2367*11066Srafael.vanoni@sun.com 			 */
2368*11066Srafael.vanoni@sun.com 			lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
2369*11066Srafael.vanoni@sun.com 			lb_cpu[cpu].lbc_cnt_start = lb;
2370*11066Srafael.vanoni@sun.com 
2371*11066Srafael.vanoni@sun.com 			/*
2372*11066Srafael.vanoni@sun.com 			 * Make sure only one thread reprograms the
2373*11066Srafael.vanoni@sun.com 			 * lbolt cyclic and changes the mode.
2374*11066Srafael.vanoni@sun.com 			 */
2375*11066Srafael.vanoni@sun.com 			if (panicstr == NULL &&
2376*11066Srafael.vanoni@sun.com 			    atomic_cas_32(&lb_info->lbi_token, 0, 1) == 0) {
2377*11066Srafael.vanoni@sun.com 
2378*11066Srafael.vanoni@sun.com 				if (lbolt_hybrid == lbolt_cyclic_driven) {
2379*11066Srafael.vanoni@sun.com 					ret = atomic_dec_32_nv(
2380*11066Srafael.vanoni@sun.com 					    &lb_info->lbi_token);
2381*11066Srafael.vanoni@sun.com 					ASSERT(ret == 0);
2382*11066Srafael.vanoni@sun.com 					return (lb);
2383*11066Srafael.vanoni@sun.com 				}
2384*11066Srafael.vanoni@sun.com 
2385*11066Srafael.vanoni@sun.com 				lbolt_softint_post();
2386*11066Srafael.vanoni@sun.com 			}
2387*11066Srafael.vanoni@sun.com 		}
2388*11066Srafael.vanoni@sun.com 	} else {
2389*11066Srafael.vanoni@sun.com 		/*
2390*11066Srafael.vanoni@sun.com 		 * Exceeded the interval, reset the usage statistics.
2391*11066Srafael.vanoni@sun.com 		 */
2392*11066Srafael.vanoni@sun.com 		lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
2393*11066Srafael.vanoni@sun.com 		lb_cpu[cpu].lbc_cnt_start = lb;
2394*11066Srafael.vanoni@sun.com 	}
2395*11066Srafael.vanoni@sun.com 
2396*11066Srafael.vanoni@sun.com 	ASSERT(lb >= lb_info->lbi_debug_time);
2397*11066Srafael.vanoni@sun.com 
2398*11066Srafael.vanoni@sun.com 	return (lb - lb_info->lbi_debug_time);
2399*11066Srafael.vanoni@sun.com }
2400*11066Srafael.vanoni@sun.com 
2401*11066Srafael.vanoni@sun.com int64_t
2402*11066Srafael.vanoni@sun.com lbolt_cyclic_driven(void)
2403*11066Srafael.vanoni@sun.com {
2404*11066Srafael.vanoni@sun.com 	int64_t lb = lb_info->lbi_internal;
2405*11066Srafael.vanoni@sun.com 	int cpu = CPU->cpu_seqid;
2406*11066Srafael.vanoni@sun.com 
2407*11066Srafael.vanoni@sun.com 	if ((lb - lb_cpu[cpu].lbc_cnt_start) < lb_info->lbi_thresh_interval) {
2408*11066Srafael.vanoni@sun.com 
2409*11066Srafael.vanoni@sun.com 		if (lb_cpu[cpu].lbc_counter == 0)
2410*11066Srafael.vanoni@sun.com 			/*
2411*11066Srafael.vanoni@sun.com 			 * Reached the threshold within the interval,
2412*11066Srafael.vanoni@sun.com 			 * prevent the lbolt cyclic from turning itself
2413*11066Srafael.vanoni@sun.com 			 * off.
2414*11066Srafael.vanoni@sun.com 			 */
2415*11066Srafael.vanoni@sun.com 			lb_info->lbi_cyc_deactivate = B_FALSE;
2416*11066Srafael.vanoni@sun.com 		else
2417*11066Srafael.vanoni@sun.com 			lb_cpu[cpu].lbc_counter--;
2418*11066Srafael.vanoni@sun.com 	} else {
2419*11066Srafael.vanoni@sun.com 		/*
2420*11066Srafael.vanoni@sun.com 		 * Only reset the usage statistics when the interval has
2421*11066Srafael.vanoni@sun.com 		 * exceeded.
2422*11066Srafael.vanoni@sun.com 		 */
2423*11066Srafael.vanoni@sun.com 		lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
2424*11066Srafael.vanoni@sun.com 		lb_cpu[cpu].lbc_cnt_start = lb;
2425*11066Srafael.vanoni@sun.com 	}
2426*11066Srafael.vanoni@sun.com 
2427*11066Srafael.vanoni@sun.com 	ASSERT(lb >= lb_info->lbi_debug_time);
2428*11066Srafael.vanoni@sun.com 
2429*11066Srafael.vanoni@sun.com 	return (lb - lb_info->lbi_debug_time);
2430*11066Srafael.vanoni@sun.com }
2431*11066Srafael.vanoni@sun.com 
2432*11066Srafael.vanoni@sun.com /*
2433*11066Srafael.vanoni@sun.com  * The lbolt_cyclic() routine will fire at a nsec_per_tick rate to satisfy
2434*11066Srafael.vanoni@sun.com  * performance needs of ddi_get_lbolt() and ddi_get_lbolt64() consumers.
2435*11066Srafael.vanoni@sun.com  * It is inactive by default, and will be activated when switching from event
2436*11066Srafael.vanoni@sun.com  * to cyclic driven lbolt. The cyclic will turn itself off unless signaled
2437*11066Srafael.vanoni@sun.com  * by lbolt_cyclic_driven().
2438*11066Srafael.vanoni@sun.com  */
2439*11066Srafael.vanoni@sun.com static void
2440*11066Srafael.vanoni@sun.com lbolt_cyclic(void)
2441*11066Srafael.vanoni@sun.com {
2442*11066Srafael.vanoni@sun.com 	int ret;
2443*11066Srafael.vanoni@sun.com 
2444*11066Srafael.vanoni@sun.com 	lb_info->lbi_internal++;
2445*11066Srafael.vanoni@sun.com 
2446*11066Srafael.vanoni@sun.com 	if (!lbolt_cyc_only) {
2447*11066Srafael.vanoni@sun.com 
2448*11066Srafael.vanoni@sun.com 		if (lb_info->lbi_cyc_deactivate) {
2449*11066Srafael.vanoni@sun.com 			/*
2450*11066Srafael.vanoni@sun.com 			 * Switching from cyclic to event driven mode.
2451*11066Srafael.vanoni@sun.com 			 */
2452*11066Srafael.vanoni@sun.com 			if (atomic_cas_32(&lb_info->lbi_token, 0, 1) == 0) {
2453*11066Srafael.vanoni@sun.com 
2454*11066Srafael.vanoni@sun.com 				if (lbolt_hybrid == lbolt_event_driven) {
2455*11066Srafael.vanoni@sun.com 					ret = atomic_dec_32_nv(
2456*11066Srafael.vanoni@sun.com 					    &lb_info->lbi_token);
2457*11066Srafael.vanoni@sun.com 					ASSERT(ret == 0);
2458*11066Srafael.vanoni@sun.com 					return;
2459*11066Srafael.vanoni@sun.com 				}
2460*11066Srafael.vanoni@sun.com 
2461*11066Srafael.vanoni@sun.com 				kpreempt_disable();
2462*11066Srafael.vanoni@sun.com 
2463*11066Srafael.vanoni@sun.com 				lbolt_hybrid = lbolt_event_driven;
2464*11066Srafael.vanoni@sun.com 				ret = cyclic_reprogram(lb_info->lbi_cyclic_id,
2465*11066Srafael.vanoni@sun.com 				    CY_INFINITY);
2466*11066Srafael.vanoni@sun.com 				ASSERT(ret);
2467*11066Srafael.vanoni@sun.com 
2468*11066Srafael.vanoni@sun.com 				kpreempt_enable();
2469*11066Srafael.vanoni@sun.com 
2470*11066Srafael.vanoni@sun.com 				ret = atomic_dec_32_nv(&lb_info->lbi_token);
2471*11066Srafael.vanoni@sun.com 				ASSERT(ret == 0);
2472*11066Srafael.vanoni@sun.com 			}
2473*11066Srafael.vanoni@sun.com 		}
2474*11066Srafael.vanoni@sun.com 
2475*11066Srafael.vanoni@sun.com 		/*
2476*11066Srafael.vanoni@sun.com 		 * The lbolt cyclic should not try to deactivate itself before
2477*11066Srafael.vanoni@sun.com 		 * the sampling period has elapsed.
2478*11066Srafael.vanoni@sun.com 		 */
2479*11066Srafael.vanoni@sun.com 		if (lb_info->lbi_internal - lb_info->lbi_cyc_deac_start >=
2480*11066Srafael.vanoni@sun.com 		    lb_info->lbi_thresh_interval) {
2481*11066Srafael.vanoni@sun.com 			lb_info->lbi_cyc_deactivate = B_TRUE;
2482*11066Srafael.vanoni@sun.com 			lb_info->lbi_cyc_deac_start = lb_info->lbi_internal;
2483*11066Srafael.vanoni@sun.com 		}
2484*11066Srafael.vanoni@sun.com 	}
2485*11066Srafael.vanoni@sun.com }
2486*11066Srafael.vanoni@sun.com 
2487*11066Srafael.vanoni@sun.com /*
2488*11066Srafael.vanoni@sun.com  * Since the lbolt service was historically cyclic driven, it must be 'stopped'
2489*11066Srafael.vanoni@sun.com  * when the system drops into the kernel debugger. lbolt_debug_entry() is
2490*11066Srafael.vanoni@sun.com  * called by the KDI system claim callbacks to record a hires timestamp at
2491*11066Srafael.vanoni@sun.com  * debug enter time. lbolt_debug_return() is called by the sistem release
2492*11066Srafael.vanoni@sun.com  * callbacks to account for the time spent in the debugger. The value is then
2493*11066Srafael.vanoni@sun.com  * accumulated in the lb_info structure and used by lbolt_event_driven() and
2494*11066Srafael.vanoni@sun.com  * lbolt_cyclic_driven(), as well as the mdb_get_lbolt() routine.
2495*11066Srafael.vanoni@sun.com  */
2496*11066Srafael.vanoni@sun.com void
2497*11066Srafael.vanoni@sun.com lbolt_debug_entry(void)
2498*11066Srafael.vanoni@sun.com {
2499*11066Srafael.vanoni@sun.com 	lb_info->lbi_debug_ts = gethrtime();
2500*11066Srafael.vanoni@sun.com }
2501*11066Srafael.vanoni@sun.com 
2502*11066Srafael.vanoni@sun.com void
2503*11066Srafael.vanoni@sun.com lbolt_debug_return(void)
2504*11066Srafael.vanoni@sun.com {
2505*11066Srafael.vanoni@sun.com 	if (nsec_per_tick > 0)
2506*11066Srafael.vanoni@sun.com 		lb_info->lbi_debug_time +=
2507*11066Srafael.vanoni@sun.com 		    ((gethrtime() - lb_info->lbi_debug_ts)/nsec_per_tick);
2508*11066Srafael.vanoni@sun.com 
2509*11066Srafael.vanoni@sun.com 	lb_info->lbi_debug_ts = 0;
2510*11066Srafael.vanoni@sun.com }
2511