2005-01-05 19:10:48 +00:00
|
|
|
/*-
|
2005-02-23 16:42:56 +00:00
|
|
|
* Copyright (c) 2005 Nate Lawson
|
2004-01-18 21:06:56 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2005-02-23 16:42:56 +00:00
|
|
|
|
2004-01-18 21:06:56 +00:00
|
|
|
/*
|
2005-02-23 16:42:56 +00:00
|
|
|
* Throttle clock frequency by using the thermal control circuit. This
|
|
|
|
* operates independently of SpeedStep and ACPI throttling and is supported
|
|
|
|
* on Pentium 4 and later models (feature TM).
|
2004-01-18 21:06:56 +00:00
|
|
|
*
|
2005-02-23 16:42:56 +00:00
|
|
|
* Reference: Intel Developer's manual v.3 #245472-012
|
2004-01-18 21:06:56 +00:00
|
|
|
*
|
2005-02-23 16:42:56 +00:00
|
|
|
* The original version of this driver was written by Ted Unangst for
|
|
|
|
* OpenBSD and imported by Maxim Sobolev. It was rewritten by Nate Lawson
|
|
|
|
* for use with the cpufreq framework.
|
2004-01-18 21:06:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2005-02-23 16:42:56 +00:00
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/cpu.h>
|
2004-01-18 21:06:56 +00:00
|
|
|
#include <sys/kernel.h>
|
2005-02-23 16:42:56 +00:00
|
|
|
#include <sys/module.h>
|
2004-01-18 21:06:56 +00:00
|
|
|
|
|
|
|
#include <machine/md_var.h>
|
|
|
|
#include <machine/specialreg.h>
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
#include "cpufreq_if.h"
|
|
|
|
|
2009-06-05 18:44:36 +00:00
|
|
|
#include <contrib/dev/acpica/include/acpi.h>
|
|
|
|
|
2005-04-10 19:16:27 +00:00
|
|
|
#include <dev/acpica/acpivar.h>
|
|
|
|
#include "acpi_if.h"
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
struct p4tcc_softc {
|
|
|
|
device_t dev;
|
|
|
|
int set_count;
|
|
|
|
int lowest_val;
|
|
|
|
int auto_mode;
|
2004-01-18 21:06:56 +00:00
|
|
|
};
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
#define TCC_NUM_SETTINGS 8
|
|
|
|
|
|
|
|
#define TCC_ENABLE_ONDEMAND (1<<4)
|
|
|
|
#define TCC_REG_OFFSET 1
|
|
|
|
#define TCC_SPEED_PERCENT(x) ((10000 * (x)) / TCC_NUM_SETTINGS)
|
|
|
|
|
2005-04-10 19:16:27 +00:00
|
|
|
static int p4tcc_features(driver_t *driver, u_int *features);
|
2005-02-23 16:42:56 +00:00
|
|
|
static void p4tcc_identify(driver_t *driver, device_t parent);
|
|
|
|
static int p4tcc_probe(device_t dev);
|
|
|
|
static int p4tcc_attach(device_t dev);
|
2013-05-10 22:43:27 +00:00
|
|
|
static int p4tcc_detach(device_t dev);
|
2005-02-23 16:42:56 +00:00
|
|
|
static int p4tcc_settings(device_t dev, struct cf_setting *sets,
|
|
|
|
int *count);
|
|
|
|
static int p4tcc_set(device_t dev, const struct cf_setting *set);
|
|
|
|
static int p4tcc_get(device_t dev, struct cf_setting *set);
|
|
|
|
static int p4tcc_type(device_t dev, int *type);
|
|
|
|
|
|
|
|
static device_method_t p4tcc_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_identify, p4tcc_identify),
|
|
|
|
DEVMETHOD(device_probe, p4tcc_probe),
|
|
|
|
DEVMETHOD(device_attach, p4tcc_attach),
|
2013-05-10 22:43:27 +00:00
|
|
|
DEVMETHOD(device_detach, p4tcc_detach),
|
2005-02-23 16:42:56 +00:00
|
|
|
|
|
|
|
/* cpufreq interface */
|
|
|
|
DEVMETHOD(cpufreq_drv_set, p4tcc_set),
|
|
|
|
DEVMETHOD(cpufreq_drv_get, p4tcc_get),
|
|
|
|
DEVMETHOD(cpufreq_drv_type, p4tcc_type),
|
|
|
|
DEVMETHOD(cpufreq_drv_settings, p4tcc_settings),
|
2005-04-10 19:16:27 +00:00
|
|
|
|
|
|
|
/* ACPI interface */
|
|
|
|
DEVMETHOD(acpi_get_features, p4tcc_features),
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
static driver_t p4tcc_driver = {
|
|
|
|
"p4tcc",
|
|
|
|
p4tcc_methods,
|
|
|
|
sizeof(struct p4tcc_softc),
|
|
|
|
};
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
static devclass_t p4tcc_devclass;
|
|
|
|
DRIVER_MODULE(p4tcc, cpu, p4tcc_driver, p4tcc_devclass, 0, 0);
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-04-10 19:16:27 +00:00
|
|
|
static int
|
|
|
|
p4tcc_features(driver_t *driver, u_int *features)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Notify the ACPI CPU that we support direct access to MSRs */
|
|
|
|
*features = ACPI_CAP_THR_MSRS;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-01-18 21:06:56 +00:00
|
|
|
static void
|
2005-02-23 16:42:56 +00:00
|
|
|
p4tcc_identify(driver_t *driver, device_t parent)
|
2004-01-18 21:06:56 +00:00
|
|
|
{
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
if ((cpu_feature & (CPUID_ACPI | CPUID_TM)) != (CPUID_ACPI | CPUID_TM))
|
|
|
|
return;
|
2005-02-27 02:43:02 +00:00
|
|
|
|
|
|
|
/* Make sure we're not being doubly invoked. */
|
|
|
|
if (device_find_child(parent, "p4tcc", -1) != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We attach a p4tcc child for every CPU since settings need to
|
|
|
|
* be performed on every CPU in the SMP case. See section 13.15.3
|
|
|
|
* of the IA32 Intel Architecture Software Developer's Manual,
|
|
|
|
* Volume 3, for more info.
|
|
|
|
*/
|
Attach the cpufreq child devices with specific orders to enforce relative
priority of some of the drivers that manage the same state (e.g. ichss0
vs est0). Specifically, powernow, est, and p4tcc are added at order 10,
ichss at order 20, and smist at order 30. Previously, some laptops were
seeing both ichss0 and est0 attaching and stomping on each other.
XXX: This isn't quite ideal, but works with the existing hacks, I think
what we really want instead is a single "speedstep0" device for CPUs
that the ichss, est, and smist drivers probe (but with differing
priorities).
MFC after: 1 week
2008-08-13 16:09:40 +00:00
|
|
|
if (BUS_ADD_CHILD(parent, 10, "p4tcc", -1) == NULL)
|
2005-02-23 16:42:56 +00:00
|
|
|
device_printf(parent, "add p4tcc child failed\n");
|
2004-01-18 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-02-23 16:42:56 +00:00
|
|
|
p4tcc_probe(device_t dev)
|
2004-01-18 21:06:56 +00:00
|
|
|
{
|
2012-10-22 13:06:09 +00:00
|
|
|
|
|
|
|
if (resource_disabled("p4tcc", 0))
|
|
|
|
return (ENXIO);
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
device_set_desc(dev, "CPU Frequency Thermal Control");
|
|
|
|
return (0);
|
2004-01-18 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
static int
|
|
|
|
p4tcc_attach(device_t dev)
|
2004-01-18 21:06:56 +00:00
|
|
|
{
|
2005-02-23 16:42:56 +00:00
|
|
|
struct p4tcc_softc *sc;
|
2005-10-23 19:38:06 +00:00
|
|
|
struct cf_setting set;
|
2005-02-23 16:42:56 +00:00
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
sc->dev = dev;
|
|
|
|
sc->set_count = TCC_NUM_SETTINGS;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* On boot, the TCC is usually in Automatic mode where reading the
|
|
|
|
* current performance level is likely to produce bogus results.
|
|
|
|
* We record that state here and don't trust the contents of the
|
|
|
|
* status MSR until we've set it ourselves.
|
|
|
|
*/
|
|
|
|
sc->auto_mode = TRUE;
|
|
|
|
|
2008-03-17 09:05:15 +00:00
|
|
|
/*
|
|
|
|
* XXX: After a cursory glance at various Intel specification
|
|
|
|
* XXX: updates it seems like these tests for errata is bogus.
|
|
|
|
* XXX: As far as I can tell, the failure mode is benign, in
|
|
|
|
* XXX: that cpus with no errata will have their bottom two
|
|
|
|
* XXX: STPCLK# rates disabled, so rather than waste more time
|
|
|
|
* XXX: hunting down intel docs, just document it and punt. /phk
|
|
|
|
*/
|
2008-03-17 09:00:59 +00:00
|
|
|
switch (cpu_id & 0xff) {
|
2005-02-23 16:42:56 +00:00
|
|
|
case 0x22:
|
2008-03-17 09:00:59 +00:00
|
|
|
case 0x24:
|
|
|
|
case 0x25:
|
|
|
|
case 0x27:
|
|
|
|
case 0x29:
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* These CPU models hang when set to 12.5%.
|
|
|
|
* See Errata O50, P44, and Z21.
|
|
|
|
*/
|
|
|
|
sc->set_count -= 1;
|
2004-01-18 21:06:56 +00:00
|
|
|
break;
|
2005-02-23 16:42:56 +00:00
|
|
|
case 0x07: /* errata N44 and P18 */
|
|
|
|
case 0x0a:
|
|
|
|
case 0x12:
|
|
|
|
case 0x13:
|
2008-11-26 09:46:35 +00:00
|
|
|
case 0x62: /* Pentium D B1: errata AA21 */
|
|
|
|
case 0x64: /* Pentium D C1: errata AA21 */
|
|
|
|
case 0x65: /* Pentium D D0: errata AA21 */
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* These CPU models hang when set to 12.5% or 25%.
|
2008-11-26 09:46:35 +00:00
|
|
|
* See Errata N44, P18l and AA21.
|
2005-02-23 16:42:56 +00:00
|
|
|
*/
|
|
|
|
sc->set_count -= 2;
|
2004-01-18 21:06:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-02-23 16:42:56 +00:00
|
|
|
sc->lowest_val = TCC_NUM_SETTINGS - sc->set_count + 1;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-10-23 19:38:06 +00:00
|
|
|
/*
|
|
|
|
* Before we finish attach, switch to 100%. It's possible the BIOS
|
|
|
|
* set us to a lower rate. The user can override this after boot.
|
|
|
|
*/
|
|
|
|
set.freq = 10000;
|
|
|
|
p4tcc_set(dev, &set);
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
cpufreq_register(dev);
|
|
|
|
return (0);
|
2004-01-18 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-10 22:43:27 +00:00
|
|
|
static int
|
|
|
|
p4tcc_detach(device_t dev)
|
|
|
|
{
|
|
|
|
struct cf_setting set;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = cpufreq_unregister(dev);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before we finish detach, switch to Automatic mode.
|
|
|
|
*/
|
|
|
|
set.freq = 10000;
|
|
|
|
p4tcc_set(dev, &set);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2004-01-18 21:06:56 +00:00
|
|
|
static int
|
2005-02-23 16:42:56 +00:00
|
|
|
p4tcc_settings(device_t dev, struct cf_setting *sets, int *count)
|
2004-01-18 21:06:56 +00:00
|
|
|
{
|
2005-02-23 16:42:56 +00:00
|
|
|
struct p4tcc_softc *sc;
|
|
|
|
int i, val;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
if (sets == NULL || count == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
if (*count < sc->set_count)
|
|
|
|
return (E2BIG);
|
|
|
|
|
|
|
|
/* Return a list of valid settings for this driver. */
|
|
|
|
memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * sc->set_count);
|
|
|
|
val = TCC_NUM_SETTINGS;
|
|
|
|
for (i = 0; i < sc->set_count; i++, val--) {
|
|
|
|
sets[i].freq = TCC_SPEED_PERCENT(val);
|
|
|
|
sets[i].dev = dev;
|
|
|
|
}
|
|
|
|
*count = sc->set_count;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
static int
|
|
|
|
p4tcc_set(device_t dev, const struct cf_setting *set)
|
|
|
|
{
|
|
|
|
struct p4tcc_softc *sc;
|
|
|
|
uint64_t mask, msr;
|
|
|
|
int val;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
if (set == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
sc = device_get_softc(dev);
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* Validate requested state converts to a setting that is an integer
|
|
|
|
* from [sc->lowest_val .. TCC_NUM_SETTINGS].
|
|
|
|
*/
|
|
|
|
val = set->freq * TCC_NUM_SETTINGS / 10000;
|
|
|
|
if (val * 10000 != set->freq * TCC_NUM_SETTINGS ||
|
|
|
|
val < sc->lowest_val || val > TCC_NUM_SETTINGS)
|
|
|
|
return (EINVAL);
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* Read the current register and mask off the old setting and
|
|
|
|
* On-Demand bit. If the new val is < 100%, set it and the On-Demand
|
|
|
|
* bit, otherwise just return to Automatic mode.
|
|
|
|
*/
|
|
|
|
msr = rdmsr(MSR_THERM_CONTROL);
|
|
|
|
mask = (TCC_NUM_SETTINGS - 1) << TCC_REG_OFFSET;
|
|
|
|
msr &= ~(mask | TCC_ENABLE_ONDEMAND);
|
|
|
|
if (val < TCC_NUM_SETTINGS)
|
|
|
|
msr |= (val << TCC_REG_OFFSET) | TCC_ENABLE_ONDEMAND;
|
|
|
|
wrmsr(MSR_THERM_CONTROL, msr);
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* Record whether we're now in Automatic or On-Demand mode. We have
|
|
|
|
* to cache this since there is no reliable way to check if TCC is in
|
|
|
|
* Automatic mode (i.e., at 100% or possibly 50%). Reading bit 4 of
|
|
|
|
* the ACPI Thermal Monitor Control Register produces 0 no matter
|
|
|
|
* what the current mode.
|
|
|
|
*/
|
|
|
|
if (msr & TCC_ENABLE_ONDEMAND)
|
|
|
|
sc->auto_mode = FALSE;
|
2012-02-26 21:24:27 +00:00
|
|
|
else
|
|
|
|
sc->auto_mode = TRUE;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
static int
|
|
|
|
p4tcc_get(device_t dev, struct cf_setting *set)
|
2004-01-18 21:06:56 +00:00
|
|
|
{
|
2005-02-23 16:42:56 +00:00
|
|
|
struct p4tcc_softc *sc;
|
|
|
|
uint64_t msr;
|
|
|
|
int val;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
if (set == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
sc = device_get_softc(dev);
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
/*
|
|
|
|
* Read the current register and extract the current setting. If
|
|
|
|
* in automatic mode, assume we're at TCC_NUM_SETTINGS (100%).
|
|
|
|
*
|
|
|
|
* XXX This is not completely reliable since at high temperatures
|
|
|
|
* the CPU may be automatically throttling to 50% but it's the best
|
|
|
|
* we can do.
|
|
|
|
*/
|
|
|
|
if (!sc->auto_mode) {
|
|
|
|
msr = rdmsr(MSR_THERM_CONTROL);
|
|
|
|
val = (msr >> TCC_REG_OFFSET) & (TCC_NUM_SETTINGS - 1);
|
|
|
|
} else
|
|
|
|
val = TCC_NUM_SETTINGS;
|
2004-01-18 21:06:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
|
|
|
|
set->freq = TCC_SPEED_PERCENT(val);
|
|
|
|
set->dev = dev;
|
2004-08-23 09:47:56 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2004-01-24 21:13:13 +00:00
|
|
|
|
2005-02-23 16:42:56 +00:00
|
|
|
static int
|
|
|
|
p4tcc_type(device_t dev, int *type)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (type == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
*type = CPUFREQ_TYPE_RELATIVE;
|
|
|
|
return (0);
|
2004-01-18 21:06:56 +00:00
|
|
|
}
|