clocksource.h 11 KB

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