Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / hwmon / peci / cputemp.c
blobc7112dbf008b77ff53f0d72ddfe1f05da3c342d7
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>
14 #include "common.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 {
29 u8 bus;
30 u8 dev;
31 u8 func;
32 u8 offset;
35 struct cpu_info {
36 struct resolved_cores_reg *reg;
37 u8 min_peci_revision;
38 s32 (*thermal_margin_to_millidegree)(u16 val);
41 struct peci_temp_target {
42 s32 tcontrol;
43 s32 tthrottle;
44 s32 tjmax;
45 struct peci_sensor_state state;
48 enum peci_temp_target_type {
49 tcontrol_type,
50 tthrottle_type,
51 tjmax_type,
52 crit_hyst_type,
55 struct peci_cputemp {
56 struct peci_device *peci_dev;
57 struct device *dev;
58 const char *name;
59 const struct cpu_info *gen_info;
60 struct {
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];
65 } temp;
66 const char **coretemp_label;
67 DECLARE_BITMAP(core_mask, CORE_NUMS_MAX);
70 enum cputemp_channels {
71 channel_die,
72 channel_dts,
73 channel_tcontrol,
74 channel_tthrottle,
75 channel_tjmax,
76 channel_core,
79 static const char * const cputemp_label[BASE_CHANNEL_NUMS] = {
80 "Die",
81 "DTS",
82 "Tcontrol",
83 "Tthrottle",
84 "Tjmax",
87 static int update_temp_target(struct peci_cputemp *priv)
89 s32 tthrottle_offset, tcontrol_margin;
90 u32 pcs;
91 int ret;
93 if (!peci_sensor_need_update(&priv->temp.target.state))
94 return 0;
96 ret = peci_pcs_read(priv->peci_dev, PECI_PCS_TEMP_TARGET, 0, &pcs);
97 if (ret)
98 return ret;
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);
112 return 0;
115 static int get_temp_target(struct peci_cputemp *priv, enum peci_temp_target_type type, long *val)
117 int ret;
119 mutex_lock(&priv->temp.target.state.lock);
121 ret = update_temp_target(priv);
122 if (ret)
123 goto unlock;
125 switch (type) {
126 case tcontrol_type:
127 *val = priv->temp.target.tcontrol;
128 break;
129 case tthrottle_type:
130 *val = priv->temp.target.tthrottle;
131 break;
132 case tjmax_type:
133 *val = priv->temp.target.tjmax;
134 break;
135 case crit_hyst_type:
136 *val = priv->temp.target.tjmax - priv->temp.target.tcontrol;
137 break;
138 default:
139 ret = -EOPNOTSUPP;
140 break;
142 unlock:
143 mutex_unlock(&priv->temp.target.state.lock);
145 return ret;
149 * Error codes:
150 * 0x8000: General sensor error
151 * 0x8001: Reserved
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)
180 int ret = 0;
181 long tjmax;
182 u16 temp;
184 mutex_lock(&priv->temp.die.state.lock);
185 if (!peci_sensor_need_update(&priv->temp.die.state))
186 goto skip_update;
188 ret = peci_temp_read(priv->peci_dev, &temp);
189 if (ret)
190 goto err_unlock;
192 if (!dts_valid(temp)) {
193 ret = -EIO;
194 goto err_unlock;
197 ret = get_temp_target(priv, tjmax_type, &tjmax);
198 if (ret)
199 goto err_unlock;
201 priv->temp.die.value = (s32)tjmax + dts_ten_dot_six_to_millidegree(temp);
203 peci_sensor_mark_updated(&priv->temp.die.state);
205 skip_update:
206 *val = priv->temp.die.value;
207 err_unlock:
208 mutex_unlock(&priv->temp.die.state.lock);
209 return ret;
212 static int get_dts(struct peci_cputemp *priv, long *val)
214 int ret = 0;
215 u16 thermal_margin;
216 long tcontrol;
217 u32 pcs;
219 mutex_lock(&priv->temp.dts.state.lock);
220 if (!peci_sensor_need_update(&priv->temp.dts.state))
221 goto skip_update;
223 ret = peci_pcs_read(priv->peci_dev, PECI_PCS_THERMAL_MARGIN, 0, &pcs);
224 if (ret)
225 goto err_unlock;
227 thermal_margin = FIELD_GET(DTS_MARGIN_MASK, pcs);
228 if (!dts_valid(thermal_margin)) {
229 ret = -EIO;
230 goto err_unlock;
233 ret = get_temp_target(priv, tcontrol_type, &tcontrol);
234 if (ret)
235 goto err_unlock;
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);
243 skip_update:
244 *val = priv->temp.dts.value;
245 err_unlock:
246 mutex_unlock(&priv->temp.dts.state.lock);
247 return ret;
250 static int get_core_temp(struct peci_cputemp *priv, int core_index, long *val)
252 int ret = 0;
253 u16 core_dts_margin;
254 long tjmax;
255 u32 pcs;
257 mutex_lock(&priv->temp.core[core_index].state.lock);
258 if (!peci_sensor_need_update(&priv->temp.core[core_index].state))
259 goto skip_update;
261 ret = peci_pcs_read(priv->peci_dev, PECI_PCS_MODULE_TEMP, core_index, &pcs);
262 if (ret)
263 goto err_unlock;
265 core_dts_margin = FIELD_GET(PCS_MODULE_TEMP_MASK, pcs);
266 if (!dts_valid(core_dts_margin)) {
267 ret = -EIO;
268 goto err_unlock;
271 ret = get_temp_target(priv, tjmax_type, &tjmax);
272 if (ret)
273 goto err_unlock;
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);
281 skip_update:
282 *val = priv->temp.core[core_index].value;
283 err_unlock:
284 mutex_unlock(&priv->temp.core[core_index].state.lock);
285 return ret;
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)
294 return -EOPNOTSUPP;
296 *str = channel < channel_core ?
297 cputemp_label[channel] : priv->coretemp_label[channel - channel_core];
299 return 0;
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);
307 switch (attr) {
308 case hwmon_temp_input:
309 switch (channel) {
310 case channel_die:
311 return get_die_temp(priv, val);
312 case channel_dts:
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);
318 case channel_tjmax:
319 return get_temp_target(priv, tjmax_type, val);
320 default:
321 return get_core_temp(priv, channel - channel_core, val);
323 break;
324 case hwmon_temp_max:
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);
330 default:
331 return -EOPNOTSUPP;
334 return 0;
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)
343 return 0;
345 if (channel < channel_core)
346 return 0444;
348 if (test_bit(channel - channel_core, priv->core_mask))
349 return 0444;
351 return 0;
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;
358 u64 core_mask;
359 u32 data;
360 int ret;
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);
369 if (ret)
370 return ret;
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);
376 if (ret)
377 return ret;
379 core_mask |= data;
381 break;
382 default:
383 ret = peci_pci_local_read(peci_dev, reg->bus, reg->dev,
384 reg->func, reg->offset, &data);
385 if (ret)
386 return ret;
388 core_mask = data;
390 break;
393 if (!core_mask)
394 return -EIO;
396 bitmap_from_u64(priv->core_mask, core_mask);
398 return 0;
401 static int create_temp_label(struct peci_cputemp *priv)
403 unsigned long core_max = find_last_bit(priv->core_mask, CORE_NUMS_MAX);
404 int i;
406 priv->coretemp_label = devm_kzalloc(priv->dev, (core_max + 1) * sizeof(char *), GFP_KERNEL);
407 if (!priv->coretemp_label)
408 return -ENOMEM;
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])
413 return -ENOMEM;
416 return 0;
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))
427 return;
429 if (create_temp_label(priv))
430 bitmap_zero(priv->core_mask, CORE_NUMS_MAX);
433 static void sensor_init(struct peci_cputemp *priv)
435 int i;
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,
456 /* DTS margin */
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),
468 NULL
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);
485 if (!priv)
486 return -ENOMEM;
488 priv->name = devm_kasprintf(dev, GFP_KERNEL, "peci_cputemp.cpu%d",
489 peci_dev->info.socket_id);
490 if (!priv->name)
491 return -ENOMEM;
493 priv->dev = dev;
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)
503 dev_warn(priv->dev,
504 "Unexpected PECI revision %#x, some features may be unavailable\n",
505 peci_dev->info.peci_revision);
507 check_resolved_cores(priv);
509 sensor_init(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 = {
522 .bus = 1,
523 .dev = 30,
524 .func = 3,
525 .offset = 0xb4,
528 static struct resolved_cores_reg resolved_cores_reg_icx = {
529 .bus = 14,
530 .dev = 30,
531 .func = 3,
532 .offset = 0xd0,
535 static struct resolved_cores_reg resolved_cores_reg_spr = {
536 .bus = 31,
537 .dev = 30,
538 .func = 6,
539 .offset = 0x80,
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");