blob: 3f221f5eb47ee690612657eb0fede58f7d1e813f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/i386/kernel/time.c
3 *
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
5 *
6 * This file contains the PC-specific time handling details:
7 * reading the RTC at bootup, etc..
8 * 1994-07-02 Alan Modra
9 * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
10 * 1995-03-26 Markus Kuhn
11 * fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
12 * precision CMOS clock update
13 * 1996-05-03 Ingo Molnar
14 * fixed time warps in do_[slow|fast]_gettimeoffset()
15 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
16 * "A Kernel Model for Precision Timekeeping" by Dave Mills
17 * 1998-09-05 (Various)
18 * More robust do_fast_gettimeoffset() algorithm implemented
19 * (works with APM, Cyrix 6x86MX and Centaur C6),
20 * monotonic gettimeofday() with fast_get_timeoffset(),
21 * drift-proof precision TSC calibration on boot
22 * (C. Scott Ananian <cananian@alumni.princeton.edu>, Andrew D.
23 * Balsa <andrebalsa@altern.org>, Philip Gladstone <philip@raptor.com>;
24 * ported from 2.0.35 Jumbo-9 by Michael Krause <m.krause@tu-harburg.de>).
25 * 1998-12-16 Andrea Arcangeli
26 * Fixed Jumbo-9 code in 2.1.131: do_gettimeofday was missing 1 jiffy
27 * because was not accounting lost_ticks.
28 * 1998-12-24 Copyright (C) 1998 Andrea Arcangeli
29 * Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
30 * serialize accesses to xtime/lost_ticks).
31 */
32
33#include <linux/errno.h>
34#include <linux/sched.h>
35#include <linux/kernel.h>
36#include <linux/param.h>
37#include <linux/string.h>
38#include <linux/mm.h>
39#include <linux/interrupt.h>
40#include <linux/time.h>
41#include <linux/delay.h>
42#include <linux/init.h>
43#include <linux/smp.h>
44#include <linux/module.h>
45#include <linux/sysdev.h>
46#include <linux/bcd.h>
47#include <linux/efi.h>
48#include <linux/mca.h>
49
50#include <asm/io.h>
51#include <asm/smp.h>
52#include <asm/irq.h>
53#include <asm/msr.h>
54#include <asm/delay.h>
55#include <asm/mpspec.h>
56#include <asm/uaccess.h>
57#include <asm/processor.h>
58#include <asm/timer.h>
59
60#include "mach_time.h"
61
62#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <asm/hpet.h>
65
66#include <asm/arch_hooks.h>
67
68#include "io_ports.h"
69
Ingo Molnar306e4402005-06-30 02:58:55 -070070#include <asm/i8259.h>
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072int pit_latch_buggy; /* extern */
73
74#include "do_timer.h"
75
Andrew Mortona3a255e2005-06-23 00:08:34 -070076unsigned int cpu_khz; /* Detected as we calibrate the TSC */
Alexey Dobriyan129f6942005-06-23 00:08:33 -070077EXPORT_SYMBOL(cpu_khz);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079DEFINE_SPINLOCK(rtc_lock);
Alexey Dobriyan129f6942005-06-23 00:08:33 -070080EXPORT_SYMBOL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * This is a special lock that is owned by the CPU and holds the index
84 * register we are working with. It is required for NMI access to the
85 * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
86 */
87volatile unsigned long cmos_lock = 0;
88EXPORT_SYMBOL(cmos_lock);
89
90/* Routines for accessing the CMOS RAM/RTC. */
91unsigned char rtc_cmos_read(unsigned char addr)
92{
93 unsigned char val;
94 lock_cmos_prefix(addr);
95 outb_p(addr, RTC_PORT(0));
96 val = inb_p(RTC_PORT(1));
97 lock_cmos_suffix(addr);
98 return val;
99}
100EXPORT_SYMBOL(rtc_cmos_read);
101
102void rtc_cmos_write(unsigned char val, unsigned char addr)
103{
104 lock_cmos_prefix(addr);
105 outb_p(addr, RTC_PORT(0));
106 outb_p(val, RTC_PORT(1));
107 lock_cmos_suffix(addr);
108}
109EXPORT_SYMBOL(rtc_cmos_write);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int set_rtc_mmss(unsigned long nowtime)
112{
113 int retval;
john stultz6f84fa22006-06-26 00:25:11 -0700114 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /* gets recalled with irq locally disabled */
john stultz6f84fa22006-06-26 00:25:11 -0700117 /* XXX - does irqsave resolve this? -johnstul */
118 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (efi_enabled)
120 retval = efi_set_rtc_mmss(nowtime);
121 else
122 retval = mach_set_rtc_mmss(nowtime);
john stultz6f84fa22006-06-26 00:25:11 -0700123 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return retval;
126}
127
128
129int timer_ack;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131unsigned long profile_pc(struct pt_regs *regs)
132{
133 unsigned long pc = instruction_pointer(regs);
134
Andi Kleen0cb91a22006-09-26 10:52:28 +0200135#ifdef CONFIG_SMP
136 if (!user_mode_vm(regs) && in_lock_functions(pc)) {
137#ifdef CONFIG_FRAME_POINTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return *(unsigned long *)(regs->ebp + 4);
Andi Kleen0cb91a22006-09-26 10:52:28 +0200139#else
140 unsigned long *sp;
141 if ((regs->xcs & 3) == 0)
142 sp = (unsigned long *)&regs->esp;
143 else
144 sp = (unsigned long *)regs->esp;
145 /* Return address is either directly at stack pointer
146 or above a saved eflags. Eflags has bits 22-31 zero,
147 kernel addresses don't. */
148 if (sp[0] >> 22)
149 return sp[0];
150 if (sp[1] >> 22)
151 return sp[1];
152#endif
153 }
154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return pc;
156}
157EXPORT_SYMBOL(profile_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/*
john stultz6f84fa22006-06-26 00:25:11 -0700160 * This is the same as the above, except we _also_ save the current
161 * Time Stamp Counter value at the time of the timer interrupt, so that
162 * we later on can estimate the time of day more exactly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
David Howells7d12e782006-10-05 14:55:46 +0100164irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
john stultz6f84fa22006-06-26 00:25:11 -0700166 /*
167 * Here we are in the timer irq handler. We just have irqs locally
168 * disabled but we don't know if the timer_bh is running on the other
169 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
170 * the irq version of write_lock because as just said we have irq
171 * locally disabled. -arca
172 */
173 write_seqlock(&xtime_lock);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#ifdef CONFIG_X86_IO_APIC
176 if (timer_ack) {
177 /*
178 * Subtle, when I/O APICs are used we have to ack timer IRQ
179 * manually to reset the IRR bit for do_slow_gettimeoffset().
180 * This will also deassert NMI lines for the watchdog if run
181 * on an 82489DX-based system.
182 */
183 spin_lock(&i8259A_lock);
184 outb(0x0c, PIC_MASTER_OCW3);
185 /* Ack the IRQ; AEOI will end it automatically. */
186 inb(PIC_MASTER_POLL);
187 spin_unlock(&i8259A_lock);
188 }
189#endif
190
David Howells7d12e782006-10-05 14:55:46 +0100191 do_timer_interrupt_hook();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193
194 if (MCA_bus) {
195 /* The PS/2 uses level-triggered interrupts. You can't
196 turn them off, nor would you want to (any attempt to
197 enable edge-triggered interrupts usually gets intercepted by a
198 special hardware circuit). Hence we have to acknowledge
199 the timer interrupt. Through some incredibly stupid
200 design idea, the reset for IRQ 0 is done by setting the
201 high bit of the PPI port B (0x61). Note that some PS/2s,
202 notably the 55SX, work fine if this is removed. */
203
204 irq = inb_p( 0x61 ); /* read the current state */
205 outb_p( irq|0x80, 0x61 ); /* reset the IRQ */
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 write_sequnlock(&xtime_lock);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100209
210#ifdef CONFIG_X86_LOCAL_APIC
211 if (using_apic_timer)
David Howells7d12e782006-10-05 14:55:46 +0100212 smp_send_timer_broadcast_ipi();
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100213#endif
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return IRQ_HANDLED;
216}
217
218/* not static: needed by APM */
219unsigned long get_cmos_time(void)
220{
221 unsigned long retval;
Andrew Morton7ba1c6c2006-07-10 04:44:59 -0700222 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Andrew Morton7ba1c6c2006-07-10 04:44:59 -0700224 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (efi_enabled)
227 retval = efi_get_time();
228 else
229 retval = mach_get_cmos_time();
230
Andrew Morton7ba1c6c2006-07-10 04:44:59 -0700231 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return retval;
234}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700235EXPORT_SYMBOL(get_cmos_time);
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static void sync_cmos_clock(unsigned long dummy);
238
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700239static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static void sync_cmos_clock(unsigned long dummy)
242{
243 struct timeval now, next;
244 int fail = 1;
245
246 /*
247 * If we have an externally synchronized Linux clock, then update
248 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
249 * called as close as possible to 500 ms before the new second starts.
250 * This code is run on a timer. If the clock is set, that timer
251 * may not expire at the correct time. Thus, we adjust...
252 */
john stultzb149ee22005-09-06 15:17:46 -0700253 if (!ntp_synced())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
255 * Not synced, exit, do not restart a timer (if one is
256 * running, let it run out).
257 */
258 return;
259
260 do_gettimeofday(&now);
261 if (now.tv_usec >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
262 now.tv_usec <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2)
263 fail = set_rtc_mmss(now.tv_sec);
264
265 next.tv_usec = USEC_AFTER - now.tv_usec;
266 if (next.tv_usec <= 0)
267 next.tv_usec += USEC_PER_SEC;
268
269 if (!fail)
270 next.tv_sec = 659;
271 else
272 next.tv_sec = 0;
273
274 if (next.tv_usec >= USEC_PER_SEC) {
275 next.tv_sec++;
276 next.tv_usec -= USEC_PER_SEC;
277 }
278 mod_timer(&sync_cmos_timer, jiffies + timeval_to_jiffies(&next));
279}
280
281void notify_arch_cmos_timer(void)
282{
283 mod_timer(&sync_cmos_timer, jiffies + 1);
284}
285
Rafael J. Wysocki7d145aa2006-09-25 23:32:57 -0700286static long clock_cmos_diff;
287static unsigned long sleep_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Pavel Machek438510f2005-04-16 15:25:24 -0700289static int timer_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 /*
292 * Estimate time zone so that set_time can update the clock
293 */
Rafael J. Wysocki7d145aa2006-09-25 23:32:57 -0700294 unsigned long ctime = get_cmos_time();
295
296 clock_cmos_diff = -ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 clock_cmos_diff += get_seconds();
Rafael J. Wysocki7d145aa2006-09-25 23:32:57 -0700298 sleep_start = ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300}
301
302static int timer_resume(struct sys_device *dev)
303{
304 unsigned long flags;
305 unsigned long sec;
Rafael J. Wysocki7d145aa2006-09-25 23:32:57 -0700306 unsigned long ctime = get_cmos_time();
307 long sleep_length = (ctime - sleep_start) * HZ;
john stultzc7f40ff12006-09-25 23:32:35 -0700308 struct timespec ts;
Rafael J. Wysocki7d145aa2006-09-25 23:32:57 -0700309
310 if (sleep_length < 0) {
311 printk(KERN_WARNING "CMOS clock skew detected in timer resume!\n");
312 /* The time after the resume must not be earlier than the time
313 * before the suspend or some nasty things will happen
314 */
315 sleep_length = 0;
316 ctime = sleep_start;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#ifdef CONFIG_HPET_TIMER
319 if (is_hpet_enabled())
320 hpet_reenable();
321#endif
Shaohua Lic3c433e2005-09-03 15:57:07 -0700322 setup_pit_timer();
john stultzc7f40ff12006-09-25 23:32:35 -0700323
Rafael J. Wysocki7d145aa2006-09-25 23:32:57 -0700324 sec = ctime + clock_cmos_diff;
john stultzc7f40ff12006-09-25 23:32:35 -0700325 ts.tv_sec = sec;
326 ts.tv_nsec = 0;
327 do_settimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 write_seqlock_irqsave(&xtime_lock, flags);
Atsushi Nemotof7c09bd2006-03-06 15:42:42 -0800329 jiffies_64 += sleep_length;
Atsushi Nemotof7c09bd2006-03-06 15:42:42 -0800330 write_sequnlock_irqrestore(&xtime_lock, flags);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700331 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333}
334
335static struct sysdev_class timer_sysclass = {
336 .resume = timer_resume,
337 .suspend = timer_suspend,
338 set_kset_name("timer"),
339};
340
341
342/* XXX this driverfs stuff should probably go elsewhere later -john */
343static struct sys_device device_timer = {
344 .id = 0,
345 .cls = &timer_sysclass,
346};
347
348static int time_init_device(void)
349{
350 int error = sysdev_class_register(&timer_sysclass);
351 if (!error)
352 error = sysdev_register(&device_timer);
353 return error;
354}
355
356device_initcall(time_init_device);
357
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -0800358#ifdef CONFIG_HPET_TIMER
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800359extern void (*late_time_init)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* Duplicate of time_init() below, with hpet_enable part added */
361static void __init hpet_time_init(void)
362{
john stultzc7f40ff12006-09-25 23:32:35 -0700363 struct timespec ts;
364 ts.tv_sec = get_cmos_time();
365 ts.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
366
367 do_settimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
john stultz35492df2005-05-01 08:58:50 -0700369 if ((hpet_enable() >= 0) && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 printk("Using HPET for base-timer\n");
371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 time_init_hook();
374}
375#endif
376
377void __init time_init(void)
378{
john stultzc7f40ff12006-09-25 23:32:35 -0700379 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#ifdef CONFIG_HPET_TIMER
381 if (is_hpet_capable()) {
382 /*
383 * HPET initialization needs to do memory-mapped io. So, let
384 * us do a late initialization after mem_init().
385 */
386 late_time_init = hpet_time_init;
387 return;
388 }
389#endif
john stultzc7f40ff12006-09-25 23:32:35 -0700390 ts.tv_sec = get_cmos_time();
391 ts.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
392
393 do_settimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 time_init_hook();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}