ia64/xen-unstable

view xen/arch/x86/time.c @ 4337:8bd47d443295

bitkeeper revision 1.1236.43.18 (42449899XrfAZaA5DQMd2TyF9OVOoQ)

Merge firebug.cl.cam.ac.uk:/local/scratch/kaf24/xen-2.0-testing.bk
into firebug.cl.cam.ac.uk:/local/scratch/kaf24/xen-unstable.bk
author kaf24@firebug.cl.cam.ac.uk
date Fri Mar 25 23:02:49 2005 +0000 (2005-03-25)
parents 46155de3064d cdbbf0845b2c
children a18e1426d4c8
line source
1 /****************************************************************************
2 * (C) 2002-2003 - Rolf Neugebauer - Intel Research Cambridge
3 * (C) 2002-2003 University of Cambridge
4 ****************************************************************************
5 *
6 * File: i386/time.c
7 * Author: Rolf Neugebar & Keir Fraser
8 */
10 /*
11 * linux/arch/i386/kernel/time.c
12 *
13 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
14 */
16 #include <xen/config.h>
17 #include <xen/errno.h>
18 #include <xen/event.h>
19 #include <xen/sched.h>
20 #include <xen/lib.h>
21 #include <xen/config.h>
22 #include <xen/init.h>
23 #include <xen/time.h>
24 #include <xen/ac_timer.h>
25 #include <xen/smp.h>
26 #include <xen/irq.h>
27 #include <xen/softirq.h>
28 #include <asm/io.h>
29 #include <asm/msr.h>
30 #include <asm/mpspec.h>
31 #include <asm/processor.h>
32 #include <asm/fixmap.h>
33 #include <asm/mc146818rtc.h>
35 /* GLOBAL */
36 unsigned long cpu_khz; /* Detected as we calibrate the TSC */
37 unsigned long ticks_per_usec; /* TSC ticks per microsecond. */
38 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
39 int timer_ack = 0;
40 int do_timer_lists_from_pit = 0;
41 unsigned long volatile jiffies;
43 /* PRIVATE */
44 static unsigned int rdtsc_bitshift; /* Which 32 bits of TSC do we use? */
45 static u64 cpu_freq; /* CPU frequency (Hz) */
46 static u32 st_scale_f; /* Cycles -> ns, fractional part */
47 static u32 st_scale_i; /* Cycles -> ns, integer part */
48 static u32 shifted_tsc_irq; /* CPU0's TSC at last 'time update' */
49 static u64 full_tsc_irq; /* ...ditto, but all 64 bits */
50 static s_time_t stime_irq; /* System time at last 'time update' */
51 static unsigned long wc_sec, wc_usec; /* UTC time at last 'time update'. */
52 static rwlock_t time_lock = RW_LOCK_UNLOCKED;
54 void timer_interrupt(int irq, void *dev_id, struct xen_regs *regs)
55 {
56 write_lock_irq(&time_lock);
58 #ifdef CONFIG_X86_IO_APIC
59 if ( timer_ack )
60 {
61 extern spinlock_t i8259A_lock;
62 spin_lock(&i8259A_lock);
63 outb(0x0c, 0x20);
64 /* Ack the IRQ; AEOI will end it automatically. */
65 inb(0x20);
66 spin_unlock(&i8259A_lock);
67 }
68 #endif
70 /*
71 * Updates TSC timestamp (used to interpolate passage of time between
72 * interrupts).
73 */
74 rdtscll(full_tsc_irq);
75 shifted_tsc_irq = (u32)(full_tsc_irq >> rdtsc_bitshift);
77 /* Update jiffies counter. */
78 (*(unsigned long *)&jiffies)++;
80 /* Update wall time. */
81 wc_usec += 1000000/HZ;
82 if ( wc_usec >= 1000000 )
83 {
84 wc_usec -= 1000000;
85 wc_sec++;
86 }
88 /* Updates system time (nanoseconds since boot). */
89 stime_irq += MILLISECS(1000/HZ);
91 write_unlock_irq(&time_lock);
93 /* Rough hack to allow accurate timers to sort-of-work with no APIC. */
94 if ( do_timer_lists_from_pit )
95 raise_softirq(AC_TIMER_SOFTIRQ);
96 }
98 static struct irqaction irq0 = { timer_interrupt, "timer", NULL};
100 /* ------ Calibrate the TSC -------
101 * Return processor ticks per second / CALIBRATE_FRAC.
102 */
104 #define CLOCK_TICK_RATE 1193180 /* system crystal frequency (Hz) */
105 #define CALIBRATE_FRAC 20 /* calibrate over 50ms */
106 #define CALIBRATE_LATCH ((CLOCK_TICK_RATE+(CALIBRATE_FRAC/2))/CALIBRATE_FRAC)
108 static unsigned long __init calibrate_tsc(void)
109 {
110 u64 start, end, diff;
111 unsigned long count;
113 /* Set the Gate high, disable speaker */
114 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
116 /*
117 * Now let's take care of CTC channel 2
118 *
119 * Set the Gate high, program CTC channel 2 for mode 0, (interrupt on
120 * terminal count mode), binary count, load 5 * LATCH count, (LSB and MSB)
121 * to begin countdown.
122 */
123 outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
124 outb(CALIBRATE_LATCH & 0xff, 0x42); /* LSB of count */
125 outb(CALIBRATE_LATCH >> 8, 0x42); /* MSB of count */
127 rdtscll(start);
128 for ( count = 0; (inb(0x61) & 0x20) == 0; count++ )
129 continue;
130 rdtscll(end);
132 /* Error if the CTC doesn't behave itself. */
133 if ( count == 0 )
134 return 0;
136 diff = end - start;
138 #if defined(__i386__)
139 /* If quotient doesn't fit in 32 bits then we return error (zero). */
140 if ( diff & ~0xffffffffULL )
141 return 0;
142 #endif
144 return (unsigned long)diff;
145 }
148 /***************************************************************************
149 * CMOS Timer functions
150 ***************************************************************************/
152 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
153 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
154 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
155 *
156 * [For the Julian calendar (which was used in Russia before 1917,
157 * Britain & colonies before 1752, anywhere else before 1582,
158 * and is still in use by some communities) leave out the
159 * -year/100+year/400 terms, and add 10.]
160 *
161 * This algorithm was first published by Gauss (I think).
162 *
163 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
164 * machines were long is 32-bit! (However, as time_t is signed, we
165 * will already get problems at other places on 2038-01-19 03:14:08)
166 */
167 static inline unsigned long
168 mktime (unsigned int year, unsigned int mon,
169 unsigned int day, unsigned int hour,
170 unsigned int min, unsigned int sec)
171 {
172 /* 1..12 -> 11,12,1..10: put Feb last since it has a leap day. */
173 if ( 0 >= (int) (mon -= 2) )
174 {
175 mon += 12;
176 year -= 1;
177 }
179 return ((((unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day)+
180 year*365 - 719499
181 )*24 + hour /* now have hours */
182 )*60 + min /* now have minutes */
183 )*60 + sec; /* finally seconds */
184 }
186 static unsigned long __get_cmos_time(void)
187 {
188 unsigned int year, mon, day, hour, min, sec;
190 sec = CMOS_READ(RTC_SECONDS);
191 min = CMOS_READ(RTC_MINUTES);
192 hour = CMOS_READ(RTC_HOURS);
193 day = CMOS_READ(RTC_DAY_OF_MONTH);
194 mon = CMOS_READ(RTC_MONTH);
195 year = CMOS_READ(RTC_YEAR);
197 if ( !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD )
198 {
199 BCD_TO_BIN(sec);
200 BCD_TO_BIN(min);
201 BCD_TO_BIN(hour);
202 BCD_TO_BIN(day);
203 BCD_TO_BIN(mon);
204 BCD_TO_BIN(year);
205 }
207 if ( (year += 1900) < 1970 )
208 year += 100;
210 return mktime(year, mon, day, hour, min, sec);
211 }
213 static unsigned long get_cmos_time(void)
214 {
215 unsigned long res, flags;
216 int i;
218 spin_lock_irqsave(&rtc_lock, flags);
220 /* read RTC exactly on falling edge of update flag */
221 for ( i = 0 ; i < 1000000 ; i++ ) /* may take up to 1 second... */
222 if ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
223 break;
224 for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
225 if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
226 break;
228 res = __get_cmos_time();
230 spin_unlock_irqrestore(&rtc_lock, flags);
231 return res;
232 }
234 /***************************************************************************
235 * System Time
236 ***************************************************************************/
238 static inline u64 get_time_delta(void)
239 {
240 s32 delta_tsc;
241 u32 low;
242 u64 delta, tsc;
244 rdtscll(tsc);
245 low = (u32)(tsc >> rdtsc_bitshift);
246 delta_tsc = (s32)(low - shifted_tsc_irq);
247 if ( unlikely(delta_tsc < 0) ) delta_tsc = 0;
248 delta = ((u64)delta_tsc * st_scale_f);
249 delta >>= 32;
250 delta += ((u64)delta_tsc * st_scale_i);
252 return delta;
253 }
255 s_time_t get_s_time(void)
256 {
257 s_time_t now;
258 unsigned long flags;
260 read_lock_irqsave(&time_lock, flags);
262 now = stime_irq + get_time_delta();
264 /* Ensure that the returned system time is monotonically increasing. */
265 {
266 static s_time_t prev_now = 0;
267 if ( unlikely(now < prev_now) )
268 now = prev_now;
269 prev_now = now;
270 }
272 read_unlock_irqrestore(&time_lock, flags);
274 return now;
275 }
277 static inline void __update_dom_time(struct exec_domain *ed)
278 {
279 struct domain *d = ed->domain;
280 shared_info_t *si = d->shared_info;
282 spin_lock(&d->time_lock);
284 si->time_version1++;
285 wmb();
287 si->cpu_freq = cpu_freq;
288 si->tsc_timestamp = full_tsc_irq;
289 si->system_time = stime_irq;
290 si->wc_sec = wc_sec;
291 si->wc_usec = wc_usec;
293 wmb();
294 si->time_version2++;
296 spin_unlock(&d->time_lock);
297 }
299 void update_dom_time(struct exec_domain *ed)
300 {
301 unsigned long flags;
303 if ( ed->domain->shared_info->tsc_timestamp != full_tsc_irq )
304 {
305 read_lock_irqsave(&time_lock, flags);
306 __update_dom_time(ed);
307 read_unlock_irqrestore(&time_lock, flags);
308 }
309 }
311 /* Set clock to <secs,usecs> after 00:00:00 UTC, 1 January, 1970. */
312 void do_settime(unsigned long secs, unsigned long usecs, u64 system_time_base)
313 {
314 s64 delta;
315 long _usecs = (long)usecs;
317 write_lock_irq(&time_lock);
319 delta = (s64)(stime_irq - system_time_base);
321 _usecs += (long)(delta/1000);
322 while ( _usecs >= 1000000 )
323 {
324 _usecs -= 1000000;
325 secs++;
326 }
328 wc_sec = secs;
329 wc_usec = _usecs;
331 /* Others will pick up the change at the next tick. */
332 __update_dom_time(current);
333 send_guest_virq(current, VIRQ_TIMER);
335 write_unlock_irq(&time_lock);
336 }
339 /* Late init function (after all CPUs are booted). */
340 int __init init_xen_time()
341 {
342 u64 scale;
343 unsigned int cpu_ghz;
345 cpu_ghz = (unsigned int)(cpu_freq / 1000000000ULL);
346 for ( rdtsc_bitshift = 0; cpu_ghz != 0; rdtsc_bitshift++, cpu_ghz >>= 1 )
347 continue;
349 scale = 1000000000LL << (32 + rdtsc_bitshift);
350 scale /= cpu_freq;
351 st_scale_f = scale & 0xffffffff;
352 st_scale_i = scale >> 32;
354 /* System time ticks from zero. */
355 rdtscll(full_tsc_irq);
356 stime_irq = (s_time_t)0;
357 shifted_tsc_irq = (u32)(full_tsc_irq >> rdtsc_bitshift);
359 /* Wallclock time starts as the initial RTC time. */
360 wc_sec = get_cmos_time();
362 printk("Time init:\n");
363 printk(".... System Time: %lldns\n", NOW());
364 printk(".... cpu_freq: %08X:%08X\n", (u32)(cpu_freq>>32),(u32)cpu_freq);
365 printk(".... scale: %08X:%08X\n", (u32)(scale>>32),(u32)scale);
366 printk(".... Wall Clock: %lds %ldus\n", wc_sec, wc_usec);
368 return 0;
369 }
372 /* Early init function. */
373 void __init time_init(void)
374 {
375 unsigned long ticks_per_frac = calibrate_tsc();
377 if ( !ticks_per_frac )
378 panic("Error calibrating TSC\n");
380 ticks_per_usec = ticks_per_frac / (1000000/CALIBRATE_FRAC);
381 cpu_khz = ticks_per_frac / (1000/CALIBRATE_FRAC);
383 cpu_freq = (u64)ticks_per_frac * (u64)CALIBRATE_FRAC;
385 printk("Detected %lu.%03lu MHz processor.\n",
386 cpu_khz / 1000, cpu_khz % 1000);
388 setup_irq(0, &irq0);
389 }
391 /*
392 * Local variables:
393 * mode: C
394 * c-set-style: "BSD"
395 * c-basic-offset: 4
396 * tab-width: 4
397 * indent-tabs-mode: nil
398 * End:
399 */