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 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 */ 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 } 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 core, this_core; 176fa794344SHuang Rui cpumask_var_t mask; 177*7be48818SBorislav 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 207*7be48818SBorislav 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 21511bf0d78SHuang Rui static ssize_t acc_show_power(struct device *dev, 21611bf0d78SHuang Rui struct device_attribute *attr, 21711bf0d78SHuang Rui char *buf) 21811bf0d78SHuang Rui { 21911bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 22011bf0d78SHuang Rui u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS], 22111bf0d78SHuang Rui jdelta[MAX_CUS]; 22211bf0d78SHuang Rui u64 tdelta, avg_acc; 22311bf0d78SHuang Rui int cu, cu_num, ret; 22411bf0d78SHuang Rui signed long leftover; 22511bf0d78SHuang Rui 22611bf0d78SHuang Rui /* 22711bf0d78SHuang Rui * With the new x86 topology modelling, x86_max_cores is the 22811bf0d78SHuang Rui * compute unit number. 22911bf0d78SHuang Rui */ 23011bf0d78SHuang Rui cu_num = boot_cpu_data.x86_max_cores; 23111bf0d78SHuang Rui 23211bf0d78SHuang Rui ret = read_registers(data); 23311bf0d78SHuang Rui if (ret) 23411bf0d78SHuang Rui return 0; 23511bf0d78SHuang Rui 23611bf0d78SHuang Rui for (cu = 0; cu < cu_num; cu++) { 23711bf0d78SHuang Rui prev_cu_acc_power[cu] = data->cu_acc_power[cu]; 23811bf0d78SHuang Rui prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu]; 23911bf0d78SHuang Rui } 24011bf0d78SHuang Rui 24111bf0d78SHuang Rui leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period)); 24211bf0d78SHuang Rui if (leftover) 24311bf0d78SHuang Rui return 0; 24411bf0d78SHuang Rui 24511bf0d78SHuang Rui ret = read_registers(data); 24611bf0d78SHuang Rui if (ret) 24711bf0d78SHuang Rui return 0; 24811bf0d78SHuang Rui 24911bf0d78SHuang Rui for (cu = 0, avg_acc = 0; cu < cu_num; cu++) { 25011bf0d78SHuang Rui /* check if current compute unit is online */ 25111bf0d78SHuang Rui if (data->cu_on[cu] == 0) 25211bf0d78SHuang Rui continue; 25311bf0d78SHuang Rui 25411bf0d78SHuang Rui if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) { 25511bf0d78SHuang Rui jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu]; 25611bf0d78SHuang Rui jdelta[cu] -= prev_cu_acc_power[cu]; 25711bf0d78SHuang Rui } else { 25811bf0d78SHuang Rui jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu]; 25911bf0d78SHuang Rui } 26011bf0d78SHuang Rui tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu]; 26111bf0d78SHuang Rui jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000; 26211bf0d78SHuang Rui do_div(jdelta[cu], tdelta); 26311bf0d78SHuang Rui 26411bf0d78SHuang Rui /* the unit is microWatt */ 26511bf0d78SHuang Rui avg_acc += jdelta[cu]; 26611bf0d78SHuang Rui } 26711bf0d78SHuang Rui 26811bf0d78SHuang Rui return sprintf(buf, "%llu\n", (unsigned long long)avg_acc); 26911bf0d78SHuang Rui } 27011bf0d78SHuang Rui static DEVICE_ATTR(power1_average, S_IRUGO, acc_show_power, NULL); 27111bf0d78SHuang Rui 27211bf0d78SHuang Rui static ssize_t acc_show_power_period(struct device *dev, 27311bf0d78SHuang Rui struct device_attribute *attr, 27411bf0d78SHuang Rui char *buf) 27511bf0d78SHuang Rui { 27611bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 27711bf0d78SHuang Rui 27811bf0d78SHuang Rui return sprintf(buf, "%lu\n", data->power_period); 27911bf0d78SHuang Rui } 28011bf0d78SHuang Rui 28111bf0d78SHuang Rui static ssize_t acc_set_power_period(struct device *dev, 28211bf0d78SHuang Rui struct device_attribute *attr, 28311bf0d78SHuang Rui const char *buf, size_t count) 28411bf0d78SHuang Rui { 28511bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 28611bf0d78SHuang Rui unsigned long temp; 28711bf0d78SHuang Rui int ret; 28811bf0d78SHuang Rui 28911bf0d78SHuang Rui ret = kstrtoul(buf, 10, &temp); 29011bf0d78SHuang Rui if (ret) 29111bf0d78SHuang Rui return ret; 29211bf0d78SHuang Rui 29311bf0d78SHuang Rui if (temp > MAX_INTERVAL) 29411bf0d78SHuang Rui return -EINVAL; 29511bf0d78SHuang Rui 29611bf0d78SHuang Rui /* the interval value should be greater than 0 */ 29711bf0d78SHuang Rui if (temp <= 0) 29811bf0d78SHuang Rui return -EINVAL; 29911bf0d78SHuang Rui 30011bf0d78SHuang Rui data->power_period = temp; 30111bf0d78SHuang Rui 30211bf0d78SHuang Rui return count; 30311bf0d78SHuang Rui } 30411bf0d78SHuang Rui static DEVICE_ATTR(power1_average_interval, S_IRUGO | S_IWUSR, 30511bf0d78SHuang Rui acc_show_power_period, acc_set_power_period); 30611bf0d78SHuang Rui 3077deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev, 3087deb14b1SHuang Rui struct fam15h_power_data *data) 309961a2378SAravind Gopalakrishnan { 3107deb14b1SHuang Rui int n = FAM15H_MIN_NUM_ATTRS; 3117deb14b1SHuang Rui struct attribute **fam15h_power_attrs; 31246f29c2bSHuang Rui struct cpuinfo_x86 *c = &boot_cpu_data; 3137deb14b1SHuang Rui 31446f29c2bSHuang Rui if (c->x86 == 0x15 && 31546f29c2bSHuang Rui (c->x86_model <= 0xf || 316eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3177deb14b1SHuang Rui n += 1; 3187deb14b1SHuang Rui 31911bf0d78SHuang Rui /* check if processor supports accumulated power */ 32011bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) 32111bf0d78SHuang Rui n += 2; 32211bf0d78SHuang Rui 3237deb14b1SHuang Rui fam15h_power_attrs = devm_kcalloc(&pdev->dev, n, 3247deb14b1SHuang Rui sizeof(*fam15h_power_attrs), 3257deb14b1SHuang Rui GFP_KERNEL); 3267deb14b1SHuang Rui 3277deb14b1SHuang Rui if (!fam15h_power_attrs) 3287deb14b1SHuang Rui return -ENOMEM; 3297deb14b1SHuang Rui 3307deb14b1SHuang Rui n = 0; 3317deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr; 33246f29c2bSHuang Rui if (c->x86 == 0x15 && 33346f29c2bSHuang Rui (c->x86_model <= 0xf || 334eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3357deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_input.attr; 3367deb14b1SHuang Rui 33711bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) { 33811bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average.attr; 33911bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr; 34011bf0d78SHuang Rui } 34111bf0d78SHuang Rui 3427deb14b1SHuang Rui data->group.attrs = fam15h_power_attrs; 3437deb14b1SHuang Rui 344961a2378SAravind Gopalakrishnan return 0; 345961a2378SAravind Gopalakrishnan } 346961a2378SAravind Gopalakrishnan 347d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4) 348512d1027SAndreas Herrmann { 349512d1027SAndreas Herrmann u32 val; 350512d1027SAndreas Herrmann 351512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3), 352512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val); 353512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3)) 354512d1027SAndreas Herrmann return false; 355512d1027SAndreas Herrmann 356512d1027SAndreas Herrmann return true; 357512d1027SAndreas Herrmann } 358512d1027SAndreas Herrmann 35900250ec9SAndre Przywara /* 36000250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly 36100250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids 36200250ec9SAndre Przywara * counter saturations resulting in bogus power readings. 36300250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes. 36400250ec9SAndre Przywara */ 3655f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = { 366c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 367c3e40a99SGuenter Roeck { 0 } 368c3e40a99SGuenter Roeck }; 369c3e40a99SGuenter Roeck 3705f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev) 37100250ec9SAndre Przywara { 37200250ec9SAndre Przywara u32 val; 37300250ec9SAndre Przywara 37400250ec9SAndre Przywara /* 37500250ec9SAndre Przywara * let this quirk apply only to the current version of the 37600250ec9SAndre Przywara * northbridge, since future versions may change the behavior 37700250ec9SAndre Przywara */ 378c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev)) 37900250ec9SAndre Przywara return; 38000250ec9SAndre Przywara 38100250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus, 38200250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 38300250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val); 38400250ec9SAndre Przywara if ((val & 0xf) != 0xe) 38500250ec9SAndre Przywara return; 38600250ec9SAndre Przywara 38700250ec9SAndre Przywara val &= ~0xf; 38800250ec9SAndre Przywara val |= 0x9; 38900250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus, 39000250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 39100250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val); 39200250ec9SAndre Przywara } 39300250ec9SAndre Przywara 3945f0ecb90SAndreas Herrmann #ifdef CONFIG_PM 3955f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev) 3965f0ecb90SAndreas Herrmann { 3975f0ecb90SAndreas Herrmann tweak_runavg_range(pdev); 3985f0ecb90SAndreas Herrmann return 0; 3995f0ecb90SAndreas Herrmann } 4005f0ecb90SAndreas Herrmann #else 4015f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL 4025f0ecb90SAndreas Herrmann #endif 4035f0ecb90SAndreas Herrmann 4047deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4, 405512d1027SAndreas Herrmann struct fam15h_power_data *data) 406512d1027SAndreas Herrmann { 40711bf0d78SHuang Rui u32 val; 408512d1027SAndreas Herrmann u64 tmp; 4097deb14b1SHuang Rui int ret; 410512d1027SAndreas Herrmann 411512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val); 412512d1027SAndreas Herrmann data->base_tdp = val >> 16; 413512d1027SAndreas Herrmann tmp = val & 0xffff; 414512d1027SAndreas Herrmann 415512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 416512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 417512d1027SAndreas Herrmann 418512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f); 419512d1027SAndreas Herrmann tmp *= data->tdp_to_watts; 420512d1027SAndreas Herrmann 421512d1027SAndreas Herrmann /* result not allowed to be >= 256W */ 422512d1027SAndreas Herrmann if ((tmp >> 16) >= 256) 423b55f3757SGuenter Roeck dev_warn(&f4->dev, 424b55f3757SGuenter Roeck "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n", 425512d1027SAndreas Herrmann (unsigned int) (tmp >> 16)); 426512d1027SAndreas Herrmann 427512d1027SAndreas Herrmann /* convert to microWatt */ 428512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10; 4291ed32160SHuang Rui 4307deb14b1SHuang Rui ret = fam15h_power_init_attrs(f4, data); 4317deb14b1SHuang Rui if (ret) 4327deb14b1SHuang Rui return ret; 4337deb14b1SHuang Rui 4341ed32160SHuang Rui 4351ed32160SHuang Rui /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */ 43611bf0d78SHuang Rui if (!boot_cpu_has(X86_FEATURE_ACC_POWER)) 4377deb14b1SHuang Rui return 0; 4381ed32160SHuang Rui 4391ed32160SHuang Rui /* 4401ed32160SHuang Rui * determine the ratio of the compute unit power accumulator 4411ed32160SHuang Rui * sample period to the PTSC counter period by executing CPUID 4421ed32160SHuang Rui * Fn8000_0007:ECX 4431ed32160SHuang Rui */ 44411bf0d78SHuang Rui data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007); 4457deb14b1SHuang Rui 4463b5ea47dSHuang Rui if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) { 4473b5ea47dSHuang Rui pr_err("Failed to read max compute unit power accumulator MSR\n"); 4483b5ea47dSHuang Rui return -ENODEV; 4493b5ea47dSHuang Rui } 4503b5ea47dSHuang Rui 4513b5ea47dSHuang Rui data->max_cu_acc_power = tmp; 4523b5ea47dSHuang Rui 45311bf0d78SHuang Rui /* 45411bf0d78SHuang Rui * Milliseconds are a reasonable interval for the measurement. 45511bf0d78SHuang Rui * But it shouldn't set too long here, because several seconds 45611bf0d78SHuang Rui * would cause the read function to hang. So set default 45711bf0d78SHuang Rui * interval as 10 ms. 45811bf0d78SHuang Rui */ 45911bf0d78SHuang Rui data->power_period = 10; 46011bf0d78SHuang Rui 461fa794344SHuang Rui return read_registers(data); 462512d1027SAndreas Herrmann } 463512d1027SAndreas Herrmann 4646c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev, 465512d1027SAndreas Herrmann const struct pci_device_id *id) 466512d1027SAndreas Herrmann { 467512d1027SAndreas Herrmann struct fam15h_power_data *data; 46887432a2eSGuenter Roeck struct device *dev = &pdev->dev; 469562dc973SAxel Lin struct device *hwmon_dev; 4707deb14b1SHuang Rui int ret; 471512d1027SAndreas Herrmann 47200250ec9SAndre Przywara /* 47300250ec9SAndre Przywara * though we ignore every other northbridge, we still have to 47400250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters 47500250ec9SAndre Przywara * are working hand-in-hand 47600250ec9SAndre Przywara */ 47700250ec9SAndre Przywara tweak_runavg_range(pdev); 47800250ec9SAndre Przywara 479d83e92b3SHuang Rui if (!should_load_on_this_node(pdev)) 48087432a2eSGuenter Roeck return -ENODEV; 481512d1027SAndreas Herrmann 48287432a2eSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL); 48387432a2eSGuenter Roeck if (!data) 48487432a2eSGuenter Roeck return -ENOMEM; 48587432a2eSGuenter Roeck 4867deb14b1SHuang Rui ret = fam15h_power_init_data(pdev, data); 4877deb14b1SHuang Rui if (ret) 4887deb14b1SHuang Rui return ret; 4897deb14b1SHuang Rui 490562dc973SAxel Lin data->pdev = pdev; 491512d1027SAndreas Herrmann 4927deb14b1SHuang Rui data->groups[0] = &data->group; 4937deb14b1SHuang Rui 494562dc973SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power", 495562dc973SAxel Lin data, 4967deb14b1SHuang Rui &data->groups[0]); 497562dc973SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev); 498512d1027SAndreas Herrmann } 499512d1027SAndreas Herrmann 500cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = { 501512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 5020a0039adSAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) }, 5035dc08725SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) }, 504eff2a945SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) }, 50522e32f4fSBoris Ostrovsky { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) }, 5060bd52941SAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) }, 507512d1027SAndreas Herrmann {} 508512d1027SAndreas Herrmann }; 509512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table); 510512d1027SAndreas Herrmann 511512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = { 512512d1027SAndreas Herrmann .name = "fam15h_power", 513512d1027SAndreas Herrmann .id_table = fam15h_power_id_table, 514512d1027SAndreas Herrmann .probe = fam15h_power_probe, 5155f0ecb90SAndreas Herrmann .resume = fam15h_power_resume, 516512d1027SAndreas Herrmann }; 517512d1027SAndreas Herrmann 518f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver); 519