2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz and Don Ahn.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 /* #define DELAYDEBUG */
44 * Routines to handle clock hardware.
48 #include "opt_clock.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/clock.h>
55 #include <sys/mutex.h>
58 #include <sys/timeet.h>
59 #include <sys/timetc.h>
60 #include <sys/kernel.h>
61 #include <sys/limits.h>
62 #include <sys/sysctl.h>
64 #include <sys/power.h>
66 #include <machine/clock.h>
67 #include <machine/cputypes.h>
68 #include <machine/frame.h>
69 #include <machine/intr_machdep.h>
70 #include <machine/md_var.h>
71 #include <machine/psl.h>
73 #include <machine/smp.h>
75 #include <machine/specialreg.h>
76 #include <machine/timerreg.h>
78 #include <x86/isa/icu.h>
79 #include <x86/isa/isa.h>
84 #include <machine/pmap.h>
85 #include <xen/hypervisor.h>
86 #include <xen/xen-os.h>
87 #include <machine/xen/xenfunc.h>
88 #include <xen/interface/vcpu.h>
89 #include <machine/cpu.h>
90 #include <xen/xen_intr.h>
93 * 32-bit time_t's can't reach leap years before 1904 or after 2036, so we
94 * can use a simple formula for leap years.
96 #define LEAPYEAR(y) (!((y) % 4))
97 #define DAYSPERYEAR (28+30*4+31*7)
100 #define TIMER_FREQ 1193182
103 #ifdef CYC2NS_SCALE_FACTOR
104 #undef CYC2NS_SCALE_FACTOR
106 #define CYC2NS_SCALE_FACTOR 10
108 /* Values for timerX_state: */
110 #define RELEASE_PENDING 1
112 #define ACQUIRE_PENDING 3
114 struct mtx clock_lock;
115 #define RTC_LOCK_INIT \
116 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE)
117 #define RTC_LOCK mtx_lock_spin(&clock_lock)
118 #define RTC_UNLOCK mtx_unlock_spin(&clock_lock)
119 #define NS_PER_TICK (1000000000ULL/hz)
121 int adjkerntz; /* local offset from GMT in seconds */
126 u_int timer_freq = TIMER_FREQ;
127 static u_long cyc2ns_scale;
128 static uint64_t processed_system_time; /* stime (ns) at last processing. */
130 extern volatile uint64_t xen_timer_last_time;
132 #define do_div(n,base) ({ \
133 unsigned long __upper, __low, __high, __mod, __base; \
135 __asm("":"=a" (__low), "=d" (__high):"A" (n)); \
138 __upper = __high % (__base); \
139 __high = __high / (__base); \
141 __asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
142 __asm("":"=A" (n):"a" (__low),"d" (__high)); \
147 /* convert from cycles(64bits) => nanoseconds (64bits)
149 * ns = cycles / (freq / ns_per_sec)
150 * ns = cycles * (ns_per_sec / freq)
151 * ns = cycles * (10^9 / (cpu_mhz * 10^6))
152 * ns = cycles * (10^3 / cpu_mhz)
154 * Then we use scaling math (suggested by george@mvista.com) to get:
155 * ns = cycles * (10^3 * SC / cpu_mhz) / SC
156 * ns = cycles * cyc2ns_scale / SC
158 * And since SC is a constant power of two, we can convert the div
160 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
162 static inline void set_cyc2ns_scale(unsigned long cpu_mhz)
164 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
167 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
169 return ((cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR);
175 return (xen_timer_last_time);
180 * XXX: timer needs more SMP work.
190 * Wait "n" microseconds.
191 * Relies on timer 1 counting down from (timer_freq / hz)
192 * Note: timer had better have been programmed before this is first used!
197 int delta, ticks_left;
198 uint32_t tick, prev_tick;
202 static int state = 0;
206 for (n1 = 1; n1 <= 10000000; n1 *= 10)
211 printf("DELAY(%d)...", n);
214 * Read the counter first, so that the rest of the setup overhead is
215 * counted. Guess the initial overhead is 20 usec (on most systems it
216 * takes about 1.5 usec for each of the i/o's in getit(). The loop
217 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
218 * multiplications and divisions to scale the count take a while).
220 * However, if ddb is active then use a fake counter since reading
221 * the i8254 counter involves acquiring a lock. ddb must not go
222 * locking for many reasons, but it calls here for at least atkbd
227 n -= 0; /* XXX actually guess no initial overhead */
229 * Calculate (n * (timer_freq / 1e6)) without using floating point
230 * and without any avoidable overflows.
236 * Use fixed point to avoid a slow division by 1000000.
237 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
238 * 2^15 is the first power of 2 that gives exact results
239 * for n between 0 and 256.
241 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
244 * Don't bother using fixed point, although gcc-2.7.2
245 * generates particularly poor code for the long long
246 * division, since even the slow way will complete long
247 * before the delay is up (unless we're interrupted).
249 ticks_left = ((u_int)n * (long long)timer_freq + 999999)
252 while (ticks_left > 0) {
257 delta = tick - prev_tick;
261 * Guard against timer0_max_count being wrong.
262 * This shouldn't happen in normal operation,
263 * but it may happen if set_timer_freq() is
266 /* delta += timer0_max_count; ??? */
274 printf(" %d calls to getit() at %d usec each\n",
275 getit_calls, (n + 5) / getit_calls);
284 struct vcpu_time_info *info;
286 __cpu_khz = 1000000ULL << 32;
287 info = &HYPERVISOR_shared_info->vcpu_info[0].time;
289 (void)do_div(__cpu_khz, info->tsc_to_system_mul);
290 if ( info->tsc_shift < 0 )
291 cpu_khz = __cpu_khz << -info->tsc_shift;
293 cpu_khz = __cpu_khz >> info->tsc_shift;
295 printf("Xen reported: %u.%03u MHz processor.\n",
296 cpu_khz / 1000, cpu_khz % 1000);
298 /* (10^6 * 2^32) / cpu_hz = (10^3 * 2^32) / cpu_khz =
299 (2^32 * 1 / (clocks/us)) */
301 set_cyc2ns_scale(cpu_khz/1000);
302 tsc_freq = cpu_khz * 1000;
306 * RTC support routines
313 return(bcd2bin(rtcin(port)));
317 #ifdef XEN_PRIVILEGED_GUEST
320 * Initialize the time of day register, based on the time base which is, e.g.
324 domu_inittodr(time_t base)
331 add_uptime_to_wallclock();
341 sec += tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
343 y = time_second - shadow_tv.tv_sec;
344 if (y <= -2 || y >= 2) {
345 /* badly off, adjust it */
346 tc_setclock(&shadow_tv);
352 * Write system time back to RTC.
360 struct shadow_time_info *shadow;
363 pc = pcpu_find(smp_processor_id());
364 shadow = &pc->pc_shadow_time;
365 if (xen_disable_rtc_set)
372 tm -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
374 if ((xen_start_info->flags & SIF_INITDOMAIN) &&
375 !independent_wallclock)
377 op.cmd = DOM0_SETTIME;
378 op.u.settime.secs = tm;
379 op.u.settime.nsecs = 0;
380 op.u.settime.system_time = shadow->system_timestamp;
381 HYPERVISOR_dom0_op(&op);
383 add_uptime_to_wallclock();
384 } else if (independent_wallclock) {
391 * Initialize the time of day register, based on the time base which is, e.g.
395 inittodr(time_t base)
397 unsigned long sec, days;
402 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
415 /* Look if we have a RTC present and the time is valid */
416 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
419 /* wait for time update to complete */
420 /* If RTCSA_TUP is zero, we have at least 244us before next update */
422 while (rtcin(RTC_STATUSA) & RTCSA_TUP) {
428 #ifdef USE_RTC_CENTURY
429 year = readrtc(RTC_YEAR) + readrtc(RTC_CENTURY) * 100;
431 year = readrtc(RTC_YEAR) + 1900;
439 month = readrtc(RTC_MONTH);
440 for (m = 1; m < month; m++)
441 days += daysinmonth[m-1];
442 if ((month > 2) && LEAPYEAR(year))
444 days += readrtc(RTC_DAY) - 1;
445 for (y = 1970; y < year; y++)
446 days += DAYSPERYEAR + LEAPYEAR(y);
447 sec = ((( days * 24 +
448 readrtc(RTC_HRS)) * 60 +
449 readrtc(RTC_MIN)) * 60 +
451 /* sec now contains the number of seconds, since Jan 1 1970,
452 in the local time zone */
454 sec += tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
456 y = time_second - sec;
457 if (y <= -2 || y >= 2) {
458 /* badly off, adjust it */
467 printf("Invalid time in real time clock.\n");
468 printf("Check and reset the date immediately!\n");
473 * Write system time back to RTC
481 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
486 if (xen_disable_rtc_set)
493 /* Disable RTC updates and interrupts. */
494 writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
496 /* Calculate local time to put in RTC */
498 tm -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
500 writertc(RTC_SEC, bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */
501 writertc(RTC_MIN, bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */
502 writertc(RTC_HRS, bin2bcd(tm%24)); tm /= 24; /* Write back Hours */
504 /* We have now the days since 01-01-1970 in tm */
505 writertc(RTC_WDAY, (tm + 4) % 7 + 1); /* Write back Weekday */
506 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y);
508 y++, m = DAYSPERYEAR + LEAPYEAR(y))
511 /* Now we have the years in y and the day-of-the-year in tm */
512 writertc(RTC_YEAR, bin2bcd(y%100)); /* Write back Year */
513 #ifdef USE_RTC_CENTURY
514 writertc(RTC_CENTURY, bin2bcd(y/100)); /* ... and Century */
520 if (m == 1 && LEAPYEAR(y))
527 writertc(RTC_MONTH, bin2bcd(m + 1)); /* Write back Month */
528 writertc(RTC_DAY, bin2bcd(tm + 1)); /* Write back Month Day */
530 /* Reenable RTC updates and interrupts. */
531 writertc(RTC_STATUSB, RTCSB_24HR);
537 * Start clocks running.
542 cpu_initclocks_bsp();
545 /* Return system time offset by ticks */
547 get_system_time(int ticks)
549 return (processed_system_time + (ticks * NS_PER_TICK));
553 timer_spkr_acquire(void)
560 timer_spkr_release(void)
567 timer_spkr_setfreq(int freq)