ia64/linux-2.6.18-xen.hg

annotate kernel/timer.c @ 798:b02a90bf5bbc

ACPI: Backport missing part for T-State MSR support

Part of below kernel commit was missed while packporting T-State
support.

commit f79f06ab9f86d7203006d2ec8992ac80df36a34e
Author: Zhao Yakui <yakui.zhao@intel.com>
Date: Thu Nov 15 17:06:36 2007 +0800

ACPI: Enable MSR (FixedHW) support for T-States

Add throttling control via MSR when T-states uses
the FixHW Control Status registers.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Signed-off-by: Li Shaohua <shaohua.li@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>

Signed-off-by: Wei Gang <gang.wei@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Mar 02 10:53:59 2009 +0000 (2009-03-02)
parents 61ed8662b69c
children
rev   line source
ian@0 1 /*
ian@0 2 * linux/kernel/timer.c
ian@0 3 *
ian@0 4 * Kernel internal timers, kernel timekeeping, basic process system calls
ian@0 5 *
ian@0 6 * Copyright (C) 1991, 1992 Linus Torvalds
ian@0 7 *
ian@0 8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
ian@0 9 *
ian@0 10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
ian@0 11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
ian@0 12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
ian@0 13 * serialize accesses to xtime/lost_ticks).
ian@0 14 * Copyright (C) 1998 Andrea Arcangeli
ian@0 15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
ian@0 16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
ian@0 17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
ian@0 18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
ian@0 19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
ian@0 20 */
ian@0 21
ian@0 22 #include <linux/kernel_stat.h>
ian@0 23 #include <linux/module.h>
ian@0 24 #include <linux/interrupt.h>
ian@0 25 #include <linux/percpu.h>
ian@0 26 #include <linux/init.h>
ian@0 27 #include <linux/mm.h>
ian@0 28 #include <linux/swap.h>
ian@0 29 #include <linux/notifier.h>
ian@0 30 #include <linux/thread_info.h>
ian@0 31 #include <linux/time.h>
ian@0 32 #include <linux/jiffies.h>
ian@0 33 #include <linux/posix-timers.h>
ian@0 34 #include <linux/cpu.h>
ian@0 35 #include <linux/syscalls.h>
ian@0 36 #include <linux/delay.h>
ian@0 37
ian@0 38 #include <asm/uaccess.h>
ian@0 39 #include <asm/unistd.h>
ian@0 40 #include <asm/div64.h>
ian@0 41 #include <asm/timex.h>
ian@0 42 #include <asm/io.h>
ian@0 43
ian@0 44 #ifdef CONFIG_TIME_INTERPOLATION
ian@0 45 static void time_interpolator_update(long delta_nsec);
ian@0 46 #else
ian@0 47 #define time_interpolator_update(x)
ian@0 48 #endif
ian@0 49
ian@0 50 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
ian@0 51
ian@0 52 EXPORT_SYMBOL(jiffies_64);
ian@0 53
ian@0 54 /*
ian@0 55 * per-CPU timer vector definitions:
ian@0 56 */
ian@0 57 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
ian@0 58 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
ian@0 59 #define TVN_SIZE (1 << TVN_BITS)
ian@0 60 #define TVR_SIZE (1 << TVR_BITS)
ian@0 61 #define TVN_MASK (TVN_SIZE - 1)
ian@0 62 #define TVR_MASK (TVR_SIZE - 1)
ian@0 63
ian@0 64 typedef struct tvec_s {
ian@0 65 struct list_head vec[TVN_SIZE];
ian@0 66 } tvec_t;
ian@0 67
ian@0 68 typedef struct tvec_root_s {
ian@0 69 struct list_head vec[TVR_SIZE];
ian@0 70 } tvec_root_t;
ian@0 71
ian@0 72 struct tvec_t_base_s {
ian@0 73 spinlock_t lock;
ian@0 74 struct timer_list *running_timer;
ian@0 75 unsigned long timer_jiffies;
ian@0 76 tvec_root_t tv1;
ian@0 77 tvec_t tv2;
ian@0 78 tvec_t tv3;
ian@0 79 tvec_t tv4;
ian@0 80 tvec_t tv5;
ian@0 81 } ____cacheline_aligned_in_smp;
ian@0 82
ian@0 83 typedef struct tvec_t_base_s tvec_base_t;
ian@0 84
ian@0 85 tvec_base_t boot_tvec_bases;
ian@0 86 EXPORT_SYMBOL(boot_tvec_bases);
ian@0 87 static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
ian@0 88
ian@0 89 static inline void set_running_timer(tvec_base_t *base,
ian@0 90 struct timer_list *timer)
ian@0 91 {
ian@0 92 #ifdef CONFIG_SMP
ian@0 93 base->running_timer = timer;
ian@0 94 #endif
ian@0 95 }
ian@0 96
ian@0 97 static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
ian@0 98 {
ian@0 99 unsigned long expires = timer->expires;
ian@0 100 unsigned long idx = expires - base->timer_jiffies;
ian@0 101 struct list_head *vec;
ian@0 102
ian@0 103 if (idx < TVR_SIZE) {
ian@0 104 int i = expires & TVR_MASK;
ian@0 105 vec = base->tv1.vec + i;
ian@0 106 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
ian@0 107 int i = (expires >> TVR_BITS) & TVN_MASK;
ian@0 108 vec = base->tv2.vec + i;
ian@0 109 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
ian@0 110 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
ian@0 111 vec = base->tv3.vec + i;
ian@0 112 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
ian@0 113 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
ian@0 114 vec = base->tv4.vec + i;
ian@0 115 } else if ((signed long) idx < 0) {
ian@0 116 /*
ian@0 117 * Can happen if you add a timer with expires == jiffies,
ian@0 118 * or you set a timer to go off in the past
ian@0 119 */
ian@0 120 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
ian@0 121 } else {
ian@0 122 int i;
ian@0 123 /* If the timeout is larger than 0xffffffff on 64-bit
ian@0 124 * architectures then we use the maximum timeout:
ian@0 125 */
ian@0 126 if (idx > 0xffffffffUL) {
ian@0 127 idx = 0xffffffffUL;
ian@0 128 expires = idx + base->timer_jiffies;
ian@0 129 }
ian@0 130 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
ian@0 131 vec = base->tv5.vec + i;
ian@0 132 }
ian@0 133 /*
ian@0 134 * Timers are FIFO:
ian@0 135 */
ian@0 136 list_add_tail(&timer->entry, vec);
ian@0 137 }
ian@0 138
ian@0 139 /***
ian@0 140 * init_timer - initialize a timer.
ian@0 141 * @timer: the timer to be initialized
ian@0 142 *
ian@0 143 * init_timer() must be done to a timer prior calling *any* of the
ian@0 144 * other timer functions.
ian@0 145 */
ian@0 146 void fastcall init_timer(struct timer_list *timer)
ian@0 147 {
ian@0 148 timer->entry.next = NULL;
ian@0 149 timer->base = __raw_get_cpu_var(tvec_bases);
ian@0 150 }
ian@0 151 EXPORT_SYMBOL(init_timer);
ian@0 152
ian@0 153 static inline void detach_timer(struct timer_list *timer,
ian@0 154 int clear_pending)
ian@0 155 {
ian@0 156 struct list_head *entry = &timer->entry;
ian@0 157
ian@0 158 __list_del(entry->prev, entry->next);
ian@0 159 if (clear_pending)
ian@0 160 entry->next = NULL;
ian@0 161 entry->prev = LIST_POISON2;
ian@0 162 }
ian@0 163
ian@0 164 /*
ian@0 165 * We are using hashed locking: holding per_cpu(tvec_bases).lock
ian@0 166 * means that all timers which are tied to this base via timer->base are
ian@0 167 * locked, and the base itself is locked too.
ian@0 168 *
ian@0 169 * So __run_timers/migrate_timers can safely modify all timers which could
ian@0 170 * be found on ->tvX lists.
ian@0 171 *
ian@0 172 * When the timer's base is locked, and the timer removed from list, it is
ian@0 173 * possible to set timer->base = NULL and drop the lock: the timer remains
ian@0 174 * locked.
ian@0 175 */
ian@0 176 static tvec_base_t *lock_timer_base(struct timer_list *timer,
ian@0 177 unsigned long *flags)
ian@0 178 {
ian@0 179 tvec_base_t *base;
ian@0 180
ian@0 181 for (;;) {
ian@0 182 base = timer->base;
ian@0 183 if (likely(base != NULL)) {
ian@0 184 spin_lock_irqsave(&base->lock, *flags);
ian@0 185 if (likely(base == timer->base))
ian@0 186 return base;
ian@0 187 /* The timer has migrated to another CPU */
ian@0 188 spin_unlock_irqrestore(&base->lock, *flags);
ian@0 189 }
ian@0 190 cpu_relax();
ian@0 191 }
ian@0 192 }
ian@0 193
ian@0 194 int __mod_timer(struct timer_list *timer, unsigned long expires)
ian@0 195 {
ian@0 196 tvec_base_t *base, *new_base;
ian@0 197 unsigned long flags;
ian@0 198 int ret = 0;
ian@0 199
ian@0 200 BUG_ON(!timer->function);
ian@0 201
ian@0 202 base = lock_timer_base(timer, &flags);
ian@0 203
ian@0 204 if (timer_pending(timer)) {
ian@0 205 detach_timer(timer, 0);
ian@0 206 ret = 1;
ian@0 207 }
ian@0 208
ian@0 209 new_base = __get_cpu_var(tvec_bases);
ian@0 210
ian@0 211 if (base != new_base) {
ian@0 212 /*
ian@0 213 * We are trying to schedule the timer on the local CPU.
ian@0 214 * However we can't change timer's base while it is running,
ian@0 215 * otherwise del_timer_sync() can't detect that the timer's
ian@0 216 * handler yet has not finished. This also guarantees that
ian@0 217 * the timer is serialized wrt itself.
ian@0 218 */
ian@0 219 if (likely(base->running_timer != timer)) {
ian@0 220 /* See the comment in lock_timer_base() */
ian@0 221 timer->base = NULL;
ian@0 222 spin_unlock(&base->lock);
ian@0 223 base = new_base;
ian@0 224 spin_lock(&base->lock);
ian@0 225 timer->base = base;
ian@0 226 }
ian@0 227 }
ian@0 228
ian@0 229 timer->expires = expires;
ian@0 230 internal_add_timer(base, timer);
ian@0 231 spin_unlock_irqrestore(&base->lock, flags);
ian@0 232
ian@0 233 return ret;
ian@0 234 }
ian@0 235
ian@0 236 EXPORT_SYMBOL(__mod_timer);
ian@0 237
ian@0 238 /***
ian@0 239 * add_timer_on - start a timer on a particular CPU
ian@0 240 * @timer: the timer to be added
ian@0 241 * @cpu: the CPU to start it on
ian@0 242 *
ian@0 243 * This is not very scalable on SMP. Double adds are not possible.
ian@0 244 */
ian@0 245 void add_timer_on(struct timer_list *timer, int cpu)
ian@0 246 {
ian@0 247 tvec_base_t *base = per_cpu(tvec_bases, cpu);
ian@0 248 unsigned long flags;
ian@0 249
ian@0 250 BUG_ON(timer_pending(timer) || !timer->function);
ian@0 251 spin_lock_irqsave(&base->lock, flags);
ian@0 252 timer->base = base;
ian@0 253 internal_add_timer(base, timer);
ian@0 254 spin_unlock_irqrestore(&base->lock, flags);
ian@0 255 }
ian@0 256
ian@0 257
ian@0 258 /***
ian@0 259 * mod_timer - modify a timer's timeout
ian@0 260 * @timer: the timer to be modified
ian@0 261 *
ian@0 262 * mod_timer is a more efficient way to update the expire field of an
ian@0 263 * active timer (if the timer is inactive it will be activated)
ian@0 264 *
ian@0 265 * mod_timer(timer, expires) is equivalent to:
ian@0 266 *
ian@0 267 * del_timer(timer); timer->expires = expires; add_timer(timer);
ian@0 268 *
ian@0 269 * Note that if there are multiple unserialized concurrent users of the
ian@0 270 * same timer, then mod_timer() is the only safe way to modify the timeout,
ian@0 271 * since add_timer() cannot modify an already running timer.
ian@0 272 *
ian@0 273 * The function returns whether it has modified a pending timer or not.
ian@0 274 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
ian@0 275 * active timer returns 1.)
ian@0 276 */
ian@0 277 int mod_timer(struct timer_list *timer, unsigned long expires)
ian@0 278 {
ian@0 279 BUG_ON(!timer->function);
ian@0 280
ian@0 281 /*
ian@0 282 * This is a common optimization triggered by the
ian@0 283 * networking code - if the timer is re-modified
ian@0 284 * to be the same thing then just return:
ian@0 285 */
ian@0 286 if (timer->expires == expires && timer_pending(timer))
ian@0 287 return 1;
ian@0 288
ian@0 289 return __mod_timer(timer, expires);
ian@0 290 }
ian@0 291
ian@0 292 EXPORT_SYMBOL(mod_timer);
ian@0 293
ian@0 294 /***
ian@0 295 * del_timer - deactive a timer.
ian@0 296 * @timer: the timer to be deactivated
ian@0 297 *
ian@0 298 * del_timer() deactivates a timer - this works on both active and inactive
ian@0 299 * timers.
ian@0 300 *
ian@0 301 * The function returns whether it has deactivated a pending timer or not.
ian@0 302 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
ian@0 303 * active timer returns 1.)
ian@0 304 */
ian@0 305 int del_timer(struct timer_list *timer)
ian@0 306 {
ian@0 307 tvec_base_t *base;
ian@0 308 unsigned long flags;
ian@0 309 int ret = 0;
ian@0 310
ian@0 311 if (timer_pending(timer)) {
ian@0 312 base = lock_timer_base(timer, &flags);
ian@0 313 if (timer_pending(timer)) {
ian@0 314 detach_timer(timer, 1);
ian@0 315 ret = 1;
ian@0 316 }
ian@0 317 spin_unlock_irqrestore(&base->lock, flags);
ian@0 318 }
ian@0 319
ian@0 320 return ret;
ian@0 321 }
ian@0 322
ian@0 323 EXPORT_SYMBOL(del_timer);
ian@0 324
ian@0 325 #ifdef CONFIG_SMP
ian@0 326 /*
ian@0 327 * This function tries to deactivate a timer. Upon successful (ret >= 0)
ian@0 328 * exit the timer is not queued and the handler is not running on any CPU.
ian@0 329 *
ian@0 330 * It must not be called from interrupt contexts.
ian@0 331 */
ian@0 332 int try_to_del_timer_sync(struct timer_list *timer)
ian@0 333 {
ian@0 334 tvec_base_t *base;
ian@0 335 unsigned long flags;
ian@0 336 int ret = -1;
ian@0 337
ian@0 338 base = lock_timer_base(timer, &flags);
ian@0 339
ian@0 340 if (base->running_timer == timer)
ian@0 341 goto out;
ian@0 342
ian@0 343 ret = 0;
ian@0 344 if (timer_pending(timer)) {
ian@0 345 detach_timer(timer, 1);
ian@0 346 ret = 1;
ian@0 347 }
ian@0 348 out:
ian@0 349 spin_unlock_irqrestore(&base->lock, flags);
ian@0 350
ian@0 351 return ret;
ian@0 352 }
ian@0 353
ian@0 354 /***
ian@0 355 * del_timer_sync - deactivate a timer and wait for the handler to finish.
ian@0 356 * @timer: the timer to be deactivated
ian@0 357 *
ian@0 358 * This function only differs from del_timer() on SMP: besides deactivating
ian@0 359 * the timer it also makes sure the handler has finished executing on other
ian@0 360 * CPUs.
ian@0 361 *
ian@0 362 * Synchronization rules: callers must prevent restarting of the timer,
ian@0 363 * otherwise this function is meaningless. It must not be called from
ian@0 364 * interrupt contexts. The caller must not hold locks which would prevent
ian@0 365 * completion of the timer's handler. The timer's handler must not call
ian@0 366 * add_timer_on(). Upon exit the timer is not queued and the handler is
ian@0 367 * not running on any CPU.
ian@0 368 *
ian@0 369 * The function returns whether it has deactivated a pending timer or not.
ian@0 370 */
ian@0 371 int del_timer_sync(struct timer_list *timer)
ian@0 372 {
ian@0 373 for (;;) {
ian@0 374 int ret = try_to_del_timer_sync(timer);
ian@0 375 if (ret >= 0)
ian@0 376 return ret;
ian@0 377 cpu_relax();
ian@0 378 }
ian@0 379 }
ian@0 380
ian@0 381 EXPORT_SYMBOL(del_timer_sync);
ian@0 382 #endif
ian@0 383
ian@0 384 static int cascade(tvec_base_t *base, tvec_t *tv, int index)
ian@0 385 {
ian@0 386 /* cascade all the timers from tv up one level */
ian@0 387 struct timer_list *timer, *tmp;
ian@0 388 struct list_head tv_list;
ian@0 389
ian@0 390 list_replace_init(tv->vec + index, &tv_list);
ian@0 391
ian@0 392 /*
ian@0 393 * We are removing _all_ timers from the list, so we
ian@0 394 * don't have to detach them individually.
ian@0 395 */
ian@0 396 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
ian@0 397 BUG_ON(timer->base != base);
ian@0 398 internal_add_timer(base, timer);
ian@0 399 }
ian@0 400
ian@0 401 return index;
ian@0 402 }
ian@0 403
ian@0 404 /***
ian@0 405 * __run_timers - run all expired timers (if any) on this CPU.
ian@0 406 * @base: the timer vector to be processed.
ian@0 407 *
ian@0 408 * This function cascades all vectors and executes all expired timer
ian@0 409 * vectors.
ian@0 410 */
ian@0 411 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
ian@0 412
ian@0 413 static inline void __run_timers(tvec_base_t *base)
ian@0 414 {
ian@0 415 struct timer_list *timer;
ian@0 416
ian@0 417 spin_lock_irq(&base->lock);
ian@0 418 while (time_after_eq(jiffies, base->timer_jiffies)) {
ian@0 419 struct list_head work_list;
ian@0 420 struct list_head *head = &work_list;
ian@0 421 int index = base->timer_jiffies & TVR_MASK;
ian@0 422
ian@0 423 /*
ian@0 424 * Cascade timers:
ian@0 425 */
ian@0 426 if (!index &&
ian@0 427 (!cascade(base, &base->tv2, INDEX(0))) &&
ian@0 428 (!cascade(base, &base->tv3, INDEX(1))) &&
ian@0 429 !cascade(base, &base->tv4, INDEX(2)))
ian@0 430 cascade(base, &base->tv5, INDEX(3));
ian@0 431 ++base->timer_jiffies;
ian@0 432 list_replace_init(base->tv1.vec + index, &work_list);
ian@0 433 while (!list_empty(head)) {
ian@0 434 void (*fn)(unsigned long);
ian@0 435 unsigned long data;
ian@0 436
ian@0 437 timer = list_entry(head->next,struct timer_list,entry);
ian@0 438 fn = timer->function;
ian@0 439 data = timer->data;
ian@0 440
ian@0 441 set_running_timer(base, timer);
ian@0 442 detach_timer(timer, 1);
ian@0 443 spin_unlock_irq(&base->lock);
ian@0 444 {
ian@0 445 int preempt_count = preempt_count();
ian@0 446 fn(data);
ian@0 447 if (preempt_count != preempt_count()) {
ian@0 448 printk(KERN_WARNING "huh, entered %p "
ian@0 449 "with preempt_count %08x, exited"
ian@0 450 " with %08x?\n",
ian@0 451 fn, preempt_count,
ian@0 452 preempt_count());
ian@0 453 BUG();
ian@0 454 }
ian@0 455 }
ian@0 456 spin_lock_irq(&base->lock);
ian@0 457 }
ian@0 458 }
ian@0 459 set_running_timer(base, NULL);
ian@0 460 spin_unlock_irq(&base->lock);
ian@0 461 }
ian@0 462
ian@0 463 #ifdef CONFIG_NO_IDLE_HZ
ian@0 464 /*
ian@0 465 * Find out when the next timer event is due to happen. This
ian@0 466 * is used on S/390 to stop all activity when a cpus is idle.
ian@0 467 * This functions needs to be called disabled.
ian@0 468 */
ian@0 469 unsigned long next_timer_interrupt(void)
ian@0 470 {
ian@0 471 tvec_base_t *base;
ian@0 472 struct list_head *list;
ian@0 473 struct timer_list *nte;
ian@0 474 unsigned long expires;
ian@0 475 unsigned long hr_expires = MAX_JIFFY_OFFSET;
ian@0 476 ktime_t hr_delta;
ian@0 477 tvec_t *varray[4];
ian@0 478 int i, j;
ian@0 479
ian@0 480 hr_delta = hrtimer_get_next_event();
ian@0 481 if (hr_delta.tv64 != KTIME_MAX) {
ian@0 482 struct timespec tsdelta;
ian@0 483 tsdelta = ktime_to_timespec(hr_delta);
ian@0 484 hr_expires = timespec_to_jiffies(&tsdelta);
ian@0 485 if (hr_expires < 3)
ian@0 486 return hr_expires + jiffies;
ian@0 487 }
ian@22 488 hr_expires = min_t(unsigned long,
ian@22 489 softlockup_get_next_event(),
ian@22 490 hr_expires) + jiffies;
ian@0 491
ian@0 492 base = __get_cpu_var(tvec_bases);
ian@0 493 spin_lock(&base->lock);
ian@0 494 expires = base->timer_jiffies + (LONG_MAX >> 1);
ian@0 495 list = NULL;
ian@0 496
ian@0 497 /* Look for timer events in tv1. */
ian@0 498 j = base->timer_jiffies & TVR_MASK;
ian@0 499 do {
ian@0 500 list_for_each_entry(nte, base->tv1.vec + j, entry) {
ian@0 501 expires = nte->expires;
ian@0 502 if (j < (base->timer_jiffies & TVR_MASK))
ian@0 503 list = base->tv2.vec + (INDEX(0));
ian@0 504 goto found;
ian@0 505 }
ian@0 506 j = (j + 1) & TVR_MASK;
ian@0 507 } while (j != (base->timer_jiffies & TVR_MASK));
ian@0 508
ian@0 509 /* Check tv2-tv5. */
ian@0 510 varray[0] = &base->tv2;
ian@0 511 varray[1] = &base->tv3;
ian@0 512 varray[2] = &base->tv4;
ian@0 513 varray[3] = &base->tv5;
ian@0 514 for (i = 0; i < 4; i++) {
ian@0 515 j = INDEX(i);
ian@0 516 do {
ian@0 517 if (list_empty(varray[i]->vec + j)) {
ian@0 518 j = (j + 1) & TVN_MASK;
ian@0 519 continue;
ian@0 520 }
ian@0 521 list_for_each_entry(nte, varray[i]->vec + j, entry)
ian@0 522 if (time_before(nte->expires, expires))
ian@0 523 expires = nte->expires;
ian@0 524 if (j < (INDEX(i)) && i < 3)
ian@0 525 list = varray[i + 1]->vec + (INDEX(i + 1));
ian@0 526 goto found;
ian@0 527 } while (j != (INDEX(i)));
ian@0 528 }
ian@0 529 found:
ian@0 530 if (list) {
ian@0 531 /*
ian@0 532 * The search wrapped. We need to look at the next list
ian@0 533 * from next tv element that would cascade into tv element
ian@0 534 * where we found the timer element.
ian@0 535 */
ian@0 536 list_for_each_entry(nte, list, entry) {
ian@0 537 if (time_before(nte->expires, expires))
ian@0 538 expires = nte->expires;
ian@0 539 }
ian@0 540 }
ian@0 541 spin_unlock(&base->lock);
ian@0 542
ian@0 543 /*
ian@0 544 * It can happen that other CPUs service timer IRQs and increment
ian@0 545 * jiffies, but we have not yet got a local timer tick to process
ian@0 546 * the timer wheels. In that case, the expiry time can be before
ian@0 547 * jiffies, but since the high-resolution timer here is relative to
ian@0 548 * jiffies, the default expression when high-resolution timers are
ian@0 549 * not active,
ian@0 550 *
ian@0 551 * time_before(MAX_JIFFY_OFFSET + jiffies, expires)
ian@0 552 *
ian@0 553 * would falsely evaluate to true. If that is the case, just
ian@0 554 * return jiffies so that we can immediately fire the local timer
ian@0 555 */
ian@0 556 if (time_before(expires, jiffies))
ian@0 557 return jiffies;
ian@0 558
ian@0 559 if (time_before(hr_expires, expires))
ian@0 560 return hr_expires;
ian@0 561
ian@0 562 return expires;
ian@0 563 }
ian@0 564 #endif
ian@0 565
ian@0 566 /******************************************************************/
ian@0 567
ian@0 568 /*
ian@0 569 * Timekeeping variables
ian@0 570 */
ian@0 571 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
ian@0 572 unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
ian@0 573
ian@0 574 /*
ian@0 575 * The current time
ian@0 576 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
ian@0 577 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
ian@0 578 * at zero at system boot time, so wall_to_monotonic will be negative,
ian@0 579 * however, we will ALWAYS keep the tv_nsec part positive so we can use
ian@0 580 * the usual normalization.
ian@0 581 */
ian@0 582 struct timespec xtime __attribute__ ((aligned (16)));
ian@0 583 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
ian@0 584
ian@0 585 EXPORT_SYMBOL(xtime);
ian@0 586
ian@0 587 /* Don't completely fail for HZ > 500. */
ian@0 588 int tickadj = 500/HZ ? : 1; /* microsecs */
ian@0 589
ian@0 590
ian@0 591 /*
ian@0 592 * phase-lock loop variables
ian@0 593 */
ian@0 594 /* TIME_ERROR prevents overwriting the CMOS clock */
ian@0 595 int time_state = TIME_OK; /* clock synchronization status */
ian@0 596 int time_status = STA_UNSYNC; /* clock status bits */
ian@0 597 long time_offset; /* time adjustment (us) */
ian@0 598 long time_constant = 2; /* pll time constant */
ian@0 599 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
ian@0 600 long time_precision = 1; /* clock precision (us) */
ian@0 601 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
ian@0 602 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
ian@0 603 long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
ian@0 604 /* frequency offset (scaled ppm)*/
ian@0 605 static long time_adj; /* tick adjust (scaled 1 / HZ) */
ian@0 606 long time_reftime; /* time at last adjustment (s) */
ian@0 607 long time_adjust;
ian@0 608 long time_next_adjust;
ian@0 609
ian@0 610 /*
ian@0 611 * this routine handles the overflow of the microsecond field
ian@0 612 *
ian@0 613 * The tricky bits of code to handle the accurate clock support
ian@0 614 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
ian@0 615 * They were originally developed for SUN and DEC kernels.
ian@0 616 * All the kudos should go to Dave for this stuff.
ian@0 617 *
ian@0 618 */
ian@0 619 static void second_overflow(void)
ian@0 620 {
ian@0 621 long ltemp;
ian@0 622
ian@0 623 /* Bump the maxerror field */
ian@0 624 time_maxerror += time_tolerance >> SHIFT_USEC;
ian@0 625 if (time_maxerror > NTP_PHASE_LIMIT) {
ian@0 626 time_maxerror = NTP_PHASE_LIMIT;
ian@0 627 time_status |= STA_UNSYNC;
ian@0 628 }
ian@0 629
ian@0 630 /*
ian@0 631 * Leap second processing. If in leap-insert state at the end of the
ian@0 632 * day, the system clock is set back one second; if in leap-delete
ian@0 633 * state, the system clock is set ahead one second. The microtime()
ian@0 634 * routine or external clock driver will insure that reported time is
ian@0 635 * always monotonic. The ugly divides should be replaced.
ian@0 636 */
ian@0 637 switch (time_state) {
ian@0 638 case TIME_OK:
ian@0 639 if (time_status & STA_INS)
ian@0 640 time_state = TIME_INS;
ian@0 641 else if (time_status & STA_DEL)
ian@0 642 time_state = TIME_DEL;
ian@0 643 break;
ian@0 644 case TIME_INS:
ian@0 645 if (xtime.tv_sec % 86400 == 0) {
ian@0 646 xtime.tv_sec--;
ian@0 647 wall_to_monotonic.tv_sec++;
ian@0 648 /*
ian@0 649 * The timer interpolator will make time change
ian@0 650 * gradually instead of an immediate jump by one second
ian@0 651 */
ian@0 652 time_interpolator_update(-NSEC_PER_SEC);
ian@0 653 time_state = TIME_OOP;
ian@0 654 clock_was_set();
ian@0 655 printk(KERN_NOTICE "Clock: inserting leap second "
ian@0 656 "23:59:60 UTC\n");
ian@0 657 }
ian@0 658 break;
ian@0 659 case TIME_DEL:
ian@0 660 if ((xtime.tv_sec + 1) % 86400 == 0) {
ian@0 661 xtime.tv_sec++;
ian@0 662 wall_to_monotonic.tv_sec--;
ian@0 663 /*
ian@0 664 * Use of time interpolator for a gradual change of
ian@0 665 * time
ian@0 666 */
ian@0 667 time_interpolator_update(NSEC_PER_SEC);
ian@0 668 time_state = TIME_WAIT;
ian@0 669 clock_was_set();
ian@0 670 printk(KERN_NOTICE "Clock: deleting leap second "
ian@0 671 "23:59:59 UTC\n");
ian@0 672 }
ian@0 673 break;
ian@0 674 case TIME_OOP:
ian@0 675 time_state = TIME_WAIT;
ian@0 676 break;
ian@0 677 case TIME_WAIT:
ian@0 678 if (!(time_status & (STA_INS | STA_DEL)))
ian@0 679 time_state = TIME_OK;
ian@0 680 }
ian@0 681
ian@0 682 /*
ian@0 683 * Compute the phase adjustment for the next second. In PLL mode, the
ian@0 684 * offset is reduced by a fixed factor times the time constant. In FLL
ian@0 685 * mode the offset is used directly. In either mode, the maximum phase
ian@0 686 * adjustment for each second is clamped so as to spread the adjustment
ian@0 687 * over not more than the number of seconds between updates.
ian@0 688 */
ian@0 689 ltemp = time_offset;
ian@0 690 if (!(time_status & STA_FLL))
ian@0 691 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
ian@0 692 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
ian@0 693 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
ian@0 694 time_offset -= ltemp;
ian@0 695 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
ian@0 696
ian@0 697 /*
ian@0 698 * Compute the frequency estimate and additional phase adjustment due
ian@0 699 * to frequency error for the next second.
ian@0 700 */
ian@0 701 ltemp = time_freq;
ian@0 702 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
ian@0 703
ian@0 704 #if HZ == 100
ian@0 705 /*
ian@0 706 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
ian@0 707 * get 128.125; => only 0.125% error (p. 14)
ian@0 708 */
ian@0 709 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
ian@0 710 #endif
ian@0 711 #if HZ == 250
ian@0 712 /*
ian@0 713 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
ian@0 714 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
ian@0 715 */
ian@0 716 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
ian@0 717 #endif
ian@0 718 #if HZ == 1000
ian@0 719 /*
ian@0 720 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
ian@0 721 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
ian@0 722 */
ian@0 723 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
ian@0 724 #endif
ian@0 725 }
ian@0 726
ian@0 727 /*
ian@0 728 * Returns how many microseconds we need to add to xtime this tick
ian@0 729 * in doing an adjustment requested with adjtime.
ian@0 730 */
ian@0 731 static long adjtime_adjustment(void)
ian@0 732 {
ian@0 733 long time_adjust_step;
ian@0 734
ian@0 735 time_adjust_step = time_adjust;
ian@0 736 if (time_adjust_step) {
ian@0 737 /*
ian@0 738 * We are doing an adjtime thing. Prepare time_adjust_step to
ian@0 739 * be within bounds. Note that a positive time_adjust means we
ian@0 740 * want the clock to run faster.
ian@0 741 *
ian@0 742 * Limit the amount of the step to be in the range
ian@0 743 * -tickadj .. +tickadj
ian@0 744 */
ian@0 745 time_adjust_step = min(time_adjust_step, (long)tickadj);
ian@0 746 time_adjust_step = max(time_adjust_step, (long)-tickadj);
ian@0 747 }
ian@0 748 return time_adjust_step;
ian@0 749 }
ian@0 750
ian@0 751 /* in the NTP reference this is called "hardclock()" */
ian@0 752 static void update_ntp_one_tick(void)
ian@0 753 {
ian@0 754 long time_adjust_step;
ian@0 755
ian@0 756 time_adjust_step = adjtime_adjustment();
ian@0 757 if (time_adjust_step)
ian@0 758 /* Reduce by this step the amount of time left */
ian@0 759 time_adjust -= time_adjust_step;
ian@0 760
ian@0 761 /* Changes by adjtime() do not take effect till next tick. */
ian@0 762 if (time_next_adjust != 0) {
ian@0 763 time_adjust = time_next_adjust;
ian@0 764 time_next_adjust = 0;
ian@0 765 }
ian@0 766 }
ian@0 767
ian@0 768 /*
ian@0 769 * Return how long ticks are at the moment, that is, how much time
ian@0 770 * update_wall_time_one_tick will add to xtime next time we call it
ian@0 771 * (assuming no calls to do_adjtimex in the meantime).
ian@0 772 * The return value is in fixed-point nanoseconds shifted by the
ian@0 773 * specified number of bits to the right of the binary point.
ian@0 774 * This function has no side-effects.
ian@0 775 */
ian@0 776 u64 current_tick_length(void)
ian@0 777 {
ian@0 778 long delta_nsec;
ian@0 779 u64 ret;
ian@0 780
ian@0 781 /* calculate the finest interval NTP will allow.
ian@0 782 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
ian@0 783 */
ian@0 784 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
ian@0 785 ret = (u64)delta_nsec << TICK_LENGTH_SHIFT;
ian@0 786 ret += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
ian@0 787
ian@0 788 return ret;
ian@0 789 }
ian@0 790
ian@0 791 /* XXX - all of this timekeeping code should be later moved to time.c */
ian@0 792 #include <linux/clocksource.h>
ian@0 793 static struct clocksource *clock; /* pointer to current clocksource */
ian@0 794
ian@0 795 #ifdef CONFIG_GENERIC_TIME
ian@0 796 /**
ian@0 797 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
ian@0 798 *
ian@0 799 * private function, must hold xtime_lock lock when being
ian@0 800 * called. Returns the number of nanoseconds since the
ian@0 801 * last call to update_wall_time() (adjusted by NTP scaling)
ian@0 802 */
ian@0 803 static inline s64 __get_nsec_offset(void)
ian@0 804 {
ian@0 805 cycle_t cycle_now, cycle_delta;
ian@0 806 s64 ns_offset;
ian@0 807
ian@0 808 /* read clocksource: */
ian@0 809 cycle_now = clocksource_read(clock);
ian@0 810
ian@0 811 /* calculate the delta since the last update_wall_time: */
ian@0 812 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
ian@0 813
ian@0 814 /* convert to nanoseconds: */
ian@0 815 ns_offset = cyc2ns(clock, cycle_delta);
ian@0 816
ian@0 817 return ns_offset;
ian@0 818 }
ian@0 819
ian@0 820 /**
ian@0 821 * __get_realtime_clock_ts - Returns the time of day in a timespec
ian@0 822 * @ts: pointer to the timespec to be set
ian@0 823 *
ian@0 824 * Returns the time of day in a timespec. Used by
ian@0 825 * do_gettimeofday() and get_realtime_clock_ts().
ian@0 826 */
ian@0 827 static inline void __get_realtime_clock_ts(struct timespec *ts)
ian@0 828 {
ian@0 829 unsigned long seq;
ian@0 830 s64 nsecs;
ian@0 831
ian@0 832 do {
ian@0 833 seq = read_seqbegin(&xtime_lock);
ian@0 834
ian@0 835 *ts = xtime;
ian@0 836 nsecs = __get_nsec_offset();
ian@0 837
ian@0 838 } while (read_seqretry(&xtime_lock, seq));
ian@0 839
ian@0 840 timespec_add_ns(ts, nsecs);
ian@0 841 }
ian@0 842
ian@0 843 /**
ian@0 844 * getnstimeofday - Returns the time of day in a timespec
ian@0 845 * @ts: pointer to the timespec to be set
ian@0 846 *
ian@0 847 * Returns the time of day in a timespec.
ian@0 848 */
ian@0 849 void getnstimeofday(struct timespec *ts)
ian@0 850 {
ian@0 851 __get_realtime_clock_ts(ts);
ian@0 852 }
ian@0 853
ian@0 854 EXPORT_SYMBOL(getnstimeofday);
ian@0 855
ian@0 856 /**
ian@0 857 * do_gettimeofday - Returns the time of day in a timeval
ian@0 858 * @tv: pointer to the timeval to be set
ian@0 859 *
ian@0 860 * NOTE: Users should be converted to using get_realtime_clock_ts()
ian@0 861 */
ian@0 862 void do_gettimeofday(struct timeval *tv)
ian@0 863 {
ian@0 864 struct timespec now;
ian@0 865
ian@0 866 __get_realtime_clock_ts(&now);
ian@0 867 tv->tv_sec = now.tv_sec;
ian@0 868 tv->tv_usec = now.tv_nsec/1000;
ian@0 869 }
ian@0 870
ian@0 871 EXPORT_SYMBOL(do_gettimeofday);
ian@0 872 /**
ian@0 873 * do_settimeofday - Sets the time of day
ian@0 874 * @tv: pointer to the timespec variable containing the new time
ian@0 875 *
ian@0 876 * Sets the time of day to the new time and update NTP and notify hrtimers
ian@0 877 */
ian@0 878 int do_settimeofday(struct timespec *tv)
ian@0 879 {
ian@0 880 unsigned long flags;
ian@0 881 time_t wtm_sec, sec = tv->tv_sec;
ian@0 882 long wtm_nsec, nsec = tv->tv_nsec;
ian@0 883
ian@0 884 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
ian@0 885 return -EINVAL;
ian@0 886
ian@0 887 write_seqlock_irqsave(&xtime_lock, flags);
ian@0 888
ian@0 889 nsec -= __get_nsec_offset();
ian@0 890
ian@0 891 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
ian@0 892 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
ian@0 893
ian@0 894 set_normalized_timespec(&xtime, sec, nsec);
ian@0 895 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
ian@0 896
ian@0 897 clock->error = 0;
ian@0 898 ntp_clear();
ian@0 899
ian@0 900 write_sequnlock_irqrestore(&xtime_lock, flags);
ian@0 901
ian@0 902 /* signal hrtimers about time change */
ian@0 903 clock_was_set();
ian@0 904
ian@0 905 return 0;
ian@0 906 }
ian@0 907
ian@0 908 EXPORT_SYMBOL(do_settimeofday);
ian@0 909
ian@0 910 /**
ian@0 911 * change_clocksource - Swaps clocksources if a new one is available
ian@0 912 *
ian@0 913 * Accumulates current time interval and initializes new clocksource
ian@0 914 */
ian@0 915 static int change_clocksource(void)
ian@0 916 {
ian@0 917 struct clocksource *new;
ian@0 918 cycle_t now;
ian@0 919 u64 nsec;
ian@0 920 new = clocksource_get_next();
ian@0 921 if (clock != new) {
ian@0 922 now = clocksource_read(new);
ian@0 923 nsec = __get_nsec_offset();
ian@0 924 timespec_add_ns(&xtime, nsec);
ian@0 925
ian@0 926 clock = new;
ian@0 927 clock->cycle_last = now;
ian@0 928 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
ian@0 929 clock->name);
ian@0 930 return 1;
ian@0 931 } else if (clock->update_callback) {
ian@0 932 return clock->update_callback();
ian@0 933 }
ian@0 934 return 0;
ian@0 935 }
ian@0 936 #else
ian@0 937 #define change_clocksource() (0)
ian@0 938 #endif
ian@0 939
ian@0 940 /**
ian@0 941 * timeofday_is_continuous - check to see if timekeeping is free running
ian@0 942 */
ian@0 943 int timekeeping_is_continuous(void)
ian@0 944 {
ian@0 945 unsigned long seq;
ian@0 946 int ret;
ian@0 947
ian@0 948 do {
ian@0 949 seq = read_seqbegin(&xtime_lock);
ian@0 950
ian@0 951 ret = clock->is_continuous;
ian@0 952
ian@0 953 } while (read_seqretry(&xtime_lock, seq));
ian@0 954
ian@0 955 return ret;
ian@0 956 }
ian@0 957
ian@0 958 /*
ian@0 959 * timekeeping_init - Initializes the clocksource and common timekeeping values
ian@0 960 */
ian@0 961 void __init timekeeping_init(void)
ian@0 962 {
ian@0 963 unsigned long flags;
ian@0 964
ian@0 965 write_seqlock_irqsave(&xtime_lock, flags);
ian@0 966 clock = clocksource_get_next();
ian@0 967 clocksource_calculate_interval(clock, tick_nsec);
ian@0 968 clock->cycle_last = clocksource_read(clock);
ian@0 969 ntp_clear();
ian@0 970 write_sequnlock_irqrestore(&xtime_lock, flags);
ian@0 971 }
ian@0 972
ian@0 973
ian@0 974 static int timekeeping_suspended;
ian@0 975 /*
ian@0 976 * timekeeping_resume - Resumes the generic timekeeping subsystem.
ian@0 977 * @dev: unused
ian@0 978 *
ian@0 979 * This is for the generic clocksource timekeeping.
ian@0 980 * xtime/wall_to_monotonic/jiffies/wall_jiffies/etc are
ian@0 981 * still managed by arch specific suspend/resume code.
ian@0 982 */
ian@0 983 static int timekeeping_resume(struct sys_device *dev)
ian@0 984 {
ian@0 985 unsigned long flags;
ian@0 986
ian@0 987 write_seqlock_irqsave(&xtime_lock, flags);
ian@0 988 /* restart the last cycle value */
ian@0 989 clock->cycle_last = clocksource_read(clock);
ian@0 990 clock->error = 0;
ian@0 991 timekeeping_suspended = 0;
ian@0 992 write_sequnlock_irqrestore(&xtime_lock, flags);
ian@0 993 return 0;
ian@0 994 }
ian@0 995
ian@0 996 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
ian@0 997 {
ian@0 998 unsigned long flags;
ian@0 999
ian@0 1000 write_seqlock_irqsave(&xtime_lock, flags);
ian@0 1001 timekeeping_suspended = 1;
ian@0 1002 write_sequnlock_irqrestore(&xtime_lock, flags);
ian@0 1003 return 0;
ian@0 1004 }
ian@0 1005
ian@0 1006 /* sysfs resume/suspend bits for timekeeping */
ian@0 1007 static struct sysdev_class timekeeping_sysclass = {
ian@0 1008 .resume = timekeeping_resume,
ian@0 1009 .suspend = timekeeping_suspend,
ian@0 1010 set_kset_name("timekeeping"),
ian@0 1011 };
ian@0 1012
ian@0 1013 static struct sys_device device_timer = {
ian@0 1014 .id = 0,
ian@0 1015 .cls = &timekeeping_sysclass,
ian@0 1016 };
ian@0 1017
ian@0 1018 static int __init timekeeping_init_device(void)
ian@0 1019 {
ian@0 1020 int error = sysdev_class_register(&timekeeping_sysclass);
ian@0 1021 if (!error)
ian@0 1022 error = sysdev_register(&device_timer);
ian@0 1023 return error;
ian@0 1024 }
ian@0 1025
ian@0 1026 device_initcall(timekeeping_init_device);
ian@0 1027
ian@0 1028 /*
ian@0 1029 * If the error is already larger, we look ahead even further
ian@0 1030 * to compensate for late or lost adjustments.
ian@0 1031 */
ian@0 1032 static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, s64 *offset)
ian@0 1033 {
ian@0 1034 s64 tick_error, i;
ian@0 1035 u32 look_ahead, adj;
ian@0 1036 s32 error2, mult;
ian@0 1037
ian@0 1038 /*
ian@0 1039 * Use the current error value to determine how much to look ahead.
ian@0 1040 * The larger the error the slower we adjust for it to avoid problems
ian@0 1041 * with losing too many ticks, otherwise we would overadjust and
ian@0 1042 * produce an even larger error. The smaller the adjustment the
ian@0 1043 * faster we try to adjust for it, as lost ticks can do less harm
ian@0 1044 * here. This is tuned so that an error of about 1 msec is adusted
ian@0 1045 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
ian@0 1046 */
ian@0 1047 error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
ian@0 1048 error2 = abs(error2);
ian@0 1049 for (look_ahead = 0; error2 > 0; look_ahead++)
ian@0 1050 error2 >>= 2;
ian@0 1051
ian@0 1052 /*
ian@0 1053 * Now calculate the error in (1 << look_ahead) ticks, but first
ian@0 1054 * remove the single look ahead already included in the error.
ian@0 1055 */
ian@0 1056 tick_error = current_tick_length() >> (TICK_LENGTH_SHIFT - clock->shift + 1);
ian@0 1057 tick_error -= clock->xtime_interval >> 1;
ian@0 1058 error = ((error - tick_error) >> look_ahead) + tick_error;
ian@0 1059
ian@0 1060 /* Finally calculate the adjustment shift value. */
ian@0 1061 i = *interval;
ian@0 1062 mult = 1;
ian@0 1063 if (error < 0) {
ian@0 1064 error = -error;
ian@0 1065 *interval = -*interval;
ian@0 1066 *offset = -*offset;
ian@0 1067 mult = -1;
ian@0 1068 }
ian@0 1069 for (adj = 0; error > i; adj++)
ian@0 1070 error >>= 1;
ian@0 1071
ian@0 1072 *interval <<= adj;
ian@0 1073 *offset <<= adj;
ian@0 1074 return mult << adj;
ian@0 1075 }
ian@0 1076
ian@0 1077 /*
ian@0 1078 * Adjust the multiplier to reduce the error value,
ian@0 1079 * this is optimized for the most common adjustments of -1,0,1,
ian@0 1080 * for other values we can do a bit more work.
ian@0 1081 */
ian@0 1082 static void clocksource_adjust(struct clocksource *clock, s64 offset)
ian@0 1083 {
ian@0 1084 s64 error, interval = clock->cycle_interval;
ian@0 1085 int adj;
ian@0 1086
ian@0 1087 error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
ian@0 1088 if (error > interval) {
ian@0 1089 error >>= 2;
ian@0 1090 if (likely(error <= interval))
ian@0 1091 adj = 1;
ian@0 1092 else
ian@0 1093 adj = clocksource_bigadjust(error, &interval, &offset);
ian@0 1094 } else if (error < -interval) {
ian@0 1095 error >>= 2;
ian@0 1096 if (likely(error >= -interval)) {
ian@0 1097 adj = -1;
ian@0 1098 interval = -interval;
ian@0 1099 offset = -offset;
ian@0 1100 } else
ian@0 1101 adj = clocksource_bigadjust(error, &interval, &offset);
ian@0 1102 } else
ian@0 1103 return;
ian@0 1104
ian@0 1105 clock->mult += adj;
ian@0 1106 clock->xtime_interval += interval;
ian@0 1107 clock->xtime_nsec -= offset;
ian@0 1108 clock->error -= (interval - offset) << (TICK_LENGTH_SHIFT - clock->shift);
ian@0 1109 }
ian@0 1110
ian@0 1111 /*
ian@0 1112 * update_wall_time - Uses the current clocksource to increment the wall time
ian@0 1113 *
ian@0 1114 * Called from the timer interrupt, must hold a write on xtime_lock.
ian@0 1115 */
ian@0 1116 static void update_wall_time(void)
ian@0 1117 {
ian@0 1118 cycle_t offset;
ian@0 1119
ian@0 1120 /* Make sure we're fully resumed: */
ian@0 1121 if (unlikely(timekeeping_suspended))
ian@0 1122 return;
ian@0 1123
ian@0 1124 #ifdef CONFIG_GENERIC_TIME
ian@0 1125 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
ian@0 1126 #else
ian@0 1127 offset = clock->cycle_interval;
ian@0 1128 #endif
ian@0 1129 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
ian@0 1130
ian@0 1131 /* normally this loop will run just once, however in the
ian@0 1132 * case of lost or late ticks, it will accumulate correctly.
ian@0 1133 */
ian@0 1134 while (offset >= clock->cycle_interval) {
ian@0 1135 /* accumulate one interval */
ian@0 1136 clock->xtime_nsec += clock->xtime_interval;
ian@0 1137 clock->cycle_last += clock->cycle_interval;
ian@0 1138 offset -= clock->cycle_interval;
ian@0 1139
ian@0 1140 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
ian@0 1141 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
ian@0 1142 xtime.tv_sec++;
ian@0 1143 second_overflow();
ian@0 1144 }
ian@0 1145
ian@0 1146 /* interpolator bits */
ian@0 1147 time_interpolator_update(clock->xtime_interval
ian@0 1148 >> clock->shift);
ian@0 1149 /* increment the NTP state machine */
ian@0 1150 update_ntp_one_tick();
ian@0 1151
ian@0 1152 /* accumulate error between NTP and clock interval */
ian@0 1153 clock->error += current_tick_length();
ian@0 1154 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
ian@0 1155 }
ian@0 1156
ian@0 1157 /* correct the clock when NTP error is too big */
ian@0 1158 clocksource_adjust(clock, offset);
ian@0 1159
ian@0 1160 /* store full nanoseconds into xtime */
ian@0 1161 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
ian@0 1162 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
ian@0 1163
ian@0 1164 /* check to see if there is a new clocksource to use */
ian@0 1165 if (change_clocksource()) {
ian@0 1166 clock->error = 0;
ian@0 1167 clock->xtime_nsec = 0;
ian@0 1168 clocksource_calculate_interval(clock, tick_nsec);
ian@0 1169 }
ian@0 1170 }
ian@0 1171
ian@0 1172 /*
ian@0 1173 * Called from the timer interrupt handler to charge one tick to the current
ian@0 1174 * process. user_tick is 1 if the tick is user time, 0 for system.
ian@0 1175 */
ian@0 1176 void update_process_times(int user_tick)
ian@0 1177 {
ian@0 1178 struct task_struct *p = current;
ian@0 1179 int cpu = smp_processor_id();
ian@0 1180
ian@0 1181 /* Note: this timer irq context must be accounted for as well. */
ian@0 1182 if (user_tick)
ian@0 1183 account_user_time(p, jiffies_to_cputime(1));
ian@0 1184 else
ian@0 1185 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
ian@0 1186 run_local_timers();
ian@0 1187 if (rcu_pending(cpu))
ian@0 1188 rcu_check_callbacks(cpu, user_tick);
ian@0 1189 scheduler_tick();
ian@0 1190 run_posix_cpu_timers(p);
ian@0 1191 }
ian@0 1192
ian@0 1193 /*
ian@0 1194 * Nr of active tasks - counted in fixed-point numbers
ian@0 1195 */
ian@0 1196 static unsigned long count_active_tasks(void)
ian@0 1197 {
ian@0 1198 return nr_active() * FIXED_1;
ian@0 1199 }
ian@0 1200
ian@0 1201 /*
ian@0 1202 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
ian@0 1203 * imply that avenrun[] is the standard name for this kind of thing.
ian@0 1204 * Nothing else seems to be standardized: the fractional size etc
ian@0 1205 * all seem to differ on different machines.
ian@0 1206 *
ian@0 1207 * Requires xtime_lock to access.
ian@0 1208 */
ian@0 1209 unsigned long avenrun[3];
ian@0 1210
ian@0 1211 EXPORT_SYMBOL(avenrun);
ian@0 1212
ian@0 1213 /*
ian@0 1214 * calc_load - given tick count, update the avenrun load estimates.
ian@0 1215 * This is called while holding a write_lock on xtime_lock.
ian@0 1216 */
ian@0 1217 static inline void calc_load(unsigned long ticks)
ian@0 1218 {
ian@0 1219 unsigned long active_tasks; /* fixed-point */
ian@0 1220 static int count = LOAD_FREQ;
ian@0 1221
ian@0 1222 count -= ticks;
ian@0 1223 if (count < 0) {
ian@0 1224 count += LOAD_FREQ;
ian@0 1225 active_tasks = count_active_tasks();
ian@0 1226 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
ian@0 1227 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
ian@0 1228 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
ian@0 1229 }
ian@0 1230 }
ian@0 1231
ian@0 1232 /* jiffies at the most recent update of wall time */
ian@0 1233 unsigned long wall_jiffies = INITIAL_JIFFIES;
ian@0 1234
ian@0 1235 /*
ian@0 1236 * This read-write spinlock protects us from races in SMP while
ian@0 1237 * playing with xtime and avenrun.
ian@0 1238 */
ian@0 1239 #ifndef ARCH_HAVE_XTIME_LOCK
ian@0 1240 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
ian@0 1241
ian@0 1242 EXPORT_SYMBOL(xtime_lock);
ian@0 1243 #endif
ian@0 1244
ian@0 1245 /*
ian@0 1246 * This function runs timers and the timer-tq in bottom half context.
ian@0 1247 */
ian@0 1248 static void run_timer_softirq(struct softirq_action *h)
ian@0 1249 {
ian@0 1250 tvec_base_t *base = __get_cpu_var(tvec_bases);
ian@0 1251
ian@0 1252 hrtimer_run_queues();
ian@0 1253 if (time_after_eq(jiffies, base->timer_jiffies))
ian@0 1254 __run_timers(base);
ian@0 1255 }
ian@0 1256
ian@0 1257 /*
ian@0 1258 * Called by the local, per-CPU timer interrupt on SMP.
ian@0 1259 */
ian@0 1260 void run_local_timers(void)
ian@0 1261 {
ian@0 1262 raise_softirq(TIMER_SOFTIRQ);
ian@0 1263 softlockup_tick();
ian@0 1264 }
ian@0 1265
ian@0 1266 /*
ian@0 1267 * Called by the timer interrupt. xtime_lock must already be taken
ian@0 1268 * by the timer IRQ!
ian@0 1269 */
ian@0 1270 static inline void update_times(void)
ian@0 1271 {
ian@0 1272 unsigned long ticks;
ian@0 1273
ian@0 1274 ticks = jiffies - wall_jiffies;
ian@0 1275 wall_jiffies += ticks;
ian@0 1276 update_wall_time();
ian@0 1277 calc_load(ticks);
ian@0 1278 }
ian@0 1279
ian@0 1280 /*
ian@0 1281 * The 64-bit jiffies value is not atomic - you MUST NOT read it
ian@0 1282 * without sampling the sequence number in xtime_lock.
ian@0 1283 * jiffies is defined in the linker script...
ian@0 1284 */
ian@0 1285
ian@0 1286 void do_timer(struct pt_regs *regs)
ian@0 1287 {
ian@0 1288 jiffies_64++;
ian@0 1289 /* prevent loading jiffies before storing new jiffies_64 value. */
ian@0 1290 barrier();
ian@0 1291 update_times();
ian@0 1292 }
ian@0 1293
ian@0 1294 #ifdef __ARCH_WANT_SYS_ALARM
ian@0 1295
ian@0 1296 /*
ian@0 1297 * For backwards compatibility? This can be done in libc so Alpha
ian@0 1298 * and all newer ports shouldn't need it.
ian@0 1299 */
ian@0 1300 asmlinkage unsigned long sys_alarm(unsigned int seconds)
ian@0 1301 {
ian@0 1302 return alarm_setitimer(seconds);
ian@0 1303 }
ian@0 1304
ian@0 1305 #endif
ian@0 1306
ian@0 1307 #ifndef __alpha__
ian@0 1308
ian@0 1309 /*
ian@0 1310 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
ian@0 1311 * should be moved into arch/i386 instead?
ian@0 1312 */
ian@0 1313
ian@0 1314 /**
ian@0 1315 * sys_getpid - return the thread group id of the current process
ian@0 1316 *
ian@0 1317 * Note, despite the name, this returns the tgid not the pid. The tgid and
ian@0 1318 * the pid are identical unless CLONE_THREAD was specified on clone() in
ian@0 1319 * which case the tgid is the same in all threads of the same group.
ian@0 1320 *
ian@0 1321 * This is SMP safe as current->tgid does not change.
ian@0 1322 */
ian@0 1323 asmlinkage long sys_getpid(void)
ian@0 1324 {
ian@0 1325 return current->tgid;
ian@0 1326 }
ian@0 1327
ian@0 1328 /*
ian@0 1329 * Accessing ->real_parent is not SMP-safe, it could
ian@0 1330 * change from under us. However, we can use a stale
ian@0 1331 * value of ->real_parent under rcu_read_lock(), see
ian@0 1332 * release_task()->call_rcu(delayed_put_task_struct).
ian@0 1333 */
ian@0 1334 asmlinkage long sys_getppid(void)
ian@0 1335 {
ian@0 1336 int pid;
ian@0 1337
ian@0 1338 rcu_read_lock();
ian@0 1339 pid = rcu_dereference(current->real_parent)->tgid;
ian@0 1340 rcu_read_unlock();
ian@0 1341
ian@0 1342 return pid;
ian@0 1343 }
ian@0 1344
ian@0 1345 asmlinkage long sys_getuid(void)
ian@0 1346 {
ian@0 1347 /* Only we change this so SMP safe */
ian@0 1348 return current->uid;
ian@0 1349 }
ian@0 1350
ian@0 1351 asmlinkage long sys_geteuid(void)
ian@0 1352 {
ian@0 1353 /* Only we change this so SMP safe */
ian@0 1354 return current->euid;
ian@0 1355 }
ian@0 1356
ian@0 1357 asmlinkage long sys_getgid(void)
ian@0 1358 {
ian@0 1359 /* Only we change this so SMP safe */
ian@0 1360 return current->gid;
ian@0 1361 }
ian@0 1362
ian@0 1363 asmlinkage long sys_getegid(void)
ian@0 1364 {
ian@0 1365 /* Only we change this so SMP safe */
ian@0 1366 return current->egid;
ian@0 1367 }
ian@0 1368
ian@0 1369 #endif
ian@0 1370
ian@0 1371 static void process_timeout(unsigned long __data)
ian@0 1372 {
ian@0 1373 wake_up_process((struct task_struct *)__data);
ian@0 1374 }
ian@0 1375
ian@0 1376 /**
ian@0 1377 * schedule_timeout - sleep until timeout
ian@0 1378 * @timeout: timeout value in jiffies
ian@0 1379 *
ian@0 1380 * Make the current task sleep until @timeout jiffies have
ian@0 1381 * elapsed. The routine will return immediately unless
ian@0 1382 * the current task state has been set (see set_current_state()).
ian@0 1383 *
ian@0 1384 * You can set the task state as follows -
ian@0 1385 *
ian@0 1386 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
ian@0 1387 * pass before the routine returns. The routine will return 0
ian@0 1388 *
ian@0 1389 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
ian@0 1390 * delivered to the current task. In this case the remaining time
ian@0 1391 * in jiffies will be returned, or 0 if the timer expired in time
ian@0 1392 *
ian@0 1393 * The current task state is guaranteed to be TASK_RUNNING when this
ian@0 1394 * routine returns.
ian@0 1395 *
ian@0 1396 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
ian@0 1397 * the CPU away without a bound on the timeout. In this case the return
ian@0 1398 * value will be %MAX_SCHEDULE_TIMEOUT.
ian@0 1399 *
ian@0 1400 * In all cases the return value is guaranteed to be non-negative.
ian@0 1401 */
ian@0 1402 fastcall signed long __sched schedule_timeout(signed long timeout)
ian@0 1403 {
ian@0 1404 struct timer_list timer;
ian@0 1405 unsigned long expire;
ian@0 1406
ian@0 1407 switch (timeout)
ian@0 1408 {
ian@0 1409 case MAX_SCHEDULE_TIMEOUT:
ian@0 1410 /*
ian@0 1411 * These two special cases are useful to be comfortable
ian@0 1412 * in the caller. Nothing more. We could take
ian@0 1413 * MAX_SCHEDULE_TIMEOUT from one of the negative value
ian@0 1414 * but I' d like to return a valid offset (>=0) to allow
ian@0 1415 * the caller to do everything it want with the retval.
ian@0 1416 */
ian@0 1417 schedule();
ian@0 1418 goto out;
ian@0 1419 default:
ian@0 1420 /*
ian@0 1421 * Another bit of PARANOID. Note that the retval will be
ian@0 1422 * 0 since no piece of kernel is supposed to do a check
ian@0 1423 * for a negative retval of schedule_timeout() (since it
ian@0 1424 * should never happens anyway). You just have the printk()
ian@0 1425 * that will tell you if something is gone wrong and where.
ian@0 1426 */
ian@0 1427 if (timeout < 0)
ian@0 1428 {
ian@0 1429 printk(KERN_ERR "schedule_timeout: wrong timeout "
ian@0 1430 "value %lx from %p\n", timeout,
ian@0 1431 __builtin_return_address(0));
ian@0 1432 current->state = TASK_RUNNING;
ian@0 1433 goto out;
ian@0 1434 }
ian@0 1435 }
ian@0 1436
ian@0 1437 expire = timeout + jiffies;
ian@0 1438
ian@0 1439 setup_timer(&timer, process_timeout, (unsigned long)current);
ian@0 1440 __mod_timer(&timer, expire);
ian@0 1441 schedule();
ian@0 1442 del_singleshot_timer_sync(&timer);
ian@0 1443
ian@0 1444 timeout = expire - jiffies;
ian@0 1445
ian@0 1446 out:
ian@0 1447 return timeout < 0 ? 0 : timeout;
ian@0 1448 }
ian@0 1449 EXPORT_SYMBOL(schedule_timeout);
ian@0 1450
ian@0 1451 /*
ian@0 1452 * We can use __set_current_state() here because schedule_timeout() calls
ian@0 1453 * schedule() unconditionally.
ian@0 1454 */
ian@0 1455 signed long __sched schedule_timeout_interruptible(signed long timeout)
ian@0 1456 {
ian@0 1457 __set_current_state(TASK_INTERRUPTIBLE);
ian@0 1458 return schedule_timeout(timeout);
ian@0 1459 }
ian@0 1460 EXPORT_SYMBOL(schedule_timeout_interruptible);
ian@0 1461
ian@0 1462 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
ian@0 1463 {
ian@0 1464 __set_current_state(TASK_UNINTERRUPTIBLE);
ian@0 1465 return schedule_timeout(timeout);
ian@0 1466 }
ian@0 1467 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
ian@0 1468
ian@0 1469 /* Thread ID - the internal kernel "pid" */
ian@0 1470 asmlinkage long sys_gettid(void)
ian@0 1471 {
ian@0 1472 return current->pid;
ian@0 1473 }
ian@0 1474
ian@0 1475 /*
ian@0 1476 * sys_sysinfo - fill in sysinfo struct
ian@0 1477 */
ian@0 1478 asmlinkage long sys_sysinfo(struct sysinfo __user *info)
ian@0 1479 {
ian@0 1480 struct sysinfo val;
ian@0 1481 unsigned long mem_total, sav_total;
ian@0 1482 unsigned int mem_unit, bitcount;
ian@0 1483 unsigned long seq;
ian@0 1484
ian@0 1485 memset((char *)&val, 0, sizeof(struct sysinfo));
ian@0 1486
ian@0 1487 do {
ian@0 1488 struct timespec tp;
ian@0 1489 seq = read_seqbegin(&xtime_lock);
ian@0 1490
ian@0 1491 /*
ian@0 1492 * This is annoying. The below is the same thing
ian@0 1493 * posix_get_clock_monotonic() does, but it wants to
ian@0 1494 * take the lock which we want to cover the loads stuff
ian@0 1495 * too.
ian@0 1496 */
ian@0 1497
ian@0 1498 getnstimeofday(&tp);
ian@0 1499 tp.tv_sec += wall_to_monotonic.tv_sec;
ian@0 1500 tp.tv_nsec += wall_to_monotonic.tv_nsec;
ian@0 1501 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
ian@0 1502 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
ian@0 1503 tp.tv_sec++;
ian@0 1504 }
ian@0 1505 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
ian@0 1506
ian@0 1507 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
ian@0 1508 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
ian@0 1509 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
ian@0 1510
ian@0 1511 val.procs = nr_threads;
ian@0 1512 } while (read_seqretry(&xtime_lock, seq));
ian@0 1513
ian@0 1514 si_meminfo(&val);
ian@0 1515 si_swapinfo(&val);
ian@0 1516
ian@0 1517 /*
ian@0 1518 * If the sum of all the available memory (i.e. ram + swap)
ian@0 1519 * is less than can be stored in a 32 bit unsigned long then
ian@0 1520 * we can be binary compatible with 2.2.x kernels. If not,
ian@0 1521 * well, in that case 2.2.x was broken anyways...
ian@0 1522 *
ian@0 1523 * -Erik Andersen <andersee@debian.org>
ian@0 1524 */
ian@0 1525
ian@0 1526 mem_total = val.totalram + val.totalswap;
ian@0 1527 if (mem_total < val.totalram || mem_total < val.totalswap)
ian@0 1528 goto out;
ian@0 1529 bitcount = 0;
ian@0 1530 mem_unit = val.mem_unit;
ian@0 1531 while (mem_unit > 1) {
ian@0 1532 bitcount++;
ian@0 1533 mem_unit >>= 1;
ian@0 1534 sav_total = mem_total;
ian@0 1535 mem_total <<= 1;
ian@0 1536 if (mem_total < sav_total)
ian@0 1537 goto out;
ian@0 1538 }
ian@0 1539
ian@0 1540 /*
ian@0 1541 * If mem_total did not overflow, multiply all memory values by
ian@0 1542 * val.mem_unit and set it to 1. This leaves things compatible
ian@0 1543 * with 2.2.x, and also retains compatibility with earlier 2.4.x
ian@0 1544 * kernels...
ian@0 1545 */
ian@0 1546
ian@0 1547 val.mem_unit = 1;
ian@0 1548 val.totalram <<= bitcount;
ian@0 1549 val.freeram <<= bitcount;
ian@0 1550 val.sharedram <<= bitcount;
ian@0 1551 val.bufferram <<= bitcount;
ian@0 1552 val.totalswap <<= bitcount;
ian@0 1553 val.freeswap <<= bitcount;
ian@0 1554 val.totalhigh <<= bitcount;
ian@0 1555 val.freehigh <<= bitcount;
ian@0 1556
ian@0 1557 out:
ian@0 1558 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
ian@0 1559 return -EFAULT;
ian@0 1560
ian@0 1561 return 0;
ian@0 1562 }
ian@0 1563
ian@0 1564 /*
ian@0 1565 * lockdep: we want to track each per-CPU base as a separate lock-class,
ian@0 1566 * but timer-bases are kmalloc()-ed, so we need to attach separate
ian@0 1567 * keys to them:
ian@0 1568 */
ian@0 1569 static struct lock_class_key base_lock_keys[NR_CPUS];
ian@0 1570
ian@0 1571 static int __devinit init_timers_cpu(int cpu)
ian@0 1572 {
ian@0 1573 int j;
ian@0 1574 tvec_base_t *base;
ian@0 1575 static char __devinitdata tvec_base_done[NR_CPUS];
ian@0 1576
ian@0 1577 if (!tvec_base_done[cpu]) {
ian@0 1578 static char boot_done;
ian@0 1579
ian@0 1580 if (boot_done) {
ian@0 1581 /*
ian@0 1582 * The APs use this path later in boot
ian@0 1583 */
ian@0 1584 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
ian@0 1585 cpu_to_node(cpu));
ian@0 1586 if (!base)
ian@0 1587 return -ENOMEM;
ian@0 1588 memset(base, 0, sizeof(*base));
ian@0 1589 per_cpu(tvec_bases, cpu) = base;
ian@0 1590 } else {
ian@0 1591 /*
ian@0 1592 * This is for the boot CPU - we use compile-time
ian@0 1593 * static initialisation because per-cpu memory isn't
ian@0 1594 * ready yet and because the memory allocators are not
ian@0 1595 * initialised either.
ian@0 1596 */
ian@0 1597 boot_done = 1;
ian@0 1598 base = &boot_tvec_bases;
ian@0 1599 }
ian@0 1600 tvec_base_done[cpu] = 1;
ian@0 1601 } else {
ian@0 1602 base = per_cpu(tvec_bases, cpu);
ian@0 1603 }
ian@0 1604
ian@0 1605 spin_lock_init(&base->lock);
ian@0 1606 lockdep_set_class(&base->lock, base_lock_keys + cpu);
ian@0 1607
ian@0 1608 for (j = 0; j < TVN_SIZE; j++) {
ian@0 1609 INIT_LIST_HEAD(base->tv5.vec + j);
ian@0 1610 INIT_LIST_HEAD(base->tv4.vec + j);
ian@0 1611 INIT_LIST_HEAD(base->tv3.vec + j);
ian@0 1612 INIT_LIST_HEAD(base->tv2.vec + j);
ian@0 1613 }
ian@0 1614 for (j = 0; j < TVR_SIZE; j++)
ian@0 1615 INIT_LIST_HEAD(base->tv1.vec + j);
ian@0 1616
ian@0 1617 base->timer_jiffies = jiffies;
ian@0 1618 return 0;
ian@0 1619 }
ian@0 1620
ian@0 1621 #ifdef CONFIG_HOTPLUG_CPU
ian@0 1622 static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
ian@0 1623 {
ian@0 1624 struct timer_list *timer;
ian@0 1625
ian@0 1626 while (!list_empty(head)) {
ian@0 1627 timer = list_entry(head->next, struct timer_list, entry);
ian@0 1628 detach_timer(timer, 0);
ian@0 1629 timer->base = new_base;
ian@0 1630 internal_add_timer(new_base, timer);
ian@0 1631 }
ian@0 1632 }
ian@0 1633
ian@0 1634 static void __devinit migrate_timers(int cpu)
ian@0 1635 {
ian@0 1636 tvec_base_t *old_base;
ian@0 1637 tvec_base_t *new_base;
ian@0 1638 int i;
ian@0 1639
ian@0 1640 BUG_ON(cpu_online(cpu));
ian@0 1641 old_base = per_cpu(tvec_bases, cpu);
ian@0 1642 new_base = get_cpu_var(tvec_bases);
ian@0 1643
ian@0 1644 local_irq_disable();
ian@0 1645 spin_lock(&new_base->lock);
ian@0 1646 spin_lock(&old_base->lock);
ian@0 1647
ian@0 1648 BUG_ON(old_base->running_timer);
ian@0 1649
ian@0 1650 for (i = 0; i < TVR_SIZE; i++)
ian@0 1651 migrate_timer_list(new_base, old_base->tv1.vec + i);
ian@0 1652 for (i = 0; i < TVN_SIZE; i++) {
ian@0 1653 migrate_timer_list(new_base, old_base->tv2.vec + i);
ian@0 1654 migrate_timer_list(new_base, old_base->tv3.vec + i);
ian@0 1655 migrate_timer_list(new_base, old_base->tv4.vec + i);
ian@0 1656 migrate_timer_list(new_base, old_base->tv5.vec + i);
ian@0 1657 }
ian@0 1658
ian@0 1659 spin_unlock(&old_base->lock);
ian@0 1660 spin_unlock(&new_base->lock);
ian@0 1661 local_irq_enable();
ian@0 1662 put_cpu_var(tvec_bases);
ian@0 1663 }
ian@0 1664 #endif /* CONFIG_HOTPLUG_CPU */
ian@0 1665
ian@0 1666 static int __cpuinit timer_cpu_notify(struct notifier_block *self,
ian@0 1667 unsigned long action, void *hcpu)
ian@0 1668 {
ian@0 1669 long cpu = (long)hcpu;
ian@0 1670 switch(action) {
ian@0 1671 case CPU_UP_PREPARE:
ian@0 1672 if (init_timers_cpu(cpu) < 0)
ian@0 1673 return NOTIFY_BAD;
ian@0 1674 break;
ian@0 1675 #ifdef CONFIG_HOTPLUG_CPU
ian@0 1676 case CPU_DEAD:
ian@0 1677 migrate_timers(cpu);
ian@0 1678 break;
ian@0 1679 #endif
ian@0 1680 default:
ian@0 1681 break;
ian@0 1682 }
ian@0 1683 return NOTIFY_OK;
ian@0 1684 }
ian@0 1685
ian@0 1686 static struct notifier_block __cpuinitdata timers_nb = {
ian@0 1687 .notifier_call = timer_cpu_notify,
ian@0 1688 };
ian@0 1689
ian@0 1690
ian@0 1691 void __init init_timers(void)
ian@0 1692 {
ian@0 1693 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
ian@0 1694 (void *)(long)smp_processor_id());
ian@0 1695 register_cpu_notifier(&timers_nb);
ian@0 1696 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
ian@0 1697 }
ian@0 1698
ian@0 1699 #ifdef CONFIG_TIME_INTERPOLATION
ian@0 1700
ian@0 1701 struct time_interpolator *time_interpolator __read_mostly;
ian@0 1702 static struct time_interpolator *time_interpolator_list __read_mostly;
ian@0 1703 static DEFINE_SPINLOCK(time_interpolator_lock);
ian@0 1704
ian@0 1705 static inline u64 time_interpolator_get_cycles(unsigned int src)
ian@0 1706 {
ian@0 1707 unsigned long (*x)(void);
ian@0 1708
ian@0 1709 switch (src)
ian@0 1710 {
ian@0 1711 case TIME_SOURCE_FUNCTION:
ian@0 1712 x = time_interpolator->addr;
ian@0 1713 return x();
ian@0 1714
ian@0 1715 case TIME_SOURCE_MMIO64 :
ian@0 1716 return readq_relaxed((void __iomem *)time_interpolator->addr);
ian@0 1717
ian@0 1718 case TIME_SOURCE_MMIO32 :
ian@0 1719 return readl_relaxed((void __iomem *)time_interpolator->addr);
ian@0 1720
ian@0 1721 default: return get_cycles();
ian@0 1722 }
ian@0 1723 }
ian@0 1724
ian@0 1725 static inline u64 time_interpolator_get_counter(int writelock)
ian@0 1726 {
ian@0 1727 unsigned int src = time_interpolator->source;
ian@0 1728
ian@0 1729 if (time_interpolator->jitter)
ian@0 1730 {
ian@0 1731 u64 lcycle;
ian@0 1732 u64 now;
ian@0 1733
ian@0 1734 do {
ian@0 1735 lcycle = time_interpolator->last_cycle;
ian@0 1736 now = time_interpolator_get_cycles(src);
ian@0 1737 if (lcycle && time_after(lcycle, now))
ian@0 1738 return lcycle;
ian@0 1739
ian@0 1740 /* When holding the xtime write lock, there's no need
ian@0 1741 * to add the overhead of the cmpxchg. Readers are
ian@0 1742 * force to retry until the write lock is released.
ian@0 1743 */
ian@0 1744 if (writelock) {
ian@0 1745 time_interpolator->last_cycle = now;
ian@0 1746 return now;
ian@0 1747 }
ian@0 1748 /* Keep track of the last timer value returned. The use of cmpxchg here
ian@0 1749 * will cause contention in an SMP environment.
ian@0 1750 */
ian@0 1751 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
ian@0 1752 return now;
ian@0 1753 }
ian@0 1754 else
ian@0 1755 return time_interpolator_get_cycles(src);
ian@0 1756 }
ian@0 1757
ian@0 1758 void time_interpolator_reset(void)
ian@0 1759 {
ian@0 1760 time_interpolator->offset = 0;
ian@0 1761 time_interpolator->last_counter = time_interpolator_get_counter(1);
ian@0 1762 }
ian@0 1763
ian@0 1764 #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
ian@0 1765
ian@0 1766 unsigned long time_interpolator_get_offset(void)
ian@0 1767 {
ian@0 1768 /* If we do not have a time interpolator set up then just return zero */
ian@0 1769 if (!time_interpolator)
ian@0 1770 return 0;
ian@0 1771
ian@0 1772 return time_interpolator->offset +
ian@0 1773 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
ian@0 1774 }
ian@0 1775
ian@0 1776 #define INTERPOLATOR_ADJUST 65536
ian@0 1777 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
ian@0 1778
ian@0 1779 static void time_interpolator_update(long delta_nsec)
ian@0 1780 {
ian@0 1781 u64 counter;
ian@0 1782 unsigned long offset;
ian@0 1783
ian@0 1784 /* If there is no time interpolator set up then do nothing */
ian@0 1785 if (!time_interpolator)
ian@0 1786 return;
ian@0 1787
ian@0 1788 /*
ian@0 1789 * The interpolator compensates for late ticks by accumulating the late
ian@0 1790 * time in time_interpolator->offset. A tick earlier than expected will
ian@0 1791 * lead to a reset of the offset and a corresponding jump of the clock
ian@0 1792 * forward. Again this only works if the interpolator clock is running
ian@0 1793 * slightly slower than the regular clock and the tuning logic insures
ian@0 1794 * that.
ian@0 1795 */
ian@0 1796
ian@0 1797 counter = time_interpolator_get_counter(1);
ian@0 1798 offset = time_interpolator->offset +
ian@0 1799 GET_TI_NSECS(counter, time_interpolator);
ian@0 1800
ian@0 1801 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
ian@0 1802 time_interpolator->offset = offset - delta_nsec;
ian@0 1803 else {
ian@0 1804 time_interpolator->skips++;
ian@0 1805 time_interpolator->ns_skipped += delta_nsec - offset;
ian@0 1806 time_interpolator->offset = 0;
ian@0 1807 }
ian@0 1808 time_interpolator->last_counter = counter;
ian@0 1809
ian@0 1810 /* Tuning logic for time interpolator invoked every minute or so.
ian@0 1811 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
ian@0 1812 * Increase interpolator clock speed if we skip too much time.
ian@0 1813 */
ian@0 1814 if (jiffies % INTERPOLATOR_ADJUST == 0)
ian@0 1815 {
ian@0 1816 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
ian@0 1817 time_interpolator->nsec_per_cyc--;
ian@0 1818 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
ian@0 1819 time_interpolator->nsec_per_cyc++;
ian@0 1820 time_interpolator->skips = 0;
ian@0 1821 time_interpolator->ns_skipped = 0;
ian@0 1822 }
ian@0 1823 }
ian@0 1824
ian@0 1825 static inline int
ian@0 1826 is_better_time_interpolator(struct time_interpolator *new)
ian@0 1827 {
ian@0 1828 if (!time_interpolator)
ian@0 1829 return 1;
ian@0 1830 return new->frequency > 2*time_interpolator->frequency ||
ian@0 1831 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
ian@0 1832 }
ian@0 1833
ian@0 1834 void
ian@0 1835 register_time_interpolator(struct time_interpolator *ti)
ian@0 1836 {
ian@0 1837 unsigned long flags;
ian@0 1838
ian@0 1839 /* Sanity check */
ian@0 1840 BUG_ON(ti->frequency == 0 || ti->mask == 0);
ian@0 1841
ian@0 1842 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
ian@0 1843 spin_lock(&time_interpolator_lock);
ian@0 1844 write_seqlock_irqsave(&xtime_lock, flags);
ian@0 1845 if (is_better_time_interpolator(ti)) {
ian@0 1846 time_interpolator = ti;
ian@0 1847 time_interpolator_reset();
ian@0 1848 }
ian@0 1849 write_sequnlock_irqrestore(&xtime_lock, flags);
ian@0 1850
ian@0 1851 ti->next = time_interpolator_list;
ian@0 1852 time_interpolator_list = ti;
ian@0 1853 spin_unlock(&time_interpolator_lock);
ian@0 1854 }
ian@0 1855
ian@0 1856 void
ian@0 1857 unregister_time_interpolator(struct time_interpolator *ti)
ian@0 1858 {
ian@0 1859 struct time_interpolator *curr, **prev;
ian@0 1860 unsigned long flags;
ian@0 1861
ian@0 1862 spin_lock(&time_interpolator_lock);
ian@0 1863 prev = &time_interpolator_list;
ian@0 1864 for (curr = *prev; curr; curr = curr->next) {
ian@0 1865 if (curr == ti) {
ian@0 1866 *prev = curr->next;
ian@0 1867 break;
ian@0 1868 }
ian@0 1869 prev = &curr->next;
ian@0 1870 }
ian@0 1871
ian@0 1872 write_seqlock_irqsave(&xtime_lock, flags);
ian@0 1873 if (ti == time_interpolator) {
ian@0 1874 /* we lost the best time-interpolator: */
ian@0 1875 time_interpolator = NULL;
ian@0 1876 /* find the next-best interpolator */
ian@0 1877 for (curr = time_interpolator_list; curr; curr = curr->next)
ian@0 1878 if (is_better_time_interpolator(curr))
ian@0 1879 time_interpolator = curr;
ian@0 1880 time_interpolator_reset();
ian@0 1881 }
ian@0 1882 write_sequnlock_irqrestore(&xtime_lock, flags);
ian@0 1883 spin_unlock(&time_interpolator_lock);
ian@0 1884 }
ian@0 1885 #endif /* CONFIG_TIME_INTERPOLATION */
ian@0 1886
ian@0 1887 /**
ian@0 1888 * msleep - sleep safely even with waitqueue interruptions
ian@0 1889 * @msecs: Time in milliseconds to sleep for
ian@0 1890 */
ian@0 1891 void msleep(unsigned int msecs)
ian@0 1892 {
ian@0 1893 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
ian@0 1894
ian@0 1895 while (timeout)
ian@0 1896 timeout = schedule_timeout_uninterruptible(timeout);
ian@0 1897 }
ian@0 1898
ian@0 1899 EXPORT_SYMBOL(msleep);
ian@0 1900
ian@0 1901 /**
ian@0 1902 * msleep_interruptible - sleep waiting for signals
ian@0 1903 * @msecs: Time in milliseconds to sleep for
ian@0 1904 */
ian@0 1905 unsigned long msleep_interruptible(unsigned int msecs)
ian@0 1906 {
ian@0 1907 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
ian@0 1908
ian@0 1909 while (timeout && !signal_pending(current))
ian@0 1910 timeout = schedule_timeout_interruptible(timeout);
ian@0 1911 return jiffies_to_msecs(timeout);
ian@0 1912 }
ian@0 1913
ian@0 1914 EXPORT_SYMBOL(msleep_interruptible);