also update spec file ...
[linux-flexiantxendom0-3.2.10.git] / arch / mips64 / kernel / time.c
1 /*
2  * Copyright 2001 MontaVista Software Inc.
3  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4  *
5  * Common time service routines for MIPS machines. See
6  * Documents/mips/README.txt.
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  */
13 #include <linux/config.h>
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/smp.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25
26 #include <asm/bootinfo.h>
27 #include <asm/cpu.h>
28 #include <asm/time.h>
29 #include <asm/hardirq.h>
30 #include <asm/div64.h>
31
32 /* This is for machines which generate the exact clock. */
33 #define USECS_PER_JIFFY (1000000/HZ)
34 #define USECS_PER_JIFFY_FRAC ((u32)((1000000ULL << 32) / HZ))
35
36 #define TICK_SIZE       (tick_nsec / 1000)
37
38 u64 jiffies_64;
39
40 /*
41  * forward reference
42  */
43 extern volatile unsigned long wall_jiffies;
44
45 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
46
47 /*
48  * whether we emulate local_timer_interrupts for SMP machines.
49  */
50 int emulate_local_timer_interrupt;
51
52 /*
53  * By default we provide the null RTC ops
54  */
55 static unsigned long null_rtc_get_time(void)
56 {
57         return mktime(2000, 1, 1, 0, 0, 0);
58 }
59
60 static int null_rtc_set_time(unsigned long sec)
61 {
62         return 0;
63 }
64
65 unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
66 int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
67
68
69 /*
70  * This version of gettimeofday has microsecond resolution and better than
71  * microsecond precision on fast machines with cycle counter.
72  */
73 void do_gettimeofday(struct timeval *tv)
74 {
75         unsigned long seq;
76         unsigned long usec, sec;
77
78         do {
79                 seq = read_seqbegin(&xtime_lock);
80                 usec = do_gettimeoffset();
81                 {
82                         unsigned long lost = jiffies - wall_jiffies;
83                         if (lost)
84                                 usec += lost * (1000000 / HZ);
85                 }
86                 sec = xtime.tv_sec;
87                 usec += (xtime.tv_nsec / 1000);
88         } while (read_seqretry(&xtime_lock, seq));
89
90         while (usec >= 1000000) {
91                 usec -= 1000000;
92                 sec++;
93         }
94
95         tv->tv_sec = sec;
96         tv->tv_usec = usec;
97 }
98
99 int do_settimeofday(struct timespec *tv)
100 {
101         time_t wtm_sec, sec = tv->tv_sec;
102         long wtm_nsec, nsec = tv->tv_nsec;
103
104         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
105                 return -EINVAL;
106
107         write_seqlock_irq(&xtime_lock);
108         /*
109          * This is revolting. We need to set "xtime" correctly. However, the
110          * value in this location is the value at the most recent update of
111          * wall time.  Discover what correction gettimeofday() would have
112          * made, and then undo it!
113          */
114         nsec -= do_gettimeoffset() * NSEC_PER_USEC;
115         nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
116
117         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - tsec);
118         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
119
120         set_normalized_timespec(&xtime, sec, nsec);
121         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
122
123         time_adjust = 0;                /* stop active adjtime() */
124         time_status |= STA_UNSYNC;
125         time_maxerror = NTP_PHASE_LIMIT;
126         time_esterror = NTP_PHASE_LIMIT;
127         write_sequnlock_irq(&xtime_lock);
128
129         return 0;
130 }
131
132
133 /*
134  * Gettimeoffset routines.  These routines returns the time duration
135  * since last timer interrupt in usecs.
136  *
137  * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
138  * Otherwise use calibrate_gettimeoffset()
139  *
140  * If the CPU does not have counter register all, you can either supply
141  * your own gettimeoffset() routine, or use null_gettimeoffset() routines,
142  * which gives the same resolution as HZ.
143  */
144
145
146 /* This is for machines which generate the exact clock. */
147 #define USECS_PER_JIFFY (1000000/HZ)
148
149 /* usecs per counter cycle, shifted to left by 32 bits */
150 static unsigned int sll32_usecs_per_cycle=0;
151
152 /* how many counter cycles in a jiffy */
153 static unsigned long cycles_per_jiffy=0;
154
155 /* Cycle counter value at the previous timer interrupt.. */
156 static unsigned int timerhi, timerlo;
157
158 /* expirelo is the count value for next CPU timer interrupt */
159 static unsigned int expirelo;
160
161 /* last time when xtime and rtc are sync'ed up */
162 static long last_rtc_update;
163
164 /* the function pointer to one of the gettimeoffset funcs*/
165 unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
166
167 unsigned long null_gettimeoffset(void)
168 {
169         return 0;
170 }
171
172 unsigned long fixed_rate_gettimeoffset(void)
173 {
174         u32 count;
175         unsigned long res;
176
177         /* Get last timer tick in absolute kernel time */
178         count = read_c0_count();
179
180         /* .. relative to previous jiffy (32 bits is enough) */
181         count -= timerlo;
182
183         __asm__("multu\t%1,%2\n\t"
184                 "mfhi\t%0"
185                 :"=r" (res)
186                 :"r" (count),
187                  "r" (sll32_usecs_per_cycle));
188
189         /*
190          * Due to possible jiffies inconsistencies, we need to check
191          * the result so that we'll get a timer that is monotonic.
192          */
193         if (res >= USECS_PER_JIFFY)
194                 res = USECS_PER_JIFFY-1;
195
196         return res;
197 }
198
199 /*
200  * Cached "1/(clocks per usec)*2^32" value.
201  * It has to be recalculated once each jiffy.
202  */
203 static unsigned long cached_quotient;
204
205 /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
206 static unsigned long last_jiffies = 0;
207
208
209 /*
210  * This is copied from dec/time.c:do_ioasic_gettimeoffset() by Mercij.
211  */
212 unsigned long calibrate_div32_gettimeoffset(void)
213 {
214         u32 count;
215         unsigned long res, tmp;
216         unsigned long quotient;
217
218         tmp = jiffies;
219
220         quotient = cached_quotient;
221
222         if (last_jiffies != tmp) {
223                 last_jiffies = tmp;
224                 if (last_jiffies != 0) {
225                         unsigned long r0;
226                         do_div64_32(r0, timerhi, timerlo, tmp);
227                         do_div64_32(quotient, USECS_PER_JIFFY,
228                                     USECS_PER_JIFFY_FRAC, r0);
229                         cached_quotient = quotient;
230                 }
231         }
232
233         /* Get last timer tick in absolute kernel time */
234         count = read_c0_count();
235
236         /* .. relative to previous jiffy (32 bits is enough) */
237         count -= timerlo;
238
239         __asm__("multu  %2,%3"
240                 : "=l" (tmp), "=h" (res)
241                 : "r" (count), "r" (quotient));
242
243         /*
244          * Due to possible jiffies inconsistencies, we need to check
245          * the result so that we'll get a timer that is monotonic.
246          */
247         if (res >= USECS_PER_JIFFY)
248                 res = USECS_PER_JIFFY - 1;
249
250         return res;
251 }
252
253 unsigned long calibrate_div64_gettimeoffset(void)
254 {
255         u32 count;
256         unsigned long res, tmp;
257         unsigned long quotient;
258
259         tmp = jiffies;
260
261         quotient = cached_quotient;
262
263         if (tmp && last_jiffies != tmp) {
264                 last_jiffies = tmp;
265                 __asm__(".set\tnoreorder\n\t"
266                 ".set\tnoat\n\t"
267                 ".set\tmips3\n\t"
268                 "lwu\t%0,%2\n\t"
269                 "dsll32\t$1,%1,0\n\t"
270                 "or\t$1,$1,%0\n\t"
271                 "ddivu\t$0,$1,%3\n\t"
272                 "mflo\t$1\n\t"
273                 "dsll32\t%0,%4,0\n\t"
274                 "nop\n\t"
275                 "ddivu\t$0,%0,$1\n\t"
276                 "mflo\t%0\n\t"
277                 ".set\tmips0\n\t"
278                 ".set\tat\n\t"
279                 ".set\treorder"
280                 :"=&r" (quotient)
281                 :"r" (timerhi),
282                  "m" (timerlo),
283                  "r" (tmp),
284                  "r" (USECS_PER_JIFFY));
285                 cached_quotient = quotient;
286         }
287
288         /* Get last timer tick in absolute kernel time */
289         count = read_c0_count();
290
291         /* .. relative to previous jiffy (32 bits is enough) */
292         count -= timerlo;
293
294         __asm__("multu\t%1,%2\n\t"
295                 "mfhi\t%0"
296                 :"=r" (res)
297                 :"r" (count),
298                  "r" (quotient));
299
300         /*
301          * Due to possible jiffies inconsistencies, we need to check
302          * the result so that we'll get a timer that is monotonic.
303          */
304         if (res >= USECS_PER_JIFFY)
305                 res = USECS_PER_JIFFY-1;
306
307         return res;
308 }
309
310
311 /*
312  * local_timer_interrupt() does profiling and process accounting
313  * on a per-CPU basis.
314  *
315  * In UP mode, it is invoked from the (global) timer_interrupt.
316  *
317  * In SMP mode, it might invoked by per-CPU timer interrupt, or
318  * a broadcasted inter-processor interrupt which itself is triggered
319  * by the global timer interrupt.
320  */
321 void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
322 {
323         if (!user_mode(regs)) {
324                 if (prof_buffer && current->pid) {
325                         extern int _stext;
326                         unsigned long pc = regs->cp0_epc;
327
328                         pc -= (unsigned long) &_stext;
329                         pc >>= prof_shift;
330                         /*
331                          * Dont ignore out-of-bounds pc values silently,
332                          * put them into the last histogram slot, so if
333                          * present, they will show up as a sharp peak.
334                          */
335                         if (pc > prof_len-1)
336                         pc = prof_len-1;
337                         atomic_inc((atomic_t *)&prof_buffer[pc]);
338                 }
339         }
340
341 #ifdef CONFIG_SMP
342         /* in UP mode, update_process_times() is invoked by do_timer() */
343         update_process_times(user_mode(regs));
344 #endif
345 }
346
347 /*
348  * high-level timer interrupt service routines.  This function
349  * is set as irqaction->handler and is invoked through do_IRQ.
350  */
351 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
352 {
353         if (cpu_has_counter) {
354                 unsigned int count;
355
356                 /* ack timer interrupt, and try to set next interrupt */
357                 expirelo += cycles_per_jiffy;
358                 write_c0_compare(expirelo);
359                 count = read_c0_count();
360
361                 /* check to see if we have missed any timer interrupts */
362                 if ((count - expirelo) < 0x7fffffff) {
363                         /* missed_timer_count ++; */
364                         expirelo = count + cycles_per_jiffy;
365                         write_c0_compare(expirelo);
366                 }
367
368                 /* Update timerhi/timerlo for intra-jiffy calibration. */
369                 timerhi += count < timerlo;     /* Wrap around */
370                 timerlo = count;
371         }
372
373         /*
374          * call the generic timer interrupt handling
375          */
376         do_timer(regs);
377
378         /*
379          * If we have an externally synchronized Linux clock, then update
380          * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
381          * called as close as possible to 500 ms before the new second starts.
382          */
383         write_seqlock(&xtime_lock);
384         if ((time_status & STA_UNSYNC) == 0 &&
385             xtime.tv_sec > last_rtc_update + 660 &&
386             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
387             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
388                 if (rtc_set_time(xtime.tv_sec) == 0) {
389                         last_rtc_update = xtime.tv_sec;
390                 } else {
391                         last_rtc_update = xtime.tv_sec - 600;
392                         /* do it again in 60 s */
393                 }
394         }
395         write_sequnlock(&xtime_lock);
396
397         /*
398          * If jiffies has overflowed in this timer_interrupt we must
399          * update the timer[hi]/[lo] to make fast gettimeoffset funcs
400          * quotient calc still valid. -arca
401          */
402         if (!jiffies) {
403                 timerhi = timerlo = 0;
404         }
405
406 #if !defined(CONFIG_SMP)
407         /*
408          * In UP mode, we call local_timer_interrupt() to do profiling
409          * and process accouting.
410          *
411          * In SMP mode, local_timer_interrupt() is invoked by appropriate
412          * low-level local timer interrupt handler.
413          */
414         local_timer_interrupt(0, NULL, regs);
415
416 #else   /* CONFIG_SMP */
417
418         if (emulate_local_timer_interrupt) {
419                 /*
420                  * this is the place where we send out inter-process
421                  * interrupts and let each CPU do its own profiling
422                  * and process accouting.
423                  *
424                  * Obviously we need to call local_timer_interrupt() for
425                  * the current CPU too.
426                  */
427                 panic("Not implemented yet!!!");
428         }
429 #endif  /* CONFIG_SMP */
430
431         return IRQ_HANDLED;
432 }
433
434 asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
435 {
436         int cpu = smp_processor_id();
437
438         irq_enter();
439         kstat_cpu(cpu).irqs[irq]++;
440
441         /* we keep interrupt disabled all the time */
442         timer_interrupt(irq, NULL, regs);
443
444         irq_exit();
445
446         if (softirq_pending(cpu))
447                 do_softirq();
448 }
449
450 asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
451 {
452         int cpu = smp_processor_id();
453
454         irq_enter();
455         kstat_cpu(cpu).irqs[irq]++;
456
457         /* we keep interrupt disabled all the time */
458         local_timer_interrupt(irq, NULL, regs);
459
460         irq_exit();
461
462         if (softirq_pending(cpu))
463                 do_softirq();
464 }
465
466 /*
467  * time_init() - it does the following things.
468  *
469  * 1) board_time_init() -
470  *      a) (optional) set up RTC routines,
471  *      b) (optional) calibrate and set the mips_counter_frequency
472  *          (only needed if you intended to use fixed_rate_gettimeoffset
473  *           or use cpu counter as timer interrupt source)
474  * 2) setup xtime based on rtc_get_time().
475  * 3) choose a appropriate gettimeoffset routine.
476  * 4) calculate a couple of cached variables for later usage
477  * 5) board_timer_setup() -
478  *      a) (optional) over-write any choices made above by time_init().
479  *      b) machine specific code should setup the timer irqaction.
480  *      c) enable the timer interrupt
481  */
482
483 void (*board_time_init)(void) = NULL;
484 void (*board_timer_setup)(struct irqaction *irq) = NULL;
485
486 unsigned int mips_counter_frequency = 0;
487
488 static struct irqaction timer_irqaction = {
489         timer_interrupt,
490         SA_INTERRUPT,
491         0,
492         "timer",
493         NULL,
494         NULL
495 };
496
497 void __init time_init(void)
498 {
499         if (board_time_init)
500                 board_time_init();
501
502         xtime.tv_sec = rtc_get_time();
503         xtime.tv_nsec = 0;
504         set_normalized_timespec(&wall_to_monotonic,
505                                 -xtime.tv_sec, -xtime.tv_nsec);
506
507         /* choose appropriate gettimeoffset routine */
508         if (!cpu_has_counter) {
509                 /* no cpu counter - sorry */
510                 do_gettimeoffset = null_gettimeoffset;
511         } else if (mips_counter_frequency != 0) {
512                 /* we have cpu counter and know counter frequency! */
513                 do_gettimeoffset = fixed_rate_gettimeoffset;
514         } else if ((current_cpu_data.isa_level == MIPS_CPU_ISA_M32) ||
515                    (current_cpu_data.isa_level == MIPS_CPU_ISA_I) ||
516                    (current_cpu_data.isa_level == MIPS_CPU_ISA_II) ) {
517                 /* we need to calibrate the counter but we don't have
518                  * 64-bit division. */
519                 do_gettimeoffset = calibrate_div32_gettimeoffset;
520         } else {
521                 /* we need to calibrate the counter but we *do* have
522                  * 64-bit division. */
523                 do_gettimeoffset = calibrate_div64_gettimeoffset;
524         }
525
526         /* caclulate cache parameters */
527         if (mips_counter_frequency) {
528                 cycles_per_jiffy = mips_counter_frequency / HZ;
529
530                 /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq */
531                 /* any better way to do this? */
532                 sll32_usecs_per_cycle = mips_counter_frequency / 100000;
533                 sll32_usecs_per_cycle = 0xffffffff / sll32_usecs_per_cycle;
534                 sll32_usecs_per_cycle *= 10;
535
536                 /*
537                  * For those using cpu counter as timer,  this sets up the
538                  * first interrupt
539                  */
540                 write_c0_compare(cycles_per_jiffy);
541                 write_c0_count(0);
542                 expirelo = cycles_per_jiffy;
543         }
544
545         /*
546          * Call board specific timer interrupt setup.
547          *
548          * this pointer must be setup in machine setup routine.
549          *
550          * Even if the machine choose to use low-level timer interrupt,
551          * it still needs to setup the timer_irqaction.
552          * In that case, it might be better to set timer_irqaction.handler
553          * to be NULL function so that we are sure the high-level code
554          * is not invoked accidentally.
555          */
556         board_timer_setup(&timer_irqaction);
557 }
558
559 #define FEBRUARY                2
560 #define STARTOFTIME             1970
561 #define SECDAY                  86400L
562 #define SECYR                   (SECDAY * 365)
563 #define leapyear(year)          ((year) % 4 == 0)
564 #define days_in_year(a)         (leapyear(a) ? 366 : 365)
565 #define days_in_month(a)        (month_days[(a) - 1])
566
567 static int month_days[12] = {
568         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
569 };
570
571 void to_tm(unsigned long tim, struct rtc_time * tm)
572 {
573         long hms, day, gday;
574         int i;
575
576         gday = day = tim / SECDAY;
577         hms = tim % SECDAY;
578
579         /* Hours, minutes, seconds are easy */
580         tm->tm_hour = hms / 3600;
581         tm->tm_min = (hms % 3600) / 60;
582         tm->tm_sec = (hms % 3600) % 60;
583
584         /* Number of years in days */
585         for (i = STARTOFTIME; day >= days_in_year(i); i++)
586         day -= days_in_year(i);
587         tm->tm_year = i;
588
589         /* Number of months in days left */
590         if (leapyear(tm->tm_year))
591         days_in_month(FEBRUARY) = 29;
592         for (i = 1; day >= days_in_month(i); i++)
593         day -= days_in_month(i);
594         days_in_month(FEBRUARY) = 28;
595         tm->tm_mon = i-1;       /* tm_mon starts from 0 to 11 */
596
597         /* Days are what is left over (+1) from all that. */
598         tm->tm_mday = day + 1;
599
600         /*
601          * Determine the day of week
602          */
603         tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
604 }
605
606 EXPORT_SYMBOL(rtc_lock);