1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* linux/include/linux/clocksource.h
4 * This file contains the structure definitions for clocksources.
6 * If you are not a clocksource, or timekeeping code, you should
7 * not be including this file!
9 #ifndef _LINUX_CLOCKSOURCE_H
10 #define _LINUX_CLOCKSOURCE_H
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>
20 #include <linux/clocksource_ids.h>
21 #include <asm/div64.h>
27 #if defined(CONFIG_ARCH_CLOCKSOURCE_DATA) || \
28 defined(CONFIG_GENERIC_GETTIMEOFDAY)
29 #include <asm/clocksource.h>
32 #include <vdso/clocksource.h>
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.
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
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.
60 * A correct and usable clocksource.
62 * A reasonably fast and accurate clocksource.
64 * The ideal clocksource. A must-use where
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
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
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,
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
95 u64 (*read)(struct clocksource *cs);
101 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
102 struct arch_clocksource_data archdata;
106 struct list_head list;
108 enum clocksource_ids id;
109 enum vdso_clock_mode vdso_clock_mode;
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);
120 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
121 /* Watchdog related data, used by the framework */
122 struct list_head wd_list;
126 struct module *owner;
130 * Clock source flags bits::
132 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
133 #define CLOCK_SOURCE_MUST_VERIFY 0x02
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
141 /* simplify initialization of mask field */
142 #define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)
144 static inline u32 clocksource_freq2mult(u32 freq, u32 shift_constant, u64 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
153 u64 tmp = ((u64)from) << shift_constant;
155 tmp += freq/2; /* round for do_div */
162 * clocksource_khz2mult - calculates mult from khz and shift
163 * @khz: Clocksource frequency in KHz
164 * @shift_constant: Clocksource shift factor
166 * Helper functions that converts a khz counter frequency to a timsource
167 * multiplier, given the clocksource shift value
169 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
171 return clocksource_freq2mult(khz, shift_constant, NSEC_PER_MSEC);
175 * clocksource_hz2mult - calculates mult from hz and shift
176 * @hz: Clocksource frequency in Hz
177 * @shift_constant: Clocksource shift factor
179 * Helper functions that converts a hz counter
180 * frequency to a timsource multiplier, given the
181 * clocksource shift value
183 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
185 return clocksource_freq2mult(hz, shift_constant, NSEC_PER_SEC);
189 * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
191 * @mult: cycle to nanosecond multiplier
192 * @shift: cycle to nanosecond divisor (power of two)
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.
199 * XXX - This could use some mult_lxl_ll() asm optimization
201 static inline s64 clocksource_cyc2ns(u64 cycles, u32 mult, u32 shift)
203 return ((u64) cycles * mult) >> shift;
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);
215 clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles);
216 extern u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 now);
219 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);
221 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
224 * Don't call __clocksource_register_scale directly, use
225 * clocksource_register_hz/khz
228 __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
230 __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq);
233 * Don't call this unless you are a default clocksource
234 * (AKA: jiffies) and absolutely have to.
236 static inline int __clocksource_register(struct clocksource *cs)
238 return __clocksource_register_scale(cs, 1, 0);
241 static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
243 return __clocksource_register_scale(cs, 1, hz);
246 static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
248 return __clocksource_register_scale(cs, 1000, khz);
251 static inline void __clocksource_update_freq_hz(struct clocksource *cs, u32 hz)
253 __clocksource_update_freq_scale(cs, 1, hz);
256 static inline void __clocksource_update_freq_khz(struct clocksource *cs, u32 khz)
258 __clocksource_update_freq_scale(cs, 1000, khz);
261 #ifdef CONFIG_ARCH_CLOCKSOURCE_INIT
262 extern void clocksource_arch_init(struct clocksource *cs);
264 static inline void clocksource_arch_init(struct clocksource *cs) { }
267 extern int timekeeping_notify(struct clocksource *clock);
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 *);
274 extern int clocksource_mmio_init(void __iomem *, const char *,
275 unsigned long, int, unsigned, u64 (*)(struct clocksource *));
277 extern int clocksource_i8253_init(void);
279 #define TIMER_OF_DECLARE(name, compat, fn) \
280 OF_DECLARE_1_RET(timer, name, compat, fn)
282 #ifdef CONFIG_TIMER_PROBE
283 extern void timer_probe(void);
285 static inline void timer_probe(void) {}
288 #define TIMER_ACPI_DECLARE(name, table_id, fn) \
289 ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
291 #endif /* _LINUX_CLOCKSOURCE_H */