clocksource.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * @suspend: suspend function for the clocksource, if necessary
  149. * @resume: resume function for the clocksource, if necessary
  150. */
  151. struct clocksource {
  152. /*
  153. * Hotpath data, fits in a single cache line when the
  154. * clocksource itself is cacheline aligned.
  155. */
  156. cycle_t (*read)(struct clocksource *cs);
  157. cycle_t cycle_last;
  158. cycle_t mask;
  159. u32 mult;
  160. u32 shift;
  161. u64 max_idle_ns;
  162. #ifdef CONFIG_IA64
  163. void *fsys_mmio; /* used by fsyscall asm code */
  164. #define CLKSRC_FSYS_MMIO_SET(mmio, addr) ((mmio) = (addr))
  165. #else
  166. #define CLKSRC_FSYS_MMIO_SET(mmio, addr) do { } while (0)
  167. #endif
  168. const char *name;
  169. struct list_head list;
  170. int rating;
  171. cycle_t (*vread)(void);
  172. int (*enable)(struct clocksource *cs);
  173. void (*disable)(struct clocksource *cs);
  174. unsigned long flags;
  175. void (*suspend)(struct clocksource *cs);
  176. void (*resume)(struct clocksource *cs);
  177. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  178. /* Watchdog related data, used by the framework */
  179. struct list_head wd_list;
  180. cycle_t wd_last;
  181. #endif
  182. } ____cacheline_aligned;
  183. /*
  184. * Clock source flags bits::
  185. */
  186. #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
  187. #define CLOCK_SOURCE_MUST_VERIFY 0x02
  188. #define CLOCK_SOURCE_WATCHDOG 0x10
  189. #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
  190. #define CLOCK_SOURCE_UNSTABLE 0x40
  191. /* simplify initialization of mask field */
  192. #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
  193. /**
  194. * clocksource_khz2mult - calculates mult from khz and shift
  195. * @khz: Clocksource frequency in KHz
  196. * @shift_constant: Clocksource shift factor
  197. *
  198. * Helper functions that converts a khz counter frequency to a timsource
  199. * multiplier, given the clocksource shift value
  200. */
  201. static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
  202. {
  203. /* khz = cyc/(Million ns)
  204. * mult/2^shift = ns/cyc
  205. * mult = ns/cyc * 2^shift
  206. * mult = 1Million/khz * 2^shift
  207. * mult = 1000000 * 2^shift / khz
  208. * mult = (1000000<<shift) / khz
  209. */
  210. u64 tmp = ((u64)1000000) << shift_constant;
  211. tmp += khz/2; /* round for do_div */
  212. do_div(tmp, khz);
  213. return (u32)tmp;
  214. }
  215. /**
  216. * clocksource_hz2mult - calculates mult from hz and shift
  217. * @hz: Clocksource frequency in Hz
  218. * @shift_constant: Clocksource shift factor
  219. *
  220. * Helper functions that converts a hz counter
  221. * frequency to a timsource multiplier, given the
  222. * clocksource shift value
  223. */
  224. static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  225. {
  226. /* hz = cyc/(Billion ns)
  227. * mult/2^shift = ns/cyc
  228. * mult = ns/cyc * 2^shift
  229. * mult = 1Billion/hz * 2^shift
  230. * mult = 1000000000 * 2^shift / hz
  231. * mult = (1000000000<<shift) / hz
  232. */
  233. u64 tmp = ((u64)1000000000) << shift_constant;
  234. tmp += hz/2; /* round for do_div */
  235. do_div(tmp, hz);
  236. return (u32)tmp;
  237. }
  238. /**
  239. * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
  240. *
  241. * Converts cycles to nanoseconds, using the given mult and shift.
  242. *
  243. * XXX - This could use some mult_lxl_ll() asm optimization
  244. */
  245. static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift)
  246. {
  247. return ((u64) cycles * mult) >> shift;
  248. }
  249. extern int clocksource_register(struct clocksource*);
  250. extern void clocksource_unregister(struct clocksource*);
  251. extern void clocksource_touch_watchdog(void);
  252. extern struct clocksource* clocksource_get_next(void);
  253. extern void clocksource_change_rating(struct clocksource *cs, int rating);
  254. extern void clocksource_suspend(void);
  255. extern void clocksource_resume(void);
  256. extern struct clocksource * __init __weak clocksource_default_clock(void);
  257. extern void clocksource_mark_unstable(struct clocksource *cs);
  258. extern void
  259. clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
  260. /*
  261. * Don't call __clocksource_register_scale directly, use
  262. * clocksource_register_hz/khz
  263. */
  264. extern int
  265. __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
  266. extern void
  267. __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq);
  268. static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
  269. {
  270. return __clocksource_register_scale(cs, 1, hz);
  271. }
  272. static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
  273. {
  274. return __clocksource_register_scale(cs, 1000, khz);
  275. }
  276. static inline void __clocksource_updatefreq_hz(struct clocksource *cs, u32 hz)
  277. {
  278. __clocksource_updatefreq_scale(cs, 1, hz);
  279. }
  280. static inline void __clocksource_updatefreq_khz(struct clocksource *cs, u32 khz)
  281. {
  282. __clocksource_updatefreq_scale(cs, 1000, khz);
  283. }
  284. static inline void
  285. clocksource_calc_mult_shift(struct clocksource *cs, u32 freq, u32 minsec)
  286. {
  287. return clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  288. NSEC_PER_SEC, minsec);
  289. }
  290. #ifdef CONFIG_GENERIC_TIME_VSYSCALL
  291. extern void
  292. update_vsyscall(struct timespec *ts, struct timespec *wtm,
  293. struct clocksource *c, u32 mult);
  294. extern void update_vsyscall_tz(void);
  295. #else
  296. static inline void
  297. update_vsyscall(struct timespec *ts, struct timespec *wtm,
  298. struct clocksource *c, u32 mult)
  299. {
  300. }
  301. static inline void update_vsyscall_tz(void)
  302. {
  303. }
  304. #endif
  305. extern void timekeeping_notify(struct clocksource *clock);
  306. extern int clocksource_i8253_init(void);
  307. #endif /* _LINUX_CLOCKSOURCE_H */