clocksource.h 11 KB

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