GNU Linux-libre 5.10.217-gnu1
[releases.git] / include / linux / clocksource.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*  linux/include/linux/clocksource.h
3  *
4  *  This file contains the structure definitions for clocksources.
5  *
6  *  If you are not a clocksource, or timekeeping code, you should
7  *  not be including this file!
8  */
9 #ifndef _LINUX_CLOCKSOURCE_H
10 #define _LINUX_CLOCKSOURCE_H
11
12 #include <linux/types.h>
13 #include <linux/timex.h>
14 #include <linux/time.h>
15 #include <linux/list.h>
16 #include <linux/cache.h>
17 #include <linux/timer.h>
18 #include <linux/init.h>
19 #include <linux/of.h>
20 #include <asm/div64.h>
21 #include <asm/io.h>
22
23 struct clocksource;
24 struct module;
25
26 #if defined(CONFIG_ARCH_CLOCKSOURCE_DATA) || \
27     defined(CONFIG_GENERIC_GETTIMEOFDAY)
28 #include <asm/clocksource.h>
29 #endif
30
31 #include <vdso/clocksource.h>
32
33 /**
34  * struct clocksource - hardware abstraction for a free running counter
35  *      Provides mostly state-free accessors to the underlying hardware.
36  *      This is the structure used for system time.
37  *
38  * @read:               Returns a cycle value, passes clocksource as argument
39  * @mask:               Bitmask for two's complement
40  *                      subtraction of non 64 bit counters
41  * @mult:               Cycle to nanosecond multiplier
42  * @shift:              Cycle to nanosecond divisor (power of two)
43  * @max_idle_ns:        Maximum idle time permitted by the clocksource (nsecs)
44  * @maxadj:             Maximum adjustment value to mult (~11%)
45  * @uncertainty_margin: Maximum uncertainty in nanoseconds per half second.
46  *                      Zero says to use default WATCHDOG_THRESHOLD.
47  * @archdata:           Optional arch-specific data
48  * @max_cycles:         Maximum safe cycle value which won't overflow on
49  *                      multiplication
50  * @name:               Pointer to clocksource name
51  * @list:               List head for registration (internal)
52  * @rating:             Rating value for selection (higher is better)
53  *                      To avoid rating inflation the following
54  *                      list should give you a guide as to how
55  *                      to assign your clocksource a rating
56  *                      1-99: Unfit for real use
57  *                              Only available for bootup and testing purposes.
58  *                      100-199: Base level usability.
59  *                              Functional for real use, but not desired.
60  *                      200-299: Good.
61  *                              A correct and usable clocksource.
62  *                      300-399: Desired.
63  *                              A reasonably fast and accurate clocksource.
64  *                      400-499: Perfect
65  *                              The ideal clocksource. A must-use where
66  *                              available.
67  * @flags:              Flags describing special properties
68  * @enable:             Optional function to enable the clocksource
69  * @disable:            Optional function to disable the clocksource
70  * @suspend:            Optional suspend function for the clocksource
71  * @resume:             Optional resume function for the clocksource
72  * @mark_unstable:      Optional function to inform the clocksource driver that
73  *                      the watchdog marked the clocksource unstable
74  * @tick_stable:        Optional function called periodically from the watchdog
75  *                      code to provide stable syncrhonization points
76  * @wd_list:            List head to enqueue into the watchdog list (internal)
77  * @cs_last:            Last clocksource value for clocksource watchdog
78  * @wd_last:            Last watchdog value corresponding to @cs_last
79  * @owner:              Module reference, must be set by clocksource in modules
80  *
81  * Note: This struct is not used in hotpathes of the timekeeping code
82  * because the timekeeper caches the hot path fields in its own data
83  * structure, so no cache line alignment is required,
84  *
85  * The pointer to the clocksource itself is handed to the read
86  * callback. If you need extra information there you can wrap struct
87  * clocksource into your own struct. Depending on the amount of
88  * information you need you should consider to cache line align that
89  * structure.
90  */
91 struct clocksource {
92         u64                     (*read)(struct clocksource *cs);
93         u64                     mask;
94         u32                     mult;
95         u32                     shift;
96         u64                     max_idle_ns;
97         u32                     maxadj;
98         u32                     uncertainty_margin;
99 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
100         struct arch_clocksource_data archdata;
101 #endif
102         u64                     max_cycles;
103         const char              *name;
104         struct list_head        list;
105         int                     rating;
106         enum vdso_clock_mode    vdso_clock_mode;
107         unsigned long           flags;
108
109         int                     (*enable)(struct clocksource *cs);
110         void                    (*disable)(struct clocksource *cs);
111         void                    (*suspend)(struct clocksource *cs);
112         void                    (*resume)(struct clocksource *cs);
113         void                    (*mark_unstable)(struct clocksource *cs);
114         void                    (*tick_stable)(struct clocksource *cs);
115
116         /* private: */
117 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
118         /* Watchdog related data, used by the framework */
119         struct list_head        wd_list;
120         u64                     cs_last;
121         u64                     wd_last;
122 #endif
123         struct module           *owner;
124 };
125
126 /*
127  * Clock source flags bits::
128  */
129 #define CLOCK_SOURCE_IS_CONTINUOUS              0x01
130 #define CLOCK_SOURCE_MUST_VERIFY                0x02
131
132 #define CLOCK_SOURCE_WATCHDOG                   0x10
133 #define CLOCK_SOURCE_VALID_FOR_HRES             0x20
134 #define CLOCK_SOURCE_UNSTABLE                   0x40
135 #define CLOCK_SOURCE_SUSPEND_NONSTOP            0x80
136 #define CLOCK_SOURCE_RESELECT                   0x100
137 #define CLOCK_SOURCE_VERIFY_PERCPU              0x200
138 /* simplify initialization of mask field */
139 #define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)
140
141 static inline u32 clocksource_freq2mult(u32 freq, u32 shift_constant, u64 from)
142 {
143         /*  freq = cyc/from
144          *  mult/2^shift  = ns/cyc
145          *  mult = ns/cyc * 2^shift
146          *  mult = from/freq * 2^shift
147          *  mult = from * 2^shift / freq
148          *  mult = (from<<shift) / freq
149          */
150         u64 tmp = ((u64)from) << shift_constant;
151
152         tmp += freq/2; /* round for do_div */
153         do_div(tmp, freq);
154
155         return (u32)tmp;
156 }
157
158 /**
159  * clocksource_khz2mult - calculates mult from khz and shift
160  * @khz:                Clocksource frequency in KHz
161  * @shift_constant:     Clocksource shift factor
162  *
163  * Helper functions that converts a khz counter frequency to a timsource
164  * multiplier, given the clocksource shift value
165  */
166 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
167 {
168         return clocksource_freq2mult(khz, shift_constant, NSEC_PER_MSEC);
169 }
170
171 /**
172  * clocksource_hz2mult - calculates mult from hz and shift
173  * @hz:                 Clocksource frequency in Hz
174  * @shift_constant:     Clocksource shift factor
175  *
176  * Helper functions that converts a hz counter
177  * frequency to a timsource multiplier, given the
178  * clocksource shift value
179  */
180 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
181 {
182         return clocksource_freq2mult(hz, shift_constant, NSEC_PER_SEC);
183 }
184
185 /**
186  * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
187  * @cycles:     cycles
188  * @mult:       cycle to nanosecond multiplier
189  * @shift:      cycle to nanosecond divisor (power of two)
190  *
191  * Converts clocksource cycles to nanoseconds, using the given @mult and @shift.
192  * The code is optimized for performance and is not intended to work
193  * with absolute clocksource cycles (as those will easily overflow),
194  * but is only intended to be used with relative (delta) clocksource cycles.
195  *
196  * XXX - This could use some mult_lxl_ll() asm optimization
197  */
198 static inline s64 clocksource_cyc2ns(u64 cycles, u32 mult, u32 shift)
199 {
200         return ((u64) cycles * mult) >> shift;
201 }
202
203
204 extern int clocksource_unregister(struct clocksource*);
205 extern void clocksource_touch_watchdog(void);
206 extern void clocksource_change_rating(struct clocksource *cs, int rating);
207 extern void clocksource_suspend(void);
208 extern void clocksource_resume(void);
209 extern struct clocksource * __init clocksource_default_clock(void);
210 extern void clocksource_mark_unstable(struct clocksource *cs);
211 extern void
212 clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles);
213 extern u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 now);
214
215 extern u64
216 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);
217 extern void
218 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
219
220 /*
221  * Don't call __clocksource_register_scale directly, use
222  * clocksource_register_hz/khz
223  */
224 extern int
225 __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
226 extern void
227 __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq);
228
229 /*
230  * Don't call this unless you are a default clocksource
231  * (AKA: jiffies) and absolutely have to.
232  */
233 static inline int __clocksource_register(struct clocksource *cs)
234 {
235         return __clocksource_register_scale(cs, 1, 0);
236 }
237
238 static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
239 {
240         return __clocksource_register_scale(cs, 1, hz);
241 }
242
243 static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
244 {
245         return __clocksource_register_scale(cs, 1000, khz);
246 }
247
248 static inline void __clocksource_update_freq_hz(struct clocksource *cs, u32 hz)
249 {
250         __clocksource_update_freq_scale(cs, 1, hz);
251 }
252
253 static inline void __clocksource_update_freq_khz(struct clocksource *cs, u32 khz)
254 {
255         __clocksource_update_freq_scale(cs, 1000, khz);
256 }
257
258 #ifdef CONFIG_ARCH_CLOCKSOURCE_INIT
259 extern void clocksource_arch_init(struct clocksource *cs);
260 #else
261 static inline void clocksource_arch_init(struct clocksource *cs) { }
262 #endif
263
264 extern int timekeeping_notify(struct clocksource *clock);
265
266 extern u64 clocksource_mmio_readl_up(struct clocksource *);
267 extern u64 clocksource_mmio_readl_down(struct clocksource *);
268 extern u64 clocksource_mmio_readw_up(struct clocksource *);
269 extern u64 clocksource_mmio_readw_down(struct clocksource *);
270
271 extern int clocksource_mmio_init(void __iomem *, const char *,
272         unsigned long, int, unsigned, u64 (*)(struct clocksource *));
273
274 extern int clocksource_i8253_init(void);
275
276 #define TIMER_OF_DECLARE(name, compat, fn) \
277         OF_DECLARE_1_RET(timer, name, compat, fn)
278
279 #ifdef CONFIG_TIMER_PROBE
280 extern void timer_probe(void);
281 #else
282 static inline void timer_probe(void) {}
283 #endif
284
285 #define TIMER_ACPI_DECLARE(name, table_id, fn)          \
286         ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
287
288 #endif /* _LINUX_CLOCKSOURCE_H */