blob: a250481b5a97f825bcac5ed6b6c7ecd94006de42 [file] [log] [blame]
Thomas Gleixner6e7c1092019-05-20 09:18:57 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Clemens Ladisch3c57e892009-12-16 21:38:25 +01002/*
Guenter Roeckd547552a12019-12-24 07:20:55 -08003 * k10temp.c - AMD Family 10h/11h/12h/14h/15h/16h/17h
4 * processor hardware monitoring
Clemens Ladisch3c57e892009-12-16 21:38:25 +01005 *
6 * Copyright (c) 2009 Clemens Ladisch <clemens@ladisch.de>
Guenter Roeckd547552a12019-12-24 07:20:55 -08007 * Copyright (c) 2020 Guenter Roeck <linux@roeck-us.net>
Guenter Roeckc7579382020-01-14 17:40:12 -08008 *
9 * Implementation notes:
Guenter Roeckfd8bdb22020-01-22 18:41:18 -080010 * - CCD register address information as well as the calculation to
Guenter Roeckc7579382020-01-14 17:40:12 -080011 * 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.
Guenter Roeckb00647c2020-01-14 17:54:05 -080014 * - 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
20 * file.
Clemens Ladisch3c57e892009-12-16 21:38:25 +010021 */
22
Guenter Roecka6d210d2018-04-29 08:39:24 -070023#include <linux/bitops.h>
Clemens Ladisch3c57e892009-12-16 21:38:25 +010024#include <linux/err.h>
25#include <linux/hwmon.h>
Clemens Ladisch3c57e892009-12-16 21:38:25 +010026#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/pci.h>
Woods, Briandedf7dc2018-11-06 20:08:14 +000029#include <linux/pci_ids.h>
Guenter Roeck3b031622018-05-04 13:01:33 -070030#include <asm/amd_nb.h>
Clemens Ladisch3c57e892009-12-16 21:38:25 +010031#include <asm/processor.h>
32
Andre Przywara9e581312011-05-25 20:43:31 +020033MODULE_DESCRIPTION("AMD Family 10h+ CPU core temperature monitor");
Clemens Ladisch3c57e892009-12-16 21:38:25 +010034MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
35MODULE_LICENSE("GPL");
36
37static bool force;
38module_param(force, bool, 0444);
39MODULE_PARM_DESC(force, "force loading on processors with erratum 319");
40
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -050041/* Provide lock for writing to NB_SMU_IND_ADDR */
42static DEFINE_MUTEX(nb_smu_ind_mutex);
43
Guenter Roeckccaf63b2018-04-29 09:16:45 -070044#ifndef PCI_DEVICE_ID_AMD_15H_M70H_NB_F3
45#define PCI_DEVICE_ID_AMD_15H_M70H_NB_F3 0x15b3
46#endif
47
Clemens Ladischc5114a12010-01-10 20:52:34 +010048/* CPUID function 0x80000001, ebx */
Guenter Roecka6d210d2018-04-29 08:39:24 -070049#define CPUID_PKGTYPE_MASK GENMASK(31, 28)
Clemens Ladischc5114a12010-01-10 20:52:34 +010050#define CPUID_PKGTYPE_F 0x00000000
51#define CPUID_PKGTYPE_AM2R2_AM3 0x10000000
52
53/* DRAM controller (PCI function 2) */
54#define REG_DCT0_CONFIG_HIGH 0x094
Guenter Roecka6d210d2018-04-29 08:39:24 -070055#define DDR3_MODE BIT(8)
Clemens Ladischc5114a12010-01-10 20:52:34 +010056
57/* miscellaneous (PCI function 3) */
Clemens Ladisch3c57e892009-12-16 21:38:25 +010058#define REG_HARDWARE_THERMAL_CONTROL 0x64
Guenter Roecka6d210d2018-04-29 08:39:24 -070059#define HTC_ENABLE BIT(0)
Clemens Ladisch3c57e892009-12-16 21:38:25 +010060
61#define REG_REPORTED_TEMPERATURE 0xa4
62
63#define REG_NORTHBRIDGE_CAPABILITIES 0xe8
Guenter Roecka6d210d2018-04-29 08:39:24 -070064#define NB_CAP_HTC BIT(10)
Clemens Ladisch3c57e892009-12-16 21:38:25 +010065
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -050066/*
Guenter Roeck40626a12018-04-29 08:08:24 -070067 * For F15h M60h and M70h, REG_HARDWARE_THERMAL_CONTROL
68 * and REG_REPORTED_TEMPERATURE have been moved to
69 * D0F0xBC_xD820_0C64 [Hardware Temperature Control]
70 * D0F0xBC_xD820_0CA4 [Reported Temperature Control]
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -050071 */
Guenter Roeck40626a12018-04-29 08:08:24 -070072#define F15H_M60H_HARDWARE_TEMP_CTRL_OFFSET 0xd8200c64
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -050073#define F15H_M60H_REPORTED_TEMP_CTRL_OFFSET 0xd8200ca4
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -050074
Wei Huang17822412020-08-27 00:42:41 -050075/* Common for Zen CPU families (Family 17h and 18h) */
76#define ZEN_REPORTED_TEMP_CTRL_OFFSET 0x00059800
Guenter Roeckfd8bdb22020-01-22 18:41:18 -080077
Wei Huang17822412020-08-27 00:42:41 -050078#define ZEN_CCD_TEMP(x) (0x00059954 + ((x) * 4))
79#define ZEN_CCD_TEMP_VALID BIT(11)
80#define ZEN_CCD_TEMP_MASK GENMASK(10, 0)
Guenter Roeck9af0a9a2017-09-04 18:33:53 -070081
Wei Huang17822412020-08-27 00:42:41 -050082#define ZEN_CUR_TEMP_SHIFT 21
83#define ZEN_CUR_TEMP_RANGE_SEL_MASK BIT(19)
Guenter Roeckb00647c2020-01-14 17:54:05 -080084
Wei Huang17822412020-08-27 00:42:41 -050085#define ZEN_SVI_BASE 0x0005A000
Guenter Roecka6d210d2018-04-29 08:39:24 -070086
Wei Huang17822412020-08-27 00:42:41 -050087/* F17h thermal registers through SMN */
88#define F17H_M01H_SVI_TEL_PLANE0 (ZEN_SVI_BASE + 0xc)
89#define F17H_M01H_SVI_TEL_PLANE1 (ZEN_SVI_BASE + 0x10)
Wei Huangd6144a42020-08-27 00:42:42 -050090#define F17H_M31H_SVI_TEL_PLANE0 (ZEN_SVI_BASE + 0x14)
91#define F17H_M31H_SVI_TEL_PLANE1 (ZEN_SVI_BASE + 0x10)
Wei Huang17822412020-08-27 00:42:41 -050092
Wei Huangd6144a42020-08-27 00:42:42 -050093#define F17H_M01H_CFACTOR_ICORE 1000000 /* 1A / LSB */
94#define F17H_M01H_CFACTOR_ISOC 250000 /* 0.25A / LSB */
95#define F17H_M31H_CFACTOR_ICORE 1000000 /* 1A / LSB */
96#define F17H_M31H_CFACTOR_ISOC 310000 /* 0.31A / LSB */
Guenter Roeckb00647c2020-01-14 17:54:05 -080097
Wei Huang55163a12020-09-14 15:07:15 -050098/* F19h thermal registers through SMN */
99#define F19H_M01_SVI_TEL_PLANE0 (ZEN_SVI_BASE + 0x14)
100#define F19H_M01_SVI_TEL_PLANE1 (ZEN_SVI_BASE + 0x10)
101
102#define F19H_M01H_CFACTOR_ICORE 1000000 /* 1A / LSB */
103#define F19H_M01H_CFACTOR_ISOC 310000 /* 0.31A / LSB */
104
Guenter Roeck68546ab2017-09-04 18:33:53 -0700105struct k10temp_data {
106 struct pci_dev *pdev;
Guenter Roeck40626a12018-04-29 08:08:24 -0700107 void (*read_htcreg)(struct pci_dev *pdev, u32 *regval);
Guenter Roeck68546ab2017-09-04 18:33:53 -0700108 void (*read_tempreg)(struct pci_dev *pdev, u32 *regval);
Guenter Roeck1b50b772017-09-04 18:33:53 -0700109 int temp_offset;
Guenter Roeck1b597882018-04-24 06:55:55 -0700110 u32 temp_adjust_mask;
Guenter Roeck60465242020-01-23 08:58:22 -0800111 u32 show_temp;
Guenter Roeckb00647c2020-01-14 17:54:05 -0800112 u32 svi_addr[2];
Guenter Roeck60465242020-01-23 08:58:22 -0800113 bool is_zen;
Guenter Roeckb00647c2020-01-14 17:54:05 -0800114 bool show_current;
115 int cfactor[2];
Guenter Roeck1b50b772017-09-04 18:33:53 -0700116};
117
Guenter Roeck60465242020-01-23 08:58:22 -0800118#define TCTL_BIT 0
119#define TDIE_BIT 1
120#define TCCD_BIT(x) ((x) + 2)
121
122#define HAVE_TEMP(d, channel) ((d)->show_temp & BIT(channel))
123#define HAVE_TDIE(d) HAVE_TEMP(d, TDIE_BIT)
124
Guenter Roeck1b50b772017-09-04 18:33:53 -0700125struct tctl_offset {
126 u8 model;
127 char const *id;
128 int offset;
129};
130
131static const struct tctl_offset tctl_offset_table[] = {
Guenter Roeckab5ee242017-11-13 12:38:23 -0800132 { 0x17, "AMD Ryzen 5 1600X", 20000 },
Guenter Roeck1b50b772017-09-04 18:33:53 -0700133 { 0x17, "AMD Ryzen 7 1700X", 20000 },
134 { 0x17, "AMD Ryzen 7 1800X", 20000 },
Guenter Roeck1b597882018-04-24 06:55:55 -0700135 { 0x17, "AMD Ryzen 7 2700X", 10000 },
Guenter Roeckcd6a2062018-08-09 11:50:46 -0700136 { 0x17, "AMD Ryzen Threadripper 19", 27000 }, /* 19{00,20,50}X */
137 { 0x17, "AMD Ryzen Threadripper 29", 27000 }, /* 29{20,50,70,90}[W]X */
Guenter Roeck68546ab2017-09-04 18:33:53 -0700138};
139
Guenter Roeckb00647c2020-01-14 17:54:05 -0800140static bool is_threadripper(void)
141{
142 return strstr(boot_cpu_data.x86_model_id, "Threadripper");
143}
144
145static bool is_epyc(void)
146{
147 return strstr(boot_cpu_data.x86_model_id, "EPYC");
148}
149
Guenter Roeck40626a12018-04-29 08:08:24 -0700150static void read_htcreg_pci(struct pci_dev *pdev, u32 *regval)
151{
152 pci_read_config_dword(pdev, REG_HARDWARE_THERMAL_CONTROL, regval);
153}
154
Guenter Roeck68546ab2017-09-04 18:33:53 -0700155static void read_tempreg_pci(struct pci_dev *pdev, u32 *regval)
156{
157 pci_read_config_dword(pdev, REG_REPORTED_TEMPERATURE, regval);
158}
159
160static void amd_nb_index_read(struct pci_dev *pdev, unsigned int devfn,
161 unsigned int base, int offset, u32 *val)
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -0500162{
163 mutex_lock(&nb_smu_ind_mutex);
164 pci_bus_write_config_dword(pdev->bus, devfn,
Guenter Roeck68546ab2017-09-04 18:33:53 -0700165 base, offset);
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -0500166 pci_bus_read_config_dword(pdev->bus, devfn,
Guenter Roeck68546ab2017-09-04 18:33:53 -0700167 base + 4, val);
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -0500168 mutex_unlock(&nb_smu_ind_mutex);
169}
170
Guenter Roeck40626a12018-04-29 08:08:24 -0700171static void read_htcreg_nb_f15(struct pci_dev *pdev, u32 *regval)
172{
173 amd_nb_index_read(pdev, PCI_DEVFN(0, 0), 0xb8,
174 F15H_M60H_HARDWARE_TEMP_CTRL_OFFSET, regval);
175}
176
Guenter Roeck68546ab2017-09-04 18:33:53 -0700177static void read_tempreg_nb_f15(struct pci_dev *pdev, u32 *regval)
178{
179 amd_nb_index_read(pdev, PCI_DEVFN(0, 0), 0xb8,
180 F15H_M60H_REPORTED_TEMP_CTRL_OFFSET, regval);
181}
182
Wei Huang17822412020-08-27 00:42:41 -0500183static void read_tempreg_nb_zen(struct pci_dev *pdev, u32 *regval)
Guenter Roeck9af0a9a2017-09-04 18:33:53 -0700184{
Guenter Roeck3b031622018-05-04 13:01:33 -0700185 amd_smn_read(amd_pci_dev_to_node_id(pdev),
Wei Huang17822412020-08-27 00:42:41 -0500186 ZEN_REPORTED_TEMP_CTRL_OFFSET, regval);
Guenter Roeck9af0a9a2017-09-04 18:33:53 -0700187}
188
Guenter Roeckd547552a12019-12-24 07:20:55 -0800189static long get_raw_temp(struct k10temp_data *data)
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100190{
Guenter Roeckf934c052018-04-26 12:22:29 -0700191 u32 regval;
Guenter Roeckd547552a12019-12-24 07:20:55 -0800192 long temp;
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100193
Guenter Roeck68546ab2017-09-04 18:33:53 -0700194 data->read_tempreg(data->pdev, &regval);
Wei Huang17822412020-08-27 00:42:41 -0500195 temp = (regval >> ZEN_CUR_TEMP_SHIFT) * 125;
Guenter Roeck1b597882018-04-24 06:55:55 -0700196 if (regval & data->temp_adjust_mask)
197 temp -= 49000;
Guenter Roeckf934c052018-04-26 12:22:29 -0700198 return temp;
199}
200
Jason Yan0e786f32020-04-09 16:45:02 +0800201static const char *k10temp_temp_label[] = {
Guenter Roeckd547552a12019-12-24 07:20:55 -0800202 "Tctl",
Guenter Roeckb02c6852020-01-23 07:57:09 -0800203 "Tdie",
Guenter Roeckc7579382020-01-14 17:40:12 -0800204 "Tccd1",
205 "Tccd2",
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800206 "Tccd3",
207 "Tccd4",
208 "Tccd5",
209 "Tccd6",
210 "Tccd7",
211 "Tccd8",
Guenter Roeckd547552a12019-12-24 07:20:55 -0800212};
213
Jason Yan0e786f32020-04-09 16:45:02 +0800214static const char *k10temp_in_label[] = {
Guenter Roeckb00647c2020-01-14 17:54:05 -0800215 "Vcore",
216 "Vsoc",
217};
218
Jason Yan0e786f32020-04-09 16:45:02 +0800219static const char *k10temp_curr_label[] = {
Guenter Roeckb00647c2020-01-14 17:54:05 -0800220 "Icore",
221 "Isoc",
222};
223
Guenter Roeckd547552a12019-12-24 07:20:55 -0800224static int k10temp_read_labels(struct device *dev,
225 enum hwmon_sensor_types type,
226 u32 attr, int channel, const char **str)
227{
Guenter Roeckb00647c2020-01-14 17:54:05 -0800228 switch (type) {
229 case hwmon_temp:
230 *str = k10temp_temp_label[channel];
231 break;
232 case hwmon_in:
233 *str = k10temp_in_label[channel];
234 break;
235 case hwmon_curr:
236 *str = k10temp_curr_label[channel];
237 break;
238 default:
239 return -EOPNOTSUPP;
240 }
Guenter Roeckd547552a12019-12-24 07:20:55 -0800241 return 0;
242}
243
Guenter Roeckb00647c2020-01-14 17:54:05 -0800244static int k10temp_read_curr(struct device *dev, u32 attr, int channel,
245 long *val)
246{
247 struct k10temp_data *data = dev_get_drvdata(dev);
248 u32 regval;
249
250 switch (attr) {
251 case hwmon_curr_input:
252 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
253 data->svi_addr[channel], &regval);
254 *val = DIV_ROUND_CLOSEST(data->cfactor[channel] *
255 (regval & 0xff),
256 1000);
257 break;
258 default:
259 return -EOPNOTSUPP;
260 }
261 return 0;
262}
263
264static int k10temp_read_in(struct device *dev, u32 attr, int channel, long *val)
265{
266 struct k10temp_data *data = dev_get_drvdata(dev);
267 u32 regval;
268
269 switch (attr) {
270 case hwmon_in_input:
271 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
272 data->svi_addr[channel], &regval);
273 regval = (regval >> 16) & 0xff;
274 *val = DIV_ROUND_CLOSEST(155000 - regval * 625, 100);
275 break;
276 default:
277 return -EOPNOTSUPP;
278 }
279 return 0;
280}
281
282static int k10temp_read_temp(struct device *dev, u32 attr, int channel,
283 long *val)
Guenter Roeckf934c052018-04-26 12:22:29 -0700284{
285 struct k10temp_data *data = dev_get_drvdata(dev);
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100286 u32 regval;
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100287
Guenter Roeckd547552a12019-12-24 07:20:55 -0800288 switch (attr) {
289 case hwmon_temp_input:
290 switch (channel) {
Guenter Roeckb02c6852020-01-23 07:57:09 -0800291 case 0: /* Tctl */
292 *val = get_raw_temp(data);
Guenter Roeckd547552a12019-12-24 07:20:55 -0800293 if (*val < 0)
294 *val = 0;
295 break;
Guenter Roeckb02c6852020-01-23 07:57:09 -0800296 case 1: /* Tdie */
297 *val = get_raw_temp(data) - data->temp_offset;
Guenter Roeckd547552a12019-12-24 07:20:55 -0800298 if (*val < 0)
299 *val = 0;
300 break;
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800301 case 2 ... 9: /* Tccd{1-8} */
Guenter Roeckc7579382020-01-14 17:40:12 -0800302 amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
Wei Huang17822412020-08-27 00:42:41 -0500303 ZEN_CCD_TEMP(channel - 2), &regval);
304 *val = (regval & ZEN_CCD_TEMP_MASK) * 125 - 49000;
Guenter Roeckc7579382020-01-14 17:40:12 -0800305 break;
Guenter Roeckd547552a12019-12-24 07:20:55 -0800306 default:
307 return -EOPNOTSUPP;
308 }
309 break;
310 case hwmon_temp_max:
311 *val = 70 * 1000;
312 break;
313 case hwmon_temp_crit:
314 data->read_htcreg(data->pdev, &regval);
315 *val = ((regval >> 16) & 0x7f) * 500 + 52000;
316 break;
317 case hwmon_temp_crit_hyst:
318 data->read_htcreg(data->pdev, &regval);
319 *val = (((regval >> 16) & 0x7f)
320 - ((regval >> 24) & 0xf)) * 500 + 52000;
321 break;
322 default:
323 return -EOPNOTSUPP;
324 }
325 return 0;
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100326}
327
Guenter Roeckb00647c2020-01-14 17:54:05 -0800328static int k10temp_read(struct device *dev, enum hwmon_sensor_types type,
329 u32 attr, int channel, long *val)
330{
331 switch (type) {
332 case hwmon_temp:
333 return k10temp_read_temp(dev, attr, channel, val);
334 case hwmon_in:
335 return k10temp_read_in(dev, attr, channel, val);
336 case hwmon_curr:
337 return k10temp_read_curr(dev, attr, channel, val);
338 default:
339 return -EOPNOTSUPP;
340 }
341}
342
Guenter Roeckd547552a12019-12-24 07:20:55 -0800343static umode_t k10temp_is_visible(const void *_data,
344 enum hwmon_sensor_types type,
345 u32 attr, int channel)
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700346{
Guenter Roeckd547552a12019-12-24 07:20:55 -0800347 const struct k10temp_data *data = _data;
Guenter Roeck68546ab2017-09-04 18:33:53 -0700348 struct pci_dev *pdev = data->pdev;
Guenter Roeckf934c052018-04-26 12:22:29 -0700349 u32 reg;
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700350
Guenter Roeckd547552a12019-12-24 07:20:55 -0800351 switch (type) {
352 case hwmon_temp:
353 switch (attr) {
354 case hwmon_temp_input:
Guenter Roeck60465242020-01-23 08:58:22 -0800355 if (!HAVE_TEMP(data, channel))
Guenter Roeckd547552a12019-12-24 07:20:55 -0800356 return 0;
357 break;
358 case hwmon_temp_max:
Guenter Roeck60465242020-01-23 08:58:22 -0800359 if (channel || data->is_zen)
Guenter Roeckd547552a12019-12-24 07:20:55 -0800360 return 0;
361 break;
362 case hwmon_temp_crit:
363 case hwmon_temp_crit_hyst:
364 if (channel || !data->read_htcreg)
365 return 0;
366
367 pci_read_config_dword(pdev,
368 REG_NORTHBRIDGE_CAPABILITIES,
369 &reg);
370 if (!(reg & NB_CAP_HTC))
371 return 0;
372
373 data->read_htcreg(data->pdev, &reg);
374 if (!(reg & HTC_ENABLE))
375 return 0;
376 break;
377 case hwmon_temp_label:
Guenter Roeck60465242020-01-23 08:58:22 -0800378 /* Show temperature labels only on Zen CPUs */
379 if (!data->is_zen || !HAVE_TEMP(data, channel))
Guenter Roeckd547552a12019-12-24 07:20:55 -0800380 return 0;
381 break;
382 default:
383 return 0;
384 }
385 break;
Guenter Roeckb00647c2020-01-14 17:54:05 -0800386 case hwmon_in:
387 case hwmon_curr:
388 if (!data->show_current)
389 return 0;
390 break;
Guenter Roeckf934c052018-04-26 12:22:29 -0700391 default:
Guenter Roeckd547552a12019-12-24 07:20:55 -0800392 return 0;
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700393 }
Guenter Roeckd547552a12019-12-24 07:20:55 -0800394 return 0444;
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700395}
396
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500397static bool has_erratum_319(struct pci_dev *pdev)
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100398{
Clemens Ladischc5114a12010-01-10 20:52:34 +0100399 u32 pkg_type, reg_dram_cfg;
400
401 if (boot_cpu_data.x86 != 0x10)
402 return false;
403
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100404 /*
Clemens Ladischc5114a12010-01-10 20:52:34 +0100405 * Erratum 319: The thermal sensor of Socket F/AM2+ processors
406 * may be unreliable.
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100407 */
Clemens Ladischc5114a12010-01-10 20:52:34 +0100408 pkg_type = cpuid_ebx(0x80000001) & CPUID_PKGTYPE_MASK;
409 if (pkg_type == CPUID_PKGTYPE_F)
410 return true;
411 if (pkg_type != CPUID_PKGTYPE_AM2R2_AM3)
412 return false;
413
Jean Delvareeefc2d92010-06-20 09:22:31 +0200414 /* DDR3 memory implies socket AM3, which is good */
Clemens Ladischc5114a12010-01-10 20:52:34 +0100415 pci_bus_read_config_dword(pdev->bus,
416 PCI_DEVFN(PCI_SLOT(pdev->devfn), 2),
417 REG_DCT0_CONFIG_HIGH, &reg_dram_cfg);
Jean Delvareeefc2d92010-06-20 09:22:31 +0200418 if (reg_dram_cfg & DDR3_MODE)
419 return false;
420
421 /*
422 * Unfortunately it is possible to run a socket AM3 CPU with DDR2
423 * memory. We blacklist all the cores which do exist in socket AM2+
424 * format. It still isn't perfect, as RB-C2 cores exist in both AM2+
425 * and AM3 formats, but that's the best we can do.
426 */
427 return boot_cpu_data.x86_model < 4 ||
Jia Zhangb3991512018-01-01 09:52:10 +0800428 (boot_cpu_data.x86_model == 4 && boot_cpu_data.x86_stepping <= 2);
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100429}
430
Guenter Roeckd547552a12019-12-24 07:20:55 -0800431static const struct hwmon_channel_info *k10temp_info[] = {
432 HWMON_CHANNEL_INFO(temp,
433 HWMON_T_INPUT | HWMON_T_MAX |
434 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
435 HWMON_T_LABEL,
Guenter Roeckc7579382020-01-14 17:40:12 -0800436 HWMON_T_INPUT | HWMON_T_LABEL,
437 HWMON_T_INPUT | HWMON_T_LABEL,
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800438 HWMON_T_INPUT | HWMON_T_LABEL,
439 HWMON_T_INPUT | HWMON_T_LABEL,
440 HWMON_T_INPUT | HWMON_T_LABEL,
441 HWMON_T_INPUT | HWMON_T_LABEL,
442 HWMON_T_INPUT | HWMON_T_LABEL,
443 HWMON_T_INPUT | HWMON_T_LABEL,
Guenter Roeckd547552a12019-12-24 07:20:55 -0800444 HWMON_T_INPUT | HWMON_T_LABEL),
Guenter Roeckb00647c2020-01-14 17:54:05 -0800445 HWMON_CHANNEL_INFO(in,
446 HWMON_I_INPUT | HWMON_I_LABEL,
447 HWMON_I_INPUT | HWMON_I_LABEL),
448 HWMON_CHANNEL_INFO(curr,
449 HWMON_C_INPUT | HWMON_C_LABEL,
450 HWMON_C_INPUT | HWMON_C_LABEL),
Guenter Roeckd547552a12019-12-24 07:20:55 -0800451 NULL
452};
453
454static const struct hwmon_ops k10temp_hwmon_ops = {
455 .is_visible = k10temp_is_visible,
456 .read = k10temp_read,
457 .read_string = k10temp_read_labels,
458};
459
460static const struct hwmon_chip_info k10temp_chip_info = {
461 .ops = &k10temp_hwmon_ops,
462 .info = k10temp_info,
463};
464
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800465static void k10temp_get_ccd_support(struct pci_dev *pdev,
466 struct k10temp_data *data, int limit)
467{
468 u32 regval;
469 int i;
470
471 for (i = 0; i < limit; i++) {
472 amd_smn_read(amd_pci_dev_to_node_id(pdev),
Wei Huang17822412020-08-27 00:42:41 -0500473 ZEN_CCD_TEMP(i), &regval);
474 if (regval & ZEN_CCD_TEMP_VALID)
Guenter Roeck60465242020-01-23 08:58:22 -0800475 data->show_temp |= BIT(TCCD_BIT(i));
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800476 }
477}
478
Guenter Roeckd547552a12019-12-24 07:20:55 -0800479static int k10temp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100480{
Clemens Ladischc5114a12010-01-10 20:52:34 +0100481 int unreliable = has_erratum_319(pdev);
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700482 struct device *dev = &pdev->dev;
Guenter Roeck68546ab2017-09-04 18:33:53 -0700483 struct k10temp_data *data;
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700484 struct device *hwmon_dev;
Guenter Roeck1b50b772017-09-04 18:33:53 -0700485 int i;
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100486
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700487 if (unreliable) {
488 if (!force) {
489 dev_err(dev,
490 "unreliable CPU thermal sensor; monitoring disabled\n");
491 return -ENODEV;
492 }
493 dev_warn(dev,
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100494 "unreliable CPU thermal sensor; check erratum 319\n");
Guenter Roeck3e3e1022014-08-15 09:27:03 -0700495 }
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100496
Guenter Roeck68546ab2017-09-04 18:33:53 -0700497 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
498 if (!data)
499 return -ENOMEM;
500
501 data->pdev = pdev;
Guenter Roeck60465242020-01-23 08:58:22 -0800502 data->show_temp |= BIT(TCTL_BIT); /* Always show Tctl */
Guenter Roeck68546ab2017-09-04 18:33:53 -0700503
Guenter Roeck53dfa002018-09-02 12:02:53 -0700504 if (boot_cpu_data.x86 == 0x15 &&
505 ((boot_cpu_data.x86_model & 0xf0) == 0x60 ||
506 (boot_cpu_data.x86_model & 0xf0) == 0x70)) {
Guenter Roeck40626a12018-04-29 08:08:24 -0700507 data->read_htcreg = read_htcreg_nb_f15;
Guenter Roeck68546ab2017-09-04 18:33:53 -0700508 data->read_tempreg = read_tempreg_nb_f15;
Pu Wend93217d2018-12-08 14:33:28 +0800509 } else if (boot_cpu_data.x86 == 0x17 || boot_cpu_data.x86 == 0x18) {
Wei Huang17822412020-08-27 00:42:41 -0500510 data->temp_adjust_mask = ZEN_CUR_TEMP_RANGE_SEL_MASK;
511 data->read_tempreg = read_tempreg_nb_zen;
Guenter Roeck60465242020-01-23 08:58:22 -0800512 data->show_temp |= BIT(TDIE_BIT); /* show Tdie */
513 data->is_zen = true;
Guenter Roeckc7579382020-01-14 17:40:12 -0800514
515 switch (boot_cpu_data.x86_model) {
516 case 0x1: /* Zen */
517 case 0x8: /* Zen+ */
518 case 0x11: /* Zen APU */
519 case 0x18: /* Zen+ APU */
Guenter Roeckb00647c2020-01-14 17:54:05 -0800520 data->show_current = !is_threadripper() && !is_epyc();
521 data->svi_addr[0] = F17H_M01H_SVI_TEL_PLANE0;
522 data->svi_addr[1] = F17H_M01H_SVI_TEL_PLANE1;
Wei Huangd6144a42020-08-27 00:42:42 -0500523 data->cfactor[0] = F17H_M01H_CFACTOR_ICORE;
524 data->cfactor[1] = F17H_M01H_CFACTOR_ISOC;
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800525 k10temp_get_ccd_support(pdev, data, 4);
Guenter Roeckc7579382020-01-14 17:40:12 -0800526 break;
527 case 0x31: /* Zen2 Threadripper */
528 case 0x71: /* Zen2 */
Guenter Roeckb00647c2020-01-14 17:54:05 -0800529 data->show_current = !is_threadripper() && !is_epyc();
Wei Huangd6144a42020-08-27 00:42:42 -0500530 data->cfactor[0] = F17H_M31H_CFACTOR_ICORE;
531 data->cfactor[1] = F17H_M31H_CFACTOR_ISOC;
532 data->svi_addr[0] = F17H_M31H_SVI_TEL_PLANE0;
533 data->svi_addr[1] = F17H_M31H_SVI_TEL_PLANE1;
Guenter Roeckfd8bdb22020-01-22 18:41:18 -0800534 k10temp_get_ccd_support(pdev, data, 8);
Guenter Roeckc7579382020-01-14 17:40:12 -0800535 break;
536 }
Wei Huang55163a12020-09-14 15:07:15 -0500537 } else if (boot_cpu_data.x86 == 0x19) {
538 data->temp_adjust_mask = ZEN_CUR_TEMP_RANGE_SEL_MASK;
539 data->read_tempreg = read_tempreg_nb_zen;
540 data->show_temp |= BIT(TDIE_BIT);
541 data->is_zen = true;
542
543 switch (boot_cpu_data.x86_model) {
544 case 0x0 ... 0x1: /* Zen3 */
545 data->show_current = true;
546 data->svi_addr[0] = F19H_M01_SVI_TEL_PLANE0;
547 data->svi_addr[1] = F19H_M01_SVI_TEL_PLANE1;
548 data->cfactor[0] = F19H_M01H_CFACTOR_ICORE;
549 data->cfactor[1] = F19H_M01H_CFACTOR_ISOC;
550 k10temp_get_ccd_support(pdev, data, 8);
551 break;
552 }
Guenter Roeck1b597882018-04-24 06:55:55 -0700553 } else {
Guenter Roeck40626a12018-04-29 08:08:24 -0700554 data->read_htcreg = read_htcreg_pci;
Guenter Roeck68546ab2017-09-04 18:33:53 -0700555 data->read_tempreg = read_tempreg_pci;
Guenter Roeck1b597882018-04-24 06:55:55 -0700556 }
Guenter Roeck68546ab2017-09-04 18:33:53 -0700557
Guenter Roeck1b50b772017-09-04 18:33:53 -0700558 for (i = 0; i < ARRAY_SIZE(tctl_offset_table); i++) {
559 const struct tctl_offset *entry = &tctl_offset_table[i];
560
561 if (boot_cpu_data.x86 == entry->model &&
562 strstr(boot_cpu_data.x86_model_id, entry->id)) {
563 data->temp_offset = entry->offset;
564 break;
565 }
566 }
567
Guenter Roeckd547552a12019-12-24 07:20:55 -0800568 hwmon_dev = devm_hwmon_device_register_with_info(dev, "k10temp", data,
569 &k10temp_chip_info,
570 NULL);
Guenter Roeck8999eab2020-09-08 10:13:45 -0700571 return PTR_ERR_OR_ZERO(hwmon_dev);
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100572}
573
Jingoo Hancd9bb052013-12-03 07:10:29 +0000574static const struct pci_device_id k10temp_id_table[] = {
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100575 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_10H_NB_MISC) },
576 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_11H_NB_MISC) },
Clemens Ladischaa4790a2011-02-17 03:22:40 -0500577 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CNB17H_F3) },
Andre Przywara9e581312011-05-25 20:43:31 +0200578 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F3) },
Borislav Petkov24214442012-05-04 18:28:21 +0200579 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M10H_F3) },
Phil Pokornyd303b1b2014-01-14 10:46:46 -0800580 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F3) },
Aravind Gopalakrishnanf89ce272014-08-14 18:15:27 -0500581 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F3) },
Guenter Roeckccaf63b2018-04-29 09:16:45 -0700582 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F3) },
Wei Hu30b146d12013-08-23 13:14:03 -0700583 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F3) },
Aravind Gopalakrishnanec015952014-03-11 16:25:59 -0500584 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F3) },
Guenter Roeck9af0a9a2017-09-04 18:33:53 -0700585 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_DF_F3) },
Guenter Roeck3b031622018-05-04 13:01:33 -0700586 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M10H_DF_F3) },
Woods, Brian210ba122018-11-06 20:08:21 +0000587 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M30H_DF_F3) },
Alexander Monakov279f0b32020-05-10 20:48:41 +0000588 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M60H_DF_F3) },
Marcel Bocu12163cf2019-07-22 20:46:53 +0300589 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_17H_M70H_DF_F3) },
Wei Huang55163a12020-09-14 15:07:15 -0500590 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_19H_DF_F3) },
Pu Wend93217d2018-12-08 14:33:28 +0800591 { PCI_VDEVICE(HYGON, PCI_DEVICE_ID_AMD_17H_DF_F3) },
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100592 {}
593};
594MODULE_DEVICE_TABLE(pci, k10temp_id_table);
595
596static struct pci_driver k10temp_driver = {
597 .name = "k10temp",
598 .id_table = k10temp_id_table,
599 .probe = k10temp_probe,
Clemens Ladisch3c57e892009-12-16 21:38:25 +0100600};
601
Axel Linf71f5a52012-04-02 21:25:46 -0400602module_pci_driver(k10temp_driver);