debuggers.hg

view xen/arch/x86/time.c @ 3705:4294cfa9fad3

bitkeeper revision 1.1159.212.95 (4204aa0ee0re5Xx1zWrJ9ejxzgRs3w)

Various cleanups. Remove PDB pending simpler GDB stub and/or NetBSD debugger.
Force emacs mode to appropriate tabbing in various files.
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@scramble.cl.cam.ac.uk
date Sat Feb 05 11:12:14 2005 +0000 (2005-02-05)
parents b9ab4345fd1b
children 88957a238191
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil -*-
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/errno.h>
18 #include <xen/sched.h>
19 #include <xen/lib.h>
20 #include <xen/config.h>
21 #include <xen/init.h>
22 #include <xen/time.h>
23 #include <xen/ac_timer.h>
24 #include <xen/smp.h>
25 #include <xen/irq.h>
26 #include <xen/softirq.h>
27 #include <asm/io.h>
28 #include <asm/msr.h>
29 #include <asm/mpspec.h>
30 #include <asm/processor.h>
31 #include <asm/fixmap.h>
32 #include <asm/mc146818rtc.h>
34 /* GLOBAL */
35 unsigned long cpu_khz; /* Detected as we calibrate the TSC */
36 unsigned long ticks_per_usec; /* TSC ticks per microsecond. */
37 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
38 int timer_ack = 0;
39 int do_timer_lists_from_pit = 0;
40 unsigned long volatile jiffies;
42 /* PRIVATE */
43 static unsigned int rdtsc_bitshift; /* Which 32 bits of TSC do we use? */
44 static u64 cpu_freq; /* CPU frequency (Hz) */
45 static u32 st_scale_f; /* Cycles -> ns, fractional part */
46 static u32 st_scale_i; /* Cycles -> ns, integer part */
47 static u32 shifted_tsc_irq; /* CPU0's TSC at last 'time update' */
48 static u64 full_tsc_irq; /* ...ditto, but all 64 bits */
49 static s_time_t stime_irq; /* System time at last 'time update' */
50 static unsigned long wc_sec, wc_usec; /* UTC time at last 'time update'. */
51 static rwlock_t time_lock = RW_LOCK_UNLOCKED;
53 void timer_interrupt(int irq, void *dev_id, struct xen_regs *regs)
54 {
55 write_lock_irq(&time_lock);
57 #ifdef CONFIG_X86_IO_APIC
58 if ( timer_ack )
59 {
60 extern spinlock_t i8259A_lock;
61 spin_lock(&i8259A_lock);
62 outb(0x0c, 0x20);
63 /* Ack the IRQ; AEOI will end it automatically. */
64 inb(0x20);
65 spin_unlock(&i8259A_lock);
66 }
67 #endif
69 /*
70 * Updates TSC timestamp (used to interpolate passage of time between
71 * interrupts).
72 */
73 rdtscll(full_tsc_irq);
74 shifted_tsc_irq = (u32)(full_tsc_irq >> rdtsc_bitshift);
76 /* Update jiffies counter. */
77 (*(unsigned long *)&jiffies)++;
79 /* Update wall time. */
80 wc_usec += 1000000/HZ;
81 if ( wc_usec >= 1000000 )
82 {
83 wc_usec -= 1000000;
84 wc_sec++;
85 }
87 /* Updates system time (nanoseconds since boot). */
88 stime_irq += MILLISECS(1000/HZ);
90 write_unlock_irq(&time_lock);
92 /* Rough hack to allow accurate timers to sort-of-work with no APIC. */
93 if ( do_timer_lists_from_pit )
94 raise_softirq(AC_TIMER_SOFTIRQ);
95 }
97 static struct irqaction irq0 = { timer_interrupt, "timer", NULL};
99 /* ------ Calibrate the TSC -------
100 * Return processor ticks per second / CALIBRATE_FRAC.
101 */
103 #define CLOCK_TICK_RATE 1193180 /* system crystal frequency (Hz) */
104 #define CALIBRATE_FRAC 20 /* calibrate over 50ms */
105 #define CALIBRATE_LATCH ((CLOCK_TICK_RATE+(CALIBRATE_FRAC/2))/CALIBRATE_FRAC)
107 static unsigned long __init calibrate_tsc(void)
108 {
109 u64 start, end, diff;
110 unsigned long count;
112 /* Set the Gate high, disable speaker */
113 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
115 /*
116 * Now let's take care of CTC channel 2
117 *
118 * Set the Gate high, program CTC channel 2 for mode 0, (interrupt on
119 * terminal count mode), binary count, load 5 * LATCH count, (LSB and MSB)
120 * to begin countdown.
121 */
122 outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
123 outb(CALIBRATE_LATCH & 0xff, 0x42); /* LSB of count */
124 outb(CALIBRATE_LATCH >> 8, 0x42); /* MSB of count */
126 rdtscll(start);
127 for ( count = 0; (inb(0x61) & 0x20) == 0; count++ )
128 continue;
129 rdtscll(end);
131 /* Error if the CTC doesn't behave itself. */
132 if ( count == 0 )
133 return 0;
135 diff = end - start;
137 #if defined(_i386__)
138 /* If quotient doesn't fit in 32 bits then we return error (zero). */
139 if ( diff & ~0xffffffffULL )
140 return 0;
141 #endif
143 return (unsigned long)diff;
144 }
147 /***************************************************************************
148 * CMOS Timer functions
149 ***************************************************************************/
151 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
152 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
153 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
154 *
155 * [For the Julian calendar (which was used in Russia before 1917,
156 * Britain & colonies before 1752, anywhere else before 1582,
157 * and is still in use by some communities) leave out the
158 * -year/100+year/400 terms, and add 10.]
159 *
160 * This algorithm was first published by Gauss (I think).
161 *
162 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
163 * machines were long is 32-bit! (However, as time_t is signed, we
164 * will already get problems at other places on 2038-01-19 03:14:08)
165 */
166 static inline unsigned long
167 mktime (unsigned int year, unsigned int mon,
168 unsigned int day, unsigned int hour,
169 unsigned int min, unsigned int sec)
170 {
171 /* 1..12 -> 11,12,1..10: put Feb last since it has a leap day. */
172 if ( 0 >= (int) (mon -= 2) )
173 {
174 mon += 12;
175 year -= 1;
176 }
178 return ((((unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day)+
179 year*365 - 719499
180 )*24 + hour /* now have hours */
181 )*60 + min /* now have minutes */
182 )*60 + sec; /* finally seconds */
183 }
185 static unsigned long __get_cmos_time(void)
186 {
187 unsigned int year, mon, day, hour, min, sec;
189 sec = CMOS_READ(RTC_SECONDS);
190 min = CMOS_READ(RTC_MINUTES);
191 hour = CMOS_READ(RTC_HOURS);
192 day = CMOS_READ(RTC_DAY_OF_MONTH);
193 mon = CMOS_READ(RTC_MONTH);
194 year = CMOS_READ(RTC_YEAR);
196 if ( !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD )
197 {
198 BCD_TO_BIN(sec);
199 BCD_TO_BIN(min);
200 BCD_TO_BIN(hour);
201 BCD_TO_BIN(day);
202 BCD_TO_BIN(mon);
203 BCD_TO_BIN(year);
204 }
206 if ( (year += 1900) < 1970 )
207 year += 100;
209 return mktime(year, mon, day, hour, min, sec);
210 }
212 static unsigned long get_cmos_time(void)
213 {
214 unsigned long res, flags;
215 int i;
217 spin_lock_irqsave(&rtc_lock, flags);
219 /* read RTC exactly on falling edge of update flag */
220 for ( i = 0 ; i < 1000000 ; i++ ) /* may take up to 1 second... */
221 if ( (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
222 break;
223 for ( i = 0 ; i < 1000000 ; i++ ) /* must try at least 2.228 ms */
224 if ( !(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) )
225 break;
227 res = __get_cmos_time();
229 spin_unlock_irqrestore(&rtc_lock, flags);
230 return res;
231 }
233 /***************************************************************************
234 * System Time
235 ***************************************************************************/
237 static inline u64 get_time_delta(void)
238 {
239 s32 delta_tsc;
240 u32 low;
241 u64 delta, tsc;
243 rdtscll(tsc);
244 low = (u32)(tsc >> rdtsc_bitshift);
245 delta_tsc = (s32)(low - shifted_tsc_irq);
246 if ( unlikely(delta_tsc < 0) ) delta_tsc = 0;
247 delta = ((u64)delta_tsc * st_scale_f);
248 delta >>= 32;
249 delta += ((u64)delta_tsc * st_scale_i);
251 return delta;
252 }
254 s_time_t get_s_time(void)
255 {
256 s_time_t now;
257 unsigned long flags;
259 read_lock_irqsave(&time_lock, flags);
261 now = stime_irq + get_time_delta();
263 /* Ensure that the returned system time is monotonically increasing. */
264 {
265 static s_time_t prev_now = 0;
266 if ( unlikely(now < prev_now) )
267 now = prev_now;
268 prev_now = now;
269 }
271 read_unlock_irqrestore(&time_lock, flags);
273 return now;
274 }
277 void update_dom_time(struct domain *d)
278 {
279 shared_info_t *si = d->shared_info;
280 unsigned long flags;
282 read_lock_irqsave(&time_lock, flags);
284 spin_lock(&d->time_lock);
286 si->time_version1++;
287 wmb();
289 si->cpu_freq = cpu_freq;
290 si->tsc_timestamp = full_tsc_irq;
291 si->system_time = stime_irq;
292 si->wc_sec = wc_sec;
293 si->wc_usec = wc_usec;
295 wmb();
296 si->time_version2++;
298 spin_unlock(&d->time_lock);
300 read_unlock_irqrestore(&time_lock, flags);
301 }
304 /* Set clock to <secs,usecs> after 00:00:00 UTC, 1 January, 1970. */
305 void do_settime(unsigned long secs, unsigned long usecs, u64 system_time_base)
306 {
307 s64 delta;
308 long _usecs = (long)usecs;
310 write_lock_irq(&time_lock);
312 delta = (s64)(stime_irq - system_time_base);
314 _usecs += (long)(delta/1000);
315 while ( _usecs >= 1000000 )
316 {
317 _usecs -= 1000000;
318 secs++;
319 }
321 wc_sec = secs;
322 wc_usec = _usecs;
324 write_unlock_irq(&time_lock);
326 update_dom_time(current->domain);
327 }
330 /* Late init function (after all CPUs are booted). */
331 int __init init_xen_time()
332 {
333 u64 scale;
334 unsigned int cpu_ghz;
336 cpu_ghz = (unsigned int)(cpu_freq / 1000000000ULL);
337 for ( rdtsc_bitshift = 0; cpu_ghz != 0; rdtsc_bitshift++, cpu_ghz >>= 1 )
338 continue;
340 scale = 1000000000LL << (32 + rdtsc_bitshift);
341 scale /= cpu_freq;
342 st_scale_f = scale & 0xffffffff;
343 st_scale_i = scale >> 32;
345 /* System time ticks from zero. */
346 rdtscll(full_tsc_irq);
347 stime_irq = (s_time_t)0;
348 shifted_tsc_irq = (u32)(full_tsc_irq >> rdtsc_bitshift);
350 /* Wallclock time starts as the initial RTC time. */
351 wc_sec = get_cmos_time();
353 printk("Time init:\n");
354 printk(".... System Time: %lldns\n", NOW());
355 printk(".... cpu_freq: %08X:%08X\n", (u32)(cpu_freq>>32),(u32)cpu_freq);
356 printk(".... scale: %08X:%08X\n", (u32)(scale>>32),(u32)scale);
357 printk(".... Wall Clock: %lds %ldus\n", wc_sec, wc_usec);
359 return 0;
360 }
363 /* Early init function. */
364 void __init time_init(void)
365 {
366 unsigned long ticks_per_frac = calibrate_tsc();
368 if ( !ticks_per_frac )
369 panic("Error calibrating TSC\n");
371 ticks_per_usec = ticks_per_frac / (1000000/CALIBRATE_FRAC);
372 cpu_khz = ticks_per_frac / (1000/CALIBRATE_FRAC);
374 cpu_freq = (u64)ticks_per_frac * (u64)CALIBRATE_FRAC;
376 printk("Detected %lu.%03lu MHz processor.\n",
377 cpu_khz / 1000, cpu_khz % 1000);
379 setup_irq(0, &irq0);
380 }