Module Name:    src
Committed By:   christos
Date:           Thu Jan 25 01:22:21 UTC 2018

Modified Files:
        src/sys/arch/x86/pci: files.pci
Added Files:
        src/sys/arch/x86/pci: amdsmn.c amdsmn.h amdzentemp.c

Log Message:
Add amdzentemp from FreeBSD via Ian Clark


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/x86/pci/amdsmn.c \
    src/sys/arch/x86/pci/amdsmn.h src/sys/arch/x86/pci/amdzentemp.c
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/x86/pci/files.pci

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/pci/files.pci
diff -u src/sys/arch/x86/pci/files.pci:1.21 src/sys/arch/x86/pci/files.pci:1.22
--- src/sys/arch/x86/pci/files.pci:1.21	Sun Dec 10 12:12:54 2017
+++ src/sys/arch/x86/pci/files.pci	Wed Jan 24 20:22:21 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.pci,v 1.21 2017/12/10 17:12:54 bouyer Exp $
+#	$NetBSD: files.pci,v 1.22 2018/01/25 01:22:21 christos Exp $
 
 device 	aapic
 attach 	aapic at pci
@@ -34,6 +34,16 @@ file    arch/x86/pci/amdpcib_hpet.c		amd
 device	amdnb_misc: amdnb_miscbus
 attach	amdnb_misc at pci
 file	arch/x86/pci/amdnb_misc.c	amdnb_misc
+ 
+# AMD Family 17h system management network
+device	amdsmn {}
+attach	amdsmn at pci
+file arch/x86/pci/amdsmn.c		amdsmn
+
+# AMD Family 17h CPU temp sensor
+device	amdzentemp: sysmon_envsys
+attach	amdzentemp at amdsmn
+file	arch/x86/pci/amdzentemp.c		amdzentemp
 
 device	amdtemp: sysmon_envsys
 attach	amdtemp at amdnb_miscbus

Added files:

