[PATCH] namespaces: add nsproxy
[linux-2.6/verdex.git] / arch / mips / kernel / time.c
blob845c7e55505d39f3335e66f031e723b2729d6406
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
6 * Common time service routines for MIPS machines. See
7 * Documentation/mips/time.README.
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.
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>
27 #include <asm/bootinfo.h>
28 #include <asm/cache.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>
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).
42 #define USECS_PER_JIFFY TICK_SIZE
43 #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
45 #define TICK_SIZE (tick_nsec / 1000)
48 * forward reference
50 DEFINE_SPINLOCK(rtc_lock);
53 * By default we provide the null RTC ops
55 static unsigned long null_rtc_get_time(void)
57 return mktime(2000, 1, 1, 0, 0, 0);
60 static int null_rtc_set_time(unsigned long sec)
62 return 0;
65 unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time;
66 int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time;
67 int (*rtc_mips_set_mmss)(unsigned long);
70 /* usecs per counter cycle, shifted to left by 32 bits */
71 static unsigned int sll32_usecs_per_cycle;
73 /* how many counter cycles in a jiffy */
74 static unsigned long cycles_per_jiffy __read_mostly;
76 /* Cycle counter value at the previous timer interrupt.. */
77 static unsigned int timerhi, timerlo;
79 /* expirelo is the count value for next CPU timer interrupt */
80 static unsigned int expirelo;
84 * Null timer ack for systems not needing one (e.g. i8254).
86 static void null_timer_ack(void) { /* nothing */ }
89 * Null high precision timer functions for systems lacking one.
91 static unsigned int null_hpt_read(void)
93 return 0;
96 static void null_hpt_init(unsigned int count)
98 /* nothing */
103 * Timer ack for an R4k-compatible timer of a known frequency.
105 static void c0_timer_ack(void)
107 unsigned int count;
109 #ifndef CONFIG_SOC_PNX8550 /* pnx8550 resets to zero */
110 /* Ack this timer interrupt and set the next one. */
111 expirelo += cycles_per_jiffy;
112 #endif
113 write_c0_compare(expirelo);
115 /* Check to see if we have missed any timer interrupts. */
116 while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
117 /* missed_timer_count++; */
118 expirelo = count + cycles_per_jiffy;
119 write_c0_compare(expirelo);
124 * High precision timer functions for a R4k-compatible timer.
126 static unsigned int c0_hpt_read(void)
128 return read_c0_count();
131 /* For use solely as a high precision timer. */
132 static void c0_hpt_init(unsigned int count)
134 write_c0_count(read_c0_count() - count);
137 /* For use both as a high precision timer and an interrupt source. */
138 static void c0_hpt_timer_init(unsigned int count)
140 count = read_c0_count() - count;
141 expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
142 write_c0_count(expirelo - cycles_per_jiffy);
143 write_c0_compare(expirelo);
144 write_c0_count(count);
147 int (*mips_timer_state)(void);
148 void (*mips_timer_ack)(void);
149 unsigned int (*mips_hpt_read)(void);
150 void (*mips_hpt_init)(unsigned int);
154 * This version of gettimeofday has microsecond resolution and better than
155 * microsecond precision on fast machines with cycle counter.
157 void do_gettimeofday(struct timeval *tv)
159 unsigned long seq;
160 unsigned long usec, sec;
161 unsigned long max_ntp_tick;
163 do {
164 seq = read_seqbegin(&xtime_lock);
166 usec = do_gettimeoffset();
169 * If time_adjust is negative then NTP is slowing the clock
170 * so make sure not to go into next possible interval.
171 * Better to lose some accuracy than have time go backwards..
173 if (unlikely(time_adjust < 0)) {
174 max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
175 usec = min(usec, max_ntp_tick);
178 sec = xtime.tv_sec;
179 usec += (xtime.tv_nsec / 1000);
181 } while (read_seqretry(&xtime_lock, seq));
183 while (usec >= 1000000) {
184 usec -= 1000000;
185 sec++;
188 tv->tv_sec = sec;
189 tv->tv_usec = usec;
192 EXPORT_SYMBOL(do_gettimeofday);
194 int do_settimeofday(struct timespec *tv)
196 time_t wtm_sec, sec = tv->tv_sec;
197 long wtm_nsec, nsec = tv->tv_nsec;
199 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
200 return -EINVAL;
202 write_seqlock_irq(&xtime_lock);
205 * This is revolting. We need to set "xtime" correctly. However,
206 * the value in this location is the value at the most recent update
207 * of wall time. Discover what correction gettimeofday() would have
208 * made, and then undo it!
210 nsec -= do_gettimeoffset() * NSEC_PER_USEC;
212 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
213 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
215 set_normalized_timespec(&xtime, sec, nsec);
216 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
218 ntp_clear();
219 write_sequnlock_irq(&xtime_lock);
220 clock_was_set();
221 return 0;
224 EXPORT_SYMBOL(do_settimeofday);
227 * Gettimeoffset routines. These routines returns the time duration
228 * since last timer interrupt in usecs.
230 * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
231 * Otherwise use calibrate_gettimeoffset()
233 * If the CPU does not have the counter register, you can either supply
234 * your own gettimeoffset() routine, or use null_gettimeoffset(), which
235 * gives the same resolution as HZ.
238 static unsigned long null_gettimeoffset(void)
240 return 0;
244 /* The function pointer to one of the gettimeoffset funcs. */
245 unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
248 static unsigned long fixed_rate_gettimeoffset(void)
250 u32 count;
251 unsigned long res;
253 /* Get last timer tick in absolute kernel time */
254 count = mips_hpt_read();
256 /* .. relative to previous jiffy (32 bits is enough) */
257 count -= timerlo;
259 __asm__("multu %1,%2"
260 : "=h" (res)
261 : "r" (count), "r" (sll32_usecs_per_cycle)
262 : "lo", GCC_REG_ACCUM);
265 * Due to possible jiffies inconsistencies, we need to check
266 * the result so that we'll get a timer that is monotonic.
268 if (res >= USECS_PER_JIFFY)
269 res = USECS_PER_JIFFY - 1;
271 return res;
276 * Cached "1/(clocks per usec) * 2^32" value.
277 * It has to be recalculated once each jiffy.
279 static unsigned long cached_quotient;
281 /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
282 static unsigned long last_jiffies;
285 * This is moved from dec/time.c:do_ioasic_gettimeoffset() by Maciej.
287 static unsigned long calibrate_div32_gettimeoffset(void)
289 u32 count;
290 unsigned long res, tmp;
291 unsigned long quotient;
293 tmp = jiffies;
295 quotient = cached_quotient;
297 if (last_jiffies != tmp) {
298 last_jiffies = tmp;
299 if (last_jiffies != 0) {
300 unsigned long r0;
301 do_div64_32(r0, timerhi, timerlo, tmp);
302 do_div64_32(quotient, USECS_PER_JIFFY,
303 USECS_PER_JIFFY_FRAC, r0);
304 cached_quotient = quotient;
308 /* Get last timer tick in absolute kernel time */
309 count = mips_hpt_read();
311 /* .. relative to previous jiffy (32 bits is enough) */
312 count -= timerlo;
314 __asm__("multu %1,%2"
315 : "=h" (res)
316 : "r" (count), "r" (quotient)
317 : "lo", GCC_REG_ACCUM);
320 * Due to possible jiffies inconsistencies, we need to check
321 * the result so that we'll get a timer that is monotonic.
323 if (res >= USECS_PER_JIFFY)
324 res = USECS_PER_JIFFY - 1;
326 return res;
329 static unsigned long calibrate_div64_gettimeoffset(void)
331 u32 count;
332 unsigned long res, tmp;
333 unsigned long quotient;
335 tmp = jiffies;
337 quotient = cached_quotient;
339 if (last_jiffies != tmp) {
340 last_jiffies = tmp;
341 if (last_jiffies) {
342 unsigned long r0;
343 __asm__(".set push\n\t"
344 ".set mips3\n\t"
345 "lwu %0,%3\n\t"
346 "dsll32 %1,%2,0\n\t"
347 "or %1,%1,%0\n\t"
348 "ddivu $0,%1,%4\n\t"
349 "mflo %1\n\t"
350 "dsll32 %0,%5,0\n\t"
351 "or %0,%0,%6\n\t"
352 "ddivu $0,%0,%1\n\t"
353 "mflo %0\n\t"
354 ".set pop"
355 : "=&r" (quotient), "=&r" (r0)
356 : "r" (timerhi), "m" (timerlo),
357 "r" (tmp), "r" (USECS_PER_JIFFY),
358 "r" (USECS_PER_JIFFY_FRAC)
359 : "hi", "lo", GCC_REG_ACCUM);
360 cached_quotient = quotient;
364 /* Get last timer tick in absolute kernel time */
365 count = mips_hpt_read();
367 /* .. relative to previous jiffy (32 bits is enough) */
368 count -= timerlo;
370 __asm__("multu %1,%2"
371 : "=h" (res)
372 : "r" (count), "r" (quotient)
373 : "lo", GCC_REG_ACCUM);
376 * Due to possible jiffies inconsistencies, we need to check
377 * the result so that we'll get a timer that is monotonic.
379 if (res >= USECS_PER_JIFFY)
380 res = USECS_PER_JIFFY - 1;
382 return res;
386 /* last time when xtime and rtc are sync'ed up */
387 static long last_rtc_update;
390 * local_timer_interrupt() does profiling and process accounting
391 * on a per-CPU basis.
393 * In UP mode, it is invoked from the (global) timer_interrupt.
395 * In SMP mode, it might invoked by per-CPU timer interrupt, or
396 * a broadcasted inter-processor interrupt which itself is triggered
397 * by the global timer interrupt.
399 void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
401 if (current->pid)
402 profile_tick(CPU_PROFILING, regs);
403 update_process_times(user_mode(regs));
407 * High-level timer interrupt service routines. This function
408 * is set as irqaction->handler and is invoked through do_IRQ.
410 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
412 unsigned long j;
413 unsigned int count;
415 write_seqlock(&xtime_lock);
417 count = mips_hpt_read();
418 mips_timer_ack();
420 /* Update timerhi/timerlo for intra-jiffy calibration. */
421 timerhi += count < timerlo; /* Wrap around */
422 timerlo = count;
425 * call the generic timer interrupt handling
427 do_timer(1);
430 * If we have an externally synchronized Linux clock, then update
431 * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
432 * called as close as possible to 500 ms before the new second starts.
434 if (ntp_synced() &&
435 xtime.tv_sec > last_rtc_update + 660 &&
436 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
437 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
438 if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
439 last_rtc_update = xtime.tv_sec;
440 } else {
441 /* do it again in 60 s */
442 last_rtc_update = xtime.tv_sec - 600;
447 * If jiffies has overflown in this timer_interrupt, we must
448 * update the timer[hi]/[lo] to make fast gettimeoffset funcs
449 * quotient calc still valid. -arca
451 * The first timer interrupt comes late as interrupts are
452 * enabled long after timers are initialized. Therefore the
453 * high precision timer is fast, leading to wrong gettimeoffset()
454 * calculations. We deal with it by setting it based on the
455 * number of its ticks between the second and the third interrupt.
456 * That is still somewhat imprecise, but it's a good estimate.
457 * --macro
459 j = jiffies;
460 if (j < 4) {
461 static unsigned int prev_count;
462 static int hpt_initialized;
464 switch (j) {
465 case 0:
466 timerhi = timerlo = 0;
467 mips_hpt_init(count);
468 break;
469 case 2:
470 prev_count = count;
471 break;
472 case 3:
473 if (!hpt_initialized) {
474 unsigned int c3 = 3 * (count - prev_count);
476 timerhi = 0;
477 timerlo = c3;
478 mips_hpt_init(count - c3);
479 hpt_initialized = 1;
481 break;
482 default:
483 break;
487 write_sequnlock(&xtime_lock);
490 * In UP mode, we call local_timer_interrupt() to do profiling
491 * and process accouting.
493 * In SMP mode, local_timer_interrupt() is invoked by appropriate
494 * low-level local timer interrupt handler.
496 local_timer_interrupt(irq, dev_id, regs);
498 return IRQ_HANDLED;
501 int null_perf_irq(struct pt_regs *regs)
503 return 0;
506 int (*perf_irq)(struct pt_regs *regs) = null_perf_irq;
508 EXPORT_SYMBOL(null_perf_irq);
509 EXPORT_SYMBOL(perf_irq);
511 asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
513 int r2 = cpu_has_mips_r2;
515 irq_enter();
516 kstat_this_cpu.irqs[irq]++;
519 * Suckage alert:
520 * Before R2 of the architecture there was no way to see if a
521 * performance counter interrupt was pending, so we have to run the
522 * performance counter interrupt handler anyway.
524 if (!r2 || (read_c0_cause() & (1 << 26)))
525 if (perf_irq(regs))
526 goto out;
528 /* we keep interrupt disabled all the time */
529 if (!r2 || (read_c0_cause() & (1 << 30)))
530 timer_interrupt(irq, NULL, regs);
532 out:
533 irq_exit();
536 asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
538 irq_enter();
539 if (smp_processor_id() != 0)
540 kstat_this_cpu.irqs[irq]++;
542 /* we keep interrupt disabled all the time */
543 local_timer_interrupt(irq, NULL, regs);
545 irq_exit();
549 * time_init() - it does the following things.
551 * 1) board_time_init() -
552 * a) (optional) set up RTC routines,
553 * b) (optional) calibrate and set the mips_hpt_frequency
554 * (only needed if you intended to use fixed_rate_gettimeoffset
555 * or use cpu counter as timer interrupt source)
556 * 2) setup xtime based on rtc_mips_get_time().
557 * 3) choose a appropriate gettimeoffset routine.
558 * 4) calculate a couple of cached variables for later usage
559 * 5) plat_timer_setup() -
560 * a) (optional) over-write any choices made above by time_init().
561 * b) machine specific code should setup the timer irqaction.
562 * c) enable the timer interrupt
565 void (*board_time_init)(void);
567 unsigned int mips_hpt_frequency;
569 static struct irqaction timer_irqaction = {
570 .handler = timer_interrupt,
571 .flags = IRQF_DISABLED,
572 .name = "timer",
575 static unsigned int __init calibrate_hpt(void)
577 u64 frequency;
578 u32 hpt_start, hpt_end, hpt_count, hz;
580 const int loops = HZ / 10;
581 int log_2_loops = 0;
582 int i;
585 * We want to calibrate for 0.1s, but to avoid a 64-bit
586 * division we round the number of loops up to the nearest
587 * power of 2.
589 while (loops > 1 << log_2_loops)
590 log_2_loops++;
591 i = 1 << log_2_loops;
594 * Wait for a rising edge of the timer interrupt.
596 while (mips_timer_state());
597 while (!mips_timer_state());
600 * Now see how many high precision timer ticks happen
601 * during the calculated number of periods between timer
602 * interrupts.
604 hpt_start = mips_hpt_read();
605 do {
606 while (mips_timer_state());
607 while (!mips_timer_state());
608 } while (--i);
609 hpt_end = mips_hpt_read();
611 hpt_count = hpt_end - hpt_start;
612 hz = HZ;
613 frequency = (u64)hpt_count * (u64)hz;
615 return frequency >> log_2_loops;
618 void __init time_init(void)
620 if (board_time_init)
621 board_time_init();
623 if (!rtc_mips_set_mmss)
624 rtc_mips_set_mmss = rtc_mips_set_time;
626 xtime.tv_sec = rtc_mips_get_time();
627 xtime.tv_nsec = 0;
629 set_normalized_timespec(&wall_to_monotonic,
630 -xtime.tv_sec, -xtime.tv_nsec);
632 /* Choose appropriate high precision timer routines. */
633 if (!cpu_has_counter && !mips_hpt_read) {
634 /* No high precision timer -- sorry. */
635 mips_hpt_read = null_hpt_read;
636 mips_hpt_init = null_hpt_init;
637 } else if (!mips_hpt_frequency && !mips_timer_state) {
638 /* A high precision timer of unknown frequency. */
639 if (!mips_hpt_read) {
640 /* No external high precision timer -- use R4k. */
641 mips_hpt_read = c0_hpt_read;
642 mips_hpt_init = c0_hpt_init;
645 if (cpu_has_mips32r1 || cpu_has_mips32r2 ||
646 (current_cpu_data.isa_level == MIPS_CPU_ISA_I) ||
647 (current_cpu_data.isa_level == MIPS_CPU_ISA_II))
649 * We need to calibrate the counter but we don't have
650 * 64-bit division.
652 do_gettimeoffset = calibrate_div32_gettimeoffset;
653 else
655 * We need to calibrate the counter but we *do* have
656 * 64-bit division.
658 do_gettimeoffset = calibrate_div64_gettimeoffset;
659 } else {
660 /* We know counter frequency. Or we can get it. */
661 if (!mips_hpt_read) {
662 /* No external high precision timer -- use R4k. */
663 mips_hpt_read = c0_hpt_read;
665 if (mips_timer_state)
666 mips_hpt_init = c0_hpt_init;
667 else {
668 /* No external timer interrupt -- use R4k. */
669 mips_hpt_init = c0_hpt_timer_init;
670 mips_timer_ack = c0_timer_ack;
673 if (!mips_hpt_frequency)
674 mips_hpt_frequency = calibrate_hpt();
676 do_gettimeoffset = fixed_rate_gettimeoffset;
678 /* Calculate cache parameters. */
679 cycles_per_jiffy = (mips_hpt_frequency + HZ / 2) / HZ;
681 /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq */
682 do_div64_32(sll32_usecs_per_cycle,
683 1000000, mips_hpt_frequency / 2,
684 mips_hpt_frequency);
686 /* Report the high precision timer rate for a reference. */
687 printk("Using %u.%03u MHz high precision timer.\n",
688 ((mips_hpt_frequency + 500) / 1000) / 1000,
689 ((mips_hpt_frequency + 500) / 1000) % 1000);
692 if (!mips_timer_ack)
693 /* No timer interrupt ack (e.g. i8254). */
694 mips_timer_ack = null_timer_ack;
696 /* This sets up the high precision timer for the first interrupt. */
697 mips_hpt_init(mips_hpt_read());
700 * Call board specific timer interrupt setup.
702 * this pointer must be setup in machine setup routine.
704 * Even if a machine chooses to use a low-level timer interrupt,
705 * it still needs to setup the timer_irqaction.
706 * In that case, it might be better to set timer_irqaction.handler
707 * to be NULL function so that we are sure the high-level code
708 * is not invoked accidentally.
710 plat_timer_setup(&timer_irqaction);
713 #define FEBRUARY 2
714 #define STARTOFTIME 1970
715 #define SECDAY 86400L
716 #define SECYR (SECDAY * 365)
717 #define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
718 #define days_in_year(y) (leapyear(y) ? 366 : 365)
719 #define days_in_month(m) (month_days[(m) - 1])
721 static int month_days[12] = {
722 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
725 void to_tm(unsigned long tim, struct rtc_time *tm)
727 long hms, day, gday;
728 int i;
730 gday = day = tim / SECDAY;
731 hms = tim % SECDAY;
733 /* Hours, minutes, seconds are easy */
734 tm->tm_hour = hms / 3600;
735 tm->tm_min = (hms % 3600) / 60;
736 tm->tm_sec = (hms % 3600) % 60;
738 /* Number of years in days */
739 for (i = STARTOFTIME; day >= days_in_year(i); i++)
740 day -= days_in_year(i);
741 tm->tm_year = i;
743 /* Number of months in days left */
744 if (leapyear(tm->tm_year))
745 days_in_month(FEBRUARY) = 29;
746 for (i = 1; day >= days_in_month(i); i++)
747 day -= days_in_month(i);
748 days_in_month(FEBRUARY) = 28;
749 tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
751 /* Days are what is left over (+1) from all that. */
752 tm->tm_mday = day + 1;
755 * Determine the day of week
757 tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
760 EXPORT_SYMBOL(rtc_lock);
761 EXPORT_SYMBOL(to_tm);
762 EXPORT_SYMBOL(rtc_mips_set_time);
763 EXPORT_SYMBOL(rtc_mips_get_time);
765 unsigned long long sched_clock(void)
767 return (unsigned long long)jiffies*(1000000000/HZ);