clocksource.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * @cycle_interval: Used internally by timekeeping core, please ignore.
  48. * @xtime_interval: Used internally by timekeeping core, please ignore.
  49. */
  50. struct clocksource {
  51. char *name;
  52. struct list_head list;
  53. int rating;
  54. cycle_t (*read)(void);
  55. cycle_t mask;
  56. u32 mult;
  57. u32 shift;
  58. unsigned long flags;
  59. /* timekeeping specific data, ignore */
  60. cycle_t cycle_last, cycle_interval;
  61. u64 xtime_nsec, xtime_interval;
  62. s64 error;
  63. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  64. /* Watchdog related data, used by the framework */
  65. struct list_head wd_list;
  66. cycle_t wd_last;
  67. #endif
  68. };
  69. /*
  70. * Clock source flags bits::
  71. */
  72. #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
  73. #define CLOCK_SOURCE_MUST_VERIFY 0x02
  74. #define CLOCK_SOURCE_WATCHDOG 0x10
  75. #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
  76. /* simplify initialization of mask field */
  77. #define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
  78. /**
  79. * clocksource_khz2mult - calculates mult from khz and shift
  80. * @khz: Clocksource frequency in KHz
  81. * @shift_constant: Clocksource shift factor
  82. *
  83. * Helper functions that converts a khz counter frequency to a timsource
  84. * multiplier, given the clocksource shift value
  85. */
  86. static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
  87. {
  88. /* khz = cyc/(Million ns)
  89. * mult/2^shift = ns/cyc
  90. * mult = ns/cyc * 2^shift
  91. * mult = 1Million/khz * 2^shift
  92. * mult = 1000000 * 2^shift / khz
  93. * mult = (1000000<<shift) / khz
  94. */
  95. u64 tmp = ((u64)1000000) << shift_constant;
  96. tmp += khz/2; /* round for do_div */
  97. do_div(tmp, khz);
  98. return (u32)tmp;
  99. }
  100. /**
  101. * clocksource_hz2mult - calculates mult from hz and shift
  102. * @hz: Clocksource frequency in Hz
  103. * @shift_constant: Clocksource shift factor
  104. *
  105. * Helper functions that converts a hz counter
  106. * frequency to a timsource multiplier, given the
  107. * clocksource shift value
  108. */
  109. static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  110. {
  111. /* hz = cyc/(Billion ns)
  112. * mult/2^shift = ns/cyc
  113. * mult = ns/cyc * 2^shift
  114. * mult = 1Billion/hz * 2^shift
  115. * mult = 1000000000 * 2^shift / hz
  116. * mult = (1000000000<<shift) / hz
  117. */
  118. u64 tmp = ((u64)1000000000) << shift_constant;
  119. tmp += hz/2; /* round for do_div */
  120. do_div(tmp, hz);
  121. return (u32)tmp;
  122. }
  123. /**
  124. * clocksource_read: - Access the clocksource's current cycle value
  125. * @cs: pointer to clocksource being read
  126. *
  127. * Uses the clocksource to return the current cycle_t value
  128. */
  129. static inline cycle_t clocksource_read(struct clocksource *cs)
  130. {
  131. return cs->read();
  132. }
  133. /**
  134. * cyc2ns - converts clocksource cycles to nanoseconds
  135. * @cs: Pointer to clocksource
  136. * @cycles: Cycles
  137. *
  138. * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
  139. *
  140. * XXX - This could use some mult_lxl_ll() asm optimization
  141. */
  142. static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
  143. {
  144. u64 ret = (u64)cycles;
  145. ret = (ret * cs->mult) >> cs->shift;
  146. return ret;
  147. }
  148. /**
  149. * clocksource_calculate_interval - Calculates a clocksource interval struct
  150. *
  151. * @c: Pointer to clocksource.
  152. * @length_nsec: Desired interval length in nanoseconds.
  153. *
  154. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  155. * pair and interval request.
  156. *
  157. * Unless you're the timekeeping code, you should not be using this!
  158. */
  159. static inline void clocksource_calculate_interval(struct clocksource *c,
  160. unsigned long length_nsec)
  161. {
  162. u64 tmp;
  163. /* XXX - All of this could use a whole lot of optimization */
  164. tmp = length_nsec;
  165. tmp <<= c->shift;
  166. tmp += c->mult/2;
  167. do_div(tmp, c->mult);
  168. c->cycle_interval = (cycle_t)tmp;
  169. if (c->cycle_interval == 0)
  170. c->cycle_interval = 1;
  171. c->xtime_interval = (u64)c->cycle_interval * c->mult;
  172. }
  173. /* used to install a new clocksource */
  174. extern int clocksource_register(struct clocksource*);
  175. extern struct clocksource* clocksource_get_next(void);
  176. extern void clocksource_change_rating(struct clocksource *cs, int rating);
  177. #endif /* _LINUX_CLOCKSOURCE_H */