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;
61280973Sjhb#define	TC_FLAGS_C2STOP		1	/* Timer dies in C2+. */
62255726Sgibbs#define	TC_FLAGS_SUSPEND_SAFE	2	/*
63255726Sgibbs					 * Timer functional across
64255726Sgibbs					 * suspend/resume.
65255726Sgibbs					 */
66118987Sphk
6736810Sphk	void			*tc_priv;
6895817Sphk		/* Pointer to the timecounter's private parts. */
6991200Sphk	struct timecounter	*tc_next;
7095817Sphk		/* Pointer to the next timecounter. */
7133690Sphk};
7233690Sphk
7395530Sphkextern struct timecounter *timecounter;
74212603Smavextern int tc_min_ticktock_freq; /*
75212603Smav				  * Minimal tc_ticktock() call frequency,
76212603Smav				  * required to handle counter wraps.
77212603Smav				  */
7833690Sphk
79110038Sphku_int64_t tc_getfrequency(void);
8092719Salfredvoid	tc_init(struct timecounter *tc);
8192719Salfredvoid	tc_setclock(struct timespec *ts);
82212603Smavvoid	tc_ticktock(int cnt);
83212541Smavvoid	cpu_tick_calibration(void);
841541Srgrimes
85113348Sdes#ifdef SYSCTL_DECL
86113348SdesSYSCTL_DECL(_kern_timecounter);
87113348Sdes#endif
88113348Sdes
8958377Sphk#endif /* !_SYS_TIMETC_H_ */
90