vserver 1.9.3
[linux-2.6.git] / arch / mips / kernel / time.c
1 /*
2  * Copyright 2001 MontaVista Software Inc.
3  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4  * Copyright (c) 2003  Maciej W. Rozycki
5  *
6  * Common time service routines for MIPS machines. See
7  * Documentation/mips/time.README.
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14 #include <linux/config.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/param.h>
20 #include <linux/time.h>
21 #include <linux/timex.h>
22 #include <linux/smp.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27
28 #include <asm/bootinfo.h>
29 #include <asm/cpu.h>
30 #include <asm/cpu-features.h>
31 #include <asm/div64.h>
32 #include <asm/sections.h>
33 #include <asm/time.h>
34
35 /*
36  * The integer part of the number of usecs per jiffy is taken from tick,
37  * but the fractional part is not recorded, so we calculate it using the
38  * initial value of HZ.  This aids systems where tick isn't really an
39  * integer (e.g. for HZ = 128).
40  */
41 #define USECS_PER_JIFFY         TICK_SIZE
42 #define USECS_PER_JIFFY_FRAC    ((unsigned long)(u32)((1000000ULL << 32) / HZ))
43
44 #define TICK_SIZE       (tick_nsec / 1000)
45
46 u64 jiffies_64 = INITIAL_JIFFIES;
47
48 EXPORT_SYMBOL(jiffies_64);
49
50 /*
51  * forward reference
52  */
53 extern volatile unsigned long wall_jiffies;
54
55 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
56
57 /*
58  * whether we emulate local_timer_interrupts for SMP machines.
59  */
60 int emulate_local_timer_interrupt;
61
62
63 /*
64  * By default we provide the null RTC ops
65  */
66 static unsigned long null_rtc_get_time(void)
67 {
68         return mktime(2000, 1, 1, 0, 0, 0);
69 }
70
71 static int null_rtc_set_time(unsigned long sec)
72 {
73         return 0;
74 }
75
76 unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
77 int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
78 int (*rtc_set_mmss)(unsigned long);
79
80
81 /* usecs per counter cycle, shifted to left by 32 bits */
82 static unsigned int sll32_usecs_per_cycle;
83
84 /* how many counter cycles in a jiffy */
85 static unsigned long cycles_per_jiffy;
86
87 /* Cycle counter value at the previous timer interrupt.. */
88 static unsigned int timerhi, timerlo;
89
90 /* expirelo is the count value for next CPU timer interrupt */
91 static unsigned int expirelo;
92
93
94 /*
95  * Null timer ack for systems not needing one (e.g. i8254).
96  */
97 static void null_timer_ack(void) { /* nothing */ }
98
99 /*
100  * Null high precision timer functions for systems lacking one.
101  */
102 static unsigned int null_hpt_read(void)
103 {
104         return 0;
105 }
106
107 static void null_hpt_init(unsigned int count) { /* nothing */ }
108
109
110 /*
111  * Timer ack for an R4k-compatible timer of a known frequency.
112  */
113 static void c0_timer_ack(void)
114 {
115         unsigned int count;
116
117         /* Ack this timer interrupt and set the next one.  */
118         expirelo += cycles_per_jiffy;
119         write_c0_compare(expirelo);
120
121         /* Check to see if we have missed any timer interrupts.  */
122         count = read_c0_count();
123         if ((count - expirelo) < 0x7fffffff) {
124                 /* missed_timer_count++; */
125                 expirelo = count + cycles_per_jiffy;
126                 write_c0_compare(expirelo);
127         }
128 }
129
130 /*
131  * High precision timer functions for a R4k-compatible timer.
132  */
133 static unsigned int c0_hpt_read(void)
134 {
135         return read_c0_count();
136 }
137
138 /* For use solely as a high precision timer.  */
139 static void c0_hpt_init(unsigned int count)
140 {
141         write_c0_count(read_c0_count() - count);
142 }
143
144 /* For use both as a high precision timer and an interrupt source.  */
145 static void c0_hpt_timer_init(unsigned int count)
146 {
147         count = read_c0_count() - count;
148         expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
149         write_c0_count(expirelo - cycles_per_jiffy);
150         write_c0_compare(expirelo);
151         write_c0_count(count);
152 }
153
154 int (*mips_timer_state)(void);
155 void (*mips_timer_ack)(void);
156 unsigned int (*mips_hpt_read)(void);
157 void (*mips_hpt_init)(unsigned int);
158
159
160 /*
161  * This version of gettimeofday has microsecond resolution and better than
162  * microsecond precision on fast machines with cycle counter.
163  */
164 void do_gettimeofday(struct timeval *tv)
165 {
166         unsigned long seq;
167         unsigned long lost;
168         unsigned long usec, sec;
169         unsigned long max_ntp_tick = tick_usec - tickadj;
170
171         do {
172                 seq = read_seqbegin(&xtime_lock);
173
174                 usec = do_gettimeoffset();
175
176                 lost = jiffies - wall_jiffies;
177
178                 /*
179                  * If time_adjust is negative then NTP is slowing the clock
180                  * so make sure not to go into next possible interval.
181                  * Better to lose some accuracy than have time go backwards..
182                  */
183                 if (unlikely(time_adjust < 0)) {
184                         usec = min(usec, max_ntp_tick);
185
186                         if (lost)
187                                 usec += lost * max_ntp_tick;
188                 } else if (unlikely(lost))
189                         usec += lost * tick_usec;
190
191                 sec = xtime.tv_sec;
192                 usec += (xtime.tv_nsec / 1000);
193
194         } while (read_seqretry(&xtime_lock, seq));
195
196         while (usec >= 1000000) {
197                 usec -= 1000000;
198                 sec++;
199         }
200
201         tv->tv_sec = sec;
202         tv->tv_usec = usec;
203 }
204
205 EXPORT_SYMBOL(do_gettimeofday);
206
207 int do_settimeofday(struct timespec *tv)
208 {
209         time_t wtm_sec, sec = tv->tv_sec;
210         long wtm_nsec, nsec = tv->tv_nsec;
211
212         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
213                 return -EINVAL;
214
215         write_seqlock_irq(&xtime_lock);
216
217         /*
218          * This is revolting.  We need to set "xtime" correctly.  However,
219          * the value in this location is the value at the most recent update
220          * of wall time.  Discover what correction gettimeofday() would have
221          * made, and then undo it!
222          */
223         nsec -= do_gettimeoffset() * NSEC_PER_USEC;
224         nsec -= (jiffies - wall_jiffies) * tick_nsec;
225
226         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
227         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
228
229         set_normalized_timespec(&xtime, sec, nsec);
230         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
231
232         time_adjust = 0;                        /* stop active adjtime() */
233         time_status |= STA_UNSYNC;
234         time_maxerror = NTP_PHASE_LIMIT;
235         time_esterror = NTP_PHASE_LIMIT;
236
237         write_sequnlock_irq(&xtime_lock);
238         clock_was_set();
239         return 0;
240 }
241
242 EXPORT_SYMBOL(do_settimeofday);
243
244 /*
245  * Gettimeoffset routines.  These routines returns the time duration
246  * since last timer interrupt in usecs.
247  *
248  * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
249  * Otherwise use calibrate_gettimeoffset()
250  *
251  * If the CPU does not have the counter register, you can either supply
252  * your own gettimeoffset() routine, or use null_gettimeoffset(), which
253  * gives the same resolution as HZ.
254  */
255
256 static unsigned long null_gettimeoffset(void)
257 {
258         return 0;
259 }
260
261
262 /* The function pointer to one of the gettimeoffset funcs.  */
263 unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
264
265
266 static unsigned long fixed_rate_gettimeoffset(void)
267 {
268         u32 count;
269         unsigned long res;
270
271         /* Get last timer tick in absolute kernel time */
272         count = mips_hpt_read();
273
274         /* .. relative to previous jiffy (32 bits is enough) */
275         count -= timerlo;
276
277         __asm__("multu  %1,%2"
278                 : "=h" (res)
279                 : "r" (count), "r" (sll32_usecs_per_cycle)
280                 : "lo", "accum");
281
282         /*
283          * Due to possible jiffies inconsistencies, we need to check
284          * the result so that we'll get a timer that is monotonic.
285          */
286         if (res >= USECS_PER_JIFFY)
287                 res = USECS_PER_JIFFY - 1;
288
289         return res;
290 }
291
292
293 /*
294  * Cached "1/(clocks per usec) * 2^32" value.
295  * It has to be recalculated once each jiffy.
296  */
297 static unsigned long cached_quotient;
298
299 /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
300 static unsigned long last_jiffies;
301
302 /*
303  * This is moved from dec/time.c:do_ioasic_gettimeoffset() by Maciej.
304  */
305 static unsigned long calibrate_div32_gettimeoffset(void)
306 {
307         u32 count;
308         unsigned long res, tmp;
309         unsigned long quotient;
310
311         tmp = jiffies;
312
313         quotient = cached_quotient;
314
315         if (last_jiffies != tmp) {
316                 last_jiffies = tmp;
317                 if (last_jiffies != 0) {
318                         unsigned long r0;
319                         do_div64_32(r0, timerhi, timerlo, tmp);
320                         do_div64_32(quotient, USECS_PER_JIFFY,
321                                     USECS_PER_JIFFY_FRAC, r0);
322                         cached_quotient = quotient;
323                 }
324         }
325
326         /* Get last timer tick in absolute kernel time */
327         count = mips_hpt_read();
328
329         /* .. relative to previous jiffy (32 bits is enough) */
330         count -= timerlo;
331
332         __asm__("multu  %1,%2"
333                 : "=h" (res)
334                 : "r" (count), "r" (quotient)
335                 : "lo", "accum");
336
337         /*
338          * Due to possible jiffies inconsistencies, we need to check
339          * the result so that we'll get a timer that is monotonic.
340          */
341         if (res >= USECS_PER_JIFFY)
342                 res = USECS_PER_JIFFY - 1;
343
344         return res;
345 }
346
347 static unsigned long calibrate_div64_gettimeoffset(void)
348 {
349         u32 count;
350         unsigned long res, tmp;
351         unsigned long quotient;
352
353         tmp = jiffies;
354
355         quotient = cached_quotient;
356
357         if (last_jiffies != tmp) {
358                 last_jiffies = tmp;
359                 if (last_jiffies) {
360                         unsigned long r0;
361                         __asm__(".set   push\n\t"
362                                 ".set   mips3\n\t"
363                                 "lwu    %0,%3\n\t"
364                                 "dsll32 %1,%2,0\n\t"
365                                 "or     %1,%1,%0\n\t"
366                                 "ddivu  $0,%1,%4\n\t"
367                                 "mflo   %1\n\t"
368                                 "dsll32 %0,%5,0\n\t"
369                                 "or     %0,%0,%6\n\t"
370                                 "ddivu  $0,%0,%1\n\t"
371                                 "mflo   %0\n\t"
372                                 ".set   pop"
373                                 : "=&r" (quotient), "=&r" (r0)
374                                 : "r" (timerhi), "m" (timerlo),
375                                   "r" (tmp), "r" (USECS_PER_JIFFY),
376                                   "r" (USECS_PER_JIFFY_FRAC)
377                                 : "hi", "lo", "accum");
378                         cached_quotient = quotient;
379                 }
380         }
381
382         /* Get last timer tick in absolute kernel time */
383         count = mips_hpt_read();
384
385         /* .. relative to previous jiffy (32 bits is enough) */
386         count -= timerlo;
387
388         __asm__("multu  %1,%2"
389                 : "=h" (res)
390                 : "r" (count), "r" (quotient)
391                 : "lo", "accum");
392
393         /*
394          * Due to possible jiffies inconsistencies, we need to check
395          * the result so that we'll get a timer that is monotonic.
396          */
397         if (res >= USECS_PER_JIFFY)
398                 res = USECS_PER_JIFFY - 1;
399
400         return res;
401 }
402
403
404 /* last time when xtime and rtc are sync'ed up */
405 static long last_rtc_update;
406
407 /*
408  * local_timer_interrupt() does profiling and process accounting
409  * on a per-CPU basis.
410  *
411  * In UP mode, it is invoked from the (global) timer_interrupt.
412  *
413  * In SMP mode, it might invoked by per-CPU timer interrupt, or
414  * a broadcasted inter-processor interrupt which itself is triggered
415  * by the global timer interrupt.
416  */
417 void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
418 {
419         if (current->pid)
420                 profile_tick(CPU_PROFILING, regs);
421 #ifdef CONFIG_SMP
422         /* in UP mode, update_process_times() is invoked by do_timer() */
423         update_process_times(user_mode(regs));
424 #endif
425 }
426
427 /*
428  * High-level timer interrupt service routines.  This function
429  * is set as irqaction->handler and is invoked through do_IRQ.
430  */
431 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
432 {
433         unsigned long j;
434         unsigned int count;
435
436         count = mips_hpt_read();
437         mips_timer_ack();
438
439         /* Update timerhi/timerlo for intra-jiffy calibration. */
440         timerhi += count < timerlo;                     /* Wrap around */
441         timerlo = count;
442
443         /*
444          * call the generic timer interrupt handling
445          */
446         do_timer(regs);
447
448         /*
449          * If we have an externally synchronized Linux clock, then update
450          * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
451          * called as close as possible to 500 ms before the new second starts.
452          */
453         write_seqlock(&xtime_lock);
454         if ((time_status & STA_UNSYNC) == 0 &&
455             xtime.tv_sec > last_rtc_update + 660 &&
456             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
457             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
458                 if (rtc_set_mmss(xtime.tv_sec) == 0) {
459                         last_rtc_update = xtime.tv_sec;
460                 } else {
461                         /* do it again in 60 s */
462                         last_rtc_update = xtime.tv_sec - 600;
463                 }
464         }
465         write_sequnlock(&xtime_lock);
466
467         /*
468          * If jiffies has overflown in this timer_interrupt, we must
469          * update the timer[hi]/[lo] to make fast gettimeoffset funcs
470          * quotient calc still valid. -arca
471          *
472          * The first timer interrupt comes late as interrupts are
473          * enabled long after timers are initialized.  Therefore the
474          * high precision timer is fast, leading to wrong gettimeoffset()
475          * calculations.  We deal with it by setting it based on the
476          * number of its ticks between the second and the third interrupt.
477          * That is still somewhat imprecise, but it's a good estimate.
478          * --macro
479          */
480         j = jiffies;
481         if (j < 4) {
482                 static unsigned int prev_count;
483                 static int hpt_initialized;
484
485                 switch (j) {
486                 case 0:
487                         timerhi = timerlo = 0;
488                         mips_hpt_init(count);
489                         break;
490                 case 2:
491                         prev_count = count;
492                         break;
493                 case 3:
494                         if (!hpt_initialized) {
495                                 unsigned int c3 = 3 * (count - prev_count);
496
497                                 timerhi = 0;
498                                 timerlo = c3;
499                                 mips_hpt_init(count - c3);
500                                 hpt_initialized = 1;
501                         }
502                         break;
503                 default:
504                         break;
505                 }
506         }
507
508 #if !defined(CONFIG_SMP)
509         /*
510          * In UP mode, we call local_timer_interrupt() to do profiling
511          * and process accouting.
512          *
513          * In SMP mode, local_timer_interrupt() is invoked by appropriate
514          * low-level local timer interrupt handler.
515          */
516         local_timer_interrupt(irq, dev_id, regs);
517
518 #else   /* CONFIG_SMP */
519
520         if (emulate_local_timer_interrupt) {
521                 /*
522                  * this is the place where we send out inter-process
523                  * interrupts and let each CPU do its own profiling
524                  * and process accouting.
525                  *
526                  * Obviously we need to call local_timer_interrupt() for
527                  * the current CPU too.
528                  */
529                 panic("Not implemented yet!!!");
530         }
531 #endif  /* CONFIG_SMP */
532
533         return IRQ_HANDLED;
534 }
535
536 asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
537 {
538         irq_enter();
539         kstat_this_cpu.irqs[irq]++;
540
541         /* we keep interrupt disabled all the time */
542         timer_interrupt(irq, NULL, regs);
543
544         irq_exit();
545 }
546
547 asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
548 {
549         irq_enter();
550         if (smp_processor_id() != 0)
551                 kstat_this_cpu.irqs[irq]++;
552
553         /* we keep interrupt disabled all the time */
554         local_timer_interrupt(irq, NULL, regs);
555
556         irq_exit();
557 }
558
559 /*
560  * time_init() - it does the following things.
561  *
562  * 1) board_time_init() -
563  *      a) (optional) set up RTC routines,
564  *      b) (optional) calibrate and set the mips_hpt_frequency
565  *          (only needed if you intended to use fixed_rate_gettimeoffset
566  *           or use cpu counter as timer interrupt source)
567  * 2) setup xtime based on rtc_get_time().
568  * 3) choose a appropriate gettimeoffset routine.
569  * 4) calculate a couple of cached variables for later usage
570  * 5) board_timer_setup() -
571  *      a) (optional) over-write any choices made above by time_init().
572  *      b) machine specific code should setup the timer irqaction.
573  *      c) enable the timer interrupt
574  */
575
576 void (*board_time_init)(void);
577 void (*board_timer_setup)(struct irqaction *irq);
578
579 unsigned int mips_hpt_frequency;
580
581 static struct irqaction timer_irqaction = {
582         .handler = timer_interrupt,
583         .flags = SA_INTERRUPT,
584         .name = "timer",
585 };
586
587 static unsigned int __init calibrate_hpt(void)
588 {
589         u64 frequency;
590         u32 hpt_start, hpt_end, hpt_count, hz;
591
592         const int loops = HZ / 10;
593         int log_2_loops = 0;
594         int i;
595
596         /*
597          * We want to calibrate for 0.1s, but to avoid a 64-bit
598          * division we round the number of loops up to the nearest
599          * power of 2.
600          */
601         while (loops > 1 << log_2_loops)
602                 log_2_loops++;
603         i = 1 << log_2_loops;
604
605         /*
606          * Wait for a rising edge of the timer interrupt.
607          */
608         while (mips_timer_state());
609         while (!mips_timer_state());
610
611         /*
612          * Now see how many high precision timer ticks happen
613          * during the calculated number of periods between timer
614          * interrupts.
615          */
616         hpt_start = mips_hpt_read();
617         do {
618                 while (mips_timer_state());
619                 while (!mips_timer_state());
620         } while (--i);
621         hpt_end = mips_hpt_read();
622
623         hpt_count = hpt_end - hpt_start;
624         hz = HZ;
625         frequency = (u64)hpt_count * (u64)hz;
626
627         return frequency >> log_2_loops;
628 }
629
630 void __init time_init(void)
631 {
632         if (board_time_init)
633                 board_time_init();
634
635         if (!rtc_set_mmss)
636                 rtc_set_mmss = rtc_set_time;
637
638         xtime.tv_sec = rtc_get_time();
639         xtime.tv_nsec = 0;
640
641         set_normalized_timespec(&wall_to_monotonic,
642                                 -xtime.tv_sec, -xtime.tv_nsec);
643
644         /* Choose appropriate high precision timer routines.  */
645         if (!cpu_has_counter && !mips_hpt_read) {
646                 /* No high precision timer -- sorry.  */
647                 mips_hpt_read = null_hpt_read;
648                 mips_hpt_init = null_hpt_init;
649         } else if (!mips_hpt_frequency && !mips_timer_state) {
650                 /* A high precision timer of unknown frequency.  */
651                 if (!mips_hpt_read) {
652                         /* No external high precision timer -- use R4k.  */
653                         mips_hpt_read = c0_hpt_read;
654                         mips_hpt_init = c0_hpt_init;
655                 }
656
657                 if ((current_cpu_data.isa_level == MIPS_CPU_ISA_M32) ||
658                          (current_cpu_data.isa_level == MIPS_CPU_ISA_I) ||
659                          (current_cpu_data.isa_level == MIPS_CPU_ISA_II))
660                         /*
661                          * We need to calibrate the counter but we don't have
662                          * 64-bit division.
663                          */
664                         do_gettimeoffset = calibrate_div32_gettimeoffset;
665                 else
666                         /*
667                          * We need to calibrate the counter but we *do* have
668                          * 64-bit division.
669                          */
670                         do_gettimeoffset = calibrate_div64_gettimeoffset;
671         } else {
672                 /* We know counter frequency.  Or we can get it.  */
673                 if (!mips_hpt_read) {
674                         /* No external high precision timer -- use R4k.  */
675                         mips_hpt_read = c0_hpt_read;
676
677                         if (mips_timer_state)
678                                 mips_hpt_init = c0_hpt_init;
679                         else {
680                                 /* No external timer interrupt -- use R4k.  */
681                                 mips_hpt_init = c0_hpt_timer_init;
682                                 mips_timer_ack = c0_timer_ack;
683                         }
684                 }
685                 if (!mips_hpt_frequency)
686                         mips_hpt_frequency = calibrate_hpt();
687
688                 do_gettimeoffset = fixed_rate_gettimeoffset;
689
690                 /* Calculate cache parameters.  */
691                 cycles_per_jiffy = (mips_hpt_frequency + HZ / 2) / HZ;
692
693                 /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq  */
694                 do_div64_32(sll32_usecs_per_cycle,
695                             1000000, mips_hpt_frequency / 2,
696                             mips_hpt_frequency);
697
698                 /* Report the high precision timer rate for a reference.  */
699                 printk("Using %u.%03u MHz high precision timer.\n",
700                        ((mips_hpt_frequency + 500) / 1000) / 1000,
701                        ((mips_hpt_frequency + 500) / 1000) % 1000);
702         }
703
704         if (!mips_timer_ack)
705                 /* No timer interrupt ack (e.g. i8254).  */
706                 mips_timer_ack = null_timer_ack;
707
708         /* This sets up the high precision timer for the first interrupt.  */
709         mips_hpt_init(mips_hpt_read());
710
711         /*
712          * Call board specific timer interrupt setup.
713          *
714          * this pointer must be setup in machine setup routine.
715          *
716          * Even if a machine chooses to use a low-level timer interrupt,
717          * it still needs to setup the timer_irqaction.
718          * In that case, it might be better to set timer_irqaction.handler
719          * to be NULL function so that we are sure the high-level code
720          * is not invoked accidentally.
721          */
722         board_timer_setup(&timer_irqaction);
723 }
724
725 #define FEBRUARY                2
726 #define STARTOFTIME             1970
727 #define SECDAY                  86400L
728 #define SECYR                   (SECDAY * 365)
729 #define leapyear(y)             ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
730 #define days_in_year(y)         (leapyear(y) ? 366 : 365)
731 #define days_in_month(m)        (month_days[(m) - 1])
732
733 static int month_days[12] = {
734         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
735 };
736
737 void to_tm(unsigned long tim, struct rtc_time *tm)
738 {
739         long hms, day, gday;
740         int i;
741
742         gday = day = tim / SECDAY;
743         hms = tim % SECDAY;
744
745         /* Hours, minutes, seconds are easy */
746         tm->tm_hour = hms / 3600;
747         tm->tm_min = (hms % 3600) / 60;
748         tm->tm_sec = (hms % 3600) % 60;
749
750         /* Number of years in days */
751         for (i = STARTOFTIME; day >= days_in_year(i); i++)
752                 day -= days_in_year(i);
753         tm->tm_year = i;
754
755         /* Number of months in days left */
756         if (leapyear(tm->tm_year))
757                 days_in_month(FEBRUARY) = 29;
758         for (i = 1; day >= days_in_month(i); i++)
759                 day -= days_in_month(i);
760         days_in_month(FEBRUARY) = 28;
761         tm->tm_mon = i - 1;             /* tm_mon starts from 0 to 11 */
762
763         /* Days are what is left over (+1) from all that. */
764         tm->tm_mday = day + 1;
765
766         /*
767          * Determine the day of week
768          */
769         tm->tm_wday = (gday + 4) % 7;   /* 1970/1/1 was Thursday */
770 }
771
772 EXPORT_SYMBOL(rtc_lock);
773 EXPORT_SYMBOL(to_tm);
774 EXPORT_SYMBOL(rtc_set_time);
775 EXPORT_SYMBOL(rtc_get_time);
776
777 unsigned long long sched_clock(void)
778 {
779         return (unsigned long long)jiffies*(1000000000/HZ);
780 }