xref: /openbmc/linux/arch/powerpc/kernel/time.c (revision 6f7aba7bb4bd09824992cda7f77d4e8b1704a04c)
1f2783c15SPaul Mackerras /*
2f2783c15SPaul Mackerras  * Common time routines among all ppc machines.
3f2783c15SPaul Mackerras  *
4f2783c15SPaul Mackerras  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5f2783c15SPaul Mackerras  * Paul Mackerras' version and mine for PReP and Pmac.
6f2783c15SPaul Mackerras  * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
7f2783c15SPaul Mackerras  * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
8f2783c15SPaul Mackerras  *
9f2783c15SPaul Mackerras  * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
10f2783c15SPaul Mackerras  * to make clock more stable (2.4.0-test5). The only thing
11f2783c15SPaul Mackerras  * that this code assumes is that the timebases have been synchronized
12f2783c15SPaul Mackerras  * by firmware on SMP and are never stopped (never do sleep
13f2783c15SPaul Mackerras  * on SMP then, nap and doze are OK).
14f2783c15SPaul Mackerras  *
15f2783c15SPaul Mackerras  * Speeded up do_gettimeofday by getting rid of references to
16f2783c15SPaul Mackerras  * xtime (which required locks for consistency). (mikejc@us.ibm.com)
17f2783c15SPaul Mackerras  *
18f2783c15SPaul Mackerras  * TODO (not necessarily in this file):
19f2783c15SPaul Mackerras  * - improve precision and reproducibility of timebase frequency
20f5339277SStephen Rothwell  * measurement at boot time.
21f2783c15SPaul Mackerras  * - for astronomical applications: add a new function to get
22f2783c15SPaul Mackerras  * non ambiguous timestamps even around leap seconds. This needs
23f2783c15SPaul Mackerras  * a new timestamp format and a good name.
24f2783c15SPaul Mackerras  *
25f2783c15SPaul Mackerras  * 1997-09-10  Updated NTP code according to technical memorandum Jan '96
26f2783c15SPaul Mackerras  *             "A Kernel Model for Precision Timekeeping" by Dave Mills
27f2783c15SPaul Mackerras  *
28f2783c15SPaul Mackerras  *      This program is free software; you can redistribute it and/or
29f2783c15SPaul Mackerras  *      modify it under the terms of the GNU General Public License
30f2783c15SPaul Mackerras  *      as published by the Free Software Foundation; either version
31f2783c15SPaul Mackerras  *      2 of the License, or (at your option) any later version.
32f2783c15SPaul Mackerras  */
33f2783c15SPaul Mackerras 
34f2783c15SPaul Mackerras #include <linux/errno.h>
354b16f8e2SPaul Gortmaker #include <linux/export.h>
36f2783c15SPaul Mackerras #include <linux/sched.h>
37f2783c15SPaul Mackerras #include <linux/kernel.h>
38f2783c15SPaul Mackerras #include <linux/param.h>
39f2783c15SPaul Mackerras #include <linux/string.h>
40f2783c15SPaul Mackerras #include <linux/mm.h>
41f2783c15SPaul Mackerras #include <linux/interrupt.h>
42f2783c15SPaul Mackerras #include <linux/timex.h>
43f2783c15SPaul Mackerras #include <linux/kernel_stat.h>
44f2783c15SPaul Mackerras #include <linux/time.h>
45f2783c15SPaul Mackerras #include <linux/init.h>
46f2783c15SPaul Mackerras #include <linux/profile.h>
47f2783c15SPaul Mackerras #include <linux/cpu.h>
48f2783c15SPaul Mackerras #include <linux/security.h>
49f2783c15SPaul Mackerras #include <linux/percpu.h>
50f2783c15SPaul Mackerras #include <linux/rtc.h>
51092b8f34SPaul Mackerras #include <linux/jiffies.h>
52c6622f63SPaul Mackerras #include <linux/posix-timers.h>
537d12e780SDavid Howells #include <linux/irq.h>
54177996e6SBenjamin Herrenschmidt #include <linux/delay.h>
55e360adbeSPeter Zijlstra #include <linux/irq_work.h>
566795b85cSAnton Blanchard #include <asm/trace.h>
57f2783c15SPaul Mackerras 
58f2783c15SPaul Mackerras #include <asm/io.h>
59f2783c15SPaul Mackerras #include <asm/processor.h>
60f2783c15SPaul Mackerras #include <asm/nvram.h>
61f2783c15SPaul Mackerras #include <asm/cache.h>
62f2783c15SPaul Mackerras #include <asm/machdep.h>
63f2783c15SPaul Mackerras #include <asm/uaccess.h>
64f2783c15SPaul Mackerras #include <asm/time.h>
65f2783c15SPaul Mackerras #include <asm/prom.h>
66f2783c15SPaul Mackerras #include <asm/irq.h>
67f2783c15SPaul Mackerras #include <asm/div64.h>
682249ca9dSPaul Mackerras #include <asm/smp.h>
69a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h>
70f2783c15SPaul Mackerras #include <asm/firmware.h>
7106b8e878SMichael Neuling #include <asm/cputime.h>
72f2783c15SPaul Mackerras 
734a4cfe38STony Breeds /* powerpc clocksource/clockevent code */
744a4cfe38STony Breeds 
75d831d0b8STony Breeds #include <linux/clockchips.h>
76189374aeSJohn Stultz #include <linux/timekeeper_internal.h>
774a4cfe38STony Breeds 
788e19608eSMagnus Damm static cycle_t rtc_read(struct clocksource *);
794a4cfe38STony Breeds static struct clocksource clocksource_rtc = {
804a4cfe38STony Breeds 	.name         = "rtc",
814a4cfe38STony Breeds 	.rating       = 400,
824a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
834a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
844a4cfe38STony Breeds 	.read         = rtc_read,
854a4cfe38STony Breeds };
864a4cfe38STony Breeds 
878e19608eSMagnus Damm static cycle_t timebase_read(struct clocksource *);
884a4cfe38STony Breeds static struct clocksource clocksource_timebase = {
894a4cfe38STony Breeds 	.name         = "timebase",
904a4cfe38STony Breeds 	.rating       = 400,
914a4cfe38STony Breeds 	.flags        = CLOCK_SOURCE_IS_CONTINUOUS,
924a4cfe38STony Breeds 	.mask         = CLOCKSOURCE_MASK(64),
934a4cfe38STony Breeds 	.read         = timebase_read,
944a4cfe38STony Breeds };
954a4cfe38STony Breeds 
96d831d0b8STony Breeds #define DECREMENTER_MAX	0x7fffffff
97d831d0b8STony Breeds 
98d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
99d831d0b8STony Breeds 				      struct clock_event_device *dev);
100d831d0b8STony Breeds static void decrementer_set_mode(enum clock_event_mode mode,
101d831d0b8STony Breeds 				 struct clock_event_device *dev);
102d831d0b8STony Breeds 
1036e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = {
104d831d0b8STony Breeds 	.name           = "decrementer",
105d831d0b8STony Breeds 	.rating         = 200,
106d831d0b8STony Breeds 	.irq            = 0,
107d831d0b8STony Breeds 	.set_next_event = decrementer_set_next_event,
108d831d0b8STony Breeds 	.set_mode       = decrementer_set_mode,
109d831d0b8STony Breeds 	.features       = CLOCK_EVT_FEAT_ONESHOT,
110d831d0b8STony Breeds };
1116e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent);
112d831d0b8STony Breeds 
1137df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb);
1147df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers);
115d831d0b8STony Breeds 
116f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024)
117f2783c15SPaul Mackerras 
118f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
119f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	(((xsec) * max) / XSEC_PER_SEC)
120f2783c15SPaul Mackerras #else
121f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */
122f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max)	mulhwu((xsec) << 12, max)
123f2783c15SPaul Mackerras #endif
124f2783c15SPaul Mackerras 
125f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy;
126f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */
127f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec);
128f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec;
1292cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec);	/* for cputime_t conversions */
130092b8f34SPaul Mackerras 
131f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock);
132f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock);
133f2783c15SPaul Mackerras 
134fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly;
135fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly;
136364a1246SHeiko Schocher static u64 boot_tb __read_mostly;
137f2783c15SPaul Mackerras 
138f2783c15SPaul Mackerras extern struct timezone sys_tz;
139f2783c15SPaul Mackerras static long timezone_offset;
140f2783c15SPaul Mackerras 
141f2783c15SPaul Mackerras unsigned long ppc_proc_freq;
14255ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq);
143f2783c15SPaul Mackerras unsigned long ppc_tb_freq;
14455ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq);
14596c44507SPaul Mackerras 
146abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
147c6622f63SPaul Mackerras /*
148c6622f63SPaul Mackerras  * Factors for converting from cputime_t (timebase ticks) to
1499f5072d4SAndreas Schwab  * jiffies, microseconds, seconds, and clock_t (1/USER_HZ seconds).
150c6622f63SPaul Mackerras  * These are all stored as 0.64 fixed-point binary fractions.
151c6622f63SPaul Mackerras  */
152c6622f63SPaul Mackerras u64 __cputime_jiffies_factor;
1532cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_jiffies_factor);
1549f5072d4SAndreas Schwab u64 __cputime_usec_factor;
1559f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor);
156c6622f63SPaul Mackerras u64 __cputime_sec_factor;
1572cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_sec_factor);
158c6622f63SPaul Mackerras u64 __cputime_clockt_factor;
1592cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_clockt_factor);
16006b8e878SMichael Neuling DEFINE_PER_CPU(unsigned long, cputime_last_delta);
16106b8e878SMichael Neuling DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta);
162c6622f63SPaul Mackerras 
163a42548a1SStanislaw Gruszka cputime_t cputime_one_jiffy;
164a42548a1SStanislaw Gruszka 
165872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64);
166872e439aSPaul Mackerras 
167c6622f63SPaul Mackerras static void calc_cputime_factors(void)
168c6622f63SPaul Mackerras {
169c6622f63SPaul Mackerras 	struct div_result res;
170c6622f63SPaul Mackerras 
171c6622f63SPaul Mackerras 	div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
172c6622f63SPaul Mackerras 	__cputime_jiffies_factor = res.result_low;
1739f5072d4SAndreas Schwab 	div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
1749f5072d4SAndreas Schwab 	__cputime_usec_factor = res.result_low;
175c6622f63SPaul Mackerras 	div128_by_32(1, 0, tb_ticks_per_sec, &res);
176c6622f63SPaul Mackerras 	__cputime_sec_factor = res.result_low;
177c6622f63SPaul Mackerras 	div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
178c6622f63SPaul Mackerras 	__cputime_clockt_factor = res.result_low;
179c6622f63SPaul Mackerras }
180c6622f63SPaul Mackerras 
181c6622f63SPaul Mackerras /*
182cf9efce0SPaul Mackerras  * Read the SPURR on systems that have it, otherwise the PURR,
183cf9efce0SPaul Mackerras  * or if that doesn't exist return the timebase value passed in.
184c6622f63SPaul Mackerras  */
185cf9efce0SPaul Mackerras static u64 read_spurr(u64 tb)
186c6622f63SPaul Mackerras {
187cf9efce0SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_SPURR))
188cf9efce0SPaul Mackerras 		return mfspr(SPRN_SPURR);
189c6622f63SPaul Mackerras 	if (cpu_has_feature(CPU_FTR_PURR))
190c6622f63SPaul Mackerras 		return mfspr(SPRN_PURR);
191cf9efce0SPaul Mackerras 	return tb;
192cf9efce0SPaul Mackerras }
193cf9efce0SPaul Mackerras 
194cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR
195cf9efce0SPaul Mackerras 
196cf9efce0SPaul Mackerras /*
197cf9efce0SPaul Mackerras  * Scan the dispatch trace log and count up the stolen time.
198cf9efce0SPaul Mackerras  * Should be called with interrupts disabled.
199cf9efce0SPaul Mackerras  */
200cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb)
201cf9efce0SPaul Mackerras {
202872e439aSPaul Mackerras 	u64 i = local_paca->dtl_ridx;
203cf9efce0SPaul Mackerras 	struct dtl_entry *dtl = local_paca->dtl_curr;
204cf9efce0SPaul Mackerras 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
205cf9efce0SPaul Mackerras 	struct lppaca *vpa = local_paca->lppaca_ptr;
206cf9efce0SPaul Mackerras 	u64 tb_delta;
207cf9efce0SPaul Mackerras 	u64 stolen = 0;
208cf9efce0SPaul Mackerras 	u64 dtb;
209cf9efce0SPaul Mackerras 
21084ffae55SAnton Blanchard 	if (!dtl)
21184ffae55SAnton Blanchard 		return 0;
21284ffae55SAnton Blanchard 
213cf9efce0SPaul Mackerras 	if (i == vpa->dtl_idx)
214cf9efce0SPaul Mackerras 		return 0;
215cf9efce0SPaul Mackerras 	while (i < vpa->dtl_idx) {
216872e439aSPaul Mackerras 		if (dtl_consumer)
217872e439aSPaul Mackerras 			dtl_consumer(dtl, i);
218cf9efce0SPaul Mackerras 		dtb = dtl->timebase;
219cf9efce0SPaul Mackerras 		tb_delta = dtl->enqueue_to_dispatch_time +
220cf9efce0SPaul Mackerras 			dtl->ready_to_enqueue_time;
221cf9efce0SPaul Mackerras 		barrier();
222cf9efce0SPaul Mackerras 		if (i + N_DISPATCH_LOG < vpa->dtl_idx) {
223cf9efce0SPaul Mackerras 			/* buffer has overflowed */
224cf9efce0SPaul Mackerras 			i = vpa->dtl_idx - N_DISPATCH_LOG;
225cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
226cf9efce0SPaul Mackerras 			continue;
227cf9efce0SPaul Mackerras 		}
228cf9efce0SPaul Mackerras 		if (dtb > stop_tb)
229cf9efce0SPaul Mackerras 			break;
230cf9efce0SPaul Mackerras 		stolen += tb_delta;
231cf9efce0SPaul Mackerras 		++i;
232cf9efce0SPaul Mackerras 		++dtl;
233cf9efce0SPaul Mackerras 		if (dtl == dtl_end)
234cf9efce0SPaul Mackerras 			dtl = local_paca->dispatch_log;
235cf9efce0SPaul Mackerras 	}
236cf9efce0SPaul Mackerras 	local_paca->dtl_ridx = i;
237cf9efce0SPaul Mackerras 	local_paca->dtl_curr = dtl;
238cf9efce0SPaul Mackerras 	return stolen;
239c6622f63SPaul Mackerras }
240c6622f63SPaul Mackerras 
241c6622f63SPaul Mackerras /*
242cf9efce0SPaul Mackerras  * Accumulate stolen time by scanning the dispatch trace log.
243cf9efce0SPaul Mackerras  * Called on entry from user mode.
2444603ac18SMichael Neuling  */
245cf9efce0SPaul Mackerras void accumulate_stolen_time(void)
2464603ac18SMichael Neuling {
247cf9efce0SPaul Mackerras 	u64 sst, ust;
248cf9efce0SPaul Mackerras 
249b18ae08dSTejun Heo 	u8 save_soft_enabled = local_paca->soft_enabled;
250b18ae08dSTejun Heo 
251b18ae08dSTejun Heo 	/* We are called early in the exception entry, before
252b18ae08dSTejun Heo 	 * soft/hard_enabled are sync'ed to the expected state
253b18ae08dSTejun Heo 	 * for the exception. We are hard disabled but the PACA
254b18ae08dSTejun Heo 	 * needs to reflect that so various debug stuff doesn't
255b18ae08dSTejun Heo 	 * complain
256b18ae08dSTejun Heo 	 */
257b18ae08dSTejun Heo 	local_paca->soft_enabled = 0;
258b18ae08dSTejun Heo 
259b18ae08dSTejun Heo 	sst = scan_dispatch_log(local_paca->starttime_user);
260b18ae08dSTejun Heo 	ust = scan_dispatch_log(local_paca->starttime);
261b18ae08dSTejun Heo 	local_paca->system_time -= sst;
262b18ae08dSTejun Heo 	local_paca->user_time -= ust;
263b18ae08dSTejun Heo 	local_paca->stolen_time += ust + sst;
264b18ae08dSTejun Heo 
265b18ae08dSTejun Heo 	local_paca->soft_enabled = save_soft_enabled;
2664603ac18SMichael Neuling }
2674603ac18SMichael Neuling 
268cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
269cf9efce0SPaul Mackerras {
270cf9efce0SPaul Mackerras 	u64 stolen = 0;
271cf9efce0SPaul Mackerras 
272cf9efce0SPaul Mackerras 	if (get_paca()->dtl_ridx != get_paca()->lppaca_ptr->dtl_idx) {
273cf9efce0SPaul Mackerras 		stolen = scan_dispatch_log(stop_tb);
274cf9efce0SPaul Mackerras 		get_paca()->system_time -= stolen;
275cf9efce0SPaul Mackerras 	}
276cf9efce0SPaul Mackerras 
277cf9efce0SPaul Mackerras 	stolen += get_paca()->stolen_time;
278cf9efce0SPaul Mackerras 	get_paca()->stolen_time = 0;
279cf9efce0SPaul Mackerras 	return stolen;
280cf9efce0SPaul Mackerras }
281cf9efce0SPaul Mackerras 
282cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */
283cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb)
284cf9efce0SPaul Mackerras {
285cf9efce0SPaul Mackerras 	return 0;
286cf9efce0SPaul Mackerras }
287cf9efce0SPaul Mackerras 
288cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */
289cf9efce0SPaul Mackerras 
2904603ac18SMichael Neuling /*
291c6622f63SPaul Mackerras  * Account time for a transition between system, hard irq
292c6622f63SPaul Mackerras  * or soft irq state.
293c6622f63SPaul Mackerras  */
294a7e1a9e3SFrederic Weisbecker static u64 vtime_delta(struct task_struct *tsk,
295a7e1a9e3SFrederic Weisbecker 			u64 *sys_scaled, u64 *stolen)
296c6622f63SPaul Mackerras {
297a7e1a9e3SFrederic Weisbecker 	u64 now, nowscaled, deltascaled;
298a7e1a9e3SFrederic Weisbecker 	u64 udelta, delta, user_scaled;
299c6622f63SPaul Mackerras 
3001b2852b1SFrederic Weisbecker 	WARN_ON_ONCE(!irqs_disabled());
3011b2852b1SFrederic Weisbecker 
302cf9efce0SPaul Mackerras 	now = mftb();
3034603ac18SMichael Neuling 	nowscaled = read_spurr(now);
304cf9efce0SPaul Mackerras 	get_paca()->system_time += now - get_paca()->starttime;
305cf9efce0SPaul Mackerras 	get_paca()->starttime = now;
3064603ac18SMichael Neuling 	deltascaled = nowscaled - get_paca()->startspurr;
3074603ac18SMichael Neuling 	get_paca()->startspurr = nowscaled;
308cf9efce0SPaul Mackerras 
309a7e1a9e3SFrederic Weisbecker 	*stolen = calculate_stolen_time(now);
310cf9efce0SPaul Mackerras 
311cf9efce0SPaul Mackerras 	delta = get_paca()->system_time;
312c6622f63SPaul Mackerras 	get_paca()->system_time = 0;
313cf9efce0SPaul Mackerras 	udelta = get_paca()->user_time - get_paca()->utime_sspurr;
314cf9efce0SPaul Mackerras 	get_paca()->utime_sspurr = get_paca()->user_time;
315cf9efce0SPaul Mackerras 
316cf9efce0SPaul Mackerras 	/*
317cf9efce0SPaul Mackerras 	 * Because we don't read the SPURR on every kernel entry/exit,
318cf9efce0SPaul Mackerras 	 * deltascaled includes both user and system SPURR ticks.
319cf9efce0SPaul Mackerras 	 * Apportion these ticks to system SPURR ticks and user
320cf9efce0SPaul Mackerras 	 * SPURR ticks in the same ratio as the system time (delta)
321cf9efce0SPaul Mackerras 	 * and user time (udelta) values obtained from the timebase
322cf9efce0SPaul Mackerras 	 * over the same interval.  The system ticks get accounted here;
323cf9efce0SPaul Mackerras 	 * the user ticks get saved up in paca->user_time_scaled to be
324cf9efce0SPaul Mackerras 	 * used by account_process_tick.
325cf9efce0SPaul Mackerras 	 */
326a7e1a9e3SFrederic Weisbecker 	*sys_scaled = delta;
327cf9efce0SPaul Mackerras 	user_scaled = udelta;
328cf9efce0SPaul Mackerras 	if (deltascaled != delta + udelta) {
329cf9efce0SPaul Mackerras 		if (udelta) {
330a7e1a9e3SFrederic Weisbecker 			*sys_scaled = deltascaled * delta / (delta + udelta);
331a7e1a9e3SFrederic Weisbecker 			user_scaled = deltascaled - *sys_scaled;
332cf9efce0SPaul Mackerras 		} else {
333a7e1a9e3SFrederic Weisbecker 			*sys_scaled = deltascaled;
334c6622f63SPaul Mackerras 		}
335cf9efce0SPaul Mackerras 	}
336cf9efce0SPaul Mackerras 	get_paca()->user_time_scaled += user_scaled;
337cf9efce0SPaul Mackerras 
338a7e1a9e3SFrederic Weisbecker 	return delta;
339a7e1a9e3SFrederic Weisbecker }
340a7e1a9e3SFrederic Weisbecker 
341fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk)
342a7e1a9e3SFrederic Weisbecker {
343a7e1a9e3SFrederic Weisbecker 	u64 delta, sys_scaled, stolen;
344a7e1a9e3SFrederic Weisbecker 
345a7e1a9e3SFrederic Weisbecker 	delta = vtime_delta(tsk, &sys_scaled, &stolen);
346cf9efce0SPaul Mackerras 	account_system_time(tsk, 0, delta, sys_scaled);
347cf9efce0SPaul Mackerras 	if (stolen)
348cf9efce0SPaul Mackerras 		account_steal_time(stolen);
349a7e1a9e3SFrederic Weisbecker }
350c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system);
351a7e1a9e3SFrederic Weisbecker 
352fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk)
353a7e1a9e3SFrederic Weisbecker {
354a7e1a9e3SFrederic Weisbecker 	u64 delta, sys_scaled, stolen;
355a7e1a9e3SFrederic Weisbecker 
356a7e1a9e3SFrederic Weisbecker 	delta = vtime_delta(tsk, &sys_scaled, &stolen);
357cf9efce0SPaul Mackerras 	account_idle_time(delta + stolen);
358cf9efce0SPaul Mackerras }
359c6622f63SPaul Mackerras 
360c6622f63SPaul Mackerras /*
361bcebdf84SFrederic Weisbecker  * Transfer the user time accumulated in the paca
362bcebdf84SFrederic Weisbecker  * by the exception entry and exit code to the generic
363bcebdf84SFrederic Weisbecker  * process user time records.
364c6622f63SPaul Mackerras  * Must be called with interrupts disabled.
365bcebdf84SFrederic Weisbecker  * Assumes that vtime_account_system/idle() has been called
366bcebdf84SFrederic Weisbecker  * recently (i.e. since the last entry from usermode) so that
367cf9efce0SPaul Mackerras  * get_paca()->user_time_scaled is up to date.
368c6622f63SPaul Mackerras  */
369bcebdf84SFrederic Weisbecker void vtime_account_user(struct task_struct *tsk)
370c6622f63SPaul Mackerras {
3714603ac18SMichael Neuling 	cputime_t utime, utimescaled;
372c6622f63SPaul Mackerras 
373c6622f63SPaul Mackerras 	utime = get_paca()->user_time;
374cf9efce0SPaul Mackerras 	utimescaled = get_paca()->user_time_scaled;
375c6622f63SPaul Mackerras 	get_paca()->user_time = 0;
376cf9efce0SPaul Mackerras 	get_paca()->user_time_scaled = 0;
377cf9efce0SPaul Mackerras 	get_paca()->utime_sspurr = 0;
378457533a7SMartin Schwidefsky 	account_user_time(tsk, utime, utimescaled);
379c6622f63SPaul Mackerras }
380c6622f63SPaul Mackerras 
381abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
382c6622f63SPaul Mackerras #define calc_cputime_factors()
383c6622f63SPaul Mackerras #endif
384c6622f63SPaul Mackerras 
3856defa38bSPaul Mackerras void __delay(unsigned long loops)
3866defa38bSPaul Mackerras {
3876defa38bSPaul Mackerras 	unsigned long start;
3886defa38bSPaul Mackerras 	int diff;
3896defa38bSPaul Mackerras 
3906defa38bSPaul Mackerras 	if (__USE_RTC()) {
3916defa38bSPaul Mackerras 		start = get_rtcl();
3926defa38bSPaul Mackerras 		do {
3936defa38bSPaul Mackerras 			/* the RTCL register wraps at 1000000000 */
3946defa38bSPaul Mackerras 			diff = get_rtcl() - start;
3956defa38bSPaul Mackerras 			if (diff < 0)
3966defa38bSPaul Mackerras 				diff += 1000000000;
3976defa38bSPaul Mackerras 		} while (diff < loops);
3986defa38bSPaul Mackerras 	} else {
3996defa38bSPaul Mackerras 		start = get_tbl();
4006defa38bSPaul Mackerras 		while (get_tbl() - start < loops)
4016defa38bSPaul Mackerras 			HMT_low();
4026defa38bSPaul Mackerras 		HMT_medium();
4036defa38bSPaul Mackerras 	}
4046defa38bSPaul Mackerras }
4056defa38bSPaul Mackerras EXPORT_SYMBOL(__delay);
4066defa38bSPaul Mackerras 
4076defa38bSPaul Mackerras void udelay(unsigned long usecs)
4086defa38bSPaul Mackerras {
4096defa38bSPaul Mackerras 	__delay(tb_ticks_per_usec * usecs);
4106defa38bSPaul Mackerras }
4116defa38bSPaul Mackerras EXPORT_SYMBOL(udelay);
4126defa38bSPaul Mackerras 
413f2783c15SPaul Mackerras #ifdef CONFIG_SMP
414f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs)
415f2783c15SPaul Mackerras {
416f2783c15SPaul Mackerras 	unsigned long pc = instruction_pointer(regs);
417f2783c15SPaul Mackerras 
418f2783c15SPaul Mackerras 	if (in_lock_functions(pc))
419f2783c15SPaul Mackerras 		return regs->link;
420f2783c15SPaul Mackerras 
421f2783c15SPaul Mackerras 	return pc;
422f2783c15SPaul Mackerras }
423f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc);
424f2783c15SPaul Mackerras #endif
425f2783c15SPaul Mackerras 
426e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK
427105988c0SPaul Mackerras 
4280fe1ac48SPaul Mackerras /*
4290fe1ac48SPaul Mackerras  * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
4300fe1ac48SPaul Mackerras  */
4310fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64
432e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void)
433105988c0SPaul Mackerras {
4340fe1ac48SPaul Mackerras 	unsigned long x;
4350fe1ac48SPaul Mackerras 
4360fe1ac48SPaul Mackerras 	asm volatile("lbz %0,%1(13)"
4370fe1ac48SPaul Mackerras 		: "=r" (x)
438e360adbeSPeter Zijlstra 		: "i" (offsetof(struct paca_struct, irq_work_pending)));
4390fe1ac48SPaul Mackerras 	return x;
440105988c0SPaul Mackerras }
441105988c0SPaul Mackerras 
442e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void)
4430fe1ac48SPaul Mackerras {
4440fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4450fe1ac48SPaul Mackerras 		"r" (1),
446e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4470fe1ac48SPaul Mackerras }
4480fe1ac48SPaul Mackerras 
449e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void)
4500fe1ac48SPaul Mackerras {
4510fe1ac48SPaul Mackerras 	asm volatile("stb %0,%1(13)" : :
4520fe1ac48SPaul Mackerras 		"r" (0),
453e360adbeSPeter Zijlstra 		"i" (offsetof(struct paca_struct, irq_work_pending)));
4540fe1ac48SPaul Mackerras }
4550fe1ac48SPaul Mackerras 
4560fe1ac48SPaul Mackerras #else /* 32-bit */
4570fe1ac48SPaul Mackerras 
458e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending);
4590fe1ac48SPaul Mackerras 
460e360adbeSPeter Zijlstra #define set_irq_work_pending_flag()	__get_cpu_var(irq_work_pending) = 1
461e360adbeSPeter Zijlstra #define test_irq_work_pending()		__get_cpu_var(irq_work_pending)
462e360adbeSPeter Zijlstra #define clear_irq_work_pending()	__get_cpu_var(irq_work_pending) = 0
463105988c0SPaul Mackerras 
4640fe1ac48SPaul Mackerras #endif /* 32 vs 64 bit */
4650fe1ac48SPaul Mackerras 
4664f8b50bbSPeter Zijlstra void arch_irq_work_raise(void)
4670fe1ac48SPaul Mackerras {
4680fe1ac48SPaul Mackerras 	preempt_disable();
469e360adbeSPeter Zijlstra 	set_irq_work_pending_flag();
4700fe1ac48SPaul Mackerras 	set_dec(1);
4710fe1ac48SPaul Mackerras 	preempt_enable();
4720fe1ac48SPaul Mackerras }
4730fe1ac48SPaul Mackerras 
474e360adbeSPeter Zijlstra #else  /* CONFIG_IRQ_WORK */
475105988c0SPaul Mackerras 
476e360adbeSPeter Zijlstra #define test_irq_work_pending()	0
477e360adbeSPeter Zijlstra #define clear_irq_work_pending()
478105988c0SPaul Mackerras 
479e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */
480105988c0SPaul Mackerras 
481f2783c15SPaul Mackerras /*
482f2783c15SPaul Mackerras  * timer_interrupt - gets called when the decrementer overflows,
483f2783c15SPaul Mackerras  * with interrupts disabled.
484f2783c15SPaul Mackerras  */
485f2783c15SPaul Mackerras void timer_interrupt(struct pt_regs * regs)
486f2783c15SPaul Mackerras {
4877d12e780SDavid Howells 	struct pt_regs *old_regs;
4887df10275SAnton Blanchard 	u64 *next_tb = &__get_cpu_var(decrementers_next_tb);
4897df10275SAnton Blanchard 	struct clock_event_device *evt = &__get_cpu_var(decrementers);
490860aed25SPaul Mackerras 	u64 now;
491d831d0b8STony Breeds 
492963e5d3bSBenjamin Herrenschmidt 	/* Ensure a positive value is written to the decrementer, or else
493963e5d3bSBenjamin Herrenschmidt 	 * some CPUs will continue to take decrementer exceptions.
494963e5d3bSBenjamin Herrenschmidt 	 */
495963e5d3bSBenjamin Herrenschmidt 	set_dec(DECREMENTER_MAX);
496963e5d3bSBenjamin Herrenschmidt 
497963e5d3bSBenjamin Herrenschmidt 	/* Some implementations of hotplug will get timer interrupts while
498689dfa89STiejun Chen 	 * offline, just ignore these and we also need to set
499689dfa89STiejun Chen 	 * decrementers_next_tb as MAX to make sure __check_irq_replay
500689dfa89STiejun Chen 	 * don't replay timer interrupt when return, otherwise we'll trap
501689dfa89STiejun Chen 	 * here infinitely :(
502963e5d3bSBenjamin Herrenschmidt 	 */
503689dfa89STiejun Chen 	if (!cpu_online(smp_processor_id())) {
504689dfa89STiejun Chen 		*next_tb = ~(u64)0;
505963e5d3bSBenjamin Herrenschmidt 		return;
506689dfa89STiejun Chen 	}
507963e5d3bSBenjamin Herrenschmidt 
5087230c564SBenjamin Herrenschmidt 	/* Conditionally hard-enable interrupts now that the DEC has been
5097230c564SBenjamin Herrenschmidt 	 * bumped to its maximum value
5107230c564SBenjamin Herrenschmidt 	 */
5117230c564SBenjamin Herrenschmidt 	may_hard_irq_enable();
5127230c564SBenjamin Herrenschmidt 
51389713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).timer_irqs++;
51489713ed1SAnton Blanchard 
515b0d278b7SPaul Mackerras #if defined(CONFIG_PPC32) && defined(CONFIG_PMAC)
516f2783c15SPaul Mackerras 	if (atomic_read(&ppc_n_lost_interrupts) != 0)
517f2783c15SPaul Mackerras 		do_IRQ(regs);
518f2783c15SPaul Mackerras #endif
519f2783c15SPaul Mackerras 
5207d12e780SDavid Howells 	old_regs = set_irq_regs(regs);
521f2783c15SPaul Mackerras 	irq_enter();
522f2783c15SPaul Mackerras 
523e72bbbabSLi Zhong 	trace_timer_interrupt_entry(regs);
524e72bbbabSLi Zhong 
525e360adbeSPeter Zijlstra 	if (test_irq_work_pending()) {
526e360adbeSPeter Zijlstra 		clear_irq_work_pending();
527e360adbeSPeter Zijlstra 		irq_work_run();
5280fe1ac48SPaul Mackerras 	}
5290fe1ac48SPaul Mackerras 
530860aed25SPaul Mackerras 	now = get_tb_or_rtc();
531860aed25SPaul Mackerras 	if (now >= *next_tb) {
5327df10275SAnton Blanchard 		*next_tb = ~(u64)0;
533d831d0b8STony Breeds 		if (evt->event_handler)
534d831d0b8STony Breeds 			evt->event_handler(evt);
535860aed25SPaul Mackerras 	} else {
536860aed25SPaul Mackerras 		now = *next_tb - now;
537860aed25SPaul Mackerras 		if (now <= DECREMENTER_MAX)
538860aed25SPaul Mackerras 			set_dec((int)now);
539860aed25SPaul Mackerras 	}
540f2783c15SPaul Mackerras 
541f2783c15SPaul Mackerras #ifdef CONFIG_PPC64
542f2783c15SPaul Mackerras 	/* collect purr register values often, for accurate calculations */
543f2783c15SPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
544f2783c15SPaul Mackerras 		struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
545f2783c15SPaul Mackerras 		cu->current_tb = mfspr(SPRN_PURR);
546f2783c15SPaul Mackerras 	}
547f2783c15SPaul Mackerras #endif
548f2783c15SPaul Mackerras 
549e72bbbabSLi Zhong 	trace_timer_interrupt_exit(regs);
550e72bbbabSLi Zhong 
551f2783c15SPaul Mackerras 	irq_exit();
5527d12e780SDavid Howells 	set_irq_regs(old_regs);
553f2783c15SPaul Mackerras }
554f2783c15SPaul Mackerras 
555dabe859eSPaul Mackerras /*
556dabe859eSPaul Mackerras  * Hypervisor decrementer interrupts shouldn't occur but are sometimes
557dabe859eSPaul Mackerras  * left pending on exit from a KVM guest.  We don't need to do anything
558dabe859eSPaul Mackerras  * to clear them, as they are edge-triggered.
559dabe859eSPaul Mackerras  */
560dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs)
561dabe859eSPaul Mackerras {
562dabe859eSPaul Mackerras }
563dabe859eSPaul Mackerras 
5647ac5dde9SScott Wood #ifdef CONFIG_SUSPEND
565d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void)
5667ac5dde9SScott Wood {
5677ac5dde9SScott Wood 	/* Disable the decrementer, so that it doesn't interfere
5687ac5dde9SScott Wood 	 * with suspending.
5697ac5dde9SScott Wood 	 */
5707ac5dde9SScott Wood 
571621692cbSAnton Blanchard 	set_dec(DECREMENTER_MAX);
5727ac5dde9SScott Wood 	local_irq_disable();
573621692cbSAnton Blanchard 	set_dec(DECREMENTER_MAX);
5747ac5dde9SScott Wood }
5757ac5dde9SScott Wood 
576d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void)
5777ac5dde9SScott Wood {
5787ac5dde9SScott Wood 	local_irq_enable();
5797ac5dde9SScott Wood }
5807ac5dde9SScott Wood 
5817ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
5827ac5dde9SScott Wood void arch_suspend_disable_irqs(void)
5837ac5dde9SScott Wood {
5847ac5dde9SScott Wood 	if (ppc_md.suspend_disable_irqs)
5857ac5dde9SScott Wood 		ppc_md.suspend_disable_irqs();
5867ac5dde9SScott Wood 	generic_suspend_disable_irqs();
5877ac5dde9SScott Wood }
5887ac5dde9SScott Wood 
5897ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */
5907ac5dde9SScott Wood void arch_suspend_enable_irqs(void)
5917ac5dde9SScott Wood {
5927ac5dde9SScott Wood 	generic_suspend_enable_irqs();
5937ac5dde9SScott Wood 	if (ppc_md.suspend_enable_irqs)
5947ac5dde9SScott Wood 		ppc_md.suspend_enable_irqs();
5957ac5dde9SScott Wood }
5967ac5dde9SScott Wood #endif
5977ac5dde9SScott Wood 
598f2783c15SPaul Mackerras /*
599f2783c15SPaul Mackerras  * Scheduler clock - returns current time in nanosec units.
600f2783c15SPaul Mackerras  *
601f2783c15SPaul Mackerras  * Note: mulhdu(a, b) (multiply high double unsigned) returns
602f2783c15SPaul Mackerras  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
603f2783c15SPaul Mackerras  * are 64-bit unsigned numbers.
604f2783c15SPaul Mackerras  */
605f2783c15SPaul Mackerras unsigned long long sched_clock(void)
606f2783c15SPaul Mackerras {
60796c44507SPaul Mackerras 	if (__USE_RTC())
60896c44507SPaul Mackerras 		return get_rtc();
609fc9069feSTony Breeds 	return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
610f2783c15SPaul Mackerras }
611f2783c15SPaul Mackerras 
6120bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val)
613f2783c15SPaul Mackerras {
614f2783c15SPaul Mackerras 	struct device_node *cpu;
615*6f7aba7bSAnton Blanchard 	const __be32 *fp;
6160bb474a4SAnton Blanchard 	int found = 0;
617f2783c15SPaul Mackerras 
6180bb474a4SAnton Blanchard 	/* The cpu node should have timebase and clock frequency properties */
619f2783c15SPaul Mackerras 	cpu = of_find_node_by_type(NULL, "cpu");
620f2783c15SPaul Mackerras 
621d8a8188dSOlaf Hering 	if (cpu) {
622e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
623d8a8188dSOlaf Hering 		if (fp) {
6240bb474a4SAnton Blanchard 			found = 1;
625a4dc7ff0SPaul Mackerras 			*val = of_read_ulong(fp, cells);
626f2783c15SPaul Mackerras 		}
6270bb474a4SAnton Blanchard 
6280bb474a4SAnton Blanchard 		of_node_put(cpu);
629f2783c15SPaul Mackerras 	}
6300bb474a4SAnton Blanchard 
6310bb474a4SAnton Blanchard 	return found;
6320bb474a4SAnton Blanchard }
6330bb474a4SAnton Blanchard 
63477c0a700SBenjamin Herrenschmidt void start_cpu_decrementer(void)
63577c0a700SBenjamin Herrenschmidt {
63677c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
63777c0a700SBenjamin Herrenschmidt 	/* Clear any pending timer interrupts */
63877c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
63977c0a700SBenjamin Herrenschmidt 
64077c0a700SBenjamin Herrenschmidt 	/* Enable decrementer interrupt */
64177c0a700SBenjamin Herrenschmidt 	mtspr(SPRN_TCR, TCR_DIE);
64277c0a700SBenjamin Herrenschmidt #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
64377c0a700SBenjamin Herrenschmidt }
64477c0a700SBenjamin Herrenschmidt 
6450bb474a4SAnton Blanchard void __init generic_calibrate_decr(void)
6460bb474a4SAnton Blanchard {
6470bb474a4SAnton Blanchard 	ppc_tb_freq = DEFAULT_TB_FREQ;		/* hardcoded default */
6480bb474a4SAnton Blanchard 
6490bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
6500bb474a4SAnton Blanchard 	    !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
6510bb474a4SAnton Blanchard 
652f2783c15SPaul Mackerras 		printk(KERN_ERR "WARNING: Estimating decrementer frequency "
653f2783c15SPaul Mackerras 				"(not found)\n");
6540bb474a4SAnton Blanchard 	}
655f2783c15SPaul Mackerras 
6560bb474a4SAnton Blanchard 	ppc_proc_freq = DEFAULT_PROC_FREQ;	/* hardcoded default */
6570bb474a4SAnton Blanchard 
6580bb474a4SAnton Blanchard 	if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
6590bb474a4SAnton Blanchard 	    !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
6600bb474a4SAnton Blanchard 
6610bb474a4SAnton Blanchard 		printk(KERN_ERR "WARNING: Estimating processor frequency "
6620bb474a4SAnton Blanchard 				"(not found)\n");
663f2783c15SPaul Mackerras 	}
664f2783c15SPaul Mackerras }
665f2783c15SPaul Mackerras 
666aa3be5f3STony Breeds int update_persistent_clock(struct timespec now)
667f2783c15SPaul Mackerras {
668f2783c15SPaul Mackerras 	struct rtc_time tm;
669f2783c15SPaul Mackerras 
670aa3be5f3STony Breeds 	if (!ppc_md.set_rtc_time)
671023f333aSJason Gunthorpe 		return -ENODEV;
672aa3be5f3STony Breeds 
673aa3be5f3STony Breeds 	to_tm(now.tv_sec + 1 + timezone_offset, &tm);
674aa3be5f3STony Breeds 	tm.tm_year -= 1900;
675aa3be5f3STony Breeds 	tm.tm_mon -= 1;
676aa3be5f3STony Breeds 
677aa3be5f3STony Breeds 	return ppc_md.set_rtc_time(&tm);
678aa3be5f3STony Breeds }
679aa3be5f3STony Breeds 
680978d7eb3SBenjamin Herrenschmidt static void __read_persistent_clock(struct timespec *ts)
681aa3be5f3STony Breeds {
682aa3be5f3STony Breeds 	struct rtc_time tm;
683aa3be5f3STony Breeds 	static int first = 1;
684aa3be5f3STony Breeds 
685d90246cdSMartin Schwidefsky 	ts->tv_nsec = 0;
686aa3be5f3STony Breeds 	/* XXX this is a litle fragile but will work okay in the short term */
687aa3be5f3STony Breeds 	if (first) {
688aa3be5f3STony Breeds 		first = 0;
689aa3be5f3STony Breeds 		if (ppc_md.time_init)
690aa3be5f3STony Breeds 			timezone_offset = ppc_md.time_init();
691aa3be5f3STony Breeds 
692aa3be5f3STony Breeds 		/* get_boot_time() isn't guaranteed to be safe to call late */
693d90246cdSMartin Schwidefsky 		if (ppc_md.get_boot_time) {
694d90246cdSMartin Schwidefsky 			ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
695d90246cdSMartin Schwidefsky 			return;
696aa3be5f3STony Breeds 		}
697d90246cdSMartin Schwidefsky 	}
698d90246cdSMartin Schwidefsky 	if (!ppc_md.get_rtc_time) {
699d90246cdSMartin Schwidefsky 		ts->tv_sec = 0;
700d90246cdSMartin Schwidefsky 		return;
701d90246cdSMartin Schwidefsky 	}
702f2783c15SPaul Mackerras 	ppc_md.get_rtc_time(&tm);
703978d7eb3SBenjamin Herrenschmidt 
704d4f587c6SMartin Schwidefsky 	ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
705f2783c15SPaul Mackerras 			    tm.tm_hour, tm.tm_min, tm.tm_sec);
706f2783c15SPaul Mackerras }
707f2783c15SPaul Mackerras 
708978d7eb3SBenjamin Herrenschmidt void read_persistent_clock(struct timespec *ts)
709978d7eb3SBenjamin Herrenschmidt {
710978d7eb3SBenjamin Herrenschmidt 	__read_persistent_clock(ts);
711978d7eb3SBenjamin Herrenschmidt 
712978d7eb3SBenjamin Herrenschmidt 	/* Sanitize it in case real time clock is set below EPOCH */
713978d7eb3SBenjamin Herrenschmidt 	if (ts->tv_sec < 0) {
714978d7eb3SBenjamin Herrenschmidt 		ts->tv_sec = 0;
715978d7eb3SBenjamin Herrenschmidt 		ts->tv_nsec = 0;
716978d7eb3SBenjamin Herrenschmidt 	}
717978d7eb3SBenjamin Herrenschmidt 
718978d7eb3SBenjamin Herrenschmidt }
719978d7eb3SBenjamin Herrenschmidt 
7204a4cfe38STony Breeds /* clocksource code */
7218e19608eSMagnus Damm static cycle_t rtc_read(struct clocksource *cs)
7224a4cfe38STony Breeds {
7234a4cfe38STony Breeds 	return (cycle_t)get_rtc();
7244a4cfe38STony Breeds }
7254a4cfe38STony Breeds 
7268e19608eSMagnus Damm static cycle_t timebase_read(struct clocksource *cs)
7274a4cfe38STony Breeds {
7284a4cfe38STony Breeds 	return (cycle_t)get_tb();
7294a4cfe38STony Breeds }
7304a4cfe38STony Breeds 
73170639421SJohn Stultz void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm,
7327615856eSJohn Stultz 			struct clocksource *clock, u32 mult)
7334a4cfe38STony Breeds {
734b0797b60SJohn Stultz 	u64 new_tb_to_xs, new_stamp_xsec;
7350e469db8SPaul Mackerras 	u32 frac_sec;
7364a4cfe38STony Breeds 
7374a4cfe38STony Breeds 	if (clock != &clocksource_timebase)
7384a4cfe38STony Breeds 		return;
7394a4cfe38STony Breeds 
7404a4cfe38STony Breeds 	/* Make userspace gettimeofday spin until we're done. */
7414a4cfe38STony Breeds 	++vdso_data->tb_update_count;
7424a4cfe38STony Breeds 	smp_mb();
7434a4cfe38STony Breeds 
74411b8633aSAnton Blanchard 	/* 19342813113834067 ~= 2^(20+64) / 1e9 */
74511b8633aSAnton Blanchard 	new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift);
74606d518e3SJohn Stultz 	new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC;
747b0797b60SJohn Stultz 	do_div(new_stamp_xsec, 1000000000);
74806d518e3SJohn Stultz 	new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC;
749b0797b60SJohn Stultz 
7500e469db8SPaul Mackerras 	BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC);
7510e469db8SPaul Mackerras 	/* this is tv_nsec / 1e9 as a 0.32 fraction */
7520e469db8SPaul Mackerras 	frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32;
75347916be4SThomas Gleixner 
754b0797b60SJohn Stultz 	/*
755b0797b60SJohn Stultz 	 * tb_update_count is used to allow the userspace gettimeofday code
756b0797b60SJohn Stultz 	 * to assure itself that it sees a consistent view of the tb_to_xs and
757b0797b60SJohn Stultz 	 * stamp_xsec variables.  It reads the tb_update_count, then reads
758b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
759b0797b60SJohn Stultz 	 * the two values of tb_update_count match and are even then the
760b0797b60SJohn Stultz 	 * tb_to_xs and stamp_xsec values are consistent.  If not, then it
761b0797b60SJohn Stultz 	 * loops back and reads them again until this criteria is met.
762b0797b60SJohn Stultz 	 * We expect the caller to have done the first increment of
763b0797b60SJohn Stultz 	 * vdso_data->tb_update_count already.
764b0797b60SJohn Stultz 	 */
765b0797b60SJohn Stultz 	vdso_data->tb_orig_stamp = clock->cycle_last;
766b0797b60SJohn Stultz 	vdso_data->stamp_xsec = new_stamp_xsec;
767b0797b60SJohn Stultz 	vdso_data->tb_to_xs = new_tb_to_xs;
7687615856eSJohn Stultz 	vdso_data->wtom_clock_sec = wtm->tv_sec;
7697615856eSJohn Stultz 	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
77006d518e3SJohn Stultz 	vdso_data->stamp_xtime = *wall_time;
77147916be4SThomas Gleixner 	vdso_data->stamp_sec_fraction = frac_sec;
772b0797b60SJohn Stultz 	smp_wmb();
773b0797b60SJohn Stultz 	++(vdso_data->tb_update_count);
7744a4cfe38STony Breeds }
7754a4cfe38STony Breeds 
7764a4cfe38STony Breeds void update_vsyscall_tz(void)
7774a4cfe38STony Breeds {
7784a4cfe38STony Breeds 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
7794a4cfe38STony Breeds 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
7804a4cfe38STony Breeds }
7814a4cfe38STony Breeds 
7821c21a293SMichael Ellerman static void __init clocksource_init(void)
7834a4cfe38STony Breeds {
7844a4cfe38STony Breeds 	struct clocksource *clock;
7854a4cfe38STony Breeds 
7864a4cfe38STony Breeds 	if (__USE_RTC())
7874a4cfe38STony Breeds 		clock = &clocksource_rtc;
7884a4cfe38STony Breeds 	else
7894a4cfe38STony Breeds 		clock = &clocksource_timebase;
7904a4cfe38STony Breeds 
79111b8633aSAnton Blanchard 	if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
7924a4cfe38STony Breeds 		printk(KERN_ERR "clocksource: %s is already registered\n",
7934a4cfe38STony Breeds 		       clock->name);
7944a4cfe38STony Breeds 		return;
7954a4cfe38STony Breeds 	}
7964a4cfe38STony Breeds 
7974a4cfe38STony Breeds 	printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
7984a4cfe38STony Breeds 	       clock->name, clock->mult, clock->shift);
7994a4cfe38STony Breeds }
8004a4cfe38STony Breeds 
801d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt,
802d831d0b8STony Breeds 				      struct clock_event_device *dev)
803d831d0b8STony Breeds {
8047df10275SAnton Blanchard 	__get_cpu_var(decrementers_next_tb) = get_tb_or_rtc() + evt;
805d831d0b8STony Breeds 	set_dec(evt);
806d831d0b8STony Breeds 	return 0;
807d831d0b8STony Breeds }
808d831d0b8STony Breeds 
809d831d0b8STony Breeds static void decrementer_set_mode(enum clock_event_mode mode,
810d831d0b8STony Breeds 				 struct clock_event_device *dev)
811d831d0b8STony Breeds {
812d831d0b8STony Breeds 	if (mode != CLOCK_EVT_MODE_ONESHOT)
813d831d0b8STony Breeds 		decrementer_set_next_event(DECREMENTER_MAX, dev);
814d831d0b8STony Breeds }
815d831d0b8STony Breeds 
816d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu)
817d831d0b8STony Breeds {
8187df10275SAnton Blanchard 	struct clock_event_device *dec = &per_cpu(decrementers, cpu);
819d831d0b8STony Breeds 
820d831d0b8STony Breeds 	*dec = decrementer_clockevent;
821320ab2b0SRusty Russell 	dec->cpumask = cpumask_of(cpu);
822d831d0b8STony Breeds 
823b919ee82SAnton Blanchard 	printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
824d831d0b8STony Breeds 		    dec->name, dec->mult, dec->shift, cpu);
825d831d0b8STony Breeds 
826d831d0b8STony Breeds 	clockevents_register_device(dec);
827d831d0b8STony Breeds }
828d831d0b8STony Breeds 
829c481887fSMilton Miller static void __init init_decrementer_clockevent(void)
830d831d0b8STony Breeds {
831d831d0b8STony Breeds 	int cpu = smp_processor_id();
832d831d0b8STony Breeds 
833d8afc6fdSAnton Blanchard 	clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4);
834d8afc6fdSAnton Blanchard 
835d831d0b8STony Breeds 	decrementer_clockevent.max_delta_ns =
836d831d0b8STony Breeds 		clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent);
83743875cc0SPaul Mackerras 	decrementer_clockevent.min_delta_ns =
83843875cc0SPaul Mackerras 		clockevent_delta2ns(2, &decrementer_clockevent);
839d831d0b8STony Breeds 
840d831d0b8STony Breeds 	register_decrementer_clockevent(cpu);
841d831d0b8STony Breeds }
842d831d0b8STony Breeds 
843d831d0b8STony Breeds void secondary_cpu_time_init(void)
844d831d0b8STony Breeds {
84577c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
84677c0a700SBenjamin Herrenschmidt 	 * such as BookE
84777c0a700SBenjamin Herrenschmidt 	 */
84877c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
84977c0a700SBenjamin Herrenschmidt 
850d831d0b8STony Breeds 	/* FIME: Should make unrelatred change to move snapshot_timebase
851d831d0b8STony Breeds 	 * call here ! */
852d831d0b8STony Breeds 	register_decrementer_clockevent(smp_processor_id());
853d831d0b8STony Breeds }
854d831d0b8STony Breeds 
855f2783c15SPaul Mackerras /* This function is only called on the boot processor */
856f2783c15SPaul Mackerras void __init time_init(void)
857f2783c15SPaul Mackerras {
858f2783c15SPaul Mackerras 	struct div_result res;
859d75d68cfSPaul Mackerras 	u64 scale;
860f2783c15SPaul Mackerras 	unsigned shift;
861f2783c15SPaul Mackerras 
86296c44507SPaul Mackerras 	if (__USE_RTC()) {
86396c44507SPaul Mackerras 		/* 601 processor: dec counts down by 128 every 128ns */
86496c44507SPaul Mackerras 		ppc_tb_freq = 1000000000;
86596c44507SPaul Mackerras 	} else {
86696c44507SPaul Mackerras 		/* Normal PowerPC with timebase register */
867f2783c15SPaul Mackerras 		ppc_md.calibrate_decr();
868224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
869374e99d4SPaul Mackerras 		       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
870224ad80aSOlof Johansson 		printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
871374e99d4SPaul Mackerras 		       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
87296c44507SPaul Mackerras 	}
873374e99d4SPaul Mackerras 
874374e99d4SPaul Mackerras 	tb_ticks_per_jiffy = ppc_tb_freq / HZ;
875092b8f34SPaul Mackerras 	tb_ticks_per_sec = ppc_tb_freq;
876374e99d4SPaul Mackerras 	tb_ticks_per_usec = ppc_tb_freq / 1000000;
877c6622f63SPaul Mackerras 	calc_cputime_factors();
878a42548a1SStanislaw Gruszka 	setup_cputime_one_jiffy();
879092b8f34SPaul Mackerras 
880092b8f34SPaul Mackerras 	/*
881f2783c15SPaul Mackerras 	 * Compute scale factor for sched_clock.
882f2783c15SPaul Mackerras 	 * The calibrate_decr() function has set tb_ticks_per_sec,
883f2783c15SPaul Mackerras 	 * which is the timebase frequency.
884f2783c15SPaul Mackerras 	 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
885f2783c15SPaul Mackerras 	 * the 128-bit result as a 64.64 fixed-point number.
886f2783c15SPaul Mackerras 	 * We then shift that number right until it is less than 1.0,
887f2783c15SPaul Mackerras 	 * giving us the scale factor and shift count to use in
888f2783c15SPaul Mackerras 	 * sched_clock().
889f2783c15SPaul Mackerras 	 */
890f2783c15SPaul Mackerras 	div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
891f2783c15SPaul Mackerras 	scale = res.result_low;
892f2783c15SPaul Mackerras 	for (shift = 0; res.result_high != 0; ++shift) {
893f2783c15SPaul Mackerras 		scale = (scale >> 1) | (res.result_high << 63);
894f2783c15SPaul Mackerras 		res.result_high >>= 1;
895f2783c15SPaul Mackerras 	}
896f2783c15SPaul Mackerras 	tb_to_ns_scale = scale;
897f2783c15SPaul Mackerras 	tb_to_ns_shift = shift;
898fc9069feSTony Breeds 	/* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
899c27da339SBenjamin Herrenschmidt 	boot_tb = get_tb_or_rtc();
900f2783c15SPaul Mackerras 
901092b8f34SPaul Mackerras 	/* If platform provided a timezone (pmac), we correct the time */
902092b8f34SPaul Mackerras 	if (timezone_offset) {
903092b8f34SPaul Mackerras 		sys_tz.tz_minuteswest = -timezone_offset / 60;
904092b8f34SPaul Mackerras 		sys_tz.tz_dsttime = 0;
905092b8f34SPaul Mackerras 	}
906092b8f34SPaul Mackerras 
907a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_update_count = 0;
908a7f290daSBenjamin Herrenschmidt 	vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
909f2783c15SPaul Mackerras 
91077c0a700SBenjamin Herrenschmidt 	/* Start the decrementer on CPUs that have manual control
91177c0a700SBenjamin Herrenschmidt 	 * such as BookE
91277c0a700SBenjamin Herrenschmidt 	 */
91377c0a700SBenjamin Herrenschmidt 	start_cpu_decrementer();
91477c0a700SBenjamin Herrenschmidt 
915f5339277SStephen Rothwell 	/* Register the clocksource */
9164a4cfe38STony Breeds 	clocksource_init();
9174a4cfe38STony Breeds 
918d831d0b8STony Breeds 	init_decrementer_clockevent();
919f2783c15SPaul Mackerras }
920f2783c15SPaul Mackerras 
921f2783c15SPaul Mackerras 
922f2783c15SPaul Mackerras #define FEBRUARY	2
923f2783c15SPaul Mackerras #define	STARTOFTIME	1970
924f2783c15SPaul Mackerras #define SECDAY		86400L
925f2783c15SPaul Mackerras #define SECYR		(SECDAY * 365)
926f2783c15SPaul Mackerras #define	leapyear(year)		((year) % 4 == 0 && \
927f2783c15SPaul Mackerras 				 ((year) % 100 != 0 || (year) % 400 == 0))
928f2783c15SPaul Mackerras #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
929f2783c15SPaul Mackerras #define	days_in_month(a) 	(month_days[(a) - 1])
930f2783c15SPaul Mackerras 
931f2783c15SPaul Mackerras static int month_days[12] = {
932f2783c15SPaul Mackerras 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
933f2783c15SPaul Mackerras };
934f2783c15SPaul Mackerras 
935f2783c15SPaul Mackerras /*
936f2783c15SPaul Mackerras  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
937f2783c15SPaul Mackerras  */
938f2783c15SPaul Mackerras void GregorianDay(struct rtc_time * tm)
939f2783c15SPaul Mackerras {
940f2783c15SPaul Mackerras 	int leapsToDate;
941f2783c15SPaul Mackerras 	int lastYear;
942f2783c15SPaul Mackerras 	int day;
943f2783c15SPaul Mackerras 	int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
944f2783c15SPaul Mackerras 
945f2783c15SPaul Mackerras 	lastYear = tm->tm_year - 1;
946f2783c15SPaul Mackerras 
947f2783c15SPaul Mackerras 	/*
948f2783c15SPaul Mackerras 	 * Number of leap corrections to apply up to end of last year
949f2783c15SPaul Mackerras 	 */
950f2783c15SPaul Mackerras 	leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
951f2783c15SPaul Mackerras 
952f2783c15SPaul Mackerras 	/*
953f2783c15SPaul Mackerras 	 * This year is a leap year if it is divisible by 4 except when it is
954f2783c15SPaul Mackerras 	 * divisible by 100 unless it is divisible by 400
955f2783c15SPaul Mackerras 	 *
956f2783c15SPaul Mackerras 	 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
957f2783c15SPaul Mackerras 	 */
958f2783c15SPaul Mackerras 	day = tm->tm_mon > 2 && leapyear(tm->tm_year);
959f2783c15SPaul Mackerras 
960f2783c15SPaul Mackerras 	day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
961f2783c15SPaul Mackerras 		   tm->tm_mday;
962f2783c15SPaul Mackerras 
963f2783c15SPaul Mackerras 	tm->tm_wday = day % 7;
964f2783c15SPaul Mackerras }
965f2783c15SPaul Mackerras 
966f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm)
967f2783c15SPaul Mackerras {
968f2783c15SPaul Mackerras 	register int    i;
969f2783c15SPaul Mackerras 	register long   hms, day;
970f2783c15SPaul Mackerras 
971f2783c15SPaul Mackerras 	day = tim / SECDAY;
972f2783c15SPaul Mackerras 	hms = tim % SECDAY;
973f2783c15SPaul Mackerras 
974f2783c15SPaul Mackerras 	/* Hours, minutes, seconds are easy */
975f2783c15SPaul Mackerras 	tm->tm_hour = hms / 3600;
976f2783c15SPaul Mackerras 	tm->tm_min = (hms % 3600) / 60;
977f2783c15SPaul Mackerras 	tm->tm_sec = (hms % 3600) % 60;
978f2783c15SPaul Mackerras 
979f2783c15SPaul Mackerras 	/* Number of years in days */
980f2783c15SPaul Mackerras 	for (i = STARTOFTIME; day >= days_in_year(i); i++)
981f2783c15SPaul Mackerras 		day -= days_in_year(i);
982f2783c15SPaul Mackerras 	tm->tm_year = i;
983f2783c15SPaul Mackerras 
984f2783c15SPaul Mackerras 	/* Number of months in days left */
985f2783c15SPaul Mackerras 	if (leapyear(tm->tm_year))
986f2783c15SPaul Mackerras 		days_in_month(FEBRUARY) = 29;
987f2783c15SPaul Mackerras 	for (i = 1; day >= days_in_month(i); i++)
988f2783c15SPaul Mackerras 		day -= days_in_month(i);
989f2783c15SPaul Mackerras 	days_in_month(FEBRUARY) = 28;
990f2783c15SPaul Mackerras 	tm->tm_mon = i;
991f2783c15SPaul Mackerras 
992f2783c15SPaul Mackerras 	/* Days are what is left over (+1) from all that. */
993f2783c15SPaul Mackerras 	tm->tm_mday = day + 1;
994f2783c15SPaul Mackerras 
995f2783c15SPaul Mackerras 	/*
996f2783c15SPaul Mackerras 	 * Determine the day of week
997f2783c15SPaul Mackerras 	 */
998f2783c15SPaul Mackerras 	GregorianDay(tm);
999f2783c15SPaul Mackerras }
1000f2783c15SPaul Mackerras 
1001f2783c15SPaul Mackerras /*
1002f2783c15SPaul Mackerras  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1003f2783c15SPaul Mackerras  * result.
1004f2783c15SPaul Mackerras  */
1005f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low,
1006f2783c15SPaul Mackerras 		  unsigned divisor, struct div_result *dr)
1007f2783c15SPaul Mackerras {
1008f2783c15SPaul Mackerras 	unsigned long a, b, c, d;
1009f2783c15SPaul Mackerras 	unsigned long w, x, y, z;
1010f2783c15SPaul Mackerras 	u64 ra, rb, rc;
1011f2783c15SPaul Mackerras 
1012f2783c15SPaul Mackerras 	a = dividend_high >> 32;
1013f2783c15SPaul Mackerras 	b = dividend_high & 0xffffffff;
1014f2783c15SPaul Mackerras 	c = dividend_low >> 32;
1015f2783c15SPaul Mackerras 	d = dividend_low & 0xffffffff;
1016f2783c15SPaul Mackerras 
1017f2783c15SPaul Mackerras 	w = a / divisor;
1018f2783c15SPaul Mackerras 	ra = ((u64)(a - (w * divisor)) << 32) + b;
1019f2783c15SPaul Mackerras 
1020f2783c15SPaul Mackerras 	rb = ((u64) do_div(ra, divisor) << 32) + c;
1021f2783c15SPaul Mackerras 	x = ra;
1022f2783c15SPaul Mackerras 
1023f2783c15SPaul Mackerras 	rc = ((u64) do_div(rb, divisor) << 32) + d;
1024f2783c15SPaul Mackerras 	y = rb;
1025f2783c15SPaul Mackerras 
1026f2783c15SPaul Mackerras 	do_div(rc, divisor);
1027f2783c15SPaul Mackerras 	z = rc;
1028f2783c15SPaul Mackerras 
1029f2783c15SPaul Mackerras 	dr->result_high = ((u64)w << 32) + x;
1030f2783c15SPaul Mackerras 	dr->result_low  = ((u64)y << 32) + z;
1031f2783c15SPaul Mackerras 
1032f2783c15SPaul Mackerras }
1033bcd68a70SGeert Uytterhoeven 
1034177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */
1035177996e6SBenjamin Herrenschmidt void calibrate_delay(void)
1036177996e6SBenjamin Herrenschmidt {
1037177996e6SBenjamin Herrenschmidt 	/* Some generic code (such as spinlock debug) use loops_per_jiffy
1038177996e6SBenjamin Herrenschmidt 	 * as the number of __delay(1) in a jiffy, so make it so
1039177996e6SBenjamin Herrenschmidt 	 */
1040177996e6SBenjamin Herrenschmidt 	loops_per_jiffy = tb_ticks_per_jiffy;
1041177996e6SBenjamin Herrenschmidt }
1042177996e6SBenjamin Herrenschmidt 
1043bcd68a70SGeert Uytterhoeven static int __init rtc_init(void)
1044bcd68a70SGeert Uytterhoeven {
1045bcd68a70SGeert Uytterhoeven 	struct platform_device *pdev;
1046bcd68a70SGeert Uytterhoeven 
1047bcd68a70SGeert Uytterhoeven 	if (!ppc_md.get_rtc_time)
1048bcd68a70SGeert Uytterhoeven 		return -ENODEV;
1049bcd68a70SGeert Uytterhoeven 
1050bcd68a70SGeert Uytterhoeven 	pdev = platform_device_register_simple("rtc-generic", -1, NULL, 0);
1051bcd68a70SGeert Uytterhoeven 
105209652b00SAdrian-Leonard Radu 	return PTR_RET(pdev);
1053bcd68a70SGeert Uytterhoeven }
1054bcd68a70SGeert Uytterhoeven 
1055bcd68a70SGeert Uytterhoeven module_init(rtc_init);
1056