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