Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
clocksource.h
Go to the documentation of this file.
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 
11 #include <linux/types.h>
12 #include <linux/timex.h>
13 #include <linux/time.h>
14 #include <linux/list.h>
15 #include <linux/cache.h>
16 #include <linux/timer.h>
17 #include <linux/init.h>
18 #include <asm/div64.h>
19 #include <asm/io.h>
20 
21 /* clocksource cycle base type */
22 typedef u64 cycle_t;
23 struct clocksource;
24 
25 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
26 #include <asm/clocksource.h>
27 #endif
28 
43 struct cyclecounter {
44  cycle_t (*read)(const struct cyclecounter *cc);
48 };
49 
66 struct timecounter {
67  const struct cyclecounter *cc;
70 };
71 
80 static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
81  cycle_t cycles)
82 {
83  u64 ret = (u64)cycles;
84  ret = (ret * cc->mult) >> cc->shift;
85  return ret;
86 }
87 
98 extern void timecounter_init(struct timecounter *tc,
99  const struct cyclecounter *cc,
100  u64 start_tstamp);
101 
110 extern u64 timecounter_read(struct timecounter *tc);
111 
126 extern u64 timecounter_cyc2time(struct timecounter *tc,
127  cycle_t cycle_tstamp);
128 
166 struct clocksource {
167  /*
168  * Hotpath data, fits in a single cache line when the
169  * clocksource itself is cacheline aligned.
170  */
171  cycle_t (*read)(struct clocksource *cs);
178 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
179  struct arch_clocksource_data archdata;
180 #endif
181 
182  const char *name;
183  struct list_head list;
184  int rating;
185  int (*enable)(struct clocksource *cs);
186  void (*disable)(struct clocksource *cs);
187  unsigned long flags;
188  void (*suspend)(struct clocksource *cs);
189  void (*resume)(struct clocksource *cs);
190 
191  /* private: */
192 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
193  /* Watchdog related data, used by the framework */
194  struct list_head wd_list;
195  cycle_t cs_last;
196  cycle_t wd_last;
197 #endif
199 
200 /*
201  * Clock source flags bits::
202  */
203 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
204 #define CLOCK_SOURCE_MUST_VERIFY 0x02
205 
206 #define CLOCK_SOURCE_WATCHDOG 0x10
207 #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
208 #define CLOCK_SOURCE_UNSTABLE 0x40
209 
210 /* simplify initialization of mask field */
211 #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
212 
221 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
222 {
223  /* khz = cyc/(Million ns)
224  * mult/2^shift = ns/cyc
225  * mult = ns/cyc * 2^shift
226  * mult = 1Million/khz * 2^shift
227  * mult = 1000000 * 2^shift / khz
228  * mult = (1000000<<shift) / khz
229  */
230  u64 tmp = ((u64)1000000) << shift_constant;
231 
232  tmp += khz/2; /* round for do_div */
233  do_div(tmp, khz);
234 
235  return (u32)tmp;
236 }
237 
247 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
248 {
249  /* hz = cyc/(Billion ns)
250  * mult/2^shift = ns/cyc
251  * mult = ns/cyc * 2^shift
252  * mult = 1Billion/hz * 2^shift
253  * mult = 1000000000 * 2^shift / hz
254  * mult = (1000000000<<shift) / hz
255  */
256  u64 tmp = ((u64)1000000000) << shift_constant;
257 
258  tmp += hz/2; /* round for do_div */
259  do_div(tmp, hz);
260 
261  return (u32)tmp;
262 }
263 
274 static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift)
275 {
276  return ((u64) cycles * mult) >> shift;
277 }
278 
279 
280 extern int clocksource_register(struct clocksource*);
281 extern void clocksource_unregister(struct clocksource*);
282 extern void clocksource_touch_watchdog(void);
283 extern struct clocksource* clocksource_get_next(void);
284 extern void clocksource_change_rating(struct clocksource *cs, int rating);
285 extern void clocksource_suspend(void);
286 extern void clocksource_resume(void);
287 extern struct clocksource * __init __weak clocksource_default_clock(void);
288 extern void clocksource_mark_unstable(struct clocksource *cs);
289 
290 extern void
291 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
292 
293 /*
294  * Don't call __clocksource_register_scale directly, use
295  * clocksource_register_hz/khz
296  */
297 extern int
299 extern void
301 
302 static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
303 {
304  return __clocksource_register_scale(cs, 1, hz);
305 }
306 
307 static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
308 {
309  return __clocksource_register_scale(cs, 1000, khz);
310 }
311 
312 static inline void __clocksource_updatefreq_hz(struct clocksource *cs, u32 hz)
313 {
315 }
316 
317 static inline void __clocksource_updatefreq_khz(struct clocksource *cs, u32 khz)
318 {
319  __clocksource_updatefreq_scale(cs, 1000, khz);
320 }
321 
322 
323 extern void timekeeping_notify(struct clocksource *clock);
324 
329 
330 extern int clocksource_mmio_init(void __iomem *, const char *,
331  unsigned long, int, unsigned, cycle_t (*)(struct clocksource *));
332 
333 extern int clocksource_i8253_init(void);
334 
335 #endif /* _LINUX_CLOCKSOURCE_H */