]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/linux/clocksource.h
Merge branch 'irq-fix' of git://www.modarm9.com/gitsrc/pub/people/ukleinek/linux...
[mirror_ubuntu-zesty-kernel.git] / include / linux / clocksource.h
CommitLineData
734efb46
JS
1/* 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>
329c8d84 15#include <linux/cache.h>
5d8b34fd 16#include <linux/timer.h>
734efb46
JS
17#include <asm/div64.h>
18#include <asm/io.h>
19
20/* clocksource cycle base type */
21typedef u64 cycle_t;
5d8b34fd 22struct clocksource;
734efb46
JS
23
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)
73b08d2a 50 * @flags: flags describing special properties
acc9a9dc 51 * @vread: vsyscall based read
b52f52a0 52 * @resume: resume function for the clocksource, if necessary
19923c19
RZ
53 * @cycle_interval: Used internally by timekeeping core, please ignore.
54 * @xtime_interval: Used internally by timekeeping core, please ignore.
734efb46
JS
55 */
56struct clocksource {
329c8d84
ED
57 /*
58 * First part of structure is read mostly
59 */
734efb46
JS
60 char *name;
61 struct list_head list;
62 int rating;
63 cycle_t (*read)(void);
64 cycle_t mask;
65 u32 mult;
66 u32 shift;
73b08d2a 67 unsigned long flags;
acc9a9dc 68 cycle_t (*vread)(void);
b52f52a0 69 void (*resume)(void);
0aa366f3
TL
70#ifdef CONFIG_IA64
71 void *fsys_mmio; /* used by fsyscall asm code */
72#define CLKSRC_FSYS_MMIO_SET(mmio, addr) ((mmio) = (addr))
73#else
74#define CLKSRC_FSYS_MMIO_SET(mmio, addr) do { } while (0)
75#endif
734efb46
JS
76
77 /* timekeeping specific data, ignore */
329c8d84
ED
78 cycle_t cycle_interval;
79 u64 xtime_interval;
80 /*
81 * Second part is written at each timer interrupt
82 * Keep it in a different cache line to dirty no
83 * more than one cache line.
84 */
85 cycle_t cycle_last ____cacheline_aligned_in_smp;
86 u64 xtime_nsec;
19923c19 87 s64 error;
5d8b34fd
TG
88
89#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
90 /* Watchdog related data, used by the framework */
91 struct list_head wd_list;
92 cycle_t wd_last;
93#endif
734efb46
JS
94};
95
7dffa3c6
RZ
96extern struct clocksource *clock; /* current clocksource */
97
73b08d2a
TG
98/*
99 * Clock source flags bits::
100 */
5d8b34fd
TG
101#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
102#define CLOCK_SOURCE_MUST_VERIFY 0x02
103
104#define CLOCK_SOURCE_WATCHDOG 0x10
105#define CLOCK_SOURCE_VALID_FOR_HRES 0x20
73b08d2a 106
7f9f303a 107/* simplify initialization of mask field */
1d76c262 108#define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
734efb46
JS
109
110/**
111 * clocksource_khz2mult - calculates mult from khz and shift
112 * @khz: Clocksource frequency in KHz
113 * @shift_constant: Clocksource shift factor
114 *
115 * Helper functions that converts a khz counter frequency to a timsource
116 * multiplier, given the clocksource shift value
117 */
118static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
119{
120 /* khz = cyc/(Million ns)
121 * mult/2^shift = ns/cyc
122 * mult = ns/cyc * 2^shift
123 * mult = 1Million/khz * 2^shift
124 * mult = 1000000 * 2^shift / khz
125 * mult = (1000000<<shift) / khz
126 */
127 u64 tmp = ((u64)1000000) << shift_constant;
128
129 tmp += khz/2; /* round for do_div */
130 do_div(tmp, khz);
131
132 return (u32)tmp;
133}
134
135/**
136 * clocksource_hz2mult - calculates mult from hz and shift
137 * @hz: Clocksource frequency in Hz
138 * @shift_constant: Clocksource shift factor
139 *
140 * Helper functions that converts a hz counter
141 * frequency to a timsource multiplier, given the
142 * clocksource shift value
143 */
144static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
145{
146 /* hz = cyc/(Billion ns)
147 * mult/2^shift = ns/cyc
148 * mult = ns/cyc * 2^shift
149 * mult = 1Billion/hz * 2^shift
150 * mult = 1000000000 * 2^shift / hz
151 * mult = (1000000000<<shift) / hz
152 */
153 u64 tmp = ((u64)1000000000) << shift_constant;
154
155 tmp += hz/2; /* round for do_div */
156 do_div(tmp, hz);
157
158 return (u32)tmp;
159}
160
161/**
a2752549 162 * clocksource_read: - Access the clocksource's current cycle value
734efb46
JS
163 * @cs: pointer to clocksource being read
164 *
165 * Uses the clocksource to return the current cycle_t value
166 */
a2752549 167static inline cycle_t clocksource_read(struct clocksource *cs)
734efb46
JS
168{
169 return cs->read();
170}
171
172/**
173 * cyc2ns - converts clocksource cycles to nanoseconds
174 * @cs: Pointer to clocksource
175 * @cycles: Cycles
176 *
177 * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds.
178 *
179 * XXX - This could use some mult_lxl_ll() asm optimization
180 */
181static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
182{
183 u64 ret = (u64)cycles;
184 ret = (ret * cs->mult) >> cs->shift;
185 return ret;
186}
187
188/**
a2752549 189 * clocksource_calculate_interval - Calculates a clocksource interval struct
734efb46
JS
190 *
191 * @c: Pointer to clocksource.
192 * @length_nsec: Desired interval length in nanoseconds.
193 *
194 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
195 * pair and interval request.
196 *
197 * Unless you're the timekeeping code, you should not be using this!
198 */
a2752549 199static inline void clocksource_calculate_interval(struct clocksource *c,
f5f1a24a 200 unsigned long length_nsec)
734efb46
JS
201{
202 u64 tmp;
203
204 /* XXX - All of this could use a whole lot of optimization */
205 tmp = length_nsec;
206 tmp <<= c->shift;
207 tmp += c->mult/2;
208 do_div(tmp, c->mult);
209
19923c19
RZ
210 c->cycle_interval = (cycle_t)tmp;
211 if (c->cycle_interval == 0)
212 c->cycle_interval = 1;
734efb46 213
19923c19 214 c->xtime_interval = (u64)c->cycle_interval * c->mult;
5eb6d205
JS
215}
216
217
734efb46 218/* used to install a new clocksource */
92c7e002 219extern int clocksource_register(struct clocksource*);
4713e22c 220extern void clocksource_unregister(struct clocksource*);
7c3078b6 221extern void clocksource_touch_watchdog(void);
92c7e002
TG
222extern struct clocksource* clocksource_get_next(void);
223extern void clocksource_change_rating(struct clocksource *cs, int rating);
b52f52a0 224extern void clocksource_resume(void);
734efb46 225
acc9a9dc
JS
226#ifdef CONFIG_GENERIC_TIME_VSYSCALL
227extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
2c622148 228extern void update_vsyscall_tz(void);
acc9a9dc
JS
229#else
230static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
231{
232}
2c622148
TB
233
234static inline void update_vsyscall_tz(void)
235{
236}
acc9a9dc
JS
237#endif
238
734efb46 239#endif /* _LINUX_CLOCKSOURCE_H */