diff options
Diffstat (limited to 'include/linux/clocksource.h')
-rw-r--r-- | include/linux/clocksource.h | 146 |
1 files changed, 143 insertions, 3 deletions
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h index f88d32f8ff7c..c56457c8334e 100644 --- a/include/linux/clocksource.h +++ b/include/linux/clocksource.h | |||
@@ -22,8 +22,109 @@ typedef u64 cycle_t; | |||
22 | struct clocksource; | 22 | struct clocksource; |
23 | 23 | ||
24 | /** | 24 | /** |
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. | ||
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 | */ | ||
38 | struct 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 | */ | ||
61 | struct 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 | */ | ||
75 | static 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 | */ | ||
93 | extern 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 | */ | ||
105 | extern 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 | */ | ||
121 | extern u64 timecounter_cyc2time(struct timecounter *tc, | ||
122 | cycle_t cycle_tstamp); | ||
123 | |||
124 | /** | ||
25 | * struct clocksource - hardware abstraction for a free running counter | 125 | * struct clocksource - hardware abstraction for a free running counter |
26 | * Provides mostly state-free accessors to the underlying hardware. | 126 | * Provides mostly state-free accessors to the underlying hardware. |
127 | * This is the structure used for system time. | ||
27 | * | 128 | * |
28 | * @name: ptr to clocksource name | 129 | * @name: ptr to clocksource name |
29 | * @list: list head for registration | 130 | * @list: list head for registration |
@@ -42,7 +143,9 @@ struct clocksource; | |||
42 | * 400-499: Perfect | 143 | * 400-499: Perfect |
43 | * The ideal clocksource. A must-use where | 144 | * The ideal clocksource. A must-use where |
44 | * available. | 145 | * available. |
45 | * @read: returns a cycle value | 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 | ||
46 | * @mask: bitmask for two's complement | 149 | * @mask: bitmask for two's complement |
47 | * subtraction of non 64 bit counters | 150 | * subtraction of non 64 bit counters |
48 | * @mult: cycle to nanosecond multiplier (adjusted by NTP) | 151 | * @mult: cycle to nanosecond multiplier (adjusted by NTP) |
@@ -61,7 +164,9 @@ struct clocksource { | |||
61 | char *name; | 164 | char *name; |
62 | struct list_head list; | 165 | struct list_head list; |
63 | int rating; | 166 | int rating; |
64 | cycle_t (*read)(void); | 167 | cycle_t (*read)(struct clocksource *cs); |
168 | int (*enable)(struct clocksource *cs); | ||
169 | void (*disable)(struct clocksource *cs); | ||
65 | cycle_t mask; | 170 | cycle_t mask; |
66 | u32 mult; | 171 | u32 mult; |
67 | u32 mult_orig; | 172 | u32 mult_orig; |
@@ -170,7 +275,42 @@ static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant) | |||
170 | */ | 275 | */ |
171 | static inline cycle_t clocksource_read(struct clocksource *cs) | 276 | static inline cycle_t clocksource_read(struct clocksource *cs) |
172 | { | 277 | { |
173 | return cs->read(); | 278 | return cs->read(cs); |
279 | } | ||
280 | |||
281 | /** | ||
282 | * clocksource_enable: - enable clocksource | ||
283 | * @cs: pointer to clocksource | ||
284 | * | ||
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. | ||
288 | */ | ||
289 | static inline int clocksource_enable(struct clocksource *cs) | ||
290 | { | ||
291 | int ret = 0; | ||
292 | |||
293 | if (cs->enable) | ||
294 | ret = cs->enable(cs); | ||
295 | |||
296 | /* save mult_orig on enable */ | ||
297 | cs->mult_orig = cs->mult; | ||
298 | |||
299 | return ret; | ||
300 | } | ||
301 | |||
302 | /** | ||
303 | * clocksource_disable: - disable clocksource | ||
304 | * @cs: pointer to clocksource | ||
305 | * | ||
306 | * Disables the specified clocksource. The clocksource callback | ||
307 | * function should power down the now unused hardware block to | ||
308 | * save power. | ||
309 | */ | ||
310 | static inline void clocksource_disable(struct clocksource *cs) | ||
311 | { | ||
312 | if (cs->disable) | ||
313 | cs->disable(cs); | ||
174 | } | 314 | } |
175 | 315 | ||
176 | /** | 316 | /** |