blob: f88d32f8ff7c8a0d242763328c8e14f39445006d [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/**
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
John Stultz1aa5dfb2008-08-20 16:37:28 -070048 * @mult: cycle to nanosecond multiplier (adjusted by NTP)
49 * @mult_orig: cycle to nanosecond multiplier (unadjusted by NTP)
john stultz734efb42006-06-26 00:25:05 -070050 * @shift: cycle to nanosecond divisor (power of two)
Thomas Gleixner73b08d22007-02-16 01:27:36 -080051 * @flags: flags describing special properties
john stultzacc9a9d2007-02-16 01:28:17 -080052 * @vread: vsyscall based read
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -070053 * @resume: resume function for the clocksource, if necessary
Roman Zippel19923c12006-06-26 00:25:18 -070054 * @cycle_interval: Used internally by timekeeping core, please ignore.
55 * @xtime_interval: Used internally by timekeeping core, please ignore.
john stultz734efb42006-06-26 00:25:05 -070056 */
57struct clocksource {
Eric Dumazet329c8d82007-05-08 00:27:57 -070058 /*
59 * First part of structure is read mostly
60 */
john stultz734efb42006-06-26 00:25:05 -070061 char *name;
62 struct list_head list;
63 int rating;
64 cycle_t (*read)(void);
65 cycle_t mask;
66 u32 mult;
John Stultz1aa5dfb2008-08-20 16:37:28 -070067 u32 mult_orig;
john stultz734efb42006-06-26 00:25:05 -070068 u32 shift;
Thomas Gleixner73b08d22007-02-16 01:27:36 -080069 unsigned long flags;
john stultzacc9a9d2007-02-16 01:28:17 -080070 cycle_t (*vread)(void);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -070071 void (*resume)(void);
Tony Luck0aa366f2007-07-20 11:22:30 -070072#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
john stultz734efb42006-06-26 00:25:05 -070078
79 /* timekeeping specific data, ignore */
Eric Dumazet329c8d82007-05-08 00:27:57 -070080 cycle_t cycle_interval;
81 u64 xtime_interval;
John Stultz2d422442008-08-20 16:37:30 -070082 u32 raw_interval;
Eric Dumazet329c8d82007-05-08 00:27:57 -070083 /*
84 * Second part is written at each timer interrupt
85 * Keep it in a different cache line to dirty no
86 * more than one cache line.
87 */
88 cycle_t cycle_last ____cacheline_aligned_in_smp;
89 u64 xtime_nsec;
Roman Zippel19923c12006-06-26 00:25:18 -070090 s64 error;
John Stultz2d422442008-08-20 16:37:30 -070091 struct timespec raw_time;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080092
93#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
94 /* Watchdog related data, used by the framework */
95 struct list_head wd_list;
96 cycle_t wd_last;
97#endif
john stultz734efb42006-06-26 00:25:05 -070098};
99
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700100extern struct clocksource *clock; /* current clocksource */
101
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800102/*
103 * Clock source flags bits::
104 */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800105#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
106#define CLOCK_SOURCE_MUST_VERIFY 0x02
107
108#define CLOCK_SOURCE_WATCHDOG 0x10
109#define CLOCK_SOURCE_VALID_FOR_HRES 0x20
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800110
Jim Cromie7f9f3032006-06-26 00:25:15 -0700111/* simplify initialization of mask field */
Atsushi Nemoto1d76c262008-01-30 13:30:01 +0100112#define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
john stultz734efb42006-06-26 00:25:05 -0700113
114/**
115 * clocksource_khz2mult - calculates mult from khz and shift
116 * @khz: Clocksource frequency in KHz
117 * @shift_constant: Clocksource shift factor
118 *
119 * Helper functions that converts a khz counter frequency to a timsource
120 * multiplier, given the clocksource shift value
121 */
122static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
123{
124 /* khz = cyc/(Million ns)
125 * mult/2^shift = ns/cyc
126 * mult = ns/cyc * 2^shift
127 * mult = 1Million/khz * 2^shift
128 * mult = 1000000 * 2^shift / khz
129 * mult = (1000000<<shift) / khz
130 */
131 u64 tmp = ((u64)1000000) << shift_constant;
132
133 tmp += khz/2; /* round for do_div */
134 do_div(tmp, khz);
135
136 return (u32)tmp;
137}
138
139/**
140 * clocksource_hz2mult - calculates mult from hz and shift
141 * @hz: Clocksource frequency in Hz
142 * @shift_constant: Clocksource shift factor
143 *
144 * Helper functions that converts a hz counter
145 * frequency to a timsource multiplier, given the
146 * clocksource shift value
147 */
148static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
149{
150 /* hz = cyc/(Billion ns)
151 * mult/2^shift = ns/cyc
152 * mult = ns/cyc * 2^shift
153 * mult = 1Billion/hz * 2^shift
154 * mult = 1000000000 * 2^shift / hz
155 * mult = (1000000000<<shift) / hz
156 */
157 u64 tmp = ((u64)1000000000) << shift_constant;
158
159 tmp += hz/2; /* round for do_div */
160 do_div(tmp, hz);
161
162 return (u32)tmp;
163}
164
165/**
john stultza2752542006-06-26 00:25:14 -0700166 * clocksource_read: - Access the clocksource's current cycle value
john stultz734efb42006-06-26 00:25:05 -0700167 * @cs: pointer to clocksource being read
168 *
169 * Uses the clocksource to return the current cycle_t value
170 */
john stultza2752542006-06-26 00:25:14 -0700171static inline cycle_t clocksource_read(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700172{
173 return cs->read();
174}
175
176/**
177 * cyc2ns - converts clocksource cycles to nanoseconds
178 * @cs: Pointer to clocksource
179 * @cycles: Cycles
180 *
181 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
182 *
183 * XXX - This could use some mult_lxl_ll() asm optimization
184 */
185static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
186{
187 u64 ret = (u64)cycles;
188 ret = (ret * cs->mult) >> cs->shift;
189 return ret;
190}
191
192/**
john stultza2752542006-06-26 00:25:14 -0700193 * clocksource_calculate_interval - Calculates a clocksource interval struct
john stultz734efb42006-06-26 00:25:05 -0700194 *
195 * @c: Pointer to clocksource.
196 * @length_nsec: Desired interval length in nanoseconds.
197 *
198 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
199 * pair and interval request.
200 *
201 * Unless you're the timekeeping code, you should not be using this!
202 */
john stultza2752542006-06-26 00:25:14 -0700203static inline void clocksource_calculate_interval(struct clocksource *c,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800204 unsigned long length_nsec)
john stultz734efb42006-06-26 00:25:05 -0700205{
206 u64 tmp;
207
John Stultz1aa5dfb2008-08-20 16:37:28 -0700208 /* Do the ns -> cycle conversion first, using original mult */
john stultz734efb42006-06-26 00:25:05 -0700209 tmp = length_nsec;
210 tmp <<= c->shift;
John Stultz1aa5dfb2008-08-20 16:37:28 -0700211 tmp += c->mult_orig/2;
212 do_div(tmp, c->mult_orig);
john stultz734efb42006-06-26 00:25:05 -0700213
Roman Zippel19923c12006-06-26 00:25:18 -0700214 c->cycle_interval = (cycle_t)tmp;
215 if (c->cycle_interval == 0)
216 c->cycle_interval = 1;
john stultz734efb42006-06-26 00:25:05 -0700217
John Stultz1aa5dfb2008-08-20 16:37:28 -0700218 /* Go back from cycles -> shifted ns, this time use ntp adjused mult */
Roman Zippel19923c12006-06-26 00:25:18 -0700219 c->xtime_interval = (u64)c->cycle_interval * c->mult;
John Stultz2d422442008-08-20 16:37:30 -0700220 c->raw_interval = ((u64)c->cycle_interval * c->mult_orig) >> c->shift;
john stultz5eb6d202006-06-26 00:25:07 -0700221}
222
223
john stultz734efb42006-06-26 00:25:05 -0700224/* used to install a new clocksource */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800225extern int clocksource_register(struct clocksource*);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100226extern void clocksource_unregister(struct clocksource*);
Jason Wessel7c3078b2008-02-15 14:55:54 -0600227extern void clocksource_touch_watchdog(void);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800228extern struct clocksource* clocksource_get_next(void);
229extern void clocksource_change_rating(struct clocksource *cs, int rating);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700230extern void clocksource_resume(void);
john stultz734efb42006-06-26 00:25:05 -0700231
john stultzacc9a9d2007-02-16 01:28:17 -0800232#ifdef CONFIG_GENERIC_TIME_VSYSCALL
233extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
Tony Breeds2c622142007-10-18 03:04:57 -0700234extern void update_vsyscall_tz(void);
john stultzacc9a9d2007-02-16 01:28:17 -0800235#else
236static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
237{
238}
Tony Breeds2c622142007-10-18 03:04:57 -0700239
240static inline void update_vsyscall_tz(void)
241{
242}
john stultzacc9a9d2007-02-16 01:28:17 -0800243#endif
244
john stultz734efb42006-06-26 00:25:05 -0700245#endif /* _LINUX_CLOCKSOURCE_H */