i386: prepare sharing the PIT code
[powerpc.git] / arch / x86 / kernel / time_64.c
1 /*
2  *  linux/arch/x86-64/kernel/time.c
3  *
4  *  "High Precision Event Timer" based timekeeping.
5  *
6  *  Copyright (c) 1991,1992,1995  Linus Torvalds
7  *  Copyright (c) 1994  Alan Modra
8  *  Copyright (c) 1995  Markus Kuhn
9  *  Copyright (c) 1996  Ingo Molnar
10  *  Copyright (c) 1998  Andrea Arcangeli
11  *  Copyright (c) 2002,2006  Vojtech Pavlik
12  *  Copyright (c) 2003  Andi Kleen
13  *  RTC support code taken from arch/i386/kernel/timers/time_hpet.c
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/time.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/sysdev.h>
26 #include <linux/bcd.h>
27 #include <linux/notifier.h>
28 #include <linux/cpu.h>
29 #include <linux/kallsyms.h>
30 #include <linux/acpi.h>
31 #ifdef CONFIG_ACPI
32 #include <acpi/achware.h>       /* for PM timer frequency */
33 #include <acpi/acpi_bus.h>
34 #endif
35 #include <asm/i8253.h>
36 #include <asm/pgtable.h>
37 #include <asm/vsyscall.h>
38 #include <asm/timex.h>
39 #include <asm/proto.h>
40 #include <asm/hpet.h>
41 #include <asm/sections.h>
42 #include <linux/hpet.h>
43 #include <asm/apic.h>
44 #include <asm/hpet.h>
45 #include <asm/mpspec.h>
46 #include <asm/nmi.h>
47 #include <asm/vgtod.h>
48
49 static char *timename = NULL;
50
51 DEFINE_SPINLOCK(rtc_lock);
52 EXPORT_SYMBOL(rtc_lock);
53 DEFINE_SPINLOCK(i8253_lock);
54 EXPORT_SYMBOL(i8253_lock);
55
56 volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
57
58 unsigned long profile_pc(struct pt_regs *regs)
59 {
60         unsigned long pc = instruction_pointer(regs);
61
62         /* Assume the lock function has either no stack frame or a copy
63            of eflags from PUSHF
64            Eflags always has bits 22 and up cleared unlike kernel addresses. */
65         if (!user_mode(regs) && in_lock_functions(pc)) {
66                 unsigned long *sp = (unsigned long *)regs->rsp;
67                 if (sp[0] >> 22)
68                         return sp[0];
69                 if (sp[1] >> 22)
70                         return sp[1];
71         }
72         return pc;
73 }
74 EXPORT_SYMBOL(profile_pc);
75
76 /*
77  * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
78  * ms after the second nowtime has started, because when nowtime is written
79  * into the registers of the CMOS clock, it will jump to the next second
80  * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
81  * sheet for details.
82  */
83
84 static int set_rtc_mmss(unsigned long nowtime)
85 {
86         int retval = 0;
87         int real_seconds, real_minutes, cmos_minutes;
88         unsigned char control, freq_select;
89
90 /*
91  * IRQs are disabled when we're called from the timer interrupt,
92  * no need for spin_lock_irqsave()
93  */
94
95         spin_lock(&rtc_lock);
96
97 /*
98  * Tell the clock it's being set and stop it.
99  */
100
101         control = CMOS_READ(RTC_CONTROL);
102         CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
103
104         freq_select = CMOS_READ(RTC_FREQ_SELECT);
105         CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
106
107         cmos_minutes = CMOS_READ(RTC_MINUTES);
108                 BCD_TO_BIN(cmos_minutes);
109
110 /*
111  * since we're only adjusting minutes and seconds, don't interfere with hour
112  * overflow. This avoids messing with unknown time zones but requires your RTC
113  * not to be off by more than 15 minutes. Since we're calling it only when
114  * our clock is externally synchronized using NTP, this shouldn't be a problem.
115  */
116
117         real_seconds = nowtime % 60;
118         real_minutes = nowtime / 60;
119         if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
120                 real_minutes += 30;             /* correct for half hour time zone */
121         real_minutes %= 60;
122
123         if (abs(real_minutes - cmos_minutes) >= 30) {
124                 printk(KERN_WARNING "time.c: can't update CMOS clock "
125                        "from %d to %d\n", cmos_minutes, real_minutes);
126                 retval = -1;
127         } else {
128                 BIN_TO_BCD(real_seconds);
129                 BIN_TO_BCD(real_minutes);
130                 CMOS_WRITE(real_seconds, RTC_SECONDS);
131                 CMOS_WRITE(real_minutes, RTC_MINUTES);
132         }
133
134 /*
135  * The following flags have to be released exactly in this order, otherwise the
136  * DS12887 (popular MC146818A clone with integrated battery and quartz) will
137  * not reset the oscillator and will not update precisely 500 ms later. You
138  * won't find this mentioned in the Dallas Semiconductor data sheets, but who
139  * believes data sheets anyway ... -- Markus Kuhn
140  */
141
142         CMOS_WRITE(control, RTC_CONTROL);
143         CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
144
145         spin_unlock(&rtc_lock);
146
147         return retval;
148 }
149
150 int update_persistent_clock(struct timespec now)
151 {
152         return set_rtc_mmss(now.tv_sec);
153 }
154
155 void main_timer_handler(void)
156 {
157 /*
158  * Here we are in the timer irq handler. We have irqs locally disabled (so we
159  * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
160  * on the other CPU, so we need a lock. We also need to lock the vsyscall
161  * variables, because both do_timer() and us change them -arca+vojtech
162  */
163
164         write_seqlock(&xtime_lock);
165
166 /*
167  * Do the timer stuff.
168  */
169
170         do_timer(1);
171 #ifndef CONFIG_SMP
172         update_process_times(user_mode(get_irq_regs()));
173 #endif
174
175 /*
176  * In the SMP case we use the local APIC timer interrupt to do the profiling,
177  * except when we simulate SMP mode on a uniprocessor system, in that case we
178  * have to call the local interrupt handler.
179  */
180
181         if (!using_apic_timer)
182                 smp_local_timer_interrupt();
183
184         write_sequnlock(&xtime_lock);
185 }
186
187 static irqreturn_t timer_interrupt(int irq, void *dev_id)
188 {
189         if (apic_runs_main_timer > 1)
190                 return IRQ_HANDLED;
191         main_timer_handler();
192         if (using_apic_timer)
193                 smp_send_timer_broadcast_ipi();
194         return IRQ_HANDLED;
195 }
196
197 unsigned long read_persistent_clock(void)
198 {
199         unsigned int year, mon, day, hour, min, sec;
200         unsigned long flags;
201         unsigned century = 0;
202
203         spin_lock_irqsave(&rtc_lock, flags);
204
205         do {
206                 sec = CMOS_READ(RTC_SECONDS);
207                 min = CMOS_READ(RTC_MINUTES);
208                 hour = CMOS_READ(RTC_HOURS);
209                 day = CMOS_READ(RTC_DAY_OF_MONTH);
210                 mon = CMOS_READ(RTC_MONTH);
211                 year = CMOS_READ(RTC_YEAR);
212 #ifdef CONFIG_ACPI
213                 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
214                                         acpi_gbl_FADT.century)
215                         century = CMOS_READ(acpi_gbl_FADT.century);
216 #endif
217         } while (sec != CMOS_READ(RTC_SECONDS));
218
219         spin_unlock_irqrestore(&rtc_lock, flags);
220
221         /*
222          * We know that x86-64 always uses BCD format, no need to check the
223          * config register.
224          */
225
226         BCD_TO_BIN(sec);
227         BCD_TO_BIN(min);
228         BCD_TO_BIN(hour);
229         BCD_TO_BIN(day);
230         BCD_TO_BIN(mon);
231         BCD_TO_BIN(year);
232
233         if (century) {
234                 BCD_TO_BIN(century);
235                 year += century * 100;
236                 printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
237         } else {
238                 /*
239                  * x86-64 systems only exists since 2002.
240                  * This will work up to Dec 31, 2100
241                  */
242                 year += 2000;
243         }
244
245         return mktime(year, mon, day, hour, min, sec);
246 }
247
248 /* calibrate_cpu is used on systems with fixed rate TSCs to determine
249  * processor frequency */
250 #define TICK_COUNT 100000000
251 static unsigned int __init tsc_calibrate_cpu_khz(void)
252 {
253         int tsc_start, tsc_now;
254         int i, no_ctr_free;
255         unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
256         unsigned long flags;
257
258         for (i = 0; i < 4; i++)
259                 if (avail_to_resrv_perfctr_nmi_bit(i))
260                         break;
261         no_ctr_free = (i == 4);
262         if (no_ctr_free) {
263                 i = 3;
264                 rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
265                 wrmsrl(MSR_K7_EVNTSEL3, 0);
266                 rdmsrl(MSR_K7_PERFCTR3, pmc3);
267         } else {
268                 reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
269                 reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
270         }
271         local_irq_save(flags);
272         /* start meauring cycles, incrementing from 0 */
273         wrmsrl(MSR_K7_PERFCTR0 + i, 0);
274         wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
275         rdtscl(tsc_start);
276         do {
277                 rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
278                 tsc_now = get_cycles_sync();
279         } while ((tsc_now - tsc_start) < TICK_COUNT);
280
281         local_irq_restore(flags);
282         if (no_ctr_free) {
283                 wrmsrl(MSR_K7_EVNTSEL3, 0);
284                 wrmsrl(MSR_K7_PERFCTR3, pmc3);
285                 wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
286         } else {
287                 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
288                 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
289         }
290
291         return pmc_now * tsc_khz / (tsc_now - tsc_start);
292 }
293
294 static void __pit_init(int val, u8 mode)
295 {
296         unsigned long flags;
297
298         spin_lock_irqsave(&i8253_lock, flags);
299         outb_p(mode, PIT_MODE);
300         outb_p(val & 0xff, PIT_CH0);    /* LSB */
301         outb_p(val >> 8, PIT_CH0);      /* MSB */
302         spin_unlock_irqrestore(&i8253_lock, flags);
303 }
304
305 void __init pit_init(void)
306 {
307         __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
308 }
309
310 void pit_stop_interrupt(void)
311 {
312         __pit_init(0, 0x30); /* mode 0 */
313 }
314
315 void stop_timer_interrupt(void)
316 {
317         char *name;
318         if (hpet_address) {
319                 name = "HPET";
320                 hpet_timer_stop_set_go(0);
321         } else {
322                 name = "PIT";
323                 pit_stop_interrupt();
324         }
325         printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
326 }
327
328 static struct irqaction irq0 = {
329         .handler        = timer_interrupt,
330         .flags          = IRQF_DISABLED | IRQF_IRQPOLL | IRQF_NOBALANCING,
331         .mask           = CPU_MASK_NONE,
332         .name           = "timer"
333 };
334
335 void __init time_init(void)
336 {
337         if (nohpet)
338                 hpet_address = 0;
339
340         if (hpet_arch_init())
341                 hpet_address = 0;
342
343         if (hpet_use_timer) {
344                 /* set tick_nsec to use the proper rate for HPET */
345                 tick_nsec = TICK_NSEC_HPET;
346                 timename = "HPET";
347         } else {
348                 pit_init();
349                 timename = "PIT";
350         }
351
352         tsc_calibrate();
353
354         cpu_khz = tsc_khz;
355         if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
356                 boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
357                 boot_cpu_data.x86 == 16)
358                 cpu_khz = tsc_calibrate_cpu_khz();
359
360         if (unsynchronized_tsc())
361                 mark_tsc_unstable("TSCs unsynchronized");
362
363         if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
364                 vgetcpu_mode = VGETCPU_RDTSCP;
365         else
366                 vgetcpu_mode = VGETCPU_LSL;
367
368         set_cyc2ns_scale(tsc_khz);
369         printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
370                 cpu_khz / 1000, cpu_khz % 1000);
371         init_tsc_clocksource();
372
373         setup_irq(0, &irq0);
374 }
375
376 /*
377  * sysfs support for the timer.
378  */
379
380 static int timer_suspend(struct sys_device *dev, pm_message_t state)
381 {
382         return 0;
383 }
384
385 static int timer_resume(struct sys_device *dev)
386 {
387         if (hpet_address)
388                 hpet_reenable();
389         else
390                 i8254_timer_resume();
391         return 0;
392 }
393
394 static struct sysdev_class timer_sysclass = {
395         .resume = timer_resume,
396         .suspend = timer_suspend,
397         set_kset_name("timer"),
398 };
399
400 /* XXX this sysfs stuff should probably go elsewhere later -john */
401 static struct sys_device device_timer = {
402         .id     = 0,
403         .cls    = &timer_sysclass,
404 };
405
406 static int time_init_device(void)
407 {
408         int error = sysdev_class_register(&timer_sysclass);
409         if (!error)
410                 error = sysdev_register(&device_timer);
411         return error;
412 }
413
414 device_initcall(time_init_device);