ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/time.c @ 7124:f069a06e650f

Fix fixed-point long multiplication used in time delta
calculations. The carry flag was clobbered before it was
added to the result.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Sep 28 22:18:30 2005 +0100 (2005-09-28)
parents 06d84bf87159
children 805ee053e61f
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 "xor %5,%5 ; "
190 "add %4,%%eax ; "
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 u64 tmp, wc_nsec;
236 /* Adjust wall-clock time base based on wall_jiffies ticks. */
237 wc_nsec = processed_system_time;
238 wc_nsec += (u64)sec * 1000000000ULL;
239 wc_nsec += (u64)nsec;
240 wc_nsec -= (jiffies - wall_jiffies) * (u64)(NSEC_PER_SEC / HZ);
242 /* Split wallclock base into seconds and nanoseconds. */
243 tmp = wc_nsec;
244 xtime_nsec = do_div(tmp, 1000000000);
245 xtime_sec = (time_t)tmp;
247 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - xtime_sec);
248 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - xtime_nsec);
250 set_normalized_timespec(&xtime, xtime_sec, xtime_nsec);
251 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
253 time_adjust = 0; /* stop active adjtime() */
254 time_status |= STA_UNSYNC;
255 time_maxerror = NTP_PHASE_LIMIT;
256 time_esterror = NTP_PHASE_LIMIT;
257 }
259 static void update_wallclock(void)
260 {
261 shared_info_t *s = HYPERVISOR_shared_info;
263 do {
264 shadow_tv_version = s->wc_version;
265 rmb();
266 shadow_tv.tv_sec = s->wc_sec;
267 shadow_tv.tv_nsec = s->wc_nsec;
268 rmb();
269 }
270 while ((s->wc_version & 1) | (shadow_tv_version ^ s->wc_version));
272 if (!independent_wallclock)
273 __update_wallclock(shadow_tv.tv_sec, shadow_tv.tv_nsec);
274 }
276 /*
277 * Reads a consistent set of time-base values from Xen, into a shadow data
278 * area.
279 */
280 static void get_time_values_from_xen(void)
281 {
282 shared_info_t *s = HYPERVISOR_shared_info;
283 struct vcpu_time_info *src;
284 struct shadow_time_info *dst;
286 src = &s->vcpu_time[smp_processor_id()];
287 dst = &per_cpu(shadow_time, smp_processor_id());
289 do {
290 dst->version = src->version;
291 rmb();
292 dst->tsc_timestamp = src->tsc_timestamp;
293 dst->system_timestamp = src->system_time;
294 dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
295 dst->tsc_shift = src->tsc_shift;
296 rmb();
297 }
298 while ((src->version & 1) | (dst->version ^ src->version));
300 dst->tsc_to_usec_mul = dst->tsc_to_nsec_mul / 1000;
301 }
303 static inline int time_values_up_to_date(int cpu)
304 {
305 struct vcpu_time_info *src;
306 struct shadow_time_info *dst;
308 src = &HYPERVISOR_shared_info->vcpu_time[cpu];
309 dst = &per_cpu(shadow_time, cpu);
311 return (dst->version == src->version);
312 }
314 /*
315 * This is a special lock that is owned by the CPU and holds the index
316 * register we are working with. It is required for NMI access to the
317 * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
318 */
319 volatile unsigned long cmos_lock = 0;
320 EXPORT_SYMBOL(cmos_lock);
322 /* Routines for accessing the CMOS RAM/RTC. */
323 unsigned char rtc_cmos_read(unsigned char addr)
324 {
325 unsigned char val;
326 lock_cmos_prefix(addr);
327 outb_p(addr, RTC_PORT(0));
328 val = inb_p(RTC_PORT(1));
329 lock_cmos_suffix(addr);
330 return val;
331 }
332 EXPORT_SYMBOL(rtc_cmos_read);
334 void rtc_cmos_write(unsigned char val, unsigned char addr)
335 {
336 lock_cmos_prefix(addr);
337 outb_p(addr, RTC_PORT(0));
338 outb_p(val, RTC_PORT(1));
339 lock_cmos_suffix(addr);
340 }
341 EXPORT_SYMBOL(rtc_cmos_write);
343 /*
344 * This version of gettimeofday has microsecond resolution
345 * and better than microsecond precision on fast x86 machines with TSC.
346 */
347 void do_gettimeofday(struct timeval *tv)
348 {
349 unsigned long seq;
350 unsigned long usec, sec;
351 unsigned long max_ntp_tick;
352 s64 nsec;
353 unsigned int cpu;
354 struct shadow_time_info *shadow;
355 u32 local_time_version;
357 cpu = get_cpu();
358 shadow = &per_cpu(shadow_time, cpu);
360 do {
361 unsigned long lost;
363 local_time_version = shadow->version;
364 seq = read_seqbegin(&xtime_lock);
366 usec = get_usec_offset(shadow);
367 lost = jiffies - wall_jiffies;
369 /*
370 * If time_adjust is negative then NTP is slowing the clock
371 * so make sure not to go into next possible interval.
372 * Better to lose some accuracy than have time go backwards..
373 */
374 if (unlikely(time_adjust < 0)) {
375 max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
376 usec = min(usec, max_ntp_tick);
378 if (lost)
379 usec += lost * max_ntp_tick;
380 }
381 else if (unlikely(lost))
382 usec += lost * (USEC_PER_SEC / HZ);
384 sec = xtime.tv_sec;
385 usec += (xtime.tv_nsec / NSEC_PER_USEC);
387 nsec = shadow->system_timestamp - processed_system_time;
388 __normalize_time(&sec, &nsec);
389 usec += (long)nsec / NSEC_PER_USEC;
391 if (unlikely(!time_values_up_to_date(cpu))) {
392 /*
393 * We may have blocked for a long time,
394 * rendering our calculations invalid
395 * (e.g. the time delta may have
396 * overflowed). Detect that and recalculate
397 * with fresh values.
398 */
399 get_time_values_from_xen();
400 continue;
401 }
402 } while (read_seqretry(&xtime_lock, seq) ||
403 (local_time_version != shadow->version));
405 put_cpu();
407 while (usec >= USEC_PER_SEC) {
408 usec -= USEC_PER_SEC;
409 sec++;
410 }
412 tv->tv_sec = sec;
413 tv->tv_usec = usec;
414 }
416 EXPORT_SYMBOL(do_gettimeofday);
418 int do_settimeofday(struct timespec *tv)
419 {
420 time_t sec;
421 s64 nsec;
422 unsigned int cpu;
423 struct shadow_time_info *shadow;
424 dom0_op_t op;
426 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
427 return -EINVAL;
429 cpu = get_cpu();
430 shadow = &per_cpu(shadow_time, cpu);
432 write_seqlock_irq(&xtime_lock);
434 /*
435 * Ensure we don't get blocked for a long time so that our time delta
436 * overflows. If that were to happen then our shadow time values would
437 * be stale, so we can retry with fresh ones.
438 */
439 for ( ; ; ) {
440 nsec = (s64)tv->tv_nsec - (s64)get_nsec_offset(shadow);
441 if (time_values_up_to_date(cpu))
442 break;
443 get_time_values_from_xen();
444 }
445 sec = tv->tv_sec;
446 __normalize_time(&sec, &nsec);
448 if ((xen_start_info->flags & SIF_INITDOMAIN) &&
449 !independent_wallclock) {
450 op.cmd = DOM0_SETTIME;
451 op.u.settime.secs = sec;
452 op.u.settime.nsecs = nsec;
453 op.u.settime.system_time = shadow->system_timestamp;
454 HYPERVISOR_dom0_op(&op);
455 update_wallclock();
456 } else if (independent_wallclock) {
457 nsec -= shadow->system_timestamp;
458 __normalize_time(&sec, &nsec);
459 __update_wallclock(sec, nsec);
460 }
462 write_sequnlock_irq(&xtime_lock);
464 put_cpu();
466 clock_was_set();
467 return 0;
468 }
470 EXPORT_SYMBOL(do_settimeofday);
472 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
473 static int set_rtc_mmss(unsigned long nowtime)
474 {
475 int retval;
477 WARN_ON(irqs_disabled());
479 if (!(xen_start_info->flags & SIF_INITDOMAIN))
480 return 0;
482 /* gets recalled with irq locally disabled */
483 spin_lock_irq(&rtc_lock);
484 if (efi_enabled)
485 retval = efi_set_rtc_mmss(nowtime);
486 else
487 retval = mach_set_rtc_mmss(nowtime);
488 spin_unlock_irq(&rtc_lock);
490 return retval;
491 }
492 #else
493 static int set_rtc_mmss(unsigned long nowtime)
494 {
495 return 0;
496 }
497 #endif
499 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
500 * Note: This function is required to return accurate
501 * time even in the absence of multiple timer ticks.
502 */
503 unsigned long long monotonic_clock(void)
504 {
505 int cpu = get_cpu();
506 struct shadow_time_info *shadow = &per_cpu(shadow_time, cpu);
507 u64 time;
508 u32 local_time_version;
510 do {
511 local_time_version = shadow->version;
512 smp_rmb();
513 time = shadow->system_timestamp + get_nsec_offset(shadow);
514 if (!time_values_up_to_date(cpu))
515 get_time_values_from_xen();
516 smp_rmb();
517 } while (local_time_version != shadow->version);
519 put_cpu();
521 return time;
522 }
523 EXPORT_SYMBOL(monotonic_clock);
525 unsigned long long sched_clock(void)
526 {
527 return monotonic_clock();
528 }
530 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
531 unsigned long profile_pc(struct pt_regs *regs)
532 {
533 unsigned long pc = instruction_pointer(regs);
535 if (in_lock_functions(pc))
536 return *(unsigned long *)(regs->ebp + 4);
538 return pc;
539 }
540 EXPORT_SYMBOL(profile_pc);
541 #endif
543 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
544 {
545 s64 delta, delta_cpu;
546 int i, cpu = smp_processor_id();
547 struct shadow_time_info *shadow = &per_cpu(shadow_time, cpu);
549 write_seqlock(&xtime_lock);
551 do {
552 get_time_values_from_xen();
554 delta = delta_cpu =
555 shadow->system_timestamp + get_nsec_offset(shadow);
556 delta -= processed_system_time;
557 delta_cpu -= per_cpu(processed_system_time, cpu);
558 }
559 while (!time_values_up_to_date(cpu));
561 if (unlikely(delta < (s64)-1000000) || unlikely(delta_cpu < 0)) {
562 printk("Timer ISR/%d: Time went backwards: "
563 "delta=%lld cpu_delta=%lld shadow=%lld "
564 "off=%lld processed=%lld cpu_processed=%lld\n",
565 cpu, delta, delta_cpu, shadow->system_timestamp,
566 (s64)get_nsec_offset(shadow),
567 processed_system_time,
568 per_cpu(processed_system_time, cpu));
569 for (i = 0; i < num_online_cpus(); i++)
570 printk(" %d: %lld\n", i,
571 per_cpu(processed_system_time, i));
572 }
574 /* System-wide jiffy work. */
575 while (delta >= NS_PER_TICK) {
576 delta -= NS_PER_TICK;
577 processed_system_time += NS_PER_TICK;
578 do_timer(regs);
579 }
581 if (shadow_tv_version != HYPERVISOR_shared_info->wc_version) {
582 update_wallclock();
583 clock_was_set();
584 }
586 write_sequnlock(&xtime_lock);
588 /*
589 * Local CPU jiffy work. No need to hold xtime_lock, and I'm not sure
590 * if there is risk of deadlock if we do (since update_process_times
591 * may do scheduler rebalancing work and thus acquire runqueue locks).
592 */
593 while (delta_cpu >= NS_PER_TICK) {
594 delta_cpu -= NS_PER_TICK;
595 per_cpu(processed_system_time, cpu) += NS_PER_TICK;
596 update_process_times(user_mode(regs));
597 profile_tick(CPU_PROFILING, regs);
598 }
600 return IRQ_HANDLED;
601 }
603 /* not static: needed by APM */
604 unsigned long get_cmos_time(void)
605 {
606 unsigned long retval;
608 spin_lock(&rtc_lock);
610 if (efi_enabled)
611 retval = efi_get_time();
612 else
613 retval = mach_get_cmos_time();
615 spin_unlock(&rtc_lock);
617 return retval;
618 }
619 static void sync_cmos_clock(unsigned long dummy);
621 static struct timer_list sync_cmos_timer =
622 TIMER_INITIALIZER(sync_cmos_clock, 0, 0);
624 static void sync_cmos_clock(unsigned long dummy)
625 {
626 struct timeval now, next;
627 int fail = 1;
629 /*
630 * If we have an externally synchronized Linux clock, then update
631 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
632 * called as close as possible to 500 ms before the new second starts.
633 * This code is run on a timer. If the clock is set, that timer
634 * may not expire at the correct time. Thus, we adjust...
635 */
636 if ((time_status & STA_UNSYNC) != 0)
637 /*
638 * Not synced, exit, do not restart a timer (if one is
639 * running, let it run out).
640 */
641 return;
643 do_gettimeofday(&now);
644 if (now.tv_usec >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
645 now.tv_usec <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2)
646 fail = set_rtc_mmss(now.tv_sec);
648 next.tv_usec = USEC_AFTER - now.tv_usec;
649 if (next.tv_usec <= 0)
650 next.tv_usec += USEC_PER_SEC;
652 if (!fail)
653 next.tv_sec = 659;
654 else
655 next.tv_sec = 0;
657 if (next.tv_usec >= USEC_PER_SEC) {
658 next.tv_sec++;
659 next.tv_usec -= USEC_PER_SEC;
660 }
661 mod_timer(&sync_cmos_timer, jiffies + timeval_to_jiffies(&next));
662 }
664 void notify_arch_cmos_timer(void)
665 {
666 mod_timer(&sync_cmos_timer, jiffies + 1);
667 }
669 static long clock_cmos_diff, sleep_start;
671 static int timer_suspend(struct sys_device *dev, pm_message_t state)
672 {
673 /*
674 * Estimate time zone so that set_time can update the clock
675 */
676 clock_cmos_diff = -get_cmos_time();
677 clock_cmos_diff += get_seconds();
678 sleep_start = get_cmos_time();
679 return 0;
680 }
682 static int timer_resume(struct sys_device *dev)
683 {
684 unsigned long flags;
685 unsigned long sec;
686 unsigned long sleep_length;
688 #ifdef CONFIG_HPET_TIMER
689 if (is_hpet_enabled())
690 hpet_reenable();
691 #endif
692 sec = get_cmos_time() + clock_cmos_diff;
693 sleep_length = (get_cmos_time() - sleep_start) * HZ;
694 write_seqlock_irqsave(&xtime_lock, flags);
695 xtime.tv_sec = sec;
696 xtime.tv_nsec = 0;
697 write_sequnlock_irqrestore(&xtime_lock, flags);
698 jiffies += sleep_length;
699 wall_jiffies += sleep_length;
700 return 0;
701 }
703 static struct sysdev_class timer_sysclass = {
704 .resume = timer_resume,
705 .suspend = timer_suspend,
706 set_kset_name("timer"),
707 };
710 /* XXX this driverfs stuff should probably go elsewhere later -john */
711 static struct sys_device device_timer = {
712 .id = 0,
713 .cls = &timer_sysclass,
714 };
716 static int time_init_device(void)
717 {
718 int error = sysdev_class_register(&timer_sysclass);
719 if (!error)
720 error = sysdev_register(&device_timer);
721 return error;
722 }
724 device_initcall(time_init_device);
726 #ifdef CONFIG_HPET_TIMER
727 extern void (*late_time_init)(void);
728 /* Duplicate of time_init() below, with hpet_enable part added */
729 static void __init hpet_time_init(void)
730 {
731 xtime.tv_sec = get_cmos_time();
732 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
733 set_normalized_timespec(&wall_to_monotonic,
734 -xtime.tv_sec, -xtime.tv_nsec);
736 if ((hpet_enable() >= 0) && hpet_use_timer) {
737 printk("Using HPET for base-timer\n");
738 }
740 cur_timer = select_timer();
741 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
743 time_init_hook();
744 }
745 #endif
747 /* Dynamically-mapped IRQ. */
748 DEFINE_PER_CPU(int, timer_irq);
750 static struct irqaction irq_timer = {
751 timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer0",
752 NULL, NULL
753 };
755 void __init time_init(void)
756 {
757 #ifdef CONFIG_HPET_TIMER
758 if (is_hpet_capable()) {
759 /*
760 * HPET initialization needs to do memory-mapped io. So, let
761 * us do a late initialization after mem_init().
762 */
763 late_time_init = hpet_time_init;
764 return;
765 }
766 #endif
767 get_time_values_from_xen();
769 processed_system_time = per_cpu(shadow_time, 0).system_timestamp;
770 per_cpu(processed_system_time, 0) = processed_system_time;
772 update_wallclock();
774 init_cpu_khz();
775 printk(KERN_INFO "Xen reported: %lu.%03lu MHz processor.\n",
776 cpu_khz / 1000, cpu_khz % 1000);
778 #if defined(__x86_64__)
779 vxtime.mode = VXTIME_TSC;
780 vxtime.quot = (1000000L << 32) / vxtime_hz;
781 vxtime.tsc_quot = (1000L << 32) / cpu_khz;
782 vxtime.hz = vxtime_hz;
783 sync_core();
784 rdtscll(vxtime.last_tsc);
785 #endif
787 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
788 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
789 }
791 /* Convert jiffies to system time. */
792 static inline u64 jiffies_to_st(unsigned long j)
793 {
794 unsigned long seq;
795 long delta;
796 u64 st;
798 do {
799 seq = read_seqbegin(&xtime_lock);
800 delta = j - jiffies;
801 /* NB. The next check can trigger in some wrap-around cases,
802 * but that's ok: we'll just end up with a shorter timeout. */
803 if (delta < 1)
804 delta = 1;
805 st = processed_system_time + (delta * NS_PER_TICK);
806 } while (read_seqretry(&xtime_lock, seq));
808 return st;
809 }
811 /*
812 * stop_hz_timer / start_hz_timer - enter/exit 'tickless mode' on an idle cpu
813 * These functions are based on implementations from arch/s390/kernel/time.c
814 */
815 void stop_hz_timer(void)
816 {
817 unsigned int cpu = smp_processor_id();
818 unsigned long j;
820 /* s390 does this /before/ checking rcu_pending(). We copy them. */
821 cpu_set(cpu, nohz_cpu_mask);
823 /* Leave ourselves in 'tick mode' if rcu or softirq pending. */
824 if (rcu_pending(cpu) || local_softirq_pending()) {
825 cpu_clear(cpu, nohz_cpu_mask);
826 j = jiffies + 1;
827 } else {
828 j = next_timer_interrupt();
829 }
831 BUG_ON(HYPERVISOR_set_timer_op(jiffies_to_st(j)) != 0);
832 }
834 void start_hz_timer(void)
835 {
836 cpu_clear(smp_processor_id(), nohz_cpu_mask);
837 }
839 void time_suspend(void)
840 {
841 /* nothing */
842 teardown_irq(per_cpu(timer_irq, 0), &irq_timer);
843 unbind_virq_from_irq(VIRQ_TIMER);
844 }
846 /* No locking required. We are only CPU running, and interrupts are off. */
847 void time_resume(void)
848 {
849 init_cpu_khz();
851 get_time_values_from_xen();
853 processed_system_time = per_cpu(shadow_time, 0).system_timestamp;
854 per_cpu(processed_system_time, 0) = processed_system_time;
856 update_wallclock();
858 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
859 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
860 }
862 #ifdef CONFIG_SMP
863 static char timer_name[NR_CPUS][15];
864 void local_setup_timer_irq(void)
865 {
866 int cpu = smp_processor_id();
868 if (cpu == 0)
869 return;
870 per_cpu(timer_irq, cpu) = bind_virq_to_irq(VIRQ_TIMER);
871 sprintf(timer_name[cpu], "timer%d", cpu);
872 BUG_ON(request_irq(per_cpu(timer_irq, cpu), timer_interrupt,
873 SA_INTERRUPT, timer_name[cpu], NULL));
874 }
876 void local_setup_timer(void)
877 {
878 int seq, cpu = smp_processor_id();
880 do {
881 seq = read_seqbegin(&xtime_lock);
882 per_cpu(processed_system_time, cpu) =
883 per_cpu(shadow_time, cpu).system_timestamp;
884 } while (read_seqretry(&xtime_lock, seq));
886 local_setup_timer_irq();
887 }
889 void local_teardown_timer_irq(void)
890 {
891 int cpu = smp_processor_id();
893 if (cpu == 0)
894 return;
895 free_irq(per_cpu(timer_irq, cpu), NULL);
896 unbind_virq_from_irq(VIRQ_TIMER);
897 }
898 #endif
900 /*
901 * /proc/sys/xen: This really belongs in another file. It can stay here for
902 * now however.
903 */
904 static ctl_table xen_subtable[] = {
905 {1, "independent_wallclock", &independent_wallclock,
906 sizeof(independent_wallclock), 0644, NULL, proc_dointvec},
907 {0}
908 };
909 static ctl_table xen_table[] = {
910 {123, "xen", NULL, 0, 0555, xen_subtable},
911 {0}
912 };
913 static int __init xen_sysctl_init(void)
914 {
915 (void)register_sysctl_table(xen_table, 0);
916 return 0;
917 }
918 __initcall(xen_sysctl_init);
920 /*
921 * Local variables:
922 * c-file-style: "linux"
923 * indent-tabs-mode: t
924 * c-indent-level: 8
925 * c-basic-offset: 8
926 * tab-width: 8
927 * End:
928 */