[PATCH] clocksource: fix resume logic
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / linux / clocksource.h
blobbf92c263156265d879c1a71e5d3fa35a57abee7e
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!
7 */
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/timer.h>
16 #include <asm/div64.h>
17 #include <asm/io.h>
19 /* clocksource cycle base type */
20 typedef u64 cycle_t;
21 struct clocksource;
23 /**
24 * struct clocksource - hardware abstraction for a free running counter
25 * Provides mostly state-free accessors to the underlying hardware.
27 * @name: ptr to clocksource name
28 * @list: list head for registration
29 * @rating: rating value for selection (higher is better)
30 * To avoid rating inflation the following
31 * list should give you a guide as to how
32 * to assign your clocksource a rating
33 * 1-99: Unfit for real use
34 * Only available for bootup and testing purposes.
35 * 100-199: Base level usability.
36 * Functional for real use, but not desired.
37 * 200-299: Good.
38 * A correct and usable clocksource.
39 * 300-399: Desired.
40 * A reasonably fast and accurate clocksource.
41 * 400-499: Perfect
42 * The ideal clocksource. A must-use where
43 * available.
44 * @read: returns a cycle value
45 * @mask: bitmask for two's complement
46 * subtraction of non 64 bit counters
47 * @mult: cycle to nanosecond multiplier
48 * @shift: cycle to nanosecond divisor (power of two)
49 * @flags: flags describing special properties
50 * @vread: vsyscall based read
51 * @resume: resume function for the clocksource, if necessary
52 * @cycle_interval: Used internally by timekeeping core, please ignore.
53 * @xtime_interval: Used internally by timekeeping core, please ignore.
55 struct clocksource {
56 char *name;
57 struct list_head list;
58 int rating;
59 cycle_t (*read)(void);
60 cycle_t mask;
61 u32 mult;
62 u32 shift;
63 unsigned long flags;
64 cycle_t (*vread)(void);
65 void (*resume)(void);
67 /* timekeeping specific data, ignore */
68 cycle_t cycle_last, cycle_interval;
69 u64 xtime_nsec, xtime_interval;
70 s64 error;
72 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
73 /* Watchdog related data, used by the framework */
74 struct list_head wd_list;
75 cycle_t wd_last;
76 #endif
80 * Clock source flags bits::
82 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
83 #define CLOCK_SOURCE_MUST_VERIFY 0x02
85 #define CLOCK_SOURCE_WATCHDOG 0x10
86 #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
88 /* simplify initialization of mask field */
89 #define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
91 /**
92 * clocksource_khz2mult - calculates mult from khz and shift
93 * @khz: Clocksource frequency in KHz
94 * @shift_constant: Clocksource shift factor
96 * Helper functions that converts a khz counter frequency to a timsource
97 * multiplier, given the clocksource shift value
99 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
101 /* khz = cyc/(Million ns)
102 * mult/2^shift = ns/cyc
103 * mult = ns/cyc * 2^shift
104 * mult = 1Million/khz * 2^shift
105 * mult = 1000000 * 2^shift / khz
106 * mult = (1000000<<shift) / khz
108 u64 tmp = ((u64)1000000) << shift_constant;
110 tmp += khz/2; /* round for do_div */
111 do_div(tmp, khz);
113 return (u32)tmp;
117 * clocksource_hz2mult - calculates mult from hz and shift
118 * @hz: Clocksource frequency in Hz
119 * @shift_constant: Clocksource shift factor
121 * Helper functions that converts a hz counter
122 * frequency to a timsource multiplier, given the
123 * clocksource shift value
125 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
127 /* hz = cyc/(Billion ns)
128 * mult/2^shift = ns/cyc
129 * mult = ns/cyc * 2^shift
130 * mult = 1Billion/hz * 2^shift
131 * mult = 1000000000 * 2^shift / hz
132 * mult = (1000000000<<shift) / hz
134 u64 tmp = ((u64)1000000000) << shift_constant;
136 tmp += hz/2; /* round for do_div */
137 do_div(tmp, hz);
139 return (u32)tmp;
143 * clocksource_read: - Access the clocksource's current cycle value
144 * @cs: pointer to clocksource being read
146 * Uses the clocksource to return the current cycle_t value
148 static inline cycle_t clocksource_read(struct clocksource *cs)
150 return cs->read();
154 * cyc2ns - converts clocksource cycles to nanoseconds
155 * @cs: Pointer to clocksource
156 * @cycles: Cycles
158 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
160 * XXX - This could use some mult_lxl_ll() asm optimization
162 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
164 u64 ret = (u64)cycles;
165 ret = (ret * cs->mult) >> cs->shift;
166 return ret;
170 * clocksource_calculate_interval - Calculates a clocksource interval struct
172 * @c: Pointer to clocksource.
173 * @length_nsec: Desired interval length in nanoseconds.
175 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
176 * pair and interval request.
178 * Unless you're the timekeeping code, you should not be using this!
180 static inline void clocksource_calculate_interval(struct clocksource *c,
181 unsigned long length_nsec)
183 u64 tmp;
185 /* XXX - All of this could use a whole lot of optimization */
186 tmp = length_nsec;
187 tmp <<= c->shift;
188 tmp += c->mult/2;
189 do_div(tmp, c->mult);
191 c->cycle_interval = (cycle_t)tmp;
192 if (c->cycle_interval == 0)
193 c->cycle_interval = 1;
195 c->xtime_interval = (u64)c->cycle_interval * c->mult;
199 /* used to install a new clocksource */
200 extern int clocksource_register(struct clocksource*);
201 extern struct clocksource* clocksource_get_next(void);
202 extern void clocksource_change_rating(struct clocksource *cs, int rating);
203 extern void clocksource_resume(void);
205 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
206 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
207 #else
208 static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
211 #endif
213 #endif /* _LINUX_CLOCKSOURCE_H */