Home | History | Annotate | Line # | Download | only in pci
amdzentemp.c revision 1.7.6.3
      1  1.7.6.2    martin /*      $NetBSD: amdzentemp.c,v 1.7.6.3 2020/04/21 18:42:12 martin Exp $ */
      2      1.1  christos /*      $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */
      3      1.1  christos 
      4      1.1  christos /*
      5  1.7.6.3    martin  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
      6  1.7.6.3    martin  * All rights reserved.
      7  1.7.6.3    martin  *
      8  1.7.6.3    martin  * Copyright (c) 2019 Conrad Meyer <cem (at) FreeBSD.org>
      9      1.1  christos  * All rights reserved.
     10      1.1  christos  *
     11      1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
     12      1.1  christos  * by Christoph Egger.
     13      1.1  christos  *
     14      1.1  christos  * NetBSD port by Ian Clark <mrrooster (at) gmail.com>
     15      1.1  christos  *
     16      1.1  christos  * Redistribution and use in source and binary forms, with or without
     17      1.1  christos  * modification, are permitted provided that the following conditions
     18      1.1  christos  * are met:
     19      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     20      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     21      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     22      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     23      1.1  christos  *    documentation and/or other materials provided with the distribution.
     24      1.1  christos  *
     25      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     36      1.1  christos  */
     37      1.1  christos 
     38      1.1  christos /*
     39      1.1  christos  * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd (at) bugmail.mojo.ru>
     40      1.1  christos  *
     41      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
     42      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     43      1.1  christos  * copyright notice and this permission notice appear in all copies.
     44      1.1  christos  *
     45      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     46      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     47      1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     48      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     49      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     50      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     51      1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     52      1.1  christos  */
     53      1.1  christos 
     54      1.1  christos 
     55      1.1  christos #include <sys/cdefs.h>
     56  1.7.6.2    martin __KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.7.6.3 2020/04/21 18:42:12 martin Exp $ ");
     57      1.1  christos 
     58      1.1  christos #include <sys/param.h>
     59      1.1  christos #include <sys/bus.h>
     60      1.1  christos #include <sys/cpu.h>
     61      1.1  christos #include <sys/systm.h>
     62      1.1  christos #include <sys/device.h>
     63      1.1  christos #include <sys/kmem.h>
     64      1.1  christos #include <sys/module.h>
     65      1.1  christos 
     66      1.1  christos #include <machine/specialreg.h>
     67      1.1  christos 
     68      1.1  christos #include <dev/pci/pcireg.h>
     69      1.1  christos #include <dev/pci/pcivar.h>
     70      1.1  christos #include <dev/pci/pcidevs.h>
     71      1.1  christos 
     72      1.1  christos #include <dev/sysmon/sysmonvar.h>
     73      1.1  christos 
     74      1.1  christos #include "amdsmn.h"
     75      1.1  christos 
     76  1.7.6.3    martin #define	AMD_CURTMP_RANGE_ADJUST	49000000	/* in microKelvins (ie, 49C) */
     77  1.7.6.3    martin #define	AMD_CURTMP_RANGE_CHECK	__BIT(19)
     78  1.7.6.3    martin #define	F10_TEMP_CURTMP		__BITS(31,21)	/* XXX same as amdtemp.c */
     79  1.7.6.3    martin #define	F15M60_CURTMP_TJSEL	__BITS(17,16)
     80  1.7.6.3    martin 
     81  1.7.6.3    martin /*
     82  1.7.6.3    martin  * Reported Temperature, Family 15h, M60+
     83  1.7.6.3    martin  *
     84  1.7.6.3    martin  * Same register bit definitions as other Family 15h CPUs, but access is
     85  1.7.6.3    martin  * indirect via SMN, like Family 17h.
     86  1.7.6.3    martin  */
     87  1.7.6.3    martin #define	AMD_15H_M60H_REPTMP_CTRL	0xd8200ca4
     88  1.7.6.3    martin 
     89  1.7.6.3    martin /*
     90  1.7.6.3    martin  * Reported Temperature, Family 17h
     91  1.7.6.3    martin  *
     92  1.7.6.3    martin  * According to AMD OSRR for 17H, section 4.2.1, bits 31-21 of this register
     93  1.7.6.3    martin  * provide the current temp.  bit 19, when clear, means the temp is reported in
     94  1.7.6.3    martin  * a range 0.."225C" (probable typo for 255C), and when set changes the range
     95  1.7.6.3    martin  * to -49..206C.
     96  1.7.6.3    martin  */
     97  1.7.6.3    martin #define	AMD_17H_CUR_TMP			0x59800
     98      1.1  christos 
     99      1.1  christos struct amdzentemp_softc {
    100      1.1  christos 	struct sysmon_envsys *sc_sme;
    101      1.1  christos 	device_t sc_smn;
    102      1.1  christos 	envsys_data_t *sc_sensor;
    103      1.1  christos 	size_t sc_sensor_len;
    104  1.7.6.3    martin 	size_t sc_numsensors;
    105  1.7.6.2    martin 	int32_t sc_offset;
    106      1.1  christos };
    107      1.1  christos 
    108      1.1  christos 
    109      1.1  christos static int  amdzentemp_match(device_t, cfdata_t, void *);
    110      1.1  christos static void amdzentemp_attach(device_t, device_t, void *);
    111      1.1  christos static int  amdzentemp_detach(device_t, int);
    112      1.1  christos 
    113  1.7.6.3    martin static void amdzentemp_init(struct amdzentemp_softc *);
    114  1.7.6.3    martin static void amdzentemp_setup_sensors(struct amdzentemp_softc *, int);
    115  1.7.6.3    martin static void amdzentemp_family15_refresh(struct sysmon_envsys *, envsys_data_t *);
    116      1.1  christos static void amdzentemp_family17_refresh(struct sysmon_envsys *, envsys_data_t *);
    117      1.1  christos 
    118      1.1  christos CFATTACH_DECL_NEW(amdzentemp, sizeof(struct amdzentemp_softc),
    119      1.1  christos     amdzentemp_match, amdzentemp_attach, amdzentemp_detach, NULL);
    120      1.1  christos 
    121      1.1  christos static int
    122      1.1  christos amdzentemp_match(device_t parent, cfdata_t match, void *aux)
    123      1.1  christos {
    124      1.5  pgoyette 	struct pci_attach_args *pa __diagused = aux;
    125      1.3  pgoyette 
    126      1.1  christos 	KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);
    127      1.1  christos 
    128      1.1  christos 	cfdata_t parent_cfdata = device_cfdata(parent);
    129      1.1  christos 
    130      1.1  christos 	/* Got AMD family 17h system management network */
    131      1.1  christos 	return parent_cfdata->cf_name &&
    132      1.1  christos 	    memcmp(parent_cfdata->cf_name, "amdsmn", 6) == 0;
    133      1.1  christos }
    134      1.1  christos 
    135      1.1  christos static void
    136      1.1  christos amdzentemp_attach(device_t parent, device_t self, void *aux)
    137      1.1  christos {
    138      1.1  christos 	struct amdzentemp_softc *sc = device_private(self);
    139  1.7.6.3    martin 	struct cpu_info *ci = curcpu();
    140  1.7.6.3    martin 	int family;
    141      1.1  christos 	int error;
    142      1.1  christos 	size_t i;
    143      1.1  christos 
    144  1.7.6.3    martin 	family = CPUID_TO_FAMILY(ci->ci_signature);
    145      1.1  christos 	aprint_naive("\n");
    146  1.7.6.3    martin 	aprint_normal(": AMD CPU Temperature Sensors (Family%xh)",
    147  1.7.6.3    martin 	    family);
    148      1.1  christos 
    149      1.1  christos 	sc->sc_smn = parent;
    150  1.7.6.2    martin 
    151  1.7.6.3    martin 	amdzentemp_init(sc);
    152      1.1  christos 
    153      1.1  christos 	aprint_normal("\n");
    154      1.1  christos 
    155      1.1  christos 	sc->sc_sme = sysmon_envsys_create();
    156      1.1  christos 	sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
    157      1.1  christos 	sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);
    158      1.1  christos 
    159  1.7.6.3    martin 	amdzentemp_setup_sensors(sc, device_unit(self));
    160      1.1  christos 
    161      1.1  christos 	/*
    162      1.1  christos 	 * Set properties in sensors.
    163      1.1  christos 	 */
    164      1.1  christos 	for (i = 0; i < sc->sc_numsensors; i++) {
    165      1.1  christos 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
    166      1.1  christos 			goto bad;
    167      1.1  christos 	}
    168      1.1  christos 
    169      1.1  christos 	/*
    170      1.1  christos 	 * Register the sysmon_envsys device.
    171      1.1  christos 	 */
    172      1.1  christos 	sc->sc_sme->sme_name = device_xname(self);
    173      1.1  christos 	sc->sc_sme->sme_cookie = sc;
    174      1.1  christos 
    175  1.7.6.3    martin 	switch (family) {
    176  1.7.6.3    martin 	case 0x15:
    177  1.7.6.3    martin 		sc->sc_sme->sme_refresh = amdzentemp_family15_refresh;
    178  1.7.6.3    martin 		break;
    179  1.7.6.3    martin 	case 0x17:
    180  1.7.6.3    martin 		sc->sc_sme->sme_refresh = amdzentemp_family17_refresh;
    181  1.7.6.3    martin 		break;
    182  1.7.6.3    martin 	default:
    183  1.7.6.3    martin 		/* XXX panic */
    184  1.7.6.3    martin 		break;
    185  1.7.6.3    martin 	}
    186      1.1  christos 
    187      1.1  christos 	error = sysmon_envsys_register(sc->sc_sme);
    188      1.1  christos 	if (error) {
    189      1.1  christos 		aprint_error_dev(self, "unable to register with sysmon "
    190      1.1  christos 		    "(error=%d)\n", error);
    191      1.1  christos 		goto bad;
    192      1.1  christos 	}
    193      1.1  christos 
    194      1.1  christos 	(void)pmf_device_register(self, NULL, NULL);
    195      1.1  christos 
    196      1.1  christos 	return;
    197      1.1  christos 
    198      1.1  christos bad:
    199      1.1  christos 	if (sc->sc_sme != NULL) {
    200      1.1  christos 		sysmon_envsys_destroy(sc->sc_sme);
    201      1.1  christos 		sc->sc_sme = NULL;
    202      1.1  christos 	}
    203      1.1  christos 
    204      1.7  pgoyette 	kmem_free(sc->sc_sensor, sc->sc_sensor_len);
    205      1.7  pgoyette 	sc->sc_sensor = NULL;
    206      1.1  christos }
    207      1.1  christos 
    208      1.1  christos static int
    209      1.1  christos amdzentemp_detach(device_t self, int flags)
    210      1.1  christos {
    211      1.1  christos 	struct amdzentemp_softc *sc = device_private(self);
    212      1.1  christos 
    213      1.1  christos 	pmf_device_deregister(self);
    214      1.1  christos 	if (sc->sc_sme != NULL)
    215      1.1  christos 		sysmon_envsys_unregister(sc->sc_sme);
    216      1.1  christos 
    217      1.1  christos 	if (sc->sc_sensor != NULL)
    218      1.1  christos 		kmem_free(sc->sc_sensor, sc->sc_sensor_len);
    219      1.1  christos 
    220      1.1  christos 	return 0;
    221      1.1  christos }
    222      1.1  christos 
    223      1.1  christos 
    224      1.1  christos static void
    225  1.7.6.3    martin amdzentemp_init(struct amdzentemp_softc *sc)
    226      1.1  christos {
    227  1.7.6.2    martin 
    228      1.1  christos 	sc->sc_numsensors = 1;
    229  1.7.6.2    martin 	sc->sc_offset = 0;
    230  1.7.6.2    martin 
    231  1.7.6.2    martin 	if (strstr(cpu_brand_string, "AMD Ryzen 5 1600X")
    232  1.7.6.2    martin 	    || strstr(cpu_brand_string, "AMD Ryzen 7 1700X")
    233  1.7.6.2    martin 	    || strstr(cpu_brand_string, "AMD Ryzen 7 1800X"))
    234  1.7.6.2    martin 		sc->sc_offset = -20000000;
    235  1.7.6.2    martin 	else if (strstr(cpu_brand_string, "AMD Ryzen 7 2700X"))
    236  1.7.6.2    martin 		sc->sc_offset = -10000000;
    237  1.7.6.2    martin 	else if (strstr(cpu_brand_string, "AMD Ryzen Threadripper 19")
    238  1.7.6.2    martin 	    || strstr(cpu_brand_string, "AMD Ryzen Threadripper 29"))
    239  1.7.6.2    martin 		sc->sc_offset = -27000000;
    240      1.1  christos }
    241      1.1  christos 
    242      1.1  christos static void
    243  1.7.6.3    martin amdzentemp_setup_sensors(struct amdzentemp_softc *sc, int dv_unit)
    244      1.1  christos {
    245      1.1  christos 	sc->sc_sensor[0].units = ENVSYS_STEMP;
    246      1.1  christos 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    247      1.1  christos 	sc->sc_sensor[0].flags = ENVSYS_FHAS_ENTROPY;
    248      1.1  christos 
    249      1.1  christos 	snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
    250      1.1  christos 	    "cpu%u temperature", dv_unit);
    251      1.1  christos }
    252      1.1  christos 
    253      1.1  christos static void
    254  1.7.6.3    martin amdzentemp_family15_refresh(struct sysmon_envsys *sme,
    255  1.7.6.3    martin     envsys_data_t *edata)
    256  1.7.6.3    martin {
    257  1.7.6.3    martin 	struct amdzentemp_softc *sc = sme->sme_cookie;
    258  1.7.6.3    martin 	uint32_t val, temp;
    259  1.7.6.3    martin 	int error;
    260  1.7.6.3    martin 
    261  1.7.6.3    martin 	error = amdsmn_read(sc->sc_smn, AMD_15H_M60H_REPTMP_CTRL, &val);
    262  1.7.6.3    martin 	if (error) {
    263  1.7.6.3    martin 		edata->state = ENVSYS_SINVALID;
    264  1.7.6.3    martin 		return;
    265  1.7.6.3    martin 	}
    266  1.7.6.3    martin 
    267  1.7.6.3    martin 	/* from amdtemp.c:amdtemp_family10_refresh() */
    268  1.7.6.3    martin 	temp = __SHIFTOUT(val, F10_TEMP_CURTMP);
    269  1.7.6.3    martin 
    270  1.7.6.3    martin 	/* From Celsius to micro-Kelvin. */
    271  1.7.6.3    martin 	edata->value_cur = (temp * 125000) + 273150000;
    272  1.7.6.3    martin 
    273  1.7.6.3    martin 	/*
    274  1.7.6.3    martin 	 * On Family 15h and higher, if CurTmpTjSel is 11b, the range is
    275  1.7.6.3    martin 	 * adjusted down by 49.0 degrees Celsius.  (This adjustment is not
    276  1.7.6.3    martin 	 * documented in BKDGs prior to family 15h model 00h.)
    277  1.7.6.3    martin 	 *
    278  1.7.6.3    martin 	 * XXX should be in amdtemp.c:amdtemp_family10_refresh() for f15
    279  1.7.6.3    martin 	 *     as well??
    280  1.7.6.3    martin 	 */
    281  1.7.6.3    martin 	if (__SHIFTOUT(val, F15M60_CURTMP_TJSEL) == 0x3)
    282  1.7.6.3    martin 		edata->value_cur -= AMD_CURTMP_RANGE_ADJUST;
    283  1.7.6.3    martin 
    284  1.7.6.3    martin 	edata->state = ENVSYS_SVALID;
    285  1.7.6.3    martin }
    286  1.7.6.3    martin 
    287  1.7.6.3    martin static void
    288      1.1  christos amdzentemp_family17_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    289      1.1  christos {
    290      1.1  christos 	struct amdzentemp_softc *sc = sme->sme_cookie;
    291      1.1  christos 	uint32_t temp;
    292      1.1  christos 	int error;
    293      1.1  christos 
    294      1.1  christos 	error = amdsmn_read(sc->sc_smn, AMD_17H_CUR_TMP, &temp);
    295      1.1  christos 	if (error) {
    296      1.1  christos 		edata->state = ENVSYS_SINVALID;
    297      1.1  christos 		return;
    298      1.1  christos 	}
    299      1.1  christos 	edata->state = ENVSYS_SVALID;
    300      1.1  christos 	/* From C to uK. */
    301      1.1  christos 	edata->value_cur = ((temp >> 21) * 125000) + 273150000;
    302  1.7.6.1  christos 	/* adjust for possible offset of 49K */
    303  1.7.6.3    martin 	if (temp & AMD_CURTMP_RANGE_CHECK)
    304  1.7.6.3    martin 		edata->value_cur -= AMD_CURTMP_RANGE_ADJUST;
    305  1.7.6.2    martin 	edata->value_cur += sc->sc_offset;
    306      1.1  christos }
    307      1.1  christos 
    308      1.6  pgoyette MODULE(MODULE_CLASS_DRIVER, amdzentemp, "sysmon_envsys,amdsmn");
    309      1.1  christos 
    310      1.1  christos #ifdef _MODULE
    311      1.1  christos #include "ioconf.c"
    312      1.1  christos #endif
    313      1.1  christos 
    314      1.1  christos static int
    315      1.1  christos amdzentemp_modcmd(modcmd_t cmd, void *aux)
    316      1.1  christos {
    317      1.1  christos 	int error = 0;
    318      1.1  christos 
    319      1.1  christos 	switch (cmd) {
    320      1.1  christos 	case MODULE_CMD_INIT:
    321      1.1  christos #ifdef _MODULE
    322      1.1  christos 		error = config_init_component(cfdriver_ioconf_amdzentemp,
    323      1.1  christos 		    cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
    324      1.1  christos #endif
    325      1.1  christos 		return error;
    326      1.1  christos 	case MODULE_CMD_FINI:
    327      1.1  christos #ifdef _MODULE
    328      1.1  christos 		error = config_fini_component(cfdriver_ioconf_amdzentemp,
    329      1.1  christos 		    cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
    330      1.1  christos #endif
    331      1.1  christos 		return error;
    332      1.1  christos 	default:
    333      1.1  christos 		return ENOTTY;
    334      1.1  christos 	}
    335      1.1  christos }
    336