xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision d8afc6fd95496204174f19af0cb39eefee0c3e8a)
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
20f2783c15SPaul Mackerras  * measurement at boot time. (for iSeries, we calibrate the timebase
21f2783c15SPaul Mackerras  * against the Titan chip's clock.)
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  *      This program is free software; you can redistribute it and/or
30f2783c15SPaul Mackerras  *      modify it under the terms of the GNU General Public License
31f2783c15SPaul Mackerras  *      as published by the Free Software Foundation; either version
32f2783c15SPaul Mackerras  *      2 of the License, or (at your option) any later version.
33f2783c15SPaul Mackerras  */
34f2783c15SPaul Mackerras 
35f2783c15SPaul Mackerras #include <linux/errno.h>
364b16f8e2SPaul Gortmaker #include <linux/export.h>
37f2783c15SPaul Mackerras #include <linux/sched.h>
38f2783c15SPaul Mackerras #include <linux/kernel.h>
39f2783c15SPaul Mackerras #include <linux/param.h>
40f2783c15SPaul Mackerras #include <linux/string.h>
41f2783c15SPaul Mackerras #include <linux/mm.h>
42f2783c15SPaul Mackerras #include <linux/interrupt.h>
43f2783c15SPaul Mackerras #include <linux/timex.h>
44f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
45f2783c15SPaul Mackerras #include <linux/time.h>
46f2783c15SPaul Mackerras #include <linux/init.h>
47f2783c15SPaul Mackerras #include <linux/profile.h>
48f2783c15SPaul Mackerras #include <linux/cpu.h>
49f2783c15SPaul Mackerras #include <linux/security.h>
50f2783c15SPaul Mackerras #include <linux/percpu.h>
51f2783c15SPaul Mackerras #include <linux/rtc.h>
52092b8f34SPaul Mackerras #include <linux/jiffies.h>
53c6622f63SPaul Mackerras #include <linux/posix-timers.h>
547d12e780SDavid Howells #include <linux/irq.h>
55177996e6SBenjamin Herrenschmidt #include <linux/delay.h>
56e360adbeSPeter Zijlstra #include <linux/irq_work.h>
576795b85cSAnton Blanchard #include <asm/trace.h>
58f2783c15SPaul Mackerras 
59f2783c15SPaul Mackerras #include <asm/io.h>
60f2783c15SPaul Mackerras #include <asm/processor.h>
61f2783c15SPaul Mackerras #include <asm/nvram.h>
62f2783c15SPaul Mackerras #include <asm/cache.h>
63f2783c15SPaul Mackerras #include <asm/machdep.h>
64f2783c15SPaul Mackerras #include <asm/uaccess.h>
65f2783c15SPaul Mackerras #include <asm/time.h>
66f2783c15SPaul Mackerras #include <asm/prom.h>
67f2783c15SPaul Mackerras #include <asm/irq.h>
68f2783c15SPaul Mackerras #include <asm/div64.h>
692249ca9dSPaul Mackerras #include <asm/smp.h>
70a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
71f2783c15SPaul Mackerras #include <asm/firmware.h>
7206b8e878SMichael Neuling #include <asm/cputime.h>
73f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
748875ccfbSKelly Daly #include <asm/iseries/it_lp_queue.h>
758021b8a7SKelly Daly #include <asm/iseries/hv_call_xm.h>
76f2783c15SPaul Mackerras #endif
77f2783c15SPaul Mackerras 
784a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
794a4cfe38STony Breeds 
80d831d0b8STony Breeds #include <linux/clockchips.h>
814a4cfe38STony Breeds #include <linux/clocksource.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 	.shift        = 22,
904a4cfe38STony Breeds 	.mult         = 0,	/* To be filled in */
914a4cfe38STony Breeds 	.read         = rtc_read,
924a4cfe38STony Breeds };
934a4cfe38STony Breeds 
948e19608eSMagnus Damm static cycle_t timebase_read(struct clocksource *);
954a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
964a4cfe38STony Breeds 	.name         = "timebase",
974a4cfe38STony Breeds 	.rating       = 400,
984a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
994a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
1004a4cfe38STony Breeds 	.shift        = 22,
1014a4cfe38STony Breeds 	.mult         = 0,	/* To be filled in */
1024a4cfe38STony Breeds 	.read         = timebase_read,
1034a4cfe38STony Breeds };
1044a4cfe38STony Breeds 
105d831d0b8STony Breeds #define DECREMENTER_MAX	0x7fffffff
106d831d0b8STony Breeds 
107d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
108d831d0b8STony Breeds 				      struct clock_event_device *dev);
109d831d0b8STony Breeds static void decrementer_set_mode(enum clock_event_mode mode,
110d831d0b8STony Breeds 				 struct clock_event_device *dev);
111d831d0b8STony Breeds 
112d831d0b8STony Breeds static struct clock_event_device decrementer_clockevent = {
113d831d0b8STony Breeds        .name           = "decrementer",
114d831d0b8STony Breeds        .rating         = 200,
115d831d0b8STony Breeds        .irq            = 0,
116d831d0b8STony Breeds        .set_next_event = decrementer_set_next_event,
117d831d0b8STony Breeds        .set_mode       = decrementer_set_mode,
118d831d0b8STony Breeds        .features       = CLOCK_EVT_FEAT_ONESHOT,
119d831d0b8STony Breeds };
120d831d0b8STony Breeds 
1216e6b44e8SMilton Miller struct decrementer_clock {
1226e6b44e8SMilton Miller 	struct clock_event_device event;
1236e6b44e8SMilton Miller 	u64 next_tb;
1246e6b44e8SMilton Miller };
1256e6b44e8SMilton Miller 
1266e6b44e8SMilton Miller static DEFINE_PER_CPU(struct decrementer_clock, decrementers);
127d831d0b8STony Breeds 
128f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
12971712b45STony Breeds static unsigned long __initdata iSeries_recal_titan;
13071712b45STony Breeds static signed long __initdata iSeries_recal_tb;
1314a4cfe38STony Breeds 
1324a4cfe38STony Breeds /* Forward declaration is only needed for iSereis compiles */
1331c21a293SMichael Ellerman static void __init clocksource_init(void);
134f2783c15SPaul Mackerras #endif
135f2783c15SPaul Mackerras 
136f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
137f2783c15SPaul Mackerras 
138f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
139f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
140f2783c15SPaul Mackerras #else
141f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
142f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
143f2783c15SPaul Mackerras #endif
144f2783c15SPaul Mackerras 
145f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
146f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
147f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
148f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
1492cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime_t conversions */
150092b8f34SPaul Mackerras 
151f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
152f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
153f2783c15SPaul Mackerras 
154fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
155fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
156364a1246SHeiko Schocher static u64 boot_tb __read_mostly;
157f2783c15SPaul Mackerras 
158f2783c15SPaul Mackerras extern struct timezone sys_tz;
159f2783c15SPaul Mackerras static long timezone_offset;
160f2783c15SPaul Mackerras 
161f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
16255ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq);
163f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
16455ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq);
16596c44507SPaul Mackerras 
166c6622f63SPaul Mackerras #ifdef CONFIG_VIRT_CPU_ACCOUNTING
167c6622f63SPaul Mackerras /*
168c6622f63SPaul Mackerras  * Factors for converting from cputime_t (timebase ticks) to
169c6622f63SPaul Mackerras  * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds).
170c6622f63SPaul Mackerras  * These are all stored as 0.64 fixed-point binary fractions.
171c6622f63SPaul Mackerras  */
172c6622f63SPaul Mackerras u64 __cputime_jiffies_factor;
1732cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_jiffies_factor);
174c6622f63SPaul Mackerras u64 __cputime_msec_factor;
1752cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_msec_factor);
176c6622f63SPaul Mackerras u64 __cputime_sec_factor;
1772cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_sec_factor);
178c6622f63SPaul Mackerras u64 __cputime_clockt_factor;
1792cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_clockt_factor);
18006b8e878SMichael Neuling DEFINE_PER_CPU(unsigned long, cputime_last_delta);
18106b8e878SMichael Neuling DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta);
182c6622f63SPaul Mackerras 
183a42548a1SStanislaw Gruszka cputime_t cputime_one_jiffy;
184a42548a1SStanislaw Gruszka 
185872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64);
186872e439aSPaul Mackerras 
187c6622f63SPaul Mackerras static void calc_cputime_factors(void)
188c6622f63SPaul Mackerras {
189c6622f63SPaul Mackerras 	struct div_result res;
190c6622f63SPaul Mackerras 
191c6622f63SPaul Mackerras 	div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
192c6622f63SPaul Mackerras 	__cputime_jiffies_factor = res.result_low;
193c6622f63SPaul Mackerras 	div128_by_32(1000, 0, tb_ticks_per_sec, &res);
194c6622f63SPaul Mackerras 	__cputime_msec_factor = res.result_low;
195c6622f63SPaul Mackerras 	div128_by_32(1, 0, tb_ticks_per_sec, &res);
196c6622f63SPaul Mackerras 	__cputime_sec_factor = res.result_low;
197c6622f63SPaul Mackerras 	div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
198c6622f63SPaul Mackerras 	__cputime_clockt_factor = res.result_low;
199c6622f63SPaul Mackerras }
200c6622f63SPaul Mackerras 
201c6622f63SPaul Mackerras /*
202cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
203cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
204c6622f63SPaul Mackerras  */
205cf9efce0SPaul Mackerras static u64 read_spurr(u64 tb)
206c6622f63SPaul Mackerras {
207cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
208cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
209c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
210c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
211cf9efce0SPaul Mackerras 	return tb;
212cf9efce0SPaul Mackerras }
213cf9efce0SPaul Mackerras 
214cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR
215cf9efce0SPaul Mackerras 
216cf9efce0SPaul Mackerras /*
217cf9efce0SPaul Mackerras  * Scan the dispatch trace log and count up the stolen time.
218cf9efce0SPaul Mackerras  * Should be called with interrupts disabled.
219cf9efce0SPaul Mackerras  */
220cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb)
221cf9efce0SPaul Mackerras {
222872e439aSPaul Mackerras 	u64 i = local_paca->dtl_ridx;
223cf9efce0SPaul Mackerras 	struct dtl_entry *dtl = local_paca->dtl_curr;
224cf9efce0SPaul Mackerras 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
225cf9efce0SPaul Mackerras 	struct lppaca *vpa = local_paca->lppaca_ptr;
226cf9efce0SPaul Mackerras 	u64 tb_delta;
227cf9efce0SPaul Mackerras 	u64 stolen = 0;
228cf9efce0SPaul Mackerras 	u64 dtb;
229cf9efce0SPaul Mackerras 
23084ffae55SAnton Blanchard 	if (!dtl)
23184ffae55SAnton Blanchard 		return 0;
23284ffae55SAnton Blanchard 
233cf9efce0SPaul Mackerras 	if (i == vpa->dtl_idx)
234cf9efce0SPaul Mackerras 		return 0;
235cf9efce0SPaul Mackerras 	while (i < vpa->dtl_idx) {
236872e439aSPaul Mackerras 		if (dtl_consumer)
237872e439aSPaul Mackerras 			dtl_consumer(dtl, i);
238cf9efce0SPaul Mackerras 		dtb = dtl->timebase;
239cf9efce0SPaul Mackerras 		tb_delta = dtl->enqueue_to_dispatch_time +
240cf9efce0SPaul Mackerras 			dtl->ready_to_enqueue_time;
241cf9efce0SPaul Mackerras 		barrier();
242cf9efce0SPaul Mackerras 		if (i + N_DISPATCH_LOG < vpa->dtl_idx) {
243cf9efce0SPaul Mackerras 			/* buffer has overflowed */
244cf9efce0SPaul Mackerras 			i = vpa->dtl_idx - N_DISPATCH_LOG;
245cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
246cf9efce0SPaul Mackerras 			continue;
247cf9efce0SPaul Mackerras 		}
248cf9efce0SPaul Mackerras 		if (dtb > stop_tb)
249cf9efce0SPaul Mackerras 			break;
250cf9efce0SPaul Mackerras 		stolen += tb_delta;
251cf9efce0SPaul Mackerras 		++i;
252cf9efce0SPaul Mackerras 		++dtl;
253cf9efce0SPaul Mackerras 		if (dtl == dtl_end)
254cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log;
255cf9efce0SPaul Mackerras 	}
256cf9efce0SPaul Mackerras 	local_paca->dtl_ridx = i;
257cf9efce0SPaul Mackerras 	local_paca->dtl_curr = dtl;
258cf9efce0SPaul Mackerras 	return stolen;
259c6622f63SPaul Mackerras }
260c6622f63SPaul Mackerras 
261c6622f63SPaul Mackerras /*
262cf9efce0SPaul Mackerras  * Accumulate stolen time by scanning the dispatch trace log.
263cf9efce0SPaul Mackerras  * Called on entry from user mode.
2644603ac18SMichael Neuling  */
265cf9efce0SPaul Mackerras void accumulate_stolen_time(void)
2664603ac18SMichael Neuling {
267cf9efce0SPaul Mackerras 	u64 sst, ust;
268cf9efce0SPaul Mackerras 
269b18ae08dSTejun Heo 	u8 save_soft_enabled = local_paca->soft_enabled;
270b18ae08dSTejun Heo 	u8 save_hard_enabled = local_paca->hard_enabled;
271b18ae08dSTejun Heo 
272b18ae08dSTejun Heo 	/* We are called early in the exception entry, before
273b18ae08dSTejun Heo 	 * soft/hard_enabled are sync'ed to the expected state
274b18ae08dSTejun Heo 	 * for the exception. We are hard disabled but the PACA
275b18ae08dSTejun Heo 	 * needs to reflect that so various debug stuff doesn't
276b18ae08dSTejun Heo 	 * complain
277b18ae08dSTejun Heo 	 */
278b18ae08dSTejun Heo 	local_paca->soft_enabled = 0;
279b18ae08dSTejun Heo 	local_paca->hard_enabled = 0;
280b18ae08dSTejun Heo 
281b18ae08dSTejun Heo 	sst = scan_dispatch_log(local_paca->starttime_user);
282b18ae08dSTejun Heo 	ust = scan_dispatch_log(local_paca->starttime);
283b18ae08dSTejun Heo 	local_paca->system_time -= sst;
284b18ae08dSTejun Heo 	local_paca->user_time -= ust;
285b18ae08dSTejun Heo 	local_paca->stolen_time += ust + sst;
286b18ae08dSTejun Heo 
287b18ae08dSTejun Heo 	local_paca->soft_enabled = save_soft_enabled;
288b18ae08dSTejun Heo 	local_paca->hard_enabled = save_hard_enabled;
2894603ac18SMichael Neuling }
2904603ac18SMichael Neuling 
291cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
292cf9efce0SPaul Mackerras {
293cf9efce0SPaul Mackerras 	u64 stolen = 0;
294cf9efce0SPaul Mackerras 
295cf9efce0SPaul Mackerras 	if (get_paca()->dtl_ridx != get_paca()->lppaca_ptr->dtl_idx) {
296cf9efce0SPaul Mackerras 		stolen = scan_dispatch_log(stop_tb);
297cf9efce0SPaul Mackerras 		get_paca()->system_time -= stolen;
298cf9efce0SPaul Mackerras 	}
299cf9efce0SPaul Mackerras 
300cf9efce0SPaul Mackerras 	stolen += get_paca()->stolen_time;
301cf9efce0SPaul Mackerras 	get_paca()->stolen_time = 0;
302cf9efce0SPaul Mackerras 	return stolen;
303cf9efce0SPaul Mackerras }
304cf9efce0SPaul Mackerras 
305cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */
306cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
307cf9efce0SPaul Mackerras {
308cf9efce0SPaul Mackerras 	return 0;
309cf9efce0SPaul Mackerras }
310cf9efce0SPaul Mackerras 
311cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
312cf9efce0SPaul Mackerras 
3134603ac18SMichael Neuling /*
314c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
315c6622f63SPaul Mackerras  * or soft irq state.
316c6622f63SPaul Mackerras  */
317c6622f63SPaul Mackerras void account_system_vtime(struct task_struct *tsk)
318c6622f63SPaul Mackerras {
319cf9efce0SPaul Mackerras 	u64 now, nowscaled, delta, deltascaled;
320c6622f63SPaul Mackerras 	unsigned long flags;
321cf9efce0SPaul Mackerras 	u64 stolen, udelta, sys_scaled, user_scaled;
322c6622f63SPaul Mackerras 
323c6622f63SPaul Mackerras 	local_irq_save(flags);
324cf9efce0SPaul Mackerras 	now = mftb();
3254603ac18SMichael Neuling 	nowscaled = read_spurr(now);
326cf9efce0SPaul Mackerras 	get_paca()->system_time += now - get_paca()->starttime;
327cf9efce0SPaul Mackerras 	get_paca()->starttime = now;
3284603ac18SMichael Neuling 	deltascaled = nowscaled - get_paca()->startspurr;
3294603ac18SMichael Neuling 	get_paca()->startspurr = nowscaled;
330cf9efce0SPaul Mackerras 
331cf9efce0SPaul Mackerras 	stolen = calculate_stolen_time(now);
332cf9efce0SPaul Mackerras 
333cf9efce0SPaul Mackerras 	delta = get_paca()->system_time;
334c6622f63SPaul Mackerras 	get_paca()->system_time = 0;
335cf9efce0SPaul Mackerras 	udelta = get_paca()->user_time - get_paca()->utime_sspurr;
336cf9efce0SPaul Mackerras 	get_paca()->utime_sspurr = get_paca()->user_time;
337cf9efce0SPaul Mackerras 
338cf9efce0SPaul Mackerras 	/*
339cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
340cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
341cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
342cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
343cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
344cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
345cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
346cf9efce0SPaul Mackerras 	 * used by account_process_tick.
347cf9efce0SPaul Mackerras 	 */
348cf9efce0SPaul Mackerras 	sys_scaled = delta;
349cf9efce0SPaul Mackerras 	user_scaled = udelta;
350cf9efce0SPaul Mackerras 	if (deltascaled != delta + udelta) {
351cf9efce0SPaul Mackerras 		if (udelta) {
352cf9efce0SPaul Mackerras 			sys_scaled = deltascaled * delta / (delta + udelta);
353cf9efce0SPaul Mackerras 			user_scaled = deltascaled - sys_scaled;
354cf9efce0SPaul Mackerras 		} else {
355cf9efce0SPaul Mackerras 			sys_scaled = deltascaled;
356c6622f63SPaul Mackerras 		}
357cf9efce0SPaul Mackerras 	}
358cf9efce0SPaul Mackerras 	get_paca()->user_time_scaled += user_scaled;
359cf9efce0SPaul Mackerras 
360ad5d1c88SAnton Blanchard 	if (in_interrupt() || idle_task(smp_processor_id()) != tsk) {
361cf9efce0SPaul Mackerras 		account_system_time(tsk, 0, delta, sys_scaled);
362cf9efce0SPaul Mackerras 		if (stolen)
363cf9efce0SPaul Mackerras 			account_steal_time(stolen);
364cf9efce0SPaul Mackerras 	} else {
365cf9efce0SPaul Mackerras 		account_idle_time(delta + stolen);
366cf9efce0SPaul Mackerras 	}
367c6622f63SPaul Mackerras 	local_irq_restore(flags);
368c6622f63SPaul Mackerras }
3694ab79aa8SAlexander Graf EXPORT_SYMBOL_GPL(account_system_vtime);
370c6622f63SPaul Mackerras 
371c6622f63SPaul Mackerras /*
372c6622f63SPaul Mackerras  * Transfer the user and system times accumulated in the paca
373c6622f63SPaul Mackerras  * by the exception entry and exit code to the generic process
374c6622f63SPaul Mackerras  * user and system time records.
375c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
376cf9efce0SPaul Mackerras  * Assumes that account_system_vtime() has been called recently
377cf9efce0SPaul Mackerras  * (i.e. since the last entry from usermode) so that
378cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
379c6622f63SPaul Mackerras  */
380fa13a5a1SPaul Mackerras void account_process_tick(struct task_struct *tsk, int user_tick)
381c6622f63SPaul Mackerras {
3824603ac18SMichael Neuling 	cputime_t utime, utimescaled;
383c6622f63SPaul Mackerras 
384c6622f63SPaul Mackerras 	utime = get_paca()->user_time;
385cf9efce0SPaul Mackerras 	utimescaled = get_paca()->user_time_scaled;
386c6622f63SPaul Mackerras 	get_paca()->user_time = 0;
387cf9efce0SPaul Mackerras 	get_paca()->user_time_scaled = 0;
388cf9efce0SPaul Mackerras 	get_paca()->utime_sspurr = 0;
389457533a7SMartin Schwidefsky 	account_user_time(tsk, utime, utimescaled);
390c6622f63SPaul Mackerras }
391c6622f63SPaul Mackerras 
392c6622f63SPaul Mackerras #else /* ! CONFIG_VIRT_CPU_ACCOUNTING */
393c6622f63SPaul Mackerras #define calc_cputime_factors()
394c6622f63SPaul Mackerras #endif
395c6622f63SPaul Mackerras 
3966defa38bSPaul Mackerras void __delay(unsigned long loops)
3976defa38bSPaul Mackerras {
3986defa38bSPaul Mackerras 	unsigned long start;
3996defa38bSPaul Mackerras 	int diff;
4006defa38bSPaul Mackerras 
4016defa38bSPaul Mackerras 	if (__USE_RTC()) {
4026defa38bSPaul Mackerras 		start = get_rtcl();
4036defa38bSPaul Mackerras 		do {
4046defa38bSPaul Mackerras 			/* the RTCL register wraps at 1000000000 */
4056defa38bSPaul Mackerras 			diff = get_rtcl() - start;
4066defa38bSPaul Mackerras 			if (diff < 0)
4076defa38bSPaul Mackerras 				diff += 1000000000;
4086defa38bSPaul Mackerras 		} while (diff < loops);
4096defa38bSPaul Mackerras 	} else {
4106defa38bSPaul Mackerras 		start = get_tbl();
4116defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
4126defa38bSPaul Mackerras 			HMT_low();
4136defa38bSPaul Mackerras 		HMT_medium();
4146defa38bSPaul Mackerras 	}
4156defa38bSPaul Mackerras }
4166defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4176defa38bSPaul Mackerras 
4186defa38bSPaul Mackerras void udelay(unsigned long usecs)
4196defa38bSPaul Mackerras {
4206defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4216defa38bSPaul Mackerras }
4226defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4236defa38bSPaul Mackerras 
424f2783c15SPaul Mackerras #ifdef CONFIG_SMP
425f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
426f2783c15SPaul Mackerras {
427f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
428f2783c15SPaul Mackerras 
429f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
430f2783c15SPaul Mackerras 		return regs->link;
431f2783c15SPaul Mackerras 
432f2783c15SPaul Mackerras 	return pc;
433f2783c15SPaul Mackerras }
434f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
435f2783c15SPaul Mackerras #endif
436f2783c15SPaul Mackerras 
437f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
438f2783c15SPaul Mackerras 
439f2783c15SPaul Mackerras /*
440f2783c15SPaul Mackerras  * This function recalibrates the timebase based on the 49-bit time-of-day
441f2783c15SPaul Mackerras  * value in the Titan chip.  The Titan is much more accurate than the value
442f2783c15SPaul Mackerras  * returned by the service processor for the timebase frequency.
443f2783c15SPaul Mackerras  */
444f2783c15SPaul Mackerras 
44571712b45STony Breeds static int __init iSeries_tb_recal(void)
446f2783c15SPaul Mackerras {
447f2783c15SPaul Mackerras 	unsigned long titan, tb;
44871712b45STony Breeds 
44971712b45STony Breeds 	/* Make sure we only run on iSeries */
45071712b45STony Breeds 	if (!firmware_has_feature(FW_FEATURE_ISERIES))
45171712b45STony Breeds 		return -ENODEV;
45271712b45STony Breeds 
453f2783c15SPaul Mackerras 	tb = get_tb();
454f2783c15SPaul Mackerras 	titan = HvCallXm_loadTod();
455f2783c15SPaul Mackerras 	if ( iSeries_recal_titan ) {
456f2783c15SPaul Mackerras 		unsigned long tb_ticks = tb - iSeries_recal_tb;
457f2783c15SPaul Mackerras 		unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
458f2783c15SPaul Mackerras 		unsigned long new_tb_ticks_per_sec   = (tb_ticks * USEC_PER_SEC)/titan_usec;
45914ea58adSJulia Lawall 		unsigned long new_tb_ticks_per_jiffy =
46014ea58adSJulia Lawall 			DIV_ROUND_CLOSEST(new_tb_ticks_per_sec, HZ);
461f2783c15SPaul Mackerras 		long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
462f2783c15SPaul Mackerras 		char sign = '+';
463f2783c15SPaul Mackerras 		/* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
464f2783c15SPaul Mackerras 		new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
465f2783c15SPaul Mackerras 
466f2783c15SPaul Mackerras 		if ( tick_diff < 0 ) {
467f2783c15SPaul Mackerras 			tick_diff = -tick_diff;
468f2783c15SPaul Mackerras 			sign = '-';
469f2783c15SPaul Mackerras 		}
470f2783c15SPaul Mackerras 		if ( tick_diff ) {
471f2783c15SPaul Mackerras 			if ( tick_diff < tb_ticks_per_jiffy/25 ) {
472f2783c15SPaul Mackerras 				printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
473f2783c15SPaul Mackerras 						new_tb_ticks_per_jiffy, sign, tick_diff );
474f2783c15SPaul Mackerras 				tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
475f2783c15SPaul Mackerras 				tb_ticks_per_sec   = new_tb_ticks_per_sec;
476c6622f63SPaul Mackerras 				calc_cputime_factors();
477a7f290daSBenjamin Herrenschmidt 				vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
478a42548a1SStanislaw Gruszka 				setup_cputime_one_jiffy();
479f2783c15SPaul Mackerras 			}
480f2783c15SPaul Mackerras 			else {
481f2783c15SPaul Mackerras 				printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
482f2783c15SPaul Mackerras 					"                   new tb_ticks_per_jiffy = %lu\n"
483f2783c15SPaul Mackerras 					"                   old tb_ticks_per_jiffy = %lu\n",
484f2783c15SPaul Mackerras 					new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
485f2783c15SPaul Mackerras 			}
486f2783c15SPaul Mackerras 		}
487f2783c15SPaul Mackerras 	}
488f2783c15SPaul Mackerras 	iSeries_recal_titan = titan;
489f2783c15SPaul Mackerras 	iSeries_recal_tb = tb;
49071712b45STony Breeds 
4914a4cfe38STony Breeds 	/* Called here as now we know accurate values for the timebase */
4924a4cfe38STony Breeds 	clocksource_init();
49371712b45STony Breeds 	return 0;
494f2783c15SPaul Mackerras }
49571712b45STony Breeds late_initcall(iSeries_tb_recal);
49671712b45STony Breeds 
49771712b45STony Breeds /* Called from platform early init */
49871712b45STony Breeds void __init iSeries_time_init_early(void)
49971712b45STony Breeds {
50071712b45STony Breeds 	iSeries_recal_tb = get_tb();
50171712b45STony Breeds 	iSeries_recal_titan = HvCallXm_loadTod();
50271712b45STony Breeds }
50371712b45STony Breeds #endif /* CONFIG_PPC_ISERIES */
504f2783c15SPaul Mackerras 
505e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
506105988c0SPaul Mackerras 
5070fe1ac48SPaul Mackerras /*
5080fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
5090fe1ac48SPaul Mackerras  */
5100fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
511e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void)
512105988c0SPaul Mackerras {
5130fe1ac48SPaul Mackerras 	unsigned long x;
5140fe1ac48SPaul Mackerras 
5150fe1ac48SPaul Mackerras 	asm volatile("lbz %0,%1(13)"
5160fe1ac48SPaul Mackerras 		: "=r" (x)
517e360adbeSPeter Zijlstra 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
5180fe1ac48SPaul Mackerras 	return x;
519105988c0SPaul Mackerras }
520105988c0SPaul Mackerras 
521e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
5220fe1ac48SPaul Mackerras {
5230fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
5240fe1ac48SPaul Mackerras 		"r" (1),
525e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5260fe1ac48SPaul Mackerras }
5270fe1ac48SPaul Mackerras 
528e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
5290fe1ac48SPaul Mackerras {
5300fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
5310fe1ac48SPaul Mackerras 		"r" (0),
532e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
5330fe1ac48SPaul Mackerras }
5340fe1ac48SPaul Mackerras 
5350fe1ac48SPaul Mackerras #else /* 32-bit */
5360fe1ac48SPaul Mackerras 
537e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
5380fe1ac48SPaul Mackerras 
539e360adbeSPeter Zijlstra #define set_irq_work_pending_flag()	__get_cpu_var(irq_work_pending) = 1
540e360adbeSPeter Zijlstra #define test_irq_work_pending()		__get_cpu_var(irq_work_pending)
541e360adbeSPeter Zijlstra #define clear_irq_work_pending()	__get_cpu_var(irq_work_pending) = 0
542105988c0SPaul Mackerras 
5430fe1ac48SPaul Mackerras #endif /* 32 vs 64 bit */
5440fe1ac48SPaul Mackerras 
5454f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
5460fe1ac48SPaul Mackerras {
5470fe1ac48SPaul Mackerras 	preempt_disable();
548e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
5490fe1ac48SPaul Mackerras 	set_dec(1);
5500fe1ac48SPaul Mackerras 	preempt_enable();
5510fe1ac48SPaul Mackerras }
5520fe1ac48SPaul Mackerras 
553e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
554105988c0SPaul Mackerras 
555e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
556e360adbeSPeter Zijlstra #define clear_irq_work_pending()
557105988c0SPaul Mackerras 
558e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
559105988c0SPaul Mackerras 
560f2783c15SPaul Mackerras /*
561f2783c15SPaul Mackerras  * For iSeries shared processors, we have to let the hypervisor
562f2783c15SPaul Mackerras  * set the hardware decrementer.  We set a virtual decrementer
563f2783c15SPaul Mackerras  * in the lppaca and call the hypervisor if the virtual
564f2783c15SPaul Mackerras  * decrementer is less than the current value in the hardware
565f2783c15SPaul Mackerras  * decrementer. (almost always the new decrementer value will
566f2783c15SPaul Mackerras  * be greater than the current hardware decementer so the hypervisor
567f2783c15SPaul Mackerras  * call will not be needed)
568f2783c15SPaul Mackerras  */
569f2783c15SPaul Mackerras 
570f2783c15SPaul Mackerras /*
571f2783c15SPaul Mackerras  * timer_interrupt - gets called when the decrementer overflows,
572f2783c15SPaul Mackerras  * with interrupts disabled.
573f2783c15SPaul Mackerras  */
574f2783c15SPaul Mackerras void timer_interrupt(struct pt_regs * regs)
575f2783c15SPaul Mackerras {
5767d12e780SDavid Howells 	struct pt_regs *old_regs;
5776e6b44e8SMilton Miller 	struct decrementer_clock *decrementer =  &__get_cpu_var(decrementers);
5786e6b44e8SMilton Miller 	struct clock_event_device *evt = &decrementer->event;
579d968014bSPaul Mackerras 	u64 now;
580d831d0b8STony Breeds 
581963e5d3bSBenjamin Herrenschmidt 	/* Ensure a positive value is written to the decrementer, or else
582963e5d3bSBenjamin Herrenschmidt 	 * some CPUs will continue to take decrementer exceptions.
583963e5d3bSBenjamin Herrenschmidt 	 */
584963e5d3bSBenjamin Herrenschmidt 	set_dec(DECREMENTER_MAX);
585963e5d3bSBenjamin Herrenschmidt 
586963e5d3bSBenjamin Herrenschmidt 	/* Some implementations of hotplug will get timer interrupts while
587963e5d3bSBenjamin Herrenschmidt 	 * offline, just ignore these
588963e5d3bSBenjamin Herrenschmidt 	 */
589963e5d3bSBenjamin Herrenschmidt 	if (!cpu_online(smp_processor_id()))
590963e5d3bSBenjamin Herrenschmidt 		return;
591963e5d3bSBenjamin Herrenschmidt 
5926795b85cSAnton Blanchard 	trace_timer_interrupt_entry(regs);
5936795b85cSAnton Blanchard 
59489713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).timer_irqs++;
59589713ed1SAnton Blanchard 
596b0d278b7SPaul Mackerras #if defined(CONFIG_PPC32) && defined(CONFIG_PMAC)
597f2783c15SPaul Mackerras 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
598f2783c15SPaul Mackerras 		do_IRQ(regs);
599f2783c15SPaul Mackerras #endif
600f2783c15SPaul Mackerras 
6017d12e780SDavid Howells 	old_regs = set_irq_regs(regs);
602f2783c15SPaul Mackerras 	irq_enter();
603f2783c15SPaul Mackerras 
604e360adbeSPeter Zijlstra 	if (test_irq_work_pending()) {
605e360adbeSPeter Zijlstra 		clear_irq_work_pending();
606e360adbeSPeter Zijlstra 		irq_work_run();
6070fe1ac48SPaul Mackerras 	}
6080fe1ac48SPaul Mackerras 
609f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
610501b6d29SStephen Rothwell 	if (firmware_has_feature(FW_FEATURE_ISERIES))
6113356bb9fSDavid Gibson 		get_lppaca()->int_dword.fields.decr_int = 0;
612f2783c15SPaul Mackerras #endif
613f2783c15SPaul Mackerras 
614b0d278b7SPaul Mackerras 	now = get_tb_or_rtc();
615b0d278b7SPaul Mackerras 	if (now >= decrementer->next_tb) {
616b0d278b7SPaul Mackerras 		decrementer->next_tb = ~(u64)0;
617d831d0b8STony Breeds 		if (evt->event_handler)
618d831d0b8STony Breeds 			evt->event_handler(evt);
619b0d278b7SPaul Mackerras 	} else {
620b0d278b7SPaul Mackerras 		now = decrementer->next_tb - now;
621b0d278b7SPaul Mackerras 		if (now <= DECREMENTER_MAX)
622b0d278b7SPaul Mackerras 			set_dec((int)now);
623b0d278b7SPaul Mackerras 	}
624f2783c15SPaul Mackerras 
625f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
626501b6d29SStephen Rothwell 	if (firmware_has_feature(FW_FEATURE_ISERIES) && hvlpevent_is_pending())
62735a84c2fSOlaf Hering 		process_hvlpevents();
628f2783c15SPaul Mackerras #endif
629f2783c15SPaul Mackerras 
630f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
631f2783c15SPaul Mackerras 	/* collect purr register values often, for accurate calculations */
632f2783c15SPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
633f2783c15SPaul Mackerras 		struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
634f2783c15SPaul Mackerras 		cu->current_tb = mfspr(SPRN_PURR);
635f2783c15SPaul Mackerras 	}
636f2783c15SPaul Mackerras #endif
637f2783c15SPaul Mackerras 
638f2783c15SPaul Mackerras 	irq_exit();
6397d12e780SDavid Howells 	set_irq_regs(old_regs);
6406795b85cSAnton Blanchard 
6416795b85cSAnton Blanchard 	trace_timer_interrupt_exit(regs);
642f2783c15SPaul Mackerras }
643f2783c15SPaul Mackerras 
6447ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
645d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void)
6467ac5dde9SScott Wood {
6477ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
6487ac5dde9SScott Wood 	 * with suspending.
6497ac5dde9SScott Wood 	 */
6507ac5dde9SScott Wood 
6517ac5dde9SScott Wood 	set_dec(0x7fffffff);
6527ac5dde9SScott Wood 	local_irq_disable();
6537ac5dde9SScott Wood 	set_dec(0x7fffffff);
6547ac5dde9SScott Wood }
6557ac5dde9SScott Wood 
656d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void)
6577ac5dde9SScott Wood {
6587ac5dde9SScott Wood 	local_irq_enable();
6597ac5dde9SScott Wood }
6607ac5dde9SScott Wood 
6617ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6627ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
6637ac5dde9SScott Wood {
6647ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
6657ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
6667ac5dde9SScott Wood 	generic_suspend_disable_irqs();
6677ac5dde9SScott Wood }
6687ac5dde9SScott Wood 
6697ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6707ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
6717ac5dde9SScott Wood {
6727ac5dde9SScott Wood 	generic_suspend_enable_irqs();
6737ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
6747ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
6757ac5dde9SScott Wood }
6767ac5dde9SScott Wood #endif
6777ac5dde9SScott Wood 
678f2783c15SPaul Mackerras /*
679f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
680f2783c15SPaul Mackerras  *
681f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
682f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
683f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
684f2783c15SPaul Mackerras  */
685f2783c15SPaul Mackerras unsigned long long sched_clock(void)
686f2783c15SPaul Mackerras {
68796c44507SPaul Mackerras 	if (__USE_RTC())
68896c44507SPaul Mackerras 		return get_rtc();
689fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
690f2783c15SPaul Mackerras }
691f2783c15SPaul Mackerras 
6920bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
693f2783c15SPaul Mackerras {
694f2783c15SPaul Mackerras 	struct device_node *cpu;
695a7f67bdfSJeremy Kerr 	const unsigned int *fp;
6960bb474a4SAnton Blanchard 	int found = 0;
697f2783c15SPaul Mackerras 
6980bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
699f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
700f2783c15SPaul Mackerras 
701d8a8188dSOlaf Hering 	if (cpu) {
702e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
703d8a8188dSOlaf Hering 		if (fp) {
7040bb474a4SAnton Blanchard 			found = 1;
705a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
706f2783c15SPaul Mackerras 		}
7070bb474a4SAnton Blanchard 
7080bb474a4SAnton Blanchard 		of_node_put(cpu);
709f2783c15SPaul Mackerras 	}
7100bb474a4SAnton Blanchard 
7110bb474a4SAnton Blanchard 	return found;
7120bb474a4SAnton Blanchard }
7130bb474a4SAnton Blanchard 
71477c0a700SBenjamin Herrenschmidt /* should become __cpuinit when secondary_cpu_time_init also is */
71577c0a700SBenjamin Herrenschmidt void start_cpu_decrementer(void)
71677c0a700SBenjamin Herrenschmidt {
71777c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
71877c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
71977c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
72077c0a700SBenjamin Herrenschmidt 
72177c0a700SBenjamin Herrenschmidt 	/* Enable decrementer interrupt */
72277c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TCR, TCR_DIE);
72377c0a700SBenjamin Herrenschmidt #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
72477c0a700SBenjamin Herrenschmidt }
72577c0a700SBenjamin Herrenschmidt 
7260bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
7270bb474a4SAnton Blanchard {
7280bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
7290bb474a4SAnton Blanchard 
7300bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
7310bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
7320bb474a4SAnton Blanchard 
733f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
734f2783c15SPaul Mackerras 				"(not found)\n");
7350bb474a4SAnton Blanchard 	}
736f2783c15SPaul Mackerras 
7370bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
7380bb474a4SAnton Blanchard 
7390bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
7400bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
7410bb474a4SAnton Blanchard 
7420bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
7430bb474a4SAnton Blanchard 				"(not found)\n");
744f2783c15SPaul Mackerras 	}
745f2783c15SPaul Mackerras }
746f2783c15SPaul Mackerras 
747aa3be5f3STony Breeds int update_persistent_clock(struct timespec now)
748f2783c15SPaul Mackerras {
749f2783c15SPaul Mackerras 	struct rtc_time tm;
750f2783c15SPaul Mackerras 
751aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
752aa3be5f3STony Breeds 		return 0;
753aa3be5f3STony Breeds 
754aa3be5f3STony Breeds 	to_tm(now.tv_sec + 1 + timezone_offset, &tm);
755aa3be5f3STony Breeds 	tm.tm_year -= 1900;
756aa3be5f3STony Breeds 	tm.tm_mon -= 1;
757aa3be5f3STony Breeds 
758aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
759aa3be5f3STony Breeds }
760aa3be5f3STony Breeds 
761978d7eb3SBenjamin Herrenschmidt static void __read_persistent_clock(struct timespec *ts)
762aa3be5f3STony Breeds {
763aa3be5f3STony Breeds 	struct rtc_time tm;
764aa3be5f3STony Breeds 	static int first = 1;
765aa3be5f3STony Breeds 
766d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
767aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
768aa3be5f3STony Breeds 	if (first) {
769aa3be5f3STony Breeds 		first = 0;
770aa3be5f3STony Breeds 		if (ppc_md.time_init)
771aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
772aa3be5f3STony Breeds 
773aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
774d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
775d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
776d90246cdSMartin Schwidefsky 			return;
777aa3be5f3STony Breeds 		}
778d90246cdSMartin Schwidefsky 	}
779d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
780d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
781d90246cdSMartin Schwidefsky 		return;
782d90246cdSMartin Schwidefsky 	}
783f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
784978d7eb3SBenjamin Herrenschmidt 
785d4f587c6SMartin Schwidefsky 	ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
786f2783c15SPaul Mackerras 			    tm.tm_hour, tm.tm_min, tm.tm_sec);
787f2783c15SPaul Mackerras }
788f2783c15SPaul Mackerras 
789978d7eb3SBenjamin Herrenschmidt void read_persistent_clock(struct timespec *ts)
790978d7eb3SBenjamin Herrenschmidt {
791978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
792978d7eb3SBenjamin Herrenschmidt 
793978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
794978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
795978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
796978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
797978d7eb3SBenjamin Herrenschmidt 	}
798978d7eb3SBenjamin Herrenschmidt 
799978d7eb3SBenjamin Herrenschmidt }
800978d7eb3SBenjamin Herrenschmidt 
8014a4cfe38STony Breeds /* clocksource code */
8028e19608eSMagnus Damm static cycle_t rtc_read(struct clocksource *cs)
8034a4cfe38STony Breeds {
8044a4cfe38STony Breeds 	return (cycle_t)get_rtc();
8054a4cfe38STony Breeds }
8064a4cfe38STony Breeds 
8078e19608eSMagnus Damm static cycle_t timebase_read(struct clocksource *cs)
8084a4cfe38STony Breeds {
8094a4cfe38STony Breeds 	return (cycle_t)get_tb();
8104a4cfe38STony Breeds }
8114a4cfe38STony Breeds 
8127615856eSJohn Stultz void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
8137615856eSJohn Stultz 			struct clocksource *clock, u32 mult)
8144a4cfe38STony Breeds {
815b0797b60SJohn Stultz 	u64 new_tb_to_xs, new_stamp_xsec;
8160e469db8SPaul Mackerras 	u32 frac_sec;
8174a4cfe38STony Breeds 
8184a4cfe38STony Breeds 	if (clock != &clocksource_timebase)
8194a4cfe38STony Breeds 		return;
8204a4cfe38STony Breeds 
8214a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
8224a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8234a4cfe38STony Breeds 	smp_mb();
8244a4cfe38STony Breeds 
8254a4cfe38STony Breeds 	/* XXX this assumes clock->shift == 22 */
8264a4cfe38STony Breeds 	/* 4611686018 ~= 2^(20+64-22) / 1e9 */
827b0797b60SJohn Stultz 	new_tb_to_xs = (u64) mult * 4611686018ULL;
82806d518e3SJohn Stultz 	new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC;
829b0797b60SJohn Stultz 	do_div(new_stamp_xsec, 1000000000);
83006d518e3SJohn Stultz 	new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC;
831b0797b60SJohn Stultz 
8320e469db8SPaul Mackerras 	BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC);
8330e469db8SPaul Mackerras 	/* this is tv_nsec / 1e9 as a 0.32 fraction */
8340e469db8SPaul Mackerras 	frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32;
83547916be4SThomas Gleixner 
836b0797b60SJohn Stultz 	/*
837b0797b60SJohn Stultz 	 * tb_update_count is used to allow the userspace gettimeofday code
838b0797b60SJohn Stultz 	 * to assure itself that it sees a consistent view of the tb_to_xs and
839b0797b60SJohn Stultz 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
840b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
841b0797b60SJohn Stultz 	 * the two values of tb_update_count match and are even then the
842b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
843b0797b60SJohn Stultz 	 * loops back and reads them again until this criteria is met.
844b0797b60SJohn Stultz 	 * We expect the caller to have done the first increment of
845b0797b60SJohn Stultz 	 * vdso_data->tb_update_count already.
846b0797b60SJohn Stultz 	 */
847b0797b60SJohn Stultz 	vdso_data->tb_orig_stamp = clock->cycle_last;
848b0797b60SJohn Stultz 	vdso_data->stamp_xsec = new_stamp_xsec;
849b0797b60SJohn Stultz 	vdso_data->tb_to_xs = new_tb_to_xs;
8507615856eSJohn Stultz 	vdso_data->wtom_clock_sec = wtm->tv_sec;
8517615856eSJohn Stultz 	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
85206d518e3SJohn Stultz 	vdso_data->stamp_xtime = *wall_time;
85347916be4SThomas Gleixner 	vdso_data->stamp_sec_fraction = frac_sec;
854b0797b60SJohn Stultz 	smp_wmb();
855b0797b60SJohn Stultz 	++(vdso_data->tb_update_count);
8564a4cfe38STony Breeds }
8574a4cfe38STony Breeds 
8584a4cfe38STony Breeds void update_vsyscall_tz(void)
8594a4cfe38STony Breeds {
8604a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
8614a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8624a4cfe38STony Breeds 	smp_mb();
8634a4cfe38STony Breeds 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
8644a4cfe38STony Breeds 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
8654a4cfe38STony Breeds 	smp_mb();
8664a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8674a4cfe38STony Breeds }
8684a4cfe38STony Breeds 
8691c21a293SMichael Ellerman static void __init clocksource_init(void)
8704a4cfe38STony Breeds {
8714a4cfe38STony Breeds 	struct clocksource *clock;
8724a4cfe38STony Breeds 
8734a4cfe38STony Breeds 	if (__USE_RTC())
8744a4cfe38STony Breeds 		clock = &clocksource_rtc;
8754a4cfe38STony Breeds 	else
8764a4cfe38STony Breeds 		clock = &clocksource_timebase;
8774a4cfe38STony Breeds 
8784a4cfe38STony Breeds 	clock->mult = clocksource_hz2mult(tb_ticks_per_sec, clock->shift);
8794a4cfe38STony Breeds 
8804a4cfe38STony Breeds 	if (clocksource_register(clock)) {
8814a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
8824a4cfe38STony Breeds 		       clock->name);
8834a4cfe38STony Breeds 		return;
8844a4cfe38STony Breeds 	}
8854a4cfe38STony Breeds 
8864a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
8874a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
8884a4cfe38STony Breeds }
8894a4cfe38STony Breeds 
89037fb9a02SAnton Blanchard void decrementer_check_overflow(void)
89137fb9a02SAnton Blanchard {
89237fb9a02SAnton Blanchard 	u64 now = get_tb_or_rtc();
89337fb9a02SAnton Blanchard 	struct decrementer_clock *decrementer = &__get_cpu_var(decrementers);
89437fb9a02SAnton Blanchard 
89537fb9a02SAnton Blanchard 	if (now >= decrementer->next_tb)
89637fb9a02SAnton Blanchard 		set_dec(1);
89737fb9a02SAnton Blanchard }
89837fb9a02SAnton Blanchard 
899d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
900d831d0b8STony Breeds 				      struct clock_event_device *dev)
901d831d0b8STony Breeds {
9026e6b44e8SMilton Miller 	__get_cpu_var(decrementers).next_tb = get_tb_or_rtc() + evt;
903d831d0b8STony Breeds 	set_dec(evt);
904d831d0b8STony Breeds 	return 0;
905d831d0b8STony Breeds }
906d831d0b8STony Breeds 
907d831d0b8STony Breeds static void decrementer_set_mode(enum clock_event_mode mode,
908d831d0b8STony Breeds 				 struct clock_event_device *dev)
909d831d0b8STony Breeds {
910d831d0b8STony Breeds 	if (mode != CLOCK_EVT_MODE_ONESHOT)
911d831d0b8STony Breeds 		decrementer_set_next_event(DECREMENTER_MAX, dev);
912d831d0b8STony Breeds }
913d831d0b8STony Breeds 
914d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
915d831d0b8STony Breeds {
9166e6b44e8SMilton Miller 	struct clock_event_device *dec = &per_cpu(decrementers, cpu).event;
917d831d0b8STony Breeds 
918d831d0b8STony Breeds 	*dec = decrementer_clockevent;
919320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
920d831d0b8STony Breeds 
921b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
922d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
923d831d0b8STony Breeds 
924d831d0b8STony Breeds 	clockevents_register_device(dec);
925d831d0b8STony Breeds }
926d831d0b8STony Breeds 
927c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
928d831d0b8STony Breeds {
929d831d0b8STony Breeds 	int cpu = smp_processor_id();
930d831d0b8STony Breeds 
931*d8afc6fdSAnton Blanchard 	clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4);
932*d8afc6fdSAnton Blanchard 
933d831d0b8STony Breeds 	decrementer_clockevent.max_delta_ns =
934d831d0b8STony Breeds 		clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent);
93543875cc0SPaul Mackerras 	decrementer_clockevent.min_delta_ns =
93643875cc0SPaul Mackerras 		clockevent_delta2ns(2, &decrementer_clockevent);
937d831d0b8STony Breeds 
938d831d0b8STony Breeds 	register_decrementer_clockevent(cpu);
939d831d0b8STony Breeds }
940d831d0b8STony Breeds 
941d831d0b8STony Breeds void secondary_cpu_time_init(void)
942d831d0b8STony Breeds {
94377c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
94477c0a700SBenjamin Herrenschmidt 	 * such as BookE
94577c0a700SBenjamin Herrenschmidt 	 */
94677c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
94777c0a700SBenjamin Herrenschmidt 
948d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
949d831d0b8STony Breeds 	 * call here ! */
950d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
951d831d0b8STony Breeds }
952d831d0b8STony Breeds 
953f2783c15SPaul Mackerras /* This function is only called on the boot processor */
954f2783c15SPaul Mackerras void __init time_init(void)
955f2783c15SPaul Mackerras {
956f2783c15SPaul Mackerras 	struct div_result res;
957d75d68cfSPaul Mackerras 	u64 scale;
958f2783c15SPaul Mackerras 	unsigned shift;
959f2783c15SPaul Mackerras 
96096c44507SPaul Mackerras 	if (__USE_RTC()) {
96196c44507SPaul Mackerras 		/* 601 processor: dec counts down by 128 every 128ns */
96296c44507SPaul Mackerras 		ppc_tb_freq = 1000000000;
96396c44507SPaul Mackerras 	} else {
96496c44507SPaul Mackerras 		/* Normal PowerPC with timebase register */
965f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
966224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
967374e99d4SPaul Mackerras 		       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
968224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
969374e99d4SPaul Mackerras 		       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
97096c44507SPaul Mackerras 	}
971374e99d4SPaul Mackerras 
972374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
973092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
974374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
975c6622f63SPaul Mackerras 	calc_cputime_factors();
976a42548a1SStanislaw Gruszka 	setup_cputime_one_jiffy();
977092b8f34SPaul Mackerras 
978092b8f34SPaul Mackerras 	/*
979f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
980f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
981f2783c15SPaul Mackerras 	 * which is the timebase frequency.
982f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
983f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
984f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
985f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
986f2783c15SPaul Mackerras 	 * sched_clock().
987f2783c15SPaul Mackerras 	 */
988f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
989f2783c15SPaul Mackerras 	scale = res.result_low;
990f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
991f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
992f2783c15SPaul Mackerras 		res.result_high >>= 1;
993f2783c15SPaul Mackerras 	}
994f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
995f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
996fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
997c27da339SBenjamin Herrenschmidt 	boot_tb = get_tb_or_rtc();
998f2783c15SPaul Mackerras 
999092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
1000092b8f34SPaul Mackerras         if (timezone_offset) {
1001092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
1002092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
1003092b8f34SPaul Mackerras         }
1004092b8f34SPaul Mackerras 
1005a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
1006a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
1007f2783c15SPaul Mackerras 
100877c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
100977c0a700SBenjamin Herrenschmidt 	 * such as BookE
101077c0a700SBenjamin Herrenschmidt 	 */
101177c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
101277c0a700SBenjamin Herrenschmidt 
10134a4cfe38STony Breeds 	/* Register the clocksource, if we're not running on iSeries */
10144a4cfe38STony Breeds 	if (!firmware_has_feature(FW_FEATURE_ISERIES))
10154a4cfe38STony Breeds 		clocksource_init();
10164a4cfe38STony Breeds 
1017d831d0b8STony Breeds 	init_decrementer_clockevent();
1018f2783c15SPaul Mackerras }
1019f2783c15SPaul Mackerras 
1020f2783c15SPaul Mackerras 
1021f2783c15SPaul Mackerras #define FEBRUARY	2
1022f2783c15SPaul Mackerras #define	STARTOFTIME	1970
1023f2783c15SPaul Mackerras #define SECDAY		86400L
1024f2783c15SPaul Mackerras #define SECYR		(SECDAY * 365)
1025f2783c15SPaul Mackerras #define	leapyear(year)		((year) % 4 == 0 && \
1026f2783c15SPaul Mackerras 				 ((year) % 100 != 0 || (year) % 400 == 0))
1027f2783c15SPaul Mackerras #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
1028f2783c15SPaul Mackerras #define	days_in_month(a) 	(month_days[(a) - 1])
1029f2783c15SPaul Mackerras 
1030f2783c15SPaul Mackerras static int month_days[12] = {
1031f2783c15SPaul Mackerras 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1032f2783c15SPaul Mackerras };
1033f2783c15SPaul Mackerras 
1034f2783c15SPaul Mackerras /*
1035f2783c15SPaul Mackerras  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1036f2783c15SPaul Mackerras  */
1037f2783c15SPaul Mackerras void GregorianDay(struct rtc_time * tm)
1038f2783c15SPaul Mackerras {
1039f2783c15SPaul Mackerras 	int leapsToDate;
1040f2783c15SPaul Mackerras 	int lastYear;
1041f2783c15SPaul Mackerras 	int day;
1042f2783c15SPaul Mackerras 	int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1043f2783c15SPaul Mackerras 
1044f2783c15SPaul Mackerras 	lastYear = tm->tm_year - 1;
1045f2783c15SPaul Mackerras 
1046f2783c15SPaul Mackerras 	/*
1047f2783c15SPaul Mackerras 	 * Number of leap corrections to apply up to end of last year
1048f2783c15SPaul Mackerras 	 */
1049f2783c15SPaul Mackerras 	leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1050f2783c15SPaul Mackerras 
1051f2783c15SPaul Mackerras 	/*
1052f2783c15SPaul Mackerras 	 * This year is a leap year if it is divisible by 4 except when it is
1053f2783c15SPaul Mackerras 	 * divisible by 100 unless it is divisible by 400
1054f2783c15SPaul Mackerras 	 *
1055f2783c15SPaul Mackerras 	 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1056f2783c15SPaul Mackerras 	 */
1057f2783c15SPaul Mackerras 	day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1058f2783c15SPaul Mackerras 
1059f2783c15SPaul Mackerras 	day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1060f2783c15SPaul Mackerras 		   tm->tm_mday;
1061f2783c15SPaul Mackerras 
1062f2783c15SPaul Mackerras 	tm->tm_wday = day % 7;
1063f2783c15SPaul Mackerras }
1064f2783c15SPaul Mackerras 
1065f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm)
1066f2783c15SPaul Mackerras {
1067f2783c15SPaul Mackerras 	register int    i;
1068f2783c15SPaul Mackerras 	register long   hms, day;
1069f2783c15SPaul Mackerras 
1070f2783c15SPaul Mackerras 	day = tim / SECDAY;
1071f2783c15SPaul Mackerras 	hms = tim % SECDAY;
1072f2783c15SPaul Mackerras 
1073f2783c15SPaul Mackerras 	/* Hours, minutes, seconds are easy */
1074f2783c15SPaul Mackerras 	tm->tm_hour = hms / 3600;
1075f2783c15SPaul Mackerras 	tm->tm_min = (hms % 3600) / 60;
1076f2783c15SPaul Mackerras 	tm->tm_sec = (hms % 3600) % 60;
1077f2783c15SPaul Mackerras 
1078f2783c15SPaul Mackerras 	/* Number of years in days */
1079f2783c15SPaul Mackerras 	for (i = STARTOFTIME; day >= days_in_year(i); i++)
1080f2783c15SPaul Mackerras 		day -= days_in_year(i);
1081f2783c15SPaul Mackerras 	tm->tm_year = i;
1082f2783c15SPaul Mackerras 
1083f2783c15SPaul Mackerras 	/* Number of months in days left */
1084f2783c15SPaul Mackerras 	if (leapyear(tm->tm_year))
1085f2783c15SPaul Mackerras 		days_in_month(FEBRUARY) = 29;
1086f2783c15SPaul Mackerras 	for (i = 1; day >= days_in_month(i); i++)
1087f2783c15SPaul Mackerras 		day -= days_in_month(i);
1088f2783c15SPaul Mackerras 	days_in_month(FEBRUARY) = 28;
1089f2783c15SPaul Mackerras 	tm->tm_mon = i;
1090f2783c15SPaul Mackerras 
1091f2783c15SPaul Mackerras 	/* Days are what is left over (+1) from all that. */
1092f2783c15SPaul Mackerras 	tm->tm_mday = day + 1;
1093f2783c15SPaul Mackerras 
1094f2783c15SPaul Mackerras 	/*
1095f2783c15SPaul Mackerras 	 * Determine the day of week
1096f2783c15SPaul Mackerras 	 */
1097f2783c15SPaul Mackerras 	GregorianDay(tm);
1098f2783c15SPaul Mackerras }
1099f2783c15SPaul Mackerras 
1100f2783c15SPaul Mackerras /*
1101f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1102f2783c15SPaul Mackerras  * result.
1103f2783c15SPaul Mackerras  */
1104f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1105f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1106f2783c15SPaul Mackerras {
1107f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1108f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1109f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1110f2783c15SPaul Mackerras 
1111f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1112f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1113f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1114f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1115f2783c15SPaul Mackerras 
1116f2783c15SPaul Mackerras 	w = a / divisor;
1117f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1118f2783c15SPaul Mackerras 
1119f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1120f2783c15SPaul Mackerras 	x = ra;
1121f2783c15SPaul Mackerras 
1122f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1123f2783c15SPaul Mackerras 	y = rb;
1124f2783c15SPaul Mackerras 
1125f2783c15SPaul Mackerras 	do_div(rc, divisor);
1126f2783c15SPaul Mackerras 	z = rc;
1127f2783c15SPaul Mackerras 
1128f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1129f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1130f2783c15SPaul Mackerras 
1131f2783c15SPaul Mackerras }
1132bcd68a70SGeert Uytterhoeven 
1133177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
1134177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
1135177996e6SBenjamin Herrenschmidt {
1136177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
1137177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
1138177996e6SBenjamin Herrenschmidt 	 */
1139177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
1140177996e6SBenjamin Herrenschmidt }
1141177996e6SBenjamin Herrenschmidt 
1142bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1143bcd68a70SGeert Uytterhoeven {
1144bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1145bcd68a70SGeert Uytterhoeven 
1146bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1147bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1148bcd68a70SGeert Uytterhoeven 
1149bcd68a70SGeert Uytterhoeven 	pdev = platform_device_register_simple("rtc-generic", -1, NULL, 0);
1150bcd68a70SGeert Uytterhoeven 	if (IS_ERR(pdev))
1151bcd68a70SGeert Uytterhoeven 		return PTR_ERR(pdev);
1152bcd68a70SGeert Uytterhoeven 
1153bcd68a70SGeert Uytterhoeven 	return 0;
1154bcd68a70SGeert Uytterhoeven }
1155bcd68a70SGeert Uytterhoeven 
1156bcd68a70SGeert Uytterhoeven module_init(rtc_init);
1157