xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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>
34e225c4d6SWan Jiabing #include <linux/sched/cputime.h>
35f2783c15SPaul Mackerras #include <linux/kernel.h>
36f2783c15SPaul Mackerras #include <linux/param.h>
37f2783c15SPaul Mackerras #include <linux/string.h>
38f2783c15SPaul Mackerras #include <linux/mm.h>
39f2783c15SPaul Mackerras #include <linux/interrupt.h>
40f2783c15SPaul Mackerras #include <linux/timex.h>
41f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
42f2783c15SPaul Mackerras #include <linux/time.h>
43f2783c15SPaul Mackerras #include <linux/init.h>
44f2783c15SPaul Mackerras #include <linux/profile.h>
45f2783c15SPaul Mackerras #include <linux/cpu.h>
46f2783c15SPaul Mackerras #include <linux/security.h>
47f2783c15SPaul Mackerras #include <linux/percpu.h>
48f2783c15SPaul Mackerras #include <linux/rtc.h>
49092b8f34SPaul Mackerras #include <linux/jiffies.h>
50c6622f63SPaul Mackerras #include <linux/posix-timers.h>
517d12e780SDavid Howells #include <linux/irq.h>
52177996e6SBenjamin Herrenschmidt #include <linux/delay.h>
53e360adbeSPeter Zijlstra #include <linux/irq_work.h>
5460083063SGeert Uytterhoeven #include <linux/of_clk.h>
557f92bc56SDaniel Axtens #include <linux/suspend.h>
564e287e65SNicholas Piggin #include <linux/processor.h>
57ce0091a0SHe Ying #include <linux/mc146818rtc.h>
58e6f6390aSChristophe Leroy #include <linux/platform_device.h>
59f2783c15SPaul Mackerras 
60ce0091a0SHe Ying #include <asm/trace.h>
613a96570fSNicholas Piggin #include <asm/interrupt.h>
62f2783c15SPaul Mackerras #include <asm/io.h>
63f2783c15SPaul Mackerras #include <asm/nvram.h>
64f2783c15SPaul Mackerras #include <asm/cache.h>
65f2783c15SPaul Mackerras #include <asm/machdep.h>
667c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
67f2783c15SPaul Mackerras #include <asm/time.h>
68f2783c15SPaul Mackerras #include <asm/irq.h>
69f2783c15SPaul Mackerras #include <asm/div64.h>
702249ca9dSPaul Mackerras #include <asm/smp.h>
71a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
72f2783c15SPaul Mackerras #include <asm/firmware.h>
73cc15ff32SGanesh Goudar #include <asm/mce.h>
74f2783c15SPaul Mackerras 
754a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
764a4cfe38STony Breeds 
77d831d0b8STony Breeds #include <linux/clockchips.h>
78189374aeSJohn Stultz #include <linux/timekeeper_internal.h>
794a4cfe38STony Breeds 
80a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *);
814a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
824a4cfe38STony Breeds 	.name         = "timebase",
834a4cfe38STony Breeds 	.rating       = 400,
844a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
854a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
864a4cfe38STony Breeds 	.read         = timebase_read,
87ab037dd8SChristophe Leroy 	.vdso_clock_mode	= VDSO_CLOCKMODE_ARCHTIMER,
884a4cfe38STony Breeds };
894a4cfe38STony Breeds 
9079901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
9179901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
929581991aSNicholas Piggin EXPORT_SYMBOL_GPL(decrementer_max); /* for KVM HDEC */
93d831d0b8STony Breeds 
94d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
95d831d0b8STony Breeds 				      struct clock_event_device *dev);
9637a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt);
97d831d0b8STony Breeds 
986e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = {
99d831d0b8STony Breeds 	.name			= "decrementer",
100d831d0b8STony Breeds 	.rating			= 200,
101d831d0b8STony Breeds 	.irq			= 0,
102d831d0b8STony Breeds 	.set_next_event		= decrementer_set_next_event,
10381759360SAnton Blanchard 	.set_state_oneshot_stopped = decrementer_shutdown,
10437a13e78SViresh Kumar 	.set_state_shutdown	= decrementer_shutdown,
10537a13e78SViresh Kumar 	.tick_resume		= decrementer_shutdown,
10637a13e78SViresh Kumar 	.features		= CLOCK_EVT_FEAT_ONESHOT |
10737a13e78SViresh Kumar 				  CLOCK_EVT_FEAT_C3STOP,
108d831d0b8STony Breeds };
1096e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent);
110d831d0b8STony Breeds 
11135de589cSNicholas Piggin /*
11235de589cSNicholas Piggin  * This always puts next_tb beyond now, so the clock event will never fire
11335de589cSNicholas Piggin  * with the usual comparison, no need for a separate test for stopped.
11435de589cSNicholas Piggin  */
11535de589cSNicholas Piggin #define DEC_CLOCKEVENT_STOPPED ~0ULL
11635de589cSNicholas Piggin DEFINE_PER_CPU(u64, decrementers_next_tb) = DEC_CLOCKEVENT_STOPPED;
1174ebbd075SNicholas Piggin EXPORT_SYMBOL_GPL(decrementers_next_tb);
1187df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers);
119d831d0b8STony Breeds 
120f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
121f2783c15SPaul Mackerras 
122f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
123f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
124f2783c15SPaul Mackerras #else
125f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
126f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
127f2783c15SPaul Mackerras #endif
128f2783c15SPaul Mackerras 
129f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
130f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
131f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
132f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
133f985adafSNicholas Piggin EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime conversions */
134092b8f34SPaul Mackerras 
135f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
136f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
137f2783c15SPaul Mackerras 
138fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
139fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
140364a1246SHeiko Schocher static u64 boot_tb __read_mostly;
141f2783c15SPaul Mackerras 
142f2783c15SPaul Mackerras extern struct timezone sys_tz;
143f2783c15SPaul Mackerras static long timezone_offset;
144f2783c15SPaul Mackerras 
145f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
14655ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq);
147f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
14855ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq);
14996c44507SPaul Mackerras 
150de269129SMahesh Salgaonkar bool tb_invalid;
151de269129SMahesh Salgaonkar 
152abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
153c6622f63SPaul Mackerras /*
154cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
155cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
156c6622f63SPaul Mackerras  */
read_spurr(unsigned long tb)157abcff86dSChristophe Leroy static inline unsigned long read_spurr(unsigned long tb)
158c6622f63SPaul Mackerras {
159cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
160cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
161c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
162c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
163cf9efce0SPaul Mackerras 	return tb;
164cf9efce0SPaul Mackerras }
165cf9efce0SPaul Mackerras 
1664603ac18SMichael Neuling /*
167c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
168c6622f63SPaul Mackerras  * or soft irq state.
169c6622f63SPaul Mackerras  */
vtime_delta_scaled(struct cpu_accounting_data * acct,unsigned long now,unsigned long stime)170b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
171b38a181cSChristophe Leroy 					unsigned long now, unsigned long stime)
172c6622f63SPaul Mackerras {
173abcff86dSChristophe Leroy 	unsigned long stime_scaled = 0;
174abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
175b38a181cSChristophe Leroy 	unsigned long nowscaled, deltascaled;
176a19ff1a2SFrederic Weisbecker 	unsigned long utime, utime_scaled;
177c6622f63SPaul Mackerras 
1784603ac18SMichael Neuling 	nowscaled = read_spurr(now);
179c223c903SChristophe Leroy 	deltascaled = nowscaled - acct->startspurr;
180c223c903SChristophe Leroy 	acct->startspurr = nowscaled;
181a19ff1a2SFrederic Weisbecker 	utime = acct->utime - acct->utime_sspurr;
1828c8b73c4SFrederic Weisbecker 	acct->utime_sspurr = acct->utime;
183cf9efce0SPaul Mackerras 
184cf9efce0SPaul Mackerras 	/*
185cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
186cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
187cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
188cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
189cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
190cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
191cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
192cf9efce0SPaul Mackerras 	 * used by account_process_tick.
193cf9efce0SPaul Mackerras 	 */
194b38a181cSChristophe Leroy 	stime_scaled = stime;
195a19ff1a2SFrederic Weisbecker 	utime_scaled = utime;
196a19ff1a2SFrederic Weisbecker 	if (deltascaled != stime + utime) {
197a19ff1a2SFrederic Weisbecker 		if (utime) {
198b38a181cSChristophe Leroy 			stime_scaled = deltascaled * stime / (stime + utime);
199b38a181cSChristophe Leroy 			utime_scaled = deltascaled - stime_scaled;
200cf9efce0SPaul Mackerras 		} else {
201b38a181cSChristophe Leroy 			stime_scaled = deltascaled;
202c6622f63SPaul Mackerras 		}
203cf9efce0SPaul Mackerras 	}
204a19ff1a2SFrederic Weisbecker 	acct->utime_scaled += utime_scaled;
205abcff86dSChristophe Leroy #endif
206cf9efce0SPaul Mackerras 
207b38a181cSChristophe Leroy 	return stime_scaled;
208b38a181cSChristophe Leroy }
209b38a181cSChristophe Leroy 
vtime_delta(struct cpu_accounting_data * acct,unsigned long * stime_scaled,unsigned long * steal_time)2108a6a5920SFrederic Weisbecker static unsigned long vtime_delta(struct cpu_accounting_data *acct,
211b38a181cSChristophe Leroy 				 unsigned long *stime_scaled,
212b38a181cSChristophe Leroy 				 unsigned long *steal_time)
213b38a181cSChristophe Leroy {
214b38a181cSChristophe Leroy 	unsigned long now, stime;
215b38a181cSChristophe Leroy 
216b38a181cSChristophe Leroy 	WARN_ON_ONCE(!irqs_disabled());
217b38a181cSChristophe Leroy 
218b38a181cSChristophe Leroy 	now = mftb();
219b38a181cSChristophe Leroy 	stime = now - acct->starttime;
220b38a181cSChristophe Leroy 	acct->starttime = now;
221b38a181cSChristophe Leroy 
222b38a181cSChristophe Leroy 	*stime_scaled = vtime_delta_scaled(acct, now, stime);
223b38a181cSChristophe Leroy 
2246ba5aa54SNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC_SPLPAR) &&
2256ba5aa54SNicholas Piggin 			firmware_has_feature(FW_FEATURE_SPLPAR))
2266ba5aa54SNicholas Piggin 		*steal_time = pseries_calculate_stolen_time(now);
2276ba5aa54SNicholas Piggin 	else
2286ba5aa54SNicholas Piggin 		*steal_time = 0;
229b38a181cSChristophe Leroy 
230a19ff1a2SFrederic Weisbecker 	return stime;
231a7e1a9e3SFrederic Weisbecker }
232a7e1a9e3SFrederic Weisbecker 
vtime_delta_kernel(struct cpu_accounting_data * acct,unsigned long * stime,unsigned long * stime_scaled)2338a6a5920SFrederic Weisbecker static void vtime_delta_kernel(struct cpu_accounting_data *acct,
2348a6a5920SFrederic Weisbecker 			       unsigned long *stime, unsigned long *stime_scaled)
2358a6a5920SFrederic Weisbecker {
2368a6a5920SFrederic Weisbecker 	unsigned long steal_time;
2378a6a5920SFrederic Weisbecker 
2388a6a5920SFrederic Weisbecker 	*stime = vtime_delta(acct, stime_scaled, &steal_time);
2398a6a5920SFrederic Weisbecker 	*stime -= min(*stime, steal_time);
2408a6a5920SFrederic Weisbecker 	acct->steal_time += steal_time;
2418a6a5920SFrederic Weisbecker }
2428a6a5920SFrederic Weisbecker 
vtime_account_kernel(struct task_struct * tsk)243f83eeb1aSFrederic Weisbecker void vtime_account_kernel(struct task_struct *tsk)
244a7e1a9e3SFrederic Weisbecker {
245a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
2468a6a5920SFrederic Weisbecker 	unsigned long stime, stime_scaled;
247a7e1a9e3SFrederic Weisbecker 
2488a6a5920SFrederic Weisbecker 	vtime_delta_kernel(acct, &stime, &stime_scaled);
249a19ff1a2SFrederic Weisbecker 
2508a6a5920SFrederic Weisbecker 	if (tsk->flags & PF_VCPU) {
251a19ff1a2SFrederic Weisbecker 		acct->gtime += stime;
252abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
253a19ff1a2SFrederic Weisbecker 		acct->utime_scaled += stime_scaled;
254abcff86dSChristophe Leroy #endif
255a19ff1a2SFrederic Weisbecker 	} else {
256a19ff1a2SFrederic Weisbecker 		acct->stime += stime;
257abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
258a19ff1a2SFrederic Weisbecker 		acct->stime_scaled += stime_scaled;
259abcff86dSChristophe Leroy #endif
260a19ff1a2SFrederic Weisbecker 	}
261a7e1a9e3SFrederic Weisbecker }
262f83eeb1aSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_kernel);
263a7e1a9e3SFrederic Weisbecker 
vtime_account_idle(struct task_struct * tsk)264fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk)
265a7e1a9e3SFrederic Weisbecker {
266a19ff1a2SFrederic Weisbecker 	unsigned long stime, stime_scaled, steal_time;
267a19ff1a2SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
268a7e1a9e3SFrederic Weisbecker 
2698a6a5920SFrederic Weisbecker 	stime = vtime_delta(acct, &stime_scaled, &steal_time);
270a19ff1a2SFrederic Weisbecker 	acct->idle_time += stime + steal_time;
271cf9efce0SPaul Mackerras }
272c6622f63SPaul Mackerras 
vtime_account_irq_field(struct cpu_accounting_data * acct,unsigned long * field)2738a6a5920SFrederic Weisbecker static void vtime_account_irq_field(struct cpu_accounting_data *acct,
2748a6a5920SFrederic Weisbecker 				    unsigned long *field)
2758a6a5920SFrederic Weisbecker {
2768a6a5920SFrederic Weisbecker 	unsigned long stime, stime_scaled;
2778a6a5920SFrederic Weisbecker 
2788a6a5920SFrederic Weisbecker 	vtime_delta_kernel(acct, &stime, &stime_scaled);
2798a6a5920SFrederic Weisbecker 	*field += stime;
2808a6a5920SFrederic Weisbecker #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
2818a6a5920SFrederic Weisbecker 	acct->stime_scaled += stime_scaled;
2828a6a5920SFrederic Weisbecker #endif
2838a6a5920SFrederic Weisbecker }
2848a6a5920SFrederic Weisbecker 
vtime_account_softirq(struct task_struct * tsk)2858a6a5920SFrederic Weisbecker void vtime_account_softirq(struct task_struct *tsk)
2868a6a5920SFrederic Weisbecker {
2878a6a5920SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
2888a6a5920SFrederic Weisbecker 	vtime_account_irq_field(acct, &acct->softirq_time);
2898a6a5920SFrederic Weisbecker }
2908a6a5920SFrederic Weisbecker 
vtime_account_hardirq(struct task_struct * tsk)2918a6a5920SFrederic Weisbecker void vtime_account_hardirq(struct task_struct *tsk)
2928a6a5920SFrederic Weisbecker {
2938a6a5920SFrederic Weisbecker 	struct cpu_accounting_data *acct = get_accounting(tsk);
2948a6a5920SFrederic Weisbecker 	vtime_account_irq_field(acct, &acct->hardirq_time);
2958a6a5920SFrederic Weisbecker }
2968a6a5920SFrederic Weisbecker 
vtime_flush_scaled(struct task_struct * tsk,struct cpu_accounting_data * acct)297b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk,
298b38a181cSChristophe Leroy 			       struct cpu_accounting_data *acct)
299b38a181cSChristophe Leroy {
300abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
301b38a181cSChristophe Leroy 	if (acct->utime_scaled)
302b38a181cSChristophe Leroy 		tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
303b38a181cSChristophe Leroy 	if (acct->stime_scaled)
304b38a181cSChristophe Leroy 		tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
305b38a181cSChristophe Leroy 
306b38a181cSChristophe Leroy 	acct->utime_scaled = 0;
307b38a181cSChristophe Leroy 	acct->utime_sspurr = 0;
308b38a181cSChristophe Leroy 	acct->stime_scaled = 0;
309abcff86dSChristophe Leroy #endif
310b38a181cSChristophe Leroy }
311b38a181cSChristophe Leroy 
312c6622f63SPaul Mackerras /*
313c8d7dabfSFrederic Weisbecker  * Account the whole cputime accumulated in the paca
314c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
315f83eeb1aSFrederic Weisbecker  * Assumes that vtime_account_kernel/idle() has been called
316bcebdf84SFrederic Weisbecker  * recently (i.e. since the last entry from usermode) so that
317cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
318c6622f63SPaul Mackerras  */
vtime_flush(struct task_struct * tsk)319c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk)
320c6622f63SPaul Mackerras {
321c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
322c6622f63SPaul Mackerras 
323a19ff1a2SFrederic Weisbecker 	if (acct->utime)
32423244a5cSFrederic Weisbecker 		account_user_time(tsk, cputime_to_nsecs(acct->utime));
325a19ff1a2SFrederic Weisbecker 
326a19ff1a2SFrederic Weisbecker 	if (acct->gtime)
327fb8b049cSFrederic Weisbecker 		account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
328a19ff1a2SFrederic Weisbecker 
32951eeef9eSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
330be9095edSFrederic Weisbecker 		account_steal_time(cputime_to_nsecs(acct->steal_time));
33151eeef9eSChristophe Leroy 		acct->steal_time = 0;
33251eeef9eSChristophe Leroy 	}
333a19ff1a2SFrederic Weisbecker 
334a19ff1a2SFrederic Weisbecker 	if (acct->idle_time)
33518b43a9bSFrederic Weisbecker 		account_idle_time(cputime_to_nsecs(acct->idle_time));
336a19ff1a2SFrederic Weisbecker 
337a19ff1a2SFrederic Weisbecker 	if (acct->stime)
338fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
339fb8b049cSFrederic Weisbecker 					  CPUTIME_SYSTEM);
340a19ff1a2SFrederic Weisbecker 
341a19ff1a2SFrederic Weisbecker 	if (acct->hardirq_time)
342fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
343fb8b049cSFrederic Weisbecker 					  CPUTIME_IRQ);
344a19ff1a2SFrederic Weisbecker 	if (acct->softirq_time)
345fb8b049cSFrederic Weisbecker 		account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
346fb8b049cSFrederic Weisbecker 					  CPUTIME_SOFTIRQ);
347a19ff1a2SFrederic Weisbecker 
348b38a181cSChristophe Leroy 	vtime_flush_scaled(tsk, acct);
349b38a181cSChristophe Leroy 
3508c8b73c4SFrederic Weisbecker 	acct->utime = 0;
351a19ff1a2SFrederic Weisbecker 	acct->gtime = 0;
352a19ff1a2SFrederic Weisbecker 	acct->idle_time = 0;
353a19ff1a2SFrederic Weisbecker 	acct->stime = 0;
354a19ff1a2SFrederic Weisbecker 	acct->hardirq_time = 0;
355a19ff1a2SFrederic Weisbecker 	acct->softirq_time = 0;
356c6622f63SPaul Mackerras }
357f985adafSNicholas Piggin #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
358c6622f63SPaul Mackerras 
__delay(unsigned long loops)3592a7ce82dSRohan McLure void __no_kcsan __delay(unsigned long loops)
3606defa38bSPaul Mackerras {
3616defa38bSPaul Mackerras 	unsigned long start;
3626defa38bSPaul Mackerras 
3634e287e65SNicholas Piggin 	spin_begin();
364a4c5a355SChristophe Leroy 	if (tb_invalid) {
365de269129SMahesh Salgaonkar 		/*
366de269129SMahesh Salgaonkar 		 * TB is in error state and isn't ticking anymore.
367de269129SMahesh Salgaonkar 		 * HMI handler was unable to recover from TB error.
368de269129SMahesh Salgaonkar 		 * Return immediately, so that kernel won't get stuck here.
369de269129SMahesh Salgaonkar 		 */
370de269129SMahesh Salgaonkar 		spin_cpu_relax();
3716defa38bSPaul Mackerras 	} else {
372942e8911SChristophe Leroy 		start = mftb();
373942e8911SChristophe Leroy 		while (mftb() - start < loops)
3744e287e65SNicholas Piggin 			spin_cpu_relax();
3756defa38bSPaul Mackerras 	}
3764e287e65SNicholas Piggin 	spin_end();
3776defa38bSPaul Mackerras }
3786defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
3796defa38bSPaul Mackerras 
udelay(unsigned long usecs)3802a7ce82dSRohan McLure void __no_kcsan udelay(unsigned long usecs)
3816defa38bSPaul Mackerras {
3826defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
3836defa38bSPaul Mackerras }
3846defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
3856defa38bSPaul Mackerras 
386f2783c15SPaul Mackerras #ifdef CONFIG_SMP
profile_pc(struct pt_regs * regs)387f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
388f2783c15SPaul Mackerras {
389f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
390f2783c15SPaul Mackerras 
391f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
392f2783c15SPaul Mackerras 		return regs->link;
393f2783c15SPaul Mackerras 
394f2783c15SPaul Mackerras 	return pc;
395f2783c15SPaul Mackerras }
396f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
397f2783c15SPaul Mackerras #endif
398f2783c15SPaul Mackerras 
399e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
400105988c0SPaul Mackerras 
4010fe1ac48SPaul Mackerras /*
4020fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
4030fe1ac48SPaul Mackerras  */
4040fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
test_irq_work_pending(void)40525aa1458SNicholas Piggin static inline unsigned long test_irq_work_pending(void)
40625aa1458SNicholas Piggin {
40725aa1458SNicholas Piggin 	unsigned long x;
40825aa1458SNicholas Piggin 
40925aa1458SNicholas Piggin 	asm volatile("lbz %0,%1(13)"
41025aa1458SNicholas Piggin 		: "=r" (x)
41125aa1458SNicholas Piggin 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
41225aa1458SNicholas Piggin 	return x;
41325aa1458SNicholas Piggin }
41425aa1458SNicholas Piggin 
set_irq_work_pending_flag(void)415e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
4160fe1ac48SPaul Mackerras {
4170fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4180fe1ac48SPaul Mackerras 		"r" (1),
419e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4200fe1ac48SPaul Mackerras }
4210fe1ac48SPaul Mackerras 
clear_irq_work_pending(void)422e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
4230fe1ac48SPaul Mackerras {
4240fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4250fe1ac48SPaul Mackerras 		"r" (0),
426e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4270fe1ac48SPaul Mackerras }
4280fe1ac48SPaul Mackerras 
4290fe1ac48SPaul Mackerras #else /* 32-bit */
4300fe1ac48SPaul Mackerras 
431e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
4320fe1ac48SPaul Mackerras 
43369111bacSChristoph Lameter #define set_irq_work_pending_flag()	__this_cpu_write(irq_work_pending, 1)
43469111bacSChristoph Lameter #define test_irq_work_pending()		__this_cpu_read(irq_work_pending)
43569111bacSChristoph Lameter #define clear_irq_work_pending()	__this_cpu_write(irq_work_pending, 0)
436105988c0SPaul Mackerras 
437abc3fce7SNicholas Piggin #endif /* 32 vs 64 bit */
438abc3fce7SNicholas Piggin 
arch_irq_work_raise(void)4394f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
4400fe1ac48SPaul Mackerras {
441abc3fce7SNicholas Piggin 	/*
442abc3fce7SNicholas Piggin 	 * 64-bit code that uses irq soft-mask can just cause an immediate
443abc3fce7SNicholas Piggin 	 * interrupt here that gets soft masked, if this is called under
444abc3fce7SNicholas Piggin 	 * local_irq_disable(). It might be possible to prevent that happening
445abc3fce7SNicholas Piggin 	 * by noticing interrupts are disabled and setting decrementer pending
446abc3fce7SNicholas Piggin 	 * to be replayed when irqs are enabled. The problem there is that
447abc3fce7SNicholas Piggin 	 * tracing can call irq_work_raise, including in code that does low
448abc3fce7SNicholas Piggin 	 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
449abc3fce7SNicholas Piggin 	 * which could get tangled up if we're messing with the same state
450abc3fce7SNicholas Piggin 	 * here.
451abc3fce7SNicholas Piggin 	 */
4520fe1ac48SPaul Mackerras 	preempt_disable();
453e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
4540fe1ac48SPaul Mackerras 	set_dec(1);
4550fe1ac48SPaul Mackerras 	preempt_enable();
4560fe1ac48SPaul Mackerras }
4570fe1ac48SPaul Mackerras 
set_dec_or_work(u64 val)45825aa1458SNicholas Piggin static void set_dec_or_work(u64 val)
45925aa1458SNicholas Piggin {
46025aa1458SNicholas Piggin 	set_dec(val);
46125aa1458SNicholas Piggin 	/* We may have raced with new irq work */
46225aa1458SNicholas Piggin 	if (unlikely(test_irq_work_pending()))
46325aa1458SNicholas Piggin 		set_dec(1);
46425aa1458SNicholas Piggin }
46525aa1458SNicholas Piggin 
466e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
467105988c0SPaul Mackerras 
468e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
469e360adbeSPeter Zijlstra #define clear_irq_work_pending()
470105988c0SPaul Mackerras 
set_dec_or_work(u64 val)47125aa1458SNicholas Piggin static void set_dec_or_work(u64 val)
47225aa1458SNicholas Piggin {
47325aa1458SNicholas Piggin 	set_dec(val);
47425aa1458SNicholas Piggin }
475e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
476105988c0SPaul Mackerras 
47725aa1458SNicholas Piggin #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
timer_rearm_host_dec(u64 now)47825aa1458SNicholas Piggin void timer_rearm_host_dec(u64 now)
47925aa1458SNicholas Piggin {
48025aa1458SNicholas Piggin 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
48125aa1458SNicholas Piggin 
48225aa1458SNicholas Piggin 	WARN_ON_ONCE(!arch_irqs_disabled());
48325aa1458SNicholas Piggin 	WARN_ON_ONCE(mfmsr() & MSR_EE);
48425aa1458SNicholas Piggin 
48525aa1458SNicholas Piggin 	if (now >= *next_tb) {
48625aa1458SNicholas Piggin 		local_paca->irq_happened |= PACA_IRQ_DEC;
48725aa1458SNicholas Piggin 	} else {
48825aa1458SNicholas Piggin 		now = *next_tb - now;
489cf74ff52SNicholas Piggin 		if (now > decrementer_max)
490cf74ff52SNicholas Piggin 			now = decrementer_max;
49125aa1458SNicholas Piggin 		set_dec_or_work(now);
49225aa1458SNicholas Piggin 	}
49325aa1458SNicholas Piggin }
49425aa1458SNicholas Piggin EXPORT_SYMBOL_GPL(timer_rearm_host_dec);
49525aa1458SNicholas Piggin #endif
49625aa1458SNicholas Piggin 
4971b783955SPreeti U Murthy /*
4981b783955SPreeti U Murthy  * timer_interrupt - gets called when the decrementer overflows,
4991b783955SPreeti U Murthy  * with interrupts disabled.
5001b783955SPreeti U Murthy  */
DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)5013a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)
5021b783955SPreeti U Murthy {
5033f984620SNicholas Piggin 	struct clock_event_device *evt = this_cpu_ptr(&decrementers);
50469111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
5053f984620SNicholas Piggin 	struct pt_regs *old_regs;
5063f984620SNicholas Piggin 	u64 now;
5071b783955SPreeti U Murthy 
50859d512e4SNicholas Piggin 	/*
50959d512e4SNicholas Piggin 	 * Some implementations of hotplug will get timer interrupts while
51059d512e4SNicholas Piggin 	 * offline, just ignore these.
5111b783955SPreeti U Murthy 	 */
512a7cba02dSNicholas Piggin 	if (unlikely(!cpu_online(smp_processor_id()))) {
513a7cba02dSNicholas Piggin 		set_dec(decrementer_max);
5141b783955SPreeti U Murthy 		return;
5151b783955SPreeti U Murthy 	}
5161b783955SPreeti U Murthy 
517c8455020SNicholas Piggin 	/* Conditionally hard-enable interrupts. */
518c2854801SNicholas Piggin 	if (should_hard_irq_enable(regs)) {
5190faf20a1SNicholas Piggin 		/*
5200faf20a1SNicholas Piggin 		 * Ensure a positive value is written to the decrementer, or
5210faf20a1SNicholas Piggin 		 * else some CPUs will continue to take decrementer exceptions.
5220faf20a1SNicholas Piggin 		 * When the PPC_WATCHDOG (decrementer based) is configured,
5230faf20a1SNicholas Piggin 		 * keep this at most 31 bits, which is about 4 seconds on most
5240faf20a1SNicholas Piggin 		 * systems, which gives the watchdog a chance of catching timer
5250faf20a1SNicholas Piggin 		 * interrupt hard lockups.
526a7cba02dSNicholas Piggin 		 */
527a7cba02dSNicholas Piggin 		if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
528a7cba02dSNicholas Piggin 			set_dec(0x7fffffff);
529a7cba02dSNicholas Piggin 		else
530a7cba02dSNicholas Piggin 			set_dec(decrementer_max);
531a7cba02dSNicholas Piggin 
5320faf20a1SNicholas Piggin 		do_hard_irq_enable();
533c8455020SNicholas Piggin 	}
5341b783955SPreeti U Murthy 
5356e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
5361b783955SPreeti U Murthy 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
53798694166SChristophe Leroy 		__do_IRQ(regs);
5381b783955SPreeti U Murthy #endif
5391b783955SPreeti U Murthy 
5401b783955SPreeti U Murthy 	old_regs = set_irq_regs(regs);
5411b1b6a6fSNicholas Piggin 
5423f984620SNicholas Piggin 	trace_timer_interrupt_entry(regs);
5431b783955SPreeti U Murthy 
5443f984620SNicholas Piggin 	if (test_irq_work_pending()) {
5453f984620SNicholas Piggin 		clear_irq_work_pending();
546cc15ff32SGanesh Goudar 		mce_run_irq_context_handlers();
5473f984620SNicholas Piggin 		irq_work_run();
5483f984620SNicholas Piggin 	}
5493f984620SNicholas Piggin 
5506601ec1cSChristophe Leroy 	now = get_tb();
5513f984620SNicholas Piggin 	if (now >= *next_tb) {
5523f984620SNicholas Piggin 		evt->event_handler(evt);
5533f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_event);
5543f984620SNicholas Piggin 	} else {
5553f984620SNicholas Piggin 		now = *next_tb - now;
5568defc2a5SNicholas Piggin 		if (now > decrementer_max)
5578defc2a5SNicholas Piggin 			now = decrementer_max;
55825aa1458SNicholas Piggin 		set_dec_or_work(now);
5593f984620SNicholas Piggin 		__this_cpu_inc(irq_stat.timer_irqs_others);
5603f984620SNicholas Piggin 	}
5613f984620SNicholas Piggin 
5623f984620SNicholas Piggin 	trace_timer_interrupt_exit(regs);
5631b1b6a6fSNicholas Piggin 
5647d12e780SDavid Howells 	set_irq_regs(old_regs);
565f2783c15SPaul Mackerras }
5669445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt);
567f2783c15SPaul Mackerras 
568bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
timer_broadcast_interrupt(void)5693f984620SNicholas Piggin void timer_broadcast_interrupt(void)
5703f984620SNicholas Piggin {
5713f984620SNicholas Piggin 	tick_receive_broadcast();
572e360cd37SNicholas Piggin 	__this_cpu_inc(irq_stat.broadcast_irqs_event);
5733f984620SNicholas Piggin }
574bc907113SNicholas Piggin #endif
5753f984620SNicholas Piggin 
5767ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
577e606a2f4SChristophe Leroy /* Overrides the weak version in kernel/power/main.c */
arch_suspend_disable_irqs(void)578e606a2f4SChristophe Leroy void arch_suspend_disable_irqs(void)
5797ac5dde9SScott Wood {
580e606a2f4SChristophe Leroy 	if (ppc_md.suspend_disable_irqs)
581e606a2f4SChristophe Leroy 		ppc_md.suspend_disable_irqs();
582e606a2f4SChristophe Leroy 
5837ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
5847ac5dde9SScott Wood 	 * with suspending.
5857ac5dde9SScott Wood 	 */
5867ac5dde9SScott Wood 
58779901024SOliver O'Halloran 	set_dec(decrementer_max);
5887ac5dde9SScott Wood 	local_irq_disable();
58979901024SOliver O'Halloran 	set_dec(decrementer_max);
5907ac5dde9SScott Wood }
5917ac5dde9SScott Wood 
5927ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
arch_suspend_enable_irqs(void)5937ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
5947ac5dde9SScott Wood {
595e606a2f4SChristophe Leroy 	local_irq_enable();
596e606a2f4SChristophe Leroy 
5977ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
5987ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
5997ac5dde9SScott Wood }
6007ac5dde9SScott Wood #endif
6017ac5dde9SScott Wood 
tb_to_ns(unsigned long long ticks)602b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks)
603b6c295dfSPaul Mackerras {
604b6c295dfSPaul Mackerras 	return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
605b6c295dfSPaul Mackerras }
606b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns);
607b6c295dfSPaul Mackerras 
608f2783c15SPaul Mackerras /*
609f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
610f2783c15SPaul Mackerras  *
611f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
612f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
613f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
614f2783c15SPaul Mackerras  */
sched_clock(void)6156b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void)
616f2783c15SPaul Mackerras {
617fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
618f2783c15SPaul Mackerras }
619f2783c15SPaul Mackerras 
6204be1b297SCyril Bur 
6214be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES
6224be1b297SCyril Bur 
6234be1b297SCyril Bur /*
6244be1b297SCyril Bur  * Running clock - attempts to give a view of time passing for a virtualised
6254be1b297SCyril Bur  * kernels.
6264be1b297SCyril Bur  * Uses the VTB register if available otherwise a next best guess.
6274be1b297SCyril Bur  */
running_clock(void)6284be1b297SCyril Bur unsigned long long running_clock(void)
6294be1b297SCyril Bur {
6304be1b297SCyril Bur 	/*
6314be1b297SCyril Bur 	 * Don't read the VTB as a host since KVM does not switch in host
6324be1b297SCyril Bur 	 * timebase into the VTB when it takes a guest off the CPU, reading the
6334be1b297SCyril Bur 	 * VTB would result in reading 'last switched out' guest VTB.
6344be1b297SCyril Bur 	 *
6354be1b297SCyril Bur 	 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
6364be1b297SCyril Bur 	 * would be unsafe to rely only on the #ifdef above.
6374be1b297SCyril Bur 	 */
6384be1b297SCyril Bur 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
6394be1b297SCyril Bur 	    cpu_has_feature(CPU_FTR_ARCH_207S))
6404be1b297SCyril Bur 		return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
6414be1b297SCyril Bur 
6424be1b297SCyril Bur 	/*
6434be1b297SCyril Bur 	 * This is a next best approximation without a VTB.
6444be1b297SCyril Bur 	 * On a host which is running bare metal there should never be any stolen
6454be1b297SCyril Bur 	 * time and on a host which doesn't do any virtualisation TB *should* equal
6464be1b297SCyril Bur 	 * VTB so it makes no difference anyway.
6474be1b297SCyril Bur 	 */
6489f3768e0SFrédéric Weisbecker 	return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
6494be1b297SCyril Bur }
6504be1b297SCyril Bur #endif
6514be1b297SCyril Bur 
get_freq(char * name,int cells,unsigned long * val)6520bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
653f2783c15SPaul Mackerras {
654f2783c15SPaul Mackerras 	struct device_node *cpu;
6556f7aba7bSAnton Blanchard 	const __be32 *fp;
6560bb474a4SAnton Blanchard 	int found = 0;
657f2783c15SPaul Mackerras 
6580bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
659f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
660f2783c15SPaul Mackerras 
661d8a8188dSOlaf Hering 	if (cpu) {
662e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
663d8a8188dSOlaf Hering 		if (fp) {
6640bb474a4SAnton Blanchard 			found = 1;
665a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
666f2783c15SPaul Mackerras 		}
6670bb474a4SAnton Blanchard 
6680bb474a4SAnton Blanchard 		of_node_put(cpu);
669f2783c15SPaul Mackerras 	}
6700bb474a4SAnton Blanchard 
6710bb474a4SAnton Blanchard 	return found;
6720bb474a4SAnton Blanchard }
6730bb474a4SAnton Blanchard 
start_cpu_decrementer(void)674e51df2c1SAnton Blanchard static void start_cpu_decrementer(void)
67577c0a700SBenjamin Herrenschmidt {
676047a6fd4SChristophe Leroy #ifdef CONFIG_BOOKE_OR_40x
6776e2f03e2SIvan Mikhaylov 	unsigned int tcr;
6786e2f03e2SIvan Mikhaylov 
67977c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
68077c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
68177c0a700SBenjamin Herrenschmidt 
6826e2f03e2SIvan Mikhaylov 	tcr = mfspr(SPRN_TCR);
6836e2f03e2SIvan Mikhaylov 	/*
6846e2f03e2SIvan Mikhaylov 	 * The watchdog may have already been enabled by u-boot. So leave
6856e2f03e2SIvan Mikhaylov 	 * TRC[WP] (Watchdog Period) alone.
6866e2f03e2SIvan Mikhaylov 	 */
6876e2f03e2SIvan Mikhaylov 	tcr &= TCR_WP_MASK;	/* Clear all bits except for TCR[WP] */
6886e2f03e2SIvan Mikhaylov 	tcr |= TCR_DIE;		/* Enable decrementer */
6896e2f03e2SIvan Mikhaylov 	mtspr(SPRN_TCR, tcr);
6906e2f03e2SIvan Mikhaylov #endif
69177c0a700SBenjamin Herrenschmidt }
69277c0a700SBenjamin Herrenschmidt 
generic_calibrate_decr(void)6930bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
6940bb474a4SAnton Blanchard {
6950bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
6960bb474a4SAnton Blanchard 
6970bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
6980bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
6990bb474a4SAnton Blanchard 
700f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
701f2783c15SPaul Mackerras 				"(not found)\n");
7020bb474a4SAnton Blanchard 	}
703f2783c15SPaul Mackerras 
7040bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
7050bb474a4SAnton Blanchard 
7060bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
7070bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
7080bb474a4SAnton Blanchard 
7090bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
7100bb474a4SAnton Blanchard 				"(not found)\n");
711f2783c15SPaul Mackerras 	}
712f2783c15SPaul Mackerras }
713f2783c15SPaul Mackerras 
update_persistent_clock64(struct timespec64 now)7145235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now)
715f2783c15SPaul Mackerras {
716f2783c15SPaul Mackerras 	struct rtc_time tm;
717f2783c15SPaul Mackerras 
718aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
719023f333aSJason Gunthorpe 		return -ENODEV;
720aa3be5f3STony Breeds 
7215235afa8SArnd Bergmann 	rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
722aa3be5f3STony Breeds 
723aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
724aa3be5f3STony Breeds }
725aa3be5f3STony Breeds 
__read_persistent_clock(struct timespec64 * ts)7265bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts)
727aa3be5f3STony Breeds {
728aa3be5f3STony Breeds 	struct rtc_time tm;
729aa3be5f3STony Breeds 	static int first = 1;
730aa3be5f3STony Breeds 
731d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
7321fd02f66SJulia Lawall 	/* XXX this is a little fragile but will work okay in the short term */
733aa3be5f3STony Breeds 	if (first) {
734aa3be5f3STony Breeds 		first = 0;
735aa3be5f3STony Breeds 		if (ppc_md.time_init)
736aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
737aa3be5f3STony Breeds 
738aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
739d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
740d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
741d90246cdSMartin Schwidefsky 			return;
742aa3be5f3STony Breeds 		}
743d90246cdSMartin Schwidefsky 	}
744d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
745d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
746d90246cdSMartin Schwidefsky 		return;
747d90246cdSMartin Schwidefsky 	}
748f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
749978d7eb3SBenjamin Herrenschmidt 
7505bfd6435SArnd Bergmann 	ts->tv_sec = rtc_tm_to_time64(&tm);
751f2783c15SPaul Mackerras }
752f2783c15SPaul Mackerras 
read_persistent_clock64(struct timespec64 * ts)7535bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts)
754978d7eb3SBenjamin Herrenschmidt {
755978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
756978d7eb3SBenjamin Herrenschmidt 
757978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
758978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
759978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
760978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
761978d7eb3SBenjamin Herrenschmidt 	}
762978d7eb3SBenjamin Herrenschmidt 
763978d7eb3SBenjamin Herrenschmidt }
764978d7eb3SBenjamin Herrenschmidt 
7654a4cfe38STony Breeds /* clocksource code */
timebase_read(struct clocksource * cs)7666b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs)
7674a4cfe38STony Breeds {
768a5a1d1c2SThomas Gleixner 	return (u64)get_tb();
7694a4cfe38STony Breeds }
7704a4cfe38STony Breeds 
clocksource_init(void)7711c21a293SMichael Ellerman static void __init clocksource_init(void)
7724a4cfe38STony Breeds {
773a4c5a355SChristophe Leroy 	struct clocksource *clock = &clocksource_timebase;
7744a4cfe38STony Breeds 
77511b8633aSAnton Blanchard 	if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
7764a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
7774a4cfe38STony Breeds 		       clock->name);
7784a4cfe38STony Breeds 		return;
7794a4cfe38STony Breeds 	}
7804a4cfe38STony Breeds 
7814a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
7824a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
7834a4cfe38STony Breeds }
7844a4cfe38STony Breeds 
decrementer_set_next_event(unsigned long evt,struct clock_event_device * dev)785d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
786d831d0b8STony Breeds 				      struct clock_event_device *dev)
787d831d0b8STony Breeds {
7886601ec1cSChristophe Leroy 	__this_cpu_write(decrementers_next_tb, get_tb() + evt);
78925aa1458SNicholas Piggin 	set_dec_or_work(evt);
7900215f7d8SBenjamin Herrenschmidt 
791d831d0b8STony Breeds 	return 0;
792d831d0b8STony Breeds }
793d831d0b8STony Breeds 
decrementer_shutdown(struct clock_event_device * dev)79437a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev)
795d831d0b8STony Breeds {
79635de589cSNicholas Piggin 	__this_cpu_write(decrementers_next_tb, DEC_CLOCKEVENT_STOPPED);
79735de589cSNicholas Piggin 	set_dec_or_work(decrementer_max);
79835de589cSNicholas Piggin 
79937a13e78SViresh Kumar 	return 0;
800d831d0b8STony Breeds }
801d831d0b8STony Breeds 
register_decrementer_clockevent(int cpu)802d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
803d831d0b8STony Breeds {
8047df10275SAnton Blanchard 	struct clock_event_device *dec = &per_cpu(decrementers, cpu);
805d831d0b8STony Breeds 
806d831d0b8STony Breeds 	*dec = decrementer_clockevent;
807320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
808d831d0b8STony Breeds 
8098b78fdb0SAnton Blanchard 	clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
8108b78fdb0SAnton Blanchard 
811b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
812d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
813b4d16ab5SMichael Ellerman 
814b4d16ab5SMichael Ellerman 	/* Set values for KVM, see kvm_emulate_dec() */
815b4d16ab5SMichael Ellerman 	decrementer_clockevent.mult = dec->mult;
816b4d16ab5SMichael Ellerman 	decrementer_clockevent.shift = dec->shift;
817d831d0b8STony Breeds }
818d831d0b8STony Breeds 
enable_large_decrementer(void)81979901024SOliver O'Halloran static void enable_large_decrementer(void)
82079901024SOliver O'Halloran {
82179901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
82279901024SOliver O'Halloran 		return;
82379901024SOliver O'Halloran 
82479901024SOliver O'Halloran 	if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
82579901024SOliver O'Halloran 		return;
82679901024SOliver O'Halloran 
82779901024SOliver O'Halloran 	/*
82879901024SOliver O'Halloran 	 * If we're running as the hypervisor we need to enable the LD manually
82979901024SOliver O'Halloran 	 * otherwise firmware should have done it for us.
83079901024SOliver O'Halloran 	 */
83179901024SOliver O'Halloran 	if (cpu_has_feature(CPU_FTR_HVMODE))
83279901024SOliver O'Halloran 		mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
83379901024SOliver O'Halloran }
83479901024SOliver O'Halloran 
set_decrementer_max(void)83579901024SOliver O'Halloran static void __init set_decrementer_max(void)
83679901024SOliver O'Halloran {
83779901024SOliver O'Halloran 	struct device_node *cpu;
83879901024SOliver O'Halloran 	u32 bits = 32;
83979901024SOliver O'Halloran 
84079901024SOliver O'Halloran 	/* Prior to ISAv3 the decrementer is always 32 bit */
84179901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
84279901024SOliver O'Halloran 		return;
84379901024SOliver O'Halloran 
84479901024SOliver O'Halloran 	cpu = of_find_node_by_type(NULL, "cpu");
84579901024SOliver O'Halloran 
84679901024SOliver O'Halloran 	if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
84779901024SOliver O'Halloran 		if (bits > 64 || bits < 32) {
84879901024SOliver O'Halloran 			pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
84979901024SOliver O'Halloran 			bits = 32;
85079901024SOliver O'Halloran 		}
85179901024SOliver O'Halloran 
85279901024SOliver O'Halloran 		/* calculate the signed maximum given this many bits */
85379901024SOliver O'Halloran 		decrementer_max = (1ul << (bits - 1)) - 1;
85479901024SOliver O'Halloran 	}
85579901024SOliver O'Halloran 
85679901024SOliver O'Halloran 	of_node_put(cpu);
85779901024SOliver O'Halloran 
85879901024SOliver O'Halloran 	pr_info("time_init: %u bit decrementer (max: %llx)\n",
85979901024SOliver O'Halloran 		bits, decrementer_max);
86079901024SOliver O'Halloran }
86179901024SOliver O'Halloran 
init_decrementer_clockevent(void)862c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
863d831d0b8STony Breeds {
8648b78fdb0SAnton Blanchard 	register_decrementer_clockevent(smp_processor_id());
865d831d0b8STony Breeds }
866d831d0b8STony Breeds 
secondary_cpu_time_init(void)867d831d0b8STony Breeds void secondary_cpu_time_init(void)
868d831d0b8STony Breeds {
86979901024SOliver O'Halloran 	/* Enable and test the large decrementer for this cpu */
87079901024SOliver O'Halloran 	enable_large_decrementer();
87179901024SOliver O'Halloran 
87277c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
87377c0a700SBenjamin Herrenschmidt 	 * such as BookE
87477c0a700SBenjamin Herrenschmidt 	 */
87577c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
87677c0a700SBenjamin Herrenschmidt 
8771fd02f66SJulia Lawall 	/* FIME: Should make unrelated change to move snapshot_timebase
878d831d0b8STony Breeds 	 * call here ! */
879d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
880d831d0b8STony Breeds }
881d831d0b8STony Breeds 
882f2783c15SPaul Mackerras /* This function is only called on the boot processor */
time_init(void)883f2783c15SPaul Mackerras void __init time_init(void)
884f2783c15SPaul Mackerras {
885f2783c15SPaul Mackerras 	struct div_result res;
886d75d68cfSPaul Mackerras 	u64 scale;
887f2783c15SPaul Mackerras 	unsigned shift;
888f2783c15SPaul Mackerras 
88996c44507SPaul Mackerras 	/* Normal PowerPC with timebase register */
890*0aafbdf3SChristophe Leroy 	if (ppc_md.calibrate_decr)
891f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
892*0aafbdf3SChristophe Leroy 	else
893*0aafbdf3SChristophe Leroy 		generic_calibrate_decr();
894*0aafbdf3SChristophe Leroy 
895224ad80aSOlof Johansson 	printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
896374e99d4SPaul Mackerras 	       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
897224ad80aSOlof Johansson 	printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
898374e99d4SPaul Mackerras 	       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
899374e99d4SPaul Mackerras 
900374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
901092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
902374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
903092b8f34SPaul Mackerras 
904092b8f34SPaul Mackerras 	/*
905f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
906f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
907f2783c15SPaul Mackerras 	 * which is the timebase frequency.
908f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
909f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
910f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
911f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
912f2783c15SPaul Mackerras 	 * sched_clock().
913f2783c15SPaul Mackerras 	 */
914f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
915f2783c15SPaul Mackerras 	scale = res.result_low;
916f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
917f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
918f2783c15SPaul Mackerras 		res.result_high >>= 1;
919f2783c15SPaul Mackerras 	}
920f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
921f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
922fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
9236601ec1cSChristophe Leroy 	boot_tb = get_tb();
924f2783c15SPaul Mackerras 
925092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
926092b8f34SPaul Mackerras 	if (timezone_offset) {
927092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
928092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
929092b8f34SPaul Mackerras 	}
930092b8f34SPaul Mackerras 
931a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
932f2783c15SPaul Mackerras 
93379901024SOliver O'Halloran 	/* initialise and enable the large decrementer (if we have one) */
93479901024SOliver O'Halloran 	set_decrementer_max();
93579901024SOliver O'Halloran 	enable_large_decrementer();
93679901024SOliver O'Halloran 
93777c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
93877c0a700SBenjamin Herrenschmidt 	 * such as BookE
93977c0a700SBenjamin Herrenschmidt 	 */
94077c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
94177c0a700SBenjamin Herrenschmidt 
942f5339277SStephen Rothwell 	/* Register the clocksource */
9434a4cfe38STony Breeds 	clocksource_init();
9444a4cfe38STony Breeds 
945d831d0b8STony Breeds 	init_decrementer_clockevent();
9460d948730SPreeti U Murthy 	tick_setup_hrtimer_broadcast();
947f0d37300SKevin Hao 
948f0d37300SKevin Hao 	of_clk_init(NULL);
949b709e32eSPingfan Liu 	enable_sched_clock_irqtime();
950f2783c15SPaul Mackerras }
951f2783c15SPaul Mackerras 
952f2783c15SPaul Mackerras /*
953f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
954f2783c15SPaul Mackerras  * result.
955f2783c15SPaul Mackerras  */
div128_by_32(u64 dividend_high,u64 dividend_low,unsigned divisor,struct div_result * dr)956f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
957f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
958f2783c15SPaul Mackerras {
959f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
960f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
961f2783c15SPaul Mackerras 	u64 ra, rb, rc;
962f2783c15SPaul Mackerras 
963f2783c15SPaul Mackerras 	a = dividend_high >> 32;
964f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
965f2783c15SPaul Mackerras 	c = dividend_low >> 32;
966f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
967f2783c15SPaul Mackerras 
968f2783c15SPaul Mackerras 	w = a / divisor;
969f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
970f2783c15SPaul Mackerras 
971f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
972f2783c15SPaul Mackerras 	x = ra;
973f2783c15SPaul Mackerras 
974f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
975f2783c15SPaul Mackerras 	y = rb;
976f2783c15SPaul Mackerras 
977f2783c15SPaul Mackerras 	do_div(rc, divisor);
978f2783c15SPaul Mackerras 	z = rc;
979f2783c15SPaul Mackerras 
980f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
981f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
982f2783c15SPaul Mackerras 
983f2783c15SPaul Mackerras }
984bcd68a70SGeert Uytterhoeven 
985177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
calibrate_delay(void)986177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
987177996e6SBenjamin Herrenschmidt {
988177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
989177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
990177996e6SBenjamin Herrenschmidt 	 */
991177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
992177996e6SBenjamin Herrenschmidt }
993177996e6SBenjamin Herrenschmidt 
994169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
rtc_generic_get_time(struct device * dev,struct rtc_time * tm)995169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
996169047f4SArnd Bergmann {
997169047f4SArnd Bergmann 	ppc_md.get_rtc_time(tm);
998890ae797SAlexandre Belloni 	return 0;
999169047f4SArnd Bergmann }
1000169047f4SArnd Bergmann 
rtc_generic_set_time(struct device * dev,struct rtc_time * tm)1001169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1002169047f4SArnd Bergmann {
1003169047f4SArnd Bergmann 	if (!ppc_md.set_rtc_time)
1004169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1005169047f4SArnd Bergmann 
1006169047f4SArnd Bergmann 	if (ppc_md.set_rtc_time(tm) < 0)
1007169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1008169047f4SArnd Bergmann 
1009169047f4SArnd Bergmann 	return 0;
1010169047f4SArnd Bergmann }
1011169047f4SArnd Bergmann 
1012169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = {
1013169047f4SArnd Bergmann 	.read_time = rtc_generic_get_time,
1014169047f4SArnd Bergmann 	.set_time = rtc_generic_set_time,
1015169047f4SArnd Bergmann };
1016169047f4SArnd Bergmann 
rtc_init(void)1017bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1018bcd68a70SGeert Uytterhoeven {
1019bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1020bcd68a70SGeert Uytterhoeven 
1021bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1022bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1023bcd68a70SGeert Uytterhoeven 
1024169047f4SArnd Bergmann 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1025169047f4SArnd Bergmann 					     &rtc_generic_ops,
1026169047f4SArnd Bergmann 					     sizeof(rtc_generic_ops));
1027bcd68a70SGeert Uytterhoeven 
10288c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(pdev);
1029bcd68a70SGeert Uytterhoeven }
1030bcd68a70SGeert Uytterhoeven 
10318f6b9512SPaul Gortmaker device_initcall(rtc_init);
1032169047f4SArnd Bergmann #endif
1033