tsc.c revision 216276
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/i386/i386/tsc.c 216276 2010-12-07 22:43:25Z 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;
51110370Sphkint		tsc_is_broken;
52184102Sjkimint		tsc_is_invariant;
53110370Sphku_int		tsc_present;
54167905Snjlstatic eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag;
551390Ssos
56184102SjkimSYSCTL_INT(_kern_timecounter, OID_AUTO, invariant_tsc, CTLFLAG_RDTUN,
57184108Sjkim    &tsc_is_invariant, 0, "Indicates whether the TSC is P-state invariant");
58184108SjkimTUNABLE_INT("kern.timecounter.invariant_tsc", &tsc_is_invariant);
59184102Sjkim
60113348Sdes#ifdef SMP
61113348Sdesstatic int	smp_tsc;
62121307SsilbySYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0,
63113348Sdes    "Indicates whether the TSC is safe to use in SMP mode");
64113348SdesTUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
65113348Sdes#endif
66113348Sdes
67167905Snjlstatic void tsc_freq_changed(void *arg, const struct cf_level *level,
68167905Snjl    int status);
69167905Snjlstatic void tsc_freq_changing(void *arg, const struct cf_level *level,
70167905Snjl    int *status);
7192765Salfredstatic	unsigned tsc_get_timecount(struct timecounter *tc);
72167905Snjlstatic void tsc_levels_changed(void *arg, int unit);
7317353Sbde
7440610Sphkstatic struct timecounter tsc_timecounter = {
7533690Sphk	tsc_get_timecount,	/* get_timecount */
7636741Sphk	0,			/* no poll_pps */
77167905Snjl	~0u,			/* counter_mask */
7833690Sphk	0,			/* frequency */
79167905Snjl	"TSC",			/* name */
80118987Sphk	800,			/* quality (adjusted in code) */
8133690Sphk};
8233690Sphk
831390Ssosvoid
84110370Sphkinit_TSC(void)
851390Ssos{
86110370Sphk	u_int64_t tscval[2];
871390Ssos
8832054Sphk	if (cpu_feature & CPUID_TSC)
8932054Sphk		tsc_present = 1;
9032054Sphk	else
9132054Sphk		tsc_present = 0;
9232054Sphk
93110370Sphk	if (!tsc_present)
94110370Sphk		return;
9515508Sbde
96110370Sphk	if (bootverbose)
97110370Sphk	        printf("Calibrating TSC clock ... ");
9815508Sbde
99110370Sphk	tscval[0] = rdtsc();
100110370Sphk	DELAY(1000000);
101110370Sphk	tscval[1] = rdtsc();
10215508Sbde
103110370Sphk	tsc_freq = tscval[1] - tscval[0];
104110370Sphk	if (bootverbose)
105110370Sphk		printf("TSC clock: %ju Hz\n", (intmax_t)tsc_freq);
106167905Snjl
107216272Sjkim	switch (cpu_vendor_id) {
108216272Sjkim	case CPU_VENDOR_AMD:
109216272Sjkim		if ((amd_pminfo & AMDPM_TSC_INVARIANT) ||
110216272Sjkim		    CPUID_TO_FAMILY(cpu_id) >= 0x10 || cpu_id == 0x60fb2)
111216272Sjkim			tsc_is_invariant = 1;
112216272Sjkim		break;
113216272Sjkim	case CPU_VENDOR_INTEL:
114216272Sjkim		if ((amd_pminfo & AMDPM_TSC_INVARIANT) ||
115216272Sjkim		    (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
116216272Sjkim		    CPUID_TO_MODEL(cpu_id) >= 0xe) ||
117216272Sjkim		    (CPUID_TO_FAMILY(cpu_id) == 0xf &&
118216272Sjkim		    CPUID_TO_MODEL(cpu_id) >= 0x3))
119216272Sjkim			tsc_is_invariant = 1;
120216272Sjkim		break;
121216272Sjkim	case CPU_VENDOR_CENTAUR:
122216272Sjkim		if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
123216272Sjkim		    CPUID_TO_MODEL(cpu_id) >= 0xf &&
124216272Sjkim		    (rdmsr(0x1203) & 0x100000000ULL) == 0)
125216272Sjkim			tsc_is_invariant = 1;
126216272Sjkim		break;
127216272Sjkim	}
128216272Sjkim
129167905Snjl	/*
130216274Sjkim	 * Inform CPU accounting about our boot-time clock rate.  This will
131216274Sjkim	 * be updated if someone loads a cpufreq driver after boot that
132216274Sjkim	 * discovers a new max frequency.
133167905Snjl	 */
134155534Sphk	set_cputicker(rdtsc, tsc_freq, 1);
135167905Snjl
136216274Sjkim	if (tsc_is_invariant)
137216274Sjkim		return;
138216274Sjkim
139167905Snjl	/* Register to find out about changes in CPU frequency. */
140184108Sjkim	tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
141184108Sjkim	    tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST);
142167905Snjl	tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
143167905Snjl	    tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST);
144167905Snjl	tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed,
145167905Snjl	    tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY);
146118550Sphk}
14734617Sphk
148118550Sphkvoid
149118550Sphkinit_TSC_tc(void)
150118550Sphk{
151209103Smav
152209103Smav	if (!tsc_present)
153209103Smav		return;
154209103Smav
15534617Sphk	/*
156160964Syar	 * We can not use the TSC if we support APM.  Precise timekeeping
15749186Smsmith	 * on an APM'ed machine is at best a fools pursuit, since
15834617Sphk	 * any and all of the time spent in various SMM code can't
15934617Sphk	 * be reliably accounted for.  Reading the RTC is your only
160160964Syar	 * source of reliable time info.  The i8254 loses too, of course,
16134617Sphk	 * but we need to have some kind of time...
16249186Smsmith	 * We don't know at this point whether APM is going to be used
16349186Smsmith	 * or not, nor when it might be activated.  Play it safe.
16434617Sphk	 */
16585835Siwasaki	if (power_pm_get_type() == POWER_PM_TYPE_APM) {
166118987Sphk		tsc_timecounter.tc_quality = -1000;
16785835Siwasaki		if (bootverbose)
168110370Sphk			printf("TSC timecounter disabled: APM enabled.\n");
16964031Sphk	}
17034617Sphk
171118987Sphk#ifdef SMP
172118987Sphk	/*
173118987Sphk	 * We can not use the TSC in SMP mode unless the TSCs on all CPUs
174118987Sphk	 * are somehow synchronized.  Some hardware configurations do
175118987Sphk	 * this, but we have no way of determining whether this is the
176118987Sphk	 * case, so we do not use the TSC in multi-processor systems
177118987Sphk	 * unless the user indicated (by setting kern.timecounter.smp_tsc
178118987Sphk	 * to 1) that he believes that his TSCs are synchronized.
179118987Sphk	 */
180118987Sphk	if (mp_ncpus > 1 && !smp_tsc)
181118987Sphk		tsc_timecounter.tc_quality = -100;
182118987Sphk#endif
183118987Sphk
184209103Smav	if (tsc_freq != 0 && !tsc_is_broken) {
18540610Sphk		tsc_timecounter.tc_frequency = tsc_freq;
18658377Sphk		tc_init(&tsc_timecounter);
18733690Sphk	}
1884Srgrimes}
1894Srgrimes
190167905Snjl/*
191167905Snjl * When cpufreq levels change, find out about the (new) max frequency.  We
192167905Snjl * use this to update CPU accounting in case it got a lower estimate at boot.
193167905Snjl */
194167905Snjlstatic void
195167905Snjltsc_levels_changed(void *arg, int unit)
196167905Snjl{
197167905Snjl	device_t cf_dev;
198167905Snjl	struct cf_level *levels;
199167905Snjl	int count, error;
200167905Snjl	uint64_t max_freq;
201167905Snjl
202167905Snjl	/* Only use values from the first CPU, assuming all are equal. */
203167905Snjl	if (unit != 0)
204167905Snjl		return;
205167905Snjl
206167905Snjl	/* Find the appropriate cpufreq device instance. */
207167905Snjl	cf_dev = devclass_get_device(devclass_find("cpufreq"), unit);
208167905Snjl	if (cf_dev == NULL) {
209167905Snjl		printf("tsc_levels_changed() called but no cpufreq device?\n");
210167905Snjl		return;
211167905Snjl	}
212167905Snjl
213167905Snjl	/* Get settings from the device and find the max frequency. */
214167905Snjl	count = 64;
215167905Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
216167905Snjl	if (levels == NULL)
217167905Snjl		return;
218167905Snjl	error = CPUFREQ_LEVELS(cf_dev, levels, &count);
219167905Snjl	if (error == 0 && count != 0) {
220167905Snjl		max_freq = (uint64_t)levels[0].total_set.freq * 1000000;
221167905Snjl		set_cputicker(rdtsc, max_freq, 1);
222167905Snjl	} else
223167905Snjl		printf("tsc_levels_changed: no max freq found\n");
224167905Snjl	free(levels, M_TEMP);
225167905Snjl}
226167905Snjl
227167905Snjl/*
228167905Snjl * If the TSC timecounter is in use, veto the pending change.  It may be
229167905Snjl * possible in the future to handle a dynamically-changing timecounter rate.
230167905Snjl */
231167905Snjlstatic void
232167905Snjltsc_freq_changing(void *arg, const struct cf_level *level, int *status)
233167905Snjl{
234167905Snjl
235216274Sjkim	if (*status != 0 || timecounter != &tsc_timecounter)
236167905Snjl		return;
237167905Snjl
238167905Snjl	printf("timecounter TSC must not be in use when "
239184102Sjkim	    "changing frequencies; change denied\n");
240167905Snjl	*status = EBUSY;
241167905Snjl}
242167905Snjl
243167905Snjl/* Update TSC freq with the value indicated by the caller. */
244167905Snjlstatic void
245167905Snjltsc_freq_changed(void *arg, const struct cf_level *level, int status)
246167905Snjl{
247216276Sjkim
248216276Sjkim	/* If there was an error during the transition, don't do anything. */
249216274Sjkim	if (status != 0)
250167905Snjl		return;
251167905Snjl
252167905Snjl	/* Total setting for this level gives the new frequency in MHz. */
253167905Snjl	tsc_freq = (uint64_t)level->total_set.freq * 1000000;
254167905Snjl	tsc_timecounter.tc_frequency = tsc_freq;
255167905Snjl}
256167905Snjl
25715508Sbdestatic int
25862573Sphksysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
25915508Sbde{
26015508Sbde	int error;
261110039Sphk	uint64_t freq;
26215508Sbde
26348888Sbde	if (tsc_timecounter.tc_frequency == 0)
26415508Sbde		return (EOPNOTSUPP);
26532005Sphk	freq = tsc_freq;
266170289Sdwmalone	error = sysctl_handle_quad(oidp, &freq, 0, req);
26733690Sphk	if (error == 0 && req->newptr != NULL) {
26833690Sphk		tsc_freq = freq;
26940610Sphk		tsc_timecounter.tc_frequency = tsc_freq;
27033690Sphk	}
27115508Sbde	return (error);
27215508Sbde}
27315508Sbde
274110039SphkSYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_QUAD | CTLFLAG_RW,
275211082Sdwmalone    0, 0, sysctl_machdep_tsc_freq, "QU", "");
27633690Sphk
27736441Sphkstatic unsigned
27836719Sphktsc_get_timecount(struct timecounter *tc)
27933690Sphk{
28036198Sphk	return (rdtsc());
28133690Sphk}
282