/spare/repo/netdev-2.6 branch 'master'
[deliverable/linux.git] / arch / ppc64 / kernel / time.c
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 #include <asm/firmware.h>
71
72 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
73
74 EXPORT_SYMBOL(jiffies_64);
75
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;
83 #endif
84
85 #define XSEC_PER_SEC (1024*1024)
86
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;
92 unsigned tb_to_us;
93 unsigned long processor_freq;
94 DEFINE_SPINLOCK(rtc_lock);
95 EXPORT_SYMBOL_GPL(rtc_lock);
96
97 unsigned long tb_to_ns_scale;
98 unsigned long tb_to_ns_shift;
99
100 struct gettimeofday_struct do_gtod;
101
102 extern unsigned long wall_jiffies;
103 extern int smp_tb_synchronized;
104
105 extern struct timezone sys_tz;
106
107 void ppc_adjtimex(void);
108
109 static unsigned adjusting_time = 0;
110
111 unsigned long ppc_proc_freq;
112 unsigned long ppc_tb_freq;
113
114 static __inline__ void timer_check_rtc(void)
115 {
116 /*
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.
131 */
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) {
136 struct rtc_time tm;
137 to_tm(xtime.tv_sec+1, &tm);
138 tm.tm_year -= 1900;
139 tm.tm_mon -= 1;
140 if (ppc_md.set_rtc_time(&tm) == 0)
141 last_rtc_update = xtime.tv_sec+1;
142 else
143 /* Try again one minute later */
144 last_rtc_update += 60;
145 }
146 }
147
148 /*
149 * This version of gettimeofday has microsecond resolution.
150 */
151 static inline void __do_gettimeofday(struct timeval *tv, unsigned long tb_val)
152 {
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;
157
158 /*
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)
163 */
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;
173
174 tv->tv_sec = sec;
175 tv->tv_usec = usec;
176 }
177
178 void do_gettimeofday(struct timeval *tv)
179 {
180 __do_gettimeofday(tv, get_tb());
181 }
182
183 EXPORT_SYMBOL(do_gettimeofday);
184
185 /* Synchronize xtime with do_gettimeofday */
186
187 static inline void timer_sync_xtime(unsigned long cur_tb)
188 {
189 struct timeval my_tv;
190
191 __do_gettimeofday(&my_tv, cur_tb);
192
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;
196 }
197 }
198
199 /*
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
207 * the syscall
208 */
209 static __inline__ void timer_recalc_offset(unsigned long cur_tb)
210 {
211 struct gettimeofday_vars * temp_varp;
212 unsigned temp_idx;
213 unsigned long offset, new_stamp_xsec, new_tb_orig_stamp;
214
215 if (((cur_tb - do_gtod.varp->tb_orig_stamp) & 0x80000000u) == 0)
216 return;
217
218 temp_idx = (do_gtod.var_idx == 0);
219 temp_varp = &do_gtod.vars[temp_idx];
220
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);
224
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;
228 smp_mb();
229 do_gtod.varp = temp_varp;
230 do_gtod.var_idx = temp_idx;
231
232 ++(systemcfg->tb_update_count);
233 smp_wmb();
234 systemcfg->tb_orig_stamp = new_tb_orig_stamp;
235 systemcfg->stamp_xsec = new_stamp_xsec;
236 smp_wmb();
237 ++(systemcfg->tb_update_count);
238 }
239
240 #ifdef CONFIG_SMP
241 unsigned long profile_pc(struct pt_regs *regs)
242 {
243 unsigned long pc = instruction_pointer(regs);
244
245 if (in_lock_functions(pc))
246 return regs->link;
247
248 return pc;
249 }
250 EXPORT_SYMBOL(profile_pc);
251 #endif
252
253 #ifdef CONFIG_PPC_ISERIES
254
255 /*
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.
259 */
260
261 static void iSeries_tb_recal(void)
262 {
263 struct div_result divres;
264 unsigned long titan, tb;
265 tb = get_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;
273 char sign = '+';
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;
276
277 if ( tick_diff < 0 ) {
278 tick_diff = -tick_diff;
279 sign = '-';
280 }
281 if ( 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;
293 }
294 else {
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 );
299 }
300 }
301 }
302 iSeries_recal_titan = titan;
303 iSeries_recal_tb = tb;
304 }
305 #endif
306
307 /*
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)
315 */
316
317 unsigned long tb_last_stamp __cacheline_aligned_in_smp;
318
319 /*
320 * timer_interrupt - gets called when the decrementer overflows,
321 * with interrupts disabled.
322 */
323 int timer_interrupt(struct pt_regs * regs)
324 {
325 int next_dec;
326 unsigned long cur_tb;
327 struct paca_struct *lpaca = get_paca();
328 unsigned long cpu = smp_processor_id();
329
330 irq_enter();
331
332 profile_tick(CPU_PROFILING, regs);
333
334 lpaca->lppaca.int_dword.fields.decr_int = 0;
335
336 while (lpaca->next_jiffy_update_tb <= (cur_tb = get_tb())) {
337 /*
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
342 * is the case.
343 */
344 if (!cpu_is_offline(cpu))
345 update_process_times(user_mode(regs));
346 /*
347 * No need to check whether cpu is offline here; boot_cpuid
348 * should have been fixed up by now.
349 */
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);
354 do_timer(regs);
355 timer_sync_xtime(lpaca->next_jiffy_update_tb);
356 timer_check_rtc();
357 write_sequnlock(&xtime_lock);
358 if ( adjusting_time && (time_adjust == 0) )
359 ppc_adjtimex();
360 }
361 lpaca->next_jiffy_update_tb += tb_ticks_per_jiffy;
362 }
363
364 next_dec = lpaca->next_jiffy_update_tb - cur_tb;
365 if (next_dec > lpaca->default_decr)
366 next_dec = lpaca->default_decr;
367 set_dec(next_dec);
368
369 #ifdef CONFIG_PPC_ISERIES
370 if (hvlpevent_is_pending())
371 process_hvlpevents(regs);
372 #endif
373
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);
378 }
379
380 irq_exit();
381
382 return 1;
383 }
384
385 /*
386 * Scheduler clock - returns current time in nanosec units.
387 *
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.
391 */
392 unsigned long long sched_clock(void)
393 {
394 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
395 }
396
397 int do_settimeofday(struct timespec *tv)
398 {
399 time_t wtm_sec, new_sec = tv->tv_sec;
400 long wtm_nsec, new_nsec = tv->tv_nsec;
401 unsigned long flags;
402 unsigned long delta_xsec;
403 long int tb_delta;
404 unsigned long new_xsec;
405
406 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
407 return -EINVAL;
408
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.
416 */
417 #ifdef CONFIG_PPC_ISERIES
418 if ( first_settimeofday ) {
419 iSeries_tb_recal();
420 first_settimeofday = 0;
421 }
422 #endif
423 tb_delta = tb_ticks_since(tb_last_stamp);
424 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
425
426 new_nsec -= tb_delta / tb_ticks_per_usec / 1000;
427
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);
430
431 set_normalized_timespec(&xtime, new_sec, new_nsec);
432 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
433
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.
436 */
437 last_rtc_update = new_sec - 658;
438
439 time_adjust = 0; /* stop active adjtime() */
440 time_status |= STA_UNSYNC;
441 time_maxerror = NTP_PHASE_LIMIT;
442 time_esterror = NTP_PHASE_LIMIT;
443
444 delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp),
445 do_gtod.varp->tb_to_xs );
446
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;
452 }
453 else {
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;
462 }
463
464 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
465 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
466
467 write_sequnlock_irqrestore(&xtime_lock, flags);
468 clock_was_set();
469 return 0;
470 }
471
472 EXPORT_SYMBOL(do_settimeofday);
473
474 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_MAPLE) || defined(CONFIG_PPC_BPA)
475 void __init generic_calibrate_decr(void)
476 {
477 struct device_node *cpu;
478 struct div_result divres;
479 unsigned int *fp;
480 int node_found;
481
482 /*
483 * The cpu node should have a timebase-frequency property
484 * to tell us the rate at which the decrementer counts.
485 */
486 cpu = of_find_node_by_type(NULL, "cpu");
487
488 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
489 node_found = 0;
490 if (cpu != 0) {
491 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
492 NULL);
493 if (fp != 0) {
494 node_found = 1;
495 ppc_tb_freq = *fp;
496 }
497 }
498 if (!node_found)
499 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
500 "(not found)\n");
501
502 ppc_proc_freq = DEFAULT_PROC_FREQ;
503 node_found = 0;
504 if (cpu != 0) {
505 fp = (unsigned int *)get_property(cpu, "clock-frequency",
506 NULL);
507 if (fp != 0) {
508 node_found = 1;
509 ppc_proc_freq = *fp;
510 }
511 }
512 if (!node_found)
513 printk(KERN_ERR "WARNING: Estimating processor frequency "
514 "(not found)\n");
515
516 of_node_put(cpu);
517
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);
522
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;
529
530 setup_default_decr();
531 }
532 #endif
533
534 void __init time_init(void)
535 {
536 /* This function is only called on the boot processor */
537 unsigned long flags;
538 struct rtc_time tm;
539 struct div_result res;
540 unsigned long scale, shift;
541
542 ppc_md.calibrate_decr();
543
544 /*
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
552 * sched_clock().
553 */
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;
559 }
560 tb_to_ns_scale = scale;
561 tb_to_ns_shift = shift;
562
563 #ifdef CONFIG_PPC_ISERIES
564 if (!piranha_simulator)
565 #endif
566 ppc_md.get_boot_time(&tm);
567
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];
573 do_gtod.var_idx = 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;
585
586 time_freq = 0;
587
588 xtime.tv_nsec = 0;
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);
593
594 /* Not exact, but the timer interrupt takes care of this */
595 set_dec(tb_ticks_per_jiffy);
596 }
597
598 /*
599 * After adjtimex is called, adjust the conversion of tb ticks
600 * to microseconds to keep do_gettimeofday synchronized
601 * with ntpd.
602 *
603 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
604 * adjust the frequency.
605 */
606
607 /* #define DEBUG_PPC_ADJTIMEX 1 */
608
609 void ppc_adjtimex(void)
610 {
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;
615 unsigned long flags;
616 struct gettimeofday_vars * temp_varp;
617 unsigned temp_idx;
618 long singleshot_ppm = 0;
619
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;
627 ltemp = -ltemp;
628 }
629 else {
630 ltemp = time_offset;
631 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
632 ltemp >>= SHIFT_KG + time_constant;
633 }
634
635 /* If there is a single shot time adjustment in progress */
636 if ( time_adjust ) {
637 #ifdef DEBUG_PPC_ADJTIMEX
638 printk("ppc_adjtimex: ");
639 if ( adjusting_time == 0 )
640 printk("starting ");
641 printk("single shot time_adjust = %ld\n", time_adjust);
642 #endif
643
644 adjusting_time = 1;
645
646 /* Compute parts per million frequency adjustment to match time_adjust */
647 singleshot_ppm = tickadj * HZ;
648 /*
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
652 */
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;
658 }
659 else {
660 #ifdef DEBUG_PPC_ADJTIMEX
661 if ( adjusting_time )
662 printk("ppc_adjtimex: ending single shot time_adjust\n");
663 #endif
664 adjusting_time = 0;
665 }
666
667 /* Add up all of the frequency adjustments */
668 delta_freq = time_freq + ltemp + singleshot_ppm;
669
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;
675 }
676 else {
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;
679 }
680
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);
684 #endif
685
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 );
695
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;
698
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 */
702
703 temp_idx = (do_gtod.var_idx == 0);
704 temp_varp = &do_gtod.vars[temp_idx];
705
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;
709 smp_mb();
710 do_gtod.varp = temp_varp;
711 do_gtod.var_idx = temp_idx;
712
713 /*
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.
721 */
722 ++(systemcfg->tb_update_count);
723 smp_wmb();
724 systemcfg->tb_to_xs = new_tb_to_xs;
725 systemcfg->stamp_xsec = new_stamp_xsec;
726 smp_wmb();
727 ++(systemcfg->tb_update_count);
728
729 write_sequnlock_irqrestore( &xtime_lock, flags );
730
731 }
732
733
734 #define TICK_SIZE tick
735 #define FEBRUARY 2
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])
742
743 static int month_days[12] = {
744 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
745 };
746
747 /*
748 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
749 */
750 void GregorianDay(struct rtc_time * tm)
751 {
752 int leapsToDate;
753 int lastYear;
754 int day;
755 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
756
757 lastYear=tm->tm_year-1;
758
759 /*
760 * Number of leap corrections to apply up to end of last year
761 */
762 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
763
764 /*
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
767 *
768 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
769 */
770 if((tm->tm_year%4==0) &&
771 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
772 (tm->tm_mon>2))
773 {
774 /*
775 * We are past Feb. 29 in a leap year
776 */
777 day=1;
778 }
779 else
780 {
781 day=0;
782 }
783
784 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
785 tm->tm_mday;
786
787 tm->tm_wday=day%7;
788 }
789
790 void to_tm(int tim, struct rtc_time * tm)
791 {
792 register int i;
793 register long hms, day;
794
795 day = tim / SECDAY;
796 hms = tim % SECDAY;
797
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;
802
803 /* Number of years in days */
804 for (i = STARTOFTIME; day >= days_in_year(i); i++)
805 day -= days_in_year(i);
806 tm->tm_year = i;
807
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;
814 tm->tm_mon = i;
815
816 /* Days are what is left over (+1) from all that. */
817 tm->tm_mday = day + 1;
818
819 /*
820 * Determine the day of week
821 */
822 GregorianDay(tm);
823 }
824
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.
832 */
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.
837 */
838
839 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
840 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
841 }
842
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.
848 */
849
850 err = inscale*(mlt+1);
851 if (err <= inscale/2) mlt++;
852 return mlt;
853 }
854
855 /*
856 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
857 * result.
858 */
859
860 void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
861 unsigned divisor, struct div_result *dr )
862 {
863 unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
864
865 a = dividend_high >> 32;
866 b = dividend_high & 0xffffffff;
867 c = dividend_low >> 32;
868 d = dividend_low & 0xffffffff;
869
870 w = a/divisor;
871 ra = (a - (w * divisor)) << 32;
872
873 x = (ra + b)/divisor;
874 rb = ((ra + b) - (x * divisor)) << 32;
875
876 y = (rb + c)/divisor;
877 rc = ((rb + b) - (y * divisor)) << 32;
878
879 z = (rc + d)/divisor;
880
881 dr->result_high = (w << 32) + x;
882 dr->result_low = (y << 32) + z;
883
884 }
885
This page took 0.067506 seconds and 5 git commands to generate.