1512d1027SAndreas Herrmann /* 2512d1027SAndreas Herrmann * fam15h_power.c - AMD Family 15h processor power monitoring 3512d1027SAndreas Herrmann * 4*a6e232f7SHuang 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 81512d1027SAndreas Herrmann static ssize_t show_power(struct device *dev, 82512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 83512d1027SAndreas Herrmann { 84512d1027SAndreas Herrmann u32 val, tdp_limit, running_avg_range; 85512d1027SAndreas Herrmann s32 running_avg_capture; 86512d1027SAndreas Herrmann u64 curr_pwr_watts; 87512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 88562dc973SAxel Lin struct pci_dev *f4 = data->pdev; 89512d1027SAndreas Herrmann 90512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 91512d1027SAndreas Herrmann REG_TDP_RUNNING_AVERAGE, &val); 92e9cd4d55SHuang Rui 93e9cd4d55SHuang Rui /* 94e9cd4d55SHuang Rui * On Carrizo and later platforms, TdpRunAvgAccCap bit field 95e9cd4d55SHuang Rui * is extended to 4:31 from 4:25. 96e9cd4d55SHuang Rui */ 97e9cd4d55SHuang Rui if (boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60) { 98e9cd4d55SHuang Rui running_avg_capture = val >> 4; 99e9cd4d55SHuang Rui running_avg_capture = sign_extend32(running_avg_capture, 27); 100e9cd4d55SHuang Rui } else { 101512d1027SAndreas Herrmann running_avg_capture = (val >> 4) & 0x3fffff; 102fc0900cbSAndreas Herrmann running_avg_capture = sign_extend32(running_avg_capture, 21); 103e9cd4d55SHuang Rui } 104e9cd4d55SHuang Rui 105941a956bSAndre Przywara running_avg_range = (val & 0xf) + 1; 106512d1027SAndreas Herrmann 107512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 108512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 109512d1027SAndreas Herrmann 11060dee3caSGioh Kim /* 11160dee3caSGioh Kim * On Carrizo and later platforms, ApmTdpLimit bit field 11260dee3caSGioh Kim * is extended to 16:31 from 16:28. 11360dee3caSGioh Kim */ 11460dee3caSGioh Kim if (boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60) 115512d1027SAndreas Herrmann tdp_limit = val >> 16; 11660dee3caSGioh Kim else 11760dee3caSGioh Kim tdp_limit = (val >> 16) & 0x1fff; 11860dee3caSGioh Kim 11962867d49SGuenter Roeck curr_pwr_watts = ((u64)(tdp_limit + 12062867d49SGuenter Roeck data->base_tdp)) << running_avg_range; 121941a956bSAndre Przywara curr_pwr_watts -= running_avg_capture; 122512d1027SAndreas Herrmann curr_pwr_watts *= data->tdp_to_watts; 123512d1027SAndreas Herrmann 124512d1027SAndreas Herrmann /* 125512d1027SAndreas Herrmann * Convert to microWatt 126512d1027SAndreas Herrmann * 127512d1027SAndreas Herrmann * power is in Watt provided as fixed point integer with 128512d1027SAndreas Herrmann * scaling factor 1/(2^16). For conversion we use 129512d1027SAndreas Herrmann * (10^6)/(2^16) = 15625/(2^10) 130512d1027SAndreas Herrmann */ 131941a956bSAndre Przywara curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range); 132512d1027SAndreas Herrmann return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts); 133512d1027SAndreas Herrmann } 134512d1027SAndreas Herrmann static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL); 135512d1027SAndreas Herrmann 136512d1027SAndreas Herrmann static ssize_t show_power_crit(struct device *dev, 137512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 138512d1027SAndreas Herrmann { 139512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 140512d1027SAndreas Herrmann 141512d1027SAndreas Herrmann return sprintf(buf, "%u\n", data->processor_pwr_watts); 142512d1027SAndreas Herrmann } 143512d1027SAndreas Herrmann static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL); 144512d1027SAndreas Herrmann 145fa794344SHuang Rui static void do_read_registers_on_cu(void *_data) 146fa794344SHuang Rui { 147fa794344SHuang Rui struct fam15h_power_data *data = _data; 148fa794344SHuang Rui int cpu, cu; 149fa794344SHuang Rui 150fa794344SHuang Rui cpu = smp_processor_id(); 151fa794344SHuang Rui 152fa794344SHuang Rui /* 153fa794344SHuang Rui * With the new x86 topology modelling, cpu core id actually 154fa794344SHuang Rui * is compute unit id. 155fa794344SHuang Rui */ 156fa794344SHuang Rui cu = cpu_data(cpu).cpu_core_id; 157fa794344SHuang Rui 158fa794344SHuang Rui rdmsrl_safe(MSR_F15H_CU_PWR_ACCUMULATOR, &data->cu_acc_power[cu]); 159cdb9e110SHuang Rui rdmsrl_safe(MSR_F15H_PTSC, &data->cpu_sw_pwr_ptsc[cu]); 16011bf0d78SHuang Rui 16111bf0d78SHuang Rui data->cu_on[cu] = 1; 162fa794344SHuang Rui } 163fa794344SHuang Rui 164fa794344SHuang Rui /* 165fa794344SHuang Rui * This function is only able to be called when CPUID 166fa794344SHuang Rui * Fn8000_0007:EDX[12] is set. 167fa794344SHuang Rui */ 168fa794344SHuang Rui static int read_registers(struct fam15h_power_data *data) 169fa794344SHuang Rui { 170fa794344SHuang Rui int this_cpu, ret, cpu; 171fa794344SHuang Rui int core, this_core; 172fa794344SHuang Rui cpumask_var_t mask; 173fa794344SHuang Rui 174fa794344SHuang Rui ret = zalloc_cpumask_var(&mask, GFP_KERNEL); 175fa794344SHuang Rui if (!ret) 176fa794344SHuang Rui return -ENOMEM; 177fa794344SHuang Rui 17811bf0d78SHuang Rui memset(data->cu_on, 0, sizeof(int) * MAX_CUS); 17911bf0d78SHuang Rui 180fa794344SHuang Rui get_online_cpus(); 181fa794344SHuang Rui this_cpu = smp_processor_id(); 182fa794344SHuang Rui 183fa794344SHuang Rui /* 184fa794344SHuang Rui * Choose the first online core of each compute unit, and then 185fa794344SHuang Rui * read their MSR value of power and ptsc in a single IPI, 186fa794344SHuang Rui * because the MSR value of CPU core represent the compute 187fa794344SHuang Rui * unit's. 188fa794344SHuang Rui */ 189fa794344SHuang Rui core = -1; 190fa794344SHuang Rui 191fa794344SHuang Rui for_each_online_cpu(cpu) { 192fa794344SHuang Rui this_core = topology_core_id(cpu); 193fa794344SHuang Rui 194fa794344SHuang Rui if (this_core == core) 195fa794344SHuang Rui continue; 196fa794344SHuang Rui 197fa794344SHuang Rui core = this_core; 198fa794344SHuang Rui 199fa794344SHuang Rui /* get any CPU on this compute unit */ 200fa794344SHuang Rui cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask); 201fa794344SHuang Rui } 202fa794344SHuang Rui 203fa794344SHuang Rui if (cpumask_test_cpu(this_cpu, mask)) 204fa794344SHuang Rui do_read_registers_on_cu(data); 205fa794344SHuang Rui 206fa794344SHuang Rui smp_call_function_many(mask, do_read_registers_on_cu, data, true); 207fa794344SHuang Rui put_online_cpus(); 208fa794344SHuang Rui 209fa794344SHuang Rui free_cpumask_var(mask); 210fa794344SHuang Rui 211fa794344SHuang Rui return 0; 212fa794344SHuang Rui } 213fa794344SHuang Rui 21411bf0d78SHuang Rui static ssize_t acc_show_power(struct device *dev, 21511bf0d78SHuang Rui struct device_attribute *attr, 21611bf0d78SHuang Rui 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 } 26911bf0d78SHuang Rui static DEVICE_ATTR(power1_average, S_IRUGO, acc_show_power, NULL); 27011bf0d78SHuang Rui 27111bf0d78SHuang Rui static ssize_t acc_show_power_period(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 28011bf0d78SHuang Rui static ssize_t acc_set_power_period(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 } 30311bf0d78SHuang Rui static DEVICE_ATTR(power1_average_interval, S_IRUGO | S_IWUSR, 30411bf0d78SHuang Rui acc_show_power_period, acc_set_power_period); 30511bf0d78SHuang Rui 3067deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev, 3077deb14b1SHuang Rui struct fam15h_power_data *data) 308961a2378SAravind Gopalakrishnan { 3097deb14b1SHuang Rui int n = FAM15H_MIN_NUM_ATTRS; 3107deb14b1SHuang Rui struct attribute **fam15h_power_attrs; 31146f29c2bSHuang Rui struct cpuinfo_x86 *c = &boot_cpu_data; 3127deb14b1SHuang Rui 31346f29c2bSHuang Rui if (c->x86 == 0x15 && 31446f29c2bSHuang Rui (c->x86_model <= 0xf || 315eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3167deb14b1SHuang Rui n += 1; 3177deb14b1SHuang Rui 31811bf0d78SHuang Rui /* check if processor supports accumulated power */ 31911bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) 32011bf0d78SHuang Rui n += 2; 32111bf0d78SHuang Rui 3227deb14b1SHuang Rui fam15h_power_attrs = devm_kcalloc(&pdev->dev, n, 3237deb14b1SHuang Rui sizeof(*fam15h_power_attrs), 3247deb14b1SHuang Rui GFP_KERNEL); 3257deb14b1SHuang Rui 3267deb14b1SHuang Rui if (!fam15h_power_attrs) 3277deb14b1SHuang Rui return -ENOMEM; 3287deb14b1SHuang Rui 3297deb14b1SHuang Rui n = 0; 3307deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr; 33146f29c2bSHuang Rui if (c->x86 == 0x15 && 33246f29c2bSHuang Rui (c->x86_model <= 0xf || 333eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3347deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_input.attr; 3357deb14b1SHuang Rui 33611bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) { 33711bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average.attr; 33811bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr; 33911bf0d78SHuang Rui } 34011bf0d78SHuang Rui 3417deb14b1SHuang Rui data->group.attrs = fam15h_power_attrs; 3427deb14b1SHuang Rui 343961a2378SAravind Gopalakrishnan return 0; 344961a2378SAravind Gopalakrishnan } 345961a2378SAravind Gopalakrishnan 346d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4) 347512d1027SAndreas Herrmann { 348512d1027SAndreas Herrmann u32 val; 349512d1027SAndreas Herrmann 350512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3), 351512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val); 352512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3)) 353512d1027SAndreas Herrmann return false; 354512d1027SAndreas Herrmann 355512d1027SAndreas Herrmann return true; 356512d1027SAndreas Herrmann } 357512d1027SAndreas Herrmann 35800250ec9SAndre Przywara /* 35900250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly 36000250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids 36100250ec9SAndre Przywara * counter saturations resulting in bogus power readings. 36200250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes. 36300250ec9SAndre Przywara */ 3645f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = { 365c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 366c3e40a99SGuenter Roeck { 0 } 367c3e40a99SGuenter Roeck }; 368c3e40a99SGuenter Roeck 3695f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev) 37000250ec9SAndre Przywara { 37100250ec9SAndre Przywara u32 val; 37200250ec9SAndre Przywara 37300250ec9SAndre Przywara /* 37400250ec9SAndre Przywara * let this quirk apply only to the current version of the 37500250ec9SAndre Przywara * northbridge, since future versions may change the behavior 37600250ec9SAndre Przywara */ 377c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev)) 37800250ec9SAndre Przywara return; 37900250ec9SAndre Przywara 38000250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus, 38100250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 38200250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val); 38300250ec9SAndre Przywara if ((val & 0xf) != 0xe) 38400250ec9SAndre Przywara return; 38500250ec9SAndre Przywara 38600250ec9SAndre Przywara val &= ~0xf; 38700250ec9SAndre Przywara val |= 0x9; 38800250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus, 38900250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 39000250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val); 39100250ec9SAndre Przywara } 39200250ec9SAndre Przywara 3935f0ecb90SAndreas Herrmann #ifdef CONFIG_PM 3945f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev) 3955f0ecb90SAndreas Herrmann { 3965f0ecb90SAndreas Herrmann tweak_runavg_range(pdev); 3975f0ecb90SAndreas Herrmann return 0; 3985f0ecb90SAndreas Herrmann } 3995f0ecb90SAndreas Herrmann #else 4005f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL 4015f0ecb90SAndreas Herrmann #endif 4025f0ecb90SAndreas Herrmann 4037deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4, 404512d1027SAndreas Herrmann struct fam15h_power_data *data) 405512d1027SAndreas Herrmann { 40611bf0d78SHuang Rui u32 val; 407512d1027SAndreas Herrmann u64 tmp; 4087deb14b1SHuang Rui int ret; 409512d1027SAndreas Herrmann 410512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val); 411512d1027SAndreas Herrmann data->base_tdp = val >> 16; 412512d1027SAndreas Herrmann tmp = val & 0xffff; 413512d1027SAndreas Herrmann 414512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 415512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 416512d1027SAndreas Herrmann 417512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f); 418512d1027SAndreas Herrmann tmp *= data->tdp_to_watts; 419512d1027SAndreas Herrmann 420512d1027SAndreas Herrmann /* result not allowed to be >= 256W */ 421512d1027SAndreas Herrmann if ((tmp >> 16) >= 256) 422b55f3757SGuenter Roeck dev_warn(&f4->dev, 423b55f3757SGuenter Roeck "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n", 424512d1027SAndreas Herrmann (unsigned int) (tmp >> 16)); 425512d1027SAndreas Herrmann 426512d1027SAndreas Herrmann /* convert to microWatt */ 427512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10; 4281ed32160SHuang Rui 4297deb14b1SHuang Rui ret = fam15h_power_init_attrs(f4, data); 4307deb14b1SHuang Rui if (ret) 4317deb14b1SHuang Rui return ret; 4327deb14b1SHuang Rui 4331ed32160SHuang Rui 4341ed32160SHuang Rui /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */ 43511bf0d78SHuang Rui if (!boot_cpu_has(X86_FEATURE_ACC_POWER)) 4367deb14b1SHuang Rui return 0; 4371ed32160SHuang Rui 4381ed32160SHuang Rui /* 4391ed32160SHuang Rui * determine the ratio of the compute unit power accumulator 4401ed32160SHuang Rui * sample period to the PTSC counter period by executing CPUID 4411ed32160SHuang Rui * Fn8000_0007:ECX 4421ed32160SHuang Rui */ 44311bf0d78SHuang Rui data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007); 4447deb14b1SHuang Rui 4453b5ea47dSHuang Rui if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) { 4463b5ea47dSHuang Rui pr_err("Failed to read max compute unit power accumulator MSR\n"); 4473b5ea47dSHuang Rui return -ENODEV; 4483b5ea47dSHuang Rui } 4493b5ea47dSHuang Rui 4503b5ea47dSHuang Rui data->max_cu_acc_power = tmp; 4513b5ea47dSHuang Rui 45211bf0d78SHuang Rui /* 45311bf0d78SHuang Rui * Milliseconds are a reasonable interval for the measurement. 45411bf0d78SHuang Rui * But it shouldn't set too long here, because several seconds 45511bf0d78SHuang Rui * would cause the read function to hang. So set default 45611bf0d78SHuang Rui * interval as 10 ms. 45711bf0d78SHuang Rui */ 45811bf0d78SHuang Rui data->power_period = 10; 45911bf0d78SHuang Rui 460fa794344SHuang Rui return read_registers(data); 461512d1027SAndreas Herrmann } 462512d1027SAndreas Herrmann 4636c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev, 464512d1027SAndreas Herrmann const struct pci_device_id *id) 465512d1027SAndreas Herrmann { 466512d1027SAndreas Herrmann struct fam15h_power_data *data; 46787432a2eSGuenter Roeck struct device *dev = &pdev->dev; 468562dc973SAxel Lin struct device *hwmon_dev; 4697deb14b1SHuang Rui int ret; 470512d1027SAndreas Herrmann 47100250ec9SAndre Przywara /* 47200250ec9SAndre Przywara * though we ignore every other northbridge, we still have to 47300250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters 47400250ec9SAndre Przywara * are working hand-in-hand 47500250ec9SAndre Przywara */ 47600250ec9SAndre Przywara tweak_runavg_range(pdev); 47700250ec9SAndre Przywara 478d83e92b3SHuang Rui if (!should_load_on_this_node(pdev)) 47987432a2eSGuenter Roeck return -ENODEV; 480512d1027SAndreas Herrmann 48187432a2eSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL); 48287432a2eSGuenter Roeck if (!data) 48387432a2eSGuenter Roeck return -ENOMEM; 48487432a2eSGuenter Roeck 4857deb14b1SHuang Rui ret = fam15h_power_init_data(pdev, data); 4867deb14b1SHuang Rui if (ret) 4877deb14b1SHuang Rui return ret; 4887deb14b1SHuang Rui 489562dc973SAxel Lin data->pdev = pdev; 490512d1027SAndreas Herrmann 4917deb14b1SHuang Rui data->groups[0] = &data->group; 4927deb14b1SHuang Rui 493562dc973SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power", 494562dc973SAxel Lin data, 4957deb14b1SHuang Rui &data->groups[0]); 496562dc973SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev); 497512d1027SAndreas Herrmann } 498512d1027SAndreas Herrmann 499cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = { 500512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 5010a0039adSAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) }, 5025dc08725SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) }, 503eff2a945SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) }, 50422e32f4fSBoris Ostrovsky { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) }, 5050bd52941SAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) }, 506512d1027SAndreas Herrmann {} 507512d1027SAndreas Herrmann }; 508512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table); 509512d1027SAndreas Herrmann 510512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = { 511512d1027SAndreas Herrmann .name = "fam15h_power", 512512d1027SAndreas Herrmann .id_table = fam15h_power_id_table, 513512d1027SAndreas Herrmann .probe = fam15h_power_probe, 5145f0ecb90SAndreas Herrmann .resume = fam15h_power_resume, 515512d1027SAndreas Herrmann }; 516512d1027SAndreas Herrmann 517f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver); 518