xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision 06b8e878a9bc9301201cffe186eba99c4185f20a)
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>
36f2783c15SPaul Mackerras #include <linux/module.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>
55f2783c15SPaul Mackerras 
56f2783c15SPaul Mackerras #include <asm/io.h>
57f2783c15SPaul Mackerras #include <asm/processor.h>
58f2783c15SPaul Mackerras #include <asm/nvram.h>
59f2783c15SPaul Mackerras #include <asm/cache.h>
60f2783c15SPaul Mackerras #include <asm/machdep.h>
61f2783c15SPaul Mackerras #include <asm/uaccess.h>
62f2783c15SPaul Mackerras #include <asm/time.h>
63f2783c15SPaul Mackerras #include <asm/prom.h>
64f2783c15SPaul Mackerras #include <asm/irq.h>
65f2783c15SPaul Mackerras #include <asm/div64.h>
662249ca9dSPaul Mackerras #include <asm/smp.h>
67a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
68f2783c15SPaul Mackerras #include <asm/firmware.h>
69*06b8e878SMichael Neuling #include <asm/cputime.h>
70f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
718875ccfbSKelly Daly #include <asm/iseries/it_lp_queue.h>
728021b8a7SKelly Daly #include <asm/iseries/hv_call_xm.h>
73f2783c15SPaul Mackerras #endif
74f2783c15SPaul Mackerras 
754a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
764a4cfe38STony Breeds 
77d831d0b8STony Breeds #include <linux/clockchips.h>
784a4cfe38STony Breeds #include <linux/clocksource.h>
794a4cfe38STony Breeds 
804a4cfe38STony Breeds static cycle_t rtc_read(void);
814a4cfe38STony Breeds static struct clocksource clocksource_rtc = {
824a4cfe38STony Breeds 	.name         = "rtc",
834a4cfe38STony Breeds 	.rating       = 400,
844a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
854a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
864a4cfe38STony Breeds 	.shift        = 22,
874a4cfe38STony Breeds 	.mult         = 0,	/* To be filled in */
884a4cfe38STony Breeds 	.read         = rtc_read,
894a4cfe38STony Breeds };
904a4cfe38STony Breeds 
914a4cfe38STony Breeds static cycle_t timebase_read(void);
924a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
934a4cfe38STony Breeds 	.name         = "timebase",
944a4cfe38STony Breeds 	.rating       = 400,
954a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
964a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
974a4cfe38STony Breeds 	.shift        = 22,
984a4cfe38STony Breeds 	.mult         = 0,	/* To be filled in */
994a4cfe38STony Breeds 	.read         = timebase_read,
1004a4cfe38STony Breeds };
1014a4cfe38STony Breeds 
102d831d0b8STony Breeds #define DECREMENTER_MAX	0x7fffffff
103d831d0b8STony Breeds 
104d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
105d831d0b8STony Breeds 				      struct clock_event_device *dev);
106d831d0b8STony Breeds static void decrementer_set_mode(enum clock_event_mode mode,
107d831d0b8STony Breeds 				 struct clock_event_device *dev);
108d831d0b8STony Breeds 
109d831d0b8STony Breeds static struct clock_event_device decrementer_clockevent = {
110d831d0b8STony Breeds        .name           = "decrementer",
111d831d0b8STony Breeds        .rating         = 200,
112cdec12aeSPaul Mackerras        .shift          = 16,
113d831d0b8STony Breeds        .mult           = 0,	/* To be filled in */
114d831d0b8STony Breeds        .irq            = 0,
115d831d0b8STony Breeds        .set_next_event = decrementer_set_next_event,
116d831d0b8STony Breeds        .set_mode       = decrementer_set_mode,
117d831d0b8STony Breeds        .features       = CLOCK_EVT_FEAT_ONESHOT,
118d831d0b8STony Breeds };
119d831d0b8STony Breeds 
1206e6b44e8SMilton Miller struct decrementer_clock {
1216e6b44e8SMilton Miller 	struct clock_event_device event;
1226e6b44e8SMilton Miller 	u64 next_tb;
1236e6b44e8SMilton Miller };
1246e6b44e8SMilton Miller 
1256e6b44e8SMilton Miller static DEFINE_PER_CPU(struct decrementer_clock, decrementers);
126d831d0b8STony Breeds 
127f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
12871712b45STony Breeds static unsigned long __initdata iSeries_recal_titan;
12971712b45STony Breeds static signed long __initdata iSeries_recal_tb;
1304a4cfe38STony Breeds 
1314a4cfe38STony Breeds /* Forward declaration is only needed for iSereis compiles */
1324a4cfe38STony Breeds void __init clocksource_init(void);
133f2783c15SPaul Mackerras #endif
134f2783c15SPaul Mackerras 
135f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
136f2783c15SPaul Mackerras 
137f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
138f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
139f2783c15SPaul Mackerras #else
140f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
141f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
142f2783c15SPaul Mackerras #endif
143f2783c15SPaul Mackerras 
144f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
145f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
146f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
147f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
1482cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime_t conversions */
149f2783c15SPaul Mackerras u64 tb_to_xs;
150f2783c15SPaul Mackerras unsigned tb_to_us;
151092b8f34SPaul Mackerras 
15219923c19SRoman Zippel #define TICKLEN_SCALE	TICK_LENGTH_SHIFT
153092b8f34SPaul Mackerras u64 last_tick_len;	/* units are ns / 2^TICKLEN_SCALE */
154092b8f34SPaul Mackerras u64 ticklen_to_xs;	/* 0.64 fraction */
155092b8f34SPaul Mackerras 
156092b8f34SPaul Mackerras /* If last_tick_len corresponds to about 1/HZ seconds, then
157092b8f34SPaul Mackerras    last_tick_len << TICKLEN_SHIFT will be about 2^63. */
158092b8f34SPaul Mackerras #define TICKLEN_SHIFT	(63 - 30 - TICKLEN_SCALE + SHIFT_HZ)
159092b8f34SPaul Mackerras 
160f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
161f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
162f2783c15SPaul Mackerras 
163fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
164fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
165fc9069feSTony Breeds static unsigned long boot_tb __read_mostly;
166f2783c15SPaul Mackerras 
167f2783c15SPaul Mackerras struct gettimeofday_struct do_gtod;
168f2783c15SPaul Mackerras 
169f2783c15SPaul Mackerras extern struct timezone sys_tz;
170f2783c15SPaul Mackerras static long timezone_offset;
171f2783c15SPaul Mackerras 
172f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
1731474855dSBob Nelson EXPORT_SYMBOL(ppc_proc_freq);
174f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
175f2783c15SPaul Mackerras 
176eb36c288SPaul Mackerras static u64 tb_last_jiffy __cacheline_aligned_in_smp;
177eb36c288SPaul Mackerras static DEFINE_PER_CPU(u64, last_jiffy);
17896c44507SPaul Mackerras 
179c6622f63SPaul Mackerras #ifdef CONFIG_VIRT_CPU_ACCOUNTING
180c6622f63SPaul Mackerras /*
181c6622f63SPaul Mackerras  * Factors for converting from cputime_t (timebase ticks) to
182c6622f63SPaul Mackerras  * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds).
183c6622f63SPaul Mackerras  * These are all stored as 0.64 fixed-point binary fractions.
184c6622f63SPaul Mackerras  */
185c6622f63SPaul Mackerras u64 __cputime_jiffies_factor;
1862cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_jiffies_factor);
187c6622f63SPaul Mackerras u64 __cputime_msec_factor;
1882cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_msec_factor);
189c6622f63SPaul Mackerras u64 __cputime_sec_factor;
1902cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_sec_factor);
191c6622f63SPaul Mackerras u64 __cputime_clockt_factor;
1922cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_clockt_factor);
193*06b8e878SMichael Neuling DEFINE_PER_CPU(unsigned long, cputime_last_delta);
194*06b8e878SMichael Neuling DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta);
195c6622f63SPaul Mackerras 
196c6622f63SPaul Mackerras static void calc_cputime_factors(void)
197c6622f63SPaul Mackerras {
198c6622f63SPaul Mackerras 	struct div_result res;
199c6622f63SPaul Mackerras 
200c6622f63SPaul Mackerras 	div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
201c6622f63SPaul Mackerras 	__cputime_jiffies_factor = res.result_low;
202c6622f63SPaul Mackerras 	div128_by_32(1000, 0, tb_ticks_per_sec, &res);
203c6622f63SPaul Mackerras 	__cputime_msec_factor = res.result_low;
204c6622f63SPaul Mackerras 	div128_by_32(1, 0, tb_ticks_per_sec, &res);
205c6622f63SPaul Mackerras 	__cputime_sec_factor = res.result_low;
206c6622f63SPaul Mackerras 	div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
207c6622f63SPaul Mackerras 	__cputime_clockt_factor = res.result_low;
208c6622f63SPaul Mackerras }
209c6622f63SPaul Mackerras 
210c6622f63SPaul Mackerras /*
211c6622f63SPaul Mackerras  * Read the PURR on systems that have it, otherwise the timebase.
212c6622f63SPaul Mackerras  */
213c6622f63SPaul Mackerras static u64 read_purr(void)
214c6622f63SPaul Mackerras {
215c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
216c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
217c6622f63SPaul Mackerras 	return mftb();
218c6622f63SPaul Mackerras }
219c6622f63SPaul Mackerras 
220c6622f63SPaul Mackerras /*
2214603ac18SMichael Neuling  * Read the SPURR on systems that have it, otherwise the purr
2224603ac18SMichael Neuling  */
2234603ac18SMichael Neuling static u64 read_spurr(u64 purr)
2244603ac18SMichael Neuling {
22553024fe2SMilton Miller 	/*
22653024fe2SMilton Miller 	 * cpus without PURR won't have a SPURR
22753024fe2SMilton Miller 	 * We already know the former when we use this, so tell gcc
22853024fe2SMilton Miller 	 */
22953024fe2SMilton Miller 	if (cpu_has_feature(CPU_FTR_PURR) && cpu_has_feature(CPU_FTR_SPURR))
2304603ac18SMichael Neuling 		return mfspr(SPRN_SPURR);
2314603ac18SMichael Neuling 	return purr;
2324603ac18SMichael Neuling }
2334603ac18SMichael Neuling 
2344603ac18SMichael Neuling /*
235c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
236c6622f63SPaul Mackerras  * or soft irq state.
237c6622f63SPaul Mackerras  */
238c6622f63SPaul Mackerras void account_system_vtime(struct task_struct *tsk)
239c6622f63SPaul Mackerras {
24053024fe2SMilton Miller 	u64 now, nowscaled, delta, deltascaled, sys_time;
241c6622f63SPaul Mackerras 	unsigned long flags;
242c6622f63SPaul Mackerras 
243c6622f63SPaul Mackerras 	local_irq_save(flags);
244c6622f63SPaul Mackerras 	now = read_purr();
2454603ac18SMichael Neuling 	nowscaled = read_spurr(now);
24653024fe2SMilton Miller 	delta = now - get_paca()->startpurr;
2474603ac18SMichael Neuling 	deltascaled = nowscaled - get_paca()->startspurr;
24853024fe2SMilton Miller 	get_paca()->startpurr = now;
2494603ac18SMichael Neuling 	get_paca()->startspurr = nowscaled;
250c6622f63SPaul Mackerras 	if (!in_interrupt()) {
2514603ac18SMichael Neuling 		/* deltascaled includes both user and system time.
2524603ac18SMichael Neuling 		 * Hence scale it based on the purr ratio to estimate
2534603ac18SMichael Neuling 		 * the system time */
25453024fe2SMilton Miller 		sys_time = get_paca()->system_time;
2552b46b567SMichael Neuling 		if (get_paca()->user_time)
25653024fe2SMilton Miller 			deltascaled = deltascaled * sys_time /
25753024fe2SMilton Miller 			     (sys_time + get_paca()->user_time);
25853024fe2SMilton Miller 		delta += sys_time;
259c6622f63SPaul Mackerras 		get_paca()->system_time = 0;
260c6622f63SPaul Mackerras 	}
261c6622f63SPaul Mackerras 	account_system_time(tsk, 0, delta);
2624603ac18SMichael Neuling 	account_system_time_scaled(tsk, deltascaled);
263*06b8e878SMichael Neuling 	per_cpu(cputime_last_delta, smp_processor_id()) = delta;
264*06b8e878SMichael Neuling 	per_cpu(cputime_scaled_last_delta, smp_processor_id()) = deltascaled;
265c6622f63SPaul Mackerras 	local_irq_restore(flags);
266c6622f63SPaul Mackerras }
267c6622f63SPaul Mackerras 
268c6622f63SPaul Mackerras /*
269c6622f63SPaul Mackerras  * Transfer the user and system times accumulated in the paca
270c6622f63SPaul Mackerras  * by the exception entry and exit code to the generic process
271c6622f63SPaul Mackerras  * user and system time records.
272c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
273c6622f63SPaul Mackerras  */
274fa13a5a1SPaul Mackerras void account_process_tick(struct task_struct *tsk, int user_tick)
275c6622f63SPaul Mackerras {
2764603ac18SMichael Neuling 	cputime_t utime, utimescaled;
277c6622f63SPaul Mackerras 
278c6622f63SPaul Mackerras 	utime = get_paca()->user_time;
279c6622f63SPaul Mackerras 	get_paca()->user_time = 0;
280c6622f63SPaul Mackerras 	account_user_time(tsk, utime);
2814603ac18SMichael Neuling 
282*06b8e878SMichael Neuling 	utimescaled = cputime_to_scaled(utime);
2834603ac18SMichael Neuling 	account_user_time_scaled(tsk, utimescaled);
284c6622f63SPaul Mackerras }
285c6622f63SPaul Mackerras 
286c6622f63SPaul Mackerras /*
287c6622f63SPaul Mackerras  * Stuff for accounting stolen time.
288c6622f63SPaul Mackerras  */
289c6622f63SPaul Mackerras struct cpu_purr_data {
290c6622f63SPaul Mackerras 	int	initialized;			/* thread is running */
291c6622f63SPaul Mackerras 	u64	tb;			/* last TB value read */
292c6622f63SPaul Mackerras 	u64	purr;			/* last PURR value read */
2934603ac18SMichael Neuling 	u64	spurr;			/* last SPURR value read */
294c6622f63SPaul Mackerras };
295c6622f63SPaul Mackerras 
296df211c8aSNathan Lynch /*
297df211c8aSNathan Lynch  * Each entry in the cpu_purr_data array is manipulated only by its
298df211c8aSNathan Lynch  * "owner" cpu -- usually in the timer interrupt but also occasionally
299df211c8aSNathan Lynch  * in process context for cpu online.  As long as cpus do not touch
300df211c8aSNathan Lynch  * each others' cpu_purr_data, disabling local interrupts is
301df211c8aSNathan Lynch  * sufficient to serialize accesses.
302df211c8aSNathan Lynch  */
303c6622f63SPaul Mackerras static DEFINE_PER_CPU(struct cpu_purr_data, cpu_purr_data);
304c6622f63SPaul Mackerras 
305c6622f63SPaul Mackerras static void snapshot_tb_and_purr(void *data)
306c6622f63SPaul Mackerras {
307df211c8aSNathan Lynch 	unsigned long flags;
308c6622f63SPaul Mackerras 	struct cpu_purr_data *p = &__get_cpu_var(cpu_purr_data);
309c6622f63SPaul Mackerras 
310df211c8aSNathan Lynch 	local_irq_save(flags);
311c27da339SBenjamin Herrenschmidt 	p->tb = get_tb_or_rtc();
312cbcdb93dSStephen Rothwell 	p->purr = mfspr(SPRN_PURR);
313c6622f63SPaul Mackerras 	wmb();
314c6622f63SPaul Mackerras 	p->initialized = 1;
315df211c8aSNathan Lynch 	local_irq_restore(flags);
316c6622f63SPaul Mackerras }
317c6622f63SPaul Mackerras 
318c6622f63SPaul Mackerras /*
319c6622f63SPaul Mackerras  * Called during boot when all cpus have come up.
320c6622f63SPaul Mackerras  */
321c6622f63SPaul Mackerras void snapshot_timebases(void)
322c6622f63SPaul Mackerras {
323c6622f63SPaul Mackerras 	if (!cpu_has_feature(CPU_FTR_PURR))
324c6622f63SPaul Mackerras 		return;
325c6622f63SPaul Mackerras 	on_each_cpu(snapshot_tb_and_purr, NULL, 0, 1);
326c6622f63SPaul Mackerras }
327c6622f63SPaul Mackerras 
328df211c8aSNathan Lynch /*
329df211c8aSNathan Lynch  * Must be called with interrupts disabled.
330df211c8aSNathan Lynch  */
331c6622f63SPaul Mackerras void calculate_steal_time(void)
332c6622f63SPaul Mackerras {
333cbcdb93dSStephen Rothwell 	u64 tb, purr;
334c6622f63SPaul Mackerras 	s64 stolen;
335cbcdb93dSStephen Rothwell 	struct cpu_purr_data *pme;
336c6622f63SPaul Mackerras 
3378b5621f1SMilton Miller 	pme = &__get_cpu_var(cpu_purr_data);
338c6622f63SPaul Mackerras 	if (!pme->initialized)
339db3801a8SMilton Miller 		return;		/* !CPU_FTR_PURR or early in early boot */
340c6622f63SPaul Mackerras 	tb = mftb();
341cbcdb93dSStephen Rothwell 	purr = mfspr(SPRN_PURR);
342c6622f63SPaul Mackerras 	stolen = (tb - pme->tb) - (purr - pme->purr);
343cbcdb93dSStephen Rothwell 	if (stolen > 0)
344c6622f63SPaul Mackerras 		account_steal_time(current, stolen);
345c6622f63SPaul Mackerras 	pme->tb = tb;
346c6622f63SPaul Mackerras 	pme->purr = purr;
347c6622f63SPaul Mackerras }
348c6622f63SPaul Mackerras 
3494cefebb1SMichael Neuling #ifdef CONFIG_PPC_SPLPAR
350c6622f63SPaul Mackerras /*
351c6622f63SPaul Mackerras  * Must be called before the cpu is added to the online map when
352c6622f63SPaul Mackerras  * a cpu is being brought up at runtime.
353c6622f63SPaul Mackerras  */
354c6622f63SPaul Mackerras static void snapshot_purr(void)
355c6622f63SPaul Mackerras {
356cbcdb93dSStephen Rothwell 	struct cpu_purr_data *pme;
357c6622f63SPaul Mackerras 	unsigned long flags;
358c6622f63SPaul Mackerras 
359c6622f63SPaul Mackerras 	if (!cpu_has_feature(CPU_FTR_PURR))
360c6622f63SPaul Mackerras 		return;
361df211c8aSNathan Lynch 	local_irq_save(flags);
3628b5621f1SMilton Miller 	pme = &__get_cpu_var(cpu_purr_data);
363cbcdb93dSStephen Rothwell 	pme->tb = mftb();
364cbcdb93dSStephen Rothwell 	pme->purr = mfspr(SPRN_PURR);
365c6622f63SPaul Mackerras 	pme->initialized = 1;
366df211c8aSNathan Lynch 	local_irq_restore(flags);
367c6622f63SPaul Mackerras }
368c6622f63SPaul Mackerras 
369c6622f63SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
370c6622f63SPaul Mackerras 
371c6622f63SPaul Mackerras #else /* ! CONFIG_VIRT_CPU_ACCOUNTING */
372c6622f63SPaul Mackerras #define calc_cputime_factors()
373c6622f63SPaul Mackerras #define calculate_steal_time()		do { } while (0)
374c6622f63SPaul Mackerras #endif
375c6622f63SPaul Mackerras 
376c6622f63SPaul Mackerras #if !(defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR))
377c6622f63SPaul Mackerras #define snapshot_purr()			do { } while (0)
378c6622f63SPaul Mackerras #endif
379c6622f63SPaul Mackerras 
380c6622f63SPaul Mackerras /*
381c6622f63SPaul Mackerras  * Called when a cpu comes up after the system has finished booting,
382c6622f63SPaul Mackerras  * i.e. as a result of a hotplug cpu action.
383c6622f63SPaul Mackerras  */
384c6622f63SPaul Mackerras void snapshot_timebase(void)
385c6622f63SPaul Mackerras {
386c27da339SBenjamin Herrenschmidt 	__get_cpu_var(last_jiffy) = get_tb_or_rtc();
387c6622f63SPaul Mackerras 	snapshot_purr();
388c6622f63SPaul Mackerras }
389c6622f63SPaul Mackerras 
3906defa38bSPaul Mackerras void __delay(unsigned long loops)
3916defa38bSPaul Mackerras {
3926defa38bSPaul Mackerras 	unsigned long start;
3936defa38bSPaul Mackerras 	int diff;
3946defa38bSPaul Mackerras 
3956defa38bSPaul Mackerras 	if (__USE_RTC()) {
3966defa38bSPaul Mackerras 		start = get_rtcl();
3976defa38bSPaul Mackerras 		do {
3986defa38bSPaul Mackerras 			/* the RTCL register wraps at 1000000000 */
3996defa38bSPaul Mackerras 			diff = get_rtcl() - start;
4006defa38bSPaul Mackerras 			if (diff < 0)
4016defa38bSPaul Mackerras 				diff += 1000000000;
4026defa38bSPaul Mackerras 		} while (diff < loops);
4036defa38bSPaul Mackerras 	} else {
4046defa38bSPaul Mackerras 		start = get_tbl();
4056defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
4066defa38bSPaul Mackerras 			HMT_low();
4076defa38bSPaul Mackerras 		HMT_medium();
4086defa38bSPaul Mackerras 	}
4096defa38bSPaul Mackerras }
4106defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4116defa38bSPaul Mackerras 
4126defa38bSPaul Mackerras void udelay(unsigned long usecs)
4136defa38bSPaul Mackerras {
4146defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4156defa38bSPaul Mackerras }
4166defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4176defa38bSPaul Mackerras 
418f2783c15SPaul Mackerras 
419f2783c15SPaul Mackerras /*
420f2783c15SPaul Mackerras  * There are two copies of tb_to_xs and stamp_xsec so that no
421f2783c15SPaul Mackerras  * lock is needed to access and use these values in
422f2783c15SPaul Mackerras  * do_gettimeofday.  We alternate the copies and as long as a
423f2783c15SPaul Mackerras  * reasonable time elapses between changes, there will never
424f2783c15SPaul Mackerras  * be inconsistent values.  ntpd has a minimum of one minute
425f2783c15SPaul Mackerras  * between updates.
426f2783c15SPaul Mackerras  */
427f2783c15SPaul Mackerras static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
4285d14a18dSPaul Mackerras 			       u64 new_tb_to_xs)
429f2783c15SPaul Mackerras {
430f2783c15SPaul Mackerras 	unsigned temp_idx;
431f2783c15SPaul Mackerras 	struct gettimeofday_vars *temp_varp;
432f2783c15SPaul Mackerras 
433f2783c15SPaul Mackerras 	temp_idx = (do_gtod.var_idx == 0);
434f2783c15SPaul Mackerras 	temp_varp = &do_gtod.vars[temp_idx];
435f2783c15SPaul Mackerras 
436f2783c15SPaul Mackerras 	temp_varp->tb_to_xs = new_tb_to_xs;
437f2783c15SPaul Mackerras 	temp_varp->tb_orig_stamp = new_tb_stamp;
438f2783c15SPaul Mackerras 	temp_varp->stamp_xsec = new_stamp_xsec;
439f2783c15SPaul Mackerras 	smp_mb();
440f2783c15SPaul Mackerras 	do_gtod.varp = temp_varp;
441f2783c15SPaul Mackerras 	do_gtod.var_idx = temp_idx;
442f2783c15SPaul Mackerras 
443f2783c15SPaul Mackerras 	/*
444f2783c15SPaul Mackerras 	 * tb_update_count is used to allow the userspace gettimeofday code
445f2783c15SPaul Mackerras 	 * to assure itself that it sees a consistent view of the tb_to_xs and
446f2783c15SPaul Mackerras 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
447f2783c15SPaul Mackerras 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
448f2783c15SPaul Mackerras 	 * the two values of tb_update_count match and are even then the
449f2783c15SPaul Mackerras 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
450f2783c15SPaul Mackerras 	 * loops back and reads them again until this criteria is met.
4510a45d449SPaul Mackerras 	 * We expect the caller to have done the first increment of
4520a45d449SPaul Mackerras 	 * vdso_data->tb_update_count already.
453f2783c15SPaul Mackerras 	 */
454a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_orig_stamp = new_tb_stamp;
455a7f290daSBenjamin Herrenschmidt 	vdso_data->stamp_xsec = new_stamp_xsec;
456a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_to_xs = new_tb_to_xs;
457a7f290daSBenjamin Herrenschmidt 	vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec;
458a7f290daSBenjamin Herrenschmidt 	vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec;
459f2783c15SPaul Mackerras 	smp_wmb();
460a7f290daSBenjamin Herrenschmidt 	++(vdso_data->tb_update_count);
461f2783c15SPaul Mackerras }
462f2783c15SPaul Mackerras 
463f2783c15SPaul Mackerras #ifdef CONFIG_SMP
464f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
465f2783c15SPaul Mackerras {
466f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
467f2783c15SPaul Mackerras 
468f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
469f2783c15SPaul Mackerras 		return regs->link;
470f2783c15SPaul Mackerras 
471f2783c15SPaul Mackerras 	return pc;
472f2783c15SPaul Mackerras }
473f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
474f2783c15SPaul Mackerras #endif
475f2783c15SPaul Mackerras 
476f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
477f2783c15SPaul Mackerras 
478f2783c15SPaul Mackerras /*
479f2783c15SPaul Mackerras  * This function recalibrates the timebase based on the 49-bit time-of-day
480f2783c15SPaul Mackerras  * value in the Titan chip.  The Titan is much more accurate than the value
481f2783c15SPaul Mackerras  * returned by the service processor for the timebase frequency.
482f2783c15SPaul Mackerras  */
483f2783c15SPaul Mackerras 
48471712b45STony Breeds static int __init iSeries_tb_recal(void)
485f2783c15SPaul Mackerras {
486f2783c15SPaul Mackerras 	struct div_result divres;
487f2783c15SPaul Mackerras 	unsigned long titan, tb;
48871712b45STony Breeds 
48971712b45STony Breeds 	/* Make sure we only run on iSeries */
49071712b45STony Breeds 	if (!firmware_has_feature(FW_FEATURE_ISERIES))
49171712b45STony Breeds 		return -ENODEV;
49271712b45STony Breeds 
493f2783c15SPaul Mackerras 	tb = get_tb();
494f2783c15SPaul Mackerras 	titan = HvCallXm_loadTod();
495f2783c15SPaul Mackerras 	if ( iSeries_recal_titan ) {
496f2783c15SPaul Mackerras 		unsigned long tb_ticks = tb - iSeries_recal_tb;
497f2783c15SPaul Mackerras 		unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
498f2783c15SPaul Mackerras 		unsigned long new_tb_ticks_per_sec   = (tb_ticks * USEC_PER_SEC)/titan_usec;
499f2783c15SPaul Mackerras 		unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
500f2783c15SPaul Mackerras 		long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
501f2783c15SPaul Mackerras 		char sign = '+';
502f2783c15SPaul Mackerras 		/* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
503f2783c15SPaul Mackerras 		new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
504f2783c15SPaul Mackerras 
505f2783c15SPaul Mackerras 		if ( tick_diff < 0 ) {
506f2783c15SPaul Mackerras 			tick_diff = -tick_diff;
507f2783c15SPaul Mackerras 			sign = '-';
508f2783c15SPaul Mackerras 		}
509f2783c15SPaul Mackerras 		if ( tick_diff ) {
510f2783c15SPaul Mackerras 			if ( tick_diff < tb_ticks_per_jiffy/25 ) {
511f2783c15SPaul Mackerras 				printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
512f2783c15SPaul Mackerras 						new_tb_ticks_per_jiffy, sign, tick_diff );
513f2783c15SPaul Mackerras 				tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
514f2783c15SPaul Mackerras 				tb_ticks_per_sec   = new_tb_ticks_per_sec;
515c6622f63SPaul Mackerras 				calc_cputime_factors();
516f2783c15SPaul Mackerras 				div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
517f2783c15SPaul Mackerras 				do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
518f2783c15SPaul Mackerras 				tb_to_xs = divres.result_low;
519f2783c15SPaul Mackerras 				do_gtod.varp->tb_to_xs = tb_to_xs;
520a7f290daSBenjamin Herrenschmidt 				vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
521a7f290daSBenjamin Herrenschmidt 				vdso_data->tb_to_xs = tb_to_xs;
522f2783c15SPaul Mackerras 			}
523f2783c15SPaul Mackerras 			else {
524f2783c15SPaul Mackerras 				printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
525f2783c15SPaul Mackerras 					"                   new tb_ticks_per_jiffy = %lu\n"
526f2783c15SPaul Mackerras 					"                   old tb_ticks_per_jiffy = %lu\n",
527f2783c15SPaul Mackerras 					new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
528f2783c15SPaul Mackerras 			}
529f2783c15SPaul Mackerras 		}
530f2783c15SPaul Mackerras 	}
531f2783c15SPaul Mackerras 	iSeries_recal_titan = titan;
532f2783c15SPaul Mackerras 	iSeries_recal_tb = tb;
53371712b45STony Breeds 
5344a4cfe38STony Breeds 	/* Called here as now we know accurate values for the timebase */
5354a4cfe38STony Breeds 	clocksource_init();
53671712b45STony Breeds 	return 0;
537f2783c15SPaul Mackerras }
53871712b45STony Breeds late_initcall(iSeries_tb_recal);
53971712b45STony Breeds 
54071712b45STony Breeds /* Called from platform early init */
54171712b45STony Breeds void __init iSeries_time_init_early(void)
54271712b45STony Breeds {
54371712b45STony Breeds 	iSeries_recal_tb = get_tb();
54471712b45STony Breeds 	iSeries_recal_titan = HvCallXm_loadTod();
54571712b45STony Breeds }
54671712b45STony Breeds #endif /* CONFIG_PPC_ISERIES */
547f2783c15SPaul Mackerras 
548f2783c15SPaul Mackerras /*
549f2783c15SPaul Mackerras  * For iSeries shared processors, we have to let the hypervisor
550f2783c15SPaul Mackerras  * set the hardware decrementer.  We set a virtual decrementer
551f2783c15SPaul Mackerras  * in the lppaca and call the hypervisor if the virtual
552f2783c15SPaul Mackerras  * decrementer is less than the current value in the hardware
553f2783c15SPaul Mackerras  * decrementer. (almost always the new decrementer value will
554f2783c15SPaul Mackerras  * be greater than the current hardware decementer so the hypervisor
555f2783c15SPaul Mackerras  * call will not be needed)
556f2783c15SPaul Mackerras  */
557f2783c15SPaul Mackerras 
558f2783c15SPaul Mackerras /*
559f2783c15SPaul Mackerras  * timer_interrupt - gets called when the decrementer overflows,
560f2783c15SPaul Mackerras  * with interrupts disabled.
561f2783c15SPaul Mackerras  */
562f2783c15SPaul Mackerras void timer_interrupt(struct pt_regs * regs)
563f2783c15SPaul Mackerras {
5647d12e780SDavid Howells 	struct pt_regs *old_regs;
5656e6b44e8SMilton Miller 	struct decrementer_clock *decrementer =  &__get_cpu_var(decrementers);
5666e6b44e8SMilton Miller 	struct clock_event_device *evt = &decrementer->event;
567d968014bSPaul Mackerras 	u64 now;
568d831d0b8STony Breeds 
569d831d0b8STony Breeds 	/* Ensure a positive value is written to the decrementer, or else
570d831d0b8STony Breeds 	 * some CPUs will continuue to take decrementer exceptions */
571d831d0b8STony Breeds 	set_dec(DECREMENTER_MAX);
572f2783c15SPaul Mackerras 
573f2783c15SPaul Mackerras #ifdef CONFIG_PPC32
574f2783c15SPaul Mackerras 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
575f2783c15SPaul Mackerras 		do_IRQ(regs);
576f2783c15SPaul Mackerras #endif
577f2783c15SPaul Mackerras 
578d968014bSPaul Mackerras 	now = get_tb_or_rtc();
5796e6b44e8SMilton Miller 	if (now < decrementer->next_tb) {
580d968014bSPaul Mackerras 		/* not time for this event yet */
5816e6b44e8SMilton Miller 		now = decrementer->next_tb - now;
582d968014bSPaul Mackerras 		if (now <= DECREMENTER_MAX)
58343875cc0SPaul Mackerras 			set_dec((int)now);
584d968014bSPaul Mackerras 		return;
585d968014bSPaul Mackerras 	}
5867d12e780SDavid Howells 	old_regs = set_irq_regs(regs);
587f2783c15SPaul Mackerras 	irq_enter();
588f2783c15SPaul Mackerras 
589c6622f63SPaul Mackerras 	calculate_steal_time();
590f2783c15SPaul Mackerras 
591f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
592501b6d29SStephen Rothwell 	if (firmware_has_feature(FW_FEATURE_ISERIES))
5933356bb9fSDavid Gibson 		get_lppaca()->int_dword.fields.decr_int = 0;
594f2783c15SPaul Mackerras #endif
595f2783c15SPaul Mackerras 
596d831d0b8STony Breeds 	if (evt->event_handler)
597d831d0b8STony Breeds 		evt->event_handler(evt);
598f2783c15SPaul Mackerras 
599f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
600501b6d29SStephen Rothwell 	if (firmware_has_feature(FW_FEATURE_ISERIES) && hvlpevent_is_pending())
60135a84c2fSOlaf Hering 		process_hvlpevents();
602f2783c15SPaul Mackerras #endif
603f2783c15SPaul Mackerras 
604f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
605f2783c15SPaul Mackerras 	/* collect purr register values often, for accurate calculations */
606f2783c15SPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
607f2783c15SPaul Mackerras 		struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
608f2783c15SPaul Mackerras 		cu->current_tb = mfspr(SPRN_PURR);
609f2783c15SPaul Mackerras 	}
610f2783c15SPaul Mackerras #endif
611f2783c15SPaul Mackerras 
612f2783c15SPaul Mackerras 	irq_exit();
6137d12e780SDavid Howells 	set_irq_regs(old_regs);
614f2783c15SPaul Mackerras }
615f2783c15SPaul Mackerras 
616f2783c15SPaul Mackerras void wakeup_decrementer(void)
617f2783c15SPaul Mackerras {
618092b8f34SPaul Mackerras 	unsigned long ticks;
619f2783c15SPaul Mackerras 
620f2783c15SPaul Mackerras 	/*
621092b8f34SPaul Mackerras 	 * The timebase gets saved on sleep and restored on wakeup,
622092b8f34SPaul Mackerras 	 * so all we need to do is to reset the decrementer.
623f2783c15SPaul Mackerras 	 */
624092b8f34SPaul Mackerras 	ticks = tb_ticks_since(__get_cpu_var(last_jiffy));
625092b8f34SPaul Mackerras 	if (ticks < tb_ticks_per_jiffy)
626092b8f34SPaul Mackerras 		ticks = tb_ticks_per_jiffy - ticks;
627092b8f34SPaul Mackerras 	else
628092b8f34SPaul Mackerras 		ticks = 1;
629092b8f34SPaul Mackerras 	set_dec(ticks);
630f2783c15SPaul Mackerras }
631f2783c15SPaul Mackerras 
6327ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
6337ac5dde9SScott Wood void generic_suspend_disable_irqs(void)
6347ac5dde9SScott Wood {
6357ac5dde9SScott Wood 	preempt_disable();
6367ac5dde9SScott Wood 
6377ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
6387ac5dde9SScott Wood 	 * with suspending.
6397ac5dde9SScott Wood 	 */
6407ac5dde9SScott Wood 
6417ac5dde9SScott Wood 	set_dec(0x7fffffff);
6427ac5dde9SScott Wood 	local_irq_disable();
6437ac5dde9SScott Wood 	set_dec(0x7fffffff);
6447ac5dde9SScott Wood }
6457ac5dde9SScott Wood 
6467ac5dde9SScott Wood void generic_suspend_enable_irqs(void)
6477ac5dde9SScott Wood {
6487ac5dde9SScott Wood 	wakeup_decrementer();
6497ac5dde9SScott Wood 
6507ac5dde9SScott Wood 	local_irq_enable();
6517ac5dde9SScott Wood 	preempt_enable();
6527ac5dde9SScott Wood }
6537ac5dde9SScott Wood 
6547ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6557ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
6567ac5dde9SScott Wood {
6577ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
6587ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
6597ac5dde9SScott Wood 	generic_suspend_disable_irqs();
6607ac5dde9SScott Wood }
6617ac5dde9SScott Wood 
6627ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
6637ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
6647ac5dde9SScott Wood {
6657ac5dde9SScott Wood 	generic_suspend_enable_irqs();
6667ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
6677ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
6687ac5dde9SScott Wood }
6697ac5dde9SScott Wood #endif
6707ac5dde9SScott Wood 
671a5b518edSPaul Mackerras #ifdef CONFIG_SMP
672f2783c15SPaul Mackerras void __init smp_space_timers(unsigned int max_cpus)
673f2783c15SPaul Mackerras {
674f2783c15SPaul Mackerras 	int i;
675eb36c288SPaul Mackerras 	u64 previous_tb = per_cpu(last_jiffy, boot_cpuid);
676f2783c15SPaul Mackerras 
677cbe62e2bSPaul Mackerras 	/* make sure tb > per_cpu(last_jiffy, cpu) for all cpus always */
678cbe62e2bSPaul Mackerras 	previous_tb -= tb_ticks_per_jiffy;
679e147ec8fSwill schmidt 
6800e551954SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
681c6622f63SPaul Mackerras 		if (i == boot_cpuid)
682c6622f63SPaul Mackerras 			continue;
683f2783c15SPaul Mackerras 		per_cpu(last_jiffy, i) = previous_tb;
684f2783c15SPaul Mackerras 	}
685f2783c15SPaul Mackerras }
686f2783c15SPaul Mackerras #endif
687f2783c15SPaul Mackerras 
688f2783c15SPaul Mackerras /*
689f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
690f2783c15SPaul Mackerras  *
691f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
692f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
693f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
694f2783c15SPaul Mackerras  */
695f2783c15SPaul Mackerras unsigned long long sched_clock(void)
696f2783c15SPaul Mackerras {
69796c44507SPaul Mackerras 	if (__USE_RTC())
69896c44507SPaul Mackerras 		return get_rtc();
699fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
700f2783c15SPaul Mackerras }
701f2783c15SPaul Mackerras 
7020bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
703f2783c15SPaul Mackerras {
704f2783c15SPaul Mackerras 	struct device_node *cpu;
705a7f67bdfSJeremy Kerr 	const unsigned int *fp;
7060bb474a4SAnton Blanchard 	int found = 0;
707f2783c15SPaul Mackerras 
7080bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
709f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
710f2783c15SPaul Mackerras 
711d8a8188dSOlaf Hering 	if (cpu) {
712e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
713d8a8188dSOlaf Hering 		if (fp) {
7140bb474a4SAnton Blanchard 			found = 1;
715a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
716f2783c15SPaul Mackerras 		}
7170bb474a4SAnton Blanchard 
7180bb474a4SAnton Blanchard 		of_node_put(cpu);
719f2783c15SPaul Mackerras 	}
7200bb474a4SAnton Blanchard 
7210bb474a4SAnton Blanchard 	return found;
7220bb474a4SAnton Blanchard }
7230bb474a4SAnton Blanchard 
7240bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
7250bb474a4SAnton Blanchard {
7260bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
7270bb474a4SAnton Blanchard 
7280bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
7290bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
7300bb474a4SAnton Blanchard 
731f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
732f2783c15SPaul Mackerras 				"(not found)\n");
7330bb474a4SAnton Blanchard 	}
734f2783c15SPaul Mackerras 
7350bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
7360bb474a4SAnton Blanchard 
7370bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
7380bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
7390bb474a4SAnton Blanchard 
7400bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
7410bb474a4SAnton Blanchard 				"(not found)\n");
742f2783c15SPaul Mackerras 	}
7430bb474a4SAnton Blanchard 
744aab69292SJosh Boyer #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
7450fd6f717SKumar Gala 	/* Set the time base to zero */
7460fd6f717SKumar Gala 	mtspr(SPRN_TBWL, 0);
7470fd6f717SKumar Gala 	mtspr(SPRN_TBWU, 0);
7480fd6f717SKumar Gala 
7490fd6f717SKumar Gala 	/* Clear any pending timer interrupts */
7500fd6f717SKumar Gala 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
7510fd6f717SKumar Gala 
7520fd6f717SKumar Gala 	/* Enable decrementer interrupt */
7530fd6f717SKumar Gala 	mtspr(SPRN_TCR, TCR_DIE);
7540fd6f717SKumar Gala #endif
755f2783c15SPaul Mackerras }
756f2783c15SPaul Mackerras 
757aa3be5f3STony Breeds int update_persistent_clock(struct timespec now)
758f2783c15SPaul Mackerras {
759f2783c15SPaul Mackerras 	struct rtc_time tm;
760f2783c15SPaul Mackerras 
761aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
762aa3be5f3STony Breeds 		return 0;
763aa3be5f3STony Breeds 
764aa3be5f3STony Breeds 	to_tm(now.tv_sec + 1 + timezone_offset, &tm);
765aa3be5f3STony Breeds 	tm.tm_year -= 1900;
766aa3be5f3STony Breeds 	tm.tm_mon -= 1;
767aa3be5f3STony Breeds 
768aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
769aa3be5f3STony Breeds }
770aa3be5f3STony Breeds 
771aa3be5f3STony Breeds unsigned long read_persistent_clock(void)
772aa3be5f3STony Breeds {
773aa3be5f3STony Breeds 	struct rtc_time tm;
774aa3be5f3STony Breeds 	static int first = 1;
775aa3be5f3STony Breeds 
776aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
777aa3be5f3STony Breeds 	if (first) {
778aa3be5f3STony Breeds 		first = 0;
779aa3be5f3STony Breeds 		if (ppc_md.time_init)
780aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
781aa3be5f3STony Breeds 
782aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
783f2783c15SPaul Mackerras 		if (ppc_md.get_boot_time)
784aa3be5f3STony Breeds 			return ppc_md.get_boot_time() -timezone_offset;
785aa3be5f3STony Breeds 	}
786f2783c15SPaul Mackerras 	if (!ppc_md.get_rtc_time)
787f2783c15SPaul Mackerras 		return 0;
788f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
789f2783c15SPaul Mackerras 	return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
790f2783c15SPaul Mackerras 		      tm.tm_hour, tm.tm_min, tm.tm_sec);
791f2783c15SPaul Mackerras }
792f2783c15SPaul Mackerras 
7934a4cfe38STony Breeds /* clocksource code */
7944a4cfe38STony Breeds static cycle_t rtc_read(void)
7954a4cfe38STony Breeds {
7964a4cfe38STony Breeds 	return (cycle_t)get_rtc();
7974a4cfe38STony Breeds }
7984a4cfe38STony Breeds 
7994a4cfe38STony Breeds static cycle_t timebase_read(void)
8004a4cfe38STony Breeds {
8014a4cfe38STony Breeds 	return (cycle_t)get_tb();
8024a4cfe38STony Breeds }
8034a4cfe38STony Breeds 
8044a4cfe38STony Breeds void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
8054a4cfe38STony Breeds {
8064a4cfe38STony Breeds 	u64 t2x, stamp_xsec;
8074a4cfe38STony Breeds 
8084a4cfe38STony Breeds 	if (clock != &clocksource_timebase)
8094a4cfe38STony Breeds 		return;
8104a4cfe38STony Breeds 
8114a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
8124a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8134a4cfe38STony Breeds 	smp_mb();
8144a4cfe38STony Breeds 
8154a4cfe38STony Breeds 	/* XXX this assumes clock->shift == 22 */
8164a4cfe38STony Breeds 	/* 4611686018 ~= 2^(20+64-22) / 1e9 */
8174a4cfe38STony Breeds 	t2x = (u64) clock->mult * 4611686018ULL;
8184a4cfe38STony Breeds 	stamp_xsec = (u64) xtime.tv_nsec * XSEC_PER_SEC;
8194a4cfe38STony Breeds 	do_div(stamp_xsec, 1000000000);
8204a4cfe38STony Breeds 	stamp_xsec += (u64) xtime.tv_sec * XSEC_PER_SEC;
8214a4cfe38STony Breeds 	update_gtod(clock->cycle_last, stamp_xsec, t2x);
8224a4cfe38STony Breeds }
8234a4cfe38STony Breeds 
8244a4cfe38STony Breeds void update_vsyscall_tz(void)
8254a4cfe38STony Breeds {
8264a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
8274a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8284a4cfe38STony Breeds 	smp_mb();
8294a4cfe38STony Breeds 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
8304a4cfe38STony Breeds 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
8314a4cfe38STony Breeds 	smp_mb();
8324a4cfe38STony Breeds 	++vdso_data->tb_update_count;
8334a4cfe38STony Breeds }
8344a4cfe38STony Breeds 
8354a4cfe38STony Breeds void __init clocksource_init(void)
8364a4cfe38STony Breeds {
8374a4cfe38STony Breeds 	struct clocksource *clock;
8384a4cfe38STony Breeds 
8394a4cfe38STony Breeds 	if (__USE_RTC())
8404a4cfe38STony Breeds 		clock = &clocksource_rtc;
8414a4cfe38STony Breeds 	else
8424a4cfe38STony Breeds 		clock = &clocksource_timebase;
8434a4cfe38STony Breeds 
8444a4cfe38STony Breeds 	clock->mult = clocksource_hz2mult(tb_ticks_per_sec, clock->shift);
8454a4cfe38STony Breeds 
8464a4cfe38STony Breeds 	if (clocksource_register(clock)) {
8474a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
8484a4cfe38STony Breeds 		       clock->name);
8494a4cfe38STony Breeds 		return;
8504a4cfe38STony Breeds 	}
8514a4cfe38STony Breeds 
8524a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
8534a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
8544a4cfe38STony Breeds }
8554a4cfe38STony Breeds 
856d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
857d831d0b8STony Breeds 				      struct clock_event_device *dev)
858d831d0b8STony Breeds {
8596e6b44e8SMilton Miller 	__get_cpu_var(decrementers).next_tb = get_tb_or_rtc() + evt;
860d831d0b8STony Breeds 	set_dec(evt);
861d831d0b8STony Breeds 	return 0;
862d831d0b8STony Breeds }
863d831d0b8STony Breeds 
864d831d0b8STony Breeds static void decrementer_set_mode(enum clock_event_mode mode,
865d831d0b8STony Breeds 				 struct clock_event_device *dev)
866d831d0b8STony Breeds {
867d831d0b8STony Breeds 	if (mode != CLOCK_EVT_MODE_ONESHOT)
868d831d0b8STony Breeds 		decrementer_set_next_event(DECREMENTER_MAX, dev);
869d831d0b8STony Breeds }
870d831d0b8STony Breeds 
871d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
872d831d0b8STony Breeds {
8736e6b44e8SMilton Miller 	struct clock_event_device *dec = &per_cpu(decrementers, cpu).event;
874d831d0b8STony Breeds 
875d831d0b8STony Breeds 	*dec = decrementer_clockevent;
876d831d0b8STony Breeds 	dec->cpumask = cpumask_of_cpu(cpu);
877d831d0b8STony Breeds 
8780302f12eSTony Breeds 	printk(KERN_DEBUG "clockevent: %s mult[%lx] shift[%d] cpu[%d]\n",
879d831d0b8STony Breeds 	       dec->name, dec->mult, dec->shift, cpu);
880d831d0b8STony Breeds 
881d831d0b8STony Breeds 	clockevents_register_device(dec);
882d831d0b8STony Breeds }
883d831d0b8STony Breeds 
884c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
885d831d0b8STony Breeds {
886d831d0b8STony Breeds 	int cpu = smp_processor_id();
887d831d0b8STony Breeds 
888d831d0b8STony Breeds 	decrementer_clockevent.mult = div_sc(ppc_tb_freq, NSEC_PER_SEC,
889d831d0b8STony Breeds 					     decrementer_clockevent.shift);
890d831d0b8STony Breeds 	decrementer_clockevent.max_delta_ns =
891d831d0b8STony Breeds 		clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent);
89243875cc0SPaul Mackerras 	decrementer_clockevent.min_delta_ns =
89343875cc0SPaul Mackerras 		clockevent_delta2ns(2, &decrementer_clockevent);
894d831d0b8STony Breeds 
895d831d0b8STony Breeds 	register_decrementer_clockevent(cpu);
896d831d0b8STony Breeds }
897d831d0b8STony Breeds 
898d831d0b8STony Breeds void secondary_cpu_time_init(void)
899d831d0b8STony Breeds {
900d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
901d831d0b8STony Breeds 	 * call here ! */
902d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
903d831d0b8STony Breeds }
904d831d0b8STony Breeds 
905f2783c15SPaul Mackerras /* This function is only called on the boot processor */
906f2783c15SPaul Mackerras void __init time_init(void)
907f2783c15SPaul Mackerras {
908f2783c15SPaul Mackerras 	unsigned long flags;
909f2783c15SPaul Mackerras 	struct div_result res;
910092b8f34SPaul Mackerras 	u64 scale, x;
911f2783c15SPaul Mackerras 	unsigned shift;
912f2783c15SPaul Mackerras 
91396c44507SPaul Mackerras 	if (__USE_RTC()) {
91496c44507SPaul Mackerras 		/* 601 processor: dec counts down by 128 every 128ns */
91596c44507SPaul Mackerras 		ppc_tb_freq = 1000000000;
916eb36c288SPaul Mackerras 		tb_last_jiffy = get_rtcl();
91796c44507SPaul Mackerras 	} else {
91896c44507SPaul Mackerras 		/* Normal PowerPC with timebase register */
919f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
920224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
921374e99d4SPaul Mackerras 		       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
922224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
923374e99d4SPaul Mackerras 		       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
924eb36c288SPaul Mackerras 		tb_last_jiffy = get_tb();
92596c44507SPaul Mackerras 	}
926374e99d4SPaul Mackerras 
927374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
928092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
929374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
930374e99d4SPaul Mackerras 	tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
931c6622f63SPaul Mackerras 	calc_cputime_factors();
932092b8f34SPaul Mackerras 
933092b8f34SPaul Mackerras 	/*
934092b8f34SPaul Mackerras 	 * Calculate the length of each tick in ns.  It will not be
935092b8f34SPaul Mackerras 	 * exactly 1e9/HZ unless ppc_tb_freq is divisible by HZ.
936092b8f34SPaul Mackerras 	 * We compute 1e9 * tb_ticks_per_jiffy / ppc_tb_freq,
937092b8f34SPaul Mackerras 	 * rounded up.
938092b8f34SPaul Mackerras 	 */
939092b8f34SPaul Mackerras 	x = (u64) NSEC_PER_SEC * tb_ticks_per_jiffy + ppc_tb_freq - 1;
940092b8f34SPaul Mackerras 	do_div(x, ppc_tb_freq);
941092b8f34SPaul Mackerras 	tick_nsec = x;
942092b8f34SPaul Mackerras 	last_tick_len = x << TICKLEN_SCALE;
943092b8f34SPaul Mackerras 
944092b8f34SPaul Mackerras 	/*
945092b8f34SPaul Mackerras 	 * Compute ticklen_to_xs, which is a factor which gets multiplied
946092b8f34SPaul Mackerras 	 * by (last_tick_len << TICKLEN_SHIFT) to get a tb_to_xs value.
947092b8f34SPaul Mackerras 	 * It is computed as:
948092b8f34SPaul Mackerras 	 * ticklen_to_xs = 2^N / (tb_ticks_per_jiffy * 1e9)
949092b8f34SPaul Mackerras 	 * where N = 64 + 20 - TICKLEN_SCALE - TICKLEN_SHIFT
9500a45d449SPaul Mackerras 	 * which turns out to be N = 51 - SHIFT_HZ.
9510a45d449SPaul Mackerras 	 * This gives the result as a 0.64 fixed-point fraction.
9520a45d449SPaul Mackerras 	 * That value is reduced by an offset amounting to 1 xsec per
9530a45d449SPaul Mackerras 	 * 2^31 timebase ticks to avoid problems with time going backwards
9540a45d449SPaul Mackerras 	 * by 1 xsec when we do timer_recalc_offset due to losing the
9550a45d449SPaul Mackerras 	 * fractional xsec.  That offset is equal to ppc_tb_freq/2^51
9560a45d449SPaul Mackerras 	 * since there are 2^20 xsec in a second.
957092b8f34SPaul Mackerras 	 */
9580a45d449SPaul Mackerras 	div128_by_32((1ULL << 51) - ppc_tb_freq, 0,
9590a45d449SPaul Mackerras 		     tb_ticks_per_jiffy << SHIFT_HZ, &res);
960092b8f34SPaul Mackerras 	div128_by_32(res.result_high, res.result_low, NSEC_PER_SEC, &res);
961092b8f34SPaul Mackerras 	ticklen_to_xs = res.result_low;
962092b8f34SPaul Mackerras 
963092b8f34SPaul Mackerras 	/* Compute tb_to_xs from tick_nsec */
964092b8f34SPaul Mackerras 	tb_to_xs = mulhdu(last_tick_len << TICKLEN_SHIFT, ticklen_to_xs);
965374e99d4SPaul Mackerras 
966f2783c15SPaul Mackerras 	/*
967f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
968f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
969f2783c15SPaul Mackerras 	 * which is the timebase frequency.
970f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
971f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
972f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
973f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
974f2783c15SPaul Mackerras 	 * sched_clock().
975f2783c15SPaul Mackerras 	 */
976f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
977f2783c15SPaul Mackerras 	scale = res.result_low;
978f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
979f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
980f2783c15SPaul Mackerras 		res.result_high >>= 1;
981f2783c15SPaul Mackerras 	}
982f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
983f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
984fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
985c27da339SBenjamin Herrenschmidt 	boot_tb = get_tb_or_rtc();
986f2783c15SPaul Mackerras 
987f2783c15SPaul Mackerras 	write_seqlock_irqsave(&xtime_lock, flags);
988092b8f34SPaul Mackerras 
989092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
990092b8f34SPaul Mackerras         if (timezone_offset) {
991092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
992092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
993092b8f34SPaul Mackerras         }
994092b8f34SPaul Mackerras 
995f2783c15SPaul Mackerras 	do_gtod.varp = &do_gtod.vars[0];
996f2783c15SPaul Mackerras 	do_gtod.var_idx = 0;
99796c44507SPaul Mackerras 	do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
998eb36c288SPaul Mackerras 	__get_cpu_var(last_jiffy) = tb_last_jiffy;
999f2783c15SPaul Mackerras 	do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
1000f2783c15SPaul Mackerras 	do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
1001f2783c15SPaul Mackerras 	do_gtod.varp->tb_to_xs = tb_to_xs;
1002f2783c15SPaul Mackerras 	do_gtod.tb_to_us = tb_to_us;
1003a7f290daSBenjamin Herrenschmidt 
1004a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_orig_stamp = tb_last_jiffy;
1005a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
1006a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
1007092b8f34SPaul Mackerras 	vdso_data->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
1008a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_to_xs = tb_to_xs;
1009f2783c15SPaul Mackerras 
1010f2783c15SPaul Mackerras 	time_freq = 0;
1011f2783c15SPaul Mackerras 
1012f2783c15SPaul Mackerras 	write_sequnlock_irqrestore(&xtime_lock, flags);
1013f2783c15SPaul Mackerras 
10144a4cfe38STony Breeds 	/* Register the clocksource, if we're not running on iSeries */
10154a4cfe38STony Breeds 	if (!firmware_has_feature(FW_FEATURE_ISERIES))
10164a4cfe38STony Breeds 		clocksource_init();
10174a4cfe38STony Breeds 
1018d831d0b8STony Breeds 	init_decrementer_clockevent();
1019f2783c15SPaul Mackerras }
1020f2783c15SPaul Mackerras 
1021f2783c15SPaul Mackerras 
1022f2783c15SPaul Mackerras #define FEBRUARY	2
1023f2783c15SPaul Mackerras #define	STARTOFTIME	1970
1024f2783c15SPaul Mackerras #define SECDAY		86400L
1025f2783c15SPaul Mackerras #define SECYR		(SECDAY * 365)
1026f2783c15SPaul Mackerras #define	leapyear(year)		((year) % 4 == 0 && \
1027f2783c15SPaul Mackerras 				 ((year) % 100 != 0 || (year) % 400 == 0))
1028f2783c15SPaul Mackerras #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
1029f2783c15SPaul Mackerras #define	days_in_month(a) 	(month_days[(a) - 1])
1030f2783c15SPaul Mackerras 
1031f2783c15SPaul Mackerras static int month_days[12] = {
1032f2783c15SPaul Mackerras 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1033f2783c15SPaul Mackerras };
1034f2783c15SPaul Mackerras 
1035f2783c15SPaul Mackerras /*
1036f2783c15SPaul Mackerras  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1037f2783c15SPaul Mackerras  */
1038f2783c15SPaul Mackerras void GregorianDay(struct rtc_time * tm)
1039f2783c15SPaul Mackerras {
1040f2783c15SPaul Mackerras 	int leapsToDate;
1041f2783c15SPaul Mackerras 	int lastYear;
1042f2783c15SPaul Mackerras 	int day;
1043f2783c15SPaul Mackerras 	int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1044f2783c15SPaul Mackerras 
1045f2783c15SPaul Mackerras 	lastYear = tm->tm_year - 1;
1046f2783c15SPaul Mackerras 
1047f2783c15SPaul Mackerras 	/*
1048f2783c15SPaul Mackerras 	 * Number of leap corrections to apply up to end of last year
1049f2783c15SPaul Mackerras 	 */
1050f2783c15SPaul Mackerras 	leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1051f2783c15SPaul Mackerras 
1052f2783c15SPaul Mackerras 	/*
1053f2783c15SPaul Mackerras 	 * This year is a leap year if it is divisible by 4 except when it is
1054f2783c15SPaul Mackerras 	 * divisible by 100 unless it is divisible by 400
1055f2783c15SPaul Mackerras 	 *
1056f2783c15SPaul Mackerras 	 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1057f2783c15SPaul Mackerras 	 */
1058f2783c15SPaul Mackerras 	day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1059f2783c15SPaul Mackerras 
1060f2783c15SPaul Mackerras 	day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1061f2783c15SPaul Mackerras 		   tm->tm_mday;
1062f2783c15SPaul Mackerras 
1063f2783c15SPaul Mackerras 	tm->tm_wday = day % 7;
1064f2783c15SPaul Mackerras }
1065f2783c15SPaul Mackerras 
1066f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm)
1067f2783c15SPaul Mackerras {
1068f2783c15SPaul Mackerras 	register int    i;
1069f2783c15SPaul Mackerras 	register long   hms, day;
1070f2783c15SPaul Mackerras 
1071f2783c15SPaul Mackerras 	day = tim / SECDAY;
1072f2783c15SPaul Mackerras 	hms = tim % SECDAY;
1073f2783c15SPaul Mackerras 
1074f2783c15SPaul Mackerras 	/* Hours, minutes, seconds are easy */
1075f2783c15SPaul Mackerras 	tm->tm_hour = hms / 3600;
1076f2783c15SPaul Mackerras 	tm->tm_min = (hms % 3600) / 60;
1077f2783c15SPaul Mackerras 	tm->tm_sec = (hms % 3600) % 60;
1078f2783c15SPaul Mackerras 
1079f2783c15SPaul Mackerras 	/* Number of years in days */
1080f2783c15SPaul Mackerras 	for (i = STARTOFTIME; day >= days_in_year(i); i++)
1081f2783c15SPaul Mackerras 		day -= days_in_year(i);
1082f2783c15SPaul Mackerras 	tm->tm_year = i;
1083f2783c15SPaul Mackerras 
1084f2783c15SPaul Mackerras 	/* Number of months in days left */
1085f2783c15SPaul Mackerras 	if (leapyear(tm->tm_year))
1086f2783c15SPaul Mackerras 		days_in_month(FEBRUARY) = 29;
1087f2783c15SPaul Mackerras 	for (i = 1; day >= days_in_month(i); i++)
1088f2783c15SPaul Mackerras 		day -= days_in_month(i);
1089f2783c15SPaul Mackerras 	days_in_month(FEBRUARY) = 28;
1090f2783c15SPaul Mackerras 	tm->tm_mon = i;
1091f2783c15SPaul Mackerras 
1092f2783c15SPaul Mackerras 	/* Days are what is left over (+1) from all that. */
1093f2783c15SPaul Mackerras 	tm->tm_mday = day + 1;
1094f2783c15SPaul Mackerras 
1095f2783c15SPaul Mackerras 	/*
1096f2783c15SPaul Mackerras 	 * Determine the day of week
1097f2783c15SPaul Mackerras 	 */
1098f2783c15SPaul Mackerras 	GregorianDay(tm);
1099f2783c15SPaul Mackerras }
1100f2783c15SPaul Mackerras 
1101f2783c15SPaul Mackerras /* Auxiliary function to compute scaling factors */
1102f2783c15SPaul Mackerras /* Actually the choice of a timebase running at 1/4 the of the bus
1103f2783c15SPaul Mackerras  * frequency giving resolution of a few tens of nanoseconds is quite nice.
1104f2783c15SPaul Mackerras  * It makes this computation very precise (27-28 bits typically) which
1105f2783c15SPaul Mackerras  * is optimistic considering the stability of most processor clock
1106f2783c15SPaul Mackerras  * oscillators and the precision with which the timebase frequency
1107f2783c15SPaul Mackerras  * is measured but does not harm.
1108f2783c15SPaul Mackerras  */
1109f2783c15SPaul Mackerras unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
1110f2783c15SPaul Mackerras {
1111f2783c15SPaul Mackerras         unsigned mlt=0, tmp, err;
1112f2783c15SPaul Mackerras         /* No concern for performance, it's done once: use a stupid
1113f2783c15SPaul Mackerras          * but safe and compact method to find the multiplier.
1114f2783c15SPaul Mackerras          */
1115f2783c15SPaul Mackerras 
1116f2783c15SPaul Mackerras         for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
1117f2783c15SPaul Mackerras                 if (mulhwu(inscale, mlt|tmp) < outscale)
1118f2783c15SPaul Mackerras 			mlt |= tmp;
1119f2783c15SPaul Mackerras         }
1120f2783c15SPaul Mackerras 
1121f2783c15SPaul Mackerras         /* We might still be off by 1 for the best approximation.
1122f2783c15SPaul Mackerras          * A side effect of this is that if outscale is too large
1123f2783c15SPaul Mackerras          * the returned value will be zero.
1124f2783c15SPaul Mackerras          * Many corner cases have been checked and seem to work,
1125f2783c15SPaul Mackerras          * some might have been forgotten in the test however.
1126f2783c15SPaul Mackerras          */
1127f2783c15SPaul Mackerras 
1128f2783c15SPaul Mackerras         err = inscale * (mlt+1);
1129f2783c15SPaul Mackerras         if (err <= inscale/2)
1130f2783c15SPaul Mackerras 		mlt++;
1131f2783c15SPaul Mackerras         return mlt;
1132f2783c15SPaul Mackerras }
1133f2783c15SPaul Mackerras 
1134f2783c15SPaul Mackerras /*
1135f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1136f2783c15SPaul Mackerras  * result.
1137f2783c15SPaul Mackerras  */
1138f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1139f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1140f2783c15SPaul Mackerras {
1141f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1142f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1143f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1144f2783c15SPaul Mackerras 
1145f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1146f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1147f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1148f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1149f2783c15SPaul Mackerras 
1150f2783c15SPaul Mackerras 	w = a / divisor;
1151f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1152f2783c15SPaul Mackerras 
1153f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1154f2783c15SPaul Mackerras 	x = ra;
1155f2783c15SPaul Mackerras 
1156f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1157f2783c15SPaul Mackerras 	y = rb;
1158f2783c15SPaul Mackerras 
1159f2783c15SPaul Mackerras 	do_div(rc, divisor);
1160f2783c15SPaul Mackerras 	z = rc;
1161f2783c15SPaul Mackerras 
1162f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1163f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1164f2783c15SPaul Mackerras 
1165f2783c15SPaul Mackerras }
1166