xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision de269129a48a2d590ba1d20c719e19d86e3ddb3f)
1f2783c15SPaul Mackerras /*
2f2783c15SPaul Mackerras  * Common time routines among all ppc machines.
3f2783c15SPaul Mackerras  *
4f2783c15SPaul Mackerras  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5f2783c15SPaul Mackerras  * Paul Mackerras' version and mine for PReP and Pmac.
6f2783c15SPaul Mackerras  * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
7f2783c15SPaul Mackerras  * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
8f2783c15SPaul Mackerras  *
9f2783c15SPaul Mackerras  * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
10f2783c15SPaul Mackerras  * to make clock more stable (2.4.0-test5). The only thing
11f2783c15SPaul Mackerras  * that this code assumes is that the timebases have been synchronized
12f2783c15SPaul Mackerras  * by firmware on SMP and are never stopped (never do sleep
13f2783c15SPaul Mackerras  * on SMP then, nap and doze are OK).
14f2783c15SPaul Mackerras  *
15f2783c15SPaul Mackerras  * Speeded up do_gettimeofday by getting rid of references to
16f2783c15SPaul Mackerras  * xtime (which required locks for consistency). (mikejc@us.ibm.com)
17f2783c15SPaul Mackerras  *
18f2783c15SPaul Mackerras  * TODO (not necessarily in this file):
19f2783c15SPaul Mackerras  * - improve precision and reproducibility of timebase frequency
20f5339277SStephen Rothwell  * measurement at boot time.
21f2783c15SPaul Mackerras  * - for astronomical applications: add a new function to get
22f2783c15SPaul Mackerras  * non ambiguous timestamps even around leap seconds. This needs
23f2783c15SPaul Mackerras  * a new timestamp format and a good name.
24f2783c15SPaul Mackerras  *
25f2783c15SPaul Mackerras  * 1997-09-10  Updated NTP code according to technical memorandum Jan '96
26f2783c15SPaul Mackerras  *             "A Kernel Model for Precision Timekeeping" by Dave Mills
27f2783c15SPaul Mackerras  *
28f2783c15SPaul Mackerras  *      This program is free software; you can redistribute it and/or
29f2783c15SPaul Mackerras  *      modify it under the terms of the GNU General Public License
30f2783c15SPaul Mackerras  *      as published by the Free Software Foundation; either version
31f2783c15SPaul Mackerras  *      2 of the License, or (at your option) any later version.
32f2783c15SPaul Mackerras  */
33f2783c15SPaul Mackerras 
34f2783c15SPaul Mackerras #include <linux/errno.h>
354b16f8e2SPaul Gortmaker #include <linux/export.h>
36f2783c15SPaul Mackerras #include <linux/sched.h>
37e6017571SIngo Molnar #include <linux/sched/clock.h>
38f2783c15SPaul Mackerras #include <linux/kernel.h>
39f2783c15SPaul Mackerras #include <linux/param.h>
40f2783c15SPaul Mackerras #include <linux/string.h>
41f2783c15SPaul Mackerras #include <linux/mm.h>
42f2783c15SPaul Mackerras #include <linux/interrupt.h>
43f2783c15SPaul Mackerras #include <linux/timex.h>
44f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
45f2783c15SPaul Mackerras #include <linux/time.h>
46f2783c15SPaul Mackerras #include <linux/init.h>
47f2783c15SPaul Mackerras #include <linux/profile.h>
48f2783c15SPaul Mackerras #include <linux/cpu.h>
49f2783c15SPaul Mackerras #include <linux/security.h>
50f2783c15SPaul Mackerras #include <linux/percpu.h>
51f2783c15SPaul Mackerras #include <linux/rtc.h>
52092b8f34SPaul Mackerras #include <linux/jiffies.h>
53c6622f63SPaul Mackerras #include <linux/posix-timers.h>
547d12e780SDavid Howells #include <linux/irq.h>
55177996e6SBenjamin Herrenschmidt #include <linux/delay.h>
56e360adbeSPeter Zijlstra #include <linux/irq_work.h>
57f0d37300SKevin Hao #include <linux/clk-provider.h>
587f92bc56SDaniel Axtens #include <linux/suspend.h>
5932ef5517SIngo Molnar #include <linux/sched/cputime.h>
604e287e65SNicholas Piggin #include <linux/processor.h>
616795b85cSAnton Blanchard #include <asm/trace.h>
62f2783c15SPaul Mackerras 
63f2783c15SPaul Mackerras #include <asm/io.h>
64f2783c15SPaul Mackerras #include <asm/nvram.h>
65f2783c15SPaul Mackerras #include <asm/cache.h>
66f2783c15SPaul Mackerras #include <asm/machdep.h>
677c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
68f2783c15SPaul Mackerras #include <asm/time.h>
69f2783c15SPaul Mackerras #include <asm/prom.h>
70f2783c15SPaul Mackerras #include <asm/irq.h>
71f2783c15SPaul Mackerras #include <asm/div64.h>
722249ca9dSPaul Mackerras #include <asm/smp.h>
73a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
74f2783c15SPaul Mackerras #include <asm/firmware.h>
750545d543SDaniel Axtens #include <asm/asm-prototypes.h>
76f2783c15SPaul Mackerras 
774a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
784a4cfe38STony Breeds 
79d831d0b8STony Breeds #include <linux/clockchips.h>
80189374aeSJohn Stultz #include <linux/timekeeper_internal.h>
814a4cfe38STony Breeds 
82a5a1d1c2SThomas Gleixner static u64 rtc_read(struct clocksource *);
834a4cfe38STony Breeds static struct clocksource clocksource_rtc = {
844a4cfe38STony Breeds 	.name         = "rtc",
854a4cfe38STony Breeds 	.rating       = 400,
864a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
874a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
884a4cfe38STony Breeds 	.read         = rtc_read,
894a4cfe38STony Breeds };
904a4cfe38STony Breeds 
91a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *);
924a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
934a4cfe38STony Breeds 	.name         = "timebase",
944a4cfe38STony Breeds 	.rating       = 400,
954a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
964a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
974a4cfe38STony Breeds 	.read         = timebase_read,
984a4cfe38STony Breeds };
994a4cfe38STony Breeds 
10079901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
10179901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
102d831d0b8STony Breeds 
103d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
104d831d0b8STony Breeds 				      struct clock_event_device *dev);
10537a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt);
106d831d0b8STony Breeds 
1076e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = {
108d831d0b8STony Breeds 	.name			= "decrementer",
109d831d0b8STony Breeds 	.rating			= 200,
110d831d0b8STony Breeds 	.irq			= 0,
111d831d0b8STony Breeds 	.set_next_event		= decrementer_set_next_event,
11281759360SAnton Blanchard 	.set_state_oneshot_stopped = decrementer_shutdown,
11337a13e78SViresh Kumar 	.set_state_shutdown	= decrementer_shutdown,
11437a13e78SViresh Kumar 	.tick_resume		= decrementer_shutdown,
11537a13e78SViresh Kumar 	.features		= CLOCK_EVT_FEAT_ONESHOT |
11637a13e78SViresh Kumar 				  CLOCK_EVT_FEAT_C3STOP,
117d831d0b8STony Breeds };
1186e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent);
119d831d0b8STony Breeds 
1207df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb);
1217df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers);
122d831d0b8STony Breeds 
123f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
124f2783c15SPaul Mackerras 
125f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
126f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
127f2783c15SPaul Mackerras #else
128f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
129f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
130f2783c15SPaul Mackerras #endif
131f2783c15SPaul Mackerras 
132f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
133f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
134f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
135f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
1362cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime_t conversions */
137092b8f34SPaul Mackerras 
138f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
139f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
140f2783c15SPaul Mackerras 
141fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
142fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
143364a1246SHeiko Schocher static u64 boot_tb __read_mostly;
144f2783c15SPaul Mackerras 
145f2783c15SPaul Mackerras extern struct timezone sys_tz;
146f2783c15SPaul Mackerras static long timezone_offset;
147f2783c15SPaul Mackerras 
148f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
14955ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq);
150f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
15155ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq);
15296c44507SPaul Mackerras 
153*de269129SMahesh Salgaonkar bool tb_invalid;
154*de269129SMahesh Salgaonkar 
155abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
156c6622f63SPaul Mackerras /*
157e7f340caSFrederic Weisbecker  * Factor for converting from cputime_t (timebase ticks) to
158e7f340caSFrederic Weisbecker  * microseconds. This is stored as 0.64 fixed-point binary fraction.
159c6622f63SPaul Mackerras  */
1609f5072d4SAndreas Schwab u64 __cputime_usec_factor;
1619f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor);
162a42548a1SStanislaw Gruszka 
163c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR
164872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64);
165c223c903SChristophe Leroy #endif
166c223c903SChristophe Leroy 
167c6622f63SPaul Mackerras static void calc_cputime_factors(void)
168c6622f63SPaul Mackerras {
169c6622f63SPaul Mackerras 	struct div_result res;
170c6622f63SPaul Mackerras 
1719f5072d4SAndreas Schwab 	div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
1729f5072d4SAndreas Schwab 	__cputime_usec_factor = res.result_low;
173c6622f63SPaul Mackerras }
174c6622f63SPaul Mackerras 
175c6622f63SPaul Mackerras /*
176cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
177cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
178c6622f63SPaul Mackerras  */
179abcff86dSChristophe Leroy static inline unsigned long read_spurr(unsigned long tb)
180c6622f63SPaul Mackerras {
181cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
182cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
183c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
184c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
185cf9efce0SPaul Mackerras 	return tb;
186cf9efce0SPaul Mackerras }
187cf9efce0SPaul Mackerras 
188cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR
189cf9efce0SPaul Mackerras 
190cf9efce0SPaul Mackerras /*
191cf9efce0SPaul Mackerras  * Scan the dispatch trace log and count up the stolen time.
192cf9efce0SPaul Mackerras  * Should be called with interrupts disabled.
193cf9efce0SPaul Mackerras  */
194cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb)
195cf9efce0SPaul Mackerras {
196872e439aSPaul Mackerras 	u64 i = local_paca->dtl_ridx;
197cf9efce0SPaul Mackerras 	struct dtl_entry *dtl = local_paca->dtl_curr;
198cf9efce0SPaul Mackerras 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
199cf9efce0SPaul Mackerras 	struct lppaca *vpa = local_paca->lppaca_ptr;
200cf9efce0SPaul Mackerras 	u64 tb_delta;
201cf9efce0SPaul Mackerras 	u64 stolen = 0;
202cf9efce0SPaul Mackerras 	u64 dtb;
203cf9efce0SPaul Mackerras 
20484ffae55SAnton Blanchard 	if (!dtl)
20584ffae55SAnton Blanchard 		return 0;
20684ffae55SAnton Blanchard 
2077ffcf8ecSAnton Blanchard 	if (i == be64_to_cpu(vpa->dtl_idx))
208cf9efce0SPaul Mackerras 		return 0;
2097ffcf8ecSAnton Blanchard 	while (i < be64_to_cpu(vpa->dtl_idx)) {
2107ffcf8ecSAnton Blanchard 		dtb = be64_to_cpu(dtl->timebase);
2117ffcf8ecSAnton Blanchard 		tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
2127ffcf8ecSAnton Blanchard 			be32_to_cpu(dtl->ready_to_enqueue_time);
213cf9efce0SPaul Mackerras 		barrier();
2147ffcf8ecSAnton Blanchard 		if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
215cf9efce0SPaul Mackerras 			/* buffer has overflowed */
2167ffcf8ecSAnton Blanchard 			i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
217cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
218cf9efce0SPaul Mackerras 			continue;
219cf9efce0SPaul Mackerras 		}
220cf9efce0SPaul Mackerras 		if (dtb > stop_tb)
221cf9efce0SPaul Mackerras 			break;
22284b07386SAnton Blanchard 		if (dtl_consumer)
22384b07386SAnton Blanchard 			dtl_consumer(dtl, i);
224cf9efce0SPaul Mackerras 		stolen += tb_delta;
225cf9efce0SPaul Mackerras 		++i;
226cf9efce0SPaul Mackerras 		++dtl;
227cf9efce0SPaul Mackerras 		if (dtl == dtl_end)
228cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log;
229cf9efce0SPaul Mackerras 	}
230cf9efce0SPaul Mackerras 	local_paca->dtl_ridx = i;
231cf9efce0SPaul Mackerras 	local_paca->dtl_curr = dtl;
232cf9efce0SPaul Mackerras 	return stolen;
233c6622f63SPaul Mackerras }
234c6622f63SPaul Mackerras 
235c6622f63SPaul Mackerras /*
236cf9efce0SPaul Mackerras  * Accumulate stolen time by scanning the dispatch trace log.
237cf9efce0SPaul Mackerras  * Called on entry from user mode.
2384603ac18SMichael Neuling  */
239cf9efce0SPaul Mackerras void accumulate_stolen_time(void)
2404603ac18SMichael Neuling {
241cf9efce0SPaul Mackerras 	u64 sst, ust;
2424e26bc4aSMadhavan Srinivasan 	unsigned long save_irq_soft_mask = irq_soft_mask_return();
243c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = &local_paca->accounting;
244b18ae08dSTejun Heo 
245b18ae08dSTejun Heo 	/* We are called early in the exception entry, before
246b18ae08dSTejun Heo 	 * soft/hard_enabled are sync'ed to the expected state
247b18ae08dSTejun Heo 	 * for the exception. We are hard disabled but the PACA
248b18ae08dSTejun Heo 	 * needs to reflect that so various debug stuff doesn't
249b18ae08dSTejun Heo 	 * complain
250b18ae08dSTejun Heo 	 */
2514e26bc4aSMadhavan Srinivasan 	irq_soft_mask_set(IRQS_DISABLED);
252b18ae08dSTejun Heo 
253c223c903SChristophe Leroy 	sst = scan_dispatch_log(acct->starttime_user);
254c223c903SChristophe Leroy 	ust = scan_dispatch_log(acct->starttime);
2558c8b73c4SFrederic Weisbecker 	acct->stime -= sst;
2568c8b73c4SFrederic Weisbecker 	acct->utime -= ust;
257f828c3d0SFrederic Weisbecker 	acct->steal_time += ust + sst;
258b18ae08dSTejun Heo 
2594e26bc4aSMadhavan Srinivasan 	irq_soft_mask_set(save_irq_soft_mask);
2604603ac18SMichael Neuling }
2614603ac18SMichael Neuling 
262cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
263cf9efce0SPaul Mackerras {
264a6201da3SAneesh Kumar K.V 	if (!firmware_has_feature(FW_FEATURE_SPLPAR))
265a6201da3SAneesh Kumar K.V 		return 0;
266a6201da3SAneesh Kumar K.V 
267a19ff1a2SFrederic Weisbecker 	if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
268a19ff1a2SFrederic Weisbecker 		return scan_dispatch_log(stop_tb);
269cf9efce0SPaul Mackerras 
270a19ff1a2SFrederic Weisbecker 	return 0;
271cf9efce0SPaul Mackerras }
272cf9efce0SPaul Mackerras 
273cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */
274cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
275cf9efce0SPaul Mackerras {
276cf9efce0SPaul Mackerras 	return 0;
277cf9efce0SPaul Mackerras }
278cf9efce0SPaul Mackerras 
279cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
280cf9efce0SPaul Mackerras 
2814603ac18SMichael Neuling /*
282c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
283c6622f63SPaul Mackerras  * or soft irq state.
284c6622f63SPaul Mackerras  */
285b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
286b38a181cSChristophe Leroy 					unsigned long now, unsigned long stime)
287c6622f63SPaul Mackerras {
288abcff86dSChristophe Leroy 	unsigned long stime_scaled = 0;
289abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
290b38a181cSChristophe Leroy 	unsigned long nowscaled, deltascaled;
291a19ff1a2SFrederic Weisbecker 	unsigned long utime, utime_scaled;
292c6622f63SPaul Mackerras 
2934603ac18SMichael Neuling 	nowscaled = read_spurr(now);
294c223c903SChristophe Leroy 	deltascaled = nowscaled - acct->startspurr;
295c223c903SChristophe Leroy 	acct->startspurr = nowscaled;
296a19ff1a2SFrederic Weisbecker 	utime = acct->utime - acct->utime_sspurr;
2978c8b73c4SFrederic Weisbecker 	acct->utime_sspurr = acct->utime;
298cf9efce0SPaul Mackerras 
299cf9efce0SPaul Mackerras 	/*
300cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
301cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
302cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
303cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
304cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
305cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
306cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
307cf9efce0SPaul Mackerras 	 * used by account_process_tick.
308cf9efce0SPaul Mackerras 	 */
309b38a181cSChristophe Leroy 	stime_scaled = stime;
310a19ff1a2SFrederic Weisbecker 	utime_scaled = utime;
311a19ff1a2SFrederic Weisbecker 	if (deltascaled != stime + utime) {
312a19ff1a2SFrederic Weisbecker 		if (utime) {
313b38a181cSChristophe Leroy 			stime_scaled = deltascaled * stime / (stime + utime);
314b38a181cSChristophe Leroy 			utime_scaled = deltascaled - stime_scaled;
315cf9efce0SPaul Mackerras 		} else {
316b38a181cSChristophe Leroy 			stime_scaled = deltascaled;
317c6622f63SPaul Mackerras 		}
318cf9efce0SPaul Mackerras 	}
319a19ff1a2SFrederic Weisbecker 	acct->utime_scaled += utime_scaled;
320abcff86dSChristophe Leroy #endif
321cf9efce0SPaul Mackerras 
322b38a181cSChristophe Leroy 	return stime_scaled;
323b38a181cSChristophe Leroy }
324b38a181cSChristophe Leroy 
325b38a181cSChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk,
326b38a181cSChristophe Leroy 				 unsigned long *stime_scaled,
327b38a181cSChristophe Leroy 				 unsigned long *steal_time)
328b38a181cSChristophe Leroy {
329b38a181cSChristophe Leroy 	unsigned long now, stime;
330b38a181cSChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
331b38a181cSChristophe Leroy 
332b38a181cSChristophe Leroy 	WARN_ON_ONCE(!irqs_disabled());
333b38a181cSChristophe Leroy 
334b38a181cSChristophe Leroy 	now = mftb();
335b38a181cSChristophe Leroy 	stime = now - acct->starttime;
336b38a181cSChristophe Leroy 	acct->starttime = now;
337b38a181cSChristophe Leroy 
338b38a181cSChristophe Leroy 	*stime_scaled = vtime_delta_scaled(acct, now, stime);
339b38a181cSChristophe Leroy 
340b38a181cSChristophe Leroy 	*steal_time = calculate_stolen_time(now);
341b38a181cSChristophe Leroy 
342a19ff1a2SFrederic Weisbecker 	return stime;
343a7e1a9e3SFrederic Weisbecker }
344a7e1a9e3SFrederic Weisbecker 
345fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk)
346a7e1a9e3SFrederic Weisbecker {
347a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
348a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
349a7e1a9e3SFrederic Weisbecker 
350a19ff1a2SFrederic Weisbecker 	stime = vtime_delta(tsk, &stime_scaled, &steal_time);
351a19ff1a2SFrederic Weisbecker 
352a19ff1a2SFrederic Weisbecker 	stime -= min(stime, steal_time);
353a19ff1a2SFrederic Weisbecker 	acct->steal_time += steal_time;
354a19ff1a2SFrederic Weisbecker 
355a19ff1a2SFrederic Weisbecker 	if ((tsk->flags & PF_VCPU) && !irq_count()) {
356a19ff1a2SFrederic Weisbecker 		acct->gtime += stime;
357abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
358a19ff1a2SFrederic Weisbecker 		acct->utime_scaled += stime_scaled;
359abcff86dSChristophe Leroy #endif
360a19ff1a2SFrederic Weisbecker 	} else {
361a19ff1a2SFrederic Weisbecker 		if (hardirq_count())
362a19ff1a2SFrederic Weisbecker 			acct->hardirq_time += stime;
363a19ff1a2SFrederic Weisbecker 		else if (in_serving_softirq())
364a19ff1a2SFrederic Weisbecker 			acct->softirq_time += stime;
365a19ff1a2SFrederic Weisbecker 		else
366a19ff1a2SFrederic Weisbecker 			acct->stime += stime;
367a19ff1a2SFrederic Weisbecker 
368abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
369a19ff1a2SFrederic Weisbecker 		acct->stime_scaled += stime_scaled;
370abcff86dSChristophe Leroy #endif
371a19ff1a2SFrederic Weisbecker 	}
372a7e1a9e3SFrederic Weisbecker }
373c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system);
374a7e1a9e3SFrederic Weisbecker 
375fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk)
376a7e1a9e3SFrederic Weisbecker {
377a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
378a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
379a7e1a9e3SFrederic Weisbecker 
380a19ff1a2SFrederic Weisbecker 	stime = vtime_delta(tsk, &stime_scaled, &steal_time);
381a19ff1a2SFrederic Weisbecker 	acct->idle_time += stime + steal_time;
382cf9efce0SPaul Mackerras }
383c6622f63SPaul Mackerras 
384b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk,
385b38a181cSChristophe Leroy 			       struct cpu_accounting_data *acct)
386b38a181cSChristophe Leroy {
387abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
388b38a181cSChristophe Leroy 	if (acct->utime_scaled)
389b38a181cSChristophe Leroy 		tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
390b38a181cSChristophe Leroy 	if (acct->stime_scaled)
391b38a181cSChristophe Leroy 		tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
392b38a181cSChristophe Leroy 
393b38a181cSChristophe Leroy 	acct->utime_scaled = 0;
394b38a181cSChristophe Leroy 	acct->utime_sspurr = 0;
395b38a181cSChristophe Leroy 	acct->stime_scaled = 0;
396abcff86dSChristophe Leroy #endif
397b38a181cSChristophe Leroy }
398b38a181cSChristophe Leroy 
399c6622f63SPaul Mackerras /*
400c8d7dabfSFrederic Weisbecker  * Account the whole cputime accumulated in the paca
401c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
402bcebdf84SFrederic Weisbecker  * Assumes that vtime_account_system/idle() has been called
403bcebdf84SFrederic Weisbecker  * recently (i.e. since the last entry from usermode) so that
404cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
405c6622f63SPaul Mackerras  */
406c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk)
407c6622f63SPaul Mackerras {
408c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
409c6622f63SPaul Mackerras 
410a19ff1a2SFrederic Weisbecker 	if (acct->utime)
41123244a5cSFrederic Weisbecker 		account_user_time(tsk, cputime_to_nsecs(acct->utime));
412a19ff1a2SFrederic Weisbecker 
413a19ff1a2SFrederic Weisbecker 	if (acct->gtime)
414fb8b049cSFrederic Weisbecker 		account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
415a19ff1a2SFrederic Weisbecker 
41651eeef9eSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
417be9095edSFrederic Weisbecker 		account_steal_time(cputime_to_nsecs(acct->steal_time));
41851eeef9eSChristophe Leroy 		acct->steal_time = 0;
41951eeef9eSChristophe Leroy 	}
420a19ff1a2SFrederic Weisbecker 
421a19ff1a2SFrederic Weisbecker 	if (acct->idle_time)
42218b43a9bSFrederic Weisbecker 		account_idle_time(cputime_to_nsecs(acct->idle_time));
423a19ff1a2SFrederic Weisbecker 
424a19ff1a2SFrederic Weisbecker 	if (acct->stime)
425fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
426fb8b049cSFrederic Weisbecker 					  CPUTIME_SYSTEM);
427a19ff1a2SFrederic Weisbecker 
428a19ff1a2SFrederic Weisbecker 	if (acct->hardirq_time)
429fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
430fb8b049cSFrederic Weisbecker 					  CPUTIME_IRQ);
431a19ff1a2SFrederic Weisbecker 	if (acct->softirq_time)
432fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
433fb8b049cSFrederic Weisbecker 					  CPUTIME_SOFTIRQ);
434a19ff1a2SFrederic Weisbecker 
435b38a181cSChristophe Leroy 	vtime_flush_scaled(tsk, acct);
436b38a181cSChristophe Leroy 
4378c8b73c4SFrederic Weisbecker 	acct->utime = 0;
438a19ff1a2SFrederic Weisbecker 	acct->gtime = 0;
439a19ff1a2SFrederic Weisbecker 	acct->idle_time = 0;
440a19ff1a2SFrederic Weisbecker 	acct->stime = 0;
441a19ff1a2SFrederic Weisbecker 	acct->hardirq_time = 0;
442a19ff1a2SFrederic Weisbecker 	acct->softirq_time = 0;
443c6622f63SPaul Mackerras }
444c6622f63SPaul Mackerras 
445abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
446c6622f63SPaul Mackerras #define calc_cputime_factors()
447c6622f63SPaul Mackerras #endif
448c6622f63SPaul Mackerras 
4496defa38bSPaul Mackerras void __delay(unsigned long loops)
4506defa38bSPaul Mackerras {
4516defa38bSPaul Mackerras 	unsigned long start;
4526defa38bSPaul Mackerras 	int diff;
4536defa38bSPaul Mackerras 
4544e287e65SNicholas Piggin 	spin_begin();
4556defa38bSPaul Mackerras 	if (__USE_RTC()) {
4566defa38bSPaul Mackerras 		start = get_rtcl();
4576defa38bSPaul Mackerras 		do {
4586defa38bSPaul Mackerras 			/* the RTCL register wraps at 1000000000 */
4596defa38bSPaul Mackerras 			diff = get_rtcl() - start;
4606defa38bSPaul Mackerras 			if (diff < 0)
4616defa38bSPaul Mackerras 				diff += 1000000000;
4624e287e65SNicholas Piggin 			spin_cpu_relax();
4636defa38bSPaul Mackerras 		} while (diff < loops);
464*de269129SMahesh Salgaonkar 	} else if (tb_invalid) {
465*de269129SMahesh Salgaonkar 		/*
466*de269129SMahesh Salgaonkar 		 * TB is in error state and isn't ticking anymore.
467*de269129SMahesh Salgaonkar 		 * HMI handler was unable to recover from TB error.
468*de269129SMahesh Salgaonkar 		 * Return immediately, so that kernel won't get stuck here.
469*de269129SMahesh Salgaonkar 		 */
470*de269129SMahesh Salgaonkar 		spin_cpu_relax();
4716defa38bSPaul Mackerras 	} else {
4726defa38bSPaul Mackerras 		start = get_tbl();
4736defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
4744e287e65SNicholas Piggin 			spin_cpu_relax();
4756defa38bSPaul Mackerras 	}
4764e287e65SNicholas Piggin 	spin_end();
4776defa38bSPaul Mackerras }
4786defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4796defa38bSPaul Mackerras 
4806defa38bSPaul Mackerras void udelay(unsigned long usecs)
4816defa38bSPaul Mackerras {
4826defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4836defa38bSPaul Mackerras }
4846defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4856defa38bSPaul Mackerras 
486f2783c15SPaul Mackerras #ifdef CONFIG_SMP
487f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
488f2783c15SPaul Mackerras {
489f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
490f2783c15SPaul Mackerras 
491f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
492f2783c15SPaul Mackerras 		return regs->link;
493f2783c15SPaul Mackerras 
494f2783c15SPaul Mackerras 	return pc;
495f2783c15SPaul Mackerras }
496f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
497f2783c15SPaul Mackerras #endif
498f2783c15SPaul Mackerras 
499e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
500105988c0SPaul Mackerras 
5010fe1ac48SPaul Mackerras /*
5020fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
5030fe1ac48SPaul Mackerras  */
5040fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
505e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void)
506105988c0SPaul Mackerras {
5070fe1ac48SPaul Mackerras 	unsigned long x;
5080fe1ac48SPaul Mackerras 
5090fe1ac48SPaul Mackerras 	asm volatile("lbz %0,%1(13)"
5100fe1ac48SPaul Mackerras 		: "=r" (x)
511e360adbeSPeter Zijlstra 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
5120fe1ac48SPaul Mackerras 	return x;
513105988c0SPaul Mackerras }
514105988c0SPaul Mackerras 
515e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
5160fe1ac48SPaul Mackerras {
5170fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
5180fe1ac48SPaul Mackerras 		"r" (1),
519e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5200fe1ac48SPaul Mackerras }
5210fe1ac48SPaul Mackerras 
522e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
5230fe1ac48SPaul Mackerras {
5240fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
5250fe1ac48SPaul Mackerras 		"r" (0),
526e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5270fe1ac48SPaul Mackerras }
5280fe1ac48SPaul Mackerras 
529ebb37cf3SNicholas Piggin void arch_irq_work_raise(void)
530ebb37cf3SNicholas Piggin {
531ebb37cf3SNicholas Piggin 	preempt_disable();
532ebb37cf3SNicholas Piggin 	set_irq_work_pending_flag();
533ebb37cf3SNicholas Piggin 	/*
534ebb37cf3SNicholas Piggin 	 * Non-nmi code running with interrupts disabled will replay
535ebb37cf3SNicholas Piggin 	 * irq_happened before it re-enables interrupts, so setthe
536ebb37cf3SNicholas Piggin 	 * decrementer there instead of causing a hardware exception
537ebb37cf3SNicholas Piggin 	 * which would immediately hit the masked interrupt handler
538ebb37cf3SNicholas Piggin 	 * and have the net effect of setting the decrementer in
539ebb37cf3SNicholas Piggin 	 * irq_happened.
540ebb37cf3SNicholas Piggin 	 *
541ebb37cf3SNicholas Piggin 	 * NMI interrupts can not check this when they return, so the
542ebb37cf3SNicholas Piggin 	 * decrementer hardware exception is raised, which will fire
543ebb37cf3SNicholas Piggin 	 * when interrupts are next enabled.
544ebb37cf3SNicholas Piggin 	 *
545ebb37cf3SNicholas Piggin 	 * BookE does not support this yet, it must audit all NMI
546ebb37cf3SNicholas Piggin 	 * interrupt handlers to ensure they call nmi_enter() so this
547ebb37cf3SNicholas Piggin 	 * check would be correct.
548ebb37cf3SNicholas Piggin 	 */
549ebb37cf3SNicholas Piggin 	if (IS_ENABLED(CONFIG_BOOKE) || !irqs_disabled() || in_nmi()) {
550ebb37cf3SNicholas Piggin 		set_dec(1);
551ebb37cf3SNicholas Piggin 	} else {
552ebb37cf3SNicholas Piggin 		hard_irq_disable();
553ebb37cf3SNicholas Piggin 		local_paca->irq_happened |= PACA_IRQ_DEC;
554ebb37cf3SNicholas Piggin 	}
555ebb37cf3SNicholas Piggin 	preempt_enable();
556ebb37cf3SNicholas Piggin }
557ebb37cf3SNicholas Piggin 
5580fe1ac48SPaul Mackerras #else /* 32-bit */
5590fe1ac48SPaul Mackerras 
560e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
5610fe1ac48SPaul Mackerras 
56269111bacSChristoph Lameter #define set_irq_work_pending_flag()	__this_cpu_write(irq_work_pending, 1)
56369111bacSChristoph Lameter #define test_irq_work_pending()		__this_cpu_read(irq_work_pending)
56469111bacSChristoph Lameter #define clear_irq_work_pending()	__this_cpu_write(irq_work_pending, 0)
565105988c0SPaul Mackerras 
5664f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
5670fe1ac48SPaul Mackerras {
5680fe1ac48SPaul Mackerras 	preempt_disable();
569e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
5700fe1ac48SPaul Mackerras 	set_dec(1);
5710fe1ac48SPaul Mackerras 	preempt_enable();
5720fe1ac48SPaul Mackerras }
5730fe1ac48SPaul Mackerras 
574ebb37cf3SNicholas Piggin #endif /* 32 vs 64 bit */
575ebb37cf3SNicholas Piggin 
576e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
577105988c0SPaul Mackerras 
578e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
579e360adbeSPeter Zijlstra #define clear_irq_work_pending()
580105988c0SPaul Mackerras 
581e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
582105988c0SPaul Mackerras 
5831b783955SPreeti U Murthy /*
5841b783955SPreeti U Murthy  * timer_interrupt - gets called when the decrementer overflows,
5851b783955SPreeti U Murthy  * with interrupts disabled.
5861b783955SPreeti U Murthy  */
5871b783955SPreeti U Murthy void timer_interrupt(struct pt_regs *regs)
5881b783955SPreeti U Murthy {
5893f984620SNicholas Piggin 	struct clock_event_device *evt = this_cpu_ptr(&decrementers);
59069111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
5913f984620SNicholas Piggin 	struct pt_regs *old_regs;
5923f984620SNicholas Piggin 	u64 now;
5931b783955SPreeti U Murthy 
5941b783955SPreeti U Murthy 	/* Some implementations of hotplug will get timer interrupts while
5951b783955SPreeti U Murthy 	 * offline, just ignore these and we also need to set
5961b783955SPreeti U Murthy 	 * decrementers_next_tb as MAX to make sure __check_irq_replay
5971b783955SPreeti U Murthy 	 * don't replay timer interrupt when return, otherwise we'll trap
5981b783955SPreeti U Murthy 	 * here infinitely :(
5991b783955SPreeti U Murthy 	 */
600a7cba02dSNicholas Piggin 	if (unlikely(!cpu_online(smp_processor_id()))) {
6011b783955SPreeti U Murthy 		*next_tb = ~(u64)0;
602a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
6031b783955SPreeti U Murthy 		return;
6041b783955SPreeti U Murthy 	}
6051b783955SPreeti U Murthy 
606a7cba02dSNicholas Piggin 	/* Ensure a positive value is written to the decrementer, or else
607a7cba02dSNicholas Piggin 	 * some CPUs will continue to take decrementer exceptions. When the
608a7cba02dSNicholas Piggin 	 * PPC_WATCHDOG (decrementer based) is configured, keep this at most
609a7cba02dSNicholas Piggin 	 * 31 bits, which is about 4 seconds on most systems, which gives
610a7cba02dSNicholas Piggin 	 * the watchdog a chance of catching timer interrupt hard lockups.
611a7cba02dSNicholas Piggin 	 */
612a7cba02dSNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
613a7cba02dSNicholas Piggin 		set_dec(0x7fffffff);
614a7cba02dSNicholas Piggin 	else
615a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
616a7cba02dSNicholas Piggin 
6171b783955SPreeti U Murthy 	/* Conditionally hard-enable interrupts now that the DEC has been
6181b783955SPreeti U Murthy 	 * bumped to its maximum value
6191b783955SPreeti U Murthy 	 */
6201b783955SPreeti U Murthy 	may_hard_irq_enable();
6211b783955SPreeti U Murthy 
6221b783955SPreeti U Murthy 
6236e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
6241b783955SPreeti U Murthy 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
6251b783955SPreeti U Murthy 		do_IRQ(regs);
6261b783955SPreeti U Murthy #endif
6271b783955SPreeti U Murthy 
6281b783955SPreeti U Murthy 	old_regs = set_irq_regs(regs);
6291b783955SPreeti U Murthy 	irq_enter();
6303f984620SNicholas Piggin 	trace_timer_interrupt_entry(regs);
6311b783955SPreeti U Murthy 
6323f984620SNicholas Piggin 	if (test_irq_work_pending()) {
6333f984620SNicholas Piggin 		clear_irq_work_pending();
6343f984620SNicholas Piggin 		irq_work_run();
6353f984620SNicholas Piggin 	}
6363f984620SNicholas Piggin 
6373f984620SNicholas Piggin 	now = get_tb_or_rtc();
6383f984620SNicholas Piggin 	if (now >= *next_tb) {
6393f984620SNicholas Piggin 		*next_tb = ~(u64)0;
6403f984620SNicholas Piggin 		if (evt->event_handler)
6413f984620SNicholas Piggin 			evt->event_handler(evt);
6423f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_event);
6433f984620SNicholas Piggin 	} else {
6443f984620SNicholas Piggin 		now = *next_tb - now;
6453f984620SNicholas Piggin 		if (now <= decrementer_max)
6463f984620SNicholas Piggin 			set_dec(now);
6473f984620SNicholas Piggin 		/* We may have raced with new irq work */
6483f984620SNicholas Piggin 		if (test_irq_work_pending())
6493f984620SNicholas Piggin 			set_dec(1);
6503f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_others);
6513f984620SNicholas Piggin 	}
6523f984620SNicholas Piggin 
6533f984620SNicholas Piggin 	trace_timer_interrupt_exit(regs);
654f2783c15SPaul Mackerras 	irq_exit();
6557d12e780SDavid Howells 	set_irq_regs(old_regs);
656f2783c15SPaul Mackerras }
6579445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt);
658f2783c15SPaul Mackerras 
659bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
6603f984620SNicholas Piggin void timer_broadcast_interrupt(void)
6613f984620SNicholas Piggin {
6623f984620SNicholas Piggin 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
6633f984620SNicholas Piggin 
6643f984620SNicholas Piggin 	*next_tb = ~(u64)0;
6653f984620SNicholas Piggin 	tick_receive_broadcast();
666e360cd37SNicholas Piggin 	__this_cpu_inc(irq_stat.broadcast_irqs_event);
6673f984620SNicholas Piggin }
668bc907113SNicholas Piggin #endif
6693f984620SNicholas Piggin 
670dabe859eSPaul Mackerras /*
671dabe859eSPaul Mackerras  * Hypervisor decrementer interrupts shouldn't occur but are sometimes
672dabe859eSPaul Mackerras  * left pending on exit from a KVM guest.  We don't need to do anything
673dabe859eSPaul Mackerras  * to clear them, as they are edge-triggered.
674dabe859eSPaul Mackerras  */
675dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs)
676dabe859eSPaul Mackerras {
677dabe859eSPaul Mackerras }
678dabe859eSPaul Mackerras 
6797ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
680d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void)
6817ac5dde9SScott Wood {
6827ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
6837ac5dde9SScott Wood 	 * with suspending.
6847ac5dde9SScott Wood 	 */
6857ac5dde9SScott Wood 
68679901024SOliver O'Halloran 	set_dec(decrementer_max);
6877ac5dde9SScott Wood 	local_irq_disable();
68879901024SOliver O'Halloran 	set_dec(decrementer_max);
6897ac5dde9SScott Wood }
6907ac5dde9SScott Wood 
691d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void)
6927ac5dde9SScott Wood {
6937ac5dde9SScott Wood 	local_irq_enable();
6947ac5dde9SScott Wood }
6957ac5dde9SScott Wood 
6967ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6977ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
6987ac5dde9SScott Wood {
6997ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
7007ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
7017ac5dde9SScott Wood 	generic_suspend_disable_irqs();
7027ac5dde9SScott Wood }
7037ac5dde9SScott Wood 
7047ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
7057ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
7067ac5dde9SScott Wood {
7077ac5dde9SScott Wood 	generic_suspend_enable_irqs();
7087ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
7097ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
7107ac5dde9SScott Wood }
7117ac5dde9SScott Wood #endif
7127ac5dde9SScott Wood 
713b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks)
714b6c295dfSPaul Mackerras {
715b6c295dfSPaul Mackerras 	return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
716b6c295dfSPaul Mackerras }
717b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns);
718b6c295dfSPaul Mackerras 
719f2783c15SPaul Mackerras /*
720f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
721f2783c15SPaul Mackerras  *
722f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
723f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
724f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
725f2783c15SPaul Mackerras  */
7266b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void)
727f2783c15SPaul Mackerras {
72896c44507SPaul Mackerras 	if (__USE_RTC())
72996c44507SPaul Mackerras 		return get_rtc();
730fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
731f2783c15SPaul Mackerras }
732f2783c15SPaul Mackerras 
7334be1b297SCyril Bur 
7344be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES
7354be1b297SCyril Bur 
7364be1b297SCyril Bur /*
7374be1b297SCyril Bur  * Running clock - attempts to give a view of time passing for a virtualised
7384be1b297SCyril Bur  * kernels.
7394be1b297SCyril Bur  * Uses the VTB register if available otherwise a next best guess.
7404be1b297SCyril Bur  */
7414be1b297SCyril Bur unsigned long long running_clock(void)
7424be1b297SCyril Bur {
7434be1b297SCyril Bur 	/*
7444be1b297SCyril Bur 	 * Don't read the VTB as a host since KVM does not switch in host
7454be1b297SCyril Bur 	 * timebase into the VTB when it takes a guest off the CPU, reading the
7464be1b297SCyril Bur 	 * VTB would result in reading 'last switched out' guest VTB.
7474be1b297SCyril Bur 	 *
7484be1b297SCyril Bur 	 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
7494be1b297SCyril Bur 	 * would be unsafe to rely only on the #ifdef above.
7504be1b297SCyril Bur 	 */
7514be1b297SCyril Bur 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
7524be1b297SCyril Bur 	    cpu_has_feature(CPU_FTR_ARCH_207S))
7534be1b297SCyril Bur 		return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
7544be1b297SCyril Bur 
7554be1b297SCyril Bur 	/*
7564be1b297SCyril Bur 	 * This is a next best approximation without a VTB.
7574be1b297SCyril Bur 	 * On a host which is running bare metal there should never be any stolen
7584be1b297SCyril Bur 	 * time and on a host which doesn't do any virtualisation TB *should* equal
7594be1b297SCyril Bur 	 * VTB so it makes no difference anyway.
7604be1b297SCyril Bur 	 */
7619f3768e0SFrédéric Weisbecker 	return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
7624be1b297SCyril Bur }
7634be1b297SCyril Bur #endif
7644be1b297SCyril Bur 
7650bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
766f2783c15SPaul Mackerras {
767f2783c15SPaul Mackerras 	struct device_node *cpu;
7686f7aba7bSAnton Blanchard 	const __be32 *fp;
7690bb474a4SAnton Blanchard 	int found = 0;
770f2783c15SPaul Mackerras 
7710bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
772f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
773f2783c15SPaul Mackerras 
774d8a8188dSOlaf Hering 	if (cpu) {
775e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
776d8a8188dSOlaf Hering 		if (fp) {
7770bb474a4SAnton Blanchard 			found = 1;
778a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
779f2783c15SPaul Mackerras 		}
7800bb474a4SAnton Blanchard 
7810bb474a4SAnton Blanchard 		of_node_put(cpu);
782f2783c15SPaul Mackerras 	}
7830bb474a4SAnton Blanchard 
7840bb474a4SAnton Blanchard 	return found;
7850bb474a4SAnton Blanchard }
7860bb474a4SAnton Blanchard 
787e51df2c1SAnton Blanchard static void start_cpu_decrementer(void)
78877c0a700SBenjamin Herrenschmidt {
78977c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
7906e2f03e2SIvan Mikhaylov 	unsigned int tcr;
7916e2f03e2SIvan Mikhaylov 
79277c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
79377c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
79477c0a700SBenjamin Herrenschmidt 
7956e2f03e2SIvan Mikhaylov 	tcr = mfspr(SPRN_TCR);
7966e2f03e2SIvan Mikhaylov 	/*
7976e2f03e2SIvan Mikhaylov 	 * The watchdog may have already been enabled by u-boot. So leave
7986e2f03e2SIvan Mikhaylov 	 * TRC[WP] (Watchdog Period) alone.
7996e2f03e2SIvan Mikhaylov 	 */
8006e2f03e2SIvan Mikhaylov 	tcr &= TCR_WP_MASK;	/* Clear all bits except for TCR[WP] */
8016e2f03e2SIvan Mikhaylov 	tcr |= TCR_DIE;		/* Enable decrementer */
8026e2f03e2SIvan Mikhaylov 	mtspr(SPRN_TCR, tcr);
8036e2f03e2SIvan Mikhaylov #endif
80477c0a700SBenjamin Herrenschmidt }
80577c0a700SBenjamin Herrenschmidt 
8060bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
8070bb474a4SAnton Blanchard {
8080bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
8090bb474a4SAnton Blanchard 
8100bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
8110bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
8120bb474a4SAnton Blanchard 
813f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
814f2783c15SPaul Mackerras 				"(not found)\n");
8150bb474a4SAnton Blanchard 	}
816f2783c15SPaul Mackerras 
8170bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
8180bb474a4SAnton Blanchard 
8190bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
8200bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
8210bb474a4SAnton Blanchard 
8220bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
8230bb474a4SAnton Blanchard 				"(not found)\n");
824f2783c15SPaul Mackerras 	}
825f2783c15SPaul Mackerras }
826f2783c15SPaul Mackerras 
8275235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now)
828f2783c15SPaul Mackerras {
829f2783c15SPaul Mackerras 	struct rtc_time tm;
830f2783c15SPaul Mackerras 
831aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
832023f333aSJason Gunthorpe 		return -ENODEV;
833aa3be5f3STony Breeds 
8345235afa8SArnd Bergmann 	rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
835aa3be5f3STony Breeds 
836aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
837aa3be5f3STony Breeds }
838aa3be5f3STony Breeds 
8395bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts)
840aa3be5f3STony Breeds {
841aa3be5f3STony Breeds 	struct rtc_time tm;
842aa3be5f3STony Breeds 	static int first = 1;
843aa3be5f3STony Breeds 
844d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
845aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
846aa3be5f3STony Breeds 	if (first) {
847aa3be5f3STony Breeds 		first = 0;
848aa3be5f3STony Breeds 		if (ppc_md.time_init)
849aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
850aa3be5f3STony Breeds 
851aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
852d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
853d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
854d90246cdSMartin Schwidefsky 			return;
855aa3be5f3STony Breeds 		}
856d90246cdSMartin Schwidefsky 	}
857d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
858d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
859d90246cdSMartin Schwidefsky 		return;
860d90246cdSMartin Schwidefsky 	}
861f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
862978d7eb3SBenjamin Herrenschmidt 
8635bfd6435SArnd Bergmann 	ts->tv_sec = rtc_tm_to_time64(&tm);
864f2783c15SPaul Mackerras }
865f2783c15SPaul Mackerras 
8665bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts)
867978d7eb3SBenjamin Herrenschmidt {
868978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
869978d7eb3SBenjamin Herrenschmidt 
870978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
871978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
872978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
873978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
874978d7eb3SBenjamin Herrenschmidt 	}
875978d7eb3SBenjamin Herrenschmidt 
876978d7eb3SBenjamin Herrenschmidt }
877978d7eb3SBenjamin Herrenschmidt 
8784a4cfe38STony Breeds /* clocksource code */
8796b847d79SSantosh Sivaraj static notrace u64 rtc_read(struct clocksource *cs)
8804a4cfe38STony Breeds {
881a5a1d1c2SThomas Gleixner 	return (u64)get_rtc();
8824a4cfe38STony Breeds }
8834a4cfe38STony Breeds 
8846b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs)
8854a4cfe38STony Breeds {
886a5a1d1c2SThomas Gleixner 	return (u64)get_tb();
8874a4cfe38STony Breeds }
8884a4cfe38STony Breeds 
889d4cfb113SPaul Mackerras 
890d4cfb113SPaul Mackerras void update_vsyscall(struct timekeeper *tk)
8914a4cfe38STony Breeds {
892d4cfb113SPaul Mackerras 	struct timespec xt;
893d4cfb113SPaul Mackerras 	struct clocksource *clock = tk->tkr_mono.clock;
894d4cfb113SPaul Mackerras 	u32 mult = tk->tkr_mono.mult;
895d4cfb113SPaul Mackerras 	u32 shift = tk->tkr_mono.shift;
896d4cfb113SPaul Mackerras 	u64 cycle_last = tk->tkr_mono.cycle_last;
897b0797b60SJohn Stultz 	u64 new_tb_to_xs, new_stamp_xsec;
898d4cfb113SPaul Mackerras 	u64 frac_sec;
8994a4cfe38STony Breeds 
9004a4cfe38STony Breeds 	if (clock != &clocksource_timebase)
9014a4cfe38STony Breeds 		return;
9024a4cfe38STony Breeds 
903d4cfb113SPaul Mackerras 	xt.tv_sec = tk->xtime_sec;
904d4cfb113SPaul Mackerras 	xt.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
905d4cfb113SPaul Mackerras 
9064a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
9074a4cfe38STony Breeds 	++vdso_data->tb_update_count;
9084a4cfe38STony Breeds 	smp_mb();
9094a4cfe38STony Breeds 
910d4cfb113SPaul Mackerras 	/*
911d4cfb113SPaul Mackerras 	 * This computes ((2^20 / 1e9) * mult) >> shift as a
912d4cfb113SPaul Mackerras 	 * 0.64 fixed-point fraction.
913d4cfb113SPaul Mackerras 	 * The computation in the else clause below won't overflow
914d4cfb113SPaul Mackerras 	 * (as long as the timebase frequency is >= 1.049 MHz)
915d4cfb113SPaul Mackerras 	 * but loses precision because we lose the low bits of the constant
916d4cfb113SPaul Mackerras 	 * in the shift.  Note that 19342813113834067 ~= 2^(20+64) / 1e9.
917d4cfb113SPaul Mackerras 	 * For a shift of 24 the error is about 0.5e-9, or about 0.5ns
918d4cfb113SPaul Mackerras 	 * over a second.  (Shift values are usually 22, 23 or 24.)
919d4cfb113SPaul Mackerras 	 * For high frequency clocks such as the 512MHz timebase clock
920d4cfb113SPaul Mackerras 	 * on POWER[6789], the mult value is small (e.g. 32768000)
921d4cfb113SPaul Mackerras 	 * and so we can shift the constant by 16 initially
922d4cfb113SPaul Mackerras 	 * (295147905179 ~= 2^(20+64-16) / 1e9) and then do the
923d4cfb113SPaul Mackerras 	 * remaining shifts after the multiplication, which gives a
924d4cfb113SPaul Mackerras 	 * more accurate result (e.g. with mult = 32768000, shift = 24,
925d4cfb113SPaul Mackerras 	 * the error is only about 1.2e-12, or 0.7ns over 10 minutes).
926d4cfb113SPaul Mackerras 	 */
927d4cfb113SPaul Mackerras 	if (mult <= 62500000 && clock->shift >= 16)
928d4cfb113SPaul Mackerras 		new_tb_to_xs = ((u64) mult * 295147905179ULL) >> (clock->shift - 16);
929d4cfb113SPaul Mackerras 	else
93011b8633aSAnton Blanchard 		new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift);
931b0797b60SJohn Stultz 
932d4cfb113SPaul Mackerras 	/*
933d4cfb113SPaul Mackerras 	 * Compute the fractional second in units of 2^-32 seconds.
934d4cfb113SPaul Mackerras 	 * The fractional second is tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift
935d4cfb113SPaul Mackerras 	 * in nanoseconds, so multiplying that by 2^32 / 1e9 gives
936d4cfb113SPaul Mackerras 	 * it in units of 2^-32 seconds.
937d4cfb113SPaul Mackerras 	 * We assume shift <= 32 because clocks_calc_mult_shift()
938d4cfb113SPaul Mackerras 	 * generates shift values in the range 0 - 32.
939d4cfb113SPaul Mackerras 	 */
940d4cfb113SPaul Mackerras 	frac_sec = tk->tkr_mono.xtime_nsec << (32 - shift);
941d4cfb113SPaul Mackerras 	do_div(frac_sec, NSEC_PER_SEC);
942d4cfb113SPaul Mackerras 
943d4cfb113SPaul Mackerras 	/*
944d4cfb113SPaul Mackerras 	 * Work out new stamp_xsec value for any legacy users of systemcfg.
945d4cfb113SPaul Mackerras 	 * stamp_xsec is in units of 2^-20 seconds.
946d4cfb113SPaul Mackerras 	 */
947d4cfb113SPaul Mackerras 	new_stamp_xsec = frac_sec >> 12;
948d4cfb113SPaul Mackerras 	new_stamp_xsec += tk->xtime_sec * XSEC_PER_SEC;
94947916be4SThomas Gleixner 
950b0797b60SJohn Stultz 	/*
951b0797b60SJohn Stultz 	 * tb_update_count is used to allow the userspace gettimeofday code
952b0797b60SJohn Stultz 	 * to assure itself that it sees a consistent view of the tb_to_xs and
953b0797b60SJohn Stultz 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
954b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
955b0797b60SJohn Stultz 	 * the two values of tb_update_count match and are even then the
956b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
957b0797b60SJohn Stultz 	 * loops back and reads them again until this criteria is met.
958b0797b60SJohn Stultz 	 */
9594a0e6377SThomas Gleixner 	vdso_data->tb_orig_stamp = cycle_last;
960b0797b60SJohn Stultz 	vdso_data->stamp_xsec = new_stamp_xsec;
961b0797b60SJohn Stultz 	vdso_data->tb_to_xs = new_tb_to_xs;
962d4cfb113SPaul Mackerras 	vdso_data->wtom_clock_sec = tk->wall_to_monotonic.tv_sec;
963d4cfb113SPaul Mackerras 	vdso_data->wtom_clock_nsec = tk->wall_to_monotonic.tv_nsec;
964d4cfb113SPaul Mackerras 	vdso_data->stamp_xtime = xt;
96547916be4SThomas Gleixner 	vdso_data->stamp_sec_fraction = frac_sec;
966b0797b60SJohn Stultz 	smp_wmb();
967b0797b60SJohn Stultz 	++(vdso_data->tb_update_count);
9684a4cfe38STony Breeds }
9694a4cfe38STony Breeds 
9704a4cfe38STony Breeds void update_vsyscall_tz(void)
9714a4cfe38STony Breeds {
9724a4cfe38STony Breeds 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
9734a4cfe38STony Breeds 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
9744a4cfe38STony Breeds }
9754a4cfe38STony Breeds 
9761c21a293SMichael Ellerman static void __init clocksource_init(void)
9774a4cfe38STony Breeds {
9784a4cfe38STony Breeds 	struct clocksource *clock;
9794a4cfe38STony Breeds 
9804a4cfe38STony Breeds 	if (__USE_RTC())
9814a4cfe38STony Breeds 		clock = &clocksource_rtc;
9824a4cfe38STony Breeds 	else
9834a4cfe38STony Breeds 		clock = &clocksource_timebase;
9844a4cfe38STony Breeds 
98511b8633aSAnton Blanchard 	if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
9864a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
9874a4cfe38STony Breeds 		       clock->name);
9884a4cfe38STony Breeds 		return;
9894a4cfe38STony Breeds 	}
9904a4cfe38STony Breeds 
9914a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
9924a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
9934a4cfe38STony Breeds }
9944a4cfe38STony Breeds 
995d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
996d831d0b8STony Breeds 				      struct clock_event_device *dev)
997d831d0b8STony Breeds {
99869111bacSChristoph Lameter 	__this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt);
999d831d0b8STony Breeds 	set_dec(evt);
10000215f7d8SBenjamin Herrenschmidt 
10010215f7d8SBenjamin Herrenschmidt 	/* We may have raced with new irq work */
10020215f7d8SBenjamin Herrenschmidt 	if (test_irq_work_pending())
10030215f7d8SBenjamin Herrenschmidt 		set_dec(1);
10040215f7d8SBenjamin Herrenschmidt 
1005d831d0b8STony Breeds 	return 0;
1006d831d0b8STony Breeds }
1007d831d0b8STony Breeds 
100837a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev)
1009d831d0b8STony Breeds {
101079901024SOliver O'Halloran 	decrementer_set_next_event(decrementer_max, dev);
101137a13e78SViresh Kumar 	return 0;
1012d831d0b8STony Breeds }
1013d831d0b8STony Breeds 
1014d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
1015d831d0b8STony Breeds {
10167df10275SAnton Blanchard 	struct clock_event_device *dec = &per_cpu(decrementers, cpu);
1017d831d0b8STony Breeds 
1018d831d0b8STony Breeds 	*dec = decrementer_clockevent;
1019320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
1020d831d0b8STony Breeds 
10218b78fdb0SAnton Blanchard 	clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
10228b78fdb0SAnton Blanchard 
1023b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
1024d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
1025b4d16ab5SMichael Ellerman 
1026b4d16ab5SMichael Ellerman 	/* Set values for KVM, see kvm_emulate_dec() */
1027b4d16ab5SMichael Ellerman 	decrementer_clockevent.mult = dec->mult;
1028b4d16ab5SMichael Ellerman 	decrementer_clockevent.shift = dec->shift;
1029d831d0b8STony Breeds }
1030d831d0b8STony Breeds 
103179901024SOliver O'Halloran static void enable_large_decrementer(void)
103279901024SOliver O'Halloran {
103379901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
103479901024SOliver O'Halloran 		return;
103579901024SOliver O'Halloran 
103679901024SOliver O'Halloran 	if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
103779901024SOliver O'Halloran 		return;
103879901024SOliver O'Halloran 
103979901024SOliver O'Halloran 	/*
104079901024SOliver O'Halloran 	 * If we're running as the hypervisor we need to enable the LD manually
104179901024SOliver O'Halloran 	 * otherwise firmware should have done it for us.
104279901024SOliver O'Halloran 	 */
104379901024SOliver O'Halloran 	if (cpu_has_feature(CPU_FTR_HVMODE))
104479901024SOliver O'Halloran 		mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
104579901024SOliver O'Halloran }
104679901024SOliver O'Halloran 
104779901024SOliver O'Halloran static void __init set_decrementer_max(void)
104879901024SOliver O'Halloran {
104979901024SOliver O'Halloran 	struct device_node *cpu;
105079901024SOliver O'Halloran 	u32 bits = 32;
105179901024SOliver O'Halloran 
105279901024SOliver O'Halloran 	/* Prior to ISAv3 the decrementer is always 32 bit */
105379901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
105479901024SOliver O'Halloran 		return;
105579901024SOliver O'Halloran 
105679901024SOliver O'Halloran 	cpu = of_find_node_by_type(NULL, "cpu");
105779901024SOliver O'Halloran 
105879901024SOliver O'Halloran 	if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
105979901024SOliver O'Halloran 		if (bits > 64 || bits < 32) {
106079901024SOliver O'Halloran 			pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
106179901024SOliver O'Halloran 			bits = 32;
106279901024SOliver O'Halloran 		}
106379901024SOliver O'Halloran 
106479901024SOliver O'Halloran 		/* calculate the signed maximum given this many bits */
106579901024SOliver O'Halloran 		decrementer_max = (1ul << (bits - 1)) - 1;
106679901024SOliver O'Halloran 	}
106779901024SOliver O'Halloran 
106879901024SOliver O'Halloran 	of_node_put(cpu);
106979901024SOliver O'Halloran 
107079901024SOliver O'Halloran 	pr_info("time_init: %u bit decrementer (max: %llx)\n",
107179901024SOliver O'Halloran 		bits, decrementer_max);
107279901024SOliver O'Halloran }
107379901024SOliver O'Halloran 
1074c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
1075d831d0b8STony Breeds {
10768b78fdb0SAnton Blanchard 	register_decrementer_clockevent(smp_processor_id());
1077d831d0b8STony Breeds }
1078d831d0b8STony Breeds 
1079d831d0b8STony Breeds void secondary_cpu_time_init(void)
1080d831d0b8STony Breeds {
108179901024SOliver O'Halloran 	/* Enable and test the large decrementer for this cpu */
108279901024SOliver O'Halloran 	enable_large_decrementer();
108379901024SOliver O'Halloran 
108477c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
108577c0a700SBenjamin Herrenschmidt 	 * such as BookE
108677c0a700SBenjamin Herrenschmidt 	 */
108777c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
108877c0a700SBenjamin Herrenschmidt 
1089d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
1090d831d0b8STony Breeds 	 * call here ! */
1091d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
1092d831d0b8STony Breeds }
1093d831d0b8STony Breeds 
1094f2783c15SPaul Mackerras /* This function is only called on the boot processor */
1095f2783c15SPaul Mackerras void __init time_init(void)
1096f2783c15SPaul Mackerras {
1097f2783c15SPaul Mackerras 	struct div_result res;
1098d75d68cfSPaul Mackerras 	u64 scale;
1099f2783c15SPaul Mackerras 	unsigned shift;
1100f2783c15SPaul Mackerras 
110196c44507SPaul Mackerras 	if (__USE_RTC()) {
110296c44507SPaul Mackerras 		/* 601 processor: dec counts down by 128 every 128ns */
110396c44507SPaul Mackerras 		ppc_tb_freq = 1000000000;
110496c44507SPaul Mackerras 	} else {
110596c44507SPaul Mackerras 		/* Normal PowerPC with timebase register */
1106f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
1107224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
1108374e99d4SPaul Mackerras 		       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
1109224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
1110374e99d4SPaul Mackerras 		       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
111196c44507SPaul Mackerras 	}
1112374e99d4SPaul Mackerras 
1113374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
1114092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
1115374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
1116c6622f63SPaul Mackerras 	calc_cputime_factors();
1117092b8f34SPaul Mackerras 
1118092b8f34SPaul Mackerras 	/*
1119f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
1120f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
1121f2783c15SPaul Mackerras 	 * which is the timebase frequency.
1122f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
1123f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
1124f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
1125f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
1126f2783c15SPaul Mackerras 	 * sched_clock().
1127f2783c15SPaul Mackerras 	 */
1128f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
1129f2783c15SPaul Mackerras 	scale = res.result_low;
1130f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
1131f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
1132f2783c15SPaul Mackerras 		res.result_high >>= 1;
1133f2783c15SPaul Mackerras 	}
1134f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
1135f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
1136fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
1137c27da339SBenjamin Herrenschmidt 	boot_tb = get_tb_or_rtc();
1138f2783c15SPaul Mackerras 
1139092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
1140092b8f34SPaul Mackerras 	if (timezone_offset) {
1141092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
1142092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
1143092b8f34SPaul Mackerras 	}
1144092b8f34SPaul Mackerras 
1145a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
1146a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
1147f2783c15SPaul Mackerras 
114879901024SOliver O'Halloran 	/* initialise and enable the large decrementer (if we have one) */
114979901024SOliver O'Halloran 	set_decrementer_max();
115079901024SOliver O'Halloran 	enable_large_decrementer();
115179901024SOliver O'Halloran 
115277c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
115377c0a700SBenjamin Herrenschmidt 	 * such as BookE
115477c0a700SBenjamin Herrenschmidt 	 */
115577c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
115677c0a700SBenjamin Herrenschmidt 
1157f5339277SStephen Rothwell 	/* Register the clocksource */
11584a4cfe38STony Breeds 	clocksource_init();
11594a4cfe38STony Breeds 
1160d831d0b8STony Breeds 	init_decrementer_clockevent();
11610d948730SPreeti U Murthy 	tick_setup_hrtimer_broadcast();
1162f0d37300SKevin Hao 
1163f0d37300SKevin Hao #ifdef CONFIG_COMMON_CLK
1164f0d37300SKevin Hao 	of_clk_init(NULL);
1165f0d37300SKevin Hao #endif
1166f2783c15SPaul Mackerras }
1167f2783c15SPaul Mackerras 
1168f2783c15SPaul Mackerras /*
1169f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1170f2783c15SPaul Mackerras  * result.
1171f2783c15SPaul Mackerras  */
1172f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1173f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1174f2783c15SPaul Mackerras {
1175f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1176f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1177f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1178f2783c15SPaul Mackerras 
1179f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1180f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1181f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1182f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1183f2783c15SPaul Mackerras 
1184f2783c15SPaul Mackerras 	w = a / divisor;
1185f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1186f2783c15SPaul Mackerras 
1187f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1188f2783c15SPaul Mackerras 	x = ra;
1189f2783c15SPaul Mackerras 
1190f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1191f2783c15SPaul Mackerras 	y = rb;
1192f2783c15SPaul Mackerras 
1193f2783c15SPaul Mackerras 	do_div(rc, divisor);
1194f2783c15SPaul Mackerras 	z = rc;
1195f2783c15SPaul Mackerras 
1196f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1197f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1198f2783c15SPaul Mackerras 
1199f2783c15SPaul Mackerras }
1200bcd68a70SGeert Uytterhoeven 
1201177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
1202177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
1203177996e6SBenjamin Herrenschmidt {
1204177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
1205177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
1206177996e6SBenjamin Herrenschmidt 	 */
1207177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
1208177996e6SBenjamin Herrenschmidt }
1209177996e6SBenjamin Herrenschmidt 
1210169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1211169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1212169047f4SArnd Bergmann {
1213169047f4SArnd Bergmann 	ppc_md.get_rtc_time(tm);
1214890ae797SAlexandre Belloni 	return 0;
1215169047f4SArnd Bergmann }
1216169047f4SArnd Bergmann 
1217169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1218169047f4SArnd Bergmann {
1219169047f4SArnd Bergmann 	if (!ppc_md.set_rtc_time)
1220169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1221169047f4SArnd Bergmann 
1222169047f4SArnd Bergmann 	if (ppc_md.set_rtc_time(tm) < 0)
1223169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1224169047f4SArnd Bergmann 
1225169047f4SArnd Bergmann 	return 0;
1226169047f4SArnd Bergmann }
1227169047f4SArnd Bergmann 
1228169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = {
1229169047f4SArnd Bergmann 	.read_time = rtc_generic_get_time,
1230169047f4SArnd Bergmann 	.set_time = rtc_generic_set_time,
1231169047f4SArnd Bergmann };
1232169047f4SArnd Bergmann 
1233bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1234bcd68a70SGeert Uytterhoeven {
1235bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1236bcd68a70SGeert Uytterhoeven 
1237bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1238bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1239bcd68a70SGeert Uytterhoeven 
1240169047f4SArnd Bergmann 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1241169047f4SArnd Bergmann 					     &rtc_generic_ops,
1242169047f4SArnd Bergmann 					     sizeof(rtc_generic_ops));
1243bcd68a70SGeert Uytterhoeven 
12448c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(pdev);
1245bcd68a70SGeert Uytterhoeven }
1246bcd68a70SGeert Uytterhoeven 
12478f6b9512SPaul Gortmaker device_initcall(rtc_init);
1248169047f4SArnd Bergmann #endif
1249