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