Home | History | Annotate | Line # | Download | only in pci
amdtemp.c revision 1.12
      1  1.12  jmcneill /*      $NetBSD: amdtemp.c,v 1.12 2011/07/31 22:04:07 jmcneill Exp $ */
      2   1.1    cegger /*      $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */
      3   1.1    cegger 
      4  1.10    jruoho /*
      5   1.1    cegger  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      6   1.1    cegger  * All rights reserved.
      7   1.1    cegger  *
      8   1.1    cegger  * This code is derived from software contributed to The NetBSD Foundation
      9   1.1    cegger  * by Christoph Egger.
     10   1.1    cegger  *
     11   1.1    cegger  * Redistribution and use in source and binary forms, with or without
     12   1.1    cegger  * modification, are permitted provided that the following conditions
     13   1.1    cegger  * are met:
     14   1.1    cegger  * 1. Redistributions of source code must retain the above copyright
     15   1.1    cegger  *    notice, this list of conditions and the following disclaimer.
     16   1.1    cegger  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1    cegger  *    notice, this list of conditions and the following disclaimer in the
     18   1.1    cegger  *    documentation and/or other materials provided with the distribution.
     19   1.1    cegger  *
     20   1.1    cegger  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1    cegger  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1    cegger  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1    cegger  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1    cegger  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1    cegger  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1    cegger  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1    cegger  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1    cegger  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1    cegger  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1    cegger  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1    cegger  */
     32   1.1    cegger 
     33   1.1    cegger /*
     34   1.1    cegger  * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd (at) bugmail.mojo.ru>
     35   1.1    cegger  *
     36   1.1    cegger  * Permission to use, copy, modify, and distribute this software for any
     37   1.1    cegger  * purpose with or without fee is hereby granted, provided that the above
     38   1.1    cegger  * copyright notice and this permission notice appear in all copies.
     39   1.1    cegger  *
     40   1.1    cegger  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     41   1.1    cegger  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     42   1.1    cegger  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     43   1.1    cegger  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     44   1.1    cegger  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     45   1.1    cegger  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     46   1.1    cegger  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     47   1.1    cegger  */
     48   1.1    cegger 
     49   1.1    cegger 
     50   1.1    cegger #include <sys/cdefs.h>
     51  1.12  jmcneill __KERNEL_RCSID(0, "$NetBSD: amdtemp.c,v 1.12 2011/07/31 22:04:07 jmcneill Exp $ ");
     52   1.1    cegger 
     53   1.1    cegger #include <sys/param.h>
     54  1.10    jruoho #include <sys/bus.h>
     55  1.10    jruoho #include <sys/cpu.h>
     56   1.1    cegger #include <sys/systm.h>
     57   1.1    cegger #include <sys/device.h>
     58   1.1    cegger #include <sys/kmem.h>
     59  1.10    jruoho #include <sys/module.h>
     60   1.1    cegger 
     61   1.1    cegger #include <machine/specialreg.h>
     62   1.1    cegger 
     63   1.1    cegger #include <dev/pci/pcireg.h>
     64   1.1    cegger #include <dev/pci/pcivar.h>
     65   1.1    cegger #include <dev/pci/pcidevs.h>
     66   1.1    cegger 
     67  1.10    jruoho #include <dev/sysmon/sysmonvar.h>
     68  1.10    jruoho 
     69   1.1    cegger /*
     70   1.1    cegger  * AMD K8:
     71   1.1    cegger  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/32559.pdf
     72   1.8    cegger  * AMD K8 Errata: #141
     73   1.8    cegger  * http://support.amd.com/us/Processor_TechDocs/33610_PUB_Rev3%2042v3.pdf
     74   1.8    cegger  *
     75   1.1    cegger  * Family10h:
     76   1.1    cegger  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/31116.PDF
     77   1.9    cegger  * Family10h Errata: #319
     78   1.9    cegger  * http://support.amd.com/de/Processor_TechDocs/41322.pdf
     79   1.8    cegger  *
     80   1.8    cegger  * Family11h:
     81   1.8    cegger  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/41256.pdf
     82   1.1    cegger  */
     83   1.1    cegger 
     84  1.12  jmcneill /* AMD Processors, Function 3 -- Miscellaneous Control
     85   1.1    cegger  */
     86   1.1    cegger 
     87   1.1    cegger /* Function 3 Registers */
     88   1.1    cegger #define THERMTRIP_STAT_R      0xe4
     89   1.1    cegger #define NORTHBRIDGE_CAP_R     0xe8
     90   1.1    cegger #define CPUID_FAMILY_MODEL_R  0xfc
     91   1.1    cegger 
     92   1.1    cegger /*
     93   1.1    cegger  * AMD NPT Family 0Fh Processors, Function 3 -- Miscellaneous Control
     94   1.1    cegger  */
     95   1.1    cegger 
     96   1.1    cegger /* Bits within Thermtrip Status Register */
     97   1.1    cegger #define K8_THERM_SENSE_SEL       (1 << 6)
     98   1.1    cegger #define K8_THERM_SENSE_CORE_SEL  (1 << 2)
     99   1.1    cegger 
    100   1.1    cegger /* Flip core and sensor selection bits */
    101   1.1    cegger #define K8_T_SEL_C0(v)           (v |= K8_THERM_SENSE_CORE_SEL)
    102   1.1    cegger #define K8_T_SEL_C1(v)           (v &= ~(K8_THERM_SENSE_CORE_SEL))
    103   1.1    cegger #define K8_T_SEL_S0(v)           (v &= ~(K8_THERM_SENSE_SEL))
    104   1.1    cegger #define K8_T_SEL_S1(v)           (v |= K8_THERM_SENSE_SEL)
    105   1.1    cegger 
    106   1.1    cegger 
    107   1.1    cegger 
    108   1.1    cegger /*
    109  1.12  jmcneill  * AMD Family 10h Processors, Function 3 -- Miscellaneous Control
    110   1.1    cegger  */
    111   1.1    cegger 
    112   1.1    cegger /* Function 3 Registers */
    113   1.1    cegger #define F10_TEMPERATURE_CTL_R	0xa4
    114   1.1    cegger 
    115   1.1    cegger /* Bits within Reported Temperature Control Register */
    116   1.1    cegger #define F10_TEMP_CURTEMP	(1 << 21)
    117   1.1    cegger 
    118   1.1    cegger /*
    119  1.10    jruoho  * Revision Guide for AMD NPT Family 0Fh Processors,
    120   1.1    cegger  * Publication # 33610, Revision 3.30, February 2008
    121   1.1    cegger  */
    122   1.5    cegger #define K8_SOCKET_F	1	/* Server */
    123   1.5    cegger #define K8_SOCKET_AM2	2	/* Desktop */
    124   1.5    cegger #define K8_SOCKET_S1	3	/* Laptop */
    125   1.5    cegger 
    126   1.1    cegger static const struct {
    127   1.1    cegger 	const char      rev[5];
    128   1.5    cegger 	const struct {
    129   1.5    cegger 		const pcireg_t  cpuid;
    130   1.5    cegger 		const uint8_t   socket;
    131   1.5    cegger 	} cpu[5];
    132   1.1    cegger } amdtemp_core[] = {
    133   1.5    cegger 	{ "BH-F", { { 0x00040FB0, K8_SOCKET_AM2 },	/* F2 */
    134   1.5    cegger 		  { 0x00040F80, K8_SOCKET_S1 },		/* F2 */
    135   1.5    cegger 		  { 0, 0 }, { 0, 0 }, { 0, 0 } } },
    136   1.5    cegger 	{ "DH-F", { { 0x00040FF0, K8_SOCKET_AM2 },	/* F2 */
    137   1.5    cegger 		  { 0x00040FC0, K8_SOCKET_S1 },		/* F2 */
    138   1.5    cegger 		  { 0x00050FF0, K8_SOCKET_AM2 },	/* F2, F3 */
    139   1.5    cegger 		  { 0, 0 }, { 0, 0 } } },
    140   1.5    cegger 	{ "JH-F", { { 0x00040F10, K8_SOCKET_F },	/* F2, F3 */
    141   1.5    cegger 		  { 0x00040F30, K8_SOCKET_AM2 },	/* F2, F3 */
    142   1.5    cegger 		  { 0x000C0F10, K8_SOCKET_F },		/* F3 */
    143   1.5    cegger 		  { 0, 0 }, { 0, 0 } } },
    144   1.5    cegger 	{ "BH-G", { { 0x00060FB0, K8_SOCKET_AM2 },	/* G1, G2 */
    145   1.5    cegger 		  { 0x00060F80, K8_SOCKET_S1 },		/* G1, G2 */
    146   1.5    cegger 		  { 0, 0 }, { 0, 0 }, { 0, 0 } } },
    147   1.5    cegger 	{ "DH-G", { { 0x00060FF0, K8_SOCKET_AM2 },	/* G1, G2 */
    148   1.5    cegger 		  { 0x00060FC0, K8_SOCKET_S1 },		/* G2 */
    149   1.5    cegger 		  { 0x00070FF0, K8_SOCKET_AM2 },	/* G1, G2 */
    150   1.5    cegger 		  { 0x00070FC0, K8_SOCKET_S1 },		/* G2 */
    151   1.5    cegger 		  { 0, 0 } } }
    152   1.1    cegger };
    153   1.1    cegger 
    154   1.1    cegger 
    155   1.1    cegger struct amdtemp_softc {
    156   1.1    cegger         pci_chipset_tag_t sc_pc;
    157   1.1    cegger         pcitag_t sc_pcitag;
    158   1.1    cegger 
    159   1.1    cegger 	struct sysmon_envsys *sc_sme;
    160   1.1    cegger 	envsys_data_t *sc_sensor;
    161  1.10    jruoho 	size_t sc_sensor_len;
    162   1.1    cegger 
    163   1.1    cegger         char sc_rev;
    164   1.1    cegger         int8_t sc_numsensors;
    165   1.1    cegger 	uint32_t sc_family;
    166   1.5    cegger 	int32_t sc_adjustment;
    167   1.1    cegger };
    168   1.1    cegger 
    169   1.1    cegger 
    170  1.10    jruoho static int  amdtemp_match(device_t, cfdata_t, void *);
    171   1.1    cegger static void amdtemp_attach(device_t, device_t, void *);
    172  1.10    jruoho static int  amdtemp_detach(device_t, int);
    173   1.1    cegger 
    174   1.1    cegger static void amdtemp_k8_init(struct amdtemp_softc *, pcireg_t);
    175   1.1    cegger static void amdtemp_k8_setup_sensors(struct amdtemp_softc *, int);
    176   1.1    cegger static void amdtemp_k8_refresh(struct sysmon_envsys *, envsys_data_t *);
    177   1.1    cegger 
    178   1.1    cegger static void amdtemp_family10_init(struct amdtemp_softc *);
    179   1.1    cegger static void amdtemp_family10_setup_sensors(struct amdtemp_softc *, int);
    180   1.1    cegger static void amdtemp_family10_refresh(struct sysmon_envsys *, envsys_data_t *);
    181   1.1    cegger 
    182  1.10    jruoho CFATTACH_DECL_NEW(amdtemp, sizeof(struct amdtemp_softc),
    183  1.10    jruoho 	amdtemp_match, amdtemp_attach, amdtemp_detach, NULL);
    184   1.1    cegger 
    185   1.1    cegger static int
    186   1.1    cegger amdtemp_match(device_t parent, cfdata_t match, void *aux)
    187   1.1    cegger {
    188   1.1    cegger 	struct pci_attach_args *pa = aux;
    189   1.1    cegger 	pcireg_t cpu_signature;
    190   1.1    cegger 	uint32_t family;
    191   1.1    cegger 
    192   1.1    cegger 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
    193   1.1    cegger 		return 0;
    194   1.1    cegger 
    195   1.1    cegger 	switch (PCI_PRODUCT(pa->pa_id)) {
    196   1.1    cegger 	case PCI_PRODUCT_AMD_AMD64_MISC:
    197   1.1    cegger 	case PCI_PRODUCT_AMD_AMD64_F10_MISC:
    198   1.1    cegger 	case PCI_PRODUCT_AMD_AMD64_F11_MISC:
    199  1.12  jmcneill 	case PCI_PRODUCT_AMD_F14_NB:
    200   1.1    cegger 		break;
    201   1.1    cegger 	default:
    202   1.1    cegger 		return 0;
    203   1.1    cegger 	}
    204   1.1    cegger 
    205  1.11    jruoho 	cpu_signature = pci_conf_read(pa->pa_pc,
    206  1.11    jruoho 	    pa->pa_tag, CPUID_FAMILY_MODEL_R);
    207   1.1    cegger 
    208   1.1    cegger 	/* This CPUID northbridge register has been introduced
    209   1.1    cegger 	 * in Revision F */
    210   1.1    cegger 	if (cpu_signature == 0x0)
    211   1.1    cegger 		return 0;
    212   1.1    cegger 
    213   1.1    cegger 	family = CPUID2FAMILY(cpu_signature);
    214   1.1    cegger 	if (family == 0xf)
    215   1.1    cegger 		family += CPUID2EXTFAMILY(cpu_signature);
    216   1.1    cegger 
    217   1.9    cegger 	/* Errata #319: This has been fixed in Revision C2. */
    218   1.9    cegger 	if (family == 0x10) {
    219   1.9    cegger 		if (CPUID2MODEL(cpu_signature) < 4)
    220   1.9    cegger 			return 0;
    221   1.9    cegger 		if (CPUID2MODEL(cpu_signature) == 4
    222   1.9    cegger 		    && CPUID2STEPPING(cpu_signature) < 2)
    223   1.9    cegger 			return 0;
    224   1.9    cegger 	}
    225   1.9    cegger 
    226   1.9    cegger 
    227   1.1    cegger 	/* Not yet supported CPUs */
    228  1.12  jmcneill 	if (family > 0x14)
    229   1.1    cegger 		return 0;
    230   1.1    cegger 
    231   1.1    cegger 	return 2;	/* supercede pchb(4) */
    232   1.1    cegger }
    233   1.1    cegger 
    234   1.1    cegger static void
    235   1.1    cegger amdtemp_attach(device_t parent, device_t self, void *aux)
    236   1.1    cegger {
    237   1.1    cegger 	struct amdtemp_softc *sc = device_private(self);
    238   1.1    cegger 	struct pci_attach_args *pa = aux;
    239   1.1    cegger 	pcireg_t cpu_signature;
    240   1.1    cegger 	int error;
    241   1.1    cegger 	uint8_t i;
    242   1.1    cegger 
    243   1.1    cegger 	aprint_naive("\n");
    244   1.7    cegger 	aprint_normal(": AMD CPU Temperature Sensors");
    245   1.1    cegger 
    246  1.11    jruoho 	cpu_signature = pci_conf_read(pa->pa_pc,
    247  1.11    jruoho 	    pa->pa_tag, CPUID_FAMILY_MODEL_R);
    248   1.1    cegger 
    249   1.1    cegger 	/* If we hit this, then match routine is wrong. */
    250   1.1    cegger 	KASSERT(cpu_signature != 0x0);
    251   1.1    cegger 
    252  1.11    jruoho 	sc->sc_family = CPUID2FAMILY(cpu_signature);
    253  1.11    jruoho 	sc->sc_family += CPUID2EXTFAMILY(cpu_signature);
    254  1.11    jruoho 
    255   1.1    cegger 	KASSERT(sc->sc_family >= 0xf);
    256   1.1    cegger 
    257  1.11    jruoho 	sc->sc_sme = NULL;
    258  1.11    jruoho 	sc->sc_sensor = NULL;
    259  1.11    jruoho 
    260   1.1    cegger 	sc->sc_pc = pa->pa_pc;
    261   1.1    cegger 	sc->sc_pcitag = pa->pa_tag;
    262   1.5    cegger 	sc->sc_adjustment = 0;
    263   1.1    cegger 
    264   1.1    cegger 	switch (sc->sc_family) {
    265   1.1    cegger 	case 0xf:  /* AMD K8 NPT */
    266   1.1    cegger 		amdtemp_k8_init(sc, cpu_signature);
    267   1.1    cegger 		break;
    268   1.1    cegger 
    269   1.1    cegger 	case 0x10: /* AMD Barcelona/Phenom */
    270   1.1    cegger 	case 0x11: /* AMD Griffin */
    271  1.12  jmcneill 	case 0x14: /* AMD Fusion */
    272   1.1    cegger 		amdtemp_family10_init(sc);
    273   1.1    cegger 		break;
    274   1.1    cegger 
    275   1.1    cegger 	default:
    276   1.7    cegger 		aprint_normal(", family 0x%x not supported\n",
    277   1.7    cegger 			     sc->sc_family);
    278   1.1    cegger 		return;
    279   1.1    cegger 	}
    280   1.1    cegger 
    281   1.1    cegger 	aprint_normal("\n");
    282   1.1    cegger 
    283   1.5    cegger 	if (sc->sc_adjustment != 0)
    284   1.5    cegger 		aprint_debug_dev(self, "Workaround enabled\n");
    285   1.5    cegger 
    286   1.1    cegger 	sc->sc_sme = sysmon_envsys_create();
    287  1.10    jruoho 	sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
    288  1.11    jruoho 	sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);
    289  1.11    jruoho 
    290  1.10    jruoho 	if (sc->sc_sensor == NULL)
    291  1.11    jruoho 		goto bad;
    292   1.1    cegger 
    293   1.1    cegger 	switch (sc->sc_family) {
    294   1.1    cegger 	case 0xf:
    295   1.1    cegger 		amdtemp_k8_setup_sensors(sc, device_unit(self));
    296   1.1    cegger 		break;
    297   1.1    cegger 	case 0x10:
    298   1.1    cegger 	case 0x11:
    299  1.12  jmcneill 	case 0x14:
    300   1.1    cegger 		amdtemp_family10_setup_sensors(sc, device_unit(self));
    301   1.1    cegger 		break;
    302   1.1    cegger 	}
    303   1.1    cegger 
    304   1.1    cegger 	/*
    305   1.1    cegger 	 * Set properties in sensors.
    306   1.1    cegger 	 */
    307   1.1    cegger 	for (i = 0; i < sc->sc_numsensors; i++) {
    308   1.1    cegger 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    309   1.1    cegger 						&sc->sc_sensor[i]))
    310   1.1    cegger 			goto bad;
    311   1.1    cegger 	}
    312   1.1    cegger 
    313   1.1    cegger 	/*
    314   1.1    cegger 	 * Register the sysmon_envsys device.
    315   1.1    cegger 	 */
    316   1.1    cegger 	sc->sc_sme->sme_name = device_xname(self);
    317   1.1    cegger 	sc->sc_sme->sme_cookie = sc;
    318   1.1    cegger 
    319   1.1    cegger 	switch (sc->sc_family) {
    320   1.1    cegger 	case 0xf:
    321   1.1    cegger 		sc->sc_sme->sme_refresh = amdtemp_k8_refresh;
    322   1.1    cegger 		break;
    323   1.1    cegger 	case 0x10:
    324   1.1    cegger 	case 0x11:
    325  1.12  jmcneill 	case 0x14:
    326   1.1    cegger 		sc->sc_sme->sme_refresh = amdtemp_family10_refresh;
    327   1.1    cegger 		break;
    328   1.1    cegger 	}
    329   1.1    cegger 
    330   1.1    cegger 	error = sysmon_envsys_register(sc->sc_sme);
    331   1.1    cegger 	if (error) {
    332   1.1    cegger 		aprint_error_dev(self, "unable to register with sysmon "
    333   1.1    cegger 			"(error=%d)\n", error);
    334   1.1    cegger 		goto bad;
    335   1.1    cegger 	}
    336   1.1    cegger 
    337  1.11    jruoho 	(void)pmf_device_register(self, NULL, NULL);
    338   1.1    cegger 
    339   1.1    cegger 	return;
    340   1.1    cegger 
    341   1.1    cegger bad:
    342  1.11    jruoho 	if (sc->sc_sme != NULL) {
    343  1.11    jruoho 		sysmon_envsys_destroy(sc->sc_sme);
    344  1.11    jruoho 		sc->sc_sme = NULL;
    345  1.11    jruoho 	}
    346  1.10    jruoho 
    347  1.11    jruoho 	if (sc->sc_sensor != NULL) {
    348  1.11    jruoho 		kmem_free(sc->sc_sensor, sc->sc_sensor_len);
    349  1.11    jruoho 		sc->sc_sensor = NULL;
    350  1.11    jruoho 	}
    351  1.10    jruoho }
    352  1.10    jruoho 
    353  1.10    jruoho static int
    354  1.10    jruoho amdtemp_detach(device_t self, int flags)
    355  1.10    jruoho {
    356  1.10    jruoho 	struct amdtemp_softc *sc = device_private(self);
    357  1.10    jruoho 
    358  1.10    jruoho 	if (sc->sc_sme != NULL)
    359  1.10    jruoho 		sysmon_envsys_unregister(sc->sc_sme);
    360  1.10    jruoho 
    361  1.10    jruoho 	if (sc->sc_sensor != NULL)
    362  1.10    jruoho 		kmem_free(sc->sc_sensor, sc->sc_sensor_len);
    363  1.10    jruoho 
    364  1.10    jruoho 	return 0;
    365   1.1    cegger }
    366   1.1    cegger 
    367   1.1    cegger static void
    368   1.1    cegger amdtemp_k8_init(struct amdtemp_softc *sc, pcireg_t cpu_signature)
    369   1.1    cegger {
    370   1.1    cegger 	pcireg_t data;
    371   1.1    cegger 	uint32_t cmpcap;
    372   1.1    cegger 	uint8_t i, j;
    373   1.1    cegger 
    374   1.1    cegger 	aprint_normal(" (K8");
    375   1.1    cegger 
    376   1.1    cegger 	for (i = 0; i < __arraycount(amdtemp_core) && sc->sc_rev == '\0'; i++) {
    377   1.5    cegger 		for (j = 0; amdtemp_core[i].cpu[j].cpuid != 0; j++) {
    378   1.1    cegger 			if ((cpu_signature & ~0xf)
    379   1.5    cegger 			    != amdtemp_core[i].cpu[j].cpuid)
    380   1.5    cegger 				continue;
    381   1.5    cegger 
    382   1.5    cegger 			sc->sc_rev = amdtemp_core[i].rev[3];
    383   1.5    cegger 			aprint_normal(": core rev %.4s%.1x",
    384   1.5    cegger 				amdtemp_core[i].rev,
    385   1.5    cegger 				CPUID2STEPPING(cpu_signature));
    386   1.5    cegger 
    387   1.5    cegger 			switch (amdtemp_core[i].cpu[j].socket) {
    388   1.5    cegger 			case K8_SOCKET_AM2:
    389   1.6    cegger 				if (sc->sc_rev == 'G')
    390   1.6    cegger 					sc->sc_adjustment = 21000000;
    391   1.5    cegger 				aprint_normal(", socket AM2");
    392   1.5    cegger 				break;
    393   1.5    cegger 			case K8_SOCKET_S1:
    394   1.5    cegger 				aprint_normal(", socket S1");
    395   1.5    cegger 				break;
    396   1.5    cegger 			case K8_SOCKET_F:
    397   1.5    cegger 				aprint_normal(", socket F");
    398   1.5    cegger 				break;
    399   1.1    cegger 			}
    400   1.1    cegger 		}
    401   1.1    cegger 	}
    402   1.1    cegger 
    403   1.1    cegger 	if (sc->sc_rev == '\0') {
    404   1.1    cegger 		/* CPUID Family Model Register was introduced in
    405   1.1    cegger 		 * Revision F */
    406   1.1    cegger 		sc->sc_rev = 'G';       /* newer than E, assume G */
    407   1.1    cegger 		aprint_normal(": cpuid 0x%x", cpu_signature);
    408   1.1    cegger 	}
    409   1.1    cegger 
    410   1.1    cegger 	aprint_normal(")");
    411   1.1    cegger 
    412   1.1    cegger 	data = pci_conf_read(sc->sc_pc, sc->sc_pcitag, NORTHBRIDGE_CAP_R);
    413   1.1    cegger 	cmpcap = (data >> 12) & 0x3;
    414   1.1    cegger 
    415   1.1    cegger 	sc->sc_numsensors = cmpcap ? 4 : 2;
    416   1.1    cegger }
    417   1.1    cegger 
    418   1.1    cegger 
    419   1.1    cegger static void
    420   1.1    cegger amdtemp_k8_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
    421   1.1    cegger {
    422   1.1    cegger 	uint8_t i;
    423   1.1    cegger 
    424   1.1    cegger 	/* There are two sensors per CPU core. So we use the
    425   1.1    cegger 	 * device unit as socket counter to correctly enumerate
    426   1.1    cegger 	 * the CPUs on multi-socket machines.
    427   1.1    cegger 	 */
    428   1.1    cegger 	dv_unit *= (sc->sc_numsensors / 2);
    429   1.1    cegger 	for (i = 0; i < sc->sc_numsensors; i++) {
    430   1.1    cegger 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    431   1.1    cegger 		sc->sc_sensor[i].state = ENVSYS_SVALID;
    432   1.1    cegger 
    433   1.1    cegger 		snprintf(sc->sc_sensor[i].desc, sizeof(sc->sc_sensor[i].desc),
    434   1.1    cegger 			"CPU%u Sensor%u", dv_unit + (i / 2), i % 2);
    435   1.1    cegger 	}
    436   1.1    cegger }
    437   1.1    cegger 
    438   1.1    cegger 
    439   1.1    cegger static void
    440   1.1    cegger amdtemp_k8_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    441   1.1    cegger {
    442   1.1    cegger 	struct amdtemp_softc *sc = sme->sme_cookie;
    443   1.1    cegger 	pcireg_t status, match, tmp;
    444   1.1    cegger 	uint32_t value;
    445   1.1    cegger 
    446   1.1    cegger 	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
    447   1.1    cegger 
    448   1.1    cegger 	switch(edata->sensor) { /* sensor number */
    449   1.1    cegger 	case 0: /* Core 0 Sensor 0 */
    450   1.1    cegger 		K8_T_SEL_C0(status);
    451   1.1    cegger 		K8_T_SEL_S0(status);
    452   1.1    cegger 		break;
    453   1.1    cegger 	case 1: /* Core 0 Sensor 1 */
    454   1.1    cegger 		K8_T_SEL_C0(status);
    455   1.1    cegger 		K8_T_SEL_S1(status);
    456   1.1    cegger 		break;
    457   1.1    cegger 	case 2: /* Core 1 Sensor 0 */
    458   1.1    cegger 		K8_T_SEL_C1(status);
    459   1.1    cegger 		K8_T_SEL_S0(status);
    460   1.1    cegger 		break;
    461   1.1    cegger 	case 3: /* Core 1 Sensor 1 */
    462   1.1    cegger 		K8_T_SEL_C1(status);
    463   1.1    cegger 		K8_T_SEL_S1(status);
    464   1.1    cegger 		break;
    465   1.1    cegger 	}
    466   1.1    cegger 
    467   1.1    cegger 	match = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
    468   1.1    cegger 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R, status);
    469   1.1    cegger 	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
    470   1.1    cegger 	tmp = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
    471   1.1    cegger 
    472   1.1    cegger 	value = 0x3ff & (status >> 14);
    473   1.1    cegger 	if (sc->sc_rev != 'G')
    474   1.1    cegger 		value &= ~0x3;
    475   1.1    cegger 
    476   1.1    cegger 	edata->state = ENVSYS_SINVALID;
    477   1.1    cegger 	if ((tmp == match) && ((value & ~0x3) != 0)) {
    478   1.1    cegger 		edata->state = ENVSYS_SVALID;
    479   1.5    cegger 		edata->value_cur = (value * 250000 - 49000000) + 273150000
    480   1.5    cegger 			+ sc->sc_adjustment;
    481   1.1    cegger 	}
    482   1.1    cegger }
    483   1.1    cegger 
    484   1.1    cegger 
    485   1.1    cegger static void
    486   1.1    cegger amdtemp_family10_init(struct amdtemp_softc *sc)
    487   1.1    cegger {
    488  1.12  jmcneill 	aprint_normal(" (Family%02xh)", sc->sc_family);
    489   1.1    cegger 
    490   1.1    cegger 	sc->sc_numsensors = 1;
    491   1.1    cegger }
    492   1.1    cegger 
    493   1.1    cegger static void
    494   1.1    cegger amdtemp_family10_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
    495   1.1    cegger {
    496   1.1    cegger 	/* sanity check for future enhancements */
    497   1.1    cegger 	KASSERT(sc->sc_numsensors == 1);
    498   1.1    cegger 
    499   1.1    cegger 	/* There's one sensor per memory controller (= socket)
    500   1.1    cegger 	 * so we use the device unit as socket counter
    501   1.1    cegger 	 * to correctly enumerate the CPUs
    502   1.1    cegger 	 */
    503   1.1    cegger 	sc->sc_sensor[0].units = ENVSYS_STEMP;
    504   1.1    cegger 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    505   1.1    cegger 
    506   1.1    cegger 	snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
    507  1.12  jmcneill 		"cpu%u temperature", dv_unit);
    508   1.1    cegger }
    509   1.1    cegger 
    510   1.1    cegger 
    511   1.1    cegger static void
    512   1.1    cegger amdtemp_family10_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    513   1.1    cegger {
    514   1.1    cegger 	struct amdtemp_softc *sc = sme->sme_cookie;
    515   1.1    cegger 	pcireg_t status;
    516   1.1    cegger 	uint32_t value;
    517   1.1    cegger 
    518  1.11    jruoho 	status = pci_conf_read(sc->sc_pc,
    519  1.11    jruoho 	    sc->sc_pcitag, F10_TEMPERATURE_CTL_R);
    520   1.1    cegger 
    521   1.1    cegger 	value = (status >> 21);
    522   1.1    cegger 
    523   1.1    cegger 	edata->state = ENVSYS_SVALID;
    524  1.11    jruoho 	edata->value_cur = (value * 125000) + 273150000; /* From C to uK. */
    525   1.1    cegger }
    526  1.10    jruoho 
    527  1.10    jruoho MODULE(MODULE_CLASS_DRIVER, amdtemp, NULL);
    528  1.10    jruoho 
    529  1.10    jruoho #ifdef _MODULE
    530  1.10    jruoho #include "ioconf.c"
    531  1.10    jruoho #endif
    532  1.10    jruoho 
    533  1.10    jruoho static int
    534  1.10    jruoho amdtemp_modcmd(modcmd_t cmd, void *aux)
    535  1.10    jruoho {
    536  1.10    jruoho 	int error = 0;
    537  1.10    jruoho 
    538  1.10    jruoho 	switch (cmd) {
    539  1.10    jruoho 	case MODULE_CMD_INIT:
    540  1.10    jruoho #ifdef _MODULE
    541  1.10    jruoho 		error = config_init_component(cfdriver_ioconf_amdtemp,
    542  1.10    jruoho 		    cfattach_ioconf_amdtemp, cfdata_ioconf_amdtemp);
    543  1.10    jruoho #endif
    544  1.10    jruoho 		return error;
    545  1.10    jruoho 	case MODULE_CMD_FINI:
    546  1.10    jruoho #ifdef _MODULE
    547  1.10    jruoho 		error = config_fini_component(cfdriver_ioconf_amdtemp,
    548  1.10    jruoho 		    cfattach_ioconf_amdtemp, cfdata_ioconf_amdtemp);
    549  1.10    jruoho #endif
    550  1.10    jruoho 		return error;
    551  1.10    jruoho 	default:
    552  1.10    jruoho 		return ENOTTY;
    553  1.10    jruoho 	}
    554  1.10    jruoho }
    555