[PATCH] ppc64: Rename xItLpQueue to hvlpevent_queue
[deliverable/linux.git] / arch / ppc64 / kernel / time.c
CommitLineData
1da177e4
LT
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);
6ae3db11 94EXPORT_SYMBOL_GPL(rtc_lock);
1da177e4
LT
95
96unsigned long tb_to_ns_scale;
97unsigned long tb_to_ns_shift;
98
99struct gettimeofday_struct do_gtod;
100
101extern unsigned long wall_jiffies;
102extern unsigned long lpevent_count;
103extern int smp_tb_synchronized;
104
105extern struct timezone sys_tz;
106
107void ppc_adjtimex(void);
108
109static unsigned adjusting_time = 0;
110
10f7e7c1
AB
111unsigned long ppc_proc_freq;
112unsigned long ppc_tb_freq;
113
1da177e4
LT
114static __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 */
151static 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
178void do_gettimeofday(struct timeval *tv)
179{
180 __do_gettimeofday(tv, get_tb());
181}
182
183EXPORT_SYMBOL(do_gettimeofday);
184
185/* Synchronize xtime with do_gettimeofday */
186
187static 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 */
209static __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;
0d8d4d42 228 smp_mb();
1da177e4
LT
229 do_gtod.varp = temp_varp;
230 do_gtod.var_idx = temp_idx;
231
232 ++(systemcfg->tb_update_count);
0d8d4d42 233 smp_wmb();
1da177e4
LT
234 systemcfg->tb_orig_stamp = new_tb_orig_stamp;
235 systemcfg->stamp_xsec = new_stamp_xsec;
0d8d4d42 236 smp_wmb();
1da177e4
LT
237 ++(systemcfg->tb_update_count);
238}
239
240#ifdef CONFIG_SMP
241unsigned 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}
250EXPORT_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
261static 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
317unsigned long tb_last_stamp __cacheline_aligned_in_smp;
318
319/*
320 * timer_interrupt - gets called when the decrementer overflows,
321 * with interrupts disabled.
322 */
323int 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
1da177e4 332 profile_tick(CPU_PROFILING, regs);
1da177e4
LT
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
1b19bc72
ME
370 if (ItLpQueue_isLpIntPending())
371 lpevent_count += ItLpQueue_process(regs);
1da177e4
LT
372#endif
373
374/* collect purr register values often, for accurate calculations */
375#if defined(CONFIG_PPC_PSERIES)
376 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
377 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
378 cu->current_tb = mfspr(SPRN_PURR);
379 }
380#endif
381
382 irq_exit();
383
384 return 1;
385}
386
387/*
388 * Scheduler clock - returns current time in nanosec units.
389 *
390 * Note: mulhdu(a, b) (multiply high double unsigned) returns
391 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
392 * are 64-bit unsigned numbers.
393 */
394unsigned long long sched_clock(void)
395{
396 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
397}
398
399int do_settimeofday(struct timespec *tv)
400{
401 time_t wtm_sec, new_sec = tv->tv_sec;
402 long wtm_nsec, new_nsec = tv->tv_nsec;
403 unsigned long flags;
404 unsigned long delta_xsec;
405 long int tb_delta;
406 unsigned long new_xsec;
407
408 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
409 return -EINVAL;
410
411 write_seqlock_irqsave(&xtime_lock, flags);
412 /* Updating the RTC is not the job of this code. If the time is
413 * stepped under NTP, the RTC will be update after STA_UNSYNC
414 * is cleared. Tool like clock/hwclock either copy the RTC
415 * to the system time, in which case there is no point in writing
416 * to the RTC again, or write to the RTC but then they don't call
417 * settimeofday to perform this operation.
418 */
419#ifdef CONFIG_PPC_ISERIES
420 if ( first_settimeofday ) {
421 iSeries_tb_recal();
422 first_settimeofday = 0;
423 }
424#endif
425 tb_delta = tb_ticks_since(tb_last_stamp);
426 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
427
428 new_nsec -= tb_delta / tb_ticks_per_usec / 1000;
429
430 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
431 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
432
433 set_normalized_timespec(&xtime, new_sec, new_nsec);
434 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
435
436 /* In case of a large backwards jump in time with NTP, we want the
437 * clock to be updated as soon as the PLL is again in lock.
438 */
439 last_rtc_update = new_sec - 658;
440
441 time_adjust = 0; /* stop active adjtime() */
442 time_status |= STA_UNSYNC;
443 time_maxerror = NTP_PHASE_LIMIT;
444 time_esterror = NTP_PHASE_LIMIT;
445
446 delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp),
447 do_gtod.varp->tb_to_xs );
448
449 new_xsec = (new_nsec * XSEC_PER_SEC) / NSEC_PER_SEC;
450 new_xsec += new_sec * XSEC_PER_SEC;
451 if ( new_xsec > delta_xsec ) {
452 do_gtod.varp->stamp_xsec = new_xsec - delta_xsec;
453 systemcfg->stamp_xsec = new_xsec - delta_xsec;
454 }
455 else {
456 /* This is only for the case where the user is setting the time
457 * way back to a time such that the boot time would have been
458 * before 1970 ... eg. we booted ten days ago, and we are setting
459 * the time to Jan 5, 1970 */
460 do_gtod.varp->stamp_xsec = new_xsec;
461 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
462 systemcfg->stamp_xsec = new_xsec;
463 systemcfg->tb_orig_stamp = tb_last_stamp;
464 }
465
466 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
467 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
468
469 write_sequnlock_irqrestore(&xtime_lock, flags);
470 clock_was_set();
471 return 0;
472}
473
474EXPORT_SYMBOL(do_settimeofday);
475
10f7e7c1
AB
476#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_MAPLE) || defined(CONFIG_PPC_BPA)
477void __init generic_calibrate_decr(void)
478{
479 struct device_node *cpu;
480 struct div_result divres;
481 unsigned int *fp;
482 int node_found;
483
484 /*
485 * The cpu node should have a timebase-frequency property
486 * to tell us the rate at which the decrementer counts.
487 */
488 cpu = of_find_node_by_type(NULL, "cpu");
489
490 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
491 node_found = 0;
492 if (cpu != 0) {
493 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
494 NULL);
495 if (fp != 0) {
496 node_found = 1;
497 ppc_tb_freq = *fp;
498 }
499 }
500 if (!node_found)
501 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
502 "(not found)\n");
503
504 ppc_proc_freq = DEFAULT_PROC_FREQ;
505 node_found = 0;
506 if (cpu != 0) {
507 fp = (unsigned int *)get_property(cpu, "clock-frequency",
508 NULL);
509 if (fp != 0) {
510 node_found = 1;
511 ppc_proc_freq = *fp;
512 }
513 }
514 if (!node_found)
515 printk(KERN_ERR "WARNING: Estimating processor frequency "
516 "(not found)\n");
517
518 of_node_put(cpu);
519
520 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
521 ppc_tb_freq/1000000, ppc_tb_freq%1000000);
522 printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
523 ppc_proc_freq/1000000, ppc_proc_freq%1000000);
524
525 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
526 tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
527 tb_ticks_per_usec = ppc_tb_freq / 1000000;
528 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
529 div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
530 tb_to_xs = divres.result_low;
531
532 setup_default_decr();
533}
534#endif
535
1da177e4
LT
536void __init time_init(void)
537{
538 /* This function is only called on the boot processor */
539 unsigned long flags;
540 struct rtc_time tm;
541 struct div_result res;
542 unsigned long scale, shift;
543
544 ppc_md.calibrate_decr();
545
546 /*
547 * Compute scale factor for sched_clock.
548 * The calibrate_decr() function has set tb_ticks_per_sec,
549 * which is the timebase frequency.
550 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
551 * the 128-bit result as a 64.64 fixed-point number.
552 * We then shift that number right until it is less than 1.0,
553 * giving us the scale factor and shift count to use in
554 * sched_clock().
555 */
556 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
557 scale = res.result_low;
558 for (shift = 0; res.result_high != 0; ++shift) {
559 scale = (scale >> 1) | (res.result_high << 63);
560 res.result_high >>= 1;
561 }
562 tb_to_ns_scale = scale;
563 tb_to_ns_shift = shift;
564
565#ifdef CONFIG_PPC_ISERIES
566 if (!piranha_simulator)
567#endif
568 ppc_md.get_boot_time(&tm);
569
570 write_seqlock_irqsave(&xtime_lock, flags);
571 xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
572 tm.tm_hour, tm.tm_min, tm.tm_sec);
573 tb_last_stamp = get_tb();
574 do_gtod.varp = &do_gtod.vars[0];
575 do_gtod.var_idx = 0;
576 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
8f80e5c9 577 get_paca()->next_jiffy_update_tb = tb_last_stamp + tb_ticks_per_jiffy;
1da177e4
LT
578 do_gtod.varp->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
579 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
580 do_gtod.varp->tb_to_xs = tb_to_xs;
581 do_gtod.tb_to_us = tb_to_us;
582 systemcfg->tb_orig_stamp = tb_last_stamp;
583 systemcfg->tb_update_count = 0;
584 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
585 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
586 systemcfg->tb_to_xs = tb_to_xs;
587
588 time_freq = 0;
589
590 xtime.tv_nsec = 0;
591 last_rtc_update = xtime.tv_sec;
592 set_normalized_timespec(&wall_to_monotonic,
593 -xtime.tv_sec, -xtime.tv_nsec);
594 write_sequnlock_irqrestore(&xtime_lock, flags);
595
596 /* Not exact, but the timer interrupt takes care of this */
597 set_dec(tb_ticks_per_jiffy);
598}
599
600/*
601 * After adjtimex is called, adjust the conversion of tb ticks
602 * to microseconds to keep do_gettimeofday synchronized
603 * with ntpd.
604 *
605 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
606 * adjust the frequency.
607 */
608
609/* #define DEBUG_PPC_ADJTIMEX 1 */
610
611void ppc_adjtimex(void)
612{
613 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec;
614 unsigned long tb_ticks_per_sec_delta;
615 long delta_freq, ltemp;
616 struct div_result divres;
617 unsigned long flags;
618 struct gettimeofday_vars * temp_varp;
619 unsigned temp_idx;
620 long singleshot_ppm = 0;
621
622 /* Compute parts per million frequency adjustment to accomplish the time adjustment
623 implied by time_offset to be applied over the elapsed time indicated by time_constant.
624 Use SHIFT_USEC to get it into the same units as time_freq. */
625 if ( time_offset < 0 ) {
626 ltemp = -time_offset;
627 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
628 ltemp >>= SHIFT_KG + time_constant;
629 ltemp = -ltemp;
630 }
631 else {
632 ltemp = time_offset;
633 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
634 ltemp >>= SHIFT_KG + time_constant;
635 }
636
637 /* If there is a single shot time adjustment in progress */
638 if ( time_adjust ) {
639#ifdef DEBUG_PPC_ADJTIMEX
640 printk("ppc_adjtimex: ");
641 if ( adjusting_time == 0 )
642 printk("starting ");
643 printk("single shot time_adjust = %ld\n", time_adjust);
644#endif
645
646 adjusting_time = 1;
647
648 /* Compute parts per million frequency adjustment to match time_adjust */
649 singleshot_ppm = tickadj * HZ;
650 /*
651 * The adjustment should be tickadj*HZ to match the code in
652 * linux/kernel/timer.c, but experiments show that this is too
653 * large. 3/4 of tickadj*HZ seems about right
654 */
655 singleshot_ppm -= singleshot_ppm / 4;
656 /* Use SHIFT_USEC to get it into the same units as time_freq */
657 singleshot_ppm <<= SHIFT_USEC;
658 if ( time_adjust < 0 )
659 singleshot_ppm = -singleshot_ppm;
660 }
661 else {
662#ifdef DEBUG_PPC_ADJTIMEX
663 if ( adjusting_time )
664 printk("ppc_adjtimex: ending single shot time_adjust\n");
665#endif
666 adjusting_time = 0;
667 }
668
669 /* Add up all of the frequency adjustments */
670 delta_freq = time_freq + ltemp + singleshot_ppm;
671
672 /* Compute a new value for tb_ticks_per_sec based on the frequency adjustment */
673 den = 1000000 * (1 << (SHIFT_USEC - 8));
674 if ( delta_freq < 0 ) {
675 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
676 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
677 }
678 else {
679 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
680 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
681 }
682
683#ifdef DEBUG_PPC_ADJTIMEX
684 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
685 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
686#endif
687
688 /* Compute a new value of tb_to_xs (used to convert tb to microseconds and a new value of
689 stamp_xsec which is the time (in 1/2^20 second units) corresponding to tb_orig_stamp. This
690 new value of stamp_xsec compensates for the change in frequency (implied by the new tb_to_xs)
691 which guarantees that the current time remains the same */
692 write_seqlock_irqsave( &xtime_lock, flags );
693 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
694 div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres );
695 new_tb_to_xs = divres.result_low;
696 new_xsec = mulhdu( tb_ticks, new_tb_to_xs );
697
698 old_xsec = mulhdu( tb_ticks, do_gtod.varp->tb_to_xs );
699 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
700
701 /* There are two copies of tb_to_xs and stamp_xsec so that no lock is needed to access and use these
702 values in do_gettimeofday. We alternate the copies and as long as a reasonable time elapses between
703 changes, there will never be inconsistent values. ntpd has a minimum of one minute between updates */
704
705 temp_idx = (do_gtod.var_idx == 0);
706 temp_varp = &do_gtod.vars[temp_idx];
707
708 temp_varp->tb_to_xs = new_tb_to_xs;
709 temp_varp->stamp_xsec = new_stamp_xsec;
710 temp_varp->tb_orig_stamp = do_gtod.varp->tb_orig_stamp;
0d8d4d42 711 smp_mb();
1da177e4
LT
712 do_gtod.varp = temp_varp;
713 do_gtod.var_idx = temp_idx;
714
715 /*
716 * tb_update_count is used to allow the problem state gettimeofday code
717 * to assure itself that it sees a consistent view of the tb_to_xs and
718 * stamp_xsec variables. It reads the tb_update_count, then reads
719 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
720 * the two values of tb_update_count match and are even then the
721 * tb_to_xs and stamp_xsec values are consistent. If not, then it
722 * loops back and reads them again until this criteria is met.
723 */
724 ++(systemcfg->tb_update_count);
0d8d4d42 725 smp_wmb();
1da177e4
LT
726 systemcfg->tb_to_xs = new_tb_to_xs;
727 systemcfg->stamp_xsec = new_stamp_xsec;
0d8d4d42 728 smp_wmb();
1da177e4
LT
729 ++(systemcfg->tb_update_count);
730
731 write_sequnlock_irqrestore( &xtime_lock, flags );
732
733}
734
735
736#define TICK_SIZE tick
737#define FEBRUARY 2
738#define STARTOFTIME 1970
739#define SECDAY 86400L
740#define SECYR (SECDAY * 365)
741#define leapyear(year) ((year) % 4 == 0)
742#define days_in_year(a) (leapyear(a) ? 366 : 365)
743#define days_in_month(a) (month_days[(a) - 1])
744
745static int month_days[12] = {
746 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
747};
748
749/*
750 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
751 */
752void GregorianDay(struct rtc_time * tm)
753{
754 int leapsToDate;
755 int lastYear;
756 int day;
757 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
758
759 lastYear=tm->tm_year-1;
760
761 /*
762 * Number of leap corrections to apply up to end of last year
763 */
764 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
765
766 /*
767 * This year is a leap year if it is divisible by 4 except when it is
768 * divisible by 100 unless it is divisible by 400
769 *
770 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
771 */
772 if((tm->tm_year%4==0) &&
773 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
774 (tm->tm_mon>2))
775 {
776 /*
777 * We are past Feb. 29 in a leap year
778 */
779 day=1;
780 }
781 else
782 {
783 day=0;
784 }
785
786 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
787 tm->tm_mday;
788
789 tm->tm_wday=day%7;
790}
791
792void to_tm(int tim, struct rtc_time * tm)
793{
794 register int i;
795 register long hms, day;
796
797 day = tim / SECDAY;
798 hms = tim % SECDAY;
799
800 /* Hours, minutes, seconds are easy */
801 tm->tm_hour = hms / 3600;
802 tm->tm_min = (hms % 3600) / 60;
803 tm->tm_sec = (hms % 3600) % 60;
804
805 /* Number of years in days */
806 for (i = STARTOFTIME; day >= days_in_year(i); i++)
807 day -= days_in_year(i);
808 tm->tm_year = i;
809
810 /* Number of months in days left */
811 if (leapyear(tm->tm_year))
812 days_in_month(FEBRUARY) = 29;
813 for (i = 1; day >= days_in_month(i); i++)
814 day -= days_in_month(i);
815 days_in_month(FEBRUARY) = 28;
816 tm->tm_mon = i;
817
818 /* Days are what is left over (+1) from all that. */
819 tm->tm_mday = day + 1;
820
821 /*
822 * Determine the day of week
823 */
824 GregorianDay(tm);
825}
826
827/* Auxiliary function to compute scaling factors */
828/* Actually the choice of a timebase running at 1/4 the of the bus
829 * frequency giving resolution of a few tens of nanoseconds is quite nice.
830 * It makes this computation very precise (27-28 bits typically) which
831 * is optimistic considering the stability of most processor clock
832 * oscillators and the precision with which the timebase frequency
833 * is measured but does not harm.
834 */
835unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
836 unsigned mlt=0, tmp, err;
837 /* No concern for performance, it's done once: use a stupid
838 * but safe and compact method to find the multiplier.
839 */
840
841 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
842 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
843 }
844
845 /* We might still be off by 1 for the best approximation.
846 * A side effect of this is that if outscale is too large
847 * the returned value will be zero.
848 * Many corner cases have been checked and seem to work,
849 * some might have been forgotten in the test however.
850 */
851
852 err = inscale*(mlt+1);
853 if (err <= inscale/2) mlt++;
854 return mlt;
855 }
856
857/*
858 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
859 * result.
860 */
861
862void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
863 unsigned divisor, struct div_result *dr )
864{
865 unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
866
867 a = dividend_high >> 32;
868 b = dividend_high & 0xffffffff;
869 c = dividend_low >> 32;
870 d = dividend_low & 0xffffffff;
871
872 w = a/divisor;
873 ra = (a - (w * divisor)) << 32;
874
875 x = (ra + b)/divisor;
876 rb = ((ra + b) - (x * divisor)) << 32;
877
878 y = (rb + c)/divisor;
879 rc = ((rb + b) - (y * divisor)) << 32;
880
881 z = (rc + d)/divisor;
882
883 dr->result_high = (w << 32) + x;
884 dr->result_low = (y << 32) + z;
885
886}
887
This page took 0.101171 seconds and 5 git commands to generate.