clocksource.h 6.0 KB

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