16e7c1094SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2512d1027SAndreas Herrmann /*
3512d1027SAndreas Herrmann * fam15h_power.c - AMD Family 15h processor power monitoring
4512d1027SAndreas Herrmann *
5a6e232f7SHuang Rui * Copyright (c) 2011-2016 Advanced Micro Devices, Inc.
6d034fbf0SAndreas Herrmann * Author: Andreas Herrmann <herrmann.der.user@googlemail.com>
7512d1027SAndreas Herrmann */
8512d1027SAndreas Herrmann
9512d1027SAndreas Herrmann #include <linux/err.h>
10512d1027SAndreas Herrmann #include <linux/hwmon.h>
11512d1027SAndreas Herrmann #include <linux/hwmon-sysfs.h>
12512d1027SAndreas Herrmann #include <linux/init.h>
13512d1027SAndreas Herrmann #include <linux/module.h>
14512d1027SAndreas Herrmann #include <linux/pci.h>
15512d1027SAndreas Herrmann #include <linux/bitops.h>
16fa794344SHuang Rui #include <linux/cpu.h>
17fa794344SHuang Rui #include <linux/cpumask.h>
1811bf0d78SHuang Rui #include <linux/time.h>
1911bf0d78SHuang Rui #include <linux/sched.h>
20512d1027SAndreas Herrmann #include <asm/processor.h>
213b5ea47dSHuang Rui #include <asm/msr.h>
22512d1027SAndreas Herrmann
23512d1027SAndreas Herrmann MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
24d034fbf0SAndreas Herrmann MODULE_AUTHOR("Andreas Herrmann <herrmann.der.user@googlemail.com>");
25512d1027SAndreas Herrmann MODULE_LICENSE("GPL");
26512d1027SAndreas Herrmann
27512d1027SAndreas Herrmann /* D18F3 */
28512d1027SAndreas Herrmann #define REG_NORTHBRIDGE_CAP 0xe8
29512d1027SAndreas Herrmann
30512d1027SAndreas Herrmann /* D18F4 */
31512d1027SAndreas Herrmann #define REG_PROCESSOR_TDP 0x1b8
32512d1027SAndreas Herrmann
33512d1027SAndreas Herrmann /* D18F5 */
34512d1027SAndreas Herrmann #define REG_TDP_RUNNING_AVERAGE 0xe0
35512d1027SAndreas Herrmann #define REG_TDP_LIMIT3 0xe8
36512d1027SAndreas Herrmann
377deb14b1SHuang Rui #define FAM15H_MIN_NUM_ATTRS 2
387deb14b1SHuang Rui #define FAM15H_NUM_GROUPS 2
39fa794344SHuang Rui #define MAX_CUS 8
407deb14b1SHuang Rui
4111bf0d78SHuang Rui /* set maximum interval as 1 second */
4211bf0d78SHuang Rui #define MAX_INTERVAL 1000
4311bf0d78SHuang Rui
44eff2a945SHuang Rui #define PCI_DEVICE_ID_AMD_15H_M70H_NB_F4 0x15b4
45eff2a945SHuang Rui
46512d1027SAndreas Herrmann struct fam15h_power_data {
47562dc973SAxel Lin struct pci_dev *pdev;
48512d1027SAndreas Herrmann unsigned int tdp_to_watts;
49512d1027SAndreas Herrmann unsigned int base_tdp;
50512d1027SAndreas Herrmann unsigned int processor_pwr_watts;
511ed32160SHuang Rui unsigned int cpu_pwr_sample_ratio;
527deb14b1SHuang Rui const struct attribute_group *groups[FAM15H_NUM_GROUPS];
537deb14b1SHuang Rui struct attribute_group group;
543b5ea47dSHuang Rui /* maximum accumulated power of a compute unit */
553b5ea47dSHuang Rui u64 max_cu_acc_power;
56fa794344SHuang Rui /* accumulated power of the compute units */
57fa794344SHuang Rui u64 cu_acc_power[MAX_CUS];
58cdb9e110SHuang Rui /* performance timestamp counter */
59cdb9e110SHuang Rui u64 cpu_sw_pwr_ptsc[MAX_CUS];
6011bf0d78SHuang Rui /* online/offline status of current compute unit */
6111bf0d78SHuang Rui int cu_on[MAX_CUS];
6211bf0d78SHuang Rui unsigned long power_period;
63512d1027SAndreas Herrmann };
64512d1027SAndreas Herrmann
is_carrizo_or_later(void)651d28e016SHuang Rui static bool is_carrizo_or_later(void)
661d28e016SHuang Rui {
671d28e016SHuang Rui return boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60;
681d28e016SHuang Rui }
691d28e016SHuang Rui
power1_input_show(struct device * dev,struct device_attribute * attr,char * buf)70d013f7f5SJulia Lawall static ssize_t power1_input_show(struct device *dev,
71512d1027SAndreas Herrmann struct device_attribute *attr, char *buf)
72512d1027SAndreas Herrmann {
73512d1027SAndreas Herrmann u32 val, tdp_limit, running_avg_range;
74512d1027SAndreas Herrmann s32 running_avg_capture;
75512d1027SAndreas Herrmann u64 curr_pwr_watts;
76512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev);
77562dc973SAxel Lin struct pci_dev *f4 = data->pdev;
78512d1027SAndreas Herrmann
79512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
80512d1027SAndreas Herrmann REG_TDP_RUNNING_AVERAGE, &val);
81e9cd4d55SHuang Rui
82e9cd4d55SHuang Rui /*
83e9cd4d55SHuang Rui * On Carrizo and later platforms, TdpRunAvgAccCap bit field
84e9cd4d55SHuang Rui * is extended to 4:31 from 4:25.
85e9cd4d55SHuang Rui */
861d28e016SHuang Rui if (is_carrizo_or_later()) {
87e9cd4d55SHuang Rui running_avg_capture = val >> 4;
88e9cd4d55SHuang Rui running_avg_capture = sign_extend32(running_avg_capture, 27);
89e9cd4d55SHuang Rui } else {
90512d1027SAndreas Herrmann running_avg_capture = (val >> 4) & 0x3fffff;
91fc0900cbSAndreas Herrmann running_avg_capture = sign_extend32(running_avg_capture, 21);
92e9cd4d55SHuang Rui }
93e9cd4d55SHuang Rui
94941a956bSAndre Przywara running_avg_range = (val & 0xf) + 1;
95512d1027SAndreas Herrmann
96512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
97512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val);
98512d1027SAndreas Herrmann
9960dee3caSGioh Kim /*
10060dee3caSGioh Kim * On Carrizo and later platforms, ApmTdpLimit bit field
10160dee3caSGioh Kim * is extended to 16:31 from 16:28.
10260dee3caSGioh Kim */
1031d28e016SHuang Rui if (is_carrizo_or_later())
104512d1027SAndreas Herrmann tdp_limit = val >> 16;
10560dee3caSGioh Kim else
10660dee3caSGioh Kim tdp_limit = (val >> 16) & 0x1fff;
10760dee3caSGioh Kim
10862867d49SGuenter Roeck curr_pwr_watts = ((u64)(tdp_limit +
10962867d49SGuenter Roeck data->base_tdp)) << running_avg_range;
110941a956bSAndre Przywara curr_pwr_watts -= running_avg_capture;
111512d1027SAndreas Herrmann curr_pwr_watts *= data->tdp_to_watts;
112512d1027SAndreas Herrmann
113512d1027SAndreas Herrmann /*
114512d1027SAndreas Herrmann * Convert to microWatt
115512d1027SAndreas Herrmann *
116512d1027SAndreas Herrmann * power is in Watt provided as fixed point integer with
117512d1027SAndreas Herrmann * scaling factor 1/(2^16). For conversion we use
118512d1027SAndreas Herrmann * (10^6)/(2^16) = 15625/(2^10)
119512d1027SAndreas Herrmann */
120941a956bSAndre Przywara curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
121512d1027SAndreas Herrmann return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
122512d1027SAndreas Herrmann }
123d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_input);
124512d1027SAndreas Herrmann
power1_crit_show(struct device * dev,struct device_attribute * attr,char * buf)125d013f7f5SJulia Lawall static ssize_t power1_crit_show(struct device *dev,
126512d1027SAndreas Herrmann struct device_attribute *attr, char *buf)
127512d1027SAndreas Herrmann {
128512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev);
129512d1027SAndreas Herrmann
130512d1027SAndreas Herrmann return sprintf(buf, "%u\n", data->processor_pwr_watts);
131512d1027SAndreas Herrmann }
132d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_crit);
133512d1027SAndreas Herrmann
do_read_registers_on_cu(void * _data)134fa794344SHuang Rui static void do_read_registers_on_cu(void *_data)
135fa794344SHuang Rui {
136fa794344SHuang Rui struct fam15h_power_data *data = _data;
137fa794344SHuang Rui int cpu, cu;
138fa794344SHuang Rui
139fa794344SHuang Rui cpu = smp_processor_id();
140fa794344SHuang Rui
141fa794344SHuang Rui /*
142fa794344SHuang Rui * With the new x86 topology modelling, cpu core id actually
143fa794344SHuang Rui * is compute unit id.
144fa794344SHuang Rui */
145fa794344SHuang Rui cu = cpu_data(cpu).cpu_core_id;
146fa794344SHuang Rui
147fa794344SHuang Rui rdmsrl_safe(MSR_F15H_CU_PWR_ACCUMULATOR, &data->cu_acc_power[cu]);
148cdb9e110SHuang Rui rdmsrl_safe(MSR_F15H_PTSC, &data->cpu_sw_pwr_ptsc[cu]);
14911bf0d78SHuang Rui
15011bf0d78SHuang Rui data->cu_on[cu] = 1;
151fa794344SHuang Rui }
152fa794344SHuang Rui
153fa794344SHuang Rui /*
154fa794344SHuang Rui * This function is only able to be called when CPUID
155fa794344SHuang Rui * Fn8000_0007:EDX[12] is set.
156fa794344SHuang Rui */
read_registers(struct fam15h_power_data * data)157fa794344SHuang Rui static int read_registers(struct fam15h_power_data *data)
158fa794344SHuang Rui {
159fa794344SHuang Rui int core, this_core;
160fa794344SHuang Rui cpumask_var_t mask;
1617be48818SBorislav Petkov int ret, cpu;
162fa794344SHuang Rui
163fa794344SHuang Rui ret = zalloc_cpumask_var(&mask, GFP_KERNEL);
164fa794344SHuang Rui if (!ret)
165fa794344SHuang Rui return -ENOMEM;
166fa794344SHuang Rui
16711bf0d78SHuang Rui memset(data->cu_on, 0, sizeof(int) * MAX_CUS);
16811bf0d78SHuang Rui
169*e104d530SSebastian Andrzej Siewior cpus_read_lock();
170fa794344SHuang Rui
171fa794344SHuang Rui /*
172fa794344SHuang Rui * Choose the first online core of each compute unit, and then
173fa794344SHuang Rui * read their MSR value of power and ptsc in a single IPI,
174fa794344SHuang Rui * because the MSR value of CPU core represent the compute
175fa794344SHuang Rui * unit's.
176fa794344SHuang Rui */
177fa794344SHuang Rui core = -1;
178fa794344SHuang Rui
179fa794344SHuang Rui for_each_online_cpu(cpu) {
180fa794344SHuang Rui this_core = topology_core_id(cpu);
181fa794344SHuang Rui
182fa794344SHuang Rui if (this_core == core)
183fa794344SHuang Rui continue;
184fa794344SHuang Rui
185fa794344SHuang Rui core = this_core;
186fa794344SHuang Rui
187fa794344SHuang Rui /* get any CPU on this compute unit */
188fa794344SHuang Rui cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask);
189fa794344SHuang Rui }
190fa794344SHuang Rui
1917be48818SBorislav Petkov on_each_cpu_mask(mask, do_read_registers_on_cu, data, true);
192fa794344SHuang Rui
193*e104d530SSebastian Andrzej Siewior cpus_read_unlock();
194fa794344SHuang Rui free_cpumask_var(mask);
195fa794344SHuang Rui
196fa794344SHuang Rui return 0;
197fa794344SHuang Rui }
198fa794344SHuang Rui
power1_average_show(struct device * dev,struct device_attribute * attr,char * buf)199d013f7f5SJulia Lawall static ssize_t power1_average_show(struct device *dev,
200d013f7f5SJulia Lawall struct device_attribute *attr, char *buf)
20111bf0d78SHuang Rui {
20211bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev);
20311bf0d78SHuang Rui u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS],
20411bf0d78SHuang Rui jdelta[MAX_CUS];
20511bf0d78SHuang Rui u64 tdelta, avg_acc;
20611bf0d78SHuang Rui int cu, cu_num, ret;
20711bf0d78SHuang Rui signed long leftover;
20811bf0d78SHuang Rui
20911bf0d78SHuang Rui /*
21011bf0d78SHuang Rui * With the new x86 topology modelling, x86_max_cores is the
21111bf0d78SHuang Rui * compute unit number.
21211bf0d78SHuang Rui */
21311bf0d78SHuang Rui cu_num = boot_cpu_data.x86_max_cores;
21411bf0d78SHuang Rui
21511bf0d78SHuang Rui ret = read_registers(data);
21611bf0d78SHuang Rui if (ret)
21711bf0d78SHuang Rui return 0;
21811bf0d78SHuang Rui
21911bf0d78SHuang Rui for (cu = 0; cu < cu_num; cu++) {
22011bf0d78SHuang Rui prev_cu_acc_power[cu] = data->cu_acc_power[cu];
22111bf0d78SHuang Rui prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu];
22211bf0d78SHuang Rui }
22311bf0d78SHuang Rui
22411bf0d78SHuang Rui leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period));
22511bf0d78SHuang Rui if (leftover)
22611bf0d78SHuang Rui return 0;
22711bf0d78SHuang Rui
22811bf0d78SHuang Rui ret = read_registers(data);
22911bf0d78SHuang Rui if (ret)
23011bf0d78SHuang Rui return 0;
23111bf0d78SHuang Rui
23211bf0d78SHuang Rui for (cu = 0, avg_acc = 0; cu < cu_num; cu++) {
23311bf0d78SHuang Rui /* check if current compute unit is online */
23411bf0d78SHuang Rui if (data->cu_on[cu] == 0)
23511bf0d78SHuang Rui continue;
23611bf0d78SHuang Rui
23711bf0d78SHuang Rui if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) {
23811bf0d78SHuang Rui jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu];
23911bf0d78SHuang Rui jdelta[cu] -= prev_cu_acc_power[cu];
24011bf0d78SHuang Rui } else {
24111bf0d78SHuang Rui jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu];
24211bf0d78SHuang Rui }
24311bf0d78SHuang Rui tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
24411bf0d78SHuang Rui jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
24511bf0d78SHuang Rui do_div(jdelta[cu], tdelta);
24611bf0d78SHuang Rui
24711bf0d78SHuang Rui /* the unit is microWatt */
24811bf0d78SHuang Rui avg_acc += jdelta[cu];
24911bf0d78SHuang Rui }
25011bf0d78SHuang Rui
25111bf0d78SHuang Rui return sprintf(buf, "%llu\n", (unsigned long long)avg_acc);
25211bf0d78SHuang Rui }
253d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_average);
25411bf0d78SHuang Rui
power1_average_interval_show(struct device * dev,struct device_attribute * attr,char * buf)255d013f7f5SJulia Lawall static ssize_t power1_average_interval_show(struct device *dev,
25611bf0d78SHuang Rui struct device_attribute *attr,
25711bf0d78SHuang Rui char *buf)
25811bf0d78SHuang Rui {
25911bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev);
26011bf0d78SHuang Rui
26111bf0d78SHuang Rui return sprintf(buf, "%lu\n", data->power_period);
26211bf0d78SHuang Rui }
26311bf0d78SHuang Rui
power1_average_interval_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)264d013f7f5SJulia Lawall static ssize_t power1_average_interval_store(struct device *dev,
26511bf0d78SHuang Rui struct device_attribute *attr,
26611bf0d78SHuang Rui const char *buf, size_t count)
26711bf0d78SHuang Rui {
26811bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev);
26911bf0d78SHuang Rui unsigned long temp;
27011bf0d78SHuang Rui int ret;
27111bf0d78SHuang Rui
27211bf0d78SHuang Rui ret = kstrtoul(buf, 10, &temp);
27311bf0d78SHuang Rui if (ret)
27411bf0d78SHuang Rui return ret;
27511bf0d78SHuang Rui
27611bf0d78SHuang Rui if (temp > MAX_INTERVAL)
27711bf0d78SHuang Rui return -EINVAL;
27811bf0d78SHuang Rui
27911bf0d78SHuang Rui /* the interval value should be greater than 0 */
28011bf0d78SHuang Rui if (temp <= 0)
28111bf0d78SHuang Rui return -EINVAL;
28211bf0d78SHuang Rui
28311bf0d78SHuang Rui data->power_period = temp;
28411bf0d78SHuang Rui
28511bf0d78SHuang Rui return count;
28611bf0d78SHuang Rui }
287d013f7f5SJulia Lawall static DEVICE_ATTR_RW(power1_average_interval);
28811bf0d78SHuang Rui
fam15h_power_init_attrs(struct pci_dev * pdev,struct fam15h_power_data * data)2897deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev,
2907deb14b1SHuang Rui struct fam15h_power_data *data)
291961a2378SAravind Gopalakrishnan {
2927deb14b1SHuang Rui int n = FAM15H_MIN_NUM_ATTRS;
2937deb14b1SHuang Rui struct attribute **fam15h_power_attrs;
29446f29c2bSHuang Rui struct cpuinfo_x86 *c = &boot_cpu_data;
2957deb14b1SHuang Rui
29646f29c2bSHuang Rui if (c->x86 == 0x15 &&
29746f29c2bSHuang Rui (c->x86_model <= 0xf ||
298eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
2997deb14b1SHuang Rui n += 1;
3007deb14b1SHuang Rui
30111bf0d78SHuang Rui /* check if processor supports accumulated power */
30211bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER))
30311bf0d78SHuang Rui n += 2;
30411bf0d78SHuang Rui
3057deb14b1SHuang Rui fam15h_power_attrs = devm_kcalloc(&pdev->dev, n,
3067deb14b1SHuang Rui sizeof(*fam15h_power_attrs),
3077deb14b1SHuang Rui GFP_KERNEL);
3087deb14b1SHuang Rui
3097deb14b1SHuang Rui if (!fam15h_power_attrs)
3107deb14b1SHuang Rui return -ENOMEM;
3117deb14b1SHuang Rui
3127deb14b1SHuang Rui n = 0;
3137deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr;
31446f29c2bSHuang Rui if (c->x86 == 0x15 &&
31546f29c2bSHuang Rui (c->x86_model <= 0xf ||
316eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
3177deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_input.attr;
3187deb14b1SHuang Rui
31911bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) {
32011bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average.attr;
32111bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr;
32211bf0d78SHuang Rui }
32311bf0d78SHuang Rui
3247deb14b1SHuang Rui data->group.attrs = fam15h_power_attrs;
3257deb14b1SHuang Rui
326961a2378SAravind Gopalakrishnan return 0;
327961a2378SAravind Gopalakrishnan }
328961a2378SAravind Gopalakrishnan
should_load_on_this_node(struct pci_dev * f4)329d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4)
330512d1027SAndreas Herrmann {
331512d1027SAndreas Herrmann u32 val;
332512d1027SAndreas Herrmann
333512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
334512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val);
335512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3))
336512d1027SAndreas Herrmann return false;
337512d1027SAndreas Herrmann
338512d1027SAndreas Herrmann return true;
339512d1027SAndreas Herrmann }
340512d1027SAndreas Herrmann
34100250ec9SAndre Przywara /*
34200250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly
34300250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids
34400250ec9SAndre Przywara * counter saturations resulting in bogus power readings.
34500250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes.
34600250ec9SAndre Przywara */
3475f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = {
348c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
349c3e40a99SGuenter Roeck { 0 }
350c3e40a99SGuenter Roeck };
351c3e40a99SGuenter Roeck
tweak_runavg_range(struct pci_dev * pdev)3525f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev)
35300250ec9SAndre Przywara {
35400250ec9SAndre Przywara u32 val;
35500250ec9SAndre Przywara
35600250ec9SAndre Przywara /*
35700250ec9SAndre Przywara * let this quirk apply only to the current version of the
35800250ec9SAndre Przywara * northbridge, since future versions may change the behavior
35900250ec9SAndre Przywara */
360c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev))
36100250ec9SAndre Przywara return;
36200250ec9SAndre Przywara
36300250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus,
36400250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
36500250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val);
36600250ec9SAndre Przywara if ((val & 0xf) != 0xe)
36700250ec9SAndre Przywara return;
36800250ec9SAndre Przywara
36900250ec9SAndre Przywara val &= ~0xf;
37000250ec9SAndre Przywara val |= 0x9;
37100250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus,
37200250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
37300250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val);
37400250ec9SAndre Przywara }
37500250ec9SAndre Przywara
3765f0ecb90SAndreas Herrmann #ifdef CONFIG_PM
fam15h_power_resume(struct pci_dev * pdev)3775f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev)
3785f0ecb90SAndreas Herrmann {
3795f0ecb90SAndreas Herrmann tweak_runavg_range(pdev);
3805f0ecb90SAndreas Herrmann return 0;
3815f0ecb90SAndreas Herrmann }
3825f0ecb90SAndreas Herrmann #else
3835f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL
3845f0ecb90SAndreas Herrmann #endif
3855f0ecb90SAndreas Herrmann
fam15h_power_init_data(struct pci_dev * f4,struct fam15h_power_data * data)3867deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4,
387512d1027SAndreas Herrmann struct fam15h_power_data *data)
388512d1027SAndreas Herrmann {
38911bf0d78SHuang Rui u32 val;
390512d1027SAndreas Herrmann u64 tmp;
3917deb14b1SHuang Rui int ret;
392512d1027SAndreas Herrmann
393512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
394512d1027SAndreas Herrmann data->base_tdp = val >> 16;
395512d1027SAndreas Herrmann tmp = val & 0xffff;
396512d1027SAndreas Herrmann
397512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
398512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val);
399512d1027SAndreas Herrmann
400512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
401512d1027SAndreas Herrmann tmp *= data->tdp_to_watts;
402512d1027SAndreas Herrmann
403512d1027SAndreas Herrmann /* result not allowed to be >= 256W */
404512d1027SAndreas Herrmann if ((tmp >> 16) >= 256)
405b55f3757SGuenter Roeck dev_warn(&f4->dev,
406b55f3757SGuenter Roeck "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
407512d1027SAndreas Herrmann (unsigned int) (tmp >> 16));
408512d1027SAndreas Herrmann
409512d1027SAndreas Herrmann /* convert to microWatt */
410512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10;
4111ed32160SHuang Rui
4127deb14b1SHuang Rui ret = fam15h_power_init_attrs(f4, data);
4137deb14b1SHuang Rui if (ret)
4147deb14b1SHuang Rui return ret;
4157deb14b1SHuang Rui
4161ed32160SHuang Rui
4171ed32160SHuang Rui /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */
41811bf0d78SHuang Rui if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
4197deb14b1SHuang Rui return 0;
4201ed32160SHuang Rui
4211ed32160SHuang Rui /*
4221ed32160SHuang Rui * determine the ratio of the compute unit power accumulator
4231ed32160SHuang Rui * sample period to the PTSC counter period by executing CPUID
4241ed32160SHuang Rui * Fn8000_0007:ECX
4251ed32160SHuang Rui */
42611bf0d78SHuang Rui data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
4277deb14b1SHuang Rui
4283b5ea47dSHuang Rui if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
4293b5ea47dSHuang Rui pr_err("Failed to read max compute unit power accumulator MSR\n");
4303b5ea47dSHuang Rui return -ENODEV;
4313b5ea47dSHuang Rui }
4323b5ea47dSHuang Rui
4333b5ea47dSHuang Rui data->max_cu_acc_power = tmp;
4343b5ea47dSHuang Rui
43511bf0d78SHuang Rui /*
43611bf0d78SHuang Rui * Milliseconds are a reasonable interval for the measurement.
43711bf0d78SHuang Rui * But it shouldn't set too long here, because several seconds
43811bf0d78SHuang Rui * would cause the read function to hang. So set default
43911bf0d78SHuang Rui * interval as 10 ms.
44011bf0d78SHuang Rui */
44111bf0d78SHuang Rui data->power_period = 10;
44211bf0d78SHuang Rui
443fa794344SHuang Rui return read_registers(data);
444512d1027SAndreas Herrmann }
445512d1027SAndreas Herrmann
fam15h_power_probe(struct pci_dev * pdev,const struct pci_device_id * id)4466c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev,
447512d1027SAndreas Herrmann const struct pci_device_id *id)
448512d1027SAndreas Herrmann {
449512d1027SAndreas Herrmann struct fam15h_power_data *data;
45087432a2eSGuenter Roeck struct device *dev = &pdev->dev;
451562dc973SAxel Lin struct device *hwmon_dev;
4527deb14b1SHuang Rui int ret;
453512d1027SAndreas Herrmann
45400250ec9SAndre Przywara /*
45500250ec9SAndre Przywara * though we ignore every other northbridge, we still have to
45600250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters
45700250ec9SAndre Przywara * are working hand-in-hand
45800250ec9SAndre Przywara */
45900250ec9SAndre Przywara tweak_runavg_range(pdev);
46000250ec9SAndre Przywara
461d83e92b3SHuang Rui if (!should_load_on_this_node(pdev))
46287432a2eSGuenter Roeck return -ENODEV;
463512d1027SAndreas Herrmann
46487432a2eSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
46587432a2eSGuenter Roeck if (!data)
46687432a2eSGuenter Roeck return -ENOMEM;
46787432a2eSGuenter Roeck
4687deb14b1SHuang Rui ret = fam15h_power_init_data(pdev, data);
4697deb14b1SHuang Rui if (ret)
4707deb14b1SHuang Rui return ret;
4717deb14b1SHuang Rui
472562dc973SAxel Lin data->pdev = pdev;
473512d1027SAndreas Herrmann
4747deb14b1SHuang Rui data->groups[0] = &data->group;
4757deb14b1SHuang Rui
476562dc973SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
477562dc973SAxel Lin data,
4787deb14b1SHuang Rui &data->groups[0]);
479562dc973SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev);
480512d1027SAndreas Herrmann }
481512d1027SAndreas Herrmann
482cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = {
483512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
4840a0039adSAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
4855dc08725SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
486eff2a945SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) },
48722e32f4fSBoris Ostrovsky { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
4880bd52941SAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
489512d1027SAndreas Herrmann {}
490512d1027SAndreas Herrmann };
491512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
492512d1027SAndreas Herrmann
493512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = {
494512d1027SAndreas Herrmann .name = "fam15h_power",
495512d1027SAndreas Herrmann .id_table = fam15h_power_id_table,
496512d1027SAndreas Herrmann .probe = fam15h_power_probe,
4975f0ecb90SAndreas Herrmann .resume = fam15h_power_resume,
498512d1027SAndreas Herrmann };
499512d1027SAndreas Herrmann
500f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver);
501