ia64/xen-unstable

view xen/arch/x86/time.c @ 4336:cdbbf0845b2c

bitkeeper revision 1.1159.258.69 (4244984dfA_QIQFHRNZRU9hNBMo-lw)

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