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> |
| 16 | #include <linux/sysdev.h> |
| 17 | #include <linux/clocksource.h> |
| 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/time.h> |
| 20 | #include <linux/tick.h> |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * This read-write spinlock protects us from races in SMP while |
| 25 | * playing with xtime and avenrun. |
| 26 | */ |
Adrian Bunk | ba2a631 | 2007-10-16 23:27:16 -0700 | [diff] [blame] | 27 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | /* |
| 31 | * The current time |
| 32 | * wall_to_monotonic is what we need to add to xtime (or xtime corrected |
| 33 | * for sub jiffie times) to get to monotonic time. Monotonic is pegged |
| 34 | * at zero at system boot time, so wall_to_monotonic will be negative, |
| 35 | * however, we will ALWAYS keep the tv_nsec part positive so we can use |
| 36 | * the usual normalization. |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 37 | * |
| 38 | * wall_to_monotonic is moved after resume from suspend for the monotonic |
| 39 | * time not to jump. We need to add total_sleep_time to wall_to_monotonic |
| 40 | * to get the real boot based time offset. |
| 41 | * |
| 42 | * - wall_to_monotonic is no longer the boot time, getboottime must be |
| 43 | * used instead. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 44 | */ |
| 45 | struct timespec xtime __attribute__ ((aligned (16))); |
| 46 | struct timespec wall_to_monotonic __attribute__ ((aligned (16))); |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 47 | static unsigned long total_sleep_time; /* seconds */ |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 48 | |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 49 | static struct timespec xtime_cache __attribute__ ((aligned (16))); |
Thomas Gleixner | 1001d0a | 2008-02-01 17:45:13 +0100 | [diff] [blame] | 50 | void update_xtime_cache(u64 nsec) |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 51 | { |
| 52 | xtime_cache = xtime; |
| 53 | timespec_add_ns(&xtime_cache, nsec); |
| 54 | } |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 55 | |
Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 56 | struct clocksource *clock; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | #ifdef CONFIG_GENERIC_TIME |
| 60 | /** |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 61 | * clocksource_forward_now - update clock to the current time |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 62 | * |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 63 | * Forward the current clock to update its state since the last call to |
| 64 | * update_wall_time(). This is useful before significant clock changes, |
| 65 | * as it avoids having to deal with this time offset explicitly. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 66 | */ |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 67 | static void clocksource_forward_now(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 68 | { |
| 69 | cycle_t cycle_now, cycle_delta; |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 70 | s64 nsec; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 71 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 72 | cycle_now = clocksource_read(clock); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 73 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 74 | clock->cycle_last = cycle_now; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 75 | |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 76 | nsec = cyc2ns(clock, cycle_delta); |
| 77 | timespec_add_ns(&xtime, nsec); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 78 | |
| 79 | nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift; |
| 80 | clock->raw_time.tv_nsec += nsec; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 84 | * getnstimeofday - Returns the time of day in a timespec |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 85 | * @ts: pointer to the timespec to be set |
| 86 | * |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 87 | * Returns the time of day in a timespec. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 88 | */ |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 89 | void getnstimeofday(struct timespec *ts) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 90 | { |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 91 | cycle_t cycle_now, cycle_delta; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 92 | unsigned long seq; |
| 93 | s64 nsecs; |
| 94 | |
| 95 | do { |
| 96 | seq = read_seqbegin(&xtime_lock); |
| 97 | |
| 98 | *ts = xtime; |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 99 | |
| 100 | /* read clocksource: */ |
| 101 | cycle_now = clocksource_read(clock); |
| 102 | |
| 103 | /* calculate the delta since the last update_wall_time: */ |
| 104 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
| 105 | |
| 106 | /* convert to nanoseconds: */ |
| 107 | nsecs = cyc2ns(clock, cycle_delta); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 108 | |
| 109 | } while (read_seqretry(&xtime_lock, seq)); |
| 110 | |
| 111 | timespec_add_ns(ts, nsecs); |
| 112 | } |
| 113 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 114 | EXPORT_SYMBOL(getnstimeofday); |
| 115 | |
| 116 | /** |
| 117 | * do_gettimeofday - Returns the time of day in a timeval |
| 118 | * @tv: pointer to the timeval to be set |
| 119 | * |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 120 | * NOTE: Users should be converted to using getnstimeofday() |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 121 | */ |
| 122 | void do_gettimeofday(struct timeval *tv) |
| 123 | { |
| 124 | struct timespec now; |
| 125 | |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 126 | getnstimeofday(&now); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 127 | tv->tv_sec = now.tv_sec; |
| 128 | tv->tv_usec = now.tv_nsec/1000; |
| 129 | } |
| 130 | |
| 131 | EXPORT_SYMBOL(do_gettimeofday); |
| 132 | /** |
| 133 | * do_settimeofday - Sets the time of day |
| 134 | * @tv: pointer to the timespec variable containing the new time |
| 135 | * |
| 136 | * Sets the time of day to the new time and update NTP and notify hrtimers |
| 137 | */ |
| 138 | int do_settimeofday(struct timespec *tv) |
| 139 | { |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 140 | struct timespec ts_delta; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 141 | unsigned long flags; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 142 | |
| 143 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | write_seqlock_irqsave(&xtime_lock, flags); |
| 147 | |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 148 | clocksource_forward_now(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 149 | |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 150 | ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec; |
| 151 | ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec; |
| 152 | wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 153 | |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 154 | xtime = *tv; |
| 155 | |
Thomas Gleixner | 1001d0a | 2008-02-01 17:45:13 +0100 | [diff] [blame] | 156 | update_xtime_cache(0); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 157 | |
| 158 | clock->error = 0; |
| 159 | ntp_clear(); |
| 160 | |
| 161 | update_vsyscall(&xtime, clock); |
| 162 | |
| 163 | write_sequnlock_irqrestore(&xtime_lock, flags); |
| 164 | |
| 165 | /* signal hrtimers about time change */ |
| 166 | clock_was_set(); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | EXPORT_SYMBOL(do_settimeofday); |
| 172 | |
| 173 | /** |
| 174 | * change_clocksource - Swaps clocksources if a new one is available |
| 175 | * |
| 176 | * Accumulates current time interval and initializes new clocksource |
| 177 | */ |
| 178 | static void change_clocksource(void) |
| 179 | { |
| 180 | struct clocksource *new; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 181 | |
| 182 | new = clocksource_get_next(); |
| 183 | |
| 184 | if (clock == new) |
| 185 | return; |
| 186 | |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 187 | clocksource_forward_now(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 188 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 189 | new->raw_time = clock->raw_time; |
| 190 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 191 | clock = new; |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 192 | clock->cycle_last = 0; |
| 193 | clock->cycle_last = clocksource_read(new); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 194 | clock->error = 0; |
| 195 | clock->xtime_nsec = 0; |
Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 196 | clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 197 | |
| 198 | tick_clock_notify(); |
| 199 | |
Linus Torvalds | 92896bd | 2008-03-24 11:07:15 -0700 | [diff] [blame] | 200 | /* |
| 201 | * We're holding xtime lock and waking up klogd would deadlock |
| 202 | * us on enqueue. So no printing! |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 203 | printk(KERN_INFO "Time: %s clocksource has been installed.\n", |
| 204 | clock->name); |
Linus Torvalds | 92896bd | 2008-03-24 11:07:15 -0700 | [diff] [blame] | 205 | */ |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 206 | } |
| 207 | #else |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 208 | static inline void clocksource_forward_now(void) { } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 209 | static inline void change_clocksource(void) { } |
| 210 | #endif |
| 211 | |
| 212 | /** |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 213 | * getrawmonotonic - Returns the raw monotonic time in a timespec |
| 214 | * @ts: pointer to the timespec to be set |
| 215 | * |
| 216 | * Returns the raw monotonic time (completely un-modified by ntp) |
| 217 | */ |
| 218 | void getrawmonotonic(struct timespec *ts) |
| 219 | { |
| 220 | unsigned long seq; |
| 221 | s64 nsecs; |
| 222 | cycle_t cycle_now, cycle_delta; |
| 223 | |
| 224 | do { |
| 225 | seq = read_seqbegin(&xtime_lock); |
| 226 | |
| 227 | /* read clocksource: */ |
| 228 | cycle_now = clocksource_read(clock); |
| 229 | |
| 230 | /* calculate the delta since the last update_wall_time: */ |
| 231 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
| 232 | |
| 233 | /* convert to nanoseconds: */ |
| 234 | nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift; |
| 235 | |
| 236 | *ts = clock->raw_time; |
| 237 | |
| 238 | } while (read_seqretry(&xtime_lock, seq)); |
| 239 | |
| 240 | timespec_add_ns(ts, nsecs); |
| 241 | } |
| 242 | EXPORT_SYMBOL(getrawmonotonic); |
| 243 | |
| 244 | |
| 245 | /** |
Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 246 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 247 | */ |
Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 248 | int timekeeping_valid_for_hres(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 249 | { |
| 250 | unsigned long seq; |
| 251 | int ret; |
| 252 | |
| 253 | do { |
| 254 | seq = read_seqbegin(&xtime_lock); |
| 255 | |
| 256 | ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
| 257 | |
| 258 | } while (read_seqretry(&xtime_lock, seq)); |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * read_persistent_clock - Return time in seconds from the persistent clock. |
| 265 | * |
| 266 | * Weak dummy function for arches that do not yet support it. |
| 267 | * Returns seconds from epoch using the battery backed persistent clock. |
| 268 | * Returns zero if unsupported. |
| 269 | * |
| 270 | * XXX - Do be sure to remove it once all arches implement it. |
| 271 | */ |
| 272 | unsigned long __attribute__((weak)) read_persistent_clock(void) |
| 273 | { |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * timekeeping_init - Initializes the clocksource and common timekeeping values |
| 279 | */ |
| 280 | void __init timekeeping_init(void) |
| 281 | { |
| 282 | unsigned long flags; |
| 283 | unsigned long sec = read_persistent_clock(); |
| 284 | |
| 285 | write_seqlock_irqsave(&xtime_lock, flags); |
| 286 | |
Roman Zippel | 7dffa3c | 2008-05-01 04:34:41 -0700 | [diff] [blame] | 287 | ntp_init(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 288 | |
| 289 | clock = clocksource_get_next(); |
Roman Zippel | 10a398d | 2008-03-04 15:14:26 -0800 | [diff] [blame] | 290 | clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 291 | clock->cycle_last = clocksource_read(clock); |
| 292 | |
| 293 | xtime.tv_sec = sec; |
| 294 | xtime.tv_nsec = 0; |
| 295 | set_normalized_timespec(&wall_to_monotonic, |
| 296 | -xtime.tv_sec, -xtime.tv_nsec); |
Thomas Gleixner | 1001d0a | 2008-02-01 17:45:13 +0100 | [diff] [blame] | 297 | update_xtime_cache(0); |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 298 | total_sleep_time = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 299 | write_sequnlock_irqrestore(&xtime_lock, flags); |
| 300 | } |
| 301 | |
| 302 | /* flag for if timekeeping is suspended */ |
| 303 | static int timekeeping_suspended; |
| 304 | /* time in seconds when suspend began */ |
| 305 | static unsigned long timekeeping_suspend_time; |
| 306 | |
| 307 | /** |
| 308 | * timekeeping_resume - Resumes the generic timekeeping subsystem. |
| 309 | * @dev: unused |
| 310 | * |
| 311 | * This is for the generic clocksource timekeeping. |
| 312 | * xtime/wall_to_monotonic/jiffies/etc are |
| 313 | * still managed by arch specific suspend/resume code. |
| 314 | */ |
| 315 | static int timekeeping_resume(struct sys_device *dev) |
| 316 | { |
| 317 | unsigned long flags; |
| 318 | unsigned long now = read_persistent_clock(); |
| 319 | |
Thomas Gleixner | d10ff3f | 2007-05-14 11:10:02 +0200 | [diff] [blame] | 320 | clocksource_resume(); |
| 321 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 322 | write_seqlock_irqsave(&xtime_lock, flags); |
| 323 | |
| 324 | if (now && (now > timekeeping_suspend_time)) { |
| 325 | unsigned long sleep_length = now - timekeeping_suspend_time; |
| 326 | |
| 327 | xtime.tv_sec += sleep_length; |
| 328 | wall_to_monotonic.tv_sec -= sleep_length; |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 329 | total_sleep_time += sleep_length; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 330 | } |
Thomas Gleixner | 1001d0a | 2008-02-01 17:45:13 +0100 | [diff] [blame] | 331 | update_xtime_cache(0); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 332 | /* re-base the last cycle value */ |
Thomas Gleixner | d8bb6f4 | 2008-04-01 19:45:18 +0200 | [diff] [blame] | 333 | clock->cycle_last = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 334 | clock->cycle_last = clocksource_read(clock); |
| 335 | clock->error = 0; |
| 336 | timekeeping_suspended = 0; |
| 337 | write_sequnlock_irqrestore(&xtime_lock, flags); |
| 338 | |
| 339 | touch_softlockup_watchdog(); |
| 340 | |
| 341 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); |
| 342 | |
| 343 | /* Resume hrtimers */ |
| 344 | hres_timers_resume(); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) |
| 350 | { |
| 351 | unsigned long flags; |
| 352 | |
Thomas Gleixner | 3be9095 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 353 | timekeeping_suspend_time = read_persistent_clock(); |
| 354 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 355 | write_seqlock_irqsave(&xtime_lock, flags); |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 356 | clocksource_forward_now(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 357 | timekeeping_suspended = 1; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 358 | write_sequnlock_irqrestore(&xtime_lock, flags); |
| 359 | |
| 360 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /* sysfs resume/suspend bits for timekeeping */ |
| 366 | static struct sysdev_class timekeeping_sysclass = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 367 | .name = "timekeeping", |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 368 | .resume = timekeeping_resume, |
| 369 | .suspend = timekeeping_suspend, |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | static struct sys_device device_timer = { |
| 373 | .id = 0, |
| 374 | .cls = &timekeeping_sysclass, |
| 375 | }; |
| 376 | |
| 377 | static int __init timekeeping_init_device(void) |
| 378 | { |
| 379 | int error = sysdev_class_register(&timekeeping_sysclass); |
| 380 | if (!error) |
| 381 | error = sysdev_register(&device_timer); |
| 382 | return error; |
| 383 | } |
| 384 | |
| 385 | device_initcall(timekeeping_init_device); |
| 386 | |
| 387 | /* |
| 388 | * If the error is already larger, we look ahead even further |
| 389 | * to compensate for late or lost adjustments. |
| 390 | */ |
| 391 | static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, |
| 392 | s64 *offset) |
| 393 | { |
| 394 | s64 tick_error, i; |
| 395 | u32 look_ahead, adj; |
| 396 | s32 error2, mult; |
| 397 | |
| 398 | /* |
| 399 | * Use the current error value to determine how much to look ahead. |
| 400 | * The larger the error the slower we adjust for it to avoid problems |
| 401 | * with losing too many ticks, otherwise we would overadjust and |
| 402 | * produce an even larger error. The smaller the adjustment the |
| 403 | * 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] | 404 | * 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] | 405 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). |
| 406 | */ |
Roman Zippel | 7fc5c78 | 2008-05-01 04:34:38 -0700 | [diff] [blame] | 407 | error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 408 | error2 = abs(error2); |
| 409 | for (look_ahead = 0; error2 > 0; look_ahead++) |
| 410 | error2 >>= 2; |
| 411 | |
| 412 | /* |
| 413 | * Now calculate the error in (1 << look_ahead) ticks, but first |
| 414 | * remove the single look ahead already included in the error. |
| 415 | */ |
Roman Zippel | 8383c42 | 2008-05-01 04:34:39 -0700 | [diff] [blame] | 416 | tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 417 | tick_error -= clock->xtime_interval >> 1; |
| 418 | error = ((error - tick_error) >> look_ahead) + tick_error; |
| 419 | |
| 420 | /* Finally calculate the adjustment shift value. */ |
| 421 | i = *interval; |
| 422 | mult = 1; |
| 423 | if (error < 0) { |
| 424 | error = -error; |
| 425 | *interval = -*interval; |
| 426 | *offset = -*offset; |
| 427 | mult = -1; |
| 428 | } |
| 429 | for (adj = 0; error > i; adj++) |
| 430 | error >>= 1; |
| 431 | |
| 432 | *interval <<= adj; |
| 433 | *offset <<= adj; |
| 434 | return mult << adj; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Adjust the multiplier to reduce the error value, |
| 439 | * this is optimized for the most common adjustments of -1,0,1, |
| 440 | * for other values we can do a bit more work. |
| 441 | */ |
Thomas Gleixner | 71120f1 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 442 | static void clocksource_adjust(s64 offset) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 443 | { |
| 444 | s64 error, interval = clock->cycle_interval; |
| 445 | int adj; |
| 446 | |
Roman Zippel | 7fc5c78 | 2008-05-01 04:34:38 -0700 | [diff] [blame] | 447 | error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 448 | if (error > interval) { |
| 449 | error >>= 2; |
| 450 | if (likely(error <= interval)) |
| 451 | adj = 1; |
| 452 | else |
| 453 | adj = clocksource_bigadjust(error, &interval, &offset); |
| 454 | } else if (error < -interval) { |
| 455 | error >>= 2; |
| 456 | if (likely(error >= -interval)) { |
| 457 | adj = -1; |
| 458 | interval = -interval; |
| 459 | offset = -offset; |
| 460 | } else |
| 461 | adj = clocksource_bigadjust(error, &interval, &offset); |
| 462 | } else |
| 463 | return; |
| 464 | |
| 465 | clock->mult += adj; |
| 466 | clock->xtime_interval += interval; |
| 467 | clock->xtime_nsec -= offset; |
| 468 | clock->error -= (interval - offset) << |
Roman Zippel | 7fc5c78 | 2008-05-01 04:34:38 -0700 | [diff] [blame] | 469 | (NTP_SCALE_SHIFT - clock->shift); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
| 473 | * update_wall_time - Uses the current clocksource to increment the wall time |
| 474 | * |
| 475 | * Called from the timer interrupt, must hold a write on xtime_lock. |
| 476 | */ |
| 477 | void update_wall_time(void) |
| 478 | { |
| 479 | cycle_t offset; |
| 480 | |
| 481 | /* Make sure we're fully resumed: */ |
| 482 | if (unlikely(timekeeping_suspended)) |
| 483 | return; |
| 484 | |
| 485 | #ifdef CONFIG_GENERIC_TIME |
| 486 | offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask; |
| 487 | #else |
| 488 | offset = clock->cycle_interval; |
| 489 | #endif |
Roman Zippel | 5cd1c9c | 2008-09-22 14:42:43 -0700 | [diff] [blame] | 490 | clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 491 | |
| 492 | /* normally this loop will run just once, however in the |
| 493 | * case of lost or late ticks, it will accumulate correctly. |
| 494 | */ |
| 495 | while (offset >= clock->cycle_interval) { |
| 496 | /* accumulate one interval */ |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 497 | offset -= clock->cycle_interval; |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 498 | clock->cycle_last += clock->cycle_interval; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 499 | |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 500 | clock->xtime_nsec += clock->xtime_interval; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 501 | if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) { |
| 502 | clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift; |
| 503 | xtime.tv_sec++; |
| 504 | second_overflow(); |
| 505 | } |
| 506 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 507 | clock->raw_time.tv_nsec += clock->raw_interval; |
| 508 | if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) { |
| 509 | clock->raw_time.tv_nsec -= NSEC_PER_SEC; |
| 510 | clock->raw_time.tv_sec++; |
| 511 | } |
| 512 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 513 | /* accumulate error between NTP and clock interval */ |
Roman Zippel | 8383c42 | 2008-05-01 04:34:39 -0700 | [diff] [blame] | 514 | clock->error += tick_length; |
Roman Zippel | 7fc5c78 | 2008-05-01 04:34:38 -0700 | [diff] [blame] | 515 | clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* correct the clock when NTP error is too big */ |
Thomas Gleixner | 71120f1 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 519 | clocksource_adjust(offset); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 520 | |
john stultz | 6c9bacb | 2008-12-01 18:34:41 -0800 | [diff] [blame] | 521 | /* |
| 522 | * Since in the loop above, we accumulate any amount of time |
| 523 | * in xtime_nsec over a second into xtime.tv_sec, its possible for |
| 524 | * xtime_nsec to be fairly small after the loop. Further, if we're |
| 525 | * slightly speeding the clocksource up in clocksource_adjust(), |
| 526 | * its possible the required corrective factor to xtime_nsec could |
| 527 | * cause it to underflow. |
| 528 | * |
| 529 | * Now, we cannot simply roll the accumulated second back, since |
| 530 | * the NTP subsystem has been notified via second_overflow. So |
| 531 | * instead we push xtime_nsec forward by the amount we underflowed, |
| 532 | * and add that amount into the error. |
| 533 | * |
| 534 | * We'll correct this error next time through this function, when |
| 535 | * xtime_nsec is not as small. |
| 536 | */ |
| 537 | if (unlikely((s64)clock->xtime_nsec < 0)) { |
| 538 | s64 neg = -(s64)clock->xtime_nsec; |
| 539 | clock->xtime_nsec = 0; |
| 540 | clock->error += neg << (NTP_SCALE_SHIFT - clock->shift); |
| 541 | } |
| 542 | |
Roman Zippel | 5cd1c9c | 2008-09-22 14:42:43 -0700 | [diff] [blame] | 543 | /* store full nanoseconds into xtime after rounding it up and |
| 544 | * add the remainder to the error difference. |
| 545 | */ |
| 546 | xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 547 | clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift; |
Roman Zippel | 5cd1c9c | 2008-09-22 14:42:43 -0700 | [diff] [blame] | 548 | clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 549 | |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 550 | update_xtime_cache(cyc2ns(clock, offset)); |
| 551 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 552 | /* check to see if there is a new clocksource to use */ |
| 553 | change_clocksource(); |
| 554 | update_vsyscall(&xtime, clock); |
| 555 | } |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 556 | |
| 557 | /** |
| 558 | * getboottime - Return the real time of system boot. |
| 559 | * @ts: pointer to the timespec to be set |
| 560 | * |
| 561 | * Returns the time of day in a timespec. |
| 562 | * |
| 563 | * This is based on the wall_to_monotonic offset and the total suspend |
| 564 | * time. Calls to settimeofday will affect the value returned (which |
| 565 | * basically means that however wrong your real time clock is at boot time, |
| 566 | * you get the right time here). |
| 567 | */ |
| 568 | void getboottime(struct timespec *ts) |
| 569 | { |
| 570 | set_normalized_timespec(ts, |
| 571 | - (wall_to_monotonic.tv_sec + total_sleep_time), |
| 572 | - wall_to_monotonic.tv_nsec); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * monotonic_to_bootbased - Convert the monotonic time to boot based. |
| 577 | * @ts: pointer to the timespec to be converted |
| 578 | */ |
| 579 | void monotonic_to_bootbased(struct timespec *ts) |
| 580 | { |
| 581 | ts->tv_sec += total_sleep_time; |
| 582 | } |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 583 | |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 584 | unsigned long get_seconds(void) |
| 585 | { |
| 586 | return xtime_cache.tv_sec; |
| 587 | } |
| 588 | EXPORT_SYMBOL(get_seconds); |
| 589 | |
| 590 | |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 591 | struct timespec current_kernel_time(void) |
| 592 | { |
| 593 | struct timespec now; |
| 594 | unsigned long seq; |
| 595 | |
| 596 | do { |
| 597 | seq = read_seqbegin(&xtime_lock); |
| 598 | |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 599 | now = xtime_cache; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 600 | } while (read_seqretry(&xtime_lock, seq)); |
| 601 | |
| 602 | return now; |
| 603 | } |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 604 | EXPORT_SYMBOL(current_kernel_time); |