xref: /openbmc/linux/arch/powerpc/include/asm/time.h (revision 6601ec1c2ba929430f5585ce7f9d9960b0e0a01d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Common time prototypes and such for all ppc machines.
4  *
5  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6  * Paul Mackerras' version and mine for PReP and Pmac.
7  */
8 
9 #ifndef __POWERPC_TIME_H
10 #define __POWERPC_TIME_H
11 
12 #ifdef __KERNEL__
13 #include <linux/types.h>
14 #include <linux/percpu.h>
15 
16 #include <asm/processor.h>
17 #include <asm/cpu_has_feature.h>
18 
19 /* time.c */
20 extern unsigned long tb_ticks_per_jiffy;
21 extern unsigned long tb_ticks_per_usec;
22 extern unsigned long tb_ticks_per_sec;
23 extern struct clock_event_device decrementer_clockevent;
24 
25 
26 extern void generic_calibrate_decr(void);
27 
28 /* Some sane defaults: 125 MHz timebase, 1GHz processor */
29 extern unsigned long ppc_proc_freq;
30 #define DEFAULT_PROC_FREQ	(DEFAULT_TB_FREQ * 8)
31 extern unsigned long ppc_tb_freq;
32 #define DEFAULT_TB_FREQ		125000000UL
33 
34 extern bool tb_invalid;
35 
36 struct div_result {
37 	u64 result_high;
38 	u64 result_low;
39 };
40 
41 #ifdef CONFIG_PPC64
42 
43 /* For compatibility, get_tbl() is defined as get_tb() on ppc64 */
44 #define get_tbl		get_tb
45 
46 #else
47 
48 static inline unsigned long get_tbl(void)
49 {
50 	return mftbl();
51 }
52 
53 static inline unsigned int get_tbu(void)
54 {
55 	return mftbu();
56 }
57 #endif /* !CONFIG_PPC64 */
58 
59 static inline u64 get_vtb(void)
60 {
61 #ifdef CONFIG_PPC_BOOK3S_64
62 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
63 		return mfspr(SPRN_VTB);
64 #endif
65 	return 0;
66 }
67 
68 #ifdef CONFIG_PPC64
69 static inline u64 get_tb(void)
70 {
71 	return mftb();
72 }
73 #else /* CONFIG_PPC64 */
74 static inline u64 get_tb(void)
75 {
76 	unsigned int tbhi, tblo, tbhi2;
77 
78 	do {
79 		tbhi = get_tbu();
80 		tblo = get_tbl();
81 		tbhi2 = get_tbu();
82 	} while (tbhi != tbhi2);
83 
84 	return ((u64)tbhi << 32) | tblo;
85 }
86 #endif /* !CONFIG_PPC64 */
87 
88 static inline void set_tb(unsigned int upper, unsigned int lower)
89 {
90 	mtspr(SPRN_TBWL, 0);
91 	mtspr(SPRN_TBWU, upper);
92 	mtspr(SPRN_TBWL, lower);
93 }
94 
95 /* Accessor functions for the decrementer register.
96  * The 4xx doesn't even have a decrementer.  I tried to use the
97  * generic timer interrupt code, which seems OK, with the 4xx PIT
98  * in auto-reload mode.  The problem is PIT stops counting when it
99  * hits zero.  If it would wrap, we could use it just like a decrementer.
100  */
101 static inline u64 get_dec(void)
102 {
103 #if defined(CONFIG_40x)
104 	return (mfspr(SPRN_PIT));
105 #else
106 	return (mfspr(SPRN_DEC));
107 #endif
108 }
109 
110 /*
111  * Note: Book E and 4xx processors differ from other PowerPC processors
112  * in when the decrementer generates its interrupt: on the 1 to 0
113  * transition for Book E/4xx, but on the 0 to -1 transition for others.
114  */
115 static inline void set_dec(u64 val)
116 {
117 #if defined(CONFIG_40x)
118 	mtspr(SPRN_PIT, (u32) val);
119 #else
120 #ifndef CONFIG_BOOKE
121 	--val;
122 #endif
123 	mtspr(SPRN_DEC, val);
124 #endif /* not 40x */
125 }
126 
127 static inline unsigned long tb_ticks_since(unsigned long tstamp)
128 {
129 	return get_tbl() - tstamp;
130 }
131 
132 #define mulhwu(x,y) \
133 ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
134 
135 #ifdef CONFIG_PPC64
136 #define mulhdu(x,y) \
137 ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
138 #else
139 extern u64 mulhdu(u64, u64);
140 #endif
141 
142 extern void div128_by_32(u64 dividend_high, u64 dividend_low,
143 			 unsigned divisor, struct div_result *dr);
144 
145 extern void secondary_cpu_time_init(void);
146 extern void __init time_init(void);
147 
148 DECLARE_PER_CPU(u64, decrementers_next_tb);
149 
150 /* Convert timebase ticks to nanoseconds */
151 unsigned long long tb_to_ns(unsigned long long tb_ticks);
152 
153 /* SPLPAR */
154 void accumulate_stolen_time(void);
155 
156 #endif /* __KERNEL__ */
157 #endif /* __POWERPC_TIME_H */
158