Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/hrtimer.c |
| 3 | * |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 4 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
| 5 | * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar |
| 6 | * Copyright(C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 7 | * |
| 8 | * High-resolution kernel timers |
| 9 | * |
| 10 | * In contrast to the low-resolution timeout API implemented in |
| 11 | * kernel/timer.c, hrtimers provide finer resolution and accuracy |
| 12 | * depending on system configuration and capabilities. |
| 13 | * |
| 14 | * These timers are currently used for: |
| 15 | * - itimers |
| 16 | * - POSIX timers |
| 17 | * - nanosleep |
| 18 | * - precise in-kernel timing |
| 19 | * |
| 20 | * Started by: Thomas Gleixner and Ingo Molnar |
| 21 | * |
| 22 | * Credits: |
| 23 | * based on kernel/timer.c |
| 24 | * |
Thomas Gleixner | 66188fa | 2006-02-01 03:05:13 -0800 | [diff] [blame] | 25 | * Help, testing, suggestions, bugfixes, improvements were |
| 26 | * provided by: |
| 27 | * |
| 28 | * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel |
| 29 | * et. al. |
| 30 | * |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 31 | * For licencing details see kernel-base/COPYING |
| 32 | */ |
| 33 | |
| 34 | #include <linux/cpu.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/percpu.h> |
| 37 | #include <linux/hrtimer.h> |
| 38 | #include <linux/notifier.h> |
| 39 | #include <linux/syscalls.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | |
| 42 | #include <asm/uaccess.h> |
| 43 | |
| 44 | /** |
| 45 | * ktime_get - get the monotonic time in ktime_t format |
| 46 | * |
| 47 | * returns the time in ktime_t format |
| 48 | */ |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame^] | 49 | ktime_t ktime_get(void) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 50 | { |
| 51 | struct timespec now; |
| 52 | |
| 53 | ktime_get_ts(&now); |
| 54 | |
| 55 | return timespec_to_ktime(now); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 60 | * |
| 61 | * returns the time in ktime_t format |
| 62 | */ |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame^] | 63 | ktime_t ktime_get_real(void) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 64 | { |
| 65 | struct timespec now; |
| 66 | |
| 67 | getnstimeofday(&now); |
| 68 | |
| 69 | return timespec_to_ktime(now); |
| 70 | } |
| 71 | |
| 72 | EXPORT_SYMBOL_GPL(ktime_get_real); |
| 73 | |
| 74 | /* |
| 75 | * The timer bases: |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 76 | * |
| 77 | * Note: If we want to add new timer bases, we have to skip the two |
| 78 | * clock ids captured by the cpu-timers. We do this by holding empty |
| 79 | * entries rather than doing math adjustment of the clock ids. |
| 80 | * This ensures that we capture erroneous accesses to these clock ids |
| 81 | * rather than moving them into the range of valid clock id's. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 82 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 83 | static DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 84 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 85 | |
| 86 | .clock_base = |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 87 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 88 | { |
| 89 | .index = CLOCK_REALTIME, |
| 90 | .get_time = &ktime_get_real, |
| 91 | .resolution = KTIME_REALTIME_RES, |
| 92 | }, |
| 93 | { |
| 94 | .index = CLOCK_MONOTONIC, |
| 95 | .get_time = &ktime_get, |
| 96 | .resolution = KTIME_MONOTONIC_RES, |
| 97 | }, |
| 98 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * ktime_get_ts - get the monotonic clock in timespec format |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 103 | * @ts: pointer to timespec variable |
| 104 | * |
| 105 | * The function calculates the monotonic clock from the realtime |
| 106 | * clock and the wall_to_monotonic offset and stores the result |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 107 | * in normalized timespec format in the variable pointed to by @ts. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 108 | */ |
| 109 | void ktime_get_ts(struct timespec *ts) |
| 110 | { |
| 111 | struct timespec tomono; |
| 112 | unsigned long seq; |
| 113 | |
| 114 | do { |
| 115 | seq = read_seqbegin(&xtime_lock); |
| 116 | getnstimeofday(ts); |
| 117 | tomono = wall_to_monotonic; |
| 118 | |
| 119 | } while (read_seqretry(&xtime_lock, seq)); |
| 120 | |
| 121 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, |
| 122 | ts->tv_nsec + tomono.tv_nsec); |
| 123 | } |
Matt Helsley | 69778e3 | 2006-01-09 20:52:39 -0800 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(ktime_get_ts); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 125 | |
| 126 | /* |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 127 | * Get the coarse grained time at the softirq based on xtime and |
| 128 | * wall_to_monotonic. |
| 129 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 130 | static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base) |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 131 | { |
| 132 | ktime_t xtim, tomono; |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 133 | struct timespec xts; |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 134 | unsigned long seq; |
| 135 | |
| 136 | do { |
| 137 | seq = read_seqbegin(&xtime_lock); |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 138 | #ifdef CONFIG_NO_HZ |
| 139 | getnstimeofday(&xts); |
| 140 | #else |
| 141 | xts = xtime; |
| 142 | #endif |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 143 | } while (read_seqretry(&xtime_lock, seq)); |
| 144 | |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 145 | xtim = timespec_to_ktime(xts); |
| 146 | tomono = timespec_to_ktime(wall_to_monotonic); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 147 | base->clock_base[CLOCK_REALTIME].softirq_time = xtim; |
| 148 | base->clock_base[CLOCK_MONOTONIC].softirq_time = |
| 149 | ktime_add(xtim, tomono); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 153 | * Helper function to check, whether the timer is on one of the queues |
| 154 | */ |
| 155 | static inline int hrtimer_is_queued(struct hrtimer *timer) |
| 156 | { |
| 157 | return timer->state & HRTIMER_STATE_ENQUEUED; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Helper function to check, whether the timer is running the callback |
| 162 | * function |
| 163 | */ |
| 164 | static inline int hrtimer_callback_running(struct hrtimer *timer) |
| 165 | { |
| 166 | return timer->state & HRTIMER_STATE_CALLBACK; |
| 167 | } |
| 168 | |
| 169 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 170 | * Functions and macros which are different for UP/SMP systems are kept in a |
| 171 | * single place |
| 172 | */ |
| 173 | #ifdef CONFIG_SMP |
| 174 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 175 | /* |
| 176 | * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock |
| 177 | * means that all timers which are tied to this base via timer->base are |
| 178 | * locked, and the base itself is locked too. |
| 179 | * |
| 180 | * So __run_timers/migrate_timers can safely modify all timers which could |
| 181 | * be found on the lists/queues. |
| 182 | * |
| 183 | * When the timer's base is locked, and the timer removed from list, it is |
| 184 | * possible to set timer->base = NULL and drop the lock: the timer remains |
| 185 | * locked. |
| 186 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 187 | static |
| 188 | struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, |
| 189 | unsigned long *flags) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 190 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 191 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 192 | |
| 193 | for (;;) { |
| 194 | base = timer->base; |
| 195 | if (likely(base != NULL)) { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 196 | spin_lock_irqsave(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 197 | if (likely(base == timer->base)) |
| 198 | return base; |
| 199 | /* The timer has migrated to another CPU: */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 200 | spin_unlock_irqrestore(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 201 | } |
| 202 | cpu_relax(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Switch the timer base to the current CPU when possible. |
| 208 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 209 | static inline struct hrtimer_clock_base * |
| 210 | switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 211 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 212 | struct hrtimer_clock_base *new_base; |
| 213 | struct hrtimer_cpu_base *new_cpu_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 214 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 215 | new_cpu_base = &__get_cpu_var(hrtimer_bases); |
| 216 | new_base = &new_cpu_base->clock_base[base->index]; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 217 | |
| 218 | if (base != new_base) { |
| 219 | /* |
| 220 | * We are trying to schedule the timer on the local CPU. |
| 221 | * However we can't change timer's base while it is running, |
| 222 | * so we keep it on the same CPU. No hassle vs. reprogramming |
| 223 | * the event source in the high resolution case. The softirq |
| 224 | * code will take care of this when the timer function has |
| 225 | * completed. There is no conflict as we hold the lock until |
| 226 | * the timer is enqueued. |
| 227 | */ |
Thomas Gleixner | 5cfb6de | 2007-02-16 01:27:52 -0800 | [diff] [blame] | 228 | if (unlikely(timer->state & HRTIMER_STATE_CALLBACK)) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 229 | return base; |
| 230 | |
| 231 | /* See the comment in lock_timer_base() */ |
| 232 | timer->base = NULL; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 233 | spin_unlock(&base->cpu_base->lock); |
| 234 | spin_lock(&new_base->cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 235 | timer->base = new_base; |
| 236 | } |
| 237 | return new_base; |
| 238 | } |
| 239 | |
| 240 | #else /* CONFIG_SMP */ |
| 241 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 242 | static inline struct hrtimer_clock_base * |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 243 | lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 244 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 245 | struct hrtimer_clock_base *base = timer->base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 246 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 247 | spin_lock_irqsave(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 248 | |
| 249 | return base; |
| 250 | } |
| 251 | |
| 252 | #define switch_hrtimer_base(t, b) (b) |
| 253 | |
| 254 | #endif /* !CONFIG_SMP */ |
| 255 | |
| 256 | /* |
| 257 | * Functions for the union type storage format of ktime_t which are |
| 258 | * too large for inlining: |
| 259 | */ |
| 260 | #if BITS_PER_LONG < 64 |
| 261 | # ifndef CONFIG_KTIME_SCALAR |
| 262 | /** |
| 263 | * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 264 | * @kt: addend |
| 265 | * @nsec: the scalar nsec value to add |
| 266 | * |
| 267 | * Returns the sum of kt and nsec in ktime_t format |
| 268 | */ |
| 269 | ktime_t ktime_add_ns(const ktime_t kt, u64 nsec) |
| 270 | { |
| 271 | ktime_t tmp; |
| 272 | |
| 273 | if (likely(nsec < NSEC_PER_SEC)) { |
| 274 | tmp.tv64 = nsec; |
| 275 | } else { |
| 276 | unsigned long rem = do_div(nsec, NSEC_PER_SEC); |
| 277 | |
| 278 | tmp = ktime_set((long)nsec, rem); |
| 279 | } |
| 280 | |
| 281 | return ktime_add(kt, tmp); |
| 282 | } |
| 283 | |
| 284 | #else /* CONFIG_KTIME_SCALAR */ |
| 285 | |
| 286 | # endif /* !CONFIG_KTIME_SCALAR */ |
| 287 | |
| 288 | /* |
| 289 | * Divide a ktime value by a nanosecond value |
| 290 | */ |
Roman Zippel | df869b6 | 2006-03-26 01:38:11 -0800 | [diff] [blame] | 291 | static unsigned long ktime_divns(const ktime_t kt, s64 div) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 292 | { |
| 293 | u64 dclc, inc, dns; |
| 294 | int sft = 0; |
| 295 | |
| 296 | dclc = dns = ktime_to_ns(kt); |
| 297 | inc = div; |
| 298 | /* Make sure the divisor is less than 2^32: */ |
| 299 | while (div >> 32) { |
| 300 | sft++; |
| 301 | div >>= 1; |
| 302 | } |
| 303 | dclc >>= sft; |
| 304 | do_div(dclc, (unsigned long) div); |
| 305 | |
| 306 | return (unsigned long) dclc; |
| 307 | } |
| 308 | |
| 309 | #else /* BITS_PER_LONG < 64 */ |
| 310 | # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div)) |
| 311 | #endif /* BITS_PER_LONG >= 64 */ |
| 312 | |
| 313 | /* |
| 314 | * Counterpart to lock_timer_base above: |
| 315 | */ |
| 316 | static inline |
| 317 | void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 318 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 319 | spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * hrtimer_forward - forward the timer expiry |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 324 | * @timer: hrtimer to forward |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 325 | * @now: forward past this time |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 326 | * @interval: the interval to forward |
| 327 | * |
| 328 | * Forward the timer expiry so it will expire in the future. |
Jonathan Corbet | 8dca6f3 | 2006-01-16 15:58:55 -0700 | [diff] [blame] | 329 | * Returns the number of overruns. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 330 | */ |
| 331 | unsigned long |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 332 | hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 333 | { |
| 334 | unsigned long orun = 1; |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 335 | ktime_t delta; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 336 | |
| 337 | delta = ktime_sub(now, timer->expires); |
| 338 | |
| 339 | if (delta.tv64 < 0) |
| 340 | return 0; |
| 341 | |
Thomas Gleixner | c9db4fa | 2006-01-12 11:47:34 +0100 | [diff] [blame] | 342 | if (interval.tv64 < timer->base->resolution.tv64) |
| 343 | interval.tv64 = timer->base->resolution.tv64; |
| 344 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 345 | if (unlikely(delta.tv64 >= interval.tv64)) { |
Roman Zippel | df869b6 | 2006-03-26 01:38:11 -0800 | [diff] [blame] | 346 | s64 incr = ktime_to_ns(interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 347 | |
| 348 | orun = ktime_divns(delta, incr); |
| 349 | timer->expires = ktime_add_ns(timer->expires, incr * orun); |
| 350 | if (timer->expires.tv64 > now.tv64) |
| 351 | return orun; |
| 352 | /* |
| 353 | * This (and the ktime_add() below) is the |
| 354 | * correction for exact: |
| 355 | */ |
| 356 | orun++; |
| 357 | } |
| 358 | timer->expires = ktime_add(timer->expires, interval); |
| 359 | |
| 360 | return orun; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * enqueue_hrtimer - internal function to (re)start a timer |
| 365 | * |
| 366 | * The timer is inserted in expiry order. Insertion into the |
| 367 | * red black tree is O(log(n)). Must hold the base lock. |
| 368 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 369 | static void enqueue_hrtimer(struct hrtimer *timer, |
| 370 | struct hrtimer_clock_base *base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 371 | { |
| 372 | struct rb_node **link = &base->active.rb_node; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 373 | struct rb_node *parent = NULL; |
| 374 | struct hrtimer *entry; |
| 375 | |
| 376 | /* |
| 377 | * Find the right place in the rbtree: |
| 378 | */ |
| 379 | while (*link) { |
| 380 | parent = *link; |
| 381 | entry = rb_entry(parent, struct hrtimer, node); |
| 382 | /* |
| 383 | * We dont care about collisions. Nodes with |
| 384 | * the same expiry time stay together. |
| 385 | */ |
| 386 | if (timer->expires.tv64 < entry->expires.tv64) |
| 387 | link = &(*link)->rb_left; |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 388 | else |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 389 | link = &(*link)->rb_right; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 393 | * Insert the timer to the rbtree and check whether it |
| 394 | * replaces the first pending timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 395 | */ |
| 396 | rb_link_node(&timer->node, parent, link); |
| 397 | rb_insert_color(&timer->node, &base->active); |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 398 | /* |
| 399 | * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the |
| 400 | * state of a possibly running callback. |
| 401 | */ |
| 402 | timer->state |= HRTIMER_STATE_ENQUEUED; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 403 | |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 404 | if (!base->first || timer->expires.tv64 < |
| 405 | rb_entry(base->first, struct hrtimer, node)->expires.tv64) |
| 406 | base->first = &timer->node; |
| 407 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * __remove_hrtimer - internal function to remove a timer |
| 411 | * |
| 412 | * Caller must hold the base lock. |
| 413 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 414 | static void __remove_hrtimer(struct hrtimer *timer, |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 415 | struct hrtimer_clock_base *base, |
| 416 | unsigned long newstate) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 417 | { |
| 418 | /* |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 419 | * Remove the timer from the rbtree and replace the |
| 420 | * first entry pointer if necessary. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 421 | */ |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 422 | if (base->first == &timer->node) |
| 423 | base->first = rb_next(&timer->node); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 424 | rb_erase(&timer->node, &base->active); |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 425 | timer->state = newstate; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* |
| 429 | * remove hrtimer, called with base lock held |
| 430 | */ |
| 431 | static inline int |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 432 | remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 433 | { |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 434 | if (hrtimer_is_queued(timer)) { |
| 435 | __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 436 | return 1; |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * hrtimer_start - (re)start an relative timer on the current CPU |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 443 | * @timer: the timer to be added |
| 444 | * @tim: expiry time |
| 445 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) |
| 446 | * |
| 447 | * Returns: |
| 448 | * 0 on success |
| 449 | * 1 when the timer was active |
| 450 | */ |
| 451 | int |
| 452 | hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) |
| 453 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 454 | struct hrtimer_clock_base *base, *new_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 455 | unsigned long flags; |
| 456 | int ret; |
| 457 | |
| 458 | base = lock_hrtimer_base(timer, &flags); |
| 459 | |
| 460 | /* Remove an active timer from the queue: */ |
| 461 | ret = remove_hrtimer(timer, base); |
| 462 | |
| 463 | /* Switch the timer base, if necessary: */ |
| 464 | new_base = switch_hrtimer_base(timer, base); |
| 465 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 466 | if (mode == HRTIMER_MODE_REL) { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 467 | tim = ktime_add(tim, new_base->get_time()); |
Ingo Molnar | 06027bd | 2006-02-14 13:53:15 -0800 | [diff] [blame] | 468 | /* |
| 469 | * CONFIG_TIME_LOW_RES is a temporary way for architectures |
| 470 | * to signal that they simply return xtime in |
| 471 | * do_gettimeoffset(). In this case we want to round up by |
| 472 | * resolution when starting a relative timer, to avoid short |
| 473 | * timeouts. This will go away with the GTOD framework. |
| 474 | */ |
| 475 | #ifdef CONFIG_TIME_LOW_RES |
| 476 | tim = ktime_add(tim, base->resolution); |
| 477 | #endif |
| 478 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 479 | timer->expires = tim; |
| 480 | |
| 481 | enqueue_hrtimer(timer, new_base); |
| 482 | |
| 483 | unlock_hrtimer_base(timer, &flags); |
| 484 | |
| 485 | return ret; |
| 486 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(hrtimer_start); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * hrtimer_try_to_cancel - try to deactivate a timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 491 | * @timer: hrtimer to stop |
| 492 | * |
| 493 | * Returns: |
| 494 | * 0 when the timer was not active |
| 495 | * 1 when the timer was active |
| 496 | * -1 when the timer is currently excuting the callback function and |
Randy Dunlap | fa9799e | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 497 | * cannot be stopped |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 498 | */ |
| 499 | int hrtimer_try_to_cancel(struct hrtimer *timer) |
| 500 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 501 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 502 | unsigned long flags; |
| 503 | int ret = -1; |
| 504 | |
| 505 | base = lock_hrtimer_base(timer, &flags); |
| 506 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 507 | if (!hrtimer_callback_running(timer)) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 508 | ret = remove_hrtimer(timer, base); |
| 509 | |
| 510 | unlock_hrtimer_base(timer, &flags); |
| 511 | |
| 512 | return ret; |
| 513 | |
| 514 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 515 | EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 516 | |
| 517 | /** |
| 518 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 519 | * @timer: the timer to be cancelled |
| 520 | * |
| 521 | * Returns: |
| 522 | * 0 when the timer was not active |
| 523 | * 1 when the timer was active |
| 524 | */ |
| 525 | int hrtimer_cancel(struct hrtimer *timer) |
| 526 | { |
| 527 | for (;;) { |
| 528 | int ret = hrtimer_try_to_cancel(timer); |
| 529 | |
| 530 | if (ret >= 0) |
| 531 | return ret; |
Joe Korty | 5ef37b1 | 2006-04-10 22:54:13 -0700 | [diff] [blame] | 532 | cpu_relax(); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 535 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * hrtimer_get_remaining - get remaining time for the timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 539 | * @timer: the timer to read |
| 540 | */ |
| 541 | ktime_t hrtimer_get_remaining(const struct hrtimer *timer) |
| 542 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 543 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 544 | unsigned long flags; |
| 545 | ktime_t rem; |
| 546 | |
| 547 | base = lock_hrtimer_base(timer, &flags); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 548 | rem = ktime_sub(timer->expires, base->get_time()); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 549 | unlock_hrtimer_base(timer, &flags); |
| 550 | |
| 551 | return rem; |
| 552 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 553 | EXPORT_SYMBOL_GPL(hrtimer_get_remaining); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 554 | |
Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 555 | #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ) |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 556 | /** |
| 557 | * hrtimer_get_next_event - get the time until next expiry event |
| 558 | * |
| 559 | * Returns the delta to the next expiry event or KTIME_MAX if no timer |
| 560 | * is pending. |
| 561 | */ |
| 562 | ktime_t hrtimer_get_next_event(void) |
| 563 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 564 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
| 565 | struct hrtimer_clock_base *base = cpu_base->clock_base; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 566 | ktime_t delta, mindelta = { .tv64 = KTIME_MAX }; |
| 567 | unsigned long flags; |
| 568 | int i; |
| 569 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 570 | spin_lock_irqsave(&cpu_base->lock, flags); |
| 571 | |
| 572 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 573 | struct hrtimer *timer; |
| 574 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 575 | if (!base->first) |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 576 | continue; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 577 | |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 578 | timer = rb_entry(base->first, struct hrtimer, node); |
| 579 | delta.tv64 = timer->expires.tv64; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 580 | delta = ktime_sub(delta, base->get_time()); |
| 581 | if (delta.tv64 < mindelta.tv64) |
| 582 | mindelta.tv64 = delta.tv64; |
| 583 | } |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 584 | |
| 585 | spin_unlock_irqrestore(&cpu_base->lock, flags); |
| 586 | |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 587 | if (mindelta.tv64 < 0) |
| 588 | mindelta.tv64 = 0; |
| 589 | return mindelta; |
| 590 | } |
| 591 | #endif |
| 592 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 593 | /** |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 594 | * hrtimer_init - initialize a timer to the given clock |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 595 | * @timer: the timer to be initialized |
| 596 | * @clock_id: the clock to be used |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 597 | * @mode: timer mode abs/rel |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 598 | */ |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 599 | void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 600 | enum hrtimer_mode mode) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 601 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 602 | struct hrtimer_cpu_base *cpu_base; |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 603 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 604 | memset(timer, 0, sizeof(struct hrtimer)); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 605 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 606 | cpu_base = &__raw_get_cpu_var(hrtimer_bases); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 607 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 608 | if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 609 | clock_id = CLOCK_MONOTONIC; |
| 610 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 611 | timer->base = &cpu_base->clock_base[clock_id]; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 612 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 613 | EXPORT_SYMBOL_GPL(hrtimer_init); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 614 | |
| 615 | /** |
| 616 | * hrtimer_get_res - get the timer resolution for a clock |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 617 | * @which_clock: which clock to query |
| 618 | * @tp: pointer to timespec variable to store the resolution |
| 619 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 620 | * Store the resolution of the clock selected by @which_clock in the |
| 621 | * variable pointed to by @tp. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 622 | */ |
| 623 | int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp) |
| 624 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 625 | struct hrtimer_cpu_base *cpu_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 626 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 627 | cpu_base = &__raw_get_cpu_var(hrtimer_bases); |
| 628 | *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 629 | |
| 630 | return 0; |
| 631 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 632 | EXPORT_SYMBOL_GPL(hrtimer_get_res); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | * Expire the per base hrtimer-queue: |
| 636 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 637 | static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base, |
| 638 | int index) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 639 | { |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 640 | struct rb_node *node; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 641 | struct hrtimer_clock_base *base = &cpu_base->clock_base[index]; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 642 | |
Dimitri Sivanich | 3055add | 2006-03-31 02:31:20 -0800 | [diff] [blame] | 643 | if (!base->first) |
| 644 | return; |
| 645 | |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 646 | if (base->get_softirq_time) |
| 647 | base->softirq_time = base->get_softirq_time(); |
| 648 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 649 | spin_lock_irq(&cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 650 | |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 651 | while ((node = base->first)) { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 652 | struct hrtimer *timer; |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 653 | enum hrtimer_restart (*fn)(struct hrtimer *); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 654 | int restart; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 655 | |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 656 | timer = rb_entry(node, struct hrtimer, node); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 657 | if (base->softirq_time.tv64 <= timer->expires.tv64) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | fn = timer->function; |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 661 | __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 662 | spin_unlock_irq(&cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 663 | |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame] | 664 | restart = fn(timer); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 665 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 666 | spin_lock_irq(&cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 667 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 668 | timer->state &= ~HRTIMER_STATE_CALLBACK; |
Roman Zippel | b75f7a5 | 2006-03-26 01:38:09 -0800 | [diff] [blame] | 669 | if (restart != HRTIMER_NORESTART) { |
| 670 | BUG_ON(hrtimer_active(timer)); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 671 | enqueue_hrtimer(timer, base); |
Roman Zippel | b75f7a5 | 2006-03-26 01:38:09 -0800 | [diff] [blame] | 672 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 673 | } |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 674 | spin_unlock_irq(&cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Called from timer softirq every jiffy, expire hrtimers: |
| 679 | */ |
| 680 | void hrtimer_run_queues(void) |
| 681 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 682 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 683 | int i; |
| 684 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 685 | hrtimer_get_softirq_time(cpu_base); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 686 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 687 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) |
| 688 | run_hrtimer_queue(cpu_base, i); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 692 | * Sleep related functions: |
| 693 | */ |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 694 | static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 695 | { |
| 696 | struct hrtimer_sleeper *t = |
| 697 | container_of(timer, struct hrtimer_sleeper, timer); |
| 698 | struct task_struct *task = t->task; |
| 699 | |
| 700 | t->task = NULL; |
| 701 | if (task) |
| 702 | wake_up_process(task); |
| 703 | |
| 704 | return HRTIMER_NORESTART; |
| 705 | } |
| 706 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 707 | void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 708 | { |
| 709 | sl->timer.function = hrtimer_wakeup; |
| 710 | sl->task = task; |
| 711 | } |
| 712 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 713 | static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 714 | { |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 715 | hrtimer_init_sleeper(t, current); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 716 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 717 | do { |
| 718 | set_current_state(TASK_INTERRUPTIBLE); |
| 719 | hrtimer_start(&t->timer, t->timer.expires, mode); |
| 720 | |
| 721 | schedule(); |
| 722 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 723 | hrtimer_cancel(&t->timer); |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 724 | mode = HRTIMER_MODE_ABS; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 725 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 726 | } while (t->task && !signal_pending(current)); |
| 727 | |
| 728 | return t->task == NULL; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 731 | long __sched hrtimer_nanosleep_restart(struct restart_block *restart) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 732 | { |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 733 | struct hrtimer_sleeper t; |
Ingo Molnar | ea13dbc | 2006-01-16 10:59:41 +0100 | [diff] [blame] | 734 | struct timespec __user *rmtp; |
| 735 | struct timespec tu; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 736 | ktime_t time; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 737 | |
| 738 | restart->fn = do_no_restart_syscall; |
| 739 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 740 | hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS); |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 741 | t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 742 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 743 | if (do_nanosleep(&t, HRTIMER_MODE_ABS)) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 744 | return 0; |
| 745 | |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 746 | rmtp = (struct timespec __user *) restart->arg1; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 747 | if (rmtp) { |
| 748 | time = ktime_sub(t.timer.expires, t.timer.base->get_time()); |
| 749 | if (time.tv64 <= 0) |
| 750 | return 0; |
| 751 | tu = ktime_to_timespec(time); |
| 752 | if (copy_to_user(rmtp, &tu, sizeof(tu))) |
| 753 | return -EFAULT; |
| 754 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 755 | |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 756 | restart->fn = hrtimer_nanosleep_restart; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 757 | |
| 758 | /* The other values in restart are already filled in */ |
| 759 | return -ERESTART_RESTARTBLOCK; |
| 760 | } |
| 761 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 762 | long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, |
| 763 | const enum hrtimer_mode mode, const clockid_t clockid) |
| 764 | { |
| 765 | struct restart_block *restart; |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 766 | struct hrtimer_sleeper t; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 767 | struct timespec tu; |
| 768 | ktime_t rem; |
| 769 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 770 | hrtimer_init(&t.timer, clockid, mode); |
| 771 | t.timer.expires = timespec_to_ktime(*rqtp); |
| 772 | if (do_nanosleep(&t, mode)) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 773 | return 0; |
| 774 | |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 775 | /* Absolute timers do not update the rmtp value and restart: */ |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 776 | if (mode == HRTIMER_MODE_ABS) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 777 | return -ERESTARTNOHAND; |
| 778 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 779 | if (rmtp) { |
| 780 | rem = ktime_sub(t.timer.expires, t.timer.base->get_time()); |
| 781 | if (rem.tv64 <= 0) |
| 782 | return 0; |
| 783 | tu = ktime_to_timespec(rem); |
| 784 | if (copy_to_user(rmtp, &tu, sizeof(tu))) |
| 785 | return -EFAULT; |
| 786 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 787 | |
| 788 | restart = ¤t_thread_info()->restart_block; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 789 | restart->fn = hrtimer_nanosleep_restart; |
| 790 | restart->arg0 = (unsigned long) t.timer.base->index; |
| 791 | restart->arg1 = (unsigned long) rmtp; |
| 792 | restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF; |
| 793 | restart->arg3 = t.timer.expires.tv64 >> 32; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 794 | |
| 795 | return -ERESTART_RESTARTBLOCK; |
| 796 | } |
| 797 | |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 798 | asmlinkage long |
| 799 | sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp) |
| 800 | { |
| 801 | struct timespec tu; |
| 802 | |
| 803 | if (copy_from_user(&tu, rqtp, sizeof(tu))) |
| 804 | return -EFAULT; |
| 805 | |
| 806 | if (!timespec_valid(&tu)) |
| 807 | return -EINVAL; |
| 808 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 809 | return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 812 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 813 | * Functions related to boot-time initialization: |
| 814 | */ |
| 815 | static void __devinit init_hrtimers_cpu(int cpu) |
| 816 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 817 | struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 818 | int i; |
| 819 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 820 | spin_lock_init(&cpu_base->lock); |
| 821 | lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key); |
| 822 | |
| 823 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) |
| 824 | cpu_base->clock_base[i].cpu_base = cpu_base; |
| 825 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | #ifdef CONFIG_HOTPLUG_CPU |
| 829 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 830 | static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, |
| 831 | struct hrtimer_clock_base *new_base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 832 | { |
| 833 | struct hrtimer *timer; |
| 834 | struct rb_node *node; |
| 835 | |
| 836 | while ((node = rb_first(&old_base->active))) { |
| 837 | timer = rb_entry(node, struct hrtimer, node); |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 838 | BUG_ON(timer->state & HRTIMER_STATE_CALLBACK); |
| 839 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 840 | timer->base = new_base; |
| 841 | enqueue_hrtimer(timer, new_base); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | static void migrate_hrtimers(int cpu) |
| 846 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 847 | struct hrtimer_cpu_base *old_base, *new_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 848 | int i; |
| 849 | |
| 850 | BUG_ON(cpu_online(cpu)); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 851 | old_base = &per_cpu(hrtimer_bases, cpu); |
| 852 | new_base = &get_cpu_var(hrtimer_bases); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 853 | |
| 854 | local_irq_disable(); |
| 855 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 856 | spin_lock(&new_base->lock); |
| 857 | spin_lock(&old_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 858 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 859 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 860 | migrate_hrtimer_list(&old_base->clock_base[i], |
| 861 | &new_base->clock_base[i]); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 862 | } |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 863 | spin_unlock(&old_base->lock); |
| 864 | spin_unlock(&new_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 865 | |
| 866 | local_irq_enable(); |
| 867 | put_cpu_var(hrtimer_bases); |
| 868 | } |
| 869 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 870 | |
Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 871 | static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self, |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 872 | unsigned long action, void *hcpu) |
| 873 | { |
| 874 | long cpu = (long)hcpu; |
| 875 | |
| 876 | switch (action) { |
| 877 | |
| 878 | case CPU_UP_PREPARE: |
| 879 | init_hrtimers_cpu(cpu); |
| 880 | break; |
| 881 | |
| 882 | #ifdef CONFIG_HOTPLUG_CPU |
| 883 | case CPU_DEAD: |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame^] | 884 | clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 885 | migrate_hrtimers(cpu); |
| 886 | break; |
| 887 | #endif |
| 888 | |
| 889 | default: |
| 890 | break; |
| 891 | } |
| 892 | |
| 893 | return NOTIFY_OK; |
| 894 | } |
| 895 | |
Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 896 | static struct notifier_block __cpuinitdata hrtimers_nb = { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 897 | .notifier_call = hrtimer_cpu_notify, |
| 898 | }; |
| 899 | |
| 900 | void __init hrtimers_init(void) |
| 901 | { |
| 902 | hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE, |
| 903 | (void *)(long)smp_processor_id()); |
| 904 | register_cpu_notifier(&hrtimers_nb); |
| 905 | } |
| 906 | |