xref: /openbmc/linux/drivers/hwmon/fam15h_power.c (revision d013f7f5b7977e605a9515912075e6d008a80184)
1512d1027SAndreas Herrmann /*
2512d1027SAndreas Herrmann  * fam15h_power.c - AMD Family 15h processor power monitoring
3512d1027SAndreas Herrmann  *
4a6e232f7SHuang Rui  * Copyright (c) 2011-2016 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>
28fa794344SHuang Rui #include <linux/cpu.h>
29fa794344SHuang Rui #include <linux/cpumask.h>
3011bf0d78SHuang Rui #include <linux/time.h>
3111bf0d78SHuang Rui #include <linux/sched.h>
32512d1027SAndreas Herrmann #include <asm/processor.h>
333b5ea47dSHuang Rui #include <asm/msr.h>
34512d1027SAndreas Herrmann 
35512d1027SAndreas Herrmann MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
36d034fbf0SAndreas Herrmann MODULE_AUTHOR("Andreas Herrmann <herrmann.der.user@googlemail.com>");
37512d1027SAndreas Herrmann MODULE_LICENSE("GPL");
38512d1027SAndreas Herrmann 
39512d1027SAndreas Herrmann /* D18F3 */
40512d1027SAndreas Herrmann #define REG_NORTHBRIDGE_CAP		0xe8
41512d1027SAndreas Herrmann 
42512d1027SAndreas Herrmann /* D18F4 */
43512d1027SAndreas Herrmann #define REG_PROCESSOR_TDP		0x1b8
44512d1027SAndreas Herrmann 
45512d1027SAndreas Herrmann /* D18F5 */
46512d1027SAndreas Herrmann #define REG_TDP_RUNNING_AVERAGE		0xe0
47512d1027SAndreas Herrmann #define REG_TDP_LIMIT3			0xe8
48512d1027SAndreas Herrmann 
497deb14b1SHuang Rui #define FAM15H_MIN_NUM_ATTRS		2
507deb14b1SHuang Rui #define FAM15H_NUM_GROUPS		2
51fa794344SHuang Rui #define MAX_CUS				8
527deb14b1SHuang Rui 
5311bf0d78SHuang Rui /* set maximum interval as 1 second */
5411bf0d78SHuang Rui #define MAX_INTERVAL			1000
5511bf0d78SHuang Rui 
56fa794344SHuang Rui #define MSR_F15H_CU_PWR_ACCUMULATOR	0xc001007a
573b5ea47dSHuang Rui #define MSR_F15H_CU_MAX_PWR_ACCUMULATOR	0xc001007b
58cdb9e110SHuang Rui #define MSR_F15H_PTSC			0xc0010280
593b5ea47dSHuang Rui 
60eff2a945SHuang Rui #define PCI_DEVICE_ID_AMD_15H_M70H_NB_F4 0x15b4
61eff2a945SHuang Rui 
62512d1027SAndreas Herrmann struct fam15h_power_data {
63562dc973SAxel Lin 	struct pci_dev *pdev;
64512d1027SAndreas Herrmann 	unsigned int tdp_to_watts;
65512d1027SAndreas Herrmann 	unsigned int base_tdp;
66512d1027SAndreas Herrmann 	unsigned int processor_pwr_watts;
671ed32160SHuang Rui 	unsigned int cpu_pwr_sample_ratio;
687deb14b1SHuang Rui 	const struct attribute_group *groups[FAM15H_NUM_GROUPS];
697deb14b1SHuang Rui 	struct attribute_group group;
703b5ea47dSHuang Rui 	/* maximum accumulated power of a compute unit */
713b5ea47dSHuang Rui 	u64 max_cu_acc_power;
72fa794344SHuang Rui 	/* accumulated power of the compute units */
73fa794344SHuang Rui 	u64 cu_acc_power[MAX_CUS];
74cdb9e110SHuang Rui 	/* performance timestamp counter */
75cdb9e110SHuang Rui 	u64 cpu_sw_pwr_ptsc[MAX_CUS];
7611bf0d78SHuang Rui 	/* online/offline status of current compute unit */
7711bf0d78SHuang Rui 	int cu_on[MAX_CUS];
7811bf0d78SHuang Rui 	unsigned long power_period;
79512d1027SAndreas Herrmann };
80512d1027SAndreas Herrmann 
811d28e016SHuang Rui static bool is_carrizo_or_later(void)
821d28e016SHuang Rui {
831d28e016SHuang Rui 	return boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60;
841d28e016SHuang Rui }
851d28e016SHuang Rui 
86*d013f7f5SJulia Lawall static ssize_t power1_input_show(struct device *dev,
87512d1027SAndreas Herrmann 				 struct device_attribute *attr, char *buf)
88512d1027SAndreas Herrmann {
89512d1027SAndreas Herrmann 	u32 val, tdp_limit, running_avg_range;
90512d1027SAndreas Herrmann 	s32 running_avg_capture;
91512d1027SAndreas Herrmann 	u64 curr_pwr_watts;
92512d1027SAndreas Herrmann 	struct fam15h_power_data *data = dev_get_drvdata(dev);
93562dc973SAxel Lin 	struct pci_dev *f4 = data->pdev;
94512d1027SAndreas Herrmann 
95512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
96512d1027SAndreas Herrmann 				  REG_TDP_RUNNING_AVERAGE, &val);
97e9cd4d55SHuang Rui 
98e9cd4d55SHuang Rui 	/*
99e9cd4d55SHuang Rui 	 * On Carrizo and later platforms, TdpRunAvgAccCap bit field
100e9cd4d55SHuang Rui 	 * is extended to 4:31 from 4:25.
101e9cd4d55SHuang Rui 	 */
1021d28e016SHuang Rui 	if (is_carrizo_or_later()) {
103e9cd4d55SHuang Rui 		running_avg_capture = val >> 4;
104e9cd4d55SHuang Rui 		running_avg_capture = sign_extend32(running_avg_capture, 27);
105e9cd4d55SHuang Rui 	} else {
106512d1027SAndreas Herrmann 		running_avg_capture = (val >> 4) & 0x3fffff;
107fc0900cbSAndreas Herrmann 		running_avg_capture = sign_extend32(running_avg_capture, 21);
108e9cd4d55SHuang Rui 	}
109e9cd4d55SHuang Rui 
110941a956bSAndre Przywara 	running_avg_range = (val & 0xf) + 1;
111512d1027SAndreas Herrmann 
112512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
113512d1027SAndreas Herrmann 				  REG_TDP_LIMIT3, &val);
114512d1027SAndreas Herrmann 
11560dee3caSGioh Kim 	/*
11660dee3caSGioh Kim 	 * On Carrizo and later platforms, ApmTdpLimit bit field
11760dee3caSGioh Kim 	 * is extended to 16:31 from 16:28.
11860dee3caSGioh Kim 	 */
1191d28e016SHuang Rui 	if (is_carrizo_or_later())
120512d1027SAndreas Herrmann 		tdp_limit = val >> 16;
12160dee3caSGioh Kim 	else
12260dee3caSGioh Kim 		tdp_limit = (val >> 16) & 0x1fff;
12360dee3caSGioh Kim 
12462867d49SGuenter Roeck 	curr_pwr_watts = ((u64)(tdp_limit +
12562867d49SGuenter Roeck 				data->base_tdp)) << running_avg_range;
126941a956bSAndre Przywara 	curr_pwr_watts -= running_avg_capture;
127512d1027SAndreas Herrmann 	curr_pwr_watts *= data->tdp_to_watts;
128512d1027SAndreas Herrmann 
129512d1027SAndreas Herrmann 	/*
130512d1027SAndreas Herrmann 	 * Convert to microWatt
131512d1027SAndreas Herrmann 	 *
132512d1027SAndreas Herrmann 	 * power is in Watt provided as fixed point integer with
133512d1027SAndreas Herrmann 	 * scaling factor 1/(2^16).  For conversion we use
134512d1027SAndreas Herrmann 	 * (10^6)/(2^16) = 15625/(2^10)
135512d1027SAndreas Herrmann 	 */
136941a956bSAndre Przywara 	curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
137512d1027SAndreas Herrmann 	return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
138512d1027SAndreas Herrmann }
139*d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_input);
140512d1027SAndreas Herrmann 
141*d013f7f5SJulia Lawall static ssize_t power1_crit_show(struct device *dev,
142512d1027SAndreas Herrmann 				struct device_attribute *attr, char *buf)
143512d1027SAndreas Herrmann {
144512d1027SAndreas Herrmann 	struct fam15h_power_data *data = dev_get_drvdata(dev);
145512d1027SAndreas Herrmann 
146512d1027SAndreas Herrmann 	return sprintf(buf, "%u\n", data->processor_pwr_watts);
147512d1027SAndreas Herrmann }
148*d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_crit);
149512d1027SAndreas Herrmann 
150fa794344SHuang Rui static void do_read_registers_on_cu(void *_data)
151fa794344SHuang Rui {
152fa794344SHuang Rui 	struct fam15h_power_data *data = _data;
153fa794344SHuang Rui 	int cpu, cu;
154fa794344SHuang Rui 
155fa794344SHuang Rui 	cpu = smp_processor_id();
156fa794344SHuang Rui 
157fa794344SHuang Rui 	/*
158fa794344SHuang Rui 	 * With the new x86 topology modelling, cpu core id actually
159fa794344SHuang Rui 	 * is compute unit id.
160fa794344SHuang Rui 	 */
161fa794344SHuang Rui 	cu = cpu_data(cpu).cpu_core_id;
162fa794344SHuang Rui 
163fa794344SHuang Rui 	rdmsrl_safe(MSR_F15H_CU_PWR_ACCUMULATOR, &data->cu_acc_power[cu]);
164cdb9e110SHuang Rui 	rdmsrl_safe(MSR_F15H_PTSC, &data->cpu_sw_pwr_ptsc[cu]);
16511bf0d78SHuang Rui 
16611bf0d78SHuang Rui 	data->cu_on[cu] = 1;
167fa794344SHuang Rui }
168fa794344SHuang Rui 
169fa794344SHuang Rui /*
170fa794344SHuang Rui  * This function is only able to be called when CPUID
171fa794344SHuang Rui  * Fn8000_0007:EDX[12] is set.
172fa794344SHuang Rui  */
173fa794344SHuang Rui static int read_registers(struct fam15h_power_data *data)
174fa794344SHuang Rui {
175fa794344SHuang Rui 	int core, this_core;
176fa794344SHuang Rui 	cpumask_var_t mask;
1777be48818SBorislav Petkov 	int ret, cpu;
178fa794344SHuang Rui 
179fa794344SHuang Rui 	ret = zalloc_cpumask_var(&mask, GFP_KERNEL);
180fa794344SHuang Rui 	if (!ret)
181fa794344SHuang Rui 		return -ENOMEM;
182fa794344SHuang Rui 
18311bf0d78SHuang Rui 	memset(data->cu_on, 0, sizeof(int) * MAX_CUS);
18411bf0d78SHuang Rui 
185fa794344SHuang Rui 	get_online_cpus();
186fa794344SHuang Rui 
187fa794344SHuang Rui 	/*
188fa794344SHuang Rui 	 * Choose the first online core of each compute unit, and then
189fa794344SHuang Rui 	 * read their MSR value of power and ptsc in a single IPI,
190fa794344SHuang Rui 	 * because the MSR value of CPU core represent the compute
191fa794344SHuang Rui 	 * unit's.
192fa794344SHuang Rui 	 */
193fa794344SHuang Rui 	core = -1;
194fa794344SHuang Rui 
195fa794344SHuang Rui 	for_each_online_cpu(cpu) {
196fa794344SHuang Rui 		this_core = topology_core_id(cpu);
197fa794344SHuang Rui 
198fa794344SHuang Rui 		if (this_core == core)
199fa794344SHuang Rui 			continue;
200fa794344SHuang Rui 
201fa794344SHuang Rui 		core = this_core;
202fa794344SHuang Rui 
203fa794344SHuang Rui 		/* get any CPU on this compute unit */
204fa794344SHuang Rui 		cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask);
205fa794344SHuang Rui 	}
206fa794344SHuang Rui 
2077be48818SBorislav Petkov 	on_each_cpu_mask(mask, do_read_registers_on_cu, data, true);
208fa794344SHuang Rui 
209fa794344SHuang Rui 	put_online_cpus();
210fa794344SHuang Rui 	free_cpumask_var(mask);
211fa794344SHuang Rui 
212fa794344SHuang Rui 	return 0;
213fa794344SHuang Rui }
214fa794344SHuang Rui 
215*d013f7f5SJulia Lawall static ssize_t power1_average_show(struct device *dev,
216*d013f7f5SJulia Lawall 				   struct device_attribute *attr, char *buf)
21711bf0d78SHuang Rui {
21811bf0d78SHuang Rui 	struct fam15h_power_data *data = dev_get_drvdata(dev);
21911bf0d78SHuang Rui 	u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS],
22011bf0d78SHuang Rui 	    jdelta[MAX_CUS];
22111bf0d78SHuang Rui 	u64 tdelta, avg_acc;
22211bf0d78SHuang Rui 	int cu, cu_num, ret;
22311bf0d78SHuang Rui 	signed long leftover;
22411bf0d78SHuang Rui 
22511bf0d78SHuang Rui 	/*
22611bf0d78SHuang Rui 	 * With the new x86 topology modelling, x86_max_cores is the
22711bf0d78SHuang Rui 	 * compute unit number.
22811bf0d78SHuang Rui 	 */
22911bf0d78SHuang Rui 	cu_num = boot_cpu_data.x86_max_cores;
23011bf0d78SHuang Rui 
23111bf0d78SHuang Rui 	ret = read_registers(data);
23211bf0d78SHuang Rui 	if (ret)
23311bf0d78SHuang Rui 		return 0;
23411bf0d78SHuang Rui 
23511bf0d78SHuang Rui 	for (cu = 0; cu < cu_num; cu++) {
23611bf0d78SHuang Rui 		prev_cu_acc_power[cu] = data->cu_acc_power[cu];
23711bf0d78SHuang Rui 		prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu];
23811bf0d78SHuang Rui 	}
23911bf0d78SHuang Rui 
24011bf0d78SHuang Rui 	leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period));
24111bf0d78SHuang Rui 	if (leftover)
24211bf0d78SHuang Rui 		return 0;
24311bf0d78SHuang Rui 
24411bf0d78SHuang Rui 	ret = read_registers(data);
24511bf0d78SHuang Rui 	if (ret)
24611bf0d78SHuang Rui 		return 0;
24711bf0d78SHuang Rui 
24811bf0d78SHuang Rui 	for (cu = 0, avg_acc = 0; cu < cu_num; cu++) {
24911bf0d78SHuang Rui 		/* check if current compute unit is online */
25011bf0d78SHuang Rui 		if (data->cu_on[cu] == 0)
25111bf0d78SHuang Rui 			continue;
25211bf0d78SHuang Rui 
25311bf0d78SHuang Rui 		if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) {
25411bf0d78SHuang Rui 			jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu];
25511bf0d78SHuang Rui 			jdelta[cu] -= prev_cu_acc_power[cu];
25611bf0d78SHuang Rui 		} else {
25711bf0d78SHuang Rui 			jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu];
25811bf0d78SHuang Rui 		}
25911bf0d78SHuang Rui 		tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
26011bf0d78SHuang Rui 		jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
26111bf0d78SHuang Rui 		do_div(jdelta[cu], tdelta);
26211bf0d78SHuang Rui 
26311bf0d78SHuang Rui 		/* the unit is microWatt */
26411bf0d78SHuang Rui 		avg_acc += jdelta[cu];
26511bf0d78SHuang Rui 	}
26611bf0d78SHuang Rui 
26711bf0d78SHuang Rui 	return sprintf(buf, "%llu\n", (unsigned long long)avg_acc);
26811bf0d78SHuang Rui }
269*d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_average);
27011bf0d78SHuang Rui 
271*d013f7f5SJulia Lawall static ssize_t power1_average_interval_show(struct device *dev,
27211bf0d78SHuang Rui 					    struct device_attribute *attr,
27311bf0d78SHuang Rui 					    char *buf)
27411bf0d78SHuang Rui {
27511bf0d78SHuang Rui 	struct fam15h_power_data *data = dev_get_drvdata(dev);
27611bf0d78SHuang Rui 
27711bf0d78SHuang Rui 	return sprintf(buf, "%lu\n", data->power_period);
27811bf0d78SHuang Rui }
27911bf0d78SHuang Rui 
280*d013f7f5SJulia Lawall static ssize_t power1_average_interval_store(struct device *dev,
28111bf0d78SHuang Rui 					     struct device_attribute *attr,
28211bf0d78SHuang Rui 					     const char *buf, size_t count)
28311bf0d78SHuang Rui {
28411bf0d78SHuang Rui 	struct fam15h_power_data *data = dev_get_drvdata(dev);
28511bf0d78SHuang Rui 	unsigned long temp;
28611bf0d78SHuang Rui 	int ret;
28711bf0d78SHuang Rui 
28811bf0d78SHuang Rui 	ret = kstrtoul(buf, 10, &temp);
28911bf0d78SHuang Rui 	if (ret)
29011bf0d78SHuang Rui 		return ret;
29111bf0d78SHuang Rui 
29211bf0d78SHuang Rui 	if (temp > MAX_INTERVAL)
29311bf0d78SHuang Rui 		return -EINVAL;
29411bf0d78SHuang Rui 
29511bf0d78SHuang Rui 	/* the interval value should be greater than 0 */
29611bf0d78SHuang Rui 	if (temp <= 0)
29711bf0d78SHuang Rui 		return -EINVAL;
29811bf0d78SHuang Rui 
29911bf0d78SHuang Rui 	data->power_period = temp;
30011bf0d78SHuang Rui 
30111bf0d78SHuang Rui 	return count;
30211bf0d78SHuang Rui }
303*d013f7f5SJulia Lawall static DEVICE_ATTR_RW(power1_average_interval);
30411bf0d78SHuang Rui 
3057deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev,
3067deb14b1SHuang Rui 				   struct fam15h_power_data *data)
307961a2378SAravind Gopalakrishnan {
3087deb14b1SHuang Rui 	int n = FAM15H_MIN_NUM_ATTRS;
3097deb14b1SHuang Rui 	struct attribute **fam15h_power_attrs;
31046f29c2bSHuang Rui 	struct cpuinfo_x86 *c = &boot_cpu_data;
3117deb14b1SHuang Rui 
31246f29c2bSHuang Rui 	if (c->x86 == 0x15 &&
31346f29c2bSHuang Rui 	    (c->x86_model <= 0xf ||
314eff2a945SHuang Rui 	     (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
3157deb14b1SHuang Rui 		n += 1;
3167deb14b1SHuang Rui 
31711bf0d78SHuang Rui 	/* check if processor supports accumulated power */
31811bf0d78SHuang Rui 	if (boot_cpu_has(X86_FEATURE_ACC_POWER))
31911bf0d78SHuang Rui 		n += 2;
32011bf0d78SHuang Rui 
3217deb14b1SHuang Rui 	fam15h_power_attrs = devm_kcalloc(&pdev->dev, n,
3227deb14b1SHuang Rui 					  sizeof(*fam15h_power_attrs),
3237deb14b1SHuang Rui 					  GFP_KERNEL);
3247deb14b1SHuang Rui 
3257deb14b1SHuang Rui 	if (!fam15h_power_attrs)
3267deb14b1SHuang Rui 		return -ENOMEM;
3277deb14b1SHuang Rui 
3287deb14b1SHuang Rui 	n = 0;
3297deb14b1SHuang Rui 	fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr;
33046f29c2bSHuang Rui 	if (c->x86 == 0x15 &&
33146f29c2bSHuang Rui 	    (c->x86_model <= 0xf ||
332eff2a945SHuang Rui 	     (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
3337deb14b1SHuang Rui 		fam15h_power_attrs[n++] = &dev_attr_power1_input.attr;
3347deb14b1SHuang Rui 
33511bf0d78SHuang Rui 	if (boot_cpu_has(X86_FEATURE_ACC_POWER)) {
33611bf0d78SHuang Rui 		fam15h_power_attrs[n++] = &dev_attr_power1_average.attr;
33711bf0d78SHuang Rui 		fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr;
33811bf0d78SHuang Rui 	}
33911bf0d78SHuang Rui 
3407deb14b1SHuang Rui 	data->group.attrs = fam15h_power_attrs;
3417deb14b1SHuang Rui 
342961a2378SAravind Gopalakrishnan 	return 0;
343961a2378SAravind Gopalakrishnan }
344961a2378SAravind Gopalakrishnan 
345d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4)
346512d1027SAndreas Herrmann {
347512d1027SAndreas Herrmann 	u32 val;
348512d1027SAndreas Herrmann 
349512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
350512d1027SAndreas Herrmann 				  REG_NORTHBRIDGE_CAP, &val);
351512d1027SAndreas Herrmann 	if ((val & BIT(29)) && ((val >> 30) & 3))
352512d1027SAndreas Herrmann 		return false;
353512d1027SAndreas Herrmann 
354512d1027SAndreas Herrmann 	return true;
355512d1027SAndreas Herrmann }
356512d1027SAndreas Herrmann 
35700250ec9SAndre Przywara /*
35800250ec9SAndre Przywara  * Newer BKDG versions have an updated recommendation on how to properly
35900250ec9SAndre Przywara  * initialize the running average range (was: 0xE, now: 0x9). This avoids
36000250ec9SAndre Przywara  * counter saturations resulting in bogus power readings.
36100250ec9SAndre Przywara  * We correct this value ourselves to cope with older BIOSes.
36200250ec9SAndre Przywara  */
3635f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = {
364c3e40a99SGuenter Roeck 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
365c3e40a99SGuenter Roeck 	{ 0 }
366c3e40a99SGuenter Roeck };
367c3e40a99SGuenter Roeck 
3685f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev)
36900250ec9SAndre Przywara {
37000250ec9SAndre Przywara 	u32 val;
37100250ec9SAndre Przywara 
37200250ec9SAndre Przywara 	/*
37300250ec9SAndre Przywara 	 * let this quirk apply only to the current version of the
37400250ec9SAndre Przywara 	 * northbridge, since future versions may change the behavior
37500250ec9SAndre Przywara 	 */
376c3e40a99SGuenter Roeck 	if (!pci_match_id(affected_device, pdev))
37700250ec9SAndre Przywara 		return;
37800250ec9SAndre Przywara 
37900250ec9SAndre Przywara 	pci_bus_read_config_dword(pdev->bus,
38000250ec9SAndre Przywara 		PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
38100250ec9SAndre Przywara 		REG_TDP_RUNNING_AVERAGE, &val);
38200250ec9SAndre Przywara 	if ((val & 0xf) != 0xe)
38300250ec9SAndre Przywara 		return;
38400250ec9SAndre Przywara 
38500250ec9SAndre Przywara 	val &= ~0xf;
38600250ec9SAndre Przywara 	val |=  0x9;
38700250ec9SAndre Przywara 	pci_bus_write_config_dword(pdev->bus,
38800250ec9SAndre Przywara 		PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
38900250ec9SAndre Przywara 		REG_TDP_RUNNING_AVERAGE, val);
39000250ec9SAndre Przywara }
39100250ec9SAndre Przywara 
3925f0ecb90SAndreas Herrmann #ifdef CONFIG_PM
3935f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev)
3945f0ecb90SAndreas Herrmann {
3955f0ecb90SAndreas Herrmann 	tweak_runavg_range(pdev);
3965f0ecb90SAndreas Herrmann 	return 0;
3975f0ecb90SAndreas Herrmann }
3985f0ecb90SAndreas Herrmann #else
3995f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL
4005f0ecb90SAndreas Herrmann #endif
4015f0ecb90SAndreas Herrmann 
4027deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4,
403512d1027SAndreas Herrmann 				  struct fam15h_power_data *data)
404512d1027SAndreas Herrmann {
40511bf0d78SHuang Rui 	u32 val;
406512d1027SAndreas Herrmann 	u64 tmp;
4077deb14b1SHuang Rui 	int ret;
408512d1027SAndreas Herrmann 
409512d1027SAndreas Herrmann 	pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
410512d1027SAndreas Herrmann 	data->base_tdp = val >> 16;
411512d1027SAndreas Herrmann 	tmp = val & 0xffff;
412512d1027SAndreas Herrmann 
413512d1027SAndreas Herrmann 	pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
414512d1027SAndreas Herrmann 				  REG_TDP_LIMIT3, &val);
415512d1027SAndreas Herrmann 
416512d1027SAndreas Herrmann 	data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
417512d1027SAndreas Herrmann 	tmp *= data->tdp_to_watts;
418512d1027SAndreas Herrmann 
419512d1027SAndreas Herrmann 	/* result not allowed to be >= 256W */
420512d1027SAndreas Herrmann 	if ((tmp >> 16) >= 256)
421b55f3757SGuenter Roeck 		dev_warn(&f4->dev,
422b55f3757SGuenter Roeck 			 "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
423512d1027SAndreas Herrmann 			 (unsigned int) (tmp >> 16));
424512d1027SAndreas Herrmann 
425512d1027SAndreas Herrmann 	/* convert to microWatt */
426512d1027SAndreas Herrmann 	data->processor_pwr_watts = (tmp * 15625) >> 10;
4271ed32160SHuang Rui 
4287deb14b1SHuang Rui 	ret = fam15h_power_init_attrs(f4, data);
4297deb14b1SHuang Rui 	if (ret)
4307deb14b1SHuang Rui 		return ret;
4317deb14b1SHuang Rui 
4321ed32160SHuang Rui 
4331ed32160SHuang Rui 	/* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */
43411bf0d78SHuang Rui 	if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
4357deb14b1SHuang Rui 		return 0;
4361ed32160SHuang Rui 
4371ed32160SHuang Rui 	/*
4381ed32160SHuang Rui 	 * determine the ratio of the compute unit power accumulator
4391ed32160SHuang Rui 	 * sample period to the PTSC counter period by executing CPUID
4401ed32160SHuang Rui 	 * Fn8000_0007:ECX
4411ed32160SHuang Rui 	 */
44211bf0d78SHuang Rui 	data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
4437deb14b1SHuang Rui 
4443b5ea47dSHuang Rui 	if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
4453b5ea47dSHuang Rui 		pr_err("Failed to read max compute unit power accumulator MSR\n");
4463b5ea47dSHuang Rui 		return -ENODEV;
4473b5ea47dSHuang Rui 	}
4483b5ea47dSHuang Rui 
4493b5ea47dSHuang Rui 	data->max_cu_acc_power = tmp;
4503b5ea47dSHuang Rui 
45111bf0d78SHuang Rui 	/*
45211bf0d78SHuang Rui 	 * Milliseconds are a reasonable interval for the measurement.
45311bf0d78SHuang Rui 	 * But it shouldn't set too long here, because several seconds
45411bf0d78SHuang Rui 	 * would cause the read function to hang. So set default
45511bf0d78SHuang Rui 	 * interval as 10 ms.
45611bf0d78SHuang Rui 	 */
45711bf0d78SHuang Rui 	data->power_period = 10;
45811bf0d78SHuang Rui 
459fa794344SHuang Rui 	return read_registers(data);
460512d1027SAndreas Herrmann }
461512d1027SAndreas Herrmann 
4626c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev,
463512d1027SAndreas Herrmann 			      const struct pci_device_id *id)
464512d1027SAndreas Herrmann {
465512d1027SAndreas Herrmann 	struct fam15h_power_data *data;
46687432a2eSGuenter Roeck 	struct device *dev = &pdev->dev;
467562dc973SAxel Lin 	struct device *hwmon_dev;
4687deb14b1SHuang Rui 	int ret;
469512d1027SAndreas Herrmann 
47000250ec9SAndre Przywara 	/*
47100250ec9SAndre Przywara 	 * though we ignore every other northbridge, we still have to
47200250ec9SAndre Przywara 	 * do the tweaking on _each_ node in MCM processors as the counters
47300250ec9SAndre Przywara 	 * are working hand-in-hand
47400250ec9SAndre Przywara 	 */
47500250ec9SAndre Przywara 	tweak_runavg_range(pdev);
47600250ec9SAndre Przywara 
477d83e92b3SHuang Rui 	if (!should_load_on_this_node(pdev))
47887432a2eSGuenter Roeck 		return -ENODEV;
479512d1027SAndreas Herrmann 
48087432a2eSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
48187432a2eSGuenter Roeck 	if (!data)
48287432a2eSGuenter Roeck 		return -ENOMEM;
48387432a2eSGuenter Roeck 
4847deb14b1SHuang Rui 	ret = fam15h_power_init_data(pdev, data);
4857deb14b1SHuang Rui 	if (ret)
4867deb14b1SHuang Rui 		return ret;
4877deb14b1SHuang Rui 
488562dc973SAxel Lin 	data->pdev = pdev;
489512d1027SAndreas Herrmann 
4907deb14b1SHuang Rui 	data->groups[0] = &data->group;
4917deb14b1SHuang Rui 
492562dc973SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
493562dc973SAxel Lin 							   data,
4947deb14b1SHuang Rui 							   &data->groups[0]);
495562dc973SAxel Lin 	return PTR_ERR_OR_ZERO(hwmon_dev);
496512d1027SAndreas Herrmann }
497512d1027SAndreas Herrmann 
498cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = {
499512d1027SAndreas Herrmann 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
5000a0039adSAravind Gopalakrishnan 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
5015dc08725SHuang Rui 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
502eff2a945SHuang Rui 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) },
50322e32f4fSBoris Ostrovsky 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
5040bd52941SAravind Gopalakrishnan 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
505512d1027SAndreas Herrmann 	{}
506512d1027SAndreas Herrmann };
507512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
508512d1027SAndreas Herrmann 
509512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = {
510512d1027SAndreas Herrmann 	.name = "fam15h_power",
511512d1027SAndreas Herrmann 	.id_table = fam15h_power_id_table,
512512d1027SAndreas Herrmann 	.probe = fam15h_power_probe,
5135f0ecb90SAndreas Herrmann 	.resume = fam15h_power_resume,
514512d1027SAndreas Herrmann };
515512d1027SAndreas Herrmann 
516f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver);
517