ia64/xen-unstable

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

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