2 * Common time routines among all ppc machines.
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)
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).
15 * Speeded up do_gettimeofday by getting rid of references to
16 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
18 * TODO (not necessarily in this file):
19 * - improve precision and reproducibility of timebase frequency
20 * measurement at boot time.
21 * - for astronomical applications: add a new function to get
22 * non ambiguous timestamps even around leap seconds. This needs
23 * a new timestamp format and a good name.
25 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
26 * "A Kernel Model for Precision Timekeeping" by Dave Mills
28 * This program is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU General Public License
30 * as published by the Free Software Foundation; either version
31 * 2 of the License, or (at your option) any later version.
34 #include <linux/errno.h>
35 #include <linux/export.h>
36 #include <linux/sched.h>
37 #include <linux/kernel.h>
38 #include <linux/param.h>
39 #include <linux/string.h>
41 #include <linux/interrupt.h>
42 #include <linux/timex.h>
43 #include <linux/kernel_stat.h>
44 #include <linux/time.h>
45 #include <linux/clockchips.h>
46 #include <linux/init.h>
47 #include <linux/profile.h>
48 #include <linux/cpu.h>
49 #include <linux/security.h>
50 #include <linux/percpu.h>
51 #include <linux/rtc.h>
52 #include <linux/jiffies.h>
53 #include <linux/posix-timers.h>
54 #include <linux/irq.h>
55 #include <linux/delay.h>
56 #include <linux/irq_work.h>
57 #include <linux/clk-provider.h>
58 #include <linux/suspend.h>
59 #include <asm/trace.h>
62 #include <asm/processor.h>
63 #include <asm/nvram.h>
64 #include <asm/cache.h>
65 #include <asm/machdep.h>
66 #include <asm/uaccess.h>
70 #include <asm/div64.h>
72 #include <asm/vdso_datapage.h>
73 #include <asm/firmware.h>
74 #include <asm/cputime.h>
76 /* powerpc clocksource/clockevent code */
78 #include <linux/clockchips.h>
79 #include <linux/timekeeper_internal.h>
81 static cycle_t
rtc_read(struct clocksource
*);
82 static struct clocksource clocksource_rtc
= {
85 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
86 .mask
= CLOCKSOURCE_MASK(64),
90 static cycle_t
timebase_read(struct clocksource
*);
91 static struct clocksource clocksource_timebase
= {
94 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
95 .mask
= CLOCKSOURCE_MASK(64),
96 .read
= timebase_read
,
99 #define DECREMENTER_MAX 0x7fffffff
101 static int decrementer_set_next_event(unsigned long evt
,
102 struct clock_event_device
*dev
);
103 static int decrementer_shutdown(struct clock_event_device
*evt
);
105 struct clock_event_device decrementer_clockevent
= {
106 .name
= "decrementer",
109 .set_next_event
= decrementer_set_next_event
,
110 .set_state_shutdown
= decrementer_shutdown
,
111 .tick_resume
= decrementer_shutdown
,
112 .features
= CLOCK_EVT_FEAT_ONESHOT
|
113 CLOCK_EVT_FEAT_C3STOP
,
115 EXPORT_SYMBOL(decrementer_clockevent
);
117 DEFINE_PER_CPU(u64
, decrementers_next_tb
);
118 static DEFINE_PER_CPU(struct clock_event_device
, decrementers
);
120 #define XSEC_PER_SEC (1024*1024)
123 #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
125 /* compute ((xsec << 12) * max) >> 32 */
126 #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
129 unsigned long tb_ticks_per_jiffy
;
130 unsigned long tb_ticks_per_usec
= 100; /* sane default */
131 EXPORT_SYMBOL(tb_ticks_per_usec
);
132 unsigned long tb_ticks_per_sec
;
133 EXPORT_SYMBOL(tb_ticks_per_sec
); /* for cputime_t conversions */
135 DEFINE_SPINLOCK(rtc_lock
);
136 EXPORT_SYMBOL_GPL(rtc_lock
);
138 static u64 tb_to_ns_scale __read_mostly
;
139 static unsigned tb_to_ns_shift __read_mostly
;
140 static u64 boot_tb __read_mostly
;
142 extern struct timezone sys_tz
;
143 static long timezone_offset
;
145 unsigned long ppc_proc_freq
;
146 EXPORT_SYMBOL_GPL(ppc_proc_freq
);
147 unsigned long ppc_tb_freq
;
148 EXPORT_SYMBOL_GPL(ppc_tb_freq
);
150 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
152 * Factors for converting from cputime_t (timebase ticks) to
153 * jiffies, microseconds, seconds, and clock_t (1/USER_HZ seconds).
154 * These are all stored as 0.64 fixed-point binary fractions.
156 u64 __cputime_jiffies_factor
;
157 EXPORT_SYMBOL(__cputime_jiffies_factor
);
158 u64 __cputime_usec_factor
;
159 EXPORT_SYMBOL(__cputime_usec_factor
);
160 u64 __cputime_sec_factor
;
161 EXPORT_SYMBOL(__cputime_sec_factor
);
162 u64 __cputime_clockt_factor
;
163 EXPORT_SYMBOL(__cputime_clockt_factor
);
164 DEFINE_PER_CPU(unsigned long, cputime_last_delta
);
165 DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta
);
167 cputime_t cputime_one_jiffy
;
169 void (*dtl_consumer
)(struct dtl_entry
*, u64
);
171 static void calc_cputime_factors(void)
173 struct div_result res
;
175 div128_by_32(HZ
, 0, tb_ticks_per_sec
, &res
);
176 __cputime_jiffies_factor
= res
.result_low
;
177 div128_by_32(1000000, 0, tb_ticks_per_sec
, &res
);
178 __cputime_usec_factor
= res
.result_low
;
179 div128_by_32(1, 0, tb_ticks_per_sec
, &res
);
180 __cputime_sec_factor
= res
.result_low
;
181 div128_by_32(USER_HZ
, 0, tb_ticks_per_sec
, &res
);
182 __cputime_clockt_factor
= res
.result_low
;
186 * Read the SPURR on systems that have it, otherwise the PURR,
187 * or if that doesn't exist return the timebase value passed in.
189 static u64
read_spurr(u64 tb
)
191 if (cpu_has_feature(CPU_FTR_SPURR
))
192 return mfspr(SPRN_SPURR
);
193 if (cpu_has_feature(CPU_FTR_PURR
))
194 return mfspr(SPRN_PURR
);
198 #ifdef CONFIG_PPC_SPLPAR
201 * Scan the dispatch trace log and count up the stolen time.
202 * Should be called with interrupts disabled.
204 static u64
scan_dispatch_log(u64 stop_tb
)
206 u64 i
= local_paca
->dtl_ridx
;
207 struct dtl_entry
*dtl
= local_paca
->dtl_curr
;
208 struct dtl_entry
*dtl_end
= local_paca
->dispatch_log_end
;
209 struct lppaca
*vpa
= local_paca
->lppaca_ptr
;
217 if (i
== be64_to_cpu(vpa
->dtl_idx
))
219 while (i
< be64_to_cpu(vpa
->dtl_idx
)) {
220 dtb
= be64_to_cpu(dtl
->timebase
);
221 tb_delta
= be32_to_cpu(dtl
->enqueue_to_dispatch_time
) +
222 be32_to_cpu(dtl
->ready_to_enqueue_time
);
224 if (i
+ N_DISPATCH_LOG
< be64_to_cpu(vpa
->dtl_idx
)) {
225 /* buffer has overflowed */
226 i
= be64_to_cpu(vpa
->dtl_idx
) - N_DISPATCH_LOG
;
227 dtl
= local_paca
->dispatch_log
+ (i
% N_DISPATCH_LOG
);
233 dtl_consumer(dtl
, i
);
238 dtl
= local_paca
->dispatch_log
;
240 local_paca
->dtl_ridx
= i
;
241 local_paca
->dtl_curr
= dtl
;
246 * Accumulate stolen time by scanning the dispatch trace log.
247 * Called on entry from user mode.
249 void accumulate_stolen_time(void)
253 u8 save_soft_enabled
= local_paca
->soft_enabled
;
255 /* We are called early in the exception entry, before
256 * soft/hard_enabled are sync'ed to the expected state
257 * for the exception. We are hard disabled but the PACA
258 * needs to reflect that so various debug stuff doesn't
261 local_paca
->soft_enabled
= 0;
263 sst
= scan_dispatch_log(local_paca
->starttime_user
);
264 ust
= scan_dispatch_log(local_paca
->starttime
);
265 local_paca
->system_time
-= sst
;
266 local_paca
->user_time
-= ust
;
267 local_paca
->stolen_time
+= ust
+ sst
;
269 local_paca
->soft_enabled
= save_soft_enabled
;
272 static inline u64
calculate_stolen_time(u64 stop_tb
)
276 if (get_paca()->dtl_ridx
!= be64_to_cpu(get_lppaca()->dtl_idx
)) {
277 stolen
= scan_dispatch_log(stop_tb
);
278 get_paca()->system_time
-= stolen
;
281 stolen
+= get_paca()->stolen_time
;
282 get_paca()->stolen_time
= 0;
286 #else /* CONFIG_PPC_SPLPAR */
287 static inline u64
calculate_stolen_time(u64 stop_tb
)
292 #endif /* CONFIG_PPC_SPLPAR */
295 * Account time for a transition between system, hard irq
298 static u64
vtime_delta(struct task_struct
*tsk
,
299 u64
*sys_scaled
, u64
*stolen
)
301 u64 now
, nowscaled
, deltascaled
;
302 u64 udelta
, delta
, user_scaled
;
304 WARN_ON_ONCE(!irqs_disabled());
307 nowscaled
= read_spurr(now
);
308 get_paca()->system_time
+= now
- get_paca()->starttime
;
309 get_paca()->starttime
= now
;
310 deltascaled
= nowscaled
- get_paca()->startspurr
;
311 get_paca()->startspurr
= nowscaled
;
313 *stolen
= calculate_stolen_time(now
);
315 delta
= get_paca()->system_time
;
316 get_paca()->system_time
= 0;
317 udelta
= get_paca()->user_time
- get_paca()->utime_sspurr
;
318 get_paca()->utime_sspurr
= get_paca()->user_time
;
321 * Because we don't read the SPURR on every kernel entry/exit,
322 * deltascaled includes both user and system SPURR ticks.
323 * Apportion these ticks to system SPURR ticks and user
324 * SPURR ticks in the same ratio as the system time (delta)
325 * and user time (udelta) values obtained from the timebase
326 * over the same interval. The system ticks get accounted here;
327 * the user ticks get saved up in paca->user_time_scaled to be
328 * used by account_process_tick.
331 user_scaled
= udelta
;
332 if (deltascaled
!= delta
+ udelta
) {
334 *sys_scaled
= deltascaled
* delta
/ (delta
+ udelta
);
335 user_scaled
= deltascaled
- *sys_scaled
;
337 *sys_scaled
= deltascaled
;
340 get_paca()->user_time_scaled
+= user_scaled
;
345 void vtime_account_system(struct task_struct
*tsk
)
347 u64 delta
, sys_scaled
, stolen
;
349 delta
= vtime_delta(tsk
, &sys_scaled
, &stolen
);
350 account_system_time(tsk
, 0, delta
, sys_scaled
);
352 account_steal_time(stolen
);
354 EXPORT_SYMBOL_GPL(vtime_account_system
);
356 void vtime_account_idle(struct task_struct
*tsk
)
358 u64 delta
, sys_scaled
, stolen
;
360 delta
= vtime_delta(tsk
, &sys_scaled
, &stolen
);
361 account_idle_time(delta
+ stolen
);
365 * Transfer the user time accumulated in the paca
366 * by the exception entry and exit code to the generic
367 * process user time records.
368 * Must be called with interrupts disabled.
369 * Assumes that vtime_account_system/idle() has been called
370 * recently (i.e. since the last entry from usermode) so that
371 * get_paca()->user_time_scaled is up to date.
373 void vtime_account_user(struct task_struct
*tsk
)
375 cputime_t utime
, utimescaled
;
377 utime
= get_paca()->user_time
;
378 utimescaled
= get_paca()->user_time_scaled
;
379 get_paca()->user_time
= 0;
380 get_paca()->user_time_scaled
= 0;
381 get_paca()->utime_sspurr
= 0;
382 account_user_time(tsk
, utime
, utimescaled
);
385 #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
386 #define calc_cputime_factors()
389 void __delay(unsigned long loops
)
397 /* the RTCL register wraps at 1000000000 */
398 diff
= get_rtcl() - start
;
401 } while (diff
< loops
);
404 while (get_tbl() - start
< loops
)
409 EXPORT_SYMBOL(__delay
);
411 void udelay(unsigned long usecs
)
413 __delay(tb_ticks_per_usec
* usecs
);
415 EXPORT_SYMBOL(udelay
);
418 unsigned long profile_pc(struct pt_regs
*regs
)
420 unsigned long pc
= instruction_pointer(regs
);
422 if (in_lock_functions(pc
))
427 EXPORT_SYMBOL(profile_pc
);
430 #ifdef CONFIG_IRQ_WORK
433 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
436 static inline unsigned long test_irq_work_pending(void)
440 asm volatile("lbz %0,%1(13)"
442 : "i" (offsetof(struct paca_struct
, irq_work_pending
)));
446 static inline void set_irq_work_pending_flag(void)
448 asm volatile("stb %0,%1(13)" : :
450 "i" (offsetof(struct paca_struct
, irq_work_pending
)));
453 static inline void clear_irq_work_pending(void)
455 asm volatile("stb %0,%1(13)" : :
457 "i" (offsetof(struct paca_struct
, irq_work_pending
)));
462 DEFINE_PER_CPU(u8
, irq_work_pending
);
464 #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
465 #define test_irq_work_pending() __this_cpu_read(irq_work_pending)
466 #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
468 #endif /* 32 vs 64 bit */
470 void arch_irq_work_raise(void)
473 set_irq_work_pending_flag();
478 #else /* CONFIG_IRQ_WORK */
480 #define test_irq_work_pending() 0
481 #define clear_irq_work_pending()
483 #endif /* CONFIG_IRQ_WORK */
485 static void __timer_interrupt(void)
487 struct pt_regs
*regs
= get_irq_regs();
488 u64
*next_tb
= this_cpu_ptr(&decrementers_next_tb
);
489 struct clock_event_device
*evt
= this_cpu_ptr(&decrementers
);
492 trace_timer_interrupt_entry(regs
);
494 if (test_irq_work_pending()) {
495 clear_irq_work_pending();
499 now
= get_tb_or_rtc();
500 if (now
>= *next_tb
) {
502 if (evt
->event_handler
)
503 evt
->event_handler(evt
);
504 __this_cpu_inc(irq_stat
.timer_irqs_event
);
506 now
= *next_tb
- now
;
507 if (now
<= DECREMENTER_MAX
)
509 /* We may have raced with new irq work */
510 if (test_irq_work_pending())
512 __this_cpu_inc(irq_stat
.timer_irqs_others
);
516 /* collect purr register values often, for accurate calculations */
517 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
518 struct cpu_usage
*cu
= this_cpu_ptr(&cpu_usage_array
);
519 cu
->current_tb
= mfspr(SPRN_PURR
);
523 trace_timer_interrupt_exit(regs
);
527 * timer_interrupt - gets called when the decrementer overflows,
528 * with interrupts disabled.
530 void timer_interrupt(struct pt_regs
* regs
)
532 struct pt_regs
*old_regs
;
533 u64
*next_tb
= this_cpu_ptr(&decrementers_next_tb
);
535 /* Ensure a positive value is written to the decrementer, or else
536 * some CPUs will continue to take decrementer exceptions.
538 set_dec(DECREMENTER_MAX
);
540 /* Some implementations of hotplug will get timer interrupts while
541 * offline, just ignore these and we also need to set
542 * decrementers_next_tb as MAX to make sure __check_irq_replay
543 * don't replay timer interrupt when return, otherwise we'll trap
546 if (!cpu_online(smp_processor_id())) {
551 /* Conditionally hard-enable interrupts now that the DEC has been
552 * bumped to its maximum value
554 may_hard_irq_enable();
557 #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
558 if (atomic_read(&ppc_n_lost_interrupts
) != 0)
562 old_regs
= set_irq_regs(regs
);
567 set_irq_regs(old_regs
);
571 * Hypervisor decrementer interrupts shouldn't occur but are sometimes
572 * left pending on exit from a KVM guest. We don't need to do anything
573 * to clear them, as they are edge-triggered.
575 void hdec_interrupt(struct pt_regs
*regs
)
579 #ifdef CONFIG_SUSPEND
580 static void generic_suspend_disable_irqs(void)
582 /* Disable the decrementer, so that it doesn't interfere
586 set_dec(DECREMENTER_MAX
);
588 set_dec(DECREMENTER_MAX
);
591 static void generic_suspend_enable_irqs(void)
596 /* Overrides the weak version in kernel/power/main.c */
597 void arch_suspend_disable_irqs(void)
599 if (ppc_md
.suspend_disable_irqs
)
600 ppc_md
.suspend_disable_irqs();
601 generic_suspend_disable_irqs();
604 /* Overrides the weak version in kernel/power/main.c */
605 void arch_suspend_enable_irqs(void)
607 generic_suspend_enable_irqs();
608 if (ppc_md
.suspend_enable_irqs
)
609 ppc_md
.suspend_enable_irqs();
613 unsigned long long tb_to_ns(unsigned long long ticks
)
615 return mulhdu(ticks
, tb_to_ns_scale
) << tb_to_ns_shift
;
617 EXPORT_SYMBOL_GPL(tb_to_ns
);
620 * Scheduler clock - returns current time in nanosec units.
622 * Note: mulhdu(a, b) (multiply high double unsigned) returns
623 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
624 * are 64-bit unsigned numbers.
626 unsigned long long sched_clock(void)
630 return mulhdu(get_tb() - boot_tb
, tb_to_ns_scale
) << tb_to_ns_shift
;
634 #ifdef CONFIG_PPC_PSERIES
637 * Running clock - attempts to give a view of time passing for a virtualised
639 * Uses the VTB register if available otherwise a next best guess.
641 unsigned long long running_clock(void)
644 * Don't read the VTB as a host since KVM does not switch in host
645 * timebase into the VTB when it takes a guest off the CPU, reading the
646 * VTB would result in reading 'last switched out' guest VTB.
648 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
649 * would be unsafe to rely only on the #ifdef above.
651 if (firmware_has_feature(FW_FEATURE_LPAR
) &&
652 cpu_has_feature(CPU_FTR_ARCH_207S
))
653 return mulhdu(get_vtb() - boot_tb
, tb_to_ns_scale
) << tb_to_ns_shift
;
656 * This is a next best approximation without a VTB.
657 * On a host which is running bare metal there should never be any stolen
658 * time and on a host which doesn't do any virtualisation TB *should* equal
659 * VTB so it makes no difference anyway.
661 return local_clock() - cputime_to_nsecs(kcpustat_this_cpu
->cpustat
[CPUTIME_STEAL
]);
665 static int __init
get_freq(char *name
, int cells
, unsigned long *val
)
667 struct device_node
*cpu
;
671 /* The cpu node should have timebase and clock frequency properties */
672 cpu
= of_find_node_by_type(NULL
, "cpu");
675 fp
= of_get_property(cpu
, name
, NULL
);
678 *val
= of_read_ulong(fp
, cells
);
687 static void start_cpu_decrementer(void)
689 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
690 /* Clear any pending timer interrupts */
691 mtspr(SPRN_TSR
, TSR_ENW
| TSR_WIS
| TSR_DIS
| TSR_FIS
);
693 /* Enable decrementer interrupt */
694 mtspr(SPRN_TCR
, TCR_DIE
);
695 #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
698 void __init
generic_calibrate_decr(void)
700 ppc_tb_freq
= DEFAULT_TB_FREQ
; /* hardcoded default */
702 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq
) &&
703 !get_freq("timebase-frequency", 1, &ppc_tb_freq
)) {
705 printk(KERN_ERR
"WARNING: Estimating decrementer frequency "
709 ppc_proc_freq
= DEFAULT_PROC_FREQ
; /* hardcoded default */
711 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq
) &&
712 !get_freq("clock-frequency", 1, &ppc_proc_freq
)) {
714 printk(KERN_ERR
"WARNING: Estimating processor frequency "
719 int update_persistent_clock(struct timespec now
)
723 if (!ppc_md
.set_rtc_time
)
726 to_tm(now
.tv_sec
+ 1 + timezone_offset
, &tm
);
730 return ppc_md
.set_rtc_time(&tm
);
733 static void __read_persistent_clock(struct timespec
*ts
)
736 static int first
= 1;
739 /* XXX this is a litle fragile but will work okay in the short term */
742 if (ppc_md
.time_init
)
743 timezone_offset
= ppc_md
.time_init();
745 /* get_boot_time() isn't guaranteed to be safe to call late */
746 if (ppc_md
.get_boot_time
) {
747 ts
->tv_sec
= ppc_md
.get_boot_time() - timezone_offset
;
751 if (!ppc_md
.get_rtc_time
) {
755 ppc_md
.get_rtc_time(&tm
);
757 ts
->tv_sec
= mktime(tm
.tm_year
+1900, tm
.tm_mon
+1, tm
.tm_mday
,
758 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
761 void read_persistent_clock(struct timespec
*ts
)
763 __read_persistent_clock(ts
);
765 /* Sanitize it in case real time clock is set below EPOCH */
766 if (ts
->tv_sec
< 0) {
773 /* clocksource code */
774 static cycle_t
rtc_read(struct clocksource
*cs
)
776 return (cycle_t
)get_rtc();
779 static cycle_t
timebase_read(struct clocksource
*cs
)
781 return (cycle_t
)get_tb();
784 void update_vsyscall_old(struct timespec
*wall_time
, struct timespec
*wtm
,
785 struct clocksource
*clock
, u32 mult
, cycle_t cycle_last
)
787 u64 new_tb_to_xs
, new_stamp_xsec
;
790 if (clock
!= &clocksource_timebase
)
793 /* Make userspace gettimeofday spin until we're done. */
794 ++vdso_data
->tb_update_count
;
797 /* 19342813113834067 ~= 2^(20+64) / 1e9 */
798 new_tb_to_xs
= (u64
) mult
* (19342813113834067ULL >> clock
->shift
);
799 new_stamp_xsec
= (u64
) wall_time
->tv_nsec
* XSEC_PER_SEC
;
800 do_div(new_stamp_xsec
, 1000000000);
801 new_stamp_xsec
+= (u64
) wall_time
->tv_sec
* XSEC_PER_SEC
;
803 BUG_ON(wall_time
->tv_nsec
>= NSEC_PER_SEC
);
804 /* this is tv_nsec / 1e9 as a 0.32 fraction */
805 frac_sec
= ((u64
) wall_time
->tv_nsec
* 18446744073ULL) >> 32;
808 * tb_update_count is used to allow the userspace gettimeofday code
809 * to assure itself that it sees a consistent view of the tb_to_xs and
810 * stamp_xsec variables. It reads the tb_update_count, then reads
811 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
812 * the two values of tb_update_count match and are even then the
813 * tb_to_xs and stamp_xsec values are consistent. If not, then it
814 * loops back and reads them again until this criteria is met.
815 * We expect the caller to have done the first increment of
816 * vdso_data->tb_update_count already.
818 vdso_data
->tb_orig_stamp
= cycle_last
;
819 vdso_data
->stamp_xsec
= new_stamp_xsec
;
820 vdso_data
->tb_to_xs
= new_tb_to_xs
;
821 vdso_data
->wtom_clock_sec
= wtm
->tv_sec
;
822 vdso_data
->wtom_clock_nsec
= wtm
->tv_nsec
;
823 vdso_data
->stamp_xtime
= *wall_time
;
824 vdso_data
->stamp_sec_fraction
= frac_sec
;
826 ++(vdso_data
->tb_update_count
);
829 void update_vsyscall_tz(void)
831 vdso_data
->tz_minuteswest
= sys_tz
.tz_minuteswest
;
832 vdso_data
->tz_dsttime
= sys_tz
.tz_dsttime
;
835 static void __init
clocksource_init(void)
837 struct clocksource
*clock
;
840 clock
= &clocksource_rtc
;
842 clock
= &clocksource_timebase
;
844 if (clocksource_register_hz(clock
, tb_ticks_per_sec
)) {
845 printk(KERN_ERR
"clocksource: %s is already registered\n",
850 printk(KERN_INFO
"clocksource: %s mult[%x] shift[%d] registered\n",
851 clock
->name
, clock
->mult
, clock
->shift
);
854 static int decrementer_set_next_event(unsigned long evt
,
855 struct clock_event_device
*dev
)
857 __this_cpu_write(decrementers_next_tb
, get_tb_or_rtc() + evt
);
860 /* We may have raced with new irq work */
861 if (test_irq_work_pending())
867 static int decrementer_shutdown(struct clock_event_device
*dev
)
869 decrementer_set_next_event(DECREMENTER_MAX
, dev
);
873 /* Interrupt handler for the timer broadcast IPI */
874 void tick_broadcast_ipi_handler(void)
876 u64
*next_tb
= this_cpu_ptr(&decrementers_next_tb
);
878 *next_tb
= get_tb_or_rtc();
882 static void register_decrementer_clockevent(int cpu
)
884 struct clock_event_device
*dec
= &per_cpu(decrementers
, cpu
);
886 *dec
= decrementer_clockevent
;
887 dec
->cpumask
= cpumask_of(cpu
);
889 printk_once(KERN_DEBUG
"clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
890 dec
->name
, dec
->mult
, dec
->shift
, cpu
);
892 clockevents_register_device(dec
);
895 static void __init
init_decrementer_clockevent(void)
897 int cpu
= smp_processor_id();
899 clockevents_calc_mult_shift(&decrementer_clockevent
, ppc_tb_freq
, 4);
901 decrementer_clockevent
.max_delta_ns
=
902 clockevent_delta2ns(DECREMENTER_MAX
, &decrementer_clockevent
);
903 decrementer_clockevent
.min_delta_ns
=
904 clockevent_delta2ns(2, &decrementer_clockevent
);
906 register_decrementer_clockevent(cpu
);
909 void secondary_cpu_time_init(void)
911 /* Start the decrementer on CPUs that have manual control
914 start_cpu_decrementer();
916 /* FIME: Should make unrelatred change to move snapshot_timebase
918 register_decrementer_clockevent(smp_processor_id());
921 /* This function is only called on the boot processor */
922 void __init
time_init(void)
924 struct div_result res
;
929 /* 601 processor: dec counts down by 128 every 128ns */
930 ppc_tb_freq
= 1000000000;
932 /* Normal PowerPC with timebase register */
933 ppc_md
.calibrate_decr();
934 printk(KERN_DEBUG
"time_init: decrementer frequency = %lu.%.6lu MHz\n",
935 ppc_tb_freq
/ 1000000, ppc_tb_freq
% 1000000);
936 printk(KERN_DEBUG
"time_init: processor frequency = %lu.%.6lu MHz\n",
937 ppc_proc_freq
/ 1000000, ppc_proc_freq
% 1000000);
940 tb_ticks_per_jiffy
= ppc_tb_freq
/ HZ
;
941 tb_ticks_per_sec
= ppc_tb_freq
;
942 tb_ticks_per_usec
= ppc_tb_freq
/ 1000000;
943 calc_cputime_factors();
944 setup_cputime_one_jiffy();
947 * Compute scale factor for sched_clock.
948 * The calibrate_decr() function has set tb_ticks_per_sec,
949 * which is the timebase frequency.
950 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
951 * the 128-bit result as a 64.64 fixed-point number.
952 * We then shift that number right until it is less than 1.0,
953 * giving us the scale factor and shift count to use in
956 div128_by_32(1000000000, 0, tb_ticks_per_sec
, &res
);
957 scale
= res
.result_low
;
958 for (shift
= 0; res
.result_high
!= 0; ++shift
) {
959 scale
= (scale
>> 1) | (res
.result_high
<< 63);
960 res
.result_high
>>= 1;
962 tb_to_ns_scale
= scale
;
963 tb_to_ns_shift
= shift
;
964 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
965 boot_tb
= get_tb_or_rtc();
967 /* If platform provided a timezone (pmac), we correct the time */
968 if (timezone_offset
) {
969 sys_tz
.tz_minuteswest
= -timezone_offset
/ 60;
970 sys_tz
.tz_dsttime
= 0;
973 vdso_data
->tb_update_count
= 0;
974 vdso_data
->tb_ticks_per_sec
= tb_ticks_per_sec
;
976 /* Start the decrementer on CPUs that have manual control
979 start_cpu_decrementer();
981 /* Register the clocksource */
984 init_decrementer_clockevent();
985 tick_setup_hrtimer_broadcast();
987 #ifdef CONFIG_COMMON_CLK
994 #define STARTOFTIME 1970
995 #define SECDAY 86400L
996 #define SECYR (SECDAY * 365)
997 #define leapyear(year) ((year) % 4 == 0 && \
998 ((year) % 100 != 0 || (year) % 400 == 0))
999 #define days_in_year(a) (leapyear(a) ? 366 : 365)
1000 #define days_in_month(a) (month_days[(a) - 1])
1002 static int month_days
[12] = {
1003 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1006 void to_tm(int tim
, struct rtc_time
* tm
)
1009 register long hms
, day
;
1014 /* Hours, minutes, seconds are easy */
1015 tm
->tm_hour
= hms
/ 3600;
1016 tm
->tm_min
= (hms
% 3600) / 60;
1017 tm
->tm_sec
= (hms
% 3600) % 60;
1019 /* Number of years in days */
1020 for (i
= STARTOFTIME
; day
>= days_in_year(i
); i
++)
1021 day
-= days_in_year(i
);
1024 /* Number of months in days left */
1025 if (leapyear(tm
->tm_year
))
1026 days_in_month(FEBRUARY
) = 29;
1027 for (i
= 1; day
>= days_in_month(i
); i
++)
1028 day
-= days_in_month(i
);
1029 days_in_month(FEBRUARY
) = 28;
1032 /* Days are what is left over (+1) from all that. */
1033 tm
->tm_mday
= day
+ 1;
1036 * No-one uses the day of the week.
1040 EXPORT_SYMBOL(to_tm
);
1043 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1046 void div128_by_32(u64 dividend_high
, u64 dividend_low
,
1047 unsigned divisor
, struct div_result
*dr
)
1049 unsigned long a
, b
, c
, d
;
1050 unsigned long w
, x
, y
, z
;
1053 a
= dividend_high
>> 32;
1054 b
= dividend_high
& 0xffffffff;
1055 c
= dividend_low
>> 32;
1056 d
= dividend_low
& 0xffffffff;
1059 ra
= ((u64
)(a
- (w
* divisor
)) << 32) + b
;
1061 rb
= ((u64
) do_div(ra
, divisor
) << 32) + c
;
1064 rc
= ((u64
) do_div(rb
, divisor
) << 32) + d
;
1067 do_div(rc
, divisor
);
1070 dr
->result_high
= ((u64
)w
<< 32) + x
;
1071 dr
->result_low
= ((u64
)y
<< 32) + z
;
1075 /* We don't need to calibrate delay, we use the CPU timebase for that */
1076 void calibrate_delay(void)
1078 /* Some generic code (such as spinlock debug) use loops_per_jiffy
1079 * as the number of __delay(1) in a jiffy, so make it so
1081 loops_per_jiffy
= tb_ticks_per_jiffy
;
1084 static int __init
rtc_init(void)
1086 struct platform_device
*pdev
;
1088 if (!ppc_md
.get_rtc_time
)
1091 pdev
= platform_device_register_simple("rtc-generic", -1, NULL
, 0);
1093 return PTR_ERR_OR_ZERO(pdev
);
1096 device_initcall(rtc_init
);