1*6e7c1094SThomas 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 44fa794344SHuang Rui #define MSR_F15H_CU_PWR_ACCUMULATOR 0xc001007a 453b5ea47dSHuang Rui #define MSR_F15H_CU_MAX_PWR_ACCUMULATOR 0xc001007b 46cdb9e110SHuang Rui #define MSR_F15H_PTSC 0xc0010280 473b5ea47dSHuang Rui 48eff2a945SHuang Rui #define PCI_DEVICE_ID_AMD_15H_M70H_NB_F4 0x15b4 49eff2a945SHuang Rui 50512d1027SAndreas Herrmann struct fam15h_power_data { 51562dc973SAxel Lin struct pci_dev *pdev; 52512d1027SAndreas Herrmann unsigned int tdp_to_watts; 53512d1027SAndreas Herrmann unsigned int base_tdp; 54512d1027SAndreas Herrmann unsigned int processor_pwr_watts; 551ed32160SHuang Rui unsigned int cpu_pwr_sample_ratio; 567deb14b1SHuang Rui const struct attribute_group *groups[FAM15H_NUM_GROUPS]; 577deb14b1SHuang Rui struct attribute_group group; 583b5ea47dSHuang Rui /* maximum accumulated power of a compute unit */ 593b5ea47dSHuang Rui u64 max_cu_acc_power; 60fa794344SHuang Rui /* accumulated power of the compute units */ 61fa794344SHuang Rui u64 cu_acc_power[MAX_CUS]; 62cdb9e110SHuang Rui /* performance timestamp counter */ 63cdb9e110SHuang Rui u64 cpu_sw_pwr_ptsc[MAX_CUS]; 6411bf0d78SHuang Rui /* online/offline status of current compute unit */ 6511bf0d78SHuang Rui int cu_on[MAX_CUS]; 6611bf0d78SHuang Rui unsigned long power_period; 67512d1027SAndreas Herrmann }; 68512d1027SAndreas Herrmann 691d28e016SHuang Rui static bool is_carrizo_or_later(void) 701d28e016SHuang Rui { 711d28e016SHuang Rui return boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60; 721d28e016SHuang Rui } 731d28e016SHuang Rui 74d013f7f5SJulia Lawall static ssize_t power1_input_show(struct device *dev, 75512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 76512d1027SAndreas Herrmann { 77512d1027SAndreas Herrmann u32 val, tdp_limit, running_avg_range; 78512d1027SAndreas Herrmann s32 running_avg_capture; 79512d1027SAndreas Herrmann u64 curr_pwr_watts; 80512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 81562dc973SAxel Lin struct pci_dev *f4 = data->pdev; 82512d1027SAndreas Herrmann 83512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 84512d1027SAndreas Herrmann REG_TDP_RUNNING_AVERAGE, &val); 85e9cd4d55SHuang Rui 86e9cd4d55SHuang Rui /* 87e9cd4d55SHuang Rui * On Carrizo and later platforms, TdpRunAvgAccCap bit field 88e9cd4d55SHuang Rui * is extended to 4:31 from 4:25. 89e9cd4d55SHuang Rui */ 901d28e016SHuang Rui if (is_carrizo_or_later()) { 91e9cd4d55SHuang Rui running_avg_capture = val >> 4; 92e9cd4d55SHuang Rui running_avg_capture = sign_extend32(running_avg_capture, 27); 93e9cd4d55SHuang Rui } else { 94512d1027SAndreas Herrmann running_avg_capture = (val >> 4) & 0x3fffff; 95fc0900cbSAndreas Herrmann running_avg_capture = sign_extend32(running_avg_capture, 21); 96e9cd4d55SHuang Rui } 97e9cd4d55SHuang Rui 98941a956bSAndre Przywara running_avg_range = (val & 0xf) + 1; 99512d1027SAndreas Herrmann 100512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 101512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 102512d1027SAndreas Herrmann 10360dee3caSGioh Kim /* 10460dee3caSGioh Kim * On Carrizo and later platforms, ApmTdpLimit bit field 10560dee3caSGioh Kim * is extended to 16:31 from 16:28. 10660dee3caSGioh Kim */ 1071d28e016SHuang Rui if (is_carrizo_or_later()) 108512d1027SAndreas Herrmann tdp_limit = val >> 16; 10960dee3caSGioh Kim else 11060dee3caSGioh Kim tdp_limit = (val >> 16) & 0x1fff; 11160dee3caSGioh Kim 11262867d49SGuenter Roeck curr_pwr_watts = ((u64)(tdp_limit + 11362867d49SGuenter Roeck data->base_tdp)) << running_avg_range; 114941a956bSAndre Przywara curr_pwr_watts -= running_avg_capture; 115512d1027SAndreas Herrmann curr_pwr_watts *= data->tdp_to_watts; 116512d1027SAndreas Herrmann 117512d1027SAndreas Herrmann /* 118512d1027SAndreas Herrmann * Convert to microWatt 119512d1027SAndreas Herrmann * 120512d1027SAndreas Herrmann * power is in Watt provided as fixed point integer with 121512d1027SAndreas Herrmann * scaling factor 1/(2^16). For conversion we use 122512d1027SAndreas Herrmann * (10^6)/(2^16) = 15625/(2^10) 123512d1027SAndreas Herrmann */ 124941a956bSAndre Przywara curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range); 125512d1027SAndreas Herrmann return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts); 126512d1027SAndreas Herrmann } 127d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_input); 128512d1027SAndreas Herrmann 129d013f7f5SJulia Lawall static ssize_t power1_crit_show(struct device *dev, 130512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 131512d1027SAndreas Herrmann { 132512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 133512d1027SAndreas Herrmann 134512d1027SAndreas Herrmann return sprintf(buf, "%u\n", data->processor_pwr_watts); 135512d1027SAndreas Herrmann } 136d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_crit); 137512d1027SAndreas Herrmann 138fa794344SHuang Rui static void do_read_registers_on_cu(void *_data) 139fa794344SHuang Rui { 140fa794344SHuang Rui struct fam15h_power_data *data = _data; 141fa794344SHuang Rui int cpu, cu; 142fa794344SHuang Rui 143fa794344SHuang Rui cpu = smp_processor_id(); 144fa794344SHuang Rui 145fa794344SHuang Rui /* 146fa794344SHuang Rui * With the new x86 topology modelling, cpu core id actually 147fa794344SHuang Rui * is compute unit id. 148fa794344SHuang Rui */ 149fa794344SHuang Rui cu = cpu_data(cpu).cpu_core_id; 150fa794344SHuang Rui 151fa794344SHuang Rui rdmsrl_safe(MSR_F15H_CU_PWR_ACCUMULATOR, &data->cu_acc_power[cu]); 152cdb9e110SHuang Rui rdmsrl_safe(MSR_F15H_PTSC, &data->cpu_sw_pwr_ptsc[cu]); 15311bf0d78SHuang Rui 15411bf0d78SHuang Rui data->cu_on[cu] = 1; 155fa794344SHuang Rui } 156fa794344SHuang Rui 157fa794344SHuang Rui /* 158fa794344SHuang Rui * This function is only able to be called when CPUID 159fa794344SHuang Rui * Fn8000_0007:EDX[12] is set. 160fa794344SHuang Rui */ 161fa794344SHuang Rui static int read_registers(struct fam15h_power_data *data) 162fa794344SHuang Rui { 163fa794344SHuang Rui int core, this_core; 164fa794344SHuang Rui cpumask_var_t mask; 1657be48818SBorislav Petkov int ret, cpu; 166fa794344SHuang Rui 167fa794344SHuang Rui ret = zalloc_cpumask_var(&mask, GFP_KERNEL); 168fa794344SHuang Rui if (!ret) 169fa794344SHuang Rui return -ENOMEM; 170fa794344SHuang Rui 17111bf0d78SHuang Rui memset(data->cu_on, 0, sizeof(int) * MAX_CUS); 17211bf0d78SHuang Rui 173fa794344SHuang Rui get_online_cpus(); 174fa794344SHuang Rui 175fa794344SHuang Rui /* 176fa794344SHuang Rui * Choose the first online core of each compute unit, and then 177fa794344SHuang Rui * read their MSR value of power and ptsc in a single IPI, 178fa794344SHuang Rui * because the MSR value of CPU core represent the compute 179fa794344SHuang Rui * unit's. 180fa794344SHuang Rui */ 181fa794344SHuang Rui core = -1; 182fa794344SHuang Rui 183fa794344SHuang Rui for_each_online_cpu(cpu) { 184fa794344SHuang Rui this_core = topology_core_id(cpu); 185fa794344SHuang Rui 186fa794344SHuang Rui if (this_core == core) 187fa794344SHuang Rui continue; 188fa794344SHuang Rui 189fa794344SHuang Rui core = this_core; 190fa794344SHuang Rui 191fa794344SHuang Rui /* get any CPU on this compute unit */ 192fa794344SHuang Rui cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask); 193fa794344SHuang Rui } 194fa794344SHuang Rui 1957be48818SBorislav Petkov on_each_cpu_mask(mask, do_read_registers_on_cu, data, true); 196fa794344SHuang Rui 197fa794344SHuang Rui put_online_cpus(); 198fa794344SHuang Rui free_cpumask_var(mask); 199fa794344SHuang Rui 200fa794344SHuang Rui return 0; 201fa794344SHuang Rui } 202fa794344SHuang Rui 203d013f7f5SJulia Lawall static ssize_t power1_average_show(struct device *dev, 204d013f7f5SJulia Lawall struct device_attribute *attr, char *buf) 20511bf0d78SHuang Rui { 20611bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 20711bf0d78SHuang Rui u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS], 20811bf0d78SHuang Rui jdelta[MAX_CUS]; 20911bf0d78SHuang Rui u64 tdelta, avg_acc; 21011bf0d78SHuang Rui int cu, cu_num, ret; 21111bf0d78SHuang Rui signed long leftover; 21211bf0d78SHuang Rui 21311bf0d78SHuang Rui /* 21411bf0d78SHuang Rui * With the new x86 topology modelling, x86_max_cores is the 21511bf0d78SHuang Rui * compute unit number. 21611bf0d78SHuang Rui */ 21711bf0d78SHuang Rui cu_num = boot_cpu_data.x86_max_cores; 21811bf0d78SHuang Rui 21911bf0d78SHuang Rui ret = read_registers(data); 22011bf0d78SHuang Rui if (ret) 22111bf0d78SHuang Rui return 0; 22211bf0d78SHuang Rui 22311bf0d78SHuang Rui for (cu = 0; cu < cu_num; cu++) { 22411bf0d78SHuang Rui prev_cu_acc_power[cu] = data->cu_acc_power[cu]; 22511bf0d78SHuang Rui prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu]; 22611bf0d78SHuang Rui } 22711bf0d78SHuang Rui 22811bf0d78SHuang Rui leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period)); 22911bf0d78SHuang Rui if (leftover) 23011bf0d78SHuang Rui return 0; 23111bf0d78SHuang Rui 23211bf0d78SHuang Rui ret = read_registers(data); 23311bf0d78SHuang Rui if (ret) 23411bf0d78SHuang Rui return 0; 23511bf0d78SHuang Rui 23611bf0d78SHuang Rui for (cu = 0, avg_acc = 0; cu < cu_num; cu++) { 23711bf0d78SHuang Rui /* check if current compute unit is online */ 23811bf0d78SHuang Rui if (data->cu_on[cu] == 0) 23911bf0d78SHuang Rui continue; 24011bf0d78SHuang Rui 24111bf0d78SHuang Rui if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) { 24211bf0d78SHuang Rui jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu]; 24311bf0d78SHuang Rui jdelta[cu] -= prev_cu_acc_power[cu]; 24411bf0d78SHuang Rui } else { 24511bf0d78SHuang Rui jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu]; 24611bf0d78SHuang Rui } 24711bf0d78SHuang Rui tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu]; 24811bf0d78SHuang Rui jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000; 24911bf0d78SHuang Rui do_div(jdelta[cu], tdelta); 25011bf0d78SHuang Rui 25111bf0d78SHuang Rui /* the unit is microWatt */ 25211bf0d78SHuang Rui avg_acc += jdelta[cu]; 25311bf0d78SHuang Rui } 25411bf0d78SHuang Rui 25511bf0d78SHuang Rui return sprintf(buf, "%llu\n", (unsigned long long)avg_acc); 25611bf0d78SHuang Rui } 257d013f7f5SJulia Lawall static DEVICE_ATTR_RO(power1_average); 25811bf0d78SHuang Rui 259d013f7f5SJulia Lawall static ssize_t power1_average_interval_show(struct device *dev, 26011bf0d78SHuang Rui struct device_attribute *attr, 26111bf0d78SHuang Rui char *buf) 26211bf0d78SHuang Rui { 26311bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 26411bf0d78SHuang Rui 26511bf0d78SHuang Rui return sprintf(buf, "%lu\n", data->power_period); 26611bf0d78SHuang Rui } 26711bf0d78SHuang Rui 268d013f7f5SJulia Lawall static ssize_t power1_average_interval_store(struct device *dev, 26911bf0d78SHuang Rui struct device_attribute *attr, 27011bf0d78SHuang Rui const char *buf, size_t count) 27111bf0d78SHuang Rui { 27211bf0d78SHuang Rui struct fam15h_power_data *data = dev_get_drvdata(dev); 27311bf0d78SHuang Rui unsigned long temp; 27411bf0d78SHuang Rui int ret; 27511bf0d78SHuang Rui 27611bf0d78SHuang Rui ret = kstrtoul(buf, 10, &temp); 27711bf0d78SHuang Rui if (ret) 27811bf0d78SHuang Rui return ret; 27911bf0d78SHuang Rui 28011bf0d78SHuang Rui if (temp > MAX_INTERVAL) 28111bf0d78SHuang Rui return -EINVAL; 28211bf0d78SHuang Rui 28311bf0d78SHuang Rui /* the interval value should be greater than 0 */ 28411bf0d78SHuang Rui if (temp <= 0) 28511bf0d78SHuang Rui return -EINVAL; 28611bf0d78SHuang Rui 28711bf0d78SHuang Rui data->power_period = temp; 28811bf0d78SHuang Rui 28911bf0d78SHuang Rui return count; 29011bf0d78SHuang Rui } 291d013f7f5SJulia Lawall static DEVICE_ATTR_RW(power1_average_interval); 29211bf0d78SHuang Rui 2937deb14b1SHuang Rui static int fam15h_power_init_attrs(struct pci_dev *pdev, 2947deb14b1SHuang Rui struct fam15h_power_data *data) 295961a2378SAravind Gopalakrishnan { 2967deb14b1SHuang Rui int n = FAM15H_MIN_NUM_ATTRS; 2977deb14b1SHuang Rui struct attribute **fam15h_power_attrs; 29846f29c2bSHuang Rui struct cpuinfo_x86 *c = &boot_cpu_data; 2997deb14b1SHuang Rui 30046f29c2bSHuang Rui if (c->x86 == 0x15 && 30146f29c2bSHuang Rui (c->x86_model <= 0xf || 302eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3037deb14b1SHuang Rui n += 1; 3047deb14b1SHuang Rui 30511bf0d78SHuang Rui /* check if processor supports accumulated power */ 30611bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) 30711bf0d78SHuang Rui n += 2; 30811bf0d78SHuang Rui 3097deb14b1SHuang Rui fam15h_power_attrs = devm_kcalloc(&pdev->dev, n, 3107deb14b1SHuang Rui sizeof(*fam15h_power_attrs), 3117deb14b1SHuang Rui GFP_KERNEL); 3127deb14b1SHuang Rui 3137deb14b1SHuang Rui if (!fam15h_power_attrs) 3147deb14b1SHuang Rui return -ENOMEM; 3157deb14b1SHuang Rui 3167deb14b1SHuang Rui n = 0; 3177deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr; 31846f29c2bSHuang Rui if (c->x86 == 0x15 && 31946f29c2bSHuang Rui (c->x86_model <= 0xf || 320eff2a945SHuang Rui (c->x86_model >= 0x60 && c->x86_model <= 0x7f))) 3217deb14b1SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_input.attr; 3227deb14b1SHuang Rui 32311bf0d78SHuang Rui if (boot_cpu_has(X86_FEATURE_ACC_POWER)) { 32411bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average.attr; 32511bf0d78SHuang Rui fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr; 32611bf0d78SHuang Rui } 32711bf0d78SHuang Rui 3287deb14b1SHuang Rui data->group.attrs = fam15h_power_attrs; 3297deb14b1SHuang Rui 330961a2378SAravind Gopalakrishnan return 0; 331961a2378SAravind Gopalakrishnan } 332961a2378SAravind Gopalakrishnan 333d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4) 334512d1027SAndreas Herrmann { 335512d1027SAndreas Herrmann u32 val; 336512d1027SAndreas Herrmann 337512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3), 338512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val); 339512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3)) 340512d1027SAndreas Herrmann return false; 341512d1027SAndreas Herrmann 342512d1027SAndreas Herrmann return true; 343512d1027SAndreas Herrmann } 344512d1027SAndreas Herrmann 34500250ec9SAndre Przywara /* 34600250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly 34700250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids 34800250ec9SAndre Przywara * counter saturations resulting in bogus power readings. 34900250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes. 35000250ec9SAndre Przywara */ 3515f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = { 352c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 353c3e40a99SGuenter Roeck { 0 } 354c3e40a99SGuenter Roeck }; 355c3e40a99SGuenter Roeck 3565f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev) 35700250ec9SAndre Przywara { 35800250ec9SAndre Przywara u32 val; 35900250ec9SAndre Przywara 36000250ec9SAndre Przywara /* 36100250ec9SAndre Przywara * let this quirk apply only to the current version of the 36200250ec9SAndre Przywara * northbridge, since future versions may change the behavior 36300250ec9SAndre Przywara */ 364c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev)) 36500250ec9SAndre Przywara return; 36600250ec9SAndre Przywara 36700250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus, 36800250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 36900250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val); 37000250ec9SAndre Przywara if ((val & 0xf) != 0xe) 37100250ec9SAndre Przywara return; 37200250ec9SAndre Przywara 37300250ec9SAndre Przywara val &= ~0xf; 37400250ec9SAndre Przywara val |= 0x9; 37500250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus, 37600250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 37700250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val); 37800250ec9SAndre Przywara } 37900250ec9SAndre Przywara 3805f0ecb90SAndreas Herrmann #ifdef CONFIG_PM 3815f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev) 3825f0ecb90SAndreas Herrmann { 3835f0ecb90SAndreas Herrmann tweak_runavg_range(pdev); 3845f0ecb90SAndreas Herrmann return 0; 3855f0ecb90SAndreas Herrmann } 3865f0ecb90SAndreas Herrmann #else 3875f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL 3885f0ecb90SAndreas Herrmann #endif 3895f0ecb90SAndreas Herrmann 3907deb14b1SHuang Rui static int fam15h_power_init_data(struct pci_dev *f4, 391512d1027SAndreas Herrmann struct fam15h_power_data *data) 392512d1027SAndreas Herrmann { 39311bf0d78SHuang Rui u32 val; 394512d1027SAndreas Herrmann u64 tmp; 3957deb14b1SHuang Rui int ret; 396512d1027SAndreas Herrmann 397512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val); 398512d1027SAndreas Herrmann data->base_tdp = val >> 16; 399512d1027SAndreas Herrmann tmp = val & 0xffff; 400512d1027SAndreas Herrmann 401512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 402512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 403512d1027SAndreas Herrmann 404512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f); 405512d1027SAndreas Herrmann tmp *= data->tdp_to_watts; 406512d1027SAndreas Herrmann 407512d1027SAndreas Herrmann /* result not allowed to be >= 256W */ 408512d1027SAndreas Herrmann if ((tmp >> 16) >= 256) 409b55f3757SGuenter Roeck dev_warn(&f4->dev, 410b55f3757SGuenter Roeck "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n", 411512d1027SAndreas Herrmann (unsigned int) (tmp >> 16)); 412512d1027SAndreas Herrmann 413512d1027SAndreas Herrmann /* convert to microWatt */ 414512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10; 4151ed32160SHuang Rui 4167deb14b1SHuang Rui ret = fam15h_power_init_attrs(f4, data); 4177deb14b1SHuang Rui if (ret) 4187deb14b1SHuang Rui return ret; 4197deb14b1SHuang Rui 4201ed32160SHuang Rui 4211ed32160SHuang Rui /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */ 42211bf0d78SHuang Rui if (!boot_cpu_has(X86_FEATURE_ACC_POWER)) 4237deb14b1SHuang Rui return 0; 4241ed32160SHuang Rui 4251ed32160SHuang Rui /* 4261ed32160SHuang Rui * determine the ratio of the compute unit power accumulator 4271ed32160SHuang Rui * sample period to the PTSC counter period by executing CPUID 4281ed32160SHuang Rui * Fn8000_0007:ECX 4291ed32160SHuang Rui */ 43011bf0d78SHuang Rui data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007); 4317deb14b1SHuang Rui 4323b5ea47dSHuang Rui if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) { 4333b5ea47dSHuang Rui pr_err("Failed to read max compute unit power accumulator MSR\n"); 4343b5ea47dSHuang Rui return -ENODEV; 4353b5ea47dSHuang Rui } 4363b5ea47dSHuang Rui 4373b5ea47dSHuang Rui data->max_cu_acc_power = tmp; 4383b5ea47dSHuang Rui 43911bf0d78SHuang Rui /* 44011bf0d78SHuang Rui * Milliseconds are a reasonable interval for the measurement. 44111bf0d78SHuang Rui * But it shouldn't set too long here, because several seconds 44211bf0d78SHuang Rui * would cause the read function to hang. So set default 44311bf0d78SHuang Rui * interval as 10 ms. 44411bf0d78SHuang Rui */ 44511bf0d78SHuang Rui data->power_period = 10; 44611bf0d78SHuang Rui 447fa794344SHuang Rui return read_registers(data); 448512d1027SAndreas Herrmann } 449512d1027SAndreas Herrmann 4506c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev, 451512d1027SAndreas Herrmann const struct pci_device_id *id) 452512d1027SAndreas Herrmann { 453512d1027SAndreas Herrmann struct fam15h_power_data *data; 45487432a2eSGuenter Roeck struct device *dev = &pdev->dev; 455562dc973SAxel Lin struct device *hwmon_dev; 4567deb14b1SHuang Rui int ret; 457512d1027SAndreas Herrmann 45800250ec9SAndre Przywara /* 45900250ec9SAndre Przywara * though we ignore every other northbridge, we still have to 46000250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters 46100250ec9SAndre Przywara * are working hand-in-hand 46200250ec9SAndre Przywara */ 46300250ec9SAndre Przywara tweak_runavg_range(pdev); 46400250ec9SAndre Przywara 465d83e92b3SHuang Rui if (!should_load_on_this_node(pdev)) 46687432a2eSGuenter Roeck return -ENODEV; 467512d1027SAndreas Herrmann 46887432a2eSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL); 46987432a2eSGuenter Roeck if (!data) 47087432a2eSGuenter Roeck return -ENOMEM; 47187432a2eSGuenter Roeck 4727deb14b1SHuang Rui ret = fam15h_power_init_data(pdev, data); 4737deb14b1SHuang Rui if (ret) 4747deb14b1SHuang Rui return ret; 4757deb14b1SHuang Rui 476562dc973SAxel Lin data->pdev = pdev; 477512d1027SAndreas Herrmann 4787deb14b1SHuang Rui data->groups[0] = &data->group; 4797deb14b1SHuang Rui 480562dc973SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power", 481562dc973SAxel Lin data, 4827deb14b1SHuang Rui &data->groups[0]); 483562dc973SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev); 484512d1027SAndreas Herrmann } 485512d1027SAndreas Herrmann 486cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = { 487512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 4880a0039adSAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) }, 4895dc08725SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) }, 490eff2a945SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) }, 49122e32f4fSBoris Ostrovsky { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) }, 4920bd52941SAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) }, 493512d1027SAndreas Herrmann {} 494512d1027SAndreas Herrmann }; 495512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table); 496512d1027SAndreas Herrmann 497512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = { 498512d1027SAndreas Herrmann .name = "fam15h_power", 499512d1027SAndreas Herrmann .id_table = fam15h_power_id_table, 500512d1027SAndreas Herrmann .probe = fam15h_power_probe, 5015f0ecb90SAndreas Herrmann .resume = fam15h_power_resume, 502512d1027SAndreas Herrmann }; 503512d1027SAndreas Herrmann 504f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver); 505