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