xref: /openbmc/linux/arch/powerpc/include/asm/time.h (revision 6ba5aa54)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b8b572e1SStephen Rothwell /*
3b8b572e1SStephen Rothwell  * Common time prototypes and such for all ppc machines.
4b8b572e1SStephen Rothwell  *
5b8b572e1SStephen Rothwell  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6b8b572e1SStephen Rothwell  * Paul Mackerras' version and mine for PReP and Pmac.
7b8b572e1SStephen Rothwell  */
8b8b572e1SStephen Rothwell 
9b8b572e1SStephen Rothwell #ifndef __POWERPC_TIME_H
10b8b572e1SStephen Rothwell #define __POWERPC_TIME_H
11b8b572e1SStephen Rothwell 
12b8b572e1SStephen Rothwell #ifdef __KERNEL__
13b8b572e1SStephen Rothwell #include <linux/types.h>
14b8b572e1SStephen Rothwell #include <linux/percpu.h>
15b8b572e1SStephen Rothwell 
16b8b572e1SStephen Rothwell #include <asm/processor.h>
17b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
18d26b3817SChristophe Leroy #include <asm/vdso/timebase.h>
19b8b572e1SStephen Rothwell 
20b8b572e1SStephen Rothwell /* time.c */
219581991aSNicholas Piggin extern u64 decrementer_max;
229581991aSNicholas Piggin 
23b8b572e1SStephen Rothwell extern unsigned long tb_ticks_per_jiffy;
24b8b572e1SStephen Rothwell extern unsigned long tb_ticks_per_usec;
25b8b572e1SStephen Rothwell extern unsigned long tb_ticks_per_sec;
266e35994dSBharat Bhushan extern struct clock_event_device decrementer_clockevent;
27ce0091a0SHe Ying extern u64 decrementer_max;
28b8b572e1SStephen Rothwell 
29b8b572e1SStephen Rothwell 
30b8b572e1SStephen Rothwell extern void generic_calibrate_decr(void);
31b8b572e1SStephen Rothwell 
32b8b572e1SStephen Rothwell /* Some sane defaults: 125 MHz timebase, 1GHz processor */
33b8b572e1SStephen Rothwell extern unsigned long ppc_proc_freq;
34b8b572e1SStephen Rothwell #define DEFAULT_PROC_FREQ	(DEFAULT_TB_FREQ * 8)
35b8b572e1SStephen Rothwell extern unsigned long ppc_tb_freq;
36b8b572e1SStephen Rothwell #define DEFAULT_TB_FREQ		125000000UL
37b8b572e1SStephen Rothwell 
38de269129SMahesh Salgaonkar extern bool tb_invalid;
39de269129SMahesh Salgaonkar 
40b8b572e1SStephen Rothwell struct div_result {
41b8b572e1SStephen Rothwell 	u64 result_high;
42b8b572e1SStephen Rothwell 	u64 result_low;
43b8b572e1SStephen Rothwell };
44b8b572e1SStephen Rothwell 
get_vtb(void)458f42ab27SAneesh Kumar K.V static inline u64 get_vtb(void)
468f42ab27SAneesh Kumar K.V {
478f42ab27SAneesh Kumar K.V 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
48905259e3SKevin Hao 		return mfspr(SPRN_VTB);
49c3cb5dbdSChristophe Leroy 
508f42ab27SAneesh Kumar K.V 	return 0;
518f42ab27SAneesh Kumar K.V }
528f42ab27SAneesh Kumar K.V 
53b8b572e1SStephen Rothwell /* Accessor functions for the decrementer register.
54b8b572e1SStephen Rothwell  * The 4xx doesn't even have a decrementer.  I tried to use the
55b8b572e1SStephen Rothwell  * generic timer interrupt code, which seems OK, with the 4xx PIT
56b8b572e1SStephen Rothwell  * in auto-reload mode.  The problem is PIT stops counting when it
57b8b572e1SStephen Rothwell  * hits zero.  If it would wrap, we could use it just like a decrementer.
58b8b572e1SStephen Rothwell  */
get_dec(void)5979901024SOliver O'Halloran static inline u64 get_dec(void)
60b8b572e1SStephen Rothwell {
6163f9d9dfSChristophe Leroy 	if (IS_ENABLED(CONFIG_40x))
6263f9d9dfSChristophe Leroy 		return mfspr(SPRN_PIT);
6363f9d9dfSChristophe Leroy 
6463f9d9dfSChristophe Leroy 	return mfspr(SPRN_DEC);
65b8b572e1SStephen Rothwell }
66b8b572e1SStephen Rothwell 
67b8b572e1SStephen Rothwell /*
68b8b572e1SStephen Rothwell  * Note: Book E and 4xx processors differ from other PowerPC processors
69b8b572e1SStephen Rothwell  * in when the decrementer generates its interrupt: on the 1 to 0
70b8b572e1SStephen Rothwell  * transition for Book E/4xx, but on the 0 to -1 transition for others.
71b8b572e1SStephen Rothwell  */
set_dec(u64 val)7279901024SOliver O'Halloran static inline void set_dec(u64 val)
73b8b572e1SStephen Rothwell {
7463f9d9dfSChristophe Leroy 	if (IS_ENABLED(CONFIG_40x))
7579901024SOliver O'Halloran 		mtspr(SPRN_PIT, (u32)val);
7663f9d9dfSChristophe Leroy 	else if (IS_ENABLED(CONFIG_BOOKE))
77b8b572e1SStephen Rothwell 		mtspr(SPRN_DEC, val);
7863f9d9dfSChristophe Leroy 	else
7963f9d9dfSChristophe Leroy 		mtspr(SPRN_DEC, val - 1);
80b8b572e1SStephen Rothwell }
81b8b572e1SStephen Rothwell 
tb_ticks_since(unsigned long tstamp)82b8b572e1SStephen Rothwell static inline unsigned long tb_ticks_since(unsigned long tstamp)
83b8b572e1SStephen Rothwell {
84942e8911SChristophe Leroy 	return mftb() - tstamp;
85b8b572e1SStephen Rothwell }
86b8b572e1SStephen Rothwell 
87b8b572e1SStephen Rothwell #define mulhwu(x,y) \
88b8b572e1SStephen Rothwell ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
89b8b572e1SStephen Rothwell 
90b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
91b8b572e1SStephen Rothwell #define mulhdu(x,y) \
92b8b572e1SStephen Rothwell ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
93b8b572e1SStephen Rothwell #else
94b8b572e1SStephen Rothwell extern u64 mulhdu(u64, u64);
95b8b572e1SStephen Rothwell #endif
96b8b572e1SStephen Rothwell 
97b8b572e1SStephen Rothwell extern void div128_by_32(u64 dividend_high, u64 dividend_low,
98b8b572e1SStephen Rothwell 			 unsigned divisor, struct div_result *dr);
99b8b572e1SStephen Rothwell 
100b8b572e1SStephen Rothwell extern void secondary_cpu_time_init(void);
101848092faSMathieu Malaterre extern void __init time_init(void);
102b8b572e1SStephen Rothwell 
1037df10275SAnton Blanchard DECLARE_PER_CPU(u64, decrementers_next_tb);
10437fb9a02SAnton Blanchard 
timer_get_next_tb(void)1054ebbd075SNicholas Piggin static inline u64 timer_get_next_tb(void)
1064ebbd075SNicholas Piggin {
1074ebbd075SNicholas Piggin 	return __this_cpu_read(decrementers_next_tb);
1084ebbd075SNicholas Piggin }
1094ebbd075SNicholas Piggin 
11025aa1458SNicholas Piggin #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
11125aa1458SNicholas Piggin void timer_rearm_host_dec(u64 now);
11225aa1458SNicholas Piggin #endif
11325aa1458SNicholas Piggin 
114b6c295dfSPaul Mackerras /* Convert timebase ticks to nanoseconds */
115b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long tb_ticks);
116b6c295dfSPaul Mackerras 
1170440b8a2SNicholas Piggin void timer_broadcast_interrupt(void);
1180440b8a2SNicholas Piggin 
119*6ba5aa54SNicholas Piggin /* SPLPAR and VIRT_CPU_ACCOUNTING_NATIVE */
120*6ba5aa54SNicholas Piggin void pseries_accumulate_stolen_time(void);
121*6ba5aa54SNicholas Piggin u64 pseries_calculate_stolen_time(u64 stop_tb);
12268b34588SNicholas Piggin 
123b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
124b8b572e1SStephen Rothwell #endif /* __POWERPC_TIME_H */
125