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
37 * $FreeBSD: src/sys/i386/isa/clock.c,v 1.149.2.6 2002/11/02 04:41:50 iwasaki Exp $
41 * Routines to handle clock hardware.
45 * inittodr, settodr and support routines written
46 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>
48 * reintroduced and updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
51 #include "opt_clock.h"
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/eventhandler.h>
57 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
61 #include <sys/systimer.h>
62 #include <sys/globaldata.h>
63 #include <sys/thread2.h>
64 #include <sys/machintr.h>
65 #include <sys/interrupt.h>
67 #include <machine/clock.h>
68 #include <machine/cputypes.h>
69 #include <machine/frame.h>
70 #include <machine/ipl.h>
71 #include <machine/limits.h>
72 #include <machine/md_var.h>
73 #include <machine/psl.h>
74 #include <machine/segments.h>
75 #include <machine/smp.h>
76 #include <machine/specialreg.h>
78 #include <machine_base/apic/ioapic.h>
79 #include <machine_base/apic/ioapic_abi.h>
80 #include <machine_base/icu/icu.h>
81 #include <bus/isa/isa.h>
82 #include <bus/isa/rtc.h>
83 #include <machine_base/isa/timerreg.h>
85 #include <machine/intr_machdep.h>
87 static void i8254_restore(void);
88 static void resettodr_on_shutdown(void *arg __unused);
91 * 32-bit time_t's can't reach leap years before 1904 or after 2036, so we
92 * can use a simple formula for leap years.
94 #define LEAPYEAR(y) ((u_int)(y) % 4 == 0)
95 #define DAYSPERYEAR (31+28+31+30+31+30+31+31+30+31+30+31)
98 #define TIMER_FREQ 1193182
101 static uint8_t i8254_walltimer_sel;
102 static uint16_t i8254_walltimer_cntr;
104 int adjkerntz; /* local offset from GMT in seconds */
105 int disable_rtc_set; /* disable resettodr() if != 0 */
109 int64_t tsc_frequency;
111 int wall_cmos_clock; /* wall CMOS clock assumed if != 0 */
113 enum tstate { RELEASED, ACQUIRED };
114 enum tstate timer0_state;
115 enum tstate timer1_state;
116 enum tstate timer2_state;
118 static int beeping = 0;
119 static const u_char daysinmonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
120 static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
121 static u_char rtc_statusb = RTCSB_24HR | RTCSB_PINTR;
122 static int rtc_loaded;
124 static int i8254_cputimer_div;
126 static int i8254_nointr;
127 static int i8254_intr_disable = 1;
128 TUNABLE_INT("hw.i8254.intr_disable", &i8254_intr_disable);
130 static struct callout sysbeepstop_ch;
132 static sysclock_t i8254_cputimer_count(void);
133 static void i8254_cputimer_construct(struct cputimer *cputimer, sysclock_t last);
134 static void i8254_cputimer_destruct(struct cputimer *cputimer);
136 static struct cputimer i8254_cputimer = {
137 SLIST_ENTRY_INITIALIZER,
141 i8254_cputimer_count,
142 cputimer_default_fromhz,
143 cputimer_default_fromus,
144 i8254_cputimer_construct,
145 i8254_cputimer_destruct,
150 static sysclock_t tsc_cputimer_count(void);
151 static void tsc_cputimer_construct(struct cputimer *, sysclock_t);
153 static struct cputimer tsc_cputimer = {
154 SLIST_ENTRY_INITIALIZER,
159 cputimer_default_fromhz,
160 cputimer_default_fromus,
161 tsc_cputimer_construct,
162 cputimer_default_destruct,
167 static void i8254_intr_reload(struct cputimer_intr *, sysclock_t);
168 static void i8254_intr_config(struct cputimer_intr *, const struct cputimer *);
169 static void i8254_intr_initclock(struct cputimer_intr *, boolean_t);
171 static struct cputimer_intr i8254_cputimer_intr = {
173 .reload = i8254_intr_reload,
174 .enable = cputimer_intr_default_enable,
175 .config = i8254_intr_config,
176 .restart = cputimer_intr_default_restart,
177 .pmfixup = cputimer_intr_default_pmfixup,
178 .initclock = i8254_intr_initclock,
179 .next = SLIST_ENTRY_INITIALIZER,
181 .type = CPUTIMER_INTR_8254,
182 .prio = CPUTIMER_INTR_PRIO_8254,
183 .caps = CPUTIMER_INTR_CAP_PS
187 * timer0 clock interrupt. Timer0 is in one-shot mode and has stopped
188 * counting as of this interrupt. We use timer1 in free-running mode (not
189 * generating any interrupts) as our main counter. Each cpu has timeouts
192 * This code is INTR_MPSAFE and may be called without the BGL held.
195 clkintr(void *dummy, void *frame_arg)
197 static sysclock_t sysclock_count; /* NOTE! Must be static */
198 struct globaldata *gd = mycpu;
199 struct globaldata *gscan;
203 * SWSTROBE mode is a one-shot, the timer is no longer running
208 * XXX the dispatcher needs work. right now we call systimer_intr()
209 * directly or via IPI for any cpu with systimers queued, which is
210 * usually *ALL* of them. We need to use the LAPIC timer for this.
212 sysclock_count = sys_cputimer->count();
213 for (n = 0; n < ncpus; ++n) {
214 gscan = globaldata_find(n);
215 if (TAILQ_FIRST(&gscan->gd_systimerq) == NULL)
218 lwkt_send_ipiq3(gscan, (ipifunc3_t)systimer_intr,
221 systimer_intr(&sysclock_count, 0, frame_arg);
231 acquire_timer2(int mode)
233 if (timer2_state != RELEASED)
235 timer2_state = ACQUIRED;
238 * This access to the timer registers is as atomic as possible
239 * because it is a single instruction. We could do better if we
242 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f));
249 if (timer2_state != ACQUIRED)
251 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT);
252 timer2_state = RELEASED;
260 DB_SHOW_COMMAND(rtc, rtc)
262 kprintf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
263 rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
264 rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
265 rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));
270 * Return the current cpu timer count as a 32 bit integer.
274 i8254_cputimer_count(void)
276 static __uint16_t cputimer_last;
281 outb(TIMER_MODE, i8254_walltimer_sel | TIMER_LATCH);
282 count = (__uint8_t)inb(i8254_walltimer_cntr); /* get countdown */
283 count |= ((__uint8_t)inb(i8254_walltimer_cntr) << 8);
284 count = -count; /* -> countup */
285 if (count < cputimer_last) /* rollover */
286 i8254_cputimer.base += 0x00010000;
287 ret = i8254_cputimer.base | count;
288 cputimer_last = count;
294 * This function is called whenever the system timebase changes, allowing
295 * us to calculate what is needed to convert a system timebase tick
296 * into an 8254 tick for the interrupt timer. If we can convert to a
297 * simple shift, multiplication, or division, we do so. Otherwise 64
298 * bit arithmatic is required every time the interrupt timer is reloaded.
301 i8254_intr_config(struct cputimer_intr *cti, const struct cputimer *timer)
307 * Will a simple divide do the trick?
309 div = (timer->freq + (cti->freq / 2)) / cti->freq;
310 freq = cti->freq * div;
312 if (freq >= timer->freq - 1 && freq <= timer->freq + 1)
313 i8254_cputimer_div = div;
315 i8254_cputimer_div = 0;
319 * Reload for the next timeout. It is possible for the reload value
320 * to be 0 or negative, indicating that an immediate timer interrupt
321 * is desired. For now make the minimum 2 ticks.
323 * We may have to convert from the system timebase to the 8254 timebase.
326 i8254_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
330 if (i8254_cputimer_div)
331 reload /= i8254_cputimer_div;
333 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
339 if (timer0_running) {
340 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); /* count-down timer */
341 count = (__uint8_t)inb(TIMER_CNTR0); /* lsb */
342 count |= ((__uint8_t)inb(TIMER_CNTR0) << 8); /* msb */
343 if (reload < count) {
344 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
345 outb(TIMER_CNTR0, (__uint8_t)reload); /* lsb */
346 outb(TIMER_CNTR0, (__uint8_t)(reload >> 8)); /* msb */
351 reload = 0; /* full count */
352 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
353 outb(TIMER_CNTR0, (__uint8_t)reload); /* lsb */
354 outb(TIMER_CNTR0, (__uint8_t)(reload >> 8)); /* msb */
360 * DELAY(usec) - Spin for the specified number of microseconds.
361 * DRIVERSLEEP(usec) - Spin for the specified number of microseconds,
362 * but do a thread switch in the loop
364 * Relies on timer 1 counting down from (cputimer_freq / hz)
365 * Note: timer had better have been programmed before this is first used!
368 DODELAY(int n, int doswitch)
370 ssysclock_t delta, ticks_left;
371 sysclock_t prev_tick, tick;
376 static int state = 0;
380 for (n1 = 1; n1 <= 10000000; n1 *= 10)
385 kprintf("DELAY(%d)...", n);
388 * Guard against the timer being uninitialized if we are called
389 * early for console i/o.
391 if (timer0_state == RELEASED)
395 * Read the counter first, so that the rest of the setup overhead is
396 * counted. Then calculate the number of hardware timer ticks
397 * required, rounding up to be sure we delay at least the requested
398 * number of microseconds.
400 prev_tick = sys_cputimer->count();
401 ticks_left = ((u_int)n * (int64_t)sys_cputimer->freq + 999999) /
407 while (ticks_left > 0) {
408 tick = sys_cputimer->count();
412 delta = tick - prev_tick;
417 if (doswitch && ticks_left > 0)
423 kprintf(" %d calls to getit() at %d usec each\n",
424 getit_calls, (n + 5) / getit_calls);
429 * DELAY() never switches
438 CHECKTIMEOUT(TOTALDELAY *tdd)
443 if (tdd->started == 0) {
444 if (timer0_state == RELEASED)
446 tdd->last_clock = sys_cputimer->count();
450 delta = sys_cputimer->count() - tdd->last_clock;
451 us = (u_int64_t)delta * (u_int64_t)1000000 /
452 (u_int64_t)sys_cputimer->freq;
453 tdd->last_clock += (u_int64_t)us * (u_int64_t)sys_cputimer->freq /
456 return (tdd->us < 0);
460 * DRIVERSLEEP() does not switch if called with a spinlock held or
461 * from a hard interrupt.
464 DRIVERSLEEP(int usec)
466 globaldata_t gd = mycpu;
468 if (gd->gd_intr_nesting_level || gd->gd_spinlocks) {
476 sysbeepstop(void *chan)
478 outb(IO_PPI, inb(IO_PPI)&0xFC); /* disable counter2 output to speaker */
484 sysbeep(int pitch, int period)
486 if (acquire_timer2(TIMER_SQWAVE|TIMER_16BIT))
488 if (sysbeep_enable == 0)
491 * Nobody else is using timer2, we do not need the clock lock
493 outb(TIMER_CNTR2, pitch);
494 outb(TIMER_CNTR2, (pitch>>8));
496 /* enable counter2 output to speaker */
497 outb(IO_PPI, inb(IO_PPI) | 3);
499 callout_reset(&sysbeepstop_ch, period, sysbeepstop, NULL);
505 * RTC support routines
516 val = inb(IO_RTC + 1);
523 writertc(u_char reg, u_char val)
529 outb(IO_RTC + 1, val);
530 inb(0x84); /* XXX work around wrong order in rtcin() */
537 return(bcd2bin(rtcin(port)));
541 calibrate_clocks(void)
545 sysclock_t count, prev_count;
546 int sec, start_sec, timeout;
549 kprintf("Calibrating clock(s) ...\n");
550 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
554 /* Read the mc146818A seconds counter. */
556 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP)) {
557 sec = rtcin(RTC_SEC);
564 /* Wait for the mC146818A seconds counter to change. */
567 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP)) {
568 sec = rtcin(RTC_SEC);
569 if (sec != start_sec)
576 /* Start keeping track of the i8254 counter. */
577 prev_count = sys_cputimer->count();
583 old_tsc = 0; /* shut up gcc */
586 * Wait for the mc146818A seconds counter to change. Read the i8254
587 * counter for each iteration since this is convenient and only
588 * costs a few usec of inaccuracy. The timing of the final reads
589 * of the counters almost matches the timing of the initial reads,
590 * so the main cause of inaccuracy is the varying latency from
591 * inside getit() or rtcin(RTC_STATUSA) to the beginning of the
592 * rtcin(RTC_SEC) that returns a changed seconds count. The
593 * maximum inaccuracy from this cause is < 10 usec on 486's.
597 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP))
598 sec = rtcin(RTC_SEC);
599 count = sys_cputimer->count();
600 tot_count += (int)(count - prev_count);
602 if (sec != start_sec)
609 * Read the cpu cycle counter. The timing considerations are
610 * similar to those for the i8254 clock.
613 tsc_frequency = rdtsc() - old_tsc;
617 kprintf("TSC%s clock: %llu Hz, ",
618 tsc_invariant ? " invariant" : "",
619 (long long)tsc_frequency);
621 kprintf("i8254 clock: %u Hz\n", tot_count);
625 kprintf("failed, using default i8254 clock of %u Hz\n",
626 i8254_cputimer.freq);
627 return (i8254_cputimer.freq);
633 timer0_state = ACQUIRED;
638 * Timer0 is our fine-grained variable clock interrupt
640 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
641 outb(TIMER_CNTR0, 2); /* lsb */
642 outb(TIMER_CNTR0, 0); /* msb */
646 cputimer_intr_register(&i8254_cputimer_intr);
647 cputimer_intr_select(&i8254_cputimer_intr, 0);
651 * Timer1 or timer2 is our free-running clock, but only if another
652 * has not been selected.
654 cputimer_register(&i8254_cputimer);
655 cputimer_select(&i8254_cputimer, 0);
659 i8254_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
664 * Should we use timer 1 or timer 2 ?
667 TUNABLE_INT_FETCH("hw.i8254.walltimer", &which);
668 if (which != 1 && which != 2)
673 timer->name = "i8254_timer1";
674 timer->type = CPUTIMER_8254_SEL1;
675 i8254_walltimer_sel = TIMER_SEL1;
676 i8254_walltimer_cntr = TIMER_CNTR1;
677 timer1_state = ACQUIRED;
680 timer->name = "i8254_timer2";
681 timer->type = CPUTIMER_8254_SEL2;
682 i8254_walltimer_sel = TIMER_SEL2;
683 i8254_walltimer_cntr = TIMER_CNTR2;
684 timer2_state = ACQUIRED;
688 timer->base = (oldclock + 0xFFFF) & ~0xFFFF;
691 outb(TIMER_MODE, i8254_walltimer_sel | TIMER_RATEGEN | TIMER_16BIT);
692 outb(i8254_walltimer_cntr, 0); /* lsb */
693 outb(i8254_walltimer_cntr, 0); /* msb */
694 outb(IO_PPI, inb(IO_PPI) | 1); /* bit 0: enable gate, bit 1: spkr */
699 i8254_cputimer_destruct(struct cputimer *timer)
701 switch(timer->type) {
702 case CPUTIMER_8254_SEL1:
703 timer1_state = RELEASED;
705 case CPUTIMER_8254_SEL2:
706 timer2_state = RELEASED;
717 /* Restore all of the RTC's "status" (actually, control) registers. */
718 writertc(RTC_STATUSB, RTCSB_24HR);
719 writertc(RTC_STATUSA, rtc_statusa);
720 writertc(RTC_STATUSB, rtc_statusb);
724 * Restore all the timers.
726 * This function is called to resynchronize our core timekeeping after a
727 * long halt, e.g. from apm_default_resume() and friends. It is also
728 * called if after a BIOS call we have detected munging of the 8254.
729 * It is necessary because cputimer_count() counter's delta may have grown
730 * too large for nanouptime() and friends to handle, or (in the case of 8254
731 * munging) might cause the SYSTIMER code to prematurely trigger.
737 i8254_restore(); /* restore timer_freq and hz */
738 rtc_restore(); /* reenable RTC interrupts */
743 * Initialize 8254 timer 0 early so that it can be used in DELAY().
751 * Can we use the TSC?
753 if (cpu_feature & CPUID_TSC) {
755 if ((cpu_vendor_id == CPU_VENDOR_INTEL ||
756 cpu_vendor_id == CPU_VENDOR_AMD) &&
757 cpu_exthigh >= 0x80000007) {
760 do_cpuid(0x80000007, regs);
769 * Initial RTC state, don't do anything unexpected
771 writertc(RTC_STATUSA, rtc_statusa);
772 writertc(RTC_STATUSB, RTCSB_24HR);
775 * Set the 8254 timer0 in TIMER_SWSTROBE mode and cause it to
776 * generate an interrupt, which we will ignore for now.
778 * Set the 8254 timer1 in TIMER_RATEGEN mode and load 0x0000
779 * (so it counts a full 2^16 and repeats). We will use this timer
783 freq = calibrate_clocks();
784 #ifdef CLK_CALIBRATION_LOOP
787 "Press a key on the console to abort clock calibration\n");
788 while (cncheckc() == -1)
794 * Use the calibrated i8254 frequency if it seems reasonable.
795 * Otherwise use the default, and don't use the calibrated i586
798 delta = freq > i8254_cputimer.freq ?
799 freq - i8254_cputimer.freq : i8254_cputimer.freq - freq;
800 if (delta < i8254_cputimer.freq / 100) {
801 #ifndef CLK_USE_I8254_CALIBRATION
804 "CLK_USE_I8254_CALIBRATION not specified - using default frequency\n");
805 freq = i8254_cputimer.freq;
809 * Interrupt timer's freq must be adjusted
810 * before we change the cuptimer's frequency.
812 i8254_cputimer_intr.freq = freq;
813 cputimer_set_frequency(&i8254_cputimer, freq);
817 "%d Hz differs from default of %d Hz by more than 1%%\n",
818 freq, i8254_cputimer.freq);
822 #ifndef CLK_USE_TSC_CALIBRATION
823 if (tsc_frequency != 0) {
826 "CLK_USE_TSC_CALIBRATION not specified - using old calibration method\n");
830 if (tsc_present && tsc_frequency == 0) {
832 * Calibration of the i586 clock relative to the mc146818A
833 * clock failed. Do a less accurate calibration relative
834 * to the i8254 clock.
836 u_int64_t old_tsc = rdtsc();
839 tsc_frequency = rdtsc() - old_tsc;
840 #ifdef CLK_USE_TSC_CALIBRATION
842 kprintf("TSC clock: %llu Hz (Method B)\n",
848 EVENTHANDLER_REGISTER(shutdown_post_sync, resettodr_on_shutdown, NULL, SHUTDOWN_PRI_LAST);
852 * Sync the time of day back to the RTC on shutdown, but only if
853 * we have already loaded it and have not crashed.
856 resettodr_on_shutdown(void *arg __unused)
858 if (rtc_loaded && panicstr == NULL) {
864 * Initialize the time of day register, based on the time base which is, e.g.
868 inittodr(time_t base)
870 unsigned long sec, days;
881 /* Look if we have a RTC present and the time is valid */
882 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
885 /* wait for time update to complete */
886 /* If RTCSA_TUP is zero, we have at least 244us before next update */
888 while (rtcin(RTC_STATUSA) & RTCSA_TUP) {
894 #ifdef USE_RTC_CENTURY
895 year = readrtc(RTC_YEAR) + readrtc(RTC_CENTURY) * 100;
897 year = readrtc(RTC_YEAR) + 1900;
905 month = readrtc(RTC_MONTH);
906 for (m = 1; m < month; m++)
907 days += daysinmonth[m-1];
908 if ((month > 2) && LEAPYEAR(year))
910 days += readrtc(RTC_DAY) - 1;
911 for (y = 1970; y < year; y++)
912 days += DAYSPERYEAR + LEAPYEAR(y);
913 sec = ((( days * 24 +
914 readrtc(RTC_HRS)) * 60 +
915 readrtc(RTC_MIN)) * 60 +
917 /* sec now contains the number of seconds, since Jan 1 1970,
918 in the local time zone */
920 sec += tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
922 y = (int)(time_second - sec);
923 if (y <= -2 || y >= 2) {
924 /* badly off, adjust it */
934 kprintf("Invalid time in real time clock.\n");
935 kprintf("Check and reset the date immediately!\n");
939 * Write system time back to RTC
956 /* Disable RTC updates and interrupts. */
957 writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
959 /* Calculate local time to put in RTC */
961 tm -= tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
963 writertc(RTC_SEC, bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */
964 writertc(RTC_MIN, bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */
965 writertc(RTC_HRS, bin2bcd(tm%24)); tm /= 24; /* Write back Hours */
967 /* We have now the days since 01-01-1970 in tm */
968 writertc(RTC_WDAY, (tm+4)%7); /* Write back Weekday */
969 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y);
971 y++, m = DAYSPERYEAR + LEAPYEAR(y))
974 /* Now we have the years in y and the day-of-the-year in tm */
975 writertc(RTC_YEAR, bin2bcd(y%100)); /* Write back Year */
976 #ifdef USE_RTC_CENTURY
977 writertc(RTC_CENTURY, bin2bcd(y/100)); /* ... and Century */
983 if (m == 1 && LEAPYEAR(y))
990 writertc(RTC_MONTH, bin2bcd(m + 1)); /* Write back Month */
991 writertc(RTC_DAY, bin2bcd(tm + 1)); /* Write back Month Day */
993 /* Reenable RTC updates and interrupts. */
994 writertc(RTC_STATUSB, rtc_statusb);
999 i8254_ioapic_trial(int irq, struct cputimer_intr *cti)
1005 * Following code assumes the 8254 is the cpu timer,
1006 * so make sure it is.
1008 KKASSERT(sys_cputimer == &i8254_cputimer);
1009 KKASSERT(cti == &i8254_cputimer_intr);
1011 lastcnt = get_interrupt_counter(irq, mycpuid);
1014 * Force an 8254 Timer0 interrupt and wait 1/100s for
1015 * it to happen, then see if we got it.
1017 kprintf("IOAPIC: testing 8254 interrupt delivery\n");
1019 i8254_intr_reload(cti, 2);
1020 base = sys_cputimer->count();
1021 while (sys_cputimer->count() - base < sys_cputimer->freq / 100)
1024 if (get_interrupt_counter(irq, mycpuid) - lastcnt == 0)
1030 * Start both clocks running. DragonFly note: the stat clock is no longer
1031 * used. Instead, 8254 based systimers are used for all major clock
1035 i8254_intr_initclock(struct cputimer_intr *cti, boolean_t selected)
1037 void *clkdesc = NULL;
1038 int irq = 0, mixed_mode = 0, error;
1040 KKASSERT(mycpuid == 0);
1041 callout_init(&sysbeepstop_ch);
1043 if (!selected && i8254_intr_disable)
1047 * The stat interrupt mask is different without the
1048 * statistics clock. Also, don't set the interrupt
1049 * flag which would normally cause the RTC to generate
1052 rtc_statusb = RTCSB_24HR;
1054 /* Finish initializing 8253 timer 0. */
1055 if (ioapic_enable) {
1056 irq = machintr_legacy_intr_find(0, INTR_TRIGGER_EDGE,
1057 INTR_POLARITY_HIGH);
1060 error = ioapic_conf_legacy_extint(0);
1062 irq = machintr_legacy_intr_find(0,
1063 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1070 kprintf("IOAPIC: setup mixed mode for "
1071 "irq 0 failed: %d\n", error);
1074 panic("IOAPIC: setup mixed mode for "
1075 "irq 0 failed: %d\n", error);
1080 clkdesc = register_int(irq, clkintr, NULL, "clk",
1082 INTR_EXCL | INTR_CLOCK |
1083 INTR_NOPOLL | INTR_MPSAFE |
1086 register_int(0, clkintr, NULL, "clk", NULL,
1087 INTR_EXCL | INTR_CLOCK |
1088 INTR_NOPOLL | INTR_MPSAFE |
1092 /* Initialize RTC. */
1093 writertc(RTC_STATUSA, rtc_statusa);
1094 writertc(RTC_STATUSB, RTCSB_24HR);
1096 if (ioapic_enable) {
1097 error = i8254_ioapic_trial(irq, cti);
1101 kprintf("IOAPIC: mixed mode for irq %d "
1102 "trial failed: %d\n",
1106 panic("IOAPIC: mixed mode for irq %d "
1107 "trial failed: %d\n", irq, error);
1110 kprintf("IOAPIC: warning 8254 is not connected "
1111 "to the correct pin, try mixed mode\n");
1112 unregister_int(clkdesc, 0);
1113 goto mixed_mode_setup;
1120 i8254_nointr = 1; /* don't try to register again */
1121 cputimer_intr_deregister(cti);
1125 setstatclockrate(int newhz)
1127 if (newhz == RTC_PROFRATE)
1128 rtc_statusa = RTCSA_DIVIDER | RTCSA_PROF;
1130 rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
1131 writertc(RTC_STATUSA, rtc_statusa);
1136 tsc_get_timecount(struct timecounter *tc)
1142 #ifdef KERN_TIMESTAMP
1143 #define KERN_TIMESTAMP_SIZE 16384
1144 static u_long tsc[KERN_TIMESTAMP_SIZE] ;
1145 SYSCTL_OPAQUE(_debug, OID_AUTO, timestamp, CTLFLAG_RD, tsc,
1146 sizeof(tsc), "LU", "Kernel timestamps");
1152 tsc[i] = (u_int32_t)rdtsc();
1155 if (i >= KERN_TIMESTAMP_SIZE)
1157 tsc[i] = 0; /* mark last entry */
1159 #endif /* KERN_TIMESTAMP */
1166 hw_i8254_timestamp(SYSCTL_HANDLER_ARGS)
1173 if (sys_cputimer == &i8254_cputimer)
1174 count = sys_cputimer->count();
1182 ksnprintf(buf, sizeof(buf), "%08x %016llx", count, (long long)tscval);
1183 return(SYSCTL_OUT(req, buf, strlen(buf) + 1));
1186 static uint64_t tsc_mpsync_target;
1189 tsc_mpsync_test_remote(void *arg __unused)
1194 if (tsc < tsc_mpsync_target)
1199 tsc_mpsync_test(void)
1201 struct globaldata *gd = mycpu;
1202 uint64_t test_end, test_begin;
1205 if (!tsc_invariant) {
1206 /* Not even invariant TSC */
1216 if (cpu_vendor_id != CPU_VENDOR_INTEL) {
1217 /* XXX only Intel works */
1221 kprintf("TSC testing MP synchronization ...\n");
1224 /* Run test for 100ms */
1225 test_begin = rdtsc();
1226 test_end = test_begin + (tsc_frequency / 10);
1228 #define TSC_TEST_TRYMAX 1000000 /* Make sure we could stop */
1230 for (i = 0; i < TSC_TEST_TRYMAX; ++i) {
1231 struct lwkt_cpusync cs;
1234 lwkt_cpusync_init(&cs, gd->gd_other_cpus,
1235 tsc_mpsync_test_remote, NULL);
1236 lwkt_cpusync_interlock(&cs);
1237 tsc_mpsync_target = rdtsc();
1239 lwkt_cpusync_deinterlock(&cs);
1243 kprintf("TSC is not MP synchronized @%u\n", i);
1246 if (tsc_mpsync_target > test_end)
1250 #undef TSC_TEST_TRYMAX
1253 if (tsc_mpsync_target == test_begin) {
1254 kprintf("TSC does not tick?!");
1255 /* XXX disable TSC? */
1261 kprintf("TSC is MP synchronized");
1263 kprintf(", after %u tries", i);
1267 SYSINIT(tsc_mpsync, SI_BOOT2_FINISH_SMP, SI_ORDER_ANY, tsc_mpsync_test, NULL);
1269 #define TSC_CPUTIMER_FREQMAX 128000000 /* 128Mhz */
1271 static int tsc_cputimer_shift;
1274 tsc_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
1277 timer->base = oldclock - tsc_cputimer_count();
1281 tsc_cputimer_count(void)
1286 tsc >>= tsc_cputimer_shift;
1288 return (tsc + tsc_cputimer.base);
1292 tsc_cputimer_register(void)
1300 TUNABLE_INT_FETCH("hw.tsc_cputimer_enable", &enable);
1304 freq = tsc_frequency;
1305 while (freq > TSC_CPUTIMER_FREQMAX) {
1307 ++tsc_cputimer_shift;
1309 kprintf("TSC: cputimer freq %ju, shift %d\n",
1310 (uintmax_t)freq, tsc_cputimer_shift);
1312 tsc_cputimer.freq = freq;
1314 cputimer_register(&tsc_cputimer);
1315 cputimer_select(&tsc_cputimer, 0);
1317 SYSINIT(tsc_cputimer_reg, SI_BOOT2_MACHDEP, SI_ORDER_ANY,
1318 tsc_cputimer_register, NULL);
1320 SYSCTL_NODE(_hw, OID_AUTO, i8254, CTLFLAG_RW, 0, "I8254");
1321 SYSCTL_UINT(_hw_i8254, OID_AUTO, freq, CTLFLAG_RD, &i8254_cputimer.freq, 0,
1323 SYSCTL_PROC(_hw_i8254, OID_AUTO, timestamp, CTLTYPE_STRING|CTLFLAG_RD,
1324 0, 0, hw_i8254_timestamp, "A", "");
1326 SYSCTL_INT(_hw, OID_AUTO, tsc_present, CTLFLAG_RD,
1327 &tsc_present, 0, "TSC Available");
1328 SYSCTL_INT(_hw, OID_AUTO, tsc_invariant, CTLFLAG_RD,
1329 &tsc_invariant, 0, "Invariant TSC");
1330 SYSCTL_INT(_hw, OID_AUTO, tsc_mpsync, CTLFLAG_RD,
1331 &tsc_mpsync, 0, "TSC is synchronized across CPUs");
1332 SYSCTL_QUAD(_hw, OID_AUTO, tsc_frequency, CTLFLAG_RD,
1333 &tsc_frequency, 0, "TSC Frequency");