git: 6c101ed7a3f7 - main - amdtemp: Remove CTLFLAG_NEEDGIANT from sysctls.

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Sun, 26 Dec 2021 02:08:01 UTC
The branch main has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=6c101ed7a3f7cb47d6ac5c9e5fd33580ac8fa699

commit 6c101ed7a3f7cb47d6ac5c9e5fd33580ac8fa699
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-12-26 02:03:54 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2021-12-26 02:03:54 +0000

    amdtemp: Remove CTLFLAG_NEEDGIANT from sysctls.
    
    It seems to be needed only to serialize very old K8 registers access.
    Introduce separate lock for that and remove Giant dependency.
    
    MFC after:      2 weeks
---
 sys/dev/amdtemp/amdtemp.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/sys/dev/amdtemp/amdtemp.c b/sys/dev/amdtemp/amdtemp.c
index 4606f03ae63c..c8b40b0d68d4 100644
--- a/sys/dev/amdtemp/amdtemp.c
+++ b/sys/dev/amdtemp/amdtemp.c
@@ -41,7 +41,9 @@ __FBSDID("$FreeBSD$");
 #include <sys/bus.h>
 #include <sys/conf.h>
 #include <sys/kernel.h>
+#include <sys/lock.h>
 #include <sys/module.h>
+#include <sys/mutex.h>
 #include <sys/sysctl.h>
 #include <sys/systm.h>
 
@@ -86,6 +88,7 @@ struct amdtemp_softc {
 	struct sysctl_oid *sc_sysctl_cpu[MAXCPU];
 	struct intr_config_hook sc_ich;
 	device_t	sc_smn;
+	struct mtx	sc_lock;
 };
 
 /*
@@ -479,6 +482,7 @@ amdtemp_attach(device_t dev)
 	if (sc->sc_ncores > MAXCPU)
 		return (ENXIO);
 
+	mtx_init(&sc->sc_lock, "amdtemp", NULL, MTX_DEF);
 	if (erratum319)
 		device_printf(dev,
 		    "Erratum 319: temperature measurement may be inaccurate\n");
@@ -506,7 +510,7 @@ amdtemp_attach(device_t dev)
 	SYSCTL_ADD_PROC(sysctlctx,
 	    SYSCTL_CHILDREN(sysctlnode),
 	    OID_AUTO, "sensor0",
-	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    dev, CORE0_SENSOR0, amdtemp_sysctl, "IK",
 	    "Core 0 / Sensor 0 temperature");
 
@@ -518,7 +522,7 @@ amdtemp_attach(device_t dev)
 		SYSCTL_ADD_PROC(sysctlctx,
 		    SYSCTL_CHILDREN(sysctlnode),
 		    OID_AUTO, "sensor1",
-		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
 		    dev, CORE0_SENSOR1, amdtemp_sysctl, "IK",
 		    "Core 0 / Sensor 1 temperature");
 
@@ -531,14 +535,14 @@ amdtemp_attach(device_t dev)
 			SYSCTL_ADD_PROC(sysctlctx,
 			    SYSCTL_CHILDREN(sysctlnode),
 			    OID_AUTO, "sensor0",
-			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
 			    dev, CORE1_SENSOR0, amdtemp_sysctl, "IK",
 			    "Core 1 / Sensor 0 temperature");
 
 			SYSCTL_ADD_PROC(sysctlctx,
 			    SYSCTL_CHILDREN(sysctlnode),
 			    OID_AUTO, "sensor1",
-			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
 			    dev, CORE1_SENSOR1, amdtemp_sysctl, "IK",
 			    "Core 1 / Sensor 1 temperature");
 		}
@@ -591,7 +595,7 @@ amdtemp_intrhook(void *arg)
 			sc->sc_sysctl_cpu[i] = SYSCTL_ADD_PROC(sysctlctx,
 			    SYSCTL_CHILDREN(device_get_sysctl_tree(cpu)),
 			    OID_AUTO, "temperature",
-			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
 			    dev, sensor, amdtemp_sysctl, "IK",
 			    "Current temparature");
 		}
@@ -612,6 +616,7 @@ amdtemp_detach(device_t dev)
 
 	/* NewBus removes the dev.amdtemp.N tree by itself. */
 
+	mtx_destroy(&sc->sc_lock);
 	return (0);
 }
 
@@ -652,6 +657,8 @@ amdtemp_gettemp0f(device_t dev, amdsensor_t sensor)
 	struct amdtemp_softc *sc = device_get_softc(dev);
 	uint32_t mask, offset, temp;
 
+	mtx_lock(&sc->sc_lock);
+
 	/* Set Sensor/Core selector. */
 	temp = pci_read_config(dev, AMDTEMP_THERMTP_STAT, 1);
 	temp &= ~(AMDTEMP_TTSR_SELCORE | AMDTEMP_TTSR_SELSENSOR);
@@ -683,6 +690,7 @@ amdtemp_gettemp0f(device_t dev, amdsensor_t sensor)
 	temp = ((temp >> 14) & mask) * 5 / 2;
 	temp += AMDTEMP_ZERO_C_TO_K + (sc->sc_offset - offset) * 10;
 
+	mtx_unlock(&sc->sc_lock);
 	return (temp);
 }