1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/config.h>
37#include <linux/errno.h>
38#include <linux/sched.h>
39#include <linux/kernel.h>
40#include <linux/param.h>
41#include <linux/string.h>
42#include <linux/mm.h>
43#include <linux/interrupt.h>
44#include <linux/timex.h>
45#include <linux/kernel_stat.h>
46#include <linux/mc146818rtc.h>
47#include <linux/time.h>
48#include <linux/init.h>
49
50#include <asm/naca.h>
51#include <asm/segment.h>
52#include <asm/io.h>
53#include <asm/processor.h>
54#include <asm/nvram.h>
55#include <asm/cache.h>
56#include <asm/machdep.h>
57#include <asm/init.h>
58#ifdef CONFIG_PPC_ISERIES
59#include <asm/iSeries/HvCallXm.h>
60#endif
61#include <asm/uaccess.h>
62
63#include <asm/time.h>
64#include <asm/ppcdebug.h>
65
66void smp_local_timer_interrupt(struct pt_regs *);
67
68extern void setup_before_console_init();
69
70
71time_t last_rtc_update;
72extern rwlock_t xtime_lock;
73extern int piranha_simulator;
74#ifdef CONFIG_PPC_ISERIES
75unsigned long iSeries_recal_titan = 0;
76unsigned long iSeries_recal_tb = 0;
77static unsigned long first_settimeofday = 1;
78#endif
79
80#define XSEC_PER_SEC (1024*1024)
81#define USEC_PER_SEC (1000000)
82
83unsigned long tb_ticks_per_jiffy;
84unsigned long tb_ticks_per_usec;
85unsigned long tb_ticks_per_sec;
86unsigned long next_xtime_sync_tb;
87unsigned long xtime_sync_interval;
88unsigned long tb_to_xs;
89unsigned long processor_freq;
90spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
91
92extern unsigned long wall_jiffies;
93extern unsigned long lpEvent_count;
94extern int smp_tb_synchronized;
95
96extern unsigned long prof_cpu_mask;
97extern unsigned int * prof_buffer;
98extern unsigned long prof_len;
99extern unsigned long prof_shift;
100extern char _stext;
101
102extern struct timezone sys_tz;
103
104void ppc_adjtimex(void);
105
106static unsigned adjusting_time = 0;
107
108static void ppc_do_profile (unsigned long nip)
109{
110
111
112
113
114 if (!((1<<smp_processor_id()) & prof_cpu_mask))
115 return;
116
117 nip -= (unsigned long) &_stext;
118 nip >>= prof_shift;
119
120
121
122
123
124 if (nip > prof_len-1)
125 nip = prof_len-1;
126 atomic_inc((atomic_t *)&prof_buffer[nip]);
127}
128
129
130static __inline__ void timer_check_rtc(void)
131{
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 if ( (time_status & STA_UNSYNC) == 0 &&
149 xtime.tv_sec - last_rtc_update >= 659 &&
150 abs(xtime.tv_usec - (1000000-1000000/HZ)) < 500000/HZ &&
151 jiffies - wall_jiffies == 1) {
152 struct rtc_time tm;
153 to_tm(xtime.tv_sec+1, &tm);
154 tm.tm_year -= 1900;
155 tm.tm_mon -= 1;
156 if (ppc_md.set_rtc_time(&tm) == 0)
157 last_rtc_update = xtime.tv_sec+1;
158 else
159
160 last_rtc_update += 60;
161 }
162}
163
164
165
166static __inline__ void timer_sync_xtime( unsigned long cur_tb )
167{
168 struct timeval my_tv;
169
170 if ( cur_tb > next_xtime_sync_tb ) {
171 next_xtime_sync_tb = cur_tb + xtime_sync_interval;
172 do_gettimeofday( &my_tv );
173 if ( xtime.tv_sec <= my_tv.tv_sec ) {
174 xtime.tv_sec = my_tv.tv_sec;
175 xtime.tv_usec = my_tv.tv_usec;
176 }
177 }
178}
179
180#ifdef CONFIG_PPC_ISERIES
181
182
183
184
185
186
187
188static void iSeries_tb_recal(void)
189{
190 struct div_result divres;
191 unsigned long titan, tb;
192 tb = get_tb();
193 titan = HvCallXm_loadTod();
194 if ( iSeries_recal_titan ) {
195 unsigned long tb_ticks = tb - iSeries_recal_tb;
196 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
197 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
198 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
199 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
200 char sign = '+';
201
202 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
203
204 if ( tick_diff < 0 ) {
205 tick_diff = -tick_diff;
206 sign = '-';
207 }
208 if ( tick_diff ) {
209 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
210 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
211 new_tb_ticks_per_jiffy, sign, tick_diff );
212 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
213 tb_ticks_per_sec = new_tb_ticks_per_sec;
214 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
215 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
216 tb_to_xs = divres.result_low;
217 systemcfg->tb_to_xs = tb_to_xs;
218 }
219 else {
220 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
221 " new tb_ticks_per_jiffy = %lu\n"
222 " old tb_ticks_per_jiffy = %lu\n",
223 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
224 }
225 }
226 }
227 iSeries_recal_titan = titan;
228 iSeries_recal_tb = tb;
229}
230#endif
231
232
233
234
235
236
237
238
239
240
241
242unsigned long tb_last_stamp=0;
243
244
245
246
247
248int timer_interrupt(struct pt_regs * regs)
249{
250 int next_dec;
251 unsigned long cur_tb;
252 struct paca_struct *lpaca = get_paca();
253 unsigned long cpu = lpaca->xPacaIndex;
254 struct ItLpQueue * lpq;
255
256 irq_enter(cpu);
257
258 if ((!user_mode(regs)) && (prof_buffer))
259 ppc_do_profile(instruction_pointer(regs));
260
261 pmc_timeslice_tick();
262
263 lpaca->xLpPaca.xIntDword.xFields.xDecrInt = 0;
264
265 while (lpaca->next_jiffy_update_tb <= (cur_tb = get_tb())) {
266
267#ifdef CONFIG_SMP
268 smp_local_timer_interrupt(regs);
269#endif
270 if (cpu == 0) {
271 write_lock(&xtime_lock);
272 tb_last_stamp = lpaca->next_jiffy_update_tb;
273 do_timer(regs);
274 timer_sync_xtime( cur_tb );
275 timer_check_rtc();
276 write_unlock(&xtime_lock);
277 if ( adjusting_time && (time_adjust == 0) )
278 ppc_adjtimex();
279 }
280 lpaca->next_jiffy_update_tb += tb_ticks_per_jiffy;
281 }
282
283 next_dec = lpaca->next_jiffy_update_tb - cur_tb;
284 if (next_dec > lpaca->default_decr)
285 next_dec = lpaca->default_decr;
286 set_dec(next_dec);
287
288 lpq = lpaca->lpQueuePtr;
289 if (lpq && ItLpQueue_isLpIntPending(lpq))
290 lpEvent_count += ItLpQueue_process(lpq, regs);
291
292 irq_exit(cpu);
293
294 if (softirq_pending(cpu))
295 do_softirq();
296
297 return 1;
298}
299
300
301
302
303
304void do_gettimeofday(struct timeval *tv)
305{
306 unsigned long sec, usec, tb_ticks;
307 unsigned long xsec, tb_xsec;
308 unsigned long temp_tb_to_xs, temp_stamp_xsec;
309 unsigned long tb_count_1, tb_count_2;
310 unsigned long always_zero;
311 struct systemcfg *gtdp;
312
313 gtdp = systemcfg;
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 do {
336 tb_ticks = get_tb() - gtdp->tb_orig_stamp;
337
338 tb_count_1 = gtdp->tb_update_count;
339
340 __asm__ __volatile__ (
341" andc %0,%2,%2\n\
342 add %1,%3,%0\n\
343" : "=&r"(always_zero), "=r"(gtdp)
344 : "r"(tb_count_1), "r"(gtdp) );
345
346 temp_tb_to_xs = gtdp->tb_to_xs;
347 temp_stamp_xsec = gtdp->stamp_xsec;
348
349 __asm__ __volatile__ (
350" add %0,%2,%3\n\
351 andc %0,%0,%0\n\
352 add %1,%4,%0\n\
353" : "=&r"(always_zero), "=r"(gtdp)
354 : "r"(temp_stamp_xsec), "r"(temp_tb_to_xs), "r"(gtdp) );
355
356 tb_count_2 = gtdp->tb_update_count;
357
358 } while ( tb_count_2 - ( tb_count_1 & 0xfffffffffffffffe ) );
359
360
361
362
363
364 tb_xsec = mulhdu( tb_ticks, temp_tb_to_xs );
365 xsec = temp_stamp_xsec + tb_xsec;
366 sec = xsec / XSEC_PER_SEC;
367 xsec -= sec * XSEC_PER_SEC;
368 usec = (xsec * USEC_PER_SEC)/XSEC_PER_SEC;
369
370 tv->tv_sec = sec;
371 tv->tv_usec = usec;
372}
373
374void do_settimeofday(struct timeval *tv)
375{
376 unsigned long flags;
377 unsigned long delta_xsec;
378 long int tb_delta, new_usec, new_sec;
379 unsigned long new_xsec;
380
381 write_lock_irqsave(&xtime_lock, flags);
382
383
384
385
386
387
388
389#ifdef CONFIG_PPC_ISERIES
390 if ( first_settimeofday ) {
391 iSeries_tb_recal();
392 first_settimeofday = 0;
393 }
394#endif
395 tb_delta = tb_ticks_since(tb_last_stamp);
396 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
397
398 new_sec = tv->tv_sec;
399 new_usec = tv->tv_usec - tb_delta / tb_ticks_per_usec;
400 while (new_usec <0) {
401 new_sec--;
402 new_usec += USEC_PER_SEC;
403 }
404 xtime.tv_usec = new_usec;
405 xtime.tv_sec = new_sec;
406
407
408
409
410 last_rtc_update = new_sec - 658;
411
412 time_adjust = 0;
413 time_status |= STA_UNSYNC;
414 time_maxerror = NTP_PHASE_LIMIT;
415 time_esterror = NTP_PHASE_LIMIT;
416
417 delta_xsec = mulhdu( (tb_last_stamp-systemcfg->tb_orig_stamp), systemcfg->tb_to_xs );
418 new_xsec = (tv->tv_usec * XSEC_PER_SEC) / USEC_PER_SEC;
419 new_xsec += tv->tv_sec * XSEC_PER_SEC;
420 if ( new_xsec > delta_xsec ) {
421 systemcfg->stamp_xsec = new_xsec - delta_xsec;
422 }
423 else {
424
425
426
427
428 systemcfg->stamp_xsec = new_xsec;
429 systemcfg->tb_orig_stamp = tb_last_stamp;
430 }
431
432 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
433 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
434
435 write_unlock_irqrestore(&xtime_lock, flags);
436}
437
438
439
440
441
442
443
444long ppc64_sys32_stime(int* tptr)
445{
446 int value;
447 struct timeval myTimeval;
448
449 if (!capable(CAP_SYS_TIME))
450 return -EPERM;
451
452 if (get_user(value, tptr))
453 return -EFAULT;
454
455 myTimeval.tv_sec = value;
456 myTimeval.tv_usec = 0;
457
458 do_settimeofday(&myTimeval);
459
460 return 0;
461}
462
463
464
465
466
467
468
469long ppc64_sys_stime(long* tptr)
470{
471 long value;
472 struct timeval myTimeval;
473
474 if (!capable(CAP_SYS_TIME))
475 return -EPERM;
476
477 if (get_user(value, tptr))
478 return -EFAULT;
479
480 myTimeval.tv_sec = value;
481 myTimeval.tv_usec = 0;
482
483 do_settimeofday(&myTimeval);
484
485 return 0;
486}
487
488void __init time_init(void)
489{
490
491 unsigned long flags;
492 struct rtc_time tm;
493
494 ppc_md.calibrate_decr();
495
496 if ( ! piranha_simulator ) {
497 ppc_md.get_boot_time(&tm);
498 }
499 write_lock_irqsave(&xtime_lock, flags);
500 xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
501 tm.tm_hour, tm.tm_min, tm.tm_sec);
502 tb_last_stamp = get_tb();
503 systemcfg->tb_orig_stamp = tb_last_stamp;
504 systemcfg->tb_update_count = 0;
505 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
506 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
507 systemcfg->tb_to_xs = tb_to_xs;
508
509 xtime_sync_interval = tb_ticks_per_sec - (tb_ticks_per_sec/8);
510 next_xtime_sync_tb = tb_last_stamp + xtime_sync_interval;
511
512 time_freq = 0;
513
514 xtime.tv_usec = 0;
515 last_rtc_update = xtime.tv_sec;
516 write_unlock_irqrestore(&xtime_lock, flags);
517
518
519 set_dec(tb_ticks_per_jiffy);
520
521
522 setup_before_console_init();
523}
524
525
526
527
528
529
530
531
532
533
534
535
536void ppc_adjtimex(void)
537{
538 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec;
539 unsigned long tb_ticks_per_sec_delta;
540 long delta_freq, ltemp;
541 struct div_result divres;
542 unsigned long flags;
543 long singleshot_ppm = 0;
544
545
546
547
548 if ( time_offset < 0 ) {
549 ltemp = -time_offset;
550 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
551 ltemp >>= SHIFT_KG + time_constant;
552 ltemp = -ltemp;
553 }
554 else {
555 ltemp = time_offset;
556 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
557 ltemp >>= SHIFT_KG + time_constant;
558 }
559
560
561 if ( time_adjust ) {
562#ifdef DEBUG_PPC_ADJTIMEX
563 printk("ppc_adjtimex: ");
564 if ( adjusting_time == 0 )
565 printk("starting ");
566 printk("single shot time_adjust = %ld\n", time_adjust);
567#endif
568
569 adjusting_time = 1;
570
571
572 singleshot_ppm = tickadj * HZ;
573
574
575
576
577
578 singleshot_ppm -= singleshot_ppm / 4;
579
580 singleshot_ppm <<= SHIFT_USEC;
581 if ( time_adjust < 0 )
582 singleshot_ppm = -singleshot_ppm;
583 }
584 else {
585#ifdef DEBUG_PPC_ADJTIMEX
586 if ( adjusting_time )
587 printk("ppc_adjtimex: ending single shot time_adjust\n");
588#endif
589 adjusting_time = 0;
590 }
591
592
593 delta_freq = time_freq + ltemp + singleshot_ppm;
594
595
596 den = 1000000 * (1 << (SHIFT_USEC - 8));
597 if ( delta_freq < 0 ) {
598 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
599 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
600 }
601 else {
602 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
603 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
604 }
605
606#ifdef DEBUG_PPC_ADJTIMEX
607 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
608 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
609#endif
610
611
612
613
614
615
616
617
618
619 tb_ticks = get_tb() - systemcfg->tb_orig_stamp;
620 div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres );
621 new_tb_to_xs = divres.result_low;
622 new_xsec = mulhdu( tb_ticks, new_tb_to_xs );
623
624 write_lock_irqsave( &xtime_lock, flags );
625 old_xsec = mulhdu( tb_ticks, systemcfg->tb_to_xs );
626 new_stamp_xsec = systemcfg->stamp_xsec + old_xsec - new_xsec;
627
628
629
630
631
632
633
634
635
636
637 ++(systemcfg->tb_update_count);
638 wmb();
639 systemcfg->tb_to_xs = new_tb_to_xs;
640 systemcfg->stamp_xsec = new_stamp_xsec;
641 wmb();
642 ++(systemcfg->tb_update_count);
643
644 write_unlock_irqrestore( &xtime_lock, flags );
645
646}
647
648
649#define TICK_SIZE tick
650#define FEBRUARY 2
651#define STARTOFTIME 1970
652#define SECDAY 86400L
653#define SECYR (SECDAY * 365)
654#define leapyear(year) ((year) % 4 == 0)
655#define days_in_year(a) (leapyear(a) ? 366 : 365)
656#define days_in_month(a) (month_days[(a) - 1])
657
658static int month_days[12] = {
659 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
660};
661
662
663
664
665void GregorianDay(struct rtc_time * tm)
666{
667 int leapsToDate;
668 int lastYear;
669 int day;
670 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
671
672 lastYear=tm->tm_year-1;
673
674
675
676
677 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
678
679
680
681
682
683
684
685 if((tm->tm_year%4==0) &&
686 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
687 (tm->tm_mon>2))
688 {
689
690
691
692 day=1;
693 }
694 else
695 {
696 day=0;
697 }
698
699 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
700 tm->tm_mday;
701
702 tm->tm_wday=day%7;
703}
704
705void to_tm(int tim, struct rtc_time * tm)
706{
707 register int i;
708 register long hms, day;
709
710 day = tim / SECDAY;
711 hms = tim % SECDAY;
712
713
714 tm->tm_hour = hms / 3600;
715 tm->tm_min = (hms % 3600) / 60;
716 tm->tm_sec = (hms % 3600) % 60;
717
718
719 for (i = STARTOFTIME; day >= days_in_year(i); i++)
720 day -= days_in_year(i);
721 tm->tm_year = i;
722
723
724 if (leapyear(tm->tm_year))
725 days_in_month(FEBRUARY) = 29;
726 for (i = 1; day >= days_in_month(i); i++)
727 day -= days_in_month(i);
728 days_in_month(FEBRUARY) = 28;
729 tm->tm_mon = i;
730
731
732 tm->tm_mday = day + 1;
733
734
735
736
737 GregorianDay(tm);
738}
739
740#if 0
741
742
743
744
745
746
747
748
749unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
750 unsigned mlt=0, tmp, err;
751
752
753
754
755 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
756 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
757 }
758
759
760
761
762
763
764
765
766 err = inscale*(mlt+1);
767 if (err <= inscale/2) mlt++;
768 return mlt;
769 }
770#endif
771
772
773
774
775
776
777void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
778 unsigned divisor, struct div_result *dr )
779{
780 unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
781
782 a = dividend_high >> 32;
783 b = dividend_high & 0xffffffff;
784 c = dividend_low >> 32;
785 d = dividend_low & 0xffffffff;
786
787 w = a/divisor;
788 ra = (a - (w * divisor)) << 32;
789
790 x = (ra + b)/divisor;
791 rb = ((ra + b) - (x * divisor)) << 32;
792
793 y = (rb + c)/divisor;
794 rc = ((rb + b) - (y * divisor)) << 32;
795
796 z = (rc + d)/divisor;
797
798 dr->result_high = (w << 32) + x;
799 dr->result_low = (y << 32) + z;
800
801}
802
803