aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ppc64/kernel/time.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /arch/ppc64/kernel/time.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'arch/ppc64/kernel/time.c')
-rw-r--r--arch/ppc64/kernel/time.c827
1 files changed, 827 insertions, 0 deletions
diff --git a/arch/ppc64/kernel/time.c b/arch/ppc64/kernel/time.c
new file mode 100644
index 000000000000..77ded5a363b6
--- /dev/null
+++ b/arch/ppc64/kernel/time.c
@@ -0,0 +1,827 @@
1/*
2 *
3 * Common time routines among all ppc machines.
4 *
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)
9 *
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).
15 *
16 * Speeded up do_gettimeofday by getting rid of references to
17 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
18 *
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.
26 *
27 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
28 * "A Kernel Model for Precision Timekeeping" by Dave Mills
29 *
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.
34 */
35
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>
43#include <linux/mm.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>
53
54#include <asm/segment.h>
55#include <asm/io.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>
63#endif
64#include <asm/uaccess.h>
65#include <asm/time.h>
66#include <asm/ppcdebug.h>
67#include <asm/prom.h>
68#include <asm/sections.h>
69#include <asm/systemcfg.h>
70
71u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
72
73EXPORT_SYMBOL(jiffies_64);
74
75/* keep track of when we need to update the rtc */
76time_t last_rtc_update;
77extern int piranha_simulator;
78#ifdef CONFIG_PPC_ISERIES
79unsigned long iSeries_recal_titan = 0;
80unsigned long iSeries_recal_tb = 0;
81static unsigned long first_settimeofday = 1;
82#endif
83
84#define XSEC_PER_SEC (1024*1024)
85
86unsigned long tb_ticks_per_jiffy;
87unsigned long tb_ticks_per_usec = 100; /* sane default */
88EXPORT_SYMBOL(tb_ticks_per_usec);
89unsigned long tb_ticks_per_sec;
90unsigned long tb_to_xs;
91unsigned tb_to_us;
92unsigned long processor_freq;
93DEFINE_SPINLOCK(rtc_lock);
94
95unsigned long tb_to_ns_scale;
96unsigned long tb_to_ns_shift;
97
98struct gettimeofday_struct do_gtod;
99
100extern unsigned long wall_jiffies;
101extern unsigned long lpevent_count;
102extern int smp_tb_synchronized;
103
104extern struct timezone sys_tz;
105
106void ppc_adjtimex(void);
107
108static unsigned adjusting_time = 0;
109
110static __inline__ void timer_check_rtc(void)
111{
112 /*
113 * update the rtc when needed, this should be performed on the
114 * right fraction of a second. Half or full second ?
115 * Full second works on mk48t59 clocks, others need testing.
116 * Note that this update is basically only used through
117 * the adjtimex system calls. Setting the HW clock in
118 * any other way is a /dev/rtc and userland business.
119 * This is still wrong by -0.5/+1.5 jiffies because of the
120 * timer interrupt resolution and possible delay, but here we
121 * hit a quantization limit which can only be solved by higher
122 * resolution timers and decoupling time management from timer
123 * interrupts. This is also wrong on the clocks
124 * which require being written at the half second boundary.
125 * We should have an rtc call that only sets the minutes and
126 * seconds like on Intel to avoid problems with non UTC clocks.
127 */
128 if ( (time_status & STA_UNSYNC) == 0 &&
129 xtime.tv_sec - last_rtc_update >= 659 &&
130 abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
131 jiffies - wall_jiffies == 1) {
132 struct rtc_time tm;
133 to_tm(xtime.tv_sec+1, &tm);
134 tm.tm_year -= 1900;
135 tm.tm_mon -= 1;
136 if (ppc_md.set_rtc_time(&tm) == 0)
137 last_rtc_update = xtime.tv_sec+1;
138 else
139 /* Try again one minute later */
140 last_rtc_update += 60;
141 }
142}
143
144/*
145 * This version of gettimeofday has microsecond resolution.
146 */
147static inline void __do_gettimeofday(struct timeval *tv, unsigned long tb_val)
148{
149 unsigned long sec, usec, tb_ticks;
150 unsigned long xsec, tb_xsec;
151 struct gettimeofday_vars * temp_varp;
152 unsigned long temp_tb_to_xs, temp_stamp_xsec;
153
154 /*
155 * These calculations are faster (gets rid of divides)
156 * if done in units of 1/2^20 rather than microseconds.
157 * The conversion to microseconds at the end is done
158 * without a divide (and in fact, without a multiply)
159 */
160 temp_varp = do_gtod.varp;
161 tb_ticks = tb_val - temp_varp->tb_orig_stamp;
162 temp_tb_to_xs = temp_varp->tb_to_xs;
163 temp_stamp_xsec = temp_varp->stamp_xsec;
164 tb_xsec = mulhdu( tb_ticks, temp_tb_to_xs );
165 xsec = temp_stamp_xsec + tb_xsec;
166 sec = xsec / XSEC_PER_SEC;
167 xsec -= sec * XSEC_PER_SEC;
168 usec = (xsec * USEC_PER_SEC)/XSEC_PER_SEC;
169
170 tv->tv_sec = sec;
171 tv->tv_usec = usec;
172}
173
174void do_gettimeofday(struct timeval *tv)
175{
176 __do_gettimeofday(tv, get_tb());
177}
178
179EXPORT_SYMBOL(do_gettimeofday);
180
181/* Synchronize xtime with do_gettimeofday */
182
183static inline void timer_sync_xtime(unsigned long cur_tb)
184{
185 struct timeval my_tv;
186
187 __do_gettimeofday(&my_tv, cur_tb);
188
189 if (xtime.tv_sec <= my_tv.tv_sec) {
190 xtime.tv_sec = my_tv.tv_sec;
191 xtime.tv_nsec = my_tv.tv_usec * 1000;
192 }
193}
194
195/*
196 * When the timebase - tb_orig_stamp gets too big, we do a manipulation
197 * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
198 * difference tb - tb_orig_stamp small enough to always fit inside a
199 * 32 bits number. This is a requirement of our fast 32 bits userland
200 * implementation in the vdso. If we "miss" a call to this function
201 * (interrupt latency, CPU locked in a spinlock, ...) and we end up
202 * with a too big difference, then the vdso will fallback to calling
203 * the syscall
204 */
205static __inline__ void timer_recalc_offset(unsigned long cur_tb)
206{
207 struct gettimeofday_vars * temp_varp;
208 unsigned temp_idx;
209 unsigned long offset, new_stamp_xsec, new_tb_orig_stamp;
210
211 if (((cur_tb - do_gtod.varp->tb_orig_stamp) & 0x80000000u) == 0)
212 return;
213
214 temp_idx = (do_gtod.var_idx == 0);
215 temp_varp = &do_gtod.vars[temp_idx];
216
217 new_tb_orig_stamp = cur_tb;
218 offset = new_tb_orig_stamp - do_gtod.varp->tb_orig_stamp;
219 new_stamp_xsec = do_gtod.varp->stamp_xsec + mulhdu(offset, do_gtod.varp->tb_to_xs);
220
221 temp_varp->tb_to_xs = do_gtod.varp->tb_to_xs;
222 temp_varp->tb_orig_stamp = new_tb_orig_stamp;
223 temp_varp->stamp_xsec = new_stamp_xsec;
224 mb();
225 do_gtod.varp = temp_varp;
226 do_gtod.var_idx = temp_idx;
227
228 ++(systemcfg->tb_update_count);
229 wmb();
230 systemcfg->tb_orig_stamp = new_tb_orig_stamp;
231 systemcfg->stamp_xsec = new_stamp_xsec;
232 wmb();
233 ++(systemcfg->tb_update_count);
234}
235
236#ifdef CONFIG_SMP
237unsigned long profile_pc(struct pt_regs *regs)
238{
239 unsigned long pc = instruction_pointer(regs);
240
241 if (in_lock_functions(pc))
242 return regs->link;
243
244 return pc;
245}
246EXPORT_SYMBOL(profile_pc);
247#endif
248
249#ifdef CONFIG_PPC_ISERIES
250
251/*
252 * This function recalibrates the timebase based on the 49-bit time-of-day
253 * value in the Titan chip. The Titan is much more accurate than the value
254 * returned by the service processor for the timebase frequency.
255 */
256
257static void iSeries_tb_recal(void)
258{
259 struct div_result divres;
260 unsigned long titan, tb;
261 tb = get_tb();
262 titan = HvCallXm_loadTod();
263 if ( iSeries_recal_titan ) {
264 unsigned long tb_ticks = tb - iSeries_recal_tb;
265 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
266 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
267 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
268 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
269 char sign = '+';
270 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
271 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
272
273 if ( tick_diff < 0 ) {
274 tick_diff = -tick_diff;
275 sign = '-';
276 }
277 if ( tick_diff ) {
278 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
279 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
280 new_tb_ticks_per_jiffy, sign, tick_diff );
281 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
282 tb_ticks_per_sec = new_tb_ticks_per_sec;
283 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
284 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
285 tb_to_xs = divres.result_low;
286 do_gtod.varp->tb_to_xs = tb_to_xs;
287 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
288 systemcfg->tb_to_xs = tb_to_xs;
289 }
290 else {
291 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
292 " new tb_ticks_per_jiffy = %lu\n"
293 " old tb_ticks_per_jiffy = %lu\n",
294 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
295 }
296 }
297 }
298 iSeries_recal_titan = titan;
299 iSeries_recal_tb = tb;
300}
301#endif
302
303/*
304 * For iSeries shared processors, we have to let the hypervisor
305 * set the hardware decrementer. We set a virtual decrementer
306 * in the lppaca and call the hypervisor if the virtual
307 * decrementer is less than the current value in the hardware
308 * decrementer. (almost always the new decrementer value will
309 * be greater than the current hardware decementer so the hypervisor
310 * call will not be needed)
311 */
312
313unsigned long tb_last_stamp __cacheline_aligned_in_smp;
314
315/*
316 * timer_interrupt - gets called when the decrementer overflows,
317 * with interrupts disabled.
318 */
319int timer_interrupt(struct pt_regs * regs)
320{
321 int next_dec;
322 unsigned long cur_tb;
323 struct paca_struct *lpaca = get_paca();
324 unsigned long cpu = smp_processor_id();
325
326 irq_enter();
327
328#ifndef CONFIG_PPC_ISERIES
329 profile_tick(CPU_PROFILING, regs);
330#endif
331
332 lpaca->lppaca.int_dword.fields.decr_int = 0;
333
334 while (lpaca->next_jiffy_update_tb <= (cur_tb = get_tb())) {
335 /*
336 * We cannot disable the decrementer, so in the period
337 * between this cpu's being marked offline in cpu_online_map
338 * and calling stop-self, it is taking timer interrupts.
339 * Avoid calling into the scheduler rebalancing code if this
340 * is the case.
341 */
342 if (!cpu_is_offline(cpu))
343 update_process_times(user_mode(regs));
344 /*
345 * No need to check whether cpu is offline here; boot_cpuid
346 * should have been fixed up by now.
347 */
348 if (cpu == boot_cpuid) {
349 write_seqlock(&xtime_lock);
350 tb_last_stamp = lpaca->next_jiffy_update_tb;
351 timer_recalc_offset(lpaca->next_jiffy_update_tb);
352 do_timer(regs);
353 timer_sync_xtime(lpaca->next_jiffy_update_tb);
354 timer_check_rtc();
355 write_sequnlock(&xtime_lock);
356 if ( adjusting_time && (time_adjust == 0) )
357 ppc_adjtimex();
358 }
359 lpaca->next_jiffy_update_tb += tb_ticks_per_jiffy;
360 }
361
362 next_dec = lpaca->next_jiffy_update_tb - cur_tb;
363 if (next_dec > lpaca->default_decr)
364 next_dec = lpaca->default_decr;
365 set_dec(next_dec);
366
367#ifdef CONFIG_PPC_ISERIES
368 {
369 struct ItLpQueue *lpq = lpaca->lpqueue_ptr;
370 if (lpq && ItLpQueue_isLpIntPending(lpq))
371 lpevent_count += ItLpQueue_process(lpq, regs);
372 }
373#endif
374
375/* collect purr register values often, for accurate calculations */
376#if defined(CONFIG_PPC_PSERIES)
377 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
378 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
379 cu->current_tb = mfspr(SPRN_PURR);
380 }
381#endif
382
383 irq_exit();
384
385 return 1;
386}
387
388/*
389 * Scheduler clock - returns current time in nanosec units.
390 *
391 * Note: mulhdu(a, b) (multiply high double unsigned) returns
392 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
393 * are 64-bit unsigned numbers.
394 */
395unsigned long long sched_clock(void)
396{
397 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
398}
399
400int do_settimeofday(struct timespec *tv)
401{
402 time_t wtm_sec, new_sec = tv->tv_sec;
403 long wtm_nsec, new_nsec = tv->tv_nsec;
404 unsigned long flags;
405 unsigned long delta_xsec;
406 long int tb_delta;
407 unsigned long new_xsec;
408
409 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
410 return -EINVAL;
411
412 write_seqlock_irqsave(&xtime_lock, flags);
413 /* Updating the RTC is not the job of this code. If the time is
414 * stepped under NTP, the RTC will be update after STA_UNSYNC
415 * is cleared. Tool like clock/hwclock either copy the RTC
416 * to the system time, in which case there is no point in writing
417 * to the RTC again, or write to the RTC but then they don't call
418 * settimeofday to perform this operation.
419 */
420#ifdef CONFIG_PPC_ISERIES
421 if ( first_settimeofday ) {
422 iSeries_tb_recal();
423 first_settimeofday = 0;
424 }
425#endif
426 tb_delta = tb_ticks_since(tb_last_stamp);
427 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
428
429 new_nsec -= tb_delta / tb_ticks_per_usec / 1000;
430
431 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
432 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
433
434 set_normalized_timespec(&xtime, new_sec, new_nsec);
435 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
436
437 /* In case of a large backwards jump in time with NTP, we want the
438 * clock to be updated as soon as the PLL is again in lock.
439 */
440 last_rtc_update = new_sec - 658;
441
442 time_adjust = 0; /* stop active adjtime() */
443 time_status |= STA_UNSYNC;
444 time_maxerror = NTP_PHASE_LIMIT;
445 time_esterror = NTP_PHASE_LIMIT;
446
447 delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp),
448 do_gtod.varp->tb_to_xs );
449
450 new_xsec = (new_nsec * XSEC_PER_SEC) / NSEC_PER_SEC;
451 new_xsec += new_sec * XSEC_PER_SEC;
452 if ( new_xsec > delta_xsec ) {
453 do_gtod.varp->stamp_xsec = new_xsec - delta_xsec;
454 systemcfg->stamp_xsec = new_xsec - delta_xsec;
455 }
456 else {
457 /* This is only for the case where the user is setting the time
458 * way back to a time such that the boot time would have been
459 * before 1970 ... eg. we booted ten days ago, and we are setting
460 * the time to Jan 5, 1970 */
461 do_gtod.varp->stamp_xsec = new_xsec;
462 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
463 systemcfg->stamp_xsec = new_xsec;
464 systemcfg->tb_orig_stamp = tb_last_stamp;
465 }
466
467 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
468 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
469
470 write_sequnlock_irqrestore(&xtime_lock, flags);
471 clock_was_set();
472 return 0;
473}
474
475EXPORT_SYMBOL(do_settimeofday);
476
477void __init time_init(void)
478{
479 /* This function is only called on the boot processor */
480 unsigned long flags;
481 struct rtc_time tm;
482 struct div_result res;
483 unsigned long scale, shift;
484
485 ppc_md.calibrate_decr();
486
487 /*
488 * Compute scale factor for sched_clock.
489 * The calibrate_decr() function has set tb_ticks_per_sec,
490 * which is the timebase frequency.
491 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
492 * the 128-bit result as a 64.64 fixed-point number.
493 * We then shift that number right until it is less than 1.0,
494 * giving us the scale factor and shift count to use in
495 * sched_clock().
496 */
497 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
498 scale = res.result_low;
499 for (shift = 0; res.result_high != 0; ++shift) {
500 scale = (scale >> 1) | (res.result_high << 63);
501 res.result_high >>= 1;
502 }
503 tb_to_ns_scale = scale;
504 tb_to_ns_shift = shift;
505
506#ifdef CONFIG_PPC_ISERIES
507 if (!piranha_simulator)
508#endif
509 ppc_md.get_boot_time(&tm);
510
511 write_seqlock_irqsave(&xtime_lock, flags);
512 xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
513 tm.tm_hour, tm.tm_min, tm.tm_sec);
514 tb_last_stamp = get_tb();
515 do_gtod.varp = &do_gtod.vars[0];
516 do_gtod.var_idx = 0;
517 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
518 do_gtod.varp->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
519 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
520 do_gtod.varp->tb_to_xs = tb_to_xs;
521 do_gtod.tb_to_us = tb_to_us;
522 systemcfg->tb_orig_stamp = tb_last_stamp;
523 systemcfg->tb_update_count = 0;
524 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
525 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
526 systemcfg->tb_to_xs = tb_to_xs;
527
528 time_freq = 0;
529
530 xtime.tv_nsec = 0;
531 last_rtc_update = xtime.tv_sec;
532 set_normalized_timespec(&wall_to_monotonic,
533 -xtime.tv_sec, -xtime.tv_nsec);
534 write_sequnlock_irqrestore(&xtime_lock, flags);
535
536 /* Not exact, but the timer interrupt takes care of this */
537 set_dec(tb_ticks_per_jiffy);
538}
539
540/*
541 * After adjtimex is called, adjust the conversion of tb ticks
542 * to microseconds to keep do_gettimeofday synchronized
543 * with ntpd.
544 *
545 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
546 * adjust the frequency.
547 */
548
549/* #define DEBUG_PPC_ADJTIMEX 1 */
550
551void ppc_adjtimex(void)
552{
553 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec;
554 unsigned long tb_ticks_per_sec_delta;
555 long delta_freq, ltemp;
556 struct div_result divres;
557 unsigned long flags;
558 struct gettimeofday_vars * temp_varp;
559 unsigned temp_idx;
560 long singleshot_ppm = 0;
561
562 /* Compute parts per million frequency adjustment to accomplish the time adjustment
563 implied by time_offset to be applied over the elapsed time indicated by time_constant.
564 Use SHIFT_USEC to get it into the same units as time_freq. */
565 if ( time_offset < 0 ) {
566 ltemp = -time_offset;
567 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
568 ltemp >>= SHIFT_KG + time_constant;
569 ltemp = -ltemp;
570 }
571 else {
572 ltemp = time_offset;
573 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
574 ltemp >>= SHIFT_KG + time_constant;
575 }
576
577 /* If there is a single shot time adjustment in progress */
578 if ( time_adjust ) {
579#ifdef DEBUG_PPC_ADJTIMEX
580 printk("ppc_adjtimex: ");
581 if ( adjusting_time == 0 )
582 printk("starting ");
583 printk("single shot time_adjust = %ld\n", time_adjust);
584#endif
585
586 adjusting_time = 1;
587
588 /* Compute parts per million frequency adjustment to match time_adjust */
589 singleshot_ppm = tickadj * HZ;
590 /*
591 * The adjustment should be tickadj*HZ to match the code in
592 * linux/kernel/timer.c, but experiments show that this is too
593 * large. 3/4 of tickadj*HZ seems about right
594 */
595 singleshot_ppm -= singleshot_ppm / 4;
596 /* Use SHIFT_USEC to get it into the same units as time_freq */
597 singleshot_ppm <<= SHIFT_USEC;
598 if ( time_adjust < 0 )
599 singleshot_ppm = -singleshot_ppm;
600 }
601 else {
602#ifdef DEBUG_PPC_ADJTIMEX
603 if ( adjusting_time )
604 printk("ppc_adjtimex: ending single shot time_adjust\n");
605#endif
606 adjusting_time = 0;
607 }
608
609 /* Add up all of the frequency adjustments */
610 delta_freq = time_freq + ltemp + singleshot_ppm;
611
612 /* Compute a new value for tb_ticks_per_sec based on the frequency adjustment */
613 den = 1000000 * (1 << (SHIFT_USEC - 8));
614 if ( delta_freq < 0 ) {
615 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
616 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
617 }
618 else {
619 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
620 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
621 }
622
623#ifdef DEBUG_PPC_ADJTIMEX
624 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
625 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
626#endif
627
628 /* Compute a new value of tb_to_xs (used to convert tb to microseconds and a new value of
629 stamp_xsec which is the time (in 1/2^20 second units) corresponding to tb_orig_stamp. This
630 new value of stamp_xsec compensates for the change in frequency (implied by the new tb_to_xs)
631 which guarantees that the current time remains the same */
632 write_seqlock_irqsave( &xtime_lock, flags );
633 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
634 div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres );
635 new_tb_to_xs = divres.result_low;
636 new_xsec = mulhdu( tb_ticks, new_tb_to_xs );
637
638 old_xsec = mulhdu( tb_ticks, do_gtod.varp->tb_to_xs );
639 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
640
641 /* There are two copies of tb_to_xs and stamp_xsec so that no lock is needed to access and use these
642 values in do_gettimeofday. We alternate the copies and as long as a reasonable time elapses between
643 changes, there will never be inconsistent values. ntpd has a minimum of one minute between updates */
644
645 temp_idx = (do_gtod.var_idx == 0);
646 temp_varp = &do_gtod.vars[temp_idx];
647
648 temp_varp->tb_to_xs = new_tb_to_xs;
649 temp_varp->stamp_xsec = new_stamp_xsec;
650 temp_varp->tb_orig_stamp = do_gtod.varp->tb_orig_stamp;
651 mb();
652 do_gtod.varp = temp_varp;
653 do_gtod.var_idx = temp_idx;
654
655 /*
656 * tb_update_count is used to allow the problem state gettimeofday code
657 * to assure itself that it sees a consistent view of the tb_to_xs and
658 * stamp_xsec variables. It reads the tb_update_count, then reads
659 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
660 * the two values of tb_update_count match and are even then the
661 * tb_to_xs and stamp_xsec values are consistent. If not, then it
662 * loops back and reads them again until this criteria is met.
663 */
664 ++(systemcfg->tb_update_count);
665 wmb();
666 systemcfg->tb_to_xs = new_tb_to_xs;
667 systemcfg->stamp_xsec = new_stamp_xsec;
668 wmb();
669 ++(systemcfg->tb_update_count);
670
671 write_sequnlock_irqrestore( &xtime_lock, flags );
672
673}
674
675
676#define TICK_SIZE tick
677#define FEBRUARY 2
678#define STARTOFTIME 1970
679#define SECDAY 86400L
680#define SECYR (SECDAY * 365)
681#define leapyear(year) ((year) % 4 == 0)
682#define days_in_year(a) (leapyear(a) ? 366 : 365)
683#define days_in_month(a) (month_days[(a) - 1])
684
685static int month_days[12] = {
686 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
687};
688
689/*
690 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
691 */
692void GregorianDay(struct rtc_time * tm)
693{
694 int leapsToDate;
695 int lastYear;
696 int day;
697 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
698
699 lastYear=tm->tm_year-1;
700
701 /*
702 * Number of leap corrections to apply up to end of last year
703 */
704 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
705
706 /*
707 * This year is a leap year if it is divisible by 4 except when it is
708 * divisible by 100 unless it is divisible by 400
709 *
710 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
711 */
712 if((tm->tm_year%4==0) &&
713 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
714 (tm->tm_mon>2))
715 {
716 /*
717 * We are past Feb. 29 in a leap year
718 */
719 day=1;
720 }
721 else
722 {
723 day=0;
724 }
725
726 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
727 tm->tm_mday;
728
729 tm->tm_wday=day%7;
730}
731
732void to_tm(int tim, struct rtc_time * tm)
733{
734 register int i;
735 register long hms, day;
736
737 day = tim / SECDAY;
738 hms = tim % SECDAY;
739
740 /* Hours, minutes, seconds are easy */
741 tm->tm_hour = hms / 3600;
742 tm->tm_min = (hms % 3600) / 60;
743 tm->tm_sec = (hms % 3600) % 60;
744
745 /* Number of years in days */
746 for (i = STARTOFTIME; day >= days_in_year(i); i++)
747 day -= days_in_year(i);
748 tm->tm_year = i;
749
750 /* Number of months in days left */
751 if (leapyear(tm->tm_year))
752 days_in_month(FEBRUARY) = 29;
753 for (i = 1; day >= days_in_month(i); i++)
754 day -= days_in_month(i);
755 days_in_month(FEBRUARY) = 28;
756 tm->tm_mon = i;
757
758 /* Days are what is left over (+1) from all that. */
759 tm->tm_mday = day + 1;
760
761 /*
762 * Determine the day of week
763 */
764 GregorianDay(tm);
765}
766
767/* Auxiliary function to compute scaling factors */
768/* Actually the choice of a timebase running at 1/4 the of the bus
769 * frequency giving resolution of a few tens of nanoseconds is quite nice.
770 * It makes this computation very precise (27-28 bits typically) which
771 * is optimistic considering the stability of most processor clock
772 * oscillators and the precision with which the timebase frequency
773 * is measured but does not harm.
774 */
775unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
776 unsigned mlt=0, tmp, err;
777 /* No concern for performance, it's done once: use a stupid
778 * but safe and compact method to find the multiplier.
779 */
780
781 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
782 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
783 }
784
785 /* We might still be off by 1 for the best approximation.
786 * A side effect of this is that if outscale is too large
787 * the returned value will be zero.
788 * Many corner cases have been checked and seem to work,
789 * some might have been forgotten in the test however.
790 */
791
792 err = inscale*(mlt+1);
793 if (err <= inscale/2) mlt++;
794 return mlt;
795 }
796
797/*
798 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
799 * result.
800 */
801
802void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
803 unsigned divisor, struct div_result *dr )
804{
805 unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
806
807 a = dividend_high >> 32;
808 b = dividend_high & 0xffffffff;
809 c = dividend_low >> 32;
810 d = dividend_low & 0xffffffff;
811
812 w = a/divisor;
813 ra = (a - (w * divisor)) << 32;
814
815 x = (ra + b)/divisor;
816 rb = ((ra + b) - (x * divisor)) << 32;
817
818 y = (rb + c)/divisor;
819 rc = ((rb + b) - (y * divisor)) << 32;
820
821 z = (rc + d)/divisor;
822
823 dr->result_high = (w << 32) + x;
824 dr->result_low = (y << 32) + z;
825
826}
827