2 * Copyright (c) 1998-2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
31 #include "opt_clock.h"
33 #include <sys/param.h>
36 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/sysctl.h>
41 #include <sys/timetc.h>
42 #include <sys/kernel.h>
43 #include <sys/power.h>
46 #include <machine/clock.h>
47 #include <machine/cputypes.h>
48 #include <machine/md_var.h>
49 #include <machine/specialreg.h>
50 #include <x86/vmware.h>
52 #include "cpufreq_if.h"
58 static eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag;
60 SYSCTL_INT(_kern_timecounter, OID_AUTO, invariant_tsc, CTLFLAG_RDTUN,
61 &tsc_is_invariant, 0, "Indicates whether the TSC is P-state invariant");
62 TUNABLE_INT("kern.timecounter.invariant_tsc", &tsc_is_invariant);
66 SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0,
67 "Indicates whether the TSC is safe to use in SMP mode");
68 TUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
70 int smp_tsc_adjust = 0;
71 SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc_adjust, CTLFLAG_RDTUN,
72 &smp_tsc_adjust, 0, "Try to adjust TSC on APs to match BSP");
73 TUNABLE_INT("kern.timecounter.smp_tsc_adjust", &smp_tsc_adjust);
76 static int tsc_shift = 1;
77 SYSCTL_INT(_kern_timecounter, OID_AUTO, tsc_shift, CTLFLAG_RDTUN,
78 &tsc_shift, 0, "Shift to pre-apply for the maximum TSC frequency");
79 TUNABLE_INT("kern.timecounter.tsc_shift", &tsc_shift);
81 static int tsc_disabled;
82 SYSCTL_INT(_machdep, OID_AUTO, disable_tsc, CTLFLAG_RDTUN, &tsc_disabled, 0,
83 "Disable x86 Time Stamp Counter");
84 TUNABLE_INT("machdep.disable_tsc", &tsc_disabled);
86 static int tsc_skip_calibration;
87 SYSCTL_INT(_machdep, OID_AUTO, disable_tsc_calibration, CTLFLAG_RDTUN,
88 &tsc_skip_calibration, 0, "Disable TSC frequency calibration");
89 TUNABLE_INT("machdep.disable_tsc_calibration", &tsc_skip_calibration);
91 static void tsc_freq_changed(void *arg, const struct cf_level *level,
93 static void tsc_freq_changing(void *arg, const struct cf_level *level,
95 static unsigned tsc_get_timecount(struct timecounter *tc);
96 static inline unsigned tsc_get_timecount_low(struct timecounter *tc);
97 static unsigned tsc_get_timecount_lfence(struct timecounter *tc);
98 static unsigned tsc_get_timecount_low_lfence(struct timecounter *tc);
99 static unsigned tsc_get_timecount_mfence(struct timecounter *tc);
100 static unsigned tsc_get_timecount_low_mfence(struct timecounter *tc);
101 static void tsc_levels_changed(void *arg, int unit);
103 static struct timecounter tsc_timecounter = {
104 tsc_get_timecount, /* get_timecount */
106 ~0u, /* counter_mask */
109 800, /* quality (adjusted in code) */
113 tsc_freq_vmware(void)
117 if (hv_high >= 0x40000010) {
118 do_cpuid(0x40000010, regs);
119 tsc_freq = regs[0] * 1000;
121 vmware_hvcall(VMW_HVCMD_GETHZ, regs);
122 if (regs[1] != UINT_MAX)
123 tsc_freq = regs[0] | ((uint64_t)regs[1] << 32);
125 tsc_is_invariant = 1;
138 * Intel Processor Identification and the CPUID Instruction
139 * Application Note 485.
140 * http://www.intel.com/assets/pdf/appnote/241618.pdf
142 if (cpu_exthigh >= 0x80000004) {
144 for (i = 0x80000002; i < 0x80000005; i++) {
146 memcpy(p, regs, sizeof(regs));
150 for (i = 0; i < sizeof(brand) - 1; i++)
151 if (brand[i] == 'H' && brand[i + 1] == 'z')
168 #define C2D(c) ((c) - '0')
170 freq = C2D(p[0]) * 1000;
171 freq += C2D(p[2]) * 100;
172 freq += C2D(p[3]) * 10;
175 freq = C2D(p[0]) * 1000;
176 freq += C2D(p[1]) * 100;
177 freq += C2D(p[2]) * 10;
195 if ((regs[2] & CPUID_PERF_STAT) != 0) {
197 * XXX Some emulators expose host CPUID without actual
198 * support for these MSRs. We must test whether they
204 if (rdmsr(MSR_MPERF) > 0 && rdmsr(MSR_APERF) > 0)
209 if (vm_guest == VM_GUEST_VMWARE) {
214 switch (cpu_vendor_id) {
216 if ((amd_pminfo & AMDPM_TSC_INVARIANT) != 0 ||
217 (vm_guest == VM_GUEST_NO &&
218 CPUID_TO_FAMILY(cpu_id) >= 0x10))
219 tsc_is_invariant = 1;
220 if (cpu_feature & CPUID_SSE2) {
221 tsc_timecounter.tc_get_timecount =
222 tsc_get_timecount_mfence;
225 case CPU_VENDOR_INTEL:
226 if ((amd_pminfo & AMDPM_TSC_INVARIANT) != 0 ||
227 (vm_guest == VM_GUEST_NO &&
228 ((CPUID_TO_FAMILY(cpu_id) == 0x6 &&
229 CPUID_TO_MODEL(cpu_id) >= 0xe) ||
230 (CPUID_TO_FAMILY(cpu_id) == 0xf &&
231 CPUID_TO_MODEL(cpu_id) >= 0x3))))
232 tsc_is_invariant = 1;
233 if (cpu_feature & CPUID_SSE2) {
234 tsc_timecounter.tc_get_timecount =
235 tsc_get_timecount_lfence;
238 case CPU_VENDOR_CENTAUR:
239 if (vm_guest == VM_GUEST_NO &&
240 CPUID_TO_FAMILY(cpu_id) == 0x6 &&
241 CPUID_TO_MODEL(cpu_id) >= 0xf &&
242 (rdmsr(0x1203) & 0x100000000ULL) == 0)
243 tsc_is_invariant = 1;
244 if (cpu_feature & CPUID_SSE2) {
245 tsc_timecounter.tc_get_timecount =
246 tsc_get_timecount_lfence;
251 if (tsc_skip_calibration) {
252 if (cpu_vendor_id == CPU_VENDOR_INTEL)
258 printf("Calibrating TSC clock ... ");
262 tsc_freq = tsc2 - tsc1;
264 printf("TSC clock: %ju Hz\n", (intmax_t)tsc_freq);
271 if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled)
275 /* The TSC is known to be broken on certain CPUs. */
276 switch (cpu_vendor_id) {
278 switch (cpu_id & 0xFF0) {
284 case CPU_VENDOR_CENTAUR:
285 switch (cpu_id & 0xff0) {
288 * http://www.centtech.com/c6_data_sheet.pdf
290 * I-12 RDTSC may return incoherent values in EDX:EAX
291 * I-13 RDTSC hangs when certain event counters are used
297 switch (cpu_id & 0xff0) {
299 if ((cpu_id & CPUID_STEPPING) == 0)
310 * Inform CPU accounting about our boot-time clock rate. This will
311 * be updated if someone loads a cpufreq driver after boot that
312 * discovers a new max frequency.
315 set_cputicker(rdtsc, tsc_freq, !tsc_is_invariant);
317 if (tsc_is_invariant)
320 /* Register to find out about changes in CPU frequency. */
321 tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
322 tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST);
323 tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
324 tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST);
325 tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed,
326 tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY);
332 * RDTSC is not a serializing instruction, and does not drain
333 * instruction stream, so we need to drain the stream before executing
334 * it. It could be fixed by use of RDTSCP, except the instruction is
335 * not available everywhere.
337 * Use CPUID for draining in the boot-time SMP constistency test. The
338 * timecounters use MFENCE for AMD CPUs, and LFENCE for others (Intel
339 * and VIA) when SSE2 is present, and nothing on older machines which
340 * also do not issue RDTSC prematurely. There, testing for SSE2 and
341 * vendor is too cumbersome, and we learn about TSC presence from CPUID.
343 * Do not use do_cpuid(), since we do not need CPUID results, which
344 * have to be written into memory with do_cpuid().
346 #define TSC_READ(x) \
348 tsc_read_##x(void *arg) \
350 uint64_t *tsc = arg; \
351 u_int cpu = PCPU_GET(cpuid); \
353 __asm __volatile("cpuid" : : : "eax", "ebx", "ecx", "edx"); \
354 tsc[cpu * 3 + x] = rdtsc(); \
364 comp_smp_tsc(void *arg)
368 u_int cpu = PCPU_GET(cpuid);
371 size = (mp_maxid + 1) * 3;
372 for (i = 0, tsc = arg; i < N; i++, tsc += size)
376 d1 = tsc[cpu * 3 + 1] - tsc[j * 3];
377 d2 = tsc[cpu * 3 + 2] - tsc[j * 3 + 1];
378 if (d1 <= 0 || d2 <= 0) {
386 adj_smp_tsc(void *arg)
390 u_int cpu = PCPU_GET(cpuid);
391 u_int first, i, size;
398 size = (mp_maxid + 1) * 3;
399 for (i = 0, tsc = arg; i < N; i++, tsc += size) {
400 d = tsc[first * 3] - tsc[cpu * 3 + 1];
403 d = tsc[first * 3 + 1] - tsc[cpu * 3 + 2];
406 d = tsc[first * 3 + 1] - tsc[cpu * 3];
409 d = tsc[first * 3 + 2] - tsc[cpu * 3 + 1];
415 d = min / 2 + max / 2;
417 "movl $0x10, %%ecx\n\t"
419 "addl %%edi, %%eax\n\t"
420 "adcl %%esi, %%edx\n\t"
423 : "D" ((uint32_t)d), "S" ((uint32_t)(d >> 32))
424 : "ax", "cx", "dx", "cc"
431 uint64_t *data, *tsc;
434 if ((!smp_tsc && !tsc_is_invariant) || vm_guest)
436 size = (mp_maxid + 1) * 3;
437 data = malloc(sizeof(*data) * size * N, M_TEMP, M_WAITOK);
440 for (i = 0, tsc = data; i < N; i++, tsc += size)
441 smp_rendezvous(tsc_read_0, tsc_read_1, tsc_read_2, tsc);
442 smp_tsc = 1; /* XXX */
443 smp_rendezvous(smp_no_rendevous_barrier, comp_smp_tsc,
444 smp_no_rendevous_barrier, data);
445 if (!smp_tsc && adj < smp_tsc_adjust) {
447 smp_rendezvous(smp_no_rendevous_barrier, adj_smp_tsc,
448 smp_no_rendevous_barrier, data);
453 printf("SMP: %sed TSC synchronization test%s\n",
454 smp_tsc ? "pass" : "fail",
455 adj > 0 ? " after adjustment" : "");
456 if (smp_tsc && tsc_is_invariant) {
457 switch (cpu_vendor_id) {
460 * Starting with Family 15h processors, TSC clock
461 * source is in the north bridge. Check whether
462 * we have a single-socket/multi-core platform.
463 * XXX Need more work for complex cases.
465 if (CPUID_TO_FAMILY(cpu_id) < 0x15 ||
466 (amd_feature2 & AMDID2_CMP) == 0 ||
467 smp_cpus > (cpu_procinfo2 & AMDID_CMP_CORES) + 1)
470 case CPU_VENDOR_INTEL:
472 * XXX Assume Intel platforms have synchronized TSCs.
486 * The function is not called, it is provided to avoid linking failure
487 * on uniprocessor kernel.
504 if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled)
508 * Limit timecounter frequency to fit in an int and prevent it from
509 * overflowing too fast.
514 * We can not use the TSC if we support APM. Precise timekeeping
515 * on an APM'ed machine is at best a fools pursuit, since
516 * any and all of the time spent in various SMM code can't
517 * be reliably accounted for. Reading the RTC is your only
518 * source of reliable time info. The i8254 loses too, of course,
519 * but we need to have some kind of time...
520 * We don't know at this point whether APM is going to be used
521 * or not, nor when it might be activated. Play it safe.
523 if (power_pm_get_type() == POWER_PM_TYPE_APM) {
524 tsc_timecounter.tc_quality = -1000;
526 printf("TSC timecounter disabled: APM enabled.\n");
531 * We cannot use the TSC if it stops incrementing while idle.
532 * Intel CPUs without a C-state invariant TSC can stop the TSC
533 * in either C2 or C3.
535 if (cpu_deepest_sleep >= 2 && cpu_vendor_id == CPU_VENDOR_INTEL &&
536 (amd_pminfo & AMDPM_TSC_INVARIANT) == 0) {
537 tsc_timecounter.tc_quality = -1000;
538 tsc_timecounter.tc_flags |= TC_FLAGS_C2STOP;
540 printf("TSC timecounter disabled: C2/C3 may halt it.\n");
545 * We can not use the TSC in SMP mode unless the TSCs on all CPUs
546 * are synchronized. If the user is sure that the system has
547 * synchronized TSCs, set kern.timecounter.smp_tsc tunable to a
548 * non-zero value. The TSC seems unreliable in virtualized SMP
549 * environments, so it is set to a negative quality in those cases.
552 tsc_timecounter.tc_quality = test_tsc();
553 else if (tsc_is_invariant)
554 tsc_timecounter.tc_quality = 1000;
555 max_freq >>= tsc_shift;
558 for (shift = 0; shift <= 31 && (tsc_freq >> shift) > max_freq; shift++)
560 if ((cpu_feature & CPUID_SSE2) != 0 && mp_ncpus > 1) {
561 if (cpu_vendor_id == CPU_VENDOR_AMD) {
562 tsc_timecounter.tc_get_timecount = shift > 0 ?
563 tsc_get_timecount_low_mfence :
564 tsc_get_timecount_mfence;
566 tsc_timecounter.tc_get_timecount = shift > 0 ?
567 tsc_get_timecount_low_lfence :
568 tsc_get_timecount_lfence;
571 tsc_timecounter.tc_get_timecount = shift > 0 ?
572 tsc_get_timecount_low : tsc_get_timecount;
575 tsc_timecounter.tc_name = "TSC-low";
577 printf("TSC timecounter discards lower %d bit(s)\n",
581 tsc_timecounter.tc_frequency = tsc_freq >> shift;
582 tsc_timecounter.tc_priv = (void *)(intptr_t)shift;
583 tc_init(&tsc_timecounter);
586 SYSINIT(tsc_tc, SI_SUB_SMP, SI_ORDER_ANY, init_TSC_tc, NULL);
589 * When cpufreq levels change, find out about the (new) max frequency. We
590 * use this to update CPU accounting in case it got a lower estimate at boot.
593 tsc_levels_changed(void *arg, int unit)
596 struct cf_level *levels;
600 /* Only use values from the first CPU, assuming all are equal. */
604 /* Find the appropriate cpufreq device instance. */
605 cf_dev = devclass_get_device(devclass_find("cpufreq"), unit);
606 if (cf_dev == NULL) {
607 printf("tsc_levels_changed() called but no cpufreq device?\n");
611 /* Get settings from the device and find the max frequency. */
613 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
616 error = CPUFREQ_LEVELS(cf_dev, levels, &count);
617 if (error == 0 && count != 0) {
618 max_freq = (uint64_t)levels[0].total_set.freq * 1000000;
619 set_cputicker(rdtsc, max_freq, 1);
621 printf("tsc_levels_changed: no max freq found\n");
622 free(levels, M_TEMP);
626 * If the TSC timecounter is in use, veto the pending change. It may be
627 * possible in the future to handle a dynamically-changing timecounter rate.
630 tsc_freq_changing(void *arg, const struct cf_level *level, int *status)
633 if (*status != 0 || timecounter != &tsc_timecounter)
636 printf("timecounter TSC must not be in use when "
637 "changing frequencies; change denied\n");
641 /* Update TSC freq with the value indicated by the caller. */
643 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
647 /* If there was an error during the transition, don't do anything. */
648 if (tsc_disabled || status != 0)
651 /* Total setting for this level gives the new frequency in MHz. */
652 freq = (uint64_t)level->total_set.freq * 1000000;
653 atomic_store_rel_64(&tsc_freq, freq);
654 tsc_timecounter.tc_frequency =
655 freq >> (int)(intptr_t)tsc_timecounter.tc_priv;
659 sysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
664 freq = atomic_load_acq_64(&tsc_freq);
667 error = sysctl_handle_64(oidp, &freq, 0, req);
668 if (error == 0 && req->newptr != NULL) {
669 atomic_store_rel_64(&tsc_freq, freq);
670 atomic_store_rel_64(&tsc_timecounter.tc_frequency,
671 freq >> (int)(intptr_t)tsc_timecounter.tc_priv);
676 SYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_U64 | CTLFLAG_RW,
677 0, 0, sysctl_machdep_tsc_freq, "QU", "Time Stamp Counter frequency");
680 tsc_get_timecount(struct timecounter *tc __unused)
687 tsc_get_timecount_low(struct timecounter *tc)
691 __asm __volatile("rdtsc; shrd %%cl, %%edx, %0"
692 : "=a" (rv) : "c" ((int)(intptr_t)tc->tc_priv) : "edx");
697 tsc_get_timecount_lfence(struct timecounter *tc __unused)
705 tsc_get_timecount_low_lfence(struct timecounter *tc)
709 return (tsc_get_timecount_low(tc));
713 tsc_get_timecount_mfence(struct timecounter *tc __unused)
721 tsc_get_timecount_low_mfence(struct timecounter *tc)
725 return (tsc_get_timecount_low(tc));
729 cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th)
732 vdso_th->th_x86_shift = (int)(intptr_t)timecounter->tc_priv;
733 bzero(vdso_th->th_res, sizeof(vdso_th->th_res));
734 return (timecounter == &tsc_timecounter);
737 #ifdef COMPAT_FREEBSD32
739 cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
742 vdso_th32->th_x86_shift = (int)(intptr_t)timecounter->tc_priv;
743 bzero(vdso_th32->th_res, sizeof(vdso_th32->th_res));
744 return (timecounter == &tsc_timecounter);