1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2018-2021 Intel Corporation
4 #include <linux/auxiliary_bus.h>
5 #include <linux/bitfield.h>
6 #include <linux/bitops.h>
7 #include <linux/hwmon.h>
8 #include <linux/jiffies.h>
9 #include <linux/module.h>
10 #include <linux/peci.h>
11 #include <linux/peci-cpu.h>
12 #include <linux/units.h>
16 #define CORE_NUMS_MAX 64
18 #define BASE_CHANNEL_NUMS 5
19 #define CPUTEMP_CHANNEL_NUMS (BASE_CHANNEL_NUMS + CORE_NUMS_MAX)
21 #define TEMP_TARGET_FAN_TEMP_MASK GENMASK(15, 8)
22 #define TEMP_TARGET_REF_TEMP_MASK GENMASK(23, 16)
23 #define TEMP_TARGET_TJ_OFFSET_MASK GENMASK(29, 24)
25 #define DTS_MARGIN_MASK GENMASK(15, 0)
26 #define PCS_MODULE_TEMP_MASK GENMASK(15, 0)
28 struct resolved_cores_reg
{
36 struct resolved_cores_reg
*reg
;
38 s32 (*thermal_margin_to_millidegree
)(u16 val
);
41 struct peci_temp_target
{
45 struct peci_sensor_state state
;
48 enum peci_temp_target_type
{
56 struct peci_device
*peci_dev
;
59 const struct cpu_info
*gen_info
;
61 struct peci_temp_target target
;
62 struct peci_sensor_data die
;
63 struct peci_sensor_data dts
;
64 struct peci_sensor_data core
[CORE_NUMS_MAX
];
66 const char **coretemp_label
;
67 DECLARE_BITMAP(core_mask
, CORE_NUMS_MAX
);
70 enum cputemp_channels
{
79 static const char * const cputemp_label
[BASE_CHANNEL_NUMS
] = {
87 static int update_temp_target(struct peci_cputemp
*priv
)
89 s32 tthrottle_offset
, tcontrol_margin
;
93 if (!peci_sensor_need_update(&priv
->temp
.target
.state
))
96 ret
= peci_pcs_read(priv
->peci_dev
, PECI_PCS_TEMP_TARGET
, 0, &pcs
);
100 priv
->temp
.target
.tjmax
=
101 FIELD_GET(TEMP_TARGET_REF_TEMP_MASK
, pcs
) * MILLIDEGREE_PER_DEGREE
;
103 tcontrol_margin
= FIELD_GET(TEMP_TARGET_FAN_TEMP_MASK
, pcs
);
104 tcontrol_margin
= sign_extend32(tcontrol_margin
, 7) * MILLIDEGREE_PER_DEGREE
;
105 priv
->temp
.target
.tcontrol
= priv
->temp
.target
.tjmax
- tcontrol_margin
;
107 tthrottle_offset
= FIELD_GET(TEMP_TARGET_TJ_OFFSET_MASK
, pcs
) * MILLIDEGREE_PER_DEGREE
;
108 priv
->temp
.target
.tthrottle
= priv
->temp
.target
.tjmax
- tthrottle_offset
;
110 peci_sensor_mark_updated(&priv
->temp
.target
.state
);
115 static int get_temp_target(struct peci_cputemp
*priv
, enum peci_temp_target_type type
, long *val
)
119 mutex_lock(&priv
->temp
.target
.state
.lock
);
121 ret
= update_temp_target(priv
);
127 *val
= priv
->temp
.target
.tcontrol
;
130 *val
= priv
->temp
.target
.tthrottle
;
133 *val
= priv
->temp
.target
.tjmax
;
136 *val
= priv
->temp
.target
.tjmax
- priv
->temp
.target
.tcontrol
;
143 mutex_unlock(&priv
->temp
.target
.state
.lock
);
150 * 0x8000: General sensor error
152 * 0x8002: Underflow on reading value
153 * 0x8003-0x81ff: Reserved
155 static bool dts_valid(u16 val
)
157 return val
< 0x8000 || val
> 0x81ff;
161 * Processors return a value of DTS reading in S10.6 fixed point format
162 * (16 bits: 10-bit signed magnitude, 6-bit fraction).
164 static s32
dts_ten_dot_six_to_millidegree(u16 val
)
166 return sign_extend32(val
, 15) * MILLIDEGREE_PER_DEGREE
/ 64;
170 * For older processors, thermal margin reading is returned in S8.8 fixed
171 * point format (16 bits: 8-bit signed magnitude, 8-bit fraction).
173 static s32
dts_eight_dot_eight_to_millidegree(u16 val
)
175 return sign_extend32(val
, 15) * MILLIDEGREE_PER_DEGREE
/ 256;
178 static int get_die_temp(struct peci_cputemp
*priv
, long *val
)
184 mutex_lock(&priv
->temp
.die
.state
.lock
);
185 if (!peci_sensor_need_update(&priv
->temp
.die
.state
))
188 ret
= peci_temp_read(priv
->peci_dev
, &temp
);
192 if (!dts_valid(temp
)) {
197 ret
= get_temp_target(priv
, tjmax_type
, &tjmax
);
201 priv
->temp
.die
.value
= (s32
)tjmax
+ dts_ten_dot_six_to_millidegree(temp
);
203 peci_sensor_mark_updated(&priv
->temp
.die
.state
);
206 *val
= priv
->temp
.die
.value
;
208 mutex_unlock(&priv
->temp
.die
.state
.lock
);
212 static int get_dts(struct peci_cputemp
*priv
, long *val
)
219 mutex_lock(&priv
->temp
.dts
.state
.lock
);
220 if (!peci_sensor_need_update(&priv
->temp
.dts
.state
))
223 ret
= peci_pcs_read(priv
->peci_dev
, PECI_PCS_THERMAL_MARGIN
, 0, &pcs
);
227 thermal_margin
= FIELD_GET(DTS_MARGIN_MASK
, pcs
);
228 if (!dts_valid(thermal_margin
)) {
233 ret
= get_temp_target(priv
, tcontrol_type
, &tcontrol
);
237 /* Note that the tcontrol should be available before calling it */
238 priv
->temp
.dts
.value
=
239 (s32
)tcontrol
- priv
->gen_info
->thermal_margin_to_millidegree(thermal_margin
);
241 peci_sensor_mark_updated(&priv
->temp
.dts
.state
);
244 *val
= priv
->temp
.dts
.value
;
246 mutex_unlock(&priv
->temp
.dts
.state
.lock
);
250 static int get_core_temp(struct peci_cputemp
*priv
, int core_index
, long *val
)
257 mutex_lock(&priv
->temp
.core
[core_index
].state
.lock
);
258 if (!peci_sensor_need_update(&priv
->temp
.core
[core_index
].state
))
261 ret
= peci_pcs_read(priv
->peci_dev
, PECI_PCS_MODULE_TEMP
, core_index
, &pcs
);
265 core_dts_margin
= FIELD_GET(PCS_MODULE_TEMP_MASK
, pcs
);
266 if (!dts_valid(core_dts_margin
)) {
271 ret
= get_temp_target(priv
, tjmax_type
, &tjmax
);
275 /* Note that the tjmax should be available before calling it */
276 priv
->temp
.core
[core_index
].value
=
277 (s32
)tjmax
+ dts_ten_dot_six_to_millidegree(core_dts_margin
);
279 peci_sensor_mark_updated(&priv
->temp
.core
[core_index
].state
);
282 *val
= priv
->temp
.core
[core_index
].value
;
284 mutex_unlock(&priv
->temp
.core
[core_index
].state
.lock
);
288 static int cputemp_read_string(struct device
*dev
, enum hwmon_sensor_types type
,
289 u32 attr
, int channel
, const char **str
)
291 struct peci_cputemp
*priv
= dev_get_drvdata(dev
);
293 if (attr
!= hwmon_temp_label
)
296 *str
= channel
< channel_core
?
297 cputemp_label
[channel
] : priv
->coretemp_label
[channel
- channel_core
];
302 static int cputemp_read(struct device
*dev
, enum hwmon_sensor_types type
,
303 u32 attr
, int channel
, long *val
)
305 struct peci_cputemp
*priv
= dev_get_drvdata(dev
);
308 case hwmon_temp_input
:
311 return get_die_temp(priv
, val
);
313 return get_dts(priv
, val
);
314 case channel_tcontrol
:
315 return get_temp_target(priv
, tcontrol_type
, val
);
316 case channel_tthrottle
:
317 return get_temp_target(priv
, tthrottle_type
, val
);
319 return get_temp_target(priv
, tjmax_type
, val
);
321 return get_core_temp(priv
, channel
- channel_core
, val
);
325 return get_temp_target(priv
, tcontrol_type
, val
);
326 case hwmon_temp_crit
:
327 return get_temp_target(priv
, tjmax_type
, val
);
328 case hwmon_temp_crit_hyst
:
329 return get_temp_target(priv
, crit_hyst_type
, val
);
337 static umode_t
cputemp_is_visible(const void *data
, enum hwmon_sensor_types type
,
338 u32 attr
, int channel
)
340 const struct peci_cputemp
*priv
= data
;
342 if (channel
> CPUTEMP_CHANNEL_NUMS
)
345 if (channel
< channel_core
)
348 if (test_bit(channel
- channel_core
, priv
->core_mask
))
354 static int init_core_mask(struct peci_cputemp
*priv
)
356 struct peci_device
*peci_dev
= priv
->peci_dev
;
357 struct resolved_cores_reg
*reg
= priv
->gen_info
->reg
;
362 /* Get the RESOLVED_CORES register value */
363 switch (peci_dev
->info
.x86_vfm
) {
364 case INTEL_ICELAKE_X
:
365 case INTEL_ICELAKE_D
:
366 case INTEL_SAPPHIRERAPIDS_X
:
367 ret
= peci_ep_pci_local_read(peci_dev
, 0, reg
->bus
, reg
->dev
,
368 reg
->func
, reg
->offset
+ 4, &data
);
372 core_mask
= (u64
)data
<< 32;
374 ret
= peci_ep_pci_local_read(peci_dev
, 0, reg
->bus
, reg
->dev
,
375 reg
->func
, reg
->offset
, &data
);
383 ret
= peci_pci_local_read(peci_dev
, reg
->bus
, reg
->dev
,
384 reg
->func
, reg
->offset
, &data
);
396 bitmap_from_u64(priv
->core_mask
, core_mask
);
401 static int create_temp_label(struct peci_cputemp
*priv
)
403 unsigned long core_max
= find_last_bit(priv
->core_mask
, CORE_NUMS_MAX
);
406 priv
->coretemp_label
= devm_kzalloc(priv
->dev
, (core_max
+ 1) * sizeof(char *), GFP_KERNEL
);
407 if (!priv
->coretemp_label
)
410 for_each_set_bit(i
, priv
->core_mask
, CORE_NUMS_MAX
) {
411 priv
->coretemp_label
[i
] = devm_kasprintf(priv
->dev
, GFP_KERNEL
, "Core %d", i
);
412 if (!priv
->coretemp_label
[i
])
419 static void check_resolved_cores(struct peci_cputemp
*priv
)
422 * Failure to resolve cores is non-critical, we're still able to
423 * provide other sensor data.
426 if (init_core_mask(priv
))
429 if (create_temp_label(priv
))
430 bitmap_zero(priv
->core_mask
, CORE_NUMS_MAX
);
433 static void sensor_init(struct peci_cputemp
*priv
)
437 mutex_init(&priv
->temp
.target
.state
.lock
);
438 mutex_init(&priv
->temp
.die
.state
.lock
);
439 mutex_init(&priv
->temp
.dts
.state
.lock
);
441 for_each_set_bit(i
, priv
->core_mask
, CORE_NUMS_MAX
)
442 mutex_init(&priv
->temp
.core
[i
].state
.lock
);
445 static const struct hwmon_ops peci_cputemp_ops
= {
446 .is_visible
= cputemp_is_visible
,
447 .read_string
= cputemp_read_string
,
448 .read
= cputemp_read
,
451 static const struct hwmon_channel_info
* const peci_cputemp_info
[] = {
452 HWMON_CHANNEL_INFO(temp
,
453 /* Die temperature */
454 HWMON_T_LABEL
| HWMON_T_INPUT
| HWMON_T_MAX
|
455 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
,
457 HWMON_T_LABEL
| HWMON_T_INPUT
| HWMON_T_MAX
|
458 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
,
459 /* Tcontrol temperature */
460 HWMON_T_LABEL
| HWMON_T_INPUT
| HWMON_T_CRIT
,
461 /* Tthrottle temperature */
462 HWMON_T_LABEL
| HWMON_T_INPUT
,
463 /* Tjmax temperature */
464 HWMON_T_LABEL
| HWMON_T_INPUT
,
465 /* Core temperature - for all core channels */
466 [channel_core
... CPUTEMP_CHANNEL_NUMS
- 1] =
467 HWMON_T_LABEL
| HWMON_T_INPUT
),
471 static const struct hwmon_chip_info peci_cputemp_chip_info
= {
472 .ops
= &peci_cputemp_ops
,
473 .info
= peci_cputemp_info
,
476 static int peci_cputemp_probe(struct auxiliary_device
*adev
,
477 const struct auxiliary_device_id
*id
)
479 struct device
*dev
= &adev
->dev
;
480 struct peci_device
*peci_dev
= to_peci_device(dev
->parent
);
481 struct peci_cputemp
*priv
;
482 struct device
*hwmon_dev
;
484 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
488 priv
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "peci_cputemp.cpu%d",
489 peci_dev
->info
.socket_id
);
494 priv
->peci_dev
= peci_dev
;
495 priv
->gen_info
= (const struct cpu_info
*)id
->driver_data
;
498 * This is just a sanity check. Since we're using commands that are
499 * guaranteed to be supported on a given platform, we should never see
500 * revision lower than expected.
502 if (peci_dev
->info
.peci_revision
< priv
->gen_info
->min_peci_revision
)
504 "Unexpected PECI revision %#x, some features may be unavailable\n",
505 peci_dev
->info
.peci_revision
);
507 check_resolved_cores(priv
);
511 hwmon_dev
= devm_hwmon_device_register_with_info(priv
->dev
, priv
->name
,
512 priv
, &peci_cputemp_chip_info
, NULL
);
514 return PTR_ERR_OR_ZERO(hwmon_dev
);
518 * RESOLVED_CORES PCI configuration register may have different location on
519 * different platforms.
521 static struct resolved_cores_reg resolved_cores_reg_hsx
= {
528 static struct resolved_cores_reg resolved_cores_reg_icx
= {
535 static struct resolved_cores_reg resolved_cores_reg_spr
= {
542 static const struct cpu_info cpu_hsx
= {
543 .reg
= &resolved_cores_reg_hsx
,
544 .min_peci_revision
= 0x33,
545 .thermal_margin_to_millidegree
= &dts_eight_dot_eight_to_millidegree
,
548 static const struct cpu_info cpu_skx
= {
549 .reg
= &resolved_cores_reg_hsx
,
550 .min_peci_revision
= 0x33,
551 .thermal_margin_to_millidegree
= &dts_ten_dot_six_to_millidegree
,
554 static const struct cpu_info cpu_icx
= {
555 .reg
= &resolved_cores_reg_icx
,
556 .min_peci_revision
= 0x40,
557 .thermal_margin_to_millidegree
= &dts_ten_dot_six_to_millidegree
,
560 static const struct cpu_info cpu_spr
= {
561 .reg
= &resolved_cores_reg_spr
,
562 .min_peci_revision
= 0x40,
563 .thermal_margin_to_millidegree
= &dts_ten_dot_six_to_millidegree
,
566 static const struct auxiliary_device_id peci_cputemp_ids
[] = {
568 .name
= "peci_cpu.cputemp.hsx",
569 .driver_data
= (kernel_ulong_t
)&cpu_hsx
,
572 .name
= "peci_cpu.cputemp.bdx",
573 .driver_data
= (kernel_ulong_t
)&cpu_hsx
,
576 .name
= "peci_cpu.cputemp.bdxd",
577 .driver_data
= (kernel_ulong_t
)&cpu_hsx
,
580 .name
= "peci_cpu.cputemp.skx",
581 .driver_data
= (kernel_ulong_t
)&cpu_skx
,
584 .name
= "peci_cpu.cputemp.icx",
585 .driver_data
= (kernel_ulong_t
)&cpu_icx
,
588 .name
= "peci_cpu.cputemp.icxd",
589 .driver_data
= (kernel_ulong_t
)&cpu_icx
,
592 .name
= "peci_cpu.cputemp.spr",
593 .driver_data
= (kernel_ulong_t
)&cpu_spr
,
597 MODULE_DEVICE_TABLE(auxiliary
, peci_cputemp_ids
);
599 static struct auxiliary_driver peci_cputemp_driver
= {
600 .probe
= peci_cputemp_probe
,
601 .id_table
= peci_cputemp_ids
,
604 module_auxiliary_driver(peci_cputemp_driver
);
606 MODULE_AUTHOR("Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>");
607 MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
608 MODULE_DESCRIPTION("PECI cputemp driver");
609 MODULE_LICENSE("GPL");
610 MODULE_IMPORT_NS("PECI_CPU");