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 enum vdso_clock_mode { 32 VDSO_CLOCKMODE_NONE, 33 #ifdef CONFIG_GENERIC_GETTIMEOFDAY 34 VDSO_ARCH_CLOCKMODES, 35 #endif 36 VDSO_CLOCKMODE_MAX, 37 }; 38 39 /** 40 * struct clocksource - hardware abstraction for a free running counter 41 * Provides mostly state-free accessors to the underlying hardware. 42 * This is the structure used for system time. 43 * 44 * @read: Returns a cycle value, passes clocksource as argument 45 * @mask: Bitmask for two's complement 46 * subtraction of non 64 bit counters 47 * @mult: Cycle to nanosecond multiplier 48 * @shift: Cycle to nanosecond divisor (power of two) 49 * @max_idle_ns: Maximum idle time permitted by the clocksource (nsecs) 50 * @maxadj: Maximum adjustment value to mult (~11%) 51 * @archdata: Optional arch-specific data 52 * @max_cycles: Maximum safe cycle value which won't overflow on 53 * multiplication 54 * @name: Pointer to clocksource name 55 * @list: List head for registration (internal) 56 * @rating: Rating value for selection (higher is better) 57 * To avoid rating inflation the following 58 * list should give you a guide as to how 59 * to assign your clocksource a rating 60 * 1-99: Unfit for real use 61 * Only available for bootup and testing purposes. 62 * 100-199: Base level usability. 63 * Functional for real use, but not desired. 64 * 200-299: Good. 65 * A correct and usable clocksource. 66 * 300-399: Desired. 67 * A reasonably fast and accurate clocksource. 68 * 400-499: Perfect 69 * The ideal clocksource. A must-use where 70 * available. 71 * @flags: Flags describing special properties 72 * @enable: Optional function to enable the clocksource 73 * @disable: Optional function to disable the clocksource 74 * @suspend: Optional suspend function for the clocksource 75 * @resume: Optional resume function for the clocksource 76 * @mark_unstable: Optional function to inform the clocksource driver that 77 * the watchdog marked the clocksource unstable 78 * @tick_stable: Optional function called periodically from the watchdog 79 * code to provide stable syncrhonization points 80 * @wd_list: List head to enqueue into the watchdog list (internal) 81 * @cs_last: Last clocksource value for clocksource watchdog 82 * @wd_last: Last watchdog value corresponding to @cs_last 83 * @owner: Module reference, must be set by clocksource in modules 84 * 85 * Note: This struct is not used in hotpathes of the timekeeping code 86 * because the timekeeper caches the hot path fields in its own data 87 * structure, so no cache line alignment is required, 88 * 89 * The pointer to the clocksource itself is handed to the read 90 * callback. If you need extra information there you can wrap struct 91 * clocksource into your own struct. Depending on the amount of 92 * information you need you should consider to cache line align that 93 * structure. 94 */ 95 struct clocksource { 96 u64 (*read)(struct clocksource *cs); 97 u64 mask; 98 u32 mult; 99 u32 shift; 100 u64 max_idle_ns; 101 u32 maxadj; 102 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA 103 struct arch_clocksource_data archdata; 104 #endif 105 u64 max_cycles; 106 const char *name; 107 struct list_head list; 108 int rating; 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 */ 292