ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/time.c @ 6064:d4e19aca1f72

Fix signedness issue in xenlinux timer interrupt handler.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Mon Aug 08 14:13:36 2005 +0000 (2005-08-08)
parents 2360c4d7bb2f
children 7c9513a80026 dc61689b4781
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);
147 #define INDEPENDENT_WALLCLOCK() \
148 (independent_wallclock || (xen_start_info.flags & SIF_INITDOMAIN))
150 int tsc_disable __initdata = 0;
152 static void delay_tsc(unsigned long loops)
153 {
154 unsigned long bclock, now;
156 rdtscl(bclock);
157 do
158 {
159 rep_nop();
160 rdtscl(now);
161 } while ((now-bclock) < loops);
162 }
164 struct timer_opts timer_tsc = {
165 .name = "tsc",
166 .delay = delay_tsc,
167 };
169 /*
170 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
171 * yielding a 64-bit result.
172 */
173 static inline u64 scale_delta(u64 delta, u32 mul_frac, int shift)
174 {
175 u64 product;
176 u32 tmp1, tmp2;
178 if ( shift < 0 )
179 delta >>= -shift;
180 else
181 delta <<= shift;
183 __asm__ (
184 "mul %5 ; "
185 "mov %4,%%eax ; "
186 "mov %%edx,%4 ; "
187 "mul %5 ; "
188 "add %4,%%eax ; "
189 "xor %5,%5 ; "
190 "adc %5,%%edx ; "
191 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
192 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
194 return product;
195 }
197 void init_cpu_khz(void)
198 {
199 u64 __cpu_khz = 1000000ULL << 32;
200 struct vcpu_time_info *info = &HYPERVISOR_shared_info->vcpu_time[0];
201 do_div(__cpu_khz, info->tsc_to_system_mul);
202 if ( info->tsc_shift < 0 )
203 cpu_khz = __cpu_khz >> -info->tsc_shift;
204 else
205 cpu_khz = __cpu_khz << info->tsc_shift;
206 printk(KERN_INFO "Xen reported: %lu.%03lu MHz processor.\n",
207 cpu_khz / 1000, cpu_khz % 1000);
208 }
210 static u64 get_nsec_offset(struct shadow_time_info *shadow)
211 {
212 u64 now, delta;
213 rdtscll(now);
214 delta = now - shadow->tsc_timestamp;
215 return scale_delta(delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift);
216 }
218 static unsigned long get_usec_offset(struct shadow_time_info *shadow)
219 {
220 u64 now, delta;
221 rdtscll(now);
222 delta = now - shadow->tsc_timestamp;
223 return scale_delta(delta, shadow->tsc_to_usec_mul, shadow->tsc_shift);
224 }
226 static void update_wallclock(void)
227 {
228 shared_info_t *s = HYPERVISOR_shared_info;
229 long wtm_nsec, xtime_nsec;
230 time_t wtm_sec, xtime_sec;
231 u64 tmp, nsec;
233 do {
234 shadow_tv_version = s->wc_version;
235 rmb();
236 shadow_tv.tv_sec = s->wc_sec;
237 shadow_tv.tv_nsec = s->wc_nsec;
238 rmb();
239 }
240 while ((s->wc_version & 1) | (shadow_tv_version ^ s->wc_version));
242 if (INDEPENDENT_WALLCLOCK())
243 return;
245 if ((time_status & STA_UNSYNC) != 0)
246 return;
248 /* Adjust wall-clock time base based on wall_jiffies ticks. */
249 nsec = processed_system_time;
250 nsec += (u64)shadow_tv.tv_sec * 1000000000ULL;
251 nsec += (u64)shadow_tv.tv_nsec;
252 nsec -= (jiffies - wall_jiffies) * (u64)(NSEC_PER_SEC / HZ);
254 /* Split wallclock base into seconds and nanoseconds. */
255 tmp = nsec;
256 xtime_nsec = do_div(tmp, 1000000000);
257 xtime_sec = (time_t)tmp;
259 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - xtime_sec);
260 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - xtime_nsec);
262 set_normalized_timespec(&xtime, xtime_sec, xtime_nsec);
263 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
264 }
266 /*
267 * Reads a consistent set of time-base values from Xen, into a shadow data
268 * area.
269 */
270 static void get_time_values_from_xen(void)
271 {
272 shared_info_t *s = HYPERVISOR_shared_info;
273 struct vcpu_time_info *src;
274 struct shadow_time_info *dst;
276 src = &s->vcpu_time[smp_processor_id()];
277 dst = &per_cpu(shadow_time, smp_processor_id());
279 do {
280 dst->version = src->version;
281 rmb();
282 dst->tsc_timestamp = src->tsc_timestamp;
283 dst->system_timestamp = src->system_time;
284 dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
285 dst->tsc_shift = src->tsc_shift;
286 rmb();
287 }
288 while ((src->version & 1) | (dst->version ^ src->version));
290 dst->tsc_to_usec_mul = dst->tsc_to_nsec_mul / 1000;
291 }
293 static inline int time_values_up_to_date(int cpu)
294 {
295 struct vcpu_time_info *src;
296 struct shadow_time_info *dst;
298 src = &HYPERVISOR_shared_info->vcpu_time[cpu];
299 dst = &per_cpu(shadow_time, cpu);
301 return (dst->version == src->version);
302 }
304 /*
305 * This is a special lock that is owned by the CPU and holds the index
306 * register we are working with. It is required for NMI access to the
307 * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
308 */
309 volatile unsigned long cmos_lock = 0;
310 EXPORT_SYMBOL(cmos_lock);
312 /* Routines for accessing the CMOS RAM/RTC. */
313 unsigned char rtc_cmos_read(unsigned char addr)
314 {
315 unsigned char val;
316 lock_cmos_prefix(addr);
317 outb_p(addr, RTC_PORT(0));
318 val = inb_p(RTC_PORT(1));
319 lock_cmos_suffix(addr);
320 return val;
321 }
322 EXPORT_SYMBOL(rtc_cmos_read);
324 void rtc_cmos_write(unsigned char val, unsigned char addr)
325 {
326 lock_cmos_prefix(addr);
327 outb_p(addr, RTC_PORT(0));
328 outb_p(val, RTC_PORT(1));
329 lock_cmos_suffix(addr);
330 }
331 EXPORT_SYMBOL(rtc_cmos_write);
333 /*
334 * This version of gettimeofday has microsecond resolution
335 * and better than microsecond precision on fast x86 machines with TSC.
336 */
337 void do_gettimeofday(struct timeval *tv)
338 {
339 unsigned long seq;
340 unsigned long usec, sec;
341 unsigned long max_ntp_tick;
342 s64 nsec;
343 unsigned int cpu;
344 struct shadow_time_info *shadow;
345 u32 local_time_version;
347 cpu = get_cpu();
348 shadow = &per_cpu(shadow_time, cpu);
350 do {
351 unsigned long lost;
353 local_time_version = shadow->version;
354 seq = read_seqbegin(&xtime_lock);
356 usec = get_usec_offset(shadow);
357 lost = jiffies - wall_jiffies;
359 /*
360 * If time_adjust is negative then NTP is slowing the clock
361 * so make sure not to go into next possible interval.
362 * Better to lose some accuracy than have time go backwards..
363 */
364 if (unlikely(time_adjust < 0)) {
365 max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
366 usec = min(usec, max_ntp_tick);
368 if (lost)
369 usec += lost * max_ntp_tick;
370 }
371 else if (unlikely(lost))
372 usec += lost * (USEC_PER_SEC / HZ);
374 sec = xtime.tv_sec;
375 usec += (xtime.tv_nsec / NSEC_PER_USEC);
377 nsec = shadow->system_timestamp - processed_system_time;
378 __normalize_time(&sec, &nsec);
379 usec += (long)nsec / NSEC_PER_USEC;
381 if (unlikely(!time_values_up_to_date(cpu))) {
382 /*
383 * We may have blocked for a long time,
384 * rendering our calculations invalid
385 * (e.g. the time delta may have
386 * overflowed). Detect that and recalculate
387 * with fresh values.
388 */
389 get_time_values_from_xen();
390 continue;
391 }
392 } while (read_seqretry(&xtime_lock, seq) ||
393 (local_time_version != shadow->version));
395 put_cpu();
397 while (usec >= USEC_PER_SEC) {
398 usec -= USEC_PER_SEC;
399 sec++;
400 }
402 tv->tv_sec = sec;
403 tv->tv_usec = usec;
404 }
406 EXPORT_SYMBOL(do_gettimeofday);
408 int do_settimeofday(struct timespec *tv)
409 {
410 time_t wtm_sec, sec = tv->tv_sec;
411 long wtm_nsec;
412 s64 nsec;
413 struct timespec xentime;
414 unsigned int cpu;
415 struct shadow_time_info *shadow;
417 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
418 return -EINVAL;
420 if (!INDEPENDENT_WALLCLOCK())
421 return 0; /* Silent failure? */
423 cpu = get_cpu();
424 shadow = &per_cpu(shadow_time, cpu);
426 write_seqlock_irq(&xtime_lock);
428 /*
429 * Ensure we don't get blocked for a long time so that our time delta
430 * overflows. If that were to happen then our shadow time values would
431 * be stale, so we can retry with fresh ones.
432 */
433 again:
434 nsec = (s64)tv->tv_nsec - (s64)get_nsec_offset(shadow);
435 if (unlikely(!time_values_up_to_date(cpu))) {
436 get_time_values_from_xen();
437 goto again;
438 }
440 __normalize_time(&sec, &nsec);
441 set_normalized_timespec(&xentime, sec, nsec);
443 /*
444 * This is revolting. We need to set "xtime" correctly. However, the
445 * value in this location is the value at the most recent update of
446 * wall time. Discover what correction gettimeofday() would have
447 * made, and then undo it!
448 */
449 nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
451 nsec -= (shadow->system_timestamp - processed_system_time);
453 __normalize_time(&sec, &nsec);
454 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
455 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
457 set_normalized_timespec(&xtime, sec, nsec);
458 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
460 time_adjust = 0; /* stop active adjtime() */
461 time_status |= STA_UNSYNC;
462 time_maxerror = NTP_PHASE_LIMIT;
463 time_esterror = NTP_PHASE_LIMIT;
465 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
466 if (xen_start_info.flags & SIF_INITDOMAIN) {
467 dom0_op_t op;
468 op.cmd = DOM0_SETTIME;
469 op.u.settime.secs = xentime.tv_sec;
470 op.u.settime.nsecs = xentime.tv_nsec;
471 op.u.settime.system_time = shadow->system_timestamp;
472 write_sequnlock_irq(&xtime_lock);
473 HYPERVISOR_dom0_op(&op);
474 } else
475 #endif
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 /* gets recalled with irq locally disabled */
494 spin_lock_irq(&rtc_lock);
495 if (efi_enabled)
496 retval = efi_set_rtc_mmss(nowtime);
497 else
498 retval = mach_set_rtc_mmss(nowtime);
499 spin_unlock_irq(&rtc_lock);
501 return retval;
502 }
503 #else
504 static int set_rtc_mmss(unsigned long nowtime)
505 {
506 return 0;
507 }
508 #endif
510 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
511 * Note: This function is required to return accurate
512 * time even in the absence of multiple timer ticks.
513 */
514 unsigned long long monotonic_clock(void)
515 {
516 int cpu = get_cpu();
517 struct shadow_time_info *shadow = &per_cpu(shadow_time, cpu);
518 u64 time;
519 u32 local_time_version;
521 do {
522 local_time_version = shadow->version;
523 smp_rmb();
524 time = shadow->system_timestamp + get_nsec_offset(shadow);
525 if (!time_values_up_to_date(cpu))
526 get_time_values_from_xen();
527 smp_rmb();
528 } while (local_time_version != shadow->version);
530 put_cpu();
532 return time;
533 }
534 EXPORT_SYMBOL(monotonic_clock);
536 unsigned long long sched_clock(void)
537 {
538 return monotonic_clock();
539 }
541 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
542 unsigned long profile_pc(struct pt_regs *regs)
543 {
544 unsigned long pc = instruction_pointer(regs);
546 if (in_lock_functions(pc))
547 return *(unsigned long *)(regs->ebp + 4);
549 return pc;
550 }
551 EXPORT_SYMBOL(profile_pc);
552 #endif
554 /*
555 * timer_interrupt() needs to keep up the real-time clock,
556 * as well as call the "do_timer()" routine every clocktick
557 */
558 static inline void do_timer_interrupt(int irq, void *dev_id,
559 struct pt_regs *regs)
560 {
561 s64 delta, delta_cpu;
562 int cpu = smp_processor_id();
563 struct shadow_time_info *shadow = &per_cpu(shadow_time, cpu);
565 do {
566 get_time_values_from_xen();
568 delta = delta_cpu =
569 shadow->system_timestamp + get_nsec_offset(shadow);
570 delta -= processed_system_time;
571 delta_cpu -= per_cpu(processed_system_time, cpu);
572 }
573 while (!time_values_up_to_date(cpu));
575 if (unlikely(delta < (s64)-1000000) || unlikely(delta_cpu < 0)) {
576 printk("Timer ISR/%d: Time went backwards: "
577 "delta=%lld cpu_delta=%lld shadow=%lld "
578 "off=%lld processed=%lld cpu_processed=%lld\n",
579 cpu, delta, delta_cpu, shadow->system_timestamp,
580 (s64)get_nsec_offset(shadow),
581 processed_system_time,
582 per_cpu(processed_system_time, cpu));
583 for (cpu = 0; cpu < num_online_cpus(); cpu++)
584 printk(" %d: %lld\n", cpu,
585 per_cpu(processed_system_time, cpu));
586 return;
587 }
589 /* System-wide jiffy work. */
590 while (delta >= NS_PER_TICK) {
591 delta -= NS_PER_TICK;
592 processed_system_time += NS_PER_TICK;
593 do_timer(regs);
594 }
596 /* Local CPU jiffy work. */
597 while (delta_cpu >= NS_PER_TICK) {
598 delta_cpu -= NS_PER_TICK;
599 per_cpu(processed_system_time, cpu) += NS_PER_TICK;
600 update_process_times(user_mode(regs));
601 profile_tick(CPU_PROFILING, regs);
602 }
604 if (unlikely(shadow_tv_version != HYPERVISOR_shared_info->wc_version))
605 update_wallclock();
606 }
608 /*
609 * This is the same as the above, except we _also_ save the current
610 * Time Stamp Counter value at the time of the timer interrupt, so that
611 * we later on can estimate the time of day more exactly.
612 */
613 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
614 {
615 /*
616 * Here we are in the timer irq handler. We just have irqs locally
617 * disabled but we don't know if the timer_bh is running on the other
618 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
619 * the irq version of write_lock because as just said we have irq
620 * locally disabled. -arca
621 */
622 write_seqlock(&xtime_lock);
623 do_timer_interrupt(irq, NULL, regs);
624 write_sequnlock(&xtime_lock);
625 return IRQ_HANDLED;
626 }
628 /* not static: needed by APM */
629 unsigned long get_cmos_time(void)
630 {
631 unsigned long retval;
633 spin_lock(&rtc_lock);
635 if (efi_enabled)
636 retval = efi_get_time();
637 else
638 retval = mach_get_cmos_time();
640 spin_unlock(&rtc_lock);
642 return retval;
643 }
644 static void sync_cmos_clock(unsigned long dummy);
646 static struct timer_list sync_cmos_timer =
647 TIMER_INITIALIZER(sync_cmos_clock, 0, 0);
649 static void sync_cmos_clock(unsigned long dummy)
650 {
651 struct timeval now, next;
652 int fail = 1;
654 /*
655 * If we have an externally synchronized Linux clock, then update
656 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
657 * called as close as possible to 500 ms before the new second starts.
658 * This code is run on a timer. If the clock is set, that timer
659 * may not expire at the correct time. Thus, we adjust...
660 */
661 if ((time_status & STA_UNSYNC) != 0)
662 /*
663 * Not synced, exit, do not restart a timer (if one is
664 * running, let it run out).
665 */
666 return;
668 do_gettimeofday(&now);
669 if (now.tv_usec >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
670 now.tv_usec <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2)
671 fail = set_rtc_mmss(now.tv_sec);
673 next.tv_usec = USEC_AFTER - now.tv_usec;
674 if (next.tv_usec <= 0)
675 next.tv_usec += USEC_PER_SEC;
677 if (!fail)
678 next.tv_sec = 659;
679 else
680 next.tv_sec = 0;
682 if (next.tv_usec >= USEC_PER_SEC) {
683 next.tv_sec++;
684 next.tv_usec -= USEC_PER_SEC;
685 }
686 mod_timer(&sync_cmos_timer, jiffies + timeval_to_jiffies(&next));
687 }
689 void notify_arch_cmos_timer(void)
690 {
691 mod_timer(&sync_cmos_timer, jiffies + 1);
692 }
694 static long clock_cmos_diff, sleep_start;
696 static int timer_suspend(struct sys_device *dev, pm_message_t state)
697 {
698 /*
699 * Estimate time zone so that set_time can update the clock
700 */
701 clock_cmos_diff = -get_cmos_time();
702 clock_cmos_diff += get_seconds();
703 sleep_start = get_cmos_time();
704 return 0;
705 }
707 static int timer_resume(struct sys_device *dev)
708 {
709 unsigned long flags;
710 unsigned long sec;
711 unsigned long sleep_length;
713 #ifdef CONFIG_HPET_TIMER
714 if (is_hpet_enabled())
715 hpet_reenable();
716 #endif
717 sec = get_cmos_time() + clock_cmos_diff;
718 sleep_length = (get_cmos_time() - sleep_start) * HZ;
719 write_seqlock_irqsave(&xtime_lock, flags);
720 xtime.tv_sec = sec;
721 xtime.tv_nsec = 0;
722 write_sequnlock_irqrestore(&xtime_lock, flags);
723 jiffies += sleep_length;
724 wall_jiffies += sleep_length;
725 return 0;
726 }
728 static struct sysdev_class timer_sysclass = {
729 .resume = timer_resume,
730 .suspend = timer_suspend,
731 set_kset_name("timer"),
732 };
735 /* XXX this driverfs stuff should probably go elsewhere later -john */
736 static struct sys_device device_timer = {
737 .id = 0,
738 .cls = &timer_sysclass,
739 };
741 static int time_init_device(void)
742 {
743 int error = sysdev_class_register(&timer_sysclass);
744 if (!error)
745 error = sysdev_register(&device_timer);
746 return error;
747 }
749 device_initcall(time_init_device);
751 #ifdef CONFIG_HPET_TIMER
752 extern void (*late_time_init)(void);
753 /* Duplicate of time_init() below, with hpet_enable part added */
754 static void __init hpet_time_init(void)
755 {
756 xtime.tv_sec = get_cmos_time();
757 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
758 set_normalized_timespec(&wall_to_monotonic,
759 -xtime.tv_sec, -xtime.tv_nsec);
761 if ((hpet_enable() >= 0) && hpet_use_timer) {
762 printk("Using HPET for base-timer\n");
763 }
765 cur_timer = select_timer();
766 printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
768 time_init_hook();
769 }
770 #endif
772 /* Dynamically-mapped IRQ. */
773 static DEFINE_PER_CPU(int, timer_irq);
775 static struct irqaction irq_timer = {
776 timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer0",
777 NULL, NULL
778 };
780 void __init time_init(void)
781 {
782 #ifdef CONFIG_HPET_TIMER
783 if (is_hpet_capable()) {
784 /*
785 * HPET initialization needs to do memory-mapped io. So, let
786 * us do a late initialization after mem_init().
787 */
788 late_time_init = hpet_time_init;
789 return;
790 }
791 #endif
792 get_time_values_from_xen();
793 update_wallclock();
794 set_normalized_timespec(&wall_to_monotonic,
795 -xtime.tv_sec, -xtime.tv_nsec);
796 processed_system_time = per_cpu(shadow_time, 0).system_timestamp;
797 per_cpu(processed_system_time, 0) = processed_system_time;
799 init_cpu_khz();
801 #if defined(__x86_64__)
802 vxtime.mode = VXTIME_TSC;
803 vxtime.quot = (1000000L << 32) / vxtime_hz;
804 vxtime.tsc_quot = (1000L << 32) / cpu_khz;
805 vxtime.hz = vxtime_hz;
806 sync_core();
807 rdtscll(vxtime.last_tsc);
808 #endif
810 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
811 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
812 }
814 /* Convert jiffies to system time. */
815 static inline u64 jiffies_to_st(unsigned long j)
816 {
817 unsigned long seq;
818 long delta;
819 u64 st;
821 do {
822 seq = read_seqbegin(&xtime_lock);
823 delta = j - jiffies;
824 /* NB. The next check can trigger in some wrap-around cases,
825 * but that's ok: we'll just end up with a shorter timeout. */
826 if (delta < 1)
827 delta = 1;
828 st = processed_system_time + (delta * NS_PER_TICK);
829 } while (read_seqretry(&xtime_lock, seq));
831 return st;
832 }
834 /*
835 * stop_hz_timer / start_hz_timer - enter/exit 'tickless mode' on an idle cpu
836 * These functions are based on implementations from arch/s390/kernel/time.c
837 */
838 void stop_hz_timer(void)
839 {
840 unsigned int cpu = smp_processor_id();
841 unsigned long j;
843 /* s390 does this /before/ checking rcu_pending(). We copy them. */
844 cpu_set(cpu, nohz_cpu_mask);
846 /* Leave ourselves in 'tick mode' if rcu or softirq pending. */
847 if (rcu_pending(cpu) || local_softirq_pending()) {
848 cpu_clear(cpu, nohz_cpu_mask);
849 j = jiffies + 1;
850 } else {
851 j = next_timer_interrupt();
852 }
854 BUG_ON(HYPERVISOR_set_timer_op(jiffies_to_st(j)) != 0);
855 }
857 void start_hz_timer(void)
858 {
859 cpu_clear(smp_processor_id(), nohz_cpu_mask);
860 }
862 void time_suspend(void)
863 {
864 /* nothing */
865 teardown_irq(per_cpu(timer_irq, 0), &irq_timer);
866 unbind_virq_from_irq(VIRQ_TIMER);
867 }
869 /* No locking required. We are only CPU running, and interrupts are off. */
870 void time_resume(void)
871 {
872 init_cpu_khz();
874 /* Get timebases for new environment. */
875 get_time_values_from_xen();
876 update_wallclock();
878 /* Reset our own concept of passage of system time. */
879 processed_system_time =
880 per_cpu(shadow_time, smp_processor_id()).system_timestamp;
881 per_cpu(processed_system_time, 0) = processed_system_time;
883 per_cpu(timer_irq, 0) = bind_virq_to_irq(VIRQ_TIMER);
884 (void)setup_irq(per_cpu(timer_irq, 0), &irq_timer);
885 }
887 #ifdef CONFIG_SMP
888 static char timer_name[NR_CPUS][15];
889 void local_setup_timer_irq(void)
890 {
891 int cpu = smp_processor_id();
893 if (cpu == 0)
894 return;
895 per_cpu(timer_irq, cpu) = bind_virq_to_irq(VIRQ_TIMER);
896 sprintf(timer_name[cpu], "timer%d", cpu);
897 BUG_ON(request_irq(per_cpu(timer_irq, cpu), timer_interrupt,
898 SA_INTERRUPT, timer_name[cpu], NULL));
899 }
901 void local_setup_timer(void)
902 {
903 int seq, cpu = smp_processor_id();
905 do {
906 seq = read_seqbegin(&xtime_lock);
907 per_cpu(processed_system_time, cpu) =
908 per_cpu(shadow_time, cpu).system_timestamp;
909 } while (read_seqretry(&xtime_lock, seq));
911 local_setup_timer_irq();
912 }
914 void local_teardown_timer_irq(void)
915 {
916 int cpu = smp_processor_id();
918 if (cpu == 0)
919 return;
920 free_irq(per_cpu(timer_irq, cpu), NULL);
921 unbind_virq_from_irq(VIRQ_TIMER);
922 }
923 #endif
925 /*
926 * /proc/sys/xen: This really belongs in another file. It can stay here for
927 * now however.
928 */
929 static ctl_table xen_subtable[] = {
930 {1, "independent_wallclock", &independent_wallclock,
931 sizeof(independent_wallclock), 0644, NULL, proc_dointvec},
932 {0}
933 };
934 static ctl_table xen_table[] = {
935 {123, "xen", NULL, 0, 0555, xen_subtable},
936 {0}
937 };
938 static int __init xen_sysctl_init(void)
939 {
940 (void)register_sysctl_table(xen_table, 0);
941 return 0;
942 }
943 __initcall(xen_sysctl_init);
945 /*
946 * Local variables:
947 * c-file-style: "linux"
948 * indent-tabs-mode: t
949 * c-indent-level: 8
950 * c-basic-offset: 8
951 * tab-width: 8
952 * End:
953 */