3 * Common time routines among all ppc machines.
5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6 * Paul Mackerras' version and mine for PReP and Pmac.
7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
8 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
10 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
11 * to make clock more stable (2.4.0-test5). The only thing
12 * that this code assumes is that the timebases have been synchronized
13 * by firmware on SMP and are never stopped (never do sleep
14 * on SMP then, nap and doze are OK).
16 * Speeded up do_gettimeofday by getting rid of references to
17 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
19 * TODO (not necessarily in this file):
20 * - improve precision and reproducibility of timebase frequency
21 * measurement at boot time. (for iSeries, we calibrate the timebase
22 * against the Titan chip's clock.)
23 * - for astronomical applications: add a new function to get
24 * non ambiguous timestamps even around leap seconds. This needs
25 * a new timestamp format and a good name.
27 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
28 * "A Kernel Model for Precision Timekeeping" by Dave Mills
30 * This program is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU General Public License
32 * as published by the Free Software Foundation; either version
33 * 2 of the License, or (at your option) any later version.
36 #include <linux/config.h>
37 #include <linux/errno.h>
38 #include <linux/module.h>
39 #include <linux/sched.h>
40 #include <linux/kernel.h>
41 #include <linux/param.h>
42 #include <linux/string.h>
44 #include <linux/interrupt.h>
45 #include <linux/timex.h>
46 #include <linux/kernel_stat.h>
47 #include <linux/mc146818rtc.h>
48 #include <linux/time.h>
49 #include <linux/init.h>
50 #include <linux/profile.h>
51 #include <linux/cpu.h>
52 #include <linux/security.h>
54 #include <asm/segment.h>
56 #include <asm/processor.h>
57 #include <asm/nvram.h>
58 #include <asm/cache.h>
59 #include <asm/machdep.h>
60 #ifdef CONFIG_PPC_ISERIES
61 #include <asm/iSeries/ItLpQueue.h>
62 #include <asm/iSeries/HvCallXm.h>
64 #include <asm/uaccess.h>
66 #include <asm/ppcdebug.h>
68 #include <asm/sections.h>
69 #include <asm/systemcfg.h>
70 #include <asm/firmware.h>
72 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
74 EXPORT_SYMBOL(jiffies_64);
76 /* keep track of when we need to update the rtc */
77 time_t last_rtc_update;
78 extern int piranha_simulator;
79 #ifdef CONFIG_PPC_ISERIES
80 unsigned long iSeries_recal_titan = 0;
81 unsigned long iSeries_recal_tb = 0;
82 static unsigned long first_settimeofday = 1;
85 #define XSEC_PER_SEC (1024*1024)
87 unsigned long tb_ticks_per_jiffy;
88 unsigned long tb_ticks_per_usec = 100; /* sane default */
89 EXPORT_SYMBOL(tb_ticks_per_usec);
90 unsigned long tb_ticks_per_sec;
91 unsigned long tb_to_xs;
93 unsigned long processor_freq;
94 DEFINE_SPINLOCK(rtc_lock);
95 EXPORT_SYMBOL_GPL(rtc_lock);
97 unsigned long tb_to_ns_scale;
98 unsigned long tb_to_ns_shift;
100 struct gettimeofday_struct do_gtod;
102 extern unsigned long wall_jiffies;
103 extern int smp_tb_synchronized;
105 extern struct timezone sys_tz;
107 void ppc_adjtimex(void);
109 static unsigned adjusting_time = 0;
111 unsigned long ppc_proc_freq;
112 unsigned long ppc_tb_freq;
114 static __inline__ void timer_check_rtc(void)
117 * update the rtc when needed, this should be performed on the
118 * right fraction of a second. Half or full second ?
119 * Full second works on mk48t59 clocks, others need testing.
120 * Note that this update is basically only used through
121 * the adjtimex system calls. Setting the HW clock in
122 * any other way is a /dev/rtc and userland business.
123 * This is still wrong by -0.5/+1.5 jiffies because of the
124 * timer interrupt resolution and possible delay, but here we
125 * hit a quantization limit which can only be solved by higher
126 * resolution timers and decoupling time management from timer
127 * interrupts. This is also wrong on the clocks
128 * which require being written at the half second boundary.
129 * We should have an rtc call that only sets the minutes and
130 * seconds like on Intel to avoid problems with non UTC clocks.
132 if ( (time_status & STA_UNSYNC) == 0 &&
133 xtime.tv_sec - last_rtc_update >= 659 &&
134 abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
135 jiffies - wall_jiffies == 1) {
137 to_tm(xtime.tv_sec+1, &tm);
140 if (ppc_md.set_rtc_time(&tm) == 0)
141 last_rtc_update = xtime.tv_sec+1;
143 /* Try again one minute later */
144 last_rtc_update += 60;
149 * This version of gettimeofday has microsecond resolution.
151 static inline void __do_gettimeofday(struct timeval *tv, unsigned long tb_val)
153 unsigned long sec, usec, tb_ticks;
154 unsigned long xsec, tb_xsec;
155 struct gettimeofday_vars * temp_varp;
156 unsigned long temp_tb_to_xs, temp_stamp_xsec;
159 * These calculations are faster (gets rid of divides)
160 * if done in units of 1/2^20 rather than microseconds.
161 * The conversion to microseconds at the end is done
162 * without a divide (and in fact, without a multiply)
164 temp_varp = do_gtod.varp;
165 tb_ticks = tb_val - temp_varp->tb_orig_stamp;
166 temp_tb_to_xs = temp_varp->tb_to_xs;
167 temp_stamp_xsec = temp_varp->stamp_xsec;
168 tb_xsec = mulhdu( tb_ticks, temp_tb_to_xs );
169 xsec = temp_stamp_xsec + tb_xsec;
170 sec = xsec / XSEC_PER_SEC;
171 xsec -= sec * XSEC_PER_SEC;
172 usec = (xsec * USEC_PER_SEC)/XSEC_PER_SEC;
178 void do_gettimeofday(struct timeval *tv)
180 __do_gettimeofday(tv, get_tb());
183 EXPORT_SYMBOL(do_gettimeofday);
185 /* Synchronize xtime with do_gettimeofday */
187 static inline void timer_sync_xtime(unsigned long cur_tb)
189 struct timeval my_tv;
191 __do_gettimeofday(&my_tv, cur_tb);
193 if (xtime.tv_sec <= my_tv.tv_sec) {
194 xtime.tv_sec = my_tv.tv_sec;
195 xtime.tv_nsec = my_tv.tv_usec * 1000;
200 * When the timebase - tb_orig_stamp gets too big, we do a manipulation
201 * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
202 * difference tb - tb_orig_stamp small enough to always fit inside a
203 * 32 bits number. This is a requirement of our fast 32 bits userland
204 * implementation in the vdso. If we "miss" a call to this function
205 * (interrupt latency, CPU locked in a spinlock, ...) and we end up
206 * with a too big difference, then the vdso will fallback to calling
209 static __inline__ void timer_recalc_offset(unsigned long cur_tb)
211 struct gettimeofday_vars * temp_varp;
213 unsigned long offset, new_stamp_xsec, new_tb_orig_stamp;
215 if (((cur_tb - do_gtod.varp->tb_orig_stamp) & 0x80000000u) == 0)
218 temp_idx = (do_gtod.var_idx == 0);
219 temp_varp = &do_gtod.vars[temp_idx];
221 new_tb_orig_stamp = cur_tb;
222 offset = new_tb_orig_stamp - do_gtod.varp->tb_orig_stamp;
223 new_stamp_xsec = do_gtod.varp->stamp_xsec + mulhdu(offset, do_gtod.varp->tb_to_xs);
225 temp_varp->tb_to_xs = do_gtod.varp->tb_to_xs;
226 temp_varp->tb_orig_stamp = new_tb_orig_stamp;
227 temp_varp->stamp_xsec = new_stamp_xsec;
229 do_gtod.varp = temp_varp;
230 do_gtod.var_idx = temp_idx;
232 ++(systemcfg->tb_update_count);
234 systemcfg->tb_orig_stamp = new_tb_orig_stamp;
235 systemcfg->stamp_xsec = new_stamp_xsec;
237 ++(systemcfg->tb_update_count);
241 unsigned long profile_pc(struct pt_regs *regs)
243 unsigned long pc = instruction_pointer(regs);
245 if (in_lock_functions(pc))
250 EXPORT_SYMBOL(profile_pc);
253 #ifdef CONFIG_PPC_ISERIES
256 * This function recalibrates the timebase based on the 49-bit time-of-day
257 * value in the Titan chip. The Titan is much more accurate than the value
258 * returned by the service processor for the timebase frequency.
261 static void iSeries_tb_recal(void)
263 struct div_result divres;
264 unsigned long titan, tb;
266 titan = HvCallXm_loadTod();
267 if ( iSeries_recal_titan ) {
268 unsigned long tb_ticks = tb - iSeries_recal_tb;
269 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
270 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
271 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
272 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
274 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
275 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
277 if ( tick_diff < 0 ) {
278 tick_diff = -tick_diff;
282 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
283 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
284 new_tb_ticks_per_jiffy, sign, tick_diff );
285 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
286 tb_ticks_per_sec = new_tb_ticks_per_sec;
287 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
288 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
289 tb_to_xs = divres.result_low;
290 do_gtod.varp->tb_to_xs = tb_to_xs;
291 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
292 systemcfg->tb_to_xs = tb_to_xs;
295 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
296 " new tb_ticks_per_jiffy = %lu\n"
297 " old tb_ticks_per_jiffy = %lu\n",
298 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
302 iSeries_recal_titan = titan;
303 iSeries_recal_tb = tb;
308 * For iSeries shared processors, we have to let the hypervisor
309 * set the hardware decrementer. We set a virtual decrementer
310 * in the lppaca and call the hypervisor if the virtual
311 * decrementer is less than the current value in the hardware
312 * decrementer. (almost always the new decrementer value will
313 * be greater than the current hardware decementer so the hypervisor
314 * call will not be needed)
317 unsigned long tb_last_stamp __cacheline_aligned_in_smp;
320 * timer_interrupt - gets called when the decrementer overflows,
321 * with interrupts disabled.
323 int timer_interrupt(struct pt_regs * regs)
326 unsigned long cur_tb;
327 struct paca_struct *lpaca = get_paca();
328 unsigned long cpu = smp_processor_id();
332 profile_tick(CPU_PROFILING, regs);
334 lpaca->lppaca.int_dword.fields.decr_int = 0;
336 while (lpaca->next_jiffy_update_tb <= (cur_tb = get_tb())) {
338 * We cannot disable the decrementer, so in the period
339 * between this cpu's being marked offline in cpu_online_map
340 * and calling stop-self, it is taking timer interrupts.
341 * Avoid calling into the scheduler rebalancing code if this
344 if (!cpu_is_offline(cpu))
345 update_process_times(user_mode(regs));
347 * No need to check whether cpu is offline here; boot_cpuid
348 * should have been fixed up by now.
350 if (cpu == boot_cpuid) {
351 write_seqlock(&xtime_lock);
352 tb_last_stamp = lpaca->next_jiffy_update_tb;
353 timer_recalc_offset(lpaca->next_jiffy_update_tb);
355 timer_sync_xtime(lpaca->next_jiffy_update_tb);
357 write_sequnlock(&xtime_lock);
358 if ( adjusting_time && (time_adjust == 0) )
361 lpaca->next_jiffy_update_tb += tb_ticks_per_jiffy;
364 next_dec = lpaca->next_jiffy_update_tb - cur_tb;
365 if (next_dec > lpaca->default_decr)
366 next_dec = lpaca->default_decr;
369 #ifdef CONFIG_PPC_ISERIES
370 if (hvlpevent_is_pending())
371 process_hvlpevents(regs);
374 /* collect purr register values often, for accurate calculations */
375 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
376 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
377 cu->current_tb = mfspr(SPRN_PURR);
386 * Scheduler clock - returns current time in nanosec units.
388 * Note: mulhdu(a, b) (multiply high double unsigned) returns
389 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
390 * are 64-bit unsigned numbers.
392 unsigned long long sched_clock(void)
394 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
397 int do_settimeofday(struct timespec *tv)
399 time_t wtm_sec, new_sec = tv->tv_sec;
400 long wtm_nsec, new_nsec = tv->tv_nsec;
402 unsigned long delta_xsec;
404 unsigned long new_xsec;
406 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
409 write_seqlock_irqsave(&xtime_lock, flags);
410 /* Updating the RTC is not the job of this code. If the time is
411 * stepped under NTP, the RTC will be update after STA_UNSYNC
412 * is cleared. Tool like clock/hwclock either copy the RTC
413 * to the system time, in which case there is no point in writing
414 * to the RTC again, or write to the RTC but then they don't call
415 * settimeofday to perform this operation.
417 #ifdef CONFIG_PPC_ISERIES
418 if ( first_settimeofday ) {
420 first_settimeofday = 0;
423 tb_delta = tb_ticks_since(tb_last_stamp);
424 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
426 new_nsec -= tb_delta / tb_ticks_per_usec / 1000;
428 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
429 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
431 set_normalized_timespec(&xtime, new_sec, new_nsec);
432 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
434 /* In case of a large backwards jump in time with NTP, we want the
435 * clock to be updated as soon as the PLL is again in lock.
437 last_rtc_update = new_sec - 658;
439 time_adjust = 0; /* stop active adjtime() */
440 time_status |= STA_UNSYNC;
441 time_maxerror = NTP_PHASE_LIMIT;
442 time_esterror = NTP_PHASE_LIMIT;
444 delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp),
445 do_gtod.varp->tb_to_xs );
447 new_xsec = (new_nsec * XSEC_PER_SEC) / NSEC_PER_SEC;
448 new_xsec += new_sec * XSEC_PER_SEC;
449 if ( new_xsec > delta_xsec ) {
450 do_gtod.varp->stamp_xsec = new_xsec - delta_xsec;
451 systemcfg->stamp_xsec = new_xsec - delta_xsec;
454 /* This is only for the case where the user is setting the time
455 * way back to a time such that the boot time would have been
456 * before 1970 ... eg. we booted ten days ago, and we are setting
457 * the time to Jan 5, 1970 */
458 do_gtod.varp->stamp_xsec = new_xsec;
459 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
460 systemcfg->stamp_xsec = new_xsec;
461 systemcfg->tb_orig_stamp = tb_last_stamp;
464 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
465 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
467 write_sequnlock_irqrestore(&xtime_lock, flags);
472 EXPORT_SYMBOL(do_settimeofday);
474 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_MAPLE) || defined(CONFIG_PPC_BPA)
475 void __init generic_calibrate_decr(void)
477 struct device_node *cpu;
478 struct div_result divres;
483 * The cpu node should have a timebase-frequency property
484 * to tell us the rate at which the decrementer counts.
486 cpu = of_find_node_by_type(NULL, "cpu");
488 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
491 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
499 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
502 ppc_proc_freq = DEFAULT_PROC_FREQ;
505 fp = (unsigned int *)get_property(cpu, "clock-frequency",
513 printk(KERN_ERR "WARNING: Estimating processor frequency "
518 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
519 ppc_tb_freq/1000000, ppc_tb_freq%1000000);
520 printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
521 ppc_proc_freq/1000000, ppc_proc_freq%1000000);
523 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
524 tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
525 tb_ticks_per_usec = ppc_tb_freq / 1000000;
526 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
527 div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
528 tb_to_xs = divres.result_low;
530 setup_default_decr();
534 void __init time_init(void)
536 /* This function is only called on the boot processor */
539 struct div_result res;
540 unsigned long scale, shift;
542 ppc_md.calibrate_decr();
545 * Compute scale factor for sched_clock.
546 * The calibrate_decr() function has set tb_ticks_per_sec,
547 * which is the timebase frequency.
548 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
549 * the 128-bit result as a 64.64 fixed-point number.
550 * We then shift that number right until it is less than 1.0,
551 * giving us the scale factor and shift count to use in
554 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
555 scale = res.result_low;
556 for (shift = 0; res.result_high != 0; ++shift) {
557 scale = (scale >> 1) | (res.result_high << 63);
558 res.result_high >>= 1;
560 tb_to_ns_scale = scale;
561 tb_to_ns_shift = shift;
563 #ifdef CONFIG_PPC_ISERIES
564 if (!piranha_simulator)
566 ppc_md.get_boot_time(&tm);
568 write_seqlock_irqsave(&xtime_lock, flags);
569 xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
570 tm.tm_hour, tm.tm_min, tm.tm_sec);
571 tb_last_stamp = get_tb();
572 do_gtod.varp = &do_gtod.vars[0];
574 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
575 get_paca()->next_jiffy_update_tb = tb_last_stamp + tb_ticks_per_jiffy;
576 do_gtod.varp->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
577 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
578 do_gtod.varp->tb_to_xs = tb_to_xs;
579 do_gtod.tb_to_us = tb_to_us;
580 systemcfg->tb_orig_stamp = tb_last_stamp;
581 systemcfg->tb_update_count = 0;
582 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
583 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
584 systemcfg->tb_to_xs = tb_to_xs;
589 last_rtc_update = xtime.tv_sec;
590 set_normalized_timespec(&wall_to_monotonic,
591 -xtime.tv_sec, -xtime.tv_nsec);
592 write_sequnlock_irqrestore(&xtime_lock, flags);
594 /* Not exact, but the timer interrupt takes care of this */
595 set_dec(tb_ticks_per_jiffy);
599 * After adjtimex is called, adjust the conversion of tb ticks
600 * to microseconds to keep do_gettimeofday synchronized
603 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
604 * adjust the frequency.
607 /* #define DEBUG_PPC_ADJTIMEX 1 */
609 void ppc_adjtimex(void)
611 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec;
612 unsigned long tb_ticks_per_sec_delta;
613 long delta_freq, ltemp;
614 struct div_result divres;
616 struct gettimeofday_vars * temp_varp;
618 long singleshot_ppm = 0;
620 /* Compute parts per million frequency adjustment to accomplish the time adjustment
621 implied by time_offset to be applied over the elapsed time indicated by time_constant.
622 Use SHIFT_USEC to get it into the same units as time_freq. */
623 if ( time_offset < 0 ) {
624 ltemp = -time_offset;
625 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
626 ltemp >>= SHIFT_KG + time_constant;
631 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
632 ltemp >>= SHIFT_KG + time_constant;
635 /* If there is a single shot time adjustment in progress */
637 #ifdef DEBUG_PPC_ADJTIMEX
638 printk("ppc_adjtimex: ");
639 if ( adjusting_time == 0 )
641 printk("single shot time_adjust = %ld\n", time_adjust);
646 /* Compute parts per million frequency adjustment to match time_adjust */
647 singleshot_ppm = tickadj * HZ;
649 * The adjustment should be tickadj*HZ to match the code in
650 * linux/kernel/timer.c, but experiments show that this is too
651 * large. 3/4 of tickadj*HZ seems about right
653 singleshot_ppm -= singleshot_ppm / 4;
654 /* Use SHIFT_USEC to get it into the same units as time_freq */
655 singleshot_ppm <<= SHIFT_USEC;
656 if ( time_adjust < 0 )
657 singleshot_ppm = -singleshot_ppm;
660 #ifdef DEBUG_PPC_ADJTIMEX
661 if ( adjusting_time )
662 printk("ppc_adjtimex: ending single shot time_adjust\n");
667 /* Add up all of the frequency adjustments */
668 delta_freq = time_freq + ltemp + singleshot_ppm;
670 /* Compute a new value for tb_ticks_per_sec based on the frequency adjustment */
671 den = 1000000 * (1 << (SHIFT_USEC - 8));
672 if ( delta_freq < 0 ) {
673 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
674 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
677 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
678 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
681 #ifdef DEBUG_PPC_ADJTIMEX
682 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
683 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
686 /* Compute a new value of tb_to_xs (used to convert tb to microseconds and a new value of
687 stamp_xsec which is the time (in 1/2^20 second units) corresponding to tb_orig_stamp. This
688 new value of stamp_xsec compensates for the change in frequency (implied by the new tb_to_xs)
689 which guarantees that the current time remains the same */
690 write_seqlock_irqsave( &xtime_lock, flags );
691 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
692 div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres );
693 new_tb_to_xs = divres.result_low;
694 new_xsec = mulhdu( tb_ticks, new_tb_to_xs );
696 old_xsec = mulhdu( tb_ticks, do_gtod.varp->tb_to_xs );
697 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
699 /* There are two copies of tb_to_xs and stamp_xsec so that no lock is needed to access and use these
700 values in do_gettimeofday. We alternate the copies and as long as a reasonable time elapses between
701 changes, there will never be inconsistent values. ntpd has a minimum of one minute between updates */
703 temp_idx = (do_gtod.var_idx == 0);
704 temp_varp = &do_gtod.vars[temp_idx];
706 temp_varp->tb_to_xs = new_tb_to_xs;
707 temp_varp->stamp_xsec = new_stamp_xsec;
708 temp_varp->tb_orig_stamp = do_gtod.varp->tb_orig_stamp;
710 do_gtod.varp = temp_varp;
711 do_gtod.var_idx = temp_idx;
714 * tb_update_count is used to allow the problem state gettimeofday code
715 * to assure itself that it sees a consistent view of the tb_to_xs and
716 * stamp_xsec variables. It reads the tb_update_count, then reads
717 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
718 * the two values of tb_update_count match and are even then the
719 * tb_to_xs and stamp_xsec values are consistent. If not, then it
720 * loops back and reads them again until this criteria is met.
722 ++(systemcfg->tb_update_count);
724 systemcfg->tb_to_xs = new_tb_to_xs;
725 systemcfg->stamp_xsec = new_stamp_xsec;
727 ++(systemcfg->tb_update_count);
729 write_sequnlock_irqrestore( &xtime_lock, flags );
734 #define TICK_SIZE tick
736 #define STARTOFTIME 1970
737 #define SECDAY 86400L
738 #define SECYR (SECDAY * 365)
739 #define leapyear(year) ((year) % 4 == 0)
740 #define days_in_year(a) (leapyear(a) ? 366 : 365)
741 #define days_in_month(a) (month_days[(a) - 1])
743 static int month_days[12] = {
744 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
748 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
750 void GregorianDay(struct rtc_time * tm)
755 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
757 lastYear=tm->tm_year-1;
760 * Number of leap corrections to apply up to end of last year
762 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
765 * This year is a leap year if it is divisible by 4 except when it is
766 * divisible by 100 unless it is divisible by 400
768 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
770 if((tm->tm_year%4==0) &&
771 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
775 * We are past Feb. 29 in a leap year
784 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
790 void to_tm(int tim, struct rtc_time * tm)
793 register long hms, day;
798 /* Hours, minutes, seconds are easy */
799 tm->tm_hour = hms / 3600;
800 tm->tm_min = (hms % 3600) / 60;
801 tm->tm_sec = (hms % 3600) % 60;
803 /* Number of years in days */
804 for (i = STARTOFTIME; day >= days_in_year(i); i++)
805 day -= days_in_year(i);
808 /* Number of months in days left */
809 if (leapyear(tm->tm_year))
810 days_in_month(FEBRUARY) = 29;
811 for (i = 1; day >= days_in_month(i); i++)
812 day -= days_in_month(i);
813 days_in_month(FEBRUARY) = 28;
816 /* Days are what is left over (+1) from all that. */
817 tm->tm_mday = day + 1;
820 * Determine the day of week
825 /* Auxiliary function to compute scaling factors */
826 /* Actually the choice of a timebase running at 1/4 the of the bus
827 * frequency giving resolution of a few tens of nanoseconds is quite nice.
828 * It makes this computation very precise (27-28 bits typically) which
829 * is optimistic considering the stability of most processor clock
830 * oscillators and the precision with which the timebase frequency
831 * is measured but does not harm.
833 unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
834 unsigned mlt=0, tmp, err;
835 /* No concern for performance, it's done once: use a stupid
836 * but safe and compact method to find the multiplier.
839 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
840 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
843 /* We might still be off by 1 for the best approximation.
844 * A side effect of this is that if outscale is too large
845 * the returned value will be zero.
846 * Many corner cases have been checked and seem to work,
847 * some might have been forgotten in the test however.
850 err = inscale*(mlt+1);
851 if (err <= inscale/2) mlt++;
856 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
860 void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
861 unsigned divisor, struct div_result *dr )
863 unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
865 a = dividend_high >> 32;
866 b = dividend_high & 0xffffffff;
867 c = dividend_low >> 32;
868 d = dividend_low & 0xffffffff;
871 ra = (a - (w * divisor)) << 32;
873 x = (ra + b)/divisor;
874 rb = ((ra + b) - (x * divisor)) << 32;
876 y = (rb + c)/divisor;
877 rc = ((rb + b) - (y * divisor)) << 32;
879 z = (rc + d)/divisor;
881 dr->result_high = (w << 32) + x;
882 dr->result_low = (y << 32) + z;