~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/clocksource.h

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php