clocksource.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <linux/init.h>
  17. #include <asm/div64.h>
  18. #include <asm/io.h>
  19. struct clocksource;
  20. struct module;
  21. #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
  22. #include <asm/clocksource.h>
  23. #endif
  24. /**
  25. * struct clocksource - hardware abstraction for a free running counter
  26. * Provides mostly state-free accessors to the underlying hardware.
  27. * This is the structure used for system time.
  28. *
  29. * @name: ptr to clocksource name
  30. * @list: list head for registration
  31. * @rating: rating value for selection (higher is better)
  32. * To avoid rating inflation the following
  33. * list should give you a guide as to how
  34. * to assign your clocksource a rating
  35. * 1-99: Unfit for real use
  36. * Only available for bootup and testing purposes.
  37. * 100-199: Base level usability.
  38. * Functional for real use, but not desired.
  39. * 200-299: Good.
  40. * A correct and usable clocksource.
  41. * 300-399: Desired.
  42. * A reasonably fast and accurate clocksource.
  43. * 400-499: Perfect
  44. * The ideal clocksource. A must-use where
  45. * available.
  46. * @read: returns a cycle value, passes clocksource as argument
  47. * @enable: optional function to enable the clocksource
  48. * @disable: optional function to disable the clocksource
  49. * @mask: bitmask for two's complement
  50. * subtraction of non 64 bit counters
  51. * @mult: cycle to nanosecond multiplier
  52. * @shift: cycle to nanosecond divisor (power of two)
  53. * @max_idle_ns: max idle time permitted by the clocksource (nsecs)
  54. * @maxadj: maximum adjustment value to mult (~11%)
  55. * @max_cycles: maximum safe cycle value which won't overflow on multiplication
  56. * @flags: flags describing special properties
  57. * @archdata: arch-specific data
  58. * @suspend: suspend function for the clocksource, if necessary
  59. * @resume: resume function for the clocksource, if necessary
  60. * @owner: module reference, must be set by clocksource in modules
  61. */
  62. struct clocksource {
  63. /*
  64. * Hotpath data, fits in a single cache line when the
  65. * clocksource itself is cacheline aligned.
  66. */
  67. cycle_t (*read)(struct clocksource *cs);
  68. cycle_t mask;
  69. u32 mult;
  70. u32 shift;
  71. u64 max_idle_ns;
  72. u32 maxadj;
  73. #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
  74. struct arch_clocksource_data archdata;
  75. #endif
  76. u64 max_cycles;
  77. const char *name;
  78. struct list_head list;
  79. int rating;
  80. int (*enable)(struct clocksource *cs);
  81. void (*disable)(struct clocksource *cs);
  82. unsigned long flags;
  83. void (*suspend)(struct clocksource *cs);
  84. void (*resume)(struct clocksource *cs);
  85. /* private: */
  86. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  87. /* Watchdog related data, used by the framework */
  88. struct list_head wd_list;
  89. cycle_t cs_last;
  90. cycle_t wd_last;
  91. #endif
  92. struct module *owner;
  93. } ____cacheline_aligned;
  94. /*
  95. * Clock source flags bits::
  96. */
  97. #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
  98. #define CLOCK_SOURCE_MUST_VERIFY 0x02
  99. #define CLOCK_SOURCE_WATCHDOG 0x10
  100. #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
  101. #define CLOCK_SOURCE_UNSTABLE 0x40
  102. #define CLOCK_SOURCE_SUSPEND_NONSTOP 0x80
  103. #define CLOCK_SOURCE_RESELECT 0x100
  104. /* simplify initialization of mask field */
  105. #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
  106. /**
  107. * clocksource_khz2mult - calculates mult from khz and shift
  108. * @khz: Clocksource frequency in KHz
  109. * @shift_constant: Clocksource shift factor
  110. *
  111. * Helper functions that converts a khz counter frequency to a timsource
  112. * multiplier, given the clocksource shift value
  113. */
  114. static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
  115. {
  116. /* khz = cyc/(Million ns)
  117. * mult/2^shift = ns/cyc
  118. * mult = ns/cyc * 2^shift
  119. * mult = 1Million/khz * 2^shift
  120. * mult = 1000000 * 2^shift / khz
  121. * mult = (1000000<<shift) / khz
  122. */
  123. u64 tmp = ((u64)1000000) << shift_constant;
  124. tmp += khz/2; /* round for do_div */
  125. do_div(tmp, khz);
  126. return (u32)tmp;
  127. }
  128. /**
  129. * clocksource_hz2mult - calculates mult from hz and shift
  130. * @hz: Clocksource frequency in Hz
  131. * @shift_constant: Clocksource shift factor
  132. *
  133. * Helper functions that converts a hz counter
  134. * frequency to a timsource multiplier, given the
  135. * clocksource shift value
  136. */
  137. static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  138. {
  139. /* hz = cyc/(Billion ns)
  140. * mult/2^shift = ns/cyc
  141. * mult = ns/cyc * 2^shift
  142. * mult = 1Billion/hz * 2^shift
  143. * mult = 1000000000 * 2^shift / hz
  144. * mult = (1000000000<<shift) / hz
  145. */
  146. u64 tmp = ((u64)1000000000) << shift_constant;
  147. tmp += hz/2; /* round for do_div */
  148. do_div(tmp, hz);
  149. return (u32)tmp;
  150. }
  151. /**
  152. * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
  153. * @cycles: cycles
  154. * @mult: cycle to nanosecond multiplier
  155. * @shift: cycle to nanosecond divisor (power of two)
  156. *
  157. * Converts cycles to nanoseconds, using the given mult and shift.
  158. *
  159. * XXX - This could use some mult_lxl_ll() asm optimization
  160. */
  161. static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift)
  162. {
  163. return ((u64) cycles * mult) >> shift;
  164. }
  165. extern int clocksource_unregister(struct clocksource*);
  166. extern void clocksource_touch_watchdog(void);
  167. extern void clocksource_change_rating(struct clocksource *cs, int rating);
  168. extern void clocksource_suspend(void);
  169. extern void clocksource_resume(void);
  170. extern struct clocksource * __init clocksource_default_clock(void);
  171. extern void clocksource_mark_unstable(struct clocksource *cs);
  172. extern u64
  173. clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);
  174. extern void
  175. clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
  176. /*
  177. * Don't call __clocksource_register_scale directly, use
  178. * clocksource_register_hz/khz
  179. */
  180. extern int
  181. __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
  182. extern void
  183. __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq);
  184. /*
  185. * Don't call this unless you are a default clocksource
  186. * (AKA: jiffies) and absolutely have to.
  187. */
  188. static inline int __clocksource_register(struct clocksource *cs)
  189. {
  190. return __clocksource_register_scale(cs, 1, 0);
  191. }
  192. static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
  193. {
  194. return __clocksource_register_scale(cs, 1, hz);
  195. }
  196. static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
  197. {
  198. return __clocksource_register_scale(cs, 1000, khz);
  199. }
  200. static inline void __clocksource_update_freq_hz(struct clocksource *cs, u32 hz)
  201. {
  202. __clocksource_update_freq_scale(cs, 1, hz);
  203. }
  204. static inline void __clocksource_update_freq_khz(struct clocksource *cs, u32 khz)
  205. {
  206. __clocksource_update_freq_scale(cs, 1000, khz);
  207. }
  208. extern int timekeeping_notify(struct clocksource *clock);
  209. extern cycle_t clocksource_mmio_readl_up(struct clocksource *);
  210. extern cycle_t clocksource_mmio_readl_down(struct clocksource *);
  211. extern cycle_t clocksource_mmio_readw_up(struct clocksource *);
  212. extern cycle_t clocksource_mmio_readw_down(struct clocksource *);
  213. extern int clocksource_mmio_init(void __iomem *, const char *,
  214. unsigned long, int, unsigned, cycle_t (*)(struct clocksource *));
  215. extern int clocksource_i8253_init(void);
  216. #define CLOCKSOURCE_OF_DECLARE(name, compat, fn) \
  217. OF_DECLARE_1(clksrc, name, compat, fn)
  218. #ifdef CONFIG_CLKSRC_PROBE
  219. extern void clocksource_probe(void);
  220. #else
  221. static inline void clocksource_probe(void) {}
  222. #endif
  223. #define CLOCKSOURCE_ACPI_DECLARE(name, table_id, fn) \
  224. ACPI_DECLARE_PROBE_ENTRY(clksrc, name, table_id, 0, NULL, 0, fn)
  225. #endif /* _LINUX_CLOCKSOURCE_H */