1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*-
* Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
* 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.
*/
/*
* Device driver for Intel's On Die thermal sensor via MSR.
* First introduced in Intel's Core line of processors.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/module.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/proc.h> /* for curthread */
#include <sys/sched.h>
#include <machine/specialreg.h>
#include <machine/cpufunc.h>
#include <machine/cputypes.h>
#include <machine/md_var.h>
#define TZ_ZEROC 2732
struct coretemp_softc {
device_t sc_dev;
int sc_tjmax;
struct sysctl_oid *sc_oid;
};
/*
* Device methods.
*/
static void coretemp_identify(driver_t *driver, device_t parent);
static int coretemp_probe(device_t dev);
static int coretemp_attach(device_t dev);
static int coretemp_detach(device_t dev);
static int coretemp_get_temp(device_t dev);
static int coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS);
static device_method_t coretemp_methods[] = {
/* Device interface */
DEVMETHOD(device_identify, coretemp_identify),
DEVMETHOD(device_probe, coretemp_probe),
DEVMETHOD(device_attach, coretemp_attach),
DEVMETHOD(device_detach, coretemp_detach),
{0, 0}
};
static driver_t coretemp_driver = {
"coretemp",
coretemp_methods,
sizeof(struct coretemp_softc),
};
static devclass_t coretemp_devclass;
DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
static void
coretemp_identify(driver_t *driver, device_t parent)
{
device_t child;
u_int regs[4];
/* Make sure we're not being doubly invoked. */
if (device_find_child(parent, "coretemp", -1) != NULL)
return;
/* Check that CPUID 0x06 is supported and the vendor is Intel.*/
if (cpu_high < 6 || cpu_vendor_id != CPU_VENDOR_INTEL)
return;
/*
* CPUID 0x06 returns 1 if the processor has on-die thermal
* sensors. EBX[0:3] contains the number of sensors.
*/
do_cpuid(0x06, regs);
if ((regs[0] & 0x1) != 1)
return;
/*
* We add a child for each CPU since settings must be performed
* on each CPU in the SMP case.
*/
child = device_add_child(parent, "coretemp", -1);
if (child == NULL)
device_printf(parent, "add coretemp child failed\n");
}
static int
coretemp_probe(device_t dev)
{
if (resource_disabled("coretemp", 0))
return (ENXIO);
device_set_desc(dev, "CPU On-Die Thermal Sensors");
return (BUS_PROBE_GENERIC);
}
static int
coretemp_attach(device_t dev)
{
struct coretemp_softc *sc = device_get_softc(dev);
device_t pdev;
uint64_t msr;
int cpu_model, cpu_stepping;
int ret, tjtarget;
sc->sc_dev = dev;
pdev = device_get_parent(dev);
cpu_model = CPUID_TO_MODEL(cpu_id);
cpu_stepping = cpu_id & CPUID_STEPPING;
/*
* Some CPUs, namely the PIII, don't have thermal sensors, but
* report them when the CPUID check is performed in
* coretemp_identify(). This leads to a later GPF when the sensor
* is queried via a MSR, so we stop here.
*/
if (cpu_model < 0xe)
return (ENXIO);
#if 0 /*
* XXXrpaulo: I have this CPU model and when it returns from C3
* coretemp continues to function properly.
*/
/*
* Check for errata AE18.
* "Processor Digital Thermal Sensor (DTS) Readout stops
* updating upon returning from C3/C4 state."
*
* Adapted from the Linux coretemp driver.
*/
if (cpu_model == 0xe && cpu_stepping < 0xc) {
msr = rdmsr(MSR_BIOS_SIGN);
msr = msr >> 32;
if (msr < 0x39) {
device_printf(dev, "not supported (Intel errata "
"AE18), try updating your BIOS\n");
return (ENXIO);
}
}
#endif
/*
* Use 100C as the initial value.
*/
sc->sc_tjmax = 100;
if ((cpu_model == 0xf && cpu_stepping >= 2) || cpu_model == 0xe) {
/*
* On some Core 2 CPUs, there's an undocumented MSR that
* can tell us if Tj(max) is 100 or 85.
*
* The if-clause for CPUs having the MSR_IA32_EXT_CONFIG was adapted
* from the Linux coretemp driver.
*/
msr = rdmsr(MSR_IA32_EXT_CONFIG);
if (msr & (1 << 30))
sc->sc_tjmax = 85;
} else if (cpu_model == 0x17) {
switch (cpu_stepping) {
case 0x6: /* Mobile Core 2 Duo */
sc->sc_tjmax = 104;
break;
default: /* Unknown stepping */
break;
}
} else {
/*
* Attempt to get Tj(max) from MSR IA32_TEMPERATURE_TARGET.
*
* This method is described in Intel white paper "CPU
* Monitoring With DTS/PECI". (#322683)
*/
ret = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &msr);
if (ret == 0) {
tjtarget = (msr >> 16) & 0xff;
/*
* On earlier generation of processors, the value
* obtained from IA32_TEMPERATURE_TARGET register is
* an offset that needs to be summed with a model
* specific base. It is however not clear what
* these numbers are, with the publicly available
* documents from Intel.
*
* For now, we consider [70, 100]C range, as
* described in #322683, as "reasonable" and accept
* these values whenever the MSR is available for
* read, regardless the CPU model.
*/
if (tjtarget >= 70 && tjtarget <= 100)
sc->sc_tjmax = tjtarget;
else
device_printf(dev, "Tj(target) value %d "
"does not seem right.\n", tjtarget);
} else
device_printf(dev, "Can not get Tj(target) "
"from your CPU, using 100C.\n");
}
if (bootverbose)
device_printf(dev, "Setting TjMax=%d\n", sc->sc_tjmax);
/*
* Add the "temperature" MIB to dev.cpu.N.
*/
sc->sc_oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(pdev),
SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
OID_AUTO, "temperature",
CTLTYPE_INT | CTLFLAG_RD,
dev, 0, coretemp_get_temp_sysctl, "IK",
"Current temperature");
return (0);
}
static int
coretemp_detach(device_t dev)
{
struct coretemp_softc *sc = device_get_softc(dev);
sysctl_remove_oid(sc->sc_oid, 1, 0);
return (0);
}
static int
coretemp_get_temp(device_t dev)
{
uint64_t msr;
int temp;
int cpu = device_get_unit(dev);
struct coretemp_softc *sc = device_get_softc(dev);
char stemp[16];
thread_lock(curthread);
sched_bind(curthread, cpu);
thread_unlock(curthread);
/*
* The digital temperature reading is located at bit 16
* of MSR_THERM_STATUS.
*
* There is a bit on that MSR that indicates whether the
* temperature is valid or not.
*
* The temperature is computed by subtracting the temperature
* reading by Tj(max).
*/
msr = rdmsr(MSR_THERM_STATUS);
thread_lock(curthread);
sched_unbind(curthread);
thread_unlock(curthread);
/*
* Check for Thermal Status and Thermal Status Log.
*/
if ((msr & 0x3) == 0x3)
device_printf(dev, "PROCHOT asserted\n");
/*
* Bit 31 contains "Reading valid"
*/
if (((msr >> 31) & 0x1) == 1) {
/*
* Starting on bit 16 and ending on bit 22.
*/
temp = sc->sc_tjmax - ((msr >> 16) & 0x7f);
} else
temp = -1;
/*
* Check for Critical Temperature Status and Critical
* Temperature Log.
* It doesn't really matter if the current temperature is
* invalid because the "Critical Temperature Log" bit will
* tell us if the Critical Temperature has been reached in
* past. It's not directly related to the current temperature.
*
* If we reach a critical level, allow devctl(4) to catch this
* and shutdown the system.
*/
if (((msr >> 4) & 0x3) == 0x3) {
device_printf(dev, "critical temperature detected, "
"suggest system shutdown\n");
snprintf(stemp, sizeof(stemp), "%d", temp);
devctl_notify("coretemp", "Thermal", stemp, "notify=0xcc");
}
return (temp);
}
static int
coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS)
{
device_t dev = (device_t) arg1;
int temp;
temp = coretemp_get_temp(dev) * 10 + TZ_ZEROC;
return (sysctl_handle_int(oidp, &temp, 0, req));
}
|