xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision 092b8f3488a3e50a4ab5f2f3f7c8bbf56b3144e1)
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/config.h>
36f2783c15SPaul Mackerras #include <linux/errno.h>
37f2783c15SPaul Mackerras #include <linux/module.h>
38f2783c15SPaul Mackerras #include <linux/sched.h>
39f2783c15SPaul Mackerras #include <linux/kernel.h>
40f2783c15SPaul Mackerras #include <linux/param.h>
41f2783c15SPaul Mackerras #include <linux/string.h>
42f2783c15SPaul Mackerras #include <linux/mm.h>
43f2783c15SPaul Mackerras #include <linux/interrupt.h>
44f2783c15SPaul Mackerras #include <linux/timex.h>
45f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
46f2783c15SPaul Mackerras #include <linux/time.h>
47f2783c15SPaul Mackerras #include <linux/init.h>
48f2783c15SPaul Mackerras #include <linux/profile.h>
49f2783c15SPaul Mackerras #include <linux/cpu.h>
50f2783c15SPaul Mackerras #include <linux/security.h>
51f2783c15SPaul Mackerras #include <linux/percpu.h>
52f2783c15SPaul Mackerras #include <linux/rtc.h>
53*092b8f34SPaul Mackerras #include <linux/jiffies.h>
54f2783c15SPaul Mackerras 
55f2783c15SPaul Mackerras #include <asm/io.h>
56f2783c15SPaul Mackerras #include <asm/processor.h>
57f2783c15SPaul Mackerras #include <asm/nvram.h>
58f2783c15SPaul Mackerras #include <asm/cache.h>
59f2783c15SPaul Mackerras #include <asm/machdep.h>
60f2783c15SPaul Mackerras #include <asm/uaccess.h>
61f2783c15SPaul Mackerras #include <asm/time.h>
62f2783c15SPaul Mackerras #include <asm/prom.h>
63f2783c15SPaul Mackerras #include <asm/irq.h>
64f2783c15SPaul Mackerras #include <asm/div64.h>
652249ca9dSPaul Mackerras #include <asm/smp.h>
66a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
67f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
68f2783c15SPaul Mackerras #include <asm/firmware.h>
69f2783c15SPaul Mackerras #endif
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
74732ee21fSOlof Johansson #include <asm/smp.h>
75f2783c15SPaul Mackerras 
76f2783c15SPaul Mackerras /* keep track of when we need to update the rtc */
77f2783c15SPaul Mackerras time_t last_rtc_update;
78f2783c15SPaul Mackerras extern int piranha_simulator;
79f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
80f2783c15SPaul Mackerras unsigned long iSeries_recal_titan = 0;
81f2783c15SPaul Mackerras unsigned long iSeries_recal_tb = 0;
82f2783c15SPaul Mackerras static unsigned long first_settimeofday = 1;
83f2783c15SPaul Mackerras #endif
84f2783c15SPaul Mackerras 
85f2783c15SPaul Mackerras /* The decrementer counts down by 128 every 128ns on a 601. */
86f2783c15SPaul Mackerras #define DECREMENTER_COUNT_601	(1000000000 / HZ)
87f2783c15SPaul Mackerras 
88f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
89f2783c15SPaul Mackerras 
90f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
91f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
92f2783c15SPaul Mackerras #else
93f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
94f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
95f2783c15SPaul Mackerras #endif
96f2783c15SPaul Mackerras 
97f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
98f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
99f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
100f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
101f2783c15SPaul Mackerras u64 tb_to_xs;
102f2783c15SPaul Mackerras unsigned tb_to_us;
103*092b8f34SPaul Mackerras 
104*092b8f34SPaul Mackerras #define TICKLEN_SCALE	(SHIFT_SCALE - 10)
105*092b8f34SPaul Mackerras u64 last_tick_len;	/* units are ns / 2^TICKLEN_SCALE */
106*092b8f34SPaul Mackerras u64 ticklen_to_xs;	/* 0.64 fraction */
107*092b8f34SPaul Mackerras 
108*092b8f34SPaul Mackerras /* If last_tick_len corresponds to about 1/HZ seconds, then
109*092b8f34SPaul Mackerras    last_tick_len << TICKLEN_SHIFT will be about 2^63. */
110*092b8f34SPaul Mackerras #define TICKLEN_SHIFT	(63 - 30 - TICKLEN_SCALE + SHIFT_HZ)
111*092b8f34SPaul Mackerras 
112f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
113f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
114f2783c15SPaul Mackerras 
115f2783c15SPaul Mackerras u64 tb_to_ns_scale;
116f2783c15SPaul Mackerras unsigned tb_to_ns_shift;
117f2783c15SPaul Mackerras 
118f2783c15SPaul Mackerras struct gettimeofday_struct do_gtod;
119f2783c15SPaul Mackerras 
120f2783c15SPaul Mackerras extern unsigned long wall_jiffies;
121f2783c15SPaul Mackerras 
122f2783c15SPaul Mackerras extern struct timezone sys_tz;
123f2783c15SPaul Mackerras static long timezone_offset;
124f2783c15SPaul Mackerras 
125f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
126f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
127f2783c15SPaul Mackerras 
12896c44507SPaul Mackerras u64 tb_last_jiffy __cacheline_aligned_in_smp;
12996c44507SPaul Mackerras unsigned long tb_last_stamp;
13096c44507SPaul Mackerras 
13196c44507SPaul Mackerras /*
13296c44507SPaul Mackerras  * Note that on ppc32 this only stores the bottom 32 bits of
13396c44507SPaul Mackerras  * the timebase value, but that's enough to tell when a jiffy
13496c44507SPaul Mackerras  * has passed.
13596c44507SPaul Mackerras  */
13696c44507SPaul Mackerras DEFINE_PER_CPU(unsigned long, last_jiffy);
13796c44507SPaul Mackerras 
1386defa38bSPaul Mackerras void __delay(unsigned long loops)
1396defa38bSPaul Mackerras {
1406defa38bSPaul Mackerras 	unsigned long start;
1416defa38bSPaul Mackerras 	int diff;
1426defa38bSPaul Mackerras 
1436defa38bSPaul Mackerras 	if (__USE_RTC()) {
1446defa38bSPaul Mackerras 		start = get_rtcl();
1456defa38bSPaul Mackerras 		do {
1466defa38bSPaul Mackerras 			/* the RTCL register wraps at 1000000000 */
1476defa38bSPaul Mackerras 			diff = get_rtcl() - start;
1486defa38bSPaul Mackerras 			if (diff < 0)
1496defa38bSPaul Mackerras 				diff += 1000000000;
1506defa38bSPaul Mackerras 		} while (diff < loops);
1516defa38bSPaul Mackerras 	} else {
1526defa38bSPaul Mackerras 		start = get_tbl();
1536defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
1546defa38bSPaul Mackerras 			HMT_low();
1556defa38bSPaul Mackerras 		HMT_medium();
1566defa38bSPaul Mackerras 	}
1576defa38bSPaul Mackerras }
1586defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
1596defa38bSPaul Mackerras 
1606defa38bSPaul Mackerras void udelay(unsigned long usecs)
1616defa38bSPaul Mackerras {
1626defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
1636defa38bSPaul Mackerras }
1646defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
1656defa38bSPaul Mackerras 
166f2783c15SPaul Mackerras static __inline__ void timer_check_rtc(void)
167f2783c15SPaul Mackerras {
168f2783c15SPaul Mackerras         /*
169f2783c15SPaul Mackerras          * update the rtc when needed, this should be performed on the
170f2783c15SPaul Mackerras          * right fraction of a second. Half or full second ?
171f2783c15SPaul Mackerras          * Full second works on mk48t59 clocks, others need testing.
172f2783c15SPaul Mackerras          * Note that this update is basically only used through
173f2783c15SPaul Mackerras          * the adjtimex system calls. Setting the HW clock in
174f2783c15SPaul Mackerras          * any other way is a /dev/rtc and userland business.
175f2783c15SPaul Mackerras          * This is still wrong by -0.5/+1.5 jiffies because of the
176f2783c15SPaul Mackerras          * timer interrupt resolution and possible delay, but here we
177f2783c15SPaul Mackerras          * hit a quantization limit which can only be solved by higher
178f2783c15SPaul Mackerras          * resolution timers and decoupling time management from timer
179f2783c15SPaul Mackerras          * interrupts. This is also wrong on the clocks
180f2783c15SPaul Mackerras          * which require being written at the half second boundary.
181f2783c15SPaul Mackerras          * We should have an rtc call that only sets the minutes and
182f2783c15SPaul Mackerras          * seconds like on Intel to avoid problems with non UTC clocks.
183f2783c15SPaul Mackerras          */
184d2e61512SKumar Gala         if (ppc_md.set_rtc_time && ntp_synced() &&
185f2783c15SPaul Mackerras 	    xtime.tv_sec - last_rtc_update >= 659 &&
186*092b8f34SPaul Mackerras 	    abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ) {
187f2783c15SPaul Mackerras 		struct rtc_time tm;
188f2783c15SPaul Mackerras 		to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
189f2783c15SPaul Mackerras 		tm.tm_year -= 1900;
190f2783c15SPaul Mackerras 		tm.tm_mon -= 1;
191f2783c15SPaul Mackerras 		if (ppc_md.set_rtc_time(&tm) == 0)
192f2783c15SPaul Mackerras 			last_rtc_update = xtime.tv_sec + 1;
193f2783c15SPaul Mackerras 		else
194f2783c15SPaul Mackerras 			/* Try again one minute later */
195f2783c15SPaul Mackerras 			last_rtc_update += 60;
196f2783c15SPaul Mackerras         }
197f2783c15SPaul Mackerras }
198f2783c15SPaul Mackerras 
199f2783c15SPaul Mackerras /*
200f2783c15SPaul Mackerras  * This version of gettimeofday has microsecond resolution.
201f2783c15SPaul Mackerras  */
202f2783c15SPaul Mackerras static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
203f2783c15SPaul Mackerras {
204f2783c15SPaul Mackerras 	unsigned long sec, usec;
205f2783c15SPaul Mackerras 	u64 tb_ticks, xsec;
206f2783c15SPaul Mackerras 	struct gettimeofday_vars *temp_varp;
207f2783c15SPaul Mackerras 	u64 temp_tb_to_xs, temp_stamp_xsec;
208f2783c15SPaul Mackerras 
209f2783c15SPaul Mackerras 	/*
210f2783c15SPaul Mackerras 	 * These calculations are faster (gets rid of divides)
211f2783c15SPaul Mackerras 	 * if done in units of 1/2^20 rather than microseconds.
212f2783c15SPaul Mackerras 	 * The conversion to microseconds at the end is done
213f2783c15SPaul Mackerras 	 * without a divide (and in fact, without a multiply)
214f2783c15SPaul Mackerras 	 */
215f2783c15SPaul Mackerras 	temp_varp = do_gtod.varp;
216f2783c15SPaul Mackerras 	tb_ticks = tb_val - temp_varp->tb_orig_stamp;
217f2783c15SPaul Mackerras 	temp_tb_to_xs = temp_varp->tb_to_xs;
218f2783c15SPaul Mackerras 	temp_stamp_xsec = temp_varp->stamp_xsec;
219f2783c15SPaul Mackerras 	xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
220f2783c15SPaul Mackerras 	sec = xsec / XSEC_PER_SEC;
221f2783c15SPaul Mackerras 	usec = (unsigned long)xsec & (XSEC_PER_SEC - 1);
222f2783c15SPaul Mackerras 	usec = SCALE_XSEC(usec, 1000000);
223f2783c15SPaul Mackerras 
224f2783c15SPaul Mackerras 	tv->tv_sec = sec;
225f2783c15SPaul Mackerras 	tv->tv_usec = usec;
226f2783c15SPaul Mackerras }
227f2783c15SPaul Mackerras 
228f2783c15SPaul Mackerras void do_gettimeofday(struct timeval *tv)
229f2783c15SPaul Mackerras {
23096c44507SPaul Mackerras 	if (__USE_RTC()) {
23196c44507SPaul Mackerras 		/* do this the old way */
23296c44507SPaul Mackerras 		unsigned long flags, seq;
233*092b8f34SPaul Mackerras 		unsigned int sec, nsec, usec;
23496c44507SPaul Mackerras 
23596c44507SPaul Mackerras 		do {
23696c44507SPaul Mackerras 			seq = read_seqbegin_irqsave(&xtime_lock, flags);
23796c44507SPaul Mackerras 			sec = xtime.tv_sec;
23896c44507SPaul Mackerras 			nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
23996c44507SPaul Mackerras 		} while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
240*092b8f34SPaul Mackerras 		usec = nsec / 1000;
24196c44507SPaul Mackerras 		while (usec >= 1000000) {
24296c44507SPaul Mackerras 			usec -= 1000000;
24396c44507SPaul Mackerras 			++sec;
24496c44507SPaul Mackerras 		}
24596c44507SPaul Mackerras 		tv->tv_sec = sec;
24696c44507SPaul Mackerras 		tv->tv_usec = usec;
24796c44507SPaul Mackerras 		return;
24896c44507SPaul Mackerras 	}
249f2783c15SPaul Mackerras 	__do_gettimeofday(tv, get_tb());
250f2783c15SPaul Mackerras }
251f2783c15SPaul Mackerras 
252f2783c15SPaul Mackerras EXPORT_SYMBOL(do_gettimeofday);
253f2783c15SPaul Mackerras 
254f2783c15SPaul Mackerras /*
255f2783c15SPaul Mackerras  * There are two copies of tb_to_xs and stamp_xsec so that no
256f2783c15SPaul Mackerras  * lock is needed to access and use these values in
257f2783c15SPaul Mackerras  * do_gettimeofday.  We alternate the copies and as long as a
258f2783c15SPaul Mackerras  * reasonable time elapses between changes, there will never
259f2783c15SPaul Mackerras  * be inconsistent values.  ntpd has a minimum of one minute
260f2783c15SPaul Mackerras  * between updates.
261f2783c15SPaul Mackerras  */
262f2783c15SPaul Mackerras static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
2635d14a18dSPaul Mackerras 			       u64 new_tb_to_xs)
264f2783c15SPaul Mackerras {
265f2783c15SPaul Mackerras 	unsigned temp_idx;
266f2783c15SPaul Mackerras 	struct gettimeofday_vars *temp_varp;
267f2783c15SPaul Mackerras 
268f2783c15SPaul Mackerras 	temp_idx = (do_gtod.var_idx == 0);
269f2783c15SPaul Mackerras 	temp_varp = &do_gtod.vars[temp_idx];
270f2783c15SPaul Mackerras 
271f2783c15SPaul Mackerras 	temp_varp->tb_to_xs = new_tb_to_xs;
272f2783c15SPaul Mackerras 	temp_varp->tb_orig_stamp = new_tb_stamp;
273f2783c15SPaul Mackerras 	temp_varp->stamp_xsec = new_stamp_xsec;
274f2783c15SPaul Mackerras 	smp_mb();
275f2783c15SPaul Mackerras 	do_gtod.varp = temp_varp;
276f2783c15SPaul Mackerras 	do_gtod.var_idx = temp_idx;
277f2783c15SPaul Mackerras 
278f2783c15SPaul Mackerras 	/*
279f2783c15SPaul Mackerras 	 * tb_update_count is used to allow the userspace gettimeofday code
280f2783c15SPaul Mackerras 	 * to assure itself that it sees a consistent view of the tb_to_xs and
281f2783c15SPaul Mackerras 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
282f2783c15SPaul Mackerras 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
283f2783c15SPaul Mackerras 	 * the two values of tb_update_count match and are even then the
284f2783c15SPaul Mackerras 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
285f2783c15SPaul Mackerras 	 * loops back and reads them again until this criteria is met.
286f2783c15SPaul Mackerras 	 */
287a7f290daSBenjamin Herrenschmidt 	++(vdso_data->tb_update_count);
288f2783c15SPaul Mackerras 	smp_wmb();
289a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_orig_stamp = new_tb_stamp;
290a7f290daSBenjamin Herrenschmidt 	vdso_data->stamp_xsec = new_stamp_xsec;
291a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_to_xs = new_tb_to_xs;
292a7f290daSBenjamin Herrenschmidt 	vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec;
293a7f290daSBenjamin Herrenschmidt 	vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec;
294f2783c15SPaul Mackerras 	smp_wmb();
295a7f290daSBenjamin Herrenschmidt 	++(vdso_data->tb_update_count);
296f2783c15SPaul Mackerras }
297f2783c15SPaul Mackerras 
298f2783c15SPaul Mackerras /*
299f2783c15SPaul Mackerras  * When the timebase - tb_orig_stamp gets too big, we do a manipulation
300f2783c15SPaul Mackerras  * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
301f2783c15SPaul Mackerras  * difference tb - tb_orig_stamp small enough to always fit inside a
302f2783c15SPaul Mackerras  * 32 bits number. This is a requirement of our fast 32 bits userland
303f2783c15SPaul Mackerras  * implementation in the vdso. If we "miss" a call to this function
304f2783c15SPaul Mackerras  * (interrupt latency, CPU locked in a spinlock, ...) and we end up
305f2783c15SPaul Mackerras  * with a too big difference, then the vdso will fallback to calling
306f2783c15SPaul Mackerras  * the syscall
307f2783c15SPaul Mackerras  */
308f2783c15SPaul Mackerras static __inline__ void timer_recalc_offset(u64 cur_tb)
309f2783c15SPaul Mackerras {
310f2783c15SPaul Mackerras 	unsigned long offset;
311f2783c15SPaul Mackerras 	u64 new_stamp_xsec;
312*092b8f34SPaul Mackerras 	u64 tlen, t2x;
313f2783c15SPaul Mackerras 
31496c44507SPaul Mackerras 	if (__USE_RTC())
31596c44507SPaul Mackerras 		return;
316*092b8f34SPaul Mackerras 	tlen = current_tick_length();
317f2783c15SPaul Mackerras 	offset = cur_tb - do_gtod.varp->tb_orig_stamp;
318*092b8f34SPaul Mackerras 	if (tlen == last_tick_len && offset < 0x80000000u) {
319*092b8f34SPaul Mackerras 		/* check that we're still in sync; if not, resync */
320*092b8f34SPaul Mackerras 		struct timeval tv;
321*092b8f34SPaul Mackerras 		__do_gettimeofday(&tv, cur_tb);
322*092b8f34SPaul Mackerras 		if (tv.tv_sec <= xtime.tv_sec &&
323*092b8f34SPaul Mackerras 		    (tv.tv_sec < xtime.tv_sec ||
324*092b8f34SPaul Mackerras 		     tv.tv_usec * 1000 <= xtime.tv_nsec))
325f2783c15SPaul Mackerras 			return;
326*092b8f34SPaul Mackerras 	}
327*092b8f34SPaul Mackerras 	if (tlen != last_tick_len) {
328*092b8f34SPaul Mackerras 		t2x = mulhdu(tlen << TICKLEN_SHIFT, ticklen_to_xs);
329*092b8f34SPaul Mackerras 		last_tick_len = tlen;
330*092b8f34SPaul Mackerras 	} else
331*092b8f34SPaul Mackerras 		t2x = do_gtod.varp->tb_to_xs;
332*092b8f34SPaul Mackerras 	new_stamp_xsec = (u64) xtime.tv_nsec * XSEC_PER_SEC;
333*092b8f34SPaul Mackerras 	do_div(new_stamp_xsec, 1000000000);
334*092b8f34SPaul Mackerras 	new_stamp_xsec += (u64) xtime.tv_sec * XSEC_PER_SEC;
335*092b8f34SPaul Mackerras 	update_gtod(cur_tb, new_stamp_xsec, t2x);
336f2783c15SPaul Mackerras }
337f2783c15SPaul Mackerras 
338f2783c15SPaul Mackerras #ifdef CONFIG_SMP
339f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
340f2783c15SPaul Mackerras {
341f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
342f2783c15SPaul Mackerras 
343f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
344f2783c15SPaul Mackerras 		return regs->link;
345f2783c15SPaul Mackerras 
346f2783c15SPaul Mackerras 	return pc;
347f2783c15SPaul Mackerras }
348f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
349f2783c15SPaul Mackerras #endif
350f2783c15SPaul Mackerras 
351f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
352f2783c15SPaul Mackerras 
353f2783c15SPaul Mackerras /*
354f2783c15SPaul Mackerras  * This function recalibrates the timebase based on the 49-bit time-of-day
355f2783c15SPaul Mackerras  * value in the Titan chip.  The Titan is much more accurate than the value
356f2783c15SPaul Mackerras  * returned by the service processor for the timebase frequency.
357f2783c15SPaul Mackerras  */
358f2783c15SPaul Mackerras 
359f2783c15SPaul Mackerras static void iSeries_tb_recal(void)
360f2783c15SPaul Mackerras {
361f2783c15SPaul Mackerras 	struct div_result divres;
362f2783c15SPaul Mackerras 	unsigned long titan, tb;
363f2783c15SPaul Mackerras 	tb = get_tb();
364f2783c15SPaul Mackerras 	titan = HvCallXm_loadTod();
365f2783c15SPaul Mackerras 	if ( iSeries_recal_titan ) {
366f2783c15SPaul Mackerras 		unsigned long tb_ticks = tb - iSeries_recal_tb;
367f2783c15SPaul Mackerras 		unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
368f2783c15SPaul Mackerras 		unsigned long new_tb_ticks_per_sec   = (tb_ticks * USEC_PER_SEC)/titan_usec;
369f2783c15SPaul Mackerras 		unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
370f2783c15SPaul Mackerras 		long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
371f2783c15SPaul Mackerras 		char sign = '+';
372f2783c15SPaul Mackerras 		/* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
373f2783c15SPaul Mackerras 		new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
374f2783c15SPaul Mackerras 
375f2783c15SPaul Mackerras 		if ( tick_diff < 0 ) {
376f2783c15SPaul Mackerras 			tick_diff = -tick_diff;
377f2783c15SPaul Mackerras 			sign = '-';
378f2783c15SPaul Mackerras 		}
379f2783c15SPaul Mackerras 		if ( tick_diff ) {
380f2783c15SPaul Mackerras 			if ( tick_diff < tb_ticks_per_jiffy/25 ) {
381f2783c15SPaul Mackerras 				printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
382f2783c15SPaul Mackerras 						new_tb_ticks_per_jiffy, sign, tick_diff );
383f2783c15SPaul Mackerras 				tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
384f2783c15SPaul Mackerras 				tb_ticks_per_sec   = new_tb_ticks_per_sec;
385f2783c15SPaul Mackerras 				div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
386f2783c15SPaul Mackerras 				do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
387f2783c15SPaul Mackerras 				tb_to_xs = divres.result_low;
388f2783c15SPaul Mackerras 				do_gtod.varp->tb_to_xs = tb_to_xs;
389a7f290daSBenjamin Herrenschmidt 				vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
390a7f290daSBenjamin Herrenschmidt 				vdso_data->tb_to_xs = tb_to_xs;
391f2783c15SPaul Mackerras 			}
392f2783c15SPaul Mackerras 			else {
393f2783c15SPaul Mackerras 				printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
394f2783c15SPaul Mackerras 					"                   new tb_ticks_per_jiffy = %lu\n"
395f2783c15SPaul Mackerras 					"                   old tb_ticks_per_jiffy = %lu\n",
396f2783c15SPaul Mackerras 					new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
397f2783c15SPaul Mackerras 			}
398f2783c15SPaul Mackerras 		}
399f2783c15SPaul Mackerras 	}
400f2783c15SPaul Mackerras 	iSeries_recal_titan = titan;
401f2783c15SPaul Mackerras 	iSeries_recal_tb = tb;
402f2783c15SPaul Mackerras }
403f2783c15SPaul Mackerras #endif
404f2783c15SPaul Mackerras 
405f2783c15SPaul Mackerras /*
406f2783c15SPaul Mackerras  * For iSeries shared processors, we have to let the hypervisor
407f2783c15SPaul Mackerras  * set the hardware decrementer.  We set a virtual decrementer
408f2783c15SPaul Mackerras  * in the lppaca and call the hypervisor if the virtual
409f2783c15SPaul Mackerras  * decrementer is less than the current value in the hardware
410f2783c15SPaul Mackerras  * decrementer. (almost always the new decrementer value will
411f2783c15SPaul Mackerras  * be greater than the current hardware decementer so the hypervisor
412f2783c15SPaul Mackerras  * call will not be needed)
413f2783c15SPaul Mackerras  */
414f2783c15SPaul Mackerras 
415f2783c15SPaul Mackerras /*
416f2783c15SPaul Mackerras  * timer_interrupt - gets called when the decrementer overflows,
417f2783c15SPaul Mackerras  * with interrupts disabled.
418f2783c15SPaul Mackerras  */
419f2783c15SPaul Mackerras void timer_interrupt(struct pt_regs * regs)
420f2783c15SPaul Mackerras {
421f2783c15SPaul Mackerras 	int next_dec;
422f2783c15SPaul Mackerras 	int cpu = smp_processor_id();
423f2783c15SPaul Mackerras 	unsigned long ticks;
424f2783c15SPaul Mackerras 
425f2783c15SPaul Mackerras #ifdef CONFIG_PPC32
426f2783c15SPaul Mackerras 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
427f2783c15SPaul Mackerras 		do_IRQ(regs);
428f2783c15SPaul Mackerras #endif
429f2783c15SPaul Mackerras 
430f2783c15SPaul Mackerras 	irq_enter();
431f2783c15SPaul Mackerras 
432f2783c15SPaul Mackerras 	profile_tick(CPU_PROFILING, regs);
433f2783c15SPaul Mackerras 
434f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
4353356bb9fSDavid Gibson 	get_lppaca()->int_dword.fields.decr_int = 0;
436f2783c15SPaul Mackerras #endif
437f2783c15SPaul Mackerras 
438f2783c15SPaul Mackerras 	while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
439f2783c15SPaul Mackerras 	       >= tb_ticks_per_jiffy) {
440f2783c15SPaul Mackerras 		/* Update last_jiffy */
441f2783c15SPaul Mackerras 		per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy;
442f2783c15SPaul Mackerras 		/* Handle RTCL overflow on 601 */
443f2783c15SPaul Mackerras 		if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000)
444f2783c15SPaul Mackerras 			per_cpu(last_jiffy, cpu) -= 1000000000;
445f2783c15SPaul Mackerras 
446f2783c15SPaul Mackerras 		/*
447f2783c15SPaul Mackerras 		 * We cannot disable the decrementer, so in the period
448f2783c15SPaul Mackerras 		 * between this cpu's being marked offline in cpu_online_map
449f2783c15SPaul Mackerras 		 * and calling stop-self, it is taking timer interrupts.
450f2783c15SPaul Mackerras 		 * Avoid calling into the scheduler rebalancing code if this
451f2783c15SPaul Mackerras 		 * is the case.
452f2783c15SPaul Mackerras 		 */
453f2783c15SPaul Mackerras 		if (!cpu_is_offline(cpu))
454f2783c15SPaul Mackerras 			update_process_times(user_mode(regs));
455f2783c15SPaul Mackerras 
456f2783c15SPaul Mackerras 		/*
457f2783c15SPaul Mackerras 		 * No need to check whether cpu is offline here; boot_cpuid
458f2783c15SPaul Mackerras 		 * should have been fixed up by now.
459f2783c15SPaul Mackerras 		 */
460f2783c15SPaul Mackerras 		if (cpu != boot_cpuid)
461f2783c15SPaul Mackerras 			continue;
462f2783c15SPaul Mackerras 
463f2783c15SPaul Mackerras 		write_seqlock(&xtime_lock);
46496c44507SPaul Mackerras 		tb_last_jiffy += tb_ticks_per_jiffy;
46596c44507SPaul Mackerras 		tb_last_stamp = per_cpu(last_jiffy, cpu);
466f2783c15SPaul Mackerras 		do_timer(regs);
467*092b8f34SPaul Mackerras 		timer_recalc_offset(tb_last_jiffy);
468f2783c15SPaul Mackerras 		timer_check_rtc();
469f2783c15SPaul Mackerras 		write_sequnlock(&xtime_lock);
470f2783c15SPaul Mackerras 	}
471f2783c15SPaul Mackerras 
472f2783c15SPaul Mackerras 	next_dec = tb_ticks_per_jiffy - ticks;
473f2783c15SPaul Mackerras 	set_dec(next_dec);
474f2783c15SPaul Mackerras 
475f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
476f2783c15SPaul Mackerras 	if (hvlpevent_is_pending())
477f2783c15SPaul Mackerras 		process_hvlpevents(regs);
478f2783c15SPaul Mackerras #endif
479f2783c15SPaul Mackerras 
480f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
481f2783c15SPaul Mackerras 	/* collect purr register values often, for accurate calculations */
482f2783c15SPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
483f2783c15SPaul Mackerras 		struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
484f2783c15SPaul Mackerras 		cu->current_tb = mfspr(SPRN_PURR);
485f2783c15SPaul Mackerras 	}
486f2783c15SPaul Mackerras #endif
487f2783c15SPaul Mackerras 
488f2783c15SPaul Mackerras 	irq_exit();
489f2783c15SPaul Mackerras }
490f2783c15SPaul Mackerras 
491f2783c15SPaul Mackerras void wakeup_decrementer(void)
492f2783c15SPaul Mackerras {
493*092b8f34SPaul Mackerras 	unsigned long ticks;
494f2783c15SPaul Mackerras 
495f2783c15SPaul Mackerras 	/*
496*092b8f34SPaul Mackerras 	 * The timebase gets saved on sleep and restored on wakeup,
497*092b8f34SPaul Mackerras 	 * so all we need to do is to reset the decrementer.
498f2783c15SPaul Mackerras 	 */
499*092b8f34SPaul Mackerras 	ticks = tb_ticks_since(__get_cpu_var(last_jiffy));
500*092b8f34SPaul Mackerras 	if (ticks < tb_ticks_per_jiffy)
501*092b8f34SPaul Mackerras 		ticks = tb_ticks_per_jiffy - ticks;
502*092b8f34SPaul Mackerras 	else
503*092b8f34SPaul Mackerras 		ticks = 1;
504*092b8f34SPaul Mackerras 	set_dec(ticks);
505f2783c15SPaul Mackerras }
506f2783c15SPaul Mackerras 
507a5b518edSPaul Mackerras #ifdef CONFIG_SMP
508f2783c15SPaul Mackerras void __init smp_space_timers(unsigned int max_cpus)
509f2783c15SPaul Mackerras {
510f2783c15SPaul Mackerras 	int i;
511f2783c15SPaul Mackerras 	unsigned long offset = tb_ticks_per_jiffy / max_cpus;
512f2783c15SPaul Mackerras 	unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
513f2783c15SPaul Mackerras 
514cbe62e2bSPaul Mackerras 	/* make sure tb > per_cpu(last_jiffy, cpu) for all cpus always */
515cbe62e2bSPaul Mackerras 	previous_tb -= tb_ticks_per_jiffy;
516f2783c15SPaul Mackerras 	for_each_cpu(i) {
517f2783c15SPaul Mackerras 		if (i != boot_cpuid) {
518f2783c15SPaul Mackerras 			previous_tb += offset;
519f2783c15SPaul Mackerras 			per_cpu(last_jiffy, i) = previous_tb;
520f2783c15SPaul Mackerras 		}
521f2783c15SPaul Mackerras 	}
522f2783c15SPaul Mackerras }
523f2783c15SPaul Mackerras #endif
524f2783c15SPaul Mackerras 
525f2783c15SPaul Mackerras /*
526f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
527f2783c15SPaul Mackerras  *
528f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
529f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
530f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
531f2783c15SPaul Mackerras  */
532f2783c15SPaul Mackerras unsigned long long sched_clock(void)
533f2783c15SPaul Mackerras {
53496c44507SPaul Mackerras 	if (__USE_RTC())
53596c44507SPaul Mackerras 		return get_rtc();
536f2783c15SPaul Mackerras 	return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
537f2783c15SPaul Mackerras }
538f2783c15SPaul Mackerras 
539f2783c15SPaul Mackerras int do_settimeofday(struct timespec *tv)
540f2783c15SPaul Mackerras {
541f2783c15SPaul Mackerras 	time_t wtm_sec, new_sec = tv->tv_sec;
542f2783c15SPaul Mackerras 	long wtm_nsec, new_nsec = tv->tv_nsec;
543f2783c15SPaul Mackerras 	unsigned long flags;
544*092b8f34SPaul Mackerras 	u64 new_xsec;
545*092b8f34SPaul Mackerras 	unsigned long tb_delta;
546f2783c15SPaul Mackerras 
547f2783c15SPaul Mackerras 	if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
548f2783c15SPaul Mackerras 		return -EINVAL;
549f2783c15SPaul Mackerras 
550f2783c15SPaul Mackerras 	write_seqlock_irqsave(&xtime_lock, flags);
551f2783c15SPaul Mackerras 
552f2783c15SPaul Mackerras 	/*
553f2783c15SPaul Mackerras 	 * Updating the RTC is not the job of this code. If the time is
554f2783c15SPaul Mackerras 	 * stepped under NTP, the RTC will be updated after STA_UNSYNC
555f2783c15SPaul Mackerras 	 * is cleared.  Tools like clock/hwclock either copy the RTC
556f2783c15SPaul Mackerras 	 * to the system time, in which case there is no point in writing
557f2783c15SPaul Mackerras 	 * to the RTC again, or write to the RTC but then they don't call
558f2783c15SPaul Mackerras 	 * settimeofday to perform this operation.
559f2783c15SPaul Mackerras 	 */
560f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
561f2783c15SPaul Mackerras 	if (first_settimeofday) {
562f2783c15SPaul Mackerras 		iSeries_tb_recal();
563f2783c15SPaul Mackerras 		first_settimeofday = 0;
564f2783c15SPaul Mackerras 	}
565f2783c15SPaul Mackerras #endif
566*092b8f34SPaul Mackerras 
567*092b8f34SPaul Mackerras 	/*
568*092b8f34SPaul Mackerras 	 * Subtract off the number of nanoseconds since the
569*092b8f34SPaul Mackerras 	 * beginning of the last tick.
570*092b8f34SPaul Mackerras 	 * Note that since we don't increment jiffies_64 anywhere other
571*092b8f34SPaul Mackerras 	 * than in do_timer (since we don't have a lost tick problem),
572*092b8f34SPaul Mackerras 	 * wall_jiffies will always be the same as jiffies,
573*092b8f34SPaul Mackerras 	 * and therefore the (jiffies - wall_jiffies) computation
574*092b8f34SPaul Mackerras 	 * has been removed.
575*092b8f34SPaul Mackerras 	 */
576f2783c15SPaul Mackerras 	tb_delta = tb_ticks_since(tb_last_stamp);
577*092b8f34SPaul Mackerras 	tb_delta = mulhdu(tb_delta, do_gtod.varp->tb_to_xs); /* in xsec */
578*092b8f34SPaul Mackerras 	new_nsec -= SCALE_XSEC(tb_delta, 1000000000);
579f2783c15SPaul Mackerras 
580f2783c15SPaul Mackerras 	wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
581f2783c15SPaul Mackerras 	wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
582f2783c15SPaul Mackerras 
583f2783c15SPaul Mackerras  	set_normalized_timespec(&xtime, new_sec, new_nsec);
584f2783c15SPaul Mackerras 	set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
585f2783c15SPaul Mackerras 
586f2783c15SPaul Mackerras 	/* In case of a large backwards jump in time with NTP, we want the
587f2783c15SPaul Mackerras 	 * clock to be updated as soon as the PLL is again in lock.
588f2783c15SPaul Mackerras 	 */
589f2783c15SPaul Mackerras 	last_rtc_update = new_sec - 658;
590f2783c15SPaul Mackerras 
591f2783c15SPaul Mackerras 	ntp_clear();
592f2783c15SPaul Mackerras 
593*092b8f34SPaul Mackerras 	new_xsec = xtime.tv_nsec;
594*092b8f34SPaul Mackerras 	if (new_xsec != 0) {
595*092b8f34SPaul Mackerras 		new_xsec *= XSEC_PER_SEC;
596f2783c15SPaul Mackerras 		do_div(new_xsec, NSEC_PER_SEC);
5975f6b5b97SPaul Mackerras 	}
598*092b8f34SPaul Mackerras 	new_xsec += (u64)xtime.tv_sec * XSEC_PER_SEC;
59996c44507SPaul Mackerras 	update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
600f2783c15SPaul Mackerras 
601a7f290daSBenjamin Herrenschmidt 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
602a7f290daSBenjamin Herrenschmidt 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
603f2783c15SPaul Mackerras 
604f2783c15SPaul Mackerras 	write_sequnlock_irqrestore(&xtime_lock, flags);
605f2783c15SPaul Mackerras 	clock_was_set();
606f2783c15SPaul Mackerras 	return 0;
607f2783c15SPaul Mackerras }
608f2783c15SPaul Mackerras 
609f2783c15SPaul Mackerras EXPORT_SYMBOL(do_settimeofday);
610f2783c15SPaul Mackerras 
611f2783c15SPaul Mackerras void __init generic_calibrate_decr(void)
612f2783c15SPaul Mackerras {
613f2783c15SPaul Mackerras 	struct device_node *cpu;
614f2783c15SPaul Mackerras 	unsigned int *fp;
615f2783c15SPaul Mackerras 	int node_found;
616f2783c15SPaul Mackerras 
617f2783c15SPaul Mackerras 	/*
618f2783c15SPaul Mackerras 	 * The cpu node should have a timebase-frequency property
619f2783c15SPaul Mackerras 	 * to tell us the rate at which the decrementer counts.
620f2783c15SPaul Mackerras 	 */
621f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
622f2783c15SPaul Mackerras 
623f2783c15SPaul Mackerras 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
624f2783c15SPaul Mackerras 	node_found = 0;
625d8a8188dSOlaf Hering 	if (cpu) {
626f2783c15SPaul Mackerras 		fp = (unsigned int *)get_property(cpu, "timebase-frequency",
627f2783c15SPaul Mackerras 						  NULL);
628d8a8188dSOlaf Hering 		if (fp) {
629f2783c15SPaul Mackerras 			node_found = 1;
630f2783c15SPaul Mackerras 			ppc_tb_freq = *fp;
631f2783c15SPaul Mackerras 		}
632f2783c15SPaul Mackerras 	}
633f2783c15SPaul Mackerras 	if (!node_found)
634f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
635f2783c15SPaul Mackerras 				"(not found)\n");
636f2783c15SPaul Mackerras 
637f2783c15SPaul Mackerras 	ppc_proc_freq = DEFAULT_PROC_FREQ;
638f2783c15SPaul Mackerras 	node_found = 0;
639d8a8188dSOlaf Hering 	if (cpu) {
640f2783c15SPaul Mackerras 		fp = (unsigned int *)get_property(cpu, "clock-frequency",
641f2783c15SPaul Mackerras 						  NULL);
642d8a8188dSOlaf Hering 		if (fp) {
643f2783c15SPaul Mackerras 			node_found = 1;
644f2783c15SPaul Mackerras 			ppc_proc_freq = *fp;
645f2783c15SPaul Mackerras 		}
646f2783c15SPaul Mackerras 	}
6470fd6f717SKumar Gala #ifdef CONFIG_BOOKE
6480fd6f717SKumar Gala 	/* Set the time base to zero */
6490fd6f717SKumar Gala 	mtspr(SPRN_TBWL, 0);
6500fd6f717SKumar Gala 	mtspr(SPRN_TBWU, 0);
6510fd6f717SKumar Gala 
6520fd6f717SKumar Gala 	/* Clear any pending timer interrupts */
6530fd6f717SKumar Gala 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
6540fd6f717SKumar Gala 
6550fd6f717SKumar Gala 	/* Enable decrementer interrupt */
6560fd6f717SKumar Gala 	mtspr(SPRN_TCR, TCR_DIE);
6570fd6f717SKumar Gala #endif
658f2783c15SPaul Mackerras 	if (!node_found)
659f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating processor frequency "
660f2783c15SPaul Mackerras 				"(not found)\n");
661f2783c15SPaul Mackerras 
662f2783c15SPaul Mackerras 	of_node_put(cpu);
663f2783c15SPaul Mackerras }
664f2783c15SPaul Mackerras 
665f2783c15SPaul Mackerras unsigned long get_boot_time(void)
666f2783c15SPaul Mackerras {
667f2783c15SPaul Mackerras 	struct rtc_time tm;
668f2783c15SPaul Mackerras 
669f2783c15SPaul Mackerras 	if (ppc_md.get_boot_time)
670f2783c15SPaul Mackerras 		return ppc_md.get_boot_time();
671f2783c15SPaul Mackerras 	if (!ppc_md.get_rtc_time)
672f2783c15SPaul Mackerras 		return 0;
673f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
674f2783c15SPaul Mackerras 	return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
675f2783c15SPaul Mackerras 		      tm.tm_hour, tm.tm_min, tm.tm_sec);
676f2783c15SPaul Mackerras }
677f2783c15SPaul Mackerras 
678f2783c15SPaul Mackerras /* This function is only called on the boot processor */
679f2783c15SPaul Mackerras void __init time_init(void)
680f2783c15SPaul Mackerras {
681f2783c15SPaul Mackerras 	unsigned long flags;
682f2783c15SPaul Mackerras 	unsigned long tm = 0;
683f2783c15SPaul Mackerras 	struct div_result res;
684*092b8f34SPaul Mackerras 	u64 scale, x;
685f2783c15SPaul Mackerras 	unsigned shift;
686f2783c15SPaul Mackerras 
687f2783c15SPaul Mackerras         if (ppc_md.time_init != NULL)
688f2783c15SPaul Mackerras                 timezone_offset = ppc_md.time_init();
689f2783c15SPaul Mackerras 
69096c44507SPaul Mackerras 	if (__USE_RTC()) {
69196c44507SPaul Mackerras 		/* 601 processor: dec counts down by 128 every 128ns */
69296c44507SPaul Mackerras 		ppc_tb_freq = 1000000000;
69396c44507SPaul Mackerras 		tb_last_stamp = get_rtcl();
69496c44507SPaul Mackerras 		tb_last_jiffy = tb_last_stamp;
69596c44507SPaul Mackerras 	} else {
69696c44507SPaul Mackerras 		/* Normal PowerPC with timebase register */
697f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
698374e99d4SPaul Mackerras 		printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
699374e99d4SPaul Mackerras 		       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
700374e99d4SPaul Mackerras 		printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
701374e99d4SPaul Mackerras 		       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
70296c44507SPaul Mackerras 		tb_last_stamp = tb_last_jiffy = get_tb();
70396c44507SPaul Mackerras 	}
704374e99d4SPaul Mackerras 
705374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
706*092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
707374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
708374e99d4SPaul Mackerras 	tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
709*092b8f34SPaul Mackerras 
710*092b8f34SPaul Mackerras 	/*
711*092b8f34SPaul Mackerras 	 * Calculate the length of each tick in ns.  It will not be
712*092b8f34SPaul Mackerras 	 * exactly 1e9/HZ unless ppc_tb_freq is divisible by HZ.
713*092b8f34SPaul Mackerras 	 * We compute 1e9 * tb_ticks_per_jiffy / ppc_tb_freq,
714*092b8f34SPaul Mackerras 	 * rounded up.
715*092b8f34SPaul Mackerras 	 */
716*092b8f34SPaul Mackerras 	x = (u64) NSEC_PER_SEC * tb_ticks_per_jiffy + ppc_tb_freq - 1;
717*092b8f34SPaul Mackerras 	do_div(x, ppc_tb_freq);
718*092b8f34SPaul Mackerras 	tick_nsec = x;
719*092b8f34SPaul Mackerras 	last_tick_len = x << TICKLEN_SCALE;
720*092b8f34SPaul Mackerras 
721*092b8f34SPaul Mackerras 	/*
722*092b8f34SPaul Mackerras 	 * Compute ticklen_to_xs, which is a factor which gets multiplied
723*092b8f34SPaul Mackerras 	 * by (last_tick_len << TICKLEN_SHIFT) to get a tb_to_xs value.
724*092b8f34SPaul Mackerras 	 * It is computed as:
725*092b8f34SPaul Mackerras 	 * ticklen_to_xs = 2^N / (tb_ticks_per_jiffy * 1e9)
726*092b8f34SPaul Mackerras 	 * where N = 64 + 20 - TICKLEN_SCALE - TICKLEN_SHIFT
727*092b8f34SPaul Mackerras 	 * so as to give the result as a 0.64 fixed-point fraction.
728*092b8f34SPaul Mackerras 	 */
729*092b8f34SPaul Mackerras 	div128_by_32(1ULL << (64 + 20 - TICKLEN_SCALE - TICKLEN_SHIFT), 0,
730*092b8f34SPaul Mackerras 		     tb_ticks_per_jiffy, &res);
731*092b8f34SPaul Mackerras 	div128_by_32(res.result_high, res.result_low, NSEC_PER_SEC, &res);
732*092b8f34SPaul Mackerras 	ticklen_to_xs = res.result_low;
733*092b8f34SPaul Mackerras 
734*092b8f34SPaul Mackerras 	/* Compute tb_to_xs from tick_nsec */
735*092b8f34SPaul Mackerras 	tb_to_xs = mulhdu(last_tick_len << TICKLEN_SHIFT, ticklen_to_xs);
736374e99d4SPaul Mackerras 
737f2783c15SPaul Mackerras 	/*
738f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
739f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
740f2783c15SPaul Mackerras 	 * which is the timebase frequency.
741f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
742f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
743f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
744f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
745f2783c15SPaul Mackerras 	 * sched_clock().
746f2783c15SPaul Mackerras 	 */
747f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
748f2783c15SPaul Mackerras 	scale = res.result_low;
749f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
750f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
751f2783c15SPaul Mackerras 		res.result_high >>= 1;
752f2783c15SPaul Mackerras 	}
753f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
754f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
755f2783c15SPaul Mackerras 
756f2783c15SPaul Mackerras #ifdef CONFIG_PPC_ISERIES
757f2783c15SPaul Mackerras 	if (!piranha_simulator)
758f2783c15SPaul Mackerras #endif
759f2783c15SPaul Mackerras 		tm = get_boot_time();
760f2783c15SPaul Mackerras 
761f2783c15SPaul Mackerras 	write_seqlock_irqsave(&xtime_lock, flags);
762*092b8f34SPaul Mackerras 
763*092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
764*092b8f34SPaul Mackerras         if (timezone_offset) {
765*092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
766*092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
767*092b8f34SPaul Mackerras 		tm -= timezone_offset;
768*092b8f34SPaul Mackerras         }
769*092b8f34SPaul Mackerras 
770f2783c15SPaul Mackerras 	xtime.tv_sec = tm;
771f2783c15SPaul Mackerras 	xtime.tv_nsec = 0;
772f2783c15SPaul Mackerras 	do_gtod.varp = &do_gtod.vars[0];
773f2783c15SPaul Mackerras 	do_gtod.var_idx = 0;
77496c44507SPaul Mackerras 	do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
775f2783c15SPaul Mackerras 	__get_cpu_var(last_jiffy) = tb_last_stamp;
776f2783c15SPaul Mackerras 	do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
777f2783c15SPaul Mackerras 	do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
778f2783c15SPaul Mackerras 	do_gtod.varp->tb_to_xs = tb_to_xs;
779f2783c15SPaul Mackerras 	do_gtod.tb_to_us = tb_to_us;
780a7f290daSBenjamin Herrenschmidt 
781a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_orig_stamp = tb_last_jiffy;
782a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
783a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
784*092b8f34SPaul Mackerras 	vdso_data->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
785a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_to_xs = tb_to_xs;
786f2783c15SPaul Mackerras 
787f2783c15SPaul Mackerras 	time_freq = 0;
788f2783c15SPaul Mackerras 
789f2783c15SPaul Mackerras 	last_rtc_update = xtime.tv_sec;
790f2783c15SPaul Mackerras 	set_normalized_timespec(&wall_to_monotonic,
791f2783c15SPaul Mackerras 	                        -xtime.tv_sec, -xtime.tv_nsec);
792f2783c15SPaul Mackerras 	write_sequnlock_irqrestore(&xtime_lock, flags);
793f2783c15SPaul Mackerras 
794f2783c15SPaul Mackerras 	/* Not exact, but the timer interrupt takes care of this */
795f2783c15SPaul Mackerras 	set_dec(tb_ticks_per_jiffy);
796f2783c15SPaul Mackerras }
797f2783c15SPaul Mackerras 
798f2783c15SPaul Mackerras 
799f2783c15SPaul Mackerras #define FEBRUARY	2
800f2783c15SPaul Mackerras #define	STARTOFTIME	1970
801f2783c15SPaul Mackerras #define SECDAY		86400L
802f2783c15SPaul Mackerras #define SECYR		(SECDAY * 365)
803f2783c15SPaul Mackerras #define	leapyear(year)		((year) % 4 == 0 && \
804f2783c15SPaul Mackerras 				 ((year) % 100 != 0 || (year) % 400 == 0))
805f2783c15SPaul Mackerras #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
806f2783c15SPaul Mackerras #define	days_in_month(a) 	(month_days[(a) - 1])
807f2783c15SPaul Mackerras 
808f2783c15SPaul Mackerras static int month_days[12] = {
809f2783c15SPaul Mackerras 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
810f2783c15SPaul Mackerras };
811f2783c15SPaul Mackerras 
812f2783c15SPaul Mackerras /*
813f2783c15SPaul Mackerras  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
814f2783c15SPaul Mackerras  */
815f2783c15SPaul Mackerras void GregorianDay(struct rtc_time * tm)
816f2783c15SPaul Mackerras {
817f2783c15SPaul Mackerras 	int leapsToDate;
818f2783c15SPaul Mackerras 	int lastYear;
819f2783c15SPaul Mackerras 	int day;
820f2783c15SPaul Mackerras 	int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
821f2783c15SPaul Mackerras 
822f2783c15SPaul Mackerras 	lastYear = tm->tm_year - 1;
823f2783c15SPaul Mackerras 
824f2783c15SPaul Mackerras 	/*
825f2783c15SPaul Mackerras 	 * Number of leap corrections to apply up to end of last year
826f2783c15SPaul Mackerras 	 */
827f2783c15SPaul Mackerras 	leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
828f2783c15SPaul Mackerras 
829f2783c15SPaul Mackerras 	/*
830f2783c15SPaul Mackerras 	 * This year is a leap year if it is divisible by 4 except when it is
831f2783c15SPaul Mackerras 	 * divisible by 100 unless it is divisible by 400
832f2783c15SPaul Mackerras 	 *
833f2783c15SPaul Mackerras 	 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
834f2783c15SPaul Mackerras 	 */
835f2783c15SPaul Mackerras 	day = tm->tm_mon > 2 && leapyear(tm->tm_year);
836f2783c15SPaul Mackerras 
837f2783c15SPaul Mackerras 	day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
838f2783c15SPaul Mackerras 		   tm->tm_mday;
839f2783c15SPaul Mackerras 
840f2783c15SPaul Mackerras 	tm->tm_wday = day % 7;
841f2783c15SPaul Mackerras }
842f2783c15SPaul Mackerras 
843f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm)
844f2783c15SPaul Mackerras {
845f2783c15SPaul Mackerras 	register int    i;
846f2783c15SPaul Mackerras 	register long   hms, day;
847f2783c15SPaul Mackerras 
848f2783c15SPaul Mackerras 	day = tim / SECDAY;
849f2783c15SPaul Mackerras 	hms = tim % SECDAY;
850f2783c15SPaul Mackerras 
851f2783c15SPaul Mackerras 	/* Hours, minutes, seconds are easy */
852f2783c15SPaul Mackerras 	tm->tm_hour = hms / 3600;
853f2783c15SPaul Mackerras 	tm->tm_min = (hms % 3600) / 60;
854f2783c15SPaul Mackerras 	tm->tm_sec = (hms % 3600) % 60;
855f2783c15SPaul Mackerras 
856f2783c15SPaul Mackerras 	/* Number of years in days */
857f2783c15SPaul Mackerras 	for (i = STARTOFTIME; day >= days_in_year(i); i++)
858f2783c15SPaul Mackerras 		day -= days_in_year(i);
859f2783c15SPaul Mackerras 	tm->tm_year = i;
860f2783c15SPaul Mackerras 
861f2783c15SPaul Mackerras 	/* Number of months in days left */
862f2783c15SPaul Mackerras 	if (leapyear(tm->tm_year))
863f2783c15SPaul Mackerras 		days_in_month(FEBRUARY) = 29;
864f2783c15SPaul Mackerras 	for (i = 1; day >= days_in_month(i); i++)
865f2783c15SPaul Mackerras 		day -= days_in_month(i);
866f2783c15SPaul Mackerras 	days_in_month(FEBRUARY) = 28;
867f2783c15SPaul Mackerras 	tm->tm_mon = i;
868f2783c15SPaul Mackerras 
869f2783c15SPaul Mackerras 	/* Days are what is left over (+1) from all that. */
870f2783c15SPaul Mackerras 	tm->tm_mday = day + 1;
871f2783c15SPaul Mackerras 
872f2783c15SPaul Mackerras 	/*
873f2783c15SPaul Mackerras 	 * Determine the day of week
874f2783c15SPaul Mackerras 	 */
875f2783c15SPaul Mackerras 	GregorianDay(tm);
876f2783c15SPaul Mackerras }
877f2783c15SPaul Mackerras 
878f2783c15SPaul Mackerras /* Auxiliary function to compute scaling factors */
879f2783c15SPaul Mackerras /* Actually the choice of a timebase running at 1/4 the of the bus
880f2783c15SPaul Mackerras  * frequency giving resolution of a few tens of nanoseconds is quite nice.
881f2783c15SPaul Mackerras  * It makes this computation very precise (27-28 bits typically) which
882f2783c15SPaul Mackerras  * is optimistic considering the stability of most processor clock
883f2783c15SPaul Mackerras  * oscillators and the precision with which the timebase frequency
884f2783c15SPaul Mackerras  * is measured but does not harm.
885f2783c15SPaul Mackerras  */
886f2783c15SPaul Mackerras unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
887f2783c15SPaul Mackerras {
888f2783c15SPaul Mackerras         unsigned mlt=0, tmp, err;
889f2783c15SPaul Mackerras         /* No concern for performance, it's done once: use a stupid
890f2783c15SPaul Mackerras          * but safe and compact method to find the multiplier.
891f2783c15SPaul Mackerras          */
892f2783c15SPaul Mackerras 
893f2783c15SPaul Mackerras         for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
894f2783c15SPaul Mackerras                 if (mulhwu(inscale, mlt|tmp) < outscale)
895f2783c15SPaul Mackerras 			mlt |= tmp;
896f2783c15SPaul Mackerras         }
897f2783c15SPaul Mackerras 
898f2783c15SPaul Mackerras         /* We might still be off by 1 for the best approximation.
899f2783c15SPaul Mackerras          * A side effect of this is that if outscale is too large
900f2783c15SPaul Mackerras          * the returned value will be zero.
901f2783c15SPaul Mackerras          * Many corner cases have been checked and seem to work,
902f2783c15SPaul Mackerras          * some might have been forgotten in the test however.
903f2783c15SPaul Mackerras          */
904f2783c15SPaul Mackerras 
905f2783c15SPaul Mackerras         err = inscale * (mlt+1);
906f2783c15SPaul Mackerras         if (err <= inscale/2)
907f2783c15SPaul Mackerras 		mlt++;
908f2783c15SPaul Mackerras         return mlt;
909f2783c15SPaul Mackerras }
910f2783c15SPaul Mackerras 
911f2783c15SPaul Mackerras /*
912f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
913f2783c15SPaul Mackerras  * result.
914f2783c15SPaul Mackerras  */
915f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
916f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
917f2783c15SPaul Mackerras {
918f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
919f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
920f2783c15SPaul Mackerras 	u64 ra, rb, rc;
921f2783c15SPaul Mackerras 
922f2783c15SPaul Mackerras 	a = dividend_high >> 32;
923f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
924f2783c15SPaul Mackerras 	c = dividend_low >> 32;
925f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
926f2783c15SPaul Mackerras 
927f2783c15SPaul Mackerras 	w = a / divisor;
928f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
929f2783c15SPaul Mackerras 
930f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
931f2783c15SPaul Mackerras 	x = ra;
932f2783c15SPaul Mackerras 
933f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
934f2783c15SPaul Mackerras 	y = rb;
935f2783c15SPaul Mackerras 
936f2783c15SPaul Mackerras 	do_div(rc, divisor);
937f2783c15SPaul Mackerras 	z = rc;
938f2783c15SPaul Mackerras 
939f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
940f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
941f2783c15SPaul Mackerras 
942f2783c15SPaul Mackerras }
943