2 * Copyright (c) 1990 The Regents of the University of California.
3 * Copyright (c) 2008 The DragonFly Project.
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz and Don Ahn.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
34 * $FreeBSD: src/sys/i386/isa/clock.c,v 1.149.2.6 2002/11/02 04:41:50 iwasaki Exp $
38 * Routines to handle clock hardware.
42 * inittodr, settodr and support routines written
43 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>
45 * reintroduced and updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
49 #include "opt_clock.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/eventhandler.h>
56 #include <sys/kernel.h>
58 #include <sys/sysctl.h>
61 #include <sys/systimer.h>
62 #include <sys/globaldata.h>
63 #include <sys/machintr.h>
64 #include <sys/interrupt.h>
66 #include <sys/thread2.h>
68 #include <machine/clock.h>
69 #include <machine/cputypes.h>
70 #include <machine/frame.h>
71 #include <machine/ipl.h>
72 #include <machine/limits.h>
73 #include <machine/md_var.h>
74 #include <machine/psl.h>
75 #include <machine/segments.h>
76 #include <machine/smp.h>
77 #include <machine/specialreg.h>
78 #include <machine/intr_machdep.h>
80 #include <machine_base/apic/ioapic.h>
81 #include <machine_base/apic/ioapic_abi.h>
82 #include <machine_base/icu/icu.h>
83 #include <bus/isa/isa.h>
84 #include <bus/isa/rtc.h>
85 #include <machine_base/isa/timerreg.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 int calibrate_timers_with_rtc = 0;
131 TUNABLE_INT("hw.calibrate_timers_with_rtc", &calibrate_timers_with_rtc);
133 static struct callout sysbeepstop_ch;
135 static sysclock_t i8254_cputimer_count(void);
136 static void i8254_cputimer_construct(struct cputimer *cputimer, sysclock_t last);
137 static void i8254_cputimer_destruct(struct cputimer *cputimer);
139 static struct cputimer i8254_cputimer = {
140 .next = SLIST_ENTRY_INITIALIZER,
142 .pri = CPUTIMER_PRI_8254,
143 .type = 0, /* determined later */
144 .count = i8254_cputimer_count,
145 .fromhz = cputimer_default_fromhz,
146 .fromus = cputimer_default_fromus,
147 .construct = i8254_cputimer_construct,
148 .destruct = i8254_cputimer_destruct,
152 static sysclock_t tsc_cputimer_count_mfence(void);
153 static sysclock_t tsc_cputimer_count_lfence(void);
154 static void tsc_cputimer_construct(struct cputimer *, sysclock_t);
156 static struct cputimer tsc_cputimer = {
157 .next = SLIST_ENTRY_INITIALIZER,
159 .pri = CPUTIMER_PRI_TSC,
160 .type = CPUTIMER_TSC,
161 .count = NULL, /* determined later */
162 .fromhz = cputimer_default_fromhz,
163 .fromus = cputimer_default_fromus,
164 .construct = tsc_cputimer_construct,
165 .destruct = cputimer_default_destruct,
166 .freq = 0 /* determined later */
169 static struct cpucounter tsc_cpucounter = {
170 .freq = 0, /* determined later */
171 .count = NULL, /* determined later */
172 .flags = 0, /* adjusted later */
173 .prio = CPUCOUNTER_PRIO_TSC,
174 .type = CPUCOUNTER_TSC
177 static void i8254_intr_reload(struct cputimer_intr *, sysclock_t);
178 static void i8254_intr_config(struct cputimer_intr *, const struct cputimer *);
179 static void i8254_intr_initclock(struct cputimer_intr *, boolean_t);
181 static struct cputimer_intr i8254_cputimer_intr = {
183 .reload = i8254_intr_reload,
184 .enable = cputimer_intr_default_enable,
185 .config = i8254_intr_config,
186 .restart = cputimer_intr_default_restart,
187 .pmfixup = cputimer_intr_default_pmfixup,
188 .initclock = i8254_intr_initclock,
190 .next = SLIST_ENTRY_INITIALIZER,
192 .type = CPUTIMER_INTR_8254,
193 .prio = CPUTIMER_INTR_PRIO_8254,
194 .caps = CPUTIMER_INTR_CAP_PS,
199 * timer0 clock interrupt. Timer0 is in one-shot mode and has stopped
200 * counting as of this interrupt. We use timer1 in free-running mode (not
201 * generating any interrupts) as our main counter. Each cpu has timeouts
204 * This code is INTR_MPSAFE and may be called without the BGL held.
207 clkintr(void *dummy, void *frame_arg)
209 static sysclock_t sysclock_count; /* NOTE! Must be static */
210 struct globaldata *gd = mycpu;
211 struct globaldata *gscan;
215 * SWSTROBE mode is a one-shot, the timer is no longer running
220 * XXX the dispatcher needs work. right now we call systimer_intr()
221 * directly or via IPI for any cpu with systimers queued, which is
222 * usually *ALL* of them. We need to use the LAPIC timer for this.
224 sysclock_count = sys_cputimer->count();
225 for (n = 0; n < ncpus; ++n) {
226 gscan = globaldata_find(n);
227 if (TAILQ_FIRST(&gscan->gd_systimerq) == NULL)
230 lwkt_send_ipiq3(gscan, (ipifunc3_t)systimer_intr,
233 systimer_intr(&sysclock_count, 0, frame_arg);
243 acquire_timer2(int mode)
245 if (timer2_state != RELEASED)
247 timer2_state = ACQUIRED;
250 * This access to the timer registers is as atomic as possible
251 * because it is a single instruction. We could do better if we
254 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f));
261 if (timer2_state != ACQUIRED)
263 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT);
264 timer2_state = RELEASED;
272 DB_SHOW_COMMAND(rtc, rtc)
274 kprintf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
275 rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
276 rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
277 rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));
282 * Return the current cpu timer count as a 32 bit integer.
286 i8254_cputimer_count(void)
288 static uint16_t cputimer_last;
293 outb(TIMER_MODE, i8254_walltimer_sel | TIMER_LATCH);
294 count = (uint8_t)inb(i8254_walltimer_cntr); /* get countdown */
295 count |= ((uint8_t)inb(i8254_walltimer_cntr) << 8);
296 count = -count; /* -> countup */
297 if (count < cputimer_last) /* rollover */
298 i8254_cputimer.base += 0x00010000;
299 ret = i8254_cputimer.base | count;
300 cputimer_last = count;
306 * This function is called whenever the system timebase changes, allowing
307 * us to calculate what is needed to convert a system timebase tick
308 * into an 8254 tick for the interrupt timer. If we can convert to a
309 * simple shift, multiplication, or division, we do so. Otherwise 64
310 * bit arithmatic is required every time the interrupt timer is reloaded.
313 i8254_intr_config(struct cputimer_intr *cti, const struct cputimer *timer)
319 * Will a simple divide do the trick?
321 div = (timer->freq + (cti->freq / 2)) / cti->freq;
322 freq = cti->freq * div;
324 if (freq >= timer->freq - 1 && freq <= timer->freq + 1)
325 i8254_cputimer_div = div;
327 i8254_cputimer_div = 0;
331 * Reload for the next timeout. It is possible for the reload value
332 * to be 0 or negative, indicating that an immediate timer interrupt
333 * is desired. For now make the minimum 2 ticks.
335 * We may have to convert from the system timebase to the 8254 timebase.
338 i8254_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
342 if (i8254_cputimer_div)
343 reload /= i8254_cputimer_div;
345 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
351 if (timer0_running) {
352 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); /* count-down timer */
353 count = (uint8_t)inb(TIMER_CNTR0); /* lsb */
354 count |= ((uint8_t)inb(TIMER_CNTR0) << 8); /* msb */
355 if (reload < count) {
356 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
357 outb(TIMER_CNTR0, (uint8_t)reload); /* lsb */
358 outb(TIMER_CNTR0, (uint8_t)(reload >> 8)); /* msb */
363 reload = 0; /* full count */
364 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
365 outb(TIMER_CNTR0, (uint8_t)reload); /* lsb */
366 outb(TIMER_CNTR0, (uint8_t)(reload >> 8)); /* msb */
372 * DELAY(usec) - Spin for the specified number of microseconds.
373 * DRIVERSLEEP(usec) - Spin for the specified number of microseconds,
374 * but do a thread switch in the loop
376 * Relies on timer 1 counting down from (cputimer_freq / hz)
377 * Note: timer had better have been programmed before this is first used!
380 DODELAY(int n, int doswitch)
382 ssysclock_t delta, ticks_left;
383 sysclock_t prev_tick, tick;
388 static int state = 0;
392 for (n1 = 1; n1 <= 10000000; n1 *= 10)
397 kprintf("DELAY(%d)...", n);
400 * Guard against the timer being uninitialized if we are called
401 * early for console i/o.
403 if (timer0_state == RELEASED)
407 * Read the counter first, so that the rest of the setup overhead is
408 * counted. Then calculate the number of hardware timer ticks
409 * required, rounding up to be sure we delay at least the requested
410 * number of microseconds.
412 prev_tick = sys_cputimer->count();
413 ticks_left = ((u_int)n * (int64_t)sys_cputimer->freq + 999999) /
419 while (ticks_left > 0) {
420 tick = sys_cputimer->count();
424 delta = tick - prev_tick;
429 if (doswitch && ticks_left > 0)
435 kprintf(" %d calls to getit() at %d usec each\n",
436 getit_calls, (n + 5) / getit_calls);
441 * DELAY() never switches.
450 * Returns non-zero if the specified time period has elapsed. Call
451 * first with last_clock set to 0.
454 CHECKTIMEOUT(TOTALDELAY *tdd)
459 if (tdd->started == 0) {
460 if (timer0_state == RELEASED)
462 tdd->last_clock = sys_cputimer->count();
466 delta = sys_cputimer->count() - tdd->last_clock;
467 us = (u_int64_t)delta * (u_int64_t)1000000 /
468 (u_int64_t)sys_cputimer->freq;
469 tdd->last_clock += (u_int64_t)us * (u_int64_t)sys_cputimer->freq /
472 return (tdd->us < 0);
477 * DRIVERSLEEP() does not switch if called with a spinlock held or
478 * from a hard interrupt.
481 DRIVERSLEEP(int usec)
483 globaldata_t gd = mycpu;
485 if (gd->gd_intr_nesting_level || gd->gd_spinlocks) {
493 sysbeepstop(void *chan)
495 outb(IO_PPI, inb(IO_PPI)&0xFC); /* disable counter2 output to speaker */
501 sysbeep(int pitch, int period)
503 if (acquire_timer2(TIMER_SQWAVE|TIMER_16BIT))
505 if (sysbeep_enable == 0)
508 * Nobody else is using timer2, we do not need the clock lock
510 outb(TIMER_CNTR2, pitch);
511 outb(TIMER_CNTR2, (pitch>>8));
513 /* enable counter2 output to speaker */
514 outb(IO_PPI, inb(IO_PPI) | 3);
516 callout_reset(&sysbeepstop_ch, period, sysbeepstop, NULL);
522 * RTC support routines
533 val = inb(IO_RTC + 1);
540 writertc(u_char reg, u_char val)
546 outb(IO_RTC + 1, val);
547 inb(0x84); /* XXX work around wrong order in rtcin() */
554 return(bcd2bin(rtcin(port)));
558 calibrate_clocks(void)
562 sysclock_t count, prev_count;
563 int sec, start_sec, timeout;
566 kprintf("Calibrating clock(s) ...\n");
567 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
571 /* Read the mc146818A seconds counter. */
573 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP)) {
574 sec = rtcin(RTC_SEC);
581 /* Wait for the mC146818A seconds counter to change. */
584 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP)) {
585 sec = rtcin(RTC_SEC);
586 if (sec != start_sec)
593 /* Start keeping track of the i8254 counter. */
594 prev_count = sys_cputimer->count();
600 old_tsc = 0; /* shut up gcc */
603 * Wait for the mc146818A seconds counter to change. Read the i8254
604 * counter for each iteration since this is convenient and only
605 * costs a few usec of inaccuracy. The timing of the final reads
606 * of the counters almost matches the timing of the initial reads,
607 * so the main cause of inaccuracy is the varying latency from
608 * inside getit() or rtcin(RTC_STATUSA) to the beginning of the
609 * rtcin(RTC_SEC) that returns a changed seconds count. The
610 * maximum inaccuracy from this cause is < 10 usec on 486's.
614 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP))
615 sec = rtcin(RTC_SEC);
616 count = sys_cputimer->count();
617 tot_count += (int)(count - prev_count);
619 if (sec != start_sec)
626 * Read the cpu cycle counter. The timing considerations are
627 * similar to those for the i8254 clock.
630 tsc_frequency = rdtsc() - old_tsc;
632 kprintf("TSC clock: %jd Hz (Method A)\n",
633 (intmax_t)tsc_frequency);
637 kprintf("i8254 clock: %u Hz\n", tot_count);
641 kprintf("failed, using default i8254 clock of %u Hz\n",
642 i8254_cputimer.freq);
643 return (i8254_cputimer.freq);
649 timer0_state = ACQUIRED;
654 * Timer0 is our fine-grained variable clock interrupt
656 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
657 outb(TIMER_CNTR0, 2); /* lsb */
658 outb(TIMER_CNTR0, 0); /* msb */
662 cputimer_intr_register(&i8254_cputimer_intr);
663 cputimer_intr_select(&i8254_cputimer_intr, 0);
667 * Timer1 or timer2 is our free-running clock, but only if another
668 * has not been selected.
670 cputimer_register(&i8254_cputimer);
671 cputimer_select(&i8254_cputimer, 0);
675 i8254_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
680 * Should we use timer 1 or timer 2 ?
683 TUNABLE_INT_FETCH("hw.i8254.walltimer", &which);
684 if (which != 1 && which != 2)
689 timer->name = "i8254_timer1";
690 timer->type = CPUTIMER_8254_SEL1;
691 i8254_walltimer_sel = TIMER_SEL1;
692 i8254_walltimer_cntr = TIMER_CNTR1;
693 timer1_state = ACQUIRED;
696 timer->name = "i8254_timer2";
697 timer->type = CPUTIMER_8254_SEL2;
698 i8254_walltimer_sel = TIMER_SEL2;
699 i8254_walltimer_cntr = TIMER_CNTR2;
700 timer2_state = ACQUIRED;
704 timer->base = (oldclock + 0xFFFF) & ~0xFFFF;
707 outb(TIMER_MODE, i8254_walltimer_sel | TIMER_RATEGEN | TIMER_16BIT);
708 outb(i8254_walltimer_cntr, 0); /* lsb */
709 outb(i8254_walltimer_cntr, 0); /* msb */
710 outb(IO_PPI, inb(IO_PPI) | 1); /* bit 0: enable gate, bit 1: spkr */
715 i8254_cputimer_destruct(struct cputimer *timer)
717 switch(timer->type) {
718 case CPUTIMER_8254_SEL1:
719 timer1_state = RELEASED;
721 case CPUTIMER_8254_SEL2:
722 timer2_state = RELEASED;
733 /* Restore all of the RTC's "status" (actually, control) registers. */
734 writertc(RTC_STATUSB, RTCSB_24HR);
735 writertc(RTC_STATUSA, rtc_statusa);
736 writertc(RTC_STATUSB, rtc_statusb);
740 * Restore all the timers.
742 * This function is called to resynchronize our core timekeeping after a
743 * long halt, e.g. from apm_default_resume() and friends. It is also
744 * called if after a BIOS call we have detected munging of the 8254.
745 * It is necessary because cputimer_count() counter's delta may have grown
746 * too large for nanouptime() and friends to handle, or (in the case of 8254
747 * munging) might cause the SYSTIMER code to prematurely trigger.
753 i8254_restore(); /* restore timer_freq and hz */
754 rtc_restore(); /* reenable RTC interrupts */
759 * Initialize 8254 timer 0 early so that it can be used in DELAY().
767 * Can we use the TSC?
769 * NOTE: If running under qemu, probably a good idea to force the
770 * TSC because we are not likely to detect it as being
771 * invariant or mpsyncd if you don't. This will greatly
772 * reduce SMP contention.
774 if (cpu_feature & CPUID_TSC) {
776 TUNABLE_INT_FETCH("hw.tsc_cputimer_force", &tsc_invariant);
778 if ((cpu_vendor_id == CPU_VENDOR_INTEL ||
779 cpu_vendor_id == CPU_VENDOR_AMD) &&
780 cpu_exthigh >= 0x80000007) {
783 do_cpuid(0x80000007, regs);
792 * Initial RTC state, don't do anything unexpected
794 writertc(RTC_STATUSA, rtc_statusa);
795 writertc(RTC_STATUSB, RTCSB_24HR);
798 * Set the 8254 timer0 in TIMER_SWSTROBE mode and cause it to
799 * generate an interrupt, which we will ignore for now.
801 * Set the 8254 timer1 in TIMER_RATEGEN mode and load 0x0000
802 * (so it counts a full 2^16 and repeats). We will use this timer
808 * When booting without verbose messages, it's pointless to run the
809 * calibrate_clocks() calibration code, when we don't use the
810 * results in any way. With bootverbose, we are at least printing
811 * this information to the kernel log.
813 if (calibrate_timers_with_rtc == 0 && !bootverbose)
816 freq = calibrate_clocks();
817 #ifdef CLK_CALIBRATION_LOOP
822 kprintf("Press a key on the console to "
823 "abort clock calibration\n");
824 while ((c = cncheckc()) == -1 || c == NOKEY)
831 * Use the calibrated i8254 frequency if it seems reasonable.
832 * Otherwise use the default, and don't use the calibrated i586
835 delta = freq > i8254_cputimer.freq ?
836 freq - i8254_cputimer.freq : i8254_cputimer.freq - freq;
837 if (delta < i8254_cputimer.freq / 100) {
838 if (calibrate_timers_with_rtc == 0) {
840 "hw.calibrate_timers_with_rtc not set - using default i8254 frequency\n");
841 freq = i8254_cputimer.freq;
845 * Interrupt timer's freq must be adjusted
846 * before we change the cuptimer's frequency.
848 i8254_cputimer_intr.freq = freq;
849 cputimer_set_frequency(&i8254_cputimer, freq);
853 "%d Hz differs from default of %d Hz by more than 1%%\n",
854 freq, i8254_cputimer.freq);
858 if (tsc_frequency != 0 && calibrate_timers_with_rtc == 0) {
860 "hw.calibrate_timers_with_rtc not set - using old calibration method\n");
865 if (tsc_present && tsc_frequency == 0) {
867 * Calibration of the i586 clock relative to the mc146818A
868 * clock failed. Do a less accurate calibration relative
869 * to the i8254 clock.
871 u_int64_t old_tsc = rdtsc();
874 tsc_frequency = rdtsc() - old_tsc;
875 if (bootverbose && calibrate_timers_with_rtc) {
876 kprintf("TSC clock: %jd Hz (Method B)\n",
877 (intmax_t)tsc_frequency);
882 kprintf("TSC%s clock: %jd Hz\n",
883 tsc_invariant ? " invariant" : "",
884 (intmax_t)tsc_frequency);
887 EVENTHANDLER_REGISTER(shutdown_post_sync, resettodr_on_shutdown, NULL, SHUTDOWN_PRI_LAST);
891 * Sync the time of day back to the RTC on shutdown, but only if
892 * we have already loaded it and have not crashed.
895 resettodr_on_shutdown(void *arg __unused)
897 if (rtc_loaded && panicstr == NULL) {
903 * Initialize the time of day register, based on the time base which is, e.g.
907 inittodr(time_t base)
909 unsigned long sec, days;
920 /* Look if we have a RTC present and the time is valid */
921 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
924 /* wait for time update to complete */
925 /* If RTCSA_TUP is zero, we have at least 244us before next update */
927 while (rtcin(RTC_STATUSA) & RTCSA_TUP) {
933 #ifdef USE_RTC_CENTURY
934 year = readrtc(RTC_YEAR) + readrtc(RTC_CENTURY) * 100;
936 year = readrtc(RTC_YEAR) + 1900;
944 month = readrtc(RTC_MONTH);
945 for (m = 1; m < month; m++)
946 days += daysinmonth[m-1];
947 if ((month > 2) && LEAPYEAR(year))
949 days += readrtc(RTC_DAY) - 1;
950 for (y = 1970; y < year; y++)
951 days += DAYSPERYEAR + LEAPYEAR(y);
952 sec = ((( days * 24 +
953 readrtc(RTC_HRS)) * 60 +
954 readrtc(RTC_MIN)) * 60 +
956 /* sec now contains the number of seconds, since Jan 1 1970,
957 in the local time zone */
959 sec += tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
961 y = (int)(time_second - sec);
962 if (y <= -2 || y >= 2) {
963 /* badly off, adjust it */
973 kprintf("Invalid time in real time clock.\n");
974 kprintf("Check and reset the date immediately!\n");
978 * Write system time back to RTC
995 /* Disable RTC updates and interrupts. */
996 writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
998 /* Calculate local time to put in RTC */
1000 tm -= tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
1002 writertc(RTC_SEC, bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */
1003 writertc(RTC_MIN, bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */
1004 writertc(RTC_HRS, bin2bcd(tm%24)); tm /= 24; /* Write back Hours */
1006 /* We have now the days since 01-01-1970 in tm */
1007 writertc(RTC_WDAY, (tm+4)%7); /* Write back Weekday */
1008 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y);
1010 y++, m = DAYSPERYEAR + LEAPYEAR(y))
1013 /* Now we have the years in y and the day-of-the-year in tm */
1014 writertc(RTC_YEAR, bin2bcd(y%100)); /* Write back Year */
1015 #ifdef USE_RTC_CENTURY
1016 writertc(RTC_CENTURY, bin2bcd(y/100)); /* ... and Century */
1018 for (m = 0; ; m++) {
1021 ml = daysinmonth[m];
1022 if (m == 1 && LEAPYEAR(y))
1029 writertc(RTC_MONTH, bin2bcd(m + 1)); /* Write back Month */
1030 writertc(RTC_DAY, bin2bcd(tm + 1)); /* Write back Month Day */
1032 /* Reenable RTC updates and interrupts. */
1033 writertc(RTC_STATUSB, rtc_statusb);
1038 i8254_ioapic_trial(int irq, struct cputimer_intr *cti)
1044 * Following code assumes the 8254 is the cpu timer,
1045 * so make sure it is.
1047 KKASSERT(sys_cputimer == &i8254_cputimer);
1048 KKASSERT(cti == &i8254_cputimer_intr);
1050 lastcnt = get_interrupt_counter(irq, mycpuid);
1053 * Force an 8254 Timer0 interrupt and wait 1/100s for
1054 * it to happen, then see if we got it.
1056 kprintf("IOAPIC: testing 8254 interrupt delivery\n");
1058 i8254_intr_reload(cti, 2);
1059 base = sys_cputimer->count();
1060 while (sys_cputimer->count() - base < sys_cputimer->freq / 100)
1063 if (get_interrupt_counter(irq, mycpuid) - lastcnt == 0)
1069 * Start both clocks running. DragonFly note: the stat clock is no longer
1070 * used. Instead, 8254 based systimers are used for all major clock
1074 i8254_intr_initclock(struct cputimer_intr *cti, boolean_t selected)
1076 void *clkdesc = NULL;
1077 int irq = 0, mixed_mode = 0, error;
1079 KKASSERT(mycpuid == 0);
1080 callout_init_mp(&sysbeepstop_ch);
1082 if (!selected && i8254_intr_disable)
1086 * The stat interrupt mask is different without the
1087 * statistics clock. Also, don't set the interrupt
1088 * flag which would normally cause the RTC to generate
1091 rtc_statusb = RTCSB_24HR;
1093 /* Finish initializing 8254 timer 0. */
1094 if (ioapic_enable) {
1095 irq = machintr_legacy_intr_find(0, INTR_TRIGGER_EDGE,
1096 INTR_POLARITY_HIGH);
1099 error = ioapic_conf_legacy_extint(0);
1101 irq = machintr_legacy_intr_find(0,
1102 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1109 kprintf("IOAPIC: setup mixed mode for "
1110 "irq 0 failed: %d\n", error);
1113 panic("IOAPIC: setup mixed mode for "
1114 "irq 0 failed: %d\n", error);
1119 clkdesc = register_int(irq, clkintr, NULL, "clk",
1121 INTR_EXCL | INTR_CLOCK |
1122 INTR_NOPOLL | INTR_MPSAFE |
1125 register_int(0, clkintr, NULL, "clk", NULL,
1126 INTR_EXCL | INTR_CLOCK |
1127 INTR_NOPOLL | INTR_MPSAFE |
1131 /* Initialize RTC. */
1132 writertc(RTC_STATUSA, rtc_statusa);
1133 writertc(RTC_STATUSB, RTCSB_24HR);
1135 if (ioapic_enable) {
1136 error = i8254_ioapic_trial(irq, cti);
1140 kprintf("IOAPIC: mixed mode for irq %d "
1141 "trial failed: %d\n",
1145 panic("IOAPIC: mixed mode for irq %d "
1146 "trial failed: %d\n", irq, error);
1149 kprintf("IOAPIC: warning 8254 is not connected "
1150 "to the correct pin, try mixed mode\n");
1151 unregister_int(clkdesc, 0);
1152 goto mixed_mode_setup;
1159 i8254_nointr = 1; /* don't try to register again */
1160 cputimer_intr_deregister(cti);
1164 setstatclockrate(int newhz)
1166 if (newhz == RTC_PROFRATE)
1167 rtc_statusa = RTCSA_DIVIDER | RTCSA_PROF;
1169 rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
1170 writertc(RTC_STATUSA, rtc_statusa);
1175 tsc_get_timecount(struct timecounter *tc)
1181 #ifdef KERN_TIMESTAMP
1182 #define KERN_TIMESTAMP_SIZE 16384
1183 static u_long tsc[KERN_TIMESTAMP_SIZE] ;
1184 SYSCTL_OPAQUE(_debug, OID_AUTO, timestamp, CTLFLAG_RD, tsc,
1185 sizeof(tsc), "LU", "Kernel timestamps");
1191 tsc[i] = (u_int32_t)rdtsc();
1194 if (i >= KERN_TIMESTAMP_SIZE)
1196 tsc[i] = 0; /* mark last entry */
1198 #endif /* KERN_TIMESTAMP */
1205 hw_i8254_timestamp(SYSCTL_HANDLER_ARGS)
1212 if (sys_cputimer == &i8254_cputimer)
1213 count = sys_cputimer->count();
1221 ksnprintf(buf, sizeof(buf), "%08x %016llx", count, (long long)tscval);
1222 return(SYSCTL_OUT(req, buf, strlen(buf) + 1));
1225 struct tsc_mpsync_arg {
1226 volatile uint64_t tsc_target;
1227 volatile int tsc_mpsync;
1230 struct tsc_mpsync_thr {
1231 volatile int tsc_done_cnt;
1232 volatile int tsc_mpsync_cnt;
1236 tsc_mpsync_test_remote(void *xarg)
1238 struct tsc_mpsync_arg *arg = xarg;
1241 tsc = rdtsc_ordered();
1242 if (tsc < arg->tsc_target)
1243 arg->tsc_mpsync = 0;
1247 tsc_mpsync_test_loop(struct tsc_mpsync_arg *arg)
1249 struct globaldata *gd = mycpu;
1250 uint64_t test_end, test_begin;
1254 kprintf("cpu%d: TSC testing MP synchronization ...\n",
1258 test_begin = rdtsc_ordered();
1259 /* Run test for 100ms */
1260 test_end = test_begin + (tsc_frequency / 10);
1262 arg->tsc_mpsync = 1;
1263 arg->tsc_target = test_begin;
1265 #define TSC_TEST_TRYMAX 1000000 /* Make sure we could stop */
1266 #define TSC_TEST_TRYMIN 50000
1268 for (i = 0; i < TSC_TEST_TRYMAX; ++i) {
1269 struct lwkt_cpusync cs;
1272 lwkt_cpusync_init(&cs, gd->gd_other_cpus,
1273 tsc_mpsync_test_remote, arg);
1274 lwkt_cpusync_interlock(&cs);
1275 arg->tsc_target = rdtsc_ordered();
1277 lwkt_cpusync_deinterlock(&cs);
1280 if (!arg->tsc_mpsync) {
1281 kprintf("cpu%d: TSC is not MP synchronized @%u\n",
1285 if (arg->tsc_target > test_end && i >= TSC_TEST_TRYMIN)
1289 #undef TSC_TEST_TRYMIN
1290 #undef TSC_TEST_TRYMAX
1292 if (arg->tsc_target == test_begin) {
1293 kprintf("cpu%d: TSC does not tick?!\n", gd->gd_cpuid);
1294 /* XXX disable TSC? */
1296 arg->tsc_mpsync = 0;
1300 if (arg->tsc_mpsync && bootverbose) {
1301 kprintf("cpu%d: TSC is MP synchronized after %u tries\n",
1307 tsc_mpsync_ap_thread(void *xthr)
1309 struct tsc_mpsync_thr *thr = xthr;
1310 struct tsc_mpsync_arg arg;
1312 tsc_mpsync_test_loop(&arg);
1313 if (arg.tsc_mpsync) {
1314 atomic_add_int(&thr->tsc_mpsync_cnt, 1);
1317 atomic_add_int(&thr->tsc_done_cnt, 1);
1323 tsc_mpsync_test(void)
1325 struct tsc_mpsync_arg arg;
1327 if (!tsc_invariant) {
1328 /* Not even invariant TSC */
1339 * Forcing can be used w/qemu to reduce contention
1341 TUNABLE_INT_FETCH("hw.tsc_cputimer_force", &tsc_mpsync);
1343 if (tsc_mpsync == 0) {
1344 switch(cpu_vendor_id) {
1345 case CPU_VENDOR_INTEL:
1347 * Intel probably works
1350 case CPU_VENDOR_AMD:
1352 * AMD < Ryzen probably doesn't work
1354 if (CPUID_TO_FAMILY(cpu_id) < 0x17)
1358 /* probably won't work */
1364 * Test even if forced above. If forced, we will use the TSC
1365 * even if the test fails.
1367 kprintf("TSC testing MP synchronization ...\n");
1369 tsc_mpsync_test_loop(&arg);
1370 if (arg.tsc_mpsync) {
1371 struct tsc_mpsync_thr thr;
1375 * Test TSC MP synchronization on APs.
1378 thr.tsc_done_cnt = 1;
1379 thr.tsc_mpsync_cnt = 1;
1381 for (cpu = 0; cpu < ncpus; ++cpu) {
1385 lwkt_create(tsc_mpsync_ap_thread, &thr, NULL,
1386 NULL, 0, cpu, "tsc mpsync %d", cpu);
1389 while (thr.tsc_done_cnt != ncpus) {
1393 if (thr.tsc_mpsync_cnt == ncpus)
1398 kprintf("TSC is MP synchronized\n");
1400 kprintf("TSC is not MP synchronized\n");
1402 SYSINIT(tsc_mpsync, SI_BOOT2_FINISH_SMP, SI_ORDER_ANY, tsc_mpsync_test, NULL);
1404 #define TSC_CPUTIMER_FREQMAX 128000000 /* 128Mhz */
1406 static int tsc_cputimer_shift;
1409 tsc_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
1412 timer->base = oldclock - timer->count();
1415 static __inline sysclock_t
1416 tsc_cputimer_count(void)
1421 tsc >>= tsc_cputimer_shift;
1423 return (tsc + tsc_cputimer.base);
1427 tsc_cputimer_count_lfence(void)
1430 return tsc_cputimer_count();
1434 tsc_cputimer_count_mfence(void)
1437 return tsc_cputimer_count();
1441 tsc_cpucounter_count_lfence(void)
1449 tsc_cpucounter_count_mfence(void)
1457 tsc_cputimer_register(void)
1463 if (tsc_invariant) {
1464 /* Per-cpu cpucounter still works. */
1470 TUNABLE_INT_FETCH("hw.tsc_cputimer_enable", &enable);
1474 freq = tsc_frequency;
1475 while (freq > TSC_CPUTIMER_FREQMAX) {
1477 ++tsc_cputimer_shift;
1479 kprintf("TSC: cputimer freq %ju, shift %d\n",
1480 (uintmax_t)freq, tsc_cputimer_shift);
1482 tsc_cputimer.freq = freq;
1484 if (cpu_vendor_id == CPU_VENDOR_INTEL)
1485 tsc_cputimer.count = tsc_cputimer_count_lfence;
1487 tsc_cputimer.count = tsc_cputimer_count_mfence; /* safe bet */
1489 cputimer_register(&tsc_cputimer);
1490 cputimer_select(&tsc_cputimer, 0);
1492 tsc_cpucounter.flags |= CPUCOUNTER_FLAG_MPSYNC;
1494 tsc_cpucounter.freq = tsc_frequency;
1495 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
1496 tsc_cpucounter.count =
1497 tsc_cpucounter_count_lfence;
1499 tsc_cpucounter.count =
1500 tsc_cpucounter_count_mfence; /* safe bet */
1502 cpucounter_register(&tsc_cpucounter);
1504 SYSINIT(tsc_cputimer_reg, SI_BOOT2_POST_SMP, SI_ORDER_FIRST,
1505 tsc_cputimer_register, NULL);
1507 SYSCTL_NODE(_hw, OID_AUTO, i8254, CTLFLAG_RW, 0, "I8254");
1508 SYSCTL_UINT(_hw_i8254, OID_AUTO, freq, CTLFLAG_RD, &i8254_cputimer.freq, 0,
1510 SYSCTL_PROC(_hw_i8254, OID_AUTO, timestamp, CTLTYPE_STRING|CTLFLAG_RD,
1511 0, 0, hw_i8254_timestamp, "A", "");
1513 SYSCTL_INT(_hw, OID_AUTO, tsc_present, CTLFLAG_RD,
1514 &tsc_present, 0, "TSC Available");
1515 SYSCTL_INT(_hw, OID_AUTO, tsc_invariant, CTLFLAG_RD,
1516 &tsc_invariant, 0, "Invariant TSC");
1517 SYSCTL_INT(_hw, OID_AUTO, tsc_mpsync, CTLFLAG_RD,
1518 &tsc_mpsync, 0, "TSC is synchronized across CPUs");
1519 SYSCTL_QUAD(_hw, OID_AUTO, tsc_frequency, CTLFLAG_RD,
1520 &tsc_frequency, 0, "TSC Frequency");