2 * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 * Device driver for Intel's On Die thermal sensor via MSR.
29 * First introduced in Intel's Core line of processors.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/module.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/proc.h> /* for curthread */
44 #include <sys/sched.h>
46 #include <machine/specialreg.h>
47 #include <machine/cpufunc.h>
48 #include <machine/cputypes.h>
49 #include <machine/md_var.h>
53 struct coretemp_softc {
56 struct sysctl_oid *sc_oid;
62 static void coretemp_identify(driver_t *driver, device_t parent);
63 static int coretemp_probe(device_t dev);
64 static int coretemp_attach(device_t dev);
65 static int coretemp_detach(device_t dev);
67 static int coretemp_get_temp(device_t dev);
68 static int coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS);
70 static device_method_t coretemp_methods[] = {
71 /* Device interface */
72 DEVMETHOD(device_identify, coretemp_identify),
73 DEVMETHOD(device_probe, coretemp_probe),
74 DEVMETHOD(device_attach, coretemp_attach),
75 DEVMETHOD(device_detach, coretemp_detach),
80 static driver_t coretemp_driver = {
83 sizeof(struct coretemp_softc),
86 static devclass_t coretemp_devclass;
87 DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
90 coretemp_identify(driver_t *driver, device_t parent)
95 /* Make sure we're not being doubly invoked. */
96 if (device_find_child(parent, "coretemp", -1) != NULL)
99 /* Check that CPUID 0x06 is supported and the vendor is Intel.*/
100 if (cpu_high < 6 || cpu_vendor_id != CPU_VENDOR_INTEL)
103 * CPUID 0x06 returns 1 if the processor has on-die thermal
104 * sensors. EBX[0:3] contains the number of sensors.
106 do_cpuid(0x06, regs);
107 if ((regs[0] & 0x1) != 1)
111 * We add a child for each CPU since settings must be performed
112 * on each CPU in the SMP case.
114 child = device_add_child(parent, "coretemp", -1);
116 device_printf(parent, "add coretemp child failed\n");
120 coretemp_probe(device_t dev)
122 if (resource_disabled("coretemp", 0))
125 device_set_desc(dev, "CPU On-Die Thermal Sensors");
127 return (BUS_PROBE_GENERIC);
131 coretemp_attach(device_t dev)
133 struct coretemp_softc *sc = device_get_softc(dev);
140 pdev = device_get_parent(dev);
141 cpu_model = (cpu_id >> 4) & 15;
143 cpu_model += ((cpu_id >> 16) & 0xf) << 4;
144 cpu_mask = cpu_id & 15;
147 * Some CPUs, namely the PIII, don't have thermal sensors, but
148 * report them when the CPUID check is performed in
149 * coretemp_identify(). This leads to a later GPF when the sensor
150 * is queried via a MSR, so we stop here.
156 * XXXrpaulo: I have this CPU model and when it returns from C3
157 * coretemp continues to function properly.
161 * Check for errata AE18.
162 * "Processor Digital Thermal Sensor (DTS) Readout stops
163 * updating upon returning from C3/C4 state."
165 * Adapted from the Linux coretemp driver.
167 if (cpu_model == 0xe && cpu_mask < 0xc) {
168 msr = rdmsr(MSR_BIOS_SIGN);
171 device_printf(dev, "not supported (Intel errata "
172 "AE18), try updating your BIOS\n");
178 * On some Core 2 CPUs, there's an undocumented MSR that
179 * can tell us if Tj(max) is 100 or 85.
181 * The if-clause for CPUs having the MSR_IA32_EXT_CONFIG was adapted
182 * from the Linux coretemp driver.
185 if ((cpu_model == 0xf && cpu_mask >= 2) || cpu_model == 0xe) {
186 msr = rdmsr(MSR_IA32_EXT_CONFIG);
192 * Add the "temperature" MIB to dev.cpu.N.
194 sc->sc_oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(pdev),
195 SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
196 OID_AUTO, "temperature",
197 CTLTYPE_INT | CTLFLAG_RD,
198 dev, 0, coretemp_get_temp_sysctl, "IK",
199 "Current temperature");
205 coretemp_detach(device_t dev)
207 struct coretemp_softc *sc = device_get_softc(dev);
209 sysctl_remove_oid(sc->sc_oid, 1, 0);
216 coretemp_get_temp(device_t dev)
220 int cpu = device_get_unit(dev);
221 struct coretemp_softc *sc = device_get_softc(dev);
224 thread_lock(curthread);
225 sched_bind(curthread, cpu);
226 thread_unlock(curthread);
229 * The digital temperature reading is located at bit 16
230 * of MSR_THERM_STATUS.
232 * There is a bit on that MSR that indicates whether the
233 * temperature is valid or not.
235 * The temperature is computed by subtracting the temperature
236 * reading by Tj(max).
238 msr = rdmsr(MSR_THERM_STATUS);
240 thread_lock(curthread);
241 sched_unbind(curthread);
242 thread_unlock(curthread);
245 * Check for Thermal Status and Thermal Status Log.
247 if ((msr & 0x3) == 0x3)
248 device_printf(dev, "PROCHOT asserted\n");
251 * Bit 31 contains "Reading valid"
253 if (((msr >> 31) & 0x1) == 1) {
255 * Starting on bit 16 and ending on bit 22.
257 temp = sc->sc_tjmax - ((msr >> 16) & 0x7f);
262 * Check for Critical Temperature Status and Critical
264 * It doesn't really matter if the current temperature is
265 * invalid because the "Critical Temperature Log" bit will
266 * tell us if the Critical Temperature has been reached in
267 * past. It's not directly related to the current temperature.
269 * If we reach a critical level, allow devctl(4) to catch this
270 * and shutdown the system.
272 if (((msr >> 4) & 0x3) == 0x3) {
273 device_printf(dev, "critical temperature detected, "
274 "suggest system shutdown\n");
275 snprintf(stemp, sizeof(stemp), "%d", temp);
276 devctl_notify("coretemp", "Thermal", stemp, "notify=0xcc");
283 coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS)
285 device_t dev = (device_t) arg1;
288 temp = coretemp_get_temp(dev) * 10 + TZ_ZEROC;
290 return (sysctl_handle_int(oidp, &temp, 0, req));