blob: 830a250ecf947688e5fb3b5589e20f908088f828 [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>
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080015#include <linux/timer.h>
john stultz734efb42006-06-26 00:25:05 -070016#include <asm/div64.h>
17#include <asm/io.h>
18
19/* clocksource cycle base type */
20typedef u64 cycle_t;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080021struct clocksource;
john stultz734efb42006-06-26 00:25:05 -070022
23/**
24 * struct clocksource - hardware abstraction for a free running counter
25 * Provides mostly state-free accessors to the underlying hardware.
26 *
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)
Thomas Gleixner73b08d22007-02-16 01:27:36 -080049 * @flags: flags describing special properties
Roman Zippel19923c12006-06-26 00:25:18 -070050 * @cycle_interval: Used internally by timekeeping core, please ignore.
51 * @xtime_interval: Used internally by timekeeping core, please ignore.
john stultz734efb42006-06-26 00:25:05 -070052 */
53struct clocksource {
54 char *name;
55 struct list_head list;
56 int rating;
57 cycle_t (*read)(void);
58 cycle_t mask;
59 u32 mult;
60 u32 shift;
Thomas Gleixner73b08d22007-02-16 01:27:36 -080061 unsigned long flags;
john stultz734efb42006-06-26 00:25:05 -070062
63 /* timekeeping specific data, ignore */
Roman Zippel19923c12006-06-26 00:25:18 -070064 cycle_t cycle_last, cycle_interval;
65 u64 xtime_nsec, xtime_interval;
66 s64 error;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080067
68#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
69 /* Watchdog related data, used by the framework */
70 struct list_head wd_list;
71 cycle_t wd_last;
72#endif
john stultz734efb42006-06-26 00:25:05 -070073};
74
Thomas Gleixner73b08d22007-02-16 01:27:36 -080075/*
76 * Clock source flags bits::
77 */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080078#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
79#define CLOCK_SOURCE_MUST_VERIFY 0x02
80
81#define CLOCK_SOURCE_WATCHDOG 0x10
82#define CLOCK_SOURCE_VALID_FOR_HRES 0x20
Thomas Gleixner73b08d22007-02-16 01:27:36 -080083
Jim Cromie7f9f3032006-06-26 00:25:15 -070084/* simplify initialization of mask field */
85#define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
john stultz734efb42006-06-26 00:25:05 -070086
87/**
88 * clocksource_khz2mult - calculates mult from khz and shift
89 * @khz: Clocksource frequency in KHz
90 * @shift_constant: Clocksource shift factor
91 *
92 * Helper functions that converts a khz counter frequency to a timsource
93 * multiplier, given the clocksource shift value
94 */
95static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
96{
97 /* khz = cyc/(Million ns)
98 * mult/2^shift = ns/cyc
99 * mult = ns/cyc * 2^shift
100 * mult = 1Million/khz * 2^shift
101 * mult = 1000000 * 2^shift / khz
102 * mult = (1000000<<shift) / khz
103 */
104 u64 tmp = ((u64)1000000) << shift_constant;
105
106 tmp += khz/2; /* round for do_div */
107 do_div(tmp, khz);
108
109 return (u32)tmp;
110}
111
112/**
113 * clocksource_hz2mult - calculates mult from hz and shift
114 * @hz: Clocksource frequency in Hz
115 * @shift_constant: Clocksource shift factor
116 *
117 * Helper functions that converts a hz counter
118 * frequency to a timsource multiplier, given the
119 * clocksource shift value
120 */
121static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
122{
123 /* hz = cyc/(Billion ns)
124 * mult/2^shift = ns/cyc
125 * mult = ns/cyc * 2^shift
126 * mult = 1Billion/hz * 2^shift
127 * mult = 1000000000 * 2^shift / hz
128 * mult = (1000000000<<shift) / hz
129 */
130 u64 tmp = ((u64)1000000000) << shift_constant;
131
132 tmp += hz/2; /* round for do_div */
133 do_div(tmp, hz);
134
135 return (u32)tmp;
136}
137
138/**
john stultza2752542006-06-26 00:25:14 -0700139 * clocksource_read: - Access the clocksource's current cycle value
john stultz734efb42006-06-26 00:25:05 -0700140 * @cs: pointer to clocksource being read
141 *
142 * Uses the clocksource to return the current cycle_t value
143 */
john stultza2752542006-06-26 00:25:14 -0700144static inline cycle_t clocksource_read(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700145{
146 return cs->read();
147}
148
149/**
150 * cyc2ns - converts clocksource cycles to nanoseconds
151 * @cs: Pointer to clocksource
152 * @cycles: Cycles
153 *
154 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
155 *
156 * XXX - This could use some mult_lxl_ll() asm optimization
157 */
158static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
159{
160 u64 ret = (u64)cycles;
161 ret = (ret * cs->mult) >> cs->shift;
162 return ret;
163}
164
165/**
john stultza2752542006-06-26 00:25:14 -0700166 * clocksource_calculate_interval - Calculates a clocksource interval struct
john stultz734efb42006-06-26 00:25:05 -0700167 *
168 * @c: Pointer to clocksource.
169 * @length_nsec: Desired interval length in nanoseconds.
170 *
171 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
172 * pair and interval request.
173 *
174 * Unless you're the timekeeping code, you should not be using this!
175 */
john stultza2752542006-06-26 00:25:14 -0700176static inline void clocksource_calculate_interval(struct clocksource *c,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800177 unsigned long length_nsec)
john stultz734efb42006-06-26 00:25:05 -0700178{
179 u64 tmp;
180
181 /* XXX - All of this could use a whole lot of optimization */
182 tmp = length_nsec;
183 tmp <<= c->shift;
184 tmp += c->mult/2;
185 do_div(tmp, c->mult);
186
Roman Zippel19923c12006-06-26 00:25:18 -0700187 c->cycle_interval = (cycle_t)tmp;
188 if (c->cycle_interval == 0)
189 c->cycle_interval = 1;
john stultz734efb42006-06-26 00:25:05 -0700190
Roman Zippel19923c12006-06-26 00:25:18 -0700191 c->xtime_interval = (u64)c->cycle_interval * c->mult;
john stultz5eb6d202006-06-26 00:25:07 -0700192}
193
194
john stultz734efb42006-06-26 00:25:05 -0700195/* used to install a new clocksource */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800196extern int clocksource_register(struct clocksource*);
197extern struct clocksource* clocksource_get_next(void);
198extern void clocksource_change_rating(struct clocksource *cs, int rating);
john stultz734efb42006-06-26 00:25:05 -0700199
200#endif /* _LINUX_CLOCKSOURCE_H */