tsc: Log the final TSC frequency
[dragonfly.git] / sys / platform / pc64 / isa / clock.c
blob7669125b7915590f4c2d242d1eaeb96ddb2ef595
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * Copyright (c) 2008 The DragonFly Project.
4 * All rights reserved.
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
11 * are met:
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
31 * SUCH DAMAGE.
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
48 #if 0
49 #include "opt_clock.h"
50 #endif
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/eventhandler.h>
55 #include <sys/time.h>
56 #include <sys/kernel.h>
57 #include <sys/bus.h>
58 #include <sys/sysctl.h>
59 #include <sys/cons.h>
60 #include <sys/kbio.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)
97 #ifndef TIMER_FREQ
98 #define TIMER_FREQ 1193182
99 #endif
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 */
106 int tsc_present;
107 int tsc_invariant;
108 int tsc_mpsync;
109 int64_t tsc_frequency;
110 int tsc_is_broken;
111 int wall_cmos_clock; /* wall CMOS clock assumed if != 0 */
112 int timer0_running;
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,
138 "i8254",
139 CPUTIMER_PRI_8254,
141 i8254_cputimer_count,
142 cputimer_default_fromhz,
143 cputimer_default_fromus,
144 i8254_cputimer_construct,
145 i8254_cputimer_destruct,
146 TIMER_FREQ,
147 0, 0, 0
150 static sysclock_t tsc_cputimer_count_mfence(void);
151 static sysclock_t tsc_cputimer_count_lfence(void);
152 static void tsc_cputimer_construct(struct cputimer *, sysclock_t);
154 static struct cputimer tsc_cputimer = {
155 SLIST_ENTRY_INITIALIZER,
156 "TSC",
157 CPUTIMER_PRI_TSC,
158 CPUTIMER_TSC,
159 tsc_cputimer_count_mfence, /* safe bet */
160 cputimer_default_fromhz,
161 cputimer_default_fromus,
162 tsc_cputimer_construct,
163 cputimer_default_destruct,
165 0, 0, 0
168 static void i8254_intr_reload(struct cputimer_intr *, sysclock_t);
169 static void i8254_intr_config(struct cputimer_intr *, const struct cputimer *);
170 static void i8254_intr_initclock(struct cputimer_intr *, boolean_t);
172 static struct cputimer_intr i8254_cputimer_intr = {
173 .freq = TIMER_FREQ,
174 .reload = i8254_intr_reload,
175 .enable = cputimer_intr_default_enable,
176 .config = i8254_intr_config,
177 .restart = cputimer_intr_default_restart,
178 .pmfixup = cputimer_intr_default_pmfixup,
179 .initclock = i8254_intr_initclock,
180 .next = SLIST_ENTRY_INITIALIZER,
181 .name = "i8254",
182 .type = CPUTIMER_INTR_8254,
183 .prio = CPUTIMER_INTR_PRIO_8254,
184 .caps = CPUTIMER_INTR_CAP_PS
188 * timer0 clock interrupt. Timer0 is in one-shot mode and has stopped
189 * counting as of this interrupt. We use timer1 in free-running mode (not
190 * generating any interrupts) as our main counter. Each cpu has timeouts
191 * pending.
193 * This code is INTR_MPSAFE and may be called without the BGL held.
195 static void
196 clkintr(void *dummy, void *frame_arg)
198 static sysclock_t sysclock_count; /* NOTE! Must be static */
199 struct globaldata *gd = mycpu;
200 struct globaldata *gscan;
201 int n;
204 * SWSTROBE mode is a one-shot, the timer is no longer running
206 timer0_running = 0;
209 * XXX the dispatcher needs work. right now we call systimer_intr()
210 * directly or via IPI for any cpu with systimers queued, which is
211 * usually *ALL* of them. We need to use the LAPIC timer for this.
213 sysclock_count = sys_cputimer->count();
214 for (n = 0; n < ncpus; ++n) {
215 gscan = globaldata_find(n);
216 if (TAILQ_FIRST(&gscan->gd_systimerq) == NULL)
217 continue;
218 if (gscan != gd) {
219 lwkt_send_ipiq3(gscan, (ipifunc3_t)systimer_intr,
220 &sysclock_count, 1);
221 } else {
222 systimer_intr(&sysclock_count, 0, frame_arg);
229 * NOTE! not MP safe.
232 acquire_timer2(int mode)
234 if (timer2_state != RELEASED)
235 return (-1);
236 timer2_state = ACQUIRED;
239 * This access to the timer registers is as atomic as possible
240 * because it is a single instruction. We could do better if we
241 * knew the rate.
243 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f));
244 return (0);
248 release_timer2(void)
250 if (timer2_state != ACQUIRED)
251 return (-1);
252 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT);
253 timer2_state = RELEASED;
254 return (0);
257 #include "opt_ddb.h"
258 #ifdef DDB
259 #include <ddb/ddb.h>
261 DB_SHOW_COMMAND(rtc, rtc)
263 kprintf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
264 rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
265 rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
266 rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));
268 #endif /* DDB */
271 * Return the current cpu timer count as a 32 bit integer.
273 static
274 sysclock_t
275 i8254_cputimer_count(void)
277 static uint16_t cputimer_last;
278 uint16_t count;
279 sysclock_t ret;
281 clock_lock();
282 outb(TIMER_MODE, i8254_walltimer_sel | TIMER_LATCH);
283 count = (uint8_t)inb(i8254_walltimer_cntr); /* get countdown */
284 count |= ((uint8_t)inb(i8254_walltimer_cntr) << 8);
285 count = -count; /* -> countup */
286 if (count < cputimer_last) /* rollover */
287 i8254_cputimer.base += 0x00010000;
288 ret = i8254_cputimer.base | count;
289 cputimer_last = count;
290 clock_unlock();
291 return(ret);
295 * This function is called whenever the system timebase changes, allowing
296 * us to calculate what is needed to convert a system timebase tick
297 * into an 8254 tick for the interrupt timer. If we can convert to a
298 * simple shift, multiplication, or division, we do so. Otherwise 64
299 * bit arithmatic is required every time the interrupt timer is reloaded.
301 static void
302 i8254_intr_config(struct cputimer_intr *cti, const struct cputimer *timer)
304 int freq;
305 int div;
308 * Will a simple divide do the trick?
310 div = (timer->freq + (cti->freq / 2)) / cti->freq;
311 freq = cti->freq * div;
313 if (freq >= timer->freq - 1 && freq <= timer->freq + 1)
314 i8254_cputimer_div = div;
315 else
316 i8254_cputimer_div = 0;
320 * Reload for the next timeout. It is possible for the reload value
321 * to be 0 or negative, indicating that an immediate timer interrupt
322 * is desired. For now make the minimum 2 ticks.
324 * We may have to convert from the system timebase to the 8254 timebase.
326 static void
327 i8254_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
329 uint16_t count;
331 if (i8254_cputimer_div)
332 reload /= i8254_cputimer_div;
333 else
334 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
336 if ((int)reload < 2)
337 reload = 2;
339 clock_lock();
340 if (timer0_running) {
341 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); /* count-down timer */
342 count = (uint8_t)inb(TIMER_CNTR0); /* lsb */
343 count |= ((uint8_t)inb(TIMER_CNTR0) << 8); /* msb */
344 if (reload < count) {
345 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
346 outb(TIMER_CNTR0, (uint8_t)reload); /* lsb */
347 outb(TIMER_CNTR0, (uint8_t)(reload >> 8)); /* msb */
349 } else {
350 timer0_running = 1;
351 if (reload > 0xFFFF)
352 reload = 0; /* full 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 */
357 clock_unlock();
361 * DELAY(usec) - Spin for the specified number of microseconds.
362 * DRIVERSLEEP(usec) - Spin for the specified number of microseconds,
363 * but do a thread switch in the loop
365 * Relies on timer 1 counting down from (cputimer_freq / hz)
366 * Note: timer had better have been programmed before this is first used!
368 static void
369 DODELAY(int n, int doswitch)
371 ssysclock_t delta, ticks_left;
372 sysclock_t prev_tick, tick;
374 #ifdef DELAYDEBUG
375 int getit_calls = 1;
376 int n1;
377 static int state = 0;
379 if (state == 0) {
380 state = 1;
381 for (n1 = 1; n1 <= 10000000; n1 *= 10)
382 DELAY(n1);
383 state = 2;
385 if (state == 1)
386 kprintf("DELAY(%d)...", n);
387 #endif
389 * Guard against the timer being uninitialized if we are called
390 * early for console i/o.
392 if (timer0_state == RELEASED)
393 i8254_restore();
396 * Read the counter first, so that the rest of the setup overhead is
397 * counted. Then calculate the number of hardware timer ticks
398 * required, rounding up to be sure we delay at least the requested
399 * number of microseconds.
401 prev_tick = sys_cputimer->count();
402 ticks_left = ((u_int)n * (int64_t)sys_cputimer->freq + 999999) /
403 1000000;
406 * Loop until done.
408 while (ticks_left > 0) {
409 tick = sys_cputimer->count();
410 #ifdef DELAYDEBUG
411 ++getit_calls;
412 #endif
413 delta = tick - prev_tick;
414 prev_tick = tick;
415 if (delta < 0)
416 delta = 0;
417 ticks_left -= delta;
418 if (doswitch && ticks_left > 0)
419 lwkt_switch();
420 cpu_pause();
422 #ifdef DELAYDEBUG
423 if (state == 1)
424 kprintf(" %d calls to getit() at %d usec each\n",
425 getit_calls, (n + 5) / getit_calls);
426 #endif
430 * DELAY() never switches.
432 void
433 DELAY(int n)
435 DODELAY(n, 0);
439 * Returns non-zero if the specified time period has elapsed. Call
440 * first with last_clock set to 0.
443 CHECKTIMEOUT(TOTALDELAY *tdd)
445 sysclock_t delta;
446 int us;
448 if (tdd->started == 0) {
449 if (timer0_state == RELEASED)
450 i8254_restore();
451 tdd->last_clock = sys_cputimer->count();
452 tdd->started = 1;
453 return(0);
455 delta = sys_cputimer->count() - tdd->last_clock;
456 us = (u_int64_t)delta * (u_int64_t)1000000 /
457 (u_int64_t)sys_cputimer->freq;
458 tdd->last_clock += (u_int64_t)us * (u_int64_t)sys_cputimer->freq /
459 1000000;
460 tdd->us -= us;
461 return (tdd->us < 0);
466 * DRIVERSLEEP() does not switch if called with a spinlock held or
467 * from a hard interrupt.
469 void
470 DRIVERSLEEP(int usec)
472 globaldata_t gd = mycpu;
474 if (gd->gd_intr_nesting_level || gd->gd_spinlocks) {
475 DODELAY(usec, 0);
476 } else {
477 DODELAY(usec, 1);
481 static void
482 sysbeepstop(void *chan)
484 outb(IO_PPI, inb(IO_PPI)&0xFC); /* disable counter2 output to speaker */
485 beeping = 0;
486 release_timer2();
490 sysbeep(int pitch, int period)
492 if (acquire_timer2(TIMER_SQWAVE|TIMER_16BIT))
493 return(-1);
494 if (sysbeep_enable == 0)
495 return(-1);
497 * Nobody else is using timer2, we do not need the clock lock
499 outb(TIMER_CNTR2, pitch);
500 outb(TIMER_CNTR2, (pitch>>8));
501 if (!beeping) {
502 /* enable counter2 output to speaker */
503 outb(IO_PPI, inb(IO_PPI) | 3);
504 beeping = period;
505 callout_reset(&sysbeepstop_ch, period, sysbeepstop, NULL);
507 return (0);
511 * RTC support routines
515 rtcin(int reg)
517 u_char val;
519 crit_enter();
520 outb(IO_RTC, reg);
521 inb(0x84);
522 val = inb(IO_RTC + 1);
523 inb(0x84);
524 crit_exit();
525 return (val);
528 static __inline void
529 writertc(u_char reg, u_char val)
531 crit_enter();
532 inb(0x84);
533 outb(IO_RTC, reg);
534 inb(0x84);
535 outb(IO_RTC + 1, val);
536 inb(0x84); /* XXX work around wrong order in rtcin() */
537 crit_exit();
540 static __inline int
541 readrtc(int port)
543 return(bcd2bin(rtcin(port)));
546 static u_int
547 calibrate_clocks(void)
549 u_int64_t old_tsc;
550 u_int tot_count;
551 sysclock_t count, prev_count;
552 int sec, start_sec, timeout;
554 if (bootverbose)
555 kprintf("Calibrating clock(s) ...\n");
556 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
557 goto fail;
558 timeout = 100000000;
560 /* Read the mc146818A seconds counter. */
561 for (;;) {
562 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP)) {
563 sec = rtcin(RTC_SEC);
564 break;
566 if (--timeout == 0)
567 goto fail;
570 /* Wait for the mC146818A seconds counter to change. */
571 start_sec = sec;
572 for (;;) {
573 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP)) {
574 sec = rtcin(RTC_SEC);
575 if (sec != start_sec)
576 break;
578 if (--timeout == 0)
579 goto fail;
582 /* Start keeping track of the i8254 counter. */
583 prev_count = sys_cputimer->count();
584 tot_count = 0;
586 if (tsc_present)
587 old_tsc = rdtsc();
588 else
589 old_tsc = 0; /* shut up gcc */
592 * Wait for the mc146818A seconds counter to change. Read the i8254
593 * counter for each iteration since this is convenient and only
594 * costs a few usec of inaccuracy. The timing of the final reads
595 * of the counters almost matches the timing of the initial reads,
596 * so the main cause of inaccuracy is the varying latency from
597 * inside getit() or rtcin(RTC_STATUSA) to the beginning of the
598 * rtcin(RTC_SEC) that returns a changed seconds count. The
599 * maximum inaccuracy from this cause is < 10 usec on 486's.
601 start_sec = sec;
602 for (;;) {
603 if (!(rtcin(RTC_STATUSA) & RTCSA_TUP))
604 sec = rtcin(RTC_SEC);
605 count = sys_cputimer->count();
606 tot_count += (int)(count - prev_count);
607 prev_count = count;
608 if (sec != start_sec)
609 break;
610 if (--timeout == 0)
611 goto fail;
615 * Read the cpu cycle counter. The timing considerations are
616 * similar to those for the i8254 clock.
618 if (tsc_present) {
619 tsc_frequency = rdtsc() - old_tsc;
620 if (bootverbose) {
621 kprintf("TSC clock: %jd Hz (Method A)\n",
622 (intmax_t)tsc_frequency);
626 kprintf("i8254 clock: %u Hz\n", tot_count);
627 return (tot_count);
629 fail:
630 kprintf("failed, using default i8254 clock of %u Hz\n",
631 i8254_cputimer.freq);
632 return (i8254_cputimer.freq);
635 static void
636 i8254_restore(void)
638 timer0_state = ACQUIRED;
640 clock_lock();
643 * Timer0 is our fine-grained variable clock interrupt
645 outb(TIMER_MODE, TIMER_SEL0 | TIMER_SWSTROBE | TIMER_16BIT);
646 outb(TIMER_CNTR0, 2); /* lsb */
647 outb(TIMER_CNTR0, 0); /* msb */
648 clock_unlock();
650 if (!i8254_nointr) {
651 cputimer_intr_register(&i8254_cputimer_intr);
652 cputimer_intr_select(&i8254_cputimer_intr, 0);
656 * Timer1 or timer2 is our free-running clock, but only if another
657 * has not been selected.
659 cputimer_register(&i8254_cputimer);
660 cputimer_select(&i8254_cputimer, 0);
663 static void
664 i8254_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
666 int which;
669 * Should we use timer 1 or timer 2 ?
671 which = 0;
672 TUNABLE_INT_FETCH("hw.i8254.walltimer", &which);
673 if (which != 1 && which != 2)
674 which = 2;
676 switch(which) {
677 case 1:
678 timer->name = "i8254_timer1";
679 timer->type = CPUTIMER_8254_SEL1;
680 i8254_walltimer_sel = TIMER_SEL1;
681 i8254_walltimer_cntr = TIMER_CNTR1;
682 timer1_state = ACQUIRED;
683 break;
684 case 2:
685 timer->name = "i8254_timer2";
686 timer->type = CPUTIMER_8254_SEL2;
687 i8254_walltimer_sel = TIMER_SEL2;
688 i8254_walltimer_cntr = TIMER_CNTR2;
689 timer2_state = ACQUIRED;
690 break;
693 timer->base = (oldclock + 0xFFFF) & ~0xFFFF;
695 clock_lock();
696 outb(TIMER_MODE, i8254_walltimer_sel | TIMER_RATEGEN | TIMER_16BIT);
697 outb(i8254_walltimer_cntr, 0); /* lsb */
698 outb(i8254_walltimer_cntr, 0); /* msb */
699 outb(IO_PPI, inb(IO_PPI) | 1); /* bit 0: enable gate, bit 1: spkr */
700 clock_unlock();
703 static void
704 i8254_cputimer_destruct(struct cputimer *timer)
706 switch(timer->type) {
707 case CPUTIMER_8254_SEL1:
708 timer1_state = RELEASED;
709 break;
710 case CPUTIMER_8254_SEL2:
711 timer2_state = RELEASED;
712 break;
713 default:
714 break;
716 timer->type = 0;
719 static void
720 rtc_restore(void)
722 /* Restore all of the RTC's "status" (actually, control) registers. */
723 writertc(RTC_STATUSB, RTCSB_24HR);
724 writertc(RTC_STATUSA, rtc_statusa);
725 writertc(RTC_STATUSB, rtc_statusb);
729 * Restore all the timers.
731 * This function is called to resynchronize our core timekeeping after a
732 * long halt, e.g. from apm_default_resume() and friends. It is also
733 * called if after a BIOS call we have detected munging of the 8254.
734 * It is necessary because cputimer_count() counter's delta may have grown
735 * too large for nanouptime() and friends to handle, or (in the case of 8254
736 * munging) might cause the SYSTIMER code to prematurely trigger.
738 void
739 timer_restore(void)
741 crit_enter();
742 i8254_restore(); /* restore timer_freq and hz */
743 rtc_restore(); /* reenable RTC interrupts */
744 crit_exit();
748 * Initialize 8254 timer 0 early so that it can be used in DELAY().
750 void
751 startrtclock(void)
753 u_int delta, freq;
756 * Can we use the TSC?
758 * NOTE: If running under qemu, probably a good idea to force the
759 * TSC because we are not likely to detect it as being
760 * invariant or mpsyncd if you don't. This will greatly
761 * reduce SMP contention.
763 if (cpu_feature & CPUID_TSC) {
764 tsc_present = 1;
765 TUNABLE_INT_FETCH("hw.tsc_cputimer_force", &tsc_invariant);
767 if ((cpu_vendor_id == CPU_VENDOR_INTEL ||
768 cpu_vendor_id == CPU_VENDOR_AMD) &&
769 cpu_exthigh >= 0x80000007) {
770 u_int regs[4];
772 do_cpuid(0x80000007, regs);
773 if (regs[3] & 0x100)
774 tsc_invariant = 1;
776 } else {
777 tsc_present = 0;
781 * Initial RTC state, don't do anything unexpected
783 writertc(RTC_STATUSA, rtc_statusa);
784 writertc(RTC_STATUSB, RTCSB_24HR);
787 * Set the 8254 timer0 in TIMER_SWSTROBE mode and cause it to
788 * generate an interrupt, which we will ignore for now.
790 * Set the 8254 timer1 in TIMER_RATEGEN mode and load 0x0000
791 * (so it counts a full 2^16 and repeats). We will use this timer
792 * for our counting.
794 i8254_restore();
795 freq = calibrate_clocks();
796 #ifdef CLK_CALIBRATION_LOOP
797 if (bootverbose) {
798 int c;
800 cnpoll(TRUE);
801 kprintf("Press a key on the console to "
802 "abort clock calibration\n");
803 while ((c = cncheckc()) == -1 || c == NOKEY)
804 calibrate_clocks();
805 cnpoll(FALSE);
807 #endif
810 * Use the calibrated i8254 frequency if it seems reasonable.
811 * Otherwise use the default, and don't use the calibrated i586
812 * frequency.
814 delta = freq > i8254_cputimer.freq ?
815 freq - i8254_cputimer.freq : i8254_cputimer.freq - freq;
816 if (delta < i8254_cputimer.freq / 100) {
817 #ifndef CLK_USE_I8254_CALIBRATION
818 if (bootverbose)
819 kprintf(
820 "CLK_USE_I8254_CALIBRATION not specified - using default frequency\n");
821 freq = i8254_cputimer.freq;
822 #endif
824 * NOTE:
825 * Interrupt timer's freq must be adjusted
826 * before we change the cuptimer's frequency.
828 i8254_cputimer_intr.freq = freq;
829 cputimer_set_frequency(&i8254_cputimer, freq);
830 } else {
831 if (bootverbose)
832 kprintf(
833 "%d Hz differs from default of %d Hz by more than 1%%\n",
834 freq, i8254_cputimer.freq);
835 tsc_frequency = 0;
838 #ifndef CLK_USE_TSC_CALIBRATION
839 if (tsc_frequency != 0) {
840 if (bootverbose)
841 kprintf(
842 "CLK_USE_TSC_CALIBRATION not specified - using old calibration method\n");
843 tsc_frequency = 0;
845 #endif
846 if (tsc_present && tsc_frequency == 0) {
848 * Calibration of the i586 clock relative to the mc146818A
849 * clock failed. Do a less accurate calibration relative
850 * to the i8254 clock.
852 u_int64_t old_tsc = rdtsc();
854 DELAY(1000000);
855 tsc_frequency = rdtsc() - old_tsc;
856 #ifdef CLK_USE_TSC_CALIBRATION
857 if (bootverbose) {
858 kprintf("TSC clock: %jd Hz (Method B)\n",
859 (intmax_t)tsc_frequency);
861 #endif
864 if (tsc_present) {
865 kprintf("TSC%s clock: %jd Hz\n",
866 tsc_invariant ? " invariant" : "",
867 (intmax_t)tsc_frequency);
870 EVENTHANDLER_REGISTER(shutdown_post_sync, resettodr_on_shutdown, NULL, SHUTDOWN_PRI_LAST);
874 * Sync the time of day back to the RTC on shutdown, but only if
875 * we have already loaded it and have not crashed.
877 static void
878 resettodr_on_shutdown(void *arg __unused)
880 if (rtc_loaded && panicstr == NULL) {
881 resettodr();
886 * Initialize the time of day register, based on the time base which is, e.g.
887 * from a filesystem.
889 void
890 inittodr(time_t base)
892 unsigned long sec, days;
893 int year, month;
894 int y, m;
895 struct timespec ts;
897 if (base) {
898 ts.tv_sec = base;
899 ts.tv_nsec = 0;
900 set_timeofday(&ts);
903 /* Look if we have a RTC present and the time is valid */
904 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR))
905 goto wrong_time;
907 /* wait for time update to complete */
908 /* If RTCSA_TUP is zero, we have at least 244us before next update */
909 crit_enter();
910 while (rtcin(RTC_STATUSA) & RTCSA_TUP) {
911 crit_exit();
912 crit_enter();
915 days = 0;
916 #ifdef USE_RTC_CENTURY
917 year = readrtc(RTC_YEAR) + readrtc(RTC_CENTURY) * 100;
918 #else
919 year = readrtc(RTC_YEAR) + 1900;
920 if (year < 1970)
921 year += 100;
922 #endif
923 if (year < 1970) {
924 crit_exit();
925 goto wrong_time;
927 month = readrtc(RTC_MONTH);
928 for (m = 1; m < month; m++)
929 days += daysinmonth[m-1];
930 if ((month > 2) && LEAPYEAR(year))
931 days ++;
932 days += readrtc(RTC_DAY) - 1;
933 for (y = 1970; y < year; y++)
934 days += DAYSPERYEAR + LEAPYEAR(y);
935 sec = ((( days * 24 +
936 readrtc(RTC_HRS)) * 60 +
937 readrtc(RTC_MIN)) * 60 +
938 readrtc(RTC_SEC));
939 /* sec now contains the number of seconds, since Jan 1 1970,
940 in the local time zone */
942 sec += tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
944 y = (int)(time_second - sec);
945 if (y <= -2 || y >= 2) {
946 /* badly off, adjust it */
947 ts.tv_sec = sec;
948 ts.tv_nsec = 0;
949 set_timeofday(&ts);
951 rtc_loaded = 1;
952 crit_exit();
953 return;
955 wrong_time:
956 kprintf("Invalid time in real time clock.\n");
957 kprintf("Check and reset the date immediately!\n");
961 * Write system time back to RTC
963 void
964 resettodr(void)
966 struct timeval tv;
967 unsigned long tm;
968 int m;
969 int y;
971 if (disable_rtc_set)
972 return;
974 microtime(&tv);
975 tm = tv.tv_sec;
977 crit_enter();
978 /* Disable RTC updates and interrupts. */
979 writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
981 /* Calculate local time to put in RTC */
983 tm -= tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
985 writertc(RTC_SEC, bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */
986 writertc(RTC_MIN, bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */
987 writertc(RTC_HRS, bin2bcd(tm%24)); tm /= 24; /* Write back Hours */
989 /* We have now the days since 01-01-1970 in tm */
990 writertc(RTC_WDAY, (tm+4)%7); /* Write back Weekday */
991 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y);
992 tm >= m;
993 y++, m = DAYSPERYEAR + LEAPYEAR(y))
994 tm -= m;
996 /* Now we have the years in y and the day-of-the-year in tm */
997 writertc(RTC_YEAR, bin2bcd(y%100)); /* Write back Year */
998 #ifdef USE_RTC_CENTURY
999 writertc(RTC_CENTURY, bin2bcd(y/100)); /* ... and Century */
1000 #endif
1001 for (m = 0; ; m++) {
1002 int ml;
1004 ml = daysinmonth[m];
1005 if (m == 1 && LEAPYEAR(y))
1006 ml++;
1007 if (tm < ml)
1008 break;
1009 tm -= ml;
1012 writertc(RTC_MONTH, bin2bcd(m + 1)); /* Write back Month */
1013 writertc(RTC_DAY, bin2bcd(tm + 1)); /* Write back Month Day */
1015 /* Reenable RTC updates and interrupts. */
1016 writertc(RTC_STATUSB, rtc_statusb);
1017 crit_exit();
1020 static int
1021 i8254_ioapic_trial(int irq, struct cputimer_intr *cti)
1023 sysclock_t base;
1024 long lastcnt;
1027 * Following code assumes the 8254 is the cpu timer,
1028 * so make sure it is.
1030 KKASSERT(sys_cputimer == &i8254_cputimer);
1031 KKASSERT(cti == &i8254_cputimer_intr);
1033 lastcnt = get_interrupt_counter(irq, mycpuid);
1036 * Force an 8254 Timer0 interrupt and wait 1/100s for
1037 * it to happen, then see if we got it.
1039 kprintf("IOAPIC: testing 8254 interrupt delivery\n");
1041 i8254_intr_reload(cti, 2);
1042 base = sys_cputimer->count();
1043 while (sys_cputimer->count() - base < sys_cputimer->freq / 100)
1044 ; /* nothing */
1046 if (get_interrupt_counter(irq, mycpuid) - lastcnt == 0)
1047 return ENOENT;
1048 return 0;
1052 * Start both clocks running. DragonFly note: the stat clock is no longer
1053 * used. Instead, 8254 based systimers are used for all major clock
1054 * interrupts.
1056 static void
1057 i8254_intr_initclock(struct cputimer_intr *cti, boolean_t selected)
1059 void *clkdesc = NULL;
1060 int irq = 0, mixed_mode = 0, error;
1062 KKASSERT(mycpuid == 0);
1063 callout_init_mp(&sysbeepstop_ch);
1065 if (!selected && i8254_intr_disable)
1066 goto nointr;
1069 * The stat interrupt mask is different without the
1070 * statistics clock. Also, don't set the interrupt
1071 * flag which would normally cause the RTC to generate
1072 * interrupts.
1074 rtc_statusb = RTCSB_24HR;
1076 /* Finish initializing 8254 timer 0. */
1077 if (ioapic_enable) {
1078 irq = machintr_legacy_intr_find(0, INTR_TRIGGER_EDGE,
1079 INTR_POLARITY_HIGH);
1080 if (irq < 0) {
1081 mixed_mode_setup:
1082 error = ioapic_conf_legacy_extint(0);
1083 if (!error) {
1084 irq = machintr_legacy_intr_find(0,
1085 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1086 if (irq < 0)
1087 error = ENOENT;
1090 if (error) {
1091 if (!selected) {
1092 kprintf("IOAPIC: setup mixed mode for "
1093 "irq 0 failed: %d\n", error);
1094 goto nointr;
1095 } else {
1096 panic("IOAPIC: setup mixed mode for "
1097 "irq 0 failed: %d\n", error);
1100 mixed_mode = 1;
1102 clkdesc = register_int(irq, clkintr, NULL, "clk",
1103 NULL,
1104 INTR_EXCL | INTR_CLOCK |
1105 INTR_NOPOLL | INTR_MPSAFE |
1106 INTR_NOENTROPY, 0);
1107 } else {
1108 register_int(0, clkintr, NULL, "clk", NULL,
1109 INTR_EXCL | INTR_CLOCK |
1110 INTR_NOPOLL | INTR_MPSAFE |
1111 INTR_NOENTROPY, 0);
1114 /* Initialize RTC. */
1115 writertc(RTC_STATUSA, rtc_statusa);
1116 writertc(RTC_STATUSB, RTCSB_24HR);
1118 if (ioapic_enable) {
1119 error = i8254_ioapic_trial(irq, cti);
1120 if (error) {
1121 if (mixed_mode) {
1122 if (!selected) {
1123 kprintf("IOAPIC: mixed mode for irq %d "
1124 "trial failed: %d\n",
1125 irq, error);
1126 goto nointr;
1127 } else {
1128 panic("IOAPIC: mixed mode for irq %d "
1129 "trial failed: %d\n", irq, error);
1131 } else {
1132 kprintf("IOAPIC: warning 8254 is not connected "
1133 "to the correct pin, try mixed mode\n");
1134 unregister_int(clkdesc, 0);
1135 goto mixed_mode_setup;
1139 return;
1141 nointr:
1142 i8254_nointr = 1; /* don't try to register again */
1143 cputimer_intr_deregister(cti);
1146 void
1147 setstatclockrate(int newhz)
1149 if (newhz == RTC_PROFRATE)
1150 rtc_statusa = RTCSA_DIVIDER | RTCSA_PROF;
1151 else
1152 rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
1153 writertc(RTC_STATUSA, rtc_statusa);
1156 #if 0
1157 static unsigned
1158 tsc_get_timecount(struct timecounter *tc)
1160 return (rdtsc());
1162 #endif
1164 #ifdef KERN_TIMESTAMP
1165 #define KERN_TIMESTAMP_SIZE 16384
1166 static u_long tsc[KERN_TIMESTAMP_SIZE] ;
1167 SYSCTL_OPAQUE(_debug, OID_AUTO, timestamp, CTLFLAG_RD, tsc,
1168 sizeof(tsc), "LU", "Kernel timestamps");
1169 void
1170 _TSTMP(u_int32_t x)
1172 static int i;
1174 tsc[i] = (u_int32_t)rdtsc();
1175 tsc[i+1] = x;
1176 i = i + 2;
1177 if (i >= KERN_TIMESTAMP_SIZE)
1178 i = 0;
1179 tsc[i] = 0; /* mark last entry */
1181 #endif /* KERN_TIMESTAMP */
1187 static int
1188 hw_i8254_timestamp(SYSCTL_HANDLER_ARGS)
1190 sysclock_t count;
1191 uint64_t tscval;
1192 char buf[32];
1194 crit_enter();
1195 if (sys_cputimer == &i8254_cputimer)
1196 count = sys_cputimer->count();
1197 else
1198 count = 0;
1199 if (tsc_present)
1200 tscval = rdtsc();
1201 else
1202 tscval = 0;
1203 crit_exit();
1204 ksnprintf(buf, sizeof(buf), "%08x %016llx", count, (long long)tscval);
1205 return(SYSCTL_OUT(req, buf, strlen(buf) + 1));
1208 struct tsc_mpsync_arg {
1209 volatile uint64_t tsc_target;
1210 volatile int tsc_mpsync;
1213 struct tsc_mpsync_thr {
1214 volatile int tsc_done_cnt;
1215 volatile int tsc_mpsync_cnt;
1218 static void
1219 tsc_mpsync_test_remote(void *xarg)
1221 struct tsc_mpsync_arg *arg = xarg;
1222 uint64_t tsc;
1224 tsc = rdtsc_ordered();
1225 if (tsc < arg->tsc_target)
1226 arg->tsc_mpsync = 0;
1229 static void
1230 tsc_mpsync_test_loop(struct tsc_mpsync_arg *arg)
1232 struct globaldata *gd = mycpu;
1233 uint64_t test_end, test_begin;
1234 u_int i;
1236 if (bootverbose) {
1237 kprintf("cpu%d: TSC testing MP synchronization ...\n",
1238 gd->gd_cpuid);
1241 test_begin = rdtsc_ordered();
1242 /* Run test for 100ms */
1243 test_end = test_begin + (tsc_frequency / 10);
1245 arg->tsc_mpsync = 1;
1246 arg->tsc_target = test_begin;
1248 #define TSC_TEST_TRYMAX 1000000 /* Make sure we could stop */
1249 #define TSC_TEST_TRYMIN 50000
1251 for (i = 0; i < TSC_TEST_TRYMAX; ++i) {
1252 struct lwkt_cpusync cs;
1254 crit_enter();
1255 lwkt_cpusync_init(&cs, gd->gd_other_cpus,
1256 tsc_mpsync_test_remote, arg);
1257 lwkt_cpusync_interlock(&cs);
1258 arg->tsc_target = rdtsc_ordered();
1259 cpu_mfence();
1260 lwkt_cpusync_deinterlock(&cs);
1261 crit_exit();
1263 if (!arg->tsc_mpsync) {
1264 kprintf("cpu%d: TSC is not MP synchronized @%u\n",
1265 gd->gd_cpuid, i);
1266 break;
1268 if (arg->tsc_target > test_end && i >= TSC_TEST_TRYMIN)
1269 break;
1272 #undef TSC_TEST_TRYMIN
1273 #undef TSC_TEST_TRYMAX
1275 if (arg->tsc_target == test_begin) {
1276 kprintf("cpu%d: TSC does not tick?!\n", gd->gd_cpuid);
1277 /* XXX disable TSC? */
1278 tsc_invariant = 0;
1279 arg->tsc_mpsync = 0;
1280 return;
1283 if (arg->tsc_mpsync && bootverbose) {
1284 kprintf("cpu%d: TSC is MP synchronized after %u tries\n",
1285 gd->gd_cpuid, i);
1289 static void
1290 tsc_mpsync_ap_thread(void *xthr)
1292 struct tsc_mpsync_thr *thr = xthr;
1293 struct tsc_mpsync_arg arg;
1295 tsc_mpsync_test_loop(&arg);
1296 if (arg.tsc_mpsync) {
1297 atomic_add_int(&thr->tsc_mpsync_cnt, 1);
1298 cpu_sfence();
1300 atomic_add_int(&thr->tsc_done_cnt, 1);
1302 lwkt_exit();
1305 static void
1306 tsc_mpsync_test(void)
1308 struct tsc_mpsync_arg arg;
1310 if (!tsc_invariant) {
1311 /* Not even invariant TSC */
1312 return;
1315 if (ncpus == 1) {
1316 /* Only one CPU */
1317 tsc_mpsync = 1;
1318 return;
1322 * Forcing can be used w/qemu to reduce contention
1324 TUNABLE_INT_FETCH("hw.tsc_cputimer_force", &tsc_mpsync);
1325 if (tsc_mpsync) {
1326 kprintf("TSC as cputimer forced\n");
1327 return;
1330 if (cpu_vendor_id != CPU_VENDOR_INTEL) {
1331 /* XXX only Intel works */
1332 return;
1335 kprintf("TSC testing MP synchronization ...\n");
1337 tsc_mpsync_test_loop(&arg);
1338 if (arg.tsc_mpsync) {
1339 struct tsc_mpsync_thr thr;
1340 int cpu;
1343 * Test TSC MP synchronization on APs.
1346 thr.tsc_done_cnt = 1;
1347 thr.tsc_mpsync_cnt = 1;
1349 for (cpu = 0; cpu < ncpus; ++cpu) {
1350 if (cpu == mycpuid)
1351 continue;
1353 lwkt_create(tsc_mpsync_ap_thread, &thr, NULL,
1354 NULL, 0, cpu, "tsc mpsync %d", cpu);
1357 while (thr.tsc_done_cnt != ncpus) {
1358 cpu_pause();
1359 cpu_lfence();
1361 if (thr.tsc_mpsync_cnt == ncpus)
1362 tsc_mpsync = 1;
1365 if (tsc_mpsync)
1366 kprintf("TSC is MP synchronized\n");
1367 else
1368 kprintf("TSC is not MP synchronized\n");
1370 SYSINIT(tsc_mpsync, SI_BOOT2_FINISH_SMP, SI_ORDER_ANY, tsc_mpsync_test, NULL);
1372 #define TSC_CPUTIMER_FREQMAX 128000000 /* 128Mhz */
1374 static int tsc_cputimer_shift;
1376 static void
1377 tsc_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
1379 timer->base = 0;
1380 timer->base = oldclock - timer->count();
1383 static __inline sysclock_t
1384 tsc_cputimer_count(void)
1386 uint64_t tsc;
1388 tsc = rdtsc();
1389 tsc >>= tsc_cputimer_shift;
1391 return (tsc + tsc_cputimer.base);
1394 static sysclock_t
1395 tsc_cputimer_count_lfence(void)
1397 cpu_lfence();
1398 return tsc_cputimer_count();
1401 static sysclock_t
1402 tsc_cputimer_count_mfence(void)
1404 cpu_mfence();
1405 return tsc_cputimer_count();
1408 static void
1409 tsc_cputimer_register(void)
1411 uint64_t freq;
1412 int enable = 1;
1414 if (!tsc_mpsync)
1415 return;
1417 TUNABLE_INT_FETCH("hw.tsc_cputimer_enable", &enable);
1418 if (!enable)
1419 return;
1421 freq = tsc_frequency;
1422 while (freq > TSC_CPUTIMER_FREQMAX) {
1423 freq >>= 1;
1424 ++tsc_cputimer_shift;
1426 kprintf("TSC: cputimer freq %ju, shift %d\n",
1427 (uintmax_t)freq, tsc_cputimer_shift);
1429 tsc_cputimer.freq = freq;
1431 if (cpu_vendor_id == CPU_VENDOR_INTEL)
1432 tsc_cputimer.count = tsc_cputimer_count_lfence;
1433 else
1434 tsc_cputimer.count = tsc_cputimer_count_mfence; /* safe bet */
1436 cputimer_register(&tsc_cputimer);
1437 cputimer_select(&tsc_cputimer, 0);
1439 SYSINIT(tsc_cputimer_reg, SI_BOOT2_POST_SMP, SI_ORDER_FIRST,
1440 tsc_cputimer_register, NULL);
1442 SYSCTL_NODE(_hw, OID_AUTO, i8254, CTLFLAG_RW, 0, "I8254");
1443 SYSCTL_UINT(_hw_i8254, OID_AUTO, freq, CTLFLAG_RD, &i8254_cputimer.freq, 0,
1444 "frequency");
1445 SYSCTL_PROC(_hw_i8254, OID_AUTO, timestamp, CTLTYPE_STRING|CTLFLAG_RD,
1446 0, 0, hw_i8254_timestamp, "A", "");
1448 SYSCTL_INT(_hw, OID_AUTO, tsc_present, CTLFLAG_RD,
1449 &tsc_present, 0, "TSC Available");
1450 SYSCTL_INT(_hw, OID_AUTO, tsc_invariant, CTLFLAG_RD,
1451 &tsc_invariant, 0, "Invariant TSC");
1452 SYSCTL_INT(_hw, OID_AUTO, tsc_mpsync, CTLFLAG_RD,
1453 &tsc_mpsync, 0, "TSC is synchronized across CPUs");
1454 SYSCTL_QUAD(_hw, OID_AUTO, tsc_frequency, CTLFLAG_RD,
1455 &tsc_frequency, 0, "TSC Frequency");