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