xref: /openbmc/linux/arch/powerpc/include/asm/time.h (revision b6c295df)
1b8b572e1SStephen Rothwell /*
2b8b572e1SStephen Rothwell  * Common time prototypes and such for all ppc machines.
3b8b572e1SStephen Rothwell  *
4b8b572e1SStephen Rothwell  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5b8b572e1SStephen Rothwell  * Paul Mackerras' version and mine for PReP and Pmac.
6b8b572e1SStephen Rothwell  *
7b8b572e1SStephen Rothwell  * This program is free software; you can redistribute it and/or
8b8b572e1SStephen Rothwell  * modify it under the terms of the GNU General Public License
9b8b572e1SStephen Rothwell  * as published by the Free Software Foundation; either version
10b8b572e1SStephen Rothwell  * 2 of the License, or (at your option) any later version.
11b8b572e1SStephen Rothwell  */
12b8b572e1SStephen Rothwell 
13b8b572e1SStephen Rothwell #ifndef __POWERPC_TIME_H
14b8b572e1SStephen Rothwell #define __POWERPC_TIME_H
15b8b572e1SStephen Rothwell 
16b8b572e1SStephen Rothwell #ifdef __KERNEL__
17b8b572e1SStephen Rothwell #include <linux/types.h>
18b8b572e1SStephen Rothwell #include <linux/percpu.h>
19b8b572e1SStephen Rothwell 
20b8b572e1SStephen Rothwell #include <asm/processor.h>
21b8b572e1SStephen Rothwell 
22b8b572e1SStephen Rothwell /* time.c */
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;
27b8b572e1SStephen Rothwell 
28b8b572e1SStephen Rothwell struct rtc_time;
29b8b572e1SStephen Rothwell extern void to_tm(int tim, struct rtc_time * tm);
30b8b572e1SStephen Rothwell extern void GregorianDay(struct rtc_time *tm);
311b67bee1SSrivatsa S. Bhat extern void tick_broadcast_ipi_handler(void);
32b8b572e1SStephen Rothwell 
33b8b572e1SStephen Rothwell extern void generic_calibrate_decr(void);
34b8b572e1SStephen Rothwell 
35b8b572e1SStephen Rothwell extern void set_dec_cpu6(unsigned int val);
36b8b572e1SStephen Rothwell 
37b8b572e1SStephen Rothwell /* Some sane defaults: 125 MHz timebase, 1GHz processor */
38b8b572e1SStephen Rothwell extern unsigned long ppc_proc_freq;
39b8b572e1SStephen Rothwell #define DEFAULT_PROC_FREQ	(DEFAULT_TB_FREQ * 8)
40b8b572e1SStephen Rothwell extern unsigned long ppc_tb_freq;
41b8b572e1SStephen Rothwell #define DEFAULT_TB_FREQ		125000000UL
42b8b572e1SStephen Rothwell 
43b8b572e1SStephen Rothwell struct div_result {
44b8b572e1SStephen Rothwell 	u64 result_high;
45b8b572e1SStephen Rothwell 	u64 result_low;
46b8b572e1SStephen Rothwell };
47b8b572e1SStephen Rothwell 
48b8b572e1SStephen Rothwell /* Accessor functions for the timebase (RTC on 601) registers. */
49b8b572e1SStephen Rothwell /* If one day CONFIG_POWER is added just define __USE_RTC as 1 */
50b8b572e1SStephen Rothwell #ifdef CONFIG_6xx
51b8b572e1SStephen Rothwell #define __USE_RTC()	(!cpu_has_feature(CPU_FTR_USE_TB))
52b8b572e1SStephen Rothwell #else
53b8b572e1SStephen Rothwell #define __USE_RTC()	0
54b8b572e1SStephen Rothwell #endif
55b8b572e1SStephen Rothwell 
56b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
57b8b572e1SStephen Rothwell 
58b8b572e1SStephen Rothwell /* For compatibility, get_tbl() is defined as get_tb() on ppc64 */
59b8b572e1SStephen Rothwell #define get_tbl		get_tb
60b8b572e1SStephen Rothwell 
61b8b572e1SStephen Rothwell #else
62b8b572e1SStephen Rothwell 
63b8b572e1SStephen Rothwell static inline unsigned long get_tbl(void)
64b8b572e1SStephen Rothwell {
65b8b572e1SStephen Rothwell #if defined(CONFIG_403GCX)
66b8b572e1SStephen Rothwell 	unsigned long tbl;
67b8b572e1SStephen Rothwell 	asm volatile("mfspr %0, 0x3dd" : "=r" (tbl));
68b8b572e1SStephen Rothwell 	return tbl;
69b8b572e1SStephen Rothwell #else
70b8b572e1SStephen Rothwell 	return mftbl();
71b8b572e1SStephen Rothwell #endif
72b8b572e1SStephen Rothwell }
73b8b572e1SStephen Rothwell 
74b8b572e1SStephen Rothwell static inline unsigned int get_tbu(void)
75b8b572e1SStephen Rothwell {
76b8b572e1SStephen Rothwell #ifdef CONFIG_403GCX
77b8b572e1SStephen Rothwell 	unsigned int tbu;
78b8b572e1SStephen Rothwell 	asm volatile("mfspr %0, 0x3dc" : "=r" (tbu));
79b8b572e1SStephen Rothwell 	return tbu;
80b8b572e1SStephen Rothwell #else
81b8b572e1SStephen Rothwell 	return mftbu();
82b8b572e1SStephen Rothwell #endif
83b8b572e1SStephen Rothwell }
84b8b572e1SStephen Rothwell #endif /* !CONFIG_PPC64 */
85b8b572e1SStephen Rothwell 
86b8b572e1SStephen Rothwell static inline unsigned int get_rtcl(void)
87b8b572e1SStephen Rothwell {
88b8b572e1SStephen Rothwell 	unsigned int rtcl;
89b8b572e1SStephen Rothwell 
90b8b572e1SStephen Rothwell 	asm volatile("mfrtcl %0" : "=r" (rtcl));
91b8b572e1SStephen Rothwell 	return rtcl;
92b8b572e1SStephen Rothwell }
93b8b572e1SStephen Rothwell 
94b8b572e1SStephen Rothwell static inline u64 get_rtc(void)
95b8b572e1SStephen Rothwell {
96b8b572e1SStephen Rothwell 	unsigned int hi, lo, hi2;
97b8b572e1SStephen Rothwell 
98b8b572e1SStephen Rothwell 	do {
99b8b572e1SStephen Rothwell 		asm volatile("mfrtcu %0; mfrtcl %1; mfrtcu %2"
100b8b572e1SStephen Rothwell 			     : "=r" (hi), "=r" (lo), "=r" (hi2));
101b8b572e1SStephen Rothwell 	} while (hi2 != hi);
102b8b572e1SStephen Rothwell 	return (u64)hi * 1000000000 + lo;
103b8b572e1SStephen Rothwell }
104b8b572e1SStephen Rothwell 
1058f42ab27SAneesh Kumar K.V static inline u64 get_vtb(void)
1068f42ab27SAneesh Kumar K.V {
1078f42ab27SAneesh Kumar K.V #ifdef CONFIG_PPC_BOOK3S_64
1088f42ab27SAneesh Kumar K.V 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
1098f42ab27SAneesh Kumar K.V 		return mfvtb();
1108f42ab27SAneesh Kumar K.V #endif
1118f42ab27SAneesh Kumar K.V 	return 0;
1128f42ab27SAneesh Kumar K.V }
1138f42ab27SAneesh Kumar K.V 
114b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
115b8b572e1SStephen Rothwell static inline u64 get_tb(void)
116b8b572e1SStephen Rothwell {
117b8b572e1SStephen Rothwell 	return mftb();
118b8b572e1SStephen Rothwell }
119b8b572e1SStephen Rothwell #else /* CONFIG_PPC64 */
120b8b572e1SStephen Rothwell static inline u64 get_tb(void)
121b8b572e1SStephen Rothwell {
122b8b572e1SStephen Rothwell 	unsigned int tbhi, tblo, tbhi2;
123b8b572e1SStephen Rothwell 
124b8b572e1SStephen Rothwell 	do {
125b8b572e1SStephen Rothwell 		tbhi = get_tbu();
126b8b572e1SStephen Rothwell 		tblo = get_tbl();
127b8b572e1SStephen Rothwell 		tbhi2 = get_tbu();
128b8b572e1SStephen Rothwell 	} while (tbhi != tbhi2);
129b8b572e1SStephen Rothwell 
130b8b572e1SStephen Rothwell 	return ((u64)tbhi << 32) | tblo;
131b8b572e1SStephen Rothwell }
132b8b572e1SStephen Rothwell #endif /* !CONFIG_PPC64 */
133b8b572e1SStephen Rothwell 
134b8b572e1SStephen Rothwell static inline u64 get_tb_or_rtc(void)
135b8b572e1SStephen Rothwell {
136b8b572e1SStephen Rothwell 	return __USE_RTC() ? get_rtc() : get_tb();
137b8b572e1SStephen Rothwell }
138b8b572e1SStephen Rothwell 
139b8b572e1SStephen Rothwell static inline void set_tb(unsigned int upper, unsigned int lower)
140b8b572e1SStephen Rothwell {
141b8b572e1SStephen Rothwell 	mtspr(SPRN_TBWL, 0);
142b8b572e1SStephen Rothwell 	mtspr(SPRN_TBWU, upper);
143b8b572e1SStephen Rothwell 	mtspr(SPRN_TBWL, lower);
144b8b572e1SStephen Rothwell }
145b8b572e1SStephen Rothwell 
146b8b572e1SStephen Rothwell /* Accessor functions for the decrementer register.
147b8b572e1SStephen Rothwell  * The 4xx doesn't even have a decrementer.  I tried to use the
148b8b572e1SStephen Rothwell  * generic timer interrupt code, which seems OK, with the 4xx PIT
149b8b572e1SStephen Rothwell  * in auto-reload mode.  The problem is PIT stops counting when it
150b8b572e1SStephen Rothwell  * hits zero.  If it would wrap, we could use it just like a decrementer.
151b8b572e1SStephen Rothwell  */
152b8b572e1SStephen Rothwell static inline unsigned int get_dec(void)
153b8b572e1SStephen Rothwell {
154b8b572e1SStephen Rothwell #if defined(CONFIG_40x)
155b8b572e1SStephen Rothwell 	return (mfspr(SPRN_PIT));
156b8b572e1SStephen Rothwell #else
157b8b572e1SStephen Rothwell 	return (mfspr(SPRN_DEC));
158b8b572e1SStephen Rothwell #endif
159b8b572e1SStephen Rothwell }
160b8b572e1SStephen Rothwell 
161b8b572e1SStephen Rothwell /*
162b8b572e1SStephen Rothwell  * Note: Book E and 4xx processors differ from other PowerPC processors
163b8b572e1SStephen Rothwell  * in when the decrementer generates its interrupt: on the 1 to 0
164b8b572e1SStephen Rothwell  * transition for Book E/4xx, but on the 0 to -1 transition for others.
165b8b572e1SStephen Rothwell  */
166b8b572e1SStephen Rothwell static inline void set_dec(int val)
167b8b572e1SStephen Rothwell {
168b8b572e1SStephen Rothwell #if defined(CONFIG_40x)
169b8b572e1SStephen Rothwell 	mtspr(SPRN_PIT, val);
170b8b572e1SStephen Rothwell #elif defined(CONFIG_8xx_CPU6)
171b8b572e1SStephen Rothwell 	set_dec_cpu6(val - 1);
172b8b572e1SStephen Rothwell #else
173b8b572e1SStephen Rothwell #ifndef CONFIG_BOOKE
174b8b572e1SStephen Rothwell 	--val;
175b8b572e1SStephen Rothwell #endif
176b8b572e1SStephen Rothwell 	mtspr(SPRN_DEC, val);
177b8b572e1SStephen Rothwell #endif /* not 40x or 8xx_CPU6 */
178b8b572e1SStephen Rothwell }
179b8b572e1SStephen Rothwell 
180b8b572e1SStephen Rothwell static inline unsigned long tb_ticks_since(unsigned long tstamp)
181b8b572e1SStephen Rothwell {
182b8b572e1SStephen Rothwell 	if (__USE_RTC()) {
183b8b572e1SStephen Rothwell 		int delta = get_rtcl() - (unsigned int) tstamp;
184b8b572e1SStephen Rothwell 		return delta < 0 ? delta + 1000000000 : delta;
185b8b572e1SStephen Rothwell 	}
186b8b572e1SStephen Rothwell 	return get_tbl() - tstamp;
187b8b572e1SStephen Rothwell }
188b8b572e1SStephen Rothwell 
189b8b572e1SStephen Rothwell #define mulhwu(x,y) \
190b8b572e1SStephen Rothwell ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
191b8b572e1SStephen Rothwell 
192b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
193b8b572e1SStephen Rothwell #define mulhdu(x,y) \
194b8b572e1SStephen Rothwell ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
195b8b572e1SStephen Rothwell #else
196b8b572e1SStephen Rothwell extern u64 mulhdu(u64, u64);
197b8b572e1SStephen Rothwell #endif
198b8b572e1SStephen Rothwell 
199b8b572e1SStephen Rothwell extern void div128_by_32(u64 dividend_high, u64 dividend_low,
200b8b572e1SStephen Rothwell 			 unsigned divisor, struct div_result *dr);
201b8b572e1SStephen Rothwell 
202b8b572e1SStephen Rothwell /* Used to store Processor Utilization register (purr) values */
203b8b572e1SStephen Rothwell 
204b8b572e1SStephen Rothwell struct cpu_usage {
205b8b572e1SStephen Rothwell         u64 current_tb;  /* Holds the current purr register values */
206b8b572e1SStephen Rothwell };
207b8b572e1SStephen Rothwell 
208b8b572e1SStephen Rothwell DECLARE_PER_CPU(struct cpu_usage, cpu_usage_array);
209b8b572e1SStephen Rothwell 
210b8b572e1SStephen Rothwell extern void secondary_cpu_time_init(void);
211b8b572e1SStephen Rothwell 
2127df10275SAnton Blanchard DECLARE_PER_CPU(u64, decrementers_next_tb);
21337fb9a02SAnton Blanchard 
214b6c295dfSPaul Mackerras /* Convert timebase ticks to nanoseconds */
215b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long tb_ticks);
216b6c295dfSPaul Mackerras 
217b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
218b8b572e1SStephen Rothwell #endif /* __POWERPC_TIME_H */
219