xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision 40565b5aedd6d0ca88b7dfd3859d709d2f6f8cf9)
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>
37f2783c15SPaul Mackerras #include <linux/kernel.h>
38f2783c15SPaul Mackerras #include <linux/param.h>
39f2783c15SPaul Mackerras #include <linux/string.h>
40f2783c15SPaul Mackerras #include <linux/mm.h>
41f2783c15SPaul Mackerras #include <linux/interrupt.h>
42f2783c15SPaul Mackerras #include <linux/timex.h>
43f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
44f2783c15SPaul Mackerras #include <linux/time.h>
450d948730SPreeti U Murthy #include <linux/clockchips.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>
59169047f4SArnd Bergmann #include <linux/rtc.h>
606795b85cSAnton Blanchard #include <asm/trace.h>
61f2783c15SPaul Mackerras 
62f2783c15SPaul Mackerras #include <asm/io.h>
63f2783c15SPaul Mackerras #include <asm/processor.h>
64f2783c15SPaul Mackerras #include <asm/nvram.h>
65f2783c15SPaul Mackerras #include <asm/cache.h>
66f2783c15SPaul Mackerras #include <asm/machdep.h>
67f2783c15SPaul Mackerras #include <asm/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>
7506b8e878SMichael Neuling #include <asm/cputime.h>
760545d543SDaniel Axtens #include <asm/asm-prototypes.h>
77f2783c15SPaul Mackerras 
784a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
794a4cfe38STony Breeds 
80d831d0b8STony Breeds #include <linux/clockchips.h>
81189374aeSJohn Stultz #include <linux/timekeeper_internal.h>
824a4cfe38STony Breeds 
838e19608eSMagnus Damm static cycle_t rtc_read(struct clocksource *);
844a4cfe38STony Breeds static struct clocksource clocksource_rtc = {
854a4cfe38STony Breeds 	.name         = "rtc",
864a4cfe38STony Breeds 	.rating       = 400,
874a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
884a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
894a4cfe38STony Breeds 	.read         = rtc_read,
904a4cfe38STony Breeds };
914a4cfe38STony Breeds 
928e19608eSMagnus Damm static cycle_t timebase_read(struct clocksource *);
934a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
944a4cfe38STony Breeds 	.name         = "timebase",
954a4cfe38STony Breeds 	.rating       = 400,
964a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
974a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
984a4cfe38STony Breeds 	.read         = timebase_read,
994a4cfe38STony Breeds };
1004a4cfe38STony Breeds 
10179901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
10279901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
103d831d0b8STony Breeds 
104d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
105d831d0b8STony Breeds 				      struct clock_event_device *dev);
10637a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt);
107d831d0b8STony Breeds 
1086e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = {
109d831d0b8STony Breeds 	.name			= "decrementer",
110d831d0b8STony Breeds 	.rating			= 200,
111d831d0b8STony Breeds 	.irq			= 0,
112d831d0b8STony Breeds 	.set_next_event		= decrementer_set_next_event,
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 
153abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
154c6622f63SPaul Mackerras /*
155c6622f63SPaul Mackerras  * Factors for converting from cputime_t (timebase ticks) to
1569f5072d4SAndreas Schwab  * jiffies, microseconds, seconds, and clock_t (1/USER_HZ seconds).
157c6622f63SPaul Mackerras  * These are all stored as 0.64 fixed-point binary fractions.
158c6622f63SPaul Mackerras  */
159c6622f63SPaul Mackerras u64 __cputime_jiffies_factor;
1602cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_jiffies_factor);
1619f5072d4SAndreas Schwab u64 __cputime_usec_factor;
1629f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor);
163c6622f63SPaul Mackerras u64 __cputime_sec_factor;
1642cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_sec_factor);
165c6622f63SPaul Mackerras u64 __cputime_clockt_factor;
1662cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_clockt_factor);
167c6622f63SPaul Mackerras 
168a42548a1SStanislaw Gruszka cputime_t cputime_one_jiffy;
169a42548a1SStanislaw Gruszka 
170c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR
171872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64);
172c223c903SChristophe Leroy #endif
173c223c903SChristophe Leroy 
174c223c903SChristophe Leroy #ifdef CONFIG_PPC64
175c223c903SChristophe Leroy #define get_accounting(tsk)	(&get_paca()->accounting)
176c223c903SChristophe Leroy #else
177c223c903SChristophe Leroy #define get_accounting(tsk)	(&task_thread_info(tsk)->accounting)
178c223c903SChristophe Leroy #endif
179872e439aSPaul Mackerras 
180c6622f63SPaul Mackerras static void calc_cputime_factors(void)
181c6622f63SPaul Mackerras {
182c6622f63SPaul Mackerras 	struct div_result res;
183c6622f63SPaul Mackerras 
184c6622f63SPaul Mackerras 	div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
185c6622f63SPaul Mackerras 	__cputime_jiffies_factor = res.result_low;
1869f5072d4SAndreas Schwab 	div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
1879f5072d4SAndreas Schwab 	__cputime_usec_factor = res.result_low;
188c6622f63SPaul Mackerras 	div128_by_32(1, 0, tb_ticks_per_sec, &res);
189c6622f63SPaul Mackerras 	__cputime_sec_factor = res.result_low;
190c6622f63SPaul Mackerras 	div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
191c6622f63SPaul Mackerras 	__cputime_clockt_factor = res.result_low;
192c6622f63SPaul Mackerras }
193c6622f63SPaul Mackerras 
194c6622f63SPaul Mackerras /*
195cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
196cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
197c6622f63SPaul Mackerras  */
198c223c903SChristophe Leroy static unsigned long read_spurr(unsigned long tb)
199c6622f63SPaul Mackerras {
200cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
201cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
202c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
203c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
204cf9efce0SPaul Mackerras 	return tb;
205cf9efce0SPaul Mackerras }
206cf9efce0SPaul Mackerras 
207cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR
208cf9efce0SPaul Mackerras 
209cf9efce0SPaul Mackerras /*
210cf9efce0SPaul Mackerras  * Scan the dispatch trace log and count up the stolen time.
211cf9efce0SPaul Mackerras  * Should be called with interrupts disabled.
212cf9efce0SPaul Mackerras  */
213cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb)
214cf9efce0SPaul Mackerras {
215872e439aSPaul Mackerras 	u64 i = local_paca->dtl_ridx;
216cf9efce0SPaul Mackerras 	struct dtl_entry *dtl = local_paca->dtl_curr;
217cf9efce0SPaul Mackerras 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
218cf9efce0SPaul Mackerras 	struct lppaca *vpa = local_paca->lppaca_ptr;
219cf9efce0SPaul Mackerras 	u64 tb_delta;
220cf9efce0SPaul Mackerras 	u64 stolen = 0;
221cf9efce0SPaul Mackerras 	u64 dtb;
222cf9efce0SPaul Mackerras 
22384ffae55SAnton Blanchard 	if (!dtl)
22484ffae55SAnton Blanchard 		return 0;
22584ffae55SAnton Blanchard 
2267ffcf8ecSAnton Blanchard 	if (i == be64_to_cpu(vpa->dtl_idx))
227cf9efce0SPaul Mackerras 		return 0;
2287ffcf8ecSAnton Blanchard 	while (i < be64_to_cpu(vpa->dtl_idx)) {
2297ffcf8ecSAnton Blanchard 		dtb = be64_to_cpu(dtl->timebase);
2307ffcf8ecSAnton Blanchard 		tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
2317ffcf8ecSAnton Blanchard 			be32_to_cpu(dtl->ready_to_enqueue_time);
232cf9efce0SPaul Mackerras 		barrier();
2337ffcf8ecSAnton Blanchard 		if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
234cf9efce0SPaul Mackerras 			/* buffer has overflowed */
2357ffcf8ecSAnton Blanchard 			i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
236cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
237cf9efce0SPaul Mackerras 			continue;
238cf9efce0SPaul Mackerras 		}
239cf9efce0SPaul Mackerras 		if (dtb > stop_tb)
240cf9efce0SPaul Mackerras 			break;
24184b07386SAnton Blanchard 		if (dtl_consumer)
24284b07386SAnton Blanchard 			dtl_consumer(dtl, i);
243cf9efce0SPaul Mackerras 		stolen += tb_delta;
244cf9efce0SPaul Mackerras 		++i;
245cf9efce0SPaul Mackerras 		++dtl;
246cf9efce0SPaul Mackerras 		if (dtl == dtl_end)
247cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log;
248cf9efce0SPaul Mackerras 	}
249cf9efce0SPaul Mackerras 	local_paca->dtl_ridx = i;
250cf9efce0SPaul Mackerras 	local_paca->dtl_curr = dtl;
251cf9efce0SPaul Mackerras 	return stolen;
252c6622f63SPaul Mackerras }
253c6622f63SPaul Mackerras 
254c6622f63SPaul Mackerras /*
255cf9efce0SPaul Mackerras  * Accumulate stolen time by scanning the dispatch trace log.
256cf9efce0SPaul Mackerras  * Called on entry from user mode.
2574603ac18SMichael Neuling  */
258cf9efce0SPaul Mackerras void accumulate_stolen_time(void)
2594603ac18SMichael Neuling {
260cf9efce0SPaul Mackerras 	u64 sst, ust;
261b18ae08dSTejun Heo 	u8 save_soft_enabled = local_paca->soft_enabled;
262c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = &local_paca->accounting;
263b18ae08dSTejun Heo 
264b18ae08dSTejun Heo 	/* We are called early in the exception entry, before
265b18ae08dSTejun Heo 	 * soft/hard_enabled are sync'ed to the expected state
266b18ae08dSTejun Heo 	 * for the exception. We are hard disabled but the PACA
267b18ae08dSTejun Heo 	 * needs to reflect that so various debug stuff doesn't
268b18ae08dSTejun Heo 	 * complain
269b18ae08dSTejun Heo 	 */
270b18ae08dSTejun Heo 	local_paca->soft_enabled = 0;
271b18ae08dSTejun Heo 
272c223c903SChristophe Leroy 	sst = scan_dispatch_log(acct->starttime_user);
273c223c903SChristophe Leroy 	ust = scan_dispatch_log(acct->starttime);
274c223c903SChristophe Leroy 	acct->system_time -= sst;
275c223c903SChristophe Leroy 	acct->user_time -= ust;
276b18ae08dSTejun Heo 	local_paca->stolen_time += ust + sst;
277b18ae08dSTejun Heo 
278b18ae08dSTejun Heo 	local_paca->soft_enabled = save_soft_enabled;
2794603ac18SMichael Neuling }
2804603ac18SMichael Neuling 
281cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
282cf9efce0SPaul Mackerras {
283cf9efce0SPaul Mackerras 	u64 stolen = 0;
284cf9efce0SPaul Mackerras 
2857ffcf8ecSAnton Blanchard 	if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) {
286cf9efce0SPaul Mackerras 		stolen = scan_dispatch_log(stop_tb);
287c223c903SChristophe Leroy 		get_paca()->accounting.system_time -= stolen;
288cf9efce0SPaul Mackerras 	}
289cf9efce0SPaul Mackerras 
290cf9efce0SPaul Mackerras 	stolen += get_paca()->stolen_time;
291cf9efce0SPaul Mackerras 	get_paca()->stolen_time = 0;
292cf9efce0SPaul Mackerras 	return stolen;
293cf9efce0SPaul Mackerras }
294cf9efce0SPaul Mackerras 
295cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */
296cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
297cf9efce0SPaul Mackerras {
298cf9efce0SPaul Mackerras 	return 0;
299cf9efce0SPaul Mackerras }
300cf9efce0SPaul Mackerras 
301cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
302cf9efce0SPaul Mackerras 
3034603ac18SMichael Neuling /*
304c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
305c6622f63SPaul Mackerras  * or soft irq state.
306c6622f63SPaul Mackerras  */
307c223c903SChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk,
308c223c903SChristophe Leroy 				 unsigned long *sys_scaled,
309c223c903SChristophe Leroy 				 unsigned long *stolen)
310c6622f63SPaul Mackerras {
311c223c903SChristophe Leroy 	unsigned long now, nowscaled, deltascaled;
312c223c903SChristophe Leroy 	unsigned long udelta, delta, user_scaled;
313c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
314c6622f63SPaul Mackerras 
3151b2852b1SFrederic Weisbecker 	WARN_ON_ONCE(!irqs_disabled());
3161b2852b1SFrederic Weisbecker 
317cf9efce0SPaul Mackerras 	now = mftb();
3184603ac18SMichael Neuling 	nowscaled = read_spurr(now);
319c223c903SChristophe Leroy 	acct->system_time += now - acct->starttime;
320c223c903SChristophe Leroy 	acct->starttime = now;
321c223c903SChristophe Leroy 	deltascaled = nowscaled - acct->startspurr;
322c223c903SChristophe Leroy 	acct->startspurr = nowscaled;
323cf9efce0SPaul Mackerras 
324a7e1a9e3SFrederic Weisbecker 	*stolen = calculate_stolen_time(now);
325cf9efce0SPaul Mackerras 
326c223c903SChristophe Leroy 	delta = acct->system_time;
327c223c903SChristophe Leroy 	acct->system_time = 0;
328c223c903SChristophe Leroy 	udelta = acct->user_time - acct->utime_sspurr;
329c223c903SChristophe Leroy 	acct->utime_sspurr = acct->user_time;
330cf9efce0SPaul Mackerras 
331cf9efce0SPaul Mackerras 	/*
332cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
333cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
334cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
335cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
336cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
337cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
338cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
339cf9efce0SPaul Mackerras 	 * used by account_process_tick.
340cf9efce0SPaul Mackerras 	 */
341a7e1a9e3SFrederic Weisbecker 	*sys_scaled = delta;
342cf9efce0SPaul Mackerras 	user_scaled = udelta;
343cf9efce0SPaul Mackerras 	if (deltascaled != delta + udelta) {
344cf9efce0SPaul Mackerras 		if (udelta) {
345a7e1a9e3SFrederic Weisbecker 			*sys_scaled = deltascaled * delta / (delta + udelta);
346a7e1a9e3SFrederic Weisbecker 			user_scaled = deltascaled - *sys_scaled;
347cf9efce0SPaul Mackerras 		} else {
348a7e1a9e3SFrederic Weisbecker 			*sys_scaled = deltascaled;
349c6622f63SPaul Mackerras 		}
350cf9efce0SPaul Mackerras 	}
351c223c903SChristophe Leroy 	acct->user_time_scaled += user_scaled;
352cf9efce0SPaul Mackerras 
353a7e1a9e3SFrederic Weisbecker 	return delta;
354a7e1a9e3SFrederic Weisbecker }
355a7e1a9e3SFrederic Weisbecker 
356fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk)
357a7e1a9e3SFrederic Weisbecker {
358c223c903SChristophe Leroy 	unsigned long delta, sys_scaled, stolen;
359a7e1a9e3SFrederic Weisbecker 
360a7e1a9e3SFrederic Weisbecker 	delta = vtime_delta(tsk, &sys_scaled, &stolen);
361*40565b5aSStanislaw Gruszka 	account_system_time(tsk, 0, delta);
362*40565b5aSStanislaw Gruszka 	tsk->stimescaled += sys_scaled;
363cf9efce0SPaul Mackerras 	if (stolen)
364cf9efce0SPaul Mackerras 		account_steal_time(stolen);
365a7e1a9e3SFrederic Weisbecker }
366c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system);
367a7e1a9e3SFrederic Weisbecker 
368fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk)
369a7e1a9e3SFrederic Weisbecker {
370c223c903SChristophe Leroy 	unsigned long delta, sys_scaled, stolen;
371a7e1a9e3SFrederic Weisbecker 
372a7e1a9e3SFrederic Weisbecker 	delta = vtime_delta(tsk, &sys_scaled, &stolen);
373cf9efce0SPaul Mackerras 	account_idle_time(delta + stolen);
374cf9efce0SPaul Mackerras }
375c6622f63SPaul Mackerras 
376c6622f63SPaul Mackerras /*
377bcebdf84SFrederic Weisbecker  * Transfer the user time accumulated in the paca
378bcebdf84SFrederic Weisbecker  * by the exception entry and exit code to the generic
379bcebdf84SFrederic Weisbecker  * process user time records.
380c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
381bcebdf84SFrederic Weisbecker  * Assumes that vtime_account_system/idle() has been called
382bcebdf84SFrederic Weisbecker  * recently (i.e. since the last entry from usermode) so that
383cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
384c6622f63SPaul Mackerras  */
385bcebdf84SFrederic Weisbecker void vtime_account_user(struct task_struct *tsk)
386c6622f63SPaul Mackerras {
3874603ac18SMichael Neuling 	cputime_t utime, utimescaled;
388c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(tsk);
389c6622f63SPaul Mackerras 
390c223c903SChristophe Leroy 	utime = acct->user_time;
391c223c903SChristophe Leroy 	utimescaled = acct->user_time_scaled;
392c223c903SChristophe Leroy 	acct->user_time = 0;
393c223c903SChristophe Leroy 	acct->user_time_scaled = 0;
394c223c903SChristophe Leroy 	acct->utime_sspurr = 0;
395*40565b5aSStanislaw Gruszka 	account_user_time(tsk, utime);
396*40565b5aSStanislaw Gruszka 	tsk->utimescaled += utimescaled;
397c6622f63SPaul Mackerras }
398c6622f63SPaul Mackerras 
399c223c903SChristophe Leroy #ifdef CONFIG_PPC32
400c223c903SChristophe Leroy /*
401c223c903SChristophe Leroy  * Called from the context switch with interrupts disabled, to charge all
402c223c903SChristophe Leroy  * accumulated times to the current process, and to prepare accounting on
403c223c903SChristophe Leroy  * the next process.
404c223c903SChristophe Leroy  */
405c223c903SChristophe Leroy void arch_vtime_task_switch(struct task_struct *prev)
406c223c903SChristophe Leroy {
407c223c903SChristophe Leroy 	struct cpu_accounting_data *acct = get_accounting(current);
408c223c903SChristophe Leroy 
409c223c903SChristophe Leroy 	acct->starttime = get_accounting(prev)->starttime;
410c223c903SChristophe Leroy 	acct->system_time = 0;
411c223c903SChristophe Leroy 	acct->user_time = 0;
412c223c903SChristophe Leroy }
413c223c903SChristophe Leroy #endif /* CONFIG_PPC32 */
414c223c903SChristophe Leroy 
415abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
416c6622f63SPaul Mackerras #define calc_cputime_factors()
417c6622f63SPaul Mackerras #endif
418c6622f63SPaul Mackerras 
4196defa38bSPaul Mackerras void __delay(unsigned long loops)
4206defa38bSPaul Mackerras {
4216defa38bSPaul Mackerras 	unsigned long start;
4226defa38bSPaul Mackerras 	int diff;
4236defa38bSPaul Mackerras 
4246defa38bSPaul Mackerras 	if (__USE_RTC()) {
4256defa38bSPaul Mackerras 		start = get_rtcl();
4266defa38bSPaul Mackerras 		do {
4276defa38bSPaul Mackerras 			/* the RTCL register wraps at 1000000000 */
4286defa38bSPaul Mackerras 			diff = get_rtcl() - start;
4296defa38bSPaul Mackerras 			if (diff < 0)
4306defa38bSPaul Mackerras 				diff += 1000000000;
4316defa38bSPaul Mackerras 		} while (diff < loops);
4326defa38bSPaul Mackerras 	} else {
4336defa38bSPaul Mackerras 		start = get_tbl();
4346defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
4356defa38bSPaul Mackerras 			HMT_low();
4366defa38bSPaul Mackerras 		HMT_medium();
4376defa38bSPaul Mackerras 	}
4386defa38bSPaul Mackerras }
4396defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4406defa38bSPaul Mackerras 
4416defa38bSPaul Mackerras void udelay(unsigned long usecs)
4426defa38bSPaul Mackerras {
4436defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4446defa38bSPaul Mackerras }
4456defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4466defa38bSPaul Mackerras 
447f2783c15SPaul Mackerras #ifdef CONFIG_SMP
448f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
449f2783c15SPaul Mackerras {
450f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
451f2783c15SPaul Mackerras 
452f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
453f2783c15SPaul Mackerras 		return regs->link;
454f2783c15SPaul Mackerras 
455f2783c15SPaul Mackerras 	return pc;
456f2783c15SPaul Mackerras }
457f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
458f2783c15SPaul Mackerras #endif
459f2783c15SPaul Mackerras 
460e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
461105988c0SPaul Mackerras 
4620fe1ac48SPaul Mackerras /*
4630fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
4640fe1ac48SPaul Mackerras  */
4650fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
466e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void)
467105988c0SPaul Mackerras {
4680fe1ac48SPaul Mackerras 	unsigned long x;
4690fe1ac48SPaul Mackerras 
4700fe1ac48SPaul Mackerras 	asm volatile("lbz %0,%1(13)"
4710fe1ac48SPaul Mackerras 		: "=r" (x)
472e360adbeSPeter Zijlstra 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
4730fe1ac48SPaul Mackerras 	return x;
474105988c0SPaul Mackerras }
475105988c0SPaul Mackerras 
476e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
4770fe1ac48SPaul Mackerras {
4780fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4790fe1ac48SPaul Mackerras 		"r" (1),
480e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4810fe1ac48SPaul Mackerras }
4820fe1ac48SPaul Mackerras 
483e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
4840fe1ac48SPaul Mackerras {
4850fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4860fe1ac48SPaul Mackerras 		"r" (0),
487e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4880fe1ac48SPaul Mackerras }
4890fe1ac48SPaul Mackerras 
4900fe1ac48SPaul Mackerras #else /* 32-bit */
4910fe1ac48SPaul Mackerras 
492e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
4930fe1ac48SPaul Mackerras 
49469111bacSChristoph Lameter #define set_irq_work_pending_flag()	__this_cpu_write(irq_work_pending, 1)
49569111bacSChristoph Lameter #define test_irq_work_pending()		__this_cpu_read(irq_work_pending)
49669111bacSChristoph Lameter #define clear_irq_work_pending()	__this_cpu_write(irq_work_pending, 0)
497105988c0SPaul Mackerras 
4980fe1ac48SPaul Mackerras #endif /* 32 vs 64 bit */
4990fe1ac48SPaul Mackerras 
5004f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
5010fe1ac48SPaul Mackerras {
5020fe1ac48SPaul Mackerras 	preempt_disable();
503e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
5040fe1ac48SPaul Mackerras 	set_dec(1);
5050fe1ac48SPaul Mackerras 	preempt_enable();
5060fe1ac48SPaul Mackerras }
5070fe1ac48SPaul Mackerras 
508e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
509105988c0SPaul Mackerras 
510e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
511e360adbeSPeter Zijlstra #define clear_irq_work_pending()
512105988c0SPaul Mackerras 
513e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
514105988c0SPaul Mackerras 
515e51df2c1SAnton Blanchard static void __timer_interrupt(void)
516f2783c15SPaul Mackerras {
5171b783955SPreeti U Murthy 	struct pt_regs *regs = get_irq_regs();
51869111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
51969111bacSChristoph Lameter 	struct clock_event_device *evt = this_cpu_ptr(&decrementers);
520860aed25SPaul Mackerras 	u64 now;
521d831d0b8STony Breeds 
522e72bbbabSLi Zhong 	trace_timer_interrupt_entry(regs);
523e72bbbabSLi Zhong 
524e360adbeSPeter Zijlstra 	if (test_irq_work_pending()) {
525e360adbeSPeter Zijlstra 		clear_irq_work_pending();
526e360adbeSPeter Zijlstra 		irq_work_run();
5270fe1ac48SPaul Mackerras 	}
5280fe1ac48SPaul Mackerras 
529860aed25SPaul Mackerras 	now = get_tb_or_rtc();
530860aed25SPaul Mackerras 	if (now >= *next_tb) {
5317df10275SAnton Blanchard 		*next_tb = ~(u64)0;
532d831d0b8STony Breeds 		if (evt->event_handler)
533d831d0b8STony Breeds 			evt->event_handler(evt);
53469111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.timer_irqs_event);
535860aed25SPaul Mackerras 	} else {
536860aed25SPaul Mackerras 		now = *next_tb - now;
53779901024SOliver O'Halloran 		if (now <= decrementer_max)
53879901024SOliver O'Halloran 			set_dec(now);
5390215f7d8SBenjamin Herrenschmidt 		/* We may have raced with new irq work */
5400215f7d8SBenjamin Herrenschmidt 		if (test_irq_work_pending())
5410215f7d8SBenjamin Herrenschmidt 			set_dec(1);
54269111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.timer_irqs_others);
543860aed25SPaul Mackerras 	}
544f2783c15SPaul Mackerras 
545f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
546f2783c15SPaul Mackerras 	/* collect purr register values often, for accurate calculations */
547f2783c15SPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
54869111bacSChristoph Lameter 		struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
549f2783c15SPaul Mackerras 		cu->current_tb = mfspr(SPRN_PURR);
550f2783c15SPaul Mackerras 	}
551f2783c15SPaul Mackerras #endif
552f2783c15SPaul Mackerras 
553e72bbbabSLi Zhong 	trace_timer_interrupt_exit(regs);
5541b783955SPreeti U Murthy }
555e72bbbabSLi Zhong 
5561b783955SPreeti U Murthy /*
5571b783955SPreeti U Murthy  * timer_interrupt - gets called when the decrementer overflows,
5581b783955SPreeti U Murthy  * with interrupts disabled.
5591b783955SPreeti U Murthy  */
5601b783955SPreeti U Murthy void timer_interrupt(struct pt_regs * regs)
5611b783955SPreeti U Murthy {
5621b783955SPreeti U Murthy 	struct pt_regs *old_regs;
56369111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
5641b783955SPreeti U Murthy 
5651b783955SPreeti U Murthy 	/* Ensure a positive value is written to the decrementer, or else
5661b783955SPreeti U Murthy 	 * some CPUs will continue to take decrementer exceptions.
5671b783955SPreeti U Murthy 	 */
56879901024SOliver O'Halloran 	set_dec(decrementer_max);
5691b783955SPreeti U Murthy 
5701b783955SPreeti U Murthy 	/* Some implementations of hotplug will get timer interrupts while
5711b783955SPreeti U Murthy 	 * offline, just ignore these and we also need to set
5721b783955SPreeti U Murthy 	 * decrementers_next_tb as MAX to make sure __check_irq_replay
5731b783955SPreeti U Murthy 	 * don't replay timer interrupt when return, otherwise we'll trap
5741b783955SPreeti U Murthy 	 * here infinitely :(
5751b783955SPreeti U Murthy 	 */
5761b783955SPreeti U Murthy 	if (!cpu_online(smp_processor_id())) {
5771b783955SPreeti U Murthy 		*next_tb = ~(u64)0;
5781b783955SPreeti U Murthy 		return;
5791b783955SPreeti U Murthy 	}
5801b783955SPreeti U Murthy 
5811b783955SPreeti U Murthy 	/* Conditionally hard-enable interrupts now that the DEC has been
5821b783955SPreeti U Murthy 	 * bumped to its maximum value
5831b783955SPreeti U Murthy 	 */
5841b783955SPreeti U Murthy 	may_hard_irq_enable();
5851b783955SPreeti U Murthy 
5861b783955SPreeti U Murthy 
5876e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
5881b783955SPreeti U Murthy 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
5891b783955SPreeti U Murthy 		do_IRQ(regs);
5901b783955SPreeti U Murthy #endif
5911b783955SPreeti U Murthy 
5921b783955SPreeti U Murthy 	old_regs = set_irq_regs(regs);
5931b783955SPreeti U Murthy 	irq_enter();
5941b783955SPreeti U Murthy 
5951b783955SPreeti U Murthy 	__timer_interrupt();
596f2783c15SPaul Mackerras 	irq_exit();
5977d12e780SDavid Howells 	set_irq_regs(old_regs);
598f2783c15SPaul Mackerras }
5999445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt);
600f2783c15SPaul Mackerras 
601dabe859eSPaul Mackerras /*
602dabe859eSPaul Mackerras  * Hypervisor decrementer interrupts shouldn't occur but are sometimes
603dabe859eSPaul Mackerras  * left pending on exit from a KVM guest.  We don't need to do anything
604dabe859eSPaul Mackerras  * to clear them, as they are edge-triggered.
605dabe859eSPaul Mackerras  */
606dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs)
607dabe859eSPaul Mackerras {
608dabe859eSPaul Mackerras }
609dabe859eSPaul Mackerras 
6107ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
611d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void)
6127ac5dde9SScott Wood {
6137ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
6147ac5dde9SScott Wood 	 * with suspending.
6157ac5dde9SScott Wood 	 */
6167ac5dde9SScott Wood 
61779901024SOliver O'Halloran 	set_dec(decrementer_max);
6187ac5dde9SScott Wood 	local_irq_disable();
61979901024SOliver O'Halloran 	set_dec(decrementer_max);
6207ac5dde9SScott Wood }
6217ac5dde9SScott Wood 
622d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void)
6237ac5dde9SScott Wood {
6247ac5dde9SScott Wood 	local_irq_enable();
6257ac5dde9SScott Wood }
6267ac5dde9SScott Wood 
6277ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6287ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
6297ac5dde9SScott Wood {
6307ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
6317ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
6327ac5dde9SScott Wood 	generic_suspend_disable_irqs();
6337ac5dde9SScott Wood }
6347ac5dde9SScott Wood 
6357ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6367ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
6377ac5dde9SScott Wood {
6387ac5dde9SScott Wood 	generic_suspend_enable_irqs();
6397ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
6407ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
6417ac5dde9SScott Wood }
6427ac5dde9SScott Wood #endif
6437ac5dde9SScott Wood 
644b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks)
645b6c295dfSPaul Mackerras {
646b6c295dfSPaul Mackerras 	return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
647b6c295dfSPaul Mackerras }
648b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns);
649b6c295dfSPaul Mackerras 
650f2783c15SPaul Mackerras /*
651f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
652f2783c15SPaul Mackerras  *
653f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
654f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
655f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
656f2783c15SPaul Mackerras  */
657f2783c15SPaul Mackerras unsigned long long sched_clock(void)
658f2783c15SPaul Mackerras {
65996c44507SPaul Mackerras 	if (__USE_RTC())
66096c44507SPaul Mackerras 		return get_rtc();
661fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
662f2783c15SPaul Mackerras }
663f2783c15SPaul Mackerras 
6644be1b297SCyril Bur 
6654be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES
6664be1b297SCyril Bur 
6674be1b297SCyril Bur /*
6684be1b297SCyril Bur  * Running clock - attempts to give a view of time passing for a virtualised
6694be1b297SCyril Bur  * kernels.
6704be1b297SCyril Bur  * Uses the VTB register if available otherwise a next best guess.
6714be1b297SCyril Bur  */
6724be1b297SCyril Bur unsigned long long running_clock(void)
6734be1b297SCyril Bur {
6744be1b297SCyril Bur 	/*
6754be1b297SCyril Bur 	 * Don't read the VTB as a host since KVM does not switch in host
6764be1b297SCyril Bur 	 * timebase into the VTB when it takes a guest off the CPU, reading the
6774be1b297SCyril Bur 	 * VTB would result in reading 'last switched out' guest VTB.
6784be1b297SCyril Bur 	 *
6794be1b297SCyril Bur 	 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
6804be1b297SCyril Bur 	 * would be unsafe to rely only on the #ifdef above.
6814be1b297SCyril Bur 	 */
6824be1b297SCyril Bur 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
6834be1b297SCyril Bur 	    cpu_has_feature(CPU_FTR_ARCH_207S))
6844be1b297SCyril Bur 		return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
6854be1b297SCyril Bur 
6864be1b297SCyril Bur 	/*
6874be1b297SCyril Bur 	 * This is a next best approximation without a VTB.
6884be1b297SCyril Bur 	 * On a host which is running bare metal there should never be any stolen
6894be1b297SCyril Bur 	 * time and on a host which doesn't do any virtualisation TB *should* equal
6904be1b297SCyril Bur 	 * VTB so it makes no difference anyway.
6914be1b297SCyril Bur 	 */
6924be1b297SCyril Bur 	return local_clock() - cputime_to_nsecs(kcpustat_this_cpu->cpustat[CPUTIME_STEAL]);
6934be1b297SCyril Bur }
6944be1b297SCyril Bur #endif
6954be1b297SCyril Bur 
6960bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
697f2783c15SPaul Mackerras {
698f2783c15SPaul Mackerras 	struct device_node *cpu;
6996f7aba7bSAnton Blanchard 	const __be32 *fp;
7000bb474a4SAnton Blanchard 	int found = 0;
701f2783c15SPaul Mackerras 
7020bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
703f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
704f2783c15SPaul Mackerras 
705d8a8188dSOlaf Hering 	if (cpu) {
706e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
707d8a8188dSOlaf Hering 		if (fp) {
7080bb474a4SAnton Blanchard 			found = 1;
709a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
710f2783c15SPaul Mackerras 		}
7110bb474a4SAnton Blanchard 
7120bb474a4SAnton Blanchard 		of_node_put(cpu);
713f2783c15SPaul Mackerras 	}
7140bb474a4SAnton Blanchard 
7150bb474a4SAnton Blanchard 	return found;
7160bb474a4SAnton Blanchard }
7170bb474a4SAnton Blanchard 
718e51df2c1SAnton Blanchard static void start_cpu_decrementer(void)
71977c0a700SBenjamin Herrenschmidt {
72077c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
72177c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
72277c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
72377c0a700SBenjamin Herrenschmidt 
72477c0a700SBenjamin Herrenschmidt 	/* Enable decrementer interrupt */
72577c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TCR, TCR_DIE);
72677c0a700SBenjamin Herrenschmidt #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
72777c0a700SBenjamin Herrenschmidt }
72877c0a700SBenjamin Herrenschmidt 
7290bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
7300bb474a4SAnton Blanchard {
7310bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
7320bb474a4SAnton Blanchard 
7330bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
7340bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
7350bb474a4SAnton Blanchard 
736f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
737f2783c15SPaul Mackerras 				"(not found)\n");
7380bb474a4SAnton Blanchard 	}
739f2783c15SPaul Mackerras 
7400bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
7410bb474a4SAnton Blanchard 
7420bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
7430bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
7440bb474a4SAnton Blanchard 
7450bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
7460bb474a4SAnton Blanchard 				"(not found)\n");
747f2783c15SPaul Mackerras 	}
748f2783c15SPaul Mackerras }
749f2783c15SPaul Mackerras 
750aa3be5f3STony Breeds int update_persistent_clock(struct timespec now)
751f2783c15SPaul Mackerras {
752f2783c15SPaul Mackerras 	struct rtc_time tm;
753f2783c15SPaul Mackerras 
754aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
755023f333aSJason Gunthorpe 		return -ENODEV;
756aa3be5f3STony Breeds 
757aa3be5f3STony Breeds 	to_tm(now.tv_sec + 1 + timezone_offset, &tm);
758aa3be5f3STony Breeds 	tm.tm_year -= 1900;
759aa3be5f3STony Breeds 	tm.tm_mon -= 1;
760aa3be5f3STony Breeds 
761aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
762aa3be5f3STony Breeds }
763aa3be5f3STony Breeds 
764978d7eb3SBenjamin Herrenschmidt static void __read_persistent_clock(struct timespec *ts)
765aa3be5f3STony Breeds {
766aa3be5f3STony Breeds 	struct rtc_time tm;
767aa3be5f3STony Breeds 	static int first = 1;
768aa3be5f3STony Breeds 
769d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
770aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
771aa3be5f3STony Breeds 	if (first) {
772aa3be5f3STony Breeds 		first = 0;
773aa3be5f3STony Breeds 		if (ppc_md.time_init)
774aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
775aa3be5f3STony Breeds 
776aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
777d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
778d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
779d90246cdSMartin Schwidefsky 			return;
780aa3be5f3STony Breeds 		}
781d90246cdSMartin Schwidefsky 	}
782d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
783d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
784d90246cdSMartin Schwidefsky 		return;
785d90246cdSMartin Schwidefsky 	}
786f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
787978d7eb3SBenjamin Herrenschmidt 
788d4f587c6SMartin Schwidefsky 	ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
789f2783c15SPaul Mackerras 			    tm.tm_hour, tm.tm_min, tm.tm_sec);
790f2783c15SPaul Mackerras }
791f2783c15SPaul Mackerras 
792978d7eb3SBenjamin Herrenschmidt void read_persistent_clock(struct timespec *ts)
793978d7eb3SBenjamin Herrenschmidt {
794978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
795978d7eb3SBenjamin Herrenschmidt 
796978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
797978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
798978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
799978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
800978d7eb3SBenjamin Herrenschmidt 	}
801978d7eb3SBenjamin Herrenschmidt 
802978d7eb3SBenjamin Herrenschmidt }
803978d7eb3SBenjamin Herrenschmidt 
8044a4cfe38STony Breeds /* clocksource code */
8058e19608eSMagnus Damm static cycle_t rtc_read(struct clocksource *cs)
8064a4cfe38STony Breeds {
8074a4cfe38STony Breeds 	return (cycle_t)get_rtc();
8084a4cfe38STony Breeds }
8094a4cfe38STony Breeds 
8108e19608eSMagnus Damm static cycle_t timebase_read(struct clocksource *cs)
8114a4cfe38STony Breeds {
8124a4cfe38STony Breeds 	return (cycle_t)get_tb();
8134a4cfe38STony Breeds }
8144a4cfe38STony Breeds 
81570639421SJohn Stultz void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm,
8164a0e6377SThomas Gleixner 			 struct clocksource *clock, u32 mult, cycle_t cycle_last)
8174a4cfe38STony Breeds {
818b0797b60SJohn Stultz 	u64 new_tb_to_xs, new_stamp_xsec;
8190e469db8SPaul Mackerras 	u32 frac_sec;
8204a4cfe38STony Breeds 
8214a4cfe38STony Breeds 	if (clock != &clocksource_timebase)
8224a4cfe38STony Breeds 		return;
8234a4cfe38STony Breeds 
8244a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
8254a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8264a4cfe38STony Breeds 	smp_mb();
8274a4cfe38STony Breeds 
82811b8633aSAnton Blanchard 	/* 19342813113834067 ~= 2^(20+64) / 1e9 */
82911b8633aSAnton Blanchard 	new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift);
83006d518e3SJohn Stultz 	new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC;
831b0797b60SJohn Stultz 	do_div(new_stamp_xsec, 1000000000);
83206d518e3SJohn Stultz 	new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC;
833b0797b60SJohn Stultz 
8340e469db8SPaul Mackerras 	BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC);
8350e469db8SPaul Mackerras 	/* this is tv_nsec / 1e9 as a 0.32 fraction */
8360e469db8SPaul Mackerras 	frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32;
83747916be4SThomas Gleixner 
838b0797b60SJohn Stultz 	/*
839b0797b60SJohn Stultz 	 * tb_update_count is used to allow the userspace gettimeofday code
840b0797b60SJohn Stultz 	 * to assure itself that it sees a consistent view of the tb_to_xs and
841b0797b60SJohn Stultz 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
842b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
843b0797b60SJohn Stultz 	 * the two values of tb_update_count match and are even then the
844b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
845b0797b60SJohn Stultz 	 * loops back and reads them again until this criteria is met.
846b0797b60SJohn Stultz 	 * We expect the caller to have done the first increment of
847b0797b60SJohn Stultz 	 * vdso_data->tb_update_count already.
848b0797b60SJohn Stultz 	 */
8494a0e6377SThomas Gleixner 	vdso_data->tb_orig_stamp = cycle_last;
850b0797b60SJohn Stultz 	vdso_data->stamp_xsec = new_stamp_xsec;
851b0797b60SJohn Stultz 	vdso_data->tb_to_xs = new_tb_to_xs;
8527615856eSJohn Stultz 	vdso_data->wtom_clock_sec = wtm->tv_sec;
8537615856eSJohn Stultz 	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
85406d518e3SJohn Stultz 	vdso_data->stamp_xtime = *wall_time;
85547916be4SThomas Gleixner 	vdso_data->stamp_sec_fraction = frac_sec;
856b0797b60SJohn Stultz 	smp_wmb();
857b0797b60SJohn Stultz 	++(vdso_data->tb_update_count);
8584a4cfe38STony Breeds }
8594a4cfe38STony Breeds 
8604a4cfe38STony Breeds void update_vsyscall_tz(void)
8614a4cfe38STony Breeds {
8624a4cfe38STony Breeds 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
8634a4cfe38STony Breeds 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
8644a4cfe38STony Breeds }
8654a4cfe38STony Breeds 
8661c21a293SMichael Ellerman static void __init clocksource_init(void)
8674a4cfe38STony Breeds {
8684a4cfe38STony Breeds 	struct clocksource *clock;
8694a4cfe38STony Breeds 
8704a4cfe38STony Breeds 	if (__USE_RTC())
8714a4cfe38STony Breeds 		clock = &clocksource_rtc;
8724a4cfe38STony Breeds 	else
8734a4cfe38STony Breeds 		clock = &clocksource_timebase;
8744a4cfe38STony Breeds 
87511b8633aSAnton Blanchard 	if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
8764a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
8774a4cfe38STony Breeds 		       clock->name);
8784a4cfe38STony Breeds 		return;
8794a4cfe38STony Breeds 	}
8804a4cfe38STony Breeds 
8814a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
8824a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
8834a4cfe38STony Breeds }
8844a4cfe38STony Breeds 
885d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
886d831d0b8STony Breeds 				      struct clock_event_device *dev)
887d831d0b8STony Breeds {
88869111bacSChristoph Lameter 	__this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt);
889d831d0b8STony Breeds 	set_dec(evt);
8900215f7d8SBenjamin Herrenschmidt 
8910215f7d8SBenjamin Herrenschmidt 	/* We may have raced with new irq work */
8920215f7d8SBenjamin Herrenschmidt 	if (test_irq_work_pending())
8930215f7d8SBenjamin Herrenschmidt 		set_dec(1);
8940215f7d8SBenjamin Herrenschmidt 
895d831d0b8STony Breeds 	return 0;
896d831d0b8STony Breeds }
897d831d0b8STony Breeds 
89837a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev)
899d831d0b8STony Breeds {
90079901024SOliver O'Halloran 	decrementer_set_next_event(decrementer_max, dev);
90137a13e78SViresh Kumar 	return 0;
902d831d0b8STony Breeds }
903d831d0b8STony Breeds 
9041b67bee1SSrivatsa S. Bhat /* Interrupt handler for the timer broadcast IPI */
9051b67bee1SSrivatsa S. Bhat void tick_broadcast_ipi_handler(void)
9061b67bee1SSrivatsa S. Bhat {
90769111bacSChristoph Lameter 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
9081b783955SPreeti U Murthy 
9091b783955SPreeti U Murthy 	*next_tb = get_tb_or_rtc();
9101b783955SPreeti U Murthy 	__timer_interrupt();
9111b67bee1SSrivatsa S. Bhat }
9121b67bee1SSrivatsa S. Bhat 
913d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
914d831d0b8STony Breeds {
9157df10275SAnton Blanchard 	struct clock_event_device *dec = &per_cpu(decrementers, cpu);
916d831d0b8STony Breeds 
917d831d0b8STony Breeds 	*dec = decrementer_clockevent;
918320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
919d831d0b8STony Breeds 
920b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
921d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
922d831d0b8STony Breeds 
923d831d0b8STony Breeds 	clockevents_register_device(dec);
924d831d0b8STony Breeds }
925d831d0b8STony Breeds 
92679901024SOliver O'Halloran static void enable_large_decrementer(void)
92779901024SOliver O'Halloran {
92879901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
92979901024SOliver O'Halloran 		return;
93079901024SOliver O'Halloran 
93179901024SOliver O'Halloran 	if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
93279901024SOliver O'Halloran 		return;
93379901024SOliver O'Halloran 
93479901024SOliver O'Halloran 	/*
93579901024SOliver O'Halloran 	 * If we're running as the hypervisor we need to enable the LD manually
93679901024SOliver O'Halloran 	 * otherwise firmware should have done it for us.
93779901024SOliver O'Halloran 	 */
93879901024SOliver O'Halloran 	if (cpu_has_feature(CPU_FTR_HVMODE))
93979901024SOliver O'Halloran 		mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
94079901024SOliver O'Halloran }
94179901024SOliver O'Halloran 
94279901024SOliver O'Halloran static void __init set_decrementer_max(void)
94379901024SOliver O'Halloran {
94479901024SOliver O'Halloran 	struct device_node *cpu;
94579901024SOliver O'Halloran 	u32 bits = 32;
94679901024SOliver O'Halloran 
94779901024SOliver O'Halloran 	/* Prior to ISAv3 the decrementer is always 32 bit */
94879901024SOliver O'Halloran 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
94979901024SOliver O'Halloran 		return;
95079901024SOliver O'Halloran 
95179901024SOliver O'Halloran 	cpu = of_find_node_by_type(NULL, "cpu");
95279901024SOliver O'Halloran 
95379901024SOliver O'Halloran 	if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
95479901024SOliver O'Halloran 		if (bits > 64 || bits < 32) {
95579901024SOliver O'Halloran 			pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
95679901024SOliver O'Halloran 			bits = 32;
95779901024SOliver O'Halloran 		}
95879901024SOliver O'Halloran 
95979901024SOliver O'Halloran 		/* calculate the signed maximum given this many bits */
96079901024SOliver O'Halloran 		decrementer_max = (1ul << (bits - 1)) - 1;
96179901024SOliver O'Halloran 	}
96279901024SOliver O'Halloran 
96379901024SOliver O'Halloran 	of_node_put(cpu);
96479901024SOliver O'Halloran 
96579901024SOliver O'Halloran 	pr_info("time_init: %u bit decrementer (max: %llx)\n",
96679901024SOliver O'Halloran 		bits, decrementer_max);
96779901024SOliver O'Halloran }
96879901024SOliver O'Halloran 
969c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
970d831d0b8STony Breeds {
971d831d0b8STony Breeds 	int cpu = smp_processor_id();
972d831d0b8STony Breeds 
973d8afc6fdSAnton Blanchard 	clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4);
974d8afc6fdSAnton Blanchard 
975d831d0b8STony Breeds 	decrementer_clockevent.max_delta_ns =
97679901024SOliver O'Halloran 		clockevent_delta2ns(decrementer_max, &decrementer_clockevent);
97743875cc0SPaul Mackerras 	decrementer_clockevent.min_delta_ns =
97843875cc0SPaul Mackerras 		clockevent_delta2ns(2, &decrementer_clockevent);
979d831d0b8STony Breeds 
980d831d0b8STony Breeds 	register_decrementer_clockevent(cpu);
981d831d0b8STony Breeds }
982d831d0b8STony Breeds 
983d831d0b8STony Breeds void secondary_cpu_time_init(void)
984d831d0b8STony Breeds {
98579901024SOliver O'Halloran 	/* Enable and test the large decrementer for this cpu */
98679901024SOliver O'Halloran 	enable_large_decrementer();
98779901024SOliver O'Halloran 
98877c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
98977c0a700SBenjamin Herrenschmidt 	 * such as BookE
99077c0a700SBenjamin Herrenschmidt 	 */
99177c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
99277c0a700SBenjamin Herrenschmidt 
993d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
994d831d0b8STony Breeds 	 * call here ! */
995d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
996d831d0b8STony Breeds }
997d831d0b8STony Breeds 
998f2783c15SPaul Mackerras /* This function is only called on the boot processor */
999f2783c15SPaul Mackerras void __init time_init(void)
1000f2783c15SPaul Mackerras {
1001f2783c15SPaul Mackerras 	struct div_result res;
1002d75d68cfSPaul Mackerras 	u64 scale;
1003f2783c15SPaul Mackerras 	unsigned shift;
1004f2783c15SPaul Mackerras 
100596c44507SPaul Mackerras 	if (__USE_RTC()) {
100696c44507SPaul Mackerras 		/* 601 processor: dec counts down by 128 every 128ns */
100796c44507SPaul Mackerras 		ppc_tb_freq = 1000000000;
100896c44507SPaul Mackerras 	} else {
100996c44507SPaul Mackerras 		/* Normal PowerPC with timebase register */
1010f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
1011224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
1012374e99d4SPaul Mackerras 		       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
1013224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
1014374e99d4SPaul Mackerras 		       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
101596c44507SPaul Mackerras 	}
1016374e99d4SPaul Mackerras 
1017374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
1018092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
1019374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
1020c6622f63SPaul Mackerras 	calc_cputime_factors();
1021a42548a1SStanislaw Gruszka 	setup_cputime_one_jiffy();
1022092b8f34SPaul Mackerras 
1023092b8f34SPaul Mackerras 	/*
1024f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
1025f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
1026f2783c15SPaul Mackerras 	 * which is the timebase frequency.
1027f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
1028f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
1029f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
1030f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
1031f2783c15SPaul Mackerras 	 * sched_clock().
1032f2783c15SPaul Mackerras 	 */
1033f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
1034f2783c15SPaul Mackerras 	scale = res.result_low;
1035f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
1036f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
1037f2783c15SPaul Mackerras 		res.result_high >>= 1;
1038f2783c15SPaul Mackerras 	}
1039f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
1040f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
1041fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
1042c27da339SBenjamin Herrenschmidt 	boot_tb = get_tb_or_rtc();
1043f2783c15SPaul Mackerras 
1044092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
1045092b8f34SPaul Mackerras 	if (timezone_offset) {
1046092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
1047092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
1048092b8f34SPaul Mackerras 	}
1049092b8f34SPaul Mackerras 
1050a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
1051a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
1052f2783c15SPaul Mackerras 
105379901024SOliver O'Halloran 	/* initialise and enable the large decrementer (if we have one) */
105479901024SOliver O'Halloran 	set_decrementer_max();
105579901024SOliver O'Halloran 	enable_large_decrementer();
105679901024SOliver O'Halloran 
105777c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
105877c0a700SBenjamin Herrenschmidt 	 * such as BookE
105977c0a700SBenjamin Herrenschmidt 	 */
106077c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
106177c0a700SBenjamin Herrenschmidt 
1062f5339277SStephen Rothwell 	/* Register the clocksource */
10634a4cfe38STony Breeds 	clocksource_init();
10644a4cfe38STony Breeds 
1065d831d0b8STony Breeds 	init_decrementer_clockevent();
10660d948730SPreeti U Murthy 	tick_setup_hrtimer_broadcast();
1067f0d37300SKevin Hao 
1068f0d37300SKevin Hao #ifdef CONFIG_COMMON_CLK
1069f0d37300SKevin Hao 	of_clk_init(NULL);
1070f0d37300SKevin Hao #endif
1071f2783c15SPaul Mackerras }
1072f2783c15SPaul Mackerras 
1073f2783c15SPaul Mackerras 
1074f2783c15SPaul Mackerras #define FEBRUARY	2
1075f2783c15SPaul Mackerras #define	STARTOFTIME	1970
1076f2783c15SPaul Mackerras #define SECDAY		86400L
1077f2783c15SPaul Mackerras #define SECYR		(SECDAY * 365)
1078f2783c15SPaul Mackerras #define	leapyear(year)		((year) % 4 == 0 && \
1079f2783c15SPaul Mackerras 				 ((year) % 100 != 0 || (year) % 400 == 0))
1080f2783c15SPaul Mackerras #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
1081f2783c15SPaul Mackerras #define	days_in_month(a) 	(month_days[(a) - 1])
1082f2783c15SPaul Mackerras 
1083f2783c15SPaul Mackerras static int month_days[12] = {
1084f2783c15SPaul Mackerras 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1085f2783c15SPaul Mackerras };
1086f2783c15SPaul Mackerras 
1087f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm)
1088f2783c15SPaul Mackerras {
1089f2783c15SPaul Mackerras 	register int    i;
1090f2783c15SPaul Mackerras 	register long   hms, day;
1091f2783c15SPaul Mackerras 
1092f2783c15SPaul Mackerras 	day = tim / SECDAY;
1093f2783c15SPaul Mackerras 	hms = tim % SECDAY;
1094f2783c15SPaul Mackerras 
1095f2783c15SPaul Mackerras 	/* Hours, minutes, seconds are easy */
1096f2783c15SPaul Mackerras 	tm->tm_hour = hms / 3600;
1097f2783c15SPaul Mackerras 	tm->tm_min = (hms % 3600) / 60;
1098f2783c15SPaul Mackerras 	tm->tm_sec = (hms % 3600) % 60;
1099f2783c15SPaul Mackerras 
1100f2783c15SPaul Mackerras 	/* Number of years in days */
1101f2783c15SPaul Mackerras 	for (i = STARTOFTIME; day >= days_in_year(i); i++)
1102f2783c15SPaul Mackerras 		day -= days_in_year(i);
1103f2783c15SPaul Mackerras 	tm->tm_year = i;
1104f2783c15SPaul Mackerras 
1105f2783c15SPaul Mackerras 	/* Number of months in days left */
1106f2783c15SPaul Mackerras 	if (leapyear(tm->tm_year))
1107f2783c15SPaul Mackerras 		days_in_month(FEBRUARY) = 29;
1108f2783c15SPaul Mackerras 	for (i = 1; day >= days_in_month(i); i++)
1109f2783c15SPaul Mackerras 		day -= days_in_month(i);
1110f2783c15SPaul Mackerras 	days_in_month(FEBRUARY) = 28;
1111f2783c15SPaul Mackerras 	tm->tm_mon = i;
1112f2783c15SPaul Mackerras 
1113f2783c15SPaul Mackerras 	/* Days are what is left over (+1) from all that. */
1114f2783c15SPaul Mackerras 	tm->tm_mday = day + 1;
1115f2783c15SPaul Mackerras 
1116f2783c15SPaul Mackerras 	/*
111700b912b0SDaniel Axtens 	 * No-one uses the day of the week.
1118f2783c15SPaul Mackerras 	 */
111900b912b0SDaniel Axtens 	tm->tm_wday = -1;
1120f2783c15SPaul Mackerras }
1121e1802b06SAnton Blanchard EXPORT_SYMBOL(to_tm);
1122f2783c15SPaul Mackerras 
1123f2783c15SPaul Mackerras /*
1124f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1125f2783c15SPaul Mackerras  * result.
1126f2783c15SPaul Mackerras  */
1127f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1128f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1129f2783c15SPaul Mackerras {
1130f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1131f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1132f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1133f2783c15SPaul Mackerras 
1134f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1135f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1136f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1137f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1138f2783c15SPaul Mackerras 
1139f2783c15SPaul Mackerras 	w = a / divisor;
1140f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1141f2783c15SPaul Mackerras 
1142f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1143f2783c15SPaul Mackerras 	x = ra;
1144f2783c15SPaul Mackerras 
1145f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1146f2783c15SPaul Mackerras 	y = rb;
1147f2783c15SPaul Mackerras 
1148f2783c15SPaul Mackerras 	do_div(rc, divisor);
1149f2783c15SPaul Mackerras 	z = rc;
1150f2783c15SPaul Mackerras 
1151f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1152f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1153f2783c15SPaul Mackerras 
1154f2783c15SPaul Mackerras }
1155bcd68a70SGeert Uytterhoeven 
1156177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
1157177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
1158177996e6SBenjamin Herrenschmidt {
1159177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
1160177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
1161177996e6SBenjamin Herrenschmidt 	 */
1162177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
1163177996e6SBenjamin Herrenschmidt }
1164177996e6SBenjamin Herrenschmidt 
1165169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1166169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1167169047f4SArnd Bergmann {
1168169047f4SArnd Bergmann 	ppc_md.get_rtc_time(tm);
1169169047f4SArnd Bergmann 	return rtc_valid_tm(tm);
1170169047f4SArnd Bergmann }
1171169047f4SArnd Bergmann 
1172169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1173169047f4SArnd Bergmann {
1174169047f4SArnd Bergmann 	if (!ppc_md.set_rtc_time)
1175169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1176169047f4SArnd Bergmann 
1177169047f4SArnd Bergmann 	if (ppc_md.set_rtc_time(tm) < 0)
1178169047f4SArnd Bergmann 		return -EOPNOTSUPP;
1179169047f4SArnd Bergmann 
1180169047f4SArnd Bergmann 	return 0;
1181169047f4SArnd Bergmann }
1182169047f4SArnd Bergmann 
1183169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = {
1184169047f4SArnd Bergmann 	.read_time = rtc_generic_get_time,
1185169047f4SArnd Bergmann 	.set_time = rtc_generic_set_time,
1186169047f4SArnd Bergmann };
1187169047f4SArnd Bergmann 
1188bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1189bcd68a70SGeert Uytterhoeven {
1190bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1191bcd68a70SGeert Uytterhoeven 
1192bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1193bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1194bcd68a70SGeert Uytterhoeven 
1195169047f4SArnd Bergmann 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1196169047f4SArnd Bergmann 					     &rtc_generic_ops,
1197169047f4SArnd Bergmann 					     sizeof(rtc_generic_ops));
1198bcd68a70SGeert Uytterhoeven 
11998c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(pdev);
1200bcd68a70SGeert Uytterhoeven }
1201bcd68a70SGeert Uytterhoeven 
12028f6b9512SPaul Gortmaker device_initcall(rtc_init);
1203169047f4SArnd Bergmann #endif
1204