john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 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> |
Eric Dumazet | 329c8d8 | 2007-05-08 00:27:57 -0700 | [diff] [blame] | 15 | #include <linux/cache.h> |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 16 | #include <linux/timer.h> |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 17 | #include <asm/div64.h> |
| 18 | #include <asm/io.h> |
| 19 | |
| 20 | /* clocksource cycle base type */ |
| 21 | typedef u64 cycle_t; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 22 | struct clocksource; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 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) |
Thomas Gleixner | 73b08d2 | 2007-02-16 01:27:36 -0800 | [diff] [blame] | 50 | * @flags: flags describing special properties |
john stultz | acc9a9d | 2007-02-16 01:28:17 -0800 | [diff] [blame] | 51 | * @vread: vsyscall based read |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 52 | * @resume: resume function for the clocksource, if necessary |
Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 53 | * @cycle_interval: Used internally by timekeeping core, please ignore. |
| 54 | * @xtime_interval: Used internally by timekeeping core, please ignore. |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 55 | */ |
| 56 | struct clocksource { |
Eric Dumazet | 329c8d8 | 2007-05-08 00:27:57 -0700 | [diff] [blame] | 57 | /* |
| 58 | * First part of structure is read mostly |
| 59 | */ |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 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; |
Thomas Gleixner | 73b08d2 | 2007-02-16 01:27:36 -0800 | [diff] [blame] | 67 | unsigned long flags; |
john stultz | acc9a9d | 2007-02-16 01:28:17 -0800 | [diff] [blame] | 68 | cycle_t (*vread)(void); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 69 | void (*resume)(void); |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 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 |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 76 | |
| 77 | /* timekeeping specific data, ignore */ |
Eric Dumazet | 329c8d8 | 2007-05-08 00:27:57 -0700 | [diff] [blame] | 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; |
Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 87 | s64 error; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 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 |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Thomas Gleixner | 73b08d2 | 2007-02-16 01:27:36 -0800 | [diff] [blame] | 96 | /* |
| 97 | * Clock source flags bits:: |
| 98 | */ |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 99 | #define CLOCK_SOURCE_IS_CONTINUOUS 0x01 |
| 100 | #define CLOCK_SOURCE_MUST_VERIFY 0x02 |
| 101 | |
| 102 | #define CLOCK_SOURCE_WATCHDOG 0x10 |
| 103 | #define CLOCK_SOURCE_VALID_FOR_HRES 0x20 |
Thomas Gleixner | 73b08d2 | 2007-02-16 01:27:36 -0800 | [diff] [blame] | 104 | |
Jim Cromie | 7f9f303 | 2006-06-26 00:25:15 -0700 | [diff] [blame] | 105 | /* simplify initialization of mask field */ |
Atsushi Nemoto | 1d76c26 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 106 | #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * clocksource_khz2mult - calculates mult from khz and shift |
| 110 | * @khz: Clocksource frequency in KHz |
| 111 | * @shift_constant: Clocksource shift factor |
| 112 | * |
| 113 | * Helper functions that converts a khz counter frequency to a timsource |
| 114 | * multiplier, given the clocksource shift value |
| 115 | */ |
| 116 | static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant) |
| 117 | { |
| 118 | /* khz = cyc/(Million ns) |
| 119 | * mult/2^shift = ns/cyc |
| 120 | * mult = ns/cyc * 2^shift |
| 121 | * mult = 1Million/khz * 2^shift |
| 122 | * mult = 1000000 * 2^shift / khz |
| 123 | * mult = (1000000<<shift) / khz |
| 124 | */ |
| 125 | u64 tmp = ((u64)1000000) << shift_constant; |
| 126 | |
| 127 | tmp += khz/2; /* round for do_div */ |
| 128 | do_div(tmp, khz); |
| 129 | |
| 130 | return (u32)tmp; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * clocksource_hz2mult - calculates mult from hz and shift |
| 135 | * @hz: Clocksource frequency in Hz |
| 136 | * @shift_constant: Clocksource shift factor |
| 137 | * |
| 138 | * Helper functions that converts a hz counter |
| 139 | * frequency to a timsource multiplier, given the |
| 140 | * clocksource shift value |
| 141 | */ |
| 142 | static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant) |
| 143 | { |
| 144 | /* hz = cyc/(Billion ns) |
| 145 | * mult/2^shift = ns/cyc |
| 146 | * mult = ns/cyc * 2^shift |
| 147 | * mult = 1Billion/hz * 2^shift |
| 148 | * mult = 1000000000 * 2^shift / hz |
| 149 | * mult = (1000000000<<shift) / hz |
| 150 | */ |
| 151 | u64 tmp = ((u64)1000000000) << shift_constant; |
| 152 | |
| 153 | tmp += hz/2; /* round for do_div */ |
| 154 | do_div(tmp, hz); |
| 155 | |
| 156 | return (u32)tmp; |
| 157 | } |
| 158 | |
| 159 | /** |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 160 | * clocksource_read: - Access the clocksource's current cycle value |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 161 | * @cs: pointer to clocksource being read |
| 162 | * |
| 163 | * Uses the clocksource to return the current cycle_t value |
| 164 | */ |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 165 | static inline cycle_t clocksource_read(struct clocksource *cs) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 166 | { |
| 167 | return cs->read(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * cyc2ns - converts clocksource cycles to nanoseconds |
| 172 | * @cs: Pointer to clocksource |
| 173 | * @cycles: Cycles |
| 174 | * |
| 175 | * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds. |
| 176 | * |
| 177 | * XXX - This could use some mult_lxl_ll() asm optimization |
| 178 | */ |
| 179 | static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles) |
| 180 | { |
| 181 | u64 ret = (u64)cycles; |
| 182 | ret = (ret * cs->mult) >> cs->shift; |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /** |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 187 | * clocksource_calculate_interval - Calculates a clocksource interval struct |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 188 | * |
| 189 | * @c: Pointer to clocksource. |
| 190 | * @length_nsec: Desired interval length in nanoseconds. |
| 191 | * |
| 192 | * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment |
| 193 | * pair and interval request. |
| 194 | * |
| 195 | * Unless you're the timekeeping code, you should not be using this! |
| 196 | */ |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 197 | static inline void clocksource_calculate_interval(struct clocksource *c, |
Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 198 | unsigned long length_nsec) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 199 | { |
| 200 | u64 tmp; |
| 201 | |
| 202 | /* XXX - All of this could use a whole lot of optimization */ |
| 203 | tmp = length_nsec; |
| 204 | tmp <<= c->shift; |
| 205 | tmp += c->mult/2; |
| 206 | do_div(tmp, c->mult); |
| 207 | |
Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 208 | c->cycle_interval = (cycle_t)tmp; |
| 209 | if (c->cycle_interval == 0) |
| 210 | c->cycle_interval = 1; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 211 | |
Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 212 | c->xtime_interval = (u64)c->cycle_interval * c->mult; |
john stultz | 5eb6d20 | 2006-06-26 00:25:07 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 216 | /* used to install a new clocksource */ |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 217 | extern int clocksource_register(struct clocksource*); |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 218 | extern void clocksource_unregister(struct clocksource*); |
Jason Wessel | 7c3078b | 2008-02-15 14:55:54 -0600 | [diff] [blame] | 219 | extern void clocksource_touch_watchdog(void); |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 220 | extern struct clocksource* clocksource_get_next(void); |
| 221 | extern void clocksource_change_rating(struct clocksource *cs, int rating); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 222 | extern void clocksource_resume(void); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 223 | |
john stultz | acc9a9d | 2007-02-16 01:28:17 -0800 | [diff] [blame] | 224 | #ifdef CONFIG_GENERIC_TIME_VSYSCALL |
| 225 | extern void update_vsyscall(struct timespec *ts, struct clocksource *c); |
Tony Breeds | 2c62214 | 2007-10-18 03:04:57 -0700 | [diff] [blame] | 226 | extern void update_vsyscall_tz(void); |
john stultz | acc9a9d | 2007-02-16 01:28:17 -0800 | [diff] [blame] | 227 | #else |
| 228 | static inline void update_vsyscall(struct timespec *ts, struct clocksource *c) |
| 229 | { |
| 230 | } |
Tony Breeds | 2c62214 | 2007-10-18 03:04:57 -0700 | [diff] [blame] | 231 | |
| 232 | static inline void update_vsyscall_tz(void) |
| 233 | { |
| 234 | } |
john stultz | acc9a9d | 2007-02-16 01:28:17 -0800 | [diff] [blame] | 235 | #endif |
| 236 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 237 | #endif /* _LINUX_CLOCKSOURCE_H */ |