1 /* linux/include/linux/clocksource.h
3 * This file contains the structure definitions for clocksources.
5 * If you are not a clocksource, or timekeeping code, you should
6 * not be including this file!
8 #ifndef _LINUX_CLOCKSOURCE_H
9 #define _LINUX_CLOCKSOURCE_H
11 #include <linux/types.h>
12 #include <linux/timex.h>
13 #include <linux/time.h>
14 #include <linux/list.h>
15 #include <asm/div64.h>
18 /* clocksource cycle base type */
22 * struct clocksource - hardware abstraction for a free running counter
23 * Provides mostly state-free accessors to the underlying hardware.
25 * @name: ptr to clocksource name
26 * @list: list head for registration
27 * @rating: rating value for selection (higher is better)
28 * To avoid rating inflation the following
29 * list should give you a guide as to how
30 * to assign your clocksource a rating
31 * 1-99: Unfit for real use
32 * Only available for bootup and testing purposes.
33 * 100-199: Base level usability.
34 * Functional for real use, but not desired.
36 * A correct and usable clocksource.
38 * A reasonably fast and accurate clocksource.
40 * The ideal clocksource. A must-use where
42 * @read: returns a cycle value
43 * @mask: bitmask for two's complement
44 * subtraction of non 64 bit counters
45 * @mult: cycle to nanosecond multiplier
46 * @shift: cycle to nanosecond divisor (power of two)
47 * @flags: flags describing special properties
48 * @cycle_interval: Used internally by timekeeping core, please ignore.
49 * @xtime_interval: Used internally by timekeeping core, please ignore.
53 struct list_head list;
55 cycle_t (*read)(void);
61 /* timekeeping specific data, ignore */
62 cycle_t cycle_last, cycle_interval;
63 u64 xtime_nsec, xtime_interval;
68 * Clock source flags bits::
70 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
71 #define CLOCK_SOURCE_MUST_VERIFY 0x02
73 /* simplify initialization of mask field */
74 #define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
77 * clocksource_khz2mult - calculates mult from khz and shift
78 * @khz: Clocksource frequency in KHz
79 * @shift_constant: Clocksource shift factor
81 * Helper functions that converts a khz counter frequency to a timsource
82 * multiplier, given the clocksource shift value
84 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
86 /* khz = cyc/(Million ns)
87 * mult/2^shift = ns/cyc
88 * mult = ns/cyc * 2^shift
89 * mult = 1Million/khz * 2^shift
90 * mult = 1000000 * 2^shift / khz
91 * mult = (1000000<<shift) / khz
93 u64 tmp = ((u64)1000000) << shift_constant;
95 tmp += khz/2; /* round for do_div */
102 * clocksource_hz2mult - calculates mult from hz and shift
103 * @hz: Clocksource frequency in Hz
104 * @shift_constant: Clocksource shift factor
106 * Helper functions that converts a hz counter
107 * frequency to a timsource multiplier, given the
108 * clocksource shift value
110 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
112 /* hz = cyc/(Billion ns)
113 * mult/2^shift = ns/cyc
114 * mult = ns/cyc * 2^shift
115 * mult = 1Billion/hz * 2^shift
116 * mult = 1000000000 * 2^shift / hz
117 * mult = (1000000000<<shift) / hz
119 u64 tmp = ((u64)1000000000) << shift_constant;
121 tmp += hz/2; /* round for do_div */
128 * clocksource_read: - Access the clocksource's current cycle value
129 * @cs: pointer to clocksource being read
131 * Uses the clocksource to return the current cycle_t value
133 static inline cycle_t clocksource_read(struct clocksource *cs)
139 * cyc2ns - converts clocksource cycles to nanoseconds
140 * @cs: Pointer to clocksource
143 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
145 * XXX - This could use some mult_lxl_ll() asm optimization
147 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
149 u64 ret = (u64)cycles;
150 ret = (ret * cs->mult) >> cs->shift;
155 * clocksource_calculate_interval - Calculates a clocksource interval struct
157 * @c: Pointer to clocksource.
158 * @length_nsec: Desired interval length in nanoseconds.
160 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
161 * pair and interval request.
163 * Unless you're the timekeeping code, you should not be using this!
165 static inline void clocksource_calculate_interval(struct clocksource *c,
166 unsigned long length_nsec)
170 /* XXX - All of this could use a whole lot of optimization */
174 do_div(tmp, c->mult);
176 c->cycle_interval = (cycle_t)tmp;
177 if (c->cycle_interval == 0)
178 c->cycle_interval = 1;
180 c->xtime_interval = (u64)c->cycle_interval * c->mult;
184 /* used to install a new clocksource */
185 extern int clocksource_register(struct clocksource*);
186 extern struct clocksource* clocksource_get_next(void);
187 extern void clocksource_change_rating(struct clocksource *cs, int rating);
189 #endif /* _LINUX_CLOCKSOURCE_H */