blob: 573819ef4cc076bd56f16ce0d7414e1225df906f [file] [log] [blame]
john stultz734efb42006-06-26 00:25:05 -07001/* 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>
Eric Dumazet329c8d82007-05-08 00:27:57 -070015#include <linux/cache.h>
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080016#include <linux/timer.h>
john stultz734efb42006-06-26 00:25:05 -070017#include <asm/div64.h>
18#include <asm/io.h>
19
20/* clocksource cycle base type */
21typedef u64 cycle_t;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080022struct clocksource;
john stultz734efb42006-06-26 00:25:05 -070023
24/**
Patrick Ohlya038a352009-02-12 05:03:34 +000025 * 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.
30 *
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)
37 */
38struct cyclecounter {
39 cycle_t (*read)(const struct cyclecounter *cc);
40 cycle_t mask;
41 u32 mult;
42 u32 shift;
43};
44
45/**
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.
55 *
56 * @cc: the cycle counter used by this instance
57 * @cycle_last: most recent cycle counter value seen by
58 * timecounter_read()
59 * @nsec: continuously increasing count
60 */
61struct timecounter {
62 const struct cyclecounter *cc;
63 cycle_t cycle_last;
64 u64 nsec;
65};
66
67/**
68 * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
69 * @tc: Pointer to cycle counter.
70 * @cycles: Cycles
71 *
72 * XXX - This could use some mult_lxl_ll() asm optimization. Same code
73 * as in cyc2ns, but with unsigned result.
74 */
75static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
76 cycle_t cycles)
77{
78 u64 ret = (u64)cycles;
79 ret = (ret * cc->mult) >> cc->shift;
80 return ret;
81}
82
83/**
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.
88 *
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.
92 */
93extern void timecounter_init(struct timecounter *tc,
94 const struct cyclecounter *cc,
95 u64 start_tstamp);
96
97/**
98 * timecounter_read - return nanoseconds elapsed since timecounter_init()
99 * plus the initial time stamp
100 * @tc: Pointer to time counter.
101 *
102 * In other words, keeps track of time since the same epoch as
103 * the function which generated the initial time stamp.
104 */
105extern u64 timecounter_read(struct timecounter *tc);
106
107/**
108 * timecounter_cyc2time - convert a cycle counter to same
109 * time base as values returned by
110 * timecounter_read()
111 * @tc: Pointer to time counter.
112 * @cycle: a value returned by tc->cc->read()
113 *
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.
117 *
118 * This allows conversion of cycle counter values which were generated
119 * in the past.
120 */
121extern u64 timecounter_cyc2time(struct timecounter *tc,
122 cycle_t cycle_tstamp);
123
124/**
john stultz734efb42006-06-26 00:25:05 -0700125 * struct clocksource - hardware abstraction for a free running counter
126 * Provides mostly state-free accessors to the underlying hardware.
Patrick Ohlya038a352009-02-12 05:03:34 +0000127 * This is the structure used for system time.
john stultz734efb42006-06-26 00:25:05 -0700128 *
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.
139 * 200-299: Good.
140 * A correct and usable clocksource.
141 * 300-399: Desired.
142 * A reasonably fast and accurate clocksource.
143 * 400-499: Perfect
144 * The ideal clocksource. A must-use where
145 * available.
146 * @read: returns a cycle value
147 * @mask: bitmask for two's complement
148 * subtraction of non 64 bit counters
John Stultz1aa5dfb2008-08-20 16:37:28 -0700149 * @mult: cycle to nanosecond multiplier (adjusted by NTP)
150 * @mult_orig: cycle to nanosecond multiplier (unadjusted by NTP)
john stultz734efb42006-06-26 00:25:05 -0700151 * @shift: cycle to nanosecond divisor (power of two)
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800152 * @flags: flags describing special properties
john stultzacc9a9d2007-02-16 01:28:17 -0800153 * @vread: vsyscall based read
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700154 * @resume: resume function for the clocksource, if necessary
Roman Zippel19923c12006-06-26 00:25:18 -0700155 * @cycle_interval: Used internally by timekeeping core, please ignore.
156 * @xtime_interval: Used internally by timekeeping core, please ignore.
john stultz734efb42006-06-26 00:25:05 -0700157 */
158struct clocksource {
Eric Dumazet329c8d82007-05-08 00:27:57 -0700159 /*
160 * First part of structure is read mostly
161 */
john stultz734efb42006-06-26 00:25:05 -0700162 char *name;
163 struct list_head list;
164 int rating;
165 cycle_t (*read)(void);
166 cycle_t mask;
167 u32 mult;
John Stultz1aa5dfb2008-08-20 16:37:28 -0700168 u32 mult_orig;
john stultz734efb42006-06-26 00:25:05 -0700169 u32 shift;
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800170 unsigned long flags;
john stultzacc9a9d2007-02-16 01:28:17 -0800171 cycle_t (*vread)(void);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700172 void (*resume)(void);
Tony Luck0aa366f2007-07-20 11:22:30 -0700173#ifdef CONFIG_IA64
174 void *fsys_mmio; /* used by fsyscall asm code */
175#define CLKSRC_FSYS_MMIO_SET(mmio, addr) ((mmio) = (addr))
176#else
177#define CLKSRC_FSYS_MMIO_SET(mmio, addr) do { } while (0)
178#endif
john stultz734efb42006-06-26 00:25:05 -0700179
180 /* timekeeping specific data, ignore */
Eric Dumazet329c8d82007-05-08 00:27:57 -0700181 cycle_t cycle_interval;
182 u64 xtime_interval;
John Stultz2d422442008-08-20 16:37:30 -0700183 u32 raw_interval;
Eric Dumazet329c8d82007-05-08 00:27:57 -0700184 /*
185 * Second part is written at each timer interrupt
186 * Keep it in a different cache line to dirty no
187 * more than one cache line.
188 */
189 cycle_t cycle_last ____cacheline_aligned_in_smp;
190 u64 xtime_nsec;
Roman Zippel19923c12006-06-26 00:25:18 -0700191 s64 error;
John Stultz2d422442008-08-20 16:37:30 -0700192 struct timespec raw_time;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800193
194#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
195 /* Watchdog related data, used by the framework */
196 struct list_head wd_list;
197 cycle_t wd_last;
198#endif
john stultz734efb42006-06-26 00:25:05 -0700199};
200
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700201extern struct clocksource *clock; /* current clocksource */
202
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800203/*
204 * Clock source flags bits::
205 */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800206#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
207#define CLOCK_SOURCE_MUST_VERIFY 0x02
208
209#define CLOCK_SOURCE_WATCHDOG 0x10
210#define CLOCK_SOURCE_VALID_FOR_HRES 0x20
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800211
Jim Cromie7f9f3032006-06-26 00:25:15 -0700212/* simplify initialization of mask field */
Atsushi Nemoto1d76c262008-01-30 13:30:01 +0100213#define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
john stultz734efb42006-06-26 00:25:05 -0700214
215/**
216 * clocksource_khz2mult - calculates mult from khz and shift
217 * @khz: Clocksource frequency in KHz
218 * @shift_constant: Clocksource shift factor
219 *
220 * Helper functions that converts a khz counter frequency to a timsource
221 * multiplier, given the clocksource shift value
222 */
223static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
224{
225 /* khz = cyc/(Million ns)
226 * mult/2^shift = ns/cyc
227 * mult = ns/cyc * 2^shift
228 * mult = 1Million/khz * 2^shift
229 * mult = 1000000 * 2^shift / khz
230 * mult = (1000000<<shift) / khz
231 */
232 u64 tmp = ((u64)1000000) << shift_constant;
233
234 tmp += khz/2; /* round for do_div */
235 do_div(tmp, khz);
236
237 return (u32)tmp;
238}
239
240/**
241 * clocksource_hz2mult - calculates mult from hz and shift
242 * @hz: Clocksource frequency in Hz
243 * @shift_constant: Clocksource shift factor
244 *
245 * Helper functions that converts a hz counter
246 * frequency to a timsource multiplier, given the
247 * clocksource shift value
248 */
249static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
250{
251 /* hz = cyc/(Billion ns)
252 * mult/2^shift = ns/cyc
253 * mult = ns/cyc * 2^shift
254 * mult = 1Billion/hz * 2^shift
255 * mult = 1000000000 * 2^shift / hz
256 * mult = (1000000000<<shift) / hz
257 */
258 u64 tmp = ((u64)1000000000) << shift_constant;
259
260 tmp += hz/2; /* round for do_div */
261 do_div(tmp, hz);
262
263 return (u32)tmp;
264}
265
266/**
john stultza2752542006-06-26 00:25:14 -0700267 * clocksource_read: - Access the clocksource's current cycle value
john stultz734efb42006-06-26 00:25:05 -0700268 * @cs: pointer to clocksource being read
269 *
270 * Uses the clocksource to return the current cycle_t value
271 */
john stultza2752542006-06-26 00:25:14 -0700272static inline cycle_t clocksource_read(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700273{
274 return cs->read();
275}
276
277/**
278 * cyc2ns - converts clocksource cycles to nanoseconds
279 * @cs: Pointer to clocksource
280 * @cycles: Cycles
281 *
282 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
283 *
284 * XXX - This could use some mult_lxl_ll() asm optimization
285 */
286static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
287{
288 u64 ret = (u64)cycles;
289 ret = (ret * cs->mult) >> cs->shift;
290 return ret;
291}
292
293/**
john stultza2752542006-06-26 00:25:14 -0700294 * clocksource_calculate_interval - Calculates a clocksource interval struct
john stultz734efb42006-06-26 00:25:05 -0700295 *
296 * @c: Pointer to clocksource.
297 * @length_nsec: Desired interval length in nanoseconds.
298 *
299 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
300 * pair and interval request.
301 *
302 * Unless you're the timekeeping code, you should not be using this!
303 */
john stultza2752542006-06-26 00:25:14 -0700304static inline void clocksource_calculate_interval(struct clocksource *c,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800305 unsigned long length_nsec)
john stultz734efb42006-06-26 00:25:05 -0700306{
307 u64 tmp;
308
John Stultz1aa5dfb2008-08-20 16:37:28 -0700309 /* Do the ns -> cycle conversion first, using original mult */
john stultz734efb42006-06-26 00:25:05 -0700310 tmp = length_nsec;
311 tmp <<= c->shift;
John Stultz1aa5dfb2008-08-20 16:37:28 -0700312 tmp += c->mult_orig/2;
313 do_div(tmp, c->mult_orig);
john stultz734efb42006-06-26 00:25:05 -0700314
Roman Zippel19923c12006-06-26 00:25:18 -0700315 c->cycle_interval = (cycle_t)tmp;
316 if (c->cycle_interval == 0)
317 c->cycle_interval = 1;
john stultz734efb42006-06-26 00:25:05 -0700318
John Stultz1aa5dfb2008-08-20 16:37:28 -0700319 /* Go back from cycles -> shifted ns, this time use ntp adjused mult */
Roman Zippel19923c12006-06-26 00:25:18 -0700320 c->xtime_interval = (u64)c->cycle_interval * c->mult;
John Stultz2d422442008-08-20 16:37:30 -0700321 c->raw_interval = ((u64)c->cycle_interval * c->mult_orig) >> c->shift;
john stultz5eb6d202006-06-26 00:25:07 -0700322}
323
324
john stultz734efb42006-06-26 00:25:05 -0700325/* used to install a new clocksource */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800326extern int clocksource_register(struct clocksource*);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100327extern void clocksource_unregister(struct clocksource*);
Jason Wessel7c3078b2008-02-15 14:55:54 -0600328extern void clocksource_touch_watchdog(void);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800329extern struct clocksource* clocksource_get_next(void);
330extern void clocksource_change_rating(struct clocksource *cs, int rating);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700331extern void clocksource_resume(void);
john stultz734efb42006-06-26 00:25:05 -0700332
john stultzacc9a9d2007-02-16 01:28:17 -0800333#ifdef CONFIG_GENERIC_TIME_VSYSCALL
334extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
Tony Breeds2c622142007-10-18 03:04:57 -0700335extern void update_vsyscall_tz(void);
john stultzacc9a9d2007-02-16 01:28:17 -0800336#else
337static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
338{
339}
Tony Breeds2c622142007-10-18 03:04:57 -0700340
341static inline void update_vsyscall_tz(void)
342{
343}
john stultzacc9a9d2007-02-16 01:28:17 -0800344#endif
345
john stultz734efb42006-06-26 00:25:05 -0700346#endif /* _LINUX_CLOCKSOURCE_H */