clocksource: keep track of original clocksource frequency
[linux-2.6] / include / linux / clocksource.h
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 <asm/div64.h>
18 #include <asm/io.h>
19
20 /* clocksource cycle base type */
21 typedef u64 cycle_t;
22 struct clocksource;
23
24 /**
25  * struct clocksource - hardware abstraction for a free running counter
26  *      Provides mostly state-free accessors to the underlying hardware.
27  *
28  * @name:               ptr to clocksource name
29  * @list:               list head for registration
30  * @rating:             rating value for selection (higher is better)
31  *                      To avoid rating inflation the following
32  *                      list should give you a guide as to how
33  *                      to assign your clocksource a rating
34  *                      1-99: Unfit for real use
35  *                              Only available for bootup and testing purposes.
36  *                      100-199: Base level usability.
37  *                              Functional for real use, but not desired.
38  *                      200-299: Good.
39  *                              A correct and usable clocksource.
40  *                      300-399: Desired.
41  *                              A reasonably fast and accurate clocksource.
42  *                      400-499: Perfect
43  *                              The ideal clocksource. A must-use where
44  *                              available.
45  * @read:               returns a cycle value
46  * @mask:               bitmask for two's complement
47  *                      subtraction of non 64 bit counters
48  * @mult:               cycle to nanosecond multiplier (adjusted by NTP)
49  * @mult_orig:          cycle to nanosecond multiplier (unadjusted by NTP)
50  * @shift:              cycle to nanosecond divisor (power of two)
51  * @flags:              flags describing special properties
52  * @vread:              vsyscall based read
53  * @resume:             resume function for the clocksource, if necessary
54  * @cycle_interval:     Used internally by timekeeping core, please ignore.
55  * @xtime_interval:     Used internally by timekeeping core, please ignore.
56  */
57 struct clocksource {
58         /*
59          * First part of structure is read mostly
60          */
61         char *name;
62         struct list_head list;
63         int rating;
64         cycle_t (*read)(void);
65         cycle_t mask;
66         u32 mult;
67         u32 mult_orig;
68         u32 shift;
69         unsigned long flags;
70         cycle_t (*vread)(void);
71         void (*resume)(void);
72 #ifdef CONFIG_IA64
73         void *fsys_mmio;        /* used by fsyscall asm code */
74 #define CLKSRC_FSYS_MMIO_SET(mmio, addr)      ((mmio) = (addr))
75 #else
76 #define CLKSRC_FSYS_MMIO_SET(mmio, addr)      do { } while (0)
77 #endif
78
79         /* timekeeping specific data, ignore */
80         cycle_t cycle_interval;
81         u64     xtime_interval;
82         /*
83          * Second part is written at each timer interrupt
84          * Keep it in a different cache line to dirty no
85          * more than one cache line.
86          */
87         cycle_t cycle_last ____cacheline_aligned_in_smp;
88         u64 xtime_nsec;
89         s64 error;
90
91 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
92         /* Watchdog related data, used by the framework */
93         struct list_head wd_list;
94         cycle_t wd_last;
95 #endif
96 };
97
98 extern struct clocksource *clock;       /* current clocksource */
99
100 /*
101  * Clock source flags bits::
102  */
103 #define CLOCK_SOURCE_IS_CONTINUOUS              0x01
104 #define CLOCK_SOURCE_MUST_VERIFY                0x02
105
106 #define CLOCK_SOURCE_WATCHDOG                   0x10
107 #define CLOCK_SOURCE_VALID_FOR_HRES             0x20
108
109 /* simplify initialization of mask field */
110 #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
111
112 /**
113  * clocksource_khz2mult - calculates mult from khz and shift
114  * @khz:                Clocksource frequency in KHz
115  * @shift_constant:     Clocksource shift factor
116  *
117  * Helper functions that converts a khz counter frequency to a timsource
118  * multiplier, given the clocksource shift value
119  */
120 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
121 {
122         /*  khz = cyc/(Million ns)
123          *  mult/2^shift  = ns/cyc
124          *  mult = ns/cyc * 2^shift
125          *  mult = 1Million/khz * 2^shift
126          *  mult = 1000000 * 2^shift / khz
127          *  mult = (1000000<<shift) / khz
128          */
129         u64 tmp = ((u64)1000000) << shift_constant;
130
131         tmp += khz/2; /* round for do_div */
132         do_div(tmp, khz);
133
134         return (u32)tmp;
135 }
136
137 /**
138  * clocksource_hz2mult - calculates mult from hz and shift
139  * @hz:                 Clocksource frequency in Hz
140  * @shift_constant:     Clocksource shift factor
141  *
142  * Helper functions that converts a hz counter
143  * frequency to a timsource multiplier, given the
144  * clocksource shift value
145  */
146 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
147 {
148         /*  hz = cyc/(Billion ns)
149          *  mult/2^shift  = ns/cyc
150          *  mult = ns/cyc * 2^shift
151          *  mult = 1Billion/hz * 2^shift
152          *  mult = 1000000000 * 2^shift / hz
153          *  mult = (1000000000<<shift) / hz
154          */
155         u64 tmp = ((u64)1000000000) << shift_constant;
156
157         tmp += hz/2; /* round for do_div */
158         do_div(tmp, hz);
159
160         return (u32)tmp;
161 }
162
163 /**
164  * clocksource_read: - Access the clocksource's current cycle value
165  * @cs:         pointer to clocksource being read
166  *
167  * Uses the clocksource to return the current cycle_t value
168  */
169 static inline cycle_t clocksource_read(struct clocksource *cs)
170 {
171         return cs->read();
172 }
173
174 /**
175  * cyc2ns - converts clocksource cycles to nanoseconds
176  * @cs:         Pointer to clocksource
177  * @cycles:     Cycles
178  *
179  * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
180  *
181  * XXX - This could use some mult_lxl_ll() asm optimization
182  */
183 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
184 {
185         u64 ret = (u64)cycles;
186         ret = (ret * cs->mult) >> cs->shift;
187         return ret;
188 }
189
190 /**
191  * clocksource_calculate_interval - Calculates a clocksource interval struct
192  *
193  * @c:          Pointer to clocksource.
194  * @length_nsec: Desired interval length in nanoseconds.
195  *
196  * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
197  * pair and interval request.
198  *
199  * Unless you're the timekeeping code, you should not be using this!
200  */
201 static inline void clocksource_calculate_interval(struct clocksource *c,
202                                                   unsigned long length_nsec)
203 {
204         u64 tmp;
205
206         /* Do the ns -> cycle conversion first, using original mult */
207         tmp = length_nsec;
208         tmp <<= c->shift;
209         tmp += c->mult_orig/2;
210         do_div(tmp, c->mult_orig);
211
212         c->cycle_interval = (cycle_t)tmp;
213         if (c->cycle_interval == 0)
214                 c->cycle_interval = 1;
215
216         /* Go back from cycles -> shifted ns, this time use ntp adjused mult */
217         c->xtime_interval = (u64)c->cycle_interval * c->mult;
218 }
219
220
221 /* used to install a new clocksource */
222 extern int clocksource_register(struct clocksource*);
223 extern void clocksource_unregister(struct clocksource*);
224 extern void clocksource_touch_watchdog(void);
225 extern struct clocksource* clocksource_get_next(void);
226 extern void clocksource_change_rating(struct clocksource *cs, int rating);
227 extern void clocksource_resume(void);
228
229 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
230 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
231 extern void update_vsyscall_tz(void);
232 #else
233 static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
234 {
235 }
236
237 static inline void update_vsyscall_tz(void)
238 {
239 }
240 #endif
241
242 #endif /* _LINUX_CLOCKSOURCE_H */