ia64/xen-unstable

view linux-2.6.11-xen-sparse/arch/xen/i386/kernel/time.c @ 5076:d55569f2ac18

bitkeeper revision 1.1505 (428f2c4c-dG46JqogSF24go6sxrccw)

Merge firebug.cl.cam.ac.uk:/auto/groups/xeno-xenod/BK/xen-2.0-testing.bk
into firebug.cl.cam.ac.uk:/local/scratch/cl349/xen-unstable.bk
author cl349@firebug.cl.cam.ac.uk
date Sat May 21 12:40:44 2005 +0000 (2005-05-21)
parents 56971ab9b0bb 543ffea3fc12
children da85abbfc7c0
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 extern spinlock_t i8259A_lock;
74 int pit_latch_buggy; /* extern */
76 u64 jiffies_64 = INITIAL_JIFFIES;
78 EXPORT_SYMBOL(jiffies_64);
80 #if defined(__x86_64__)
81 unsigned long vxtime_hz = PIT_TICK_RATE;
82 struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */
83 volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
84 unsigned long __wall_jiffies __section_wall_jiffies = INITIAL_JIFFIES;
85 struct timespec __xtime __section_xtime;
86 struct timezone __sys_tz __section_sys_tz;
87 #endif
89 #if defined(__x86_64__)
90 unsigned int cpu_khz; /* Detected as we calibrate the TSC */
91 #else
92 unsigned long cpu_khz; /* Detected as we calibrate the TSC */
93 #endif
95 extern unsigned long wall_jiffies;
97 DEFINE_SPINLOCK(rtc_lock);
99 DEFINE_SPINLOCK(i8253_lock);
100 EXPORT_SYMBOL(i8253_lock);
102 extern struct init_timer_opts timer_tsc_init;
103 extern struct timer_opts timer_tsc;
104 struct timer_opts *cur_timer = &timer_tsc;
106 /* These are peridically updated in shared_info, and then copied here. */
107 u32 shadow_tsc_stamp;
108 u64 shadow_system_time;
109 static u32 shadow_time_version;
110 static struct timeval shadow_tv;
112 /*
113 * We use this to ensure that gettimeofday() is monotonically increasing. We
114 * only break this guarantee if the wall clock jumps backwards "a long way".
115 */
116 static struct timeval last_seen_tv = {0,0};
118 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
119 /* Periodically propagate synchronised time base to the RTC and to Xen. */
120 static long last_rtc_update, last_update_to_xen;
121 #endif
123 /* Periodically take synchronised time base from Xen, if we need it. */
124 static long last_update_from_xen; /* UTC seconds when last read Xen clock. */
126 /* Keep track of last time we did processing/updating of jiffies and xtime. */
127 static u64 processed_system_time; /* System time (ns) at last processing. */
128 static DEFINE_PER_CPU(u64, processed_system_time);
130 #define NS_PER_TICK (1000000000ULL/HZ)
132 #define HANDLE_USEC_UNDERFLOW(_tv) do { \
133 while ((_tv).tv_usec < 0) { \
134 (_tv).tv_usec += USEC_PER_SEC; \
135 (_tv).tv_sec--; \
136 } \
137 } while (0)
138 #define HANDLE_USEC_OVERFLOW(_tv) do { \
139 while ((_tv).tv_usec >= USEC_PER_SEC) { \
140 (_tv).tv_usec -= USEC_PER_SEC; \
141 (_tv).tv_sec++; \
142 } \
143 } while (0)
144 static inline void __normalize_time(time_t *sec, s64 *nsec)
145 {
146 while (*nsec >= NSEC_PER_SEC) {
147 (*nsec) -= NSEC_PER_SEC;
148 (*sec)++;
149 }
150 while (*nsec < 0) {
151 (*nsec) += NSEC_PER_SEC;
152 (*sec)--;
153 }
154 }
156 /* Does this guest OS track Xen time, or set its wall clock independently? */
157 static int independent_wallclock = 0;
158 static int __init __independent_wallclock(char *str)
159 {
160 independent_wallclock = 1;
161 return 1;
162 }
163 __setup("independent_wallclock", __independent_wallclock);
164 #define INDEPENDENT_WALLCLOCK() \
165 (independent_wallclock || (xen_start_info.flags & SIF_INITDOMAIN))
167 /*
168 * Reads a consistent set of time-base values from Xen, into a shadow data
169 * area. Must be called with the xtime_lock held for writing.
170 */
171 static void __get_time_values_from_xen(void)
172 {
173 shared_info_t *s = HYPERVISOR_shared_info;
175 do {
176 shadow_time_version = s->time_version2;
177 rmb();
178 shadow_tv.tv_sec = s->wc_sec;
179 shadow_tv.tv_usec = s->wc_usec;
180 shadow_tsc_stamp = (u32)s->tsc_timestamp;
181 shadow_system_time = s->system_time;
182 rmb();
183 }
184 while (shadow_time_version != s->time_version1);
186 cur_timer->mark_offset();
187 }
189 #define TIME_VALUES_UP_TO_DATE \
190 ({ rmb(); (shadow_time_version == HYPERVISOR_shared_info->time_version2); })
192 /*
193 * This version of gettimeofday has microsecond resolution
194 * and better than microsecond precision on fast x86 machines with TSC.
195 */
196 void do_gettimeofday(struct timeval *tv)
197 {
198 unsigned long seq;
199 unsigned long usec, sec;
200 unsigned long max_ntp_tick;
201 unsigned long flags;
202 s64 nsec;
204 do {
205 unsigned long lost;
207 seq = read_seqbegin(&xtime_lock);
209 usec = cur_timer->get_offset();
210 lost = jiffies - wall_jiffies;
212 /*
213 * If time_adjust is negative then NTP is slowing the clock
214 * so make sure not to go into next possible interval.
215 * Better to lose some accuracy than have time go backwards..
216 */
217 if (unlikely(time_adjust < 0)) {
218 max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
219 usec = min(usec, max_ntp_tick);
221 if (lost)
222 usec += lost * max_ntp_tick;
223 }
224 else if (unlikely(lost))
225 usec += lost * (USEC_PER_SEC / HZ);
227 sec = xtime.tv_sec;
228 usec += (xtime.tv_nsec / NSEC_PER_USEC);
230 nsec = shadow_system_time - processed_system_time;
231 __normalize_time(&sec, &nsec);
232 usec += (long)nsec / NSEC_PER_USEC;
234 if (unlikely(!TIME_VALUES_UP_TO_DATE)) {
235 /*
236 * We may have blocked for a long time,
237 * rendering our calculations invalid
238 * (e.g. the time delta may have
239 * overflowed). Detect that and recalculate
240 * with fresh values.
241 */
242 write_seqlock_irqsave(&xtime_lock, flags);
243 __get_time_values_from_xen();
244 write_sequnlock_irqrestore(&xtime_lock, flags);
245 continue;
246 }
247 } while (read_seqretry(&xtime_lock, seq));
249 while (usec >= USEC_PER_SEC) {
250 usec -= USEC_PER_SEC;
251 sec++;
252 }
254 /* Ensure that time-of-day is monotonically increasing. */
255 if ((sec < last_seen_tv.tv_sec) ||
256 ((sec == last_seen_tv.tv_sec) && (usec < last_seen_tv.tv_usec))) {
257 sec = last_seen_tv.tv_sec;
258 usec = last_seen_tv.tv_usec;
259 } else {
260 last_seen_tv.tv_sec = sec;
261 last_seen_tv.tv_usec = usec;
262 }
264 tv->tv_sec = sec;
265 tv->tv_usec = usec;
266 }
268 EXPORT_SYMBOL(do_gettimeofday);
270 int do_settimeofday(struct timespec *tv)
271 {
272 time_t wtm_sec, sec = tv->tv_sec;
273 long wtm_nsec;
274 s64 nsec;
275 struct timespec xentime;
277 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
278 return -EINVAL;
280 if (!INDEPENDENT_WALLCLOCK())
281 return 0; /* Silent failure? */
283 write_seqlock_irq(&xtime_lock);
285 /*
286 * Ensure we don't get blocked for a long time so that our time delta
287 * overflows. If that were to happen then our shadow time values would
288 * be stale, so we can retry with fresh ones.
289 */
290 again:
291 nsec = (s64)tv->tv_nsec -
292 ((s64)cur_timer->get_offset() * (s64)NSEC_PER_USEC);
293 if (unlikely(!TIME_VALUES_UP_TO_DATE)) {
294 __get_time_values_from_xen();
295 goto again;
296 }
298 __normalize_time(&sec, &nsec);
299 set_normalized_timespec(&xentime, sec, nsec);
301 /*
302 * This is revolting. We need to set "xtime" correctly. However, the
303 * value in this location is the value at the most recent update of
304 * wall time. Discover what correction gettimeofday() would have
305 * made, and then undo it!
306 */
307 nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
309 nsec -= (shadow_system_time - processed_system_time);
311 __normalize_time(&sec, &nsec);
312 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
313 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
315 set_normalized_timespec(&xtime, sec, nsec);
316 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
318 time_adjust = 0; /* stop active adjtime() */
319 time_status |= STA_UNSYNC;
320 time_maxerror = NTP_PHASE_LIMIT;
321 time_esterror = NTP_PHASE_LIMIT;
323 /* Reset all our running time counts. They make no sense now. */
324 last_seen_tv.tv_sec = 0;
325 last_update_from_xen = 0;
327 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
328 if (xen_start_info.flags & SIF_INITDOMAIN) {
329 dom0_op_t op;
330 last_rtc_update = last_update_to_xen = 0;
331 op.cmd = DOM0_SETTIME;
332 op.u.settime.secs = xentime.tv_sec;
333 op.u.settime.usecs = xentime.tv_nsec / NSEC_PER_USEC;
334 op.u.settime.system_time = shadow_system_time;
335 write_sequnlock_irq(&xtime_lock);
336 HYPERVISOR_dom0_op(&op);
337 } else
338 #endif
339 write_sequnlock_irq(&xtime_lock);
341 clock_was_set();
342 return 0;
343 }
345 EXPORT_SYMBOL(do_settimeofday);
347 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
348 static int set_rtc_mmss(unsigned long nowtime)
349 {
350 int retval;
352 /* gets recalled with irq locally disabled */
353 spin_lock(&rtc_lock);
354 if (efi_enabled)
355 retval = efi_set_rtc_mmss(nowtime);
356 else
357 retval = mach_set_rtc_mmss(nowtime);
358 spin_unlock(&rtc_lock);
360 return retval;
361 }
362 #endif
364 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
365 * Note: This function is required to return accurate
366 * time even in the absence of multiple timer ticks.
367 */
368 unsigned long long monotonic_clock(void)
369 {
370 return cur_timer->monotonic_clock();
371 }
372 EXPORT_SYMBOL(monotonic_clock);
374 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
375 unsigned long profile_pc(struct pt_regs *regs)
376 {
377 unsigned long pc = instruction_pointer(regs);
379 if (in_lock_functions(pc))
380 return *(unsigned long *)(regs->ebp + 4);
382 return pc;
383 }
384 EXPORT_SYMBOL(profile_pc);
385 #endif
387 /*
388 * timer_interrupt() needs to keep up the real-time clock,
389 * as well as call the "do_timer()" routine every clocktick
390 */
391 static inline void do_timer_interrupt(int irq, void *dev_id,
392 struct pt_regs *regs)
393 {
394 time_t wtm_sec, sec;
395 s64 delta, delta_cpu, nsec;
396 long sec_diff, wtm_nsec;
397 int cpu = smp_processor_id();
399 do {
400 __get_time_values_from_xen();
402 delta = delta_cpu = (s64)shadow_system_time +
403 ((s64)cur_timer->get_offset() * (s64)NSEC_PER_USEC);
404 delta -= processed_system_time;
405 delta_cpu -= per_cpu(processed_system_time, cpu);
406 }
407 while (!TIME_VALUES_UP_TO_DATE);
409 if (unlikely(delta < 0) || unlikely(delta_cpu < 0)) {
410 printk("Timer ISR/%d: Time went backwards: "
411 "delta=%lld cpu_delta=%lld shadow=%lld "
412 "off=%lld processed=%lld cpu_processed=%lld\n",
413 cpu, delta, delta_cpu, shadow_system_time,
414 ((s64)cur_timer->get_offset() * (s64)NSEC_PER_USEC),
415 processed_system_time,
416 per_cpu(processed_system_time, cpu));
417 for (cpu = 0; cpu < num_online_cpus(); cpu++)
418 printk(" %d: %lld\n", cpu,
419 per_cpu(processed_system_time, cpu));
420 return;
421 }
423 /* System-wide jiffy work. */
424 while (delta >= NS_PER_TICK) {
425 delta -= NS_PER_TICK;
426 processed_system_time += NS_PER_TICK;
427 do_timer(regs);
428 }
430 /* Local CPU jiffy work. */
431 while (delta_cpu >= NS_PER_TICK) {
432 delta_cpu -= NS_PER_TICK;
433 per_cpu(processed_system_time, cpu) += NS_PER_TICK;
434 update_process_times(user_mode(regs));
435 profile_tick(CPU_PROFILING, regs);
436 }
438 if (cpu != 0)
439 return;
441 /*
442 * Take synchronised time from Xen once a minute if we're not
443 * synchronised ourselves, and we haven't chosen to keep an independent
444 * time base.
445 */
446 if (!INDEPENDENT_WALLCLOCK() &&
447 ((time_status & STA_UNSYNC) != 0) &&
448 (xtime.tv_sec > (last_update_from_xen + 60))) {
449 /* Adjust shadow for jiffies that haven't updated xtime yet. */
450 shadow_tv.tv_usec -=
451 (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ);
452 HANDLE_USEC_UNDERFLOW(shadow_tv);
454 /*
455 * Reset our running time counts if they are invalidated by
456 * a warp backwards of more than 500ms.
457 */
458 sec_diff = xtime.tv_sec - shadow_tv.tv_sec;
459 if (unlikely(abs(sec_diff) > 1) ||
460 unlikely(((sec_diff * USEC_PER_SEC) +
461 (xtime.tv_nsec / NSEC_PER_USEC) -
462 shadow_tv.tv_usec) > 500000)) {
463 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
464 last_rtc_update = last_update_to_xen = 0;
465 #endif
466 last_seen_tv.tv_sec = 0;
467 }
469 /* Update our unsynchronised xtime appropriately. */
470 sec = shadow_tv.tv_sec;
471 nsec = shadow_tv.tv_usec * NSEC_PER_USEC;
473 __normalize_time(&sec, &nsec);
474 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
475 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
477 set_normalized_timespec(&xtime, sec, nsec);
478 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
480 last_update_from_xen = sec;
481 }
483 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
484 if (!(xen_start_info.flags & SIF_INITDOMAIN))
485 return;
487 /* Send synchronised time to Xen approximately every minute. */
488 if (((time_status & STA_UNSYNC) == 0) &&
489 (xtime.tv_sec > (last_update_to_xen + 60))) {
490 dom0_op_t op;
491 struct timeval tv;
493 tv.tv_sec = xtime.tv_sec;
494 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
495 tv.tv_usec += (jiffies - wall_jiffies) * (USEC_PER_SEC/HZ);
496 HANDLE_USEC_OVERFLOW(tv);
498 op.cmd = DOM0_SETTIME;
499 op.u.settime.secs = tv.tv_sec;
500 op.u.settime.usecs = tv.tv_usec;
501 op.u.settime.system_time = shadow_system_time;
502 HYPERVISOR_dom0_op(&op);
504 last_update_to_xen = xtime.tv_sec;
505 }
507 /*
508 * If we have an externally synchronized Linux clock, then update
509 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
510 * called as close as possible to 500 ms before the new second starts.
511 */
512 if ((time_status & STA_UNSYNC) == 0 &&
513 xtime.tv_sec > last_rtc_update + 660 &&
514 (xtime.tv_nsec / 1000)
515 >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
516 (xtime.tv_nsec / 1000)
517 <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2) {
518 /* horrible...FIXME */
519 if (efi_enabled) {
520 if (efi_set_rtc_mmss(xtime.tv_sec) == 0)
521 last_rtc_update = xtime.tv_sec;
522 else
523 last_rtc_update = xtime.tv_sec - 600;
524 } else if (set_rtc_mmss(xtime.tv_sec) == 0)
525 last_rtc_update = xtime.tv_sec;
526 else
527 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
528 }
529 #endif
530 }
532 /*
533 * This is the same as the above, except we _also_ save the current
534 * Time Stamp Counter value at the time of the timer interrupt, so that
535 * we later on can estimate the time of day more exactly.
536 */
537 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
538 {
539 /*
540 * Here we are in the timer irq handler. We just have irqs locally
541 * disabled but we don't know if the timer_bh is running on the other
542 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
543 * the irq version of write_lock because as just said we have irq
544 * locally disabled. -arca
545 */
546 write_seqlock(&xtime_lock);
547 do_timer_interrupt(irq, NULL, regs);
548 write_sequnlock(&xtime_lock);
549 return IRQ_HANDLED;
550 }
552 /* not static: needed by APM */
553 unsigned long get_cmos_time(void)
554 {
555 unsigned long retval;
557 spin_lock(&rtc_lock);
559 if (efi_enabled)
560 retval = efi_get_time();
561 else
562 retval = mach_get_cmos_time();
564 spin_unlock(&rtc_lock);
566 return retval;
567 }
569 static long clock_cmos_diff, sleep_start;
571 static int timer_suspend(struct sys_device *dev, u32 state)
572 {
573 /*
574 * Estimate time zone so that set_time can update the clock
575 */
576 clock_cmos_diff = -get_cmos_time();
577 clock_cmos_diff += get_seconds();
578 sleep_start = get_cmos_time();
579 return 0;
580 }
582 static int timer_resume(struct sys_device *dev)
583 {
584 unsigned long flags;
585 unsigned long sec;
586 unsigned long sleep_length;
588 #ifdef CONFIG_HPET_TIMER
589 if (is_hpet_enabled())
590 hpet_reenable();
591 #endif
592 sec = get_cmos_time() + clock_cmos_diff;
593 sleep_length = (get_cmos_time() - sleep_start) * HZ;
594 write_seqlock_irqsave(&xtime_lock, flags);
595 xtime.tv_sec = sec;
596 xtime.tv_nsec = 0;
597 write_sequnlock_irqrestore(&xtime_lock, flags);
598 jiffies += sleep_length;
599 wall_jiffies += sleep_length;
600 return 0;
601 }
603 static struct sysdev_class timer_sysclass = {
604 .resume = timer_resume,
605 .suspend = timer_suspend,
606 set_kset_name("timer"),
607 };
610 /* XXX this driverfs stuff should probably go elsewhere later -john */
611 static struct sys_device device_timer = {
612 .id = 0,
613 .cls = &timer_sysclass,
614 };
616 static int time_init_device(void)
617 {
618 int error = sysdev_class_register(&timer_sysclass);
619 if (!error)
620 error = sysdev_register(&device_timer);
621 return error;
622 }
624 device_initcall(time_init_device);
626 #ifdef CONFIG_HPET_TIMER
627 extern void (*late_time_init)(void);
628 /* Duplicate of time_init() below, with hpet_enable part added */
629 void __init hpet_time_init(void)
630 {
631 xtime.tv_sec = get_cmos_time();
632 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
633 set_normalized_timespec(&wall_to_monotonic,
634 -xtime.tv_sec, -xtime.tv_nsec);
636 if (hpet_enable() >= 0) {
637 printk("Using HPET for base-timer\n");
638 }
640 cur_timer = select_timer();
641 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
643 time_init_hook();
644 }
645 #endif
647 /* Dynamically-mapped IRQ. */
648 static DEFINE_PER_CPU(int, timer_irq);
650 static struct irqaction irq_timer = {
651 timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer0",
652 NULL, NULL
653 };
655 void __init time_init(void)
656 {
657 #ifdef CONFIG_HPET_TIMER
658 if (is_hpet_capable()) {
659 /*
660 * HPET initialization needs to do memory-mapped io. So, let
661 * us do a late initialization after mem_init().
662 */
663 late_time_init = hpet_time_init;
664 return;
665 }
666 #endif
667 __get_time_values_from_xen();
668 xtime.tv_sec = shadow_tv.tv_sec;
669 xtime.tv_nsec = shadow_tv.tv_usec * NSEC_PER_USEC;
670 set_normalized_timespec(&wall_to_monotonic,
671 -xtime.tv_sec, -xtime.tv_nsec);
672 processed_system_time = shadow_system_time;
673 per_cpu(processed_system_time, 0) = processed_system_time;
675 if (timer_tsc_init.init(NULL) != 0)
676 BUG();
677 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
679 #if defined(__x86_64__)
680 vxtime.mode = VXTIME_TSC;
681 vxtime.quot = (1000000L << 32) / vxtime_hz;
682 vxtime.tsc_quot = (1000L << 32) / cpu_khz;
683 vxtime.hz = vxtime_hz;
684 sync_core();
685 rdtscll(vxtime.last_tsc);
686 #endif
688 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
689 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
690 }
692 /* Convert jiffies to system time. Call with xtime_lock held for reading. */
693 static inline u64 __jiffies_to_st(unsigned long j)
694 {
695 long delta = j - jiffies;
696 /* NB. The next check can trigger in some wrap-around cases, but
697 * that's ok -- we'll just end up with a shorter timeout. */
698 if (delta < 1)
699 delta = 1;
700 return processed_system_time + (delta * NS_PER_TICK);
701 }
703 /*
704 * This function works out when the the next timer function has to be
705 * executed (by looking at the timer list) and sets the Xen one-shot
706 * domain timer to the appropriate value. This is typically called in
707 * cpu_idle() before the domain blocks.
708 *
709 * The function returns a non-0 value on error conditions.
710 *
711 * It must be called with interrupts disabled.
712 */
713 int set_timeout_timer(void)
714 {
715 u64 alarm = 0;
716 int ret = 0;
717 unsigned long j;
718 #ifdef CONFIG_SMP
719 unsigned long seq;
720 #endif
722 /*
723 * This is safe against long blocking (since calculations are
724 * not based on TSC deltas). It is also safe against warped
725 * system time since suspend-resume is cooperative and we
726 * would first get locked out.
727 */
728 #ifdef CONFIG_SMP
729 do {
730 seq = read_seqbegin(&xtime_lock);
731 j = jiffies + 1;
732 alarm = __jiffies_to_st(j);
733 } while (read_seqretry(&xtime_lock, seq));
734 #else
735 j = next_timer_interrupt();
736 alarm = __jiffies_to_st(j);
737 #endif
739 /* Failure is pretty bad, but we'd best soldier on. */
740 if ( HYPERVISOR_set_timer_op(alarm) != 0 )
741 ret = -1;
743 return ret;
744 }
746 void time_suspend(void)
747 {
748 /* nothing */
749 }
751 /* No locking required. We are only CPU running, and interrupts are off. */
752 void time_resume(void)
753 {
754 if (timer_tsc_init.init(NULL) != 0)
755 BUG();
757 /* Get timebases for new environment. */
758 __get_time_values_from_xen();
760 /* Reset our own concept of passage of system time. */
761 processed_system_time = shadow_system_time;
762 per_cpu(processed_system_time, 0) = processed_system_time;
764 /* Accept a warp in UTC (wall-clock) time. */
765 last_seen_tv.tv_sec = 0;
767 /* Make sure we resync UTC time with Xen on next timer interrupt. */
768 last_update_from_xen = 0;
769 }
771 #ifdef CONFIG_SMP
772 static char timer_name[NR_CPUS][15];
773 void local_setup_timer(void)
774 {
775 int seq, cpu = smp_processor_id();
777 do {
778 seq = read_seqbegin(&xtime_lock);
779 per_cpu(processed_system_time, cpu) = shadow_system_time;
780 } while (read_seqretry(&xtime_lock, seq));
782 per_cpu(timer_irq, cpu) = bind_virq_to_irq(VIRQ_TIMER);
783 sprintf(timer_name[cpu], "timer%d", cpu);
784 BUG_ON(request_irq(per_cpu(timer_irq, cpu), timer_interrupt,
785 SA_INTERRUPT, timer_name[cpu], NULL));
786 }
787 #endif
789 /*
790 * /proc/sys/xen: This really belongs in another file. It can stay here for
791 * now however.
792 */
793 static ctl_table xen_subtable[] = {
794 {1, "independent_wallclock", &independent_wallclock,
795 sizeof(independent_wallclock), 0644, NULL, proc_dointvec},
796 {0}
797 };
798 static ctl_table xen_table[] = {
799 {123, "xen", NULL, 0, 0555, xen_subtable},
800 {0}
801 };
802 static int __init xen_sysctl_init(void)
803 {
804 (void)register_sysctl_table(xen_table, 0);
805 return 0;
806 }
807 __initcall(xen_sysctl_init);