1512d1027SAndreas Herrmann /* 2512d1027SAndreas Herrmann * fam15h_power.c - AMD Family 15h processor power monitoring 3512d1027SAndreas Herrmann * 4512d1027SAndreas Herrmann * Copyright (c) 2011 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> 28512d1027SAndreas Herrmann #include <asm/processor.h> 29512d1027SAndreas Herrmann 30512d1027SAndreas Herrmann MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor"); 31d034fbf0SAndreas Herrmann MODULE_AUTHOR("Andreas Herrmann <herrmann.der.user@googlemail.com>"); 32512d1027SAndreas Herrmann MODULE_LICENSE("GPL"); 33512d1027SAndreas Herrmann 34512d1027SAndreas Herrmann /* D18F3 */ 35512d1027SAndreas Herrmann #define REG_NORTHBRIDGE_CAP 0xe8 36512d1027SAndreas Herrmann 37512d1027SAndreas Herrmann /* D18F4 */ 38512d1027SAndreas Herrmann #define REG_PROCESSOR_TDP 0x1b8 39512d1027SAndreas Herrmann 40512d1027SAndreas Herrmann /* D18F5 */ 41512d1027SAndreas Herrmann #define REG_TDP_RUNNING_AVERAGE 0xe0 42512d1027SAndreas Herrmann #define REG_TDP_LIMIT3 0xe8 43512d1027SAndreas Herrmann 44512d1027SAndreas Herrmann struct fam15h_power_data { 45562dc973SAxel Lin struct pci_dev *pdev; 46512d1027SAndreas Herrmann unsigned int tdp_to_watts; 47512d1027SAndreas Herrmann unsigned int base_tdp; 48512d1027SAndreas Herrmann unsigned int processor_pwr_watts; 49*1ed32160SHuang Rui unsigned int cpu_pwr_sample_ratio; 50512d1027SAndreas Herrmann }; 51512d1027SAndreas Herrmann 52512d1027SAndreas Herrmann static ssize_t show_power(struct device *dev, 53512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 54512d1027SAndreas Herrmann { 55512d1027SAndreas Herrmann u32 val, tdp_limit, running_avg_range; 56512d1027SAndreas Herrmann s32 running_avg_capture; 57512d1027SAndreas Herrmann u64 curr_pwr_watts; 58512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 59562dc973SAxel Lin struct pci_dev *f4 = data->pdev; 60512d1027SAndreas Herrmann 61512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 62512d1027SAndreas Herrmann REG_TDP_RUNNING_AVERAGE, &val); 63e9cd4d55SHuang Rui 64e9cd4d55SHuang Rui /* 65e9cd4d55SHuang Rui * On Carrizo and later platforms, TdpRunAvgAccCap bit field 66e9cd4d55SHuang Rui * is extended to 4:31 from 4:25. 67e9cd4d55SHuang Rui */ 68e9cd4d55SHuang Rui if (boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60) { 69e9cd4d55SHuang Rui running_avg_capture = val >> 4; 70e9cd4d55SHuang Rui running_avg_capture = sign_extend32(running_avg_capture, 27); 71e9cd4d55SHuang Rui } else { 72512d1027SAndreas Herrmann running_avg_capture = (val >> 4) & 0x3fffff; 73fc0900cbSAndreas Herrmann running_avg_capture = sign_extend32(running_avg_capture, 21); 74e9cd4d55SHuang Rui } 75e9cd4d55SHuang Rui 76941a956bSAndre Przywara running_avg_range = (val & 0xf) + 1; 77512d1027SAndreas Herrmann 78512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 79512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 80512d1027SAndreas Herrmann 81512d1027SAndreas Herrmann tdp_limit = val >> 16; 8262867d49SGuenter Roeck curr_pwr_watts = ((u64)(tdp_limit + 8362867d49SGuenter Roeck data->base_tdp)) << running_avg_range; 84941a956bSAndre Przywara curr_pwr_watts -= running_avg_capture; 85512d1027SAndreas Herrmann curr_pwr_watts *= data->tdp_to_watts; 86512d1027SAndreas Herrmann 87512d1027SAndreas Herrmann /* 88512d1027SAndreas Herrmann * Convert to microWatt 89512d1027SAndreas Herrmann * 90512d1027SAndreas Herrmann * power is in Watt provided as fixed point integer with 91512d1027SAndreas Herrmann * scaling factor 1/(2^16). For conversion we use 92512d1027SAndreas Herrmann * (10^6)/(2^16) = 15625/(2^10) 93512d1027SAndreas Herrmann */ 94941a956bSAndre Przywara curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range); 95512d1027SAndreas Herrmann return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts); 96512d1027SAndreas Herrmann } 97512d1027SAndreas Herrmann static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL); 98512d1027SAndreas Herrmann 99512d1027SAndreas Herrmann static ssize_t show_power_crit(struct device *dev, 100512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 101512d1027SAndreas Herrmann { 102512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 103512d1027SAndreas Herrmann 104512d1027SAndreas Herrmann return sprintf(buf, "%u\n", data->processor_pwr_watts); 105512d1027SAndreas Herrmann } 106512d1027SAndreas Herrmann static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL); 107512d1027SAndreas Herrmann 108961a2378SAravind Gopalakrishnan static umode_t fam15h_power_is_visible(struct kobject *kobj, 109961a2378SAravind Gopalakrishnan struct attribute *attr, 110961a2378SAravind Gopalakrishnan int index) 111961a2378SAravind Gopalakrishnan { 112961a2378SAravind Gopalakrishnan /* power1_input is only reported for Fam15h, Models 00h-0fh */ 113961a2378SAravind Gopalakrishnan if (attr == &dev_attr_power1_input.attr && 114961a2378SAravind Gopalakrishnan (boot_cpu_data.x86 != 0x15 || boot_cpu_data.x86_model > 0xf)) 115961a2378SAravind Gopalakrishnan return 0; 116961a2378SAravind Gopalakrishnan 117961a2378SAravind Gopalakrishnan return attr->mode; 118961a2378SAravind Gopalakrishnan } 119961a2378SAravind Gopalakrishnan 120512d1027SAndreas Herrmann static struct attribute *fam15h_power_attrs[] = { 121512d1027SAndreas Herrmann &dev_attr_power1_input.attr, 122512d1027SAndreas Herrmann &dev_attr_power1_crit.attr, 123512d1027SAndreas Herrmann NULL 124512d1027SAndreas Herrmann }; 125512d1027SAndreas Herrmann 126961a2378SAravind Gopalakrishnan static const struct attribute_group fam15h_power_group = { 127961a2378SAravind Gopalakrishnan .attrs = fam15h_power_attrs, 128961a2378SAravind Gopalakrishnan .is_visible = fam15h_power_is_visible, 129961a2378SAravind Gopalakrishnan }; 130961a2378SAravind Gopalakrishnan __ATTRIBUTE_GROUPS(fam15h_power); 131512d1027SAndreas Herrmann 132d83e92b3SHuang Rui static bool should_load_on_this_node(struct pci_dev *f4) 133512d1027SAndreas Herrmann { 134512d1027SAndreas Herrmann u32 val; 135512d1027SAndreas Herrmann 136512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3), 137512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val); 138512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3)) 139512d1027SAndreas Herrmann return false; 140512d1027SAndreas Herrmann 141512d1027SAndreas Herrmann return true; 142512d1027SAndreas Herrmann } 143512d1027SAndreas Herrmann 14400250ec9SAndre Przywara /* 14500250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly 14600250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids 14700250ec9SAndre Przywara * counter saturations resulting in bogus power readings. 14800250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes. 14900250ec9SAndre Przywara */ 1505f0ecb90SAndreas Herrmann static const struct pci_device_id affected_device[] = { 151c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 152c3e40a99SGuenter Roeck { 0 } 153c3e40a99SGuenter Roeck }; 154c3e40a99SGuenter Roeck 1555f0ecb90SAndreas Herrmann static void tweak_runavg_range(struct pci_dev *pdev) 15600250ec9SAndre Przywara { 15700250ec9SAndre Przywara u32 val; 15800250ec9SAndre Przywara 15900250ec9SAndre Przywara /* 16000250ec9SAndre Przywara * let this quirk apply only to the current version of the 16100250ec9SAndre Przywara * northbridge, since future versions may change the behavior 16200250ec9SAndre Przywara */ 163c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev)) 16400250ec9SAndre Przywara return; 16500250ec9SAndre Przywara 16600250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus, 16700250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 16800250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val); 16900250ec9SAndre Przywara if ((val & 0xf) != 0xe) 17000250ec9SAndre Przywara return; 17100250ec9SAndre Przywara 17200250ec9SAndre Przywara val &= ~0xf; 17300250ec9SAndre Przywara val |= 0x9; 17400250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus, 17500250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 17600250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val); 17700250ec9SAndre Przywara } 17800250ec9SAndre Przywara 1795f0ecb90SAndreas Herrmann #ifdef CONFIG_PM 1805f0ecb90SAndreas Herrmann static int fam15h_power_resume(struct pci_dev *pdev) 1815f0ecb90SAndreas Herrmann { 1825f0ecb90SAndreas Herrmann tweak_runavg_range(pdev); 1835f0ecb90SAndreas Herrmann return 0; 1845f0ecb90SAndreas Herrmann } 1855f0ecb90SAndreas Herrmann #else 1865f0ecb90SAndreas Herrmann #define fam15h_power_resume NULL 1875f0ecb90SAndreas Herrmann #endif 1885f0ecb90SAndreas Herrmann 1896c931ae1SBill Pemberton static void fam15h_power_init_data(struct pci_dev *f4, 190512d1027SAndreas Herrmann struct fam15h_power_data *data) 191512d1027SAndreas Herrmann { 192*1ed32160SHuang Rui u32 val, eax, ebx, ecx, edx; 193512d1027SAndreas Herrmann u64 tmp; 194512d1027SAndreas Herrmann 195512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val); 196512d1027SAndreas Herrmann data->base_tdp = val >> 16; 197512d1027SAndreas Herrmann tmp = val & 0xffff; 198512d1027SAndreas Herrmann 199512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 200512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 201512d1027SAndreas Herrmann 202512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f); 203512d1027SAndreas Herrmann tmp *= data->tdp_to_watts; 204512d1027SAndreas Herrmann 205512d1027SAndreas Herrmann /* result not allowed to be >= 256W */ 206512d1027SAndreas Herrmann if ((tmp >> 16) >= 256) 207b55f3757SGuenter Roeck dev_warn(&f4->dev, 208b55f3757SGuenter Roeck "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n", 209512d1027SAndreas Herrmann (unsigned int) (tmp >> 16)); 210512d1027SAndreas Herrmann 211512d1027SAndreas Herrmann /* convert to microWatt */ 212512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10; 213*1ed32160SHuang Rui 214*1ed32160SHuang Rui cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 215*1ed32160SHuang Rui 216*1ed32160SHuang Rui /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */ 217*1ed32160SHuang Rui if (!(edx & BIT(12))) 218*1ed32160SHuang Rui return; 219*1ed32160SHuang Rui 220*1ed32160SHuang Rui /* 221*1ed32160SHuang Rui * determine the ratio of the compute unit power accumulator 222*1ed32160SHuang Rui * sample period to the PTSC counter period by executing CPUID 223*1ed32160SHuang Rui * Fn8000_0007:ECX 224*1ed32160SHuang Rui */ 225*1ed32160SHuang Rui data->cpu_pwr_sample_ratio = ecx; 226512d1027SAndreas Herrmann } 227512d1027SAndreas Herrmann 2286c931ae1SBill Pemberton static int fam15h_power_probe(struct pci_dev *pdev, 229512d1027SAndreas Herrmann const struct pci_device_id *id) 230512d1027SAndreas Herrmann { 231512d1027SAndreas Herrmann struct fam15h_power_data *data; 23287432a2eSGuenter Roeck struct device *dev = &pdev->dev; 233562dc973SAxel Lin struct device *hwmon_dev; 234512d1027SAndreas Herrmann 23500250ec9SAndre Przywara /* 23600250ec9SAndre Przywara * though we ignore every other northbridge, we still have to 23700250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters 23800250ec9SAndre Przywara * are working hand-in-hand 23900250ec9SAndre Przywara */ 24000250ec9SAndre Przywara tweak_runavg_range(pdev); 24100250ec9SAndre Przywara 242d83e92b3SHuang Rui if (!should_load_on_this_node(pdev)) 24387432a2eSGuenter Roeck return -ENODEV; 244512d1027SAndreas Herrmann 24587432a2eSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL); 24687432a2eSGuenter Roeck if (!data) 24787432a2eSGuenter Roeck return -ENOMEM; 24887432a2eSGuenter Roeck 249512d1027SAndreas Herrmann fam15h_power_init_data(pdev, data); 250562dc973SAxel Lin data->pdev = pdev; 251512d1027SAndreas Herrmann 252562dc973SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power", 253562dc973SAxel Lin data, 254562dc973SAxel Lin fam15h_power_groups); 255562dc973SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev); 256512d1027SAndreas Herrmann } 257512d1027SAndreas Herrmann 258cd9bb056SJingoo Han static const struct pci_device_id fam15h_power_id_table[] = { 259512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 2600a0039adSAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) }, 2615dc08725SHuang Rui { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) }, 26222e32f4fSBoris Ostrovsky { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) }, 2630bd52941SAravind Gopalakrishnan { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) }, 264512d1027SAndreas Herrmann {} 265512d1027SAndreas Herrmann }; 266512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table); 267512d1027SAndreas Herrmann 268512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = { 269512d1027SAndreas Herrmann .name = "fam15h_power", 270512d1027SAndreas Herrmann .id_table = fam15h_power_id_table, 271512d1027SAndreas Herrmann .probe = fam15h_power_probe, 2725f0ecb90SAndreas Herrmann .resume = fam15h_power_resume, 273512d1027SAndreas Herrmann }; 274512d1027SAndreas Herrmann 275f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver); 276