clocksource.h 7.8 KB

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