From 79422085d49358af16e00ab015fe2601692235e9 Mon Sep 17 00:00:00 2001 From: Jung-uk Kim Date: Fri, 11 Mar 2011 00:44:32 +0000 Subject: [PATCH] Add a tunable "machdep.disable_tsc" to turn off TSC. Specifically, it turns off boot-time CPU frequency calibration, DELAY(9) with TSC, and using TSC as a CPU ticker. Note tsc_present does not change by this tunable. --- sys/amd64/amd64/machdep.c | 37 ++++++++++++++++++++++--------- sys/contrib/altq/altq/altq_subr.c | 4 ++-- sys/i386/i386/machdep.c | 25 +++++++++++++-------- sys/pc98/pc98/machdep.c | 23 ++++++++++++------- sys/x86/x86/tsc.c | 18 +++++++++------ 5 files changed, 71 insertions(+), 36 deletions(-) diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c index 0bb552c5ccb..808dc08fe38 100644 --- a/sys/amd64/amd64/machdep.c +++ b/sys/amd64/amd64/machdep.c @@ -80,6 +80,9 @@ __FBSDID("$FreeBSD$"); #include #include #include +#ifdef SMP +#include +#endif #include #include #include @@ -544,20 +547,22 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) return (EINVAL); /* If TSC is P-state invariant, DELAY(9) based logic fails. */ - if (tsc_is_invariant) + if (tsc_is_invariant && tsc_freq != 0) return (EOPNOTSUPP); /* If we're booting, trust the rate calibrated moments ago. */ - if (cold) { + if (cold && tsc_freq != 0) { *rate = tsc_freq; return (0); } #ifdef SMP - /* Schedule ourselves on the indicated cpu. */ - thread_lock(curthread); - sched_bind(curthread, cpu_id); - thread_unlock(curthread); + if (smp_cpus > 1) { + /* Schedule ourselves on the indicated cpu. */ + thread_lock(curthread); + sched_bind(curthread, cpu_id); + thread_unlock(curthread); + } #endif /* Calibrate by measuring a short delay. */ @@ -568,12 +573,24 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) intr_restore(reg); #ifdef SMP - thread_lock(curthread); - sched_unbind(curthread); - thread_unlock(curthread); + if (smp_cpus > 1) { + thread_lock(curthread); + sched_unbind(curthread); + thread_unlock(curthread); + } #endif - *rate = (tsc2 - tsc1) * 1000; + tsc2 -= tsc1; + if (tsc_freq != 0) { + *rate = tsc2 * 1000; + return (0); + } + + /* + * Subtract 0.5% of the total. Empirical testing has shown that + * overhead in DELAY() works out to approximately this value. + */ + *rate = tsc2 * 1000 - tsc2 * 5; return (0); } diff --git a/sys/contrib/altq/altq/altq_subr.c b/sys/contrib/altq/altq/altq_subr.c index 7d2fc77da3f..a58747dd67c 100644 --- a/sys/contrib/altq/altq/altq_subr.c +++ b/sys/contrib/altq/altq/altq_subr.c @@ -78,10 +78,10 @@ #include #endif #if defined(__amd64__) || defined(__i386__) +#include /* for cpu_feature or tsc_present */ #include /* for pentium tsc */ #if defined(__NetBSD__) || defined(__OpenBSD__) #include /* for CPUID_TSC */ -#include /* for cpu_feature */ #endif #endif /* __amd64 || __i386__ */ @@ -927,7 +927,7 @@ init_machclk_setup(void) #if defined(__amd64__) || defined(__i386__) /* check if TSC is available */ #ifdef __FreeBSD__ - if (tsc_freq == 0) + if (!tsc_present || tsc_freq == 0) #else if ((cpu_feature & CPUID_TSC) == 0) #endif diff --git a/sys/i386/i386/machdep.c b/sys/i386/i386/machdep.c index c5c0dfc297b..a87095fcd52 100644 --- a/sys/i386/i386/machdep.c +++ b/sys/i386/i386/machdep.c @@ -79,6 +79,9 @@ __FBSDID("$FreeBSD$"); #include #include #include +#ifdef SMP +#include +#endif #include #include #include @@ -1142,20 +1145,22 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) return (EOPNOTSUPP); /* If TSC is P-state invariant, DELAY(9) based logic fails. */ - if (tsc_is_invariant) + if (tsc_is_invariant && tsc_freq != 0) return (EOPNOTSUPP); /* If we're booting, trust the rate calibrated moments ago. */ - if (cold) { + if (cold && tsc_freq != 0) { *rate = tsc_freq; return (0); } #ifdef SMP - /* Schedule ourselves on the indicated cpu. */ - thread_lock(curthread); - sched_bind(curthread, cpu_id); - thread_unlock(curthread); + if (smp_cpus > 1) { + /* Schedule ourselves on the indicated cpu. */ + thread_lock(curthread); + sched_bind(curthread, cpu_id); + thread_unlock(curthread); + } #endif /* Calibrate by measuring a short delay. */ @@ -1166,9 +1171,11 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) intr_restore(reg); #ifdef SMP - thread_lock(curthread); - sched_unbind(curthread); - thread_unlock(curthread); + if (smp_cpus > 1) { + thread_lock(curthread); + sched_unbind(curthread); + thread_unlock(curthread); + } #endif tsc2 -= tsc1; diff --git a/sys/pc98/pc98/machdep.c b/sys/pc98/pc98/machdep.c index 07b3b841ea6..c1d065c0fb9 100644 --- a/sys/pc98/pc98/machdep.c +++ b/sys/pc98/pc98/machdep.c @@ -77,6 +77,9 @@ __FBSDID("$FreeBSD$"); #include #include #include +#ifdef SMP +#include +#endif #include #include #include @@ -1077,16 +1080,18 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) return (EOPNOTSUPP); /* If we're booting, trust the rate calibrated moments ago. */ - if (cold) { + if (cold && tsc_freq != 0) { *rate = tsc_freq; return (0); } #ifdef SMP - /* Schedule ourselves on the indicated cpu. */ - thread_lock(curthread); - sched_bind(curthread, cpu_id); - thread_unlock(curthread); + if (smp_cpus > 1) { + /* Schedule ourselves on the indicated cpu. */ + thread_lock(curthread); + sched_bind(curthread, cpu_id); + thread_unlock(curthread); + } #endif /* Calibrate by measuring a short delay. */ @@ -1097,9 +1102,11 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) intr_restore(reg); #ifdef SMP - thread_lock(curthread); - sched_unbind(curthread); - thread_unlock(curthread); + if (smp_cpus > 1) { + thread_lock(curthread); + sched_unbind(curthread); + thread_unlock(curthread); + } #endif tsc2 -= tsc1; diff --git a/sys/x86/x86/tsc.c b/sys/x86/x86/tsc.c index 3e0300b9ecd..40d5f3c72a2 100644 --- a/sys/x86/x86/tsc.c +++ b/sys/x86/x86/tsc.c @@ -63,6 +63,11 @@ SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0, TUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc); #endif +static int tsc_disabled; +SYSCTL_INT(_machdep, OID_AUTO, disable_tsc, CTLFLAG_RDTUN, &tsc_disabled, 0, + "Disable x86 Time Stamp Counter"); +TUNABLE_INT("machdep.disable_tsc", &tsc_disabled); + static void tsc_freq_changed(void *arg, const struct cf_level *level, int status); static void tsc_freq_changing(void *arg, const struct cf_level *level, @@ -84,12 +89,11 @@ init_TSC(void) { u_int64_t tscval[2]; - if (cpu_feature & CPUID_TSC) - tsc_present = 1; - else - tsc_present = 0; + if ((cpu_feature & CPUID_TSC) == 0) + return; + tsc_present = 1; - if (!tsc_present) + if (tsc_disabled) return; if (bootverbose) @@ -151,7 +155,7 @@ void init_TSC_tc(void) { - if (!tsc_present) + if (!tsc_present || tsc_disabled) return; /* @@ -248,7 +252,7 @@ tsc_freq_changed(void *arg, const struct cf_level *level, int status) { /* If there was an error during the transition, don't do anything. */ - if (status != 0) + if (tsc_disabled || status != 0) return; /* Total setting for this level gives the new frequency in MHz. */ -- 2.45.2