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 * @update_callback: called when safe to alter clocksource values
48 * @is_continuous: defines if clocksource is free-running.
49 * @interval_cycles: Used internally by timekeeping core, please ignore.
50 * @interval_snsecs: Used internally by timekeeping core, please ignore.
54 struct list_head list;
56 cycle_t (*read)(void);
60 int (*update_callback)(void);
63 /* timekeeping specific data, ignore */
64 cycle_t interval_cycles;
70 * clocksource_khz2mult - calculates mult from khz and shift
71 * @khz: Clocksource frequency in KHz
72 * @shift_constant: Clocksource shift factor
74 * Helper functions that converts a khz counter frequency to a timsource
75 * multiplier, given the clocksource shift value
77 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
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
86 u64 tmp = ((u64)1000000) << shift_constant;
88 tmp += khz/2; /* round for do_div */
95 * clocksource_hz2mult - calculates mult from hz and shift
96 * @hz: Clocksource frequency in Hz
97 * @shift_constant: Clocksource shift factor
99 * Helper functions that converts a hz counter
100 * frequency to a timsource multiplier, given the
101 * clocksource shift value
103 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
105 /* hz = cyc/(Billion ns)
106 * mult/2^shift = ns/cyc
107 * mult = ns/cyc * 2^shift
108 * mult = 1Billion/hz * 2^shift
109 * mult = 1000000000 * 2^shift / hz
110 * mult = (1000000000<<shift) / hz
112 u64 tmp = ((u64)1000000000) << shift_constant;
114 tmp += hz/2; /* round for do_div */
121 * clocksource_read: - Access the clocksource's current cycle value
122 * @cs: pointer to clocksource being read
124 * Uses the clocksource to return the current cycle_t value
126 static inline cycle_t clocksource_read(struct clocksource *cs)
132 * cyc2ns - converts clocksource cycles to nanoseconds
133 * @cs: Pointer to clocksource
136 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
138 * XXX - This could use some mult_lxl_ll() asm optimization
140 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
142 u64 ret = (u64)cycles;
143 ret = (ret * cs->mult) >> cs->shift;
148 * clocksource_calculate_interval - Calculates a clocksource interval struct
150 * @c: Pointer to clocksource.
151 * @length_nsec: Desired interval length in nanoseconds.
153 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
154 * pair and interval request.
156 * Unless you're the timekeeping code, you should not be using this!
158 static inline void clocksource_calculate_interval(struct clocksource *c,
159 unsigned long length_nsec)
163 /* XXX - All of this could use a whole lot of optimization */
167 do_div(tmp, c->mult);
169 c->interval_cycles = (cycle_t)tmp;
170 if(c->interval_cycles == 0)
171 c->interval_cycles = 1;
173 c->interval_snsecs = (u64)c->interval_cycles * c->mult;
178 * error_aproximation - calculates an error adjustment for a given error
180 * @error: Error value (unsigned)
181 * @unit: Adjustment unit
183 * For a given error value, this function takes the adjustment unit
184 * and uses binary approximation to return a power of two adjustment value.
186 * This function is only for use by the the make_ntp_adj() function
187 * and you must hold a write on the xtime_lock when calling.
189 static inline int error_aproximation(u64 error, u64 unit)
191 static int saved_adj = 0;
192 u64 adjusted_unit = unit << saved_adj;
194 if (error > (adjusted_unit * 2)) {
195 /* large error, so increment the adjustment factor */
197 } else if (error > adjusted_unit) {
198 /* just right, don't touch it */
199 } else if (saved_adj) {
200 /* small error, so drop the adjustment factor */
210 * make_ntp_adj - Adjusts the specified clocksource for a given error
212 * @clock: Pointer to clock to be adjusted
213 * @cycles_delta: Current unacounted cycle delta
214 * @error: Pointer to current error value
216 * Returns clock shifted nanosecond adjustment to be applied against
217 * the accumulated time value (ie: xtime).
219 * If the error value is large enough, this function calulates the
220 * (power of two) adjustment value, and adjusts the clock's mult and
221 * interval_snsecs values accordingly.
223 * However, since there may be some unaccumulated cycles, to avoid
224 * time inconsistencies we must adjust the accumulation value
227 * This is not very intuitive, so the following proof should help:
228 * The basic timeofday algorithm: base + cycle * mult
230 * new_base + cycle * new_mult = old_base + cycle * old_mult
231 * new_base = old_base + cycle * old_mult - cycle * new_mult
232 * new_base = old_base + cycle * (old_mult - new_mult)
233 * new_base - old_base = cycle * (old_mult - new_mult)
234 * base_delta = cycle * (old_mult - new_mult)
235 * base_delta = cycle * (mult_delta)
237 * Where mult_delta is the adjustment value made to mult
240 static inline s64 make_ntp_adj(struct clocksource *clock,
241 cycles_t cycles_delta, s64* error)
244 if (*error > ((s64)clock->interval_cycles+1)/2) {
245 /* calculate adjustment value */
246 int adjustment = error_aproximation(*error,
247 clock->interval_cycles);
249 clock->mult += 1 << adjustment;
250 clock->interval_snsecs += clock->interval_cycles << adjustment;
252 /* adjust the base and error for the adjustment */
253 ret = -(cycles_delta << adjustment);
254 *error -= clock->interval_cycles << adjustment;
255 /* XXX adj error for cycle_delta offset? */
256 } else if ((-(*error)) > ((s64)clock->interval_cycles+1)/2) {
257 /* calculate adjustment value */
258 int adjustment = error_aproximation(-(*error),
259 clock->interval_cycles);
261 clock->mult -= 1 << adjustment;
262 clock->interval_snsecs -= clock->interval_cycles << adjustment;
264 /* adjust the base and error for the adjustment */
265 ret = cycles_delta << adjustment;
266 *error += clock->interval_cycles << adjustment;
267 /* XXX adj error for cycle_delta offset? */
273 /* used to install a new clocksource */
274 int clocksource_register(struct clocksource*);
275 void clocksource_reselect(void);
276 struct clocksource* clocksource_get_next(void);
278 #endif /* _LINUX_CLOCKSOURCE_H */