tsc.c revision 219461
14Srgrimes/*-
2110379Sphk * Copyright (c) 1998-2003 Poul-Henning Kamp
34Srgrimes * All rights reserved.
44Srgrimes *
54Srgrimes * Redistribution and use in source and binary forms, with or without
64Srgrimes * modification, are permitted provided that the following conditions
74Srgrimes * are met:
84Srgrimes * 1. Redistributions of source code must retain the above copyright
94Srgrimes *    notice, this list of conditions and the following disclaimer.
104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
114Srgrimes *    notice, this list of conditions and the following disclaimer in the
124Srgrimes *    documentation and/or other materials provided with the distribution.
134Srgrimes *
14110379Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17110379Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244Srgrimes * SUCH DAMAGE.
254Srgrimes */
264Srgrimes
27115683Sobrien#include <sys/cdefs.h>
28115683Sobrien__FBSDID("$FreeBSD: head/sys/x86/x86/tsc.c 219461 2011-03-10 20:02:58Z jkim $");
29115683Sobrien
3016299Spst#include "opt_clock.h"
3113228Swollman
322056Swollman#include <sys/param.h>
33167905Snjl#include <sys/bus.h>
34167905Snjl#include <sys/cpu.h>
35167905Snjl#include <sys/malloc.h>
362056Swollman#include <sys/systm.h>
37113348Sdes#include <sys/sysctl.h>
382056Swollman#include <sys/time.h>
3958377Sphk#include <sys/timetc.h>
402056Swollman#include <sys/kernel.h>
4185835Siwasaki#include <sys/power.h>
42113348Sdes#include <sys/smp.h>
434180Sbde#include <machine/clock.h>
44216272Sjkim#include <machine/cputypes.h>
4532054Sphk#include <machine/md_var.h>
4632054Sphk#include <machine/specialreg.h>
4715508Sbde
48167905Snjl#include "cpufreq_if.h"
49167905Snjl
50216163Sjkimuint64_t	tsc_freq;
51184102Sjkimint		tsc_is_invariant;
52216279Sjkimint		tsc_present;
53167905Snjlstatic eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag;
541390Ssos
55184102SjkimSYSCTL_INT(_kern_timecounter, OID_AUTO, invariant_tsc, CTLFLAG_RDTUN,
56184108Sjkim    &tsc_is_invariant, 0, "Indicates whether the TSC is P-state invariant");
57184108SjkimTUNABLE_INT("kern.timecounter.invariant_tsc", &tsc_is_invariant);
58184102Sjkim
59113348Sdes#ifdef SMP
60113348Sdesstatic int	smp_tsc;
61121307SsilbySYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0,
62113348Sdes    "Indicates whether the TSC is safe to use in SMP mode");
63113348SdesTUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
64113348Sdes#endif
65113348Sdes
66167905Snjlstatic void tsc_freq_changed(void *arg, const struct cf_level *level,
67167905Snjl    int status);
68167905Snjlstatic void tsc_freq_changing(void *arg, const struct cf_level *level,
69167905Snjl    int *status);
7092765Salfredstatic	unsigned tsc_get_timecount(struct timecounter *tc);
71167905Snjlstatic void tsc_levels_changed(void *arg, int unit);
7217353Sbde
7340610Sphkstatic struct timecounter tsc_timecounter = {
7433690Sphk	tsc_get_timecount,	/* get_timecount */
7536741Sphk	0,			/* no poll_pps */
76167905Snjl	~0u,			/* counter_mask */
7733690Sphk	0,			/* frequency */
78167905Snjl	"TSC",			/* name */
79118987Sphk	800,			/* quality (adjusted in code) */
8033690Sphk};
8133690Sphk
821390Ssosvoid
83110370Sphkinit_TSC(void)
841390Ssos{
85110370Sphk	u_int64_t tscval[2];
861390Ssos
8732054Sphk	if (cpu_feature & CPUID_TSC)
8832054Sphk		tsc_present = 1;
8932054Sphk	else
9032054Sphk		tsc_present = 0;
9132054Sphk
92110370Sphk	if (!tsc_present)
93110370Sphk		return;
9415508Sbde
95110370Sphk	if (bootverbose)
96110370Sphk	        printf("Calibrating TSC clock ... ");
9715508Sbde
98110370Sphk	tscval[0] = rdtsc();
99110370Sphk	DELAY(1000000);
100110370Sphk	tscval[1] = rdtsc();
10115508Sbde
102110370Sphk	tsc_freq = tscval[1] - tscval[0];
103110370Sphk	if (bootverbose)
104110370Sphk		printf("TSC clock: %ju Hz\n", (intmax_t)tsc_freq);
105167905Snjl
106216272Sjkim	switch (cpu_vendor_id) {
107216272Sjkim	case CPU_VENDOR_AMD:
108216272Sjkim		if ((amd_pminfo & AMDPM_TSC_INVARIANT) ||
109216337Sjkim		    CPUID_TO_FAMILY(cpu_id) >= 0x10)
110216272Sjkim			tsc_is_invariant = 1;
111216272Sjkim		break;
112216272Sjkim	case CPU_VENDOR_INTEL:
113216272Sjkim		if ((amd_pminfo & AMDPM_TSC_INVARIANT) ||
114216272Sjkim		    (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
115216272Sjkim		    CPUID_TO_MODEL(cpu_id) >= 0xe) ||
116216272Sjkim		    (CPUID_TO_FAMILY(cpu_id) == 0xf &&
117216272Sjkim		    CPUID_TO_MODEL(cpu_id) >= 0x3))
118216272Sjkim			tsc_is_invariant = 1;
119216272Sjkim		break;
120216272Sjkim	case CPU_VENDOR_CENTAUR:
121216272Sjkim		if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
122216272Sjkim		    CPUID_TO_MODEL(cpu_id) >= 0xf &&
123216272Sjkim		    (rdmsr(0x1203) & 0x100000000ULL) == 0)
124216272Sjkim			tsc_is_invariant = 1;
125216272Sjkim		break;
126216272Sjkim	}
127216272Sjkim
128167905Snjl	/*
129216274Sjkim	 * Inform CPU accounting about our boot-time clock rate.  This will
130216274Sjkim	 * be updated if someone loads a cpufreq driver after boot that
131216274Sjkim	 * discovers a new max frequency.
132167905Snjl	 */
133155534Sphk	set_cputicker(rdtsc, tsc_freq, 1);
134167905Snjl
135216274Sjkim	if (tsc_is_invariant)
136216274Sjkim		return;
137216274Sjkim
138167905Snjl	/* Register to find out about changes in CPU frequency. */
139184108Sjkim	tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
140184108Sjkim	    tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST);
141167905Snjl	tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
142167905Snjl	    tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST);
143167905Snjl	tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed,
144167905Snjl	    tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY);
145118550Sphk}
14634617Sphk
147118550Sphkvoid
148118550Sphkinit_TSC_tc(void)
149118550Sphk{
150209103Smav
151209103Smav	if (!tsc_present)
152209103Smav		return;
153209103Smav
15434617Sphk	/*
155160964Syar	 * We can not use the TSC if we support APM.  Precise timekeeping
15649186Smsmith	 * on an APM'ed machine is at best a fools pursuit, since
15734617Sphk	 * any and all of the time spent in various SMM code can't
15834617Sphk	 * be reliably accounted for.  Reading the RTC is your only
159160964Syar	 * source of reliable time info.  The i8254 loses too, of course,
16034617Sphk	 * but we need to have some kind of time...
16149186Smsmith	 * We don't know at this point whether APM is going to be used
16249186Smsmith	 * or not, nor when it might be activated.  Play it safe.
16334617Sphk	 */
16485835Siwasaki	if (power_pm_get_type() == POWER_PM_TYPE_APM) {
165118987Sphk		tsc_timecounter.tc_quality = -1000;
16685835Siwasaki		if (bootverbose)
167110370Sphk			printf("TSC timecounter disabled: APM enabled.\n");
16864031Sphk	}
16934617Sphk
170118987Sphk#ifdef SMP
171118987Sphk	/*
172118987Sphk	 * We can not use the TSC in SMP mode unless the TSCs on all CPUs
173118987Sphk	 * are somehow synchronized.  Some hardware configurations do
174118987Sphk	 * this, but we have no way of determining whether this is the
175118987Sphk	 * case, so we do not use the TSC in multi-processor systems
176118987Sphk	 * unless the user indicated (by setting kern.timecounter.smp_tsc
177118987Sphk	 * to 1) that he believes that his TSCs are synchronized.
178118987Sphk	 */
179118987Sphk	if (mp_ncpus > 1 && !smp_tsc)
180118987Sphk		tsc_timecounter.tc_quality = -100;
181118987Sphk#endif
182118987Sphk
183219461Sjkim	if (tsc_freq != 0) {
18440610Sphk		tsc_timecounter.tc_frequency = tsc_freq;
18558377Sphk		tc_init(&tsc_timecounter);
18633690Sphk	}
1874Srgrimes}
1884Srgrimes
189167905Snjl/*
190167905Snjl * When cpufreq levels change, find out about the (new) max frequency.  We
191167905Snjl * use this to update CPU accounting in case it got a lower estimate at boot.
192167905Snjl */
193167905Snjlstatic void
194167905Snjltsc_levels_changed(void *arg, int unit)
195167905Snjl{
196167905Snjl	device_t cf_dev;
197167905Snjl	struct cf_level *levels;
198167905Snjl	int count, error;
199167905Snjl	uint64_t max_freq;
200167905Snjl
201167905Snjl	/* Only use values from the first CPU, assuming all are equal. */
202167905Snjl	if (unit != 0)
203167905Snjl		return;
204167905Snjl
205167905Snjl	/* Find the appropriate cpufreq device instance. */
206167905Snjl	cf_dev = devclass_get_device(devclass_find("cpufreq"), unit);
207167905Snjl	if (cf_dev == NULL) {
208167905Snjl		printf("tsc_levels_changed() called but no cpufreq device?\n");
209167905Snjl		return;
210167905Snjl	}
211167905Snjl
212167905Snjl	/* Get settings from the device and find the max frequency. */
213167905Snjl	count = 64;
214167905Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
215167905Snjl	if (levels == NULL)
216167905Snjl		return;
217167905Snjl	error = CPUFREQ_LEVELS(cf_dev, levels, &count);
218167905Snjl	if (error == 0 && count != 0) {
219167905Snjl		max_freq = (uint64_t)levels[0].total_set.freq * 1000000;
220167905Snjl		set_cputicker(rdtsc, max_freq, 1);
221167905Snjl	} else
222167905Snjl		printf("tsc_levels_changed: no max freq found\n");
223167905Snjl	free(levels, M_TEMP);
224167905Snjl}
225167905Snjl
226167905Snjl/*
227167905Snjl * If the TSC timecounter is in use, veto the pending change.  It may be
228167905Snjl * possible in the future to handle a dynamically-changing timecounter rate.
229167905Snjl */
230167905Snjlstatic void
231167905Snjltsc_freq_changing(void *arg, const struct cf_level *level, int *status)
232167905Snjl{
233167905Snjl
234216274Sjkim	if (*status != 0 || timecounter != &tsc_timecounter)
235167905Snjl		return;
236167905Snjl
237167905Snjl	printf("timecounter TSC must not be in use when "
238184102Sjkim	    "changing frequencies; change denied\n");
239167905Snjl	*status = EBUSY;
240167905Snjl}
241167905Snjl
242167905Snjl/* Update TSC freq with the value indicated by the caller. */
243167905Snjlstatic void
244167905Snjltsc_freq_changed(void *arg, const struct cf_level *level, int status)
245167905Snjl{
246216276Sjkim
247216276Sjkim	/* If there was an error during the transition, don't do anything. */
248216274Sjkim	if (status != 0)
249167905Snjl		return;
250167905Snjl
251167905Snjl	/* Total setting for this level gives the new frequency in MHz. */
252167905Snjl	tsc_freq = (uint64_t)level->total_set.freq * 1000000;
253167905Snjl	tsc_timecounter.tc_frequency = tsc_freq;
254167905Snjl}
255167905Snjl
25615508Sbdestatic int
25762573Sphksysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
25815508Sbde{
25915508Sbde	int error;
260110039Sphk	uint64_t freq;
26115508Sbde
26248888Sbde	if (tsc_timecounter.tc_frequency == 0)
26315508Sbde		return (EOPNOTSUPP);
26432005Sphk	freq = tsc_freq;
265217616Smdf	error = sysctl_handle_64(oidp, &freq, 0, req);
26633690Sphk	if (error == 0 && req->newptr != NULL) {
26733690Sphk		tsc_freq = freq;
26840610Sphk		tsc_timecounter.tc_frequency = tsc_freq;
26933690Sphk	}
27015508Sbde	return (error);
27115508Sbde}
27215508Sbde
273217616SmdfSYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_U64 | CTLFLAG_RW,
274211082Sdwmalone    0, 0, sysctl_machdep_tsc_freq, "QU", "");
27533690Sphk
27636441Sphkstatic unsigned
27736719Sphktsc_get_timecount(struct timecounter *tc)
27833690Sphk{
27936198Sphk	return (rdtsc());
28033690Sphk}
281