xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision ab037dd87a2f946556850e204c06cbd7a2a19390)
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,
85*ab037dd8SChristophe Leroy 	.vdso_clock_mode	= VDSO_CLOCKMODE_ARCHTIMER,
864a4cfe38STony Breeds };
874a4cfe38STony Breeds 
8879901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
8979901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
90d831d0b8STony Breeds 
91d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
92d831d0b8STony Breeds 				      struct clock_event_device *dev);
9337a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt);
94d831d0b8STony Breeds 
956e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = {
96d831d0b8STony Breeds 	.name			= "decrementer",
97d831d0b8STony Breeds 	.rating			= 200,
98d831d0b8STony Breeds 	.irq			= 0,
99d831d0b8STony Breeds 	.set_next_event		= decrementer_set_next_event,
10081759360SAnton Blanchard 	.set_state_oneshot_stopped = decrementer_shutdown,
10137a13e78SViresh Kumar 	.set_state_shutdown	= decrementer_shutdown,
10237a13e78SViresh Kumar 	.tick_resume		= decrementer_shutdown,
10337a13e78SViresh Kumar 	.features		= CLOCK_EVT_FEAT_ONESHOT |
10437a13e78SViresh Kumar 				  CLOCK_EVT_FEAT_C3STOP,
105d831d0b8STony Breeds };
1066e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent);
107d831d0b8STony Breeds 
1087df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb);
1097df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers);
110d831d0b8STony Breeds 
111f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
112f2783c15SPaul Mackerras 
113f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
114f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
115f2783c15SPaul Mackerras #else
116f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
117f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
118f2783c15SPaul Mackerras #endif
119f2783c15SPaul Mackerras 
120f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
121f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
122f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
123f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
1242cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime_t conversions */
125092b8f34SPaul Mackerras 
126f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
127f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
128f2783c15SPaul Mackerras 
129fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
130fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
131364a1246SHeiko Schocher static u64 boot_tb __read_mostly;
132f2783c15SPaul Mackerras 
133f2783c15SPaul Mackerras extern struct timezone sys_tz;
134f2783c15SPaul Mackerras static long timezone_offset;
135f2783c15SPaul Mackerras 
136f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
13755ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq);
138f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
13955ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq);
14096c44507SPaul Mackerras 
141de269129SMahesh Salgaonkar bool tb_invalid;
142de269129SMahesh Salgaonkar 
143abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
144c6622f63SPaul Mackerras /*
145e7f340caSFrederic Weisbecker  * Factor for converting from cputime_t (timebase ticks) to
146e7f340caSFrederic Weisbecker  * microseconds. This is stored as 0.64 fixed-point binary fraction.
147c6622f63SPaul Mackerras  */
1489f5072d4SAndreas Schwab u64 __cputime_usec_factor;
1499f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor);
150a42548a1SStanislaw Gruszka 
151c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR
152872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64);
153c223c903SChristophe Leroy #endif
154c223c903SChristophe Leroy 
155c6622f63SPaul Mackerras static void calc_cputime_factors(void)
156c6622f63SPaul Mackerras {
157c6622f63SPaul Mackerras 	struct div_result res;
158c6622f63SPaul Mackerras 
1599f5072d4SAndreas Schwab 	div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
1609f5072d4SAndreas Schwab 	__cputime_usec_factor = res.result_low;
161c6622f63SPaul Mackerras }
162c6622f63SPaul Mackerras 
163c6622f63SPaul Mackerras /*
164cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
165cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
166c6622f63SPaul Mackerras  */
167abcff86dSChristophe Leroy static inline unsigned long read_spurr(unsigned long tb)
168c6622f63SPaul Mackerras {
169cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
170cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
171c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
172c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
173cf9efce0SPaul Mackerras 	return tb;
174cf9efce0SPaul Mackerras }
175cf9efce0SPaul Mackerras 
176cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR
177cf9efce0SPaul Mackerras 
178d6bdceb6SPeter Zijlstra #include <asm/dtl.h>
179d6bdceb6SPeter Zijlstra 
180cf9efce0SPaul Mackerras /*
181cf9efce0SPaul Mackerras  * Scan the dispatch trace log and count up the stolen time.
182cf9efce0SPaul Mackerras  * Should be called with interrupts disabled.
183cf9efce0SPaul Mackerras  */
184cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb)
185cf9efce0SPaul Mackerras {
186872e439aSPaul Mackerras 	u64 i = local_paca->dtl_ridx;
187cf9efce0SPaul Mackerras 	struct dtl_entry *dtl = local_paca->dtl_curr;
188cf9efce0SPaul Mackerras 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
189cf9efce0SPaul Mackerras 	struct lppaca *vpa = local_paca->lppaca_ptr;
190cf9efce0SPaul Mackerras 	u64 tb_delta;
191cf9efce0SPaul Mackerras 	u64 stolen = 0;
192cf9efce0SPaul Mackerras 	u64 dtb;
193cf9efce0SPaul Mackerras 
19484ffae55SAnton Blanchard 	if (!dtl)
19584ffae55SAnton Blanchard 		return 0;
19684ffae55SAnton Blanchard 
1977ffcf8ecSAnton Blanchard 	if (i == be64_to_cpu(vpa->dtl_idx))
198cf9efce0SPaul Mackerras 		return 0;
1997ffcf8ecSAnton Blanchard 	while (i < be64_to_cpu(vpa->dtl_idx)) {
2007ffcf8ecSAnton Blanchard 		dtb = be64_to_cpu(dtl->timebase);
2017ffcf8ecSAnton Blanchard 		tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
2027ffcf8ecSAnton Blanchard 			be32_to_cpu(dtl->ready_to_enqueue_time);
203cf9efce0SPaul Mackerras 		barrier();
2047ffcf8ecSAnton Blanchard 		if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
205cf9efce0SPaul Mackerras 			/* buffer has overflowed */
2067ffcf8ecSAnton Blanchard 			i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
207cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
208cf9efce0SPaul Mackerras 			continue;
209cf9efce0SPaul Mackerras 		}
210cf9efce0SPaul Mackerras 		if (dtb > stop_tb)
211cf9efce0SPaul Mackerras 			break;
21284b07386SAnton Blanchard 		if (dtl_consumer)
21384b07386SAnton Blanchard 			dtl_consumer(dtl, i);
214cf9efce0SPaul Mackerras 		stolen += tb_delta;
215cf9efce0SPaul Mackerras 		++i;
216cf9efce0SPaul Mackerras 		++dtl;
217cf9efce0SPaul Mackerras 		if (dtl == dtl_end)
218cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log;
219cf9efce0SPaul Mackerras 	}
220cf9efce0SPaul Mackerras 	local_paca->dtl_ridx = i;
221cf9efce0SPaul Mackerras 	local_paca->dtl_curr = dtl;
222cf9efce0SPaul Mackerras 	return stolen;
223c6622f63SPaul Mackerras }
224c6622f63SPaul Mackerras 
225c6622f63SPaul Mackerras /*
226cf9efce0SPaul Mackerras  * Accumulate stolen time by scanning the dispatch trace log.
227cf9efce0SPaul Mackerras  * Called on entry from user mode.
2284603ac18SMichael Neuling  */
229eb8e20f8SMichael Ellerman void notrace accumulate_stolen_time(void)
2304603ac18SMichael Neuling {
231cf9efce0SPaul Mackerras 	u64 sst, ust;
2324e26bc4aSMadhavan Srinivasan 	unsigned long save_irq_soft_mask = irq_soft_mask_return();
233c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = &local_paca->accounting;
234b18ae08dSTejun Heo 
235b18ae08dSTejun Heo 	/* We are called early in the exception entry, before
236b18ae08dSTejun Heo 	 * soft/hard_enabled are sync'ed to the expected state
237b18ae08dSTejun Heo 	 * for the exception. We are hard disabled but the PACA
238b18ae08dSTejun Heo 	 * needs to reflect that so various debug stuff doesn't
239b18ae08dSTejun Heo 	 * complain
240b18ae08dSTejun Heo 	 */
2414e26bc4aSMadhavan Srinivasan 	irq_soft_mask_set(IRQS_DISABLED);
242b18ae08dSTejun Heo 
243c223c903SChristophe Leroy 	sst = scan_dispatch_log(acct->starttime_user);
244c223c903SChristophe Leroy 	ust = scan_dispatch_log(acct->starttime);
2458c8b73c4SFrederic Weisbecker 	acct->stime -= sst;
2468c8b73c4SFrederic Weisbecker 	acct->utime -= ust;
247f828c3d0SFrederic Weisbecker 	acct->steal_time += ust + sst;
248b18ae08dSTejun Heo 
2494e26bc4aSMadhavan Srinivasan 	irq_soft_mask_set(save_irq_soft_mask);
2504603ac18SMichael Neuling }
2514603ac18SMichael Neuling 
252cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
253cf9efce0SPaul Mackerras {
254a6201da3SAneesh Kumar K.V 	if (!firmware_has_feature(FW_FEATURE_SPLPAR))
255a6201da3SAneesh Kumar K.V 		return 0;
256a6201da3SAneesh Kumar K.V 
257a19ff1a2SFrederic Weisbecker 	if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
258a19ff1a2SFrederic Weisbecker 		return scan_dispatch_log(stop_tb);
259cf9efce0SPaul Mackerras 
260a19ff1a2SFrederic Weisbecker 	return 0;
261cf9efce0SPaul Mackerras }
262cf9efce0SPaul Mackerras 
263cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */
264cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
265cf9efce0SPaul Mackerras {
266cf9efce0SPaul Mackerras 	return 0;
267cf9efce0SPaul Mackerras }
268cf9efce0SPaul Mackerras 
269cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
270cf9efce0SPaul Mackerras 
2714603ac18SMichael Neuling /*
272c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
273c6622f63SPaul Mackerras  * or soft irq state.
274c6622f63SPaul Mackerras  */
275b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
276b38a181cSChristophe Leroy 					unsigned long now, unsigned long stime)
277c6622f63SPaul Mackerras {
278abcff86dSChristophe Leroy 	unsigned long stime_scaled = 0;
279abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
280b38a181cSChristophe Leroy 	unsigned long nowscaled, deltascaled;
281a19ff1a2SFrederic Weisbecker 	unsigned long utime, utime_scaled;
282c6622f63SPaul Mackerras 
2834603ac18SMichael Neuling 	nowscaled = read_spurr(now);
284c223c903SChristophe Leroy 	deltascaled = nowscaled - acct->startspurr;
285c223c903SChristophe Leroy 	acct->startspurr = nowscaled;
286a19ff1a2SFrederic Weisbecker 	utime = acct->utime - acct->utime_sspurr;
2878c8b73c4SFrederic Weisbecker 	acct->utime_sspurr = acct->utime;
288cf9efce0SPaul Mackerras 
289cf9efce0SPaul Mackerras 	/*
290cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
291cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
292cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
293cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
294cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
295cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
296cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
297cf9efce0SPaul Mackerras 	 * used by account_process_tick.
298cf9efce0SPaul Mackerras 	 */
299b38a181cSChristophe Leroy 	stime_scaled = stime;
300a19ff1a2SFrederic Weisbecker 	utime_scaled = utime;
301a19ff1a2SFrederic Weisbecker 	if (deltascaled != stime + utime) {
302a19ff1a2SFrederic Weisbecker 		if (utime) {
303b38a181cSChristophe Leroy 			stime_scaled = deltascaled * stime / (stime + utime);
304b38a181cSChristophe Leroy 			utime_scaled = deltascaled - stime_scaled;
305cf9efce0SPaul Mackerras 		} else {
306b38a181cSChristophe Leroy 			stime_scaled = deltascaled;
307c6622f63SPaul Mackerras 		}
308cf9efce0SPaul Mackerras 	}
309a19ff1a2SFrederic Weisbecker 	acct->utime_scaled += utime_scaled;
310abcff86dSChristophe Leroy #endif
311cf9efce0SPaul Mackerras 
312b38a181cSChristophe Leroy 	return stime_scaled;
313b38a181cSChristophe Leroy }
314b38a181cSChristophe Leroy 
315b38a181cSChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk,
316b38a181cSChristophe Leroy 				 unsigned long *stime_scaled,
317b38a181cSChristophe Leroy 				 unsigned long *steal_time)
318b38a181cSChristophe Leroy {
319b38a181cSChristophe Leroy 	unsigned long now, stime;
320b38a181cSChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
321b38a181cSChristophe Leroy 
322b38a181cSChristophe Leroy 	WARN_ON_ONCE(!irqs_disabled());
323b38a181cSChristophe Leroy 
324b38a181cSChristophe Leroy 	now = mftb();
325b38a181cSChristophe Leroy 	stime = now - acct->starttime;
326b38a181cSChristophe Leroy 	acct->starttime = now;
327b38a181cSChristophe Leroy 
328b38a181cSChristophe Leroy 	*stime_scaled = vtime_delta_scaled(acct, now, stime);
329b38a181cSChristophe Leroy 
330b38a181cSChristophe Leroy 	*steal_time = calculate_stolen_time(now);
331b38a181cSChristophe Leroy 
332a19ff1a2SFrederic Weisbecker 	return stime;
333a7e1a9e3SFrederic Weisbecker }
334a7e1a9e3SFrederic Weisbecker 
335f83eeb1aSFrederic Weisbecker void vtime_account_kernel(struct task_struct *tsk)
336a7e1a9e3SFrederic Weisbecker {
337a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
338a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
339a7e1a9e3SFrederic Weisbecker 
340a19ff1a2SFrederic Weisbecker 	stime = vtime_delta(tsk, &stime_scaled, &steal_time);
341a19ff1a2SFrederic Weisbecker 
342a19ff1a2SFrederic Weisbecker 	stime -= min(stime, steal_time);
343a19ff1a2SFrederic Weisbecker 	acct->steal_time += steal_time;
344a19ff1a2SFrederic Weisbecker 
345a19ff1a2SFrederic Weisbecker 	if ((tsk->flags & PF_VCPU) && !irq_count()) {
346a19ff1a2SFrederic Weisbecker 		acct->gtime += stime;
347abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
348a19ff1a2SFrederic Weisbecker 		acct->utime_scaled += stime_scaled;
349abcff86dSChristophe Leroy #endif
350a19ff1a2SFrederic Weisbecker 	} else {
351a19ff1a2SFrederic Weisbecker 		if (hardirq_count())
352a19ff1a2SFrederic Weisbecker 			acct->hardirq_time += stime;
353a19ff1a2SFrederic Weisbecker 		else if (in_serving_softirq())
354a19ff1a2SFrederic Weisbecker 			acct->softirq_time += stime;
355a19ff1a2SFrederic Weisbecker 		else
356a19ff1a2SFrederic Weisbecker 			acct->stime += stime;
357a19ff1a2SFrederic Weisbecker 
358abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
359a19ff1a2SFrederic Weisbecker 		acct->stime_scaled += stime_scaled;
360abcff86dSChristophe Leroy #endif
361a19ff1a2SFrederic Weisbecker 	}
362a7e1a9e3SFrederic Weisbecker }
363f83eeb1aSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_kernel);
364a7e1a9e3SFrederic Weisbecker 
365fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk)
366a7e1a9e3SFrederic Weisbecker {
367a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
368a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
369a7e1a9e3SFrederic Weisbecker 
370a19ff1a2SFrederic Weisbecker 	stime = vtime_delta(tsk, &stime_scaled, &steal_time);
371a19ff1a2SFrederic Weisbecker 	acct->idle_time += stime + steal_time;
372cf9efce0SPaul Mackerras }
373c6622f63SPaul Mackerras 
374b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk,
375b38a181cSChristophe Leroy 			       struct cpu_accounting_data *acct)
376b38a181cSChristophe Leroy {
377abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
378b38a181cSChristophe Leroy 	if (acct->utime_scaled)
379b38a181cSChristophe Leroy 		tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
380b38a181cSChristophe Leroy 	if (acct->stime_scaled)
381b38a181cSChristophe Leroy 		tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
382b38a181cSChristophe Leroy 
383b38a181cSChristophe Leroy 	acct->utime_scaled = 0;
384b38a181cSChristophe Leroy 	acct->utime_sspurr = 0;
385b38a181cSChristophe Leroy 	acct->stime_scaled = 0;
386abcff86dSChristophe Leroy #endif
387b38a181cSChristophe Leroy }
388b38a181cSChristophe Leroy 
389c6622f63SPaul Mackerras /*
390c8d7dabfSFrederic Weisbecker  * Account the whole cputime accumulated in the paca
391c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
392f83eeb1aSFrederic Weisbecker  * Assumes that vtime_account_kernel/idle() has been called
393bcebdf84SFrederic Weisbecker  * recently (i.e. since the last entry from usermode) so that
394cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
395c6622f63SPaul Mackerras  */
396c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk)
397c6622f63SPaul Mackerras {
398c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
399c6622f63SPaul Mackerras 
400a19ff1a2SFrederic Weisbecker 	if (acct->utime)
40123244a5cSFrederic Weisbecker 		account_user_time(tsk, cputime_to_nsecs(acct->utime));
402a19ff1a2SFrederic Weisbecker 
403a19ff1a2SFrederic Weisbecker 	if (acct->gtime)
404fb8b049cSFrederic Weisbecker 		account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
405a19ff1a2SFrederic Weisbecker 
40651eeef9eSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
407be9095edSFrederic Weisbecker 		account_steal_time(cputime_to_nsecs(acct->steal_time));
40851eeef9eSChristophe Leroy 		acct->steal_time = 0;
40951eeef9eSChristophe Leroy 	}
410a19ff1a2SFrederic Weisbecker 
411a19ff1a2SFrederic Weisbecker 	if (acct->idle_time)
41218b43a9bSFrederic Weisbecker 		account_idle_time(cputime_to_nsecs(acct->idle_time));
413a19ff1a2SFrederic Weisbecker 
414a19ff1a2SFrederic Weisbecker 	if (acct->stime)
415fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
416fb8b049cSFrederic Weisbecker 					  CPUTIME_SYSTEM);
417a19ff1a2SFrederic Weisbecker 
418a19ff1a2SFrederic Weisbecker 	if (acct->hardirq_time)
419fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
420fb8b049cSFrederic Weisbecker 					  CPUTIME_IRQ);
421a19ff1a2SFrederic Weisbecker 	if (acct->softirq_time)
422fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
423fb8b049cSFrederic Weisbecker 					  CPUTIME_SOFTIRQ);
424a19ff1a2SFrederic Weisbecker 
425b38a181cSChristophe Leroy 	vtime_flush_scaled(tsk, acct);
426b38a181cSChristophe Leroy 
4278c8b73c4SFrederic Weisbecker 	acct->utime = 0;
428a19ff1a2SFrederic Weisbecker 	acct->gtime = 0;
429a19ff1a2SFrederic Weisbecker 	acct->idle_time = 0;
430a19ff1a2SFrederic Weisbecker 	acct->stime = 0;
431a19ff1a2SFrederic Weisbecker 	acct->hardirq_time = 0;
432a19ff1a2SFrederic Weisbecker 	acct->softirq_time = 0;
433c6622f63SPaul Mackerras }
434c6622f63SPaul Mackerras 
435abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
436c6622f63SPaul Mackerras #define calc_cputime_factors()
437c6622f63SPaul Mackerras #endif
438c6622f63SPaul Mackerras 
4396defa38bSPaul Mackerras void __delay(unsigned long loops)
4406defa38bSPaul Mackerras {
4416defa38bSPaul Mackerras 	unsigned long start;
4426defa38bSPaul Mackerras 
4434e287e65SNicholas Piggin 	spin_begin();
444a4c5a355SChristophe Leroy 	if (tb_invalid) {
445de269129SMahesh Salgaonkar 		/*
446de269129SMahesh Salgaonkar 		 * TB is in error state and isn't ticking anymore.
447de269129SMahesh Salgaonkar 		 * HMI handler was unable to recover from TB error.
448de269129SMahesh Salgaonkar 		 * Return immediately, so that kernel won't get stuck here.
449de269129SMahesh Salgaonkar 		 */
450de269129SMahesh Salgaonkar 		spin_cpu_relax();
4516defa38bSPaul Mackerras 	} else {
452942e8911SChristophe Leroy 		start = mftb();
453942e8911SChristophe Leroy 		while (mftb() - start < loops)
4544e287e65SNicholas Piggin 			spin_cpu_relax();
4556defa38bSPaul Mackerras 	}
4564e287e65SNicholas Piggin 	spin_end();
4576defa38bSPaul Mackerras }
4586defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4596defa38bSPaul Mackerras 
4606defa38bSPaul Mackerras void udelay(unsigned long usecs)
4616defa38bSPaul Mackerras {
4626defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4636defa38bSPaul Mackerras }
4646defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4656defa38bSPaul Mackerras 
466f2783c15SPaul Mackerras #ifdef CONFIG_SMP
467f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
468f2783c15SPaul Mackerras {
469f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
470f2783c15SPaul Mackerras 
471f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
472f2783c15SPaul Mackerras 		return regs->link;
473f2783c15SPaul Mackerras 
474f2783c15SPaul Mackerras 	return pc;
475f2783c15SPaul Mackerras }
476f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
477f2783c15SPaul Mackerras #endif
478f2783c15SPaul Mackerras 
479e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
480105988c0SPaul Mackerras 
4810fe1ac48SPaul Mackerras /*
4820fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
4830fe1ac48SPaul Mackerras  */
4840fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
485e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void)
486105988c0SPaul Mackerras {
4870fe1ac48SPaul Mackerras 	unsigned long x;
4880fe1ac48SPaul Mackerras 
4890fe1ac48SPaul Mackerras 	asm volatile("lbz %0,%1(13)"
4900fe1ac48SPaul Mackerras 		: "=r" (x)
491e360adbeSPeter Zijlstra 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
4920fe1ac48SPaul Mackerras 	return x;
493105988c0SPaul Mackerras }
494105988c0SPaul Mackerras 
495e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
4960fe1ac48SPaul Mackerras {
4970fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4980fe1ac48SPaul Mackerras 		"r" (1),
499e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5000fe1ac48SPaul Mackerras }
5010fe1ac48SPaul Mackerras 
502e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
5030fe1ac48SPaul Mackerras {
5040fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
5050fe1ac48SPaul Mackerras 		"r" (0),
506e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5070fe1ac48SPaul Mackerras }
5080fe1ac48SPaul Mackerras 
5090fe1ac48SPaul Mackerras #else /* 32-bit */
5100fe1ac48SPaul Mackerras 
511e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
5120fe1ac48SPaul Mackerras 
51369111bacSChristoph Lameter #define set_irq_work_pending_flag()	__this_cpu_write(irq_work_pending, 1)
51469111bacSChristoph Lameter #define test_irq_work_pending()		__this_cpu_read(irq_work_pending)
51569111bacSChristoph Lameter #define clear_irq_work_pending()	__this_cpu_write(irq_work_pending, 0)
516105988c0SPaul Mackerras 
517abc3fce7SNicholas Piggin #endif /* 32 vs 64 bit */
518abc3fce7SNicholas Piggin 
5194f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
5200fe1ac48SPaul Mackerras {
521abc3fce7SNicholas Piggin 	/*
522abc3fce7SNicholas Piggin 	 * 64-bit code that uses irq soft-mask can just cause an immediate
523abc3fce7SNicholas Piggin 	 * interrupt here that gets soft masked, if this is called under
524abc3fce7SNicholas Piggin 	 * local_irq_disable(). It might be possible to prevent that happening
525abc3fce7SNicholas Piggin 	 * by noticing interrupts are disabled and setting decrementer pending
526abc3fce7SNicholas Piggin 	 * to be replayed when irqs are enabled. The problem there is that
527abc3fce7SNicholas Piggin 	 * tracing can call irq_work_raise, including in code that does low
528abc3fce7SNicholas Piggin 	 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
529abc3fce7SNicholas Piggin 	 * which could get tangled up if we're messing with the same state
530abc3fce7SNicholas Piggin 	 * here.
531abc3fce7SNicholas Piggin 	 */
5320fe1ac48SPaul Mackerras 	preempt_disable();
533e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
5340fe1ac48SPaul Mackerras 	set_dec(1);
5350fe1ac48SPaul Mackerras 	preempt_enable();
5360fe1ac48SPaul Mackerras }
5370fe1ac48SPaul Mackerras 
538e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
539105988c0SPaul Mackerras 
540e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
541e360adbeSPeter Zijlstra #define clear_irq_work_pending()
542105988c0SPaul Mackerras 
543e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
544105988c0SPaul Mackerras 
5451b783955SPreeti U Murthy /*
5461b783955SPreeti U Murthy  * timer_interrupt - gets called when the decrementer overflows,
5471b783955SPreeti U Murthy  * with interrupts disabled.
5481b783955SPreeti U Murthy  */
5491b783955SPreeti U Murthy void timer_interrupt(struct pt_regs *regs)
5501b783955SPreeti U Murthy {
5513f984620SNicholas Piggin 	struct clock_event_device *evt = this_cpu_ptr(&decrementers);
55269111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
5533f984620SNicholas Piggin 	struct pt_regs *old_regs;
5543f984620SNicholas Piggin 	u64 now;
5551b783955SPreeti U Murthy 
5561b783955SPreeti U Murthy 	/* Some implementations of hotplug will get timer interrupts while
5571b783955SPreeti U Murthy 	 * offline, just ignore these and we also need to set
5581b783955SPreeti U Murthy 	 * decrementers_next_tb as MAX to make sure __check_irq_replay
5591b783955SPreeti U Murthy 	 * don't replay timer interrupt when return, otherwise we'll trap
5601b783955SPreeti U Murthy 	 * here infinitely :(
5611b783955SPreeti U Murthy 	 */
562a7cba02dSNicholas Piggin 	if (unlikely(!cpu_online(smp_processor_id()))) {
5631b783955SPreeti U Murthy 		*next_tb = ~(u64)0;
564a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
5651b783955SPreeti U Murthy 		return;
5661b783955SPreeti U Murthy 	}
5671b783955SPreeti U Murthy 
568a7cba02dSNicholas Piggin 	/* Ensure a positive value is written to the decrementer, or else
569a7cba02dSNicholas Piggin 	 * some CPUs will continue to take decrementer exceptions. When the
570a7cba02dSNicholas Piggin 	 * PPC_WATCHDOG (decrementer based) is configured, keep this at most
571a7cba02dSNicholas Piggin 	 * 31 bits, which is about 4 seconds on most systems, which gives
572a7cba02dSNicholas Piggin 	 * the watchdog a chance of catching timer interrupt hard lockups.
573a7cba02dSNicholas Piggin 	 */
574a7cba02dSNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
575a7cba02dSNicholas Piggin 		set_dec(0x7fffffff);
576a7cba02dSNicholas Piggin 	else
577a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
578a7cba02dSNicholas Piggin 
5791b783955SPreeti U Murthy 	/* Conditionally hard-enable interrupts now that the DEC has been
5801b783955SPreeti U Murthy 	 * bumped to its maximum value
5811b783955SPreeti U Murthy 	 */
5821b783955SPreeti U Murthy 	may_hard_irq_enable();
5831b783955SPreeti U Murthy 
5841b783955SPreeti U Murthy 
5856e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
5861b783955SPreeti U Murthy 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
5871b783955SPreeti U Murthy 		do_IRQ(regs);
5881b783955SPreeti U Murthy #endif
5891b783955SPreeti U Murthy 
5901b783955SPreeti U Murthy 	old_regs = set_irq_regs(regs);
5911b783955SPreeti U Murthy 	irq_enter();
5923f984620SNicholas Piggin 	trace_timer_interrupt_entry(regs);
5931b783955SPreeti U Murthy 
5943f984620SNicholas Piggin 	if (test_irq_work_pending()) {
5953f984620SNicholas Piggin 		clear_irq_work_pending();
5963f984620SNicholas Piggin 		irq_work_run();
5973f984620SNicholas Piggin 	}
5983f984620SNicholas Piggin 
5996601ec1cSChristophe Leroy 	now = get_tb();
6003f984620SNicholas Piggin 	if (now >= *next_tb) {
6013f984620SNicholas Piggin 		*next_tb = ~(u64)0;
6023f984620SNicholas Piggin 		if (evt->event_handler)
6033f984620SNicholas Piggin 			evt->event_handler(evt);
6043f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_event);
6053f984620SNicholas Piggin 	} else {
6063f984620SNicholas Piggin 		now = *next_tb - now;
6073f984620SNicholas Piggin 		if (now <= decrementer_max)
6083f984620SNicholas Piggin 			set_dec(now);
6093f984620SNicholas Piggin 		/* We may have raced with new irq work */
6103f984620SNicholas Piggin 		if (test_irq_work_pending())
6113f984620SNicholas Piggin 			set_dec(1);
6123f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_others);
6133f984620SNicholas Piggin 	}
6143f984620SNicholas Piggin 
6153f984620SNicholas Piggin 	trace_timer_interrupt_exit(regs);
616f2783c15SPaul Mackerras 	irq_exit();
6177d12e780SDavid Howells 	set_irq_regs(old_regs);
618f2783c15SPaul Mackerras }
6199445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt);
620f2783c15SPaul Mackerras 
621bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
6223f984620SNicholas Piggin void timer_broadcast_interrupt(void)
6233f984620SNicholas Piggin {
6243f984620SNicholas Piggin 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
6253f984620SNicholas Piggin 
6263f984620SNicholas Piggin 	*next_tb = ~(u64)0;
6273f984620SNicholas Piggin 	tick_receive_broadcast();
628e360cd37SNicholas Piggin 	__this_cpu_inc(irq_stat.broadcast_irqs_event);
6293f984620SNicholas Piggin }
630bc907113SNicholas Piggin #endif
6313f984620SNicholas Piggin 
6327ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
633d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void)
6347ac5dde9SScott Wood {
6357ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
6367ac5dde9SScott Wood 	 * with suspending.
6377ac5dde9SScott Wood 	 */
6387ac5dde9SScott Wood 
63979901024SOliver O'Halloran 	set_dec(decrementer_max);
6407ac5dde9SScott Wood 	local_irq_disable();
64179901024SOliver O'Halloran 	set_dec(decrementer_max);
6427ac5dde9SScott Wood }
6437ac5dde9SScott Wood 
644d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void)
6457ac5dde9SScott Wood {
6467ac5dde9SScott Wood 	local_irq_enable();
6477ac5dde9SScott Wood }
6487ac5dde9SScott Wood 
6497ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6507ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
6517ac5dde9SScott Wood {
6527ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
6537ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
6547ac5dde9SScott Wood 	generic_suspend_disable_irqs();
6557ac5dde9SScott Wood }
6567ac5dde9SScott Wood 
6577ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6587ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
6597ac5dde9SScott Wood {
6607ac5dde9SScott Wood 	generic_suspend_enable_irqs();
6617ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
6627ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
6637ac5dde9SScott Wood }
6647ac5dde9SScott Wood #endif
6657ac5dde9SScott Wood 
666b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks)
667b6c295dfSPaul Mackerras {
668b6c295dfSPaul Mackerras 	return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
669b6c295dfSPaul Mackerras }
670b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns);
671b6c295dfSPaul Mackerras 
672f2783c15SPaul Mackerras /*
673f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
674f2783c15SPaul Mackerras  *
675f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
676f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
677f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
678f2783c15SPaul Mackerras  */
6796b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void)
680f2783c15SPaul Mackerras {
681fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
682f2783c15SPaul Mackerras }
683f2783c15SPaul Mackerras 
6844be1b297SCyril Bur 
6854be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES
6864be1b297SCyril Bur 
6874be1b297SCyril Bur /*
6884be1b297SCyril Bur  * Running clock - attempts to give a view of time passing for a virtualised
6894be1b297SCyril Bur  * kernels.
6904be1b297SCyril Bur  * Uses the VTB register if available otherwise a next best guess.
6914be1b297SCyril Bur  */
6924be1b297SCyril Bur unsigned long long running_clock(void)
6934be1b297SCyril Bur {
6944be1b297SCyril Bur 	/*
6954be1b297SCyril Bur 	 * Don't read the VTB as a host since KVM does not switch in host
6964be1b297SCyril Bur 	 * timebase into the VTB when it takes a guest off the CPU, reading the
6974be1b297SCyril Bur 	 * VTB would result in reading 'last switched out' guest VTB.
6984be1b297SCyril Bur 	 *
6994be1b297SCyril Bur 	 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
7004be1b297SCyril Bur 	 * would be unsafe to rely only on the #ifdef above.
7014be1b297SCyril Bur 	 */
7024be1b297SCyril Bur 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
7034be1b297SCyril Bur 	    cpu_has_feature(CPU_FTR_ARCH_207S))
7044be1b297SCyril Bur 		return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
7054be1b297SCyril Bur 
7064be1b297SCyril Bur 	/*
7074be1b297SCyril Bur 	 * This is a next best approximation without a VTB.
7084be1b297SCyril Bur 	 * On a host which is running bare metal there should never be any stolen
7094be1b297SCyril Bur 	 * time and on a host which doesn't do any virtualisation TB *should* equal
7104be1b297SCyril Bur 	 * VTB so it makes no difference anyway.
7114be1b297SCyril Bur 	 */
7129f3768e0SFrédéric Weisbecker 	return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
7134be1b297SCyril Bur }
7144be1b297SCyril Bur #endif
7154be1b297SCyril Bur 
7160bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
717f2783c15SPaul Mackerras {
718f2783c15SPaul Mackerras 	struct device_node *cpu;
7196f7aba7bSAnton Blanchard 	const __be32 *fp;
7200bb474a4SAnton Blanchard 	int found = 0;
721f2783c15SPaul Mackerras 
7220bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
723f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
724f2783c15SPaul Mackerras 
725d8a8188dSOlaf Hering 	if (cpu) {
726e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
727d8a8188dSOlaf Hering 		if (fp) {
7280bb474a4SAnton Blanchard 			found = 1;
729a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
730f2783c15SPaul Mackerras 		}
7310bb474a4SAnton Blanchard 
7320bb474a4SAnton Blanchard 		of_node_put(cpu);
733f2783c15SPaul Mackerras 	}
7340bb474a4SAnton Blanchard 
7350bb474a4SAnton Blanchard 	return found;
7360bb474a4SAnton Blanchard }
7370bb474a4SAnton Blanchard 
738e51df2c1SAnton Blanchard static void start_cpu_decrementer(void)
73977c0a700SBenjamin Herrenschmidt {
74077c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
7416e2f03e2SIvan Mikhaylov 	unsigned int tcr;
7426e2f03e2SIvan Mikhaylov 
74377c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
74477c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
74577c0a700SBenjamin Herrenschmidt 
7466e2f03e2SIvan Mikhaylov 	tcr = mfspr(SPRN_TCR);
7476e2f03e2SIvan Mikhaylov 	/*
7486e2f03e2SIvan Mikhaylov 	 * The watchdog may have already been enabled by u-boot. So leave
7496e2f03e2SIvan Mikhaylov 	 * TRC[WP] (Watchdog Period) alone.
7506e2f03e2SIvan Mikhaylov 	 */
7516e2f03e2SIvan Mikhaylov 	tcr &= TCR_WP_MASK;	/* Clear all bits except for TCR[WP] */
7526e2f03e2SIvan Mikhaylov 	tcr |= TCR_DIE;		/* Enable decrementer */
7536e2f03e2SIvan Mikhaylov 	mtspr(SPRN_TCR, tcr);
7546e2f03e2SIvan Mikhaylov #endif
75577c0a700SBenjamin Herrenschmidt }
75677c0a700SBenjamin Herrenschmidt 
7570bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
7580bb474a4SAnton Blanchard {
7590bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
7600bb474a4SAnton Blanchard 
7610bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
7620bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
7630bb474a4SAnton Blanchard 
764f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
765f2783c15SPaul Mackerras 				"(not found)\n");
7660bb474a4SAnton Blanchard 	}
767f2783c15SPaul Mackerras 
7680bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
7690bb474a4SAnton Blanchard 
7700bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
7710bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
7720bb474a4SAnton Blanchard 
7730bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
7740bb474a4SAnton Blanchard 				"(not found)\n");
775f2783c15SPaul Mackerras 	}
776f2783c15SPaul Mackerras }
777f2783c15SPaul Mackerras 
7785235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now)
779f2783c15SPaul Mackerras {
780f2783c15SPaul Mackerras 	struct rtc_time tm;
781f2783c15SPaul Mackerras 
782aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
783023f333aSJason Gunthorpe 		return -ENODEV;
784aa3be5f3STony Breeds 
7855235afa8SArnd Bergmann 	rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
786aa3be5f3STony Breeds 
787aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
788aa3be5f3STony Breeds }
789aa3be5f3STony Breeds 
7905bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts)
791aa3be5f3STony Breeds {
792aa3be5f3STony Breeds 	struct rtc_time tm;
793aa3be5f3STony Breeds 	static int first = 1;
794aa3be5f3STony Breeds 
795d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
796aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
797aa3be5f3STony Breeds 	if (first) {
798aa3be5f3STony Breeds 		first = 0;
799aa3be5f3STony Breeds 		if (ppc_md.time_init)
800aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
801aa3be5f3STony Breeds 
802aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
803d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
804d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
805d90246cdSMartin Schwidefsky 			return;
806aa3be5f3STony Breeds 		}
807d90246cdSMartin Schwidefsky 	}
808d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
809d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
810d90246cdSMartin Schwidefsky 		return;
811d90246cdSMartin Schwidefsky 	}
812f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
813978d7eb3SBenjamin Herrenschmidt 
8145bfd6435SArnd Bergmann 	ts->tv_sec = rtc_tm_to_time64(&tm);
815f2783c15SPaul Mackerras }
816f2783c15SPaul Mackerras 
8175bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts)
818978d7eb3SBenjamin Herrenschmidt {
819978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
820978d7eb3SBenjamin Herrenschmidt 
821978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
822978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
823978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
824978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
825978d7eb3SBenjamin Herrenschmidt 	}
826978d7eb3SBenjamin Herrenschmidt 
827978d7eb3SBenjamin Herrenschmidt }
828978d7eb3SBenjamin Herrenschmidt 
8294a4cfe38STony Breeds /* clocksource code */
8306b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs)
8314a4cfe38STony Breeds {
832a5a1d1c2SThomas Gleixner 	return (u64)get_tb();
8334a4cfe38STony Breeds }
8344a4cfe38STony Breeds 
8351c21a293SMichael Ellerman static void __init clocksource_init(void)
8364a4cfe38STony Breeds {
837a4c5a355SChristophe Leroy 	struct clocksource *clock = &clocksource_timebase;
8384a4cfe38STony Breeds 
83911b8633aSAnton Blanchard 	if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
8404a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
8414a4cfe38STony Breeds 		       clock->name);
8424a4cfe38STony Breeds 		return;
8434a4cfe38STony Breeds 	}
8444a4cfe38STony Breeds 
8454a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
8464a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
8474a4cfe38STony Breeds }
8484a4cfe38STony Breeds 
849d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
850d831d0b8STony Breeds 				      struct clock_event_device *dev)
851d831d0b8STony Breeds {
8526601ec1cSChristophe Leroy 	__this_cpu_write(decrementers_next_tb, get_tb() + evt);
853d831d0b8STony Breeds 	set_dec(evt);
8540215f7d8SBenjamin Herrenschmidt 
8550215f7d8SBenjamin Herrenschmidt 	/* We may have raced with new irq work */
8560215f7d8SBenjamin Herrenschmidt 	if (test_irq_work_pending())
8570215f7d8SBenjamin Herrenschmidt 		set_dec(1);
8580215f7d8SBenjamin Herrenschmidt 
859d831d0b8STony Breeds 	return 0;
860d831d0b8STony Breeds }
861d831d0b8STony Breeds 
86237a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev)
863d831d0b8STony Breeds {
86479901024SOliver O'Halloran 	decrementer_set_next_event(decrementer_max, dev);
86537a13e78SViresh Kumar 	return 0;
866d831d0b8STony Breeds }
867d831d0b8STony Breeds 
868d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
869d831d0b8STony Breeds {
8707df10275SAnton Blanchard 	struct clock_event_device *dec = &per_cpu(decrementers, cpu);
871d831d0b8STony Breeds 
872d831d0b8STony Breeds 	*dec = decrementer_clockevent;
873320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
874d831d0b8STony Breeds 
8758b78fdb0SAnton Blanchard 	clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
8768b78fdb0SAnton Blanchard 
877b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
878d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
879b4d16ab5SMichael Ellerman 
880b4d16ab5SMichael Ellerman 	/* Set values for KVM, see kvm_emulate_dec() */
881b4d16ab5SMichael Ellerman 	decrementer_clockevent.mult = dec->mult;
882b4d16ab5SMichael Ellerman 	decrementer_clockevent.shift = dec->shift;
883d831d0b8STony Breeds }
884d831d0b8STony Breeds 
88579901024SOliver O'Halloran static void enable_large_decrementer(void)
88679901024SOliver O'Halloran {
88779901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
88879901024SOliver O'Halloran 		return;
88979901024SOliver O'Halloran 
89079901024SOliver O'Halloran 	if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
89179901024SOliver O'Halloran 		return;
89279901024SOliver O'Halloran 
89379901024SOliver O'Halloran 	/*
89479901024SOliver O'Halloran 	 * If we're running as the hypervisor we need to enable the LD manually
89579901024SOliver O'Halloran 	 * otherwise firmware should have done it for us.
89679901024SOliver O'Halloran 	 */
89779901024SOliver O'Halloran 	if (cpu_has_feature(CPU_FTR_HVMODE))
89879901024SOliver O'Halloran 		mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
89979901024SOliver O'Halloran }
90079901024SOliver O'Halloran 
90179901024SOliver O'Halloran static void __init set_decrementer_max(void)
90279901024SOliver O'Halloran {
90379901024SOliver O'Halloran 	struct device_node *cpu;
90479901024SOliver O'Halloran 	u32 bits = 32;
90579901024SOliver O'Halloran 
90679901024SOliver O'Halloran 	/* Prior to ISAv3 the decrementer is always 32 bit */
90779901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
90879901024SOliver O'Halloran 		return;
90979901024SOliver O'Halloran 
91079901024SOliver O'Halloran 	cpu = of_find_node_by_type(NULL, "cpu");
91179901024SOliver O'Halloran 
91279901024SOliver O'Halloran 	if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
91379901024SOliver O'Halloran 		if (bits > 64 || bits < 32) {
91479901024SOliver O'Halloran 			pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
91579901024SOliver O'Halloran 			bits = 32;
91679901024SOliver O'Halloran 		}
91779901024SOliver O'Halloran 
91879901024SOliver O'Halloran 		/* calculate the signed maximum given this many bits */
91979901024SOliver O'Halloran 		decrementer_max = (1ul << (bits - 1)) - 1;
92079901024SOliver O'Halloran 	}
92179901024SOliver O'Halloran 
92279901024SOliver O'Halloran 	of_node_put(cpu);
92379901024SOliver O'Halloran 
92479901024SOliver O'Halloran 	pr_info("time_init: %u bit decrementer (max: %llx)\n",
92579901024SOliver O'Halloran 		bits, decrementer_max);
92679901024SOliver O'Halloran }
92779901024SOliver O'Halloran 
928c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
929d831d0b8STony Breeds {
9308b78fdb0SAnton Blanchard 	register_decrementer_clockevent(smp_processor_id());
931d831d0b8STony Breeds }
932d831d0b8STony Breeds 
933d831d0b8STony Breeds void secondary_cpu_time_init(void)
934d831d0b8STony Breeds {
93579901024SOliver O'Halloran 	/* Enable and test the large decrementer for this cpu */
93679901024SOliver O'Halloran 	enable_large_decrementer();
93779901024SOliver O'Halloran 
93877c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
93977c0a700SBenjamin Herrenschmidt 	 * such as BookE
94077c0a700SBenjamin Herrenschmidt 	 */
94177c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
94277c0a700SBenjamin Herrenschmidt 
943d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
944d831d0b8STony Breeds 	 * call here ! */
945d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
946d831d0b8STony Breeds }
947d831d0b8STony Breeds 
948f2783c15SPaul Mackerras /* This function is only called on the boot processor */
949f2783c15SPaul Mackerras void __init time_init(void)
950f2783c15SPaul Mackerras {
951f2783c15SPaul Mackerras 	struct div_result res;
952d75d68cfSPaul Mackerras 	u64 scale;
953f2783c15SPaul Mackerras 	unsigned shift;
954f2783c15SPaul Mackerras 
95596c44507SPaul Mackerras 	/* Normal PowerPC with timebase register */
956f2783c15SPaul Mackerras 	ppc_md.calibrate_decr();
957224ad80aSOlof Johansson 	printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
958374e99d4SPaul Mackerras 	       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
959224ad80aSOlof Johansson 	printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
960374e99d4SPaul Mackerras 	       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
961374e99d4SPaul Mackerras 
962374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
963092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
964374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
965c6622f63SPaul Mackerras 	calc_cputime_factors();
966092b8f34SPaul Mackerras 
967092b8f34SPaul Mackerras 	/*
968f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
969f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
970f2783c15SPaul Mackerras 	 * which is the timebase frequency.
971f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
972f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
973f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
974f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
975f2783c15SPaul Mackerras 	 * sched_clock().
976f2783c15SPaul Mackerras 	 */
977f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
978f2783c15SPaul Mackerras 	scale = res.result_low;
979f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
980f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
981f2783c15SPaul Mackerras 		res.result_high >>= 1;
982f2783c15SPaul Mackerras 	}
983f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
984f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
985fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
9866601ec1cSChristophe Leroy 	boot_tb = get_tb();
987f2783c15SPaul Mackerras 
988092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
989092b8f34SPaul Mackerras 	if (timezone_offset) {
990092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
991092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
992092b8f34SPaul Mackerras 	}
993092b8f34SPaul Mackerras 
994a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
995f2783c15SPaul Mackerras 
99679901024SOliver O'Halloran 	/* initialise and enable the large decrementer (if we have one) */
99779901024SOliver O'Halloran 	set_decrementer_max();
99879901024SOliver O'Halloran 	enable_large_decrementer();
99979901024SOliver O'Halloran 
100077c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
100177c0a700SBenjamin Herrenschmidt 	 * such as BookE
100277c0a700SBenjamin Herrenschmidt 	 */
100377c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
100477c0a700SBenjamin Herrenschmidt 
1005f5339277SStephen Rothwell 	/* Register the clocksource */
10064a4cfe38STony Breeds 	clocksource_init();
10074a4cfe38STony Breeds 
1008d831d0b8STony Breeds 	init_decrementer_clockevent();
10090d948730SPreeti U Murthy 	tick_setup_hrtimer_broadcast();
1010f0d37300SKevin Hao 
1011f0d37300SKevin Hao 	of_clk_init(NULL);
1012f2783c15SPaul Mackerras }
1013f2783c15SPaul Mackerras 
1014f2783c15SPaul Mackerras /*
1015f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1016f2783c15SPaul Mackerras  * result.
1017f2783c15SPaul Mackerras  */
1018f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1019f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1020f2783c15SPaul Mackerras {
1021f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1022f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1023f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1024f2783c15SPaul Mackerras 
1025f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1026f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1027f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1028f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1029f2783c15SPaul Mackerras 
1030f2783c15SPaul Mackerras 	w = a / divisor;
1031f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1032f2783c15SPaul Mackerras 
1033f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1034f2783c15SPaul Mackerras 	x = ra;
1035f2783c15SPaul Mackerras 
1036f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1037f2783c15SPaul Mackerras 	y = rb;
1038f2783c15SPaul Mackerras 
1039f2783c15SPaul Mackerras 	do_div(rc, divisor);
1040f2783c15SPaul Mackerras 	z = rc;
1041f2783c15SPaul Mackerras 
1042f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1043f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1044f2783c15SPaul Mackerras 
1045f2783c15SPaul Mackerras }
1046bcd68a70SGeert Uytterhoeven 
1047177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
1048177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
1049177996e6SBenjamin Herrenschmidt {
1050177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
1051177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
1052177996e6SBenjamin Herrenschmidt 	 */
1053177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
1054177996e6SBenjamin Herrenschmidt }
1055177996e6SBenjamin Herrenschmidt 
1056169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1057169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1058169047f4SArnd Bergmann {
1059169047f4SArnd Bergmann 	ppc_md.get_rtc_time(tm);
1060890ae797SAlexandre Belloni 	return 0;
1061169047f4SArnd Bergmann }
1062169047f4SArnd Bergmann 
1063169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1064169047f4SArnd Bergmann {
1065169047f4SArnd Bergmann 	if (!ppc_md.set_rtc_time)
1066169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1067169047f4SArnd Bergmann 
1068169047f4SArnd Bergmann 	if (ppc_md.set_rtc_time(tm) < 0)
1069169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1070169047f4SArnd Bergmann 
1071169047f4SArnd Bergmann 	return 0;
1072169047f4SArnd Bergmann }
1073169047f4SArnd Bergmann 
1074169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = {
1075169047f4SArnd Bergmann 	.read_time = rtc_generic_get_time,
1076169047f4SArnd Bergmann 	.set_time = rtc_generic_set_time,
1077169047f4SArnd Bergmann };
1078169047f4SArnd Bergmann 
1079bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1080bcd68a70SGeert Uytterhoeven {
1081bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1082bcd68a70SGeert Uytterhoeven 
1083bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1084bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1085bcd68a70SGeert Uytterhoeven 
1086169047f4SArnd Bergmann 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1087169047f4SArnd Bergmann 					     &rtc_generic_ops,
1088169047f4SArnd Bergmann 					     sizeof(rtc_generic_ops));
1089bcd68a70SGeert Uytterhoeven 
10908c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(pdev);
1091bcd68a70SGeert Uytterhoeven }
1092bcd68a70SGeert Uytterhoeven 
10938f6b9512SPaul Gortmaker device_initcall(rtc_init);
1094169047f4SArnd Bergmann #endif
1095