blob: 45b0c310ae82ab8e8275509daae1e06ce4391b66 [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>
15#include <asm/div64.h>
16#include <asm/io.h>
17
18/* clocksource cycle base type */
19typedef u64 cycle_t;
20
21/**
22 * struct clocksource - hardware abstraction for a free running counter
23 * Provides mostly state-free accessors to the underlying hardware.
24 *
25 * @name: ptr to clocksource name
26 * @list: list head for registration
27 * @rating: rating value for selection (higher is better)
28 * To avoid rating inflation the following
29 * list should give you a guide as to how
30 * to assign your clocksource a rating
31 * 1-99: Unfit for real use
32 * Only available for bootup and testing purposes.
33 * 100-199: Base level usability.
34 * Functional for real use, but not desired.
35 * 200-299: Good.
36 * A correct and usable clocksource.
37 * 300-399: Desired.
38 * A reasonably fast and accurate clocksource.
39 * 400-499: Perfect
40 * The ideal clocksource. A must-use where
41 * available.
42 * @read: returns a cycle value
43 * @mask: bitmask for two's complement
44 * subtraction of non 64 bit counters
45 * @mult: cycle to nanosecond multiplier
46 * @shift: cycle to nanosecond divisor (power of two)
47 * @update_callback: called when safe to alter clocksource values
Thomas Gleixner73b08d22007-02-16 01:27:36 -080048 * @flags: flags describing special properties
Roman Zippel19923c12006-06-26 00:25:18 -070049 * @cycle_interval: Used internally by timekeeping core, please ignore.
50 * @xtime_interval: Used internally by timekeeping core, please ignore.
john stultz734efb42006-06-26 00:25:05 -070051 */
52struct clocksource {
53 char *name;
54 struct list_head list;
55 int rating;
56 cycle_t (*read)(void);
57 cycle_t mask;
58 u32 mult;
59 u32 shift;
60 int (*update_callback)(void);
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;
john stultz734efb42006-06-26 00:25:05 -070067};
68
Thomas Gleixner73b08d22007-02-16 01:27:36 -080069/*
70 * Clock source flags bits::
71 */
72#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
73#define CLOCK_SOURCE_MUST_VERIFY 0x02
74
Jim Cromie7f9f3032006-06-26 00:25:15 -070075/* simplify initialization of mask field */
76#define CLOCKSOURCE_MASK(bits) (cycle_t)(bits<64 ? ((1ULL<<bits)-1) : -1)
john stultz734efb42006-06-26 00:25:05 -070077
78/**
79 * clocksource_khz2mult - calculates mult from khz and shift
80 * @khz: Clocksource frequency in KHz
81 * @shift_constant: Clocksource shift factor
82 *
83 * Helper functions that converts a khz counter frequency to a timsource
84 * multiplier, given the clocksource shift value
85 */
86static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
87{
88 /* khz = cyc/(Million ns)
89 * mult/2^shift = ns/cyc
90 * mult = ns/cyc * 2^shift
91 * mult = 1Million/khz * 2^shift
92 * mult = 1000000 * 2^shift / khz
93 * mult = (1000000<<shift) / khz
94 */
95 u64 tmp = ((u64)1000000) << shift_constant;
96
97 tmp += khz/2; /* round for do_div */
98 do_div(tmp, khz);
99
100 return (u32)tmp;
101}
102
103/**
104 * clocksource_hz2mult - calculates mult from hz and shift
105 * @hz: Clocksource frequency in Hz
106 * @shift_constant: Clocksource shift factor
107 *
108 * Helper functions that converts a hz counter
109 * frequency to a timsource multiplier, given the
110 * clocksource shift value
111 */
112static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
113{
114 /* hz = cyc/(Billion ns)
115 * mult/2^shift = ns/cyc
116 * mult = ns/cyc * 2^shift
117 * mult = 1Billion/hz * 2^shift
118 * mult = 1000000000 * 2^shift / hz
119 * mult = (1000000000<<shift) / hz
120 */
121 u64 tmp = ((u64)1000000000) << shift_constant;
122
123 tmp += hz/2; /* round for do_div */
124 do_div(tmp, hz);
125
126 return (u32)tmp;
127}
128
129/**
john stultza2752542006-06-26 00:25:14 -0700130 * clocksource_read: - Access the clocksource's current cycle value
john stultz734efb42006-06-26 00:25:05 -0700131 * @cs: pointer to clocksource being read
132 *
133 * Uses the clocksource to return the current cycle_t value
134 */
john stultza2752542006-06-26 00:25:14 -0700135static inline cycle_t clocksource_read(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700136{
137 return cs->read();
138}
139
140/**
141 * cyc2ns - converts clocksource cycles to nanoseconds
142 * @cs: Pointer to clocksource
143 * @cycles: Cycles
144 *
145 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
146 *
147 * XXX - This could use some mult_lxl_ll() asm optimization
148 */
149static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
150{
151 u64 ret = (u64)cycles;
152 ret = (ret * cs->mult) >> cs->shift;
153 return ret;
154}
155
156/**
john stultza2752542006-06-26 00:25:14 -0700157 * clocksource_calculate_interval - Calculates a clocksource interval struct
john stultz734efb42006-06-26 00:25:05 -0700158 *
159 * @c: Pointer to clocksource.
160 * @length_nsec: Desired interval length in nanoseconds.
161 *
162 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
163 * pair and interval request.
164 *
165 * Unless you're the timekeeping code, you should not be using this!
166 */
john stultza2752542006-06-26 00:25:14 -0700167static inline void clocksource_calculate_interval(struct clocksource *c,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800168 unsigned long length_nsec)
john stultz734efb42006-06-26 00:25:05 -0700169{
170 u64 tmp;
171
172 /* XXX - All of this could use a whole lot of optimization */
173 tmp = length_nsec;
174 tmp <<= c->shift;
175 tmp += c->mult/2;
176 do_div(tmp, c->mult);
177
Roman Zippel19923c12006-06-26 00:25:18 -0700178 c->cycle_interval = (cycle_t)tmp;
179 if (c->cycle_interval == 0)
180 c->cycle_interval = 1;
john stultz734efb42006-06-26 00:25:05 -0700181
Roman Zippel19923c12006-06-26 00:25:18 -0700182 c->xtime_interval = (u64)c->cycle_interval * c->mult;
john stultz5eb6d202006-06-26 00:25:07 -0700183}
184
185
john stultz734efb42006-06-26 00:25:05 -0700186/* used to install a new clocksource */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800187extern int clocksource_register(struct clocksource*);
188extern struct clocksource* clocksource_get_next(void);
189extern void clocksource_change_rating(struct clocksource *cs, int rating);
john stultz734efb42006-06-26 00:25:05 -0700190
191#endif /* _LINUX_CLOCKSOURCE_H */