| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/kernel/time/timekeeping.c | 
|  | 3 | * | 
|  | 4 | *  Kernel timekeeping code and accessor functions | 
|  | 5 | * | 
|  | 6 | *  This code was moved from linux/kernel/timer.c. | 
|  | 7 | *  Please see that file for copyright and history logs. | 
|  | 8 | * | 
|  | 9 | */ | 
|  | 10 |  | 
|  | 11 | #include <linux/module.h> | 
|  | 12 | #include <linux/interrupt.h> | 
|  | 13 | #include <linux/percpu.h> | 
|  | 14 | #include <linux/init.h> | 
|  | 15 | #include <linux/mm.h> | 
| Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 17 | #include <linux/sysdev.h> | 
|  | 18 | #include <linux/clocksource.h> | 
|  | 19 | #include <linux/jiffies.h> | 
|  | 20 | #include <linux/time.h> | 
|  | 21 | #include <linux/tick.h> | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 22 | #include <linux/stop_machine.h> | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 23 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 24 | /* Structure holding internal timekeeping values. */ | 
|  | 25 | struct timekeeper { | 
|  | 26 | /* Current clocksource used for timekeeping. */ | 
|  | 27 | struct clocksource *clock; | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 28 | /* The shift value of the current clocksource. */ | 
|  | 29 | int	shift; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 30 |  | 
|  | 31 | /* Number of clock cycles in one NTP interval. */ | 
|  | 32 | cycle_t cycle_interval; | 
|  | 33 | /* Number of clock shifted nano seconds in one NTP interval. */ | 
|  | 34 | u64	xtime_interval; | 
|  | 35 | /* Raw nano seconds accumulated per NTP interval. */ | 
|  | 36 | u32	raw_interval; | 
|  | 37 |  | 
|  | 38 | /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */ | 
|  | 39 | u64	xtime_nsec; | 
|  | 40 | /* Difference between accumulated time and NTP time in ntp | 
|  | 41 | * shifted nano seconds. */ | 
|  | 42 | s64	ntp_error; | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 43 | /* Shift conversion between clock shifted nano seconds and | 
|  | 44 | * ntp shifted nano seconds. */ | 
|  | 45 | int	ntp_error_shift; | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 46 | /* NTP adjusted clock multiplier */ | 
|  | 47 | u32	mult; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 48 | }; | 
|  | 49 |  | 
|  | 50 | struct timekeeper timekeeper; | 
|  | 51 |  | 
|  | 52 | /** | 
|  | 53 | * timekeeper_setup_internals - Set up internals to use clocksource clock. | 
|  | 54 | * | 
|  | 55 | * @clock:		Pointer to clocksource. | 
|  | 56 | * | 
|  | 57 | * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment | 
|  | 58 | * pair and interval request. | 
|  | 59 | * | 
|  | 60 | * Unless you're the timekeeping code, you should not be using this! | 
|  | 61 | */ | 
|  | 62 | static void timekeeper_setup_internals(struct clocksource *clock) | 
|  | 63 | { | 
|  | 64 | cycle_t interval; | 
|  | 65 | u64 tmp; | 
|  | 66 |  | 
|  | 67 | timekeeper.clock = clock; | 
|  | 68 | clock->cycle_last = clock->read(clock); | 
|  | 69 |  | 
|  | 70 | /* Do the ns -> cycle conversion first, using original mult */ | 
|  | 71 | tmp = NTP_INTERVAL_LENGTH; | 
|  | 72 | tmp <<= clock->shift; | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 73 | tmp += clock->mult/2; | 
|  | 74 | do_div(tmp, clock->mult); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 75 | if (tmp == 0) | 
|  | 76 | tmp = 1; | 
|  | 77 |  | 
|  | 78 | interval = (cycle_t) tmp; | 
|  | 79 | timekeeper.cycle_interval = interval; | 
|  | 80 |  | 
|  | 81 | /* Go back from cycles -> shifted ns */ | 
|  | 82 | timekeeper.xtime_interval = (u64) interval * clock->mult; | 
|  | 83 | timekeeper.raw_interval = | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 84 | ((u64) interval * clock->mult) >> clock->shift; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 85 |  | 
|  | 86 | timekeeper.xtime_nsec = 0; | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 87 | timekeeper.shift = clock->shift; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 88 |  | 
|  | 89 | timekeeper.ntp_error = 0; | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 90 | timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 91 |  | 
|  | 92 | /* | 
|  | 93 | * The timekeeper keeps its own mult values for the currently | 
|  | 94 | * active clocksource. These value will be adjusted via NTP | 
|  | 95 | * to counteract clock drifting. | 
|  | 96 | */ | 
|  | 97 | timekeeper.mult = clock->mult; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 98 | } | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 99 |  | 
| Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 100 | /* Timekeeper helper functions. */ | 
|  | 101 | static inline s64 timekeeping_get_ns(void) | 
|  | 102 | { | 
|  | 103 | cycle_t cycle_now, cycle_delta; | 
|  | 104 | struct clocksource *clock; | 
|  | 105 |  | 
|  | 106 | /* read clocksource: */ | 
|  | 107 | clock = timekeeper.clock; | 
|  | 108 | cycle_now = clock->read(clock); | 
|  | 109 |  | 
|  | 110 | /* calculate the delta since the last update_wall_time: */ | 
|  | 111 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 
|  | 112 |  | 
|  | 113 | /* return delta convert to nanoseconds using ntp adjusted mult. */ | 
|  | 114 | return clocksource_cyc2ns(cycle_delta, timekeeper.mult, | 
|  | 115 | timekeeper.shift); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | static inline s64 timekeeping_get_ns_raw(void) | 
|  | 119 | { | 
|  | 120 | cycle_t cycle_now, cycle_delta; | 
|  | 121 | struct clocksource *clock; | 
|  | 122 |  | 
|  | 123 | /* read clocksource: */ | 
|  | 124 | clock = timekeeper.clock; | 
|  | 125 | cycle_now = clock->read(clock); | 
|  | 126 |  | 
|  | 127 | /* calculate the delta since the last update_wall_time: */ | 
|  | 128 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 
|  | 129 |  | 
|  | 130 | /* return delta convert to nanoseconds using ntp adjusted mult. */ | 
|  | 131 | return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); | 
|  | 132 | } | 
|  | 133 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 134 | /* | 
|  | 135 | * This read-write spinlock protects us from races in SMP while | 
| Thomas Gleixner | dce48a8 | 2009-04-11 10:43:41 +0200 | [diff] [blame] | 136 | * playing with xtime. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 137 | */ | 
| Adrian Bunk | ba2a631 | 2007-10-16 23:27:16 -0700 | [diff] [blame] | 138 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 139 |  | 
|  | 140 |  | 
|  | 141 | /* | 
|  | 142 | * The current time | 
|  | 143 | * wall_to_monotonic is what we need to add to xtime (or xtime corrected | 
|  | 144 | * for sub jiffie times) to get to monotonic time.  Monotonic is pegged | 
|  | 145 | * at zero at system boot time, so wall_to_monotonic will be negative, | 
|  | 146 | * however, we will ALWAYS keep the tv_nsec part positive so we can use | 
|  | 147 | * the usual normalization. | 
| Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 148 | * | 
|  | 149 | * wall_to_monotonic is moved after resume from suspend for the monotonic | 
|  | 150 | * time not to jump. We need to add total_sleep_time to wall_to_monotonic | 
|  | 151 | * to get the real boot based time offset. | 
|  | 152 | * | 
|  | 153 | * - wall_to_monotonic is no longer the boot time, getboottime must be | 
|  | 154 | * used instead. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 155 | */ | 
|  | 156 | struct timespec xtime __attribute__ ((aligned (16))); | 
|  | 157 | struct timespec wall_to_monotonic __attribute__ ((aligned (16))); | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 158 | static struct timespec total_sleep_time; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 159 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 160 | /* | 
|  | 161 | * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. | 
|  | 162 | */ | 
|  | 163 | struct timespec raw_time; | 
|  | 164 |  | 
| Thomas Gleixner | 1c5745a | 2008-12-22 23:05:28 +0100 | [diff] [blame] | 165 | /* flag for if timekeeping is suspended */ | 
|  | 166 | int __read_mostly timekeeping_suspended; | 
|  | 167 |  | 
| John Stultz | 31089c1 | 2009-08-14 15:47:18 +0200 | [diff] [blame] | 168 | /* must hold xtime_lock */ | 
|  | 169 | void timekeeping_leap_insert(int leapsecond) | 
|  | 170 | { | 
|  | 171 | xtime.tv_sec += leapsecond; | 
|  | 172 | wall_to_monotonic.tv_sec -= leapsecond; | 
| Lin Ming | 0696b71 | 2009-11-17 13:49:50 +0800 | [diff] [blame] | 173 | update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult); | 
| John Stultz | 31089c1 | 2009-08-14 15:47:18 +0200 | [diff] [blame] | 174 | } | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 175 |  | 
|  | 176 | #ifdef CONFIG_GENERIC_TIME | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 177 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 178 | /** | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 179 | * timekeeping_forward_now - update clock to the current time | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 180 | * | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 181 | * Forward the current clock to update its state since the last call to | 
|  | 182 | * update_wall_time(). This is useful before significant clock changes, | 
|  | 183 | * as it avoids having to deal with this time offset explicitly. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 184 | */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 185 | static void timekeeping_forward_now(void) | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 186 | { | 
|  | 187 | cycle_t cycle_now, cycle_delta; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 188 | struct clocksource *clock; | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 189 | s64 nsec; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 190 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 191 | clock = timekeeper.clock; | 
| Martin Schwidefsky | a0f7d48 | 2009-08-14 15:47:19 +0200 | [diff] [blame] | 192 | cycle_now = clock->read(clock); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 193 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 194 | clock->cycle_last = cycle_now; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 195 |  | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 196 | nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult, | 
|  | 197 | timekeeper.shift); | 
| john stultz | 7d27558 | 2009-05-01 13:10:26 -0700 | [diff] [blame] | 198 |  | 
|  | 199 | /* If arch requires, add in gettimeoffset() */ | 
|  | 200 | nsec += arch_gettimeoffset(); | 
|  | 201 |  | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 202 | timespec_add_ns(&xtime, nsec); | 
| John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 203 |  | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 204 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 205 | timespec_add_ns(&raw_time, nsec); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
|  | 208 | /** | 
| Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 209 | * getnstimeofday - Returns the time of day in a timespec | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 210 | * @ts:		pointer to the timespec to be set | 
|  | 211 | * | 
| Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 212 | * Returns the time of day in a timespec. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 213 | */ | 
| Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 214 | void getnstimeofday(struct timespec *ts) | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 215 | { | 
|  | 216 | unsigned long seq; | 
|  | 217 | s64 nsecs; | 
|  | 218 |  | 
| Thomas Gleixner | 1c5745a | 2008-12-22 23:05:28 +0100 | [diff] [blame] | 219 | WARN_ON(timekeeping_suspended); | 
|  | 220 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 221 | do { | 
|  | 222 | seq = read_seqbegin(&xtime_lock); | 
|  | 223 |  | 
|  | 224 | *ts = xtime; | 
| Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 225 | nsecs = timekeeping_get_ns(); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 226 |  | 
| john stultz | 7d27558 | 2009-05-01 13:10:26 -0700 | [diff] [blame] | 227 | /* If arch requires, add in gettimeoffset() */ | 
|  | 228 | nsecs += arch_gettimeoffset(); | 
|  | 229 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 230 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 231 |  | 
|  | 232 | timespec_add_ns(ts, nsecs); | 
|  | 233 | } | 
|  | 234 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 235 | EXPORT_SYMBOL(getnstimeofday); | 
|  | 236 |  | 
| Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 237 | ktime_t ktime_get(void) | 
|  | 238 | { | 
| Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 239 | unsigned int seq; | 
|  | 240 | s64 secs, nsecs; | 
|  | 241 |  | 
|  | 242 | WARN_ON(timekeeping_suspended); | 
|  | 243 |  | 
|  | 244 | do { | 
|  | 245 | seq = read_seqbegin(&xtime_lock); | 
|  | 246 | secs = xtime.tv_sec + wall_to_monotonic.tv_sec; | 
|  | 247 | nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec; | 
| Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 248 | nsecs += timekeeping_get_ns(); | 
| Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 249 |  | 
|  | 250 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 251 | /* | 
|  | 252 | * Use ktime_set/ktime_add_ns to create a proper ktime on | 
|  | 253 | * 32-bit architectures without CONFIG_KTIME_SCALAR. | 
|  | 254 | */ | 
|  | 255 | return ktime_add_ns(ktime_set(secs, 0), nsecs); | 
|  | 256 | } | 
|  | 257 | EXPORT_SYMBOL_GPL(ktime_get); | 
|  | 258 |  | 
|  | 259 | /** | 
|  | 260 | * ktime_get_ts - get the monotonic clock in timespec format | 
|  | 261 | * @ts:		pointer to timespec variable | 
|  | 262 | * | 
|  | 263 | * The function calculates the monotonic clock from the realtime | 
|  | 264 | * clock and the wall_to_monotonic offset and stores the result | 
|  | 265 | * in normalized timespec format in the variable pointed to by @ts. | 
|  | 266 | */ | 
|  | 267 | void ktime_get_ts(struct timespec *ts) | 
|  | 268 | { | 
| Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 269 | struct timespec tomono; | 
|  | 270 | unsigned int seq; | 
|  | 271 | s64 nsecs; | 
|  | 272 |  | 
|  | 273 | WARN_ON(timekeeping_suspended); | 
|  | 274 |  | 
|  | 275 | do { | 
|  | 276 | seq = read_seqbegin(&xtime_lock); | 
|  | 277 | *ts = xtime; | 
|  | 278 | tomono = wall_to_monotonic; | 
| Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 279 | nsecs = timekeeping_get_ns(); | 
| Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 280 |  | 
|  | 281 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 282 |  | 
|  | 283 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, | 
|  | 284 | ts->tv_nsec + tomono.tv_nsec + nsecs); | 
|  | 285 | } | 
|  | 286 | EXPORT_SYMBOL_GPL(ktime_get_ts); | 
|  | 287 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 288 | /** | 
|  | 289 | * do_gettimeofday - Returns the time of day in a timeval | 
|  | 290 | * @tv:		pointer to the timeval to be set | 
|  | 291 | * | 
| Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 292 | * NOTE: Users should be converted to using getnstimeofday() | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 293 | */ | 
|  | 294 | void do_gettimeofday(struct timeval *tv) | 
|  | 295 | { | 
|  | 296 | struct timespec now; | 
|  | 297 |  | 
| Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 298 | getnstimeofday(&now); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 299 | tv->tv_sec = now.tv_sec; | 
|  | 300 | tv->tv_usec = now.tv_nsec/1000; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | EXPORT_SYMBOL(do_gettimeofday); | 
|  | 304 | /** | 
|  | 305 | * do_settimeofday - Sets the time of day | 
|  | 306 | * @tv:		pointer to the timespec variable containing the new time | 
|  | 307 | * | 
|  | 308 | * Sets the time of day to the new time and update NTP and notify hrtimers | 
|  | 309 | */ | 
|  | 310 | int do_settimeofday(struct timespec *tv) | 
|  | 311 | { | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 312 | struct timespec ts_delta; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 313 | unsigned long flags; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 314 |  | 
|  | 315 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | 
|  | 316 | return -EINVAL; | 
|  | 317 |  | 
|  | 318 | write_seqlock_irqsave(&xtime_lock, flags); | 
|  | 319 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 320 | timekeeping_forward_now(); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 321 |  | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 322 | ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec; | 
|  | 323 | ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec; | 
|  | 324 | wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 325 |  | 
| Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 326 | xtime = *tv; | 
|  | 327 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 328 | timekeeper.ntp_error = 0; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 329 | ntp_clear(); | 
|  | 330 |  | 
| Lin Ming | 0696b71 | 2009-11-17 13:49:50 +0800 | [diff] [blame] | 331 | update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 332 |  | 
|  | 333 | write_sequnlock_irqrestore(&xtime_lock, flags); | 
|  | 334 |  | 
|  | 335 | /* signal hrtimers about time change */ | 
|  | 336 | clock_was_set(); | 
|  | 337 |  | 
|  | 338 | return 0; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | EXPORT_SYMBOL(do_settimeofday); | 
|  | 342 |  | 
|  | 343 | /** | 
|  | 344 | * change_clocksource - Swaps clocksources if a new one is available | 
|  | 345 | * | 
|  | 346 | * Accumulates current time interval and initializes new clocksource | 
|  | 347 | */ | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 348 | static int change_clocksource(void *data) | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 349 | { | 
| Magnus Damm | 4614e6a | 2009-04-21 12:24:02 -0700 | [diff] [blame] | 350 | struct clocksource *new, *old; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 351 |  | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 352 | new = (struct clocksource *) data; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 353 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 354 | timekeeping_forward_now(); | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 355 | if (!new->enable || new->enable(new) == 0) { | 
|  | 356 | old = timekeeper.clock; | 
|  | 357 | timekeeper_setup_internals(new); | 
|  | 358 | if (old->disable) | 
|  | 359 | old->disable(old); | 
|  | 360 | } | 
|  | 361 | return 0; | 
|  | 362 | } | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 363 |  | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 364 | /** | 
|  | 365 | * timekeeping_notify - Install a new clock source | 
|  | 366 | * @clock:		pointer to the clock source | 
|  | 367 | * | 
|  | 368 | * This function is called from clocksource.c after a new, better clock | 
|  | 369 | * source has been registered. The caller holds the clocksource_mutex. | 
|  | 370 | */ | 
|  | 371 | void timekeeping_notify(struct clocksource *clock) | 
|  | 372 | { | 
|  | 373 | if (timekeeper.clock == clock) | 
| Magnus Damm | 4614e6a | 2009-04-21 12:24:02 -0700 | [diff] [blame] | 374 | return; | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 375 | stop_machine(change_clocksource, clock, NULL); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 376 | tick_clock_notify(); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 377 | } | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 378 |  | 
| Thomas Gleixner | a40f262 | 2009-07-07 13:00:31 +0200 | [diff] [blame] | 379 | #else /* GENERIC_TIME */ | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 380 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 381 | static inline void timekeeping_forward_now(void) { } | 
| Thomas Gleixner | a40f262 | 2009-07-07 13:00:31 +0200 | [diff] [blame] | 382 |  | 
|  | 383 | /** | 
|  | 384 | * ktime_get - get the monotonic time in ktime_t format | 
|  | 385 | * | 
|  | 386 | * returns the time in ktime_t format | 
|  | 387 | */ | 
|  | 388 | ktime_t ktime_get(void) | 
|  | 389 | { | 
|  | 390 | struct timespec now; | 
|  | 391 |  | 
|  | 392 | ktime_get_ts(&now); | 
|  | 393 |  | 
|  | 394 | return timespec_to_ktime(now); | 
|  | 395 | } | 
|  | 396 | EXPORT_SYMBOL_GPL(ktime_get); | 
|  | 397 |  | 
|  | 398 | /** | 
|  | 399 | * ktime_get_ts - get the monotonic clock in timespec format | 
|  | 400 | * @ts:		pointer to timespec variable | 
|  | 401 | * | 
|  | 402 | * The function calculates the monotonic clock from the realtime | 
|  | 403 | * clock and the wall_to_monotonic offset and stores the result | 
|  | 404 | * in normalized timespec format in the variable pointed to by @ts. | 
|  | 405 | */ | 
|  | 406 | void ktime_get_ts(struct timespec *ts) | 
|  | 407 | { | 
|  | 408 | struct timespec tomono; | 
|  | 409 | unsigned long seq; | 
|  | 410 |  | 
|  | 411 | do { | 
|  | 412 | seq = read_seqbegin(&xtime_lock); | 
|  | 413 | getnstimeofday(ts); | 
|  | 414 | tomono = wall_to_monotonic; | 
|  | 415 |  | 
|  | 416 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 417 |  | 
|  | 418 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, | 
|  | 419 | ts->tv_nsec + tomono.tv_nsec); | 
|  | 420 | } | 
|  | 421 | EXPORT_SYMBOL_GPL(ktime_get_ts); | 
| Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 422 |  | 
| Thomas Gleixner | a40f262 | 2009-07-07 13:00:31 +0200 | [diff] [blame] | 423 | #endif /* !GENERIC_TIME */ | 
|  | 424 |  | 
|  | 425 | /** | 
|  | 426 | * ktime_get_real - get the real (wall-) time in ktime_t format | 
|  | 427 | * | 
|  | 428 | * returns the time in ktime_t format | 
|  | 429 | */ | 
|  | 430 | ktime_t ktime_get_real(void) | 
|  | 431 | { | 
|  | 432 | struct timespec now; | 
|  | 433 |  | 
|  | 434 | getnstimeofday(&now); | 
|  | 435 |  | 
|  | 436 | return timespec_to_ktime(now); | 
|  | 437 | } | 
|  | 438 | EXPORT_SYMBOL_GPL(ktime_get_real); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 439 |  | 
|  | 440 | /** | 
| John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 441 | * getrawmonotonic - Returns the raw monotonic time in a timespec | 
|  | 442 | * @ts:		pointer to the timespec to be set | 
|  | 443 | * | 
|  | 444 | * Returns the raw monotonic time (completely un-modified by ntp) | 
|  | 445 | */ | 
|  | 446 | void getrawmonotonic(struct timespec *ts) | 
|  | 447 | { | 
|  | 448 | unsigned long seq; | 
|  | 449 | s64 nsecs; | 
| John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 450 |  | 
|  | 451 | do { | 
|  | 452 | seq = read_seqbegin(&xtime_lock); | 
| Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 453 | nsecs = timekeeping_get_ns_raw(); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 454 | *ts = raw_time; | 
| John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 455 |  | 
|  | 456 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 457 |  | 
|  | 458 | timespec_add_ns(ts, nsecs); | 
|  | 459 | } | 
|  | 460 | EXPORT_SYMBOL(getrawmonotonic); | 
|  | 461 |  | 
|  | 462 |  | 
|  | 463 | /** | 
| Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 464 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 465 | */ | 
| Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 466 | int timekeeping_valid_for_hres(void) | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 467 | { | 
|  | 468 | unsigned long seq; | 
|  | 469 | int ret; | 
|  | 470 |  | 
|  | 471 | do { | 
|  | 472 | seq = read_seqbegin(&xtime_lock); | 
|  | 473 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 474 | ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 475 |  | 
|  | 476 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 477 |  | 
|  | 478 | return ret; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | /** | 
| Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 482 | * timekeeping_max_deferment - Returns max time the clocksource can be deferred | 
|  | 483 | * | 
|  | 484 | * Caller must observe xtime_lock via read_seqbegin/read_seqretry to | 
|  | 485 | * ensure that the clocksource does not change! | 
|  | 486 | */ | 
|  | 487 | u64 timekeeping_max_deferment(void) | 
|  | 488 | { | 
|  | 489 | return timekeeper.clock->max_idle_ns; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | /** | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 493 | * read_persistent_clock -  Return time from the persistent clock. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 494 | * | 
|  | 495 | * Weak dummy function for arches that do not yet support it. | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 496 | * Reads the time from the battery backed persistent clock. | 
|  | 497 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 498 | * | 
|  | 499 | *  XXX - Do be sure to remove it once all arches implement it. | 
|  | 500 | */ | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 501 | void __attribute__((weak)) read_persistent_clock(struct timespec *ts) | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 502 | { | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 503 | ts->tv_sec = 0; | 
|  | 504 | ts->tv_nsec = 0; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 505 | } | 
|  | 506 |  | 
| Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 507 | /** | 
|  | 508 | * read_boot_clock -  Return time of the system start. | 
|  | 509 | * | 
|  | 510 | * Weak dummy function for arches that do not yet support it. | 
|  | 511 | * Function to read the exact time the system has been started. | 
|  | 512 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. | 
|  | 513 | * | 
|  | 514 | *  XXX - Do be sure to remove it once all arches implement it. | 
|  | 515 | */ | 
|  | 516 | void __attribute__((weak)) read_boot_clock(struct timespec *ts) | 
|  | 517 | { | 
|  | 518 | ts->tv_sec = 0; | 
|  | 519 | ts->tv_nsec = 0; | 
|  | 520 | } | 
|  | 521 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 522 | /* | 
|  | 523 | * timekeeping_init - Initializes the clocksource and common timekeeping values | 
|  | 524 | */ | 
|  | 525 | void __init timekeeping_init(void) | 
|  | 526 | { | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 527 | struct clocksource *clock; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 528 | unsigned long flags; | 
| Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 529 | struct timespec now, boot; | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 530 |  | 
|  | 531 | read_persistent_clock(&now); | 
| Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 532 | read_boot_clock(&boot); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 533 |  | 
|  | 534 | write_seqlock_irqsave(&xtime_lock, flags); | 
|  | 535 |  | 
| Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 536 | ntp_init(); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 537 |  | 
| Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 538 | clock = clocksource_default_clock(); | 
| Martin Schwidefsky | a0f7d48 | 2009-08-14 15:47:19 +0200 | [diff] [blame] | 539 | if (clock->enable) | 
|  | 540 | clock->enable(clock); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 541 | timekeeper_setup_internals(clock); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 542 |  | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 543 | xtime.tv_sec = now.tv_sec; | 
|  | 544 | xtime.tv_nsec = now.tv_nsec; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 545 | raw_time.tv_sec = 0; | 
|  | 546 | raw_time.tv_nsec = 0; | 
| Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 547 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) { | 
|  | 548 | boot.tv_sec = xtime.tv_sec; | 
|  | 549 | boot.tv_nsec = xtime.tv_nsec; | 
|  | 550 | } | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 551 | set_normalized_timespec(&wall_to_monotonic, | 
| Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 552 | -boot.tv_sec, -boot.tv_nsec); | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 553 | total_sleep_time.tv_sec = 0; | 
|  | 554 | total_sleep_time.tv_nsec = 0; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 555 | write_sequnlock_irqrestore(&xtime_lock, flags); | 
|  | 556 | } | 
|  | 557 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 558 | /* time in seconds when suspend began */ | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 559 | static struct timespec timekeeping_suspend_time; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 560 |  | 
|  | 561 | /** | 
|  | 562 | * timekeeping_resume - Resumes the generic timekeeping subsystem. | 
|  | 563 | * @dev:	unused | 
|  | 564 | * | 
|  | 565 | * This is for the generic clocksource timekeeping. | 
|  | 566 | * xtime/wall_to_monotonic/jiffies/etc are | 
|  | 567 | * still managed by arch specific suspend/resume code. | 
|  | 568 | */ | 
|  | 569 | static int timekeeping_resume(struct sys_device *dev) | 
|  | 570 | { | 
|  | 571 | unsigned long flags; | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 572 | struct timespec ts; | 
|  | 573 |  | 
|  | 574 | read_persistent_clock(&ts); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 575 |  | 
| Thomas Gleixner | d10ff3f | 2007-05-14 11:10:02 +0200 | [diff] [blame] | 576 | clocksource_resume(); | 
|  | 577 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 578 | write_seqlock_irqsave(&xtime_lock, flags); | 
|  | 579 |  | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 580 | if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { | 
|  | 581 | ts = timespec_sub(ts, timekeeping_suspend_time); | 
|  | 582 | xtime = timespec_add_safe(xtime, ts); | 
|  | 583 | wall_to_monotonic = timespec_sub(wall_to_monotonic, ts); | 
|  | 584 | total_sleep_time = timespec_add_safe(total_sleep_time, ts); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 585 | } | 
|  | 586 | /* re-base the last cycle value */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 587 | timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); | 
|  | 588 | timekeeper.ntp_error = 0; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 589 | timekeeping_suspended = 0; | 
|  | 590 | write_sequnlock_irqrestore(&xtime_lock, flags); | 
|  | 591 |  | 
|  | 592 | touch_softlockup_watchdog(); | 
|  | 593 |  | 
|  | 594 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); | 
|  | 595 |  | 
|  | 596 | /* Resume hrtimers */ | 
|  | 597 | hres_timers_resume(); | 
|  | 598 |  | 
|  | 599 | return 0; | 
|  | 600 | } | 
|  | 601 |  | 
|  | 602 | static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) | 
|  | 603 | { | 
|  | 604 | unsigned long flags; | 
|  | 605 |  | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 606 | read_persistent_clock(&timekeeping_suspend_time); | 
| Thomas Gleixner | 3be9095 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 607 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 608 | write_seqlock_irqsave(&xtime_lock, flags); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 609 | timekeeping_forward_now(); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 610 | timekeeping_suspended = 1; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 611 | write_sequnlock_irqrestore(&xtime_lock, flags); | 
|  | 612 |  | 
|  | 613 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | 
|  | 614 |  | 
|  | 615 | return 0; | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | /* sysfs resume/suspend bits for timekeeping */ | 
|  | 619 | static struct sysdev_class timekeeping_sysclass = { | 
| Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 620 | .name		= "timekeeping", | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 621 | .resume		= timekeeping_resume, | 
|  | 622 | .suspend	= timekeeping_suspend, | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 623 | }; | 
|  | 624 |  | 
|  | 625 | static struct sys_device device_timer = { | 
|  | 626 | .id		= 0, | 
|  | 627 | .cls		= &timekeeping_sysclass, | 
|  | 628 | }; | 
|  | 629 |  | 
|  | 630 | static int __init timekeeping_init_device(void) | 
|  | 631 | { | 
|  | 632 | int error = sysdev_class_register(&timekeeping_sysclass); | 
|  | 633 | if (!error) | 
|  | 634 | error = sysdev_register(&device_timer); | 
|  | 635 | return error; | 
|  | 636 | } | 
|  | 637 |  | 
|  | 638 | device_initcall(timekeeping_init_device); | 
|  | 639 |  | 
|  | 640 | /* | 
|  | 641 | * If the error is already larger, we look ahead even further | 
|  | 642 | * to compensate for late or lost adjustments. | 
|  | 643 | */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 644 | static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 645 | s64 *offset) | 
|  | 646 | { | 
|  | 647 | s64 tick_error, i; | 
|  | 648 | u32 look_ahead, adj; | 
|  | 649 | s32 error2, mult; | 
|  | 650 |  | 
|  | 651 | /* | 
|  | 652 | * Use the current error value to determine how much to look ahead. | 
|  | 653 | * The larger the error the slower we adjust for it to avoid problems | 
|  | 654 | * with losing too many ticks, otherwise we would overadjust and | 
|  | 655 | * produce an even larger error.  The smaller the adjustment the | 
|  | 656 | * faster we try to adjust for it, as lost ticks can do less harm | 
| Li Zefan | 3eb0567 | 2008-02-08 04:19:25 -0800 | [diff] [blame] | 657 | * here.  This is tuned so that an error of about 1 msec is adjusted | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 658 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). | 
|  | 659 | */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 660 | error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 661 | error2 = abs(error2); | 
|  | 662 | for (look_ahead = 0; error2 > 0; look_ahead++) | 
|  | 663 | error2 >>= 2; | 
|  | 664 |  | 
|  | 665 | /* | 
|  | 666 | * Now calculate the error in (1 << look_ahead) ticks, but first | 
|  | 667 | * remove the single look ahead already included in the error. | 
|  | 668 | */ | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 669 | tick_error = tick_length >> (timekeeper.ntp_error_shift + 1); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 670 | tick_error -= timekeeper.xtime_interval >> 1; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 671 | error = ((error - tick_error) >> look_ahead) + tick_error; | 
|  | 672 |  | 
|  | 673 | /* Finally calculate the adjustment shift value.  */ | 
|  | 674 | i = *interval; | 
|  | 675 | mult = 1; | 
|  | 676 | if (error < 0) { | 
|  | 677 | error = -error; | 
|  | 678 | *interval = -*interval; | 
|  | 679 | *offset = -*offset; | 
|  | 680 | mult = -1; | 
|  | 681 | } | 
|  | 682 | for (adj = 0; error > i; adj++) | 
|  | 683 | error >>= 1; | 
|  | 684 |  | 
|  | 685 | *interval <<= adj; | 
|  | 686 | *offset <<= adj; | 
|  | 687 | return mult << adj; | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | /* | 
|  | 691 | * Adjust the multiplier to reduce the error value, | 
|  | 692 | * this is optimized for the most common adjustments of -1,0,1, | 
|  | 693 | * for other values we can do a bit more work. | 
|  | 694 | */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 695 | static void timekeeping_adjust(s64 offset) | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 696 | { | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 697 | s64 error, interval = timekeeper.cycle_interval; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 698 | int adj; | 
|  | 699 |  | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 700 | error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 701 | if (error > interval) { | 
|  | 702 | error >>= 2; | 
|  | 703 | if (likely(error <= interval)) | 
|  | 704 | adj = 1; | 
|  | 705 | else | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 706 | adj = timekeeping_bigadjust(error, &interval, &offset); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 707 | } else if (error < -interval) { | 
|  | 708 | error >>= 2; | 
|  | 709 | if (likely(error >= -interval)) { | 
|  | 710 | adj = -1; | 
|  | 711 | interval = -interval; | 
|  | 712 | offset = -offset; | 
|  | 713 | } else | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 714 | adj = timekeeping_bigadjust(error, &interval, &offset); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 715 | } else | 
|  | 716 | return; | 
|  | 717 |  | 
| Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 718 | timekeeper.mult += adj; | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 719 | timekeeper.xtime_interval += interval; | 
|  | 720 | timekeeper.xtime_nsec -= offset; | 
|  | 721 | timekeeper.ntp_error -= (interval - offset) << | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 722 | timekeeper.ntp_error_shift; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 723 | } | 
|  | 724 |  | 
|  | 725 | /** | 
| john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 726 | * logarithmic_accumulation - shifted accumulation of cycles | 
|  | 727 | * | 
|  | 728 | * This functions accumulates a shifted interval of cycles into | 
|  | 729 | * into a shifted interval nanoseconds. Allows for O(log) accumulation | 
|  | 730 | * loop. | 
|  | 731 | * | 
|  | 732 | * Returns the unconsumed cycles. | 
|  | 733 | */ | 
|  | 734 | static cycle_t logarithmic_accumulation(cycle_t offset, int shift) | 
|  | 735 | { | 
|  | 736 | u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift; | 
|  | 737 |  | 
|  | 738 | /* If the offset is smaller then a shifted interval, do nothing */ | 
|  | 739 | if (offset < timekeeper.cycle_interval<<shift) | 
|  | 740 | return offset; | 
|  | 741 |  | 
|  | 742 | /* Accumulate one shifted interval */ | 
|  | 743 | offset -= timekeeper.cycle_interval << shift; | 
|  | 744 | timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift; | 
|  | 745 |  | 
|  | 746 | timekeeper.xtime_nsec += timekeeper.xtime_interval << shift; | 
|  | 747 | while (timekeeper.xtime_nsec >= nsecps) { | 
|  | 748 | timekeeper.xtime_nsec -= nsecps; | 
|  | 749 | xtime.tv_sec++; | 
|  | 750 | second_overflow(); | 
|  | 751 | } | 
|  | 752 |  | 
|  | 753 | /* Accumulate into raw time */ | 
|  | 754 | raw_time.tv_nsec += timekeeper.raw_interval << shift;; | 
|  | 755 | while (raw_time.tv_nsec >= NSEC_PER_SEC) { | 
|  | 756 | raw_time.tv_nsec -= NSEC_PER_SEC; | 
|  | 757 | raw_time.tv_sec++; | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | /* Accumulate error between NTP and clock interval */ | 
|  | 761 | timekeeper.ntp_error += tick_length << shift; | 
|  | 762 | timekeeper.ntp_error -= timekeeper.xtime_interval << | 
|  | 763 | (timekeeper.ntp_error_shift + shift); | 
|  | 764 |  | 
|  | 765 | return offset; | 
|  | 766 | } | 
|  | 767 |  | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 768 | /** | 
|  | 769 | * update_wall_time - Uses the current clocksource to increment the wall time | 
|  | 770 | * | 
|  | 771 | * Called from the timer interrupt, must hold a write on xtime_lock. | 
|  | 772 | */ | 
|  | 773 | void update_wall_time(void) | 
|  | 774 | { | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 775 | struct clocksource *clock; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 776 | cycle_t offset; | 
| john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 777 | int shift = 0, maxshift; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 778 |  | 
|  | 779 | /* Make sure we're fully resumed: */ | 
|  | 780 | if (unlikely(timekeeping_suspended)) | 
|  | 781 | return; | 
|  | 782 |  | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 783 | clock = timekeeper.clock; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 784 | #ifdef CONFIG_GENERIC_TIME | 
| Martin Schwidefsky | a0f7d48 | 2009-08-14 15:47:19 +0200 | [diff] [blame] | 785 | offset = (clock->read(clock) - clock->cycle_last) & clock->mask; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 786 | #else | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 787 | offset = timekeeper.cycle_interval; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 788 | #endif | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 789 | timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 790 |  | 
| john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 791 | /* | 
|  | 792 | * With NO_HZ we may have to accumulate many cycle_intervals | 
|  | 793 | * (think "ticks") worth of time at once. To do this efficiently, | 
|  | 794 | * we calculate the largest doubling multiple of cycle_intervals | 
|  | 795 | * that is smaller then the offset. We then accumulate that | 
|  | 796 | * chunk in one go, and then try to consume the next smaller | 
|  | 797 | * doubled multiple. | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 798 | */ | 
| john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 799 | shift = ilog2(offset) - ilog2(timekeeper.cycle_interval); | 
|  | 800 | shift = max(0, shift); | 
|  | 801 | /* Bound shift to one less then what overflows tick_length */ | 
|  | 802 | maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1; | 
|  | 803 | shift = min(shift, maxshift); | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 804 | while (offset >= timekeeper.cycle_interval) { | 
| john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 805 | offset = logarithmic_accumulation(offset, shift); | 
|  | 806 | shift--; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 807 | } | 
|  | 808 |  | 
|  | 809 | /* correct the clock when NTP error is too big */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 810 | timekeeping_adjust(offset); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 811 |  | 
| john stultz | 6c9bacb | 2008-12-01 18:34:41 -0800 | [diff] [blame] | 812 | /* | 
|  | 813 | * Since in the loop above, we accumulate any amount of time | 
|  | 814 | * in xtime_nsec over a second into xtime.tv_sec, its possible for | 
|  | 815 | * xtime_nsec to be fairly small after the loop. Further, if we're | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 816 | * slightly speeding the clocksource up in timekeeping_adjust(), | 
| john stultz | 6c9bacb | 2008-12-01 18:34:41 -0800 | [diff] [blame] | 817 | * its possible the required corrective factor to xtime_nsec could | 
|  | 818 | * cause it to underflow. | 
|  | 819 | * | 
|  | 820 | * Now, we cannot simply roll the accumulated second back, since | 
|  | 821 | * the NTP subsystem has been notified via second_overflow. So | 
|  | 822 | * instead we push xtime_nsec forward by the amount we underflowed, | 
|  | 823 | * and add that amount into the error. | 
|  | 824 | * | 
|  | 825 | * We'll correct this error next time through this function, when | 
|  | 826 | * xtime_nsec is not as small. | 
|  | 827 | */ | 
| Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 828 | if (unlikely((s64)timekeeper.xtime_nsec < 0)) { | 
|  | 829 | s64 neg = -(s64)timekeeper.xtime_nsec; | 
|  | 830 | timekeeper.xtime_nsec = 0; | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 831 | timekeeper.ntp_error += neg << timekeeper.ntp_error_shift; | 
| john stultz | 6c9bacb | 2008-12-01 18:34:41 -0800 | [diff] [blame] | 832 | } | 
|  | 833 |  | 
| Roman Zippel | 5cd1c9c | 2008-09-22 14:42:43 -0700 | [diff] [blame] | 834 | /* store full nanoseconds into xtime after rounding it up and | 
|  | 835 | * add the remainder to the error difference. | 
|  | 836 | */ | 
| Martin Schwidefsky | 23ce721 | 2009-08-14 15:47:27 +0200 | [diff] [blame] | 837 | xtime.tv_nsec =	((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1; | 
|  | 838 | timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift; | 
|  | 839 | timekeeper.ntp_error +=	timekeeper.xtime_nsec << | 
|  | 840 | timekeeper.ntp_error_shift; | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 841 |  | 
|  | 842 | /* check to see if there is a new clocksource to use */ | 
| Lin Ming | 0696b71 | 2009-11-17 13:49:50 +0800 | [diff] [blame] | 843 | update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult); | 
| john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 844 | } | 
| Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 845 |  | 
|  | 846 | /** | 
|  | 847 | * getboottime - Return the real time of system boot. | 
|  | 848 | * @ts:		pointer to the timespec to be set | 
|  | 849 | * | 
|  | 850 | * Returns the time of day in a timespec. | 
|  | 851 | * | 
|  | 852 | * This is based on the wall_to_monotonic offset and the total suspend | 
|  | 853 | * time. Calls to settimeofday will affect the value returned (which | 
|  | 854 | * basically means that however wrong your real time clock is at boot time, | 
|  | 855 | * you get the right time here). | 
|  | 856 | */ | 
|  | 857 | void getboottime(struct timespec *ts) | 
|  | 858 | { | 
| Hiroshi Shimamoto | 36d4748 | 2009-08-25 15:08:30 +0900 | [diff] [blame] | 859 | struct timespec boottime = { | 
|  | 860 | .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec, | 
|  | 861 | .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec | 
|  | 862 | }; | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 863 |  | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 864 | set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); | 
| Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 865 | } | 
|  | 866 |  | 
|  | 867 | /** | 
|  | 868 | * monotonic_to_bootbased - Convert the monotonic time to boot based. | 
|  | 869 | * @ts:		pointer to the timespec to be converted | 
|  | 870 | */ | 
|  | 871 | void monotonic_to_bootbased(struct timespec *ts) | 
|  | 872 | { | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 873 | *ts = timespec_add_safe(*ts, total_sleep_time); | 
| Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 874 | } | 
| john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 875 |  | 
| john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 876 | unsigned long get_seconds(void) | 
|  | 877 | { | 
| john stultz | 7bc7d63 | 2009-10-02 16:24:15 -0700 | [diff] [blame] | 878 | return xtime.tv_sec; | 
| john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 879 | } | 
|  | 880 | EXPORT_SYMBOL(get_seconds); | 
|  | 881 |  | 
| john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 882 | struct timespec __current_kernel_time(void) | 
|  | 883 | { | 
| john stultz | 7bc7d63 | 2009-10-02 16:24:15 -0700 | [diff] [blame] | 884 | return xtime; | 
| john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 885 | } | 
| john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 886 |  | 
| john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 887 | struct timespec current_kernel_time(void) | 
|  | 888 | { | 
|  | 889 | struct timespec now; | 
|  | 890 | unsigned long seq; | 
|  | 891 |  | 
|  | 892 | do { | 
|  | 893 | seq = read_seqbegin(&xtime_lock); | 
| john stultz | 7bc7d63 | 2009-10-02 16:24:15 -0700 | [diff] [blame] | 894 | now = xtime; | 
| john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 895 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 896 |  | 
|  | 897 | return now; | 
|  | 898 | } | 
| john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 899 | EXPORT_SYMBOL(current_kernel_time); | 
| john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 900 |  | 
|  | 901 | struct timespec get_monotonic_coarse(void) | 
|  | 902 | { | 
|  | 903 | struct timespec now, mono; | 
|  | 904 | unsigned long seq; | 
|  | 905 |  | 
|  | 906 | do { | 
|  | 907 | seq = read_seqbegin(&xtime_lock); | 
| john stultz | 7bc7d63 | 2009-10-02 16:24:15 -0700 | [diff] [blame] | 908 | now = xtime; | 
| john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 909 | mono = wall_to_monotonic; | 
|  | 910 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 911 |  | 
|  | 912 | set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, | 
|  | 913 | now.tv_nsec + mono.tv_nsec); | 
|  | 914 | return now; | 
|  | 915 | } |