clocksource.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/timer.h>
  15. #include <asm/div64.h>
  16. #include <asm/io.h>
  17. /* clocksource cycle base type */
  18. typedef u64 cycle_t;
  19. struct clocksource;
  20. /**
  21. * struct clocksource - hardware abstraction for a free running counter
  22. * Provides mostly state-free accessors to the underlying hardware.
  23. *
  24. * @name: ptr to clocksource name
  25. * @list: list head for registration
  26. * @rating: rating value for selection (higher is better)
  27. * To avoid rating inflation the following
  28. * list should give you a guide as to how
  29. * to assign your clocksource a rating
  30. * 1-99: Unfit for real use
  31. * Only available for bootup and testing purposes.
  32. * 100-199: Base level usability.
  33. * Functional for real use, but not desired.
  34. * 200-299: Good.
  35. * A correct and usable clocksource.
  36. * 300-399: Desired.
  37. * A reasonably fast and accurate clocksource.
  38. * 400-499: Perfect
  39. * The ideal clocksource. A must-use where
  40. * available.
  41. * @read: returns a cycle value
  42. * @mask: bitmask for two's complement
  43. * subtraction of non 64 bit counters
  44. * @mult: cycle to nanosecond multiplier
  45. * @shift: cycle to nanosecond divisor (power of two)
  46. * @flags: flags describing special properties
  47. * @vread: vsyscall based read
  48. * @resume: resume function for the clocksource, if necessary
  49. * @cycle_interval: Used internally by timekeeping core, please ignore.
  50. * @xtime_interval: Used internally by timekeeping core, please ignore.
  51. */
  52. struct clocksource {
  53. char *name;
  54. struct list_head list;
  55. int rating;
  56. cycle_t (*read)(void);
  57. cycle_t mask;
  58. u32 mult;
  59. u32 shift;
  60. unsigned long flags;
  61. cycle_t (*vread)(void);
  62. void (*resume)(void);
  63. /* timekeeping specific data, ignore */
  64. cycle_t cycle_last, cycle_interval;
  65. u64 xtime_nsec, xtime_interval;
  66. s64 error;
  67. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  68. /* Watchdog related data, used by the framework */
  69. struct list_head wd_list;
  70. cycle_t wd_last;
  71. #endif
  72. };
  73. /*
  74. * Clock source flags bits::
  75. */
  76. #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
  77. #define CLOCK_SOURCE_MUST_VERIFY 0x02
  78. #define CLOCK_SOURCE_WATCHDOG 0x10
  79. #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
  80. /* simplify initialization of mask field */
  81. #define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
  82. /**
  83. * clocksource_khz2mult - calculates mult from khz and shift
  84. * @khz: Clocksource frequency in KHz
  85. * @shift_constant: Clocksource shift factor
  86. *
  87. * Helper functions that converts a khz counter frequency to a timsource
  88. * multiplier, given the clocksource shift value
  89. */
  90. static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
  91. {
  92. /* khz = cyc/(Million ns)
  93. * mult/2^shift = ns/cyc
  94. * mult = ns/cyc * 2^shift
  95. * mult = 1Million/khz * 2^shift
  96. * mult = 1000000 * 2^shift / khz
  97. * mult = (1000000<<shift) / khz
  98. */
  99. u64 tmp = ((u64)1000000) << shift_constant;
  100. tmp += khz/2; /* round for do_div */
  101. do_div(tmp, khz);
  102. return (u32)tmp;
  103. }
  104. /**
  105. * clocksource_hz2mult - calculates mult from hz and shift
  106. * @hz: Clocksource frequency in Hz
  107. * @shift_constant: Clocksource shift factor
  108. *
  109. * Helper functions that converts a hz counter
  110. * frequency to a timsource multiplier, given the
  111. * clocksource shift value
  112. */
  113. static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  114. {
  115. /* hz = cyc/(Billion ns)
  116. * mult/2^shift = ns/cyc
  117. * mult = ns/cyc * 2^shift
  118. * mult = 1Billion/hz * 2^shift
  119. * mult = 1000000000 * 2^shift / hz
  120. * mult = (1000000000<<shift) / hz
  121. */
  122. u64 tmp = ((u64)1000000000) << shift_constant;
  123. tmp += hz/2; /* round for do_div */
  124. do_div(tmp, hz);
  125. return (u32)tmp;
  126. }
  127. /**
  128. * clocksource_read: - Access the clocksource's current cycle value
  129. * @cs: pointer to clocksource being read
  130. *
  131. * Uses the clocksource to return the current cycle_t value
  132. */
  133. static inline cycle_t clocksource_read(struct clocksource *cs)
  134. {
  135. return cs->read();
  136. }
  137. /**
  138. * cyc2ns - converts clocksource cycles to nanoseconds
  139. * @cs: Pointer to clocksource
  140. * @cycles: Cycles
  141. *
  142. * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
  143. *
  144. * XXX - This could use some mult_lxl_ll() asm optimization
  145. */
  146. static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
  147. {
  148. u64 ret = (u64)cycles;
  149. ret = (ret * cs->mult) >> cs->shift;
  150. return ret;
  151. }
  152. /**
  153. * clocksource_calculate_interval - Calculates a clocksource interval struct
  154. *
  155. * @c: Pointer to clocksource.
  156. * @length_nsec: Desired interval length in nanoseconds.
  157. *
  158. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  159. * pair and interval request.
  160. *
  161. * Unless you're the timekeeping code, you should not be using this!
  162. */
  163. static inline void clocksource_calculate_interval(struct clocksource *c,
  164. unsigned long length_nsec)
  165. {
  166. u64 tmp;
  167. /* XXX - All of this could use a whole lot of optimization */
  168. tmp = length_nsec;
  169. tmp <<= c->shift;
  170. tmp += c->mult/2;
  171. do_div(tmp, c->mult);
  172. c->cycle_interval = (cycle_t)tmp;
  173. if (c->cycle_interval == 0)
  174. c->cycle_interval = 1;
  175. c->xtime_interval = (u64)c->cycle_interval * c->mult;
  176. }
  177. /* used to install a new clocksource */
  178. extern int clocksource_register(struct clocksource*);
  179. extern struct clocksource* clocksource_get_next(void);
  180. extern void clocksource_change_rating(struct clocksource *cs, int rating);
  181. extern void clocksource_resume(void);
  182. #ifdef CONFIG_GENERIC_TIME_VSYSCALL
  183. extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
  184. #else
  185. static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
  186. {
  187. }
  188. #endif
  189. #endif /* _LINUX_CLOCKSOURCE_H */