1 /* $NetBSD: timetc.h,v 1.9 2020/09/04 00:36:07 thorpej Exp $ */
2 
3 /*-
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  *
11  * $FreeBSD: src/sys/sys/timetc.h,v 1.58 2003/08/16 08:23:52 phk Exp $
12  */
13 
14 #ifndef _SYS_TIMETC_H_
15 #define   _SYS_TIMETC_H_
16 
17 #if !defined(_KERNEL) && !defined(_KMEMUSER)
18 #error "no user-serviceable parts inside"
19 #endif
20 
21 /*
22  * max recommended timecounter name length
23  *
24  * it is not a functional limit but names longer
25  * then that will not be controllable via
26  * sysctl. see kern/kern_tc.c for the sysctl
27  * implementation.
28  */
29 #define MAX_TCNAMELEN         64
30 
31 /*-
32  * `struct timecounter' is the interface between the hardware which implements
33  * a timecounter and the MI code which uses this to keep track of time.
34  *
35  * A timecounter is a binary counter which has two properties:
36  *    * it runs at a fixed, known frequency.
37  *    * it has sufficient bits to not roll over in less than approximately
38  *      max(2 msec, 2/HZ seconds).  (The value 2 here is really 1 + delta,
39  *      for some indeterminate value of delta.)
40  */
41 
42 struct timecounter;
43 struct timespec;
44 typedef u_int timecounter_get_t(struct timecounter *);
45 typedef void timecounter_pps_t(struct timecounter *);
46 
47 struct timecounter {
48           timecounter_get_t   *tc_get_timecount;
49                     /*
50                      * This function reads the counter.  It is not required to
51                      * mask any unimplemented bits out, as long as they are
52                      * constant.
53                      */
54           timecounter_pps_t   *tc_poll_pps;
55                     /*
56                      * This function is optional.  It will be called whenever the
57                      * timecounter is rewound, and is intended to check for PPS
58                      * events.  Normal hardware does not need it but timecounters
59                      * which latch PPS in hardware (like sys/pci/xrpu.c) do.
60                      */
61           u_int                         tc_counter_mask;
62                     /* This mask should mask off any unimplemented bits. */
63           uint64_t            tc_frequency;
64                     /* Frequency of the counter in Hz. */
65           const char                    *tc_name;
66                     /* Name of the timecounter. */
67           int                           tc_quality;
68                     /*
69                      * Used to determine if this timecounter is better than
70                      * another timecounter higher means better.  Negative
71                      * means "only use at explicit request".
72                      */
73 
74           void                          *tc_priv;
75                     /* Pointer to the timecounter's private parts. */
76           struct timecounter  *tc_next;
77                     /* Pointer to the next timecounter. */
78 };
79 
80 #ifdef _KERNEL
81 extern struct timecounter *timecounter;
82 
83 uint64_t tc_getfrequency(void);
84 void      tc_init(struct timecounter *tc);
85 int       tc_detach(struct timecounter *);
86 void      tc_setclock(const struct timespec *ts);
87 void      tc_ticktock(void);
88 void      tc_gonebad(struct timecounter *);
89 
90 #ifdef SYSCTL_DECL
91 SYSCTL_DECL(_kern_timecounter);
92 #endif
93 #endif /* _KERNEL */
94 
95 #endif /* !_SYS_TIMETC_H_ */
96