]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/coretemp/coretemp.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / dev / coretemp / coretemp.c
1 /*-
2  * Copyright (c) 2007, 2008 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
27 /*
28  * Device driver for Intel's On Die thermal sensor via MSR.
29  * First introduced in Intel's Core line of processors.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/module.h>
40 #include <sys/conf.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/proc.h>   /* for curthread */
44 #include <sys/sched.h>
45
46 #include <machine/specialreg.h>
47 #include <machine/cpufunc.h>
48 #include <machine/md_var.h>
49
50 struct coretemp_softc {
51         device_t        sc_dev;
52         int             sc_tjmax;
53         struct sysctl_oid *sc_oid;
54 };
55
56 /*
57  * Device methods.
58  */
59 static void     coretemp_identify(driver_t *driver, device_t parent);
60 static int      coretemp_probe(device_t dev);
61 static int      coretemp_attach(device_t dev);
62 static int      coretemp_detach(device_t dev);
63
64 static int      coretemp_get_temp(device_t dev);
65 static int      coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS);
66
67 static device_method_t coretemp_methods[] = {
68         /* Device interface */
69         DEVMETHOD(device_identify,      coretemp_identify),
70         DEVMETHOD(device_probe,         coretemp_probe),
71         DEVMETHOD(device_attach,        coretemp_attach),
72         DEVMETHOD(device_detach,        coretemp_detach),
73
74         {0, 0}
75 };
76
77 static driver_t coretemp_driver = {
78         "coretemp",
79         coretemp_methods,
80         sizeof(struct coretemp_softc),
81 };
82
83 static devclass_t coretemp_devclass;
84 DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
85
86 static void
87 coretemp_identify(driver_t *driver, device_t parent)
88 {
89         device_t child;
90         u_int regs[4];
91
92         /* Make sure we're not being doubly invoked. */
93         if (device_find_child(parent, "coretemp", -1) != NULL)
94                 return;
95
96         /* Check that CPUID 0x06 is supported and the vendor is Intel.*/
97         if (cpu_high < 6 || strcmp(cpu_vendor, "GenuineIntel"))
98                 return;
99         /*
100          * CPUID 0x06 returns 1 if the processor has on-die thermal
101          * sensors. EBX[0:3] contains the number of sensors.
102          */
103         do_cpuid(0x06, regs);
104         if ((regs[0] & 0x1) != 1)
105                 return;
106
107         /*
108          * We add a child for each CPU since settings must be performed
109          * on each CPU in the SMP case.
110          */
111         child = device_add_child(parent, "coretemp", -1);
112         if (child == NULL)
113                 device_printf(parent, "add coretemp child failed\n");
114 }
115
116 static int
117 coretemp_probe(device_t dev)
118 {
119         if (resource_disabled("coretemp", 0))
120                 return (ENXIO);
121
122         device_set_desc(dev, "CPU On-Die Thermal Sensors");
123
124         return (BUS_PROBE_GENERIC);
125 }
126
127 static int
128 coretemp_attach(device_t dev)
129 {
130         struct coretemp_softc *sc = device_get_softc(dev);
131         device_t pdev;
132         uint64_t msr;
133         int cpu_model;
134         int cpu_mask;
135
136         sc->sc_dev = dev;
137         pdev = device_get_parent(dev);
138         cpu_model = (cpu_id >> 4) & 15;
139         /* extended model */
140         cpu_model += ((cpu_id >> 16) & 0xf) << 4;
141         cpu_mask = cpu_id & 15;
142
143         /*
144          * Some CPUs, namely the PIII, don't have thermal sensors, but
145          * report them when the CPUID check is performed in
146          * coretemp_identify(). This leads to a later GPF when the sensor
147          * is queried via a MSR, so we stop here.
148          */
149         if (cpu_model < 0xe)
150                 return (ENXIO);
151         
152 #if 0 /*
153        * XXXrpaulo: I have this CPU model and when it returns from C3
154        * coretemp continues to function properly.
155        */
156          
157         /*
158          * Check for errata AE18.
159          * "Processor Digital Thermal Sensor (DTS) Readout stops
160          *  updating upon returning from C3/C4 state."
161          *
162          * Adapted from the Linux coretemp driver.
163          */
164         if (cpu_model == 0xe && cpu_mask < 0xc) {
165                 msr = rdmsr(MSR_BIOS_SIGN);
166                 msr = msr >> 32;
167                 if (msr < 0x39) {
168                         device_printf(dev, "not supported (Intel errata "
169                             "AE18), try updating your BIOS\n");
170                         return (ENXIO);
171                 }
172         }
173 #endif
174         /*
175          * On some Core 2 CPUs, there's an undocumented MSR that
176          * can tell us if Tj(max) is 100 or 85.
177          *
178          * The if-clause for CPUs having the MSR_IA32_EXT_CONFIG was adapted
179          * from the Linux coretemp driver.
180          */
181         sc->sc_tjmax = 100;
182         if ((cpu_model == 0xf && cpu_mask >= 2) || cpu_model == 0xe) {
183                 msr = rdmsr(MSR_IA32_EXT_CONFIG);
184                 if (msr & (1 << 30))
185                         sc->sc_tjmax = 85;
186         }
187
188         /*
189          * Add the "temperature" MIB to dev.cpu.N.
190          */
191         sc->sc_oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(pdev),
192             SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
193             OID_AUTO, "temperature",
194             CTLTYPE_INT | CTLFLAG_RD,
195             dev, 0, coretemp_get_temp_sysctl, "I",
196             "Current temperature in degC");
197
198         return (0);
199 }
200
201 static int
202 coretemp_detach(device_t dev)
203 {
204         struct coretemp_softc *sc = device_get_softc(dev);
205
206         sysctl_remove_oid(sc->sc_oid, 1, 0);
207
208         return (0);
209 }
210
211
212 static int
213 coretemp_get_temp(device_t dev)
214 {
215         uint64_t msr;
216         int temp;
217         int cpu = device_get_unit(dev);
218         struct coretemp_softc *sc = device_get_softc(dev);
219         char stemp[16];
220
221         thread_lock(curthread);
222         sched_bind(curthread, cpu);
223         thread_unlock(curthread);
224
225         /*
226          * The digital temperature reading is located at bit 16
227          * of MSR_THERM_STATUS.
228          *
229          * There is a bit on that MSR that indicates whether the
230          * temperature is valid or not.
231          *
232          * The temperature is computed by subtracting the temperature
233          * reading by Tj(max).
234          */
235         msr = rdmsr(MSR_THERM_STATUS);
236
237         thread_lock(curthread);
238         sched_unbind(curthread);
239         thread_unlock(curthread);
240
241         /*
242          * Check for Thermal Status and Thermal Status Log.
243          */
244         if ((msr & 0x3) == 0x3)
245                 device_printf(dev, "PROCHOT asserted\n");
246
247         /*
248          * Bit 31 contains "Reading valid"
249          */
250         if (((msr >> 31) & 0x1) == 1) {
251                 /*
252                  * Starting on bit 16 and ending on bit 22.
253                  */
254                 temp = sc->sc_tjmax - ((msr >> 16) & 0x7f);
255         } else
256                 temp = -1;
257
258         /*
259          * Check for Critical Temperature Status and Critical
260          * Temperature Log.
261          * It doesn't really matter if the current temperature is
262          * invalid because the "Critical Temperature Log" bit will
263          * tell us if the Critical Temperature has been reached in
264          * past. It's not directly related to the current temperature.
265          *
266          * If we reach a critical level, allow devctl(4) to catch this
267          * and shutdown the system.
268          */
269         if (((msr >> 4) & 0x3) == 0x3) {
270                 device_printf(dev, "critical temperature detected, "
271                     "suggest system shutdown\n");
272                 snprintf(stemp, sizeof(stemp), "%d", temp);
273                 devctl_notify("coretemp", "Thermal", stemp, "notify=0xcc");
274         }
275
276         return (temp);
277 }
278
279 static int
280 coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS)
281 {
282         device_t dev = (device_t) arg1;
283         int temp;
284
285         temp = coretemp_get_temp(dev);
286
287         return (sysctl_handle_int(oidp, &temp, 0, req));
288 }