clocksource.h 9.6 KB

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