1dd7d207dSJung-uk Kim /*- 2dd7d207dSJung-uk Kim * Copyright (c) 1998-2003 Poul-Henning Kamp 3dd7d207dSJung-uk Kim * All rights reserved. 4dd7d207dSJung-uk Kim * 5dd7d207dSJung-uk Kim * Redistribution and use in source and binary forms, with or without 6dd7d207dSJung-uk Kim * modification, are permitted provided that the following conditions 7dd7d207dSJung-uk Kim * are met: 8dd7d207dSJung-uk Kim * 1. Redistributions of source code must retain the above copyright 9dd7d207dSJung-uk Kim * notice, this list of conditions and the following disclaimer. 10dd7d207dSJung-uk Kim * 2. Redistributions in binary form must reproduce the above copyright 11dd7d207dSJung-uk Kim * notice, this list of conditions and the following disclaimer in the 12dd7d207dSJung-uk Kim * documentation and/or other materials provided with the distribution. 13dd7d207dSJung-uk Kim * 14dd7d207dSJung-uk Kim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15dd7d207dSJung-uk Kim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16dd7d207dSJung-uk Kim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17dd7d207dSJung-uk Kim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18dd7d207dSJung-uk Kim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19dd7d207dSJung-uk Kim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20dd7d207dSJung-uk Kim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21dd7d207dSJung-uk Kim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22dd7d207dSJung-uk Kim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23dd7d207dSJung-uk Kim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24dd7d207dSJung-uk Kim * SUCH DAMAGE. 25dd7d207dSJung-uk Kim */ 26dd7d207dSJung-uk Kim 27dd7d207dSJung-uk Kim #include <sys/cdefs.h> 28dd7d207dSJung-uk Kim __FBSDID("$FreeBSD$"); 29dd7d207dSJung-uk Kim 30aea81038SKonstantin Belousov #include "opt_compat.h" 31dd7d207dSJung-uk Kim #include "opt_clock.h" 32dd7d207dSJung-uk Kim 33dd7d207dSJung-uk Kim #include <sys/param.h> 34dd7d207dSJung-uk Kim #include <sys/bus.h> 35dd7d207dSJung-uk Kim #include <sys/cpu.h> 365da5812bSJung-uk Kim #include <sys/limits.h> 37dd7d207dSJung-uk Kim #include <sys/malloc.h> 38dd7d207dSJung-uk Kim #include <sys/systm.h> 39dd7d207dSJung-uk Kim #include <sys/sysctl.h> 40dd7d207dSJung-uk Kim #include <sys/time.h> 41dd7d207dSJung-uk Kim #include <sys/timetc.h> 42dd7d207dSJung-uk Kim #include <sys/kernel.h> 43dd7d207dSJung-uk Kim #include <sys/power.h> 44dd7d207dSJung-uk Kim #include <sys/smp.h> 45aea81038SKonstantin Belousov #include <sys/vdso.h> 46dd7d207dSJung-uk Kim #include <machine/clock.h> 47dd7d207dSJung-uk Kim #include <machine/cputypes.h> 48dd7d207dSJung-uk Kim #include <machine/md_var.h> 49dd7d207dSJung-uk Kim #include <machine/specialreg.h> 5001e1933dSJohn Baldwin #include <x86/vmware.h> 5116808549SKonstantin Belousov #include <dev/acpica/acpi_hpet.h> 52dd7d207dSJung-uk Kim 53dd7d207dSJung-uk Kim #include "cpufreq_if.h" 54dd7d207dSJung-uk Kim 55dd7d207dSJung-uk Kim uint64_t tsc_freq; 56dd7d207dSJung-uk Kim int tsc_is_invariant; 57155094d7SJung-uk Kim int tsc_perf_stat; 58155094d7SJung-uk Kim 59dd7d207dSJung-uk Kim static eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag; 60dd7d207dSJung-uk Kim 61dd7d207dSJung-uk Kim SYSCTL_INT(_kern_timecounter, OID_AUTO, invariant_tsc, CTLFLAG_RDTUN, 62dd7d207dSJung-uk Kim &tsc_is_invariant, 0, "Indicates whether the TSC is P-state invariant"); 63dd7d207dSJung-uk Kim 64dd7d207dSJung-uk Kim #ifdef SMP 651472b87fSNeel Natu int smp_tsc; 66dd7d207dSJung-uk Kim SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0, 67dd7d207dSJung-uk Kim "Indicates whether the TSC is safe to use in SMP mode"); 68b2c63698SAlexander Motin 69b2c63698SAlexander Motin int smp_tsc_adjust = 0; 70b2c63698SAlexander Motin SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc_adjust, CTLFLAG_RDTUN, 71b2c63698SAlexander Motin &smp_tsc_adjust, 0, "Try to adjust TSC on APs to match BSP"); 72dd7d207dSJung-uk Kim #endif 73dd7d207dSJung-uk Kim 74e7f1427dSKonstantin Belousov static int tsc_shift = 1; 75e7f1427dSKonstantin Belousov SYSCTL_INT(_kern_timecounter, OID_AUTO, tsc_shift, CTLFLAG_RDTUN, 76e7f1427dSKonstantin Belousov &tsc_shift, 0, "Shift to pre-apply for the maximum TSC frequency"); 77e7f1427dSKonstantin Belousov 7879422085SJung-uk Kim static int tsc_disabled; 7979422085SJung-uk Kim SYSCTL_INT(_machdep, OID_AUTO, disable_tsc, CTLFLAG_RDTUN, &tsc_disabled, 0, 8079422085SJung-uk Kim "Disable x86 Time Stamp Counter"); 8179422085SJung-uk Kim 82a4e4127fSJung-uk Kim static int tsc_skip_calibration; 83a4e4127fSJung-uk Kim SYSCTL_INT(_machdep, OID_AUTO, disable_tsc_calibration, CTLFLAG_RDTUN, 84a4e4127fSJung-uk Kim &tsc_skip_calibration, 0, "Disable TSC frequency calibration"); 85a4e4127fSJung-uk Kim 86dd7d207dSJung-uk Kim static void tsc_freq_changed(void *arg, const struct cf_level *level, 87dd7d207dSJung-uk Kim int status); 88dd7d207dSJung-uk Kim static void tsc_freq_changing(void *arg, const struct cf_level *level, 89dd7d207dSJung-uk Kim int *status); 90dd7d207dSJung-uk Kim static unsigned tsc_get_timecount(struct timecounter *tc); 91814124c3SKonstantin Belousov static inline unsigned tsc_get_timecount_low(struct timecounter *tc); 92814124c3SKonstantin Belousov static unsigned tsc_get_timecount_lfence(struct timecounter *tc); 93814124c3SKonstantin Belousov static unsigned tsc_get_timecount_low_lfence(struct timecounter *tc); 94814124c3SKonstantin Belousov static unsigned tsc_get_timecount_mfence(struct timecounter *tc); 95814124c3SKonstantin Belousov static unsigned tsc_get_timecount_low_mfence(struct timecounter *tc); 96dd7d207dSJung-uk Kim static void tsc_levels_changed(void *arg, int unit); 9716808549SKonstantin Belousov static uint32_t x86_tsc_vdso_timehands(struct vdso_timehands *vdso_th, 9816808549SKonstantin Belousov struct timecounter *tc); 9916808549SKonstantin Belousov #ifdef COMPAT_FREEBSD32 10016808549SKonstantin Belousov static uint32_t x86_tsc_vdso_timehands32(struct vdso_timehands32 *vdso_th32, 10116808549SKonstantin Belousov struct timecounter *tc); 10216808549SKonstantin Belousov #endif 103dd7d207dSJung-uk Kim 104dd7d207dSJung-uk Kim static struct timecounter tsc_timecounter = { 10516808549SKonstantin Belousov .tc_get_timecount = tsc_get_timecount, 10616808549SKonstantin Belousov .tc_counter_mask = ~0u, 10716808549SKonstantin Belousov .tc_name = "TSC", 10816808549SKonstantin Belousov .tc_quality = 800, /* adjusted in code */ 10916808549SKonstantin Belousov .tc_fill_vdso_timehands = x86_tsc_vdso_timehands, 11016808549SKonstantin Belousov #ifdef COMPAT_FREEBSD32 11116808549SKonstantin Belousov .tc_fill_vdso_timehands32 = x86_tsc_vdso_timehands32, 11216808549SKonstantin Belousov #endif 113dd7d207dSJung-uk Kim }; 114dd7d207dSJung-uk Kim 11501e1933dSJohn Baldwin static void 1165da5812bSJung-uk Kim tsc_freq_vmware(void) 1175da5812bSJung-uk Kim { 1185da5812bSJung-uk Kim u_int regs[4]; 1195da5812bSJung-uk Kim 1205da5812bSJung-uk Kim if (hv_high >= 0x40000010) { 1215da5812bSJung-uk Kim do_cpuid(0x40000010, regs); 1225da5812bSJung-uk Kim tsc_freq = regs[0] * 1000; 1235da5812bSJung-uk Kim } else { 1245da5812bSJung-uk Kim vmware_hvcall(VMW_HVCMD_GETHZ, regs); 1255da5812bSJung-uk Kim if (regs[1] != UINT_MAX) 1265da5812bSJung-uk Kim tsc_freq = regs[0] | ((uint64_t)regs[1] << 32); 1275da5812bSJung-uk Kim } 1285da5812bSJung-uk Kim tsc_is_invariant = 1; 1295da5812bSJung-uk Kim } 1305da5812bSJung-uk Kim 131a4e4127fSJung-uk Kim static void 132a4e4127fSJung-uk Kim tsc_freq_intel(void) 133dd7d207dSJung-uk Kim { 134a4e4127fSJung-uk Kim char brand[48]; 135a4e4127fSJung-uk Kim u_int regs[4]; 136a4e4127fSJung-uk Kim uint64_t freq; 137a4e4127fSJung-uk Kim char *p; 138a4e4127fSJung-uk Kim u_int i; 139dd7d207dSJung-uk Kim 140a4e4127fSJung-uk Kim /* 141a4e4127fSJung-uk Kim * Intel Processor Identification and the CPUID Instruction 142a4e4127fSJung-uk Kim * Application Note 485. 143a4e4127fSJung-uk Kim * http://www.intel.com/assets/pdf/appnote/241618.pdf 144a4e4127fSJung-uk Kim */ 145a4e4127fSJung-uk Kim if (cpu_exthigh >= 0x80000004) { 146a4e4127fSJung-uk Kim p = brand; 147a4e4127fSJung-uk Kim for (i = 0x80000002; i < 0x80000005; i++) { 148a4e4127fSJung-uk Kim do_cpuid(i, regs); 149a4e4127fSJung-uk Kim memcpy(p, regs, sizeof(regs)); 150a4e4127fSJung-uk Kim p += sizeof(regs); 151a4e4127fSJung-uk Kim } 152a4e4127fSJung-uk Kim p = NULL; 153a4e4127fSJung-uk Kim for (i = 0; i < sizeof(brand) - 1; i++) 154a4e4127fSJung-uk Kim if (brand[i] == 'H' && brand[i + 1] == 'z') 155a4e4127fSJung-uk Kim p = brand + i; 156a4e4127fSJung-uk Kim if (p != NULL) { 157a4e4127fSJung-uk Kim p -= 5; 158a4e4127fSJung-uk Kim switch (p[4]) { 159a4e4127fSJung-uk Kim case 'M': 160a4e4127fSJung-uk Kim i = 1; 161a4e4127fSJung-uk Kim break; 162a4e4127fSJung-uk Kim case 'G': 163a4e4127fSJung-uk Kim i = 1000; 164a4e4127fSJung-uk Kim break; 165a4e4127fSJung-uk Kim case 'T': 166a4e4127fSJung-uk Kim i = 1000000; 167a4e4127fSJung-uk Kim break; 168a4e4127fSJung-uk Kim default: 169dd7d207dSJung-uk Kim return; 170a4e4127fSJung-uk Kim } 171a4e4127fSJung-uk Kim #define C2D(c) ((c) - '0') 172a4e4127fSJung-uk Kim if (p[1] == '.') { 173a4e4127fSJung-uk Kim freq = C2D(p[0]) * 1000; 174a4e4127fSJung-uk Kim freq += C2D(p[2]) * 100; 175a4e4127fSJung-uk Kim freq += C2D(p[3]) * 10; 176a4e4127fSJung-uk Kim freq *= i * 1000; 177a4e4127fSJung-uk Kim } else { 178a4e4127fSJung-uk Kim freq = C2D(p[0]) * 1000; 179a4e4127fSJung-uk Kim freq += C2D(p[1]) * 100; 180a4e4127fSJung-uk Kim freq += C2D(p[2]) * 10; 181a4e4127fSJung-uk Kim freq += C2D(p[3]); 182a4e4127fSJung-uk Kim freq *= i * 1000000; 183a4e4127fSJung-uk Kim } 184a4e4127fSJung-uk Kim #undef C2D 185a4e4127fSJung-uk Kim tsc_freq = freq; 186a4e4127fSJung-uk Kim } 187a4e4127fSJung-uk Kim } 188a4e4127fSJung-uk Kim } 189dd7d207dSJung-uk Kim 190a4e4127fSJung-uk Kim static void 191a4e4127fSJung-uk Kim probe_tsc_freq(void) 192a4e4127fSJung-uk Kim { 193155094d7SJung-uk Kim u_int regs[4]; 194a4e4127fSJung-uk Kim uint64_t tsc1, tsc2; 195dd7d207dSJung-uk Kim 1965da5812bSJung-uk Kim if (cpu_high >= 6) { 1975da5812bSJung-uk Kim do_cpuid(6, regs); 1985da5812bSJung-uk Kim if ((regs[2] & CPUID_PERF_STAT) != 0) { 1995da5812bSJung-uk Kim /* 2005da5812bSJung-uk Kim * XXX Some emulators expose host CPUID without actual 2015da5812bSJung-uk Kim * support for these MSRs. We must test whether they 2025da5812bSJung-uk Kim * really work. 2035da5812bSJung-uk Kim */ 2045da5812bSJung-uk Kim wrmsr(MSR_MPERF, 0); 2055da5812bSJung-uk Kim wrmsr(MSR_APERF, 0); 2065da5812bSJung-uk Kim DELAY(10); 2075da5812bSJung-uk Kim if (rdmsr(MSR_MPERF) > 0 && rdmsr(MSR_APERF) > 0) 2085da5812bSJung-uk Kim tsc_perf_stat = 1; 2095da5812bSJung-uk Kim } 2105da5812bSJung-uk Kim } 2115da5812bSJung-uk Kim 21201e1933dSJohn Baldwin if (vm_guest == VM_GUEST_VMWARE) { 21301e1933dSJohn Baldwin tsc_freq_vmware(); 2145da5812bSJung-uk Kim return; 21501e1933dSJohn Baldwin } 2165da5812bSJung-uk Kim 217dd7d207dSJung-uk Kim switch (cpu_vendor_id) { 218dd7d207dSJung-uk Kim case CPU_VENDOR_AMD: 219a106a27cSJung-uk Kim if ((amd_pminfo & AMDPM_TSC_INVARIANT) != 0 || 220a106a27cSJung-uk Kim (vm_guest == VM_GUEST_NO && 221a106a27cSJung-uk Kim CPUID_TO_FAMILY(cpu_id) >= 0x10)) 222dd7d207dSJung-uk Kim tsc_is_invariant = 1; 223814124c3SKonstantin Belousov if (cpu_feature & CPUID_SSE2) { 224814124c3SKonstantin Belousov tsc_timecounter.tc_get_timecount = 225814124c3SKonstantin Belousov tsc_get_timecount_mfence; 226814124c3SKonstantin Belousov } 227dd7d207dSJung-uk Kim break; 228dd7d207dSJung-uk Kim case CPU_VENDOR_INTEL: 229a106a27cSJung-uk Kim if ((amd_pminfo & AMDPM_TSC_INVARIANT) != 0 || 230a106a27cSJung-uk Kim (vm_guest == VM_GUEST_NO && 231a106a27cSJung-uk Kim ((CPUID_TO_FAMILY(cpu_id) == 0x6 && 232dd7d207dSJung-uk Kim CPUID_TO_MODEL(cpu_id) >= 0xe) || 233dd7d207dSJung-uk Kim (CPUID_TO_FAMILY(cpu_id) == 0xf && 234a106a27cSJung-uk Kim CPUID_TO_MODEL(cpu_id) >= 0x3)))) 235dd7d207dSJung-uk Kim tsc_is_invariant = 1; 236814124c3SKonstantin Belousov if (cpu_feature & CPUID_SSE2) { 237814124c3SKonstantin Belousov tsc_timecounter.tc_get_timecount = 238814124c3SKonstantin Belousov tsc_get_timecount_lfence; 239814124c3SKonstantin Belousov } 240dd7d207dSJung-uk Kim break; 241dd7d207dSJung-uk Kim case CPU_VENDOR_CENTAUR: 242a106a27cSJung-uk Kim if (vm_guest == VM_GUEST_NO && 243a106a27cSJung-uk Kim CPUID_TO_FAMILY(cpu_id) == 0x6 && 244dd7d207dSJung-uk Kim CPUID_TO_MODEL(cpu_id) >= 0xf && 245dd7d207dSJung-uk Kim (rdmsr(0x1203) & 0x100000000ULL) == 0) 246dd7d207dSJung-uk Kim tsc_is_invariant = 1; 247814124c3SKonstantin Belousov if (cpu_feature & CPUID_SSE2) { 248814124c3SKonstantin Belousov tsc_timecounter.tc_get_timecount = 249814124c3SKonstantin Belousov tsc_get_timecount_lfence; 250814124c3SKonstantin Belousov } 251dd7d207dSJung-uk Kim break; 252dd7d207dSJung-uk Kim } 253dd7d207dSJung-uk Kim 254a4e4127fSJung-uk Kim if (tsc_skip_calibration) { 255a4e4127fSJung-uk Kim if (cpu_vendor_id == CPU_VENDOR_INTEL) 256a4e4127fSJung-uk Kim tsc_freq_intel(); 257a4e4127fSJung-uk Kim return; 258a4e4127fSJung-uk Kim } 259a4e4127fSJung-uk Kim 260a4e4127fSJung-uk Kim if (bootverbose) 261a4e4127fSJung-uk Kim printf("Calibrating TSC clock ... "); 262a4e4127fSJung-uk Kim tsc1 = rdtsc(); 263a4e4127fSJung-uk Kim DELAY(1000000); 264a4e4127fSJung-uk Kim tsc2 = rdtsc(); 265a4e4127fSJung-uk Kim tsc_freq = tsc2 - tsc1; 266a4e4127fSJung-uk Kim if (bootverbose) 267a4e4127fSJung-uk Kim printf("TSC clock: %ju Hz\n", (intmax_t)tsc_freq); 268a4e4127fSJung-uk Kim } 269a4e4127fSJung-uk Kim 270a4e4127fSJung-uk Kim void 271a4e4127fSJung-uk Kim init_TSC(void) 272a4e4127fSJung-uk Kim { 273a4e4127fSJung-uk Kim 274a4e4127fSJung-uk Kim if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled) 275a4e4127fSJung-uk Kim return; 276a4e4127fSJung-uk Kim 277fe760cfaSJohn Baldwin #ifdef __i386__ 278fe760cfaSJohn Baldwin /* The TSC is known to be broken on certain CPUs. */ 279fe760cfaSJohn Baldwin switch (cpu_vendor_id) { 280fe760cfaSJohn Baldwin case CPU_VENDOR_AMD: 281fe760cfaSJohn Baldwin switch (cpu_id & 0xFF0) { 282fe760cfaSJohn Baldwin case 0x500: 283fe760cfaSJohn Baldwin /* K5 Model 0 */ 284fe760cfaSJohn Baldwin return; 285fe760cfaSJohn Baldwin } 286fe760cfaSJohn Baldwin break; 287fe760cfaSJohn Baldwin case CPU_VENDOR_CENTAUR: 288fe760cfaSJohn Baldwin switch (cpu_id & 0xff0) { 289fe760cfaSJohn Baldwin case 0x540: 290fe760cfaSJohn Baldwin /* 291fe760cfaSJohn Baldwin * http://www.centtech.com/c6_data_sheet.pdf 292fe760cfaSJohn Baldwin * 293fe760cfaSJohn Baldwin * I-12 RDTSC may return incoherent values in EDX:EAX 294fe760cfaSJohn Baldwin * I-13 RDTSC hangs when certain event counters are used 295fe760cfaSJohn Baldwin */ 296fe760cfaSJohn Baldwin return; 297fe760cfaSJohn Baldwin } 298fe760cfaSJohn Baldwin break; 299fe760cfaSJohn Baldwin case CPU_VENDOR_NSC: 300fe760cfaSJohn Baldwin switch (cpu_id & 0xff0) { 301fe760cfaSJohn Baldwin case 0x540: 302fe760cfaSJohn Baldwin if ((cpu_id & CPUID_STEPPING) == 0) 303fe760cfaSJohn Baldwin return; 304fe760cfaSJohn Baldwin break; 305fe760cfaSJohn Baldwin } 306fe760cfaSJohn Baldwin break; 307fe760cfaSJohn Baldwin } 308fe760cfaSJohn Baldwin #endif 309fe760cfaSJohn Baldwin 310a4e4127fSJung-uk Kim probe_tsc_freq(); 311a4e4127fSJung-uk Kim 312dd7d207dSJung-uk Kim /* 313dd7d207dSJung-uk Kim * Inform CPU accounting about our boot-time clock rate. This will 314dd7d207dSJung-uk Kim * be updated if someone loads a cpufreq driver after boot that 315dd7d207dSJung-uk Kim * discovers a new max frequency. 316dd7d207dSJung-uk Kim */ 317a4e4127fSJung-uk Kim if (tsc_freq != 0) 3185ac44f72SJung-uk Kim set_cputicker(rdtsc, tsc_freq, !tsc_is_invariant); 319dd7d207dSJung-uk Kim 320dd7d207dSJung-uk Kim if (tsc_is_invariant) 321dd7d207dSJung-uk Kim return; 322dd7d207dSJung-uk Kim 323dd7d207dSJung-uk Kim /* Register to find out about changes in CPU frequency. */ 324dd7d207dSJung-uk Kim tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change, 325dd7d207dSJung-uk Kim tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST); 326dd7d207dSJung-uk Kim tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change, 327dd7d207dSJung-uk Kim tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST); 328dd7d207dSJung-uk Kim tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed, 329dd7d207dSJung-uk Kim tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY); 330dd7d207dSJung-uk Kim } 331dd7d207dSJung-uk Kim 33265e7d70bSJung-uk Kim #ifdef SMP 33365e7d70bSJung-uk Kim 334814124c3SKonstantin Belousov /* 335814124c3SKonstantin Belousov * RDTSC is not a serializing instruction, and does not drain 336814124c3SKonstantin Belousov * instruction stream, so we need to drain the stream before executing 337814124c3SKonstantin Belousov * it. It could be fixed by use of RDTSCP, except the instruction is 338814124c3SKonstantin Belousov * not available everywhere. 339814124c3SKonstantin Belousov * 340814124c3SKonstantin Belousov * Use CPUID for draining in the boot-time SMP constistency test. The 341814124c3SKonstantin Belousov * timecounters use MFENCE for AMD CPUs, and LFENCE for others (Intel 342814124c3SKonstantin Belousov * and VIA) when SSE2 is present, and nothing on older machines which 343814124c3SKonstantin Belousov * also do not issue RDTSC prematurely. There, testing for SSE2 and 344e1a18e46SKonstantin Belousov * vendor is too cumbersome, and we learn about TSC presence from CPUID. 345814124c3SKonstantin Belousov * 346814124c3SKonstantin Belousov * Do not use do_cpuid(), since we do not need CPUID results, which 347814124c3SKonstantin Belousov * have to be written into memory with do_cpuid(). 348814124c3SKonstantin Belousov */ 34965e7d70bSJung-uk Kim #define TSC_READ(x) \ 35065e7d70bSJung-uk Kim static void \ 35165e7d70bSJung-uk Kim tsc_read_##x(void *arg) \ 35265e7d70bSJung-uk Kim { \ 3537bfcb3bbSJim Harris uint64_t *tsc = arg; \ 35465e7d70bSJung-uk Kim u_int cpu = PCPU_GET(cpuid); \ 35565e7d70bSJung-uk Kim \ 356814124c3SKonstantin Belousov __asm __volatile("cpuid" : : : "eax", "ebx", "ecx", "edx"); \ 3577bfcb3bbSJim Harris tsc[cpu * 3 + x] = rdtsc(); \ 35865e7d70bSJung-uk Kim } 35965e7d70bSJung-uk Kim TSC_READ(0) 36065e7d70bSJung-uk Kim TSC_READ(1) 36165e7d70bSJung-uk Kim TSC_READ(2) 36265e7d70bSJung-uk Kim #undef TSC_READ 36365e7d70bSJung-uk Kim 36465e7d70bSJung-uk Kim #define N 1000 36565e7d70bSJung-uk Kim 36665e7d70bSJung-uk Kim static void 36765e7d70bSJung-uk Kim comp_smp_tsc(void *arg) 36865e7d70bSJung-uk Kim { 3697bfcb3bbSJim Harris uint64_t *tsc; 3707bfcb3bbSJim Harris int64_t d1, d2; 37165e7d70bSJung-uk Kim u_int cpu = PCPU_GET(cpuid); 37265e7d70bSJung-uk Kim u_int i, j, size; 37365e7d70bSJung-uk Kim 37465e7d70bSJung-uk Kim size = (mp_maxid + 1) * 3; 37565e7d70bSJung-uk Kim for (i = 0, tsc = arg; i < N; i++, tsc += size) 37665e7d70bSJung-uk Kim CPU_FOREACH(j) { 37765e7d70bSJung-uk Kim if (j == cpu) 37865e7d70bSJung-uk Kim continue; 37965e7d70bSJung-uk Kim d1 = tsc[cpu * 3 + 1] - tsc[j * 3]; 38065e7d70bSJung-uk Kim d2 = tsc[cpu * 3 + 2] - tsc[j * 3 + 1]; 38165e7d70bSJung-uk Kim if (d1 <= 0 || d2 <= 0) { 38265e7d70bSJung-uk Kim smp_tsc = 0; 38365e7d70bSJung-uk Kim return; 38465e7d70bSJung-uk Kim } 38565e7d70bSJung-uk Kim } 38665e7d70bSJung-uk Kim } 38765e7d70bSJung-uk Kim 388b2c63698SAlexander Motin static void 389b2c63698SAlexander Motin adj_smp_tsc(void *arg) 390b2c63698SAlexander Motin { 391b2c63698SAlexander Motin uint64_t *tsc; 392b2c63698SAlexander Motin int64_t d, min, max; 393b2c63698SAlexander Motin u_int cpu = PCPU_GET(cpuid); 394b2c63698SAlexander Motin u_int first, i, size; 395b2c63698SAlexander Motin 396b2c63698SAlexander Motin first = CPU_FIRST(); 397b2c63698SAlexander Motin if (cpu == first) 398b2c63698SAlexander Motin return; 399b2c63698SAlexander Motin min = INT64_MIN; 400b2c63698SAlexander Motin max = INT64_MAX; 401b2c63698SAlexander Motin size = (mp_maxid + 1) * 3; 402b2c63698SAlexander Motin for (i = 0, tsc = arg; i < N; i++, tsc += size) { 403b2c63698SAlexander Motin d = tsc[first * 3] - tsc[cpu * 3 + 1]; 404b2c63698SAlexander Motin if (d > min) 405b2c63698SAlexander Motin min = d; 406b2c63698SAlexander Motin d = tsc[first * 3 + 1] - tsc[cpu * 3 + 2]; 407b2c63698SAlexander Motin if (d > min) 408b2c63698SAlexander Motin min = d; 409b2c63698SAlexander Motin d = tsc[first * 3 + 1] - tsc[cpu * 3]; 410b2c63698SAlexander Motin if (d < max) 411b2c63698SAlexander Motin max = d; 412b2c63698SAlexander Motin d = tsc[first * 3 + 2] - tsc[cpu * 3 + 1]; 413b2c63698SAlexander Motin if (d < max) 414b2c63698SAlexander Motin max = d; 415b2c63698SAlexander Motin } 416b2c63698SAlexander Motin if (min > max) 417b2c63698SAlexander Motin return; 418b2c63698SAlexander Motin d = min / 2 + max / 2; 419b2c63698SAlexander Motin __asm __volatile ( 420b2c63698SAlexander Motin "movl $0x10, %%ecx\n\t" 421b2c63698SAlexander Motin "rdmsr\n\t" 422b2c63698SAlexander Motin "addl %%edi, %%eax\n\t" 423b2c63698SAlexander Motin "adcl %%esi, %%edx\n\t" 424b2c63698SAlexander Motin "wrmsr\n" 425b2c63698SAlexander Motin : /* No output */ 426b2c63698SAlexander Motin : "D" ((uint32_t)d), "S" ((uint32_t)(d >> 32)) 427b2c63698SAlexander Motin : "ax", "cx", "dx", "cc" 428b2c63698SAlexander Motin ); 429b2c63698SAlexander Motin } 430b2c63698SAlexander Motin 43165e7d70bSJung-uk Kim static int 432e7f1427dSKonstantin Belousov test_tsc(void) 43365e7d70bSJung-uk Kim { 4347bfcb3bbSJim Harris uint64_t *data, *tsc; 435b2c63698SAlexander Motin u_int i, size, adj; 43665e7d70bSJung-uk Kim 437e7f1427dSKonstantin Belousov if ((!smp_tsc && !tsc_is_invariant) || vm_guest) 43865e7d70bSJung-uk Kim return (-100); 43965e7d70bSJung-uk Kim size = (mp_maxid + 1) * 3; 44065e7d70bSJung-uk Kim data = malloc(sizeof(*data) * size * N, M_TEMP, M_WAITOK); 441b2c63698SAlexander Motin adj = 0; 442b2c63698SAlexander Motin retry: 44365e7d70bSJung-uk Kim for (i = 0, tsc = data; i < N; i++, tsc += size) 44465e7d70bSJung-uk Kim smp_rendezvous(tsc_read_0, tsc_read_1, tsc_read_2, tsc); 44565e7d70bSJung-uk Kim smp_tsc = 1; /* XXX */ 44665e7d70bSJung-uk Kim smp_rendezvous(smp_no_rendevous_barrier, comp_smp_tsc, 44765e7d70bSJung-uk Kim smp_no_rendevous_barrier, data); 448b2c63698SAlexander Motin if (!smp_tsc && adj < smp_tsc_adjust) { 449b2c63698SAlexander Motin adj++; 450b2c63698SAlexander Motin smp_rendezvous(smp_no_rendevous_barrier, adj_smp_tsc, 451b2c63698SAlexander Motin smp_no_rendevous_barrier, data); 452b2c63698SAlexander Motin goto retry; 453b2c63698SAlexander Motin } 45465e7d70bSJung-uk Kim free(data, M_TEMP); 45565e7d70bSJung-uk Kim if (bootverbose) 456b2c63698SAlexander Motin printf("SMP: %sed TSC synchronization test%s\n", 457b2c63698SAlexander Motin smp_tsc ? "pass" : "fail", 458b2c63698SAlexander Motin adj > 0 ? " after adjustment" : ""); 45926e6537aSJung-uk Kim if (smp_tsc && tsc_is_invariant) { 46026e6537aSJung-uk Kim switch (cpu_vendor_id) { 46126e6537aSJung-uk Kim case CPU_VENDOR_AMD: 46226e6537aSJung-uk Kim /* 46326e6537aSJung-uk Kim * Starting with Family 15h processors, TSC clock 46426e6537aSJung-uk Kim * source is in the north bridge. Check whether 46526e6537aSJung-uk Kim * we have a single-socket/multi-core platform. 46626e6537aSJung-uk Kim * XXX Need more work for complex cases. 46726e6537aSJung-uk Kim */ 46826e6537aSJung-uk Kim if (CPUID_TO_FAMILY(cpu_id) < 0x15 || 46926e6537aSJung-uk Kim (amd_feature2 & AMDID2_CMP) == 0 || 47026e6537aSJung-uk Kim smp_cpus > (cpu_procinfo2 & AMDID_CMP_CORES) + 1) 47126e6537aSJung-uk Kim break; 47226e6537aSJung-uk Kim return (1000); 47326e6537aSJung-uk Kim case CPU_VENDOR_INTEL: 47426e6537aSJung-uk Kim /* 47526e6537aSJung-uk Kim * XXX Assume Intel platforms have synchronized TSCs. 47626e6537aSJung-uk Kim */ 47726e6537aSJung-uk Kim return (1000); 47826e6537aSJung-uk Kim } 47926e6537aSJung-uk Kim return (800); 48026e6537aSJung-uk Kim } 48126e6537aSJung-uk Kim return (-100); 48265e7d70bSJung-uk Kim } 48365e7d70bSJung-uk Kim 48465e7d70bSJung-uk Kim #undef N 48565e7d70bSJung-uk Kim 486e7f1427dSKonstantin Belousov #else 487e7f1427dSKonstantin Belousov 488e7f1427dSKonstantin Belousov /* 489e7f1427dSKonstantin Belousov * The function is not called, it is provided to avoid linking failure 490e7f1427dSKonstantin Belousov * on uniprocessor kernel. 491e7f1427dSKonstantin Belousov */ 492e7f1427dSKonstantin Belousov static int 493e7f1427dSKonstantin Belousov test_tsc(void) 494e7f1427dSKonstantin Belousov { 495e7f1427dSKonstantin Belousov 496e7f1427dSKonstantin Belousov return (0); 497e7f1427dSKonstantin Belousov } 498e7f1427dSKonstantin Belousov 49965e7d70bSJung-uk Kim #endif /* SMP */ 50065e7d70bSJung-uk Kim 50165e7d70bSJung-uk Kim static void 502dd7d207dSJung-uk Kim init_TSC_tc(void) 503dd7d207dSJung-uk Kim { 50495f2f098SJung-uk Kim uint64_t max_freq; 50595f2f098SJung-uk Kim int shift; 506dd7d207dSJung-uk Kim 50738b8542cSJung-uk Kim if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled) 508dd7d207dSJung-uk Kim return; 509dd7d207dSJung-uk Kim 510dd7d207dSJung-uk Kim /* 51195f2f098SJung-uk Kim * Limit timecounter frequency to fit in an int and prevent it from 51295f2f098SJung-uk Kim * overflowing too fast. 51395f2f098SJung-uk Kim */ 51495f2f098SJung-uk Kim max_freq = UINT_MAX; 51595f2f098SJung-uk Kim 51695f2f098SJung-uk Kim /* 517dd7d207dSJung-uk Kim * We can not use the TSC if we support APM. Precise timekeeping 518dd7d207dSJung-uk Kim * on an APM'ed machine is at best a fools pursuit, since 519dd7d207dSJung-uk Kim * any and all of the time spent in various SMM code can't 520dd7d207dSJung-uk Kim * be reliably accounted for. Reading the RTC is your only 521dd7d207dSJung-uk Kim * source of reliable time info. The i8254 loses too, of course, 522dd7d207dSJung-uk Kim * but we need to have some kind of time... 523dd7d207dSJung-uk Kim * We don't know at this point whether APM is going to be used 524dd7d207dSJung-uk Kim * or not, nor when it might be activated. Play it safe. 525dd7d207dSJung-uk Kim */ 526dd7d207dSJung-uk Kim if (power_pm_get_type() == POWER_PM_TYPE_APM) { 527dd7d207dSJung-uk Kim tsc_timecounter.tc_quality = -1000; 528dd7d207dSJung-uk Kim if (bootverbose) 529dd7d207dSJung-uk Kim printf("TSC timecounter disabled: APM enabled.\n"); 53065e7d70bSJung-uk Kim goto init; 531dd7d207dSJung-uk Kim } 532dd7d207dSJung-uk Kim 533a49399a9SJung-uk Kim /* 53492597e06SJohn Baldwin * Intel CPUs without a C-state invariant TSC can stop the TSC 535d1411416SJohn Baldwin * in either C2 or C3. Disable use of C2 and C3 while using 536d1411416SJohn Baldwin * the TSC as the timecounter. The timecounter can be changed 537d1411416SJohn Baldwin * to enable C2 and C3. 538d1411416SJohn Baldwin * 539d1411416SJohn Baldwin * Note that the TSC is used as the cputicker for computing 540d1411416SJohn Baldwin * thread runtime regardless of the timecounter setting, so 541d1411416SJohn Baldwin * using an alternate timecounter and enabling C2 or C3 can 542d1411416SJohn Baldwin * result incorrect runtimes for kernel idle threads (but not 543d1411416SJohn Baldwin * for any non-idle threads). 544a49399a9SJung-uk Kim */ 5458cd59625SKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_INTEL && 546a49399a9SJung-uk Kim (amd_pminfo & AMDPM_TSC_INVARIANT) == 0) { 54792597e06SJohn Baldwin tsc_timecounter.tc_flags |= TC_FLAGS_C2STOP; 548a49399a9SJung-uk Kim if (bootverbose) 549d1411416SJohn Baldwin printf("TSC timecounter disables C2 and C3.\n"); 550a49399a9SJung-uk Kim } 551a49399a9SJung-uk Kim 552dd7d207dSJung-uk Kim /* 553e7f1427dSKonstantin Belousov * We can not use the TSC in SMP mode unless the TSCs on all CPUs 554e7f1427dSKonstantin Belousov * are synchronized. If the user is sure that the system has 555e7f1427dSKonstantin Belousov * synchronized TSCs, set kern.timecounter.smp_tsc tunable to a 556e7f1427dSKonstantin Belousov * non-zero value. The TSC seems unreliable in virtualized SMP 5575cf8ac1bSMike Silbersack * environments, so it is set to a negative quality in those cases. 558dd7d207dSJung-uk Kim */ 559e7f1427dSKonstantin Belousov if (mp_ncpus > 1) 560e7f1427dSKonstantin Belousov tsc_timecounter.tc_quality = test_tsc(); 561e7f1427dSKonstantin Belousov else if (tsc_is_invariant) 56226e6537aSJung-uk Kim tsc_timecounter.tc_quality = 1000; 563e7f1427dSKonstantin Belousov max_freq >>= tsc_shift; 56426e6537aSJung-uk Kim 56565e7d70bSJung-uk Kim init: 566e7f1427dSKonstantin Belousov for (shift = 0; shift <= 31 && (tsc_freq >> shift) > max_freq; shift++) 56795f2f098SJung-uk Kim ; 568e7f1427dSKonstantin Belousov if ((cpu_feature & CPUID_SSE2) != 0 && mp_ncpus > 1) { 569814124c3SKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_AMD) { 570e7f1427dSKonstantin Belousov tsc_timecounter.tc_get_timecount = shift > 0 ? 571e7f1427dSKonstantin Belousov tsc_get_timecount_low_mfence : 572e7f1427dSKonstantin Belousov tsc_get_timecount_mfence; 573814124c3SKonstantin Belousov } else { 574e7f1427dSKonstantin Belousov tsc_timecounter.tc_get_timecount = shift > 0 ? 575e7f1427dSKonstantin Belousov tsc_get_timecount_low_lfence : 576e7f1427dSKonstantin Belousov tsc_get_timecount_lfence; 577814124c3SKonstantin Belousov } 578e7f1427dSKonstantin Belousov } else { 579e7f1427dSKonstantin Belousov tsc_timecounter.tc_get_timecount = shift > 0 ? 580e7f1427dSKonstantin Belousov tsc_get_timecount_low : tsc_get_timecount; 581e7f1427dSKonstantin Belousov } 582e7f1427dSKonstantin Belousov if (shift > 0) { 58395f2f098SJung-uk Kim tsc_timecounter.tc_name = "TSC-low"; 58495f2f098SJung-uk Kim if (bootverbose) 585bc8e4ad2SJung-uk Kim printf("TSC timecounter discards lower %d bit(s)\n", 58695f2f098SJung-uk Kim shift); 58795f2f098SJung-uk Kim } 588bc34c87eSJung-uk Kim if (tsc_freq != 0) { 58995f2f098SJung-uk Kim tsc_timecounter.tc_frequency = tsc_freq >> shift; 59095f2f098SJung-uk Kim tsc_timecounter.tc_priv = (void *)(intptr_t)shift; 591dd7d207dSJung-uk Kim tc_init(&tsc_timecounter); 592dd7d207dSJung-uk Kim } 593dd7d207dSJung-uk Kim } 59465e7d70bSJung-uk Kim SYSINIT(tsc_tc, SI_SUB_SMP, SI_ORDER_ANY, init_TSC_tc, NULL); 595dd7d207dSJung-uk Kim 596dd7d207dSJung-uk Kim /* 597dd7d207dSJung-uk Kim * When cpufreq levels change, find out about the (new) max frequency. We 598dd7d207dSJung-uk Kim * use this to update CPU accounting in case it got a lower estimate at boot. 599dd7d207dSJung-uk Kim */ 600dd7d207dSJung-uk Kim static void 601dd7d207dSJung-uk Kim tsc_levels_changed(void *arg, int unit) 602dd7d207dSJung-uk Kim { 603dd7d207dSJung-uk Kim device_t cf_dev; 604dd7d207dSJung-uk Kim struct cf_level *levels; 605dd7d207dSJung-uk Kim int count, error; 606dd7d207dSJung-uk Kim uint64_t max_freq; 607dd7d207dSJung-uk Kim 608dd7d207dSJung-uk Kim /* Only use values from the first CPU, assuming all are equal. */ 609dd7d207dSJung-uk Kim if (unit != 0) 610dd7d207dSJung-uk Kim return; 611dd7d207dSJung-uk Kim 612dd7d207dSJung-uk Kim /* Find the appropriate cpufreq device instance. */ 613dd7d207dSJung-uk Kim cf_dev = devclass_get_device(devclass_find("cpufreq"), unit); 614dd7d207dSJung-uk Kim if (cf_dev == NULL) { 615dd7d207dSJung-uk Kim printf("tsc_levels_changed() called but no cpufreq device?\n"); 616dd7d207dSJung-uk Kim return; 617dd7d207dSJung-uk Kim } 618dd7d207dSJung-uk Kim 619dd7d207dSJung-uk Kim /* Get settings from the device and find the max frequency. */ 620dd7d207dSJung-uk Kim count = 64; 621dd7d207dSJung-uk Kim levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT); 622dd7d207dSJung-uk Kim if (levels == NULL) 623dd7d207dSJung-uk Kim return; 624dd7d207dSJung-uk Kim error = CPUFREQ_LEVELS(cf_dev, levels, &count); 625dd7d207dSJung-uk Kim if (error == 0 && count != 0) { 626dd7d207dSJung-uk Kim max_freq = (uint64_t)levels[0].total_set.freq * 1000000; 627dd7d207dSJung-uk Kim set_cputicker(rdtsc, max_freq, 1); 628dd7d207dSJung-uk Kim } else 629dd7d207dSJung-uk Kim printf("tsc_levels_changed: no max freq found\n"); 630dd7d207dSJung-uk Kim free(levels, M_TEMP); 631dd7d207dSJung-uk Kim } 632dd7d207dSJung-uk Kim 633dd7d207dSJung-uk Kim /* 634dd7d207dSJung-uk Kim * If the TSC timecounter is in use, veto the pending change. It may be 635dd7d207dSJung-uk Kim * possible in the future to handle a dynamically-changing timecounter rate. 636dd7d207dSJung-uk Kim */ 637dd7d207dSJung-uk Kim static void 638dd7d207dSJung-uk Kim tsc_freq_changing(void *arg, const struct cf_level *level, int *status) 639dd7d207dSJung-uk Kim { 640dd7d207dSJung-uk Kim 641dd7d207dSJung-uk Kim if (*status != 0 || timecounter != &tsc_timecounter) 642dd7d207dSJung-uk Kim return; 643dd7d207dSJung-uk Kim 644dd7d207dSJung-uk Kim printf("timecounter TSC must not be in use when " 645dd7d207dSJung-uk Kim "changing frequencies; change denied\n"); 646dd7d207dSJung-uk Kim *status = EBUSY; 647dd7d207dSJung-uk Kim } 648dd7d207dSJung-uk Kim 649dd7d207dSJung-uk Kim /* Update TSC freq with the value indicated by the caller. */ 650dd7d207dSJung-uk Kim static void 651dd7d207dSJung-uk Kim tsc_freq_changed(void *arg, const struct cf_level *level, int status) 652dd7d207dSJung-uk Kim { 6533453537fSJung-uk Kim uint64_t freq; 654dd7d207dSJung-uk Kim 655dd7d207dSJung-uk Kim /* If there was an error during the transition, don't do anything. */ 65679422085SJung-uk Kim if (tsc_disabled || status != 0) 657dd7d207dSJung-uk Kim return; 658dd7d207dSJung-uk Kim 659dd7d207dSJung-uk Kim /* Total setting for this level gives the new frequency in MHz. */ 6603453537fSJung-uk Kim freq = (uint64_t)level->total_set.freq * 1000000; 6613453537fSJung-uk Kim atomic_store_rel_64(&tsc_freq, freq); 66295f2f098SJung-uk Kim tsc_timecounter.tc_frequency = 66395f2f098SJung-uk Kim freq >> (int)(intptr_t)tsc_timecounter.tc_priv; 664dd7d207dSJung-uk Kim } 665dd7d207dSJung-uk Kim 666dd7d207dSJung-uk Kim static int 667dd7d207dSJung-uk Kim sysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS) 668dd7d207dSJung-uk Kim { 669dd7d207dSJung-uk Kim int error; 670dd7d207dSJung-uk Kim uint64_t freq; 671dd7d207dSJung-uk Kim 6723453537fSJung-uk Kim freq = atomic_load_acq_64(&tsc_freq); 6733453537fSJung-uk Kim if (freq == 0) 674dd7d207dSJung-uk Kim return (EOPNOTSUPP); 675cbc134adSMatthew D Fleming error = sysctl_handle_64(oidp, &freq, 0, req); 6767ebbcb21SJung-uk Kim if (error == 0 && req->newptr != NULL) { 6773453537fSJung-uk Kim atomic_store_rel_64(&tsc_freq, freq); 678bc8e4ad2SJung-uk Kim atomic_store_rel_64(&tsc_timecounter.tc_frequency, 679bc8e4ad2SJung-uk Kim freq >> (int)(intptr_t)tsc_timecounter.tc_priv); 6807ebbcb21SJung-uk Kim } 681dd7d207dSJung-uk Kim return (error); 682dd7d207dSJung-uk Kim } 683dd7d207dSJung-uk Kim 684cbc134adSMatthew D Fleming SYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_U64 | CTLFLAG_RW, 6855331d61dSJung-uk Kim 0, 0, sysctl_machdep_tsc_freq, "QU", "Time Stamp Counter frequency"); 686dd7d207dSJung-uk Kim 687727c7b2dSJung-uk Kim static u_int 68895f2f098SJung-uk Kim tsc_get_timecount(struct timecounter *tc __unused) 689dd7d207dSJung-uk Kim { 690727c7b2dSJung-uk Kim 691727c7b2dSJung-uk Kim return (rdtsc32()); 692dd7d207dSJung-uk Kim } 69395f2f098SJung-uk Kim 694814124c3SKonstantin Belousov static inline u_int 695bc8e4ad2SJung-uk Kim tsc_get_timecount_low(struct timecounter *tc) 69695f2f098SJung-uk Kim { 6975df88f46SJung-uk Kim uint32_t rv; 69895f2f098SJung-uk Kim 6995df88f46SJung-uk Kim __asm __volatile("rdtsc; shrd %%cl, %%edx, %0" 7005df88f46SJung-uk Kim : "=a" (rv) : "c" ((int)(intptr_t)tc->tc_priv) : "edx"); 7015df88f46SJung-uk Kim return (rv); 70295f2f098SJung-uk Kim } 703aea81038SKonstantin Belousov 704814124c3SKonstantin Belousov static u_int 705814124c3SKonstantin Belousov tsc_get_timecount_lfence(struct timecounter *tc __unused) 706814124c3SKonstantin Belousov { 707814124c3SKonstantin Belousov 708814124c3SKonstantin Belousov lfence(); 709814124c3SKonstantin Belousov return (rdtsc32()); 710814124c3SKonstantin Belousov } 711814124c3SKonstantin Belousov 712814124c3SKonstantin Belousov static u_int 713814124c3SKonstantin Belousov tsc_get_timecount_low_lfence(struct timecounter *tc) 714814124c3SKonstantin Belousov { 715814124c3SKonstantin Belousov 716814124c3SKonstantin Belousov lfence(); 717814124c3SKonstantin Belousov return (tsc_get_timecount_low(tc)); 718814124c3SKonstantin Belousov } 719814124c3SKonstantin Belousov 720814124c3SKonstantin Belousov static u_int 721814124c3SKonstantin Belousov tsc_get_timecount_mfence(struct timecounter *tc __unused) 722814124c3SKonstantin Belousov { 723814124c3SKonstantin Belousov 724814124c3SKonstantin Belousov mfence(); 725814124c3SKonstantin Belousov return (rdtsc32()); 726814124c3SKonstantin Belousov } 727814124c3SKonstantin Belousov 728814124c3SKonstantin Belousov static u_int 729814124c3SKonstantin Belousov tsc_get_timecount_low_mfence(struct timecounter *tc) 730814124c3SKonstantin Belousov { 731814124c3SKonstantin Belousov 732814124c3SKonstantin Belousov mfence(); 733814124c3SKonstantin Belousov return (tsc_get_timecount_low(tc)); 734814124c3SKonstantin Belousov } 735814124c3SKonstantin Belousov 73616808549SKonstantin Belousov static uint32_t 73716808549SKonstantin Belousov x86_tsc_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc) 738aea81038SKonstantin Belousov { 739aea81038SKonstantin Belousov 74016808549SKonstantin Belousov vdso_th->th_algo = VDSO_TH_ALGO_X86_TSC; 741d1b1b600SNeel Natu vdso_th->th_x86_shift = (int)(intptr_t)tc->tc_priv; 74216808549SKonstantin Belousov vdso_th->th_x86_hpet_idx = 0xffffffff; 743aea81038SKonstantin Belousov bzero(vdso_th->th_res, sizeof(vdso_th->th_res)); 74416808549SKonstantin Belousov return (1); 745aea81038SKonstantin Belousov } 746aea81038SKonstantin Belousov 747aea81038SKonstantin Belousov #ifdef COMPAT_FREEBSD32 74816808549SKonstantin Belousov static uint32_t 74916808549SKonstantin Belousov x86_tsc_vdso_timehands32(struct vdso_timehands32 *vdso_th32, 750d1b1b600SNeel Natu struct timecounter *tc) 751aea81038SKonstantin Belousov { 752aea81038SKonstantin Belousov 75316808549SKonstantin Belousov vdso_th32->th_algo = VDSO_TH_ALGO_X86_TSC; 754d1b1b600SNeel Natu vdso_th32->th_x86_shift = (int)(intptr_t)tc->tc_priv; 75516808549SKonstantin Belousov vdso_th32->th_x86_hpet_idx = 0xffffffff; 756aea81038SKonstantin Belousov bzero(vdso_th32->th_res, sizeof(vdso_th32->th_res)); 75716808549SKonstantin Belousov return (1); 758aea81038SKonstantin Belousov } 759aea81038SKonstantin Belousov #endif 760