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