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