xref: /openbmc/linux/drivers/hwmon/fam15h_power.c (revision eff2a94598ee0c0c7f293a1d3d1999a5e887797a)
1512d1027SAndreas Herrmann /*
2512d1027SAndreas Herrmann  * fam15h_power.c - AMD Family 15h processor power monitoring
3512d1027SAndreas Herrmann  *
4512d1027SAndreas Herrmann  * Copyright (c) 2011 Advanced Micro Devices, Inc.
5d034fbf0SAndreas Herrmann  * Author: Andreas Herrmann <herrmann.der.user@googlemail.com>
6512d1027SAndreas Herrmann  *
7512d1027SAndreas Herrmann  *
8512d1027SAndreas Herrmann  * This driver is free software; you can redistribute it and/or
9512d1027SAndreas Herrmann  * modify it under the terms of the GNU General Public License; either
10512d1027SAndreas Herrmann  * version 2 of the License, or (at your option) any later version.
11512d1027SAndreas Herrmann  *
12512d1027SAndreas Herrmann  * This driver is distributed in the hope that it will be useful,
13512d1027SAndreas Herrmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14512d1027SAndreas Herrmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15512d1027SAndreas Herrmann  * See the GNU General Public License for more details.
16512d1027SAndreas Herrmann  *
17512d1027SAndreas Herrmann  * You should have received a copy of the GNU General Public License
18512d1027SAndreas Herrmann  * along with this driver; if not, see <http://www.gnu.org/licenses/>.
19512d1027SAndreas Herrmann  */
20512d1027SAndreas Herrmann 
21512d1027SAndreas Herrmann #include <linux/err.h>
22512d1027SAndreas Herrmann #include <linux/hwmon.h>
23512d1027SAndreas Herrmann #include <linux/hwmon-sysfs.h>
24512d1027SAndreas Herrmann #include <linux/init.h>
25512d1027SAndreas Herrmann #include <linux/module.h>
26512d1027SAndreas Herrmann #include <linux/pci.h>
27512d1027SAndreas Herrmann #include <linux/bitops.h>
28512d1027SAndreas Herrmann #include <asm/processor.h>
293b5ea47dSHuang Rui #include <asm/msr.h>
30512d1027SAndreas Herrmann 
31512d1027SAndreas Herrmann MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
32d034fbf0SAndreas Herrmann MODULE_AUTHOR("Andreas Herrmann <herrmann.der.user@googlemail.com>");
33512d1027SAndreas Herrmann MODULE_LICENSE("GPL");
34512d1027SAndreas Herrmann 
35512d1027SAndreas Herrmann /* D18F3 */
36512d1027SAndreas Herrmann #define REG_NORTHBRIDGE_CAP		0xe8
37512d1027SAndreas Herrmann 
38512d1027SAndreas Herrmann /* D18F4 */
39512d1027SAndreas Herrmann #define REG_PROCESSOR_TDP		0x1b8
40512d1027SAndreas Herrmann 
41512d1027SAndreas Herrmann /* D18F5 */
42512d1027SAndreas Herrmann #define REG_TDP_RUNNING_AVERAGE		0xe0
43512d1027SAndreas Herrmann #define REG_TDP_LIMIT3			0xe8
44512d1027SAndreas Herrmann 
457deb14b1SHuang Rui #define FAM15H_MIN_NUM_ATTRS		2
467deb14b1SHuang Rui #define FAM15H_NUM_GROUPS		2
477deb14b1SHuang Rui 
483b5ea47dSHuang Rui #define MSR_F15H_CU_MAX_PWR_ACCUMULATOR	0xc001007b
493b5ea47dSHuang Rui 
50*eff2a945SHuang Rui #define PCI_DEVICE_ID_AMD_15H_M70H_NB_F4 0x15b4
51*eff2a945SHuang Rui 
52512d1027SAndreas Herrmann struct fam15h_power_data {
53562dc973SAxel Lin 	struct pci_dev *pdev;
54512d1027SAndreas Herrmann 	unsigned int tdp_to_watts;
55512d1027SAndreas Herrmann 	unsigned int base_tdp;
56512d1027SAndreas Herrmann 	unsigned int processor_pwr_watts;
571ed32160SHuang Rui 	unsigned int cpu_pwr_sample_ratio;
587deb14b1SHuang Rui 	const struct attribute_group *groups[FAM15H_NUM_GROUPS];
597deb14b1SHuang Rui 	struct attribute_group group;
603b5ea47dSHuang Rui 	/* maximum accumulated power of a compute unit */
613b5ea47dSHuang Rui 	u64 max_cu_acc_power;
62512d1027SAndreas Herrmann };
63512d1027SAndreas Herrmann 
64512d1027SAndreas Herrmann static ssize_t show_power(struct device *dev,
65512d1027SAndreas Herrmann 			  struct device_attribute *attr, char *buf)
66512d1027SAndreas Herrmann {
67512d1027SAndreas Herrmann 	u32 val, tdp_limit, running_avg_range;
68512d1027SAndreas Herrmann 	s32 running_avg_capture;
69512d1027SAndreas Herrmann 	u64 curr_pwr_watts;
70512d1027SAndreas Herrmann 	struct fam15h_power_data *data = dev_get_drvdata(dev);
71562dc973SAxel Lin 	struct pci_dev *f4 = data->pdev;
72512d1027SAndreas Herrmann 
73512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
74512d1027SAndreas Herrmann 				  REG_TDP_RUNNING_AVERAGE, &val);
75e9cd4d55SHuang Rui 
76e9cd4d55SHuang Rui 	/*
77e9cd4d55SHuang Rui 	 * On Carrizo and later platforms, TdpRunAvgAccCap bit field
78e9cd4d55SHuang Rui 	 * is extended to 4:31 from 4:25.
79e9cd4d55SHuang Rui 	 */
80e9cd4d55SHuang Rui 	if (boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60) {
81e9cd4d55SHuang Rui 		running_avg_capture = val >> 4;
82e9cd4d55SHuang Rui 		running_avg_capture = sign_extend32(running_avg_capture, 27);
83e9cd4d55SHuang Rui 	} else {
84512d1027SAndreas Herrmann 		running_avg_capture = (val >> 4) & 0x3fffff;
85fc0900cbSAndreas Herrmann 		running_avg_capture = sign_extend32(running_avg_capture, 21);
86e9cd4d55SHuang Rui 	}
87e9cd4d55SHuang Rui 
88941a956bSAndre Przywara 	running_avg_range = (val & 0xf) + 1;
89512d1027SAndreas Herrmann 
90512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
91512d1027SAndreas Herrmann 				  REG_TDP_LIMIT3, &val);
92512d1027SAndreas Herrmann 
93512d1027SAndreas Herrmann 	tdp_limit = val >> 16;
9462867d49SGuenter Roeck 	curr_pwr_watts = ((u64)(tdp_limit +
9562867d49SGuenter Roeck 				data->base_tdp)) << running_avg_range;
96941a956bSAndre Przywara 	curr_pwr_watts -= running_avg_capture;
97512d1027SAndreas Herrmann 	curr_pwr_watts *= data->tdp_to_watts;
98512d1027SAndreas Herrmann 
99512d1027SAndreas Herrmann 	/*
100512d1027SAndreas Herrmann 	 * Convert to microWatt
101512d1027SAndreas Herrmann 	 *
102512d1027SAndreas Herrmann 	 * power is in Watt provided as fixed point integer with
103512d1027SAndreas Herrmann 	 * scaling factor 1/(2^16).  For conversion we use
104512d1027SAndreas Herrmann 	 * (10^6)/(2^16) = 15625/(2^10)
105512d1027SAndreas Herrmann 	 */
106941a956bSAndre Przywara 	curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
107512d1027SAndreas Herrmann 	return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
108512d1027SAndreas Herrmann }
109512d1027SAndreas Herrmann static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL);
110512d1027SAndreas Herrmann 
111512d1027SAndreas Herrmann static ssize_t show_power_crit(struct device *dev,
112512d1027SAndreas Herrmann 			       struct device_attribute *attr, char *buf)
113512d1027SAndreas Herrmann {
114512d1027SAndreas Herrmann 	struct fam15h_power_data *data = dev_get_drvdata(dev);
115512d1027SAndreas Herrmann 
116512d1027SAndreas Herrmann 	return sprintf(buf, "%u\n", data->processor_pwr_watts);
117512d1027SAndreas Herrmann }
118512d1027SAndreas Herrmann static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL);
119512d1027SAndreas Herrmann 
1207deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev,
1217deb14b1SHuang Rui 				   struct fam15h_power_data *data)
122961a2378SAravind Gopalakrishnan {
1237deb14b1SHuang Rui 	int n = FAM15H_MIN_NUM_ATTRS;
1247deb14b1SHuang Rui 	struct attribute **fam15h_power_attrs;
12546f29c2bSHuang Rui 	struct cpuinfo_x86 *c = &boot_cpu_data;
1267deb14b1SHuang Rui 
12746f29c2bSHuang Rui 	if (c->x86 == 0x15 &&
12846f29c2bSHuang Rui 	    (c->x86_model <= 0xf ||
129*eff2a945SHuang Rui 	     (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
1307deb14b1SHuang Rui 		n += 1;
1317deb14b1SHuang Rui 
1327deb14b1SHuang Rui 	fam15h_power_attrs = devm_kcalloc(&pdev->dev, n,
1337deb14b1SHuang Rui 					  sizeof(*fam15h_power_attrs),
1347deb14b1SHuang Rui 					  GFP_KERNEL);
1357deb14b1SHuang Rui 
1367deb14b1SHuang Rui 	if (!fam15h_power_attrs)
1377deb14b1SHuang Rui 		return -ENOMEM;
1387deb14b1SHuang Rui 
1397deb14b1SHuang Rui 	n = 0;
1407deb14b1SHuang Rui 	fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr;
14146f29c2bSHuang Rui 	if (c->x86 == 0x15 &&
14246f29c2bSHuang Rui 	    (c->x86_model <= 0xf ||
143*eff2a945SHuang Rui 	     (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
1447deb14b1SHuang Rui 		fam15h_power_attrs[n++] = &dev_attr_power1_input.attr;
1457deb14b1SHuang Rui 
1467deb14b1SHuang Rui 	data->group.attrs = fam15h_power_attrs;
1477deb14b1SHuang Rui 
148961a2378SAravind Gopalakrishnan 	return 0;
149961a2378SAravind Gopalakrishnan }
150961a2378SAravind Gopalakrishnan 
151d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4)
152512d1027SAndreas Herrmann {
153512d1027SAndreas Herrmann 	u32 val;
154512d1027SAndreas Herrmann 
155512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
156512d1027SAndreas Herrmann 				  REG_NORTHBRIDGE_CAP, &val);
157512d1027SAndreas Herrmann 	if ((val & BIT(29)) && ((val >> 30) & 3))
158512d1027SAndreas Herrmann 		return false;
159512d1027SAndreas Herrmann 
160512d1027SAndreas Herrmann 	return true;
161512d1027SAndreas Herrmann }
162512d1027SAndreas Herrmann 
16300250ec9SAndre Przywara /*
16400250ec9SAndre Przywara  * Newer BKDG versions have an updated recommendation on how to properly
16500250ec9SAndre Przywara  * initialize the running average range (was: 0xE, now: 0x9). This avoids
16600250ec9SAndre Przywara  * counter saturations resulting in bogus power readings.
16700250ec9SAndre Przywara  * We correct this value ourselves to cope with older BIOSes.
16800250ec9SAndre Przywara  */
1695f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = {
170c3e40a99SGuenter Roeck 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
171c3e40a99SGuenter Roeck 	{ 0 }
172c3e40a99SGuenter Roeck };
173c3e40a99SGuenter Roeck 
1745f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev)
17500250ec9SAndre Przywara {
17600250ec9SAndre Przywara 	u32 val;
17700250ec9SAndre Przywara 
17800250ec9SAndre Przywara 	/*
17900250ec9SAndre Przywara 	 * let this quirk apply only to the current version of the
18000250ec9SAndre Przywara 	 * northbridge, since future versions may change the behavior
18100250ec9SAndre Przywara 	 */
182c3e40a99SGuenter Roeck 	if (!pci_match_id(affected_device, pdev))
18300250ec9SAndre Przywara 		return;
18400250ec9SAndre Przywara 
18500250ec9SAndre Przywara 	pci_bus_read_config_dword(pdev->bus,
18600250ec9SAndre Przywara 		PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
18700250ec9SAndre Przywara 		REG_TDP_RUNNING_AVERAGE, &val);
18800250ec9SAndre Przywara 	if ((val & 0xf) != 0xe)
18900250ec9SAndre Przywara 		return;
19000250ec9SAndre Przywara 
19100250ec9SAndre Przywara 	val &= ~0xf;
19200250ec9SAndre Przywara 	val |=  0x9;
19300250ec9SAndre Przywara 	pci_bus_write_config_dword(pdev->bus,
19400250ec9SAndre Przywara 		PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
19500250ec9SAndre Przywara 		REG_TDP_RUNNING_AVERAGE, val);
19600250ec9SAndre Przywara }
19700250ec9SAndre Przywara 
1985f0ecb90SAndreas Herrmann #ifdef CONFIG_PM
1995f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev)
2005f0ecb90SAndreas Herrmann {
2015f0ecb90SAndreas Herrmann 	tweak_runavg_range(pdev);
2025f0ecb90SAndreas Herrmann 	return 0;
2035f0ecb90SAndreas Herrmann }
2045f0ecb90SAndreas Herrmann #else
2055f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL
2065f0ecb90SAndreas Herrmann #endif
2075f0ecb90SAndreas Herrmann 
2087deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4,
209512d1027SAndreas Herrmann 				  struct fam15h_power_data *data)
210512d1027SAndreas Herrmann {
2111ed32160SHuang Rui 	u32 val, eax, ebx, ecx, edx;
212512d1027SAndreas Herrmann 	u64 tmp;
2137deb14b1SHuang Rui 	int ret;
214512d1027SAndreas Herrmann 
215512d1027SAndreas Herrmann 	pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
216512d1027SAndreas Herrmann 	data->base_tdp = val >> 16;
217512d1027SAndreas Herrmann 	tmp = val & 0xffff;
218512d1027SAndreas Herrmann 
219512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
220512d1027SAndreas Herrmann 				  REG_TDP_LIMIT3, &val);
221512d1027SAndreas Herrmann 
222512d1027SAndreas Herrmann 	data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
223512d1027SAndreas Herrmann 	tmp *= data->tdp_to_watts;
224512d1027SAndreas Herrmann 
225512d1027SAndreas Herrmann 	/* result not allowed to be >= 256W */
226512d1027SAndreas Herrmann 	if ((tmp >> 16) >= 256)
227b55f3757SGuenter Roeck 		dev_warn(&f4->dev,
228b55f3757SGuenter Roeck 			 "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
229512d1027SAndreas Herrmann 			 (unsigned int) (tmp >> 16));
230512d1027SAndreas Herrmann 
231512d1027SAndreas Herrmann 	/* convert to microWatt */
232512d1027SAndreas Herrmann 	data->processor_pwr_watts = (tmp * 15625) >> 10;
2331ed32160SHuang Rui 
2347deb14b1SHuang Rui 	ret = fam15h_power_init_attrs(f4, data);
2357deb14b1SHuang Rui 	if (ret)
2367deb14b1SHuang Rui 		return ret;
2377deb14b1SHuang Rui 
2381ed32160SHuang Rui 	cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2391ed32160SHuang Rui 
2401ed32160SHuang Rui 	/* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */
2411ed32160SHuang Rui 	if (!(edx & BIT(12)))
2427deb14b1SHuang Rui 		return 0;
2431ed32160SHuang Rui 
2441ed32160SHuang Rui 	/*
2451ed32160SHuang Rui 	 * determine the ratio of the compute unit power accumulator
2461ed32160SHuang Rui 	 * sample period to the PTSC counter period by executing CPUID
2471ed32160SHuang Rui 	 * Fn8000_0007:ECX
2481ed32160SHuang Rui 	 */
2491ed32160SHuang Rui 	data->cpu_pwr_sample_ratio = ecx;
2507deb14b1SHuang Rui 
2513b5ea47dSHuang Rui 	if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
2523b5ea47dSHuang Rui 		pr_err("Failed to read max compute unit power accumulator MSR\n");
2533b5ea47dSHuang Rui 		return -ENODEV;
2543b5ea47dSHuang Rui 	}
2553b5ea47dSHuang Rui 
2563b5ea47dSHuang Rui 	data->max_cu_acc_power = tmp;
2573b5ea47dSHuang Rui 
2587deb14b1SHuang Rui 	return 0;
259512d1027SAndreas Herrmann }
260512d1027SAndreas Herrmann 
2616c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev,
262512d1027SAndreas Herrmann 			      const struct pci_device_id *id)
263512d1027SAndreas Herrmann {
264512d1027SAndreas Herrmann 	struct fam15h_power_data *data;
26587432a2eSGuenter Roeck 	struct device *dev = &pdev->dev;
266562dc973SAxel Lin 	struct device *hwmon_dev;
2677deb14b1SHuang Rui 	int ret;
268512d1027SAndreas Herrmann 
26900250ec9SAndre Przywara 	/*
27000250ec9SAndre Przywara 	 * though we ignore every other northbridge, we still have to
27100250ec9SAndre Przywara 	 * do the tweaking on _each_ node in MCM processors as the counters
27200250ec9SAndre Przywara 	 * are working hand-in-hand
27300250ec9SAndre Przywara 	 */
27400250ec9SAndre Przywara 	tweak_runavg_range(pdev);
27500250ec9SAndre Przywara 
276d83e92b3SHuang Rui 	if (!should_load_on_this_node(pdev))
27787432a2eSGuenter Roeck 		return -ENODEV;
278512d1027SAndreas Herrmann 
27987432a2eSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
28087432a2eSGuenter Roeck 	if (!data)
28187432a2eSGuenter Roeck 		return -ENOMEM;
28287432a2eSGuenter Roeck 
2837deb14b1SHuang Rui 	ret = fam15h_power_init_data(pdev, data);
2847deb14b1SHuang Rui 	if (ret)
2857deb14b1SHuang Rui 		return ret;
2867deb14b1SHuang Rui 
287562dc973SAxel Lin 	data->pdev = pdev;
288512d1027SAndreas Herrmann 
2897deb14b1SHuang Rui 	data->groups[0] = &data->group;
2907deb14b1SHuang Rui 
291562dc973SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
292562dc973SAxel Lin 							   data,
2937deb14b1SHuang Rui 							   &data->groups[0]);
294562dc973SAxel Lin 	return PTR_ERR_OR_ZERO(hwmon_dev);
295512d1027SAndreas Herrmann }
296512d1027SAndreas Herrmann 
297cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = {
298512d1027SAndreas Herrmann 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
2990a0039adSAravind Gopalakrishnan 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
3005dc08725SHuang Rui 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
301*eff2a945SHuang Rui 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) },
30222e32f4fSBoris Ostrovsky 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
3030bd52941SAravind Gopalakrishnan 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
304512d1027SAndreas Herrmann 	{}
305512d1027SAndreas Herrmann };
306512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
307512d1027SAndreas Herrmann 
308512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = {
309512d1027SAndreas Herrmann 	.name = "fam15h_power",
310512d1027SAndreas Herrmann 	.id_table = fam15h_power_id_table,
311512d1027SAndreas Herrmann 	.probe = fam15h_power_probe,
3125f0ecb90SAndreas Herrmann 	.resume = fam15h_power_resume,
313512d1027SAndreas Herrmann };
314512d1027SAndreas Herrmann 
315f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver);
316