clocksource.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <asm/div64.h>
  17. #include <asm/io.h>
  18. /* clocksource cycle base type */
  19. typedef u64 cycle_t;
  20. struct clocksource;
  21. /**
  22. * struct cyclecounter - hardware abstraction for a free running counter
  23. * Provides completely state-free accessors to the underlying hardware.
  24. * Depending on which hardware it reads, the cycle counter may wrap
  25. * around quickly. Locking rules (if necessary) have to be defined
  26. * by the implementor and user of specific instances of this API.
  27. *
  28. * @read: returns the current cycle value
  29. * @mask: bitmask for two's complement
  30. * subtraction of non 64 bit counters,
  31. * see CLOCKSOURCE_MASK() helper macro
  32. * @mult: cycle to nanosecond multiplier
  33. * @shift: cycle to nanosecond divisor (power of two)
  34. */
  35. struct cyclecounter {
  36. cycle_t (*read)(const struct cyclecounter *cc);
  37. cycle_t mask;
  38. u32 mult;
  39. u32 shift;
  40. };
  41. /**
  42. * struct timecounter - layer above a %struct cyclecounter which counts nanoseconds
  43. * Contains the state needed by timecounter_read() to detect
  44. * cycle counter wrap around. Initialize with
  45. * timecounter_init(). Also used to convert cycle counts into the
  46. * corresponding nanosecond counts with timecounter_cyc2time(). Users
  47. * of this code are responsible for initializing the underlying
  48. * cycle counter hardware, locking issues and reading the time
  49. * more often than the cycle counter wraps around. The nanosecond
  50. * counter will only wrap around after ~585 years.
  51. *
  52. * @cc: the cycle counter used by this instance
  53. * @cycle_last: most recent cycle counter value seen by
  54. * timecounter_read()
  55. * @nsec: continuously increasing count
  56. */
  57. struct timecounter {
  58. const struct cyclecounter *cc;
  59. cycle_t cycle_last;
  60. u64 nsec;
  61. };
  62. /**
  63. * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
  64. * @tc: Pointer to cycle counter.
  65. * @cycles: Cycles
  66. *
  67. * XXX - This could use some mult_lxl_ll() asm optimization. Same code
  68. * as in cyc2ns, but with unsigned result.
  69. */
  70. static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
  71. cycle_t cycles)
  72. {
  73. u64 ret = (u64)cycles;
  74. ret = (ret * cc->mult) >> cc->shift;
  75. return ret;
  76. }
  77. /**
  78. * timecounter_init - initialize a time counter
  79. * @tc: Pointer to time counter which is to be initialized/reset
  80. * @cc: A cycle counter, ready to be used.
  81. * @start_tstamp: Arbitrary initial time stamp.
  82. *
  83. * After this call the current cycle register (roughly) corresponds to
  84. * the initial time stamp. Every call to timecounter_read() increments
  85. * the time stamp counter by the number of elapsed nanoseconds.
  86. */
  87. extern void timecounter_init(struct timecounter *tc,
  88. const struct cyclecounter *cc,
  89. u64 start_tstamp);
  90. /**
  91. * timecounter_read - return nanoseconds elapsed since timecounter_init()
  92. * plus the initial time stamp
  93. * @tc: Pointer to time counter.
  94. *
  95. * In other words, keeps track of time since the same epoch as
  96. * the function which generated the initial time stamp.
  97. */
  98. extern u64 timecounter_read(struct timecounter *tc);
  99. /**
  100. * timecounter_cyc2time - convert a cycle counter to same
  101. * time base as values returned by
  102. * timecounter_read()
  103. * @tc: Pointer to time counter.
  104. * @cycle: a value returned by tc->cc->read()
  105. *
  106. * Cycle counts that are converted correctly as long as they
  107. * fall into the interval [-1/2 max cycle count, +1/2 max cycle count],
  108. * with "max cycle count" == cs->mask+1.
  109. *
  110. * This allows conversion of cycle counter values which were generated
  111. * in the past.
  112. */
  113. extern u64 timecounter_cyc2time(struct timecounter *tc,
  114. cycle_t cycle_tstamp);
  115. /**
  116. * struct clocksource - hardware abstraction for a free running counter
  117. * Provides mostly state-free accessors to the underlying hardware.
  118. * This is the structure used for system time.
  119. *
  120. * @name: ptr to clocksource name
  121. * @list: list head for registration
  122. * @rating: rating value for selection (higher is better)
  123. * To avoid rating inflation the following
  124. * list should give you a guide as to how
  125. * to assign your clocksource a rating
  126. * 1-99: Unfit for real use
  127. * Only available for bootup and testing purposes.
  128. * 100-199: Base level usability.
  129. * Functional for real use, but not desired.
  130. * 200-299: Good.
  131. * A correct and usable clocksource.
  132. * 300-399: Desired.
  133. * A reasonably fast and accurate clocksource.
  134. * 400-499: Perfect
  135. * The ideal clocksource. A must-use where
  136. * available.
  137. * @read: returns a cycle value, passes clocksource as argument
  138. * @enable: optional function to enable the clocksource
  139. * @disable: optional function to disable the clocksource
  140. * @mask: bitmask for two's complement
  141. * subtraction of non 64 bit counters
  142. * @mult: cycle to nanosecond multiplier (adjusted by NTP)
  143. * @mult_orig: cycle to nanosecond multiplier (unadjusted by NTP)
  144. * @shift: cycle to nanosecond divisor (power of two)
  145. * @flags: flags describing special properties
  146. * @vread: vsyscall based read
  147. * @resume: resume function for the clocksource, if necessary
  148. * @cycle_interval: Used internally by timekeeping core, please ignore.
  149. * @xtime_interval: Used internally by timekeeping core, please ignore.
  150. */
  151. struct clocksource {
  152. /*
  153. * First part of structure is read mostly
  154. */
  155. char *name;
  156. struct list_head list;
  157. int rating;
  158. cycle_t (*read)(struct clocksource *cs);
  159. int (*enable)(struct clocksource *cs);
  160. void (*disable)(struct clocksource *cs);
  161. cycle_t mask;
  162. u32 mult;
  163. u32 mult_orig;
  164. u32 shift;
  165. unsigned long flags;
  166. cycle_t (*vread)(void);
  167. void (*resume)(void);
  168. #ifdef CONFIG_IA64
  169. void *fsys_mmio; /* used by fsyscall asm code */
  170. #define CLKSRC_FSYS_MMIO_SET(mmio, addr) ((mmio) = (addr))
  171. #else
  172. #define CLKSRC_FSYS_MMIO_SET(mmio, addr) do { } while (0)
  173. #endif
  174. /* timekeeping specific data, ignore */
  175. cycle_t cycle_interval;
  176. u64 xtime_interval;
  177. u32 raw_interval;
  178. /*
  179. * Second part is written at each timer interrupt
  180. * Keep it in a different cache line to dirty no
  181. * more than one cache line.
  182. */
  183. cycle_t cycle_last ____cacheline_aligned_in_smp;
  184. u64 xtime_nsec;
  185. s64 error;
  186. struct timespec raw_time;
  187. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  188. /* Watchdog related data, used by the framework */
  189. struct list_head wd_list;
  190. cycle_t wd_last;
  191. #endif
  192. };
  193. extern struct clocksource *clock; /* current clocksource */
  194. /*
  195. * Clock source flags bits::
  196. */
  197. #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
  198. #define CLOCK_SOURCE_MUST_VERIFY 0x02
  199. #define CLOCK_SOURCE_WATCHDOG 0x10
  200. #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
  201. /* simplify initialization of mask field */
  202. #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
  203. /**
  204. * clocksource_khz2mult - calculates mult from khz and shift
  205. * @khz: Clocksource frequency in KHz
  206. * @shift_constant: Clocksource shift factor
  207. *
  208. * Helper functions that converts a khz counter frequency to a timsource
  209. * multiplier, given the clocksource shift value
  210. */
  211. static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
  212. {
  213. /* khz = cyc/(Million ns)
  214. * mult/2^shift = ns/cyc
  215. * mult = ns/cyc * 2^shift
  216. * mult = 1Million/khz * 2^shift
  217. * mult = 1000000 * 2^shift / khz
  218. * mult = (1000000<<shift) / khz
  219. */
  220. u64 tmp = ((u64)1000000) << shift_constant;
  221. tmp += khz/2; /* round for do_div */
  222. do_div(tmp, khz);
  223. return (u32)tmp;
  224. }
  225. /**
  226. * clocksource_hz2mult - calculates mult from hz and shift
  227. * @hz: Clocksource frequency in Hz
  228. * @shift_constant: Clocksource shift factor
  229. *
  230. * Helper functions that converts a hz counter
  231. * frequency to a timsource multiplier, given the
  232. * clocksource shift value
  233. */
  234. static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  235. {
  236. /* hz = cyc/(Billion ns)
  237. * mult/2^shift = ns/cyc
  238. * mult = ns/cyc * 2^shift
  239. * mult = 1Billion/hz * 2^shift
  240. * mult = 1000000000 * 2^shift / hz
  241. * mult = (1000000000<<shift) / hz
  242. */
  243. u64 tmp = ((u64)1000000000) << shift_constant;
  244. tmp += hz/2; /* round for do_div */
  245. do_div(tmp, hz);
  246. return (u32)tmp;
  247. }
  248. /**
  249. * clocksource_read: - Access the clocksource's current cycle value
  250. * @cs: pointer to clocksource being read
  251. *
  252. * Uses the clocksource to return the current cycle_t value
  253. */
  254. static inline cycle_t clocksource_read(struct clocksource *cs)
  255. {
  256. return cs->read(cs);
  257. }
  258. /**
  259. * clocksource_enable: - enable clocksource
  260. * @cs: pointer to clocksource
  261. *
  262. * Enables the specified clocksource. The clocksource callback
  263. * function should start up the hardware and setup mult and field
  264. * members of struct clocksource to reflect hardware capabilities.
  265. */
  266. static inline int clocksource_enable(struct clocksource *cs)
  267. {
  268. int ret = 0;
  269. if (cs->enable)
  270. ret = cs->enable(cs);
  271. /*
  272. * The frequency may have changed while the clocksource
  273. * was disabled. If so the code in ->enable() must update
  274. * the mult value to reflect the new frequency. Make sure
  275. * mult_orig follows this change.
  276. */
  277. cs->mult_orig = cs->mult;
  278. return ret;
  279. }
  280. /**
  281. * clocksource_disable: - disable clocksource
  282. * @cs: pointer to clocksource
  283. *
  284. * Disables the specified clocksource. The clocksource callback
  285. * function should power down the now unused hardware block to
  286. * save power.
  287. */
  288. static inline void clocksource_disable(struct clocksource *cs)
  289. {
  290. /*
  291. * Save mult_orig in mult so clocksource_enable() can
  292. * restore the value regardless if ->enable() updates
  293. * the value of mult or not.
  294. */
  295. cs->mult = cs->mult_orig;
  296. if (cs->disable)
  297. cs->disable(cs);
  298. }
  299. /**
  300. * cyc2ns - converts clocksource cycles to nanoseconds
  301. * @cs: Pointer to clocksource
  302. * @cycles: Cycles
  303. *
  304. * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
  305. *
  306. * XXX - This could use some mult_lxl_ll() asm optimization
  307. */
  308. static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
  309. {
  310. u64 ret = (u64)cycles;
  311. ret = (ret * cs->mult) >> cs->shift;
  312. return ret;
  313. }
  314. /**
  315. * clocksource_calculate_interval - Calculates a clocksource interval struct
  316. *
  317. * @c: Pointer to clocksource.
  318. * @length_nsec: Desired interval length in nanoseconds.
  319. *
  320. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  321. * pair and interval request.
  322. *
  323. * Unless you're the timekeeping code, you should not be using this!
  324. */
  325. static inline void clocksource_calculate_interval(struct clocksource *c,
  326. unsigned long length_nsec)
  327. {
  328. u64 tmp;
  329. /* Do the ns -> cycle conversion first, using original mult */
  330. tmp = length_nsec;
  331. tmp <<= c->shift;
  332. tmp += c->mult_orig/2;
  333. do_div(tmp, c->mult_orig);
  334. c->cycle_interval = (cycle_t)tmp;
  335. if (c->cycle_interval == 0)
  336. c->cycle_interval = 1;
  337. /* Go back from cycles -> shifted ns, this time use ntp adjused mult */
  338. c->xtime_interval = (u64)c->cycle_interval * c->mult;
  339. c->raw_interval = ((u64)c->cycle_interval * c->mult_orig) >> c->shift;
  340. }
  341. /* used to install a new clocksource */
  342. extern int clocksource_register(struct clocksource*);
  343. extern void clocksource_unregister(struct clocksource*);
  344. extern void clocksource_touch_watchdog(void);
  345. extern struct clocksource* clocksource_get_next(void);
  346. extern void clocksource_change_rating(struct clocksource *cs, int rating);
  347. extern void clocksource_resume(void);
  348. #ifdef CONFIG_GENERIC_TIME_VSYSCALL
  349. extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
  350. extern void update_vsyscall_tz(void);
  351. #else
  352. static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
  353. {
  354. }
  355. static inline void update_vsyscall_tz(void)
  356. {
  357. }
  358. #endif
  359. #endif /* _LINUX_CLOCKSOURCE_H */