xref: /linux-6.15/include/linux/clocksource.h (revision b454cc66)
1 /*  linux/include/linux/clocksource.h
2  *
3  *  This file contains the structure definitions for clocksources.
4  *
5  *  If you are not a clocksource, or timekeeping code, you should
6  *  not be including this file!
7  */
8 #ifndef _LINUX_CLOCKSOURCE_H
9 #define _LINUX_CLOCKSOURCE_H
10 
11 #include <linux/types.h>
12 #include <linux/timex.h>
13 #include <linux/time.h>
14 #include <linux/list.h>
15 #include <asm/div64.h>
16 #include <asm/io.h>
17 
18 /* clocksource cycle base type */
19 typedef u64 cycle_t;
20 
21 /**
22  * struct clocksource - hardware abstraction for a free running counter
23  *	Provides mostly state-free accessors to the underlying hardware.
24  *
25  * @name:		ptr to clocksource name
26  * @list:		list head for registration
27  * @rating:		rating value for selection (higher is better)
28  *			To avoid rating inflation the following
29  *			list should give you a guide as to how
30  *			to assign your clocksource a rating
31  *			1-99: Unfit for real use
32  *				Only available for bootup and testing purposes.
33  *			100-199: Base level usability.
34  *				Functional for real use, but not desired.
35  *			200-299: Good.
36  *				A correct and usable clocksource.
37  *			300-399: Desired.
38  *				A reasonably fast and accurate clocksource.
39  *			400-499: Perfect
40  *				The ideal clocksource. A must-use where
41  *				available.
42  * @read:		returns a cycle value
43  * @mask:		bitmask for two's complement
44  *			subtraction of non 64 bit counters
45  * @mult:		cycle to nanosecond multiplier
46  * @shift:		cycle to nanosecond divisor (power of two)
47  * @update_callback:	called when safe to alter clocksource values
48  * @is_continuous:	defines if clocksource is free-running.
49  * @cycle_interval:	Used internally by timekeeping core, please ignore.
50  * @xtime_interval:	Used internally by timekeeping core, please ignore.
51  */
52 struct clocksource {
53 	char *name;
54 	struct list_head list;
55 	int rating;
56 	cycle_t (*read)(void);
57 	cycle_t mask;
58 	u32 mult;
59 	u32 shift;
60 	int (*update_callback)(void);
61 	int is_continuous;
62 
63 	/* timekeeping specific data, ignore */
64 	cycle_t cycle_last, cycle_interval;
65 	u64 xtime_nsec, xtime_interval;
66 	s64 error;
67 };
68 
69 /* simplify initialization of mask field */
70 #define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
71 
72 /**
73  * clocksource_khz2mult - calculates mult from khz and shift
74  * @khz:		Clocksource frequency in KHz
75  * @shift_constant:	Clocksource shift factor
76  *
77  * Helper functions that converts a khz counter frequency to a timsource
78  * multiplier, given the clocksource shift value
79  */
80 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
81 {
82 	/*  khz = cyc/(Million ns)
83 	 *  mult/2^shift  = ns/cyc
84 	 *  mult = ns/cyc * 2^shift
85 	 *  mult = 1Million/khz * 2^shift
86 	 *  mult = 1000000 * 2^shift / khz
87 	 *  mult = (1000000<<shift) / khz
88 	 */
89 	u64 tmp = ((u64)1000000) << shift_constant;
90 
91 	tmp += khz/2; /* round for do_div */
92 	do_div(tmp, khz);
93 
94 	return (u32)tmp;
95 }
96 
97 /**
98  * clocksource_hz2mult - calculates mult from hz and shift
99  * @hz:			Clocksource frequency in Hz
100  * @shift_constant:	Clocksource shift factor
101  *
102  * Helper functions that converts a hz counter
103  * frequency to a timsource multiplier, given the
104  * clocksource shift value
105  */
106 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
107 {
108 	/*  hz = cyc/(Billion ns)
109 	 *  mult/2^shift  = ns/cyc
110 	 *  mult = ns/cyc * 2^shift
111 	 *  mult = 1Billion/hz * 2^shift
112 	 *  mult = 1000000000 * 2^shift / hz
113 	 *  mult = (1000000000<<shift) / hz
114 	 */
115 	u64 tmp = ((u64)1000000000) << shift_constant;
116 
117 	tmp += hz/2; /* round for do_div */
118 	do_div(tmp, hz);
119 
120 	return (u32)tmp;
121 }
122 
123 /**
124  * clocksource_read: - Access the clocksource's current cycle value
125  * @cs:		pointer to clocksource being read
126  *
127  * Uses the clocksource to return the current cycle_t value
128  */
129 static inline cycle_t clocksource_read(struct clocksource *cs)
130 {
131 	return cs->read();
132 }
133 
134 /**
135  * cyc2ns - converts clocksource cycles to nanoseconds
136  * @cs:		Pointer to clocksource
137  * @cycles:	Cycles
138  *
139  * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
140  *
141  * XXX - This could use some mult_lxl_ll() asm optimization
142  */
143 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
144 {
145 	u64 ret = (u64)cycles;
146 	ret = (ret * cs->mult) >> cs->shift;
147 	return ret;
148 }
149 
150 /**
151  * clocksource_calculate_interval - Calculates a clocksource interval struct
152  *
153  * @c:		Pointer to clocksource.
154  * @length_nsec: Desired interval length in nanoseconds.
155  *
156  * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
157  * pair and interval request.
158  *
159  * Unless you're the timekeeping code, you should not be using this!
160  */
161 static inline void clocksource_calculate_interval(struct clocksource *c,
162 					  	  unsigned long length_nsec)
163 {
164 	u64 tmp;
165 
166 	/* XXX - All of this could use a whole lot of optimization */
167 	tmp = length_nsec;
168 	tmp <<= c->shift;
169 	tmp += c->mult/2;
170 	do_div(tmp, c->mult);
171 
172 	c->cycle_interval = (cycle_t)tmp;
173 	if (c->cycle_interval == 0)
174 		c->cycle_interval = 1;
175 
176 	c->xtime_interval = (u64)c->cycle_interval * c->mult;
177 }
178 
179 
180 /* used to install a new clocksource */
181 int clocksource_register(struct clocksource*);
182 void clocksource_reselect(void);
183 struct clocksource* clocksource_get_next(void);
184 
185 #endif /* _LINUX_CLOCKSOURCE_H */
186