clocksource.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <asm/div64.h>
  18. #include <asm/io.h>
  19. /* clocksource cycle base type */
  20. typedef u64 cycle_t;
  21. struct clocksource;
  22. /**
  23. * struct cyclecounter - hardware abstraction for a free running counter
  24. * Provides completely state-free accessors to the underlying hardware.
  25. * Depending on which hardware it reads, the cycle counter may wrap
  26. * around quickly. Locking rules (if necessary) have to be defined
  27. * by the implementor and user of specific instances of this API.
  28. *
  29. * @read: returns the current cycle value
  30. * @mask: bitmask for two's complement
  31. * subtraction of non 64 bit counters,
  32. * see CLOCKSOURCE_MASK() helper macro
  33. * @mult: cycle to nanosecond multiplier
  34. * @shift: cycle to nanosecond divisor (power of two)
  35. */
  36. struct cyclecounter {
  37. cycle_t (*read)(const struct cyclecounter *cc);
  38. cycle_t mask;
  39. u32 mult;
  40. u32 shift;
  41. };
  42. /**
  43. * struct timecounter - layer above a %struct cyclecounter which counts nanoseconds
  44. * Contains the state needed by timecounter_read() to detect
  45. * cycle counter wrap around. Initialize with
  46. * timecounter_init(). Also used to convert cycle counts into the
  47. * corresponding nanosecond counts with timecounter_cyc2time(). Users
  48. * of this code are responsible for initializing the underlying
  49. * cycle counter hardware, locking issues and reading the time
  50. * more often than the cycle counter wraps around. The nanosecond
  51. * counter will only wrap around after ~585 years.
  52. *
  53. * @cc: the cycle counter used by this instance
  54. * @cycle_last: most recent cycle counter value seen by
  55. * timecounter_read()
  56. * @nsec: continuously increasing count
  57. */
  58. struct timecounter {
  59. const struct cyclecounter *cc;
  60. cycle_t cycle_last;
  61. u64 nsec;
  62. };
  63. /**
  64. * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
  65. * @tc: Pointer to cycle counter.
  66. * @cycles: Cycles
  67. *
  68. * XXX - This could use some mult_lxl_ll() asm optimization. Same code
  69. * as in cyc2ns, but with unsigned result.
  70. */
  71. static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
  72. cycle_t cycles)
  73. {
  74. u64 ret = (u64)cycles;
  75. ret = (ret * cc->mult) >> cc->shift;
  76. return ret;
  77. }
  78. /**
  79. * timecounter_init - initialize a time counter
  80. * @tc: Pointer to time counter which is to be initialized/reset
  81. * @cc: A cycle counter, ready to be used.
  82. * @start_tstamp: Arbitrary initial time stamp.
  83. *
  84. * After this call the current cycle register (roughly) corresponds to
  85. * the initial time stamp. Every call to timecounter_read() increments
  86. * the time stamp counter by the number of elapsed nanoseconds.
  87. */
  88. extern void timecounter_init(struct timecounter *tc,
  89. const struct cyclecounter *cc,
  90. u64 start_tstamp);
  91. /**
  92. * timecounter_read - return nanoseconds elapsed since timecounter_init()
  93. * plus the initial time stamp
  94. * @tc: Pointer to time counter.
  95. *
  96. * In other words, keeps track of time since the same epoch as
  97. * the function which generated the initial time stamp.
  98. */
  99. extern u64 timecounter_read(struct timecounter *tc);
  100. /**
  101. * timecounter_cyc2time - convert a cycle counter to same
  102. * time base as values returned by
  103. * timecounter_read()
  104. * @tc: Pointer to time counter.
  105. * @cycle: a value returned by tc->cc->read()
  106. *
  107. * Cycle counts that are converted correctly as long as they
  108. * fall into the interval [-1/2 max cycle count, +1/2 max cycle count],
  109. * with "max cycle count" == cs->mask+1.
  110. *
  111. * This allows conversion of cycle counter values which were generated
  112. * in the past.
  113. */
  114. extern u64 timecounter_cyc2time(struct timecounter *tc,
  115. cycle_t cycle_tstamp);
  116. /**
  117. * struct clocksource - hardware abstraction for a free running counter
  118. * Provides mostly state-free accessors to the underlying hardware.
  119. * This is the structure used for system time.
  120. *
  121. * @name: ptr to clocksource name
  122. * @list: list head for registration
  123. * @rating: rating value for selection (higher is better)
  124. * To avoid rating inflation the following
  125. * list should give you a guide as to how
  126. * to assign your clocksource a rating
  127. * 1-99: Unfit for real use
  128. * Only available for bootup and testing purposes.
  129. * 100-199: Base level usability.
  130. * Functional for real use, but not desired.
  131. * 200-299: Good.
  132. * A correct and usable clocksource.
  133. * 300-399: Desired.
  134. * A reasonably fast and accurate clocksource.
  135. * 400-499: Perfect
  136. * The ideal clocksource. A must-use where
  137. * available.
  138. * @read: returns a cycle value, passes clocksource as argument
  139. * @enable: optional function to enable the clocksource
  140. * @disable: optional function to disable the clocksource
  141. * @mask: bitmask for two's complement
  142. * subtraction of non 64 bit counters
  143. * @mult: cycle to nanosecond multiplier
  144. * @shift: cycle to nanosecond divisor (power of two)
  145. * @max_idle_ns: max idle time permitted by the clocksource (nsecs)
  146. * @flags: flags describing special properties
  147. * @vread: vsyscall based read
  148. * @resume: resume function for the clocksource, if necessary
  149. */
  150. struct clocksource {
  151. /*
  152. * First part of structure is read mostly
  153. */
  154. char *name;
  155. struct list_head list;
  156. int rating;
  157. cycle_t (*read)(struct clocksource *cs);
  158. int (*enable)(struct clocksource *cs);
  159. void (*disable)(struct clocksource *cs);
  160. cycle_t mask;
  161. u32 mult;
  162. u32 shift;
  163. u64 max_idle_ns;
  164. unsigned long flags;
  165. cycle_t (*vread)(void);
  166. void (*resume)(struct clocksource *cs);
  167. #ifdef CONFIG_IA64
  168. void *fsys_mmio; /* used by fsyscall asm code */
  169. #define CLKSRC_FSYS_MMIO_SET(mmio, addr) ((mmio) = (addr))
  170. #else
  171. #define CLKSRC_FSYS_MMIO_SET(mmio, addr) do { } while (0)
  172. #endif
  173. /*
  174. * Second part is written at each timer interrupt
  175. * Keep it in a different cache line to dirty no
  176. * more than one cache line.
  177. */
  178. cycle_t cycle_last ____cacheline_aligned_in_smp;
  179. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  180. /* Watchdog related data, used by the framework */
  181. struct list_head wd_list;
  182. cycle_t wd_last;
  183. #endif
  184. };
  185. /*
  186. * Clock source flags bits::
  187. */
  188. #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
  189. #define CLOCK_SOURCE_MUST_VERIFY 0x02
  190. #define CLOCK_SOURCE_WATCHDOG 0x10
  191. #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
  192. #define CLOCK_SOURCE_UNSTABLE 0x40
  193. /* simplify initialization of mask field */
  194. #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
  195. /**
  196. * clocksource_khz2mult - calculates mult from khz and shift
  197. * @khz: Clocksource frequency in KHz
  198. * @shift_constant: Clocksource shift factor
  199. *
  200. * Helper functions that converts a khz counter frequency to a timsource
  201. * multiplier, given the clocksource shift value
  202. */
  203. static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
  204. {
  205. /* khz = cyc/(Million ns)
  206. * mult/2^shift = ns/cyc
  207. * mult = ns/cyc * 2^shift
  208. * mult = 1Million/khz * 2^shift
  209. * mult = 1000000 * 2^shift / khz
  210. * mult = (1000000<<shift) / khz
  211. */
  212. u64 tmp = ((u64)1000000) << shift_constant;
  213. tmp += khz/2; /* round for do_div */
  214. do_div(tmp, khz);
  215. return (u32)tmp;
  216. }
  217. /**
  218. * clocksource_hz2mult - calculates mult from hz and shift
  219. * @hz: Clocksource frequency in Hz
  220. * @shift_constant: Clocksource shift factor
  221. *
  222. * Helper functions that converts a hz counter
  223. * frequency to a timsource multiplier, given the
  224. * clocksource shift value
  225. */
  226. static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  227. {
  228. /* hz = cyc/(Billion ns)
  229. * mult/2^shift = ns/cyc
  230. * mult = ns/cyc * 2^shift
  231. * mult = 1Billion/hz * 2^shift
  232. * mult = 1000000000 * 2^shift / hz
  233. * mult = (1000000000<<shift) / hz
  234. */
  235. u64 tmp = ((u64)1000000000) << shift_constant;
  236. tmp += hz/2; /* round for do_div */
  237. do_div(tmp, hz);
  238. return (u32)tmp;
  239. }
  240. /**
  241. * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
  242. *
  243. * Converts cycles to nanoseconds, using the given mult and shift.
  244. *
  245. * XXX - This could use some mult_lxl_ll() asm optimization
  246. */
  247. static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift)
  248. {
  249. return ((u64) cycles * mult) >> shift;
  250. }
  251. /* used to install a new clocksource */
  252. extern int clocksource_register(struct clocksource*);
  253. extern void clocksource_unregister(struct clocksource*);
  254. extern void clocksource_touch_watchdog(void);
  255. extern struct clocksource* clocksource_get_next(void);
  256. extern void clocksource_change_rating(struct clocksource *cs, int rating);
  257. extern void clocksource_resume(void);
  258. extern struct clocksource * __init __weak clocksource_default_clock(void);
  259. extern void clocksource_mark_unstable(struct clocksource *cs);
  260. extern void
  261. clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
  262. static inline void
  263. clocksource_calc_mult_shift(struct clocksource *cs, u32 freq, u32 minsec)
  264. {
  265. return clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  266. NSEC_PER_SEC, minsec);
  267. }
  268. #ifdef CONFIG_GENERIC_TIME_VSYSCALL
  269. extern void
  270. update_vsyscall(struct timespec *ts, struct clocksource *c, u32 mult);
  271. extern void update_vsyscall_tz(void);
  272. #else
  273. static inline void
  274. update_vsyscall(struct timespec *ts, struct clocksource *c, u32 mult)
  275. {
  276. }
  277. static inline void update_vsyscall_tz(void)
  278. {
  279. }
  280. #endif
  281. extern void timekeeping_notify(struct clocksource *clock);
  282. #endif /* _LINUX_CLOCKSOURCE_H */