Index: src/sys/arch/x86/pci/amdsmn.c
diff -u /dev/null src/sys/arch/x86/pci/amdsmn.c:1.1
--- /dev/null	Wed Jan 24 20:22:21 2018
+++ src/sys/arch/x86/pci/amdsmn.c	Wed Jan 24 20:22:21 2018
@@ -0,0 +1,138 @@
+/*	$NetBSD: amdsmn.c,v 1.1 2018/01/25 01:22:21 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2017 Conrad Meyer <[email protected]>
+ * All rights reserved.
+ *
+ * NetBSD port by Ian Clark <[email protected]>
+ *
+ * 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.1 2018/01/25 01:22:21 christos Exp $ ");
+
+/*
+ * Driver for the AMD Family 17h CPU System Management Network.
+ */
+
+#include <sys/param.h>
+#include <sys/device.h>
+#include <sys/errno.h>
+#include <sys/mutex.h>
+#include <sys/systm.h>
+#include <sys/cpu.h>
+
+#include <machine/specialreg.h>
+
+#include <dev/pci/pcireg.h>
+#include <dev/pci/pcivar.h>
+#include <dev/pci/pcidevs.h>
+
+#include "amdsmn.h"
+
+#define	SMN_ADDR_REG	0x60
+#define	SMN_DATA_REG	0x64
+#define	AMD_17H_MANAGEMENT_NETWORK_PCI_ID	0x14501022
+
+struct amdsmn_softc {
+	kmutex_t smn_lock;
+	struct pci_attach_args pa;
+	pci_chipset_tag_t pc;
+	pcitag_t pcitag;
+};
+
+static int amdsmn_match(device_t, cfdata_t, void *);
+static void amdsmn_attach(device_t, device_t, void *);
+static int amdsmn_detach(device_t, int);
+static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
+
+CFATTACH_DECL_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
+    amdsmn_attach, amdsmn_detach, NULL);
+    
+static int
+amdsmn_match(device_t parent, cfdata_t match, void *aux) 
+{
+	struct pci_attach_args *pa = aux;
+	
+	return pa->pa_id == AMD_17H_MANAGEMENT_NETWORK_PCI_ID ? 2 : 0;
+}
+
+static int 
+amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux) 
+{
+	if (config_match(parent, cf, aux))
+		config_attach_loc(parent, cf, locs, aux, NULL);
+
+	return 0;
+}
+
+static void 
+amdsmn_attach(device_t parent, device_t self, void *aux) 
+{
+	struct amdsmn_softc *sc = device_private(self);
+	struct pci_attach_args *pa = aux;
+
+	mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
+	sc->pa = *pa;
+	sc->pc = pa->pa_pc;
+	sc->pcitag = pa->pa_tag;
+	aprint_normal(": AMD Family 17h System Management Network\n");
+	config_search_loc(amdsmn_misc_search, self, "amdsmn", NULL, &sc->pa);
+}
+
+static int
+amdsmn_detach(device_t self, int flags) 
+{
+	struct amdsmn_softc *sc = device_private(self);
+
+	mutex_destroy(&sc->smn_lock);
+	aprint_normal_dev(self,"detach!\n");
+
+	return 0;
+}
+
+int
+amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
+{
+	struct amdsmn_softc *sc = device_private(dev);
+
+	mutex_enter(&sc->smn_lock);
+	pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
+	*value = pci_conf_read(sc->pc, sc->pcitag, SMN_DATA_REG);
+	mutex_exit(&sc->smn_lock);
+
+	return 0;
+}
+
+int
+amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
+{
+	struct amdsmn_softc *sc = device_private(dev);
+
+	mutex_enter(&sc->smn_lock);
+	pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
+	pci_conf_write(sc->pc, sc->pcitag, SMN_DATA_REG, value);
+	mutex_exit(&sc->smn_lock);
+
+	return 0;
+}
Index: src/sys/arch/x86/pci/amdsmn.h
diff -u /dev/null src/sys/arch/x86/pci/amdsmn.h:1.1
--- /dev/null	Wed Jan 24 20:22:21 2018
+++ src/sys/arch/x86/pci/amdsmn.h	Wed Jan 24 20:22:21 2018
@@ -0,0 +1,32 @@
+/*	$NetBSD: amdsmn.h,v 1.1 2018/01/25 01:22:21 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2017 Conrad Meyer <[email protected]>
+ * 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.
+ *
+ * $FreeBSD: head/sys/dev/amdsmn/amdsmn.h 323184 2017-09-05 15:13:41Z cem $
+ */
+
+int amdsmn_read(device_t, uint32_t, uint32_t *);
+int amdsmn_write(device_t, uint32_t, uint32_t);
Index: src/sys/arch/x86/pci/amdzentemp.c
diff -u /dev/null src/sys/arch/x86/pci/amdzentemp.c:1.1
--- /dev/null	Wed Jan 24 20:22:21 2018
+++ src/sys/arch/x86/pci/amdzentemp.c	Wed Jan 24 20:22:21 2018
@@ -0,0 +1,254 @@
+/*      $NetBSD: amdzentemp.c,v 1.1 2018/01/25 01:22:21 christos Exp $ */
+/*      $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */
+
+/*
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christoph Egger.
+ *
+ * NetBSD port by Ian Clark <[email protected]>
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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.
+ */
+
+/*
+ * Copyright (c) 2008 Constantine A. Murenin <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.1 2018/01/25 01:22:21 christos 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>
+
+#include "amdsmn.h"
+
+/* Address to query for temp on family 17h */
+#define AMD_17H_CUR_TMP    0x59800
+
+struct amdzentemp_softc {
+        pci_chipset_tag_t sc_pc;
+        pcitag_t sc_pcitag;
+	struct sysmon_envsys *sc_sme;
+	device_t sc_smn;
+	envsys_data_t *sc_sensor;
+	size_t sc_sensor_len;
+        size_t sc_numsensors;
+};
+
+
+static int  amdzentemp_match(device_t, cfdata_t, void *);
+static void amdzentemp_attach(device_t, device_t, void *);
+static int  amdzentemp_detach(device_t, int);
+
+static void amdzentemp_family17_init(struct amdzentemp_softc *);
+static void amdzentemp_family17_setup_sensors(struct amdzentemp_softc *, int);
+static void amdzentemp_family17_refresh(struct sysmon_envsys *, envsys_data_t *);
+
+CFATTACH_DECL_NEW(amdzentemp, sizeof(struct amdzentemp_softc),
+    amdzentemp_match, amdzentemp_attach, amdzentemp_detach, NULL);
+
+static int
+amdzentemp_match(device_t parent, cfdata_t match, void *aux)
+{
+	struct pci_attach_args *pa = aux;
+
+	KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);
+     
+	cfdata_t parent_cfdata = device_cfdata(parent);
+     
+	/* Got AMD family 17h system management network */
+	return parent_cfdata->cf_name &&
+	    memcmp(parent_cfdata->cf_name, "amdsmn", 6) == 0;
+}
+
+static void
+amdzentemp_attach(device_t parent, device_t self, void *aux)
+{
+	struct amdzentemp_softc *sc = device_private(self);
+	struct pci_attach_args *pa = aux;
+	int error;
+	size_t i;
+
+	aprint_naive("\n");
+	aprint_normal(": AMD CPU Temperature Sensors (Family17h)");
+
+	sc->sc_pc = pa->pa_pc;
+	sc->sc_pcitag = pa->pa_tag;
+	sc->sc_smn = parent;
+     
+	amdzentemp_family17_init(sc);
+
+	aprint_normal("\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);
+
+	amdzentemp_family17_setup_sensors(sc, device_unit(self));
+
+	/*
+	 * Set properties in sensors.
+	 */
+	for (i = 0; i < sc->sc_numsensors; i++) {
+		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
+			goto bad;
+	}
+
+	/*
+	 * Register the sysmon_envsys device.
+	 */
+	sc->sc_sme->sme_name = device_xname(self);
+	sc->sc_sme->sme_cookie = sc;
+
+	sc->sc_sme->sme_refresh = amdzentemp_family17_refresh;
+
+	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
+amdzentemp_detach(device_t self, int flags)
+{
+	struct amdzentemp_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
+amdzentemp_family17_init(struct amdzentemp_softc *sc) 
+{
+	sc->sc_numsensors = 1;
+}
+
+static void
+amdzentemp_family17_setup_sensors(struct amdzentemp_softc *sc, int dv_unit) 
+{
+	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
+amdzentemp_family17_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 
+{
+	struct amdzentemp_softc *sc = sme->sme_cookie;
+	uint32_t temp;
+	int error;
+	
+	error = amdsmn_read(sc->sc_smn, AMD_17H_CUR_TMP, &temp);  
+	if (error) {
+		edata->state = ENVSYS_SINVALID;
+		return;
+	}
+	edata->state = ENVSYS_SVALID;
+	/* From C to uK. */      
+	edata->value_cur = ((temp >> 21) * 125000) + 273150000;
+}
+
+MODULE(MODULE_CLASS_DRIVER, amdzentemp, "sysmon_envsys");
+
+#ifdef _MODULE
+#include "ioconf.c"
+#endif
+
+static int
+amdzentemp_modcmd(modcmd_t cmd, void *aux)
+{
+	int error = 0;
+
+	switch (cmd) {
+	case MODULE_CMD_INIT:
+#ifdef _MODULE
+		error = config_init_component(cfdriver_ioconf_amdzentemp,
+		    cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
+#endif
+		return error;
+	case MODULE_CMD_FINI:
+#ifdef _MODULE
+		error = config_fini_component(cfdriver_ioconf_amdzentemp,
+		    cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
+#endif
+		return error;
+	default:
+		return ENOTTY;
+	}
+}
+

Reply via email to