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