xref: /freebsd/sys/x86/cpufreq/p4tcc.c (revision a8de37b0244d1101e634922d018579cc3073318f)
132580301SAttilio Rao /*-
232580301SAttilio Rao  * Copyright (c) 2005 Nate Lawson
332580301SAttilio Rao  * All rights reserved.
432580301SAttilio Rao  *
532580301SAttilio Rao  * Redistribution and use in source and binary forms, with or without
632580301SAttilio Rao  * modification, are permitted provided that the following conditions
732580301SAttilio Rao  * are met:
832580301SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
932580301SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
1032580301SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
1132580301SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
1232580301SAttilio Rao  *    documentation and/or other materials provided with the distribution.
1332580301SAttilio Rao  *
1432580301SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1532580301SAttilio Rao  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1632580301SAttilio Rao  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1732580301SAttilio Rao  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1832580301SAttilio Rao  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
1932580301SAttilio Rao  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2032580301SAttilio Rao  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2132580301SAttilio Rao  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2232580301SAttilio Rao  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2332580301SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2432580301SAttilio Rao  * SUCH DAMAGE.
2532580301SAttilio Rao  */
2632580301SAttilio Rao 
2732580301SAttilio Rao /*
2832580301SAttilio Rao  * Throttle clock frequency by using the thermal control circuit.  This
2932580301SAttilio Rao  * operates independently of SpeedStep and ACPI throttling and is supported
3032580301SAttilio Rao  * on Pentium 4 and later models (feature TM).
3132580301SAttilio Rao  *
3232580301SAttilio Rao  * Reference:  Intel Developer's manual v.3 #245472-012
3332580301SAttilio Rao  *
3432580301SAttilio Rao  * The original version of this driver was written by Ted Unangst for
3532580301SAttilio Rao  * OpenBSD and imported by Maxim Sobolev.  It was rewritten by Nate Lawson
3632580301SAttilio Rao  * for use with the cpufreq framework.
3732580301SAttilio Rao  */
3832580301SAttilio Rao 
3932580301SAttilio Rao #include <sys/cdefs.h>
4032580301SAttilio Rao __FBSDID("$FreeBSD$");
4132580301SAttilio Rao 
4232580301SAttilio Rao #include <sys/param.h>
4332580301SAttilio Rao #include <sys/systm.h>
4432580301SAttilio Rao #include <sys/bus.h>
4532580301SAttilio Rao #include <sys/cpu.h>
4632580301SAttilio Rao #include <sys/kernel.h>
4732580301SAttilio Rao #include <sys/module.h>
4832580301SAttilio Rao 
4932580301SAttilio Rao #include <machine/md_var.h>
5032580301SAttilio Rao #include <machine/specialreg.h>
5132580301SAttilio Rao 
5232580301SAttilio Rao #include "cpufreq_if.h"
5332580301SAttilio Rao 
5432580301SAttilio Rao #include <contrib/dev/acpica/include/acpi.h>
5532580301SAttilio Rao 
5632580301SAttilio Rao #include <dev/acpica/acpivar.h>
5732580301SAttilio Rao #include "acpi_if.h"
5832580301SAttilio Rao 
5932580301SAttilio Rao struct p4tcc_softc {
6032580301SAttilio Rao 	device_t	dev;
6132580301SAttilio Rao 	int		set_count;
6232580301SAttilio Rao 	int		lowest_val;
6332580301SAttilio Rao 	int		auto_mode;
6432580301SAttilio Rao };
6532580301SAttilio Rao 
6632580301SAttilio Rao #define TCC_NUM_SETTINGS	8
6732580301SAttilio Rao 
6832580301SAttilio Rao #define TCC_ENABLE_ONDEMAND	(1<<4)
6932580301SAttilio Rao #define TCC_REG_OFFSET		1
7032580301SAttilio Rao #define TCC_SPEED_PERCENT(x)	((10000 * (x)) / TCC_NUM_SETTINGS)
7132580301SAttilio Rao 
7232580301SAttilio Rao static int	p4tcc_features(driver_t *driver, u_int *features);
7332580301SAttilio Rao static void	p4tcc_identify(driver_t *driver, device_t parent);
7432580301SAttilio Rao static int	p4tcc_probe(device_t dev);
7532580301SAttilio Rao static int	p4tcc_attach(device_t dev);
7632580301SAttilio Rao static int	p4tcc_settings(device_t dev, struct cf_setting *sets,
7732580301SAttilio Rao 		    int *count);
7832580301SAttilio Rao static int	p4tcc_set(device_t dev, const struct cf_setting *set);
7932580301SAttilio Rao static int	p4tcc_get(device_t dev, struct cf_setting *set);
8032580301SAttilio Rao static int	p4tcc_type(device_t dev, int *type);
8132580301SAttilio Rao 
8232580301SAttilio Rao static device_method_t p4tcc_methods[] = {
8332580301SAttilio Rao 	/* Device interface */
8432580301SAttilio Rao 	DEVMETHOD(device_identify,	p4tcc_identify),
8532580301SAttilio Rao 	DEVMETHOD(device_probe,		p4tcc_probe),
8632580301SAttilio Rao 	DEVMETHOD(device_attach,	p4tcc_attach),
8732580301SAttilio Rao 
8832580301SAttilio Rao 	/* cpufreq interface */
8932580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_set,	p4tcc_set),
9032580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_get,	p4tcc_get),
9132580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_type,	p4tcc_type),
9232580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_settings,	p4tcc_settings),
9332580301SAttilio Rao 
9432580301SAttilio Rao 	/* ACPI interface */
9532580301SAttilio Rao 	DEVMETHOD(acpi_get_features,	p4tcc_features),
9632580301SAttilio Rao 
9732580301SAttilio Rao 	{0, 0}
9832580301SAttilio Rao };
9932580301SAttilio Rao 
10032580301SAttilio Rao static driver_t p4tcc_driver = {
10132580301SAttilio Rao 	"p4tcc",
10232580301SAttilio Rao 	p4tcc_methods,
10332580301SAttilio Rao 	sizeof(struct p4tcc_softc),
10432580301SAttilio Rao };
10532580301SAttilio Rao 
10632580301SAttilio Rao static devclass_t p4tcc_devclass;
10732580301SAttilio Rao DRIVER_MODULE(p4tcc, cpu, p4tcc_driver, p4tcc_devclass, 0, 0);
10832580301SAttilio Rao 
10932580301SAttilio Rao static int
11032580301SAttilio Rao p4tcc_features(driver_t *driver, u_int *features)
11132580301SAttilio Rao {
11232580301SAttilio Rao 
11332580301SAttilio Rao 	/* Notify the ACPI CPU that we support direct access to MSRs */
11432580301SAttilio Rao 	*features = ACPI_CAP_THR_MSRS;
11532580301SAttilio Rao 	return (0);
11632580301SAttilio Rao }
11732580301SAttilio Rao 
11832580301SAttilio Rao static void
11932580301SAttilio Rao p4tcc_identify(driver_t *driver, device_t parent)
12032580301SAttilio Rao {
12132580301SAttilio Rao 
12232580301SAttilio Rao 	if ((cpu_feature & (CPUID_ACPI | CPUID_TM)) != (CPUID_ACPI | CPUID_TM))
12332580301SAttilio Rao 		return;
12432580301SAttilio Rao 
12532580301SAttilio Rao 	/* Make sure we're not being doubly invoked. */
12632580301SAttilio Rao 	if (device_find_child(parent, "p4tcc", -1) != NULL)
12732580301SAttilio Rao 		return;
12832580301SAttilio Rao 
12932580301SAttilio Rao 	/*
13032580301SAttilio Rao 	 * We attach a p4tcc child for every CPU since settings need to
13132580301SAttilio Rao 	 * be performed on every CPU in the SMP case.  See section 13.15.3
13232580301SAttilio Rao 	 * of the IA32 Intel Architecture Software Developer's Manual,
13332580301SAttilio Rao 	 * Volume 3, for more info.
13432580301SAttilio Rao 	 */
13532580301SAttilio Rao 	if (BUS_ADD_CHILD(parent, 10, "p4tcc", -1) == NULL)
13632580301SAttilio Rao 		device_printf(parent, "add p4tcc child failed\n");
13732580301SAttilio Rao }
13832580301SAttilio Rao 
13932580301SAttilio Rao static int
14032580301SAttilio Rao p4tcc_probe(device_t dev)
14132580301SAttilio Rao {
142*a8de37b0SEitan Adler 
143*a8de37b0SEitan Adler 	if (resource_disabled("p4tcc", 0))
144*a8de37b0SEitan Adler 		return (ENXIO);
145*a8de37b0SEitan Adler 
14632580301SAttilio Rao 	device_set_desc(dev, "CPU Frequency Thermal Control");
14732580301SAttilio Rao 	return (0);
14832580301SAttilio Rao }
14932580301SAttilio Rao 
15032580301SAttilio Rao static int
15132580301SAttilio Rao p4tcc_attach(device_t dev)
15232580301SAttilio Rao {
15332580301SAttilio Rao 	struct p4tcc_softc *sc;
15432580301SAttilio Rao 	struct cf_setting set;
15532580301SAttilio Rao 
15632580301SAttilio Rao 	sc = device_get_softc(dev);
15732580301SAttilio Rao 	sc->dev = dev;
15832580301SAttilio Rao 	sc->set_count = TCC_NUM_SETTINGS;
15932580301SAttilio Rao 
16032580301SAttilio Rao 	/*
16132580301SAttilio Rao 	 * On boot, the TCC is usually in Automatic mode where reading the
16232580301SAttilio Rao 	 * current performance level is likely to produce bogus results.
16332580301SAttilio Rao 	 * We record that state here and don't trust the contents of the
16432580301SAttilio Rao 	 * status MSR until we've set it ourselves.
16532580301SAttilio Rao 	 */
16632580301SAttilio Rao 	sc->auto_mode = TRUE;
16732580301SAttilio Rao 
16832580301SAttilio Rao 	/*
16932580301SAttilio Rao 	 * XXX: After a cursory glance at various Intel specification
17032580301SAttilio Rao 	 * XXX: updates it seems like these tests for errata is bogus.
17132580301SAttilio Rao 	 * XXX: As far as I can tell, the failure mode is benign, in
17232580301SAttilio Rao 	 * XXX: that cpus with no errata will have their bottom two
17332580301SAttilio Rao 	 * XXX: STPCLK# rates disabled, so rather than waste more time
17432580301SAttilio Rao 	 * XXX: hunting down intel docs, just document it and punt. /phk
17532580301SAttilio Rao 	 */
17632580301SAttilio Rao 	switch (cpu_id & 0xff) {
17732580301SAttilio Rao 	case 0x22:
17832580301SAttilio Rao 	case 0x24:
17932580301SAttilio Rao 	case 0x25:
18032580301SAttilio Rao 	case 0x27:
18132580301SAttilio Rao 	case 0x29:
18232580301SAttilio Rao 		/*
18332580301SAttilio Rao 		 * These CPU models hang when set to 12.5%.
18432580301SAttilio Rao 		 * See Errata O50, P44, and Z21.
18532580301SAttilio Rao 		 */
18632580301SAttilio Rao 		sc->set_count -= 1;
18732580301SAttilio Rao 		break;
18832580301SAttilio Rao 	case 0x07:	/* errata N44 and P18 */
18932580301SAttilio Rao 	case 0x0a:
19032580301SAttilio Rao 	case 0x12:
19132580301SAttilio Rao 	case 0x13:
19232580301SAttilio Rao 	case 0x62:	/* Pentium D B1: errata AA21 */
19332580301SAttilio Rao 	case 0x64:	/* Pentium D C1: errata AA21 */
19432580301SAttilio Rao 	case 0x65:	/* Pentium D D0: errata AA21 */
19532580301SAttilio Rao 		/*
19632580301SAttilio Rao 		 * These CPU models hang when set to 12.5% or 25%.
19732580301SAttilio Rao 		 * See Errata N44, P18l and AA21.
19832580301SAttilio Rao 		 */
19932580301SAttilio Rao 		sc->set_count -= 2;
20032580301SAttilio Rao 		break;
20132580301SAttilio Rao 	}
20232580301SAttilio Rao 	sc->lowest_val = TCC_NUM_SETTINGS - sc->set_count + 1;
20332580301SAttilio Rao 
20432580301SAttilio Rao 	/*
20532580301SAttilio Rao 	 * Before we finish attach, switch to 100%.  It's possible the BIOS
20632580301SAttilio Rao 	 * set us to a lower rate.  The user can override this after boot.
20732580301SAttilio Rao 	 */
20832580301SAttilio Rao 	set.freq = 10000;
20932580301SAttilio Rao 	p4tcc_set(dev, &set);
21032580301SAttilio Rao 
21132580301SAttilio Rao 	cpufreq_register(dev);
21232580301SAttilio Rao 	return (0);
21332580301SAttilio Rao }
21432580301SAttilio Rao 
21532580301SAttilio Rao static int
21632580301SAttilio Rao p4tcc_settings(device_t dev, struct cf_setting *sets, int *count)
21732580301SAttilio Rao {
21832580301SAttilio Rao 	struct p4tcc_softc *sc;
21932580301SAttilio Rao 	int i, val;
22032580301SAttilio Rao 
22132580301SAttilio Rao 	sc = device_get_softc(dev);
22232580301SAttilio Rao 	if (sets == NULL || count == NULL)
22332580301SAttilio Rao 		return (EINVAL);
22432580301SAttilio Rao 	if (*count < sc->set_count)
22532580301SAttilio Rao 		return (E2BIG);
22632580301SAttilio Rao 
22732580301SAttilio Rao 	/* Return a list of valid settings for this driver. */
22832580301SAttilio Rao 	memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * sc->set_count);
22932580301SAttilio Rao 	val = TCC_NUM_SETTINGS;
23032580301SAttilio Rao 	for (i = 0; i < sc->set_count; i++, val--) {
23132580301SAttilio Rao 		sets[i].freq = TCC_SPEED_PERCENT(val);
23232580301SAttilio Rao 		sets[i].dev = dev;
23332580301SAttilio Rao 	}
23432580301SAttilio Rao 	*count = sc->set_count;
23532580301SAttilio Rao 
23632580301SAttilio Rao 	return (0);
23732580301SAttilio Rao }
23832580301SAttilio Rao 
23932580301SAttilio Rao static int
24032580301SAttilio Rao p4tcc_set(device_t dev, const struct cf_setting *set)
24132580301SAttilio Rao {
24232580301SAttilio Rao 	struct p4tcc_softc *sc;
24332580301SAttilio Rao 	uint64_t mask, msr;
24432580301SAttilio Rao 	int val;
24532580301SAttilio Rao 
24632580301SAttilio Rao 	if (set == NULL)
24732580301SAttilio Rao 		return (EINVAL);
24832580301SAttilio Rao 	sc = device_get_softc(dev);
24932580301SAttilio Rao 
25032580301SAttilio Rao 	/*
25132580301SAttilio Rao 	 * Validate requested state converts to a setting that is an integer
25232580301SAttilio Rao 	 * from [sc->lowest_val .. TCC_NUM_SETTINGS].
25332580301SAttilio Rao 	 */
25432580301SAttilio Rao 	val = set->freq * TCC_NUM_SETTINGS / 10000;
25532580301SAttilio Rao 	if (val * 10000 != set->freq * TCC_NUM_SETTINGS ||
25632580301SAttilio Rao 	    val < sc->lowest_val || val > TCC_NUM_SETTINGS)
25732580301SAttilio Rao 		return (EINVAL);
25832580301SAttilio Rao 
25932580301SAttilio Rao 	/*
26032580301SAttilio Rao 	 * Read the current register and mask off the old setting and
26132580301SAttilio Rao 	 * On-Demand bit.  If the new val is < 100%, set it and the On-Demand
26232580301SAttilio Rao 	 * bit, otherwise just return to Automatic mode.
26332580301SAttilio Rao 	 */
26432580301SAttilio Rao 	msr = rdmsr(MSR_THERM_CONTROL);
26532580301SAttilio Rao 	mask = (TCC_NUM_SETTINGS - 1) << TCC_REG_OFFSET;
26632580301SAttilio Rao 	msr &= ~(mask | TCC_ENABLE_ONDEMAND);
26732580301SAttilio Rao 	if (val < TCC_NUM_SETTINGS)
26832580301SAttilio Rao 		msr |= (val << TCC_REG_OFFSET) | TCC_ENABLE_ONDEMAND;
26932580301SAttilio Rao 	wrmsr(MSR_THERM_CONTROL, msr);
27032580301SAttilio Rao 
27132580301SAttilio Rao 	/*
27232580301SAttilio Rao 	 * Record whether we're now in Automatic or On-Demand mode.  We have
27332580301SAttilio Rao 	 * to cache this since there is no reliable way to check if TCC is in
27432580301SAttilio Rao 	 * Automatic mode (i.e., at 100% or possibly 50%).  Reading bit 4 of
27532580301SAttilio Rao 	 * the ACPI Thermal Monitor Control Register produces 0 no matter
27632580301SAttilio Rao 	 * what the current mode.
27732580301SAttilio Rao 	 */
27832580301SAttilio Rao 	if (msr & TCC_ENABLE_ONDEMAND)
27932580301SAttilio Rao 		sc->auto_mode = FALSE;
2802f42a9bfSAlexander Kabaev 	else
2812f42a9bfSAlexander Kabaev 		sc->auto_mode = TRUE;
28232580301SAttilio Rao 
28332580301SAttilio Rao 	return (0);
28432580301SAttilio Rao }
28532580301SAttilio Rao 
28632580301SAttilio Rao static int
28732580301SAttilio Rao p4tcc_get(device_t dev, struct cf_setting *set)
28832580301SAttilio Rao {
28932580301SAttilio Rao 	struct p4tcc_softc *sc;
29032580301SAttilio Rao 	uint64_t msr;
29132580301SAttilio Rao 	int val;
29232580301SAttilio Rao 
29332580301SAttilio Rao 	if (set == NULL)
29432580301SAttilio Rao 		return (EINVAL);
29532580301SAttilio Rao 	sc = device_get_softc(dev);
29632580301SAttilio Rao 
29732580301SAttilio Rao 	/*
29832580301SAttilio Rao 	 * Read the current register and extract the current setting.  If
29932580301SAttilio Rao 	 * in automatic mode, assume we're at TCC_NUM_SETTINGS (100%).
30032580301SAttilio Rao 	 *
30132580301SAttilio Rao 	 * XXX This is not completely reliable since at high temperatures
30232580301SAttilio Rao 	 * the CPU may be automatically throttling to 50% but it's the best
30332580301SAttilio Rao 	 * we can do.
30432580301SAttilio Rao 	 */
30532580301SAttilio Rao 	if (!sc->auto_mode) {
30632580301SAttilio Rao 		msr = rdmsr(MSR_THERM_CONTROL);
30732580301SAttilio Rao 		val = (msr >> TCC_REG_OFFSET) & (TCC_NUM_SETTINGS - 1);
30832580301SAttilio Rao 	} else
30932580301SAttilio Rao 		val = TCC_NUM_SETTINGS;
31032580301SAttilio Rao 
31132580301SAttilio Rao 	memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
31232580301SAttilio Rao 	set->freq = TCC_SPEED_PERCENT(val);
31332580301SAttilio Rao 	set->dev = dev;
31432580301SAttilio Rao 
31532580301SAttilio Rao 	return (0);
31632580301SAttilio Rao }
31732580301SAttilio Rao 
31832580301SAttilio Rao static int
31932580301SAttilio Rao p4tcc_type(device_t dev, int *type)
32032580301SAttilio Rao {
32132580301SAttilio Rao 
32232580301SAttilio Rao 	if (type == NULL)
32332580301SAttilio Rao 		return (EINVAL);
32432580301SAttilio Rao 
32532580301SAttilio Rao 	*type = CPUFREQ_TYPE_RELATIVE;
32632580301SAttilio Rao 	return (0);
32732580301SAttilio Rao }
328