xref: /freebsd/sys/x86/cpufreq/p4tcc.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
132580301SAttilio Rao /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni  *
432580301SAttilio Rao  * Copyright (c) 2005 Nate Lawson
532580301SAttilio Rao  * All rights reserved.
632580301SAttilio Rao  *
732580301SAttilio Rao  * Redistribution and use in source and binary forms, with or without
832580301SAttilio Rao  * modification, are permitted provided that the following conditions
932580301SAttilio Rao  * are met:
1032580301SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
1132580301SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
1232580301SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
1332580301SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
1432580301SAttilio Rao  *    documentation and/or other materials provided with the distribution.
1532580301SAttilio Rao  *
1632580301SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1732580301SAttilio Rao  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1832580301SAttilio Rao  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1932580301SAttilio Rao  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2032580301SAttilio Rao  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2132580301SAttilio Rao  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2232580301SAttilio Rao  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2332580301SAttilio Rao  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2432580301SAttilio Rao  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2532580301SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2632580301SAttilio Rao  * SUCH DAMAGE.
2732580301SAttilio Rao  */
2832580301SAttilio Rao 
2932580301SAttilio Rao /*
3032580301SAttilio Rao  * Throttle clock frequency by using the thermal control circuit.  This
3132580301SAttilio Rao  * operates independently of SpeedStep and ACPI throttling and is supported
3232580301SAttilio Rao  * on Pentium 4 and later models (feature TM).
3332580301SAttilio Rao  *
3432580301SAttilio Rao  * Reference:  Intel Developer's manual v.3 #245472-012
3532580301SAttilio Rao  *
3632580301SAttilio Rao  * The original version of this driver was written by Ted Unangst for
3732580301SAttilio Rao  * OpenBSD and imported by Maxim Sobolev.  It was rewritten by Nate Lawson
3832580301SAttilio Rao  * for use with the cpufreq framework.
3932580301SAttilio Rao  */
4032580301SAttilio Rao 
4132580301SAttilio Rao #include <sys/param.h>
4232580301SAttilio Rao #include <sys/systm.h>
4332580301SAttilio Rao #include <sys/bus.h>
4432580301SAttilio Rao #include <sys/cpu.h>
4532580301SAttilio Rao #include <sys/kernel.h>
4632580301SAttilio Rao #include <sys/module.h>
4732580301SAttilio Rao 
4832580301SAttilio Rao #include <machine/md_var.h>
4932580301SAttilio Rao #include <machine/specialreg.h>
5032580301SAttilio Rao 
5132580301SAttilio Rao #include "cpufreq_if.h"
5232580301SAttilio Rao 
5332580301SAttilio Rao #include <contrib/dev/acpica/include/acpi.h>
5432580301SAttilio Rao 
5532580301SAttilio Rao #include <dev/acpica/acpivar.h>
5632580301SAttilio Rao #include "acpi_if.h"
5732580301SAttilio Rao 
5832580301SAttilio Rao struct p4tcc_softc {
5932580301SAttilio Rao 	device_t	dev;
6032580301SAttilio Rao 	int		set_count;
6132580301SAttilio Rao 	int		lowest_val;
6232580301SAttilio Rao 	int		auto_mode;
6332580301SAttilio Rao };
6432580301SAttilio Rao 
6532580301SAttilio Rao #define TCC_NUM_SETTINGS	8
6632580301SAttilio Rao 
6732580301SAttilio Rao #define TCC_ENABLE_ONDEMAND	(1<<4)
6832580301SAttilio Rao #define TCC_REG_OFFSET		1
6932580301SAttilio Rao #define TCC_SPEED_PERCENT(x)	((10000 * (x)) / TCC_NUM_SETTINGS)
7032580301SAttilio Rao 
7132580301SAttilio Rao static int	p4tcc_features(driver_t *driver, u_int *features);
7232580301SAttilio Rao static void	p4tcc_identify(driver_t *driver, device_t parent);
7332580301SAttilio Rao static int	p4tcc_probe(device_t dev);
7432580301SAttilio Rao static int	p4tcc_attach(device_t dev);
7546b29ff9SHiren Panchasara static int	p4tcc_detach(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),
8746b29ff9SHiren Panchasara 	DEVMETHOD(device_detach,	p4tcc_detach),
8832580301SAttilio Rao 
8932580301SAttilio Rao 	/* cpufreq interface */
9032580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_set,	p4tcc_set),
9132580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_get,	p4tcc_get),
9232580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_type,	p4tcc_type),
9332580301SAttilio Rao 	DEVMETHOD(cpufreq_drv_settings,	p4tcc_settings),
9432580301SAttilio Rao 
9532580301SAttilio Rao 	/* ACPI interface */
9632580301SAttilio Rao 	DEVMETHOD(acpi_get_features,	p4tcc_features),
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 
106b3407dccSJohn Baldwin DRIVER_MODULE(p4tcc, cpu, p4tcc_driver, 0, 0);
10732580301SAttilio Rao 
10832580301SAttilio Rao static int
p4tcc_features(driver_t * driver,u_int * features)10932580301SAttilio Rao p4tcc_features(driver_t *driver, u_int *features)
11032580301SAttilio Rao {
11132580301SAttilio Rao 
11232580301SAttilio Rao 	/* Notify the ACPI CPU that we support direct access to MSRs */
11332580301SAttilio Rao 	*features = ACPI_CAP_THR_MSRS;
11432580301SAttilio Rao 	return (0);
11532580301SAttilio Rao }
11632580301SAttilio Rao 
11732580301SAttilio Rao static void
p4tcc_identify(driver_t * driver,device_t parent)11832580301SAttilio Rao p4tcc_identify(driver_t *driver, device_t parent)
11932580301SAttilio Rao {
12032580301SAttilio Rao 
12132580301SAttilio Rao 	if ((cpu_feature & (CPUID_ACPI | CPUID_TM)) != (CPUID_ACPI | CPUID_TM))
12232580301SAttilio Rao 		return;
12332580301SAttilio Rao 
12432580301SAttilio Rao 	/* Make sure we're not being doubly invoked. */
12532580301SAttilio Rao 	if (device_find_child(parent, "p4tcc", -1) != NULL)
12632580301SAttilio Rao 		return;
12732580301SAttilio Rao 
12832580301SAttilio Rao 	/*
12932580301SAttilio Rao 	 * We attach a p4tcc child for every CPU since settings need to
13032580301SAttilio Rao 	 * be performed on every CPU in the SMP case.  See section 13.15.3
13132580301SAttilio Rao 	 * of the IA32 Intel Architecture Software Developer's Manual,
13232580301SAttilio Rao 	 * Volume 3, for more info.
13332580301SAttilio Rao 	 */
134d3a8f98aSAlexander Motin 	if (BUS_ADD_CHILD(parent, 10, "p4tcc", device_get_unit(parent))
135d3a8f98aSAlexander Motin 	    == NULL)
13632580301SAttilio Rao 		device_printf(parent, "add p4tcc child failed\n");
13732580301SAttilio Rao }
13832580301SAttilio Rao 
13932580301SAttilio Rao static int
p4tcc_probe(device_t dev)14032580301SAttilio Rao p4tcc_probe(device_t dev)
14132580301SAttilio Rao {
142a8de37b0SEitan Adler 
143a8de37b0SEitan Adler 	if (resource_disabled("p4tcc", 0))
144a8de37b0SEitan Adler 		return (ENXIO);
145a8de37b0SEitan Adler 
14632580301SAttilio Rao 	device_set_desc(dev, "CPU Frequency Thermal Control");
14732580301SAttilio Rao 	return (0);
14832580301SAttilio Rao }
14932580301SAttilio Rao 
15032580301SAttilio Rao static int
p4tcc_attach(device_t dev)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
p4tcc_detach(device_t dev)21646b29ff9SHiren Panchasara p4tcc_detach(device_t dev)
21746b29ff9SHiren Panchasara {
21846b29ff9SHiren Panchasara 	struct cf_setting set;
21946b29ff9SHiren Panchasara 	int error;
22046b29ff9SHiren Panchasara 
22146b29ff9SHiren Panchasara 	error = cpufreq_unregister(dev);
22246b29ff9SHiren Panchasara 	if (error)
22346b29ff9SHiren Panchasara 		return (error);
22446b29ff9SHiren Panchasara 
22546b29ff9SHiren Panchasara 	/*
22646b29ff9SHiren Panchasara 	 * Before we finish detach, switch to Automatic mode.
22746b29ff9SHiren Panchasara 	 */
22846b29ff9SHiren Panchasara 	set.freq = 10000;
22946b29ff9SHiren Panchasara 	p4tcc_set(dev, &set);
23046b29ff9SHiren Panchasara 	return(0);
23146b29ff9SHiren Panchasara }
23246b29ff9SHiren Panchasara 
23346b29ff9SHiren Panchasara static int
p4tcc_settings(device_t dev,struct cf_setting * sets,int * count)23432580301SAttilio Rao p4tcc_settings(device_t dev, struct cf_setting *sets, int *count)
23532580301SAttilio Rao {
23632580301SAttilio Rao 	struct p4tcc_softc *sc;
23732580301SAttilio Rao 	int i, val;
23832580301SAttilio Rao 
23932580301SAttilio Rao 	sc = device_get_softc(dev);
24032580301SAttilio Rao 	if (sets == NULL || count == NULL)
24132580301SAttilio Rao 		return (EINVAL);
24232580301SAttilio Rao 	if (*count < sc->set_count)
24332580301SAttilio Rao 		return (E2BIG);
24432580301SAttilio Rao 
24532580301SAttilio Rao 	/* Return a list of valid settings for this driver. */
24632580301SAttilio Rao 	memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * sc->set_count);
24732580301SAttilio Rao 	val = TCC_NUM_SETTINGS;
24832580301SAttilio Rao 	for (i = 0; i < sc->set_count; i++, val--) {
24932580301SAttilio Rao 		sets[i].freq = TCC_SPEED_PERCENT(val);
25032580301SAttilio Rao 		sets[i].dev = dev;
25132580301SAttilio Rao 	}
25232580301SAttilio Rao 	*count = sc->set_count;
25332580301SAttilio Rao 
25432580301SAttilio Rao 	return (0);
25532580301SAttilio Rao }
25632580301SAttilio Rao 
25732580301SAttilio Rao static int
p4tcc_set(device_t dev,const struct cf_setting * set)25832580301SAttilio Rao p4tcc_set(device_t dev, const struct cf_setting *set)
25932580301SAttilio Rao {
26032580301SAttilio Rao 	struct p4tcc_softc *sc;
26132580301SAttilio Rao 	uint64_t mask, msr;
26232580301SAttilio Rao 	int val;
26332580301SAttilio Rao 
26432580301SAttilio Rao 	if (set == NULL)
26532580301SAttilio Rao 		return (EINVAL);
26632580301SAttilio Rao 	sc = device_get_softc(dev);
26732580301SAttilio Rao 
26832580301SAttilio Rao 	/*
26932580301SAttilio Rao 	 * Validate requested state converts to a setting that is an integer
27032580301SAttilio Rao 	 * from [sc->lowest_val .. TCC_NUM_SETTINGS].
27132580301SAttilio Rao 	 */
27232580301SAttilio Rao 	val = set->freq * TCC_NUM_SETTINGS / 10000;
27332580301SAttilio Rao 	if (val * 10000 != set->freq * TCC_NUM_SETTINGS ||
27432580301SAttilio Rao 	    val < sc->lowest_val || val > TCC_NUM_SETTINGS)
27532580301SAttilio Rao 		return (EINVAL);
27632580301SAttilio Rao 
27732580301SAttilio Rao 	/*
27832580301SAttilio Rao 	 * Read the current register and mask off the old setting and
27932580301SAttilio Rao 	 * On-Demand bit.  If the new val is < 100%, set it and the On-Demand
28032580301SAttilio Rao 	 * bit, otherwise just return to Automatic mode.
28132580301SAttilio Rao 	 */
28232580301SAttilio Rao 	msr = rdmsr(MSR_THERM_CONTROL);
28332580301SAttilio Rao 	mask = (TCC_NUM_SETTINGS - 1) << TCC_REG_OFFSET;
28432580301SAttilio Rao 	msr &= ~(mask | TCC_ENABLE_ONDEMAND);
28532580301SAttilio Rao 	if (val < TCC_NUM_SETTINGS)
28632580301SAttilio Rao 		msr |= (val << TCC_REG_OFFSET) | TCC_ENABLE_ONDEMAND;
28732580301SAttilio Rao 	wrmsr(MSR_THERM_CONTROL, msr);
28832580301SAttilio Rao 
28932580301SAttilio Rao 	/*
29032580301SAttilio Rao 	 * Record whether we're now in Automatic or On-Demand mode.  We have
29132580301SAttilio Rao 	 * to cache this since there is no reliable way to check if TCC is in
29232580301SAttilio Rao 	 * Automatic mode (i.e., at 100% or possibly 50%).  Reading bit 4 of
29332580301SAttilio Rao 	 * the ACPI Thermal Monitor Control Register produces 0 no matter
29432580301SAttilio Rao 	 * what the current mode.
29532580301SAttilio Rao 	 */
29632580301SAttilio Rao 	if (msr & TCC_ENABLE_ONDEMAND)
29732580301SAttilio Rao 		sc->auto_mode = FALSE;
2982f42a9bfSAlexander Kabaev 	else
2992f42a9bfSAlexander Kabaev 		sc->auto_mode = TRUE;
30032580301SAttilio Rao 
30132580301SAttilio Rao 	return (0);
30232580301SAttilio Rao }
30332580301SAttilio Rao 
30432580301SAttilio Rao static int
p4tcc_get(device_t dev,struct cf_setting * set)30532580301SAttilio Rao p4tcc_get(device_t dev, struct cf_setting *set)
30632580301SAttilio Rao {
30732580301SAttilio Rao 	struct p4tcc_softc *sc;
30832580301SAttilio Rao 	uint64_t msr;
30932580301SAttilio Rao 	int val;
31032580301SAttilio Rao 
31132580301SAttilio Rao 	if (set == NULL)
31232580301SAttilio Rao 		return (EINVAL);
31332580301SAttilio Rao 	sc = device_get_softc(dev);
31432580301SAttilio Rao 
31532580301SAttilio Rao 	/*
31632580301SAttilio Rao 	 * Read the current register and extract the current setting.  If
31732580301SAttilio Rao 	 * in automatic mode, assume we're at TCC_NUM_SETTINGS (100%).
31832580301SAttilio Rao 	 *
31932580301SAttilio Rao 	 * XXX This is not completely reliable since at high temperatures
32032580301SAttilio Rao 	 * the CPU may be automatically throttling to 50% but it's the best
32132580301SAttilio Rao 	 * we can do.
32232580301SAttilio Rao 	 */
32332580301SAttilio Rao 	if (!sc->auto_mode) {
32432580301SAttilio Rao 		msr = rdmsr(MSR_THERM_CONTROL);
32532580301SAttilio Rao 		val = (msr >> TCC_REG_OFFSET) & (TCC_NUM_SETTINGS - 1);
32632580301SAttilio Rao 	} else
32732580301SAttilio Rao 		val = TCC_NUM_SETTINGS;
32832580301SAttilio Rao 
32932580301SAttilio Rao 	memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
33032580301SAttilio Rao 	set->freq = TCC_SPEED_PERCENT(val);
33132580301SAttilio Rao 	set->dev = dev;
33232580301SAttilio Rao 
33332580301SAttilio Rao 	return (0);
33432580301SAttilio Rao }
33532580301SAttilio Rao 
33632580301SAttilio Rao static int
p4tcc_type(device_t dev,int * type)33732580301SAttilio Rao p4tcc_type(device_t dev, int *type)
33832580301SAttilio Rao {
33932580301SAttilio Rao 
34032580301SAttilio Rao 	if (type == NULL)
34132580301SAttilio Rao 		return (EINVAL);
34232580301SAttilio Rao 
34332580301SAttilio Rao 	*type = CPUFREQ_TYPE_RELATIVE;
34432580301SAttilio Rao 	return (0);
34532580301SAttilio Rao }
346