clocksource.h 9.1 KB

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