1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * k10temp.c - AMD Family 10h/11h/12h/14h/15h/16h/17h
4 * processor hardware monitoring
6 * Copyright (c) 2009 Clemens Ladisch <clemens@ladisch.de>
7 * Copyright (c) 2020 Guenter Roeck <linux@roeck-us.net>
9 * Implementation notes:
10 * - CCD1 and CCD2 register address information as well as the calculation to
11 * convert raw register values is from https://github.com/ocerman/zenpower.
12 * The information is not confirmed from chip datasheets, but experiments
13 * suggest that it provides reasonable temperature values.
14 * - Register addresses to read chip voltage and current are also from
15 * https://github.com/ocerman/zenpower, and not confirmed from chip
16 * datasheets. Current calibration is board specific and not typically
17 * shared by board vendors. For this reason, current values are
18 * normalized to report 1A/LSB for core current and and 0.25A/LSB for SoC
19 * current. Reported values can be adjusted using the sensors configuration
21 * - It is unknown if the mechanism to read CCD1/CCD2 temperature as well as
22 * current and voltage information works on higher-end Ryzen CPUs.
23 * Information reported by Windows tools suggests that additional sensors
24 * (both temperature and voltage/current) are supported, but their register
25 * location is currently unknown.
28 #include <linux/bitops.h>
29 #include <linux/err.h>
30 #include <linux/hwmon.h>
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/pci_ids.h>
35 #include <asm/amd_nb.h>
36 #include <asm/processor.h>
38 MODULE_DESCRIPTION("AMD Family 10h+ CPU core temperature monitor");
39 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
40 MODULE_LICENSE("GPL");
43 module_param(force, bool, 0444);
44 MODULE_PARM_DESC(force, "force loading on processors with erratum 319");
46 /* Provide lock for writing to NB_SMU_IND_ADDR */
47 static DEFINE_MUTEX(nb_smu_ind_mutex);
49 #ifndef PCI_DEVICE_ID_AMD_15H_M70H_NB_F3
50 #define PCI_DEVICE_ID_AMD_15H_M70H_NB_F3 0x15b3
53 /* CPUID function 0x80000001, ebx */
54 #define CPUID_PKGTYPE_MASK GENMASK(31, 28)
55 #define CPUID_PKGTYPE_F 0x00000000
56 #define CPUID_PKGTYPE_AM2R2_AM3 0x10000000
58 /* DRAM controller (PCI function 2) */
59 #define REG_DCT0_CONFIG_HIGH 0x094
60 #define DDR3_MODE BIT(8)
62 /* miscellaneous (PCI function 3) */
63 #define REG_HARDWARE_THERMAL_CONTROL 0x64
64 #define HTC_ENABLE BIT(0)
66 #define REG_REPORTED_TEMPERATURE 0xa4
68 #define REG_NORTHBRIDGE_CAPABILITIES 0xe8
69 #define NB_CAP_HTC BIT(10)
72 * For F15h M60h and M70h, REG_HARDWARE_THERMAL_CONTROL
73 * and REG_REPORTED_TEMPERATURE have been moved to
74 * D0F0xBC_xD820_0C64 [Hardware Temperature Control]
75 * D0F0xBC_xD820_0CA4 [Reported Temperature Control]
77 #define F15H_M60H_HARDWARE_TEMP_CTRL_OFFSET 0xd8200c64
78 #define F15H_M60H_REPORTED_TEMP_CTRL_OFFSET 0xd8200ca4
80 /* F17h M01h Access througn SMN */
81 #define F17H_M01H_REPORTED_TEMP_CTRL_OFFSET 0x00059800
82 #define F17H_M70H_CCD1_TEMP 0x00059954
83 #define F17H_M70H_CCD2_TEMP 0x00059958
85 #define F17H_M01H_SVI 0x0005A000
86 #define F17H_M01H_SVI_TEL_PLANE0 (F17H_M01H_SVI + 0xc)
87 #define F17H_M01H_SVI_TEL_PLANE1 (F17H_M01H_SVI + 0x10)
89 #define CUR_TEMP_SHIFT 21
90 #define CUR_TEMP_RANGE_SEL_MASK BIT(19)
92 #define CFACTOR_ICORE 1000000 /* 1A / LSB */
93 #define CFACTOR_ISOC 250000 /* 0.25A / LSB */
97 void (*read_htcreg)(struct pci_dev *pdev, u32 *regval);
98 void (*read_tempreg)(struct pci_dev *pdev, u32 *regval);
100 u32 temp_adjust_mask;
115 static const struct tctl_offset tctl_offset_table[] = {
116 { 0x17, "AMD Ryzen 5 1600X", 20000 },
117 { 0x17, "AMD Ryzen 7 1700X", 20000 },
118 { 0x17, "AMD Ryzen 7 1800X", 20000 },
119 { 0x17, "AMD Ryzen 7 2700X", 10000 },
120 { 0x17, "AMD Ryzen Threadripper 19", 27000 }, /* 19{00,20,50}X */
121 { 0x17, "AMD Ryzen Threadripper 29", 27000 }, /* 29{20,50,70,90}[W]X */
124 static bool is_threadripper(void)
126 return strstr(boot_cpu_data.x86_model_id, "Threadripper");
129 static bool is_epyc(void)
131 return strstr(boot_cpu_data.x86_model_id, "EPYC");
134 static void read_htcreg_pci(struct pci_dev *pdev, u32 *regval)
136 pci_read_config_dword(pdev, REG_HARDWARE_THERMAL_CONTROL, regval);
139 static void read_tempreg_pci(struct pci_dev *pdev, u32 *regval)
141 pci_read_config_dword(pdev, REG_REPORTED_TEMPERATURE, regval);
144 static void amd_nb_index_read(struct pci_dev *pdev, unsigned int devfn,
145 unsigned int base, int offset, u32 *val)
147 mutex_lock(&nb_smu_ind_mutex);
148 pci_bus_write_config_dword(pdev->bus, devfn,
150 pci_bus_read_config_dword(pdev->bus, devfn,
152 mutex_unlock(&nb_smu_ind_mutex);
155 static void read_htcreg_nb_f15(struct pci_dev *pdev, u32 *regval)
157 amd_nb_index_read(pdev, PCI_DEVFN(0, 0), 0xb8,
158 F15H_M60H_HARDWARE_TEMP_CTRL_OFFSET, regval);
161 static void read_tempreg_nb_f15(struct pci_dev *pdev, u32 *regval)
163 amd_nb_index_read(pdev, PCI_DEVFN(0, 0), 0xb8,
164 F15H_M60H_REPORTED_TEMP_CTRL_OFFSET, regval);
167 static void read_tempreg_nb_f17(struct pci_dev *pdev, u32 *regval)
169 amd_smn_read(amd_pci_dev_to_node_id(pdev),
170 F17H_M01H_REPORTED_TEMP_CTRL_OFFSET, regval);
173 static long get_raw_temp(struct k10temp_data *data)
178 data->read_tempreg(data->pdev, ®val);
179 temp = (regval >> CUR_TEMP_SHIFT) * 125;
180 if (regval & data->temp_adjust_mask)
185 const char *k10temp_temp_label[] = {
192 const char *k10temp_in_label[] = {
197 const char *k10temp_curr_label[] = {
202 static int k10temp_read_labels(struct device *dev,
203 enum hwmon_sensor_types type,
204 u32 attr, int channel, const char **str)
208 *str = k10temp_temp_label[channel];
211 *str = k10temp_in_label[channel];
214 *str = k10temp_curr_label[channel];
222 static int k10temp_read_curr(struct device *dev, u32 attr, int channel,
225 struct k10temp_data *data = dev_get_drvdata(dev);
229 case hwmon_curr_input:
230 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
231 data->svi_addr[channel], ®val);
232 *val = DIV_ROUND_CLOSEST(data->cfactor[channel] *
242 static int k10temp_read_in(struct device *dev, u32 attr, int channel, long *val)
244 struct k10temp_data *data = dev_get_drvdata(dev);
249 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
250 data->svi_addr[channel], ®val);
251 regval = (regval >> 16) & 0xff;
252 *val = DIV_ROUND_CLOSEST(155000 - regval * 625, 100);
260 static int k10temp_read_temp(struct device *dev, u32 attr, int channel,
263 struct k10temp_data *data = dev_get_drvdata(dev);
267 case hwmon_temp_input:
270 *val = get_raw_temp(data) - data->temp_offset;
275 *val = get_raw_temp(data);
280 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
281 F17H_M70H_CCD1_TEMP, ®val);
282 *val = (regval & 0xfff) * 125 - 305000;
285 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
286 F17H_M70H_CCD2_TEMP, ®val);
287 *val = (regval & 0xfff) * 125 - 305000;
296 case hwmon_temp_crit:
297 data->read_htcreg(data->pdev, ®val);
298 *val = ((regval >> 16) & 0x7f) * 500 + 52000;
300 case hwmon_temp_crit_hyst:
301 data->read_htcreg(data->pdev, ®val);
302 *val = (((regval >> 16) & 0x7f)
303 - ((regval >> 24) & 0xf)) * 500 + 52000;
311 static int k10temp_read(struct device *dev, enum hwmon_sensor_types type,
312 u32 attr, int channel, long *val)
316 return k10temp_read_temp(dev, attr, channel, val);
318 return k10temp_read_in(dev, attr, channel, val);
320 return k10temp_read_curr(dev, attr, channel, val);
326 static umode_t k10temp_is_visible(const void *_data,
327 enum hwmon_sensor_types type,
328 u32 attr, int channel)
330 const struct k10temp_data *data = _data;
331 struct pci_dev *pdev = data->pdev;
337 case hwmon_temp_input:
339 case 0: /* Tdie, or Tctl if we don't show it */
342 if (!data->show_tdie)
346 if (!data->show_tccd1)
350 if (!data->show_tccd2)
361 case hwmon_temp_crit:
362 case hwmon_temp_crit_hyst:
363 if (channel || !data->read_htcreg)
366 pci_read_config_dword(pdev,
367 REG_NORTHBRIDGE_CAPABILITIES,
369 if (!(reg & NB_CAP_HTC))
372 data->read_htcreg(data->pdev, ®);
373 if (!(reg & HTC_ENABLE))
376 case hwmon_temp_label:
377 /* No labels if we don't show the die temperature */
378 if (!data->show_tdie)
385 if (!data->show_tccd1)
389 if (!data->show_tccd2)
402 if (!data->show_current)
411 static bool has_erratum_319(struct pci_dev *pdev)
413 u32 pkg_type, reg_dram_cfg;
415 if (boot_cpu_data.x86 != 0x10)
419 * Erratum 319: The thermal sensor of Socket F/AM2+ processors
422 pkg_type = cpuid_ebx(0x80000001) & CPUID_PKGTYPE_MASK;
423 if (pkg_type == CPUID_PKGTYPE_F)
425 if (pkg_type != CPUID_PKGTYPE_AM2R2_AM3)
428 /* DDR3 memory implies socket AM3, which is good */
429 pci_bus_read_config_dword(pdev->bus,
430 PCI_DEVFN(PCI_SLOT(pdev->devfn), 2),
431 REG_DCT0_CONFIG_HIGH, ®_dram_cfg);
432 if (reg_dram_cfg & DDR3_MODE)
436 * Unfortunately it is possible to run a socket AM3 CPU with DDR2
437 * memory. We blacklist all the cores which do exist in socket AM2+
438 * format. It still isn't perfect, as RB-C2 cores exist in both AM2+
439 * and AM3 formats, but that's the best we can do.
441 return boot_cpu_data.x86_model < 4 ||
442 (boot_cpu_data.x86_model == 4 && boot_cpu_data.x86_stepping <= 2);
445 static const struct hwmon_channel_info *k10temp_info[] = {
446 HWMON_CHANNEL_INFO(temp,
447 HWMON_T_INPUT | HWMON_T_MAX |
448 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
450 HWMON_T_INPUT | HWMON_T_LABEL,
451 HWMON_T_INPUT | HWMON_T_LABEL,
452 HWMON_T_INPUT | HWMON_T_LABEL),
453 HWMON_CHANNEL_INFO(in,
454 HWMON_I_INPUT | HWMON_I_LABEL,
455 HWMON_I_INPUT | HWMON_I_LABEL),
456 HWMON_CHANNEL_INFO(curr,
457 HWMON_C_INPUT | HWMON_C_LABEL,
458 HWMON_C_INPUT | HWMON_C_LABEL),
462 static const struct hwmon_ops k10temp_hwmon_ops = {
463 .is_visible = k10temp_is_visible,
464 .read = k10temp_read,
465 .read_string = k10temp_read_labels,
468 static const struct hwmon_chip_info k10temp_chip_info = {
469 .ops = &k10temp_hwmon_ops,
470 .info = k10temp_info,
473 static int k10temp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
475 int unreliable = has_erratum_319(pdev);
476 struct device *dev = &pdev->dev;
477 struct k10temp_data *data;
478 struct device *hwmon_dev;
484 "unreliable CPU thermal sensor; monitoring disabled\n");
488 "unreliable CPU thermal sensor; check erratum 319\n");
491 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
497 if (boot_cpu_data.x86 == 0x15 &&
498 ((boot_cpu_data.x86_model & 0xf0) == 0x60 ||
499 (boot_cpu_data.x86_model & 0xf0) == 0x70)) {
500 data->read_htcreg = read_htcreg_nb_f15;
501 data->read_tempreg = read_tempreg_nb_f15;
502 } else if (boot_cpu_data.x86 == 0x17 || boot_cpu_data.x86 == 0x18) {
505 data->temp_adjust_mask = CUR_TEMP_RANGE_SEL_MASK;
506 data->read_tempreg = read_tempreg_nb_f17;
507 data->show_tdie = true;
509 switch (boot_cpu_data.x86_model) {
512 case 0x11: /* Zen APU */
513 case 0x18: /* Zen+ APU */
514 data->show_current = !is_threadripper() && !is_epyc();
515 data->svi_addr[0] = F17H_M01H_SVI_TEL_PLANE0;
516 data->svi_addr[1] = F17H_M01H_SVI_TEL_PLANE1;
517 data->cfactor[0] = CFACTOR_ICORE;
518 data->cfactor[1] = CFACTOR_ISOC;
520 case 0x31: /* Zen2 Threadripper */
521 case 0x71: /* Zen2 */
522 data->show_current = !is_threadripper() && !is_epyc();
523 data->cfactor[0] = CFACTOR_ICORE;
524 data->cfactor[1] = CFACTOR_ISOC;
525 data->svi_addr[0] = F17H_M01H_SVI_TEL_PLANE1;
526 data->svi_addr[1] = F17H_M01H_SVI_TEL_PLANE0;
527 amd_smn_read(amd_pci_dev_to_node_id(pdev),
528 F17H_M70H_CCD1_TEMP, ®val);
530 data->show_tccd1 = true;
532 amd_smn_read(amd_pci_dev_to_node_id(pdev),
533 F17H_M70H_CCD2_TEMP, ®val);
535 data->show_tccd2 = true;
539 data->read_htcreg = read_htcreg_pci;
540 data->read_tempreg = read_tempreg_pci;
543 for (i = 0; i < ARRAY_SIZE(tctl_offset_table); i++) {
544 const struct tctl_offset *entry = &tctl_offset_table[i];
546 if (boot_cpu_data.x86 == entry->model &&
547 strstr(boot_cpu_data.x86_model_id, entry->id)) {
548 data->temp_offset = entry->offset;
553 hwmon_dev = devm_hwmon_device_register_with_info(dev, "k10temp", data,
556 return PTR_ERR_OR_ZERO(hwmon_dev);
559 static const struct pci_device_id k10temp_id_table[] = {
560 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_10H_NB_MISC) },
561 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_11H_NB_MISC) },
562 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CNB17H_F3) },
563 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F3) },
564 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M10H_F3) },
565 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F3) },
566 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F3) },
567 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F3) },
568 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F3) },
569 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F3) },
570 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_DF_F3) },
571 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M10H_DF_F3) },
572 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M30H_DF_F3) },
573 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M70H_DF_F3) },
574 { PCI_VDEVICE(HYGON, PCI_DEVICE_ID_AMD_17H_DF_F3) },
577 MODULE_DEVICE_TABLE(pci, k10temp_id_table);
579 static struct pci_driver k10temp_driver = {
581 .id_table = k10temp_id_table,
582 .probe = k10temp_probe,
585 module_pci_driver(k10temp_driver);