source: trunk/packages/xen-common/xen-common/xen/arch/x86/time.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

File size: 24.4 KB
Line 
1/******************************************************************************
2 * arch/x86/time.c
3 *
4 * Per-CPU time calibration and management.
5 *
6 * Copyright (c) 2002-2005, K A Fraser
7 *
8 * Portions from Linux are:
9 * Copyright (c) 1991, 1992, 1995  Linus Torvalds
10 */
11
12#include <xen/config.h>
13#include <xen/errno.h>
14#include <xen/event.h>
15#include <xen/sched.h>
16#include <xen/lib.h>
17#include <xen/config.h>
18#include <xen/init.h>
19#include <xen/time.h>
20#include <xen/timer.h>
21#include <xen/smp.h>
22#include <xen/irq.h>
23#include <xen/softirq.h>
24#include <asm/io.h>
25#include <asm/msr.h>
26#include <asm/mpspec.h>
27#include <asm/processor.h>
28#include <asm/fixmap.h>
29#include <asm/mc146818rtc.h>
30#include <asm/div64.h>
31#include <asm/hpet.h>
32#include <io_ports.h>
33
34/* opt_hpet_force: If true, force HPET configuration via PCI space. */
35/* NB. This is a gross hack. Mainly useful for HPET testing. */
36static int opt_hpet_force = 0;
37boolean_param("hpet_force", opt_hpet_force);
38
39#define EPOCH MILLISECS(1000)
40
41unsigned long cpu_khz;  /* CPU clock frequency in kHz. */
42unsigned long hpet_address;
43DEFINE_SPINLOCK(rtc_lock);
44volatile unsigned long jiffies;
45static u32 wc_sec, wc_nsec; /* UTC time at last 'time update'. */
46static DEFINE_SPINLOCK(wc_lock);
47
48struct time_scale {
49    int shift;
50    u32 mul_frac;
51};
52
53struct cpu_time {
54    u64 local_tsc_stamp;
55    s_time_t stime_local_stamp;
56    s_time_t stime_master_stamp;
57    struct time_scale tsc_scale;
58    struct timer calibration_timer;
59};
60
61static DEFINE_PER_CPU(struct cpu_time, cpu_time);
62
63/*
64 * Protected by platform_timer_lock, which must be acquired with interrupts
65 * disabled because pit_overflow() is called from PIT ch0 interrupt context.
66 */
67static s_time_t stime_platform_stamp;
68static u64 platform_timer_stamp;
69static struct time_scale platform_timer_scale;
70static DEFINE_SPINLOCK(platform_timer_lock);
71static u64 (*read_platform_count)(void);
72
73/*
74 * Folding 16-bit PIT into 64-bit software counter is a really critical
75 * operation! We therefore do it directly in PIT ch0 interrupt handler,
76 * based on this flag.
77 */
78static int using_pit;
79static void pit_overflow(void);
80
81/*
82 * 32-bit division of integer dividend and integer divisor yielding
83 * 32-bit fractional quotient.
84 */
85static inline u32 div_frac(u32 dividend, u32 divisor)
86{
87    u32 quotient, remainder;
88    ASSERT(dividend < divisor);
89    __asm__ ( 
90        "divl %4"
91        : "=a" (quotient), "=d" (remainder)
92        : "0" (0), "1" (dividend), "r" (divisor) );
93    return quotient;
94}
95
96/*
97 * 32-bit multiplication of multiplicand and fractional multiplier
98 * yielding 32-bit product (radix point at same position as in multiplicand).
99 */
100static inline u32 mul_frac(u32 multiplicand, u32 multiplier)
101{
102    u32 product_int, product_frac;
103    __asm__ (
104        "mul %3"
105        : "=a" (product_frac), "=d" (product_int)
106        : "0" (multiplicand), "r" (multiplier) );
107    return product_int;
108}
109
110/*
111 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
112 * yielding a 64-bit result.
113 */
114static inline u64 scale_delta(u64 delta, struct time_scale *scale)
115{
116    u64 product;
117#ifdef CONFIG_X86_32
118    u32 tmp1, tmp2;
119#endif
120
121    if ( scale->shift < 0 )
122        delta >>= -scale->shift;
123    else
124        delta <<= scale->shift;
125
126#ifdef CONFIG_X86_32
127    __asm__ (
128        "mul  %5       ; "
129        "mov  %4,%%eax ; "
130        "mov  %%edx,%4 ; "
131        "mul  %5       ; "
132        "xor  %5,%5    ; "
133        "add  %4,%%eax ; "
134        "adc  %5,%%edx ; "
135        : "=A" (product), "=r" (tmp1), "=r" (tmp2)
136        : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (scale->mul_frac) );
137#else
138    __asm__ (
139        "mul %%rdx ; shrd $32,%%rdx,%%rax"
140        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
141#endif
142
143    return product;
144}
145
146void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
147{
148    ASSERT(local_irq_is_enabled());
149
150    /* Update jiffies counter. */
151    (*(volatile unsigned long *)&jiffies)++;
152
153    /* Rough hack to allow accurate timers to sort-of-work with no APIC. */
154    if ( !cpu_has_apic )
155        raise_softirq(TIMER_SOFTIRQ);
156
157    if ( using_pit )
158        pit_overflow();
159}
160
161static struct irqaction irq0 = { timer_interrupt, "timer", NULL};
162
163/* ------ Calibrate the TSC -------
164 * Return processor ticks per second / CALIBRATE_FRAC.
165 */
166
167#define CLOCK_TICK_RATE 1193180 /* system crystal frequency (Hz) */
168#define CALIBRATE_FRAC  20      /* calibrate over 50ms */
169#define CALIBRATE_LATCH ((CLOCK_TICK_RATE+(CALIBRATE_FRAC/2))/CALIBRATE_FRAC)
170
171static u64 calibrate_boot_tsc(void)
172{
173    u64 start, end;
174    unsigned long count;
175
176    /* Set the Gate high, disable speaker */
177    outb((inb(0x61) & ~0x02) | 0x01, 0x61);
178
179    /*
180     * Now let's take care of CTC channel 2
181     *
182     * Set the Gate high, program CTC channel 2 for mode 0, (interrupt on
183     * terminal count mode), binary count, load 5 * LATCH count, (LSB and MSB)
184     * to begin countdown.
185     */
186    outb(0xb0, PIT_MODE);           /* binary, mode 0, LSB/MSB, Ch 2 */
187    outb(CALIBRATE_LATCH & 0xff, PIT_CH2); /* LSB of count */
188    outb(CALIBRATE_LATCH >> 8, PIT_CH2);   /* MSB of count */
189
190    rdtscll(start);
191    for ( count = 0; (inb(0x61) & 0x20) == 0; count++ )
192        continue;
193    rdtscll(end);
194
195    /* Error if the CTC doesn't behave itself. */
196    if ( count == 0 )
197        return 0;
198
199    return ((end - start) * (u64)CALIBRATE_FRAC);
200}
201
202static void set_time_scale(struct time_scale *ts, u64 ticks_per_sec)
203{
204    u64 tps64 = ticks_per_sec;
205    u32 tps32;
206    int shift = 0;
207
208    while ( tps64 > (MILLISECS(1000)*2) )
209    {
210        tps64 >>= 1;
211        shift--;
212    }
213
214    tps32 = (u32)tps64;
215    while ( tps32 < (u32)MILLISECS(1000) )
216    {
217        tps32 <<= 1;
218        shift++;
219    }
220
221    ts->mul_frac = div_frac(MILLISECS(1000), tps32);
222    ts->shift    = shift;
223}
224
225static atomic_t tsc_calibrate_gang = ATOMIC_INIT(0);
226static unsigned int tsc_calibrate_status = 0;
227
228void calibrate_tsc_bp(void)
229{
230    while ( atomic_read(&tsc_calibrate_gang) != (num_booting_cpus() - 1) )
231        mb();
232
233    outb(CALIBRATE_LATCH & 0xff, PIT_CH2);
234    outb(CALIBRATE_LATCH >> 8, PIT_CH2);
235
236    tsc_calibrate_status = 1;
237    wmb();
238
239    while ( (inb(0x61) & 0x20) == 0 )
240        continue;
241
242    tsc_calibrate_status = 2;
243    wmb();
244
245    while ( atomic_read(&tsc_calibrate_gang) != 0 )
246        mb();
247}
248
249void calibrate_tsc_ap(void)
250{
251    u64 t1, t2, ticks_per_sec;
252
253    atomic_inc(&tsc_calibrate_gang);
254
255    while ( tsc_calibrate_status < 1 )
256        mb();
257
258    rdtscll(t1);
259
260    while ( tsc_calibrate_status < 2 )
261        mb();
262
263    rdtscll(t2);
264
265    ticks_per_sec = (t2 - t1) * (u64)CALIBRATE_FRAC;
266    set_time_scale(&this_cpu(cpu_time).tsc_scale, ticks_per_sec);
267
268    atomic_dec(&tsc_calibrate_gang);
269}
270
271static char *freq_string(u64 freq)
272{
273    static char s[20];
274    unsigned int x, y;
275    y = (unsigned int)do_div(freq, 1000000) / 1000;
276    x = (unsigned int)freq;
277    snprintf(s, sizeof(s), "%u.%03uMHz", x, y);
278    return s;
279}
280
281/************************************************************
282 * PLATFORM TIMER 1: PROGRAMMABLE INTERVAL TIMER (LEGACY PIT)
283 */
284
285/* Protected by platform_timer_lock. */
286static u64 pit_counter64;
287static u16 pit_stamp;
288
289static u16 pit_read_counter(void)
290{
291    u16 count;
292    ASSERT(spin_is_locked(&platform_timer_lock));
293    outb(0x80, PIT_MODE);
294    count  = inb(PIT_CH2);
295    count |= inb(PIT_CH2) << 8;
296    return count;
297}
298
299static void pit_overflow(void)
300{
301    u16 counter;
302
303    spin_lock_irq(&platform_timer_lock);
304    counter = pit_read_counter();
305    pit_counter64 += (u16)(pit_stamp - counter);
306    pit_stamp = counter;
307    spin_unlock_irq(&platform_timer_lock);
308}
309
310static u64 read_pit_count(void)
311{
312    return pit_counter64 + (u16)(pit_stamp - pit_read_counter());
313}
314
315static void init_pit(void)
316{
317    read_platform_count = read_pit_count;
318
319    pit_overflow();
320    platform_timer_stamp = pit_counter64;
321    set_time_scale(&platform_timer_scale, CLOCK_TICK_RATE);
322
323    printk("Platform timer is %s PIT\n", freq_string(CLOCK_TICK_RATE));
324    using_pit = 1;
325}
326
327/************************************************************
328 * PLATFORM TIMER 2: HIGH PRECISION EVENT TIMER (HPET)
329 */
330
331/* Protected by platform_timer_lock. */
332static u64 hpet_counter64, hpet_overflow_period;
333static u32 hpet_stamp;
334static struct timer hpet_overflow_timer;
335
336static void hpet_overflow(void *unused)
337{
338    u32 counter;
339
340    spin_lock_irq(&platform_timer_lock);
341    counter = hpet_read32(HPET_COUNTER);
342    hpet_counter64 += (u32)(counter - hpet_stamp);
343    hpet_stamp = counter;
344    spin_unlock_irq(&platform_timer_lock);
345
346    set_timer(&hpet_overflow_timer, NOW() + hpet_overflow_period);
347}
348
349static u64 read_hpet_count(void)
350{
351    return hpet_counter64 + (u32)(hpet_read32(HPET_COUNTER) - hpet_stamp);
352}
353
354static int init_hpet(void)
355{
356    u64 hpet_rate;
357    u32 hpet_id, hpet_period, cfg;
358    int i;
359
360    if ( (hpet_address == 0) && opt_hpet_force )
361    {
362        outl(0x800038a0, 0xcf8);
363        outl(0xff000001, 0xcfc);
364        outl(0x800038a0, 0xcf8);
365        hpet_address = inl(0xcfc) & 0xfffffffe;
366        printk("WARNING: Forcibly enabled HPET at %#lx.\n", hpet_address);
367    }
368
369    if ( hpet_address == 0 )
370        return 0;
371
372    set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
373
374    hpet_id = hpet_read32(HPET_ID);
375    if ( hpet_id == 0 )
376    {
377        printk("BAD HPET vendor id.\n");
378        return 0;
379    }
380
381    /* Check for sane period (100ps <= period <= 100ns). */
382    hpet_period = hpet_read32(HPET_PERIOD);
383    if ( (hpet_period > 100000000) || (hpet_period < 100000) )
384    {
385        printk("BAD HPET period %u.\n", hpet_period);
386        return 0;
387    }
388
389    cfg = hpet_read32(HPET_CFG);
390    cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
391    hpet_write32(cfg, HPET_CFG);
392
393    for ( i = 0; i <= ((hpet_id >> 8) & 31); i++ )
394    {
395        cfg = hpet_read32(HPET_T0_CFG + i*0x20);
396        cfg &= ~HPET_TN_ENABLE;
397        hpet_write32(cfg & ~HPET_TN_ENABLE, HPET_T0_CFG);
398    }
399
400    cfg = hpet_read32(HPET_CFG);
401    cfg |= HPET_CFG_ENABLE;
402    hpet_write32(cfg, HPET_CFG);
403
404    read_platform_count = read_hpet_count;
405
406    hpet_rate = 1000000000000000ULL; /* 10^15 */
407    (void)do_div(hpet_rate, hpet_period);
408    set_time_scale(&platform_timer_scale, hpet_rate);
409
410    /* Trigger overflow avoidance roughly when counter increments 2^31. */
411    if ( (hpet_rate >> 31) != 0 )
412    {
413        hpet_overflow_period = MILLISECS(1000);
414        (void)do_div(hpet_overflow_period, (u32)(hpet_rate >> 31) + 1);
415    }
416    else
417    {
418        hpet_overflow_period = MILLISECS(1000) << 31;
419        (void)do_div(hpet_overflow_period, (u32)hpet_rate);
420    }
421
422    init_timer(&hpet_overflow_timer, hpet_overflow, NULL, 0);
423    hpet_overflow(NULL);
424    platform_timer_stamp = hpet_counter64;
425
426    printk("Platform timer is %s HPET\n", freq_string(hpet_rate));
427
428    return 1;
429}
430
431/************************************************************
432 * PLATFORM TIMER 3: IBM 'CYCLONE' TIMER
433 */
434
435int use_cyclone;
436
437/*
438 * Although the counter is read via a 64-bit register, I believe it is actually
439 * a 40-bit counter. Since this will wrap, I read only the low 32 bits and
440 * periodically fold into a 64-bit software counter, just as for PIT and HPET.
441 */
442#define CYCLONE_CBAR_ADDR   0xFEB00CD0
443#define CYCLONE_PMCC_OFFSET 0x51A0
444#define CYCLONE_MPMC_OFFSET 0x51D0
445#define CYCLONE_MPCS_OFFSET 0x51A8
446#define CYCLONE_TIMER_FREQ  100000000
447
448/* Protected by platform_timer_lock. */
449static u64 cyclone_counter64;
450static u32 cyclone_stamp;
451static struct timer cyclone_overflow_timer;
452static volatile u32 *cyclone_timer; /* Cyclone MPMC0 register */
453
454static void cyclone_overflow(void *unused)
455{
456    u32 counter;
457
458    spin_lock_irq(&platform_timer_lock);
459    counter = *cyclone_timer;
460    cyclone_counter64 += (u32)(counter - cyclone_stamp);
461    cyclone_stamp = counter;
462    spin_unlock_irq(&platform_timer_lock);
463
464    set_timer(&cyclone_overflow_timer, NOW() + MILLISECS(20000));
465}
466
467static u64 read_cyclone_count(void)
468{
469    return cyclone_counter64 + (u32)(*cyclone_timer - cyclone_stamp);
470}
471
472static volatile u32 *map_cyclone_reg(unsigned long regaddr)
473{
474    unsigned long pageaddr = regaddr &  PAGE_MASK;
475    unsigned long offset   = regaddr & ~PAGE_MASK;
476    set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);
477    return (volatile u32 *)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);
478}
479
480static int init_cyclone(void)
481{
482    u32 base;
483   
484    if ( !use_cyclone )
485        return 0;
486
487    /* Find base address. */
488    base = *(map_cyclone_reg(CYCLONE_CBAR_ADDR));
489    if ( base == 0 )
490    {
491        printk(KERN_ERR "Cyclone: Could not find valid CBAR value.\n");
492        return 0;
493    }
494 
495    /* Enable timer and map the counter register. */
496    *(map_cyclone_reg(base + CYCLONE_PMCC_OFFSET)) = 1;
497    *(map_cyclone_reg(base + CYCLONE_MPCS_OFFSET)) = 1;
498    cyclone_timer = map_cyclone_reg(base + CYCLONE_MPMC_OFFSET);
499
500    read_platform_count = read_cyclone_count;
501
502    init_timer(&cyclone_overflow_timer, cyclone_overflow, NULL, 0);
503    cyclone_overflow(NULL);
504    platform_timer_stamp = cyclone_counter64;
505    set_time_scale(&platform_timer_scale, CYCLONE_TIMER_FREQ);
506
507    printk("Platform timer is %s IBM Cyclone\n",
508           freq_string(CYCLONE_TIMER_FREQ));
509
510    return 1;
511}
512
513/************************************************************
514 * GENERIC PLATFORM TIMER INFRASTRUCTURE
515 */
516
517static s_time_t __read_platform_stime(u64 platform_time)
518{
519    u64 diff = platform_time - platform_timer_stamp;
520    ASSERT(spin_is_locked(&platform_timer_lock));
521    return (stime_platform_stamp + scale_delta(diff, &platform_timer_scale));
522}
523
524static s_time_t read_platform_stime(void)
525{
526    u64 counter;
527    s_time_t stime;
528
529    spin_lock_irq(&platform_timer_lock);
530    counter = read_platform_count();
531    stime   = __read_platform_stime(counter);
532    spin_unlock_irq(&platform_timer_lock);
533
534    return stime;
535}
536
537static void platform_time_calibration(void)
538{
539    u64 counter;
540    s_time_t stamp;
541
542    spin_lock_irq(&platform_timer_lock);
543    counter = read_platform_count();
544    stamp   = __read_platform_stime(counter);
545    stime_platform_stamp = stamp;
546    platform_timer_stamp = counter;
547    spin_unlock_irq(&platform_timer_lock);
548}
549
550static void init_platform_timer(void)
551{
552    if ( !init_cyclone() && !init_hpet() )
553        init_pit();
554}
555
556
557/***************************************************************************
558 * CMOS Timer functions
559 ***************************************************************************/
560
561/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
562 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
563 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
564 *
565 * [For the Julian calendar (which was used in Russia before 1917,
566 * Britain & colonies before 1752, anywhere else before 1582,
567 * and is still in use by some communities) leave out the
568 * -year/100+year/400 terms, and add 10.]
569 *
570 * This algorithm was first published by Gauss (I think).
571 *
572 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
573 * machines were long is 32-bit! (However, as time_t is signed, we
574 * will already get problems at other places on 2038-01-19 03:14:08)
575 */
576unsigned long
577mktime (unsigned int year, unsigned int mon,
578        unsigned int day, unsigned int hour,
579        unsigned int min, unsigned int sec)
580{
581    /* 1..12 -> 11,12,1..10: put Feb last since it has a leap day. */
582    if ( 0 >= (int) (mon -= 2) )
583    {
584        mon += 12;
585        year -= 1;
586    }
587
588    return ((((unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day)+
589              year*365 - 719499
590        )*24 + hour /* now have hours */
591        )*60 + min  /* now have minutes */
592        )*60 + sec; /* finally seconds */
593}
594
595static unsigned long __get_cmos_time(void)
596{
597    unsigned int year, mon, day, hour, min, sec;
598
599    sec  = CMOS_READ(RTC_SECONDS);
600    min  = CMOS_READ(RTC_MINUTES);
601    hour = CMOS_READ(RTC_HOURS);
602    day  = CMOS_READ(RTC_DAY_OF_MONTH);
603    mon  = CMOS_READ(RTC_MONTH);
604    year = CMOS_READ(RTC_YEAR);
605   
606    if ( !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD )
607    {
608        BCD_TO_BIN(sec);
609        BCD_TO_BIN(min);
610        BCD_TO_BIN(hour);
611        BCD_TO_BIN(day);
612        BCD_TO_BIN(mon);
613        BCD_TO_BIN(year);
614    }
615
616    if ( (year += 1900) < 1970 )
617        year += 100;
618
619    return mktime(year, mon, day, hour, min, sec);
620}
621
622static unsigned long get_cmos_time(void)
623{
624    unsigned long res, flags;
625    int i;
626
627    spin_lock_irqsave(&rtc_lock, flags);
628
629    /* read RTC exactly on falling edge of update flag */
630    for ( i = 0 ; i < 1000000 ; i++ ) /* may take up to 1 second... */
631        if ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
632            break;
633    for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
634        if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
635            break;
636
637    res = __get_cmos_time();
638
639    spin_unlock_irqrestore(&rtc_lock, flags);
640    return res;
641}
642
643/***************************************************************************
644 * System Time
645 ***************************************************************************/
646
647s_time_t get_s_time(void)
648{
649    struct cpu_time *t = &this_cpu(cpu_time);
650    u64 tsc, delta;
651    s_time_t now;
652
653    rdtscll(tsc);
654    delta = tsc - t->local_tsc_stamp;
655    now = t->stime_local_stamp + scale_delta(delta, &t->tsc_scale);
656
657    return now;
658}
659
660static inline void version_update_begin(u32 *version)
661{
662    /* Explicitly OR with 1 just in case version number gets out of sync. */
663    *version = (*version + 1) | 1;
664    wmb();
665}
666
667static inline void version_update_end(u32 *version)
668{
669    wmb();
670    (*version)++;
671}
672
673void update_vcpu_system_time(struct vcpu *v)
674{
675    struct cpu_time       *t;
676    struct vcpu_time_info *u;
677
678    if ( v->vcpu_info == NULL )
679        return;
680
681    t = &this_cpu(cpu_time);
682    u = &vcpu_info(v, time);
683
684    if ( u->tsc_timestamp == t->local_tsc_stamp )
685        return;
686
687    version_update_begin(&u->version);
688
689    u->tsc_timestamp     = t->local_tsc_stamp;
690    u->system_time       = t->stime_local_stamp;
691    u->tsc_to_system_mul = t->tsc_scale.mul_frac;
692    u->tsc_shift         = (s8)t->tsc_scale.shift;
693
694    version_update_end(&u->version);
695}
696
697void update_domain_wallclock_time(struct domain *d)
698{
699    spin_lock(&wc_lock);
700    version_update_begin(&shared_info(d, wc_version));
701    shared_info(d, wc_sec)  = wc_sec + d->time_offset_seconds;
702    shared_info(d, wc_nsec) = wc_nsec;
703    version_update_end(&shared_info(d, wc_version));
704    spin_unlock(&wc_lock);
705}
706
707/* Set clock to <secs,usecs> after 00:00:00 UTC, 1 January, 1970. */
708void do_settime(unsigned long secs, unsigned long nsecs, u64 system_time_base)
709{
710    u64 x;
711    u32 y, _wc_sec, _wc_nsec;
712    struct domain *d;
713
714    x = (secs * 1000000000ULL) + (u64)nsecs - system_time_base;
715    y = do_div(x, 1000000000);
716
717    spin_lock(&wc_lock);
718    wc_sec  = _wc_sec  = (u32)x;
719    wc_nsec = _wc_nsec = (u32)y;
720    spin_unlock(&wc_lock);
721
722    rcu_read_lock(&domlist_read_lock);
723    for_each_domain ( d )
724        update_domain_wallclock_time(d);
725    rcu_read_unlock(&domlist_read_lock);
726}
727
728static void local_time_calibration(void *unused)
729{
730    struct cpu_time *t = &this_cpu(cpu_time);
731
732    /*
733     * System timestamps, extrapolated from local and master oscillators,
734     * taken during this calibration and the previous calibration.
735     */
736    s_time_t prev_local_stime, curr_local_stime;
737    s_time_t prev_master_stime, curr_master_stime;
738
739    /* TSC timestamps taken during this calibration and prev calibration. */
740    u64 prev_tsc, curr_tsc;
741
742    /*
743     * System time and TSC ticks elapsed during the previous calibration
744     * 'epoch'. These values are down-shifted to fit in 32 bits.
745     */
746    u64 stime_elapsed64, tsc_elapsed64;
747    u32 stime_elapsed32, tsc_elapsed32;
748
749    /* The accumulated error in the local estimate. */
750    u64 local_stime_err;
751
752    /* Error correction to slow down a fast local clock. */
753    u32 error_factor = 0;
754
755    /* Calculated TSC shift to ensure 32-bit scale multiplier. */
756    int tsc_shift = 0;
757
758    /* The overall calibration scale multiplier. */
759    u32 calibration_mul_frac;
760
761    prev_tsc          = t->local_tsc_stamp;
762    prev_local_stime  = t->stime_local_stamp;
763    prev_master_stime = t->stime_master_stamp;
764
765    /* Disable IRQs to get 'instantaneous' current timestamps. */
766    local_irq_disable();
767    rdtscll(curr_tsc);
768    curr_local_stime  = get_s_time();
769    curr_master_stime = read_platform_stime();
770    local_irq_enable();
771
772#if 0
773    printk("PRE%d: tsc=%"PRIu64" stime=%"PRIu64" master=%"PRIu64"\n",
774           smp_processor_id(), prev_tsc, prev_local_stime, prev_master_stime);
775    printk("CUR%d: tsc=%"PRIu64" stime=%"PRIu64" master=%"PRIu64
776           " -> %"PRId64"\n",
777           smp_processor_id(), curr_tsc, curr_local_stime, curr_master_stime,
778           curr_master_stime - curr_local_stime);
779#endif
780
781    /* Local time warps forward if it lags behind master time. */
782    if ( curr_local_stime < curr_master_stime )
783        curr_local_stime = curr_master_stime;
784
785    stime_elapsed64 = curr_master_stime - prev_master_stime;
786    tsc_elapsed64   = curr_tsc - prev_tsc;
787
788    /*
789     * Weirdness can happen if we lose sync with the platform timer.
790     * We could be smarter here: resync platform timer with local timer?
791     */
792    if ( ((s64)stime_elapsed64 < (EPOCH / 2)) )
793        goto out;
794
795    /*
796     * Calculate error-correction factor. This only slows down a fast local
797     * clock (slow clocks are warped forwards). The scale factor is clamped
798     * to >= 0.5.
799     */
800    if ( curr_local_stime != curr_master_stime )
801    {
802        local_stime_err = curr_local_stime - curr_master_stime;
803        if ( local_stime_err > EPOCH )
804            local_stime_err = EPOCH;
805        error_factor = div_frac(EPOCH, EPOCH + (u32)local_stime_err);
806    }
807
808    /*
809     * We require 0 < stime_elapsed < 2^31.
810     * This allows us to binary shift a 32-bit tsc_elapsed such that:
811     * stime_elapsed < tsc_elapsed <= 2*stime_elapsed
812     */
813    while ( ((u32)stime_elapsed64 != stime_elapsed64) ||
814            ((s32)stime_elapsed64 < 0) )
815    {
816        stime_elapsed64 >>= 1;
817        tsc_elapsed64   >>= 1;
818    }
819
820    /* stime_master_diff now fits in a 32-bit word. */
821    stime_elapsed32 = (u32)stime_elapsed64;
822
823    /* tsc_elapsed <= 2*stime_elapsed */
824    while ( tsc_elapsed64 > (stime_elapsed32 * 2) )
825    {
826        tsc_elapsed64 >>= 1;
827        tsc_shift--;
828    }
829
830    /* Local difference must now fit in 32 bits. */
831    ASSERT((u32)tsc_elapsed64 == tsc_elapsed64);
832    tsc_elapsed32 = (u32)tsc_elapsed64;
833
834    /* tsc_elapsed > stime_elapsed */
835    ASSERT(tsc_elapsed32 != 0);
836    while ( tsc_elapsed32 <= stime_elapsed32 )
837    {
838        tsc_elapsed32 <<= 1;
839        tsc_shift++;
840    }
841
842    calibration_mul_frac = div_frac(stime_elapsed32, tsc_elapsed32);
843    if ( error_factor != 0 )
844        calibration_mul_frac = mul_frac(calibration_mul_frac, error_factor);
845
846#if 0
847    printk("---%d: %08x %08x %d\n", smp_processor_id(),
848           error_factor, calibration_mul_frac, tsc_shift);
849#endif
850
851    /* Record new timestamp information. */
852    t->tsc_scale.mul_frac = calibration_mul_frac;
853    t->tsc_scale.shift    = tsc_shift;
854    t->local_tsc_stamp    = curr_tsc;
855    t->stime_local_stamp  = curr_local_stime;
856    t->stime_master_stamp = curr_master_stime;
857
858    update_vcpu_system_time(current);
859
860 out:
861    set_timer(&t->calibration_timer, NOW() + EPOCH);
862
863    if ( smp_processor_id() == 0 )
864        platform_time_calibration();
865}
866
867void init_percpu_time(void)
868{
869    struct cpu_time *t = &this_cpu(cpu_time);
870    unsigned long flags;
871    s_time_t now;
872
873    local_irq_save(flags);
874    rdtscll(t->local_tsc_stamp);
875    now = (smp_processor_id() == 0) ? 0 : read_platform_stime();
876    local_irq_restore(flags);
877
878    t->stime_master_stamp = now;
879    t->stime_local_stamp  = now;
880
881    init_timer(&t->calibration_timer, local_time_calibration,
882               NULL, smp_processor_id());
883    set_timer(&t->calibration_timer, NOW() + EPOCH);
884}
885
886/* Late init function (after all CPUs are booted). */
887int __init init_xen_time(void)
888{
889    wc_sec = get_cmos_time();
890
891    local_irq_disable();
892
893    init_percpu_time();
894
895    stime_platform_stamp = 0;
896    init_platform_timer();
897
898    local_irq_enable();
899
900    return 0;
901}
902
903
904/* Early init function. */
905void __init early_time_init(void)
906{
907    u64 tmp = calibrate_boot_tsc();
908
909    set_time_scale(&per_cpu(cpu_time, 0).tsc_scale, tmp);
910
911    do_div(tmp, 1000);
912    cpu_khz = (unsigned long)tmp;
913    printk("Detected %lu.%03lu MHz processor.\n", 
914           cpu_khz / 1000, cpu_khz % 1000);
915
916    setup_irq(0, &irq0);
917}
918
919void send_timer_event(struct vcpu *v)
920{
921    send_guest_vcpu_virq(v, VIRQ_TIMER);
922}
923
924/* Return secs after 00:00:00 localtime, 1 January, 1970. */
925unsigned long get_localtime(struct domain *d)
926{
927    return wc_sec + (wc_nsec + NOW()) / 1000000000ULL 
928        + d->time_offset_seconds;
929}
930
931/*
932 * Local variables:
933 * mode: C
934 * c-set-style: "BSD"
935 * c-basic-offset: 4
936 * tab-width: 4
937 * indent-tabs-mode: nil
938 * End:
939 */
Note: See TracBrowser for help on using the repository browser.