clocksource.h 5.1 KB

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