1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Common time routines among all ppc machines.
5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6 * Paul Mackerras' version and mine for PReP and Pmac.
7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
8 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
10 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
11 * to make clock more stable (2.4.0-test5). The only thing
12 * that this code assumes is that the timebases have been synchronized
13 * by firmware on SMP and are never stopped (never do sleep
14 * on SMP then, nap and doze are OK).
16 * Speeded up do_gettimeofday by getting rid of references to
17 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
19 * TODO (not necessarily in this file):
20 * - improve precision and reproducibility of timebase frequency
21 * measurement at boot time.
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.
26 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
27 * "A Kernel Model for Precision Timekeeping" by Dave Mills
30 #include <linux/errno.h>
31 #include <linux/export.h>
32 #include <linux/sched.h>
33 #include <linux/sched/clock.h>
34 #include <linux/sched/cputime.h>
35 #include <linux/kernel.h>
36 #include <linux/param.h>
37 #include <linux/string.h>
39 #include <linux/interrupt.h>
40 #include <linux/timex.h>
41 #include <linux/kernel_stat.h>
42 #include <linux/time.h>
43 #include <linux/init.h>
44 #include <linux/profile.h>
45 #include <linux/cpu.h>
46 #include <linux/security.h>
47 #include <linux/percpu.h>
48 #include <linux/rtc.h>
49 #include <linux/jiffies.h>
50 #include <linux/posix-timers.h>
51 #include <linux/irq.h>
52 #include <linux/delay.h>
53 #include <linux/irq_work.h>
54 #include <linux/of_clk.h>
55 #include <linux/suspend.h>
56 #include <linux/processor.h>
57 #include <asm/trace.h>
59 #include <asm/interrupt.h>
61 #include <asm/nvram.h>
62 #include <asm/cache.h>
63 #include <asm/machdep.h>
64 #include <linux/uaccess.h>
68 #include <asm/div64.h>
70 #include <asm/vdso_datapage.h>
71 #include <asm/firmware.h>
72 #include <asm/asm-prototypes.h>
74 /* powerpc clocksource/clockevent code */
76 #include <linux/clockchips.h>
77 #include <linux/timekeeper_internal.h>
79 static u64 timebase_read(struct clocksource *);
80 static struct clocksource clocksource_timebase = {
83 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
84 .mask = CLOCKSOURCE_MASK(64),
85 .read = timebase_read,
86 .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
89 #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
90 u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
92 static int decrementer_set_next_event(unsigned long evt,
93 struct clock_event_device *dev);
94 static int decrementer_shutdown(struct clock_event_device *evt);
96 struct clock_event_device decrementer_clockevent = {
97 .name = "decrementer",
100 .set_next_event = decrementer_set_next_event,
101 .set_state_oneshot_stopped = decrementer_shutdown,
102 .set_state_shutdown = decrementer_shutdown,
103 .tick_resume = decrementer_shutdown,
104 .features = CLOCK_EVT_FEAT_ONESHOT |
105 CLOCK_EVT_FEAT_C3STOP,
107 EXPORT_SYMBOL(decrementer_clockevent);
109 DEFINE_PER_CPU(u64, decrementers_next_tb);
110 static DEFINE_PER_CPU(struct clock_event_device, decrementers);
112 #define XSEC_PER_SEC (1024*1024)
115 #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
117 /* compute ((xsec << 12) * max) >> 32 */
118 #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
121 unsigned long tb_ticks_per_jiffy;
122 unsigned long tb_ticks_per_usec = 100; /* sane default */
123 EXPORT_SYMBOL(tb_ticks_per_usec);
124 unsigned long tb_ticks_per_sec;
125 EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
127 DEFINE_SPINLOCK(rtc_lock);
128 EXPORT_SYMBOL_GPL(rtc_lock);
130 static u64 tb_to_ns_scale __read_mostly;
131 static unsigned tb_to_ns_shift __read_mostly;
132 static u64 boot_tb __read_mostly;
134 extern struct timezone sys_tz;
135 static long timezone_offset;
137 unsigned long ppc_proc_freq;
138 EXPORT_SYMBOL_GPL(ppc_proc_freq);
139 unsigned long ppc_tb_freq;
140 EXPORT_SYMBOL_GPL(ppc_tb_freq);
144 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
146 * Factor for converting from cputime_t (timebase ticks) to
147 * microseconds. This is stored as 0.64 fixed-point binary fraction.
149 u64 __cputime_usec_factor;
150 EXPORT_SYMBOL(__cputime_usec_factor);
152 #ifdef CONFIG_PPC_SPLPAR
153 void (*dtl_consumer)(struct dtl_entry *, u64);
156 static void calc_cputime_factors(void)
158 struct div_result res;
160 div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
161 __cputime_usec_factor = res.result_low;
165 * Read the SPURR on systems that have it, otherwise the PURR,
166 * or if that doesn't exist return the timebase value passed in.
168 static inline unsigned long read_spurr(unsigned long tb)
170 if (cpu_has_feature(CPU_FTR_SPURR))
171 return mfspr(SPRN_SPURR);
172 if (cpu_has_feature(CPU_FTR_PURR))
173 return mfspr(SPRN_PURR);
177 #ifdef CONFIG_PPC_SPLPAR
182 * Scan the dispatch trace log and count up the stolen time.
183 * Should be called with interrupts disabled.
185 static u64 scan_dispatch_log(u64 stop_tb)
187 u64 i = local_paca->dtl_ridx;
188 struct dtl_entry *dtl = local_paca->dtl_curr;
189 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
190 struct lppaca *vpa = local_paca->lppaca_ptr;
198 if (i == be64_to_cpu(vpa->dtl_idx))
200 while (i < be64_to_cpu(vpa->dtl_idx)) {
201 dtb = be64_to_cpu(dtl->timebase);
202 tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
203 be32_to_cpu(dtl->ready_to_enqueue_time);
205 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
206 /* buffer has overflowed */
207 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
208 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
214 dtl_consumer(dtl, i);
219 dtl = local_paca->dispatch_log;
221 local_paca->dtl_ridx = i;
222 local_paca->dtl_curr = dtl;
227 * Accumulate stolen time by scanning the dispatch trace log.
228 * Called on entry from user mode.
230 void notrace accumulate_stolen_time(void)
233 struct cpu_accounting_data *acct = &local_paca->accounting;
235 sst = scan_dispatch_log(acct->starttime_user);
236 ust = scan_dispatch_log(acct->starttime);
239 acct->steal_time += ust + sst;
242 static inline u64 calculate_stolen_time(u64 stop_tb)
244 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
247 if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
248 return scan_dispatch_log(stop_tb);
253 #else /* CONFIG_PPC_SPLPAR */
254 static inline u64 calculate_stolen_time(u64 stop_tb)
259 #endif /* CONFIG_PPC_SPLPAR */
262 * Account time for a transition between system, hard irq
265 static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
266 unsigned long now, unsigned long stime)
268 unsigned long stime_scaled = 0;
269 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
270 unsigned long nowscaled, deltascaled;
271 unsigned long utime, utime_scaled;
273 nowscaled = read_spurr(now);
274 deltascaled = nowscaled - acct->startspurr;
275 acct->startspurr = nowscaled;
276 utime = acct->utime - acct->utime_sspurr;
277 acct->utime_sspurr = acct->utime;
280 * Because we don't read the SPURR on every kernel entry/exit,
281 * deltascaled includes both user and system SPURR ticks.
282 * Apportion these ticks to system SPURR ticks and user
283 * SPURR ticks in the same ratio as the system time (delta)
284 * and user time (udelta) values obtained from the timebase
285 * over the same interval. The system ticks get accounted here;
286 * the user ticks get saved up in paca->user_time_scaled to be
287 * used by account_process_tick.
289 stime_scaled = stime;
290 utime_scaled = utime;
291 if (deltascaled != stime + utime) {
293 stime_scaled = deltascaled * stime / (stime + utime);
294 utime_scaled = deltascaled - stime_scaled;
296 stime_scaled = deltascaled;
299 acct->utime_scaled += utime_scaled;
305 static unsigned long vtime_delta(struct cpu_accounting_data *acct,
306 unsigned long *stime_scaled,
307 unsigned long *steal_time)
309 unsigned long now, stime;
311 WARN_ON_ONCE(!irqs_disabled());
314 stime = now - acct->starttime;
315 acct->starttime = now;
317 *stime_scaled = vtime_delta_scaled(acct, now, stime);
319 *steal_time = calculate_stolen_time(now);
324 static void vtime_delta_kernel(struct cpu_accounting_data *acct,
325 unsigned long *stime, unsigned long *stime_scaled)
327 unsigned long steal_time;
329 *stime = vtime_delta(acct, stime_scaled, &steal_time);
330 *stime -= min(*stime, steal_time);
331 acct->steal_time += steal_time;
334 void vtime_account_kernel(struct task_struct *tsk)
336 struct cpu_accounting_data *acct = get_accounting(tsk);
337 unsigned long stime, stime_scaled;
339 vtime_delta_kernel(acct, &stime, &stime_scaled);
341 if (tsk->flags & PF_VCPU) {
342 acct->gtime += stime;
343 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
344 acct->utime_scaled += stime_scaled;
347 acct->stime += stime;
348 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
349 acct->stime_scaled += stime_scaled;
353 EXPORT_SYMBOL_GPL(vtime_account_kernel);
355 void vtime_account_idle(struct task_struct *tsk)
357 unsigned long stime, stime_scaled, steal_time;
358 struct cpu_accounting_data *acct = get_accounting(tsk);
360 stime = vtime_delta(acct, &stime_scaled, &steal_time);
361 acct->idle_time += stime + steal_time;
364 static void vtime_account_irq_field(struct cpu_accounting_data *acct,
365 unsigned long *field)
367 unsigned long stime, stime_scaled;
369 vtime_delta_kernel(acct, &stime, &stime_scaled);
371 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
372 acct->stime_scaled += stime_scaled;
376 void vtime_account_softirq(struct task_struct *tsk)
378 struct cpu_accounting_data *acct = get_accounting(tsk);
379 vtime_account_irq_field(acct, &acct->softirq_time);
382 void vtime_account_hardirq(struct task_struct *tsk)
384 struct cpu_accounting_data *acct = get_accounting(tsk);
385 vtime_account_irq_field(acct, &acct->hardirq_time);
388 static void vtime_flush_scaled(struct task_struct *tsk,
389 struct cpu_accounting_data *acct)
391 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
392 if (acct->utime_scaled)
393 tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
394 if (acct->stime_scaled)
395 tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
397 acct->utime_scaled = 0;
398 acct->utime_sspurr = 0;
399 acct->stime_scaled = 0;
404 * Account the whole cputime accumulated in the paca
405 * Must be called with interrupts disabled.
406 * Assumes that vtime_account_kernel/idle() has been called
407 * recently (i.e. since the last entry from usermode) so that
408 * get_paca()->user_time_scaled is up to date.
410 void vtime_flush(struct task_struct *tsk)
412 struct cpu_accounting_data *acct = get_accounting(tsk);
415 account_user_time(tsk, cputime_to_nsecs(acct->utime));
418 account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
420 if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
421 account_steal_time(cputime_to_nsecs(acct->steal_time));
422 acct->steal_time = 0;
426 account_idle_time(cputime_to_nsecs(acct->idle_time));
429 account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
432 if (acct->hardirq_time)
433 account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
435 if (acct->softirq_time)
436 account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
439 vtime_flush_scaled(tsk, acct);
445 acct->hardirq_time = 0;
446 acct->softirq_time = 0;
449 #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
450 #define calc_cputime_factors()
453 void __delay(unsigned long loops)
460 * TB is in error state and isn't ticking anymore.
461 * HMI handler was unable to recover from TB error.
462 * Return immediately, so that kernel won't get stuck here.
467 while (mftb() - start < loops)
472 EXPORT_SYMBOL(__delay);
474 void udelay(unsigned long usecs)
476 __delay(tb_ticks_per_usec * usecs);
478 EXPORT_SYMBOL(udelay);
481 unsigned long profile_pc(struct pt_regs *regs)
483 unsigned long pc = instruction_pointer(regs);
485 if (in_lock_functions(pc))
490 EXPORT_SYMBOL(profile_pc);
493 #ifdef CONFIG_IRQ_WORK
496 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
499 static inline void set_irq_work_pending_flag(void)
501 asm volatile("stb %0,%1(13)" : :
503 "i" (offsetof(struct paca_struct, irq_work_pending)));
506 static inline void clear_irq_work_pending(void)
508 asm volatile("stb %0,%1(13)" : :
510 "i" (offsetof(struct paca_struct, irq_work_pending)));
515 DEFINE_PER_CPU(u8, irq_work_pending);
517 #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
518 #define test_irq_work_pending() __this_cpu_read(irq_work_pending)
519 #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
521 #endif /* 32 vs 64 bit */
523 void arch_irq_work_raise(void)
526 * 64-bit code that uses irq soft-mask can just cause an immediate
527 * interrupt here that gets soft masked, if this is called under
528 * local_irq_disable(). It might be possible to prevent that happening
529 * by noticing interrupts are disabled and setting decrementer pending
530 * to be replayed when irqs are enabled. The problem there is that
531 * tracing can call irq_work_raise, including in code that does low
532 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
533 * which could get tangled up if we're messing with the same state
537 set_irq_work_pending_flag();
542 #else /* CONFIG_IRQ_WORK */
544 #define test_irq_work_pending() 0
545 #define clear_irq_work_pending()
547 #endif /* CONFIG_IRQ_WORK */
550 * timer_interrupt - gets called when the decrementer overflows,
551 * with interrupts disabled.
553 DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)
555 struct clock_event_device *evt = this_cpu_ptr(&decrementers);
556 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
557 struct pt_regs *old_regs;
561 * Some implementations of hotplug will get timer interrupts while
562 * offline, just ignore these.
564 if (unlikely(!cpu_online(smp_processor_id()))) {
565 set_dec(decrementer_max);
569 /* Ensure a positive value is written to the decrementer, or else
570 * some CPUs will continue to take decrementer exceptions. When the
571 * PPC_WATCHDOG (decrementer based) is configured, keep this at most
572 * 31 bits, which is about 4 seconds on most systems, which gives
573 * the watchdog a chance of catching timer interrupt hard lockups.
575 if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
578 set_dec(decrementer_max);
580 /* Conditionally hard-enable interrupts now that the DEC has been
581 * bumped to its maximum value
583 may_hard_irq_enable();
586 #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
587 if (atomic_read(&ppc_n_lost_interrupts) != 0)
591 old_regs = set_irq_regs(regs);
593 trace_timer_interrupt_entry(regs);
595 if (test_irq_work_pending()) {
596 clear_irq_work_pending();
601 if (now >= *next_tb) {
603 if (evt->event_handler)
604 evt->event_handler(evt);
605 __this_cpu_inc(irq_stat.timer_irqs_event);
607 now = *next_tb - now;
608 if (now <= decrementer_max)
610 /* We may have raced with new irq work */
611 if (test_irq_work_pending())
613 __this_cpu_inc(irq_stat.timer_irqs_others);
616 trace_timer_interrupt_exit(regs);
618 set_irq_regs(old_regs);
620 EXPORT_SYMBOL(timer_interrupt);
622 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
623 void timer_broadcast_interrupt(void)
625 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
628 tick_receive_broadcast();
629 __this_cpu_inc(irq_stat.broadcast_irqs_event);
633 #ifdef CONFIG_SUSPEND
634 static void generic_suspend_disable_irqs(void)
636 /* Disable the decrementer, so that it doesn't interfere
640 set_dec(decrementer_max);
642 set_dec(decrementer_max);
645 static void generic_suspend_enable_irqs(void)
650 /* Overrides the weak version in kernel/power/main.c */
651 void arch_suspend_disable_irqs(void)
653 if (ppc_md.suspend_disable_irqs)
654 ppc_md.suspend_disable_irqs();
655 generic_suspend_disable_irqs();
658 /* Overrides the weak version in kernel/power/main.c */
659 void arch_suspend_enable_irqs(void)
661 generic_suspend_enable_irqs();
662 if (ppc_md.suspend_enable_irqs)
663 ppc_md.suspend_enable_irqs();
667 unsigned long long tb_to_ns(unsigned long long ticks)
669 return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
671 EXPORT_SYMBOL_GPL(tb_to_ns);
674 * Scheduler clock - returns current time in nanosec units.
676 * Note: mulhdu(a, b) (multiply high double unsigned) returns
677 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
678 * are 64-bit unsigned numbers.
680 notrace unsigned long long sched_clock(void)
682 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
686 #ifdef CONFIG_PPC_PSERIES
689 * Running clock - attempts to give a view of time passing for a virtualised
691 * Uses the VTB register if available otherwise a next best guess.
693 unsigned long long running_clock(void)
696 * Don't read the VTB as a host since KVM does not switch in host
697 * timebase into the VTB when it takes a guest off the CPU, reading the
698 * VTB would result in reading 'last switched out' guest VTB.
700 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
701 * would be unsafe to rely only on the #ifdef above.
703 if (firmware_has_feature(FW_FEATURE_LPAR) &&
704 cpu_has_feature(CPU_FTR_ARCH_207S))
705 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
708 * This is a next best approximation without a VTB.
709 * On a host which is running bare metal there should never be any stolen
710 * time and on a host which doesn't do any virtualisation TB *should* equal
711 * VTB so it makes no difference anyway.
713 return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
717 static int __init get_freq(char *name, int cells, unsigned long *val)
719 struct device_node *cpu;
723 /* The cpu node should have timebase and clock frequency properties */
724 cpu = of_find_node_by_type(NULL, "cpu");
727 fp = of_get_property(cpu, name, NULL);
730 *val = of_read_ulong(fp, cells);
739 static void start_cpu_decrementer(void)
741 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
744 /* Clear any pending timer interrupts */
745 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
747 tcr = mfspr(SPRN_TCR);
749 * The watchdog may have already been enabled by u-boot. So leave
750 * TRC[WP] (Watchdog Period) alone.
752 tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */
753 tcr |= TCR_DIE; /* Enable decrementer */
754 mtspr(SPRN_TCR, tcr);
758 void __init generic_calibrate_decr(void)
760 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
762 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
763 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
765 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
769 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
771 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
772 !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
774 printk(KERN_ERR "WARNING: Estimating processor frequency "
779 int update_persistent_clock64(struct timespec64 now)
783 if (!ppc_md.set_rtc_time)
786 rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
788 return ppc_md.set_rtc_time(&tm);
791 static void __read_persistent_clock(struct timespec64 *ts)
794 static int first = 1;
797 /* XXX this is a litle fragile but will work okay in the short term */
800 if (ppc_md.time_init)
801 timezone_offset = ppc_md.time_init();
803 /* get_boot_time() isn't guaranteed to be safe to call late */
804 if (ppc_md.get_boot_time) {
805 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
809 if (!ppc_md.get_rtc_time) {
813 ppc_md.get_rtc_time(&tm);
815 ts->tv_sec = rtc_tm_to_time64(&tm);
818 void read_persistent_clock64(struct timespec64 *ts)
820 __read_persistent_clock(ts);
822 /* Sanitize it in case real time clock is set below EPOCH */
823 if (ts->tv_sec < 0) {
830 /* clocksource code */
831 static notrace u64 timebase_read(struct clocksource *cs)
833 return (u64)get_tb();
836 static void __init clocksource_init(void)
838 struct clocksource *clock = &clocksource_timebase;
840 if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
841 printk(KERN_ERR "clocksource: %s is already registered\n",
846 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
847 clock->name, clock->mult, clock->shift);
850 static int decrementer_set_next_event(unsigned long evt,
851 struct clock_event_device *dev)
853 __this_cpu_write(decrementers_next_tb, get_tb() + evt);
856 /* We may have raced with new irq work */
857 if (test_irq_work_pending())
863 static int decrementer_shutdown(struct clock_event_device *dev)
865 decrementer_set_next_event(decrementer_max, dev);
869 static void register_decrementer_clockevent(int cpu)
871 struct clock_event_device *dec = &per_cpu(decrementers, cpu);
873 *dec = decrementer_clockevent;
874 dec->cpumask = cpumask_of(cpu);
876 clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
878 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
879 dec->name, dec->mult, dec->shift, cpu);
881 /* Set values for KVM, see kvm_emulate_dec() */
882 decrementer_clockevent.mult = dec->mult;
883 decrementer_clockevent.shift = dec->shift;
886 static void enable_large_decrementer(void)
888 if (!cpu_has_feature(CPU_FTR_ARCH_300))
891 if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
895 * If we're running as the hypervisor we need to enable the LD manually
896 * otherwise firmware should have done it for us.
898 if (cpu_has_feature(CPU_FTR_HVMODE))
899 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
902 static void __init set_decrementer_max(void)
904 struct device_node *cpu;
907 /* Prior to ISAv3 the decrementer is always 32 bit */
908 if (!cpu_has_feature(CPU_FTR_ARCH_300))
911 cpu = of_find_node_by_type(NULL, "cpu");
913 if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
914 if (bits > 64 || bits < 32) {
915 pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
919 /* calculate the signed maximum given this many bits */
920 decrementer_max = (1ul << (bits - 1)) - 1;
925 pr_info("time_init: %u bit decrementer (max: %llx)\n",
926 bits, decrementer_max);
929 static void __init init_decrementer_clockevent(void)
931 register_decrementer_clockevent(smp_processor_id());
934 void secondary_cpu_time_init(void)
936 /* Enable and test the large decrementer for this cpu */
937 enable_large_decrementer();
939 /* Start the decrementer on CPUs that have manual control
942 start_cpu_decrementer();
944 /* FIME: Should make unrelatred change to move snapshot_timebase
946 register_decrementer_clockevent(smp_processor_id());
949 /* This function is only called on the boot processor */
950 void __init time_init(void)
952 struct div_result res;
956 /* Normal PowerPC with timebase register */
957 ppc_md.calibrate_decr();
958 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
959 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
960 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
961 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
963 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
964 tb_ticks_per_sec = ppc_tb_freq;
965 tb_ticks_per_usec = ppc_tb_freq / 1000000;
966 calc_cputime_factors();
969 * Compute scale factor for sched_clock.
970 * The calibrate_decr() function has set tb_ticks_per_sec,
971 * which is the timebase frequency.
972 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
973 * the 128-bit result as a 64.64 fixed-point number.
974 * We then shift that number right until it is less than 1.0,
975 * giving us the scale factor and shift count to use in
978 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
979 scale = res.result_low;
980 for (shift = 0; res.result_high != 0; ++shift) {
981 scale = (scale >> 1) | (res.result_high << 63);
982 res.result_high >>= 1;
984 tb_to_ns_scale = scale;
985 tb_to_ns_shift = shift;
986 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
989 /* If platform provided a timezone (pmac), we correct the time */
990 if (timezone_offset) {
991 sys_tz.tz_minuteswest = -timezone_offset / 60;
992 sys_tz.tz_dsttime = 0;
995 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
997 /* initialise and enable the large decrementer (if we have one) */
998 set_decrementer_max();
999 enable_large_decrementer();
1001 /* Start the decrementer on CPUs that have manual control
1004 start_cpu_decrementer();
1006 /* Register the clocksource */
1009 init_decrementer_clockevent();
1010 tick_setup_hrtimer_broadcast();
1013 enable_sched_clock_irqtime();
1017 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1020 void div128_by_32(u64 dividend_high, u64 dividend_low,
1021 unsigned divisor, struct div_result *dr)
1023 unsigned long a, b, c, d;
1024 unsigned long w, x, y, z;
1027 a = dividend_high >> 32;
1028 b = dividend_high & 0xffffffff;
1029 c = dividend_low >> 32;
1030 d = dividend_low & 0xffffffff;
1033 ra = ((u64)(a - (w * divisor)) << 32) + b;
1035 rb = ((u64) do_div(ra, divisor) << 32) + c;
1038 rc = ((u64) do_div(rb, divisor) << 32) + d;
1041 do_div(rc, divisor);
1044 dr->result_high = ((u64)w << 32) + x;
1045 dr->result_low = ((u64)y << 32) + z;
1049 /* We don't need to calibrate delay, we use the CPU timebase for that */
1050 void calibrate_delay(void)
1052 /* Some generic code (such as spinlock debug) use loops_per_jiffy
1053 * as the number of __delay(1) in a jiffy, so make it so
1055 loops_per_jiffy = tb_ticks_per_jiffy;
1058 #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1059 static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1061 ppc_md.get_rtc_time(tm);
1065 static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1067 if (!ppc_md.set_rtc_time)
1070 if (ppc_md.set_rtc_time(tm) < 0)
1076 static const struct rtc_class_ops rtc_generic_ops = {
1077 .read_time = rtc_generic_get_time,
1078 .set_time = rtc_generic_set_time,
1081 static int __init rtc_init(void)
1083 struct platform_device *pdev;
1085 if (!ppc_md.get_rtc_time)
1088 pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1090 sizeof(rtc_generic_ops));
1092 return PTR_ERR_OR_ZERO(pdev);
1095 device_initcall(rtc_init);