blob: 2665ca04cf8f5e919d2f72bfdde6d4390986f062 [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
48 * @mult: cycle to nanosecond multiplier
49 * @shift: cycle to nanosecond divisor (power of two)
Thomas Gleixner73b08d22007-02-16 01:27:36 -080050 * @flags: flags describing special properties
john stultzacc9a9d2007-02-16 01:28:17 -080051 * @vread: vsyscall based read
Roman Zippel19923c12006-06-26 00:25:18 -070052 * @cycle_interval: Used internally by timekeeping core, please ignore.
53 * @xtime_interval: Used internally by timekeeping core, please ignore.
john stultz734efb42006-06-26 00:25:05 -070054 */
55struct clocksource {
Eric Dumazet329c8d82007-05-08 00:27:57 -070056 /*
57 * First part of structure is read mostly
58 */
john stultz734efb42006-06-26 00:25:05 -070059 char *name;
60 struct list_head list;
61 int rating;
62 cycle_t (*read)(void);
63 cycle_t mask;
64 u32 mult;
65 u32 shift;
Thomas Gleixner73b08d22007-02-16 01:27:36 -080066 unsigned long flags;
john stultzacc9a9d2007-02-16 01:28:17 -080067 cycle_t (*vread)(void);
john stultz734efb42006-06-26 00:25:05 -070068
69 /* timekeeping specific data, ignore */
Eric Dumazet329c8d82007-05-08 00:27:57 -070070 cycle_t cycle_interval;
71 u64 xtime_interval;
72 /*
73 * Second part is written at each timer interrupt
74 * Keep it in a different cache line to dirty no
75 * more than one cache line.
76 */
77 cycle_t cycle_last ____cacheline_aligned_in_smp;
78 u64 xtime_nsec;
Roman Zippel19923c12006-06-26 00:25:18 -070079 s64 error;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080080
81#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
82 /* Watchdog related data, used by the framework */
83 struct list_head wd_list;
84 cycle_t wd_last;
85#endif
john stultz734efb42006-06-26 00:25:05 -070086};
87
Thomas Gleixner73b08d22007-02-16 01:27:36 -080088/*
89 * Clock source flags bits::
90 */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080091#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
92#define CLOCK_SOURCE_MUST_VERIFY 0x02
93
94#define CLOCK_SOURCE_WATCHDOG 0x10
95#define CLOCK_SOURCE_VALID_FOR_HRES 0x20
Thomas Gleixner73b08d22007-02-16 01:27:36 -080096
Jim Cromie7f9f3032006-06-26 00:25:15 -070097/* simplify initialization of mask field */
98#define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
john stultz734efb42006-06-26 00:25:05 -070099
100/**
101 * clocksource_khz2mult - calculates mult from khz and shift
102 * @khz: Clocksource frequency in KHz
103 * @shift_constant: Clocksource shift factor
104 *
105 * Helper functions that converts a khz counter frequency to a timsource
106 * multiplier, given the clocksource shift value
107 */
108static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
109{
110 /* khz = cyc/(Million ns)
111 * mult/2^shift = ns/cyc
112 * mult = ns/cyc * 2^shift
113 * mult = 1Million/khz * 2^shift
114 * mult = 1000000 * 2^shift / khz
115 * mult = (1000000<<shift) / khz
116 */
117 u64 tmp = ((u64)1000000) << shift_constant;
118
119 tmp += khz/2; /* round for do_div */
120 do_div(tmp, khz);
121
122 return (u32)tmp;
123}
124
125/**
126 * clocksource_hz2mult - calculates mult from hz and shift
127 * @hz: Clocksource frequency in Hz
128 * @shift_constant: Clocksource shift factor
129 *
130 * Helper functions that converts a hz counter
131 * frequency to a timsource multiplier, given the
132 * clocksource shift value
133 */
134static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
135{
136 /* hz = cyc/(Billion ns)
137 * mult/2^shift = ns/cyc
138 * mult = ns/cyc * 2^shift
139 * mult = 1Billion/hz * 2^shift
140 * mult = 1000000000 * 2^shift / hz
141 * mult = (1000000000<<shift) / hz
142 */
143 u64 tmp = ((u64)1000000000) << shift_constant;
144
145 tmp += hz/2; /* round for do_div */
146 do_div(tmp, hz);
147
148 return (u32)tmp;
149}
150
151/**
john stultza2752542006-06-26 00:25:14 -0700152 * clocksource_read: - Access the clocksource's current cycle value
john stultz734efb42006-06-26 00:25:05 -0700153 * @cs: pointer to clocksource being read
154 *
155 * Uses the clocksource to return the current cycle_t value
156 */
john stultza2752542006-06-26 00:25:14 -0700157static inline cycle_t clocksource_read(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700158{
159 return cs->read();
160}
161
162/**
163 * cyc2ns - converts clocksource cycles to nanoseconds
164 * @cs: Pointer to clocksource
165 * @cycles: Cycles
166 *
167 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
168 *
169 * XXX - This could use some mult_lxl_ll() asm optimization
170 */
171static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
172{
173 u64 ret = (u64)cycles;
174 ret = (ret * cs->mult) >> cs->shift;
175 return ret;
176}
177
178/**
john stultza2752542006-06-26 00:25:14 -0700179 * clocksource_calculate_interval - Calculates a clocksource interval struct
john stultz734efb42006-06-26 00:25:05 -0700180 *
181 * @c: Pointer to clocksource.
182 * @length_nsec: Desired interval length in nanoseconds.
183 *
184 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
185 * pair and interval request.
186 *
187 * Unless you're the timekeeping code, you should not be using this!
188 */
john stultza2752542006-06-26 00:25:14 -0700189static inline void clocksource_calculate_interval(struct clocksource *c,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800190 unsigned long length_nsec)
john stultz734efb42006-06-26 00:25:05 -0700191{
192 u64 tmp;
193
194 /* XXX - All of this could use a whole lot of optimization */
195 tmp = length_nsec;
196 tmp <<= c->shift;
197 tmp += c->mult/2;
198 do_div(tmp, c->mult);
199
Roman Zippel19923c12006-06-26 00:25:18 -0700200 c->cycle_interval = (cycle_t)tmp;
201 if (c->cycle_interval == 0)
202 c->cycle_interval = 1;
john stultz734efb42006-06-26 00:25:05 -0700203
Roman Zippel19923c12006-06-26 00:25:18 -0700204 c->xtime_interval = (u64)c->cycle_interval * c->mult;
john stultz5eb6d202006-06-26 00:25:07 -0700205}
206
207
john stultz734efb42006-06-26 00:25:05 -0700208/* used to install a new clocksource */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800209extern int clocksource_register(struct clocksource*);
210extern struct clocksource* clocksource_get_next(void);
211extern void clocksource_change_rating(struct clocksource *cs, int rating);
john stultz734efb42006-06-26 00:25:05 -0700212
john stultzacc9a9d2007-02-16 01:28:17 -0800213#ifdef CONFIG_GENERIC_TIME_VSYSCALL
214extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
215#else
216static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
217{
218}
219#endif
220
john stultz734efb42006-06-26 00:25:05 -0700221#endif /* _LINUX_CLOCKSOURCE_H */