#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: amdtemp.c,v 1.23 2018/12/30 15:43:43 is Exp $ ");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <machine/specialreg.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/sysmon/sysmonvar.h>
#define THERMTRIP_STAT_R 0xe4
#define NORTHBRIDGE_CAP_R 0xe8
#define CPUID_FAMILY_MODEL_R 0xfc
#define K8_THERM_SENSE_SEL (1 << 6)
#define K8_THERM_SENSE_CORE_SEL (1 << 2)
#define K8_T_SEL_C0(v) (v |= K8_THERM_SENSE_CORE_SEL)
#define K8_T_SEL_C1(v) (v &= ~(K8_THERM_SENSE_CORE_SEL))
#define K8_T_SEL_S0(v) (v &= ~(K8_THERM_SENSE_SEL))
#define K8_T_SEL_S1(v) (v |= K8_THERM_SENSE_SEL)
#define F10_TEMPERATURE_CTL_R 0xa4
#define F10_TEMP_CURTMP __BITS(31,21)
#define K8_SOCKET_F 1
#define K8_SOCKET_AM2 2
#define K8_SOCKET_S1 3
static const struct {
const char rev[5];
const struct {
const pcireg_t cpuid;
const uint8_t socket;
} cpu[5];
} amdtemp_core[] = {
{ "BH-F", { { 0x00040FB0, K8_SOCKET_AM2 },
{ 0x00040F80, K8_SOCKET_S1 },
{ 0, 0 }, { 0, 0 }, { 0, 0 } } },
{ "DH-F", { { 0x00040FF0, K8_SOCKET_AM2 },
{ 0x00040FC0, K8_SOCKET_S1 },
{ 0x00050FF0, K8_SOCKET_AM2 },
{ 0, 0 }, { 0, 0 } } },
{ "JH-F", { { 0x00040F10, K8_SOCKET_F },
{ 0x00040F30, K8_SOCKET_AM2 },
{ 0x000C0F10, K8_SOCKET_F },
{ 0, 0 }, { 0, 0 } } },
{ "BH-G", { { 0x00060FB0, K8_SOCKET_AM2 },
{ 0x00060F80, K8_SOCKET_S1 },
{ 0, 0 }, { 0, 0 }, { 0, 0 } } },
{ "DH-G", { { 0x00060FF0, K8_SOCKET_AM2 },
{ 0x00060FC0, K8_SOCKET_S1 },
{ 0x00070FF0, K8_SOCKET_AM2 },
{ 0x00070FC0, K8_SOCKET_S1 },
{ 0, 0 } } }
};
struct amdtemp_softc {
pci_chipset_tag_t sc_pc;
pcitag_t sc_pcitag;
struct sysmon_envsys *sc_sme;
envsys_data_t *sc_sensor;
size_t sc_sensor_len;
char sc_rev;
int8_t sc_numsensors;
uint32_t sc_family;
int32_t sc_adjustment;
};
static int amdtemp_match(device_t, cfdata_t, void *);
static void amdtemp_attach(device_t, device_t, void *);
static int amdtemp_detach(device_t, int);
static void amdtemp_k8_init(struct amdtemp_softc *, pcireg_t);
static void amdtemp_k8_setup_sensors(struct amdtemp_softc *, int);
static void amdtemp_k8_refresh(struct sysmon_envsys *, envsys_data_t *);
static void amdtemp_family10_init(struct amdtemp_softc *);
static void amdtemp_family10_setup_sensors(struct amdtemp_softc *, int);
static void amdtemp_family10_refresh(struct sysmon_envsys *, envsys_data_t *);
CFATTACH_DECL_NEW(amdtemp, sizeof(struct amdtemp_softc),
amdtemp_match, amdtemp_attach, amdtemp_detach, NULL);
static int
amdtemp_match(device_t parent, cfdata_t match, void *aux)
{
struct pci_attach_args *pa = aux;
pcireg_t cpu_signature;
uint32_t family;
KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);
cpu_signature = pci_conf_read(pa->pa_pc,
pa->pa_tag, CPUID_FAMILY_MODEL_R);
if (cpu_signature == 0x0)
return 0;
family = CPUID_TO_FAMILY(cpu_signature);
if (family == 0x10) {
if (CPUID_TO_BASEMODEL(cpu_signature) < 4)
return 0;
if (CPUID_TO_BASEMODEL(cpu_signature) == 4 &&
CPUID_TO_STEPPING(cpu_signature) < 2)
return 0;
}
if (family > 0x16)
return 0;
return 1;
}
static void
amdtemp_attach(device_t parent, device_t self, void *aux)
{
struct amdtemp_softc *sc = device_private(self);
struct pci_attach_args *pa = aux;
pcireg_t cpu_signature;
int error;
uint8_t i;
aprint_naive("\n");
aprint_normal(": AMD CPU Temperature Sensors");
cpu_signature = pci_conf_read(pa->pa_pc,
pa->pa_tag, CPUID_FAMILY_MODEL_R);
KASSERT(cpu_signature != 0x0);
sc->sc_family = CPUID_TO_FAMILY(cpu_signature);
KASSERT(sc->sc_family >= 0xf);
sc->sc_sme = NULL;
sc->sc_sensor = NULL;
sc->sc_pc = pa->pa_pc;
sc->sc_pcitag = pa->pa_tag;
sc->sc_adjustment = 0;
switch (sc->sc_family) {
case 0xf:
amdtemp_k8_init(sc, cpu_signature);
break;
case 0x10:
case 0x11:
case 0x12:
case 0x14:
case 0x15:
case 0x16:
amdtemp_family10_init(sc);
break;
default:
aprint_normal(", family 0x%x not supported\n",
sc->sc_family);
return;
}
aprint_normal("\n");
if (sc->sc_adjustment != 0)
aprint_debug_dev(self, "Workaround enabled\n");
sc->sc_sme = sysmon_envsys_create();
sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);
switch (sc->sc_family) {
case 0xf:
amdtemp_k8_setup_sensors(sc, device_unit(self));
break;
case 0x10:
case 0x11:
case 0x12:
case 0x14:
case 0x15:
case 0x16:
amdtemp_family10_setup_sensors(sc, device_unit(self));
break;
}
for (i = 0; i < sc->sc_numsensors; i++) {
if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
goto bad;
}
sc->sc_sme->sme_name = device_xname(self);
sc->sc_sme->sme_cookie = sc;
switch (sc->sc_family) {
case 0xf:
sc->sc_sme->sme_refresh = amdtemp_k8_refresh;
break;
case 0x10:
case 0x11:
case 0x12:
case 0x14:
case 0x15:
case 0x16:
sc->sc_sme->sme_refresh = amdtemp_family10_refresh;
break;
}
error = sysmon_envsys_register(sc->sc_sme);
if (error) {
aprint_error_dev(self, "unable to register with sysmon "
"(error=%d)\n", error);
goto bad;
}
(void)pmf_device_register(self, NULL, NULL);
return;
bad:
if (sc->sc_sme != NULL) {
sysmon_envsys_destroy(sc->sc_sme);
sc->sc_sme = NULL;
}
if (sc->sc_sensor != NULL) {
kmem_free(sc->sc_sensor, sc->sc_sensor_len);
sc->sc_sensor = NULL;
}
}
static int
amdtemp_detach(device_t self, int flags)
{
struct amdtemp_softc *sc = device_private(self);
pmf_device_deregister(self);
if (sc->sc_sme != NULL)
sysmon_envsys_unregister(sc->sc_sme);
if (sc->sc_sensor != NULL)
kmem_free(sc->sc_sensor, sc->sc_sensor_len);
return 0;
}
static void
amdtemp_k8_init(struct amdtemp_softc *sc, pcireg_t cpu_signature)
{
pcireg_t data;
uint32_t cmpcap;
uint8_t i, j;
aprint_normal(" (K8");
for (i = 0; i < __arraycount(amdtemp_core) && sc->sc_rev == '\0'; i++) {
for (j = 0; amdtemp_core[i].cpu[j].cpuid != 0; j++) {
if ((cpu_signature & ~0xf)
!= amdtemp_core[i].cpu[j].cpuid)
continue;
sc->sc_rev = amdtemp_core[i].rev[3];
aprint_normal(": core rev %.4s%.1x",
amdtemp_core[i].rev,
CPUID_TO_STEPPING(cpu_signature));
switch (amdtemp_core[i].cpu[j].socket) {
case K8_SOCKET_AM2:
if (sc->sc_rev == 'G')
sc->sc_adjustment = 21000000;
aprint_normal(", socket AM2");
break;
case K8_SOCKET_S1:
aprint_normal(", socket S1");
break;
case K8_SOCKET_F:
aprint_normal(", socket F");
break;
}
}
}
if (sc->sc_rev == '\0') {
sc->sc_rev = 'G';
aprint_normal(": cpuid 0x%x", cpu_signature);
}
aprint_normal(")");
data = pci_conf_read(sc->sc_pc, sc->sc_pcitag, NORTHBRIDGE_CAP_R);
cmpcap = (data >> 12) & 0x3;
sc->sc_numsensors = cmpcap ? 4 : 2;
}
static void
amdtemp_k8_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
{
uint8_t i;
dv_unit *= (sc->sc_numsensors / 2);
for (i = 0; i < sc->sc_numsensors; i++) {
sc->sc_sensor[i].units = ENVSYS_STEMP;
sc->sc_sensor[i].state = ENVSYS_SVALID;
sc->sc_sensor[i].flags = ENVSYS_FHAS_ENTROPY;
snprintf(sc->sc_sensor[i].desc, sizeof(sc->sc_sensor[i].desc),
"CPU%u Sensor%u", dv_unit + (i / 2), i % 2);
}
}
static void
amdtemp_k8_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
struct amdtemp_softc *sc = sme->sme_cookie;
pcireg_t status, match, tmp;
uint32_t value;
status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
switch (edata->sensor) {
case 0:
K8_T_SEL_C0(status);
K8_T_SEL_S0(status);
break;
case 1:
K8_T_SEL_C0(status);
K8_T_SEL_S1(status);
break;
case 2:
K8_T_SEL_C1(status);
K8_T_SEL_S0(status);
break;
case 3:
K8_T_SEL_C1(status);
K8_T_SEL_S1(status);
break;
}
match = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
pci_conf_write(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R, status);
status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
tmp = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
value = 0x3ff & (status >> 14);
if (sc->sc_rev != 'G')
value &= ~0x3;
edata->state = ENVSYS_SINVALID;
if ((tmp == match) && ((value & ~0x3) != 0)) {
edata->state = ENVSYS_SVALID;
edata->value_cur = (value * 250000 - 49000000) + 273150000 +
sc->sc_adjustment;
}
}
static void
amdtemp_family10_init(struct amdtemp_softc *sc)
{
aprint_normal(" (Family%02xh)", sc->sc_family);
sc->sc_numsensors = 1;
}
static void
amdtemp_family10_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
{
KASSERT(sc->sc_numsensors == 1);
sc->sc_sensor[0].units = ENVSYS_STEMP;
sc->sc_sensor[0].state = ENVSYS_SVALID;
sc->sc_sensor[0].flags = ENVSYS_FHAS_ENTROPY;
snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
"cpu%u temperature", dv_unit);
}
static void
amdtemp_family10_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
struct amdtemp_softc *sc = sme->sme_cookie;
pcireg_t status;
uint32_t value;
status = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
F10_TEMPERATURE_CTL_R);
value = __SHIFTOUT(status, F10_TEMP_CURTMP);
edata->value_cur = (value * 125000) + 273150000;
edata->state = ENVSYS_SVALID;
}
MODULE(MODULE_CLASS_DRIVER, amdtemp, "sysmon_envsys");
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
amdtemp_modcmd(modcmd_t cmd, void *aux)
{
int error = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = config_init_component(cfdriver_ioconf_amdtemp,
cfattach_ioconf_amdtemp, cfdata_ioconf_amdtemp);
#endif
return error;
case MODULE_CMD_FINI:
#ifdef _MODULE
error = config_fini_component(cfdriver_ioconf_amdtemp,
cfattach_ioconf_amdtemp, cfdata_ioconf_amdtemp);
#endif
return error;
default:
return ENOTTY;
}
}