clocksource.h 5.0 KB

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