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 81*1d28e016SHuang Rui static bool is_carrizo_or_later(void) 82*1d28e016SHuang Rui { 83*1d28e016SHuang Rui return boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60; 84*1d28e016SHuang Rui } 85*1d28e016SHuang Rui 86512d1027SAndreas Herrmann static ssize_t show_power(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 */ 102*1d28e016SHuang 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 */ 119*1d28e016SHuang 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 } 139512d1027SAndreas Herrmann static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL); 140512d1027SAndreas Herrmann 141512d1027SAndreas Herrmann static ssize_t show_power_crit(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 } 148512d1027SAndreas Herrmann static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL); 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 this_cpu, ret, cpu; 176fa794344SHuang Rui int core, this_core; 177fa794344SHuang Rui cpumask_var_t mask; 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 this_cpu = smp_processor_id(); 187fa794344SHuang Rui 188fa794344SHuang Rui /* 189fa794344SHuang Rui * Choose the first online core of each compute unit, and then 190fa794344SHuang Rui * read their MSR value of power and ptsc in a single IPI, 191fa794344SHuang Rui * because the MSR value of CPU core represent the compute 192fa794344SHuang Rui * unit's. 193fa794344SHuang Rui */ 194fa794344SHuang Rui core = -1; 195fa794344SHuang Rui 196fa794344SHuang Rui for_each_online_cpu(cpu) { 197fa794344SHuang Rui this_core = topology_core_id(cpu); 198fa794344SHuang Rui 199fa794344SHuang Rui if (this_core == core) 200fa794344SHuang Rui continue; 201fa794344SHuang Rui 202fa794344SHuang Rui core = this_core; 203fa794344SHuang Rui 204fa794344SHuang Rui /* get any CPU on this compute unit */ 205fa794344SHuang Rui cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask); 206fa794344SHuang Rui } 207fa794344SHuang Rui 208fa794344SHuang Rui if (cpumask_test_cpu(this_cpu, mask)) 209fa794344SHuang Rui do_read_registers_on_cu(data); 210fa794344SHuang Rui 211fa794344SHuang Rui smp_call_function_many(mask, do_read_registers_on_cu, data, true); 212fa794344SHuang Rui put_online_cpus(); 213fa794344SHuang Rui 214fa794344SHuang Rui free_cpumask_var(mask); 215fa794344SHuang Rui 216fa794344SHuang Rui return 0; 217fa794344SHuang Rui } 218fa794344SHuang Rui 21911bf0d78SHuang Rui static ssize_t acc_show_power(struct device *dev, 22011bf0d78SHuang Rui struct device_attribute *attr, 22111bf0d78SHuang Rui char *buf) 22211bf0d78SHuang Rui { 22311bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 22411bf0d78SHuang Rui u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS], 22511bf0d78SHuang Rui jdelta[MAX_CUS]; 22611bf0d78SHuang Rui u64 tdelta, avg_acc; 22711bf0d78SHuang Rui int cu, cu_num, ret; 22811bf0d78SHuang Rui signed long leftover; 22911bf0d78SHuang Rui 23011bf0d78SHuang Rui /* 23111bf0d78SHuang Rui * With the new x86 topology modelling, x86_max_cores is the 23211bf0d78SHuang Rui * compute unit number. 23311bf0d78SHuang Rui */ 23411bf0d78SHuang Rui cu_num = boot_cpu_data.x86_max_cores; 23511bf0d78SHuang Rui 23611bf0d78SHuang Rui ret = read_registers(data); 23711bf0d78SHuang Rui if (ret) 23811bf0d78SHuang Rui return 0; 23911bf0d78SHuang Rui 24011bf0d78SHuang Rui for (cu = 0; cu < cu_num; cu++) { 24111bf0d78SHuang Rui prev_cu_acc_power[cu] = data->cu_acc_power[cu]; 24211bf0d78SHuang Rui prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu]; 24311bf0d78SHuang Rui } 24411bf0d78SHuang Rui 24511bf0d78SHuang Rui leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period)); 24611bf0d78SHuang Rui if (leftover) 24711bf0d78SHuang Rui return 0; 24811bf0d78SHuang Rui 24911bf0d78SHuang Rui ret = read_registers(data); 25011bf0d78SHuang Rui if (ret) 25111bf0d78SHuang Rui return 0; 25211bf0d78SHuang Rui 25311bf0d78SHuang Rui for (cu = 0, avg_acc = 0; cu < cu_num; cu++) { 25411bf0d78SHuang Rui /* check if current compute unit is online */ 25511bf0d78SHuang Rui if (data->cu_on[cu] == 0) 25611bf0d78SHuang Rui continue; 25711bf0d78SHuang Rui 25811bf0d78SHuang Rui if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) { 25911bf0d78SHuang Rui jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu]; 26011bf0d78SHuang Rui jdelta[cu] -= prev_cu_acc_power[cu]; 26111bf0d78SHuang Rui } else { 26211bf0d78SHuang Rui jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu]; 26311bf0d78SHuang Rui } 26411bf0d78SHuang Rui tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu]; 26511bf0d78SHuang Rui jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000; 26611bf0d78SHuang Rui do_div(jdelta[cu], tdelta); 26711bf0d78SHuang Rui 26811bf0d78SHuang Rui /* the unit is microWatt */ 26911bf0d78SHuang Rui avg_acc += jdelta[cu]; 27011bf0d78SHuang Rui } 27111bf0d78SHuang Rui 27211bf0d78SHuang Rui return sprintf(buf, "%llu\n", (unsigned long long)avg_acc); 27311bf0d78SHuang Rui } 27411bf0d78SHuang Rui static DEVICE_ATTR(power1_average, S_IRUGO, acc_show_power, NULL); 27511bf0d78SHuang Rui 27611bf0d78SHuang Rui static ssize_t acc_show_power_period(struct device *dev, 27711bf0d78SHuang Rui struct device_attribute *attr, 27811bf0d78SHuang Rui char *buf) 27911bf0d78SHuang Rui { 28011bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 28111bf0d78SHuang Rui 28211bf0d78SHuang Rui return sprintf(buf, "%lu\n", data->power_period); 28311bf0d78SHuang Rui } 28411bf0d78SHuang Rui 28511bf0d78SHuang Rui static ssize_t acc_set_power_period(struct device *dev, 28611bf0d78SHuang Rui struct device_attribute *attr, 28711bf0d78SHuang Rui const char *buf, size_t count) 28811bf0d78SHuang Rui { 28911bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 29011bf0d78SHuang Rui unsigned long temp; 29111bf0d78SHuang Rui int ret; 29211bf0d78SHuang Rui 29311bf0d78SHuang Rui ret = kstrtoul(buf, 10, &temp); 29411bf0d78SHuang Rui if (ret) 29511bf0d78SHuang Rui return ret; 29611bf0d78SHuang Rui 29711bf0d78SHuang Rui if (temp > MAX_INTERVAL) 29811bf0d78SHuang Rui return -EINVAL; 29911bf0d78SHuang Rui 30011bf0d78SHuang Rui /* the interval value should be greater than 0 */ 30111bf0d78SHuang Rui if (temp <= 0) 30211bf0d78SHuang Rui return -EINVAL; 30311bf0d78SHuang Rui 30411bf0d78SHuang Rui data->power_period = temp; 30511bf0d78SHuang Rui 30611bf0d78SHuang Rui return count; 30711bf0d78SHuang Rui } 30811bf0d78SHuang Rui static DEVICE_ATTR(power1_average_interval, S_IRUGO | S_IWUSR, 30911bf0d78SHuang Rui acc_show_power_period, acc_set_power_period); 31011bf0d78SHuang Rui 3117deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev, 3127deb14b1SHuang Rui struct fam15h_power_data *data) 313961a2378SAravind Gopalakrishnan { 3147deb14b1SHuang Rui int n = FAM15H_MIN_NUM_ATTRS; 3157deb14b1SHuang Rui struct attribute **fam15h_power_attrs; 31646f29c2bSHuang Rui struct cpuinfo_x86 *c = &boot_cpu_data; 3177deb14b1SHuang Rui 31846f29c2bSHuang Rui if (c->x86 == 0x15 && 31946f29c2bSHuang Rui (c->x86_model <= 0xf || 320eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3217deb14b1SHuang Rui n += 1; 3227deb14b1SHuang Rui 32311bf0d78SHuang Rui /* check if processor supports accumulated power */ 32411bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) 32511bf0d78SHuang Rui n += 2; 32611bf0d78SHuang Rui 3277deb14b1SHuang Rui fam15h_power_attrs = devm_kcalloc(&pdev->dev, n, 3287deb14b1SHuang Rui sizeof(*fam15h_power_attrs), 3297deb14b1SHuang Rui GFP_KERNEL); 3307deb14b1SHuang Rui 3317deb14b1SHuang Rui if (!fam15h_power_attrs) 3327deb14b1SHuang Rui return -ENOMEM; 3337deb14b1SHuang Rui 3347deb14b1SHuang Rui n = 0; 3357deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr; 33646f29c2bSHuang Rui if (c->x86 == 0x15 && 33746f29c2bSHuang Rui (c->x86_model <= 0xf || 338eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3397deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_input.attr; 3407deb14b1SHuang Rui 34111bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) { 34211bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average.attr; 34311bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr; 34411bf0d78SHuang Rui } 34511bf0d78SHuang Rui 3467deb14b1SHuang Rui data->group.attrs = fam15h_power_attrs; 3477deb14b1SHuang Rui 348961a2378SAravind Gopalakrishnan return 0; 349961a2378SAravind Gopalakrishnan } 350961a2378SAravind Gopalakrishnan 351d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4) 352512d1027SAndreas Herrmann { 353512d1027SAndreas Herrmann u32 val; 354512d1027SAndreas Herrmann 355512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3), 356512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val); 357512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3)) 358512d1027SAndreas Herrmann return false; 359512d1027SAndreas Herrmann 360512d1027SAndreas Herrmann return true; 361512d1027SAndreas Herrmann } 362512d1027SAndreas Herrmann 36300250ec9SAndre Przywara /* 36400250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly 36500250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids 36600250ec9SAndre Przywara * counter saturations resulting in bogus power readings. 36700250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes. 36800250ec9SAndre Przywara */ 3695f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = { 370c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 371c3e40a99SGuenter Roeck { 0 } 372c3e40a99SGuenter Roeck }; 373c3e40a99SGuenter Roeck 3745f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev) 37500250ec9SAndre Przywara { 37600250ec9SAndre Przywara u32 val; 37700250ec9SAndre Przywara 37800250ec9SAndre Przywara /* 37900250ec9SAndre Przywara * let this quirk apply only to the current version of the 38000250ec9SAndre Przywara * northbridge, since future versions may change the behavior 38100250ec9SAndre Przywara */ 382c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev)) 38300250ec9SAndre Przywara return; 38400250ec9SAndre Przywara 38500250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus, 38600250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 38700250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val); 38800250ec9SAndre Przywara if ((val & 0xf) != 0xe) 38900250ec9SAndre Przywara return; 39000250ec9SAndre Przywara 39100250ec9SAndre Przywara val &= ~0xf; 39200250ec9SAndre Przywara val |= 0x9; 39300250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus, 39400250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 39500250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val); 39600250ec9SAndre Przywara } 39700250ec9SAndre Przywara 3985f0ecb90SAndreas Herrmann #ifdef CONFIG_PM 3995f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev) 4005f0ecb90SAndreas Herrmann { 4015f0ecb90SAndreas Herrmann tweak_runavg_range(pdev); 4025f0ecb90SAndreas Herrmann return 0; 4035f0ecb90SAndreas Herrmann } 4045f0ecb90SAndreas Herrmann #else 4055f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL 4065f0ecb90SAndreas Herrmann #endif 4075f0ecb90SAndreas Herrmann 4087deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4, 409512d1027SAndreas Herrmann struct fam15h_power_data *data) 410512d1027SAndreas Herrmann { 41111bf0d78SHuang Rui u32 val; 412512d1027SAndreas Herrmann u64 tmp; 4137deb14b1SHuang Rui int ret; 414512d1027SAndreas Herrmann 415512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val); 416512d1027SAndreas Herrmann data->base_tdp = val >> 16; 417512d1027SAndreas Herrmann tmp = val & 0xffff; 418512d1027SAndreas Herrmann 419512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 420512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 421512d1027SAndreas Herrmann 422512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f); 423512d1027SAndreas Herrmann tmp *= data->tdp_to_watts; 424512d1027SAndreas Herrmann 425512d1027SAndreas Herrmann /* result not allowed to be >= 256W */ 426512d1027SAndreas Herrmann if ((tmp >> 16) >= 256) 427b55f3757SGuenter Roeck dev_warn(&f4->dev, 428b55f3757SGuenter Roeck "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n", 429512d1027SAndreas Herrmann (unsigned int) (tmp >> 16)); 430512d1027SAndreas Herrmann 431512d1027SAndreas Herrmann /* convert to microWatt */ 432512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10; 4331ed32160SHuang Rui 4347deb14b1SHuang Rui ret = fam15h_power_init_attrs(f4, data); 4357deb14b1SHuang Rui if (ret) 4367deb14b1SHuang Rui return ret; 4377deb14b1SHuang Rui 4381ed32160SHuang Rui 4391ed32160SHuang Rui /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */ 44011bf0d78SHuang Rui if (!boot_cpu_has(X86_FEATURE_ACC_POWER)) 4417deb14b1SHuang Rui return 0; 4421ed32160SHuang Rui 4431ed32160SHuang Rui /* 4441ed32160SHuang Rui * determine the ratio of the compute unit power accumulator 4451ed32160SHuang Rui * sample period to the PTSC counter period by executing CPUID 4461ed32160SHuang Rui * Fn8000_0007:ECX 4471ed32160SHuang Rui */ 44811bf0d78SHuang Rui data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007); 4497deb14b1SHuang Rui 4503b5ea47dSHuang Rui if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) { 4513b5ea47dSHuang Rui pr_err("Failed to read max compute unit power accumulator MSR\n"); 4523b5ea47dSHuang Rui return -ENODEV; 4533b5ea47dSHuang Rui } 4543b5ea47dSHuang Rui 4553b5ea47dSHuang Rui data->max_cu_acc_power = tmp; 4563b5ea47dSHuang Rui 45711bf0d78SHuang Rui /* 45811bf0d78SHuang Rui * Milliseconds are a reasonable interval for the measurement. 45911bf0d78SHuang Rui * But it shouldn't set too long here, because several seconds 46011bf0d78SHuang Rui * would cause the read function to hang. So set default 46111bf0d78SHuang Rui * interval as 10 ms. 46211bf0d78SHuang Rui */ 46311bf0d78SHuang Rui data->power_period = 10; 46411bf0d78SHuang Rui 465fa794344SHuang Rui return read_registers(data); 466512d1027SAndreas Herrmann } 467512d1027SAndreas Herrmann 4686c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev, 469512d1027SAndreas Herrmann const struct pci_device_id *id) 470512d1027SAndreas Herrmann { 471512d1027SAndreas Herrmann struct fam15h_power_data *data; 47287432a2eSGuenter Roeck struct device *dev = &pdev->dev; 473562dc973SAxel Lin struct device *hwmon_dev; 4747deb14b1SHuang Rui int ret; 475512d1027SAndreas Herrmann 47600250ec9SAndre Przywara /* 47700250ec9SAndre Przywara * though we ignore every other northbridge, we still have to 47800250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters 47900250ec9SAndre Przywara * are working hand-in-hand 48000250ec9SAndre Przywara */ 48100250ec9SAndre Przywara tweak_runavg_range(pdev); 48200250ec9SAndre Przywara 483d83e92b3SHuang Rui if (!should_load_on_this_node(pdev)) 48487432a2eSGuenter Roeck return -ENODEV; 485512d1027SAndreas Herrmann 48687432a2eSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL); 48787432a2eSGuenter Roeck if (!data) 48887432a2eSGuenter Roeck return -ENOMEM; 48987432a2eSGuenter Roeck 4907deb14b1SHuang Rui ret = fam15h_power_init_data(pdev, data); 4917deb14b1SHuang Rui if (ret) 4927deb14b1SHuang Rui return ret; 4937deb14b1SHuang Rui 494562dc973SAxel Lin data->pdev = pdev; 495512d1027SAndreas Herrmann 4967deb14b1SHuang Rui data->groups[0] = &data->group; 4977deb14b1SHuang Rui 498562dc973SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power", 499562dc973SAxel Lin data, 5007deb14b1SHuang Rui &data->groups[0]); 501562dc973SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev); 502512d1027SAndreas Herrmann } 503512d1027SAndreas Herrmann 504cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = { 505512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 5060a0039adSAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) }, 5075dc08725SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) }, 508eff2a945SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) }, 50922e32f4fSBoris Ostrovsky { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) }, 5100bd52941SAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) }, 511512d1027SAndreas Herrmann {} 512512d1027SAndreas Herrmann }; 513512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table); 514512d1027SAndreas Herrmann 515512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = { 516512d1027SAndreas Herrmann .name = "fam15h_power", 517512d1027SAndreas Herrmann .id_table = fam15h_power_id_table, 518512d1027SAndreas Herrmann .probe = fam15h_power_probe, 5195f0ecb90SAndreas Herrmann .resume = fam15h_power_resume, 520512d1027SAndreas Herrmann }; 521512d1027SAndreas Herrmann 522f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver); 523