-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoretemp.c
463 lines (371 loc) · 8.86 KB
/
coretemp.c
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
* Copyright (c) 2015, Alexandru Pirvulescu
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/processor.h>
#include <sys/cpuvar.h>
#include <sys/kmem.h>
#include <sys/modctl.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/systm.h>
#include <sys/kstat.h>
#include <sys/auxv.h>
#include <sys/systeminfo.h>
#include <sys/cpuvar.h>
#include <sys/pghw.h>
#include <sys/ontrap.h>
#include <coretemp.h>
#include <sys/x86_archext.h>
static dev_info_t *ctemp_devi;
static kmutex_t ctemp_mutex;
struct ctemp_kstat_t {
kstat_named_t chip_id;
kstat_named_t core_id;
kstat_named_t thread_id;
kstat_named_t core_temp;
kstat_named_t chip_temp;
kstat_named_t tj_max;
kstat_named_t target_temp;
kstat_named_t valid;
} ctemp_kstat_t = {
{ "chip_id", KSTAT_DATA_INT32 },
{ "core_id", KSTAT_DATA_INT32 },
{ "thread_id", KSTAT_DATA_INT32 },
{ "core_temp", KSTAT_DATA_INT32 },
{ "chip_temp", KSTAT_DATA_INT32 },
{ "tj_max", KSTAT_DATA_INT32 },
{ "target_temp", KSTAT_DATA_INT32 },
{ "valid", KSTAT_DATA_INT32 },
};
#define KSTAT_VAL(__V__) ctemp_kstat_t.__V__.value.i32
static struct cpuid_regs current_cpuid;
typedef struct {
uint32_t eax;
uint32_t edx;
} msr_regs_t;
typedef struct {
uint32_t msr_index;
uint64_t *result;
} msr_req_t;
typedef struct {
int chip_id;
int core_id;
cpu_t *cpu;
} ctemp_core_t;
kstat_t *ctemp_kstat_entries[MAX_CPUS];
static int ctemp_fill_fields(cpu_t *);
static int ctemp_rdmsr(cpu_t *, uint32_t, msr_regs_t *);
static void ctemp_cpuid(cpu_t *, uint32_t, struct cpuid_regs *);
static void ctemp_fill_tj_max(cpu_t *cpu);
static void ctemp_fill_pkg_temp(cpu_t *cpu);
static void ctemp_fill_core_temp(cpu_t *cpu);
static void ctemp_msr_req(uintptr_t, uintptr_t);
static int
ctemp_kstat_update(kstat_t *kstat, int rw)
{
if (rw == KSTAT_WRITE) {
return (EACCES);
}
if (!is_x86_feature(x86_featureset, X86FSET_MSR)) {
return (ENXIO);
}
cpu_t *cpu_ptr = (cpu_t *)kstat->ks_private;
ctemp_fill_fields(cpu_ptr);
/* misc data */
KSTAT_VAL(core_id) = cpuid_get_coreid(cpu_ptr);
KSTAT_VAL(chip_id) = cpuid_get_chipid(cpu_ptr);
KSTAT_VAL(thread_id) = cpuid_get_clogid(cpu_ptr);
return (0);
}
static int
ctemp_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg,
void **result)
{
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
case DDI_INFO_DEVT2INSTANCE:
break;
default:
return (DDI_FAILURE);
}
if (cmd == DDI_INFO_DEVT2INSTANCE) {
*result = 0;
} else {
*result = ctemp_devi;
}
return (DDI_SUCCESS);
}
static int
ctemp_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
if (cmd != DDI_ATTACH) {
return (DDI_FAILURE);
}
if (!is_x86_feature(x86_featureset, X86FSET_CPUID)) {
dev_err(devi, CE_NOTE,
"This CPU does not support CPUID instruction");
return (DDI_FAILURE);
}
switch (x86_vendor) {
case X86_VENDOR_Intel:
break;
default:
dev_err(devi, CE_NOTE,
"This CPU vendor is not supported");
return (DDI_FAILURE);
}
ctemp_devi = devi;
int i, j;
int ncores;
ctemp_core_t cores[MAX_CPUS];
/* find physical cores */
ncores = 0;
for (i = 0; i < ncpus; i++) {
cpu_t *processor = cpu[i];
int chip_id = cpuid_get_chipid(processor);
int core_id = cpuid_get_coreid(processor);
/* see if we already visited this core */
int visited = 0;
for (j = 0; j < ncores; j++) {
if ((cores[j].chip_id == chip_id) &&
(cores[j].core_id == core_id)) {
visited = 1;
break;
}
}
if (!visited) {
cores[ncores].chip_id = chip_id;
cores[ncores].core_id = core_id;
cores[ncores].cpu = processor;
ncores++;
}
}
/* initialize a kstat instance for each core */
for (i = 0; i < ncores; i++) {
kstat_t *ksp = kstat_create(
KSTAT_CORETEMP_MODULE,
i,
KSTAT_CORETEMP_NAME,
KSTAT_CORETEMP_CLASS,
KSTAT_TYPE_NAMED,
sizeof (ctemp_kstat_t) / sizeof (kstat_named_t),
KSTAT_FLAG_VIRTUAL);
if (!ksp) {
dev_err(devi, CE_WARN, "Failed to create kstat entry");
return (DDI_FAILURE);
}
ctemp_kstat_entries[i] = ksp;
ksp->ks_data = (void *)&ctemp_kstat_t;
ksp->ks_lock = &ctemp_mutex;
ksp->ks_update = ctemp_kstat_update;
ksp->ks_private = (void *)cores[i].cpu;
kstat_install(ksp);
}
return (DDI_SUCCESS);
}
static int
ctemp_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
if (cmd != DDI_DETACH) {
return (DDI_FAILURE);
}
ctemp_devi = NULL;
int i;
for (i = 0; i < ncpus; i++) {
kstat_delete(ctemp_kstat_entries[i]);
}
return (DDI_SUCCESS);
}
static struct dev_ops ctemp_dv_ops = {
DEVO_REV,
0,
ctemp_getinfo,
nulldev, /* identify */
nulldev, /* probe */
ctemp_attach,
ctemp_detach,
nodev, /* reset */
NULL,
NULL,
NULL,
ddi_quiesce_not_needed, /* quiesce */
};
static struct modldrv modldrv = {
&mod_driverops,
"coretemp driver 0.1",
&ctemp_dv_ops
};
static struct modlinkage modl = {
MODREV_1,
&modldrv
};
int
_init(void)
{
return (mod_install(&modl));
}
int
_fini(void)
{
return (mod_remove(&modl));
}
int
_info(struct modinfo *modinfo)
{
return (mod_info(&modl, modinfo));
}
/* private stuff */
int
intel_fill_fields(cpu_t *cpu)
{
/* initialize data with common values */
ctemp_cpuid(cpu, 0x06, ¤t_cpuid);
/* initialize with dummy data */
KSTAT_VAL(tj_max) = 100;
KSTAT_VAL(chip_temp) = 0;
KSTAT_VAL(core_temp) = 0;
KSTAT_VAL(target_temp) = 0;
KSTAT_VAL(valid) = 0;
/* fill Tj_max information */
ctemp_fill_tj_max(cpu);
ctemp_fill_core_temp(cpu);
ctemp_fill_pkg_temp(cpu);
return (0);
}
int
ctemp_fill_fields(cpu_t *cpu)
{
switch (x86_vendor) {
case X86_VENDOR_Intel:
intel_fill_fields(cpu);
default:
/* vendor not implemented (yet! :) ) */
return (EINVAL);
}
return (0);
}
static void
ctemp_fill_tj_max(cpu_t *cpu)
{
int model = cpuid_getmodel(cpu);
if (model < 0x0e) {
return;
}
if ((model == 0x1c) ||
(model == 0x26) ||
(model == 0x27) ||
(model == 0x35) ||
(model == 0x36)) {
return;
}
msr_regs_t regs;
if (ctemp_rdmsr(cpu, MSR_IA32_TEMPERATURE_TARGET, ®s) != 0) {
return;
}
KSTAT_VAL(tj_max) = (regs.eax >> 16) & 0x7f;
KSTAT_VAL(target_temp) = KSTAT_VAL(tj_max) - ((regs.eax >> 8) & 0xff);
}
static void
ctemp_fill_core_temp(cpu_t *cpu)
{
if ((current_cpuid.cp_eax & 0x01) == 0) {
return;
}
msr_regs_t regs;
if (ctemp_rdmsr(cpu, MSR_IA32_THERM_STATUS, ®s) != 0) {
return;
}
if (regs.eax & 0x80000000) {
KSTAT_VAL(core_temp) =
KSTAT_VAL(tj_max) - ((regs.eax >> 16) & 0x7f);
KSTAT_VAL(valid) = 1;
}
}
static void
ctemp_fill_pkg_temp(cpu_t *cpu)
{
KSTAT_VAL(chip_temp) = KSTAT_VAL(core_temp);
if (((current_cpuid.cp_eax >> 6) & 0x01) == 0) {
return;
}
msr_regs_t regs;
if (ctemp_rdmsr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, ®s) != 0) {
return;
}
KSTAT_VAL(chip_temp) = KSTAT_VAL(tj_max) - ((regs.eax >> 16) & 0x7f);
}
/* Execute CPUID on specified CPU */
static void
ctemp_cpuid(cpu_t *cpu, uint32_t cpuid_func,
struct cpuid_regs *result)
{
result->cp_eax = cpuid_func;
result->cp_ebx = result->cp_ecx = result->cp_edx = 0;
(void) cpuid_insn(cpu, result);
}
static int
ctemp_rdmsr(cpu_t *cpu, uint32_t msr_index, msr_regs_t *result)
{
int error;
if (cpu == NULL) {
cpu = CPU;
}
msr_req_t request;
request.msr_index = msr_index;
request.result = (uint64_t *)result;
cpu_call(cpu, (cpu_call_func_t)ctemp_msr_req,
(uintptr_t)&request, (uintptr_t)&error);
return (error);
}
/* Execute RDMSR on specified CPU */
static void
ctemp_msr_req(uintptr_t req_ptr, uintptr_t error_ptr)
{
uint32_t msr_index;
uint64_t *result;
msr_index = ((msr_req_t *)req_ptr)->msr_index;
result = ((msr_req_t *)req_ptr)->result;
int error;
on_trap_data_t otd;
if (on_trap(&otd, OT_DATA_ACCESS) == 0) {
error = checked_rdmsr(msr_index, result);
} else {
dev_err(ctemp_devi, CE_WARN,
"Invalid rdmsr(0x%08" PRIx32 ")", (uint32_t)msr_index);
error = EFAULT;
}
no_trap();
*((int *)error_ptr) = error;
return;
}