ia64/xen-unstable

view linux-2.6.10-xen-sparse/arch/xen/i386/kernel/time.c @ 3470:c90e94e09339

bitkeeper revision 1.1159.224.3 (41f02e490kpSofHA0mGdhL54XCyaXA)

Merge scramble.cl.cam.ac.uk:/local/scratch/kaf24/xen-2.0-testing.bk
into scramble.cl.cam.ac.uk:/local/scratch/kaf24/xen-unstable.bk
author kaf24@scramble.cl.cam.ac.uk
date Thu Jan 20 22:18:49 2005 +0000 (2005-01-20)
parents 39a7a74fd6f9 bf476d24f798
children 12e84ce22ce3
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/sysctl.h>
49 #include <linux/percpu.h>
51 #include <asm/io.h>
52 #include <asm/smp.h>
53 #include <asm/irq.h>
54 #include <asm/msr.h>
55 #include <asm/delay.h>
56 #include <asm/mpspec.h>
57 #include <asm/uaccess.h>
58 #include <asm/processor.h>
59 #include <asm/timer.h>
61 #include "mach_time.h"
63 #include <linux/timex.h>
64 #include <linux/config.h>
66 #include <asm/hpet.h>
68 #include <asm/arch_hooks.h>
70 #include "io_ports.h"
72 extern spinlock_t i8259A_lock;
73 int pit_latch_buggy; /* extern */
75 u64 jiffies_64 = INITIAL_JIFFIES;
77 EXPORT_SYMBOL(jiffies_64);
79 unsigned long cpu_khz; /* Detected as we calibrate the TSC */
81 extern unsigned long wall_jiffies;
83 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
85 spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
86 EXPORT_SYMBOL(i8253_lock);
88 extern struct init_timer_opts timer_tsc_init;
89 extern struct timer_opts timer_tsc;
90 struct timer_opts *cur_timer = &timer_tsc;
92 /* These are peridically updated in shared_info, and then copied here. */
93 u32 shadow_tsc_stamp;
94 u64 shadow_system_time;
95 static u32 shadow_time_version;
96 static struct timeval shadow_tv;
98 /*
99 * We use this to ensure that gettimeofday() is monotonically increasing. We
100 * only break this guarantee if the wall clock jumps backwards "a long way".
101 */
102 static struct timeval last_seen_tv = {0,0};
104 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
105 /* Periodically propagate synchronised time base to the RTC and to Xen. */
106 static long last_rtc_update, last_update_to_xen;
107 #endif
109 /* Periodically take synchronised time base from Xen, if we need it. */
110 static long last_update_from_xen; /* UTC seconds when last read Xen clock. */
112 /* Keep track of last time we did processing/updating of jiffies and xtime. */
113 u64 processed_system_time; /* System time (ns) at last processing. */
114 DEFINE_PER_CPU(u64, processed_system_time);
116 #define NS_PER_TICK (1000000000ULL/HZ)
118 #define HANDLE_USEC_UNDERFLOW(_tv) do { \
119 while ((_tv).tv_usec < 0) { \
120 (_tv).tv_usec += USEC_PER_SEC; \
121 (_tv).tv_sec--; \
122 } \
123 } while (0)
124 #define HANDLE_USEC_OVERFLOW(_tv) do { \
125 while ((_tv).tv_usec >= USEC_PER_SEC) { \
126 (_tv).tv_usec -= USEC_PER_SEC; \
127 (_tv).tv_sec++; \
128 } \
129 } while (0)
130 static inline void __normalize_time(time_t *sec, s64 *nsec)
131 {
132 while (*nsec >= NSEC_PER_SEC) {
133 (*nsec) -= NSEC_PER_SEC;
134 (*sec)++;
135 }
136 while (*nsec < 0) {
137 (*nsec) += NSEC_PER_SEC;
138 (*sec)--;
139 }
140 }
142 /* Does this guest OS track Xen time, or set its wall clock independently? */
143 static int independent_wallclock = 0;
144 static int __init __independent_wallclock(char *str)
145 {
146 independent_wallclock = 1;
147 return 1;
148 }
149 __setup("independent_wallclock", __independent_wallclock);
150 #define INDEPENDENT_WALLCLOCK() \
151 (independent_wallclock || (xen_start_info.flags & SIF_INITDOMAIN))
153 /*
154 * Reads a consistent set of time-base values from Xen, into a shadow data
155 * area. Must be called with the xtime_lock held for writing.
156 */
157 static void __get_time_values_from_xen(void)
158 {
159 shared_info_t *s = HYPERVISOR_shared_info;
161 do {
162 shadow_time_version = s->time_version2;
163 rmb();
164 shadow_tv.tv_sec = s->wc_sec;
165 shadow_tv.tv_usec = s->wc_usec;
166 shadow_tsc_stamp = (u32)s->tsc_timestamp;
167 shadow_system_time = s->system_time;
168 rmb();
169 }
170 while (shadow_time_version != s->time_version1);
172 cur_timer->mark_offset();
173 }
175 #define TIME_VALUES_UP_TO_DATE \
176 ({ rmb(); (shadow_time_version == HYPERVISOR_shared_info->time_version2); })
178 /*
179 * This version of gettimeofday has microsecond resolution
180 * and better than microsecond precision on fast x86 machines with TSC.
181 */
182 void do_gettimeofday(struct timeval *tv)
183 {
184 unsigned long seq;
185 unsigned long usec, sec;
186 unsigned long max_ntp_tick;
187 unsigned long flags;
188 s64 nsec;
190 do {
191 unsigned long lost;
193 seq = read_seqbegin(&xtime_lock);
195 usec = cur_timer->get_offset();
196 lost = jiffies - wall_jiffies;
198 /*
199 * If time_adjust is negative then NTP is slowing the clock
200 * so make sure not to go into next possible interval.
201 * Better to lose some accuracy than have time go backwards..
202 */
203 if (unlikely(time_adjust < 0)) {
204 max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
205 usec = min(usec, max_ntp_tick);
207 if (lost)
208 usec += lost * max_ntp_tick;
209 }
210 else if (unlikely(lost))
211 usec += lost * (USEC_PER_SEC / HZ);
213 sec = xtime.tv_sec;
214 usec += (xtime.tv_nsec / NSEC_PER_USEC);
216 nsec = shadow_system_time - processed_system_time;
217 __normalize_time(&sec, &nsec);
218 usec += (long)nsec / NSEC_PER_USEC;
220 if (unlikely(!TIME_VALUES_UP_TO_DATE)) {
221 /*
222 * We may have blocked for a long time,
223 * rendering our calculations invalid
224 * (e.g. the time delta may have
225 * overflowed). Detect that and recalculate
226 * with fresh values.
227 */
228 write_seqlock_irqsave(&xtime_lock, flags);
229 __get_time_values_from_xen();
230 write_sequnlock_irqrestore(&xtime_lock, flags);
231 continue;
232 }
233 } while (read_seqretry(&xtime_lock, seq));
235 while (usec >= USEC_PER_SEC) {
236 usec -= USEC_PER_SEC;
237 sec++;
238 }
240 /* Ensure that time-of-day is monotonically increasing. */
241 if ((sec < last_seen_tv.tv_sec) ||
242 ((sec == last_seen_tv.tv_sec) && (usec < last_seen_tv.tv_usec))) {
243 sec = last_seen_tv.tv_sec;
244 usec = last_seen_tv.tv_usec;
245 } else {
246 last_seen_tv.tv_sec = sec;
247 last_seen_tv.tv_usec = usec;
248 }
250 tv->tv_sec = sec;
251 tv->tv_usec = usec;
252 }
254 EXPORT_SYMBOL(do_gettimeofday);
256 int do_settimeofday(struct timespec *tv)
257 {
258 time_t wtm_sec, sec = tv->tv_sec;
259 long wtm_nsec;
260 s64 nsec;
261 struct timespec xentime;
263 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
264 return -EINVAL;
266 if (!INDEPENDENT_WALLCLOCK())
267 return 0; /* Silent failure? */
269 write_seqlock_irq(&xtime_lock);
271 /*
272 * Ensure we don't get blocked for a long time so that our time delta
273 * overflows. If that were to happen then our shadow time values would
274 * be stale, so we can retry with fresh ones.
275 */
276 again:
277 nsec = (s64)tv->tv_nsec -
278 ((s64)cur_timer->get_offset() * (s64)NSEC_PER_USEC);
279 if (unlikely(!TIME_VALUES_UP_TO_DATE)) {
280 __get_time_values_from_xen();
281 goto again;
282 }
284 __normalize_time(&sec, &nsec);
285 set_normalized_timespec(&xentime, sec, nsec);
287 /*
288 * This is revolting. We need to set "xtime" correctly. However, the
289 * value in this location is the value at the most recent update of
290 * wall time. Discover what correction gettimeofday() would have
291 * made, and then undo it!
292 */
293 nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
295 nsec -= (shadow_system_time - processed_system_time);
297 __normalize_time(&sec, &nsec);
298 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
299 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
301 set_normalized_timespec(&xtime, sec, nsec);
302 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
304 time_adjust = 0; /* stop active adjtime() */
305 time_status |= STA_UNSYNC;
306 time_maxerror = NTP_PHASE_LIMIT;
307 time_esterror = NTP_PHASE_LIMIT;
309 /* Reset all our running time counts. They make no sense now. */
310 last_seen_tv.tv_sec = 0;
311 last_update_from_xen = 0;
313 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
314 if (xen_start_info.flags & SIF_INITDOMAIN) {
315 dom0_op_t op;
316 last_rtc_update = last_update_to_xen = 0;
317 op.cmd = DOM0_SETTIME;
318 op.u.settime.secs = xentime.tv_sec;
319 op.u.settime.usecs = xentime.tv_nsec / NSEC_PER_USEC;
320 op.u.settime.system_time = shadow_system_time;
321 write_sequnlock_irq(&xtime_lock);
322 HYPERVISOR_dom0_op(&op);
323 } else
324 #endif
325 write_sequnlock_irq(&xtime_lock);
327 clock_was_set();
328 return 0;
329 }
331 EXPORT_SYMBOL(do_settimeofday);
333 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
334 static int set_rtc_mmss(unsigned long nowtime)
335 {
336 int retval;
338 /* gets recalled with irq locally disabled */
339 spin_lock(&rtc_lock);
340 if (efi_enabled)
341 retval = efi_set_rtc_mmss(nowtime);
342 else
343 retval = mach_set_rtc_mmss(nowtime);
344 spin_unlock(&rtc_lock);
346 return retval;
347 }
348 #endif
350 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
351 * Note: This function is required to return accurate
352 * time even in the absence of multiple timer ticks.
353 */
354 unsigned long long monotonic_clock(void)
355 {
356 return cur_timer->monotonic_clock();
357 }
358 EXPORT_SYMBOL(monotonic_clock);
360 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
361 unsigned long profile_pc(struct pt_regs *regs)
362 {
363 unsigned long pc = instruction_pointer(regs);
365 if (in_lock_functions(pc))
366 return *(unsigned long *)(regs->ebp + 4);
368 return pc;
369 }
370 EXPORT_SYMBOL(profile_pc);
371 #endif
373 /*
374 * timer_interrupt() needs to keep up the real-time clock,
375 * as well as call the "do_timer()" routine every clocktick
376 */
377 static inline void do_timer_interrupt(int irq, void *dev_id,
378 struct pt_regs *regs)
379 {
380 time_t wtm_sec, sec;
381 s64 delta, nsec;
382 long sec_diff, wtm_nsec;
384 do {
385 __get_time_values_from_xen();
387 delta = (s64)(shadow_system_time +
388 ((s64)cur_timer->get_offset() *
389 (s64)NSEC_PER_USEC) -
390 processed_system_time);
391 }
392 while (!TIME_VALUES_UP_TO_DATE);
394 if (unlikely(delta < 0)) {
395 printk("Timer ISR: Time went backwards: %lld %lld %lld %lld\n",
396 delta, shadow_system_time,
397 ((s64)cur_timer->get_offset() * (s64)NSEC_PER_USEC),
398 processed_system_time);
399 return;
400 }
402 /* Process elapsed jiffies since last call. */
403 while (delta >= NS_PER_TICK) {
404 delta -= NS_PER_TICK;
405 processed_system_time += NS_PER_TICK;
406 do_timer(regs);
407 update_process_times(user_mode(regs));
408 if (regs)
409 profile_tick(CPU_PROFILING, regs);
410 }
412 /*
413 * Take synchronised time from Xen once a minute if we're not
414 * synchronised ourselves, and we haven't chosen to keep an independent
415 * time base.
416 */
417 if (!INDEPENDENT_WALLCLOCK() &&
418 ((time_status & STA_UNSYNC) != 0) &&
419 (xtime.tv_sec > (last_update_from_xen + 60))) {
420 /* Adjust shadow for jiffies that haven't updated xtime yet. */
421 shadow_tv.tv_usec -=
422 (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ);
423 HANDLE_USEC_UNDERFLOW(shadow_tv);
425 /*
426 * Reset our running time counts if they are invalidated by
427 * a warp backwards of more than 500ms.
428 */
429 sec_diff = xtime.tv_sec - shadow_tv.tv_sec;
430 if (unlikely(abs(sec_diff) > 1) ||
431 unlikely(((sec_diff * USEC_PER_SEC) +
432 (xtime.tv_nsec / NSEC_PER_USEC) -
433 shadow_tv.tv_usec) > 500000)) {
434 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
435 last_rtc_update = last_update_to_xen = 0;
436 #endif
437 last_seen_tv.tv_sec = 0;
438 }
440 /* Update our unsynchronised xtime appropriately. */
441 sec = shadow_tv.tv_sec;
442 nsec = shadow_tv.tv_usec * NSEC_PER_USEC;
444 __normalize_time(&sec, &nsec);
445 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
446 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
448 set_normalized_timespec(&xtime, sec, nsec);
449 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
451 last_update_from_xen = sec;
452 }
454 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
455 if (!(xen_start_info.flags & SIF_INITDOMAIN))
456 return;
458 /* Send synchronised time to Xen approximately every minute. */
459 if (((time_status & STA_UNSYNC) == 0) &&
460 (xtime.tv_sec > (last_update_to_xen + 60))) {
461 dom0_op_t op;
462 struct timeval tv;
464 tv.tv_sec = xtime.tv_sec;
465 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
466 tv.tv_usec += (jiffies - wall_jiffies) * (USEC_PER_SEC/HZ);
467 HANDLE_USEC_OVERFLOW(tv);
469 op.cmd = DOM0_SETTIME;
470 op.u.settime.secs = tv.tv_sec;
471 op.u.settime.usecs = tv.tv_usec;
472 op.u.settime.system_time = shadow_system_time;
473 HYPERVISOR_dom0_op(&op);
475 last_update_to_xen = xtime.tv_sec;
476 }
478 /*
479 * If we have an externally synchronized Linux clock, then update
480 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
481 * called as close as possible to 500 ms before the new second starts.
482 */
483 if ((time_status & STA_UNSYNC) == 0 &&
484 xtime.tv_sec > last_rtc_update + 660 &&
485 (xtime.tv_nsec / 1000)
486 >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
487 (xtime.tv_nsec / 1000)
488 <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2) {
489 /* horrible...FIXME */
490 if (efi_enabled) {
491 if (efi_set_rtc_mmss(xtime.tv_sec) == 0)
492 last_rtc_update = xtime.tv_sec;
493 else
494 last_rtc_update = xtime.tv_sec - 600;
495 } else if (set_rtc_mmss(xtime.tv_sec) == 0)
496 last_rtc_update = xtime.tv_sec;
497 else
498 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
499 }
500 #endif
501 }
503 /*
504 * This is the same as the above, except we _also_ save the current
505 * Time Stamp Counter value at the time of the timer interrupt, so that
506 * we later on can estimate the time of day more exactly.
507 */
508 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
509 {
510 /*
511 * Here we are in the timer irq handler. We just have irqs locally
512 * disabled but we don't know if the timer_bh is running on the other
513 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
514 * the irq version of write_lock because as just said we have irq
515 * locally disabled. -arca
516 */
517 write_seqlock(&xtime_lock);
518 do_timer_interrupt(irq, NULL, regs);
519 write_sequnlock(&xtime_lock);
520 return IRQ_HANDLED;
521 }
523 /* not static: needed by APM */
524 unsigned long get_cmos_time(void)
525 {
526 unsigned long retval;
528 spin_lock(&rtc_lock);
530 if (efi_enabled)
531 retval = efi_get_time();
532 else
533 retval = mach_get_cmos_time();
535 spin_unlock(&rtc_lock);
537 return retval;
538 }
540 static long clock_cmos_diff, sleep_start;
542 static int timer_suspend(struct sys_device *dev, u32 state)
543 {
544 /*
545 * Estimate time zone so that set_time can update the clock
546 */
547 clock_cmos_diff = -get_cmos_time();
548 clock_cmos_diff += get_seconds();
549 sleep_start = get_cmos_time();
550 return 0;
551 }
553 static int timer_resume(struct sys_device *dev)
554 {
555 unsigned long flags;
556 unsigned long sec;
557 unsigned long sleep_length;
559 #ifdef CONFIG_HPET_TIMER
560 if (is_hpet_enabled())
561 hpet_reenable();
562 #endif
563 sec = get_cmos_time() + clock_cmos_diff;
564 sleep_length = get_cmos_time() - sleep_start;
565 write_seqlock_irqsave(&xtime_lock, flags);
566 xtime.tv_sec = sec;
567 xtime.tv_nsec = 0;
568 write_sequnlock_irqrestore(&xtime_lock, flags);
569 jiffies += sleep_length * HZ;
570 return 0;
571 }
573 static struct sysdev_class timer_sysclass = {
574 .resume = timer_resume,
575 .suspend = timer_suspend,
576 set_kset_name("timer"),
577 };
580 /* XXX this driverfs stuff should probably go elsewhere later -john */
581 static struct sys_device device_timer = {
582 .id = 0,
583 .cls = &timer_sysclass,
584 };
586 static int time_init_device(void)
587 {
588 int error = sysdev_class_register(&timer_sysclass);
589 if (!error)
590 error = sysdev_register(&device_timer);
591 return error;
592 }
594 device_initcall(time_init_device);
596 #ifdef CONFIG_HPET_TIMER
597 extern void (*late_time_init)(void);
598 /* Duplicate of time_init() below, with hpet_enable part added */
599 void __init hpet_time_init(void)
600 {
601 xtime.tv_sec = get_cmos_time();
602 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
603 set_normalized_timespec(&wall_to_monotonic,
604 -xtime.tv_sec, -xtime.tv_nsec);
606 if (hpet_enable() >= 0) {
607 printk("Using HPET for base-timer\n");
608 }
610 cur_timer = select_timer();
611 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
613 time_init_hook();
614 }
615 #endif
617 /* Dynamically-mapped IRQ. */
618 static int TIMER_IRQ;
620 static struct irqaction irq_timer = {
621 timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer",
622 NULL, NULL
623 };
625 void __init time_init(void)
626 {
627 #ifdef CONFIG_HPET_TIMER
628 if (is_hpet_capable()) {
629 /*
630 * HPET initialization needs to do memory-mapped io. So, let
631 * us do a late initialization after mem_init().
632 */
633 late_time_init = hpet_time_init;
634 return;
635 }
636 #endif
637 __get_time_values_from_xen();
638 xtime.tv_sec = shadow_tv.tv_sec;
639 xtime.tv_nsec = shadow_tv.tv_usec * NSEC_PER_USEC;
640 set_normalized_timespec(&wall_to_monotonic,
641 -xtime.tv_sec, -xtime.tv_nsec);
642 processed_system_time = shadow_system_time;
644 if (timer_tsc_init.init(NULL) != 0)
645 BUG();
646 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
648 TIMER_IRQ = bind_virq_to_irq(VIRQ_TIMER);
650 (void)setup_irq(TIMER_IRQ, &irq_timer);
651 }
653 /* Convert jiffies to system time. Call with xtime_lock held for reading. */
654 static inline u64 __jiffies_to_st(unsigned long j)
655 {
656 return processed_system_time + ((j - jiffies) * NS_PER_TICK);
657 }
659 /*
660 * This function works out when the the next timer function has to be
661 * executed (by looking at the timer list) and sets the Xen one-shot
662 * domain timer to the appropriate value. This is typically called in
663 * cpu_idle() before the domain blocks.
664 *
665 * The function returns a non-0 value on error conditions.
666 *
667 * It must be called with interrupts disabled.
668 */
669 int set_timeout_timer(void)
670 {
671 u64 alarm = 0;
672 int ret = 0;
673 #ifdef CONFIG_SMP
674 unsigned long seq;
675 #endif
677 /*
678 * This is safe against long blocking (since calculations are
679 * not based on TSC deltas). It is also safe against warped
680 * system time since suspend-resume is cooperative and we
681 * would first get locked out.
682 */
683 #ifdef CONFIG_SMP
684 do {
685 seq = read_seqbegin(&xtime_lock);
686 if (smp_processor_id())
687 alarm = __jiffies_to_st(jiffies + 1);
688 else
689 alarm = __jiffies_to_st(jiffies + 1);
690 } while (read_seqretry(&xtime_lock, seq));
691 #else
692 alarm = __jiffies_to_st(next_timer_interrupt());
693 #endif
695 /* Failure is pretty bad, but we'd best soldier on. */
696 if ( HYPERVISOR_set_timer_op(alarm) != 0 )
697 ret = -1;
699 return ret;
700 }
702 void time_suspend(void)
703 {
704 /* nothing */
705 }
707 /* No locking required. We are only CPU running, and interrupts are off. */
708 void time_resume(void)
709 {
710 if (timer_tsc_init.init(NULL) != 0)
711 BUG();
713 /* Get timebases for new environment. */
714 __get_time_values_from_xen();
716 /* Reset our own concept of passage of system time. */
717 processed_system_time = shadow_system_time;
719 /* Accept a warp in UTC (wall-clock) time. */
720 last_seen_tv.tv_sec = 0;
722 /* Make sure we resync UTC time with Xen on next timer interrupt. */
723 last_update_from_xen = 0;
724 }
726 #ifdef CONFIG_SMP
727 #define xxprint(msg) HYPERVISOR_console_io(CONSOLEIO_write, strlen(msg), msg)
729 static irqreturn_t local_timer_interrupt(int irq, void *dev_id,
730 struct pt_regs *regs)
731 {
732 s64 delta;
733 int cpu = smp_processor_id();
735 do {
736 __get_time_values_from_xen();
738 delta = (s64)(shadow_system_time +
739 ((s64)cur_timer->get_offset() *
740 (s64)NSEC_PER_USEC) -
741 per_cpu(processed_system_time, cpu));
742 }
743 while (!TIME_VALUES_UP_TO_DATE);
745 if (unlikely(delta < 0)) {
746 printk("Timer ISR/%d: Time went backwards: %lld %lld %lld %lld\n",
747 cpu, delta, shadow_system_time,
748 ((s64)cur_timer->get_offset() * (s64)NSEC_PER_USEC),
749 processed_system_time);
750 return IRQ_HANDLED;
751 }
753 /* Process elapsed jiffies since last call. */
754 while (delta >= NS_PER_TICK) {
755 delta -= NS_PER_TICK;
756 per_cpu(processed_system_time, cpu) += NS_PER_TICK;
757 if (regs)
758 update_process_times(user_mode(regs));
759 #if 0
760 if (regs)
761 profile_tick(CPU_PROFILING, regs);
762 #endif
763 }
765 if (smp_processor_id() == 0) {
766 xxprint("bug bug\n");
767 BUG();
768 }
770 return IRQ_HANDLED;
771 }
773 static struct irqaction local_irq_timer = {
774 local_timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "ltimer",
775 NULL, NULL
776 };
778 void local_setup_timer(void)
779 {
780 int seq, time_irq;
781 int cpu = smp_processor_id();
783 do {
784 seq = read_seqbegin(&xtime_lock);
785 per_cpu(processed_system_time, cpu) = shadow_system_time;
786 } while (read_seqretry(&xtime_lock, seq));
788 time_irq = bind_virq_to_irq(VIRQ_TIMER);
789 (void)setup_irq(time_irq, &local_irq_timer);
790 }
791 #endif
793 /*
794 * /proc/sys/xen: This really belongs in another file. It can stay here for
795 * now however.
796 */
797 static ctl_table xen_subtable[] = {
798 {1, "independent_wallclock", &independent_wallclock,
799 sizeof(independent_wallclock), 0644, NULL, proc_dointvec},
800 {0}
801 };
802 static ctl_table xen_table[] = {
803 {123, "xen", NULL, 0, 0555, xen_subtable},
804 {0}
805 };
806 static int __init xen_sysctl_init(void)
807 {
808 (void)register_sysctl_table(xen_table, 0);
809 return 0;
810 }
811 __initcall(xen_sysctl_init);