powerpc: Remove T command from xmon help text since it no longer exists
[linux-block.git] / arch / powerpc / kernel / time.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Common time routines among all ppc machines.
3 *
4 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5 * Paul Mackerras' version and mine for PReP and Pmac.
6 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
7 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
8 *
9 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
10 * to make clock more stable (2.4.0-test5). The only thing
11 * that this code assumes is that the timebases have been synchronized
12 * by firmware on SMP and are never stopped (never do sleep
13 * on SMP then, nap and doze are OK).
14 *
15 * Speeded up do_gettimeofday by getting rid of references to
16 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
17 *
18 * TODO (not necessarily in this file):
19 * - improve precision and reproducibility of timebase frequency
20 * measurement at boot time. (for iSeries, we calibrate the timebase
21 * against the Titan chip's clock.)
22 * - for astronomical applications: add a new function to get
23 * non ambiguous timestamps even around leap seconds. This needs
24 * a new timestamp format and a good name.
25 *
26 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
27 * "A Kernel Model for Precision Timekeeping" by Dave Mills
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35#include <linux/config.h>
36#include <linux/errno.h>
37#include <linux/module.h>
38#include <linux/sched.h>
39#include <linux/kernel.h>
40#include <linux/param.h>
41#include <linux/string.h>
42#include <linux/mm.h>
43#include <linux/interrupt.h>
44#include <linux/timex.h>
45#include <linux/kernel_stat.h>
1da177e4
LT
46#include <linux/time.h>
47#include <linux/init.h>
48#include <linux/profile.h>
49#include <linux/cpu.h>
50#include <linux/security.h>
f2783c15
PM
51#include <linux/percpu.h>
52#include <linux/rtc.h>
1da177e4 53
1da177e4
LT
54#include <asm/io.h>
55#include <asm/processor.h>
56#include <asm/nvram.h>
57#include <asm/cache.h>
58#include <asm/machdep.h>
1da177e4
LT
59#include <asm/uaccess.h>
60#include <asm/time.h>
1da177e4 61#include <asm/prom.h>
f2783c15
PM
62#include <asm/irq.h>
63#include <asm/div64.h>
64#ifdef CONFIG_PPC64
1da177e4 65#include <asm/systemcfg.h>
1ababe11 66#include <asm/firmware.h>
f2783c15
PM
67#endif
68#ifdef CONFIG_PPC_ISERIES
69#include <asm/iSeries/ItLpQueue.h>
70#include <asm/iSeries/HvCallXm.h>
71#endif
1da177e4
LT
72
73u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
74
75EXPORT_SYMBOL(jiffies_64);
76
77/* keep track of when we need to update the rtc */
78time_t last_rtc_update;
79extern int piranha_simulator;
80#ifdef CONFIG_PPC_ISERIES
81unsigned long iSeries_recal_titan = 0;
82unsigned long iSeries_recal_tb = 0;
83static unsigned long first_settimeofday = 1;
84#endif
85
f2783c15
PM
86/* The decrementer counts down by 128 every 128ns on a 601. */
87#define DECREMENTER_COUNT_601 (1000000000 / HZ)
88
1da177e4
LT
89#define XSEC_PER_SEC (1024*1024)
90
f2783c15
PM
91#ifdef CONFIG_PPC64
92#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
93#else
94/* compute ((xsec << 12) * max) >> 32 */
95#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
96#endif
97
1da177e4
LT
98unsigned long tb_ticks_per_jiffy;
99unsigned long tb_ticks_per_usec = 100; /* sane default */
100EXPORT_SYMBOL(tb_ticks_per_usec);
101unsigned long tb_ticks_per_sec;
f2783c15
PM
102u64 tb_to_xs;
103unsigned tb_to_us;
1da177e4
LT
104unsigned long processor_freq;
105DEFINE_SPINLOCK(rtc_lock);
6ae3db11 106EXPORT_SYMBOL_GPL(rtc_lock);
1da177e4 107
f2783c15
PM
108u64 tb_to_ns_scale;
109unsigned tb_to_ns_shift;
1da177e4
LT
110
111struct gettimeofday_struct do_gtod;
112
113extern unsigned long wall_jiffies;
1da177e4
LT
114
115extern struct timezone sys_tz;
f2783c15 116static long timezone_offset;
1da177e4
LT
117
118void ppc_adjtimex(void);
119
120static unsigned adjusting_time = 0;
121
10f7e7c1
AB
122unsigned long ppc_proc_freq;
123unsigned long ppc_tb_freq;
124
f2783c15
PM
125#ifdef CONFIG_PPC32 /* XXX for now */
126#define boot_cpuid 0
127#endif
128
96c44507
PM
129u64 tb_last_jiffy __cacheline_aligned_in_smp;
130unsigned long tb_last_stamp;
131
132/*
133 * Note that on ppc32 this only stores the bottom 32 bits of
134 * the timebase value, but that's enough to tell when a jiffy
135 * has passed.
136 */
137DEFINE_PER_CPU(unsigned long, last_jiffy);
138
1da177e4
LT
139static __inline__ void timer_check_rtc(void)
140{
141 /*
142 * update the rtc when needed, this should be performed on the
143 * right fraction of a second. Half or full second ?
144 * Full second works on mk48t59 clocks, others need testing.
145 * Note that this update is basically only used through
146 * the adjtimex system calls. Setting the HW clock in
147 * any other way is a /dev/rtc and userland business.
148 * This is still wrong by -0.5/+1.5 jiffies because of the
149 * timer interrupt resolution and possible delay, but here we
150 * hit a quantization limit which can only be solved by higher
151 * resolution timers and decoupling time management from timer
152 * interrupts. This is also wrong on the clocks
153 * which require being written at the half second boundary.
154 * We should have an rtc call that only sets the minutes and
155 * seconds like on Intel to avoid problems with non UTC clocks.
156 */
d2e61512 157 if (ppc_md.set_rtc_time && ntp_synced() &&
f2783c15
PM
158 xtime.tv_sec - last_rtc_update >= 659 &&
159 abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
160 jiffies - wall_jiffies == 1) {
161 struct rtc_time tm;
162 to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
163 tm.tm_year -= 1900;
164 tm.tm_mon -= 1;
165 if (ppc_md.set_rtc_time(&tm) == 0)
166 last_rtc_update = xtime.tv_sec + 1;
167 else
168 /* Try again one minute later */
169 last_rtc_update += 60;
1da177e4
LT
170 }
171}
172
173/*
174 * This version of gettimeofday has microsecond resolution.
175 */
f2783c15 176static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
1da177e4 177{
f2783c15
PM
178 unsigned long sec, usec;
179 u64 tb_ticks, xsec;
180 struct gettimeofday_vars *temp_varp;
181 u64 temp_tb_to_xs, temp_stamp_xsec;
1da177e4
LT
182
183 /*
184 * These calculations are faster (gets rid of divides)
185 * if done in units of 1/2^20 rather than microseconds.
186 * The conversion to microseconds at the end is done
187 * without a divide (and in fact, without a multiply)
188 */
189 temp_varp = do_gtod.varp;
190 tb_ticks = tb_val - temp_varp->tb_orig_stamp;
191 temp_tb_to_xs = temp_varp->tb_to_xs;
192 temp_stamp_xsec = temp_varp->stamp_xsec;
f2783c15 193 xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
1da177e4 194 sec = xsec / XSEC_PER_SEC;
f2783c15
PM
195 usec = (unsigned long)xsec & (XSEC_PER_SEC - 1);
196 usec = SCALE_XSEC(usec, 1000000);
1da177e4
LT
197
198 tv->tv_sec = sec;
199 tv->tv_usec = usec;
200}
201
202void do_gettimeofday(struct timeval *tv)
203{
96c44507
PM
204 if (__USE_RTC()) {
205 /* do this the old way */
206 unsigned long flags, seq;
207 unsigned int sec, nsec, usec, lost;
208
209 do {
210 seq = read_seqbegin_irqsave(&xtime_lock, flags);
211 sec = xtime.tv_sec;
212 nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
213 lost = jiffies - wall_jiffies;
214 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
215 usec = nsec / 1000 + lost * (1000000 / HZ);
216 while (usec >= 1000000) {
217 usec -= 1000000;
218 ++sec;
219 }
220 tv->tv_sec = sec;
221 tv->tv_usec = usec;
222 return;
223 }
1da177e4
LT
224 __do_gettimeofday(tv, get_tb());
225}
226
227EXPORT_SYMBOL(do_gettimeofday);
228
229/* Synchronize xtime with do_gettimeofday */
230
231static inline void timer_sync_xtime(unsigned long cur_tb)
232{
f2783c15
PM
233#ifdef CONFIG_PPC64
234 /* why do we do this? */
1da177e4
LT
235 struct timeval my_tv;
236
237 __do_gettimeofday(&my_tv, cur_tb);
238
239 if (xtime.tv_sec <= my_tv.tv_sec) {
240 xtime.tv_sec = my_tv.tv_sec;
241 xtime.tv_nsec = my_tv.tv_usec * 1000;
242 }
f2783c15 243#endif
1da177e4
LT
244}
245
246/*
f2783c15
PM
247 * There are two copies of tb_to_xs and stamp_xsec so that no
248 * lock is needed to access and use these values in
249 * do_gettimeofday. We alternate the copies and as long as a
250 * reasonable time elapses between changes, there will never
251 * be inconsistent values. ntpd has a minimum of one minute
252 * between updates.
1da177e4 253 */
f2783c15 254static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
5d14a18d 255 u64 new_tb_to_xs)
1da177e4 256{
1da177e4 257 unsigned temp_idx;
f2783c15 258 struct gettimeofday_vars *temp_varp;
1da177e4
LT
259
260 temp_idx = (do_gtod.var_idx == 0);
261 temp_varp = &do_gtod.vars[temp_idx];
262
f2783c15
PM
263 temp_varp->tb_to_xs = new_tb_to_xs;
264 temp_varp->tb_orig_stamp = new_tb_stamp;
1da177e4 265 temp_varp->stamp_xsec = new_stamp_xsec;
0d8d4d42 266 smp_mb();
1da177e4
LT
267 do_gtod.varp = temp_varp;
268 do_gtod.var_idx = temp_idx;
269
f2783c15
PM
270#ifdef CONFIG_PPC64
271 /*
272 * tb_update_count is used to allow the userspace gettimeofday code
273 * to assure itself that it sees a consistent view of the tb_to_xs and
274 * stamp_xsec variables. It reads the tb_update_count, then reads
275 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
276 * the two values of tb_update_count match and are even then the
277 * tb_to_xs and stamp_xsec values are consistent. If not, then it
278 * loops back and reads them again until this criteria is met.
279 */
1da177e4 280 ++(systemcfg->tb_update_count);
0d8d4d42 281 smp_wmb();
f2783c15 282 systemcfg->tb_orig_stamp = new_tb_stamp;
1da177e4 283 systemcfg->stamp_xsec = new_stamp_xsec;
f2783c15 284 systemcfg->tb_to_xs = new_tb_to_xs;
0d8d4d42 285 smp_wmb();
1da177e4 286 ++(systemcfg->tb_update_count);
f2783c15
PM
287#endif
288}
289
290/*
291 * When the timebase - tb_orig_stamp gets too big, we do a manipulation
292 * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
293 * difference tb - tb_orig_stamp small enough to always fit inside a
294 * 32 bits number. This is a requirement of our fast 32 bits userland
295 * implementation in the vdso. If we "miss" a call to this function
296 * (interrupt latency, CPU locked in a spinlock, ...) and we end up
297 * with a too big difference, then the vdso will fallback to calling
298 * the syscall
299 */
300static __inline__ void timer_recalc_offset(u64 cur_tb)
301{
302 unsigned long offset;
303 u64 new_stamp_xsec;
304
96c44507
PM
305 if (__USE_RTC())
306 return;
f2783c15
PM
307 offset = cur_tb - do_gtod.varp->tb_orig_stamp;
308 if ((offset & 0x80000000u) == 0)
309 return;
310 new_stamp_xsec = do_gtod.varp->stamp_xsec
311 + mulhdu(offset, do_gtod.varp->tb_to_xs);
312 update_gtod(cur_tb, new_stamp_xsec, do_gtod.varp->tb_to_xs);
1da177e4
LT
313}
314
315#ifdef CONFIG_SMP
316unsigned long profile_pc(struct pt_regs *regs)
317{
318 unsigned long pc = instruction_pointer(regs);
319
320 if (in_lock_functions(pc))
321 return regs->link;
322
323 return pc;
324}
325EXPORT_SYMBOL(profile_pc);
326#endif
327
328#ifdef CONFIG_PPC_ISERIES
329
330/*
331 * This function recalibrates the timebase based on the 49-bit time-of-day
332 * value in the Titan chip. The Titan is much more accurate than the value
333 * returned by the service processor for the timebase frequency.
334 */
335
336static void iSeries_tb_recal(void)
337{
338 struct div_result divres;
339 unsigned long titan, tb;
340 tb = get_tb();
341 titan = HvCallXm_loadTod();
342 if ( iSeries_recal_titan ) {
343 unsigned long tb_ticks = tb - iSeries_recal_tb;
344 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
345 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
346 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
347 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
348 char sign = '+';
349 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
350 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
351
352 if ( tick_diff < 0 ) {
353 tick_diff = -tick_diff;
354 sign = '-';
355 }
356 if ( tick_diff ) {
357 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
358 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
359 new_tb_ticks_per_jiffy, sign, tick_diff );
360 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
361 tb_ticks_per_sec = new_tb_ticks_per_sec;
362 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
363 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
364 tb_to_xs = divres.result_low;
365 do_gtod.varp->tb_to_xs = tb_to_xs;
366 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
367 systemcfg->tb_to_xs = tb_to_xs;
368 }
369 else {
370 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
371 " new tb_ticks_per_jiffy = %lu\n"
372 " old tb_ticks_per_jiffy = %lu\n",
373 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
374 }
375 }
376 }
377 iSeries_recal_titan = titan;
378 iSeries_recal_tb = tb;
379}
380#endif
381
382/*
383 * For iSeries shared processors, we have to let the hypervisor
384 * set the hardware decrementer. We set a virtual decrementer
385 * in the lppaca and call the hypervisor if the virtual
386 * decrementer is less than the current value in the hardware
387 * decrementer. (almost always the new decrementer value will
388 * be greater than the current hardware decementer so the hypervisor
389 * call will not be needed)
390 */
391
1da177e4
LT
392/*
393 * timer_interrupt - gets called when the decrementer overflows,
394 * with interrupts disabled.
395 */
c7aeffc4 396void timer_interrupt(struct pt_regs * regs)
1da177e4
LT
397{
398 int next_dec;
f2783c15
PM
399 int cpu = smp_processor_id();
400 unsigned long ticks;
401
402#ifdef CONFIG_PPC32
403 if (atomic_read(&ppc_n_lost_interrupts) != 0)
404 do_IRQ(regs);
405#endif
1da177e4
LT
406
407 irq_enter();
408
1da177e4 409 profile_tick(CPU_PROFILING, regs);
1da177e4 410
f2783c15
PM
411#ifdef CONFIG_PPC_ISERIES
412 get_paca()->lppaca.int_dword.fields.decr_int = 0;
413#endif
414
415 while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
416 >= tb_ticks_per_jiffy) {
417 /* Update last_jiffy */
418 per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy;
419 /* Handle RTCL overflow on 601 */
420 if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000)
421 per_cpu(last_jiffy, cpu) -= 1000000000;
1da177e4 422
1da177e4
LT
423 /*
424 * We cannot disable the decrementer, so in the period
425 * between this cpu's being marked offline in cpu_online_map
426 * and calling stop-self, it is taking timer interrupts.
427 * Avoid calling into the scheduler rebalancing code if this
428 * is the case.
429 */
430 if (!cpu_is_offline(cpu))
431 update_process_times(user_mode(regs));
f2783c15 432
1da177e4
LT
433 /*
434 * No need to check whether cpu is offline here; boot_cpuid
435 * should have been fixed up by now.
436 */
f2783c15
PM
437 if (cpu != boot_cpuid)
438 continue;
439
440 write_seqlock(&xtime_lock);
96c44507
PM
441 tb_last_jiffy += tb_ticks_per_jiffy;
442 tb_last_stamp = per_cpu(last_jiffy, cpu);
443 timer_recalc_offset(tb_last_jiffy);
f2783c15 444 do_timer(regs);
96c44507 445 timer_sync_xtime(tb_last_jiffy);
f2783c15
PM
446 timer_check_rtc();
447 write_sequnlock(&xtime_lock);
448 if (adjusting_time && (time_adjust == 0))
449 ppc_adjtimex();
1da177e4
LT
450 }
451
f2783c15 452 next_dec = tb_ticks_per_jiffy - ticks;
1da177e4
LT
453 set_dec(next_dec);
454
455#ifdef CONFIG_PPC_ISERIES
937b31b1 456 if (hvlpevent_is_pending())
74889802 457 process_hvlpevents(regs);
1da177e4
LT
458#endif
459
f2783c15 460#ifdef CONFIG_PPC64
8d15a3e5 461 /* collect purr register values often, for accurate calculations */
1ababe11 462 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
1da177e4
LT
463 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
464 cu->current_tb = mfspr(SPRN_PURR);
465 }
f2783c15 466#endif
1da177e4
LT
467
468 irq_exit();
1da177e4
LT
469}
470
f2783c15
PM
471void wakeup_decrementer(void)
472{
473 int i;
474
475 set_dec(tb_ticks_per_jiffy);
476 /*
477 * We don't expect this to be called on a machine with a 601,
478 * so using get_tbl is fine.
479 */
96c44507 480 tb_last_stamp = tb_last_jiffy = get_tb();
f2783c15
PM
481 for_each_cpu(i)
482 per_cpu(last_jiffy, i) = tb_last_stamp;
483}
484
a5b518ed 485#ifdef CONFIG_SMP
f2783c15
PM
486void __init smp_space_timers(unsigned int max_cpus)
487{
488 int i;
489 unsigned long offset = tb_ticks_per_jiffy / max_cpus;
490 unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
491
492 for_each_cpu(i) {
493 if (i != boot_cpuid) {
494 previous_tb += offset;
495 per_cpu(last_jiffy, i) = previous_tb;
496 }
497 }
498}
499#endif
500
1da177e4
LT
501/*
502 * Scheduler clock - returns current time in nanosec units.
503 *
504 * Note: mulhdu(a, b) (multiply high double unsigned) returns
505 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
506 * are 64-bit unsigned numbers.
507 */
508unsigned long long sched_clock(void)
509{
96c44507
PM
510 if (__USE_RTC())
511 return get_rtc();
1da177e4
LT
512 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
513}
514
515int do_settimeofday(struct timespec *tv)
516{
517 time_t wtm_sec, new_sec = tv->tv_sec;
518 long wtm_nsec, new_nsec = tv->tv_nsec;
519 unsigned long flags;
1da177e4 520 long int tb_delta;
f2783c15 521 u64 new_xsec;
1da177e4
LT
522
523 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
524 return -EINVAL;
525
526 write_seqlock_irqsave(&xtime_lock, flags);
f2783c15
PM
527
528 /*
529 * Updating the RTC is not the job of this code. If the time is
530 * stepped under NTP, the RTC will be updated after STA_UNSYNC
531 * is cleared. Tools like clock/hwclock either copy the RTC
1da177e4
LT
532 * to the system time, in which case there is no point in writing
533 * to the RTC again, or write to the RTC but then they don't call
534 * settimeofday to perform this operation.
535 */
536#ifdef CONFIG_PPC_ISERIES
f2783c15 537 if (first_settimeofday) {
1da177e4
LT
538 iSeries_tb_recal();
539 first_settimeofday = 0;
540 }
541#endif
542 tb_delta = tb_ticks_since(tb_last_stamp);
543 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
544
f2783c15 545 new_nsec -= 1000 * mulhwu(tb_to_us, tb_delta);
1da177e4
LT
546
547 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
548 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
549
550 set_normalized_timespec(&xtime, new_sec, new_nsec);
551 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
552
553 /* In case of a large backwards jump in time with NTP, we want the
554 * clock to be updated as soon as the PLL is again in lock.
555 */
556 last_rtc_update = new_sec - 658;
557
b149ee22 558 ntp_clear();
1da177e4 559
f2783c15
PM
560 new_xsec = (u64)new_nsec * XSEC_PER_SEC;
561 do_div(new_xsec, NSEC_PER_SEC);
562 new_xsec += (u64)new_sec * XSEC_PER_SEC;
96c44507 563 update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
1da177e4 564
f2783c15 565#ifdef CONFIG_PPC64
1da177e4
LT
566 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
567 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
f2783c15 568#endif
1da177e4
LT
569
570 write_sequnlock_irqrestore(&xtime_lock, flags);
571 clock_was_set();
572 return 0;
573}
574
575EXPORT_SYMBOL(do_settimeofday);
576
10f7e7c1
AB
577void __init generic_calibrate_decr(void)
578{
579 struct device_node *cpu;
10f7e7c1
AB
580 unsigned int *fp;
581 int node_found;
582
583 /*
584 * The cpu node should have a timebase-frequency property
585 * to tell us the rate at which the decrementer counts.
586 */
587 cpu = of_find_node_by_type(NULL, "cpu");
588
589 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
590 node_found = 0;
591 if (cpu != 0) {
592 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
593 NULL);
594 if (fp != 0) {
595 node_found = 1;
596 ppc_tb_freq = *fp;
597 }
598 }
599 if (!node_found)
600 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
601 "(not found)\n");
602
603 ppc_proc_freq = DEFAULT_PROC_FREQ;
604 node_found = 0;
605 if (cpu != 0) {
606 fp = (unsigned int *)get_property(cpu, "clock-frequency",
607 NULL);
608 if (fp != 0) {
609 node_found = 1;
610 ppc_proc_freq = *fp;
611 }
612 }
0fd6f717
KG
613#ifdef CONFIG_BOOKE
614 /* Set the time base to zero */
615 mtspr(SPRN_TBWL, 0);
616 mtspr(SPRN_TBWU, 0);
617
618 /* Clear any pending timer interrupts */
619 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
620
621 /* Enable decrementer interrupt */
622 mtspr(SPRN_TCR, TCR_DIE);
623#endif
10f7e7c1
AB
624 if (!node_found)
625 printk(KERN_ERR "WARNING: Estimating processor frequency "
626 "(not found)\n");
627
628 of_node_put(cpu);
10f7e7c1 629}
10f7e7c1 630
f2783c15
PM
631unsigned long get_boot_time(void)
632{
633 struct rtc_time tm;
634
635 if (ppc_md.get_boot_time)
636 return ppc_md.get_boot_time();
637 if (!ppc_md.get_rtc_time)
638 return 0;
639 ppc_md.get_rtc_time(&tm);
640 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
641 tm.tm_hour, tm.tm_min, tm.tm_sec);
642}
643
644/* This function is only called on the boot processor */
1da177e4
LT
645void __init time_init(void)
646{
1da177e4 647 unsigned long flags;
f2783c15 648 unsigned long tm = 0;
1da177e4 649 struct div_result res;
f2783c15
PM
650 u64 scale;
651 unsigned shift;
652
653 if (ppc_md.time_init != NULL)
654 timezone_offset = ppc_md.time_init();
1da177e4 655
96c44507
PM
656 if (__USE_RTC()) {
657 /* 601 processor: dec counts down by 128 every 128ns */
658 ppc_tb_freq = 1000000000;
659 tb_last_stamp = get_rtcl();
660 tb_last_jiffy = tb_last_stamp;
661 } else {
662 /* Normal PowerPC with timebase register */
663 ppc_md.calibrate_decr();
664 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
665 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
666 printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
667 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
668 tb_last_stamp = tb_last_jiffy = get_tb();
669 }
374e99d4
PM
670
671 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
672 tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
673 tb_ticks_per_usec = ppc_tb_freq / 1000000;
674 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
675 div128_by_32(1024*1024, 0, tb_ticks_per_sec, &res);
676 tb_to_xs = res.result_low;
677
f2783c15
PM
678#ifdef CONFIG_PPC64
679 get_paca()->default_decr = tb_ticks_per_jiffy;
680#endif
681
1da177e4
LT
682 /*
683 * Compute scale factor for sched_clock.
684 * The calibrate_decr() function has set tb_ticks_per_sec,
685 * which is the timebase frequency.
686 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
687 * the 128-bit result as a 64.64 fixed-point number.
688 * We then shift that number right until it is less than 1.0,
689 * giving us the scale factor and shift count to use in
690 * sched_clock().
691 */
692 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
693 scale = res.result_low;
694 for (shift = 0; res.result_high != 0; ++shift) {
695 scale = (scale >> 1) | (res.result_high << 63);
696 res.result_high >>= 1;
697 }
698 tb_to_ns_scale = scale;
699 tb_to_ns_shift = shift;
700
701#ifdef CONFIG_PPC_ISERIES
702 if (!piranha_simulator)
703#endif
f2783c15 704 tm = get_boot_time();
1da177e4
LT
705
706 write_seqlock_irqsave(&xtime_lock, flags);
f2783c15
PM
707 xtime.tv_sec = tm;
708 xtime.tv_nsec = 0;
1da177e4
LT
709 do_gtod.varp = &do_gtod.vars[0];
710 do_gtod.var_idx = 0;
96c44507 711 do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
f2783c15
PM
712 __get_cpu_var(last_jiffy) = tb_last_stamp;
713 do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
1da177e4
LT
714 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
715 do_gtod.varp->tb_to_xs = tb_to_xs;
716 do_gtod.tb_to_us = tb_to_us;
f2783c15 717#ifdef CONFIG_PPC64
96c44507 718 systemcfg->tb_orig_stamp = tb_last_jiffy;
1da177e4
LT
719 systemcfg->tb_update_count = 0;
720 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
721 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
722 systemcfg->tb_to_xs = tb_to_xs;
f2783c15 723#endif
1da177e4
LT
724
725 time_freq = 0;
726
f2783c15
PM
727 /* If platform provided a timezone (pmac), we correct the time */
728 if (timezone_offset) {
729 sys_tz.tz_minuteswest = -timezone_offset / 60;
730 sys_tz.tz_dsttime = 0;
731 xtime.tv_sec -= timezone_offset;
732 }
733
1da177e4
LT
734 last_rtc_update = xtime.tv_sec;
735 set_normalized_timespec(&wall_to_monotonic,
736 -xtime.tv_sec, -xtime.tv_nsec);
737 write_sequnlock_irqrestore(&xtime_lock, flags);
738
739 /* Not exact, but the timer interrupt takes care of this */
740 set_dec(tb_ticks_per_jiffy);
741}
742
743/*
744 * After adjtimex is called, adjust the conversion of tb ticks
745 * to microseconds to keep do_gettimeofday synchronized
746 * with ntpd.
747 *
748 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
749 * adjust the frequency.
750 */
751
752/* #define DEBUG_PPC_ADJTIMEX 1 */
753
754void ppc_adjtimex(void)
755{
f2783c15
PM
756#ifdef CONFIG_PPC64
757 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec,
758 new_tb_to_xs, new_xsec, new_stamp_xsec;
1da177e4
LT
759 unsigned long tb_ticks_per_sec_delta;
760 long delta_freq, ltemp;
761 struct div_result divres;
762 unsigned long flags;
1da177e4
LT
763 long singleshot_ppm = 0;
764
f2783c15
PM
765 /*
766 * Compute parts per million frequency adjustment to
767 * accomplish the time adjustment implied by time_offset to be
768 * applied over the elapsed time indicated by time_constant.
769 * Use SHIFT_USEC to get it into the same units as
770 * time_freq.
771 */
1da177e4
LT
772 if ( time_offset < 0 ) {
773 ltemp = -time_offset;
774 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
775 ltemp >>= SHIFT_KG + time_constant;
776 ltemp = -ltemp;
f2783c15 777 } else {
1da177e4
LT
778 ltemp = time_offset;
779 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
780 ltemp >>= SHIFT_KG + time_constant;
781 }
782
783 /* If there is a single shot time adjustment in progress */
784 if ( time_adjust ) {
785#ifdef DEBUG_PPC_ADJTIMEX
786 printk("ppc_adjtimex: ");
787 if ( adjusting_time == 0 )
788 printk("starting ");
789 printk("single shot time_adjust = %ld\n", time_adjust);
790#endif
791
792 adjusting_time = 1;
793
f2783c15
PM
794 /*
795 * Compute parts per million frequency adjustment
796 * to match time_adjust
797 */
1da177e4
LT
798 singleshot_ppm = tickadj * HZ;
799 /*
800 * The adjustment should be tickadj*HZ to match the code in
801 * linux/kernel/timer.c, but experiments show that this is too
802 * large. 3/4 of tickadj*HZ seems about right
803 */
804 singleshot_ppm -= singleshot_ppm / 4;
f2783c15 805 /* Use SHIFT_USEC to get it into the same units as time_freq */
1da177e4
LT
806 singleshot_ppm <<= SHIFT_USEC;
807 if ( time_adjust < 0 )
808 singleshot_ppm = -singleshot_ppm;
809 }
810 else {
811#ifdef DEBUG_PPC_ADJTIMEX
812 if ( adjusting_time )
813 printk("ppc_adjtimex: ending single shot time_adjust\n");
814#endif
815 adjusting_time = 0;
816 }
817
818 /* Add up all of the frequency adjustments */
819 delta_freq = time_freq + ltemp + singleshot_ppm;
820
f2783c15
PM
821 /*
822 * Compute a new value for tb_ticks_per_sec based on
823 * the frequency adjustment
824 */
1da177e4
LT
825 den = 1000000 * (1 << (SHIFT_USEC - 8));
826 if ( delta_freq < 0 ) {
827 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
828 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
829 }
830 else {
831 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
832 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
833 }
834
835#ifdef DEBUG_PPC_ADJTIMEX
836 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
837 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
838#endif
f2783c15
PM
839
840 /*
841 * Compute a new value of tb_to_xs (used to convert tb to
842 * microseconds) and a new value of stamp_xsec which is the
843 * time (in 1/2^20 second units) corresponding to
844 * tb_orig_stamp. This new value of stamp_xsec compensates
845 * for the change in frequency (implied by the new tb_to_xs)
846 * which guarantees that the current time remains the same.
847 */
1da177e4
LT
848 write_seqlock_irqsave( &xtime_lock, flags );
849 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
f2783c15 850 div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres);
1da177e4 851 new_tb_to_xs = divres.result_low;
f2783c15 852 new_xsec = mulhdu(tb_ticks, new_tb_to_xs);
1da177e4 853
f2783c15 854 old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs);
1da177e4
LT
855 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
856
f2783c15 857 update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs);
1da177e4
LT
858
859 write_sequnlock_irqrestore( &xtime_lock, flags );
f2783c15 860#endif /* CONFIG_PPC64 */
1da177e4
LT
861}
862
863
1da177e4
LT
864#define FEBRUARY 2
865#define STARTOFTIME 1970
866#define SECDAY 86400L
867#define SECYR (SECDAY * 365)
f2783c15
PM
868#define leapyear(year) ((year) % 4 == 0 && \
869 ((year) % 100 != 0 || (year) % 400 == 0))
1da177e4
LT
870#define days_in_year(a) (leapyear(a) ? 366 : 365)
871#define days_in_month(a) (month_days[(a) - 1])
872
873static int month_days[12] = {
874 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
875};
876
877/*
878 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
879 */
880void GregorianDay(struct rtc_time * tm)
881{
882 int leapsToDate;
883 int lastYear;
884 int day;
885 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
886
f2783c15 887 lastYear = tm->tm_year - 1;
1da177e4
LT
888
889 /*
890 * Number of leap corrections to apply up to end of last year
891 */
f2783c15 892 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1da177e4
LT
893
894 /*
895 * This year is a leap year if it is divisible by 4 except when it is
896 * divisible by 100 unless it is divisible by 400
897 *
f2783c15 898 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1da177e4 899 */
f2783c15 900 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1da177e4
LT
901
902 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
903 tm->tm_mday;
904
f2783c15 905 tm->tm_wday = day % 7;
1da177e4
LT
906}
907
908void to_tm(int tim, struct rtc_time * tm)
909{
910 register int i;
911 register long hms, day;
912
913 day = tim / SECDAY;
914 hms = tim % SECDAY;
915
916 /* Hours, minutes, seconds are easy */
917 tm->tm_hour = hms / 3600;
918 tm->tm_min = (hms % 3600) / 60;
919 tm->tm_sec = (hms % 3600) % 60;
920
921 /* Number of years in days */
922 for (i = STARTOFTIME; day >= days_in_year(i); i++)
923 day -= days_in_year(i);
924 tm->tm_year = i;
925
926 /* Number of months in days left */
927 if (leapyear(tm->tm_year))
928 days_in_month(FEBRUARY) = 29;
929 for (i = 1; day >= days_in_month(i); i++)
930 day -= days_in_month(i);
931 days_in_month(FEBRUARY) = 28;
932 tm->tm_mon = i;
933
934 /* Days are what is left over (+1) from all that. */
935 tm->tm_mday = day + 1;
936
937 /*
938 * Determine the day of week
939 */
940 GregorianDay(tm);
941}
942
943/* Auxiliary function to compute scaling factors */
944/* Actually the choice of a timebase running at 1/4 the of the bus
945 * frequency giving resolution of a few tens of nanoseconds is quite nice.
946 * It makes this computation very precise (27-28 bits typically) which
947 * is optimistic considering the stability of most processor clock
948 * oscillators and the precision with which the timebase frequency
949 * is measured but does not harm.
950 */
f2783c15
PM
951unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
952{
1da177e4
LT
953 unsigned mlt=0, tmp, err;
954 /* No concern for performance, it's done once: use a stupid
955 * but safe and compact method to find the multiplier.
956 */
957
958 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
f2783c15
PM
959 if (mulhwu(inscale, mlt|tmp) < outscale)
960 mlt |= tmp;
1da177e4
LT
961 }
962
963 /* We might still be off by 1 for the best approximation.
964 * A side effect of this is that if outscale is too large
965 * the returned value will be zero.
966 * Many corner cases have been checked and seem to work,
967 * some might have been forgotten in the test however.
968 */
969
f2783c15
PM
970 err = inscale * (mlt+1);
971 if (err <= inscale/2)
972 mlt++;
1da177e4 973 return mlt;
f2783c15 974}
1da177e4
LT
975
976/*
977 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
978 * result.
979 */
f2783c15
PM
980void div128_by_32(u64 dividend_high, u64 dividend_low,
981 unsigned divisor, struct div_result *dr)
1da177e4 982{
f2783c15
PM
983 unsigned long a, b, c, d;
984 unsigned long w, x, y, z;
985 u64 ra, rb, rc;
1da177e4
LT
986
987 a = dividend_high >> 32;
988 b = dividend_high & 0xffffffff;
989 c = dividend_low >> 32;
990 d = dividend_low & 0xffffffff;
991
f2783c15
PM
992 w = a / divisor;
993 ra = ((u64)(a - (w * divisor)) << 32) + b;
994
f2783c15
PM
995 rb = ((u64) do_div(ra, divisor) << 32) + c;
996 x = ra;
1da177e4 997
f2783c15
PM
998 rc = ((u64) do_div(rb, divisor) << 32) + d;
999 y = rb;
1000
1001 do_div(rc, divisor);
1002 z = rc;
1da177e4 1003
f2783c15
PM
1004 dr->result_high = ((u64)w << 32) + x;
1005 dr->result_low = ((u64)y << 32) + z;
1da177e4
LT
1006
1007}