195817Sphk/*-
258377Sphk * ----------------------------------------------------------------------------
358377Sphk * "THE BEER-WARE LICENSE" (Revision 42):
458377Sphk * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
558377Sphk * can do whatever you want with this stuff. If we meet some day, and you think
658377Sphk * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
758377Sphk * ----------------------------------------------------------------------------
81541Srgrimes *
950477Speter * $FreeBSD$
101541Srgrimes */
111541Srgrimes
1258377Sphk#ifndef _SYS_TIMETC_H_
1395817Sphk#define	_SYS_TIMETC_H_
141541Srgrimes
1595817Sphk#ifndef _KERNEL
1695817Sphk#error "no user-serviceable parts inside"
1795817Sphk#endif
1895817Sphk
19210226Strasz/*-
2095817Sphk * `struct timecounter' is the interface between the hardware which implements
2195661Sphk * a timecounter and the MI code which uses this to keep track of time.
2233690Sphk *
2395661Sphk * A timecounter is a binary counter which has two properties:
2434901Sphk *    * it runs at a fixed, known frequency.
2595817Sphk *    * it has sufficient bits to not roll over in less than approximately
2695817Sphk *      max(2 msec, 2/HZ seconds).  (The value 2 here is really 1 + delta,
2795817Sphk *      for some indeterminate value of delta.)
2833690Sphk */
2933690Sphk
3033690Sphkstruct timecounter;
3195661Sphktypedef u_int timecounter_get_t(struct timecounter *);
3292719Salfredtypedef void timecounter_pps_t(struct timecounter *);
3333690Sphk
3433690Sphkstruct timecounter {
3536810Sphk	timecounter_get_t	*tc_get_timecount;
3695661Sphk		/*
3795661Sphk		 * This function reads the counter.  It is not required to
3895661Sphk		 * mask any unimplemented bits out, as long as they are
3995661Sphk		 * constant.
4095661Sphk		 */
4136810Sphk	timecounter_pps_t	*tc_poll_pps;
4295661Sphk		/*
4395817Sphk		 * This function is optional.  It will be called whenever the
4495661Sphk		 * timecounter is rewound, and is intended to check for PPS
4595817Sphk		 * events.  Normal hardware does not need it but timecounters
4695817Sphk		 * which latch PPS in hardware (like sys/pci/xrpu.c) do.
4795661Sphk		 */
4895976Sphk	u_int 			tc_counter_mask;
4995817Sphk		/* This mask should mask off any unimplemented bits. */
50224043Sjkim	uint64_t		tc_frequency;
5195661Sphk		/* Frequency of the counter in Hz. */
5236810Sphk	char			*tc_name;
5395817Sphk		/* Name of the timecounter. */
54118987Sphk	int			tc_quality;
55118987Sphk		/*
56118987Sphk		 * Used to determine if this timecounter is better than
57118987Sphk		 * another timecounter higher means better.  Negative
58118987Sphk		 * means "only use at explicit request".
59118987Sphk		 */
60224042Sjkim	u_int			tc_flags;
61224042Sjkim#define	TC_FLAGS_C3STOP		1	/* Timer dies in C3. */
62118987Sphk
6336810Sphk	void			*tc_priv;
6495817Sphk		/* Pointer to the timecounter's private parts. */
6591200Sphk	struct timecounter	*tc_next;
6695817Sphk		/* Pointer to the next timecounter. */
6733690Sphk};
6833690Sphk
6995530Sphkextern struct timecounter *timecounter;
70212603Smavextern int tc_min_ticktock_freq; /*
71212603Smav				  * Minimal tc_ticktock() call frequency,
72212603Smav				  * required to handle counter wraps.
73212603Smav				  */
7433690Sphk
75110038Sphku_int64_t tc_getfrequency(void);
7692719Salfredvoid	tc_init(struct timecounter *tc);
7792719Salfredvoid	tc_setclock(struct timespec *ts);
78212603Smavvoid	tc_ticktock(int cnt);
79212541Smavvoid	cpu_tick_calibration(void);
801541Srgrimes
81113348Sdes#ifdef SYSCTL_DECL
82113348SdesSYSCTL_DECL(_kern_timecounter);
83113348Sdes#endif
84113348Sdes
8558377Sphk#endif /* !_SYS_TIMETC_H_ */
86