xref: /onnv-gate/usr/src/uts/sun4/sys/clock.h (revision 11172:a792f425ae2e)
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
52973Sgovinda  * Common Development and Distribution License (the "License").
62973Sgovinda  * 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 /*
22*11172SHaik.Aftandilian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef _SYS_CLOCK_H
270Sstevel@tonic-gate #define	_SYS_CLOCK_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #ifdef	__cplusplus
300Sstevel@tonic-gate extern "C" {
310Sstevel@tonic-gate #endif
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/spl.h>
340Sstevel@tonic-gate #include <sys/time.h>
350Sstevel@tonic-gate #include <sys/machclock.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #ifndef _ASM
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #ifdef	_KERNEL
400Sstevel@tonic-gate 
410Sstevel@tonic-gate extern void	setcpudelay(void);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate extern uint_t	nsec_scale;
440Sstevel@tonic-gate extern uint_t	nsec_shift;
450Sstevel@tonic-gate extern uint_t	nsec_per_sys_tick;
460Sstevel@tonic-gate extern uint64_t	sys_tick_freq;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate extern int	traptrace_use_stick;
490Sstevel@tonic-gate extern uint64_t	system_clock_freq;
500Sstevel@tonic-gate extern uint_t	sys_clock_mhz;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate extern void mon_clock_init(void);
530Sstevel@tonic-gate extern void mon_clock_start(void);
540Sstevel@tonic-gate extern void mon_clock_stop(void);
550Sstevel@tonic-gate extern void mon_clock_share(void);
560Sstevel@tonic-gate extern void mon_clock_unshare(void);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate extern hrtime_t hrtime_base;
590Sstevel@tonic-gate extern void hres_tick(void);
600Sstevel@tonic-gate extern void	clkstart(void);
610Sstevel@tonic-gate extern void cbe_level14();
620Sstevel@tonic-gate extern hrtime_t tick2ns(hrtime_t, uint_t);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate typedef struct {
652973Sgovinda 	uint64_t cbe_level1_inum;
662973Sgovinda 	uint64_t cbe_level10_inum;
670Sstevel@tonic-gate } cbe_data_t;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #endif	/* _KERNEL */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #endif	/* _ASM */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #define	CBE_LOW_PIL	1
750Sstevel@tonic-gate #define	CBE_LOCK_PIL	LOCK_LEVEL
760Sstevel@tonic-gate #define	CBE_HIGH_PIL	14
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	ADJ_SHIFT	4	/* used in get_hrestime and _level10 */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Locking strategy for high-resolution timing services
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * We generally construct timestamps from two or more components:
840Sstevel@tonic-gate  * a hardware time source and one or more software time sources.
850Sstevel@tonic-gate  * These components cannot all be loaded simultaneously, so we need
860Sstevel@tonic-gate  * some sort of locking strategy to generate consistent timestamps.
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * To minimize lock contention and cache thrashing we employ the
890Sstevel@tonic-gate  * weakest possible synchronization model: writers (rare) serialize
900Sstevel@tonic-gate  * on an acquisition-counting mutex, described below; readers (common)
910Sstevel@tonic-gate  * execute in parallel with no synchronization at all -- they don't
920Sstevel@tonic-gate  * exclude other readers, and they don't even exclude writers.  Instead,
930Sstevel@tonic-gate  * readers just examine the writer lock's value before and after loading
940Sstevel@tonic-gate  * all the components of a timestamp to detect writer intervention.
950Sstevel@tonic-gate  * In the rare case when a writer does intervene, the reader will
960Sstevel@tonic-gate  * detect it, discard the timestamp and try again.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * The writer lock, hres_lock, is a 32-bit integer consisting of an
990Sstevel@tonic-gate  * 8-bit lock and a 24-bit acquisition count.  To acquire the lock we
1000Sstevel@tonic-gate  * set the lock field with ldstub, which sets the low-order 8 bits to
1010Sstevel@tonic-gate  * 0xff; to clear the lock, we increment it, which simultaneously clears
1020Sstevel@tonic-gate  * the lock field (0xff --> 0x00) and increments the acquisition count
1030Sstevel@tonic-gate  * (due to carry into bit 8).  Thus each acquisition transforms hres_lock
1040Sstevel@tonic-gate  * from N:0 to N:ff, and each release transforms N:ff into (N+1):0.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * Readers can detect writer intervention by loading hres_lock before
1070Sstevel@tonic-gate  * and after loading the time components they need; if either lock value
1080Sstevel@tonic-gate  * contains 0xff in the low-order bits (lock held), or if the lock values
1090Sstevel@tonic-gate  * are not equal (lock was acquired and released), a writer intervened
1100Sstevel@tonic-gate  * and the reader must try again.  If the lock values are equal and the
1110Sstevel@tonic-gate  * low-order 8 bits are clear, the timestamp must be valid.  We can check
1120Sstevel@tonic-gate  * both of these conditions with a single compare instruction by checking
1130Sstevel@tonic-gate  * whether old_hres_lock & ~1 == new_hres_lock, as illustrated by the
1140Sstevel@tonic-gate  * following table of all possible lock states:
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  *	initial	& ~1	final		result of compare
1170Sstevel@tonic-gate  *	------------	-----		-----------------
1180Sstevel@tonic-gate  *	now:00		now:00		valid
1190Sstevel@tonic-gate  *	now:00		now:ff		invalid
1200Sstevel@tonic-gate  *	now:00		later:00	invalid
1210Sstevel@tonic-gate  *	now:00		later:ff	invalid
1220Sstevel@tonic-gate  *	now:fe		now:ff		invalid
1230Sstevel@tonic-gate  *	now:fe		later:00	invalid
1240Sstevel@tonic-gate  *	now:fe		later:ff	invalid
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  * Implementation considerations:
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  * (1) Load buffering.
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * On a CPU that does load buffering we must ensure that the load of
1310Sstevel@tonic-gate  * hres_lock completes before the load of any timestamp components.
1320Sstevel@tonic-gate  * This is essential *even on a CPU that does in-order loads* because
1330Sstevel@tonic-gate  * accessing the hardware time source may not involve a memory reference
1340Sstevel@tonic-gate  * (e.g. rd %tick).  A convenient way to address this is to clear the
1350Sstevel@tonic-gate  * lower bit (andn with 1) of the old lock value right away, since this
1360Sstevel@tonic-gate  * generates a dependency on the load of hres_lock.  We have to do this
1370Sstevel@tonic-gate  * anyway to perform the lock comparison described above.
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  * (2) Out-of-order loads.
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * On a CPU that does out-of-order loads we must ensure that the loads
1420Sstevel@tonic-gate  * of all timestamp components have completed before we load the final
1430Sstevel@tonic-gate  * value of hres_lock.  This can be done either by generating load
1440Sstevel@tonic-gate  * dependencies on the timestamp components or by membar #LoadLoad.
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  * (3) Interaction with the high level cyclic handler, hres_tick().
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * One unusual property of hres_lock is that it's acquired in a high
1490Sstevel@tonic-gate  * level cyclic handler, hres_tick().  Thus, hres_lock must be acquired at
1500Sstevel@tonic-gate  * CBE_HIGH_PIL or higher to prevent single-CPU deadlock.
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  * (4) Cross-calls.
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  * If a cross-call happens while one CPU has hres_lock and another is
1550Sstevel@tonic-gate  * trying to acquire it in the clock interrupt path, the system will
1560Sstevel@tonic-gate  * deadlock: the first CPU will never release hres_lock since it's
1570Sstevel@tonic-gate  * waiting to be released from the cross-call, and the cross-call can't
1580Sstevel@tonic-gate  * complete because the second CPU is spinning on hres_lock with traps
1590Sstevel@tonic-gate  * disabled.  Thus cross-calls must be blocked while holding hres_lock.
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  * Together, (3) and (4) imply that hres_lock should only be acquired
1620Sstevel@tonic-gate  * at PIL >= max(XCALL_PIL, CBE_HIGH_PIL), or while traps are disabled.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate #define	HRES_LOCK_OFFSET 3
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #define	CLOCK_LOCK(oldsplp)	\
1670Sstevel@tonic-gate 	lock_set_spl((lock_t *)&hres_lock + HRES_LOCK_OFFSET, \
1680Sstevel@tonic-gate 		ipltospl(CBE_HIGH_PIL), oldsplp)
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #define	CLOCK_UNLOCK(spl)	\
1710Sstevel@tonic-gate 	membar_ldst_stst();	\
1720Sstevel@tonic-gate 	hres_lock++;		\
1730Sstevel@tonic-gate 	splx(spl);		\
1740Sstevel@tonic-gate 	LOCKSTAT_RECORD0(LS_CLOCK_UNLOCK_RELEASE,	\
1750Sstevel@tonic-gate 		(lock_t *)&hres_lock + HRES_LOCK_OFFSET);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * NATIVE_TIME_TO_NSEC_SCALE is called with NSEC_SHIFT to convert hi-res
1790Sstevel@tonic-gate  * timestamps into nanoseconds. On systems that have a %stick register,
1800Sstevel@tonic-gate  * hi-res timestamps are in %stick units. On systems that do not have a
1810Sstevel@tonic-gate  * %stick register, hi-res timestamps are in %tick units.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * NATIVE_TIME_TO_NSEC_SCALE is called with TICK_NSEC_SHIFT to convert from
1840Sstevel@tonic-gate  * %tick units to nanoseconds on all implementations whether %stick is
1850Sstevel@tonic-gate  * available or not.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * At least 62.5 MHz CPU %tick frequency
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate #define	TICK_NSEC_SHIFT	4
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Convert hi-res native time (V9's %tick in our case) into nanoseconds.
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  * The challenge is to multiply a %tick value by (NANOSEC / sys_tick_freq)
1980Sstevel@tonic-gate  * without using floating point and without overflowing 64-bit integers.
1990Sstevel@tonic-gate  * We assume that all sun4u systems will have a 16 nsec or better clock
2000Sstevel@tonic-gate  * (i.e. faster than 62.5 MHz), which means that (ticks << 4) has units
2010Sstevel@tonic-gate  * greater than one nanosecond, so converting from (ticks << 4) to nsec
2020Sstevel@tonic-gate  * requires multiplication by a rational number, R, between 0 and 1.
2030Sstevel@tonic-gate  * To avoid floating-point we precompute (R * 2^32) during boot and
2040Sstevel@tonic-gate  * stash this away in nsec_scale.  Thus we can compute (tick * R) as
2050Sstevel@tonic-gate  * (tick * nsec_scale) >> 32, which is accurate to about 1 part per billion.
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  * To avoid 64-bit overflow when multiplying (tick << 4) by nsec_scale,
2080Sstevel@tonic-gate  * we split (tick << 4) into its high and low 32-bit pieces, H and L,
2090Sstevel@tonic-gate  * multiply each piece separately, and add up the relevant bits of the
2100Sstevel@tonic-gate  * partial products.  Putting it all together we have:
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  * nsec = (tick << 4) * R
2130Sstevel@tonic-gate  *	= ((tick << 4) * nsec_scale) >> 32
2140Sstevel@tonic-gate  *	= ((H << 32) + L) * nsec_scale) >> 32
2150Sstevel@tonic-gate  *	= (H * nsec_scale) + ((L * nsec_scale) >> 32)
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * The last line is the computation we actually perform: it requires no
2180Sstevel@tonic-gate  * floating point and all intermediate results fit in 64-bit registers.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * Note that we require that tick is less than (1 << (64 - NSEC_SHIFT));
2210Sstevel@tonic-gate  * greater values will result in overflow and misbehavior (not that this
2220Sstevel@tonic-gate  * is a serious problem; (1 << (64 - NSEC_SHIFT)) nanoseconds is over
2230Sstevel@tonic-gate  * thirty-six years).  Nonetheless, clients may wish to be aware of this
2240Sstevel@tonic-gate  * limitation; NATIVE_TIME_MAX() returns this maximum native time.
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * We provide two versions of this macro: a "full-service" version that
2270Sstevel@tonic-gate  * just converts ticks to nanoseconds and a higher-performance version that
2280Sstevel@tonic-gate  * expects the scaling factor nsec_scale as its second argument (so that
2290Sstevel@tonic-gate  * callers can distance the load of nsec_scale from its use).  Note that
2300Sstevel@tonic-gate  * we take a fast path if we determine the ticks to be less than 32 bits
2310Sstevel@tonic-gate  * (as it often is for the delta between %tick values for successive
2320Sstevel@tonic-gate  * firings of the hres_tick() cyclic).
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * Note that in the 32-bit path we don't even bother clearing NPT.
2350Sstevel@tonic-gate  * We get away with this by making hardclk.c ensure than nsec_scale
2360Sstevel@tonic-gate  * is even, so we can take advantage of the associativity of modular
2370Sstevel@tonic-gate  * arithmetic: multiplying %tick by any even number, say 2*n, is
2380Sstevel@tonic-gate  * equivalent to multiplying %tick by 2, then by n.  Multiplication
2390Sstevel@tonic-gate  * by 2 is equivalent to shifting left by one, which clears NPT.
2400Sstevel@tonic-gate  *
2410Sstevel@tonic-gate  * Finally, note that the macros use the labels "6:" and "7:"; these
2420Sstevel@tonic-gate  * labels must not be used across an invocation of either macro.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate #define	NATIVE_TIME_TO_NSEC_SCALE(out, scr1, scr2, shift)		\
2450Sstevel@tonic-gate 	srlx	out, 32, scr2;		/* check high 32 bits */	\
2460Sstevel@tonic-gate /* CSTYLED */ 								\
2470Sstevel@tonic-gate 	brz,a,pt scr2, 6f;		/* if clear, 32-bit fast path */\
2480Sstevel@tonic-gate 	mulx	out, scr1, out;		/* delay: 32-bit fast path */	\
2490Sstevel@tonic-gate 	sllx	out, shift, out;	/* clear NPT and pre-scale */	\
2500Sstevel@tonic-gate 	srlx	out, 32, scr2;		/* scr2 = hi32(tick<<4) = H */	\
2510Sstevel@tonic-gate 	mulx	scr2, scr1, scr2;	/* scr2 = (H*F) */		\
2520Sstevel@tonic-gate 	srl	out, 0, out;		/* out = lo32(tick<<4) = L */	\
2530Sstevel@tonic-gate 	mulx	out, scr1, scr1;	/* scr1 = (L*F) */		\
2540Sstevel@tonic-gate 	srlx	scr1, 32, scr1;		/* scr1 = (L*F) >> 32 */	\
2550Sstevel@tonic-gate 	ba	7f;			/* branch over 32-bit path */	\
2560Sstevel@tonic-gate 	add	scr1, scr2, out;	/* out = (H*F) + ((L*F) >> 32) */\
2570Sstevel@tonic-gate 6:									\
2580Sstevel@tonic-gate 	srlx	out, 32 - shift, out;					\
2590Sstevel@tonic-gate 7:
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate #define	NATIVE_TIME_TO_NSEC(out, scr1, scr2)				\
2620Sstevel@tonic-gate 	sethi	%hi(nsec_scale), scr1;	/* load scaling factor */	\
2630Sstevel@tonic-gate 	ld	[scr1 + %lo(nsec_scale)], scr1;				\
2640Sstevel@tonic-gate 	NATIVE_TIME_TO_NSEC_SCALE(out, scr1, scr2, NSEC_SHIFT);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate #define	NATIVE_TIME_MAX(out)						\
2670Sstevel@tonic-gate 	mov	-1, out;						\
2680Sstevel@tonic-gate 	srlx	out, NSEC_SHIFT, out
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * NSEC_SHIFT and VTRACE_SHIFT constants are defined in
2720Sstevel@tonic-gate  * <sys/machclock.h> file.
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate #ifdef	__cplusplus
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate #endif	/* !_SYS_CLOCK_H */
280