xref: /dflybsd-src/sys/dev/powermng/coretemp/coretemp.c (revision d6f1548688ce6fc623d8ec7fca44865ddde2a5d7)
1 /*
2  * Copyright (c) 2007 Rui Paulo <rpaulo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/coretemp/coretemp.c,v 1.2 2007/08/23 10:53:03 des Exp $
27  */
28 
29 /*
30  * Device driver for Intel's On Die thermal sensor via MSR.
31  * First introduced in Intel's Core line of processors.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/module.h>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41 #include <sys/sensors.h>
42 #include <sys/spinlock.h>
43 #include <sys/spinlock2.h>
44 #include <sys/proc.h>	/* for curthread */
45 #include <sys/sched.h>
46 
47 #include <machine/specialreg.h>
48 #include <machine/cpufunc.h>
49 #include <machine/md_var.h>
50 
51 static struct spinlock		coretemp_lock;
52 
53 struct coretemp_softc {
54 	struct ksensordev	sc_sensordev;
55 	struct ksensor		sc_sensor;
56 	device_t		sc_dev;
57 	int			sc_tjmax;
58 };
59 
60 /*
61  * Device methods.
62  */
63 static void	coretemp_identify(driver_t *driver, device_t parent);
64 static int	coretemp_probe(device_t dev);
65 static int	coretemp_attach(device_t dev);
66 static int	coretemp_detach(device_t dev);
67 
68 static int	coretemp_get_temp(device_t dev);
69 static void	coretemp_refresh(void *arg);
70 
71 static device_method_t coretemp_methods[] = {
72 	/* Device interface */
73 	DEVMETHOD(device_identify,	coretemp_identify),
74 	DEVMETHOD(device_probe,		coretemp_probe),
75 	DEVMETHOD(device_attach,	coretemp_attach),
76 	DEVMETHOD(device_detach,	coretemp_detach),
77 
78 	{0, 0}
79 };
80 
81 static driver_t coretemp_driver = {
82 	"coretemp",
83 	coretemp_methods,
84 	sizeof(struct coretemp_softc),
85 };
86 
87 static devclass_t coretemp_devclass;
88 DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
89 
90 static void
91 coretemp_identify(driver_t *driver, device_t parent)
92 {
93 	device_t child;
94 	u_int regs[4];
95 
96 	/* Make sure we're not being doubly invoked. */
97 	if (device_find_child(parent, "coretemp", -1) != NULL)
98 		return;
99 
100 	/* Check that CPUID is supported and the vendor is Intel.*/
101 	if (cpu_high == 0 || strcmp(cpu_vendor, "GenuineIntel"))
102 		return;
103 	/*
104 	 * CPUID 0x06 returns 1 if the processor has on-die thermal
105 	 * sensors. EBX[0:3] contains the number of sensors.
106 	 */
107 	do_cpuid(0x06, regs);
108 	if ((regs[0] & 0x1) != 1)
109 		return;
110 
111 	/*
112 	 * We add a child for each CPU since settings must be performed
113 	 * on each CPU in the SMP case.
114 	 */
115 	child = device_add_child(parent, "coretemp", -1);
116 	if (child == NULL)
117 		device_printf(parent, "add coretemp child failed\n");
118 }
119 
120 static int
121 coretemp_probe(device_t dev)
122 {
123 	if (resource_disabled("coretemp", 0))
124 		return (ENXIO);
125 
126 	device_set_desc(dev, "CPU On-Die Thermal Sensors");
127 
128 	return (BUS_PROBE_GENERIC);
129 }
130 
131 static int
132 coretemp_attach(device_t dev)
133 {
134 	struct coretemp_softc *sc = device_get_softc(dev);
135 	device_t pdev;
136 	uint64_t msr;
137 	int cpu_model;
138 	int cpu_mask;
139 
140 	sc->sc_dev = dev;
141 	pdev = device_get_parent(dev);
142 	cpu_model = (cpu_id >> 4) & 15;
143 	/* extended model */
144 	cpu_model += ((cpu_id >> 16) & 0xf) << 4;
145 	cpu_mask = cpu_id & 15;
146 
147 	/*
148 	 * Check for errata AE18.
149 	 * "Processor Digital Thermal Sensor (DTS) Readout stops
150 	 *  updating upon returning from C3/C4 state."
151 	 *
152 	 * Adapted from the Linux coretemp driver.
153 	 */
154 	if (cpu_model == 0xe && cpu_mask < 0xc) {
155 		msr = rdmsr(MSR_BIOS_SIGN);
156 		msr = msr >> 32;
157 		if (msr < 0x39) {
158 			device_printf(dev, "not supported (Intel errata "
159 			    "AE18), try updating your BIOS\n");
160 			return (ENXIO);
161 		}
162 	}
163 	/*
164 	 * On some Core 2 CPUs, there's an undocumented MSR that
165 	 * can tell us if Tj(max) is 100 or 85.
166 	 *
167 	 * The if-clause for CPUs having the MSR_IA32_EXT_CONFIG was adapted
168 	 * from the Linux coretemp driver.
169 	 */
170 	sc->sc_tjmax = 100;
171 	if ((cpu_model == 0xf && cpu_mask >= 2) || cpu_model == 0xe) {
172 		msr = rdmsr(MSR_IA32_EXT_CONFIG);
173 		if (msr & (1 << 30))
174 			sc->sc_tjmax = 85;
175 	}
176 
177 	/*
178 	 * Add hw.sensors.cpuN.temp0 MIB.
179 	 */
180 	strlcpy(sc->sc_sensordev.xname, device_get_nameunit(pdev),
181 	    sizeof(sc->sc_sensordev.xname));
182 	sc->sc_sensor.type = SENSOR_TEMP;
183 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
184 	if (sensor_task_register(sc, coretemp_refresh, 2)) {
185 		device_printf(dev, "unable to register update task\n");
186 		return (ENXIO);
187 	}
188 	sensordev_install(&sc->sc_sensordev);
189 	spin_init(&coretemp_lock);
190 
191 	return (0);
192 }
193 
194 static int
195 coretemp_detach(device_t dev)
196 {
197 	struct coretemp_softc *sc = device_get_softc(dev);
198 
199 	spin_lock_wr(&coretemp_lock);
200 	sensordev_deinstall(&sc->sc_sensordev);
201 	sensor_task_unregister(sc);
202 	spin_unlock_wr(&coretemp_lock);
203 	spin_uninit(&coretemp_lock);
204 
205 	return (0);
206 }
207 
208 
209 static int
210 coretemp_get_temp(device_t dev)
211 {
212 	uint64_t msr;
213 	int temp, cpu, origcpu;
214 	struct coretemp_softc *sc = device_get_softc(dev);
215 	char stemp[16];
216 
217 	cpu = device_get_unit(device_get_parent(dev));
218 
219 	/*
220 	 * Bind to specific CPU to read the correct temperature.
221 	 * If not all CPUs are initialised, then only read from
222 	 * cpu0, returning -1 on all other CPUs.
223 	 */
224 	if (ncpus > 1) {
225 		origcpu = mycpuid;
226 		lwkt_migratecpu(cpu);
227 
228 		spin_lock_rd(&coretemp_lock);
229 		msr = rdmsr(MSR_THERM_STATUS);
230 		spin_unlock_rd(&coretemp_lock);
231 
232 		lwkt_migratecpu(origcpu);
233 	} else if (cpu != 0)
234 		return (-1);
235 	else
236 		msr = rdmsr(MSR_THERM_STATUS);
237 
238 	/*
239 	 * Check for Thermal Status and Thermal Status Log.
240 	 */
241 	if ((msr & 0x3) == 0x3)
242 		device_printf(dev, "PROCHOT asserted\n");
243 
244 	/*
245 	 * Bit 31 contains "Reading valid"
246 	 */
247 	if (((msr >> 31) & 0x1) == 1) {
248 		/*
249 		 * Starting on bit 16 and ending on bit 22.
250 		 */
251 		temp = sc->sc_tjmax - ((msr >> 16) & 0x7f);
252 	} else
253 		temp = -1;
254 
255 	/*
256 	 * Check for Critical Temperature Status and Critical
257 	 * Temperature Log.
258 	 * It doesn't really matter if the current temperature is
259 	 * invalid because the "Critical Temperature Log" bit will
260 	 * tell us if the Critical Temperature has been reached in
261 	 * past. It's not directly related to the current temperature.
262 	 *
263 	 * If we reach a critical level, allow devctl(4) to catch this
264 	 * and shutdown the system.
265 	 */
266 	if (((msr >> 4) & 0x3) == 0x3) {
267 		device_printf(dev, "critical temperature detected, "
268 		    "suggest system shutdown\n");
269 		ksnprintf(stemp, sizeof(stemp), "%d", temp);
270 		devctl_notify("coretemp", "Thermal", stemp, "notify=0xcc");
271 	}
272 
273 	return (temp);
274 }
275 
276 static void
277 coretemp_refresh(void *arg)
278 {
279 	struct coretemp_softc *sc = arg;
280 	device_t dev = sc->sc_dev;
281 	struct ksensor *s = &sc->sc_sensor;
282 	int temp;
283 
284 	temp = coretemp_get_temp(dev);
285 
286 	if (temp == -1) {
287 		s->flags |= SENSOR_FINVALID;
288 		s->value = 0;
289 	} else {
290 		s->flags &= ~SENSOR_FINVALID;
291 		s->value = temp * 1000000 + 273150000;
292 	}
293 }
294