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 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 .next
= SLIST_ENTRY_INITIALIZER
,
139 .pri
= CPUTIMER_PRI_8254
,
140 .type
= 0, /* determined later */
141 .count
= i8254_cputimer_count
,
142 .fromhz
= cputimer_default_fromhz
,
143 .fromus
= cputimer_default_fromus
,
144 .construct
= i8254_cputimer_construct
,
145 .destruct
= i8254_cputimer_destruct
,
149 static sysclock_t
tsc_cputimer_count_mfence(void);
150 static sysclock_t
tsc_cputimer_count_lfence(void);
151 static void tsc_cputimer_construct(struct cputimer
*, sysclock_t
);
153 static struct cputimer tsc_cputimer
= {
154 .next
= SLIST_ENTRY_INITIALIZER
,
156 .pri
= CPUTIMER_PRI_TSC
,
157 .type
= CPUTIMER_TSC
,
158 .count
= NULL
, /* determined later */
159 .fromhz
= cputimer_default_fromhz
,
160 .fromus
= cputimer_default_fromus
,
161 .construct
= tsc_cputimer_construct
,
162 .destruct
= cputimer_default_destruct
,
163 .freq
= 0 /* determined later */
166 static struct cpucounter tsc_cpucounter
= {
167 .freq
= 0, /* determined later */
168 .count
= NULL
, /* determined later */
169 .flags
= 0, /* adjusted later */
170 .prio
= CPUCOUNTER_PRIO_TSC
,
171 .type
= CPUCOUNTER_TSC
174 static void i8254_intr_reload(struct cputimer_intr
*, sysclock_t
);
175 static void i8254_intr_config(struct cputimer_intr
*, const struct cputimer
*);
176 static void i8254_intr_initclock(struct cputimer_intr
*, boolean_t
);
178 static struct cputimer_intr i8254_cputimer_intr
= {
180 .reload
= i8254_intr_reload
,
181 .enable
= cputimer_intr_default_enable
,
182 .config
= i8254_intr_config
,
183 .restart
= cputimer_intr_default_restart
,
184 .pmfixup
= cputimer_intr_default_pmfixup
,
185 .initclock
= i8254_intr_initclock
,
187 .next
= SLIST_ENTRY_INITIALIZER
,
189 .type
= CPUTIMER_INTR_8254
,
190 .prio
= CPUTIMER_INTR_PRIO_8254
,
191 .caps
= CPUTIMER_INTR_CAP_PS
,
196 * timer0 clock interrupt. Timer0 is in one-shot mode and has stopped
197 * counting as of this interrupt. We use timer1 in free-running mode (not
198 * generating any interrupts) as our main counter. Each cpu has timeouts
201 * This code is INTR_MPSAFE and may be called without the BGL held.
204 clkintr(void *dummy
, void *frame_arg
)
206 static sysclock_t sysclock_count
; /* NOTE! Must be static */
207 struct globaldata
*gd
= mycpu
;
208 struct globaldata
*gscan
;
212 * SWSTROBE mode is a one-shot, the timer is no longer running
217 * XXX the dispatcher needs work. right now we call systimer_intr()
218 * directly or via IPI for any cpu with systimers queued, which is
219 * usually *ALL* of them. We need to use the LAPIC timer for this.
221 sysclock_count
= sys_cputimer
->count();
222 for (n
= 0; n
< ncpus
; ++n
) {
223 gscan
= globaldata_find(n
);
224 if (TAILQ_FIRST(&gscan
->gd_systimerq
) == NULL
)
227 lwkt_send_ipiq3(gscan
, (ipifunc3_t
)systimer_intr
,
230 systimer_intr(&sysclock_count
, 0, frame_arg
);
240 acquire_timer2(int mode
)
242 if (timer2_state
!= RELEASED
)
244 timer2_state
= ACQUIRED
;
247 * This access to the timer registers is as atomic as possible
248 * because it is a single instruction. We could do better if we
251 outb(TIMER_MODE
, TIMER_SEL2
| (mode
& 0x3f));
258 if (timer2_state
!= ACQUIRED
)
260 outb(TIMER_MODE
, TIMER_SEL2
| TIMER_SQWAVE
| TIMER_16BIT
);
261 timer2_state
= RELEASED
;
269 DB_SHOW_COMMAND(rtc
, rtc
)
271 kprintf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
272 rtcin(RTC_YEAR
), rtcin(RTC_MONTH
), rtcin(RTC_DAY
),
273 rtcin(RTC_HRS
), rtcin(RTC_MIN
), rtcin(RTC_SEC
),
274 rtcin(RTC_STATUSA
), rtcin(RTC_STATUSB
), rtcin(RTC_INTR
));
279 * Return the current cpu timer count as a 32 bit integer.
283 i8254_cputimer_count(void)
285 static uint16_t cputimer_last
;
290 outb(TIMER_MODE
, i8254_walltimer_sel
| TIMER_LATCH
);
291 count
= (uint8_t)inb(i8254_walltimer_cntr
); /* get countdown */
292 count
|= ((uint8_t)inb(i8254_walltimer_cntr
) << 8);
293 count
= -count
; /* -> countup */
294 if (count
< cputimer_last
) /* rollover */
295 i8254_cputimer
.base
+= 0x00010000;
296 ret
= i8254_cputimer
.base
| count
;
297 cputimer_last
= count
;
303 * This function is called whenever the system timebase changes, allowing
304 * us to calculate what is needed to convert a system timebase tick
305 * into an 8254 tick for the interrupt timer. If we can convert to a
306 * simple shift, multiplication, or division, we do so. Otherwise 64
307 * bit arithmatic is required every time the interrupt timer is reloaded.
310 i8254_intr_config(struct cputimer_intr
*cti
, const struct cputimer
*timer
)
316 * Will a simple divide do the trick?
318 div
= (timer
->freq
+ (cti
->freq
/ 2)) / cti
->freq
;
319 freq
= cti
->freq
* div
;
321 if (freq
>= timer
->freq
- 1 && freq
<= timer
->freq
+ 1)
322 i8254_cputimer_div
= div
;
324 i8254_cputimer_div
= 0;
328 * Reload for the next timeout. It is possible for the reload value
329 * to be 0 or negative, indicating that an immediate timer interrupt
330 * is desired. For now make the minimum 2 ticks.
332 * We may have to convert from the system timebase to the 8254 timebase.
335 i8254_intr_reload(struct cputimer_intr
*cti
, sysclock_t reload
)
339 if (i8254_cputimer_div
)
340 reload
/= i8254_cputimer_div
;
342 reload
= (int64_t)reload
* cti
->freq
/ sys_cputimer
->freq
;
348 if (timer0_running
) {
349 outb(TIMER_MODE
, TIMER_SEL0
| TIMER_LATCH
); /* count-down timer */
350 count
= (uint8_t)inb(TIMER_CNTR0
); /* lsb */
351 count
|= ((uint8_t)inb(TIMER_CNTR0
) << 8); /* msb */
352 if (reload
< count
) {
353 outb(TIMER_MODE
, TIMER_SEL0
| TIMER_SWSTROBE
| TIMER_16BIT
);
354 outb(TIMER_CNTR0
, (uint8_t)reload
); /* lsb */
355 outb(TIMER_CNTR0
, (uint8_t)(reload
>> 8)); /* msb */
360 reload
= 0; /* full count */
361 outb(TIMER_MODE
, TIMER_SEL0
| TIMER_SWSTROBE
| TIMER_16BIT
);
362 outb(TIMER_CNTR0
, (uint8_t)reload
); /* lsb */
363 outb(TIMER_CNTR0
, (uint8_t)(reload
>> 8)); /* msb */
369 * DELAY(usec) - Spin for the specified number of microseconds.
370 * DRIVERSLEEP(usec) - Spin for the specified number of microseconds,
371 * but do a thread switch in the loop
373 * Relies on timer 1 counting down from (cputimer_freq / hz)
374 * Note: timer had better have been programmed before this is first used!
377 DODELAY(int n
, int doswitch
)
379 ssysclock_t delta
, ticks_left
;
380 sysclock_t prev_tick
, tick
;
385 static int state
= 0;
389 for (n1
= 1; n1
<= 10000000; n1
*= 10)
394 kprintf("DELAY(%d)...", n
);
397 * Guard against the timer being uninitialized if we are called
398 * early for console i/o.
400 if (timer0_state
== RELEASED
)
404 * Read the counter first, so that the rest of the setup overhead is
405 * counted. Then calculate the number of hardware timer ticks
406 * required, rounding up to be sure we delay at least the requested
407 * number of microseconds.
409 prev_tick
= sys_cputimer
->count();
410 ticks_left
= ((u_int
)n
* (int64_t)sys_cputimer
->freq
+ 999999) /
416 while (ticks_left
> 0) {
417 tick
= sys_cputimer
->count();
421 delta
= tick
- prev_tick
;
426 if (doswitch
&& ticks_left
> 0)
432 kprintf(" %d calls to getit() at %d usec each\n",
433 getit_calls
, (n
+ 5) / getit_calls
);
438 * DELAY() never switches.
447 * Returns non-zero if the specified time period has elapsed. Call
448 * first with last_clock set to 0.
451 CHECKTIMEOUT(TOTALDELAY
*tdd
)
456 if (tdd
->started
== 0) {
457 if (timer0_state
== RELEASED
)
459 tdd
->last_clock
= sys_cputimer
->count();
463 delta
= sys_cputimer
->count() - tdd
->last_clock
;
464 us
= (u_int64_t
)delta
* (u_int64_t
)1000000 /
465 (u_int64_t
)sys_cputimer
->freq
;
466 tdd
->last_clock
+= (u_int64_t
)us
* (u_int64_t
)sys_cputimer
->freq
/
469 return (tdd
->us
< 0);
474 * DRIVERSLEEP() does not switch if called with a spinlock held or
475 * from a hard interrupt.
478 DRIVERSLEEP(int usec
)
480 globaldata_t gd
= mycpu
;
482 if (gd
->gd_intr_nesting_level
|| gd
->gd_spinlocks
) {
490 sysbeepstop(void *chan
)
492 outb(IO_PPI
, inb(IO_PPI
)&0xFC); /* disable counter2 output to speaker */
498 sysbeep(int pitch
, int period
)
500 if (acquire_timer2(TIMER_SQWAVE
|TIMER_16BIT
))
502 if (sysbeep_enable
== 0)
505 * Nobody else is using timer2, we do not need the clock lock
507 outb(TIMER_CNTR2
, pitch
);
508 outb(TIMER_CNTR2
, (pitch
>>8));
510 /* enable counter2 output to speaker */
511 outb(IO_PPI
, inb(IO_PPI
) | 3);
513 callout_reset(&sysbeepstop_ch
, period
, sysbeepstop
, NULL
);
519 * RTC support routines
530 val
= inb(IO_RTC
+ 1);
537 writertc(u_char reg
, u_char val
)
543 outb(IO_RTC
+ 1, val
);
544 inb(0x84); /* XXX work around wrong order in rtcin() */
551 return(bcd2bin(rtcin(port
)));
555 calibrate_clocks(void)
559 sysclock_t count
, prev_count
;
560 int sec
, start_sec
, timeout
;
563 kprintf("Calibrating clock(s) ...\n");
564 if (!(rtcin(RTC_STATUSD
) & RTCSD_PWR
))
568 /* Read the mc146818A seconds counter. */
570 if (!(rtcin(RTC_STATUSA
) & RTCSA_TUP
)) {
571 sec
= rtcin(RTC_SEC
);
578 /* Wait for the mC146818A seconds counter to change. */
581 if (!(rtcin(RTC_STATUSA
) & RTCSA_TUP
)) {
582 sec
= rtcin(RTC_SEC
);
583 if (sec
!= start_sec
)
590 /* Start keeping track of the i8254 counter. */
591 prev_count
= sys_cputimer
->count();
597 old_tsc
= 0; /* shut up gcc */
600 * Wait for the mc146818A seconds counter to change. Read the i8254
601 * counter for each iteration since this is convenient and only
602 * costs a few usec of inaccuracy. The timing of the final reads
603 * of the counters almost matches the timing of the initial reads,
604 * so the main cause of inaccuracy is the varying latency from
605 * inside getit() or rtcin(RTC_STATUSA) to the beginning of the
606 * rtcin(RTC_SEC) that returns a changed seconds count. The
607 * maximum inaccuracy from this cause is < 10 usec on 486's.
611 if (!(rtcin(RTC_STATUSA
) & RTCSA_TUP
))
612 sec
= rtcin(RTC_SEC
);
613 count
= sys_cputimer
->count();
614 tot_count
+= (int)(count
- prev_count
);
616 if (sec
!= start_sec
)
623 * Read the cpu cycle counter. The timing considerations are
624 * similar to those for the i8254 clock.
627 tsc_frequency
= rdtsc() - old_tsc
;
629 kprintf("TSC clock: %jd Hz (Method A)\n",
630 (intmax_t)tsc_frequency
);
634 kprintf("i8254 clock: %u Hz\n", tot_count
);
638 kprintf("failed, using default i8254 clock of %u Hz\n",
639 i8254_cputimer
.freq
);
640 return (i8254_cputimer
.freq
);
646 timer0_state
= ACQUIRED
;
651 * Timer0 is our fine-grained variable clock interrupt
653 outb(TIMER_MODE
, TIMER_SEL0
| TIMER_SWSTROBE
| TIMER_16BIT
);
654 outb(TIMER_CNTR0
, 2); /* lsb */
655 outb(TIMER_CNTR0
, 0); /* msb */
659 cputimer_intr_register(&i8254_cputimer_intr
);
660 cputimer_intr_select(&i8254_cputimer_intr
, 0);
664 * Timer1 or timer2 is our free-running clock, but only if another
665 * has not been selected.
667 cputimer_register(&i8254_cputimer
);
668 cputimer_select(&i8254_cputimer
, 0);
672 i8254_cputimer_construct(struct cputimer
*timer
, sysclock_t oldclock
)
677 * Should we use timer 1 or timer 2 ?
680 TUNABLE_INT_FETCH("hw.i8254.walltimer", &which
);
681 if (which
!= 1 && which
!= 2)
686 timer
->name
= "i8254_timer1";
687 timer
->type
= CPUTIMER_8254_SEL1
;
688 i8254_walltimer_sel
= TIMER_SEL1
;
689 i8254_walltimer_cntr
= TIMER_CNTR1
;
690 timer1_state
= ACQUIRED
;
693 timer
->name
= "i8254_timer2";
694 timer
->type
= CPUTIMER_8254_SEL2
;
695 i8254_walltimer_sel
= TIMER_SEL2
;
696 i8254_walltimer_cntr
= TIMER_CNTR2
;
697 timer2_state
= ACQUIRED
;
701 timer
->base
= (oldclock
+ 0xFFFF) & ~0xFFFF;
704 outb(TIMER_MODE
, i8254_walltimer_sel
| TIMER_RATEGEN
| TIMER_16BIT
);
705 outb(i8254_walltimer_cntr
, 0); /* lsb */
706 outb(i8254_walltimer_cntr
, 0); /* msb */
707 outb(IO_PPI
, inb(IO_PPI
) | 1); /* bit 0: enable gate, bit 1: spkr */
712 i8254_cputimer_destruct(struct cputimer
*timer
)
714 switch(timer
->type
) {
715 case CPUTIMER_8254_SEL1
:
716 timer1_state
= RELEASED
;
718 case CPUTIMER_8254_SEL2
:
719 timer2_state
= RELEASED
;
730 /* Restore all of the RTC's "status" (actually, control) registers. */
731 writertc(RTC_STATUSB
, RTCSB_24HR
);
732 writertc(RTC_STATUSA
, rtc_statusa
);
733 writertc(RTC_STATUSB
, rtc_statusb
);
737 * Restore all the timers.
739 * This function is called to resynchronize our core timekeeping after a
740 * long halt, e.g. from apm_default_resume() and friends. It is also
741 * called if after a BIOS call we have detected munging of the 8254.
742 * It is necessary because cputimer_count() counter's delta may have grown
743 * too large for nanouptime() and friends to handle, or (in the case of 8254
744 * munging) might cause the SYSTIMER code to prematurely trigger.
750 i8254_restore(); /* restore timer_freq and hz */
751 rtc_restore(); /* reenable RTC interrupts */
756 * Initialize 8254 timer 0 early so that it can be used in DELAY().
764 * Can we use the TSC?
766 * NOTE: If running under qemu, probably a good idea to force the
767 * TSC because we are not likely to detect it as being
768 * invariant or mpsyncd if you don't. This will greatly
769 * reduce SMP contention.
771 if (cpu_feature
& CPUID_TSC
) {
773 TUNABLE_INT_FETCH("hw.tsc_cputimer_force", &tsc_invariant
);
775 if ((cpu_vendor_id
== CPU_VENDOR_INTEL
||
776 cpu_vendor_id
== CPU_VENDOR_AMD
) &&
777 cpu_exthigh
>= 0x80000007) {
780 do_cpuid(0x80000007, regs
);
789 * Initial RTC state, don't do anything unexpected
791 writertc(RTC_STATUSA
, rtc_statusa
);
792 writertc(RTC_STATUSB
, RTCSB_24HR
);
795 * Set the 8254 timer0 in TIMER_SWSTROBE mode and cause it to
796 * generate an interrupt, which we will ignore for now.
798 * Set the 8254 timer1 in TIMER_RATEGEN mode and load 0x0000
799 * (so it counts a full 2^16 and repeats). We will use this timer
803 freq
= calibrate_clocks();
804 #ifdef CLK_CALIBRATION_LOOP
809 kprintf("Press a key on the console to "
810 "abort clock calibration\n");
811 while ((c
= cncheckc()) == -1 || c
== NOKEY
)
818 * Use the calibrated i8254 frequency if it seems reasonable.
819 * Otherwise use the default, and don't use the calibrated i586
822 delta
= freq
> i8254_cputimer
.freq
?
823 freq
- i8254_cputimer
.freq
: i8254_cputimer
.freq
- freq
;
824 if (delta
< i8254_cputimer
.freq
/ 100) {
825 #ifndef CLK_USE_I8254_CALIBRATION
828 "CLK_USE_I8254_CALIBRATION not specified - using default frequency\n");
829 freq
= i8254_cputimer
.freq
;
833 * Interrupt timer's freq must be adjusted
834 * before we change the cuptimer's frequency.
836 i8254_cputimer_intr
.freq
= freq
;
837 cputimer_set_frequency(&i8254_cputimer
, freq
);
841 "%d Hz differs from default of %d Hz by more than 1%%\n",
842 freq
, i8254_cputimer
.freq
);
846 #ifndef CLK_USE_TSC_CALIBRATION
847 if (tsc_frequency
!= 0) {
850 "CLK_USE_TSC_CALIBRATION not specified - using old calibration method\n");
854 if (tsc_present
&& tsc_frequency
== 0) {
856 * Calibration of the i586 clock relative to the mc146818A
857 * clock failed. Do a less accurate calibration relative
858 * to the i8254 clock.
860 u_int64_t old_tsc
= rdtsc();
863 tsc_frequency
= rdtsc() - old_tsc
;
864 #ifdef CLK_USE_TSC_CALIBRATION
866 kprintf("TSC clock: %jd Hz (Method B)\n",
867 (intmax_t)tsc_frequency
);
873 kprintf("TSC%s clock: %jd Hz\n",
874 tsc_invariant
? " invariant" : "",
875 (intmax_t)tsc_frequency
);
878 EVENTHANDLER_REGISTER(shutdown_post_sync
, resettodr_on_shutdown
, NULL
, SHUTDOWN_PRI_LAST
);
882 * Sync the time of day back to the RTC on shutdown, but only if
883 * we have already loaded it and have not crashed.
886 resettodr_on_shutdown(void *arg __unused
)
888 if (rtc_loaded
&& panicstr
== NULL
) {
894 * Initialize the time of day register, based on the time base which is, e.g.
898 inittodr(time_t base
)
900 unsigned long sec
, days
;
911 /* Look if we have a RTC present and the time is valid */
912 if (!(rtcin(RTC_STATUSD
) & RTCSD_PWR
))
915 /* wait for time update to complete */
916 /* If RTCSA_TUP is zero, we have at least 244us before next update */
918 while (rtcin(RTC_STATUSA
) & RTCSA_TUP
) {
924 #ifdef USE_RTC_CENTURY
925 year
= readrtc(RTC_YEAR
) + readrtc(RTC_CENTURY
) * 100;
927 year
= readrtc(RTC_YEAR
) + 1900;
935 month
= readrtc(RTC_MONTH
);
936 for (m
= 1; m
< month
; m
++)
937 days
+= daysinmonth
[m
-1];
938 if ((month
> 2) && LEAPYEAR(year
))
940 days
+= readrtc(RTC_DAY
) - 1;
941 for (y
= 1970; y
< year
; y
++)
942 days
+= DAYSPERYEAR
+ LEAPYEAR(y
);
943 sec
= ((( days
* 24 +
944 readrtc(RTC_HRS
)) * 60 +
945 readrtc(RTC_MIN
)) * 60 +
947 /* sec now contains the number of seconds, since Jan 1 1970,
948 in the local time zone */
950 sec
+= tz
.tz_minuteswest
* 60 + (wall_cmos_clock
? adjkerntz
: 0);
952 y
= (int)(time_second
- sec
);
953 if (y
<= -2 || y
>= 2) {
954 /* badly off, adjust it */
964 kprintf("Invalid time in real time clock.\n");
965 kprintf("Check and reset the date immediately!\n");
969 * Write system time back to RTC
986 /* Disable RTC updates and interrupts. */
987 writertc(RTC_STATUSB
, RTCSB_HALT
| RTCSB_24HR
);
989 /* Calculate local time to put in RTC */
991 tm
-= tz
.tz_minuteswest
* 60 + (wall_cmos_clock
? adjkerntz
: 0);
993 writertc(RTC_SEC
, bin2bcd(tm
%60)); tm
/= 60; /* Write back Seconds */
994 writertc(RTC_MIN
, bin2bcd(tm
%60)); tm
/= 60; /* Write back Minutes */
995 writertc(RTC_HRS
, bin2bcd(tm
%24)); tm
/= 24; /* Write back Hours */
997 /* We have now the days since 01-01-1970 in tm */
998 writertc(RTC_WDAY
, (tm
+4)%7); /* Write back Weekday */
999 for (y
= 1970, m
= DAYSPERYEAR
+ LEAPYEAR(y
);
1001 y
++, m
= DAYSPERYEAR
+ LEAPYEAR(y
))
1004 /* Now we have the years in y and the day-of-the-year in tm */
1005 writertc(RTC_YEAR
, bin2bcd(y
%100)); /* Write back Year */
1006 #ifdef USE_RTC_CENTURY
1007 writertc(RTC_CENTURY
, bin2bcd(y
/100)); /* ... and Century */
1009 for (m
= 0; ; m
++) {
1012 ml
= daysinmonth
[m
];
1013 if (m
== 1 && LEAPYEAR(y
))
1020 writertc(RTC_MONTH
, bin2bcd(m
+ 1)); /* Write back Month */
1021 writertc(RTC_DAY
, bin2bcd(tm
+ 1)); /* Write back Month Day */
1023 /* Reenable RTC updates and interrupts. */
1024 writertc(RTC_STATUSB
, rtc_statusb
);
1029 i8254_ioapic_trial(int irq
, struct cputimer_intr
*cti
)
1035 * Following code assumes the 8254 is the cpu timer,
1036 * so make sure it is.
1038 KKASSERT(sys_cputimer
== &i8254_cputimer
);
1039 KKASSERT(cti
== &i8254_cputimer_intr
);
1041 lastcnt
= get_interrupt_counter(irq
, mycpuid
);
1044 * Force an 8254 Timer0 interrupt and wait 1/100s for
1045 * it to happen, then see if we got it.
1047 kprintf("IOAPIC: testing 8254 interrupt delivery\n");
1049 i8254_intr_reload(cti
, 2);
1050 base
= sys_cputimer
->count();
1051 while (sys_cputimer
->count() - base
< sys_cputimer
->freq
/ 100)
1054 if (get_interrupt_counter(irq
, mycpuid
) - lastcnt
== 0)
1060 * Start both clocks running. DragonFly note: the stat clock is no longer
1061 * used. Instead, 8254 based systimers are used for all major clock
1065 i8254_intr_initclock(struct cputimer_intr
*cti
, boolean_t selected
)
1067 void *clkdesc
= NULL
;
1068 int irq
= 0, mixed_mode
= 0, error
;
1070 KKASSERT(mycpuid
== 0);
1071 callout_init_mp(&sysbeepstop_ch
);
1073 if (!selected
&& i8254_intr_disable
)
1077 * The stat interrupt mask is different without the
1078 * statistics clock. Also, don't set the interrupt
1079 * flag which would normally cause the RTC to generate
1082 rtc_statusb
= RTCSB_24HR
;
1084 /* Finish initializing 8254 timer 0. */
1085 if (ioapic_enable
) {
1086 irq
= machintr_legacy_intr_find(0, INTR_TRIGGER_EDGE
,
1087 INTR_POLARITY_HIGH
);
1090 error
= ioapic_conf_legacy_extint(0);
1092 irq
= machintr_legacy_intr_find(0,
1093 INTR_TRIGGER_EDGE
, INTR_POLARITY_HIGH
);
1100 kprintf("IOAPIC: setup mixed mode for "
1101 "irq 0 failed: %d\n", error
);
1104 panic("IOAPIC: setup mixed mode for "
1105 "irq 0 failed: %d\n", error
);
1110 clkdesc
= register_int(irq
, clkintr
, NULL
, "clk",
1112 INTR_EXCL
| INTR_CLOCK
|
1113 INTR_NOPOLL
| INTR_MPSAFE
|
1116 register_int(0, clkintr
, NULL
, "clk", NULL
,
1117 INTR_EXCL
| INTR_CLOCK
|
1118 INTR_NOPOLL
| INTR_MPSAFE
|
1122 /* Initialize RTC. */
1123 writertc(RTC_STATUSA
, rtc_statusa
);
1124 writertc(RTC_STATUSB
, RTCSB_24HR
);
1126 if (ioapic_enable
) {
1127 error
= i8254_ioapic_trial(irq
, cti
);
1131 kprintf("IOAPIC: mixed mode for irq %d "
1132 "trial failed: %d\n",
1136 panic("IOAPIC: mixed mode for irq %d "
1137 "trial failed: %d\n", irq
, error
);
1140 kprintf("IOAPIC: warning 8254 is not connected "
1141 "to the correct pin, try mixed mode\n");
1142 unregister_int(clkdesc
, 0);
1143 goto mixed_mode_setup
;
1150 i8254_nointr
= 1; /* don't try to register again */
1151 cputimer_intr_deregister(cti
);
1155 setstatclockrate(int newhz
)
1157 if (newhz
== RTC_PROFRATE
)
1158 rtc_statusa
= RTCSA_DIVIDER
| RTCSA_PROF
;
1160 rtc_statusa
= RTCSA_DIVIDER
| RTCSA_NOPROF
;
1161 writertc(RTC_STATUSA
, rtc_statusa
);
1166 tsc_get_timecount(struct timecounter
*tc
)
1172 #ifdef KERN_TIMESTAMP
1173 #define KERN_TIMESTAMP_SIZE 16384
1174 static u_long tsc
[KERN_TIMESTAMP_SIZE
] ;
1175 SYSCTL_OPAQUE(_debug
, OID_AUTO
, timestamp
, CTLFLAG_RD
, tsc
,
1176 sizeof(tsc
), "LU", "Kernel timestamps");
1182 tsc
[i
] = (u_int32_t
)rdtsc();
1185 if (i
>= KERN_TIMESTAMP_SIZE
)
1187 tsc
[i
] = 0; /* mark last entry */
1189 #endif /* KERN_TIMESTAMP */
1196 hw_i8254_timestamp(SYSCTL_HANDLER_ARGS
)
1203 if (sys_cputimer
== &i8254_cputimer
)
1204 count
= sys_cputimer
->count();
1212 ksnprintf(buf
, sizeof(buf
), "%08x %016llx", count
, (long long)tscval
);
1213 return(SYSCTL_OUT(req
, buf
, strlen(buf
) + 1));
1216 struct tsc_mpsync_arg
{
1217 volatile uint64_t tsc_target
;
1218 volatile int tsc_mpsync
;
1221 struct tsc_mpsync_thr
{
1222 volatile int tsc_done_cnt
;
1223 volatile int tsc_mpsync_cnt
;
1227 tsc_mpsync_test_remote(void *xarg
)
1229 struct tsc_mpsync_arg
*arg
= xarg
;
1232 tsc
= rdtsc_ordered();
1233 if (tsc
< arg
->tsc_target
)
1234 arg
->tsc_mpsync
= 0;
1238 tsc_mpsync_test_loop(struct tsc_mpsync_arg
*arg
)
1240 struct globaldata
*gd
= mycpu
;
1241 uint64_t test_end
, test_begin
;
1245 kprintf("cpu%d: TSC testing MP synchronization ...\n",
1249 test_begin
= rdtsc_ordered();
1250 /* Run test for 100ms */
1251 test_end
= test_begin
+ (tsc_frequency
/ 10);
1253 arg
->tsc_mpsync
= 1;
1254 arg
->tsc_target
= test_begin
;
1256 #define TSC_TEST_TRYMAX 1000000 /* Make sure we could stop */
1257 #define TSC_TEST_TRYMIN 50000
1259 for (i
= 0; i
< TSC_TEST_TRYMAX
; ++i
) {
1260 struct lwkt_cpusync cs
;
1263 lwkt_cpusync_init(&cs
, gd
->gd_other_cpus
,
1264 tsc_mpsync_test_remote
, arg
);
1265 lwkt_cpusync_interlock(&cs
);
1266 arg
->tsc_target
= rdtsc_ordered();
1268 lwkt_cpusync_deinterlock(&cs
);
1271 if (!arg
->tsc_mpsync
) {
1272 kprintf("cpu%d: TSC is not MP synchronized @%u\n",
1276 if (arg
->tsc_target
> test_end
&& i
>= TSC_TEST_TRYMIN
)
1280 #undef TSC_TEST_TRYMIN
1281 #undef TSC_TEST_TRYMAX
1283 if (arg
->tsc_target
== test_begin
) {
1284 kprintf("cpu%d: TSC does not tick?!\n", gd
->gd_cpuid
);
1285 /* XXX disable TSC? */
1287 arg
->tsc_mpsync
= 0;
1291 if (arg
->tsc_mpsync
&& bootverbose
) {
1292 kprintf("cpu%d: TSC is MP synchronized after %u tries\n",
1298 tsc_mpsync_ap_thread(void *xthr
)
1300 struct tsc_mpsync_thr
*thr
= xthr
;
1301 struct tsc_mpsync_arg arg
;
1303 tsc_mpsync_test_loop(&arg
);
1304 if (arg
.tsc_mpsync
) {
1305 atomic_add_int(&thr
->tsc_mpsync_cnt
, 1);
1308 atomic_add_int(&thr
->tsc_done_cnt
, 1);
1314 tsc_mpsync_test(void)
1316 struct tsc_mpsync_arg arg
;
1318 if (!tsc_invariant
) {
1319 /* Not even invariant TSC */
1330 * Forcing can be used w/qemu to reduce contention
1332 TUNABLE_INT_FETCH("hw.tsc_cputimer_force", &tsc_mpsync
);
1334 kprintf("TSC as cputimer forced\n");
1338 if (cpu_vendor_id
!= CPU_VENDOR_INTEL
) {
1339 /* XXX only Intel works */
1343 kprintf("TSC testing MP synchronization ...\n");
1345 tsc_mpsync_test_loop(&arg
);
1346 if (arg
.tsc_mpsync
) {
1347 struct tsc_mpsync_thr thr
;
1351 * Test TSC MP synchronization on APs.
1354 thr
.tsc_done_cnt
= 1;
1355 thr
.tsc_mpsync_cnt
= 1;
1357 for (cpu
= 0; cpu
< ncpus
; ++cpu
) {
1361 lwkt_create(tsc_mpsync_ap_thread
, &thr
, NULL
,
1362 NULL
, 0, cpu
, "tsc mpsync %d", cpu
);
1365 while (thr
.tsc_done_cnt
!= ncpus
) {
1369 if (thr
.tsc_mpsync_cnt
== ncpus
)
1374 kprintf("TSC is MP synchronized\n");
1376 kprintf("TSC is not MP synchronized\n");
1378 SYSINIT(tsc_mpsync
, SI_BOOT2_FINISH_SMP
, SI_ORDER_ANY
, tsc_mpsync_test
, NULL
);
1380 #define TSC_CPUTIMER_FREQMAX 128000000 /* 128Mhz */
1382 static int tsc_cputimer_shift
;
1385 tsc_cputimer_construct(struct cputimer
*timer
, sysclock_t oldclock
)
1388 timer
->base
= oldclock
- timer
->count();
1391 static __inline sysclock_t
1392 tsc_cputimer_count(void)
1397 tsc
>>= tsc_cputimer_shift
;
1399 return (tsc
+ tsc_cputimer
.base
);
1403 tsc_cputimer_count_lfence(void)
1406 return tsc_cputimer_count();
1410 tsc_cputimer_count_mfence(void)
1413 return tsc_cputimer_count();
1417 tsc_cpucounter_count_lfence(void)
1425 tsc_cpucounter_count_mfence(void)
1433 tsc_cputimer_register(void)
1439 if (tsc_invariant
) {
1440 /* Per-cpu cpucounter still works. */
1446 TUNABLE_INT_FETCH("hw.tsc_cputimer_enable", &enable
);
1450 freq
= tsc_frequency
;
1451 while (freq
> TSC_CPUTIMER_FREQMAX
) {
1453 ++tsc_cputimer_shift
;
1455 kprintf("TSC: cputimer freq %ju, shift %d\n",
1456 (uintmax_t)freq
, tsc_cputimer_shift
);
1458 tsc_cputimer
.freq
= freq
;
1460 if (cpu_vendor_id
== CPU_VENDOR_INTEL
)
1461 tsc_cputimer
.count
= tsc_cputimer_count_lfence
;
1463 tsc_cputimer
.count
= tsc_cputimer_count_mfence
; /* safe bet */
1465 cputimer_register(&tsc_cputimer
);
1466 cputimer_select(&tsc_cputimer
, 0);
1468 tsc_cpucounter
.flags
|= CPUCOUNTER_FLAG_MPSYNC
;
1470 tsc_cpucounter
.freq
= tsc_frequency
;
1471 if (cpu_vendor_id
== CPU_VENDOR_INTEL
) {
1472 tsc_cpucounter
.count
=
1473 tsc_cpucounter_count_lfence
;
1475 tsc_cpucounter
.count
=
1476 tsc_cpucounter_count_mfence
; /* safe bet */
1478 cpucounter_register(&tsc_cpucounter
);
1480 SYSINIT(tsc_cputimer_reg
, SI_BOOT2_POST_SMP
, SI_ORDER_FIRST
,
1481 tsc_cputimer_register
, NULL
);
1483 SYSCTL_NODE(_hw
, OID_AUTO
, i8254
, CTLFLAG_RW
, 0, "I8254");
1484 SYSCTL_UINT(_hw_i8254
, OID_AUTO
, freq
, CTLFLAG_RD
, &i8254_cputimer
.freq
, 0,
1486 SYSCTL_PROC(_hw_i8254
, OID_AUTO
, timestamp
, CTLTYPE_STRING
|CTLFLAG_RD
,
1487 0, 0, hw_i8254_timestamp
, "A", "");
1489 SYSCTL_INT(_hw
, OID_AUTO
, tsc_present
, CTLFLAG_RD
,
1490 &tsc_present
, 0, "TSC Available");
1491 SYSCTL_INT(_hw
, OID_AUTO
, tsc_invariant
, CTLFLAG_RD
,
1492 &tsc_invariant
, 0, "Invariant TSC");
1493 SYSCTL_INT(_hw
, OID_AUTO
, tsc_mpsync
, CTLFLAG_RD
,
1494 &tsc_mpsync
, 0, "TSC is synchronized across CPUs");
1495 SYSCTL_QUAD(_hw
, OID_AUTO
, tsc_frequency
, CTLFLAG_RD
,
1496 &tsc_frequency
, 0, "TSC Frequency");