xref: /openbmc/linux/drivers/hwmon/k10temp.c (revision 3c57e89b)
13c57e89bSClemens Ladisch /*
23c57e89bSClemens Ladisch  * k10temp.c - AMD Family 10h/11h processor hardware monitoring
33c57e89bSClemens Ladisch  *
43c57e89bSClemens Ladisch  * Copyright (c) 2009 Clemens Ladisch <clemens@ladisch.de>
53c57e89bSClemens Ladisch  *
63c57e89bSClemens Ladisch  *
73c57e89bSClemens Ladisch  * This driver is free software; you can redistribute it and/or
83c57e89bSClemens Ladisch  * modify it under the terms of the GNU General Public License; either
93c57e89bSClemens Ladisch  * version 2 of the License, or (at your option) any later version.
103c57e89bSClemens Ladisch  *
113c57e89bSClemens Ladisch  * This driver is distributed in the hope that it will be useful,
123c57e89bSClemens Ladisch  * but WITHOUT ANY WARRANTY; without even the implied warranty of
133c57e89bSClemens Ladisch  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
143c57e89bSClemens Ladisch  * See the GNU General Public License for more details.
153c57e89bSClemens Ladisch  *
163c57e89bSClemens Ladisch  * You should have received a copy of the GNU General Public License
173c57e89bSClemens Ladisch  * along with this driver; if not, see <http://www.gnu.org/licenses/>.
183c57e89bSClemens Ladisch  */
193c57e89bSClemens Ladisch 
203c57e89bSClemens Ladisch #include <linux/err.h>
213c57e89bSClemens Ladisch #include <linux/hwmon.h>
223c57e89bSClemens Ladisch #include <linux/hwmon-sysfs.h>
233c57e89bSClemens Ladisch #include <linux/init.h>
243c57e89bSClemens Ladisch #include <linux/module.h>
253c57e89bSClemens Ladisch #include <linux/pci.h>
263c57e89bSClemens Ladisch #include <asm/processor.h>
273c57e89bSClemens Ladisch 
283c57e89bSClemens Ladisch MODULE_DESCRIPTION("AMD Family 10h/11h CPU core temperature monitor");
293c57e89bSClemens Ladisch MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
303c57e89bSClemens Ladisch MODULE_LICENSE("GPL");
313c57e89bSClemens Ladisch 
323c57e89bSClemens Ladisch static bool force;
333c57e89bSClemens Ladisch module_param(force, bool, 0444);
343c57e89bSClemens Ladisch MODULE_PARM_DESC(force, "force loading on processors with erratum 319");
353c57e89bSClemens Ladisch 
363c57e89bSClemens Ladisch #define REG_HARDWARE_THERMAL_CONTROL	0x64
373c57e89bSClemens Ladisch #define  HTC_ENABLE			0x00000001
383c57e89bSClemens Ladisch 
393c57e89bSClemens Ladisch #define REG_REPORTED_TEMPERATURE	0xa4
403c57e89bSClemens Ladisch 
413c57e89bSClemens Ladisch #define REG_NORTHBRIDGE_CAPABILITIES	0xe8
423c57e89bSClemens Ladisch #define  NB_CAP_HTC			0x00000400
433c57e89bSClemens Ladisch 
443c57e89bSClemens Ladisch static ssize_t show_temp(struct device *dev,
453c57e89bSClemens Ladisch 			 struct device_attribute *attr, char *buf)
463c57e89bSClemens Ladisch {
473c57e89bSClemens Ladisch 	u32 regval;
483c57e89bSClemens Ladisch 
493c57e89bSClemens Ladisch 	pci_read_config_dword(to_pci_dev(dev),
503c57e89bSClemens Ladisch 			      REG_REPORTED_TEMPERATURE, &regval);
513c57e89bSClemens Ladisch 	return sprintf(buf, "%u\n", (regval >> 21) * 125);
523c57e89bSClemens Ladisch }
533c57e89bSClemens Ladisch 
543c57e89bSClemens Ladisch static ssize_t show_temp_max(struct device *dev,
553c57e89bSClemens Ladisch 			     struct device_attribute *attr, char *buf)
563c57e89bSClemens Ladisch {
573c57e89bSClemens Ladisch 	return sprintf(buf, "%d\n", 70 * 1000);
583c57e89bSClemens Ladisch }
593c57e89bSClemens Ladisch 
603c57e89bSClemens Ladisch static ssize_t show_temp_crit(struct device *dev,
613c57e89bSClemens Ladisch 			      struct device_attribute *devattr, char *buf)
623c57e89bSClemens Ladisch {
633c57e89bSClemens Ladisch 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
643c57e89bSClemens Ladisch 	int show_hyst = attr->index;
653c57e89bSClemens Ladisch 	u32 regval;
663c57e89bSClemens Ladisch 	int value;
673c57e89bSClemens Ladisch 
683c57e89bSClemens Ladisch 	pci_read_config_dword(to_pci_dev(dev),
693c57e89bSClemens Ladisch 			      REG_HARDWARE_THERMAL_CONTROL, &regval);
703c57e89bSClemens Ladisch 	value = ((regval >> 16) & 0x7f) * 500 + 52000;
713c57e89bSClemens Ladisch 	if (show_hyst)
723c57e89bSClemens Ladisch 		value -= ((regval >> 24) & 0xf) * 500;
733c57e89bSClemens Ladisch 	return sprintf(buf, "%d\n", value);
743c57e89bSClemens Ladisch }
753c57e89bSClemens Ladisch 
763c57e89bSClemens Ladisch static ssize_t show_name(struct device *dev,
773c57e89bSClemens Ladisch 			 struct device_attribute *attr, char *buf)
783c57e89bSClemens Ladisch {
793c57e89bSClemens Ladisch 	return sprintf(buf, "k10temp\n");
803c57e89bSClemens Ladisch }
813c57e89bSClemens Ladisch 
823c57e89bSClemens Ladisch static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
833c57e89bSClemens Ladisch static DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL);
843c57e89bSClemens Ladisch static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
853c57e89bSClemens Ladisch static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, show_temp_crit, NULL, 1);
863c57e89bSClemens Ladisch static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
873c57e89bSClemens Ladisch 
883c57e89bSClemens Ladisch static bool __devinit has_erratum_319(void)
893c57e89bSClemens Ladisch {
903c57e89bSClemens Ladisch 	/*
913c57e89bSClemens Ladisch 	 * Erratum 319: The thermal sensor of older Family 10h processors
923c57e89bSClemens Ladisch 	 *              (B steppings) may be unreliable.
933c57e89bSClemens Ladisch 	 */
943c57e89bSClemens Ladisch 	return boot_cpu_data.x86 == 0x10 && boot_cpu_data.x86_model <= 2;
953c57e89bSClemens Ladisch }
963c57e89bSClemens Ladisch 
973c57e89bSClemens Ladisch static int __devinit k10temp_probe(struct pci_dev *pdev,
983c57e89bSClemens Ladisch 				   const struct pci_device_id *id)
993c57e89bSClemens Ladisch {
1003c57e89bSClemens Ladisch 	struct device *hwmon_dev;
1013c57e89bSClemens Ladisch 	u32 reg_caps, reg_htc;
1023c57e89bSClemens Ladisch 	int err;
1033c57e89bSClemens Ladisch 
1043c57e89bSClemens Ladisch 	if (has_erratum_319() && !force) {
1053c57e89bSClemens Ladisch 		dev_err(&pdev->dev,
1063c57e89bSClemens Ladisch 			"unreliable CPU thermal sensor; monitoring disabled\n");
1073c57e89bSClemens Ladisch 		err = -ENODEV;
1083c57e89bSClemens Ladisch 		goto exit;
1093c57e89bSClemens Ladisch 	}
1103c57e89bSClemens Ladisch 
1113c57e89bSClemens Ladisch 	err = device_create_file(&pdev->dev, &dev_attr_temp1_input);
1123c57e89bSClemens Ladisch 	if (err)
1133c57e89bSClemens Ladisch 		goto exit;
1143c57e89bSClemens Ladisch 	err = device_create_file(&pdev->dev, &dev_attr_temp1_max);
1153c57e89bSClemens Ladisch 	if (err)
1163c57e89bSClemens Ladisch 		goto exit_remove;
1173c57e89bSClemens Ladisch 
1183c57e89bSClemens Ladisch 	pci_read_config_dword(pdev, REG_NORTHBRIDGE_CAPABILITIES, &reg_caps);
1193c57e89bSClemens Ladisch 	pci_read_config_dword(pdev, REG_HARDWARE_THERMAL_CONTROL, &reg_htc);
1203c57e89bSClemens Ladisch 	if ((reg_caps & NB_CAP_HTC) && (reg_htc & HTC_ENABLE)) {
1213c57e89bSClemens Ladisch 		err = device_create_file(&pdev->dev,
1223c57e89bSClemens Ladisch 				&sensor_dev_attr_temp1_crit.dev_attr);
1233c57e89bSClemens Ladisch 		if (err)
1243c57e89bSClemens Ladisch 			goto exit_remove;
1253c57e89bSClemens Ladisch 		err = device_create_file(&pdev->dev,
1263c57e89bSClemens Ladisch 				&sensor_dev_attr_temp1_crit_hyst.dev_attr);
1273c57e89bSClemens Ladisch 		if (err)
1283c57e89bSClemens Ladisch 			goto exit_remove;
1293c57e89bSClemens Ladisch 	}
1303c57e89bSClemens Ladisch 
1313c57e89bSClemens Ladisch 	err = device_create_file(&pdev->dev, &dev_attr_name);
1323c57e89bSClemens Ladisch 	if (err)
1333c57e89bSClemens Ladisch 		goto exit_remove;
1343c57e89bSClemens Ladisch 
1353c57e89bSClemens Ladisch 	hwmon_dev = hwmon_device_register(&pdev->dev);
1363c57e89bSClemens Ladisch 	if (IS_ERR(hwmon_dev)) {
1373c57e89bSClemens Ladisch 		err = PTR_ERR(hwmon_dev);
1383c57e89bSClemens Ladisch 		goto exit_remove;
1393c57e89bSClemens Ladisch 	}
1403c57e89bSClemens Ladisch 	dev_set_drvdata(&pdev->dev, hwmon_dev);
1413c57e89bSClemens Ladisch 
1423c57e89bSClemens Ladisch 	if (has_erratum_319() && force)
1433c57e89bSClemens Ladisch 		dev_warn(&pdev->dev,
1443c57e89bSClemens Ladisch 			 "unreliable CPU thermal sensor; check erratum 319\n");
1453c57e89bSClemens Ladisch 	return 0;
1463c57e89bSClemens Ladisch 
1473c57e89bSClemens Ladisch exit_remove:
1483c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev, &dev_attr_name);
1493c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev, &dev_attr_temp1_input);
1503c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev, &dev_attr_temp1_max);
1513c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev,
1523c57e89bSClemens Ladisch 			   &sensor_dev_attr_temp1_crit.dev_attr);
1533c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev,
1543c57e89bSClemens Ladisch 			   &sensor_dev_attr_temp1_crit_hyst.dev_attr);
1553c57e89bSClemens Ladisch exit:
1563c57e89bSClemens Ladisch 	return err;
1573c57e89bSClemens Ladisch }
1583c57e89bSClemens Ladisch 
1593c57e89bSClemens Ladisch static void __devexit k10temp_remove(struct pci_dev *pdev)
1603c57e89bSClemens Ladisch {
1613c57e89bSClemens Ladisch 	hwmon_device_unregister(dev_get_drvdata(&pdev->dev));
1623c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev, &dev_attr_name);
1633c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev, &dev_attr_temp1_input);
1643c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev, &dev_attr_temp1_max);
1653c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev,
1663c57e89bSClemens Ladisch 			   &sensor_dev_attr_temp1_crit.dev_attr);
1673c57e89bSClemens Ladisch 	device_remove_file(&pdev->dev,
1683c57e89bSClemens Ladisch 			   &sensor_dev_attr_temp1_crit_hyst.dev_attr);
1693c57e89bSClemens Ladisch 	dev_set_drvdata(&pdev->dev, NULL);
1703c57e89bSClemens Ladisch }
1713c57e89bSClemens Ladisch 
1723c57e89bSClemens Ladisch static struct pci_device_id k10temp_id_table[] = {
1733c57e89bSClemens Ladisch 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_10H_NB_MISC) },
1743c57e89bSClemens Ladisch 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_11H_NB_MISC) },
1753c57e89bSClemens Ladisch 	{}
1763c57e89bSClemens Ladisch };
1773c57e89bSClemens Ladisch MODULE_DEVICE_TABLE(pci, k10temp_id_table);
1783c57e89bSClemens Ladisch 
1793c57e89bSClemens Ladisch static struct pci_driver k10temp_driver = {
1803c57e89bSClemens Ladisch 	.name = "k10temp",
1813c57e89bSClemens Ladisch 	.id_table = k10temp_id_table,
1823c57e89bSClemens Ladisch 	.probe = k10temp_probe,
1833c57e89bSClemens Ladisch 	.remove = __devexit_p(k10temp_remove),
1843c57e89bSClemens Ladisch };
1853c57e89bSClemens Ladisch 
1863c57e89bSClemens Ladisch static int __init k10temp_init(void)
1873c57e89bSClemens Ladisch {
1883c57e89bSClemens Ladisch 	return pci_register_driver(&k10temp_driver);
1893c57e89bSClemens Ladisch }
1903c57e89bSClemens Ladisch 
1913c57e89bSClemens Ladisch static void __exit k10temp_exit(void)
1923c57e89bSClemens Ladisch {
1933c57e89bSClemens Ladisch 	pci_unregister_driver(&k10temp_driver);
1943c57e89bSClemens Ladisch }
1953c57e89bSClemens Ladisch 
1963c57e89bSClemens Ladisch module_init(k10temp_init)
1973c57e89bSClemens Ladisch module_exit(k10temp_exit)
198