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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2018-2021 Intel Corporation
#include <linux/auxiliary_bus.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/hwmon.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/peci.h>
#include <linux/peci-cpu.h>
#include <linux/units.h>
#include "common.h"
#define CORE_NUMS_MAX 64
#define BASE_CHANNEL_NUMS 5
#define CPUTEMP_CHANNEL_NUMS (BASE_CHANNEL_NUMS + CORE_NUMS_MAX)
#define TEMP_TARGET_FAN_TEMP_MASK GENMASK(15, 8)
#define TEMP_TARGET_REF_TEMP_MASK GENMASK(23, 16)
#define TEMP_TARGET_TJ_OFFSET_MASK GENMASK(29, 24)
#define DTS_MARGIN_MASK GENMASK(15, 0)
#define PCS_MODULE_TEMP_MASK GENMASK(15, 0)
struct resolved_cores_reg {
u8 bus;
u8 dev;
u8 func;
u8 offset;
};
struct cpu_info {
struct resolved_cores_reg *reg;
u8 min_peci_revision;
s32 (*thermal_margin_to_millidegree)(u16 val);
};
struct peci_temp_target {
s32 tcontrol;
s32 tthrottle;
s32 tjmax;
struct peci_sensor_state state;
};
enum peci_temp_target_type {
tcontrol_type,
tthrottle_type,
tjmax_type,
crit_hyst_type,
};
struct peci_cputemp {
struct peci_device *peci_dev;
struct device *dev;
const char *name;
const struct cpu_info *gen_info;
struct {
struct peci_temp_target target;
struct peci_sensor_data die;
struct peci_sensor_data dts;
struct peci_sensor_data core[CORE_NUMS_MAX];
} temp;
const char **coretemp_label;
DECLARE_BITMAP(core_mask, CORE_NUMS_MAX);
};
enum cputemp_channels {
channel_die,
channel_dts,
channel_tcontrol,
channel_tthrottle,
channel_tjmax,
channel_core,
};
static const char * const cputemp_label[BASE_CHANNEL_NUMS] = {
"Die",
"DTS",
"Tcontrol",
"Tthrottle",
"Tjmax",
};
static int update_temp_target(struct peci_cputemp *priv)
{
s32 tthrottle_offset, tcontrol_margin;
u32 pcs;
int ret;
if (!peci_sensor_need_update(&priv->temp.target.state))
return 0;
ret = peci_pcs_read(priv->peci_dev, PECI_PCS_TEMP_TARGET, 0, &pcs);
if (ret)
return ret;
priv->temp.target.tjmax =
FIELD_GET(TEMP_TARGET_REF_TEMP_MASK, pcs) * MILLIDEGREE_PER_DEGREE;
tcontrol_margin = FIELD_GET(TEMP_TARGET_FAN_TEMP_MASK, pcs);
tcontrol_margin = sign_extend32(tcontrol_margin, 7) * MILLIDEGREE_PER_DEGREE;
priv->temp.target.tcontrol = priv->temp.target.tjmax - tcontrol_margin;
tthrottle_offset = FIELD_GET(TEMP_TARGET_TJ_OFFSET_MASK, pcs) * MILLIDEGREE_PER_DEGREE;
priv->temp.target.tthrottle = priv->temp.target.tjmax - tthrottle_offset;
peci_sensor_mark_updated(&priv->temp.target.state);
return 0;
}
static int get_temp_target(struct peci_cputemp *priv, enum peci_temp_target_type type, long *val)
{
int ret;
mutex_lock(&priv->temp.target.state.lock);
ret = update_temp_target(priv);
if (ret)
goto unlock;
switch (type) {
case tcontrol_type:
*val = priv->temp.target.tcontrol;
break;
case tthrottle_type:
*val = priv->temp.target.tthrottle;
break;
case tjmax_type:
*val = priv->temp.target.tjmax;
break;
case crit_hyst_type:
*val = priv->temp.target.tjmax - priv->temp.target.tcontrol;
break;
default:
ret = -EOPNOTSUPP;
break;
}
unlock:
mutex_unlock(&priv->temp.target.state.lock);
return ret;
}
/*
* Error codes:
* 0x8000: General sensor error
* 0x8001: Reserved
* 0x8002: Underflow on reading value
* 0x8003-0x81ff: Reserved
*/
static bool dts_valid(u16 val)
{
return val < 0x8000 || val > 0x81ff;
}
/*
* Processors return a value of DTS reading in S10.6 fixed point format
* (16 bits: 10-bit signed magnitude, 6-bit fraction).
*/
static s32 dts_ten_dot_six_to_millidegree(u16 val)
{
return sign_extend32(val, 15) * MILLIDEGREE_PER_DEGREE / 64;
}
/*
* For older processors, thermal margin reading is returned in S8.8 fixed
* point format (16 bits: 8-bit signed magnitude, 8-bit fraction).
*/
static s32 dts_eight_dot_eight_to_millidegree(u16 val)
{
return sign_extend32(val, 15) * MILLIDEGREE_PER_DEGREE / 256;
}
static int get_die_temp(struct peci_cputemp *priv, long *val)
{
int ret = 0;
long tjmax;
u16 temp;
mutex_lock(&priv->temp.die.state.lock);
if (!peci_sensor_need_update(&priv->temp.die.state))
goto skip_update;
ret = peci_temp_read(priv->peci_dev, &temp);
if (ret)
goto err_unlock;
if (!dts_valid(temp)) {
ret = -EIO;
goto err_unlock;
}
ret = get_temp_target(priv, tjmax_type, &tjmax);
if (ret)
goto err_unlock;
priv->temp.die.value = (s32)tjmax + dts_ten_dot_six_to_millidegree(temp);
peci_sensor_mark_updated(&priv->temp.die.state);
skip_update:
*val = priv->temp.die.value;
err_unlock:
mutex_unlock(&priv->temp.die.state.lock);
return ret;
}
static int get_dts(struct peci_cputemp *priv, long *val)
{
int ret = 0;
u16 thermal_margin;
long tcontrol;
u32 pcs;
mutex_lock(&priv->temp.dts.state.lock);
if (!peci_sensor_need_update(&priv->temp.dts.state))
goto skip_update;
ret = peci_pcs_read(priv->peci_dev, PECI_PCS_THERMAL_MARGIN, 0, &pcs);
if (ret)
goto err_unlock;
thermal_margin = FIELD_GET(DTS_MARGIN_MASK, pcs);
if (!dts_valid(thermal_margin)) {
ret = -EIO;
goto err_unlock;
}
ret = get_temp_target(priv, tcontrol_type, &tcontrol);
if (ret)
goto err_unlock;
/* Note that the tcontrol should be available before calling it */
priv->temp.dts.value =
(s32)tcontrol - priv->gen_info->thermal_margin_to_millidegree(thermal_margin);
peci_sensor_mark_updated(&priv->temp.dts.state);
skip_update:
*val = priv->temp.dts.value;
err_unlock:
mutex_unlock(&priv->temp.dts.state.lock);
return ret;
}
static int get_core_temp(struct peci_cputemp *priv, int core_index, long *val)
{
int ret = 0;
u16 core_dts_margin;
long tjmax;
u32 pcs;
mutex_lock(&priv->temp.core[core_index].state.lock);
if (!peci_sensor_need_update(&priv->temp.core[core_index].state))
goto skip_update;
ret = peci_pcs_read(priv->peci_dev, PECI_PCS_MODULE_TEMP, core_index, &pcs);
if (ret)
goto err_unlock;
core_dts_margin = FIELD_GET(PCS_MODULE_TEMP_MASK, pcs);
if (!dts_valid(core_dts_margin)) {
ret = -EIO;
goto err_unlock;
}
ret = get_temp_target(priv, tjmax_type, &tjmax);
if (ret)
goto err_unlock;
/* Note that the tjmax should be available before calling it */
priv->temp.core[core_index].value =
(s32)tjmax + dts_ten_dot_six_to_millidegree(core_dts_margin);
peci_sensor_mark_updated(&priv->temp.core[core_index].state);
skip_update:
*val = priv->temp.core[core_index].value;
err_unlock:
mutex_unlock(&priv->temp.core[core_index].state.lock);
return ret;
}
static int cputemp_read_string(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, const char **str)
{
struct peci_cputemp *priv = dev_get_drvdata(dev);
if (attr != hwmon_temp_label)
return -EOPNOTSUPP;
*str = channel < channel_core ?
cputemp_label[channel] : priv->coretemp_label[channel - channel_core];
return 0;
}
static int cputemp_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
struct peci_cputemp *priv = dev_get_drvdata(dev);
switch (attr) {
case hwmon_temp_input:
switch (channel) {
case channel_die:
return get_die_temp(priv, val);
case channel_dts:
return get_dts(priv, val);
case channel_tcontrol:
return get_temp_target(priv, tcontrol_type, val);
case channel_tthrottle:
return get_temp_target(priv, tthrottle_type, val);
case channel_tjmax:
return get_temp_target(priv, tjmax_type, val);
default:
return get_core_temp(priv, channel - channel_core, val);
}
break;
case hwmon_temp_max:
return get_temp_target(priv, tcontrol_type, val);
case hwmon_temp_crit:
return get_temp_target(priv, tjmax_type, val);
case hwmon_temp_crit_hyst:
return get_temp_target(priv, crit_hyst_type, val);
default:
return -EOPNOTSUPP;
}
return 0;
}
static umode_t cputemp_is_visible(const void *data, enum hwmon_sensor_types type,
u32 attr, int channel)
{
const struct peci_cputemp *priv = data;
if (channel > CPUTEMP_CHANNEL_NUMS)
return 0;
if (channel < channel_core)
return 0444;
if (test_bit(channel - channel_core, priv->core_mask))
return 0444;
return 0;
}
static int init_core_mask(struct peci_cputemp *priv)
{
struct peci_device *peci_dev = priv->peci_dev;
struct resolved_cores_reg *reg = priv->gen_info->reg;
u64 core_mask;
u32 data;
int ret;
/* Get the RESOLVED_CORES register value */
switch (peci_dev->info.model) {
case INTEL_FAM6_ICELAKE_X:
case INTEL_FAM6_ICELAKE_D:
ret = peci_ep_pci_local_read(peci_dev, 0, reg->bus, reg->dev,
reg->func, reg->offset + 4, &data);
if (ret)
return ret;
core_mask = (u64)data << 32;
ret = peci_ep_pci_local_read(peci_dev, 0, reg->bus, reg->dev,
reg->func, reg->offset, &data);
if (ret)
return ret;
core_mask |= data;
break;
default:
ret = peci_pci_local_read(peci_dev, reg->bus, reg->dev,
reg->func, reg->offset, &data);
if (ret)
return ret;
core_mask = data;
break;
}
if (!core_mask)
return -EIO;
bitmap_from_u64(priv->core_mask, core_mask);
return 0;
}
static int create_temp_label(struct peci_cputemp *priv)
{
unsigned long core_max = find_last_bit(priv->core_mask, CORE_NUMS_MAX);
int i;
priv->coretemp_label = devm_kzalloc(priv->dev, core_max * sizeof(char *), GFP_KERNEL);
if (!priv->coretemp_label)
return -ENOMEM;
for_each_set_bit(i, priv->core_mask, CORE_NUMS_MAX) {
priv->coretemp_label[i] = devm_kasprintf(priv->dev, GFP_KERNEL, "Core %d", i);
if (!priv->coretemp_label[i])
return -ENOMEM;
}
return 0;
}
static void check_resolved_cores(struct peci_cputemp *priv)
{
/*
* Failure to resolve cores is non-critical, we're still able to
* provide other sensor data.
*/
if (init_core_mask(priv))
return;
if (create_temp_label(priv))
bitmap_zero(priv->core_mask, CORE_NUMS_MAX);
}
static void sensor_init(struct peci_cputemp *priv)
{
int i;
mutex_init(&priv->temp.target.state.lock);
mutex_init(&priv->temp.die.state.lock);
mutex_init(&priv->temp.dts.state.lock);
for_each_set_bit(i, priv->core_mask, CORE_NUMS_MAX)
mutex_init(&priv->temp.core[i].state.lock);
}
static const struct hwmon_ops peci_cputemp_ops = {
.is_visible = cputemp_is_visible,
.read_string = cputemp_read_string,
.read = cputemp_read,
};
static const u32 peci_cputemp_temp_channel_config[] = {
/* Die temperature */
HWMON_T_LABEL | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_CRIT_HYST,
/* DTS margin */
HWMON_T_LABEL | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_CRIT_HYST,
/* Tcontrol temperature */
HWMON_T_LABEL | HWMON_T_INPUT | HWMON_T_CRIT,
/* Tthrottle temperature */
HWMON_T_LABEL | HWMON_T_INPUT,
/* Tjmax temperature */
HWMON_T_LABEL | HWMON_T_INPUT,
/* Core temperature - for all core channels */
[channel_core ... CPUTEMP_CHANNEL_NUMS - 1] = HWMON_T_LABEL | HWMON_T_INPUT,
0
};
static const struct hwmon_channel_info peci_cputemp_temp_channel = {
.type = hwmon_temp,
.config = peci_cputemp_temp_channel_config,
};
static const struct hwmon_channel_info *peci_cputemp_info[] = {
&peci_cputemp_temp_channel,
NULL
};
static const struct hwmon_chip_info peci_cputemp_chip_info = {
.ops = &peci_cputemp_ops,
.info = peci_cputemp_info,
};
static int peci_cputemp_probe(struct auxiliary_device *adev,
const struct auxiliary_device_id *id)
{
struct device *dev = &adev->dev;
struct peci_device *peci_dev = to_peci_device(dev->parent);
struct peci_cputemp *priv;
struct device *hwmon_dev;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->name = devm_kasprintf(dev, GFP_KERNEL, "peci_cputemp.cpu%d",
peci_dev->info.socket_id);
if (!priv->name)
return -ENOMEM;
priv->dev = dev;
priv->peci_dev = peci_dev;
priv->gen_info = (const struct cpu_info *)id->driver_data;
/*
* This is just a sanity check. Since we're using commands that are
* guaranteed to be supported on a given platform, we should never see
* revision lower than expected.
*/
if (peci_dev->info.peci_revision < priv->gen_info->min_peci_revision)
dev_warn(priv->dev,
"Unexpected PECI revision %#x, some features may be unavailable\n",
peci_dev->info.peci_revision);
check_resolved_cores(priv);
sensor_init(priv);
hwmon_dev = devm_hwmon_device_register_with_info(priv->dev, priv->name,
priv, &peci_cputemp_chip_info, NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
/*
* RESOLVED_CORES PCI configuration register may have different location on
* different platforms.
*/
static struct resolved_cores_reg resolved_cores_reg_hsx = {
.bus = 1,
.dev = 30,
.func = 3,
.offset = 0xb4,
};
static struct resolved_cores_reg resolved_cores_reg_icx = {
.bus = 14,
.dev = 30,
.func = 3,
.offset = 0xd0,
};
static const struct cpu_info cpu_hsx = {
.reg = &resolved_cores_reg_hsx,
.min_peci_revision = 0x33,
.thermal_margin_to_millidegree = &dts_eight_dot_eight_to_millidegree,
};
static const struct cpu_info cpu_icx = {
.reg = &resolved_cores_reg_icx,
.min_peci_revision = 0x40,
.thermal_margin_to_millidegree = &dts_ten_dot_six_to_millidegree,
};
static const struct auxiliary_device_id peci_cputemp_ids[] = {
{
.name = "peci_cpu.cputemp.hsx",
.driver_data = (kernel_ulong_t)&cpu_hsx,
},
{
.name = "peci_cpu.cputemp.bdx",
.driver_data = (kernel_ulong_t)&cpu_hsx,
},
{
.name = "peci_cpu.cputemp.bdxd",
.driver_data = (kernel_ulong_t)&cpu_hsx,
},
{
.name = "peci_cpu.cputemp.skx",
.driver_data = (kernel_ulong_t)&cpu_hsx,
},
{
.name = "peci_cpu.cputemp.icx",
.driver_data = (kernel_ulong_t)&cpu_icx,
},
{
.name = "peci_cpu.cputemp.icxd",
.driver_data = (kernel_ulong_t)&cpu_icx,
},
{ }
};
MODULE_DEVICE_TABLE(auxiliary, peci_cputemp_ids);
static struct auxiliary_driver peci_cputemp_driver = {
.probe = peci_cputemp_probe,
.id_table = peci_cputemp_ids,
};
module_auxiliary_driver(peci_cputemp_driver);
MODULE_AUTHOR("Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>");
MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
MODULE_DESCRIPTION("PECI cputemp driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS(PECI_CPU);
|