ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/time.c @ 6141:7c2fdcb2c933

another merge
author kaf24@firebug.cl.cam.ac.uk
date Fri Aug 12 14:53:26 2005 +0000 (2005-08-12)
parents 38bee85ddeb8 10b395bc465e
children 4995d5f167c9 f51fe43c5d1c 6783e59e1c45 40b887fa79d0
line source
1 /*
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 */
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 #include <linux/sysctl.h>
50 #include <linux/percpu.h>
52 #include <asm/io.h>
53 #include <asm/smp.h>
54 #include <asm/irq.h>
55 #include <asm/msr.h>
56 #include <asm/delay.h>
57 #include <asm/mpspec.h>
58 #include <asm/uaccess.h>
59 #include <asm/processor.h>
60 #include <asm/timer.h>
62 #include "mach_time.h"
64 #include <linux/timex.h>
65 #include <linux/config.h>
67 #include <asm/hpet.h>
69 #include <asm/arch_hooks.h>
71 #include "io_ports.h"
73 #include <asm-xen/evtchn.h>
75 extern spinlock_t i8259A_lock;
76 int pit_latch_buggy; /* extern */
78 u64 jiffies_64 = INITIAL_JIFFIES;
80 EXPORT_SYMBOL(jiffies_64);
82 #if defined(__x86_64__)
83 unsigned long vxtime_hz = PIT_TICK_RATE;
84 struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */
85 volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
86 unsigned long __wall_jiffies __section_wall_jiffies = INITIAL_JIFFIES;
87 struct timespec __xtime __section_xtime;
88 struct timezone __sys_tz __section_sys_tz;
89 #endif
91 #if defined(__x86_64__)
92 unsigned int cpu_khz; /* Detected as we calibrate the TSC */
93 #else
94 unsigned long cpu_khz; /* Detected as we calibrate the TSC */
95 #endif
97 extern unsigned long wall_jiffies;
99 DEFINE_SPINLOCK(rtc_lock);
101 DEFINE_SPINLOCK(i8253_lock);
102 EXPORT_SYMBOL(i8253_lock);
104 extern struct init_timer_opts timer_tsc_init;
105 extern struct timer_opts timer_tsc;
106 struct timer_opts *cur_timer = &timer_tsc;
108 /* These are peridically updated in shared_info, and then copied here. */
109 struct shadow_time_info {
110 u64 tsc_timestamp; /* TSC at last update of time vals. */
111 u64 system_timestamp; /* Time, in nanosecs, since boot. */
112 u32 tsc_to_nsec_mul;
113 u32 tsc_to_usec_mul;
114 int tsc_shift;
115 u32 version;
116 };
117 static DEFINE_PER_CPU(struct shadow_time_info, shadow_time);
118 static struct timespec shadow_tv;
119 static u32 shadow_tv_version;
121 /* Keep track of last time we did processing/updating of jiffies and xtime. */
122 static u64 processed_system_time; /* System time (ns) at last processing. */
123 static DEFINE_PER_CPU(u64, processed_system_time);
125 #define NS_PER_TICK (1000000000L/HZ)
127 static inline void __normalize_time(time_t *sec, s64 *nsec)
128 {
129 while (*nsec >= NSEC_PER_SEC) {
130 (*nsec) -= NSEC_PER_SEC;
131 (*sec)++;
132 }
133 while (*nsec < 0) {
134 (*nsec) += NSEC_PER_SEC;
135 (*sec)--;
136 }
137 }
139 /* Does this guest OS track Xen time, or set its wall clock independently? */
140 static int independent_wallclock = 0;
141 static int __init __independent_wallclock(char *str)
142 {
143 independent_wallclock = 1;
144 return 1;
145 }
146 __setup("independent_wallclock", __independent_wallclock);
148 int tsc_disable __initdata = 0;
150 static void delay_tsc(unsigned long loops)
151 {
152 unsigned long bclock, now;
154 rdtscl(bclock);
155 do
156 {
157 rep_nop();
158 rdtscl(now);
159 } while ((now-bclock) < loops);
160 }
162 struct timer_opts timer_tsc = {
163 .name = "tsc",
164 .delay = delay_tsc,
165 };
167 /*
168 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
169 * yielding a 64-bit result.
170 */
171 static inline u64 scale_delta(u64 delta, u32 mul_frac, int shift)
172 {
173 u64 product;
174 #ifdef __i386__
175 u32 tmp1, tmp2;
176 #endif
178 if ( shift < 0 )
179 delta >>= -shift;
180 else
181 delta <<= shift;
183 #ifdef __i386__
184 __asm__ (
185 "mul %5 ; "
186 "mov %4,%%eax ; "
187 "mov %%edx,%4 ; "
188 "mul %5 ; "
189 "add %4,%%eax ; "
190 "xor %5,%5 ; "
191 "adc %5,%%edx ; "
192 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
193 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
194 #else
195 __asm__ (
196 "mul %%rdx ; shrd $32,%%rdx,%%rax"
197 : "=a" (product) : "0" (delta), "d" ((u64)mul_frac) );
198 #endif
200 return product;
201 }
203 void init_cpu_khz(void)
204 {
205 u64 __cpu_khz = 1000000ULL << 32;
206 struct vcpu_time_info *info = &HYPERVISOR_shared_info->vcpu_time[0];
207 do_div(__cpu_khz, info->tsc_to_system_mul);
208 if ( info->tsc_shift < 0 )
209 cpu_khz = __cpu_khz >> -info->tsc_shift;
210 else
211 cpu_khz = __cpu_khz << info->tsc_shift;
212 }
214 static u64 get_nsec_offset(struct shadow_time_info *shadow)
215 {
216 u64 now, delta;
217 rdtscll(now);
218 delta = now - shadow->tsc_timestamp;
219 return scale_delta(delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift);
220 }
222 static unsigned long get_usec_offset(struct shadow_time_info *shadow)
223 {
224 u64 now, delta;
225 rdtscll(now);
226 delta = now - shadow->tsc_timestamp;
227 return scale_delta(delta, shadow->tsc_to_usec_mul, shadow->tsc_shift);
228 }
230 static void __update_wallclock(time_t sec, long nsec)
231 {
232 long wtm_nsec, xtime_nsec;
233 time_t wtm_sec, xtime_sec;
234 s64 tmp, wc_nsec;
236 /* Adjust wall-clock time base based on wall_jiffies ticks. */
237 wc_nsec = processed_system_time;
238 wc_nsec += (sec * 1000000000LL) + nsec;
239 wc_nsec -= (jiffies - wall_jiffies) * (u64)(NSEC_PER_SEC / HZ);
241 /* Split wallclock base into seconds and nanoseconds. */
242 if ( (tmp = wc_nsec) < 0 )
243 {
244 /* -ve UTC offset => -ve seconds, +ve nanoseconds. */
245 tmp = -tmp;
246 xtime_nsec = do_div(tmp, 1000000000);
247 tmp = -tmp;
248 if ( xtime_nsec != 0 )
249 {
250 xtime_nsec = 1000000000 - xtime_nsec;
251 tmp--;
252 }
253 }
254 else
255 {
256 /* +ve UTC offset => +ve seconds, +ve nanoseconds. */
257 xtime_nsec = do_div(tmp, 1000000000);
258 }
259 xtime_sec = (time_t)tmp;
261 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - xtime_sec);
262 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - xtime_nsec);
264 set_normalized_timespec(&xtime, xtime_sec, xtime_nsec);
265 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
267 time_adjust = 0; /* stop active adjtime() */
268 time_status |= STA_UNSYNC;
269 time_maxerror = NTP_PHASE_LIMIT;
270 time_esterror = NTP_PHASE_LIMIT;
271 }
273 static void update_wallclock(void)
274 {
275 shared_info_t *s = HYPERVISOR_shared_info;
277 do {
278 shadow_tv_version = s->wc_version;
279 rmb();
280 shadow_tv.tv_sec = s->wc_sec;
281 shadow_tv.tv_nsec = s->wc_nsec;
282 rmb();
283 }
284 while ((s->wc_version & 1) | (shadow_tv_version ^ s->wc_version));
286 if (!independent_wallclock)
287 __update_wallclock(shadow_tv.tv_sec, shadow_tv.tv_nsec);
288 }
290 /*
291 * Reads a consistent set of time-base values from Xen, into a shadow data
292 * area.
293 */
294 static void get_time_values_from_xen(void)
295 {
296 shared_info_t *s = HYPERVISOR_shared_info;
297 struct vcpu_time_info *src;
298 struct shadow_time_info *dst;
300 src = &s->vcpu_time[smp_processor_id()];
301 dst = &per_cpu(shadow_time, smp_processor_id());
303 do {
304 dst->version = src->version;
305 rmb();
306 dst->tsc_timestamp = src->tsc_timestamp;
307 dst->system_timestamp = src->system_time;
308 dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
309 dst->tsc_shift = src->tsc_shift;
310 rmb();
311 }
312 while ((src->version & 1) | (dst->version ^ src->version));
314 dst->tsc_to_usec_mul = dst->tsc_to_nsec_mul / 1000;
315 }
317 static inline int time_values_up_to_date(int cpu)
318 {
319 struct vcpu_time_info *src;
320 struct shadow_time_info *dst;
322 src = &HYPERVISOR_shared_info->vcpu_time[cpu];
323 dst = &per_cpu(shadow_time, cpu);
325 return (dst->version == src->version);
326 }
328 /*
329 * This is a special lock that is owned by the CPU and holds the index
330 * register we are working with. It is required for NMI access to the
331 * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
332 */
333 volatile unsigned long cmos_lock = 0;
334 EXPORT_SYMBOL(cmos_lock);
336 /* Routines for accessing the CMOS RAM/RTC. */
337 unsigned char rtc_cmos_read(unsigned char addr)
338 {
339 unsigned char val;
340 lock_cmos_prefix(addr);
341 outb_p(addr, RTC_PORT(0));
342 val = inb_p(RTC_PORT(1));
343 lock_cmos_suffix(addr);
344 return val;
345 }
346 EXPORT_SYMBOL(rtc_cmos_read);
348 void rtc_cmos_write(unsigned char val, unsigned char addr)
349 {
350 lock_cmos_prefix(addr);
351 outb_p(addr, RTC_PORT(0));
352 outb_p(val, RTC_PORT(1));
353 lock_cmos_suffix(addr);
354 }
355 EXPORT_SYMBOL(rtc_cmos_write);
357 /*
358 * This version of gettimeofday has microsecond resolution
359 * and better than microsecond precision on fast x86 machines with TSC.
360 */
361 void do_gettimeofday(struct timeval *tv)
362 {
363 unsigned long seq;
364 unsigned long usec, sec;
365 unsigned long max_ntp_tick;
366 s64 nsec;
367 unsigned int cpu;
368 struct shadow_time_info *shadow;
369 u32 local_time_version;
371 cpu = get_cpu();
372 shadow = &per_cpu(shadow_time, cpu);
374 do {
375 unsigned long lost;
377 local_time_version = shadow->version;
378 seq = read_seqbegin(&xtime_lock);
380 usec = get_usec_offset(shadow);
381 lost = jiffies - wall_jiffies;
383 /*
384 * If time_adjust is negative then NTP is slowing the clock
385 * so make sure not to go into next possible interval.
386 * Better to lose some accuracy than have time go backwards..
387 */
388 if (unlikely(time_adjust < 0)) {
389 max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
390 usec = min(usec, max_ntp_tick);
392 if (lost)
393 usec += lost * max_ntp_tick;
394 }
395 else if (unlikely(lost))
396 usec += lost * (USEC_PER_SEC / HZ);
398 sec = xtime.tv_sec;
399 usec += (xtime.tv_nsec / NSEC_PER_USEC);
401 nsec = shadow->system_timestamp - processed_system_time;
402 __normalize_time(&sec, &nsec);
403 usec += (long)nsec / NSEC_PER_USEC;
405 if (unlikely(!time_values_up_to_date(cpu))) {
406 /*
407 * We may have blocked for a long time,
408 * rendering our calculations invalid
409 * (e.g. the time delta may have
410 * overflowed). Detect that and recalculate
411 * with fresh values.
412 */
413 get_time_values_from_xen();
414 continue;
415 }
416 } while (read_seqretry(&xtime_lock, seq) ||
417 (local_time_version != shadow->version));
419 put_cpu();
421 while (usec >= USEC_PER_SEC) {
422 usec -= USEC_PER_SEC;
423 sec++;
424 }
426 tv->tv_sec = sec;
427 tv->tv_usec = usec;
428 }
430 EXPORT_SYMBOL(do_gettimeofday);
432 int do_settimeofday(struct timespec *tv)
433 {
434 time_t sec;
435 s64 nsec;
436 unsigned int cpu;
437 struct shadow_time_info *shadow;
438 dom0_op_t op;
440 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
441 return -EINVAL;
443 cpu = get_cpu();
444 shadow = &per_cpu(shadow_time, cpu);
446 write_seqlock_irq(&xtime_lock);
448 /*
449 * Ensure we don't get blocked for a long time so that our time delta
450 * overflows. If that were to happen then our shadow time values would
451 * be stale, so we can retry with fresh ones.
452 */
453 for ( ; ; ) {
454 nsec = (s64)tv->tv_nsec - (s64)get_nsec_offset(shadow);
455 if (time_values_up_to_date(cpu))
456 break;
457 get_time_values_from_xen();
458 }
459 sec = tv->tv_sec;
460 __normalize_time(&sec, &nsec);
462 if ((xen_start_info.flags & SIF_INITDOMAIN) &&
463 !independent_wallclock) {
464 op.cmd = DOM0_SETTIME;
465 op.u.settime.secs = sec;
466 op.u.settime.nsecs = nsec;
467 op.u.settime.system_time = shadow->system_timestamp;
468 HYPERVISOR_dom0_op(&op);
469 update_wallclock();
470 } else if (independent_wallclock) {
471 nsec -= shadow->system_timestamp;
472 __normalize_time(&sec, &nsec);
473 __update_wallclock(sec, nsec);
474 }
476 write_sequnlock_irq(&xtime_lock);
478 put_cpu();
480 clock_was_set();
481 return 0;
482 }
484 EXPORT_SYMBOL(do_settimeofday);
486 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
487 static int set_rtc_mmss(unsigned long nowtime)
488 {
489 int retval;
491 WARN_ON(irqs_disabled());
493 if (!(xen_start_info.flags & SIF_INITDOMAIN))
494 return 0;
496 /* gets recalled with irq locally disabled */
497 spin_lock_irq(&rtc_lock);
498 if (efi_enabled)
499 retval = efi_set_rtc_mmss(nowtime);
500 else
501 retval = mach_set_rtc_mmss(nowtime);
502 spin_unlock_irq(&rtc_lock);
504 return retval;
505 }
506 #else
507 static int set_rtc_mmss(unsigned long nowtime)
508 {
509 return 0;
510 }
511 #endif
513 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
514 * Note: This function is required to return accurate
515 * time even in the absence of multiple timer ticks.
516 */
517 unsigned long long monotonic_clock(void)
518 {
519 int cpu = get_cpu();
520 struct shadow_time_info *shadow = &per_cpu(shadow_time, cpu);
521 u64 time;
522 u32 local_time_version;
524 do {
525 local_time_version = shadow->version;
526 smp_rmb();
527 time = shadow->system_timestamp + get_nsec_offset(shadow);
528 if (!time_values_up_to_date(cpu))
529 get_time_values_from_xen();
530 smp_rmb();
531 } while (local_time_version != shadow->version);
533 put_cpu();
535 return time;
536 }
537 EXPORT_SYMBOL(monotonic_clock);
539 unsigned long long sched_clock(void)
540 {
541 return monotonic_clock();
542 }
544 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
545 unsigned long profile_pc(struct pt_regs *regs)
546 {
547 unsigned long pc = instruction_pointer(regs);
549 if (in_lock_functions(pc))
550 return *(unsigned long *)(regs->ebp + 4);
552 return pc;
553 }
554 EXPORT_SYMBOL(profile_pc);
555 #endif
557 /*
558 * timer_interrupt() needs to keep up the real-time clock,
559 * as well as call the "do_timer()" routine every clocktick
560 */
561 static inline void do_timer_interrupt(int irq, void *dev_id,
562 struct pt_regs *regs)
563 {
564 s64 delta, delta_cpu;
565 int cpu = smp_processor_id();
566 struct shadow_time_info *shadow = &per_cpu(shadow_time, cpu);
568 do {
569 get_time_values_from_xen();
571 delta = delta_cpu =
572 shadow->system_timestamp + get_nsec_offset(shadow);
573 delta -= processed_system_time;
574 delta_cpu -= per_cpu(processed_system_time, cpu);
575 }
576 while (!time_values_up_to_date(cpu));
578 if (unlikely(delta < (s64)-1000000) || unlikely(delta_cpu < 0)) {
579 printk("Timer ISR/%d: Time went backwards: "
580 "delta=%lld cpu_delta=%lld shadow=%lld "
581 "off=%lld processed=%lld cpu_processed=%lld\n",
582 cpu, delta, delta_cpu, shadow->system_timestamp,
583 (s64)get_nsec_offset(shadow),
584 processed_system_time,
585 per_cpu(processed_system_time, cpu));
586 for (cpu = 0; cpu < num_online_cpus(); cpu++)
587 printk(" %d: %lld\n", cpu,
588 per_cpu(processed_system_time, cpu));
589 return;
590 }
592 /* System-wide jiffy work. */
593 while (delta >= NS_PER_TICK) {
594 delta -= NS_PER_TICK;
595 processed_system_time += NS_PER_TICK;
596 do_timer(regs);
597 }
599 /* Local CPU jiffy work. */
600 while (delta_cpu >= NS_PER_TICK) {
601 delta_cpu -= NS_PER_TICK;
602 per_cpu(processed_system_time, cpu) += NS_PER_TICK;
603 update_process_times(user_mode(regs));
604 profile_tick(CPU_PROFILING, regs);
605 }
607 if (shadow_tv_version != HYPERVISOR_shared_info->wc_version) {
608 update_wallclock();
609 clock_was_set();
610 }
611 }
613 /*
614 * This is the same as the above, except we _also_ save the current
615 * Time Stamp Counter value at the time of the timer interrupt, so that
616 * we later on can estimate the time of day more exactly.
617 */
618 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
619 {
620 /*
621 * Here we are in the timer irq handler. We just have irqs locally
622 * disabled but we don't know if the timer_bh is running on the other
623 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
624 * the irq version of write_lock because as just said we have irq
625 * locally disabled. -arca
626 */
627 write_seqlock(&xtime_lock);
628 do_timer_interrupt(irq, NULL, regs);
629 write_sequnlock(&xtime_lock);
630 return IRQ_HANDLED;
631 }
633 /* not static: needed by APM */
634 unsigned long get_cmos_time(void)
635 {
636 unsigned long retval;
638 spin_lock(&rtc_lock);
640 if (efi_enabled)
641 retval = efi_get_time();
642 else
643 retval = mach_get_cmos_time();
645 spin_unlock(&rtc_lock);
647 return retval;
648 }
649 static void sync_cmos_clock(unsigned long dummy);
651 static struct timer_list sync_cmos_timer =
652 TIMER_INITIALIZER(sync_cmos_clock, 0, 0);
654 static void sync_cmos_clock(unsigned long dummy)
655 {
656 struct timeval now, next;
657 int fail = 1;
659 /*
660 * If we have an externally synchronized Linux clock, then update
661 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
662 * called as close as possible to 500 ms before the new second starts.
663 * This code is run on a timer. If the clock is set, that timer
664 * may not expire at the correct time. Thus, we adjust...
665 */
666 if ((time_status & STA_UNSYNC) != 0)
667 /*
668 * Not synced, exit, do not restart a timer (if one is
669 * running, let it run out).
670 */
671 return;
673 do_gettimeofday(&now);
674 if (now.tv_usec >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
675 now.tv_usec <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2)
676 fail = set_rtc_mmss(now.tv_sec);
678 next.tv_usec = USEC_AFTER - now.tv_usec;
679 if (next.tv_usec <= 0)
680 next.tv_usec += USEC_PER_SEC;
682 if (!fail)
683 next.tv_sec = 659;
684 else
685 next.tv_sec = 0;
687 if (next.tv_usec >= USEC_PER_SEC) {
688 next.tv_sec++;
689 next.tv_usec -= USEC_PER_SEC;
690 }
691 mod_timer(&sync_cmos_timer, jiffies + timeval_to_jiffies(&next));
692 }
694 void notify_arch_cmos_timer(void)
695 {
696 mod_timer(&sync_cmos_timer, jiffies + 1);
697 }
699 static long clock_cmos_diff, sleep_start;
701 static int timer_suspend(struct sys_device *dev, pm_message_t state)
702 {
703 /*
704 * Estimate time zone so that set_time can update the clock
705 */
706 clock_cmos_diff = -get_cmos_time();
707 clock_cmos_diff += get_seconds();
708 sleep_start = get_cmos_time();
709 return 0;
710 }
712 static int timer_resume(struct sys_device *dev)
713 {
714 unsigned long flags;
715 unsigned long sec;
716 unsigned long sleep_length;
718 #ifdef CONFIG_HPET_TIMER
719 if (is_hpet_enabled())
720 hpet_reenable();
721 #endif
722 sec = get_cmos_time() + clock_cmos_diff;
723 sleep_length = (get_cmos_time() - sleep_start) * HZ;
724 write_seqlock_irqsave(&xtime_lock, flags);
725 xtime.tv_sec = sec;
726 xtime.tv_nsec = 0;
727 write_sequnlock_irqrestore(&xtime_lock, flags);
728 jiffies += sleep_length;
729 wall_jiffies += sleep_length;
730 return 0;
731 }
733 static struct sysdev_class timer_sysclass = {
734 .resume = timer_resume,
735 .suspend = timer_suspend,
736 set_kset_name("timer"),
737 };
740 /* XXX this driverfs stuff should probably go elsewhere later -john */
741 static struct sys_device device_timer = {
742 .id = 0,
743 .cls = &timer_sysclass,
744 };
746 static int time_init_device(void)
747 {
748 int error = sysdev_class_register(&timer_sysclass);
749 if (!error)
750 error = sysdev_register(&device_timer);
751 return error;
752 }
754 device_initcall(time_init_device);
756 #ifdef CONFIG_HPET_TIMER
757 extern void (*late_time_init)(void);
758 /* Duplicate of time_init() below, with hpet_enable part added */
759 static void __init hpet_time_init(void)
760 {
761 xtime.tv_sec = get_cmos_time();
762 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
763 set_normalized_timespec(&wall_to_monotonic,
764 -xtime.tv_sec, -xtime.tv_nsec);
766 if ((hpet_enable() >= 0) && hpet_use_timer) {
767 printk("Using HPET for base-timer\n");
768 }
770 cur_timer = select_timer();
771 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
773 time_init_hook();
774 }
775 #endif
777 /* Dynamically-mapped IRQ. */
778 static DEFINE_PER_CPU(int, timer_irq);
780 static struct irqaction irq_timer = {
781 timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer0",
782 NULL, NULL
783 };
785 void __init time_init(void)
786 {
787 #ifdef CONFIG_HPET_TIMER
788 if (is_hpet_capable()) {
789 /*
790 * HPET initialization needs to do memory-mapped io. So, let
791 * us do a late initialization after mem_init().
792 */
793 late_time_init = hpet_time_init;
794 return;
795 }
796 #endif
797 get_time_values_from_xen();
799 processed_system_time = per_cpu(shadow_time, 0).system_timestamp;
800 per_cpu(processed_system_time, 0) = processed_system_time;
802 update_wallclock();
804 init_cpu_khz();
805 printk(KERN_INFO "Xen reported: %lu.%03lu MHz processor.\n",
806 cpu_khz / 1000, cpu_khz % 1000);
808 #if defined(__x86_64__)
809 vxtime.mode = VXTIME_TSC;
810 vxtime.quot = (1000000L << 32) / vxtime_hz;
811 vxtime.tsc_quot = (1000L << 32) / cpu_khz;
812 vxtime.hz = vxtime_hz;
813 sync_core();
814 rdtscll(vxtime.last_tsc);
815 #endif
817 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
818 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
819 }
821 /* Convert jiffies to system time. */
822 static inline u64 jiffies_to_st(unsigned long j)
823 {
824 unsigned long seq;
825 long delta;
826 u64 st;
828 do {
829 seq = read_seqbegin(&xtime_lock);
830 delta = j - jiffies;
831 /* NB. The next check can trigger in some wrap-around cases,
832 * but that's ok: we'll just end up with a shorter timeout. */
833 if (delta < 1)
834 delta = 1;
835 st = processed_system_time + (delta * NS_PER_TICK);
836 } while (read_seqretry(&xtime_lock, seq));
838 return st;
839 }
841 /*
842 * stop_hz_timer / start_hz_timer - enter/exit 'tickless mode' on an idle cpu
843 * These functions are based on implementations from arch/s390/kernel/time.c
844 */
845 void stop_hz_timer(void)
846 {
847 unsigned int cpu = smp_processor_id();
848 unsigned long j;
850 /* s390 does this /before/ checking rcu_pending(). We copy them. */
851 cpu_set(cpu, nohz_cpu_mask);
853 /* Leave ourselves in 'tick mode' if rcu or softirq pending. */
854 if (rcu_pending(cpu) || local_softirq_pending()) {
855 cpu_clear(cpu, nohz_cpu_mask);
856 j = jiffies + 1;
857 } else {
858 j = next_timer_interrupt();
859 }
861 BUG_ON(HYPERVISOR_set_timer_op(jiffies_to_st(j)) != 0);
862 }
864 void start_hz_timer(void)
865 {
866 cpu_clear(smp_processor_id(), nohz_cpu_mask);
867 }
869 void time_suspend(void)
870 {
871 /* nothing */
872 teardown_irq(per_cpu(timer_irq, 0), &irq_timer);
873 unbind_virq_from_irq(VIRQ_TIMER);
874 }
876 /* No locking required. We are only CPU running, and interrupts are off. */
877 void time_resume(void)
878 {
879 init_cpu_khz();
881 get_time_values_from_xen();
883 processed_system_time = per_cpu(shadow_time, 0).system_timestamp;
884 per_cpu(processed_system_time, 0) = processed_system_time;
886 update_wallclock();
888 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
889 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
890 }
892 #ifdef CONFIG_SMP
893 static char timer_name[NR_CPUS][15];
894 void local_setup_timer_irq(void)
895 {
896 int cpu = smp_processor_id();
898 if (cpu == 0)
899 return;
900 per_cpu(timer_irq, cpu) = bind_virq_to_irq(VIRQ_TIMER);
901 sprintf(timer_name[cpu], "timer%d", cpu);
902 BUG_ON(request_irq(per_cpu(timer_irq, cpu), timer_interrupt,
903 SA_INTERRUPT, timer_name[cpu], NULL));
904 }
906 void local_setup_timer(void)
907 {
908 int seq, cpu = smp_processor_id();
910 do {
911 seq = read_seqbegin(&xtime_lock);
912 per_cpu(processed_system_time, cpu) =
913 per_cpu(shadow_time, cpu).system_timestamp;
914 } while (read_seqretry(&xtime_lock, seq));
916 local_setup_timer_irq();
917 }
919 void local_teardown_timer_irq(void)
920 {
921 int cpu = smp_processor_id();
923 if (cpu == 0)
924 return;
925 free_irq(per_cpu(timer_irq, cpu), NULL);
926 unbind_virq_from_irq(VIRQ_TIMER);
927 }
928 #endif
930 /*
931 * /proc/sys/xen: This really belongs in another file. It can stay here for
932 * now however.
933 */
934 static ctl_table xen_subtable[] = {
935 {1, "independent_wallclock", &independent_wallclock,
936 sizeof(independent_wallclock), 0644, NULL, proc_dointvec},
937 {0}
938 };
939 static ctl_table xen_table[] = {
940 {123, "xen", NULL, 0, 0555, xen_subtable},
941 {0}
942 };
943 static int __init xen_sysctl_init(void)
944 {
945 (void)register_sysctl_table(xen_table, 0);
946 return 0;
947 }
948 __initcall(xen_sysctl_init);
950 /*
951 * Local variables:
952 * c-file-style: "linux"
953 * indent-tabs-mode: t
954 * c-indent-level: 8
955 * c-basic-offset: 8
956 * tab-width: 8
957 * End:
958 */