Merge tag 'gpio-fixes-for-v6.9-rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / include / linux / clocksource.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
734efb46 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>
329c8d84 16#include <linux/cache.h>
5d8b34fd 17#include <linux/timer.h>
f1b82746 18#include <linux/init.h>
02fad5e9 19#include <linux/of.h>
b2c67cbe 20#include <linux/clocksource_ids.h>
734efb46 21#include <asm/div64.h>
22#include <asm/io.h>
23
5d8b34fd 24struct clocksource;
09ac369c 25struct module;
734efb46 26
5d51bee7 27#if defined(CONFIG_ARCH_CLOCKSOURCE_DATA) || \
f86fd32d 28 defined(CONFIG_GENERIC_GETTIMEOFDAY)
433bd805 29#include <asm/clocksource.h>
ae7bd11b 30#endif
433bd805 31
14ee2ac6 32#include <vdso/clocksource.h>
5d51bee7 33
734efb46 34/**
35 * struct clocksource - hardware abstraction for a free running counter
36 * Provides mostly state-free accessors to the underlying hardware.
a038a353 37 * This is the structure used for system time.
734efb46 38 *
3bd142a4
TG
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%)
2e27e793
PM
46 * @uncertainty_margin: Maximum uncertainty in nanoseconds per half second.
47 * Zero says to use default WATCHDOG_THRESHOLD.
3bd142a4
TG
48 * @archdata: Optional arch-specific data
49 * @max_cycles: Maximum safe cycle value which won't overflow on
50 * multiplication
51 * @name: Pointer to clocksource name
52 * @list: List head for registration (internal)
53 * @rating: Rating value for selection (higher is better)
734efb46 54 * To avoid rating inflation the following
55 * list should give you a guide as to how
56 * to assign your clocksource a rating
57 * 1-99: Unfit for real use
58 * Only available for bootup and testing purposes.
59 * 100-199: Base level usability.
60 * Functional for real use, but not desired.
61 * 200-299: Good.
62 * A correct and usable clocksource.
63 * 300-399: Desired.
64 * A reasonably fast and accurate clocksource.
65 * 400-499: Perfect
66 * The ideal clocksource. A must-use where
67 * available.
b2c67cbe
TG
68 * @id: Defaults to CSID_GENERIC. The id value is captured
69 * in certain snapshot functions to allow callers to
70 * validate the clocksource from which the snapshot was
71 * taken.
3bd142a4
TG
72 * @flags: Flags describing special properties
73 * @enable: Optional function to enable the clocksource
74 * @disable: Optional function to disable the clocksource
75 * @suspend: Optional suspend function for the clocksource
76 * @resume: Optional resume function for the clocksource
12907fbb
TG
77 * @mark_unstable: Optional function to inform the clocksource driver that
78 * the watchdog marked the clocksource unstable
3bd142a4 79 * @tick_stable: Optional function called periodically from the watchdog
4bf07f65 80 * code to provide stable synchronization points
3bd142a4
TG
81 * @wd_list: List head to enqueue into the watchdog list (internal)
82 * @cs_last: Last clocksource value for clocksource watchdog
83 * @wd_last: Last watchdog value corresponding to @cs_last
84 * @owner: Module reference, must be set by clocksource in modules
09a99820
TG
85 *
86 * Note: This struct is not used in hotpathes of the timekeeping code
87 * because the timekeeper caches the hot path fields in its own data
3bd142a4 88 * structure, so no cache line alignment is required,
09a99820
TG
89 *
90 * The pointer to the clocksource itself is handed to the read
91 * callback. If you need extra information there you can wrap struct
92 * clocksource into your own struct. Depending on the amount of
93 * information you need you should consider to cache line align that
94 * structure.
734efb46 95 */
96struct clocksource {
3bd142a4
TG
97 u64 (*read)(struct clocksource *cs);
98 u64 mask;
99 u32 mult;
100 u32 shift;
101 u64 max_idle_ns;
102 u32 maxadj;
2e27e793 103 u32 uncertainty_margin;
ae7bd11b 104#ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
433bd805 105 struct arch_clocksource_data archdata;
0aa366f3 106#endif
3bd142a4
TG
107 u64 max_cycles;
108 const char *name;
109 struct list_head list;
110 int rating;
b2c67cbe 111 enum clocksource_ids id;
5d51bee7 112 enum vdso_clock_mode vdso_clock_mode;
3bd142a4
TG
113 unsigned long flags;
114
115 int (*enable)(struct clocksource *cs);
116 void (*disable)(struct clocksource *cs);
117 void (*suspend)(struct clocksource *cs);
118 void (*resume)(struct clocksource *cs);
119 void (*mark_unstable)(struct clocksource *cs);
120 void (*tick_stable)(struct clocksource *cs);
5d8b34fd 121
b1b73d09 122 /* private: */
5d8b34fd
TG
123#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
124 /* Watchdog related data, used by the framework */
3bd142a4
TG
125 struct list_head wd_list;
126 u64 cs_last;
127 u64 wd_last;
5d8b34fd 128#endif
3bd142a4 129 struct module *owner;
09a99820 130};
734efb46 131
73b08d2a
TG
132/*
133 * Clock source flags bits::
134 */
5d8b34fd
TG
135#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
136#define CLOCK_SOURCE_MUST_VERIFY 0x02
137
138#define CLOCK_SOURCE_WATCHDOG 0x10
139#define CLOCK_SOURCE_VALID_FOR_HRES 0x20
c55c87c8 140#define CLOCK_SOURCE_UNSTABLE 0x40
5caf4636 141#define CLOCK_SOURCE_SUSPEND_NONSTOP 0x80
332962f2 142#define CLOCK_SOURCE_RESELECT 0x100
7560c02b 143#define CLOCK_SOURCE_VERIFY_PERCPU 0x200
7f9f303a 144/* simplify initialization of mask field */
0773cea3 145#define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)
734efb46 146
7aca0c07
AK
147static inline u32 clocksource_freq2mult(u32 freq, u32 shift_constant, u64 from)
148{
149 /* freq = cyc/from
150 * mult/2^shift = ns/cyc
151 * mult = ns/cyc * 2^shift
152 * mult = from/freq * 2^shift
153 * mult = from * 2^shift / freq
154 * mult = (from<<shift) / freq
155 */
156 u64 tmp = ((u64)from) << shift_constant;
157
158 tmp += freq/2; /* round for do_div */
159 do_div(tmp, freq);
160
161 return (u32)tmp;
162}
163
734efb46 164/**
165 * clocksource_khz2mult - calculates mult from khz and shift
166 * @khz: Clocksource frequency in KHz
167 * @shift_constant: Clocksource shift factor
168 *
169 * Helper functions that converts a khz counter frequency to a timsource
170 * multiplier, given the clocksource shift value
171 */
172static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
173{
7aca0c07 174 return clocksource_freq2mult(khz, shift_constant, NSEC_PER_MSEC);
734efb46 175}
176
177/**
178 * clocksource_hz2mult - calculates mult from hz and shift
179 * @hz: Clocksource frequency in Hz
180 * @shift_constant: Clocksource shift factor
181 *
182 * Helper functions that converts a hz counter
183 * frequency to a timsource multiplier, given the
184 * clocksource shift value
185 */
186static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
187{
7aca0c07 188 return clocksource_freq2mult(hz, shift_constant, NSEC_PER_SEC);
734efb46 189}
190
734efb46 191/**
155ec602 192 * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
b1b73d09
KK
193 * @cycles: cycles
194 * @mult: cycle to nanosecond multiplier
195 * @shift: cycle to nanosecond divisor (power of two)
734efb46 196 *
ec4101e8
CM
197 * Converts clocksource cycles to nanoseconds, using the given @mult and @shift.
198 * The code is optimized for performance and is not intended to work
199 * with absolute clocksource cycles (as those will easily overflow),
200 * but is only intended to be used with relative (delta) clocksource cycles.
734efb46 201 *
202 * XXX - This could use some mult_lxl_ll() asm optimization
203 */
a5a1d1c2 204static inline s64 clocksource_cyc2ns(u64 cycles, u32 mult, u32 shift)
734efb46 205{
155ec602 206 return ((u64) cycles * mult) >> shift;
5eb6d205 207}
208
209
a89c7edb 210extern int clocksource_unregister(struct clocksource*);
7c3078b6 211extern void clocksource_touch_watchdog(void);
92c7e002 212extern void clocksource_change_rating(struct clocksource *cs, int rating);
c54a42b1 213extern void clocksource_suspend(void);
b52f52a0 214extern void clocksource_resume(void);
96a2adbc 215extern struct clocksource * __init clocksource_default_clock(void);
7285dd7f 216extern void clocksource_mark_unstable(struct clocksource *cs);
39232ed5
BW
217extern void
218clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles);
219extern u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 now);
734efb46 220
87d8b9eb 221extern u64
fb82fe2f 222clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);
7d2f944a
TG
223extern void
224clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
225
d7e81c26
JS
226/*
227 * Don't call __clocksource_register_scale directly, use
228 * clocksource_register_hz/khz
229 */
230extern int
231__clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
852db46d 232extern void
fba9e072 233__clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq);
d7e81c26 234
f8935983
JS
235/*
236 * Don't call this unless you are a default clocksource
237 * (AKA: jiffies) and absolutely have to.
238 */
239static inline int __clocksource_register(struct clocksource *cs)
240{
241 return __clocksource_register_scale(cs, 1, 0);
242}
243
d7e81c26
JS
244static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
245{
246 return __clocksource_register_scale(cs, 1, hz);
247}
248
249static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
250{
251 return __clocksource_register_scale(cs, 1000, khz);
252}
253
fba9e072 254static inline void __clocksource_update_freq_hz(struct clocksource *cs, u32 hz)
852db46d 255{
fba9e072 256 __clocksource_update_freq_scale(cs, 1, hz);
852db46d
JS
257}
258
fba9e072 259static inline void __clocksource_update_freq_khz(struct clocksource *cs, u32 khz)
852db46d 260{
fba9e072 261 __clocksource_update_freq_scale(cs, 1000, khz);
852db46d 262}
d7e81c26 263
d67f34c1
TG
264#ifdef CONFIG_ARCH_CLOCKSOURCE_INIT
265extern void clocksource_arch_init(struct clocksource *cs);
266#else
267static inline void clocksource_arch_init(struct clocksource *cs) { }
268#endif
acc9a9dc 269
ba919d1c 270extern int timekeeping_notify(struct clocksource *clock);
75c5158f 271
a5a1d1c2
TG
272extern u64 clocksource_mmio_readl_up(struct clocksource *);
273extern u64 clocksource_mmio_readl_down(struct clocksource *);
274extern u64 clocksource_mmio_readw_up(struct clocksource *);
275extern u64 clocksource_mmio_readw_down(struct clocksource *);
442c8176
RK
276
277extern int clocksource_mmio_init(void __iomem *, const char *,
a5a1d1c2 278 unsigned long, int, unsigned, u64 (*)(struct clocksource *));
442c8176 279
8c414ff3
RK
280extern int clocksource_i8253_init(void);
281
17273395 282#define TIMER_OF_DECLARE(name, compat, fn) \
2fcc112a 283 OF_DECLARE_1_RET(timer, name, compat, fn)
b7c4db86 284
bb0eb050 285#ifdef CONFIG_TIMER_PROBE
ba5d08c0 286extern void timer_probe(void);
e1d7ef1c 287#else
ba5d08c0 288static inline void timer_probe(void) {}
ae278a93
SW
289#endif
290
77d62f53 291#define TIMER_ACPI_DECLARE(name, table_id, fn) \
2fcc112a 292 ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
c625f76a 293
2ed08e4b
FT
294static inline unsigned int clocksource_get_max_watchdog_retry(void)
295{
296 /*
297 * When system is in the boot phase or under heavy workload, there
298 * can be random big latencies during the clocksource/watchdog
299 * read, so allow retries to filter the noise latency. As the
300 * latency's frequency and maximum value goes up with the number of
301 * CPUs, scale the number of retries with the number of online
302 * CPUs.
303 */
304 return (ilog2(num_online_cpus()) / 2) + 1;
305}
306
1253b9b8
PM
307void clocksource_verify_percpu(struct clocksource *cs);
308
734efb46 309#endif /* _LINUX_CLOCKSOURCE_H */