1512d1027SAndreas Herrmann /* 2512d1027SAndreas Herrmann * fam15h_power.c - AMD Family 15h processor power monitoring 3512d1027SAndreas Herrmann * 4512d1027SAndreas Herrmann * Copyright (c) 2011 Advanced Micro Devices, Inc. 5512d1027SAndreas Herrmann * Author: Andreas Herrmann <andreas.herrmann3@amd.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"); 31512d1027SAndreas Herrmann MODULE_AUTHOR("Andreas Herrmann <andreas.herrmann3@amd.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 { 45512d1027SAndreas Herrmann struct device *hwmon_dev; 46512d1027SAndreas Herrmann unsigned int tdp_to_watts; 47512d1027SAndreas Herrmann unsigned int base_tdp; 48512d1027SAndreas Herrmann unsigned int processor_pwr_watts; 49512d1027SAndreas Herrmann }; 50512d1027SAndreas Herrmann 51512d1027SAndreas Herrmann static ssize_t show_power(struct device *dev, 52512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 53512d1027SAndreas Herrmann { 54512d1027SAndreas Herrmann u32 val, tdp_limit, running_avg_range; 55512d1027SAndreas Herrmann s32 running_avg_capture; 56512d1027SAndreas Herrmann u64 curr_pwr_watts; 57512d1027SAndreas Herrmann struct pci_dev *f4 = to_pci_dev(dev); 58512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 59512d1027SAndreas Herrmann 60512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 61512d1027SAndreas Herrmann REG_TDP_RUNNING_AVERAGE, &val); 62512d1027SAndreas Herrmann running_avg_capture = (val >> 4) & 0x3fffff; 63fc0900cbSAndreas Herrmann running_avg_capture = sign_extend32(running_avg_capture, 21); 64941a956bSAndre Przywara running_avg_range = (val & 0xf) + 1; 65512d1027SAndreas Herrmann 66512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 67512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 68512d1027SAndreas Herrmann 69512d1027SAndreas Herrmann tdp_limit = val >> 16; 70941a956bSAndre Przywara curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range; 71941a956bSAndre Przywara curr_pwr_watts -= running_avg_capture; 72512d1027SAndreas Herrmann curr_pwr_watts *= data->tdp_to_watts; 73512d1027SAndreas Herrmann 74512d1027SAndreas Herrmann /* 75512d1027SAndreas Herrmann * Convert to microWatt 76512d1027SAndreas Herrmann * 77512d1027SAndreas Herrmann * power is in Watt provided as fixed point integer with 78512d1027SAndreas Herrmann * scaling factor 1/(2^16). For conversion we use 79512d1027SAndreas Herrmann * (10^6)/(2^16) = 15625/(2^10) 80512d1027SAndreas Herrmann */ 81941a956bSAndre Przywara curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range); 82512d1027SAndreas Herrmann return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts); 83512d1027SAndreas Herrmann } 84512d1027SAndreas Herrmann static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL); 85512d1027SAndreas Herrmann 86512d1027SAndreas Herrmann static ssize_t show_power_crit(struct device *dev, 87512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 88512d1027SAndreas Herrmann { 89512d1027SAndreas Herrmann struct fam15h_power_data *data = dev_get_drvdata(dev); 90512d1027SAndreas Herrmann 91512d1027SAndreas Herrmann return sprintf(buf, "%u\n", data->processor_pwr_watts); 92512d1027SAndreas Herrmann } 93512d1027SAndreas Herrmann static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL); 94512d1027SAndreas Herrmann 95512d1027SAndreas Herrmann static ssize_t show_name(struct device *dev, 96512d1027SAndreas Herrmann struct device_attribute *attr, char *buf) 97512d1027SAndreas Herrmann { 98512d1027SAndreas Herrmann return sprintf(buf, "fam15h_power\n"); 99512d1027SAndreas Herrmann } 100512d1027SAndreas Herrmann static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 101512d1027SAndreas Herrmann 102512d1027SAndreas Herrmann static struct attribute *fam15h_power_attrs[] = { 103512d1027SAndreas Herrmann &dev_attr_power1_input.attr, 104512d1027SAndreas Herrmann &dev_attr_power1_crit.attr, 105512d1027SAndreas Herrmann &dev_attr_name.attr, 106512d1027SAndreas Herrmann NULL 107512d1027SAndreas Herrmann }; 108512d1027SAndreas Herrmann 109512d1027SAndreas Herrmann static const struct attribute_group fam15h_power_attr_group = { 110512d1027SAndreas Herrmann .attrs = fam15h_power_attrs, 111512d1027SAndreas Herrmann }; 112512d1027SAndreas Herrmann 113512d1027SAndreas Herrmann static bool __devinit fam15h_power_is_internal_node0(struct pci_dev *f4) 114512d1027SAndreas Herrmann { 115512d1027SAndreas Herrmann u32 val; 116512d1027SAndreas Herrmann 117512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3), 118512d1027SAndreas Herrmann REG_NORTHBRIDGE_CAP, &val); 119512d1027SAndreas Herrmann if ((val & BIT(29)) && ((val >> 30) & 3)) 120512d1027SAndreas Herrmann return false; 121512d1027SAndreas Herrmann 122512d1027SAndreas Herrmann return true; 123512d1027SAndreas Herrmann } 124512d1027SAndreas Herrmann 12500250ec9SAndre Przywara /* 12600250ec9SAndre Przywara * Newer BKDG versions have an updated recommendation on how to properly 12700250ec9SAndre Przywara * initialize the running average range (was: 0xE, now: 0x9). This avoids 12800250ec9SAndre Przywara * counter saturations resulting in bogus power readings. 12900250ec9SAndre Przywara * We correct this value ourselves to cope with older BIOSes. 13000250ec9SAndre Przywara */ 131c3e40a99SGuenter Roeck static DEFINE_PCI_DEVICE_TABLE(affected_device) = { 132c3e40a99SGuenter Roeck { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 133c3e40a99SGuenter Roeck { 0 } 134c3e40a99SGuenter Roeck }; 135c3e40a99SGuenter Roeck 13600250ec9SAndre Przywara static void __devinit tweak_runavg_range(struct pci_dev *pdev) 13700250ec9SAndre Przywara { 13800250ec9SAndre Przywara u32 val; 13900250ec9SAndre Przywara 14000250ec9SAndre Przywara /* 14100250ec9SAndre Przywara * let this quirk apply only to the current version of the 14200250ec9SAndre Przywara * northbridge, since future versions may change the behavior 14300250ec9SAndre Przywara */ 144c3e40a99SGuenter Roeck if (!pci_match_id(affected_device, pdev)) 14500250ec9SAndre Przywara return; 14600250ec9SAndre Przywara 14700250ec9SAndre Przywara pci_bus_read_config_dword(pdev->bus, 14800250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 14900250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, &val); 15000250ec9SAndre Przywara if ((val & 0xf) != 0xe) 15100250ec9SAndre Przywara return; 15200250ec9SAndre Przywara 15300250ec9SAndre Przywara val &= ~0xf; 15400250ec9SAndre Przywara val |= 0x9; 15500250ec9SAndre Przywara pci_bus_write_config_dword(pdev->bus, 15600250ec9SAndre Przywara PCI_DEVFN(PCI_SLOT(pdev->devfn), 5), 15700250ec9SAndre Przywara REG_TDP_RUNNING_AVERAGE, val); 15800250ec9SAndre Przywara } 15900250ec9SAndre Przywara 160512d1027SAndreas Herrmann static void __devinit fam15h_power_init_data(struct pci_dev *f4, 161512d1027SAndreas Herrmann struct fam15h_power_data *data) 162512d1027SAndreas Herrmann { 163512d1027SAndreas Herrmann u32 val; 164512d1027SAndreas Herrmann u64 tmp; 165512d1027SAndreas Herrmann 166512d1027SAndreas Herrmann pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val); 167512d1027SAndreas Herrmann data->base_tdp = val >> 16; 168512d1027SAndreas Herrmann tmp = val & 0xffff; 169512d1027SAndreas Herrmann 170512d1027SAndreas Herrmann pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5), 171512d1027SAndreas Herrmann REG_TDP_LIMIT3, &val); 172512d1027SAndreas Herrmann 173512d1027SAndreas Herrmann data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f); 174512d1027SAndreas Herrmann tmp *= data->tdp_to_watts; 175512d1027SAndreas Herrmann 176512d1027SAndreas Herrmann /* result not allowed to be >= 256W */ 177512d1027SAndreas Herrmann if ((tmp >> 16) >= 256) 178512d1027SAndreas Herrmann dev_warn(&f4->dev, "Bogus value for ProcessorPwrWatts " 179512d1027SAndreas Herrmann "(processor_pwr_watts>=%u)\n", 180512d1027SAndreas Herrmann (unsigned int) (tmp >> 16)); 181512d1027SAndreas Herrmann 182512d1027SAndreas Herrmann /* convert to microWatt */ 183512d1027SAndreas Herrmann data->processor_pwr_watts = (tmp * 15625) >> 10; 184512d1027SAndreas Herrmann } 185512d1027SAndreas Herrmann 186512d1027SAndreas Herrmann static int __devinit fam15h_power_probe(struct pci_dev *pdev, 187512d1027SAndreas Herrmann const struct pci_device_id *id) 188512d1027SAndreas Herrmann { 189512d1027SAndreas Herrmann struct fam15h_power_data *data; 190512d1027SAndreas Herrmann struct device *dev; 191512d1027SAndreas Herrmann int err; 192512d1027SAndreas Herrmann 19300250ec9SAndre Przywara /* 19400250ec9SAndre Przywara * though we ignore every other northbridge, we still have to 19500250ec9SAndre Przywara * do the tweaking on _each_ node in MCM processors as the counters 19600250ec9SAndre Przywara * are working hand-in-hand 19700250ec9SAndre Przywara */ 19800250ec9SAndre Przywara tweak_runavg_range(pdev); 19900250ec9SAndre Przywara 200512d1027SAndreas Herrmann if (!fam15h_power_is_internal_node0(pdev)) { 201512d1027SAndreas Herrmann err = -ENODEV; 202512d1027SAndreas Herrmann goto exit; 203512d1027SAndreas Herrmann } 204512d1027SAndreas Herrmann 205512d1027SAndreas Herrmann data = kzalloc(sizeof(struct fam15h_power_data), GFP_KERNEL); 206512d1027SAndreas Herrmann if (!data) { 207512d1027SAndreas Herrmann err = -ENOMEM; 208512d1027SAndreas Herrmann goto exit; 209512d1027SAndreas Herrmann } 210512d1027SAndreas Herrmann fam15h_power_init_data(pdev, data); 211512d1027SAndreas Herrmann dev = &pdev->dev; 212512d1027SAndreas Herrmann 213512d1027SAndreas Herrmann dev_set_drvdata(dev, data); 214512d1027SAndreas Herrmann err = sysfs_create_group(&dev->kobj, &fam15h_power_attr_group); 215512d1027SAndreas Herrmann if (err) 216512d1027SAndreas Herrmann goto exit_free_data; 217512d1027SAndreas Herrmann 218512d1027SAndreas Herrmann data->hwmon_dev = hwmon_device_register(dev); 219512d1027SAndreas Herrmann if (IS_ERR(data->hwmon_dev)) { 220512d1027SAndreas Herrmann err = PTR_ERR(data->hwmon_dev); 221512d1027SAndreas Herrmann goto exit_remove_group; 222512d1027SAndreas Herrmann } 223512d1027SAndreas Herrmann 224512d1027SAndreas Herrmann return 0; 225512d1027SAndreas Herrmann 226512d1027SAndreas Herrmann exit_remove_group: 227512d1027SAndreas Herrmann sysfs_remove_group(&dev->kobj, &fam15h_power_attr_group); 228512d1027SAndreas Herrmann exit_free_data: 229512d1027SAndreas Herrmann kfree(data); 230512d1027SAndreas Herrmann exit: 231512d1027SAndreas Herrmann return err; 232512d1027SAndreas Herrmann } 233512d1027SAndreas Herrmann 234512d1027SAndreas Herrmann static void __devexit fam15h_power_remove(struct pci_dev *pdev) 235512d1027SAndreas Herrmann { 236512d1027SAndreas Herrmann struct device *dev; 237512d1027SAndreas Herrmann struct fam15h_power_data *data; 238512d1027SAndreas Herrmann 239512d1027SAndreas Herrmann dev = &pdev->dev; 240512d1027SAndreas Herrmann data = dev_get_drvdata(dev); 241512d1027SAndreas Herrmann hwmon_device_unregister(data->hwmon_dev); 242512d1027SAndreas Herrmann sysfs_remove_group(&dev->kobj, &fam15h_power_attr_group); 243512d1027SAndreas Herrmann dev_set_drvdata(dev, NULL); 244512d1027SAndreas Herrmann kfree(data); 245512d1027SAndreas Herrmann } 246512d1027SAndreas Herrmann 247512d1027SAndreas Herrmann static DEFINE_PCI_DEVICE_TABLE(fam15h_power_id_table) = { 248512d1027SAndreas Herrmann { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) }, 249512d1027SAndreas Herrmann {} 250512d1027SAndreas Herrmann }; 251512d1027SAndreas Herrmann MODULE_DEVICE_TABLE(pci, fam15h_power_id_table); 252512d1027SAndreas Herrmann 253512d1027SAndreas Herrmann static struct pci_driver fam15h_power_driver = { 254512d1027SAndreas Herrmann .name = "fam15h_power", 255512d1027SAndreas Herrmann .id_table = fam15h_power_id_table, 256512d1027SAndreas Herrmann .probe = fam15h_power_probe, 257512d1027SAndreas Herrmann .remove = __devexit_p(fam15h_power_remove), 258512d1027SAndreas Herrmann }; 259512d1027SAndreas Herrmann 260*f71f5a55SAxel Lin module_pci_driver(fam15h_power_driver); 261