Home | History | Annotate | Line # | Download | only in pci
amdtemp.c revision 1.1.2.3
      1  1.1.2.3    yamt /*      $NetBSD: amdtemp.c,v 1.1.2.3 2009/06/20 07:20:12 yamt 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.1  cegger /*
      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.1.2.3    yamt __KERNEL_RCSID(0, "$NetBSD: amdtemp.c,v 1.1.2.3 2009/06/20 07:20:12 yamt Exp $ ");
     52      1.1  cegger 
     53      1.1  cegger #include <sys/param.h>
     54      1.1  cegger #include <sys/systm.h>
     55      1.1  cegger #include <sys/device.h>
     56      1.1  cegger #include <sys/kmem.h>
     57      1.1  cegger #include <dev/sysmon/sysmonvar.h>
     58      1.1  cegger 
     59  1.1.2.3    yamt #include <sys/bus.h>
     60  1.1.2.3    yamt #include <sys/cpu.h>
     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.1  cegger /*
     68      1.1  cegger  * AMD K8:
     69      1.1  cegger  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/32559.pdf
     70  1.1.2.3    yamt  * AMD K8 Errata: #141
     71  1.1.2.3    yamt  * http://support.amd.com/us/Processor_TechDocs/33610_PUB_Rev3%2042v3.pdf
     72  1.1.2.3    yamt  *
     73      1.1  cegger  * Family10h:
     74      1.1  cegger  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/31116.PDF
     75  1.1.2.3    yamt  *
     76  1.1.2.3    yamt  * Family11h:
     77  1.1.2.3    yamt  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/41256.pdf
     78      1.1  cegger  */
     79      1.1  cegger 
     80      1.1  cegger /* AMD Proessors, Function 3 -- Miscellaneous Control
     81      1.1  cegger  */
     82      1.1  cegger 
     83      1.1  cegger /* Function 3 Registers */
     84      1.1  cegger #define THERMTRIP_STAT_R      0xe4
     85      1.1  cegger #define NORTHBRIDGE_CAP_R     0xe8
     86      1.1  cegger #define CPUID_FAMILY_MODEL_R  0xfc
     87      1.1  cegger 
     88      1.1  cegger /*
     89      1.1  cegger  * AMD NPT Family 0Fh Processors, Function 3 -- Miscellaneous Control
     90      1.1  cegger  */
     91      1.1  cegger 
     92      1.1  cegger /* Bits within Thermtrip Status Register */
     93      1.1  cegger #define K8_THERM_SENSE_SEL       (1 << 6)
     94      1.1  cegger #define K8_THERM_SENSE_CORE_SEL  (1 << 2)
     95      1.1  cegger 
     96      1.1  cegger /* Flip core and sensor selection bits */
     97      1.1  cegger #define K8_T_SEL_C0(v)           (v |= K8_THERM_SENSE_CORE_SEL)
     98      1.1  cegger #define K8_T_SEL_C1(v)           (v &= ~(K8_THERM_SENSE_CORE_SEL))
     99      1.1  cegger #define K8_T_SEL_S0(v)           (v &= ~(K8_THERM_SENSE_SEL))
    100      1.1  cegger #define K8_T_SEL_S1(v)           (v |= K8_THERM_SENSE_SEL)
    101      1.1  cegger 
    102      1.1  cegger 
    103      1.1  cegger 
    104      1.1  cegger /*
    105      1.1  cegger  * AMD Family 10h Processorcs, Function 3 -- Miscellaneous Control
    106      1.1  cegger  */
    107      1.1  cegger 
    108      1.1  cegger /* Function 3 Registers */
    109      1.1  cegger #define F10_TEMPERATURE_CTL_R	0xa4
    110      1.1  cegger 
    111      1.1  cegger /* Bits within Reported Temperature Control Register */
    112      1.1  cegger #define F10_TEMP_CURTEMP	(1 << 21)
    113      1.1  cegger 
    114      1.1  cegger /*
    115      1.1  cegger  * Revision Guide for AMD NPT Family 0Fh Processors,
    116      1.1  cegger  * Publication # 33610, Revision 3.30, February 2008
    117      1.1  cegger  */
    118  1.1.2.2    yamt #define K8_SOCKET_F	1	/* Server */
    119  1.1.2.2    yamt #define K8_SOCKET_AM2	2	/* Desktop */
    120  1.1.2.2    yamt #define K8_SOCKET_S1	3	/* Laptop */
    121  1.1.2.2    yamt 
    122      1.1  cegger static const struct {
    123      1.1  cegger 	const char      rev[5];
    124  1.1.2.2    yamt 	const struct {
    125  1.1.2.2    yamt 		const pcireg_t  cpuid;
    126  1.1.2.2    yamt 		const uint8_t   socket;
    127  1.1.2.2    yamt 	} cpu[5];
    128      1.1  cegger } amdtemp_core[] = {
    129  1.1.2.2    yamt 	{ "BH-F", { { 0x00040FB0, K8_SOCKET_AM2 },	/* F2 */
    130  1.1.2.2    yamt 		  { 0x00040F80, K8_SOCKET_S1 },		/* F2 */
    131  1.1.2.2    yamt 		  { 0, 0 }, { 0, 0 }, { 0, 0 } } },
    132  1.1.2.2    yamt 	{ "DH-F", { { 0x00040FF0, K8_SOCKET_AM2 },	/* F2 */
    133  1.1.2.2    yamt 		  { 0x00040FC0, K8_SOCKET_S1 },		/* F2 */
    134  1.1.2.2    yamt 		  { 0x00050FF0, K8_SOCKET_AM2 },	/* F2, F3 */
    135  1.1.2.2    yamt 		  { 0, 0 }, { 0, 0 } } },
    136  1.1.2.2    yamt 	{ "JH-F", { { 0x00040F10, K8_SOCKET_F },	/* F2, F3 */
    137  1.1.2.2    yamt 		  { 0x00040F30, K8_SOCKET_AM2 },	/* F2, F3 */
    138  1.1.2.2    yamt 		  { 0x000C0F10, K8_SOCKET_F },		/* F3 */
    139  1.1.2.2    yamt 		  { 0, 0 }, { 0, 0 } } },
    140  1.1.2.2    yamt 	{ "BH-G", { { 0x00060FB0, K8_SOCKET_AM2 },	/* G1, G2 */
    141  1.1.2.2    yamt 		  { 0x00060F80, K8_SOCKET_S1 },		/* G1, G2 */
    142  1.1.2.2    yamt 		  { 0, 0 }, { 0, 0 }, { 0, 0 } } },
    143  1.1.2.2    yamt 	{ "DH-G", { { 0x00060FF0, K8_SOCKET_AM2 },	/* G1, G2 */
    144  1.1.2.2    yamt 		  { 0x00060FC0, K8_SOCKET_S1 },		/* G2 */
    145  1.1.2.2    yamt 		  { 0x00070FF0, K8_SOCKET_AM2 },	/* G1, G2 */
    146  1.1.2.2    yamt 		  { 0x00070FC0, K8_SOCKET_S1 },		/* G2 */
    147  1.1.2.2    yamt 		  { 0, 0 } } }
    148      1.1  cegger };
    149      1.1  cegger 
    150      1.1  cegger 
    151      1.1  cegger struct amdtemp_softc {
    152      1.1  cegger         pci_chipset_tag_t sc_pc;
    153      1.1  cegger         pcitag_t sc_pcitag;
    154      1.1  cegger 
    155      1.1  cegger 	struct sysmon_envsys *sc_sme;
    156      1.1  cegger 	envsys_data_t *sc_sensor;
    157      1.1  cegger 
    158      1.1  cegger         char sc_rev;
    159      1.1  cegger         int8_t sc_numsensors;
    160      1.1  cegger 	uint32_t sc_family;
    161  1.1.2.2    yamt 	int32_t sc_adjustment;
    162      1.1  cegger };
    163      1.1  cegger 
    164      1.1  cegger 
    165      1.1  cegger static int amdtemp_match(device_t, cfdata_t, void *);
    166      1.1  cegger static void amdtemp_attach(device_t, device_t, void *);
    167      1.1  cegger 
    168      1.1  cegger static void amdtemp_k8_init(struct amdtemp_softc *, pcireg_t);
    169      1.1  cegger static void amdtemp_k8_setup_sensors(struct amdtemp_softc *, int);
    170      1.1  cegger static void amdtemp_k8_refresh(struct sysmon_envsys *, envsys_data_t *);
    171      1.1  cegger 
    172      1.1  cegger static void amdtemp_family10_init(struct amdtemp_softc *);
    173      1.1  cegger static void amdtemp_family10_setup_sensors(struct amdtemp_softc *, int);
    174      1.1  cegger static void amdtemp_family10_refresh(struct sysmon_envsys *, envsys_data_t *);
    175      1.1  cegger 
    176      1.1  cegger CFATTACH_DECL_NEW(amdtemp, sizeof(struct amdtemp_softc),
    177      1.1  cegger 	amdtemp_match, amdtemp_attach, NULL, NULL);
    178      1.1  cegger 
    179      1.1  cegger static int
    180      1.1  cegger amdtemp_match(device_t parent, cfdata_t match, void *aux)
    181      1.1  cegger {
    182      1.1  cegger 	struct pci_attach_args *pa = aux;
    183      1.1  cegger 	pcireg_t cpu_signature;
    184      1.1  cegger 	uint32_t family;
    185      1.1  cegger 
    186      1.1  cegger 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
    187      1.1  cegger 		return 0;
    188      1.1  cegger 
    189      1.1  cegger 	switch (PCI_PRODUCT(pa->pa_id)) {
    190      1.1  cegger 	case PCI_PRODUCT_AMD_AMD64_MISC:
    191      1.1  cegger 	case PCI_PRODUCT_AMD_AMD64_F10_MISC:
    192      1.1  cegger 	case PCI_PRODUCT_AMD_AMD64_F11_MISC:
    193      1.1  cegger 		break;
    194      1.1  cegger 	default:
    195      1.1  cegger 		return 0;
    196      1.1  cegger 	}
    197      1.1  cegger 
    198      1.1  cegger 	cpu_signature = pci_conf_read(pa->pa_pc, pa->pa_tag,
    199      1.1  cegger 				CPUID_FAMILY_MODEL_R);
    200      1.1  cegger 
    201      1.1  cegger 	/* This CPUID northbridge register has been introduced
    202      1.1  cegger 	 * in Revision F */
    203      1.1  cegger 	if (cpu_signature == 0x0)
    204      1.1  cegger 		return 0;
    205      1.1  cegger 
    206      1.1  cegger 	family = CPUID2FAMILY(cpu_signature);
    207      1.1  cegger 	if (family == 0xf)
    208      1.1  cegger 		family += CPUID2EXTFAMILY(cpu_signature);
    209      1.1  cegger 
    210      1.1  cegger 	/* Not yet supported CPUs */
    211      1.1  cegger 	if (family >= 0x12)
    212      1.1  cegger 		return 0;
    213      1.1  cegger 
    214      1.1  cegger 	return 2;	/* supercede pchb(4) */
    215      1.1  cegger }
    216      1.1  cegger 
    217      1.1  cegger static void
    218      1.1  cegger amdtemp_attach(device_t parent, device_t self, void *aux)
    219      1.1  cegger {
    220      1.1  cegger 	struct amdtemp_softc *sc = device_private(self);
    221      1.1  cegger 	struct pci_attach_args *pa = aux;
    222      1.1  cegger 	pcireg_t cpu_signature;
    223      1.1  cegger 	size_t len;
    224      1.1  cegger 	int error;
    225      1.1  cegger 	uint8_t i;
    226      1.1  cegger 
    227      1.1  cegger 	aprint_naive("\n");
    228  1.1.2.2    yamt 	aprint_normal(": AMD CPU Temperature Sensors");
    229      1.1  cegger 
    230      1.1  cegger 	cpu_signature = pci_conf_read(pa->pa_pc, pa->pa_tag,
    231      1.1  cegger 				CPUID_FAMILY_MODEL_R);
    232      1.1  cegger 
    233      1.1  cegger 	/* If we hit this, then match routine is wrong. */
    234      1.1  cegger 	KASSERT(cpu_signature != 0x0);
    235      1.1  cegger 
    236      1.1  cegger 	sc->sc_family = CPUID2FAMILY(cpu_signature)
    237      1.1  cegger 		+ CPUID2EXTFAMILY(cpu_signature);
    238      1.1  cegger 	KASSERT(sc->sc_family >= 0xf);
    239      1.1  cegger 
    240      1.1  cegger 	sc->sc_pc = pa->pa_pc;
    241      1.1  cegger 	sc->sc_pcitag = pa->pa_tag;
    242  1.1.2.2    yamt 	sc->sc_adjustment = 0;
    243      1.1  cegger 
    244      1.1  cegger 	switch (sc->sc_family) {
    245      1.1  cegger 	case 0xf:  /* AMD K8 NPT */
    246      1.1  cegger 		amdtemp_k8_init(sc, cpu_signature);
    247      1.1  cegger 		break;
    248      1.1  cegger 
    249      1.1  cegger 	case 0x10: /* AMD Barcelona/Phenom */
    250      1.1  cegger 	case 0x11: /* AMD Griffin */
    251      1.1  cegger 		amdtemp_family10_init(sc);
    252      1.1  cegger 		break;
    253      1.1  cegger 
    254      1.1  cegger 	default:
    255  1.1.2.2    yamt 		aprint_normal(", family 0x%x not supported\n",
    256  1.1.2.2    yamt 			     sc->sc_family);
    257      1.1  cegger 		return;
    258      1.1  cegger 	}
    259      1.1  cegger 
    260      1.1  cegger 	aprint_normal("\n");
    261      1.1  cegger 
    262  1.1.2.2    yamt 	if (sc->sc_adjustment != 0)
    263  1.1.2.2    yamt 		aprint_debug_dev(self, "Workaround enabled\n");
    264  1.1.2.2    yamt 
    265      1.1  cegger 	sc->sc_sme = sysmon_envsys_create();
    266      1.1  cegger 	len = sizeof(envsys_data_t) * sc->sc_numsensors;
    267      1.1  cegger 	sc->sc_sensor = kmem_zalloc(len, KM_NOSLEEP);
    268      1.1  cegger 	if (!sc->sc_sensor)
    269      1.1  cegger 		goto bad2;
    270      1.1  cegger 
    271      1.1  cegger 	switch (sc->sc_family) {
    272      1.1  cegger 	case 0xf:
    273      1.1  cegger 		amdtemp_k8_setup_sensors(sc, device_unit(self));
    274      1.1  cegger 		break;
    275      1.1  cegger 	case 0x10:
    276      1.1  cegger 	case 0x11:
    277      1.1  cegger 		amdtemp_family10_setup_sensors(sc, device_unit(self));
    278      1.1  cegger 		break;
    279      1.1  cegger 	}
    280      1.1  cegger 
    281      1.1  cegger 	/*
    282      1.1  cegger 	 * Set properties in sensors.
    283      1.1  cegger 	 */
    284      1.1  cegger 	for (i = 0; i < sc->sc_numsensors; i++) {
    285      1.1  cegger 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    286      1.1  cegger 						&sc->sc_sensor[i]))
    287      1.1  cegger 			goto bad;
    288      1.1  cegger 	}
    289      1.1  cegger 
    290      1.1  cegger 	/*
    291      1.1  cegger 	 * Register the sysmon_envsys device.
    292      1.1  cegger 	 */
    293      1.1  cegger 	sc->sc_sme->sme_name = device_xname(self);
    294      1.1  cegger 	sc->sc_sme->sme_cookie = sc;
    295      1.1  cegger 
    296      1.1  cegger 	switch (sc->sc_family) {
    297      1.1  cegger 	case 0xf:
    298      1.1  cegger 		sc->sc_sme->sme_refresh = amdtemp_k8_refresh;
    299      1.1  cegger 		break;
    300      1.1  cegger 	case 0x10:
    301      1.1  cegger 	case 0x11:
    302      1.1  cegger 		sc->sc_sme->sme_refresh = amdtemp_family10_refresh;
    303      1.1  cegger 		break;
    304      1.1  cegger 	}
    305      1.1  cegger 
    306      1.1  cegger 	error = sysmon_envsys_register(sc->sc_sme);
    307      1.1  cegger 	if (error) {
    308      1.1  cegger 		aprint_error_dev(self, "unable to register with sysmon "
    309      1.1  cegger 			"(error=%d)\n", error);
    310      1.1  cegger 		goto bad;
    311      1.1  cegger 	}
    312      1.1  cegger 
    313      1.1  cegger 	if (!pmf_device_register(self, NULL, NULL))
    314      1.1  cegger 		aprint_error_dev(self, "couldn't establish power handler\n");
    315      1.1  cegger 
    316      1.1  cegger 	return;
    317      1.1  cegger 
    318      1.1  cegger bad:
    319      1.1  cegger 	kmem_free(sc->sc_sensor, len);
    320      1.1  cegger bad2:
    321      1.1  cegger 	sysmon_envsys_destroy(sc->sc_sme);
    322      1.1  cegger }
    323      1.1  cegger 
    324      1.1  cegger static void
    325      1.1  cegger amdtemp_k8_init(struct amdtemp_softc *sc, pcireg_t cpu_signature)
    326      1.1  cegger {
    327      1.1  cegger 	pcireg_t data;
    328      1.1  cegger 	uint32_t cmpcap;
    329      1.1  cegger 	uint8_t i, j;
    330      1.1  cegger 
    331      1.1  cegger 	aprint_normal(" (K8");
    332      1.1  cegger 
    333      1.1  cegger 	for (i = 0; i < __arraycount(amdtemp_core) && sc->sc_rev == '\0'; i++) {
    334  1.1.2.2    yamt 		for (j = 0; amdtemp_core[i].cpu[j].cpuid != 0; j++) {
    335      1.1  cegger 			if ((cpu_signature & ~0xf)
    336  1.1.2.2    yamt 			    != amdtemp_core[i].cpu[j].cpuid)
    337  1.1.2.2    yamt 				continue;
    338  1.1.2.2    yamt 
    339  1.1.2.2    yamt 			sc->sc_rev = amdtemp_core[i].rev[3];
    340  1.1.2.2    yamt 			aprint_normal(": core rev %.4s%.1x",
    341  1.1.2.2    yamt 				amdtemp_core[i].rev,
    342  1.1.2.2    yamt 				CPUID2STEPPING(cpu_signature));
    343  1.1.2.2    yamt 
    344  1.1.2.2    yamt 			switch (amdtemp_core[i].cpu[j].socket) {
    345  1.1.2.2    yamt 			case K8_SOCKET_AM2:
    346  1.1.2.2    yamt 				if (sc->sc_rev == 'G')
    347  1.1.2.2    yamt 					sc->sc_adjustment = 21000000;
    348  1.1.2.2    yamt 				aprint_normal(", socket AM2");
    349  1.1.2.2    yamt 				break;
    350  1.1.2.2    yamt 			case K8_SOCKET_S1:
    351  1.1.2.2    yamt 				aprint_normal(", socket S1");
    352  1.1.2.2    yamt 				break;
    353  1.1.2.2    yamt 			case K8_SOCKET_F:
    354  1.1.2.2    yamt 				aprint_normal(", socket F");
    355  1.1.2.2    yamt 				break;
    356      1.1  cegger 			}
    357      1.1  cegger 		}
    358      1.1  cegger 	}
    359      1.1  cegger 
    360      1.1  cegger 	if (sc->sc_rev == '\0') {
    361      1.1  cegger 		/* CPUID Family Model Register was introduced in
    362      1.1  cegger 		 * Revision F */
    363      1.1  cegger 		sc->sc_rev = 'G';       /* newer than E, assume G */
    364      1.1  cegger 		aprint_normal(": cpuid 0x%x", cpu_signature);
    365      1.1  cegger 	}
    366      1.1  cegger 
    367      1.1  cegger 	aprint_normal(")");
    368      1.1  cegger 
    369      1.1  cegger 	data = pci_conf_read(sc->sc_pc, sc->sc_pcitag, NORTHBRIDGE_CAP_R);
    370      1.1  cegger 	cmpcap = (data >> 12) & 0x3;
    371      1.1  cegger 
    372      1.1  cegger 	sc->sc_numsensors = cmpcap ? 4 : 2;
    373      1.1  cegger }
    374      1.1  cegger 
    375      1.1  cegger 
    376      1.1  cegger static void
    377      1.1  cegger amdtemp_k8_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
    378      1.1  cegger {
    379      1.1  cegger 	uint8_t i;
    380      1.1  cegger 
    381      1.1  cegger 	/* There are two sensors per CPU core. So we use the
    382      1.1  cegger 	 * device unit as socket counter to correctly enumerate
    383      1.1  cegger 	 * the CPUs on multi-socket machines.
    384      1.1  cegger 	 */
    385      1.1  cegger 	dv_unit *= (sc->sc_numsensors / 2);
    386      1.1  cegger 	for (i = 0; i < sc->sc_numsensors; i++) {
    387      1.1  cegger 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    388      1.1  cegger 		sc->sc_sensor[i].state = ENVSYS_SVALID;
    389      1.1  cegger 
    390      1.1  cegger 		snprintf(sc->sc_sensor[i].desc, sizeof(sc->sc_sensor[i].desc),
    391      1.1  cegger 			"CPU%u Sensor%u", dv_unit + (i / 2), i % 2);
    392      1.1  cegger 	}
    393      1.1  cegger }
    394      1.1  cegger 
    395      1.1  cegger 
    396      1.1  cegger static void
    397      1.1  cegger amdtemp_k8_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    398      1.1  cegger {
    399      1.1  cegger 	struct amdtemp_softc *sc = sme->sme_cookie;
    400      1.1  cegger 	pcireg_t status, match, tmp;
    401      1.1  cegger 	uint32_t value;
    402      1.1  cegger 
    403      1.1  cegger 	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
    404      1.1  cegger 
    405      1.1  cegger 	switch(edata->sensor) { /* sensor number */
    406      1.1  cegger 	case 0: /* Core 0 Sensor 0 */
    407      1.1  cegger 		K8_T_SEL_C0(status);
    408      1.1  cegger 		K8_T_SEL_S0(status);
    409      1.1  cegger 		break;
    410      1.1  cegger 	case 1: /* Core 0 Sensor 1 */
    411      1.1  cegger 		K8_T_SEL_C0(status);
    412      1.1  cegger 		K8_T_SEL_S1(status);
    413      1.1  cegger 		break;
    414      1.1  cegger 	case 2: /* Core 1 Sensor 0 */
    415      1.1  cegger 		K8_T_SEL_C1(status);
    416      1.1  cegger 		K8_T_SEL_S0(status);
    417      1.1  cegger 		break;
    418      1.1  cegger 	case 3: /* Core 1 Sensor 1 */
    419      1.1  cegger 		K8_T_SEL_C1(status);
    420      1.1  cegger 		K8_T_SEL_S1(status);
    421      1.1  cegger 		break;
    422      1.1  cegger 	}
    423      1.1  cegger 
    424      1.1  cegger 	match = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
    425      1.1  cegger 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R, status);
    426      1.1  cegger 	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
    427      1.1  cegger 	tmp = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
    428      1.1  cegger 
    429      1.1  cegger 	value = 0x3ff & (status >> 14);
    430      1.1  cegger 	if (sc->sc_rev != 'G')
    431      1.1  cegger 		value &= ~0x3;
    432      1.1  cegger 
    433      1.1  cegger 	edata->state = ENVSYS_SINVALID;
    434      1.1  cegger 	if ((tmp == match) && ((value & ~0x3) != 0)) {
    435      1.1  cegger 		edata->state = ENVSYS_SVALID;
    436  1.1.2.2    yamt 		edata->value_cur = (value * 250000 - 49000000) + 273150000
    437  1.1.2.2    yamt 			+ sc->sc_adjustment;
    438      1.1  cegger 	}
    439      1.1  cegger }
    440      1.1  cegger 
    441      1.1  cegger 
    442      1.1  cegger static void
    443      1.1  cegger amdtemp_family10_init(struct amdtemp_softc *sc)
    444      1.1  cegger {
    445  1.1.2.2    yamt 	aprint_normal(" (Family10h / Family11h)");
    446      1.1  cegger 
    447      1.1  cegger 	sc->sc_numsensors = 1;
    448      1.1  cegger }
    449      1.1  cegger 
    450      1.1  cegger static void
    451      1.1  cegger amdtemp_family10_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
    452      1.1  cegger {
    453      1.1  cegger 	/* sanity check for future enhancements */
    454      1.1  cegger 	KASSERT(sc->sc_numsensors == 1);
    455      1.1  cegger 
    456      1.1  cegger 	/* There's one sensor per memory controller (= socket)
    457      1.1  cegger 	 * so we use the device unit as socket counter
    458      1.1  cegger 	 * to correctly enumerate the CPUs
    459      1.1  cegger 	 */
    460      1.1  cegger 	sc->sc_sensor[0].units = ENVSYS_STEMP;
    461      1.1  cegger 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    462      1.1  cegger 
    463      1.1  cegger 	snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
    464      1.1  cegger 		"CPU%u Sensor0", dv_unit);
    465      1.1  cegger }
    466      1.1  cegger 
    467      1.1  cegger 
    468      1.1  cegger static void
    469      1.1  cegger amdtemp_family10_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    470      1.1  cegger {
    471      1.1  cegger 	struct amdtemp_softc *sc = sme->sme_cookie;
    472      1.1  cegger 	pcireg_t status;
    473      1.1  cegger 	uint32_t value;
    474      1.1  cegger 
    475      1.1  cegger 	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, F10_TEMPERATURE_CTL_R);
    476      1.1  cegger 
    477      1.1  cegger 	value = (status >> 21);
    478      1.1  cegger 
    479      1.1  cegger 	edata->state = ENVSYS_SVALID;
    480  1.1.2.2    yamt 	/* envsys(4) wants uK... convert from Celsius. */
    481  1.1.2.2    yamt 	edata->value_cur = (value * 125000) + 273150000;
    482      1.1  cegger }
    483