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 <linux/cache.h>
16 #include <linux/timer.h>
17 #include <asm/div64.h>
20 /* clocksource cycle base type */
25 * struct cyclecounter - hardware abstraction for a free running counter
26 * Provides completely state-free accessors to the underlying hardware.
27 * Depending on which hardware it reads, the cycle counter may wrap
28 * around quickly. Locking rules (if necessary) have to be defined
29 * by the implementor and user of specific instances of this API.
31 * @read: returns the current cycle value
32 * @mask: bitmask for two's complement
33 * subtraction of non 64 bit counters,
34 * see CLOCKSOURCE_MASK() helper macro
35 * @mult: cycle to nanosecond multiplier
36 * @shift: cycle to nanosecond divisor (power of two)
39 cycle_t (*read)(const struct cyclecounter *cc);
46 * struct timecounter - layer above a %struct cyclecounter which counts nanoseconds
47 * Contains the state needed by timecounter_read() to detect
48 * cycle counter wrap around. Initialize with
49 * timecounter_init(). Also used to convert cycle counts into the
50 * corresponding nanosecond counts with timecounter_cyc2time(). Users
51 * of this code are responsible for initializing the underlying
52 * cycle counter hardware, locking issues and reading the time
53 * more often than the cycle counter wraps around. The nanosecond
54 * counter will only wrap around after ~585 years.
56 * @cc: the cycle counter used by this instance
57 * @cycle_last: most recent cycle counter value seen by
59 * @nsec: continuously increasing count
62 const struct cyclecounter *cc;
68 * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
69 * @tc: Pointer to cycle counter.
72 * XXX - This could use some mult_lxl_ll() asm optimization. Same code
73 * as in cyc2ns, but with unsigned result.
75 static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
78 u64 ret = (u64)cycles;
79 ret = (ret * cc->mult) >> cc->shift;
84 * timecounter_init - initialize a time counter
85 * @tc: Pointer to time counter which is to be initialized/reset
86 * @cc: A cycle counter, ready to be used.
87 * @start_tstamp: Arbitrary initial time stamp.
89 * After this call the current cycle register (roughly) corresponds to
90 * the initial time stamp. Every call to timecounter_read() increments
91 * the time stamp counter by the number of elapsed nanoseconds.
93 extern void timecounter_init(struct timecounter *tc,
94 const struct cyclecounter *cc,
98 * timecounter_read - return nanoseconds elapsed since timecounter_init()
99 * plus the initial time stamp
100 * @tc: Pointer to time counter.
102 * In other words, keeps track of time since the same epoch as
103 * the function which generated the initial time stamp.
105 extern u64 timecounter_read(struct timecounter *tc);
108 * timecounter_cyc2time - convert a cycle counter to same
109 * time base as values returned by
111 * @tc: Pointer to time counter.
112 * @cycle: a value returned by tc->cc->read()
114 * Cycle counts that are converted correctly as long as they
115 * fall into the interval [-1/2 max cycle count, +1/2 max cycle count],
116 * with "max cycle count" == cs->mask+1.
118 * This allows conversion of cycle counter values which were generated
121 extern u64 timecounter_cyc2time(struct timecounter *tc,
122 cycle_t cycle_tstamp);
125 * struct clocksource - hardware abstraction for a free running counter
126 * Provides mostly state-free accessors to the underlying hardware.
127 * This is the structure used for system time.
129 * @name: ptr to clocksource name
130 * @list: list head for registration
131 * @rating: rating value for selection (higher is better)
132 * To avoid rating inflation the following
133 * list should give you a guide as to how
134 * to assign your clocksource a rating
135 * 1-99: Unfit for real use
136 * Only available for bootup and testing purposes.
137 * 100-199: Base level usability.
138 * Functional for real use, but not desired.
140 * A correct and usable clocksource.
142 * A reasonably fast and accurate clocksource.
144 * The ideal clocksource. A must-use where
146 * @read: returns a cycle value, passes clocksource as argument
147 * @enable: optional function to enable the clocksource
148 * @disable: optional function to disable the clocksource
149 * @mask: bitmask for two's complement
150 * subtraction of non 64 bit counters
151 * @mult: cycle to nanosecond multiplier (adjusted by NTP)
152 * @mult_orig: cycle to nanosecond multiplier (unadjusted by NTP)
153 * @shift: cycle to nanosecond divisor (power of two)
154 * @flags: flags describing special properties
155 * @vread: vsyscall based read
156 * @resume: resume function for the clocksource, if necessary
157 * @cycle_interval: Used internally by timekeeping core, please ignore.
158 * @xtime_interval: Used internally by timekeeping core, please ignore.
162 * First part of structure is read mostly
165 struct list_head list;
167 cycle_t (*read)(struct clocksource *cs);
168 int (*enable)(struct clocksource *cs);
169 void (*disable)(struct clocksource *cs);
175 cycle_t (*vread)(void);
176 void (*resume)(void);
178 void *fsys_mmio; /* used by fsyscall asm code */
179 #define CLKSRC_FSYS_MMIO_SET(mmio, addr) ((mmio) = (addr))
181 #define CLKSRC_FSYS_MMIO_SET(mmio, addr) do { } while (0)
184 /* timekeeping specific data, ignore */
185 cycle_t cycle_interval;
189 * Second part is written at each timer interrupt
190 * Keep it in a different cache line to dirty no
191 * more than one cache line.
193 cycle_t cycle_last ____cacheline_aligned_in_smp;
196 struct timespec raw_time;
198 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
199 /* Watchdog related data, used by the framework */
200 struct list_head wd_list;
205 extern struct clocksource *clock; /* current clocksource */
208 * Clock source flags bits::
210 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
211 #define CLOCK_SOURCE_MUST_VERIFY 0x02
213 #define CLOCK_SOURCE_WATCHDOG 0x10
214 #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
216 /* simplify initialization of mask field */
217 #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
220 * clocksource_khz2mult - calculates mult from khz and shift
221 * @khz: Clocksource frequency in KHz
222 * @shift_constant: Clocksource shift factor
224 * Helper functions that converts a khz counter frequency to a timsource
225 * multiplier, given the clocksource shift value
227 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
229 /* khz = cyc/(Million ns)
230 * mult/2^shift = ns/cyc
231 * mult = ns/cyc * 2^shift
232 * mult = 1Million/khz * 2^shift
233 * mult = 1000000 * 2^shift / khz
234 * mult = (1000000<<shift) / khz
236 u64 tmp = ((u64)1000000) << shift_constant;
238 tmp += khz/2; /* round for do_div */
245 * clocksource_hz2mult - calculates mult from hz and shift
246 * @hz: Clocksource frequency in Hz
247 * @shift_constant: Clocksource shift factor
249 * Helper functions that converts a hz counter
250 * frequency to a timsource multiplier, given the
251 * clocksource shift value
253 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
255 /* hz = cyc/(Billion ns)
256 * mult/2^shift = ns/cyc
257 * mult = ns/cyc * 2^shift
258 * mult = 1Billion/hz * 2^shift
259 * mult = 1000000000 * 2^shift / hz
260 * mult = (1000000000<<shift) / hz
262 u64 tmp = ((u64)1000000000) << shift_constant;
264 tmp += hz/2; /* round for do_div */
271 * clocksource_read: - Access the clocksource's current cycle value
272 * @cs: pointer to clocksource being read
274 * Uses the clocksource to return the current cycle_t value
276 static inline cycle_t clocksource_read(struct clocksource *cs)
282 * clocksource_enable: - enable clocksource
283 * @cs: pointer to clocksource
285 * Enables the specified clocksource. The clocksource callback
286 * function should start up the hardware and setup mult and field
287 * members of struct clocksource to reflect hardware capabilities.
289 static inline int clocksource_enable(struct clocksource *cs)
291 return cs->enable ? cs->enable(cs) : 0;
295 * clocksource_disable: - disable clocksource
296 * @cs: pointer to clocksource
298 * Disables the specified clocksource. The clocksource callback
299 * function should power down the now unused hardware block to
302 static inline void clocksource_disable(struct clocksource *cs)
309 * cyc2ns - converts clocksource cycles to nanoseconds
310 * @cs: Pointer to clocksource
313 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
315 * XXX - This could use some mult_lxl_ll() asm optimization
317 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
319 u64 ret = (u64)cycles;
320 ret = (ret * cs->mult) >> cs->shift;
325 * clocksource_calculate_interval - Calculates a clocksource interval struct
327 * @c: Pointer to clocksource.
328 * @length_nsec: Desired interval length in nanoseconds.
330 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
331 * pair and interval request.
333 * Unless you're the timekeeping code, you should not be using this!
335 static inline void clocksource_calculate_interval(struct clocksource *c,
336 unsigned long length_nsec)
340 /* Do the ns -> cycle conversion first, using original mult */
343 tmp += c->mult_orig/2;
344 do_div(tmp, c->mult_orig);
346 c->cycle_interval = (cycle_t)tmp;
347 if (c->cycle_interval == 0)
348 c->cycle_interval = 1;
350 /* Go back from cycles -> shifted ns, this time use ntp adjused mult */
351 c->xtime_interval = (u64)c->cycle_interval * c->mult;
352 c->raw_interval = ((u64)c->cycle_interval * c->mult_orig) >> c->shift;
356 /* used to install a new clocksource */
357 extern int clocksource_register(struct clocksource*);
358 extern void clocksource_unregister(struct clocksource*);
359 extern void clocksource_touch_watchdog(void);
360 extern struct clocksource* clocksource_get_next(void);
361 extern void clocksource_change_rating(struct clocksource *cs, int rating);
362 extern void clocksource_resume(void);
364 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
365 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
366 extern void update_vsyscall_tz(void);
368 static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
372 static inline void update_vsyscall_tz(void)
377 #endif /* _LINUX_CLOCKSOURCE_H */