xref: /openbmc/linux/drivers/hwmon/fam15h_power.c (revision 562dc9732ac0ab1274d2ca129b5bee5a3cf778cc)
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>
29512d1027SAndreas Herrmann 
30512d1027SAndreas Herrmann MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
31d034fbf0SAndreas Herrmann MODULE_AUTHOR("Andreas Herrmann <herrmann.der.user@googlemail.com>");
32512d1027SAndreas Herrmann MODULE_LICENSE("GPL");
33512d1027SAndreas Herrmann 
3422e32f4fSBoris Ostrovsky /* Family 16h Northbridge's function 4 PCI ID */
3522e32f4fSBoris Ostrovsky #define PCI_DEVICE_ID_AMD_16H_NB_F4	0x1534
3622e32f4fSBoris Ostrovsky 
37512d1027SAndreas Herrmann /* D18F3 */
38512d1027SAndreas Herrmann #define REG_NORTHBRIDGE_CAP		0xe8
39512d1027SAndreas Herrmann 
40512d1027SAndreas Herrmann /* D18F4 */
41512d1027SAndreas Herrmann #define REG_PROCESSOR_TDP		0x1b8
42512d1027SAndreas Herrmann 
43512d1027SAndreas Herrmann /* D18F5 */
44512d1027SAndreas Herrmann #define REG_TDP_RUNNING_AVERAGE		0xe0
45512d1027SAndreas Herrmann #define REG_TDP_LIMIT3			0xe8
46512d1027SAndreas Herrmann 
47512d1027SAndreas Herrmann struct fam15h_power_data {
48*562dc973SAxel Lin 	struct pci_dev *pdev;
49512d1027SAndreas Herrmann 	unsigned int tdp_to_watts;
50512d1027SAndreas Herrmann 	unsigned int base_tdp;
51512d1027SAndreas Herrmann 	unsigned int processor_pwr_watts;
52512d1027SAndreas Herrmann };
53512d1027SAndreas Herrmann 
54512d1027SAndreas Herrmann static ssize_t show_power(struct device *dev,
55512d1027SAndreas Herrmann 			  struct device_attribute *attr, char *buf)
56512d1027SAndreas Herrmann {
57512d1027SAndreas Herrmann 	u32 val, tdp_limit, running_avg_range;
58512d1027SAndreas Herrmann 	s32 running_avg_capture;
59512d1027SAndreas Herrmann 	u64 curr_pwr_watts;
60512d1027SAndreas Herrmann 	struct fam15h_power_data *data = dev_get_drvdata(dev);
61*562dc973SAxel Lin 	struct pci_dev *f4 = data->pdev;
62512d1027SAndreas Herrmann 
63512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
64512d1027SAndreas Herrmann 				  REG_TDP_RUNNING_AVERAGE, &val);
65512d1027SAndreas Herrmann 	running_avg_capture = (val >> 4) & 0x3fffff;
66fc0900cbSAndreas Herrmann 	running_avg_capture = sign_extend32(running_avg_capture, 21);
67941a956bSAndre Przywara 	running_avg_range = (val & 0xf) + 1;
68512d1027SAndreas Herrmann 
69512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
70512d1027SAndreas Herrmann 				  REG_TDP_LIMIT3, &val);
71512d1027SAndreas Herrmann 
72512d1027SAndreas Herrmann 	tdp_limit = val >> 16;
7362867d49SGuenter Roeck 	curr_pwr_watts = ((u64)(tdp_limit +
7462867d49SGuenter Roeck 				data->base_tdp)) << running_avg_range;
75941a956bSAndre Przywara 	curr_pwr_watts -= running_avg_capture;
76512d1027SAndreas Herrmann 	curr_pwr_watts *= data->tdp_to_watts;
77512d1027SAndreas Herrmann 
78512d1027SAndreas Herrmann 	/*
79512d1027SAndreas Herrmann 	 * Convert to microWatt
80512d1027SAndreas Herrmann 	 *
81512d1027SAndreas Herrmann 	 * power is in Watt provided as fixed point integer with
82512d1027SAndreas Herrmann 	 * scaling factor 1/(2^16).  For conversion we use
83512d1027SAndreas Herrmann 	 * (10^6)/(2^16) = 15625/(2^10)
84512d1027SAndreas Herrmann 	 */
85941a956bSAndre Przywara 	curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
86512d1027SAndreas Herrmann 	return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
87512d1027SAndreas Herrmann }
88512d1027SAndreas Herrmann static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL);
89512d1027SAndreas Herrmann 
90512d1027SAndreas Herrmann static ssize_t show_power_crit(struct device *dev,
91512d1027SAndreas Herrmann 			       struct device_attribute *attr, char *buf)
92512d1027SAndreas Herrmann {
93512d1027SAndreas Herrmann 	struct fam15h_power_data *data = dev_get_drvdata(dev);
94512d1027SAndreas Herrmann 
95512d1027SAndreas Herrmann 	return sprintf(buf, "%u\n", data->processor_pwr_watts);
96512d1027SAndreas Herrmann }
97512d1027SAndreas Herrmann static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL);
98512d1027SAndreas Herrmann 
99512d1027SAndreas Herrmann static struct attribute *fam15h_power_attrs[] = {
100512d1027SAndreas Herrmann 	&dev_attr_power1_input.attr,
101512d1027SAndreas Herrmann 	&dev_attr_power1_crit.attr,
102512d1027SAndreas Herrmann 	NULL
103512d1027SAndreas Herrmann };
104512d1027SAndreas Herrmann 
105*562dc973SAxel Lin ATTRIBUTE_GROUPS(fam15h_power);
106512d1027SAndreas Herrmann 
1076c931ae1SBill Pemberton static bool fam15h_power_is_internal_node0(struct pci_dev *f4)
108512d1027SAndreas Herrmann {
109512d1027SAndreas Herrmann 	u32 val;
110512d1027SAndreas Herrmann 
111512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
112512d1027SAndreas Herrmann 				  REG_NORTHBRIDGE_CAP, &val);
113512d1027SAndreas Herrmann 	if ((val & BIT(29)) && ((val >> 30) & 3))
114512d1027SAndreas Herrmann 		return false;
115512d1027SAndreas Herrmann 
116512d1027SAndreas Herrmann 	return true;
117512d1027SAndreas Herrmann }
118512d1027SAndreas Herrmann 
11900250ec9SAndre Przywara /*
12000250ec9SAndre Przywara  * Newer BKDG versions have an updated recommendation on how to properly
12100250ec9SAndre Przywara  * initialize the running average range (was: 0xE, now: 0x9). This avoids
12200250ec9SAndre Przywara  * counter saturations resulting in bogus power readings.
12300250ec9SAndre Przywara  * We correct this value ourselves to cope with older BIOSes.
12400250ec9SAndre Przywara  */
1255f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = {
126c3e40a99SGuenter Roeck 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
127c3e40a99SGuenter Roeck 	{ 0 }
128c3e40a99SGuenter Roeck };
129c3e40a99SGuenter Roeck 
1305f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev)
13100250ec9SAndre Przywara {
13200250ec9SAndre Przywara 	u32 val;
13300250ec9SAndre Przywara 
13400250ec9SAndre Przywara 	/*
13500250ec9SAndre Przywara 	 * let this quirk apply only to the current version of the
13600250ec9SAndre Przywara 	 * northbridge, since future versions may change the behavior
13700250ec9SAndre Przywara 	 */
138c3e40a99SGuenter Roeck 	if (!pci_match_id(affected_device, pdev))
13900250ec9SAndre Przywara 		return;
14000250ec9SAndre Przywara 
14100250ec9SAndre Przywara 	pci_bus_read_config_dword(pdev->bus,
14200250ec9SAndre Przywara 		PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
14300250ec9SAndre Przywara 		REG_TDP_RUNNING_AVERAGE, &val);
14400250ec9SAndre Przywara 	if ((val & 0xf) != 0xe)
14500250ec9SAndre Przywara 		return;
14600250ec9SAndre Przywara 
14700250ec9SAndre Przywara 	val &= ~0xf;
14800250ec9SAndre Przywara 	val |=  0x9;
14900250ec9SAndre Przywara 	pci_bus_write_config_dword(pdev->bus,
15000250ec9SAndre Przywara 		PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
15100250ec9SAndre Przywara 		REG_TDP_RUNNING_AVERAGE, val);
15200250ec9SAndre Przywara }
15300250ec9SAndre Przywara 
1545f0ecb90SAndreas Herrmann #ifdef CONFIG_PM
1555f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev)
1565f0ecb90SAndreas Herrmann {
1575f0ecb90SAndreas Herrmann 	tweak_runavg_range(pdev);
1585f0ecb90SAndreas Herrmann 	return 0;
1595f0ecb90SAndreas Herrmann }
1605f0ecb90SAndreas Herrmann #else
1615f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL
1625f0ecb90SAndreas Herrmann #endif
1635f0ecb90SAndreas Herrmann 
1646c931ae1SBill Pemberton static void fam15h_power_init_data(struct pci_dev *f4,
165512d1027SAndreas Herrmann 					     struct fam15h_power_data *data)
166512d1027SAndreas Herrmann {
167512d1027SAndreas Herrmann 	u32 val;
168512d1027SAndreas Herrmann 	u64 tmp;
169512d1027SAndreas Herrmann 
170512d1027SAndreas Herrmann 	pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
171512d1027SAndreas Herrmann 	data->base_tdp = val >> 16;
172512d1027SAndreas Herrmann 	tmp = val & 0xffff;
173512d1027SAndreas Herrmann 
174512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
175512d1027SAndreas Herrmann 				  REG_TDP_LIMIT3, &val);
176512d1027SAndreas Herrmann 
177512d1027SAndreas Herrmann 	data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
178512d1027SAndreas Herrmann 	tmp *= data->tdp_to_watts;
179512d1027SAndreas Herrmann 
180512d1027SAndreas Herrmann 	/* result not allowed to be >= 256W */
181512d1027SAndreas Herrmann 	if ((tmp >> 16) >= 256)
182b55f3757SGuenter Roeck 		dev_warn(&f4->dev,
183b55f3757SGuenter Roeck 			 "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
184512d1027SAndreas Herrmann 			 (unsigned int) (tmp >> 16));
185512d1027SAndreas Herrmann 
186512d1027SAndreas Herrmann 	/* convert to microWatt */
187512d1027SAndreas Herrmann 	data->processor_pwr_watts = (tmp * 15625) >> 10;
188512d1027SAndreas Herrmann }
189512d1027SAndreas Herrmann 
1906c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev,
191512d1027SAndreas Herrmann 					const struct pci_device_id *id)
192512d1027SAndreas Herrmann {
193512d1027SAndreas Herrmann 	struct fam15h_power_data *data;
19487432a2eSGuenter Roeck 	struct device *dev = &pdev->dev;
195*562dc973SAxel Lin 	struct device *hwmon_dev;
196512d1027SAndreas Herrmann 
19700250ec9SAndre Przywara 	/*
19800250ec9SAndre Przywara 	 * though we ignore every other northbridge, we still have to
19900250ec9SAndre Przywara 	 * do the tweaking on _each_ node in MCM processors as the counters
20000250ec9SAndre Przywara 	 * are working hand-in-hand
20100250ec9SAndre Przywara 	 */
20200250ec9SAndre Przywara 	tweak_runavg_range(pdev);
20300250ec9SAndre Przywara 
20487432a2eSGuenter Roeck 	if (!fam15h_power_is_internal_node0(pdev))
20587432a2eSGuenter Roeck 		return -ENODEV;
206512d1027SAndreas Herrmann 
20787432a2eSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
20887432a2eSGuenter Roeck 	if (!data)
20987432a2eSGuenter Roeck 		return -ENOMEM;
21087432a2eSGuenter Roeck 
211512d1027SAndreas Herrmann 	fam15h_power_init_data(pdev, data);
212*562dc973SAxel Lin 	data->pdev = pdev;
213512d1027SAndreas Herrmann 
214*562dc973SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
215*562dc973SAxel Lin 							   data,
216*562dc973SAxel Lin 							   fam15h_power_groups);
217*562dc973SAxel Lin 	return PTR_ERR_OR_ZERO(hwmon_dev);
218512d1027SAndreas Herrmann }
219512d1027SAndreas Herrmann 
220cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = {
221512d1027SAndreas Herrmann 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
22222e32f4fSBoris Ostrovsky 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
223512d1027SAndreas Herrmann 	{}
224512d1027SAndreas Herrmann };
225512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
226512d1027SAndreas Herrmann 
227512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = {
228512d1027SAndreas Herrmann 	.name = "fam15h_power",
229512d1027SAndreas Herrmann 	.id_table = fam15h_power_id_table,
230512d1027SAndreas Herrmann 	.probe = fam15h_power_probe,
2315f0ecb90SAndreas Herrmann 	.resume = fam15h_power_resume,
232512d1027SAndreas Herrmann };
233512d1027SAndreas Herrmann 
234f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver);
235