]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/linux/clocksource.h
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[mirror_ubuntu-artful-kernel.git] / 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
49 * @shift: cycle to nanosecond divisor (power of two)
50 * @flags: flags describing special properties
51 * @vread: vsyscall based read
52 * @resume: resume function for the clocksource, if necessary
53 * @cycle_interval: Used internally by timekeeping core, please ignore.
54 * @xtime_interval: Used internally by timekeeping core, please ignore.
55 */
56 struct clocksource {
57 /*
58 * First part of structure is read mostly
59 */
60 char *name;
61 struct list_head list;
62 int rating;
63 cycle_t (*read)(void);
64 cycle_t mask;
65 u32 mult;
66 u32 shift;
67 unsigned long flags;
68 cycle_t (*vread)(void);
69 void (*resume)(void);
70
71 /* timekeeping specific data, ignore */
72 cycle_t cycle_interval;
73 u64 xtime_interval;
74 /*
75 * Second part is written at each timer interrupt
76 * Keep it in a different cache line to dirty no
77 * more than one cache line.
78 */
79 cycle_t cycle_last ____cacheline_aligned_in_smp;
80 u64 xtime_nsec;
81 s64 error;
82
83 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
84 /* Watchdog related data, used by the framework */
85 struct list_head wd_list;
86 cycle_t wd_last;
87 #endif
88 };
89
90 /*
91 * Clock source flags bits::
92 */
93 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
94 #define CLOCK_SOURCE_MUST_VERIFY 0x02
95
96 #define CLOCK_SOURCE_WATCHDOG 0x10
97 #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
98
99 /* simplify initialization of mask field */
100 #define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
101
102 /**
103 * clocksource_khz2mult - calculates mult from khz and shift
104 * @khz: Clocksource frequency in KHz
105 * @shift_constant: Clocksource shift factor
106 *
107 * Helper functions that converts a khz counter frequency to a timsource
108 * multiplier, given the clocksource shift value
109 */
110 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
111 {
112 /* khz = cyc/(Million ns)
113 * mult/2^shift = ns/cyc
114 * mult = ns/cyc * 2^shift
115 * mult = 1Million/khz * 2^shift
116 * mult = 1000000 * 2^shift / khz
117 * mult = (1000000<<shift) / khz
118 */
119 u64 tmp = ((u64)1000000) << shift_constant;
120
121 tmp += khz/2; /* round for do_div */
122 do_div(tmp, khz);
123
124 return (u32)tmp;
125 }
126
127 /**
128 * clocksource_hz2mult - calculates mult from hz and shift
129 * @hz: Clocksource frequency in Hz
130 * @shift_constant: Clocksource shift factor
131 *
132 * Helper functions that converts a hz counter
133 * frequency to a timsource multiplier, given the
134 * clocksource shift value
135 */
136 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
137 {
138 /* hz = cyc/(Billion ns)
139 * mult/2^shift = ns/cyc
140 * mult = ns/cyc * 2^shift
141 * mult = 1Billion/hz * 2^shift
142 * mult = 1000000000 * 2^shift / hz
143 * mult = (1000000000<<shift) / hz
144 */
145 u64 tmp = ((u64)1000000000) << shift_constant;
146
147 tmp += hz/2; /* round for do_div */
148 do_div(tmp, hz);
149
150 return (u32)tmp;
151 }
152
153 /**
154 * clocksource_read: - Access the clocksource's current cycle value
155 * @cs: pointer to clocksource being read
156 *
157 * Uses the clocksource to return the current cycle_t value
158 */
159 static inline cycle_t clocksource_read(struct clocksource *cs)
160 {
161 return cs->read();
162 }
163
164 /**
165 * cyc2ns - converts clocksource cycles to nanoseconds
166 * @cs: Pointer to clocksource
167 * @cycles: Cycles
168 *
169 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
170 *
171 * XXX - This could use some mult_lxl_ll() asm optimization
172 */
173 static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
174 {
175 u64 ret = (u64)cycles;
176 ret = (ret * cs->mult) >> cs->shift;
177 return ret;
178 }
179
180 /**
181 * clocksource_calculate_interval - Calculates a clocksource interval struct
182 *
183 * @c: Pointer to clocksource.
184 * @length_nsec: Desired interval length in nanoseconds.
185 *
186 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
187 * pair and interval request.
188 *
189 * Unless you're the timekeeping code, you should not be using this!
190 */
191 static inline void clocksource_calculate_interval(struct clocksource *c,
192 unsigned long length_nsec)
193 {
194 u64 tmp;
195
196 /* XXX - All of this could use a whole lot of optimization */
197 tmp = length_nsec;
198 tmp <<= c->shift;
199 tmp += c->mult/2;
200 do_div(tmp, c->mult);
201
202 c->cycle_interval = (cycle_t)tmp;
203 if (c->cycle_interval == 0)
204 c->cycle_interval = 1;
205
206 c->xtime_interval = (u64)c->cycle_interval * c->mult;
207 }
208
209
210 /* used to install a new clocksource */
211 extern int clocksource_register(struct clocksource*);
212 extern struct clocksource* clocksource_get_next(void);
213 extern void clocksource_change_rating(struct clocksource *cs, int rating);
214 extern void clocksource_resume(void);
215
216 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
217 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
218 #else
219 static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
220 {
221 }
222 #endif
223
224 #endif /* _LINUX_CLOCKSOURCE_H */