xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision 6601ec1c2ba929430f5585ce7f9d9960b0e0a01d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f2783c15SPaul Mackerras /*
3f2783c15SPaul Mackerras  * Common time routines among all ppc machines.
4f2783c15SPaul Mackerras  *
5f2783c15SPaul Mackerras  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6f2783c15SPaul Mackerras  * Paul Mackerras' version and mine for PReP and Pmac.
7f2783c15SPaul Mackerras  * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
8f2783c15SPaul Mackerras  * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
9f2783c15SPaul Mackerras  *
10f2783c15SPaul Mackerras  * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
11f2783c15SPaul Mackerras  * to make clock more stable (2.4.0-test5). The only thing
12f2783c15SPaul Mackerras  * that this code assumes is that the timebases have been synchronized
13f2783c15SPaul Mackerras  * by firmware on SMP and are never stopped (never do sleep
14f2783c15SPaul Mackerras  * on SMP then, nap and doze are OK).
15f2783c15SPaul Mackerras  *
16f2783c15SPaul Mackerras  * Speeded up do_gettimeofday by getting rid of references to
17f2783c15SPaul Mackerras  * xtime (which required locks for consistency). (mikejc@us.ibm.com)
18f2783c15SPaul Mackerras  *
19f2783c15SPaul Mackerras  * TODO (not necessarily in this file):
20f2783c15SPaul Mackerras  * - improve precision and reproducibility of timebase frequency
21f5339277SStephen Rothwell  * measurement at boot time.
22f2783c15SPaul Mackerras  * - for astronomical applications: add a new function to get
23f2783c15SPaul Mackerras  * non ambiguous timestamps even around leap seconds. This needs
24f2783c15SPaul Mackerras  * a new timestamp format and a good name.
25f2783c15SPaul Mackerras  *
26f2783c15SPaul Mackerras  * 1997-09-10  Updated NTP code according to technical memorandum Jan '96
27f2783c15SPaul Mackerras  *             "A Kernel Model for Precision Timekeeping" by Dave Mills
28f2783c15SPaul Mackerras  */
29f2783c15SPaul Mackerras 
30f2783c15SPaul Mackerras #include <linux/errno.h>
314b16f8e2SPaul Gortmaker #include <linux/export.h>
32f2783c15SPaul Mackerras #include <linux/sched.h>
33e6017571SIngo Molnar #include <linux/sched/clock.h>
34f2783c15SPaul Mackerras #include <linux/kernel.h>
35f2783c15SPaul Mackerras #include <linux/param.h>
36f2783c15SPaul Mackerras #include <linux/string.h>
37f2783c15SPaul Mackerras #include <linux/mm.h>
38f2783c15SPaul Mackerras #include <linux/interrupt.h>
39f2783c15SPaul Mackerras #include <linux/timex.h>
40f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
41f2783c15SPaul Mackerras #include <linux/time.h>
42f2783c15SPaul Mackerras #include <linux/init.h>
43f2783c15SPaul Mackerras #include <linux/profile.h>
44f2783c15SPaul Mackerras #include <linux/cpu.h>
45f2783c15SPaul Mackerras #include <linux/security.h>
46f2783c15SPaul Mackerras #include <linux/percpu.h>
47f2783c15SPaul Mackerras #include <linux/rtc.h>
48092b8f34SPaul Mackerras #include <linux/jiffies.h>
49c6622f63SPaul Mackerras #include <linux/posix-timers.h>
507d12e780SDavid Howells #include <linux/irq.h>
51177996e6SBenjamin Herrenschmidt #include <linux/delay.h>
52e360adbeSPeter Zijlstra #include <linux/irq_work.h>
5360083063SGeert Uytterhoeven #include <linux/of_clk.h>
547f92bc56SDaniel Axtens #include <linux/suspend.h>
5532ef5517SIngo Molnar #include <linux/sched/cputime.h>
564e287e65SNicholas Piggin #include <linux/processor.h>
576795b85cSAnton Blanchard #include <asm/trace.h>
58f2783c15SPaul Mackerras 
59f2783c15SPaul Mackerras #include <asm/io.h>
60f2783c15SPaul Mackerras #include <asm/nvram.h>
61f2783c15SPaul Mackerras #include <asm/cache.h>
62f2783c15SPaul Mackerras #include <asm/machdep.h>
637c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
64f2783c15SPaul Mackerras #include <asm/time.h>
65f2783c15SPaul Mackerras #include <asm/prom.h>
66f2783c15SPaul Mackerras #include <asm/irq.h>
67f2783c15SPaul Mackerras #include <asm/div64.h>
682249ca9dSPaul Mackerras #include <asm/smp.h>
69a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
70f2783c15SPaul Mackerras #include <asm/firmware.h>
710545d543SDaniel Axtens #include <asm/asm-prototypes.h>
72f2783c15SPaul Mackerras 
734a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
744a4cfe38STony Breeds 
75d831d0b8STony Breeds #include <linux/clockchips.h>
76189374aeSJohn Stultz #include <linux/timekeeper_internal.h>
774a4cfe38STony Breeds 
78a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *);
794a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
804a4cfe38STony Breeds 	.name         = "timebase",
814a4cfe38STony Breeds 	.rating       = 400,
824a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
834a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
844a4cfe38STony Breeds 	.read         = timebase_read,
854a4cfe38STony Breeds };
864a4cfe38STony Breeds 
8779901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
8879901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
89d831d0b8STony Breeds 
90d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
91d831d0b8STony Breeds 				      struct clock_event_device *dev);
9237a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt);
93d831d0b8STony Breeds 
946e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = {
95d831d0b8STony Breeds 	.name			= "decrementer",
96d831d0b8STony Breeds 	.rating			= 200,
97d831d0b8STony Breeds 	.irq			= 0,
98d831d0b8STony Breeds 	.set_next_event		= decrementer_set_next_event,
9981759360SAnton Blanchard 	.set_state_oneshot_stopped = decrementer_shutdown,
10037a13e78SViresh Kumar 	.set_state_shutdown	= decrementer_shutdown,
10137a13e78SViresh Kumar 	.tick_resume		= decrementer_shutdown,
10237a13e78SViresh Kumar 	.features		= CLOCK_EVT_FEAT_ONESHOT |
10337a13e78SViresh Kumar 				  CLOCK_EVT_FEAT_C3STOP,
104d831d0b8STony Breeds };
1056e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent);
106d831d0b8STony Breeds 
1077df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb);
1087df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers);
109d831d0b8STony Breeds 
110f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
111f2783c15SPaul Mackerras 
112f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
113f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
114f2783c15SPaul Mackerras #else
115f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
116f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
117f2783c15SPaul Mackerras #endif
118f2783c15SPaul Mackerras 
119f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
120f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
121f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
122f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
1232cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime_t conversions */
124092b8f34SPaul Mackerras 
125f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
126f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
127f2783c15SPaul Mackerras 
128fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
129fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
130364a1246SHeiko Schocher static u64 boot_tb __read_mostly;
131f2783c15SPaul Mackerras 
132f2783c15SPaul Mackerras extern struct timezone sys_tz;
133f2783c15SPaul Mackerras static long timezone_offset;
134f2783c15SPaul Mackerras 
135f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
13655ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq);
137f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
13855ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq);
13996c44507SPaul Mackerras 
140de269129SMahesh Salgaonkar bool tb_invalid;
141de269129SMahesh Salgaonkar 
142abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
143c6622f63SPaul Mackerras /*
144e7f340caSFrederic Weisbecker  * Factor for converting from cputime_t (timebase ticks) to
145e7f340caSFrederic Weisbecker  * microseconds. This is stored as 0.64 fixed-point binary fraction.
146c6622f63SPaul Mackerras  */
1479f5072d4SAndreas Schwab u64 __cputime_usec_factor;
1489f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor);
149a42548a1SStanislaw Gruszka 
150c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR
151872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64);
152c223c903SChristophe Leroy #endif
153c223c903SChristophe Leroy 
154c6622f63SPaul Mackerras static void calc_cputime_factors(void)
155c6622f63SPaul Mackerras {
156c6622f63SPaul Mackerras 	struct div_result res;
157c6622f63SPaul Mackerras 
1589f5072d4SAndreas Schwab 	div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
1599f5072d4SAndreas Schwab 	__cputime_usec_factor = res.result_low;
160c6622f63SPaul Mackerras }
161c6622f63SPaul Mackerras 
162c6622f63SPaul Mackerras /*
163cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
164cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
165c6622f63SPaul Mackerras  */
166abcff86dSChristophe Leroy static inline unsigned long read_spurr(unsigned long tb)
167c6622f63SPaul Mackerras {
168cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
169cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
170c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
171c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
172cf9efce0SPaul Mackerras 	return tb;
173cf9efce0SPaul Mackerras }
174cf9efce0SPaul Mackerras 
175cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR
176cf9efce0SPaul Mackerras 
177d6bdceb6SPeter Zijlstra #include <asm/dtl.h>
178d6bdceb6SPeter Zijlstra 
179cf9efce0SPaul Mackerras /*
180cf9efce0SPaul Mackerras  * Scan the dispatch trace log and count up the stolen time.
181cf9efce0SPaul Mackerras  * Should be called with interrupts disabled.
182cf9efce0SPaul Mackerras  */
183cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb)
184cf9efce0SPaul Mackerras {
185872e439aSPaul Mackerras 	u64 i = local_paca->dtl_ridx;
186cf9efce0SPaul Mackerras 	struct dtl_entry *dtl = local_paca->dtl_curr;
187cf9efce0SPaul Mackerras 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
188cf9efce0SPaul Mackerras 	struct lppaca *vpa = local_paca->lppaca_ptr;
189cf9efce0SPaul Mackerras 	u64 tb_delta;
190cf9efce0SPaul Mackerras 	u64 stolen = 0;
191cf9efce0SPaul Mackerras 	u64 dtb;
192cf9efce0SPaul Mackerras 
19384ffae55SAnton Blanchard 	if (!dtl)
19484ffae55SAnton Blanchard 		return 0;
19584ffae55SAnton Blanchard 
1967ffcf8ecSAnton Blanchard 	if (i == be64_to_cpu(vpa->dtl_idx))
197cf9efce0SPaul Mackerras 		return 0;
1987ffcf8ecSAnton Blanchard 	while (i < be64_to_cpu(vpa->dtl_idx)) {
1997ffcf8ecSAnton Blanchard 		dtb = be64_to_cpu(dtl->timebase);
2007ffcf8ecSAnton Blanchard 		tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
2017ffcf8ecSAnton Blanchard 			be32_to_cpu(dtl->ready_to_enqueue_time);
202cf9efce0SPaul Mackerras 		barrier();
2037ffcf8ecSAnton Blanchard 		if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
204cf9efce0SPaul Mackerras 			/* buffer has overflowed */
2057ffcf8ecSAnton Blanchard 			i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
206cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
207cf9efce0SPaul Mackerras 			continue;
208cf9efce0SPaul Mackerras 		}
209cf9efce0SPaul Mackerras 		if (dtb > stop_tb)
210cf9efce0SPaul Mackerras 			break;
21184b07386SAnton Blanchard 		if (dtl_consumer)
21284b07386SAnton Blanchard 			dtl_consumer(dtl, i);
213cf9efce0SPaul Mackerras 		stolen += tb_delta;
214cf9efce0SPaul Mackerras 		++i;
215cf9efce0SPaul Mackerras 		++dtl;
216cf9efce0SPaul Mackerras 		if (dtl == dtl_end)
217cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log;
218cf9efce0SPaul Mackerras 	}
219cf9efce0SPaul Mackerras 	local_paca->dtl_ridx = i;
220cf9efce0SPaul Mackerras 	local_paca->dtl_curr = dtl;
221cf9efce0SPaul Mackerras 	return stolen;
222c6622f63SPaul Mackerras }
223c6622f63SPaul Mackerras 
224c6622f63SPaul Mackerras /*
225cf9efce0SPaul Mackerras  * Accumulate stolen time by scanning the dispatch trace log.
226cf9efce0SPaul Mackerras  * Called on entry from user mode.
2274603ac18SMichael Neuling  */
228eb8e20f8SMichael Ellerman void notrace accumulate_stolen_time(void)
2294603ac18SMichael Neuling {
230cf9efce0SPaul Mackerras 	u64 sst, ust;
2314e26bc4aSMadhavan Srinivasan 	unsigned long save_irq_soft_mask = irq_soft_mask_return();
232c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = &local_paca->accounting;
233b18ae08dSTejun Heo 
234b18ae08dSTejun Heo 	/* We are called early in the exception entry, before
235b18ae08dSTejun Heo 	 * soft/hard_enabled are sync'ed to the expected state
236b18ae08dSTejun Heo 	 * for the exception. We are hard disabled but the PACA
237b18ae08dSTejun Heo 	 * needs to reflect that so various debug stuff doesn't
238b18ae08dSTejun Heo 	 * complain
239b18ae08dSTejun Heo 	 */
2404e26bc4aSMadhavan Srinivasan 	irq_soft_mask_set(IRQS_DISABLED);
241b18ae08dSTejun Heo 
242c223c903SChristophe Leroy 	sst = scan_dispatch_log(acct->starttime_user);
243c223c903SChristophe Leroy 	ust = scan_dispatch_log(acct->starttime);
2448c8b73c4SFrederic Weisbecker 	acct->stime -= sst;
2458c8b73c4SFrederic Weisbecker 	acct->utime -= ust;
246f828c3d0SFrederic Weisbecker 	acct->steal_time += ust + sst;
247b18ae08dSTejun Heo 
2484e26bc4aSMadhavan Srinivasan 	irq_soft_mask_set(save_irq_soft_mask);
2494603ac18SMichael Neuling }
2504603ac18SMichael Neuling 
251cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
252cf9efce0SPaul Mackerras {
253a6201da3SAneesh Kumar K.V 	if (!firmware_has_feature(FW_FEATURE_SPLPAR))
254a6201da3SAneesh Kumar K.V 		return 0;
255a6201da3SAneesh Kumar K.V 
256a19ff1a2SFrederic Weisbecker 	if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
257a19ff1a2SFrederic Weisbecker 		return scan_dispatch_log(stop_tb);
258cf9efce0SPaul Mackerras 
259a19ff1a2SFrederic Weisbecker 	return 0;
260cf9efce0SPaul Mackerras }
261cf9efce0SPaul Mackerras 
262cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */
263cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
264cf9efce0SPaul Mackerras {
265cf9efce0SPaul Mackerras 	return 0;
266cf9efce0SPaul Mackerras }
267cf9efce0SPaul Mackerras 
268cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
269cf9efce0SPaul Mackerras 
2704603ac18SMichael Neuling /*
271c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
272c6622f63SPaul Mackerras  * or soft irq state.
273c6622f63SPaul Mackerras  */
274b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
275b38a181cSChristophe Leroy 					unsigned long now, unsigned long stime)
276c6622f63SPaul Mackerras {
277abcff86dSChristophe Leroy 	unsigned long stime_scaled = 0;
278abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
279b38a181cSChristophe Leroy 	unsigned long nowscaled, deltascaled;
280a19ff1a2SFrederic Weisbecker 	unsigned long utime, utime_scaled;
281c6622f63SPaul Mackerras 
2824603ac18SMichael Neuling 	nowscaled = read_spurr(now);
283c223c903SChristophe Leroy 	deltascaled = nowscaled - acct->startspurr;
284c223c903SChristophe Leroy 	acct->startspurr = nowscaled;
285a19ff1a2SFrederic Weisbecker 	utime = acct->utime - acct->utime_sspurr;
2868c8b73c4SFrederic Weisbecker 	acct->utime_sspurr = acct->utime;
287cf9efce0SPaul Mackerras 
288cf9efce0SPaul Mackerras 	/*
289cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
290cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
291cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
292cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
293cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
294cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
295cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
296cf9efce0SPaul Mackerras 	 * used by account_process_tick.
297cf9efce0SPaul Mackerras 	 */
298b38a181cSChristophe Leroy 	stime_scaled = stime;
299a19ff1a2SFrederic Weisbecker 	utime_scaled = utime;
300a19ff1a2SFrederic Weisbecker 	if (deltascaled != stime + utime) {
301a19ff1a2SFrederic Weisbecker 		if (utime) {
302b38a181cSChristophe Leroy 			stime_scaled = deltascaled * stime / (stime + utime);
303b38a181cSChristophe Leroy 			utime_scaled = deltascaled - stime_scaled;
304cf9efce0SPaul Mackerras 		} else {
305b38a181cSChristophe Leroy 			stime_scaled = deltascaled;
306c6622f63SPaul Mackerras 		}
307cf9efce0SPaul Mackerras 	}
308a19ff1a2SFrederic Weisbecker 	acct->utime_scaled += utime_scaled;
309abcff86dSChristophe Leroy #endif
310cf9efce0SPaul Mackerras 
311b38a181cSChristophe Leroy 	return stime_scaled;
312b38a181cSChristophe Leroy }
313b38a181cSChristophe Leroy 
314b38a181cSChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk,
315b38a181cSChristophe Leroy 				 unsigned long *stime_scaled,
316b38a181cSChristophe Leroy 				 unsigned long *steal_time)
317b38a181cSChristophe Leroy {
318b38a181cSChristophe Leroy 	unsigned long now, stime;
319b38a181cSChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
320b38a181cSChristophe Leroy 
321b38a181cSChristophe Leroy 	WARN_ON_ONCE(!irqs_disabled());
322b38a181cSChristophe Leroy 
323b38a181cSChristophe Leroy 	now = mftb();
324b38a181cSChristophe Leroy 	stime = now - acct->starttime;
325b38a181cSChristophe Leroy 	acct->starttime = now;
326b38a181cSChristophe Leroy 
327b38a181cSChristophe Leroy 	*stime_scaled = vtime_delta_scaled(acct, now, stime);
328b38a181cSChristophe Leroy 
329b38a181cSChristophe Leroy 	*steal_time = calculate_stolen_time(now);
330b38a181cSChristophe Leroy 
331a19ff1a2SFrederic Weisbecker 	return stime;
332a7e1a9e3SFrederic Weisbecker }
333a7e1a9e3SFrederic Weisbecker 
334f83eeb1aSFrederic Weisbecker void vtime_account_kernel(struct task_struct *tsk)
335a7e1a9e3SFrederic Weisbecker {
336a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
337a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
338a7e1a9e3SFrederic Weisbecker 
339a19ff1a2SFrederic Weisbecker 	stime = vtime_delta(tsk, &stime_scaled, &steal_time);
340a19ff1a2SFrederic Weisbecker 
341a19ff1a2SFrederic Weisbecker 	stime -= min(stime, steal_time);
342a19ff1a2SFrederic Weisbecker 	acct->steal_time += steal_time;
343a19ff1a2SFrederic Weisbecker 
344a19ff1a2SFrederic Weisbecker 	if ((tsk->flags & PF_VCPU) && !irq_count()) {
345a19ff1a2SFrederic Weisbecker 		acct->gtime += stime;
346abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
347a19ff1a2SFrederic Weisbecker 		acct->utime_scaled += stime_scaled;
348abcff86dSChristophe Leroy #endif
349a19ff1a2SFrederic Weisbecker 	} else {
350a19ff1a2SFrederic Weisbecker 		if (hardirq_count())
351a19ff1a2SFrederic Weisbecker 			acct->hardirq_time += stime;
352a19ff1a2SFrederic Weisbecker 		else if (in_serving_softirq())
353a19ff1a2SFrederic Weisbecker 			acct->softirq_time += stime;
354a19ff1a2SFrederic Weisbecker 		else
355a19ff1a2SFrederic Weisbecker 			acct->stime += stime;
356a19ff1a2SFrederic Weisbecker 
357abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
358a19ff1a2SFrederic Weisbecker 		acct->stime_scaled += stime_scaled;
359abcff86dSChristophe Leroy #endif
360a19ff1a2SFrederic Weisbecker 	}
361a7e1a9e3SFrederic Weisbecker }
362f83eeb1aSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_kernel);
363a7e1a9e3SFrederic Weisbecker 
364fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk)
365a7e1a9e3SFrederic Weisbecker {
366a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
367a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
368a7e1a9e3SFrederic Weisbecker 
369a19ff1a2SFrederic Weisbecker 	stime = vtime_delta(tsk, &stime_scaled, &steal_time);
370a19ff1a2SFrederic Weisbecker 	acct->idle_time += stime + steal_time;
371cf9efce0SPaul Mackerras }
372c6622f63SPaul Mackerras 
373b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk,
374b38a181cSChristophe Leroy 			       struct cpu_accounting_data *acct)
375b38a181cSChristophe Leroy {
376abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
377b38a181cSChristophe Leroy 	if (acct->utime_scaled)
378b38a181cSChristophe Leroy 		tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
379b38a181cSChristophe Leroy 	if (acct->stime_scaled)
380b38a181cSChristophe Leroy 		tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
381b38a181cSChristophe Leroy 
382b38a181cSChristophe Leroy 	acct->utime_scaled = 0;
383b38a181cSChristophe Leroy 	acct->utime_sspurr = 0;
384b38a181cSChristophe Leroy 	acct->stime_scaled = 0;
385abcff86dSChristophe Leroy #endif
386b38a181cSChristophe Leroy }
387b38a181cSChristophe Leroy 
388c6622f63SPaul Mackerras /*
389c8d7dabfSFrederic Weisbecker  * Account the whole cputime accumulated in the paca
390c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
391f83eeb1aSFrederic Weisbecker  * Assumes that vtime_account_kernel/idle() has been called
392bcebdf84SFrederic Weisbecker  * recently (i.e. since the last entry from usermode) so that
393cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
394c6622f63SPaul Mackerras  */
395c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk)
396c6622f63SPaul Mackerras {
397c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
398c6622f63SPaul Mackerras 
399a19ff1a2SFrederic Weisbecker 	if (acct->utime)
40023244a5cSFrederic Weisbecker 		account_user_time(tsk, cputime_to_nsecs(acct->utime));
401a19ff1a2SFrederic Weisbecker 
402a19ff1a2SFrederic Weisbecker 	if (acct->gtime)
403fb8b049cSFrederic Weisbecker 		account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
404a19ff1a2SFrederic Weisbecker 
40551eeef9eSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
406be9095edSFrederic Weisbecker 		account_steal_time(cputime_to_nsecs(acct->steal_time));
40751eeef9eSChristophe Leroy 		acct->steal_time = 0;
40851eeef9eSChristophe Leroy 	}
409a19ff1a2SFrederic Weisbecker 
410a19ff1a2SFrederic Weisbecker 	if (acct->idle_time)
41118b43a9bSFrederic Weisbecker 		account_idle_time(cputime_to_nsecs(acct->idle_time));
412a19ff1a2SFrederic Weisbecker 
413a19ff1a2SFrederic Weisbecker 	if (acct->stime)
414fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
415fb8b049cSFrederic Weisbecker 					  CPUTIME_SYSTEM);
416a19ff1a2SFrederic Weisbecker 
417a19ff1a2SFrederic Weisbecker 	if (acct->hardirq_time)
418fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
419fb8b049cSFrederic Weisbecker 					  CPUTIME_IRQ);
420a19ff1a2SFrederic Weisbecker 	if (acct->softirq_time)
421fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
422fb8b049cSFrederic Weisbecker 					  CPUTIME_SOFTIRQ);
423a19ff1a2SFrederic Weisbecker 
424b38a181cSChristophe Leroy 	vtime_flush_scaled(tsk, acct);
425b38a181cSChristophe Leroy 
4268c8b73c4SFrederic Weisbecker 	acct->utime = 0;
427a19ff1a2SFrederic Weisbecker 	acct->gtime = 0;
428a19ff1a2SFrederic Weisbecker 	acct->idle_time = 0;
429a19ff1a2SFrederic Weisbecker 	acct->stime = 0;
430a19ff1a2SFrederic Weisbecker 	acct->hardirq_time = 0;
431a19ff1a2SFrederic Weisbecker 	acct->softirq_time = 0;
432c6622f63SPaul Mackerras }
433c6622f63SPaul Mackerras 
434abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
435c6622f63SPaul Mackerras #define calc_cputime_factors()
436c6622f63SPaul Mackerras #endif
437c6622f63SPaul Mackerras 
4386defa38bSPaul Mackerras void __delay(unsigned long loops)
4396defa38bSPaul Mackerras {
4406defa38bSPaul Mackerras 	unsigned long start;
4416defa38bSPaul Mackerras 
4424e287e65SNicholas Piggin 	spin_begin();
443a4c5a355SChristophe Leroy 	if (tb_invalid) {
444de269129SMahesh Salgaonkar 		/*
445de269129SMahesh Salgaonkar 		 * TB is in error state and isn't ticking anymore.
446de269129SMahesh Salgaonkar 		 * HMI handler was unable to recover from TB error.
447de269129SMahesh Salgaonkar 		 * Return immediately, so that kernel won't get stuck here.
448de269129SMahesh Salgaonkar 		 */
449de269129SMahesh Salgaonkar 		spin_cpu_relax();
4506defa38bSPaul Mackerras 	} else {
4516defa38bSPaul Mackerras 		start = get_tbl();
4526defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
4534e287e65SNicholas Piggin 			spin_cpu_relax();
4546defa38bSPaul Mackerras 	}
4554e287e65SNicholas Piggin 	spin_end();
4566defa38bSPaul Mackerras }
4576defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4586defa38bSPaul Mackerras 
4596defa38bSPaul Mackerras void udelay(unsigned long usecs)
4606defa38bSPaul Mackerras {
4616defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4626defa38bSPaul Mackerras }
4636defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4646defa38bSPaul Mackerras 
465f2783c15SPaul Mackerras #ifdef CONFIG_SMP
466f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
467f2783c15SPaul Mackerras {
468f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
469f2783c15SPaul Mackerras 
470f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
471f2783c15SPaul Mackerras 		return regs->link;
472f2783c15SPaul Mackerras 
473f2783c15SPaul Mackerras 	return pc;
474f2783c15SPaul Mackerras }
475f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
476f2783c15SPaul Mackerras #endif
477f2783c15SPaul Mackerras 
478e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
479105988c0SPaul Mackerras 
4800fe1ac48SPaul Mackerras /*
4810fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
4820fe1ac48SPaul Mackerras  */
4830fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
484e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void)
485105988c0SPaul Mackerras {
4860fe1ac48SPaul Mackerras 	unsigned long x;
4870fe1ac48SPaul Mackerras 
4880fe1ac48SPaul Mackerras 	asm volatile("lbz %0,%1(13)"
4890fe1ac48SPaul Mackerras 		: "=r" (x)
490e360adbeSPeter Zijlstra 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
4910fe1ac48SPaul Mackerras 	return x;
492105988c0SPaul Mackerras }
493105988c0SPaul Mackerras 
494e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
4950fe1ac48SPaul Mackerras {
4960fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4970fe1ac48SPaul Mackerras 		"r" (1),
498e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4990fe1ac48SPaul Mackerras }
5000fe1ac48SPaul Mackerras 
501e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
5020fe1ac48SPaul Mackerras {
5030fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
5040fe1ac48SPaul Mackerras 		"r" (0),
505e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5060fe1ac48SPaul Mackerras }
5070fe1ac48SPaul Mackerras 
5080fe1ac48SPaul Mackerras #else /* 32-bit */
5090fe1ac48SPaul Mackerras 
510e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
5110fe1ac48SPaul Mackerras 
51269111bacSChristoph Lameter #define set_irq_work_pending_flag()	__this_cpu_write(irq_work_pending, 1)
51369111bacSChristoph Lameter #define test_irq_work_pending()		__this_cpu_read(irq_work_pending)
51469111bacSChristoph Lameter #define clear_irq_work_pending()	__this_cpu_write(irq_work_pending, 0)
515105988c0SPaul Mackerras 
516abc3fce7SNicholas Piggin #endif /* 32 vs 64 bit */
517abc3fce7SNicholas Piggin 
5184f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
5190fe1ac48SPaul Mackerras {
520abc3fce7SNicholas Piggin 	/*
521abc3fce7SNicholas Piggin 	 * 64-bit code that uses irq soft-mask can just cause an immediate
522abc3fce7SNicholas Piggin 	 * interrupt here that gets soft masked, if this is called under
523abc3fce7SNicholas Piggin 	 * local_irq_disable(). It might be possible to prevent that happening
524abc3fce7SNicholas Piggin 	 * by noticing interrupts are disabled and setting decrementer pending
525abc3fce7SNicholas Piggin 	 * to be replayed when irqs are enabled. The problem there is that
526abc3fce7SNicholas Piggin 	 * tracing can call irq_work_raise, including in code that does low
527abc3fce7SNicholas Piggin 	 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
528abc3fce7SNicholas Piggin 	 * which could get tangled up if we're messing with the same state
529abc3fce7SNicholas Piggin 	 * here.
530abc3fce7SNicholas Piggin 	 */
5310fe1ac48SPaul Mackerras 	preempt_disable();
532e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
5330fe1ac48SPaul Mackerras 	set_dec(1);
5340fe1ac48SPaul Mackerras 	preempt_enable();
5350fe1ac48SPaul Mackerras }
5360fe1ac48SPaul Mackerras 
537e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
538105988c0SPaul Mackerras 
539e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
540e360adbeSPeter Zijlstra #define clear_irq_work_pending()
541105988c0SPaul Mackerras 
542e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
543105988c0SPaul Mackerras 
5441b783955SPreeti U Murthy /*
5451b783955SPreeti U Murthy  * timer_interrupt - gets called when the decrementer overflows,
5461b783955SPreeti U Murthy  * with interrupts disabled.
5471b783955SPreeti U Murthy  */
5481b783955SPreeti U Murthy void timer_interrupt(struct pt_regs *regs)
5491b783955SPreeti U Murthy {
5503f984620SNicholas Piggin 	struct clock_event_device *evt = this_cpu_ptr(&decrementers);
55169111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
5523f984620SNicholas Piggin 	struct pt_regs *old_regs;
5533f984620SNicholas Piggin 	u64 now;
5541b783955SPreeti U Murthy 
5551b783955SPreeti U Murthy 	/* Some implementations of hotplug will get timer interrupts while
5561b783955SPreeti U Murthy 	 * offline, just ignore these and we also need to set
5571b783955SPreeti U Murthy 	 * decrementers_next_tb as MAX to make sure __check_irq_replay
5581b783955SPreeti U Murthy 	 * don't replay timer interrupt when return, otherwise we'll trap
5591b783955SPreeti U Murthy 	 * here infinitely :(
5601b783955SPreeti U Murthy 	 */
561a7cba02dSNicholas Piggin 	if (unlikely(!cpu_online(smp_processor_id()))) {
5621b783955SPreeti U Murthy 		*next_tb = ~(u64)0;
563a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
5641b783955SPreeti U Murthy 		return;
5651b783955SPreeti U Murthy 	}
5661b783955SPreeti U Murthy 
567a7cba02dSNicholas Piggin 	/* Ensure a positive value is written to the decrementer, or else
568a7cba02dSNicholas Piggin 	 * some CPUs will continue to take decrementer exceptions. When the
569a7cba02dSNicholas Piggin 	 * PPC_WATCHDOG (decrementer based) is configured, keep this at most
570a7cba02dSNicholas Piggin 	 * 31 bits, which is about 4 seconds on most systems, which gives
571a7cba02dSNicholas Piggin 	 * the watchdog a chance of catching timer interrupt hard lockups.
572a7cba02dSNicholas Piggin 	 */
573a7cba02dSNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
574a7cba02dSNicholas Piggin 		set_dec(0x7fffffff);
575a7cba02dSNicholas Piggin 	else
576a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
577a7cba02dSNicholas Piggin 
5781b783955SPreeti U Murthy 	/* Conditionally hard-enable interrupts now that the DEC has been
5791b783955SPreeti U Murthy 	 * bumped to its maximum value
5801b783955SPreeti U Murthy 	 */
5811b783955SPreeti U Murthy 	may_hard_irq_enable();
5821b783955SPreeti U Murthy 
5831b783955SPreeti U Murthy 
5846e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
5851b783955SPreeti U Murthy 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
5861b783955SPreeti U Murthy 		do_IRQ(regs);
5871b783955SPreeti U Murthy #endif
5881b783955SPreeti U Murthy 
5891b783955SPreeti U Murthy 	old_regs = set_irq_regs(regs);
5901b783955SPreeti U Murthy 	irq_enter();
5913f984620SNicholas Piggin 	trace_timer_interrupt_entry(regs);
5921b783955SPreeti U Murthy 
5933f984620SNicholas Piggin 	if (test_irq_work_pending()) {
5943f984620SNicholas Piggin 		clear_irq_work_pending();
5953f984620SNicholas Piggin 		irq_work_run();
5963f984620SNicholas Piggin 	}
5973f984620SNicholas Piggin 
598*6601ec1cSChristophe Leroy 	now = get_tb();
5993f984620SNicholas Piggin 	if (now >= *next_tb) {
6003f984620SNicholas Piggin 		*next_tb = ~(u64)0;
6013f984620SNicholas Piggin 		if (evt->event_handler)
6023f984620SNicholas Piggin 			evt->event_handler(evt);
6033f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_event);
6043f984620SNicholas Piggin 	} else {
6053f984620SNicholas Piggin 		now = *next_tb - now;
6063f984620SNicholas Piggin 		if (now <= decrementer_max)
6073f984620SNicholas Piggin 			set_dec(now);
6083f984620SNicholas Piggin 		/* We may have raced with new irq work */
6093f984620SNicholas Piggin 		if (test_irq_work_pending())
6103f984620SNicholas Piggin 			set_dec(1);
6113f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_others);
6123f984620SNicholas Piggin 	}
6133f984620SNicholas Piggin 
6143f984620SNicholas Piggin 	trace_timer_interrupt_exit(regs);
615f2783c15SPaul Mackerras 	irq_exit();
6167d12e780SDavid Howells 	set_irq_regs(old_regs);
617f2783c15SPaul Mackerras }
6189445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt);
619f2783c15SPaul Mackerras 
620bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
6213f984620SNicholas Piggin void timer_broadcast_interrupt(void)
6223f984620SNicholas Piggin {
6233f984620SNicholas Piggin 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
6243f984620SNicholas Piggin 
6253f984620SNicholas Piggin 	*next_tb = ~(u64)0;
6263f984620SNicholas Piggin 	tick_receive_broadcast();
627e360cd37SNicholas Piggin 	__this_cpu_inc(irq_stat.broadcast_irqs_event);
6283f984620SNicholas Piggin }
629bc907113SNicholas Piggin #endif
6303f984620SNicholas Piggin 
6317ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
632d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void)
6337ac5dde9SScott Wood {
6347ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
6357ac5dde9SScott Wood 	 * with suspending.
6367ac5dde9SScott Wood 	 */
6377ac5dde9SScott Wood 
63879901024SOliver O'Halloran 	set_dec(decrementer_max);
6397ac5dde9SScott Wood 	local_irq_disable();
64079901024SOliver O'Halloran 	set_dec(decrementer_max);
6417ac5dde9SScott Wood }
6427ac5dde9SScott Wood 
643d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void)
6447ac5dde9SScott Wood {
6457ac5dde9SScott Wood 	local_irq_enable();
6467ac5dde9SScott Wood }
6477ac5dde9SScott Wood 
6487ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6497ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
6507ac5dde9SScott Wood {
6517ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
6527ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
6537ac5dde9SScott Wood 	generic_suspend_disable_irqs();
6547ac5dde9SScott Wood }
6557ac5dde9SScott Wood 
6567ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6577ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
6587ac5dde9SScott Wood {
6597ac5dde9SScott Wood 	generic_suspend_enable_irqs();
6607ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
6617ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
6627ac5dde9SScott Wood }
6637ac5dde9SScott Wood #endif
6647ac5dde9SScott Wood 
665b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks)
666b6c295dfSPaul Mackerras {
667b6c295dfSPaul Mackerras 	return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
668b6c295dfSPaul Mackerras }
669b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns);
670b6c295dfSPaul Mackerras 
671f2783c15SPaul Mackerras /*
672f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
673f2783c15SPaul Mackerras  *
674f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
675f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
676f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
677f2783c15SPaul Mackerras  */
6786b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void)
679f2783c15SPaul Mackerras {
680fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
681f2783c15SPaul Mackerras }
682f2783c15SPaul Mackerras 
6834be1b297SCyril Bur 
6844be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES
6854be1b297SCyril Bur 
6864be1b297SCyril Bur /*
6874be1b297SCyril Bur  * Running clock - attempts to give a view of time passing for a virtualised
6884be1b297SCyril Bur  * kernels.
6894be1b297SCyril Bur  * Uses the VTB register if available otherwise a next best guess.
6904be1b297SCyril Bur  */
6914be1b297SCyril Bur unsigned long long running_clock(void)
6924be1b297SCyril Bur {
6934be1b297SCyril Bur 	/*
6944be1b297SCyril Bur 	 * Don't read the VTB as a host since KVM does not switch in host
6954be1b297SCyril Bur 	 * timebase into the VTB when it takes a guest off the CPU, reading the
6964be1b297SCyril Bur 	 * VTB would result in reading 'last switched out' guest VTB.
6974be1b297SCyril Bur 	 *
6984be1b297SCyril Bur 	 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
6994be1b297SCyril Bur 	 * would be unsafe to rely only on the #ifdef above.
7004be1b297SCyril Bur 	 */
7014be1b297SCyril Bur 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
7024be1b297SCyril Bur 	    cpu_has_feature(CPU_FTR_ARCH_207S))
7034be1b297SCyril Bur 		return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
7044be1b297SCyril Bur 
7054be1b297SCyril Bur 	/*
7064be1b297SCyril Bur 	 * This is a next best approximation without a VTB.
7074be1b297SCyril Bur 	 * On a host which is running bare metal there should never be any stolen
7084be1b297SCyril Bur 	 * time and on a host which doesn't do any virtualisation TB *should* equal
7094be1b297SCyril Bur 	 * VTB so it makes no difference anyway.
7104be1b297SCyril Bur 	 */
7119f3768e0SFrédéric Weisbecker 	return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
7124be1b297SCyril Bur }
7134be1b297SCyril Bur #endif
7144be1b297SCyril Bur 
7150bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
716f2783c15SPaul Mackerras {
717f2783c15SPaul Mackerras 	struct device_node *cpu;
7186f7aba7bSAnton Blanchard 	const __be32 *fp;
7190bb474a4SAnton Blanchard 	int found = 0;
720f2783c15SPaul Mackerras 
7210bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
722f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
723f2783c15SPaul Mackerras 
724d8a8188dSOlaf Hering 	if (cpu) {
725e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
726d8a8188dSOlaf Hering 		if (fp) {
7270bb474a4SAnton Blanchard 			found = 1;
728a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
729f2783c15SPaul Mackerras 		}
7300bb474a4SAnton Blanchard 
7310bb474a4SAnton Blanchard 		of_node_put(cpu);
732f2783c15SPaul Mackerras 	}
7330bb474a4SAnton Blanchard 
7340bb474a4SAnton Blanchard 	return found;
7350bb474a4SAnton Blanchard }
7360bb474a4SAnton Blanchard 
737e51df2c1SAnton Blanchard static void start_cpu_decrementer(void)
73877c0a700SBenjamin Herrenschmidt {
73977c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
7406e2f03e2SIvan Mikhaylov 	unsigned int tcr;
7416e2f03e2SIvan Mikhaylov 
74277c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
74377c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
74477c0a700SBenjamin Herrenschmidt 
7456e2f03e2SIvan Mikhaylov 	tcr = mfspr(SPRN_TCR);
7466e2f03e2SIvan Mikhaylov 	/*
7476e2f03e2SIvan Mikhaylov 	 * The watchdog may have already been enabled by u-boot. So leave
7486e2f03e2SIvan Mikhaylov 	 * TRC[WP] (Watchdog Period) alone.
7496e2f03e2SIvan Mikhaylov 	 */
7506e2f03e2SIvan Mikhaylov 	tcr &= TCR_WP_MASK;	/* Clear all bits except for TCR[WP] */
7516e2f03e2SIvan Mikhaylov 	tcr |= TCR_DIE;		/* Enable decrementer */
7526e2f03e2SIvan Mikhaylov 	mtspr(SPRN_TCR, tcr);
7536e2f03e2SIvan Mikhaylov #endif
75477c0a700SBenjamin Herrenschmidt }
75577c0a700SBenjamin Herrenschmidt 
7560bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
7570bb474a4SAnton Blanchard {
7580bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
7590bb474a4SAnton Blanchard 
7600bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
7610bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
7620bb474a4SAnton Blanchard 
763f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
764f2783c15SPaul Mackerras 				"(not found)\n");
7650bb474a4SAnton Blanchard 	}
766f2783c15SPaul Mackerras 
7670bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
7680bb474a4SAnton Blanchard 
7690bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
7700bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
7710bb474a4SAnton Blanchard 
7720bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
7730bb474a4SAnton Blanchard 				"(not found)\n");
774f2783c15SPaul Mackerras 	}
775f2783c15SPaul Mackerras }
776f2783c15SPaul Mackerras 
7775235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now)
778f2783c15SPaul Mackerras {
779f2783c15SPaul Mackerras 	struct rtc_time tm;
780f2783c15SPaul Mackerras 
781aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
782023f333aSJason Gunthorpe 		return -ENODEV;
783aa3be5f3STony Breeds 
7845235afa8SArnd Bergmann 	rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
785aa3be5f3STony Breeds 
786aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
787aa3be5f3STony Breeds }
788aa3be5f3STony Breeds 
7895bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts)
790aa3be5f3STony Breeds {
791aa3be5f3STony Breeds 	struct rtc_time tm;
792aa3be5f3STony Breeds 	static int first = 1;
793aa3be5f3STony Breeds 
794d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
795aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
796aa3be5f3STony Breeds 	if (first) {
797aa3be5f3STony Breeds 		first = 0;
798aa3be5f3STony Breeds 		if (ppc_md.time_init)
799aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
800aa3be5f3STony Breeds 
801aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
802d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
803d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
804d90246cdSMartin Schwidefsky 			return;
805aa3be5f3STony Breeds 		}
806d90246cdSMartin Schwidefsky 	}
807d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
808d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
809d90246cdSMartin Schwidefsky 		return;
810d90246cdSMartin Schwidefsky 	}
811f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
812978d7eb3SBenjamin Herrenschmidt 
8135bfd6435SArnd Bergmann 	ts->tv_sec = rtc_tm_to_time64(&tm);
814f2783c15SPaul Mackerras }
815f2783c15SPaul Mackerras 
8165bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts)
817978d7eb3SBenjamin Herrenschmidt {
818978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
819978d7eb3SBenjamin Herrenschmidt 
820978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
821978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
822978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
823978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
824978d7eb3SBenjamin Herrenschmidt 	}
825978d7eb3SBenjamin Herrenschmidt 
826978d7eb3SBenjamin Herrenschmidt }
827978d7eb3SBenjamin Herrenschmidt 
8284a4cfe38STony Breeds /* clocksource code */
8296b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs)
8304a4cfe38STony Breeds {
831a5a1d1c2SThomas Gleixner 	return (u64)get_tb();
8324a4cfe38STony Breeds }
8334a4cfe38STony Breeds 
834d4cfb113SPaul Mackerras 
835d4cfb113SPaul Mackerras void update_vsyscall(struct timekeeper *tk)
8364a4cfe38STony Breeds {
837176ed98cSArnd Bergmann 	struct timespec64 xt;
838d4cfb113SPaul Mackerras 	struct clocksource *clock = tk->tkr_mono.clock;
839d4cfb113SPaul Mackerras 	u32 mult = tk->tkr_mono.mult;
840d4cfb113SPaul Mackerras 	u32 shift = tk->tkr_mono.shift;
841d4cfb113SPaul Mackerras 	u64 cycle_last = tk->tkr_mono.cycle_last;
842b0797b60SJohn Stultz 	u64 new_tb_to_xs, new_stamp_xsec;
843d4cfb113SPaul Mackerras 	u64 frac_sec;
8444a4cfe38STony Breeds 
8454a4cfe38STony Breeds 	if (clock != &clocksource_timebase)
8464a4cfe38STony Breeds 		return;
8474a4cfe38STony Breeds 
848d4cfb113SPaul Mackerras 	xt.tv_sec = tk->xtime_sec;
849d4cfb113SPaul Mackerras 	xt.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
850d4cfb113SPaul Mackerras 
8514a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
8524a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8534a4cfe38STony Breeds 	smp_mb();
8544a4cfe38STony Breeds 
855d4cfb113SPaul Mackerras 	/*
856d4cfb113SPaul Mackerras 	 * This computes ((2^20 / 1e9) * mult) >> shift as a
857d4cfb113SPaul Mackerras 	 * 0.64 fixed-point fraction.
858d4cfb113SPaul Mackerras 	 * The computation in the else clause below won't overflow
859d4cfb113SPaul Mackerras 	 * (as long as the timebase frequency is >= 1.049 MHz)
860d4cfb113SPaul Mackerras 	 * but loses precision because we lose the low bits of the constant
861d4cfb113SPaul Mackerras 	 * in the shift.  Note that 19342813113834067 ~= 2^(20+64) / 1e9.
862d4cfb113SPaul Mackerras 	 * For a shift of 24 the error is about 0.5e-9, or about 0.5ns
863d4cfb113SPaul Mackerras 	 * over a second.  (Shift values are usually 22, 23 or 24.)
864d4cfb113SPaul Mackerras 	 * For high frequency clocks such as the 512MHz timebase clock
865d4cfb113SPaul Mackerras 	 * on POWER[6789], the mult value is small (e.g. 32768000)
866d4cfb113SPaul Mackerras 	 * and so we can shift the constant by 16 initially
867d4cfb113SPaul Mackerras 	 * (295147905179 ~= 2^(20+64-16) / 1e9) and then do the
868d4cfb113SPaul Mackerras 	 * remaining shifts after the multiplication, which gives a
869d4cfb113SPaul Mackerras 	 * more accurate result (e.g. with mult = 32768000, shift = 24,
870d4cfb113SPaul Mackerras 	 * the error is only about 1.2e-12, or 0.7ns over 10 minutes).
871d4cfb113SPaul Mackerras 	 */
872d4cfb113SPaul Mackerras 	if (mult <= 62500000 && clock->shift >= 16)
873d4cfb113SPaul Mackerras 		new_tb_to_xs = ((u64) mult * 295147905179ULL) >> (clock->shift - 16);
874d4cfb113SPaul Mackerras 	else
87511b8633aSAnton Blanchard 		new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift);
876b0797b60SJohn Stultz 
877d4cfb113SPaul Mackerras 	/*
878d4cfb113SPaul Mackerras 	 * Compute the fractional second in units of 2^-32 seconds.
879d4cfb113SPaul Mackerras 	 * The fractional second is tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift
880d4cfb113SPaul Mackerras 	 * in nanoseconds, so multiplying that by 2^32 / 1e9 gives
881d4cfb113SPaul Mackerras 	 * it in units of 2^-32 seconds.
882d4cfb113SPaul Mackerras 	 * We assume shift <= 32 because clocks_calc_mult_shift()
883d4cfb113SPaul Mackerras 	 * generates shift values in the range 0 - 32.
884d4cfb113SPaul Mackerras 	 */
885d4cfb113SPaul Mackerras 	frac_sec = tk->tkr_mono.xtime_nsec << (32 - shift);
886d4cfb113SPaul Mackerras 	do_div(frac_sec, NSEC_PER_SEC);
887d4cfb113SPaul Mackerras 
888d4cfb113SPaul Mackerras 	/*
889d4cfb113SPaul Mackerras 	 * Work out new stamp_xsec value for any legacy users of systemcfg.
890d4cfb113SPaul Mackerras 	 * stamp_xsec is in units of 2^-20 seconds.
891d4cfb113SPaul Mackerras 	 */
892d4cfb113SPaul Mackerras 	new_stamp_xsec = frac_sec >> 12;
893d4cfb113SPaul Mackerras 	new_stamp_xsec += tk->xtime_sec * XSEC_PER_SEC;
89447916be4SThomas Gleixner 
895b0797b60SJohn Stultz 	/*
896b0797b60SJohn Stultz 	 * tb_update_count is used to allow the userspace gettimeofday code
897b0797b60SJohn Stultz 	 * to assure itself that it sees a consistent view of the tb_to_xs and
898b0797b60SJohn Stultz 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
899b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
900b0797b60SJohn Stultz 	 * the two values of tb_update_count match and are even then the
901b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
902b0797b60SJohn Stultz 	 * loops back and reads them again until this criteria is met.
903b0797b60SJohn Stultz 	 */
9044a0e6377SThomas Gleixner 	vdso_data->tb_orig_stamp = cycle_last;
905b0797b60SJohn Stultz 	vdso_data->stamp_xsec = new_stamp_xsec;
906b0797b60SJohn Stultz 	vdso_data->tb_to_xs = new_tb_to_xs;
907d4cfb113SPaul Mackerras 	vdso_data->wtom_clock_sec = tk->wall_to_monotonic.tv_sec;
908d4cfb113SPaul Mackerras 	vdso_data->wtom_clock_nsec = tk->wall_to_monotonic.tv_nsec;
909176ed98cSArnd Bergmann 	vdso_data->stamp_xtime_sec = xt.tv_sec;
910176ed98cSArnd Bergmann 	vdso_data->stamp_xtime_nsec = xt.tv_nsec;
91147916be4SThomas Gleixner 	vdso_data->stamp_sec_fraction = frac_sec;
91255226345SVincenzo Frascino 	vdso_data->hrtimer_res = hrtimer_resolution;
913b0797b60SJohn Stultz 	smp_wmb();
914b0797b60SJohn Stultz 	++(vdso_data->tb_update_count);
9154a4cfe38STony Breeds }
9164a4cfe38STony Breeds 
9174a4cfe38STony Breeds void update_vsyscall_tz(void)
9184a4cfe38STony Breeds {
9194a4cfe38STony Breeds 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
9204a4cfe38STony Breeds 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
9214a4cfe38STony Breeds }
9224a4cfe38STony Breeds 
9231c21a293SMichael Ellerman static void __init clocksource_init(void)
9244a4cfe38STony Breeds {
925a4c5a355SChristophe Leroy 	struct clocksource *clock = &clocksource_timebase;
9264a4cfe38STony Breeds 
92711b8633aSAnton Blanchard 	if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
9284a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
9294a4cfe38STony Breeds 		       clock->name);
9304a4cfe38STony Breeds 		return;
9314a4cfe38STony Breeds 	}
9324a4cfe38STony Breeds 
9334a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
9344a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
9354a4cfe38STony Breeds }
9364a4cfe38STony Breeds 
937d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
938d831d0b8STony Breeds 				      struct clock_event_device *dev)
939d831d0b8STony Breeds {
940*6601ec1cSChristophe Leroy 	__this_cpu_write(decrementers_next_tb, get_tb() + evt);
941d831d0b8STony Breeds 	set_dec(evt);
9420215f7d8SBenjamin Herrenschmidt 
9430215f7d8SBenjamin Herrenschmidt 	/* We may have raced with new irq work */
9440215f7d8SBenjamin Herrenschmidt 	if (test_irq_work_pending())
9450215f7d8SBenjamin Herrenschmidt 		set_dec(1);
9460215f7d8SBenjamin Herrenschmidt 
947d831d0b8STony Breeds 	return 0;
948d831d0b8STony Breeds }
949d831d0b8STony Breeds 
95037a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev)
951d831d0b8STony Breeds {
95279901024SOliver O'Halloran 	decrementer_set_next_event(decrementer_max, dev);
95337a13e78SViresh Kumar 	return 0;
954d831d0b8STony Breeds }
955d831d0b8STony Breeds 
956d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
957d831d0b8STony Breeds {
9587df10275SAnton Blanchard 	struct clock_event_device *dec = &per_cpu(decrementers, cpu);
959d831d0b8STony Breeds 
960d831d0b8STony Breeds 	*dec = decrementer_clockevent;
961320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
962d831d0b8STony Breeds 
9638b78fdb0SAnton Blanchard 	clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
9648b78fdb0SAnton Blanchard 
965b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
966d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
967b4d16ab5SMichael Ellerman 
968b4d16ab5SMichael Ellerman 	/* Set values for KVM, see kvm_emulate_dec() */
969b4d16ab5SMichael Ellerman 	decrementer_clockevent.mult = dec->mult;
970b4d16ab5SMichael Ellerman 	decrementer_clockevent.shift = dec->shift;
971d831d0b8STony Breeds }
972d831d0b8STony Breeds 
97379901024SOliver O'Halloran static void enable_large_decrementer(void)
97479901024SOliver O'Halloran {
97579901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
97679901024SOliver O'Halloran 		return;
97779901024SOliver O'Halloran 
97879901024SOliver O'Halloran 	if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
97979901024SOliver O'Halloran 		return;
98079901024SOliver O'Halloran 
98179901024SOliver O'Halloran 	/*
98279901024SOliver O'Halloran 	 * If we're running as the hypervisor we need to enable the LD manually
98379901024SOliver O'Halloran 	 * otherwise firmware should have done it for us.
98479901024SOliver O'Halloran 	 */
98579901024SOliver O'Halloran 	if (cpu_has_feature(CPU_FTR_HVMODE))
98679901024SOliver O'Halloran 		mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
98779901024SOliver O'Halloran }
98879901024SOliver O'Halloran 
98979901024SOliver O'Halloran static void __init set_decrementer_max(void)
99079901024SOliver O'Halloran {
99179901024SOliver O'Halloran 	struct device_node *cpu;
99279901024SOliver O'Halloran 	u32 bits = 32;
99379901024SOliver O'Halloran 
99479901024SOliver O'Halloran 	/* Prior to ISAv3 the decrementer is always 32 bit */
99579901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
99679901024SOliver O'Halloran 		return;
99779901024SOliver O'Halloran 
99879901024SOliver O'Halloran 	cpu = of_find_node_by_type(NULL, "cpu");
99979901024SOliver O'Halloran 
100079901024SOliver O'Halloran 	if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
100179901024SOliver O'Halloran 		if (bits > 64 || bits < 32) {
100279901024SOliver O'Halloran 			pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
100379901024SOliver O'Halloran 			bits = 32;
100479901024SOliver O'Halloran 		}
100579901024SOliver O'Halloran 
100679901024SOliver O'Halloran 		/* calculate the signed maximum given this many bits */
100779901024SOliver O'Halloran 		decrementer_max = (1ul << (bits - 1)) - 1;
100879901024SOliver O'Halloran 	}
100979901024SOliver O'Halloran 
101079901024SOliver O'Halloran 	of_node_put(cpu);
101179901024SOliver O'Halloran 
101279901024SOliver O'Halloran 	pr_info("time_init: %u bit decrementer (max: %llx)\n",
101379901024SOliver O'Halloran 		bits, decrementer_max);
101479901024SOliver O'Halloran }
101579901024SOliver O'Halloran 
1016c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
1017d831d0b8STony Breeds {
10188b78fdb0SAnton Blanchard 	register_decrementer_clockevent(smp_processor_id());
1019d831d0b8STony Breeds }
1020d831d0b8STony Breeds 
1021d831d0b8STony Breeds void secondary_cpu_time_init(void)
1022d831d0b8STony Breeds {
102379901024SOliver O'Halloran 	/* Enable and test the large decrementer for this cpu */
102479901024SOliver O'Halloran 	enable_large_decrementer();
102579901024SOliver O'Halloran 
102677c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
102777c0a700SBenjamin Herrenschmidt 	 * such as BookE
102877c0a700SBenjamin Herrenschmidt 	 */
102977c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
103077c0a700SBenjamin Herrenschmidt 
1031d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
1032d831d0b8STony Breeds 	 * call here ! */
1033d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
1034d831d0b8STony Breeds }
1035d831d0b8STony Breeds 
1036f2783c15SPaul Mackerras /* This function is only called on the boot processor */
1037f2783c15SPaul Mackerras void __init time_init(void)
1038f2783c15SPaul Mackerras {
1039f2783c15SPaul Mackerras 	struct div_result res;
1040d75d68cfSPaul Mackerras 	u64 scale;
1041f2783c15SPaul Mackerras 	unsigned shift;
1042f2783c15SPaul Mackerras 
104396c44507SPaul Mackerras 	/* Normal PowerPC with timebase register */
1044f2783c15SPaul Mackerras 	ppc_md.calibrate_decr();
1045224ad80aSOlof Johansson 	printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
1046374e99d4SPaul Mackerras 	       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
1047224ad80aSOlof Johansson 	printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
1048374e99d4SPaul Mackerras 	       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
1049374e99d4SPaul Mackerras 
1050374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
1051092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
1052374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
1053c6622f63SPaul Mackerras 	calc_cputime_factors();
1054092b8f34SPaul Mackerras 
1055092b8f34SPaul Mackerras 	/*
1056f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
1057f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
1058f2783c15SPaul Mackerras 	 * which is the timebase frequency.
1059f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
1060f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
1061f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
1062f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
1063f2783c15SPaul Mackerras 	 * sched_clock().
1064f2783c15SPaul Mackerras 	 */
1065f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
1066f2783c15SPaul Mackerras 	scale = res.result_low;
1067f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
1068f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
1069f2783c15SPaul Mackerras 		res.result_high >>= 1;
1070f2783c15SPaul Mackerras 	}
1071f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
1072f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
1073fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
1074*6601ec1cSChristophe Leroy 	boot_tb = get_tb();
1075f2783c15SPaul Mackerras 
1076092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
1077092b8f34SPaul Mackerras 	if (timezone_offset) {
1078092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
1079092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
1080092b8f34SPaul Mackerras 	}
1081092b8f34SPaul Mackerras 
1082a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
1083a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
1084f2783c15SPaul Mackerras 
108579901024SOliver O'Halloran 	/* initialise and enable the large decrementer (if we have one) */
108679901024SOliver O'Halloran 	set_decrementer_max();
108779901024SOliver O'Halloran 	enable_large_decrementer();
108879901024SOliver O'Halloran 
108977c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
109077c0a700SBenjamin Herrenschmidt 	 * such as BookE
109177c0a700SBenjamin Herrenschmidt 	 */
109277c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
109377c0a700SBenjamin Herrenschmidt 
1094f5339277SStephen Rothwell 	/* Register the clocksource */
10954a4cfe38STony Breeds 	clocksource_init();
10964a4cfe38STony Breeds 
1097d831d0b8STony Breeds 	init_decrementer_clockevent();
10980d948730SPreeti U Murthy 	tick_setup_hrtimer_broadcast();
1099f0d37300SKevin Hao 
1100f0d37300SKevin Hao 	of_clk_init(NULL);
1101f2783c15SPaul Mackerras }
1102f2783c15SPaul Mackerras 
1103f2783c15SPaul Mackerras /*
1104f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1105f2783c15SPaul Mackerras  * result.
1106f2783c15SPaul Mackerras  */
1107f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1108f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1109f2783c15SPaul Mackerras {
1110f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1111f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1112f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1113f2783c15SPaul Mackerras 
1114f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1115f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1116f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1117f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1118f2783c15SPaul Mackerras 
1119f2783c15SPaul Mackerras 	w = a / divisor;
1120f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1121f2783c15SPaul Mackerras 
1122f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1123f2783c15SPaul Mackerras 	x = ra;
1124f2783c15SPaul Mackerras 
1125f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1126f2783c15SPaul Mackerras 	y = rb;
1127f2783c15SPaul Mackerras 
1128f2783c15SPaul Mackerras 	do_div(rc, divisor);
1129f2783c15SPaul Mackerras 	z = rc;
1130f2783c15SPaul Mackerras 
1131f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1132f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1133f2783c15SPaul Mackerras 
1134f2783c15SPaul Mackerras }
1135bcd68a70SGeert Uytterhoeven 
1136177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
1137177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
1138177996e6SBenjamin Herrenschmidt {
1139177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
1140177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
1141177996e6SBenjamin Herrenschmidt 	 */
1142177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
1143177996e6SBenjamin Herrenschmidt }
1144177996e6SBenjamin Herrenschmidt 
1145169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1146169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1147169047f4SArnd Bergmann {
1148169047f4SArnd Bergmann 	ppc_md.get_rtc_time(tm);
1149890ae797SAlexandre Belloni 	return 0;
1150169047f4SArnd Bergmann }
1151169047f4SArnd Bergmann 
1152169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1153169047f4SArnd Bergmann {
1154169047f4SArnd Bergmann 	if (!ppc_md.set_rtc_time)
1155169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1156169047f4SArnd Bergmann 
1157169047f4SArnd Bergmann 	if (ppc_md.set_rtc_time(tm) < 0)
1158169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1159169047f4SArnd Bergmann 
1160169047f4SArnd Bergmann 	return 0;
1161169047f4SArnd Bergmann }
1162169047f4SArnd Bergmann 
1163169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = {
1164169047f4SArnd Bergmann 	.read_time = rtc_generic_get_time,
1165169047f4SArnd Bergmann 	.set_time = rtc_generic_set_time,
1166169047f4SArnd Bergmann };
1167169047f4SArnd Bergmann 
1168bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1169bcd68a70SGeert Uytterhoeven {
1170bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1171bcd68a70SGeert Uytterhoeven 
1172bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1173bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1174bcd68a70SGeert Uytterhoeven 
1175169047f4SArnd Bergmann 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1176169047f4SArnd Bergmann 					     &rtc_generic_ops,
1177169047f4SArnd Bergmann 					     sizeof(rtc_generic_ops));
1178bcd68a70SGeert Uytterhoeven 
11798c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(pdev);
1180bcd68a70SGeert Uytterhoeven }
1181bcd68a70SGeert Uytterhoeven 
11828f6b9512SPaul Gortmaker device_initcall(rtc_init);
1183169047f4SArnd Bergmann #endif
1184