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 <asm/trace.h>
61 #include <asm/processor.h>
62 #include <asm/nvram.h>
63 #include <asm/cache.h>
64 #include <asm/machdep.h>
65 #include <asm/uaccess.h>
69 #include <asm/div64.h>
71 #include <asm/vdso_datapage.h>
72 #include <asm/firmware.h>
73 #include <asm/cputime.h>
75 /* powerpc clocksource/clockevent code */
77 #include <linux/clockchips.h>
78 #include <linux/timekeeper_internal.h>
80 static cycle_t rtc_read(struct clocksource *);
81 static struct clocksource clocksource_rtc = {
84 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
85 .mask = CLOCKSOURCE_MASK(64),
89 static cycle_t timebase_read(struct clocksource *);
90 static struct clocksource clocksource_timebase = {
93 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
94 .mask = CLOCKSOURCE_MASK(64),
95 .read = timebase_read,
98 #define DECREMENTER_MAX 0x7fffffff
100 static int decrementer_set_next_event(unsigned long evt,
101 struct clock_event_device *dev);
102 static void decrementer_set_mode(enum clock_event_mode mode,
103 struct clock_event_device *dev);
105 struct clock_event_device decrementer_clockevent = {
106 .name = "decrementer",
109 .set_next_event = decrementer_set_next_event,
110 .set_mode = decrementer_set_mode,
111 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP,
113 EXPORT_SYMBOL(decrementer_clockevent);
115 DEFINE_PER_CPU(u64, decrementers_next_tb);
116 static DEFINE_PER_CPU(struct clock_event_device, decrementers);
118 #define XSEC_PER_SEC (1024*1024)
121 #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
123 /* compute ((xsec << 12) * max) >> 32 */
124 #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
127 unsigned long tb_ticks_per_jiffy;
128 unsigned long tb_ticks_per_usec = 100; /* sane default */
129 EXPORT_SYMBOL(tb_ticks_per_usec);
130 unsigned long tb_ticks_per_sec;
131 EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
133 DEFINE_SPINLOCK(rtc_lock);
134 EXPORT_SYMBOL_GPL(rtc_lock);
136 static u64 tb_to_ns_scale __read_mostly;
137 static unsigned tb_to_ns_shift __read_mostly;
138 static u64 boot_tb __read_mostly;
140 extern struct timezone sys_tz;
141 static long timezone_offset;
143 unsigned long ppc_proc_freq;
144 EXPORT_SYMBOL_GPL(ppc_proc_freq);
145 unsigned long ppc_tb_freq;
146 EXPORT_SYMBOL_GPL(ppc_tb_freq);
148 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
150 * Factors for converting from cputime_t (timebase ticks) to
151 * jiffies, microseconds, seconds, and clock_t (1/USER_HZ seconds).
152 * These are all stored as 0.64 fixed-point binary fractions.
154 u64 __cputime_jiffies_factor;
155 EXPORT_SYMBOL(__cputime_jiffies_factor);
156 u64 __cputime_usec_factor;
157 EXPORT_SYMBOL(__cputime_usec_factor);
158 u64 __cputime_sec_factor;
159 EXPORT_SYMBOL(__cputime_sec_factor);
160 u64 __cputime_clockt_factor;
161 EXPORT_SYMBOL(__cputime_clockt_factor);
162 DEFINE_PER_CPU(unsigned long, cputime_last_delta);
163 DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta);
165 cputime_t cputime_one_jiffy;
167 void (*dtl_consumer)(struct dtl_entry *, u64);
169 static void calc_cputime_factors(void)
171 struct div_result res;
173 div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
174 __cputime_jiffies_factor = res.result_low;
175 div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
176 __cputime_usec_factor = res.result_low;
177 div128_by_32(1, 0, tb_ticks_per_sec, &res);
178 __cputime_sec_factor = res.result_low;
179 div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
180 __cputime_clockt_factor = res.result_low;
184 * Read the SPURR on systems that have it, otherwise the PURR,
185 * or if that doesn't exist return the timebase value passed in.
187 static u64 read_spurr(u64 tb)
189 if (cpu_has_feature(CPU_FTR_SPURR))
190 return mfspr(SPRN_SPURR);
191 if (cpu_has_feature(CPU_FTR_PURR))
192 return mfspr(SPRN_PURR);
196 #ifdef CONFIG_PPC_SPLPAR
199 * Scan the dispatch trace log and count up the stolen time.
200 * Should be called with interrupts disabled.
202 static u64 scan_dispatch_log(u64 stop_tb)
204 u64 i = local_paca->dtl_ridx;
205 struct dtl_entry *dtl = local_paca->dtl_curr;
206 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
207 struct lppaca *vpa = local_paca->lppaca_ptr;
215 if (i == be64_to_cpu(vpa->dtl_idx))
217 while (i < be64_to_cpu(vpa->dtl_idx)) {
218 dtb = be64_to_cpu(dtl->timebase);
219 tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
220 be32_to_cpu(dtl->ready_to_enqueue_time);
222 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
223 /* buffer has overflowed */
224 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
225 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
231 dtl_consumer(dtl, i);
236 dtl = local_paca->dispatch_log;
238 local_paca->dtl_ridx = i;
239 local_paca->dtl_curr = dtl;
244 * Accumulate stolen time by scanning the dispatch trace log.
245 * Called on entry from user mode.
247 void accumulate_stolen_time(void)
251 u8 save_soft_enabled = local_paca->soft_enabled;
253 /* We are called early in the exception entry, before
254 * soft/hard_enabled are sync'ed to the expected state
255 * for the exception. We are hard disabled but the PACA
256 * needs to reflect that so various debug stuff doesn't
259 local_paca->soft_enabled = 0;
261 sst = scan_dispatch_log(local_paca->starttime_user);
262 ust = scan_dispatch_log(local_paca->starttime);
263 local_paca->system_time -= sst;
264 local_paca->user_time -= ust;
265 local_paca->stolen_time += ust + sst;
267 local_paca->soft_enabled = save_soft_enabled;
270 static inline u64 calculate_stolen_time(u64 stop_tb)
274 if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) {
275 stolen = scan_dispatch_log(stop_tb);
276 get_paca()->system_time -= stolen;
279 stolen += get_paca()->stolen_time;
280 get_paca()->stolen_time = 0;
284 #else /* CONFIG_PPC_SPLPAR */
285 static inline u64 calculate_stolen_time(u64 stop_tb)
290 #endif /* CONFIG_PPC_SPLPAR */
293 * Account time for a transition between system, hard irq
296 static u64 vtime_delta(struct task_struct *tsk,
297 u64 *sys_scaled, u64 *stolen)
299 u64 now, nowscaled, deltascaled;
300 u64 udelta, delta, user_scaled;
302 WARN_ON_ONCE(!irqs_disabled());
305 nowscaled = read_spurr(now);
306 get_paca()->system_time += now - get_paca()->starttime;
307 get_paca()->starttime = now;
308 deltascaled = nowscaled - get_paca()->startspurr;
309 get_paca()->startspurr = nowscaled;
311 *stolen = calculate_stolen_time(now);
313 delta = get_paca()->system_time;
314 get_paca()->system_time = 0;
315 udelta = get_paca()->user_time - get_paca()->utime_sspurr;
316 get_paca()->utime_sspurr = get_paca()->user_time;
319 * Because we don't read the SPURR on every kernel entry/exit,
320 * deltascaled includes both user and system SPURR ticks.
321 * Apportion these ticks to system SPURR ticks and user
322 * SPURR ticks in the same ratio as the system time (delta)
323 * and user time (udelta) values obtained from the timebase
324 * over the same interval. The system ticks get accounted here;
325 * the user ticks get saved up in paca->user_time_scaled to be
326 * used by account_process_tick.
329 user_scaled = udelta;
330 if (deltascaled != delta + udelta) {
332 *sys_scaled = deltascaled * delta / (delta + udelta);
333 user_scaled = deltascaled - *sys_scaled;
335 *sys_scaled = deltascaled;
338 get_paca()->user_time_scaled += user_scaled;
343 void vtime_account_system(struct task_struct *tsk)
345 u64 delta, sys_scaled, stolen;
347 delta = vtime_delta(tsk, &sys_scaled, &stolen);
348 account_system_time(tsk, 0, delta, sys_scaled);
350 account_steal_time(stolen);
352 EXPORT_SYMBOL_GPL(vtime_account_system);
354 void vtime_account_idle(struct task_struct *tsk)
356 u64 delta, sys_scaled, stolen;
358 delta = vtime_delta(tsk, &sys_scaled, &stolen);
359 account_idle_time(delta + stolen);
363 * Transfer the user time accumulated in the paca
364 * by the exception entry and exit code to the generic
365 * process user time records.
366 * Must be called with interrupts disabled.
367 * Assumes that vtime_account_system/idle() has been called
368 * recently (i.e. since the last entry from usermode) so that
369 * get_paca()->user_time_scaled is up to date.
371 void vtime_account_user(struct task_struct *tsk)
373 cputime_t utime, utimescaled;
375 utime = get_paca()->user_time;
376 utimescaled = get_paca()->user_time_scaled;
377 get_paca()->user_time = 0;
378 get_paca()->user_time_scaled = 0;
379 get_paca()->utime_sspurr = 0;
380 account_user_time(tsk, utime, utimescaled);
383 #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
384 #define calc_cputime_factors()
387 void __delay(unsigned long loops)
395 /* the RTCL register wraps at 1000000000 */
396 diff = get_rtcl() - start;
399 } while (diff < loops);
402 while (get_tbl() - start < loops)
407 EXPORT_SYMBOL(__delay);
409 void udelay(unsigned long usecs)
411 __delay(tb_ticks_per_usec * usecs);
413 EXPORT_SYMBOL(udelay);
416 unsigned long profile_pc(struct pt_regs *regs)
418 unsigned long pc = instruction_pointer(regs);
420 if (in_lock_functions(pc))
425 EXPORT_SYMBOL(profile_pc);
428 #ifdef CONFIG_IRQ_WORK
431 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
434 static inline unsigned long test_irq_work_pending(void)
438 asm volatile("lbz %0,%1(13)"
440 : "i" (offsetof(struct paca_struct, irq_work_pending)));
444 static inline void set_irq_work_pending_flag(void)
446 asm volatile("stb %0,%1(13)" : :
448 "i" (offsetof(struct paca_struct, irq_work_pending)));
451 static inline void clear_irq_work_pending(void)
453 asm volatile("stb %0,%1(13)" : :
455 "i" (offsetof(struct paca_struct, irq_work_pending)));
460 DEFINE_PER_CPU(u8, irq_work_pending);
462 #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
463 #define test_irq_work_pending() __this_cpu_read(irq_work_pending)
464 #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
466 #endif /* 32 vs 64 bit */
468 void arch_irq_work_raise(void)
471 set_irq_work_pending_flag();
476 #else /* CONFIG_IRQ_WORK */
478 #define test_irq_work_pending() 0
479 #define clear_irq_work_pending()
481 #endif /* CONFIG_IRQ_WORK */
483 static void __timer_interrupt(void)
485 struct pt_regs *regs = get_irq_regs();
486 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
487 struct clock_event_device *evt = this_cpu_ptr(&decrementers);
490 trace_timer_interrupt_entry(regs);
492 if (test_irq_work_pending()) {
493 clear_irq_work_pending();
497 now = get_tb_or_rtc();
498 if (now >= *next_tb) {
500 if (evt->event_handler)
501 evt->event_handler(evt);
502 __this_cpu_inc(irq_stat.timer_irqs_event);
504 now = *next_tb - now;
505 if (now <= DECREMENTER_MAX)
507 /* We may have raced with new irq work */
508 if (test_irq_work_pending())
510 __this_cpu_inc(irq_stat.timer_irqs_others);
514 /* collect purr register values often, for accurate calculations */
515 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
516 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
517 cu->current_tb = mfspr(SPRN_PURR);
521 trace_timer_interrupt_exit(regs);
525 * timer_interrupt - gets called when the decrementer overflows,
526 * with interrupts disabled.
528 void timer_interrupt(struct pt_regs * regs)
530 struct pt_regs *old_regs;
531 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
533 /* Ensure a positive value is written to the decrementer, or else
534 * some CPUs will continue to take decrementer exceptions.
536 set_dec(DECREMENTER_MAX);
538 /* Some implementations of hotplug will get timer interrupts while
539 * offline, just ignore these and we also need to set
540 * decrementers_next_tb as MAX to make sure __check_irq_replay
541 * don't replay timer interrupt when return, otherwise we'll trap
544 if (!cpu_online(smp_processor_id())) {
549 /* Conditionally hard-enable interrupts now that the DEC has been
550 * bumped to its maximum value
552 may_hard_irq_enable();
555 #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
556 if (atomic_read(&ppc_n_lost_interrupts) != 0)
560 old_regs = set_irq_regs(regs);
565 set_irq_regs(old_regs);
569 * Hypervisor decrementer interrupts shouldn't occur but are sometimes
570 * left pending on exit from a KVM guest. We don't need to do anything
571 * to clear them, as they are edge-triggered.
573 void hdec_interrupt(struct pt_regs *regs)
577 #ifdef CONFIG_SUSPEND
578 static void generic_suspend_disable_irqs(void)
580 /* Disable the decrementer, so that it doesn't interfere
584 set_dec(DECREMENTER_MAX);
586 set_dec(DECREMENTER_MAX);
589 static void generic_suspend_enable_irqs(void)
594 /* Overrides the weak version in kernel/power/main.c */
595 void arch_suspend_disable_irqs(void)
597 if (ppc_md.suspend_disable_irqs)
598 ppc_md.suspend_disable_irqs();
599 generic_suspend_disable_irqs();
602 /* Overrides the weak version in kernel/power/main.c */
603 void arch_suspend_enable_irqs(void)
605 generic_suspend_enable_irqs();
606 if (ppc_md.suspend_enable_irqs)
607 ppc_md.suspend_enable_irqs();
612 * Scheduler clock - returns current time in nanosec units.
614 * Note: mulhdu(a, b) (multiply high double unsigned) returns
615 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
616 * are 64-bit unsigned numbers.
618 unsigned long long sched_clock(void)
622 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
626 #ifdef CONFIG_PPC_PSERIES
629 * Running clock - attempts to give a view of time passing for a virtualised
631 * Uses the VTB register if available otherwise a next best guess.
633 unsigned long long running_clock(void)
636 * Don't read the VTB as a host since KVM does not switch in host
637 * timebase into the VTB when it takes a guest off the CPU, reading the
638 * VTB would result in reading 'last switched out' guest VTB.
640 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
641 * would be unsafe to rely only on the #ifdef above.
643 if (firmware_has_feature(FW_FEATURE_LPAR) &&
644 cpu_has_feature(CPU_FTR_ARCH_207S))
645 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
648 * This is a next best approximation without a VTB.
649 * On a host which is running bare metal there should never be any stolen
650 * time and on a host which doesn't do any virtualisation TB *should* equal
651 * VTB so it makes no difference anyway.
653 return local_clock() - cputime_to_nsecs(kcpustat_this_cpu->cpustat[CPUTIME_STEAL]);
657 static int __init get_freq(char *name, int cells, unsigned long *val)
659 struct device_node *cpu;
663 /* The cpu node should have timebase and clock frequency properties */
664 cpu = of_find_node_by_type(NULL, "cpu");
667 fp = of_get_property(cpu, name, NULL);
670 *val = of_read_ulong(fp, cells);
679 static void start_cpu_decrementer(void)
681 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
682 /* Clear any pending timer interrupts */
683 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
685 /* Enable decrementer interrupt */
686 mtspr(SPRN_TCR, TCR_DIE);
687 #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
690 void __init generic_calibrate_decr(void)
692 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
694 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
695 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
697 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
701 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
703 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
704 !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
706 printk(KERN_ERR "WARNING: Estimating processor frequency "
711 int update_persistent_clock(struct timespec now)
715 if (!ppc_md.set_rtc_time)
718 to_tm(now.tv_sec + 1 + timezone_offset, &tm);
722 return ppc_md.set_rtc_time(&tm);
725 static void __read_persistent_clock(struct timespec *ts)
728 static int first = 1;
731 /* XXX this is a litle fragile but will work okay in the short term */
734 if (ppc_md.time_init)
735 timezone_offset = ppc_md.time_init();
737 /* get_boot_time() isn't guaranteed to be safe to call late */
738 if (ppc_md.get_boot_time) {
739 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
743 if (!ppc_md.get_rtc_time) {
747 ppc_md.get_rtc_time(&tm);
749 ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
750 tm.tm_hour, tm.tm_min, tm.tm_sec);
753 void read_persistent_clock(struct timespec *ts)
755 __read_persistent_clock(ts);
757 /* Sanitize it in case real time clock is set below EPOCH */
758 if (ts->tv_sec < 0) {
765 /* clocksource code */
766 static cycle_t rtc_read(struct clocksource *cs)
768 return (cycle_t)get_rtc();
771 static cycle_t timebase_read(struct clocksource *cs)
773 return (cycle_t)get_tb();
776 void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm,
777 struct clocksource *clock, u32 mult, cycle_t cycle_last)
779 u64 new_tb_to_xs, new_stamp_xsec;
782 if (clock != &clocksource_timebase)
785 /* Make userspace gettimeofday spin until we're done. */
786 ++vdso_data->tb_update_count;
789 /* 19342813113834067 ~= 2^(20+64) / 1e9 */
790 new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift);
791 new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC;
792 do_div(new_stamp_xsec, 1000000000);
793 new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC;
795 BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC);
796 /* this is tv_nsec / 1e9 as a 0.32 fraction */
797 frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32;
800 * tb_update_count is used to allow the userspace gettimeofday code
801 * to assure itself that it sees a consistent view of the tb_to_xs and
802 * stamp_xsec variables. It reads the tb_update_count, then reads
803 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
804 * the two values of tb_update_count match and are even then the
805 * tb_to_xs and stamp_xsec values are consistent. If not, then it
806 * loops back and reads them again until this criteria is met.
807 * We expect the caller to have done the first increment of
808 * vdso_data->tb_update_count already.
810 vdso_data->tb_orig_stamp = cycle_last;
811 vdso_data->stamp_xsec = new_stamp_xsec;
812 vdso_data->tb_to_xs = new_tb_to_xs;
813 vdso_data->wtom_clock_sec = wtm->tv_sec;
814 vdso_data->wtom_clock_nsec = wtm->tv_nsec;
815 vdso_data->stamp_xtime = *wall_time;
816 vdso_data->stamp_sec_fraction = frac_sec;
818 ++(vdso_data->tb_update_count);
821 void update_vsyscall_tz(void)
823 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
824 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
827 static void __init clocksource_init(void)
829 struct clocksource *clock;
832 clock = &clocksource_rtc;
834 clock = &clocksource_timebase;
836 if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
837 printk(KERN_ERR "clocksource: %s is already registered\n",
842 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
843 clock->name, clock->mult, clock->shift);
846 static int decrementer_set_next_event(unsigned long evt,
847 struct clock_event_device *dev)
849 __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt);
852 /* We may have raced with new irq work */
853 if (test_irq_work_pending())
859 static void decrementer_set_mode(enum clock_event_mode mode,
860 struct clock_event_device *dev)
862 if (mode != CLOCK_EVT_MODE_ONESHOT)
863 decrementer_set_next_event(DECREMENTER_MAX, dev);
866 /* Interrupt handler for the timer broadcast IPI */
867 void tick_broadcast_ipi_handler(void)
869 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
871 *next_tb = get_tb_or_rtc();
875 static void register_decrementer_clockevent(int cpu)
877 struct clock_event_device *dec = &per_cpu(decrementers, cpu);
879 *dec = decrementer_clockevent;
880 dec->cpumask = cpumask_of(cpu);
882 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
883 dec->name, dec->mult, dec->shift, cpu);
885 clockevents_register_device(dec);
888 static void __init init_decrementer_clockevent(void)
890 int cpu = smp_processor_id();
892 clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4);
894 decrementer_clockevent.max_delta_ns =
895 clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent);
896 decrementer_clockevent.min_delta_ns =
897 clockevent_delta2ns(2, &decrementer_clockevent);
899 register_decrementer_clockevent(cpu);
902 void secondary_cpu_time_init(void)
904 /* Start the decrementer on CPUs that have manual control
907 start_cpu_decrementer();
909 /* FIME: Should make unrelatred change to move snapshot_timebase
911 register_decrementer_clockevent(smp_processor_id());
914 /* This function is only called on the boot processor */
915 void __init time_init(void)
917 struct div_result res;
922 /* 601 processor: dec counts down by 128 every 128ns */
923 ppc_tb_freq = 1000000000;
925 /* Normal PowerPC with timebase register */
926 ppc_md.calibrate_decr();
927 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
928 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
929 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
930 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
933 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
934 tb_ticks_per_sec = ppc_tb_freq;
935 tb_ticks_per_usec = ppc_tb_freq / 1000000;
936 calc_cputime_factors();
937 setup_cputime_one_jiffy();
940 * Compute scale factor for sched_clock.
941 * The calibrate_decr() function has set tb_ticks_per_sec,
942 * which is the timebase frequency.
943 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
944 * the 128-bit result as a 64.64 fixed-point number.
945 * We then shift that number right until it is less than 1.0,
946 * giving us the scale factor and shift count to use in
949 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
950 scale = res.result_low;
951 for (shift = 0; res.result_high != 0; ++shift) {
952 scale = (scale >> 1) | (res.result_high << 63);
953 res.result_high >>= 1;
955 tb_to_ns_scale = scale;
956 tb_to_ns_shift = shift;
957 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
958 boot_tb = get_tb_or_rtc();
960 /* If platform provided a timezone (pmac), we correct the time */
961 if (timezone_offset) {
962 sys_tz.tz_minuteswest = -timezone_offset / 60;
963 sys_tz.tz_dsttime = 0;
966 vdso_data->tb_update_count = 0;
967 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
969 /* Start the decrementer on CPUs that have manual control
972 start_cpu_decrementer();
974 /* Register the clocksource */
977 init_decrementer_clockevent();
978 tick_setup_hrtimer_broadcast();
980 #ifdef CONFIG_COMMON_CLK
987 #define STARTOFTIME 1970
988 #define SECDAY 86400L
989 #define SECYR (SECDAY * 365)
990 #define leapyear(year) ((year) % 4 == 0 && \
991 ((year) % 100 != 0 || (year) % 400 == 0))
992 #define days_in_year(a) (leapyear(a) ? 366 : 365)
993 #define days_in_month(a) (month_days[(a) - 1])
995 static int month_days[12] = {
996 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1000 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1002 void GregorianDay(struct rtc_time * tm)
1007 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1009 lastYear = tm->tm_year - 1;
1012 * Number of leap corrections to apply up to end of last year
1014 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1017 * This year is a leap year if it is divisible by 4 except when it is
1018 * divisible by 100 unless it is divisible by 400
1020 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1022 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1024 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1027 tm->tm_wday = day % 7;
1029 EXPORT_SYMBOL_GPL(GregorianDay);
1031 void to_tm(int tim, struct rtc_time * tm)
1034 register long hms, day;
1039 /* Hours, minutes, seconds are easy */
1040 tm->tm_hour = hms / 3600;
1041 tm->tm_min = (hms % 3600) / 60;
1042 tm->tm_sec = (hms % 3600) % 60;
1044 /* Number of years in days */
1045 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1046 day -= days_in_year(i);
1049 /* Number of months in days left */
1050 if (leapyear(tm->tm_year))
1051 days_in_month(FEBRUARY) = 29;
1052 for (i = 1; day >= days_in_month(i); i++)
1053 day -= days_in_month(i);
1054 days_in_month(FEBRUARY) = 28;
1057 /* Days are what is left over (+1) from all that. */
1058 tm->tm_mday = day + 1;
1061 * Determine the day of week
1065 EXPORT_SYMBOL(to_tm);
1068 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1071 void div128_by_32(u64 dividend_high, u64 dividend_low,
1072 unsigned divisor, struct div_result *dr)
1074 unsigned long a, b, c, d;
1075 unsigned long w, x, y, z;
1078 a = dividend_high >> 32;
1079 b = dividend_high & 0xffffffff;
1080 c = dividend_low >> 32;
1081 d = dividend_low & 0xffffffff;
1084 ra = ((u64)(a - (w * divisor)) << 32) + b;
1086 rb = ((u64) do_div(ra, divisor) << 32) + c;
1089 rc = ((u64) do_div(rb, divisor) << 32) + d;
1092 do_div(rc, divisor);
1095 dr->result_high = ((u64)w << 32) + x;
1096 dr->result_low = ((u64)y << 32) + z;
1100 /* We don't need to calibrate delay, we use the CPU timebase for that */
1101 void calibrate_delay(void)
1103 /* Some generic code (such as spinlock debug) use loops_per_jiffy
1104 * as the number of __delay(1) in a jiffy, so make it so
1106 loops_per_jiffy = tb_ticks_per_jiffy;
1109 static int __init rtc_init(void)
1111 struct platform_device *pdev;
1113 if (!ppc_md.get_rtc_time)
1116 pdev = platform_device_register_simple("rtc-generic", -1, NULL, 0);
1118 return PTR_ERR_OR_ZERO(pdev);
1121 module_init(rtc_init);