1/*	$OpenBSD: timetc.h,v 1.14 2023/02/04 19:19:35 cheloha Exp $ */
2
3/*
4 * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19/*
20 * If we meet some day, and you think this stuff is worth it, you
21 * can buy me a beer in return. Poul-Henning Kamp
22 */
23
24#ifndef _SYS_TIMETC_H_
25#define	_SYS_TIMETC_H_
26
27#if !defined(_KERNEL) && !defined(_LIBC)
28#error "no user-serviceable parts inside"
29#endif
30
31#include <machine/timetc.h>
32#include <sys/queue.h>
33
34/*-
35 * `struct timecounter' is the interface between the hardware which implements
36 * a timecounter and the MI code which uses this to keep track of time.
37 *
38 * A timecounter is a binary counter which has two properties:
39 *    * it runs at a fixed, known frequency.
40 *    * it has sufficient bits to not roll over in less than approximately
41 *      max(2 msec, 2/HZ seconds).  (The value 2 here is really 1 + delta,
42 *      for some indeterminate value of delta.)
43 */
44
45struct timecounter;
46typedef u_int timecounter_get_t(struct timecounter *);
47
48/*
49 * Locks used to protect struct members in this file:
50 *	I	immutable after initialization
51 *	T	tc_lock
52 *	W	windup_mtx
53 */
54
55struct timecounter {
56	timecounter_get_t	*tc_get_timecount;	/* [I] */
57		/*
58		 * This function reads the counter.  It is not required to
59		 * mask any unimplemented bits out, as long as they are
60		 * constant.
61		 */
62	u_int 			tc_counter_mask;	/* [I] */
63		/* This mask should mask off any unimplemented bits. */
64	u_int64_t		tc_frequency;		/* [I] */
65		/* Frequency of the counter in Hz. */
66	char			*tc_name;		/* [I] */
67		/* Name of the timecounter. */
68	int			tc_quality;		/* [I] */
69		/*
70		 * Used to determine if this timecounter is better than
71		 * another timecounter higher means better.  Negative
72		 * means "only use at explicit request".
73		 */
74	void			*tc_priv;		/* [I] */
75		/* Pointer to the timecounter's private parts. */
76	int			tc_user;		/* [I] */
77		/* Expose this timecounter to userland. */
78	SLIST_ENTRY(timecounter) tc_next;		/* [I] */
79		/* Pointer to the next timecounter. */
80	int64_t			tc_freq_adj;		/* [T,W] */
81		/* Current frequency adjustment. */
82	u_int64_t		tc_precision;		/* [I] */
83		/* Precision of the counter.  Computed in tc_init(). */
84};
85
86struct timekeep {
87	/* set at initialization */
88	uint32_t	tk_version;		/* version number */
89
90	/* timehands members */
91	uint64_t	tk_scale;
92	u_int		tk_offset_count;
93	struct bintime	tk_offset;
94	struct bintime	tk_naptime;
95	struct bintime	tk_boottime;
96	volatile u_int	tk_generation;
97
98	/* timecounter members */
99	int		tk_user;
100	u_int		tk_counter_mask;
101};
102#define TK_VERSION	0
103
104struct rwlock;
105extern struct rwlock tc_lock;
106
107extern struct timecounter *timecounter;
108
109extern struct uvm_object *timekeep_object;
110extern struct timekeep *timekeep;
111
112u_int64_t tc_getfrequency(void);
113u_int64_t tc_getprecision(void);
114void	tc_init(struct timecounter *tc);
115void	tc_reset_quality(struct timecounter *, int);
116void	tc_setclock(const struct timespec *ts);
117void	tc_setrealtimeclock(const struct timespec *ts);
118void	tc_ticktock(void);
119void	inittimecounter(void);
120int	sysctl_tc(int *, u_int, void *, size_t *, void *, size_t);
121void	tc_adjfreq(int64_t *, int64_t *);
122void	tc_adjtime(int64_t *, int64_t *);
123
124#endif /* !_SYS_TIMETC_H_ */
125