clocksource.h 11 KB

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